4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * - figure out whether we *really* got this right
23 * - check for errors and throw exceptions
39 #include "wine/debug.h"
41 #include "rpc_binding.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
47 DWORD RPCRT4_GetHeaderSize(RpcPktHdr *Header)
49 static const DWORD header_sizes[] = {
50 sizeof(Header->request), 0, sizeof(Header->response),
51 sizeof(Header->fault), 0, 0, 0, 0, 0, 0, 0, sizeof(Header->bind),
52 sizeof(Header->bind_ack), sizeof(Header->bind_nack),
57 if (Header->common.ptype < sizeof(header_sizes) / sizeof(header_sizes[0])) {
58 ret = header_sizes[Header->common.ptype];
60 FIXME("unhandled packet type\n");
61 if (Header->common.flags & RPC_FLG_OBJECT_UUID)
64 TRACE("invalid packet type\n");
70 VOID RPCRT4_BuildCommonHeader(RpcPktHdr *Header, unsigned char PacketType,
71 unsigned long DataRepresentation)
73 Header->common.rpc_ver = RPC_VER_MAJOR;
74 Header->common.rpc_ver_minor = RPC_VER_MINOR;
75 Header->common.ptype = PacketType;
76 Header->common.drep[0] = LOBYTE(LOWORD(DataRepresentation));
77 Header->common.drep[1] = HIBYTE(LOWORD(DataRepresentation));
78 Header->common.drep[2] = LOBYTE(HIWORD(DataRepresentation));
79 Header->common.drep[3] = HIBYTE(HIWORD(DataRepresentation));
80 Header->common.auth_len = 0;
81 Header->common.call_id = 1;
82 Header->common.flags = 0;
83 /* Flags and fragment length are computed in RPCRT4_Send. */
86 RpcPktHdr *RPCRT4_BuildRequestHeader(unsigned long DataRepresentation,
87 unsigned long BufferLength,
88 unsigned short ProcNum,
95 has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
96 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
97 sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
102 RPCRT4_BuildCommonHeader(header, PKT_REQUEST, DataRepresentation);
103 header->common.frag_len = sizeof(header->request);
104 header->request.alloc_hint = BufferLength;
105 header->request.context_id = 0;
106 header->request.opnum = ProcNum;
108 header->common.flags |= RPC_FLG_OBJECT_UUID;
109 header->common.frag_len += sizeof(UUID);
110 memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
116 RpcPktHdr *RPCRT4_BuildResponseHeader(unsigned long DataRepresentation,
117 unsigned long BufferLength)
121 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->response));
122 if (header == NULL) {
126 RPCRT4_BuildCommonHeader(header, PKT_RESPONSE, DataRepresentation);
127 header->common.frag_len = sizeof(header->response);
128 header->response.alloc_hint = BufferLength;
133 RpcPktHdr *RPCRT4_BuildFaultHeader(unsigned long DataRepresentation,
138 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->fault));
139 if (header == NULL) {
143 RPCRT4_BuildCommonHeader(header, PKT_FAULT, DataRepresentation);
144 header->common.frag_len = sizeof(header->fault);
145 header->fault.status = Status;
150 RpcPktHdr *RPCRT4_BuildBindHeader(unsigned long DataRepresentation,
151 unsigned short MaxTransmissionSize,
152 unsigned short MaxReceiveSize,
153 RPC_SYNTAX_IDENTIFIER *AbstractId,
154 RPC_SYNTAX_IDENTIFIER *TransferId)
158 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind));
159 if (header == NULL) {
163 RPCRT4_BuildCommonHeader(header, PKT_BIND, DataRepresentation);
164 header->common.frag_len = sizeof(header->bind);
165 header->bind.max_tsize = MaxTransmissionSize;
166 header->bind.max_rsize = MaxReceiveSize;
167 header->bind.num_elements = 1;
168 header->bind.num_syntaxes = 1;
169 memcpy(&header->bind.abstract, AbstractId, sizeof(RPC_SYNTAX_IDENTIFIER));
170 memcpy(&header->bind.transfer, TransferId, sizeof(RPC_SYNTAX_IDENTIFIER));
175 RpcPktHdr *RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation,
176 unsigned char RpcVersion,
177 unsigned char RpcVersionMinor)
181 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind_nack));
182 if (header == NULL) {
186 RPCRT4_BuildCommonHeader(header, PKT_BIND_NACK, DataRepresentation);
187 header->common.frag_len = sizeof(header->bind_nack);
188 header->bind_nack.protocols_count = 1;
189 header->bind_nack.protocols[0].rpc_ver = RpcVersion;
190 header->bind_nack.protocols[0].rpc_ver_minor = RpcVersionMinor;
195 RpcPktHdr *RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation,
196 unsigned short MaxTransmissionSize,
197 unsigned short MaxReceiveSize,
199 unsigned long Result,
200 unsigned long Reason,
201 RPC_SYNTAX_IDENTIFIER *TransferId)
204 unsigned long header_size;
205 RpcAddressString *server_address;
207 RPC_SYNTAX_IDENTIFIER *transfer_id;
209 header_size = sizeof(header->bind_ack) + sizeof(RpcResults) +
210 sizeof(RPC_SYNTAX_IDENTIFIER) + sizeof(RpcAddressString) +
211 strlen(ServerAddress);
213 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, header_size);
214 if (header == NULL) {
218 RPCRT4_BuildCommonHeader(header, PKT_BIND_ACK, DataRepresentation);
219 header->common.frag_len = header_size;
220 header->bind_ack.max_tsize = MaxTransmissionSize;
221 header->bind_ack.max_rsize = MaxReceiveSize;
222 server_address = (RpcAddressString*)(&header->bind_ack + 1);
223 server_address->length = strlen(ServerAddress) + 1;
224 strcpy(server_address->string, ServerAddress);
225 results = (RpcResults*)((ULONG_PTR)server_address + sizeof(RpcAddressString) + server_address->length - 1);
226 results->num_results = 1;
227 results->results[0].result = Result;
228 results->results[0].reason = Reason;
229 transfer_id = (RPC_SYNTAX_IDENTIFIER*)(results + 1);
230 memcpy(transfer_id, TransferId, sizeof(RPC_SYNTAX_IDENTIFIER));
235 VOID RPCRT4_FreeHeader(RpcPktHdr *Header)
237 HeapFree(GetProcessHeap(), 0, Header);
240 /***********************************************************************
241 * RPCRT4_Send (internal)
243 * Transmit a packet over connection in acceptable fragments.
245 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
246 void *Buffer, unsigned int BufferLength)
249 DWORD hdr_size, count;
252 /* The packet building functions save the packet header size, so we can use it. */
253 hdr_size = Header->common.frag_len;
254 Header->common.flags |= RPC_FLG_FIRST;
255 Header->common.flags &= ~RPC_FLG_LAST;
256 while (!(Header->common.flags & RPC_FLG_LAST)) {
257 /* decide if we need to split the packet into fragments */
258 if ((BufferLength + hdr_size) <= Connection->MaxTransmissionSize) {
259 Header->common.flags |= RPC_FLG_LAST;
260 Header->common.frag_len = BufferLength + hdr_size;
262 Header->common.frag_len = Connection->MaxTransmissionSize;
263 buffer_pos += Header->common.frag_len - hdr_size;
264 BufferLength -= Header->common.frag_len - hdr_size;
267 /* transmit packet header */
268 if (!WriteFile(Connection->conn, Header, hdr_size, &count, NULL)) {
269 WARN("WriteFile failed with error %ld\n", GetLastError());
270 return GetLastError();
273 /* fragment consisted of header only and is the last one */
274 if (hdr_size == Header->common.frag_len &&
275 Header->common.flags & RPC_FLG_LAST) {
279 /* send the fragment data */
280 if (!WriteFile(Connection->conn, buffer_pos, Header->common.frag_len - hdr_size, &count, NULL)) {
281 WARN("WriteFile failed with error %ld\n", GetLastError());
282 return GetLastError();
285 Header->common.flags &= ~RPC_FLG_FIRST;
291 /***********************************************************************
292 * RPCRT4_Receive (internal)
294 * Receive a packet from connection and merge the fragments.
296 RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
300 DWORD dwRead, hdr_length;
301 unsigned short first_flag;
302 unsigned long data_length;
303 unsigned long buffer_length;
304 unsigned char *buffer_ptr;
305 RpcPktCommonHdr common_hdr;
309 TRACE("(%p, %p, %p)\n", Connection, Header, pMsg);
311 /* read packet common header */
312 if (!ReadFile(Connection->conn, &common_hdr, sizeof(common_hdr), &dwRead, NULL)) {
313 if (GetLastError() != ERROR_MORE_DATA) {
314 WARN("ReadFile failed with error %ld\n", GetLastError());
315 status = RPC_S_PROTOCOL_ERROR;
319 if (dwRead != sizeof(common_hdr)) {
320 WARN("Short read of header, %ld/%d bytes\n", dwRead, sizeof(common_hdr));
321 status = RPC_S_PROTOCOL_ERROR;
325 /* verify if the header really makes sense */
326 if (common_hdr.rpc_ver != RPC_VER_MAJOR ||
327 common_hdr.rpc_ver_minor != RPC_VER_MINOR) {
328 WARN("unhandled packet version\n");
329 status = RPC_S_PROTOCOL_ERROR;
333 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
334 if (hdr_length == 0) {
335 WARN("header length == 0\n");
336 status = RPC_S_PROTOCOL_ERROR;
340 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
341 memcpy(*Header, &common_hdr, sizeof(common_hdr));
343 /* read the rest of packet header */
344 if (!ReadFile(Connection->conn, &(*Header)->common + 1,
345 hdr_length - sizeof(common_hdr), &dwRead, NULL)) {
346 if (GetLastError() != ERROR_MORE_DATA) {
347 WARN("ReadFile failed with error %ld\n", GetLastError());
348 status = RPC_S_PROTOCOL_ERROR;
352 if (dwRead != hdr_length - sizeof(common_hdr)) {
353 WARN("bad header length, %ld/%ld bytes\n", dwRead, hdr_length - sizeof(common_hdr));
354 status = RPC_S_PROTOCOL_ERROR;
358 /* read packet body */
359 switch (common_hdr.ptype) {
361 pMsg->BufferLength = (*Header)->response.alloc_hint;
364 pMsg->BufferLength = (*Header)->request.alloc_hint;
367 pMsg->BufferLength = common_hdr.frag_len - hdr_length;
370 TRACE("buffer length = %u\n", pMsg->BufferLength);
372 status = I_RpcGetBuffer(pMsg);
373 if (status != RPC_S_OK) goto fail;
375 first_flag = RPC_FLG_FIRST;
377 buffer_ptr = pMsg->Buffer;
378 while (buffer_length < pMsg->BufferLength)
380 data_length = (*Header)->common.frag_len - hdr_length;
381 if (((*Header)->common.flags & RPC_FLG_FIRST) != first_flag ||
382 data_length + buffer_length > pMsg->BufferLength) {
383 TRACE("invalid packet flags or buffer length\n");
384 status = RPC_S_PROTOCOL_ERROR;
388 if (data_length == 0) dwRead = 0; else
389 if (!ReadFile(Connection->conn, buffer_ptr, data_length, &dwRead, NULL)) {
390 if (GetLastError() != ERROR_MORE_DATA) {
391 WARN("ReadFile failed with error %ld\n", GetLastError());
392 status = RPC_S_PROTOCOL_ERROR;
396 if (dwRead != data_length) {
397 WARN("bad data length, %ld/%ld\n", dwRead, data_length);
398 status = RPC_S_PROTOCOL_ERROR;
402 /* when there is no more data left, it should be the last packet */
403 if (buffer_length == pMsg->BufferLength &&
404 ((*Header)->common.flags & RPC_FLG_LAST) == 0) {
405 WARN("no more data left, but not last packet\n");
406 status = RPC_S_PROTOCOL_ERROR;
410 buffer_length += data_length;
411 if (buffer_length < pMsg->BufferLength) {
412 TRACE("next header\n");
414 /* read the header of next packet */
415 if (!ReadFile(Connection->conn, *Header, hdr_length, &dwRead, NULL)) {
416 if (GetLastError() != ERROR_MORE_DATA) {
417 WARN("ReadFile failed with error %ld\n", GetLastError());
418 status = GetLastError();
422 if (dwRead != hdr_length) {
423 WARN("invalid packet header size (%ld)\n", dwRead);
424 status = RPC_S_PROTOCOL_ERROR;
428 buffer_ptr += data_length;
437 if (status != RPC_S_OK && *Header) {
438 RPCRT4_FreeHeader(*Header);
444 /***********************************************************************
445 * I_RpcGetBuffer [RPCRT4.@]
447 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
449 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
451 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
452 /* FIXME: pfnAllocate? */
454 /* it turns out that the original buffer data must still be available
455 * while the RPC server is marshalling a reply, so we should not deallocate
456 * it, we'll leave deallocating the original buffer to the RPC server */
457 pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
459 HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
460 pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
462 TRACE("Buffer=%p\n", pMsg->Buffer);
463 /* FIXME: which errors to return? */
464 return pMsg->Buffer ? S_OK : E_OUTOFMEMORY;
467 /***********************************************************************
468 * I_RpcFreeBuffer [RPCRT4.@]
470 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
472 TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
473 /* FIXME: pfnFree? */
474 HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
479 /***********************************************************************
480 * I_RpcSend [RPCRT4.@]
482 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
484 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
486 RPC_CLIENT_INTERFACE* cif = NULL;
487 RPC_SERVER_INTERFACE* sif = NULL;
491 TRACE("(%p)\n", pMsg);
492 if (!bind) return RPC_S_INVALID_BINDING;
495 sif = pMsg->RpcInterfaceInformation;
496 if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
497 status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
500 cif = pMsg->RpcInterfaceInformation;
501 if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
502 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
506 if (status != RPC_S_OK) return status;
509 if (pMsg->RpcFlags & WINE_RPCFLAG_EXCEPTION) {
510 hdr = RPCRT4_BuildFaultHeader(pMsg->DataRepresentation,
513 hdr = RPCRT4_BuildResponseHeader(pMsg->DataRepresentation,
517 hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
518 pMsg->BufferLength, pMsg->ProcNum,
522 status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
524 RPCRT4_FreeHeader(hdr);
528 /* save the connection, so the response can be read from it */
529 pMsg->ReservedForRuntime = conn;
532 RPCRT4_CloseBinding(bind, conn);
538 /***********************************************************************
539 * I_RpcReceive [RPCRT4.@]
541 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
543 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
545 RPC_CLIENT_INTERFACE* cif = NULL;
546 RPC_SERVER_INTERFACE* sif = NULL;
548 RpcPktHdr *hdr = NULL;
550 TRACE("(%p)\n", pMsg);
551 if (!bind) return RPC_S_INVALID_BINDING;
553 if (pMsg->ReservedForRuntime) {
554 conn = pMsg->ReservedForRuntime;
555 pMsg->ReservedForRuntime = NULL;
558 sif = pMsg->RpcInterfaceInformation;
559 if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
560 status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
563 cif = pMsg->RpcInterfaceInformation;
564 if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
565 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
568 if (status != RPC_S_OK) return status;
571 status = RPCRT4_Receive(conn, &hdr, pMsg);
572 if (status != RPC_S_OK) {
573 WARN("receive failed with error %lx\n", status);
577 status = RPC_S_PROTOCOL_ERROR;
579 switch (hdr->common.ptype) {
581 if (bind->server) goto fail;
584 if (!bind->server) goto fail;
587 pMsg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
588 ERR ("we got fault packet with status %lx\n", hdr->fault.status);
589 status = RPC_S_CALL_FAILED; /* ? */
592 WARN("bad packet type %d\n", hdr->common.ptype);
601 RPCRT4_FreeHeader(hdr);
603 RPCRT4_CloseBinding(bind, conn);
607 /***********************************************************************
608 * I_RpcSendReceive [RPCRT4.@]
610 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
614 TRACE("(%p)\n", pMsg);
615 status = I_RpcSend(pMsg);
616 if (status == RPC_S_OK)
617 status = I_RpcReceive(pMsg);