dhcp: make network interface name configurable

related to https://github.com/gokrazy/gokrazy/issues/57
This commit is contained in:
Michael Stapelberg 2020-05-27 08:24:50 +02:00
parent c2116a79ed
commit 9e57e3cf2e

View File

@ -9,11 +9,13 @@ package main
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"os" "os"
"path/filepath"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -127,6 +129,13 @@ func (c *client) request(last *layers.DHCPv4) (*layers.DHCPv4, error) {
func main() { func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
var (
ifname = flag.String(
"interface",
"eth0",
"network interface to obtain a DHCP lease on")
)
flag.Parse()
// NOTE: cannot gokrazy.WaitForClock() here, since the clock can only be // NOTE: cannot gokrazy.WaitForClock() here, since the clock can only be
// initialized once the network is up. // initialized once the network is up.
@ -137,12 +146,12 @@ func main() {
} }
hostname := string(utsname.Nodename[:bytes.IndexByte(utsname.Nodename[:], 0)]) hostname := string(utsname.Nodename[:bytes.IndexByte(utsname.Nodename[:], 0)])
eth0, err := net.InterfaceByName("eth0") intf, err := net.InterfaceByName(*ifname)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
cs, err := iface.NewConfigSocket("eth0") cs, err := iface.NewConfigSocket(*ifname)
if err != nil { if err != nil {
log.Fatalf("config socket: %v", err) log.Fatalf("config socket: %v", err)
} }
@ -156,14 +165,14 @@ func main() {
// Wait for up to 10 seconds for the link to indicate it has a // Wait for up to 10 seconds for the link to indicate it has a
// carrier. // carrier.
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
b, err := ioutil.ReadFile("/sys/class/net/eth0/carrier") b, err := ioutil.ReadFile(filepath.Join("/sys/class/net", *ifname, "carrier"))
if err == nil && strings.TrimSpace(string(b)) == "1" { if err == nil && strings.TrimSpace(string(b)) == "1" {
break break
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
conn, err := raw.ListenPacket(eth0, syscall.ETH_P_IP, &raw.Config{ conn, err := raw.ListenPacket(intf, syscall.ETH_P_IP, &raw.Config{
LinuxSockDGRAM: true, LinuxSockDGRAM: true,
}) })
if err != nil { if err != nil {
@ -172,8 +181,8 @@ func main() {
c := &client{ c := &client{
hostname: hostname, hostname: hostname,
hardwareAddr: eth0.HardwareAddr, hardwareAddr: intf.HardwareAddr,
generateXID: dhcp4.XIDGenerator(eth0.HardwareAddr), generateXID: dhcp4.XIDGenerator(intf.HardwareAddr),
conn: conn, conn: conn,
} }
offer, err := c.discover() offer, err := c.discover()