Obtain the local code from the IP address in the Asp.net DZ forum.

Source: Internet
Author: User

Usage: ipsearch. getaddresswithip ("202.96.128.167 ")
CS Code Copy code The Code is 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;

// Imported namespace
Using system. IO;

/// <Summary>
/// Determines the IP address attribution 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 search results
/// </Summary>
/// <Param name = "ipvalue"> IP address to be searched </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 class used to save 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 );
}

// Use the binary method to locate the index area and initialize the search range
Public void initialize ()
{
Search_index_set = 0;
Search_index_end = index_count-1;
}

// Close the 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 the file length
If (filestrm. Length <8)
{
Filestrm. Close ();
// Filestrm. Dispose ();
Return false;
}
// Obtain 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 ();

// Obtain the total number of Indexes
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)
{

// Initialize the interval of the current round of search.

// Interval Header
Search_set = indexinfoatpos (search_index_set );
// End of the Interval
Search_end = indexinfoatpos (search_index_end );

// Determine whether the IP address is in the range header
If (ip> = search_set.ipset & IP <= search_set.ipend)
Return readaddressinfoatoffset (search_set.offset );

// Determine whether the IP address is at 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 whether the IP address is in the midpoint
If (ip> = search_mid.ipset & IP <= search_mid.ipend)
Return readaddressinfoatoffset (search_mid.offset );

// This round is not found. Prepare for the next round
If (IP <search_mid.ipset)
// The IP address is smaller than the midpoint of the interval, and the end of the interval is set to the current midpoint, Which is doubled.
Search_index_end = (search_index_end + search_index_set)/2;
Else
// The IP address is larger than the midpoint of the interval. Set the header to the current midpoint and reduce the interval by one time.
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;
// Skip 4 bytes because these 4 bytes are the maximum IP address range of the index.
Filestrm. Seek (Offset + 4, seekorigin. Begin );

// Read one byte to obtain the "addressing mode" that describes the country information"
Tag = gettag ();

If (TAG = 0x01)
{

// Mode 0x01, indicating that the next three bytes represent the offset position
Filestrm. Seek (getoffset (), seekorigin. Begin );

// Continue to check "addressing mode"
Tag = gettag ();
If (TAG = 0x02)
{
// Mode 0x02, indicating that the next three bytes represent the offset position of the country information
// Save the offset first because we want to read the region information behind it.
Country_offset = getoffset ();
// Read region information (Note: According to Luma's instructions, there seems to be no such possibility, but some situations are not taken into account during the test,
// Write a readarea () for reading.
Area = readarea ();
// Read country information
Filestrm. Seek (country_offset, seekorigin. Begin );
Country = readstring ();
}
Else
{
// This mode indicates that the information of the saved country and region is followed by '\ 0.
Filestrm. Seek (-1, seekorigin. Current );
Country = readstring ();
Area = readarea ();

}
}
Else if (TAG = 0x02)
{
// Mode 0x02, indicating that the country information is an offset
Country_offset = getoffset ();
// Read the region information first
Area = readarea ();
// Read country information
Filestrm. Seek (country_offset, seekorigin. Begin );
Country = readstring ();
}
Else
{
// This mode is the simplest. it is OK to directly read the country and region.
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 [1, 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 ('\ 0 ');
}

Protected byte gettag ()
{
Return (byte) filestrm. readbyte ();
}

Protected cz_index_info indexinfoatpos (uint32 index_pos)
{
Cz_index_info index_info = new cz_index_info ();
// Calculate the offset position 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>
/// Determine whether it is a number
/// </Summary>
/// <Param name = "str"> string to be judged </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 );
}
}

}

IP database is required, which is included in the package. Package download http://xiazai.jb51.net/200810/others/iptoaddress.rar

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.