jwaicmpapi.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. // $Id: JwaIcmpApi.pas,v 1.14 2005/09/06 16:36:50 marquardt Exp $
  43. unit JwaIcmpApi;
  44. {$WEAKPACKAGEUNIT}
  45. {$HPPEMIT ''}
  46. {$HPPEMIT '#include "icmpapi.h"'}
  47. {$HPPEMIT ''}
  48. {$I jediapilib.inc}
  49. interface
  50. uses
  51. JwaWindows, JwaIpExport, JwaWS2tcpip, JwaWinternl;
  52. // Declarations for the Win32 ICMP Echo request API.
  53. //
  54. // Exported Routines.
  55. //
  56. //++
  57. //
  58. // Routine Name:
  59. //
  60. // IcmpCreateFile
  61. //
  62. // Routine Description:
  63. //
  64. // Opens a handle on which ICMP Echo Requests can be issued.
  65. //
  66. // Arguments:
  67. //
  68. // None.
  69. //
  70. // Return Value:
  71. //
  72. // An open file handle or INVALID_HANDLE_VALUE. Extended error information
  73. // is available by calling GetLastError().
  74. //
  75. //--
  76. function IcmpCreateFile: HANDLE; stdcall;
  77. {$EXTERNALSYM IcmpCreateFile}
  78. //++
  79. //
  80. // Routine Name:
  81. //
  82. // Icmp6CreateFile
  83. //
  84. // Routine Description:
  85. //
  86. // Opens a handle on which ICMPv6 Echo Requests can be issued.
  87. //
  88. // Arguments:
  89. //
  90. // None.
  91. //
  92. // Return Value:
  93. //
  94. // An open file handle or INVALID_HANDLE_VALUE. Extended error information
  95. // is available by calling GetLastError().
  96. //
  97. //--
  98. function Icmp6CreateFile: HANDLE; stdcall;
  99. {$EXTERNALSYM Icmp6CreateFile}
  100. //++
  101. //
  102. // Routine Name:
  103. //
  104. // IcmpCloseHandle
  105. //
  106. // Routine Description:
  107. //
  108. // Closes a handle opened by ICMPOpenFile.
  109. //
  110. // Arguments:
  111. //
  112. // IcmpHandle - The handle to close.
  113. //
  114. // Return Value:
  115. //
  116. // TRUE if the handle was closed successfully, otherwise FALSE. Extended
  117. // error information is available by calling GetLastError().
  118. //
  119. //--
  120. function IcmpCloseHandle(IcmpHandle: HANDLE): BOOL; stdcall;
  121. {$EXTERNALSYM IcmpCloseHandle}
  122. //++
  123. //
  124. // Routine Name:
  125. //
  126. // IcmpSendEcho
  127. //
  128. // Routine Description:
  129. //
  130. // Sends an ICMP Echo request and returns any replies. The
  131. // call returns when the timeout has expired or the reply buffer
  132. // is filled.
  133. //
  134. // Arguments:
  135. //
  136. // IcmpHandle - An open handle returned by ICMPCreateFile.
  137. //
  138. // DestinationAddress - The destination of the echo request.
  139. //
  140. // RequestData - A buffer containing the data to send in the
  141. // request.
  142. //
  143. // RequestSize - The number of bytes in the request data buffer.
  144. //
  145. // RequestOptions - Pointer to the IP header options for the request.
  146. // May be NULL.
  147. //
  148. // ReplyBuffer - A buffer to hold any replies to the request.
  149. // On return, the buffer will contain an array of
  150. // ICMP_ECHO_REPLY structures followed by the
  151. // options and data for the replies. The buffer
  152. // should be large enough to hold at least one
  153. // ICMP_ECHO_REPLY structure plus
  154. // MAX(RequestSize, 8) bytes of data since an ICMP
  155. // error message contains 8 bytes of data.
  156. //
  157. // ReplySize - The size in bytes of the reply buffer.
  158. //
  159. // Timeout - The time in milliseconds to wait for replies.
  160. //
  161. // Return Value:
  162. //
  163. // Returns the number of ICMP_ECHO_REPLY structures stored in ReplyBuffer.
  164. // The status of each reply is contained in the structure. If the return
  165. // value is zero, extended error information is available via
  166. // GetLastError().
  167. //
  168. //--
  169. function IcmpSendEcho(
  170. IcmpHandle: HANDLE;
  171. DestinationAddress: IpAddr;
  172. RequestData: LPVOID;
  173. RequestSize: WORD;
  174. RequestOptions: PIP_OPTION_INFORMATION;
  175. ReplyBuffer: LPVOID;
  176. ReplySize: DWORD;
  177. Timeout: DWORD): DWORD; stdcall;
  178. {$EXTERNALSYM IcmpSendEcho}
  179. //++
  180. //
  181. // Routine Description:
  182. //
  183. // Sends an ICMP Echo request and the call returns either immediately
  184. // (if Event or ApcRoutine is NonNULL) or returns after the specified
  185. // timeout. The ReplyBuffer contains the ICMP responses, if any.
  186. //
  187. // Arguments:
  188. //
  189. // IcmpHandle - An open handle returned by ICMPCreateFile.
  190. //
  191. // Event - This is the event to be signalled whenever an IcmpResponse
  192. // comes in.
  193. //
  194. // ApcRoutine - This routine would be called when the calling thread
  195. // is in an alertable thread and an ICMP reply comes in.
  196. //
  197. // ApcContext - This optional parameter is given to the ApcRoutine when
  198. // this call succeeds.
  199. //
  200. // DestinationAddress - The destination of the echo request.
  201. //
  202. // RequestData - A buffer containing the data to send in the
  203. // request.
  204. //
  205. // RequestSize - The number of bytes in the request data buffer.
  206. //
  207. // RequestOptions - Pointer to the IP header options for the request.
  208. // May be NULL.
  209. //
  210. // ReplyBuffer - A buffer to hold any replies to the request.
  211. // On return, the buffer will contain an array of
  212. // ICMP_ECHO_REPLY structures followed by options
  213. // and data. The buffer must be large enough to
  214. // hold at least one ICMP_ECHO_REPLY structure.
  215. // It should be large enough to also hold
  216. // 8 more bytes of data - this is the size of
  217. // an ICMP error message.
  218. //
  219. // ReplySize - The size in bytes of the reply buffer.
  220. //
  221. // Timeout - The time in milliseconds to wait for replies.
  222. // This is NOT used if ApcRoutine is not NULL or if Event
  223. // is not NULL.
  224. //
  225. // Return Value:
  226. //
  227. // Returns the number of replies received and stored in ReplyBuffer. If
  228. // the return value is zero, extended error information is available
  229. // via GetLastError().
  230. //
  231. // Remarks:
  232. //
  233. // On NT platforms,
  234. // If used Asynchronously (either ApcRoutine or Event is specified), then
  235. // ReplyBuffer and ReplySize are still needed. This is where the response
  236. // comes in.
  237. // ICMP Response data is copied to the ReplyBuffer provided, and the caller of
  238. // this function has to parse it asynchronously. The function IcmpParseReply
  239. // is provided for this purpose.
  240. //
  241. // On non-NT platforms,
  242. // Event, ApcRoutine and ApcContext are IGNORED.
  243. //
  244. //--
  245. function IcmpSendEcho2(
  246. IcmpHandle: HANDLE;
  247. Event: HANDLE;
  248. ApcRoutine: PIO_APC_ROUTINE;
  249. ApcContext: PVOID;
  250. DestinationAddress: IpAddr;
  251. RequestData: LPVOID;
  252. RequestSize: WORD;
  253. RequestOptions: PIP_OPTION_INFORMATION;
  254. ReplyBuffer: LPVOID;
  255. ReplySize: DWORD;
  256. Timeout: DWORD): DWORD; stdcall;
  257. {$EXTERNALSYM IcmpSendEcho2}
  258. function Icmp6SendEcho2(
  259. IcmpHandle: HANDLE;
  260. Event: HANDLE;
  261. ApcRoutine: PIO_APC_ROUTINE;
  262. ApcContext: PVOID;
  263. SourceAddress: Psockaddr_in6;
  264. DestinationAddress: Psockaddr_in6;
  265. RequestData,
  266. RequestSize: WORD;
  267. RequestOptions: PIP_OPTION_INFORMATION;
  268. ReplyBuffer: LPVOID;
  269. ReplySize: DWORD;
  270. Timeout: DWORD): DWORD; stdcall;
  271. {$EXTERNALSYM Icmp6SendEcho2}
  272. //++
  273. //
  274. // Routine Description:
  275. //
  276. // Parses the reply buffer provided and returns the number of ICMP responses found.
  277. //
  278. // Arguments:
  279. //
  280. // ReplyBuffer - This must be the same buffer that was passed to IcmpSendEcho2
  281. // This is rewritten to hold an array of ICMP_ECHO_REPLY structures.
  282. // (i.e. the type is PICMP_ECHO_REPLY).
  283. //
  284. // ReplySize - This must be the size of the above buffer.
  285. //
  286. // Return Value:
  287. // Returns the number of ICMP responses found. If there is an errors, return value is
  288. // zero. The error can be determined by a call to GetLastError.
  289. //
  290. // Remarks:
  291. // This function SHOULD NOT BE USED on a reply buffer that was passed to SendIcmpEcho.
  292. // SendIcmpEcho actually parses the buffer before returning back to the user. This function
  293. // is meant to be used only with SendIcmpEcho2.
  294. //--
  295. function IcmpParseReplies(ReplyBuffer: LPVOID; ReplySize: DWORD): DWORD; stdcall;
  296. {$EXTERNALSYM IcmpParseReplies}
  297. function Icmp6ParseReplies(ReplyBuffer: LPVOID; ReplySize: DWORD): DWORD; stdcall;
  298. {$EXTERNALSYM Icmp6ParseReplies}
  299. implementation
  300. uses
  301. JwaWinDLLNames;
  302. {$IFDEF DYNAMIC_LINK}
  303. var
  304. _IcmpCreateFile: Pointer;
  305. function IcmpCreateFile;
  306. begin
  307. GetProcedureAddress(_IcmpCreateFile, icmplib, 'IcmpCreateFile');
  308. asm
  309. MOV ESP, EBP
  310. POP EBP
  311. JMP [_IcmpCreateFile]
  312. end;
  313. end;
  314. var
  315. _Icmp6CreateFile: Pointer;
  316. function Icmp6CreateFile;
  317. begin
  318. GetProcedureAddress(_Icmp6CreateFile, icmplib, 'Icmp6CreateFile');
  319. asm
  320. MOV ESP, EBP
  321. POP EBP
  322. JMP [_Icmp6CreateFile]
  323. end;
  324. end;
  325. var
  326. _IcmpCloseHandle: Pointer;
  327. function IcmpCloseHandle;
  328. begin
  329. GetProcedureAddress(_IcmpCloseHandle, icmplib, 'IcmpCloseHandle');
  330. asm
  331. MOV ESP, EBP
  332. POP EBP
  333. JMP [_IcmpCloseHandle]
  334. end;
  335. end;
  336. var
  337. _IcmpSendEcho: Pointer;
  338. function IcmpSendEcho;
  339. begin
  340. GetProcedureAddress(_IcmpSendEcho, icmplib, 'IcmpSendEcho');
  341. asm
  342. MOV ESP, EBP
  343. POP EBP
  344. JMP [_IcmpSendEcho]
  345. end;
  346. end;
  347. var
  348. _IcmpSendEcho2: Pointer;
  349. function IcmpSendEcho2;
  350. begin
  351. GetProcedureAddress(_IcmpSendEcho2, icmplib, 'IcmpSendEcho2');
  352. asm
  353. MOV ESP, EBP
  354. POP EBP
  355. JMP [_IcmpSendEcho2]
  356. end;
  357. end;
  358. var
  359. _Icmp6SendEcho2: Pointer;
  360. function Icmp6SendEcho2;
  361. begin
  362. GetProcedureAddress(_Icmp6SendEcho2, icmplib, 'Icmp6SendEcho2');
  363. asm
  364. MOV ESP, EBP
  365. POP EBP
  366. JMP [_Icmp6SendEcho2]
  367. end;
  368. end;
  369. var
  370. _IcmpParseReplies: Pointer;
  371. function IcmpParseReplies;
  372. begin
  373. GetProcedureAddress(_IcmpParseReplies, icmplib, 'IcmpParseReplies');
  374. asm
  375. MOV ESP, EBP
  376. POP EBP
  377. JMP [_IcmpParseReplies]
  378. end;
  379. end;
  380. var
  381. _Icmp6ParseReplies: Pointer;
  382. function Icmp6ParseReplies;
  383. begin
  384. GetProcedureAddress(_Icmp6ParseReplies, icmplib, 'Icmp6ParseReplies');
  385. asm
  386. MOV ESP, EBP
  387. POP EBP
  388. JMP [_Icmp6ParseReplies]
  389. end;
  390. end;
  391. {$ELSE}
  392. function IcmpCreateFile; external icmplib name 'IcmpCreateFile';
  393. function Icmp6CreateFile; external icmplib name 'Icmp6CreateFile';
  394. function IcmpCloseHandle; external icmplib name 'IcmpCloseHandle';
  395. function IcmpSendEcho; external icmplib name 'IcmpSendEcho';
  396. function IcmpSendEcho2; external icmplib name 'IcmpSendEcho2';
  397. function Icmp6SendEcho2; external icmplib name 'Icmp6SendEcho2';
  398. function IcmpParseReplies; external icmplib name 'IcmpParseReplies';
  399. function Icmp6ParseReplies; external icmplib name 'Icmp6ParseReplies';
  400. {$ENDIF DYNAMIC_LINK}
  401. end.