1
- const ip = exports ;
2
- const { Buffer } = require ( 'buffer' ) ;
3
- const os = require ( 'os' ) ;
1
+ var ip = exports ;
2
+ var { Buffer } = require ( 'buffer' ) ;
3
+ var os = require ( 'os' ) ;
4
4
5
5
ip . toBuffer = function ( ip , buff , offset ) {
6
6
offset = ~ ~ offset ;
7
7
8
- let result ;
8
+ var result ;
9
9
10
10
if ( this . isV4Format ( ip ) ) {
11
11
result = buff || new Buffer ( offset + 4 ) ;
12
12
ip . split ( / \. / g) . map ( ( byte ) => {
13
13
result [ offset ++ ] = parseInt ( byte , 10 ) & 0xff ;
14
14
} ) ;
15
15
} else if ( this . isV6Format ( ip ) ) {
16
- const sections = ip . split ( ':' , 8 ) ;
16
+ var sections = ip . split ( ':' , 8 ) ;
17
17
18
- let i ;
18
+ var i ;
19
19
for ( i = 0 ; i < sections . length ; i ++ ) {
20
- const isv4 = this . isV4Format ( sections [ i ] ) ;
21
- let v4Buffer ;
20
+ var isv4 = this . isV4Format ( sections [ i ] ) ;
21
+ var v4Buffer ;
22
22
23
23
if ( isv4 ) {
24
24
v4Buffer = this . toBuffer ( sections [ i ] ) ;
@@ -36,16 +36,16 @@ ip.toBuffer = function (ip, buff, offset) {
36
36
while ( sections . length < 8 ) sections . push ( '0' ) ;
37
37
} else if ( sections . length < 8 ) {
38
38
for ( i = 0 ; i < sections . length && sections [ i ] !== '' ; i ++ ) ;
39
- const argv = [ i , 1 ] ;
39
+ var argv = [ i , 1 ] ;
40
40
for ( i = 9 - sections . length ; i > 0 ; i -- ) {
41
41
argv . push ( '0' ) ;
42
42
}
43
- sections . splice ( ... argv ) ;
43
+ sections . splice . apply ( sections , argv ) ;
44
44
}
45
45
46
46
result = buff || new Buffer ( offset + 16 ) ;
47
47
for ( i = 0 ; i < sections . length ; i ++ ) {
48
- const word = parseInt ( sections [ i ] , 16 ) ;
48
+ var word = parseInt ( sections [ i ] , 16 ) ;
49
49
result [ offset ++ ] = ( word >> 8 ) & 0xff ;
50
50
result [ offset ++ ] = word & 0xff ;
51
51
}
@@ -62,16 +62,17 @@ ip.toString = function (buff, offset, length) {
62
62
offset = ~ ~ offset ;
63
63
length = length || ( buff . length - offset ) ;
64
64
65
- let result = [ ] ;
65
+ var result = [ ] ;
66
+ var i ;
66
67
if ( length === 4 ) {
67
68
// IPv4
68
- for ( let i = 0 ; i < length ; i ++ ) {
69
+ for ( i = 0 ; i < length ; i ++ ) {
69
70
result . push ( buff [ offset + i ] ) ;
70
71
}
71
72
result = result . join ( '.' ) ;
72
73
} else if ( length === 16 ) {
73
74
// IPv6
74
- for ( let i = 0 ; i < length ; i += 2 ) {
75
+ for ( i = 0 ; i < length ; i += 2 ) {
75
76
result . push ( buff . readUInt16BE ( offset + i ) . toString ( 16 ) ) ;
76
77
}
77
78
result = result . join ( ':' ) ;
@@ -82,8 +83,8 @@ ip.toString = function (buff, offset, length) {
82
83
return result ;
83
84
} ;
84
85
85
- const ipv4Regex = / ^ ( \d { 1 , 3 } \. ) { 3 , 3 } \d { 1 , 3 } $ / ;
86
- const ipv6Regex = / ^ ( : : ) ? ( ( ( \d { 1 , 3 } \. ) { 3 } ( \d { 1 , 3 } ) { 1 } ) ? ( [ 0 - 9 a - f ] ) { 0 , 4 } : { 0 , 2 } ) { 1 , 8 } ( : : ) ? $ / i;
86
+ var ipv4Regex = / ^ ( \d { 1 , 3 } \. ) { 3 , 3 } \d { 1 , 3 } $ / ;
87
+ var ipv6Regex = / ^ ( : : ) ? ( ( ( \d { 1 , 3 } \. ) { 3 } ( \d { 1 , 3 } ) { 1 } ) ? ( [ 0 - 9 a - f ] ) { 0 , 4 } : { 0 , 2 } ) { 1 , 8 } ( : : ) ? $ / i;
87
88
88
89
ip . isV4Format = function ( ip ) {
89
90
return ipv4Regex . test ( ip ) ;
@@ -110,14 +111,14 @@ ip.fromPrefixLen = function (prefixlen, family) {
110
111
family = _normalizeFamily ( family ) ;
111
112
}
112
113
113
- let len = 4 ;
114
+ var len = 4 ;
114
115
if ( family === 'ipv6' ) {
115
116
len = 16 ;
116
117
}
117
- const buff = new Buffer ( len ) ;
118
+ var buff = new Buffer ( len ) ;
118
119
119
- for ( let i = 0 , n = buff . length ; i < n ; ++ i ) {
120
- let bits = 8 ;
120
+ for ( var i = 0 , n = buff . length ; i < n ; ++ i ) {
121
+ var bits = 8 ;
121
122
if ( prefixlen < 8 ) {
122
123
bits = prefixlen ;
123
124
}
@@ -133,10 +134,10 @@ ip.mask = function (addr, mask) {
133
134
addr = ip . toBuffer ( addr ) ;
134
135
mask = ip . toBuffer ( mask ) ;
135
136
136
- const result = new Buffer ( Math . max ( addr . length , mask . length ) ) ;
137
+ var result = new Buffer ( Math . max ( addr . length , mask . length ) ) ;
137
138
138
139
// Same protocol - do bitwise and
139
- let i ;
140
+ var i ;
140
141
if ( addr . length === mask . length ) {
141
142
for ( i = 0 ; i < addr . length ; i ++ ) {
142
143
result [ i ] = addr [ i ] & mask [ i ] ;
@@ -169,38 +170,38 @@ ip.mask = function (addr, mask) {
169
170
} ;
170
171
171
172
ip . cidr = function ( cidrString ) {
172
- const cidrParts = cidrString . split ( '/' ) ;
173
+ var cidrParts = cidrString . split ( '/' ) ;
173
174
174
- const addr = cidrParts [ 0 ] ;
175
+ var addr = cidrParts [ 0 ] ;
175
176
if ( cidrParts . length !== 2 ) {
176
177
throw new Error ( `invalid CIDR subnet: ${ addr } ` ) ;
177
178
}
178
179
179
- const mask = ip . fromPrefixLen ( parseInt ( cidrParts [ 1 ] , 10 ) ) ;
180
+ var mask = ip . fromPrefixLen ( parseInt ( cidrParts [ 1 ] , 10 ) ) ;
180
181
181
182
return ip . mask ( addr , mask ) ;
182
183
} ;
183
184
184
185
ip . subnet = function ( addr , mask ) {
185
- const networkAddress = ip . toLong ( ip . mask ( addr , mask ) ) ;
186
+ var networkAddress = ip . toLong ( ip . mask ( addr , mask ) ) ;
186
187
187
188
// Calculate the mask's length.
188
- const maskBuffer = ip . toBuffer ( mask ) ;
189
- let maskLength = 0 ;
189
+ var maskBuffer = ip . toBuffer ( mask ) ;
190
+ var maskLength = 0 ;
190
191
191
- for ( let i = 0 ; i < maskBuffer . length ; i ++ ) {
192
+ for ( var i = 0 ; i < maskBuffer . length ; i ++ ) {
192
193
if ( maskBuffer [ i ] === 0xff ) {
193
194
maskLength += 8 ;
194
195
} else {
195
- let octet = maskBuffer [ i ] & 0xff ;
196
+ var octet = maskBuffer [ i ] & 0xff ;
196
197
while ( octet ) {
197
198
octet = ( octet << 1 ) & 0xff ;
198
199
maskLength ++ ;
199
200
}
200
201
}
201
202
}
202
203
203
- const numberOfAddresses = 2 ** ( 32 - maskLength ) ;
204
+ var numberOfAddresses = 2 ** ( 32 - maskLength ) ;
204
205
205
206
return {
206
207
networkAddress : ip . fromLong ( networkAddress ) ,
@@ -223,82 +224,86 @@ ip.subnet = function (addr, mask) {
223
224
} ;
224
225
225
226
ip . cidrSubnet = function ( cidrString ) {
226
- const cidrParts = cidrString . split ( '/' ) ;
227
+ var cidrParts = cidrString . split ( '/' ) ;
227
228
228
- const addr = cidrParts [ 0 ] ;
229
+ var addr = cidrParts [ 0 ] ;
229
230
if ( cidrParts . length !== 2 ) {
230
231
throw new Error ( `invalid CIDR subnet: ${ addr } ` ) ;
231
232
}
232
233
233
- const mask = ip . fromPrefixLen ( parseInt ( cidrParts [ 1 ] , 10 ) ) ;
234
+ var mask = ip . fromPrefixLen ( parseInt ( cidrParts [ 1 ] , 10 ) ) ;
234
235
235
236
return ip . subnet ( addr , mask ) ;
236
237
} ;
237
238
238
239
ip . not = function ( addr ) {
239
- const buff = ip . toBuffer ( addr ) ;
240
- for ( let i = 0 ; i < buff . length ; i ++ ) {
240
+ var buff = ip . toBuffer ( addr ) ;
241
+ for ( var i = 0 ; i < buff . length ; i ++ ) {
241
242
buff [ i ] = 0xff ^ buff [ i ] ;
242
243
}
243
244
return ip . toString ( buff ) ;
244
245
} ;
245
246
246
247
ip . or = function ( a , b ) {
248
+ var i ;
249
+
247
250
a = ip . toBuffer ( a ) ;
248
251
b = ip . toBuffer ( b ) ;
249
252
250
253
// same protocol
251
254
if ( a . length === b . length ) {
252
- for ( let i = 0 ; i < a . length ; ++ i ) {
255
+ for ( i = 0 ; i < a . length ; ++ i ) {
253
256
a [ i ] |= b [ i ] ;
254
257
}
255
258
return ip . toString ( a ) ;
256
259
257
260
// mixed protocols
258
261
}
259
- let buff = a ;
260
- let other = b ;
262
+ var buff = a ;
263
+ var other = b ;
261
264
if ( b . length > a . length ) {
262
265
buff = b ;
263
266
other = a ;
264
267
}
265
268
266
- const offset = buff . length - other . length ;
267
- for ( let i = offset ; i < buff . length ; ++ i ) {
269
+ var offset = buff . length - other . length ;
270
+ for ( i = offset ; i < buff . length ; ++ i ) {
268
271
buff [ i ] |= other [ i - offset ] ;
269
272
}
270
273
271
274
return ip . toString ( buff ) ;
272
275
} ;
273
276
274
277
ip . isEqual = function ( a , b ) {
278
+ var i ;
279
+
275
280
a = ip . toBuffer ( a ) ;
276
281
b = ip . toBuffer ( b ) ;
277
282
278
283
// Same protocol
279
284
if ( a . length === b . length ) {
280
- for ( let i = 0 ; i < a . length ; i ++ ) {
285
+ for ( i = 0 ; i < a . length ; i ++ ) {
281
286
if ( a [ i ] !== b [ i ] ) return false ;
282
287
}
283
288
return true ;
284
289
}
285
290
286
291
// Swap
287
292
if ( b . length === 4 ) {
288
- const t = b ;
293
+ var t = b ;
289
294
b = a ;
290
295
a = t ;
291
296
}
292
297
293
298
// a - IPv4, b - IPv6
294
- for ( let i = 0 ; i < 10 ; i ++ ) {
299
+ for ( i = 0 ; i < 10 ; i ++ ) {
295
300
if ( b [ i ] !== 0 ) return false ;
296
301
}
297
302
298
- const word = b . readUInt16BE ( 10 ) ;
303
+ var word = b . readUInt16BE ( 10 ) ;
299
304
if ( word !== 0 && word !== 0xffff ) return false ;
300
305
301
- for ( let i = 0 ; i < 4 ; i ++ ) {
306
+ for ( i = 0 ; i < 4 ; i ++ ) {
302
307
if ( a [ i ] !== b [ i + 12 ] ) return false ;
303
308
}
304
309
@@ -360,7 +365,7 @@ ip.loopback = function (family) {
360
365
// * undefined: First address with `ipv4` or loopback address `127.0.0.1`.
361
366
//
362
367
ip . address = function ( name , family ) {
363
- const interfaces = os . networkInterfaces ( ) ;
368
+ var interfaces = os . networkInterfaces ( ) ;
364
369
365
370
//
366
371
// Default to `ipv4`
@@ -372,8 +377,8 @@ ip.address = function (name, family) {
372
377
// return the address.
373
378
//
374
379
if ( name && name !== 'private' && name !== 'public' ) {
375
- const res = interfaces [ name ] . filter ( ( details ) => {
376
- const itemFamily = _normalizeFamily ( details . family ) ;
380
+ var res = interfaces [ name ] . filter ( ( details ) => {
381
+ var itemFamily = _normalizeFamily ( details . family ) ;
377
382
return itemFamily === family ;
378
383
} ) ;
379
384
if ( res . length === 0 ) {
@@ -382,12 +387,12 @@ ip.address = function (name, family) {
382
387
return res [ 0 ] . address ;
383
388
}
384
389
385
- const all = Object . keys ( interfaces ) . map ( ( nic ) => {
390
+ var all = Object . keys ( interfaces ) . map ( ( nic ) => {
386
391
//
387
392
// Note: name will only be `public` or `private`
388
393
// when this is called.
389
394
//
390
- const addresses = interfaces [ nic ] . filter ( ( details ) => {
395
+ var addresses = interfaces [ nic ] . filter ( ( details ) => {
391
396
details . family = _normalizeFamily ( details . family ) ;
392
397
if ( details . family !== family || ip . isLoopback ( details . address ) ) {
393
398
return false ;
@@ -406,7 +411,7 @@ ip.address = function (name, family) {
406
411
} ;
407
412
408
413
ip . toLong = function ( ip ) {
409
- let ipl = 0 ;
414
+ var ipl = 0 ;
410
415
ip . split ( '.' ) . forEach ( ( octet ) => {
411
416
ipl <<= 8 ;
412
417
ipl += parseInt ( octet ) ;
0 commit comments