摘要
網(wǎng)絡上的IP數(shù)據(jù)庫以純真版的最為流行,LumaQQ也采用了純真版IP數(shù)據(jù)庫做為IP查詢功能的基礎。不過關于其格式的文檔卻非常之少,后來終于在網(wǎng)上
找到了一份文檔,得以了解其內幕,不過那份文檔寥寥數(shù)語,也是頗為耐心才讀明白。在這里我重寫一份,以此做為LumaQQ開發(fā)者文檔的一部分,我想還是必
要的。本文詳細介紹了純真IP數(shù)據(jù)庫的格式,并且給出了一些Demo以供參考。
Luma, 清華大學
修改日期: 2005/01/14
Note: 在此感謝純真IP數(shù)據(jù)庫作者金狐和那唯一一份文檔的作者。
修改歷史:
2005-01-14 修改了原來一些表達不清和錯誤的地方
自從有了IP數(shù)據(jù)庫這種東西,QQ外掛的顯示IP功能也隨之而生,本人見識頗窄,是否還有其他應用不得而知,不過,IP數(shù)據(jù)庫確實是個不錯的東西。
如今網(wǎng)絡上最流行的IP數(shù)據(jù)庫我想應該是純真版的(說錯了也不要扁我),迄今為止其IP記錄條數(shù)已經(jīng)接近30000,對于有些IP甚至能精確到樓層,不亦
快哉。2004年4、5月間,正逢LumaQQ破土動工,為了加上這個人人都喜歡,但是好像人人都不知道為什么喜歡的顯IP功能,我也采用了純真版IP數(shù)
據(jù)庫,它的優(yōu)點是記錄多,查詢速度快,它只用一個文件QQWry.dat就包含了所有記錄,方便嵌入到其他程序中,也方便升級。
基本結構
QQWry.dat文件在結構上分為3塊:文件頭,記錄區(qū),索引區(qū)。一般我們要查找IP時,先在索引區(qū)查找記錄偏移,然后再到記錄區(qū)讀出信息。由于
記錄區(qū)的記錄是不定長的,所以直接在記錄區(qū)中搜索是不可能的。由于記錄數(shù)比較多,如果我們遍歷索引區(qū)也會是有點慢的,一般來說,我們可以用二分查找法搜索
索引區(qū),其速度比遍歷索引區(qū)快若干數(shù)量級。圖1是QQWry.dat的文件結構圖。
圖1. QQWry.dat文件結構
要注意的是,QQWry.dat里面全部采用了little-endian字節(jié)序
一. 了解文件頭
QQWry.dat的文件頭只有8個字節(jié),其結構非常簡單,首四個字節(jié)是第一條索引的絕對偏移,后四個字節(jié)是最后一條索引的絕對偏移。
二. 了解記錄區(qū)
每條IP記錄都由國家和地區(qū)名組成,國家地區(qū)在這里并不是太確切,因為可能會查出來“清華大學計算機系”之類的,這里清華大學就成了國家名了,所以
這個國家地區(qū)名和IP數(shù)據(jù)庫制作的時候有關系。所以記錄的格式有點像QName,有一個全局部分和局部部分組成,我們這里還是沿用國家名和地區(qū)名的說法。
于是我們想象著一條記錄的格式應該是:
[IP地址][國家名][地區(qū)名],當然,這個沒有什么問題,但是這只是最簡單的情況。很顯然,國家名和地區(qū)名可能會有很多的重復,如果每條記錄都保存一
個完整的名稱拷貝是非常不理想的,所以我們就需要重定向以節(jié)省空間。所以為了得到一個國家名或者地區(qū)名,我們就有了兩個可能:第一就是直接的字符串表示的
國家名,第二就是一個4字節(jié)的結構,第一個字節(jié)表明了重定向的模式,后面3個字節(jié)是國家名或者地區(qū)名的實際偏移位置。對于國家名來說,情況還可能更復雜
些,因為這樣的重定向最多可能有兩次。
那么什么是重定向模式?根據(jù)上面所說,一條記錄的格式是[IP地址][國家記錄][地區(qū)記錄],如果國家記錄是重定向的話,那么地區(qū)記錄是有可能沒有的,于是就有了兩種情況,我管他叫做模式1和模式2。我們對這些格式的情況舉圖說明:
圖2. IP記錄的最簡單形式
圖2表示了最簡單的IP記錄格式,我想沒有什么可以解釋的
圖3. 重定向模式1
圖3演示了重定向模式1的情況。我們看到在模式1的情況下,地區(qū)記錄也跟著國家記錄走了,在IP地址之后只剩下了國家記錄的4字節(jié),后面3個字節(jié)構成了一個指針,指向了實際的國家名,然后又跟著地址名。模式1的標識字節(jié)是0x01。
圖4. 重定向模式2
圖4演示了重定向模式2的情況。我們看到了在模式2的情況下(其標識字節(jié)是0x02),地區(qū)記錄沒有跟著國家記錄走,因此在國家記錄之后4個字節(jié)之
后還是有地區(qū)記錄。我想你已經(jīng)明白了模式1和模式2的區(qū)別,即:模式1的國家記錄后面不會再有地區(qū)記錄,模式2的國家記錄后會有地區(qū)記錄。下面我們來看一
下更復雜的情況。
圖5. 混和情況1
圖5演示了當國家記錄為模式1的時候可能出現(xiàn)的更復雜情況,在這種情況下,重定向指向的位置仍然是個重定向,不過第二次重定向為模式2。大家不用擔
心,沒有模式3了,這個重定向也最多只有兩次,并且如果發(fā)生了第二次重定向,則其一定為模式2,而且這種情況只會發(fā)生在國家記錄上,對于地區(qū)記錄,模式1
和模式2是一樣的,地區(qū)記錄也不會發(fā)生2次重定向。不過,這個圖還可以更復雜,如圖7:
圖6. 混和情況2
圖6是模式1下最復雜的混和情況,不過我想應該也很好理解,只不過地區(qū)記錄也來重定向而已,有一點我要提醒你,如果重定向的地址是0,則表示未知的地區(qū)名。
所以我們總結如下:一條IP記錄由[IP地址][國家記錄][地區(qū)記錄]組成,對于國家記錄,可以有三種表示方式:字符串形式,重定向模式1和重定
向模式2。對于地區(qū)記錄,可以有兩種表示方式:字符串形式和重定向,另外有一條規(guī)則:重定向模式1的國家記錄后不能跟地區(qū)記錄。按照這個總結,在這些方式
中合理組合,就構成了IP記錄的所有可能情況。
設計的理由
在我們繼續(xù)去了解索引區(qū)的結構之前,我們先來了解一下為何記錄區(qū)的結構要如此設計。我想你可能想到了答案:字符串重用。沒錯,在這種結構下,對于一
個國家名和地區(qū)名,我只需要保存其一次就可以了。我們舉例說明,為了表示方便,我們用小寫字母代表IP記錄,C表示國家名,A表示地區(qū)名:
- 有兩條記錄a(C1, A1), b(C2, A2),如果C1 = C2, A1 = A2,那么我們就可以使用圖3顯示的結構來實現(xiàn)重用
- 有三條記錄a(C1, A1), b(C2, A2), c(C3, A3),如果C1 = C2, A2 = A3,現(xiàn)在我們想存儲記錄b,那么我們可以用圖6的結構來實現(xiàn)重用
- 有兩條記錄a(C1, A1), b(C2, A2),如果C1 = C2,現(xiàn)在我們想存儲記錄b,那么我們可以采用模式2表示C2,用字符串表示A2
你可以舉出更多的情況,你也會發(fā)現(xiàn)在這種結構下,不同的字符串只需要存儲一次。
了解索引區(qū)
在"了解文件頭"部分,我們說明了文件頭實際上是兩個指針,分別指向了第一條索引和最后一條索引的絕對偏移。如圖8所示:
圖8. 文件頭指向索引區(qū)圖示
實在是很簡單,不是嗎?從文件頭你就可以定位到索引區(qū),然后你就可以開始搜索IP了!每條索引長度為7個字節(jié),前4個字節(jié)是起始IP地址,后三個字
節(jié)就指向了IP記錄。這里有些概念需要說明一下,什么是起始IP,那么有沒有結束IP? 假設有這么一條記錄:166.111.0.0 -
166.111.255.255,那么166.111.0.0就是起始IP,166.111.255.255就是結束IP,結束IP就是IP記錄中的那頭
4個字節(jié),這下你應該就清楚了吧。于是乎,每條索引配合一條記錄,構成了一個IP范圍,如果你要查找166.111.138.138所在的位置,你就會發(fā)
現(xiàn)166.111.138.138落在了166.111.0.0 - 166.111.255.255
這個范圍內,那么你就可以順著這條索引去讀取國家和地區(qū)名了。那么我們給出一個最詳細的圖解吧:
圖9. 文件詳細結構
現(xiàn)在一切都清楚了是不是?也許還有一點你不清楚,QQWry.dat的版本信息存在哪里呢?
答案是:最后一條IP記錄實際上就是版本信息,最后一條記錄顯示出來就是這樣:255.255.255.0 255.255.255.255
純真網(wǎng)絡 2004年6月25日IP數(shù)據(jù)。OK,到現(xiàn)在你應該全部清楚了。
Demo
下一步:我給出一個讀取IP記錄的程序片斷,此片斷摘錄自LumaQQ源文件edu.tsinghua.lumaqq.IPSeeker.java,如果你有興趣,可以下載源代碼詳細看看。
/**
* 給定一個ip國家地區(qū)記錄的偏移,返回一個IPLocation結構
* @param offset 國家記錄的起始偏移
* @return IPLocation對象
*/
private IPLocation getIPLocation(long offset) {
try {
// 跳過4字節(jié)ip
ipFile.seek(offset + 4);
// 讀取第一個字節(jié)判斷是否標志字節(jié)
byte b = ipFile.readByte();
if(b == REDIRECT_MODE_1) {
// 讀取國家偏移
long countryOffset = readLong3();
// 跳轉至偏移處
ipFile.seek(countryOffset);
// 再檢查一次標志字節(jié),因為這個時候這個地方仍然可能是個重定向
b = ipFile.readByte();
if(b == REDIRECT_MODE_2) {
loc.country = readString(readLong3());
ipFile.seek(countryOffset + 4);
} else
loc.country = readString(countryOffset);
// 讀取地區(qū)標志
loc.area = readArea(ipFile.getFilePointer());
} else if(b == REDIRECT_MODE_2) {
loc.country = readString(readLong3());
loc.area = readArea(offset + 8);
} else {
loc.country = readString(ipFile.getFilePointer() - 1);
loc.area = readArea(ipFile.getFilePointer());
}
return loc;
} catch (IOException e) {
return null;
}
}
/**
* 從offset偏移開始解析后面的字節(jié),讀出一個地區(qū)名
* @param offset 地區(qū)記錄的起始偏移
* @return 地區(qū)名字符串
* @throws IOException 地區(qū)名字符串
*/
private String readArea(long offset) throws IOException {
ipFile.seek(offset);
byte b = ipFile.readByte();
if(b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) {
long areaOffset = readLong3(offset + 1);
if(areaOffset == 0)
return LumaQQ.getString("unknown.area");
else
return readString(areaOffset);
} else
return readString(offset);
}
/**
* 從offset位置讀取3個字節(jié)為一個long,因為java為big-endian格式,所以沒辦法
* 用了這么一個函數(shù)來做轉換
* @param offset 整數(shù)的起始偏移
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong3(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從當前位置讀取3個字節(jié)轉換成long
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong3() {
long ret = 0;
try {
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從offset偏移處讀取一個以0結束的字符串
* @param offset 字符串起始偏移
* @return 讀取的字符串,出錯返回空字符串
*/
private String readString(long offset) {
try {
ipFile.seek(offset);
int i;
for(i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] = ipFile.readByte());
if(i != 0)
return Utils.getString(buf, 0, i, "GBK");
} catch (IOException e) {
log.error(e.getMessage());
}
return "";
}
代碼并不復雜,getIPLocation是主要方法,它檢查國家記錄格式,并針對字符串形式,模式1,模式2采用不同的代碼,readArea則相對簡單,因為只有字符串和重定向兩種情況需要處理。
總結
純真IP數(shù)據(jù)庫的結構使得查找IP簡單迅速,不過你想要編輯它卻是比較麻煩的,我想應該需要專門的工具來生成QQWry.dat文件,由于其文件格式的限制,你要直接添加IP記錄就不容易了。不過,能查到IP已經(jīng)很開心了,希望純真記錄越來越多~。
解析的主類
-
package
?com.showtime.IPparse;??
-
??
-
import
?java.io.File;??
-
import
?java.io.FileNotFoundException;??
-
import
?java.io.IOException;??
-
import
?java.io.RandomAccessFile;??
-
import
?java.nio.ByteOrder;??
-
import
?java.nio.MappedByteBuffer;??
-
import
?java.nio.channels.FileChannel;??
-
import
?java.util.ArrayList;??
-
import
?java.util.HashMap;??
-
import
?java.util.List;??
-
import
?java.util.Map;??
-
import
?com.showtime.util.LogFactory;??
-
import
?org.apache.log4j.Level;??
-
??
-
public
?
class
?IPSeeker?{??
-
??????
-
????private?String?IP_FILE="QQWry.Dat";??
-
??????
-
????private?String?INSTALL_DIR="f:/qqwry";??
-
??????
-
??????
-
??????
-
????private?static?final?int?IP_RECORD_LENGTH?=?7;??
-
????private?static?final?byte?REDIRECT_MODE_1?=?0x01;??
-
????private?static?final?byte?REDIRECT_MODE_2?=?0x02;??
-
??????
-
??????
-
????private?Map<String,?IPLocation>?ipCache;??
-
??????
-
????private?RandomAccessFile?ipFile;??
-
??????
-
????private?MappedByteBuffer?mbb;??
-
??????
-
????private?long?ipBegin,?ipEnd;??
-
??????
-
????private?IPLocation?loc;??
-
????private?byte[]?buf;??
-
????private?byte[]?b4;??
-
????private?byte[]?b3;??
-
??????
-
????public?IPSeeker(String?fileName,String?dir)??{??
-
????????this.INSTALL_DIR=dir;??
-
????????this.IP_FILE=fileName;??
-
????????ipCache?=?new?HashMap<String,?IPLocation>();??
-
????????loc?=?new?IPLocation();??
-
????????buf?=?new?byte[100];??
-
????????b4?=?new?byte[4];??
-
????????b3?=?new?byte[3];??
-
????????try?{??
-
????????????ipFile?=?new?RandomAccessFile(IP_FILE,?"r");??
-
????????}?catch?(FileNotFoundException?e)?{??
-
??????????????
-
??????????????
-
????????????String?filename?=?new?File(IP_FILE).getName().toLowerCase();??
-
????????????File[]?files?=?new?File(INSTALL_DIR).listFiles();??
-
????????????for(int?i?=?0;?i?<?files.length;?i++)?{??
-
????????????????if(files[i].isFile())?{??
-
????????????????????if(files[i].getName().toLowerCase().equals(filename))?{??
-
????????????????????????try?{??
-
????????????????????????????ipFile?=?new?RandomAccessFile(files[i],?"r");??
-
????????????????????????}?catch?(FileNotFoundException?e1)?{??
-
????????????????????????????LogFactory.log("IP地址信息文件沒有找到,IP顯示功能將無法使用",Level.ERROR,e1);??
-
????????????????????????????ipFile?=?null;??
-
????????????????????????}??
-
????????????????????????break;??
-
????????????????????}??
-
????????????????}??
-
????????????}??
-
????????}???
-
??????????
-
????????if(ipFile?!=?null)?{??
-
????????????try?{??
-
????????????????ipBegin?=?readLong4(0);??
-
????????????????ipEnd?=?readLong4(4);??
-
????????????????if(ipBegin?==?-1?||?ipEnd?==?-1)?{??
-
????????????????????ipFile.close();??
-
????????????????????ipFile?=?null;??
-
????????????????}?????????????
-
????????????}?catch?(IOException?e)?{??
-
????????????????LogFactory.log("IP地址信息文件格式有錯誤,IP顯示功能將無法使用",Level.ERROR,e);??
-
????????????????ipFile?=?null;??
-
????????????}?????????????
-
????????}??
-
????}??
-
??????
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????public?List?getIPEntriesDebug(String?s)?{??
-
????????List<IPEntry>?ret?=?new?ArrayList<IPEntry>();??
-
????????long?endOffset?=?ipEnd?+?4;??
-
????????for(long?offset?=?ipBegin?+?4;?offset?<=?endOffset;?offset?+=?IP_RECORD_LENGTH)?{??
-
??????????????
-
????????????long?temp?=?readLong3(offset);??
-
??????????????
-
????????????if(temp?!=?-1)?{??
-
????????????????IPLocation?ipLoc?=?getIPLocation(temp);??
-
??????????????????
-
????????????????if(ipLoc.getCountry().indexOf(s)?!=?-1?||?ipLoc.getArea().indexOf(s)?!=?-1)?{??
-
????????????????????IPEntry?entry?=?new?IPEntry();??
-
????????????????????entry.country?=?ipLoc.getCountry();??
-
????????????????????entry.area?=?ipLoc.getArea();??
-
??????????????????????
-
????????????????????readIP(offset?-?4,?b4);??
-
????????????????????entry.beginIp?=?Util.getIpStringFromBytes(b4);??
-
??????????????????????
-
????????????????????readIP(temp,?b4);??
-
????????????????????entry.endIp?=?Util.getIpStringFromBytes(b4);??
-
??????????????????????
-
????????????????????ret.add(entry);??
-
????????????????}??
-
????????????}??
-
????????}??
-
????????return?ret;??
-
????}??
-
??????
-
????public?IPLocation?getIPLocation(String?ip){??
-
????????IPLocation?location=new?IPLocation();??
-
????????location.setArea(this.getArea(ip));??
-
????????location.setCountry(this.getCountry(ip));??
-
????????return?location;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????public?List<IPEntry>?getIPEntries(String?s)?{??
-
????????List<IPEntry>?ret?=?new?ArrayList<IPEntry>();??
-
????????try?{??
-
??????????????
-
????????????if(mbb?==?null)?{??
-
????????????????FileChannel?fc?=?ipFile.getChannel();??
-
????????????????mbb?=?fc.map(FileChannel.MapMode.READ_ONLY,?0,?ipFile.length());??
-
????????????????mbb.order(ByteOrder.LITTLE_ENDIAN);???????????????
-
????????????}??
-
??????????????
-
????????????int?endOffset?=?(int)ipEnd;??
-
????????????for(int?offset?=?(int)ipBegin?+?4;?offset?<=?endOffset;?offset?+=?IP_RECORD_LENGTH)?{??
-
????????????????int?temp?=?readInt3(offset);??
-
????????????????if(temp?!=?-1)?{??
-
????????????????????IPLocation?ipLoc?=?getIPLocation(temp);??
-
??????????????????????
-
????????????????????if(ipLoc.getCountry().indexOf(s)?!=?-1?||?ipLoc.getArea().indexOf(s)?!=?-1)?{??
-
????????????????????????IPEntry?entry?=?new?IPEntry();??
-
????????????????????????entry.country?=?ipLoc.getCountry();??
-
????????????????????????entry.area?=?ipLoc.getArea();??
-
??????????????????????????
-
????????????????????????readIP(offset?-?4,?b4);??
-
????????????????????????entry.beginIp?=?Util.getIpStringFromBytes(b4);??
-
??????????????????????????
-
????????????????????????readIP(temp,?b4);??
-
????????????????????????entry.endIp?=?Util.getIpStringFromBytes(b4);??
-
??????????????????????????
-
????????????????????????ret.add(entry);??
-
????????????????????}??
-
????????????????}??
-
????????????}?????????????
-
????????}?catch?(IOException?e)?{??
-
????????????LogFactory.log("",Level.ERROR,e);??
-
????????}??
-
????????return?ret;??
-
????}??
-
??
-
?????
-
?
-
?
-
?
-
??
-
????private?int?readInt3(int?offset)?{??
-
????????mbb.position(offset);??
-
????????return?mbb.getInt()?&?0x00FFFFFF;??
-
????}??
-
??
-
?????
-
?
-
?
-
??
-
????private?int?readInt3()?{??
-
????????return?mbb.getInt()?&?0x00FFFFFF;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????public?String?getCountry(byte[]?ip)?{??
-
??????????
-
????????if(ipFile?==?null)???
-
????????????return?Message.bad_ip_file;??
-
??????????
-
????????String?ipStr?=?Util.getIpStringFromBytes(ip);??
-
??????????
-
????????if(ipCache.containsKey(ipStr))?{??
-
????????????IPLocation?ipLoc?=?ipCache.get(ipStr);??
-
????????????return?ipLoc.getCountry();??
-
????????}?else?{??
-
????????????IPLocation?ipLoc?=?getIPLocation(ip);??
-
????????????ipCache.put(ipStr,?ipLoc.getCopy());??
-
????????????return?ipLoc.getCountry();??
-
????????}??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????public?String?getCountry(String?ip)?{??
-
????????return?getCountry(Util.getIpByteArrayFromString(ip));??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????public?String?getArea(byte[]?ip)?{??
-
??????????
-
????????if(ipFile?==?null)???
-
????????????return?Message.bad_ip_file;??
-
??????????
-
????????String?ipStr?=?Util.getIpStringFromBytes(ip);??
-
??????????
-
????????if(ipCache.containsKey(ipStr))?{??
-
????????????IPLocation?ipLoc?=?ipCache.get(ipStr);??
-
????????????return?ipLoc.getArea();??
-
????????}?else?{??
-
????????????IPLocation?ipLoc?=?getIPLocation(ip);??
-
????????????ipCache.put(ipStr,?ipLoc.getCopy());??
-
????????????return?ipLoc.getArea();??
-
????????}??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????public?String?getArea(String?ip)?{??
-
????????return?getArea(Util.getIpByteArrayFromString(ip));??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????private?IPLocation?getIPLocation(byte[]?ip)?{??
-
????????IPLocation?info?=?null;??
-
????????long?offset?=?locateIP(ip);??
-
????????if(offset?!=?-1)??
-
????????????info?=?getIPLocation(offset);??
-
????????if(info?==?null)?{??
-
????????????info?=?new?IPLocation();??
-
????????????info.setCountry?(??Message.unknown_country);??
-
????????????info.setArea(Message.unknown_area);??
-
????????}??
-
????????return?info;??
-
????}?????
-
??
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?long?readLong4(long?offset)?{??
-
????????long?ret?=?0;??
-
????????try?{??
-
????????????ipFile.seek(offset);??
-
????????????ret?|=?(ipFile.readByte()?&?0xFF);??
-
????????????ret?|=?((ipFile.readByte()?<<?8)?&?0xFF00);??
-
????????????ret?|=?((ipFile.readByte()?<<?16)?&?0xFF0000);??
-
????????????ret?|=?((ipFile.readByte()?<<?24)?&?0xFF000000);??
-
????????????return?ret;??
-
????????}?catch?(IOException?e)?{??
-
????????????return?-1;??
-
????????}??
-
????}??
-
??
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?long?readLong3(long?offset)?{??
-
????????long?ret?=?0;??
-
????????try?{??
-
????????????ipFile.seek(offset);??
-
????????????ipFile.readFully(b3);??
-
????????????ret?|=?(b3[0]?&?0xFF);??
-
????????????ret?|=?((b3[1]?<<?8)?&?0xFF00);??
-
????????????ret?|=?((b3[2]?<<?16)?&?0xFF0000);??
-
????????????return?ret;??
-
????????}?catch?(IOException?e)?{??
-
????????????return?-1;??
-
????????}??
-
????}?????
-
??????
-
?????
-
?
-
?
-
??
-
????private?long?readLong3()?{??
-
????????long?ret?=?0;??
-
????????try?{??
-
????????????ipFile.readFully(b3);??
-
????????????ret?|=?(b3[0]?&?0xFF);??
-
????????????ret?|=?((b3[1]?<<?8)?&?0xFF00);??
-
????????????ret?|=?((b3[2]?<<?16)?&?0xFF0000);??
-
????????????return?ret;??
-
????????}?catch?(IOException?e)?{??
-
????????????return?-1;??
-
????????}??
-
????}??
-
????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?void?readIP(long?offset,?byte[]?ip)?{??
-
????????try?{??
-
????????????ipFile.seek(offset);??
-
????????????ipFile.readFully(ip);??
-
????????????byte?temp?=?ip[0];??
-
????????????ip[0]?=?ip[3];??
-
????????????ip[3]?=?temp;??
-
????????????temp?=?ip[1];??
-
????????????ip[1]?=?ip[2];??
-
????????????ip[2]?=?temp;??
-
????????}?catch?(IOException?e)?{??
-
????????????LogFactory.log("",Level.ERROR,e);??
-
????????}??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?void?readIP(int?offset,?byte[]?ip)?{??
-
????????mbb.position(offset);??
-
????????mbb.get(ip);??
-
????????byte?temp?=?ip[0];??
-
????????ip[0]?=?ip[3];??
-
????????ip[3]?=?temp;??
-
????????temp?=?ip[1];??
-
????????ip[1]?=?ip[2];??
-
????????ip[2]?=?temp;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?int?compareIP(byte[]?ip,?byte[]?beginIp)?{??
-
????????for(int?i?=?0;?i?<?4;?i++)?{??
-
????????????int?r?=?compareByte(ip[i],?beginIp[i]);??
-
????????????if(r?!=?0)??
-
????????????????return?r;??
-
????????}??
-
????????return?0;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?int?compareByte(byte?b1,?byte?b2)?{??
-
????????if((b1?&?0xFF)?>?(b2?&?0xFF))???
-
????????????return?1;??
-
????????else?if((b1?^?b2)?==?0)??
-
????????????return?0;??
-
????????else???
-
????????????return?-1;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?long?locateIP(byte[]?ip)?{??
-
????????long?m?=?0;??
-
????????int?r;??
-
??????????
-
????????readIP(ipBegin,?b4);??
-
????????r?=?compareIP(ip,?b4);??
-
????????if(r?==?0)?return?ipBegin;??
-
????????else?if(r?<?0)?return?-1;??
-
??????????
-
????????for(long?i?=?ipBegin,?j?=?ipEnd;?i?<?j;?)?{??
-
????????????m?=?getMiddleOffset(i,?j);??
-
????????????readIP(m,?b4);??
-
????????????r?=?compareIP(ip,?b4);??
-
??????????????
-
????????????if(r?>?0)??
-
????????????????i?=?m;??
-
????????????else?if(r?<?0)?{??
-
????????????????if(m?==?j)?{??
-
????????????????????j?-=?IP_RECORD_LENGTH;??
-
????????????????????m?=?j;??
-
????????????????}?else???
-
????????????????????j?=?m;??
-
????????????}?else??
-
????????????????return?readLong3(m?+?4);??
-
????????}??
-
??????????
-
??????????
-
????????m?=?readLong3(m?+?4);??
-
????????readIP(m,?b4);??
-
????????r?=?compareIP(ip,?b4);??
-
????????if(r?<=?0)?return?m;??
-
????????else?return?-1;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?long?getMiddleOffset(long?begin,?long?end)?{??
-
????????long?records?=?(end?-?begin)?/?IP_RECORD_LENGTH;??
-
????????records?>>=?1;??
-
????????if(records?==?0)?records?=?1;??
-
????????return?begin?+?records?*?IP_RECORD_LENGTH;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????private?IPLocation?getIPLocation(long?offset)?{??
-
????????try?{??
-
??????????????
-
????????????ipFile.seek(offset?+?4);??
-
??????????????
-
????????????byte?b?=?ipFile.readByte();??
-
????????????if(b?==?REDIRECT_MODE_1)?{??
-
??????????????????
-
????????????????long?countryOffset?=?readLong3();??
-
??????????????????
-
????????????????ipFile.seek(countryOffset);??
-
??????????????????
-
????????????????b?=?ipFile.readByte();??
-
????????????????if(b?==?REDIRECT_MODE_2)?{??
-
????????????????????loc.setCountry?(??readString(readLong3()));??
-
????????????????????ipFile.seek(countryOffset?+?4);??
-
????????????????}?else??
-
????????????????????loc.setCountry?(?readString(countryOffset));??
-
??????????????????
-
????????????????loc.setArea(?readArea(ipFile.getFilePointer()));??
-
????????????}?else?if(b?==?REDIRECT_MODE_2)?{??
-
????????????????loc.setCountry?(?readString(readLong3()));??
-
????????????????loc.setArea(?readArea(offset?+?8));??
-
????????????}?else?{??
-
????????????????loc.setCountry?(??readString(ipFile.getFilePointer()?-?1));??
-
????????????????loc.setArea(?readArea(ipFile.getFilePointer()));??
-
????????????}??
-
????????????return?loc;??
-
????????}?catch?(IOException?e)?{??
-
????????????return?null;??
-
????????}??
-
????}?????
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????private?IPLocation?getIPLocation(int?offset)?{??
-
??????????
-
????????mbb.position(offset?+?4);??
-
??????????
-
????????byte?b?=?mbb.get();??
-
????????if(b?==?REDIRECT_MODE_1)?{??
-
??????????????
-
????????????int?countryOffset?=?readInt3();??
-
??????????????
-
????????????mbb.position(countryOffset);??
-
??????????????
-
????????????b?=?mbb.get();??
-
????????????if(b?==?REDIRECT_MODE_2)?{??
-
????????????????loc.setCountry?(??readString(readInt3()));??
-
????????????????mbb.position(countryOffset?+?4);??
-
????????????}?else??
-
????????????????loc.setCountry?(??readString(countryOffset));??
-
??????????????
-
????????????loc.setArea(readArea(mbb.position()));??
-
????????}?else?if(b?==?REDIRECT_MODE_2)?{??
-
????????????loc.setCountry?(?readString(readInt3()));??
-
????????????loc.setArea(readArea(offset?+?8));??
-
????????}?else?{??
-
????????????loc.setCountry?(??readString(mbb.position()?-?1));??
-
????????????loc.setArea(readArea(mbb.position()));??
-
????????}??
-
????????return?loc;??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
?
-
??
-
????private?String?readArea(long?offset)?throws?IOException?{??
-
????????ipFile.seek(offset);??
-
????????byte?b?=?ipFile.readByte();??
-
????????if(b?==?REDIRECT_MODE_1?||?b?==?REDIRECT_MODE_2)?{??
-
????????????long?areaOffset?=?readLong3(offset?+?1);??
-
????????????if(areaOffset?==?0)??
-
????????????????return?Message.unknown_area;??
-
????????????else??
-
????????????????return?readString(areaOffset);??
-
????????}?else??
-
????????????return?readString(offset);??
-
????}??
-
??????
-
?????
-
?
-
?
-
??
-
????private?String?readArea(int?offset)?{??
-
????????mbb.position(offset);??
-
????????byte?b?=?mbb.get();??
-
????????if(b?==?REDIRECT_MODE_1?||?b?==?REDIRECT_MODE_2)?{??
-
????????????int?areaOffset?=?readInt3();??
-
????????????if(areaOffset?==?0)??
-
????????????????return?Message.unknown_area;??
-
????????????else??
-
????????????????return?readString(areaOffset);??
-
????????}?else??
-
????????????return?readString(offset);??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????private?String?readString(long?offset)?{??
-
????????try?{??
-
????????????ipFile.seek(offset);??
-
????????????int?i;??
-
????????????for(i?=?0,?buf[i]?=?ipFile.readByte();?buf[i]?!=?0;?buf[++i]?=?ipFile.readByte());??
-
????????????if(i?!=?0)???
-
????????????????return?Util.getString(buf,?0,?i,?"GBK");??
-
????????}?catch?(IOException?e)?{?????????????
-
????????????LogFactory.log("",Level.ERROR,e);??
-
????????}??
-
????????return?"";??
-
????}??
-
??????
-
?????
-
?
-
?
-
?
-
??
-
????private?String?readString(int?offset)?{??
-
????????try?{??
-
????????????mbb.position(offset);??
-
????????????int?i;??
-
????????????for(i?=?0,?buf[i]?=?mbb.get();?buf[i]?!=?0;?buf[++i]?=?mbb.get());??
-
????????????if(i?!=?0)???
-
????????????????return?Util.getString(buf,?0,?i,?"GBK");?????????
-
????????}?catch?(IllegalArgumentException?e)?{??
-
????????????LogFactory.log("",Level.ERROR,e);??
-
????????}??
-
????????return?"";?????
-
????}??
-
}??
package com.showtime.IPparse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.showtime.util.LogFactory;
import org.apache.log4j.Level;
public class IPSeeker {
//純真IP數(shù)據(jù)庫名
private String IP_FILE="QQWry.Dat";
//保存的文件夾
private String INSTALL_DIR="f:/qqwry";
// 一些固定常量,比如記錄長度等等
private static final int IP_RECORD_LENGTH = 7;
private static final byte REDIRECT_MODE_1 = 0x01;
private static final byte REDIRECT_MODE_2 = 0x02;
// 用來做為cache,查詢一個ip時首先查看cache,以減少不必要的重復查找
private Map<String, IPLocation> ipCache;
// 隨機文件訪問類
private RandomAccessFile ipFile;
// 內存映射文件
private MappedByteBuffer mbb;
// 起始地區(qū)的開始和結束的絕對偏移
private long ipBegin, ipEnd;
// 為提高效率而采用的臨時變量
private IPLocation loc;
private byte[] buf;
private byte[] b4;
private byte[] b3;
public IPSeeker(String fileName,String dir) {
this.INSTALL_DIR=dir;
this.IP_FILE=fileName;
ipCache = new HashMap<String, IPLocation>();
loc = new IPLocation();
buf = new byte[100];
b4 = new byte[4];
b3 = new byte[3];
try {
ipFile = new RandomAccessFile(IP_FILE, "r");
} catch (FileNotFoundException e) {
// 如果找不到這個文件,再嘗試再當前目錄下搜索,這次全部改用小寫文件名
// 因為有些系統(tǒng)可能區(qū)分大小寫導致找不到ip地址信息文件
String filename = new File(IP_FILE).getName().toLowerCase();
File[] files = new File(INSTALL_DIR).listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isFile()) {
if(files[i].getName().toLowerCase().equals(filename)) {
try {
ipFile = new RandomAccessFile(files[i], "r");
} catch (FileNotFoundException e1) {
LogFactory.log("IP地址信息文件沒有找到,IP顯示功能將無法使用",Level.ERROR,e1);
ipFile = null;
}
break;
}
}
}
}
// 如果打開文件成功,讀取文件頭信息
if(ipFile != null) {
try {
ipBegin = readLong4(0);
ipEnd = readLong4(4);
if(ipBegin == -1 || ipEnd == -1) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
LogFactory.log("IP地址信息文件格式有錯誤,IP顯示功能將無法使用",Level.ERROR,e);
ipFile = null;
}
}
}
/**
* 給定一個地點的不完全名字,得到一系列包含s子串的IP范圍記錄
* @param s 地點子串
* @return 包含IPEntry類型的List
*/
public List getIPEntriesDebug(String s) {
List<IPEntry> ret = new ArrayList<IPEntry>();
long endOffset = ipEnd + 4;
for(long offset = ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
// 讀取結束IP偏移
long temp = readLong3(offset);
// 如果temp不等于-1,讀取IP的地點信息
if(temp != -1) {
IPLocation ipLoc = getIPLocation(temp);
// 判斷是否這個地點里面包含了s子串,如果包含了,添加這個記錄到List中,如果沒有,繼續(xù)
if(ipLoc.getCountry().indexOf(s) != -1 || ipLoc.getArea().indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = ipLoc.getCountry();
entry.area = ipLoc.getArea();
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = Util.getIpStringFromBytes(b4);
// 得到結束IP
readIP(temp, b4);
entry.endIp = Util.getIpStringFromBytes(b4);
// 添加該記錄
ret.add(entry);
}
}
}
return ret;
}
public IPLocation getIPLocation(String ip){
IPLocation location=new IPLocation();
location.setArea(this.getArea(ip));
location.setCountry(this.getCountry(ip));
return location;
}
/**
* 給定一個地點的不完全名字,得到一系列包含s子串的IP范圍記錄
* @param s 地點子串
* @return 包含IPEntry類型的List
*/
public List<IPEntry> getIPEntries(String s) {
List<IPEntry> ret = new ArrayList<IPEntry>();
try {
// 映射IP信息文件到內存中
if(mbb == null) {
FileChannel fc = ipFile.getChannel();
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ipFile.length());
mbb.order(ByteOrder.LITTLE_ENDIAN);
}
int endOffset = (int)ipEnd;
for(int offset = (int)ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
int temp = readInt3(offset);
if(temp != -1) {
IPLocation ipLoc = getIPLocation(temp);
// 判斷是否這個地點里面包含了s子串,如果包含了,添加這個記錄到List中,如果沒有,繼續(xù)
if(ipLoc.getCountry().indexOf(s) != -1 || ipLoc.getArea().indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = ipLoc.getCountry();
entry.area = ipLoc.getArea();
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = Util.getIpStringFromBytes(b4);
// 得到結束IP
readIP(temp, b4);
entry.endIp = Util.getIpStringFromBytes(b4);
// 添加該記錄
ret.add(entry);
}
}
}
} catch (IOException e) {
LogFactory.log("",Level.ERROR,e);
}
return ret;
}
/**
* 從內存映射文件的offset位置開始的3個字節(jié)讀取一個int
* @param offset
* @return
*/
private int readInt3(int offset) {
mbb.position(offset);
return mbb.getInt() & 0x00FFFFFF;
}
/**
* 從內存映射文件的當前位置開始的3個字節(jié)讀取一個int
* @return
*/
private int readInt3() {
return mbb.getInt() & 0x00FFFFFF;
}
/**
* 根據(jù)IP得到國家名
* @param ip ip的字節(jié)數(shù)組形式
* @return 國家名字符串
*/
public String getCountry(byte[] ip) {
// 檢查ip地址文件是否正常
if(ipFile == null)
return Message.bad_ip_file;
// 保存ip,轉換ip字節(jié)數(shù)組為字符串形式
String ipStr = Util.getIpStringFromBytes(ip);
// 先檢查cache中是否已經(jīng)包含有這個ip的結果,沒有再搜索文件
if(ipCache.containsKey(ipStr)) {
IPLocation ipLoc = ipCache.get(ipStr);
return ipLoc.getCountry();
} else {
IPLocation ipLoc = getIPLocation(ip);
ipCache.put(ipStr, ipLoc.getCopy());
return ipLoc.getCountry();
}
}
/**
* 根據(jù)IP得到國家名
* @param ip IP的字符串形式
* @return 國家名字符串
*/
public String getCountry(String ip) {
return getCountry(Util.getIpByteArrayFromString(ip));
}
/**
* 根據(jù)IP得到地區(qū)名
* @param ip ip的字節(jié)數(shù)組形式
* @return 地區(qū)名字符串
*/
public String getArea(byte[] ip) {
// 檢查ip地址文件是否正常
if(ipFile == null)
return Message.bad_ip_file;
// 保存ip,轉換ip字節(jié)數(shù)組為字符串形式
String ipStr = Util.getIpStringFromBytes(ip);
// 先檢查cache中是否已經(jīng)包含有這個ip的結果,沒有再搜索文件
if(ipCache.containsKey(ipStr)) {
IPLocation ipLoc = ipCache.get(ipStr);
return ipLoc.getArea();
} else {
IPLocation ipLoc = getIPLocation(ip);
ipCache.put(ipStr, ipLoc.getCopy());
return ipLoc.getArea();
}
}
/**
* 根據(jù)IP得到地區(qū)名
* @param ip IP的字符串形式
* @return 地區(qū)名字符串
*/
public String getArea(String ip) {
return getArea(Util.getIpByteArrayFromString(ip));
}
/**
* 根據(jù)ip搜索ip信息文件,得到IPLocation結構,所搜索的ip參數(shù)從類成員ip中得到
* @param ip 要查詢的IP
* @return IPLocation結構
*/
private IPLocation getIPLocation(byte[] ip) {
IPLocation info = null;
long offset = locateIP(ip);
if(offset != -1)
info = getIPLocation(offset);
if(info == null) {
info = new IPLocation();
info.setCountry ( Message.unknown_country);
info.setArea(Message.unknown_area);
}
return info;
}
/**
* 從offset位置讀取4個字節(jié)為一個long,因為java為big-endian格式,所以沒辦法
* 用了這么一個函數(shù)來做轉換
* @param offset
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong4(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ret |= (ipFile.readByte() & 0xFF);
ret |= ((ipFile.readByte() << 8) & 0xFF00);
ret |= ((ipFile.readByte() << 16) & 0xFF0000);
ret |= ((ipFile.readByte() << 24) & 0xFF000000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從offset位置讀取3個字節(jié)為一個long,因為java為big-endian格式,所以沒辦法
* 用了這么一個函數(shù)來做轉換
* @param offset 整數(shù)的起始偏移
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong3(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從當前位置讀取3個字節(jié)轉換成long
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong3() {
long ret = 0;
try {
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從offset位置讀取四個字節(jié)的ip地址放入ip數(shù)組中,讀取后的ip為big-endian格式,但是
* 文件中是little-endian形式,將會進行轉換
* @param offset
* @param ip
*/
private void readIP(long offset, byte[] ip) {
try {
ipFile.seek(offset);
ipFile.readFully(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
} catch (IOException e) {
LogFactory.log("",Level.ERROR,e);
}
}
/**
* 從offset位置讀取四個字節(jié)的ip地址放入ip數(shù)組中,讀取后的ip為big-endian格式,但是
* 文件中是little-endian形式,將會進行轉換
* @param offset
* @param ip
*/
private void readIP(int offset, byte[] ip) {
mbb.position(offset);
mbb.get(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
}
/**
* 把類成員ip和beginIp比較,注意這個beginIp是big-endian的
* @param ip 要查詢的IP
* @param beginIp 和被查詢IP相比較的IP
* @return 相等返回0,ip大于beginIp則返回1,小于返回-1。
*/
private int compareIP(byte[] ip, byte[] beginIp) {
for(int i = 0; i < 4; i++) {
int r = compareByte(ip[i], beginIp[i]);
if(r != 0)
return r;
}
return 0;
}
/**
* 把兩個byte當作無符號數(shù)進行比較
* @param b1
* @param b2
* @return 若b1大于b2則返回1,相等返回0,小于返回-1
*/
private int compareByte(byte b1, byte b2) {
if((b1 & 0xFF) > (b2 & 0xFF)) // 比較是否大于
return 1;
else if((b1 ^ b2) == 0)// 判斷是否相等
return 0;
else
return -1;
}
/**
* 這個方法將根據(jù)ip的內容,定位到包含這個ip國家地區(qū)的記錄處,返回一個絕對偏移
* 方法使用二分法查找。
* @param ip 要查詢的IP
* @return 如果找到了,返回結束IP的偏移,如果沒有找到,返回-1
*/
private long locateIP(byte[] ip) {
long m = 0;
int r;
// 比較第一個ip項
readIP(ipBegin, b4);
r = compareIP(ip, b4);
if(r == 0) return ipBegin;
else if(r < 0) return -1;
// 開始二分搜索
for(long i = ipBegin, j = ipEnd; i < j; ) {
m = getMiddleOffset(i, j);
readIP(m, b4);
r = compareIP(ip, b4);
// log.debug(Utils.getIpStringFromBytes(b));
if(r > 0)
i = m;
else if(r < 0) {
if(m == j) {
j -= IP_RECORD_LENGTH;
m = j;
} else
j = m;
} else
return readLong3(m + 4);
}
// 如果循環(huán)結束了,那么i和j必定是相等的,這個記錄為最可能的記錄,但是并非
// 肯定就是,還要檢查一下,如果是,就返回結束地址區(qū)的絕對偏移
m = readLong3(m + 4);
readIP(m, b4);
r = compareIP(ip, b4);
if(r <= 0) return m;
else return -1;
}
/**
* 得到begin偏移和end偏移中間位置記錄的偏移
* @param begin
* @param end
* @return
*/
private long getMiddleOffset(long begin, long end) {
long records = (end - begin) / IP_RECORD_LENGTH;
records >>= 1;
if(records == 0) records = 1;
return begin + records * IP_RECORD_LENGTH;
}
/**
* 給定一個ip國家地區(qū)記錄的偏移,返回一個IPLocation結構
* @param offset 國家記錄的起始偏移
* @return IPLocation對象
*/
private IPLocation getIPLocation(long offset) {
try {
// 跳過4字節(jié)ip
ipFile.seek(offset + 4);
// 讀取第一個字節(jié)判斷是否標志字節(jié)
byte b = ipFile.readByte();
if(b == REDIRECT_MODE_1) {
// 讀取國家偏移
long countryOffset = readLong3();
// 跳轉至偏移處
ipFile.seek(countryOffset);
// 再檢查一次標志字節(jié),因為這個時候這個地方仍然可能是個重定向
b = ipFile.readByte();
if(b == REDIRECT_MODE_2) {
loc.setCountry ( readString(readLong3()));
ipFile.seek(countryOffset + 4);
} else
loc.setCountry ( readString(countryOffset));
// 讀取地區(qū)標志
loc.setArea( readArea(ipFile.getFilePointer()));
} else if(b == REDIRECT_MODE_2) {
loc.setCountry ( readString(readLong3()));
loc.setArea( readArea(offset + 8));
} else {
loc.setCountry ( readString(ipFile.getFilePointer() - 1));
loc.setArea( readArea(ipFile.getFilePointer()));
}
return loc;
} catch (IOException e) {
return null;
}
}
/**
* 給定一個ip國家地區(qū)記錄的偏移,返回一個IPLocation結構,此方法應用與內存映射文件方式
* @param offset 國家記錄的起始偏移
* @return IPLocation對象
*/
private IPLocation getIPLocation(int offset) {
// 跳過4字節(jié)ip
mbb.position(offset + 4);
// 讀取第一個字節(jié)判斷是否標志字節(jié)
byte b = mbb.get();
if(b == REDIRECT_MODE_1) {
// 讀取國家偏移
int countryOffset = readInt3();
// 跳轉至偏移處
mbb.position(countryOffset);
// 再檢查一次標志字節(jié),因為這個時候這個地方仍然可能是個重定向
b = mbb.get();
if(b == REDIRECT_MODE_2) {
loc.setCountry ( readString(readInt3()));
mbb.position(countryOffset + 4);
} else
loc.setCountry ( readString(countryOffset));
// 讀取地區(qū)標志
loc.setArea(readArea(mbb.position()));
} else if(b == REDIRECT_MODE_2) {
loc.setCountry ( readString(readInt3()));
loc.setArea(readArea(offset + 8));
} else {
loc.setCountry ( readString(mbb.position() - 1));
loc.setArea(readArea(mbb.position()));
}
return loc;
}
/**
* 從offset偏移開始解析后面的字節(jié),讀出一個地區(qū)名
* @param offset 地區(qū)記錄的起始偏移
* @return 地區(qū)名字符串
* @throws IOException
*/
private String readArea(long offset) throws IOException {
ipFile.seek(offset);
byte b = ipFile.readByte();
if(b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) {
long areaOffset = readLong3(offset + 1);
if(areaOffset == 0)
return Message.unknown_area;
else
return readString(areaOffset);
} else
return readString(offset);
}
/**
* @param offset 地區(qū)記錄的起始偏移
* @return 地區(qū)名字符串
*/
private String readArea(int offset) {
mbb.position(offset);
byte b = mbb.get();
if(b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) {
int areaOffset = readInt3();
if(areaOffset == 0)
return Message.unknown_area;
else
return readString(areaOffset);
} else
return readString(offset);
}
/**
* 從offset偏移處讀取一個以0結束的字符串
* @param offset 字符串起始偏移
* @return 讀取的字符串,出錯返回空字符串
*/
private String readString(long offset) {
try {
ipFile.seek(offset);
int i;
for(i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] = ipFile.readByte());
if(i != 0)
return Util.getString(buf, 0, i, "GBK");
} catch (IOException e) {
LogFactory.log("",Level.ERROR,e);
}
return "";
}
/**
* 從內存映射文件的offset位置得到一個0結尾字符串
* @param offset 字符串起始偏移
* @return 讀取的字符串,出錯返回空字符串
*/
private String readString(int offset) {
try {
mbb.position(offset);
int i;
for(i = 0, buf[i] = mbb.get(); buf[i] != 0; buf[++i] = mbb.get());
if(i != 0)
return Util.getString(buf, 0, i, "GBK");
} catch (IllegalArgumentException e) {
LogFactory.log("",Level.ERROR,e);
}
return "";
}
}
在實際項目用我使用spring注入IP地址庫文件的名字和所在目錄,并能保證IPSeeker的單一實例。
下面是個工具類,把string和btye數(shù)組之間互相轉換的類。
- package?com.showtime.IPparse;??
- ??
- ??
- import?java.io.UnsupportedEncodingException;??
- import?java.util.StringTokenizer;??
- ??
- import?org.apache.log4j.Level;??
- ??
- import??com.showtime.util.LogFactory;??
- ??
- ??
- ??
- ?
- ?
- ??
- public?class?Util?{??
- ??????
- ????private?static?StringBuilder?sb?=?new?StringBuilder();??
- ?????
- ?
- ?
- ?
- ??
- ????public?static?byte[]?getIpByteArrayFromString(String?ip)?{??
- ????????byte[]?ret?=?new?byte[4];??
- ????????StringTokenizer?st?=?new?StringTokenizer(ip,?".");??
- ????????try?{??
- ????????????ret[0]?=?(byte)(Integer.parseInt(st.nextToken())?&?0xFF);??
- ????????????ret[1]?=?(byte)(Integer.parseInt(st.nextToken())?&?0xFF);??
- ????????????ret[2]?=?(byte)(Integer.parseInt(st.nextToken())?&?0xFF);??
- ????????????ret[3]?=?(byte)(Integer.parseInt(st.nextToken())?&?0xFF);??
- ????????}?catch?(Exception?e)?{??
- ??????????LogFactory.log("從ip的字符串形式得到字節(jié)數(shù)組形式報錯",?Level.ERROR,?e);??
- ????????}??
- ????????return?ret;??
- ????}??
- ?????
- ?
- ?
- ??
- ????public?static?String?getIpStringFromBytes(byte[]?ip)?{??
- ????????sb.delete(0,?sb.length());??
- ????????sb.append(ip[0]?&?0xFF);??
- ????????sb.append('.');???????
- ????????sb.append(ip[1]?&?0xFF);??
- ????????sb.append('.');???????
- ????????sb.append(ip[2]?&?0xFF);??
- ????????sb.append('.');???????
- ????????sb.append(ip[3]?&?0xFF);??
- ????????return?sb.toString();??
- ????}??
- ??????
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?String?getString(byte[]?b,?int?offset,?int?len,?String?encoding)?{??
- ????????try?{??
- ????????????return?new?String(b,?offset,?len,?encoding);??
- ????????}?catch?(UnsupportedEncodingException?e)?{??
- ????????????return?new?String(b,?offset,?len);??
- ????????}??
- ????}??
- }??
package com.showtime.IPparse;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
import org.apache.log4j.Level;
import com.showtime.util.LogFactory;
/**
* 工具類,提供一些方便的方法
*/
public class Util {
private static StringBuilder sb = new StringBuilder();
/**
* 從ip的字符串形式得到字節(jié)數(shù)組形式
* @param ip 字符串形式的ip
* @return 字節(jié)數(shù)組形式的ip
*/
public static byte[] getIpByteArrayFromString(String ip) {
byte[] ret = new byte[4];
StringTokenizer st = new StringTokenizer(ip, ".");
try {
ret[0] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
ret[1] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
ret[2] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
ret[3] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
} catch (Exception e) {
LogFactory.log("從ip的字符串形式得到字節(jié)數(shù)組形式報錯", Level.ERROR, e);
}
return ret;
}
/**
* @param ip ip的字節(jié)數(shù)組形式
* @return 字符串形式的ip
*/
public static String getIpStringFromBytes(byte[] ip) {
sb.delete(0, sb.length());
sb.append(ip[0] & 0xFF);
sb.append('.');
sb.append(ip[1] & 0xFF);
sb.append('.');
sb.append(ip[2] & 0xFF);
sb.append('.');
sb.append(ip[3] & 0xFF);
return sb.toString();
}
/**
* 根據(jù)某種編碼方式將字節(jié)數(shù)組轉換成字符串
* @param b 字節(jié)數(shù)組
* @param offset 要轉換的起始位置
* @param len 要轉換的長度
* @param encoding 編碼方式
* @return 如果encoding不支持,返回一個缺省編碼的字符串
*/
public static String getString(byte[] b, int offset, int len, String encoding) {
try {
return new String(b, offset, len, encoding);
} catch (UnsupportedEncodingException e) {
return new String(b, offset, len);
}
}
}
下面是個常量值的類,用接口形式來定義省事不少。
- package?com.showtime.IPparse;??
- ??
- public?interface?Message?{??
- ????String?bad_ip_file="IP地址庫文件錯誤";??
- ????String?unknown_country="未知國家";??
- ????String?unknown_area="未知地區(qū)";??
- }??
package com.showtime.IPparse;
public interface Message {
String bad_ip_file="IP地址庫文件錯誤";
String unknown_country="未知國家";
String unknown_area="未知地區(qū)";
}
一個封裝國家和地區(qū)的實體類
- package?com.showtime.IPparse;??
- ??
- ??
- ?
- ?
- ?
- ??
- ??
- public?class?IPLocation?{??
- ????private?String?country;??
- ????private?String?area;??
- ??????
- ????public?IPLocation()?{??
- ????????country?=?area?=?"";??
- ????}??
- ??????
- ????public?IPLocation?getCopy()?{??
- ????????IPLocation?ret?=?new?IPLocation();??
- ????????ret.country?=?country;??
- ????????ret.area?=?area;??
- ????????return?ret;??
- ????}??
- ??
- ????public?String?getCountry()?{??
- ????????return?country;??
- ????}??
- ??
- ????public?void?setCountry(String?country)?{??
- ????????this.country?=?country;??
- ????}??
- ??
- ????public?String?getArea()?{??
- ????????return?area;??
- ????}??
- ??
- ????public?void?setArea(String?area)?{??
- ??????????????????
- ????????if(area.trim().equals("CZ88.NET")){??
- ????????????this.area="本機或本網(wǎng)絡";??
- ????????}else{??
- ????????????this.area?=?area;??
- ????????}??
- ????}??
- }??
package com.showtime.IPparse;
/**
*
* @category 用來封裝ip相關信息,目前只有兩個字段,ip所在的國家和地區(qū)
*/
public class IPLocation {
private String country;
private String area;
public IPLocation() {
country = area = "";
}
public IPLocation getCopy() {
IPLocation ret = new IPLocation();
ret.country = country;
ret.area = area;
return ret;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getArea() {
return area;
}
public void setArea(String area) {
//如果為局域網(wǎng),純真IP地址庫的地區(qū)會顯示CZ88.NET,這里把它去掉
if(area.trim().equals("CZ88.NET")){
this.area="本機或本網(wǎng)絡";
}else{
this.area = area;
}
}
}
一下是一個范圍記錄的類
- package?com.showtime.IPparse;??
- ?
- ?
- ?
- ?
- ??
- public?class?IPEntry?{??
- ????public?String?beginIp;??
- ????public?String?endIp;??
- ????public?String?country;??
- ????public?String?area;??
- ??????
- ?????
- ?
- ??
- ????public?IPEntry()?{??
- ????????beginIp?=?endIp?=?country?=?area?=?"";??
- ????}??
- }??
package com.showtime.IPparse;
/**
* <pre>
* 一條IP范圍記錄,不僅包括國家和區(qū)域,也包括起始IP和結束IP
* </pre>
*/
public class IPEntry {
public String beginIp;
public String endIp;
public String country;
public String area;
/**
* 構造函數(shù)
*/
public IPEntry() {
beginIp = endIp = country = area = "";
}
}
日志記錄類
- package?com.showtime.util;??
- ??
- import?org.apache.log4j.Level;??
- import?org.apache.log4j.Logger;??
- ??
- ?
- ?
- ?
- ?
- ??
- public?class?LogFactory?{??
- ????private?static?final?Logger?logger;??
- ????static?{??
- ????????logger?=?Logger.getLogger("stdout");??
- ????????logger.setLevel(Level.DEBUG);??
- ????}??
- ??
- ????public?static?void?log(String?info,?Level?level,?Throwable?ex)?{??
- ????????logger.log(level,?info,?ex);??
- ????}??
- ??????
- ????public?static?Level??getLogLevel(){??
- ????????return?logger.getLevel();??
- ????}??
- ??
- }??
package com.showtime.util;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
/**
*
*
* 日志工廠
*/
public class LogFactory {
private static final Logger logger;
static {
logger = Logger.getLogger("stdout");
logger.setLevel(Level.DEBUG);
}
public static void log(String info, Level level, Throwable ex) {
logger.log(level, info, ex);
}
public static Level getLogLevel(){
return logger.getLevel();
}
}
下面是測試類
- package?com.showtime.IPparse;??
- ??
- import?junit.framework.TestCase;??
- ??
- public?class?IPtest?extends?TestCase?{??
- ??????
- ????public?void?testIp(){??
- ??????????????????
- ????????IPSeeker?ip=new?IPSeeker("QQWry.Dat","f:/qqwry");??
- ???????????
- System.out.println(ip.getIPLocation("58.20.43.13").getCountry()+":"+ip.getIPLocation("58.20.43.13").getArea());??
- ????}??
- }??
package com.showtime.IPparse;
import junit.framework.TestCase;
public class IPtest extends TestCase {
public void testIp(){
//指定純真數(shù)據(jù)庫的文件名,所在文件夾
IPSeeker ip=new IPSeeker("QQWry.Dat","f:/qqwry");
//測試IP 58.20.43.13
System.out.println(ip.getIPLocation("58.20.43.13").getCountry()+":"+ip.getIPLocation("58.20.43.13").getArea());
}
}
當輸出:湖南省長沙市:網(wǎng)通