summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_domainsearchlist.go
blob: e3e9c17ade19f935e248439564fb78bd213ed8dc (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
package dhcpv6

// This module defines the OptDomainSearchList structure.
// https://www.ietf.org/rfc/rfc3646.txt

import (
	"encoding/binary"
	"fmt"
)

type OptDomainSearchList struct {
	domainSearchList []string
}

func (op *OptDomainSearchList) Code() OptionCode {
	return DOMAIN_SEARCH_LIST
}

func (op *OptDomainSearchList) ToBytes() []byte {
	buf := make([]byte, 4)
	binary.BigEndian.PutUint16(buf[0:2], uint16(DOMAIN_SEARCH_LIST))
	binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length()))
	buf = append(buf, LabelsToBytes(op.domainSearchList)...)
	return buf
}

func (op *OptDomainSearchList) DomainSearchList() []string {
	return op.domainSearchList
}

func (op *OptDomainSearchList) SetDomainSearchList(dsList []string) {
	op.domainSearchList = dsList
}

func (op *OptDomainSearchList) Length() int {
	var length int
	for _, label := range op.domainSearchList {
		length += len(label) + 2 // add the first and the last length bytes
	}
	return length
}

func (op *OptDomainSearchList) String() string {
	return fmt.Sprintf("OptDomainSearchList{searchlist=%v}", op.domainSearchList)
}

// build an OptDomainSearchList structure from a sequence of bytes.
// The input data does not include option code and length bytes.
func ParseOptDomainSearchList(data []byte) (*OptDomainSearchList, error) {
	opt := OptDomainSearchList{}
	var err error
	opt.domainSearchList, err = LabelsFromBytes(data)
	if err != nil {
		return nil, err
	}
	return &opt, nil
}