1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
4 * Copyright (C) 2006 MaxMind LLC
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.
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.
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
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
36 #include <sys/types.h> /* for fstat */
37 #include <sys/stat.h> /* for fstat */
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
45 /* 128 bit address in network order */
46 typedef struct in6_addr geoipv6_t
;
48 #define GEOIP_CHKBIT_V6(bit,ptr) (ptr[((127UL - bit) >> 3)] & (1UL << (~(127 - bit) & 7)))
50 typedef struct GeoIPTag
{
54 unsigned char *index_cache
;
55 unsigned int *databaseSegments
;
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
;
69 GEOIP_CHARSET_ISO_8859_1
= 0,
70 GEOIP_CHARSET_UTF8
= 1
73 typedef struct GeoIPRegionTag
{
80 GEOIP_MEMORY_CACHE
= 1,
81 GEOIP_CHECK_CACHE
= 2,
82 GEOIP_INDEX_CACHE
= 4,
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,
102 GEOIP_ANON_PROXY
= 1,
103 GEOIP_HTTP_X_FORWARDED_FOR_PROXY
= 2,
104 GEOIP_HTTP_CLIENT_IP_PROXY
= 3,
108 GEOIP_UNKNOWN_SPEED
= 0,
109 GEOIP_DIALUP_SPEED
= 1,
110 GEOIP_CABLEDSL_SPEED
= 2,
111 GEOIP_CORPORATE_SPEED
= 3,
112 } GeoIPNetspeedValues
;
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
;
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];
130 #define GEOIP_API __declspec(dllexport)
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
185 /* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
186 GEOIP_API
void GeoIPRegion_delete (GeoIPRegion
*gir
);
188 GEOIP_API
void GeoIP_assign_region_by_inetaddr(GeoIP
* gi
, unsigned long inetaddr
, GeoIPRegion
*gir
);
190 GEOIP_API
void GeoIP_assign_region_by_inetaddr_v6(GeoIP
* gi
, geoipv6_t inetaddr
, GeoIPRegion
*gir
);
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
);
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
);
201 /** return two letter country code */
202 GEOIP_API
const char* GeoIP_code_by_id(int id
);
204 /** return three letter country code */
205 GEOIP_API
const char* GeoIP_code3_by_id(int id
);
207 /** return full name of country */
208 GEOIP_API
const char* GeoIP_name_by_id(int id
);
210 /** return continent of country */
211 GEOIP_API
const char* GeoIP_continent_by_id(int id
);
213 /** return id by country code **/
214 GEOIP_API
int GeoIP_id_by_code(const char *country
);
216 /** return return number of known countries */
217 GEOIP_API
unsigned GeoIP_num_countries(void);
219 GEOIP_API
char *GeoIP_database_info (GeoIP
* gi
);
220 GEOIP_API
unsigned char GeoIP_database_edition (GeoIP
* gi
);
222 GEOIP_API
int GeoIP_charset (GeoIP
* gi
);
223 GEOIP_API
int GeoIP_set_charset (GeoIP
* gi
, int charset
);
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
);
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
);
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
);
236 #define memcpy(dest, src, n) bcopy(src, dest, n)