-
Notifications
You must be signed in to change notification settings - Fork 658
/
Geolocation.cpp
319 lines (249 loc) · 9.86 KB
/
Geolocation.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
/*
*
* (C) 2013-18 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
/*
This product includes GeoLite data created by MaxMind, available from
<a href="http://www.maxmind.com">http://www.maxmind.com</a>.
http://dev.maxmind.com/geoip/legacy/geolite
*/
#include "ntop_includes.h"
/* *************************************** */
#if defined(HAVE_MAXMINDDB) && defined(TEST_GEOLOCATION)
void Geolocation::testme() {
sockaddr *sa = NULL;
ssize_t sa_len;
IpAddress test;
int mmdb_error, status;
MMDB_lookup_result_s result;
MMDB_entry_data_list_s *entry_data_list = NULL;
MMDB_entry_data_s entry_data;
static char *ips[] = {(char*)"192.168.1.1",
(char*)"69.89.31.226",
(char*)"8.8.8.8",
(char*)"69.63.181.15",
(char*)"2a03:2880:f10c:83:face:b00c:0:25de"};
for(u_int32_t i = 0; i < sizeof(ips) / sizeof(char*); i++) {
char * ip = ips[i];
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Geolocating [%s]", ip);
test.set(ip);
if(test.get_sockaddr(&sa, &sa_len)) {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Autonomous System Information", ip);
/* TEST Autonomous Systems database */
result = MMDB_lookup_sockaddr(&geo_ip_asn_mmdb, sa, &mmdb_error);
if(mmdb_error != MMDB_SUCCESS) {
}
if(result.found_entry) {
entry_data_list = NULL;
if((status = MMDB_get_entry_data_list(&result.entry, &entry_data_list)) != MMDB_SUCCESS)
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to lookup address [%s]", MMDB_strerror(status));
if(entry_data_list)
MMDB_dump_entry_data_list(stdout, entry_data_list, 2);
if((status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_number", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UINT32)
ntop->getTrace()->traceEvent(TRACE_NORMAL, "ASN: %d", entry_data.uint32);
} else
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to lookup autonomous system number [%s]", MMDB_strerror(status));
if((status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_organization", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
char *org;
if((org = (char*)malloc(entry_data.data_size + 1))) {
snprintf(org, entry_data.data_size + 1, "%s", entry_data.utf8_string);
org[entry_data.data_size] = '\0';
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Organization: %s", org);
free(org);
}
}
} else
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to lookup autonomous system organization [%s]", MMDB_strerror(status));
}
/* TEST City database */
ntop->getTrace()->traceEvent(TRACE_NORMAL, "City Information", ip);
result = MMDB_lookup_sockaddr(&geo_ip_city_mmdb, sa, &mmdb_error);
if (mmdb_error != MMDB_SUCCESS) {
}
if (result.found_entry) {
entry_data_list = NULL;
status = MMDB_get_entry_data_list(&result.entry,
&entry_data_list);
if(status != MMDB_SUCCESS)
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to lookup address [%s]", MMDB_strerror(status));
if(entry_data_list) {
MMDB_dump_entry_data_list(stdout, entry_data_list, 2);
}
}
free(sa);
}
}
}
#endif
/* *************************************** */
Geolocation::Geolocation(char *db_home) {
char path[MAX_PATH];
snprintf(path, sizeof(path), "%s/geoip", db_home);
#ifdef HAVE_MAXMINDDB
mmdbs_ok = loadMaxMindDB(path, "GeoLite2-ASN.mmdb", &geo_ip_asn_mmdb)
&& loadMaxMindDB(path, "GeoLite2-City.mmdb", &geo_ip_city_mmdb);
#endif
}
/* *************************************** */
#ifdef HAVE_MAXMINDDB
bool Geolocation::loadMaxMindDB(const char * const base_path, const char * const db_name, MMDB_s * const mmdb) const {
char path[MAX_PATH];
struct stat buf;
bool found;
snprintf(path, sizeof(path), "%s/%s", base_path, db_name);
ntop->fixPath(path);
found = ((stat(path, &buf) == 0) && (S_ISREG(buf.st_mode))) ? true : false;
if(!found) return false;
int status = MMDB_open(path, MMDB_MODE_MMAP, mmdb);
if(status != MMDB_SUCCESS) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to open %s [%s]",
path, MMDB_strerror(status));
if(status == MMDB_IO_ERROR)
ntop->getTrace()->traceEvent(TRACE_ERROR, "IO error [%s]", strerror(errno));
return false;
} else {
ntop->getTrace()->traceEvent(TRACE_INFO, "Loaded database %s [ip_version: %d]",
db_name, mmdb->metadata.ip_version);
return true;
}
}
#endif
/* *************************************** */
Geolocation::~Geolocation() {
#ifdef HAVE_MAXMINDDB
if(mmdbs_ok) {
MMDB_close(&geo_ip_asn_mmdb);
MMDB_close(&geo_ip_city_mmdb);
}
#endif
}
/* *************************************** */
void Geolocation::getAS(IpAddress *addr, u_int32_t *asn, char **asname) {
if(asn) *asn = 0;
if(asname) *asname = NULL;
#ifdef HAVE_MAXMINDDB
sockaddr *sa = NULL;
ssize_t sa_len;
int mmdb_error, status;
MMDB_lookup_result_s result;
MMDB_entry_data_s entry_data;
if(!mmdbs_ok) return;
if(addr && addr->get_sockaddr(&sa, &sa_len)) {
result = MMDB_lookup_sockaddr(&geo_ip_asn_mmdb, sa, &mmdb_error);
if(mmdb_error == MMDB_SUCCESS) {
if(result.found_entry) {
/* Get the ASN */
if(asn && (status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_number", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UINT32)
*asn = entry_data.uint32;
}
/* Get the AS Organization, that is an utf8 string that is NOT terminated with a null character. */
if(asname && (status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_organization", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
char *org;
if((org = (char*)malloc(entry_data.data_size + 1))) {
snprintf(org, entry_data.data_size + 1, "%s", entry_data.utf8_string);
*asname = org;
}
}
}
}
} else
ntop->getTrace()->traceEvent(TRACE_ERROR, "Lookup failed [%s]", MMDB_strerror(mmdb_error));
free(sa);
}
#endif
return;
}
/* *************************************** */
void Geolocation::getInfo(IpAddress *addr, char **continent_code, char **country_code,
char **city, float *latitude, float *longitude) {
if(continent_code) *continent_code = strdup((char*)UNKNOWN_CONTINENT);
if(country_code) *country_code = strdup((char*)UNKNOWN_COUNTRY);
if(city) *city = strdup((char*)UNKNOWN_CITY);
if(latitude) *latitude = 0;
if(longitude) *longitude = 0;
#ifdef HAVE_MAXMINDDB
sockaddr *sa = NULL;
ssize_t sa_len;
char *cdata;
if(!mmdbs_ok) return;
if(addr && addr->get_sockaddr(&sa, &sa_len)) {
int mmdb_error, status;
MMDB_lookup_result_s result;
MMDB_entry_data_s entry_data;
result = MMDB_lookup_sockaddr(&geo_ip_city_mmdb, sa, &mmdb_error);
if(mmdb_error == MMDB_SUCCESS) {
if(result.found_entry) {
/* Get the continent code */
if(continent_code && (status = MMDB_get_value(&result.entry, &entry_data, "continent", "code", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
if((cdata = (char*)malloc(entry_data.data_size + 1))) {
snprintf(cdata, entry_data.data_size + 1, "%s", entry_data.utf8_string);
free(*continent_code);
*continent_code = cdata;
}
}
}
/* Get the country code */
if(country_code && (status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
if((cdata = (char*)malloc(entry_data.data_size + 1))) {
snprintf(cdata, entry_data.data_size + 1, "%s", entry_data.utf8_string);
free(*country_code);
*country_code = cdata;
}
}
}
/* Get the city (seems that there are only localized versions of the city name) */
if(city && (status = MMDB_get_value(&result.entry, &entry_data, "city", "names", "en", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
if((cdata = (char*)malloc(entry_data.data_size + 1))) {
snprintf(cdata, entry_data.data_size + 1, "%s", entry_data.utf8_string);
free(*city);
*city = cdata;
}
}
}
/* Get the latitude */
if(latitude && (status = MMDB_get_value(&result.entry, &entry_data, "location", "latitude", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_DOUBLE)
*latitude = (float)entry_data.double_value;
}
/* Get the longitude */
if(longitude && (status = MMDB_get_value(&result.entry, &entry_data, "location", "longitude", NULL)) == MMDB_SUCCESS) {
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_DOUBLE)
*longitude = (float)entry_data.double_value;
}
}
}
free(sa);
} else
ntop->getTrace()->traceEvent(TRACE_ERROR, "Invalid address lookup");
#endif
return;
}
/* *************************************** */
void Geolocation::freeInfo(char **continent_code, char **country_code, char **city) {
if(continent_code) { free(*continent_code); *continent_code = NULL; }
if(country_code) { free(*country_code); *country_code = NULL; }
if(city) { free(*city); *city = NULL; }
}