This commit is contained in:
Will Scott
2020-03-07 18:38:47 -08:00
parent 81127d7183
commit a7cc795bf9
3 changed files with 27 additions and 3 deletions
-2
View File
@@ -3,8 +3,6 @@
package netroute
import (
"net"
"github.com/google/gopacket/routing"
)
+4 -1
View File
@@ -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))
+23
View File
@@ -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
}