4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006 CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
38 #include "rpc_binding.h"
41 #include "rpc_message.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
45 /* note: the DCE/RPC spec says the alignment amount should be 4, but
46 * MS/RPC servers seem to always use 16 */
47 #define AUTH_ALIGNMENT 16
49 /* gets the amount needed to round a value up to the specified alignment */
50 #define ROUND_UP_AMOUNT(value, alignment) \
51 (((alignment) - (((value) % (alignment)))) % (alignment))
52 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
54 static RPC_STATUS
I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg
);
56 static DWORD
RPCRT4_GetHeaderSize(RpcPktHdr
*Header
)
58 static const DWORD header_sizes
[] = {
59 sizeof(Header
->request
), 0, sizeof(Header
->response
),
60 sizeof(Header
->fault
), 0, 0, 0, 0, 0, 0, 0, sizeof(Header
->bind
),
61 sizeof(Header
->bind_ack
), sizeof(Header
->bind_nack
),
66 if (Header
->common
.ptype
< sizeof(header_sizes
) / sizeof(header_sizes
[0])) {
67 ret
= header_sizes
[Header
->common
.ptype
];
69 FIXME("unhandled packet type\n");
70 if (Header
->common
.flags
& RPC_FLG_OBJECT_UUID
)
73 TRACE("invalid packet type\n");
79 static VOID
RPCRT4_BuildCommonHeader(RpcPktHdr
*Header
, unsigned char PacketType
,
80 unsigned long DataRepresentation
)
82 Header
->common
.rpc_ver
= RPC_VER_MAJOR
;
83 Header
->common
.rpc_ver_minor
= RPC_VER_MINOR
;
84 Header
->common
.ptype
= PacketType
;
85 Header
->common
.drep
[0] = LOBYTE(LOWORD(DataRepresentation
));
86 Header
->common
.drep
[1] = HIBYTE(LOWORD(DataRepresentation
));
87 Header
->common
.drep
[2] = LOBYTE(HIWORD(DataRepresentation
));
88 Header
->common
.drep
[3] = HIBYTE(HIWORD(DataRepresentation
));
89 Header
->common
.auth_len
= 0;
90 Header
->common
.call_id
= 1;
91 Header
->common
.flags
= 0;
92 /* Flags and fragment length are computed in RPCRT4_Send. */
95 static RpcPktHdr
*RPCRT4_BuildRequestHeader(unsigned long DataRepresentation
,
96 unsigned long BufferLength
,
97 unsigned short ProcNum
,
104 has_object
= (ObjectUuid
!= NULL
&& !UuidIsNil(ObjectUuid
, &status
));
105 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
106 sizeof(header
->request
) + (has_object
? sizeof(UUID
) : 0));
107 if (header
== NULL
) {
111 RPCRT4_BuildCommonHeader(header
, PKT_REQUEST
, DataRepresentation
);
112 header
->common
.frag_len
= sizeof(header
->request
);
113 header
->request
.alloc_hint
= BufferLength
;
114 header
->request
.context_id
= 0;
115 header
->request
.opnum
= ProcNum
;
117 header
->common
.flags
|= RPC_FLG_OBJECT_UUID
;
118 header
->common
.frag_len
+= sizeof(UUID
);
119 memcpy(&header
->request
+ 1, ObjectUuid
, sizeof(UUID
));
125 static RpcPktHdr
*RPCRT4_BuildResponseHeader(unsigned long DataRepresentation
,
126 unsigned long BufferLength
)
130 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->response
));
131 if (header
== NULL
) {
135 RPCRT4_BuildCommonHeader(header
, PKT_RESPONSE
, DataRepresentation
);
136 header
->common
.frag_len
= sizeof(header
->response
);
137 header
->response
.alloc_hint
= BufferLength
;
142 RpcPktHdr
*RPCRT4_BuildFaultHeader(unsigned long DataRepresentation
,
147 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->fault
));
148 if (header
== NULL
) {
152 RPCRT4_BuildCommonHeader(header
, PKT_FAULT
, DataRepresentation
);
153 header
->common
.frag_len
= sizeof(header
->fault
);
154 header
->fault
.status
= Status
;
159 RpcPktHdr
*RPCRT4_BuildBindHeader(unsigned long DataRepresentation
,
160 unsigned short MaxTransmissionSize
,
161 unsigned short MaxReceiveSize
,
162 RPC_SYNTAX_IDENTIFIER
*AbstractId
,
163 RPC_SYNTAX_IDENTIFIER
*TransferId
)
167 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->bind
));
168 if (header
== NULL
) {
172 RPCRT4_BuildCommonHeader(header
, PKT_BIND
, DataRepresentation
);
173 header
->common
.frag_len
= sizeof(header
->bind
);
174 header
->bind
.max_tsize
= MaxTransmissionSize
;
175 header
->bind
.max_rsize
= MaxReceiveSize
;
176 header
->bind
.num_elements
= 1;
177 header
->bind
.num_syntaxes
= 1;
178 memcpy(&header
->bind
.abstract
, AbstractId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
179 memcpy(&header
->bind
.transfer
, TransferId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
184 RpcPktHdr
*RPCRT4_BuildAuthHeader(unsigned long DataRepresentation
)
188 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
189 sizeof(header
->common
) + 12);
193 RPCRT4_BuildCommonHeader(header
, PKT_AUTH3
, DataRepresentation
);
194 header
->common
.frag_len
= 0x14;
195 header
->common
.auth_len
= 0;
200 RpcPktHdr
*RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation
,
201 unsigned char RpcVersion
,
202 unsigned char RpcVersionMinor
)
206 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->bind_nack
));
207 if (header
== NULL
) {
211 RPCRT4_BuildCommonHeader(header
, PKT_BIND_NACK
, DataRepresentation
);
212 header
->common
.frag_len
= sizeof(header
->bind_nack
);
213 header
->bind_nack
.protocols_count
= 1;
214 header
->bind_nack
.protocols
[0].rpc_ver
= RpcVersion
;
215 header
->bind_nack
.protocols
[0].rpc_ver_minor
= RpcVersionMinor
;
220 RpcPktHdr
*RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation
,
221 unsigned short MaxTransmissionSize
,
222 unsigned short MaxReceiveSize
,
224 unsigned long Result
,
225 unsigned long Reason
,
226 RPC_SYNTAX_IDENTIFIER
*TransferId
)
229 unsigned long header_size
;
230 RpcAddressString
*server_address
;
232 RPC_SYNTAX_IDENTIFIER
*transfer_id
;
234 header_size
= sizeof(header
->bind_ack
) +
235 ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[strlen(ServerAddress
) + 1]), 4) +
237 sizeof(RPC_SYNTAX_IDENTIFIER
);
239 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, header_size
);
240 if (header
== NULL
) {
244 RPCRT4_BuildCommonHeader(header
, PKT_BIND_ACK
, DataRepresentation
);
245 header
->common
.frag_len
= header_size
;
246 header
->bind_ack
.max_tsize
= MaxTransmissionSize
;
247 header
->bind_ack
.max_rsize
= MaxReceiveSize
;
248 server_address
= (RpcAddressString
*)(&header
->bind_ack
+ 1);
249 server_address
->length
= strlen(ServerAddress
) + 1;
250 strcpy(server_address
->string
, ServerAddress
);
251 /* results is 4-byte aligned */
252 results
= (RpcResults
*)((ULONG_PTR
)server_address
+ ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[server_address
->length
]), 4));
253 results
->num_results
= 1;
254 results
->results
[0].result
= Result
;
255 results
->results
[0].reason
= Reason
;
256 transfer_id
= (RPC_SYNTAX_IDENTIFIER
*)(results
+ 1);
257 memcpy(transfer_id
, TransferId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
262 VOID
RPCRT4_FreeHeader(RpcPktHdr
*Header
)
264 HeapFree(GetProcessHeap(), 0, Header
);
267 /***********************************************************************
268 * RPCRT4_SendAuth (internal)
270 * Transmit a packet with authorization data over connection in acceptable fragments.
272 static RPC_STATUS
RPCRT4_SendAuth(RpcConnection
*Connection
, RpcPktHdr
*Header
,
273 void *Buffer
, unsigned int BufferLength
,
274 void *Auth
, unsigned int AuthLength
)
280 LONG alen
= AuthLength
? (AuthLength
+ sizeof(RpcAuthVerifier
)) : 0;
283 /* The packet building functions save the packet header size, so we can use it. */
284 hdr_size
= Header
->common
.frag_len
;
285 Header
->common
.auth_len
= AuthLength
;
286 Header
->common
.flags
|= RPC_FLG_FIRST
;
287 Header
->common
.flags
&= ~RPC_FLG_LAST
;
288 while (!(Header
->common
.flags
& RPC_FLG_LAST
)) {
289 unsigned char auth_pad_len
= AuthLength
? ROUND_UP_AMOUNT(BufferLength
, AUTH_ALIGNMENT
) : 0;
290 unsigned int pkt_size
= BufferLength
+ hdr_size
+ alen
+ auth_pad_len
;
292 /* decide if we need to split the packet into fragments */
293 if (pkt_size
<= Connection
->MaxTransmissionSize
) {
294 Header
->common
.flags
|= RPC_FLG_LAST
;
295 Header
->common
.frag_len
= pkt_size
;
298 /* make sure packet payload will be a multiple of 16 */
299 Header
->common
.frag_len
=
300 ((Connection
->MaxTransmissionSize
- hdr_size
- alen
) & ~(AUTH_ALIGNMENT
-1)) +
304 pkt
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, Header
->common
.frag_len
);
306 memcpy(pkt
, Header
, hdr_size
);
308 /* fragment consisted of header only and is the last one */
309 if (hdr_size
== Header
->common
.frag_len
)
312 memcpy(pkt
+ hdr_size
, buffer_pos
, Header
->common
.frag_len
- hdr_size
- auth_pad_len
- alen
);
314 /* add the authorization info */
315 if (Connection
->AuthInfo
&& AuthLength
)
317 RpcAuthVerifier
*auth_hdr
= (RpcAuthVerifier
*)&pkt
[Header
->common
.frag_len
- alen
];
319 auth_hdr
->auth_type
= Connection
->AuthInfo
->AuthnSvc
;
320 auth_hdr
->auth_level
= Connection
->AuthInfo
->AuthnLevel
;
321 auth_hdr
->auth_pad_length
= auth_pad_len
;
322 auth_hdr
->auth_reserved
= 0;
323 /* a unique number... */
324 auth_hdr
->auth_context_id
= (unsigned long)Connection
;
326 memcpy(auth_hdr
+ 1, Auth
, AuthLength
);
330 count
= rpcrt4_conn_write(Connection
, pkt
, Header
->common
.frag_len
);
331 HeapFree(GetProcessHeap(), 0, pkt
);
333 WARN("rpcrt4_conn_write failed (auth)\n");
334 return RPC_S_PROTOCOL_ERROR
;
337 buffer_pos
+= Header
->common
.frag_len
- hdr_size
- alen
- auth_pad_len
;
338 BufferLength
-= Header
->common
.frag_len
- hdr_size
- alen
- auth_pad_len
;
339 Header
->common
.flags
&= ~RPC_FLG_FIRST
;
345 /***********************************************************************
346 * RPCRT4_AuthNegotiate (internal)
348 static void RPCRT4_AuthNegotiate(RpcConnection
*conn
, SecBuffer
*out
)
351 SecBufferDesc out_desc
;
352 unsigned char *buffer
;
354 buffer
= HeapAlloc(GetProcessHeap(), 0, 0x100);
356 out
->BufferType
= SECBUFFER_TOKEN
;
357 out
->cbBuffer
= 0x100;
358 out
->pvBuffer
= buffer
;
360 out_desc
.ulVersion
= 0;
361 out_desc
.cBuffers
= 1;
362 out_desc
.pBuffers
= out
;
365 conn
->ctx
.dwLower
= 0;
366 conn
->ctx
.dwUpper
= 0;
368 r
= InitializeSecurityContextA(&conn
->AuthInfo
->cred
, NULL
, NULL
,
369 ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
| ISC_REQ_MUTUAL_AUTH
|
370 ISC_REQ_DELEGATE
, 0, SECURITY_NETWORK_DREP
,
371 NULL
, 0, &conn
->ctx
, &out_desc
, &conn
->attr
, &conn
->exp
);
373 TRACE("r = %08x cbBuffer = %ld attr = %08x\n", r
, out
->cbBuffer
, conn
->attr
);
376 /***********************************************************************
377 * RPCRT4_AuthorizeBinding (internal)
379 static RPC_STATUS
RPCRT_AuthorizeConnection(RpcConnection
* conn
,
380 BYTE
*challenge
, ULONG count
)
382 SecBufferDesc inp_desc
, out_desc
;
385 unsigned char buffer
[0x100];
389 TRACE("challenge %s, %d bytes\n", challenge
, count
);
391 out
.BufferType
= SECBUFFER_TOKEN
;
392 out
.cbBuffer
= sizeof buffer
;
393 out
.pvBuffer
= buffer
;
395 out_desc
.ulVersion
= 0;
396 out_desc
.cBuffers
= 1;
397 out_desc
.pBuffers
= &out
;
399 inp
.BufferType
= SECBUFFER_TOKEN
;
400 inp
.pvBuffer
= challenge
;
401 inp
.cbBuffer
= count
;
403 inp_desc
.cBuffers
= 1;
404 inp_desc
.pBuffers
= &inp
;
405 inp_desc
.ulVersion
= 0;
407 r
= InitializeSecurityContextA(&conn
->AuthInfo
->cred
, &conn
->ctx
, NULL
,
408 ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
| ISC_REQ_MUTUAL_AUTH
|
409 ISC_REQ_DELEGATE
, 0, SECURITY_NETWORK_DREP
,
410 &inp_desc
, 0, &conn
->ctx
, &out_desc
, &conn
->attr
, &conn
->exp
);
413 WARN("InitializeSecurityContext failed with error 0x%08x\n", r
);
414 return ERROR_ACCESS_DENIED
;
417 resp_hdr
= RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION
);
419 return E_OUTOFMEMORY
;
421 status
= RPCRT4_SendAuth(conn
, resp_hdr
, NULL
, 0, out
.pvBuffer
, out
.cbBuffer
);
423 RPCRT4_FreeHeader(resp_hdr
);
428 /***********************************************************************
429 * RPCRT4_Send (internal)
431 * Transmit a packet over connection in acceptable fragments.
433 RPC_STATUS
RPCRT4_Send(RpcConnection
*Connection
, RpcPktHdr
*Header
,
434 void *Buffer
, unsigned int BufferLength
)
439 /* if we've already authenticated, just send the context */
440 if (Connection
->ctx
.dwUpper
|| Connection
->ctx
.dwLower
)
442 unsigned char buffer
[0x10];
444 memset(buffer
, 0, sizeof buffer
);
445 buffer
[0] = 1; /* version number lsb */
447 return RPCRT4_SendAuth(Connection
, Header
, Buffer
, BufferLength
, buffer
, sizeof buffer
);
450 if (!Connection
->AuthInfo
||
451 Connection
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_DEFAULT
||
452 Connection
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_NONE
)
454 return RPCRT4_SendAuth(Connection
, Header
, Buffer
, BufferLength
, NULL
, 0);
457 out
.BufferType
= SECBUFFER_TOKEN
;
461 /* tack on a negotiate packet */
462 RPCRT4_AuthNegotiate(Connection
, &out
);
463 r
= RPCRT4_SendAuth(Connection
, Header
, Buffer
, BufferLength
, out
.pvBuffer
, out
.cbBuffer
);
464 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
469 /***********************************************************************
470 * RPCRT4_Receive (internal)
472 * Receive a packet from connection and merge the fragments.
474 RPC_STATUS
RPCRT4_Receive(RpcConnection
*Connection
, RpcPktHdr
**Header
,
480 unsigned short first_flag
;
481 unsigned long data_length
;
482 unsigned long buffer_length
;
483 unsigned long auth_length
;
484 unsigned char *auth_data
= NULL
;
485 RpcPktCommonHdr common_hdr
;
489 TRACE("(%p, %p, %p)\n", Connection
, Header
, pMsg
);
491 /* read packet common header */
492 dwRead
= rpcrt4_conn_read(Connection
, &common_hdr
, sizeof(common_hdr
));
493 if (dwRead
!= sizeof(common_hdr
)) {
494 WARN("Short read of header, %d bytes\n", dwRead
);
495 status
= RPC_S_PROTOCOL_ERROR
;
499 /* verify if the header really makes sense */
500 if (common_hdr
.rpc_ver
!= RPC_VER_MAJOR
||
501 common_hdr
.rpc_ver_minor
!= RPC_VER_MINOR
) {
502 WARN("unhandled packet version\n");
503 status
= RPC_S_PROTOCOL_ERROR
;
507 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
508 if (hdr_length
== 0) {
509 WARN("header length == 0\n");
510 status
= RPC_S_PROTOCOL_ERROR
;
514 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
515 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
517 /* read the rest of packet header */
518 dwRead
= rpcrt4_conn_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
519 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
520 WARN("bad header length, %d/%d bytes\n", dwRead
, hdr_length
- sizeof(common_hdr
));
521 status
= RPC_S_PROTOCOL_ERROR
;
525 /* read packet body */
526 switch (common_hdr
.ptype
) {
528 pMsg
->BufferLength
= (*Header
)->response
.alloc_hint
;
531 pMsg
->BufferLength
= (*Header
)->request
.alloc_hint
;
534 pMsg
->BufferLength
= common_hdr
.frag_len
- hdr_length
- RPC_AUTH_VERIFIER_LEN(&common_hdr
);
537 TRACE("buffer length = %u\n", pMsg
->BufferLength
);
539 status
= I_RpcGetBuffer(pMsg
);
540 if (status
!= RPC_S_OK
) goto fail
;
542 first_flag
= RPC_FLG_FIRST
;
543 auth_length
= common_hdr
.auth_len
;
545 auth_data
= HeapAlloc(GetProcessHeap(), 0, RPC_AUTH_VERIFIER_LEN(&common_hdr
));
547 status
= RPC_S_PROTOCOL_ERROR
;
554 unsigned int header_auth_len
= RPC_AUTH_VERIFIER_LEN(&(*Header
)->common
);
556 /* verify header fields */
558 if (((*Header
)->common
.frag_len
< hdr_length
) ||
559 ((*Header
)->common
.frag_len
- hdr_length
< header_auth_len
)) {
560 WARN("frag_len %d too small for hdr_length %d and auth_len %d\n",
561 common_hdr
.frag_len
, hdr_length
, common_hdr
.auth_len
);
562 status
= RPC_S_PROTOCOL_ERROR
;
566 if ((*Header
)->common
.auth_len
!= auth_length
) {
567 WARN("auth_len header field changed from %ld to %d\n",
568 auth_length
, (*Header
)->common
.auth_len
);
569 status
= RPC_S_PROTOCOL_ERROR
;
573 if (((*Header
)->common
.flags
& RPC_FLG_FIRST
) != first_flag
) {
574 TRACE("invalid packet flags\n");
575 status
= RPC_S_PROTOCOL_ERROR
;
579 data_length
= (*Header
)->common
.frag_len
- hdr_length
- header_auth_len
;
580 if (data_length
+ buffer_length
> pMsg
->BufferLength
) {
581 TRACE("allocation hint exceeded, new buffer length = %ld\n",
582 data_length
+ buffer_length
);
583 pMsg
->BufferLength
= data_length
+ buffer_length
;
584 status
= I_RpcReAllocateBuffer(pMsg
);
585 if (status
!= RPC_S_OK
) goto fail
;
588 if (data_length
== 0) dwRead
= 0; else
589 dwRead
= rpcrt4_conn_read(Connection
,
590 (unsigned char *)pMsg
->Buffer
+ buffer_length
, data_length
);
591 if (dwRead
!= data_length
) {
592 WARN("bad data length, %d/%ld\n", dwRead
, data_length
);
593 status
= RPC_S_PROTOCOL_ERROR
;
597 if (header_auth_len
) {
598 /* FIXME: we should accumulate authentication data for the bind,
599 * bind_ack, alter_context and alter_context_response if necessary.
600 * however, the details of how this is done is very sketchy in the
601 * DCE/RPC spec. for all other packet types that have authentication
602 * verifier data then it is just duplicated in all the fragments */
603 dwRead
= rpcrt4_conn_read(Connection
, auth_data
, header_auth_len
);
604 if (dwRead
!= header_auth_len
) {
605 WARN("bad authentication data length, %d/%d\n", dwRead
,
607 status
= RPC_S_PROTOCOL_ERROR
;
612 buffer_length
+= data_length
;
613 if (!((*Header
)->common
.flags
& RPC_FLG_LAST
)) {
614 TRACE("next header\n");
616 /* read the header of next packet */
617 dwRead
= rpcrt4_conn_read(Connection
, *Header
, hdr_length
);
618 if (dwRead
!= hdr_length
) {
619 WARN("invalid packet header size (%d)\n", dwRead
);
620 status
= RPC_S_PROTOCOL_ERROR
;
629 pMsg
->BufferLength
= buffer_length
;
631 /* respond to authorization request */
632 if (common_hdr
.ptype
== PKT_BIND_ACK
&& auth_length
> sizeof(RpcAuthVerifier
))
634 status
= RPCRT_AuthorizeConnection(Connection
,
635 auth_data
+ sizeof(RpcAuthVerifier
),
645 if (status
!= RPC_S_OK
) {
646 RPCRT4_FreeHeader(*Header
);
649 HeapFree(GetProcessHeap(), 0, auth_data
);
653 /***********************************************************************
654 * I_RpcGetBuffer [RPCRT4.@]
656 RPC_STATUS WINAPI
I_RpcGetBuffer(PRPC_MESSAGE pMsg
)
658 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
659 /* FIXME: pfnAllocate? */
660 pMsg
->Buffer
= HeapAlloc(GetProcessHeap(), 0, pMsg
->BufferLength
);
662 TRACE("Buffer=%p\n", pMsg
->Buffer
);
663 /* FIXME: which errors to return? */
664 return pMsg
->Buffer
? S_OK
: E_OUTOFMEMORY
;
667 /***********************************************************************
668 * I_RpcReAllocateBuffer (internal)
670 static RPC_STATUS
I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg
)
672 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
673 pMsg
->Buffer
= HeapReAlloc(GetProcessHeap(), 0, pMsg
->Buffer
, pMsg
->BufferLength
);
675 TRACE("Buffer=%p\n", pMsg
->Buffer
);
676 return pMsg
->Buffer
? RPC_S_OK
: RPC_S_OUT_OF_RESOURCES
;
679 /***********************************************************************
680 * I_RpcFreeBuffer [RPCRT4.@]
682 RPC_STATUS WINAPI
I_RpcFreeBuffer(PRPC_MESSAGE pMsg
)
684 TRACE("(%p) Buffer=%p\n", pMsg
, pMsg
->Buffer
);
685 /* FIXME: pfnFree? */
686 HeapFree(GetProcessHeap(), 0, pMsg
->Buffer
);
691 /***********************************************************************
692 * I_RpcSend [RPCRT4.@]
694 RPC_STATUS WINAPI
I_RpcSend(PRPC_MESSAGE pMsg
)
696 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
698 RPC_CLIENT_INTERFACE
* cif
= NULL
;
699 RPC_SERVER_INTERFACE
* sif
= NULL
;
703 TRACE("(%p)\n", pMsg
);
704 if (!bind
) return RPC_S_INVALID_BINDING
;
707 sif
= pMsg
->RpcInterfaceInformation
;
708 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
709 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
712 cif
= pMsg
->RpcInterfaceInformation
;
713 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
715 if (!bind
->Endpoint
|| !bind
->Endpoint
[0])
717 TRACE("automatically resolving partially bound binding\n");
718 status
= RpcEpResolveBinding(bind
, cif
);
719 if (status
!= RPC_S_OK
) return status
;
722 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
726 if (status
!= RPC_S_OK
) return status
;
729 if (pMsg
->RpcFlags
& WINE_RPCFLAG_EXCEPTION
) {
730 hdr
= RPCRT4_BuildFaultHeader(pMsg
->DataRepresentation
,
733 hdr
= RPCRT4_BuildResponseHeader(pMsg
->DataRepresentation
,
737 hdr
= RPCRT4_BuildRequestHeader(pMsg
->DataRepresentation
,
738 pMsg
->BufferLength
, pMsg
->ProcNum
,
740 hdr
->common
.call_id
= conn
->NextCallId
++;
743 status
= RPCRT4_Send(conn
, hdr
, pMsg
->Buffer
, pMsg
->BufferLength
);
745 RPCRT4_FreeHeader(hdr
);
749 /* save the connection, so the response can be read from it */
750 pMsg
->ReservedForRuntime
= conn
;
753 RPCRT4_CloseBinding(bind
, conn
);
758 /***********************************************************************
759 * I_RpcReceive [RPCRT4.@]
761 RPC_STATUS WINAPI
I_RpcReceive(PRPC_MESSAGE pMsg
)
763 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
765 RPC_CLIENT_INTERFACE
* cif
= NULL
;
766 RPC_SERVER_INTERFACE
* sif
= NULL
;
768 RpcPktHdr
*hdr
= NULL
;
770 TRACE("(%p)\n", pMsg
);
771 if (!bind
) return RPC_S_INVALID_BINDING
;
773 if (pMsg
->ReservedForRuntime
) {
774 conn
= pMsg
->ReservedForRuntime
;
775 pMsg
->ReservedForRuntime
= NULL
;
778 sif
= pMsg
->RpcInterfaceInformation
;
779 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
780 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
783 cif
= pMsg
->RpcInterfaceInformation
;
784 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
786 if (!bind
->Endpoint
|| !bind
->Endpoint
[0])
788 TRACE("automatically resolving partially bound binding\n");
789 status
= RpcEpResolveBinding(bind
, cif
);
790 if (status
!= RPC_S_OK
) return status
;
793 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
796 if (status
!= RPC_S_OK
) return status
;
799 status
= RPCRT4_Receive(conn
, &hdr
, pMsg
);
800 if (status
!= RPC_S_OK
) {
801 WARN("receive failed with error %lx\n", status
);
805 status
= RPC_S_PROTOCOL_ERROR
;
807 switch (hdr
->common
.ptype
) {
809 if (bind
->server
) goto fail
;
812 if (!bind
->server
) goto fail
;
815 pMsg
->RpcFlags
|= WINE_RPCFLAG_EXCEPTION
;
816 ERR ("we got fault packet with status 0x%lx\n", hdr
->fault
.status
);
817 status
= hdr
->fault
.status
; /* FIXME: do translation from nca error codes */
820 WARN("bad packet type %d\n", hdr
->common
.ptype
);
828 RPCRT4_FreeHeader(hdr
);
829 RPCRT4_CloseBinding(bind
, conn
);
833 /***********************************************************************
834 * I_RpcSendReceive [RPCRT4.@]
836 RPC_STATUS WINAPI
I_RpcSendReceive(PRPC_MESSAGE pMsg
)
840 TRACE("(%p)\n", pMsg
);
841 status
= I_RpcSend(pMsg
);
842 if (status
== RPC_S_OK
)
843 status
= I_RpcReceive(pMsg
);