-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_001.c
345 lines (286 loc) · 6.12 KB
/
main_001.c
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
/******************************************************************************
* *
* File Name : main_001.c *
* Author : zhengnan *
* Create Date : 2014-03-17 *
* Version : 1.0 *
* Function : *
* *
******************************************************************************/
#include "gnInclude.h"
#include "libcom.h"
#include "gnMutex.h"
#include "gnMem.h"
#include "gnLog.h"
#include "gnTimer.h"
#include "gnNet.h"
#include "gnUtls.h"
#include "gnFlow.h"
#include "gnPkt.h"
#include "gnproto.h"
#include "workque.h"
#include "cmdline.h"
//////////////////////////////////////////////////////////////////////////////
#ifndef MAX_RX_QUES
#define MAX_RX_QUES 8
#endif
//////////////////////////////////////////////////////////////////////////////
#define PAD_LEN 20
#define CRC_LEN 4
//////////////////////////////////////////////////////////////////////////////
int dpi_flag = 0;
int xmit_flag = 1;
int debug_flag = 0;
TAB_ID flow_v6tab[MAX_RX_QUES]={0};
int rx_port = -1;
int tx_port = -1;
UINT4 flow_tabsz = 178911;
static void flow_v6timeout(void *owner, tTimer *tid)
{
tIpV6Flow *flow = (tIpV6Flow *)owner;
if(flow->state == FLOW_ALIVE)
{
flow->state = FLOW_DEAD;
return;
}
if(debug_flag)
{
printf("flow timeout!\n");
}
kill_timer(tid);
del_ipv6flow_safe(flow->id, flow);
}
static void init_dpi(int num_ques)
{
int qid;
for(qid=0; qid<num_ques; qid++)
{
flow_v6tab[qid] = create_ipv6_flowtab(flow_tabsz, 1000, 0, CREAT_TCP | CREAT_UDP | CREAT_OTHER,
30, (TimeFunc)flow_v6timeout, NULL);
if(flow_v6tab[qid])
{
int opt;
opt = rx_port;
set_flowtab_opt(flow_v6tab[qid], FLOWTAB_IFINDEX, &opt);
opt = qid;
set_flowtab_opt(flow_v6tab[qid], FLOWTAB_PID, &opt);
}
}
}
static inline int proc_dpi_out(tEthpkt *pkthdr, tEther *pEth, tIpv6 *pIpv6)
{
tIpV6Flow *flow;
int pid;
int flag = PACKET_OUT;
pid = pkthdr->pid;
flow = create_ipv6flow_safe_ext(flow_v6tab[pid], pIpv6, &flag);
if(!flow)
{
return 0;
}
if(flag == FLOW_NEW)
{
flow->ext.t_start = cur_sys_time.tv_sec;
flow->ext.t_stop = cur_sys_time.tv_sec;
if(debug_flag)
{
printf("new ipv6flow(out)!\n");
}
}
else
{
if(flow->ext.t_stop != cur_sys_time.tv_sec)
{
flow->ext.t_stop = cur_sys_time.tv_sec;
}
if(debug_flag)
{
printf("find ipv6flow(out)!\n");
}
}
flow->ext.up_pkts++;
flow->ext.up_bytes += pkthdr->len;
if(flow->state == FLOW_DEAD)
{
flow->state = FLOW_ALIVE;
}
if(!flow->appid)
{
flow->appid = AnalyzeProtoV6(PACKET_OUT, pIpv6, flow);
if(debug_flag)
{
if(flow->appid)
{
printf("[FindApp](out) id:%d, %s.\n",
flow->appid, GetNamebyProtoId(flow->appid));
}
}
}
return 0;
}
static inline int proc_dpi_in(tEthpkt *pkthdr, tEther *pEth, tIpv6 *pIpv6)
{
printf("wait...\n");
return 1;
}
int ProcNapiPkt(tEthpkt *pkthdr, int count)
{
tEthpkt *phdr;
int ii;
UINT4 rxbytes = 0;
phdr = pkthdr;
if(dpi_flag)
{
if(pkthdr->inout == PACKET_OUT)
{
for(ii=0; ii<count; ii++)
{
if(phdr->pEth->proto == gn_ntohs(ETHERTYPE_IPv6))
{
proc_dpi_out(phdr, phdr->pEth, (tIpv6 *)phdr->pEth->data);
}
rxbytes += PAD_LEN + CRC_LEN + phdr->len;
phdr++;
}
}
else
{
for(ii=0; ii<count; ii++)
{
if(phdr->pEth->proto == gn_ntohs(ETHERTYPE_IPv6))
{
proc_dpi_in(phdr, phdr->pEth, (tIpv6 *)phdr->pEth->data);
}
rxbytes += PAD_LEN + CRC_LEN + phdr->len;
phdr++;
}
}
}
else
{
for(ii=0; ii<count; ii++)
{
rxbytes = PAD_LEN + CRC_LEN + phdr->len;
phdr++;
}
}
}
void show_help()
{
printf("\nHelp of software.\n");
printf("-rethx ----recive packets from ethx.\n");
printf("-m ----m0 receive mode, other peekmode.\n");
printf("-dpi ----enable dpi mode.\n");
printf("-t ----number of thread.\n");
printf("-debug ----will display some infomation.\n");
printf("-help ----help infomation.\n");
}
int main(int argc, char** argv)
{
int i, opt = 0;
int rx_mode = DMA_MODE;
int tx_mode = DMA_MODE;
int rx_sockfd = -1;
int tx_sockfd = -1;
int numproc = 1;
int recv_mode;
char* pif;
char* bindstr = NULL;
char rxif[128];
char ifname[16];
lib_init();
for(i=1; i<argc; i++)
{
if(!strncmp(argv[i], "-dpi", 4))
{
dpi_flag = 1;
}
else if(!strncmp(argv[i], "-r", 2))
{
sprintf(rxif, "%s", argv[i]+2);
}
else if(!strncmp(argv[i], "-m", 2))
{
pif = argv[i];
pif += 2;
sscanf(pif, "%d", &recv_mode);
}
else if(!strncmp(argv[i], "-t", 2))
{
pif = argv[i];
pif += 2;
scanf(pif, "%d", &numproc);
}
else if(!strncmp(argv[i], "-debug", 5))
{
debug_flag = 1;
}
else
{
show_help();
exit(0);
}
}
if(recv_mode == PEEK_MODE)
{
xmit_flag = 0;
}
if(rxif[0])
{
pif = rxif;
sscanf(pif, "eth%d", &rx_port);
sprintf(ifname, "eth%d", rx_port);
rx_sockfd = open_sock(ifname, rx_mode, tx_mode, recv_mode, numproc);
if(rx_sockfd < 0)
{
printf("open_sock %s failure!\n", ifname);
return -1;
}
opt = PACKET_OUT;
set_sockopt(rx_sockfd, SET_IF_INOUT, &opt);
if(numproc == 2)
{
bindstr = "1:2";
}
else if(numproc == 4){
bindstr = "1:2:3:4";
}
else
{
bindstr = "1:1:1:1";
}
set_sockopt(rx_sockfd, SET_BINDING, (int *)bindstr);
}
memset(flow_v6tab, 0, sizeof(flow_v6tab));
if(dpi_flag)
{
init_dpi(numproc);
InitProtoAnalyzer();
}
if(rx_sockfd>=0)
{
#if 0
set_frame_proc(rx_sockfd, (RX_PROC)ProcIpPkt);
#else
set_napi_proc(rx_sockfd, (RX_NAPI)ProcNapiPkt);
#endif
start_proc(rx_sockfd);
}
while(1)
{
sleep(3);
}
if(dpi_flag)
{
ShutProtoAnalyzer();
for(i=0; i<numproc; i++)
{
if(flow_v6tab[i])
{
delete_ipv6_flowtab(flow_v6tab[i]);
}
}
}
close_sock(rx_sockfd);
return 0;
}