ip

repository·main·Indexed 23 days ago

https://github.com/indutny/node-ip

A Node.js module providing utilities for retrieving, comparing, validating, and converting IP addresses. Version 2.0.1 includes support for IPv4 and IPv6 format validation, Buffer conversions, bitwise operations, and subnet information retrieval via CIDR or mask, including range checking with the .contains() method.

Tokens
1.3K
Snippets
3
Records
3
Agent score
32%

What's inside ip

  1. Common IP address operations with ip

    main

    The ip module provides utilities for retrieving, comparing, validating, and converting IP addresses.

    Basic Operations

    • ip.address(): Returns your current IP address.
    • ip.isEqual(addr1, addr2): Compares two IP addresses for equality.
    • ip.isPrivate(addr): Checks if an IP address is private.
    • ip.isV4Format(addr) / ip.isV6Format(addr): Validates the IP format.

    Conversions and Bitwise Operations

    • ip.toBuffer(addr): Converts an IP string to a Buffer.
    • ip.toString(buffer): Converts a Buffer back to an IP string.
    • ip.toLong(addr) / ip.fromLong(long): Converts between IPv4 strings and long integers.
    • ip.mask(addr, mask): Returns the network address.
    • ip.cidr(cidr): Returns the network address from a CIDR string.
    • ip.not(mask): Returns the bitwise NOT of a mask.
    • ip.or(addr, mask): Returns the bitwise OR of an address and a mask.

    In-place Buffer Operations

    You can operate on existing buffers using an offset to avoid extra allocations:

    var buf = new Buffer(128);
    var offset = 64;
    ip.toBuffer('127.0.0.1', buf, offset);  // [127, 0, 0, 1] at offset 64
    ip.toString(buf, offset, 4);            // '127.0.0.1'
    var ip = require('ip');
    
    ip.address() // my ip address
    ip.isEqual('::1', '::0:1'); // true
    ip.toBuffer('127.0.0.1') // Buffer([127, 0, 0, 1])
    ip.toString(new Buffer([127, 0, 0, 1])) // 127.0.0.1
    ip.fromPrefixLen(24) // 255.255.255.0
    ip.mask('192.168.1.134', '255.255.255.0') // 192.168.1.0
    ip.cidr('192.168.1.134/26') // 192.168.1.128
    ip.not('255.255.255.0') // 0.0.0.255
    ip.or('192.168.1.134', '0.0.0.255') // 192.168.1.255
    ip.isPrivate('127.0.0.1') // true
    ip.isV4Format('127.0.0.1'); // true
    ip.isV6Format('::ffff:127.0.0.1'); // true
    
    // operate on buffers in-place
    var buf = new Buffer(128);
    var offset = 64;
    ip.toBuffer('127.0.0.1', buf, offset);  // [127, 0, 0, 1] at offset 64
    ip.toString(buf, offset, 4);            // '127.0.0.1'
    
    // ipv4 long conversion
    ip.toLong('127.0.0.1'); // 2130706433
    ip.fromLong(2130706433); // '127.0.0.1'
  2. Get subnet information and perform range checking

    main

    Use ip.subnet(address, mask) or ip.cidrSubnet(cidr) to retrieve detailed information about a network range. These methods return an object containing network details and a .contains(addr) method for range checking.

    Subnet Object Structure

    The returned object includes:

    • networkAddress: The starting address of the network.
    • firstAddress: The first usable address.
    • lastAddress: The last usable address.
    • broadcastAddress: The broadcast address.
    • subnetMask: The subnet mask string.
    • subnetMaskLength: The prefix length (e.g., 26).
    • numHosts: Number of available hosts.
    • length: Total length of the subnet.
    • contains: A function to check if an address belongs to this subnet.

    Usage Example

    // Using CIDR notation
    var subnet = ip.cidrSubnet('192.168.1.134/26');
    console.log(subnet.networkAddress); // '192.168.1.128'
    
    // Range checking
    var isInRange = subnet.contains('192.168.1.190'); // true
    ip.subnet('192.168.1.134', '255.255.255.192')
    // { networkAddress: '192.168.1.128',
    //   firstAddress: '192.168.1.129',
    //   lastAddress: '192.168.1.190',
    //   broadcastAddress: '192.168.1.191',
    //   subnetMask: '255.255.255.192',
    //   subnetMaskLength: 26,
    //   numHosts: 62,
    //   length: 64,
    //   contains: function(addr){...} }
    
    ip.cidrSubnet('192.168.1.134/26')
    // Same as previous.
    
    // range checking
    ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true