Skip to content

Commit

Permalink
Pull request: aghnet: fix catching timeout errors
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from fix-is-timeout to master

Squashed commit of the following:

commit b0fefd0
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 29 15:57:06 2022 +0300

    aghnet: fix catching timeout errors
  • Loading branch information
EugeneOne1 committed Mar 29, 2022
1 parent 0d562a7 commit f31ffcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ In this release, the schema version has changed from 12 to 13.

- Go 1.17 support. v0.109.0 will require at least Go 1.18 to build.

### Fixed

- I/O timeout errors on checking another DHCP server.

### Removed

- Go 1.16 support.
Expand Down
39 changes: 14 additions & 25 deletions internal/aghnet/dhcp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func tryConn4(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, n
b := make([]byte, 1500)
n, _, err := c.ReadFrom(b)
if err != nil {
if isTimeout(err) {
if errors.Is(err, os.ErrDeadlineExceeded) {
log.Debug("dhcpv4: didn't receive dhcp response")

return false, false, nil
Expand All @@ -176,20 +176,21 @@ func tryConn4(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, n

log.Debug("dhcpv4: received message from server: %s", response.Summary())

if !(response.OpCode == dhcpv4.OpcodeBootReply &&
response.HWType == iana.HWTypeEthernet &&
bytes.Equal(response.ClientHWAddr, iface.HardwareAddr) &&
bytes.Equal(response.TransactionID[:], req.TransactionID[:]) &&
response.Options.Has(dhcpv4.OptionDHCPMessageType)) {

log.Debug("dhcpv4: received message from server doesn't match our request")
switch {
case
response.OpCode != dhcpv4.OpcodeBootReply,
response.HWType != iana.HWTypeEthernet,
!bytes.Equal(response.ClientHWAddr, iface.HardwareAddr),
response.TransactionID != req.TransactionID,
!response.Options.Has(dhcpv4.OptionDHCPMessageType):
log.Debug("dhcpv4: received response doesn't match the request")

return false, true, nil
}

log.Tracef("dhcpv4: the packet is from an active dhcp server")
default:
log.Tracef("dhcpv4: the packet is from an active dhcp server")

return true, false, nil
return true, false, nil
}
}

// checkOtherDHCPv6 sends a DHCP request to the specified network interface, and
Expand Down Expand Up @@ -275,7 +276,7 @@ func tryConn6(req *dhcpv6.Message, c net.PacketConn) (ok, next bool, err error)

n, _, err := c.ReadFrom(b)
if err != nil {
if isTimeout(err) {
if errors.Is(err, os.ErrDeadlineExceeded) {
log.Debug("dhcpv6: didn't receive dhcp response")

return false, false, nil
Expand Down Expand Up @@ -318,15 +319,3 @@ func tryConn6(req *dhcpv6.Message, c net.PacketConn) (ok, next bool, err error)

return true, false, nil
}

// isTimeout returns true if err is an operation timeout error from net package.
//
// TODO(e.burkov): Consider moving into netutil.
func isTimeout(err error) (ok bool) {
var operr *net.OpError
if errors.As(err, &operr) {
return operr.Timeout()
}

return false
}

0 comments on commit f31ffcc

Please sign in to comment.