summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_vivc.go
blob: b0fe85d24bf96b5dd5423cbd63fe07c047d50be8 (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
package dhcpv4

import (
	"bytes"
	"fmt"

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

// This option implements the Vendor-Identifying Vendor Class Option
// https://tools.ietf.org/html/rfc3925

// VIVCIdentifier represents one Vendor-Identifying vendor class option.
type VIVCIdentifier struct {
	EntID uint32
	Data  []byte
}

// OptVIVC represents the DHCP message type option.
type OptVIVC struct {
	Identifiers []VIVCIdentifier
}

// ParseOptVIVC contructs an OptVIVC tsruct from a sequence of bytes and returns
// it, or an error.
func ParseOptVIVC(data []byte) (*OptVIVC, error) {
	buf := uio.NewBigEndianBuffer(data)

	var ids []VIVCIdentifier
	for buf.Has(5) {
		entID := buf.Read32()
		idLen := int(buf.Read8())
		ids = append(ids, VIVCIdentifier{EntID: entID, Data: buf.CopyN(idLen)})
	}

	return &OptVIVC{Identifiers: ids}, buf.FinError()
}

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

// ToBytes returns a serialized stream of bytes for this option.
func (o *OptVIVC) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	for _, id := range o.Identifiers {
		buf.Write32(id.EntID)
		buf.Write8(uint8(len(id.Data)))
		buf.WriteBytes(id.Data)
	}
	return buf.Data()
}

// String returns a human-readable string for this option.
func (o *OptVIVC) String() string {
	buf := bytes.Buffer{}
	fmt.Fprintf(&buf, "Vendor-Identifying Vendor Class ->")

	for _, id := range o.Identifiers {
		fmt.Fprintf(&buf, " %d:'%s',", id.EntID, id.Data)
	}

	return buf.String()[:buf.Len()-1]
}