0

I am trying to write C# code that will determine the ipv6 prefix ('subnet' in IPv4) for an ipv6 address given in slash/ notation. As with routers, this requires a bitwise operation on an ipv6 address with its prefix length ('subnet mask' in ipv4).

for example: given 2000:1234::1234/64

the code will do:

(2000:1234::1234) AND (FFFF:FFFF:FFFF:FFFF) and will determine that the prefix for this address is 2000:1234:0000:0000

My difficulty doing this comes from the fact that unlike doing bitwise AND on two integers I have to do it on a byteArray or some other data structure. My idea was to convert the ipv6 address into a byte array using IpAddress.getAddressBytes() as well as convert the /prefix integer from the input into a byte array and do a bitwise AND on the two arrays.

Is this the right approach for solving this problem or should I look into other options such as using a 128bit integer library instead of using byte arrays?

Does anyone know from experience what would be the best approach to take?

cake
  • 45
  • 1
  • 4
  • possible duplicate of [how to retrieve IP v6 subnet mask length](http://stackoverflow.com/questions/11834004/how-to-retrieve-ip-v6-subnet-mask-length) – MichaC Jan 04 '14 at 01:19
  • 1
    @MichaC Definitely not a duplicate of that question. Maybe of some _other_ question, though... – Michael Hampton Jan 04 '14 at 03:31
  • I looked around and hopefully didn't miss anything.. Not a lot on ipv6 in .net and I am not finding many 3rd party libraries either. Any help will be much appreciated. – cake Jan 04 '14 at 03:49

0 Answers0