summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_vivc.go
blob: 3900bf024ed0604af108932def3b4f34ae356fe5 (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/insomniacslk/dhcp/iana"
	"github.com/u-root/uio/uio"
)

// VIVCIdentifier implements the vendor-identifying vendor class option
// described by RFC 3925.
type VIVCIdentifier struct {
	// EntID is the enterprise ID.
	EntID iana.EnterpriseID
	Data  []byte
}

// OptVIVC returns a new vendor-identifying vendor class option.
//
// The option is described by RFC 3925.
func OptVIVC(identifiers ...VIVCIdentifier) Option {
	return Option{
		Code:  OptionVendorIdentifyingVendorClass,
		Value: VIVCIdentifiers(identifiers),
	}
}

// VIVCIdentifiers implements encoding and decoding methods for a DHCP option
// described in RFC 3925.
type VIVCIdentifiers []VIVCIdentifier

// FromBytes parses data into ids per RFC 3925.
func (ids *VIVCIdentifiers) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	for buf.Has(5) {
		entID := iana.EnterpriseID(buf.Read32())
		idLen := int(buf.Read8())
		*ids = append(*ids, VIVCIdentifier{EntID: entID, Data: buf.CopyN(idLen)})
	}
	return buf.FinError()
}

// ToBytes returns a serialized stream of bytes for this option.
func (ids VIVCIdentifiers) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	for _, id := range ids {
		buf.Write32(uint32(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 (ids VIVCIdentifiers) String() string {
	if len(ids) == 0 {
		return ""
	}
	buf := bytes.Buffer{}
	for _, id := range ids {
		fmt.Fprintf(&buf, " %d:'%s',", id.EntID, id.Data)
	}
	return buf.String()[1 : buf.Len()-1]
}