summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/nclient4/lease.go
diff options
context:
space:
mode:
authorHu Jun <hujun.work@gmail.com>2020-07-16 15:42:07 -0700
committerHu Jun <hujun.work@gmail.com>2020-07-16 15:42:07 -0700
commit57847a4895b05f50095ce4412073937a3181f4fe (patch)
treee5777f9ecb1011fbb0f3d3e0cf5128dc59aa2a1d /dhcpv4/nclient4/lease.go
parentb1a957bdf2f63d8627e045c8dedbd00096d38ae9 (diff)
- remove Lease.IDOptions and update corresponding struct / function
- add NewReleaseFromLease and update Client.Release() accordingly - update lease_test.go accordingly - remove example_lease_test.go Signed-off-by: Hu Jun <hujun.work@gmail.com>
Diffstat (limited to 'dhcpv4/nclient4/lease.go')
-rw-r--r--dhcpv4/nclient4/lease.go57
1 files changed, 24 insertions, 33 deletions
diff --git a/dhcpv4/nclient4/lease.go b/dhcpv4/nclient4/lease.go
index 838e39b..bd684b0 100644
--- a/dhcpv4/nclient4/lease.go
+++ b/dhcpv4/nclient4/lease.go
@@ -1,4 +1,4 @@
-//This is lease support for nclient4
+// This is lease support for nclient4
package nclient4
@@ -10,51 +10,42 @@ import (
"github.com/insomniacslk/dhcp/dhcpv4"
)
-//Lease contains a DHCPv4 lease after DORA.
-//note: Lease doesn't include binding interface name
+// Lease contains a DHCPv4 lease after DORA.
+// note: Lease doesn't include binding interface name
type Lease struct {
Offer *dhcpv4.DHCPv4
ACK *dhcpv4.DHCPv4
CreationTime time.Time
- IDOptions dhcpv4.Options //DHCPv4 options to identify the client like client-id, option82/remote-id
}
-// WithClientIDOptions configures a list of DHCPv4 option code that DHCP server
-// uses to identify client, beside the MAC address.
-func WithClientIDOptions(cidl dhcpv4.OptionCodeList) ClientOpt {
- return func(c *Client) (err error) {
- c.clientIDOptions = cidl
- return
- }
-}
-
-//Release send DHCPv4 release messsage to server, based on specified lease.
-//release is sent as unicast per RFC2131, section 4.4.4.
-//Note: some DHCP server requries of using assigned IP address as source IP,
-//use nclient4.WithUnicast to create client for such case.
-func (c *Client) Release(lease *Lease) error {
- if lease == nil {
- return fmt.Errorf("lease is nil")
- }
- modList := []dhcpv4.Modifier{
+// NewReleaseFromLease creates a DHCPv4 Release message from the lease.
+// default Release message without any Modifer is created as following:
+// - option Message Type is Release
+// - ClientIP is set to lease.ACK.YourIPAddr
+// - ClientHWAddr is set to lease.ACK.ClientHWAddr
+// - Unicast
+// - option Server Identifier is set to ServerIdentifier of lease.ACK
+func NewReleaseFromLease(lease *Lease, modifiers ...dhcpv4.Modifier) (*dhcpv4.DHCPv4, error) {
+ return dhcpv4.New(dhcpv4.PrependModifiers(modifiers,
dhcpv4.WithMessageType(dhcpv4.MessageTypeRelease),
dhcpv4.WithClientIP(lease.ACK.YourIPAddr),
dhcpv4.WithHwAddr(lease.ACK.ClientHWAddr),
dhcpv4.WithBroadcast(false),
dhcpv4.WithOption(dhcpv4.OptServerIdentifier(lease.ACK.ServerIdentifier())),
- }
+ )...)
+}
- req, err := dhcpv4.New(modList...)
- if err != nil {
- return err
+// Release send DHCPv4 release messsage to server, based on specified lease.
+// release is sent as unicast per RFC2131, section 4.4.4.
+// Note: some DHCP server requries of using assigned IP address as source IP,
+// use nclient4.WithUnicast to create client for such case.
+func (c *Client) Release(lease *Lease, modifiers ...dhcpv4.Modifier) error {
+ if lease == nil {
+ return fmt.Errorf("lease is nil")
}
- //This is to make sure use same client identification options used during
- //DORA, so that DHCP server could identify the required lease
- for t := range lease.IDOptions {
- req.UpdateOption(
- dhcpv4.OptGeneric(dhcpv4.GenericOptionCode(t),
- lease.IDOptions.Get(dhcpv4.GenericOptionCode(t))),
- )
+ req, err := NewReleaseFromLease(lease, modifiers...)
+ if err != nil {
+ return fmt.Errorf("fail to create release request,%w", err)
}
_, err = c.conn.WriteTo(req.ToBytes(), &net.UDPAddr{IP: lease.ACK.Options.Get(dhcpv4.OptionServerIdentifier), Port: ServerPort})
if err == nil {