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 "stg/plugin_creator.h"
41 #include "debug_cap.h"
42 #include "../../../traffcounter.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 //-----------------------------------------------------------------------------
59 PLUGIN_CREATOR<DEBUG_CAP> cdc;
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 BASE_PLUGIN * GetPlugin()
65 return cdc.GetPlugin();
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
72 RAW_PACKET MakeTCPPacket(const char * src,
77 const string DEBUG_CAP::GetVersion() const
79 return "Debug_cap v.0.01a";
81 //-----------------------------------------------------------------------------
82 DEBUG_CAP::DEBUG_CAP()
87 //-----------------------------------------------------------------------------
88 void DEBUG_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
92 //-----------------------------------------------------------------------------
93 const string & DEBUG_CAP::GetStrError() const
97 //-----------------------------------------------------------------------------
98 int DEBUG_CAP::Start()
103 printfd(__FILE__, "DEBUG_CAP::Start()\n");
107 if (pthread_create(&thread, NULL, Run1, this) == 0)
112 errorStr = "Cannot create thread.";
115 //-----------------------------------------------------------------------------
116 int DEBUG_CAP::Stop()
123 //5 seconds to thread stops itself
125 for (i = 0; i < 25; i++)
137 //after 5 seconds waiting thread still running. now killing it
140 //TODO pthread_cancel()
141 if (pthread_kill(thread, SIGINT))
143 errorStr = "Cannot kill thread.";
150 //-----------------------------------------------------------------------------
151 bool DEBUG_CAP::IsRunning()
155 //-----------------------------------------------------------------------------
156 void * DEBUG_CAP::Run1(void * data)
158 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
160 DEBUG_CAP * dc = (DEBUG_CAP *)data;
161 dc->isRunning = true;
164 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
173 //2 upload : 3 download
180 int min = tm->tm_min;
181 int sec = tm->tm_sec;
190 for (int i = 8; i <= 252; i++)
193 usize = random()%100 + 100;
194 dsize = random()%500 + 900;
196 for (int j = 2; j < 11; j++)
198 sprintf(cliIP, "192.168.%d.%d", j, i);
199 sprintf(srvIP, "10.1.%d.%d", random()%8, 1);
201 rp = MakeTCPPacket(srvIP, cliIP, 80, random()%2 + 2000, dsize);
203 dc->traffCnt->Process(rp);
205 rp = MakeTCPPacket(cliIP, srvIP, random()%2 + 2000, 80, usize);
207 dc->traffCnt->Process(rp);
214 if (min != localtime(&t)->tm_min)
216 min = localtime(&t)->tm_min;
225 dc->isRunning = false;
228 //-----------------------------------------------------------------------------
229 void * DEBUG_CAP::Run2(void * data)
231 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
233 DEBUG_CAP * dc = (DEBUG_CAP *)data;
234 dc->isRunning = true;
237 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
246 //2 upload : 3 download
253 int min = tm->tm_min;
260 for (int i = 101; i <= 150; i++)
262 sprintf(cliIP, "192.168.1.%d", i);
263 for (int dp = 0; dp < 1; dp++)
265 //sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
266 sprintf(srvIP, "10.1.%d.%d", i, 10 + dp);
268 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
270 dc->traffCnt->Process(rp);
272 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
274 dc->traffCnt->Process(rp);
276 rp = MakeTCPPacket(srvIP, cliIP, 80, 10000 + i + dp, dsize);
277 dc->traffCnt->Process(rp);
281 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
283 dc->traffCnt->Process(rp);
285 rp = MakeTCPPacket(cliIP, srvIP, 10000 + i + dp, 80, usize);
287 dc->traffCnt->Process(rp);
294 if (min != localtime(&t)->tm_min)
296 min = localtime(&t)->tm_min;
305 dc->isRunning = false;
308 //-----------------------------------------------------------------------------
309 void * DEBUG_CAP::Run3(void * data)
311 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
313 DEBUG_CAP * dc = (DEBUG_CAP *)data;
314 dc->isRunning = true;
317 rp = MakeTCPPacket("192.168.1.1", "192.168.1.21", 255, 255, 200);
326 //2 upload : 3 download
339 int firstTime = true;
345 sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
347 sprintf(srvIP2, "%d.%d.%d.%d",
353 sprintf(srvIP3, "%d.%d.%d.%d",
359 printfd(__FILE__, "firstTime=false\n");
363 int rnd = random() % 400;
366 sprintf(srvIP1, "10.1.%d.%d", random() % 14 + 153, random() % 11 + 35);
367 printfd(__FILE__, "srvIP1=%s\n", srvIP1);
371 sprintf(srvIP2, "%d.%d.%d.%d",
376 printfd(__FILE__, "srvIP2=%s\n", srvIP2);
380 sprintf(srvIP2, "%d.%d.%d.%d",
385 printfd(__FILE__, "srvIP3=%s\n", srvIP3);
388 for (int i = 2; i < 52; i++)
390 sprintf(cliIP, "192.168.1.%d", i);
391 for (int dp = 0; dp < 1; dp++)
393 usize = 50 + random() % 100;
394 dsize = 1000 + random() % 400;
396 rp = MakeTCPPacket(srvIP1, cliIP, 80, 10000 + i + dp, dsize);
397 dc->traffCnt->Process(rp);
399 rp = MakeTCPPacket(srvIP2, cliIP, 80, 10000 + i + dp, dsize);
400 dc->traffCnt->Process(rp);
402 rp = MakeTCPPacket(srvIP3, cliIP, 80, 10000 + i + dp, dsize);
403 dc->traffCnt->Process(rp);
406 rp = MakeTCPPacket(cliIP, srvIP1, 10000 + i + dp, 80, usize);
407 dc->traffCnt->Process(rp);
409 rp = MakeTCPPacket(cliIP, srvIP2, 10000 + i + dp, 80, usize);
410 dc->traffCnt->Process(rp);
412 rp = MakeTCPPacket(cliIP, srvIP3, 10000 + i + dp, 80, usize);
413 dc->traffCnt->Process(rp);
420 if (min != localtime(&t)->tm_min)
422 min = localtime(&t)->tm_min;
431 dc->isRunning = false;
434 //-----------------------------------------------------------------------------
435 uint16_t DEBUG_CAP::GetStartPosition() const
439 //-----------------------------------------------------------------------------
440 uint16_t DEBUG_CAP::GetStopPosition() const
444 //-----------------------------------------------------------------------------
445 RAW_PACKET MakeTCPPacket(const char * src,
452 if (pkt_init(&pkt, 0, 100))
454 printfd(__FILE__, "pkt_init error!\n");
457 in_addr_t sip = inet_addr(src);
458 in_addr_t dip = inet_addr(dst);
473 pkt_move_actptr(&pkt, 20);
488 memcpy(&rp, pkt.pkt, sizeof(rp));
492 printfd(__FILE__, "pkt_free error!\n");
495 strcpy(rp.iface, "eth0");
498 //-----------------------------------------------------------------------------