set svn:eol-style to native
[reactos.git] / reactos / lib / rpcrt4 / rpc_message.c
1 /*
2 * RPC messages
3 *
4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 *
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.
11 *
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.
16 *
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
20 *
21 * TODO:
22 * - figure out whether we *really* got this right
23 * - check for errors and throw exceptions
24 */
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winreg.h"
34
35 #include "rpc.h"
36 #include "rpcndr.h"
37 #include "rpcdcep.h"
38
39 #include "wine/debug.h"
40
41 #include "rpc_binding.h"
42 #include "rpc_misc.h"
43 #include "rpc_defs.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
46
47 DWORD RPCRT4_GetHeaderSize(RpcPktHdr *Header)
48 {
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),
53 0, 0, 0, 0, 0
54 };
55 ULONG ret = 0;
56
57 if (Header->common.ptype < sizeof(header_sizes) / sizeof(header_sizes[0])) {
58 ret = header_sizes[Header->common.ptype];
59 if (ret == 0)
60 FIXME("unhandled packet type\n");
61 if (Header->common.flags & RPC_FLG_OBJECT_UUID)
62 ret += sizeof(UUID);
63 } else {
64 TRACE("invalid packet type\n");
65 }
66
67 return ret;
68 }
69
70 VOID RPCRT4_BuildCommonHeader(RpcPktHdr *Header, unsigned char PacketType,
71 unsigned long DataRepresentation)
72 {
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. */
84 }
85
86 RpcPktHdr *RPCRT4_BuildRequestHeader(unsigned long DataRepresentation,
87 unsigned long BufferLength,
88 unsigned short ProcNum,
89 UUID *ObjectUuid)
90 {
91 RpcPktHdr *header;
92 BOOL has_object;
93 RPC_STATUS status;
94
95 has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
96 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
97 sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
98 if (header == NULL) {
99 return NULL;
100 }
101
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;
107 if (has_object) {
108 header->common.flags |= RPC_FLG_OBJECT_UUID;
109 header->common.frag_len += sizeof(UUID);
110 memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
111 }
112
113 return header;
114 }
115
116 RpcPktHdr *RPCRT4_BuildResponseHeader(unsigned long DataRepresentation,
117 unsigned long BufferLength)
118 {
119 RpcPktHdr *header;
120
121 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->response));
122 if (header == NULL) {
123 return NULL;
124 }
125
126 RPCRT4_BuildCommonHeader(header, PKT_RESPONSE, DataRepresentation);
127 header->common.frag_len = sizeof(header->response);
128 header->response.alloc_hint = BufferLength;
129
130 return header;
131 }
132
133 RpcPktHdr *RPCRT4_BuildFaultHeader(unsigned long DataRepresentation,
134 RPC_STATUS Status)
135 {
136 RpcPktHdr *header;
137
138 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->fault));
139 if (header == NULL) {
140 return NULL;
141 }
142
143 RPCRT4_BuildCommonHeader(header, PKT_FAULT, DataRepresentation);
144 header->common.frag_len = sizeof(header->fault);
145 header->fault.status = Status;
146
147 return header;
148 }
149
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)
155 {
156 RpcPktHdr *header;
157
158 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind));
159 if (header == NULL) {
160 return NULL;
161 }
162
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));
171
172 return header;
173 }
174
175 RpcPktHdr *RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation,
176 unsigned char RpcVersion,
177 unsigned char RpcVersionMinor)
178 {
179 RpcPktHdr *header;
180
181 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind_nack));
182 if (header == NULL) {
183 return NULL;
184 }
185
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;
191
192 return header;
193 }
194
195 RpcPktHdr *RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation,
196 unsigned short MaxTransmissionSize,
197 unsigned short MaxReceiveSize,
198 LPSTR ServerAddress,
199 unsigned long Result,
200 unsigned long Reason,
201 RPC_SYNTAX_IDENTIFIER *TransferId)
202 {
203 RpcPktHdr *header;
204 unsigned long header_size;
205 RpcAddressString *server_address;
206 RpcResults *results;
207 RPC_SYNTAX_IDENTIFIER *transfer_id;
208
209 header_size = sizeof(header->bind_ack) + sizeof(RpcResults) +
210 sizeof(RPC_SYNTAX_IDENTIFIER) + sizeof(RpcAddressString) +
211 strlen(ServerAddress);
212
213 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, header_size);
214 if (header == NULL) {
215 return NULL;
216 }
217
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));
231
232 return header;
233 }
234
235 VOID RPCRT4_FreeHeader(RpcPktHdr *Header)
236 {
237 HeapFree(GetProcessHeap(), 0, Header);
238 }
239
240 /***********************************************************************
241 * RPCRT4_Send (internal)
242 *
243 * Transmit a packet over connection in acceptable fragments.
244 */
245 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
246 void *Buffer, unsigned int BufferLength)
247 {
248 PUCHAR buffer_pos;
249 DWORD hdr_size, count;
250
251 buffer_pos = Buffer;
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;
261 } else {
262 Header->common.frag_len = Connection->MaxTransmissionSize;
263 buffer_pos += Header->common.frag_len - hdr_size;
264 BufferLength -= Header->common.frag_len - hdr_size;
265 }
266
267 /* transmit packet header */
268 if (!WriteFile(Connection->conn, Header, hdr_size, &count, &Connection->ovl[1]) &&
269 ERROR_IO_PENDING != GetLastError()) {
270 WARN("WriteFile failed with error %ld\n", GetLastError());
271 return GetLastError();
272 }
273 if (!GetOverlappedResult(Connection->conn, &Connection->ovl[1], &count, TRUE)) {
274 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
275 return GetLastError();
276 }
277
278 /* fragment consisted of header only and is the last one */
279 if (hdr_size == Header->common.frag_len &&
280 Header->common.flags & RPC_FLG_LAST) {
281 return RPC_S_OK;
282 }
283
284 /* send the fragment data */
285 if (!WriteFile(Connection->conn, buffer_pos, Header->common.frag_len - hdr_size, &count, &Connection->ovl[1]) &&
286 ERROR_IO_PENDING != GetLastError()) {
287 WARN("WriteFile failed with error %ld\n", GetLastError());
288 return GetLastError();
289 }
290 if (!GetOverlappedResult(Connection->conn, &Connection->ovl[1], &count, TRUE)) {
291 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
292 return GetLastError();
293 }
294
295 Header->common.flags &= ~RPC_FLG_FIRST;
296 }
297
298 return RPC_S_OK;
299 }
300
301 /***********************************************************************
302 * RPCRT4_Receive (internal)
303 *
304 * Receive a packet from connection and merge the fragments.
305 */
306 RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
307 PRPC_MESSAGE pMsg)
308 {
309 RPC_STATUS status;
310 DWORD dwRead, hdr_length;
311 unsigned short first_flag;
312 unsigned long data_length;
313 unsigned long buffer_length;
314 unsigned char *buffer_ptr;
315 RpcPktCommonHdr common_hdr;
316
317 *Header = NULL;
318
319 TRACE("(%p, %p, %p)\n", Connection, Header, pMsg);
320
321 /* read packet common header */
322 if (!ReadFile(Connection->conn, &common_hdr, sizeof(common_hdr), &dwRead, &Connection->ovl[0]) &&
323 ERROR_IO_PENDING != GetLastError()) {
324 WARN("ReadFile failed with error %ld\n", GetLastError());
325 status = RPC_S_PROTOCOL_ERROR;
326 goto fail;
327 }
328 if (!GetOverlappedResult(Connection->conn, &Connection->ovl[0], &dwRead, TRUE)) {
329 if (GetLastError() != ERROR_MORE_DATA) {
330 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
331 status = RPC_S_PROTOCOL_ERROR;
332 goto fail;
333 }
334 }
335 if (dwRead != sizeof(common_hdr)) {
336 WARN("Short read of header, %ld/%d bytes\n", dwRead, sizeof(common_hdr));
337 status = RPC_S_PROTOCOL_ERROR;
338 goto fail;
339 }
340
341 /* verify if the header really makes sense */
342 if (common_hdr.rpc_ver != RPC_VER_MAJOR ||
343 common_hdr.rpc_ver_minor != RPC_VER_MINOR) {
344 WARN("unhandled packet version\n");
345 status = RPC_S_PROTOCOL_ERROR;
346 goto fail;
347 }
348
349 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
350 if (hdr_length == 0) {
351 WARN("header length == 0\n");
352 status = RPC_S_PROTOCOL_ERROR;
353 goto fail;
354 }
355
356 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
357 memcpy(*Header, &common_hdr, sizeof(common_hdr));
358
359 /* read the rest of packet header */
360 if (!ReadFile(Connection->conn, &(*Header)->common + 1,
361 hdr_length - sizeof(common_hdr), &dwRead, &Connection->ovl[0]) &&
362 ERROR_IO_PENDING != GetLastError()) {
363 WARN("ReadFile failed with error %ld\n", GetLastError());
364 status = RPC_S_PROTOCOL_ERROR;
365 goto fail;
366 }
367 if (!GetOverlappedResult(Connection->conn, &Connection->ovl[0], &dwRead, TRUE)) {
368 if (GetLastError() != ERROR_MORE_DATA) {
369 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
370 status = RPC_S_PROTOCOL_ERROR;
371 goto fail;
372 }
373 }
374 if (dwRead != hdr_length - sizeof(common_hdr)) {
375 WARN("bad header length, %ld/%ld bytes\n", dwRead, hdr_length - sizeof(common_hdr));
376 status = RPC_S_PROTOCOL_ERROR;
377 goto fail;
378 }
379
380
381 /* read packet body */
382 switch (common_hdr.ptype) {
383 case PKT_RESPONSE:
384 pMsg->BufferLength = (*Header)->response.alloc_hint;
385 break;
386 case PKT_REQUEST:
387 pMsg->BufferLength = (*Header)->request.alloc_hint;
388 break;
389 default:
390 pMsg->BufferLength = common_hdr.frag_len - hdr_length;
391 }
392
393 TRACE("buffer length = %u\n", pMsg->BufferLength);
394
395 status = I_RpcGetBuffer(pMsg);
396 if (status != RPC_S_OK) goto fail;
397
398 first_flag = RPC_FLG_FIRST;
399 buffer_length = 0;
400 buffer_ptr = pMsg->Buffer;
401 while (buffer_length < pMsg->BufferLength)
402 {
403 data_length = (*Header)->common.frag_len - hdr_length;
404 if (((*Header)->common.flags & RPC_FLG_FIRST) != first_flag ||
405 data_length + buffer_length > pMsg->BufferLength) {
406 TRACE("invalid packet flags or buffer length\n");
407 status = RPC_S_PROTOCOL_ERROR;
408 goto fail;
409 }
410
411 if (data_length == 0) dwRead = 0; else {
412 if (!ReadFile(Connection->conn, buffer_ptr, data_length, &dwRead, &Connection->ovl[0]) &&
413 ERROR_IO_PENDING != GetLastError()) {
414 WARN("ReadFile failed with error %ld\n", GetLastError());
415 status = RPC_S_PROTOCOL_ERROR;
416 goto fail;
417 }
418 if (!GetOverlappedResult(Connection->conn, &Connection->ovl[0], &dwRead, TRUE)) {
419 if (GetLastError() != ERROR_MORE_DATA) {
420 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
421 status = RPC_S_PROTOCOL_ERROR;
422 goto fail;
423 }
424 }
425 }
426 if (dwRead != data_length) {
427 WARN("bad data length, %ld/%ld\n", dwRead, data_length);
428 status = RPC_S_PROTOCOL_ERROR;
429 goto fail;
430 }
431
432 /* when there is no more data left, it should be the last packet */
433 if (buffer_length == pMsg->BufferLength &&
434 ((*Header)->common.flags & RPC_FLG_LAST) == 0) {
435 WARN("no more data left, but not last packet\n");
436 status = RPC_S_PROTOCOL_ERROR;
437 goto fail;
438 }
439
440 buffer_length += data_length;
441 if (buffer_length < pMsg->BufferLength) {
442 TRACE("next header\n");
443
444 /* read the header of next packet */
445 if (!ReadFile(Connection->conn, *Header, hdr_length, &dwRead, &Connection->ovl[0]) &&
446 ERROR_IO_PENDING != GetLastError()) {
447 WARN("ReadFile failed with error %ld\n", GetLastError());
448 status = GetLastError();
449 goto fail;
450 }
451 if (!GetOverlappedResult(Connection->conn, &Connection->ovl[0], &dwRead, TRUE)) {
452 if (GetLastError() != ERROR_MORE_DATA) {
453 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
454 status = RPC_S_PROTOCOL_ERROR;
455 goto fail;
456 }
457 }
458 if (dwRead != hdr_length) {
459 WARN("invalid packet header size (%ld)\n", dwRead);
460 status = RPC_S_PROTOCOL_ERROR;
461 goto fail;
462 }
463
464 buffer_ptr += data_length;
465 first_flag = 0;
466 }
467 }
468
469 /* success */
470 status = RPC_S_OK;
471
472 fail:
473 if (status != RPC_S_OK && *Header) {
474 RPCRT4_FreeHeader(*Header);
475 *Header = NULL;
476 }
477 return status;
478 }
479
480 /***********************************************************************
481 * I_RpcGetBuffer [RPCRT4.@]
482 */
483 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
484 {
485 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
486
487 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
488 /* FIXME: pfnAllocate? */
489 if (bind->server) {
490 /* it turns out that the original buffer data must still be available
491 * while the RPC server is marshalling a reply, so we should not deallocate
492 * it, we'll leave deallocating the original buffer to the RPC server */
493 pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
494 } else {
495 HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
496 pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
497 }
498 TRACE("Buffer=%p\n", pMsg->Buffer);
499 /* FIXME: which errors to return? */
500 return pMsg->Buffer ? S_OK : E_OUTOFMEMORY;
501 }
502
503 /***********************************************************************
504 * I_RpcFreeBuffer [RPCRT4.@]
505 */
506 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
507 {
508 TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
509 /* FIXME: pfnFree? */
510 HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
511 pMsg->Buffer = NULL;
512 return S_OK;
513 }
514
515 /***********************************************************************
516 * I_RpcSend [RPCRT4.@]
517 */
518 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
519 {
520 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
521 RpcConnection* conn;
522 RPC_CLIENT_INTERFACE* cif = NULL;
523 RPC_SERVER_INTERFACE* sif = NULL;
524 RPC_STATUS status;
525 RpcPktHdr *hdr;
526
527 TRACE("(%p)\n", pMsg);
528 if (!bind) return RPC_S_INVALID_BINDING;
529
530 if (bind->server) {
531 sif = pMsg->RpcInterfaceInformation;
532 if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
533 status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
534 &sif->InterfaceId);
535 } else {
536 cif = pMsg->RpcInterfaceInformation;
537 if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
538 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
539 &cif->InterfaceId);
540 }
541
542 if (status != RPC_S_OK) return status;
543
544 if (bind->server) {
545 if (pMsg->RpcFlags & WINE_RPCFLAG_EXCEPTION) {
546 hdr = RPCRT4_BuildFaultHeader(pMsg->DataRepresentation,
547 RPC_S_CALL_FAILED);
548 } else {
549 hdr = RPCRT4_BuildResponseHeader(pMsg->DataRepresentation,
550 pMsg->BufferLength);
551 }
552 } else {
553 hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
554 pMsg->BufferLength, pMsg->ProcNum,
555 &bind->ObjectUuid);
556 }
557
558 status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
559
560 RPCRT4_FreeHeader(hdr);
561
562 /* success */
563 if (!bind->server) {
564 /* save the connection, so the response can be read from it */
565 pMsg->ReservedForRuntime = conn;
566 return RPC_S_OK;
567 }
568 RPCRT4_CloseBinding(bind, conn);
569 status = RPC_S_OK;
570
571 return status;
572 }
573
574 /***********************************************************************
575 * I_RpcReceive [RPCRT4.@]
576 */
577 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
578 {
579 RpcBinding* bind = (RpcBinding*)pMsg->Handle;
580 RpcConnection* conn;
581 RPC_CLIENT_INTERFACE* cif = NULL;
582 RPC_SERVER_INTERFACE* sif = NULL;
583 RPC_STATUS status;
584 RpcPktHdr *hdr = NULL;
585
586 TRACE("(%p)\n", pMsg);
587 if (!bind) return RPC_S_INVALID_BINDING;
588
589 if (pMsg->ReservedForRuntime) {
590 conn = pMsg->ReservedForRuntime;
591 pMsg->ReservedForRuntime = NULL;
592 } else {
593 if (bind->server) {
594 sif = pMsg->RpcInterfaceInformation;
595 if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
596 status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
597 &sif->InterfaceId);
598 } else {
599 cif = pMsg->RpcInterfaceInformation;
600 if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
601 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
602 &cif->InterfaceId);
603 }
604 if (status != RPC_S_OK) return status;
605 }
606
607 status = RPCRT4_Receive(conn, &hdr, pMsg);
608 if (status != RPC_S_OK) {
609 WARN("receive failed with error %lx\n", status);
610 goto fail;
611 }
612
613 status = RPC_S_PROTOCOL_ERROR;
614
615 switch (hdr->common.ptype) {
616 case PKT_RESPONSE:
617 if (bind->server) goto fail;
618 break;
619 case PKT_REQUEST:
620 if (!bind->server) goto fail;
621 break;
622 case PKT_FAULT:
623 pMsg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
624 ERR ("we got fault packet with status %lx\n", hdr->fault.status);
625 status = RPC_S_CALL_FAILED; /* ? */
626 goto fail;
627 default:
628 WARN("bad packet type %d\n", hdr->common.ptype);
629 goto fail;
630 }
631
632 /* success */
633 status = RPC_S_OK;
634
635 fail:
636 if (hdr) {
637 RPCRT4_FreeHeader(hdr);
638 }
639 RPCRT4_CloseBinding(bind, conn);
640 return status;
641 }
642
643 /***********************************************************************
644 * I_RpcSendReceive [RPCRT4.@]
645 */
646 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
647 {
648 RPC_STATUS status;
649
650 TRACE("(%p)\n", pMsg);
651 status = I_RpcSend(pMsg);
652 if (status == RPC_S_OK)
653 status = I_RpcReceive(pMsg);
654 return status;
655 }