From 09196118e9f5f089dc09e792f040bdc97fcc7432 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Fri, 13 Nov 2020 14:40:22 +0300 Subject: [PATCH] Pull request: dhcpd: fix possible infinite loop, set timeouts better Merge in DNS/adguard-home from 2301-dhcp-check to master Closes #2301. Squashed commit of the following: commit bf5c31b8592f909a372fcdaebacc491d310cc3e6 Author: Ainar Garipov Date: Fri Nov 13 14:28:54 2020 +0300 dhcpd: fix possible infinite loop, set timeouts better --- CHANGELOG.md | 4 ++++ internal/dhcpd/check_other_dhcp.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e67b966..63909d43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,3 +13,7 @@ and this project adheres to - This changelog :-) (#2294). - `HACKING.md`, a guide for developers. + +### Fixed + +- Infinite loop in `/dhcp/find_active_dhcp` (#2301). diff --git a/internal/dhcpd/check_other_dhcp.go b/internal/dhcpd/check_other_dhcp.go index 0292528c..674949aa 100644 --- a/internal/dhcpd/check_other_dhcp.go +++ b/internal/dhcpd/check_other_dhcp.go @@ -88,13 +88,17 @@ func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, error) { for { ok, next, err := tryConn(req, c, iface) if next { + if err != nil { + log.Debug("dhcpv4: trying a connection: %s", err) + } + continue } if ok { return true, nil } if err != nil { - log.Debug("%s", err) + return false, err } } } @@ -107,7 +111,11 @@ func tryConn(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, ne log.Tracef("waiting %v for an answer", defaultDiscoverTime) b := make([]byte, 1500) - _ = c.SetReadDeadline(time.Now().Add(defaultDiscoverTime)) + err = c.SetDeadline(time.Now().Add(defaultDiscoverTime)) + if err != nil { + return false, false, fmt.Errorf("setting deadline: %w", err) + } + n, _, err := c.ReadFrom(b) if err != nil { if isTimeout(err) {