GithubHelp home page GithubHelp logo

vasturiano / ip.js Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 5.0 628 KB

A JS library for manipulating IP addresses, prefixes and ranges

License: MIT License

JavaScript 80.90% HTML 19.10%
ip ipv4 ipv6 prefix range ipaddress network javascript

ip.js's Introduction

IP.js

NPM package Build Size NPM Downloads

A JS library for manipulating IP addresses, prefixes and ranges. Supports both IPv4 and IPv6. Uses JSBI BigInt to handle IPv6 operations, to retain numeric resolution beyond the 53-bit max safe integer in JavaScript.

Quick start

import { Addr, Prefix, Range } from 'ip.js';

or

const Ip = require('ip.js');

or even

<script src="//unpkg.com/ip.js"></script>

Usage example

const myIp = new Ip.Addr('10.0.0.42');
myIp.version(); // 4
myIp.toNum(); // 167772160

const myPrefix = new Ip.Prefix('10.0.0.0/24');
myPrefix.countIps(); // 256
myPrefix.slice(26); // [10.0.0.0/26, 10.0.0.64/26, 10.0.0.128/26, 10.0.0.192/26]

const myV6Prefix = new Ip.Prefix('2002:DEAD::/48');
myV6Prefix.isIpV4(); // false
myV6Prefix.firstIp(); // 2002:dead::
myV6Prefix.lastIp(); // 2002:dead::ffff:ffff:ffff:ffff:ffff

const myRange = new Ip.Range('10.0.0.0', '10.0.0.42');
myRange.countIps(); // 43
myRange.toEncompassingPrefix(); // 10.0.0.0/26
myRange.toPrefixes(); // [10.0.0.0/27, 10.0.0.32/29, 10.0.0.40/31, 10.0.0.42/32]

API reference

Addr

Class to manipulate single IP addresses.

Instantiation

new Addr(ip, [isV4])

Creates a new object representing an IP address, based on the input ip argument. ip can be either a string (f.e. 10.0.0.0 or 2002:DEAD::), a number (167772160), a BigInt, or another instance of Addr. The optional argument isV4 defines the address family (IPv4 (true) or IPv6 (false)). If it is undefined, the address family is automatically derived from the input ip by either looking for specific family syntax in the string, or checking for numbers larger than 32 bit.

Methods

Method Arguments Return Value Description
toString() - string Returns a string representation of the IP address. IPv6 addresses are lowercase and compressed by removing empty octets.
toNum() - number or BigInt Returns a numeric representation of the IP address. IPv4 addresses are represented as regular JS numbers, while IPv6 are represented as BigInts.
toBin() - string Returns a string with the binary representation of the IP (IPv4: 32 bits long, IPv6: 128 bits long).
version() - number: 4 or 6 Get the IP address family.
isIPv4() - boolean Whether the address family is IPv4.
addIp(ip) ip: Addr Addr Given another IP address (Addr object), returns a new Addr object representing the numeric sum of the two IP addresses. If the two IPs are of different address families, this operation will fail due to the incompatibility between addresses.
subIp(ip) ip: Addr Addr Given another IP address (Addr object), returns a new Addr object representing the numeric subtraction of the input IP from this one. Make sure the input IP is no higher then the existing one, which would lead to negative values. If the two IPs are of different address families, this operation will fail due to the incompatibility between addresses.
compare2Ip(ip) ip: Addr number: -1, 0 or 1 Performs a numeric comparison with another IP address (Addr object), and returns -1 if the current IP is lower than the input, 0 if they are equal and 1 if the current IP is higher than the input. If the two IPs are of different address families, this method returns null.

Prefix

Class to manipulate IP prefixes, with the format <IP address>/<cidr>.

Instantiation

new Prefix(prefix)

or

new Prefix(ip, cidr, [isV4])

Creates a new object representing an IP prefix, based on the input prefix or (ip, cidr) arguments. prefix should be a string representation (f.e. 10.0.0.0/24 or 2002:DEAD::/48). The class can also be instantiated by passing a pair of ip, cidr arguments. ip supports any format understood by the Addr class, and cidr should be a number (IPv4: 0-32, IPv6: 0-128). The optional argument isV4 defines the address family (IPv4 (true) or IPv6 (false)). If it is undefined, the address family is automatically derived from the input prefix.

Methods

Method Arguments Return Value Description
toString() - string Returns a string representation of the IP prefix, following the <ip>/<cidr> syntax. Just as in the Addr class, IPv6 prefix addresses are lowercase and compressed by removing empty octets.
toRange() - Range Returns a Range object representing the address range covered by the prefix.
isIPv4() - boolean Whether the prefix address family is IPv4.
firstIp() - Addr Returns an Addr object representing the first IP address in the prefix.
lastIp() - Addr Returns an Addr object representing the last IP address in the prefix.
getCidr() - number Returns the CIDR size of the prefix.
countIps() - number (IPv4) or BigInt (IPv6) The number of addresses in the prefix (represented as regular number if the prefix is IPv4, or a BigInt if it is IPv6). The result will always be a power of 2 number, according to the prefix's CIDR size.
correctBitBoundary() - Prefix Validates and returns a new Prefix object by setting the correct start IP address that falls on this prefix CIDR bit boundary.
slice(cidr) cidr: number []Prefix De-aggregates the prefix into an array of smaller prefixes of the specified CIDR size. Each smaller prefix is represented as a Prefix object. If the specified CIDR size is larger than the current one, no de-aggregation is performed.

