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.
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.
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
22 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
27 $Date: 2009/03/19 20:03:35 $
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
40 #include "debug_cap.h"
41 #include "../../../traffcounter.h"
44 //-----------------------------------------------------------------------------
45 void WriteStat(uint32_t u, uint32_t d)
48 f = fopen("/tmp/cap.stat", "at");
49 fprintf(f, "up %5.2f Mbit, down %5.2f Mbit, sum %5.2f Mbit\n",
52 (u + d) / (1000000*8.0));
55 //-----------------------------------------------------------------------------
57 class CAP_DEBUG_CREATOR
72 DEBUG_CAP * GetCapturer()
77 //-----------------------------------------------------------------------------
78 RAW_PACKET MakeTCPPacket(const char * src,
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 CAP_DEBUG_CREATOR cdc;
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 BASE_PLUGIN * GetPlugin()
91 return cdc.GetCapturer();
93 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 const string DEBUG_CAP::GetVersion() const
98 return "Debug_cap v.0.01a";
100 //-----------------------------------------------------------------------------
101 DEBUG_CAP::DEBUG_CAP()
106 //-----------------------------------------------------------------------------
107 void DEBUG_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
111 //-----------------------------------------------------------------------------
112 const string & DEBUG_CAP::GetStrError() const
116 //-----------------------------------------------------------------------------
117 int DEBUG_CAP::Start()
122 printfd(__FILE__, "DEBUG_CAP::Start()\n");
126 if (pthread_create(&thread, NULL, Run1, this) == 0)
131 errorStr = "Cannot create thread.";
134 //-----------------------------------------------------------------------------
135 int DEBUG_CAP::Stop()
142 //5 seconds to thread stops itself
144 for (i = 0; i < 25; i++)
156 //after 5 seconds waiting thread still running. now killing it
159 //TODO pthread_cancel()
160 if (pthread_kill(thread, SIGINT))
162 errorStr = "Cannot kill thread.";
169 //-----------------------------------------------------------------------------
170 bool DEBUG_CAP::IsRunning()
174 //-----------------------------------------------------------------------------
175 void * DEBUG_CAP::Run1(void * data)
177 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
179 DEBUG_CAP * dc = (DEBUG_CAP *)data;
180 dc->isRunning = true;
183 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
192 //2 upload : 3 download
199 int min = tm->tm_min;
200 int sec = tm->tm_sec;
209 for (int i = 8; i <= 252; i++)
212 usize = random()%100 + 100;
213 dsize = random()%500 + 900;
215 for (int j = 2; j < 11; j++)
217 sprintf(cliIP, "192.168.%d.%d", j, i);
218 sprintf(srvIP, "10.1.%d.%d", random()%8, 1);
220 rp = MakeTCPPacket(srvIP, cliIP, 80, random()%2 + 2000, dsize);
222 dc->traffCnt->Process(rp);
224 rp = MakeTCPPacket(cliIP, srvIP, random()%2 + 2000, 80, usize);
226 dc->traffCnt->Process(rp);
233 if (min != localtime(&t)->tm_min)
235 min = localtime(&t)->tm_min;
244 dc->isRunning = false;
247 //-----------------------------------------------------------------------------
248 void * DEBUG_CAP::Run2(void * data)
250 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
252 DEBUG_CAP * dc = (DEBUG_CAP *)data;
253 dc->isRunning = true;
256 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
265 //2 upload : 3 download
272 int min = tm->tm_min;
279 for (int i = 101; i <= 150; i++)
281 sprintf(cliIP, "192.168.1.%d", i);
282 for (int dp = 0; dp < 1; dp++)
284 //sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
285 sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
287 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
289 dc->traffCnt->Process(rp);
291 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
293 dc->traffCnt->Process(rp);
295 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
296 dc->traffCnt->Process(rp);
300 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
302 dc->traffCnt->Process(rp);
304 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
306 dc->traffCnt->Process(rp);
313 if (min != localtime(&t)->tm_min)
315 min = localtime(&t)->tm_min;
324 dc->isRunning = false;
327 //-----------------------------------------------------------------------------
328 void * DEBUG_CAP::Run3(void * data)
330 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
332 DEBUG_CAP * dc = (DEBUG_CAP *)data;
333 dc->isRunning = true;
336 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
345 //2 upload : 3 download
358 int firstTime = true;
364 sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
366 sprintf(srvIP2, "%d.%d.%d.%d",
372 sprintf(srvIP3, "%d.%d.%d.%d",
378 printfd(__FILE__, "firstTime=false\n");
382 int rnd = random() % 400;
385 sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
386 printfd(__FILE__, "srvIP1=%s\n", srvIP1);
390 sprintf(srvIP2, "%d.%d.%d.%d",
395 printfd(__FILE__, "srvIP2=%s\n", srvIP2);
399 sprintf(srvIP2, "%d.%d.%d.%d",
404 printfd(__FILE__, "srvIP3=%s\n", srvIP3);
407 for (int i = 2; i < 52; i++)
409 sprintf(cliIP, "192.168.1.%d", i);
410 for (int dp = 0; dp < 1; dp++)
412 usize = 50 + random() % 100;
413 dsize = 1000 + random() % 400;
415 rp = MakeTCPPacket(srvIP1, cliIP, 80, 10000 + i + dp, dsize);
416 dc->traffCnt->Process(rp);
418 rp = MakeTCPPacket(srvIP2, cliIP, 80, 10000 + i + dp, dsize);
419 dc->traffCnt->Process(rp);
421 rp = MakeTCPPacket(srvIP3, cliIP, 80, 10000 + i + dp, dsize);
422 dc->traffCnt->Process(rp);
425 rp = MakeTCPPacket(cliIP, srvIP1, 10000 + i + dp, 80, usize);
426 dc->traffCnt->Process(rp);
428 rp = MakeTCPPacket(cliIP, srvIP2, 10000 + i + dp, 80, usize);
429 dc->traffCnt->Process(rp);
431 rp = MakeTCPPacket(cliIP, srvIP3, 10000 + i + dp, 80, usize);
432 dc->traffCnt->Process(rp);
439 if (min != localtime(&t)->tm_min)
441 min = localtime(&t)->tm_min;
450 dc->isRunning = false;
453 //-----------------------------------------------------------------------------
454 uint16_t DEBUG_CAP::GetStartPosition() const
458 //-----------------------------------------------------------------------------
459 uint16_t DEBUG_CAP::GetStopPosition() const
463 //-----------------------------------------------------------------------------
464 RAW_PACKET MakeTCPPacket(const char * src,
471 if (pkt_init(&pkt, 0, 100))
473 printfd(__FILE__, "pkt_init error!\n");
476 in_addr_t sip = inet_addr(src);
477 in_addr_t dip = inet_addr(dst);
492 pkt_move_actptr(&pkt, 20);
507 memcpy(&rp, pkt.pkt, sizeof(rp));
511 printfd(__FILE__, "pkt_free error!\n");
514 strcpy(rp.iface, "eth0");
517 //-----------------------------------------------------------------------------