diff options
author | Anatole Denis <natolumin@unverle.fr> | 2021-04-24 18:11:36 +0200 |
---|---|---|
committer | Anatole Denis <Natolumin@users.noreply.github.com> | 2021-08-27 17:43:01 +0200 |
commit | 9bb7354278c1ab7d4ec1addf82ddb236a8857798 (patch) | |
tree | 5aada75c256f078b01ace57285f128d3e4652b27 /dhcpv6 | |
parent | 9b22cbe025f2ac29f10a32a748fd232238311064 (diff) |
dhcpv6: Implement RelayPort option
This is a simple 1-value option used in relay messages for RFC8357, specifying
the UDP port on which messages should be sent to the relay
Signed-off-by: Anatole Denis <natolumin@unverle.fr>
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/option_relayport.go | 42 | ||||
-rw-r--r-- | dhcpv6/option_relayport_test.go | 18 | ||||
-rw-r--r-- | dhcpv6/options.go | 2 |
3 files changed, 62 insertions, 0 deletions
diff --git a/dhcpv6/option_relayport.go b/dhcpv6/option_relayport.go new file mode 100644 index 0000000..fd51948 --- /dev/null +++ b/dhcpv6/option_relayport.go @@ -0,0 +1,42 @@ +// This module defines the optRelayPort structure. +// https://www.ietf.org/rfc/rfc8357.txt + +package dhcpv6 + +import ( + "fmt" + + "github.com/u-root/uio/uio" +) + +// OptRelayPort specifies an UDP port to use for the downstream relay +func OptRelayPort(port uint16) Option { + return &optRelayPort{DownstreamSourcePort: port} +} + +type optRelayPort struct { + DownstreamSourcePort uint16 +} + +func (op *optRelayPort) Code() OptionCode { + return OptionRelayPort +} + +func (op *optRelayPort) ToBytes() []byte { + buf := uio.NewBigEndianBuffer(nil) + buf.Write16(op.DownstreamSourcePort) + return buf.Data() +} + +func (op *optRelayPort) String() string { + return fmt.Sprintf("RelayPort: %d", op.DownstreamSourcePort) +} + +// build an optRelayPort structure from a sequence of bytes. +// The input data does not include option code and length bytes. +func parseOptRelayPort(data []byte) (*optRelayPort, error) { + var opt optRelayPort + buf := uio.NewBigEndianBuffer(data) + opt.DownstreamSourcePort = buf.Read16() + return &opt, buf.FinError() +} diff --git a/dhcpv6/option_relayport_test.go b/dhcpv6/option_relayport_test.go new file mode 100644 index 0000000..124af62 --- /dev/null +++ b/dhcpv6/option_relayport_test.go @@ -0,0 +1,18 @@ +package dhcpv6 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestParseRelayPort(t *testing.T) { + opt, err := parseOptRelayPort([]byte{0x12, 0x32}) + require.NoError(t, err) + require.Equal(t, &optRelayPort{DownstreamSourcePort: 0x1232}, opt) +} + +func TestRelayPortToBytes(t *testing.T) { + op := OptRelayPort(0x3845) + require.Equal(t, []byte{0x38, 0x45}, op.ToBytes()) +} diff --git a/dhcpv6/options.go b/dhcpv6/options.go index 06a1028..f8445bb 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -105,6 +105,8 @@ func ParseOption(code OptionCode, optData []byte) (Option, error) { opt, err = ParseOpt4RDMapRule(optData) case Option4RDNonMapRule: opt, err = ParseOpt4RDNonMapRule(optData) + case OptionRelayPort: + opt, err = parseOptRelayPort(optData) default: opt = &OptionGeneric{OptionCode: code, OptionData: optData} } |