-
Notifications
You must be signed in to change notification settings - Fork 15
/
ip2city.c
executable file
·363 lines (319 loc) · 9.11 KB
/
ip2city.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ip2city.h"
#include "ext/standard/php_standard.h"
#define BYTE3INT(X) ( ( X[0] & 0x000000FF ) \
| ( ( X[1] & 0x000000FF ) << 8 ) \
| ( ( X[2] & 0x000000FF ) << 16 ) )
#define BYTE4INT(X) ( ( X[0] & 0x000000FF ) \
| ( ( X[1] & 0x000000FF ) << 8 ) \
| ( ( X[2] & 0x000000FF ) << 16 ) \
| ( ( X[3] & 0x000000FF ) << 24 ) )
#define BYTE1INT(X) ( ( X[0] & 0x000000FF ) )
typedef struct IndexIp {
unsigned char ip[4];
unsigned char local[3];
} IpStruct;
#define ipsize (int)sizeof(IpStruct)
#ifdef HAVE_INET_NTOP
ZEND_BEGIN_ARG_INFO(arginfo_inet_ntop, 0)
ZEND_ARG_INFO(0, in_addr)
ZEND_END_ARG_INFO()
#endif
/* If you declare any globals in php_ip2city.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(ip2city)
*/
/* True global resources - no need for thread safety here */
static int le_ip2city;
FILE * in;
/* {{{ ip2city_functions[]
*
* Every user visible function must have an entry in ip2city_functions[].
*/
const zend_function_entry ip2city_functions[] = {
PHP_FE(ip_city, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in ip2city_functions[] */
};
/* }}} */
/* {{{ ip2city_module_entry
*/
zend_module_entry ip2city_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"ip2city",
ip2city_functions,
PHP_MINIT(ip2city),
PHP_MSHUTDOWN(ip2city),
PHP_RINIT(ip2city), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(ip2city), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(ip2city),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_IP2CITY
ZEND_GET_MODULE(ip2city)
#endif
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
PHP_INI_ENTRY("ip2city.data", "foobar", PHP_INI_ALL, NULL)
PHP_INI_END()
/* }}} */
/* {{{ php_ip2city_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_ip2city_init_globals(zend_ip2city_globals *ip2city_globals)
{
ip2city_globals->global_value = 0;
ip2city_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ip2city)
{
REGISTER_INI_ENTRIES();
in = fopen(INI_STR("ip2city.data"), "rb");
// php_printf("php_minit_function\n");
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ip2city)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(ip2city)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(ip2city)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ip2city)
{
php_info_print_table_start();
php_info_print_table_header(2, "ip2city support", "enabled");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string ip_city(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(ip_city)
{
char *addr;
size_t addr_len;
unsigned int iplong = 0;
#ifdef HAVE_INET_PTON
struct in_addr ip;
#else
zend_ulong ip;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) == FAILURE) {
return;
}
#ifdef HAVE_INET_PTON
if (addr_len == 0 || inet_pton(AF_INET, addr, &ip) != 1) {
RETURN_FALSE;
}
iplong = ntohl(ip.s_addr);
#else
if (addr_len ==0 || (ip = inet_addr(addr)) == INADDR_NONE) {
RETURN_FLASE;
}
iplong = ntohl(ip);
#endif
unsigned int indexHead = 0;
unsigned int indexTail = 0;
fseek(in, 0, SEEK_SET);
fread(&indexHead, sizeof(indexHead), 1, in);
fread(&indexTail, sizeof(indexTail), 1, in);
IpStruct tmp;
char local[255] = {0};
int pos = searchIndex(iplong, in, indexHead, indexTail);
fseek(in, indexHead+pos*ipsize, SEEK_SET);
fread(&tmp, ipsize, 1, in);
searchLocal(tmp, iplong, in, local);
RETURN_STRING(local, 1);
}
int searchIndex(unsigned int iplong, FILE * db, int startPos, int mount) {
int i = 0;
int j = mount;
int m = 0;
int c = 0;
IpStruct tmpA;
while (i < j - 1) {
m = (int) (i + j) / 2;
fseek(db, startPos+m*ipsize, SEEK_SET);
fread(&tmpA, ipsize, 1, db);
c = compare(tmpA.ip, iplong);
// printf("compare out: %d m= %d %s\n", c, m, c < 0 ? "c < 0" : "c not < 0");
if (c < 0) {
i = m;
} else if (c > 0) {
j = m;
} else {
i = j = m;
}
}
return i;
}
int compare(unsigned char a[4], unsigned int tb) {
unsigned int ta = BYTE4INT(a);
if (ta > tb) {
return 1;
} else if (ta < tb) {
return -1;
} else {
return 0;
}
}
void GetData(unsigned char* str, FILE* db, int max) {
int i = 0;
unsigned char byte[1] = {0};;
int c = 0;
while ( (c = fgetc(db)) != '\0') {
*(str+i)=c;
i++;
}
str[i] = 0;
}
int searchAreaAddr(FILE * db, char * local) {
int pos = 0;
char buf[80] = {0};
fread(buf, 1, 1, db);
if (buf[0] == 0x02 ) {
// 获取地区偏移
fread(buf, 3, 1, db);
pos = BYTE3INT(buf);
fseek(db, pos, SEEK_SET);
fread(buf, 1, 1, db);
}
if ( buf[0] == 0x01 || buf[0] == 0x02 ) {
strcat(local, "未知");
return 0;
}
if ( buf[0] ) {
GetData(buf+1, db, 40);
}
strcat(local, (char*)buf);
}
int searchLocal(IpStruct tmp, unsigned int iplong, FILE * db, char * local) {
char buf[80] = {0};
int first = 0;
int offset = 0;
int countPos= 0;
int tmpCount = 0;
int pos = BYTE3INT(tmp.local);
fseek(db, pos, SEEK_SET);
fread(buf, 4, 1, db);
int c = compare(buf, iplong);
// 获取资料
fread(buf, 1, 1, db);
if ( buf[0] == 0x01 ) { // 国家地区均重复, 跳转至新地址
fread(buf, 3, 1, db);
pos = BYTE3INT(buf);
fseek(db, pos, SEEK_SET);
fread(buf, 1, 1, db);
}
// 获取国家
if ( buf[0] == 0x02 ) {
// 获取国家偏移
fread(buf, 3, 1, db);
// 保存地区信息
tmpCount = ftell(db);
pos = BYTE3INT(buf);
fseek(db, pos, SEEK_SET);
fread(buf, 1, 1, db);
}
if ( buf[0] == 0x01 || buf[0] == 0x02 ) {
strcat(local, "未知");
return;
}
if ( buf[0] ) {
GetData(buf+1, db, 40);
}
strcat(local, (char*)buf);
strcat(local, " ");
// 获取地区
if ( tmpCount ) {
fseek(db, tmpCount, SEEK_SET);
}
fread(buf, 1, 1, db);
while ( buf[0] == 0x02 ) {
// 获取地区偏移
fread(buf, 3, 1, db);
pos = BYTE3INT(buf);
fseek(db, pos, SEEK_SET);
fread(buf, 1, 1, db);
}
if ( buf[0] == 0x01 || buf[0] == 0x02 ) {
strcat(local, "未知");
return;
}
if ( buf[0] ) {
GetData(buf+1, db, 40);
}
strcat(local, (char*)buf);
return;
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/