summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--dhcpv6/dhcpv6_test.go18
-rw-r--r--dhcpv6/dhcpv6message.go32
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) {