Skip to content

Commit

Permalink
Updating TIdIcmpClient.Ping() to let users pass in a TIdBytes instead…
Browse files Browse the repository at this point in the history
… of a String for user-defined data.
  • Loading branch information
rlebeau committed Sep 11, 2023
1 parent 93a3f55 commit 65ae690
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
38 changes: 20 additions & 18 deletions Lib/Core/IdIcmpClient.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -627,23 +626,21 @@ 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);
end;
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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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.
2 changes: 1 addition & 1 deletion Lib/Core/IdTraceRoute.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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 : ;
Expand Down

0 comments on commit 65ae690

Please sign in to comment.