From b5256f7b71b50af9ab04aca253d71b39afcb578a Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 19 Jan 2016 12:26:51 +0000 Subject: [PATCH] refactor: inline This eliminates the unnecessary indirection via interface names, instead identifying the interface by its index. --- net/if.go | 22 ++++++++++++++++------ net/route.go | 11 ----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/net/if.go b/net/if.go index ece27bd4..dc391b83 100644 --- a/net/if.go +++ b/net/if.go @@ -52,15 +52,25 @@ func EnsureInterfaceAndMcastRoute(ifaceName string) (*net.Interface, error) { return nil, err } dest := net.IPv4(224, 0, 0, 0) - // check for currently-existing route after subscribing, to avoid race - if CheckRouteExists(ifaceName, dest) { - return iface, err + check := func(route netlink.Route) bool { + return route.LinkIndex == iface.Index && route.Dst.IP.Equal(dest) } - for update := range ch { - if update.Route.LinkIndex == iface.Index && update.Route.Dst.IP.Equal(dest) { - break + // check for currently-existing route after subscribing, to avoid race + routes, err := netlink.RouteList(nil, netlink.FAMILY_V4) + if err != nil { + return nil, err + } + for _, route := range routes { + if check(route) { + return iface, nil } } + for update := range ch { + if check(update.Route) { + return iface, nil + } + } + // should never get here return iface, nil } diff --git a/net/route.go b/net/route.go index f28b969a..a46000f0 100644 --- a/net/route.go +++ b/net/route.go @@ -54,14 +54,3 @@ func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(name string, } return nil } - -func CheckRouteExists(ifaceName string, dest net.IP) bool { - found := false - forEachRoute(map[string]struct{}{}, func(name string, route netlink.Route) error { - if name == ifaceName && route.Dst.IP.Equal(dest) { - found = true - } - return nil - }) - return found -}