conflicts fixed

This commit is contained in:
狮子的魂 2015-12-08 21:40:44 +08:00
commit f0c91e70b9
26 changed files with 90799 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
*.class
*.out
*.o
META-INF/
# Binary Files #
*.jar
# Package Files #
.settings/
.classpath
.project
# vim swp file #
*.swp

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "binding/ip2region4php"]
path = binding/ip2region4php
url = https://git.oschina.net/monsterkiller/ip2region.git

226
LICENSE.md Normal file
View File

@ -0,0 +1,226 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==========================================================================
The following license applies to the JQuery JavaScript library
--------------------------------------------------------------------------
Copyright (c) 2010 John Resig, http://jquery.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

75
README.md Normal file
View File

@ -0,0 +1,75 @@
ip2region - ip到地区的映射库妈妈再也不用担心我的ip定位。
**1. 99.9%准确率,不定时更新:**
数据聚合了一些知名ip到地名查询提供商的数据这些是他们官方的的准确率经测试着实比纯真啥的准确多了。<br />
每次聚合一下数据需要1-2天会不定时更新。
**2. 标准化的数据格式:**
每条ip数据段都固定了格式_城市Id|国家|区域|省份|城市|ISP_<br />
只有中国的数据精确到了城市其他国家只能定位到国家后前的选项全部是0已经包含了全部你能查到的大大小小的国家。<br />
请忽略前面的城市Id个人项目需求
**3. 体积小:**
生成的数据库文件ip2region.db只有3.5M
**4. 多查询客户端的支持0.0x毫秒级别的查询**
已经集成的客户端有java, php, c, pythonphp扩展(目前只支持linux)。
<pre>
提供了两种查询算法,响应时间如下:
客户端/binary算法/b-tree算法
java/0.x毫秒/0.x毫秒 (使用RandomAccessFile)
php/0.x毫秒/0.1x毫秒
c/0.0x毫秒/0.0x毫秒(b-tree算法基本稳定在0.02x毫秒级别)
python/0.x毫秒/0.1x毫秒
</pre>
任何客户端b-tree都比binary算法快
**5. 测试程序:**
java:
<pre>
cd binding/java
ant all
java -jar ip2region-{version}.jar ./data/ip2region.db
</pre>
php:
<pre>
php binding/php/testSearcher ./data/ip2region.db
</pre>
c:
<pre>cd binding/c/
gcc -g -O2 testSearcher.c ip2region.c
./a.out ../../data/ip2region.db
</pre>
python:
<pre>
python binding/python/testSearcher ./data/ip2region.db
</pre>
均会看到如下界面:
<pre>
initializing B-tree ...
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+
p2region>> 101.105.35.57
2163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds
</pre>
输入ip地址开始测试第一次会稍微有点慢在运行命令后面接入binary来尝试binary算法建议使用b-tree算法。
具体集成请参考不同客户端的测试源码。
**6. 联系作者:**
狮子的魂: chenxin619315@gmail.com

349
binding/c/ip2region.c Normal file
View File

