Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-netroute
Commits
f5febac9
Unverified
Commit
f5febac9
authored
Mar 07, 2020
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test nits
parent
a7cc795b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
9 deletions
+25
-9
README.md
README.md
+2
-0
netroute_darwin.go
netroute_darwin.go
+1
-1
netroute_test.go
netroute_test.go
+17
-3
netroute_windows.go
netroute_windows.go
+5
-5
No files found.
README.md
View file @
f5febac9
Go Netroute
===
[
![Build Status
](
https://travis-ci.com/willscott/go-netroute.svg?branch=master
)
](https://travis-ci.com/willscott/go-netroute)
A cross-platform implementation of the
[
`gopacket/routing.Router`
](
https://godoc.org/github.com/google/gopacket/routing#Router
)
interface.
This library uses
`gopacket`
for linux,
`x/net/route`
...
...
netroute_darwin.go
View file @
f5febac9
...
...
@@ -168,7 +168,7 @@ func toIPAddr(a route.Addr) (net.IP, error) {
copy
(
ip
,
t
.
IP
[
:
])
return
ip
,
nil
default
:
return
net
.
IP
{},
fmt
.
Errorf
(
"unknown family: %
T
"
,
t
)
return
net
.
IP
{},
fmt
.
Errorf
(
"unknown family: %
v
"
,
t
)
}
}
...
...
netroute_test.go
View file @
f5febac9
...
...
@@ -2,6 +2,7 @@ package netroute
import
(
"net"
"strings"
"testing"
)
...
...
@@ -11,12 +12,25 @@ func TestRoute(t *testing.T) {
t
.
Fatal
(
err
)
}
// Route to 127.0.0.1 shouldn't have a gateway
_
,
gw
,
_
,
err
:=
r
.
Route
(
net
.
IPv4
(
127
,
0
,
0
,
1
))
ifs
,
err
:=
net
.
Interfaces
()
if
err
!=
nil
||
len
(
ifs
)
==
0
{
t
.
Skip
(
"Can't test routing without access to system interfaces"
)
}
var
localAddr
net
.
IP
addrs
,
err
:=
ifs
[
0
]
.
Addrs
()
for
_
,
addr
:=
range
addrs
{
if
strings
.
HasPrefix
(
addr
.
Network
(),
"ip"
)
{
localAddr
,
_
,
_
=
net
.
ParseCIDR
(
addr
.
String
())
break
}
}
_
,
gw
,
src
,
err
:=
r
.
Route
(
localAddr
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
gw
!=
nil
{
if
gw
!=
nil
||
!
src
.
Equal
(
localAddr
)
{
t
.
Fatalf
(
"Did not expect gateway to localhost: %v"
,
gw
)
}
...
...
netroute_windows.go
View file @
f5febac9
...
...
@@ -174,7 +174,7 @@ func getIface(index uint32) *net.Interface {
ifRow
.
Index
=
index
err
:=
windows
.
GetIfEntry
(
&
ifRow
)
if
err
!=
nil
{
return
il
return
n
il
}
ifaces
,
err
:=
net
.
Interfaces
()
...
...
@@ -182,7 +182,7 @@ func getIface(index uint32) *net.Interface {
return
nil
}
for
_
,
iface
:=
range
ifaces
{
if
bytes
.
Equal
(
iface
.
HardwareAddr
,
ifRow
.
PhysAddr
)
{
if
bytes
.
Equal
(
iface
.
HardwareAddr
,
ifRow
.
PhysAddr
[
:
]
)
{
return
&
iface
}
}
...
...
@@ -200,10 +200,10 @@ func (r *winRouter) RouteWithSrc(input net.HardwareAddr, src, dst net.IP) (iface
if
err
!=
nil
{
return
nil
,
nil
,
nil
,
err
}
iface
:
=
getIface
(
route
.
index
)
iface
=
getIface
(
route
.
index
)
if
route
.
nextHop
.
Addr
.
Family
==
0
/* AF_UNDEF */
{
return
nil
,
nil
,
pref
,
nil
return
iface
,
nil
,
pref
,
nil
}
addr
,
err
:=
route
.
nextHop
.
Sockaddr
()
if
err
!=
nil
{
...
...
@@ -211,7 +211,7 @@ func (r *winRouter) RouteWithSrc(input net.HardwareAddr, src, dst net.IP) (iface
}
nextHop
,
_
:=
sockaddrnet
.
SockaddrToIPAndZone
(
addr
)
return
nil
,
nextHop
,
pref
,
nil
return
iface
,
nextHop
,
pref
,
nil
}
func
New
()
(
routing
.
Router
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment