Corrected last error information.
[wine] / dlls / rpcrt4 / rpc_message.c
1 /*
2  * RPC messages
3  *
4  * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5  * Copyright 2004 Filip Navara
6  *
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.
11  *
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.
16  *
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
20  *
21  * TODO:
22  *  - figure out whether we *really* got this right
23  *  - check for errors and throw exceptions
24  */
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winreg.h"
34
35 #include "rpc.h"
36 #include "rpcndr.h"
37 #include "rpcdcep.h"
38
39 #include "wine/debug.h"
40
41 #include "rpc_binding.h"
42 #include "rpc_misc.h"
43 #include "rpc_defs.h"
44 #include "rpc_message.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
47
48 static DWORD RPCRT4_GetHeaderSize(RpcPktHdr *Header)
49 {
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),
54     0, 0, 0, 0, 0
55   };
56   ULONG ret = 0;
57   
58   if (Header->common.ptype < sizeof(header_sizes) / sizeof(header_sizes[0])) {
59     ret = header_sizes[Header->common.ptype];
60     if (ret == 0)
61       FIXME("unhandled packet type\n");
62     if (Header->common.flags & RPC_FLG_OBJECT_UUID)
63       ret += sizeof(UUID);
64   } else {
65     TRACE("invalid packet type\n");
66   }
67
68   return ret;
69 }
70
71 static VOID RPCRT4_BuildCommonHeader(RpcPktHdr *Header, unsigned char PacketType,
72                               unsigned long DataRepresentation)
73 {
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. */
85 }                              
86
87 static RpcPktHdr *RPCRT4_BuildRequestHeader(unsigned long DataRepresentation,
88                                      unsigned long BufferLength,
89                                      unsigned short ProcNum,
90                                      UUID *ObjectUuid)
91 {
92   RpcPktHdr *header;
93   BOOL has_object;
94   RPC_STATUS status;
95
96   has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
97   header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
98                      sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
99   if (header == NULL) {
100     return NULL;
101   }
102
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;
108   if (has_object) {
109     header->common.flags |= RPC_FLG_OBJECT_UUID;
110     header->common.frag_len += sizeof(UUID);
111     memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
112   }
113
114   return header;
115 }
116
117 static RpcPktHdr *RPCRT4_BuildResponseHeader(unsigned long DataRepresentation,
118                                       unsigned long BufferLength)
119 {
120   RpcPktHdr *header;
121
122   header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->response));
123   if (header == NULL) {
124     return NULL;
125   }
126
127   RPCRT4_BuildCommonHeader(header, PKT_RESPONSE, DataRepresentation);
128   header->common.frag_len = sizeof(header->response);
129   header->response.alloc_hint = BufferLength;
130
131   return header;
132 }
133
134 RpcPktHdr *RPCRT4_BuildFaultHeader(unsigned long DataRepresentation,
135                                    RPC_STATUS Status)
136 {
137   RpcPktHdr *header;
138
139   header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->fault));
140   if (header == NULL) {
141     return NULL;
142   }
143
144   RPCRT4_BuildCommonHeader(header, PKT_FAULT, DataRepresentation);
145   header->common.frag_len = sizeof(header->fault);
146   header->fault.status = Status;
147
148   return header;
149 }
150
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)
156 {
157   RpcPktHdr *header;
158
159   header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind));
160   if (header == NULL) {
161     return NULL;
162   }
163
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));
172
173   return header;
174 }
175
176 RpcPktHdr *RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation,
177                                       unsigned char RpcVersion,
178                                       unsigned char RpcVersionMinor)
179 {
180   RpcPktHdr *header;
181
182   header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->bind_nack));
183   if (header == NULL) {
184     return NULL;
185   }
186
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;
192
193   return header;
194 }
195
196 RpcPktHdr *RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation,
197                                      unsigned short MaxTransmissionSize,
198                                      unsigned short MaxReceiveSize,
199                                      LPSTR ServerAddress,
200                                      unsigned long Result,
201                                      unsigned long Reason,
202                                      RPC_SYNTAX_IDENTIFIER *TransferId)
203 {
204   RpcPktHdr *header;
205   unsigned long header_size;
206   RpcAddressString *server_address;
207   RpcResults *results;
208   RPC_SYNTAX_IDENTIFIER *transfer_id;
209
210   header_size = sizeof(header->bind_ack) + sizeof(RpcResults) +
211                 sizeof(RPC_SYNTAX_IDENTIFIER) + sizeof(RpcAddressString) +
212                 strlen(ServerAddress);
213
214   header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, header_size);
215   if (header == NULL) {
216     return NULL;
217   }
218
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));
232
233   return header;
234 }
235
236 VOID RPCRT4_FreeHeader(RpcPktHdr *Header)
237 {
238   HeapFree(GetProcessHeap(), 0, Header);
239 }
240
241 /***********************************************************************
242  *           RPCRT4_Send (internal)
243  * 
244  * Transmit a packet over connection in acceptable fragments.
245  */
246 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
247                        void *Buffer, unsigned int BufferLength)
248 {
249   PUCHAR buffer_pos;
250   DWORD hdr_size, count;
251
252   buffer_pos = Buffer;
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;
262     } else {
263       Header->common.frag_len = Connection->MaxTransmissionSize;
264       buffer_pos += Header->common.frag_len - hdr_size;
265       BufferLength -= Header->common.frag_len - hdr_size;
266     }
267
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();
272     }
273
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) {
277       return RPC_S_OK;
278     }
279
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();
284     }
285
286     Header->common.flags &= ~RPC_FLG_FIRST;
287   }
288
289   return RPC_S_OK;
290 }
291
292 /***********************************************************************
293  *           RPCRT4_Receive (internal)
294  * 
295  * Receive a packet from connection and merge the fragments.
296  */
297 RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
298                           PRPC_MESSAGE pMsg)
299 {
300   RPC_STATUS status;
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;
307
308   *Header = NULL;
309
310   TRACE("(%p, %p, %p)\n", Connection, Header, pMsg);
311
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;
317       goto fail;
318     }
319   }
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;
323     goto fail;
324   }
325
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;
331     goto fail;
332   }
333
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;
338     goto fail;
339   }
340
341   *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
342   memcpy(*Header, &common_hdr, sizeof(common_hdr));
343
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;
350       goto fail;
351     }
352   }
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;
356     goto fail;
357   }
358
359   /* read packet body */
360   switch (common_hdr.ptype) {
361   case PKT_RESPONSE:
362     pMsg->BufferLength = (*Header)->response.alloc_hint;
363     break;
364   case PKT_REQUEST:
365     pMsg->BufferLength = (*Header)->request.alloc_hint;
366     break;
367   default:
368     pMsg->BufferLength = common_hdr.frag_len - hdr_length;
369   }
370
371   TRACE("buffer length = %u\n", pMsg->BufferLength);
372
373   status = I_RpcGetBuffer(pMsg);
374   if (status != RPC_S_OK) goto fail;
375
376   first_flag = RPC_FLG_FIRST;
377   buffer_length = 0;
378   buffer_ptr = pMsg->Buffer;
379   while (buffer_length < pMsg->BufferLength)
380   {
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;
386       goto fail;
387     }
388
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;
394         goto fail;
395       }
396     }
397     if (dwRead != data_length) {
398       WARN("bad data length, %ld/%ld\n", dwRead, data_length);
399       status = RPC_S_PROTOCOL_ERROR;
400       goto fail;
401     }
402
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;
408       goto fail;
409     }
410
411     buffer_length += data_length;
412     if (buffer_length < pMsg->BufferLength) {
413       TRACE("next header\n");
414
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();
420           goto fail;
421         }
422       }
423       if (dwRead != hdr_length) {
424         WARN("invalid packet header size (%ld)\n", dwRead);
425         status = RPC_S_PROTOCOL_ERROR;
426         goto fail;
427       }
428
429       buffer_ptr += data_length;
430       first_flag = 0;
431     }
432   }
433
434   /* success */
435   status = RPC_S_OK;
436
437 fail:
438   if (status != RPC_S_OK && *Header) {
439     RPCRT4_FreeHeader(*Header);
440     *Header = NULL;
441   }
442   return status;
443 }
444
445 /***********************************************************************
446  *           I_RpcGetBuffer [RPCRT4.@]
447  */
448 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
449 {
450   TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
451   /* FIXME: pfnAllocate? */
452   pMsg->Buffer = HeapAlloc(GetProcessHeap(), 0, pMsg->BufferLength);
453
454   TRACE("Buffer=%p\n", pMsg->Buffer);
455   /* FIXME: which errors to return? */
456   return pMsg->Buffer ? S_OK : E_OUTOFMEMORY;
457 }
458
459 /***********************************************************************
460  *           I_RpcFreeBuffer [RPCRT4.@]
461  */
462 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
463 {
464   TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
465   /* FIXME: pfnFree? */
466   HeapFree(GetProcessHeap(), 0, pMsg->Buffer);
467   pMsg->Buffer = NULL;
468   return S_OK;
469 }
470
471 /***********************************************************************
472  *           I_RpcSend [RPCRT4.@]
473  */
474 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
475 {
476   RpcBinding* bind = (RpcBinding*)pMsg->Handle;
477   RpcConnection* conn;
478   RPC_CLIENT_INTERFACE* cif = NULL;
479   RPC_SERVER_INTERFACE* sif = NULL;
480   RPC_STATUS status;
481   RpcPktHdr *hdr;
482
483   TRACE("(%p)\n", pMsg);
484   if (!bind) return RPC_S_INVALID_BINDING;
485
486   if (bind->server) {
487     sif = pMsg->RpcInterfaceInformation;
488     if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
489     status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
490                                 &sif->InterfaceId);
491   } else {
492     cif = pMsg->RpcInterfaceInformation;
493     if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
494     status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
495                                 &cif->InterfaceId);
496   }
497
498   if (status != RPC_S_OK) return status;
499
500   if (bind->server) {
501     if (pMsg->RpcFlags & WINE_RPCFLAG_EXCEPTION) {
502       hdr = RPCRT4_BuildFaultHeader(pMsg->DataRepresentation,
503                                     RPC_S_CALL_FAILED);
504     } else {
505       hdr = RPCRT4_BuildResponseHeader(pMsg->DataRepresentation,
506                                        pMsg->BufferLength);
507     }
508   } else {
509     hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
510                                     pMsg->BufferLength, pMsg->ProcNum,
511                                     &bind->ObjectUuid);
512   }
513
514   status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
515
516   RPCRT4_FreeHeader(hdr);
517
518   /* success */
519   if (!bind->server) {
520     /* save the connection, so the response can be read from it */
521     pMsg->ReservedForRuntime = conn;
522     return RPC_S_OK;
523   }
524   RPCRT4_CloseBinding(bind, conn);
525   status = RPC_S_OK;
526
527   return status;
528 }
529
530 /***********************************************************************
531  *           I_RpcReceive [RPCRT4.@]
532  */
533 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
534 {
535   RpcBinding* bind = (RpcBinding*)pMsg->Handle;
536   RpcConnection* conn;
537   RPC_CLIENT_INTERFACE* cif = NULL;
538   RPC_SERVER_INTERFACE* sif = NULL;
539   RPC_STATUS status;
540   RpcPktHdr *hdr = NULL;
541
542   TRACE("(%p)\n", pMsg);
543   if (!bind) return RPC_S_INVALID_BINDING;
544
545   if (pMsg->ReservedForRuntime) {
546     conn = pMsg->ReservedForRuntime;
547     pMsg->ReservedForRuntime = NULL;
548   } else {
549     if (bind->server) {
550       sif = pMsg->RpcInterfaceInformation;
551       if (!sif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
552       status = RPCRT4_OpenBinding(bind, &conn, &sif->TransferSyntax,
553                                   &sif->InterfaceId);
554     } else {
555       cif = pMsg->RpcInterfaceInformation;
556       if (!cif) return RPC_S_INTERFACE_NOT_FOUND; /* ? */
557       status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
558                                   &cif->InterfaceId);
559     }
560     if (status != RPC_S_OK) return status;
561   }
562
563   status = RPCRT4_Receive(conn, &hdr, pMsg);
564   if (status != RPC_S_OK) {
565     WARN("receive failed with error %lx\n", status);
566     goto fail;
567   }
568
569   status = RPC_S_PROTOCOL_ERROR;
570
571   switch (hdr->common.ptype) {
572   case PKT_RESPONSE:
573     if (bind->server) goto fail;
574     break;
575   case PKT_REQUEST:
576     if (!bind->server) goto fail;
577     break;
578   case PKT_FAULT:
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; /* ? */
582     goto fail;
583   default:
584     WARN("bad packet type %d\n", hdr->common.ptype);
585     goto fail;
586   }
587
588   /* success */
589   status = RPC_S_OK;
590
591 fail:
592   if (hdr) {
593     RPCRT4_FreeHeader(hdr);
594   }
595   RPCRT4_CloseBinding(bind, conn);
596   return status;
597 }
598
599 /***********************************************************************
600  *           I_RpcSendReceive [RPCRT4.@]
601  */
602 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
603 {
604   RPC_STATUS status;
605
606   TRACE("(%p)\n", pMsg);
607   status = I_RpcSend(pMsg);
608   if (status == RPC_S_OK)
609     status = I_RpcReceive(pMsg);
610   return status;
611 }