interface.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package bridge
  2. import (
  3. "net"
  4. "github.com/vishvananda/netlink"
  5. )
  6. const (
  7. // DefaultBridgeName is the default name for the bridge interface managed
  8. // by the driver when unspecified by the caller.
  9. DefaultBridgeName = "docker0"
  10. )
  11. // Interface models the bridge network device.
  12. type bridgeInterface struct {
  13. Link netlink.Link
  14. bridgeIPv4 *net.IPNet
  15. bridgeIPv6 *net.IPNet
  16. gatewayIPv4 net.IP
  17. gatewayIPv6 net.IP
  18. }
  19. // newInterface creates a new bridge interface structure. It attempts to find
  20. // an already existing device identified by the configuration BridgeName field,
  21. // or the default bridge name when unspecified), but doesn't attempt to create
  22. // one when missing
  23. func newInterface(config *networkConfiguration) *bridgeInterface {
  24. i := &bridgeInterface{}
  25. // Initialize the bridge name to the default if unspecified.
  26. if config.BridgeName == "" {
  27. config.BridgeName = DefaultBridgeName
  28. }
  29. // Attempt to find an existing bridge named with the specified name.
  30. i.Link, _ = netlink.LinkByName(config.BridgeName)
  31. return i
  32. }
  33. // exists indicates if the existing bridge interface exists on the system.
  34. func (i *bridgeInterface) exists() bool {
  35. return i.Link != nil
  36. }
  37. // addresses returns a single IPv4 address and all IPv6 addresses for the
  38. // bridge interface.
  39. func (i *bridgeInterface) addresses() (netlink.Addr, []netlink.Addr, error) {
  40. v4addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V4)
  41. if err != nil {
  42. return netlink.Addr{}, nil, err
  43. }
  44. v6addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V6)
  45. if err != nil {
  46. return netlink.Addr{}, nil, err
  47. }
  48. if len(v4addr) == 0 {
  49. return netlink.Addr{}, v6addr, nil
  50. }
  51. return v4addr[0], v6addr, nil
  52. }