简体   繁体   中英

Longest IPv6 prefix match

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)

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 i th 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM