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
, &Connection
->ovl
[1]) &&
270 ERROR_IO_PENDING
!= GetLastError()) {
271 WARN("WriteFile failed with error %ld\n", GetLastError());
272 return GetLastError();
274 if (!GetOverlappedResult(Connection
->conn
, &Connection
->ovl
[1], &count
, TRUE
)) {
275 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
276 return GetLastError();
279 /* fragment consisted of header only and is the last one */
280 if (hdr_size
== Header
->common
.frag_len
&&
281 Header
->common
.flags
& RPC_FLG_LAST
) {
285 /* send the fragment data */
286 if (!WriteFile(Connection
->conn
, buffer_pos
, Header
->common
.frag_len
- hdr_size
, &count
, &Connection
->ovl
[1]) &&
287 ERROR_IO_PENDING
!= GetLastError()) {
288 WARN("WriteFile failed with error %ld\n", GetLastError());
289 return GetLastError();
291 if (!GetOverlappedResult(Connection
->conn
, &Connection
->ovl
[1], &count
, TRUE
)) {
292 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
293 return GetLastError();
296 Header
->common
.flags
&= ~RPC_FLG_FIRST
;
302 /***********************************************************************
303 * RPCRT4_Receive (internal)
305 * Receive a packet from connection and merge the fragments.
307 RPC_STATUS
RPCRT4_Receive(RpcConnection
*Connection
, RpcPktHdr
**Header
,
311 DWORD dwRead
, hdr_length
;
312 unsigned short first_flag
;
313 unsigned long data_length
;
314 unsigned long buffer_length
;
315 unsigned char *buffer_ptr
;
316 RpcPktCommonHdr common_hdr
;
320 TRACE("(%p, %p, %p)\n", Connection
, Header
, pMsg
);
322 /* read packet common header */
323 if (!ReadFile(Connection
->conn
, &common_hdr
, sizeof(common_hdr
), &dwRead
, &Connection
->ovl
[0]) &&
324 ERROR_IO_PENDING
!= GetLastError()) {
325 WARN("ReadFile failed with error %ld\n", GetLastError());
326 status
= RPC_S_PROTOCOL_ERROR
;
329 if (!GetOverlappedResult(Connection
->conn
, &Connection
->ovl
[0], &dwRead
, TRUE
)) {
330 if (GetLastError() != ERROR_MORE_DATA
) {
331 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
332 status
= RPC_S_PROTOCOL_ERROR
;
336 if (dwRead
!= sizeof(common_hdr
)) {
337 WARN("Short read of header, %ld/%d bytes\n", dwRead
, sizeof(common_hdr
));
338 status
= RPC_S_PROTOCOL_ERROR
;
342 /* verify if the header really makes sense */
343 if (common_hdr
.rpc_ver
!= RPC_VER_MAJOR
||
344 common_hdr
.rpc_ver_minor
!= RPC_VER_MINOR
) {
345 WARN("unhandled packet version\n");
346 status
= RPC_S_PROTOCOL_ERROR
;
350 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
351 if (hdr_length
== 0) {
352 WARN("header length == 0\n");
353 status
= RPC_S_PROTOCOL_ERROR
;
357 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
358 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
360 /* read the rest of packet header */
361 if (!ReadFile(Connection
->conn
, &(*Header
)->common
+ 1,
362 hdr_length
- sizeof(common_hdr
), &dwRead
, &Connection
->ovl
[0]) &&
363 ERROR_IO_PENDING
!= GetLastError()) {
364 WARN("ReadFile failed with error %ld\n", GetLastError());
365 status
= RPC_S_PROTOCOL_ERROR
;
368 if (!GetOverlappedResult(Connection
->conn
, &Connection
->ovl
[0], &dwRead
, TRUE
)) {
369 if (GetLastError() != ERROR_MORE_DATA
) {
370 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
371 status
= RPC_S_PROTOCOL_ERROR
;
375 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
376 WARN("bad header length, %ld/%ld bytes\n", dwRead
, hdr_length
- sizeof(common_hdr
));
377 status
= RPC_S_PROTOCOL_ERROR
;
382 /* read packet body */
383 switch (common_hdr
.ptype
) {
385 pMsg
->BufferLength
= (*Header
)->response
.alloc_hint
;
388 pMsg
->BufferLength
= (*Header
)->request
.alloc_hint
;
391 pMsg
->BufferLength
= common_hdr
.frag_len
- hdr_length
;
394 TRACE("buffer length = %u\n", pMsg
->BufferLength
);
396 status
= I_RpcGetBuffer(pMsg
);
397 if (status
!= RPC_S_OK
) goto fail
;
399 first_flag
= RPC_FLG_FIRST
;
401 buffer_ptr
= pMsg
->Buffer
;
402 while (buffer_length
< pMsg
->BufferLength
)
404 data_length
= (*Header
)->common
.frag_len
- hdr_length
;
405 if (((*Header
)->common
.flags
& RPC_FLG_FIRST
) != first_flag
||
406 data_length
+ buffer_length
> pMsg
->BufferLength
) {
407 TRACE("invalid packet flags or buffer length\n");
408 status
= RPC_S_PROTOCOL_ERROR
;
412 if (data_length
== 0) dwRead
= 0; else {
413 if (!ReadFile(Connection
->conn
, buffer_ptr
, data_length
, &dwRead
, &Connection
->ovl
[0]) &&
414 ERROR_IO_PENDING
!= GetLastError()) {
415 WARN("ReadFile failed with error %ld\n", GetLastError());
416 status
= RPC_S_PROTOCOL_ERROR
;
419 if (!GetOverlappedResult(Connection
->conn
, &Connection
->ovl
[0], &dwRead
, TRUE
)) {
420 if (GetLastError() != ERROR_MORE_DATA
) {
421 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
422 status
= RPC_S_PROTOCOL_ERROR
;
427 if (dwRead
!= data_length
) {
428 WARN("bad data length, %ld/%ld\n", dwRead
, data_length
);
429 status
= RPC_S_PROTOCOL_ERROR
;
433 /* when there is no more data left, it should be the last packet */
434 if (buffer_length
== pMsg
->BufferLength
&&
435 ((*Header
)->common
.flags
& RPC_FLG_LAST
) == 0) {
436 WARN("no more data left, but not last packet\n");
437 status
= RPC_S_PROTOCOL_ERROR
;
441 buffer_length
+= data_length
;
442 if (buffer_length
< pMsg
->BufferLength
) {
443 TRACE("next header\n");
445 /* read the header of next packet */
446 if (!ReadFile(Connection
->conn
, *Header
, hdr_length
, &dwRead
, &Connection
->ovl
[0]) &&
447 ERROR_IO_PENDING
!= GetLastError()) {
448 WARN("ReadFile failed with error %ld\n", GetLastError());
449 status
= GetLastError();
452 if (!GetOverlappedResult(Connection
->conn
, &Connection
->ovl
[0], &dwRead
, TRUE
)) {
453 if (GetLastError() != ERROR_MORE_DATA
) {
454 WARN("GetOverlappedResult failed with error %ld\n", GetLastError());
455 status
= RPC_S_PROTOCOL_ERROR
;
459 if (dwRead
!= hdr_length
) {
460 WARN("invalid packet header size (%ld)\n", dwRead
);
461 status
= RPC_S_PROTOCOL_ERROR
;
465 buffer_ptr
+= data_length
;
474 if (status
!= RPC_S_OK
&& *Header
) {
475 RPCRT4_FreeHeader(*Header
);
481 /***********************************************************************
482 * I_RpcGetBuffer [RPCRT4.@]
484 RPC_STATUS WINAPI
I_RpcGetBuffer(PRPC_MESSAGE pMsg
)
486 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
487 /* FIXME: pfnAllocate? */
488 pMsg
->Buffer
= HeapAlloc(GetProcessHeap(), 0, pMsg
->BufferLength
);
490 TRACE("Buffer=%p\n", pMsg
->Buffer
);
491 /* FIXME: which errors to return? */
492 return pMsg
->Buffer
? S_OK
: E_OUTOFMEMORY
;
495 /***********************************************************************
496 * I_RpcFreeBuffer [RPCRT4.@]
498 RPC_STATUS WINAPI
I_RpcFreeBuffer(PRPC_MESSAGE pMsg
)
500 TRACE("(%p) Buffer=%p\n", pMsg
, pMsg
->Buffer
);
501 /* FIXME: pfnFree? */
502 HeapFree(GetProcessHeap(), 0, pMsg
->Buffer
);
507 /***********************************************************************
508 * I_RpcSend [RPCRT4.@]
510 RPC_STATUS WINAPI
I_RpcSend(PRPC_MESSAGE pMsg
)
512 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
514 RPC_CLIENT_INTERFACE
* cif
= NULL
;
515 RPC_SERVER_INTERFACE
* sif
= NULL
;
519 TRACE("(%p)\n", pMsg
);
520 if (!bind
) return RPC_S_INVALID_BINDING
;
523 sif
= pMsg
->RpcInterfaceInformation
;
524 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
525 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
528 cif
= pMsg
->RpcInterfaceInformation
;
529 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
530 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
534 if (status
!= RPC_S_OK
) return status
;
537 if (pMsg
->RpcFlags
& WINE_RPCFLAG_EXCEPTION
) {
538 hdr
= RPCRT4_BuildFaultHeader(pMsg
->DataRepresentation
,
541 hdr
= RPCRT4_BuildResponseHeader(pMsg
->DataRepresentation
,
545 hdr
= RPCRT4_BuildRequestHeader(pMsg
->DataRepresentation
,
546 pMsg
->BufferLength
, pMsg
->ProcNum
,
550 status
= RPCRT4_Send(conn
, hdr
, pMsg
->Buffer
, pMsg
->BufferLength
);
552 RPCRT4_FreeHeader(hdr
);
556 /* save the connection, so the response can be read from it */
557 pMsg
->ReservedForRuntime
= conn
;
560 RPCRT4_CloseBinding(bind
, conn
);
566 /***********************************************************************
567 * I_RpcReceive [RPCRT4.@]
569 RPC_STATUS WINAPI
I_RpcReceive(PRPC_MESSAGE pMsg
)
571 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
573 RPC_CLIENT_INTERFACE
* cif
= NULL
;
574 RPC_SERVER_INTERFACE
* sif
= NULL
;
576 RpcPktHdr
*hdr
= NULL
;
578 TRACE("(%p)\n", pMsg
);
579 if (!bind
) return RPC_S_INVALID_BINDING
;
581 if (pMsg
->ReservedForRuntime
) {
582 conn
= pMsg
->ReservedForRuntime
;
583 pMsg
->ReservedForRuntime
= NULL
;
586 sif
= pMsg
->RpcInterfaceInformation
;
587 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
588 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
591 cif
= pMsg
->RpcInterfaceInformation
;
592 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
593 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
596 if (status
!= RPC_S_OK
) return status
;
599 status
= RPCRT4_Receive(conn
, &hdr
, pMsg
);
600 if (status
!= RPC_S_OK
) {
601 WARN("receive failed with error %lx\n", status
);
605 status
= RPC_S_PROTOCOL_ERROR
;
607 switch (hdr
->common
.ptype
) {
609 if (bind
->server
) goto fail
;
612 if (!bind
->server
) goto fail
;
615 pMsg
->RpcFlags
|= WINE_RPCFLAG_EXCEPTION
;
616 ERR ("we got fault packet with status %lx\n", hdr
->fault
.status
);
617 status
= RPC_S_CALL_FAILED
; /* ? */
620 WARN("bad packet type %d\n", hdr
->common
.ptype
);
629 RPCRT4_FreeHeader(hdr
);
631 RPCRT4_CloseBinding(bind
, conn
);
635 /***********************************************************************
636 * I_RpcSendReceive [RPCRT4.@]
638 RPC_STATUS WINAPI
I_RpcSendReceive(PRPC_MESSAGE pMsg
)
642 TRACE("(%p)\n", pMsg
);
643 status
= I_RpcSend(pMsg
);
644 if (status
== RPC_S_OK
)
645 status
= I_RpcReceive(pMsg
);