資源簡介
封裝在一個類里的可以直接運行的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
評論
共有 條評論