summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_dnsrecursivenameserver.go
blob: ef3c6905b9ed05234d30925b381724de225b266a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dhcpv6

// 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
}