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.

442 lines
12 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1993-2000 Microsoft Corporation
  3. Module Name :
  4. auxilary.cxx
  5. Abstract :
  6. This file contains auxilary routines used for initialization of the
  7. RPC and stub messages and the offline batching of common code sequences
  8. needed by the stubs.
  9. Author :
  10. David Kays dkays September 1993.
  11. Revision History :
  12. ---------------------------------------------------------------------*/
  13. #include "precomp.hxx"
  14. #include "..\..\ndr20\ndrole.h"
  15. #include "asyncndr.h"
  16. #include "auxilary.h"
  17. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  18. Static data for NS library operations
  19. ---------------------------------------------------------------------*/
  20. #pragma code_seg(".ndr64")
  21. void
  22. MakeSureWeHaveNonPipeArgs(
  23. PMIDL_STUB_MESSAGE pStubMsg,
  24. unsigned long BufferSize );
  25. void
  26. EnsureNSLoaded();
  27. void
  28. Ndr64ClientInitialize(
  29. PRPC_MESSAGE pRpcMsg,
  30. PMIDL_STUB_MESSAGE pStubMsg,
  31. PMIDL_STUBLESS_PROXY_INFO pProxyInfo,
  32. unsigned int ProcNum )
  33. /*++
  34. Routine Description :
  35. This routine is called by client side stubs to initialize the RPC message
  36. and stub message, and to get the RPC buffer.
  37. Arguments :
  38. pRpcMsg - pointer to RPC message structure
  39. pStubMsg - pointer to stub message structure
  40. pStubDescriptor - pointer to stub descriptor structure
  41. ProcNum - remote procedure number
  42. --*/
  43. {
  44. //
  45. // Initialize RPC message fields.
  46. //
  47. // The leftmost bit of the procnum field is supposed to be set to 1 inr
  48. // order for the runtime to know if it is talking to the older stubs or
  49. // not.
  50. //
  51. NDR_PROC_CONTEXT * pContext = ( NDR_PROC_CONTEXT *) pStubMsg->pContext ;
  52. memset ( pRpcMsg, 0, sizeof( RPC_MESSAGE ) );
  53. pRpcMsg->RpcInterfaceInformation = pProxyInfo->pStubDesc->
  54. RpcInterfaceInformation;
  55. // default transfer syntax
  56. if ( pProxyInfo->pTransferSyntax )
  57. pRpcMsg->TransferSyntax = pProxyInfo->pTransferSyntax;
  58. else
  59. pRpcMsg->TransferSyntax = (PRPC_SYNTAX_IDENTIFIER)&NDR_TRANSFER_SYNTAX;
  60. //#if !defined(__RPC_WIN64__)
  61. pRpcMsg->ProcNum = ProcNum | RPC_FLAGS_VALID_BIT;
  62. //#endif
  63. //
  64. // Initialize the Stub messsage fields.
  65. //
  66. memset( pStubMsg, 0, sizeof(MIDL_STUB_MESSAGE) );
  67. pStubMsg->RpcMsg = pRpcMsg;
  68. pStubMsg->StubDesc = pProxyInfo->pStubDesc;
  69. pStubMsg->pfnAllocate = pProxyInfo->pStubDesc->pfnAllocate;
  70. pStubMsg->pfnFree = pProxyInfo->pStubDesc->pfnFree;
  71. pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
  72. pStubMsg->pContext = pContext;
  73. pStubMsg->StackTop = pContext->StartofStack;
  74. pStubMsg->IsClient = TRUE;
  75. NdrSetupLowStackMark( pStubMsg );
  76. if ( pProxyInfo->pStubDesc->pMallocFreeStruct )
  77. {
  78. MALLOC_FREE_STRUCT *pMFS = pProxyInfo->pStubDesc->pMallocFreeStruct;
  79. NdrpSetRpcSsDefaults(pMFS->pfnAllocate, pMFS->pfnFree);
  80. }
  81. // This exception should be raised after initializing StubMsg.
  82. if ( pProxyInfo->pStubDesc->Version > NDR_VERSION )
  83. {
  84. NDR_ASSERT( 0, "ClientInitialize : Bad version number" );
  85. RpcRaiseException( RPC_X_WRONG_STUB_VERSION );
  86. }
  87. // This is where we would need to deal with initializing StubMsg fields
  88. // added after NT 5.1 release, if we added them.
  89. }
  90. inline void
  91. Ndr64ServerInitializeCommon(
  92. PRPC_MESSAGE pRpcMsg,
  93. PMIDL_STUB_MESSAGE pStubMsg,
  94. PMIDL_STUB_DESC pStubDescriptor )
  95. /*++
  96. Routine Description :
  97. This routine is called by the server stubs before unmarshalling.
  98. It initializes the stub message fields.
  99. Aruguments :
  100. pStubMsg - pointer to the stub message structure
  101. pStubDescriptor - pointer to the stub descriptor structure
  102. Note :
  103. This is a core server-side initializer, called by everybody,
  104. pipes or not.
  105. --*/
  106. {
  107. NDR_PROC_CONTEXT * pContext = ( NDR_PROC_CONTEXT *) pStubMsg->pContext ;
  108. memset( pStubMsg, 0, sizeof( MIDL_STUB_MESSAGE ) );
  109. pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
  110. //
  111. // Set BufferStart and BufferEnd before unmarshalling.
  112. // Ndr64PointerFree uses these values to detect pointers into the
  113. // rpc message buffer.
  114. //
  115. pStubMsg->BufferStart = (uchar*)pRpcMsg->Buffer;
  116. pStubMsg->BufferEnd = pStubMsg->BufferStart + pRpcMsg->BufferLength;
  117. pStubMsg->pfnAllocate = pStubDescriptor->pfnAllocate;
  118. pStubMsg->pfnFree = pStubDescriptor->pfnFree;
  119. pStubMsg->ReuseBuffer = FALSE;
  120. pStubMsg->StubDesc = pStubDescriptor;
  121. pStubMsg->RpcMsg = pRpcMsg;
  122. pStubMsg->Buffer = (uchar*)pRpcMsg->Buffer;
  123. pStubMsg->pContext = pContext;
  124. pStubMsg->StackTop = pContext->StartofStack;
  125. NdrSetupLowStackMark( pStubMsg );
  126. if ( pStubDescriptor->pMallocFreeStruct )
  127. {
  128. MALLOC_FREE_STRUCT *pMFS = pStubDescriptor->pMallocFreeStruct;
  129. NdrpSetRpcSsDefaults(pMFS->pfnAllocate, pMFS->pfnFree);
  130. }
  131. // This exception should be raised after initializing StubMsg.
  132. NdrRpcSetNDRSlot( pStubMsg );
  133. if ( pStubDescriptor->Version > NDR_VERSION )
  134. {
  135. NDR_ASSERT( 0, "ServerInitialize : bad version number" );
  136. RpcRaiseException( RPC_X_WRONG_STUB_VERSION );
  137. }
  138. // This is where we would need to deal with initializing StubMsg fields
  139. // added after NT 5.1 release, if we added them.
  140. }
  141. void
  142. Ndr64ServerInitializePartial(
  143. PRPC_MESSAGE pRpcMsg,
  144. PMIDL_STUB_MESSAGE pStubMsg,
  145. PMIDL_STUB_DESC pStubDescriptor,
  146. unsigned long RequestedBufferSize )
  147. /*++
  148. Routine Description :
  149. This routine is called by the server stubs for pipes.
  150. It is almost identical to Ndr64ServerInitializeNew, except that
  151. it calls Ndr64pServerInitialize.
  152. Aruguments :
  153. pStubMsg - pointer to the stub message structure
  154. pStubDescriptor - pointer to the stub descriptor structure
  155. pBuffer - pointer to the beginning of the RPC message buffer
  156. --*/
  157. {
  158. Ndr64ServerInitializeCommon( pRpcMsg,
  159. pStubMsg,
  160. pStubDescriptor );
  161. // Last but not least...
  162. MakeSureWeHaveNonPipeArgs( pStubMsg, RequestedBufferSize );
  163. }
  164. unsigned char *
  165. Ndr64ServerInitialize(
  166. PRPC_MESSAGE pRpcMsg,
  167. PMIDL_STUB_MESSAGE pStubMsg,
  168. PMIDL_STUB_DESC pStubDescriptor
  169. )
  170. /*++
  171. Routine Description :
  172. This routine is called by the server stubs before unmarshalling.
  173. It initializes the stub message fields.
  174. Aruguments :
  175. pStubMsg - pointer to the stub message structure
  176. pStubDescriptor - pointer to the stub descriptor structure
  177. Note.
  178. Ndr64ServerInitializeNew is almost identical to Ndr64ServerInitializePartial.
  179. Ndr64ServerInitializeNew is generated for non-pipes and is backward comp.
  180. Ndr64ServerInitializePartial is generated for routines with pipes args.
  181. --*/
  182. {
  183. Ndr64ServerInitializeCommon( pRpcMsg,
  184. pStubMsg,
  185. pStubDescriptor );
  186. if ( !(pRpcMsg->RpcFlags & RPC_BUFFER_COMPLETE ) )
  187. {
  188. // A non-pipe call with an incomplete buffer.
  189. // This can happen only for non-pipe calls in an interface that
  190. // has some pipe calls.
  191. RPC_STATUS Status;
  192. pRpcMsg->RpcFlags = RPC_BUFFER_EXTRA;
  193. // The size argument is ignored, we will get everything.
  194. Status = I_RpcReceive( pRpcMsg, 0 );
  195. if ( Status != RPC_S_OK )
  196. {
  197. // This is the same behavior (and comment) as in MakeSure..
  198. // routine above for non-pipe data case in a pipe call.
  199. // For this particular error case, i.e. a call to Receive to get
  200. // all (non-pipe) data failing, we don't want to restore the
  201. // original dispatch buffer into the rpc message.
  202. // In case of an error the buffer coming back here would be 0.
  203. //
  204. RpcRaiseException( Status );
  205. }
  206. NDR_ASSERT( 0 == pRpcMsg->BufferLength ||
  207. NULL != pRpcMsg->Buffer,
  208. "Rpc runtime returned an invalid buffer.");
  209. // In case this is a new buffer
  210. pStubMsg->Buffer = (uchar*)pRpcMsg->Buffer;
  211. pStubMsg->BufferStart = (uchar*)pRpcMsg->Buffer;
  212. pStubMsg->BufferEnd = pStubMsg->BufferStart + pRpcMsg->BufferLength;
  213. }
  214. return 0;
  215. }
  216. unsigned char *
  217. Ndr64GetBuffer(
  218. PMIDL_STUB_MESSAGE pStubMsg,
  219. unsigned long BufferLength )
  220. /*++
  221. Routine Description :
  222. Performs an RpcGetBuffer.
  223. Arguments :
  224. pStubMsg - Pointer to stub message structure.
  225. BufferLength - Length of requested rpc message buffer.
  226. Handle - Bound handle.
  227. --*/
  228. {
  229. RPC_STATUS Status;
  230. LENGTH_ALIGN( BufferLength, 3 );
  231. pStubMsg->RpcMsg->BufferLength = BufferLength;
  232. Status = I_RpcGetBuffer( pStubMsg->RpcMsg );
  233. if ( Status )
  234. {
  235. // For raw rpc, if async, don't call abort later.
  236. if ( pStubMsg->pAsyncMsg )
  237. pStubMsg->pAsyncMsg->Flags.RuntimeCleanedUp = 1;
  238. RpcRaiseException( Status );
  239. }
  240. NDR_ASSERT( 0 == BufferLength ||
  241. NULL != pStubMsg->RpcMsg->Buffer,
  242. "Rpc runtime returned an invalid buffer.");
  243. NDR_ASSERT( ! ((ULONG_PTR)pStubMsg->RpcMsg->Buffer & 0x7),
  244. "marshaling buffer misaligned" );
  245. pStubMsg->Buffer = (uchar *) pStubMsg->RpcMsg->Buffer;
  246. pStubMsg->fBufferValid = TRUE;
  247. return pStubMsg->Buffer;
  248. }
  249. unsigned char *
  250. Ndr64NsGetBuffer( PMIDL_STUB_MESSAGE pStubMsg,
  251. unsigned long BufferLength )
  252. /*++
  253. Routine Description :
  254. Performs an RpcNsGetBuffer.
  255. Will load the RpcNs4 DLL if not already loaded
  256. Arguments :
  257. pStubMsg - Pointer to stub message structure.
  258. BufferLength - Length of requested rpc message buffer.
  259. Handle - Bound handle
  260. --*/
  261. {
  262. RPC_STATUS Status;
  263. EnsureNSLoaded();
  264. LENGTH_ALIGN( BufferLength, 3 );
  265. pStubMsg->RpcMsg->BufferLength = BufferLength;
  266. Status = (*pRpcNsGetBuffer)( pStubMsg->RpcMsg );
  267. if ( Status )
  268. RpcRaiseException( Status );
  269. NDR_ASSERT( ! ((ULONG_PTR)pStubMsg->RpcMsg->Buffer & 0x7),
  270. "marshaling buffer misaligned" );
  271. pStubMsg->Buffer = (uchar *) pStubMsg->RpcMsg->Buffer;
  272. pStubMsg->fBufferValid = TRUE;
  273. return pStubMsg->Buffer;
  274. }
  275. void
  276. Ndr64pInitUserMarshalCB(
  277. MIDL_STUB_MESSAGE *pStubMsg,
  278. NDR64_USER_MARSHAL_FORMAT * pUserFormat,
  279. USER_MARSHAL_CB_TYPE CBType,
  280. USER_MARSHAL_CB *pUserMarshalCB
  281. )
  282. /*++
  283. Routine Description :
  284. Initialize a user marshall callback structure.
  285. Arguments :
  286. pStubMsg - Supplies the stub message for the call.
  287. pFormat - Supplies the format string for the type(FC64_USER_MARSHAL).
  288. CBType - Supplies the callback type.
  289. pUserMarshalCB - Pointer to the callback to be initialized.
  290. Return :
  291. None.
  292. --*/
  293. {
  294. pUserMarshalCB->Flags = USER_CALL_CTXT_MASK( pStubMsg->dwDestContext );
  295. if ( USER_MARSHAL_CB_UNMARSHALL == CBType )
  296. {
  297. pUserMarshalCB->Flags |=
  298. (((pStubMsg->RpcMsg->DataRepresentation & (ulong)0x0000FFFF)) << 16 );
  299. }
  300. if ( pStubMsg->pAsyncMsg )
  301. pUserMarshalCB->Flags |= USER_CALL_IS_ASYNC;
  302. if ( pStubMsg->fHasNewCorrDesc )
  303. pUserMarshalCB->Flags |= USER_CALL_NEW_CORRELATION_DESC;
  304. pUserMarshalCB->pStubMsg = pStubMsg;
  305. pUserMarshalCB->pReserve = ( pUserFormat->Flags & USER_MARSHAL_IID) ?
  306. (PFORMAT_STRING)pUserFormat + sizeof( NDR64_USER_MARSHAL_FORMAT )
  307. : 0;
  308. pUserMarshalCB->Signature = USER_MARSHAL_CB_SIGNATURE;
  309. pUserMarshalCB->CBType = CBType;
  310. pUserMarshalCB->pFormat = ( PFORMAT_STRING )pUserFormat;
  311. pUserMarshalCB->pTypeFormat = (PFORMAT_STRING)pUserFormat->TransmittedType;
  312. }
  313. #pragma code_seg()