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
d8d75cf2
Unverified
Commit
d8d75cf2
authored
Mar 30, 2020
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
directly store interfaces rather than pushing to an array
parent
1e19d3de
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
8 deletions
+15
-8
common.go
common.go
+7
-4
netroute_bsd.go
netroute_bsd.go
+4
-2
netroute_linux.go
netroute_linux.go
+4
-2
No files found.
common.go
View file @
d8d75cf2
...
...
@@ -57,8 +57,8 @@ func (r routeSlice) Swap(i, j int) {
}
type
router
struct
{
ifaces
[
]
net
.
Interface
addrs
[
]
ipAddrs
ifaces
map
[
int
]
net
.
Interface
addrs
map
[
int
]
ipAddrs
v4
,
v6
routeSlice
}
...
...
@@ -98,9 +98,12 @@ func (r *router) RouteWithSrc(input net.HardwareAddr, src, dst net.IP) (iface *n
}
// Interfaces are 1-indexed, but we store them in a 0-indexed array.
ifaceIndex
--
correspondingIface
,
ok
:=
r
.
ifaces
[
ifaceIndex
]
if
!
ok
{
err
=
errors
.
New
(
"Route refereced unknown interface"
)
}
iface
=
&
correspondingIface
iface
=
&
r
.
ifaces
[
ifaceIndex
]
if
preferredSrc
==
nil
{
switch
{
case
dst
.
To4
()
!=
nil
:
...
...
netroute_bsd.go
View file @
d8d75cf2
...
...
@@ -53,6 +53,8 @@ const (
func
New
()
(
routing
.
Router
,
error
)
{
rtr
:=
&
router
{}
rtr
.
ifaces
=
make
(
map
[
int
]
net
.
Interface
)
rtr
.
addrs
=
make
(
map
[
int
]
ipAddrs
)
tab
,
err
:=
route
.
FetchRIB
(
syscall
.
AF_UNSPEC
,
route
.
RIBTypeRoute
,
0
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -125,7 +127,7 @@ func New() (routing.Router, error) {
return
nil
,
err
}
for
_
,
iface
:=
range
ifaces
{
rtr
.
ifaces
=
append
(
rtr
.
ifaces
,
iface
)
rtr
.
ifaces
[
iface
.
Index
]
=
iface
var
addrs
ipAddrs
ifaceAddrs
,
err
:=
iface
.
Addrs
()
if
err
!=
nil
{
...
...
@@ -145,7 +147,7 @@ func New() (routing.Router, error) {
}
}
}
rtr
.
addrs
=
append
(
rtr
.
addrs
,
addrs
)
rtr
.
addrs
[
iface
.
Index
]
=
addrs
}
return
rtr
,
nil
}
netroute_linux.go
View file @
d8d75cf2
...
...
@@ -22,6 +22,8 @@ import (
func
New
()
(
routing
.
Router
,
error
)
{
rtr
:=
&
router
{}
rtr
.
ifaces
=
make
(
map
[
int
]
net
.
Interface
)
rtr
.
addrs
=
make
(
map
[
int
]
ipAddrs
)
tab
,
err
:=
syscall
.
NetlinkRIB
(
syscall
.
RTM_GETROUTE
,
syscall
.
AF_UNSPEC
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -83,7 +85,7 @@ loop:
return
nil
,
err
}
for
_
,
iface
:=
range
ifaces
{
rtr
.
ifaces
=
append
(
rtr
.
ifaces
,
iface
)
rtr
.
ifaces
[
iface
.
Index
]
=
iface
var
addrs
ipAddrs
ifaceAddrs
,
err
:=
iface
.
Addrs
()
if
err
!=
nil
{
...
...
@@ -103,7 +105,7 @@ loop:
}
}
}
rtr
.
addrs
=
append
(
rtr
.
addrs
,
addrs
)
rtr
.
addrs
[
iface
.
Index
]
=
addrs
}
return
rtr
,
nil
}
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