Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
9.3 KiB

  1. // netwatch.cpp
  2. //
  3. // Copyright 2000 Microsoft Corporation, all rights reserved
  4. //
  5. // Created 2-00 anbrad
  6. //
  7. #include "netwatch.h"
  8. #include <shlwapi.h>
  9. #include "main.h"
  10. #include "dsubmit.h"
  11. // NETMON includes
  12. #include "blob.h"
  13. #include "irtc.h"
  14. #include "idelaydc.h"
  15. #include "nmerr.h"
  16. #include "adapter.h" // CAdapter
  17. static DWORD AddressOffsetTable[] = { 0, 0, 2, 1 };
  18. DWORD WINAPI StatusCallbackProc( UPDATE_EVENT Event );
  19. DWORD m_dwAdapters;
  20. CAdapter* m_pAdapters;
  21. HRESULT StartListening(HWND hwndParent)
  22. {
  23. HRESULT rc;
  24. HBLOB FilterBlob = 0;
  25. PBLOB_TABLE pBlobTable;
  26. //
  27. // Create a netmon blob
  28. //
  29. CreateBlob(&FilterBlob);
  30. //
  31. // We want realtime data
  32. //
  33. if (NOERROR != (rc = SetBoolInBlob( FilterBlob,
  34. OWNER_NPP,
  35. CATEGORY_CONFIG,
  36. TAG_INTERFACE_DELAYED_CAPTURE,
  37. TRUE)))
  38. {
  39. return E_FAIL;
  40. }
  41. //
  42. // Get the table of devices we can use
  43. //
  44. if (NOERROR != GetNPPBlobTable(
  45. FilterBlob,
  46. &pBlobTable))
  47. {
  48. return E_FAIL;
  49. }
  50. if (pBlobTable->dwNumBlobs < 0)
  51. return E_FAIL;
  52. m_dwAdapters = pBlobTable->dwNumBlobs;
  53. m_pAdapters = new CAdapter[m_dwAdapters];
  54. if (!m_pAdapters)
  55. return E_FAIL;
  56. for (CAdapter* pAdapter = m_pAdapters;
  57. pAdapter < m_pAdapters + m_dwAdapters;
  58. ++pAdapter)
  59. {
  60. // Grab the first one.
  61. pAdapter->m_hBlob = pBlobTable->hBlobs[pAdapter - m_pAdapters];
  62. if (!SUCCEEDED(rc = CreateNPPInterface(pAdapter->m_hBlob,
  63. IID_IDelaydC,
  64. (LPVOID*) &pAdapter->m_pRtc)))
  65. {
  66. return E_FAIL;
  67. }
  68. const char* MacType;
  69. if (NMERR_SUCCESS != GetStringFromBlob(pAdapter->m_hBlob,
  70. OWNER_NPP,
  71. CATEGORY_NETWORKINFO,
  72. TAG_MACTYPE,
  73. &MacType))
  74. {
  75. return E_FAIL;
  76. }
  77. //
  78. // Grab the Mac Address
  79. //
  80. if (NMERR_SUCCESS != GetMacAddressFromBlob(pAdapter->m_hBlob,
  81. OWNER_NPP,
  82. CATEGORY_LOCATION,
  83. TAG_MACADDRESS,
  84. pAdapter->MacAddress))
  85. {
  86. return E_FAIL;
  87. }
  88. }
  89. // Start the capture
  90. StartCapture();
  91. return S_OK;
  92. }
  93. void StartCapture()
  94. {
  95. DWORD rc;
  96. NETWORKINFO NetworkInfo;
  97. HBLOB hErrorBlob;
  98. BOOL WantProtocolInfo = TRUE;
  99. DWORD Value = 1;
  100. BOOL bRas;
  101. // initialize the error blob
  102. CreateBlob(&hErrorBlob);
  103. for (CAdapter* pAdapter = m_pAdapters;
  104. pAdapter < m_pAdapters + m_dwAdapters;
  105. ++pAdapter)
  106. {
  107. pAdapter->m_dwFrames = 0;
  108. pAdapter->m_qBytes = 0;
  109. rc = GetBoolFromBlob(
  110. pAdapter->m_hBlob,
  111. OWNER_NPP,
  112. CATEGORY_LOCATION,
  113. TAG_RAS,
  114. &bRas);
  115. if( rc != NMERR_SUCCESS )
  116. {
  117. goto START_FAILURE;
  118. }
  119. if (bRas)
  120. continue;
  121. //
  122. // get the networkinfo from the blob that we may need
  123. //
  124. rc = GetNetworkInfoFromBlob(pAdapter->m_hBlob,
  125. &NetworkInfo);
  126. pAdapter->m_dwLinkSpeed = NetworkInfo.LinkSpeed;
  127. pAdapter->m_dwHeaderOffset = AddressOffsetTable[NetworkInfo.MacType];
  128. //
  129. // Set the WantProtocolInfo
  130. //
  131. rc = SetBoolInBlob( pAdapter->m_hBlob,
  132. OWNER_NPP,
  133. CATEGORY_CONFIG,
  134. TAG_WANT_PROTOCOL_INFO,
  135. WantProtocolInfo);
  136. if( rc != NMERR_SUCCESS )
  137. {
  138. goto START_FAILURE;
  139. }
  140. //
  141. // connect to and configure the specified network
  142. //
  143. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Connect(
  144. pAdapter->m_hBlob,
  145. StatusCallbackProc,
  146. (LPVOID)pAdapter,
  147. hErrorBlob));
  148. if( rc != NMERR_SUCCESS )
  149. {
  150. // the connect failed
  151. goto START_FAILURE;
  152. }
  153. //
  154. // start the capture
  155. //
  156. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Start(pAdapter->m_szCaptureFile));
  157. if( rc != NMERR_SUCCESS )
  158. {
  159. // the start failed
  160. goto START_FAILURE;
  161. }
  162. } // for each adapter
  163. START_FAILURE:
  164. DestroyBlob(hErrorBlob);
  165. }
  166. void SaveCapture()
  167. {
  168. DWORD rc;
  169. TCHAR szScratch[MAX_PATH];
  170. TCHAR szMachine[MAX_COMPUTERNAME_LENGTH+1];
  171. DWORD dwMachine = MAX_COMPUTERNAME_LENGTH+1;
  172. TCHAR szFileName[MAX_PATH];
  173. HANDLE hFile = INVALID_HANDLE_VALUE;
  174. DWORD dwWritten;
  175. HBLOB hErrorBlob = NULL;
  176. CreateBlob(&hErrorBlob);
  177. ZeroMemory(szMachine, sizeof(szMachine));
  178. GetComputerName(szMachine, &dwMachine);
  179. for (CAdapter* pAdapter = m_pAdapters;
  180. pAdapter < m_pAdapters + m_dwAdapters;
  181. ++pAdapter)
  182. {
  183. if (_tcslen(pAdapter->m_szCaptureFile) == 0)
  184. continue;
  185. //
  186. // copy capture file to
  187. // \\scratch\scratch\anbrad\user:MACHINE1 1234.cap
  188. //
  189. _tcscpy (szFileName, pAdapter->m_szCaptureFile);
  190. PathStripPath(szFileName);
  191. _tcscpy (szScratch, "\\\\scratch\\scratch\\anbrad\\");
  192. _tcscat (szScratch, g_szName);
  193. _tcscat (szScratch, " - ");
  194. _tcscat (szScratch, szMachine);
  195. _tcscat (szScratch, " ");
  196. _tcscat (szScratch, szFileName);
  197. if (!CopyFile(pAdapter->m_szCaptureFile, szScratch, FALSE))
  198. {
  199. DWORD dw = GetLastError();
  200. goto Error;
  201. }
  202. //
  203. // Save the problem
  204. //
  205. PathRemoveExtension(szScratch);
  206. PathAddExtension(szScratch, TEXT(".txt"));
  207. hFile = CreateFile(
  208. szScratch,
  209. GENERIC_READ | GENERIC_WRITE,
  210. 0,
  211. NULL,
  212. CREATE_NEW,
  213. FILE_ATTRIBUTE_NORMAL,
  214. NULL);
  215. if (INVALID_HANDLE_VALUE == hFile)
  216. goto Error;
  217. if (!WriteFile(hFile, g_szProblem, _tcslen(g_szProblem), &dwWritten, NULL))
  218. goto Error;
  219. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Configure(
  220. pAdapter->m_hBlob,
  221. hErrorBlob));
  222. if( rc != NMERR_SUCCESS )
  223. {
  224. // the start failed
  225. goto Error;
  226. }
  227. //
  228. // start the capture
  229. //
  230. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Start(pAdapter->m_szCaptureFile));
  231. if( rc != NMERR_SUCCESS )
  232. {
  233. // the start failed
  234. goto Error;
  235. }
  236. }
  237. Error:
  238. if (hFile != INVALID_HANDLE_VALUE)
  239. CloseHandle(hFile);
  240. if (hErrorBlob)
  241. DestroyBlob(hErrorBlob);
  242. return;
  243. }
  244. void EndCapture()
  245. {
  246. DWORD rc;
  247. STATISTICS stats;
  248. for (CAdapter* pAdapter = m_pAdapters;
  249. pAdapter < m_pAdapters + m_dwAdapters;
  250. ++pAdapter)
  251. {
  252. //
  253. // stop capturing
  254. //
  255. pAdapter->m_pRtc->Stop(&stats);
  256. // disconnect from the network
  257. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Disconnect());
  258. if( rc != NMERR_SUCCESS )
  259. {
  260. //TRACE("NPPDisconnect failed: %d\n", rc);
  261. }
  262. }
  263. }
  264. void StopCapture()
  265. {
  266. STATISTICS stats;
  267. for (CAdapter* pAdapter = m_pAdapters;
  268. pAdapter < m_pAdapters + m_dwAdapters;
  269. ++pAdapter)
  270. {
  271. //
  272. // stop capturing
  273. //
  274. pAdapter->m_pRtc->Stop(&stats);
  275. }
  276. }
  277. void RestartCapture()
  278. {
  279. DWORD rc;
  280. HBLOB hErrorBlob = NULL;
  281. CreateBlob(&hErrorBlob);
  282. for (CAdapter* pAdapter = m_pAdapters;
  283. pAdapter < m_pAdapters + m_dwAdapters;
  284. ++pAdapter)
  285. {
  286. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Configure(
  287. pAdapter->m_hBlob,
  288. hErrorBlob));
  289. if( rc != NMERR_SUCCESS )
  290. {
  291. // the start failed
  292. goto Error;
  293. }
  294. //
  295. // start the capture
  296. //
  297. rc = HRESULT_TO_NMERR(pAdapter->m_pRtc->Start(pAdapter->m_szCaptureFile));
  298. if( rc != NMERR_SUCCESS )
  299. {
  300. // the start failed
  301. goto Error;
  302. }
  303. }
  304. Error:
  305. if (hErrorBlob)
  306. DestroyBlob(hErrorBlob);
  307. }
  308. // ----------------------------------------------------------------------------
  309. DWORD WINAPI StatusCallbackProc( UPDATE_EVENT Event )
  310. {
  311. CAdapter* pAdapter = (CAdapter*) Event.lpUserContext;
  312. // we could do stuff here, but what?
  313. return( NMERR_SUCCESS );
  314. }