"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "internal/dhcpd/check_other_dhcp.go" between
AdGuardHome-0.104.1.tar.gz and AdGuardHome-0.104.3.tar.gz

About: AdGuard Home is a network-wide ads & trackers blocking DNS server. It operates as a DNS server that re-routes tracking domains to a "black hole," thus preventing your devices from connecting to those servers.

check_other_dhcp.go  (AdGuardHome-0.104.1):check_other_dhcp.go  (AdGuardHome-0.104.3)
skipping to change at line 89 skipping to change at line 89
defer c.Close() defer c.Close()
} }
// send to 255.255.255.255:67 // send to 255.255.255.255:67
_, err = c.WriteTo(req.ToBytes(), dstAddr) _, err = c.WriteTo(req.ToBytes(), dstAddr)
if err != nil { if err != nil {
return false, fmt.Errorf("couldn't send a packet to %s: %w", dst, err) return false, fmt.Errorf("couldn't send a packet to %s: %w", dst, err)
} }
for { for {
ok, next, err := tryConn(req, c, iface) ok, next, err := tryConn4(req, c, iface)
if next { if next {
if err != nil {
log.Debug("dhcpv4: trying a connection: %s", err)
}
continue continue
} }
if ok { if ok {
return true, nil return true, nil
} }
if err != nil { if err != nil {
log.Debug("%s", err) return false, err
} }
} }
} }
// TODO(a.garipov): Refactor further. Inspect error handling, remove the next // TODO(a.garipov): Refactor further. Inspect error handling, remove parameter
// parameter, address the TODO, etc. // next, address the TODO, merge with tryConn6, etc.
func tryConn(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, ne func tryConn4(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, n
xt bool, err error) { ext bool, err error) {
// TODO: replicate dhclient's behavior of retrying several times with // TODO: replicate dhclient's behavior of retrying several times with
// progressively longer timeouts. // progressively longer timeouts.
log.Tracef("waiting %v for an answer", defaultDiscoverTime) log.Tracef("dhcpv4: waiting %v for an answer", defaultDiscoverTime)
b := make([]byte, 1500) 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) n, _, err := c.ReadFrom(b)
if err != nil { if err != nil {
if isTimeout(err) { if isTimeout(err) {
log.Debug("dhcpv4: didn't receive dhcp response") log.Debug("dhcpv4: didn't receive dhcp response")
return false, false, nil return false, false, nil
} }
return false, false, fmt.Errorf("receiving packet: %w", err) return false, false, fmt.Errorf("receiving packet: %w", err)
} }
log.Tracef("received packet, %d bytes", n) log.Tracef("dhcpv4: received packet, %d bytes", n)
response, err := dhcpv4.FromBytes(b[:n]) response, err := dhcpv4.FromBytes(b[:n])
if err != nil { if err != nil {
log.Debug("dhcpv4: encoding: %s", err) log.Debug("dhcpv4: encoding: %s", err)
return false, true, err return false, true, err
} }
log.Debug("dhcpv4: received message from server: %s", response.Summary()) log.Debug("dhcpv4: received message from server: %s", response.Summary())
skipping to change at line 144 skipping to change at line 152
response.HWType == iana.HWTypeEthernet && response.HWType == iana.HWTypeEthernet &&
bytes.Equal(response.ClientHWAddr, iface.HardwareAddr) && bytes.Equal(response.ClientHWAddr, iface.HardwareAddr) &&
bytes.Equal(response.TransactionID[:], req.TransactionID[:]) && bytes.Equal(response.TransactionID[:], req.TransactionID[:]) &&
response.Options.Has(dhcpv4.OptionDHCPMessageType)) { response.Options.Has(dhcpv4.OptionDHCPMessageType)) {
log.Debug("dhcpv4: received message from server doesn't match our request") log.Debug("dhcpv4: received message from server doesn't match our request")
return false, true, nil return false, true, nil
} }
log.Tracef("the packet is from an active dhcp server") log.Tracef("dhcpv4: the packet is from an active dhcp server")
return true, false, nil return true, false, nil
} }
// CheckIfOtherDHCPServersPresentV6 sends a DHCP request to the specified networ k interface, // CheckIfOtherDHCPServersPresentV6 sends a DHCP request to the specified networ k interface,
// and waits for a response for a period defined by defaultDiscoverTime // and waits for a response for a period defined by defaultDiscoverTime
func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) { func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error) {
iface, err := net.InterfaceByName(ifaceName) iface, err := net.InterfaceByName(ifaceName)
if err != nil { if err != nil {
return false, fmt.Errorf("dhcpv6: net.InterfaceByName: %s: %w", i faceName, err) return false, fmt.Errorf("dhcpv6: net.InterfaceByName: %s: %w", i faceName, err)
skipping to change at line 203 skipping to change at line 211
if c != nil { if c != nil {
defer c.Close() defer c.Close()
} }
_, err = c.WriteTo(req.ToBytes(), dstAddr) _, err = c.WriteTo(req.ToBytes(), dstAddr)
if err != nil { if err != nil {
return false, fmt.Errorf("dhcpv6: Couldn't send a packet to %s: % w", dst, err) return false, fmt.Errorf("dhcpv6: Couldn't send a packet to %s: % w", dst, err)
} }
for { for {
log.Debug("DHCPv6: Waiting %v for an answer", defaultDiscoverTime ok, next, err := tryConn6(req, c)
) if next {
b := make([]byte, 4096) if err != nil {
_ = c.SetReadDeadline(time.Now().Add(defaultDiscoverTime)) log.Debug("dhcpv6: trying a connection: %s", err)
n, _, err := c.ReadFrom(b) }
if isTimeout(err) {
log.Debug("DHCPv6: didn't receive DHCP response") continue
return false, nil }
if ok {
return true, nil
} }
if err != nil { if err != nil {
return false, fmt.Errorf("couldn't receive packet: %w", e rr) return false, err
} }
}
}
log.Debug("DHCPv6: Received packet (%v bytes)", n) // TODO(a.garipov): See the comment on tryConn4. Sigh…
func tryConn6(req *dhcpv6.Message, c net.PacketConn) (ok, next bool, err error)
{
// TODO: replicate dhclient's behavior of retrying several times with
// progressively longer timeouts.
log.Tracef("dhcpv6: waiting %v for an answer", defaultDiscoverTime)
resp, err := dhcpv6.FromBytes(b[:n]) b := make([]byte, 4096)
if err != nil { err = c.SetDeadline(time.Now().Add(defaultDiscoverTime))
log.Debug("DHCPv6: dhcpv6.FromBytes: %s", err) if err != nil {
continue return false, false, fmt.Errorf("setting deadline: %w", err)
} }
log.Debug("DHCPv6: received message from server: %s", resp.Summar n, _, err := c.ReadFrom(b)
y()) if err != nil {
if isTimeout(err) {
log.Debug("dhcpv6: didn't receive dhcp response")
cid := req.Options.ClientID() return false, false, nil
msg, err := resp.GetInnerMessage()
if err != nil {
log.Debug("DHCPv6: resp.GetInnerMessage: %s", err)
continue
}
rcid := msg.Options.ClientID()
if resp.Type() == dhcpv6.MessageTypeAdvertise &&
msg.TransactionID == req.TransactionID &&
rcid != nil &&
cid.Equal(*rcid) {
log.Debug("DHCPv6: The packet is from an active DHCP serv
er")
return true, nil
} }
log.Debug("DHCPv6: received message from server doesn't match our return false, false, fmt.Errorf("receiving packet: %w", err)
request") }
log.Tracef("dhcpv6: received packet, %d bytes", n)
response, err := dhcpv6.FromBytes(b[:n])
if err != nil {
log.Debug("dhcpv6: encoding: %s", err)
return false, true, err
}
log.Debug("dhcpv6: received message from server: %s", response.Summary())
cid := req.Options.ClientID()
msg, err := response.GetInnerMessage()
if err != nil {
log.Debug("dhcpv6: resp.GetInnerMessage(): %s", err)
return false, true, err
}
rcid := msg.Options.ClientID()
if !(response.Type() == dhcpv6.MessageTypeAdvertise &&
msg.TransactionID == req.TransactionID &&
rcid != nil &&
cid.Equal(*rcid)) {
log.Debug("dhcpv6: received message from server doesn't match our
request")
return false, true, nil
} }
log.Tracef("dhcpv6: the packet is from an active dhcp server")
return true, false, nil
} }
 End of changes. 17 change blocks. 
43 lines changed or deleted 83 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)