Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Net.WalkPrefix API #10

Merged
merged 3 commits into from
Oct 16, 2019
Merged

Add Net.WalkPrefix API #10

merged 3 commits into from
Oct 16, 2019

Conversation

fujita
Copy link
Contributor

@fujita fujita commented Oct 8, 2019

For interating routes that have a given prefix.

Signed-off-by: FUJITA Tomonori [email protected]

For interating routes that have a given prefix.
@k-sone
Copy link
Owner

k-sone commented Oct 10, 2019

Thank you for sending a pull request.

I wrote a sample code and ran it.

package main
 
import (
        "fmt"
        "net"
 
        "github.com/k-sone/critbitgo"
)

func testWalkPrefix(trie *critbitgo.Net, n string) {
        f := func(n *net.IPNet, _ interface{}) bool {
                fmt.Println(n)
                return true
        }

        fmt.Printf("Case of %s\n", n)
        _, s, _ := net.ParseCIDR(n)
        trie.WalkPrefix(s, f)
}

func main() {
        trie := critbitgo.NewNet()
        trie.AddCIDR("192.168.0.0/24", nil)
        trie.AddCIDR("192.168.1.0/24", nil)
        trie.AddCIDR("192.168.2.0/24", nil)

        testWalkPrefix(trie, "192.168.0.0/24")
        testWalkPrefix(trie, "192.168.0.0/23")
}

And I got the output.

Case of 192.168.0.0/24
192.168.0.0/24
Case of 192.168.0.0/23
192.168.0.0/24
192.168.1.0/24
192.168.2.0/24

I think the last line is unnecessary, Is this your expected results?

@fujita
Copy link
Contributor Author

fujita commented Oct 10, 2019

Oops, I messed up. Fixed the handling of prefix length, non multiple of 8.
Thanks for the quick response!

net.go Outdated
wrapper := func(key []byte, value interface{}) bool {
if bit != 0 {
if prefix[div]>>bit != key[div]>>bit {
return true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-				return true
+				return false

If true, I think will unnecessary repetition occurs in Allprefixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, fixed. Thanks!

@k-sone
Copy link
Owner

k-sone commented Oct 16, 2019

Thanks!

@k-sone k-sone merged commit b84b31d into k-sone:master Oct 16, 2019
@fujita fujita deleted the net-walkprefix-api branch October 17, 2019 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants