2021-08-25 17:52:05 +00:00
|
|
|
package routing
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
2022-09-14 02:18:10 +02:00
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/netlink"
|
2021-08-25 17:52:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func IPIsPrivate(ip net.IP) bool {
|
|
|
|
|
return ip.IsPrivate() || ip.IsLoopback() ||
|
|
|
|
|
ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
errInterfaceIPNotFound = errors.New("IP address not found for interface")
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-14 02:18:10 +02:00
|
|
|
func ipMatchesFamily(ip net.IP, family int) bool {
|
|
|
|
|
return (family == netlink.FAMILY_V6 && ip.To4() == nil) ||
|
|
|
|
|
(family == netlink.FAMILY_V4 && ip.To4() != nil)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 13:41:05 +00:00
|
|
|
func ensureNoIPv6WrappedIPv4(candidateIP net.IP) (resultIP net.IP) {
|
|
|
|
|
const ipv4Size = 4
|
|
|
|
|
if candidateIP.To4() == nil || len(candidateIP) == ipv4Size { // ipv6 or ipv4
|
|
|
|
|
return candidateIP
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ipv6-wrapped ipv4
|
|
|
|
|
resultIP = make(net.IP, ipv4Size)
|
|
|
|
|
copy(resultIP, candidateIP[12:16])
|
|
|
|
|
return resultIP
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 02:18:10 +02:00
|
|
|
func (r *Routing) assignedIP(interfaceName string, family int) (ip net.IP, err error) {
|
2021-08-25 17:52:05 +00:00
|
|
|
iface, err := net.InterfaceByName(interfaceName)
|
|
|
|
|
if err != nil {
|
2022-02-20 02:58:16 +00:00
|
|
|
return nil, fmt.Errorf("network interface %s not found: %w", interfaceName, err)
|
2021-08-25 17:52:05 +00:00
|
|
|
}
|
|
|
|
|
addresses, err := iface.Addrs()
|
|
|
|
|
if err != nil {
|
2023-04-01 16:53:04 +00:00
|
|
|
return nil, fmt.Errorf("listing interface %s addresses: %w", interfaceName, err)
|
2021-08-25 17:52:05 +00:00
|
|
|
}
|
|
|
|
|
for _, address := range addresses {
|
|
|
|
|
switch value := address.(type) {
|
|
|
|
|
case *net.IPAddr:
|
2023-04-27 13:41:05 +00:00
|
|
|
ip = value.IP
|
2021-08-25 17:52:05 +00:00
|
|
|
case *net.IPNet:
|
2023-04-27 13:41:05 +00:00
|
|
|
ip = value.IP
|
|
|
|
|
default:
|
|
|
|
|
continue
|
2021-08-25 17:52:05 +00:00
|
|
|
}
|
2023-04-27 13:41:05 +00:00
|
|
|
|
|
|
|
|
if !ipMatchesFamily(ip, family) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure we don't return an IPv6-wrapped IPv4 address
|
|
|
|
|
// since netip.Address String method works differently than
|
|
|
|
|
// net.IP String method for this kind of addresses.
|
|
|
|
|
ip = ensureNoIPv6WrappedIPv4(ip)
|
|
|
|
|
return ip, nil
|
2021-08-25 17:52:05 +00:00
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("%w: interface %s in %d addresses",
|
|
|
|
|
errInterfaceIPNotFound, interfaceName, len(addresses))
|
|
|
|
|
}
|