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.

485 lines
12 KiB

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // open.cpp
  7. //
  8. // Abstract:
  9. // This module contains functions associated with enumerating,
  10. // opening, and closing the tdi device objects
  11. //
  12. //////////////////////////////////////////////////////////////////////////
  13. #include "stdafx.h"
  14. //////////////////////////////////////////////////////////////////////////
  15. // private prototype
  16. //////////////////////////////////////////////////////////////////////////
  17. VOID
  18. StringToUcntString(
  19. PUCNTSTRING pusDestination,
  20. TCHAR *sSource
  21. );
  22. //////////////////////////////////////////////////////////////////////////
  23. // Public functions
  24. //////////////////////////////////////////////////////////////////////////
  25. // --------------------------------------------------------------------
  26. //
  27. // Function: DoGetNumDevices
  28. //
  29. // Arguments: ulAddressType -- address type to scan list for
  30. //
  31. // Returns: number of devices found
  32. //
  33. // Descript: This function gets the number of openable devices
  34. // of this address type registered with tdisample.sys
  35. //
  36. //---------------------------------------------------------------------
  37. ULONG
  38. DoGetNumDevices(ULONG ulAddressType)
  39. {
  40. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  41. SEND_BUFFER SendBuffer; // arguments for command
  42. //
  43. // set up arguments
  44. //
  45. SendBuffer.COMMAND_ARGS.GetDevArgs.ulAddressType = ulAddressType;
  46. //
  47. // call driver to execute command, and deal with results
  48. //
  49. if (TdiLibDeviceIO(ulGETNUMDEVICES,
  50. &SendBuffer,
  51. &ReceiveBuffer) == STATUS_SUCCESS)
  52. {
  53. return ReceiveBuffer.RESULTS.ulReturnValue;
  54. }
  55. else
  56. {
  57. return 0;
  58. }
  59. }
  60. // --------------------------------------------------------------------
  61. //
  62. // Function: DoGetDeviceName
  63. //
  64. // Arguments: addresstype -- address type to get
  65. // slotnum -- which device to get of that type
  66. // pName -- buffer large enough to hold name
  67. // (supplied by caller)
  68. //
  69. // Returns: status of command
  70. //
  71. // Descript: This function gets the n'th device from the list of devices
  72. // of this address type registered with tdisample.sys
  73. //
  74. //---------------------------------------------------------------------
  75. NTSTATUS
  76. DoGetDeviceName(ULONG ulAddressType,
  77. ULONG ulSlotNum,
  78. TCHAR *pName) // buffer from caller!!
  79. {
  80. NTSTATUS lStatus; // status of command
  81. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  82. SEND_BUFFER SendBuffer; // arguments for command
  83. //
  84. // set up arguments
  85. //
  86. SendBuffer.COMMAND_ARGS.GetDevArgs.ulAddressType = ulAddressType;
  87. SendBuffer.COMMAND_ARGS.GetDevArgs.ulSlotNum = ulSlotNum;
  88. //
  89. // call the driver
  90. //
  91. lStatus = TdiLibDeviceIO(ulGETDEVICE,
  92. &SendBuffer,
  93. &ReceiveBuffer);
  94. //
  95. // deal with results
  96. //
  97. if (lStatus == STATUS_SUCCESS)
  98. {
  99. WCHAR *pSourceTemp = ReceiveBuffer.RESULTS.ucsStringReturn.wcBuffer;
  100. for(;;)
  101. {
  102. *pName = (TCHAR)*pSourceTemp++;
  103. if (*pName == 0)
  104. {
  105. break;
  106. }
  107. pName++;
  108. }
  109. }
  110. return lStatus;
  111. }
  112. // --------------------------------------------------------------------
  113. //
  114. // Function: DoGetAddress
  115. //
  116. // Arguments: addresstype -- address type to get
  117. // slotnum -- which device to get
  118. // pTransAddr -- transport address (allocated by calleer,
  119. // filled by this function)
  120. //
  121. // Returns: status of command
  122. // if successful, pTransAddr is filled
  123. //
  124. // Descript: This function gets the address of the n'th device from the
  125. // list of devices registered with tdisample.sys
  126. //
  127. //---------------------------------------------------------------------
  128. NTSTATUS
  129. DoGetAddress(ULONG ulAddressType,
  130. ULONG ulSlotNum,
  131. PTRANSPORT_ADDRESS pTransAddr)
  132. {
  133. NTSTATUS lStatus; // status of command
  134. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  135. SEND_BUFFER SendBuffer; // arguments for command
  136. //
  137. // set up arguments
  138. //
  139. SendBuffer.COMMAND_ARGS.GetDevArgs.ulAddressType = ulAddressType;
  140. SendBuffer.COMMAND_ARGS.GetDevArgs.ulSlotNum = ulSlotNum;
  141. //
  142. // call the driver
  143. //
  144. lStatus = TdiLibDeviceIO(ulGETADDRESS,
  145. &SendBuffer,
  146. &ReceiveBuffer);
  147. //
  148. // deal with the results
  149. //
  150. if (lStatus == STATUS_SUCCESS)
  151. {
  152. PTRANSPORT_ADDRESS pTransportAddress
  153. = (PTRANSPORT_ADDRESS)&ReceiveBuffer.RESULTS.TransAddr;
  154. ULONG ulLength
  155. = FIELD_OFFSET(TRANSPORT_ADDRESS, Address)
  156. + FIELD_OFFSET(TA_ADDRESS, Address)
  157. + pTransportAddress->Address[0].AddressLength;
  158. memcpy(pTransAddr,
  159. pTransportAddress,
  160. ulLength);
  161. }
  162. return lStatus;
  163. }
  164. // ------------------------------------------
  165. //
  166. // Function: DoOpenControl
  167. //
  168. // Arguments: strDeviceName -- device name to open
  169. //
  170. // Returns: TdiHandle (ULONG) if successful; 0 if failure
  171. //
  172. // Descript: calls the driver to open control channel
  173. //
  174. // ------------------------------------------
  175. TDIHANDLE
  176. DoOpenControl(TCHAR *strDeviceName)
  177. {
  178. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  179. SEND_BUFFER SendBuffer; // arguments for command
  180. //
  181. // set up the arguments
  182. //
  183. StringToUcntString(&SendBuffer.COMMAND_ARGS.OpenArgs.ucsDeviceName,
  184. strDeviceName);
  185. //
  186. // call the driver
  187. //
  188. if (TdiLibDeviceIO(ulOPENCONTROL,
  189. &SendBuffer,
  190. &ReceiveBuffer) == STATUS_SUCCESS)
  191. {
  192. return ReceiveBuffer.RESULTS.TdiHandle;
  193. }
  194. else
  195. {
  196. return NULL;
  197. }
  198. }
  199. //-------------------------------------------------------------
  200. //
  201. // Function: DoCloseControl
  202. //
  203. // Argument: ulTdiHandle -- handle for control channel
  204. //
  205. // Returns: none
  206. //
  207. // Descript: This function closes the indicated control channel
  208. //
  209. //-------------------------------------------------------------
  210. VOID
  211. DoCloseControl(ULONG ulTdiHandle)
  212. {
  213. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  214. SEND_BUFFER SendBuffer; // arguments for command
  215. //
  216. // set up the arguments
  217. //
  218. SendBuffer.TdiHandle = ulTdiHandle;
  219. //
  220. // call the driver
  221. //
  222. NTSTATUS lStatus = TdiLibDeviceIO(ulCLOSECONTROL,
  223. &SendBuffer,
  224. &ReceiveBuffer);
  225. if (lStatus != STATUS_SUCCESS)
  226. {
  227. _tprintf(TEXT("DoCloseControl: failure, status = %s\n"), TdiLibStatusMessage(lStatus));
  228. }
  229. }
  230. // ------------------------------------------
  231. //
  232. // Function: DoOpenAddress
  233. //
  234. // Arguments: strDeviceName -- device name to open
  235. // pTransportAddress -- address to open
  236. // pulTdiHandle -- returned handle if successful
  237. //
  238. // Returns: status of command
  239. //
  240. // Descript: calls the driver to open address object
  241. //
  242. // ------------------------------------------
  243. TDIHANDLE
  244. DoOpenAddress(TCHAR *strDeviceName,
  245. PTRANSPORT_ADDRESS pTransportAddress)
  246. {
  247. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  248. SEND_BUFFER SendBuffer; // arguments for command
  249. //
  250. // set up arguments
  251. //
  252. StringToUcntString(&SendBuffer.COMMAND_ARGS.OpenArgs.ucsDeviceName,
  253. strDeviceName);
  254. memcpy(&SendBuffer.COMMAND_ARGS.OpenArgs.TransAddr,
  255. pTransportAddress,
  256. (FIELD_OFFSET(TRANSPORT_ADDRESS, Address)
  257. + FIELD_OFFSET(TA_ADDRESS, Address)
  258. + pTransportAddress->Address[0].AddressLength));
  259. //
  260. // call the driver
  261. //
  262. if (TdiLibDeviceIO(ulOPENADDRESS,
  263. &SendBuffer,
  264. &ReceiveBuffer) == STATUS_SUCCESS)
  265. {
  266. return ReceiveBuffer.RESULTS.TdiHandle;
  267. }
  268. else
  269. {
  270. return NULL;
  271. }
  272. }
  273. //-------------------------------------------------------------
  274. //
  275. // Function: DoCloseAddress
  276. //
  277. // Argument: ulTdiHandle -- handle for address object
  278. //
  279. // Returns: None
  280. //
  281. // Descript: This function closes the indicated address object
  282. //
  283. //-------------------------------------------------------------
  284. VOID
  285. DoCloseAddress(ULONG ulTdiHandle)
  286. {
  287. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  288. SEND_BUFFER SendBuffer; // arguments for command
  289. //
  290. // set up arguments
  291. //
  292. SendBuffer.TdiHandle = ulTdiHandle;
  293. //
  294. // call the driver
  295. //
  296. NTSTATUS lStatus = TdiLibDeviceIO(ulCLOSEADDRESS,
  297. &SendBuffer,
  298. &ReceiveBuffer);
  299. if (lStatus != STATUS_SUCCESS)
  300. {
  301. _tprintf(TEXT("DoCloseAddress: failure, status = %s\n"), TdiLibStatusMessage(lStatus));
  302. }
  303. }
  304. // ------------------------------------------
  305. //
  306. // Function: DoOpenEndpoint
  307. //
  308. // Arguments: strDeviceName -- device name to open
  309. // pTransportAddress -- address to open
  310. // pulTdiHandle -- returned handled (if successful)
  311. //
  312. // Returns: status of command
  313. //
  314. // Descript: calls the driver to open endpoint object
  315. //
  316. // ------------------------------------------
  317. TDIHANDLE
  318. DoOpenEndpoint(TCHAR *strDeviceName,
  319. PTRANSPORT_ADDRESS pTransportAddress)
  320. {
  321. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  322. SEND_BUFFER SendBuffer; // arguments for command
  323. //
  324. // set up the arguments
  325. //
  326. StringToUcntString(&SendBuffer.COMMAND_ARGS.OpenArgs.ucsDeviceName,
  327. strDeviceName);
  328. memcpy(&SendBuffer.COMMAND_ARGS.OpenArgs.TransAddr,
  329. pTransportAddress,
  330. (FIELD_OFFSET(TRANSPORT_ADDRESS, Address)
  331. + FIELD_OFFSET(TA_ADDRESS, Address)
  332. + pTransportAddress->Address[0].AddressLength));
  333. //
  334. // call the driver
  335. //
  336. if (TdiLibDeviceIO(ulOPENENDPOINT,
  337. &SendBuffer,
  338. &ReceiveBuffer) == STATUS_SUCCESS)
  339. {
  340. return ReceiveBuffer.RESULTS.TdiHandle;
  341. }
  342. else
  343. {
  344. return NULL;
  345. }
  346. }
  347. //-------------------------------------------------------------
  348. //
  349. // Function: DoCloseEndpoint
  350. //
  351. // Argument: pTdiHandle -- handle for endpoint object
  352. //
  353. // Returns: none
  354. //
  355. // Descript: This function closes the indicated endpoint object
  356. //
  357. //-------------------------------------------------------------
  358. VOID
  359. DoCloseEndpoint(ULONG ulTdiHandle)
  360. {
  361. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  362. SEND_BUFFER SendBuffer; // arguments for command
  363. //
  364. // set up arguments
  365. //
  366. SendBuffer.TdiHandle = ulTdiHandle;
  367. //
  368. // call the driver
  369. //
  370. NTSTATUS lStatus = TdiLibDeviceIO(ulCLOSEENDPOINT,
  371. &SendBuffer,
  372. &ReceiveBuffer);
  373. if (lStatus != STATUS_SUCCESS)
  374. {
  375. _tprintf(TEXT("DoCloseEndpoint: failure, status = %s\n"), TdiLibStatusMessage(lStatus));
  376. }
  377. }
  378. ///////////////////////////////////////////
  379. // private functions
  380. ///////////////////////////////////////////
  381. // -------------------------------
  382. //
  383. // Function: StringToUcntString
  384. //
  385. // Arguments: pusDestination -- counted wide string
  386. // pcSource -- asci string
  387. //
  388. // Returns: none
  389. //
  390. // Descript: copies ansi (ascii) string to counted wide string
  391. //
  392. // -------------------------------
  393. VOID
  394. StringToUcntString(PUCNTSTRING pusDestination,
  395. TCHAR *Source)
  396. {
  397. PWCHAR pwcString // ptr to data of wide string
  398. = pusDestination->wcBuffer;
  399. ULONG ulLength = _tcslen(Source);
  400. for(ULONG ulCount = 0; ulCount < ulLength; ulCount++)
  401. {
  402. *pwcString++ = Source[ulCount];
  403. }
  404. *pwcString = 0;
  405. pusDestination->usLength = (USHORT)(ulLength * 2);
  406. }
  407. ////////////////////////////////////////////////////////////////////
  408. // end of file open.cpp
  409. ////////////////////////////////////////////////////////////////////