summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_vivc.go
blob: 7576637190290f73e0934e4428f91a35c91d300d (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package dhcpv4

import (
	"bytes"
	"encoding/binary"
	"fmt"
)

// 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) {
	if len(data) < 2 {
		return nil, ErrShortByteStream
	}
	code := OptionCode(data[0])
	if code != OptionVendorIdentifyingVendorClass {
		return nil, fmt.Errorf("expected code %v, got %v", OptionVendorIdentifyingVendorClass, code)
	}

	length := int(data[1])
	if len(data) < 2+length {
		return nil, ErrShortByteStream
	}
	data = data[2:length+2]

	ids := []VIVCIdentifier{}
	for len(data) > 5 {
		entID := binary.BigEndian.Uint32(data[0:4])
		idLen := int(data[4])
		data = data[5:]

		if idLen > len(data) {
			return nil, ErrShortByteStream
		}

		ids = append(ids, VIVCIdentifier{EntID: entID, Data: data[:idLen]})
		data = data[idLen:]
	}

	if len(data) != 0 {
		return nil, ErrShortByteStream
	}

	return &OptVIVC{Identifiers: ids}, nil
}

// 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 := make([]byte, o.Length()+2)
	copy(buf[0:], []byte{byte(o.Code()), byte(o.Length())})

	b := buf[2:]
	for _, id := range o.Identifiers {
		binary.BigEndian.PutUint32(b[0:4], id.EntID)
		b[4] = byte(len(id.Data))
		copy(b[5:], id.Data)
		b = b[len(id.Data)+5:]
	}
	return buf
}

// 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]
}

// Length returns the length of the data portion (excluding option code and byte
// for length, if any).
func (o *OptVIVC) Length() int {
	n := 0
	for _, id := range o.Identifiers {
		// each class has a header of endID (4 bytes) and length (1 byte)
		n += 5 + len(id.Data)
	}
	return n
}