Packet32.pas
上传用户:sunrenlu
上传日期:2007-01-08
资源大小:199k
文件大小:10k
源码类别:

Internet/网络编程

开发平台:

Delphi

  1. unit packet32;
  2. {
  3.      PACKET32.PAS
  4.      Original source is PACKET32.H
  5.      Written by Sang-Eun Han
  6.      (seh@brabo1.korea.ac.kr,  http://widecomm.korea.ac.kr/~seh).
  7.      Convert to Delphi 4.0 by: Jagad (don@indo.net.id)
  8.      Some updates by francois.piette@pophost.eunet.be (see below)
  9.      Use static link to Packet32.DLL
  10.      Note:
  11.       - BOOLEAN convert to Boolean, don't convert to BOOL because
  12.         BOOL is LongBool.
  13.       - ULONG convert to BOOL if it's return TRUE or FALSE
  14.      Updates:
  15.       May 10, 1999  Jagad   Add Directive to support D3
  16.       May 10, 1999          Change UINT to Longint for D3
  17.       May 10, 1999  FPiette confirm update: OVERLAPPED changes
  18.                             TOverlapped for D3
  19.       May 17, 1999  FPiette Added automatic Delphi3/Delphi4 detection
  20.                     FPiette introduced USLONG type to unify between D3 and D4
  21.                     with simplified directives
  22. }
  23. {$ALIGN ON}
  24. interface
  25. uses windows;
  26. const
  27. //copied from Ndis.h
  28. //the following constants are to be used to direct
  29. //the underlying NIC driver to choose which type of
  30. //packet can be delivered to the upper bound driver,
  31. //that is, our snoop driver.
  32. NDIS_PACKET_TYPE_DIRECTED =           $0001;
  33. NDIS_PACKET_TYPE_MULTICAST =          $0002;
  34. NDIS_PACKET_TYPE_ALL_MULTICAST =      $0004;
  35. NDIS_PACKET_TYPE_BROADCAST =          $0008;
  36. NDIS_PACKET_TYPE_SOURCE_ROUTING =     $0010;
  37. NDIS_PACKET_TYPE_PROMISCUOUS =        $0020; //for snoop
  38. NDIS_PACKET_TYPE_SMT =                $0040;
  39. NDIS_PACKET_TYPE_MAC_FRAME =          $8000;
  40. NDIS_PACKET_TYPE_FUNCTIONAL =         $4000;
  41. NDIS_PACKET_TYPE_ALL_FUNCTIONAL =     $2000;
  42. NDIS_PACKET_TYPE_GROUP =              $1000;
  43. //
  44. MAX_LINK_NAME_LENGTH =   5;
  45. type
  46. { Jagad like to have DELPHI3/DELPHI4 defined for lisibility }
  47. { VER100 is automatically defined by Delphi3 compiler       }
  48. { VER120 is automatically defined by Delphi4 compiler       }
  49. {$IFDEF VER100}
  50.     {$DEFINE DELPHI3}
  51. {$ELSE}
  52.     {$DEFINE DELPHI4}
  53. {$ENDIF}
  54. {$IFDEF DELPHI3}
  55.     USLONG = LongInt;
  56. {$ELSE}
  57.     USLONG = LongWord;
  58. {$ENDIF}
  59. //typedef struct tagADAPTER {
  60. // HANDLE     hFile;
  61. // TCHAR      szAdapterName[MAX_LINK_NAME_LENGTH];
  62. // TCHAR      SymbolicLink[MAX_PATH];
  63. //} ADAPTER, *LPADAPTER;
  64. ADAPTER = packed record
  65.   hFile:                THandle;
  66.   szAdapterName:        array [0..MAX_LINK_NAME_LENGTH-1] of Char;
  67.   SymbolicLink:         array [0..MAX_PATH-1] of Char;
  68. end;
  69. LPADAPTER = ^ADAPTER;
  70. //typedef struct tagPACKET {
  71. // HANDLE       hEvent;
  72. // OVERLAPPED   OverLapped;
  73. // PVOID        Buffer;
  74. // UINT         Length;
  75. //} PACKET, *LPPACKET;
  76. PACKET = packed record
  77.   hEvent:      THandle;
  78.   xOverlapped: TOVERLAPPED;    // FPiette May 10, 1999
  79.   Buffer:      PChar;
  80.   Length:      USLONG;         // FPiette May 17, 1999
  81. end;
  82. LPPACKET = ^PACKET;
  83. //typedef struct tagADAPTER_DESC {
  84. // TCHAR szAdapterName[MAX_LINK_NAME_LENGTH];
  85. // TCHAR szAdapterDesc[MAX_PATH];
  86. //} ADAPTER_DESC, *LPADAPTER_DESC;
  87. ADAPTER_DESC = packed record
  88.   szAdapterName:     array[0..MAX_LINK_NAME_LENGTH-1] of Char;
  89.   szAdapterDesc:     array[0..MAX_PATH-1] of Char;
  90. end;
  91. LPADAPTER_DESC = ^ADAPTER_DESC;
  92. (*
  93. PVOID WINAPI
  94. PacketOpenAdapter(
  95. LPTSTR   AdapterName
  96. /*
  97. description:
  98. open the adapter named as the argument.
  99. to get the adapter name, call PacketGetAdapterNames.
  100. return:
  101. if success, return the pointer to the allocated ADAPTER structure.
  102. else, return NULL
  103. */
  104. );
  105. *)
  106. function PacketOpenAdapter( AdapterName: LPSTR ): DWORD; stdcall;
  107. (*BOOLEAN  WINAPI
  108. PacketSendPacket(
  109. LPADAPTER   AdapterObject,
  110. LPPACKET    lpPacket,
  111. BOOLEAN     Sync
  112. /*
  113. description:
  114. send the user-supplied data via lpPacket.
  115. Sync value is ignored.
  116. return:
  117. if success, return TRUE
  118. else, return FALSE
  119. */
  120. );
  121. *)
  122. function PacketSendPacket( AdapterObject: LPADAPTER;
  123.                            plpPacket:     LPPACKET;
  124.                            Sync:          Boolean ): Boolean; stdcall;
  125. (*PVOID  WINAPI
  126. PacketAllocatePacket(
  127. LPADAPTER   AdapterObject
  128. /*
  129. description:
  130. allocate the new packet
  131. return:
  132. if success, return the pointer to the allocated PACKET structure
  133. else, return NULL
  134. */
  135. );
  136. *)
  137. function PacketAllocatePacket( AdapterObject: LPADAPTER ): DWORD; stdcall;
  138. (*VOID  WINAPI
  139. PacketInitPacket(
  140. LPPACKET    lpPacket,
  141. PVOID       Buffer,
  142. UINT        Length
  143. /*
  144. description:
  145. set lpPacket's buffer to the user-passed Buffer
  146. set lpPacket's length to the user-passed Length
  147. return:
  148. */
  149. );
  150. *)
  151. procedure PacketInitPacket(
  152.     plpPacket: LPPACKET;
  153.     Buffer:    PChar;
  154.     Length:    USLONG); stdcall;  // FPiette May 17, 1999
  155. (*VOID  WINAPI
  156. PacketFreePacket(
  157. LPPACKET    lpPacket
  158. /*
  159. description:
  160. free the pre-allocated lpPacket
  161. This doesn't free the lpPacket's buffer.
  162. return:
  163. */
  164. );
  165. *)
  166. procedure PacketFreePacket( plpPacket: LPPACKET ); stdcall;
  167. (*BOOLEAN  WINAPI
  168. PacketResetAdapter(
  169. LPADAPTER  AdapterObject
  170. /*
  171. description:
  172. flush the adpater's receive queue
  173. clear any pending if it is.
  174. return:
  175. */
  176. );
  177. *)
  178. function PacketResetAdapter( AdapterObject: LPADAPTER ): Boolean; stdcall;
  179. (*BOOLEAN  WINAPI
  180. PacketGetAddress(
  181. LPADAPTER  AdapterObject,
  182. PUCHAR     AddressBuffer,
  183. DWORD cbBytes,
  184. LPDWORD lpcbBytes
  185. /*
  186. description:
  187. get the adapter's current MAC address
  188. return:
  189. if success, AddressBuffer contains the MAC address,
  190. lpcbBytes is of the real size of the MAC address in bytes,
  191. return TRUE
  192. else, return FALSE
  193. */
  194. );
  195. *)
  196. function PacketGetAddress( AdapterObject: LPADAPTER;
  197.                            AddressByffer: PUCHAR;
  198.                            cbBytes:       DWORD;
  199.                            lpcbBytes:     LPDWORD): Boolean; stdcall;
  200. (*BOOLEAN  WINAPI
  201. PacketWaitPacket(
  202. LPADAPTER  AdapterObject,
  203. LPPACKET   lpPacket,
  204. PULONG     BytesReceived
  205. /*
  206. description:
  207. if the user called PacketReceivePacket with the 3rd argument, Sync, set to FALSE
  208. before calling this and PacketReceivePacket returned FALSE, then,
  209. this function waits until a packet is received.
  210. return:
  211. if success, the contents of the received packet is passed via lpPacket's Buffer,
  212. the length of the received packet in bytes is passed via *BytesReceived,
  213. and return TRUE
  214. else, return FALSE
  215. */
  216. );
  217. *)
  218. function PacketWaitPacket( AdapterObject: LPADAPTER;
  219.                            plpPacket:     LPPACKET;
  220.                            BytesReceived: PULONG): Boolean; stdcall;
  221. (*BOOLEAN WINAPI
  222. PacketReceivePacket(
  223. LPADAPTER   AdapterObject,
  224. LPPACKET    lpPacket,
  225. BOOLEAN     Sync,
  226. PULONG      BytesReceived
  227. /*
  228. description:
  229. receive a packet transitted over the transmission line attached to this PC.
  230. if Sync is set to FALSE, then it immediately returns (later, call PacketWaitPacket)
  231. ;otherwise, it waits until any packet is received.
  232. return:
  233. if success, the contents of the received packet is passed via lpPacket's Buffer,
  234. the length of the received packet in bytes is passed via *BytesReceived,
  235. and return TRUE
  236. else, *BytesReceived = 0 and return FALSE
  237. */
  238. );
  239. *)
  240. function PacketReceivePacket( AdapterObject: LPADAPTER;
  241.                               plpPacket:     LPPACKET;
  242.                               Sync:          Boolean;
  243.                               BytesReceived: PULONG): Boolean; stdcall;
  244. (*VOID WINAPI
  245. PacketCloseAdapter(
  246. LPADAPTER   lpAdapter
  247. /*
  248. description:
  249. close the underlying adpater
  250. return:
  251. */
  252. );
  253. *)
  254. procedure PacketCloseAdapter( AdapterObject: LPADAPTER ); stdcall;
  255. (*BOOLEAN WINAPI
  256. PacketSetFilter(
  257. LPADAPTER  AdapterObject,
  258. ULONG      Filter
  259. /*
  260. description:
  261. select the mode of how to receive packets.
  262. For Filter, see the topmost part of this header file.
  263. return:
  264. if success, return TRUE
  265. else, return FALSE
  266. */
  267. );
  268. *)
  269. function PacketSetFilter( AdapterObject: LPADAPTER;
  270.                           Filter:        ULONG): Boolean; stdcall;
  271. (*ULONG  WINAPI
  272. PacketGetAdapterNames(
  273. PVOID pAdapterDescs,
  274. UINT nAdapterDescs,
  275. PUINT pnAdapterDescsMax
  276. /*
  277. description:
  278. find all the adapters installed into this PC.
  279. The user should set the maximum bound of the number of adapters,
  280. normally 3 is enough.
  281. return:
  282. if found, *pnAdapterDescsMax is the total number of the found adapters
  283. and return TRUE.
  284. else, *pnAdapterDescsMax = 0 and return FALSE
  285. */
  286. );
  287. *)
  288. function PacketGetAdapterNames(
  289.     pAdapterDescs:      PChar;
  290.     nAdapterDescs:      USLONG;         // FPiette May 17, 1999
  291.     pnAdapterDescsMax:  PUINT ): BOOL; stdcall;
  292. (*BOOLEAN WINAPI
  293. PacketAdapterDesc(
  294. LPADAPTER AdapterObject,
  295. LPSTR lpszVendorSays,
  296. DWORD cbBytes,
  297. LPDWORD lpcbBytes
  298. /*
  299. description:
  300. get the vendor's description of the underlying NIC.
  301. return:
  302. if success, the vendor's description is passed via lpszVendorSays,
  303. *lpcbBytes is of the length of the description in bytes.
  304. and return TRUE.
  305. else, return FALSE.
  306. */
  307. );
  308. *)
  309. function PacketAdapterDesc( AdapterObject:  LPADAPTER;
  310.                             lpszVendorSays: LPSTR;
  311.                             cbBytes:        DWORD;
  312.                             lpcbBytes:      LPDWORD ): Boolean; stdcall;
  313. implementation
  314. const Packet32Lib = 'Packet32.dll';
  315. function PacketOpenAdapter; external Packet32Lib name 'PacketOpenAdapter';
  316. function PacketSendPacket; external Packet32Lib name 'PacketSendPacket';
  317. function PacketAllocatePacket; external Packet32Lib name 'PacketAllocatePacket';
  318. procedure PacketInitPacket; external Packet32Lib name 'PacketInitPacket';
  319. procedure PacketFreePacket; external Packet32Lib name 'PacketFreePacket';
  320. function PacketResetAdapter; external Packet32Lib name 'PacketResetAdapter';
  321. function PacketGetAddress; external Packet32Lib name 'PacketGetAddress';
  322. function PacketWaitPacket; external Packet32Lib name 'PacketWaitPacket';
  323. function PacketReceivePacket; external Packet32Lib name 'PacketReceivePacket';
  324. procedure PacketCloseAdapter; external Packet32Lib name 'PacketCloseAdapter';
  325. function PacketSetFilter; external Packet32Lib name 'PacketSetFilter';
  326. function PacketGetAdapterNames; external Packet32Lib name 'PacketGetAdapterNames';
  327. function PacketAdapterDesc; external Packet32Lib name 'PacketAdapterDesc';
  328. end.