@ -0,0 +1,349 @@
/**
* default ip2region implementation
*
* @see #ip2region.h
* @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-30
*/
#include "ip2region.h"
#include <stdio.h>
#include <stdlib.h>
/**
* create a new ip2region object
*
* @param dbFile path
*/
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
{
memset(ip2rObj, 0x00, sizeof(ip2region_entry));
ip2rObj->headerLen = 0;
ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
if ( ip2rObj->HeaderSip == NULL )
{
return 0;
}
ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
if ( ip2rObj->HeaderPtr == NULL )
{
IP2R_FREE(ip2rObj->HeaderSip);
return 0;
}
//open the db file
ip2rObj->dbHandler = fopen(dbFile, "rb");
if ( ip2rObj->dbHandler == NULL )
{
IP2R_FREE(ip2rObj->HeaderSip);
IP2R_FREE(ip2rObj->HeaderPtr);
return 0;
}
ip2rObj->firstIndexPtr = 0;
ip2rObj->lastIndexPtr = 0;
ip2rObj->totalBlocks = 0;
return 1;
}
/**
* destroy the specifield ip2region object
*
* @param ip2region_t
*/
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
{
IP2R_FREE(ip2rObj->HeaderSip);
ip2rObj->HeaderSip = NULL;
IP2R_FREE(ip2rObj->HeaderPtr);
ip2rObj->HeaderPtr = NULL;
//close the db file resource
if ( ip2rObj->dbHandler != NULL )
{
fclose(ip2rObj->dbHandler);
ip2rObj->dbHandler = NULL;
}
return 1;
}
/**
* get the region associated with the specifield ip address with binary search algorithm
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{
int l, h, m, p;
uint_t sip, eip, dptr;
char buffer[256];
int dataLen, dataptr;
if ( ip2rObj->totalBlocks == 0 )
{
fseek(ip2rObj->dbHandler, 0, 0);
if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) {
return 0;
}
ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0);
ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4);
ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
}
//binary search the index blocks to define the data block
l = 0; h = ip2rObj->totalBlocks; dptr = 0;
while ( l <= h ) {
m = (l + h) >> 1;
p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH;
fseek(ip2rObj->dbHandler, p, 0);
if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
return 0;
}
sip = getUnsignedInt(buffer, 0);
if ( ip < sip ) {
h = m - 1;
} else {
eip = getUnsignedInt(buffer, 4);
if ( ip > eip ) {
l = m + 1;
} else {
dptr = getUnsignedInt(buffer, 8);
break;
}
}
}
if ( dptr == 0 ) return 0;
//get the data
dataLen = ((dptr >> 24) & 0xFF);
dataptr = (dptr & 0x00FFFFFF);
//memset(data, 0x00, sizeof(data));
fseek(ip2rObj->dbHandler, dataptr, 0);
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
return 0;
}
//fill the data to the datablock
datablock->city_id = getUnsignedInt(buffer, 0);
dataLen -= 4; //reduce the length of the city_id
memcpy(datablock->region, buffer + 4, dataLen);
datablock->region[dataLen] = '\0';
return 1;
}
IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
{
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
}
/**
* get the region associated with the specifield ip address with b-tree algorithm
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{
int i, idx;
int l, m, h, p, sptr, eptr, indexBlockLen, dataLen, dataptr;
uint_t sip, eip, idxptr, dptr;
char buffer[TOTAL_HEADER_LENGTH];
if ( ip2rObj->headerLen == 0 )
{
idx = 0;
fseek(ip2rObj->dbHandler, 8, 0); //pass the super block
if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
return 0;
}
for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 )
{
sip = getUnsignedInt(buffer, i);
idxptr = getUnsignedInt(buffer, i + 4);
if ( idxptr == 0 ) break;
ip2rObj->HeaderSip[idx] = sip;
ip2rObj->HeaderPtr[idx] = idxptr;
idx++;
}
ip2rObj->headerLen = idx;
}
//search the header block to define the index block
l = 0; h = ip2rObj->headerLen; sptr = 0; eptr = 0;
while ( l <= h ) {
m = ((l + h) >> 1);
//perfetc matched, just return it
if ( ip == ip2rObj->HeaderSip[m] ) {
if ( m > 0 ) {
sptr = ip2rObj->HeaderPtr[m-1];
eptr = ip2rObj->HeaderPtr[m ];
} else {
sptr = ip2rObj->HeaderPtr[m ];
eptr = ip2rObj->HeaderPtr[m+1];
}
break;
}
//less then the middle value
if ( ip < ip2rObj->HeaderSip[m] ) {
if ( m == 0 ) {
sptr = ip2rObj->HeaderPtr[m ];
eptr = ip2rObj->HeaderPtr[m+1];
break;
} else if ( ip > ip2rObj->HeaderSip[m-1] ) {
sptr = ip2rObj->HeaderPtr[m-1];
eptr = ip2rObj->HeaderPtr[m ];
break;
}
h = m - 1;
} else {
if ( m == ip2rObj->headerLen - 1 ) {
sptr = ip2rObj->HeaderPtr[m-1];
eptr = ip2rObj->HeaderPtr[m ];
break;
} else if ( ip <= ip2rObj->HeaderSip[m+1] ) {
sptr = ip2rObj->HeaderPtr[m ];
eptr = ip2rObj->HeaderPtr[m+1];
break;
}
l = m + 1;
}
}
//not matched just stop it
if ( sptr == 0 ) return 0;
indexBlockLen = eptr - sptr;
fseek(ip2rObj->dbHandler, sptr, 0);
if ( fread(buffer, indexBlockLen + INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
return 0;
}
dptr = 0; l = 0; h = indexBlockLen / INDEX_BLOCK_LENGTH;
while ( l <= h ) {
m = ((l + h) >> 1);
p = m * INDEX_BLOCK_LENGTH;
sip = getUnsignedInt(buffer, p);
if ( ip < sip ) {
h = m - 1;
} else {
eip = getUnsignedInt(buffer, p + 4);
if ( ip > eip ) {
l = m + 1;
} else {
dptr = getUnsignedInt(buffer, p + 8);
break;
}
}
}
if ( dptr == 0 ) return 0;
dataLen = ((dptr >> 24) & 0xFF);
dataptr = (dptr & 0x00FFFFFF);
fseek(ip2rObj->dbHandler, dataptr, 0);
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
return 0;
}
datablock->city_id = getUnsignedInt(buffer, 0);
dataLen -= 4;
memcpy(datablock->region, buffer + 4, dataLen);
datablock->region[dataLen] = '\0';
return 1;
}
IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
{
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
}
/**
* get a unsinged long(4bytes) from a specifield buffer start from the specifield offset
*
* @param buffer
* @param offset
* @return uint_t
*/
IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
{
return (
((buffer[offset ]) & 0x000000FF) |
((buffer[offset+1] << 8) & 0x0000FF00) |
((buffer[offset+2] << 16) & 0x00FF0000) |
((buffer[offset+3] << 24) & 0xFF000000)
);
}
/**
* string ip to long
*
* @param ip
* @return uint_t
*/
IP2R_API uint_t ip2long(char *ip)
{
int i = 0, p = 24;
char buffer[4], *cs = ip;
uint_t ipval = 0;
while ( *cs != '\0' )
{
if ( *cs == '.' ) {
//single part length limit
if ( i > 3 ) {
ipval = 0;
break;
}
if ( p < 0 ) break;
buffer[i] = '\0';
ipval |= (atoi(buffer) << p);
p -= 8;
i = 0;
} else {
buffer[i++] = *cs;
}
cs++;
}
//append the rest parts
if ( i > 3 ) return 0;
buffer[i] = '\0';
ipval |= atoi(buffer);
return ipval;
}
/**
* long to string ip
*
* @param ip
* @param buffer
* @return uint_t(1 for success and 0 for failed)
*/
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
{
return 0;
}

136
binding/c/ip2region.h Normal file
View File

@ -0,0 +1,136 @@
/**
* ip2region header file
*
* @author chenxin<chenxin619315@gmail.com>
*/
#ifndef _IP2REGION_H
#define _IP2REGION_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//yat, just take it as this way, 99 percent and they works find
#if ( defined(_WIN32) || defined(_WINDOWS_) || defined(__WINDOWS_) )
# define OS_WINDOW
#else
# define OS_LINUX
#endif
#ifdef OS_WINDOW
# define IP2R_API extern __declspec(dllexport)
#else
/*platform shared library statement :: unix*/
# define IP2R_API extern
#endif
#define print(str) printf("%s", str)
#define println(str) printf("%s\n", str)
/*
* memory allocation macro definition.
* cause we should use emalloc,ecalloc .ege. in php.
* so you could make it better apdat the php environment.
*/
#define IP2R_CALLOC(_bytes, _blocks) calloc(_bytes, _blocks)
#define IP2R_MALLOC(_bytes) malloc(_bytes)
#define IP2R_FREE(_ptr) free(_ptr)
typedef unsigned short ushort_t;
typedef unsigned char uchar_t;
typedef unsigned int uint_t;
typedef unsigned long ulong_t;
#define INDEX_BLOCK_LENGTH 12
#define TOTAL_HEADER_LENGTH 4096
/*
* ip2region properties struct
*/
typedef struct {
uint_t *HeaderSip; //header start ip blocks
uint_t *HeaderPtr; //header ptr blocks
uint_t headerLen; //header block number
FILE *dbHandler; //file handler
uint_t firstIndexPtr; //first index ptr
uint_t lastIndexPtr; //last index ptr
uint_t totalBlocks; //total index blocks number
} ip2region_entry;
typedef ip2region_entry * ip2region_t;
/*
* data block
*/
typedef struct {
uint_t city_id;
char region[256];
} datablock_entry;
typedef datablock_entry * datablock_t;
/**
* create a new ip2region object
*
* @param ip2rObj
* @param dbFile path
*/
IP2R_API uint_t ip2region_create(ip2region_t, char *);
/**
* destroy the specifield ip2region object
*
* @param ip2region_t
*/
IP2R_API uint_t ip2region_destroy(ip2region_t);
/**
* get the region associated with the specifield ip address with binary search algorithm
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_binary_search(ip2region_t, uint_t, datablock_t);
IP2R_API uint_t ip2region_binary_search_string(ip2region_t, char *, datablock_t);
/**
* get the region associated with the specifield ip address with b-tree algorithm
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_btree_search(ip2region_t, uint_t, datablock_t);
IP2R_API uint_t ip2region_btree_search_string(ip2region_t, char *, datablock_t);
/**
* get a unsinged long(4bytes) from a specifield buffer start from the specifield offset
*
* @param buffer
* @param offset
* @return uint_t
*/
IP2R_API uint_t getUnsignedInt(char *, int);
/**
* string ip to long
*
* @param ip
* @return uint_t
*/
IP2R_API uint_t ip2long(char *);
/**
* long to string ip
*
* @param ip
* @param buffer
* @return uint_t(1 for success and 0 for failed)
*/
IP2R_API uint_t long2ip(uint_t, char *);
#endif /*end ifndef*/

