IP Address | data | Data source A few days ago, a friend said that he had always wanted to have such a program, can be more popular on the network QQwry.Dat as an IP data source to achieve the IP address query display. But the internet has never appeared. NET version of. I think so, then I will take time to write a good, first go to Google go a bit, find the relevant introduction article, understand its format, and then refer to the other language version of the program, a little more than a day, and finally this thing to write well. Dare not to enjoy the/**//******************************************************************.
* * File Name:IPScaner.cs
* * Copyright (c) 2004-2005 Pptech Studio (pptech.net)
* * CREATER:REXSP (msn:yubo@x263.net)
* * Create date:2004-12-27 20:10:28
* * Modifier:
* * Modify Date:
* * description:to Scan the IP location from Qqwry.dat
* * Version:ipscaner 1.0.0
******************************************************************/
Using System;
Using System.IO;
Using System.Collections;
Using System.Text;
Using System.Text.RegularExpressions;
Namespace PPTech.WebSite.BusinessRules
{
/**////<summary>
To scan the IP location from Qqwry.dat
</summary>
public class Ipscaner
{
Private member #region Private member
private string DataPath;
private string IP;
private string country;
private string Local;
Private long firststartip=0;
Private long laststartip=0;
Private FileStream OBJFS = null;
Private long startip=0;
Private long endip=0;
private int countryflag=0;
Private long endipoff=0;
private string Errmsg=null;
#endregion
constructor function #region Constructor
Public Ipscaner ()
{
//
TODO: Add constructor logic here
//
}
#endregion
Public Properties #region Public properties
public string DataPath
{
Set{datapath=value;}
}
public string IP
{
Set{ip=value;}
}
public string Country
{
Get{return Country;}
}
public string Local
{
Get{return Local;
}
public string ErrMsg
{
Get{return errmsg;}
}
#endregion
Search matching data #region search matching data
private int Qqwry ()
{
String pattern = @ "((\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])) ";
Regex objre = new regex (pattern);
Match Objma = Objre.match (IP);
if (!objma.success)
{
this.errmsg= "IP format error";
return 4;
}
Long Ip_int = this. Iptoint (IP);
int nret=0;
if (Ip_int>=iptoint ("127.0.0.0") &&ip_int<=iptoint ("127.255.255.255")
{
this.country= "native internal loopback address";
This.local= "";
Nret=1;
}
else if ((Ip_int>=iptoint ("0.0.0.0") &&ip_int<=iptoint ("2.255.255.255") | | (Ip_int>=iptoint ("64.0.0.0") &&ip_int<=iptoint ("126.255.255.255") | | (Ip_int>=iptoint ("58.0.0.0") &&ip_int<=iptoint ("60.255.255.255"))
{
this.country= "Network reservation Address";
This.local= "";
Nret=1;
}
OBJFS = new FileStream (This.datapath, FileMode.Open, FileAccess.Read);
Try
{
objFS. Seek (0,seekorigin.begin);
objFS. position=0;
byte[] buff = new Byte[8];
objFS. Read (buff,0,8);
firststartip=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256;
laststartip=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256;
Long Recordcount=convert.toint64 ((laststartip-firststartip)/7.0);
if (recordcount<=1)
{
country= "Filedataerror";
objFS. Close ();
return 2;
}
Long Range=recordcount;
Long rangb=0;
Long recno=0;
while (rangb<range-1)
{
recno= (RANGE+RANGB)/2;
This. Getstartip (RECNO);
if (Ip_int==this.startip)
{
RANGB = Recno;
Break
}
if (Ip_int>this.startip)
Rangb=recno;
Else
Range=recno;
}
This. Getstartip (RANGB);
This. Getendip ();
if (this.startip<=ip_int&&this.endip>=ip_int)
{
This. Getcountry ();
This.local=this.local.replace ("We must liberate Taiwan!!! )","");
}
Else
{
nret=3;
this.country= "Unknown";
This.local= "";
}
objFS. Close ();
return nret;
}
Catch
{
return 1;
}
}
#endregion
Convert IP address to int data #region IP address to int data
Private long Iptoint (string IP)
{
char[] dot = new char[]{'. '};
string [] Iparr = IP. Split (dot);
if (iparr.length==3)
Ip=ip+ ". 0";
Iparr=ip. Split (dot);
Long ip_int=0;
Long P1=long. Parse (iparr[0]) *256*256*256;
Long P2=long. Parse (iparr[1]) *256*256;
Long P3=long. Parse (iparr[2]) *256;
Long P4=long. Parse (Iparr[3]);
IP_INT=P1+P2+P3+P4;
return ip_int;
}
#endregion
int converts to ip#region int to IP
private string Inttoip (long Ip_int)
{
Long seg1= (ip_int&0xff000000) >>24;
if (seg1<0)
seg1+=0x100;
Long seg2= (ip_int&0x00ff0000) >>16;
if (seg2<0)
seg2+=0x100;
Long seg3= (IP_INT&0X0000FF00) >>8;
if (seg3<0)
seg3+=0x100;
Long seg4= (IP_INT&0X000000FF);
if (seg4<0)
seg4+=0x100;
String IP=SEG1. ToString () + "." +SEG2. ToString () + "." +seg3. ToString () + "." +seg4. ToString ();
return IP;
}
#endregion
Get start IP range #region get start IP range
Private Long Getstartip (long recno)
{
Long offSet = firststartip+recno*7;
objFS. Seek (Offset,seekorigin.begin);
objFS. Position=offset;
byte [] buff = new BYTE[7];
objFS. Read (buff,0,7);
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.