version 1.1.1.1, 2018/08/12 12:07:41 |
version 1.1.1.2, 2019/09/05 19:27:32 |
|
|
|
|
#include <config.h> |
#include <config.h> |
|
|
|
#if defined(HAVE_GEOIP2) |
|
#include <maxminddb.h> |
|
#elif defined(HAVE_GEOIP) |
|
#include <GeoIP.h> |
|
#include <GeoIPCity.h> |
|
#endif |
|
|
|
#include <isc/print.h> |
|
#include <isc/string.h> |
#include <isc/util.h> |
#include <isc/util.h> |
|
|
|
#include <dns/geoip.h> |
|
|
#include <named/log.h> |
#include <named/log.h> |
#include <named/geoip.h> |
#include <named/geoip.h> |
|
|
#include <dns/geoip.h> |
static dns_geoip_databases_t geoip_table; |
|
|
#ifdef HAVE_GEOIP |
|
static dns_geoip_databases_t geoip_table = { |
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
|
}; |
|
|
|
|
#if defined(HAVE_GEOIP) |
static void |
static void |
init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback, |
init_geoip_db(void **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback, |
GeoIPOptions method, const char *name) |
GeoIPOptions method, const char *name) |
{ |
{ |
char *info; |
char *info; |
Line 36 init_geoip_db(GeoIP **dbp, GeoIPDBTypes |
|
Line 43 init_geoip_db(GeoIP **dbp, GeoIPDBTypes |
|
|
|
REQUIRE(dbp != NULL); |
REQUIRE(dbp != NULL); |
|
|
db = *dbp; |
db = (GeoIP *)*dbp; |
|
|
if (db != NULL) { |
if (db != NULL) { |
GeoIP_delete(db); |
GeoIP_delete(db); |
Line 74 init_geoip_db(GeoIP **dbp, GeoIPDBTypes |
|
Line 81 init_geoip_db(GeoIP **dbp, GeoIPDBTypes |
|
|
|
*dbp = db; |
*dbp = db; |
return; |
return; |
|
|
fail: |
fail: |
if (fallback != 0) |
if (fallback != 0) { |
init_geoip_db(dbp, fallback, 0, method, name); |
init_geoip_db(dbp, fallback, 0, method, name); |
|
} |
|
|
} |
} |
#endif /* HAVE_GEOIP */ |
#elif defined(HAVE_GEOIP2) |
|
static MMDB_s geoip_country, geoip_city, geoip_as, geoip_isp, geoip_domain; |
|
|
|
static MMDB_s * |
|
open_geoip2(const char *dir, const char *dbfile, MMDB_s *mmdb) { |
|
char pathbuf[PATH_MAX]; |
|
unsigned int n; |
|
int ret; |
|
|
|
n = snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dir, dbfile); |
|
if (n >= sizeof(pathbuf)) { |
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, |
|
NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR, |
|
"GeoIP2 database '%s/%s': path too long", |
|
dir, dbfile); |
|
return (NULL); |
|
} |
|
|
|
ret = MMDB_open(pathbuf, MMDB_MODE_MMAP, mmdb); |
|
if (ret == MMDB_SUCCESS) { |
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, |
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO, |
|
"opened GeoIP2 database '%s'", pathbuf); |
|
return (mmdb); |
|
} |
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, |
|
NAMED_LOGMODULE_SERVER, ISC_LOG_DEBUG(1), |
|
"unable to open GeoIP2 database '%s' (status %d)", |
|
pathbuf, ret); |
|
|
|
return (NULL); |
|
} |
|
#endif /* HAVE_GEOIP2 */ |
|
|
|
|
void |
void |
named_geoip_init(void) { |
named_geoip_init(void) { |
#ifndef HAVE_GEOIP |
#if defined(HAVE_GEOIP) || defined(HAVE_GEOIP2) |
return; |
if (named_g_geoip == NULL) { |
#else |
|
GeoIP_cleanup(); |
|
if (named_g_geoip == NULL) |
|
named_g_geoip = &geoip_table; |
named_g_geoip = &geoip_table; |
|
} |
|
#if defined(HAVE_GEOIP) |
|
GeoIP_cleanup(); |
|
#endif |
|
#else |
|
return; |
#endif |
#endif |
} |
} |
|
|
void |
void |
named_geoip_load(char *dir) { |
named_geoip_load(char *dir) { |
#ifndef HAVE_GEOIP |
#if defined(HAVE_GEOIP2) |
|
REQUIRE(dir != NULL); |
|
|
UNUSED(dir); |
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, |
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO, |
|
"looking for GeoIP2 databases in '%s'", dir); |
|
|
return; |
named_g_geoip->country = open_geoip2(dir, "GeoIP2-Country.mmdb", |
#else |
&geoip_country); |
|
if (named_g_geoip->country == NULL) { |
|
named_g_geoip->country = open_geoip2(dir, |
|
"GeoLite2-Country.mmdb", |
|
&geoip_country); |
|
} |
|
|
|
named_g_geoip->city = open_geoip2(dir, "GeoIP2-City.mmdb", |
|
&geoip_city); |
|
if (named_g_geoip->city == NULL) { |
|
named_g_geoip->city = open_geoip2(dir, "GeoLite2-City.mmdb", |
|
&geoip_city); |
|
} |
|
|
|
named_g_geoip->as = open_geoip2(dir, "GeoIP2-ASN.mmdb", &geoip_as); |
|
if (named_g_geoip->as == NULL) { |
|
named_g_geoip->as = open_geoip2(dir, "GeoLite2-ASN.mmdb", |
|
&geoip_as); |
|
} |
|
|
|
named_g_geoip->isp = open_geoip2(dir, "GeoIP2-ISP.mmdb", &geoip_isp); |
|
named_g_geoip->domain = open_geoip2(dir, "GeoIP2-Domain.mmdb", |
|
&geoip_domain); |
|
#elif defined(HAVE_GEOIP) |
GeoIPOptions method; |
GeoIPOptions method; |
|
|
#ifdef _WIN32 |
#ifdef _WIN32 |
Line 143 named_geoip_load(char *dir) { |
|
Line 215 named_geoip_load(char *dir) { |
|
method, "Domain"); |
method, "Domain"); |
init_geoip_db(&named_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0, |
init_geoip_db(&named_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0, |
method, "NetSpeed"); |
method, "NetSpeed"); |
#endif /* HAVE_GEOIP */ |
#else |
|
UNUSED(dir); |
|
|
|
return; |
|
#endif |
|
} |
|
|
|
void |
|
named_geoip_shutdown(void) { |
|
#ifdef HAVE_GEOIP2 |
|
if (named_g_geoip->country != NULL) { |
|
MMDB_close(named_g_geoip->country); |
|
named_g_geoip->country = NULL; |
|
} |
|
if (named_g_geoip->city != NULL) { |
|
MMDB_close(named_g_geoip->city); |
|
named_g_geoip->city = NULL; |
|
} |
|
if (named_g_geoip->as != NULL) { |
|
MMDB_close(named_g_geoip->as); |
|
named_g_geoip->as = NULL; |
|
} |
|
if (named_g_geoip->isp != NULL) { |
|
MMDB_close(named_g_geoip->isp); |
|
named_g_geoip->isp = NULL; |
|
} |
|
if (named_g_geoip->domain != NULL) { |
|
MMDB_close(named_g_geoip->domain); |
|
named_g_geoip->domain = NULL; |
|
} |
|
#endif /* HAVE_GEOIP2 */ |
|
|
|
dns_geoip_shutdown(); |
} |
} |