forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip1.c
439 lines (381 loc) · 11.9 KB
/
geoip1.c
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* geoip.c -- implementation of GeoIP
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_LIBGEOIP
#include <GeoIP.h>
#include <GeoIPCity.h>
#endif
#include "geoip1.h"
#include "error.h"
#include "util.h"
static GeoIP *geo_location_data;
/* Determine if we have a valid geoip resource.
*
* If the geoip resource is NULL, 0 is returned.
* If the geoip resource is valid and malloc'd, 1 is returned. */
int
is_geoip_resource (void)
{
return geo_location_data != NULL ? 1 : 0;
}
/* Free up GeoIP resources */
void
geoip_free (void)
{
if (!is_geoip_resource ())
return;
GeoIP_delete (geo_location_data);
GeoIP_cleanup ();
}
/* Open the given GeoLocation database and set its charset.
*
* On error, it aborts.
* On success, a new geolocation structure is returned. */
static GeoIP *
geoip_open_db (const char *db)
{
GeoIP *geoip;
geoip = GeoIP_open (db, GEOIP_MEMORY_CACHE);
if (geoip == NULL)
FATAL ("Unable to open GeoIP database: %s\n", db);
GeoIP_set_charset (geoip, GEOIP_CHARSET_UTF8);
LOG_DEBUG (("Opened GeoIP City database: %s\n", db));
return geoip;
}
/* Set up and open GeoIP database */
void
init_geoip (void)
{
/* open custom city GeoIP database */
if (conf.geoip_database != NULL)
geo_location_data = geoip_open_db (conf.geoip_database);
/* fall back to legacy GeoIP database */
else
geo_location_data = GeoIP_new (conf.geo_db);
}
/* Get continent name concatenated with code.
*
* If continent not found, "Unknown" is returned.
* On success, the continent code & name is returned . */
static const char *
get_continent_name_and_code (const char *continentid)
{
if (memcmp (continentid, "NA", 2) == 0)
return "NA North America";
else if (memcmp (continentid, "OC", 2) == 0)
return "OC Oceania";
else if (memcmp (continentid, "EU", 2) == 0)
return "EU Europe";
else if (memcmp (continentid, "SA", 2) == 0)
return "SA South America";
else if (memcmp (continentid, "AF", 2) == 0)
return "AF Africa";
else if (memcmp (continentid, "AN", 2) == 0)
return "AN Antarctica";
else if (memcmp (continentid, "AS", 2) == 0)
return "AS Asia";
else
return "-- Unknown";
}
/* Compose a string with the country name and code and store it in the
* given buffer. */
static void
geoip_set_country (const char *country, const char *code, char *loc)
{
if (country && code)
snprintf (loc, COUNTRY_LEN, "%s %s", code, country);
else
snprintf (loc, COUNTRY_LEN, "%s", "Unknown");
}
/* Compose a string with the city name and state/region and store it
* in the given buffer. */
static void
geoip_set_city (const char *city, const char *region, char *loc)
{
snprintf (loc, CITY_LEN, "%s, %s", city ? city : "N/A City",
region ? region : "N/A Region");
}
/* Compose a string with the continent name and store it in the given
* buffer. */
static void
geoip_set_continent (const char *continent, char *loc)
{
if (continent)
snprintf (loc, CONTINENT_LEN, "%s",
get_continent_name_and_code (continent));
else
snprintf (loc, CONTINENT_LEN, "%s", "Unknown");
}
/* Get detailed information found in the GeoIP Database about the
* given IPv4 or IPv6.
*
* On error, NULL is returned
* On success, GeoIPRecord structure is returned */
static GeoIPRecord *
get_geoip_record (const char *addr, GTypeIP type_ip)
{
GeoIPRecord *rec = NULL;
if (TYPE_IPV4 == type_ip)
rec = GeoIP_record_by_name (geo_location_data, addr);
else if (TYPE_IPV6 == type_ip)
rec = GeoIP_record_by_name_v6 (geo_location_data, addr);
return rec;
}
/* Set country data by record into the given `location` buffer based
* on the IP version. */
static void
geoip_set_country_by_record (const char *ip, char *location, GTypeIP type_ip)
{
GeoIPRecord *rec = NULL;
const char *country = NULL, *code = NULL, *addr = ip;
if (conf.geoip_database == NULL || geo_location_data == NULL)
return;
/* Custom GeoIP database */
if ((rec = get_geoip_record (addr, type_ip))) {
country = rec->country_name;
code = rec->country_code;
}
geoip_set_country (country, code, location);
if (rec != NULL) {
GeoIPRecord_delete (rec);
}
}
/* Get the GeoIP location id by name.
*
* On error, 0 is returned
* On success, the GeoIP location id is returned */
static int
geoip_get_geoid (const char *addr, GTypeIP type_ip)
{
int geoid = 0;
if (TYPE_IPV4 == type_ip)
geoid = GeoIP_id_by_name (geo_location_data, addr);
else if (TYPE_IPV6 == type_ip)
geoid = GeoIP_id_by_name_v6 (geo_location_data, addr);
return geoid;
}
/* Get the country name by GeoIP location id.
*
* On error, NULL is returned
* On success, the country name is returned */
static const char *
geoip_get_country_by_geoid (const char *addr, GTypeIP type_ip)
{
const char *country = NULL;
if (TYPE_IPV4 == type_ip)
country = GeoIP_country_name_by_name (geo_location_data, addr);
else if (TYPE_IPV6 == type_ip)
country = GeoIP_country_name_by_name_v6 (geo_location_data, addr);
return country;
}
/* Set country data by geoid into the given `location` buffer based on
* the IP version. */
static void
geoip_set_country_by_geoid (const char *ip, char *location, GTypeIP type_ip)
{
const char *country = NULL, *code = NULL, *addr = ip;
int geoid = 0;
if (!is_geoip_resource ())
return;
if (!(country = geoip_get_country_by_geoid (addr, type_ip)))
goto out;
/* return two letter country code */
if (!(geoid = geoip_get_geoid (addr, type_ip)))
goto out;
code = GeoIP_code_by_id (geoid);
out:
geoip_set_country (country, code, location);
}
/* Set country data by geoid or record into the given `location` buffer
* based on the IP version and currently used database edition. */
void
geoip_get_country (const char *ip, char *location, GTypeIP type_ip)
{
unsigned char rec = GeoIP_database_edition (geo_location_data);
switch (rec) {
case GEOIP_COUNTRY_EDITION:
if (TYPE_IPV4 == type_ip)
geoip_set_country_by_geoid (ip, location, TYPE_IPV4);
else
geoip_set_country (NULL, NULL, location);
break;
case GEOIP_COUNTRY_EDITION_V6:
if (TYPE_IPV6 == type_ip)
geoip_set_country_by_geoid (ip, location, TYPE_IPV6);
else
geoip_set_country (NULL, NULL, location);
break;
case GEOIP_CITY_EDITION_REV0:
case GEOIP_CITY_EDITION_REV1:
if (TYPE_IPV4 == type_ip)
geoip_set_country_by_record (ip, location, TYPE_IPV4);
else
geoip_set_country (NULL, NULL, location);
break;
case GEOIP_CITY_EDITION_REV0_V6:
case GEOIP_CITY_EDITION_REV1_V6:
if (TYPE_IPV6 == type_ip)
geoip_set_country_by_record (ip, location, TYPE_IPV6);
else
geoip_set_country (NULL, NULL, location);
break;
}
}
/* Set continent data by record into the given `location` buffer based
* on the IP version. */
static void
geoip_set_continent_by_record (const char *ip, char *location, GTypeIP type_ip)
{
GeoIPRecord *rec = NULL;
const char *continent = NULL, *addr = ip;
if (conf.geoip_database == NULL || geo_location_data == NULL)
return;
/* Custom GeoIP database */
if ((rec = get_geoip_record (addr, type_ip)))
continent = rec->continent_code;
geoip_set_continent (continent, location);
if (rec != NULL) {
GeoIPRecord_delete (rec);
}
}
/* Set continent data by geoid into the given `location` buffer based
* on the IP version. */
static void
geoip_set_continent_by_geoid (const char *ip, char *location, GTypeIP type_ip)
{
const char *continent = NULL, *addr = ip;
int geoid = 0;
if (!is_geoip_resource ())
return;
if (!(geoid = geoip_get_geoid (addr, type_ip)))
goto out;
continent = GeoIP_continent_by_id (geoid);
out:
geoip_set_continent (continent, location);
}
/* Set continent data by geoid or record into the given `location` buffer
* based on the IP version and currently used database edition. */
void
geoip_get_continent (const char *ip, char *location, GTypeIP type_ip)
{
unsigned char rec = GeoIP_database_edition (geo_location_data);
switch (rec) {
case GEOIP_COUNTRY_EDITION:
if (TYPE_IPV4 == type_ip)
geoip_set_continent_by_geoid (ip, location, TYPE_IPV4);
else
geoip_set_continent (NULL, location);
break;
case GEOIP_COUNTRY_EDITION_V6:
if (TYPE_IPV6 == type_ip)
geoip_set_continent_by_geoid (ip, location, TYPE_IPV6);
else
geoip_set_continent (NULL, location);
break;
case GEOIP_CITY_EDITION_REV0:
case GEOIP_CITY_EDITION_REV1:
if (TYPE_IPV4 == type_ip)
geoip_set_continent_by_record (ip, location, TYPE_IPV4);
else
geoip_set_continent (NULL, location);
break;
case GEOIP_CITY_EDITION_REV0_V6:
case GEOIP_CITY_EDITION_REV1_V6:
if (TYPE_IPV6 == type_ip)
geoip_set_continent_by_record (ip, location, TYPE_IPV6);
else
geoip_set_continent (NULL, location);
break;
}
}
/* Set city data by record into the given `location` buffer based on
* the IP version. */
static void
geoip_set_city_by_record (const char *ip, char *location, GTypeIP type_ip)
{
GeoIPRecord *rec = NULL;
const char *city = NULL, *region = NULL, *addr = ip;
/* Custom GeoIP database */
if ((rec = get_geoip_record (addr, type_ip))) {
city = rec->city;
region = rec->region;
}
geoip_set_city (city, region, location);
if (rec != NULL) {
GeoIPRecord_delete (rec);
}
}
/* Set city data by geoid or record into the given `location` buffer
* based on the IP version and currently used database edition.
* It uses the custom GeoIP database - i.e., GeoLiteCity.dat */
void
geoip_get_city (const char *ip, char *location, GTypeIP type_ip)
{
unsigned char rec = GeoIP_database_edition (geo_location_data);
if (conf.geoip_database == NULL || geo_location_data == NULL)
return;
switch (rec) {
case GEOIP_CITY_EDITION_REV0:
case GEOIP_CITY_EDITION_REV1:
if (TYPE_IPV4 == type_ip)
geoip_set_city_by_record (ip, location, TYPE_IPV4);
else
geoip_set_city (NULL, NULL, location);
break;
case GEOIP_CITY_EDITION_REV0_V6:
case GEOIP_CITY_EDITION_REV1_V6:
if (TYPE_IPV6 == type_ip)
geoip_set_city_by_record (ip, location, TYPE_IPV6);
else
geoip_set_city (NULL, NULL, location);
break;
}
}
/* Entry point to set GeoIP location into the corresponding buffers,
* (continent, country, city).
*
* On error, 1 is returned
* On success, buffers are set and 0 is returned */
int
set_geolocation (char *host, char *continent, char *country, char *city)
{
int type_ip = 0;
if (!is_geoip_resource ())
return 1;
if (invalid_ipaddr (host, &type_ip))
return 1;
geoip_get_country (host, country, type_ip);
geoip_get_continent (host, continent, type_ip);
if (conf.geoip_database)
geoip_get_city (host, city, type_ip);
return 0;
}