xref: /minix3/external/bsd/bind/dist/bin/named/geoip.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: geoip.c,v 1.1.1.3 2014/12/10 03:34:24 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2013, 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
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.
9  *
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.
17  */
18 
19 /*! \file */
20 
21 #include <config.h>
22 
23 #include <isc/util.h>
24 
25 #include <named/log.h>
26 #include <named/geoip.h>
27 
28 #include <dns/geoip.h>
29 
30 #ifdef HAVE_GEOIP
31 static dns_geoip_databases_t geoip_table = {
32 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
33 };
34 
35 static void
init_geoip_db(GeoIP ** dbp,GeoIPDBTypes edition,GeoIPDBTypes fallback,GeoIPOptions method,const char * name)36 init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
37 	      GeoIPOptions method, const char *name)
38 {
39 	char *info;
40 	GeoIP *db;
41 
42 	REQUIRE(dbp != NULL);
43 
44 	db = *dbp;
45 
46 	if (db != NULL) {
47 		GeoIP_delete(db);
48 		db = *dbp = NULL;
49 	}
50 
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;
56 	}
57 
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);
61 
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;
70 	}
71 
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);
77 
78 	*dbp = db;
79 	return;
80  fail:
81 	if (fallback != 0)
82 		init_geoip_db(dbp, fallback, 0, method, name);
83 
84 }
85 #endif /* HAVE_GEOIP */
86 
87 void
ns_geoip_init(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
96 }
97 
98 void
ns_geoip_load(char * dir)99 ns_geoip_load(char *dir) {
100 #ifndef HAVE_GEOIP
101 
102 	UNUSED(dir);
103 
104 	return;
105 #else
106 	GeoIPOptions method;
107 
108 #ifdef _WIN32
109 	method = GEOIP_STANDARD;
110 #else
111 	method = GEOIP_MMAP_CACHE;
112 #endif
113 
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);
120 	}
121 
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
128 
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
135 
136 	init_geoip_db(&ns_g_geoip->region, GEOIP_REGION_EDITION_REV1,
137 		      GEOIP_REGION_EDITION_REV0, method, "Region");
138 
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 */
150 }
151