func (t *Tunnel) gateways() (map[string]string, error) { routes, err := util.ParseRoutes() if err != nil { return nil, err } gateways := make(map[string]string) for _, r := range routes { if !begins(r.Iface, "eth") { continue } if r.Gateway == nil { continue } gateways[r.Iface] = r.Gateway.String() } if len(gateways) == 0 { return nil, errors.New("no interfaces found") } return gateways, nil }
func TestRoutesUnique(t *testing.T) { r, err := util.ParseRoutes() if err == util.ErrNotImplemented { t.Skipf("util.ParseRoutes is not implemented for %s", runtime.GOOS) } if err != nil { t.Fatal(err) } if len(r) == 0 { t.Fatal("no routes parsed") } gateways := make(map[string]struct{}) for _, r := range r { if r.Gateway != nil { gateways[r.Gateway.String()] = struct{}{} } } addrs, err := net.InterfaceAddrs() if err != nil { t.Fatal(err) } for i, a := range addrs { ipnet, ok := a.(*net.IPNet) if !ok { t.Errorf("%d: want %v to be %T, was %T", i, a, (*net.IPNet)(nil), a) continue } if _, ok = gateways[ipnet.IP.String()]; ok { t.Errorf("%d: %s is not a gateway, but was parsed by routes", i, ipnet.IP) } } }