summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPablo Mazzini <pmazzini@gmail.com>2022-10-01 13:31:59 +0100
committerGitHub <noreply@github.com>2022-10-01 13:31:59 +0100
commitea8b2cd4dcf75da1cc1e9bebc85afe0761d858fd (patch)
treebf8f054c33dbfb5c7ef27a285dc2b410e90875b2
parent4b60c8bb3e481810b4a52e883a3a70b776c6165d (diff)
parent17a7e45e47326931084145a08aaeb1470f6504dd (diff)
Merge branch 'master' into newinform-formatting-consistency
-rw-r--r--dhcpv4/dhcpv4_test.go10
-rw-r--r--dhcpv4/nclient4/lease.go20
-rw-r--r--dhcpv4/nclient4/lease_test.go2
-rw-r--r--dhcpv4/server4/server_test.go8
4 files changed, 27 insertions, 13 deletions
diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go
index eff7a71..d2d6edd 100644
--- a/dhcpv4/dhcpv4_test.go
+++ b/dhcpv4/dhcpv4_test.go
@@ -355,6 +355,16 @@ func TestNewInform(t *testing.T) {
require.True(t, m.ClientIPAddr.Equal(localIP))
}
+func TestDHCPv4NewInformWithModifier(t *testing.T) {
+ hwAddr := net.HardwareAddr{1, 2, 3, 4, 5, 6}
+ localIP := net.IPv4(10, 10, 11, 11)
+ serverID := net.IPv4(192, 168, 0, 1)
+ m, err := NewInform(hwAddr, localIP, WithOption(OptServerIdentifier(serverID)))
+
+ require.NoError(t, err)
+ require.True(t, serverID.Equal(m.ServerIdentifier()))
+}
+
func TestIsOptionRequested(t *testing.T) {
pkt, err := New()
require.NoError(t, err)
diff --git a/dhcpv4/nclient4/lease.go b/dhcpv4/nclient4/lease.go
index 1895dd0..9ee3109 100644
--- a/dhcpv4/nclient4/lease.go
+++ b/dhcpv4/nclient4/lease.go
@@ -42,15 +42,15 @@ func (c *Client) Release(lease *Lease, modifiers ...dhcpv4.Modifier) error {
// 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 {
+func (c *Client) Renew(ctx context.Context, lease *Lease, modifiers ...dhcpv4.Modifier) (*Lease, error) {
if lease == nil {
- return fmt.Errorf("lease is nil")
+ return nil, 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)
+ return nil, fmt.Errorf("unable to create a request: %w", err)
}
// Servers are supposed to only respond to Requests containing their server identifier,
@@ -61,17 +61,19 @@ func (c *Client) Renew(ctx context.Context, lease *Lease, modifiers ...dhcpv4.Mo
IsCorrectServer(lease.Offer.ServerIdentifier()),
IsMessageType(dhcpv4.MessageTypeAck, dhcpv4.MessageTypeNak)))
if err != nil {
- return fmt.Errorf("got an error while processing the request: %w", err)
+ return nil, fmt.Errorf("got an error while processing the request: %w", err)
}
if response.MessageType() == dhcpv4.MessageTypeNak {
- return &ErrNak{
+ return nil, &ErrNak{
Offer: lease.Offer,
Nak: response,
}
}
- // Update the ACK of the lease with the ACK of the latest renewal
- lease.ACK = response
-
- return nil
+ // Return a new lease with the latest ACK and updated creation time
+ return &Lease{
+ Offer: lease.Offer,
+ ACK: response,
+ CreationTime: time.Now(),
+ }, nil
}
diff --git a/dhcpv4/nclient4/lease_test.go b/dhcpv4/nclient4/lease_test.go
index d27eeca..d9377e7 100644
--- a/dhcpv4/nclient4/lease_test.go
+++ b/dhcpv4/nclient4/lease_test.go
@@ -238,7 +238,7 @@ func (sll *testServerLeaseList) runTest(t *testing.T) {
sll.lastTestSvrErrLock.RUnlock()
if keepgoing {
- err = clnt.Renew(context.Background(), lease)
+ lease, err = clnt.Renew(context.Background(), lease)
sll.lastTestSvrErrLock.RLock()
keepgoing = chkerr(err, sll.lastTestSvrErr, l.ShouldFail, t)
sll.lastTestSvrErrLock.RUnlock()
diff --git a/dhcpv4/server4/server_test.go b/dhcpv4/server4/server_test.go
index 7be868c..9fc44a3 100644
--- a/dhcpv4/server4/server_test.go
+++ b/dhcpv4/server4/server_test.go
@@ -110,15 +110,17 @@ func TestServer(t *testing.T) {
lease, err := c.Request(context.Background(), modifiers...)
require.NoError(t, err)
- require.NotNil(t, lease.Offer, lease.ACK)
+ require.NotNil(t, lease.Offer)
+ require.NotNil(t, 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)
}
- err = c.Renew(context.Background(), lease, modifiers...)
+ lease, err = c.Renew(context.Background(), lease, modifiers...)
require.NoError(t, err)
- require.NotNil(t, lease.Offer, lease.ACK)
+ require.NotNil(t, lease.Offer)
+ require.NotNil(t, 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)