Asp.net pure IP library obtains the local implementation code

Source: Internet
Author: User

Knowledge about pure IP database: View (PDF)
Pure IP library official: http://www.cz88.net/
Used class (. NET 2.0 has been debugged and has been modified by mistake ): CopyCode The Code is as follows: using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. Text. regularexpressions;

Namespace qqwry. net
{

Public class iplocation
{
Private string _ IP;
Private string _ country;
Private string _ local;
Public String IP
{Get {return _ IP ;}
Set {_ IP = value ;}
}
Public String country
{Get {return _ country ;}
Set {_ Country = value ;}
}
Public String local
{Get {return _ local ;}
Set {_ local = value ;}
}
}
Public class qqwrylocator
{
Private byte [] data;
RegEx = new RegEx (@ "(\ D {1, 2}) | (1 \ D {2}) | (2 [0-4] \ D) | (25 [0-5]) \.) {3} (\ D {1, 2}) | (1 \ D {2}) | (2 [0-4] \ D) | (25 [0-5]) ");
Long firststartipoffset;
Long laststartipoffset;
Long ipcount;
Public Long Count {get {return ipcount ;}}
Public qqwrylocator (string datapath)
{
Using (filestream FS = new filestream (datapath, filemode. Open, fileaccess. Read, fileshare. Read ))
{
Data = new byte [fs. Length];
FS. Read (data, 0, Data. Length );
}
Byte [] buffer = new byte [8];
Array. Copy (data, 0, buffer, 0, 8 );
Firststartipoffset = (buffer [0] + (buffer [1] * 0x100) + (buffer [2] * 0x100) * 0x100 )) + (buffer [3] * 0x100) * 0x100) * 0x100 );
Laststartipoffset = (buffer [4] + (buffer [5] * 0x100) + (buffer [6] * 0x100) * 0x100 )) + (buffer [7] * 0x100) * 0x100) * 0x100 );
Ipcount = convert. toint64 (double) (laststartipoffset-firststartipoffset)/7.0 ));

