Parcourir la source

Handle interfaces with multiples IPv4 addresses

Josselin Costanzi il y a 10 ans
Parent
commit
673089b63e
1 fichiers modifiés avec 21 ajouts et 1 suppressions
  1. 21 1
      pkg/ip/iface.go

+ 21 - 1
pkg/ip/iface.go

@@ -7,6 +7,26 @@ import (
 	"github.com/coreos/rudder/Godeps/_workspace/src/github.com/docker/libcontainer/netlink"
 )
 
+func GetIfaceIP4AddrMatch(iface *net.Interface, matchAddr net.IP) (net.IP, error) {
+	addrs, err := iface.Addrs()
+	if err != nil {
+		return nil, err
+	}
+
+	for _, addr := range addrs {
+		// Attempt to parse the address in CIDR notation
+		// and assert it is IPv4
+		ip, _, err := net.ParseCIDR(addr.String())
+		if err == nil && ip.To4() != nil {
+			if ip.To4().Equal(matchAddr) {
+				return ip.To4(), nil
+			}
+		}
+	}
+
+	return nil, errors.New("No IPv4 address found for given interface")
+}
+
 func GetIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
 	addrs, err := iface.Addrs()
 	if err != nil {
@@ -50,7 +70,7 @@ func GetInterfaceByIP(ip net.IP) (*net.Interface, error) {
 	}
 
 	for _, iface := range ifaces {
-		addr, err := GetIfaceIP4Addr(&iface)
+		addr, err := GetIfaceIP4AddrMatch(&iface, ip)
 		if err == nil && ip.Equal(addr) {
 			return &iface, nil
 		}