jwaicmpapi.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. {******************************************************************************}
  2. { }
  3. { ICMP Echo API interface Unit for Object Pascal }
  4. { }
  5. { Portions created by Microsoft are Copyright (C) 1995-2001 Microsoft }
  6. { Corporation. All Rights Reserved. }
  7. { }
  8. { The original file is: icmpapi.h, released Feb 2003. The original Pascal }
  9. { code is: IcmpApi.pas, released December 2003. The initial developer of the }
  10. { Pascal code is Marcel van Brakel (brakelm att chello dott nl). }
  11. { }
  12. { Portions created by Marcel van Brakel are Copyright (C) 1999-2001 }
  13. { Marcel van Brakel. All Rights Reserved. }
  14. { }
  15. { Obtained through: Joint Endeavour of Delphi Innovators (Project JEDI) }
  16. { }
  17. { You may retrieve the latest version of this file at the Project JEDI }
  18. { APILIB home page, located at http://jedi-apilib.sourceforge.net }
  19. { }
  20. { The contents of this file are used with permission, subject to the Mozilla }
  21. { Public License Version 1.1 (the "License"); you may not use this file except }
  22. { in compliance with the License. You may obtain a copy of the License at }
  23. { http://www.mozilla.org/MPL/MPL-1.1.html }
  24. { }
  25. { Software distributed under the License is distributed on an "AS IS" basis, }
  26. { WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
  27. { the specific language governing rights and limitations under the License. }
  28. { }
  29. { Alternatively, the contents of this file may be used under the terms of the }
  30. { GNU Lesser General Public License (the "LGPL License"), in which case the }
  31. { provisions of the LGPL License are applicable instead of those above. }
  32. { If you wish to allow use of your version of this file only under the terms }
  33. { of the LGPL License and not to allow others to use your version of this file }
  34. { under the MPL, indicate your decision by deleting the provisions above and }
  35. { replace them with the notice and other provisions required by the LGPL }
  36. { License. If you do not delete the provisions above, a recipient may use }
  37. { your version of this file under either the MPL or the LGPL License. }
  38. { }
  39. { For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html }
  40. { }
  41. {******************************************************************************}
  42. unit JwaIcmpApi;
  43. {$WEAKPACKAGEUNIT}
  44. {$HPPEMIT ''}
  45. {$HPPEMIT '#include "icmpapi.h"'}
  46. {$HPPEMIT ''}
  47. {$I jediapilib.inc}
  48. interface
  49. uses
  50. JwaWinType, JwaIpExport, JwaNative, JwaWS2tcpip;
  51. // Declarations for the Win32 ICMP Echo request API.
  52. //
  53. // Exported Routines.
  54. //
  55. //++
  56. //
  57. // Routine Name:
  58. //
  59. // IcmpCreateFile
  60. //
  61. // Routine Description:
  62. //
  63. // Opens a handle on which ICMP Echo Requests can be issued.
  64. //
  65. // Arguments:
  66. //
  67. // None.
  68. //
  69. // Return Value:
  70. //
  71. // An open file handle or INVALID_HANDLE_VALUE. Extended error information
  72. // is available by calling GetLastError().
  73. //
  74. //--
  75. function IcmpCreateFile: HANDLE; stdcall;
  76. {$EXTERNALSYM IcmpCreateFile}
  77. //++
  78. //
  79. // Routine Name:
  80. //
  81. // Icmp6CreateFile
  82. //
  83. // Routine Description:
  84. //
  85. // Opens a handle on which ICMPv6 Echo Requests can be issued.
  86. //
  87. // Arguments:
  88. //
  89. // None.
  90. //
  91. // Return Value:
  92. //
  93. // An open file handle or INVALID_HANDLE_VALUE. Extended error information
  94. // is available by calling GetLastError().
  95. //
  96. //--
  97. function Icmp6CreateFile: HANDLE; stdcall;
  98. {$EXTERNALSYM Icmp6CreateFile}
  99. //++
  100. //
  101. // Routine Name:
  102. //
  103. // IcmpCloseHandle
  104. //
  105. // Routine Description:
  106. //
  107. // Closes a handle opened by ICMPOpenFile.
  108. //
  109. // Arguments:
  110. //
  111. // IcmpHandle - The handle to close.
  112. //
  113. // Return Value:
  114. //
  115. // TRUE if the handle was closed successfully, otherwise FALSE. Extended
  116. // error information is available by calling GetLastError().
  117. //
  118. //--
  119. function IcmpCloseHandle(IcmpHandle: HANDLE): BOOL; stdcall;
  120. {$EXTERNALSYM IcmpCloseHandle}
  121. //++
  122. //
  123. // Routine Name:
  124. //
  125. // IcmpSendEcho
  126. //
  127. // Routine Description:
  128. //
  129. // Sends an ICMP Echo request and returns any replies. The
  130. // call returns when the timeout has expired or the reply buffer
  131. // is filled.
  132. //
  133. // Arguments:
  134. //
  135. // IcmpHandle - An open handle returned by ICMPCreateFile.
  136. //
  137. // DestinationAddress - The destination of the echo request.
  138. //
  139. // RequestData - A buffer containing the data to send in the
  140. // request.
  141. //
  142. // RequestSize - The number of bytes in the request data buffer.
  143. //
  144. // RequestOptions - Pointer to the IP header options for the request.
  145. // May be NULL.
  146. //
  147. // ReplyBuffer - A buffer to hold any replies to the request.
  148. // On return, the buffer will contain an array of
  149. // ICMP_ECHO_REPLY structures followed by the
  150. // options and data for the replies. The buffer
  151. // should be large enough to hold at least one
  152. // ICMP_ECHO_REPLY structure plus
  153. // MAX(RequestSize, 8) bytes of data since an ICMP
  154. // error message contains 8 bytes of data.
  155. //
  156. // ReplySize - The size in bytes of the reply buffer.
  157. //
  158. // Timeout - The time in milliseconds to wait for replies.
  159. //
  160. // Return Value:
  161. //
  162. // Returns the number of ICMP_ECHO_REPLY structures stored in ReplyBuffer.
  163. // The status of each reply is contained in the structure. If the return
  164. // value is zero, extended error information is available via
  165. // GetLastError().
  166. //
  167. //--
  168. function IcmpSendEcho(
  169. IcmpHandle: HANDLE;
  170. DestinationAddress: IpAddr;
  171. RequestData: LPVOID;
  172. RequestSize: WORD;
  173. RequestOptions: PIP_OPTION_INFORMATION;
  174. ReplyBuffer: LPVOID;
  175. ReplySize: DWORD;
  176. Timeout: DWORD): DWORD; stdcall;
  177. {$EXTERNALSYM IcmpSendEcho}
  178. //++
  179. //
  180. // Routine Description:
  181. //
  182. // Sends an ICMP Echo request and the call returns either immediately
  183. // (if Event or ApcRoutine is NonNULL) or returns after the specified
  184. // timeout. The ReplyBuffer contains the ICMP responses, if any.
  185. //
  186. // Arguments:
  187. //
  188. // IcmpHandle - An open handle returned by ICMPCreateFile.
  189. //
  190. // Event - This is the event to be signalled whenever an IcmpResponse
  191. // comes in.
  192. //
  193. // ApcRoutine - This routine would be called when the calling thread
  194. // is in an alertable thread and an ICMP reply comes in.
  195. //
  196. // ApcContext - This optional parameter is given to the ApcRoutine when
  197. // this call succeeds.
  198. //
  199. // DestinationAddress - The destination of the echo request.
  200. //
  201. // RequestData - A buffer containing the data to send in the
  202. // request.
  203. //
  204. // RequestSize - The number of bytes in the request data buffer.
  205. //
  206. // RequestOptions - Pointer to the IP header options for the request.
  207. // May be NULL.
  208. //
  209. // ReplyBuffer - A buffer to hold any replies to the request.
  210. // On return, the buffer will contain an array of
  211. // ICMP_ECHO_REPLY structures followed by options
  212. // and data. The buffer must be large enough to
  213. // hold at least one ICMP_ECHO_REPLY structure.
  214. // It should be large enough to also hold
  215. // 8 more bytes of data - this is the size of
  216. // an ICMP error message.
  217. //
  218. // ReplySize - The size in bytes of the reply buffer.
  219. //
  220. // Timeout - The time in milliseconds to wait for replies.
  221. // This is NOT used if ApcRoutine is not NULL or if Event
  222. // is not NULL.
  223. //
  224. // Return Value:
  225. //
  226. // Returns the number of replies received and stored in ReplyBuffer. If
  227. // the return value is zero, extended error information is available
  228. // via GetLastError().
  229. //
  230. // Remarks:
  231. //
  232. // On NT platforms,
  233. // If used Asynchronously (either ApcRoutine or Event is specified), then
  234. // ReplyBuffer and ReplySize are still needed. This is where the response
  235. // comes in.
  236. // ICMP Response data is copied to the ReplyBuffer provided, and the caller of
  237. // this function has to parse it asynchronously. The function IcmpParseReply
  238. // is provided for this purpose.
  239. //
  240. // On non-NT platforms,
  241. // Event, ApcRoutine and ApcContext are IGNORED.
  242. //
  243. //--
  244. function IcmpSendEcho2(
  245. IcmpHandle: HANDLE;
  246. Event: HANDLE;
  247. ApcRoutine: PIO_APC_ROUTINE;
  248. ApcContext: PVOID;
  249. DestinationAddress: IpAddr;
  250. RequestData: LPVOID;
  251. RequestSize: WORD;
  252. RequestOptions: PIP_OPTION_INFORMATION;
  253. ReplyBuffer: LPVOID;
  254. ReplySize: DWORD;
  255. Timeout: DWORD): DWORD; stdcall;
  256. function Icmp6SendEcho2(
  257. IcmpHandle: HANDLE;
  258. Event: HANDLE;
  259. ApcRoutine: PIO_APC_ROUTINE;
  260. ApcContext: PVOID;
  261. SourceAddress: Psockaddr_in6;
  262. DestinationAddress: Psockaddr_in6;
  263. RequestData,
  264. RequestSize: WORD;
  265. RequestOptions: PIP_OPTION_INFORMATION;
  266. ReplyBuffer: LPVOID;
  267. ReplySize: DWORD;
  268. Timeout: DWORD): DWORD; stdcall;
  269. //++
  270. //
  271. // Routine Description:
  272. //
  273. // Parses the reply buffer provided and returns the number of ICMP responses found.
  274. //
  275. // Arguments:
  276. //
  277. // ReplyBuffer - This must be the same buffer that was passed to IcmpSendEcho2
  278. // This is rewritten to hold an array of ICMP_ECHO_REPLY structures.
  279. // (i.e. the type is PICMP_ECHO_REPLY).
  280. //
  281. // ReplySize - This must be the size of the above buffer.
  282. //
  283. // Return Value:
  284. // Returns the number of ICMP responses found. If there is an errors, return value is
  285. // zero. The error can be determined by a call to GetLastError.
  286. //
  287. // Remarks:
  288. // This function SHOULD NOT BE USED on a reply buffer that was passed to SendIcmpEcho.
  289. // SendIcmpEcho actually parses the buffer before returning back to the user. This function
  290. // is meant to be used only with SendIcmpEcho2.
  291. //--
  292. function IcmpParseReplies(ReplyBuffer: LPVOID; ReplySize: DWORD): DWORD; stdcall;
  293. {$EXTERNALSYM IcmpParseReplies}
  294. function Icmp6ParseReplies(ReplyBuffer: LPVOID; ReplySize: DWORD): DWORD; stdcall;
  295. {$EXTERNALSYM Icmp6ParseReplies}
  296. implementation
  297. const
  298. icmplib = 'iphlpapi.dll';
  299. {$IFDEF DYNAMIC_LINK}
  300. var
  301. _IcmpCreateFile: Pointer;
  302. function IcmpCreateFile;
  303. begin
  304. GetProcedureAddress(_IcmpCreateFile, icmplib, 'IcmpCreateFile');
  305. asm
  306. MOV ESP, EBP
  307. POP EBP
  308. JMP [_IcmpCreateFile]
  309. end;
  310. end;
  311. var
  312. _Icmp6CreateFile: Pointer;
  313. function Icmp6CreateFile;
  314. begin
  315. GetProcedureAddress(_Icmp6CreateFile, icmplib, 'Icmp6CreateFile');
  316. asm
  317. MOV ESP, EBP
  318. POP EBP
  319. JMP [_Icmp6CreateFile]
  320. end;
  321. end;
  322. var
  323. _IcmpCloseHandle: Pointer;
  324. function IcmpCloseHandle;
  325. begin
  326. GetProcedureAddress(_IcmpCloseHandle, icmplib, 'IcmpCloseHandle');
  327. asm
  328. MOV ESP, EBP
  329. POP EBP
  330. JMP [_IcmpCloseHandle]
  331. end;
  332. end;
  333. var
  334. _IcmpSendEcho: Pointer;
  335. function IcmpSendEcho;
  336. begin
  337. GetProcedureAddress(_IcmpSendEcho, icmplib, 'IcmpSendEcho');
  338. asm
  339. MOV ESP, EBP
  340. POP EBP
  341. JMP [_IcmpSendEcho]
  342. end;
  343. end;
  344. var
  345. _IcmpSendEcho2: Pointer;
  346. function IcmpSendEcho2;
  347. begin
  348. GetProcedureAddress(_IcmpSendEcho2, icmplib, 'IcmpSendEcho2');
  349. asm
  350. MOV ESP, EBP
  351. POP EBP
  352. JMP [_IcmpSendEcho2]
  353. end;
  354. end;
  355. var
  356. _Icmp6SendEcho2: Pointer;
  357. function Icmp6SendEcho2;
  358. begin
  359. GetProcedureAddress(_Icmp6SendEcho2, icmplib, 'Icmp6SendEcho2');
  360. asm
  361. MOV ESP, EBP
  362. POP EBP
  363. JMP [_Icmp6SendEcho2]
  364. end;
  365. end;
  366. var
  367. _IcmpParseReplies: Pointer;
  368. function IcmpParseReplies;
  369. begin
  370. GetProcedureAddress(_IcmpParseReplies, icmplib, 'IcmpParseReplies');
  371. asm
  372. MOV ESP, EBP
  373. POP EBP
  374. JMP [_IcmpParseReplies]
  375. end;
  376. end;
  377. var
  378. _Icmp6ParseReplies: Pointer;
  379. function Icmp6ParseReplies;
  380. begin
  381. GetProcedureAddress(_Icmp6ParseReplies, icmplib, 'Icmp6ParseReplies');
  382. asm
  383. MOV ESP, EBP
  384. POP EBP
  385. JMP [_Icmp6ParseReplies]
  386. end;
  387. end;
  388. {$ELSE}
  389. function IcmpCreateFile; external icmplib name 'IcmpCreateFile';
  390. function Icmp6CreateFile; external icmplib name 'Icmp6CreateFile';
  391. function IcmpCloseHandle; external icmplib name 'IcmpCloseHandle';
  392. function IcmpSendEcho; external icmplib name 'IcmpSendEcho';
  393. function IcmpSendEcho2; external icmplib name 'IcmpSendEcho2';
  394. function Icmp6SendEcho2; external icmplib name 'Icmp6SendEcho2';
  395. function IcmpParseReplies; external icmplib name 'IcmpParseReplies';
  396. function Icmp6ParseReplies; external icmplib name 'Icmp6ParseReplies';
  397. {$ENDIF DYNAMIC_LINK}
  398. end.