Leaked source code of windows server 2003
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.

303 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. utl.cpp
  5. Abstract:
  6. Misc. Shared and Platform-Indepdent Utilities for the RDP Client
  7. Device Redirector
  8. Author:
  9. Tad Brockway
  10. Revision History:
  11. --*/
  12. #include <precom.h>
  13. #define TRC_FILE "utl"
  14. #include "utl.h"
  15. #include "atrcapi.h"
  16. #include "drdbg.h"
  17. DWORD
  18. GetUserSessionID()
  19. {
  20. DC_BEGIN_FN("GetUserSessionID");
  21. //
  22. // Fetch user TS session ID
  23. //
  24. DWORD tsSessionId = INVALID_SESSIONID;
  25. HMODULE hKernel32 = NULL;
  26. PROCESSIDTOSESSIONID hGetProcAddress;
  27. BOOL bSuccess = FALSE;
  28. hKernel32 = LoadLibrary( TEXT("KERNEL32.DLL") );
  29. if( hKernel32 ) {
  30. #ifndef OS_WINCE
  31. hGetProcAddress = (PROCESSIDTOSESSIONID)GetProcAddress( hKernel32, "ProcessIdToSessionId");
  32. #else
  33. hGetProcAddress = (PROCESSIDTOSESSIONID)GetProcAddress( hKernel32, L"ProcessIdToSessionId");
  34. #endif
  35. if( NULL != hGetProcAddress ) {
  36. bSuccess = (hGetProcAddress)( GetCurrentProcessId(), &tsSessionId );
  37. if (!bSuccess) {
  38. tsSessionId = INVALID_SESSIONID;
  39. }
  40. }
  41. }
  42. if( NULL != hKernel32 ) {
  43. FreeLibrary( hKernel32) ;
  44. }
  45. DC_END_FN();
  46. return tsSessionId;
  47. }
  48. NTSTATUS DrUTL_CheckIOBufInputSize(
  49. IN PRDPDR_IOREQUEST_PACKET pIoReq,
  50. IN ULONG requiredSize
  51. )
  52. /*++
  53. Routine Description:
  54. Confirm that the IOCTL input buffer matches the expected size.
  55. Arguments:
  56. pIoReq - Request packet from server.
  57. requiredSize - Expected size.
  58. Return Value:
  59. None
  60. --*/
  61. {
  62. DC_BEGIN_FN("DrUTL_CheckIOBufInputSize");
  63. NTSTATUS status;
  64. if (pIoReq->IoRequest.Parameters.DeviceIoControl.InputBufferLength <
  65. requiredSize) {
  66. status = STATUS_BUFFER_TOO_SMALL;
  67. ASSERT(FALSE);
  68. }
  69. else {
  70. status = STATUS_SUCCESS;
  71. }
  72. DC_END_FN();
  73. return status;
  74. }
  75. NTSTATUS DrUTL_CheckIOBufOutputSize(
  76. IN PRDPDR_IOREQUEST_PACKET pIoReq,
  77. IN ULONG requiredSize
  78. )
  79. /*++
  80. Routine Description:
  81. Confirm that the IOCTL output buf matches the expected size.
  82. Arguments:
  83. pIoReq - Request packet from server.
  84. requiredSize - Expected size.
  85. Return Value:
  86. None
  87. --*/
  88. {
  89. DC_BEGIN_FN("DrUTL_CheckIOBufOutputSize");
  90. NTSTATUS status;
  91. if (pIoReq->IoRequest.Parameters.DeviceIoControl.OutputBufferLength <
  92. requiredSize) {
  93. status = STATUS_BUFFER_TOO_SMALL;
  94. ASSERT(FALSE);
  95. }
  96. else {
  97. status = STATUS_SUCCESS;
  98. }
  99. DC_END_FN();
  100. return status;
  101. }
  102. NTSTATUS
  103. DrUTL_AllocateReplyBuf(
  104. IN PRDPDR_IOREQUEST_PACKET pIoReq,
  105. OUT PRDPDR_IOCOMPLETION_PACKET *pReplyPacket,
  106. OUT ULONG *replyPacketSize
  107. )
  108. /*++
  109. Routine Description:
  110. Allocate a reply buffer to be returned in response to a server
  111. request.
  112. Arguments:
  113. pIoReq - Request packet from server.
  114. pReplyPacket - Reply packet is returned here.
  115. replyPacketSize - Size of reply packet is returned here.
  116. Return Value:
  117. None
  118. --*/
  119. {
  120. DC_BEGIN_FN("DrUTL_AllocateReplyBuf");
  121. NTSTATUS status = STATUS_SUCCESS;
  122. *replyPacketSize = (ULONG)FIELD_OFFSET(
  123. RDPDR_IOCOMPLETION_PACKET,
  124. IoCompletion.Parameters.DeviceIoControl.OutputBuffer) +
  125. pIoReq->IoRequest.Parameters.DeviceIoControl.OutputBufferLength;
  126. *pReplyPacket = DrUTL_AllocIOCompletePacket(pIoReq, *replyPacketSize) ;
  127. if (*pReplyPacket == NULL) {
  128. status = STATUS_INSUFFICIENT_RESOURCES;
  129. TRC_ERR((TB, _T("Failed to alloc %ld bytes."),*replyPacketSize));
  130. }
  131. else {
  132. status = STATUS_SUCCESS;
  133. }
  134. DC_END_FN();
  135. return status;
  136. }
  137. PRDPDR_IOCOMPLETION_PACKET
  138. DrUTL_AllocIOCompletePacket(
  139. IN const PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  140. IN ULONG sz
  141. )
  142. /*++
  143. Routine Description:
  144. Allocate/release a IO request completion packet for a specified IO
  145. request packet.
  146. Arguments:
  147. pIoRequestPacket - IO request from server.
  148. Return Value:
  149. None
  150. --*/
  151. {
  152. PRDPDR_IOCOMPLETION_PACKET ppIoComp;
  153. PRDPDR_DEVICE_IOREQUEST pIoRequest;
  154. DC_BEGIN_FN("DrUTL_AllocIOCompletePacket");
  155. //
  156. // Get IO request pointer.
  157. //
  158. pIoRequest = &pIoRequestPacket->IoRequest;
  159. ppIoComp = (PRDPDR_IOCOMPLETION_PACKET)new BYTE[ sz ];
  160. if( ppIoComp != NULL ) {
  161. memset(ppIoComp, 0, (size_t)sz);
  162. ppIoComp->IoCompletion.DeviceId = pIoRequest->DeviceId;
  163. ppIoComp->IoCompletion.CompletionId = pIoRequest->CompletionId;
  164. ppIoComp->Header.Component = RDPDR_CTYP_CORE;
  165. ppIoComp->Header.PacketId = DR_CORE_DEVICE_IOCOMPLETION;
  166. }
  167. else {
  168. TRC_ERR((TB, _T("Alloc failed.")));
  169. }
  170. DC_END_FN();
  171. return ppIoComp;
  172. }
  173. VOID DrUTL_FreeIOCompletePacket(
  174. IN PRDPDR_IOCOMPLETION_PACKET packet
  175. )
  176. /*++
  177. Routine Description:
  178. Free an IO complete packet allocated by DrUTL_AllocIOCompletePacket.
  179. Arguments:
  180. packet - Packet to free.
  181. Return Value:
  182. None
  183. --*/
  184. {
  185. DC_BEGIN_FN("DrUTL_FreeIOCompletePacket");
  186. delete []((BYTE *)packet);
  187. DC_END_FN();
  188. }
  189. #ifdef OS_WINCE
  190. ULONG GetActivePortsList(TCHAR *pszPort)
  191. {
  192. DC_BEGIN_FN("GetActivePortsList");
  193. ULONG ulPorts = 0;
  194. TCHAR szActive[] = _T("Drivers\\Active");
  195. HKEY hkActive = NULL;
  196. int nPortLen = _tcslen(pszPort);
  197. if ((ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szActive, 0, 0, &hkActive)) && (hkActive != NULL))
  198. {
  199. TCHAR szName[64];
  200. DWORD dwCount = sizeof(szName)/sizeof(TCHAR);
  201. DWORD dwIndex = 0;
  202. while ((ERROR_SUCCESS == RegEnumKeyEx(hkActive, dwIndex++, szName, &dwCount, NULL, NULL, NULL, NULL)) &&
  203. (dwCount < (sizeof(szName)/sizeof(TCHAR)) ))
  204. {
  205. HKEY hk = NULL;
  206. TCHAR szValue[MAX_PATH];
  207. DWORD dwType = 0;
  208. DWORD dwSize = sizeof(szValue);
  209. if ((ERROR_SUCCESS == RegOpenKeyEx(hkActive, szName, 0, 0, &hk)) &&
  210. (hk != NULL) &&
  211. (ERROR_SUCCESS == RegQueryValueEx(hk, _T("Name"), NULL, &dwType, (LPBYTE )szValue, &dwSize)) &&
  212. (dwType == REG_SZ) && (dwSize < sizeof(szValue)) && (0 == _tcsncmp(pszPort, szValue, nPortLen)) &&
  213. (_tcslen(szValue) == 5) && (szValue[4] == _T(':')) && ((szValue[3] - _T('0')) < 10) )
  214. {
  215. int nPortNum = szValue[3] - _T('0');
  216. TRC_ASSERT( ((ulPorts & (1 << nPortNum)) == 0), (TB, _T("Duplicate port found!")));
  217. ulPorts |= (1 << nPortNum);
  218. }
  219. if (hk)
  220. RegCloseKey(hk);
  221. dwCount = sizeof(szName)/sizeof(TCHAR);
  222. }
  223. RegCloseKey(hkActive);
  224. }
  225. DC_END_FN();
  226. return ulPorts;
  227. }
  228. #endif