diff options
author | Hu Jun <hujun.work@gmail.com> | 2020-06-26 20:41:33 -0700 |
---|---|---|
committer | Hu Jun <hujun.work@gmail.com> | 2020-06-26 20:41:33 -0700 |
commit | b418fa564662be29be64d464e045e353500ceb3e (patch) | |
tree | 34e9101aa69ff6a7f21c98b26ecf92743f984a32 /dhcpv4/nclient4/example_lease_test.go | |
parent | ec310975369ab43122823425959e6426e229140a (diff) |
- Client.Release() now use existing conn to send packet
- update example_lease_test.go accordingly
- add lease_test.go, which contain some test cases for lease&release, using socketpair
Signed-off-by: Hu Jun <hujun.work@gmail.com>
Diffstat (limited to 'dhcpv4/nclient4/example_lease_test.go')
-rw-r--r-- | dhcpv4/nclient4/example_lease_test.go | 44 |
1 files changed, 8 insertions, 36 deletions
diff --git a/dhcpv4/nclient4/example_lease_test.go b/dhcpv4/nclient4/example_lease_test.go index 69095a8..3639c4e 100644 --- a/dhcpv4/nclient4/example_lease_test.go +++ b/dhcpv4/nclient4/example_lease_test.go @@ -1,60 +1,32 @@ -//this is an example for nclient4 with lease/release - package nclient4_test import ( "context" - "fmt" "log" "github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv4/nclient4" - "github.com/vishvananda/netlink" ) -//applyLease adding the assigned ip to the interface specified by ifname -func applyLease(lease *nclient4.Lease, ifname string) error { - link, err := netlink.LinkByName(ifname) - if err != nil { - return err - } - prefixlen := 32 - if ipmask := lease.ACK.SubnetMask(); ipmask != nil { - prefixlen, _ = ipmask.Size() - - } - prefixstr := fmt.Sprintf("%v/%v", lease.ACK.YourIPAddr, prefixlen) - naddr, err := netlink.ParseAddr(prefixstr) - if err != nil { - return err - } - err = netlink.AddrReplace(link, naddr) - return err - -} - -func main() { - ifname := "eth1.200" - remoteid := "client-1" +func Example() { + ifname := "enp0s10" + clntid := "client-1" var idoptlist dhcpv4.OptionCodeList - //specify option82 is part of client identification used by DHCPv4 server - idoptlist.Add(dhcpv4.OptionRelayAgentInformation) + //specify option61 is part of client identification used by DHCPv4 server + idoptlist.Add(dhcpv4.OptionClientIdentifier) clntOptions := []nclient4.ClientOpt{nclient4.WithClientIDOptions(idoptlist), nclient4.WithDebugLogger()} clnt, err := nclient4.New(ifname, clntOptions...) if err != nil { log.Fatalf("failed to create dhcpv4 client,%v", err) } - //adding option82/remote-id option to discovery and request - remoteidsubopt := dhcpv4.OptGeneric(dhcpv4.AgentRemoteIDSubOption, []byte(remoteid)) - option82 := dhcpv4.OptRelayAgentInfo(remoteidsubopt) - _, lease, err := clnt.Request(context.Background(), dhcpv4.WithOption(option82)) + //add option61 to discovery and request + option61 := dhcpv4.OptClientIdentifier([]byte(clntid)) + _, lease, err := clnt.Request(context.Background(), dhcpv4.WithOption(option61)) if err != nil { log.Fatal(err) } //print the lease log.Printf("Got lease:\n%+v", lease) - //apply the lease - applyLease(lease, ifname) //release the lease log.Print("Releasing lease...") err = clnt.Release(lease) |