node-ikev2 0.1.3 → 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.
- package/lib/src/configuration-attribute.d.ts +302 -0
- package/lib/src/configuration-attribute.js +851 -0
- package/lib/src/index.d.ts +2 -0
- package/lib/src/index.js +2 -0
- package/lib/src/ip-address.d.ts +6 -0
- package/lib/src/ip-address.js +159 -0
- package/lib/src/payload.d.ts +4 -6
- package/lib/src/payload.js +17 -19
- package/lib/src/selector.d.ts +2 -2
- package/lib/src/selector.js +77 -17
- package/package.json +1 -1
package/lib/src/index.d.ts
CHANGED
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
|
+
}
|
package/lib/src/payload.d.ts
CHANGED
|
@@ -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
|
-
|
|
735
|
+
tsType: payloadType;
|
|
736
736
|
tsList: TrafficSelector[];
|
|
737
737
|
critical: boolean;
|
|
738
738
|
length: number;
|
|
739
|
-
constructor(nextPayload: payloadType,
|
|
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,
|
|
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,
|
|
797
|
+
constructor(nextPayload: payloadType, tsList: TrafficSelector[], critical?: boolean, length?: number);
|
|
800
798
|
}
|
|
801
799
|
/**
|
|
802
800
|
* IKEv2 Encrypted and Authenticated Payload
|
package/lib/src/payload.js
CHANGED
|
@@ -1347,10 +1347,10 @@ exports.PayloadVENDOR = PayloadVENDOR;
|
|
|
1347
1347
|
* @extends Payload
|
|
1348
1348
|
*/
|
|
1349
1349
|
class PayloadTS extends Payload {
|
|
1350
|
-
constructor(nextPayload,
|
|
1351
|
-
super(
|
|
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.
|
|
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,
|
|
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 =
|
|
1414
|
+
this.length = 8 + tsBuffer.length;
|
|
1411
1415
|
const buffer = Buffer.alloc(this.length);
|
|
1412
1416
|
super.serialize().copy(buffer);
|
|
1413
|
-
buffer.writeUInt8(this.
|
|
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}\
|
|
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,
|
|
1447
|
-
super(nextPayload,
|
|
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,
|
|
1464
|
-
super(nextPayload,
|
|
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(
|
|
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
|
-
//
|
|
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
|
}
|
package/lib/src/selector.d.ts
CHANGED
|
@@ -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
|
-
|
|
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.
|
package/lib/src/selector.js
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
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(
|
|
126
|
+
buffer.writeUInt16BE(length, 2);
|
|
85
127
|
buffer.writeUInt16BE(json.startPort, 4);
|
|
86
128
|
buffer.writeUInt16BE(json.endPort, 6);
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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,
|
|
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
|
|
120
|
-
endAddress: this.endAddress
|
|
179
|
+
startAddress: (0, ip_address_1.formatIPAddressBuffer)(this.startAddress),
|
|
180
|
+
endAddress: (0, ip_address_1.formatIPAddressBuffer)(this.endAddress),
|
|
121
181
|
};
|
|
122
182
|
}
|
|
123
183
|
/**
|