Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 1 | package internal |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 5 | |
| 6 | "veyron.io/veyron/veyron2/ipc" |
Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame] | 7 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 8 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 9 | "veyron.io/veyron/veyron/lib/netstate" |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | // IPAddressChooser returns the preferred IP address, which is, |
| 13 | // a public IPv4 address, then any non-loopback IPv4, then a public |
| 14 | // IPv6 address and finally any non-loopback/link-local IPv6 |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 15 | func IPAddressChooser(network string, addrs []ipc.Address) ([]ipc.Address, error) { |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 16 | if !netstate.IsIPProtocol(network) { |
| 17 | return nil, fmt.Errorf("can't support network protocol %q", network) |
| 18 | } |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 19 | accessible := netstate.AddrList(addrs) |
| 20 | |
| 21 | // Try and find an address on a interface with a default route. |
| 22 | predicates := []netstate.AddressPredicate{netstate.IsPublicUnicastIPv4, |
| 23 | netstate.IsUnicastIPv4, netstate.IsPublicUnicastIPv6} |
| 24 | for _, predicate := range predicates { |
| 25 | if addrs := accessible.Filter(predicate); len(addrs) > 0 { |
| 26 | onDefaultRoutes := addrs.Filter(netstate.IsOnDefaultRoute) |
| 27 | if len(onDefaultRoutes) > 0 { |
| 28 | return onDefaultRoutes, nil |
| 29 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 32 | |
| 33 | // We failed to find any addresses with default routes, try again |
| 34 | // but without the default route requirement. |
| 35 | for _, predicate := range predicates { |
| 36 | if addrs := accessible.Filter(predicate); len(addrs) > 0 { |
| 37 | return addrs, nil |
| 38 | } |
| 39 | } |
| 40 | |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 41 | return nil, fmt.Errorf("failed to find any usable address for %q", network) |
| 42 | } |
Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame] | 43 | |
| 44 | // HasPublicIP returns true if the host has at least one public IP address. |
| 45 | func HasPublicIP(log vlog.Logger) bool { |
| 46 | state, err := netstate.GetAccessibleIPs() |
| 47 | if err != nil { |
| 48 | log.Infof("failed to determine network state: %s", err) |
| 49 | return false |
| 50 | } |
| 51 | any := state.Filter(netstate.IsUnicastIP) |
| 52 | if len(any) == 0 { |
| 53 | log.Infof("failed to find any usable IP addresses at startup") |
| 54 | return false |
| 55 | } |
| 56 | for _, a := range any { |
| 57 | if netstate.IsPublicUnicastIPv4(a) { |
| 58 | return true |
| 59 | } |
| 60 | } |
| 61 | return false |
| 62 | } |