]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/debug_cap.cpp
Massive refactoring.
[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 "libpal.h"
41 #include "stg/plugin_creator.h"
42 #include "stg/traffcounter.h"
43 #include "debug_cap.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 namespace
60 {
61 PLUGIN_CREATOR<DEBUG_CAP> cdc;
62 }
63
64 extern "C" BASE_PLUGIN * GetPlugin();
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 BASE_PLUGIN * GetPlugin()
69 {
70 return cdc.GetPlugin();
71 }
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75
76 //-----------------------------------------------------------------------------
77 RAW_PACKET MakeTCPPacket(const char * src,
78                          const char * dst,
79                          uint16_t sport,
80                          uint16_t dport,
81                          uint16_t len);
82 std::string DEBUG_CAP::GetVersion() const
83 {
84 return "Debug_cap v.0.01a";
85 }
86 //-----------------------------------------------------------------------------
87 DEBUG_CAP::DEBUG_CAP()
88 {
89 isRunning = false;
90 nonstop = false;
91 }
92 //-----------------------------------------------------------------------------
93 void DEBUG_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
94 {
95 traffCnt = tc;
96 }
97 //-----------------------------------------------------------------------------
98 const std::string & DEBUG_CAP::GetStrError() const
99 {
100 return errorStr;
101 }
102 //-----------------------------------------------------------------------------
103 int DEBUG_CAP::Start()
104 {
105 if (isRunning)
106     return 0;
107
108 printfd(__FILE__, "DEBUG_CAP::Start()\n");
109
110 nonstop = true;
111
112 if (pthread_create(&thread, NULL, Run1, this) == 0)
113     {
114     return 0;
115     }
116
117 errorStr = "Cannot create thread.";
118 return -1;
119 }
120 //-----------------------------------------------------------------------------
121 int DEBUG_CAP::Stop()
122 {
123 if (!isRunning)
124     return 0;
125
126 nonstop = false;
127
128 //5 seconds to thread stops itself
129 int i;
130 for (i = 0; i < 25; i++)
131     {
132     if (!isRunning)
133         break;
134
135     stgUsleep(200000);
136     //printf(".");
137     }
138
139 /*if (i)
140     printf("\n");*/
141
142 //after 5 seconds waiting thread still running. now killing it
143 if (isRunning)
144     {
145     //TODO pthread_cancel()
146     if (pthread_kill(thread, SIGINT))
147         {
148         errorStr = "Cannot kill thread.";
149         return -1;
150         }
151     }
152
153 return 0;
154 }
155 //-----------------------------------------------------------------------------
156 bool DEBUG_CAP::IsRunning()
157 {
158 return nonstop;
159 }
160 //-----------------------------------------------------------------------------
161 void * DEBUG_CAP::Run1(void * data)
162 {
163 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
164
165 DEBUG_CAP * dc = (DEBUG_CAP *)data;
166 dc->isRunning = true;
167
168 RAW_PACKET rp;
169 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
170 int a = 0;
171 sleep(3);
172
173 struct tm * tm;
174 time_t t;
175 uint32_t u = 0;
176 uint32_t d = 0;
177
178 //2 upload : 3 download
179
180 int usize;
181 int dsize;
182
183 t = stgTime;
184 tm = localtime(&t);
185 int min = tm->tm_min;
186 int sec = tm->tm_sec;
187
188 char cliIP[20];
189 char srvIP[20];
190 char trashIP1[20];
191 char trashIP2[20];
192
193 while (dc->nonstop)
194     {
195     for (int i = 8; i <= 252; i++)
196         {
197                 
198         usize = random()%100 + 100;
199                 dsize = random()%500 + 900;
200                 
201         for (int j = 2; j < 11; j++)
202             {
203                         sprintf(cliIP, "192.168.%d.%d", j, i);
204             sprintf(srvIP, "10.1.%d.%d", random()%8, 1);
205
206             rp = MakeTCPPacket(srvIP, cliIP, 80, random()%2 + 2000, dsize);
207             d += dsize;
208             dc->traffCnt->Process(rp);
209
210             rp = MakeTCPPacket(cliIP, srvIP, random()%2 + 2000, 80, usize);
211             u += usize;
212             dc->traffCnt->Process(rp);
213             }
214         }
215
216     usleep(100000);
217     t = stgTime;
218
219     if (min != localtime(&t)->tm_min)
220         {
221         min = localtime(&t)->tm_min;
222         WriteStat(u, d);
223         u = d = 0;
224         }
225
226     a++;
227
228     }
229
230 dc->isRunning = false;
231 return NULL;
232 }
233 //-----------------------------------------------------------------------------
234 void * DEBUG_CAP::Run2(void * data)
235 {
236 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
237
238 DEBUG_CAP * dc = (DEBUG_CAP *)data;
239 dc->isRunning = true;
240
241 RAW_PACKET rp;
242 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
243 int a = 0;
244 sleep(3);
245
246 struct tm * tm;
247 time_t t;
248 uint32_t u = 0;
249 uint32_t d = 0;
250
251 //2 upload : 3 download
252
253 int usize = 200;
254 int dsize = 1500;
255
256 t = stgTime;
257 tm = localtime(&t);
258 int min = tm->tm_min;
259
260 char cliIP[20];
261 char srvIP[20];
262
263 while (dc->nonstop)
264     {
265     for (int i = 101; i <= 150; i++)
266         {
267         sprintf(cliIP, "192.168.1.%d", i);
268         for (int dp = 0; dp < 1; dp++)
269             {
270             //sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
271             sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
272
273             rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
274             d += dsize;
275             dc->traffCnt->Process(rp);
276
277             rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
278             d += dsize;
279             dc->traffCnt->Process(rp);
280
281             rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
282             dc->traffCnt->Process(rp);
283             d += dsize;
284
285
286             rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
287             u += usize;
288             dc->traffCnt->Process(rp);
289
290             rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
291             u += usize;
292             dc->traffCnt->Process(rp);
293             }
294         }
295
296     //usleep(20000);
297     t = stgTime;
298
299     if (min != localtime(&t)->tm_min)
300         {
301         min = localtime(&t)->tm_min;
302         WriteStat(u, d);
303         u = d = 0;
304         }
305
306     a++;
307
308     }
309
310 dc->isRunning = false;
311 return NULL;
312 }
313 //-----------------------------------------------------------------------------
314 void * DEBUG_CAP::Run3(void * data)
315 {
316 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
317
318 DEBUG_CAP * dc = (DEBUG_CAP *)data;
319 dc->isRunning = true;
320
321 RAW_PACKET rp;
322 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
323 int a = 0;
324 sleep(3);
325
326 struct tm * tm;
327 time_t t;
328 uint32_t u = 0;
329 uint32_t d = 0;
330
331 //2 upload : 3 download
332
333 int usize = 200;
334 int dsize = 1500;
335
336 t = stgTime;
337 tm = localtime(&t);
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 strcpy(rp.iface, "eth0");
501 return rp;
502 }
503 //-----------------------------------------------------------------------------
504
505