99
binding/c/testSearcher.c Normal file
View File

@ -0,0 +1,99 @@
/**
* test ip2region searcher program
*
* @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-30
*/
#include "ip2region.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#define __PRINT_ABOUT__ \
println("+-------------------------------------+"); \
println("| ip2region test program |"); \
println("| Author: chenxin619315@gmail.com. |"); \
println("| Type 'quit' to exit the program. |"); \
println("+-------------------------------------+");
//read a line from a command line.
static char *getLine( FILE *fp, char *__dst )
{
register int c;
register char *cs;
cs = __dst;
while ( ( c = getc( fp ) ) != EOF ) {
if ( c == '\n' ) break;
*cs++ = c;
}
*cs = '\0';
return ( c == EOF && cs == __dst ) ? NULL : __dst;
}
static double getTime()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec * 1000 + ((double)tv.tv_usec)/1000);
}
int main( int argc, char **argv )
{
ip2region_entry ip2rEntry;
datablock_entry datablock;
char *dbFile = NULL, *algorithm = NULL;
char line[256];
uint_t (*func_ptr)(ip2region_t, char *, datablock_t);
double s_time, c_time;
memset(&datablock, 0x00, sizeof(datablock_entry));
if ( argc < 2 ) {
printf("Usage: a.out [ip2region db file path] [algorithm]");
return 0;
}
dbFile = argv[1];
algorithm = "B-tree";
func_ptr = ip2region_btree_search_string;
if ( argc >= 3 && strcmp(argv[2], "binary") == 0 ) {
algorithm = "Binary";
func_ptr = ip2region_binary_search_string;
}
//create a new ip2rObj
printf("+--initializing %s ... \n", algorithm);
if ( ip2region_create(&ip2rEntry, dbFile) == 0 )
{
println("Error: Fail to create the ip2region object");
return 0;
}
__PRINT_ABOUT__;
while ( 1 )
{
print("ip2region>> ");
getLine( stdin, line );
if ( strlen(line) < 2 ) continue;
if ( strcasecmp( line, "quit" ) == 0 ) {
println("+--Bye!");
break;
}
s_time = getTime();
func_ptr(&ip2rEntry, line, &datablock);
c_time = getTime() - s_time;
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
}
//destory the ip2rObj
ip2region_destroy(&ip2rEntry);
return 0;
}

1
binding/ip2region4php Submodule

@ -0,0 +1 @@
Subproject commit fc4849567b3af642f544a804ff6f411cc83862b6

72
binding/java/build.xml Normal file
View File

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="ip2region" default="usage" basedir=".">
<!--Initialize-->
<target name="init">
<echo message="------------------------------------------------------------"/>
<echo message="----------BUILDING JCSEG PACKAGE----------------------------"/>
<echo message=""/>
<property name="bottom" value="ip2region 2015"/>
<property name="jars" value="${basedir}"/>
<property name="sources" value="${basedir}/src"/>
<property name="classes" value="${basedir}/classes"/>
<property name="version" value="1.0"/>
<property name="api" value="${basedir}/doc"/>
<mkdir dir="${classes}"/>
<mkdir dir="${api}"/>
</target>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<!--Build-->
<target name="build" depends="init">
<echo message="-------------Compiling Application--------------"/>
<javac srcdir="${sources}" destdir="${classes}"></javac>
</target>
<!--Archive-->
<target name="dist" depends="build">
<jar jarfile="${jars}/ip2region-${version}.jar" basedir="${classes}">
<!--class fileter-->
<include name="**/*"/>
<!--manifest information setting-->
<manifest>
<attribute name="Main-Class" value="org.lionsoul.ip2region.test.TestSearcher"/>
<attribute name="Class-Path" value=""/>
</manifest>
</jar>
</target>
<!--Java document-->
<target name="all" depends="dist">
<echo message="------------Making Java Document------------------"/>
<javadoc packagenames="org.*"
sourcepath="${sources}"
destdir="${api}"
bottom="${bottom}"
charset="UTF-8"
author="false">
<classpath>
<pathelement location="${classes}"/>
</classpath>
</javadoc>
<jar jarfile="${jars}/ip2region-${version}-javadoc.jar" basedir="${api}" includes="**/*"></jar>
<!--do the data clean up-->
<delete dir="${classes}"/>
<delete dir="${api}"/>
</target>
<target name="usage">
<echo message="*** ip2region ANT Build Script ***"/>
<echo message="Usage: "/>
<echo message=" ant [target]"/>
<echo message=""/>
<echo message=" target : "/>
<echo message=" build : Build Application"/>
<echo message=" dist : Build Application + Archive (JAR)"/>
<echo message=" all : Build Application + Archive + JavaDoc"/>
</target>
</project>

View File

@ -0,0 +1,52 @@
package org.lionsoul.ip2region;
/**
* data block class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DataBlock
{
/**
* city id
*/
private int city_id;
/**
* region address
*/
private String region;
public DataBlock( int city_id, String region )
{
this.city_id = city_id;
this.region = region;
}
public int getCityId() {
return city_id;
}
public DataBlock setCityId(int city_id) {
this.city_id = city_id;
return this;
}
public String getRegion() {
return region;
}
public DataBlock setRegion(String region) {
this.region = region;
return this;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(city_id).append('|').append(region);
return sb.toString();
}
}

View File

