File: option_vendorclass.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20250417.5f8cf70-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 1,096 kB
  • sloc: makefile: 3
file content (55 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download
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
package dhcpv6

import (
	"fmt"
	"strings"

	"github.com/u-root/uio/uio"
)

// OptVendorClass represents a DHCPv6 Vendor Class option
type OptVendorClass struct {
	EnterpriseNumber uint32
	Data             [][]byte
}

// Code returns the option code
func (op *OptVendorClass) Code() OptionCode {
	return OptionVendorClass
}

// ToBytes serializes the option and returns it as a sequence of bytes
func (op *OptVendorClass) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write32(op.EnterpriseNumber)
	for _, data := range op.Data {
		buf.Write16(uint16(len(data)))
		buf.WriteBytes(data)
	}
	return buf.Data()
}

// String returns a string representation of the VendorClass data
func (op *OptVendorClass) String() string {
	vcStrings := make([]string, 0)
	for _, data := range op.Data {
		vcStrings = append(vcStrings, string(data))
	}
	return fmt.Sprintf("%s: {EnterpriseNumber=%d Data=[%s]}", op.Code(), op.EnterpriseNumber, strings.Join(vcStrings, ", "))
}

// FromBytes builds an OptVendorClass structure from a sequence of bytes. The
// input data does not include option code and length bytes.
func (op *OptVendorClass) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	*op = OptVendorClass{}
	op.EnterpriseNumber = buf.Read32()
	for buf.Has(2) {
		len := buf.Read16()
		op.Data = append(op.Data, buf.CopyN(int(len)))
	}
	if len(op.Data) == 0 {
		return fmt.Errorf("%w: vendor class data should not be empty", uio.ErrBufferTooShort)
	}
	return buf.FinError()
}