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 $
31 #include "debug_cap.h"
35 #include "stg/plugin_creator.h"
36 #include "stg/traffcounter.h"
37 #include "stg/common.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 extern volatile time_t stgTime;
50 //-----------------------------------------------------------------------------
51 void WriteStat(uint32_t u, uint32_t d)
54 f = fopen("/tmp/cap.stat", "at");
55 fprintf(f, "up %5.2f Mbit, down %5.2f Mbit, sum %5.2f Mbit\n",
58 (u + d) / (1000000*8.0));
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
66 PLUGIN_CREATOR<DEBUG_CAP> cdc;
69 extern "C" PLUGIN * GetPlugin();
70 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
75 return cdc.GetPlugin();
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 RAW_PACKET MakeTCPPacket(const char * src,
87 std::string DEBUG_CAP::GetVersion() const
89 return "cap_debug v.0.01a";
91 //-----------------------------------------------------------------------------
92 DEBUG_CAP::DEBUG_CAP()
98 //-----------------------------------------------------------------------------
99 void DEBUG_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
103 //-----------------------------------------------------------------------------
104 const std::string & DEBUG_CAP::GetStrError() const
108 //-----------------------------------------------------------------------------
109 int DEBUG_CAP::Start()
114 printfd(__FILE__, "DEBUG_CAP::Start()\n");
118 if (pthread_create(&thread, NULL, Run1, this) == 0)
123 errorStr = "Cannot create thread.";
126 //-----------------------------------------------------------------------------
127 int DEBUG_CAP::Stop()
134 //5 seconds to thread stops itself
136 for (i = 0; i < 25; i++)
148 //after 5 seconds waiting thread still running. now killing it
151 //TODO pthread_cancel()
152 if (pthread_kill(thread, SIGINT))
154 errorStr = "Cannot kill thread.";
161 //-----------------------------------------------------------------------------
162 bool DEBUG_CAP::IsRunning()
166 //-----------------------------------------------------------------------------
167 void * DEBUG_CAP::Run1(void * data)
169 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
171 DEBUG_CAP * dc = static_cast<DEBUG_CAP *>(data);
172 dc->isRunning = true;
175 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
184 //2 upload : 3 download
191 int min = tm->tm_min;
198 for (int i = 8; i <= 252; i++)
200 usize = random()%100 + 100;
201 dsize = random()%500 + 900;
203 for (int j = 2; j < 11; j++)
205 sprintf(cliIP, "192.168.%d.%d", j, i);
206 sprintf(srvIP, "10.1.%d.%d", random()%8, 1);
208 rp = MakeTCPPacket(srvIP, cliIP, 80, random()%2 + 2000, dsize);
210 dc->traffCnt->Process(rp);
212 rp = MakeTCPPacket(cliIP, srvIP, random()%2 + 2000, 80, usize);
214 dc->traffCnt->Process(rp);
221 if (min != localtime(&t)->tm_min)
223 min = localtime(&t)->tm_min;
232 dc->isRunning = false;
235 //-----------------------------------------------------------------------------
236 void * DEBUG_CAP::Run2(void * data)
238 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
240 DEBUG_CAP * dc = static_cast<DEBUG_CAP *>(data);
241 dc->isRunning = true;
244 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
253 //2 upload : 3 download
260 int min = tm->tm_min;
267 for (int i = 101; i <= 150; i++)
269 sprintf(cliIP, "192.168.1.%d", i);
270 for (int dp = 0; dp < 1; dp++)
272 //sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
273 sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
275 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
277 dc->traffCnt->Process(rp);
279 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
281 dc->traffCnt->Process(rp);
283 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
284 dc->traffCnt->Process(rp);
288 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
290 dc->traffCnt->Process(rp);
292 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
294 dc->traffCnt->Process(rp);
301 if (min != localtime(&t)->tm_min)
303 min = localtime(&t)->tm_min;
312 dc->isRunning = false;
315 //-----------------------------------------------------------------------------
316 void * DEBUG_CAP::Run3(void * data)
318 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
320 DEBUG_CAP * dc = static_cast<DEBUG_CAP *>(data);
321 dc->isRunning = true;
324 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
332 //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");
502 //-----------------------------------------------------------------------------