fastgeoip 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +25 -0
- data/LICENSE +20 -0
- data/README.rdoc +11 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/ext/GeoIP.c +1640 -0
- data/ext/GeoIP.h +252 -0
- data/ext/GeoIPCity.c +297 -0
- data/ext/GeoIPCity.h +72 -0
- data/ext/GeoIP_internal.h +19 -0
- data/ext/extconf.h +5 -0
- data/ext/extconf.rb +58 -0
- data/ext/fastgeoip.c +59 -0
- data/ext/global.h +32 -0
- data/ext/md5.c +326 -0
- data/ext/md5.h +40 -0
- data/ext/mkmf.log +24 -0
- data/ext/regionName.c +13518 -0
- data/ext/ruby_geoip.h +15 -0
- data/ext/timeZone.c +941 -0
- data/ext/types.h +140 -0
- data/fastgeoip.gemspec +66 -0
- data/lib/fastgeoip.rb +0 -0
- data/test/helper.rb +10 -0
- metadata +92 -0
data/ext/GeoIP.h
ADDED
@@ -0,0 +1,252 @@
|
|
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
|
+
|
30
|
+
#if !defined(_WIN32)
|
31
|
+
#include <sys/socket.h>
|
32
|
+
#include <netinet/in.h>
|
33
|
+
#include <arpa/inet.h>
|
34
|
+
#else /* !defined(_WIN32) */
|
35
|
+
#include <winsock2.h>
|
36
|
+
#include <ws2tcpip.h>
|
37
|
+
#include <windows.h>
|
38
|
+
#define snprintf _snprintf
|
39
|
+
#define FILETIME_TO_USEC(ft) (((unsigned __int64) ft.dwHighDateTime << 32 | ft.dwLowDateTime) / 10)
|
40
|
+
#endif /* !defined(_WIN32) */
|
41
|
+
|
42
|
+
#include<stdio.h>
|
43
|
+
#include<stdlib.h>
|
44
|
+
#include<string.h>
|
45
|
+
#include <sys/types.h> /* for fstat */
|
46
|
+
#include <sys/stat.h> /* for fstat */
|
47
|
+
|
48
|
+
#define SEGMENT_RECORD_LENGTH 3
|
49
|
+
#define STANDARD_RECORD_LENGTH 3
|
50
|
+
#define ORG_RECORD_LENGTH 4
|
51
|
+
#define MAX_RECORD_LENGTH 4
|
52
|
+
#define NUM_DB_TYPES 20
|
53
|
+
|
54
|
+
/* 128 bit address in network order */
|
55
|
+
typedef struct in6_addr geoipv6_t;
|
56
|
+
|
57
|
+
#define GEOIP_CHKBIT_V6(bit,ptr) (ptr[((127UL - bit) >> 3)] & (1UL << (~(127 - bit) & 7)))
|
58
|
+
|
59
|
+
typedef struct GeoIPTag {
|
60
|
+
FILE *GeoIPDatabase;
|
61
|
+
char *file_path;
|
62
|
+
unsigned char *cache;
|
63
|
+
unsigned char *index_cache;
|
64
|
+
unsigned int *databaseSegments;
|
65
|
+
char databaseType;
|
66
|
+
time_t mtime;
|
67
|
+
int flags;
|
68
|
+
off_t size;
|
69
|
+
char record_length;
|
70
|
+
int charset; /* 0 iso-8859-1 1 utf8 */
|
71
|
+
int record_iter; /* used in GeoIP_next_record */
|
72
|
+
int netmask; /* netmask of last lookup - set using depth in _GeoIP_seek_record */
|
73
|
+
time_t last_mtime_check;
|
74
|
+
} GeoIP;
|
75
|
+
|
76
|
+
|
77
|
+
typedef enum {
|
78
|
+
GEOIP_CHARSET_ISO_8859_1 = 0,
|
79
|
+
GEOIP_CHARSET_UTF8 = 1
|
80
|
+
} GeoIPCharset;
|
81
|
+
|
82
|
+
typedef struct GeoIPRegionTag {
|
83
|
+
char country_code[3];
|
84
|
+
char region[3];
|
85
|
+
} GeoIPRegion;
|
86
|
+
|
87
|
+
typedef enum {
|
88
|
+
GEOIP_STANDARD = 0,
|
89
|
+
GEOIP_MEMORY_CACHE = 1,
|
90
|
+
GEOIP_CHECK_CACHE = 2,
|
91
|
+
GEOIP_INDEX_CACHE = 4,
|
92
|
+
GEOIP_MMAP_CACHE = 8,
|
93
|
+
} GeoIPOptions;
|
94
|
+
|
95
|
+
typedef enum {
|
96
|
+
GEOIP_COUNTRY_EDITION = 1,
|
97
|
+
GEOIP_REGION_EDITION_REV0 = 7,
|
98
|
+
GEOIP_CITY_EDITION_REV0 = 6,
|
99
|
+
GEOIP_ORG_EDITION = 5,
|
100
|
+
GEOIP_ISP_EDITION = 4,
|
101
|
+
GEOIP_CITY_EDITION_REV1 = 2,
|
102
|
+
GEOIP_REGION_EDITION_REV1 = 3,
|
103
|
+
GEOIP_PROXY_EDITION = 8,
|
104
|
+
GEOIP_ASNUM_EDITION = 9,
|
105
|
+
GEOIP_NETSPEED_EDITION = 10,
|
106
|
+
GEOIP_DOMAIN_EDITION = 11,
|
107
|
+
GEOIP_COUNTRY_EDITION_V6 = 12,
|
108
|
+
} GeoIPDBTypes;
|
109
|
+
|
110
|
+
typedef enum {
|
111
|
+
GEOIP_ANON_PROXY = 1,
|
112
|
+
GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2,
|
113
|
+
GEOIP_HTTP_CLIENT_IP_PROXY = 3,
|
114
|
+
} GeoIPProxyTypes;
|
115
|
+
|
116
|
+
typedef enum {
|
117
|
+
GEOIP_UNKNOWN_SPEED = 0,
|
118
|
+
GEOIP_DIALUP_SPEED = 1,
|
119
|
+
GEOIP_CABLEDSL_SPEED = 2,
|
120
|
+
GEOIP_CORPORATE_SPEED = 3,
|
121
|
+
} GeoIPNetspeedValues;
|
122
|
+
|
123
|
+
extern char **GeoIPDBFileName;
|
124
|
+
extern const char * GeoIPDBDescription[NUM_DB_TYPES];
|
125
|
+
extern const char *GeoIPCountryDBFileName;
|
126
|
+
extern const char *GeoIPRegionDBFileName;
|
127
|
+
extern const char *GeoIPCityDBFileName;
|
128
|
+
extern const char *GeoIPOrgDBFileName;
|
129
|
+
extern const char *GeoIPISPDBFileName;
|
130
|
+
|
131
|
+
/* Warning: do not use those arrays as doing so may break your
|
132
|
+
* program with newer GeoIP versions */
|
133
|
+
extern const char GeoIP_country_code[253][3];
|
134
|
+
extern const char GeoIP_country_code3[253][4];
|
135
|
+
extern const char * GeoIP_country_name[253];
|
136
|
+
extern const char GeoIP_country_continent[253][3];
|
137
|
+
|
138
|
+
#ifdef DLL
|
139
|
+
#define GEOIP_API __declspec(dllexport)
|
140
|
+
#else
|
141
|
+
#define GEOIP_API
|
142
|
+
#endif /* DLL */
|
143
|
+
|
144
|
+
GEOIP_API void GeoIP_setup_custom_directory(char *dir);
|
145
|
+
GEOIP_API GeoIP* GeoIP_open_type (int type, int flags);
|
146
|
+
GEOIP_API GeoIP* GeoIP_new(int flags);
|
147
|
+
GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags);
|
148
|
+
GEOIP_API int GeoIP_db_avail(int type);
|
149
|
+
GEOIP_API void GeoIP_delete(GeoIP* gi);
|
150
|
+
GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr);
|
151
|
+
GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host);
|
152
|
+
GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr);
|
153
|
+
GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host);
|
154
|
+
GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr);
|
155
|
+
GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host);
|
156
|
+
GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
|
157
|
+
GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum);
|
158
|
+
GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum);
|
159
|
+
|
160
|
+
/* */
|
161
|
+
GEOIP_API const char *GeoIP_country_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
|
162
|
+
GEOIP_API const char *GeoIP_country_code_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
|
163
|
+
GEOIP_API const char *GeoIP_country_code3_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
|
164
|
+
|
165
|
+
/* Deprecated - for backwards compatibility only */
|
166
|
+
GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr);
|
167
|
+
GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host);
|
168
|
+
GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr);
|
169
|
+
GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host);
|
170
|
+
GEOIP_API char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum);
|
171
|
+
|
172
|
+
GEOIP_API char *GeoIP_org_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
|
173
|
+
GEOIP_API char *GeoIP_org_by_addr_v6 (GeoIP* gi, const char *addr);
|
174
|
+
GEOIP_API char *GeoIP_org_by_name_v6 (GeoIP* gi, const char *name);
|
175
|
+
|
176
|
+
/* End deprecated */
|
177
|
+
|
178
|
+
GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr);
|
179
|
+
GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host);
|
180
|
+
GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum);
|
181
|
+
|
182
|
+
GEOIP_API int GeoIP_id_by_addr_v6 (GeoIP* gi, const char *addr);
|
183
|
+
GEOIP_API int GeoIP_id_by_name_v6 (GeoIP* gi, const char *host);
|
184
|
+
GEOIP_API int GeoIP_id_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
|
185
|
+
|
186
|
+
GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr);
|
187
|
+
GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host);
|
188
|
+
GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum);
|
189
|
+
|
190
|
+
GEOIP_API GeoIPRegion * GeoIP_region_by_addr_v6 (GeoIP* gi, const char *addr);
|
191
|
+
GEOIP_API GeoIPRegion * GeoIP_region_by_name_v6 (GeoIP* gi, const char *host);
|
192
|
+
GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum_v6 (GeoIP *gi, geoipv6_t ipnum);
|
193
|
+
|
194
|
+
/* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
|
195
|
+
GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
|
196
|
+
|
197
|
+
GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir);
|
198
|
+
|
199
|
+
GEOIP_API void GeoIP_assign_region_by_inetaddr_v6(GeoIP* gi, geoipv6_t inetaddr, GeoIPRegion *gir);
|
200
|
+
|
201
|
+
/* Used to query GeoIP Organization, ISP and AS Number databases */
|
202
|
+
GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
|
203
|
+
GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr);
|
204
|
+
GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host);
|
205
|
+
|
206
|
+
GEOIP_API char *GeoIP_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
|
207
|
+
GEOIP_API char *GeoIP_name_by_addr_v6 (GeoIP* gi, const char *addr);
|
208
|
+
GEOIP_API char *GeoIP_name_by_name_v6 (GeoIP* gi, const char *name);
|
209
|
+
|
210
|
+
/** return two letter country code */
|
211
|
+
GEOIP_API const char* GeoIP_code_by_id(int id);
|
212
|
+
|
213
|
+
/** return three letter country code */
|
214
|
+
GEOIP_API const char* GeoIP_code3_by_id(int id);
|
215
|
+
|
216
|
+
/** return full name of country */
|
217
|
+
GEOIP_API const char* GeoIP_name_by_id(int id);
|
218
|
+
|
219
|
+
/** return continent of country */
|
220
|
+
GEOIP_API const char* GeoIP_continent_by_id(int id);
|
221
|
+
|
222
|
+
/** return id by country code **/
|
223
|
+
GEOIP_API int GeoIP_id_by_code(const char *country);
|
224
|
+
|
225
|
+
/** return return number of known countries */
|
226
|
+
GEOIP_API unsigned GeoIP_num_countries(void);
|
227
|
+
|
228
|
+
GEOIP_API char *GeoIP_database_info (GeoIP* gi);
|
229
|
+
GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi);
|
230
|
+
|
231
|
+
GEOIP_API int GeoIP_charset (GeoIP* gi);
|
232
|
+
GEOIP_API int GeoIP_set_charset (GeoIP* gi, int charset);
|
233
|
+
|
234
|
+
GEOIP_API int GeoIP_last_netmask (GeoIP* gi);
|
235
|
+
GEOIP_API char **GeoIP_range_by_ip (GeoIP* gi, const char *addr);
|
236
|
+
GEOIP_API void GeoIP_range_by_ip_delete(char **ptr);
|
237
|
+
|
238
|
+
/* Convert region code to region name */
|
239
|
+
GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code);
|
240
|
+
|
241
|
+
/* Get timezone from country and region code */
|
242
|
+
GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code);
|
243
|
+
|
244
|
+
#ifdef BSD
|
245
|
+
#define memcpy(dest, src, n) bcopy(src, dest, n)
|
246
|
+
#endif
|
247
|
+
|
248
|
+
#ifdef __cplusplus
|
249
|
+
}
|
250
|
+
#endif
|
251
|
+
|
252
|
+
#endif /* GEOIP_H */
|
data/ext/GeoIPCity.c
ADDED
@@ -0,0 +1,297 @@
|
|
1
|
+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
|
2
|
+
/* GeoIPCity.c
|
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
|
+
#include <GeoIP.h>
|
22
|
+
#include <GeoIP_internal.h>
|
23
|
+
#include <GeoIPCity.h>
|
24
|
+
#if !defined(_WIN32)
|
25
|
+
#include <netdb.h>
|
26
|
+
#include <netinet/in.h> /* For ntohl */
|
27
|
+
#else
|
28
|
+
#include <windows.h>
|
29
|
+
#include <winsock.h>
|
30
|
+
#endif
|
31
|
+
#include <sys/types.h> /* For uint32_t */
|
32
|
+
#ifdef HAVE_STDINT_H
|
33
|
+
#include <stdint.h> /* For uint32_t */
|
34
|
+
#endif
|
35
|
+
|
36
|
+
static
|
37
|
+
const int FULL_RECORD_LENGTH = 50;
|
38
|
+
|
39
|
+
static
|
40
|
+
GeoIPRecord * _extract_record(GeoIP* gi, unsigned int seek_record, int *next_record_ptr) {
|
41
|
+
int record_pointer;
|
42
|
+
unsigned char *record_buf = NULL;
|
43
|
+
unsigned char *begin_record_buf = NULL;
|
44
|
+
GeoIPRecord * record;
|
45
|
+
int str_length = 0;
|
46
|
+
int j;
|
47
|
+
double latitude = 0, longitude = 0;
|
48
|
+
int metroarea_combo = 0;
|
49
|
+
int bytes_read = 0;
|
50
|
+
if (seek_record == gi->databaseSegments[0])
|
51
|
+
return NULL;
|
52
|
+
|
53
|
+
record = malloc(sizeof(GeoIPRecord));
|
54
|
+
memset(record, 0, sizeof(GeoIPRecord));
|
55
|
+
|
56
|
+
record->charset = gi->charset;
|
57
|
+
|
58
|
+
record_pointer = seek_record + (2 * gi->record_length - 1) * gi->databaseSegments[0];
|
59
|
+
|
60
|
+
if (gi->cache == NULL) {
|
61
|
+
fseek(gi->GeoIPDatabase, record_pointer, SEEK_SET);
|
62
|
+
begin_record_buf = record_buf = malloc(sizeof(char) * FULL_RECORD_LENGTH);
|
63
|
+
bytes_read = fread(record_buf, sizeof(char), FULL_RECORD_LENGTH, gi->GeoIPDatabase);
|
64
|
+
if (bytes_read == 0) {
|
65
|
+
/* eof or other error */
|
66
|
+
free(begin_record_buf);
|
67
|
+
free(record);
|
68
|
+
return NULL;
|
69
|
+
}
|
70
|
+
} else {
|
71
|
+
record_buf = gi->cache + (long)record_pointer;
|
72
|
+
}
|
73
|
+
|
74
|
+
/* get country */
|
75
|
+
record->continent_code = (char *) GeoIP_country_continent[record_buf[0]];
|
76
|
+
record->country_code = (char *) GeoIP_country_code [record_buf[0]];
|
77
|
+
record->country_code3 = (char *) GeoIP_country_code3[record_buf[0]];
|
78
|
+
record->country_name = (char *) GeoIP_country_name [record_buf[0]];
|
79
|
+
record_buf++;
|
80
|
+
|
81
|
+
/* get region */
|
82
|
+
while (record_buf[str_length] != '\0')
|
83
|
+
str_length++;
|
84
|
+
if (str_length > 0) {
|
85
|
+
record->region = malloc(str_length+1);
|
86
|
+
strncpy(record->region, (char *)record_buf, str_length+1);
|
87
|
+
}
|
88
|
+
record_buf += str_length + 1;
|
89
|
+
str_length = 0;
|
90
|
+
|
91
|
+
/* get city */
|
92
|
+
while (record_buf[str_length] != '\0')
|
93
|
+
str_length++;
|
94
|
+
if (str_length > 0) {
|
95
|
+
if ( gi->charset == GEOIP_CHARSET_UTF8 ) {
|
96
|
+
record->city = _iso_8859_1__utf8( (const char * ) record_buf );
|
97
|
+
} else {
|
98
|
+
record->city = malloc(str_length+1);
|
99
|
+
strncpy(record->city, ( const char * ) record_buf, str_length+1);
|
100
|
+
}
|
101
|
+
}
|
102
|
+
record_buf += (str_length + 1);
|
103
|
+
str_length = 0;
|
104
|
+
|
105
|
+
/* get postal code */
|
106
|
+
while (record_buf[str_length] != '\0')
|
107
|
+
str_length++;
|
108
|
+
if (str_length > 0) {
|
109
|
+
record->postal_code = malloc(str_length+1);
|
110
|
+
strncpy(record->postal_code, (char *)record_buf, str_length+1);
|
111
|
+
}
|
112
|
+
record_buf += (str_length + 1);
|
113
|
+
|
114
|
+
/* get latitude */
|
115
|
+
for (j = 0; j < 3; ++j)
|
116
|
+
latitude += (record_buf[j] << (j * 8));
|
117
|
+
record->latitude = latitude/10000 - 180;
|
118
|
+
record_buf += 3;
|
119
|
+
|
120
|
+
/* get longitude */
|
121
|
+
for (j = 0; j < 3; ++j)
|
122
|
+
longitude += (record_buf[j] << (j * 8));
|
123
|
+
record->longitude = longitude/10000 - 180;
|
124
|
+
|
125
|
+
/* get area code and metro code for post April 2002 databases and for US locations */
|
126
|
+
if (GEOIP_CITY_EDITION_REV1 == gi->databaseType) {
|
127
|
+
if (!strcmp(record->country_code, "US")) {
|
128
|
+
record_buf += 3;
|
129
|
+
for (j = 0; j < 3; ++j)
|
130
|
+
metroarea_combo += (record_buf[j] << (j * 8));
|
131
|
+
record->metro_code = metroarea_combo/1000;
|
132
|
+
record->area_code = metroarea_combo % 1000;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
if (gi->cache == NULL)
|
137
|
+
free(begin_record_buf);
|
138
|
+
|
139
|
+
/* Used for GeoIP_next_record */
|
140
|
+
if (next_record_ptr != NULL)
|
141
|
+
*next_record_ptr = seek_record + record_buf - begin_record_buf + 3;
|
142
|
+
|
143
|
+
return record;
|
144
|
+
}
|
145
|
+
|
146
|
+
static
|
147
|
+
GeoIPRecord * _get_record(GeoIP* gi, unsigned long ipnum) {
|
148
|
+
unsigned int seek_record;
|
149
|
+
|
150
|
+
if (gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
|
151
|
+
gi->databaseType != GEOIP_CITY_EDITION_REV1) {
|
152
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
|
153
|
+
return 0;
|
154
|
+
}
|
155
|
+
|
156
|
+
seek_record = _GeoIP_seek_record(gi, ipnum);
|
157
|
+
return _extract_record(gi, seek_record, NULL);
|
158
|
+
}
|
159
|
+
|
160
|
+
static
|
161
|
+
GeoIPRecord * _get_record_v6(GeoIP* gi, geoipv6_t ipnum) {
|
162
|
+
unsigned int seek_record;
|
163
|
+
|
164
|
+
if (gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
|
165
|
+
gi->databaseType != GEOIP_CITY_EDITION_REV1) {
|
166
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
|
167
|
+
return 0;
|
168
|
+
}
|
169
|
+
|
170
|
+
seek_record = _GeoIP_seek_record_v6(gi, ipnum);
|
171
|
+
return _extract_record(gi, seek_record, NULL);
|
172
|
+
}
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
GeoIPRecord * GeoIP_record_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
177
|
+
return _get_record(gi, ipnum);
|
178
|
+
}
|
179
|
+
|
180
|
+
GeoIPRecord * GeoIP_record_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
181
|
+
return _get_record_v6(gi, ipnum);
|
182
|
+
}
|
183
|
+
|
184
|
+
GeoIPRecord * GeoIP_record_by_addr (GeoIP* gi, const char *addr) {
|
185
|
+
unsigned long ipnum;
|
186
|
+
if (addr == NULL) {
|
187
|
+
return 0;
|
188
|
+
}
|
189
|
+
ipnum = _GeoIP_addr_to_num(addr);
|
190
|
+
return _get_record(gi, ipnum);
|
191
|
+
}
|
192
|
+
|
193
|
+
GeoIPRecord * GeoIP_record_by_addr_v6 (GeoIP* gi, const char *addr) {
|
194
|
+
geoipv6_t ipnum;
|
195
|
+
if (addr == NULL) {
|
196
|
+
return 0;
|
197
|
+
}
|
198
|
+
ipnum = _GeoIP_addr_to_num_v6(addr);
|
199
|
+
return _get_record_v6(gi, ipnum);
|
200
|
+
}
|
201
|
+
|
202
|
+
GeoIPRecord * GeoIP_record_by_name (GeoIP* gi, const char *name) {
|
203
|
+
unsigned long ipnum;
|
204
|
+
if (name == NULL) {
|
205
|
+
return 0;
|
206
|
+
}
|
207
|
+
ipnum = _GeoIP_lookupaddress(name);
|
208
|
+
return _get_record(gi, ipnum);
|
209
|
+
}
|
210
|
+
|
211
|
+
GeoIPRecord * GeoIP_record_by_name_v6 (GeoIP* gi, const char *name) {
|
212
|
+
geoipv6_t ipnum;
|
213
|
+
if (name == NULL) {
|
214
|
+
return 0;
|
215
|
+
}
|
216
|
+
ipnum = _GeoIP_lookupaddress_v6(name);
|
217
|
+
return _get_record_v6(gi, ipnum);
|
218
|
+
}
|
219
|
+
|
220
|
+
int GeoIP_record_id_by_addr (GeoIP* gi, const char *addr) {
|
221
|
+
unsigned long ipnum;
|
222
|
+
if (gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
|
223
|
+
gi->databaseType != GEOIP_CITY_EDITION_REV1) {
|
224
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
|
225
|
+
return 0;
|
226
|
+
}
|
227
|
+
if (addr == NULL) {
|
228
|
+
return 0;
|
229
|
+
}
|
230
|
+
ipnum = _GeoIP_addr_to_num(addr);
|
231
|
+
return _GeoIP_seek_record(gi, ipnum);
|
232
|
+
}
|
233
|
+
|
234
|
+
int GeoIP_record_id_by_addr_v6 (GeoIP* gi, const char *addr) {
|
235
|
+
geoipv6_t ipnum;
|
236
|
+
if (gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
|
237
|
+
gi->databaseType != GEOIP_CITY_EDITION_REV1) {
|
238
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]);
|
239
|
+
return 0;
|
240
|
+
}
|
241
|
+
if (addr == NULL) {
|
242
|
+
return 0;
|
243
|
+
}
|
244
|
+
ipnum = _GeoIP_addr_to_num_v6(addr);
|
245
|
+
return _GeoIP_seek_record_v6(gi, ipnum);
|
246
|
+
}
|
247
|
+
|
248
|
+
int GeoIP_init_record_iter (GeoIP* gi) {
|
249
|
+
return gi->databaseSegments[0] + 1;
|
250
|
+
}
|
251
|
+
|
252
|
+
int GeoIP_next_record (GeoIP* gi, GeoIPRecord **gir, int *record_iter) {
|
253
|
+
if (gi->cache != NULL) {
|
254
|
+
printf("GeoIP_next_record not supported in memory cache mode\n");
|
255
|
+
return 1;
|
256
|
+
}
|
257
|
+
*gir = _extract_record(gi, *record_iter, record_iter);
|
258
|
+
return 0;
|
259
|
+
}
|
260
|
+
|
261
|
+
void GeoIPRecord_delete (GeoIPRecord *gir) {
|
262
|
+
free(gir->region);
|
263
|
+
free(gir->city);
|
264
|
+
free(gir->postal_code);
|
265
|
+
free(gir);
|
266
|
+
}
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
char * _iso_8859_1__utf8(const char * iso) {
|
271
|
+
signed char c;
|
272
|
+
char k;
|
273
|
+
char * p;
|
274
|
+
char * t = (char *)iso;
|
275
|
+
int len = 0;
|
276
|
+
while ( ( c = *t++) ){
|
277
|
+
if ( c < 0 )
|
278
|
+
len++;
|
279
|
+
}
|
280
|
+
len += t - iso;
|
281
|
+
t = p = malloc( len );
|
282
|
+
|
283
|
+
if ( p ){
|
284
|
+
while ( ( c = *iso++ ) ) {
|
285
|
+
if (c < 0 ) {
|
286
|
+
k = 0xc2;
|
287
|
+
if (c >= -64 )
|
288
|
+
k++;
|
289
|
+
*t++ = k;
|
290
|
+
c &= ~0x40;
|
291
|
+
}
|
292
|
+
*t++ = c;
|
293
|
+
}
|
294
|
+
*t++ = 0x00;
|
295
|
+
}
|
296
|
+
return p;
|
297
|
+
}
|