4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006 CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
38 #include "rpc_binding.h"
41 #include "rpc_message.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
45 /* note: the DCE/RPC spec says the alignment amount should be 4, but
46 * MS/RPC servers seem to always use 16 */
47 #define AUTH_ALIGNMENT 16
49 /* gets the amount needed to round a value up to the specified alignment */
50 #define ROUND_UP_AMOUNT(value, alignment) \
51 (((alignment) - (((value) % (alignment)))) % (alignment))
53 static DWORD RPCRT4_GetHeaderSize(RpcPktHdr *Header)
55 static const DWORD header_sizes[] = {
56 sizeof(Header->request), 0, sizeof(Header->response),
57 sizeof(Header->fault), 0, 0, 0, 0, 0, 0, 0, sizeof(Header->bind),
58 sizeof(Header->bind_ack), sizeof(Header->bind_nack),
63 if (Header->common.ptype < sizeof(header_sizes) / sizeof(header_sizes[0])) {
64 ret = header_sizes[Header->common.ptype];
66 FIXME("unhandled packet type\n");
67 if (Header->common.flags & RPC_FLG_OBJECT_UUID)
70 TRACE("invalid packet type\n");
76 static VOID RPCRT4_BuildCommonHeader(RpcPktHdr *Header, unsigned char PacketType,
77 unsigned long DataRepresentation)
79 Header->common.rpc_ver = RPC_VER_MAJOR;
80 Header->common.rpc_ver_minor = RPC_VER_MINOR;
81 Header->common.ptype = PacketType;
82 Header->common.drep[0] = LOBYTE(LOWORD(DataRepresentation));
83 Header->common.drep[1] = HIBYTE(LOWORD(DataRepresentation));
84 Header->common.drep[2] = LOBYTE(HIWORD(DataRepresentation));
85 Header->common.drep[3] = HIBYTE(HIWORD(DataRepresentation));
86 Header->common.auth_len = 0;
87 Header->common.call_id = 1;
88 Header->common.flags = 0;
89 /* Flags and fragment length are computed in RPCRT4_Send. */
92 static RpcPktHdr *RPCRT4_BuildRequestHeader(unsigned long DataRepresentation,
93 unsigned long BufferLength,
94 unsigned short ProcNum,
101 has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
102 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
103 sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
104 if (header == NULL) {
108 RPCRT4_BuildCommonHeader(header, PKT_REQUEST, DataRepresentation);
109 header->common.frag_len = sizeof(header->request);
110 header->request.alloc_hint = BufferLength;
111 header->request.context_id = 0;
112 header->request.opnum = ProcNum;
114 header->common.flags |= RPC_FLG_OBJECT_UUID;
115 header->common.frag_len += sizeof(UUID);
116 memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
122 static RpcPktHdr *RPCRT4_BuildResponseHeader(unsigned long DataRepresentation,
123 unsigned long BufferLength)
127 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->response));
128 if (header == NULL) {
132 RPCRT4_BuildCommonHeader(header, PKT_RESPONSE, DataRepresentation);
133 header->common.frag_len = sizeof(header->response);
134 header->response.alloc_hint = BufferLength;
139 RpcPktHdr *RPCRT4_BuildFaultHeader(unsigned long DataRepresentation,
144 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->fault));
145 if (header == NULL) {
149 RPCRT4_BuildCommonHeader(header, PKT_FAULT, DataRepresentation);
150 header->common.frag_len = sizeof(header->fault);
151 header->fault.status = Status;
156 RpcPktHdr *RPCRT4_BuildBindHeader(unsigned long DataRepresentation,
157 unsigned short MaxTransmissionSize,
158 unsigned short MaxReceiveSize,
159 RPC_SYNTAX_IDENTIFIER *AbstractId,
160 RPC_SYNTAX_IDENTIFIER *TransferId)
164 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind));
165 if (header == NULL) {
169 RPCRT4_BuildCommonHeader(header, PKT_BIND, DataRepresentation);
170 header->common.frag_len = sizeof(header->bind);
171 header->bind.max_tsize = MaxTransmissionSize;
172 header->bind.max_rsize = MaxReceiveSize;
173 header->bind.num_elements = 1;
174 header->bind.num_syntaxes = 1;
175 memcpy(&header->bind.abstract, AbstractId, sizeof(RPC_SYNTAX_IDENTIFIER));
176 memcpy(&header->bind.transfer, TransferId, sizeof(RPC_SYNTAX_IDENTIFIER));
181 RpcPktHdr *RPCRT4_BuildAuthHeader(unsigned long DataRepresentation)
185 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
186 sizeof(header->common) + 12);
190 RPCRT4_BuildCommonHeader(header, PKT_AUTH3, DataRepresentation);
191 header->common.frag_len = 0x14;
192 header->common.auth_len = 0;
197 RpcPktHdr *RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation,
198 unsigned char RpcVersion,
199 unsigned char RpcVersionMinor)
203 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind_nack));
204 if (header == NULL) {
208 RPCRT4_BuildCommonHeader(header, PKT_BIND_NACK, DataRepresentation);
209 header->common.frag_len = sizeof(header->bind_nack);
210 header->bind_nack.protocols_count = 1;
211 header->bind_nack.protocols[0].rpc_ver = RpcVersion;
212 header->bind_nack.protocols[0].rpc_ver_minor = RpcVersionMinor;
217 RpcPktHdr *RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation,
218 unsigned short MaxTransmissionSize,
219 unsigned short MaxReceiveSize,
221 unsigned long Result,
222 unsigned long Reason,
223 RPC_SYNTAX_IDENTIFIER *TransferId)
226 unsigned long header_size;
227 RpcAddressString *server_address;
229 RPC_SYNTAX_IDENTIFIER *transfer_id;
231 header_size = sizeof(header->bind_ack) + sizeof(RpcResults) +
232 sizeof(RPC_SYNTAX_IDENTIFIER) + sizeof(RpcAddressString) +
233 strlen(ServerAddress);
235 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, header_size);
236 if (header == NULL) {
240 RPCRT4_BuildCommonHeader(header, PKT_BIND_ACK, DataRepresentation);
241 header->common.frag_len = header_size;
242 header->bind_ack.max_tsize = MaxTransmissionSize;
243 header->bind_ack.max_rsize = MaxReceiveSize;
244 server_address = (RpcAddressString*)(&header->bind_ack + 1);
245 server_address->length = strlen(ServerAddress) + 1;
246 strcpy(server_address->string, ServerAddress);
247 results = (RpcResults*)((ULONG_PTR)server_address + sizeof(RpcAddressString) + server_address->length - 1);
248 results->num_results = 1;
249 results->results[0].result = Result;
250 results->results[0].reason = Reason;
251 transfer_id = (RPC_SYNTAX_IDENTIFIER*)(results + 1);
252 memcpy(transfer_id, TransferId, sizeof(RPC_SYNTAX_IDENTIFIER));
257 VOID RPCRT4_FreeHeader(RpcPktHdr *Header)
259 HeapFree(GetProcessHeap(), 0, Header);
262 /***********************************************************************
263 * RPCRT4_SendAuth (internal)
265 * Transmit a packet with authorization data over connection in acceptable fragments.
267 static RPC_STATUS RPCRT4_SendAuth(RpcConnection *Connection, RpcPktHdr *Header,
268 void *Buffer, unsigned int BufferLength,
269 void *Auth, unsigned int AuthLength)
275 LONG alen = AuthLength ? (AuthLength + sizeof(RpcAuthVerifier)) : 0;
278 /* The packet building functions save the packet header size, so we can use it. */
279 hdr_size = Header->common.frag_len;
280 Header->common.auth_len = AuthLength;
281 Header->common.flags |= RPC_FLG_FIRST;
282 Header->common.flags &= ~RPC_FLG_LAST;
283 while (!(Header->common.flags & RPC_FLG_LAST)) {
284 unsigned char auth_pad_len = AuthLength ? ROUND_UP_AMOUNT(BufferLength, AUTH_ALIGNMENT) : 0;
285 unsigned int pkt_size = BufferLength + hdr_size + alen + auth_pad_len;
287 /* decide if we need to split the packet into fragments */
288 if (pkt_size <= Connection->MaxTransmissionSize) {
289 Header->common.flags |= RPC_FLG_LAST;
290 Header->common.frag_len = pkt_size;
293 /* make sure packet payload will be a multiple of 16 */
294 Header->common.frag_len =
295 ((Connection->MaxTransmissionSize - hdr_size - alen) & ~(AUTH_ALIGNMENT-1)) +
299 pkt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Header->common.frag_len);
301 memcpy(pkt, Header, hdr_size);
303 /* fragment consisted of header only and is the last one */
304 if (hdr_size == Header->common.frag_len)
307 memcpy(pkt + hdr_size, buffer_pos, Header->common.frag_len - hdr_size - auth_pad_len - alen);
309 /* add the authorization info */
310 if (Connection->AuthInfo && AuthLength)
312 RpcAuthVerifier *auth_hdr = (RpcAuthVerifier *)&pkt[Header->common.frag_len - alen];
314 auth_hdr->auth_type = Connection->AuthInfo->AuthnSvc;
315 auth_hdr->auth_level = Connection->AuthInfo->AuthnLevel;
316 auth_hdr->auth_pad_length = auth_pad_len;
317 auth_hdr->auth_reserved = 0;
318 /* a unique number... */
319 auth_hdr->auth_context_id = (unsigned long)Connection;
321 memcpy(auth_hdr + 1, Auth, AuthLength);
325 count = rpcrt4_conn_write(Connection, pkt, Header->common.frag_len);
326 HeapFree(GetProcessHeap(), 0, pkt);
328 WARN("rpcrt4_conn_write failed (auth)\n");
329 return RPC_S_PROTOCOL_ERROR;
332 buffer_pos += Header->common.frag_len - hdr_size - alen - auth_pad_len;
333 BufferLength -= Header->common.frag_len - hdr_size - alen - auth_pad_len;
334 Header->common.flags &= ~RPC_FLG_FIRST;
340 /***********************************************************************
341 * RPCRT4_AuthNegotiate (internal)
343 static void RPCRT4_AuthNegotiate(RpcConnection *conn, SecBuffer *out)
346 SecBufferDesc out_desc;
347 unsigned char *buffer;
349 buffer = HeapAlloc(GetProcessHeap(), 0, 0x100);
351 out->BufferType = SECBUFFER_TOKEN;
352 out->cbBuffer = 0x100;
353 out->pvBuffer = buffer;
355 out_desc.ulVersion = 0;
356 out_desc.cBuffers = 1;
357 out_desc.pBuffers = out;
360 conn->ctx.dwLower = 0;
361 conn->ctx.dwUpper = 0;
363 r = InitializeSecurityContextA(&conn->AuthInfo->cred, NULL, NULL,
364 ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE | ISC_REQ_MUTUAL_AUTH |
365 ISC_REQ_DELEGATE, 0, SECURITY_NETWORK_DREP,
366 NULL, 0, &conn->ctx, &out_desc, &conn->attr, &conn->exp);
368 TRACE("r = %08lx cbBuffer = %ld attr = %08lx\n", r, out->cbBuffer, conn->attr);
371 /***********************************************************************
372 * RPCRT4_AuthorizeBinding (internal)
374 static RPC_STATUS RPCRT_AuthorizeConnection(RpcConnection* conn,
375 BYTE *challenge, ULONG count)
377 SecBufferDesc inp_desc, out_desc;
380 unsigned char buffer[0x100];
384 TRACE("challenge %s, %ld bytes\n", challenge, count);
386 out.BufferType = SECBUFFER_TOKEN;
387 out.cbBuffer = sizeof buffer;
388 out.pvBuffer = buffer;
390 out_desc.ulVersion = 0;
391 out_desc.cBuffers = 1;
392 out_desc.pBuffers = &out;
394 inp.BufferType = SECBUFFER_TOKEN;
395 inp.pvBuffer = challenge;
396 inp.cbBuffer = count;
398 inp_desc.cBuffers = 1;
399 inp_desc.pBuffers = &inp;
400 inp_desc.ulVersion = 0;
402 r = InitializeSecurityContextA(&conn->AuthInfo->cred, &conn->ctx, NULL,
403 ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE | ISC_REQ_MUTUAL_AUTH |
404 ISC_REQ_DELEGATE, 0, SECURITY_NETWORK_DREP,
405 &inp_desc, 0, &conn->ctx, &out_desc, &conn->attr, &conn->exp);
408 WARN("InitializeSecurityContext failed with error 0x%08lx\n", r);
409 return ERROR_ACCESS_DENIED;
412 resp_hdr = RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION);
414 return E_OUTOFMEMORY;
416 status = RPCRT4_SendAuth(conn, resp_hdr, NULL, 0, out.pvBuffer, out.cbBuffer);
418 RPCRT4_FreeHeader(resp_hdr);
423 /***********************************************************************
424 * RPCRT4_Send (internal)
426 * Transmit a packet over connection in acceptable fragments.
428 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
429 void *Buffer, unsigned int BufferLength)
434 /* if we've already authenticated, just send the context */
435 if (Connection->ctx.dwUpper || Connection->ctx.dwLower)
437 unsigned char buffer[0x10];
439 memset(buffer, 0, sizeof buffer);
440 buffer[0] = 1; /* version number lsb */
442 return RPCRT4_SendAuth(Connection, Header, Buffer, BufferLength, buffer, sizeof buffer);
445 if (!Connection->AuthInfo ||
446 Connection->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT ||
447 Connection->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_NONE)
449 return RPCRT4_SendAuth(Connection, Header, Buffer, BufferLength, NULL, 0);
452 out.BufferType = SECBUFFER_TOKEN;
456 /* tack on a negotiate packet */
457 RPCRT4_AuthNegotiate(Connection, &out);
458 r = RPCRT4_SendAuth(Connection, Header, Buffer, BufferLength, out.pvBuffer, out.cbBuffer);
459 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
464 /***********************************************************************
465 * RPCRT4_Receive (internal)
467 * Receive a packet from connection and merge the fragments.
469 RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
475 unsigned short first_flag;
476 unsigned long data_length;
477 unsigned long buffer_length;
478 unsigned long auth_length;
479 unsigned char *buffer_ptr;
480 unsigned char *auth_data = NULL;
481 RpcPktCommonHdr common_hdr;
485 TRACE("(%p, %p, %p)\n", Connection, Header, pMsg);
487 /* read packet common header */
488 dwRead = rpcrt4_conn_read(Connection, &common_hdr, sizeof(common_hdr));
489 if (dwRead != sizeof(common_hdr)) {
490 WARN("Short read of header, %ld bytes\n", dwRead);
491 status = RPC_S_PROTOCOL_ERROR;
495 /* verify if the header really makes sense */
496 if (common_hdr.rpc_ver != RPC_VER_MAJOR ||
497 common_hdr.rpc_ver_minor != RPC_VER_MINOR) {
498 WARN("unhandled packet version\n");
499 status = RPC_S_PROTOCOL_ERROR;
503 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
504 if (hdr_length == 0) {
505 WARN("header length == 0\n");
506 status = RPC_S_PROTOCOL_ERROR;
510 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
511 memcpy(*Header, &common_hdr, sizeof(common_hdr));
513 /* read the rest of packet header */
514 dwRead = rpcrt4_conn_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
515 if (dwRead != hdr_length - sizeof(common_hdr)) {
516 WARN("bad header length, %ld/%ld bytes\n", dwRead, hdr_length - sizeof(common_hdr));
517 status = RPC_S_PROTOCOL_ERROR;
521 /* read packet body */
522 switch (common_hdr.ptype) {
524 pMsg->BufferLength = (*Header)->response.alloc_hint;
527 pMsg->BufferLength = (*Header)->request.alloc_hint;
530 pMsg->BufferLength = common_hdr.frag_len - hdr_length - RPC_AUTH_VERIFIER_LEN(&common_hdr);
533 TRACE("buffer length = %u\n", pMsg->BufferLength);
535 status = I_RpcGetBuffer(pMsg);
536 if (status != RPC_S_OK) goto fail;
538 first_flag = RPC_FLG_FIRST;
539 auth_length = common_hdr.auth_len;
541 auth_data = HeapAlloc(GetProcessHeap(), 0, RPC_AUTH_VERIFIER_LEN(&common_hdr));
543 status = RPC_S_PROTOCOL_ERROR;
548 buffer_ptr = pMsg->Buffer;
549 while (buffer_length < pMsg->BufferLength)
551 unsigned int header_auth_len = RPC_AUTH_VERIFIER_LEN(&(*Header)->common);
553 /* verify header fields */
555 if (((*Header)->common.frag_len < hdr_length) ||
556 ((*Header)->common.frag_len - hdr_length < header_auth_len)) {
557 WARN("frag_len %d too small for hdr_length %ld and auth_len %d\n",
558 common_hdr.frag_len, hdr_length, common_hdr.auth_len);
559 status = RPC_S_PROTOCOL_ERROR;
563 if ((*Header)->common.auth_len != auth_length) {
564 WARN("auth_len header field changed from %ld to %d\n",
565 auth_length, (*Header)->common.auth_len);
566 status = RPC_S_PROTOCOL_ERROR;
570 data_length = (*Header)->common.frag_len - hdr_length - header_auth_len;
571 if (((*Header)->common.flags & RPC_FLG_FIRST) != first_flag ||
572 data_length + buffer_length > pMsg->BufferLength) {
573 TRACE("invalid packet flags or buffer length\n");
574 status = RPC_S_PROTOCOL_ERROR;
578 if (data_length == 0) dwRead = 0; else
579 dwRead = rpcrt4_conn_read(Connection, buffer_ptr, data_length);
580 if (dwRead != data_length) {
581 WARN("bad data length, %ld/%ld\n", dwRead, data_length);
582 status = RPC_S_PROTOCOL_ERROR;
586 if (header_auth_len) {
587 /* FIXME: we should accumulate authentication data for the bind,
588 * bind_ack, alter_context and alter_context_response if necessary.
589 * however, the details of how this is done is very sketchy in the
590 * DCE/RPC spec. for all other packet types that have authentication
591 * verifier data then it is just duplicated in all the fragments */
592 dwRead = rpcrt4_conn_read(Connection, auth_data, header_auth_len);
593 if (dwRead != header_auth_len) {
594 WARN("bad authentication data length, %ld/%d\n", dwRead,
596 status = RPC_S_PROTOCOL_ERROR;
601 /* when there is no more data left, it should be the last packet */
602 if (buffer_length == pMsg->BufferLength &&
603 ((*Header)->common.flags & RPC_FLG_LAST) == 0) {
604 WARN("no more data left, but not last packet\n");
605 status = RPC_S_PROTOCOL_ERROR;
609 buffer_length += data_length;
610 if (buffer_length < pMsg->BufferLength) {
611 TRACE("next header\n");
613 /* read the header of next packet */
614 dwRead = rpcrt4_conn_read(Connection, *Header, hdr_length);
615 if (dwRead != hdr_length) {
616 WARN("invalid packet header size (%ld)\n", dwRead);
617 status = RPC_S_PROTOCOL_ERROR;
621 buffer_ptr += data_length;
626 /* respond to authorization request */
627 if (common_hdr.ptype == PKT_BIND_ACK && auth_length > sizeof(RpcAuthVerifier))
629 status = RPCRT_AuthorizeConnection(Connection,
630 auth_data + sizeof(RpcAuthVerifier),
640 if (status != RPC_S_OK) {
641 RPCRT4_FreeHeader(*Header);
644 HeapFree(GetProcessHeap(), 0, auth_data);
648 /***********************************************************************
649 * I_RpcGetBuffer [RPCRT4.@]
651 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
653 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
654 /* FIXME: pfnAllocate? */
655 pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
657 TRACE("Buffer=%p\n", pMsg->Buffer);
658 /* FIXME: which errors to return? */
659 return pMsg->Buffer ? S_OK : E_OUTOFMEMORY;
662 /***********************************************************************
663 * I_RpcFreeBuffer [RPCRT4.@]
665 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
667 TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
668 /* FIXME: pfnFree? */
669 HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
674 /***********************************************************************
675 * I_RpcSend [RPCRT4.@]
677 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
679 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
681 RPC_CLIENT_INTERFACE* cif = NULL;
682 RPC_SERVER_INTERFACE* sif = NULL;
686 TRACE("(%p)\n", pMsg);
687 if (!bind) return RPC_S_INVALID_BINDING;
690 sif = pMsg->RpcInterfaceInformation;
691 if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
692 status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
695 cif = pMsg->RpcInterfaceInformation;
696 if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
698 if (!bind->Endpoint || !bind->Endpoint[0])
700 TRACE("automatically resolving partially bound binding\n");
701 status = RpcEpResolveBinding(bind, cif);
702 if (status != RPC_S_OK) return status;
705 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
709 if (status != RPC_S_OK) return status;
712 if (pMsg->RpcFlags & WINE_RPCFLAG_EXCEPTION) {
713 hdr = RPCRT4_BuildFaultHeader(pMsg->DataRepresentation,
716 hdr = RPCRT4_BuildResponseHeader(pMsg->DataRepresentation,
720 hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
721 pMsg->BufferLength, pMsg->ProcNum,
723 hdr->common.call_id = conn->NextCallId++;
726 status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
728 RPCRT4_FreeHeader(hdr);
732 /* save the connection, so the response can be read from it */
733 pMsg->ReservedForRuntime = conn;
736 RPCRT4_CloseBinding(bind, conn);
741 /***********************************************************************
742 * I_RpcReceive [RPCRT4.@]
744 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
746 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
748 RPC_CLIENT_INTERFACE* cif = NULL;
749 RPC_SERVER_INTERFACE* sif = NULL;
751 RpcPktHdr *hdr = NULL;
753 TRACE("(%p)\n", pMsg);
754 if (!bind) return RPC_S_INVALID_BINDING;
756 if (pMsg->ReservedForRuntime) {
757 conn = pMsg->ReservedForRuntime;
758 pMsg->ReservedForRuntime = NULL;
761 sif = pMsg->RpcInterfaceInformation;
762 if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
763 status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
766 cif = pMsg->RpcInterfaceInformation;
767 if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
769 if (!bind->Endpoint || !bind->Endpoint[0])
771 TRACE("automatically resolving partially bound binding\n");
772 status = RpcEpResolveBinding(bind, cif);
773 if (status != RPC_S_OK) return status;
776 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
779 if (status != RPC_S_OK) return status;
782 status = RPCRT4_Receive(conn, &hdr, pMsg);
783 if (status != RPC_S_OK) {
784 WARN("receive failed with error %lx\n", status);
788 status = RPC_S_PROTOCOL_ERROR;
790 switch (hdr->common.ptype) {
792 if (bind->server) goto fail;
795 if (!bind->server) goto fail;
798 pMsg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
799 ERR ("we got fault packet with status 0x%lx\n", hdr->fault.status);
800 status = hdr->fault.status; /* FIXME: do translation from nca error codes */
803 WARN("bad packet type %d\n", hdr->common.ptype);
811 RPCRT4_FreeHeader(hdr);
812 RPCRT4_CloseBinding(bind, conn);
816 /***********************************************************************
817 * I_RpcSendReceive [RPCRT4.@]
819 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
823 TRACE("(%p)\n", pMsg);
824 status = I_RpcSend(pMsg);
825 if (status == RPC_S_OK)
826 status = I_RpcReceive(pMsg);