diff options
author | Chris Koch <chrisko@google.com> | 2019-12-28 04:05:29 -0800 |
---|---|---|
committer | Chris K <c@chrisko.ch> | 2020-03-11 13:52:51 -0700 |
commit | 4d26b5e0b42f27fa41d872b15a7d2bc867787975 (patch) | |
tree | 08b8a584619b1ba4d1245fc1c0c8a184e193fdef /dhcpv6 | |
parent | 6469ef2f3d85469f0f7cbf3cb40c2f931e406397 (diff) |
v6: rename IAPD option to be shorter
Also removes superfluous GetOne/Del proxy functions.
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/dhcpv6message.go | 8 | ||||
-rw-r--r-- | dhcpv6/option_iapd.go | 61 | ||||
-rw-r--r-- | dhcpv6/option_iapd_test.go (renamed from dhcpv6/option_prefixdelegation_test.go) | 73 | ||||
-rw-r--r-- | dhcpv6/option_prefixdelegation.go | 72 | ||||
-rw-r--r-- | dhcpv6/options.go | 2 |
5 files changed, 76 insertions, 140 deletions
diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index cb0503f..7d96ce3 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -68,17 +68,17 @@ func (mo MessageOptions) OneIANA() *OptIANA { } // IAPD returns all Identity Association for Prefix Delegation options. -func (mo MessageOptions) IAPD() []*OptIAForPrefixDelegation { +func (mo MessageOptions) IAPD() []*OptIAPD { opts := mo.Get(OptionIAPD) - var ianas []*OptIAForPrefixDelegation + var ianas []*OptIAPD for _, o := range opts { - ianas = append(ianas, o.(*OptIAForPrefixDelegation)) + ianas = append(ianas, o.(*OptIAPD)) } return ianas } // OneIAPD returns the first IAPD option. -func (mo MessageOptions) OneIAPD() *OptIAForPrefixDelegation { +func (mo MessageOptions) OneIAPD() *OptIAPD { iapds := mo.IAPD() if len(iapds) == 0 { return nil diff --git a/dhcpv6/option_iapd.go b/dhcpv6/option_iapd.go new file mode 100644 index 0000000..03f3744 --- /dev/null +++ b/dhcpv6/option_iapd.go @@ -0,0 +1,61 @@ +package dhcpv6 + +import ( + "fmt" + "time" + + "github.com/u-root/u-root/pkg/uio" +) + +// OptIAPD implements the identity association for prefix +// delegation option defined by RFC 3633, Section 9. +type OptIAPD struct { + IaId [4]byte + T1 time.Duration + T2 time.Duration + Options Options +} + +// Code returns the option code +func (op *OptIAPD) Code() OptionCode { + return OptionIAPD +} + +// ToBytes serializes the option and returns it as a sequence of bytes +func (op *OptIAPD) ToBytes() []byte { + buf := uio.NewBigEndianBuffer(nil) + buf.WriteBytes(op.IaId[:]) + + t1 := Duration{op.T1} + t1.Marshal(buf) + t2 := Duration{op.T2} + t2.Marshal(buf) + + buf.WriteBytes(op.Options.ToBytes()) + return buf.Data() +} + +// String returns a string representation of the OptIAPD data +func (op *OptIAPD) String() string { + return fmt.Sprintf("OptIAPD{IAID=%v, t1=%v, t2=%v, options=%v}", + op.IaId, op.T1, op.T2, op.Options) +} + +// ParseOptIAPD builds an OptIAPD structure from a sequence of bytes. +// The input data does not include option code and length bytes. +func ParseOptIAPD(data []byte) (*OptIAPD, error) { + var opt OptIAPD + buf := uio.NewBigEndianBuffer(data) + buf.ReadBytes(opt.IaId[:]) + + var t1, t2 Duration + t1.Unmarshal(buf) + t2.Unmarshal(buf) + opt.T1 = t1.Duration + opt.T2 = t2.Duration + + if err := opt.Options.FromBytes(buf.ReadAll()); err != nil { + return nil, err + } + return &opt, buf.FinError() +} diff --git a/dhcpv6/option_prefixdelegation_test.go b/dhcpv6/option_iapd_test.go index 1fff283..babb5a7 100644 --- a/dhcpv6/option_prefixdelegation_test.go +++ b/dhcpv6/option_iapd_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegation(t *testing.T) { +func TestOptIAPDParseOptIAPD(t *testing.T) { data := []byte{ 1, 0, 0, 0, // IAID 0, 0, 0, 1, // T1 @@ -19,7 +19,7 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegation(t *testing.T) { 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix } - opt, err := ParseOptIAForPrefixDelegation(data) + opt, err := ParseOptIAPD(data) require.NoError(t, err) require.Equal(t, OptionIAPD, opt.Code()) require.Equal(t, [4]byte{1, 0, 0, 0}, opt.IaId) @@ -27,17 +27,17 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegation(t *testing.T) { require.Equal(t, 2*time.Second, opt.T2) } -func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidLength(t *testing.T) { +func TestOptIAPDParseOptIAPDInvalidLength(t *testing.T) { data := []byte{ 1, 0, 0, 0, // IAID 0, 0, 0, 1, // T1 // truncated from here } - _, err := ParseOptIAForPrefixDelegation(data) + _, err := ParseOptIAPD(data) require.Error(t, err) } -func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidOptions(t *testing.T) { +func TestOptIAPDParseOptIAPDInvalidOptions(t *testing.T) { data := []byte{ 1, 0, 0, 0, // IAID 0, 0, 0, 1, // T1 @@ -48,71 +48,18 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidOptions(t * 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // IAPrefix ipv6Prefix missing last byte } - _, err := ParseOptIAForPrefixDelegation(data) + _, err := ParseOptIAPD(data) require.Error(t, err) } -func TestOptIAForPrefixDelegationGetOneOption(t *testing.T) { - buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime - 36, // prefixLength - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix - } - oaddr, err := ParseOptIAPrefix(buf) - if err != nil { - t.Fatal(err) - } - opt := OptIAForPrefixDelegation{} - opt.Options = append(opt.Options, oaddr) - require.Equal(t, oaddr, opt.GetOneOption(OptionIAPrefix)) -} - -func TestOptIAForPrefixDelegationGetOneOptionMissingOpt(t *testing.T) { - buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime - 36, // prefixLength - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix - } - oaddr, err := ParseOptIAPrefix(buf) - if err != nil { - t.Fatal(err) - } - opt := OptIAForPrefixDelegation{} - opt.Options = append(opt.Options, oaddr) - require.Equal(t, nil, opt.GetOneOption(OptionDNSRecursiveNameServer)) -} - -func TestOptIAForPrefixDelegationDelOption(t *testing.T) { - optiana1 := OptIAForPrefixDelegation{} - optiana2 := OptIAForPrefixDelegation{} - optiaaddr := OptIAPrefix{} - optsc := OptStatusCode{} - - optiana1.Options = append(optiana1.Options, &optsc) - optiana1.Options = append(optiana1.Options, &optiaaddr) - optiana1.Options = append(optiana1.Options, &optiaaddr) - optiana1.DelOption(OptionIAPrefix) - require.Equal(t, len(optiana1.Options), 1) - require.Equal(t, optiana1.Options[0], &optsc) - - optiana2.Options = append(optiana2.Options, &optiaaddr) - optiana2.Options = append(optiana2.Options, &optsc) - optiana2.Options = append(optiana2.Options, &optiaaddr) - optiana2.DelOption(OptionIAPrefix) - require.Equal(t, len(optiana2.Options), 1) - require.Equal(t, optiana2.Options[0], &optsc) -} - -func TestOptIAForPrefixDelegationToBytes(t *testing.T) { +func TestOptIAPDToBytes(t *testing.T) { oaddr := OptIAPrefix{} oaddr.PreferredLifetime = 0xaabbccdd * time.Second oaddr.ValidLifetime = 0xeeff0011 * time.Second oaddr.SetPrefixLength(36) oaddr.SetIPv6Prefix(net.IPv6loopback) - opt := OptIAForPrefixDelegation{} + opt := OptIAPD{} opt.IaId = [4]byte{1, 2, 3, 4} opt.T1 = 12345 * time.Second opt.T2 = 54321 * time.Second @@ -131,7 +78,7 @@ func TestOptIAForPrefixDelegationToBytes(t *testing.T) { require.Equal(t, expected, opt.ToBytes()) } -func TestOptIAForPrefixDelegationString(t *testing.T) { +func TestOptIAPDString(t *testing.T) { data := []byte{ 1, 0, 0, 0, // IAID 0, 0, 0, 1, // T1 @@ -142,7 +89,7 @@ func TestOptIAForPrefixDelegationString(t *testing.T) { 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix } - opt, err := ParseOptIAForPrefixDelegation(data) + opt, err := ParseOptIAPD(data) require.NoError(t, err) str := opt.String() diff --git a/dhcpv6/option_prefixdelegation.go b/dhcpv6/option_prefixdelegation.go deleted file mode 100644 index fe72936..0000000 --- a/dhcpv6/option_prefixdelegation.go +++ /dev/null @@ -1,72 +0,0 @@ -package dhcpv6 - -import ( - "fmt" - "time" - - "github.com/u-root/u-root/pkg/uio" -) - -// OptIAForPrefixDelegation implements the identity association for prefix -// delegation option defined by RFC 3633, Section 9. -type OptIAForPrefixDelegation struct { - IaId [4]byte - T1 time.Duration - T2 time.Duration - Options Options -} - -// Code returns the option code -func (op *OptIAForPrefixDelegation) Code() OptionCode { - return OptionIAPD -} - -// ToBytes serializes the option and returns it as a sequence of bytes -func (op *OptIAForPrefixDelegation) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.WriteBytes(op.IaId[:]) - - t1 := Duration{op.T1} - t1.Marshal(buf) - t2 := Duration{op.T2} - t2.Marshal(buf) - - buf.WriteBytes(op.Options.ToBytes()) - return buf.Data() -} - -// String returns a string representation of the OptIAForPrefixDelegation data -func (op *OptIAForPrefixDelegation) String() string { - return fmt.Sprintf("OptIAForPrefixDelegation{IAID=%v, t1=%v, t2=%v, options=%v}", - op.IaId, op.T1, op.T2, op.Options) -} - -// GetOneOption will get an option of the give type from the Options field, if -// it is present. It will return `nil` otherwise -func (op *OptIAForPrefixDelegation) GetOneOption(code OptionCode) Option { - return op.Options.GetOne(code) -} - -// DelOption will remove all the options that match a Option code. -func (op *OptIAForPrefixDelegation) DelOption(code OptionCode) { - op.Options.Del(code) -} - -// build an OptIAForPrefixDelegation structure from a sequence of bytes. -// The input data does not include option code and length bytes. -func ParseOptIAForPrefixDelegation(data []byte) (*OptIAForPrefixDelegation, error) { - var opt OptIAForPrefixDelegation - buf := uio.NewBigEndianBuffer(data) - buf.ReadBytes(opt.IaId[:]) - - var t1, t2 Duration - t1.Unmarshal(buf) - t2.Unmarshal(buf) - opt.T1 = t1.Duration - opt.T2 = t2.Duration - - if err := opt.Options.FromBytes(buf.ReadAll()); err != nil { - return nil, err - } - return &opt, buf.FinError() -} diff --git a/dhcpv6/options.go b/dhcpv6/options.go index 752ca2c..aefdfc8 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -70,7 +70,7 @@ func ParseOption(code OptionCode, optData []byte) (Option, error) { case OptionDomainSearchList: opt, err = parseOptDomainSearchList(optData) case OptionIAPD: - opt, err = ParseOptIAForPrefixDelegation(optData) + opt, err = ParseOptIAPD(optData) case OptionIAPrefix: opt, err = ParseOptIAPrefix(optData) case OptionRemoteID: |