资源简介
封装在一个类里的可以直接运行的ip数据库查询,可以直接在自己的程序中调用该类,并且带有最新的20110920QQwry.dat

代码片段和文件信息
#include “IPSearch.h“
IPSearch::IPSearch()
{
this->data = 0; //指向纯真IP数据库的指针
}
IPSearch::~IPSearch()
{
}
// 功能:将纯真IP数据库中的所有数据读入全局数组data
// 参数:file 文件字符串
// 返回值: 0 成功
int IPSearch::init(byte* file)
{
ulong fileSize; //文件长度
FILE* fp;
fp = fopen((char*)file“rb“);
if(fp == NULL)
return 1;
//获取文件长度
fseek(fp 0 SEEK_END);
fileSize = ftell(fp);
//将纯真IP数据库中的所有数据读入数组
data=(byte*)malloc(fileSize* sizeof(byte));
fseek(fp0SEEK_SET);
fread(data1fileSizefp);
if(data == NULL)
return 2;
fclose(fp);
fp=NULL;
//读取IP数据库文件头
dbheader.firstStartIpOffset=getInt32(&data[0]4);
dbheader.lastStartIpOffset=getInt32(&data[4]4);
//printf(“%ld %ld \n“dbheader.firstStartIpOffsetdbheader.lastStartIpOffset);
return 0;
}
// 功能:查询ipStr
IPLocation IPSearch::queryIp(byte* ipStr)
{
ulong ip;
ulong offset;
if(init((byte*)“QQWry.Dat“)==1)
{
printf(“QQWry.Dat 初始化失败!\n“);
}
ip=IpToLong(ipStr);
offset=getOffsetOfIp(ip);
//printf(“ip:%u offset: %u“ipoffset);
return getIPLocation(offset);
}
//功能: 二分法获得IP记录偏移量
//参数:ip 待查询ip
//返回值:相对于文件头的偏移量 (0 失败)
ulong IPSearch::getOffsetOfIp(ulong ip)
{
ulong low=0;
ulong high=(dbheader.lastStartIpOffset-dbheader.firstStartIpOffset)/7;
ulong endIpOff;
ulong mid;
while(low {
mid=(low+high)/2;
if(ip high=mid;
else
low=mid;
}
//printf(“\nlow:%ld \n“low);
if(ip>=getIpFromIndex(low&endIpOff)&&ip<=getIpFromRecord(endIpOff))
{
return endIpOff;
}
return 0L;
}
// 功能:从索引区获得IP地址
// 参数:left 索引
// 返回值:起始Ip地址
// endIpOff 该索引所对应的结束Ip偏移量
ulong IPSearch::getIpFromIndex(ulong leftulong* addr)
{
ulong leftOffset = dbheader.firstStartIpOffset + (left * 7L);
*addr=getInt32(&data[leftOffset+4]3);
return getInt32(&data[leftOffset]4);
}
// 功能:从记录区获得IP地址
ulong IPSearch::getIpFromRecord(ulong endIpOff)
{
return getInt32(&data[endIpOff]4);
}
// 功能:将字符数组转成一个整数
ulong IPSearch::getInt32(byte* bufint num)
{
ulong ret=0;
int i;
for(i=0;i {
ret+=(buf[i]<<(8*i));
}
return ret ;
}
//将ip从字符串形式转化为一个长整数
ulong IPSearch::IpToLong(byte* ipStr)
{
byte* p=ipStr;
byte ch;
int i=0;
int shift=24;
ulong ret=0temp;
for(i=0;i<4;i++)
{
temp=0;
while((ch=*p++)!=‘.‘ && ch)
temp=temp*10+(ch -‘0‘);
ret+=(temp< shift-=8;
}
if(i!=4)
return -1;
return ret;
}
// 功能:给定一个ip国家地区记录的偏移,返回一个IPLocation结构
// 参数: offset 国家记录的起始偏移
// 返回值:IPLocation对象
IPLocation IPSearch::getIPLocation(ulong offset)
{
byte flag_char;
// 跳过4字节ip读取随后字节判断是否为标志字节
flag_char=data[offset+4];
if(flag_char == REDIRECT_MODE_1) {
// 读取国家偏移(3个字节)
ulong countryOffset=getInt32(&data[offset+5]3);
// 再检查一次标志字节,因为这个时候这个地方仍然可能是个重定向
flag_char=data[countryOffset];
if(flag_char == REDIRECT_MODE_2) {
// 读取国家偏移
int temp;
temp=getInt3
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 36352 2011-10-10 14:33 ip_c_class\Debug\ip_c_class.exe
文件 352768 2011-10-10 14:33 ip_c_class\Debug\ip_c_class.ilk
文件 584704 2011-10-10 14:33 ip_c_class\Debug\ip_c_class.pdb
文件 6914 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\BuildLog.htm
文件 17571 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\IPSearch.obj
文件 663 2011-10-10 13:24 ip_c_class\ip_c_class\Debug\ip_c_class.exe.em
文件 728 2011-10-10 13:24 ip_c_class\ip_c_class\Debug\ip_c_class.exe.em
文件 621 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\ip_c_class.exe.intermediate.manifest
文件 67 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\mt.dep
文件 25513 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\test.obj
文件 191488 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\vc90.idb
文件 217088 2011-10-10 14:33 ip_c_class\ip_c_class\Debug\vc90.pdb
文件 4726 2011-10-10 14:28 ip_c_class\ip_c_class\IPSearch.cpp
文件 2037 2011-10-10 14:30 ip_c_class\ip_c_class\IPSearch.h
文件 4048 2011-10-10 11:15 ip_c_class\ip_c_class\ip_c_class.vcproj
文件 1427 2011-10-10 14:34 ip_c_class\ip_c_class\ip_c_class.vcproj.PC-201108081419.Administrator.user
文件 9151777 2011-09-20 19:28 ip_c_class\ip_c_class\QQWry.Dat
文件 404 2011-10-10 14:32 ip_c_class\ip_c_class\test.cpp
文件 1567744 2011-10-10 14:34 ip_c_class\ip_c_class.ncb
文件 896 2011-10-10 11:09 ip_c_class\ip_c_class.sln
..A..H. 14336 2011-10-10 14:34 ip_c_class\ip_c_class.suo
目录 0 2011-10-10 14:33 ip_c_class\ip_c_class\Debug
目录 0 2011-10-10 14:30 ip_c_class\Debug
目录 0 2011-10-10 14:32 ip_c_class\ip_c_class
目录 0 2011-10-10 11:54 ip_c_class
----------- --------- ---------- ----- ----
12181872 25
相关资源
- 北京化工大学计算方法(C/C++)讲义
- GBT 28169-2011 嵌入式软件 C语言编码规范
- XUnZip Zip解压缩.rar
- Windows_API_函数大全 C/C++
- C语言程序设计教材习题参考答案.do
- 基于MFC的VC++仿QQ浏览器源码(雏形)
- 高效FFT的C/C++代码实现包括基2的DIF和
- c/c++开发网络验证和本地验证
- 操作系统存储管理实验报告c/c++
- C++编写的万年历源码
- C语言进阶源码---基于graphics实现图书
- 井字棋三连棋的AI实现,C/C++
- 《水果忍者》设计报告.doc
- MFC实现的红绿灯程序
- Win32简易画图程序
- C++网络爬虫项目
- 泡泡堂(炸弹人)小游戏C/C++完整源码
- 使用C/C++读取BITMAP的内容
- VC图像处理-用Canny算子提取边缘
- C/C++视频教程
- 个人总结的一些C/C++编码规范
- 高斯消去法求解线性方程组C/C++程序输
- celrityC/C++源码查看工具
- 网络编程MFC 实验四 FTP客户端功能实现
- C/C++语言大作业、小游戏
- 完整的C/C++时序的B+树数据库系统实现
- Eclipse C/C++ 自动补全的cdt补丁
- MongoDB C/C++开发使用案例Demo
- C/C++使用WinIO读取CMOS数据代码
- Diab C/C++ Compiler for PowerPC
评论
共有 条评论