From 72e14d6762cd895f00fd5c04fec8a16df521b65f Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Sat, 28 Dec 2019 09:49:06 -0800 Subject: v6: add BootFileParam getter Signed-off-by: Chris Koch --- dhcpv6/dhcpv6message.go | 12 ++++++++++++ dhcpv6/option_bootfileparam.go | 27 ++++++++++++++------------- dhcpv6/option_bootfileparam_test.go | 8 ++++---- dhcpv6/options.go | 2 +- 4 files changed, 31 insertions(+), 18 deletions(-) (limited to 'dhcpv6') diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index 4e5717f..745a837 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -116,6 +116,18 @@ func (mo MessageOptions) BootFileURL() string { return "" } +// BootFileParam returns the Boot File Param option as defined by RFC 5970. +func (mo MessageOptions) BootFileParam() []string { + opt := mo.Options.GetOne(OptionBootfileParam) + if opt == nil { + return nil + } + if u, ok := opt.(optBootFileParam); ok { + return []string(u) + } + return nil +} + // ElapsedTime returns the Elapsed Time option as defined by RFC 3315 Section 22.9. // // ElapsedTime returns a duration of 0 if the option is not present. diff --git a/dhcpv6/option_bootfileparam.go b/dhcpv6/option_bootfileparam.go index 13ec16a..4e2750b 100644 --- a/dhcpv6/option_bootfileparam.go +++ b/dhcpv6/option_bootfileparam.go @@ -6,21 +6,21 @@ import ( "github.com/u-root/u-root/pkg/uio" ) -// OptBootFileParam implements the OptionBootfileParam option -// -// This module defines the OPT_BOOTFILE_PARAM structure. -// https://www.ietf.org/rfc/rfc5970.txt (section 3.2) -type OptBootFileParam []string +// OptBootFileParam returns a BootfileParam option as defined in RFC 5970 +// Section 3.2. +func OptBootFileParam(args ...string) Option { + return optBootFileParam(args) +} -var _ Option = OptBootFileParam(nil) +type optBootFileParam []string // Code returns the option code -func (op OptBootFileParam) Code() OptionCode { +func (optBootFileParam) Code() OptionCode { return OptionBootfileParam } // ToBytes serializes the option and returns it as a sequence of bytes -func (op OptBootFileParam) ToBytes() []byte { +func (op optBootFileParam) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) for _, param := range op { if len(param) >= 1<<16 { @@ -41,14 +41,15 @@ func (op OptBootFileParam) ToBytes() []byte { return buf.Data() } -func (op OptBootFileParam) String() string { - return fmt.Sprintf("OptBootFileParam(%v)", ([]string)(op)) +func (op optBootFileParam) String() string { + return fmt.Sprintf("BootFileParam: %v", ([]string)(op)) } -// ParseOptBootFileParam builds an OptBootFileParam structure from a sequence +// parseOptBootFileParam builds an OptBootFileParam structure from a sequence // of bytes. The input data does not include option code and length bytes. -func ParseOptBootFileParam(data []byte) (result OptBootFileParam, err error) { +func parseOptBootFileParam(data []byte) (optBootFileParam, error) { buf := uio.NewBigEndianBuffer(data) + var result optBootFileParam for buf.Has(2) { length := buf.Read16() result = append(result, string(buf.CopyN(int(length)))) @@ -56,5 +57,5 @@ func ParseOptBootFileParam(data []byte) (result OptBootFileParam, err error) { if err := buf.FinError(); err != nil { return nil, err } - return + return result, nil } diff --git a/dhcpv6/option_bootfileparam_test.go b/dhcpv6/option_bootfileparam_test.go index 80a4386..467f245 100644 --- a/dhcpv6/option_bootfileparam_test.go +++ b/dhcpv6/option_bootfileparam_test.go @@ -11,13 +11,13 @@ import ( var ( testBootfileParams0Compiled = "\x00\x0eroot=/dev/sda1\x00\x00\x00\x02rw" - testBootfileParams1 = []string{ + testBootfileParams1 = []string{ "initrd=http://myserver.mycompany.local/initrd.xz", "", "root=/dev/sda1", "rw", "netconsole=..:\000:.something\000here.::..", - string(make([]byte, (1<<16) - 1)), + string(make([]byte, (1<<16)-1)), } ) @@ -41,7 +41,7 @@ func compileTestBootfileParams(t *testing.T, params []string) []byte { func TestOptBootFileParam(t *testing.T) { expected := string(compileTestBootfileParams(t, testBootfileParams1)) - opt, err := ParseOptBootFileParam([]byte(expected)) + opt, err := parseOptBootFileParam([]byte(expected)) if err != nil { t.Fatal(err) } @@ -54,7 +54,7 @@ func TestParsedTypeOptBootFileParam(t *testing.T) { tryParse := func(compiled []byte, expected []string) { opt, err := ParseOption(OptionBootfileParam, compiled) require.NoError(t, err) - bootfileParamOpt, ok := opt.(OptBootFileParam) + bootfileParamOpt, ok := opt.(optBootFileParam) require.True(t, ok, fmt.Sprintf("invalid type: %T instead of %T", opt, bootfileParamOpt)) require.Equal(t, compiled, bootfileParamOpt.ToBytes()) require.Equal(t, expected, ([]string)(bootfileParamOpt)) diff --git a/dhcpv6/options.go b/dhcpv6/options.go index 63261c1..23a0ba9 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -80,7 +80,7 @@ func ParseOption(code OptionCode, optData []byte) (Option, error) { case OptionBootfileURL: opt, err = parseOptBootFileURL(optData) case OptionBootfileParam: - opt, err = ParseOptBootFileParam(optData) + opt, err = parseOptBootFileParam(optData) case OptionClientArchType: opt, err = parseOptClientArchType(optData) case OptionNII: -- cgit v1.2.3