PACKET32.C
上传用户:d748196
上传日期:2007-01-07
资源大小:42k
文件大小:18k
源码类别:

网络截获/分析

开发平台:

WINDOWS

  1. // packet32.c - PacketInit PacketOpenAdapter PacketCloseAdapter
  2. //              PacketAllocatePacket PacketFreePacket PacketInitPacket
  3. //              PacketSendPacket PacketReceivePacket PacketWaitPacket
  4. //              PacketResetAdapter PacketRequest PacketSetFilter
  5. //              PacketGetAddr StartPacketDriver PacketGetAdapterNames
  6. //              PacketGetName
  7. // 
  8. // Author: Christopher Chlap chrisc@fir.canberra.edu.au 28. November, 1997
  9. //
  10. // #define UNICODE 1
  11. #include <windows.h>
  12. #include <windowsx.h>
  13. #include "packet32.h"
  14. //------------------------------------------------------------------------
  15. //
  16. //  Globals
  17. //
  18. //------------------------------------------------------------------------
  19. TCHAR   szWindowTitle[] = TEXT("PACKET32.DLL");
  20. //------------------------------------------------------------------------
  21. //
  22. //  Prototypes
  23. //
  24. //------------------------------------------------------------------------
  25. #if DBG
  26. #define ODS(_x) OutputDebugString(TEXT(_x))
  27. #else
  28. #define ODS(_x)
  29. #endif
  30. BOOLEAN StartPacketDriver(LPTSTR ServiceName);
  31. //------------------------------------------------------------------------
  32. //
  33. //  PacketInit - Start the Packet Driver
  34. //
  35. //      Args:   DllHandle   - not used
  36. //              Option      - attach or detach
  37. //              Context     - not used
  38. //
  39. //------------------------------------------------------------------------
  40. BOOLEAN PacketInit(
  41.     IN PVOID DllHandle,
  42.     IN ULONG Option,
  43.     IN PCONTEXT Context OPTIONAL)
  44. {
  45.     BOOLEAN     Status;
  46.     ODS("Packet32: DllEntryn");
  47.     switch (Option) {
  48.         case DLL_PROCESS_ATTACH:
  49.             Status = StartPacketDriver(TEXT("PACKET"));
  50.         case DLL_PROCESS_DETACH:
  51.         default:
  52.             break;
  53.     }
  54.     return Status;
  55. }
  56. //------------------------------------------------------------------------
  57. //
  58. //  PacketOpenAdapter - Open the named adapter
  59. //
  60. //      Args:   AdapterName - the UNICODE registry name of the adapter
  61. //
  62. //   Returns:   pointer to an Adapter or NULL
  63. // 
  64. //------------------------------------------------------------------------
  65. PVOID PacketOpenAdapter(LPTSTR AdapterName)
  66. {
  67.     LPADAPTER  lpAdapter;
  68.     BOOLEAN    Result;
  69.     ODS("Packet32: PacketOpenAdaptern");
  70.     lpAdapter = (LPADAPTER)GlobalAllocPtr(
  71.                     GMEM_MOVEABLE | GMEM_ZEROINIT,
  72.                     sizeof(ADAPTER));
  73.     if (lpAdapter == NULL) {
  74.         ODS("Packet32: PacketOpenAdapter GlobalAlloc Failedn");
  75.         return NULL;
  76.     }
  77.     wsprintf(
  78.         lpAdapter->SymbolicLink,
  79.         TEXT("\\.\%s%s"),
  80.         DOSNAMEPREFIX,
  81.         &AdapterName[8]);
  82.     Result = DefineDosDevice(
  83.         DDD_RAW_TARGET_PATH,
  84.         &lpAdapter->SymbolicLink[4],
  85.         AdapterName);
  86.     if (Result) {
  87.         lpAdapter->hFile = CreateFile(lpAdapter->SymbolicLink,
  88.                                 GENERIC_WRITE | GENERIC_READ,
  89.                                 0,
  90.                                 NULL,
  91.                                 CREATE_ALWAYS,
  92.                                 FILE_FLAG_OVERLAPPED,
  93.                                 0);
  94.         if (lpAdapter->hFile != INVALID_HANDLE_VALUE)
  95.             return lpAdapter;
  96.     }
  97.     ODS("Packet32: PacketOpenAdapter Could not open adaptern");
  98.     GlobalFreePtr(lpAdapter);
  99.     return NULL;
  100. }
  101. //------------------------------------------------------------------------
  102. //
  103. //  PacketCloseAdapter - Close the specified adapter
  104. //
  105. //      Args:   lpAdapter - pointer to an adapter object
  106. //
  107. //   Returns:   
  108. // 
  109. //------------------------------------------------------------------------
  110. VOID PacketCloseAdapter(LPADAPTER lpAdapter)
  111. {
  112.     ODS("Packet32: PacketCloseAdaptern");
  113.     CloseHandle(lpAdapter->hFile);
  114.     GlobalFreePtr(lpAdapter);
  115. }
  116. //------------------------------------------------------------------------
  117. //
  118. //  PacketAllocatePacket - allocate a Packet for send and receive
  119. //
  120. //      Args:   none
  121. //
  122. //   Returns:   pointer to a Packet or NULL   
  123. // 
  124. //------------------------------------------------------------------------
  125. PVOID PacketAllocatePacket()
  126. {
  127.     LPPACKET    lpPacket;
  128.     lpPacket = (LPPACKET)GlobalAllocPtr(
  129.                              GMEM_MOVEABLE | GMEM_ZEROINIT,
  130.                              sizeof(PACKET));
  131.     if (lpPacket == NULL) {
  132.         ODS("Packet32: PacketAllocateSendPacket: GlobalAlloc Failedn");
  133.         return NULL;
  134.     }
  135.     lpPacket->OverLapped.hEvent=CreateEvent(NULL, FALSE, FALSE, NULL);
  136.     if (lpPacket->OverLapped.hEvent == NULL) {
  137.         ODS("Packet32: PacketAllocateSendPacket: CreateEvent Failedn");
  138.         GlobalFreePtr(lpPacket);
  139.         return NULL;
  140.     }
  141.     return lpPacket;
  142. }
  143. //------------------------------------------------------------------------
  144. //
  145. //  PacketFreePacket - release a Packet
  146. //
  147. //      Args:   lpPacket - pointer to a Packet
  148. //
  149. //   Returns:
  150. // 
  151. //------------------------------------------------------------------------
  152. VOID PacketFreePacket(LPPACKET lpPacket)
  153. {
  154.     CloseHandle(lpPacket->OverLapped.hEvent);
  155.     GlobalFreePtr(lpPacket);
  156. }
  157. //------------------------------------------------------------------------
  158. //
  159. //  PacketInitPacket - initialize an allocated Packet
  160. //
  161. //      Args:   lpPacket - pointer to a Packet
  162. //              Buffer   - buffer pointer
  163. //              Length   - length of buffer
  164. //
  165. //   Returns:
  166. // 
  167. //------------------------------------------------------------------------
  168. VOID PacketInitPacket(LPPACKET lpPacket, PVOID Buffer, UINT Length)
  169. {
  170.     lpPacket->Buffer = Buffer;
  171.     lpPacket->Length = Length;
  172. }
  173. //------------------------------------------------------------------------
  174. //
  175. //  PacketSendPacket - send a Packet
  176. //
  177. //      Args:   lpAdapter - pointer to an Adapter
  178. //              lpPacket  - pointer to a Packet
  179. //              Sync      - synchronous or asynchronous I/O
  180. //
  181. //   Returns:   TRUE - synchronous operation completed or
  182. //                     asynchronous operation is pending
  183. // 
  184. //------------------------------------------------------------------------
  185. BOOLEAN PacketSendPacket(LPADAPTER lpAdapter,
  186.                          LPPACKET lpPacket,
  187.                          BOOLEAN Sync)
  188. {
  189.     BOOLEAN Result;
  190.     DWORD   BytesTransfered;
  191.     lpPacket->OverLapped.Offset = 0;
  192.     lpPacket->OverLapped.OffsetHigh = 0;
  193.     if (!ResetEvent(lpPacket->OverLapped.hEvent)) return FALSE;
  194.     Result=WriteFile(lpAdapter->hFile,
  195.                      lpPacket->Buffer,
  196.                      lpPacket->Length,
  197.                      &BytesTransfered,
  198.                      &lpPacket->OverLapped);
  199.     if (Sync) Result = GetOverlappedResult(lpAdapter->hFile,
  200.                                            &lpPacket->OverLapped,
  201.                                            &BytesTransfered,
  202.                                            TRUE);
  203.     else 
  204.     
  205.         Result = TRUE;
  206.     return Result;
  207. }
  208. //------------------------------------------------------------------------
  209. //
  210. //  PacketReceivePacket - receive a Packet
  211. //
  212. //      Args:   lpAdapter   - pointer to an Adapter
  213. //              lpPacket    - pointer to a Packet
  214. //              Sync        - synchronous or asynchronous I/O
  215. //              BytesReveived   - pointer to number of bytes received
  216. //
  217. //   Returns:   TRUE  - synchronous operation completed or
  218. //                      asynchronous operation is pending
  219. // 
  220. //------------------------------------------------------------------------
  221. BOOLEAN PacketReceivePacket(LPADAPTER lpAdapter,
  222.                             LPPACKET  lpPacket,
  223.                             BOOLEAN   Sync,
  224.                             PULONG    BytesReceived)
  225. {
  226.     BOOLEAN Result;
  227.     lpPacket->OverLapped.Offset = 0;
  228.     lpPacket->OverLapped.OffsetHigh = 0;
  229.     if (!ResetEvent(lpPacket->OverLapped.hEvent)) return FALSE;
  230.     Result = ReadFile(lpAdapter->hFile,
  231.                       lpPacket->Buffer,
  232.                       lpPacket->Length,
  233.                       BytesReceived,
  234.                       &lpPacket->OverLapped);
  235.     if (Sync) Result = GetOverlappedResult(lpAdapter->hFile,
  236.                                            &lpPacket->OverLapped,
  237.                                            BytesReceived,
  238.                                            TRUE);
  239.     else 
  240.         
  241.         Result = TRUE;
  242.     return Result;
  243. }
  244. //------------------------------------------------------------------------
  245. //
  246. //  PacketWaitPacket - wait for an asynchronous operation to complete
  247. //
  248. //      Args:   lpAdapter     - pointer to an Adapter object
  249. //              lpPacket      - pointer to a Packet
  250. //              BytesReceived - pointer to a bytes received ULONG
  251. //
  252. //   Returns:   result of asynchronous operation
  253. // 
  254. //------------------------------------------------------------------------
  255. BOOLEAN PacketWaitPacket(LPADAPTER lpAdapter,
  256.                          LPPACKET  lpPacket,
  257.                          PULONG    BytesReceived)
  258. {
  259.     return GetOverlappedResult(lpAdapter->hFile,
  260.                                &lpPacket->OverLapped,
  261.                                BytesReceived,
  262.                                TRUE);
  263. }
  264. //------------------------------------------------------------------------
  265. //
  266. //  PacketResetAdapter - reset adapter, completing all pending operations
  267. //
  268. //      Args:   lpAdapter - pointer to an Adapter
  269. //
  270. //   Returns:   TRUE
  271. // 
  272. //------------------------------------------------------------------------
  273. BOOLEAN PacketResetAdapter(LPADAPTER lpAdapter)
  274. {
  275.     UINT    BytesReturned;
  276.     DeviceIoControl(lpAdapter,
  277.                     (DWORD)IOCTL_PROTOCOL_RESET,
  278.                     NULL,
  279.                     0,
  280.                     NULL,
  281.                     0,
  282.                     &BytesReturned,
  283.                     NULL);
  284.     return TRUE;
  285. }
  286. //------------------------------------------------------------------------
  287. //
  288. //  PacketRequest - issue a request to an adapter
  289. //
  290. //      Args:   lpAdapter - pointer to an Adapter
  291. //              Set       - set OID if TRUE else get OID
  292. //              OidData - pointer to OID data
  293. //
  294. //   Returns:   Number of bytes returned   
  295. // 
  296. //------------------------------------------------------------------------
  297. ULONG PacketRequest(LPADAPTER lpAdapter,
  298.                     BOOLEAN   Set,
  299.                     PPACKET_OID_DATA OidData)
  300. {
  301.     UINT       BytesReturned = 0;
  302.     UINT       Result;
  303.     Result = DeviceIoControl(lpAdapter->hFile,
  304.     (DWORD) Set ? IOCTL_PROTOCOL_SET_OID : IOCTL_PROTOCOL_QUERY_OID, OidData,
  305.                              sizeof(PACKET_OID_DATA)-1+OidData->Length,
  306.                              OidData,
  307.                              sizeof(PACKET_OID_DATA)-1+OidData->Length,
  308.                              &BytesReturned,
  309.                              NULL);
  310.     return BytesReturned;
  311. }
  312. //------------------------------------------------------------------------
  313. //
  314. //  PacketSetFilter - issue a set filter request to an adapter
  315. //
  316. //      Args:   lpAdapter  - pointer to an Adapter
  317. //              Filter     - filter to set
  318. //
  319. //   Returns:   
  320. // 
  321. //------------------------------------------------------------------------
  322. BOOLEAN PacketSetFilter(LPADAPTER lpAdapter, ULONG Filter)
  323. {
  324.     BOOLEAN Status;
  325.     ULONG   IoCtlBufferLength = (sizeof(PACKET_OID_DATA)+sizeof(ULONG)-1);
  326.     PPACKET_OID_DATA OidData;
  327.     OidData = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, IoCtlBufferLength);
  328.     if (OidData == NULL) return FALSE;
  329.     OidData->Oid = OID_GEN_CURRENT_PACKET_FILTER;
  330.     OidData->Length = sizeof(ULONG);
  331.     *((PULONG)OidData->Data) = Filter;
  332.     Status = PacketRequest(lpAdapter, TRUE, OidData);
  333.     GlobalFreePtr(OidData);
  334.     return Status;
  335. }
  336. //------------------------------------------------------------------------
  337. //
  338. //  PacketGetAddress - issue a get address request to an adapter
  339. //
  340. //      Args:   lpAdapter     - pointer to an Adapter
  341. //              AddressBuffer - buffer to hold the address
  342. //
  343. //   Returns:   number of bytes copied to address buffer   
  344. // 
  345. //------------------------------------------------------------------------
  346. ULONG PacketGetAddress(LPADAPTER lpAdapter, PUCHAR AddressBuffer)
  347.                              
  348. {
  349.     ULONG   Result;
  350.     
  351.     BYTE iBuf[sizeof(PACKET_OID_DATA) + 128];
  352.     PPACKET_OID_DATA pOidData = (PPACKET_OID_DATA) iBuf;
  353.     pOidData->Oid = OID_802_3_CURRENT_ADDRESS;
  354.     pOidData->Length = 6;
  355.     pOidData->Data[0] = 0;
  356.     Result = PacketRequest(lpAdapter, 0, pOidData);
  357.     if (Result > 0) {
  358.         memcpy(AddressBuffer, &iBuf[8], 6);
  359.         Result = 6;
  360.     }
  361.     else
  362.         Result = 0;
  363.         
  364.     return Result;
  365. }
  366. //------------------------------------------------------------------------
  367. //
  368. //  StartPacketDriver - starts the kernel mode packet driver
  369. //
  370. //      Args:   ServiceName - name of service to start
  371. //
  372. //   Returns:   
  373. // 
  374. //------------------------------------------------------------------------
  375. BOOLEAN StartPacketDriver(LPTSTR ServiceName)
  376. {
  377.     BOOLEAN  Status;
  378.     SC_HANDLE  SCManagerHandle;
  379.     SC_HANDLE  SCServiceHandle;
  380.     // Open a handle to the SC Manager database.
  381.     SCManagerHandle = OpenSCManager(NULL,       // local machine
  382.                                     NULL,       // ServicesActive database
  383.                                     SC_MANAGER_ALL_ACCESS);
  384.     if (SCManagerHandle==NULL) {
  385.         MessageBox(NULL,TEXT("Could not open SC"), szWindowTitle, MB_OK);
  386.         return FALSE;
  387.     } else {
  388.         SCServiceHandle = OpenService(SCManagerHandle,
  389.                                       ServiceName,
  390.                                       SERVICE_START);
  391.         if (SCServiceHandle == NULL)
  392.             MessageBox(NULL,TEXT("Could not open service"),szWindowTitle,MB_OK);
  393.         Status = StartService(SCServiceHandle, 0, NULL);
  394.         if (!Status) {
  395.             if (GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) {
  396.                 ODS("Packet32: Packet service already startedn");
  397.                 return TRUE;
  398.             }
  399.         }
  400.         return Status;
  401.     }
  402. }
  403. //------------------------------------------------------------------------
  404. //
  405. //  PacketGetAdapterNames - returns the names of all available adapters
  406. //
  407. //      Args:   pStr   - pointer to buffer for UNICODE names
  408. //              pSize  - address of ulong containing string length
  409. //
  410. //   Returns:   
  411. // 
  412. //------------------------------------------------------------------------
  413. ULONG PacketGetAdapterNames(PTSTR pStr, PULONG pSize)
  414. {
  415.     HKEY    SystemKey;
  416.     HKEY    ControlSetKey;
  417.     HKEY    ServicesKey;
  418.     HKEY    NdisPerfKey;
  419.     HKEY    LinkageKey;
  420.     LONG    Status;
  421.     DWORD   RegType;
  422.     Status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  423.                           TEXT("SYSTEM"),
  424.                           0,
  425.                           KEY_READ,
  426.                           &SystemKey);
  427.     if (Status == ERROR_SUCCESS) {
  428.         Status = RegOpenKeyEx(SystemKey,
  429.                               TEXT("CurrentControlSet"),
  430.                               0,
  431.                               KEY_READ,
  432.                               &ControlSetKey);
  433.         if (Status == ERROR_SUCCESS) {
  434.             Status = RegOpenKeyEx(ControlSetKey,
  435.                                   TEXT("Services"),
  436.                                   0,
  437.                                   KEY_READ,
  438.                                   &ServicesKey);
  439.             if (Status == ERROR_SUCCESS) {
  440.                 Status = RegOpenKeyEx(ServicesKey,
  441.                                       TEXT("Packet"),
  442.                                       0,
  443.                                       KEY_READ,
  444.                                       &NdisPerfKey);
  445.                 if (Status == ERROR_SUCCESS) {
  446.                     Status = RegOpenKeyEx(NdisPerfKey,
  447.                                           TEXT("Linkage"),
  448.                                           0,
  449.                                           KEY_READ,
  450.                                           &LinkageKey);
  451.                     if (Status == ERROR_SUCCESS) {
  452.                         Status = RegQueryValueEx(LinkageKey,
  453.                                                  TEXT("Export"),
  454.                                                  NULL,
  455.                                                  &RegType,
  456.                                                  (LPBYTE)pStr,
  457.                                                  pSize);
  458.                         RegCloseKey(LinkageKey);
  459.                     }
  460.                     RegCloseKey(NdisPerfKey);
  461.                 }
  462.                 RegCloseKey(ServicesKey);
  463.             }
  464.             RegCloseKey(ControlSetKey);
  465.         }
  466.         RegCloseKey(SystemKey);
  467.     }
  468.     return Status;
  469. }
  470. //------------------------------------------------------------------------
  471. //
  472. //  PacketGetName - returns the name of a specified adapter
  473. //
  474. //      Args:   pStr - pointer to buffer for a UNICODE name
  475. //              n    - which name to return
  476. //
  477. //   Returns:   TRUE  if the n'th name was found
  478. //              FALSE if the n'th name does not exist   
  479. // 
  480. //------------------------------------------------------------------------
  481. ULONG PacketGetName(PTSTR pStr, ULONG n)
  482. {
  483.     TCHAR   buf[256];
  484.     TCHAR   *Name;
  485.     ULONG   NameLength = 256;
  486.     Name = buf;
  487.         
  488.     PacketGetAdapterNames(Name, &NameLength);
  489.     switch (n) {
  490.         case 3: Name = &Name[strlen(Name)+1];
  491.                 if (!strlen(Name))
  492.                     break;
  493.         case 2: Name = (char *) &Name[strlen(Name)+1];
  494.                 if (!strlen(Name))
  495.                     break;
  496.         case 1: Name = (char *) &Name[strlen(Name)+1];
  497.                 if (!strlen(Name))
  498.                     break;
  499.         case 0: break;
  500.     }
  501.     
  502.     if (strlen(Name) != 0) {
  503.         strcpy(pStr, Name);
  504.         return TRUE;
  505.         }
  506.     else
  507.         return FALSE;
  508. }