@ -0,0 +1,59 @@
package org.lionsoul.ip2region;
/**
* database configuration class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbConfig
{
/**
* total header data block size
*/
private int totalHeaderSize;
/**
* max index data block size
* u should always choice the fastest read block size
*/
private int indexBlockSize;
/**
* construct method
*
* @param totalHeaderSize
* @param dataBlockSize
* @throws DbMakerConfigException
*/
public DbConfig( int totalHeaderSize ) throws DbMakerConfigException {
if ( (totalHeaderSize % 8) != 0 )
{
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
}
this.totalHeaderSize = totalHeaderSize;
this.indexBlockSize = 4096; //4 * 1024
}
public DbConfig() throws DbMakerConfigException {
this(8192);
}
public int getTotalHeaderSize() {
return totalHeaderSize;
}
public DbConfig setTotalHeaderSize(int totalHeaderSize) {
this.totalHeaderSize = totalHeaderSize;
return this;
}
public int getIndexBlockSize() {
return indexBlockSize;
}
public DbConfig setIndexBlockSize(int dataBlockSize) {
this.indexBlockSize = dataBlockSize;
return this;
}
}

View File

@ -0,0 +1,23 @@
package org.lionsoul.ip2region;
/**
* configuration exception
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbMakerConfigException extends Exception
{
private static final long serialVersionUID = 4495714680349884838L;
public DbMakerConfigException( String info ) {
super(info);
}
public DbMakerConfigException( Throwable res ) {
super(res);
}
public DbMakerConfigException( String info, Throwable res ) {
super(info, res);
}
}

View File

@ -0,0 +1,316 @@
package org.lionsoul.ip2region;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* ip db searcher class (Not thread safe)
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbSearcher
{
public static final int BTREE_ALGORITHM = 1;
public static final int BIN_ALGORITHM = 2;
/**
* db config
*/
private DbConfig dbConfig = null;
/**
* db file access handler
*/
private RandomAccessFile raf = null;
/**
* header blocks buffer
*/
private long[] HeaderSip = null;
private int[] HeaderPtr = null;
private int headerLength;
/**
* super blocks info
*/
private long firstIndexPtr = 0;
private long lastIndexPtr = 0;
private int totalIndexBlocks = 0;
/**
* construct class
*
* @param bdConfig
* @param dbFile
* @throws FileNotFoundException
*/
public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException
{
this.dbConfig = dbConfig;
raf = new RandomAccessFile(dbFile, "r");
}
/**
* get by index ptr
*
* @param indexPtr
* @throws IOException
*/
public DataBlock getByIndexPtr( long ptr ) throws IOException
{
raf.seek(ptr);
byte[] buffer = new byte[12];
raf.readFully(buffer, 0, buffer.length);
//long startIp = Util.getIntLong(buffer, 0);
//long endIp = Util.getIntLong(buffer, 4);
long extra = Util.getIntLong(buffer, 8);
int dataLen = (int)((extra >> 24) & 0xFF);
int dataPtr = (int)((extra & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.length);
int city_id = (int)Util.getIntLong(data, 0);
String region = new String(data, 4, data.length - 4, "UTF-8");
return new DataBlock(city_id, region);
}
/**
* get the region with a int ip address with b-tree algorithm
*
* @param ip
* @throws IOException
*/
public DataBlock btreeSearch( long ip ) throws IOException
{
//check and load the header
if ( HeaderSip == null )
{
raf.seek(8L); //pass the super block
//byte[] b = new byte[dbConfig.getTotalHeaderSize()];
byte[] b = new byte[4096];
raf.readFully(b, 0, b.length);
//fill the header
int len = b.length >> 3, idx = 0; //b.lenght / 8
HeaderSip = new long[len];
HeaderPtr = new int [len];
long startIp, dataPtr;
for ( int i = 0; i < b.length; i += 8 ) {
startIp = Util.getIntLong(b, i);
dataPtr = Util.getIntLong(b, i + 4);
if ( dataPtr == 0 ) break;
HeaderSip[idx] = startIp;
HeaderPtr[idx] = (int)dataPtr;
idx++;
}
headerLength = idx;
}
//1. define the index block with the binary search
if ( ip == HeaderSip[0] ) {
return getByIndexPtr(HeaderPtr[0]);
} else if ( ip == HeaderSip[headerLength-1] ) {
return getByIndexPtr(HeaderPtr[headerLength-1]);
}
int l = 0, h = headerLength, sptr = 0, eptr = 0;
while ( l <= h )
{
int m = (l + h) >> 1;
//perfetc matched, just return it
if ( ip == HeaderSip[m] ) {
if ( m > 0 ) {
sptr = HeaderPtr[m-1];
eptr = HeaderPtr[m ];
} else {
sptr = HeaderPtr[m ];
eptr = HeaderPtr[m+1];
}
break;
}
//less then the middle value
if ( ip < HeaderSip[m] ) {
if ( m == 0 ) {
sptr = HeaderPtr[m ];
eptr = HeaderPtr[m+1];
break;
} else if ( ip > HeaderSip[m-1] ) {
sptr = HeaderPtr[m-1];
eptr = HeaderPtr[m ];
break;
}
h = m - 1;
} else {
if ( m == headerLength - 1 ) {
sptr = HeaderPtr[m-1];
eptr = HeaderPtr[m ];
break;
} else if ( ip <= HeaderSip[m+1] ) {
sptr = HeaderPtr[m ];
eptr = HeaderPtr[m+1];
break;
}
l = m + 1;
}
}
//match nothing just stop it
if ( sptr == 0 ) return null;
//2. search the index blocks to define the data
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength();
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
raf.seek(sptr);
raf.readFully(iBuffer, 0, iBuffer.length);
l = 0; h = blockLen / blen;
long sip, eip, dataptr = 0;
while ( l <= h ) {
int m = (l + h) >> 1;
int p = m * blen;
sip = Util.getIntLong(iBuffer, p);
if ( ip < sip ) {
h = m - 1;
} else {
eip = Util.getIntLong(iBuffer, p + 4);
if ( ip > eip ) {
l = m + 1;
} else {
dataptr = Util.getIntLong(iBuffer, p + 8);
break;
}
}
}
//not matched
if ( dataptr == 0 ) return null;
//3. get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.length);
int city_id = (int)Util.getIntLong(data, 0);
String region = new String(data, 4, data.length - 4, "UTF-8");
return new DataBlock(city_id, region);
}
/**
* get the region throught the ip address with b-tree search algorithm
*
* @param ip
* @return DataBlock
* @throws IOException
*/
public DataBlock btreeSearch( String ip ) throws IOException
{
return btreeSearch(Util.ip2long(ip));
}
/**
* get the region with a int ip address with binary search algorithm
*
* @param ip
* @throws IOException
*/
public DataBlock binarySearch( long ip ) throws IOException
{
int blen = IndexBlock.getIndexBlockLength();
if ( totalIndexBlocks == 0 )
{
raf.seek(0L);
byte[] superBytes = new byte[8];
raf.readFully(superBytes, 0, superBytes.length);
//initialize the global vars
firstIndexPtr = Util.getIntLong(superBytes, 0);
lastIndexPtr = Util.getIntLong(superBytes, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1;
}
//search the index blocks to define the data
int l = 0, h = totalIndexBlocks;
byte[] buffer = new byte[blen];
long sip, eip, dataptr = 0;
while ( l <= h ) {
int m = (l + h) >> 1;
raf.seek(firstIndexPtr + m * blen); //set the file pointer
raf.readFully(buffer, 0, buffer.length);
sip = Util.getIntLong(buffer, 0);
if ( ip < sip ) {
h = m - 1;
} else {
eip = Util.getIntLong(buffer, 4);
if ( ip > eip ) {
l = m + 1;
} else {
dataptr = Util.getIntLong(buffer, 8);
break;
}
}
}
//not matched
if ( dataptr == 0 ) return null;
//get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.length);
int city_id = (int)Util.getIntLong(data, 0);
String region = new String(data, 4, data.length - 4, "UTF-8");
return new DataBlock(city_id, region);
}
/**
* get the region throught the ip address with binary search algorithm
*
* @param ip
* @return DataBlock
* @throws IOException
*/
public DataBlock binarySearch( String ip ) throws IOException
{
return binarySearch(Util.ip2long(ip));
}
/**
* get the db config
*
* @return DbConfig
*/
public DbConfig getDbConfig()
{
return dbConfig;
}
/**
* close the db
*
* @throws IOException
*/
public void close() throws IOException
{
HeaderSip = null; //let gc do its work
HeaderPtr = null;
raf.close();
}
}

View File

@ -0,0 +1,64 @@
package org.lionsoul.ip2region;
/**
* header block class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class HeaderBlock
{
/**
* index block start ip address
*/
private long indexStartIp;
/**
* ip address
*/
private int indexPtr;
public HeaderBlock( long indexStartIp, int indexPtr )
{
this.indexStartIp = indexStartIp;
this.indexPtr = indexPtr;
}
public long getIndexStartIp() {
return indexStartIp;
}
public HeaderBlock setIndexStartIp(long indexStartIp) {
this.indexStartIp = indexStartIp;
return this;
}
public int getIndexPtr() {
return indexPtr;
}
public HeaderBlock setIndexPtr(int indexPtr) {
this.indexPtr = indexPtr;
return this;
}
/**
* get the bytes for db storage
*
* @return byte[]
*/
public byte[] getBytes()
{
/*
* +------------+-----------+
* | 4bytes | 4bytes |
* +------------+-----------+
* start ip index ptr
*/
byte[] b = new byte[8];
Util.writeIntLong(b, 0, indexStartIp);
Util.writeIntLong(b, 4, indexPtr);
return b;
}
}

