Skip to content

Commit

Permalink
directly store interfaces rather than pushing to an array
Browse files Browse the repository at this point in the history
  • Loading branch information
willscott committed Mar 31, 2020
1 parent 1e19d3d commit d8d75cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
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
6 changes: 4 additions & 2 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 @@ -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 {
Expand All @@ -145,7 +147,7 @@ func New() (routing.Router, error) {
}
}
}
rtr.addrs = append(rtr.addrs, addrs)
rtr.addrs[iface.Index] = addrs
}
return rtr, nil
}
6 changes: 4 additions & 2 deletions netroute_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -103,7 +105,7 @@ loop:
}
}
}
rtr.addrs = append(rtr.addrs, addrs)
rtr.addrs[iface.Index] = addrs
}
return rtr, nil
}

0 comments on commit d8d75cf

Please sign in to comment.