diff options
author | Mikma <mikma@users.noreply.github.com> | 2021-10-26 00:09:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-25 23:09:14 +0100 |
commit | 63954f993bd3cc56f2999e34c67e75fb5be7ed0f (patch) | |
tree | f953c43587d8bb7deb4117a931659077ca60faf6 | |
parent | b28ed3e9daf60f867fe8475745add4461356eaad (diff) |
dhcpv6: fix and improve IA_TA (#449)
Fix typo in MessageOptions.IATA(). Add iaid argument to WithIATA() to
allow it to be set easily in line with WithIAPD(). Add a test for
WithIATA(),
Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
Co-authored-by: Mikael Magnusson <mikma@users.sourceforge.net>
Co-authored-by: Pablo Mazzini <pmazzini@gmail.com>
-rw-r--r-- | dhcpv6/dhcpv6message.go | 2 | ||||
-rw-r--r-- | dhcpv6/modifiers.go | 8 | ||||
-rw-r--r-- | dhcpv6/modifiers_test.go | 22 |
3 files changed, 28 insertions, 4 deletions
diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index a120898..e2210fd 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -69,7 +69,7 @@ func (mo MessageOptions) OneIANA() *OptIANA { // IATA returns all Identity Association for Temporary Address options. func (mo MessageOptions) IATA() []*OptIATA { - opts := mo.Get(OptionIANA) + opts := mo.Get(OptionIATA) var iatas []*OptIATA for _, o := range opts { iatas = append(iatas, o.(*OptIATA)) diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go index 8c708d9..1d8c49f 100644 --- a/dhcpv6/modifiers.go +++ b/dhcpv6/modifiers.go @@ -91,15 +91,17 @@ func WithIAID(iaid [4]byte) Modifier { } } -// WithIATA adds or updates an OptIANA option with the provided IAAddress -// options -func WithIATA(addrs ...OptIAAddress) Modifier { +// WithIATA adds or updates an OptIATA option with the provided IAID, +// and IAAddress options +func WithIATA(iaid [4]byte, addrs ...OptIAAddress) Modifier { return func(d DHCPv6) { if msg, ok := d.(*Message); ok { iata := msg.Options.OneIATA() if iata == nil { iata = &OptIATA{} } + copy(iata.IaId[:], iaid[:]) + for _, addr := range addrs { iata.Options.Add(&addr) } diff --git a/dhcpv6/modifiers_test.go b/dhcpv6/modifiers_test.go index 3c3204d..1d48086 100644 --- a/dhcpv6/modifiers_test.go +++ b/dhcpv6/modifiers_test.go @@ -4,6 +4,7 @@ import ( "log" "net" "testing" + "time" "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" @@ -134,3 +135,24 @@ func TestWithClientLinkLayerAddress(t *testing.T) { require.Equal(t, iana.HWTypeEthernet, llt) require.Equal(t, mac, lla) } + +func TestWithIATA(t *testing.T) { + var d Message + WithIATA([4]byte{1, 2, 3, 4}, OptIAAddress{ + IPv6Addr: net.ParseIP("2001:DB8:7689::1234"), + PreferredLifetime: 3600, + ValidLifetime: 5200, + })(&d) + require.Equal(t, 1, len(d.Options.Options)) + + iata := d.Options.OneIATA() + iataOpts := iata.Options.Get(OptionIAAddr) + iaAddr := iataOpts[0].(*OptIAAddress) + + require.Equal(t, OptionIATA, iata.Code()) + require.Equal(t, [4]byte{1, 2, 3, 4}, iata.IaId) + require.Equal(t, net.ParseIP("2001:DB8:7689::1234"), + iaAddr.IPv6Addr) + require.Equal(t, time.Duration(3600), iaAddr.PreferredLifetime) + require.Equal(t, time.Duration(5200), iaAddr.ValidLifetime) +} |