I have seen plenty of great C# examples which demonstrate how to convert IPv4 addresses provided in CIDR notation (e.g. 192.168.0.1/25) into their relevant ranges (192.168.0.1 – 192.168.0.126). My program needs to be able to do this (to compute all the addresses within my local subnet) but I want to also support IPv6.
If my C# program has all of my typical ipconfig information (IPv4 address, subnet mask, IPv6 address, link-local v6 address, default gateway) – how would I go about generating a list of all of the IPv6 addresses in my local subnet and outputting them to the console?
You can use the eExNetworkLibrary.IP.IPAddressAnalysis class from the eExNetworkLibrary.
The following code works with IPv4 and IPv6 (just tested).
I’m not completely sure if I have done the conversion from the prefix length to a byte array containing the subnet mask right, but this code should give you a good starting point.Edit: Updated the bit-bending part of the code. May be ugly, but works for this example. I think you will be capable of finding a better solution, if you need to. Those BitArrays are a pain in the neck.
Be aware that generating an IPv6 network range can be a very memory/cpu exhausting task if the network is large.