Test adding routes

This commit is contained in:
Jim Colderwood 2024-04-15 11:26:37 +01:00
parent 497269f63c
commit 6fe049546b
4 changed files with 44 additions and 14 deletions

View File

@ -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("")
}
}

View File

@ -17,6 +17,7 @@ type Routes struct {
type Route struct {
Addr net.IP
FIB bool
Subnet net.IPMask
Nexthop net.IP
Metric uint32

View File

@ -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)
}
}

8
rtable/types.go Normal file
View File

@ -0,0 +1,8 @@
package rtable
type COMMAND string
const (
ADD COMMAND = "add"
REMOVE COMMAND = "delete"
)