diff options
author | insomniac <insomniacslk@users.noreply.github.com> | 2018-11-12 13:46:50 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-12 13:46:50 +0000 |
commit | 22ab333fff995af78b17bb533de93317fc7b65d5 (patch) | |
tree | ad115cae67c2d5f96b97d868727b30dc3818f681 /dhcpv4/option_relay_agent_information.go | |
parent | 08eace5b3537c9cedf8fbf2f2a61562e3ffbc302 (diff) | |
parent | 5ee47520014b0835b58e3624381e01bdf14ef9ba (diff) |
add OptRelayAgentInformation (#193)
Diffstat (limited to 'dhcpv4/option_relay_agent_information.go')
-rw-r--r-- | dhcpv4/option_relay_agent_information.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go new file mode 100644 index 0000000..447783e --- /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 +} + +// 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 + } + 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 +} |