diff options
author | insomniac <insomniacslk@users.noreply.github.com> | 2018-04-20 00:01:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-20 00:01:31 +0200 |
commit | 640d78837b3c44975dc2ae2abdc3aabc4003c87f (patch) | |
tree | 0206078500f839608d8ae570da7a9f96b249ad43 /dhcpv6/client.go | |
parent | ae181b6e52105a03adeda151ee944a14622e0729 (diff) |
dhcpv6: added modifiers (#41)
dhcpv6: added modifiers
Added support for packet modifiers, i.e. functions that can arbitrarily
manipulate a DHCPv6 packet. These modifiers are used by NewMessage,
NewSolicitForInterface, NewRequestForAdvertise, and can be used by other
packet creation functions.
A bunch of sample modifiers have been added under modifiers.go , too.
With the introduction of modifiers I also removed some options that
should not necessarily be in a standard DHCPv6 message.
Diffstat (limited to 'dhcpv6/client.go')
-rw-r--r-- | dhcpv6/client.go | 17 |
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 |