diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-12-17 23:38:26 -0800 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-12-17 23:38:26 -0800 |
commit | ac5b50529000cf5cccec024707c3b052f461b06b (patch) | |
tree | 3b8101983ee8b4602401f566d07c785a910eeadc /server/rpki.go | |
parent | f8929462982c77f6be2022c9cf42fe05bf3a4b92 (diff) |
rpki: sort ROA results
Sort the results in order of prefix, maxlen, as, and roa server.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'server/rpki.go')
-rw-r--r-- | server/rpki.go | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/server/rpki.go b/server/rpki.go index 1864fa25..7fa9aea4 100644 --- a/server/rpki.go +++ b/server/rpki.go @@ -26,6 +26,7 @@ import ( "github.com/osrg/gobgp/table" "gopkg.in/tomb.v2" "net" + "sort" "strconv" "strings" "time" @@ -43,6 +44,32 @@ type roa struct { AS []uint32 } +type roas []*api.ROA + +func (r roas) Len() int { + return len(r) +} + +func (r roas) Swap(i, j int) { + r[i], r[j] = r[j], r[i] +} + +func (r roas) Less(i, j int) bool { + r1 := r[i] + r2 := r[j] + + if r1.Maxlen < r1.Maxlen { + return true + } else if r1.Maxlen > r1.Maxlen { + return false + } + + if r1.As < r2.As { + return true + } + return false +} + const ( CONNECTED uint8 = iota DISCONNECTED @@ -278,11 +305,11 @@ func (c *roaManager) handleGRPC(grpcReq *GrpcRequest) { if tree, ok := c.roas[rf]; ok { tree.Walk(func(s string, v interface{}) bool { b, _ := v.(*roaBucket) + var roaList roas for _, r := range b.entries { for _, as := range r.AS { - result := &GrpcResponse{} host, port := splitHostPort(r.Src) - result.Data = &api.ROA{ + roa := &api.ROA{ As: as, Maxlen: uint32(r.MaxLen), Prefixlen: uint32(b.PrefixLen), @@ -292,8 +319,15 @@ func (c *roaManager) handleGRPC(grpcReq *GrpcRequest) { RemotePort: uint32(port), }, } - results = append(results, result) + roaList = append(roaList, roa) + } + } + sort.Sort(roaList) + for _, roa := range roaList { + result := &GrpcResponse{ + Data: roa, } + results = append(results, result) } return false }) |