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.go41
-rw-r--r--dhcpv4/options.go21
3 files changed, 131 insertions, 5 deletions
diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go
new file mode 100644
index 0000000..844c4bc
--- /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
+}
+
+// Parse 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..e3310e9
--- /dev/null
+++ b/dhcpv4/option_relay_agent_information_test.go
@@ -0,0 +1,41 @@
+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',
+ }
+ opt, err := ParseOptRelayAgentInformation(data)
+ require.NoError(t, err)
+
+ 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)
+}
diff --git a/dhcpv4/options.go b/dhcpv4/options.go
index 6256ef7..b117728 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