asp.net DZ forum based on IP address to obtain the location of code _ Practical skills

Source: Internet
Author: User
How to use: Ipsearch.getaddresswithip ("202.96.128.167")
CS Class Code
Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Introduction of namespaces
Using System.IO;

<summary>
Judge IP attribution to class
</summary>
public class Ipsearch
{
private static Object Lockhelper = new Object ();

static Phczip Pcz = new Phczip ();

static string FilePath = "";

static bool Fileisexsit = TRUE;

Static Ipsearch ()
{
FilePath = HttpContext.Current.Server.MapPath ("~/ipdata.config");
Pcz. Setdbfilepath (FilePath);
}

<summary>
Return IP Lookup Results
</summary>
<param name= "Ipvalue" > IP address to look for </param>
<returns></returns>
public static string Getaddresswithip (String ipvalue)
{
Lock (Lockhelper)
{
string result = Pcz. Getaddresswithip (Ipvalue.trim ());

if (fileisexsit)
{
if (result. IndexOf ("IANA") >= 0)
{
Return "";
}
Else
{
return result;
}
}
Else
{
return null;
}
}
}

<summary>
Auxiliary classes, for storing IP index information
</summary>
///
public class Cz_index_info
{
Public UInt32 Ipset;
Public UInt32 Ipend;
Public UInt32 Offset;

Public Cz_index_info ()
{
Ipset = 0;
ipend = 0;
Offset = 0;
}
}

Read Pure IP Database class
public class Phczip
{
protected bool bfilepathinitialized;
protected string FilePath;
protected FileStream FILESTRM;
protected UInt32 Index_set;
protected UInt32 index_end;
protected UInt32 Index_count;
protected UInt32 Search_index_set;
protected UInt32 search_index_end;
protected Cz_index_info Search_set;
protected Cz_index_info Search_mid;
protected Cz_index_info search_end;

Public Phczip ()
{
Bfilepathinitialized = false;
}

Public Phczip (String dbfilepath)
{
Bfilepathinitialized = false;
Setdbfilepath (Dbfilepath);
}

To find the index area using the binary method, initialize the lookup interval
public void Initialize ()
{
Search_index_set = 0;
Search_index_end = index_count-1;
}

Close File
public void Dispose ()
{
if (bfilepathinitialized)
{
Bfilepathinitialized = false;
Filestrm.close ();
Filestrm.dispose ();
}

}


public bool Setdbfilepath (string Dbfilepath)
{
if (Dbfilepath = "")
{
return false;
}

Try
{
FILESTRM = new FileStream (Dbfilepath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
Catch
{
return false;
}
Check file length
if (Filestrm.length < 8)
{
Filestrm.close ();
Filestrm.dispose ();
return false;
}
Gets the absolute offset of the first index and the absolute offset of the last index
Filestrm.seek (0, Seekorigin.begin);
Index_set = GetUInt32 ();
Index_end = GetUInt32 ();

Get the total number of index bars
Index_count = (index_end-index_set)/7 + 1;
Bfilepathinitialized = true;

return true;

}

public string Getaddresswithip (string ipvalue)
{
if (!bfilepathinitialized)
{
Return "";
}

Initialize ();

UInt32 IP = IPToUInt32 (ipvalue);

while (true)
{

First initialize the interval of this round lookup

Interval Head
Search_set = Indexinfoatpos (Search_index_set);
Interval tail
Search_end = Indexinfoatpos (search_index_end);

Determine whether IP is in the interval header
if (IP >= search_set.ipset && IP <= search_set.ipend)
Return Readaddressinfoatoffset (Search_set.offset);


Determine if IP is in the end of the interval
if (IP >= search_end.ipset && IP <= search_end.ipend)
Return Readaddressinfoatoffset (Search_end.offset);

Calculate the midpoint of the interval
Search_mid = Indexinfoatpos ((search_index_end + search_index_set)/2);

Determine if IP is in midpoint
if (IP >= search_mid.ipset && IP <= search_mid.ipend)
Return Readaddressinfoatoffset (Search_mid.offset);

This round is not found, ready for the next round
if (IP < search_mid.ipset)
The IP is smaller than the midpoint of the interval, and the interval tail is set to the midpoint of the current, narrowing the interval by 1 time times.
Search_index_end = (search_index_end + search_index_set)/2;
Else
The IP is larger than the midpoint of the interval, and the interval head is set to the midpoint now, narrowing the interval 1 time times.
Search_index_set = (search_index_end + search_index_set)/2;
}

Return "";

}

private String Readaddressinfoatoffset (UInt32 Offset)
{
String country = "";
String area = "";
UInt32 country_offset = 0;
byte Tag = 0;
Skips 4 bytes Because these 4 bytes are the upper IP interval limit for the index.
Filestrm.seek (Offset + 4, seekorigin.begin);

Read a byte to get the "addressing way" describing the country information
Tag = Gettag ();

if (Tag = = 0x01)
{

Pattern 0x01, indicating that the next 3 bytes are the offset position
Filestrm.seek (GetOffset (), seekorigin.begin);

Continue to check "addressing mode"
Tag = Gettag ();
if (Tag = = 0x02)
{
Pattern 0x02, representing the offset position of the next 3 bytes representing the country information
First, save the offset position, because we're going to read the region's information behind it.
Country_offset = GetOffset ();
Read the area information (note: According to Luma, there seems to be not so many possibilities, but there seem to be some situations that are not taken into account during the test.
So I wrote a readarea () to read it.
Area = Readarea ();
Read Country information
Filestrm.seek (Country_offset, Seekorigin.begin);
Country = ReadString ();
}
Else
{
This pattern shows that the next step is to save the country and the region information, with the ' Yes ' representative end.
Filestrm.seek ( -1, seekorigin.current);
Country = ReadString ();
Area = Readarea ();

}
}
else if (Tag = = 0x02)
{
Pattern 0x02, indicating that the national information is an offset position
Country_offset = GetOffset ();
Read area information first
Area = Readarea ();
Read Country information
Filestrm.seek (Country_offset, Seekorigin.begin);
Country = ReadString ();
}
Else
{
This model is the simplest, direct reading of countries and regions is OK
Filestrm.seek ( -1, seekorigin.current);
Country = ReadString ();
Area = Readarea ();

}
String address = Country + "" + area;
return address;

}

Private UInt32 GetOffset ()
{
byte[] TempByte4 = new Byte[4];
Tempbyte4[0] = (byte) filestrm.readbyte ();
TEMPBYTE4[1] = (byte) filestrm.readbyte ();
TEMPBYTE4[2] = (byte) filestrm.readbyte ();
TEMPBYTE4[3] = 0;
Return Bitconverter.touint32 (TempByte4, 0);
}

Protected string Readarea ()
{
byte Tag = Gettag ();

if (Tag = = 0x01 | | Tag = = 0x02)
{
Filestrm.seek (GetOffset (), seekorigin.begin);
return ReadString ();
}
Else
{
Filestrm.seek ( -1, seekorigin.current);
return ReadString ();
}
}

Protected string ReadString ()
{
UInt32 Offset = 0;
byte[] Tempbytearray = new byte[256];
Tempbytearray[offset] = (byte) filestrm.readbyte ();
while (Tempbytearray[offset]!= 0x00)
{
Offset + + 1;
Tempbytearray[offset] = (byte) filestrm.readbyte ();
}
Return System.Text.Encoding.Default.GetString (Tempbytearray). TrimEnd (' ");
}

protected byte Gettag ()
{
Return (byte) filestrm.readbyte ();
}

Protected Cz_index_info Indexinfoatpos (UInt32 index_pos)
{
Cz_index_info index_info = new Cz_index_info ();
Calculates the offset in the file, based on the index number
Filestrm.seek (Index_set + 7 * index_pos, Seekorigin.begin);
Index_info.ipset = GetUInt32 ();
Index_info.offset = GetOffset ();
Filestrm.seek (Index_info.offset, Seekorigin.begin);
Index_info.ipend = GetUInt32 ();

return index_info;
}

<summary>
Convert from IP to Int32
</summary>
<param name= "Ipvalue" ></param>
<returns></returns>
Public UInt32 IPToUInt32 (string ipvalue)
{
string[] Ipbyte = Ipvalue.split ('. ');
Int32 nupperbound = ipbyte.getupperbound (0);
if (Nupperbound!= 3)
{
Ipbyte = new String[4];
for (Int32 i = 1; I <= 3-nupperbound; i++)
Ipbyte[nupperbound + i] = "0";
}

byte[] TempByte4 = new Byte[4];
for (Int32 i = 0; I <= 3; i++)
{
' If it is. Net 2.0, you can support TryParse.
' If not (Byte.tryparse (Ipbyte (i), TempByte4 (3-i)) Then
' TempByte4 (3-i) = &h0
' End If
if (IsNumeric (Ipbyte[i]))
Tempbyte4[3-i] = (byte) (Convert.ToInt32 (ipbyte[i)) & 0xff);
}

Return Bitconverter.touint32 (TempByte4, 0);
}

<summary>
Judge whether it is a number
</summary>
<param name= "str" > Waiting to be judged string </param>
<returns></returns>
protected bool IsNumeric (String str)
{
if (str!= null && System.Text.RegularExpressions.Regex.IsMatch (str, @ "^-?\d+$"))
return true;
Else
return false;
}

Protected UInt32 GetUInt32 ()
{
byte[] TempByte4 = new Byte[4];
Filestrm.read (TempByte4, 0, 4);
Return Bitconverter.touint32 (TempByte4, 0);
}
}

}

Need to use IP database, in package. Package Download Http://xiazai.jb51.net/200810/others/iptoaddress.rar
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.