關于 純真IP數據庫格式,詳細見下面帖子:
http://blog.chinaunix.net/u1/41420/showart_322320.html
程序說明:能夠根據輸入的IP,在 純真IP數據庫 中,搜索并且讀取對應的 物理地址,還可以導出所有的IP段地址信息。
#include <stdio.h>
#include <string.h>
#include <process.h>
#define QQWRY "QQWry.dat"
#define REDIRECT_MODE_1 0x01
#define REDIRECT_MODE_2 0x02
#define MAXBUF 255
/*unsigned long getValue( 獲取文件中指定的16進制串的值,并返回
FILE *fp, 指定文件指針
unsigned long start, 指定文件偏移量
int length) 獲取的16進制字符個數/長度
*/
unsigned long getValue(FILE *fp, unsigned long start, int length)
{
unsigned long variable=0;
long i;
long *val = new long[length];
fseek(fp,start,SEEK_SET);
for(i=0;i<length;i++)
{
/*過濾高位,一次讀取一個字符*/
val[i]=fgetc(fp)&0x000000FF;
}
for(i=length-1;i>=0;i--)
{
/*因為讀取多個16進制字符,疊加*/
variable=variable*0x100+val[i];
}
if(val!=NULL)
{
delete val;
val = NULL;
}
return variable;
};
/*int getString( 獲取文件中指定的字符串,返回字符串長度
FILE *fp, 指定文件指針
unsigned long start, 指定文件偏移量
char **string) 用來存放將讀取字符串的字符串空間的首地址
*/
int getString(FILE *fp, unsigned long start, char **string)
{
unsigned long i=0;
char val;
fseek(fp,start,SEEK_SET);
/*讀取字符串,直到遇到0x00為止*/
do
{
val=fgetc(fp);
/*依次放入用來存儲的字符串空間中*/
*(*string+i)=val;
i++;
}while(val!=0x00);
/*返回字符串長度*/
return i;
};
/*void getAddress( 讀取指定IP的國家位置和地域位置
FILE *fp, 指定文件指針
unsigned long start, 指定IP在索引中的文件偏移量
char **country, 用來存放國家位置的字符串空間的首地址
char **location) 用來存放地域位置的字符串空間的首地址
*/
void getAddress(FILE *fp, unsigned long start, char **country, char **location)
{
unsigned long redirect_address,counrty_address,location_address;
char val;
start+=4;
fseek(fp,start,SEEK_SET);
/*讀取首地址的值*/
val=(fgetc(fp)&0x000000FF);
if(val==REDIRECT_MODE_1)
{
/*重定向1類型的*/
redirect_address=getValue(fp,start+1,3);
fseek(fp,redirect_address,SEEK_SET);
/*混合類型,重定向1類型進入后遇到重定向2類型
讀取重定向后的內容,并設置地域位置的文件偏移量*/
if((fgetc(fp)&0x000000FF)==REDIRECT_MODE_2)
{
counrty_address=getValue(fp,redirect_address+1,3);
location_address=redirect_address+4;
getString(fp,counrty_address,country);
}
/*讀取重定向1后的內容,并設置地域位置的文件偏移量*/
else
{
counrty_address=redirect_address;
location_address=redirect_address+getString(fp,counrty_address,country);
}
}
/*重定向2類型的*/
else if(val==REDIRECT_MODE_2)
{
counrty_address=getValue(fp,start+1,3);
location_address=start+4;
getString(fp,counrty_address,country);
}
else
{
counrty_address=start;
location_address=counrty_address+getString(fp,counrty_address,country);
}
/*讀取地域位置*/
fseek(fp,location_address,SEEK_SET);
if((fgetc(fp)&0x000000FF)==REDIRECT_MODE_2||(fgetc(fp)&0x000000FF)==REDIRECT_MODE_1)
{
location_address=getValue(fp,location_address+1,3);
}
getString(fp,location_address,location);
return;
};
/*void getHead( 讀取索引部分的范圍(在文件頭中,最先的2個8位16進制)
FILE *fp, 指定文件指針
unsigned long *start, 文件偏移量,索引的起止位置
unsigned long *end) 文件偏移量,索引的結束位置
*/
void getHead(FILE *fp,unsigned long *start,unsigned long *end)
{
/*索引的起止位置的文件偏移量,存儲在文件頭中的前8個16進制中
設置偏移量為0,讀取4個字符*/
*start=getValue(fp,0L,4);
/*索引的結束位置的文件偏移量,存儲在文件頭中的第8個到第15個的16進制中
設置偏移量為4個字符,再讀取4個字符*/
*end=getValue(fp,4L,4);
};
/*unsigned long searchIP( 搜索指定IP在索引區的位置,采用二分查找法;
返回IP在索引區域的文件偏移量
一條索引記錄的結果是,前4個16進制表示起始IP地址
后面3個16進制,表示該起始IP在IP信息段中的位置,文件偏移量
FILE *fp,
unsigned long index_start, 索引起始位置的文件偏移量
unsigned long index_end, 索引結束位置的文件偏移量
unsigned long ip) 關鍵字,要索引的IP
*/
unsigned long searchIP(FILE *fp, unsigned long index_start, \
unsigned long index_end, unsigned long ip)
{
unsigned long index_current,index_top,index_bottom;
unsigned long record;
index_bottom=index_start;
index_top=index_end;
/*此處的7,是因為一條索引記錄的長度是7*/
index_current=((index_top-index_bottom)/7/2)*7+index_bottom;
/*二分查找法*/
do{
record=getValue(fp,index_current,4);
if(record>ip)
{
index_top=index_current;
index_current=((index_top-index_bottom)/14)*7+index_bottom;
}
else
{
index_bottom=index_current;
index_current=((index_top-index_bottom)/14)*7+index_bottom;
}
}while(index_bottom<index_current);
/*返回關鍵字IP在索引區域的文件偏移量*/
return index_current;
};
/*unsigned long putAll( 導出所有IP信息到文件文件中,函數返回導出總條數
FILE *fp,
FILE *out, 導出的文件指針,必須擁有寫權限
unsigned long index_start, 索引區域的起始文件偏移量
unsigned long index_end) 索引區域的結束文件偏移量
*/
unsigned long putAll(FILE *fp, FILE *out, unsigned long index_start, unsigned long index_end)
{
unsigned long i,count=0;
unsigned long start_ip,end_ip;
char *country;
char *location;
country=new char[255];
location=new char[255];
/*此處的7,是因為一條索引記錄的長度是7*/
for(i=index_start;i<index_end;i+=7)
{
/*獲取IP段的起始IP和結束IP,
起始IP為索引部分的前4位16進制
結束IP在IP信息部分的前4位16進制中,靠索引部分指定的偏移量找尋*/
start_ip=getValue(fp,i,4);
end_ip=getValue(fp,getValue(fp,i+4,3),4);
/*導出IP信息,格式是
起始IP\t結束IP\t國家位置\t地域位置\n*/
fprintf(out,"%d.%d.%d.%d",(start_ip&0xFF000000)>>0x18,\
(start_ip&0x00FF0000)>>0x10,(start_ip&0x0000FF00)>>0x8,start_ip&0x000000FF);
fprintf(out,"\t");
fprintf(out,"%d.%d.%d.%d",(end_ip&0xFF000000)>>0x18, \
(end_ip&0x00FF0000)>>0x10,(end_ip&0x0000FF00)>>0x8,end_ip&0x000000FF);
getAddress(fp,getValue(fp,i+4,3),&country,&location);
fprintf(out,"\t%s\t%s\n",country,location);
count++;
}
if(country!=NULL)
{
delete country;
country = NULL;
}
if(location!=NULL)
{
delete location;
location = NULL;
}
/*返回導出總條數*/
return count;
};
/*判斷一個字符是否為數字字符,
如果是,返回0
如果不是,返回1*/
int beNumber(char c)
{
if(c>='0'&&c<='9')
return 0;
else
return 1;
};
/*函數的參數是一個存儲著IP地址的字符串首地址
返回該IP的16進制代碼
如果輸入的IP地址有錯誤,函數將返回0*/
unsigned long getIP(char *ip_addr)
{
unsigned long ip=0;
int i,j=0;
/*依次讀取字符串中的各個字符*/
for(i=0;i<strlen(ip_addr);i++)
{
/*如果是IP地址間隔的‘.’符號
把當前讀取到的IP字段的值,存入ip變量中
(注意,ip為疊加時,乘以16進制的0x100)
并清除臨時變量的值*/
if(*(ip_addr+i)=='.')
{
ip=ip*0x100+j;
j=0;
}
/*往臨時變量中寫入當前讀取到的IP字段中的字符值
疊加乘以10,因為輸入的IP地址是10進制*/
else
{
/*判斷,如果輸入的IP地址不規范,不是10進制字符
函數將返回0*/
if(beNumber(*(ip_addr+i))==0)
j=j*10+*(ip_addr+i)-'0';
else
return 0;
}
}
/*IP字段有4個,但是‘.’只有3個,疊加第四個字段值*/
ip=ip*0x100+j;
return ip;
};
/*顯示logo信息*/
void logo(void)
{
printf("=============================================================================\n");
printf("--- Get the IP info.s from QQWry.dat v0.1 by dorainm dorainm@gmail.com ---\n");
printf("=============================================================================\n");
};
/*顯示程序語法*/
void usage(char *app_name)
{
printf("\nUsage : %s [options]\n",app_name);
printf("options:\n");
printf(" -a <address> Search and display the Informations by Location Address.(*)\n");
printf(" -i <IP> Search and display the Informations by IP Address.\n");
printf(" -o <FILE> Output all the informations to a text file.\n");
printf(" -local Display the localhost IP's informations.(*)\n");
printf(" -updata Update the QQWry.dat from the Internet.(*)\n\n");
printf("ps: the optionss marked (*) are incompleted.\n");
};
/*顯示結束信息*/
void showend(void)
{
printf("\n\nThe command completed successfully.\n\n");
};
/*主函數*/
int main(int argc, char *argv[])
{
FILE *fp; /*打開QQWry.dat的文件指針*/
unsigned long index_start,index_end,current; /*索引部分的起始位置的文件偏移量
索引部分的結束位置的文件偏移量
待搜索IP地址的索引條目的文件偏移量*/
char *country; /*國家位置*/
char *location; /*地域位置*/
country=new char[MAXBUF];
location=new char[MAXBUF];
logo();
if(argc<3)
{
usage(argv[0]);
showend();
return 1;
}
/*打開QQWry.dat文件*/
if((fp=fopen(QQWRY,"rb"))==NULL)
{
printf("[-] Error : Can not open the file %s.\n",QQWRY);
showend();
return 2;
}
else
printf("[+] Open the file [ %s ] successfully.\n",QQWRY);
/*顯示QQWry.dat文件信息*/
getHead(fp,&index_start,&index_end);
getAddress(fp,getValue(fp,index_end+4,3),&country,&location);
printf("[+] Version of QQWry.dat : [ %s %s ]\n",country,location);
printf("[+] Index Location [ 0x%X - 0x%X ].\n",index_start,index_end);
/*判斷第一個參數的值*/
if((strncmp(argv[1],"-i",2)==0)||(strncmp(argv[1],"-I",2)==0))
{
/*-i參數,搜索IP*/
unsigned long ip;
ip=getIP(argv[2]);
if(ip==0)
{
printf("[-] Error : the IP Address inputed.\n");
showend();
return 3;
}
/*搜索IP在索引區域的條目的偏移量*/
current=searchIP(fp,index_start,index_end,ip);
printf("[+] Address of index for [ %X ] is %X\n",ip,current);
/*獲取該IP對因的國家地址和地域地址*/
getAddress(fp,getValue(fp,current+4,3),&country,&location);
printf("[+] Get the location for the IP address.\n");
printf("[+] [ IP Address ] %d.%d.%d.%d\n",(ip&0xFF000000)>>0x18,(ip&0x00FF0000)>>0x10,(ip&0x0000FF00)>>0x8,ip&0x000000FF);
printf("[+] [ Location ] %s %s\n",country,location);
}
else if((strncmp(argv[1],"-o",2)==0)||(strncmp(argv[1],"-O",2)==0))
{
/*-o參數,解壓縮數據庫,導出IP信息到文本文件*/
FILE *out;
unsigned long num;
if((out=fopen(argv[2],"w"))==NULL)
{
printf("[-] Error create the output text file [ %s ].\n","out.txt");
showend();
}
else
{
printf("[+] Create the output text file [ %s ] successfully.\n","out.txt");
}
/*導出IP條目信息*/
printf("[+] Outputing the informations
");
num=putAll(fp,out,index_start,index_end);
printf("Finished.\n");
fclose(out);
/*顯示導出條目的數量*/
printf("[+] The Total items number is [ %d ].",num);
}
/*關閉文件指針,釋放變量空間,結束程序*/
fclose(fp);
if(country!=NULL)
{
delete country;
country = NULL;
}
if(location!=NULL)
{
delete location;
location = NULL;
}
showend();
return 0;
}
使用語法
============================================================================= --- Get the IP info.s from QQWry.dat v0.1 by dorainm dorainm@gmail.com --- =============================================================================
Usage : showip [options] options: -a <address> Search and display the Informations by Location Address.(*) -i <IP> Search and display the Informations by IP Address. -o <FILE> Output all the informations to a text file. -local Display the localhost IP's informations.(*) -updata Update the QQWry.dat from the Internet.(*)
ps: the optionss marked (*) are incompleted.
The command completed successfully. |
搜索IP
showip -i 222.19.211.254
============================================================================= --- Get the IP info.s from QQWry.dat v0.1 by dorainm dorainm@gmail.com --- ============================================================================= [+] Open the file [ QQWry.dat ] successfully. [+] Version of QQWry.dat : [ 純真網絡 2006年3月5日IP數據 ] [+] Index Location [ 0x37A265 - 0x535EB1 ]. [+] Address of index for [ DE13D3FE ] is 51BB44 [+] Get the location for the IP address. [+] [ IP Address ] 222.19.211.254 [+] [ Location ] 云南大學 國家示范性軟件學院
The command completed successfully. |
導出所有IP信息,語法是 showip -o out.txt