4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006 CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
38 #include "rpc_binding.h"
40 #include "rpc_message.h"
41 #include "ncastatus.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
45 /* note: the DCE/RPC spec says the alignment amount should be 4, but
46 * MS/RPC servers seem to always use 16 */
47 #define AUTH_ALIGNMENT 16
49 /* gets the amount needed to round a value up to the specified alignment */
50 #define ROUND_UP_AMOUNT(value, alignment) \
51 (((alignment) - (((value) % (alignment)))) % (alignment))
52 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
54 enum secure_packet_direction
60 static RPC_STATUS I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg);
62 DWORD RPCRT4_GetHeaderSize(const RpcPktHdr *Header)
64 static const DWORD header_sizes[] = {
65 sizeof(Header->request), 0, sizeof(Header->response),
66 sizeof(Header->fault), 0, 0, 0, 0, 0, 0, 0, sizeof(Header->bind),
67 sizeof(Header->bind_ack), sizeof(Header->bind_nack),
68 0, 0, 0, 0, 0, 0, sizeof(Header->http)
72 if (Header->common.ptype < sizeof(header_sizes) / sizeof(header_sizes[0])) {
73 ret = header_sizes[Header->common.ptype];
75 FIXME("unhandled packet type\n");
76 if (Header->common.flags & RPC_FLG_OBJECT_UUID)
79 WARN("invalid packet type %u\n", Header->common.ptype);
85 static int packet_has_body(const RpcPktHdr *Header)
87 return (Header->common.ptype == PKT_FAULT) ||
88 (Header->common.ptype == PKT_REQUEST) ||
89 (Header->common.ptype == PKT_RESPONSE);
92 static int packet_has_auth_verifier(const RpcPktHdr *Header)
94 return !(Header->common.ptype == PKT_BIND_NACK) &&
95 !(Header->common.ptype == PKT_SHUTDOWN);
98 static VOID RPCRT4_BuildCommonHeader(RpcPktHdr *Header, unsigned char PacketType,
99 ULONG DataRepresentation)
101 Header->common.rpc_ver = RPC_VER_MAJOR;
102 Header->common.rpc_ver_minor = RPC_VER_MINOR;
103 Header->common.ptype = PacketType;
104 Header->common.drep[0] = LOBYTE(LOWORD(DataRepresentation));
105 Header->common.drep[1] = HIBYTE(LOWORD(DataRepresentation));
106 Header->common.drep[2] = LOBYTE(HIWORD(DataRepresentation));
107 Header->common.drep[3] = HIBYTE(HIWORD(DataRepresentation));
108 Header->common.auth_len = 0;
109 Header->common.call_id = 1;
110 Header->common.flags = 0;
111 /* Flags and fragment length are computed in RPCRT4_Send. */
114 static RpcPktHdr *RPCRT4_BuildRequestHeader(ULONG DataRepresentation,
116 unsigned short ProcNum,
123 has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
124 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
125 sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
126 if (header == NULL) {
130 RPCRT4_BuildCommonHeader(header, PKT_REQUEST, DataRepresentation);
131 header->common.frag_len = sizeof(header->request);
132 header->request.alloc_hint = BufferLength;
133 header->request.context_id = 0;
134 header->request.opnum = ProcNum;
136 header->common.flags |= RPC_FLG_OBJECT_UUID;
137 header->common.frag_len += sizeof(UUID);
138 memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
144 RpcPktHdr *RPCRT4_BuildResponseHeader(ULONG DataRepresentation, ULONG BufferLength)
148 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->response));
149 if (header == NULL) {
153 RPCRT4_BuildCommonHeader(header, PKT_RESPONSE, DataRepresentation);
154 header->common.frag_len = sizeof(header->response);
155 header->response.alloc_hint = BufferLength;
160 RpcPktHdr *RPCRT4_BuildFaultHeader(ULONG DataRepresentation, RPC_STATUS Status)
164 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->fault));
165 if (header == NULL) {
169 RPCRT4_BuildCommonHeader(header, PKT_FAULT, DataRepresentation);
170 header->common.frag_len = sizeof(header->fault);
171 header->fault.status = Status;
176 RpcPktHdr *RPCRT4_BuildBindHeader(ULONG DataRepresentation,
177 unsigned short MaxTransmissionSize,
178 unsigned short MaxReceiveSize,
180 const RPC_SYNTAX_IDENTIFIER *AbstractId,
181 const RPC_SYNTAX_IDENTIFIER *TransferId)
184 RpcContextElement *ctxt_elem;
186 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
187 sizeof(header->bind) + FIELD_OFFSET(RpcContextElement, transfer_syntaxes[1]));
188 if (header == NULL) {
191 ctxt_elem = (RpcContextElement *)(&header->bind + 1);
193 RPCRT4_BuildCommonHeader(header, PKT_BIND, DataRepresentation);
194 header->common.frag_len = sizeof(header->bind) + FIELD_OFFSET(RpcContextElement, transfer_syntaxes[1]);
195 header->bind.max_tsize = MaxTransmissionSize;
196 header->bind.max_rsize = MaxReceiveSize;
197 header->bind.assoc_gid = AssocGroupId;
198 header->bind.num_elements = 1;
199 ctxt_elem->num_syntaxes = 1;
200 ctxt_elem->abstract_syntax = *AbstractId;
201 ctxt_elem->transfer_syntaxes[0] = *TransferId;
206 static RpcPktHdr *RPCRT4_BuildAuthHeader(ULONG DataRepresentation)
210 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
211 sizeof(header->common) + 12);
215 RPCRT4_BuildCommonHeader(header, PKT_AUTH3, DataRepresentation);
216 header->common.frag_len = 0x14;
217 header->common.auth_len = 0;
222 RpcPktHdr *RPCRT4_BuildBindNackHeader(ULONG DataRepresentation,
223 unsigned char RpcVersion,
224 unsigned char RpcVersionMinor,
225 unsigned short RejectReason)
229 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(RpcPktHdr, bind_nack.protocols[1]));
230 if (header == NULL) {
234 RPCRT4_BuildCommonHeader(header, PKT_BIND_NACK, DataRepresentation);
235 header->common.frag_len = FIELD_OFFSET(RpcPktHdr, bind_nack.protocols[1]);
236 header->bind_nack.reject_reason = RejectReason;
237 header->bind_nack.protocols_count = 1;
238 header->bind_nack.protocols[0].rpc_ver = RpcVersion;
239 header->bind_nack.protocols[0].rpc_ver_minor = RpcVersionMinor;
244 RpcPktHdr *RPCRT4_BuildBindAckHeader(ULONG DataRepresentation,
245 unsigned short MaxTransmissionSize,
246 unsigned short MaxReceiveSize,
248 LPCSTR ServerAddress,
249 unsigned char ResultCount,
250 const RpcResult *Results)
254 RpcAddressString *server_address;
255 RpcResultList *results;
257 header_size = sizeof(header->bind_ack) +
258 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[strlen(ServerAddress) + 1]), 4) +
259 FIELD_OFFSET(RpcResultList, results[ResultCount]);
261 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, header_size);
262 if (header == NULL) {
266 RPCRT4_BuildCommonHeader(header, PKT_BIND_ACK, DataRepresentation);
267 header->common.frag_len = header_size;
268 header->bind_ack.max_tsize = MaxTransmissionSize;
269 header->bind_ack.max_rsize = MaxReceiveSize;
270 header->bind_ack.assoc_gid = AssocGroupId;
271 server_address = (RpcAddressString*)(&header->bind_ack + 1);
272 server_address->length = strlen(ServerAddress) + 1;
273 strcpy(server_address->string, ServerAddress);
274 /* results is 4-byte aligned */
275 results = (RpcResultList*)((ULONG_PTR)server_address + ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4));
276 results->num_results = ResultCount;
277 memcpy(&results->results[0], Results, ResultCount * sizeof(*Results));
282 RpcPktHdr *RPCRT4_BuildHttpHeader(ULONG DataRepresentation,
283 unsigned short flags,
284 unsigned short num_data_items,
285 unsigned int payload_size)
289 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->http) + payload_size);
290 if (header == NULL) {
291 ERR("failed to allocate memory\n");
295 RPCRT4_BuildCommonHeader(header, PKT_HTTP, DataRepresentation);
296 /* since the packet isn't current sent using RPCRT4_Send, set the flags
298 header->common.flags = RPC_FLG_FIRST|RPC_FLG_LAST;
299 header->common.call_id = 0;
300 header->common.frag_len = sizeof(header->http) + payload_size;
301 header->http.flags = flags;
302 header->http.num_data_items = num_data_items;
307 #define WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, type, value) \
309 *(unsigned int *)(payload) = (type); \
311 *(unsigned int *)(payload) = (value); \
315 #define WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, type, uuid) \
317 *(unsigned int *)(payload) = (type); \
319 *(UUID *)(payload) = (uuid); \
320 (payload) += sizeof(UUID); \
323 #define WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted, flow_control_increment, uuid) \
325 *(unsigned int *)(payload) = 0x00000001; \
327 *(unsigned int *)(payload) = (bytes_transmitted); \
329 *(unsigned int *)(payload) = (flow_control_increment); \
331 *(UUID *)(payload) = (uuid); \
332 (payload) += sizeof(UUID); \
335 RpcPktHdr *RPCRT4_BuildHttpConnectHeader(unsigned short flags, int out_pipe,
336 const UUID *connection_uuid,
337 const UUID *pipe_uuid,
338 const UUID *association_uuid)
344 size = 8 + 4 + sizeof(UUID) + 4 + sizeof(UUID) + 8;
346 size += 8 + 4 + sizeof(UUID);
348 header = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, flags,
349 out_pipe ? 4 : 6, size);
350 if (!header) return NULL;
351 payload = (char *)(&header->http+1);
353 /* FIXME: what does this part of the payload do? */
354 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000006, 0x00000001);
356 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x00000003, *connection_uuid);
357 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x00000003, *pipe_uuid);
360 /* FIXME: what does this part of the payload do? */
361 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000000, 0x00010000);
364 /* FIXME: what does this part of the payload do? */
365 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000004, 0x40000000);
366 /* FIXME: what does this part of the payload do? */
367 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000005, 0x000493e0);
369 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x0000000c, *association_uuid);
375 RpcPktHdr *RPCRT4_BuildHttpFlowControlHeader(BOOL server, ULONG bytes_transmitted,
376 ULONG flow_control_increment,
377 const UUID *pipe_uuid)
382 header = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x2, 2,
383 5 * sizeof(ULONG) + sizeof(UUID));
384 if (!header) return NULL;
385 payload = (char *)(&header->http+1);
387 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x0000000d, (server ? 0x0 : 0x3));
389 WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted,
390 flow_control_increment, *pipe_uuid);
394 VOID RPCRT4_FreeHeader(RpcPktHdr *Header)
396 HeapFree(GetProcessHeap(), 0, Header);
399 NCA_STATUS RPC2NCA_STATUS(RPC_STATUS status)
403 case ERROR_INVALID_HANDLE: return NCA_S_FAULT_CONTEXT_MISMATCH;
404 case ERROR_OUTOFMEMORY: return NCA_S_FAULT_REMOTE_NO_MEMORY;
405 case RPC_S_NOT_LISTENING: return NCA_S_SERVER_TOO_BUSY;
406 case RPC_S_UNKNOWN_IF: return NCA_S_UNK_IF;
407 case RPC_S_SERVER_TOO_BUSY: return NCA_S_SERVER_TOO_BUSY;
408 case RPC_S_CALL_FAILED: return NCA_S_FAULT_UNSPEC;
409 case RPC_S_CALL_FAILED_DNE: return NCA_S_MANAGER_NOT_ENTERED;
410 case RPC_S_PROTOCOL_ERROR: return NCA_S_PROTO_ERROR;
411 case RPC_S_UNSUPPORTED_TYPE: return NCA_S_UNSUPPORTED_TYPE;
412 case RPC_S_INVALID_TAG: return NCA_S_FAULT_INVALID_TAG;
413 case RPC_S_INVALID_BOUND: return NCA_S_FAULT_INVALID_BOUND;
414 case RPC_S_PROCNUM_OUT_OF_RANGE: return NCA_S_OP_RNG_ERROR;
415 case RPC_X_SS_HANDLES_MISMATCH: return NCA_S_FAULT_CONTEXT_MISMATCH;
416 case RPC_S_CALL_CANCELLED: return NCA_S_FAULT_CANCEL;
417 case RPC_S_COMM_FAILURE: return NCA_S_COMM_FAILURE;
418 case RPC_X_WRONG_PIPE_ORDER: return NCA_S_FAULT_PIPE_ORDER;
419 case RPC_X_PIPE_CLOSED: return NCA_S_FAULT_PIPE_CLOSED;
420 case RPC_X_PIPE_DISCIPLINE_ERROR: return NCA_S_FAULT_PIPE_DISCIPLINE;
421 case RPC_X_PIPE_EMPTY: return NCA_S_FAULT_PIPE_EMPTY;
422 case STATUS_FLOAT_DIVIDE_BY_ZERO: return NCA_S_FAULT_FP_DIV_ZERO;
423 case STATUS_FLOAT_INVALID_OPERATION: return NCA_S_FAULT_FP_ERROR;
424 case STATUS_FLOAT_OVERFLOW: return NCA_S_FAULT_FP_OVERFLOW;
425 case STATUS_FLOAT_UNDERFLOW: return NCA_S_FAULT_FP_UNDERFLOW;
426 case STATUS_INTEGER_DIVIDE_BY_ZERO: return NCA_S_FAULT_INT_DIV_BY_ZERO;
427 case STATUS_INTEGER_OVERFLOW: return NCA_S_FAULT_INT_OVERFLOW;
428 default: return status;
432 static RPC_STATUS NCA2RPC_STATUS(NCA_STATUS status)
436 case NCA_S_COMM_FAILURE: return RPC_S_COMM_FAILURE;
437 case NCA_S_OP_RNG_ERROR: return RPC_S_PROCNUM_OUT_OF_RANGE;
438 case NCA_S_UNK_IF: return RPC_S_UNKNOWN_IF;
439 case NCA_S_YOU_CRASHED: return RPC_S_CALL_FAILED;
440 case NCA_S_PROTO_ERROR: return RPC_S_PROTOCOL_ERROR;
441 case NCA_S_OUT_ARGS_TOO_BIG: return ERROR_NOT_ENOUGH_SERVER_MEMORY;
442 case NCA_S_SERVER_TOO_BUSY: return RPC_S_SERVER_TOO_BUSY;
443 case NCA_S_UNSUPPORTED_TYPE: return RPC_S_UNSUPPORTED_TYPE;
444 case NCA_S_FAULT_INT_DIV_BY_ZERO: return RPC_S_ZERO_DIVIDE;
445 case NCA_S_FAULT_ADDR_ERROR: return RPC_S_ADDRESS_ERROR;
446 case NCA_S_FAULT_FP_DIV_ZERO: return RPC_S_FP_DIV_ZERO;
447 case NCA_S_FAULT_FP_UNDERFLOW: return RPC_S_FP_UNDERFLOW;
448 case NCA_S_FAULT_FP_OVERFLOW: return RPC_S_FP_OVERFLOW;
449 case NCA_S_FAULT_INVALID_TAG: return RPC_S_INVALID_TAG;
450 case NCA_S_FAULT_INVALID_BOUND: return RPC_S_INVALID_BOUND;
451 case NCA_S_RPC_VERSION_MISMATCH: return RPC_S_PROTOCOL_ERROR;
452 case NCA_S_UNSPEC_REJECT: return RPC_S_CALL_FAILED_DNE;
453 case NCA_S_BAD_ACTID: return RPC_S_CALL_FAILED_DNE;
454 case NCA_S_WHO_ARE_YOU_FAILED: return RPC_S_CALL_FAILED;
455 case NCA_S_MANAGER_NOT_ENTERED: return RPC_S_CALL_FAILED_DNE;
456 case NCA_S_FAULT_CANCEL: return RPC_S_CALL_CANCELLED;
457 case NCA_S_FAULT_ILL_INST: return RPC_S_ADDRESS_ERROR;
458 case NCA_S_FAULT_FP_ERROR: return RPC_S_FP_OVERFLOW;
459 case NCA_S_FAULT_INT_OVERFLOW: return RPC_S_ADDRESS_ERROR;
460 case NCA_S_FAULT_UNSPEC: return RPC_S_CALL_FAILED;
461 case NCA_S_FAULT_PIPE_EMPTY: return RPC_X_PIPE_EMPTY;
462 case NCA_S_FAULT_PIPE_CLOSED: return RPC_X_PIPE_CLOSED;
463 case NCA_S_FAULT_PIPE_ORDER: return RPC_X_WRONG_PIPE_ORDER;
464 case NCA_S_FAULT_PIPE_DISCIPLINE: return RPC_X_PIPE_DISCIPLINE_ERROR;
465 case NCA_S_FAULT_PIPE_COMM_ERROR: return RPC_S_COMM_FAILURE;
466 case NCA_S_FAULT_PIPE_MEMORY: return ERROR_OUTOFMEMORY;
467 case NCA_S_FAULT_CONTEXT_MISMATCH: return ERROR_INVALID_HANDLE;
468 case NCA_S_FAULT_REMOTE_NO_MEMORY: return ERROR_NOT_ENOUGH_SERVER_MEMORY;
469 default: return status;
473 /* assumes the common header fields have already been validated */
474 BOOL RPCRT4_IsValidHttpPacket(RpcPktHdr *hdr, unsigned char *data,
475 unsigned short data_len)
480 for (i = 0; i < hdr->http.num_data_items; i++)
484 if (data_len < sizeof(ULONG))
489 data_len -= sizeof(ULONG);
495 if (data_len < sizeof(GUID))
498 data_len -= sizeof(GUID);
506 if (data_len < sizeof(ULONG))
509 data_len -= sizeof(ULONG);
518 FIXME("unimplemented type 0x%x\n", type);
525 /* assumes the HTTP packet has been validated */
526 static unsigned char *RPCRT4_NextHttpHeaderField(unsigned char *data)
530 type = *(ULONG *)data;
531 data += sizeof(ULONG);
537 return data + sizeof(GUID);
544 return data + sizeof(ULONG);
548 FIXME("unimplemented type 0x%x\n", type);
553 #define READ_HTTP_PAYLOAD_FIELD_TYPE(data) *(ULONG *)(data)
554 #define GET_HTTP_PAYLOAD_FIELD_DATA(data) ((data) + sizeof(ULONG))
556 /* assumes the HTTP packet has been validated */
557 RPC_STATUS RPCRT4_ParseHttpPrepareHeader1(RpcPktHdr *header,
558 unsigned char *data, ULONG *field1)
561 if (header->http.flags != 0x0)
563 ERR("invalid flags 0x%x\n", header->http.flags);
564 return RPC_S_PROTOCOL_ERROR;
566 if (header->http.num_data_items != 1)
568 ERR("invalid number of data items %d\n", header->http.num_data_items);
569 return RPC_S_PROTOCOL_ERROR;
571 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
572 if (type != 0x00000002)
574 ERR("invalid type 0x%08x\n", type);
575 return RPC_S_PROTOCOL_ERROR;
577 *field1 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
581 /* assumes the HTTP packet has been validated */
582 RPC_STATUS RPCRT4_ParseHttpPrepareHeader2(RpcPktHdr *header,
583 unsigned char *data, ULONG *field1,
584 ULONG *bytes_until_next_packet,
588 if (header->http.flags != 0x0)
590 ERR("invalid flags 0x%x\n", header->http.flags);
591 return RPC_S_PROTOCOL_ERROR;
593 if (header->http.num_data_items != 3)
595 ERR("invalid number of data items %d\n", header->http.num_data_items);
596 return RPC_S_PROTOCOL_ERROR;
599 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
600 if (type != 0x00000006)
602 ERR("invalid type for field 1: 0x%08x\n", type);
603 return RPC_S_PROTOCOL_ERROR;
605 *field1 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
606 data = RPCRT4_NextHttpHeaderField(data);
608 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
609 if (type != 0x00000000)
611 ERR("invalid type for field 2: 0x%08x\n", type);
612 return RPC_S_PROTOCOL_ERROR;
614 *bytes_until_next_packet = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
615 data = RPCRT4_NextHttpHeaderField(data);
617 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
618 if (type != 0x00000002)
620 ERR("invalid type for field 3: 0x%08x\n", type);
621 return RPC_S_PROTOCOL_ERROR;
623 *field3 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
628 RPC_STATUS RPCRT4_ParseHttpFlowControlHeader(RpcPktHdr *header,
629 unsigned char *data, BOOL server,
630 ULONG *bytes_transmitted,
631 ULONG *flow_control_increment,
635 if (header->http.flags != 0x2)
637 ERR("invalid flags 0x%x\n", header->http.flags);
638 return RPC_S_PROTOCOL_ERROR;
640 if (header->http.num_data_items != 2)
642 ERR("invalid number of data items %d\n", header->http.num_data_items);
643 return RPC_S_PROTOCOL_ERROR;
646 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
647 if (type != 0x0000000d)
649 ERR("invalid type for field 1: 0x%08x\n", type);
650 return RPC_S_PROTOCOL_ERROR;
652 if (*(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data) != (server ? 0x3 : 0x0))
654 ERR("invalid type for 0xd field data: 0x%08x\n", *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data));
655 return RPC_S_PROTOCOL_ERROR;
657 data = RPCRT4_NextHttpHeaderField(data);
659 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
660 if (type != 0x00000001)
662 ERR("invalid type for field 2: 0x%08x\n", type);
663 return RPC_S_PROTOCOL_ERROR;
665 *bytes_transmitted = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
666 *flow_control_increment = *(ULONG *)(GET_HTTP_PAYLOAD_FIELD_DATA(data) + 4);
667 *pipe_uuid = *(UUID *)(GET_HTTP_PAYLOAD_FIELD_DATA(data) + 8);
673 static RPC_STATUS RPCRT4_SecurePacket(RpcConnection *Connection,
674 enum secure_packet_direction dir,
675 RpcPktHdr *hdr, unsigned int hdr_size,
676 unsigned char *stub_data, unsigned int stub_data_size,
677 RpcAuthVerifier *auth_hdr,
678 unsigned char *auth_value, unsigned int auth_value_size)
680 SecBufferDesc message;
681 SecBuffer buffers[4];
682 SECURITY_STATUS sec_status;
684 message.ulVersion = SECBUFFER_VERSION;
685 message.cBuffers = sizeof(buffers)/sizeof(buffers[0]);
686 message.pBuffers = buffers;
688 buffers[0].cbBuffer = hdr_size;
689 buffers[0].BufferType = SECBUFFER_DATA|SECBUFFER_READONLY_WITH_CHECKSUM;
690 buffers[0].pvBuffer = hdr;
691 buffers[1].cbBuffer = stub_data_size;
692 buffers[1].BufferType = SECBUFFER_DATA;
693 buffers[1].pvBuffer = stub_data;
694 buffers[2].cbBuffer = sizeof(*auth_hdr);
695 buffers[2].BufferType = SECBUFFER_DATA|SECBUFFER_READONLY_WITH_CHECKSUM;
696 buffers[2].pvBuffer = auth_hdr;
697 buffers[3].cbBuffer = auth_value_size;
698 buffers[3].BufferType = SECBUFFER_TOKEN;
699 buffers[3].pvBuffer = auth_value;
701 if (dir == SECURE_PACKET_SEND)
703 if ((auth_hdr->auth_level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(hdr))
705 sec_status = EncryptMessage(&Connection->ctx, 0, &message, 0 /* FIXME */);
706 if (sec_status != SEC_E_OK)
708 ERR("EncryptMessage failed with 0x%08x\n", sec_status);
709 return RPC_S_SEC_PKG_ERROR;
712 else if (auth_hdr->auth_level != RPC_C_AUTHN_LEVEL_NONE)
714 sec_status = MakeSignature(&Connection->ctx, 0, &message, 0 /* FIXME */);
715 if (sec_status != SEC_E_OK)
717 ERR("MakeSignature failed with 0x%08x\n", sec_status);
718 return RPC_S_SEC_PKG_ERROR;
722 else if (dir == SECURE_PACKET_RECEIVE)
724 if ((auth_hdr->auth_level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(hdr))
726 sec_status = DecryptMessage(&Connection->ctx, &message, 0 /* FIXME */, 0);
727 if (sec_status != SEC_E_OK)
729 ERR("DecryptMessage failed with 0x%08x\n", sec_status);
730 return RPC_S_SEC_PKG_ERROR;
733 else if (auth_hdr->auth_level != RPC_C_AUTHN_LEVEL_NONE)
735 sec_status = VerifySignature(&Connection->ctx, &message, 0 /* FIXME */, NULL);
736 if (sec_status != SEC_E_OK)
738 ERR("VerifySignature failed with 0x%08x\n", sec_status);
739 return RPC_S_SEC_PKG_ERROR;
747 /***********************************************************************
748 * RPCRT4_SendWithAuth (internal)
750 * Transmit a packet with authorization data over connection in acceptable fragments.
752 static RPC_STATUS RPCRT4_SendWithAuth(RpcConnection *Connection, RpcPktHdr *Header,
753 void *Buffer, unsigned int BufferLength,
754 const void *Auth, unsigned int AuthLength)
763 RPCRT4_SetThreadCurrentConnection(Connection);
766 /* The packet building functions save the packet header size, so we can use it. */
767 hdr_size = Header->common.frag_len;
769 Header->common.auth_len = AuthLength;
770 else if (Connection->AuthInfo && packet_has_auth_verifier(Header))
772 if ((Connection->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(Header))
773 Header->common.auth_len = Connection->encryption_auth_len;
775 Header->common.auth_len = Connection->signature_auth_len;
778 Header->common.auth_len = 0;
779 Header->common.flags |= RPC_FLG_FIRST;
780 Header->common.flags &= ~RPC_FLG_LAST;
782 alen = RPC_AUTH_VERIFIER_LEN(&Header->common);
784 while (!(Header->common.flags & RPC_FLG_LAST)) {
785 unsigned char auth_pad_len = Header->common.auth_len ? ROUND_UP_AMOUNT(BufferLength, AUTH_ALIGNMENT) : 0;
786 unsigned int pkt_size = BufferLength + hdr_size + alen + auth_pad_len;
788 /* decide if we need to split the packet into fragments */
789 if (pkt_size <= Connection->MaxTransmissionSize) {
790 Header->common.flags |= RPC_FLG_LAST;
791 Header->common.frag_len = pkt_size;
794 /* make sure packet payload will be a multiple of 16 */
795 Header->common.frag_len =
796 ((Connection->MaxTransmissionSize - hdr_size - alen) & ~(AUTH_ALIGNMENT-1)) +
800 pkt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Header->common.frag_len);
802 memcpy(pkt, Header, hdr_size);
804 /* fragment consisted of header only and is the last one */
805 if (hdr_size == Header->common.frag_len)
808 memcpy(pkt + hdr_size, buffer_pos, Header->common.frag_len - hdr_size - auth_pad_len - alen);
810 /* add the authorization info */
811 if (Connection->AuthInfo && packet_has_auth_verifier(Header))
813 RpcAuthVerifier *auth_hdr = (RpcAuthVerifier *)&pkt[Header->common.frag_len - alen];
815 auth_hdr->auth_type = Connection->AuthInfo->AuthnSvc;
816 auth_hdr->auth_level = Connection->AuthInfo->AuthnLevel;
817 auth_hdr->auth_pad_length = auth_pad_len;
818 auth_hdr->auth_reserved = 0;
819 /* a unique number... */
820 auth_hdr->auth_context_id = Connection->auth_context_id;
823 memcpy(auth_hdr + 1, Auth, AuthLength);
826 status = RPCRT4_SecurePacket(Connection, SECURE_PACKET_SEND,
827 (RpcPktHdr *)pkt, hdr_size,
828 pkt + hdr_size, Header->common.frag_len - hdr_size - alen,
830 (unsigned char *)(auth_hdr + 1), Header->common.auth_len);
831 if (status != RPC_S_OK)
833 HeapFree(GetProcessHeap(), 0, pkt);
834 RPCRT4_SetThreadCurrentConnection(NULL);
841 count = rpcrt4_conn_write(Connection, pkt, Header->common.frag_len);
842 HeapFree(GetProcessHeap(), 0, pkt);
844 WARN("rpcrt4_conn_write failed (auth)\n");
845 RPCRT4_SetThreadCurrentConnection(NULL);
846 return RPC_S_CALL_FAILED;
849 buffer_pos += Header->common.frag_len - hdr_size - alen - auth_pad_len;
850 BufferLength -= Header->common.frag_len - hdr_size - alen - auth_pad_len;
851 Header->common.flags &= ~RPC_FLG_FIRST;
854 RPCRT4_SetThreadCurrentConnection(NULL);
858 /***********************************************************************
859 * RPCRT4_ClientAuthorize (internal)
861 * Authorize a client connection. A NULL in param signifies a new connection.
863 static RPC_STATUS RPCRT4_ClientAuthorize(RpcConnection *conn, SecBuffer *in,
867 SecBufferDesc out_desc;
868 SecBufferDesc inp_desc;
869 SecPkgContext_Sizes secctx_sizes;
870 BOOL continue_needed;
871 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
872 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
874 if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY)
875 context_req |= ISC_REQ_INTEGRITY;
876 else if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
877 context_req |= ISC_REQ_CONFIDENTIALITY | ISC_REQ_INTEGRITY;
879 out->BufferType = SECBUFFER_TOKEN;
880 out->cbBuffer = conn->AuthInfo->cbMaxToken;
881 out->pvBuffer = HeapAlloc(GetProcessHeap(), 0, out->cbBuffer);
882 if (!out->pvBuffer) return ERROR_OUTOFMEMORY;
884 out_desc.ulVersion = 0;
885 out_desc.cBuffers = 1;
886 out_desc.pBuffers = out;
888 inp_desc.cBuffers = 1;
889 inp_desc.pBuffers = in;
890 inp_desc.ulVersion = 0;
892 r = InitializeSecurityContextW(&conn->AuthInfo->cred, in ? &conn->ctx : NULL,
893 in ? NULL : conn->AuthInfo->server_principal_name, context_req, 0,
894 SECURITY_NETWORK_DREP, in ? &inp_desc : NULL, 0, &conn->ctx,
895 &out_desc, &conn->attr, &conn->exp);
898 WARN("InitializeSecurityContext failed with error 0x%08x\n", r);
902 TRACE("r = 0x%08x, attr = 0x%08x\n", r, conn->attr);
903 continue_needed = ((r == SEC_I_CONTINUE_NEEDED) ||
904 (r == SEC_I_COMPLETE_AND_CONTINUE));
906 if ((r == SEC_I_COMPLETE_NEEDED) || (r == SEC_I_COMPLETE_AND_CONTINUE))
908 TRACE("complete needed\n");
909 r = CompleteAuthToken(&conn->ctx, &out_desc);
912 WARN("CompleteAuthToken failed with error 0x%08x\n", r);
917 TRACE("cbBuffer = %d\n", out->cbBuffer);
919 if (!continue_needed)
921 r = QueryContextAttributesA(&conn->ctx, SECPKG_ATTR_SIZES, &secctx_sizes);
924 WARN("QueryContextAttributes failed with error 0x%08x\n", r);
927 conn->signature_auth_len = secctx_sizes.cbMaxSignature;
928 conn->encryption_auth_len = secctx_sizes.cbSecurityTrailer;
934 HeapFree(GetProcessHeap(), 0, out->pvBuffer);
935 out->pvBuffer = NULL;
936 return ERROR_ACCESS_DENIED; /* FIXME: is this correct? */
939 /***********************************************************************
940 * RPCRT4_AuthorizeBinding (internal)
942 RPC_STATUS RPCRT4_AuthorizeConnection(RpcConnection* conn, BYTE *challenge,
949 TRACE("challenge %s, %d bytes\n", challenge, count);
951 inp.BufferType = SECBUFFER_TOKEN;
952 inp.pvBuffer = challenge;
953 inp.cbBuffer = count;
955 status = RPCRT4_ClientAuthorize(conn, &inp, &out);
956 if (status) return status;
958 resp_hdr = RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION);
960 return E_OUTOFMEMORY;
962 status = RPCRT4_SendWithAuth(conn, resp_hdr, NULL, 0, out.pvBuffer, out.cbBuffer);
964 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
965 RPCRT4_FreeHeader(resp_hdr);
970 /***********************************************************************
971 * RPCRT4_Send (internal)
973 * Transmit a packet over connection in acceptable fragments.
975 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
976 void *Buffer, unsigned int BufferLength)
981 if (!Connection->AuthInfo || SecIsValidHandle(&Connection->ctx))
983 return RPCRT4_SendWithAuth(Connection, Header, Buffer, BufferLength, NULL, 0);
986 /* tack on a negotiate packet */
987 r = RPCRT4_ClientAuthorize(Connection, NULL, &out);
990 r = RPCRT4_SendWithAuth(Connection, Header, Buffer, BufferLength, out.pvBuffer, out.cbBuffer);
991 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
997 /* validates version and frag_len fields */
998 RPC_STATUS RPCRT4_ValidateCommonHeader(const RpcPktCommonHdr *hdr)
1002 /* verify if the header really makes sense */
1003 if (hdr->rpc_ver != RPC_VER_MAJOR ||
1004 hdr->rpc_ver_minor != RPC_VER_MINOR)
1006 WARN("unhandled packet version\n");
1007 return RPC_S_PROTOCOL_ERROR;
1010 hdr_length = RPCRT4_GetHeaderSize((const RpcPktHdr*)hdr);
1011 if (hdr_length == 0)
1013 WARN("header length == 0\n");
1014 return RPC_S_PROTOCOL_ERROR;
1017 if (hdr->frag_len < hdr_length)
1019 WARN("bad frag length %d\n", hdr->frag_len);
1020 return RPC_S_PROTOCOL_ERROR;
1026 /***********************************************************************
1027 * RPCRT4_default_receive_fragment (internal)
1029 * Receive a fragment from a connection.
1031 static RPC_STATUS RPCRT4_default_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
1036 RpcPktCommonHdr common_hdr;
1041 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
1043 /* read packet common header */
1044 dwRead = rpcrt4_conn_read(Connection, &common_hdr, sizeof(common_hdr));
1045 if (dwRead != sizeof(common_hdr)) {
1046 WARN("Short read of header, %d bytes\n", dwRead);
1047 status = RPC_S_CALL_FAILED;
1051 status = RPCRT4_ValidateCommonHeader(&common_hdr);
1052 if (status != RPC_S_OK) goto fail;
1054 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
1055 if (hdr_length == 0) {
1056 WARN("header length == 0\n");
1057 status = RPC_S_PROTOCOL_ERROR;
1061 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
1062 memcpy(*Header, &common_hdr, sizeof(common_hdr));
1064 /* read the rest of packet header */
1065 dwRead = rpcrt4_conn_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
1066 if (dwRead != hdr_length - sizeof(common_hdr)) {
1067 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
1068 status = RPC_S_CALL_FAILED;
1072 if (common_hdr.frag_len - hdr_length)
1074 *Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
1077 status = RPC_S_OUT_OF_RESOURCES;
1081 dwRead = rpcrt4_conn_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
1082 if (dwRead != common_hdr.frag_len - hdr_length)
1084 WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
1085 status = RPC_S_CALL_FAILED;
1096 if (status != RPC_S_OK) {
1097 RPCRT4_FreeHeader(*Header);
1099 HeapFree(GetProcessHeap(), 0, *Payload);
1105 static RPC_STATUS RPCRT4_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
1107 if (Connection->ops->receive_fragment)
1108 return Connection->ops->receive_fragment(Connection, Header, Payload);
1110 return RPCRT4_default_receive_fragment(Connection, Header, Payload);
1113 /***********************************************************************
1114 * RPCRT4_ReceiveWithAuth (internal)
1116 * Receive a packet from connection, merge the fragments and return the auth
1119 RPC_STATUS RPCRT4_ReceiveWithAuth(RpcConnection *Connection, RpcPktHdr **Header,
1121 unsigned char **auth_data_out,
1122 ULONG *auth_length_out)
1126 unsigned short first_flag;
1128 ULONG buffer_length;
1129 ULONG auth_length = 0;
1130 unsigned char *auth_data = NULL;
1131 RpcPktHdr *CurrentHeader = NULL;
1132 void *payload = NULL;
1135 pMsg->Buffer = NULL;
1137 TRACE("(%p, %p, %p, %p)\n", Connection, Header, pMsg, auth_data_out);
1139 RPCRT4_SetThreadCurrentConnection(Connection);
1141 status = RPCRT4_receive_fragment(Connection, Header, &payload);
1142 if (status != RPC_S_OK) goto fail;
1144 hdr_length = RPCRT4_GetHeaderSize(*Header);
1146 /* read packet body */
1147 switch ((*Header)->common.ptype) {
1149 pMsg->BufferLength = (*Header)->response.alloc_hint;
1152 pMsg->BufferLength = (*Header)->request.alloc_hint;
1155 pMsg->BufferLength = (*Header)->common.frag_len - hdr_length - RPC_AUTH_VERIFIER_LEN(&(*Header)->common);
1158 TRACE("buffer length = %u\n", pMsg->BufferLength);
1160 pMsg->Buffer = I_RpcAllocate(pMsg->BufferLength);
1163 status = ERROR_OUTOFMEMORY;
1167 first_flag = RPC_FLG_FIRST;
1168 auth_length = (*Header)->common.auth_len;
1170 auth_data = HeapAlloc(GetProcessHeap(), 0, RPC_AUTH_VERIFIER_LEN(&(*Header)->common));
1172 status = RPC_S_OUT_OF_RESOURCES;
1176 CurrentHeader = *Header;
1180 unsigned int header_auth_len = RPC_AUTH_VERIFIER_LEN(&CurrentHeader->common);
1182 /* verify header fields */
1184 if ((CurrentHeader->common.frag_len < hdr_length) ||
1185 (CurrentHeader->common.frag_len - hdr_length < header_auth_len)) {
1186 WARN("frag_len %d too small for hdr_length %d and auth_len %d\n",
1187 CurrentHeader->common.frag_len, hdr_length, CurrentHeader->common.auth_len);
1188 status = RPC_S_PROTOCOL_ERROR;
1192 if (CurrentHeader->common.auth_len != auth_length) {
1193 WARN("auth_len header field changed from %d to %d\n",
1194 auth_length, CurrentHeader->common.auth_len);
1195 status = RPC_S_PROTOCOL_ERROR;
1199 if ((CurrentHeader->common.flags & RPC_FLG_FIRST) != first_flag) {
1200 TRACE("invalid packet flags\n");
1201 status = RPC_S_PROTOCOL_ERROR;
1205 data_length = CurrentHeader->common.frag_len - hdr_length - header_auth_len;
1206 if (data_length + buffer_length > pMsg->BufferLength) {
1207 TRACE("allocation hint exceeded, new buffer length = %d\n",
1208 data_length + buffer_length);
1209 pMsg->BufferLength = data_length + buffer_length;
1210 status = I_RpcReAllocateBuffer(pMsg);
1211 if (status != RPC_S_OK) goto fail;
1214 memcpy((unsigned char *)pMsg->Buffer + buffer_length, payload, data_length);
1216 if (header_auth_len) {
1217 if (header_auth_len < sizeof(RpcAuthVerifier) ||
1218 header_auth_len > RPC_AUTH_VERIFIER_LEN(&(*Header)->common)) {
1219 WARN("bad auth verifier length %d\n", header_auth_len);
1220 status = RPC_S_PROTOCOL_ERROR;
1224 /* FIXME: we should accumulate authentication data for the bind,
1225 * bind_ack, alter_context and alter_context_response if necessary.
1226 * however, the details of how this is done is very sketchy in the
1227 * DCE/RPC spec. for all other packet types that have authentication
1228 * verifier data then it is just duplicated in all the fragments */
1229 memcpy(auth_data, (unsigned char *)payload + data_length, header_auth_len);
1231 /* these packets are handled specially, not by the generic SecurePacket
1233 if (!auth_data_out && SecIsValidHandle(&Connection->ctx))
1235 status = RPCRT4_SecurePacket(Connection, SECURE_PACKET_RECEIVE,
1236 CurrentHeader, hdr_length,
1237 (unsigned char *)pMsg->Buffer + buffer_length, data_length,
1238 (RpcAuthVerifier *)auth_data,
1239 auth_data + sizeof(RpcAuthVerifier),
1240 header_auth_len - sizeof(RpcAuthVerifier));
1241 if (status != RPC_S_OK) goto fail;
1245 buffer_length += data_length;
1246 if (!(CurrentHeader->common.flags & RPC_FLG_LAST)) {
1247 TRACE("next header\n");
1249 if (*Header != CurrentHeader)
1251 RPCRT4_FreeHeader(CurrentHeader);
1252 CurrentHeader = NULL;
1254 HeapFree(GetProcessHeap(), 0, payload);
1257 status = RPCRT4_receive_fragment(Connection, &CurrentHeader, &payload);
1258 if (status != RPC_S_OK) goto fail;
1265 pMsg->BufferLength = buffer_length;
1271 RPCRT4_SetThreadCurrentConnection(NULL);
1272 if (CurrentHeader != *Header)
1273 RPCRT4_FreeHeader(CurrentHeader);
1274 if (status != RPC_S_OK) {
1275 I_RpcFree(pMsg->Buffer);
1276 pMsg->Buffer = NULL;
1277 RPCRT4_FreeHeader(*Header);
1280 if (auth_data_out && status == RPC_S_OK) {
1281 *auth_length_out = auth_length;
1282 *auth_data_out = auth_data;
1285 HeapFree(GetProcessHeap(), 0, auth_data);
1286 HeapFree(GetProcessHeap(), 0, payload);
1290 /***********************************************************************
1291 * RPCRT4_Receive (internal)
1293 * Receive a packet from connection and merge the fragments.
1295 RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
1298 return RPCRT4_ReceiveWithAuth(Connection, Header, pMsg, NULL, NULL);
1301 /***********************************************************************
1302 * I_RpcNegotiateTransferSyntax [RPCRT4.@]
1304 * Negotiates the transfer syntax used by a client connection by connecting
1308 * pMsg [I] RPC Message structure.
1309 * pAsync [I] Asynchronous state to set.
1312 * Success: RPC_S_OK.
1313 * Failure: Any error code.
1315 RPC_STATUS WINAPI I_RpcNegotiateTransferSyntax(PRPC_MESSAGE pMsg)
1317 RpcBinding* bind = pMsg->Handle;
1318 RpcConnection* conn;
1319 RPC_STATUS status = RPC_S_OK;
1321 TRACE("(%p)\n", pMsg);
1323 if (!bind || bind->server)
1325 ERR("no binding\n");
1326 return RPC_S_INVALID_BINDING;
1329 /* if we already have a connection, we don't need to negotiate again */
1330 if (!pMsg->ReservedForRuntime)
1332 RPC_CLIENT_INTERFACE *cif = pMsg->RpcInterfaceInformation;
1333 if (!cif) return RPC_S_INTERFACE_NOT_FOUND;
1335 if (!bind->Endpoint || !bind->Endpoint[0])
1337 TRACE("automatically resolving partially bound binding\n");
1338 status = RpcEpResolveBinding(bind, cif);
1339 if (status != RPC_S_OK) return status;
1342 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
1345 if (status == RPC_S_OK)
1347 pMsg->ReservedForRuntime = conn;
1348 RPCRT4_AddRefBinding(bind);
1355 /***********************************************************************
1356 * I_RpcGetBuffer [RPCRT4.@]
1358 * Allocates a buffer for use by I_RpcSend or I_RpcSendReceive and binds to the
1362 * pMsg [I/O] RPC message information.
1365 * Success: RPC_S_OK.
1366 * Failure: RPC_S_INVALID_BINDING if pMsg->Handle is invalid.
1367 * RPC_S_SERVER_UNAVAILABLE if unable to connect to server.
1368 * ERROR_OUTOFMEMORY if buffer allocation failed.
1371 * The pMsg->BufferLength field determines the size of the buffer to allocate,
1374 * Use I_RpcFreeBuffer() to unbind from the server and free the message buffer.
1377 * I_RpcFreeBuffer(), I_RpcSend(), I_RpcReceive(), I_RpcSendReceive().
1379 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
1382 RpcBinding* bind = pMsg->Handle;
1384 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
1388 ERR("no binding\n");
1389 return RPC_S_INVALID_BINDING;
1392 pMsg->Buffer = I_RpcAllocate(pMsg->BufferLength);
1393 TRACE("Buffer=%p\n", pMsg->Buffer);
1396 return ERROR_OUTOFMEMORY;
1400 status = I_RpcNegotiateTransferSyntax(pMsg);
1401 if (status != RPC_S_OK)
1402 I_RpcFree(pMsg->Buffer);
1410 /***********************************************************************
1411 * I_RpcReAllocateBuffer (internal)
1413 static RPC_STATUS I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg)
1415 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
1416 pMsg->Buffer = HeapReAlloc(GetProcessHeap(), 0, pMsg->Buffer, pMsg->BufferLength);
1418 TRACE("Buffer=%p\n", pMsg->Buffer);
1419 return pMsg->Buffer ? RPC_S_OK : ERROR_OUTOFMEMORY;
1422 /***********************************************************************
1423 * I_RpcFreeBuffer [RPCRT4.@]
1425 * Frees a buffer allocated by I_RpcGetBuffer or I_RpcReceive and unbinds from
1426 * the server interface.
1429 * pMsg [I/O] RPC message information.
1435 * I_RpcGetBuffer(), I_RpcReceive().
1437 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
1439 RpcBinding* bind = pMsg->Handle;
1441 TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
1445 ERR("no binding\n");
1446 return RPC_S_INVALID_BINDING;
1449 if (pMsg->ReservedForRuntime)
1451 RpcConnection *conn = pMsg->ReservedForRuntime;
1452 RPCRT4_CloseBinding(bind, conn);
1453 RPCRT4_ReleaseBinding(bind);
1454 pMsg->ReservedForRuntime = NULL;
1456 I_RpcFree(pMsg->Buffer);
1460 static void CALLBACK async_apc_notifier_proc(ULONG_PTR ulParam)
1462 RPC_ASYNC_STATE *state = (RPC_ASYNC_STATE *)ulParam;
1463 state->u.APC.NotificationRoutine(state, NULL, state->Event);
1466 static DWORD WINAPI async_notifier_proc(LPVOID p)
1468 RpcConnection *conn = p;
1469 RPC_ASYNC_STATE *state = conn->async_state;
1471 if (state && conn->ops->wait_for_incoming_data(conn) != -1)
1473 state->Event = RpcCallComplete;
1474 switch (state->NotificationType)
1476 case RpcNotificationTypeEvent:
1477 TRACE("RpcNotificationTypeEvent %p\n", state->u.hEvent);
1478 SetEvent(state->u.hEvent);
1480 case RpcNotificationTypeApc:
1481 TRACE("RpcNotificationTypeApc %p\n", state->u.APC.hThread);
1482 QueueUserAPC(async_apc_notifier_proc, state->u.APC.hThread, (ULONG_PTR)state);
1484 case RpcNotificationTypeIoc:
1485 TRACE("RpcNotificationTypeIoc %p, 0x%x, 0x%lx, %p\n",
1486 state->u.IOC.hIOPort, state->u.IOC.dwNumberOfBytesTransferred,
1487 state->u.IOC.dwCompletionKey, state->u.IOC.lpOverlapped);
1488 PostQueuedCompletionStatus(state->u.IOC.hIOPort,
1489 state->u.IOC.dwNumberOfBytesTransferred,
1490 state->u.IOC.dwCompletionKey,
1491 state->u.IOC.lpOverlapped);
1493 case RpcNotificationTypeHwnd:
1494 TRACE("RpcNotificationTypeHwnd %p 0x%x\n", state->u.HWND.hWnd,
1496 PostMessageW(state->u.HWND.hWnd, state->u.HWND.Msg, 0, 0);
1498 case RpcNotificationTypeCallback:
1499 TRACE("RpcNotificationTypeCallback %p\n", state->u.NotificationRoutine);
1500 state->u.NotificationRoutine(state, NULL, state->Event);
1502 case RpcNotificationTypeNone:
1503 TRACE("RpcNotificationTypeNone\n");
1506 FIXME("unknown NotificationType: %d/0x%x\n", state->NotificationType, state->NotificationType);
1514 /***********************************************************************
1515 * I_RpcSend [RPCRT4.@]
1517 * Sends a message to the server.
1520 * pMsg [I/O] RPC message information.
1526 * The buffer must have been allocated with I_RpcGetBuffer().
1529 * I_RpcGetBuffer(), I_RpcReceive(), I_RpcSendReceive().
1531 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
1533 RpcBinding* bind = pMsg->Handle;
1534 RpcConnection* conn;
1538 TRACE("(%p)\n", pMsg);
1539 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1541 conn = pMsg->ReservedForRuntime;
1543 hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
1545 pMsg->ProcNum & ~RPC_FLAGS_VALID_BIT,
1548 return ERROR_OUTOFMEMORY;
1549 hdr->common.call_id = conn->NextCallId++;
1551 status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
1553 RPCRT4_FreeHeader(hdr);
1555 if (status == RPC_S_OK && pMsg->RpcFlags & RPC_BUFFER_ASYNC)
1557 if (!QueueUserWorkItem(async_notifier_proc, conn, WT_EXECUTEDEFAULT | WT_EXECUTELONGFUNCTION))
1558 status = RPC_S_OUT_OF_RESOURCES;
1564 /* is this status something that the server can't recover from? */
1565 static inline BOOL is_hard_error(RPC_STATUS status)
1569 case 0: /* user-defined fault */
1570 case ERROR_ACCESS_DENIED:
1571 case ERROR_INVALID_PARAMETER:
1572 case RPC_S_PROTOCOL_ERROR:
1573 case RPC_S_CALL_FAILED:
1574 case RPC_S_CALL_FAILED_DNE:
1575 case RPC_S_SEC_PKG_ERROR:
1582 /***********************************************************************
1583 * I_RpcReceive [RPCRT4.@]
1585 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
1587 RpcBinding* bind = pMsg->Handle;
1589 RpcPktHdr *hdr = NULL;
1590 RpcConnection *conn;
1592 TRACE("(%p)\n", pMsg);
1593 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1595 conn = pMsg->ReservedForRuntime;
1596 status = RPCRT4_Receive(conn, &hdr, pMsg);
1597 if (status != RPC_S_OK) {
1598 WARN("receive failed with error %x\n", status);
1602 switch (hdr->common.ptype) {
1606 ERR ("we got fault packet with status 0x%x\n", hdr->fault.status);
1607 status = NCA2RPC_STATUS(hdr->fault.status);
1608 if (is_hard_error(status))
1612 WARN("bad packet type %d\n", hdr->common.ptype);
1613 status = RPC_S_PROTOCOL_ERROR;
1618 RPCRT4_FreeHeader(hdr);
1622 RPCRT4_FreeHeader(hdr);
1623 RPCRT4_DestroyConnection(conn);
1624 pMsg->ReservedForRuntime = NULL;
1628 /***********************************************************************
1629 * I_RpcSendReceive [RPCRT4.@]
1631 * Sends a message to the server and receives the response.
1634 * pMsg [I/O] RPC message information.
1637 * Success: RPC_S_OK.
1638 * Failure: Any error code.
1641 * The buffer must have been allocated with I_RpcGetBuffer().
1644 * I_RpcGetBuffer(), I_RpcSend(), I_RpcReceive().
1646 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
1649 void *original_buffer;
1651 TRACE("(%p)\n", pMsg);
1653 original_buffer = pMsg->Buffer;
1654 status = I_RpcSend(pMsg);
1655 if (status == RPC_S_OK)
1656 status = I_RpcReceive(pMsg);
1657 /* free the buffer replaced by a new buffer in I_RpcReceive */
1658 if (status == RPC_S_OK)
1659 I_RpcFree(original_buffer);
1663 /***********************************************************************
1664 * I_RpcAsyncSetHandle [RPCRT4.@]
1666 * Sets the asynchronous state of the handle contained in the RPC message
1670 * pMsg [I] RPC Message structure.
1671 * pAsync [I] Asynchronous state to set.
1674 * Success: RPC_S_OK.
1675 * Failure: Any error code.
1677 RPC_STATUS WINAPI I_RpcAsyncSetHandle(PRPC_MESSAGE pMsg, PRPC_ASYNC_STATE pAsync)
1679 RpcBinding* bind = pMsg->Handle;
1680 RpcConnection *conn;
1682 TRACE("(%p, %p)\n", pMsg, pAsync);
1684 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1686 conn = pMsg->ReservedForRuntime;
1687 conn->async_state = pAsync;
1692 /***********************************************************************
1693 * I_RpcAsyncAbortCall [RPCRT4.@]
1695 * Aborts an asynchronous call.
1698 * pAsync [I] Asynchronous state.
1699 * ExceptionCode [I] Exception code.
1702 * Success: RPC_S_OK.
1703 * Failure: Any error code.
1705 RPC_STATUS WINAPI I_RpcAsyncAbortCall(PRPC_ASYNC_STATE pAsync, ULONG ExceptionCode)
1707 FIXME("(%p, %d): stub\n", pAsync, ExceptionCode);
1708 return RPC_S_INVALID_ASYNC_HANDLE;