From 65ae6909208257e51450be7ec724fd54aedfeba4 Mon Sep 17 00:00:00 2001 From: Remy Lebeau Date: Mon, 11 Sep 2023 10:48:33 -0700 Subject: [PATCH] Updating TIdIcmpClient.Ping() to let users pass in a TIdBytes instead of a String for user-defined data. --- Lib/Core/IdIcmpClient.pas | 38 ++++++++++++++++++++------------------ Lib/Core/IdTraceRoute.pas | 2 +- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Lib/Core/IdIcmpClient.pas b/Lib/Core/IdIcmpClient.pas index 853010e26..0d6bead53 100644 --- a/Lib/Core/IdIcmpClient.pas +++ b/Lib/Core/IdIcmpClient.pas @@ -166,17 +166,17 @@ TIdCustomIcmpClient = class(TIdRawClient) procedure GetEchoReply; procedure InitComponent; override; {$IFNDEF DOTNET_1_1} - procedure PrepareEchoRequestIPv6(const ABuffer: String); + procedure PrepareEchoRequestIPv6(const ABuffer: TIdBytes); {$ENDIF} - procedure PrepareEchoRequestIPv4(const ABuffer: String); - procedure PrepareEchoRequest(const ABuffer: String); + procedure PrepareEchoRequestIPv4(const ABuffer: TIdBytes); + procedure PrepareEchoRequest(const ABuffer: TIdBytes); procedure SendEchoRequest; overload; procedure SendEchoRequest(const AIP : String); overload; function GetPacketSize: Integer; procedure SetPacketSize(const AValue: Integer); //these are made public in the client - procedure InternalPing(const AIP : String; const ABuffer: String = ''; SequenceID: Word = 0); overload; {Do not Localize} + procedure InternalPing(const AIP : String; const ABuffer: TIdBytes = nil; SequenceID: Word = 0); overload; // property PacketSize : Integer read GetPacketSize write SetPacketSize default DEF_PACKET_SIZE; property ReplyData: string read FReplydata; @@ -193,7 +193,8 @@ TIdCustomIcmpClient = class(TIdRawClient) TIdIcmpClient = class(TIdCustomIcmpClient) public - procedure Ping(const ABuffer: String = ''; SequenceID: Word = 0); {Do not Localize} + procedure Ping(const ABuffer: TIdBytes = nil; SequenceID: Word = 0); overload; + procedure Ping(const ABuffer: String; SequenceID: Word = 0); overload; property ReplyData; property ReplyStatus; published @@ -223,7 +224,7 @@ implementation { TIdCustomIcmpClient } -procedure TIdCustomIcmpClient.PrepareEchoRequest(const ABuffer: String); +procedure TIdCustomIcmpClient.PrepareEchoRequest(const ABuffer: TIdBytes); begin {$IFNDEF DOTNET_1_1} if IPVersion = Id_IPv6 then begin @@ -601,15 +602,13 @@ function TIdCustomIcmpClient.DecodeIPv4Packet(BytesRead: UInt32): Boolean; end; end; -procedure TIdCustomIcmpClient.PrepareEchoRequestIPv4(const ABuffer: String); +procedure TIdCustomIcmpClient.PrepareEchoRequestIPv4(const ABuffer: TIdBytes); var LIcmp: TIdICMPHdr; LIdx: UInt32; - LBuffer: TIdBytes; LBufferLen: Integer; begin - LBuffer := ToBytes(ABuffer, IndyTextEncoding_8Bit); - LBufferLen := IndyMin(Length(LBuffer), FPacketSize); + LBufferLen := IndyMin(Length(ABuffer), FPacketSize); SetLength(FBufIcmp, ICMP_MIN + SizeOf(TIdTicks) + LBufferLen); FillBytes(FBufIcmp, Length(FBufIcmp), 0); @@ -627,7 +626,7 @@ procedure TIdCustomIcmpClient.PrepareEchoRequestIPv4(const ABuffer: String); CopyTIdTicks(Ticks64, FBufIcmp, LIdx); Inc(LIdx, SizeOf(TIdTicks)); if LBufferLen > 0 then begin - CopyTIdBytes(LBuffer, 0, FBufIcmp, LIdx, LBufferLen); + CopyTIdBytes(ABuffer, 0, FBufIcmp, LIdx, LBufferLen); end; finally FreeAndNil(LIcmp); @@ -635,15 +634,13 @@ procedure TIdCustomIcmpClient.PrepareEchoRequestIPv4(const ABuffer: String); end; {$IFNDEF DOTNET_1_1} -procedure TIdCustomIcmpClient.PrepareEchoRequestIPv6(const ABuffer: String); +procedure TIdCustomIcmpClient.PrepareEchoRequestIPv6(const ABuffer: TIdBytes); var LIcmp : TIdicmp6_hdr; LIdx : UInt32; - LBuffer: TIdBytes; LBufferLen: Integer; begin - LBuffer := ToBytes(ABuffer, IndyTextEncoding_8Bit); - LBufferLen := IndyMin(Length(LBuffer), FPacketSize); + LBufferLen := IndyMin(Length(ABuffer), FPacketSize); SetLength(FBufIcmp, ICMP_MIN + SizeOf(TIdTicks) + LBufferLen); FillBytes(FBufIcmp, Length(FBufIcmp), 0); @@ -661,7 +658,7 @@ procedure TIdCustomIcmpClient.PrepareEchoRequestIPv6(const ABuffer: String); CopyTIdTicks(Ticks64, FBufIcmp, LIdx); Inc(LIdx, SizeOf(TIdTicks)); if LBufferLen > 0 then begin - CopyTIdBytes(LBuffer, 0, FBufIcmp, LIdx, LBufferLen); + CopyTIdBytes(ABuffer, 0, FBufIcmp, LIdx, LBufferLen); end; finally FreeAndNil(LIcmp); @@ -799,7 +796,7 @@ procedure TIdCustomIcmpClient.SetPacketSize(const AValue: Integer); end; end; -procedure TIdCustomIcmpClient.InternalPing(const AIP, ABuffer: String; SequenceID: Word); +procedure TIdCustomIcmpClient.InternalPing(const AIP: String; const ABuffer: TIdBytes; SequenceID: Word); begin if SequenceID <> 0 then begin wSeqNo := SequenceID; @@ -819,9 +816,14 @@ procedure TIdCustomIcmpClient.SendEchoRequest(const AIP: String); { TIdIcmpClient } -procedure TIdIcmpClient.Ping(const ABuffer: String; SequenceID: Word); +procedure TIdIcmpClient.Ping(const ABuffer: TIdBytes; SequenceID: Word); begin InternalPing(GStack.ResolveHost(Host, IPVersion), ABuffer, SequenceID); end; +procedure TIdIcmpClient.Ping(const ABuffer: String; SequenceID: Word); +begin + Ping(ToBytes(ABuffer, IndyTextEncoding_8Bit), SequenceID); +end; + end. diff --git a/Lib/Core/IdTraceRoute.pas b/Lib/Core/IdTraceRoute.pas index 462404033..7bf4de1de 100644 --- a/Lib/Core/IdTraceRoute.pas +++ b/Lib/Core/IdTraceRoute.pas @@ -95,7 +95,7 @@ procedure TIdTraceRoute.Trace; for i := 1 to 30 do begin ReplyStatus.PacketNumber := i; - InternalPing(LIPAddr, '', LSeq); + InternalPing(LIPAddr, nil, LSeq); case ReplyStatus.ReplyStatusType of rsErrorTTLExceeded, rsTimeout : ;