mirror of
https://github.com/netbirdio/go-netroute.git
synced 2026-05-22 17:11:07 -07:00
nits
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
package netroute
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/google/gopacket/routing"
|
||||
)
|
||||
|
||||
|
||||
+4
-1
@@ -6,7 +6,10 @@ import (
|
||||
)
|
||||
|
||||
func TestRoute(t *testing.T) {
|
||||
r, _ := New()
|
||||
r, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Route to 127.0.0.1 shouldn't have a gateway
|
||||
_, gw, _, err := r.Route(net.IPv4(127, 0, 0, 1))
|
||||
|
||||
@@ -7,6 +7,7 @@ package netroute
|
||||
// Reference:
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-getbestroute2
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -168,6 +169,26 @@ func getBestRoute2(interfaceLuid *NetLUID, interfaceIndex uint32, sourceAddress,
|
||||
return
|
||||
}
|
||||
|
||||
func getIface(index uint32) *net.Interface {
|
||||
var ifRow windows.MibIfRow
|
||||
ifRow.Index = index
|
||||
err := windows.GetIfEntry(&ifRow)
|
||||
if err != nil {
|
||||
return il
|
||||
}
|
||||
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, iface := range ifaces {
|
||||
if bytes.Equal(iface.HardwareAddr, ifRow.PhysAddr) {
|
||||
return &iface
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type winRouter struct{}
|
||||
|
||||
func (r *winRouter) Route(dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error) {
|
||||
@@ -179,6 +200,8 @@ func (r *winRouter) RouteWithSrc(input net.HardwareAddr, src, dst net.IP) (iface
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
iface := getIface(route.index)
|
||||
|
||||
if route.nextHop.Addr.Family == 0 /* AF_UNDEF */ {
|
||||
return nil, nil, pref, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user