vc实现纯真IP数据库显物理位置

转自:不详

纯真IP数据库 可到纯真论坛下载:www.cz88.net

//IpBase.h
#include<fstream> 
#include <string>
using namespace std; 

class CIpBase
{
public :
	CIpBase(string fileName="QQWry.Dat");
	~CIpBase();
	string Find(unsigned long ip);
	string Find(string ip);
	bool operator !();
	bool operator ==(bool flag);
	unsigned long GetRange(){return lastIp;}
private:
	unsigned char *buf;
	unsigned long lastIp;
	bool bSuccess;
    struct head{unsigned long first,last;}*Head; 
	struct Taddr{unsigned long ip;string state,area;}; 
	struct ipMsg{unsigned long ip;unsigned char ipAddr[3];}; 
	int hlen;
};
bool CIpBase::operator ==(bool flag)
{
	return flag==bSuccess;
}
bool CIpBase::operator !()
{
	return !bSuccess;
}
CIpBase::~CIpBase()
{
	if(buf)delete buf;
}
CIpBase::CIpBase(string fileName)
{
	ifstream file(&fileName[0],ios::binary); 
	if(!file){bSuccess=false;return ;} 
	Head=new head;
	file.read((char*)Head,sizeof(head));
	unsigned char *temp=(unsigned char *)Head;
	Head->first=(temp[3]* 0x1000000)+(temp[2]* 0x10000)+(temp[1]* 0x100)+temp[0];
	Head->last=(temp[7]* 0x1000000)+(temp[6]* 0x10000)+(temp[5]* 0x100)+temp[4];
	int bufLen=Head->last-sizeof(head);
	hlen=sizeof(head); 
	buf=new unsigned char [bufLen]; 
	if(!buf){bSuccess=false;return ;}
	file.read((char *)buf,bufLen); 
	file.close();
	bSuccess=true;
}
string CIpBase::Find(string ip)
{
	unsigned long a=0,b=0,c=0,d=0;
	int i=0;
	int count=0;
	while(i<ip.size()){if((ip[i]>'9'||ip[i]<0)&&ip[i]!='.')return "不合规则的ip地址";if(ip[i]=='.')count++;i++;}
	if(count!=3)return "不能识别该ip";
	i=0;
	while(ip[i]!='.'&&i<ip.size()){a*=10;a+=ip[i]-'0';i++;}
	i++;
	while(ip[i]!='.'&&i<ip.size()){b*=10;b+=ip[i]-'0';i++;}
	i++;
	while(ip[i]!='.'&&i<ip.size()){c*=10;c+=ip[i]-'0';i++;}
	i++;
	while(i<ip.size()){d*=10;d+=ip[i]-'0';i++;}
	return Find(a*0x1000000+b*0x10000+c*0x100+d);
}
string CIpBase::Find(unsigned long ip)
{
	unsigned char *first=buf+Head->first-hlen; 
	unsigned char *last=buf+Head->last-hlen; 
	if(((ipMsg *)first)->ip>ip||((ipMsg *)last)->ip<ip)return "异常ip";
	unsigned char *mid=last;
	while(first!=last&&last-first>7)
	{
		mid=(last-first+7)/2/7*7+first;
		ipMsg *t=(ipMsg *)mid;
		if(t->ip<ip)first=mid;
		else last=mid;
	}
	unsigned char *addr=first;
	ipMsg * temp=(ipMsg *)addr;
	
	unsigned long  caddr=(temp->ipAddr[2]* 0x10000)+(temp->ipAddr[1]* 0x100 )+temp->ipAddr[0];     
	unsigned char *record=buf+caddr-hlen;    
	unsigned char *tip=(unsigned char *)record;
	lastIp=tip[3]*0x1000000+tip[2]*0x10000+tip[1]*0x100+tip[0];
	Taddr Msg;
	Msg.ip=lastIp; 
	unsigned char *state,*area; 
	if(record[4]!=1&&record[4]!=2)
	{
		state=&record[4];
		Msg.state=(char *)state;
		area= &record[4]+strlen((char *)&record[4]);
		Msg.area=(char *)area; 
	}else 
		if(record[4]==2) 
		{
			int caddr=(record[7]*0x10000)+(record[6]*0x100)+record[5];   
			state= buf+caddr-hlen;
			Msg.state=(char *)state;
			if(record[8]!=1&&record[8]!=2)
			{
				area= &record[8];
				Msg.area=(char *)area;
			}
			else
			{
				int caddr=(record[11]*0x10000)+(record[10]*0x100)+record[9];
				area= buf+caddr-hlen;
				Msg.state=(char *)area;
			}
		}else
		{
			int caddr=(record[7]*0x10000)+(record[6]*0x100)+record[5];    
			unsigned char *record1=buf+caddr-hlen;
			unsigned char *record2; 
			if(record1[0]!=1&&record1[0]!=2)
			{
				state=record1;
				Msg.state=(char *)state; 
				record2=record1+strlen((char *)record1);
			}else
			{
				int caddr=(record1[3]*0x10000)+(record1[2]*0x100)+record1[1]; 
				state=buf+caddr-hlen;
				Msg.state=(char *)state; 
				record2=record1+4;
			}
			if(record2[0]!=1&&record2[0]!=2)
			{
				area=record2;
				Msg.area=(char *)area;    
			}else
			{
				int caddr=(record1[3]*0x10000)+(record1[2]*0x100)+record1[1]; 
				area= buf+caddr-hlen;
				Msg.area=(char *)area;
			} 
		} 
		return Msg.state+Msg.area;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值