Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPv4/IPv6 network calculations and validation for Java?

I am looking for a package that is similar to Net_IPv4 and Net_IPv6 but written for Java. It needs to be able to do the following:

  • Verify an address is valid (e.g. 127.0.0.1 is valid, 127.0.0.257 is not)
  • Return if an address is contained within a subnet (e.g. 127.0.0.11 is in 127.0.0.0/28)
  • Return the broadcast address for a given subnet (e.g. for 127.0.0.0/28 it's 127.0.0.15)

It would be awesome if it could also:

  • Return a list of addresses for a subnet in order
  • Sort a list of addresses

I could probably write a package to do all this, but if someone has already gone through the trouble and most likely has done a better job, I'm all about using that. Anyone know of such a package or packages that could do all this? We are expanding into IPv6, so it needs to work for both IPv4 and IPv6 if possible.

I appreciate any help.

like image 971
Joseph Avatar asked Feb 17 '09 18:02

Joseph


People also ask

What is the best way to validate the IP address in Java?

In Java, this can be done using Pattern. matcher(). Return true if the string matches with the given regex, else return false.

How do I calculate IPv4 from IPv6?

IPv4 to IPv6 mapping To convert Internet Protocol 4 (IPv4) to Internet Protocol 6 (IPv6), perform the following steps. Open the tool: IPv4 to IPv6 converter. Enter any valid IPv4 address, and click on the "Convert to IPv6" button. The tool will process your request and provide you the converted IPv6 address.

How do I know if I have IPv4 or IPv6 Java?

We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.

How do you check if an IP address is IPv4 or IPv6?

IPv4 addresses are separated by periods, while IPv6 addresses are separated by colons. Both IP addresses are used to identify machines connected to a network.


1 Answers

Maybe CIDRUtils can help you. It enables you to convert CIDR notation to an IP range.

Disclaimer: I am the author of CIDRUtils.

like image 197
Edin Dazdarevic Avatar answered Oct 05 '22 23:10

Edin Dazdarevic