1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
| //------------------------------------------------------------------------------
(* SoLuTions is an Versatile Library for Delphi -
* -
* Version alternative publiée sur "www.developpez.net" -
* Post : "Comment connaître le type d'un élément dans un réseau IP ? " -
* Post Number : 2357046 -
* Post URL = "http://www.developpez.net/forums/d387409/environnements-developpement/delphi/web-reseau/connaitre-type-d-element-reseau-ip/#post2357046"
* -
* Version alternative publiée sur "www.developpez.net" -
* Post : "Utilisation d'un ping sous Delphi XE6" -
* Post Number : 8397605 -
* Post URL = "http://www.developpez.net/forums/d1545448/environnements-developpement/delphi/web-reseau/utilisation-d-ping-sous-delphi-xe6/#post8397605"
* -
* Version alternative publiée sur "www.developpez.net" -
* Post : "Client Socket avec domaine DNS Dynamique " -
* Post Number : 7469064 -
* Post URL = "http://www.developpez.net/forums/d1376072/c-cpp/outils-c-cpp/cppbuilder/client-socket-domaine-dns-dynamique/#post7469064"
* -
* Copyright ou © ou Copr. "SLT Solutions", (2006) -
* contributeur : ShaiLeTroll (2015) - Reprise du Ping de la SLT<2006> sous Delphi 7 avec fusion du QueryDNS de la SLT<2012> sous C++Builder XE2/XE3 vers la SLT<2013> sous Delphi XE2
* -
* ShaiLeTroll -
* -
* Ce logiciel est un programme informatique servant à aider les développeurs -
* Delphi avec une bibliothèque polyvalente, adaptable et fragmentable. -
* -
* Ce logiciel est régi par la licence CeCILL-C soumise au droit français et -
* respectant les principes de diffusion des logiciels libres. Vous pouvez -
* utiliser, modifier et/ou redistribuer ce programme sous les conditions -
* de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA -
* sur le site "http://www.cecill.info". -
* -
* En contrepartie de l'accessibilité au code source et des droits de copie, -
* de modification et de redistribution accordés par cette licence, il n'est -
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, -
* seule une responsabilité restreinte pèse sur l'auteur du programme, le -
* titulaire des droits patrimoniaux et les concédants successifs. -
* -
* A cet égard l'attention de l'utilisateur est attirée sur les risques -
* associés au chargement, à l'utilisation, à la modification et/ou au -
* développement et à la reproduction du logiciel par l'utilisateur étant -
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à -
* manipuler et qui le réserve donc à des développeurs et des professionnels -
* avertis possédant des connaissances informatiques approfondies. Les -
* utilisateurs sont donc invités à charger et tester l'adéquation du -
* logiciel à leurs besoins dans des conditions permettant d'assurer la -
* sécurité de leurs systèmes et ou de leurs données et, plus généralement, -
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. -
* -
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez -
* pris connaissance de la licence CeCILL-C, et que vous en avez accepté les -
* termes. -
* -
*----------------------------------------------------------------------------*)
unit SLT.Common.ICMP;
{$ALIGN ON}
{$MINENUMSIZE 4}
interface
uses System.Classes, System.SysUtils,
Winapi.Windows, Winapi.WinSock;
type
{ Forward class declarations }
TSLTICMPRequest = class;
/// <summary>TSLTICMPRequest encapsule une requete Ping via ICMP</summary>
TSLTICMPRequest = class(TObject)
private
FTextResult: TStrings;
function GetTextResult(AAllowMultiLines: Boolean): string;
procedure AddResult(const AText: string);
function QueryDNS(const HostName: string; out IP: TInAddr): Boolean;
public
destructor Destroy(); override;
function Ping(const IP: string): Boolean;
property TextResult[AAllowMultiLines: Boolean]: string read GetTextResult;
end;
implementation
// -----------------------------------------------------------------------------
procedure TSLTICMPRequest.AddResult(const AText: string);
begin
if not Assigned(FTextResult) then
FTextResult := TStringList.Create();
FTextResult.Add(AText);
end;
// -----------------------------------------------------------------------------
destructor TSLTICMPRequest.Destroy();
begin
FreeAndNil(FTextResult);
inherited Destroy();
end;
// -----------------------------------------------------------------------------
function TSLTICMPRequest.GetTextResult(AAllowMultiLines: Boolean): string;
begin
if Assigned(FTextResult) then
begin
if not AAllowMultiLines then
begin
FTextResult.Delimiter := ';';
FTextResult.QuoteChar := '!';
FTextResult.StrictDelimiter := True;
Result := FTextResult.DelimitedText;
end
else
Result := FTextResult.Text;
end
else
Result := '';
end;
// -----------------------------------------------------------------------------
function TSLTICMPRequest.Ping(const IP: string): Boolean;
type
TECHO_REPLY_MSG = record
Status: ULONG;
Message: string;
end;
const
MSG: array[1..27] of TECHO_REPLY_MSG = (
(Status: 0; Message: 'The status was success.'),
(Status: 1; Message: 'The reply buffer was too small.'),
(Status: 2; Message: 'The destination network was unreachable. In IPv6 terminology, this status value is also defined as IP_DEST_NO_ROUTE.'),
(Status: 3; Message: 'The destination host was unreachable. In IPv6 terminology, this status value is also defined as IP_DEST_ADDR_UNREACHABLE.'),
(Status: 4; Message: 'The destination protocol was unreachable. In IPv6 terminology, this status value is also defined as IP_DEST_PROHIBITED.'),
(Status: 5; Message: 'The destination port was unreachable.'),
(Status: 6; Message: 'No IP resources were available.'),
(Status: 7; Message: 'A bad IP option was specified.'),
(Status: 8; Message: 'A hardware error occurred.'),
(Status: 9; Message: 'The packet was too big.'),
(Status: 10; Message: 'The request timed out.'),
(Status: 11; Message: 'A bad request.'),
(Status: 12; Message: 'A bad route.'),
(Status: 13; Message: 'The Time to Live (IPv4) or Hop Limit (IPv6) expired in transit. In IPv6 terminology, this status value is also defined as IP_HOP_LIMIT_EXCEEDED.'),
(Status: 14; Message: 'The Time to Live (IPv4) or Hop Limit (IPv6) expired during fragment reassembly. In IPv6 terminology, this status value is also defined as IP_REASSEMBLY_TIME_EXCEEDED.'),
(Status: 15; Message: 'A parameter problem. In IPv6 terminology, this status value is also defined as IP_PARAMETER_PROBLEM.'),
(Status: 16; Message: ' Message: ''Datagrams are arriving too fast to be processed and datagrams may have been discarded.'),
(Status: 17; Message: 'An IP option was too big.'),
(Status: 18; Message: 'A bad destination.'),
(Status: 40; Message: 'The destination was unreachable. The value is only applicable to IPv6.'),
(Status: 41; Message: 'The time was exceeded. The value is only applicable to IPv6.'),
(Status: 42; Message: 'A bad IP header was encountered. The value is only applicable to IPv6.'),
(Status: 43; Message: 'An unrecognized next header was encountered. The value is only applicable to IPv6.'),
(Status: 44; Message: 'An ICMP error occurred. The value is only applicable to IPv6.'),
(Status: 45; Message: 'A destination scope ID mismatch occurred. The value is only applicable to IPv6.'),
(Status: 50; Message: 'General Failure.'),
(Status: 255; Message: 'IP Pending.')
);
function GetStatus(Status: ULONG): string;
var
iMsg: Integer;
begin
Result := SysErrorMessage(Status);
if Status >= 11000 then
Status := Status - 11000;
for iMsg := Low(MSG) to High(MSG) do
begin
if MSG[iMsg].Status = Status then
begin
Result := MSG[iMsg].Message;
Exit;
end;
end;
end;
type
PIP_OPTION_INFORMATION = ^TIP_OPTION_INFORMATION;
TIP_OPTION_INFORMATION = record
Ttl: UCHAR;
Tos: UCHAR;
Flags: UCHAR;
OptionsSize: UCHAR;
OptionsData: PUCHAR;
end;
PICMP_ECHO_REPLY = ^TICMP_ECHO_REPLY;
TICMP_ECHO_REPLY = record
Address: TInAddr;
Status: ULONG;
RoundTripTime: ULONG;
DataSize: Word;
Reserved: Word;
Data: Pointer;
Options: TIP_OPTION_INFORMATION;
end;
var
IphlpapiHandle: THandle;
_IcmpCreateFile: function(): THandle; stdcall;
_IcmpCloseHandle: function(IcmpHandle: THandle): BOOL; stdcall;
_IcmpSendEcho: function(IcmpHandle: THandle; DestinationAddress: TInAddr; RequestData: Pointer; RequestSize: WORD; RequestOptions: PIP_OPTION_INFORMATION; ReplyBuffer: Pointer; ReplySize: DWORD; Timeout: DWORD): DWORD; stdcall;
var
InAddr: TInAddr;
IcmpHandle: THandle;
SendText: string;
SendData: PChar;
SendLen, ReplyLen: Integer;
dwRetVal: DWORD;
Reply: PICMP_ECHO_REPLY; // si compilé en 64 bits, faut changer en ICMP_ECHO_REPLY32
TimeStr: string;
begin
try
IphlpapiHandle := LoadLibrary('Iphlpapi');
Result := IphlpapiHandle > 0;
if Result then
begin
try
@_IcmpCreateFile := GetProcAddress(IphlpapiHandle, 'IcmpCreateFile');
@_IcmpCloseHandle := GetProcAddress(IphlpapiHandle, 'IcmpCloseHandle');
@_IcmpSendEcho := GetProcAddress(IphlpapiHandle, 'IcmpSendEcho');
Result := Assigned(@_IcmpCreateFile) and Assigned(@_IcmpCloseHandle) and Assigned(@_IcmpSendEcho);
if Result then
begin
SendText := 'SLT - Test de la Connexion (Ping) par un Send Echo (ICMP) !';
InAddr.S_addr := inet_addr(PAnsiChar(AnsiString(IP)));
if DWORD(InAddr.S_addr) = INADDR_NONE then
begin
AddResult(Format('Ping sur %s, Conversion Nom d''Hôte en IP', [IP]));
if not QueryDNS(IP, InAddr) then
Exit(False);
end;
IcmpHandle := _IcmpCreateFile();
try
SendLen := Length(SendText) * SizeOf(Char);
// The buffer should be large enough to hold at least one ICMP_ECHO_REPLY structure plus RequestSize bytes of data
ReplyLen := SendLen + SizeOf(TICMP_ECHO_REPLY);
GetMem(Reply, ReplyLen);
try
SendData := PChar(SendText);
dwRetVal := _IcmpSendEcho(IcmpHandle, InAddr, SendData, StrLen(SendData), nil, Reply, ReplyLen, 1000);
Result := dwRetVal <> 0;
if Result then
begin
if Reply^.RoundTripTime <= 0 then
TimeStr := ' < 1ms'
else
TimeStr := Format(' = %dms', [Reply^.RoundTripTime]);
AddResult(Format('Ping sur %s, Status : "%s", Temps%s, Message Reçu %d/1, TTL = %d', [IP, GetStatus(Reply^.Status), TimeStr, dwRetVal, Reply^.Options.Ttl]));
end
else
begin
AddResult(Format('Echec du Ping sur %s, Erreur %d : %s', [IP, Winapi.Windows.GetLastError(), GetStatus(Winapi.Windows.GetLastError())]));
end;
finally
FreeMem(Reply);
end;
finally
_IcmpCloseHandle(IcmpHandle);
end;
end
else
AddResult('Impossible d''Obtenir les Procédures "IcmpCreateFile", "IcmpCloseHandle" et "IcmpSendEcho"');
finally
FreeLibrary(IphlpapiHandle);
end;
end
else
AddResult('Impossible de Charger "Iphlpapi.dll"');
except
on E: Exception do
begin
AddResult(E.Message);
Result := False;
end;
end;
end;
// -----------------------------------------------------------------------------
function TSLTICMPRequest.QueryDNS(const HostName: string; out IP: TInAddr): Boolean;
type
PIP4_ADDRESS = ^IP4_ADDRESS;
IP4_ADDRESS = DWORD;
IP4_ARRAY = record
AddrCount: DWORD;
AddrArray: PIP4_ADDRESS;
end;
DNS_A_DATA = record
IpAddress: IP4_ADDRESS;
end;
PDNS_RECORD = ^DNS_RECORD;
DNS_RECORD = record
pNext: PDNS_RECORD;
pName: PChar;
wType: WORD;
wDataLength: WORD;
Flags: record
DNS_RECORD_FLAGS: DWORD; // champs de bits
end;
dwTtl: DWORD;
dwReserved: DWORD;
Data: record
A: DNS_A_DATA;
end;
end;
DNS_STATUS = LONG;
DNS_FREE_TYPE = (DnsFreeFlat, DnsFreeRecordList);
const
DNS_TYPE_A = $0001;
DNS_QUERY_STANDARD = $00000000;
var
pDnsRecord: PDNS_RECORD;
DnsapiHandle: THandle;
_DnsQuery: function(lpstrName: PChar; wType: WORD; Options: DWORD; pExtra: Pointer; var ppQueryResultsSet: PDNS_RECORD; pReserved: Pointer): DNS_STATUS; stdcall;
_DnsRecordListFree: procedure(pRecordList: PDNS_RECORD; FreeType: DNS_FREE_TYPE); stdcall;
IPV4Value: IP4_ADDRESS;
begin
try
DnsapiHandle := LoadLibrary('Dnsapi');
Result := DnsapiHandle > 0;
if Result then
begin
try
@_DnsQuery := GetProcAddress(DnsapiHandle, 'DnsQuery_W');
@_DnsRecordListFree := GetProcAddress(DnsapiHandle, 'DnsRecordListFree');
Result := Assigned(_DnsQuery) and Assigned(_DnsRecordListFree);
if Result then
begin
Result := _DnsQuery(PChar(HostName), DNS_TYPE_A, DNS_QUERY_STANDARD, nil, pDnsRecord, nil) = ERROR_SUCCESS;
if Result then
begin
IPV4Value := pDnsRecord.Data.A.IpAddress;
_DnsRecordListFree(pDnsRecord, DnsFreeRecordList);
IP.S_addr := IPV4Value;
AddResult(Format('Résolution de %s en %s', [HostName, inet_ntoa(IP)]));
end
else
AddResult(Format('Echec de résolution de %s', [HostName]));
end
else
AddResult('Impossible d''Obtenir la procédure "DnsQuery" ou "DnsRecordListFree"');
finally
FreeLibrary(DnsapiHandle);
end;
end
else
AddResult('Impossible de Charger "Dnsapi.dll"');
except
on E: Exception do
begin
AddResult(E.Message);
Result := False;
end;
end;
end;
end. |
Partager