# inet.ipaddr

> IPAddress library for handling IP addresses, both IPv4 and IPv6

Latest version **0.0.1** (published 2018-01-24) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install inet.ipaddr
pnpm add inet.ipaddr
yarn add inet.ipaddr
bun add inet.ipaddr
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2018-01-24 |
| First published | 2018-01-24 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 536 |
| Author | Sean C Foley |
| Maintainers | seancfoley |
| Keywords | IP, IPv6, IPv4, MAC, host, CIDR, subnet, subnetting, wildcards, polymorphism, node.js, browser, parsing, validation, prefix, ports, section, segment, operations, framework |

## Links

- npm: https://www.npmjs.com/package/inet.ipaddr
- Repository: https://github.com/seancfoley/IPAddress
- Homepage: https://github.com/seancfoley/IPAddress#readme
- Issues: https://github.com/seancfoley/IPAddress/issues
- npm.io page: https://npm.io/package/inet.ipaddr

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 0.0.1 (latest) — 2018-01-24
- 0.0.0 — 2018-01-24

## README

# 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](https://mvnrepository.com/open-source/ip-address-libs) for IP address handling with the Java Virtual Machine

[Docs](https://seancfoley.github.io/IPAddress/)

[Code Examples](https://github.com/seancfoley/IPAddress/wiki/Code-Examples)

[Javadoc](https://seancfoley.github.io/IPAddress/IPAddress/apidocs/)

[List of Users](https://github.com/seancfoley/IPAddress/wiki)

[In the Maven Central Repository](https://repo1.maven.org/maven2/com/github/seancfoley/ipaddress/), packaged as an OSGI bundle, packaged as a [Linux Fedora rpm](http://rpmfind.net/linux/rpm2html/search.php?query=ipaddress)
- Maven group id: com.github.seancfoley
- Maven artifact id: [ipaddress](https://central.sonatype.com/artifact/com.github.seancfoley/ipaddress)
- Maven versions: [2.0.2](https://central.sonatype.com/artifact/com.github.seancfoley/ipaddress/2.0.2), [3.0.0](https://central.sonatype.com/artifact/com.github.seancfoley/ipaddress/3.0.0), [4.3.3](https://central.sonatype.com/artifact/com.github.seancfoley/ipaddress/4.3.3), [5.6.2](https://central.sonatype.com/artifact/com.github.seancfoley/ipaddress/5.6.2) 
- Latest Maven version: [![Maven Central](https://img.shields.io/maven-central/v/com.github.seancfoley/ipaddress.svg?label=Maven%20Central)](https://central.sonatype.com/namespace/com.github.seancfoley)
- OSGI bundle since version 5.3.1: com.github.seancfoley.ipaddress

![Java](https://github.com/seancfoley/IPAddress/blob/gh-pages/images/java_logo.png?raw=true) ![Kotlin](https://github.com/seancfoley/IPAddress/blob/gh-pages/images/kotlin_logo.png?raw=true)![Go](https://github.com/seancfoley/IPAddress/blob/gh-pages/images/go_logo.png?raw=true) ![Scala](https://github.com/seancfoley/IPAddress/blob/gh-pages/images/scala.png?raw=true)![Groovy](https://github.com/seancfoley/IPAddress/blob/gh-pages/images/groovy.png?raw=true)![Clojure](https://github.com/seancfoley/IPAddress/blob/gh-pages/images/clojure.png?raw=true)

As a [Java](https://www.oracle.com/java/) library, it is also interoperable with [Kotlin](https://kotlinlang.org/), [Scala](https://scala-lang.org/), [Groovy](http://www.groovy-lang.org/) and [Clojure](https://clojure.org/)

Available as a [Go](https://go.dev/) library from the [ipaddress-go repository](https://github.com/seancfoley/ipaddress-go)

Version | Notes         |
------- | ------------- |
[1.0.1](https://github.com/seancfoley/IPAddress/releases/tag/v1.0.1) | Requires Java 6 or higher |
[2.0.2](https://github.com/seancfoley/IPAddress/releases/tag/v2.0.2) | Requires Java 8 or higher |
[3.0.0](https://github.com/seancfoley/IPAddress/releases/tag/v3.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](https://github.com/seancfoley/IPAddress/releases/tag/v4.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.6.2](https://github.com/seancfoley/IPAddress/releases/tag/v5.6.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) in Java 9 or later versions.  You may need (or wish) to [delete the module-info](https://github.com/seancfoley/IPAddress/issues/16#issuecomment-452425235), which can be done [with gradle](https://github.com/seancfoley/IPAddress/issues/16#issuecomment-452564690), 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](https://github.com/seancfoley/IPAddress/releases/tag/v5.0.0), [5.1.0](https://github.com/seancfoley/IPAddress/releases/tag/v5.1.0), [5.2.0](https://github.com/seancfoley/IPAddress/releases/tag/v5.2.0), [5.3.0](https://github.com/seancfoley/IPAddress/releases/tag/v5.3.0), [5.4.0](https://github.com/seancfoley/IPAddress/releases/tag/v5.4.0), [5.5.0](https://github.com/seancfoley/IPAddress/releases/tag/v5.5.0), and [5.6.0](https://github.com/seancfoley/IPAddress/releases/tag/v5.6.0)


## 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
}
```

---
_Source: https://npm.io/package/inet.ipaddr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
