crip/rtable/table.go

38 lines
645 B
Go
Raw Normal View History

2024-04-14 18:14:20 +01:00
package rtable
2024-04-15 11:26:37 +01:00
import (
"errors"
"fmt"
"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-16 12:55:08 +01:00
func Add(r *ripv2.Routes) {
2024-04-15 11:26:37 +01:00
r.Lock()
for _, r := range r.Routes {
if !r.FIB {
command(ADD, &r)
r.FIB = true
}
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:
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()
}
2024-04-14 18:14:20 +01:00
2024-04-16 12:55:08 +01:00
func Remove(r *ripv2.Routes) {
2024-04-15 11:26:37 +01:00
for _, r := range r.Routes {
command(REMOVE, &r)
2024-04-14 18:14:20 +01:00
}
}