0
# IPv4 Address Operations
1
2
Complete IPv4 address parsing, validation, and manipulation functionality including subnet operations, format conversions, and range calculations.
3
4
## Capabilities
5
6
### Address4 Class
7
8
The main class for handling IPv4 addresses with comprehensive parsing and validation.
9
10
```typescript { .api }
11
/**
12
* Represents an IPv4 address with optional subnet mask
13
* @param address - IPv4 address string (e.g., "192.168.1.1" or "192.168.1.1/24")
14
*/
15
class Address4 {
16
constructor(address: string);
17
18
// Properties
19
address: string; // Original address string
20
addressMinusSuffix?: string; // Address without subnet
21
groups: number; // Always 4 for IPv4
22
parsedAddress: string[]; // Array of address octets
23
parsedSubnet: string; // Subnet portion as string
24
subnet: string; // Subnet string (default "/32")
25
subnetMask: number; // Subnet mask length (0-32)
26
v4: boolean; // Always true for IPv4
27
}
28
```
29
30
### Static Creation Methods
31
32
Create Address4 instances from various formats and representations.
33
34
```typescript { .api }
35
/**
36
* Validate an IPv4 address string without creating an instance
37
* @param address - IPv4 address string to validate
38
* @returns true if valid, false otherwise
39
*/
40
static isValid(address: string): boolean;
41
42
/**
43
* Create Address4 from hexadecimal string
44
* @param hex - Hex string (e.g., "c0a80101" or "c0:a8:01:01")
45
* @returns Address4 instance
46
*/
47
static fromHex(hex: string): Address4;
48
49
/**
50
* Create Address4 from integer representation
51
* @param integer - 32-bit integer representation
52
* @returns Address4 instance
53
*/
54
static fromInteger(integer: number): Address4;
55
56
/**
57
* Create Address4 from reverse DNS (in-addr.arpa) format
58
* @param arpaFormAddress - Reverse DNS string (e.g., "1.1.168.192.in-addr.arpa.")
59
* @returns Address4 instance
60
*/
61
static fromArpa(arpaFormAddress: string): Address4;
62
63
/**
64
* Create Address4 from BigInt representation
65
* @param bigInt - BigInt representation of the address
66
* @returns Address4 instance
67
*/
68
static fromBigInt(bigInt: bigint): Address4;
69
```
70
71
**Usage Examples:**
72
73
```typescript
74
import { Address4 } from "ip-address";
75
76
// From string
77
const addr1 = new Address4("192.168.1.1/24");
78
79
// From hex
80
const addr2 = Address4.fromHex("c0a80101");
81
console.log(addr2.correctForm()); // "192.168.1.1"
82
83
// From integer
84
const addr3 = Address4.fromInteger(3232235777);
85
console.log(addr3.correctForm()); // "192.168.1.1"
86
87
// From reverse DNS
88
const addr4 = Address4.fromArpa("1.1.168.192.in-addr.arpa.");
89
console.log(addr4.correctForm()); // "192.168.1.1"
90
91
// Validation
92
console.log(Address4.isValid("192.168.1.1")); // true
93
console.log(Address4.isValid("256.1.1.1")); // false
94
```
95
96
### Form and Validation Methods
97
98
Methods for address validation and canonical representation.
99
100
```typescript { .api }
101
/**
102
* Parse an IPv4 address string into components
103
* @param address - IPv4 address string
104
* @returns Array of address octets as strings
105
*/
106
parse(address: string): string[];
107
108
/**
109
* Return the canonical form of the address
110
* @returns Canonical IPv4 address string
111
*/
112
correctForm(): string;
113
114
/**
115
* Check if the address is in correct/canonical form
116
* @returns true if address is in correct form
117
*/
118
isCorrect(): boolean;
119
120
/**
121
* Return reverse DNS (in-addr.arpa) form
122
* @param options - Options for reverse form generation
123
* @returns Reverse DNS string
124
*/
125
reverseForm(options?: ReverseFormOptions): string;
126
```
127
128
**Usage Examples:**
129
130
```typescript
131
const addr = new Address4("192.168.001.001/24");
132
133
console.log(addr.correctForm()); // "192.168.1.1"
134
console.log(addr.isCorrect()); // false (has leading zeros)
135
136
const correctAddr = new Address4("192.168.1.1");
137
console.log(correctAddr.isCorrect()); // true
138
139
// Reverse DNS
140
console.log(addr.reverseForm()); // "1.1.168.192.in-addr.arpa."
141
console.log(addr.reverseForm({ omitSuffix: true })); // "1.1.168.192"
142
```
143
144
### Conversion Methods
145
146
Convert IPv4 addresses to different representations and formats.
147
148
```typescript { .api }
149
/**
150
* Convert address to hexadecimal string
151
* @returns Colon-separated hex string (e.g., "c0:a8:01:01")
152
*/
153
toHex(): string;
154
155
/**
156
* Convert address to byte array
157
* @returns Array of 4 integers (0-255)
158
*/
159
toArray(): number[];
160
161
/**
162
* Convert address to IPv6 group format for embedding
163
* @returns IPv6-compatible group string
164
*/
165
toGroup6(): string;
166
167
/**
168
* Convert address to BigInt representation
169
* @returns BigInt value of the address
170
*/
171
bigInt(): bigint;
172
173
/**
174
* Convert address to zero-padded binary string
175
* @returns 32-character binary string
176
*/
177
binaryZeroPad(): string;
178
```
179
180
**Usage Examples:**
181
182
```typescript
183
const addr = new Address4("192.168.1.1");
184
185
console.log(addr.toHex()); // "c0:a8:01:01"
186
console.log(addr.toArray()); // [192, 168, 1, 1]
187
console.log(addr.toGroup6()); // "c0a8:0101"
188
console.log(addr.bigInt()); // 3232235777n
189
console.log(addr.binaryZeroPad()); // "11000000101010000000000100000001"
190
```
191
192
### Subnet and Range Methods
193
194
Advanced subnet operations for network analysis and range calculations.
195
196
```typescript { .api }
197
/**
198
* Get first n bits of address as binary string (defaults to subnet mask)
199
* @param mask - Number of bits to mask (optional)
200
* @returns Binary string of masked bits
201
*/
202
mask(mask?: number): string;
203
204
/**
205
* Get bits in specified range as binary string
206
* @param start - Start bit position
207
* @param end - End bit position
208
* @returns Binary string of specified bits
209
*/
210
getBitsBase2(start: number, end: number): string;
211
212
/**
213
* Get first address in subnet (Network Address)
214
* @returns Address4 instance of network address
215
*/
216
startAddress(): Address4;
217
218
/**
219
* Get first host address in subnet (after Network Address)
220
* @returns Address4 instance of first host
221
*/
222
startAddressExclusive(): Address4;
223
224
/**
225
* Get last address in subnet (Broadcast Address)
226
* @returns Address4 instance of broadcast address
227
*/
228
endAddress(): Address4;
229
230
/**
231
* Get last host address in subnet (before Broadcast Address)
232
* @returns Address4 instance of last host
233
*/
234
endAddressExclusive(): Address4;
235
236
/**
237
* Check if another address is within this address's subnet
238
* @param address - Address4 instance to test
239
* @returns true if address is in subnet
240
*/
241
isInSubnet(address: Address4): boolean;
242
```
243
244
**Usage Examples:**
245
246
```typescript
247
const network = new Address4("192.168.1.0/24");
248
249
console.log(network.startAddress().correctForm()); // "192.168.1.0"
250
console.log(network.startAddressExclusive().correctForm()); // "192.168.1.1"
251
console.log(network.endAddress().correctForm()); // "192.168.1.255"
252
console.log(network.endAddressExclusive().correctForm()); // "192.168.1.254"
253
254
const host = new Address4("192.168.1.100");
255
console.log(network.isInSubnet(host)); // true
256
257
console.log(network.mask()); // First 24 bits as binary
258
console.log(network.getBitsBase2(0, 8)); // First octet as binary
259
```
260
261
### Address Type Detection
262
263
Identify special IPv4 address types and properties.
264
265
```typescript { .api }
266
/**
267
* Check if address is a multicast address (224.0.0.0/4)
268
* @returns true if multicast address
269
*/
270
isMulticast(): boolean;
271
```
272
273
**Usage Examples:**
274
275
```typescript
276
const multicast = new Address4("224.1.1.1");
277
console.log(multicast.isMulticast()); // true
278
279
const unicast = new Address4("192.168.1.1");
280
console.log(unicast.isMulticast()); // false
281
```
282
283
### HTML Display Methods
284
285
Methods for generating HTML representations with styling classes.
286
287
```typescript { .api }
288
/**
289
* Generate HTML representation with styling classes for IPv6 display
290
* @returns HTML string with span elements and CSS classes
291
*/
292
groupForV6(): string;
293
```
294
295
**Usage Examples:**
296
297
```typescript
298
const addr = new Address4("192.168.1.1");
299
console.log(addr.groupForV6());
300
// '<span class="hover-group group-v4 group-6">192.168</span>.<span class="hover-group group-v4 group-7">1.1</span>'
301
```