View File

@ -0,0 +1,104 @@
package org.lionsoul.ip2region;
/**
* item index class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class IndexBlock
{
private static int LENGTH = 12;
/**
* start ip address
*/
private long startIp;
/**
* end ip address
*/
private long endIp;
/**
* data ptr and data length
*/
private int dataPtr;
/**
* data length
*/
private int dataLen;
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
{
this.startIp = startIp;
this.endIp = endIp;
this.dataPtr = dataPtr;
this.dataLen = dataLen;
}
public long getStartIp() {
return startIp;
}
public IndexBlock setStartIp(long startIp) {
this.startIp = startIp;
return this;
}
public long getEndIp() {
return endIp;
}
public IndexBlock setEndIp(long endIp) {
this.endIp = endIp;
return this;
}
public int getDataPtr() {
return dataPtr;
}
public IndexBlock setDataPtr(int dataPtr) {
this.dataPtr = dataPtr;
return this;
}
public int getDataLen() {
return dataLen;
}
public IndexBlock setDataLen(int dataLen) {
this.dataLen = dataLen;
return this;
}
public static int getIndexBlockLength() {
return LENGTH;
}
/**
* get the bytes for storage
*
* @return byte[]
*/
public byte[] getBytes()
{
/*
* +------------+-----------+-----------+
* | 4bytes | 4bytes | 4bytes |
* +------------+-----------+-----------+
* start ip end ip data ptr + len
*/
byte[] b = new byte[12];
Util.writeIntLong(b, 0, startIp); //start ip
Util.writeIntLong(b, 4, endIp); //end ip
//write the data ptr and the length
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
Util.writeIntLong(b, 8, mix);
return b;
}
}

View File

@ -0,0 +1,145 @@
package org.lionsoul.ip2region;
/**
* util class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class Util
{
/**
* write specfield bytes to a byte array start from offset
*
* @param b
* @param offset
* @param v
* @param bytes
*/
public static void write( byte[] b, int offset, long v, int bytes)
{
for ( int i = 0; i < bytes; i++ )
{
b[offset++] = (byte)((v >>> (8 * i)) & 0xFF);
}
}
/**
* write a int to a byte array
*
* @param b
* @param offet
* @param v
*/
public static void writeIntLong( byte[] b, int offset, long v )
{
b[offset++] = (byte)((v >> 0) & 0xFF);
b[offset++] = (byte)((v >> 8) & 0xFF);
b[offset++] = (byte)((v >> 16) & 0xFF);
b[offset ] = (byte)((v >> 24) & 0xFF);
}
/**
* get a int from a byte array start from the specifiled offset
*
* @param b
* @param offset
*/
public static long getIntLong( byte[] b, int offset )
{
return (
((b[offset++] & 0x000000FFL)) |
((b[offset++] << 8) & 0x0000FF00L) |
((b[offset++] << 16) & 0x00FF0000L) |
((b[offset ] << 24) & 0xFF000000L)
);
}
/**
* get a int from a byte array start from the specifield offset
*
* @param b
* @param offset
*/
public static int getInt3( byte[] b, int offset )
{
return (
(b[offset++] & 0x000000FF) |
(b[offset++] & 0x0000FF00) |
(b[offset ] & 0x00FF0000)
);
}
public static int getInt2( byte[] b, int offset )
{
return (
(b[offset++] & 0x000000FF) |
(b[offset ] & 0x0000FF00)
);
}
public static int getInt1( byte[] b, int offset )
{
return (
(b[offset] & 0x000000FF)
);
}
/**
* string ip to long ip
*
* @param ip
* @return long
*/
public static long ip2long( String ip )
{
String[] p = ip.split("\\.");
if ( p.length != 4 ) return 0;
int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000);
int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000);
int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00);
int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF);
return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
}
/**
* int to ip string
*
* @param ip
* @return string
*/
public static String long2ip( long ip )
{
StringBuilder sb = new StringBuilder();
sb
.append((ip >> 24) & 0xFF).append('.')
.append((ip >> 16) & 0xFF).append('.')
.append((ip >> 8) & 0xFF).append('.')
.append((ip >> 0) & 0xFF);
return sb.toString();
}
/**
* check the validate of the specifeld ip address
*
* @param ip
* @return boolean
*/
public static boolean isIpAddress( String ip )
{
String[] p = ip.split("\\.");
if ( p.length != 4 ) return false;
for ( String pp : p )
{
if ( pp.length() > 3 ) return false;
int val = Integer.valueOf(pp);
if ( val > 255 ) return false;
}
return true;
}
}

