forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipaccount.h
370 lines (351 loc) · 9.31 KB
/
ipaccount.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* Martin Vit [email protected]
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2.
*/
#ifndef IPACCOUNT_H
#define IPACCOUNT_H
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include "sniff.h"
#include "sql_db.h"
void ipaccount(time_t, struct iphdr2 *, int, int);
struct octects_t {
octects_t() {
octects = 0;
numpackets = 0;
interval_time = 0;
voippacket = 0;
erase = false;
}
unsigned int octects;
unsigned int numpackets;
unsigned int interval_time;
int voippacket;
bool erase;
};
struct octects_live_t {
int all;
unsigned long long int dst_octects;
unsigned int dst_numpackets;
unsigned long long int src_octects;
unsigned int src_numpackets;
unsigned long long int voipdst_octects;
unsigned int voipdst_numpackets;
unsigned long long int voipsrc_octects;
unsigned int voipsrc_numpackets;
unsigned long long int all_octects;
unsigned long long int voipall_octects;
unsigned int all_numpackets;
unsigned int voipall_numpackets;
vector<unsigned int> ipfilter;
unsigned int fetch_timestamp;
bool isIpInFilter(unsigned int ip) {
vector<unsigned int>::iterator findIp;
findIp = std::lower_bound(ipfilter.begin(), ipfilter.end(), ip);
return(findIp != ipfilter.end() && (*findIp) == ip);
}
void setFilter(const char *ipfilter);
};
struct cust_cache_item {
cust_cache_item() {
cust_id = 0;
add_timestamp = 0;
}
unsigned int cust_id;
unsigned int add_timestamp;
};
struct cust_cache_rec {
cust_cache_rec() {
ip = 0;
cust_id = 0;
}
unsigned int ip;
unsigned int cust_id;
bool operator < (const cust_cache_rec& other) const {
return(this->ip < other.ip);
}
bool operator < (const unsigned int& _ip) const {
return(this->ip < _ip);
}
};
struct cust_pn_cache_rec {
cust_pn_cache_rec() {
cust_id = 0;
}
string numberFrom;
string numberTo;
unsigned int cust_id;
string reseller_id;
bool operator < (const cust_pn_cache_rec& other) const {
return(this->numberFrom < other.numberFrom);
}
bool operator < (const string& _numberFrom) const {
return(this->numberFrom < _numberFrom);
}
};
struct next_cache_rec {
next_cache_rec() {
ip = 0;
mask = 0;
}
unsigned int ip;
unsigned int mask;
bool operator < (const next_cache_rec& other) const {
return((this->ip < other.ip) ? 1 : (this->ip > other.ip) ? 0 :
(this->mask < other.mask));
}
};
struct cust_reseller {
cust_reseller() {
cust_id = 0;
}
unsigned int cust_id;
string reseller_id;
};
typedef map<string, octects_t*> t_ipacc_buffer;
class Ipacc {
public:
struct packet {
time_t timestamp;
unsigned int saddr;
unsigned int daddr;
int port;
int proto;
int packetlen;
int voippacket;
volatile int used;
};
public:
Ipacc();
~Ipacc();
inline void push(time_t timestamp, unsigned int saddr, unsigned int daddr, int port, int proto, int packetlen, int voippacket);
void init();
void term();
int refreshCustIpCache();
void save(int indexIpaccBuffer, unsigned int interval_time_limit = 0);
inline void add_octets(time_t timestamp, unsigned int saddr, unsigned int daddr, int port, int proto, int packetlen, int voippacket);
unsigned int lengthBuffer() {
return(ipacc_buffer[0].size() + ipacc_buffer[1].size());
}
class CustIpCache *getCustIpCache() {
return(custIpCache);
}
class CustPhoneNumberCache *getCustPnCache() {
return(custPnCache);
}
void preparePstatData();
double getCpuUsagePerc(bool preparePstatData);
void startThread();
private:
void *outThreadFunction();
private:
t_ipacc_buffer ipacc_buffer[2];
int sync_save_ipacc_buffer[2];
unsigned int last_flush_interval_time;
class CustIpCache *custIpCache;
class NextIpCache *nextIpCache;
class CustPhoneNumberCache *custPnCache;
SqlDb *sqlDbSave;
packet *qring;
unsigned int qringmax;
volatile unsigned int readit;
volatile unsigned int writeit;
pthread_t out_thread_handle;
int outThreadId;
pstat_data threadPstatData[2];
friend inline void *_Ipacc_outThreadFunction(void *arg);
};
class IpaccAgreg {
public:
struct AgregData {
AgregData() {
id_customer = 0;
id_customer2 = 0;
traffic_in = 0;
traffic_out = 0;
packets_in = 0;
packets_out = 0;
traffic_voip_in = 0;
traffic_voip_out = 0;
packets_voip_in = 0;
packets_voip_out = 0;
}
void addIn(unsigned int traffic, unsigned int packets, bool voip) {
traffic_in += traffic;
packets_in += packets;
if(voip) {
traffic_voip_in += traffic;
packets_voip_in += packets;
}
}
void addOut(unsigned int traffic, unsigned int packets, bool voip) {
traffic_out += traffic;
packets_out += packets;
if(voip) {
traffic_voip_out += traffic;
packets_voip_out += packets;
}
}
unsigned int id_customer;
unsigned int id_customer2;
unsigned long traffic_in;
unsigned long traffic_out;
unsigned long packets_in;
unsigned long packets_out;
unsigned long traffic_voip_in;
unsigned long traffic_voip_out;
unsigned long packets_voip_in;
unsigned long packets_voip_out;
};
struct AgregIP {
AgregIP(unsigned int ip, unsigned int proto, unsigned int port) {
this->ip = ip;
this->proto = proto;
this->port = port;
}
unsigned int ip;
unsigned int proto;
unsigned int port;
bool operator < (const AgregIP& other) const {
return((this->ip < other.ip) ? 1 : (this->ip > other.ip) ? 0 :
(this->proto < other.proto) ? 1 : (this->proto > other.proto) ? 0 :
(this->port < other.port));
}
};
struct AgregIP2 {
AgregIP2(unsigned int ip1, unsigned int ip2, unsigned int proto, unsigned int port) {
this->ip1 = ip1;
this->ip2 = ip2;
this->proto = proto;
this->port = port;
}
unsigned int ip1;
unsigned int ip2;
unsigned int proto;
unsigned int port;
bool operator < (const AgregIP2& other) const {
return((this->ip1 < other.ip1) ? 1 : (this->ip1 > other.ip1) ? 0 :
(this->ip2 < other.ip2) ? 1 : (this->ip2 > other.ip2) ? 0 :
(this->proto < other.proto) ? 1 : (this->proto > other.proto) ? 0 :
(this->port < other.port));
}
};
~IpaccAgreg();
void add(unsigned int src, unsigned int dst,
unsigned int src_id_customer, unsigned int dst_id_customer,
bool src_ip_next, bool dst_ip_next,
unsigned int proto, unsigned int port,
unsigned int traffic, unsigned int packets, bool voip);
void save(unsigned int time_interval);
private:
map<AgregIP, AgregData*> map1;
map<AgregIP2, AgregData*> map2;
};
class CustIpCache {
public:
CustIpCache();
~CustIpCache();
void setConnectParams(const char *sqlDriver, const char *odbcDsn, const char *odbcUser, const char *odbcPassword, const char *odbcDriver);
void setConnectParamsRadius(const char *radiusSqlDriver, const char *radiusHost, const char *radiusDb,const char *radiusUser, const char *radiusPassword, bool radiusDisableSecureAuth);
void setQueryes(const char *getIp, const char *fetchAllIp);
void setQueryesRadius(const char *fetchAllRadiusNames, const char *fetchAllRadiusIp, const char *fetchAllRadiusIpWhere);
int connect();
bool okParams();
int getCustByIp(unsigned int ip);
int getCustByIpFromDb(unsigned int ip, bool saveToCache = false);
int fetchAllIpQueryFromDb();
int getCustByIpFromCacheMap(unsigned int ip);
int getCustByIpFromCacheVect(unsigned int ip);
void flush();
void clear();
string printVect();
void setMaxQueryPass(unsigned int maxQueryPass) {
if(this->sqlDb) {
this->sqlDb->setMaxQueryPass(maxQueryPass);
}
if(this->sqlDbRadius) {
this->sqlDbRadius->setMaxQueryPass(maxQueryPass);
}
}
private:
SqlDb *sqlDb;
SqlDb *sqlDbRadius;
map<unsigned int, cust_cache_item> custCacheMap;
vector<cust_cache_rec> custCacheVect;
string sqlDriver;
string odbcDsn;
string odbcUser;
string odbcPassword;
string odbcDriver;
string radiusSqlDriver;
string radiusHost;
string radiusDb;
string radiusUser;
string radiusPassword;
bool radiusDisableSecureAuth;
string query_getIp;
string query_fetchAllIp;
string query_fetchAllRadiusNames;
string query_fetchAllRadiusIp;
string query_fetchAllRadiusIpWhere;
unsigned int flushCounter;
bool doFlushVect;
};
class NextIpCache {
public:
NextIpCache();
~NextIpCache();
int connect();
bool isIn(unsigned int ip);
void fetch();
void flush();
void setMaxQueryPass(unsigned int maxQueryPass) {
if(this->sqlDb) {
this->sqlDb->setMaxQueryPass(maxQueryPass);
}
}
private:
SqlDb *sqlDb;
vector<next_cache_rec> nextCache;
unsigned int flushCounter;
bool doFlush;
};
class CustPhoneNumberCache {
public:
CustPhoneNumberCache();
~CustPhoneNumberCache();
void setConnectParams(const char *sqlDriver, const char *odbcDsn, const char *odbcUser, const char *odbcPassword, const char *odbcDriver);
void setQueryes(const char *fetchPhoneNumbers);
int connect();
bool okParams();
cust_reseller getCustomerByPhoneNumber(char *number);
int fetchPhoneNumbersFromDb();
void flush();
void setMaxQueryPass(unsigned int maxQueryPass) {
if(this->sqlDb) {
this->sqlDb->setMaxQueryPass(maxQueryPass);
}
}
private:
SqlDb *sqlDb;
vector<cust_pn_cache_rec> custCache;
string sqlDriver;
string odbcDsn;
string odbcUser;
string odbcPassword;
string odbcDriver;
string query_fetchPhoneNumbers;
unsigned int flushCounter;
bool doFlush;
};
CustIpCache *getCustIpCache();
CustPhoneNumberCache *getCustPnCache();
int refreshCustIpCache();
unsigned int lengthIpaccBuffer();
string getIpaccCpuUsagePerc();
void initIpacc();
void termIpacc();
void ipaccStartThread();
#endif