Range

Class to manipulate IP ranges, with the format <first IP> - <last IP>.

Instantiation

new Range(firstIp, lastIp, [isV4])

Creates a new object representing an IP address range, based on the input firstIp and lastIp arguments. Both IPs are required and support any format understood by the Addr class. The optional argument isV4 defines the address family (IPv4 (true) or IPv6 (false)). If it is undefined, the address family is automatically derived from the input IP addresses. If the two IP addresses are of different address families an exception is thrown.

Methods

Method Arguments Return Value Description
toString() - string Returns a string representation of the IP range, following the <firstIp> - <lastIp> syntax. Just as in the Addr class, IPv6 prefix addresses are lowercase and compressed by removing empty octets.
isIPv4() - boolean Whether the range address family is IPv4.
firstIp() - Addr Returns an Addr object representing the first IP address in the range.
lastIp() - Addr Returns an Addr object representing the last IP address in the range.
countIps() - number (IPv4) or BigInt (IPv6) The number of addresses in the address range (represented as regular number if the range is IPv4, or a BigInt if it is IPv6).
cidrCount() - number The calculated CIDR size based on the number of addresses in the address range. This number will be fracional if the address count doesn't fall on a power of 2 bit boundary.
toPrefixes() - []Prefix Generates the array of adjacent prefixes that collectively represent this address range, without any gaps or overlaps.
toEncompassingPrefix() - Prefix Returns the closest smallest valid prefix that fully encompasses this address range.
ipRelPosition(ip, [after]]) ip: Addr, after: boolean number Returns the percentual value (0 to 1) of what is the relative position of the specified IP within this address range. Values <0 or >1 are possible if the IP is outside of the range. The second (optional) argument after (default to false) indicates if the value should be calculated for immediately before the IP address (false) or after (true). Returns null if the IP is not of the same address family as the range.
ipAtPerc(perc) perc: number Addr Returns the IP address that is at the percentual position (0-100) within the range.
overlapPerc(childRange) childRange: Prefix of Range [number, number] Returns the start and end percentage of overlap between this range and a child prefix/range. Values <0 or >100 are possible if the ranges only partially overlap or do not overlap at all. Percentages are relative to the current range.
getNeighborPrefixes() - { up, left, right, downleft, downright }Prefix Returns the 5 CIDR neighbor prefixes: one CIDR up, two side siblings left and right, first and second half down (downleft and downright).

ip.js's People

Contributors

dependabot[bot] avatar vasturiano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ip.js's Issues

Range.toPrefixes() cause error when range end with 255.255.255.255

Describe the bug
Error calling Range.toPrefixes() when range ends with 255.255.255.255 (the last available IPv4 address). See code example.
The IPv4 255.255.255.254 and smaller as last ip is working fine.

To Reproduce
Code example:

const Ip = require('ip.js');

const prefix6 = new Ip.Prefix('::/0');
const range6 = new Ip.Range(prefix6.firstIp(), prefix6.lastIp());

const prefix4 = new Ip.Prefix('0.0.0.0/0');
const range4 = new Ip.Range(prefix4.firstIp(), prefix4.lastIp());
// Same as:
// const range4 = new Ip.Range(new Ip.Addr('0.0.0.0'), new Ip.Addr('255.255.255.255'));

range6.toPrefixes().forEach((p) => {
    console.log(p.toString());
    // Expected:    ::/0
    // Get:         ::/0
    // Worked as expected.
});

range4.toPrefixes().forEach((p) => {
    console.log(p.toString());
    // Expected: 0.0.0.0/0
    // Get: Error: Cidr /33 outside bounds in prefix ::1:0:0
});

Expected behavior
It should return the prefix "0.0.0.0/0".

Desktop (please complete the following information):

  • OS: macOS
  • Node v17.0.1
  • ip.js v1.3.5 (latest)

Codebase refactoring

@vasturiano it will be nice if you permit me to refactor your codebase:

  • add typescript as a main language
  • add eslint & prettier
  • rename & split files (one class per file)
  • setup gitlab actions for automatic tests
  • add semantic-release for automation package publishing when changes landed to master branch

Using prefix returns wrong ip range.

Describe the bug
Using prefix returns wrong ip range. Normarlly I give to prefix function network like 8.8.8.8/24 ,I expect to see 8.8.8.0-8.8.8.255 ip pool but function returns 8.8.8.8-8.8.9.7

To Reproduce
Steps to reproduce the behavior:

  1. let prefix=new Ip.Prefix("8.8.8.8",24)
  2. prefix.lastIp().toString() ;//"8.8.9.7"

Expected behavior

  1. let prefix=new Ip.Prefix("8.8.8.8",24)
  2. prefix.lastIp().toString() ;//"8.8.8.255"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.