diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-04-30 19:08:37 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-04-30 20:08:37 +0200 |
commit | d4458d35f16d328a86eb7d0a83ae0576e9d11c77 (patch) | |
tree | 527d541130b2ff325111e5309c235541961405af /dhcpv6 | |
parent | c0a91005599b5d89db3a24758c362f8a95313a3e (diff) |
add NewAdvertiseFromSolicit (#50)
add NewAdvertiseFromSolicit
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 18 | ||||
-rw-r--r-- | dhcpv6/dhcpv6message.go | 32 |
2 files changed, 50 insertions, 0 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index 17a8896..2e79f1f 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -82,5 +82,23 @@ func TestFromAndToBytes(t *testing.T) { require.Equal(t, expected, toBytes) } +func withServerID(d DHCPv6) DHCPv6 { + sid := OptServerId{} + d.AddOption(&sid) + return d +} + +func TestNewAdvertiseFromSolicit(t *testing.T) { + s := DHCPv6Message{} + s.SetMessage(SOLICIT) + s.SetTransactionID(0xabcdef) + cid := OptClientId{} + s.AddOption(&cid) + + a, err := NewAdvertiseFromSolicit(&s, withServerID) + require.NoError(t, err) + require.Equal(t, a.(*DHCPv6Message).TransactionID(), s.TransactionID()) +} + // TODO test NewSolicit // test String and Summary diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index 0cf2360..60b7af5 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -3,6 +3,7 @@ package dhcpv6 import ( "crypto/rand" "encoding/binary" + "errors" "fmt" "log" "net" @@ -106,6 +107,37 @@ func NewSolicitForInterface(ifname string, modifiers ...Modifier) (DHCPv6, error return d, nil } +// NewAdvertiseFromSolicit creates a new ADVERTISE packet based on an SOLICIT packet. +func NewAdvertiseFromSolicit(solicit DHCPv6, modifiers ...Modifier) (DHCPv6, error) { + if solicit == nil { + return nil, errors.New("SOLICIT cannot be nil") + } + if solicit.Type() != SOLICIT { + return nil, errors.New("The passed SOLICIT must have SOLICIT type set") + } + sol, ok := solicit.(*DHCPv6Message) + if !ok { + return nil, errors.New("The passed SOLICIT must be of DHCPv6Message type") + } + // build ADVERTISE from SOLICIT + adv := DHCPv6Message{} + adv.SetMessage(ADVERTISE) + adv.SetTransactionID(sol.TransactionID()) + // add Client ID + cid := sol.GetOneOption(OPTION_CLIENTID) + if cid == nil { + return nil, errors.New("Client ID cannot be nil in SOLICIT when building ADVERTISE") + } + adv.AddOption(cid) + + // apply modifiers + d := DHCPv6(&adv) + for _, mod := range modifiers { + d = mod(d) + } + return d, nil +} + // NewRequestFromAdvertise creates a new REQUEST packet based on an ADVERTISE // packet options. func NewRequestFromAdvertise(advertise DHCPv6, modifiers ...Modifier) (DHCPv6, error) { |