Implementing IP Address retrieval with QQWry.Dat as data source [C #]

Source: Internet
Author: User
Tags constructor get ip split tostring
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&AMP;0X0000FF00) >>8;
if (seg3<0)
seg3+=0x100;
Long seg4= (IP_INT&AMP;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);

Endipoff=convert.toint64 (Buff[4]. ToString ()) +convert.toint64 (Buff[5]. ToString ()) *256+convert.toint64 (Buff[6]. ToString ()) *256*256;
Startip=convert.toint64 (Buff[0]. ToString ()) +convert.toint64 (Buff[1]. ToString ()) *256+convert.toint64 (buff[2]. ToString ()) *256*256+convert.toint64 (Buff[3]. ToString ()) *256*256*256;
return startip;
}
#endregion

Get end ip#region Get end IP
Private Long Getendip ()
{
objFS. Seek (Endipoff,seekorigin.begin);
objFS. Position=endipoff;
byte [] buff = new BYTE[5];
objFS. Read (buff,0,5);
This.endip=convert.toint64 (Buff[0]. ToString ()) +convert.toint64 (Buff[1]. ToString ()) *256+convert.toint64 (buff[2]. ToString ()) *256*256+convert.toint64 (Buff[3]. ToString ()) *256*256*256;
THIS.COUNTRYFLAG=BUFF[4];
return THIS.ENDIP;
}
#endregion

Get Country/region offset #region Get country/region offset
private String Getcountry ()
{
Switch (This.countryflag)
{
Case 1:
Case 2:
This.country=getflagstr (THIS.ENDIPOFF+4);
this.local= (1 = this.countryflag)? " ": this. Getflagstr (THIS.ENDIPOFF+8);
Break
Default
This.country=this. Getflagstr (THIS.ENDIPOFF+4);
This.local=this. Getflagstr (OBJFS. Position);
Break
}
Return "";
}
#endregion

Get country/region string #region Get country/region string
private string Getflagstr (long OffSet)
{
int flag=0;
byte [] buff = new BYTE[3];
while (1==1)
{
objFS. Seek (Offset,seekorigin.begin);
objFS. Position=offset;
Flag = objFS. ReadByte ();
if (flag==1| | flag==2)
{
objFS. Read (buff,0,3);
if (flag==2)
{
this.countryflag=2;
this.endipoff=offset-4;
}
Offset=convert.toint64 (Buff[0]. ToString ()) +convert.toint64 (Buff[1]. ToString ()) *256+convert.toint64 (buff[2]. ToString ()) *256*256;
}
Else
{
Break
}
}
if (offset<12)
Return "";
objFS. Position=offset;
return Getstr ();
}
#endregion

Getstr#region Getstr
private String Getstr ()
{
BYTE lowc=0;
BYTE upc=0;
String Str= "";
byte[] buff = new byte[2];
while (1==1)
{
Lowc= (Byte) objfs. ReadByte ();
if (lowc==0)
Break
if (lowc>127)
{
Upc= (Byte) objfs. ReadByte ();
BUFF[0]=LOWC;
BUFF[1]=UPC;
System.Text.Encoding enc = System.Text.Encoding.GetEncoding ("GB2312");
Str+=enc. GetString (Buff);
}
Else
{
str+= (char) LOWC;
}
}
return str;
}
#endregion

Get IP address #region get IP address
public string iplocation ()
{
This. Qqwry ();
return this.country+this.local;
}
public string iplocation (String datapath,string IP)
{
This.datapath=datapath;
This.ip=ip;
This. Qqwry ();
return this.country+this.local;
}
#endregion


}
}

Call Mode:
Test address search #region Test address search
Ipscaner Objscan = new Ipscaner ();
String ip= "221.224.205.13";
objscan.datapath=@ "e:\ personal Data \imtools\qqwryupdate\qqwry.dat";
Objscan.ip=ip;
String Addre=objscan.iplocation ();
String err=objscan.errmsg;

#endregion


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.