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.

402 lines
9.9 KiB

  1. /*************************************************************************
  2. * ICAAPI.C
  3. *
  4. * ICA DLL Interface for ICA Device Driver
  5. *
  6. * Copyright 1996, Citrix Systems Inc.
  7. * Copyright (C) 1997-1999 Microsoft Corp.
  8. *
  9. * Author: Marc Bloomfield
  10. * Terry Treder
  11. * Brad Pedersen
  12. *************************************************************************/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. /*=============================================================================
  16. == External procedures defined
  17. =============================================================================*/
  18. #ifdef BUILD_AS_DLL
  19. BOOL WINAPI DllEntryPoint( HINSTANCE, DWORD, LPVOID );
  20. #endif
  21. NTSTATUS IcaOpen( HANDLE * phIca );
  22. NTSTATUS IcaClose( HANDLE hIca );
  23. VOID cdecl IcaSystemTrace( IN HANDLE hIca, ULONG, ULONG, char *, ... );
  24. VOID cdecl IcaTrace( IN HANDLE hIca, ULONG, ULONG, char *, ... );
  25. NTSTATUS IcaIoControl( HANDLE hIca, ULONG, PVOID, ULONG, PVOID, ULONG, PULONG );
  26. /*=============================================================================
  27. == Internal procedures defined
  28. =============================================================================*/
  29. NTSTATUS _IcaOpen( PHANDLE hIca, PVOID, ULONG );
  30. /*=============================================================================
  31. == Procedures used
  32. =============================================================================*/
  33. #ifdef BUILD_AS_DLL
  34. /****************************************************************************
  35. *
  36. * DllEntryPoint
  37. *
  38. * Function is called when the DLL is loaded and unloaded.
  39. *
  40. * ENTRY:
  41. * hinstDLL (input)
  42. * Handle of DLL module
  43. * fdwReason (input)
  44. * Why function was called
  45. * lpvReserved (input)
  46. * Reserved; must be NULL
  47. *
  48. * EXIT:
  49. * TRUE - Success
  50. * FALSE - Error occurred
  51. *
  52. ****************************************************************************/
  53. BOOL WINAPI
  54. DllEntryPoint( HINSTANCE hinstDLL,
  55. DWORD fdwReason,
  56. LPVOID lpvReserved )
  57. {
  58. switch ( fdwReason ) {
  59. case DLL_PROCESS_ATTACH:
  60. DisableThreadLibraryCalls(hinstDLL);
  61. break;
  62. default:
  63. break;
  64. }
  65. return( TRUE );
  66. }
  67. #endif
  68. /****************************************************************************
  69. *
  70. * IcaOpen
  71. *
  72. * Open an instance to the ICA Device Driver
  73. *
  74. * ENTRY:
  75. * phIca (output)
  76. * Pointer to ICA instance handle
  77. *
  78. * EXIT:
  79. * STATUS_SUCCESS - Success
  80. * other - Error return code
  81. *
  82. ****************************************************************************/
  83. NTSTATUS
  84. IcaOpen( OUT HANDLE * phIca )
  85. {
  86. NTSTATUS Status;
  87. Status = _IcaOpen( phIca, NULL, 0 );
  88. if ( !NT_SUCCESS(Status) )
  89. goto badopen;
  90. TRACE(( *phIca, TC_ICAAPI, TT_API1, "TSAPI: IcaOpen, success\n" ));
  91. return( STATUS_SUCCESS );
  92. /*=============================================================================
  93. == Error returns
  94. =============================================================================*/
  95. badopen:
  96. *phIca = NULL;
  97. return( Status );
  98. }
  99. /****************************************************************************
  100. *
  101. * IcaClose
  102. *
  103. * Close an instance to the ICA Device Driver
  104. *
  105. * ENTRY:
  106. * hIca (input)
  107. * ICA instance handle
  108. *
  109. * EXIT:
  110. * STATUS_SUCCESS - Success
  111. * other - Error return code
  112. *
  113. ****************************************************************************/
  114. NTSTATUS
  115. IcaClose( IN HANDLE hIca )
  116. {
  117. NTSTATUS Status;
  118. TRACE(( hIca, TC_ICAAPI, TT_API1, "TSAPI: IcaClose\n" ));
  119. /*
  120. * Close the ICA device driver instance
  121. */
  122. Status = NtClose( hIca );
  123. ASSERT( NT_SUCCESS(Status) );
  124. return( Status );
  125. }
  126. /*******************************************************************************
  127. *
  128. * IcaSystemTrace
  129. *
  130. * Write a trace record to the system trace file
  131. *
  132. * ENTRY:
  133. * hIca (input)
  134. * ICA instance handle
  135. * TraceClass (input)
  136. * trace class bit mask
  137. * TraceEnable (input)
  138. * trace type bit mask
  139. * Format (input)
  140. * format string
  141. * ... (input)
  142. * enough arguments to satisfy format string
  143. *
  144. * EXIT:
  145. * nothing
  146. *
  147. ******************************************************************************/
  148. VOID cdecl
  149. IcaSystemTrace( IN HANDLE hIca,
  150. IN ULONG TraceClass,
  151. IN ULONG TraceEnable,
  152. IN char * Format,
  153. IN ... )
  154. {
  155. ICA_TRACE_BUFFER Buffer;
  156. va_list arg_marker;
  157. ULONG Length;
  158. va_start( arg_marker, Format );
  159. Length = (ULONG) _vsnprintf( Buffer.Data, sizeof(Buffer.Data), Format, arg_marker );
  160. Buffer.TraceClass = TraceClass;
  161. Buffer.TraceEnable = TraceEnable;
  162. Buffer.DataLength = Length;
  163. (void) IcaIoControl( hIca,
  164. IOCTL_ICA_SYSTEM_TRACE,
  165. &Buffer,
  166. sizeof(Buffer) - sizeof(Buffer.Data) + Length,
  167. NULL,
  168. 0,
  169. NULL );
  170. }
  171. /*******************************************************************************
  172. *
  173. * IcaTrace
  174. *
  175. * Write a trace record to the winstation trace file
  176. *
  177. * ENTRY:
  178. * hIca (input)
  179. * ICA instance handle
  180. * TraceClass (input)
  181. * trace class bit mask
  182. * TraceEnable (input)
  183. * trace type bit mask
  184. * Format (input)
  185. * format string
  186. * ... (input)
  187. * enough arguments to satisfy format string
  188. *
  189. * EXIT:
  190. * nothing
  191. *
  192. ******************************************************************************/
  193. VOID cdecl
  194. IcaTrace( IN HANDLE hIca,
  195. IN ULONG TraceClass,
  196. IN ULONG TraceEnable,
  197. IN char * Format,
  198. IN ... )
  199. {
  200. ICA_TRACE_BUFFER Buffer;
  201. va_list arg_marker;
  202. ULONG Length;
  203. va_start( arg_marker, Format );
  204. Length = (ULONG) _vsnprintf( Buffer.Data, sizeof(Buffer.Data), Format, arg_marker );
  205. Buffer.TraceClass = TraceClass;
  206. Buffer.TraceEnable = TraceEnable;
  207. Buffer.DataLength = Length;
  208. (void) IcaIoControl( hIca,
  209. IOCTL_ICA_TRACE,
  210. &Buffer,
  211. sizeof(Buffer) - sizeof(Buffer.Data) + Length,
  212. NULL,
  213. 0,
  214. NULL );
  215. }
  216. /****************************************************************************
  217. *
  218. * IcaIoControl
  219. *
  220. * Generic interface to the ICA Device Driver
  221. *
  222. * ENTRY:
  223. * hIca (input)
  224. * ICA instance handle
  225. *
  226. * IoControlCode (input)
  227. * I/O control code
  228. *
  229. * pInBuffer (input)
  230. * Pointer to input parameters
  231. *
  232. * InBufferSize (input)
  233. * Size of pInBuffer
  234. *
  235. * pOutBuffer (output)
  236. * Pointer to output buffer
  237. *
  238. * OutBufferSize (input)
  239. * Size of pOutBuffer
  240. *
  241. * pBytesReturned (output)
  242. * Pointer to number of bytes returned
  243. *
  244. * EXIT:
  245. * STATUS_SUCCESS - Success
  246. * other - Error return code
  247. *
  248. ****************************************************************************/
  249. NTSTATUS
  250. IcaIoControl( IN HANDLE hIca,
  251. IN ULONG IoControlCode,
  252. IN PVOID pInBuffer,
  253. IN ULONG InBufferSize,
  254. OUT PVOID pOutBuffer,
  255. IN ULONG OutBufferSize,
  256. OUT PULONG pBytesReturned )
  257. {
  258. IO_STATUS_BLOCK Iosb;
  259. NTSTATUS Status;
  260. /*
  261. * Issue ioctl
  262. */
  263. Status = NtDeviceIoControlFile( hIca,
  264. NULL,
  265. NULL,
  266. NULL,
  267. &Iosb,
  268. IoControlCode,
  269. pInBuffer,
  270. InBufferSize,
  271. pOutBuffer,
  272. OutBufferSize );
  273. /*
  274. * Wait for ioctl to complete
  275. */
  276. if ( Status == STATUS_PENDING ) {
  277. Status = NtWaitForSingleObject( hIca, FALSE, NULL );
  278. if ( NT_SUCCESS(Status))
  279. Status = Iosb.Status;
  280. }
  281. /*
  282. * Convert warning into error
  283. */
  284. if ( Status == STATUS_BUFFER_OVERFLOW )
  285. Status = STATUS_BUFFER_TOO_SMALL;
  286. /*
  287. * Initialize bytes returned
  288. */
  289. if ( pBytesReturned )
  290. *pBytesReturned = (ULONG)Iosb.Information;
  291. return( Status );
  292. }
  293. /****************************************************************************
  294. *
  295. * _IcaOpen
  296. *
  297. * Open an instance to the ICA Device Driver or an ICA stack
  298. *
  299. * ENTRY:
  300. * ph (output)
  301. * Pointer to ICA or ICA stack instance handle
  302. *
  303. * pEa (input)
  304. * Pointer to extended attribute buffer
  305. *
  306. * cbEa (input)
  307. * Size of extended attribute buffer
  308. *
  309. * EXIT:
  310. * STATUS_SUCCESS - Success
  311. * other - Error return code
  312. *
  313. ****************************************************************************/
  314. NTSTATUS
  315. _IcaOpen( PHANDLE ph,
  316. PVOID pEa,
  317. ULONG cbEa )
  318. {
  319. NTSTATUS Status;
  320. OBJECT_ATTRIBUTES objectAttributes;
  321. UNICODE_STRING IcaName;
  322. IO_STATUS_BLOCK ioStatusBlock;
  323. /*
  324. * Initialize the object attributes
  325. */
  326. RtlInitUnicodeString( &IcaName, ICA_DEVICE_NAME );
  327. InitializeObjectAttributes( &objectAttributes,
  328. &IcaName,
  329. OBJ_CASE_INSENSITIVE,
  330. NULL,
  331. NULL );
  332. /*
  333. * Open an instance to the ICA device driver
  334. */
  335. Status = NtCreateFile( ph,
  336. GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
  337. &objectAttributes,
  338. &ioStatusBlock,
  339. NULL, // AllocationSize
  340. 0L, // FileAttributes
  341. FILE_SHARE_READ | FILE_SHARE_WRITE, // ShareAccess
  342. FILE_OPEN_IF, // CreateDisposition
  343. 0,
  344. pEa,
  345. cbEa );
  346. return( Status );
  347. }