2013-02-27 17:19:39 -08:00
|
|
|
/*
|
2014-01-23 23:46:17 +00:00
|
|
|
* Copyright (C) 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
|
2013-02-27 17:19:39 -08:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-03-01 09:58:32 +11:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2013-02-27 17:19:39 -08:00
|
|
|
#include <named/log.h>
|
|
|
|
#include <named/geoip.h>
|
|
|
|
|
|
|
|
#include <dns/geoip.h>
|
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
#ifdef HAVE_GEOIP
|
2013-02-27 17:19:39 -08:00
|
|
|
static dns_geoip_databases_t geoip_table = {
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
|
2013-02-27 17:19:39 -08:00
|
|
|
GeoIPOptions method, const char *name)
|
|
|
|
{
|
|
|
|
char *info;
|
|
|
|
GeoIP *db;
|
|
|
|
|
|
|
|
REQUIRE(dbp != NULL);
|
|
|
|
|
|
|
|
db = *dbp;
|
|
|
|
|
|
|
|
if (db != NULL) {
|
|
|
|
GeoIP_delete(db);
|
|
|
|
db = *dbp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! GeoIP_db_avail(edition)) {
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
|
|
|
|
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
|
2014-01-23 12:46:02 -08:00
|
|
|
"GeoIP %s (type %d) DB not available", name, edition);
|
|
|
|
goto fail;
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
|
|
|
|
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
|
2014-01-23 12:46:02 -08:00
|
|
|
"initializing GeoIP %s (type %d) DB", name, edition);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
db = GeoIP_open_type(edition, method);
|
|
|
|
if (db == NULL) {
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
|
|
|
|
NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
|
2014-01-23 12:46:02 -08:00
|
|
|
"failed to initialize GeoIP %s (type %d) DB%s",
|
|
|
|
name, edition, fallback == 0
|
|
|
|
? "geoip matches using this database will fail" : "");
|
|
|
|
goto fail;
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
info = GeoIP_database_info(db);
|
|
|
|
if (info != NULL)
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
|
|
|
|
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
|
|
|
|
"%s", info);
|
|
|
|
|
|
|
|
*dbp = db;
|
2014-01-23 12:46:02 -08:00
|
|
|
return;
|
|
|
|
fail:
|
|
|
|
if (fallback != 0)
|
|
|
|
init_geoip_db(dbp, fallback, 0, method, name);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GEOIP */
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_geoip_init(void) {
|
|
|
|
#ifndef HAVE_GEOIP
|
|
|
|
return;
|
|
|
|
#else
|
2014-11-16 08:43:22 -08:00
|
|
|
GeoIP_cleanup();
|
2013-02-27 17:19:39 -08:00
|
|
|
if (ns_g_geoip == NULL)
|
|
|
|
ns_g_geoip = &geoip_table;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_geoip_load(char *dir) {
|
|
|
|
#ifndef HAVE_GEOIP
|
2013-03-01 09:58:32 +11:00
|
|
|
|
|
|
|
UNUSED(dir);
|
|
|
|
|
2013-02-27 17:19:39 -08:00
|
|
|
return;
|
|
|
|
#else
|
|
|
|
GeoIPOptions method;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
method = GEOIP_STANDARD;
|
|
|
|
#else
|
|
|
|
method = GEOIP_MMAP_CACHE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ns_geoip_init();
|
|
|
|
if (dir != NULL) {
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
|
|
|
|
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
|
|
|
|
"using \"%s\" as GeoIP directory", dir);
|
|
|
|
GeoIP_setup_custom_directory(dir);
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->country_v4, GEOIP_COUNTRY_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Country (IPv4)");
|
|
|
|
#ifdef HAVE_GEOIP_V6
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Country (IPv6)");
|
|
|
|
#endif
|
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->city_v4, GEOIP_CITY_EDITION_REV1,
|
|
|
|
GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
|
2013-02-27 17:19:39 -08:00
|
|
|
#if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->city_v6, GEOIP_CITY_EDITION_REV1_V6,
|
|
|
|
GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
|
2013-02-27 17:19:39 -08:00
|
|
|
#endif
|
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->region, GEOIP_REGION_EDITION_REV1,
|
|
|
|
GEOIP_REGION_EDITION_REV0, method, "Region");
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->isp, GEOIP_ISP_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "ISP");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->org, GEOIP_ORG_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Org");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->as, GEOIP_ASNUM_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "AS");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->domain, GEOIP_DOMAIN_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Domain");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&ns_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "NetSpeed");
|
|
|
|
#endif /* HAVE_GEOIP */
|
|
|
|
}
|