map string key
This commit is contained in:
parent
83bbd52cee
commit
47006bd973
5
main.go
5
main.go
@ -39,6 +39,9 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
routes.ParseRoutes(*UDPAddr, &rip.RIP)
|
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("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crip/ripv2"
|
"github.com/crip/ripv2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Routes []Route
|
type Routes map[string]Route
|
||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Addr net.IP
|
Addr net.IP
|
||||||
@ -18,12 +20,22 @@ type Route struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New() Routes {
|
func New() Routes {
|
||||||
var routes Routes
|
routes := make(map[string]Route, 25)
|
||||||
return routes
|
return routes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Routes) append(route Route) {
|
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) {
|
func (r *Routes) ParseRoutes(addr net.UDPAddr, rip *[25]ripv2.RIP) {
|
||||||
|
Loading…
Reference in New Issue
Block a user