2 * GeoIP database support
4 * Copyright 2008, Gerald Combs <gerald@wireshark.org>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * We currently return a single string for each database. Some databases,
29 * e.g. GeoIPCity, can return other info such as area codes.
40 #include "GeoIPCity.h"
45 #include "report_err.h"
46 #include "value_string.h"
47 #include <wsutil/file_util.h>
49 /* This needs to match NUM_GEOIP_COLS in hostlist_table.h */
50 #define MAX_GEOIP_DBS 13
52 /* Column names for each database type */
53 value_string geoip_type_name_vals[] = {
54 { GEOIP_COUNTRY_EDITION, "Country" },
55 { GEOIP_REGION_EDITION_REV0, "Region" },
56 { GEOIP_CITY_EDITION_REV0, "City"},
57 { GEOIP_ORG_EDITION, "Organization" },
58 { GEOIP_ISP_EDITION, "ISP" },
59 { GEOIP_CITY_EDITION_REV1, "City" },
60 { GEOIP_REGION_EDITION_REV1, "Region" },
61 { GEOIP_PROXY_EDITION, "Proxy" },
62 { GEOIP_ASNUM_EDITION, "AS Number" },
63 { GEOIP_NETSPEED_EDITION, "Speed" },
64 { GEOIP_DOMAIN_EDITION, "Domain" },
65 { WS_LAT_FAKE_EDITION, "Latitude" }, /* fake database */
66 { WS_LON_FAKE_EDITION, "Longitude" }, /* fake database */
70 static GArray *geoip_dat_arr = NULL;
72 /* UAT definitions. Copied from oids.c */
73 typedef struct _geoip_db_path_t {
77 static geoip_db_path_t *geoip_db_paths = NULL;
78 static guint num_geoip_db_paths = 0;
79 static uat_t *geoip_db_paths_uat = NULL;
80 UAT_CSTRING_CB_DEF(geoip_mod, path, geoip_db_path_t)
84 * Scan a directory for GeoIP databases and load them
87 geoip_dat_scan_dir(const char *dirname) {
94 if ((dir = ws_dir_open(dirname, 0, NULL)) != NULL) {
95 while ((file = ws_dir_read_name(dir)) != NULL) {
96 name = ws_dir_get_name(file);
97 if (g_str_has_prefix(file, "Geo") && g_str_has_suffix(file, ".dat")) {
98 datname = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dirname, name);
99 gi = GeoIP_open(datname, GEOIP_MEMORY_CACHE);
101 g_array_append_val(geoip_dat_arr, gi);
110 static void* geoip_db_path_copy_cb(void* dest, const void* orig, unsigned len _U_) {
111 const geoip_db_path_t *m = orig;
112 geoip_db_path_t *d = dest;
114 d->path = g_strdup(m->path);
119 static void geoip_db_path_free_cb(void* p) {
120 geoip_db_path_t *m = p;
125 * Initialize GeoIP lookups
128 geoip_db_init(void) {
130 static uat_field_t geoip_db_paths_fields[] = {
131 UAT_FLD_PATHNAME(geoip_mod, path, "Database pathname", "The database path"),
134 char* geoip_load_error = NULL;
136 geoip_dat_arr = g_array_new(FALSE, FALSE, sizeof(GeoIP *));
138 geoip_db_paths_uat = uat_new("GeoIP Database Paths",
139 sizeof(geoip_db_path_t),
142 (void*)&geoip_db_paths,
146 geoip_db_path_copy_cb,
148 geoip_db_path_free_cb,
149 geoip_db_paths_fields);
151 uat_load(geoip_db_paths_uat, &geoip_load_error);
153 if (geoip_load_error) {
154 report_failure("Error loading GeoIP database path table: %s", geoip_load_error);
158 for (i = 0; i < num_geoip_db_paths; i++) {
159 if (geoip_db_paths[i].path) {
160 geoip_dat_scan_dir(geoip_db_paths[i].path);
164 /* add fake databases for latitude and longitude (using "City" in reality) */
169 gi_lat = g_malloc(sizeof (GeoIP));
170 gi_lat->databaseType = WS_LAT_FAKE_EDITION;
171 g_array_append_val(geoip_dat_arr, gi_lat);
172 gi_lon = g_malloc(sizeof (GeoIP));
173 gi_lon->databaseType = WS_LON_FAKE_EDITION;
174 g_array_append_val(geoip_dat_arr, gi_lon);
179 geoip_db_num_dbs(void) {
180 return geoip_dat_arr->len;
184 geoip_db_name(guint dbnum) {
187 gi = g_array_index(geoip_dat_arr, GeoIP *, dbnum);
189 return (val_to_str(gi->databaseType, geoip_type_name_vals, "Unknown database"));
191 return "Invalid database";
195 geoip_db_type(guint dbnum) {
198 gi = g_array_index(geoip_dat_arr, GeoIP *, dbnum);
200 return (gi->databaseType);
206 geoip_db_lookup_latlon(guint32 addr, float *lat, float *lon) {
211 for (i = 0; i < geoip_db_num_dbs(); i++) {
212 gi = g_array_index(geoip_dat_arr, GeoIP *, i);
214 switch (gi->databaseType) {
215 case GEOIP_CITY_EDITION_REV0:
216 case GEOIP_CITY_EDITION_REV1:
217 gir = GeoIP_record_by_ipnum(gi, addr);
219 *lat = gir->latitude;
220 *lon = gir->longitude;
234 #define VAL_STR_LEN 100
236 geoip_db_lookup_ipv4(guint dbnum, guint32 addr, char *not_found) {
239 const char *ret = not_found;
240 static char val[VAL_STR_LEN];
242 gi = g_array_index(geoip_dat_arr, GeoIP *, dbnum);
244 switch (gi->databaseType) {
245 case GEOIP_COUNTRY_EDITION:
246 ret = GeoIP_country_name_by_ipnum(gi, addr);
249 case GEOIP_CITY_EDITION_REV0:
250 case GEOIP_CITY_EDITION_REV1:
251 gir = GeoIP_record_by_ipnum(gi, addr);
252 if (gir && gir->city && gir->region) {
253 g_snprintf(val, VAL_STR_LEN, "%s, %s", gir->city, gir->region);
255 } else if (gir && gir->city) {
256 g_snprintf(val, VAL_STR_LEN, "%s", gir->city);
261 case GEOIP_ORG_EDITION:
262 case GEOIP_ISP_EDITION:
263 case GEOIP_ASNUM_EDITION:
264 ret = GeoIP_name_by_ipnum(gi, addr);
267 case WS_LAT_FAKE_EDITION:
272 if(geoip_db_lookup_latlon(addr, &lat, &lon) == 0) {
273 g_snprintf(val, VAL_STR_LEN, "%f", lat);
274 c = strchr(val, ',');
275 if (c != NULL) *c = '.';
281 case WS_LON_FAKE_EDITION:
286 if(geoip_db_lookup_latlon(addr, &lat, &lon) == 0) {
287 g_snprintf(val, VAL_STR_LEN, "%f", lon);
288 c = strchr(val, ',');
289 if (c != NULL) *c = '.';
306 geoip_db_get_paths(void) {
307 GString* path_str = NULL;
312 path_str = g_string_new("");
314 path_separator = ';';
316 path_separator = ':';
319 for (i = 0; i < num_geoip_db_paths; i++) {
320 if (geoip_db_paths[i].path) {
321 g_string_append_printf(path_str, "%s%c", geoip_db_paths[i].path, path_separator);
325 g_string_truncate(path_str, path_str->len-1);
326 path_ret = path_str->str;
327 g_string_free(path_str, FALSE);
332 #else /* HAVE_GEOIP */
334 geoip_db_init(void) {}
337 geoip_db_num_dbs(void) {
342 geoip_db_name(guint dbnum _U_) {
343 return "Unsupported";
347 geoip_db_type(guint dbnum _U_) {
352 geoip_db_lookup_ipv4(guint dbnum _U_, guint32 addr _U_, char *not_found) {
357 geoip_db_get_paths(void) {
361 #endif /* HAVE_GEOIP */
369 * indent-tabs-mode: t
372 * ex: set shiftwidth=4 tabstop=4 noexpandtab
373 * :indentSize=4:tabSize=4:noTabs=false: