node-ikev2 0.1.4 → 0.2.1

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.
@@ -263,7 +263,7 @@ export declare class P_CSCF_IP6_ADDRESS extends ConfigurationAttribute {
263
263
  toJSON(): Record<string, any>;
264
264
  toString(): string;
265
265
  }
266
- export declare class CP_Attributes {
266
+ export declare class CPAttributes {
267
267
  INTERNAL_IP4_ADDRESS: INTERNAL_IP4_ADDRESS[];
268
268
  INTERNAL_IP4_NETMASK: INTERNAL_IP4_NETMASK | undefined;
269
269
  INTERNAL_IP4_DNS: INTERNAL_IP4_DNS[];
@@ -280,7 +280,7 @@ export declare class CP_Attributes {
280
280
  P_CSCF_IP6_ADDRESS: P_CSCF_IP6_ADDRESS[];
281
281
  OtherAttributes: ConfigurationAttribute[];
282
282
  constructor();
283
- parse(buffer: Buffer): void;
283
+ static parse(buffer: Buffer): CPAttributes;
284
284
  /**
285
285
  * Serializes the configuration attributes to a buffer
286
286
  * @public
@@ -300,7 +300,3 @@ export declare class CP_Attributes {
300
300
  */
301
301
  toString(): string;
302
302
  }
303
- export declare function parseIPv4AddressString(addressString: string): Buffer;
304
- export declare function formatIPv4AddressBuffer(buffer: Buffer): string;
305
- export declare function parseIPv6AddressString(addressString: string): Buffer;
306
- export declare function formatIPv6AddressBuffer(addressBuffer: Buffer): string;
@@ -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;
@@ -0,0 +1,6 @@
1
+ export declare function parseIPv4AddressString(addressString: string): Buffer;
2
+ export declare function formatIPv4AddressBuffer(buffer: Buffer): string;
3
+ export declare function parseIPv6AddressString(addressString: string): Buffer;
4
+ export declare function formatIPv6AddressBuffer(addressBuffer: Buffer): string;
5
+ export declare function parseIPAddressString(addressString: string): Buffer;
6
+ export declare function formatIPAddressBuffer(addressBuffer: Buffer): string;
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseIPv4AddressString = parseIPv4AddressString;
4
+ exports.formatIPv4AddressBuffer = formatIPv4AddressBuffer;
5
+ exports.parseIPv6AddressString = parseIPv6AddressString;
6
+ exports.formatIPv6AddressBuffer = formatIPv6AddressBuffer;
7
+ exports.parseIPAddressString = parseIPAddressString;
8
+ exports.formatIPAddressBuffer = formatIPAddressBuffer;
9
+ function parseIPv4AddressString(addressString) {
10
+ if (addressString.length === 0) {
11
+ throw new Error(`Invalid IPv4 address ${addressString}`);
12
+ }
13
+ const buffer = Buffer.alloc(4);
14
+ const stringParts = addressString.split(".");
15
+ const parts = stringParts.map((p) => {
16
+ if (!/^\d+$/.test(p)) {
17
+ throw new Error(`Invalid IPv4 address ${addressString}: part "${p}" contains non-digit characters.`);
18
+ }
19
+ const parsed = parseInt(p, 10);
20
+ if (parsed < 0 || parsed > 255) {
21
+ throw new Error(`Invalid IPv4 address ${addressString}: part "${p}" is not a valid octet.`);
22
+ }
23
+ return parsed;
24
+ });
25
+ if (parts.length !== 4) {
26
+ throw new Error(`Invalid IPv4 address ${addressString}`);
27
+ }
28
+ buffer.writeUInt8(parts[0], 0);
29
+ buffer.writeUInt8(parts[1], 1);
30
+ buffer.writeUInt8(parts[2], 2);
31
+ buffer.writeUInt8(parts[3], 3);
32
+ return buffer;
33
+ }
34
+ function formatIPv4AddressBuffer(buffer) {
35
+ if (buffer.length === 0) {
36
+ throw new Error(`Invalid IPv4 address ${buffer}`);
37
+ }
38
+ if (buffer.length !== 4) {
39
+ throw new Error(`Invalid IPv4 address ${buffer}`);
40
+ }
41
+ const parts = [
42
+ buffer.readUInt8(0),
43
+ buffer.readUInt8(1),
44
+ buffer.readUInt8(2),
45
+ buffer.readUInt8(3),
46
+ ];
47
+ return parts.join(".");
48
+ }
49
+ function parseIPv6AddressString(addressString) {
50
+ if (addressString.length === 0) {
51
+ return Buffer.alloc(0);
52
+ }
53
+ const buffer = Buffer.alloc(16);
54
+ let parts;
55
+ let zeroFillCount = 0;
56
+ let currentBufferIndex = 0;
57
+ if (addressString.includes("::")) {
58
+ const partsSplitted = addressString.split("::");
59
+ if (partsSplitted.length > 2) {
60
+ throw new Error(`Invalid IPv6 address ${addressString}: multiple '::' sequences.`);
61
+ }
62
+ const [beforeDoubleColon, afterDoubleColon] = partsSplitted;
63
+ const beforeParts = beforeDoubleColon.split(":").filter((p) => p !== "");
64
+ const afterParts = afterDoubleColon.split(":").filter((p) => p !== "");
65
+ // Calculate how many zero groups are needed for '::'
66
+ zeroFillCount = 8 - (beforeParts.length + afterParts.length);
67
+ if (zeroFillCount < 0) {
68
+ throw new Error(`Invalid IPv6 address ${addressString}: too many parts for '::' expansion.`);
69
+ }
70
+ parts = [...beforeParts];
71
+ for (let i = 0; i < zeroFillCount; i++) {
72
+ parts.push("0"); // Represent the zero groups
73
+ }
74
+ parts.push(...afterParts);
75
+ }
76
+ else {
77
+ parts = addressString.split(":");
78
+ }
79
+ if (parts.length !== 8) {
80
+ throw new Error(`Invalid IPv6 address ${addressString}: expected 8 parts, got ${parts.length}.`);
81
+ }
82
+ for (let i = 0; i < 8; i++) {
83
+ const part = parts[i];
84
+ if (part.length > 4) {
85
+ throw new Error(`Invalid IPv6 address ${addressString}: part length: ${part}`);
86
+ }
87
+ const value = parseInt(part, 16);
88
+ if (isNaN(value) || value < 0 || value > 0xffff) {
89
+ throw new Error(`Invalid IPv6 address ${addressString}: part: ${part}`);
90
+ }
91
+ buffer.writeUInt16BE(value, currentBufferIndex);
92
+ currentBufferIndex += 2;
93
+ }
94
+ return buffer;
95
+ }
96
+ function formatIPv6AddressBuffer(addressBuffer) {
97
+ if (addressBuffer.length === 0) {
98
+ return "";
99
+ }
100
+ if (addressBuffer.length !== 16) {
101
+ throw new Error(`Invalid buffer length for IPv6 address ${addressBuffer}: expected 16 bytes.`);
102
+ }
103
+ const parts = Array.from({ length: 8 }, (_, i) => addressBuffer
104
+ .readUInt16BE(i * 2)
105
+ .toString(16)
106
+ .replace(/^0+/, "") || "0");
107
+ let maxZeroLength = 0;
108
+ let maxZeroIndex = -1;
109
+ let currentZeroLength = 0;
110
+ let currentZeroIndex = -1;
111
+ for (let i = 0; i < parts.length; i++) {
112
+ if (parts[i] === "0") {
113
+ if (currentZeroLength === 0) {
114
+ currentZeroIndex = i;
115
+ }
116
+ currentZeroLength++;
117
+ }
118
+ else {
119
+ if (currentZeroLength > maxZeroLength) {
120
+ maxZeroLength = currentZeroLength;
121
+ maxZeroIndex = currentZeroIndex;
122
+ }
123
+ currentZeroLength = 0;
124
+ }
125
+ }
126
+ if (currentZeroLength > maxZeroLength) {
127
+ maxZeroLength = currentZeroLength;
128
+ maxZeroIndex = currentZeroIndex;
129
+ }
130
+ if (maxZeroLength > 1) {
131
+ const before = parts.slice(0, maxZeroIndex).join(":");
132
+ const after = parts.slice(maxZeroIndex + maxZeroLength).join(":");
133
+ return `${before}::${after}`;
134
+ }
135
+ else {
136
+ return parts.join(":");
137
+ }
138
+ }
139
+ function parseIPAddressString(addressString) {
140
+ if (addressString.length === 0) {
141
+ return Buffer.alloc(0);
142
+ }
143
+ if (addressString.includes(":")) {
144
+ return parseIPv6AddressString(addressString);
145
+ }
146
+ return parseIPv4AddressString(addressString);
147
+ }
148
+ function formatIPAddressBuffer(addressBuffer) {
149
+ if (addressBuffer.length === 0) {
150
+ return "";
151
+ }
152
+ if (addressBuffer.length === 4) {
153
+ return formatIPv4AddressBuffer(addressBuffer);
154
+ }
155
+ if (addressBuffer.length === 16) {
156
+ return formatIPv6AddressBuffer(addressBuffer);
157
+ }
158
+ throw new Error(`Invalid buffer length for IP address ${addressBuffer}: expected 4 or 16 bytes.`);
159
+ }
@@ -732,11 +732,11 @@ export declare class PayloadVENDOR extends Payload {
732
732
  */
733
733
  export declare class PayloadTS extends Payload {
734
734
  nextPayload: payloadType;
735
- numTs: number;
735
+ tsType: payloadType;
736
736
  tsList: TrafficSelector[];
737
737
  critical: boolean;
738
738
  length: number;
739
- constructor(nextPayload: payloadType, numTs: number, tsList: TrafficSelector[], critical?: boolean, length?: number);
739
+ constructor(nextPayload: payloadType, tsType: payloadType, tsList: TrafficSelector[], critical?: boolean, length?: number);
740
740
  /**
741
741
  * Parses a Traffic Selector Payload from a buffer
742
742
  * @param buffer
@@ -779,11 +779,10 @@ export declare class PayloadTS extends Payload {
779
779
  */
780
780
  export declare class PayloadTSi extends PayloadTS {
781
781
  nextPayload: payloadType;
782
- numTs: number;
783
782
  tsList: TrafficSelector[];
784
783
  critical: boolean;
785
784
  length: number;
786
- constructor(nextPayload: payloadType, numTs: number, tsList: TrafficSelector[], critical?: boolean, length?: number);
785
+ constructor(nextPayload: payloadType, tsList: TrafficSelector[], critical?: boolean, length?: number);
787
786
  }
788
787
  /**
789
788
  * IKEv2 Traffic Selector - Responder Payload
@@ -792,11 +791,10 @@ export declare class PayloadTSi extends PayloadTS {
792
791
  */
793
792
  export declare class PayloadTSr extends PayloadTS {
794
793
  nextPayload: payloadType;
795
- numTs: number;
796
794
  tsList: TrafficSelector[];
797
795
  critical: boolean;
798
796
  length: number;
799
- constructor(nextPayload: payloadType, numTs: number, tsList: TrafficSelector[], critical?: boolean, length?: number);
797
+ constructor(nextPayload: payloadType, tsList: TrafficSelector[], critical?: boolean, length?: number);
800
798
  }
801
799
  /**
802
800
  * IKEv2 Encrypted and Authenticated Payload
@@ -1347,10 +1347,10 @@ exports.PayloadVENDOR = PayloadVENDOR;
1347
1347
  * @extends Payload
1348
1348
  */
1349
1349
  class PayloadTS extends Payload {
1350
- constructor(nextPayload, numTs, tsList, critical = false, length = 0) {
1351
- super(payloadType.NONE, nextPayload, critical, length > 0 ? length : 5 + tsList.reduce((acc, ts) => acc + ts.length, 0));
1350
+ constructor(nextPayload, tsType, tsList, critical = false, length = 0) {
1351
+ super(tsType, nextPayload, critical, length > 0 ? length : 0);
1352
1352
  this.nextPayload = nextPayload;
1353
- this.numTs = numTs;
1353
+ this.tsType = tsType;
1354
1354
  this.tsList = tsList;
1355
1355
  this.critical = critical;
1356
1356
  this.length = length;
@@ -1372,7 +1372,7 @@ class PayloadTS extends Payload {
1372
1372
  tsList.push(ts);
1373
1373
  offset += ts.length;
1374
1374
  }
1375
- return new PayloadTS(genericPayload.nextPayload, numTs, tsList, genericPayload.critical, genericPayload.length);
1375
+ return new PayloadTS(genericPayload.nextPayload, genericPayload.type, tsList, genericPayload.critical, genericPayload.length);
1376
1376
  }
1377
1377
  /**
1378
1378
  * Serializes a JSON representation of the TS payload to a buffer
@@ -1383,6 +1383,7 @@ class PayloadTS extends Payload {
1383
1383
  */
1384
1384
  static serializeJSON(json) {
1385
1385
  var _a;
1386
+ // const type = json.type; // TODO include the type in the JSON, as it is discriminating
1386
1387
  const buffer = Buffer.alloc(json.length);
1387
1388
  const genericPayload = Payload.serializeJSON(json);
1388
1389
  genericPayload.copy(buffer);
@@ -1404,13 +1405,16 @@ class PayloadTS extends Payload {
1404
1405
  */
1405
1406
  serialize() {
1406
1407
  // Encode deep first to calculate length
1408
+ if (this.tsList.length > 255) {
1409
+ throw new Error(`Too many traffic selectors ${this.tsList.length}`);
1410
+ }
1407
1411
  const tsListBuffer = this.tsList.map((ts) => ts.serialize());
1408
1412
  const tsBuffer = Buffer.concat(tsListBuffer);
1409
1413
  // Fix the length
1410
- this.length = 5 + tsBuffer.length;
1414
+ this.length = 8 + tsBuffer.length;
1411
1415
  const buffer = Buffer.alloc(this.length);
1412
1416
  super.serialize().copy(buffer);
1413
- buffer.writeUInt8(this.numTs, 4);
1417
+ buffer.writeUInt8(this.tsList.length, 4);
1414
1418
  // No need to blank 3 reserved bytes, Buffer.alloc does that
1415
1419
  tsBuffer.copy(buffer, 8);
1416
1420
  return buffer;
@@ -1422,7 +1426,6 @@ class PayloadTS extends Payload {
1422
1426
  */
1423
1427
  toJSON() {
1424
1428
  const json = super.genToJSON();
1425
- json.numTs = this.numTs;
1426
1429
  json.tsList = this.tsList.map((ts) => ts.toJSON());
1427
1430
  return json;
1428
1431
  }
@@ -1433,7 +1436,7 @@ class PayloadTS extends Payload {
1433
1436
  */
1434
1437
  toString() {
1435
1438
  const genericString = super.genToString();
1436
- return `${genericString}\nnumTs: ${this.numTs}\ntsList: ${this.tsList.map((ts) => ts.toString()).join(", ")}`;
1439
+ return `${genericString}\ntsList: ${this.tsList.map((ts) => ts.toString()).join(", ")}`;
1437
1440
  }
1438
1441
  }
1439
1442
  exports.PayloadTS = PayloadTS;
@@ -1443,10 +1446,9 @@ exports.PayloadTS = PayloadTS;
1443
1446
  * @extends Payload
1444
1447
  */
1445
1448
  class PayloadTSi extends PayloadTS {
1446
- constructor(nextPayload, numTs, tsList, critical = false, length = 0) {
1447
- super(nextPayload, numTs, tsList, critical, length > 0 ? length : 5 + tsList.reduce((acc, ts) => acc + ts.length, 0));
1449
+ constructor(nextPayload, tsList, critical = false, length = 0) {
1450
+ super(nextPayload, payloadType.TSi, tsList, critical, length > 0 ? length : 0);
1448
1451
  this.nextPayload = nextPayload;
1449
- this.numTs = numTs;
1450
1452
  this.tsList = tsList;
1451
1453
  this.critical = critical;
1452
1454
  this.length = length;
@@ -1460,10 +1462,9 @@ exports.PayloadTSi = PayloadTSi;
1460
1462
  * @extends Payload
1461
1463
  */
1462
1464
  class PayloadTSr extends PayloadTS {
1463
- constructor(nextPayload, numTs, tsList, critical = false, length = 0) {
1464
- super(nextPayload, numTs, tsList, critical, length > 0 ? length : 5 + tsList.reduce((acc, ts) => acc + ts.length, 0));
1465
+ constructor(nextPayload, tsList, critical = false, length = 0) {
1466
+ super(nextPayload, payloadType.TSr, tsList, critical, length > 0 ? length : 0);
1465
1467
  this.nextPayload = nextPayload;
1466
- this.numTs = numTs;
1467
1468
  this.tsList = tsList;
1468
1469
  this.critical = critical;
1469
1470
  this.length = length;
@@ -1684,7 +1685,7 @@ class PayloadCP extends Payload {
1684
1685
  static parse(buffer) {
1685
1686
  const genericPayload = Payload.parse(buffer);
1686
1687
  const cfgType = buffer.readUInt8(4);
1687
- const cfgData = buffer.subarray(5, genericPayload.length);
1688
+ const cfgData = buffer.subarray(8, genericPayload.length);
1688
1689
  return new PayloadCP(genericPayload.nextPayload, cfgType, cfgData, genericPayload.critical, genericPayload.length);
1689
1690
  }
1690
1691
  /**
@@ -1699,10 +1700,7 @@ class PayloadCP extends Payload {
1699
1700
  const genericPayload = Payload.serializeJSON(json);
1700
1701
  genericPayload.copy(buffer);
1701
1702
  buffer.writeUInt8(json.cfgType, 4);
1702
- // Write 3-byte reserved field as zeros in big-endian order
1703
- buffer.writeUInt8(0, 5);
1704
- buffer.writeUInt8(0, 6);
1705
- buffer.writeUInt8(0, 7);
1703
+ // 3-bytes reserved field as zeros in big-endian order
1706
1704
  Buffer.from(json.cfgData, "hex").copy(buffer, 8);
1707
1705
  return buffer;
1708
1706
  }
@@ -37,12 +37,12 @@ export declare enum TrafficSelectorType {
37
37
  export declare class TrafficSelector {
38
38
  type: number;
39
39
  protocolId: number;
40
- length: number;
41
40
  startPort: number;
42
41
  endPort: number;
43
42
  startAddress: Buffer;
44
43
  endAddress: Buffer;
45
- constructor(type: number, protocolId: number, length: number, startPort: number, endPort: number, startAddress: Buffer, endAddress: Buffer);
44
+ length: number;
45
+ constructor(type: number, protocolId: number, startPort: number, endPort: number, startAddress: Buffer, endAddress: Buffer, length?: number);
46
46
  /**
47
47
  * Parses a Traffic Selector from a buffer
48
48
  * @param buffer The buffer to parse from.
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrafficSelector = exports.TrafficSelectorType = void 0;
4
+ const ip_address_1 = require("./ip-address");
2
5
  /**
3
6
  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
4
7
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -17,8 +20,6 @@
17
20
 
18
21
  Traffic Selector
19
22
  */
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.TrafficSelector = exports.TrafficSelectorType = void 0;
22
23
  /**
23
24
  * Traffic Selector Type
24
25
  */
@@ -39,14 +40,14 @@ var TrafficSelectorType;
39
40
  * @property {Buffer} endAddress - 4 bytes
40
41
  */
41
42
  class TrafficSelector {
42
- constructor(type, protocolId, length, startPort, endPort, startAddress, endAddress) {
43
+ constructor(type, protocolId, startPort, endPort, startAddress, endAddress, length = 0) {
43
44
  this.type = type;
44
45
  this.protocolId = protocolId;
45
- this.length = length;
46
46
  this.startPort = startPort;
47
47
  this.endPort = endPort;
48
48
  this.startAddress = startAddress;
49
49
  this.endAddress = endAddress;
50
+ this.length = length;
50
51
  }
51
52
  /**
52
53
  * Parses a Traffic Selector from a buffer
@@ -58,16 +59,42 @@ class TrafficSelector {
58
59
  static parse(buffer) {
59
60
  try {
60
61
  const type = buffer.readUInt8(0);
62
+ var expectedLength;
63
+ switch (type) {
64
+ case TrafficSelectorType.TS_IPV4_ADDR_RANGE:
65
+ expectedLength = 16;
66
+ break;
67
+ case TrafficSelectorType.TS_IPV6_ADDR_RANGE:
68
+ expectedLength = 40;
69
+ break;
70
+ default:
71
+ throw new Error("Invalid traffic selector type");
72
+ }
61
73
  const protocolId = buffer.readUInt8(1);
62
74
  const length = buffer.readUInt16BE(2);
75
+ if (length !== expectedLength) {
76
+ throw new Error("Invalid traffic selector length");
77
+ }
63
78
  const startPort = buffer.readUInt16BE(4);
64
79
  const endPort = buffer.readUInt16BE(6);
65
- const startAddress = buffer.subarray(8, 12);
66
- const endAddress = buffer.subarray(12, 16);
67
- return new TrafficSelector(type, protocolId, length, startPort, endPort, startAddress, endAddress);
80
+ var startAddress;
81
+ var endAddress;
82
+ switch (type) {
83
+ case TrafficSelectorType.TS_IPV4_ADDR_RANGE:
84
+ startAddress = buffer.subarray(8, 12);
85
+ endAddress = buffer.subarray(12, 16);
86
+ break;
87
+ case TrafficSelectorType.TS_IPV6_ADDR_RANGE:
88
+ startAddress = buffer.subarray(8, 24);
89
+ endAddress = buffer.subarray(24, 40);
90
+ break;
91
+ default:
92
+ throw new Error("Invalid traffic selector type");
93
+ }
94
+ return new TrafficSelector(type, protocolId, startPort, endPort, startAddress, endAddress, length);
68
95
  }
69
96
  catch (error) {
70
- throw new Error("Failed to parse traffic selector");
97
+ throw new Error("Failed to parse traffic selector: " + error);
71
98
  }
72
99
  }
73
100
  /**
@@ -78,14 +105,29 @@ class TrafficSelector {
78
105
  * @returns {Buffer}
79
106
  */
80
107
  static serializeJSON(json) {
81
- const buffer = Buffer.alloc(16);
108
+ var length;
109
+ var ipLength;
110
+ const startAddress = (0, ip_address_1.parseIPAddressString)(json.startAddress);
111
+ const endAddress = (0, ip_address_1.parseIPAddressString)(json.endAddress);
112
+ if (startAddress.length === 4 && endAddress.length === 4) {
113
+ length = 16;
114
+ ipLength = 4;
115
+ }
116
+ else if (startAddress.length === 16 && endAddress.length === 16) {
117
+ length = 40;
118
+ ipLength = 16;
119
+ }
120
+ else {
121
+ throw new Error("Invalid traffic selector length");
122
+ }
123
+ const buffer = Buffer.alloc(length);
82
124
  buffer.writeUInt8(json.type, 0);
83
125
  buffer.writeUInt8(json.protocolId, 1);
84
- buffer.writeUInt16BE(json.length, 2);
126
+ buffer.writeUInt16BE(length, 2);
85
127
  buffer.writeUInt16BE(json.startPort, 4);
86
128
  buffer.writeUInt16BE(json.endPort, 6);
87
- Buffer.from(json.startAddress, "hex").copy(buffer, 8);
88
- Buffer.from(json.endAddress, "hex").copy(buffer, 12);
129
+ startAddress.copy(buffer, 8);
130
+ endAddress.copy(buffer, 8 + ipLength);
89
131
  return buffer;
90
132
  }
91
133
  /**
@@ -94,14 +136,33 @@ class TrafficSelector {
94
136
  * @returns {Buffer}
95
137
  */
96
138
  serialize() {
97
- const buffer = Buffer.alloc(16);
139
+ var ipLength;
140
+ switch (this.type) {
141
+ case TrafficSelectorType.TS_IPV4_ADDR_RANGE:
142
+ if (this.startAddress.length != 4 || this.endAddress.length != 4) {
143
+ throw new Error("Invalid traffic selector length");
144
+ }
145
+ this.length = 16;
146
+ ipLength = 4;
147
+ break;
148
+ case TrafficSelectorType.TS_IPV6_ADDR_RANGE:
149
+ if (this.startAddress.length != 16 || this.endAddress.length != 16) {
150
+ throw new Error("Invalid traffic selector length");
151
+ }
152
+ this.length = 40;
153
+ ipLength = 16;
154
+ break;
155
+ default:
156
+ throw new Error("Invalid traffic selector type");
157
+ }
158
+ const buffer = Buffer.alloc(this.length);
98
159
  buffer.writeUInt8(this.type, 0);
99
160
  buffer.writeUInt8(this.protocolId, 1);
100
161
  buffer.writeUInt16BE(this.length, 2);
101
162
  buffer.writeUInt16BE(this.startPort, 4);
102
163
  buffer.writeUInt16BE(this.endPort, 6);
103
164
  this.startAddress.copy(buffer, 8);
104
- this.endAddress.copy(buffer, 12);
165
+ this.endAddress.copy(buffer, 8 + ipLength);
105
166
  return buffer;
106
167
  }
107
168
  /**
@@ -113,11 +174,10 @@ class TrafficSelector {
113
174
  return {
114
175
  type: this.type,
115
176
  protocolId: this.protocolId,
116
- length: this.length,
117
177
  startPort: this.startPort,
118
178
  endPort: this.endPort,
119
- startAddress: this.startAddress.toString("hex"),
120
- endAddress: this.endAddress.toString("hex"),
179
+ startAddress: (0, ip_address_1.formatIPAddressBuffer)(this.startAddress),
180
+ endAddress: (0, ip_address_1.formatIPAddressBuffer)(this.endAddress),
121
181
  };
122
182
  }
123
183
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-ikev2",
3
3
  "title": "Node-IKEV2",
4
- "version": "0.1.4",
4
+ "version": "0.2.1",
5
5
  "description": "IKEv2 parser and serializer for Node.js",
6
6
  "main": "./lib/src/index.js",
7
7
  "types": "./lib/src/index.d.ts",