唐朝程序员

我来自唐朝

.NET读取QQWry.Dat 纯真版ip数据库格式数据源

1 /****************************************************************** 
  2 ** File Name:IPScaner.cs 
  3 ** Create Date:2004-12-27 20:10:28 
  4 ** Modifier: 
  5 ** Modify Date: 
  6 ** Description:to scan the ip location from qqwry.dat 
  7 ** Version: IPScaner 1.0.0 
  8 ******************************************************************/ 
  9 using System; 
 10 using System.IO; 
 11 using System.Collections; 
 12 using System.Text; 
 13 using System.Text.RegularExpressions; 
 14 namespace chat.Rules
 15 
 16     /**//// <summary> 
 17     /// to scan the ip location from qqwry.dat 
 18     /// </summary> 
 19     public class IPScaner 
 20     { 
 21         #region 私有成员 
 22         private string dataPath; 
 23         private string ip; 
 24         private string country; 
 25         private string local; 
 26  
 27         private long firstStartIp=0
 28         private long lastStartIp=0
 29         private FileStream objfs = null
 30         private long startIp=0
 31         private long endIp=0
 32         private int countryFlag=0
 33         private long endIpOff=0
 34         private string errMsg=null
 35         #endregion 
 36  
 37         #region 构造函数 
 38             public IPScaner() 
 39             { 
 40                 // 
 41                 // TODO: 在此处添加构造函数逻辑 
 42                 // 
 43             } 
 44         #endregion 
 45  
 46         #region 公共属性 
 47             public string DataPath 
 48             { 
 49                 set{dataPath=value;} 
 50             } 
 51         public string IP 
 52         { 
 53             set{ip=value;} 
 54         } 
 55         public string Country 
 56         { 
 57             get{return country;} 
 58         } 
 59         public string Local 
 60         { 
 61             get{return local;} 
 62         } 
 63         public string ErrMsg 
 64         { 
 65             get{return errMsg;} 
 66         } 
 67         #endregion 
 68  
 69         #region 搜索匹配数据 
 70             private int QQwry() 
 71             { 
 72                 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]))"
 73                 Regex objRe = new Regex(pattern); 
 74                 Match objMa = objRe.Match(ip); 
 75                 if(!objMa.Success) 
 76                 { 
 77                     this.errMsg="IP格式错误"
 78                     return 4
 79                 } 
 80  
 81                 long ip_Int = this.IpToInt(ip); 
 82                 int nRet=0
 83                 if(ip_Int>=IpToInt("127.0.0.0")&&ip_Int<=IpToInt("127.255.255.255")) 
 84                 { 
 85                     this.country="本机内部环回地址"
 86                     this.local=""
 87                     nRet=1
 88                 } 
 89                 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"))) 
 90                 { 
 91                     this.country="网络保留地址"
 92                     this.local=""
 93                     nRet=1
 94                 } 
 95                 objfs =  new FileStream(this.dataPath, FileMode.Open, FileAccess.Read); 
 96                 try 
 97                 { 
 98                     //objfs.Seek(0,SeekOrigin.Begin); 
 99                     objfs.Position=0
100                     byte[] buff = new Byte[8] ; 
101                     objfs.Read(buff,0,8); 
102                     firstStartIp=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256
103                     lastStartIp=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256
104                     long recordCount=Convert.ToInt64((lastStartIp-firstStartIp)/7.0); 
105                     if(recordCount<=1
106                     { 
107                         country="FileDataError"
108                         objfs.Close(); 
109                         return 2
110                     } 
111                     long rangE=recordCount; 
112                     long rangB=0
113                     long recNO=0
114                     while(rangB<rangE-1
115                     { 
116                         recNO=(rangE+rangB)/2
117                         this.GetStartIp(recNO); 
118                         if(ip_Int==this.startIp) 
119                         { 
120                             rangB = recNO; 
121                             break
122                         } 
123                         if(ip_Int>this.startIp) 
124                             rangB=recNO; 
125                         else 
126                             rangE=recNO; 
127                     } 
128                     this.GetStartIp(rangB); 
129                     this.GetEndIp(); 
130                     if(this.startIp<=ip_Int&&this.endIp>=ip_Int) 
131                     { 
132                         this.GetCountry(); 
133                         this.local=this.local.Replace("(我们一定要解放台湾!!!)",""); 
134                     } 
135                     else 
136                     { 
137                         nRet=3
138                         this.country="未知"
139                         this.local=""
140                     } 
141                     objfs.Close(); 
142                     return nRet; 
143                 } 
144                 catch 
145                 { 
146                     return 1
147                 } 
148  
149             } 
150         #endregion 
151  
152         #region IP地址转换成Int数据 
153             private long IpToInt(string ip) 
154             { 
155                 char[] dot = new char[]{'.'}; 
156                 string [] ipArr = ip.Split(dot); 
157                 if(ipArr.Length==3
158                     ip=ip+".0"
159                 ipArr=ip.Split(dot); 
160  
161                 long  ip_Int=0
162                 long p1=long.Parse(ipArr[0])*256*256*256
163                 long p2=long.Parse(ipArr[1])*256*256
164                 long p3=long.Parse(ipArr[2])*256
165                 long p4=long.Parse(ipArr[3]); 
166                 ip_Int=p1+p2+p3+p4; 
167                 return ip_Int; 
168             } 
169         #endregion 
170  
171         #region int转换成IP 
172             private string  IntToIP(long ip_Int) 
173             { 
174                 long seg1=(ip_Int&0xff000000)>>24
175                 if(seg1<0
176                     seg1+=0x100
177                 long seg2=(ip_Int&0x00ff0000)>>16
178                 if(seg2<0
179                     seg2+=0x100
180                 long seg3=(ip_Int&0x0000ff00)>>8
181                 if(seg3<0
182                     seg3+=0x100
183                 long seg4=(ip_Int&0x000000ff); 
184                 if(seg4<0
185                     seg4+=0x100
186                 string ip=seg1.ToString()+"."+seg2.ToString()+"."+seg3.ToString()+"."+seg4.ToString(); 
187  
188                 return ip; 
189             } 
190         #endregion 
191  
192         #region 获取起始IP范围 
193             private long GetStartIp(long recNO) 
194             { 
195                 long offSet = firstStartIp+recNO*7
196                 //objfs.Seek(offSet,SeekOrigin.Begin); 
197                 objfs.Position=offSet; 
198                 byte [] buff = new Byte[7]; 
199                 objfs.Read(buff,0,7); 
200  
201                 endIpOff=Convert.ToInt64(buff[4].ToString())+Convert.ToInt64(buff[5].ToString())*256+Convert.ToInt64(buff[6].ToString())*256*256
202                 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
203                 return startIp; 
204             } 
205         #endregion 
206  
207         #region 获取结束IP 
208             private long GetEndIp() 
209             { 
210                 //objfs.Seek(endIpOff,SeekOrigin.Begin); 
211                 objfs.Position=endIpOff; 
212                 byte [] buff = new Byte[5]; 
213                 objfs.Read(buff,0,5); 
214                 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
215                 this.countryFlag=buff[4]; 
216                 return this.endIp; 
217             } 
218         #endregion 
219  
220         #region 获取国家/区域偏移量 
221                  private string GetCountry() 
222                  { 
223                      switch(this.countryFlag) 
224                      { 
225                          case 1
226                          case 2
227                              this.country=GetFlagStr(this.endIpOff+4); 
228                              this.local=1 == this.countryFlag )?" ":this.GetFlagStr(this.endIpOff+8); 
229                              break
230                          default
231                              this.country=this.GetFlagStr(this.endIpOff+4); 
232                              this.local=this.GetFlagStr(objfs.Position); 
233                              break
234                      } 
235                      return " "
236                  } 
237         #endregion 
238  
239         #region 获取国家/区域字符串 
240                  private string GetFlagStr(long offSet) 
241                  { 
242                      int flag=0
243                      byte [] buff = new Byte[3]; 
244                      while(1==1
245                      { 
246                          //objfs.Seek(offSet,SeekOrigin.Begin); 
247                          objfs.Position=offSet; 
248                          flag = objfs.ReadByte(); 
249                          if(flag==1||flag==2
250                          { 
251                              objfs.Read(buff,0,3); 
252                              if(flag==2
253                              { 
254                                  this.countryFlag=2
255                                  this.endIpOff=offSet-4
256                              } 
257                              offSet=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256
258                          } 
259                          else 
260                          { 
261                              break
262                          } 
263                      } 
264                      if(offSet<12
265                          return " "
266                      objfs.Position=offSet; 
267                      return GetStr(); 
268                  } 
269         #endregion 
270  
271         #region GetStr 
272             private string GetStr() 
273             { 
274                 byte lowC=0
275                 byte upC=0
276                 string str=""
277                 byte[] buff =  new byte[2]; 
278                 while(1==1
279                 { 
280                     lowC= (Byte)objfs.ReadByte(); 
281                     if(lowC==0
282                         break
283                     if(lowC>127
284                     { 
285                         upC=(byte)objfs.ReadByte(); 
286                         buff[0]=lowC; 
287                         buff[1]=upC; 
288                         System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312"); 
289                         str+=enc.GetString(buff); 
290                     } 
291                     else 
292                     { 
293                         str+=(char)lowC; 
294                     } 
295                 } 
296                 return str; 
297             }     
298         #endregion 
299  
300         #region 获取IP地址 
301             public string IPLocation() 
302             { 
303                 this.QQwry(); 
304                 return this.country+this.local; 
305             } 
306         public string IPLocation(string dataPath,string ip) 
307         { 
308             this.dataPath=dataPath; 
309             this.ip=ip; 
310             this.QQwry(); 
311             return  this.country+this.local; 
312         } 
313         #endregion 
314  
315 
316     } 
317 
调用方式:
 1 #region 测试地址搜索 
 2             IPScaner objScan = new IPScaner(); 
 3             string ip="221.224.205.13"
 4             objScan.DataPath=@"E:\QQWry.Dat"
 5             objScan.IP=ip; 
 6             string addre=objScan.IPLocation(); 
 7             string err=objScan.ErrMsg; 
 8  
 9 #endregion
10 

posted on 2007-01-10 20:58  唐朝程序员  阅读(736)  评论(0编辑  收藏  举报

导航