ndr_clientserver.c 8.19 KB
Newer Older
1 2 3
/*
 * MIDL proxy/stub stuff
 *
4
 * Copyright 2002 Ove Kåven, TransGaming Technologies
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 21 22 23 24
 *
 * TODO:
 *  - figure out whether we *really* got this right
 *  - check for errors and throw exceptions
 */

25
#include <stdarg.h>
26

27 28
#define COBJMACROS

29 30 31 32
#include "windef.h"
#include "winbase.h"
#include "winerror.h"

33
#include "objbase.h"
34 35 36 37 38 39

#include "rpcproxy.h"

#include "wine/debug.h"

#include "ndr_misc.h"
40
#include "rpcndr.h"
41

42
WINE_DEFAULT_DEBUG_CHANNEL(rpc);
43

44 45 46 47
/************************************************************************
 *             NdrClientInitializeNew [RPCRT4.@]
 */
void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg, 
48
                                    PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
49
{
50 51 52
  TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
    pRpcMessage, pStubMsg, pStubDesc, ProcNum);

53
  pRpcMessage->Handle = NULL;
54
  pRpcMessage->ProcNum = ProcNum | RPC_FLAGS_VALID_BIT;
55 56
  pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
  pRpcMessage->RpcFlags = 0;
57
  pRpcMessage->ReservedForRuntime = NULL;
58
  pRpcMessage->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
59

60 61 62 63
  pStubMsg->RpcMsg = pRpcMessage;
  pStubMsg->BufferStart = NULL;
  pStubMsg->BufferEnd = NULL;
  pStubMsg->BufferLength = 0;
64
  pStubMsg->IsClient = TRUE;
65 66 67 68 69
  pStubMsg->ReuseBuffer = FALSE;
  pStubMsg->pAllocAllNodesContext = NULL;
  pStubMsg->pPointerQueueState = NULL;
  pStubMsg->IgnoreEmbeddedPointers = 0;
  pStubMsg->PointerBufferMark = NULL;
70
  pStubMsg->CorrDespIncrement = 0;
71
  pStubMsg->uFlags = 0;
72
  pStubMsg->UniquePtrCount = 0;
73 74
  pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
  pStubMsg->pfnFree = pStubDesc->pfnFree;
75 76 77 78 79 80 81 82 83 84
  pStubMsg->StackTop = NULL;
  pStubMsg->StubDesc = pStubDesc;
  pStubMsg->FullPtrRefId = 0;
  pStubMsg->PointerLength = 0;
  pStubMsg->fInDontFree = 0;
  pStubMsg->fDontCallFreeInst = 0;
  pStubMsg->fInOnlyParam = 0;
  pStubMsg->fHasReturn = 0;
  pStubMsg->fHasExtensions = 0;
  pStubMsg->fHasNewCorrDesc = 0;
85 86 87 88 89 90 91
  pStubMsg->fIsIn = 0;
  pStubMsg->fIsOut = 0;
  pStubMsg->fIsOicf = 0;
  pStubMsg->fBufferValid = 0;
  pStubMsg->fHasMemoryValidateCallback = 0;
  pStubMsg->fInFree = 0;
  pStubMsg->fNeedMCCP = 0;
92 93 94 95 96 97 98 99 100 101 102
  pStubMsg->fUnused = 0;
  pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
  pStubMsg->pvDestContext = NULL;
  pStubMsg->pRpcChannelBuffer = NULL;
  pStubMsg->pArrayInfo = NULL;
  pStubMsg->dwStubPhase = 0;
  /* FIXME: LowStackMark */
  pStubMsg->pAsyncMsg = NULL;
  pStubMsg->pCorrInfo = NULL;
  pStubMsg->pCorrMemory = NULL;
  pStubMsg->pMemoryList = NULL;
103 104
}

105 106 107 108 109 110
/***********************************************************************
 *             NdrServerInitializeNew [RPCRT4.@]
 */
unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
                                              PMIDL_STUB_DESC pStubDesc )
{
111 112 113
  TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);

  pStubMsg->RpcMsg = pRpcMsg;
114
  pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
115
  pStubMsg->BufferEnd = pStubMsg->Buffer + pRpcMsg->BufferLength;
116
  pStubMsg->BufferLength = pRpcMsg->BufferLength;
117 118 119 120 121 122 123
  pStubMsg->IsClient = FALSE;
  pStubMsg->ReuseBuffer = FALSE;
  pStubMsg->pAllocAllNodesContext = NULL;
  pStubMsg->pPointerQueueState = NULL;
  pStubMsg->IgnoreEmbeddedPointers = 0;
  pStubMsg->PointerBufferMark = NULL;
  pStubMsg->uFlags = 0;
124
  pStubMsg->UniquePtrCount = 0;
125 126 127 128 129 130 131 132 133 134 135 136 137
  pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
  pStubMsg->pfnFree = pStubDesc->pfnFree;
  pStubMsg->StackTop = NULL;
  pStubMsg->StubDesc = pStubDesc;
  pStubMsg->FullPtrXlatTables = NULL;
  pStubMsg->FullPtrRefId = 0;
  pStubMsg->PointerLength = 0;
  pStubMsg->fInDontFree = 0;
  pStubMsg->fDontCallFreeInst = 0;
  pStubMsg->fInOnlyParam = 0;
  pStubMsg->fHasReturn = 0;
  pStubMsg->fHasExtensions = 0;
  pStubMsg->fHasNewCorrDesc = 0;
