summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv4')
-rw-r--r--dhcpv4/option_relay_agent_information.go74
-rw-r--r--dhcpv4/option_relay_agent_information_test.go67
-rw-r--r--dhcpv4/options.go23
-rw-r--r--dhcpv4/options_test.go37
4 files changed, 189 insertions, 12 deletions
diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go
new file mode 100644
index 0000000..447783e
--- /dev/null
+++ b/dhcpv4/option_relay_agent_information.go
@@ -0,0 +1,74 @@
+package dhcpv4
+
+import "fmt"
+
+// This option implements the relay agent information option
+// https://tools.ietf.org/html/rfc3046
+
+// OptRelayAgentInformation is a "container" option for specific agent-supplied
+// sub-options.
+type OptRelayAgentInformation struct {
+ Options []Option
+}
+
+// ParseOptRelayAgentInformation returns a new OptRelayAgentInformation from a
+// byte stream, or error if any.
+func ParseOptRelayAgentInformation(data []byte) (*OptRelayAgentInformation, error) {
+ if len(data) < 4 {
+ return nil, ErrShortByteStream
+ }
+ code := OptionCode(data[0])
+ if code != OptionRelayAgentInformation {
+ return nil, fmt.Errorf("expected code %v, got %v", OptionRelayAgentInformation, code)
+ }
+ length := int(data[1])
+ if len(data) < 2+length {
+ return nil, ErrShortByteStream
+ }
+ options, err := OptionsFromBytesWithParser(data[2:length+2], relayParseOption)
+ if err != nil {
+ return nil, err
+ }
+ return &OptRelayAgentInformation{Options: options}, nil
+}
+
+func relayParseOption(data []byte) (Option, error) {
+ if len(data) < 2 {
+ return nil, ErrShortByteStream
+ }
+ code := OptionCode(data[0])
+ length := int(data[1])
+ if len(data) < 2+length {
+ return nil, ErrShortByteStream
+ }
+ return &OptionGeneric{OptionCode: code, Data: data[2:length+2]}, nil
+}
+
+// Code returns the option code.
+func (o *OptRelayAgentInformation) Code() OptionCode {
+ return OptionRelayAgentInformation
+}
+
+// ToBytes returns a serialized stream of bytes for this option.
+func (o *OptRelayAgentInformation) ToBytes() []byte {
+ ret := []byte{byte(o.Code()), byte(o.Length())}
+ for _, opt := range o.Options {
+ ret = append(ret, opt.ToBytes()...)
+ }
+ return ret
+}
+
+// String returns a human-readable string for this option.
+func (o *OptRelayAgentInformation) String() string {
+ return fmt.Sprintf("Relay Agent Information -> %v", o.Options)
+}
+
+// Length returns the length of the data portion (excluding option code and byte
+// for length, if any).
+func (o *OptRelayAgentInformation) Length() int {
+ l := 0
+ for _, opt := range o.Options {
+ l += 2 + opt.Length()
+ }
+ return l
+}
diff --git a/dhcpv4/option_relay_agent_information_test.go b/dhcpv4/option_relay_agent_information_test.go
new file mode 100644
index 0000000..1e99206
--- /dev/null
+++ b/dhcpv4/option_relay_agent_information_test.go
@@ -0,0 +1,67 @@
+package dhcpv4
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestParseOptRelayAgentInformation(t *testing.T) {
+ data := []byte{
+ byte(OptionRelayAgentInformation),
+ 13,
+ 1, 5, 'l', 'i', 'n', 'u', 'x',
+ 2, 4, 'b', 'o', 'o', 't',
+ }
+
+ // short option bytes
+ opt, err := ParseOptRelayAgentInformation([]byte{})
+ require.Error(t, err)
+
+ // wrong code
+ opt, err = ParseOptRelayAgentInformation([]byte{1, 2, 1, 0})
+ require.Error(t, err)
+
+ // wrong option length
+ opt, err = ParseOptRelayAgentInformation([]byte{82, 3, 1, 0})
+ require.Error(t, err)
+
+ // short sub-option bytes
+ opt, err = ParseOptRelayAgentInformation([]byte{82, 3, 1, 0, 1})
+ require.Error(t, err)
+
+ // short sub-option length
+ opt, err = ParseOptRelayAgentInformation([]byte{82, 2, 1, 1})
+ require.Error(t, err)
+
+ opt, err = ParseOptRelayAgentInformation(data)
+ require.NoError(t, err)
+ require.Equal(t, len(opt.Options), 2)
+ circuit, ok := opt.Options[0].(*OptionGeneric)
+ require.True(t, ok)
+ remote, ok := opt.Options[1].(*OptionGeneric)
+ require.True(t, ok)
+ require.Equal(t, circuit.Data, []byte("linux"))
+ require.Equal(t, remote.Data, []byte("boot"))
+}
+
+func TestParseOptRelayAgentInformationToBytes(t *testing.T) {
+ opt := OptRelayAgentInformation{}
+ opt1 := &OptionGeneric{OptionCode: 1, Data: []byte("linux")}
+ opt.Options = append(opt.Options, opt1)
+ opt2 := &OptionGeneric{OptionCode: 2, Data: []byte("boot")}
+ opt.Options = append(opt.Options, opt2)
+ data := opt.ToBytes()
+ expected := []byte{
+ byte(OptionRelayAgentInformation),
+ 13,
+ 1, 5, 'l', 'i', 'n', 'u', 'x',
+ 2, 4, 'b', 'o', 'o', 't',
+ }
+ require.Equal(t, expected, data)
+}
+
+func TestOptRelayAgentInformationToBytesString(t *testing.T) {
+ o := OptRelayAgentInformation{}
+ require.Equal(t, "Relay Agent Information -> []", o.String())
+}
diff --git a/dhcpv4/options.go b/dhcpv4/options.go
index 6256ef7..dc4a724 100644
--- a/dhcpv4/options.go
+++ b/dhcpv4/options.go
@@ -50,6 +50,8 @@ func ParseOption(data []byte) (Option, error) {
opt, err = ParseOptHostName(data)
case OptionDomainName:
opt, err = ParseOptDomainName(data)
+ case OptionRootPath:
+ opt, err = ParseOptRootPath(data)
case OptionBroadcastAddress:
opt, err = ParseOptBroadcastAddress(data)
case OptionNTPServers:
@@ -74,14 +76,14 @@ func ParseOption(data []byte) (Option, error) {
opt, err = ParseOptBootfileName(data)
case OptionUserClassInformation:
opt, err = ParseOptUserClass(data)
+ case OptionRelayAgentInformation:
+ opt, err = ParseOptRelayAgentInformation(data)
case OptionClientSystemArchitectureType:
opt, err = ParseOptClientArchType(data)
- case OptionVendorIdentifyingVendorClass:
- opt, err = ParseOptVIVC(data)
case OptionDNSDomainSearchList:
opt, err = ParseOptDomainSearch(data)
- case OptionRootPath:
- opt, err = ParseOptRootPath(data)
+ case OptionVendorIdentifyingVendorClass:
+ opt, err = ParseOptVIVC(data)
default:
opt, err = ParseOptionGeneric(data)
}
@@ -112,6 +114,15 @@ func OptionsFromBytes(data []byte) ([]Option, error) {
// and builds a list of options from it. The sequence should not contain the
// DHCP magic cookie. Returns an error if any invalid option or length is found.
func OptionsFromBytesWithoutMagicCookie(data []byte) ([]Option, error) {
+ return OptionsFromBytesWithParser(data, ParseOption)
+}
+
+// OptionParser is a function signature for option parsing
+type OptionParser func(data []byte) (Option, error)
+
+// OptionsFromBytesWithParser parses Options from byte sequences using the
+// parsing function that is passed in as a paremeter
+func OptionsFromBytesWithParser(data []byte, parser OptionParser) ([]Option, error) {
options := make([]Option, 0, 10)
idx := 0
for {
@@ -122,7 +133,7 @@ func OptionsFromBytesWithoutMagicCookie(data []byte) ([]Option, error) {
if idx > len(data) {
return nil, errors.New("read past the end of options")
}
- opt, err := ParseOption(data[idx:])
+ opt, err := parser(data[idx:])
idx++
if err != nil {
return nil, err
@@ -134,7 +145,7 @@ func OptionsFromBytesWithoutMagicCookie(data []byte) ([]Option, error) {
// Options with zero length have no length byte, so here we handle the
// ones with nonzero length
- if opt.Length() > 0 {
+ if opt.Code() != OptionPad {
idx++
}
idx += opt.Length()
diff --git a/dhcpv4/options_test.go b/dhcpv4/options_test.go
index c06f6f5..bf869f2 100644
--- a/dhcpv4/options_test.go
+++ b/dhcpv4/options_test.go
@@ -57,6 +57,22 @@ func TestParseOption(t *testing.T) {
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
+ // Option root path
+ option = []byte{17, 4, '/', 'f', 'o', 'o'}
+ opt, err = ParseOption(option)
+ require.NoError(t, err)
+ require.Equal(t, OptionRootPath, opt.Code(), "Code")
+ require.Equal(t, 4, opt.Length(), "Length")
+ require.Equal(t, option, opt.ToBytes(), "ToBytes")
+
+ // Option broadcast address
+ option = []byte{28, 4, 255, 255, 255, 255}
+ opt, err = ParseOption(option)
+ require.NoError(t, err)
+ require.Equal(t, OptionBroadcastAddress, opt.Code(), "Code")
+ require.Equal(t, 4, opt.Length(), "Length")
+ require.Equal(t, option, opt.ToBytes(), "ToBytes")
+
// Option NTP servers
option = []byte{42, 4, 10, 10, 10, 10}
opt, err = ParseOption(option)
@@ -73,6 +89,14 @@ func TestParseOption(t *testing.T) {
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
+ // Requested IP address lease time
+ option = []byte{51, 4, 0, 0, 0, 0}
+ opt, err = ParseOption(option)
+ require.NoError(t, err)
+ require.Equal(t, OptionIPAddressLeaseTime, opt.Code(), "Code")
+ require.Equal(t, 4, opt.Length(), "Length")
+ require.Equal(t, option, opt.ToBytes(), "ToBytes")
+
// Message type
option = []byte{53, 1, 1}
opt, err = ParseOption(option)
@@ -137,18 +161,19 @@ func TestParseOption(t *testing.T) {
require.Equal(t, 5, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
- // Option client system architecture type option
- option = []byte{93, 4, 't', 'e', 's', 't'}
+ // Option relay agent information
+ option = []byte{82, 2, 1, 0}
opt, err = ParseOption(option)
require.NoError(t, err)
- require.Equal(t, OptionClientSystemArchitectureType, opt.Code(), "Code")
- require.Equal(t, 4, opt.Length(), "Length")
+ require.Equal(t, OptionRelayAgentInformation, opt.Code(), "Code")
+ require.Equal(t, 2, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
- option = []byte{17, 4, '/', 'f', 'o', 'o'}
+ // Option client system architecture type option
+ option = []byte{93, 4, 't', 'e', 's', 't'}
opt, err = ParseOption(option)
require.NoError(t, err)
- require.Equal(t, OptionRootPath, opt.Code(), "Code")
+ require.Equal(t, OptionClientSystemArchitectureType, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
}