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.

320 lines
6.2 KiB

  1. #ifndef __SSLCONFIGPIPE__HXX_
  2. #define __SSLCONFIGPIPE__HXX_
  3. /*++
  4. Copyright (c) 2001 Microsoft Corporation
  5. Module Name :
  6. sslconfigpipe.cxx
  7. Abstract:
  8. SSL CONFIG PIPE implementation
  9. simple blocking pipe implementation
  10. that enables
  11. - sending/receiving commands,
  12. - sending/receiving response headers
  13. - sending/receiving arbitrary data blocks
  14. - implementing pipe listener that runs on dedicated thread
  15. - safe cleanup for thread running pipe listener
  16. CODEWORK: this class is actually general purpose
  17. It doesn't contain any specifics of ssl config
  18. It should eventually be named more appropriately
  19. and made available for general use
  20. Author:
  21. Jaroslav Dunajsky April-24-2001
  22. Environment:
  23. Win32 - User Mode
  24. Project:
  25. Stream Filter Worker Process
  26. --*/
  27. //
  28. // Command structure to be sent over the named pipe
  29. //
  30. struct SSL_CONFIG_PIPE_COMMAND
  31. {
  32. DWORD dwCommandId;
  33. DWORD dwParameter1;
  34. DWORD dwParameter2;
  35. };
  36. //
  37. // Each response to SSL_CONFIG_PIPE_COMMAND starts with
  38. // SSL_CONFIG_PIPE_RESPONSE_HEADER
  39. // if dwResponseSize is 0, it means that data will be terminated in a
  40. // way specific to sent commmand
  41. //
  42. struct SSL_CONFIG_PIPE_RESPONSE_HEADER
  43. {
  44. DWORD dwCommandId;
  45. DWORD dwParameter1;
  46. DWORD dwParameter2;
  47. HRESULT hrErrorCode;
  48. DWORD dwResponseSize;
  49. };
  50. class SSL_CONFIG_PIPE
  51. {
  52. public:
  53. SSL_CONFIG_PIPE()
  54. :
  55. _hSslConfigPipe( INVALID_HANDLE_VALUE ),
  56. _hPipeListenerThread( NULL ),
  57. _dwCurrThreadId( 0 ),
  58. _dwLockRecursionLevel( 0 ),
  59. _CancelFlag( 0 ),
  60. _InitStatus( INIT_NONE )
  61. {
  62. }
  63. ~SSL_CONFIG_PIPE()
  64. {
  65. }
  66. HRESULT
  67. PipeInitializeServer(
  68. IN const WCHAR * wszPipeName
  69. )
  70. {
  71. return PipeInitialize( wszPipeName,
  72. TRUE /*server*/ );
  73. }
  74. HRESULT
  75. PipeInitializeClient(
  76. IN const WCHAR * wszPipeName
  77. )
  78. {
  79. return PipeInitialize( wszPipeName,
  80. FALSE /*client*/ );
  81. }
  82. HRESULT
  83. PipeTerminate(
  84. VOID
  85. );
  86. HRESULT
  87. PipeConnect(
  88. VOID
  89. );
  90. HRESULT
  91. PipeDisconnect(
  92. VOID
  93. );
  94. VOID
  95. PipeLock(
  96. VOID
  97. );
  98. VOID
  99. PipeUnlock(
  100. VOID
  101. );
  102. BOOL
  103. IsPipeLocked(
  104. VOID
  105. )
  106. {
  107. return _dwCurrThreadId == GetCurrentThreadId();
  108. }
  109. BOOL
  110. IsPipeInitialized(
  111. VOID
  112. )
  113. {
  114. return _InitStatus == INIT_PIPE_CONNECTED;
  115. }
  116. HRESULT
  117. PipeSendData(
  118. IN DWORD cbNumberOfBytesToWrite,
  119. IN BYTE * pbBuffer
  120. );
  121. HRESULT
  122. PipeReceiveData(
  123. IN DWORD cbBytesToRead,
  124. OUT BYTE * pbBuffer
  125. );
  126. HRESULT
  127. PipeSendCommand(
  128. IN SSL_CONFIG_PIPE_COMMAND * pCommand
  129. );
  130. HRESULT
  131. PipeReceiveCommand(
  132. OUT SSL_CONFIG_PIPE_COMMAND * pCommand
  133. );
  134. HRESULT
  135. PipeSendResponseHeader(
  136. IN SSL_CONFIG_PIPE_RESPONSE_HEADER * ResponseHeader
  137. );
  138. HRESULT
  139. PipeReceiveResponseHeader(
  140. OUT SSL_CONFIG_PIPE_RESPONSE_HEADER * ResponseHeader
  141. );
  142. BOOL
  143. QueryPipeIsCancelled(
  144. VOID
  145. )
  146. {
  147. return !!_CancelFlag;
  148. }
  149. VOID
  150. PipeCancel(
  151. VOID
  152. );
  153. protected:
  154. virtual
  155. HRESULT
  156. PipeListener(
  157. VOID
  158. )
  159. /*++
  160. Routine Description:
  161. Inheriting class may implement function that will be executed
  162. on dedicated thread. This class will handle proper synchronization
  163. for cleanup of the thread
  164. Note: when implementing new PipeListener, do not forget to enable it
  165. by implementing QueryEnablePipeListener() that returns TRUE
  166. Arguments:
  167. Return Value:
  168. HRESULT
  169. --*/
  170. {
  171. DBG_ASSERT( FALSE );
  172. return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  173. };
  174. virtual
  175. BOOL
  176. QueryEnablePipeListener(
  177. VOID
  178. )
  179. {
  180. return FALSE;
  181. }
  182. private:
  183. HRESULT
  184. PipeInitialize(
  185. IN const WCHAR * wszPipeName,
  186. IN BOOL fServer
  187. );
  188. HRESULT
  189. PipeConnectServer(
  190. VOID
  191. );
  192. HRESULT
  193. PipeDisconnectServer(
  194. VOID
  195. );
  196. HRESULT
  197. PipeConnectClient(
  198. VOID
  199. );
  200. HRESULT
  201. PipeDisconnectClient(
  202. VOID
  203. );
  204. HRESULT
  205. PipeStartListenerThread(
  206. VOID
  207. );
  208. static
  209. DWORD
  210. PipeListenerThread(
  211. LPVOID ThreadParam
  212. );
  213. HRESULT
  214. PipeWaitForCompletion(
  215. IN HRESULT hrLastError,
  216. IN OVERLAPPED* pOverlapped,
  217. OUT DWORD * pcbTransferred
  218. );
  219. HRESULT
  220. PipeCleanup(
  221. VOID
  222. )
  223. {
  224. if( CancelIo( _hSslConfigPipe ) )
  225. {
  226. return S_OK;
  227. }
  228. return E_FAIL;
  229. }
  230. enum INIT_STATUS
  231. {
  232. INIT_NONE,
  233. INIT_PIPE_LOCK_CREATED,
  234. INIT_OVERLAPPED_R_CREATED,
  235. INIT_OVERLAPPED_W_CREATED,
  236. INIT_CANCEL_EVENT_CREATED,
  237. INIT_SERVER_END_PIPE_CREATED,
  238. INIT_PIPE_CONNECTED
  239. };
  240. INIT_STATUS _InitStatus;
  241. CRITICAL_SECTION _csPipeLock;
  242. DWORD _dwLockRecursionLevel;
  243. DWORD _dwCurrThreadId;
  244. HANDLE _hSslConfigPipe;
  245. // reads and write may be happening concurrenly
  246. // that's why we need 2 overlapped structures
  247. // overlapped for Read
  248. OVERLAPPED _OverlappedR;
  249. // overlapped for Write
  250. OVERLAPPED _OverlappedW;
  251. HANDLE _hPipeListenerThread;
  252. LONG _CancelFlag;
  253. STRU _strPipeName;
  254. // flag indicating Server side pipe
  255. BOOL _fServer;
  256. HANDLE _hCancelEvent;
  257. };
  258. #endif