Files
Quentin McGaw 4a78989d9d chore: do not use sentinel errors when unneeded
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly
- only use sentinel errors when it has to be checked using `errors.Is`
- replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error
- exclude the sentinel error definition requirement from .golangci.yml
- update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
2026-05-02 03:29:46 +00:00

49 lines
1.2 KiB
Go

package routing
import (
"fmt"
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/netlink"
)
func ipIsPrivate(ip netip.Addr) bool {
return ip.IsPrivate() || ip.IsLoopback() ||
ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
}
func ipMatchesFamily(ip netip.Addr, family uint8) bool {
return (family == netlink.FamilyV4 && ip.Is4()) ||
(family == netlink.FamilyV6 && ip.Is6())
}
func (r *Routing) AssignedIP(interfaceName string, family uint8) (ip netip.Addr, err error) {
iface, err := net.InterfaceByName(interfaceName)
if err != nil {
return ip, fmt.Errorf("network interface %s not found: %w", interfaceName, err)
}
addresses, err := iface.Addrs()
if err != nil {
return ip, fmt.Errorf("listing interface %s addresses: %w", interfaceName, err)
}
for _, address := range addresses {
switch value := address.(type) {
case *net.IPAddr:
ip = netIPToNetipAddress(value.IP)
case *net.IPNet:
ip = netIPToNetipAddress(value.IP)
default:
continue
}
if !ipMatchesFamily(ip, family) {
continue
}
return ip, nil
}
return ip, fmt.Errorf("IP address not found for interface: interface %s in %d addresses",
interfaceName, len(addresses))
}