forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolocation.c
167 lines (146 loc) · 4.35 KB
/
geolocation.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
/**
* geolocation.c -- GeoLocation related functions
* Copyright (C) 2009-2014 by Gerardo Orellana <[email protected]>
* GoAccess - An Ncurses apache weblog analyzer & interactive viewer
*
* 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 2 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.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_LIBGEOIP
#include <GeoIP.h>
#include <GeoIPCity.h>
#endif
#include "geolocation.h"
#include "error.h"
#include "settings.h"
GeoIP *geo_location_data;
/* Get continent name concatenated with code */
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 "-- Location Unknown";
}
/* Geolocation data */
GeoIP *
geoip_open_db (const char *db)
{
GeoIP *geoip;
geoip = GeoIP_open (db, GEOIP_INDEX_CACHE);
if (geoip == NULL)
FATAL ("Unable to open GeoIP City database: %s\n", db);
LOG_DEBUG (("Opened GeoIP City database: %s\n", db));
return geoip;
}
static void
geoip_set_country (const char *country, const char *code, char *loc)
{
if (country && code)
sprintf (loc, "%s %s", code, country);
else
sprintf (loc, "%s", "Country Unknown");
}
void
geoip_get_country (const char *ip, char *location)
{
GeoIPRecord *rec = NULL;
const char *country = NULL, *code = NULL, *addr = ip;
int geoid = 0;
/* Custom GeoIP database */
if (conf.geoip_city_data != NULL && geo_location_data != NULL) {
rec = GeoIP_record_by_name (geo_location_data, addr);
if (rec) {
code = rec->country_code;
country = rec->country_name;
}
}
/* Legacy GeoIP database */
else if (geo_location_data != NULL) {
geoid = GeoIP_id_by_name (geo_location_data, addr);
country = GeoIP_country_name_by_name (geo_location_data, addr);
code = GeoIP_code_by_id (geoid);
}
geoip_set_country (country, code, location);
if (rec != NULL)
GeoIPRecord_delete (rec);
}
static void
geoip_set_continent (const char *continent, char *loc)
{
if (continent)
sprintf (loc, "%s", get_continent_name_and_code (continent));
else
sprintf (loc, "%s", "Continent Unknown");
}
void
geoip_get_continent (const char *ip, char *location)
{
GeoIPRecord *rec = NULL;
const char *continent = NULL, *addr = ip;
int geoid = 0;
/* Custom GeoIP database */
if (conf.geoip_city_data != NULL && geo_location_data != NULL) {
rec = GeoIP_record_by_name (geo_location_data, addr);
if (rec)
continent = rec->continent_code;
}
/* Legacy GeoIP database */
else if (geo_location_data != NULL) {
geoid = GeoIP_id_by_name (geo_location_data, addr);
continent = GeoIP_continent_by_id (geoid);
}
if (rec != NULL)
GeoIPRecord_delete (rec);
geoip_set_continent (continent, location);
}
static void
geoip_set_city (const char *city, const char *region, char *loc)
{
sprintf (loc, "%s, %s", city ? city : "N/A City",
region ? region : "N/A Region");
}
/* Custom GeoIP database - i.e., GeoLiteCity.dat */
void
geoip_get_city (const char *ip, char *location)
{
GeoIPRecord *rec = NULL;
const char *city = NULL, *region = NULL, *addr = ip;
if (geo_location_data != NULL)
rec = GeoIP_record_by_name (geo_location_data, addr);
if (rec != NULL) {
city = rec->city;
region = rec->region;
}
geoip_set_city (city, region, location);
if (rec != NULL)
GeoIPRecord_delete (rec);
}