diff options
Diffstat (limited to 'dhcpv6/options/dnsrecursivenameserver.go')
-rw-r--r-- | dhcpv6/options/dnsrecursivenameserver.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/dhcpv6/options/dnsrecursivenameserver.go b/dhcpv6/options/dnsrecursivenameserver.go new file mode 100644 index 0000000..a28fd1d --- /dev/null +++ b/dhcpv6/options/dnsrecursivenameserver.go @@ -0,0 +1,59 @@ +package options + +// This module defines the OptDNSRecursiveNameServer structure. +// https://www.ietf.org/rfc/rfc3646.txt + +import ( + "encoding/binary" + "fmt" + "net" +) + +type OptDNSRecursiveNameServer struct { + nameServers []net.IP +} + +func (op *OptDNSRecursiveNameServer) Code() OptionCode { + return DNS_RECURSIVE_NAME_SERVER +} + +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 { + 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 +} + +func (op *OptDNSRecursiveNameServer) Length() int { + return len(op.nameServers) * net.IPv6len +} + +func (op *OptDNSRecursiveNameServer) String() string { + 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. +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") + } + 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 + return &opt, nil +} |