3 * - server: 192.168.0.1
4 * - user A: 192.168.0.2
5 * - user B: 192.168.0.3
8 * - host 1: 216.239.59.104
9 * - host 2: 72.14.221.104
10 * - host 3: 66.249.93.104
11 * - host 4: 195.5.61.68
14 * - Local: ALL 192.168.0.0/24
15 * - DNS: TCP_UDP 195.5.61.68/32:53
16 * - FTP: TCP 129.22.8.159/32:20-21
17 * - World: ALL 0.0.0.0/0
27 #include <arpa/inet.h>
28 #include <netinet/ip.h>
32 #include "traffcounter.h"
38 class StatPrinter: public unary_function<const TRAFF_ITEM &, void> {
40 void operator()(const TRAFF_ITEM & item) const
42 LOG_IT << inet_ntoa(*(in_addr *)(&item.first.saddr));
43 cout << ":" << item.first.sport;
44 cout << " -> " << inet_ntoa(*(in_addr *)(&item.first.daddr));
45 cout << ":" << item.first.dport;
46 cout << "\tproto: " << item.first.proto;
47 cout << "\tlength: " << item.second.length;
61 RULE MakeRule(const string & ip,
70 rule.ip = inet_addr(ip.c_str());
71 rule.mask = inet_addr(mask.c_str());
83 RULE local(MakeRule("192.168.0.0",
89 RULE dns(MakeRule("195.5.61.68",
95 RULE ftp(MakeRule("129.22.8.159",
101 RULE world(MakeRule("0.0.0.0",
108 rules.push_back(local);
110 rules.push_back(dns);
112 rules.push_back(ftp);
114 rules.push_back(world);
119 iphdr MakePacket(const string & from,
129 hdr.tot_len = length;
133 hdr.protocol = proto;
135 hdr.saddr = inet_addr(from.c_str());
136 hdr.daddr = inet_addr(to.c_str());
141 void PrepareTraffic(vector<PACKET> & pckts,
153 outpacket.hdr = skel;
155 outpacket.hdr.saddr ^= outpacket.hdr.daddr;
156 outpacket.hdr.daddr ^= outpacket.hdr.saddr;
157 outpacket.hdr.saddr ^= outpacket.hdr.daddr;
159 inpacket.sport = sport;
160 inpacket.dport = dport;
161 outpacket.sport = dport;
162 outpacket.dport = sport;
164 inpacket.hdr.tot_len = in / packets;
165 outpacket.hdr.tot_len = out / packets;
167 for (int i = 0; i < packets; ++i) {
168 //inpacket.hdr.daddr = outpacket.hdr.saddr = rand() * 32768 + rand();
169 pckts.push_back(inpacket);
170 pckts.push_back(outpacket);
174 struct TC_TESTER : public unary_function<const PACKET &, void>
177 TC_TESTER(TRAFFCOUNTER & t)
181 void operator () (const PACKET & p)
183 tc.AddPacket(p.hdr, p.sport, p.dport);
191 RULES rules(PrepareRules());
195 vector<PACKET> packets;
200 LOG_IT << "::main() Error: traffcounter not started" << endl;
204 tc.AddIP(inet_addr("192.168.0.1")); // Server
205 tc.AddIP(inet_addr("192.168.0.2")); // User A
206 tc.AddIP(inet_addr("192.168.0.3")); // User B
208 for (int i = 0; i < 10000; ++i) {
209 tc.AddIP(rand() * 32768 + rand());
229 gettimeofday(&tv_from, NULL);
230 // S - local, A - local
231 PrepareTraffic(packets, MakePacket("192.168.0.2", "192.168.0.1", 6, 0), 3215, 20, 1024 * 1024, 2048 * 1024, 512 * 2);
232 // S - local, B - local
233 PrepareTraffic(packets, MakePacket("192.168.0.3", "192.168.0.1", 6, 0), 5432, 22, 2048 * 1024, 1024 * 1024, 512 * 2);
234 // A - local, B - local
235 PrepareTraffic(packets, MakePacket("192.168.0.3", "192.168.0.2", 6, 0), 9875, 21, 2048 * 1024, 2048 * 1024, 512 * 2);
237 //PrepareTraffic(packets, MakePacket("192.168.0.2", "195.5.61.68", 6, 0), 4521, 53, 1024 * 1024, 2048 * 1024, 512 * 2);
239 //PrepareTraffic(packets, MakePacket("192.168.0.2", "195.5.61.68", 6, 0), 4521, 80, 1024 * 1024, 2048 * 1024, 512 * 2);
241 //PrepareTraffic(packets, MakePacket("192.168.0.2", "129.22.8.159", 6, 0), 4522, 20, 512 * 1024, 512 * 1024, 512 * 2);
243 //PrepareTraffic(packets, MakePacket("192.168.0.2", "129.22.8.159", 6, 0), 4522, 21, 512 * 1024, 4096 * 1024, 512 * 2);
245 //PrepareTraffic(packets, MakePacket("192.168.0.3", "66.249.93.104", 6, 0), 3541, 80, 1024 * 1024, 1024 * 1024, 512 * 2);
246 gettimeofday(&tv_to, NULL);
248 uint64_t diff = tv_to.tv_sec - tv_from.tv_sec;
250 diff -= tv_from.tv_usec;
251 diff += tv_to.tv_usec;
253 LOG_IT << "::main() Prepared " << packets.size() << " packets by " << diff << " usecs" << endl;
255 gettimeofday(&tv_from, NULL);
256 for_each(packets.begin(),
259 gettimeofday(&tv_to, NULL);
261 diff = tv_to.tv_sec - tv_from.tv_sec;
263 diff -= tv_from.tv_usec;
264 diff += tv_to.tv_usec;
266 LOG_IT << "::main() Recorded " << packets.size() << " packets by " << diff << " usecs" << endl;
269 while ((p = tc.PendingCount())) {
270 LOG_IT << "::main() Pending packets: " << p << " at " << time(NULL) << endl;
276 tc.DeleteIP(inet_addr("192.168.0.1"), &data);
277 for_each(data.begin(),
280 data.erase(data.begin(), data.end());
281 tc.DeleteIP(inet_addr("192.168.0.2"), &data);
282 for_each(data.begin(),
285 data.erase(data.begin(), data.end());
286 tc.DeleteIP(inet_addr("192.168.0.3"), &data);
287 for_each(data.begin(),
290 data.erase(data.begin(), data.end());
293 LOG_IT << "::main() Error: traffcounter not stopped" << endl;
297 LOG_IT << "::main() Sessions: " << tc.SessionsCount() << endl;
298 LOG_IT << "::main() Cache hits: " << tc.CacheHits() << endl;
299 LOG_IT << "::main() Cache misses: " << tc.CacheMisses() << endl;
300 LOG_IT << "::main() Stream quality: " << tc.StreamQuality() << endl;