Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

487 Commits

Repository files navigation

IPAddress

Java library for handling IP addresses and subnets, both IPv4 and IPv6

IP address and network manipulations, CIDR, address and subnet operations, address ranges, iterators, containment checks, longest prefix match, subnetting, address tries, address collections, and more, with polymorphic code

IPAddress is the most popular library for IP address handling with the Java Virtual Machine

Docs

Code Examples

Javadoc

List of Users

In the Maven Central Repository, packaged as an OSGI bundle, packaged as a Linux Fedora rpm

  • Maven group id: com.github.seancfoley
  • Maven artifact id: ipaddress
  • Maven versions: 2.0.2, 3.0.0, 4.3.3, 5.6.2
  • Latest Maven version: Maven Central
  • OSGI bundle since version 5.3.1: com.github.seancfoley.ipaddress

JavaKotlinGoScalaGroovyClojure

As a Java library, it is also interoperable with Kotlin, Scala, Groovy and Clojure

Available as a Go library from the ipaddress-go repository

VersionNotes
1.0.1Requires Java 6 or higher
2.0.2Requires Java 8 or higher
3.0.0Requires Java 8 or higher, features MAC address support, EUI-48 and EUI-64 MAC integration with IPv6, new address framework, new IP string formats parsed and produced, and other additions
4.3.3Requires Java 8 or higher. Features new prefix length handling. IPv4-network/IPv6-subnet-router-anycast/zero-host addresses are interpreted as the prefix block subnet, while other prefixed addresses are individual addresses. There exists the option to preserve the version 3 behaviour. Version 4.2.0 has additional methods for managing prefix blocks. Version 4.3 features improved parsing performance and a change to increment(long) behaviour for subnets.
Latest Version 5.6.2Requires Java 8 or higher. Compatible with Android using Android API level 24 or higher. The code is compiled with Java 8, but provides a Java 9 compiled module-info.class file for those using the Java Platform Module System (JPMS) in Java 9 or later versions. You may need (or wish) to delete the module-info, which can be done with gradle, when using Java 8 environments. Version 5 features the addition of IPAddress sequential range classes IP*AddressSeqRange, the reorganization of classes and interfaces in inet.ipaddr.format package to standard, large, and string subpackages, enhanced address block splitting and merging functionality, Java 8 stream and spliterator methods, additional parsing options, address tries, associative address tries, containment tries, the prefix block allocator, and sequential range lists. Other enhancements are listed on the releases page for 5.0.0, 5.1.0, 5.2.0, 5.3.0, 5.4.0, 5.5.0, and 5.6.0

Getting Started

Java

starting with address or subnet strings

Stringipv6Str = "::/64";
Stringipv4Str = "1.2.255.4/255.255.0.0";
try {
IPAddressipv6Address = newIPAddressString(ipv6Str).toAddress();
IPAddressipv4Address = newIPAddressString(ipv4Str).toAddress();
// use addresses
} catch (AddressStringExceptione) {
Stringmsg = e.getMessage();//detailed message indicating improper string format// handle improperly formatted address string
}

starting with host name strings

StringhostPortStr = "[a:b:c:d:e:f:a:b]:8080";
StringhostServiceStr = "a.b.com:service";
StringhostAddressStr = "1.2.3.4";
StringdnsStr = "a.b.com";
try {
HostNamehost = newHostName(hostPortStr);
InetSocketAddresssocketAddress = host.asInetSocketAddress();
// use socket addresshost = newHostName(hostServiceStr);
socketAddress = host.asInetSocketAddress(
service -> service.equals("service") ? 100 : null);
// use socket addresshost = newHostName(hostAddressStr);
IPAddressaddress = host.asAddress(); // does not resolve// use addresshost = newHostName(dnsStr);
address = host.toAddress(); // resolves if necessary// use address
} catch (HostNameException | UnknownHostExceptione) {
Stringmsg = e.getMessage();
// handle improperly formatted host name or address string
}

Kotlin

starting with address or subnet strings, using exceptions for invalid formats

val ipv6Str ="a:b:c:d::a:b/64"try {
val ipv6AddressStr =IPAddressString(ipv6Str)
val ipv6Addr = ipv6AddressStr.toAddress()
// use addressprintln(ipv6Addr) // a:b:c:d::a:b/64
} catch(e:AddressStringException) {
// handle improperly formatted address stringprintln(e.message)
}

starting with address or subnet strings, using nullable types and safe calls to handle invalid or unexpected formats

val ipv6v4Str ="a:b:c:d:e:f:1.2.3.4/112"val ipv6v4AddressStr =IPAddressString(ipv6v4Str)
val ipAddr:IPAddress?= ipv6v4AddressStr.address
println(ipAddr) // a:b:c:d:e:f:102:304/112val ipv4Addr = ipAddr?.toIPv6()?.embeddedIPv4Address
println(ipv4Addr) // 1.2.3.4/16

Scala

starting with address strings, using exceptions for invalid formats

importscala.util.{Failure, Success, Try}
valaddressStr=newIPAddressString("a:b:c:d::/64")
Try(addressStr.toAddress) match {
caseSuccess(userInfo) =>// use addresscaseFailure(exception: AddressStringException) =>// handle improperly formatted address string
}

Groovy

starting with address or subnet strings, using exceptions for invalid formats

def addressStr =newIPAddressString('a:b:c:d:e:f:1.2.3.4')
try {
def address = addressStr.toAddress()
// use address
} catch (AddressStringException e) {
// handle improperly formatted address string
}

starting with address or subnet strings, checking for null for invalid formats

def subnetStr =newIPAddressString('108.30-31.*.*')
def subnet = subnetStr.getAddress()
if(subnet !=null) {
// use address
}