diff --git a/main.go b/main.go index 6a4c932..8352b66 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "github.com/crip/ripv2" "github.com/crip/ripv2/routes" + "github.com/crip/rtable" ) func main() { @@ -43,6 +44,7 @@ func main() { for _, v := range routes.Routes { fmt.Printf("Route:%s\tSubnet:%s\tNexthop:%s\tMetric:%d\tTime: %v\n", v.Addr.String(), v.Subnet.String(), v.Nexthop.String(), v.Metric, v.TTL.Unix()) } + rtable.Add(routes) fmt.Println("") } } diff --git a/ripv2/routes/routes.go b/ripv2/routes/routes.go index 9ade96b..59bbf6a 100644 --- a/ripv2/routes/routes.go +++ b/ripv2/routes/routes.go @@ -17,6 +17,7 @@ type Routes struct { type Route struct { Addr net.IP + FIB bool Subnet net.IPMask Nexthop net.IP Metric uint32 diff --git a/rtable/table.go b/rtable/table.go index 11e5a55..cabc80b 100644 --- a/rtable/table.go +++ b/rtable/table.go @@ -1,19 +1,38 @@ package rtable -import "github.com/crip/ripv2" - -func Add(r *ripv2.RIP) { - if check(r) { - return - } -} - -func check(r *ripv2.RIP) bool { - return false -} - -func Remove(r *ripv2.RIP) { - if check(r) { +import ( + "errors" + "fmt" + "os/exec" + "github.com/crip/ripv2/routes" +) + +func Add(r *routes.Routes) { + r.Lock() + for _, r := range r.Routes { + if !r.FIB { + command(ADD, &r) + r.FIB = true + } + } + r.Unlock() +} + +func command(command COMMAND, r *routes.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() +} + +func Remove(r *routes.Routes) { + for _, r := range r.Routes { + command(REMOVE, &r) } } diff --git a/rtable/types.go b/rtable/types.go new file mode 100644 index 0000000..f1c47b1 --- /dev/null +++ b/rtable/types.go @@ -0,0 +1,8 @@ +package rtable + +type COMMAND string + +const ( + ADD COMMAND = "add" + REMOVE COMMAND = "delete" +)