summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_userclass.go
blob: 2e31846b3164be857d58bcb6bc664660b613a1df (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
package dhcpv4

import (
	"errors"
	"fmt"
	"strings"

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

// This option implements the User Class option
// https://tools.ietf.org/html/rfc3004

// OptUserClass represents an option encapsulating User Classes.
type OptUserClass struct {
	UserClasses [][]byte
	Rfc3004     bool
}

// Code returns the option code
func (op *OptUserClass) Code() OptionCode {
	return OptionUserClassInformation
}

// ToBytes serializes the option and returns it as a sequence of bytes
func (op *OptUserClass) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	if !op.Rfc3004 {
		buf.WriteBytes(op.UserClasses[0])
	} else {
		for _, uc := range op.UserClasses {
			buf.Write8(uint8(len(uc)))
			buf.WriteBytes(uc)
		}
	}
	return buf.Data()
}

func (op *OptUserClass) String() string {
	ucStrings := make([]string, 0, len(op.UserClasses))
	if !op.Rfc3004 {
		ucStrings = append(ucStrings, string(op.UserClasses[0]))
	} else {
		for _, uc := range op.UserClasses {
			ucStrings = append(ucStrings, string(uc))
		}
	}
	return fmt.Sprintf("User Class Information -> %v", strings.Join(ucStrings, ", "))
}

// ParseOptUserClass returns a new OptUserClass from a byte stream or
// error if any
func ParseOptUserClass(data []byte) (*OptUserClass, error) {
	opt := OptUserClass{}
	buf := uio.NewBigEndianBuffer(data)

	// Check if option is Microsoft style instead of RFC compliant, issue #113

	// User-class options are, according to RFC3004, supposed to contain a set
	// of strings each with length UC_Len_i. Here we check that this is so,
	// by seeing if all the UC_Len_i lengths are consistent with the overall
	// option length. If the lengths don't add up, we assume that the option
	// is a single string and non RFC3004 compliant
	var counting int
	for counting < buf.Len() {
		// UC_Len_i does not include itself so add 1
		counting += int(data[counting]) + 1
	}
	if counting != buf.Len() {
		opt.UserClasses = append(opt.UserClasses, data)
		return &opt, nil
	}
	opt.Rfc3004 = true
	for buf.Has(1) {
		ucLen := buf.Read8()
		if ucLen == 0 {
			return nil, fmt.Errorf("DHCP user class must have length greater than 0")
		}
		opt.UserClasses = append(opt.UserClasses, buf.CopyN(int(ucLen)))
	}
	if len(opt.UserClasses) == 0 {
		return nil, errors.New("ParseOptUserClass: at least one user class is required")
	}
	return &opt, buf.FinError()
}