4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * - figure out whether we *really* got this right
23 * - check for errors and throw exceptions
39 #include "wine/debug.h"
41 #include "rpc_binding.h"
44 #include "rpc_message.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
48 static DWORD
RPCRT4_GetHeaderSize(RpcPktHdr
*Header
)
50 static const DWORD header_sizes
[] = {
51 sizeof(Header
->request
), 0, sizeof(Header
->response
),
52 sizeof(Header
->fault
), 0, 0, 0, 0, 0, 0, 0, sizeof(Header
->bind
),
53 sizeof(Header
->bind_ack
), sizeof(Header
->bind_nack
),
58 if (Header
->common
.ptype
< sizeof(header_sizes
) / sizeof(header_sizes
[0])) {
59 ret
= header_sizes
[Header
->common
.ptype
];
61 FIXME("unhandled packet type\n");
62 if (Header
->common
.flags
& RPC_FLG_OBJECT_UUID
)
65 TRACE("invalid packet type\n");
71 static VOID
RPCRT4_BuildCommonHeader(RpcPktHdr
*Header
, unsigned char PacketType
,
72 unsigned long DataRepresentation
)
74 Header
->common
.rpc_ver
= RPC_VER_MAJOR
;
75 Header
->common
.rpc_ver_minor
= RPC_VER_MINOR
;
76 Header
->common
.ptype
= PacketType
;
77 Header
->common
.drep
[0] = LOBYTE(LOWORD(DataRepresentation
));
78 Header
->common
.drep
[1] = HIBYTE(LOWORD(DataRepresentation
));
79 Header
->common
.drep
[2] = LOBYTE(HIWORD(DataRepresentation
));
80 Header
->common
.drep
[3] = HIBYTE(HIWORD(DataRepresentation
));
81 Header
->common
.auth_len
= 0;
82 Header
->common
.call_id
= 1;
83 Header
->common
.flags
= 0;
84 /* Flags and fragment length are computed in RPCRT4_Send. */
87 static RpcPktHdr
*RPCRT4_BuildRequestHeader(unsigned long DataRepresentation
,
88 unsigned long BufferLength
,
89 unsigned short ProcNum
,
96 has_object
= (ObjectUuid
!= NULL
&& !UuidIsNil(ObjectUuid
, &status
));
97 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
98 sizeof(header
->request
) + (has_object
? sizeof(UUID
) : 0));
103 RPCRT4_BuildCommonHeader(header
, PKT_REQUEST
, DataRepresentation
);
104 header
->common
.frag_len
= sizeof(header
->request
);
105 header
->request
.alloc_hint
= BufferLength
;
106 header
->request
.context_id
= 0;
107 header
->request
.opnum
= ProcNum
;
109 header
->common
.flags
|= RPC_FLG_OBJECT_UUID
;
110 header
->common
.frag_len
+= sizeof(UUID
);
111 memcpy(&header
->request
+ 1, ObjectUuid
, sizeof(UUID
));
117 static RpcPktHdr
*RPCRT4_BuildResponseHeader(unsigned long DataRepresentation
,
118 unsigned long BufferLength
)
122 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->response
));
123 if (header
== NULL
) {
127 RPCRT4_BuildCommonHeader(header
, PKT_RESPONSE
, DataRepresentation
);
128 header
->common
.frag_len
= sizeof(header
->response
);
129 header
->response
.alloc_hint
= BufferLength
;
134 RpcPktHdr
*RPCRT4_BuildFaultHeader(unsigned long DataRepresentation
,
139 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->fault
));
140 if (header
== NULL
) {
144 RPCRT4_BuildCommonHeader(header
, PKT_FAULT
, DataRepresentation
);
145 header
->common
.frag_len
= sizeof(header
->fault
);
146 header
->fault
.status
= Status
;
151 RpcPktHdr
*RPCRT4_BuildBindHeader(unsigned long DataRepresentation
,
152 unsigned short MaxTransmissionSize
,
153 unsigned short MaxReceiveSize
,
154 RPC_SYNTAX_IDENTIFIER
*AbstractId
,
155 RPC_SYNTAX_IDENTIFIER
*TransferId
)
159 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->bind
));
160 if (header
== NULL
) {
164 RPCRT4_BuildCommonHeader(header
, PKT_BIND
, DataRepresentation
);
165 header
->common
.frag_len
= sizeof(header
->bind
);
166 header
->bind
.max_tsize
= MaxTransmissionSize
;
167 header
->bind
.max_rsize
= MaxReceiveSize
;
168 header
->bind
.num_elements
= 1;
169 header
->bind
.num_syntaxes
= 1;
170 memcpy(&header
->bind
.abstract
, AbstractId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
171 memcpy(&header
->bind
.transfer
, TransferId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
176 RpcPktHdr
*RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation
,
177 unsigned char RpcVersion
,
178 unsigned char RpcVersionMinor
)
182 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->bind_nack
));
183 if (header
== NULL
) {
187 RPCRT4_BuildCommonHeader(header
, PKT_BIND_NACK
, DataRepresentation
);
188 header
->common
.frag_len
= sizeof(header
->bind_nack
);
189 header
->bind_nack
.protocols_count
= 1;
190 header
->bind_nack
.protocols
[0].rpc_ver
= RpcVersion
;
191 header
->bind_nack
.protocols
[0].rpc_ver_minor
= RpcVersionMinor
;
196 RpcPktHdr
*RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation
,
197 unsigned short MaxTransmissionSize
,
198 unsigned short MaxReceiveSize
,
200 unsigned long Result
,
201 unsigned long Reason
,
202 RPC_SYNTAX_IDENTIFIER
*TransferId
)
205 unsigned long header_size
;
206 RpcAddressString
*server_address
;
208 RPC_SYNTAX_IDENTIFIER
*transfer_id
;
210 header_size
= sizeof(header
->bind_ack
) + sizeof(RpcResults
) +
211 sizeof(RPC_SYNTAX_IDENTIFIER
) + sizeof(RpcAddressString
) +
212 strlen(ServerAddress
);
214 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, header_size
);
215 if (header
== NULL
) {
219 RPCRT4_BuildCommonHeader(header
, PKT_BIND_ACK
, DataRepresentation
);
220 header
->common
.frag_len
= header_size
;
221 header
->bind_ack
.max_tsize
= MaxTransmissionSize
;
222 header
->bind_ack
.max_rsize
= MaxReceiveSize
;
223 server_address
= (RpcAddressString
*)(&header
->bind_ack
+ 1);
224 server_address
->length
= strlen(ServerAddress
) + 1;
225 strcpy(server_address
->string
, ServerAddress
);
226 results
= (RpcResults
*)((ULONG_PTR
)server_address
+ sizeof(RpcAddressString
) + server_address
->length
- 1);
227 results
->num_results
= 1;
228 results
->results
[0].result
= Result
;
229 results
->results
[0].reason
= Reason
;
230 transfer_id
= (RPC_SYNTAX_IDENTIFIER
*)(results
+ 1);
231 memcpy(transfer_id
, TransferId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
236 VOID
RPCRT4_FreeHeader(RpcPktHdr
*Header
)
238 HeapFree(GetProcessHeap(), 0, Header
);
241 /***********************************************************************
242 * RPCRT4_Send (internal)
244 * Transmit a packet over connection in acceptable fragments.
246 RPC_STATUS
RPCRT4_Send(RpcConnection
*Connection
, RpcPktHdr
*Header
,
247 void *Buffer
, unsigned int BufferLength
)
250 DWORD hdr_size
, count
;
253 /* The packet building functions save the packet header size, so we can use it. */
254 hdr_size
= Header
->common
.frag_len
;
255 Header
->common
.flags
|= RPC_FLG_FIRST
;
256 Header
->common
.flags
&= ~RPC_FLG_LAST
;
257 while (!(Header
->common
.flags
& RPC_FLG_LAST
)) {
258 /* decide if we need to split the packet into fragments */
259 if ((BufferLength
+ hdr_size
) <= Connection
->MaxTransmissionSize
) {
260 Header
->common
.flags
|= RPC_FLG_LAST
;
261 Header
->common
.frag_len
= BufferLength
+ hdr_size
;
263 Header
->common
.frag_len
= Connection
->MaxTransmissionSize
;
264 buffer_pos
+= Header
->common
.frag_len
- hdr_size
;
265 BufferLength
-= Header
->common
.frag_len
- hdr_size
;
268 /* transmit packet header */
269 if (!WriteFile(Connection
->conn
, Header
, hdr_size
, &count
, NULL
)) {
270 WARN("WriteFile failed with error %ld\n", GetLastError());
271 return GetLastError();
274 /* fragment consisted of header only and is the last one */
275 if (hdr_size
== Header
->common
.frag_len
&&
276 Header
->common
.flags
& RPC_FLG_LAST
) {
280 /* send the fragment data */
281 if (!WriteFile(Connection
->conn
, buffer_pos
, Header
->common
.frag_len
- hdr_size
, &count
, NULL
)) {
282 WARN("WriteFile failed with error %ld\n", GetLastError());
283 return GetLastError();
286 Header
->common
.flags
&= ~RPC_FLG_FIRST
;
292 /***********************************************************************
293 * RPCRT4_Receive (internal)
295 * Receive a packet from connection and merge the fragments.
297 RPC_STATUS
RPCRT4_Receive(RpcConnection
*Connection
, RpcPktHdr
**Header
,
301 DWORD dwRead
, hdr_length
;
302 unsigned short first_flag
;
303 unsigned long data_length
;
304 unsigned long buffer_length
;
305 unsigned char *buffer_ptr
;
306 RpcPktCommonHdr common_hdr
;
310 TRACE("(%p, %p, %p)\n", Connection
, Header
, pMsg
);
312 /* read packet common header */
313 if (!ReadFile(Connection
->conn
, &common_hdr
, sizeof(common_hdr
), &dwRead
, NULL
)) {
314 if (GetLastError() != ERROR_MORE_DATA
) {
315 WARN("ReadFile failed with error %ld\n", GetLastError());
316 status
= RPC_S_PROTOCOL_ERROR
;
320 if (dwRead
!= sizeof(common_hdr
)) {
321 WARN("Short read of header, %ld/%d bytes\n", dwRead
, sizeof(common_hdr
));
322 status
= RPC_S_PROTOCOL_ERROR
;
326 /* verify if the header really makes sense */
327 if (common_hdr
.rpc_ver
!= RPC_VER_MAJOR
||
328 common_hdr
.rpc_ver_minor
!= RPC_VER_MINOR
) {
329 WARN("unhandled packet version\n");
330 status
= RPC_S_PROTOCOL_ERROR
;
334 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
335 if (hdr_length
== 0) {
336 WARN("header length == 0\n");
337 status
= RPC_S_PROTOCOL_ERROR
;
341 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
342 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
344 /* read the rest of packet header */
345 if (!ReadFile(Connection
->conn
, &(*Header
)->common
+ 1,
346 hdr_length
- sizeof(common_hdr
), &dwRead
, NULL
)) {
347 if (GetLastError() != ERROR_MORE_DATA
) {
348 WARN("ReadFile failed with error %ld\n", GetLastError());
349 status
= RPC_S_PROTOCOL_ERROR
;
353 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
354 WARN("bad header length, %ld/%ld bytes\n", dwRead
, hdr_length
- sizeof(common_hdr
));
355 status
= RPC_S_PROTOCOL_ERROR
;
359 /* read packet body */
360 switch (common_hdr
.ptype
) {
362 pMsg
->BufferLength
= (*Header
)->response
.alloc_hint
;
365 pMsg
->BufferLength
= (*Header
)->request
.alloc_hint
;
368 pMsg
->BufferLength
= common_hdr
.frag_len
- hdr_length
;
371 TRACE("buffer length = %u\n", pMsg
->BufferLength
);
373 status
= I_RpcGetBuffer(pMsg
);
374 if (status
!= RPC_S_OK
) goto fail
;
376 first_flag
= RPC_FLG_FIRST
;
378 buffer_ptr
= pMsg
->Buffer
;
379 while (buffer_length
< pMsg
->BufferLength
)
381 data_length
= (*Header
)->common
.frag_len
- hdr_length
;
382 if (((*Header
)->common
.flags
& RPC_FLG_FIRST
) != first_flag
||
383 data_length
+ buffer_length
> pMsg
->BufferLength
) {
384 TRACE("invalid packet flags or buffer length\n");
385 status
= RPC_S_PROTOCOL_ERROR
;
389 if (data_length
== 0) dwRead
= 0; else
390 if (!ReadFile(Connection
->conn
, buffer_ptr
, data_length
, &dwRead
, NULL
)) {
391 if (GetLastError() != ERROR_MORE_DATA
) {
392 WARN("ReadFile failed with error %ld\n", GetLastError());
393 status
= RPC_S_PROTOCOL_ERROR
;
397 if (dwRead
!= data_length
) {
398 WARN("bad data length, %ld/%ld\n", dwRead
, data_length
);
399 status
= RPC_S_PROTOCOL_ERROR
;
403 /* when there is no more data left, it should be the last packet */
404 if (buffer_length
== pMsg
->BufferLength
&&
405 ((*Header
)->common
.flags
& RPC_FLG_LAST
) == 0) {
406 WARN("no more data left, but not last packet\n");
407 status
= RPC_S_PROTOCOL_ERROR
;
411 buffer_length
+= data_length
;
412 if (buffer_length
< pMsg
->BufferLength
) {
413 TRACE("next header\n");
415 /* read the header of next packet */
416 if (!ReadFile(Connection
->conn
, *Header
, hdr_length
, &dwRead
, NULL
)) {
417 if (GetLastError() != ERROR_MORE_DATA
) {
418 WARN("ReadFile failed with error %ld\n", GetLastError());
419 status
= GetLastError();
423 if (dwRead
!= hdr_length
) {
424 WARN("invalid packet header size (%ld)\n", dwRead
);
425 status
= RPC_S_PROTOCOL_ERROR
;
429 buffer_ptr
+= data_length
;
438 if (status
!= RPC_S_OK
&& *Header
) {
439 RPCRT4_FreeHeader(*Header
);
445 /***********************************************************************
446 * I_RpcGetBuffer [RPCRT4.@]
448 RPC_STATUS WINAPI
I_RpcGetBuffer(PRPC_MESSAGE pMsg
)
450 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
451 /* FIXME: pfnAllocate? */
452 pMsg
->Buffer
= HeapAlloc(GetProcessHeap(), 0, pMsg
->BufferLength
);
454 TRACE("Buffer=%p\n", pMsg
->Buffer
);
455 /* FIXME: which errors to return? */
456 return pMsg
->Buffer
? S_OK
: E_OUTOFMEMORY
;
459 /***********************************************************************
460 * I_RpcFreeBuffer [RPCRT4.@]
462 RPC_STATUS WINAPI
I_RpcFreeBuffer(PRPC_MESSAGE pMsg
)
464 TRACE("(%p) Buffer=%p\n", pMsg
, pMsg
->Buffer
);
465 /* FIXME: pfnFree? */
466 HeapFree(GetProcessHeap(), 0, pMsg
->Buffer
);
471 /***********************************************************************
472 * I_RpcSend [RPCRT4.@]
474 RPC_STATUS WINAPI
I_RpcSend(PRPC_MESSAGE pMsg
)
476 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
478 RPC_CLIENT_INTERFACE
* cif
= NULL
;
479 RPC_SERVER_INTERFACE
* sif
= NULL
;
483 TRACE("(%p)\n", pMsg
);
484 if (!bind
) return RPC_S_INVALID_BINDING
;
487 sif
= pMsg
->RpcInterfaceInformation
;
488 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
489 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
492 cif
= pMsg
->RpcInterfaceInformation
;
493 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
494 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
498 if (status
!= RPC_S_OK
) return status
;
501 if (pMsg
->RpcFlags
& WINE_RPCFLAG_EXCEPTION
) {
502 hdr
= RPCRT4_BuildFaultHeader(pMsg
->DataRepresentation
,
505 hdr
= RPCRT4_BuildResponseHeader(pMsg
->DataRepresentation
,
509 hdr
= RPCRT4_BuildRequestHeader(pMsg
->DataRepresentation
,
510 pMsg
->BufferLength
, pMsg
->ProcNum
,
514 status
= RPCRT4_Send(conn
, hdr
, pMsg
->Buffer
, pMsg
->BufferLength
);
516 RPCRT4_FreeHeader(hdr
);
520 /* save the connection, so the response can be read from it */
521 pMsg
->ReservedForRuntime
= conn
;
524 RPCRT4_CloseBinding(bind
, conn
);
530 /***********************************************************************
531 * I_RpcReceive [RPCRT4.@]
533 RPC_STATUS WINAPI
I_RpcReceive(PRPC_MESSAGE pMsg
)
535 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
537 RPC_CLIENT_INTERFACE
* cif
= NULL
;
538 RPC_SERVER_INTERFACE
* sif
= NULL
;
540 RpcPktHdr
*hdr
= NULL
;
542 TRACE("(%p)\n", pMsg
);
543 if (!bind
) return RPC_S_INVALID_BINDING
;
545 if (pMsg
->ReservedForRuntime
) {
546 conn
= pMsg
->ReservedForRuntime
;
547 pMsg
->ReservedForRuntime
= NULL
;
550 sif
= pMsg
->RpcInterfaceInformation
;
551 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
552 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
555 cif
= pMsg
->RpcInterfaceInformation
;
556 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
557 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
560 if (status
!= RPC_S_OK
) return status
;
563 status
= RPCRT4_Receive(conn
, &hdr
, pMsg
);
564 if (status
!= RPC_S_OK
) {
565 WARN("receive failed with error %lx\n", status
);
569 status
= RPC_S_PROTOCOL_ERROR
;
571 switch (hdr
->common
.ptype
) {
573 if (bind
->server
) goto fail
;
576 if (!bind
->server
) goto fail
;
579 pMsg
->RpcFlags
|= WINE_RPCFLAG_EXCEPTION
;
580 ERR ("we got fault packet with status %lx\n", hdr
->fault
.status
);
581 status
= RPC_S_CALL_FAILED
; /* ? */
584 WARN("bad packet type %d\n", hdr
->common
.ptype
);
593 RPCRT4_FreeHeader(hdr
);
595 RPCRT4_CloseBinding(bind
, conn
);
599 /***********************************************************************
600 * I_RpcSendReceive [RPCRT4.@]
602 RPC_STATUS WINAPI
I_RpcSendReceive(PRPC_MESSAGE pMsg
)
606 TRACE("(%p)\n", pMsg
);
607 status
= I_RpcSend(pMsg
);
608 if (status
== RPC_S_OK
)
609 status
= I_RpcReceive(pMsg
);