directly store interfaces rather than pushing to an array

parent 1e19d3de
......@@ -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:
......
......@@ -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
}
......@@ -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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment