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 <A.Garipov@AdGuard.COM>
Date:   Fri Nov 13 14:28:54 2020 +0300

    dhcpd: fix possible infinite loop, set timeouts better
This commit is contained in:
Ainar Garipov 2020-11-13 14:40:22 +03:00
parent e802e6645e
commit 09196118e9
2 changed files with 14 additions and 2 deletions

View File

@ -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).

View File

@ -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) {