View File

@ -0,0 +1,82 @@
package org.lionsoul.ip2region.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
/**
* project test script
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class TestSearcher
{
public static void main(String[] argv)
{
if ( argv.length == 0 ) {
System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]");
return;
}
int algorithm = DbSearcher.BTREE_ALGORITHM;
File file = new File(argv[0]);
if ( file.exists() == false ) {
System.out.println("Error: Invalid ip2region.db file");
return;
}
if ( argv.length > 1 ) {
if ( argv[1].equalsIgnoreCase("binary")) algorithm = DbSearcher.BIN_ALGORITHM;
}
try {
System.out.println("initializing "+((algorithm==2)?"Binary":"B-tree")+" ... ");
DbConfig config = new DbConfig();
DbSearcher seacher = new DbSearcher(config, argv[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("+----------------------------------+");
System.out.println("| ip2region test shell |");
System.out.println("| Author: chenxin619315@gmail.com |");
System.out.println("| Type 'quit' to exit program |");
System.out.println("+----------------------------------+");
double sTime = 0, cTime = 0;
String line = null;
DataBlock dataBlock = null;
while ( true )
{
System.out.print("ip2region>> ");
line = reader.readLine().trim();
if ( line.length() < 2 ) continue;
if ( line.equalsIgnoreCase("quit") ) break;
if ( Util.isIpAddress(line) == false ) {
System.out.println("Error: Invalid ip address");
continue;
}
sTime = System.nanoTime();
dataBlock = algorithm==2 ? seacher.binarySearch(line) : seacher.btreeSearch(line);
cTime = (System.nanoTime() - sTime) / 1000000;
System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime);
}
reader.close();
seacher.close();
System.out.println("+--Bye");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DbMakerConfigException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,96 @@
package org.lionsoul.ip2region.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
/**
* data check class
*
* @author koma<komazhang@foxmail.com>
**/
public class TestUnit {
public static void main(String[] args) {
try {
DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db");
BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt"));
BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true));
int errCount = 0;
int lineCount = 0;
String str = null;
while ( (str = bfr.readLine()) != null ) {
StringBuffer line = new StringBuffer(str);
//get first ip
int first_idx = line.indexOf("|");
String first_ip = line.substring(0, first_idx);
line = new StringBuffer( line.substring(first_idx + 1) );
//get second ip
int second_idx = line.indexOf("|");
String second_ip = line.substring(0, second_idx);
//get addr
String source_region = line.substring(second_idx + 1);
//search from DbSearcher
System.out.println("+---Start, start to search");
System.out.println("+---[Info]: Source region = "+source_region);
System.out.println("+---[Info]: Step1, search for first IP: "+first_ip);
DataBlock fdata = _searcher.binarySearch(first_ip);
if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) {
System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion());
bwr.write("[Source]: Region: "+fdata.getRegion());
bwr.newLine();
bwr.write("[Source]: First Ip: "+first_ip);
bwr.newLine();
bwr.write("[DB]: Region: "+fdata.getRegion());
bwr.newLine();
bwr.flush();
errCount++;
}
System.out.println("+---[Info]: Step2, search for second IP: "+second_ip);
DataBlock sdata = _searcher.btreeSearch(second_ip);
if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) {
System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion());
bwr.write("[Source]: Region: "+sdata.getRegion());
bwr.newLine();
bwr.write("[Source]: First Ip: "+second_ip);
bwr.newLine();
bwr.write("[DB]: Region: "+sdata.getRegion());
bwr.newLine();
bwr.flush();
errCount++;
}
lineCount++;
}
bwr.close();
bfr.close();
System.out.println("+---Done, search complished");
System.out.println("+---Statistics, Error count = "+errCount
+", Total line = "+lineCount
+", Fail ratio = "+((float)(errCount/lineCount))*100+"%");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DbMakerConfigException e) {
e.printStackTrace();
} catch ( Exception e ) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,57 @@
package org.lionsoul.ip2region.test;
import org.lionsoul.ip2region.Util;
/**
* project test script
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class TestUtil
{
public static void main(String[] argv)
{
/* //1. test the ip2long
String[] ipSet = new String[]{
"120.24.78.68",
"120.24.229.68",
"120.24.87.145",
"218.17.162.99"
};
for ( String ip : ipSet )
{
int ipInt = Util.ip2Int(ip);
System.out.println("src ip: " + ip + ", ip2Int: " + ipInt + ", int2IP: " + Util.int2IP(ipInt));
}*/
/* int[] arr = new int[]{12344, -1234, 2146789, 0, -1024};
byte[] b = new byte[arr.length * 4];
//write the int
System.out.println("+--Testing writeInt ... ");
int i, idx = 0;
for ( i = 0; i < b.length; i += 4 )
{
System.out.println("offset: " + i);
Util.writeInt(b, i, arr[idx++]);
}
System.out.println("|----[Ok]");
//read the int
System.out.println("+--Testing getInt ... ");
idx = 0;
for ( i = 0; i < b.length; i += 4 )
{
System.out.println(arr[idx++]+", " + Util.getInt(b, i));
}
System.out.println("|----[Ok]");*/
/* HeaderBlock headerBlock = new HeaderBlock(241658345, 2134785);
byte[] b = headerBlock.getBytes();
System.out.println(headerBlock.getIndexStartIp() + ", " + headerBlock.getIndexPtr());
System.out.println(Util.getInt(b, 0) + ", " + Util.getInt(b, 4));*/
System.out.println(Util.ip2long("255.255.255.0"));
}
}

View File

