node-ikev2 0.1.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CP_Attributes = exports.P_CSCF_IP6_ADDRESS = exports.P_CSCF_IP4_ADDRESS = exports.INTERNAL_IP6_SUBNET = exports.SUPPORTED_ATTRIBUTES = exports.INTERNAL_IP4_SUBNET = exports.INTERNAL_IP6_DHCP = exports.INTERNAL_IP6_DNS = exports.INTERNAL_IP6_ADDRESS = exports.APPLICATION_VERSION = exports.INTERNAL_IP4_DHCP = exports.INTERNAL_IP4_NBNS = exports.INTERNAL_IP4_DNS = exports.INTERNAL_IP4_NETMASK = exports.INTERNAL_IP4_ADDRESS = exports.ConfigurationAttribute = exports.configurationAttributeType = void 0;
4
- exports.parseIPv4AddressString = parseIPv4AddressString;
5
- exports.formatIPv4AddressBuffer = formatIPv4AddressBuffer;
6
- exports.parseIPv6AddressString = parseIPv6AddressString;
7
- exports.formatIPv6AddressBuffer = formatIPv6AddressBuffer;
3
+ exports.CPAttributes = exports.P_CSCF_IP6_ADDRESS = exports.P_CSCF_IP4_ADDRESS = exports.INTERNAL_IP6_SUBNET = exports.SUPPORTED_ATTRIBUTES = exports.INTERNAL_IP4_SUBNET = exports.INTERNAL_IP6_DHCP = exports.INTERNAL_IP6_DNS = exports.INTERNAL_IP6_ADDRESS = exports.APPLICATION_VERSION = exports.INTERNAL_IP4_DHCP = exports.INTERNAL_IP4_NBNS = exports.INTERNAL_IP4_DNS = exports.INTERNAL_IP4_NETMASK = exports.INTERNAL_IP4_ADDRESS = exports.ConfigurationAttribute = exports.configurationAttributeType = void 0;
4
+ const ip_address_1 = require("./ip-address");
8
5
  /**
9
6
  * Configuration Attribute Types
10
7
  *
@@ -182,7 +179,7 @@ class ConfigurationAttribute {
182
179
  static parseConfigurationAttributes(buffer) {
183
180
  const attributes = [];
184
181
  let offset = 0;
185
- while (offset < buffer.length) {
182
+ while (offset + 4 <= buffer.length) {
186
183
  // const attributeType = buffer.readUInt16BE(offset) & 0x7fff;
187
184
  const attributeLength = buffer.readUInt16BE(offset + 2);
188
185
  const attributeBuffer = buffer.subarray(offset, offset + 4 + attributeLength);
@@ -218,25 +215,28 @@ class ConfigurationAttribute {
218
215
  exports.ConfigurationAttribute = ConfigurationAttribute;
219
216
  class INTERNAL_IP4_ADDRESS extends ConfigurationAttribute {
220
217
  constructor(address) {
221
- if (address.length !== 4) {
218
+ if (address.length !== 0 && address.length !== 4) {
222
219
  throw new Error("Invalid value length for INTERNAL_IP4_ADDRESS");
223
220
  }
224
221
  super(configurationAttributeType.INTERNAL_IP4_ADDRESS, address);
225
222
  this.address = address;
226
223
  }
227
224
  static parse(buffer) {
228
- if (buffer.length !== 4) {
225
+ if (buffer.length !== 0 && buffer.length !== 4) {
229
226
  throw new Error("Invalid value length for INTERNAL_IP4_ADDRESS");
230
227
  }
228
+ if (buffer.length === 0) {
229
+ return new INTERNAL_IP4_ADDRESS(Buffer.alloc(0));
230
+ }
231
231
  return new INTERNAL_IP4_ADDRESS(buffer.subarray(0, 4));
232
232
  }
233
233
  static serializeJSON(json) {
234
- const address = parseIPv4AddressString(json.address);
234
+ const address = (0, ip_address_1.parseIPv4AddressString)(json.address);
235
235
  return new INTERNAL_IP4_ADDRESS(address).serialize();
236
236
  }
237
237
  toJSON() {
238
238
  return {
239
- address: formatIPv4AddressBuffer(this.address),
239
+ address: (0, ip_address_1.formatIPv4AddressBuffer)(this.address),
240
240
  };
241
241
  }
242
242
  toString() {
@@ -247,25 +247,28 @@ class INTERNAL_IP4_ADDRESS extends ConfigurationAttribute {
247
247
  exports.INTERNAL_IP4_ADDRESS = INTERNAL_IP4_ADDRESS;
248
248
  class INTERNAL_IP4_NETMASK extends ConfigurationAttribute {
249
249
  constructor(netmask) {
250
- if (netmask.length !== 4) {
250
+ if (netmask.length !== 0 && netmask.length !== 4) {
251
251
  throw new Error("Invalid value length for INTERNAL_IP4_NETMASK");
252
252
  }
253
253
  super(configurationAttributeType.INTERNAL_IP4_NETMASK, netmask);
254
254
  this.netmask = netmask;
255
255
  }
256
256
  static parse(buffer) {
257
- if (buffer.length !== 4) {
257
+ if (buffer.length !== 0 && buffer.length !== 4) {
258
258
  throw new Error("Invalid value length for INTERNAL_IP4_NETMASK");
259
259
  }
260
+ if (buffer.length === 0) {
261
+ return new INTERNAL_IP4_NETMASK(Buffer.alloc(0));
262
+ }
260
263
  return new INTERNAL_IP4_NETMASK(buffer.subarray(0, 4));
261
264
  }
262
265
  static serializeJSON(json) {
263
- const netmask = parseIPv4AddressString(json.netmask);
266
+ const netmask = (0, ip_address_1.parseIPv4AddressString)(json.netmask);
264
267
  return new INTERNAL_IP4_NETMASK(netmask).serialize();
265
268
  }
266
269
  toJSON() {
267
270
  return {
268
- netmask: formatIPv4AddressBuffer(this.netmask),
271
+ netmask: (0, ip_address_1.formatIPv4AddressBuffer)(this.netmask),
269
272
  };
270
273
  }
271
274
  toString() {
@@ -276,25 +279,28 @@ class INTERNAL_IP4_NETMASK extends ConfigurationAttribute {
276
279
  exports.INTERNAL_IP4_NETMASK = INTERNAL_IP4_NETMASK;
277
280
  class INTERNAL_IP4_DNS extends ConfigurationAttribute {
278
281
  constructor(dns) {
279
- if (dns.length !== 4) {
282
+ if (dns.length !== 0 && dns.length !== 4) {
280
283
  throw new Error("Invalid value length for INTERNAL_IP4_DNS");
281
284
  }
282
285
  super(configurationAttributeType.INTERNAL_IP4_DNS, dns);
283
286
  this.dns = dns;
284
287
  }
285
288
  static parse(buffer) {
286
- if (buffer.length !== 4) {
289
+ if (buffer.length !== 0 && buffer.length !== 4) {
287
290
  throw new Error("Invalid value length for INTERNAL_IP4_DNS");
288
291
  }
292
+ if (buffer.length === 0) {
293
+ return new INTERNAL_IP4_DNS(Buffer.alloc(0));
294
+ }
289
295
  return new INTERNAL_IP4_DNS(buffer.subarray(0, 4));
290
296
  }
291
297
  static serializeJSON(json) {
292
- const dns = parseIPv4AddressString(json.dns);
298
+ const dns = (0, ip_address_1.parseIPv4AddressString)(json.dns);
293
299
  return new INTERNAL_IP4_DNS(dns).serialize();
294
300
  }
295
301
  toJSON() {
296
302
  return {
297
- dns: formatIPv4AddressBuffer(this.dns),
303
+ dns: (0, ip_address_1.formatIPv4AddressBuffer)(this.dns),
298
304
  };
299
305
  }
300
306
  toString() {
@@ -305,25 +311,28 @@ class INTERNAL_IP4_DNS extends ConfigurationAttribute {
305
311
  exports.INTERNAL_IP4_DNS = INTERNAL_IP4_DNS;
306
312
  class INTERNAL_IP4_NBNS extends ConfigurationAttribute {
307
313
  constructor(nbns) {
308
- if (nbns.length !== 4) {
314
+ if (nbns.length !== 0 && nbns.length !== 4) {
309
315
  throw new Error("Invalid value length for INTERNAL_IP4_NBNS");
310
316
  }
311
317
  super(configurationAttributeType.INTERNAL_IP4_NBNS, nbns);
312
318
  this.nbns = nbns;
313
319
  }
314
320
  static parse(buffer) {
315
- if (buffer.length !== 4) {
321
+ if (buffer.length !== 0 && buffer.length !== 4) {
316
322
  throw new Error("Invalid value length for INTERNAL_IP4_NBNS");
317
323
  }
324
+ if (buffer.length === 0) {
325
+ return new INTERNAL_IP4_NBNS(Buffer.alloc(0));
326
+ }
318
327
  return new INTERNAL_IP4_NBNS(buffer.subarray(0, 4));
319
328
  }
320
329
  static serializeJSON(json) {
321
- const nbns = parseIPv4AddressString(json.nbns);
330
+ const nbns = (0, ip_address_1.parseIPv4AddressString)(json.nbns);
322
331
  return new INTERNAL_IP4_NBNS(nbns).serialize();
323
332
  }
324
333
  toJSON() {
325
334
  return {
326
- nbns: formatIPv4AddressBuffer(this.nbns),
335
+ nbns: (0, ip_address_1.formatIPv4AddressBuffer)(this.nbns),
327
336
  };
328
337
  }
329
338
  toString() {
@@ -334,25 +343,28 @@ class INTERNAL_IP4_NBNS extends ConfigurationAttribute {
334
343
  exports.INTERNAL_IP4_NBNS = INTERNAL_IP4_NBNS;
335
344
  class INTERNAL_IP4_DHCP extends ConfigurationAttribute {
336
345
  constructor(dhcp) {
337
- if (dhcp.length !== 4) {
346
+ if (dhcp.length !== 0 && dhcp.length !== 4) {
338
347
  throw new Error("Invalid value length for INTERNAL_IP4_DHCP");
339
348
  }
340
349
  super(configurationAttributeType.INTERNAL_IP4_DHCP, dhcp);
341
350
  this.dhcp = dhcp;
342
351
  }
343
352
  static parse(buffer) {
344
- if (buffer.length !== 4) {
353
+ if (buffer.length !== 0 && buffer.length !== 4) {
345
354
  throw new Error("Invalid value length for INTERNAL_IP4_DHCP");
346
355
  }
356
+ if (buffer.length === 0) {
357
+ return new INTERNAL_IP4_DHCP(Buffer.alloc(0));
358
+ }
347
359
  return new INTERNAL_IP4_DHCP(buffer.subarray(0, 4));
348
360
  }
349
361
  static serializeJSON(json) {
350
- const dhcp = parseIPv4AddressString(json.dhcp);
362
+ const dhcp = (0, ip_address_1.parseIPv4AddressString)(json.dhcp);
351
363
  return new INTERNAL_IP4_DHCP(dhcp).serialize();
352
364
  }
353
365
  toJSON() {
354
366
  return {
355
- dhcp: formatIPv4AddressBuffer(this.dhcp),
367
+ dhcp: (0, ip_address_1.formatIPv4AddressBuffer)(this.dhcp),
356
368
  };
357
369
  }
358
370
  toString() {
@@ -387,33 +399,42 @@ class APPLICATION_VERSION extends ConfigurationAttribute {
387
399
  exports.APPLICATION_VERSION = APPLICATION_VERSION;
388
400
  class INTERNAL_IP6_ADDRESS extends ConfigurationAttribute {
389
401
  constructor(address, prefixLength) {
390
- if (address.length !== 16) {
402
+ if (address.length !== 16 && address.length !== 0) {
391
403
  throw new Error("Invalid value length for INTERNAL_IP6_ADDRESS");
392
404
  }
393
- if (prefixLength < 0 || prefixLength > 128) {
394
- throw new Error("Invalid prefix length for INTERNAL_IP6_ADDRESS");
405
+ var value;
406
+ if (address.length === 0) {
407
+ value = address;
408
+ }
409
+ else {
410
+ if (prefixLength < 0 || prefixLength > 128) {
411
+ throw new Error("Invalid prefix length for INTERNAL_IP6_ADDRESS");
412
+ }
413
+ value = Buffer.alloc(17);
414
+ address.copy(value, 0);
415
+ value.writeUInt8(prefixLength, 16);
395
416
  }
396
- let value = Buffer.alloc(17);
397
- address.copy(value, 0);
398
- value.writeUInt8(prefixLength, 16);
399
417
  super(configurationAttributeType.INTERNAL_IP6_ADDRESS, value);
400
418
  this.address = address;
401
419
  this.prefixLength = prefixLength;
402
420
  }
403
421
  static parse(buffer) {
404
- if (buffer.length !== 17) {
422
+ if (buffer.length !== 17 && buffer.length !== 0) {
405
423
  throw new Error("Invalid value length for INTERNAL_IP6_ADDRESS");
406
424
  }
425
+ if (buffer.length === 0) {
426
+ return new INTERNAL_IP6_ADDRESS(Buffer.alloc(0), 0);
427
+ }
407
428
  return new INTERNAL_IP6_ADDRESS(buffer.subarray(0, 16), buffer.readUInt8(16));
408
429
  }
409
430
  static serializeJSON(json) {
410
- const address = parseIPv6AddressString(json.address);
431
+ const address = (0, ip_address_1.parseIPv6AddressString)(json.address);
411
432
  const prefixLength = json.prefixLength;
412
433
  return new INTERNAL_IP6_ADDRESS(address, prefixLength).serialize();
413
434
  }
414
435
  toJSON() {
415
436
  return {
416
- address: formatIPv6AddressBuffer(this.address),
437
+ address: (0, ip_address_1.formatIPv6AddressBuffer)(this.address),
417
438
  prefixLength: this.prefixLength,
418
439
  };
419
440
  }
@@ -425,25 +446,28 @@ class INTERNAL_IP6_ADDRESS extends ConfigurationAttribute {
425
446
  exports.INTERNAL_IP6_ADDRESS = INTERNAL_IP6_ADDRESS;
426
447
  class INTERNAL_IP6_DNS extends ConfigurationAttribute {
427
448
  constructor(dns) {
428
- if (dns.length !== 16) {
449
+ if (dns.length !== 16 && dns.length !== 0) {
429
450
  throw new Error("Invalid value length for INTERNAL_IP6_DNS");
430
451
  }
431
452
  super(configurationAttributeType.INTERNAL_IP6_DNS, dns);
432
453
  this.dns = dns;
433
454
  }
434
455
  static parse(buffer) {
435
- if (buffer.length !== 16) {
456
+ if (buffer.length !== 16 && buffer.length !== 0) {
436
457
  throw new Error("Invalid value length for INTERNAL_IP6_DNS");
437
458
  }
459
+ if (buffer.length === 0) {
460
+ return new INTERNAL_IP6_DNS(Buffer.alloc(0));
461
+ }
438
462
  return new INTERNAL_IP6_DNS(buffer.subarray(0, 16));
439
463
  }
440
464
  static serializeJSON(json) {
441
- const dns = parseIPv6AddressString(json.dns);
465
+ const dns = (0, ip_address_1.parseIPv6AddressString)(json.dns);
442
466
  return new INTERNAL_IP6_DNS(dns).serialize();
443
467
  }
444
468
  toJSON() {
445
469
  return {
446
- dns: formatIPv6AddressBuffer(this.dns),
470
+ dns: (0, ip_address_1.formatIPv6AddressBuffer)(this.dns),
447
471
  };
448
472
  }
449
473
  toString() {
@@ -454,25 +478,28 @@ class INTERNAL_IP6_DNS extends ConfigurationAttribute {
454
478
  exports.INTERNAL_IP6_DNS = INTERNAL_IP6_DNS;
455
479
  class INTERNAL_IP6_DHCP extends ConfigurationAttribute {
456
480
  constructor(dhcp) {
457
- if (dhcp.length !== 16) {
481
+ if (dhcp.length !== 16 && dhcp.length !== 0) {
458
482
  throw new Error("Invalid value length for INTERNAL_IP6_DHCP");
459
483
  }
460
484
  super(configurationAttributeType.INTERNAL_IP6_DHCP, dhcp);
461
485
  this.dhcp = dhcp;
462
486
  }
463
487
  static parse(buffer) {
464
- if (buffer.length !== 16) {
488
+ if (buffer.length !== 16 && buffer.length !== 0) {
465
489
  throw new Error("Invalid value length for INTERNAL_IP6_DHCP");
466
490
  }
491
+ if (buffer.length === 0) {
492
+ return new INTERNAL_IP6_DHCP(Buffer.alloc(0));
493
+ }
467
494
  return new INTERNAL_IP6_DHCP(buffer.subarray(0, 16));
468
495
  }
469
496
  static serializeJSON(json) {
470
- const dhcp = parseIPv6AddressString(json.dhcp);
497
+ const dhcp = (0, ip_address_1.parseIPv6AddressString)(json.dhcp);
471
498
  return new INTERNAL_IP6_DHCP(dhcp).serialize();
472
499
  }
473
500
  toJSON() {
474
501
  return {
475
- dhcp: formatIPv6AddressBuffer(this.dhcp),
502
+ dhcp: (0, ip_address_1.formatIPv6AddressBuffer)(this.dhcp),
476
503
  };
477
504
  }
478
505
  toString() {
@@ -483,34 +510,35 @@ class INTERNAL_IP6_DHCP extends ConfigurationAttribute {
483
510
  exports.INTERNAL_IP6_DHCP = INTERNAL_IP6_DHCP;
484
511
  class INTERNAL_IP4_SUBNET extends ConfigurationAttribute {
485
512
  constructor(address, netmask) {
486
- if (address.length !== 4) {
513
+ if (!((address.length == 4 && netmask.length == 4) ||
514
+ (address.length == 0 && netmask.length == 0))) {
487
515
  throw new Error("Invalid value length for INTERNAL_IP4_SUBNET");
488
516
  }
489
- if (netmask.length !== 4) {
490
- throw new Error("Invalid value length for INTERNAL_IP4_SUBNET");
491
- }
492
- let value = Buffer.alloc(8);
517
+ let value = Buffer.alloc(address.length + netmask.length);
493
518
  address.copy(value, 0);
494
- netmask.copy(value, 4);
519
+ netmask.copy(value, address.length);
495
520
  super(configurationAttributeType.INTERNAL_IP4_SUBNET, value);
496
521
  this.address = address;
497
522
  this.netmask = netmask;
498
523
  }
499
524
  static parse(buffer) {
500
- if (buffer.length !== 8) {
525
+ if (buffer.length !== 8 && buffer.length != 0) {
501
526
  throw new Error("Invalid value length for INTERNAL_IP4_SUBNET");
502
527
  }
528
+ if (buffer.length === 0) {
529
+ return new INTERNAL_IP4_SUBNET(Buffer.alloc(0), Buffer.alloc(0));
530
+ }
503
531
  return new INTERNAL_IP4_SUBNET(buffer.subarray(0, 4), buffer.subarray(4, 8));
504
532
  }
505
533
  static serializeJSON(json) {
506
- const address = parseIPv4AddressString(json.address);
507
- const netmask = parseIPv4AddressString(json.netmask);
534
+ const address = (0, ip_address_1.parseIPv4AddressString)(json.address);
535
+ const netmask = (0, ip_address_1.parseIPv4AddressString)(json.netmask);
508
536
  return new INTERNAL_IP4_SUBNET(address, netmask).serialize();
509
537
  }
510
538
  toJSON() {
511
539
  return {
512
- address: formatIPv4AddressBuffer(this.address),
513
- netmask: formatIPv4AddressBuffer(this.netmask),
540
+ address: (0, ip_address_1.formatIPv4AddressBuffer)(this.address),
541
+ netmask: (0, ip_address_1.formatIPv4AddressBuffer)(this.netmask),
514
542
  };
515
543
  }
516
544
  toString() {
@@ -554,15 +582,21 @@ class SUPPORTED_ATTRIBUTES extends ConfigurationAttribute {
554
582
  exports.SUPPORTED_ATTRIBUTES = SUPPORTED_ATTRIBUTES;
555
583
  class INTERNAL_IP6_SUBNET extends ConfigurationAttribute {
556
584
  constructor(address, prefixLength) {
557
- if (address.length !== 16) {
585
+ if (address.length !== 16 && address.length !== 0) {
558
586
  throw new Error("Invalid value length for INTERNAL_IP6_SUBNET");
559
587
  }
560
- if (prefixLength < 0 || prefixLength > 128) {
561
- throw new Error("Invalid prefix length for INTERNAL_IP6_SUBNET");
588
+ var value;
589
+ if (address.length === 0) {
590
+ value = address;
591
+ }
592
+ else {
593
+ if (prefixLength < 0 || prefixLength > 128) {
594
+ throw new Error("Invalid prefix length for INTERNAL_IP6_SUBNET");
595
+ }
596
+ value = Buffer.alloc(17);
597
+ address.copy(value, 0);
598
+ value.writeUInt8(prefixLength, 16);
562
599
  }
563
- let value = Buffer.alloc(17);
564
- address.copy(value, 0);
565
- value.writeUInt8(prefixLength, 16);
566
600
  super(configurationAttributeType.INTERNAL_IP6_SUBNET, value);
567
601
  this.address = address;
568
602
  this.prefixLength = prefixLength;
@@ -574,13 +608,13 @@ class INTERNAL_IP6_SUBNET extends ConfigurationAttribute {
574
608
  return new INTERNAL_IP6_SUBNET(buffer.subarray(0, 16), buffer.readUInt8(16));
575
609
  }
576
610
  static serializeJSON(json) {
577
- const address = parseIPv6AddressString(json.address);
611
+ const address = (0, ip_address_1.parseIPv6AddressString)(json.address);
578
612
  const prefixLength = json.prefixLength;
579
613
  return new INTERNAL_IP6_SUBNET(address, prefixLength).serialize();
580
614
  }
581
615
  toJSON() {
582
616
  return {
583
- address: formatIPv6AddressBuffer(this.address),
617
+ address: (0, ip_address_1.formatIPv6AddressBuffer)(this.address),
584
618
  prefixLength: this.prefixLength,
585
619
  };
586
620
  }
@@ -592,25 +626,28 @@ class INTERNAL_IP6_SUBNET extends ConfigurationAttribute {
592
626
  exports.INTERNAL_IP6_SUBNET = INTERNAL_IP6_SUBNET;
593
627
  class P_CSCF_IP4_ADDRESS extends ConfigurationAttribute {
594
628
  constructor(address) {
595
- if (address.length !== 4) {
629
+ if (address.length !== 4 && address.length !== 0) {
596
630
  throw new Error("Invalid value length for P_CSCF_IP4_ADDRESS");
597
631
  }
598
632
  super(configurationAttributeType.P_CSCF_IP4_ADDRESS, address);
599
633
  this.address = address;
600
634
  }
601
635
  static parse(buffer) {
602
- if (buffer.length !== 4) {
636
+ if (buffer.length !== 4 && buffer.length !== 0) {
603
637
  throw new Error("Invalid value length for P_CSCF_IP4_ADDRESS");
604
638
  }
639
+ if (buffer.length === 0) {
640
+ return new P_CSCF_IP4_ADDRESS(Buffer.alloc(0));
641
+ }
605
642
  return new P_CSCF_IP4_ADDRESS(buffer.subarray(0, 4));
606
643
  }
607
644
  static serializeJSON(json) {
608
- const address = parseIPv4AddressString(json.address);
645
+ const address = (0, ip_address_1.parseIPv4AddressString)(json.address);
609
646
  return new P_CSCF_IP4_ADDRESS(address).serialize();
610
647
  }
611
648
  toJSON() {
612
649
  return {
613
- address: formatIPv4AddressBuffer(this.address),
650
+ address: (0, ip_address_1.formatIPv4AddressBuffer)(this.address),
614
651
  };
615
652
  }
616
653
  toString() {
@@ -621,25 +658,28 @@ class P_CSCF_IP4_ADDRESS extends ConfigurationAttribute {
621
658
  exports.P_CSCF_IP4_ADDRESS = P_CSCF_IP4_ADDRESS;
622
659
  class P_CSCF_IP6_ADDRESS extends ConfigurationAttribute {
623
660
  constructor(address) {
624
- if (address.length !== 16) {
661
+ if (address.length !== 16 && address.length !== 0) {
625
662
  throw new Error("Invalid value length for P_CSCF_IP6_ADDRESS");
626
663
  }
627
664
  super(configurationAttributeType.P_CSCF_IP6_ADDRESS, address);
628
665
  this.address = address;
629
666
  }
630
667
  static parse(buffer) {
631
- if (buffer.length !== 16) {
668
+ if (buffer.length !== 16 && buffer.length !== 0) {
632
669
  throw new Error("Invalid value length for P_CSCF_IP6_ADDRESS");
633
670
  }
671
+ if (buffer.length === 0) {
672
+ return new P_CSCF_IP6_ADDRESS(Buffer.alloc(0));
673
+ }
634
674
  return new P_CSCF_IP6_ADDRESS(buffer.subarray(0, 16));
635
675
  }
636
676
  static serializeJSON(json) {
637
- const address = parseIPv6AddressString(json.address);
677
+ const address = (0, ip_address_1.parseIPv6AddressString)(json.address);
638
678
  return new P_CSCF_IP6_ADDRESS(address).serialize();
639
679
  }
640
680
  toJSON() {
641
681
  return {
642
- address: formatIPv6AddressBuffer(this.address),
682
+ address: (0, ip_address_1.formatIPv6AddressBuffer)(this.address),
643
683
  };
644
684
  }
645
685
  toString() {
@@ -648,7 +688,7 @@ class P_CSCF_IP6_ADDRESS extends ConfigurationAttribute {
648
688
  }
649
689
  }
650
690
  exports.P_CSCF_IP6_ADDRESS = P_CSCF_IP6_ADDRESS;
651
- class CP_Attributes {
691
+ class CPAttributes {
652
692
  constructor() {
653
693
  this.INTERNAL_IP4_ADDRESS = [];
654
694
  this.INTERNAL_IP4_NETMASK = undefined;
@@ -666,57 +706,59 @@ class CP_Attributes {
666
706
  this.P_CSCF_IP6_ADDRESS = [];
667
707
  this.OtherAttributes = [];
668
708
  }
669
- parse(buffer) {
709
+ static parse(buffer) {
710
+ const cpAttributes = new CPAttributes();
670
711
  let attributes = ConfigurationAttribute.parseConfigurationAttributes(buffer);
671
712
  for (let attribute of attributes) {
672
713
  switch (attribute.type) {
673
714
  case configurationAttributeType.INTERNAL_IP4_ADDRESS:
674
- this.INTERNAL_IP4_ADDRESS.push(INTERNAL_IP4_ADDRESS.parse(attribute.value));
715
+ cpAttributes.INTERNAL_IP4_ADDRESS.push(INTERNAL_IP4_ADDRESS.parse(attribute.value));
675
716
  break;
676
717
  case configurationAttributeType.INTERNAL_IP4_NETMASK:
677
- this.INTERNAL_IP4_NETMASK = INTERNAL_IP4_NETMASK.parse(attribute.value);
718
+ cpAttributes.INTERNAL_IP4_NETMASK = INTERNAL_IP4_NETMASK.parse(attribute.value);
678
719
  break;
679
720
  case configurationAttributeType.INTERNAL_IP4_DNS:
680
- this.INTERNAL_IP4_DNS.push(INTERNAL_IP4_DNS.parse(attribute.value));
721
+ cpAttributes.INTERNAL_IP4_DNS.push(INTERNAL_IP4_DNS.parse(attribute.value));
681
722
  break;
682
723
  case configurationAttributeType.INTERNAL_IP4_NBNS:
683
- this.INTERNAL_IP4_NBNS.push(INTERNAL_IP4_NBNS.parse(attribute.value));
724
+ cpAttributes.INTERNAL_IP4_NBNS.push(INTERNAL_IP4_NBNS.parse(attribute.value));
684
725
  break;
685
726
  case configurationAttributeType.INTERNAL_IP4_DHCP:
686
- this.INTERNAL_IP4_DHCP.push(INTERNAL_IP4_DHCP.parse(attribute.value));
727
+ cpAttributes.INTERNAL_IP4_DHCP.push(INTERNAL_IP4_DHCP.parse(attribute.value));
687
728
  break;
688
729
  case configurationAttributeType.APPLICATION_VERSION:
689
- this.APPLICATION_VERSION = APPLICATION_VERSION.parse(attribute.value);
730
+ cpAttributes.APPLICATION_VERSION = APPLICATION_VERSION.parse(attribute.value);
690
731
  break;
691
732
  case configurationAttributeType.INTERNAL_IP6_ADDRESS:
692
- this.INTERNAL_IP6_ADDRESS.push(INTERNAL_IP6_ADDRESS.parse(attribute.value));
733
+ cpAttributes.INTERNAL_IP6_ADDRESS.push(INTERNAL_IP6_ADDRESS.parse(attribute.value));
693
734
  break;
694
735
  case configurationAttributeType.INTERNAL_IP6_DNS:
695
- this.INTERNAL_IP6_DNS.push(INTERNAL_IP6_DNS.parse(attribute.value));
736
+ cpAttributes.INTERNAL_IP6_DNS.push(INTERNAL_IP6_DNS.parse(attribute.value));
696
737
  break;
697
738
  case configurationAttributeType.INTERNAL_IP6_DHCP:
698
- this.INTERNAL_IP6_DHCP.push(INTERNAL_IP6_DHCP.parse(attribute.value));
739
+ cpAttributes.INTERNAL_IP6_DHCP.push(INTERNAL_IP6_DHCP.parse(attribute.value));
699
740
  break;
700
741
  case configurationAttributeType.INTERNAL_IP4_SUBNET:
701
- this.INTERNAL_IP4_SUBNET.push(INTERNAL_IP4_SUBNET.parse(attribute.value));
742
+ cpAttributes.INTERNAL_IP4_SUBNET.push(INTERNAL_IP4_SUBNET.parse(attribute.value));
702
743
  break;
703
744
  case configurationAttributeType.SUPPORTED_ATTRIBUTES:
704
- this.SUPPORTED_ATTRIBUTES = SUPPORTED_ATTRIBUTES.parse(attribute.value);
745
+ cpAttributes.SUPPORTED_ATTRIBUTES = SUPPORTED_ATTRIBUTES.parse(attribute.value);
705
746
  break;
706
747
  case configurationAttributeType.INTERNAL_IP6_SUBNET:
707
- this.INTERNAL_IP6_SUBNET.push(INTERNAL_IP6_SUBNET.parse(attribute.value));
748
+ cpAttributes.INTERNAL_IP6_SUBNET.push(INTERNAL_IP6_SUBNET.parse(attribute.value));
708
749
  break;
709
750
  case configurationAttributeType.P_CSCF_IP4_ADDRESS:
710
- this.P_CSCF_IP4_ADDRESS.push(P_CSCF_IP4_ADDRESS.parse(attribute.value));
751
+ cpAttributes.P_CSCF_IP4_ADDRESS.push(P_CSCF_IP4_ADDRESS.parse(attribute.value));
711
752
  break;
712
753
  case configurationAttributeType.P_CSCF_IP6_ADDRESS:
713
- this.P_CSCF_IP6_ADDRESS.push(P_CSCF_IP6_ADDRESS.parse(attribute.value));
754
+ cpAttributes.P_CSCF_IP6_ADDRESS.push(P_CSCF_IP6_ADDRESS.parse(attribute.value));
714
755
  break;
715
756
  default:
716
- this.OtherAttributes.push(attribute);
757
+ cpAttributes.OtherAttributes.push(attribute);
717
758
  break;
718
759
  }
719
760
  }
761
+ return cpAttributes;
720
762
  }
721
763
  /**
722
764
  * Serializes the configuration attributes to a buffer
@@ -806,119 +848,4 @@ class CP_Attributes {
806
848
  return JSON.stringify(prettyJson, null, 2);
807
849
  }
808
850
  }
809
- exports.CP_Attributes = CP_Attributes;
810
- function parseIPv4AddressString(addressString) {
811
- const buffer = Buffer.alloc(4);
812
- const stringParts = addressString.split('.');
813
- const parts = stringParts.map(p => {
814
- if (!/^\d+$/.test(p)) {
815
- throw new Error(`Invalid IPv4 address: part "${p}" contains non-digit characters.`);
816
- }
817
- const parsed = parseInt(p, 10);
818
- if (parsed < 0 || parsed > 255) {
819
- throw new Error(`Invalid IPv4 address: part "${p}" is not a valid octet.`);
820
- }
821
- return parsed;
822
- });
823
- if (parts.length !== 4) {
824
- throw new Error("Invalid IPv4 address");
825
- }
826
- buffer.writeUInt8(parts[0], 0);
827
- buffer.writeUInt8(parts[1], 1);
828
- buffer.writeUInt8(parts[2], 2);
829
- buffer.writeUInt8(parts[3], 3);
830
- return buffer;
831
- }
832
- function formatIPv4AddressBuffer(buffer) {
833
- if (buffer.length !== 4) {
834
- throw new Error("Invalid IPv4 address");
835
- }
836
- const parts = [
837
- buffer.readUInt8(0),
838
- buffer.readUInt8(1),
839
- buffer.readUInt8(2),
840
- buffer.readUInt8(3),
841
- ];
842
- return parts.join('.');
843
- }
844
- function parseIPv6AddressString(addressString) {
845
- const buffer = Buffer.alloc(16);
846
- let parts;
847
- let zeroFillCount = 0;
848
- let currentBufferIndex = 0;
849
- if (addressString.includes('::')) {
850
- const partsSplitted = addressString.split('::');
851
- if (partsSplitted.length > 2) {
852
- throw new Error("Invalid IPv6 address: multiple '::' sequences.");
853
- }
854
- const [beforeDoubleColon, afterDoubleColon] = partsSplitted;
855
- const beforeParts = beforeDoubleColon.split(':').filter(p => p !== '');
856
- const afterParts = afterDoubleColon.split(':').filter(p => p !== '');
857
- // Calculate how many zero groups are needed for '::'
858
- zeroFillCount = 8 - (beforeParts.length + afterParts.length);
859
- if (zeroFillCount < 0) {
860
- throw new Error("Invalid IPv6 address: too many parts for '::' expansion.");
861
- }
862
- parts = [...beforeParts];
863
- for (let i = 0; i < zeroFillCount; i++) {
864
- parts.push('0'); // Represent the zero groups
865
- }
866
- parts.push(...afterParts);
867
- }
868
- else {
869
- parts = addressString.split(':');
870
- }
871
- if (parts.length !== 8) {
872
- throw new Error(`Invalid IPv6 address: expected 8 parts, got ${parts.length}.`);
873
- }
874
- for (let i = 0; i < 8; i++) {
875
- const part = parts[i];
876
- if (part.length > 4) {
877
- throw new Error(`Invalid IPv6 address part length: ${part}`);
878
- }
879
- const value = parseInt(part, 16);
880
- if (isNaN(value) || value < 0 || value > 0xFFFF) {
881
- throw new Error(`Invalid IPv6 address part: ${part}`);
882
- }
883
- buffer.writeUInt16BE(value, currentBufferIndex);
884
- currentBufferIndex += 2;
885
- }
886
- return buffer;
887
- }
888
- function formatIPv6AddressBuffer(addressBuffer) {
889
- if (addressBuffer.length !== 16) {
890
- throw new Error("Invalid buffer length for IPv6 address; expected 16 bytes.");
891
- }
892
- const parts = Array.from({ length: 8 }, (_, i) => addressBuffer.readUInt16BE(i * 2).toString(16).replace(/^0+/, '') || '0');
893
- let maxZeroLength = 0;
894
- let maxZeroIndex = -1;
895
- let currentZeroLength = 0;
896
- let currentZeroIndex = -1;
897
- for (let i = 0; i < parts.length; i++) {
898
- if (parts[i] === '0') {
899
- if (currentZeroLength === 0) {
900
- currentZeroIndex = i;
901
- }
902
- currentZeroLength++;
903
- }
904
- else {
905
- if (currentZeroLength > maxZeroLength) {
906
- maxZeroLength = currentZeroLength;
907
- maxZeroIndex = currentZeroIndex;
908
- }
909
- currentZeroLength = 0;
910
- }
911
- }
912
- if (currentZeroLength > maxZeroLength) {
913
- maxZeroLength = currentZeroLength;
914
- maxZeroIndex = currentZeroIndex;
915
- }
916
- if (maxZeroLength > 1) {
917
- const before = parts.slice(0, maxZeroIndex).join(':');
918
- const after = parts.slice(maxZeroIndex + maxZeroLength).join(':');
919
- return `${before}::${after}`;
920
- }
921
- else {
922
- return parts.join(':');
923
- }
924
- }
851
+ exports.CPAttributes = CPAttributes;
@@ -5,5 +5,7 @@ export * from "./proposal";
5
5
  export * from "./selector";
6
6
  export * from "./transform";
7
7
  export * from "./message";
8
+ export * from "./configuration-attribute";
9
+ export * from "./ip-address";
8
10
  import * as ikev2 from "./message";
9
11
  export { ikev2 };
package/lib/src/index.js CHANGED
@@ -44,5 +44,7 @@ __exportStar(require("./proposal"), exports);
44
44
  __exportStar(require("./selector"), exports);
45
45
  __exportStar(require("./transform"), exports);
46
46
  __exportStar(require("./message"), exports);
47
+ __exportStar(require("./configuration-attribute"), exports);
48
+ __exportStar(require("./ip-address"), exports);
47
49
  const ikev2 = __importStar(require("./message"));
48
50
  exports.ikev2 = ikev2;