xref: /reactos/dll/win32/rpcrt4/ndr_clientserver.c (revision b819608e)
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->uFlags = 0;
109   pStubMsg->UniquePtrCount = 0;
110   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
111   pStubMsg->pfnFree = pStubDesc->pfnFree;
112   pStubMsg->StackTop = NULL;
113   pStubMsg->StubDesc = pStubDesc;
114   pStubMsg->FullPtrXlatTables = NULL;
115   pStubMsg->FullPtrRefId = 0;
116   pStubMsg->PointerLength = 0;
117   pStubMsg->fInDontFree = 0;
118   pStubMsg->fDontCallFreeInst = 0;
119   pStubMsg->fInOnlyParam = 0;
120   pStubMsg->fHasReturn = 0;
121   pStubMsg->fHasExtensions = 0;
122   pStubMsg->fHasNewCorrDesc = 0;
123   pStubMsg->fIsIn = 0;
124   pStubMsg->fIsOut = 0;
125   pStubMsg->fIsOicf = 0;
126   pStubMsg->fHasMemoryValidateCallback = 0;
127   pStubMsg->fInFree = 0;
128   pStubMsg->fNeedMCCP = 0;
129   pStubMsg->fUnused = 0;
130   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
131   pStubMsg->pvDestContext = NULL;
132   pStubMsg->pRpcChannelBuffer = NULL;
133   pStubMsg->pArrayInfo = NULL;
134   pStubMsg->dwStubPhase = 0;
135   /* FIXME: LowStackMark */
136   pStubMsg->pAsyncMsg = NULL;
137   pStubMsg->pCorrInfo = NULL;
138   pStubMsg->pCorrMemory = NULL;
139   pStubMsg->pMemoryList = NULL;
140 
141   return NULL;
142 }
143 
144 /***********************************************************************
145  *           NdrGetBuffer [RPCRT4.@]
146  */
147 unsigned char *WINAPI NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg, ULONG buflen, RPC_BINDING_HANDLE handle)
148 {
149   RPC_STATUS status;
150 
151   TRACE("(stubmsg == ^%p, buflen == %u, handle == %p)\n", stubmsg, buflen, handle);
152 
153   stubmsg->RpcMsg->Handle = handle;
154   stubmsg->RpcMsg->BufferLength = buflen;
155 
156   status = I_RpcGetBuffer(stubmsg->RpcMsg);
157   if (status != RPC_S_OK)
158     RpcRaiseException(status);
159 
160   stubmsg->Buffer = stubmsg->RpcMsg->Buffer;
161   stubmsg->fBufferValid = TRUE;
162   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
163   return stubmsg->Buffer;
164 }
165 /***********************************************************************
166  *           NdrFreeBuffer [RPCRT4.@]
167  */
168 void WINAPI NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)
169 {
170   TRACE("(pStubMsg == ^%p)\n", pStubMsg);
171   if (pStubMsg->fBufferValid)
172   {
173     I_RpcFreeBuffer(pStubMsg->RpcMsg);
174     pStubMsg->fBufferValid = FALSE;
175   }
176 }
177 
178 /************************************************************************
179  *           NdrSendReceive [RPCRT4.@]
180  */
181 unsigned char *WINAPI NdrSendReceive( PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer  )
182 {
183   RPC_STATUS status;
184 
185   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
186 
187   /* FIXME: how to handle errors? (raise exception?) */
188   if (!stubmsg) {
189     ERR("NULL stub message.  No action taken.\n");
190     return NULL;
191   }
192   if (!stubmsg->RpcMsg) {
193     ERR("RPC Message not present in stub message.  No action taken.\n");
194     return NULL;
195   }
196 
197   /* avoid sending uninitialised parts of the buffer on the wire */
198   stubmsg->RpcMsg->BufferLength = buffer - (unsigned char *)stubmsg->RpcMsg->Buffer;
199   status = I_RpcSendReceive(stubmsg->RpcMsg);
200   if (status != RPC_S_OK)
201     RpcRaiseException(status);
202 
203   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
204   stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
205   stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
206   stubmsg->Buffer = stubmsg->BufferStart;
207 
208   /* FIXME: is this the right return value? */
209   return NULL;
210 }
211 
212 /************************************************************************
213  *           NdrMapCommAndFaultStatus [RPCRT4.@]
214  */
215 RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
216                                                ULONG *pCommStatus,
217                                                ULONG *pFaultStatus,
218                                                RPC_STATUS Status )
219 {
220     TRACE("(%p, %p, %p, %d)\n", pStubMsg, pCommStatus, pFaultStatus, Status);
221 
222     switch (Status)
223     {
224     case ERROR_INVALID_HANDLE:
225     case RPC_S_INVALID_BINDING:
226     case RPC_S_UNKNOWN_IF:
227     case RPC_S_SERVER_UNAVAILABLE:
228     case RPC_S_SERVER_TOO_BUSY:
229     case RPC_S_CALL_FAILED_DNE:
230     case RPC_S_PROTOCOL_ERROR:
231     case RPC_S_UNSUPPORTED_TRANS_SYN:
232     case RPC_S_UNSUPPORTED_TYPE:
233     case RPC_S_PROCNUM_OUT_OF_RANGE:
234     case EPT_S_NOT_REGISTERED:
235     case RPC_S_COMM_FAILURE:
236         *pCommStatus = Status;
237         *pFaultStatus = 0;
238         break;
239     default:
240         *pCommStatus = 0;
241         *pFaultStatus = Status;
242     }
243 
244     return RPC_S_OK;
245 }
246