diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-10-15 22:11:38 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-10-15 22:11:38 +0100 |
commit | 12c15033719dec9aa6415b3c0e25c1fcd97cba42 (patch) | |
tree | cfc2a5a5989259482f08b30656972b93e9a18db8 /dhcpv6 | |
parent | f1e31f7484231a7c51b57dba85abd174f043cc1e (diff) |
OptIAForPrefixDelegation: make fields public (#176)
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/option_prefixdelegation.go | 80 | ||||
-rw-r--r-- | dhcpv6/option_prefixdelegation_test.go | 53 |
2 files changed, 35 insertions, 98 deletions
diff --git a/dhcpv6/option_prefixdelegation.go b/dhcpv6/option_prefixdelegation.go index 6cc22fd..87c027e 100644 --- a/dhcpv6/option_prefixdelegation.go +++ b/dhcpv6/option_prefixdelegation.go @@ -6,14 +6,13 @@ package dhcpv6 import ( "encoding/binary" "fmt" - "log" ) type OptIAForPrefixDelegation struct { - iaId [4]byte - t1 uint32 - t2 uint32 - options []Option + IaId [4]byte + T1 uint32 + T2 uint32 + Options []Option } // Code returns the option code @@ -26,66 +25,19 @@ func (op *OptIAForPrefixDelegation) ToBytes() []byte { buf := make([]byte, 16) binary.BigEndian.PutUint16(buf[0:2], uint16(OptionIAPD)) binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - copy(buf[4:8], op.iaId[:]) - binary.BigEndian.PutUint32(buf[8:12], op.t1) - binary.BigEndian.PutUint32(buf[12:16], op.t2) - for _, opt := range op.options { + copy(buf[4:8], op.IaId[:]) + binary.BigEndian.PutUint32(buf[8:12], op.T1) + binary.BigEndian.PutUint32(buf[12:16], op.T2) + for _, opt := range op.Options { buf = append(buf, opt.ToBytes()...) } return buf } -// IAID returns the identity association identifier for this option -func (op *OptIAForPrefixDelegation) IAID() []byte { - return op.iaId[:] -} - -// SetIAID sets the identity association identifier for this option -func (op *OptIAForPrefixDelegation) SetIAID(iaId [4]byte) { - op.iaId = iaId -} - -// T1 returns the T1 timer for this option -func (op *OptIAForPrefixDelegation) T1() uint32 { - return op.t1 -} - -// SetT1 sets the T1 timer for this option -func (op *OptIAForPrefixDelegation) SetT1(t1 uint32) { - op.t1 = t1 -} - -// T2 returns the T2 timer for this option -func (op *OptIAForPrefixDelegation) T2() uint32 { - return op.t2 -} - -// SetT2 sets the T2 timer for this option -func (op *OptIAForPrefixDelegation) SetT2(t2 uint32) { - op.t2 = t2 -} - -// Options serializes the options and returns them as a sequence of bytes -func (op *OptIAForPrefixDelegation) Options() []byte { - log.Printf("Warning: OptIAForPrefixDelegation.Options() is deprecated and will be changed to a public field") - buf := op.ToBytes() - return buf[16:] -} - -// SetOptions sets the options as a sequence of bytes -func (op *OptIAForPrefixDelegation) SetOptions(options []byte) error { - var err error - op.options, err = OptionsFromBytes(options) - if err != nil { - return err - } - return nil -} - // Length returns the option length func (op *OptIAForPrefixDelegation) Length() int { l := 12 - for _, opt := range op.options { + for _, opt := range op.Options { l += 4 + opt.Length() } return l @@ -94,18 +46,18 @@ func (op *OptIAForPrefixDelegation) Length() int { // 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) + 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 getOption(op.options, code) + return getOption(op.Options, code) } // DelOption will remove all the options that match a Option code. func (op *OptIAForPrefixDelegation) DelOption(code OptionCode) { - op.options = delOption(op.options, code) + op.Options = delOption(op.Options, code) } // build an OptIAForPrefixDelegation structure from a sequence of bytes. @@ -116,10 +68,10 @@ func ParseOptIAForPrefixDelegation(data []byte) (*OptIAForPrefixDelegation, erro if len(data) < 12 { return nil, fmt.Errorf("Invalid IA for Prefix Delegation data length. Expected at least 12 bytes, got %v", len(data)) } - copy(opt.iaId[:], data[:4]) - opt.t1 = binary.BigEndian.Uint32(data[4:8]) - opt.t2 = binary.BigEndian.Uint32(data[8:12]) - opt.options, err = OptionsFromBytes(data[12:]) + copy(opt.IaId[:], data[:4]) + opt.T1 = binary.BigEndian.Uint32(data[4:8]) + opt.T2 = binary.BigEndian.Uint32(data[8:12]) + opt.Options, err = OptionsFromBytes(data[12:]) if err != nil { return nil, err } diff --git a/dhcpv6/option_prefixdelegation_test.go b/dhcpv6/option_prefixdelegation_test.go index bf84038..cb90e25 100644 --- a/dhcpv6/option_prefixdelegation_test.go +++ b/dhcpv6/option_prefixdelegation_test.go @@ -21,9 +21,9 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegation(t *testing.T) { require.NoError(t, err) require.Equal(t, len(data), opt.Length()) require.Equal(t, OptionIAPD, opt.Code()) - require.Equal(t, []byte{1, 0, 0, 0}, opt.IAID()) - require.Equal(t, uint32(1), opt.T1()) - require.Equal(t, uint32(2), opt.T2()) + require.Equal(t, [4]byte{1, 0, 0, 0}, opt.IaId) + require.Equal(t, uint32(1), opt.T1) + require.Equal(t, uint32(2), opt.T2) } func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidLength(t *testing.T) { @@ -63,7 +63,7 @@ func TestOptIAForPrefixDelegationGetOneOption(t *testing.T) { t.Fatal(err) } opt := OptIAForPrefixDelegation{} - opt.SetOptions(oaddr.ToBytes()) + opt.Options = append(opt.Options, oaddr) require.Equal(t, oaddr, opt.GetOneOption(OptionIAPrefix)) } @@ -79,7 +79,7 @@ func TestOptIAForPrefixDelegationGetOneOptionMissingOpt(t *testing.T) { t.Fatal(err) } opt := OptIAForPrefixDelegation{} - opt.SetOptions(oaddr.ToBytes()) + opt.Options = append(opt.Options, oaddr) require.Equal(t, nil, opt.GetOneOption(OptionDNSRecursiveNameServer)) } @@ -89,22 +89,19 @@ func TestOptIAForPrefixDelegationDelOption(t *testing.T) { optiaaddr := OptIAPrefix{} optsc := OptStatusCode{} - var buf = make([]byte, 0) - - buf = append(buf, optsc.ToBytes()...) - buf = append(buf, optiaaddr.ToBytes()...) - buf = append(buf, optiaaddr.ToBytes()...) - optiana1.SetOptions(buf) + optiana1.Options = append(optiana1.Options, &optsc) + optiana1.Options = append(optiana1.Options, &optiaaddr) + optiana1.Options = append(optiana1.Options, &optiaaddr) optiana1.DelOption(OptionIAPrefix) - require.Equal(t, optiana1.Options(), optsc.ToBytes()) + require.Equal(t, len(optiana1.Options), 1) + require.Equal(t, optiana1.Options[0], &optsc) - buf = make([]byte, 0) - buf = append(buf, optiaaddr.ToBytes()...) - buf = append(buf, optsc.ToBytes()...) - buf = append(buf, optiaaddr.ToBytes()...) - optiana2.SetOptions(buf) + optiana2.Options = append(optiana2.Options, &optiaaddr) + optiana2.Options = append(optiana2.Options, &optsc) + optiana2.Options = append(optiana2.Options, &optiaaddr) optiana2.DelOption(OptionIAPrefix) - require.Equal(t, optiana2.Options(), optsc.ToBytes()) + require.Equal(t, len(optiana2.Options), 1) + require.Equal(t, optiana2.Options[0], &optsc) } func TestOptIAForPrefixDelegationToBytes(t *testing.T) { @@ -115,10 +112,10 @@ func TestOptIAForPrefixDelegationToBytes(t *testing.T) { oaddr.SetIPv6Prefix([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) opt := OptIAForPrefixDelegation{} - opt.SetIAID([4]byte{1, 2, 3, 4}) - opt.SetT1(12345) - opt.SetT2(54321) - opt.SetOptions(oaddr.ToBytes()) + opt.IaId = [4]byte{1, 2, 3, 4} + opt.T1 = 12345 + opt.T2 = 54321 + opt.Options = append(opt.Options, &oaddr) expected := []byte{ 0, 25, // OptionIAPD @@ -135,18 +132,6 @@ func TestOptIAForPrefixDelegationToBytes(t *testing.T) { require.Equal(t, expected, opt.ToBytes()) } -func TestOptIAForPrefixDelegationSetOptionsTooShort(t *testing.T) { - buf := []byte{ - 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length - 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime - 0xee, 0xff, // truncated half-way through validLifetime - } - - oaddr := OptIAForPrefixDelegation{} - err := oaddr.SetOptions(buf) - require.Error(t, err, "SetOptions() should return an error if invalid options are set") -} - func TestOptIAForPrefixDelegationString(t *testing.T) { data := []byte{ 1, 0, 0, 0, // IAID |