0
# IP
1
2
IP is a comprehensive Node.js library providing utilities for IPv4 and IPv6 address manipulation, validation, conversion, and network operations. It supports address format conversion between strings, buffers, and long integers, subnet calculations, CIDR operations, network masking, and network interface queries.
3
4
## Package Information
5
6
- **Package Name**: ip
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install ip`
10
11
## Core Imports
12
13
```javascript
14
const ip = require('ip');
15
```
16
17
For ES modules:
18
19
```javascript
20
import * as ip from 'ip';
21
// or
22
import ip from 'ip';
23
```
24
25
## Basic Usage
26
27
```javascript
28
const ip = require('ip');
29
30
// Get local IP address
31
console.log(ip.address()); // Your local IP address
32
33
// Validate IP formats
34
console.log(ip.isV4Format('192.168.1.1')); // true
35
console.log(ip.isV6Format('::1')); // true
36
37
// Convert between formats
38
const buffer = ip.toBuffer('127.0.0.1');
39
console.log(ip.toString(buffer)); // '127.0.0.1'
40
41
// Subnet operations
42
const subnet = ip.subnet('192.168.1.134', '255.255.255.192');
43
console.log(subnet.networkAddress); // '192.168.1.128'
44
console.log(subnet.contains('192.168.1.150')); // true
45
46
// CIDR operations
47
console.log(ip.cidr('192.168.1.134/26')); // '192.168.1.128'
48
49
// Check private addresses
50
console.log(ip.isPrivate('192.168.1.1')); // true
51
console.log(ip.isPrivate('8.8.8.8')); // false
52
```
53
54
## Architecture
55
56
The IP library is built around several key functional areas:
57
58
- **Address Conversion**: Convert between string, buffer, and long integer representations
59
- **Address Validation**: Validate IP address formats and classify address types
60
- **Network Operations**: Perform subnet calculations, CIDR operations, and network masking
61
- **Bitwise Operations**: Apply bitwise NOT and OR operations to IP addresses
62
- **Network Interface Operations**: Query local network interfaces for IP addresses
63
64
## Capabilities
65
66
### Address Conversion
67
68
Core functions for converting IP addresses between different representations including strings, buffers, and long integers.
69
70
```javascript { .api }
71
function toBuffer(ip, buff, offset);
72
function toString(buff, offset, length);
73
function toLong(ip);
74
function fromLong(ipl);
75
function normalizeToLong(addr);
76
```
77
78
[Address Conversion](./address-conversion.md)
79
80
### Address Validation
81
82
Functions for validating IP address formats and classifying addresses as private, public, or loopback.
83
84
```javascript { .api }
85
function isV4Format(ip);
86
function isV6Format(ip);
87
function isEqual(a, b);
88
function isPrivate(addr);
89
function isPublic(addr);
90
function isLoopback(addr);
91
```
92
93
[Address Validation](./address-validation.md)
94
95
### Network Operations
96
97
Comprehensive subnet calculation, CIDR operations, and network masking functionality for network analysis.
98
99
```javascript { .api }
100
function fromPrefixLen(prefixlen, family);
101
function mask(addr, mask);
102
function cidr(cidrString);
103
function subnet(addr, mask);
104
function cidrSubnet(cidrString);
105
```
106
107
[Network Operations](./network-operations.md)
108
109
### Bitwise Operations
110
111
Bitwise manipulation functions for IP addresses including NOT and OR operations.
112
113
```javascript { .api }
114
function not(addr);
115
function or(a, b);
116
```
117
118
[Bitwise Operations](./bitwise-operations.md)
119
120
### Network Interface Operations
121
122
Functions for querying local network interfaces and retrieving IP addresses from the system.
123
124
```javascript { .api }
125
function address(name, family);
126
function loopback(family);
127
```
128
129
[Network Interface Operations](./network-interfaces.md)