diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-10-27 09:34:32 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-10-27 09:34:32 +0900 |
commit | c66b596122b6c27546ed21c7604bd0d6a042fd6c (patch) | |
tree | 9f8f4f34fb4bbb69d2cb6e9e213df3ea35afcf4f | |
parent | ed5026413cfb87527541a7cf9f83118df311ca73 (diff) |
server: fix radix key bug in rpki test
also clean up functions to create a radix key.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | server/rpki.go | 30 | ||||
-rw-r--r-- | server/rpki_test.go | 25 | ||||
-rw-r--r-- | table/destination.go | 22 | ||||
-rw-r--r-- | table/destination_test.go | 8 |
4 files changed, 41 insertions, 44 deletions
diff --git a/server/rpki.go b/server/rpki.go index 4f991169..1e743af9 100644 --- a/server/rpki.go +++ b/server/rpki.go @@ -17,7 +17,6 @@ package server import ( "bufio" - "bytes" "fmt" log "github.com/Sirupsen/logrus" "github.com/armon/go-radix" @@ -51,7 +50,8 @@ func (c *roaClient) recieveROA() chan []byte { return c.outgoing } -func handleIPPrefix(tree *radix.Tree, key string, as uint32, prefix []byte, prefixLen, maxLen uint8) { +func addROA(tree *radix.Tree, as uint32, prefix []byte, prefixLen, maxLen uint8) { + key := table.IpToRadixkey(prefix, prefixLen) b, _ := tree.Get(key) if b == nil { p := make([]byte, len(prefix)) @@ -88,14 +88,6 @@ func handleIPPrefix(tree *radix.Tree, key string, as uint32, prefix []byte, pref } } -func prefixToKey(prefix []byte, prefixLen uint8) string { - var buffer bytes.Buffer - for i := 0; i < len(prefix) && i < int(prefixLen); i++ { - buffer.WriteString(fmt.Sprintf("%08b", prefix[i])) - } - return buffer.String()[:prefixLen] -} - func (c *roaClient) handleRTRMsg(buf []byte) { received := &c.config.RpkiServerList[0].RpkiServerState.RpkiMessages.RpkiReceived @@ -109,7 +101,6 @@ func (c *roaClient) handleRTRMsg(buf []byte) { case *bgp.RTRCacheResponse: received.CacheResponse++ case *bgp.RTRIPPrefix: - key := prefixToKey(msg.Prefix, msg.PrefixLen) var tree *radix.Tree if net.IP(msg.Prefix).To4() != nil { received.Ipv4Prefix++ @@ -118,7 +109,7 @@ func (c *roaClient) handleRTRMsg(buf []byte) { received.Ipv6Prefix++ tree = c.roas[bgp.RF_IPv6_UC] } - handleIPPrefix(tree, key, msg.AS, msg.Prefix, msg.PrefixLen, msg.MaxLen) + addROA(tree, msg.AS, msg.Prefix, msg.PrefixLen, msg.MaxLen) case *bgp.RTREndOfData: received.EndOfData++ case *bgp.RTRCacheReset: @@ -184,8 +175,11 @@ func (c *roaClient) handleGRPC(grpcReq *GrpcRequest) { } } -func validateOne(tree *radix.Tree, key string, prefixLen uint8, as uint32) config.RpkiValidationResultType { - _, b, _ := tree.LongestPrefix(key) +func validateOne(tree *radix.Tree, cidr string, as uint32) config.RpkiValidationResultType { + _, n, _ := net.ParseCIDR(cidr) + ones, _ := n.Mask.Size() + prefixLen := uint8(ones) + _, b, _ := tree.LongestPrefix(table.IpToRadixkey(n.IP, prefixLen)) if b == nil { return config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND } else { @@ -217,13 +211,7 @@ func validateOne(tree *radix.Tree, key string, prefixLen uint8, as uint32) confi func (c *roaClient) validate(pathList []*table.Path) { for _, path := range pathList { if tree, ok := c.roas[path.GetRouteFamily()]; ok { - _, n, _ := net.ParseCIDR(path.GetNlri().String()) - ones, _ := n.Mask.Size() - var buffer bytes.Buffer - for i := 0; i < len(n.IP) && i < ones; i++ { - buffer.WriteString(fmt.Sprintf("%08b", n.IP[i])) - } - path.Validation = validateOne(tree, buffer.String()[:ones], uint8(ones), path.GetSourceAs()) + path.Validation = validateOne(tree, path.GetNlri().String(), path.GetSourceAs()) } } } diff --git a/server/rpki_test.go b/server/rpki_test.go index 4fd5de50..9dd135ec 100644 --- a/server/rpki_test.go +++ b/server/rpki_test.go @@ -23,37 +23,28 @@ import ( "testing" ) -func addROA(tree *radix.Tree, addr string, as uint32, prefixLen, maxLen uint8) { - a := net.ParseIP(addr) - b := a.To4() - if b == nil { - b = a.To16() - } - handleIPPrefix(tree, prefixToKey(net.ParseIP(addr), prefixLen), as, b, prefixLen, maxLen) -} - func TestValidate(t *testing.T) { assert := assert.New(t) tree := radix.New() - addROA(tree, "192.168.0.0", 100, 24, 32) - addROA(tree, "192.168.0.0", 200, 24, 24) + addROA(tree, 100, net.ParseIP("192.168.0.0"), 24, 32) + addROA(tree, 200, net.ParseIP("192.168.0.0"), 24, 24) - r1 := validateOne(tree, prefixToKey(net.ParseIP("192.168.0.0"), 24), 24, 100) + r1 := validateOne(tree, "192.168.0.0/24", 100) assert.Equal(r1, config.RPKI_VALIDATION_RESULT_TYPE_VALID) - r2 := validateOne(tree, prefixToKey(net.ParseIP("192.168.0.0"), 24), 24, 200) + r2 := validateOne(tree, "192.168.0.0/24", 200) assert.Equal(r2, config.RPKI_VALIDATION_RESULT_TYPE_VALID) - r3 := validateOne(tree, prefixToKey(net.ParseIP("192.168.0.0"), 24), 24, 300) + r3 := validateOne(tree, "192.168.0.0/24", 300) assert.Equal(r3, config.RPKI_VALIDATION_RESULT_TYPE_INVALID) - r4 := validateOne(tree, prefixToKey(net.ParseIP("192.168.0.0"), 25), 25, 100) + r4 := validateOne(tree, "192.168.0.0/25", 100) assert.Equal(r4, config.RPKI_VALIDATION_RESULT_TYPE_VALID) - r5 := validateOne(tree, prefixToKey(net.ParseIP("192.168.0.0"), 25), 25, 200) + r5 := validateOne(tree, "192.168.0.0/25", 200) assert.Equal(r5, config.RPKI_VALIDATION_RESULT_TYPE_INVALID) - r6 := validateOne(tree, prefixToKey(net.ParseIP("192.168.0.0"), 25), 25, 300) + r6 := validateOne(tree, "192.168.0.0/25", 300) assert.Equal(r6, config.RPKI_VALIDATION_RESULT_TYPE_INVALID) } diff --git a/table/destination.go b/table/destination.go index 4176a1ae..7d3fe487 100644 --- a/table/destination.go +++ b/table/destination.go @@ -41,14 +41,26 @@ const ( BPR_ROUTER_ID = "Router ID" ) +func toRadixkey(b []byte, max uint8) string { + var buffer bytes.Buffer + for i := 0; i < len(b) && i < int(max); i++ { + buffer.WriteString(fmt.Sprintf("%08b", b[i])) + } + return buffer.String()[:max] +} + +func IpToRadixkey(prefix net.IP, prefixLen uint8) string { + b := prefix.To4() + if b == nil { + b = prefix.To16() + } + return toRadixkey(b, prefixLen) +} + func CidrToRadixkey(cidr string) string { _, n, _ := net.ParseCIDR(cidr) ones, _ := n.Mask.Size() - var buffer bytes.Buffer - for i := 0; i < len(n.IP) && i < ones; i++ { - buffer.WriteString(fmt.Sprintf("%08b", n.IP[i])) - } - return buffer.String()[:ones] + return toRadixkey(n.IP, uint8(ones)) } type PeerInfo struct { diff --git a/table/destination_test.go b/table/destination_test.go index abf1c652..e8edf562 100644 --- a/table/destination_test.go +++ b/table/destination_test.go @@ -19,7 +19,7 @@ import ( //"fmt" "github.com/osrg/gobgp/packet" "github.com/stretchr/testify/assert" - //"net" + "net" "testing" "time" ) @@ -191,3 +191,9 @@ func updateMsgD3() *bgp.BGPMessage { UpdatePathAttrs4ByteAs(updateMsg.Body.(*bgp.BGPUpdate)) return updateMsg } + +func TestRadixkey(t *testing.T) { + assert.Equal(t, "000010100000001100100000", CidrToRadixkey("10.3.32.0/24")) + assert.Equal(t, "000010100000001100100000", IpToRadixkey(net.ParseIP("10.3.32.0"), 24)) + assert.Equal(t, "000010100000001100100000", IpToRadixkey(net.ParseIP("10.3.32.0").To4(), 24)) +} |