Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / bin / named / geoip.c
blob3a791015f4831623f0f27b7b726e8d758c025482
1 /* $NetBSD: geoip.c,v 1.1.1.3 2014/12/10 03:34:24 christos Exp $ */
3 /*
4 * Copyright (C) 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /*! \file */
21 #include <config.h>
23 #include <isc/util.h>
25 #include <named/log.h>
26 #include <named/geoip.h>
28 #include <dns/geoip.h>
30 #ifdef HAVE_GEOIP
31 static dns_geoip_databases_t geoip_table = {
32 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
35 static void
36 init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
37 GeoIPOptions method, const char *name)
39 char *info;
40 GeoIP *db;
42 REQUIRE(dbp != NULL);
44 db = *dbp;
46 if (db != NULL) {
47 GeoIP_delete(db);
48 db = *dbp = NULL;
51 if (! GeoIP_db_avail(edition)) {
52 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
53 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
54 "GeoIP %s (type %d) DB not available", name, edition);
55 goto fail;
58 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
59 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
60 "initializing GeoIP %s (type %d) DB", name, edition);
62 db = GeoIP_open_type(edition, method);
63 if (db == NULL) {
64 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
65 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
66 "failed to initialize GeoIP %s (type %d) DB%s",
67 name, edition, fallback == 0
68 ? "geoip matches using this database will fail" : "");
69 goto fail;
72 info = GeoIP_database_info(db);
73 if (info != NULL)
74 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
75 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
76 "%s", info);
78 *dbp = db;
79 return;
80 fail:
81 if (fallback != 0)
82 init_geoip_db(dbp, fallback, 0, method, name);
85 #endif /* HAVE_GEOIP */
87 void
88 ns_geoip_init(void) {
89 #ifndef HAVE_GEOIP
90 return;
91 #else
92 GeoIP_cleanup();
93 if (ns_g_geoip == NULL)
94 ns_g_geoip = &geoip_table;
95 #endif
98 void
99 ns_geoip_load(char *dir) {
100 #ifndef HAVE_GEOIP
102 UNUSED(dir);
104 return;
105 #else
106 GeoIPOptions method;
108 #ifdef _WIN32
109 method = GEOIP_STANDARD;
110 #else
111 method = GEOIP_MMAP_CACHE;
112 #endif
114 ns_geoip_init();
115 if (dir != NULL) {
116 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
117 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
118 "using \"%s\" as GeoIP directory", dir);
119 GeoIP_setup_custom_directory(dir);
122 init_geoip_db(&ns_g_geoip->country_v4, GEOIP_COUNTRY_EDITION, 0,
123 method, "Country (IPv4)");
124 #ifdef HAVE_GEOIP_V6
125 init_geoip_db(&ns_g_geoip->country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
126 method, "Country (IPv6)");
127 #endif
129 init_geoip_db(&ns_g_geoip->city_v4, GEOIP_CITY_EDITION_REV1,
130 GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
131 #if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
132 init_geoip_db(&ns_g_geoip->city_v6, GEOIP_CITY_EDITION_REV1_V6,
133 GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
134 #endif
136 init_geoip_db(&ns_g_geoip->region, GEOIP_REGION_EDITION_REV1,
137 GEOIP_REGION_EDITION_REV0, method, "Region");
139 init_geoip_db(&ns_g_geoip->isp, GEOIP_ISP_EDITION, 0,
140 method, "ISP");
141 init_geoip_db(&ns_g_geoip->org, GEOIP_ORG_EDITION, 0,
142 method, "Org");
143 init_geoip_db(&ns_g_geoip->as, GEOIP_ASNUM_EDITION, 0,
144 method, "AS");
145 init_geoip_db(&ns_g_geoip->domain, GEOIP_DOMAIN_EDITION, 0,
146 method, "Domain");
147 init_geoip_db(&ns_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0,
148 method, "NetSpeed");
149 #endif /* HAVE_GEOIP */