summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2018-08-02 11:01:48 +0200
committerPablo Mazzini <pmazzini@gmail.com>2018-08-02 11:01:48 +0200
commit5fd3f4fea8d73e29d931c0fabb4179b9e98ad275 (patch)
treedec90a8f9b93a7b221e1bf60d0e34985b1b464ab /dhcpv4
parent40474d58d9e4302b97e535e199e79ad1c63ade06 (diff)
fix OptUserClass
Diffstat (limited to 'dhcpv4')
-rw-r--r--dhcpv4/option_userclass.go9
-rw-r--r--dhcpv4/option_userclass_test.go14
2 files changed, 22 insertions, 1 deletions
diff --git a/dhcpv4/option_userclass.go b/dhcpv4/option_userclass.go
index fc088fa..f7e4d7f 100644
--- a/dhcpv4/option_userclass.go
+++ b/dhcpv4/option_userclass.go
@@ -12,6 +12,7 @@ import (
// OptUserClass represents an option encapsulating User Classes.
type OptUserClass struct {
UserClasses [][]byte
+ Rfc3004 bool
}
// Code returns the option code
@@ -22,6 +23,9 @@ func (op *OptUserClass) Code() OptionCode {
// ToBytes serializes the option and returns it as a sequence of bytes
func (op *OptUserClass) ToBytes() []byte {
buf := []byte{byte(op.Code()), byte(op.Length())}
+ if !op.Rfc3004 && len(op.UserClasses) == 1 {
+ return append(buf, op.UserClasses[0]...)
+ }
for _, uc := range op.UserClasses {
buf = append(buf, byte(len(uc)))
buf = append(buf, uc...)
@@ -32,6 +36,9 @@ func (op *OptUserClass) ToBytes() []byte {
// Length returns the option length
func (op *OptUserClass) Length() int {
ret := 0
+ if !op.Rfc3004 && len(op.UserClasses) == 1 {
+ return len(op.UserClasses[0])
+ }
for _, uc := range op.UserClasses {
ret += 1 + len(uc)
}
@@ -82,7 +89,7 @@ func ParseOptUserClass(data []byte) (*OptUserClass, error) {
opt.UserClasses = append(opt.UserClasses, data[:totalLength])
return &opt, nil
}
-
+ opt.Rfc3004 = true
for i := 0; i < totalLength; {
ucLen := int(data[i])
if ucLen == 0 {
diff --git a/dhcpv4/option_userclass_test.go b/dhcpv4/option_userclass_test.go
index 02b6f94..0e74ee1 100644
--- a/dhcpv4/option_userclass_test.go
+++ b/dhcpv4/option_userclass_test.go
@@ -9,6 +9,7 @@ import (
func TestOptUserClassToBytes(t *testing.T) {
opt := OptUserClass{
UserClasses: [][]byte{[]byte("linuxboot")},
+ Rfc3004: true,
}
data := opt.ToBytes()
expected := []byte{
@@ -19,6 +20,19 @@ func TestOptUserClassToBytes(t *testing.T) {
require.Equal(t, expected, data)
}
+func TestOptUserClassMicrosoftToBytes(t *testing.T) {
+ opt := OptUserClass{
+ UserClasses: [][]byte{[]byte("linuxboot")},
+ }
+ data := opt.ToBytes()
+ expected := []byte{
+ 77, // OptionUserClass
+ 9, // length
+ 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
+ }
+ require.Equal(t, expected, data)
+}
+
func TestParseOptUserClassMultiple(t *testing.T) {
expected := []byte{
77, 15,