]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/debug_cap.cpp
Добавление исходников
[stg.git] / projects / stargazer / plugins / capture / cap_debug / debug_cap.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18 Date: 18.09.2002
19 */
20
21 /*
22 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
23 */
24
25 /*
26 $Revision: 1.21 $
27 $Date: 2009/03/19 20:03:35 $
28 $Author: faust $
29 */
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39
40 #include "debug_cap.h"
41 #include "../../../traffcounter.h"
42 #include "libpal.h"
43
44 //-----------------------------------------------------------------------------
45 void WriteStat(uint32_t u, uint32_t d)
46 {
47 FILE * f;
48 f = fopen("/tmp/cap.stat", "at");
49 fprintf(f, "up %5.2f Mbit, down %5.2f Mbit, sum %5.2f Mbit\n",
50         u / (1000000*8.0),
51         d / (1000000*8.0),
52         (u + d) / (1000000*8.0));
53 fclose(f);
54 }
55 //-----------------------------------------------------------------------------
56
57 class CAP_DEBUG_CREATOR
58 {
59 private:
60     DEBUG_CAP * dc;
61
62 public:
63     CAP_DEBUG_CREATOR()
64         : dc(new DEBUG_CAP())
65         {
66         };
67     ~CAP_DEBUG_CREATOR()
68         {
69         delete dc;
70         };
71
72     DEBUG_CAP * GetCapturer()
73     {
74         return dc;
75     };
76 };
77 //-----------------------------------------------------------------------------
78 RAW_PACKET MakeTCPPacket(const char * src,
79                          const char * dst,
80                          uint16_t sport,
81                          uint16_t dport,
82                          uint16_t len);
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 CAP_DEBUG_CREATOR cdc;
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 BASE_PLUGIN * GetPlugin()
90 {
91 return cdc.GetCapturer();
92 }
93 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 const string DEBUG_CAP::GetVersion() const
97 {
98 return "Debug_cap v.0.01a";
99 }
100 //-----------------------------------------------------------------------------
101 DEBUG_CAP::DEBUG_CAP()
102 {
103 isRunning = false;
104 nonstop = false;
105 }
106 //-----------------------------------------------------------------------------
107 void DEBUG_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
108 {
109 traffCnt = tc;
110 }
111 //-----------------------------------------------------------------------------
112 const string & DEBUG_CAP::GetStrError() const
113 {
114 return errorStr;
115 }
116 //-----------------------------------------------------------------------------
117 int DEBUG_CAP::Start()
118 {
119 if (isRunning)
120     return 0;
121
122 printfd(__FILE__, "DEBUG_CAP::Start()\n");
123
124 nonstop = true;
125
126 if (pthread_create(&thread, NULL, Run1, this) == 0)
127     {
128     return 0;
129     }
130
131 errorStr = "Cannot create thread.";
132 return -1;
133 }
134 //-----------------------------------------------------------------------------
135 int DEBUG_CAP::Stop()
136 {
137 if (!isRunning)
138     return 0;
139
140 nonstop = false;
141
142 //5 seconds to thread stops itself
143 int i;
144 for (i = 0; i < 25; i++)
145     {
146     if (!isRunning)
147         break;
148
149     stgUsleep(200000);
150     //printf(".");
151     }
152
153 /*if (i)
154     printf("\n");*/
155
156 //after 5 seconds waiting thread still running. now killing it
157 if (isRunning)
158     {
159     //TODO pthread_cancel()
160     if (pthread_kill(thread, SIGINT))
161         {
162         errorStr = "Cannot kill thread.";
163         return -1;
164         }
165     }
166
167 return 0;
168 }
169 //-----------------------------------------------------------------------------
170 bool DEBUG_CAP::IsRunning()
171 {
172 return nonstop;
173 }
174 //-----------------------------------------------------------------------------
175 void * DEBUG_CAP::Run1(void * data)
176 {
177 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
178
179 DEBUG_CAP * dc = (DEBUG_CAP *)data;
180 dc->isRunning = true;
181
182 RAW_PACKET rp;
183 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
184 int a = 0;
185 sleep(3);
186
187 struct tm * tm;
188 time_t t;
189 uint32_t u = 0;
190 uint32_t d = 0;
191
192 //2 upload : 3 download
193
194 int usize;
195 int dsize;
196
197 t = stgTime;
198 tm = localtime(&t);
199 int min = tm->tm_min;
200 int sec = tm->tm_sec;
201
202 char cliIP[20];
203 char srvIP[20];
204 char trashIP1[20];
205 char trashIP2[20];
206
207 while (dc->nonstop)
208     {
209     for (int i = 8; i <= 252; i++)
210         {
211                 
212         usize = random()%100 + 100;
213                 dsize = random()%500 + 900;
214                 
215         for (int j = 2; j < 11; j++)
216             {
217                         sprintf(cliIP, "192.168.%d.%d", j, i);
218             sprintf(srvIP, "10.1.%d.%d", random()%8, 1);
219
220             rp = MakeTCPPacket(srvIP, cliIP, 80, random()%2 + 2000, dsize);
221             d += dsize;
222             dc->traffCnt->Process(rp);
223
224             rp = MakeTCPPacket(cliIP, srvIP, random()%2 + 2000, 80, usize);
225             u += usize;
226             dc->traffCnt->Process(rp);
227             }
228         }
229
230     usleep(100000);
231     t = stgTime;
232
233     if (min != localtime(&t)->tm_min)
234         {
235         min = localtime(&t)->tm_min;
236         WriteStat(u, d);
237         u = d = 0;
238         }
239
240     a++;
241
242     }
243
244 dc->isRunning = false;
245 return NULL;
246 }
247 //-----------------------------------------------------------------------------
248 void * DEBUG_CAP::Run2(void * data)
249 {
250 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
251
252 DEBUG_CAP * dc = (DEBUG_CAP *)data;
253 dc->isRunning = true;
254
255 RAW_PACKET rp;
256 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
257 int a = 0;
258 sleep(3);
259
260 struct tm * tm;
261 time_t t;
262 uint32_t u = 0;
263 uint32_t d = 0;
264
265 //2 upload : 3 download
266
267 int usize = 200;
268 int dsize = 1500;
269
270 t = stgTime;
271 tm = localtime(&t);
272 int min = tm->tm_min;
273
274 char cliIP[20];
275 char srvIP[20];
276
277 while (dc->nonstop)
278     {
279     for (int i = 101; i <= 150; i++)
280         {
281         sprintf(cliIP, "192.168.1.%d", i);
282         for (int dp = 0; dp < 1; dp++)
283             {
284             //sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
285             sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
286
287             rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
288             d += dsize;
289             dc->traffCnt->Process(rp);
290
291             rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
292             d += dsize;
293             dc->traffCnt->Process(rp);
294
295             rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
296             dc->traffCnt->Process(rp);
297             d += dsize;
298
299
300             rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
301             u += usize;
302             dc->traffCnt->Process(rp);
303
304             rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
305             u += usize;
306             dc->traffCnt->Process(rp);
307             }
308         }
309
310     //usleep(20000);
311     t = stgTime;
312
313     if (min != localtime(&t)->tm_min)
314         {
315         min = localtime(&t)->tm_min;
316         WriteStat(u, d);
317         u = d = 0;
318         }
319
320     a++;
321
322     }
323
324 dc->isRunning = false;
325 return NULL;
326 }
327 //-----------------------------------------------------------------------------
328 void * DEBUG_CAP::Run3(void * data)
329 {
330 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
331
332 DEBUG_CAP * dc = (DEBUG_CAP *)data;
333 dc->isRunning = true;
334
335 RAW_PACKET rp;
336 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
337 int a = 0;
338 sleep(3);
339
340 struct tm * tm;
341 time_t t;
342 uint32_t u = 0;
343 uint32_t d = 0;
344
345 //2 upload : 3 download
346
347 int usize = 200;
348 int dsize = 1500;
349
350 t = stgTime;
351 tm = localtime(&t);
352
353 char cliIP[20];
354 char srvIP1[20];
355 char srvIP2[20];
356 char srvIP3[20];
357
358 int firstTime = true;
359
360 while (dc->nonstop)
361     {
362     if (firstTime)
363         {
364         sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
365
366         sprintf(srvIP2, "%d.%d.%d.%d",
367                 random() % 20 + 81,
368                 random() % 28 + 153,
369                 random() % 28 + 37,
370                 random() % 28 + 13);
371
372         sprintf(srvIP3, "%d.%d.%d.%d",
373                 random() % 20 + 81,
374                 random() % 28 + 153,
375                 random() % 28 + 37,
376                 random() % 28 + 13);
377
378         printfd(__FILE__, "firstTime=false\n");
379         firstTime = false;
380         }
381
382     int rnd = random() % 400;
383     if (rnd < 5)
384         {
385         sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
386         printfd(__FILE__, "srvIP1=%s\n", srvIP1);
387         }
388     if (rnd == 9)
389         {
390         sprintf(srvIP2, "%d.%d.%d.%d",
391                 random() % 20 + 81,
392                 random() % 28 + 153,
393                 random() % 28 + 37,
394                 random() % 28 + 13);
395         printfd(__FILE__, "srvIP2=%s\n", srvIP2);
396         }
397     if (rnd == 5)
398         {
399         sprintf(srvIP2, "%d.%d.%d.%d",
400                 random() % 20 + 81,
401                 random() % 28 + 153,
402                 random() % 28 + 37,
403                 random() % 28 + 13);
404         printfd(__FILE__, "srvIP3=%s\n", srvIP3);
405         }
406
407     for (int i = 2; i < 52; i++)
408         {
409         sprintf(cliIP, "192.168.1.%d", i);
410         for (int dp = 0; dp < 1; dp++)
411             {
412             usize = 50 + random() % 100;
413             dsize = 1000 + random() % 400;
414
415             rp = MakeTCPPacket(srvIP1, cliIP, 80, 10000 + i + dp, dsize);
416             dc->traffCnt->Process(rp);
417
418             rp = MakeTCPPacket(srvIP2, cliIP, 80, 10000 + i + dp, dsize);
419             dc->traffCnt->Process(rp);
420
421             rp = MakeTCPPacket(srvIP3, cliIP, 80, 10000 + i + dp, dsize);
422             dc->traffCnt->Process(rp);
423
424
425             rp = MakeTCPPacket(cliIP, srvIP1, 10000 + i + dp, 80, usize);
426             dc->traffCnt->Process(rp);
427
428             rp = MakeTCPPacket(cliIP, srvIP2, 10000 + i + dp, 80, usize);
429             dc->traffCnt->Process(rp);
430
431             rp = MakeTCPPacket(cliIP, srvIP3, 10000 + i + dp, 80, usize);
432             dc->traffCnt->Process(rp);
433             }
434         }
435
436     usleep(300000);
437     /*t = stgTime;
438
439     if (min != localtime(&t)->tm_min)
440         {
441         min = localtime(&t)->tm_min;
442         WriteStat(u, d);
443         u = d = 0;
444         }*/
445
446     a++;
447
448     }
449
450 dc->isRunning = false;
451 return NULL;
452 }
453 //-----------------------------------------------------------------------------
454 uint16_t DEBUG_CAP::GetStartPosition() const
455 {
456 return 0;
457 }
458 //-----------------------------------------------------------------------------
459 uint16_t DEBUG_CAP::GetStopPosition() const
460 {
461 return 0;
462 }
463 //-----------------------------------------------------------------------------
464 RAW_PACKET MakeTCPPacket(const char * src,
465                          const char * dst,
466                          uint16_t sport,
467                          uint16_t dport,
468                          uint16_t len)
469 {
470 struct packet pkt;
471 if (pkt_init(&pkt, 0, 100))
472     {
473     printfd(__FILE__, "pkt_init error!\n");
474     }
475
476 in_addr_t sip = inet_addr(src);
477 in_addr_t dip = inet_addr(dst);
478
479 pkt_ip_header(&pkt,
480               5,        //header len
481               4,
482               0,
483               len,      //total len
484               0,
485               0,
486               64,        //ttl
487               6,         //TCP
488               0,
489               *(uint32_t*)&sip,
490               *(uint32_t*)&dip);
491
492 pkt_move_actptr(&pkt, 20);
493
494 pkt_tcp_header(&pkt,
495                sport,
496                dport,
497                1,           // seq,
498                1,           // ackseq,
499                5,           // headerlen,
500                0,           // reserved,
501                0,           // flags,
502                0,           // window,
503                0,           // checksum,
504                0);          // urgent
505
506 RAW_PACKET rp;
507 memcpy(&rp, pkt.pkt, sizeof(rp));
508
509 if (pkt_free(&pkt))
510     {
511     printfd(__FILE__, "pkt_free error!\n");
512     }
513 rp.dataLen = -1;
514 strcpy(rp.iface, "eth0");
515 return rp;
516 }
517 //-----------------------------------------------------------------------------
518
519