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-01-31 21:36:24 +07:00
committed by Cuong Manh Le
parent 149941f17f
commit 37de5441c1
2 changed files with 20 additions and 28 deletions

View File

@@ -221,8 +221,8 @@ func initCLI() {
{s.Start, true},
}
if doTasks(tasks) {
mainLog.Info().Msg("Service started")
prog.setDNS()
mainLog.Info().Msg("Service started")
}
},
}
@@ -252,8 +252,8 @@ func initCLI() {
}
initLogging()
if doTasks([]task{{s.Stop, true}}) {
mainLog.Info().Msg("Service stopped")
prog.resetDNS()
mainLog.Info().Msg("Service stopped")
}
},
}
@@ -321,8 +321,8 @@ func initCLI() {
}
initLogging()
if doTasks(tasks) {
mainLog.Info().Msg("Service uninstalled")
prog.resetDNS()
mainLog.Info().Msg("Service uninstalled")
return
}
},

View File

@@ -14,7 +14,7 @@ import (
"github.com/insomniacslk/dhcp/dhcpv4/nclient4"
"github.com/insomniacslk/dhcp/dhcpv6"
"github.com/insomniacslk/dhcp/dhcpv6/nclient6"
"github.com/insomniacslk/dhcp/dhcpv6/client6"
"tailscale.com/net/dns"
"tailscale.com/util/dnsname"
@@ -84,37 +84,29 @@ func resetDNS(iface *net.Interface) error {
ns = append(ns, nameserver.String())
}
// TODO(cuonglm): handle DHCPv6 properly.
if supportsIPv6() {
c, err := nclient6.New(iface.Name)
c := client6.NewClient()
conversation, err := c.Exchange(iface.Name)
if err != nil {
mainLog.Warn().Err(err).Msg("could not create DHCPv6 client")
mainLog.Debug().Err(err).Msg("could not exchange DHCPv6")
return nil
}
defer c.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
solicit, err := dhcpv6.NewSolicit(iface.HardwareAddr)
if err != nil {
return fmt.Errorf("dhcpv6.NewSolicit: %w", err)
}
advertise, err := dhcpv6.NewAdvertiseFromSolicit(solicit)
if err != nil {
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
for _, packet := range conversation {
if packet.Type() == dhcpv6.MessageTypeReply {
msg, err := packet.GetInnerMessage()
if err != nil {
mainLog.Debug().Err(err).Msg("could not get inner DHCPv6 message")
return nil
}
nameservers := msg.Options.DNS()
for _, nameserver := range nameservers {
ns = append(ns, nameserver.String())
}
}
ns = append(ns, nameserver.String())
}
}
return ignoringEINTR(func() error {
return setDNS(iface, ns)
})