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