From aee09d29e9d622362fdfa85b3cacf45daf05658a Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Wed, 21 Apr 2021 14:42:19 +0300 Subject: [PATCH] Pull request: dhcpd: fix dbload panic Closes #2991. Squashed commit of the following: commit cd820a9a903d628aa5964f8f973edc7bb7993ae5 Author: Ainar Garipov Date: Wed Apr 21 14:34:45 2021 +0300 dhcpd: fix err handling commit 775b834683f0edb5954b20fedb896e6af1fa843e Author: Ainar Garipov Date: Wed Apr 21 14:21:41 2021 +0300 dhcpd: fix build for go1.15 commit 8441d6fffa72cc6890fad2df76559e21819786d6 Author: Ainar Garipov Date: Wed Apr 21 14:05:08 2021 +0300 dhcpd: fix dbload panic --- Makefile | 12 ++++++++---- internal/dhcpd/dhcpd.go | 6 ++---- internal/dhcpd/v4.go | 28 +++++++++++++++++++++++----- internal/home/home.go | 5 ++++- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index d4441c47..52cb1d57 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ CLIENT_BETA_DIR = client2 CLIENT_DIR = client COMMIT = $$(git rev-parse --short HEAD) DIST_DIR = dist +# TODO(a.garipov): Find out a way to make this work in GNU Make. +# +# GO = $${GO:-go} +# GO = go GOPROXY = https://goproxy.cn|https://proxy.golang.org|direct GPG_KEY = devteam@adguard.com @@ -93,10 +97,10 @@ go-check: go-tools go-lint go-test # A quick check to make sure that all supported operating systems can be # typechecked and built successfully. go-os-check: - env GOOS='darwin' go vet ./internal/... - env GOOS='freebsd' go vet ./internal/... - env GOOS='linux' go vet ./internal/... - env GOOS='windows' go vet ./internal/... + env GOOS='darwin' $(GO) vet ./internal/... + env GOOS='freebsd' $(GO) vet ./internal/... + env GOOS='linux' $(GO) vet ./internal/... + env GOOS='windows' $(GO) vet ./internal/... openapi-lint: ; cd ./openapi/ && $(YARN) test openapi-show: ; cd ./openapi/ && $(YARN) start diff --git a/internal/dhcpd/dhcpd.go b/internal/dhcpd/dhcpd.go index 26388cb0..3d2b8a8b 100644 --- a/internal/dhcpd/dhcpd.go +++ b/internal/dhcpd/dhcpd.go @@ -242,16 +242,14 @@ func (s *Server) WriteDiskConfig(c *ServerConfig) { } // Start will listen on port 67 and serve DHCP requests. -func (s *Server) Start() error { - err := s.srv4.Start() +func (s *Server) Start() (err error) { + err = s.srv4.Start() if err != nil { - log.Error("DHCPv4: start: %s", err) return err } err = s.srv6.Start() if err != nil { - log.Error("DHCPv6: start: %s", err) return err } diff --git a/internal/dhcpd/v4.go b/internal/dhcpd/v4.go index 2a4bbc27..5cbfc370 100644 --- a/internal/dhcpd/v4.go +++ b/internal/dhcpd/v4.go @@ -51,6 +51,12 @@ func (s *v4Server) WriteDiskConfig6(c *V6ServerConf) { // ResetLeases - reset leases func (s *v4Server) ResetLeases(leases []*Lease) { + if !s.conf.Enabled { + return + } + + s.leasedOffsets = newBitSet() + s.leaseHosts = aghstrings.NewSet() s.leases = nil for _, l := range leases { @@ -826,7 +832,9 @@ func (s *v4Server) packetHandler(conn net.PacketConn, peer net.Addr, req *dhcpv4 } // Start starts the IPv4 DHCP server. -func (s *v4Server) Start() error { +func (s *v4Server) Start() (err error) { + defer agherr.Annotate("dhcpv4: %w", &err) + if !s.conf.Enabled { return nil } @@ -834,14 +842,14 @@ func (s *v4Server) Start() error { ifaceName := s.conf.InterfaceName iface, err := net.InterfaceByName(ifaceName) if err != nil { - return fmt.Errorf("dhcpv4: finding interface %s by name: %w", ifaceName, err) + return fmt.Errorf("finding interface %s by name: %w", ifaceName, err) } log.Debug("dhcpv4: starting...") dnsIPAddrs, err := ifaceDNSIPAddrs(iface, ipVersion4, defaultMaxAttempts, defaultBackoff) if err != nil { - return fmt.Errorf("dhcpv4: interface %s: %w", ifaceName, err) + return fmt.Errorf("interface %s: %w", ifaceName, err) } if len(dnsIPAddrs) == 0 { @@ -863,8 +871,18 @@ func (s *v4Server) Start() error { log.Info("dhcpv4: listening") go func() { - err = s.srv.Serve() - log.Debug("dhcpv4: srv.Serve: %s", err) + serr := s.srv.Serve() + // TODO(a.garipov): Uncomment in Go 1.16. + // + // if errors.Is(serr, net.ErrClosed) { + // log.Info("dhcpv4: server is closed") + // + // return + // } + + if serr != nil { + log.Error("dhcpv4: srv.Serve: %s", serr) + } }() // Signal to the clients containers in packages home and dnsforward that diff --git a/internal/home/home.go b/internal/home/home.go index 4a590639..b202d187 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -332,7 +332,10 @@ func run(args options) { }() if Context.dhcpServer != nil { - _ = Context.dhcpServer.Start() + err = Context.dhcpServer.Start() + if err != nil { + log.Error("starting dhcp server: %s", err) + } } }