type AddressFamily … const familyIPv4 … const familyIPv6 … type AddressFamilyPreference … var preferIPv4 … var preferIPv6 … const LoopbackInterfaceName … const ipv4RouteFile … const ipv6RouteFile … type Route … type RouteFile … type noRoutesError … func (e noRoutesError) Error() string { … } // IsNoRoutesError checks if an error is of type noRoutesError func IsNoRoutesError(err error) bool { … } var v4File … var v6File … func (rf RouteFile) extract() ([]Route, error) { … } // getIPv4DefaultRoutes obtains the IPv4 routes, and filters out non-default routes. func getIPv4DefaultRoutes(input io.Reader) ([]Route, error) { … } func getIPv6DefaultRoutes(input io.Reader) ([]Route, error) { … } // parseIP takes the hex IP address string from route file and converts it // to a net.IP address. For IPv4, the value must be converted to big endian. func parseIP(str string, family AddressFamily) (net.IP, error) { … } func isInterfaceUp(intf *net.Interface) bool { … } func isLoopbackOrPointToPoint(intf *net.Interface) bool { … } // getMatchingGlobalIP returns the first valid global unicast address of the given // 'family' from the list of 'addrs'. func getMatchingGlobalIP(addrs []net.Addr, family AddressFamily) (net.IP, error) { … } // getIPFromInterface gets the IPs on an interface and returns a global unicast address, if any. The // interface must be up, the IP must in the family requested, and the IP must be a global unicast address. func getIPFromInterface(intfName string, forFamily AddressFamily, nw networkInterfacer) (net.IP, error) { … } // getIPFromLoopbackInterface gets the IPs on a loopback interface and returns a global unicast address, if any. // The loopback interface must be up, the IP must in the family requested, and the IP must be a global unicast address. func getIPFromLoopbackInterface(forFamily AddressFamily, nw networkInterfacer) (net.IP, error) { … } // memberOf tells if the IP is of the desired family. Used for checking interface addresses. func memberOf(ip net.IP, family AddressFamily) bool { … } // chooseIPFromHostInterfaces looks at all system interfaces, trying to find one that is up that // has a global unicast address (non-loopback, non-link local, non-point2point), and returns the IP. // addressFamilies determines whether it prefers IPv4 or IPv6 func chooseIPFromHostInterfaces(nw networkInterfacer, addressFamilies AddressFamilyPreference) (net.IP, error) { … } // ChooseHostInterface is a method used fetch an IP for a daemon. // If there is no routing info file, it will choose a global IP from the system // interfaces. Otherwise, it will use IPv4 and IPv6 route information to return the // IP of the interface with a gateway on it (with priority given to IPv4). For a node // with no internet connection, it returns error. func ChooseHostInterface() (net.IP, error) { … } func chooseHostInterface(addressFamilies AddressFamilyPreference) (net.IP, error) { … } type networkInterfacer … type networkInterface … func (_ networkInterface) InterfaceByName(intfName string) (*net.Interface, error) { … } func (_ networkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) { … } func (_ networkInterface) Interfaces() ([]net.Interface, error) { … } // getAllDefaultRoutes obtains IPv4 and IPv6 default routes on the node. If unable // to read the IPv4 routing info file, we return an error. If unable to read the IPv6 // routing info file (which is optional), we'll just use the IPv4 route information. // Using all the routing info, if no default routes are found, an error is returned. func getAllDefaultRoutes() ([]Route, error) { … } // chooseHostInterfaceFromRoute cycles through each default route provided, looking for a // global IP address from the interface for the route. If there are routes but no global // address is obtained from the interfaces, it checks if the loopback interface has a global address. // addressFamilies determines whether it prefers IPv4 or IPv6 func chooseHostInterfaceFromRoute(routes []Route, nw networkInterfacer, addressFamilies AddressFamilyPreference) (net.IP, error) { … } // ResolveBindAddress returns the IP address of a daemon, based on the given bindAddress: // If bindAddress is unset, it returns the host's default IP, as with ChooseHostInterface(). // If bindAddress is unspecified or loopback, it returns the default IP of the same // address family as bindAddress. // Otherwise, it just returns bindAddress. func ResolveBindAddress(bindAddress net.IP) (net.IP, error) { … } // ChooseBindAddressForInterface choose a global IP for a specific interface, with priority given to IPv4. // This is required in case of network setups where default routes are present, but network // interfaces use only link-local addresses (e.g. as described in RFC5549). // e.g when using BGP to announce a host IP over link-local ip addresses and this ip address is attached to the lo interface. func ChooseBindAddressForInterface(intfName string) (net.IP, error) { … }