summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_domain_name_server.go
blob: 470eaa01b8a9d3ac15e97ea81f24586a366d269d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
package dhcpv4

import (
	"fmt"
	"net"
)

// This option implements the domain name server option
// https://tools.ietf.org/html/rfc2132

// OptDomainNameServer represents an option encapsulating the domain name
// servers.
type OptDomainNameServer struct {
	NameServers []net.IP
}

// ParseOptDomainNameServer returns a new OptDomainNameServer from a byte
// stream, or error if any.
func ParseOptDomainNameServer(data []byte) (*OptDomainNameServer, error) {
	if len(data) < 2 {
		return nil, ErrShortByteStream
	}
	code := OptionCode(data[0])
	if code != OptionDomainNameServer {
		return nil, fmt.Errorf("expected code %v, got %v", OptionDomainNameServer, code)
	}
	length := int(data[1])
	if length == 0 || length%4 != 0 {
		return nil, fmt.Errorf("Invalid length: expected multiple of 4 larger than 4, got %v", length)
	}
	if len(data) < 2+length {
		return nil, ErrShortByteStream
	}
	nameservers := make([]net.IP, 0, length%4)
	for idx := 0; idx < length; idx += 4 {
		b := data[2+idx : 2+idx+4]
		nameservers = append(nameservers, net.IPv4(b[0], b[1], b[2], b[3]))
	}
	return &OptDomainNameServer{NameServers: nameservers}, nil
}

// Code returns the option code.
func (o *OptDomainNameServer) Code() OptionCode {
	return OptionDomainNameServer
}

// ToBytes returns a serialized stream of bytes for this option.
func (o *OptDomainNameServer) ToBytes() []byte {
	ret := []byte{byte(o.Code()), byte(o.Length())}
	for _, ns := range o.NameServers {
		ret = append(ret, ns.To4()...)
	}
	return ret
}

// String returns a human-readable string.
func (o *OptDomainNameServer) String() string {
	var servers string
	for idx, ns := range o.NameServers {
		servers += ns.String()
		if idx < len(o.NameServers)-1 {
			servers += ", "
		}
	}
	return fmt.Sprintf("Domain Name Servers -> %v", servers)
}

// Length returns the length of the data portion (excluding option code an byte
// length).
func (o *OptDomainNameServer) Length() int {
	return len(o.NameServers) * 4
}