]> git.stg.codes - stg.git/blob - projects/traffcounter/tc_tester.cpp
Добавление исходников
[stg.git] / projects / traffcounter / tc_tester.cpp
1 /*
2  *  Network:
3  *   - server: 192.168.0.1
4  *   - user A: 192.168.0.2
5  *   - user B: 192.168.0.3
6  *
7  *  External resources:
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
12  *   
13  *  Directions:
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
18  *
19  */
20
21
22
23 #include <iostream>
24 #include <algorithm>
25 #include <functional>
26
27 #include <arpa/inet.h>
28 #include <netinet/ip.h>
29 #include <sys/time.h>
30
31 #include "rules.h"
32 #include "traffcounter.h"
33 #include "logger.h"
34
35 using namespace std;
36 using namespace STG;
37
38 class StatPrinter: public unary_function<const TRAFF_ITEM &, void> {
39 public:
40     void operator()(const TRAFF_ITEM & item) const
41     {
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;
48         cout   << endl;
49     }
50 };
51
52 STGLogger log;
53
54 struct PACKET
55 {
56     iphdr hdr;
57     uint16_t sport;
58     uint16_t dport;
59 };
60
61 RULE MakeRule(const string & ip,
62               const string & mask,
63               uint16_t port1,
64               uint16_t port2,
65               int proto,
66               int dir)
67 {
68     RULE rule;
69
70     rule.ip = inet_addr(ip.c_str());
71     rule.mask = inet_addr(mask.c_str());
72     rule.port1 = port1;
73     rule.port2 = port2;
74     rule.proto = proto;
75     rule.dir = dir;
76
77     return rule;
78 }
79
80 RULES PrepareRules()
81 {
82     RULES rules;
83     RULE local(MakeRule("192.168.0.0",
84                         "255.255.255.0",
85                         0,
86                         65535,
87                         -1,
88                         0));
89     RULE dns(MakeRule("195.5.61.68",
90                       "255.255.255.255",
91                       53,
92                       53,
93                       -1,
94                       1));
95     RULE ftp(MakeRule("129.22.8.159",
96                       "255.255.255.255",
97                       20,
98                       21,
99                       -1,
100                       2));
101     RULE world(MakeRule("0.0.0.0",
102                         "0.0.0.0",
103                         0,
104                         65535,
105                         -1,
106                         3));
107
108     rules.push_back(local);
109
110     rules.push_back(dns);
111
112     rules.push_back(ftp);
113
114     rules.push_back(world);
115
116     return rules;
117 }
118
119 iphdr MakePacket(const string & from,
120                  const string & to,
121                  int proto,
122                  uint16_t length)
123 {
124     iphdr hdr;
125
126     hdr.ihl = 5;
127     hdr.version = 4;
128     hdr.tos = 0;
129     hdr.tot_len = length;
130     hdr.id = 0;
131     hdr.frag_off = 50;
132     hdr.ttl = 64;
133     hdr.protocol = proto;
134     hdr.check = 0;
135     hdr.saddr = inet_addr(from.c_str());
136     hdr.daddr = inet_addr(to.c_str());
137
138     return hdr;
139 }
140
141 void PrepareTraffic(vector<PACKET> & pckts,
142                     const iphdr & skel,
143                     uint16_t sport,
144                     uint16_t dport,
145                     uint32_t in,
146                     uint32_t out,
147                     int packets)
148 {
149     PACKET inpacket;
150     PACKET outpacket;
151
152     inpacket.hdr = skel;
153     outpacket.hdr = skel;
154
155     outpacket.hdr.saddr ^= outpacket.hdr.daddr;
156     outpacket.hdr.daddr ^= outpacket.hdr.saddr;
157     outpacket.hdr.saddr ^= outpacket.hdr.daddr;
158
159     inpacket.sport = sport;
160     inpacket.dport = dport;
161     outpacket.sport = dport;
162     outpacket.dport = sport;
163
164     inpacket.hdr.tot_len = in / packets;
165     outpacket.hdr.tot_len = out / packets;
166
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);
171     }
172 }
173
174 struct TC_TESTER : public unary_function<const PACKET &, void>
175 {
176 public:
177     TC_TESTER(TRAFFCOUNTER & t)
178         : tc(t)
179     {}
180
181     void operator () (const PACKET & p)
182     {
183         tc.AddPacket(p.hdr, p.sport, p.dport);
184     }
185 private:
186     TRAFFCOUNTER & tc;
187 };
188
189 int main()
190 {
191     RULES rules(PrepareRules());
192
193     TRAFFCOUNTER tc;
194
195     vector<PACKET> packets;
196
197     tc.SetRules(rules);
198
199     if (tc.Start()) {
200         LOG_IT << "::main() Error: traffcounter not started" << endl;
201         return EXIT_FAILURE;
202     }
203
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
207
208     for (int i = 0; i < 10000; ++i) {
209         tc.AddIP(rand() * 32768 + rand());
210     }
211
212     /*
213      * A -> S
214      * S -> A
215      * A -> B
216      * B -> A
217      * A -> h1
218      * h1 -> A
219      * A -> h2
220      * h2 -> A
221      * A -> h3
222      * h3 -> A
223      * A -> h4
224      * h4 -> A
225      */
226
227     timeval tv_from;
228     timeval tv_to;
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);
236     // A - DNS
237     //PrepareTraffic(packets, MakePacket("192.168.0.2", "195.5.61.68", 6, 0), 4521, 53, 1024 * 1024, 2048 * 1024, 512 * 2);
238     // A - World
239     //PrepareTraffic(packets, MakePacket("192.168.0.2", "195.5.61.68", 6, 0), 4521, 80, 1024 * 1024, 2048 * 1024, 512 * 2);
240     // A - FTP
241     //PrepareTraffic(packets, MakePacket("192.168.0.2", "129.22.8.159", 6, 0), 4522, 20, 512 * 1024, 512  * 1024, 512 * 2);
242     // A - FTP
243     //PrepareTraffic(packets, MakePacket("192.168.0.2", "129.22.8.159", 6, 0), 4522, 21, 512 * 1024, 4096 * 1024, 512 * 2);
244     // B - World
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);
247
248     uint64_t diff = tv_to.tv_sec - tv_from.tv_sec;
249     diff *= 1000000;
250     diff -= tv_from.tv_usec;
251     diff += tv_to.tv_usec;
252
253     LOG_IT << "::main() Prepared " << packets.size() << " packets by " << diff << " usecs" << endl;
254
255     gettimeofday(&tv_from, NULL);
256     for_each(packets.begin(),
257                   packets.end(),
258                   TC_TESTER(tc));
259     gettimeofday(&tv_to, NULL);
260
261     diff = tv_to.tv_sec - tv_from.tv_sec;
262     diff *= 1000000;
263     diff -= tv_from.tv_usec;
264     diff += tv_to.tv_usec;
265
266     LOG_IT << "::main() Recorded " << packets.size() << " packets by " << diff << " usecs" << endl;
267
268     int p;
269     while ((p = tc.PendingCount())) {
270         LOG_IT << "::main() Pending packets: " << p << " at " << time(NULL) << endl;
271         sleep(1);
272     }
273
274     TRAFF_DATA data;
275
276     tc.DeleteIP(inet_addr("192.168.0.1"), &data);
277     for_each(data.begin(),
278                   data.end(),
279                   StatPrinter());
280     data.erase(data.begin(), data.end());
281     tc.DeleteIP(inet_addr("192.168.0.2"), &data);
282     for_each(data.begin(),
283                   data.end(),
284                   StatPrinter());
285     data.erase(data.begin(), data.end());
286     tc.DeleteIP(inet_addr("192.168.0.3"), &data);
287     for_each(data.begin(),
288                   data.end(),
289                   StatPrinter());
290     data.erase(data.begin(), data.end());
291
292     if (tc.Stop()) {
293         LOG_IT << "::main() Error: traffcounter not stopped" << endl;
294         return EXIT_FAILURE;
295     }
296
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;
301
302     return EXIT_SUCCESS;
303 }