data.pas
上传用户:hbtcygglw
上传日期:2007-01-07
资源大小:281k
文件大小:5k
- unit data;
- {$define DEBUGVERSION}
- interface
- uses dialogs,windows,SysUtils,forms;
- type
- TIPInfo=record
- StartIp:string[18];
- EndIp:string[18];
- Country:string[16];
- Location:string[54];
- end;
- procedure LoadIPInfo(FileName:String);
- function FindIpInfo(ip:string):TIPInfo;
- procedure SendFakeMsg(SrcId,SrcFace,DestId,DestIp:string;DestPort:WORD;msg:String;aDate,aTime:TDateTime);
- function StrToIp(str:string):DWORD;
- function GetParse(buf:array of char;buflen,StartPos:integer;var s:string):integer;
- function ArrayStrCopy(var buf:array of char;buflen,pos:Integer;s:string):Integer;
- function GetStrFromBuf(buf:array of char;BufLen,nOffset:DWORD):string;
- function ParseStrFromBuf(buf:array of char;buflen,nOffset,flag:DWORD;var str:String):Integer;
- var
- IPInfoCnt:Integer;
- IpInfos:array of TIPInfo;
- nFakeCnt:Integer;
- implementation
- uses info,main,dbf_c;
- function ParseStrFromBuf(buf:array of char;buflen,nOffset,flag:DWORD;var str:String):Integer;
- var
- i:DWORD;
- begin
- i:=nOffset;
- str:='';
- while(i<BufLen)do
- begin
- if((buf[i]=chr($1f)) or (buf[i]=chr($1e)))then Break
- else str:=str+buf[i];
- Inc(i);
- end;
- Result:=i;
- end;
- function GetStrFromBuf(buf:array of char;BufLen,nOffset:DWORD):string;
- var
- i:DWORD;
- begin
- i:=nOffset;
- Result:='';
- while(i<BufLen)do
- begin
- if(buf[i]=chr($1f))then Break
- else Result:=Result+buf[i];
- Inc(i);
- end;
- end;
- function StrToIp(str:string):DWORD;
- var
- s:string;
- l,i,parse:Integer;
- adr:array [0..3] of DWORD;
- begin
- parse:=0;
- l:=length(str);
- for i:=1 to l+1 do
- begin
- if(i<=l) and (str[i]<>'.')then
- begin
- s:=s+str[i];
- end
- else
- begin
- adr[parse]:=StrToIntDef(s,0);
- Inc(parse);
- s:='';
- end;
- end;
- Result:=(adr[0]shl 24)or(adr[1] shl 16) or(adr[2] shl 8) or adr[3];
- end;
- function IPEqu(ip1,ip2:string):Boolean;
- begin
- Result:=(StrToIp(ip1)=StrToIp(ip2));
- end;
- function GetParse(buf:array of char;buflen,StartPos:integer;var s:string):integer;
- var
- i,st:Integer;
- begin
- s:='';
- st:=StartPos;
- Result:=0;
- //remove head $1f and $1e
- for i:=StartPos to buflen-1 do
- begin
- if(not (Ord(buf[i]) in [$1e,$1f]))then Break
- else Inc(st);
- end;
- for i:=st to buflen-1 do
- begin
- if(not (Ord(buf[i]) in [$1e,$1f]))then
- // if(Ord(buf[i])<>$1f)then
- begin
- s:=s+buf[i];
- end
- else
- begin
- Result:=i+1;
- exit;
- end;
- end;
- end;
- function ArrayStrCopy(var buf:array of char;buflen,pos:Integer;s:string):Integer;
- var
- i,l:Integer;
- begin
- Result:=pos;
- l:=length(s);
- for i:=1 to l do
- begin
- if((pos+i-1)<buflen)then buf[pos+i-1]:=s[i];
- Result:=pos+i;
- end;
- end;
- procedure SendFakeMsg(SrcId,SrcFace,DestId,DestIp:String;DestPort:WORD;msg:String;aDate,aTime:TDateTime);
- var
- pBuf:array [0..2048]of char;
- i:Integer;
- begin
- for i:=0 to 2047 do
- begin
- pBuf[i]:=chr(0);
- end;
- Inc(nFakeCnt);
- pBuf[0]:=chr(2);
- pBuf[1]:=chr(0);
- pBuf[2]:=chr(0);
- pBuf[3]:=chr(0);
- pBuf[4]:=chr($80);
- pBuf[5]:=chr(nFakeCnt);
- pBuf[6]:=chr(0);
- pBuf[7]:='0';
- pBuf[8]:='0';
- pBuf[9]:=chr($1f);
- i:=ArrayStrCopy(pBuf,2048,10,SrcId);
- pBuf[i]:=chr($1f);
- i:=ArrayStrCopy(pBuf,2048,i+1,DestId);
- pBuf[i]:=chr($1f);
- pBuf[i+1]:='0';
- pBuf[i+2]:=chr($1f);
- i:=ArrayStrCopy(pBuf,2048,i+3,SrcFace);
- pBuf[i]:=chr($1f);
- i:=ArrayStrCopy(pBuf,2048,i+1,DateToStr(aDate));
- pBuf[i]:=chr($1f);
- i:=ArrayStrCopy(pBuf,2048,i+1,TimeToStr(aTime));
- pBuf[i]:=chr($1f);
- i:=ArrayStrCopy(pBuf,2048,i+1,msg);
- pBuf[i]:=chr($03);
- MainForm.FakeUDP.RemoteHost:=DestIp;
- MainForm.FakeUDP.RemotePort:=DestPort;
- MainForm.FakeUDP.SendBuffer(pBuf,i+1);
- //MainForm.Timer.Enabled:=True;
- end;
- function FindIpInfo(ip:string):TIPInfo;
- var
- sip,eip,ipd:DWORD;
- i:DWORD;
- n:Integer;
- begin
- ipd:=$ffffffff;
- i:=StrToIp(ip);
- for n:=0 to IPInfoCnt-1 do
- begin
- sip:=StrToIp(IPInfos[n].StartIp);
- eip:=StrToIp(IPInfos[n].EndIp);
- if(sip<i) and (eip>i)then
- begin
- if((eip-sip)<ipd)then
- begin
- ipd:=eip-sip;
- Result:=IPInfos[n];
- end;
- end;
- end;
- end;
- {procedure LoadIPInfo(FileName:String);
- var
- i:Integer;
- f:File of TIPInfo;
- begin
- Assign(f,FileName);
- Reset(f);
- IPInfoCnt:=FileSize(f);
- SetLength(IpInfos,IPInfoCnt);
- i:=0;
- while not eof(f) do
- begin
- Read(f,IpInfos[i]);
- Inc(i);
- end;
- CloseFile(f);
- end;}
- procedure LoadIPInfo(FileName:String);
- var
- dbf:TDBF;
- i:Integer;
- begin
- i:=0;
- dbf:=TDBF.Create(Application);
- dbf.TableName:=FileName;
- dbf.Open;
- IPInfoCnt:=dbf.RecordCount;
- SetLength(IpInfos,IPInfoCnt);
- try
- while(not dbf.Eof)do
- begin
- IpInfos[i].StartIp:=dbf.FieldByName('StartIp').AsString;
- IpInfos[i].EndIp:=dbf.FieldByName('EndIp').AsString;
- IpInfos[i].Country:=dbf.FieldByName('Country').AsString;
- IpInfos[i].Location:=dbf.FieldByName('Local').AsString;
- dbf.Next;
- Inc(i);
- // if(i=190)then ShowMessage('break');
- end;
- except
- On e:Exception do
- begin
- ShowMessage(e.Message);
- end;
- end;
- dbf.Close;
- end;
- end.