diff --git a/main.go b/main.go index fd5bd43..e6e595a 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,9 @@ func main() { log.Fatalln(err) } routes.ParseRoutes(*UDPAddr, &rip.RIP) - fmt.Println(routes) + for _, v := range 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()) + } + fmt.Println("") } } diff --git a/ripv2/routes/routes.go b/ripv2/routes/routes.go index 35894be..0692c01 100644 --- a/ripv2/routes/routes.go +++ b/ripv2/routes/routes.go @@ -1,13 +1,15 @@ package routes import ( + "crypto/sha256" + "fmt" "net" "time" "github.com/crip/ripv2" ) -type Routes []Route +type Routes map[string]Route type Route struct { Addr net.IP @@ -18,12 +20,22 @@ type Route struct { } func New() Routes { - var routes Routes + routes := make(map[string]Route, 25) return routes } func (r *Routes) append(route Route) { - *r = append(*r, route) + key := route.Addr.String() + route.Subnet.String() + route.Nexthop.String() + fmt.Sprintf("%d", route.Metric) + h := sha256.New() + h.Write([]byte(key)) + bs := h.Sum(nil) + k, ok := (*r)[string(bs)] + if !ok { + (*r)[string(bs)] = route + return + } + k.TTL = time.Now() + (*r)[string(bs)] = k } func (r *Routes) ParseRoutes(addr net.UDPAddr, rip *[25]ripv2.RIP) {