xref: /reactos/dll/win32/rpcrt4/ndr_clientserver.c (revision 321bcc05)
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 "precomp.h"
26 
27 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
28 
29 /************************************************************************
30  *             NdrClientInitializeNew [RPCRT4.@]
31  */
32 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg,
33                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
34 {
35   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
36     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
37 
38   pRpcMessage->Handle = NULL;
39   pRpcMessage->ProcNum = ProcNum | RPC_FLAGS_VALID_BIT;
40   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
41   pRpcMessage->RpcFlags = 0;
42   pRpcMessage->ReservedForRuntime = NULL;
43   pRpcMessage->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
44 
45   pStubMsg->RpcMsg = pRpcMessage;
46   pStubMsg->BufferStart = NULL;
47   pStubMsg->BufferEnd = NULL;
48   pStubMsg->BufferLength = 0;
49   pStubMsg->IsClient = TRUE;
50   pStubMsg->ReuseBuffer = FALSE;
51   pStubMsg->pAllocAllNodesContext = NULL;
52   pStubMsg->pPointerQueueState = NULL;
53   pStubMsg->IgnoreEmbeddedPointers = 0;
54   pStubMsg->PointerBufferMark = NULL;
55   pStubMsg->CorrDespIncrement = 0;
56   pStubMsg->uFlags = 0;
57   pStubMsg->UniquePtrCount = 0;
58   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
59   pStubMsg->pfnFree = pStubDesc->pfnFree;
60   pStubMsg->StackTop = NULL;
61   pStubMsg->StubDesc = pStubDesc;
62   pStubMsg->FullPtrRefId = 0;
63   pStubMsg->PointerLength = 0;
64   pStubMsg->fInDontFree = 0;
65   pStubMsg->fDontCallFreeInst = 0;
66   pStubMsg->fInOnlyParam = 0;
67   pStubMsg->fHasReturn = 0;
68   pStubMsg->fHasExtensions = 0;
69   pStubMsg->fHasNewCorrDesc = 0;
70   pStubMsg->fIsIn = 0;
71   pStubMsg->fIsOut = 0;
72   pStubMsg->fIsOicf = 0;
73   pStubMsg->fBufferValid = 0;
74   pStubMsg->fHasMemoryValidateCallback = 0;
75   pStubMsg->fInFree = 0;
76   pStubMsg->fNeedMCCP = 0;
77   pStubMsg->fUnused = 0;
78   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
79   pStubMsg->pvDestContext = NULL;
80   pStubMsg->pRpcChannelBuffer = NULL;
81   pStubMsg->pArrayInfo = NULL;
82   pStubMsg->dwStubPhase = 0;
83   /* FIXME: LowStackMark */
84   pStubMsg->pAsyncMsg = NULL;
85   pStubMsg->pCorrInfo = NULL;
86   pStubMsg->pCorrMemory = NULL;
87   pStubMsg->pMemoryList = NULL;
88 }
89 
90 /***********************************************************************
91  *             NdrServerInitializeNew [RPCRT4.@]
92  */
93 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
94                                               PMIDL_STUB_DESC pStubDesc )
95 {
96   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
97 
98   pStubMsg->RpcMsg = pRpcMsg;
99   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
100   pStubMsg->BufferEnd = pStubMsg->Buffer + pRpcMsg->BufferLength;
101   pStubMsg->BufferLength = pRpcMsg->BufferLength;
102   pStubMsg->IsClient = FALSE;
103   pStubMsg->ReuseBuffer = FALSE;
104   pStubMsg->pAllocAllNodesContext = NULL;
105   pStubMsg->pPointerQueueState = NULL;
106   pStubMsg->IgnoreEmbeddedPointers = 0;
107   pStubMsg->PointerBufferMark = NULL;
108   pStubMsg->CorrDespIncrement = 0;
109   pStubMsg->uFlags = 0;
110   pStubMsg->UniquePtrCount = 0;
111   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
112   pStubMsg->pfnFree = pStubDesc->pfnFree;
113   pStubMsg->StackTop = NULL;
114   pStubMsg->StubDesc = pStubDesc;
115   pStubMsg->FullPtrXlatTables = NULL;
116   pStubMsg->FullPtrRefId = 0;
117   pStubMsg->PointerLength = 0;
118   pStubMsg->fInDontFree = 0;
119   pStubMsg->fDontCallFreeInst = 0;
120   pStubMsg->fInOnlyParam = 0;
121   pStubMsg->fHasReturn = 0;
122   pStubMsg->fHasExtensions = 0;
123   pStubMsg->fHasNewCorrDesc = 0;
124   pStubMsg->fIsIn = 0;
125   pStubMsg->fIsOut = 0;
126   pStubMsg->fIsOicf = 0;
127   pStubMsg->fHasMemoryValidateCallback = 0;
128   pStubMsg->fInFree = 0;
129   pStubMsg->fNeedMCCP = 0;
130   pStubMsg->fUnused = 0;
131   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
132   pStubMsg->pvDestContext = NULL;
133   pStubMsg->pRpcChannelBuffer = NULL;
134   pStubMsg->pArrayInfo = NULL;
135   pStubMsg->dwStubPhase = 0;
136   /* FIXME: LowStackMark */
137   pStubMsg->pAsyncMsg = NULL;
138   pStubMsg->pCorrInfo = NULL;
139   pStubMsg->pCorrMemory = NULL;
140   pStubMsg->pMemoryList = NULL;
141 
142   return NULL;
143 }
144 
145 /***********************************************************************
146  *           NdrGetBuffer [RPCRT4.@]
147  */
148 unsigned char *WINAPI NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg, ULONG buflen, RPC_BINDING_HANDLE handle)
149 {
150   RPC_STATUS status;
151 
152   TRACE("(stubmsg == ^%p, buflen == %u, handle == %p)\n", stubmsg, buflen, handle);
153 
154   stubmsg->RpcMsg->Handle = handle;
155   stubmsg->RpcMsg->BufferLength = buflen;
156 
157   status = I_RpcGetBuffer(stubmsg->RpcMsg);
158   if (status != RPC_S_OK)
159     RpcRaiseException(status);
160 
161   stubmsg->Buffer = stubmsg->RpcMsg->Buffer;
162   stubmsg->fBufferValid = TRUE;
163   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
164   return stubmsg->Buffer;
165 }
166 /***********************************************************************
167  *           NdrFreeBuffer [RPCRT4.@]
168  */
169 void WINAPI NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)
170 {
171   TRACE("(pStubMsg == ^%p)\n", pStubMsg);
172   if (pStubMsg->fBufferValid)
173   {
174     I_RpcFreeBuffer(pStubMsg->RpcMsg);
175     pStubMsg->fBufferValid = FALSE;
176   }
177 }
178 
179 /************************************************************************
180  *           NdrSendReceive [RPCRT4.@]
181  */
182 unsigned char *WINAPI NdrSendReceive( PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer  )
183 {
184   RPC_STATUS status;
185 
186   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
187 
188   /* FIXME: how to handle errors? (raise exception?) */
189   if (!stubmsg) {
190     ERR("NULL stub message.  No action taken.\n");
191     return NULL;
192   }
193   if (!stubmsg->RpcMsg) {
194     ERR("RPC Message not present in stub message.  No action taken.\n");
195     return NULL;
196   }
197 
198   /* avoid sending uninitialised parts of the buffer on the wire */
199   stubmsg->RpcMsg->BufferLength = buffer - (unsigned char *)stubmsg->RpcMsg->Buffer;
200   status = I_RpcSendReceive(stubmsg->RpcMsg);
201   if (status != RPC_S_OK)
202     RpcRaiseException(status);
203 
204   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
205   stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
206   stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
207   stubmsg->Buffer = stubmsg->BufferStart;
208 
209   /* FIXME: is this the right return value? */
210   return NULL;
211 }
212 
213 /************************************************************************
214  *           NdrMapCommAndFaultStatus [RPCRT4.@]
215  */
216 RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
217                                                ULONG *pCommStatus,
218                                                ULONG *pFaultStatus,
219                                                RPC_STATUS Status )
220 {
221     TRACE("(%p, %p, %p, %d)\n", pStubMsg, pCommStatus, pFaultStatus, Status);
222 
223     switch (Status)
224     {
225     case ERROR_INVALID_HANDLE:
226     case RPC_S_INVALID_BINDING:
227     case RPC_S_UNKNOWN_IF:
228     case RPC_S_SERVER_UNAVAILABLE:
229     case RPC_S_SERVER_TOO_BUSY:
230     case RPC_S_CALL_FAILED_DNE:
231     case RPC_S_PROTOCOL_ERROR:
232     case RPC_S_UNSUPPORTED_TRANS_SYN:
233     case RPC_S_UNSUPPORTED_TYPE:
234     case RPC_S_PROCNUM_OUT_OF_RANGE:
235     case EPT_S_NOT_REGISTERED:
236     case RPC_S_COMM_FAILURE:
237         *pCommStatus = Status;
238         *pFaultStatus = 0;
239         break;
240     default:
241         *pCommStatus = 0;
242         *pFaultStatus = Status;
243     }
244 
245     return RPC_S_OK;
246 }
247