1

I have two IPv6 addresses in string format

1) ipv6addr1 is a global network.

2) ipv6addr2 is the current address

I need to find if ipv6addr2 is within the same subnet range of ipv6addr1.

I know I can convert addresses to bytes and then shift bits by prefix and compare but IPv6 addresses are 128 Bits and I cant think of breaking into two longs and then doing it.

AJ.
  • 2,561
  • 9
  • 46
  • 81
  • Hint : 128 bits = 16 bytes ~= 4 `ints` each of 4 bytes..so address fits in array of 4 `ints` – GoldRoger Apr 12 '14 at 06:36
  • I am doing this If you need to compare masked addresses then you will have to do a little more work. inet_pton will not parse prefix lengths ("/something") for you so you will have to: Find the slash Pass the part before the slash to inet_pton Parse the integer after the slash with plain ol' atoi Manually zero out 128 minus that number of bits at the end of the binary form of the address – AJ. Apr 12 '14 at 12:34
  • But How do I Zero out the "128 -prefix" bits in the in6_addr ? – AJ. Apr 12 '14 at 12:35
  • This question has already been answered. See https://stackoverflow.com/questions/7951061 – Sean the Bean Mar 20 '18 at 15:41
  • Possible duplicate of [Matching IPv6 address to a CIDR subnet](https://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet) – Sean the Bean Mar 20 '18 at 15:42

0 Answers0