1)
Kopier og gem den følgende unit :
interface
uses
Classes, Sysutils, IdIcmpClient, IdAntiFreeze, IdHTTP;
type
TDNSResolver = class
private
FTarget: string;
FTraceList: TStringList;
FMaxHops: Integer;
FTraceLog: TStringList;
ResolvedHost: string;
IdIcmpClient: TIdIcmpClient;
IdAntiFreeze1: TIdAntiFreeze;
procedure SetTarget(const Value: string);
procedure SetMaxHops(const Value: Integer);
procedure Trace;
public
constructor Create;
destructor Destroy; override;
function Execute: Boolean;
function PingHost(Host: string; TTL: Integer): Boolean;
published
property Target: string read FTarget write SetTarget;
property TraceList: TStringList read FTraceList;
property TraceLog: TStringList read FTraceLog;
property MaxHops: Integer read FMaxHops write SetMaxHops default 30;
end;
THttpCheckSum = class
private
FTarget: string;
procedure SetTarget(const Value: string);
public
function Execute: string;
property Target: string read FTarget write SetTarget;
end;
implementation
uses
idStack, IdException, MD5;
{ TDNSResolver }
constructor TDNSResolver.Create;
begin
inherited;
FTraceList := TStringList.Create;
FTraceLog := TStringList.Create;
FMaxHops := 30;
FTarget := ''; //localhost
IdIcmpClient := TIdIcmpClient.Create(nil);
IdAntiFreeze1 := TIdAntiFreeze.Create(nil);
end;
destructor TDNSResolver.Destroy;
begin
FreeAndNil(FTraceList);
FreeAndNil(FTraceLog);
FreeAndNil(IdIcmpClient);
FreeAndNil(IdAntiFreeze1);
inherited;
end;
function TDNSResolver.Execute: Boolean;
begin
Result := False;
FTraceList.Clear;
FTraceLog.Clear;
FTraceLog.Add(Format('resolving %s', [Target]));
try
ResolvedHost := gStack.WSGetHostByName(Target);
Result := True;
FTraceLog.Add(format('%s resolved to %s', [Target, ResolvedHost]));
except
on e: EIdSocketError do
FTraceLog.Text := FTraceLog.Text + e.message;
end;
if Result then
begin
PingHost(ResolvedHost, MaxHops);
Trace;
end;
end;
function TDNSResolver.PingHost(Host: string; TTL: Integer): Boolean;
begin
Result := False;
FTraceLog.Add(Format('Ping %s', [Host]));
IdIcmpClient.Host := Host;
IdIcmpClient.TTL := TTL;
IdIcmpClient.ReceiveTimeout := 5000;
IdIcmpClient.Ping;
with IdIcmpClient, ReplyStatus do
case IdIcmpClient.ReplyStatus.ReplyStatusType of
rsEcho:
begin
FTraceLog.Add(Format('response from host %s in %d millisec.', [FromIpAddress, MsRoundTripTime]));
Result := True;
end;
rsError:
FTraceLog.Add('Unknown error.');
rsTimeOut:
FTraceLog.Add('Timed out.');
rsErrorUnreachable:
FTraceLog.Add(format('Host %s reports destination network unreachable.', [FromIpAddress]));
rsErrorTTLExceeded:
FTraceLog.Add(format('Hope %d %s: TTL expired.', [TTL, FromIpAddress]));
end; // case
end;
procedure TDNSResolver.SetMaxHops(const Value: Integer);
begin
FMaxHops := Value;
end;
procedure TDNSResolver.SetTarget(const Value: string);
begin
FTarget := Value;
end;
procedure TDNSResolver.Trace;
var
TTL: Integer;
Reached: Boolean;
aItem: string;
begin
TTL := 0;
reached := False;
FTraceList.Clear;
repeat
inc(TTL);
IdIcmpClient.Host := ResolvedHost;
IdIcmpClient.TTL := TTL;
IdIcmpClient.ReceiveTimeout := 5000;
IdIcmpClient.Ping;
aItem := '';
case IdIcmpClient.ReplyStatus.ReplyStatusType of
rsEcho:
begin
aItem := IdIcmpClient.ReplyStatus.FromIpAddress + #9 + format('Reached in : %d ms', [IdIcmpClient.ReplyStatus.MsRoundTripTime]);
reached := True;
end;
rsError:
begin
aItem := IdIcmpClient.ReplyStatus.FromIpAddress + #9 + 'Unknown error.';
end;
rsTimeOut:
begin
aItem := '?.?.?.?'#9'Timed out.';
end;
rsErrorUnreachable:
begin
aItem := IdIcmpClient.ReplyStatus.FromIpAddress + #9 + format('Destination network unreachable', [IdIcmpClient.ReplyStatus.MsRoundTripTime]);
break;
end;
rsErrorTTLExceeded:
begin
aItem := IdIcmpClient.ReplyStatus.FromIpAddress + #9 + format('TTL=%d', [IdIcmpClient.ReplyStatus.TimeToLive]);
end;
end; // case
FTraceList.Add(IntToStr(TTL) + #9 + aItem);
until reached or (TTL > MaxHops);
end;
{ THttpCheckSum }
function THttpCheckSum.Execute: string;
begin
with TIdHTTP.Create(nil) do
try
Result := StrMd5(Get(Target));
finally
free;
end;
end;
procedure THttpCheckSum.SetTarget(const Value: string);
begin
if not SameText(copy(Value, 1, 7), '
http://') then
FTarget := '
http://' + Value
else
FTarget := Value;
end;
end.
2)
Smid en knap, et Edit felt (EditIP), og et Memo på din form.
3)
Prøv så det her :
with TDNSResolver.Create do
try
Target := EditIP.Text;
Execute;
Memo1.Text := ' === Trace Log === ';
Memo1.Lines.AddStrings(TraceLog);
Memo1.Lines.Add('');
Memo1.Lines.Add(' === Trace List ===');
Memo1.Lines.AddStrings(TraceList);
finally
free;
end;
Jens B