diff options
author | Hu Jun <hujun.work@gmail.com> | 2020-06-18 10:53:18 -0700 |
---|---|---|
committer | Hu Jun <hujun.work@gmail.com> | 2020-06-18 10:53:18 -0700 |
commit | eb6bb0498f94a5d396a03d9ec5b028703071e2d7 (patch) | |
tree | 18d63709a486df4f96c16d41aef6a8f2e1d49e6a /dhcpv4/nclient4 | |
parent | 42e8d6f8badf5831edf0b8a11f03273031e4949e (diff) |
- move the example to a separate file example_lease_test.go
- fix a few more lint complains
Signed-off-by: Hu Jun <hujun.work@gmail.com>
Diffstat (limited to 'dhcpv4/nclient4')
-rw-r--r-- | dhcpv4/nclient4/client.go | 6 | ||||
-rw-r--r-- | dhcpv4/nclient4/example_lease_test.go | 40 | ||||
-rw-r--r-- | dhcpv4/nclient4/lease.go | 81 |
3 files changed, 62 insertions, 65 deletions
diff --git a/dhcpv4/nclient4/client.go b/dhcpv4/nclient4/client.go index d326ddb..414a48e 100644 --- a/dhcpv4/nclient4/client.go +++ b/dhcpv4/nclient4/client.go @@ -166,7 +166,7 @@ type Client struct { //clientIdOptions is a list of DHCPv4 option code that DHCP server used to //identify client other than the HWAddress, //like client-id, option82/remote-id..etc - clientIdOptions dhcpv4.OptionCodeList + clientIDOptions dhcpv4.OptionCodeList //lease info after DORA, nil before DORA lease *DHCPv4ClientLease @@ -201,7 +201,7 @@ func new(iface string, conn net.PacketConn, ifaceHWAddr net.HardwareAddr, opts . pending: make(map[dhcpv4.TransactionID]*pendingCh), lease: nil, leaseApplyHandler: defaultLeaseApplyHandler, - clientIdOptions: dhcpv4.OptionCodeList{}, + clientIDOptions: dhcpv4.OptionCodeList{}, ifName: iface, } @@ -538,7 +538,7 @@ var errDeadlineExceeded = errors.New("INTERNAL ERROR: deadline exceeded") func (c *Client) SendAndRead(ctx context.Context, dest *net.UDPAddr, p *dhcpv4.DHCPv4, match Matcher) (*dhcpv4.DHCPv4, error) { var response *dhcpv4.DHCPv4 //check if the request packet has all options required by c.clientIdOptions - for _, optioncode := range c.clientIdOptions { + for _, optioncode := range c.clientIDOptions { if len(p.Options.Get(optioncode)) == 0 { err := fmt.Errorf("Option %v required for client identification is missing in request", optioncode) return nil, err diff --git a/dhcpv4/nclient4/example_lease_test.go b/dhcpv4/nclient4/example_lease_test.go new file mode 100644 index 0000000..1d7d7a4 --- /dev/null +++ b/dhcpv4/nclient4/example_lease_test.go @@ -0,0 +1,40 @@ +//this is an example for nclient4 with lease + +package nclient4_test + +import ( + "context" + "log" + + "github.com/insomniacslk/dhcp/dhcpv4" + "github.com/insomniacslk/dhcp/dhcpv4/nclient4" +) + +func Example_dHCPv4ClientLease() { + ifname := "eth0" + remote_id := "client-1" + var idoptlist dhcpv4.OptionCodeList + //specify option82 is part of client identification used by DHCPv4 server + idoptlist.Add(dhcpv4.OptionRelayAgentInformation) + clnt_options := []nclient4.ClientOpt{nclient4.WithClientIDOptions(idoptlist), nclient4.WithDebugLogger()} + clnt, err := nclient4.New(ifname, clnt_options...) + if err != nil { + log.Fatalf("failed to create dhcpv4 client,%v", err) + } + //adding option82/remote-id option to discovery and request + remote_id_sub_opt := dhcpv4.OptGeneric(dhcpv4.AgentRemoteIDSubOption, []byte(remote_id)) + option82 := dhcpv4.OptRelayAgentInfo(remote_id_sub_opt) + _, _, err = clnt.RequestSavingLease(context.Background(), dhcpv4.WithOption(option82)) + if err != nil { + log.Fatal(err) + } + //print the lease + log.Printf("Got lease:\n%v", clnt.GetLease()) + //release the lease + log.Print("Releasing lease...") + err = clnt.Release() + if err != nil { + log.Fatal(err) + } + log.Print("done") +} diff --git a/dhcpv4/nclient4/lease.go b/dhcpv4/nclient4/lease.go index 2a8ba18..b4f317e 100644 --- a/dhcpv4/nclient4/lease.go +++ b/dhcpv4/nclient4/lease.go @@ -1,48 +1,5 @@ -/* -An example, bind to eth0, using option82/remote-id for client identification: +//This is lease managment for nclient4 - - package main - - import ( - "context" - "log" - - "github.com/insomniacslk/dhcp/dhcpv4" - "github.com/insomniacslk/dhcp/dhcpv4/nclient4" - ) - - func main() { - ifname := "eth0" - remote_id := "client-1" - var idoptlist dhcpv4.OptionCodeList - //specify option82 is part of client identification used by DHCPv4 server - idoptlist.Add(dhcpv4.OptionRelayAgentInformation) - clnt_options := []nclient4.ClientOpt{nclient4.WithClientIdOptions(idoptlist), nclient4.WithDebugLogger()} - clnt, err := nclient4.New(ifname, clnt_options...) - if err != nil { - log.Fatalf("failed to create dhcpv4 client,%v", err) - } - //adding option82/remote-id option to discovery and request - remote_id_sub_opt := dhcpv4.OptGeneric(dhcpv4.AgentRemoteIDSubOption, []byte(remote_id)) - option82 := dhcpv4.OptRelayAgentInfo(remote_id_sub_opt) - _, _, err = clnt.RequestSavingLease(context.Background(), dhcpv4.WithOption(option82)) - if err != nil { - log.Fatal(err) - } - //print the lease - log.Printf("Got lease:\n%v", clnt.GetLease()) - //release the lease - log.Print("Releasing lease...") - err = clnt.Release() - if err != nil { - log.Fatal(err) - } - log.Print("done") - } - - -*/ package nclient4 import ( @@ -72,30 +29,30 @@ type DHCPv4ClientLease struct { LeaseDuration time.Duration RenewInterval time.Duration RebindInterval time.Duration - IdOptions dhcpv4.Options //DHCPv4 options to identify the client like client-id, option82/remote-id + IDOptions dhcpv4.Options //DHCPv4 options to identify the client like client-id, option82/remote-id AckOptions dhcpv4.Options //DHCPv4 options in ACK, could be used for applying lease } //return a string representation func (lease DHCPv4ClientLease) String() string { - const FMTSTR = "%-35s\t%-35s\n" - const TIME_FMT = "01/02/2006 15:04:05.000" - rstr := fmt.Sprintf(FMTSTR, fmt.Sprintf("Interface:%v", lease.IfName), fmt.Sprintf("MAC:%v", lease.MACAddr)) - rstr += fmt.Sprintf(FMTSTR, fmt.Sprintf("Svr:%v", lease.ServerAddr.IP), fmt.Sprintf("Created:%v", lease.CreationTime.Format(TIME_FMT))) + const fmtstr = "%-35s\t%-35s\n" + const timefmtstr = "01/02/2006 15:04:05.000" + rstr := fmt.Sprintf(fmtstr, fmt.Sprintf("Interface:%v", lease.IfName), fmt.Sprintf("MAC:%v", lease.MACAddr)) + rstr += fmt.Sprintf(fmtstr, fmt.Sprintf("Svr:%v", lease.ServerAddr.IP), fmt.Sprintf("Created:%v", lease.CreationTime.Format(timefmtstr))) prefixlen, _ := lease.AssignedIPMask.Size() - rstr += fmt.Sprintf(FMTSTR, fmt.Sprintf("IP:%v/%v", lease.AssignedIP, prefixlen), fmt.Sprintf("Lease time:%v", lease.LeaseDuration)) - rstr += fmt.Sprintf(FMTSTR, fmt.Sprintf("Renew interval:%v", lease.RenewInterval), fmt.Sprintf("Rebind interval:%v", lease.RebindInterval)) - rstr += fmt.Sprintf("Id options:\n%v", lease.IdOptions) + rstr += fmt.Sprintf(fmtstr, fmt.Sprintf("IP:%v/%v", lease.AssignedIP, prefixlen), fmt.Sprintf("Lease time:%v", lease.LeaseDuration)) + rstr += fmt.Sprintf(fmtstr, fmt.Sprintf("Renew interval:%v", lease.RenewInterval), fmt.Sprintf("Rebind interval:%v", lease.RebindInterval)) + rstr += fmt.Sprintf("Id options:\n%v", lease.IDOptions) rstr += fmt.Sprintf("ACK options:\n%v", lease.AckOptions) return rstr } -// WithClientIdOptions configures a list of DHCPv4 option code that DHCP server +// 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 { +func WithClientIDOptions(cidl dhcpv4.OptionCodeList) ClientOpt { return func(c *Client) (err error) { - c.clientIdOptions = cidl + c.clientIDOptions = cidl return } } @@ -151,7 +108,7 @@ func (c *Client) GetLease() (clease DHCPv4ClientLease) { return } -// RequestRequestSavingLease completes DORA handshake and store&apply the lease +// RequestSavingLease completes DORA handshake and store&apply the lease // // Note that modifiers will be applied *both* to Discover and Request packets. func (c *Client) RequestSavingLease(ctx context.Context, modifiers ...dhcpv4.Modifier) (offer, ack *dhcpv4.DHCPv4, err error) { @@ -199,10 +156,10 @@ func (c *Client) RequestSavingLease(ctx context.Context, modifiers ...dhcpv4.Mod c.logger.Printf("warning: server doesn't include Renew Time option or it is zero seconds, setting lease time to default %v", c.lease.RebindInterval) } - c.lease.IdOptions = dhcpv4.Options{} - for _, optioncode := range c.clientIdOptions { + c.lease.IDOptions = dhcpv4.Options{} + for _, optioncode := range c.clientIDOptions { v := request.Options.Get(optioncode) - c.lease.IdOptions.Update(dhcpv4.OptGeneric(optioncode, v)) + c.lease.IDOptions.Update(dhcpv4.OptGeneric(optioncode, v)) } c.lease.AckOptions = ack.Options //update server address @@ -224,7 +181,7 @@ func (c *Client) Release() error { } //This is to make sure use same client identification options used during //DORA, so that DHCP server could identify the required lease - req.Options = c.lease.IdOptions + req.Options = c.lease.IDOptions req.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeRelease)) req.ClientHWAddr = c.ifaceHWAddr @@ -262,8 +219,8 @@ func NewWithLease(clease DHCPv4ClientLease, opts ...ClientOpt) (*Client, error) } clnt.ifName = clease.IfName clnt.lease = &clease - for optioncode := range clease.IdOptions { - clnt.clientIdOptions.Add(dhcpv4.GenericOptionCode(optioncode)) + for optioncode := range clease.IDOptions { + clnt.clientIDOptions.Add(dhcpv4.GenericOptionCode(optioncode)) } return clnt, nil |