tags:

views:

68

answers:

1

Does anyone have some code that will determine if an IP address (IPv4 or IPv6) is on the same subnet as the machine running the application? I've seen numerous examples of code that does this with IPv4 but I can't find any that support IPv6.

A: 

It should be the same as IPv4 - just mask using the netmask and test for equality.

It's common in IPv6 to have a 64-bit host portion (to allow stateless auto-configuration), but it's not a requirement.

Stephen Cleary
How do I determine the IPv6 netmask?
Spencer Ruport
Hmmm... That's a good question! I did some searching, but I wasn't able to find an answer.
Stephen Cleary
Haha same here! There's an IPv4Mask field in the UnicastAddress class but no equivalent IPv6 field. Very odd.
Spencer Ruport
Downvoted the answer, because an important distinction between IPv4 and IPv6 is that prefixes are advertised in router advertisements, and the Prefix Information Option has a bit in it that tells you whether the addresses with a particular advertised prefix are "on-link" or not. The code may need to inspect the neighbor discovery cache and iterate the list of on-link prefixes. We probably need a more specific description of what "on the same subnet" means to the original poster before we can describe what needs to be done for IPv6.
james woodyatt