0

I am trying to calculate the last IP address from CIDR using Apache commons-net for IPV4

SubnetUtils su = new SubnetUtils(cidr);
SubnetInfo si = su.getInfo();
si.getHighAddress();

It won't work for IPV6, can anybody suggest some library to solve this issue?

Cœur
  • 37,241
  • 25
  • 195
  • 267
karthik
  • 11
  • 1
  • "_Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it._" – Ron Maupin Sep 09 '16 at 15:10
  • https://stackoverflow.com/questions/577363/how-to-check-if-an-ip-address-is-from-a-particular-network-netmask-in-java – Omid Dec 03 '19 at 14:32

2 Answers2

3

The IPAddress Java library can do what you are describing here.

For any given CIDR string, whether IPv4 such as 1.2.0.0/16, or IPv6 such as 1:2:3:4::/64, you can do the following:

IPAddressString string = new IPAddressString("1:2:3:4::/64");
IPAddress subnet = string.getAddress();
IPAddress highest = subnet.getUpper();
String highestString = highest.toCanonicalString();
System.out.println(highestString);

For 1:2:3:4::/64 the output of the above code is 1:2:3:4:ffff:ffff:ffff:ffff

Sean F
  • 4,344
  • 16
  • 30
1

Normally, an IPv6 subnet will be a /64 (there are a very few exceptions to this, and other subnet sizes can break things). Since IPv6 doesn't even have a broadcast concept, the last address in a subnet will have ffff:ffff:ffff:ffff as the last 64 bits. IPv6 can also use the first (0000:0000:0000:0000) address in a subnet. This makes it much easier than IPv4 since every address in the subnet is a valid address.

Ron Maupin
  • 6,180
  • 4
  • 29
  • 36