interface.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  17. // newInterface creates a new bridge interface structure. It attempts to find
  18. // an already existing device identified by the Configuration BridgeName field,
  19. // or the default bridge name when unspecified), but doesn't attempt to create
  20. // one when missing
  21. func newInterface(config *Configuration) *bridgeInterface {
  22. i := &bridgeInterface{}
  23. // Initialize the bridge name to the default if unspecified.
  24. if config.BridgeName == "" {
  25. config.BridgeName = DefaultBridgeName
  26. }
  27. // Attempt to find an existing bridge named with the specified name.
  28. i.Link, _ = netlink.LinkByName(config.BridgeName)
  29. return i
  30. }
  31. // exists indicates if the existing bridge interface exists on the system.
  32. func (i *bridgeInterface) exists() bool {
  33. return i.Link != nil
  34. }
  35. // addresses returns a single IPv4 address and all IPv6 addresses for the
  36. // bridge interface.
  37. func (i *bridgeInterface) addresses() (netlink.Addr, []netlink.Addr, error) {
  38. v4addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V4)
  39. if err != nil {
  40. return netlink.Addr{}, nil, err
  41. }
  42. v6addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V6)
  43. if err != nil {
  44. return netlink.Addr{}, nil, err
  45. }
  46. if len(v4addr) == 0 {
  47. return netlink.Addr{}, v6addr, nil
  48. }
  49. return v4addr[0], v6addr, nil
  50. }