From 2d381d2a8580e9a6cb0b5de7492f0dc485019fe2 Mon Sep 17 00:00:00 2001 From: Jim Colderwood Date: Thu, 18 Apr 2024 14:32:35 +0100 Subject: [PATCH] fib update --- main.go | 2 +- ripv2/routes.go | 2 +- ripv2/timers.go | 4 ++-- ripv2/types.go | 7 +++++++ rtable/table.go | 23 ++++++++++++----------- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 5f23dd7..7d3f1f3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( ) func callback(r *ripv2.Routes) { - rtable.Add(r) + rtable.Pass(r) } func main() { diff --git a/ripv2/routes.go b/ripv2/routes.go index 8807b37..de80428 100644 --- a/ripv2/routes.go +++ b/ripv2/routes.go @@ -15,7 +15,7 @@ type Routes struct { type Route struct { Addr net.IP - FIB bool + FIB FIB Subnet net.IPMask Nexthop net.IP Metric uint32 diff --git a/ripv2/timers.go b/ripv2/timers.go index 6e7c6ef..d1985e7 100644 --- a/ripv2/timers.go +++ b/ripv2/timers.go @@ -10,9 +10,9 @@ func (r *Routes) Timer() { for { <-t.C r.Lock() - for k, route := range r.Routes { + for _, route := range r.Routes { if time.Since(route.TTL) >= time.Minute*2 { - delete(r.Routes, k) + route.FIB = DELETE continue } } diff --git a/ripv2/types.go b/ripv2/types.go index f7075ae..a4205a2 100644 --- a/ripv2/types.go +++ b/ripv2/types.go @@ -1,6 +1,7 @@ package ripv2 type Command uint8 +type FIB uint8 type Version uint8 const ( @@ -9,6 +10,12 @@ const ( RESPONSE ) +const ( + NEW FIB = iota + TABLE + DELETE +) + const ( RIPv1 Version = iota + 1 RIPv2 diff --git a/rtable/table.go b/rtable/table.go index 6160d1c..6258513 100644 --- a/rtable/table.go +++ b/rtable/table.go @@ -3,17 +3,24 @@ package rtable import ( "errors" "fmt" + "log" "os/exec" "github.com/crip/ripv2" ) -func Add(r *ripv2.Routes) { +func Pass(r *ripv2.Routes) { r.Lock() - for _, r := range r.Routes { - if !r.FIB { - command(ADD, &r) - r.FIB = true + for k, route := range r.Routes { + switch route.FIB { + case ripv2.NEW: + command(ADD, &route) + route.FIB = ripv2.TABLE + case ripv2.DELETE: + command(REMOVE, &route) + delete(r.Routes, k) + default: + log.Println("unknown FIB type") } } r.Unlock() @@ -29,9 +36,3 @@ func command(command COMMAND, r *ripv2.Route) error { } return cmd.Run() } - -func Remove(r *ripv2.Routes) { - for _, r := range r.Routes { - command(REMOVE, &r) - } -}