summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDennis Marttinen <twelho@welho.tech>2022-07-15 12:56:31 +0300
committerDennis Marttinen <twelho@welho.tech>2022-07-27 15:54:45 +0300
commitaeb78d02bd032611653400cd55e3331a1f839aa9 (patch)
treefb858be02bbd66b75273d4380bbcc4ec63e661aa
parent0405526172c87e4fc75325c9a195834a8fb4ba94 (diff)
dhcpv4: add a client method to facilitate lease renewals
Signed-off-by: Dennis Marttinen <twelho@welho.tech>
-rw-r--r--dhcpv4/dhcpv4.go16
-rw-r--r--dhcpv4/nclient4/client.go1
-rw-r--r--dhcpv4/nclient4/lease.go39
-rw-r--r--dhcpv4/nclient4/lease_test.go10
-rw-r--r--dhcpv4/server4/server_test.go8
5 files changed, 73 insertions, 1 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go
index 900bb22..e2fd190 100644
--- a/dhcpv4/dhcpv4.go
+++ b/dhcpv4/dhcpv4.go
@@ -231,6 +231,7 @@ func NewInform(hwaddr net.HardwareAddr, localIP net.IP, modifiers ...Modifier) (
}
// NewRequestFromOffer builds a DHCPv4 request from an offer.
+// It assumes the SELECTING state by default, see Section 4.3.2 in RFC 2131 for more details.
func NewRequestFromOffer(offer *DHCPv4, modifiers ...Modifier) (*DHCPv4, error) {
return New(PrependModifiers(modifiers,
WithReply(offer),
@@ -248,6 +249,21 @@ func NewRequestFromOffer(offer *DHCPv4, modifiers ...Modifier) (*DHCPv4, error)
)...)
}
+// NewRenewFromOffer builds a DHCPv4 RENEW-style request from an offer. RENEW requests have minor
+// changes to their options compared to SELECT requests as specified by RFC 2131, section 4.3.2.
+func NewRenewFromOffer(offer *DHCPv4, modifiers ...Modifier) (*DHCPv4, error) {
+ return NewRequestFromOffer(offer, PrependModifiers(modifiers,
+ // The server identifier option must not be filled in
+ WithoutOption(OptionServerIdentifier),
+ // The requested IP address must not be filled in
+ WithoutOption(OptionRequestedIPAddress),
+ // The client IP must be filled in with the IP offered to the client
+ WithClientIP(offer.YourIPAddr),
+ // The renewal request must use unicast
+ WithBroadcast(false),
+ )...)
+}
+
// NewReplyFromRequest builds a DHCPv4 reply from a request.
func NewReplyFromRequest(request *DHCPv4, modifiers ...Modifier) (*DHCPv4, error) {
return New(PrependModifiers(modifiers,
diff --git a/dhcpv4/nclient4/client.go b/dhcpv4/nclient4/client.go
index 02bd1f6..d40e1a9 100644
--- a/dhcpv4/nclient4/client.go
+++ b/dhcpv4/nclient4/client.go
@@ -493,6 +493,7 @@ func (e *ErrNak) Error() string {
}
// RequestFromOffer sends a Request message and waits for an response.
+// It assumes the SELECTING state by default, see Section 4.3.2 in RFC 2131 for more details.
func (c *Client) RequestFromOffer(ctx context.Context, offer *dhcpv4.DHCPv4, modifiers ...dhcpv4.Modifier) (*Lease, error) {
// TODO(chrisko): should this be unicast to the server?
request, err := dhcpv4.NewRequestFromOffer(offer, dhcpv4.PrependModifiers(modifiers,
diff --git a/dhcpv4/nclient4/lease.go b/dhcpv4/nclient4/lease.go
index 184fae2..1895dd0 100644
--- a/dhcpv4/nclient4/lease.go
+++ b/dhcpv4/nclient4/lease.go
@@ -3,6 +3,7 @@
package nclient4
import (
+ "context"
"fmt"
"net"
"time"
@@ -36,3 +37,41 @@ func (c *Client) Release(lease *Lease, modifiers ...dhcpv4.Modifier) error {
}
return err
}
+
+// Renew sends a DHCPv4 request to the server to renew the given lease. The renewal information is
+// sourced from the initial offer in the lease, and the ACK of the lease is updated to the ACK of
+// the latest renewal. This avoids issues with DHCP servers that omit information needed to build a
+// completely new lease from their renewal ACK (such as the Windows DHCP Server).
+func (c *Client) Renew(ctx context.Context, lease *Lease, modifiers ...dhcpv4.Modifier) error {
+ if lease == nil {
+ return fmt.Errorf("lease is nil")
+ }
+
+ request, err := dhcpv4.NewRenewFromOffer(lease.Offer, dhcpv4.PrependModifiers(modifiers,
+ dhcpv4.WithOption(dhcpv4.OptMaxMessageSize(MaxMessageSize)))...)
+ if err != nil {
+ return fmt.Errorf("unable to create a request: %w", err)
+ }
+
+ // Servers are supposed to only respond to Requests containing their server identifier,
+ // but sometimes non-compliant servers respond anyway.
+ // Clients are not required to validate this field, but servers are required to
+ // include the server identifier in their Offer per RFC 2131 Section 4.3.1 Table 3.
+ response, err := c.SendAndRead(ctx, c.serverAddr, request, IsAll(
+ IsCorrectServer(lease.Offer.ServerIdentifier()),
+ IsMessageType(dhcpv4.MessageTypeAck, dhcpv4.MessageTypeNak)))
+ if err != nil {
+ return fmt.Errorf("got an error while processing the request: %w", err)
+ }
+ if response.MessageType() == dhcpv4.MessageTypeNak {
+ return &ErrNak{
+ Offer: lease.Offer,
+ Nak: response,
+ }
+ }
+
+ // Update the ACK of the lease with the ACK of the latest renewal
+ lease.ACK = response
+
+ return nil
+}
diff --git a/dhcpv4/nclient4/lease_test.go b/dhcpv4/nclient4/lease_test.go
index 361847f..d27eeca 100644
--- a/dhcpv4/nclient4/lease_test.go
+++ b/dhcpv4/nclient4/lease_test.go
@@ -1,4 +1,4 @@
-// this tests nclient4 with lease and release
+// this tests nclient4 with lease, renew and release
package nclient4
@@ -236,6 +236,14 @@ func (sll *testServerLeaseList) runTest(t *testing.T) {
sll.lastTestSvrErrLock.RLock()
keepgoing := chkerr(err, sll.lastTestSvrErr, l.ShouldFail, t)
sll.lastTestSvrErrLock.RUnlock()
+
+ if keepgoing {
+ err = clnt.Renew(context.Background(), lease)
+ sll.lastTestSvrErrLock.RLock()
+ keepgoing = chkerr(err, sll.lastTestSvrErr, l.ShouldFail, t)
+ sll.lastTestSvrErrLock.RUnlock()
+ }
+
if keepgoing {
err = clnt.Release(lease)
//this sleep is to make sure release is handled by server
diff --git a/dhcpv4/server4/server_test.go b/dhcpv4/server4/server_test.go
index b3f6d9d..7be868c 100644
--- a/dhcpv4/server4/server_test.go
+++ b/dhcpv4/server4/server_test.go
@@ -115,6 +115,14 @@ func TestServer(t *testing.T) {
require.Equal(t, xid, p.TransactionID)
require.Equal(t, ifaces[0].HardwareAddr, p.ClientHWAddr)
}
+
+ err = c.Renew(context.Background(), lease, modifiers...)
+ require.NoError(t, err)
+ require.NotNil(t, lease.Offer, lease.ACK)
+ for _, p := range []*dhcpv4.DHCPv4{lease.Offer, lease.ACK} {
+ require.Equal(t, xid, p.TransactionID)
+ require.Equal(t, ifaces[0].HardwareAddr, p.ClientHWAddr)
+ }
}
func TestBadAddrFamily(t *testing.T) {