xref: /reactos/dll/win32/rpcrt4/ndr_clientserver.c (revision 87a48c83)
1 /*
2  * MIDL proxy/stub stuff
3  *
4  * Copyright 2002 Ove Kåven, TransGaming Technologies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * TODO:
21  *  - figure out whether we *really* got this right
22  *  - check for errors and throw exceptions
23  */
24 
25 #include <stdarg.h>
26 
27 #define COBJMACROS
28 
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 
33 #include "objbase.h"
34 
35 #include "rpcproxy.h"
36 
37 #include "wine/debug.h"
38 
39 #include "ndr_misc.h"
40 #include "rpcndr.h"
41 
42 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
43 
44 /************************************************************************
45  *             NdrClientInitializeNew [RPCRT4.@]
46  */
NdrClientInitializeNew(PRPC_MESSAGE pRpcMessage,PMIDL_STUB_MESSAGE pStubMsg,PMIDL_STUB_DESC pStubDesc,unsigned int ProcNum)47 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg,
48                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
49 {
50   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
51     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
52 
53   pRpcMessage->Handle = NULL;
54   pRpcMessage->ProcNum = ProcNum | RPC_FLAGS_VALID_BIT;
55   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
56   pRpcMessage->RpcFlags = 0;
57   pRpcMessage->ReservedForRuntime = NULL;
58   pRpcMessage->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
59 
60   pStubMsg->RpcMsg = pRpcMessage;
61   pStubMsg->BufferStart = NULL;
62   pStubMsg->BufferEnd = NULL;
63   pStubMsg->BufferLength = 0;
64   pStubMsg->IsClient = TRUE;
65   pStubMsg->ReuseBuffer = FALSE;
66   pStubMsg->pAllocAllNodesContext = NULL;
67   pStubMsg->pPointerQueueState = NULL;
68   pStubMsg->IgnoreEmbeddedPointers = 0;
69   pStubMsg->PointerBufferMark = NULL;
70   pStubMsg->CorrDespIncrement = 0;
71   pStubMsg->uFlags = 0;
72   pStubMsg->UniquePtrCount = 0;
73   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
74   pStubMsg->pfnFree = pStubDesc->pfnFree;
75   pStubMsg->StackTop = NULL;
76   pStubMsg->StubDesc = pStubDesc;
77   pStubMsg->FullPtrRefId = 0;
78   pStubMsg->PointerLength = 0;
79   pStubMsg->fInDontFree = 0;
80   pStubMsg->fDontCallFreeInst = 0;
81   pStubMsg->fInOnlyParam = 0;
82   pStubMsg->fHasReturn = 0;
83   pStubMsg->fHasExtensions = 0;
84   pStubMsg->fHasNewCorrDesc = 0;
85   pStubMsg->fIsIn = 0;
86   pStubMsg->fIsOut = 0;
87   pStubMsg->fIsOicf = 0;
88   pStubMsg->fBufferValid = 0;
89   pStubMsg->fHasMemoryValidateCallback = 0;
90   pStubMsg->fInFree = 0;
91   pStubMsg->fNeedMCCP = 0;
92   pStubMsg->fUnused = 0;
93   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
94   pStubMsg->pvDestContext = NULL;
95   pStubMsg->pRpcChannelBuffer = NULL;
96   pStubMsg->pArrayInfo = NULL;
97   pStubMsg->dwStubPhase = 0;
98   /* FIXME: LowStackMark */
99   pStubMsg->pAsyncMsg = NULL;
100   pStubMsg->pCorrInfo = NULL;
101   pStubMsg->pCorrMemory = NULL;
102   pStubMsg->pMemoryList = NULL;
103 }
104 
105 /***********************************************************************
106  *             NdrServerInitializeNew [RPCRT4.@]
107  */
NdrServerInitializeNew(PRPC_MESSAGE pRpcMsg,PMIDL_STUB_MESSAGE pStubMsg,PMIDL_STUB_DESC pStubDesc)108 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
109                                               PMIDL_STUB_DESC pStubDesc )
110 {
111   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
112 
113   pStubMsg->RpcMsg = pRpcMsg;
114   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
115   pStubMsg->BufferEnd = pStubMsg->Buffer + pRpcMsg->BufferLength;
116   pStubMsg->BufferLength = pRpcMsg->BufferLength;
117   pStubMsg->IsClient = FALSE;
118   pStubMsg->ReuseBuffer = FALSE;
119   pStubMsg->pAllocAllNodesContext = NULL;
120   pStubMsg->pPointerQueueState = NULL;
121   pStubMsg->IgnoreEmbeddedPointers = 0;
122   pStubMsg->PointerBufferMark = NULL;
123   pStubMsg->CorrDespIncrement = 0;
124   pStubMsg->uFlags = 0;
125   pStubMsg->UniquePtrCount = 0;
126   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
127   pStubMsg->pfnFree = pStubDesc->pfnFree;
128   pStubMsg->StackTop = NULL;
129   pStubMsg->StubDesc = pStubDesc;
130   pStubMsg->FullPtrXlatTables = NULL;
131   pStubMsg->FullPtrRefId = 0;
132   pStubMsg->PointerLength = 0;
133   pStubMsg->fInDontFree = 0;
134   pStubMsg->fDontCallFreeInst = 0;
135   pStubMsg->fInOnlyParam = 0;
136   pStubMsg->fHasReturn = 0;
137   pStubMsg->fHasExtensions = 0;
138   pStubMsg->fHasNewCorrDesc = 0;
139   pStubMsg->fIsIn = 0;
140   pStubMsg->fIsOut = 0;
141   pStubMsg->fIsOicf = 0;
142   pStubMsg->fHasMemoryValidateCallback = 0;
143   pStubMsg->fInFree = 0;
144   pStubMsg->fNeedMCCP = 0;
145   pStubMsg->fUnused = 0;
146   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
147   pStubMsg->pvDestContext = NULL;
148   pStubMsg->pRpcChannelBuffer = NULL;
149   pStubMsg->pArrayInfo = NULL;
150   pStubMsg->dwStubPhase = 0;
151   /* FIXME: LowStackMark */
152   pStubMsg->pAsyncMsg = NULL;
153   pStubMsg->pCorrInfo = NULL;
154   pStubMsg->pCorrMemory = NULL;
155   pStubMsg->pMemoryList = NULL;
156 
157   return NULL;
158 }
159 
160 /***********************************************************************
161  *           NdrGetBuffer [RPCRT4.@]
162  */
NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg,ULONG buflen,RPC_BINDING_HANDLE handle)163 unsigned char *WINAPI NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg, ULONG buflen, RPC_BINDING_HANDLE handle)
164 {
165   RPC_STATUS status;
166 
167   TRACE("(stubmsg == ^%p, buflen == %u, handle == %p)\n", stubmsg, buflen, handle);
168 
169   stubmsg->RpcMsg->Handle = handle;
170   stubmsg->RpcMsg->BufferLength = buflen;
171 
172   status = I_RpcGetBuffer(stubmsg->RpcMsg);
173   if (status != RPC_S_OK)
174     RpcRaiseException(status);
175 
176   stubmsg->Buffer = stubmsg->RpcMsg->Buffer;
177   stubmsg->fBufferValid = TRUE;
178   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
179   return stubmsg->Buffer;
180 }
181 /***********************************************************************
182  *           NdrFreeBuffer [RPCRT4.@]
183  */
NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)184 void WINAPI NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)
185 {
186   TRACE("(pStubMsg == ^%p)\n", pStubMsg);
187   if (pStubMsg->fBufferValid)
188   {
189     I_RpcFreeBuffer(pStubMsg->RpcMsg);
190     pStubMsg->fBufferValid = FALSE;
191   }
192 }
193 
194 /************************************************************************
195  *           NdrSendReceive [RPCRT4.@]
196  */
NdrSendReceive(PMIDL_STUB_MESSAGE stubmsg,unsigned char * buffer)197 unsigned char *WINAPI NdrSendReceive( PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer  )
198 {
199   RPC_STATUS status;
200 
201   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
202 
203   /* FIXME: how to handle errors? (raise exception?) */
204   if (!stubmsg) {
205     ERR("NULL stub message.  No action taken.\n");
206     return NULL;
207   }
208   if (!stubmsg->RpcMsg) {
209     ERR("RPC Message not present in stub message.  No action taken.\n");
210     return NULL;
211   }
212 
213   /* avoid sending uninitialised parts of the buffer on the wire */
214   stubmsg->RpcMsg->BufferLength = buffer - (unsigned char *)stubmsg->RpcMsg->Buffer;
215   status = I_RpcSendReceive(stubmsg->RpcMsg);
216   if (status != RPC_S_OK)
217     RpcRaiseException(status);
218 
219   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
220   stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
221   stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
222   stubmsg->Buffer = stubmsg->BufferStart;
223 
224   /* FIXME: is this the right return value? */
225   return NULL;
226 }
227 
228 /************************************************************************
229  *           NdrMapCommAndFaultStatus [RPCRT4.@]
230  */
NdrMapCommAndFaultStatus(PMIDL_STUB_MESSAGE pStubMsg,ULONG * pCommStatus,ULONG * pFaultStatus,RPC_STATUS Status)231 RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
232                                                ULONG *pCommStatus,
233                                                ULONG *pFaultStatus,
234                                                RPC_STATUS Status )
235 {
236     TRACE("(%p, %p, %p, %d)\n", pStubMsg, pCommStatus, pFaultStatus, Status);
237 
238     switch (Status)
239     {
240     case ERROR_INVALID_HANDLE:
241     case RPC_S_INVALID_BINDING:
242     case RPC_S_UNKNOWN_IF:
243     case RPC_S_SERVER_UNAVAILABLE:
244     case RPC_S_SERVER_TOO_BUSY:
245     case RPC_S_CALL_FAILED_DNE:
246     case RPC_S_PROTOCOL_ERROR:
247     case RPC_S_UNSUPPORTED_TRANS_SYN:
248     case RPC_S_UNSUPPORTED_TYPE:
249     case RPC_S_PROCNUM_OUT_OF_RANGE:
250     case EPT_S_NOT_REGISTERED:
251     case RPC_S_COMM_FAILURE:
252         *pCommStatus = Status;
253         *pFaultStatus = 0;
254         break;
255     default:
256         *pCommStatus = 0;
257         *pFaultStatus = Status;
258     }
259 
260     return RPC_S_OK;
261 }
262