2002-10-10 20:55:24 +02:00
|
|
|
|
/*
|
|
|
|
|
* RPC messages
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2001-2002 Ove K<EFBFBD>ven, TransGaming Technologies
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* TODO:
|
|
|
|
|
* - figure out whether we *really* got this right
|
|
|
|
|
* - check for errors and throw exceptions
|
|
|
|
|
* - decide if OVERLAPPED_WORKS
|
|
|
|
|
*/
|
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
|
#include <stdarg.h>
|
2002-10-10 20:55:24 +02:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
|
#include "winbase.h"
|
|
|
|
|
#include "winerror.h"
|
|
|
|
|
#include "winreg.h"
|
|
|
|
|
|
|
|
|
|
#include "rpc.h"
|
|
|
|
|
#include "rpcdcep.h"
|
|
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
|
|
#include "rpc_binding.h"
|
2003-05-22 05:36:00 +02:00
|
|
|
|
#include "rpc_misc.h"
|
2002-10-10 20:55:24 +02:00
|
|
|
|
#include "rpc_defs.h"
|
|
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ole);
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* I_RpcGetBuffer [RPCRT4.@]
|
|
|
|
|
*/
|
|
|
|
|
RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
|
|
|
|
|
{
|
2003-05-21 20:23:06 +02:00
|
|
|
|
RpcBinding* bind = (RpcBinding*)pMsg->Handle;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
2003-02-19 04:44:35 +01:00
|
|
|
|
TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
|
2002-10-29 00:53:23 +01:00
|
|
|
|
/* FIXME: pfnAllocate? */
|
2003-05-21 20:23:06 +02:00
|
|
|
|
if (bind->server) {
|
|
|
|
|
/* it turns out that the original buffer data must still be available
|
|
|
|
|
* while the RPC server is marshalling a reply, so we should not deallocate
|
|
|
|
|
* it, we'll leave deallocating the original buffer to the RPC server */
|
2004-02-27 05:33:41 +01:00
|
|
|
|
pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
|
2003-05-21 20:23:06 +02:00
|
|
|
|
} else {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
if (pMsg->Buffer)
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
|
|
|
|
|
pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
|
2003-05-21 20:23:06 +02:00
|
|
|
|
}
|
2004-02-27 05:33:41 +01:00
|
|
|
|
TRACE("Buffer=%p\n", pMsg->Buffer);
|
2002-10-10 20:55:24 +02:00
|
|
|
|
/* FIXME: which errors to return? */
|
2004-02-27 05:33:41 +01:00
|
|
|
|
return pMsg->Buffer ? S_OK : E_OUTOFMEMORY;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* I_RpcFreeBuffer [RPCRT4.@]
|
|
|
|
|
*/
|
|
|
|
|
RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
|
|
|
|
|
{
|
2003-05-21 20:23:06 +02:00
|
|
|
|
TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
|
2002-10-29 00:53:23 +01:00
|
|
|
|
/* FIXME: pfnFree? */
|
2002-10-10 20:55:24 +02:00
|
|
|
|
HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
|
|
|
|
|
pMsg->Buffer = NULL;
|
|
|
|
|
return S_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* I_RpcSend [RPCRT4.@]
|
|
|
|
|
*/
|
|
|
|
|
RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
|
|
|
|
|
{
|
|
|
|
|
RpcBinding* bind = (RpcBinding*)pMsg->Handle;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
RpcConnection* conn;
|
2002-10-25 23:02:02 +02:00
|
|
|
|
RPC_CLIENT_INTERFACE* cif = NULL;
|
|
|
|
|
RPC_SERVER_INTERFACE* sif = NULL;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
UUID* obj;
|
|
|
|
|
UUID* act;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
RPC_STATUS status;
|
|
|
|
|
RpcPktHdr hdr;
|
2004-04-19 04:56:12 +02:00
|
|
|
|
DWORD count;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
|
|
|
|
TRACE("(%p)\n", pMsg);
|
|
|
|
|
if (!bind) return RPC_S_INVALID_BINDING;
|
|
|
|
|
|
2003-02-19 04:44:35 +01:00
|
|
|
|
status = RPCRT4_OpenBinding(bind, &conn);
|
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
|
|
|
|
|
obj = &bind->ObjectUuid;
|
|
|
|
|
act = &bind->ActiveUuid;
|
|
|
|
|
|
2002-10-25 23:02:02 +02:00
|
|
|
|
if (bind->server) {
|
|
|
|
|
sif = pMsg->RpcInterfaceInformation;
|
|
|
|
|
if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
|
|
|
|
|
} else {
|
|
|
|
|
cif = pMsg->RpcInterfaceInformation;
|
|
|
|
|
if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
|
|
|
|
/* initialize packet header */
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
|
hdr.rpc_ver = 4;
|
2003-05-22 05:36:00 +02:00
|
|
|
|
hdr.ptype = bind->server
|
|
|
|
|
? ((pMsg->RpcFlags & WINE_RPCFLAG_EXCEPTION) ? PKT_FAULT : PKT_RESPONSE)
|
|
|
|
|
: PKT_REQUEST;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
hdr.object = *obj; /* FIXME: IIRC iff no object, the header structure excludes this elt */
|
2002-10-25 23:02:02 +02:00
|
|
|
|
hdr.if_id = (bind->server) ? sif->InterfaceId.SyntaxGUID : cif->InterfaceId.SyntaxGUID;
|
|
|
|
|
hdr.if_vers =
|
|
|
|
|
(bind->server) ?
|
|
|
|
|
MAKELONG(sif->InterfaceId.SyntaxVersion.MinorVersion, sif->InterfaceId.SyntaxVersion.MajorVersion) :
|
|
|
|
|
MAKELONG(cif->InterfaceId.SyntaxVersion.MinorVersion, cif->InterfaceId.SyntaxVersion.MajorVersion);
|
2003-02-19 04:44:35 +01:00
|
|
|
|
hdr.act_id = *act;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
hdr.opnum = pMsg->ProcNum;
|
2002-10-28 22:14:16 +01:00
|
|
|
|
/* only the low-order 3 octets of the DataRepresentation go in the header */
|
|
|
|
|
hdr.drep[0] = LOBYTE(LOWORD(pMsg->DataRepresentation));
|
|
|
|
|
hdr.drep[1] = HIBYTE(LOWORD(pMsg->DataRepresentation));
|
|
|
|
|
hdr.drep[2] = LOBYTE(HIWORD(pMsg->DataRepresentation));
|
2002-10-10 20:55:24 +02:00
|
|
|
|
hdr.len = pMsg->BufferLength;
|
|
|
|
|
|
|
|
|
|
/* transmit packet */
|
2004-04-19 04:56:12 +02:00
|
|
|
|
if (!WriteFile(conn->conn, &hdr, sizeof(hdr), &count, NULL)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("WriteFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-02-27 05:33:41 +01:00
|
|
|
|
|
|
|
|
|
if (!pMsg->BufferLength)
|
|
|
|
|
{
|
|
|
|
|
status = RPC_S_OK;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-19 04:56:12 +02:00
|
|
|
|
if (!WriteFile(conn->conn, pMsg->Buffer, pMsg->BufferLength, &count, NULL)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("WriteFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
|
|
|
|
/* success */
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (!bind->server) {
|
|
|
|
|
/* save the connection, so the response can be read from it */
|
|
|
|
|
pMsg->ReservedForRuntime = conn;
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
|
}
|
|
|
|
|
RPCRT4_CloseBinding(bind, conn);
|
|
|
|
|
status = RPC_S_OK;
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
return status;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* I_RpcReceive [RPCRT4.@]
|
|
|
|
|
*/
|
|
|
|
|
RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
|
|
|
|
|
{
|
|
|
|
|
RpcBinding* bind = (RpcBinding*)pMsg->Handle;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
RpcConnection* conn;
|
|
|
|
|
UUID* act;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
RPC_STATUS status;
|
|
|
|
|
RpcPktHdr hdr;
|
|
|
|
|
DWORD dwRead;
|
|
|
|
|
|
|
|
|
|
TRACE("(%p)\n", pMsg);
|
|
|
|
|
if (!bind) return RPC_S_INVALID_BINDING;
|
|
|
|
|
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (pMsg->ReservedForRuntime) {
|
|
|
|
|
conn = pMsg->ReservedForRuntime;
|
|
|
|
|
pMsg->ReservedForRuntime = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
status = RPCRT4_OpenBinding(bind, &conn);
|
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
act = &bind->ActiveUuid;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
2003-02-19 04:44:35 +01:00
|
|
|
|
for (;;) {
|
|
|
|
|
/* read packet header */
|
2002-10-10 20:55:24 +02:00
|
|
|
|
#ifdef OVERLAPPED_WORKS
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (!ReadFile(conn->conn, &hdr, sizeof(hdr), &dwRead, &conn->ovl)) {
|
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
|
if (err != ERROR_IO_PENDING) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("ReadFile failed with error %ld\n", err);
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
if (!GetOverlappedResult(conn->conn, &conn->ovl, &dwRead, TRUE)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("ReadFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
}
|
|
|
|
|
#else
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (!ReadFile(conn->conn, &hdr, sizeof(hdr), &dwRead, NULL)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("ReadFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
#endif
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (dwRead != sizeof(hdr)) {
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
2003-02-19 04:44:35 +01:00
|
|
|
|
/* read packet body */
|
|
|
|
|
pMsg->BufferLength = hdr.len;
|
|
|
|
|
status = I_RpcGetBuffer(pMsg);
|
|
|
|
|
if (status != RPC_S_OK) goto fail;
|
|
|
|
|
if (!pMsg->BufferLength) dwRead = 0; else
|
2002-10-10 20:55:24 +02:00
|
|
|
|
#ifdef OVERLAPPED_WORKS
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (!ReadFile(conn->conn, pMsg->Buffer, hdr.len, &dwRead, &conn->ovl)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
if (GetLastError() != ERROR_IO_PENDING) {
|
|
|
|
|
WARN("ReadFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
if (!GetOverlappedResult(conn->conn, &conn->ovl, &dwRead, TRUE)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("ReadFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
}
|
|
|
|
|
#else
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (!ReadFile(conn->conn, pMsg->Buffer, hdr.len, &dwRead, NULL)) {
|
2004-02-27 05:33:41 +01:00
|
|
|
|
WARN("ReadFile failed with error %ld\n", GetLastError());
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
2003-02-19 04:44:35 +01:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
#endif
|
2003-02-19 04:44:35 +01:00
|
|
|
|
if (dwRead != hdr.len) {
|
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2002-10-10 20:55:24 +02:00
|
|
|
|
|
2003-05-22 05:36:00 +02:00
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
|
|
|
|
|
|
switch (hdr.ptype) {
|
|
|
|
|
case PKT_RESPONSE:
|
|
|
|
|
if (bind->server) goto fail;
|
|
|
|
|
break;
|
|
|
|
|
case PKT_REQUEST:
|
|
|
|
|
if (!bind->server) goto fail;
|
|
|
|
|
break;
|
|
|
|
|
case PKT_FAULT:
|
|
|
|
|
pMsg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
|
|
|
|
|
status = RPC_S_CALL_FAILED; /* ? */
|
|
|
|
|
goto fail;
|
|
|
|
|
default:
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-19 04:44:35 +01:00
|
|
|
|
/* success */
|
|
|
|
|
status = RPC_S_OK;
|
|
|
|
|
|
2003-05-22 05:36:00 +02:00
|
|
|
|
/* FIXME: check destination, etc? */
|
2003-02-19 04:44:35 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fail:
|
|
|
|
|
RPCRT4_CloseBinding(bind, conn);
|
|
|
|
|
return status;
|
2002-10-10 20:55:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* I_RpcSendReceive [RPCRT4.@]
|
|
|
|
|
*/
|
|
|
|
|
RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
|
|
|
|
|
{
|
|
|
|
|
RPC_STATUS status;
|
|
|
|
|
|
|
|
|
|
TRACE("(%p)\n", pMsg);
|
|
|
|
|
status = I_RpcSend(pMsg);
|
|
|
|
|
if (status == RPC_S_OK)
|
|
|
|
|
status = I_RpcReceive(pMsg);
|
|
|
|
|
return status;
|
|
|
|
|
}
|