diff options
Diffstat (limited to 'dhcpv4/option_relay_agent_information.go')
-rw-r--r-- | dhcpv4/option_relay_agent_information.go | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go index 42625ca..fb86c70 100644 --- a/dhcpv4/option_relay_agent_information.go +++ b/dhcpv4/option_relay_agent_information.go @@ -2,37 +2,52 @@ package dhcpv4 import ( "fmt" - - "github.com/u-root/u-root/pkg/uio" ) -// OptRelayAgentInformation implements the relay agent info option described by -// RFC 3046. -type OptRelayAgentInformation struct { - Options Options +// RelayOptions is like Options, but stringifies using the Relay Agent Specific +// option space. +type RelayOptions struct { + Options } -// ParseOptRelayAgentInformation returns a new OptRelayAgentInformation from a -// byte stream, or error if any. -func ParseOptRelayAgentInformation(data []byte) (*OptRelayAgentInformation, error) { - options, err := OptionsFromBytesWithParser(data, codeGetter, ParseOptionGeneric, false /* don't check for OptionEnd tag */) - if err != nil { - return nil, err - } - return &OptRelayAgentInformation{Options: options}, nil +var relayHumanizer = OptionHumanizer{ + ValueHumanizer: func(code OptionCode, data []byte) fmt.Stringer { + return OptionGeneric{data} + }, + CodeHumanizer: func(c uint8) OptionCode { + return GenericOptionCode(c) + }, +} + +// String prints the contained options using Relay Agent-specific option code parsing. +func (r RelayOptions) String() string { + return r.Options.ToString(relayHumanizer) } -// Code returns the option code. -func (o *OptRelayAgentInformation) Code() OptionCode { - return OptionRelayAgentInformation +// FromBytes parses relay agent options from data. +func (r *RelayOptions) FromBytes(data []byte) error { + r.Options = make(Options) + return r.Options.FromBytes(data) } -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptRelayAgentInformation) ToBytes() []byte { - return uio.ToBigEndian(o.Options) +// OptRelayAgentInfo returns a new DHCP Relay Agent Info option. +// +// The relay agent info option is described by RFC 3046. +func OptRelayAgentInfo(o ...Option) Option { + return Option{Code: OptionRelayAgentInformation, Value: RelayOptions{OptionsFromList(o...)}} } -// String returns a human-readable string for this option. -func (o *OptRelayAgentInformation) String() string { - return fmt.Sprintf("Relay Agent Information -> %v", o.Options) +// GetRelayAgentInfo returns options embedded by the relay agent. +// +// The relay agent info option is described by RFC 3046. +func GetRelayAgentInfo(o Options) *RelayOptions { + v := o.Get(OptionRelayAgentInformation) + if v == nil { + return nil + } + var relayOptions RelayOptions + if err := relayOptions.FromBytes(v); err != nil { + return nil + } + return &relayOptions } |