Giter VIP home page Giter VIP logo

critbitgo's People

Contributors

bboreham avatar fujita avatar k-sone avatar santosh653 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

critbitgo's Issues

IPv4/v6 compatibility

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
	})
}

What does LongestPrefix do?

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?

MatchIP produces incorrect result

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>

Allprefixed() incorrectly matches keys

Allpfrefixed() incorrectly matches keys.

Example

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
        })
}

Example Output

Allprefixed: /router_message 2
Allprefixed: /router_ready 3

Expected Output

Allprefixed: /router_message 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.