k-sone / critbitgo Goto Github PK
View Code? Open in Web Editor NEWcrit-bit for golang and its applications (sorted map, IP routing table)
License: MIT License
crit-bit for golang and its applications (sorted map, IP routing table)
License: MIT License
Hi, I'm not sure if this repo is still maintained. It seems that the WalkMatch method does not support both IPv4 and IPv6. This is unexpected to me since a regular Match works fine. Can this be fixed?
func Test_TrieBug(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
trie := critbitgo.NewNet()
_, ipNet, _ := net.ParseCIDR("160.0.0.0/8")
_ = trie.Add(ipNet, 4)
_, ipNet, _ = net.ParseCIDR("a000:0000:0800::/48")
_ = trie.Add(ipNet, 6)
_, ipNet, _ = net.ParseCIDR("a000:0000:0800:a0a0:a0a0:a0a0:a0a0:a0a0/128")
trie.WalkMatch(ipNet, func(ipNet *net.IPNet, i interface{}) bool {
if ipNet.IP.To4() != nil {
t.Fatalf("V6 IP matched with V4 IP")
}
return true
})
}
If I Insert() the following keys:
/hi/bye/why
/hi/bye
/hi
LongestPrefix(/hi) will output /hi. I am expecting /hi/bye/why from the description.
Why is /hi the longest prefix?
I work with the full Internet routing table, over a million IPv4/IPv6 routes. If I then add an IPv6 default route to the crit-bit trie, this route is not found reliably but nil
is returned, which should not be the case. I have now reduced it to the simplest test case for me. I was able to determine that I definitely need IPv4 and IPv6 routes to produce the error case:
package main_test
import (
"net"
"testing"
"github.com/k-sone/critbitgo"
)
func checkerr(err error) {
if err != nil {
panic(err)
}
}
func TestMatchIP(t *testing.T) {
// loopback
_, lo4, err := net.ParseCIDR("127.0.0.1/8")
checkerr(err)
_, lo6, err := net.ParseCIDR("::1/128")
checkerr(err)
// default routes
_, dg4, err := net.ParseCIDR("0.0.0.0/0")
checkerr(err)
_, dg6, err := net.ParseCIDR("::/0")
checkerr(err)
// make new crit-bit trie
trie := critbitgo.NewNet()
// add some CIDRs, mixed IP version needed to reproduce the error
for _, r := range []*net.IPNet{lo4, lo6, dg4, dg6} {
err := trie.Add(r, nil)
checkerr(err)
}
// test IPv6 addr
ip := net.ParseIP("fe80::1")
// longest-prefix-match for test IPv6 addr, expect "::/0"
cidr, _, err := trie.MatchIP(ip)
checkerr(err)
if cidr == nil {
t.Fatalf("MatchIP(%q), expect %q, got %v", ip, dg6, cidr)
}
}
see the test result, the IPv6 default route isn't found:
$ go test
--- FAIL: TestMatchIP (0.00s)
main_test.go:48: MatchIP("fe80::1"), expect "::/0", got <nil>
Allpfrefixed() incorrectly matches keys.
package main
import (
"log"
"github.com/k-sone/critbitgo"
)
func main() {
log.SetFlags(0)
trie := critbitgo.NewTrie()
trie.Insert([]byte("/ctrl_ready"), 1)
trie.Insert([]byte("/router_message"), 2)
trie.Insert([]byte("/router_ready"), 3)
key := []byte("/router_message")
trie.Allprefixed(key, func(key []byte, value interface{}) bool {
log.Printf("Allprefixed: %s %v", key, value)
return true
})
}
Allprefixed: /router_message 2
Allprefixed: /router_ready 3
Allprefixed: /router_message 2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.