0

We are trying to implement a lngest prefix match of IPv6 addresses. What is the best way to represent IPv6 addresses to perform this computation (Longest Prefix match) efficiently.

IPv6 addresses are usually represented in Array[Byte]. (IPv6 addresses are usually represented in Array[Byte] [16]).

And to find a longest prefix match we need to convert the addresses to BitSet (or some sort of array representation of bits) and then find out the longest prefix.

New to this level of stack, and wondering if there is anything I am missing.

Any pointers that would help me move in right direction is highly appreciated.

FYI, this is on Scala. (So any JVM related pointers would be helpful)

user462455
  • 12,838
  • 18
  • 65
  • 96

1 Answers1

0

If you just keep your original two arrays and scan down the bytes with a while-loop until they're non-identical, you should be about as fast as you can be. Once you get a hit, if you want to know what bit it is, use

java.lang.Integer.numberOfLeadingZeros((a[i] << 24) ^ (b[i] << 24))

to count how many bits within the ith index (assuming a and b are your arrays) match.

One could think about converting to a larger numeric type like Long first, but typically you'll take as much time doing the conversion as you will to find the match, so unless you have a bunch more bitwise math to do on the addresses, you may as well keep it as bytes.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407