k-sone / critbitgo

crit-bit for golang and its applications (sorted map, IP routing table)
MIT License
45 stars 14 forks source link

IPv4/v6 compatibility #15

Open kc834 opened 11 months ago

kc834 commented 11 months ago

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