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
31 #include <sys/types.h> /* for fstat */
32 #include <sys/stat.h> /* for fstat */
34 #define SEGMENT_RECORD_LENGTH 3
35 #define STANDARD_RECORD_LENGTH 3
36 #define ORG_RECORD_LENGTH 4
37 #define MAX_RECORD_LENGTH 4
38 #define NUM_DB_TYPES 20
40 typedef struct GeoIPTag {
44 unsigned char *index_cache;
45 unsigned int *databaseSegments;
50 int record_iter; /* used in GeoIP_next_record */
53 typedef struct GeoIPRegionTag {
60 GEOIP_MEMORY_CACHE = 1,
61 GEOIP_CHECK_CACHE = 2,
62 GEOIP_INDEX_CACHE = 4,
66 GEOIP_COUNTRY_EDITION = 1,
67 GEOIP_REGION_EDITION_REV0 = 7,
68 GEOIP_CITY_EDITION_REV0 = 6,
69 GEOIP_ORG_EDITION = 5,
70 GEOIP_ISP_EDITION = 4,
71 GEOIP_CITY_EDITION_REV1 = 2,
72 GEOIP_REGION_EDITION_REV1 = 3,
73 GEOIP_PROXY_EDITION = 8,
74 GEOIP_ASNUM_EDITION = 9,
75 GEOIP_NETSPEED_EDITION = 10,
76 GEOIP_DOMAIN_EDITION = 11
81 GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2,
82 GEOIP_HTTP_CLIENT_IP_PROXY = 3,
86 GEOIP_UNKNOWN_SPEED = 0,
87 GEOIP_DIALUP_SPEED = 1,
88 GEOIP_CABLEDSL_SPEED = 2,
89 GEOIP_CORPORATE_SPEED = 3,
90 } GeoIPNetspeedValues;
92 extern char **GeoIPDBFileName;
93 extern const char * GeoIPDBDescription[NUM_DB_TYPES];
94 extern const char *GeoIPCountryDBFileName;
95 extern const char *GeoIPRegionDBFileName;
96 extern const char *GeoIPCityDBFileName;
97 extern const char *GeoIPOrgDBFileName;
98 extern const char *GeoIPISPDBFileName;
100 extern const char GeoIP_country_code[251][3];
101 extern const char GeoIP_country_code3[251][4];
102 extern const char * GeoIP_country_name[251];
103 extern const char GeoIP_country_continent[251][3];
106 #define GEOIP_API __declspec(dllexport)
111 GEOIP_API void GeoIP_setup_custom_directory(char *dir);
112 GEOIP_API GeoIP* GeoIP_open_type (int type, int flags);
113 GEOIP_API GeoIP* GeoIP_new(int flags);
114 GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags);
115 GEOIP_API int GeoIP_db_avail(int type);
116 GEOIP_API void GeoIP_delete(GeoIP* gi);
117 GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr);
118 GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host);
119 GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr);
120 GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host);
121 GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr);
122 GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host);
123 GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
124 GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum);
125 GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum);
127 /* Deprecated - for backwards compatibility only */
128 GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr);
129 GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host);
130 GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr);
131 GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host);
134 GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr);
135 GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host);
136 GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum);
138 GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr);
139 GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host);
140 GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum);
142 /* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
143 GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
145 GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir);
147 /* Used to query GeoIP Organization, ISP and AS Number databases */
148 GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
149 GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr);
150 GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host);
152 GEOIP_API char *GeoIP_database_info (GeoIP* gi);
153 GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi);
155 /* Convert region code to region name */
156 GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code);
158 /* Get timezone from country and region code */
159 GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code);
162 #define memcpy(dest, src, n) bcopy(src, dest, n)