2008-04-23 20:34:31 +00:00
|
|
|
{==============================================================================|
|
2008-04-24 07:40:57 +00:00
|
|
|
| Project : Ararat Synapse | 003.001.008 |
|
2008-04-23 20:34:31 +00:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: PING sender |
|
|
|
|
|==============================================================================|
|
2008-04-24 07:22:17 +00:00
|
|
|
| Copyright (c)1999-2003, Lukas Gebauer |
|
2008-04-24 07:20:39 +00:00
|
|
|
| All rights reserved. |
|
2008-04-23 20:34:31 +00:00
|
|
|
| |
|
2008-04-24 07:20:39 +00:00
|
|
|
| Redistribution and use in source and binary forms, with or without |
|
|
|
|
| modification, are permitted provided that the following conditions are met: |
|
|
|
|
| |
|
|
|
|
| Redistributions of source code must retain the above copyright notice, this |
|
|
|
|
| list of conditions and the following disclaimer. |
|
|
|
|
| |
|
|
|
|
| Redistributions in binary form must reproduce the above copyright notice, |
|
|
|
|
| this list of conditions and the following disclaimer in the documentation |
|
|
|
|
| and/or other materials provided with the distribution. |
|
|
|
|
| |
|
|
|
|
| Neither the name of Lukas Gebauer nor the names of its contributors may |
|
|
|
|
| be used to endorse or promote products derived from this software without |
|
|
|
|
| specific prior written permission. |
|
|
|
|
| |
|
|
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
|
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
|
|
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
|
|
|
|
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
|
|
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
|
|
|
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
|
|
|
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
|
|
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
|
|
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
|
|
|
| DAMAGE. |
|
2008-04-23 20:34:31 +00:00
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2008-04-24 07:22:17 +00:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c)2000-2003. |
|
2008-04-23 20:34:31 +00:00
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
2008-04-23 20:48:39 +00:00
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
2008-04-23 20:34:31 +00:00
|
|
|
|==============================================================================}
|
|
|
|
|
2008-04-24 07:29:09 +00:00
|
|
|
{:@abstract(ICMP PING implementation.)
|
|
|
|
Allows create PING and TRACEROUTE. Or you can diagnose your network.
|
|
|
|
|
|
|
|
Warning: this unit using RAW sockets. On some systems you must have special
|
|
|
|
rights for using this sort of sockets. So, it working allways when you have
|
|
|
|
administator/root rights. Otherwise you can have problems!
|
|
|
|
|
|
|
|
Note: IPv6 not working under .NET. It is lack of Microsoft's .NET framework.
|
|
|
|
}
|
|
|
|
|
2008-04-24 07:25:18 +00:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE DELPHI}
|
|
|
|
{$ENDIF}
|
2008-04-24 07:00:43 +00:00
|
|
|
{$Q-}
|
2008-04-24 07:23:38 +00:00
|
|
|
{$R-}
|
2008-04-24 07:25:18 +00:00
|
|
|
{$H+}
|
2008-04-24 07:00:43 +00:00
|
|
|
|
2008-04-24 07:40:57 +00:00
|
|
|
{$IFDEF CIL}
|
|
|
|
Sorry, this unit is not for .NET!
|
|
|
|
{$ENDIF}
|
|
|
|
|
2008-04-24 07:25:18 +00:00
|
|
|
unit pingsend;
|
2008-04-23 20:34:31 +00:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-04-24 07:05:26 +00:00
|
|
|
SysUtils,
|
2008-04-24 07:25:18 +00:00
|
|
|
synsock, blcksock, synautil;
|
2008-04-23 20:34:31 +00:00
|
|
|
|
|
|
|
const
|
2008-04-24 07:05:26 +00:00
|
|
|
ICMP_ECHO = 8;
|
|
|
|
ICMP_ECHOREPLY = 0;
|
2008-04-24 07:25:18 +00:00
|
|
|
ICMP_UNREACH = 3;
|
|
|
|
ICMP_TIME_EXCEEDED = 11;
|
|
|
|
//rfc-2292
|
2008-04-24 07:23:38 +00:00
|
|
|
ICMP6_ECHO = 128;
|
|
|
|
ICMP6_ECHOREPLY = 129;
|
2008-04-24 07:25:18 +00:00
|
|
|
ICMP6_UNREACH = 1;
|
|
|
|
ICMP6_TIME_EXCEEDED = 3;
|
2008-04-23 20:34:31 +00:00
|
|
|
|
|
|
|
type
|
2008-04-24 07:29:09 +00:00
|
|
|
{:Record for ICMP ECHO packet header.}
|
2008-04-24 07:05:26 +00:00
|
|
|
TIcmpEchoHeader = record
|
|
|
|
i_type: Byte;
|
|
|
|
i_code: Byte;
|
|
|
|
i_checkSum: Word;
|
|
|
|
i_Id: Word;
|
|
|
|
i_seq: Word;
|
2008-04-24 07:40:57 +00:00
|
|
|
TimeStamp: integer;
|
2008-04-24 07:23:38 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-24 07:29:09 +00:00
|
|
|
{:record used internally by TPingSend for compute checksum of ICMPv6 packet
|
|
|
|
pseudoheader.}
|
2008-04-24 07:23:38 +00:00
|
|
|
TICMP6Packet = record
|
|
|
|
in_source: TInAddr6;
|
|
|
|
in_dest: TInAddr6;
|
|
|
|
Length: integer;
|
|
|
|
free0: Byte;
|
|
|
|
free1: Byte;
|
|
|
|
free2: Byte;
|
|
|
|
proto: Byte;
|
2008-04-24 07:05:26 +00:00
|
|
|
end;
|
2008-04-23 20:34:31 +00:00
|
|
|
|
2008-04-24 07:29:09 +00:00
|
|
|
{:List of possible ICMP reply packet types.}
|
2008-04-24 07:25:18 +00:00
|
|
|
TICMPError = (
|
|
|
|
IE_NoError,
|
|
|
|
IE_Other,
|
|
|
|
IE_TTLExceed,
|
|
|
|
IE_UnreachOther,
|
|
|
|
IE_UnreachRoute,
|
|
|
|
IE_UnreachAdmin,
|
|
|
|
IE_UnreachAddr,
|
|
|
|
IE_UnreachPort
|
|
|
|
);
|
|
|
|
|
2008-04-24 07:29:09 +00:00
|
|
|
{:@abstract(Implementation of ICMP PING and ICMPv6 PING.)
|
|
|
|
|
|
|
|
Note: Are you missing properties for specify server address and port? Look to
|
|
|
|
parent @link(TSynaClient) too!}
|
2008-04-24 07:20:39 +00:00
|
|
|
TPINGSend = class(TSynaClient)
|
2008-04-23 20:34:31 +00:00
|
|
|
private
|
2008-04-24 07:05:26 +00:00
|
|
|
FSock: TICMPBlockSocket;
|
|
|
|
FBuffer: string;
|
|
|
|
FSeq: Integer;
|
|
|
|
FId: Integer;
|
|
|
|
FPacketSize: Integer;
|
|
|
|
FPingTime: Integer;
|
2008-04-24 07:23:38 +00:00
|
|
|
FIcmpEcho: Byte;
|
|
|
|
FIcmpEchoReply: Byte;
|
2008-04-24 07:25:18 +00:00
|
|
|
FIcmpUnreach: Byte;
|
|
|
|
FReplyFrom: string;
|
|
|
|
FReplyType: byte;
|
|
|
|
FReplyCode: byte;
|
|
|
|
FReplyError: TICMPError;
|
|
|
|
FReplyErrorDesc: string;
|
2008-04-24 07:23:38 +00:00
|
|
|
function Checksum(Value: string): Word;
|
|
|
|
function Checksum6(Value: string): Word;
|
2008-04-24 07:05:26 +00:00
|
|
|
function ReadPacket: Boolean;
|
2008-04-24 07:25:18 +00:00
|
|
|
procedure TranslateError;
|
2008-04-23 20:34:31 +00:00
|
|
|
public
|
2008-04-24 07:29:09 +00:00
|
|
|
{:Send ICMP ping to host and count @link(pingtime). If ping OK, result is
|
|
|
|
@true.}
|
2008-04-24 07:05:26 +00:00
|
|
|
function Ping(const Host: string): Boolean;
|
2008-04-23 20:34:31 +00:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2008-04-24 07:05:26 +00:00
|
|
|
published
|
2008-04-24 07:29:09 +00:00
|
|
|
{:Size of PING packet. Default size is 32 bytes.}
|
2008-04-24 07:05:26 +00:00
|
|
|
property PacketSize: Integer read FPacketSize Write FPacketSize;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:Time between request and reply.}
|
2008-04-24 07:05:26 +00:00
|
|
|
property PingTime: Integer read FPingTime;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:From this address is sended reply for your PING request. It maybe not your
|
|
|
|
requested destination, when some error occured!}
|
2008-04-24 07:25:18 +00:00
|
|
|
property ReplyFrom: string read FReplyFrom;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:ICMP type of PING reply. Each protocol using another values! For IPv4 and
|
|
|
|
IPv6 are used different values!}
|
2008-04-24 07:25:18 +00:00
|
|
|
property ReplyType: byte read FReplyType;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:ICMP code of PING reply. Each protocol using another values! For IPv4 and
|
|
|
|
IPv6 are used different values! For protocol independent value look to
|
|
|
|
@link(ReplyError)}
|
2008-04-24 07:25:18 +00:00
|
|
|
property ReplyCode: byte read FReplyCode;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:Return type of returned ICMP message. This value is independent on used
|
|
|
|
protocol!}
|
2008-04-24 07:25:18 +00:00
|
|
|
property ReplyError: TICMPError read FReplyError;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:Return human readable description of returned packet type.}
|
2008-04-24 07:25:18 +00:00
|
|
|
property ReplyErrorDesc: string read FReplyErrorDesc;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:Socket object used for TCP/IP operation. Good for seting OnStatus hook, etc.}
|
2008-04-24 07:07:45 +00:00
|
|
|
property Sock: TICMPBlockSocket read FSock;
|
2008-04-24 07:05:26 +00:00
|
|
|
end;
|
2008-04-23 20:34:31 +00:00
|
|
|
|
2008-04-24 07:29:09 +00:00
|
|
|
{:A very useful function and example of its use would be found in the TPINGSend
|
|
|
|
object. Use it to ping to any host. If successful, returns the ping time in
|
|
|
|
milliseconds. Returns -1 if an error occurred.}
|
2008-04-24 07:05:26 +00:00
|
|
|
function PingHost(const Host: string): Integer;
|
2008-04-24 07:29:09 +00:00
|
|
|
|
|
|
|
{:A very useful function and example of its use would be found in the TPINGSend
|
|
|
|
object. Use it to TraceRoute to any host.}
|
2008-04-24 07:25:18 +00:00
|
|
|
function TraceRouteHost(const Host: string): string;
|
2008-04-23 20:34:31 +00:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-24 07:05:26 +00:00
|
|
|
constructor TPINGSend.Create;
|
2008-04-23 20:34:31 +00:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2008-04-24 07:05:26 +00:00
|
|
|
FSock := TICMPBlockSocket.Create;
|
|
|
|
FTimeout := 5000;
|
|
|
|
FPacketSize := 32;
|
|
|
|
FSeq := 0;
|
|
|
|
Randomize;
|
2008-04-23 20:34:31 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-24 07:05:26 +00:00
|
|
|
destructor TPINGSend.Destroy;
|
2008-04-23 20:34:31 +00:00
|
|
|
begin
|
2008-04-24 07:05:26 +00:00
|
|
|
FSock.Free;
|
|
|
|
inherited Destroy;
|
2008-04-23 20:34:31 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-24 07:05:26 +00:00
|
|
|
function TPINGSend.ReadPacket: Boolean;
|
2008-04-23 20:34:31 +00:00
|
|
|
begin
|
2008-04-24 07:09:13 +00:00
|
|
|
FBuffer := FSock.RecvPacket(Ftimeout);
|
|
|
|
Result := FSock.LastError = 0;
|
2008-04-23 20:34:31 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-24 07:05:26 +00:00
|
|
|
function TPINGSend.Ping(const Host: string): Boolean;
|
|
|
|
var
|
|
|
|
IPHeadPtr: ^TIPHeader;
|
|
|
|
IpHdrLen: Integer;
|
|
|
|
IcmpEchoHeaderPtr: ^TICMPEchoHeader;
|
|
|
|
t: Boolean;
|
2008-04-24 07:25:18 +00:00
|
|
|
x: cardinal;
|
2008-04-24 07:29:09 +00:00
|
|
|
IcmpReqHead: string;
|
2008-04-24 07:05:26 +00:00
|
|
|
begin
|
|
|
|
Result := False;
|
2008-04-24 07:23:38 +00:00
|
|
|
FPingTime := -1;
|
2008-04-24 07:25:18 +00:00
|
|
|
FReplyFrom := '';
|
|
|
|
FReplyType := 0;
|
|
|
|
FReplyCode := 0;
|
|
|
|
FReplyError := IE_NoError;
|
|
|
|
FReplyErrorDesc := '';
|
2008-04-24 07:20:39 +00:00
|
|
|
FSock.Bind(FIPInterface, cAnyPort);
|
2008-04-24 07:05:26 +00:00
|
|
|
FSock.Connect(Host, '0');
|
2008-04-24 07:23:38 +00:00
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
|
|
|
FSock.SizeRecvBuffer := 60 * 1024;
|
|
|
|
if FSock.IP6used then
|
|
|
|
begin
|
|
|
|
FIcmpEcho := ICMP6_ECHO;
|
|
|
|
FIcmpEchoReply := ICMP6_ECHOREPLY;
|
2008-04-24 07:25:18 +00:00
|
|
|
FIcmpUnreach := ICMP6_UNREACH;
|
2008-04-24 07:23:38 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
FIcmpEcho := ICMP_ECHO;
|
|
|
|
FIcmpEchoReply := ICMP_ECHOREPLY;
|
2008-04-24 07:25:18 +00:00
|
|
|
FIcmpUnreach := ICMP_UNREACH;
|
2008-04-24 07:23:38 +00:00
|
|
|
end;
|
2008-04-24 07:25:18 +00:00
|
|
|
FBuffer := StringOfChar(#55, SizeOf(TICMPEchoHeader) + FPacketSize);
|
2008-04-24 07:05:26 +00:00
|
|
|
IcmpEchoHeaderPtr := Pointer(FBuffer);
|
|
|
|
with IcmpEchoHeaderPtr^ do
|
|
|
|
begin
|
2008-04-24 07:23:38 +00:00
|
|
|
i_type := FIcmpEcho;
|
2008-04-24 07:05:26 +00:00
|
|
|
i_code := 0;
|
|
|
|
i_CheckSum := 0;
|
2008-04-24 07:25:18 +00:00
|
|
|
FId := System.Random(32767);
|
2008-04-24 07:05:26 +00:00
|
|
|
i_Id := FId;
|
|
|
|
TimeStamp := GetTick;
|
|
|
|
Inc(FSeq);
|
|
|
|
i_Seq := FSeq;
|
2008-04-24 07:25:18 +00:00
|
|
|
if fSock.IP6used then
|
|
|
|
i_CheckSum := CheckSum6(FBuffer)
|
|
|
|
else
|
|
|
|
i_CheckSum := CheckSum(FBuffer);
|
2008-04-24 07:05:26 +00:00
|
|
|
end;
|
|
|
|
FSock.SendString(FBuffer);
|
2008-04-24 07:29:09 +00:00
|
|
|
// remember first 8 bytes of ICMP packet
|
|
|
|
IcmpReqHead := Copy(FBuffer, 1, 8);
|
2008-04-24 07:25:18 +00:00
|
|
|
x := GetTick;
|
2008-04-24 07:05:26 +00:00
|
|
|
repeat
|
|
|
|
t := ReadPacket;
|
|
|
|
if not t then
|
|
|
|
break;
|
2008-04-24 07:23:38 +00:00
|
|
|
if fSock.IP6used then
|
|
|
|
begin
|
2008-04-24 07:40:57 +00:00
|
|
|
{$IFNDEF WIN32}
|
2008-04-24 07:23:38 +00:00
|
|
|
IcmpEchoHeaderPtr := Pointer(FBuffer);
|
|
|
|
{$ELSE}
|
2008-04-24 07:29:09 +00:00
|
|
|
//WinXP SP1 with networking update doing this think by another way ;-O
|
|
|
|
// FBuffer := StringOfChar(#0, 4) + FBuffer;
|
2008-04-24 07:23:38 +00:00
|
|
|
IcmpEchoHeaderPtr := Pointer(FBuffer);
|
2008-04-24 07:29:09 +00:00
|
|
|
// IcmpEchoHeaderPtr^.i_type := FIcmpEchoReply;
|
2008-04-24 07:23:38 +00:00
|
|
|
{$ENDIF}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
IPHeadPtr := Pointer(FBuffer);
|
|
|
|
IpHdrLen := (IPHeadPtr^.VerLen and $0F) * 4;
|
|
|
|
IcmpEchoHeaderPtr := @FBuffer[IpHdrLen + 1];
|
|
|
|
end;
|
2008-04-24 07:40:57 +00:00
|
|
|
//check for timeout
|
|
|
|
if TickDelta(x, GetTick) > FTimeout then
|
|
|
|
begin
|
|
|
|
t := false;
|
|
|
|
Break;
|
|
|
|
end;
|
2008-04-24 07:29:09 +00:00
|
|
|
//it discard sometimes possible 'echoes' of previosly sended packet
|
|
|
|
//or other unwanted ICMP packets...
|
2008-04-24 07:25:18 +00:00
|
|
|
until (IcmpEchoHeaderPtr^.i_type <> FIcmpEcho)
|
2008-04-24 07:29:09 +00:00
|
|
|
and ((IcmpEchoHeaderPtr^.i_id = FId)
|
|
|
|
or (Pos(IcmpReqHead, FBuffer) > 0));
|
2008-04-24 07:05:26 +00:00
|
|
|
if t then
|
2008-04-24 07:23:38 +00:00
|
|
|
begin
|
2008-04-24 07:25:18 +00:00
|
|
|
FPingTime := TickDelta(x, GetTick);
|
|
|
|
FReplyFrom := FSock.GetRemoteSinIP;
|
|
|
|
FReplyType := IcmpEchoHeaderPtr^.i_type;
|
|
|
|
FReplyCode := IcmpEchoHeaderPtr^.i_code;
|
|
|
|
TranslateError;
|
2008-04-24 07:23:38 +00:00
|
|
|
Result := True;
|
|
|
|
end;
|
2008-04-24 07:05:26 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-24 07:23:38 +00:00
|
|
|
function TPINGSend.Checksum(Value: string): Word;
|
2008-04-23 20:34:31 +00:00
|
|
|
var
|
2008-04-24 07:40:57 +00:00
|
|
|
CkSum: integer;
|
2008-04-24 07:05:26 +00:00
|
|
|
Num, Remain: Integer;
|
2008-04-24 07:25:18 +00:00
|
|
|
n, i: Integer;
|
2008-04-23 20:34:31 +00:00
|
|
|
begin
|
2008-04-24 07:23:38 +00:00
|
|
|
Num := Length(Value) div 2;
|
|
|
|
Remain := Length(Value) mod 2;
|
2008-04-23 20:34:31 +00:00
|
|
|
CkSum := 0;
|
2008-04-24 07:25:18 +00:00
|
|
|
i := 1;
|
2008-04-24 07:05:26 +00:00
|
|
|
for n := 0 to Num - 1 do
|
2008-04-24 07:25:18 +00:00
|
|
|
begin
|
|
|
|
CkSum := CkSum + Synsock.HtoNs(DecodeInt(Value, i));
|
|
|
|
inc(i, 2);
|
|
|
|
end;
|
2008-04-24 07:05:26 +00:00
|
|
|
if Remain <> 0 then
|
2008-04-24 07:23:38 +00:00
|
|
|
CkSum := CkSum + Ord(Value[Length(Value)]);
|
2008-04-24 07:05:26 +00:00
|
|
|
CkSum := (CkSum shr 16) + (CkSum and $FFFF);
|
|
|
|
CkSum := CkSum + (CkSum shr 16);
|
|
|
|
Result := Word(not CkSum);
|
2008-04-23 20:34:31 +00:00
|
|
|
end;
|
|
|
|
|
2008-04-24 07:23:38 +00:00
|
|
|
function TPINGSend.Checksum6(Value: string): Word;
|
|
|
|
const
|
|
|
|
IOC_OUT = $40000000;
|
|
|
|
IOC_IN = $80000000;
|
|
|
|
IOC_INOUT = (IOC_IN or IOC_OUT);
|
|
|
|
IOC_WS2 = $08000000;
|
|
|
|
SIO_ROUTING_INTERFACE_QUERY = 20 or IOC_WS2 or IOC_INOUT;
|
|
|
|
var
|
|
|
|
ICMP6Ptr: ^TICMP6Packet;
|
|
|
|
s: string;
|
|
|
|
b: integer;
|
|
|
|
ip6: TSockAddrIn6;
|
|
|
|
x: integer;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
2008-04-24 07:40:57 +00:00
|
|
|
{$IFDEF WIN32}
|
2008-04-24 07:23:38 +00:00
|
|
|
s := StringOfChar(#0, SizeOf(TICMP6Packet)) + Value;
|
|
|
|
ICMP6Ptr := Pointer(s);
|
|
|
|
x := synsock.WSAIoctl(FSock.Socket, SIO_ROUTING_INTERFACE_QUERY,
|
2008-04-24 07:25:18 +00:00
|
|
|
@FSock.RemoteSin, SizeOf(FSock.RemoteSin),
|
2008-04-24 07:23:38 +00:00
|
|
|
@ip6, SizeOf(ip6), @b, nil, nil);
|
|
|
|
if x <> -1 then
|
|
|
|
ICMP6Ptr^.in_dest := ip6.sin6_addr
|
|
|
|
else
|
2008-04-24 07:25:18 +00:00
|
|
|
ICMP6Ptr^.in_dest := FSock.LocalSin.sin6_addr;
|
|
|
|
ICMP6Ptr^.in_source := FSock.RemoteSin.sin6_addr;
|
2008-04-24 07:23:38 +00:00
|
|
|
ICMP6Ptr^.Length := synsock.htonl(Length(Value));
|
|
|
|
ICMP6Ptr^.proto := IPPROTO_ICMPV6;
|
|
|
|
Result := Checksum(s);
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
|
2008-04-24 07:25:18 +00:00
|
|
|
procedure TPINGSend.TranslateError;
|
|
|
|
begin
|
|
|
|
if fSock.IP6used then
|
|
|
|
begin
|
|
|
|
case FReplyType of
|
|
|
|
ICMP6_ECHOREPLY:
|
|
|
|
FReplyError := IE_NoError;
|
|
|
|
ICMP6_TIME_EXCEEDED:
|
|
|
|
FReplyError := IE_TTLExceed;
|
|
|
|
ICMP6_UNREACH:
|
|
|
|
case FReplyCode of
|
|
|
|
0:
|
|
|
|
FReplyError := IE_UnreachRoute;
|
|
|
|
3:
|
|
|
|
FReplyError := IE_UnreachAddr;
|
|
|
|
4:
|
|
|
|
FReplyError := IE_UnreachPort;
|
|
|
|
1:
|
|
|
|
FReplyError := IE_UnreachAdmin;
|
|
|
|
else
|
|
|
|
FReplyError := IE_UnreachOther;
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
FReplyError := IE_Other;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
case FReplyType of
|
|
|
|
ICMP_ECHOREPLY:
|
|
|
|
FReplyError := IE_NoError;
|
|
|
|
ICMP_TIME_EXCEEDED:
|
|
|
|
FReplyError := IE_TTLExceed;
|
|
|
|
ICMP_UNREACH:
|
|
|
|
case FReplyCode of
|
|
|
|
0:
|
|
|
|
FReplyError := IE_UnreachRoute;
|
|
|
|
1:
|
|
|
|
FReplyError := IE_UnreachAddr;
|
|
|
|
3:
|
|
|
|
FReplyError := IE_UnreachPort;
|
|
|
|
13:
|
|
|
|
FReplyError := IE_UnreachAdmin;
|
|
|
|
else
|
|
|
|
FReplyError := IE_UnreachOther;
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
FReplyError := IE_Other;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
case FReplyError of
|
|
|
|
IE_NoError:
|
|
|
|
FReplyErrorDesc := '';
|
|
|
|
IE_Other:
|
|
|
|
FReplyErrorDesc := 'Unknown error';
|
|
|
|
IE_TTLExceed:
|
|
|
|
FReplyErrorDesc := 'TTL Exceeded';
|
|
|
|
IE_UnreachOther:
|
|
|
|
FReplyErrorDesc := 'Unknown unreachable';
|
|
|
|
IE_UnreachRoute:
|
|
|
|
FReplyErrorDesc := 'No route to destination';
|
|
|
|
IE_UnreachAdmin:
|
|
|
|
FReplyErrorDesc := 'Administratively prohibited';
|
|
|
|
IE_UnreachAddr:
|
|
|
|
FReplyErrorDesc := 'Address unreachable';
|
|
|
|
IE_UnreachPort:
|
|
|
|
FReplyErrorDesc := 'Port unreachable';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 20:34:31 +00:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-24 07:05:26 +00:00
|
|
|
function PingHost(const Host: string): Integer;
|
2008-04-23 20:34:31 +00:00
|
|
|
begin
|
2008-04-24 07:05:26 +00:00
|
|
|
with TPINGSend.Create do
|
2008-04-23 20:34:31 +00:00
|
|
|
try
|
2008-04-24 07:25:18 +00:00
|
|
|
Result := -1;
|
|
|
|
if Ping(Host) then
|
|
|
|
if ReplyError = IE_NoError then
|
|
|
|
Result := PingTime;
|
2008-04-23 20:34:31 +00:00
|
|
|
finally
|
2008-04-24 07:05:26 +00:00
|
|
|
Free;
|
2008-04-23 20:34:31 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 07:25:18 +00:00
|
|
|
function TraceRouteHost(const Host: string): string;
|
|
|
|
var
|
|
|
|
Ping: TPingSend;
|
|
|
|
ttl : byte;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
Ping := TPINGSend.Create;
|
|
|
|
try
|
|
|
|
ttl := 1;
|
|
|
|
repeat
|
|
|
|
ping.Sock.TTL := ttl;
|
|
|
|
inc(ttl);
|
|
|
|
if ttl > 30 then
|
|
|
|
Break;
|
|
|
|
if not ping.Ping(Host) then
|
|
|
|
begin
|
|
|
|
Result := Result + cAnyHost+ ' Timeout' + CRLF;
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
if (ping.ReplyError <> IE_NoError)
|
|
|
|
and (ping.ReplyError <> IE_TTLExceed) then
|
|
|
|
begin
|
|
|
|
Result := Result + Ping.ReplyFrom + ' ' + Ping.ReplyErrorDesc + CRLF;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
Result := Result + Ping.ReplyFrom + ' ' + IntToStr(Ping.PingTime) + CRLF;
|
|
|
|
until ping.ReplyError = IE_NoError;
|
|
|
|
finally
|
|
|
|
Ping.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 20:34:31 +00:00
|
|
|
end.
|