44 lines
937 B
Go
44 lines
937 B
Go
package rtable
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
|
|
"github.com/crip/ripv2"
|
|
)
|
|
|
|
func Pass(r *ripv2.Routes) {
|
|
var err error
|
|
r.Lock()
|
|
for k, route := range r.Routes {
|
|
switch route.FIB {
|
|
case ripv2.NEW:
|
|
if err = command(ADD, &route); err != nil {
|
|
log.Printf("rtable: failed to add route %d: %s", route.FIB, err)
|
|
}
|
|
route.FIB = ripv2.TABLE
|
|
case ripv2.DELETE:
|
|
if err = command(REMOVE, &route); err != nil {
|
|
log.Printf("rtable: failed to remove route %d: %s", route.FIB, err)
|
|
}
|
|
delete(r.Routes, k)
|
|
default:
|
|
log.Println("unknown FIB type")
|
|
}
|
|
}
|
|
r.Unlock()
|
|
}
|
|
|
|
func command(command COMMAND, r *ripv2.Route) error {
|
|
var cmd *exec.Cmd
|
|
switch command {
|
|
case ADD, REMOVE:
|
|
cmd = exec.Command("route", string(command), "-net", r.Addr.String(), r.Subnet.String(), r.NextHop.String(), "-hopcount", fmt.Sprint(r.Metric))
|
|
default:
|
|
return errors.New("command not supported")
|
|
}
|
|
return cmd.Run()
|
|
}
|