forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry_detect.cpp
420 lines (371 loc) · 10.3 KB
/
country_detect.cpp
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "voipmonitor.h"
#include "country_detect.h"
CountryDetect *countryDetect;
extern int opt_nocdr;
CountryDetect_base_table::CountryDetect_base_table() {
loadOK = false;
}
bool CountryDetect_base_table::checkTable(eTableType tableType, string &tableName) {
tableName = getTableName(tableType);
if(tableName.empty()) {
loadOK = false;
return(false);
}
bool rslt = true;
SqlDb *sqlDb = createSqlObject();
if(!sqlDb->existsTable(tableName)) {
syslog(LOG_WARNING, "missing table %s - table is created from gui", tableName.c_str());
rslt = false;
} else if(sqlDb->emptyTable(tableName)) {
syslog(LOG_WARNING, "table %s is empty - table is filled from gui", tableName.c_str());
rslt = false;
}
delete sqlDb;
loadOK = rslt;
return(rslt);
}
string CountryDetect_base_table::getTableName(eTableType tableType) {
string tableName;
bool useCloudShare = false;
switch(tableType) {
case _country_code:
tableName = "country_code";
useCloudShare = true;
break;
case _international_rules:
tableName = "international_rules";
break;
case _country_code_prefix:
tableName = "country_code_prefix";
useCloudShare = true;
break;
case _geoip_country:
tableName = "geoip_country";
useCloudShare = true;
break;
}
if(!tableName.empty() && isCloud() && useCloudShare) {
tableName = "cloudshare." + tableName;
}
return(tableName);
}
CountryCodes::CountryCodes() {
}
bool CountryCodes::load() {
string tableName;
if(!checkTable(_country_code, tableName)) {
return(false);
}
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select * \
from " + tableName + " \
where parent_id is null");
SqlDb_row row;
while((row = sqlDb->fetchRow())) {
continents[row["code"]] = row["name"];
}
sqlDb->query("select country.*, continent.code as continent \
from " + tableName + " country \
join " + tableName + " continent on (continent.id = country.parent_id) \
where country.parent_id is not null");
while((row = sqlDb->fetchRow())) {
countries[row["code"]] = row["name"];
countryContinent[row["code"]] = row["continent"];
continentCountry[row["continent"]].push_back(row["code"]);
}
delete sqlDb;
return(true);
}
bool CountryCodes::isCountry(const char *code) {
map<string, string>::iterator iter;
iter = countries.find(code);
return(iter != countries.end());
}
string CountryCodes::getNameCountry(const char *code) {
map<string, string>::iterator iter;
iter = countries.find(code);
return(iter != countries.end() ? iter->second : "");
}
string CountryCodes::getNameContinent(const char *code) {
map<string, string>::iterator iter;
iter = continents.find(code);
return(iter != continents.end() ? iter->second : "");
}
string CountryCodes::getName(const char *code) {
return(isCountry(code) ? getNameCountry(code) : getNameContinent(code));
}
string CountryCodes::getContinent(const char *code) {
map<string, string>::iterator iter;
iter = countryContinent.find(code);
return(iter != countryContinent.end() ? iter->second : "");
}
bool CountryCodes::isLocationIn(const char *location, vector<string> *in, bool continent) {
string location_s = continent ? string("c_") + location : location;
vector<string>::iterator iter = in->begin();
while(iter != in->end()) {
if(location_s == *iter) {
return(true);
}
++iter;
}
return(false);
}
CheckInternational::CheckInternational() {
internationalPrefixes = split("+, 00", ",", true);
internationalMinLength = 0;
internationalMinLengthPrefixesStrict = false;
enableCheckNapaWithoutPrefix = false;
}
void CheckInternational::setInternationalPrefixes(const char *prefixes) {
this->internationalPrefixes = split(prefixes, ",", true);
}
void CheckInternational::setSkipPrefixes(const char *prefixes) {
this->skipPrefixes = split(prefixes, ",", true);
}
void CheckInternational::setInternationalMinLength(int internationalMinLength, bool internationalMinLengthPrefixesStrict) {
this->internationalMinLength = internationalMinLength;
this->internationalMinLengthPrefixesStrict = internationalMinLengthPrefixesStrict;
}
void CheckInternational::setEnableCheckNapaWithoutPrefix(bool enableCheckNapaWithoutPrefix) {
this->enableCheckNapaWithoutPrefix = enableCheckNapaWithoutPrefix;
}
void CheckInternational::load(SqlDb_row *dbRow) {
string _prefixes = (*dbRow)["international_prefixes"];
if(!_prefixes.empty()) {
internationalPrefixes = split(_prefixes.c_str(), split(",|;| ", "|"), true);
} else {
internationalPrefixes.clear();
}
internationalMinLength = atoi((*dbRow)["international_number_min_length"].c_str());
internationalMinLengthPrefixesStrict = atoi((*dbRow)["international_number_min_length_prefixes_strict"].c_str());
countryCodeForLocalNumbers = (*dbRow)["country_code_for_local_numbers"];
enableCheckNapaWithoutPrefix = atoi((*dbRow)["enable_check_napa_without_prefix"].c_str());
_prefixes = (*dbRow)["skip_prefixes"];
if(!_prefixes.empty()) {
skipPrefixes = split(_prefixes.c_str(), split(",|;| ", "|"), true);
}
}
bool CheckInternational::load() {
string tableName;
if(!checkTable(_international_rules, tableName)) {
return(false);
}
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select * from " + tableName);
SqlDb_row row;
while((row = sqlDb->fetchRow())) {
this->load(&row);
}
delete sqlDb;
return(true);
}
CountryPrefixes::CountryPrefixes() {
}
bool CountryPrefixes::load() {
string tableName;
if(!checkTable(_country_code_prefix, tableName)) {
return(false);
}
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select * \
from " + tableName + " \
order by prefix");
SqlDb_row row;
while((row = sqlDb->fetchRow())) {
data.push_back(CountryPrefix_rec(
row["prefix"].c_str(),
row["country_code"].c_str(),
row["descr"].c_str()));
}
std::sort(data.begin(), data.end());
delete sqlDb;
return(true);
}
GeoIP_country::GeoIP_country() {
}
bool GeoIP_country::load() {
string tableName;
if(!checkTable(_geoip_country, tableName)) {
return(false);
}
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select * \
from " + tableName + " \
order by ip_from");
SqlDb_row row;
while((row = sqlDb->fetchRow())) {
data.push_back(GeoIP_country_rec(
atol(row["ip_from"].c_str()),
atol(row["ip_to"].c_str()),
row["country"].c_str()));
}
std::sort(data.begin(), data.end());
delete sqlDb;
return(true);
}
CountryDetect::CountryDetect() {
countryCodes = new FILE_LINE(0) CountryCodes;
countryPrefixes = new FILE_LINE(0) CountryPrefixes;
geoIP_country = new FILE_LINE(0) GeoIP_country;
checkInternational = new FILE_LINE(0) CheckInternational;
countryCodes_reload = NULL;
countryPrefixes_reload = NULL;
geoIP_country_reload = NULL;
checkInternational_reload = NULL;
reload_do = false;
_sync = 0;
_sync_reload = 0;
}
CountryDetect::~CountryDetect() {
delete countryCodes;
delete countryPrefixes;
delete geoIP_country;
delete checkInternational;
}
void CountryDetect::load() {
countryCodes->load();
countryPrefixes->load();
geoIP_country->load();
checkInternational->load();
}
string CountryDetect::getCountryByPhoneNumber(const char *phoneNumber) {
string rslt;
lock();
if(countryPrefixes->loadOK) {
rslt = countryPrefixes->getCountry(phoneNumber, NULL, NULL, checkInternational);
}
unlock();
return(rslt);
}
bool CountryDetect::isLocalByPhoneNumber(const char *phoneNumber) {
bool rslt = false;
lock();
if(countryPrefixes->loadOK) {
rslt = countryPrefixes->isLocal(phoneNumber, checkInternational);
}
unlock();
return(rslt);
}
string CountryDetect::getCountryByIP(u_int32_t ip) {
string rslt;
lock();
if(geoIP_country->loadOK) {
rslt = geoIP_country->getCountry(ip);
}
unlock();
return(rslt);
}
string CountryDetect::getContinentByCountry(const char *country) {
string rslt;
lock();
if(countryCodes->loadOK) {
rslt = countryCodes->getContinent(country);
}
unlock();
return(rslt);
}
void CountryDetect::prepareReload() {
if(opt_nocdr) {
return;
}
reload_do = false;
lock_reload();
if(countryCodes_reload) {
delete countryCodes_reload;
}
if(countryPrefixes_reload) {
delete countryPrefixes_reload;
}
if(geoIP_country_reload) {
delete geoIP_country_reload;
}
if(checkInternational_reload) {
delete checkInternational_reload;
}
countryCodes_reload = new FILE_LINE(0) CountryCodes;
countryPrefixes_reload = new FILE_LINE(0) CountryPrefixes;
geoIP_country_reload = new FILE_LINE(0) GeoIP_country;
checkInternational_reload = new FILE_LINE(0) CheckInternational;
countryCodes_reload->load();
countryPrefixes_reload->load();
geoIP_country_reload->load();
checkInternational_reload->load();
reload_do = true;
syslog(LOG_NOTICE, "CountryDetect::prepareReload");
unlock_reload();
}
void CountryDetect::applyReload() {
if(reload_do) {
lock_reload();
lock();
delete countryCodes;
delete countryPrefixes;
delete geoIP_country;
delete checkInternational;
countryCodes = countryCodes_reload;
countryPrefixes = countryPrefixes_reload;
geoIP_country = geoIP_country_reload;
checkInternational = checkInternational_reload;
unlock();
countryCodes_reload = NULL;
countryPrefixes_reload = NULL;
geoIP_country_reload = NULL;
checkInternational_reload = NULL;
reload_do = false;
syslog(LOG_NOTICE, "CountryDetect::applyReload");
unlock_reload();
}
}
void CountryDetectInit() {
if(!opt_nocdr) {
countryDetect = new FILE_LINE(0) CountryDetect;
countryDetect->load();
}
}
void CountryDetectTerm() {
if(countryDetect) {
delete countryDetect;
}
}
string getCountryByPhoneNumber(const char *phoneNumber, bool suppressStringLocal) {
if(countryDetect) {
string country = countryDetect->getCountryByPhoneNumber(phoneNumber);
if(suppressStringLocal && country == "local") {
country = "";
}
return(country);
}
return("");
}
bool isLocalByPhoneNumber(const char *phoneNumber) {
if(countryDetect) {
return(countryDetect->isLocalByPhoneNumber(phoneNumber));
}
return(false);
}
string getCountryByIP(u_int32_t ip, bool suppressStringLocal) {
if(countryDetect) {
string country = countryDetect->getCountryByIP(ip);
if(suppressStringLocal && country == "local") {
country = "";
}
return(country);
}
return("");
}
string getContinentByCountry(const char *country) {
if(countryDetect) {
return(countryDetect->getContinentByCountry(country));
}
return("");
}
void CountryDetectPrepareReload() {
if(countryDetect) {
return(countryDetect->prepareReload());
}
}
void CountryDetectApplyReload() {
if(countryDetect) {
return(countryDetect->applyReload());
}
}