asp.net pure IP library to achieve the code _ practical skills

Source: Internet
Author: User
Knowledge of the Pure IP Library: view (PDF format)
Pure IP Library official download address: http://www.cz88.net/
Class used (already debugged. NET 2.0, errors have been modified):
Copy Code code 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 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 {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) ((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 & 0xffL;
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 iplocation = new Iplocation ();
Iplocation.ip = IP;
Long Intip = Iptoint (IP);
if ((Intip >= iptoint ("127.0.0.1") && (Intip <= ("Iptoint")))
{
Iplocation.country = "Native internal loopback address";
Iplocation.local = "";
}
Else
{
if (((Intip >= iptoint ("0.0.0.0") && (Intip <= iptoint ("2.255.255.255")) | | ((Intip >= iptoint ("64.0.0.0")) && Intip <= ("Iptoint"))) ||
((Intip >= iptoint ("58.0.0.0")) && Intip <= ("Iptoint")))
{
Iplocation.country = "Network reservation 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);
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 for forward accumulation offsets
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 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);
}
}
}
}
}

Use:
Copy Code code 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);

Results:

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.