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 | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 7 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 8 | "veyron.io/veyron/veyron/lib/netstate" |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // IPAddressChooser returns the preferred IP address, which is, |
| 12 | // a public IPv4 address, then any non-loopback IPv4, then a public |
| 13 | // IPv6 address and finally any non-loopback/link-local IPv6 |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 14 | func IPAddressChooser(network string, addrs []ipc.Address) ([]ipc.Address, error) { |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 15 | if !netstate.IsIPProtocol(network) { |
| 16 | return nil, fmt.Errorf("can't support network protocol %q", network) |
| 17 | } |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 18 | accessible := netstate.AddrList(addrs) |
| 19 | |
| 20 | // Try and find an address on a interface with a default route. |
| 21 | predicates := []netstate.AddressPredicate{netstate.IsPublicUnicastIPv4, |
| 22 | netstate.IsUnicastIPv4, netstate.IsPublicUnicastIPv6} |
| 23 | for _, predicate := range predicates { |
| 24 | if addrs := accessible.Filter(predicate); len(addrs) > 0 { |
| 25 | onDefaultRoutes := addrs.Filter(netstate.IsOnDefaultRoute) |
| 26 | if len(onDefaultRoutes) > 0 { |
| 27 | return onDefaultRoutes, nil |
| 28 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 31 | |
| 32 | // We failed to find any addresses with default routes, try again |
| 33 | // but without the default route requirement. |
| 34 | for _, predicate := range predicates { |
| 35 | if addrs := accessible.Filter(predicate); len(addrs) > 0 { |
| 36 | return addrs, nil |
| 37 | } |
| 38 | } |
| 39 | |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 40 | return nil, fmt.Errorf("failed to find any usable address for %q", network) |
| 41 | } |