diff --git a/common.go b/common.go index 5007397..d11c004 100644 --- a/common.go +++ b/common.go @@ -46,6 +46,11 @@ func (rt rtInfo) IsMoreSpecThan(mostSpecificRt *rtInfo) bool { return false } + // if all else is equal, prefer a route with a gateway. + if mostSpecificRt.Priority == rt.Priority && rt.Gateway == nil && mostSpecificRt.Gateway != nil { + return false + } + // Windows and MacOS hasn't metric/priority on rule entry, // But the interface device has the priority property. // diff --git a/netroute_linux.go b/netroute_linux.go index 1f039db..d94b105 100644 --- a/netroute_linux.go +++ b/netroute_linux.go @@ -60,12 +60,7 @@ loop: if err != nil { return nil, err } - switch rt.Family { - case syscall.AF_INET: - rtr.v4 = append(rtr.v4, &routeInfo) - case syscall.AF_INET6: - rtr.v6 = append(rtr.v6, &routeInfo) - default: + if rt.Family != syscall.AF_INET && rt.Family != syscall.AF_INET6 { continue loop } for _, attr := range attrs { @@ -92,6 +87,18 @@ loop: routeInfo.Priority = *(*uint32)(unsafe.Pointer(&attr.Value[0])) } } + if routeInfo.Dst == nil && routeInfo.Src == nil && routeInfo.Gateway == nil { + continue loop + } + switch rt.Family { + case syscall.AF_INET: + rtr.v4 = append(rtr.v4, &routeInfo) + case syscall.AF_INET6: + rtr.v6 = append(rtr.v6, &routeInfo) + default: + // should not happen. + continue loop + } } } sort.Sort(rtr.v4)