"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/bin/named/geoip.c" (4 Sep 2020, 3720 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "geoip.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*! \file */
   13 
   14 #if defined(HAVE_GEOIP2)
   15 #include <maxminddb.h>
   16 #endif /* if defined(HAVE_GEOIP2) */
   17 
   18 #include <isc/print.h>
   19 #include <isc/string.h>
   20 #include <isc/util.h>
   21 
   22 #include <dns/geoip.h>
   23 
   24 #include <named/geoip.h>
   25 #include <named/log.h>
   26 
   27 static dns_geoip_databases_t geoip_table;
   28 
   29 #if defined(HAVE_GEOIP2)
   30 static MMDB_s geoip_country, geoip_city, geoip_as, geoip_isp, geoip_domain;
   31 
   32 static MMDB_s *
   33 open_geoip2(const char *dir, const char *dbfile, MMDB_s *mmdb) {
   34     char pathbuf[PATH_MAX];
   35     unsigned int n;
   36     int ret;
   37 
   38     n = snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dir, dbfile);
   39     if (n >= sizeof(pathbuf)) {
   40         isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
   41                   NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
   42                   "GeoIP2 database '%s/%s': path too long", dir,
   43                   dbfile);
   44         return (NULL);
   45     }
   46 
   47     ret = MMDB_open(pathbuf, MMDB_MODE_MMAP, mmdb);
   48     if (ret == MMDB_SUCCESS) {
   49         isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
   50                   NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
   51                   "opened GeoIP2 database '%s'", pathbuf);
   52         return (mmdb);
   53     }
   54 
   55     isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
   56               NAMED_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
   57               "unable to open GeoIP2 database '%s' (status %d)",
   58               pathbuf, ret);
   59 
   60     return (NULL);
   61 }
   62 #endif /* HAVE_GEOIP2 */
   63 
   64 void
   65 named_geoip_init(void) {
   66 #if defined(HAVE_GEOIP2)
   67     if (named_g_geoip == NULL) {
   68         named_g_geoip = &geoip_table;
   69     }
   70 #else  /* if defined(HAVE_GEOIP2) */
   71     return;
   72 #endif /* if defined(HAVE_GEOIP2) */
   73 }
   74 
   75 void
   76 named_geoip_load(char *dir) {
   77 #if defined(HAVE_GEOIP2)
   78     REQUIRE(dir != NULL);
   79 
   80     isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
   81               NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
   82               "looking for GeoIP2 databases in '%s'", dir);
   83 
   84     named_g_geoip->country = open_geoip2(dir, "GeoIP2-Country.mmdb",
   85                          &geoip_country);
   86     if (named_g_geoip->country == NULL) {
   87         named_g_geoip->country = open_geoip2(
   88             dir, "GeoLite2-Country.mmdb", &geoip_country);
   89     }
   90 
   91     named_g_geoip->city = open_geoip2(dir, "GeoIP2-City.mmdb", &geoip_city);
   92     if (named_g_geoip->city == NULL) {
   93         named_g_geoip->city = open_geoip2(dir, "GeoLite2-City.mmdb",
   94                           &geoip_city);
   95     }
   96 
   97     named_g_geoip->as = open_geoip2(dir, "GeoIP2-ASN.mmdb", &geoip_as);
   98     if (named_g_geoip->as == NULL) {
   99         named_g_geoip->as = open_geoip2(dir, "GeoLite2-ASN.mmdb",
  100                         &geoip_as);
  101     }
  102 
  103     named_g_geoip->isp = open_geoip2(dir, "GeoIP2-ISP.mmdb", &geoip_isp);
  104     named_g_geoip->domain = open_geoip2(dir, "GeoIP2-Domain.mmdb",
  105                         &geoip_domain);
  106 #else  /* if defined(HAVE_GEOIP2) */
  107     UNUSED(dir);
  108 
  109     return;
  110 #endif /* if defined(HAVE_GEOIP2) */
  111 }
  112 
  113 void
  114 named_geoip_unload(void) {
  115 #ifdef HAVE_GEOIP2
  116     if (named_g_geoip->country != NULL) {
  117         MMDB_close(named_g_geoip->country);
  118         named_g_geoip->country = NULL;
  119     }
  120     if (named_g_geoip->city != NULL) {
  121         MMDB_close(named_g_geoip->city);
  122         named_g_geoip->city = NULL;
  123     }
  124     if (named_g_geoip->as != NULL) {
  125         MMDB_close(named_g_geoip->as);
  126         named_g_geoip->as = NULL;
  127     }
  128     if (named_g_geoip->isp != NULL) {
  129         MMDB_close(named_g_geoip->isp);
  130         named_g_geoip->isp = NULL;
  131     }
  132     if (named_g_geoip->domain != NULL) {
  133         MMDB_close(named_g_geoip->domain);
  134         named_g_geoip->domain = NULL;
  135     }
  136 #endif /* ifdef HAVE_GEOIP2 */
  137 }
  138 
  139 void
  140 named_geoip_shutdown(void) {
  141 #ifdef HAVE_GEOIP2
  142     named_geoip_unload();
  143 #endif /* HAVE_GEOIP2 */
  144 }