Skip to content

Commit

Permalink
Merge pull request #9 from libp2p/bug/ifaceperm
Browse files Browse the repository at this point in the history
remove full iface access assumption [linux/bsd]
  • Loading branch information
willscott authored Mar 31, 2020
2 parents bca610e + d8d75cf commit 29d70d1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
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/[email protected]
ci-go: ipfs/[email protected].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)
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
}

0 comments on commit 29d70d1

Please sign in to comment.