### Install ipaddr.js Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Install the ipaddr.js library using npm. ```bash npm install ipaddr.js ``` -------------------------------- ### Quick Start: Basic Usage Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Demonstrates basic usage of ipaddr.js, including validation, parsing, and CIDR parsing. ```javascript const ipaddr = require('ipaddr.js'); ipaddr.isValid('192.168.1.1'); // => true ipaddr.isValid('2001:db8::1'); // => true ipaddr.isValid('not an address'); // => false const addr = ipaddr.parse('2001:db8::1'); addr.kind(); // => 'ipv6' addr.toString(); // => '2001:db8::1' const [network, prefix] = ipaddr.parseCIDR('10.0.0.0/8'); network.toString(); // => '10.0.0.0' prefix; // => 8 ``` -------------------------------- ### Get IPv4 Address Kind Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the string 'ipv4' to identify the address type. ```javascript ipaddr.parse('192.168.1.1').kind(); // => 'ipv4' ``` -------------------------------- ### Get Compact IPv6 Address String Representation Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use `toString()` to obtain the compact string representation of an IPv6 address, which is identical to the output of `toRFC5952String()`. ```javascript ipaddr.parse('2001:0db8::0001').toString(); // => '2001:db8::1' ``` -------------------------------- ### Get IPv4 Subnet Mask from Prefix Length Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv4 subnet mask corresponding to the given CIDR prefix length. Use the toString() method to get the string representation. ```javascript ipaddr.IPv4.subnetMaskFromPrefixLength(24).toString(); // => '255.255.255.0' ipaddr.IPv4.subnetMaskFromPrefixLength(16).toString(); // => '255.255.0.0' ``` -------------------------------- ### Normalize IPv6 Address to Full String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use `toNormalizedString()` to get the IPv6 address with all eight 16-bit groups written out in lowercase hexadecimal, separated by colons, and without `::` compression. ```javascript ipaddr.parse('2001:db8::1').toNormalizedString(); // => '2001:db8:0:0:0:0:0:1' ``` -------------------------------- ### Get the network address from an IPv6 CIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the network address (first address) for a given IPv6 CIDR block. ```javascript ipaddr.IPv6.networkAddressFromCIDR('2001:db8::42/32').toString(); // => '2001:db8::' ``` -------------------------------- ### Get IPv4 Address Octets Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Retrieves the four octets of an IPv4 address as an array of numbers. ```javascript ipaddr.parse('192.168.1.1').octets; // => [192, 168, 1, 1] ``` -------------------------------- ### Get IPv4 Network Address from CIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the network address for a given IPv4 CIDR block. Use the toString() method to get the string representation. ```javascript ipaddr.IPv4.networkAddressFromCIDR('192.168.1.42/24').toString(); // => '192.168.1.0' ipaddr.IPv4.networkAddressFromCIDR('10.1.2.3/8').toString(); // => '10.0.0.0' ``` -------------------------------- ### Get IPv4 Broadcast Address from CIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the broadcast address for a given IPv4 CIDR block. Use the toString() method to get the string representation. ```javascript ipaddr.IPv4.broadcastAddressFromCIDR('192.168.1.0/24').toString(); // => '192.168.1.255' ipaddr.IPv4.broadcastAddressFromCIDR('10.0.0.1/8').toString(); // => '10.255.255.255' ``` -------------------------------- ### Get IPv6 subnet mask from prefix length Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Converts a CIDR prefix length into its corresponding IPv6 subnet mask representation. ```javascript ipaddr.IPv6.subnetMaskFromPrefixLength(64).toString(); // => 'ffff:ffff:ffff:ffff::' ipaddr.IPv6.subnetMaskFromPrefixLength(48).toString(); // => 'ffff:ffff:ffff::' ``` -------------------------------- ### Get the broadcast address from an IPv6 CIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Calculates and returns the broadcast address (last address) for a given IPv6 CIDR block. ```javascript ipaddr.IPv6.broadcastAddressFromCIDR('2001:db8::/120').toString(); // => '2001:db8::ff' ``` -------------------------------- ### Get IPv4 Address Range Name Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the name of the built-in range the address belongs to, or 'unicast' if it does not match any special range. Recognised names include 'unspecified', 'broadcast', 'multicast', 'linkLocal', 'loopback', 'private', and 'reserved'. ```javascript ipaddr.parse('127.0.0.1').range(); // => 'loopback' ipaddr.parse('192.168.1.1').range(); // => 'private' ipaddr.parse('169.254.1.1').range(); // => 'linkLocal' ipaddr.parse('8.8.8.8').range(); // => 'unicast' ``` -------------------------------- ### Get prefix length from IPv6 subnet mask Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Determines the CIDR prefix length if the given IPv6 address is a valid, contiguous subnet mask. Returns `null` otherwise. ```javascript ipaddr.parse('ffff:ffff:ffff:ffff::').prefixLengthFromSubnetMask(); // => 64 ipaddr.parse('ffff:ffff::').prefixLengthFromSubnetMask(); // => 32 ``` -------------------------------- ### Get IPv4 Prefix Length from Subnet Mask Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the CIDR prefix length if the address is a valid contiguous subnet mask, or null otherwise. ```javascript ipaddr.parse('255.255.255.0').prefixLengthFromSubnetMask(); // => 24 ipaddr.parse('255.255.255.240').prefixLengthFromSubnetMask(); // => 28 ipaddr.parse('255.192.168.0').prefixLengthFromSubnetMask(); // => null ``` -------------------------------- ### addr.toString Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IP address as a dotted-decimal string. ```APIDOC ## addr.toString() ### Description Returns the address as a dotted-decimal string. ### Method Instance method. ### Response - Returns the IP address as a dotted-decimal string. ### Request Example ```js ipaddr.parse('192.168.001.001').toString(); ``` ### Response Example ``` '192.168.1.1' ``` ``` -------------------------------- ### addr.toString() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the compact string representation of the address, identical to `toRFC5952String()`. ```APIDOC ## addr.toString() ### Description Returns the compact string representation of the address, identical to `toRFC5952String()`. ### Method N/A (Method on an address object) ### Endpoint N/A ### Parameters None ### Request Example ```js ipaddr.parse('2001:0db8::0001').toString(); ``` ### Response #### Success Response - **string**: The compact string representation of the address. #### Response Example ``` '2001:db8::1' ``` ``` -------------------------------- ### Convert IPv4 Address to String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv4 address as a dotted-decimal string. ```javascript ipaddr.parse('192.168.001.001').toString(); // => '192.168.1.1' ``` -------------------------------- ### addr.toIPv4Address() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Converts an IPv4-mapped IPv6 address to its IPv4 equivalent. Throws if the address is not IPv4-mapped. ```APIDOC ## addr.toIPv4Address() ### Description Converts an IPv4-mapped IPv6 address to its IPv4 equivalent. Throws if the address is not IPv4-mapped. ### Method N/A (Method on an address object) ### Endpoint N/A ### Parameters None ### Request Example ```js ipaddr.parse('::ffff:192.168.1.1').toIPv4Address().toString(); ``` ### Response #### Success Response - **IPv4Address Object**: An object representing the IPv4 address. #### Response Example ``` '192.168.1.1' ``` ``` -------------------------------- ### Convert IPv4 Address to Normalized String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv4 address in standard four-part dotted-decimal notation. For IPv4, this is the same as toString(). ```javascript ipaddr.parse('192.168.1.1').toNormalizedString(); // => '192.168.1.1' ``` -------------------------------- ### Parse CIDR Notation Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parse an IP address with a CIDR prefix length. Returns a two-element array [address, prefixLength]. Throws if the input is invalid. ```javascript const [addr, prefix] = ipaddr.parseCIDR('192.168.1.0/24'); addr.toString(); // => '192.168.1.0' prefix; // => 24 ``` -------------------------------- ### addr.toByteArray() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Converts the IPv6 address into an array of 16 bytes, ordered in network byte order. ```APIDOC ## addr.toByteArray() ### Description Returns the address as an array of 16 bytes in network byte order. ### Example ```js ipaddr.parse('2001:db8::1').toByteArray(); // => [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] ``` ``` -------------------------------- ### Construct IP Address from Byte Array Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use ipaddr.fromByteArray to create an IPv4 or IPv6 address from a byte array in network byte order. The array must contain 4 bytes for IPv4 or 16 bytes for IPv6. ```javascript ipaddr.fromByteArray([127, 0, 0, 1]).toString(); // => '127.0.0.1' ipaddr.fromByteArray([ 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, ]).toString(); // => '2001:db8::1' ``` -------------------------------- ### addr.match(other, cidrBits) / addr.match([address, cidrBits]) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Checks if the current IPv6 address falls within a specified CIDR range. Can accept either separate address and CIDR bits, or a parsed CIDR object. ```APIDOC ## addr.match(other, cidrBits) / addr.match([address, cidrBits]) ### Description Returns `true` if the address falls within the given CIDR range. ### Parameters #### Path Parameters - **other** (IPv6 object or array) - Required - The address or CIDR range to match against. - **cidrBits** (number) - Optional - The CIDR prefix length if `other` is an IPv6 object. ### Request Example ```js const addr = ipaddr.parse('2001:db8:1234::1'); addr.match(ipaddr.parse('2001:db8::'), 32); // => true addr.match(ipaddr.parseCIDR('2001:db8::/32')); // => true addr.match(ipaddr.parseCIDR('2001:db9::/32')); // => false ``` ``` -------------------------------- ### Identify IPv6 address kind Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the string 'ipv6' to identify the address type. ```javascript ipaddr.parse('2001:db8::1').kind(); // => 'ipv6' ``` -------------------------------- ### addr.subnetMatch(rangeList[, defaultName]) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Provides a shorthand for `ipaddr.subnetMatch(addr, rangeList, defaultName)`, matching the address against a list of defined ranges. ```APIDOC ## addr.subnetMatch(rangeList[, defaultName]) ### Description Instance-method shorthand for `ipaddr.subnetMatch(addr, rangeList, defaultName)`. ### Parameters #### Path Parameters - **rangeList** (object) - Required - An object mapping range names to CIDR definitions. - **defaultName** (string) - Optional - A default name to return if no match is found. ### Request Example ```js const addr = ipaddr.parse('2001:db8::1'); const ranges = { documentation: [ipaddr.parse('2001:db8::'), 32] }; addr.subnetMatch(ranges); // => 'documentation' ``` ``` -------------------------------- ### addr.kind() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the kind of IP address, which is always 'ipv6' for IPv6 address objects. ```APIDOC ## addr.kind() ### Description Always returns `'ipv6'`. ### Example ```js ipaddr.parse('2001:db8::1').kind(); // => 'ipv6' ``` ``` -------------------------------- ### addr.parts Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Provides access to the eight 16-bit groups of an IPv6 address as an array of numbers. ```APIDOC ## addr.parts ### Description The eight 16-bit groups of the address as an array of numbers. ### Example ```js ipaddr.parse('2001:db8:10::1234:dead').parts; // => [0x2001, 0x0db8, 0x0010, 0, 0, 0, 0x1234, 0xdead] ``` ``` -------------------------------- ### Convert IPv6 address to byte array Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv6 address represented as an array of 16 bytes in network byte order. ```javascript ipaddr.parse('2001:db8::1').toByteArray(); // => [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] ``` -------------------------------- ### Convert IPv4 Address to IPv4-Mapped IPv6 Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv4-mapped IPv6 representation of an IPv4 address (::ffff:x.x.x.x). ```javascript ipaddr.parse('192.168.1.1').toIPv4MappedAddress().toString(); // => '::ffff:c0a8:101' ``` -------------------------------- ### addr.toFixedLengthString() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the address with all eight groups fully expanded to four hex digits, separated by colons. No `::` abbreviation is used. ```APIDOC ## addr.toFixedLengthString() ### Description Returns the address with all eight groups fully expanded to four hex digits, separated by colons. No `::` abbreviation is used. ### Method N/A (Method on an address object) ### Endpoint N/A ### Parameters None ### Request Example ```js ipaddr.parse('2001:db8::1').toFixedLengthString(); ``` ### Response #### Success Response - **string**: The address in fixed-length string format. #### Response Example ``` '2001:0db8:0000:0000:0000:0000:0000:0001' ``` ``` -------------------------------- ### Convert IPv6-mapped IPv4 Address to IPv4 Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use `toIPv4Address()` to convert an IPv4-mapped IPv6 address to its equivalent IPv4 string representation. This method throws an error if the address is not IPv4-mapped. ```javascript ipaddr.parse('::ffff:192.168.1.1').toIPv4Address().toString(); // => '192.168.1.1' ``` -------------------------------- ### Match IPv6 address against a list of ranges Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md An instance-method shorthand for `ipaddr.subnetMatch`. It checks if the address falls within any of the provided ranges and returns the associated name. ```javascript const addr = ipaddr.parse('2001:db8::1'); const ranges = { documentation: [ipaddr.parse('2001:db8::'), 32] }; addr.subnetMatch(ranges); // => 'documentation' ``` -------------------------------- ### addr.kind Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the type of the IP address, which is always 'ipv4' for IPv4 addresses. ```APIDOC ## addr.kind() ### Description Always returns 'ipv4'. ### Method Instance method. ### Response - Returns the string 'ipv4'. ### Request Example ```js ipaddr.parse('192.168.1.1').kind(); ``` ### Response Example ``` 'ipv4' ``` ``` -------------------------------- ### Expand IPv6 Address to Fixed Length String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use `toFixedLengthString()` to represent an IPv6 address with all eight groups expanded to four hexadecimal digits, separated by colons, without using `::` abbreviation. ```javascript ipaddr.parse('2001:db8::1').toFixedLengthString(); // => '2001:0db8:0000:0000:0000:0000:0000:0001' ``` -------------------------------- ### addr.toNormalizedString Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IP address in standard four-part dotted-decimal notation. For IPv4, this is identical to `toString()`. ```APIDOC ## addr.toNormalizedString() ### Description Returns the address in standard four-part dotted-decimal notation. For IPv4, this is the same as `toString()`. ### Method Instance method. ### Response - Returns the IP address as a dotted-decimal string. ### Request Example ```js ipaddr.parse('192.168.1.1').toNormalizedString(); ``` ### Response Example ``` '192.168.1.1' ``` ``` -------------------------------- ### Match IPv4 Address against Subnet Ranges Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Instance-method shorthand for ipaddr.subnetMatch. Matches an address against a list of named ranges. ```javascript const addr = ipaddr.parse('192.168.1.1'); addr.subnetMatch({ private: [ipaddr.parse('192.168.0.0'), 16] }); // => 'private' ``` -------------------------------- ### Convert IPv4 Address to Byte Array Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv4 address as an array of four bytes in network byte order. ```javascript ipaddr.parse('192.168.1.1').toByteArray(); // => [192, 168, 1, 1] ipaddr.parse('127.0.0.1').toByteArray(); // => [127, 0, 0, 1] ``` -------------------------------- ### addr.octets Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Provides the four octets of an IP address as an array of numbers. ```APIDOC ## addr.octets ### Description The four octets of the address as an array of numbers. ### Method Instance property. ### Response - **octets** (Array) - An array containing the four octets of the IP address. ### Request Example ```js ipaddr.parse('192.168.1.1').octets; ``` ### Response Example ``` [192, 168, 1, 1] ``` ``` -------------------------------- ### addr.prefixLengthFromSubnetMask() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Calculates the CIDR prefix length if the IPv6 address represents a valid, contiguous subnet mask. Returns `null` otherwise. ```APIDOC ## addr.prefixLengthFromSubnetMask() ### Description Returns the CIDR prefix length if this address is a valid contiguous subnet mask, or `null` otherwise. ### Example ```js ipaddr.parse('ffff:ffff:ffff:ffff::').prefixLengthFromSubnetMask(); // => 64 ipaddr.parse('ffff:ffff::').prefixLengthFromSubnetMask(); // => 32 ``` ``` -------------------------------- ### Access IPv6 address parts Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Retrieves the eight 16-bit groups of an IPv6 address as an array of numbers. ```javascript ipaddr.parse('2001:db8:10::1234:dead').parts; // => [0x2001, 0x0db8, 0x0010, 0, 0, 0, 0x1234, 0xdead] ``` -------------------------------- ### addr.toByteArray Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Converts an IP address to an array of four bytes in network byte order. ```APIDOC ## addr.toByteArray() ### Description Returns the address as an array of four bytes in network byte order. ### Method Instance method. ### Response - Returns an array of four numbers representing the bytes of the IP address. ### Request Example ```js ipaddr.parse('192.168.1.1').toByteArray(); ``` ### Response Example ``` [192, 168, 1, 1] ``` ``` -------------------------------- ### Format IPv6 Address according to RFC 5952 Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use `toRFC5952String()` to format an IPv6 address according to RFC 5952, which includes lowercase hex, omitted leading zeros, and the longest run of zero groups replaced by `::`. ```javascript ipaddr.parse('2001:0db8:0000:0000:0000:0000:0000:0001').toRFC5952String(); // => '2001:db8::1' ``` -------------------------------- ### addr.toRFC5952String() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the address in the canonical format defined by [RFC 5952]: lowercase hex, leading zeros omitted, and the longest run of consecutive all-zero groups replaced by `::`. ```APIDOC ## addr.toRFC5952String() ### Description Returns the address in the canonical format defined by [RFC 5952]: lowercase hex, leading zeros omitted, and the longest run of consecutive all-zero groups replaced by `::`. ### Method N/A (Method on an address object) ### Endpoint N/A ### Parameters None ### Request Example ```js ipaddr.parse('2001:0db8:0000:0000:0000:0000:0000:0001').toRFC5952String(); ``` ### Response #### Success Response - **string**: The address in RFC 5952 canonical format. #### Response Example ``` '2001:db8::1' ``` ``` -------------------------------- ### Parse an IPv6 CIDR string Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parses an IPv6 CIDR string and returns an array containing the `IPv6` object and the prefix length. Throws an error for invalid input. ```javascript const [addr, prefix] = ipaddr.IPv6.parseCIDR('2001:db8::/32'); addr.toString(); // => '2001:db8::' prefix; // => 32 ``` -------------------------------- ### addr.match Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Checks if an IP address falls within a specified CIDR range. It can take either two arguments (an address and CIDR bits) or a single tuple containing both. ```APIDOC ## addr.match(other, cidrBits) / addr.match([address, cidrBits]) ### Description Returns `true` if the address falls within the given CIDR range. ### Method Instance method. ### Parameters #### Path Parameters - **other** (ipaddr.Address) - Required - The IP address object to compare against. - **cidrBits** (number) - Required - The number of bits in the CIDR range. OR - **[address, cidrBits]** (Array) - Required - A tuple containing the IP address object and the CIDR bits. ### Response - Returns `true` if the address is within the CIDR range, `false` otherwise. ### Request Example ```js const addr = ipaddr.parse('192.168.1.42'); addr.match(ipaddr.parse('192.168.1.0'), 24); addr.match(ipaddr.parseCIDR('192.168.1.0/24')); ``` ### Response Example ``` true ``` ``` -------------------------------- ### ipaddr.fromByteArray(bytes) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Constructs an IPv4 or IPv6 address object from a byte array. ```APIDOC ## `ipaddr.fromByteArray(bytes)` ### Description Constructs an IPv4 or IPv6 address object from a byte array, assuming network byte order (Most Significant Byte first). This function accepts 4 bytes for an IPv4 address or 16 bytes for an IPv6 address. It throws an error if the array length is not exactly 4 or 16. ### Parameters * **bytes** (Array) - An array of numbers representing the bytes of the IP address. ### Returns An IP address object (IPv4 or IPv6). ### Throws An error if the input array length is not 4 or 16. ### Example ```js ipaddr.fromByteArray([127, 0, 0, 1]).toString(); // => '127.0.0.1' ipaddr.fromByteArray([ 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, ]).toString(); // => '2001:db8::1' ``` ``` -------------------------------- ### Validate IPv6 Address String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns true if the string is a valid IPv6 address; false otherwise. Never throws. Accepts addresses with a zone ID. ```javascript ipaddr.IPv6.isValid('2001:db8::1'); // => true ipaddr.IPv6.isValid('::1'); // => true ipaddr.IPv6.isValid('fe80::1%eth0'); // => true ipaddr.IPv6.isValid('192.168.1.1'); // => false ``` -------------------------------- ### ipaddr.parseCIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parses an IP address string with a CIDR prefix length, returning the address and prefix length. Throws for invalid input. ```APIDOC ## ipaddr.parseCIDR(string) ### Description Parses an IP address with a CIDR prefix length and returns a two-element array `[address, prefixLength]`. Throws if the input is not valid CIDR notation. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```js const [addr, prefix] = ipaddr.parseCIDR('192.168.1.0/24'); ``` ### Response #### Success Response (200) - **array**: A two-element array where the first element is the address object and the second is the prefix length (number). ### Response Example ```json [ { "toString": "192.168.1.0" }, 24 ] ``` ``` -------------------------------- ### addr.range() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Identifies and returns the name of the built-in IP address range the address belongs to. Defaults to 'unicast' if no specific range is matched. ```APIDOC ## addr.range() ### Description Returns the name of the built-in range the address belongs to, or `'unicast'` if it does not match any. Recognised names include: `'unspecified'`, `'linkLocal'`, `'multicast'`, `'loopback'`, `'uniqueLocal'`, `'ipv4Mapped'`, `'reserved'`, and others. See the [source][IPv6 ranges] for the full list. ### Example ```js ipaddr.parse('::1').range(); // => 'loopback' ipaddr.parse('fe80::1').range(); // => 'linkLocal' ipaddr.parse('fc00::1').range(); // => 'uniqueLocal' ipaddr.parse('2001:db8::1').range(); // => 'reserved' ipaddr.parse('2607:f8b0::1').range(); // => 'unicast' ``` ``` -------------------------------- ### ipaddr.subnetMatch(address, rangeList[, defaultName]) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Matches an IP address against a list of named CIDR ranges and returns the name of the first matching range. ```APIDOC ## `ipaddr.subnetMatch(address, rangeList[, defaultName])` ### Description Matches the given `address` against a map of named CIDR ranges (`rangeList`) and returns the name of the first matching range. If no range matches, it returns `defaultName`, which defaults to `'unicast'`. Each value in `rangeList` can be a single `[address, prefixLength]` pair or an array of such pairs. The list can contain both IPv4 and IPv6 entries; entries of the incorrect address family are safely ignored. ### Parameters * **address** (IPAddress object) - The IP address to match. * **rangeList** (object) - An object where keys are range names and values are CIDR range definitions. * **defaultName** (string, optional) - The name to return if no range matches. Defaults to `'unicast'`. ### Returns The name of the first matching range, or `defaultName` if no match is found. ### Example ```js const rangeList = { private: [ [ipaddr.parse('10.0.0.0'), 8], [ipaddr.parse('172.16.0.0'), 12], [ipaddr.parse('192.168.0.0'), 16], ], loopback: [ ipaddr.parse('127.0.0.0'), 8 ] }; ipaddr.subnetMatch(ipaddr.parse('192.168.1.1'), rangeList); // => 'private' ipaddr.subnetMatch(ipaddr.parse('127.0.0.1'), rangeList); // => 'loopback' ipaddr.subnetMatch(ipaddr.parse('8.8.8.8'), rangeList, 'public'); // => 'public' ipaddr.subnetMatch(ipaddr.parse('8.8.8.8'), rangeList); // => 'unicast' ``` ``` -------------------------------- ### Match Address Against CIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Check if an IP address matches a given CIDR range. The CIDR range should be obtained using `ipaddr.parseCIDR()`. ```javascript const addr = ipaddr.parse('192.168.1.42'); addr.match(ipaddr.parseCIDR('192.168.1.0/24')); // => true ``` -------------------------------- ### Validate IPv6 CIDR String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns true if the string is a valid IPv6 CIDR address; false otherwise. Never throws. ```javascript ipaddr.IPv6.isValidCIDR('2001:db8::/32'); // => true ipaddr.IPv6.isValidCIDR('2001:db8::/129'); // => false ``` -------------------------------- ### addr.toNormalizedString() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the address with all eight 16-bit groups written out in lowercase hex, separated by colons, without `::` compression. ```APIDOC ## addr.toNormalizedString() ### Description Returns the address with all eight 16-bit groups written out in lowercase hex, separated by colons, without `::` compression. ### Method N/A (Method on an address object) ### Endpoint N/A ### Parameters None ### Request Example ```js ipaddr.parse('2001:db8::1').toNormalizedString(); ``` ### Response #### Success Response - **string**: The address in normalized string format. #### Response Example ``` '2001:db8:0:0:0:0:0:1' ``` ``` -------------------------------- ### ipaddr.IPv6.subnetMaskFromPrefixLength Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Generates the IPv6 subnet mask corresponding to a specified CIDR prefix length. ```APIDOC ## ipaddr.IPv6.subnetMaskFromPrefixLength(prefix) ### Description Returns the IPv6 subnet mask corresponding to the given CIDR prefix length. ### Parameters #### Path Parameters - **prefix** (number) - Required - The CIDR prefix length. ### Request Example ```js ipaddr.IPv6.subnetMaskFromPrefixLength(64).toString(); // => 'ffff:ffff:ffff:ffff::' ipaddr.IPv6.subnetMaskFromPrefixLength(48).toString(); // => 'ffff:ffff:ffff::' ``` ``` -------------------------------- ### ipaddr.process(string) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Processes an IP address string, converting IPv4-mapped IPv6 addresses to their IPv4 equivalents. Useful for handling dual-stack socket connections. ```APIDOC ## `ipaddr.process(string)` ### Description Processes an IP address string. It automatically converts IPv4-mapped IPv6 addresses (e.g., `::ffff:192.168.1.1`) to their IPv4 equivalents. Other addresses are returned as-is. This is particularly useful when accepting connections on a dual-stack IPv6 socket where IPv4 client addresses appear as IPv4-mapped IPv6 addresses. ### Parameters * **string** (string) - The IP address string to process. ### Returns An IP address object representing the processed address. ### Example ```js ipaddr.process('::ffff:192.168.1.1').toString(); // => '192.168.1.1' ipaddr.process('::ffff:192.168.1.1').kind(); // => 'ipv4' ipaddr.process('2001:db8::1').kind(); // => 'ipv6' ipaddr.process('192.168.1.1').kind(); // => 'ipv4' ``` ``` -------------------------------- ### addr.toIPv4MappedAddress Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Converts an IPv4 address to its IPv4-mapped IPv6 representation (::ffff:x.x.x.x). ```APIDOC ## addr.toIPv4MappedAddress() ### Description Returns the IPv4-mapped IPv6 representation of this address (`::ffff:x.x.x.x`). ### Method Instance method. ### Response - Returns an IPv6 address object representing the IPv4-mapped address. ### Request Example ```js ipaddr.parse('192.168.1.1').toIPv4MappedAddress().toString(); ``` ### Response Example ``` '::ffff:c0a8:101' ``` ``` -------------------------------- ### ipaddr.parse Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parses a string into an IPv4 or IPv6 address object. Throws an error for invalid input. ```APIDOC ## ipaddr.parse(string) ### Description Parses the string and returns an `IPv4` or `IPv6` object. Throws if the string is not a valid address. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```js const v4 = ipaddr.parse('192.168.1.1'); ``` ### Response #### Success Response (200) - **object**: An `IPv4` or `IPv6` object representing the parsed address. ### Response Example ```json { "kind": "ipv4", "toString": "192.168.1.1" } ``` ``` -------------------------------- ### ipaddr.IPv4.parseCIDR(string) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parses an IPv4 CIDR address string and returns the address and prefix length. ```APIDOC ## `ipaddr.IPv4.parseCIDR(string)` ### Description Parses an IPv4 CIDR (Classless Inter-Domain Routing) address string and returns an array containing the `IPv4` address object and the prefix length. This method throws an error if the input string is invalid. ### Parameters * **string** (string) - The IPv4 CIDR address string to parse (e.g., '192.168.1.0/24'). ### Returns An array `[addr, prefix]` where `addr` is an `IPv4` object and `prefix` is the prefix length (number). ### Throws An error if the input string is not a valid IPv4 CIDR address. ### Example ```js const [addr, prefix] = ipaddr.IPv4.parseCIDR('192.168.1.0/24'); addr.toString(); // => '192.168.1.0' prefix; // => 24 ``` ``` -------------------------------- ### addr.subnetMatch Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Performs a subnet match against a list of ranges. This is a shorthand for the static `ipaddr.subnetMatch` method. ```APIDOC ## addr.subnetMatch(rangeList[, defaultName]) ### Description Instance-method shorthand for `ipaddr.subnetMatch(addr, rangeList, defaultName)`. ### Method Instance method. ### Parameters #### Path Parameters - **rangeList** (Object) - Required - An object where keys are range names and values are tuples of [address, cidrBits]. - **defaultName** (string) - Optional - A default name to return if no match is found. ### Response - Returns the name of the matched subnet range, or `defaultName` if provided and no match is found. ### Request Example ```js const addr = ipaddr.parse('192.168.1.1'); addr.subnetMatch({ private: [ipaddr.parse('192.168.0.0'), 16] }); ``` ### Response Example ``` 'private' ``` ``` -------------------------------- ### Determine IPv6 address range Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the name of the predefined range an IPv6 address belongs to (e.g., 'linkLocal', 'multicast'). Defaults to 'unicast' if no specific range matches. ```javascript ipaddr.parse('::1').range(); // => 'loopback' ipaddr.parse('fe80::1').range(); // => 'linkLocal' ipaddr.parse('fc00::1').range(); // => 'uniqueLocal' ipaddr.parse('2001:db8::1').range(); // => 'reserved' ipaddr.parse('2607:f8b0::1').range(); // => 'unicast' ``` -------------------------------- ### Parse an IPv6 address string Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parses an IPv6 address string into an `IPv6` object. Throws an error if the input is not a valid IPv6 address. ```javascript ipaddr.IPv6.parse('2001:db8::1').toString(); // => '2001:db8::1' ipaddr.IPv6.parse('::ffff:192.168.1.1').toString(); // => '::ffff:c0a8:101' ``` -------------------------------- ### Process IP Addresses, Converting IPv4-mapped IPv6 Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use ipaddr.process to convert IPv4-mapped IPv6 addresses to their IPv4 equivalents. Other addresses are returned as is. This is useful for dual-stack sockets. ```javascript ipaddr.process('::ffff:192.168.1.1').toString(); // => '192.168.1.1' ipaddr.process('::ffff:192.168.1.1').kind(); // => 'ipv4' ipaddr.process('2001:db8::1').kind(); // => 'ipv6' ipaddr.process('192.168.1.1').kind(); // => 'ipv4' ``` -------------------------------- ### ipaddr.IPv4.isIPv4(string) Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Performs a lightweight regex check to determine if a string matches the basic IPv4 address pattern. ```APIDOC ## `ipaddr.IPv4.isIPv4(string)` ### Description Returns `true` if the input string matches the general pattern of an IPv4 address. This is a quick, regex-based check and does not validate if all octets are within the valid range of 0–255. For authoritative validation, it is recommended to use `isValid()`. ### Parameters * **string** (string) - The string to check. ### Returns `true` if the string matches the IPv4 pattern, `false` otherwise. ### Example ```js ipaddr.IPv4.isIPv4('192.168.1.1'); // => true ipaddr.IPv4.isIPv4('2001:db8::1'); // => false ``` ``` -------------------------------- ### Check if a string is an IPv6 address Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use `isIPv6` for a quick regex-based check. It does not perform full validation; use `isValid()` for that. ```javascript ipaddr.IPv6.isIPv6('2001:db8::1'); // => true ipaddr.IPv6.isIPv6('192.168.1.1'); // => false ``` -------------------------------- ### Parse IP Address Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parse an IP address string into an IPv4 or IPv6 object. Throws if the string is invalid. ```javascript const v4 = ipaddr.parse('192.168.1.1'); v4.kind(); // => 'ipv4' v4.toString(); // => '192.168.1.1' const v6 = ipaddr.parse('2001:db8::1'); v6.kind(); // => 'ipv6' v6.toString(); // => '2001:db8::1' ``` -------------------------------- ### Match IP Address Against Named CIDR Ranges Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use ipaddr.subnetMatch to find the name of the first matching CIDR range for a given address. A default name can be provided if no range matches. The rangeList can contain both IPv4 and IPv6 entries. ```javascript const rangeList = { private: [ [ipaddr.parse('10.0.0.0'), 8], [ipaddr.parse('172.16.0.0'), 12], [ipaddr.parse('192.168.0.0'), 16], ], loopback: [ ipaddr.parse('127.0.0.0'), 8 ] }; ipaddr.subnetMatch(ipaddr.parse('192.168.1.1'), rangeList); // => 'private' ipaddr.subnetMatch(ipaddr.parse('127.0.0.1'), rangeList); // => 'loopback' ipaddr.subnetMatch(ipaddr.parse('8.8.8.8'), rangeList, 'public'); // => 'public' ipaddr.subnetMatch(ipaddr.parse('8.8.8.8'), rangeList); // => 'unicast' ``` -------------------------------- ### addr.isIPv4MappedAddress() Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Determines if the IPv6 address is an IPv4-mapped IPv6 address, meaning it falls within the `::ffff:0:0/96` range. ```APIDOC ## addr.isIPv4MappedAddress() ### Description Returns `true` if this is an IPv4-mapped IPv6 address (i.e. in the `::ffff:0:0/96` range). ### Example ```js ipaddr.parse('::ffff:192.168.1.1').isIPv4MappedAddress(); // => true ipaddr.parse('2001:db8::1').isIPv4MappedAddress(); // => false ``` ``` -------------------------------- ### addr.zoneId Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Retrieves the zone ID string for link-local IPv6 addresses. Returns `undefined` if no zone ID is present. ```APIDOC ## addr.zoneId ### Description The zone ID string for link-local addresses, or `undefined` if absent. ### Example ```js ipaddr.parse('fe80::1%eth0').zoneId; // => 'eth0' ipaddr.parse('2001:db8::1').zoneId; // => undefined ``` ``` -------------------------------- ### ipaddr.IPv6.parseCIDR Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Parses an IPv6 CIDR notation string and returns an array containing the IPv6 address object and the prefix length. Throws an error if the input is invalid. ```APIDOC ## ipaddr.IPv6.parseCIDR(string) ### Description Parses an IPv6 CIDR address and returns `[IPv6, prefixLength]`. Throws if the input is invalid. ### Parameters #### Path Parameters - **string** (string) - Required - The IPv6 CIDR string to parse. ### Request Example ```js const [addr, prefix] = ipaddr.IPv6.parseCIDR('2001:db8::/32'); addr.toString(); // => '2001:db8::' prefix; // => 32 ``` ``` -------------------------------- ### Parse IPv4 Address String Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use ipaddr.IPv4.parse to convert a string into an IPv4 object. It accepts standard dotted-decimal as well as POSIX inet_aton formats like hex, octal, three-part, two-part, and single-value notations. Throws if the input is invalid. ```javascript ipaddr.IPv4.parse('192.168.1.1').toString(); // => '192.168.1.1' ipaddr.IPv4.parse('0xc0.168.1.1').toString(); // => '192.168.1.1' ``` -------------------------------- ### ipaddr.IPv4.subnetMaskFromPrefixLength Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Returns the IPv4 subnet mask corresponding to a given CIDR prefix length. It takes a prefix length as input and returns the subnet mask as a string. ```APIDOC ## ipaddr.IPv4.subnetMaskFromPrefixLength(prefix) ### Description Returns the IPv4 subnet mask corresponding to the given CIDR prefix length. ### Method Static method of `ipaddr.IPv4`. ### Parameters #### Path Parameters - **prefix** (number) - Required - The CIDR prefix length (e.g., 24). ### Response - Returns the IPv4 subnet mask as a string. ### Request Example ```js ipaddr.IPv4.subnetMaskFromPrefixLength(24).toString(); ``` ### Response Example ``` '255.255.255.0' ``` ``` -------------------------------- ### Validate CIDR Notation Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Check if a string is a valid IPv4 or IPv6 address in CIDR notation. This function never throws. ```javascript ipaddr.isValidCIDR('192.168.0.0/24'); // => true ipaddr.isValidCIDR('2001:db8::/32'); // => true ipaddr.isValidCIDR('192.168.0.1/33'); // => false ``` -------------------------------- ### Lightweight IPv4 Address Pattern Check Source: https://github.com/whitequark/ipaddr.js/blob/main/README.md Use ipaddr.IPv4.isIPv4 for a quick regex check if a string matches the IPv4 address pattern. This does not validate octet ranges; prefer isValid() for authoritative checks. ```javascript ipaddr.IPv4.isIPv4('192.168.1.1'); // => true ipaddr.IPv4.isIPv4('2001:db8::1'); // => false ```