@ -0,0 +1,253 @@
<?php
/**
* ip2region php seacher client class
*
* @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-29
*/
defined('INDEX_BLOCK_LENGTH') or define('INDEX_BLOCK_LENGTH', 12);
defined('TOTAL_HEADER_LENGTH') or define('TOTAL_HEADER_LENGTH', 4096);
class Ip2Region
{
/**
* db file handler
*/
private $dbFileHandler = NULL;
/**
* header block info
*/
private $HeaderSip = NULL;
private $HeaderPtr = NULL;
private $headerLen = 0;
/**
* super block index info
*/
private $firstIndexPtr = 0;
private $lastIndexPtr = 0;
private $totalBlocks = 0;
/**
* construct method
*
* @param ip2regionFile
*/
public function __construct( $ip2regionFile )
{
$this->dbFileHandler = fopen($ip2regionFile, 'r');
}
/**
* get the data block throught the specifield ip address or long ip numeric with binary search algorithm
*
* @param ip
* @return mixed Array or NULL for any error
*/
public function binarySearch( $ip )
{
//check and conver the ip address
if ( is_string($ip) ) $ip = ip2long($ip);
if ( $this->totalBlocks == 0 )
{
fseek($this->dbFileHandler, 0);
$superBlock = fread($this->dbFileHandler, 8);
$this->firstIndexPtr = self::getLong($superBlock, 0);
$this->lastIndexPtr = self::getLong($superBlock, 4);
$this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
}
//binary search to define the data
$l = 0;
$h = $this->totalBlocks;
$dataPtr = 0;
while ( $l <= $h )
{
$m = (($l + $h) >> 1);
$p = $m * INDEX_BLOCK_LENGTH;
fseek($this->dbFileHandler, $this->firstIndexPtr + $p);
$buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH);
$sip = self::getLong($buffer, 0);
if ( $ip < $sip ) {
$h = $m - 1;
} else {
$eip = self::getLong($buffer, 4);
if ( $ip > $eip ) {
$l = $m + 1;
} else {
$dataPtr = self::getLong($buffer, 8);
break;
}
}
}
//not matched just stop it here
if ( $dataPtr == 0 ) return NULL;
//get the data
$dataLen = (($dataPtr >> 24) & 0xFF);
$dataPtr = ($dataPtr & 0x00FFFFFF);
fseek($this->dbFileHandler, $dataPtr);
$data = fread($this->dbFileHandler, $dataLen);
return array(
'city_id' => self::getLong($data, 0),
'region' => substr($data, 4)
);
}
/**
* get the data block associated with the specifield ip with b-tree search algorithm
*
* @param ip
* @return Mixed Array for NULL for any error
*/
public function btreeSearch( $ip )
{
if ( is_string($ip) ) $ip = ip2long($ip);
//check and load the header
if ( $this->HeaderSip == NULL )
{
fseek($this->dbFileHandler, 8);
$buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH);
//fill the header
$idx = 0;
$this->HeaderSip = array();
$this->HeaderPtr = array();
for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 )
{
$startIp = self::getLong($buffer, $i);
$dataPtr = self::getLong($buffer, $i + 4);
if ( $dataPtr == 0 ) break;
$this->HeaderSip[] = $startIp;
$this->HeaderPtr[] = $dataPtr;
$idx++;
}
$this->headerLen = $idx;
}
//1. define the index block with the binary search
$l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0;
while ( $l <= $h )
{
$m = (($l + $h) >> 1);
//perfetc matched, just return it
if ( $ip == $this->HeaderSip[$m] ) {
if ( $m > 0 ) {
$sptr = $this->HeaderPtr[$m-1];
$eptr = $this->HeaderPtr[$m ];
} else {
$sptr = $this->HeaderPtr[$m ];
$eptr = $this->HeaderPtr[$m+1];
}
break;
}
//less then the middle value
if ( $ip < $this->HeaderSip[$m] ) {
if ( $m == 0 ) {
$sptr = $this->HeaderPtr[$m ];
$eptr = $this->HeaderPtr[$m+1];
break;
} else if ( $ip > $this->HeaderSip[$m-1] ) {
$sptr = $this->HeaderPtr[$m-1];
$eptr = $this->HeaderPtr[$m ];
break;
}
$h = $m - 1;
} else {
if ( $m == $this->headerLen - 1 ) {
$sptr = $this->HeaderPtr[$m-1];
$eptr = $this->HeaderPtr[$m ];
break;
} else if ( $ip <= $this->HeaderSip[$m+1] ) {
$sptr = $this->HeaderPtr[$m ];
$eptr = $this->HeaderPtr[$m+1];
break;
}
$l = $m + 1;
}
}
//match nothing just stop it
if ( $sptr == 0 ) return NULL;
//2. search the index blocks to define the data
$blockLen = $eptr - $sptr;
fseek($this->dbFileHandler, $sptr);
$index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH);
$dataptr = 0;
$l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH;
while ( $l <= $h ) {
$m = (($l + $h) >> 1);
$p = (int)($m * INDEX_BLOCK_LENGTH);
$sip = self::getLong($index, $p);
if ( $ip < $sip ) {
$h = $m - 1;
} else {
$eip = self::getLong($index, $p + 4);
if ( $ip > $eip ) {
$l = $m + 1;
} else {
$dataptr = self::getLong($index, $p + 8);
break;
}
}
}
//not matched
if ( $dataptr == 0 ) return NULL;
//3. get the data
$dataLen = (($dataptr >> 24) & 0xFF);
$dataPtr = ($dataptr & 0x00FFFFFF);
fseek($this->dbFileHandler, $dataPtr);
$data = fread($this->dbFileHandler, $dataLen);
return array(
'city_id' => self::getLong($data, 0),
'region' => substr($data, 4)
);
}
/**
* read a long from a byte buffer
*
* @param b
* @param offset
*/
public static function getLong( $b, $offset )
{
return (
(ord($b[$offset++])) |
(ord($b[$offset++]) << 8) |
(ord($b[$offset++]) << 16) |
(ord($b[$offset ]) << 24)
);
}
/**
* destruct method, resource destroy
*/
public function __destruct()
{
if ( $this->dbFileHandler != NULL ) fclose($this->dbFileHandler);
$this->HeaderSip = NULL;
$this->HeaderPtr = NULL;
}
}
?>

View File

