summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv6/client.go')
-rw-r--r--dhcpv6/client.go17
1 files changed, 12 insertions, 5 deletions
diff --git a/dhcpv6/client.go b/dhcpv6/client.go
index 3e79a0a..6edf798 100644
--- a/dhcpv6/client.go
+++ b/dhcpv6/client.go
@@ -37,8 +37,10 @@ func NewClient() *Client {
}
// Exchange executes a 4-way DHCPv6 request (SOLICIT, ADVERTISE, REQUEST,
-// REPLY). If the SOLICIT packet is nil, defaults are used.
-func (c *Client) Exchange(ifname string, solicit DHCPv6) ([]DHCPv6, error) {
+// REPLY). If the SOLICIT packet is nil, defaults are used. The modifiers will
+// be applied to the Request packet. A common use is to make sure that the
+// Request packet has the right options, see modifiers.go
+func (c *Client) Exchange(ifname string, solicit DHCPv6, modifiers ...Modifier) ([]DHCPv6, error) {
conversation := make([]DHCPv6, 0)
var err error
@@ -56,6 +58,9 @@ func (c *Client) Exchange(ifname string, solicit DHCPv6) ([]DHCPv6, error) {
if request != nil {
conversation = append(conversation, request)
}
+ for _, mod := range modifiers {
+ request = mod(request)
+ }
if err != nil {
return conversation, err
}
@@ -121,26 +126,28 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT
return nil, err
}
- // wait for an ADVERTISE response
- buf := make([]byte, maxUDPReceivedPacketSize)
+ // wait for a reply
oobdata := []byte{} // ignoring oob data
conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
var (
adv DHCPv6
isMessage bool
)
+ defer conn.Close()
msg, ok := packet.(*DHCPv6Message)
if ok {
isMessage = true
}
for {
+ buf := make([]byte, maxUDPReceivedPacketSize)
n, _, _, _, err := conn.ReadMsgUDP(buf, oobdata)
if err != nil {
return nil, err
}
adv, err = FromBytes(buf[:n])
if err != nil {
- return nil, err
+ // skip non-DHCP packets
+ continue
}
if recvMsg, ok := adv.(*DHCPv6Message); ok && isMessage {
// if a regular message, check the transaction ID first