2015-02-22 20:24:22 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import "github.com/vishvananda/netlink"
|
|
|
|
|
2015-02-22 20:58:52 -05:00
|
|
|
const (
|
|
|
|
DefaultBridgeName = "docker0"
|
|
|
|
)
|
|
|
|
|
2015-02-22 20:24:22 -05:00
|
|
|
type Interface struct {
|
|
|
|
Config *Configuration
|
|
|
|
Link netlink.Link
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInterface(config *Configuration) *Interface {
|
|
|
|
i := &Interface{
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
2015-02-22 20:58:52 -05:00
|
|
|
// Initialize the bridge name to the default if unspecified.
|
|
|
|
if i.Config.BridgeName == "" {
|
|
|
|
i.Config.BridgeName = DefaultBridgeName
|
|
|
|
}
|
|
|
|
|
2015-02-22 20:24:22 -05:00
|
|
|
// Attempt to find an existing bridge named with the specified name.
|
|
|
|
i.Link, _ = netlink.LinkByName(i.Config.BridgeName)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists indicates if the existing bridge interface exists on the system.
|
|
|
|
func (i *Interface) Exists() bool {
|
|
|
|
return i.Link != nil
|
|
|
|
}
|
2015-02-23 00:32:48 -05:00
|
|
|
|
|
|
|
// Addresses returns a single IPv4 address and all IPv6 addresses for the
|
|
|
|
// bridge interface.
|
|
|
|
func (i *Interface) Addresses() (netlink.Addr, []netlink.Addr, error) {
|
|
|
|
v4addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
return netlink.Addr{}, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v6addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V6)
|
|
|
|
if err != nil {
|
|
|
|
return netlink.Addr{}, nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:41:17 -05:00
|
|
|
if len(v4addr) == 0 {
|
|
|
|
return netlink.Addr{}, v6addr, nil
|
|
|
|
}
|
2015-02-23 00:32:48 -05:00
|
|
|
return v4addr[0], v6addr, nil
|
|
|
|
}
|