@ -0,0 +1,61 @@
<?php
/**
* Ip2Region php client test script
*
* @author chenxin<chenxin619315@gmail.com>
*/
if ( $argc < 2 )
{
$usage = <<<EOF
Usage: php Test.php [ip2region db file] [alrogrithm]
+-Algorithm: binary or b-tree\n
EOF;
exit($usage);
}
array_shift($argv);
$dbFile = $argv[0];
$method = 1;
$algorithm = 'B-tree';
if ( isset($argv[1])
&& strtolower($argv[1]) == 'binary' ) {
$method = 2;
$algorithm = 'Binary';
}
require dirname(__FILE__) . '/Ip2Region.class.php';
$ip2regionObj = new Ip2Region($dbFile);
$initStr = <<<INIT
initializing {$algorithm} ...
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+
INIT;
echo $initStr, "\n";
while ( true )
{
echo "ip2region>> ";
$line = trim(fgets(STDIN));
if ( strlen($line) < 2 ) continue;
if ( $line == 'quit' ) break;
if ( ip2long($line) == NULL ) {
echo "Error: invalid ip address\n";
continue;
}
$s_time = getTime();
$data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line);
$c_time = getTime() - $s_time;
printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time);
}
function getTime()
{
return (microtime(true) * 1000);
}
?>

192
binding/python/ip2Region.py Normal file
View File

@ -0,0 +1,192 @@
#-*- coding:utf-8 -*-
"""
" ip2region python seacher client module
"
" Autho: koma<komazhang@foxmail.com>
" Date : 2015-11-06
"""
import struct, io, socket, sys
class Ip2Region(object):
__headerSip = []
__headerPtr = []
__f = None
def __init__(self, dbfile):
self.initDatabase(dbfile)
def binarySearch(self, ip):
"""
" binary search method
" param: ip
"""
if not ip.isdigit(): ip = self.ip2long(ip)
self.__f.seek(0)
b = self.__f.read(8)
startPtr = self.getLong(b, 0)
endPtr = self.getLong(b, 4)
indexLen = endPtr - startPtr
self.__f.seek(startPtr)
b = self.__f.read(indexLen+12)
l, h, mixPtr = (0, int(indexLen/12), 0)
while l <= h:
m = int((l+h)/2)
ptr = startPtr + m*12
self.__f.seek(ptr)
b = self.__f.read(12)
sip = self.getLong(b, 0)
eip = self.getLong(b, 4)
if ip > sip:
if ip > eip:
l = m + 1
else:
mixPtr = self.getLong(b, 8)
break;
else:
h = m - 1
if mixPtr == 0: return "N2"
dataPtr = mixPtr & 0x00FFFFFFL
dataLen = (mixPtr >> 24) & 0xFF
self.__f.seek(dataPtr)
data = self.__f.read(dataLen)
return {
"city_id": self.getLong(data, 0),
"region" : data[4:]
}
def btreeSearch(self, ip):
"""
" b-tree search method
" param: ip
"""
if not ip.isdigit(): ip = self.ip2long(ip)
headerLen = len(self.__headerSip) - 1
l, h, sptr, eptr = (0, headerLen, 0, 0)
while l <= h:
m = int((l+h)/2)
if ip == self.__headerSip[m]:
if m > 0:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
else:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
if ip > self.__headerSip[m]:
if m == headerLen:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
elif ip < self.__headerSip[m+1]:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
l = m + 1
else:
if m == 0:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
elif ip > self.__headerSip[m-1]:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
h = m - 1
if sptr == 0: return "N1"
indexLen = eptr - sptr
self.__f.seek(sptr)
b = self.__f.read(indexLen + 12)
l, h, mixPtr = (0, int(indexLen/12), 0)
while l <= h:
m = int((l+h)/2)
offset = m * 12
if ip > self.getLong(b, offset):
if ip > self.getLong(b, offset+4):
l = m + 1
else:
mixPtr = self.getLong(b, offset+8)
break;
else:
h = m - 1
if mixPtr == 0: return "N2"
dataPtr = mixPtr & 0x00FFFFFFL
dataLen = (mixPtr >> 24) & 0xFF
self.__f.seek(dataPtr)
data = self.__f.read(dataLen)
return {
"city_id": self.getLong(data, 0),
"region" : data[4:]
}
def initDatabase(self, dbfile):
"""
" initialize the database for search
" param: dbFile
"""
try:
self.__f = io.open(dbfile, "rb")
#pass the super block
self.__f.seek(8)
#read the header block
b = self.__f.read(4086)
#parse the header block
sip = None
ptr = None
for i in range(0, len(b)-1, 8):
sip = self.getLong(b, i)
ptr = self.getLong(b, i+4)
if ptr == 0:
break
self.__headerSip.append(sip)
self.__headerPtr.append(ptr)
except IOError, e:
print "[Error]: ", e
sys.exit()
def ip2long(self, ip):
_ip = socket.inet_aton(ip)
return struct.unpack("!L", _ip)[0]
def isip(self, ip):
p = ip.split(".")
if len(p) != 4 : return False
for pp in p:
if not pp.isdigit(): return False
if len(pp) > 3 : return False
if int(pp) > 255 : return False
return True
def getLong(self, b, offset):
if len( b[offset:offset+4] ) == 4:
return struct.unpack('I', b[offset:offset+4])[0]
return 0
def close(self):
self.__headerSip = None
self.__headerPtr = None
self.__f.close()
self.__f = None

Binary file not shown.

View File

@ -0,0 +1,75 @@
#-*- coding:utf-8 -*-
"""
" ip2region python seacher client module
"
" Autho: koma<komazhang@foxmail.com>
" Date : 2015-11-06
"""
import struct, sys, os, time
from ip2Region import Ip2Region
def testSearch():
"""
" ip2region test function
"""
llen = len(sys.argv)
if llen < 2:
print "Usage: python ip2Region.py [ip2region db file] [alrogrithm]"
print "Algorithm: binary or b-tree"
return 0
dbFile = sys.argv[1]
method = 1
algorithm = "b-tree"
if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
print "[Error]: Specified db file is not exists."
return 0
if llen > 2:
algorithm = sys.argv[2]
if algorithm == "binary":
method = 2
print "initializing %s..." % (algorithm)
print "+----------------------------------+"
print "| ip2region test script |"
print "| Author: komazhang@foxmail.com |"
print "| Type 'quit' to exit program |"
print "+----------------------------------+"
searcher = Ip2Region(dbFile);
while True:
line = raw_input("ip2region>> ")
line = line.strip()
if line == "":
print "[Error]: Invalid ip address."
continue
if line == "quit":
print "[Info]: Thanks for your use, Bye."
break
if not searcher.isip(line):
print "[Error]: Invalid ip address."
continue
sTime = time.time() * 1000
if method == 1:
data = searcher.btreeSearch(line)
else:
data = searcher.binarySearch(line)
eTime = time.time() * 1000
if isinstance(data, dict):
print "[Return]: %s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
else:
print "[Error]: ", data
searcher.close()
if __name__ == "__main__":
testSearch()

88247
data/ip.merge.txt Normal file

File diff suppressed because it is too large Load Diff

BIN
data/ip2region.db Normal file

Binary file not shown.