睡觉前我想到了
ICMP干什么的?
跟踪路由嘛?
网上一查,真的有啊
代码:
unit uRoutes;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdTCPConnection, IdTCPClient,
IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient,
StdCtrls;
type
TRoutes = class(TThread)
private
{ Private declarations }
icmpseq : integer;
findhost: string;
hop: string;
pingtime: integer;
FResult: TStringList;
FPublicResult: TStringList;
IdIcmpClient1: TIdIcmpClient;
FHost:string;
FFound:Boolean;
procedure IdIcmpClient1Reply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
function GetRoutes: TStringList;
public
{ Public declarations }
procedure Execute;override;
constructor Create; reintroduce;
destructor Destroy;override;
property Host:string read FHost write FHost;
property Routes:TStringList read FResult ;
property PublicRoutes:TStringList read GetRoutes ;
end;
implementation
uses IdStack, idGlobal,IdException;
constructor TRoutes.Create;
begin
inherited Create(true);
self.FreeOnTerminate:=true;
//self.OnTerminate:=MyTerminate;
FResult:=TStringList.Create;
FPublicResult:=TStringList.Create;
IdIcmpClient1:=TIdIcmpClient.create(nil);
IdIcmpClient1.OnReply:=IdIcmpClient1Reply;
end;
destructor TRoutes.Destroy;
begin
IdIcmpClient1.free;
FPublicResult.free;
FResult.Free;
inherited;
end;
procedure TRoutes.Execute;
var
i: integer;
ResolvedHost: string;
begin
if isValidIP(FHost) then
ResolvedHost:=FHost
else
ResolvedHost := gStack.WSGetHostByName(FHost);
IdIcmpClient1.ReceiveTimeout := 2000;
icmpseq :=30;
for i := 1 to icmpseq do begin
if findhost <> ResolvedHost then
begin
hop:=inttostr(i);
IdIcmpClient1.Host := FHost;
IdIcmpClient1.TTL:=i;
FFound:=False;
IdIcmpClient1.Ping;
IdIcmpClient1.Host:=findhost;
IdIcmpClient1.TTL:=icmpseq;
if (not FFound) and (findhost <> '0.0.0.0') then
IdIcmpClient1.Ping;
if (not FFound) and (findhost <> '0.0.0.0') then
IdIcmpClient1.Ping
else
continue;
if (not FFound)and (findhost <> '0.0.0.0') then
IdIcmpClient1.Ping;
end;
end;
end;
function TRoutes.GetRoutes: TStringList;
var
I:integer;
begin
FPublicResult.Assign(FResult);
for I:=FPublicResult.Count-1 downto 0 do
begin
if pos('192.168.',FPublicResult)<>0 then //不知道这样是不是公网
FPublicResult.Delete(I);
end;
result:=FPublicResult;
end;
procedure TRoutes.IdIcmpClient1Reply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
begin
findhost:=AReplyStatus.FromIpAddress;
if (AReplyStatus.BytesReceived <> 0 ) and (AReplyStatus.SequenceId <> 0) then
begin
FFound:=true;
FResult.append(findhost);
end;
end;
end.