-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.ts
333 lines (233 loc) · 10.5 KB
/
ip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { IPProtocolToName, DSCPToName } from "./names.ts";
const formatIP = ( bytes: number[] ) =>
"0x"
+ bytes.map( dec => dec.toString( 16 ).padStart( 2, "0" ) ).join( "" )
+ " (" + bytes.join( "." ) + ")";
const formatTimeStamp = ( bytes: number[] ) =>
"0x"
+ bytes.map( dec => dec.toString( 16 ).padStart( 2, "0" ) ).join( "" )
+ " (" + new Date( bytes.reduce( ( acc, cur ) => 256 * acc + cur, 0 ) * 1000 ).toISOString() + ")";
const groupIPs = ( bytes: number[] ): string[] =>
bytes.length === 0
? []
: [
formatIP( bytes.slice( 0, 4 ) ),
...groupIPs( bytes.slice( 4 ) )
]
;
const groupTimeStamps = ( bytes: number[] ): string[] =>
bytes.length === 0
? []
: [
formatTimeStamp( bytes.slice( 0, 4 ) ),
...groupTimeStamps( bytes.slice( 4 ) )
]
;
const groupIPsAndTimeStamps = ( bytes: number[] ): string[] =>
bytes.length === 0
? []
: [
formatIP( bytes.slice( 0, 4 ) ) + " at " +
formatTimeStamp( bytes.slice( 4, 8 ) ),
...groupIPsAndTimeStamps( bytes.slice( 8 ) )
]
;
const parseOptions = ( bytes: number[] ): { [ key: string ]: string | string[] | { Name: string, "Copy on fragmentation": string, Class: string, Number: string } }[] => {
if ( bytes.length === 0 )
return [];
switch ( bytes[ 0 ] ) {
case 0:
return [ {
Type: {
Name: "0x00 (End Of Options List)",
"Copy on fragmentation": "0... .... (No)",
Class: ".00. .... (Control)",
Number: `...0 0000 (0)`
}
} ];
case 1:
return [ {
Type: {
Name: "0x01 (No Operation)",
"Copy on fragmentation": "0... .... (No)",
Class: ".00. .... (Control)",
Number: `...0 0001 (1)`
} },
...parseOptions( bytes.slice( 1 ) )
];
case 131: {
const [ type, length, pointer ] = bytes;
return [ {
Type: {
Name: "0x83 (Loose Source Route)",
"Copy on fragmentation": "1... .... (Yes)",
Class: ".00. .... (Control)",
Number: `...0 0011 (3)`
},
Length: `0x${ length.toString( 16 ).padStart( 2, "0" ) } (${ length } bytes)`,
Pointer: `0x${ pointer.toString( 16 ).padStart( 2, "0" ) } (${ pointer })`,
Route: groupIPs( bytes.slice( 4, pointer ) )
},
...parseOptions( bytes.slice( length ) )
];
}
case 68: {
const [ type, length, pointer, overflowFlag ] = bytes;
const overflow = Math.floor( overflowFlag / 8 );
const flag = overflowFlag % 8;
return [ {
Type: {
Name: "0x44 (Time Stamp)",
"Copy on fragmentation": "0... .... (No)",
Class: ".10. .... (Debugging and measurement)",
Number: `...0 0100 (4)`
},
Length: `0x${ length.toString( 16 ).padStart( 2, "0" ) } (${ length } bytes)`,
Pointer: `0x${ pointer.toString( 16 ).padStart( 2, "0" ) } (${ pointer })`,
Overflow: `${ overflow.toString( 2 ).padStart( 4, "0" ) } .... (${ overflow })`,
Flag: `${ flag.toString( 2 ).padStart( 4, "0" ) } .... (${ flag })`,
TimeStamps: ( flag === 0 ? groupTimeStamps : groupIPsAndTimeStamps )( bytes.slice( 4, pointer ) )
},
...parseOptions( bytes.slice( length ) )
];
}
case 134: {
const length = bytes[ 1 ];
const DOI = 256 ** 3 * bytes[ 2 ] + 256 ** 2 * bytes[ 3 ] + 256 * bytes[ 4 ] + bytes[ 5 ]
return [ {
Type: {
Name: "0x86 (Commercial security)",
"Copy on fragmentation": "1... .... (Yes)",
Class: ".00. .... (Control)",
Number: `...0 0110 (6)`
},
Length: `0x${ length.toString( 16 ).padStart( 2, "0" ) } (${ length } bytes)`,
DOI: `0x${ DOI.toString( 16 ).padStart( 2, "0" ) } (${ DOI })`,
Tags: "0x" + bytes.slice( 6, length ).map( dec => dec.toString( 16 ).padStart( 2, "0" ) ).join( "" )
},
...parseOptions( bytes.slice( length ) )
];
}
case 7: {
const [ type, length, pointer ] = bytes;
return [ {
Type: {
Name: "0x07 (Record Route)",
"Copy on fragmentation": "0... .... (No)",
Class: ".00. .... (Control)",
Number: `...0 0111 (7)`
},
Length: `0x${ length.toString( 16 ).padStart( 2, "0" ) } (${ length } bytes)`,
Pointer: `0x${ pointer.toString( 16 ).padStart( 2, "0" ) } (${ pointer })`,
Route: groupIPs( bytes.slice( 4, pointer ) )
},
...parseOptions( bytes.slice( length ) )
];
}
case 136: {
const length = bytes[ 1 ];
const streamID = bytes.slice( 2, length ).reduce( ( acc, cur ) => 256 * acc + cur, 0 );
return [ {
Type: {
Name: "0x6c (Stream Identifier)",
"Copy on fragmentation": "1... .... (Yes)",
Class: ".00. .... (Control)",
Number: `...0 1000 (8)`
},
Length: `0x${ length.toString( 16 ).padStart( 2, "0" ) } (${ length } bytes)`,
"Stream ID": `0x${ streamID.toString( 16 ).padStart( 2, "0" ) } (${ streamID })`
},
...parseOptions( bytes.slice( length ) )
];
}
case 137: {
const [ type, length, pointer ] = bytes;
return [ {
Type: {
Name: "0x89 (Strict Source Route)",
"Copy on fragmentation": "1... .... (Yes)",
Class: ".00. .... (Control)",
Number: `...0 1001 (9)`
},
Length: `0x${ length.toString( 16 ).padStart( 2, "0" ) } (${ length } bytes)`,
Pointer: `0x${ pointer.toString( 16 ).padStart( 2, "0" ) } (${ pointer })`,
Route: groupIPs( bytes.slice( 4, pointer ) )
},
...parseOptions( bytes.slice( length ) )
];
}
case 148: {
const value = bytes[ 2 ] * 256 + bytes[ 3 ];
const valueHex = value.toString( 2 ).padStart( 4, "0" );
return [ {
Type: {
Name: "0x94 (Router Alert)",
"Copy on fragmentation": "1... .... (Yes)",
Class: ".00. .... (Control)",
Number: `...1 0100 (20)`
},
Length: "0x04 (4 bytes)",
Value: value === 0
? "0x0000 (Router shall examine packet)"
: value < 33
? `0x${ valueHex } (Aggregated Reservation Nesting Level)`
: value < 65
? `0x${ valueHex } (QoS NSLP Aggregation Levels 0-31)`
: value < 66
? "0x41 (NSIS NATFW NSLP)"
: value < 65503
? `0x${ valueHex } (Unassigned)`
: value < 65535
? `0x${ valueHex } (Reserved for experimental use)`
: "0xff Reserved"
},
...parseOptions( bytes.slice( 4 ) )
];
}
default:
console.log( "Invalid option." );
return [];
}
};
export const layerToIP = ( layer: number[] ) => {
const version = Math.floor( layer[ 0 ] / 16 );
const headerLength = layer[ 0 ] % 16;
const DSCP = Math.floor( layer[ 1 ] / 4 );
const explicitCongestionNotification = layer[ 1 ] % 4;
const totalLength = layer[ 2 ] * 256 + layer[ 3 ];
const identification = layer[ 4 ] * 256 + layer[ 5 ];
const reservedBit = Math.floor( layer[ 6 ] / 128 );
const dontFragment = Math.floor( layer[ 6 ] / 64 ) % 2;
const moreFragments = Math.floor( layer[ 6 ] / 32 ) % 2;
const fragmentOffset = layer[ 6 ] * 256 % 32 + layer[ 7 ];
const timeToLive = layer[ 8 ];
const protocol = layer[ 9 ];
const headerChecksum = layer[ 10 ] * 256 + layer[ 11 ];
return {
Version: `0x${ version.toString( 16 ) } (${ version })`,
"Header length": `0x${ headerLength.toString( 16 ) } (${ headerLength * 4 } bytes)`,
"Differentiated services field": {
"Differentiated services codepoint": `${ DSCP.toString( 2 ).padStart( 6, "0" ) }.. (${ DSCPToName( DSCP ) })`,
"Explicit congestion notification": [
"......00 Non ECN-Capable Transport",
"......01 ECN Capable Transport",
"......10 ECN Capable Transport",
"......11 Congestion Encountered"
][ explicitCongestionNotification ]
},
"Total length": `0x${ totalLength.toString( 16 ).padStart( 4, "0" ) } (${ totalLength })`,
Identification: `0x${ identification.toString( 16 ).padStart( 4, "0" ) } (${ identification })`,
Flags: {
"Reserved bit": reservedBit ? "1.. (Set)" : "0.. (Not set)",
"Don't fragment": dontFragment ? ".1. (Set)" : ".0. (Not set)",
"More fragments": moreFragments ? "..1 (Set)" : "..0 (Not set)"
},
"Fragment offset": `0x${ fragmentOffset.toString( 16 ) } (${ fragmentOffset })`,
"Time to live": `0x${ timeToLive.toString( 16 ).padStart( 2, "0" ) } (${ timeToLive })`,
Protocol: `0x${ protocol.toString( 16 ).padStart( 2, "0" ) } (${ IPProtocolToName( protocol ) })`,
"Header checksum": `0x${ headerChecksum.toString( 16 ) } (${ headerChecksum })`,
Source: formatIP( layer.slice( 12, 16 ) ),
Destination: formatIP( layer.slice( 16, 20 ) ),
Options: parseOptions( layer.slice( 20 ) ),
};
};