diff options
author | Sean Karlage <skarlage@get9.io> | 2018-10-03 10:17:23 -0700 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-10-03 19:17:23 +0200 |
commit | 896739d57dea7a47d0bc00831e02d715c5f1a5ba (patch) | |
tree | 5d7a4461e92e63118805f78e4d85e9b3e9ea09e9 /dhcpv4 | |
parent | f6c77dba515d567131ead5b0f87de81ef2230f31 (diff) |
dhcpv4: Add OptRootPath (#163)
* dhcpv4: Add OptRootPath
Adds a root path option that supports DHCPv4 option 17
* Add root path parsing to giant option parsing switch
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/option_root_path.go | 52 | ||||
-rw-r--r-- | dhcpv4/option_root_path_test.go | 46 | ||||
-rw-r--r-- | dhcpv4/options.go | 2 | ||||
-rw-r--r-- | dhcpv4/options_test.go | 7 |
4 files changed, 107 insertions, 0 deletions
diff --git a/dhcpv4/option_root_path.go b/dhcpv4/option_root_path.go new file mode 100644 index 0000000..504ed17 --- /dev/null +++ b/dhcpv4/option_root_path.go @@ -0,0 +1,52 @@ +package dhcpv4 + +import ( + "fmt" +) + +// This option implements the root path option +// https://tools.ietf.org/html/rfc2132 + +// OptRootPath represents the path to the client's root disk. +type OptRootPath struct { + Path string +} + +// ParseOptRootPath constructs an OptRootPath struct from a sequence of bytes +// and returns it, or an error. +func ParseOptRootPath(data []byte) (*OptRootPath, error) { + // Should at least have code and length + if len(data) < 2 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionRootPath { + return nil, fmt.Errorf("expected option %v, got %v instead", OptionRootPath, code) + } + length := int(data[1]) + if len(data) < 2+length { + return nil, ErrShortByteStream + } + return &OptRootPath{Path: string(data[2 : 2+length])}, nil +} + +// Code returns the option code. +func (o *OptRootPath) Code() OptionCode { + return OptionRootPath +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptRootPath) ToBytes() []byte { + return append([]byte{byte(o.Code()), byte(o.Length())}, []byte(o.Path)...) +} + +// String returns a human-readable string for this option. +func (o *OptRootPath) String() string { + return fmt.Sprintf("Root Path -> %v", o.Path) +} + +// Length returns the length of the data portion (excluding option code and byte +// for length, if any). +func (o *OptRootPath) Length() int { + return len(o.Path) +} diff --git a/dhcpv4/option_root_path_test.go b/dhcpv4/option_root_path_test.go new file mode 100644 index 0000000..53de45b --- /dev/null +++ b/dhcpv4/option_root_path_test.go @@ -0,0 +1,46 @@ +package dhcpv4 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptRootPathInterfaceMethods(t *testing.T) { + o := OptRootPath{Path: "/foo/bar/baz"} + require.Equal(t, OptionRootPath, o.Code(), "Code") + require.Equal(t, 12, o.Length(), "Length") + wantBytes := []byte{ + byte(OptionRootPath), + 12, + '/', 'f', 'o', 'o', '/', 'b', 'a', 'r', '/', 'b', 'a', 'z', + } + require.Equal(t, wantBytes, o.ToBytes(), "ToBytes") +} + +func TestParseOptRootPath(t *testing.T) { + data := []byte{byte(OptionRootPath), 4, '/', 'f', 'o', 'o'} + o, err := ParseOptRootPath(data) + require.NoError(t, err) + require.Equal(t, &OptRootPath{Path: "/foo"}, o) + + // Short byte stream + data = []byte{byte(OptionRootPath)} + _, err = ParseOptRootPath(data) + require.Error(t, err, "should get error from short byte stream") + + // Wrong code + data = []byte{43, 2, 1, 1} + _, err = ParseOptRootPath(data) + require.Error(t, err, "should get error from wrong code") + + // Bad length + data = []byte{byte(OptionRootPath), 6, 1, 1, 1} + _, err = ParseOptRootPath(data) + require.Error(t, err, "should get error from bad length") +} + +func TestOptRootPathString(t *testing.T) { + o := OptRootPath{Path: "/foo/bar/baz"} + require.Equal(t, "Root Path -> /foo/bar/baz", o.String()) +} diff --git a/dhcpv4/options.go b/dhcpv4/options.go index 02fa6e4..6256ef7 100644 --- a/dhcpv4/options.go +++ b/dhcpv4/options.go @@ -80,6 +80,8 @@ func ParseOption(data []byte) (Option, error) { opt, err = ParseOptVIVC(data) case OptionDNSDomainSearchList: opt, err = ParseOptDomainSearch(data) + case OptionRootPath: + opt, err = ParseOptRootPath(data) default: opt, err = ParseOptionGeneric(data) } diff --git a/dhcpv4/options_test.go b/dhcpv4/options_test.go index 899fb2c..c06f6f5 100644 --- a/dhcpv4/options_test.go +++ b/dhcpv4/options_test.go @@ -144,6 +144,13 @@ func TestParseOption(t *testing.T) { require.Equal(t, OptionClientSystemArchitectureType, opt.Code(), "Code") require.Equal(t, 4, opt.Length(), "Length") require.Equal(t, option, opt.ToBytes(), "ToBytes") + + 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") } func TestParseOptionZeroLength(t *testing.T) { |