纯真版IP数据库查询代码
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
//纯真版IP数据库检索程序 //code by davies //patch by Julien Woo #include <stdio.h> #include <stdlib.h> #include <string> #include <sstream> #include <iostream> #include <fstream> #include <strstream> using namespace std; typedef unsigned int IP_TYPE; const int IP_SIZE = 4; const int OFFSET_SIZE = 3; const int INDEX_RECORD_SIZE = IP_SIZE + OFFSET_SIZE; class IpLocater{ private: FILE* dbfile; int first_index; int last_index; enum {REDIRECT_MODE_1 = 0x01,REDIRECT_MODE_2 = 0x02}; protected: public: IpLocater( const string dbfilename = "QQWry.Dat" ) { dbfile = fopen(dbfilename.c_str(),"rb"); if( !dbfile ){ printf("can not open the ip db file %s\n",dbfilename.c_str()); exit(0); } fread(&first_index,sizeof(int),1,dbfile); fread(&last_index,sizeof(int),1,dbfile); } ~IpLocater(){ fclose(dbfile); } int getRecordCount() const { return (last_index - first_index ) / INDEX_RECORD_SIZE + 1; } string readString(const int offset = 0){ if ( offset ) { fseek(dbfile,offset,SEEK_SET); } char ch = fgetc(dbfile); ostringstream sstr ; while ( ch != 0 && ch != EOF ) { sstr << ch; ch = fgetc(dbfile); } return sstr.str(); } inline int readInt3(const int offset = 0 ){ if ( offset ) { fseek(dbfile,offset,SEEK_SET); } int result = 0; fread(&result,sizeof(char),3,dbfile); return result; } string readAreaAddr(const int offset = 0){ if ( offset ) { fseek(dbfile,offset,SEEK_SET); } char b = fgetc(dbfile); if ( b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) { int areaOffset=0; fread(&areaOffset,1,3,dbfile); if ( areaOffset ) { return readString( areaOffset ); }else{ return "Unkown"; } }else{ fseek(dbfile,-1,SEEK_CUR); return readString(); } } unsigned int readLastIp(const int offset){ fseek(dbfile,offset,SEEK_SET); unsigned int ip = 0; fread(&ip,sizeof(unsigned int),1,dbfile); return ip; } string readFullAddr(const int offset,int ip = 0 ){ string address = ""; fseek(dbfile,offset+4,SEEK_SET); char ch = fgetc(dbfile); if ( ch == REDIRECT_MODE_1 ) { // //cout<<"REDIRECT_MODE_1"; int countryOffset = 0; fread(&countryOffset,sizeof(char),3,dbfile); fseek(dbfile,countryOffset,SEEK_SET); char byte = fgetc(dbfile); if ( byte == REDIRECT_MODE_2 ) { int p = 0; fread(&p,1,3,dbfile); address = readString(p); fseek(dbfile,countryOffset+4,SEEK_SET); }else{ address = readString(countryOffset); } address += " "; address += readAreaAddr(); // current position }else if ( ch == REDIRECT_MODE_2 ) { //cout<<"REDIRECT_MODE_2"; int p = 0; fread(&p,1,3,dbfile); address = readString(p); address += " "; address += readAreaAddr( offset + 8); } else{ fseek(dbfile,-1,SEEK_CUR); address = readString(); address += " "; address += readAreaAddr(); } return address; } void printAddr(int first = 0, int count = 100){ int record_count = getRecordCount(); for( int i = first; i< first+count && i<record_count; i++ ) { fseek(dbfile,i*INDEX_RECORD_SIZE+first_index,SEEK_SET); unsigned int ip = 0; fread(&ip,sizeof(unsigned int),1,dbfile); int offset = 0; fread(&offset,1,OFFSET_SIZE,dbfile); cout << ip2string( ip ) << "-" << ip2string( readLastIp(offset) ) << readFullAddr( offset ) << endl; } } int find(unsigned int ip,int left ,int right){ if ( right-left == 1) { return left; }else{ int middle = (left + right) / 2; int offset = first_index + middle * INDEX_RECORD_SIZE; fseek(dbfile,offset,SEEK_SET); unsigned int new_ip = 0; fread(&new_ip,sizeof(unsigned int),1,dbfile); if ( ip >= new_ip ) { return find(ip,middle,right); }else{ return find(ip,left,middle); } } } string getIpAddr( unsigned int ip){ int index = find(ip,0, getRecordCount() - 1 ); int index_offset = first_index + index * INDEX_RECORD_SIZE + 4; int addr_offset = 0; fseek(dbfile,index_offset,SEEK_SET); fread(&addr_offset,1,3,dbfile); string address = readFullAddr( addr_offset,ip ); return address; } string ip2string( unsigned int ip) const{ ostringstream sstr; sstr << ((ip & 0xff000000)>>24) ; sstr << "." << ((ip & 0xff0000)>>16); sstr << "." << ((ip & 0xff00)>>8); sstr << "." << (ip & 0xff); return sstr.str(); } unsigned int string2ip(const string ipstr)const{ string str = ipstr; unsigned int ip = 0; int p = 0; p = str.find("."); ip += atoi(str.substr(0,p).c_str()); ip <<= 8; str = str.substr(p+1,str.length()); p = str.find("."); ip += atoi(str.substr(0,p).c_str()); ip <<= 8; str = str.substr(p+1,str.length()); p = str.find("."); ip += atoi(str.substr(0,p).c_str()); ip <<= 8; ip += atoi(str.substr(p+1,str.length()).c_str()); return ip; } string getIpAddr(string ip){ return getIpAddr( string2ip( ip ) ); } }; int main(int argc, char* argv[]) { IpLocater locater("D:\\Dev\\ipseek\\QQWry.Dat"); ifstream input("ip.txt",ios::in); string str; ofstream output("out.txt",ios::out); while (!input.eof()) { getline(input,str); cout << str << " " << locater.getIpAddr( str ) << endl; output << str << " " << locater.getIpAddr( str ) << endl; } input.close(); output.close(); return 0; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=619