📄 iplooktable.cs
字号:
using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Configuration;
using System.Net;
namespace webvisitStatistic.Reporter.IPLook
{
/// <summary>
/// IpLookTable 的摘要说明。
/// </summary>
public class IpLookTable
{
public struct IpRange
{
public Int64 sip;
public Int64 eip;
}
public class IpPosition
{
public IpRange RangeOfIp;
public String WidePos;
public String NarrowPos;
public IpPosition(Int64 startip,Int64 endip,String wPos,String nPos)
{
RangeOfIp.sip = startip;
RangeOfIp.eip = endip;
WidePos = wPos;
NarrowPos = nPos;
}
}
protected IpLookTable()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Init(String connstr)
{
_connString = connstr;
using (SqlConnection conn= new SqlConnection(_connString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = SelCounts;
try
{
conn.Open();
_lenOfiptable = (int)cmd.ExecuteScalar();
// 读入数据
_ipItems = new IpRange[_lenOfiptable];
cmd.CommandText = SelIpPosList;
SqlDataReader reader = cmd.ExecuteReader();
int i = 0;
while (reader.Read())
{
_ipItems[i].sip = (Int64)reader[0];
_ipItems[i].eip = (Int64)reader[1];
++i;
}
Debug.Assert(_lenOfiptable==i);
reader.Close();
}catch(Exception ex)
{
_lenOfiptable = 0;
_ipItems = null;
throw ex;
}
}
}
// 返回ip地址所在的范围
public IpPosition SearchIpPostion(String ip){
Debug.Assert(IPAddress.Parse(ip)!=null);
return SearchIpPostion( IPAddress.Parse(ip).Address );
}
public IpPosition SearchIpPostion(Int64 ip)
{
int index = SearchRangeOfStartIp(ip);
if (-1==index) {
return new IpPosition(-1,-1,"未知地址",String.Empty);
}
IpPosition ippos;
using (SqlConnection conn=new SqlConnection(_connString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = SelIpPosRange;
cmd.Parameters.Add("@startip",System.Data.SqlDbType.BigInt).Value=_ipItems[index].sip;
conn.Open();
SqlDataReader reader =
cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);
Boolean has = reader.Read();
Debug.Assert(has);
ippos = new IpPosition(
(Int64)reader[0]
,(Int64)reader[1]
,(reader[2]==System.DBNull.Value)?String.Empty:(String)reader[2]
,(reader[3]==System.DBNull.Value)?String.Empty:(String)reader[3]);
reader.Close();
}
return ippos;
}
// 检索指定的ip范围在IpTable中的位置 -1表示未知的位置
public int SearchRangeOfStartIp(Int64 ip)
{
int a,b,m;
Debug.Assert(_lenOfiptable>0);
a = 0; b = _lenOfiptable;
while (a<=b) {
m=(a+b)>>1;
if (ip>_ipItems[m].eip) {
a = m+1;
}else if(ip<_ipItems[m].sip)
{
b = m-1;
}else{
return m;
}
}
return -1;
}
public static IpLookTable Instance()
{
if(null==_theLookupTable)
{
_theLookupTable = new IpLookTable();
}
return _theLookupTable;
}
public IpRange[] Items{
get{
return _ipItems;
}
}
private const String SelCounts = "select count(*) from ippos";
private const String SelIpPosList = "select startip,endip from ippos order by startip";
private const String SelIpPosRange = "SelIpPosRange";
private String _connString;
private IpRange[] _ipItems;
private int _lenOfiptable = 0;
private static IpLookTable _theLookupTable = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -