asergeyev / nradix

Translated to Golang implementation of nginx's radix tree code.
MIT License
46 stars 17 forks source link

IPv4 and IPv6 collision #2

Open fishnux opened 4 years ago

fishnux commented 4 years ago

There seems to be a collision with the following specific CIDRs:

package main

import (
    "fmt"
    "github.com/asergeyev/nradix"
)

func main() {
    tr := nradix.NewTree(0)

    err := tr.AddCIDR("42.0.0.0/22", "Got 42.0.0.0/22")
    if err != nil {
        fmt.Print(err)
    }

    inf, err := tr.FindCIDR("2a00::/22")
    if err != nil {
        fmt.Print(err)
    }

    fmt.Print(inf)
}

https://play.golang.org/p/xQiaBZQZkWi

lior-k commented 4 years ago

yes, I'm encountering the same issue. I'm feeding the tree IPV4 CIDRs only and when I ask on an IPv6 - it finds it in the tree. Can we please have a fix for it?

misiek08 commented 2 days ago

I did some simplification works on branch in my fork - replacing current implementation for IPv4 with IPv6-mapped addresses. The only problem for some of you can be performance - simple IPv4 Find (e.g. t.FindCIDR("73.26.28.24") in tree with 1 million of /24 entries) takes 200ns/op instead of 36ns/op.

When I'll find again some free time I'll try to look into it - I used net package for IPv4 parsing and it is a lot slower than custom made bit mangling, of course :)