?? ipsearcher.cs
字號:
using System;
using System.IO;
using System.Text;
using System.Data;
namespace SmartIP
{
/// <summary>
/// IpSearcher 的摘要說明。
/// </summary>
public class IpSearcher
{
private Stream dbStream;
private uint ipCount = 0;
private uint indexPos = 0;//索引區開始位置
public IpSearcher()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
private String getStr(Stream stream)
{
byte[] byteArray = new byte[256];
Encoding gb2312 = Encoding.GetEncoding("GB2312");
int count = 0;
while(true)
{
byteArray[count] = (byte)stream.ReadByte();
if(0 == byteArray[count])
break;
count++;
}
char[] charArray = new char[count];
gb2312.GetChars(byteArray, 0, count, charArray, 0);
string result = "";
for(int i=0; i<charArray.Length && '\0'!=(int)charArray[i]; i++)
{
result = result + charArray[i];
}
return result;
}
private uint byteArray2uint(byte[] byteArray)
{
uint result = 0;
for(int i=0; i<byteArray.Length; i++)
{
result = (uint)byteArray[i] * (uint)Math.Pow(256, i) + result;
}
return result;
}
public int loadIpDB(String fileName) //加載IP數據庫
{
if (!File.Exists(fileName))
{
return 0;//文件未找到
}
try
{
dbStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception e)
{
Console.WriteLine("Exception in IpSearcher.loadIpDB(): " + e.Message);
return 1;//無法打開文件
}
uint beginInt = 0;
uint endInt = 0;
byte[] tmpByte = new byte[4];
dbStream.Read(tmpByte,0, 4);
beginInt = byteArray2uint(tmpByte);
dbStream.Read(tmpByte,0, 4);
endInt = byteArray2uint(tmpByte);
uint total = endInt - beginInt;
if (total%7 != 0)
{
return 2;//文件損壞
}
ipCount = total/7;
ipCount++;
indexPos = beginInt;
//dbStream.
return -1;
}
private uint str2IP(String strIP)
{
uint iIP = 0;
uint tmpIP = 0;
for(int i=0; i<strIP.Length; i++)
{
char curChar = strIP[i];
if('.' == curChar)
{
iIP = 256 * iIP + tmpIP;
tmpIP = 0;
}
else
{
tmpIP = 10 * tmpIP + curChar - '0';
}
}
iIP = 256 * iIP + tmpIP;
return iIP;
}
public String ip2Address(String strIP) //61.48.0.0(2) 61.53.27.0 61.131.46.0 3/1026818048/3d.34/61.52
{
String result = "";
if(null == dbStream)
{
return "";
}
uint iIP = str2IP(strIP);
uint beginPos = 0;
uint endPos = ipCount;
byte[] tmpByte = new byte[4];
while(true)
{
if(beginPos >= endPos - 1)
{
break;
}
dbStream.Position = indexPos + (beginPos + endPos)/2 * 7;
dbStream.Read(tmpByte,0, 4);
uint tmpIP = byteArray2uint(tmpByte);
if(iIP < tmpIP)
{
endPos = (beginPos + endPos)/2;
}
else
{
beginPos = (beginPos + endPos)/2;
}
}
byte[] tmp3Byte = new byte[3];
dbStream.Position = indexPos + 7 * beginPos + 4;
dbStream.Read(tmp3Byte,0, 3);
uint ipPos = byteArray2uint(tmp3Byte);//ipPos要查找的IP紀錄的起始位置
dbStream.Position = ipPos;
dbStream.Read(tmpByte,0, 4);
if(iIP <= byteArray2uint(tmpByte))//找到該IP的位置信息
{
uint curPos = ipPos + 4;
if(0x01 == dbStream.ReadByte())
{
dbStream.Read(tmp3Byte,0, 3);
curPos = byteArray2uint(tmp3Byte);
dbStream.Position = curPos;
}
else
{
dbStream.Seek(-1, SeekOrigin.Current);
}
if(0x02 == dbStream.ReadByte())
{
dbStream.Read(tmp3Byte,0, 3);
curPos = byteArray2uint(tmp3Byte);
dbStream.Position = curPos;
}
else
{
dbStream.Seek(-1, SeekOrigin.Current);
}
//找到國家
result = "國家:" + getStr(dbStream);
//(0為失敗中止標志)
//地區
if( 0x02 == dbStream.ReadByte()) //找到國家以后還發現0x02跳過去找地區(國家/地區)
{
dbStream.Read(tmp3Byte,0, 3);
curPos = byteArray2uint(tmp3Byte);
}
else
{
dbStream.Seek(-1, SeekOrigin.Current);
}
result = result + "地區:" + getStr(dbStream);
}
else
{
result = "未知數據";
}
return result;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -