Parsing IPV6 and Storing in Radix Trie for LPM match

I am looking to parse a IPv6 CIDR string to store in a radix Trie. Can anyone help?
Below is a sample of IPv4 for explaining the problem.

func main() {
ip, ipv4Net, err := net.ParseCIDR(“”)
if err != nil {
fmt.Println(“ip”, ip, " ipnet", ipv4Net, “n/w”, ipv4Net.IP, “mask”, ipv4Net.Mask)

Result: ip ipnet n/w mask ffffff00

Radix tree implements Insert and GetLongestPrefix.

tree.Insert([]byte(“”), “value1”) —> I must insert 192.168.100 and not which is return by parseCIDR

tree.GetLongestPrefix(“”) -->No Match

“” is not a hit because there is no key “192.168.100” in the tree.
How do I parse the CIDR and convert the subnet prefix into a key that is of size mask length?

Ideally I need a byte array of the prefix for the mask to store as a key.

Indeed, ParseCIDR is the right function as it will get you a *net.IPNet that has both the address and netmask. Note that both the IP and IPMask type are in fact byte slices holding the address/mask bits, so you can use them as such. The IPMask type also has a Size() method that returns the number of leading one bits - the prefix length. In the simplest case you can use this to slice the appropriate prefix from the IP and insert into your trie.

However, prefixes don’t necessarily end on byte boundaries. In IPv6 you’ll usually have at least nibble alignment, but it’s not guaranteed, it’s still less than a byte, and of course in IPv4 all bets are off.

So you will probably need to teach your trie about bit masks so it can calculate prefixes based on bits, not bytes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.