If (ipcount <= 1l)
{
Throw new argumentexception ("ip filedataerror ");
}
}
Private Static long iptoint (string IP)
{
Char [] separator = new char [] {'.'};
If (IP. Split (separator). Length = 3)
{
IP = IP + ". 0 ";
}
String [] strarray = IP. Split (separator );
Long num2 = (long. parse (strarray [0]) * 0x100l) * 0x100l) * 0x100l;
Long num3 = (long. parse (strarray [1]) * 0x100l) * 0x100l;
Long num4 = long. parse (strarray [2]) * 0x100l;
Long num5 = long. parse (strarray [3]);
Return (num2 + num3) + num4) + num5 );
}
Private Static string inttoip (long ip_int)
{
Long num = (long) (ip_int & 0xff000000l)> 0x18 );
If (Num <0l)
{
Num + = 0x100l;
}
Long num2 = (ip_int & 0xff0000l)> 0x10;
If (num2 <0l)
{
Num2 + = 0x100l;
}
Long num3 = (ip_int & 0xff00l)> 8;
If (num3 <0l)
{
Num3 + = 0x100l;
}
Long num4 = ip_int & 0 xffl;
If (num4 <0l)
{
Num4 + = 0x100l;
}
Return (Num. tostring () + "." + num2.tostring () + "." + num3.tostring () + "." + num4.tostring ());
}
Public iplocation query (string IP)
{
If (! RegEx. Match (IP). Success)
{
Throw new argumentexception ("IP Format error ");
}
Iplocation = new iplocation ();
Iplocation. IP = IP;
Long intip = iptoint (IP );
If (intip> = iptoint ("127.0.0.1") & (intip <= iptoint ("127.20.255 "))))
{
Iplocation. Country = "Local internal loopback address ";
Iplocation. Local = "";
}
Else
{
If (intip> = iptoint ("0.0.0.0") & (intip <= iptoint ("2.20.0000255 "))) | (intip> = iptoint ("64.0.0.0") & (intip <= iptoint ("126.255.255.255") |
(Intip> = iptoint ("58.0.0.0") & (intip <= iptoint ("60.00000000255 "))))
{
Iplocation. Country = "network reserved address ";
Iplocation. Local = "";
}
}
Long right = ipcount;
Long left = 0l;
Long Middle = 0l;
Long startip = 0l;
Long endipoff = 0l;
Long endip = 0l;
Int countryflag = 0;
While (left <(right-1l ))
{
Middle = (right + Left)/2L;
Startip = getstartip (middle, out endipoff );
If (intip = startip)
{
Left = middle;
Break;
}
If (intip> startip)
{
Left = middle;
}
Else
{
Right = middle;
}
}
Startip = getstartip (left, out endipoff );
Endip = getendip (endipoff, out countryflag );
If (startip <= intip) & (endip> = intip ))
{
String local;
Iplocation. Country = getcountry (endipoff, countryflag, out local );
Iplocation. Local = local;
}
Else
{
Iplocation. Country = "unknown ";
Iplocation. Local = "";
}
Return iplocation;
}
Private long getstartip (long left, out long endipoff)
{
Long leftoffset = firststartipoffset + (left * 7l );
Byte [] buffer = new byte [7];
Array. Copy (data, leftoffset, buffer, 0, 7 );
Endipoff = (convert. toint64 (buffer [4]. tostring () + (convert. toint64 (buffer [5]. tostring () * 0x100l) + (convert. toint64 (buffer [6]. tostring () * 0x100l) * 0x100l );
Return (convert. toint64 (buffer [0]. tostring () + (convert. toint64 (buffer [1]. tostring () * 0x100l) + (convert. toint64 (buffer [2]. tostring () * 0x100l) * 0x100l) + (convert. toint64 (buffer [3]. tostring () * 0x100l) * 0x100l) * 0x100l );
}
Private long getendip (long endipoff, out int countryflag)
{
Byte [] buffer = new byte [5];
Array. Copy (data, endipoff, buffer, 0, 5 );
Countryflag = buffer [4];
Return (convert. toint64 (buffer [0]. tostring () + (convert. toint64 (buffer [1]. tostring () * 0x100l) + (convert. toint64 (buffer [2]. tostring () * 0x100l) * 0x100l) + (convert. toint64 (buffer [3]. tostring () * 0x100l) * 0x100l) * 0x100l );
}
/// <Summary>
/// Gets the country.
/// </Summary>
/// <Param name = "endipoff"> the end IP off. </param>
/// <Param name = "countryflag"> the country flag. </param>
/// <Param name = "local"> the local. </param>
/// <Returns> country </returns>
Private string getcountry (long endipoff, int countryflag, out string local)
{
String Country = "";
Long offset = endipoff + 4l;
Switch (countryflag)
{
Case 1:
Case 2:
Country = getflagstr (ref offset, ref countryflag, ref endipoff );
Offset = endipoff + 8l;
Local = (1 = countryflag )? "": Getflagstr (ref offset, ref countryflag, ref endipoff );
Break;
Default:
Country = getflagstr (ref offset, ref countryflag, ref endipoff );
Local = getflagstr (ref offset, ref countryflag, ref endipoff );
Break;
}
Return country;
}
Private string getflagstr (ref long offset, ref int countryflag, ref long endipoff)
{
Int flag = 0;
Byte [] buffer = new byte [3];

While (true)
{
// Used to accumulate the offset forward.
Long forwardoffset = offset;
Flag = data [forwardoffset ++];
// No redirection
If (flag! = 1 & flag! = 2)
{
Break;
}
Array. Copy (data, forwardoffset, buffer, 0, 3 );
Forwardoffset + = 3;
If (flag = 2)
{
Countryflag = 2;
Endipoff = offset-4l;
}
Offset = (convert. toint64 (buffer [0]. tostring () + (convert. toint64 (buffer [1]. tostring () * 0x100l) + (convert. toint64 (buffer [2]. tostring () * 0x100l) * 0x100l );
}
If (offset <12l)
{
Return "";
}
Return getstr (ref offset );
}
Private string getstr (ref long offset)
{
Byte lowbyte = 0;
Byte highbyte = 0;
Stringbuilder = new stringbuilder ();
Byte [] bytes = new byte [2];
Encoding encoding = encoding. getencoding ("gb2312 ");
While (true)
{
Lowbyte = data [offset ++];
If (lowbyte = 0)
{
Return stringbuilder. tostring ();
}
If (lowbyte> 0x7f)
{
Highbyte = data [offset ++];
Bytes [0] = lowbyte;
Bytes [1] = highbyte;
If (highbyte = 0)
{
Return stringbuilder. tostring ();
}
Stringbuilder. append (encoding. getstring (bytes ));
}
Else
{
Stringbuilder. append (char) lowbyte );
}
}
}
}
}

Usage:Copy codeThe Code is as follows: qqwry. net. qqwrylocator qqwry = new qqwry. net. qqwrylocator (server. mappath ("qqwry. dat "));
Qqwry. net. iplocation IP = qqwry. Query ("120.67.217.7 ");
Response. Write (IP. IP + "<br/> ");
Response. Write (IP. Country + "<br/> ");
Response. Write (IP. Local );

Result:

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.