From d1b2960b7b2d4d43374def6488faf96b7d4cb5dc Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Sun, 11 Nov 2018 20:49:42 +0000 Subject: add OptRelayAgentInformation --- dhcpv4/option_relay_agent_information.go | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 dhcpv4/option_relay_agent_information.go (limited to 'dhcpv4/option_relay_agent_information.go') 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 +} -- cgit v1.2.3 From 190b8db21355bbb3d28f875ba2063143bd51f27c Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Sun, 11 Nov 2018 20:55:54 +0000 Subject: OptRelayAgentInformation: fix comment --- dhcpv4/option_relay_agent_information.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'dhcpv4/option_relay_agent_information.go') diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go index 844c4bc..75bbef0 100644 --- a/dhcpv4/option_relay_agent_information.go +++ b/dhcpv4/option_relay_agent_information.go @@ -11,8 +11,8 @@ type OptRelayAgentInformation struct { Options []Option } -// Parse returns a new OptRelayAgentInformation from a byte stream, or error if -// any. +// 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 -- cgit v1.2.3 From 472ea5db936189a5cfb3d92847714f914a16ea1d Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Sun, 11 Nov 2018 22:28:05 +0000 Subject: OptRelayAgentInformation: increase coverage --- dhcpv4/option_relay_agent_information.go | 2 +- dhcpv4/option_relay_agent_information_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'dhcpv4/option_relay_agent_information.go') diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go index 75bbef0..447783e 100644 --- a/dhcpv4/option_relay_agent_information.go +++ b/dhcpv4/option_relay_agent_information.go @@ -60,7 +60,7 @@ func (o *OptRelayAgentInformation) ToBytes() []byte { // String returns a human-readable string for this option. func (o *OptRelayAgentInformation) String() string { - return fmt.Sprintf("Relay Agent Information -> [%v]", o.Options) + return fmt.Sprintf("Relay Agent Information -> %v", o.Options) } // Length returns the length of the data portion (excluding option code and byte diff --git a/dhcpv4/option_relay_agent_information_test.go b/dhcpv4/option_relay_agent_information_test.go index 68b5c73..2b7b7a3 100644 --- a/dhcpv4/option_relay_agent_information_test.go +++ b/dhcpv4/option_relay_agent_information_test.go @@ -39,3 +39,9 @@ func TestParseOptRelayAgentInformationToBytes(t *testing.T) { } require.Equal(t, expected, data) } + +func TestOptRelayAgentInformationToBytesString(t *testing.T) { + o := OptRelayAgentInformation{} + require.Equal(t, "Relay Agent Information -> []", o.String()) +} + -- cgit v1.2.3