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.

686 lines
11 KiB

  1. /*++
  2. Copyright (c) 1990-1998 Microsoft Corporation, All Rights Reserved.
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. This file contains routines for starting and stopping the driver and
  7. a few other helper routines.
  8. Author:
  9. Anil Francis Thomas (10/98)
  10. Environment:
  11. User mode
  12. Revision History:
  13. --*/
  14. #include <windows.h>
  15. #include <winioctl.h>
  16. #include <winsvc.h>
  17. #include "assert.h"
  18. #include <stdio.h>
  19. #include "atmsample.h"
  20. #include "utils.h"
  21. #include "atmsmapp.h"
  22. const int TIMEOUT_COUNT = 15;
  23. DWORD AtmSmDriverCheckState(
  24. PDWORD pdwState
  25. )
  26. /*++
  27. Routine Description:
  28. Check whether the driver is installed, removed, running etc.
  29. Arguments:
  30. pdwState - State of the driver
  31. Return Value:
  32. NO_ERROR - If successful
  33. Others - failure
  34. --*/
  35. {
  36. SC_HANDLE hSCMan = NULL;
  37. SC_HANDLE hDriver = NULL;
  38. DWORD dwStatus = NO_ERROR;
  39. SERVICE_STATUS ServiceStatus;
  40. *pdwState = DriverStateNotInstalled;
  41. do
  42. { // break off loop
  43. hSCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  44. if(NULL == hSCMan)
  45. {
  46. dwStatus = GetLastError();
  47. printf("Failed to OpenSCManager. Error %u\n", dwStatus);
  48. break;
  49. }
  50. //open service to see if it exists
  51. hDriver = OpenService(hSCMan, ATMSM_SERVICE_NAME, SERVICE_ALL_ACCESS);
  52. if(NULL != hDriver)
  53. {
  54. *pdwState = DriverStateInstalled;
  55. //service does exist
  56. if(QueryServiceStatus(hDriver, &ServiceStatus))
  57. {
  58. switch(ServiceStatus.dwCurrentState)
  59. {
  60. case SERVICE_STOPPED:
  61. printf("AtmSmDrv current status -- STOPPED\n");
  62. break;
  63. case SERVICE_RUNNING:
  64. printf("AtmSmDrv current status -- RUNNING\n");
  65. *pdwState = DriverStateRunning;
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. }
  72. else
  73. {
  74. printf("Failed to OpenService - Service not installed\n");
  75. // driver not installed.
  76. }
  77. } while(FALSE);
  78. if(NULL != hDriver)
  79. CloseServiceHandle(hDriver);
  80. if(NULL != hSCMan)
  81. CloseServiceHandle(hSCMan);
  82. return dwStatus;
  83. }
  84. DWORD AtmSmDriverStart(
  85. )
  86. /*++
  87. Routine Description:
  88. Start the AtmSmDrv.Sys.
  89. Arguments:
  90. -
  91. Return Value:
  92. NO_ERROR - If successful
  93. Others - failure
  94. --*/
  95. {
  96. DWORD dwState, dwStatus;
  97. if(NO_ERROR != (dwStatus = AtmSmDriverCheckState(&dwState)))
  98. {
  99. return dwStatus;
  100. }
  101. switch(dwState)
  102. {
  103. case DriverStateNotInstalled:
  104. dwStatus = ERROR_SERVICE_DOES_NOT_EXIST; // not installed
  105. break;
  106. case DriverStateRunning:
  107. break;
  108. case DriverStateInstalled: {
  109. // the service is installed, start it
  110. SC_HANDLE hSCMan = NULL;
  111. SC_HANDLE hDriver = NULL;
  112. SERVICE_STATUS ServiceStatus;
  113. int i;
  114. do
  115. { // break off loop
  116. hSCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  117. if(NULL == hSCMan)
  118. {
  119. dwStatus = GetLastError();
  120. printf("Failed to OpenSCManager. Error - %u\n", dwStatus);
  121. break;
  122. }
  123. hDriver = OpenService(hSCMan, ATMSM_SERVICE_NAME,
  124. SERVICE_ALL_ACCESS);
  125. // start service
  126. if(!hDriver || !StartService(hDriver, 0, NULL))
  127. {
  128. dwStatus = GetLastError();
  129. printf("Failed to StartService - Error %u\n", dwStatus);
  130. break;
  131. }
  132. dwStatus = ERROR_TIMEOUT;
  133. // query state of service
  134. for(i = 0; i < TIMEOUT_COUNT; i++)
  135. {
  136. Sleep(1000);
  137. if(QueryServiceStatus(hDriver, &ServiceStatus))
  138. {
  139. if(ServiceStatus.dwCurrentState == SERVICE_RUNNING)
  140. {
  141. dwStatus = NO_ERROR;
  142. break;
  143. }
  144. }
  145. }
  146. } while(FALSE);
  147. if(NULL != hDriver)
  148. CloseServiceHandle(hDriver);
  149. if(NULL != hSCMan)
  150. CloseServiceHandle(hSCMan);
  151. break;
  152. }
  153. }
  154. return dwStatus;
  155. }
  156. DWORD AtmSmDriverStop(
  157. )
  158. /*++
  159. Routine Description:
  160. Stop the AtmSmDrv.Sys.
  161. Arguments:
  162. -
  163. Return Value:
  164. NO_ERROR - If successful
  165. Others - failure
  166. --*/
  167. {
  168. DWORD dwState, dwStatus;
  169. if(NO_ERROR != (dwStatus = AtmSmDriverCheckState(&dwState)))
  170. {
  171. return dwStatus;
  172. }
  173. switch(dwState)
  174. {
  175. case DriverStateNotInstalled:
  176. case DriverStateInstalled:
  177. // not running
  178. break;
  179. case DriverStateRunning: {
  180. // the service is running, stop it
  181. SC_HANDLE hSCMan = NULL;
  182. SC_HANDLE hDriver = NULL;
  183. SERVICE_STATUS ServiceStatus;
  184. int i;
  185. do
  186. { // break off loop
  187. hSCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  188. if(NULL == hSCMan)
  189. {
  190. dwStatus = GetLastError();
  191. printf("Failed to OpenSCManager. Error %u\n", dwStatus);
  192. break;
  193. }
  194. hDriver = OpenService(hSCMan, ATMSM_SERVICE_NAME,
  195. SERVICE_ALL_ACCESS);
  196. // stop service
  197. if(!hDriver || !ControlService(hDriver,
  198. SERVICE_CONTROL_STOP,
  199. &ServiceStatus))
  200. {
  201. dwStatus = GetLastError();
  202. printf("Failed to StopService. Error %u\n", dwStatus);
  203. break;
  204. }
  205. // query state of service
  206. i = 0;
  207. while((ServiceStatus.dwCurrentState != SERVICE_STOPPED) &&
  208. (i < TIMEOUT_COUNT))
  209. {
  210. if(!QueryServiceStatus(hDriver, &ServiceStatus))
  211. break;
  212. Sleep(1000);
  213. i++;
  214. }
  215. if(ServiceStatus.dwCurrentState != SERVICE_STOPPED)
  216. dwStatus = ERROR_TIMEOUT;
  217. } while(FALSE);
  218. if(NULL != hDriver)
  219. CloseServiceHandle(hDriver);
  220. if(NULL != hSCMan)
  221. CloseServiceHandle(hSCMan);
  222. break;
  223. }
  224. }
  225. return dwStatus;
  226. }
  227. DWORD AtmSmOpenDriver(
  228. OUT HANDLE *phDriver
  229. )
  230. /*++
  231. Routine Description:
  232. Get a handle to the driver
  233. Arguments:
  234. pointer to a handle
  235. Return Value:
  236. NO_ERROR or other
  237. --*/
  238. {
  239. DWORD dwStatus = NO_ERROR;
  240. HANDLE hDriver;
  241. hDriver = CreateFile(
  242. ATM_SAMPLE_CLIENT_DOS_NAME,
  243. GENERIC_READ | GENERIC_WRITE,
  244. FILE_SHARE_READ | FILE_SHARE_WRITE,
  245. NULL,
  246. OPEN_EXISTING,
  247. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  248. NULL);
  249. if(INVALID_HANDLE_VALUE == hDriver)
  250. {
  251. dwStatus = GetLastError();
  252. printf("Failed to open AtmSm Driver. Error %u\n", dwStatus);
  253. }
  254. *phDriver = hDriver;
  255. return dwStatus;
  256. }
  257. VOID AtmSmCloseDriver(
  258. IN HANDLE hDriver
  259. )
  260. /*++
  261. Routine Description:
  262. Close the handle to the driver
  263. Arguments:
  264. Handle to the driver
  265. Return Value:
  266. None
  267. --*/
  268. {
  269. CloseHandle(hDriver);
  270. }
  271. DWORD AtmSmEnumerateAdapters(
  272. IN HANDLE hDriver,
  273. IN OUT PADAPTER_INFO pAdaptInfo,
  274. IN OUT PDWORD pdwSize
  275. )
  276. /*++
  277. Routine Description:
  278. Enumerate the interfaces of the driver.
  279. Note: This call doesn't pend
  280. Arguments:
  281. pointer to a handle
  282. Return Value:
  283. NO_ERROR or other
  284. --*/
  285. {
  286. DWORD dwStatus = NO_ERROR;
  287. DWORD dwReturnBytes;
  288. BOOL bResult;
  289. OVERLAPPED Overlapped;
  290. memset(&Overlapped, 0, sizeof(OVERLAPPED));
  291. do
  292. {
  293. Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  294. if(NULL == Overlapped.hEvent)
  295. {
  296. dwStatus = GetLastError();
  297. break;
  298. }
  299. dwReturnBytes = 0;
  300. // Get the address of adapters
  301. bResult = DeviceIoControl(
  302. hDriver,
  303. (ULONG)IOCTL_ENUMERATE_ADAPTERS,
  304. NULL,
  305. 0,
  306. pAdaptInfo,
  307. *pdwSize,
  308. &dwReturnBytes,
  309. &Overlapped);
  310. if(!bResult)
  311. {
  312. dwStatus = GetLastError();
  313. printf("Failed to enumerate AtmSm Driver. Error %u\n", dwStatus);
  314. // this is a sync operation, it can't pend
  315. assert(ERROR_IO_PENDING != dwStatus);
  316. }
  317. else
  318. *pdwSize = dwReturnBytes;
  319. } while(FALSE);
  320. if(NULL != Overlapped.hEvent)
  321. CloseHandle(Overlapped.hEvent);
  322. return dwStatus;
  323. }
  324. DWORD CloseConnectHandle(
  325. OVERLAPPED *pOverlapped
  326. )
  327. /*++
  328. Routine Description:
  329. Close a handle that was obtained when establishing connection to
  330. destinations.
  331. Arguments:
  332. pOverlapped - An overlapped structure with an Event handle set already.
  333. Return Value:
  334. Status
  335. --*/
  336. {
  337. BOOL bResult;
  338. DWORD dwReturnBytes = 0;
  339. DWORD dwStatus = NO_ERROR;
  340. do
  341. { // break off loop
  342. if(!g_ProgramOptions.hSend)
  343. break;
  344. printf("Closing SendHandle - 0x%p\n", g_ProgramOptions.hSend);
  345. ResetEvent(pOverlapped->hEvent);
  346. // close the connection handle
  347. bResult = DeviceIoControl(
  348. g_ProgramOptions.hDriver,
  349. (ULONG)IOCTL_CLOSE_SEND_HANDLE,
  350. &g_ProgramOptions.hSend,
  351. sizeof(HANDLE),
  352. NULL,
  353. 0,
  354. &dwReturnBytes,
  355. pOverlapped);
  356. if(!bResult)
  357. {
  358. dwStatus = GetLastError();
  359. if(ERROR_IO_PENDING != dwStatus)
  360. {
  361. printf("Failed to disconnect to destinations. Error %u\n",
  362. dwStatus);
  363. break;
  364. }
  365. // the operation is pending
  366. bResult = GetOverlappedResult(
  367. g_ProgramOptions.hDriver,
  368. pOverlapped,
  369. &dwReturnBytes,
  370. TRUE
  371. );
  372. if(!bResult)
  373. {
  374. dwStatus = GetLastError();
  375. printf("Wait for connection to drop, failed. Error - %u\n",
  376. dwStatus);
  377. break;
  378. }
  379. }
  380. } while(FALSE);
  381. return dwStatus;
  382. }
  383. DWORD CloseReceiveHandle(
  384. OVERLAPPED *pOverlapped
  385. )
  386. /*++
  387. Routine Description:
  388. Close a receive handle that was obtained when an adapter was opened for
  389. recvs.
  390. Arguments:
  391. pOverlapped - An overlapped structure with an Event handle set already.
  392. Return Value:
  393. Status
  394. --*/
  395. {
  396. BOOL bResult;
  397. DWORD dwReturnBytes = 0;
  398. DWORD dwStatus = NO_ERROR;
  399. HANDLE hReceive;
  400. do
  401. { // break off loop
  402. if(!g_ProgramOptions.hReceive)
  403. break;
  404. hReceive = g_ProgramOptions.hReceive;
  405. g_ProgramOptions.hReceive = NULL;
  406. printf("Closing ReceiveHandle - 0x%p\n", g_ProgramOptions.hReceive);
  407. ResetEvent(pOverlapped->hEvent);
  408. // close the connection handle
  409. bResult = DeviceIoControl(
  410. g_ProgramOptions.hDriver,
  411. (ULONG)IOCTL_CLOSE_RECV_HANDLE,
  412. &hReceive,
  413. sizeof(HANDLE),
  414. NULL,
  415. 0,
  416. &dwReturnBytes,
  417. pOverlapped);
  418. if(!bResult)
  419. {
  420. dwStatus = GetLastError();
  421. if(ERROR_IO_PENDING != dwStatus)
  422. {
  423. printf("Failed to close the recv handle. Error %u\n", dwStatus);
  424. break;
  425. }
  426. // the operation is pending
  427. bResult = GetOverlappedResult(
  428. g_ProgramOptions.hDriver,
  429. pOverlapped,
  430. &dwReturnBytes,
  431. TRUE
  432. );
  433. if(!bResult)
  434. {
  435. dwStatus = GetLastError();
  436. printf("Wait for closing the recv handle failed. Error - %u\n",
  437. dwStatus);
  438. break;
  439. }
  440. }
  441. } while(FALSE);
  442. return dwStatus;
  443. }
  444. VOID FillPattern(
  445. PUCHAR pBuf,
  446. DWORD dwSize
  447. )
  448. /*++
  449. Routine Description:
  450. Fill a Byte pattern: Every Byte has a value Offset % (0xFF+1)
  451. (Values 0 - FF)
  452. Arguments:
  453. Buffer and size to be filled.
  454. Return Value:
  455. NONE
  456. --*/
  457. {
  458. DWORD dw;
  459. for(dw = 0; dw < dwSize; dw++)
  460. {
  461. pBuf[dw] = (UCHAR)(dw % (MAX_BYTE_VALUE+1));
  462. }
  463. }
  464. BOOL VerifyPattern(
  465. PUCHAR pBuf,
  466. DWORD dwSize
  467. )
  468. /*++
  469. Routine Description:
  470. Verify that the pattern is correct.
  471. Arguments:
  472. Buffer and size to be filled.
  473. Return Value:
  474. NONE
  475. --*/
  476. {
  477. DWORD dw;
  478. UCHAR uch;
  479. for(dw = 0; dw < dwSize; dw++)
  480. {
  481. uch = (UCHAR)(dw % (MAX_BYTE_VALUE+1));
  482. if(uch != (UCHAR)pBuf[dw])
  483. {
  484. printf("Error verifying data. Pattern wrong at offset %u"
  485. " Expected - %.2X Got - %.2X\n",
  486. dw, uch, pBuf[dw]);
  487. return FALSE;
  488. }
  489. }
  490. return TRUE;
  491. }
  492. char * GetErrorString(
  493. DWORD dwError
  494. )
  495. /*++
  496. Routine Description:
  497. Get the formated error string from the system, given an error number.
  498. Arguments:
  499. dwError - Error Number
  500. Return Value:
  501. ErrorString from the system
  502. --*/
  503. {
  504. #define ERR_STR_LEN 512
  505. static char szErrorString[ERR_STR_LEN];
  506. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  507. NULL,
  508. dwError,
  509. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  510. szErrorString,
  511. ERR_STR_LEN,
  512. NULL);
  513. return szErrorString;
  514. #undef ERR_STR_LEN
  515. }