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