cmd/ctrld: silent DHCPv6 error

It's hard to imagine a system with IPv6 but not IPv4, so silent the
DHCPv6 error if any.
This commit is contained in:
Cuong Manh Le
2023-02-01 23:11:33 +07:00
committed by Cuong Manh Le
parent 149941f17f
commit 37de5441c1
2 changed files with 20 additions and 28 deletions
+3 -3
View File
@@ -221,8 +221,8 @@ func initCLI() {
{s.Start, true}, {s.Start, true},
} }
if doTasks(tasks) { if doTasks(tasks) {
mainLog.Info().Msg("Service started")
prog.setDNS() prog.setDNS()
mainLog.Info().Msg("Service started")
} }
}, },
} }
@@ -252,8 +252,8 @@ func initCLI() {
} }
initLogging() initLogging()
if doTasks([]task{{s.Stop, true}}) { if doTasks([]task{{s.Stop, true}}) {
mainLog.Info().Msg("Service stopped")
prog.resetDNS() prog.resetDNS()
mainLog.Info().Msg("Service stopped")
} }
}, },
} }
@@ -321,8 +321,8 @@ func initCLI() {
} }
initLogging() initLogging()
if doTasks(tasks) { if doTasks(tasks) {
mainLog.Info().Msg("Service uninstalled")
prog.resetDNS() prog.resetDNS()
mainLog.Info().Msg("Service uninstalled")
return return
} }
}, },
+17 -25
View File
@@ -14,7 +14,7 @@ import (
"github.com/insomniacslk/dhcp/dhcpv4/nclient4" "github.com/insomniacslk/dhcp/dhcpv4/nclient4"
"github.com/insomniacslk/dhcp/dhcpv6" "github.com/insomniacslk/dhcp/dhcpv6"
"github.com/insomniacslk/dhcp/dhcpv6/nclient6" "github.com/insomniacslk/dhcp/dhcpv6/client6"
"tailscale.com/net/dns" "tailscale.com/net/dns"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
@@ -84,37 +84,29 @@ func resetDNS(iface *net.Interface) error {
ns = append(ns, nameserver.String()) ns = append(ns, nameserver.String())
} }
// TODO(cuonglm): handle DHCPv6 properly.
if supportsIPv6() { if supportsIPv6() {
c, err := nclient6.New(iface.Name) c := client6.NewClient()
conversation, err := c.Exchange(iface.Name)
if err != nil { if err != nil {
mainLog.Warn().Err(err).Msg("could not create DHCPv6 client") mainLog.Debug().Err(err).Msg("could not exchange DHCPv6")
return nil return nil
} }
defer c.Close() for _, packet := range conversation {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) if packet.Type() == dhcpv6.MessageTypeReply {
defer cancel() msg, err := packet.GetInnerMessage()
if err != nil {
solicit, err := dhcpv6.NewSolicit(iface.HardwareAddr) mainLog.Debug().Err(err).Msg("could not get inner DHCPv6 message")
if err != nil { return nil
return fmt.Errorf("dhcpv6.NewSolicit: %w", err) }
} nameservers := msg.Options.DNS()
advertise, err := dhcpv6.NewAdvertiseFromSolicit(solicit) for _, nameserver := range nameservers {
if err != nil { ns = append(ns, nameserver.String())
return fmt.Errorf("dhcpv6.NewAdvertiseFromSolicit: %w", err) }
}
msg, err := c.Request(ctx, advertise)
if err != nil {
return fmt.Errorf("nclient6.Request: %w", err)
}
nameservers := msg.Options.DNS()
for _, nameserver := range nameservers {
if nameserver.Equal(net.IPv6zero) {
continue
} }
ns = append(ns, nameserver.String())
} }
} }
return ignoringEINTR(func() error { return ignoringEINTR(func() error {
return setDNS(iface, ns) return setDNS(iface, ns)
}) })