-
Notifications
You must be signed in to change notification settings - Fork 110
/
dhcp6.go
106 lines (97 loc) · 2.68 KB
/
dhcp6.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
98
99
100
101
102
103
104
105
106
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Binary dhcp6 obtains a DHCPv6 lease, persists it to
// /perm/dhcp6/wire/lease.json and notifies netconfigd.
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/google/renameio"
"github.com/jpillora/backoff"
"github.com/rtr7/router7/internal/dhcp6"
"github.com/rtr7/router7/internal/notify"
"github.com/rtr7/router7/internal/teelogger"
)
var log = teelogger.NewConsole()
func logic() error {
const leasePath = "/perm/dhcp6/wire/lease.json"
if err := os.MkdirAll(filepath.Dir(leasePath), 0755); err != nil {
return err
}
duid, err := ioutil.ReadFile("/perm/dhcp6/duid")
if err != nil {
log.Printf("could not read /perm/dhcp6/duid (%v), proceeding with DUID-LLT", err)
}
c, err := dhcp6.NewClient(dhcp6.ClientConfig{
InterfaceName: "uplink0",
DUID: duid,
})
if err != nil {
return err
}
usr2 := make(chan os.Signal, 1)
signal.Notify(usr2, syscall.SIGUSR2)
backoff := backoff.Backoff{
Factor: 2,
Jitter: true,
Min: 10 * time.Second,
Max: 1 * time.Minute,
}
for c.ObtainOrRenew() {
if err := c.Err(); err != nil {
dur := backoff.Duration()
log.Printf("Temporary error: %v (waiting %v)", err, dur)
time.Sleep(dur)
continue
}
backoff.Reset()
log.Printf("lease: %+v", c.Config())
b, err := json.Marshal(c.Config())
if err != nil {
return err
}
if err := renameio.WriteFile(leasePath, b, 0644); err != nil {
return err
}
if err := notify.Process("/user/netconfigd", syscall.SIGUSR1); err != nil {
log.Printf("notifying netconfig: %v", err)
}
if err := notify.Process("/user/radvd", syscall.SIGUSR1); err != nil {
log.Printf("notifying radvd: %v", err)
}
select {
case <-time.After(time.Until(c.Config().RenewAfter)):
// fallthrough and renew the DHCP lease
case <-usr2:
log.Printf("SIGUSR2 received, sending DHCPRELEASE")
if _, _, err := c.Release(); err != nil {
return err
}
os.Exit(125) // quit supervision by gokrazy
}
}
return c.Err() // permanent error
}
func main() {
flag.Parse()
if err := logic(); err != nil {
log.Fatal(err)
}
}