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 25extern "C" { 26#endif 27 28#include <sys/types.h> 29#if !defined(_WIN32) 30#include <sys/socket.h> 31#include <netinet/in.h> 32#include <arpa/inet.h> 33#else /* !defined(_WIN32) */ 34#include <winsock2.h> 35#include <ws2tcpip.h> 36#define snprintf _snprintf 37#define FILETIME_TO_USEC(ft) (((unsigned __int64) ft.dwHighDateTime << 32 | ft.dwLowDateTime) / 10) 38#endif /* !defined(_WIN32) */ 39 40#include<stdio.h> 41#include<stdlib.h> 42#include<string.h> 43#include <sys/types.h> /* for fstat */ 44#include <sys/stat.h> /* for fstat */ 45 46#define SEGMENT_RECORD_LENGTH 3 47#define LARGE_SEGMENT_RECORD_LENGTH 4 48#define STANDARD_RECORD_LENGTH 3 49#define ORG_RECORD_LENGTH 4 50#define MAX_RECORD_LENGTH 4 51#define NUM_DB_TYPES (38+1) 52 53/* 128 bit address in network order */ 54typedef struct in6_addr geoipv6_t; 55 56#define GEOIP_CHKBIT_V6(bit,ptr) (ptr[((127UL - bit) >> 3)] & (1UL << (~(127 - bit) & 7))) 57 58typedef struct GeoIPTag { 59 FILE *GeoIPDatabase; 60 char *file_path; 61 unsigned char *cache; 62 unsigned char *index_cache; 63 unsigned int *databaseSegments; 64 char databaseType; 65 time_t mtime; 66 int flags; 67 off_t size; 68 char record_length; 69 int charset; /* 0 iso-8859-1 1 utf8 */ 70 int record_iter; /* used in GeoIP_next_record */ 71 int netmask; /* netmask of last lookup - set using depth in _GeoIP_seek_record */ 72 time_t last_mtime_check; 73 off_t dyn_seg_size; /* currently only used by the cityconfidence database */ 74 unsigned int ext_flags; /* bit 0 teredo support enabled */ 75} GeoIP; 76 77typedef struct GeoIPLookup { 78 int netmask; 79} GeoIPLookup; 80 81typedef enum { 82 GEOIP_TEREDO_BIT = 0 83} GeoIPExtFlags; 84 85typedef enum { 86 GEOIP_CHARSET_ISO_8859_1 = 0, 87 GEOIP_CHARSET_UTF8 = 1 88} GeoIPCharset; 89 90typedef struct GeoIPRegionTag { 91 char country_code[3]; 92 char region[3]; 93} GeoIPRegion; 94 95typedef enum { 96 GEOIP_STANDARD = 0, 97 GEOIP_MEMORY_CACHE = 1, 98 GEOIP_CHECK_CACHE = 2, 99 GEOIP_INDEX_CACHE = 4, 100 GEOIP_MMAP_CACHE = 8, 101} GeoIPOptions; 102 103typedef enum { 104 GEOIP_COUNTRY_EDITION = 1, 105 GEOIP_REGION_EDITION_REV0 = 7, 106 GEOIP_CITY_EDITION_REV0 = 6, 107 GEOIP_ORG_EDITION = 5, 108 GEOIP_ISP_EDITION = 4, 109 GEOIP_CITY_EDITION_REV1 = 2, 110 GEOIP_REGION_EDITION_REV1 = 3, 111 GEOIP_PROXY_EDITION = 8, 112 GEOIP_ASNUM_EDITION = 9, 113 GEOIP_NETSPEED_EDITION = 10, 114 GEOIP_DOMAIN_EDITION = 11, 115 GEOIP_COUNTRY_EDITION_V6 = 12, 116 GEOIP_LOCATIONA_EDITION = 13, 117 GEOIP_ACCURACYRADIUS_EDITION = 14, 118 GEOIP_CITYCONFIDENCE_EDITION = 15, /* unsupported */ 119 GEOIP_CITYCONFIDENCEDIST_EDITION = 16, /* unsupported */ 120 GEOIP_LARGE_COUNTRY_EDITION = 17, 121 GEOIP_LARGE_COUNTRY_EDITION_V6 = 18, 122 GEOIP_CITYCONFIDENCEDIST_ISP_ORG_EDITION = 19, /* unsued, but gaps are not allowed */ 123 GEOIP_CCM_COUNTRY_EDITION =20, /* unsued, but gaps are not allowed */ 124 GEOIP_ASNUM_EDITION_V6 = 21, 125 GEOIP_ISP_EDITION_V6 = 22, 126 GEOIP_ORG_EDITION_V6 = 23, 127 GEOIP_DOMAIN_EDITION_V6 = 24, 128 GEOIP_LOCATIONA_EDITION_V6 = 25, 129 GEOIP_REGISTRAR_EDITION = 26, 130 GEOIP_REGISTRAR_EDITION_V6 = 27, 131 GEOIP_USERTYPE_EDITION = 28, 132 GEOIP_USERTYPE_EDITION_V6 = 29, 133 GEOIP_CITY_EDITION_REV1_V6 = 30, 134 GEOIP_CITY_EDITION_REV0_V6 = 31, 135 GEOIP_NETSPEED_EDITION_REV1 = 32, 136 GEOIP_NETSPEED_EDITION_REV1_V6 = 33, 137 GEOIP_COUNTRYCONF_EDITION = 34, 138 GEOIP_CITYCONF_EDITION = 35, 139 GEOIP_REGIONCONF_EDITION = 36, 140 GEOIP_POSTALCONF_EDITION = 37, 141 GEOIP_ACCURACYRADIUS_EDITION_V6 = 38 142} GeoIPDBTypes; 143 144typedef enum { 145 GEOIP_ANON_PROXY = 1, 146 GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2, 147 GEOIP_HTTP_CLIENT_IP_PROXY = 3, 148} GeoIPProxyTypes; 149 150typedef enum { 151 GEOIP_UNKNOWN_SPEED = 0, 152 GEOIP_DIALUP_SPEED = 1, 153 GEOIP_CABLEDSL_SPEED = 2, 154 GEOIP_CORPORATE_SPEED = 3, 155} GeoIPNetspeedValues; 156 157extern char **GeoIPDBFileName; 158extern const char * GeoIPDBDescription[NUM_DB_TYPES]; 159extern const char *GeoIPCountryDBFileName; 160extern const char *GeoIPRegionDBFileName; 161extern const char *GeoIPCityDBFileName; 162extern const char *GeoIPOrgDBFileName; 163extern const char *GeoIPISPDBFileName; 164extern const char *GeoIPLocationADBFileName; 165extern const char *GeoIPAccuracyRadiusFileName; 166extern const char *GeoIPCityConfidenceFileName; 167extern char * GeoIP_custom_directory; 168 169/* Warning: do not use those arrays as doing so may break your 170 * program with newer GeoIP versions */ 171extern const char GeoIP_country_code[255][3]; 172extern const char GeoIP_country_code3[255][4]; 173extern const char * GeoIP_country_name[255]; 174extern const char * GeoIP_utf8_country_name[255]; 175extern const char GeoIP_country_continent[255][3]; 176 177#ifdef DLL 178#define GEOIP_API __declspec(dllexport) 179#else 180#define GEOIP_API 181#endif /* DLL */ 182 183GEOIP_API void GeoIP_setup_custom_directory(char *dir); 184GEOIP_API GeoIP* GeoIP_open_type (int type, int flags); 185GEOIP_API GeoIP* GeoIP_new(int flags); 186GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags); 187GEOIP_API int GeoIP_db_avail(int type); 188GEOIP_API void GeoIP_delete(GeoIP* gi); 189 190GEOIP_API const char *GeoIP_country_code_by_addr_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 191GEOIP_API const char *GeoIP_country_code_by_name_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 192GEOIP_API const char *GeoIP_country_code3_by_addr_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 193GEOIP_API const char *GeoIP_country_code3_by_name_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 194GEOIP_API const char *GeoIP_country_name_by_addr_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 195GEOIP_API const char *GeoIP_country_name_by_name_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 196GEOIP_API const char *GeoIP_country_name_by_ipnum_gl (GeoIP* gi, unsigned long ipnum, GeoIPLookup * gl); 197GEOIP_API const char *GeoIP_country_code_by_ipnum_gl (GeoIP* gi, unsigned long ipnum, GeoIPLookup * gl); 198GEOIP_API const char *GeoIP_country_code3_by_ipnum_gl (GeoIP* gi, unsigned long ipnum, GeoIPLookup * gl); 199 200/* */ 201GEOIP_API const char *GeoIP_country_name_by_ipnum_v6_gl (GeoIP* gi, geoipv6_t ipnum, GeoIPLookup * gl); 202GEOIP_API const char *GeoIP_country_code_by_ipnum_v6_gl (GeoIP* gi, geoipv6_t ipnum, GeoIPLookup * gl); 203GEOIP_API const char *GeoIP_country_code3_by_ipnum_v6_gl (GeoIP* gi, geoipv6_t ipnum, GeoIPLookup * gl); 204 205GEOIP_API const char *GeoIP_country_code_by_addr_v6_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 206GEOIP_API const char *GeoIP_country_code_by_name_v6_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 207GEOIP_API const char *GeoIP_country_code3_by_addr_v6_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 208GEOIP_API const char *GeoIP_country_code3_by_name_v6_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 209GEOIP_API const char *GeoIP_country_name_by_addr_v6_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 210GEOIP_API const char *GeoIP_country_name_by_name_v6_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 211 212GEOIP_API int GeoIP_id_by_addr_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 213GEOIP_API int GeoIP_id_by_name_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 214GEOIP_API int GeoIP_id_by_ipnum_gl (GeoIP* gi, unsigned long ipnum, GeoIPLookup * gl); 215 216GEOIP_API int GeoIP_id_by_addr_v6_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 217GEOIP_API int GeoIP_id_by_name_v6_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 218GEOIP_API int GeoIP_id_by_ipnum_v6_gl (GeoIP* gi, geoipv6_t ipnum, GeoIPLookup * gl); 219 220GEOIP_API GeoIPRegion * GeoIP_region_by_addr_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 221GEOIP_API GeoIPRegion * GeoIP_region_by_name_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 222GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum_gl (GeoIP *gi, unsigned long ipnum, GeoIPLookup * gl); 223 224GEOIP_API GeoIPRegion * GeoIP_region_by_addr_v6_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 225GEOIP_API GeoIPRegion * GeoIP_region_by_name_v6_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 226GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum_v6_gl (GeoIP *gi, geoipv6_t ipnum, GeoIPLookup * gl); 227 228/* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */ 229GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir); 230 231GEOIP_API void GeoIP_assign_region_by_inetaddr_gl(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir, GeoIPLookup * gl); 232GEOIP_API void GeoIP_assign_region_by_inetaddr_v6_gl(GeoIP* gi, geoipv6_t inetaddr, GeoIPRegion *gir, GeoIPLookup * gl); 233 234/* Used to query GeoIP Organization, ISP and AS Number databases */ 235GEOIP_API char *GeoIP_name_by_ipnum_gl (GeoIP* gi, unsigned long ipnum, GeoIPLookup * gl); 236GEOIP_API char *GeoIP_name_by_addr_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 237GEOIP_API char *GeoIP_name_by_name_gl (GeoIP* gi, const char *host, GeoIPLookup * gl); 238 239GEOIP_API char *GeoIP_name_by_ipnum_v6_gl (GeoIP* gi, geoipv6_t ipnum, GeoIPLookup * gl); 240GEOIP_API char *GeoIP_name_by_addr_v6_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 241GEOIP_API char *GeoIP_name_by_name_v6_gl (GeoIP* gi, const char *name, GeoIPLookup * gl); 242 243/** return two letter country code */ 244GEOIP_API const char* GeoIP_code_by_id(int id); 245 246/** return three letter country code */ 247GEOIP_API const char* GeoIP_code3_by_id(int id); 248 249/** return full name of country in utf8 or iso-8859-1 */ 250GEOIP_API const char* GeoIP_country_name_by_id(GeoIP* gi, int id); 251 252/** return full name of country */ 253GEOIP_API const char* GeoIP_name_by_id(int id); 254 255/** return continent of country */ 256GEOIP_API const char* GeoIP_continent_by_id(int id); 257 258/** return id by country code **/ 259GEOIP_API int GeoIP_id_by_code(const char *country); 260 261/** return return number of known countries */ 262GEOIP_API unsigned GeoIP_num_countries(void); 263 264GEOIP_API char *GeoIP_database_info (GeoIP* gi); 265GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi); 266 267GEOIP_API int GeoIP_charset (GeoIP* gi); 268GEOIP_API int GeoIP_set_charset (GeoIP* gi, int charset); 269GEOIP_API int GeoIP_enable_teredo (GeoIP* gi, int true_false ); 270GEOIP_API int GeoIP_teredo (GeoIP* gi ); 271 272GEOIP_API char **GeoIP_range_by_ip_gl (GeoIP* gi, const char *addr, GeoIPLookup * gl); 273GEOIP_API void GeoIP_range_by_ip_delete(char **ptr); 274 275/* Convert region code to region name */ 276GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code); 277 278/* Get timezone from country and region code */ 279GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code); 280 281/* some v4 helper functions as of 1.4.7 exported to the public API */ 282GEOIP_API unsigned long GeoIP_addr_to_num(const char *addr); 283GEOIP_API char * GeoIP_num_to_addr(unsigned long ipnum); 284 285/* Internal function -- convert iso to utf8; return a malloced utf8 string. */ 286char * _GeoIP_iso_8859_1__utf8(const char * iso); 287 288/* Cleans up memory used to hold file name paths. Returns 1 if successful; otherwise 0. 289 * */ 290GEOIP_API int GeoIP_cleanup(void); 291 292/* Returns the library version in use. Helpful if your loading dynamically. */ 293GEOIP_API const char * GeoIP_lib_version(void); 294 295 296/* depricated */ 297GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr); 298GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host); 299GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr); 300GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host); 301GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr); 302GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host); 303GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum); 304GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum); 305GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum); 306 307GEOIP_API const char *GeoIP_country_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum); 308GEOIP_API const char *GeoIP_country_code_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum); 309GEOIP_API const char *GeoIP_country_code3_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum); 310 311GEOIP_API const char *GeoIP_country_code_by_addr_v6 (GeoIP* gi, const char *addr); 312GEOIP_API const char *GeoIP_country_code_by_name_v6 (GeoIP* gi, const char *host); 313GEOIP_API const char *GeoIP_country_code3_by_addr_v6 (GeoIP* gi, const char *addr); 314GEOIP_API const char *GeoIP_country_code3_by_name_v6 (GeoIP* gi, const char *host); 315GEOIP_API const char *GeoIP_country_name_by_addr_v6 (GeoIP* gi, const char *addr); 316GEOIP_API const char *GeoIP_country_name_by_name_v6 (GeoIP* gi, const char *host); 317 318GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr); 319GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host); 320GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum); 321 322GEOIP_API int GeoIP_id_by_addr_v6 (GeoIP* gi, const char *addr); 323GEOIP_API int GeoIP_id_by_name_v6 (GeoIP* gi, const char *host); 324GEOIP_API int GeoIP_id_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum); 325 326GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr); 327GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host); 328GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum); 329 330GEOIP_API GeoIPRegion * GeoIP_region_by_addr_v6 (GeoIP* gi, const char *addr); 331GEOIP_API GeoIPRegion * GeoIP_region_by_name_v6 (GeoIP* gi, const char *host); 332GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum_v6 (GeoIP *gi, geoipv6_t ipnum); 333 334GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir); 335GEOIP_API void GeoIP_assign_region_by_inetaddr_v6(GeoIP* gi, geoipv6_t inetaddr, GeoIPRegion *gir); 336 337GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum); 338GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr); 339GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host); 340 341GEOIP_API char *GeoIP_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum); 342GEOIP_API char *GeoIP_name_by_addr_v6 (GeoIP* gi, const char *addr); 343GEOIP_API char *GeoIP_name_by_name_v6 (GeoIP* gi, const char *name); 344 345 346/** GeoIP_last_netmask is deprecated - it is not thread safe */ 347GEOIP_API int GeoIP_last_netmask (GeoIP* gi); 348GEOIP_API char **GeoIP_range_by_ip (GeoIP* gi, const char *addr); 349 350/* Deprecated - for backwards compatibility only */ 351GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr); 352GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host); 353GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr); 354GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host); 355GEOIP_API char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum); 356 357GEOIP_API int GeoIP_country_id_by_addr_v6 (GeoIP* gi, const char *addr); 358GEOIP_API char *GeoIP_org_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum); 359GEOIP_API char *GeoIP_org_by_addr_v6 (GeoIP* gi, const char *addr); 360GEOIP_API char *GeoIP_org_by_name_v6 (GeoIP* gi, const char *name); 361 362/* End deprecated */ 363 364 365 366# 367#ifdef __cplusplus 368} 369#endif 370 371#endif /* GEOIP_H */ 372