summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_archtype.go
blob: 83ebf0ece739eb14f04bc6fa6c9ddaa0d61b3c59 (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
package dhcpv6

import (
	"fmt"
	"strings"

	"github.com/insomniacslk/dhcp/iana"
	"github.com/u-root/u-root/pkg/uio"
)

// OptClientArchType represents an option CLIENT_ARCH_TYPE
//
// This module defines the OptClientArchType structure.
// https://www.ietf.org/rfc/rfc5970.txt
type OptClientArchType struct {
	ArchTypes []iana.Arch
}

func (op *OptClientArchType) Code() OptionCode {
	return OptionClientArchType
}

// ToBytes marshals the client arch type as defined by RFC 5970.
func (op *OptClientArchType) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	for _, at := range op.ArchTypes {
		buf.Write16(uint16(at))
	}
	return buf.Data()
}

func (op *OptClientArchType) String() string {
	atStrings := make([]string, 0)
	for _, at := range op.ArchTypes {
		atStrings = append(atStrings, at.String())
	}
	return fmt.Sprintf("OptClientArchType{archtype=%v}", strings.Join(atStrings, ", "))
}

// ParseOptClientArchType builds an OptClientArchType structure from
// a sequence of bytes The input data does not include option code and
// length bytes.
func ParseOptClientArchType(data []byte) (*OptClientArchType, error) {
	var opt OptClientArchType
	buf := uio.NewBigEndianBuffer(data)
	for buf.Has(2) {
		opt.ArchTypes = append(opt.ArchTypes, iana.Arch(buf.Read16()))
	}
	return &opt, buf.FinError()
}