]> jfr.im git - irc/quakenet/newserv.git/blob - geoip/libGeoIP/GeoIP.h
CHANSERV: fix issue where chanserv_relay doesn't wait for db to be loaded before...
[irc/quakenet/newserv.git] / geoip / libGeoIP / GeoIP.h
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* GeoIP.h
3 *
4 * Copyright (C) 2006 MaxMind LLC
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef GEOIP_H
22 #define GEOIP_H
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32
33 #include<stdio.h>
34 #include<stdlib.h>
35 #include<string.h>
36 #include <sys/types.h> /* for fstat */
37 #include <sys/stat.h> /* for fstat */
38
39 #define SEGMENT_RECORD_LENGTH 3
40 #define STANDARD_RECORD_LENGTH 3
41 #define ORG_RECORD_LENGTH 4
42 #define MAX_RECORD_LENGTH 4
43 #define NUM_DB_TYPES 20
44
45 /* 128 bit address in network order */
46 typedef struct in6_addr geoipv6_t;
47
48 #define GEOIP_CHKBIT_V6(bit,ptr) (ptr[((127UL - bit) >> 3)] & (1UL << (~(127 - bit) & 7)))
49
50 typedef struct GeoIPTag {
51 FILE *GeoIPDatabase;
52 char *file_path;
53 unsigned char *cache;
54 unsigned char *index_cache;
55 unsigned int *databaseSegments;
56 char databaseType;
57 time_t mtime;
58 int flags;
59 off_t size;
60 char record_length;
61 int charset; /* 0 iso-8859-1 1 utf8 */
62 int record_iter; /* used in GeoIP_next_record */
63 int netmask; /* netmask of last lookup - set using depth in _GeoIP_seek_record */
64 time_t last_mtime_check;
65 } GeoIP;
66
67
68 typedef enum {
69 GEOIP_CHARSET_ISO_8859_1 = 0,
70 GEOIP_CHARSET_UTF8 = 1
71 } GeoIPCharset;
72
73 typedef struct GeoIPRegionTag {
74 char country_code[3];
75 char region[3];
76 } GeoIPRegion;
77
78 typedef enum {
79 GEOIP_STANDARD = 0,
80 GEOIP_MEMORY_CACHE = 1,
81 GEOIP_CHECK_CACHE = 2,
82 GEOIP_INDEX_CACHE = 4,
83 GEOIP_MMAP_CACHE = 8,
84 } GeoIPOptions;
85
86 typedef enum {
87 GEOIP_COUNTRY_EDITION = 1,
88 GEOIP_REGION_EDITION_REV0 = 7,
89 GEOIP_CITY_EDITION_REV0 = 6,
90 GEOIP_ORG_EDITION = 5,
91 GEOIP_ISP_EDITION = 4,
92 GEOIP_CITY_EDITION_REV1 = 2,
93 GEOIP_REGION_EDITION_REV1 = 3,
94 GEOIP_PROXY_EDITION = 8,
95 GEOIP_ASNUM_EDITION = 9,
96 GEOIP_NETSPEED_EDITION = 10,
97 GEOIP_DOMAIN_EDITION = 11,
98 GEOIP_COUNTRY_EDITION_V6 = 12,
99 } GeoIPDBTypes;
100
101 typedef enum {
102 GEOIP_ANON_PROXY = 1,
103 GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2,
104 GEOIP_HTTP_CLIENT_IP_PROXY = 3,
105 } GeoIPProxyTypes;
106
107 typedef enum {
108 GEOIP_UNKNOWN_SPEED = 0,
109 GEOIP_DIALUP_SPEED = 1,
110 GEOIP_CABLEDSL_SPEED = 2,
111 GEOIP_CORPORATE_SPEED = 3,
112 } GeoIPNetspeedValues;
113
114 extern char **GeoIPDBFileName;
115 extern const char * GeoIPDBDescription[NUM_DB_TYPES];
116 extern const char *GeoIPCountryDBFileName;
117 extern const char *GeoIPRegionDBFileName;
118 extern const char *GeoIPCityDBFileName;
119 extern const char *GeoIPOrgDBFileName;
120 extern const char *GeoIPISPDBFileName;
121
122 /* Warning: do not use those arrays as doing so may break your
123 * program with newer GeoIP versions */
124 extern const char GeoIP_country_code[253][3];
125 extern const char GeoIP_country_code3[253][4];
126 extern const char * GeoIP_country_name[253];
127 extern const char GeoIP_country_continent[253][3];
128
129 #ifdef DLL
130 #define GEOIP_API __declspec(dllexport)
131 #else
132 #define GEOIP_API
133 #endif /* DLL */
134
135 GEOIP_API void GeoIP_setup_custom_directory(char *dir);
136 GEOIP_API GeoIP* GeoIP_open_type (int type, int flags);
137 GEOIP_API GeoIP* GeoIP_new(int flags);
138 GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags);
139 GEOIP_API int GeoIP_db_avail(int type);
140 GEOIP_API void GeoIP_delete(GeoIP* gi);
141 GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr);
142 GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host);
143 GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr);
144 GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host);
145 GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr);
146 GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host);
147 GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
148 GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum);
149 GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum);
150
151 /* */
152 GEOIP_API const char *GeoIP_country_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
153 GEOIP_API const char *GeoIP_country_code_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
154 GEOIP_API const char *GeoIP_country_code3_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
155
156 /* Deprecated - for backwards compatibility only */
157 GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr);
158 GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host);
159 GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr);
160 GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host);
161 GEOIP_API char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum);
162
163 GEOIP_API char *GeoIP_org_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
164 GEOIP_API char *GeoIP_org_by_addr_v6 (GeoIP* gi, const char *addr);
165 GEOIP_API char *GeoIP_org_by_name_v6 (GeoIP* gi, const char *name);
166
167 /* End deprecated */
168
169 GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr);
170 GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host);
171 GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum);
172
173 GEOIP_API int GeoIP_id_by_addr_v6 (GeoIP* gi, const char *addr);
174 GEOIP_API int GeoIP_id_by_name_v6 (GeoIP* gi, const char *host);
175 GEOIP_API int GeoIP_id_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
176
177 GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr);
178 GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host);
179 GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum);
180
181 GEOIP_API GeoIPRegion * GeoIP_region_by_addr_v6 (GeoIP* gi, const char *addr);
182 GEOIP_API GeoIPRegion * GeoIP_region_by_name_v6 (GeoIP* gi, const char *host);
183 GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum_v6 (GeoIP *gi, geoipv6_t ipnum);
184
185 /* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
186 GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
187
188 GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir);
189
190 GEOIP_API void GeoIP_assign_region_by_inetaddr_v6(GeoIP* gi, geoipv6_t inetaddr, GeoIPRegion *gir);
191
192 /* Used to query GeoIP Organization, ISP and AS Number databases */
193 GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
194 GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr);
195 GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host);
196
197 GEOIP_API char *GeoIP_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
198 GEOIP_API char *GeoIP_name_by_addr_v6 (GeoIP* gi, const char *addr);
199 GEOIP_API char *GeoIP_name_by_name_v6 (GeoIP* gi, const char *name);
200
201 /** return two letter country code */
202 GEOIP_API const char* GeoIP_code_by_id(int id);
203
204 /** return three letter country code */
205 GEOIP_API const char* GeoIP_code3_by_id(int id);
206
207 /** return full name of country */
208 GEOIP_API const char* GeoIP_name_by_id(int id);
209
210 /** return continent of country */
211 GEOIP_API const char* GeoIP_continent_by_id(int id);
212
213 /** return id by country code **/
214 GEOIP_API int GeoIP_id_by_code(const char *country);
215
216 /** return return number of known countries */
217 GEOIP_API unsigned GeoIP_num_countries(void);
218
219 GEOIP_API char *GeoIP_database_info (GeoIP* gi);
220 GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi);
221
222 GEOIP_API int GeoIP_charset (GeoIP* gi);
223 GEOIP_API int GeoIP_set_charset (GeoIP* gi, int charset);
224
225 GEOIP_API int GeoIP_last_netmask (GeoIP* gi);
226 GEOIP_API char **GeoIP_range_by_ip (GeoIP* gi, const char *addr);
227 GEOIP_API void GeoIP_range_by_ip_delete(char **ptr);
228
229 /* Convert region code to region name */
230 GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code);
231
232 /* Get timezone from country and region code */
233 GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code);
234
235 #ifdef BSD
236 #define memcpy(dest, src, n) bcopy(src, dest, n)
237 #endif
238
239 #ifdef __cplusplus
240 }
241 #endif
242
243 #endif /* GEOIP_H */