-
Notifications
You must be signed in to change notification settings - Fork 18
/
bbfedi2eti.cpp
361 lines (335 loc) · 9.12 KB
/
bbfedi2eti.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* fedi2eti.c
* Uses parts of astra-sm and edi2eti.c
*
* Created on: 01.06.2018
* Author: athoik
*/
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
/*
* edi-tools edi2eti.c
*/
#include <arpa/inet.h>
extern "C" {
#include "edi_parser.h"
}
#include "network.h"
#include "logging.h"
#include<stdio.h>
#include<unistd.h>
#include<iostream>
#include<cstring>
extern char *optarg;
extern int optind, opterr, optopt;
bool has_src_ip=false;
bool has_dst_ip=false;
bool has_src_port=false;
bool has_dst_port=false;
bool has_mis=false;
unsigned char src_ip[4];
unsigned char dst_ip[4];
unsigned char src_port[4];
unsigned char dst_port[4];
unsigned char mis;
static
void write_file(void *privData, void *etiData, int etiLen)
{
FILE *fh = (FILE *) privData;
fwrite(etiData, 1, etiLen, fh);
}
using namespace std;
istream* inbuf;
ostream* outbuf;
char indata[8100];
int active=0;
unsigned char** fragmentor;
unsigned int fragmentorLength[256];
unsigned int fragmentorPos[256];
edi_handler_t *edi_p;
typedef struct {
unsigned char MaType1;
unsigned char MaType2;
unsigned char Upl1;
unsigned char Upl2;
unsigned char Dfl1;
unsigned char Dfl2;
char Sync;
char SyncD1;
char SyncD2;
char Crc8;
} bbheader;
struct layer3{
unsigned char L3Sync;
/*unsigned char AcmCommand;
unsigned char CNI;
unsigned char PlFrameId;*/
bbheader header;
unsigned char payload[7264-10];
};
bool isSelected(unsigned char* buf)
{
if(has_src_ip && (!(buf[0]==src_ip[0] && buf[1]==src_ip[1] && buf[2]==src_ip[2] && buf[3]==src_ip[3])))
return false;
if(has_dst_ip && (!(buf[4]==dst_ip[0] && buf[5]==dst_ip[1] && buf[6]==dst_ip[2] && buf[7]==dst_ip[3])))
return false;
if(has_src_port &&(!(buf[0x8]==src_port[0] && buf[0x9]==src_port[1])))
return false;
if(has_dst_port &&(!(buf[0xa]==dst_port[0] && buf[0xb]==dst_port[1])))
return false;
return true;
}
bool processBBframe(unsigned char* payload, unsigned int gseLength)
{
//fprintf(stderr, "GSELength:%x\n", gseLength);
unsigned int offset=0;
unsigned int fragID=0;
//START=1 STOP=0
if((payload[0]&0xC0)==0x80) {
fragID=payload[2];
unsigned int length=(payload[3]<<8) | payload[4];
if(fragmentor[fragID]!=0)
delete [] fragmentor[fragID];
fragmentor[fragID]=new unsigned char[length+2];
fragmentorLength[fragID]=length+2;
fragmentor[fragID][0]=payload[0];
fragmentor[fragID][1]=payload[1];
//SET START=1 STOP=1
fragmentor[fragID][0]|=0xC0;
memcpy(&fragmentor[fragID][2], &payload[5], gseLength-3);
fragmentorPos[fragID]=gseLength-1;
}
//START=0 STOP=0
else if((payload[0]&0xC0)==0x00) {
fragID=payload[2];
if(fragmentor[fragID]==0)
return true;
memcpy(&fragmentor[fragID][fragmentorPos[fragID]], &payload[3], gseLength-1);
fragmentorPos[fragID]+=gseLength-1;
}
//START=0 STOP=1
else if((payload[0]&0xC0)==0x40) {
fragID=payload[2];
if(fragmentor[fragID]==0)
return true;
memcpy(&fragmentor[fragID][fragmentorPos[fragID]], &payload[3], gseLength-5);
fragmentorPos[fragID]+=gseLength-1;
processBBframe(fragmentor[fragID],fragmentorLength[fragID]);
delete [] fragmentor[fragID];
fragmentor[fragID]=0;
}
//START=1 STOP=1
else if((payload[0]&0xC0)==0xC0) {
if(payload[offset+2]==0x00 && payload[offset+3]==0x04) {
//LABEL
if((payload[0]&0x30)==0x01) {
offset += 3;
}
else if((payload[0]&0x30)==0x00) {
offset += 6;
}
//fprintf(stderr, "Start of 00 04 packet:%02x %02x %02x %02x %02x\n",payload[6], payload[7], payload[8], payload[9], payload[10]);
if(payload[6]&0x80) {
offset+=3;
offset += 2;
offset += 0x10;
if(isSelected(&payload[offset])) {
offset +=0x10;
active=payload[7];
}
else {
active=0;
return true;
}
}
else if(active==payload[7]) {
offset+=9;
}
else {
return true;
}
}
else if(payload[offset+2]==0x00 && payload[offset+3]==0x00) {
//LABEL
if((payload[0]&0x30)==0x01) {
offset += 3;
}
else if((payload[0]&0x30)==0x00) {
offset += 6;
}
offset += 2;
offset += 0x10;
if(isSelected(&payload[offset])) {
offset+=0x10;
}
else return true;
}
else if(payload[offset+2]==0x08 && payload[offset+3]==0x00) {
if((payload[0]&0x30)==0x01) {
offset += 3;
}
else if((payload[0]&0x30)==0x00) {
offset += 6;
}
offset += 0x10;
if(isSelected(&payload[offset])) {
offset+=0x10;
}
else return true;
}
// outbuf->write((char*)&payload[offset],gseLength + 2 -offset);
HandleEDIPacket(edi_p, &payload[offset], gseLength+2-offset);
return true;
}
//PADDING
else if((payload[0]&0xf0)==0x00) {
return false;
}
return true;
}
int main(int argc, char** argv) {
int c;
if(argc<=1) {
fprintf(stderr, "Usage: %s [-src-ip x.x.x.x] [-src-port xxxx] [-dst-ip x.x.x.x] [-dst-port xxxx] [-mis xxxx]\n",argv[0]);
fprintf(stderr, "Note: At least one argument is required\n");
exit(1);
}
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"src-ip", required_argument, 0, 0 },
{"dst-ip", required_argument, 0, 0 },
{"src-port", required_argument, 0, 0 },
{"dst-port", required_argument, 0, 0 },
{"mis", required_argument, 0, 0 },
{"help", no_argument , 0, 0 },
{0, 0, 0, 0 }
};
c = getopt_long_only(argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
fprintf(stderr,"option %s", long_options[option_index].name);
if(option_index==0) //src-ip
{
has_src_ip=true;
char** ptr=&optarg;
char* inv;
for(int i=0; i<4;i++)
{
src_ip[i]=(unsigned char) strtol(*ptr,&inv,10);
inv++;
ptr=&inv;
}
}
else if(option_index==1) //dst_ip
{
has_dst_ip=true;
char** ptr=&optarg;
char* inv;
for(int i=0; i<4;i++)
{
dst_ip[i]=(unsigned char) strtol(*ptr,&inv,10);
inv++;
ptr=&inv;
}
}
else if(option_index==2) //src_port
{
has_src_port=true;
unsigned short tmp=(unsigned short) strtol(optarg,NULL,10);
src_port[0]=tmp>>8;
src_port[1]=tmp&0xff;
}
else if(option_index==3) //dst_port
{
has_dst_port=true;
unsigned short tmp=(unsigned short) strtol(optarg,NULL,10);
dst_port[0]=tmp>>8;
dst_port[1]=tmp&0xff;
}
else if(option_index==4) //mis
{
has_mis=true;
mis=(unsigned char) strtol(optarg,NULL,10);
}
break;
case '?':
break;
default:
fprintf(stderr,"?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
fprintf(stderr,"non-option ARGV-elements: ");
while (optind < argc)
fprintf(stderr,"%s ", argv[optind++]);
fprintf(stderr,"\n");
exit(1);
}
unsigned char active=0;
inbuf=&cin;
outbuf=&cout;
struct layer3 * mylayer=(struct layer3*) indata;
edi_p = initEDIHandle(ETI_FMT_RAW, write_file, stdout);
fragmentor=new unsigned char*[256];
for(int i=0; i<256;i++)
{
fragmentor[i]=0;
}
while(1)
{
if(inbuf->peek() == 0xb8) break;
inbuf->read(indata,1);
}
while(1)
{
inbuf->read(indata, 11);
if(inbuf->fail()) break;
if(mylayer->L3Sync!=0xb8) {
while(1)
{
if(inbuf->peek() == 0xb8) break;
inbuf->read(indata,1);
if(inbuf->fail()) break;
}
continue;
}
unsigned int bblength=mylayer->header.Dfl1<<8 | mylayer->header.Dfl2;
bblength>>=3;
//fprintf(stderr, "%x %x %x %x %x\n",mylayer->L3Sync, mylayer->header.MaType1, mylayer->header.MaType2, mylayer->header.Dfl1, mylayer->header.Dfl2);
//fprintf(stderr, "BBlength:%x\n",bblength);
inbuf->read(&indata[11], bblength);
if(inbuf->fail()) break;
if(has_mis && (mylayer->header.MaType2 != mis)) continue;
//fprintf(stderr, "GSE Header:%x %x %x\n",mylayer->payload[0],mylayer->payload[1],mylayer->payload[2]);
int pos=0;
while(pos<bblength - 4) { //last 4 bytes contain crc32
unsigned int gseLength=((mylayer->payload[pos]&0x0f)<<8) | (mylayer->payload[pos+1]);
if((mylayer->payload[pos]&0xf0)==0) break;
if(gseLength+2>bblength-pos) break;
if(!processBBframe(&mylayer->payload[pos], gseLength)) break;
pos+=gseLength+2;
}
if(inbuf->fail()) break;
}
::closeEDIHandle(edi_p);
return 0;
}