138 139 140 141 142 143
  pStubMsg->fIsIn = 0;
  pStubMsg->fIsOut = 0;
  pStubMsg->fIsOicf = 0;
  pStubMsg->fHasMemoryValidateCallback = 0;
  pStubMsg->fInFree = 0;
  pStubMsg->fNeedMCCP = 0;
144 145 146 147 148 149 150 151 152 153 154
  pStubMsg->fUnused = 0;
  pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
  pStubMsg->pvDestContext = NULL;
  pStubMsg->pRpcChannelBuffer = NULL;
  pStubMsg->pArrayInfo = NULL;
  pStubMsg->dwStubPhase = 0;
  /* FIXME: LowStackMark */
  pStubMsg->pAsyncMsg = NULL;
  pStubMsg->pCorrInfo = NULL;
  pStubMsg->pCorrMemory = NULL;
  pStubMsg->pMemoryList = NULL;
155

156 157 158
  return NULL;
}

159 160 161
/***********************************************************************
 *           NdrGetBuffer [RPCRT4.@]
 */
162
unsigned char *WINAPI NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg, ULONG buflen, RPC_BINDING_HANDLE handle)
163
{
164
  RPC_STATUS status;
165

166
  TRACE("(stubmsg == ^%p, buflen == %u, handle == %p)\n", stubmsg, buflen, handle);
167
  
168
  stubmsg->RpcMsg->Handle = handle;
169 170
  stubmsg->RpcMsg->BufferLength = buflen;

171 172 173 174 175 176
  status = I_RpcGetBuffer(stubmsg->RpcMsg);
  if (status != RPC_S_OK)
    RpcRaiseException(status);

  stubmsg->Buffer = stubmsg->RpcMsg->Buffer;
  stubmsg->fBufferValid = TRUE;
177
  stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
178
  return stubmsg->Buffer;
179 180 181 182
}
/***********************************************************************
 *           NdrFreeBuffer [RPCRT4.@]
 */
183
void WINAPI NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)
184
{
185 186 187 188 189 190
  TRACE("(pStubMsg == ^%p)\n", pStubMsg);
  if (pStubMsg->fBufferValid)
  {
    I_RpcFreeBuffer(pStubMsg->RpcMsg);
    pStubMsg->fBufferValid = FALSE;
  }
191 192 193 194 195
}

/************************************************************************
 *           NdrSendReceive [RPCRT4.@]
 */
196
unsigned char *WINAPI NdrSendReceive( PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer  )
197
{
198 199
  RPC_STATUS status;

200 201 202 203 204 205 206 207 208 209 210
  TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);

  /* FIXME: how to handle errors? (raise exception?) */
  if (!stubmsg) {
    ERR("NULL stub message.  No action taken.\n");
    return NULL;
  }
  if (!stubmsg->RpcMsg) {
    ERR("RPC Message not present in stub message.  No action taken.\n");
    return NULL;
  }
211

212
  /* avoid sending uninitialised parts of the buffer on the wire */
213
  stubmsg->RpcMsg->BufferLength = buffer - (unsigned char *)stubmsg->RpcMsg->Buffer;
214 215 216
  status = I_RpcSendReceive(stubmsg->RpcMsg);
  if (status != RPC_S_OK)
    RpcRaiseException(status);
217

218 219 220 221 222
  stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
  stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
  stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
  stubmsg->Buffer = stubmsg->BufferStart;

223
  /* FIXME: is this the right return value? */
224
  return NULL;
225
}
226 227 228 229 230

/************************************************************************
 *           NdrMapCommAndFaultStatus [RPCRT4.@]
 */
RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
231 232
                                               ULONG *pCommStatus,
                                               ULONG *pFaultStatus,
233 234
                                               RPC_STATUS Status )
{
235
    TRACE("(%p, %p, %p, %d)\n", pStubMsg, pCommStatus, pFaultStatus, Status);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

    switch (Status)
    {
    case ERROR_INVALID_HANDLE:
    case RPC_S_INVALID_BINDING:
    case RPC_S_UNKNOWN_IF:
    case RPC_S_SERVER_UNAVAILABLE:
    case RPC_S_SERVER_TOO_BUSY:
    case RPC_S_CALL_FAILED_DNE:
    case RPC_S_PROTOCOL_ERROR:
    case RPC_S_UNSUPPORTED_TRANS_SYN:
    case RPC_S_UNSUPPORTED_TYPE:
    case RPC_S_PROCNUM_OUT_OF_RANGE:
    case EPT_S_NOT_REGISTERED:
    case RPC_S_COMM_FAILURE:
        *pCommStatus = Status;
        *pFaultStatus = 0;
        break;
    default:
        *pCommStatus = 0;
        *pFaultStatus = Status;
    }
258 259 260

    return RPC_S_OK;
}