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>
41 #include "stg/plugin_creator.h"
42 #include "stg/traffcounter.h"
43 #include "debug_cap.h"
45 //-----------------------------------------------------------------------------
46 void WriteStat(uint32_t u, uint32_t d)
49 f = fopen("/tmp/cap.stat", "at");
50 fprintf(f, "up %5.2f Mbit, down %5.2f Mbit, sum %5.2f Mbit\n",
53 (u + d) / (1000000*8.0));
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
61 PLUGIN_CREATOR<DEBUG_CAP> cdc;
64 extern "C" BASE_PLUGIN * GetPlugin();
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 BASE_PLUGIN * GetPlugin()
70 return cdc.GetPlugin();
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 RAW_PACKET MakeTCPPacket(const char * src,
82 std::string DEBUG_CAP::GetVersion() const
84 return "cap_debug v.0.01a";
86 //-----------------------------------------------------------------------------
87 DEBUG_CAP::DEBUG_CAP()
92 //-----------------------------------------------------------------------------
93 void DEBUG_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
97 //-----------------------------------------------------------------------------
98 const std::string & DEBUG_CAP::GetStrError() const
102 //-----------------------------------------------------------------------------
103 int DEBUG_CAP::Start()
108 printfd(__FILE__, "DEBUG_CAP::Start()\n");
112 if (pthread_create(&thread, NULL, Run1, this) == 0)
117 errorStr = "Cannot create thread.";
120 //-----------------------------------------------------------------------------
121 int DEBUG_CAP::Stop()
128 //5 seconds to thread stops itself
130 for (i = 0; i < 25; i++)
142 //after 5 seconds waiting thread still running. now killing it
145 //TODO pthread_cancel()
146 if (pthread_kill(thread, SIGINT))
148 errorStr = "Cannot kill thread.";
155 //-----------------------------------------------------------------------------
156 bool DEBUG_CAP::IsRunning()
160 //-----------------------------------------------------------------------------
161 void * DEBUG_CAP::Run1(void * data)
163 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
165 DEBUG_CAP * dc = (DEBUG_CAP *)data;
166 dc->isRunning = true;
169 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
178 //2 upload : 3 download
185 int min = tm->tm_min;
186 int sec = tm->tm_sec;
195 for (int i = 8; i <= 252; i++)
198 usize = random()%100 + 100;
199 dsize = random()%500 + 900;
201 for (int j = 2; j < 11; j++)
203 sprintf(cliIP, "192.168.%d.%d", j, i);
204 sprintf(srvIP, "10.1.%d.%d", random()%8, 1);
206 rp = MakeTCPPacket(srvIP, cliIP, 80, random()%2 + 2000, dsize);
208 dc->traffCnt->Process(rp);
210 rp = MakeTCPPacket(cliIP, srvIP, random()%2 + 2000, 80, usize);
212 dc->traffCnt->Process(rp);
219 if (min != localtime(&t)->tm_min)
221 min = localtime(&t)->tm_min;
230 dc->isRunning = false;
233 //-----------------------------------------------------------------------------
234 void * DEBUG_CAP::Run2(void * data)
236 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
238 DEBUG_CAP * dc = (DEBUG_CAP *)data;
239 dc->isRunning = true;
242 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
251 //2 upload : 3 download
258 int min = tm->tm_min;
265 for (int i = 101; i <= 150; i++)
267 sprintf(cliIP, "192.168.1.%d", i);
268 for (int dp = 0; dp < 1; dp++)
270 //sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
271 sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
273 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
275 dc->traffCnt->Process(rp);
277 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
279 dc->traffCnt->Process(rp);
281 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
282 dc->traffCnt->Process(rp);
286 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
288 dc->traffCnt->Process(rp);
290 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
292 dc->traffCnt->Process(rp);
299 if (min != localtime(&t)->tm_min)
301 min = localtime(&t)->tm_min;
310 dc->isRunning = false;
313 //-----------------------------------------------------------------------------
314 void * DEBUG_CAP::Run3(void * data)
316 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
318 DEBUG_CAP * dc = (DEBUG_CAP *)data;
319 dc->isRunning = true;
322 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
331 //2 upload : 3 download
344 int firstTime = true;
350 sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
352 sprintf(srvIP2, "%d.%d.%d.%d",
358 sprintf(srvIP3, "%d.%d.%d.%d",
364 printfd(__FILE__, "firstTime=false\n");
368 int rnd = random() % 400;
371 sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
372 printfd(__FILE__, "srvIP1=%s\n", srvIP1);
376 sprintf(srvIP2, "%d.%d.%d.%d",
381 printfd(__FILE__, "srvIP2=%s\n", srvIP2);
385 sprintf(srvIP2, "%d.%d.%d.%d",
390 printfd(__FILE__, "srvIP3=%s\n", srvIP3);
393 for (int i = 2; i < 52; i++)
395 sprintf(cliIP, "192.168.1.%d", i);
396 for (int dp = 0; dp < 1; dp++)
398 usize = 50 + random() % 100;
399 dsize = 1000 + random() % 400;
401 rp = MakeTCPPacket(srvIP1, cliIP, 80, 10000 + i + dp, dsize);
402 dc->traffCnt->Process(rp);
404 rp = MakeTCPPacket(srvIP2, cliIP, 80, 10000 + i + dp, dsize);
405 dc->traffCnt->Process(rp);
407 rp = MakeTCPPacket(srvIP3, cliIP, 80, 10000 + i + dp, dsize);
408 dc->traffCnt->Process(rp);
411 rp = MakeTCPPacket(cliIP, srvIP1, 10000 + i + dp, 80, usize);
412 dc->traffCnt->Process(rp);
414 rp = MakeTCPPacket(cliIP, srvIP2, 10000 + i + dp, 80, usize);
415 dc->traffCnt->Process(rp);
417 rp = MakeTCPPacket(cliIP, srvIP3, 10000 + i + dp, 80, usize);
418 dc->traffCnt->Process(rp);
425 if (min != localtime(&t)->tm_min)
427 min = localtime(&t)->tm_min;
436 dc->isRunning = false;
439 //-----------------------------------------------------------------------------
440 uint16_t DEBUG_CAP::GetStartPosition() const
444 //-----------------------------------------------------------------------------
445 uint16_t DEBUG_CAP::GetStopPosition() const
449 //-----------------------------------------------------------------------------
450 RAW_PACKET MakeTCPPacket(const char * src,
457 if (pkt_init(&pkt, 0, 100))
459 printfd(__FILE__, "pkt_init error!\n");
462 in_addr_t sip = inet_addr(src);
463 in_addr_t dip = inet_addr(dst);
478 pkt_move_actptr(&pkt, 20);
493 memcpy(&rp, pkt.pkt, sizeof(rp));
497 printfd(__FILE__, "pkt_free error!\n");
500 strcpy(rp.iface, "eth0");
503 //-----------------------------------------------------------------------------