github.com/erriapo/docker@v1.6.0-rc2/daemon/networkdriver/utils.go (about) 1 package networkdriver 2 3 import ( 4 "errors" 5 "fmt" 6 "net" 7 8 "github.com/docker/libcontainer/netlink" 9 ) 10 11 var ( 12 networkGetRoutesFct = netlink.NetworkGetRoutes 13 ErrNoDefaultRoute = errors.New("no default route") 14 ) 15 16 func CheckNameserverOverlaps(nameservers []string, toCheck *net.IPNet) error { 17 if len(nameservers) > 0 { 18 for _, ns := range nameservers { 19 _, nsNetwork, err := net.ParseCIDR(ns) 20 if err != nil { 21 return err 22 } 23 if NetworkOverlaps(toCheck, nsNetwork) { 24 return ErrNetworkOverlapsWithNameservers 25 } 26 } 27 } 28 return nil 29 } 30 31 func CheckRouteOverlaps(toCheck *net.IPNet) error { 32 networks, err := networkGetRoutesFct() 33 if err != nil { 34 return err 35 } 36 37 for _, network := range networks { 38 if network.IPNet != nil && NetworkOverlaps(toCheck, network.IPNet) { 39 return ErrNetworkOverlaps 40 } 41 } 42 return nil 43 } 44 45 // Detects overlap between one IPNet and another 46 func NetworkOverlaps(netX *net.IPNet, netY *net.IPNet) bool { 47 if len(netX.IP) == len(netY.IP) { 48 if firstIP, _ := NetworkRange(netX); netY.Contains(firstIP) { 49 return true 50 } 51 if firstIP, _ := NetworkRange(netY); netX.Contains(firstIP) { 52 return true 53 } 54 } 55 return false 56 } 57 58 // Calculates the first and last IP addresses in an IPNet 59 func NetworkRange(network *net.IPNet) (net.IP, net.IP) { 60 var netIP net.IP 61 if network.IP.To4() != nil { 62 netIP = network.IP.To4() 63 } else if network.IP.To16() != nil { 64 netIP = network.IP.To16() 65 } else { 66 return nil, nil 67 } 68 69 lastIP := make([]byte, len(netIP), len(netIP)) 70 71 for i := 0; i < len(netIP); i++ { 72 lastIP[i] = netIP[i] | ^network.Mask[i] 73 } 74 return netIP.Mask(network.Mask), net.IP(lastIP) 75 } 76 77 // Return the first IPv4 address and slice of IPv6 addresses for the specified network interface 78 func GetIfaceAddr(name string) (net.Addr, []net.Addr, error) { 79 iface, err := net.InterfaceByName(name) 80 if err != nil { 81 return nil, nil, err 82 } 83 addrs, err := iface.Addrs() 84 if err != nil { 85 return nil, nil, err 86 } 87 var addrs4 []net.Addr 88 var addrs6 []net.Addr 89 for _, addr := range addrs { 90 ip := (addr.(*net.IPNet)).IP 91 if ip4 := ip.To4(); ip4 != nil { 92 addrs4 = append(addrs4, addr) 93 } else if ip6 := ip.To16(); len(ip6) == net.IPv6len { 94 addrs6 = append(addrs6, addr) 95 } 96 } 97 switch { 98 case len(addrs4) == 0: 99 return nil, nil, fmt.Errorf("Interface %v has no IPv4 addresses", name) 100 case len(addrs4) > 1: 101 fmt.Printf("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n", 102 name, (addrs4[0].(*net.IPNet)).IP) 103 } 104 return addrs4[0], addrs6, nil 105 } 106 107 func GetDefaultRouteIface() (*net.Interface, error) { 108 rs, err := networkGetRoutesFct() 109 if err != nil { 110 return nil, fmt.Errorf("unable to get routes: %v", err) 111 } 112 for _, r := range rs { 113 if r.Default { 114 return r.Iface, nil 115 } 116 } 117 return nil, ErrNoDefaultRoute 118 }