diff options
author | insomniac <insomniacslk@users.noreply.github.com> | 2018-05-10 11:52:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-10 11:52:34 +0200 |
commit | eab1c08cbc637f5d370bf84da1feba8673fb7cc3 (patch) | |
tree | 8398d039145e3a390a51ad3983d94d38cd8a48fd /dhcpv6 | |
parent | f371c3544b9bdfd70ceab7d96895e6069cd46462 (diff) |
OptDNSRecursiveNameServer gets public fields and tests, removes setter and getter (#62)
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/option_dnsrecursivenameserver.go | 32 | ||||
-rw-r--r-- | dhcpv6/option_dnsrecursivenameserver_test.go | 35 |
2 files changed, 50 insertions, 17 deletions
diff --git a/dhcpv6/option_dnsrecursivenameserver.go b/dhcpv6/option_dnsrecursivenameserver.go index ef3c690..737a2d3 100644 --- a/dhcpv6/option_dnsrecursivenameserver.go +++ b/dhcpv6/option_dnsrecursivenameserver.go @@ -9,51 +9,49 @@ import ( "net" ) +// OptDNSRecursiveNameServer represents a DNS_RECURSIVE_NAME_SERVER option type OptDNSRecursiveNameServer struct { - nameServers []net.IP + NameServers []net.IP } +// Code returns the option code func (op *OptDNSRecursiveNameServer) Code() OptionCode { return DNS_RECURSIVE_NAME_SERVER } +// ToBytes returns the option serialized to bytes, including option code and +// length func (op *OptDNSRecursiveNameServer) ToBytes() []byte { buf := make([]byte, 4) binary.BigEndian.PutUint16(buf[0:2], uint16(DNS_RECURSIVE_NAME_SERVER)) binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - for _, ns := range op.nameServers { + for _, ns := range op.NameServers { buf = append(buf, ns...) } return buf } -func (op *OptDNSRecursiveNameServer) NameServers() []net.IP { - return op.nameServers -} - -func (op *OptDNSRecursiveNameServer) SetNameServers(ns []net.IP) { - op.nameServers = ns -} - +// Length returns the option length func (op *OptDNSRecursiveNameServer) Length() int { - return len(op.nameServers) * net.IPv6len + return len(op.NameServers) * net.IPv6len } func (op *OptDNSRecursiveNameServer) String() string { - return fmt.Sprintf("OptDNSRecursiveNameServer{nameservers=%v}", op.nameServers) + return fmt.Sprintf("OptDNSRecursiveNameServer{nameservers=%v}", op.NameServers) } -// build an OptDNSRecursiveNameServer structure from a sequence of bytes. -// The input data does not include option code and length bytes. +// ParseOptDNSRecursiveNameServer builds an OptDNSRecursiveNameServer structure +// from a sequence of bytes. The input data does not include option code and length +// bytes. func ParseOptDNSRecursiveNameServer(data []byte) (*OptDNSRecursiveNameServer, error) { - if len(data)%2 != 0 { - return nil, fmt.Errorf("Invalid OptDNSRecursiveNameServer data: length is not a multiple of 2") + if len(data)%net.IPv6len != 0 { + return nil, fmt.Errorf("Invalid OptDNSRecursiveNameServer data: length is not a multiple of %d", net.IPv6len) } opt := OptDNSRecursiveNameServer{} var nameServers []net.IP for i := 0; i < len(data); i += net.IPv6len { nameServers = append(nameServers, data[i:i+net.IPv6len]) } - opt.nameServers = nameServers + opt.NameServers = nameServers return &opt, nil } diff --git a/dhcpv6/option_dnsrecursivenameserver_test.go b/dhcpv6/option_dnsrecursivenameserver_test.go new file mode 100644 index 0000000..85bb982 --- /dev/null +++ b/dhcpv6/option_dnsrecursivenameserver_test.go @@ -0,0 +1,35 @@ +package dhcpv6 + +import ( + "net" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestParseOptDNSRecursiveNameServer(t *testing.T) { + data := []byte{ + 0x2a, 0x03, 0x28, 0x80, 0xff, 0xfe, 0x00, 0x0c, 0xfa, 0xce, 0xb0, 0x0c, 0x00, 0x00, 0x00, 0x35, + } + expected := []net.IP{ + net.IP(data), + } + opt, err := ParseOptDNSRecursiveNameServer(data) + require.NoError(t, err) + require.Equal(t, opt.NameServers, expected) + require.Equal(t, opt.Length(), 16) +} + +func TestOptDNSRecursiveNameServerToBytes(t *testing.T) { + ns1 := net.ParseIP("2a03:2880:fffe:c:face:b00c:0:35") + ns2 := net.ParseIP("2001:4860:4860::8888") + nameservers := []net.IP{ns1, ns2} + expected := []byte{ + 0, 23, // DNS_RECURSIVE_NAME_SERVER + 0, 32, // length + } + expected = append(expected, []byte(ns1)...) + expected = append(expected, []byte(ns2)...) + opt := OptDNSRecursiveNameServer{NameServers: nameservers} + require.Equal(t, opt.ToBytes(), expected) +} |