dsdmo: Add Flanger effect stub.
[wine.git] / dlls / rpcrt4 / rpc_message.c
blob46736c21fc1245ee07b2fe679aaff2cb2aac423d
1 /*
2 * RPC messages
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "winuser.h"
33 #include "rpc.h"
34 #include "rpcndr.h"
35 #include "rpcdcep.h"
37 #include "wine/debug.h"
39 #include "rpc_binding.h"
40 #include "rpc_defs.h"
41 #include "rpc_message.h"
42 #include "rpc_assoc.h"
43 #include "ncastatus.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
47 /* note: the DCE/RPC spec says the alignment amount should be 4, but
48 * MS/RPC servers seem to always use 16 */
49 #define AUTH_ALIGNMENT 16
51 /* gets the amount needed to round a value up to the specified alignment */
52 #define ROUND_UP_AMOUNT(value, alignment) \
53 (((alignment) - (((value) % (alignment)))) % (alignment))
54 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
56 static RPC_STATUS I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg);
58 DWORD RPCRT4_GetHeaderSize(const RpcPktHdr *Header)
60 static const DWORD header_sizes[] = {
61 sizeof(Header->request), 0, sizeof(Header->response),
62 sizeof(Header->fault), 0, 0, 0, 0, 0, 0, 0, sizeof(Header->bind),
63 sizeof(Header->bind_ack), sizeof(Header->bind_nack),
64 0, 0, sizeof(Header->auth3), 0, 0, 0, sizeof(Header->http)
66 ULONG ret = 0;
68 if (Header->common.ptype < ARRAY_SIZE(header_sizes)) {
69 ret = header_sizes[Header->common.ptype];
70 if (ret == 0)
71 FIXME("unhandled packet type %u\n", Header->common.ptype);
72 if (Header->common.flags & RPC_FLG_OBJECT_UUID)
73 ret += sizeof(UUID);
74 } else {
75 WARN("invalid packet type %u\n", Header->common.ptype);
78 return ret;
81 static BOOL packet_has_body(const RpcPktHdr *Header)
83 return (Header->common.ptype == PKT_FAULT) ||
84 (Header->common.ptype == PKT_REQUEST) ||
85 (Header->common.ptype == PKT_RESPONSE);
88 static BOOL packet_has_auth_verifier(const RpcPktHdr *Header)
90 return !(Header->common.ptype == PKT_BIND_NACK) &&
91 !(Header->common.ptype == PKT_SHUTDOWN);
94 static BOOL packet_does_auth_negotiation(const RpcPktHdr *Header)
96 switch (Header->common.ptype)
98 case PKT_BIND:
99 case PKT_BIND_ACK:
100 case PKT_AUTH3:
101 case PKT_ALTER_CONTEXT:
102 case PKT_ALTER_CONTEXT_RESP:
103 return TRUE;
104 default:
105 return FALSE;
109 static VOID RPCRT4_BuildCommonHeader(RpcPktCommonHdr *Header, unsigned char PacketType,
110 ULONG DataRepresentation)
112 Header->rpc_ver = RPC_VER_MAJOR;
113 Header->rpc_ver_minor = RPC_VER_MINOR;
114 Header->ptype = PacketType;
115 Header->drep[0] = LOBYTE(LOWORD(DataRepresentation));
116 Header->drep[1] = HIBYTE(LOWORD(DataRepresentation));
117 Header->drep[2] = LOBYTE(HIWORD(DataRepresentation));
118 Header->drep[3] = HIBYTE(HIWORD(DataRepresentation));
119 Header->auth_len = 0;
120 Header->call_id = 1;
121 Header->flags = 0;
122 /* Flags and fragment length are computed in RPCRT4_Send. */
125 static RpcPktHdr *RPCRT4_BuildRequestHeader(ULONG DataRepresentation,
126 ULONG BufferLength,
127 unsigned short ProcNum,
128 UUID *ObjectUuid)
130 RpcPktHdr *header;
131 BOOL has_object;
132 RPC_STATUS status;
134 has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
135 header = calloc(1, sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
136 if (header == NULL) {
137 return NULL;
140 RPCRT4_BuildCommonHeader(&header->common, PKT_REQUEST, DataRepresentation);
141 header->common.frag_len = sizeof(header->request);
142 header->request.alloc_hint = BufferLength;
143 header->request.context_id = 0;
144 header->request.opnum = ProcNum;
145 if (has_object) {
146 header->common.flags |= RPC_FLG_OBJECT_UUID;
147 header->common.frag_len += sizeof(UUID);
148 memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
151 return header;
154 RpcPktHdr *RPCRT4_BuildResponseHeader(ULONG DataRepresentation, ULONG BufferLength)
156 RpcPktResponseHdr *header;
158 header = calloc(1, sizeof(*header));
159 if (header == NULL) {
160 return NULL;
163 RPCRT4_BuildCommonHeader(&header->common, PKT_RESPONSE, DataRepresentation);
164 header->common.frag_len = sizeof(*header);
165 header->alloc_hint = BufferLength;
167 return (RpcPktHdr *)header;
170 RpcPktHdr *RPCRT4_BuildFaultHeader(ULONG DataRepresentation, RPC_STATUS Status)
172 RpcPktFaultHdr *header;
174 header = calloc(1, sizeof(*header));
175 if (header == NULL) {
176 return NULL;
179 RPCRT4_BuildCommonHeader(&header->common, PKT_FAULT, DataRepresentation);
180 header->common.frag_len = sizeof(*header);
181 header->status = Status;
183 return (RpcPktHdr *)header;
186 RpcPktHdr *RPCRT4_BuildBindHeader(ULONG DataRepresentation,
187 unsigned short MaxTransmissionSize,
188 unsigned short MaxReceiveSize,
189 ULONG AssocGroupId,
190 const RPC_SYNTAX_IDENTIFIER *AbstractId,
191 const RPC_SYNTAX_IDENTIFIER *TransferId)
193 RpcPktHdr *header;
194 RpcContextElement *ctxt_elem;
196 header = calloc(1, sizeof(header->bind) + FIELD_OFFSET(RpcContextElement, transfer_syntaxes[1]));
197 if (header == NULL) {
198 return NULL;
200 ctxt_elem = (RpcContextElement *)(&header->bind + 1);
202 RPCRT4_BuildCommonHeader(&header->common, PKT_BIND, DataRepresentation);
203 header->common.frag_len = sizeof(header->bind) + FIELD_OFFSET(RpcContextElement, transfer_syntaxes[1]);
204 header->bind.max_tsize = MaxTransmissionSize;
205 header->bind.max_rsize = MaxReceiveSize;
206 header->bind.assoc_gid = AssocGroupId;
207 header->bind.num_elements = 1;
208 ctxt_elem->num_syntaxes = 1;
209 ctxt_elem->abstract_syntax = *AbstractId;
210 ctxt_elem->transfer_syntaxes[0] = *TransferId;
212 return header;
215 static RpcPktHdr *RPCRT4_BuildAuthHeader(ULONG DataRepresentation)
217 RpcPktAuth3Hdr *header;
219 header = calloc(1, sizeof(*header));
220 if (header == NULL)
221 return NULL;
223 RPCRT4_BuildCommonHeader(&header->common, PKT_AUTH3, DataRepresentation);
224 header->common.frag_len = sizeof(*header);
226 return (RpcPktHdr*)header;
229 RpcPktHdr *RPCRT4_BuildBindNackHeader(ULONG DataRepresentation,
230 unsigned char RpcVersion,
231 unsigned char RpcVersionMinor,
232 unsigned short RejectReason)
234 RpcPktBindNAckHdr *header;
235 C_ASSERT(sizeof(*header) >= FIELD_OFFSET(RpcPktBindNAckHdr, protocols[1]));
237 header = calloc(1, sizeof(*header));
238 if (header == NULL) {
239 return NULL;
242 RPCRT4_BuildCommonHeader(&header->common, PKT_BIND_NACK, DataRepresentation);
243 header->common.frag_len = FIELD_OFFSET(RpcPktBindNAckHdr, protocols[1]);
244 header->reject_reason = RejectReason;
245 header->protocols_count = 1;
246 header->protocols[0].rpc_ver = RpcVersion;
247 header->protocols[0].rpc_ver_minor = RpcVersionMinor;
249 return (RpcPktHdr *)header;
252 RpcPktHdr *RPCRT4_BuildBindAckHeader(ULONG DataRepresentation,
253 unsigned short MaxTransmissionSize,
254 unsigned short MaxReceiveSize,
255 ULONG AssocGroupId,
256 LPCSTR ServerAddress,
257 unsigned char ResultCount,
258 const RpcResult *Results)
260 RpcPktHdr *header;
261 ULONG header_size;
262 RpcAddressString *server_address;
263 RpcResultList *results;
265 header_size = sizeof(header->bind_ack) +
266 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[strlen(ServerAddress) + 1]), 4) +
267 FIELD_OFFSET(RpcResultList, results[ResultCount]);
269 header = calloc(1, header_size);
270 if (header == NULL) {
271 return NULL;
274 RPCRT4_BuildCommonHeader(&header->common, PKT_BIND_ACK, DataRepresentation);
275 header->common.frag_len = header_size;
276 header->bind_ack.max_tsize = MaxTransmissionSize;
277 header->bind_ack.max_rsize = MaxReceiveSize;
278 header->bind_ack.assoc_gid = AssocGroupId;
279 server_address = (RpcAddressString*)(&header->bind_ack + 1);
280 server_address->length = strlen(ServerAddress) + 1;
281 strcpy(server_address->string, ServerAddress);
282 /* results is 4-byte aligned */
283 results = (RpcResultList*)((ULONG_PTR)server_address + ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4));
284 results->num_results = ResultCount;
285 memcpy(&results->results[0], Results, ResultCount * sizeof(*Results));
287 return header;
290 RpcPktHdr *RPCRT4_BuildHttpHeader(ULONG DataRepresentation,
291 unsigned short flags,
292 unsigned short num_data_items,
293 unsigned int payload_size)
295 RpcPktHdr *header;
297 header = calloc(1, sizeof(header->http) + payload_size);
298 if (header == NULL) {
299 ERR("failed to allocate memory\n");
300 return NULL;
303 RPCRT4_BuildCommonHeader(&header->common, PKT_HTTP, DataRepresentation);
304 /* since the packet isn't current sent using RPCRT4_Send, set the flags
305 * manually here */
306 header->common.flags = RPC_FLG_FIRST|RPC_FLG_LAST;
307 header->common.call_id = 0;
308 header->common.frag_len = sizeof(header->http) + payload_size;
309 header->http.flags = flags;
310 header->http.num_data_items = num_data_items;
312 return header;
315 #define WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, type, value) \
316 do { \
317 *(unsigned int *)(payload) = (type); \
318 (payload) += 4; \
319 *(unsigned int *)(payload) = (value); \
320 (payload) += 4; \
321 } while (0)
323 #define WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, type, uuid) \
324 do { \
325 *(unsigned int *)(payload) = (type); \
326 (payload) += 4; \
327 *(UUID *)(payload) = (uuid); \
328 (payload) += sizeof(UUID); \
329 } while (0)
331 #define WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted, flow_control_increment, uuid) \
332 do { \
333 *(unsigned int *)(payload) = 0x00000001; \
334 (payload) += 4; \
335 *(unsigned int *)(payload) = (bytes_transmitted); \
336 (payload) += 4; \
337 *(unsigned int *)(payload) = (flow_control_increment); \
338 (payload) += 4; \
339 *(UUID *)(payload) = (uuid); \
340 (payload) += sizeof(UUID); \
341 } while (0)
343 RpcPktHdr *RPCRT4_BuildHttpConnectHeader(int out_pipe,
344 const UUID *connection_uuid,
345 const UUID *pipe_uuid,
346 const UUID *association_uuid)
348 RpcPktHdr *header;
349 unsigned int size;
350 char *payload;
352 size = 8 + 4 + sizeof(UUID) + 4 + sizeof(UUID) + 8;
353 if (!out_pipe)
354 size += 8 + 4 + sizeof(UUID);
356 header = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0,
357 out_pipe ? 4 : 6, size);
358 if (!header) return NULL;
359 payload = (char *)(&header->http+1);
361 /* FIXME: what does this part of the payload do? */
362 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000006, 0x00000001);
364 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x00000003, *connection_uuid);
365 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x00000003, *pipe_uuid);
367 if (out_pipe)
368 /* FIXME: what does this part of the payload do? */
369 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000000, 0x00010000);
370 else
372 /* FIXME: what does this part of the payload do? */
373 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000004, 0x40000000);
374 /* FIXME: what does this part of the payload do? */
375 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000005, 0x000493e0);
377 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x0000000c, *association_uuid);
380 return header;
383 RpcPktHdr *RPCRT4_BuildHttpFlowControlHeader(BOOL server, ULONG bytes_transmitted,
384 ULONG flow_control_increment,
385 const UUID *pipe_uuid)
387 RpcPktHdr *header;
388 char *payload;
390 header = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x2, 2,
391 5 * sizeof(ULONG) + sizeof(UUID));
392 if (!header) return NULL;
393 payload = (char *)(&header->http+1);
395 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x0000000d, (server ? 0x0 : 0x3));
397 WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted,
398 flow_control_increment, *pipe_uuid);
399 return header;
402 NCA_STATUS RPC2NCA_STATUS(RPC_STATUS status)
404 switch (status)
406 case ERROR_INVALID_HANDLE: return NCA_S_FAULT_CONTEXT_MISMATCH;
407 case ERROR_OUTOFMEMORY: return NCA_S_FAULT_REMOTE_NO_MEMORY;
408 case RPC_S_NOT_LISTENING: return NCA_S_SERVER_TOO_BUSY;
409 case RPC_S_UNKNOWN_IF: return NCA_S_UNK_IF;
410 case RPC_S_SERVER_TOO_BUSY: return NCA_S_SERVER_TOO_BUSY;
411 case RPC_S_CALL_FAILED: return NCA_S_FAULT_UNSPEC;
412 case RPC_S_CALL_FAILED_DNE: return NCA_S_MANAGER_NOT_ENTERED;
413 case RPC_S_PROTOCOL_ERROR: return NCA_S_PROTO_ERROR;
414 case RPC_S_UNSUPPORTED_TYPE: return NCA_S_UNSUPPORTED_TYPE;
415 case RPC_S_INVALID_TAG: return NCA_S_FAULT_INVALID_TAG;
416 case RPC_S_INVALID_BOUND: return NCA_S_FAULT_INVALID_BOUND;
417 case RPC_S_PROCNUM_OUT_OF_RANGE: return NCA_S_OP_RNG_ERROR;
418 case RPC_X_SS_HANDLES_MISMATCH: return NCA_S_FAULT_CONTEXT_MISMATCH;
419 case RPC_S_CALL_CANCELLED: return NCA_S_FAULT_CANCEL;
420 case RPC_S_COMM_FAILURE: return NCA_S_COMM_FAILURE;
421 case RPC_X_WRONG_PIPE_ORDER: return NCA_S_FAULT_PIPE_ORDER;
422 case RPC_X_PIPE_CLOSED: return NCA_S_FAULT_PIPE_CLOSED;
423 case RPC_X_PIPE_DISCIPLINE_ERROR: return NCA_S_FAULT_PIPE_DISCIPLINE;
424 case RPC_X_PIPE_EMPTY: return NCA_S_FAULT_PIPE_EMPTY;
425 case STATUS_FLOAT_DIVIDE_BY_ZERO: return NCA_S_FAULT_FP_DIV_ZERO;
426 case STATUS_FLOAT_INVALID_OPERATION: return NCA_S_FAULT_FP_ERROR;
427 case STATUS_FLOAT_OVERFLOW: return NCA_S_FAULT_FP_OVERFLOW;
428 case STATUS_FLOAT_UNDERFLOW: return NCA_S_FAULT_FP_UNDERFLOW;
429 case STATUS_INTEGER_DIVIDE_BY_ZERO: return NCA_S_FAULT_INT_DIV_BY_ZERO;
430 case STATUS_INTEGER_OVERFLOW: return NCA_S_FAULT_INT_OVERFLOW;
431 default: return status;
435 static RPC_STATUS NCA2RPC_STATUS(NCA_STATUS status)
437 switch (status)
439 case NCA_S_COMM_FAILURE: return RPC_S_COMM_FAILURE;
440 case NCA_S_OP_RNG_ERROR: return RPC_S_PROCNUM_OUT_OF_RANGE;
441 case NCA_S_UNK_IF: return RPC_S_UNKNOWN_IF;
442 case NCA_S_YOU_CRASHED: return RPC_S_CALL_FAILED;
443 case NCA_S_PROTO_ERROR: return RPC_S_PROTOCOL_ERROR;
444 case NCA_S_OUT_ARGS_TOO_BIG: return ERROR_NOT_ENOUGH_SERVER_MEMORY;
445 case NCA_S_SERVER_TOO_BUSY: return RPC_S_SERVER_TOO_BUSY;
446 case NCA_S_UNSUPPORTED_TYPE: return RPC_S_UNSUPPORTED_TYPE;
447 case NCA_S_FAULT_INT_DIV_BY_ZERO: return RPC_S_ZERO_DIVIDE;
448 case NCA_S_FAULT_ADDR_ERROR: return RPC_S_ADDRESS_ERROR;
449 case NCA_S_FAULT_FP_DIV_ZERO: return RPC_S_FP_DIV_ZERO;
450 case NCA_S_FAULT_FP_UNDERFLOW: return RPC_S_FP_UNDERFLOW;
451 case NCA_S_FAULT_FP_OVERFLOW: return RPC_S_FP_OVERFLOW;
452 case NCA_S_FAULT_INVALID_TAG: return RPC_S_INVALID_TAG;
453 case NCA_S_FAULT_INVALID_BOUND: return RPC_S_INVALID_BOUND;
454 case NCA_S_RPC_VERSION_MISMATCH: return RPC_S_PROTOCOL_ERROR;
455 case NCA_S_UNSPEC_REJECT: return RPC_S_CALL_FAILED_DNE;
456 case NCA_S_BAD_ACTID: return RPC_S_CALL_FAILED_DNE;
457 case NCA_S_WHO_ARE_YOU_FAILED: return RPC_S_CALL_FAILED;
458 case NCA_S_MANAGER_NOT_ENTERED: return RPC_S_CALL_FAILED_DNE;
459 case NCA_S_FAULT_CANCEL: return RPC_S_CALL_CANCELLED;
460 case NCA_S_FAULT_ILL_INST: return RPC_S_ADDRESS_ERROR;
461 case NCA_S_FAULT_FP_ERROR: return RPC_S_FP_OVERFLOW;
462 case NCA_S_FAULT_INT_OVERFLOW: return RPC_S_ADDRESS_ERROR;
463 case NCA_S_FAULT_UNSPEC: return RPC_S_CALL_FAILED;
464 case NCA_S_FAULT_PIPE_EMPTY: return RPC_X_PIPE_EMPTY;
465 case NCA_S_FAULT_PIPE_CLOSED: return RPC_X_PIPE_CLOSED;
466 case NCA_S_FAULT_PIPE_ORDER: return RPC_X_WRONG_PIPE_ORDER;
467 case NCA_S_FAULT_PIPE_DISCIPLINE: return RPC_X_PIPE_DISCIPLINE_ERROR;
468 case NCA_S_FAULT_PIPE_COMM_ERROR: return RPC_S_COMM_FAILURE;
469 case NCA_S_FAULT_PIPE_MEMORY: return ERROR_OUTOFMEMORY;
470 case NCA_S_FAULT_CONTEXT_MISMATCH: return ERROR_INVALID_HANDLE;
471 case NCA_S_FAULT_REMOTE_NO_MEMORY: return ERROR_NOT_ENOUGH_SERVER_MEMORY;
472 default: return status;
476 /* assumes the common header fields have already been validated */
477 BOOL RPCRT4_IsValidHttpPacket(RpcPktHdr *hdr, unsigned char *data,
478 unsigned short data_len)
480 unsigned short i;
481 BYTE *p = data;
483 for (i = 0; i < hdr->http.num_data_items; i++)
485 ULONG type;
487 if (data_len < sizeof(ULONG))
488 return FALSE;
490 type = *(ULONG *)p;
491 p += sizeof(ULONG);
492 data_len -= sizeof(ULONG);
494 switch (type)
496 case 0x3:
497 case 0xc:
498 if (data_len < sizeof(GUID))
499 return FALSE;
500 p += sizeof(GUID);
501 data_len -= sizeof(GUID);
502 break;
503 case 0x0:
504 case 0x2:
505 case 0x4:
506 case 0x5:
507 case 0x6:
508 case 0xd:
509 if (data_len < sizeof(ULONG))
510 return FALSE;
511 p += sizeof(ULONG);
512 data_len -= sizeof(ULONG);
513 break;
514 case 0x1:
515 if (data_len < 24)
516 return FALSE;
517 p += 24;
518 data_len -= 24;
519 break;
520 default:
521 FIXME("unimplemented type 0x%lx\n", type);
522 break;
525 return TRUE;
528 /* assumes the HTTP packet has been validated */
529 static unsigned char *RPCRT4_NextHttpHeaderField(unsigned char *data)
531 ULONG type;
533 type = *(ULONG *)data;
534 data += sizeof(ULONG);
536 switch (type)
538 case 0x3:
539 case 0xc:
540 return data + sizeof(GUID);
541 case 0x0:
542 case 0x2:
543 case 0x4:
544 case 0x5:
545 case 0x6:
546 case 0xd:
547 return data + sizeof(ULONG);
548 case 0x1:
549 return data + 24;
550 default:
551 FIXME("unimplemented type 0x%lx\n", type);
552 return data;
556 #define READ_HTTP_PAYLOAD_FIELD_TYPE(data) *(ULONG *)(data)
557 #define GET_HTTP_PAYLOAD_FIELD_DATA(data) ((data) + sizeof(ULONG))
559 /* assumes the HTTP packet has been validated */
560 RPC_STATUS RPCRT4_ParseHttpPrepareHeader1(RpcPktHdr *header,
561 unsigned char *data, ULONG *field1)
563 ULONG type;
564 if (header->http.flags != 0x0)
566 ERR("invalid flags 0x%x\n", header->http.flags);
567 return RPC_S_PROTOCOL_ERROR;
569 if (header->http.num_data_items != 1)
571 ERR("invalid number of data items %d\n", header->http.num_data_items);
572 return RPC_S_PROTOCOL_ERROR;
574 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
575 if (type != 0x00000002)
577 ERR("invalid type 0x%08lx\n", type);
578 return RPC_S_PROTOCOL_ERROR;
580 *field1 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
581 return RPC_S_OK;
584 /* assumes the HTTP packet has been validated */
585 RPC_STATUS RPCRT4_ParseHttpPrepareHeader2(RpcPktHdr *header,
586 unsigned char *data, ULONG *field1,
587 ULONG *bytes_until_next_packet,
588 ULONG *field3)
590 ULONG type;
591 if (header->http.flags != 0x0)
593 ERR("invalid flags 0x%x\n", header->http.flags);
594 return RPC_S_PROTOCOL_ERROR;
596 if (header->http.num_data_items != 3)
598 ERR("invalid number of data items %d\n", header->http.num_data_items);
599 return RPC_S_PROTOCOL_ERROR;
602 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
603 if (type != 0x00000006)
605 ERR("invalid type for field 1: 0x%08lx\n", type);
606 return RPC_S_PROTOCOL_ERROR;
608 *field1 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
609 data = RPCRT4_NextHttpHeaderField(data);
611 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
612 if (type != 0x00000000)
614 ERR("invalid type for field 2: 0x%08lx\n", type);
615 return RPC_S_PROTOCOL_ERROR;
617 *bytes_until_next_packet = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
618 data = RPCRT4_NextHttpHeaderField(data);
620 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
621 if (type != 0x00000002)
623 ERR("invalid type for field 3: 0x%08lx\n", type);
624 return RPC_S_PROTOCOL_ERROR;
626 *field3 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
628 return RPC_S_OK;
631 RPC_STATUS RPCRT4_ParseHttpFlowControlHeader(RpcPktHdr *header,
632 unsigned char *data, BOOL server,
633 ULONG *bytes_transmitted,
634 ULONG *flow_control_increment,
635 UUID *pipe_uuid)
637 ULONG type;
638 if (header->http.flags != 0x2)
640 ERR("invalid flags 0x%x\n", header->http.flags);
641 return RPC_S_PROTOCOL_ERROR;
643 if (header->http.num_data_items != 2)
645 ERR("invalid number of data items %d\n", header->http.num_data_items);
646 return RPC_S_PROTOCOL_ERROR;
649 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
650 if (type != 0x0000000d)
652 ERR("invalid type for field 1: 0x%08lx\n", type);
653 return RPC_S_PROTOCOL_ERROR;
655 if (*(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data) != (server ? 0x3 : 0x0))
657 ERR("invalid type for 0xd field data: 0x%08lx\n", *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data));
658 return RPC_S_PROTOCOL_ERROR;
660 data = RPCRT4_NextHttpHeaderField(data);
662 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
663 if (type != 0x00000001)
665 ERR("invalid type for field 2: 0x%08lx\n", type);
666 return RPC_S_PROTOCOL_ERROR;
668 *bytes_transmitted = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
669 *flow_control_increment = *(ULONG *)(GET_HTTP_PAYLOAD_FIELD_DATA(data) + 4);
670 *pipe_uuid = *(UUID *)(GET_HTTP_PAYLOAD_FIELD_DATA(data) + 8);
672 return RPC_S_OK;
676 RPC_STATUS RPCRT4_default_secure_packet(RpcConnection *Connection,
677 enum secure_packet_direction dir,
678 RpcPktHdr *hdr, unsigned int hdr_size,
679 unsigned char *stub_data, unsigned int stub_data_size,
680 RpcAuthVerifier *auth_hdr,
681 unsigned char *auth_value, unsigned int auth_value_size)
683 SecBufferDesc message;
684 SecBuffer buffers[4];
685 SECURITY_STATUS sec_status;
687 message.ulVersion = SECBUFFER_VERSION;
688 message.cBuffers = ARRAY_SIZE(buffers);
689 message.pBuffers = buffers;
691 buffers[0].cbBuffer = hdr_size;
692 buffers[0].BufferType = SECBUFFER_DATA|SECBUFFER_READONLY_WITH_CHECKSUM;
693 buffers[0].pvBuffer = hdr;
694 buffers[1].cbBuffer = stub_data_size;
695 buffers[1].BufferType = SECBUFFER_DATA;
696 buffers[1].pvBuffer = stub_data;
697 buffers[2].cbBuffer = sizeof(*auth_hdr);
698 buffers[2].BufferType = SECBUFFER_DATA|SECBUFFER_READONLY_WITH_CHECKSUM;
699 buffers[2].pvBuffer = auth_hdr;
700 buffers[3].cbBuffer = auth_value_size;
701 buffers[3].BufferType = SECBUFFER_TOKEN;
702 buffers[3].pvBuffer = auth_value;
704 if (dir == SECURE_PACKET_SEND)
706 if ((auth_hdr->auth_level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(hdr))
708 sec_status = EncryptMessage(&Connection->ctx, 0, &message, 0 /* FIXME */);
709 if (sec_status != SEC_E_OK)
711 ERR("EncryptMessage failed with 0x%08lx\n", sec_status);
712 return RPC_S_SEC_PKG_ERROR;
715 else if (auth_hdr->auth_level != RPC_C_AUTHN_LEVEL_NONE)
717 sec_status = MakeSignature(&Connection->ctx, 0, &message, 0 /* FIXME */);
718 if (sec_status != SEC_E_OK)
720 ERR("MakeSignature failed with 0x%08lx\n", sec_status);
721 return RPC_S_SEC_PKG_ERROR;
725 else if (dir == SECURE_PACKET_RECEIVE)
727 if ((auth_hdr->auth_level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(hdr))
729 sec_status = DecryptMessage(&Connection->ctx, &message, 0 /* FIXME */, 0);
730 if (sec_status != SEC_E_OK)
732 ERR("DecryptMessage failed with 0x%08lx\n", sec_status);
733 return RPC_S_SEC_PKG_ERROR;
736 else if (auth_hdr->auth_level != RPC_C_AUTHN_LEVEL_NONE)
738 sec_status = VerifySignature(&Connection->ctx, &message, 0 /* FIXME */, NULL);
739 if (sec_status != SEC_E_OK)
741 ERR("VerifySignature failed with 0x%08lx\n", sec_status);
742 return RPC_S_SEC_PKG_ERROR;
747 return RPC_S_OK;
750 /***********************************************************************
751 * RPCRT4_SendWithAuth (internal)
753 * Transmit a packet with authorization data over connection in acceptable fragments.
755 RPC_STATUS RPCRT4_SendWithAuth(RpcConnection *Connection, RpcPktHdr *Header,
756 void *Buffer, unsigned int BufferLength,
757 const void *Auth, unsigned int AuthLength)
759 PUCHAR buffer_pos;
760 DWORD hdr_size;
761 LONG count;
762 unsigned char *pkt;
763 LONG alen;
764 RPC_STATUS status;
766 RPCRT4_SetThreadCurrentConnection(Connection);
768 buffer_pos = Buffer;
769 /* The packet building functions save the packet header size, so we can use it. */
770 hdr_size = Header->common.frag_len;
771 if (AuthLength)
772 Header->common.auth_len = AuthLength;
773 else if (Connection->AuthInfo && packet_has_auth_verifier(Header))
775 if ((Connection->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(Header))
776 Header->common.auth_len = Connection->encryption_auth_len;
777 else
778 Header->common.auth_len = Connection->signature_auth_len;
780 else
781 Header->common.auth_len = 0;
782 Header->common.flags |= RPC_FLG_FIRST;
783 Header->common.flags &= ~RPC_FLG_LAST;
785 alen = RPC_AUTH_VERIFIER_LEN(&Header->common);
787 while (!(Header->common.flags & RPC_FLG_LAST)) {
788 unsigned char auth_pad_len = Header->common.auth_len ? ROUND_UP_AMOUNT(BufferLength, AUTH_ALIGNMENT) : 0;
789 unsigned int pkt_size = BufferLength + hdr_size + alen + auth_pad_len;
791 /* decide if we need to split the packet into fragments */
792 if (pkt_size <= Connection->MaxTransmissionSize) {
793 Header->common.flags |= RPC_FLG_LAST;
794 Header->common.frag_len = pkt_size;
795 } else {
796 auth_pad_len = 0;
797 /* make sure packet payload will be a multiple of 16 */
798 Header->common.frag_len =
799 ((Connection->MaxTransmissionSize - hdr_size - alen) & ~(AUTH_ALIGNMENT-1)) +
800 hdr_size + alen;
803 pkt = calloc(1, Header->common.frag_len);
805 memcpy(pkt, Header, hdr_size);
807 /* fragment consisted of header only and is the last one */
808 if (hdr_size == Header->common.frag_len)
809 goto write;
811 memcpy(pkt + hdr_size, buffer_pos, Header->common.frag_len - hdr_size - auth_pad_len - alen);
813 /* add the authorization info */
814 if (Header->common.auth_len)
816 RpcAuthVerifier *auth_hdr = (RpcAuthVerifier *)&pkt[Header->common.frag_len - alen];
818 auth_hdr->auth_type = Connection->AuthInfo->AuthnSvc;
819 auth_hdr->auth_level = Connection->AuthInfo->AuthnLevel;
820 auth_hdr->auth_pad_length = auth_pad_len;
821 auth_hdr->auth_reserved = 0;
822 /* a unique number... */
823 auth_hdr->auth_context_id = Connection->auth_context_id;
825 if (AuthLength)
826 memcpy(auth_hdr + 1, Auth, AuthLength);
827 else
829 status = rpcrt4_conn_secure_packet(Connection, SECURE_PACKET_SEND,
830 (RpcPktHdr *)pkt, hdr_size,
831 pkt + hdr_size, Header->common.frag_len - hdr_size - alen,
832 auth_hdr,
833 (unsigned char *)(auth_hdr + 1), Header->common.auth_len);
834 if (status != RPC_S_OK)
836 free(pkt);
837 RPCRT4_SetThreadCurrentConnection(NULL);
838 return status;
843 write:
844 count = rpcrt4_conn_write(Connection, pkt, Header->common.frag_len);
845 free(pkt);
846 if (count<0) {
847 WARN("rpcrt4_conn_write failed (auth)\n");
848 RPCRT4_SetThreadCurrentConnection(NULL);
849 return RPC_S_CALL_FAILED;
852 buffer_pos += Header->common.frag_len - hdr_size - alen - auth_pad_len;
853 BufferLength -= Header->common.frag_len - hdr_size - alen - auth_pad_len;
854 Header->common.flags &= ~RPC_FLG_FIRST;
857 RPCRT4_SetThreadCurrentConnection(NULL);
858 return RPC_S_OK;
861 /***********************************************************************
862 * RPCRT4_default_authorize (internal)
864 * Authorize a client connection.
866 RPC_STATUS RPCRT4_default_authorize(RpcConnection *conn, BOOL first_time,
867 unsigned char *in_buffer,
868 unsigned int in_size,
869 unsigned char *out_buffer,
870 unsigned int *out_size)
872 SECURITY_STATUS r;
873 SecBufferDesc out_desc;
874 SecBufferDesc inp_desc;
875 SecPkgContext_Sizes secctx_sizes;
876 BOOL continue_needed;
877 ULONG context_req;
878 SecBuffer in, out;
880 if (!out_buffer)
882 *out_size = conn->AuthInfo->cbMaxToken;
883 return RPC_S_OK;
886 in.BufferType = SECBUFFER_TOKEN;
887 in.pvBuffer = in_buffer;
888 in.cbBuffer = in_size;
890 out.BufferType = SECBUFFER_TOKEN;
891 out.pvBuffer = out_buffer;
892 out.cbBuffer = *out_size;
894 out_desc.ulVersion = 0;
895 out_desc.cBuffers = 1;
896 out_desc.pBuffers = &out;
898 inp_desc.ulVersion = 0;
899 inp_desc.cBuffers = 1;
900 inp_desc.pBuffers = &in;
902 if (conn->server)
904 context_req = ASC_REQ_CONNECTION | ASC_REQ_USE_DCE_STYLE |
905 ASC_REQ_DELEGATE;
907 if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY)
908 context_req |= ASC_REQ_INTEGRITY;
909 else if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
910 context_req |= ASC_REQ_CONFIDENTIALITY | ASC_REQ_INTEGRITY;
912 r = AcceptSecurityContext(&conn->AuthInfo->cred,
913 first_time ? NULL : &conn->ctx,
914 &inp_desc, context_req, SECURITY_NETWORK_DREP,
915 &conn->ctx,
916 &out_desc, &conn->attr, &conn->exp);
917 if (r == SEC_E_OK || r == SEC_I_COMPLETE_NEEDED)
919 /* authorisation done, so nothing more to send */
920 out.cbBuffer = 0;
923 else
925 context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
926 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
928 if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY)
929 context_req |= ISC_REQ_INTEGRITY;
930 else if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
931 context_req |= ISC_REQ_CONFIDENTIALITY | ISC_REQ_INTEGRITY;
933 r = InitializeSecurityContextW(&conn->AuthInfo->cred,
934 first_time ? NULL: &conn->ctx,
935 first_time ? conn->AuthInfo->server_principal_name : NULL,
936 context_req, 0, SECURITY_NETWORK_DREP,
937 first_time ? NULL : &inp_desc, 0, &conn->ctx,
938 &out_desc, &conn->attr, &conn->exp);
940 if (FAILED(r))
942 WARN("InitializeSecurityContext failed with error 0x%08lx\n", r);
943 goto failed;
946 TRACE("r = 0x%08lx, attr = 0x%08lx\n", r, conn->attr);
947 continue_needed = ((r == SEC_I_CONTINUE_NEEDED) ||
948 (r == SEC_I_COMPLETE_AND_CONTINUE));
950 if ((r == SEC_I_COMPLETE_NEEDED) || (r == SEC_I_COMPLETE_AND_CONTINUE))
952 TRACE("complete needed\n");
953 r = CompleteAuthToken(&conn->ctx, &out_desc);
954 if (FAILED(r))
956 WARN("CompleteAuthToken failed with error 0x%08lx\n", r);
957 goto failed;
961 TRACE("cbBuffer = %ld\n", out.cbBuffer);
963 if (!continue_needed)
965 r = QueryContextAttributesA(&conn->ctx, SECPKG_ATTR_SIZES, &secctx_sizes);
966 if (FAILED(r))
968 WARN("QueryContextAttributes failed with error 0x%08lx\n", r);
969 goto failed;
971 conn->signature_auth_len = secctx_sizes.cbMaxSignature;
972 conn->encryption_auth_len = secctx_sizes.cbSecurityTrailer;
975 *out_size = out.cbBuffer;
976 return RPC_S_OK;
978 failed:
979 *out_size = 0;
980 return ERROR_ACCESS_DENIED; /* FIXME: is this correct? */
983 /***********************************************************************
984 * RPCRT4_ClientConnectionAuth (internal)
986 RPC_STATUS RPCRT4_ClientConnectionAuth(RpcConnection* conn, BYTE *challenge,
987 ULONG count)
989 RpcPktHdr *resp_hdr;
990 RPC_STATUS status;
991 unsigned char *out_buffer;
992 unsigned int out_len = 0;
994 TRACE("challenge %s, %ld bytes\n", challenge, count);
996 status = rpcrt4_conn_authorize(conn, FALSE, challenge, count, NULL, &out_len);
997 if (status) return status;
998 out_buffer = malloc(out_len);
999 if (!out_buffer) return RPC_S_OUT_OF_RESOURCES;
1000 status = rpcrt4_conn_authorize(conn, FALSE, challenge, count, out_buffer, &out_len);
1001 if (status) return status;
1003 resp_hdr = RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION);
1005 if (resp_hdr)
1006 status = RPCRT4_SendWithAuth(conn, resp_hdr, NULL, 0, out_buffer, out_len);
1007 else
1008 status = RPC_S_OUT_OF_RESOURCES;
1010 free(out_buffer);
1011 free(resp_hdr);
1013 return status;
1016 /***********************************************************************
1017 * RPCRT4_ServerConnectionAuth (internal)
1019 RPC_STATUS RPCRT4_ServerConnectionAuth(RpcConnection* conn,
1020 BOOL start,
1021 RpcAuthVerifier *auth_data_in,
1022 ULONG auth_length_in,
1023 unsigned char **auth_data_out,
1024 ULONG *auth_length_out)
1026 unsigned char *out_buffer;
1027 unsigned int out_size;
1028 RPC_STATUS status;
1030 if (start)
1032 /* remove any existing authentication information */
1033 if (conn->AuthInfo)
1035 RpcAuthInfo_Release(conn->AuthInfo);
1036 conn->AuthInfo = NULL;
1038 if (SecIsValidHandle(&conn->ctx))
1040 DeleteSecurityContext(&conn->ctx);
1041 SecInvalidateHandle(&conn->ctx);
1043 if (auth_length_in >= sizeof(RpcAuthVerifier))
1045 CredHandle cred;
1046 TimeStamp exp;
1047 ULONG max_token;
1049 status = RPCRT4_ServerGetRegisteredAuthInfo(
1050 auth_data_in->auth_type, &cred, &exp, &max_token);
1051 if (status != RPC_S_OK)
1053 ERR("unknown authentication service %u\n", auth_data_in->auth_type);
1054 return status;
1057 status = RpcAuthInfo_Create(auth_data_in->auth_level,
1058 auth_data_in->auth_type, cred, exp,
1059 max_token, NULL, &conn->AuthInfo);
1060 if (status != RPC_S_OK)
1062 FreeCredentialsHandle(&cred);
1063 return status;
1066 /* FIXME: should auth_data_in->auth_context_id be checked in the !start case? */
1067 conn->auth_context_id = auth_data_in->auth_context_id;
1071 if (auth_length_in < sizeof(RpcAuthVerifier))
1072 return RPC_S_OK;
1074 if (!conn->AuthInfo)
1075 /* should have filled in authentication info by now */
1076 return RPC_S_PROTOCOL_ERROR;
1078 status = rpcrt4_conn_authorize(
1079 conn, start, (unsigned char *)(auth_data_in + 1),
1080 auth_length_in - sizeof(RpcAuthVerifier), NULL, &out_size);
1081 if (status) return status;
1083 out_buffer = malloc(out_size);
1084 if (!out_buffer) return RPC_S_OUT_OF_RESOURCES;
1086 status = rpcrt4_conn_authorize(
1087 conn, start, (unsigned char *)(auth_data_in + 1),
1088 auth_length_in - sizeof(RpcAuthVerifier), out_buffer, &out_size);
1089 if (status != RPC_S_OK)
1091 free(out_buffer);
1092 return status;
1095 if (out_size && !auth_length_out)
1097 ERR("expected authentication to be complete but SSP returned data of "
1098 "%u bytes to be sent back to client\n", out_size);
1099 free(out_buffer);
1100 return RPC_S_SEC_PKG_ERROR;
1102 else
1104 *auth_data_out = out_buffer;
1105 *auth_length_out = out_size;
1108 return status;
1111 /***********************************************************************
1112 * RPCRT4_default_is_authorized (internal)
1114 * Has a connection started the process of authorizing with the server?
1116 BOOL RPCRT4_default_is_authorized(RpcConnection *Connection)
1118 return Connection->AuthInfo && SecIsValidHandle(&Connection->ctx);
1121 /***********************************************************************
1122 * RPCRT4_default_impersonate_client (internal)
1125 RPC_STATUS RPCRT4_default_impersonate_client(RpcConnection *conn)
1127 SECURITY_STATUS sec_status;
1129 TRACE("(%p)\n", conn);
1131 if (!conn->AuthInfo || !SecIsValidHandle(&conn->ctx))
1132 return RPC_S_NO_CONTEXT_AVAILABLE;
1133 sec_status = ImpersonateSecurityContext(&conn->ctx);
1134 if (sec_status != SEC_E_OK)
1135 WARN("ImpersonateSecurityContext returned 0x%08lx\n", sec_status);
1136 switch (sec_status)
1138 case SEC_E_UNSUPPORTED_FUNCTION:
1139 return RPC_S_CANNOT_SUPPORT;
1140 case SEC_E_NO_IMPERSONATION:
1141 return RPC_S_NO_CONTEXT_AVAILABLE;
1142 case SEC_E_OK:
1143 return RPC_S_OK;
1144 default:
1145 return RPC_S_SEC_PKG_ERROR;
1149 /***********************************************************************
1150 * RPCRT4_default_revert_to_self (internal)
1153 RPC_STATUS RPCRT4_default_revert_to_self(RpcConnection *conn)
1155 SECURITY_STATUS sec_status;
1157 TRACE("(%p)\n", conn);
1159 if (!conn->AuthInfo || !SecIsValidHandle(&conn->ctx))
1160 return RPC_S_NO_CONTEXT_AVAILABLE;
1161 sec_status = RevertSecurityContext(&conn->ctx);
1162 if (sec_status != SEC_E_OK)
1163 WARN("RevertSecurityContext returned 0x%08lx\n", sec_status);
1164 switch (sec_status)
1166 case SEC_E_UNSUPPORTED_FUNCTION:
1167 return RPC_S_CANNOT_SUPPORT;
1168 case SEC_E_NO_IMPERSONATION:
1169 return RPC_S_NO_CONTEXT_AVAILABLE;
1170 case SEC_E_OK:
1171 return RPC_S_OK;
1172 default:
1173 return RPC_S_SEC_PKG_ERROR;
1177 /***********************************************************************
1178 * RPCRT4_default_inquire_auth_client (internal)
1180 * Default function to retrieve the authentication details that the client
1181 * is using to call the server.
1183 RPC_STATUS RPCRT4_default_inquire_auth_client(
1184 RpcConnection *conn, RPC_AUTHZ_HANDLE *privs, RPC_WSTR *server_princ_name,
1185 ULONG *authn_level, ULONG *authn_svc, ULONG *authz_svc, ULONG flags)
1187 if (!conn->AuthInfo) return RPC_S_BINDING_HAS_NO_AUTH;
1189 if (privs)
1191 FIXME("privs not implemented\n");
1192 *privs = NULL;
1194 if (server_princ_name)
1196 *server_princ_name = wcsdup(conn->AuthInfo->server_principal_name);
1197 if (!*server_princ_name) return ERROR_OUTOFMEMORY;
1199 if (authn_level) *authn_level = conn->AuthInfo->AuthnLevel;
1200 if (authn_svc) *authn_svc = conn->AuthInfo->AuthnSvc;
1201 if (authz_svc)
1203 FIXME("authorization service not implemented\n");
1204 *authz_svc = RPC_C_AUTHZ_NONE;
1206 if (flags)
1207 FIXME("flags 0x%lx not implemented\n", flags);
1209 return RPC_S_OK;
1212 /***********************************************************************
1213 * RPCRT4_Send (internal)
1215 * Transmit a packet over connection in acceptable fragments.
1217 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
1218 void *Buffer, unsigned int BufferLength)
1220 RPC_STATUS r;
1222 if (packet_does_auth_negotiation(Header) &&
1223 Connection->AuthInfo &&
1224 !rpcrt4_conn_is_authorized(Connection))
1226 unsigned int out_size = 0;
1227 unsigned char *out_buffer;
1229 r = rpcrt4_conn_authorize(Connection, TRUE, NULL, 0, NULL, &out_size);
1230 if (r != RPC_S_OK) return r;
1232 out_buffer = malloc(out_size);
1233 if (!out_buffer) return RPC_S_OUT_OF_RESOURCES;
1235 /* tack on a negotiate packet */
1236 r = rpcrt4_conn_authorize(Connection, TRUE, NULL, 0, out_buffer, &out_size);
1237 if (r == RPC_S_OK)
1238 r = RPCRT4_SendWithAuth(Connection, Header, Buffer, BufferLength, out_buffer, out_size);
1240 free(out_buffer);
1242 else
1243 r = RPCRT4_SendWithAuth(Connection, Header, Buffer, BufferLength, NULL, 0);
1245 return r;
1248 /* validates version and frag_len fields */
1249 RPC_STATUS RPCRT4_ValidateCommonHeader(const RpcPktCommonHdr *hdr)
1251 DWORD hdr_length;
1253 /* verify if the header really makes sense */
1254 if (hdr->rpc_ver != RPC_VER_MAJOR ||
1255 hdr->rpc_ver_minor != RPC_VER_MINOR)
1257 WARN("unhandled packet version\n");
1258 return RPC_S_PROTOCOL_ERROR;
1261 hdr_length = RPCRT4_GetHeaderSize((const RpcPktHdr*)hdr);
1262 if (hdr_length == 0)
1264 WARN("header length == 0\n");
1265 return RPC_S_PROTOCOL_ERROR;
1268 if (hdr->frag_len < hdr_length)
1270 WARN("bad frag length %d\n", hdr->frag_len);
1271 return RPC_S_PROTOCOL_ERROR;
1274 return RPC_S_OK;
1277 /***********************************************************************
1278 * RPCRT4_default_receive_fragment (internal)
1280 * Receive a fragment from a connection.
1282 static RPC_STATUS RPCRT4_default_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
1284 RPC_STATUS status;
1285 DWORD hdr_length;
1286 LONG dwRead;
1287 RpcPktCommonHdr common_hdr;
1289 *Header = NULL;
1290 *Payload = NULL;
1292 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
1294 /* read packet common header */
1295 dwRead = rpcrt4_conn_read(Connection, &common_hdr, sizeof(common_hdr));
1296 if (dwRead != sizeof(common_hdr)) {
1297 WARN("Short read of header, %ld bytes\n", dwRead);
1298 status = RPC_S_CALL_FAILED;
1299 goto fail;
1302 status = RPCRT4_ValidateCommonHeader(&common_hdr);
1303 if (status != RPC_S_OK) goto fail;
1305 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
1306 if (hdr_length == 0) {
1307 WARN("header length == 0\n");
1308 status = RPC_S_PROTOCOL_ERROR;
1309 goto fail;
1312 *Header = malloc(hdr_length);
1313 memcpy(*Header, &common_hdr, sizeof(common_hdr));
1315 /* read the rest of packet header */
1316 dwRead = rpcrt4_conn_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
1317 if (dwRead != hdr_length - sizeof(common_hdr)) {
1318 WARN("bad header length, %ld bytes, hdr_length %ld\n", dwRead, hdr_length);
1319 status = RPC_S_CALL_FAILED;
1320 goto fail;
1323 if (common_hdr.frag_len - hdr_length)
1325 *Payload = malloc(common_hdr.frag_len - hdr_length);
1326 if (!*Payload)
1328 status = RPC_S_OUT_OF_RESOURCES;
1329 goto fail;
1332 dwRead = rpcrt4_conn_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
1333 if (dwRead != common_hdr.frag_len - hdr_length)
1335 WARN("bad data length, %ld/%ld\n", dwRead, common_hdr.frag_len - hdr_length);
1336 status = RPC_S_CALL_FAILED;
1337 goto fail;
1340 else
1341 *Payload = NULL;
1343 /* success */
1344 status = RPC_S_OK;
1346 fail:
1347 if (status != RPC_S_OK) {
1348 free(*Header);
1349 *Header = NULL;
1350 free(*Payload);
1351 *Payload = NULL;
1353 return status;
1356 static RPC_STATUS RPCRT4_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
1358 if (Connection->ops->receive_fragment)
1359 return Connection->ops->receive_fragment(Connection, Header, Payload);
1360 else
1361 return RPCRT4_default_receive_fragment(Connection, Header, Payload);
1364 /***********************************************************************
1365 * RPCRT4_ReceiveWithAuth (internal)
1367 * Receive a packet from connection, merge the fragments and return the auth
1368 * data.
1370 RPC_STATUS RPCRT4_ReceiveWithAuth(RpcConnection *Connection, RpcPktHdr **Header,
1371 PRPC_MESSAGE pMsg,
1372 unsigned char **auth_data_out,
1373 ULONG *auth_length_out)
1375 RPC_STATUS status;
1376 DWORD hdr_length;
1377 unsigned short first_flag;
1378 ULONG data_length;
1379 ULONG buffer_length;
1380 ULONG auth_length = 0;
1381 unsigned char *auth_data = NULL;
1382 RpcPktHdr *CurrentHeader = NULL;
1383 void *payload = NULL;
1385 *Header = NULL;
1386 pMsg->Buffer = NULL;
1387 if (auth_data_out) *auth_data_out = NULL;
1388 if (auth_length_out) *auth_length_out = 0;
1390 TRACE("(%p, %p, %p, %p)\n", Connection, Header, pMsg, auth_data_out);
1392 RPCRT4_SetThreadCurrentConnection(Connection);
1394 status = RPCRT4_receive_fragment(Connection, Header, &payload);
1395 if (status != RPC_S_OK) goto fail;
1397 hdr_length = RPCRT4_GetHeaderSize(*Header);
1399 /* read packet body */
1400 switch ((*Header)->common.ptype) {
1401 case PKT_RESPONSE:
1402 pMsg->BufferLength = (*Header)->response.alloc_hint;
1403 break;
1404 case PKT_REQUEST:
1405 pMsg->BufferLength = (*Header)->request.alloc_hint;
1406 break;
1407 default:
1408 pMsg->BufferLength = (*Header)->common.frag_len - hdr_length - RPC_AUTH_VERIFIER_LEN(&(*Header)->common);
1411 TRACE("buffer length = %u\n", pMsg->BufferLength);
1413 pMsg->Buffer = I_RpcAllocate(pMsg->BufferLength);
1414 if (!pMsg->Buffer)
1416 status = ERROR_OUTOFMEMORY;
1417 goto fail;
1420 first_flag = RPC_FLG_FIRST;
1421 auth_length = (*Header)->common.auth_len;
1422 if (auth_length) {
1423 auth_data = malloc(RPC_AUTH_VERIFIER_LEN(&(*Header)->common));
1424 if (!auth_data) {
1425 status = RPC_S_OUT_OF_RESOURCES;
1426 goto fail;
1429 CurrentHeader = *Header;
1430 buffer_length = 0;
1431 while (TRUE)
1433 unsigned int header_auth_len = RPC_AUTH_VERIFIER_LEN(&CurrentHeader->common);
1435 /* verify header fields */
1437 if ((CurrentHeader->common.frag_len < hdr_length) ||
1438 (CurrentHeader->common.frag_len - hdr_length < header_auth_len)) {
1439 WARN("frag_len %d too small for hdr_length %ld and auth_len %d\n",
1440 CurrentHeader->common.frag_len, hdr_length, CurrentHeader->common.auth_len);
1441 status = RPC_S_PROTOCOL_ERROR;
1442 goto fail;
1445 if (CurrentHeader->common.auth_len != auth_length) {
1446 WARN("auth_len header field changed from %ld to %d\n",
1447 auth_length, CurrentHeader->common.auth_len);
1448 status = RPC_S_PROTOCOL_ERROR;
1449 goto fail;
1452 if ((CurrentHeader->common.flags & RPC_FLG_FIRST) != first_flag) {
1453 TRACE("invalid packet flags\n");
1454 status = RPC_S_PROTOCOL_ERROR;
1455 goto fail;
1458 data_length = CurrentHeader->common.frag_len - hdr_length - header_auth_len;
1459 if (data_length + buffer_length > pMsg->BufferLength) {
1460 TRACE("allocation hint exceeded, new buffer length = %ld\n",
1461 data_length + buffer_length);
1462 pMsg->BufferLength = data_length + buffer_length;
1463 status = I_RpcReAllocateBuffer(pMsg);
1464 if (status != RPC_S_OK) goto fail;
1467 memcpy((unsigned char *)pMsg->Buffer + buffer_length, payload, data_length);
1469 if (header_auth_len) {
1470 if (header_auth_len < sizeof(RpcAuthVerifier) ||
1471 header_auth_len > RPC_AUTH_VERIFIER_LEN(&(*Header)->common)) {
1472 WARN("bad auth verifier length %d\n", header_auth_len);
1473 status = RPC_S_PROTOCOL_ERROR;
1474 goto fail;
1477 /* FIXME: we should accumulate authentication data for the bind,
1478 * bind_ack, alter_context and alter_context_response if necessary.
1479 * however, the details of how this is done is very sketchy in the
1480 * DCE/RPC spec. for all other packet types that have authentication
1481 * verifier data then it is just duplicated in all the fragments */
1482 memcpy(auth_data, (unsigned char *)payload + data_length, header_auth_len);
1484 /* these packets are handled specially, not by the generic SecurePacket
1485 * function */
1486 if (!packet_does_auth_negotiation(*Header) && rpcrt4_conn_is_authorized(Connection))
1488 status = rpcrt4_conn_secure_packet(Connection, SECURE_PACKET_RECEIVE,
1489 CurrentHeader, hdr_length,
1490 (unsigned char *)pMsg->Buffer + buffer_length, data_length,
1491 (RpcAuthVerifier *)auth_data,
1492 auth_data + sizeof(RpcAuthVerifier),
1493 header_auth_len - sizeof(RpcAuthVerifier));
1494 if (status != RPC_S_OK) goto fail;
1498 buffer_length += data_length;
1499 if (!(CurrentHeader->common.flags & RPC_FLG_LAST)) {
1500 TRACE("next header\n");
1502 if (*Header != CurrentHeader)
1504 free(CurrentHeader);
1505 CurrentHeader = NULL;
1507 free(payload);
1508 payload = NULL;
1510 status = RPCRT4_receive_fragment(Connection, &CurrentHeader, &payload);
1511 if (status != RPC_S_OK) goto fail;
1513 first_flag = 0;
1514 } else {
1515 break;
1518 pMsg->BufferLength = buffer_length;
1520 /* success */
1521 status = RPC_S_OK;
1523 fail:
1524 RPCRT4_SetThreadCurrentConnection(NULL);
1525 if (CurrentHeader != *Header)
1526 free(CurrentHeader);
1527 if (status != RPC_S_OK) {
1528 I_RpcFree(pMsg->Buffer);
1529 pMsg->Buffer = NULL;
1530 free(*Header);
1531 *Header = NULL;
1533 if (auth_data_out && status == RPC_S_OK) {
1534 *auth_length_out = auth_length;
1535 *auth_data_out = auth_data;
1537 else
1538 free(auth_data);
1539 free(payload);
1540 return status;
1543 /***********************************************************************
1544 * RPCRT4_Receive (internal)
1546 * Receive a packet from connection and merge the fragments.
1548 static RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
1549 PRPC_MESSAGE pMsg)
1551 return RPCRT4_ReceiveWithAuth(Connection, Header, pMsg, NULL, NULL);
1554 /***********************************************************************
1555 * I_RpcNegotiateTransferSyntax [RPCRT4.@]
1557 * Negotiates the transfer syntax used by a client connection by connecting
1558 * to the server.
1560 * PARAMS
1561 * pMsg [I] RPC Message structure.
1562 * pAsync [I] Asynchronous state to set.
1564 * RETURNS
1565 * Success: RPC_S_OK.
1566 * Failure: Any error code.
1568 RPC_STATUS WINAPI I_RpcNegotiateTransferSyntax(PRPC_MESSAGE pMsg)
1570 RpcBinding* bind = pMsg->Handle;
1571 RpcConnection* conn;
1572 RPC_STATUS status = RPC_S_OK;
1574 TRACE("(%p)\n", pMsg);
1576 if (!bind || bind->server)
1578 ERR("no binding\n");
1579 return RPC_S_INVALID_BINDING;
1582 /* if we already have a connection, we don't need to negotiate again */
1583 if (!pMsg->ReservedForRuntime)
1585 RPC_CLIENT_INTERFACE *cif = pMsg->RpcInterfaceInformation;
1586 if (!cif) return RPC_S_INTERFACE_NOT_FOUND;
1588 if (!bind->Endpoint || !bind->Endpoint[0])
1590 TRACE("automatically resolving partially bound binding\n");
1591 status = RpcEpResolveBinding(bind, cif);
1592 if (status != RPC_S_OK) return status;
1595 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
1596 &cif->InterfaceId, NULL);
1598 if (status == RPC_S_OK)
1600 pMsg->ReservedForRuntime = conn;
1601 RPCRT4_AddRefBinding(bind);
1605 return status;
1608 /***********************************************************************
1609 * I_RpcGetBuffer [RPCRT4.@]
1611 * Allocates a buffer for use by I_RpcSend or I_RpcSendReceive and binds to the
1612 * server interface.
1614 * PARAMS
1615 * pMsg [I/O] RPC message information.
1617 * RETURNS
1618 * Success: RPC_S_OK.
1619 * Failure: RPC_S_INVALID_BINDING if pMsg->Handle is invalid.
1620 * RPC_S_SERVER_UNAVAILABLE if unable to connect to server.
1621 * ERROR_OUTOFMEMORY if buffer allocation failed.
1623 * NOTES
1624 * The pMsg->BufferLength field determines the size of the buffer to allocate,
1625 * in bytes.
1627 * Use I_RpcFreeBuffer() to unbind from the server and free the message buffer.
1629 * SEE ALSO
1630 * I_RpcFreeBuffer(), I_RpcSend(), I_RpcReceive(), I_RpcSendReceive().
1632 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
1634 RPC_STATUS status;
1635 RpcBinding* bind = pMsg->Handle;
1637 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
1639 if (!bind)
1641 ERR("no binding\n");
1642 return RPC_S_INVALID_BINDING;
1645 pMsg->Buffer = I_RpcAllocate(pMsg->BufferLength);
1646 TRACE("Buffer=%p\n", pMsg->Buffer);
1648 if (!pMsg->Buffer)
1649 return ERROR_OUTOFMEMORY;
1651 if (!bind->server)
1653 status = I_RpcNegotiateTransferSyntax(pMsg);
1654 if (status != RPC_S_OK)
1655 I_RpcFree(pMsg->Buffer);
1657 else
1658 status = RPC_S_OK;
1660 return status;
1663 /***********************************************************************
1664 * I_RpcReAllocateBuffer (internal)
1666 static RPC_STATUS I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg)
1668 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
1669 pMsg->Buffer = realloc(pMsg->Buffer, pMsg->BufferLength);
1671 TRACE("Buffer=%p\n", pMsg->Buffer);
1672 return pMsg->Buffer ? RPC_S_OK : ERROR_OUTOFMEMORY;
1675 /***********************************************************************
1676 * I_RpcFreeBuffer [RPCRT4.@]
1678 * Frees a buffer allocated by I_RpcGetBuffer or I_RpcReceive and unbinds from
1679 * the server interface.
1681 * PARAMS
1682 * pMsg [I/O] RPC message information.
1684 * RETURNS
1685 * RPC_S_OK.
1687 * SEE ALSO
1688 * I_RpcGetBuffer(), I_RpcReceive().
1690 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
1692 RpcBinding* bind = pMsg->Handle;
1694 TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
1696 if (!bind)
1698 ERR("no binding\n");
1699 return RPC_S_INVALID_BINDING;
1702 if (pMsg->ReservedForRuntime)
1704 RpcConnection *conn = pMsg->ReservedForRuntime;
1705 RPCRT4_CloseBinding(bind, conn);
1706 RPCRT4_ReleaseBinding(bind);
1707 pMsg->ReservedForRuntime = NULL;
1709 I_RpcFree(pMsg->Buffer);
1710 return RPC_S_OK;
1713 static void CALLBACK async_apc_notifier_proc(ULONG_PTR ulParam)
1715 RPC_ASYNC_STATE *state = (RPC_ASYNC_STATE *)ulParam;
1716 state->u.APC.NotificationRoutine(state, NULL, state->Event);
1719 static DWORD WINAPI async_notifier_proc(LPVOID p)
1721 RpcConnection *conn = p;
1722 RPC_ASYNC_STATE *state = conn->async_state;
1724 if (state && conn->ops->wait_for_incoming_data(conn) != -1)
1726 state->Event = RpcCallComplete;
1727 switch (state->NotificationType)
1729 case RpcNotificationTypeEvent:
1730 TRACE("RpcNotificationTypeEvent %p\n", state->u.hEvent);
1731 SetEvent(state->u.hEvent);
1732 break;
1733 case RpcNotificationTypeApc:
1734 TRACE("RpcNotificationTypeApc %p\n", state->u.APC.hThread);
1735 QueueUserAPC(async_apc_notifier_proc, state->u.APC.hThread, (ULONG_PTR)state);
1736 break;
1737 case RpcNotificationTypeIoc:
1738 TRACE("RpcNotificationTypeIoc %p, 0x%lx, 0x%Ix, %p\n",
1739 state->u.IOC.hIOPort, state->u.IOC.dwNumberOfBytesTransferred,
1740 state->u.IOC.dwCompletionKey, state->u.IOC.lpOverlapped);
1741 PostQueuedCompletionStatus(state->u.IOC.hIOPort,
1742 state->u.IOC.dwNumberOfBytesTransferred,
1743 state->u.IOC.dwCompletionKey,
1744 state->u.IOC.lpOverlapped);
1745 break;
1746 case RpcNotificationTypeHwnd:
1747 TRACE("RpcNotificationTypeHwnd %p 0x%x\n", state->u.HWND.hWnd,
1748 state->u.HWND.Msg);
1749 PostMessageW(state->u.HWND.hWnd, state->u.HWND.Msg, 0, 0);
1750 break;
1751 case RpcNotificationTypeCallback:
1752 TRACE("RpcNotificationTypeCallback %p\n", state->u.NotificationRoutine);
1753 state->u.NotificationRoutine(state, NULL, state->Event);
1754 break;
1755 case RpcNotificationTypeNone:
1756 TRACE("RpcNotificationTypeNone\n");
1757 break;
1758 default:
1759 FIXME("unknown NotificationType: %d/0x%x\n", state->NotificationType, state->NotificationType);
1760 break;
1764 return 0;
1767 /***********************************************************************
1768 * I_RpcSend [RPCRT4.@]
1770 * Sends a message to the server.
1772 * PARAMS
1773 * pMsg [I/O] RPC message information.
1775 * RETURNS
1776 * Unknown.
1778 * NOTES
1779 * The buffer must have been allocated with I_RpcGetBuffer().
1781 * SEE ALSO
1782 * I_RpcGetBuffer(), I_RpcReceive(), I_RpcSendReceive().
1784 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
1786 RpcBinding* bind = pMsg->Handle;
1787 RPC_CLIENT_INTERFACE *cif;
1788 RpcConnection* conn;
1789 RPC_STATUS status;
1790 RpcPktHdr *hdr;
1791 BOOL from_cache = TRUE;
1793 TRACE("(%p)\n", pMsg);
1794 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1796 for (;;)
1798 conn = pMsg->ReservedForRuntime;
1799 hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
1800 pMsg->BufferLength,
1801 pMsg->ProcNum & ~RPC_FLAGS_VALID_BIT,
1802 &bind->ObjectUuid);
1803 if (!hdr)
1804 return ERROR_OUTOFMEMORY;
1806 hdr->common.call_id = conn->NextCallId++;
1807 status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
1808 free(hdr);
1809 if (status == RPC_S_OK || conn->server || !from_cache)
1810 break;
1812 WARN("Send failed, trying to reconnect\n");
1813 cif = pMsg->RpcInterfaceInformation;
1814 RPCRT4_ReleaseConnection(conn);
1815 pMsg->ReservedForRuntime = NULL;
1816 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax, &cif->InterfaceId, &from_cache);
1817 if (status != RPC_S_OK) break;
1818 pMsg->ReservedForRuntime = conn;
1821 if (status == RPC_S_OK && pMsg->RpcFlags & RPC_BUFFER_ASYNC)
1823 if (!QueueUserWorkItem(async_notifier_proc, conn, WT_EXECUTEDEFAULT | WT_EXECUTELONGFUNCTION))
1824 status = RPC_S_OUT_OF_RESOURCES;
1827 return status;
1830 /* is this status something that the server can't recover from? */
1831 static inline BOOL is_hard_error(RPC_STATUS status)
1833 switch (status)
1835 case 0: /* user-defined fault */
1836 case ERROR_ACCESS_DENIED:
1837 case ERROR_INVALID_PARAMETER:
1838 case RPC_S_PROTOCOL_ERROR:
1839 case RPC_S_CALL_FAILED:
1840 case RPC_S_CALL_FAILED_DNE:
1841 case RPC_S_SEC_PKG_ERROR:
1842 return TRUE;
1843 default:
1844 return FALSE;
1848 /***********************************************************************
1849 * I_RpcReceive [RPCRT4.@]
1851 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
1853 RpcBinding* bind = pMsg->Handle;
1854 RPC_STATUS status;
1855 RpcPktHdr *hdr = NULL;
1856 RpcConnection *conn;
1858 TRACE("(%p)\n", pMsg);
1859 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1861 conn = pMsg->ReservedForRuntime;
1862 status = RPCRT4_Receive(conn, &hdr, pMsg);
1863 if (status != RPC_S_OK) {
1864 WARN("receive failed with error %lx\n", status);
1865 goto fail;
1868 switch (hdr->common.ptype) {
1869 case PKT_RESPONSE:
1870 break;
1871 case PKT_FAULT:
1872 ERR ("we got fault packet with status 0x%x\n", hdr->fault.status);
1873 status = NCA2RPC_STATUS(hdr->fault.status);
1874 if (is_hard_error(status))
1875 goto fail;
1876 break;
1877 default:
1878 WARN("bad packet type %d\n", hdr->common.ptype);
1879 status = RPC_S_PROTOCOL_ERROR;
1880 goto fail;
1883 /* success */
1884 free(hdr);
1885 return status;
1887 fail:
1888 free(hdr);
1889 RPCRT4_ReleaseConnection(conn);
1890 pMsg->ReservedForRuntime = NULL;
1891 return status;
1894 /***********************************************************************
1895 * I_RpcSendReceive [RPCRT4.@]
1897 * Sends a message to the server and receives the response.
1899 * PARAMS
1900 * pMsg [I/O] RPC message information.
1902 * RETURNS
1903 * Success: RPC_S_OK.
1904 * Failure: Any error code.
1906 * NOTES
1907 * The buffer must have been allocated with I_RpcGetBuffer().
1909 * SEE ALSO
1910 * I_RpcGetBuffer(), I_RpcSend(), I_RpcReceive().
1912 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
1914 RPC_STATUS status;
1915 void *original_buffer;
1917 TRACE("(%p)\n", pMsg);
1919 original_buffer = pMsg->Buffer;
1920 status = I_RpcSend(pMsg);
1921 if (status == RPC_S_OK)
1922 status = I_RpcReceive(pMsg);
1923 /* free the buffer replaced by a new buffer in I_RpcReceive */
1924 if (status == RPC_S_OK)
1925 I_RpcFree(original_buffer);
1926 return status;
1929 /***********************************************************************
1930 * I_RpcAsyncSetHandle [RPCRT4.@]
1932 * Sets the asynchronous state of the handle contained in the RPC message
1933 * structure.
1935 * PARAMS
1936 * pMsg [I] RPC Message structure.
1937 * pAsync [I] Asynchronous state to set.
1939 * RETURNS
1940 * Success: RPC_S_OK.
1941 * Failure: Any error code.
1943 RPC_STATUS WINAPI I_RpcAsyncSetHandle(PRPC_MESSAGE pMsg, PRPC_ASYNC_STATE pAsync)
1945 RpcBinding* bind = pMsg->Handle;
1946 RpcConnection *conn;
1948 TRACE("(%p, %p)\n", pMsg, pAsync);
1950 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1952 conn = pMsg->ReservedForRuntime;
1953 conn->async_state = pAsync;
1955 return RPC_S_OK;
1958 /***********************************************************************
1959 * I_RpcAsyncAbortCall [RPCRT4.@]
1961 * Aborts an asynchronous call.
1963 * PARAMS
1964 * pAsync [I] Asynchronous state.
1965 * ExceptionCode [I] Exception code.
1967 * RETURNS
1968 * Success: RPC_S_OK.
1969 * Failure: Any error code.
1971 RPC_STATUS WINAPI I_RpcAsyncAbortCall(PRPC_ASYNC_STATE pAsync, ULONG ExceptionCode)
1973 FIXME("(%p, %ld): stub\n", pAsync, ExceptionCode);
1974 return RPC_S_INVALID_ASYNC_HANDLE;