gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/dhclient/dhclient.go (about) 1 // Copyright 2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !plan9 6 7 // dhclient sets up network config using DHCP. 8 // 9 // Synopsis: 10 // dhclient [OPTIONS...] 11 // 12 // Options: 13 // -timeout: lease timeout in seconds 14 // -renewals: number of DHCP renewals before exiting 15 // -verbose: verbose output 16 package main 17 18 import ( 19 "context" 20 "flag" 21 "log" 22 "net" 23 "time" 24 25 "github.com/insomniacslk/dhcp/dhcpv4" 26 "github.com/insomniacslk/dhcp/dhcpv6" 27 "github.com/u-root/u-root/pkg/dhclient" 28 "github.com/vishvananda/netlink" 29 ) 30 31 var ( 32 ifName = "^e.*" 33 timeout = flag.Int("timeout", 15, "Lease timeout in seconds") 34 retry = flag.Int("retry", 5, "Max number of attempts for DHCP clients to send requests. -1 means infinity") 35 dryRun = flag.Bool("dry-run", false, "Just make the DHCP requests, but don't configure interfaces") 36 verbose = flag.Bool("v", false, "Verbose output (print message summary for each DHCP message sent/received)") 37 vverbose = flag.Bool("vv", false, "Really verbose output (print all message options for each DHCP message sent/received)") 38 ipv4 = flag.Bool("ipv4", true, "use IPV4") 39 ipv6 = flag.Bool("ipv6", true, "use IPV6") 40 41 v6Port = flag.Int("v6-port", dhcpv6.DefaultServerPort, "DHCPv6 server port to send to") 42 v6Server = flag.String("v6-server", "ff02::1:2", "DHCPv6 server address to send to (multicast or unicast)") 43 44 v4Port = flag.Int("v4-port", dhcpv4.ServerPort, "DHCPv4 server port to send to") 45 ) 46 47 func main() { 48 flag.Parse() 49 if len(flag.Args()) > 1 { 50 log.Fatalf("only one re") 51 } 52 53 if len(flag.Args()) > 0 { 54 ifName = flag.Args()[0] 55 } 56 57 filteredIfs, err := dhclient.Interfaces(ifName) 58 if err != nil { 59 log.Fatal(err) 60 } 61 62 configureAll(filteredIfs) 63 } 64 65 func configureAll(ifs []netlink.Link) { 66 packetTimeout := time.Duration(*timeout) * time.Second 67 68 c := dhclient.Config{ 69 Timeout: packetTimeout, 70 Retries: *retry, 71 V4ServerAddr: &net.UDPAddr{ 72 IP: net.IPv4bcast, 73 Port: *v4Port, 74 }, 75 V6ServerAddr: &net.UDPAddr{ 76 IP: net.ParseIP(*v6Server), 77 Port: *v6Port, 78 }, 79 } 80 if *verbose { 81 c.LogLevel = dhclient.LogSummary 82 } 83 if *vverbose { 84 c.LogLevel = dhclient.LogDebug 85 } 86 r := dhclient.SendRequests(context.Background(), ifs, *ipv4, *ipv6, c, 30*time.Second) 87 88 for result := range r { 89 if result.Err != nil { 90 log.Printf("Could not configure %s for %s: %v", result.Interface.Attrs().Name, result.Protocol, result.Err) 91 } else if *dryRun { 92 log.Printf("Dry run: would have configured %s with %s", result.Interface.Attrs().Name, result.Lease) 93 } else if err := result.Lease.Configure(); err != nil { 94 log.Printf("Could not configure %s for %s: %v", result.Interface.Attrs().Name, result.Protocol, err) 95 } else { 96 log.Printf("Configured %s with %s", result.Interface.Attrs().Name, result.Lease) 97 } 98 } 99 log.Printf("Finished trying to configure all interfaces.") 100 }