0.0.1 • Published 6 years ago

inet.ipaddr v0.0.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
6 years ago

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

Developed as an Eclipse project, the project files are checked in so it can be easily be imported into an Eclipse workspace. As a java library, it is also interoperable with Kotlin, Scala, Groovy and Clojure.

VersionNotes
v1.0.1Requires Java 6 or higher
v2.0.2Requires Java 8 or higher
v3.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
v4.3.1Requires 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 v5.1.0Requires Java 8 or higher. Support for Java 9, 10, 11 JPMS modules - the code is compiled with Java 8 but provides a Java 9 compiled module-info.class file. Compatible with Android using Android API level 24 or higher. You may need to delete the module-info when using Android or other 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, and the improved parsing performance introduced with version 4.3.0. Additional parsing options. Other enhancements listed on the releases page

Planned future additions: ports to TypeScript / JavaScript and Go.

Getting Started

Add to Intellij Project

  1. Click File from the toolbar
  2. Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
  3. Select Modules at the left panel
  4. Dependencies tab
  5. '+' → JARs or directories
  6. Library...
  7. From Maven...
  8. Enter "ipaddress" into search bar, search
  9. After waiting for Intellij to locate ipaddress on maven, select the ipaddress version you prefer

Add to Android Studio 3.5 Project

First Open your project's build.gradle. Add maven central to the end of the list of repositories, as shown:

    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}
  1. Click File from the toolbar
  2. Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
  3. Select Modules in the left panel, and app under modules, in the dialogs for Source and Target Compatibility choose 1.8 (Java 8)
  4. Select Dependencies at the left panel, and app under modules
  5. Select '+' under Declared Dependencies
  6. Choose Library Dependency
  7. In the Add Library Dependency dialog, enter "ipaddress" into search bar, search
  8. After waiting for Android Studio to locate ipaddress on maven, select the ipaddress version you prefer
  9. You need to run on API level 24 or higher

Add to Eclipse Project

  1. From the toolbar click Window -> Show View -> Other
  2. In the Show View window, open Maven -> Maven Repositories
  3. In the window that appears, under "Global Repositories", right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"
  4. It will take a while to build the index
  5. Once indexing is complete, if the project is not a Maven project, in the package explorer view, right-click on the project name -> configure -> convert to Maven project
  6. In the package explorer view, right-click on the project name -> Maven -> Add Dependency
  7. In the search box halfway down, type "ipaddress"
  8. After waiting for Eclipse to locate ipaddress in the index, select the ipaddress version you prefer, click OK

Java

starting with address or subnet strings

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 format in address string
	// handle improperly formatted address string
}

starting with host name strings

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

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

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