IPAddress

by seancfoley

seancfoley /IPAddress

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

160 Stars 22 Forks Last release: 29 days ago (v5.3.2) Apache License 2.0 321 Commits 22 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

IPAddress

Library for handling IP addresses and subnets, both IPv4 and IPv6

View Project Page

View Javadoc

View Code Examples

In the Maven Central Repository and the Bintray JCenter 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.3.2 - OSGI bundle since version 5.3.1: com.github.seancfoley.ipaddress

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

Integrate into Development: Intellij, Android, and Eclipse

Make your IPv4 App work with IPv6

Version

Notes
1.0.1 Requires Java 6 or higher
2.0.2 Requires Java 8 or higher
3.0.0 Requires 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.3 Requires 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.3.2 Requires 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). You may need (or wish) to delete the module-info, which can be done with gradle, when using Android, other Java 8 environments, or Java version 9 and up environments that do not properly support modules. Since Android Studio 3.3, deleting the module-info is no longer necessary. 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, the improved parsing performance introduced with version 4.3.0, Java 8 stream and spliterator methods, additional parsing options, address tries, and associative address tries. Other enhancements are listed on the releases page for 5.0.0, 5.1.0, 5.2.0 and 5.3.0

Planned future additions: ports to JavaScript and Go.

Getting Started

Java

starting with address or subnet strings

java
String ipv6Str = "::/64";
String ipv4Str = "1.2.255.4/255.255.0.0";
try {
    IPAddress ipv6Address = new IPAddressString(ipv6Str).toAddress();
    IPAddress ipv4Address = new IPAddressString(ipv4Str).toAddress();
        // use addresses
} catch (AddressStringException e) {
    String msg = e.getMessage();//detailed message indicating improper string format
    // handle improperly formatted address string
}
starting with host name strings ```java String hostPortStr = "[a:b:c:d:e:f:a:b]:8080"; String hostServiceStr = "a.b.com:service"; String hostAddressStr = "1.2.3.4"; String dnsStr = "a.b.com"; try { HostName host = new HostName(hostPortStr); InetSocketAddress socketAddress = host.asInetSocketAddress(); // use socket address
host = new HostName(hostServiceStr);
socketAddress = host.asInetSocketAddress(
    service -> service.equals("service") ? 100 : null);
// use socket address

host = new HostName(hostAddressStr); IPAddress address = host.asAddress(); // does not resolve // use address

host = new HostName(dnsStr); address = host.toAddress(); // resolves if necessary // use address

} catch (HostNameException | UnknownHostException e) { String msg = e.getMessage(); // handle improperly formatted host name or address string } ```

Kotlin

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

kotlin
val ipv6Str = "a:b:c:d::a:b/64"
try {
    val ipv6AddressStr = IPAddressString(ipv6Str)
    val ipv6Addr = ipv6AddressStr.toAddress()
    // use address
    println(ipv6Addr) // a:b:c:d::a:b/64
} catch(e: AddressStringException) {
    // handle improperly formatted address string
    println(e.message)
}
starting with address or subnet strings, using nullable types and safe calls to handle invalid or unexpected formats ```kotlin 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/112

val ipv4Addr = ipAddr?.toIPv6()?.embeddedIPv4Address println(ipv4Addr) // 1.2.3.4/16 ```

Scala

starting with address strings, using exceptions for invalid formats ```scala import scala.util.{Failure, Success, Try}

val addressStr = new IPAddressString("a:b:c:d::/64") Try(addressStr.toAddress) match { case Success(userInfo) => // use address case Failure(exception: AddressStringException) => // handle improperly formatted address string } ```

Groovy

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

groovy
def addressStr = new IPAddressString('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
groovy
def subnetStr = new IPAddressString('108.30-31.*.*')
def subnet = subnetStr.getAddress()
if(subnet != null) {
    // use address
}

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.