Skip to content

Commit

Permalink
dhclient: bring interface up
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniacslk committed Nov 13, 2018
1 parent 97eea2d commit 2d78528
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions dhclient/main.go
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"log"
"net"
"time"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv6"
Expand All @@ -13,13 +14,14 @@ import (
var (
ver = flag.Int("v", 6, "IP version to use")
ifname = flag.String("i", "eth0", "Interface name")
dryrun = flag.Bool("dryrun", false, "Do not change network configuration")
debug = flag.Bool("d", false, "Print debug output")
)

func dhclient6(ifname string, verbose bool) error {
func dhclient6(ifname string, verbose bool) (*netboot.NetConf, error) {
llAddr, err := dhcpv6.GetLinkLocalAddr(ifname)
if err != nil {
return err
return nil, err
}
laddr := net.UDPAddr{
IP: llAddr,
Expand All @@ -41,18 +43,14 @@ func dhclient6(ifname string, verbose bool) error {
}
}
if err != nil {
return err
return nil, err
}
// extract the network configuration
netconf, _, err := netboot.ConversationToNetconf(conv)
if err != nil {
return err
}
// configure the interface
return netboot.ConfigureInterface(ifname, netconf)
return netconf, err
}

func dhclient4(ifname string, verbose bool) error {
func dhclient4(ifname string, verbose bool) (*netboot.NetConf, error) {
client := dhcpv4.NewClient()
conv, err := client.Exchange(ifname, nil)
if verbose {
Expand All @@ -61,27 +59,39 @@ func dhclient4(ifname string, verbose bool) error {
}
}
if err != nil {
return err
return nil, err
}
// extract the network configuration
netconf, _, err := netboot.ConversationToNetconfv4(conv)
if err != nil {
return err
}
// configure the interface
return netboot.ConfigureInterface(ifname, netconf)
return netconf, err
}

func main() {
flag.Parse()

var err error
var (
err error
netconf *netboot.NetConf
)
// bring interface up
_, err = netboot.IfUp(*ifname, 5*time.Second)
if err != nil {
log.Fatal(err)
}
if *ver == 6 {
err = dhclient6(*ifname, *debug)
netconf, err = dhclient6(*ifname, *debug)
} else {
err = dhclient4(*ifname, *debug)
netconf, err = dhclient4(*ifname, *debug)
}
if err != nil {
log.Fatal(err)
}
// configure the interface
if *debug {
log.Printf("Setting network configuration:")
log.Printf("%+v", netconf)
}
if err := netboot.ConfigureInterface(*ifname, netconf); err != nil {
log.Fatal(err)
}
}

0 comments on commit 2d78528

Please sign in to comment.