Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove full iface access assumption [linux/bsd] #9

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2.1
orbs:
ci-go: ipfs/ci-go@0.2
ci-go: ipfs/ci-go@0.2.8

workflows:
version: 2
Expand Down
11 changes: 7 additions & 4 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions netroute_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,11 +126,8 @@ func New() (routing.Router, error) {
if err != nil {
return nil, err
}
for i, iface := range ifaces {
if i != iface.Index-1 {
return nil, fmt.Errorf("out of order iface %d = %v", i, iface)
}
rtr.ifaces = append(rtr.ifaces, iface)
for _, iface := range ifaces {
rtr.ifaces[iface.Index] = iface
var addrs ipAddrs
ifaceAddrs, err := iface.Addrs()
if err != nil {
Expand All @@ -148,7 +147,7 @@ func New() (routing.Router, error) {
}
}
}
rtr.addrs = append(rtr.addrs, addrs)
rtr.addrs[iface.Index] = addrs
}
return rtr, nil
}
12 changes: 5 additions & 7 deletions netroute_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package netroute

import (
"fmt"
"net"
"sort"
"syscall"
Expand All @@ -23,6 +22,8 @@ import (

func New() (routing.Router, error) {
rtr := &router{}
rtr.ifaces = make(map[int]net.Interface)
rtr.addrs = make(map[int]ipAddrs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should probably pre-allocate these.

tab, err := syscall.NetlinkRIB(syscall.RTM_GETROUTE, syscall.AF_UNSPEC)
if err != nil {
return nil, err
Expand Down Expand Up @@ -83,11 +84,8 @@ loop:
if err != nil {
return nil, err
}
for i, iface := range ifaces {
if i != iface.Index-1 {
return nil, fmt.Errorf("out of order iface %d = %v", i, iface)
}
rtr.ifaces = append(rtr.ifaces, iface)
for _, iface := range ifaces {
rtr.ifaces[iface.Index] = iface
var addrs ipAddrs
ifaceAddrs, err := iface.Addrs()
if err != nil {
Expand All @@ -107,7 +105,7 @@ loop:
}
}
}
rtr.addrs = append(rtr.addrs, addrs)
rtr.addrs[iface.Index] = addrs
}
return rtr, nil
}