geoip.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /*! \file */
00018 
00019 #include <config.h>
00020 
00021 #include <isc/util.h>
00022 
00023 #include <named/log.h>
00024 #include <named/geoip.h>
00025 
00026 #include <dns/geoip.h>
00027 
00028 #ifdef HAVE_GEOIP
00029 static dns_geoip_databases_t geoip_table = {
00030         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
00031 };
00032 
00033 static void
00034 init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
00035               GeoIPOptions method, const char *name)
00036 {
00037         char *info;
00038         GeoIP *db;
00039 
00040         REQUIRE(dbp != NULL);
00041 
00042         db = *dbp;
00043 
00044         if (db != NULL) {
00045                 GeoIP_delete(db);
00046                 db = *dbp = NULL;
00047         }
00048 
00049         if (! GeoIP_db_avail(edition)) {
00050                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
00051                         NS_LOGMODULE_SERVER, ISC_LOG_INFO,
00052                         "GeoIP %s (type %d) DB not available", name, edition);
00053                 goto fail;
00054         }
00055 
00056         isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
00057                 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
00058                 "initializing GeoIP %s (type %d) DB", name, edition);
00059 
00060         db = GeoIP_open_type(edition, method);
00061         if (db == NULL) {
00062                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
00063                         NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
00064                         "failed to initialize GeoIP %s (type %d) DB%s",
00065                         name, edition, fallback == 0
00066                          ? "geoip matches using this database will fail" : "");
00067                 goto fail;
00068         }
00069 
00070         info = GeoIP_database_info(db);
00071         if (info != NULL)
00072                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
00073                               NS_LOGMODULE_SERVER, ISC_LOG_INFO,
00074                               "%s", info);
00075 
00076         *dbp = db;
00077         return;
00078  fail:
00079         if (fallback != 0)
00080                 init_geoip_db(dbp, fallback, 0, method, name);
00081 
00082 }
00083 #endif /* HAVE_GEOIP */
00084 
00085 void
00086 ns_geoip_init(void) {
00087 #ifndef HAVE_GEOIP
00088         return;
00089 #else
00090         GeoIP_cleanup();
00091         if (ns_g_geoip == NULL)
00092                 ns_g_geoip = &geoip_table;
00093 #endif
00094 }
00095 
00096 void
00097 ns_geoip_load(char *dir) {
00098 #ifndef HAVE_GEOIP
00099 
00100         UNUSED(dir);
00101 
00102         return;
00103 #else
00104         GeoIPOptions method;
00105 
00106 #ifdef _WIN32
00107         method = GEOIP_STANDARD;
00108 #else
00109         method = GEOIP_MMAP_CACHE;
00110 #endif
00111 
00112         ns_geoip_init();
00113         if (dir != NULL) {
00114                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
00115                               NS_LOGMODULE_SERVER, ISC_LOG_INFO,
00116                               "using \"%s\" as GeoIP directory", dir);
00117                 GeoIP_setup_custom_directory(dir);
00118         }
00119 
00120         init_geoip_db(&ns_g_geoip->country_v4, GEOIP_COUNTRY_EDITION, 0,
00121                       method, "Country (IPv4)");
00122 #ifdef HAVE_GEOIP_V6
00123         init_geoip_db(&ns_g_geoip->country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
00124                       method, "Country (IPv6)");
00125 #endif
00126 
00127         init_geoip_db(&ns_g_geoip->city_v4, GEOIP_CITY_EDITION_REV1,
00128                       GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
00129 #if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
00130         init_geoip_db(&ns_g_geoip->city_v6, GEOIP_CITY_EDITION_REV1_V6,
00131                       GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
00132 #endif
00133 
00134         init_geoip_db(&ns_g_geoip->region, GEOIP_REGION_EDITION_REV1,
00135                       GEOIP_REGION_EDITION_REV0, method, "Region");
00136 
00137         init_geoip_db(&ns_g_geoip->isp, GEOIP_ISP_EDITION, 0,
00138                       method, "ISP");
00139         init_geoip_db(&ns_g_geoip->org, GEOIP_ORG_EDITION, 0,
00140                       method, "Org");
00141         init_geoip_db(&ns_g_geoip->as, GEOIP_ASNUM_EDITION, 0,
00142                       method, "AS");
00143         init_geoip_db(&ns_g_geoip->domain, GEOIP_DOMAIN_EDITION, 0,
00144                       method, "Domain");
00145         init_geoip_db(&ns_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0,
00146                       method, "NetSpeed");
00147 #endif /* HAVE_GEOIP */
00148 }

Generated on Tue Apr 28 17:40:56 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha