-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
97 lines (82 loc) · 2.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"flag"
"fmt"
"log/slog"
"net"
"os"
"os/signal"
"syscall"
"github.com/digineo/go-dhclient"
"github.com/google/gopacket/layers"
)
var (
options = optionList{}
requestParams = byteList{}
)
func init() {
flag.Usage = func() {
fmt.Printf("syntax: %s [flags] IFNAME\n", os.Args[0])
flag.PrintDefaults()
}
flag.Var(&options, "option", "custom DHCP option for the request (code,value)")
flag.Var(&requestParams, "request", "Additional value for the DHCP Request List Option 55 (code)")
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
ifname := flag.Arg(0)
iface, err := net.InterfaceByName(ifname)
if err != nil {
fmt.Printf("unable to find interface %s: %s\n", ifname, err)
os.Exit(1)
}
logHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})
logger := slog.New(logHandler)
client := dhclient.Client{
Iface: iface,
Logger: logger,
OnBound: func(lease *dhclient.Lease) {
logger.Info("bound", "lease", lease)
},
}
// Add requests for default options
for _, param := range dhclient.DefaultParamsRequestList {
logger.Info("Requesting default option", "param", param)
client.AddParamRequest(layers.DHCPOpt(param))
}
// Add requests for custom options
for _, param := range requestParams {
logger.Info("Requesting custom option", "param", param)
client.AddParamRequest(layers.DHCPOpt(param))
}
// Add hostname option
hostname, _ := os.Hostname()
client.AddOption(layers.DHCPOptHostname, []byte(hostname))
// Add custom options
for _, option := range options {
slog.Info("Adding custom option", "type", option.Type, "value", fmt.Sprintf("0x%x", option.Data))
client.AddOption(option.Type, option.Data)
}
client.Start()
defer client.Stop()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGUSR1)
for {
sig := <-c
logger.Info("received signal", "type", sig)
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
return
case syscall.SIGHUP:
logger.Info("renew lease")
client.Renew()
case syscall.SIGUSR1:
logger.Info("acquire new lease")
client.Rebind()
}
}
}