utils_linux.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build linux
  2. // Network utility functions.
  3. package netutils
  4. import (
  5. "net"
  6. "strings"
  7. "github.com/docker/libnetwork/types"
  8. "github.com/vishvananda/netlink"
  9. )
  10. var (
  11. networkGetRoutesFct = netlink.RouteList
  12. )
  13. // CheckRouteOverlaps checks whether the passed network overlaps with any existing routes
  14. func CheckRouteOverlaps(toCheck *net.IPNet) error {
  15. networks, err := networkGetRoutesFct(nil, netlink.FAMILY_V4)
  16. if err != nil {
  17. return err
  18. }
  19. for _, network := range networks {
  20. if network.Dst != nil && NetworkOverlaps(toCheck, network.Dst) {
  21. return ErrNetworkOverlaps
  22. }
  23. }
  24. return nil
  25. }
  26. // GenerateIfaceName returns an interface name using the passed in
  27. // prefix and the length of random bytes. The api ensures that the
  28. // there are is no interface which exists with that name.
  29. func GenerateIfaceName(prefix string, len int) (string, error) {
  30. for i := 0; i < 3; i++ {
  31. name, err := GenerateRandomName(prefix, len)
  32. if err != nil {
  33. continue
  34. }
  35. if _, err := netlink.LinkByName(name); err != nil {
  36. if strings.Contains(err.Error(), "not found") {
  37. return name, nil
  38. }
  39. return "", err
  40. }
  41. }
  42. return "", types.InternalErrorf("could not generate interface name")
  43. }