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