func GetDefaultRouteIface() (int, error) { defaultRt := net.ParseIP("0.0.0.0") rs, err := netlink.RouteGet(defaultRt) if err != nil { return -1, fmt.Errorf("unable to get default route: %v", err) } if len(rs) > 0 { return rs[0].LinkIndex, nil } return -1, ErrNoDefaultRoute }
func GetIfaceForRoute(address string) (string, error) { addr := net.ParseIP(address) if addr == nil { return "", errors.New("invalid address") } routes, err := netlink.RouteGet(addr) if err != nil { return "", err } if len(routes) <= 0 { return "", errors.New("no route to destination") } link, err := netlink.LinkByIndex(routes[0].LinkIndex) if err != nil { return "", err } return link.Attrs().Name, nil }