summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6
diff options
context:
space:
mode:
authorChristopher Koch <chrisko@google.com>2019-04-06 15:50:01 -0700
committerinsomniac <insomniacslk@users.noreply.github.com>2019-04-07 23:42:56 +0100
commitb88ad4b870e4c440db756d101fe448f47a487dd8 (patch)
treea5f6ba6e1faac7ef3507a4596ac5b7b618e3e862 /dhcpv6
parent73a6b7ffe80ca04462acde3883dbd811f3ab5461 (diff)
nclient6: small fixes
- RapidCommit solicits wait for Reply messages, not Advertise. - Default recipient should be all relay agents and servers, not just servers. - Make New() and NewWithConn() interface same as in nclient4. Signed-off-by: Christopher Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6')
-rw-r--r--dhcpv6/nclient6/client.go37
-rw-r--r--dhcpv6/nclient6/client_test.go4
-rw-r--r--dhcpv6/server6/server_test.go11
3 files changed, 40 insertions, 12 deletions
diff --git a/dhcpv6/nclient6/client.go b/dhcpv6/nclient6/client.go
index dc5dd33..8b338db 100644
--- a/dhcpv6/nclient6/client.go
+++ b/dhcpv6/nclient6/client.go
@@ -90,15 +90,30 @@ func NewIPv6UDPConn(iface string, port int) (net.PacketConn, error) {
})
}
-// New creates a new DHCP client that sends and receives packets on the given
-// interface.
-func New(ifaceHWAddr net.HardwareAddr, opts ...ClientOpt) (*Client, error) {
+// New returns a new DHCPv6 client for the given network interface.
+func New(iface string, opts ...ClientOpt) (*Client, error) {
+ c, err := NewIPv6UDPConn(iface, dhcpv6.DefaultClientPort)
+ if err != nil {
+ return nil, err
+ }
+
+ i, err := net.InterfaceByName(iface)
+ if err != nil {
+ return nil, err
+ }
+ return NewWithConn(c, i.HardwareAddr, opts...)
+}
+
+// NewWithConn creates a new DHCP client that sends and receives packets on the
+// given interface.
+func NewWithConn(conn net.PacketConn, ifaceHWAddr net.HardwareAddr, opts ...ClientOpt) (*Client, error) {
c := &Client{
ifaceHWAddr: ifaceHWAddr,
timeout: 5 * time.Second,
retry: 3,
- serverAddr: AllDHCPServers,
+ serverAddr: AllDHCPRelayAgentsAndServers,
bufferCap: 5,
+ conn: conn,
done: make(chan struct{}),
pending: make(map[dhcpv6.TransactionID]*pendingCh),
@@ -239,6 +254,20 @@ func IsMessageType(t dhcpv6.MessageType) Matcher {
}
}
+// RapidSolicit sends a solicitation message with the RapidCommit option and
+// returns the first valid reply received.
+func (c *Client) RapidSolicit(ctx context.Context, modifiers ...dhcpv6.Modifier) (*dhcpv6.Message, error) {
+ solicit, err := dhcpv6.NewSolicit(c.ifaceHWAddr, append(modifiers, dhcpv6.WithRapidCommit)...)
+ if err != nil {
+ return nil, err
+ }
+ msg, err := c.SendAndRead(ctx, c.serverAddr, solicit, IsMessageType(dhcpv6.MessageTypeReply))
+ if err != nil {
+ return nil, err
+ }
+ return msg, nil
+}
+
// Solicit sends a solicitation message and returns the first valid
// advertisement received.
func (c *Client) Solicit(ctx context.Context, modifiers ...dhcpv6.Modifier) (*dhcpv6.Message, error) {
diff --git a/dhcpv6/nclient6/client_test.go b/dhcpv6/nclient6/client_test.go
index cba4ef8..49b914e 100644
--- a/dhcpv6/nclient6/client_test.go
+++ b/dhcpv6/nclient6/client_test.go
@@ -55,9 +55,9 @@ func serveAndClient(ctx context.Context, responses [][]*dhcpv6.Message, opt ...C
panic(err)
}
- o := []ClientOpt{WithConn(clientRawConn), WithRetry(1), WithTimeout(2 * time.Second)}
+ o := []ClientOpt{WithRetry(1), WithTimeout(2 * time.Second)}
o = append(o, opt...)
- mc, err := New(net.HardwareAddr{0xa, 0xb, 0xc, 0xd, 0xe, 0xf}, o...)
+ mc, err := NewWithConn(clientRawConn, net.HardwareAddr{0xa, 0xb, 0xc, 0xd, 0xe, 0xf}, o...)
if err != nil {
panic(err)
}
diff --git a/dhcpv6/server6/server_test.go b/dhcpv6/server6/server_test.go
index 05d62cb..3b204f1 100644
--- a/dhcpv6/server6/server_test.go
+++ b/dhcpv6/server6/server_test.go
@@ -12,15 +12,16 @@ import (
"github.com/stretchr/testify/require"
)
-type fakeUnconnectedConn struct {
+// Turns a connected UDP conn into an "unconnected" UDP conn.
+type unconnectedConn struct {
*net.UDPConn
}
-func (f fakeUnconnectedConn) WriteTo(b []byte, _ net.Addr) (int, error) {
+func (f unconnectedConn) WriteTo(b []byte, _ net.Addr) (int, error) {
return f.UDPConn.Write(b)
}
-func (f fakeUnconnectedConn) ReadFrom(b []byte) (int, net.Addr, error) {
+func (f unconnectedConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, err := f.Read(b)
return n, nil, err
}
@@ -43,12 +44,10 @@ func setUpClientAndServer(handler Handler) (*nclient6.Client, *Server) {
panic(err)
}
- c, err := nclient6.New(net.HardwareAddr{1, 2, 3, 4, 5, 6},
- nclient6.WithConn(fakeUnconnectedConn{clientConn}))
+ c, err := nclient6.NewWithConn(unconnectedConn{clientConn}, net.HardwareAddr{1, 2, 3, 4, 5, 6})
if err != nil {
panic(err)
}
-
return c, s
}