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