router7/cmd/dhcp6/dhcp6.go

110 lines
2.8 KiB
Go
Raw Permalink Normal View History

2018-06-28 13:39:48 +02:00
// 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.
2018-05-28 09:53:54 +02:00
// Binary dhcp6 obtains a DHCPv6 lease, persists it to
2018-05-27 17:30:42 +02:00
// /perm/dhcp6/wire/lease.json and notifies netconfigd.
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
2018-06-02 21:00:56 +02:00
"os/signal"
"path"
2018-05-27 17:30:42 +02:00
"path/filepath"
"syscall"
"time"
2018-07-09 08:54:04 +02:00
2019-01-06 15:23:09 +01:00
"github.com/google/renameio"
2020-07-02 22:07:26 +02:00
"github.com/jpillora/backoff"
2018-07-09 08:54:04 +02:00
"github.com/rtr7/router7/internal/dhcp6"
"github.com/rtr7/router7/internal/notify"
"github.com/rtr7/router7/internal/teelogger"
2018-05-27 17:30:42 +02:00
)
2018-06-02 21:00:56 +02:00
var log = teelogger.NewConsole()
var perm = flag.String("perm", "/perm", "path to replace /perm")
2018-05-27 17:30:42 +02:00
func logic() error {
leasePath := path.Join(*perm, "/dhcp6/wire/lease.json")
2019-01-06 15:23:09 +01:00
if err := os.MkdirAll(filepath.Dir(leasePath), 0755); err != nil {
2018-05-27 17:30:42 +02:00
return err
}
duid, err := ioutil.ReadFile(path.Join(*perm, "/dhcp6/duid"))
2018-06-02 21:00:56 +02:00
if err != nil {
log.Printf("could not read %s (%v), proceeding with DUID-LLT", path.Join(*perm, "/dhcp6/duid"), err)
2018-06-02 21:00:56 +02:00
}
2018-05-27 17:30:42 +02:00
c, err := dhcp6.NewClient(dhcp6.ClientConfig{
InterfaceName: "uplink0",
2018-06-02 21:00:56 +02:00
DUID: duid,
2018-05-27 17:30:42 +02:00
})
if err != nil {
return err
}
2018-06-02 21:00:56 +02:00
usr2 := make(chan os.Signal, 1)
signal.Notify(usr2, syscall.SIGUSR2)
2020-07-02 22:07:26 +02:00
backoff := backoff.Backoff{
Factor: 2,
Jitter: true,
Min: 10 * time.Second,
Max: 1 * time.Minute,
}
2018-05-27 17:30:42 +02:00
for c.ObtainOrRenew() {
if err := c.Err(); err != nil {
2020-07-02 22:07:26 +02:00
dur := backoff.Duration()
log.Printf("Temporary error: %v (waiting %v)", err, dur)
time.Sleep(dur)
2018-05-27 17:30:42 +02:00
continue
}
2020-07-02 22:07:26 +02:00
backoff.Reset()
2018-05-27 17:30:42 +02:00
log.Printf("lease: %+v", c.Config())
b, err := json.Marshal(c.Config())
if err != nil {
return err
}
2019-01-06 15:23:09 +01:00
if err := renameio.WriteFile(leasePath, b, 0644); err != nil {
2018-05-27 17:30:42 +02:00
return err
}
if err := notify.Process(path.Join(path.Dir(os.Args[0]), "/netconfigd"), syscall.SIGUSR1); err != nil {
2018-05-27 17:30:42 +02:00
log.Printf("notifying netconfig: %v", err)
}
if err := notify.Process(path.Join(path.Dir(os.Args[0]), "/radvd"), syscall.SIGUSR1); err != nil {
log.Printf("notifying radvd: %v", err)
}
2018-06-02 21:00:56 +02:00
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
}
2018-05-27 17:30:42 +02:00
}
return c.Err() // permanent error
}
func main() {
flag.Parse()
if err := logic(); err != nil {
log.Fatal(err)
}
}