diff options
author | Sean Karlage <skarlage@fb.com> | 2018-03-04 10:59:15 -0800 |
---|---|---|
committer | Sean Karlage <skarlage@fb.com> | 2018-03-05 15:08:56 -0800 |
commit | 6491fc7ec777dbdfdfe05365c85878bb6e4d691d (patch) | |
tree | 0de605d9df238a604272afa31e1831fc1cc7eee2 /dhcpv4/options.go | |
parent | 6cfb183f6e3c8496a80aad03b8f47423eeac1123 (diff) |
Move bsdp functionality to its own subpackage. Bring in stretchr/testify/assert for nicer asserts
Diffstat (limited to 'dhcpv4/options.go')
-rw-r--r-- | dhcpv4/options.go | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/dhcpv4/options.go b/dhcpv4/options.go index 989a93b..6576c05 100644 --- a/dhcpv4/options.go +++ b/dhcpv4/options.go @@ -10,6 +10,7 @@ type OptionCode byte var MagicCookie = []byte{99, 130, 83, 99} +// TODO: implement Option as an interface similar to dhcpv6. type Option struct { Code OptionCode Data []byte @@ -38,27 +39,27 @@ func ParseOption(dataStart []byte) (*Option, error) { } } -// OptionsFromBytesWithMagicCookie parses a sequence of bytes until the end and -// builds a list of options from it. The sequence must contain the Magic Cookie. -// Returns an error if any invalid option or length is found. -func OptionsFromBytesWithMagicCookie(data []byte) ([]Option, error) { +// OptionsFromBytes parses a sequence of bytes until the end and builds a list +// of options from it. The sequence must contain the Magic Cookie. Returns an +// error if any invalid option or length is found. +func OptionsFromBytes(data []byte) ([]Option, error) { if len(data) < len(MagicCookie) { return nil, errors.New("Invalid options: shorter than 4 bytes") } if !bytes.Equal(data[:len(MagicCookie)], MagicCookie) { return nil, fmt.Errorf("Invalid Magic Cookie: %v", data[:len(MagicCookie)]) } - opts, err := OptionsFromBytes(data[len(MagicCookie):]) + opts, err := OptionsFromBytesWithoutMagicCookie(data[len(MagicCookie):]) if err != nil { return nil, err } return opts, nil } -// OptionsFromBytes parses a sequence of bytes until the end 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 OptionsFromBytes(data []byte) ([]Option, error) { +// OptionsFromBytesWithoutMagicCookie parses a sequence of bytes until the end +// 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) { options := make([]Option, 0, 10) idx := 0 for { @@ -86,15 +87,15 @@ func OptionsFromBytes(data []byte) ([]Option, error) { return options, nil } -// OptionsToBytesWithMagicCookie converts a list of options to a wire-format -// representation with the DHCP magic cookie prepended. -func OptionsToBytesWithMagicCookie(options []Option) []byte { - ret := MagicCookie - return append(ret, OptionsToBytes(options)...) +// OptionsToBytes converts a list of options to a wire-format representation +// with the DHCP magic cookie prepended. +func OptionsToBytes(options []Option) []byte { + return append(MagicCookie, OptionsToBytesWithoutMagicCookie(options)...) } -// OptionsToBytes converts a list of options to a wire-format representation. -func OptionsToBytes(options []Option) []byte { +// OptionsToBytesWithoutMagicCookie converts a list of options to a wire-format +// representation. +func OptionsToBytesWithoutMagicCookie(options []Option) []byte { ret := []byte{} for _, opt := range options { ret = append(ret, opt.ToBytes()...) @@ -110,16 +111,6 @@ func (o *Option) String() string { return fmt.Sprintf("%v -> %v", code, o.Data) } -// BSDPString converts a BSDP-specific option embedded in -// vendor-specific information to a human-readable string. -func (o *Option) BSDPString() string { - code, ok := BSDPOptionCodeToString[o.Code] - if !ok { - code = "Unknown" - } - return fmt.Sprintf("%v -> %v", code, o.Data) -} - func (o *Option) ToBytes() []byte { // Convert a single option to its wire-format representation ret := []byte{byte(o.Code)} |