2024-04-14 18:14:20 +01:00
|
|
|
package rtable
|
|
|
|
|
2024-04-15 11:26:37 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-04-18 14:32:35 +01:00
|
|
|
"log"
|
2024-04-15 11:26:37 +01:00
|
|
|
"os/exec"
|
2024-04-14 18:14:20 +01:00
|
|
|
|
2024-04-16 12:55:08 +01:00
|
|
|
"github.com/crip/ripv2"
|
2024-04-15 11:26:37 +01:00
|
|
|
)
|
|
|
|
|
2024-04-18 14:32:35 +01:00
|
|
|
func Pass(r *ripv2.Routes) {
|
2024-04-25 14:39:46 +01:00
|
|
|
var err error
|
2024-04-15 11:26:37 +01:00
|
|
|
r.Lock()
|
2024-04-18 14:32:35 +01:00
|
|
|
for k, route := range r.Routes {
|
|
|
|
switch route.FIB {
|
|
|
|
case ripv2.NEW:
|
2024-04-25 14:39:46 +01:00
|
|
|
if err = command(ADD, &route); err != nil {
|
|
|
|
log.Printf("rtable: failed to add route %d: %s", route.FIB, err)
|
|
|
|
}
|
2024-04-18 14:32:35 +01:00
|
|
|
route.FIB = ripv2.TABLE
|
|
|
|
case ripv2.DELETE:
|
2024-04-25 14:39:46 +01:00
|
|
|
if err = command(REMOVE, &route); err != nil {
|
|
|
|
log.Printf("rtable: failed to remove route %d: %s", route.FIB, err)
|
|
|
|
}
|
2024-04-18 14:32:35 +01:00
|
|
|
delete(r.Routes, k)
|
|
|
|
default:
|
|
|
|
log.Println("unknown FIB type")
|
2024-04-15 11:26:37 +01:00
|
|
|
}
|
2024-04-14 18:14:20 +01:00
|
|
|
}
|
2024-04-15 11:26:37 +01:00
|
|
|
r.Unlock()
|
2024-04-14 18:14:20 +01:00
|
|
|
}
|
|
|
|
|
2024-04-16 12:55:08 +01:00
|
|
|
func command(command COMMAND, r *ripv2.Route) error {
|
2024-04-15 11:26:37 +01:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
switch command {
|
|
|
|
case ADD, REMOVE:
|
2024-04-25 14:39:46 +01:00
|
|
|
cmd = exec.Command("route", string(command), "-net", r.Addr.String(), r.Subnet.String(), r.NextHop.String(), "-hopcount", fmt.Sprint(r.Metric))
|
2024-04-15 11:26:37 +01:00
|
|
|
default:
|
|
|
|
return errors.New("command not supported")
|
|
|
|
}
|
|
|
|
return cmd.Run()
|
|
|
|
}
|