Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-message.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-message.c  DBusMessage object
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat Inc.
5  * Copyright (C) 2002, 2003  CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-marshal-recursive.h"
28 #include "dbus-marshal-validate.h"
29 #include "dbus-marshal-byteswap.h"
30 #include "dbus-marshal-header.h"
31 #include "dbus-signature.h"
32 #include "dbus-message-private.h"
33 #include "dbus-object-tree.h"
34 #include "dbus-memory.h"
35 #include "dbus-list.h"
36 #include "dbus-threads-internal.h"
37 #ifdef HAVE_UNIX_FD_PASSING
38 #include "dbus-sysdeps-unix.h"
39 #endif
40
41 #include <string.h>
42
43 static void dbus_message_finalize (DBusMessage *message);
44
45 /**
46  * @defgroup DBusMessageInternals DBusMessage implementation details
47  * @ingroup DBusInternals
48  * @brief DBusMessage private implementation details.
49  *
50  * The guts of DBusMessage and its methods.
51  *
52  * @{
53  */
54
55 /* Not thread locked, but strictly const/read-only so should be OK
56  */
57 /** An static string representing an empty signature */
58 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str,  "");
59
60 /* these have wacky values to help trap uninitialized iterators;
61  * but has to fit in 3 bits
62  */
63 enum {
64   DBUS_MESSAGE_ITER_TYPE_READER = 3,
65   DBUS_MESSAGE_ITER_TYPE_WRITER = 7
66 };
67
68 /** typedef for internals of message iterator */
69 typedef struct DBusMessageRealIter DBusMessageRealIter;
70
71 /**
72  * @brief Internals of DBusMessageIter
73  *
74  * Object representing a position in a message. All fields are internal.
75  */
76 struct DBusMessageRealIter
77 {
78   DBusMessage *message; /**< Message used */
79   dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
80   dbus_uint32_t iter_type : 3;      /**< whether this is a reader or writer iter */
81   dbus_uint32_t sig_refcount : 8;   /**< depth of open_signature() */
82   union
83   {
84     DBusTypeWriter writer; /**< writer */
85     DBusTypeReader reader; /**< reader */
86   } u; /**< the type writer or reader that does all the work */
87 };
88
89 static void
90 get_const_signature (DBusHeader        *header,
91                      const DBusString **type_str_p,
92                      int               *type_pos_p)
93 {
94   if (_dbus_header_get_field_raw (header,
95                                   DBUS_HEADER_FIELD_SIGNATURE,
96                                   type_str_p,
97                                   type_pos_p))
98     {
99       *type_pos_p += 1; /* skip the signature length which is 1 byte */
100     }
101   else
102     {
103       *type_str_p = &_dbus_empty_signature_str;
104       *type_pos_p = 0;
105     }
106 }
107
108 /**
109  * Swaps the message to compiler byte order if required
110  *
111  * @param message the message
112  */
113 static void
114 _dbus_message_byteswap (DBusMessage *message)
115 {
116   const DBusString *type_str;
117   int type_pos;
118   char byte_order;
119
120   byte_order = _dbus_header_get_byte_order (&message->header);
121
122   if (byte_order == DBUS_COMPILER_BYTE_ORDER)
123     return;
124
125   _dbus_verbose ("Swapping message into compiler byte order\n");
126   
127   get_const_signature (&message->header, &type_str, &type_pos);
128   
129   _dbus_marshal_byteswap (type_str, type_pos,
130                           byte_order,
131                           DBUS_COMPILER_BYTE_ORDER,
132                           &message->body, 0);
133
134   _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
135   _dbus_assert (_dbus_header_get_byte_order (&message->header) ==
136                 DBUS_COMPILER_BYTE_ORDER);
137 }
138
139 /** byte-swap the message if it doesn't match our byte order.
140  *  Called only when we need the message in our own byte order,
141  *  normally when reading arrays of integers or doubles.
142  *  Otherwise should not be called since it would do needless
143  *  work.
144  */
145 #define ensure_byte_order(message) _dbus_message_byteswap (message)
146
147 /**
148  * Gets the data to be sent over the network for this message.
149  * The header and then the body should be written out.
150  * This function is guaranteed to always return the same
151  * data once a message is locked (with dbus_message_lock()).
152  *
153  * @param message the message.
154  * @param header return location for message header data.
155  * @param body return location for message body data.
156  */
157 void
158 _dbus_message_get_network_data (DBusMessage          *message,
159                                 const DBusString    **header,
160                                 const DBusString    **body)
161 {
162   _dbus_assert (message->locked);
163
164   *header = &message->header.data;
165   *body = &message->body;
166 }
167
168 /**
169  * Gets the unix fds to be sent over the network for this message.
170  * This function is guaranteed to always return the same data once a
171  * message is locked (with dbus_message_lock()).
172  *
173  * @param message the message.
174  * @param fds return location of unix fd array
175  * @param n_fds return number of entries in array
176  */
177 void _dbus_message_get_unix_fds(DBusMessage *message,
178                                 const int  **fds,
179                                 unsigned    *n_fds)
180 {
181   _dbus_assert (message->locked);
182
183 #ifdef HAVE_UNIX_FD_PASSING
184   *fds = message->unix_fds;
185   *n_fds = message->n_unix_fds;
186 #else
187   *fds = NULL;
188   *n_fds = 0;
189 #endif
190 }
191
192 /**
193  * Sets the serial number of a message.
194  * This can only be done once on a message.
195  *
196  * DBusConnection will automatically set the serial to an appropriate value 
197  * when the message is sent; this function is only needed when encapsulating 
198  * messages in another protocol, or otherwise bypassing DBusConnection.
199  *
200  * @param message the message
201  * @param serial the serial
202  */
203 void 
204 dbus_message_set_serial (DBusMessage   *message,
205                          dbus_uint32_t  serial)
206 {
207   _dbus_return_if_fail (message != NULL);
208   _dbus_return_if_fail (!message->locked);
209
210   _dbus_header_set_serial (&message->header, serial);
211 }
212
213 /**
214  * Adds a counter to be incremented immediately with the size/unix fds
215  * of this message, and decremented by the size/unix fds of this
216  * message when this message if finalized.  The link contains a
217  * counter with its refcount already incremented, but the counter
218  * itself not incremented.  Ownership of link and counter refcount is
219  * passed to the message.
220  *
221  * This function may be called with locks held. As a result, the counter's
222  * notify function is not called; the caller is expected to either call
223  * _dbus_counter_notify() on the counter when they are no longer holding
224  * locks, or take the same action that would be taken by the notify function.
225  *
226  * @param message the message
227  * @param link link with counter as data
228  */
229 void
230 _dbus_message_add_counter_link (DBusMessage  *message,
231                                 DBusList     *link)
232 {
233   /* right now we don't recompute the delta when message
234    * size changes, and that's OK for current purposes
235    * I think, but could be important to change later.
236    * Do recompute it whenever there are no outstanding counters,
237    * since it's basically free.
238    */
239   if (message->counters == NULL)
240     {
241       message->size_counter_delta =
242         _dbus_string_get_length (&message->header.data) +
243         _dbus_string_get_length (&message->body);
244
245 #ifdef HAVE_UNIX_FD_PASSING
246       message->unix_fd_counter_delta = message->n_unix_fds;
247 #endif
248
249 #if 0
250       _dbus_verbose ("message has size %ld\n",
251                      message->size_counter_delta);
252 #endif
253     }
254
255   _dbus_list_append_link (&message->counters, link);
256
257   _dbus_counter_adjust_size (link->data, message->size_counter_delta);
258
259 #ifdef HAVE_UNIX_FD_PASSING
260   _dbus_counter_adjust_unix_fd (link->data, message->unix_fd_counter_delta);
261 #endif
262 }
263
264 /**
265  * Adds a counter to be incremented immediately with the size/unix fds
266  * of this message, and decremented by the size/unix fds of this
267  * message when this message if finalized.
268  *
269  * This function may be called with locks held. As a result, the counter's
270  * notify function is not called; the caller is expected to either call
271  * _dbus_counter_notify() on the counter when they are no longer holding
272  * locks, or take the same action that would be taken by the notify function.
273  *
274  * @param message the message
275  * @param counter the counter
276  * @returns #FALSE if no memory
277  */
278 dbus_bool_t
279 _dbus_message_add_counter (DBusMessage *message,
280                            DBusCounter *counter)
281 {
282   DBusList *link;
283
284   link = _dbus_list_alloc_link (counter);
285   if (link == NULL)
286     return FALSE;
287
288   _dbus_counter_ref (counter);
289   _dbus_message_add_counter_link (message, link);
290
291   return TRUE;
292 }
293
294 /**
295  * Removes a counter tracking the size/unix fds of this message, and
296  * decrements the counter by the size/unix fds of this message.
297  *
298  * @param message the message
299  * @param counter the counter
300  */
301 void
302 _dbus_message_remove_counter (DBusMessage  *message,
303                               DBusCounter  *counter)
304 {
305   DBusList *link;
306
307   link = _dbus_list_find_last (&message->counters,
308                                counter);
309   _dbus_assert (link != NULL);
310
311   _dbus_list_remove_link (&message->counters, link);
312
313   _dbus_counter_adjust_size (counter, - message->size_counter_delta);
314
315 #ifdef HAVE_UNIX_FD_PASSING
316   _dbus_counter_adjust_unix_fd (counter, - message->unix_fd_counter_delta);
317 #endif
318
319   _dbus_counter_notify (counter);
320   _dbus_counter_unref (counter);
321 }
322
323 /**
324  * Locks a message. Allows checking that applications don't keep a
325  * reference to a message in the outgoing queue and change it
326  * underneath us. Messages are locked when they enter the outgoing
327  * queue (dbus_connection_send_message()), and the library complains
328  * if the message is modified while locked. This function may also 
329  * called externally, for applications wrapping D-Bus in another protocol.
330  *
331  * @param message the message to lock.
332  */
333 void
334 dbus_message_lock (DBusMessage  *message)
335 {
336   if (!message->locked)
337     {
338       _dbus_header_update_lengths (&message->header,
339                                    _dbus_string_get_length (&message->body));
340
341       /* must have a signature if you have a body */
342       _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
343                     dbus_message_get_signature (message) != NULL);
344
345       message->locked = TRUE;
346     }
347 }
348
349 static dbus_bool_t
350 set_or_delete_string_field (DBusMessage *message,
351                             int          field,
352                             int          typecode,
353                             const char  *value)
354 {
355   if (value == NULL)
356     return _dbus_header_delete_field (&message->header, field);
357   else
358     return _dbus_header_set_field_basic (&message->header,
359                                          field,
360                                          typecode,
361                                          &value);
362 }
363
364 #if 0
365 /* Probably we don't need to use this */
366 /**
367  * Sets the signature of the message, i.e. the arguments in the
368  * message payload. The signature includes only "in" arguments for
369  * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
370  * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
371  * what you might expect (it does not include the signature of the
372  * entire C++-style method).
373  *
374  * The signature is a string made up of type codes such as
375  * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
376  * the value of #DBUS_TYPE_INVALID). The macros such as
377  * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
378  * may find it useful to use the string forms, such as
379  * #DBUS_TYPE_INT32_AS_STRING.
380  *
381  * An "unset" or #NULL signature is considered the same as an empty
382  * signature. In fact dbus_message_get_signature() will never return
383  * #NULL.
384  *
385  * @param message the message
386  * @param signature the type signature or #NULL to unset
387  * @returns #FALSE if no memory
388  */
389 static dbus_bool_t
390 _dbus_message_set_signature (DBusMessage *message,
391                              const char  *signature)
392 {
393   _dbus_return_val_if_fail (message != NULL, FALSE);
394   _dbus_return_val_if_fail (!message->locked, FALSE);
395   _dbus_return_val_if_fail (signature == NULL ||
396                             _dbus_check_is_valid_signature (signature));
397   /* can't delete the signature if you have a message body */
398   _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
399                             signature != NULL);
400
401   return set_or_delete_string_field (message,
402                                      DBUS_HEADER_FIELD_SIGNATURE,
403                                      DBUS_TYPE_SIGNATURE,
404                                      signature);
405 }
406 #endif
407
408 /* Message Cache
409  *
410  * We cache some DBusMessage to reduce the overhead of allocating
411  * them.  In my profiling this consistently made about an 8%
412  * difference.  It avoids the malloc for the message, the malloc for
413  * the slot list, the malloc for the header string and body string,
414  * and the associated free() calls. It does introduce another global
415  * lock which could be a performance issue in certain cases.
416  *
417  * For the echo client/server the round trip time goes from around
418  * .000077 to .000069 with the message cache on my laptop. The sysprof
419  * change is as follows (numbers are cumulative percentage):
420  *
421  *  with message cache implemented as array as it is now (0.000069 per):
422  *    new_empty_header           1.46
423  *      mutex_lock               0.56    # i.e. _DBUS_LOCK(message_cache)
424  *      mutex_unlock             0.25
425  *      self                     0.41
426  *    unref                      2.24
427  *      self                     0.68
428  *      list_clear               0.43
429  *      mutex_lock               0.33    # i.e. _DBUS_LOCK(message_cache)
430  *      mutex_unlock             0.25
431  *
432  *  with message cache implemented as list (0.000070 per roundtrip):
433  *    new_empty_header           2.72
434  *      list_pop_first           1.88
435  *    unref                      3.3
436  *      list_prepend             1.63
437  *
438  * without cache (0.000077 per roundtrip):
439  *    new_empty_header           6.7
440  *      string_init_preallocated 3.43
441  *        dbus_malloc            2.43
442  *      dbus_malloc0             2.59
443  *
444  *    unref                      4.02
445  *      string_free              1.82
446  *        dbus_free              1.63
447  *      dbus_free                0.71
448  *
449  * If you implement the message_cache with a list, the primary reason
450  * it's slower is that you add another thread lock (on the DBusList
451  * mempool).
452  */
453
454 /** Avoid caching huge messages */
455 #define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
456
457 /** Avoid caching too many messages */
458 #define MAX_MESSAGE_CACHE_SIZE    5
459
460 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
461 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
462 static int message_cache_count = 0;
463 static dbus_bool_t message_cache_shutdown_registered = FALSE;
464
465 static void
466 dbus_message_cache_shutdown (void *data)
467 {
468   int i;
469
470   _DBUS_LOCK (message_cache);
471
472   i = 0;
473   while (i < MAX_MESSAGE_CACHE_SIZE)
474     {
475       if (message_cache[i])
476         dbus_message_finalize (message_cache[i]);
477
478       ++i;
479     }
480
481   message_cache_count = 0;
482   message_cache_shutdown_registered = FALSE;
483
484   _DBUS_UNLOCK (message_cache);
485 }
486
487 /**
488  * Tries to get a message from the message cache.  The retrieved
489  * message will have junk in it, so it still needs to be cleared out
490  * in dbus_message_new_empty_header()
491  *
492  * @returns the message, or #NULL if none cached
493  */
494 static DBusMessage*
495 dbus_message_get_cached (void)
496 {
497   DBusMessage *message;
498   int i;
499
500   message = NULL;
501
502   _DBUS_LOCK (message_cache);
503
504   _dbus_assert (message_cache_count >= 0);
505
506   if (message_cache_count == 0)
507     {
508       _DBUS_UNLOCK (message_cache);
509       return NULL;
510     }
511
512   /* This is not necessarily true unless count > 0, and
513    * message_cache is uninitialized until the shutdown is
514    * registered
515    */
516   _dbus_assert (message_cache_shutdown_registered);
517
518   i = 0;
519   while (i < MAX_MESSAGE_CACHE_SIZE)
520     {
521       if (message_cache[i])
522         {
523           message = message_cache[i];
524           message_cache[i] = NULL;
525           message_cache_count -= 1;
526           break;
527         }
528       ++i;
529     }
530   _dbus_assert (message_cache_count >= 0);
531   _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
532   _dbus_assert (message != NULL);
533
534   _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
535
536   _dbus_assert (message->counters == NULL);
537   
538   _DBUS_UNLOCK (message_cache);
539
540   return message;
541 }
542
543 #ifdef HAVE_UNIX_FD_PASSING
544 static void
545 close_unix_fds(int *fds, unsigned *n_fds)
546 {
547   DBusError e;
548   int i;
549
550   if (*n_fds <= 0)
551     return;
552
553   dbus_error_init(&e);
554
555   for (i = 0; i < *n_fds; i++)
556     {
557       if (!_dbus_close(fds[i], &e))
558         {
559           _dbus_warn("Failed to close file descriptor: %s\n", e.message);
560           dbus_error_free(&e);
561         }
562     }
563
564   *n_fds = 0;
565
566   /* We don't free the array here, in case we can recycle it later */
567 }
568 #endif
569
570 static void
571 free_counter (void *element,
572               void *data)
573 {
574   DBusCounter *counter = element;
575   DBusMessage *message = data;
576
577   _dbus_counter_adjust_size (counter, - message->size_counter_delta);
578 #ifdef HAVE_UNIX_FD_PASSING
579   _dbus_counter_adjust_unix_fd (counter, - message->unix_fd_counter_delta);
580 #endif
581
582   _dbus_counter_notify (counter);
583   _dbus_counter_unref (counter);
584 }
585
586 /**
587  * Tries to cache a message, otherwise finalize it.
588  *
589  * @param message the message
590  */
591 static void
592 dbus_message_cache_or_finalize (DBusMessage *message)
593 {
594   dbus_bool_t was_cached;
595   int i;
596
597   _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
598
599   /* This calls application code and has to be done first thing
600    * without holding the lock
601    */
602   _dbus_data_slot_list_clear (&message->slot_list);
603
604   _dbus_list_foreach (&message->counters,
605                       free_counter, message);
606   _dbus_list_clear (&message->counters);
607
608 #ifdef HAVE_UNIX_FD_PASSING
609   close_unix_fds(message->unix_fds, &message->n_unix_fds);
610 #endif
611
612   was_cached = FALSE;
613
614   _DBUS_LOCK (message_cache);
615
616   if (!message_cache_shutdown_registered)
617     {
618       _dbus_assert (message_cache_count == 0);
619
620       if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
621         goto out;
622
623       i = 0;
624       while (i < MAX_MESSAGE_CACHE_SIZE)
625         {
626           message_cache[i] = NULL;
627           ++i;
628         }
629
630       message_cache_shutdown_registered = TRUE;
631     }
632
633   _dbus_assert (message_cache_count >= 0);
634
635   if ((_dbus_string_get_length (&message->header.data) +
636        _dbus_string_get_length (&message->body)) >
637       MAX_MESSAGE_SIZE_TO_CACHE)
638     goto out;
639
640   if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
641     goto out;
642
643   /* Find empty slot */
644   i = 0;
645   while (message_cache[i] != NULL)
646     ++i;
647
648   _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
649
650   _dbus_assert (message_cache[i] == NULL);
651   message_cache[i] = message;
652   message_cache_count += 1;
653   was_cached = TRUE;
654 #ifndef DBUS_DISABLE_CHECKS
655   message->in_cache = TRUE;
656 #endif
657
658  out:
659   _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
660
661   _DBUS_UNLOCK (message_cache);
662   
663   if (!was_cached)
664     dbus_message_finalize (message);
665 }
666
667 #ifndef DBUS_DISABLE_CHECKS
668 static dbus_bool_t
669 _dbus_message_iter_check (DBusMessageRealIter *iter)
670 {
671   char byte_order;
672
673   if (iter == NULL)
674     {
675       _dbus_warn_check_failed ("dbus message iterator is NULL\n");
676       return FALSE;
677     }
678
679   byte_order = _dbus_header_get_byte_order (&iter->message->header);
680
681   if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
682     {
683       if (iter->u.reader.byte_order != byte_order)
684         {
685           _dbus_warn_check_failed ("dbus message changed byte order since iterator was created\n");
686           return FALSE;
687         }
688       /* because we swap the message into compiler order when you init an iter */
689       _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
690     }
691   else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
692     {
693       if (iter->u.writer.byte_order != byte_order)
694         {
695           _dbus_warn_check_failed ("dbus message changed byte order since append iterator was created\n");
696           return FALSE;
697         }
698       /* because we swap the message into compiler order when you init an iter */
699       _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
700     }
701   else
702     {
703       _dbus_warn_check_failed ("dbus message iterator looks uninitialized or corrupted\n");
704       return FALSE;
705     }
706
707   if (iter->changed_stamp != iter->message->changed_stamp)
708     {
709       _dbus_warn_check_failed ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
710       return FALSE;
711     }
712
713   return TRUE;
714 }
715 #endif /* DBUS_DISABLE_CHECKS */
716
717 /**
718  * Implementation of the varargs arg-getting functions.
719  * dbus_message_get_args() is the place to go for complete
720  * documentation.
721  *
722  * @todo This may leak memory and file descriptors if parsing fails. See #21259
723  *
724  * @see dbus_message_get_args
725  * @param iter the message iter
726  * @param error error to be filled in
727  * @param first_arg_type type of the first argument
728  * @param var_args return location for first argument, followed by list of type/location pairs
729  * @returns #FALSE if error was set
730  */
731 dbus_bool_t
732 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
733                                     DBusError       *error,
734                                     int              first_arg_type,
735                                     va_list          var_args)
736 {
737   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
738   int spec_type, msg_type, i;
739   dbus_bool_t retval;
740
741   _dbus_assert (_dbus_message_iter_check (real));
742
743   retval = FALSE;
744
745   spec_type = first_arg_type;
746   i = 0;
747
748   while (spec_type != DBUS_TYPE_INVALID)
749     {
750       msg_type = dbus_message_iter_get_arg_type (iter);
751
752       if (msg_type != spec_type)
753         {
754           dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
755                           "Argument %d is specified to be of type \"%s\", but "
756                           "is actually of type \"%s\"\n", i,
757                           _dbus_type_to_string (spec_type),
758                           _dbus_type_to_string (msg_type));
759
760           goto out;
761         }
762
763       if (spec_type == DBUS_TYPE_UNIX_FD)
764         {
765 #ifdef HAVE_UNIX_FD_PASSING
766           DBusBasicValue idx;
767           int *pfd, nfd;
768
769           pfd = va_arg (var_args, int*);
770           _dbus_assert(pfd);
771
772           _dbus_type_reader_read_basic(&real->u.reader, &idx);
773
774           if (idx.u32 >= real->message->n_unix_fds)
775             {
776               dbus_set_error (error, DBUS_ERROR_INCONSISTENT_MESSAGE,
777                               "Message refers to file descriptor at index %i,"
778                               "but has only %i descriptors attached.\n",
779                               idx.u32,
780                               real->message->n_unix_fds);
781               goto out;
782             }
783
784           if ((nfd = _dbus_dup(real->message->unix_fds[idx.u32], error)) < 0)
785             goto out;
786
787           *pfd = nfd;
788 #else
789           dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
790                           "Platform does not support file desciptor passing.\n");
791           goto out;
792 #endif
793         }
794       else if (dbus_type_is_basic (spec_type))
795         {
796           DBusBasicValue *ptr;
797
798           ptr = va_arg (var_args, DBusBasicValue*);
799
800           _dbus_assert (ptr != NULL);
801
802           _dbus_type_reader_read_basic (&real->u.reader,
803                                         ptr);
804         }
805       else if (spec_type == DBUS_TYPE_ARRAY)
806         {
807           int element_type;
808           int spec_element_type;
809           const DBusBasicValue **ptr;
810           int *n_elements_p;
811           DBusTypeReader array;
812
813           spec_element_type = va_arg (var_args, int);
814           element_type = _dbus_type_reader_get_element_type (&real->u.reader);
815
816           if (spec_element_type != element_type)
817             {
818               dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
819                               "Argument %d is specified to be an array of \"%s\", but "
820                               "is actually an array of \"%s\"\n",
821                               i,
822                               _dbus_type_to_string (spec_element_type),
823                               _dbus_type_to_string (element_type));
824
825               goto out;
826             }
827
828           if (dbus_type_is_fixed (spec_element_type) &&
829               element_type != DBUS_TYPE_UNIX_FD)
830             {
831               ptr = va_arg (var_args, const DBusBasicValue**);
832               n_elements_p = va_arg (var_args, int*);
833
834               _dbus_assert (ptr != NULL);
835               _dbus_assert (n_elements_p != NULL);
836
837               _dbus_type_reader_recurse (&real->u.reader, &array);
838
839               _dbus_type_reader_read_fixed_multi (&array,
840                                                   (void *) ptr, n_elements_p);
841             }
842           else if (spec_element_type == DBUS_TYPE_STRING ||
843                    spec_element_type == DBUS_TYPE_SIGNATURE ||
844                    spec_element_type == DBUS_TYPE_OBJECT_PATH)
845             {
846               char ***str_array_p;
847               int n_elements;
848               char **str_array;
849
850               str_array_p = va_arg (var_args, char***);
851               n_elements_p = va_arg (var_args, int*);
852
853               _dbus_assert (str_array_p != NULL);
854               _dbus_assert (n_elements_p != NULL);
855
856               /* Count elements in the array */
857               _dbus_type_reader_recurse (&real->u.reader, &array);
858
859               n_elements = 0;
860               while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
861                 {
862                   ++n_elements;
863                   _dbus_type_reader_next (&array);
864                 }
865
866               str_array = dbus_new0 (char*, n_elements + 1);
867               if (str_array == NULL)
868                 {
869                   _DBUS_SET_OOM (error);
870                   goto out;
871                 }
872
873               /* Now go through and dup each string */
874               _dbus_type_reader_recurse (&real->u.reader, &array);
875
876               i = 0;
877               while (i < n_elements)
878                 {
879                   const char *s;
880                   _dbus_type_reader_read_basic (&array,
881                                                 (void *) &s);
882                   
883                   str_array[i] = _dbus_strdup (s);
884                   if (str_array[i] == NULL)
885                     {
886                       dbus_free_string_array (str_array);
887                       _DBUS_SET_OOM (error);
888                       goto out;
889                     }
890                   
891                   ++i;
892                   
893                   if (!_dbus_type_reader_next (&array))
894                     _dbus_assert (i == n_elements);
895                 }
896
897               _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
898               _dbus_assert (i == n_elements);
899               _dbus_assert (str_array[i] == NULL);
900
901               *str_array_p = str_array;
902               *n_elements_p = n_elements;
903             }
904 #ifndef DBUS_DISABLE_CHECKS
905           else
906             {
907               _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
908                           _DBUS_FUNCTION_NAME);
909               goto out;
910             }
911 #endif
912         }
913 #ifndef DBUS_DISABLE_CHECKS
914       else
915         {
916           _dbus_warn ("you can only read arrays and basic types with %s for now\n",
917                       _DBUS_FUNCTION_NAME);
918           goto out;
919         }
920 #endif
921
922       spec_type = va_arg (var_args, int);
923       if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
924         {
925           dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
926                           "Message has only %d arguments, but more were expected", i);
927           goto out;
928         }
929
930       i++;
931     }
932
933   retval = TRUE;
934
935  out:
936
937   return retval;
938 }
939
940 /** @} */
941
942 /**
943  * @defgroup DBusMessage DBusMessage
944  * @ingroup  DBus
945  * @brief Message to be sent or received over a #DBusConnection.
946  *
947  * A DBusMessage is the most basic unit of communication over a
948  * DBusConnection. A DBusConnection represents a stream of messages
949  * received from a remote application, and a stream of messages
950  * sent to a remote application.
951  *
952  * A message has a message type, returned from
953  * dbus_message_get_type().  This indicates whether the message is a
954  * method call, a reply to a method call, a signal, or an error reply.
955  *
956  * A message has header fields such as the sender, destination, method
957  * or signal name, and so forth. DBusMessage has accessor functions for
958  * these, such as dbus_message_get_member().
959  *
960  * Convenience functions dbus_message_is_method_call(), dbus_message_is_signal(),
961  * and dbus_message_is_error() check several header fields at once and are
962  * slightly more efficient than checking the header fields with individual
963  * accessor functions.
964  *
965  * Finally, a message has arguments. The number and types of arguments
966  * are in the message's signature header field (accessed with
967  * dbus_message_get_signature()).  Simple argument values are usually
968  * retrieved with dbus_message_get_args() but more complex values such
969  * as structs may require the use of #DBusMessageIter.
970  *
971  * The D-Bus specification goes into some more detail about header fields and
972  * message types.
973  * 
974  * @{
975  */
976
977 /**
978  * @typedef DBusMessage
979  *
980  * Opaque data type representing a message received from or to be
981  * sent to another application.
982  */
983
984 /**
985  * Returns the serial of a message or 0 if none has been specified.
986  * The message's serial number is provided by the application sending
987  * the message and is used to identify replies to this message.
988  *
989  * All messages received on a connection will have a serial provided
990  * by the remote application.
991  *
992  * For messages you're sending, dbus_connection_send() will assign a
993  * serial and return it to you.
994  *
995  * @param message the message
996  * @returns the serial
997  */
998 dbus_uint32_t
999 dbus_message_get_serial (DBusMessage *message)
1000 {
1001   _dbus_return_val_if_fail (message != NULL, 0);
1002
1003   return _dbus_header_get_serial (&message->header);
1004 }
1005
1006 /**
1007  * Sets the reply serial of a message (the serial of the message this
1008  * is a reply to).
1009  *
1010  * @param message the message
1011  * @param reply_serial the serial we're replying to
1012  * @returns #FALSE if not enough memory
1013  */
1014 dbus_bool_t
1015 dbus_message_set_reply_serial (DBusMessage   *message,
1016                                dbus_uint32_t  reply_serial)
1017 {
1018   _dbus_return_val_if_fail (message != NULL, FALSE);
1019   _dbus_return_val_if_fail (!message->locked, FALSE);
1020   _dbus_return_val_if_fail (reply_serial != 0, FALSE); /* 0 is invalid */
1021
1022   return _dbus_header_set_field_basic (&message->header,
1023                                        DBUS_HEADER_FIELD_REPLY_SERIAL,
1024                                        DBUS_TYPE_UINT32,
1025                                        &reply_serial);
1026 }
1027
1028 /**
1029  * Returns the serial that the message is a reply to or 0 if none.
1030  *
1031  * @param message the message
1032  * @returns the reply serial
1033  */
1034 dbus_uint32_t
1035 dbus_message_get_reply_serial  (DBusMessage *message)
1036 {
1037   dbus_uint32_t v_UINT32;
1038
1039   _dbus_return_val_if_fail (message != NULL, 0);
1040
1041   if (_dbus_header_get_field_basic (&message->header,
1042                                     DBUS_HEADER_FIELD_REPLY_SERIAL,
1043                                     DBUS_TYPE_UINT32,
1044                                     &v_UINT32))
1045     return v_UINT32;
1046   else
1047     return 0;
1048 }
1049
1050 static void
1051 dbus_message_finalize (DBusMessage *message)
1052 {
1053   _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
1054
1055   /* This calls application callbacks! */
1056   _dbus_data_slot_list_free (&message->slot_list);
1057
1058   _dbus_list_foreach (&message->counters,
1059                       free_counter, message);
1060   _dbus_list_clear (&message->counters);
1061
1062   _dbus_header_free (&message->header);
1063   _dbus_string_free (&message->body);
1064
1065 #ifdef HAVE_UNIX_FD_PASSING
1066   close_unix_fds(message->unix_fds, &message->n_unix_fds);
1067   dbus_free(message->unix_fds);
1068 #endif
1069
1070   _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
1071
1072   dbus_free (message);
1073 }
1074
1075 static DBusMessage*
1076 dbus_message_new_empty_header (void)
1077 {
1078   DBusMessage *message;
1079   dbus_bool_t from_cache;
1080
1081   message = dbus_message_get_cached ();
1082
1083   if (message != NULL)
1084     {
1085       from_cache = TRUE;
1086     }
1087   else
1088     {
1089       from_cache = FALSE;
1090       message = dbus_new0 (DBusMessage, 1);
1091       if (message == NULL)
1092         return NULL;
1093 #ifndef DBUS_DISABLE_CHECKS
1094       message->generation = _dbus_current_generation;
1095 #endif
1096
1097 #ifdef HAVE_UNIX_FD_PASSING
1098       message->unix_fds = NULL;
1099       message->n_unix_fds_allocated = 0;
1100 #endif
1101     }
1102
1103   _dbus_atomic_inc (&message->refcount);
1104
1105   message->locked = FALSE;
1106 #ifndef DBUS_DISABLE_CHECKS
1107   message->in_cache = FALSE;
1108 #endif
1109   message->counters = NULL;
1110   message->size_counter_delta = 0;
1111   message->changed_stamp = 0;
1112
1113 #ifdef HAVE_UNIX_FD_PASSING
1114   message->n_unix_fds = 0;
1115   message->n_unix_fds_allocated = 0;
1116   message->unix_fd_counter_delta = 0;
1117 #endif
1118
1119   if (!from_cache)
1120     _dbus_data_slot_list_init (&message->slot_list);
1121
1122   if (from_cache)
1123     {
1124       _dbus_header_reinit (&message->header);
1125       _dbus_string_set_length (&message->body, 0);
1126     }
1127   else
1128     {
1129       if (!_dbus_header_init (&message->header))
1130         {
1131           dbus_free (message);
1132           return NULL;
1133         }
1134
1135       if (!_dbus_string_init_preallocated (&message->body, 32))
1136         {
1137           _dbus_header_free (&message->header);
1138           dbus_free (message);
1139           return NULL;
1140         }
1141     }
1142
1143   return message;
1144 }
1145
1146 /**
1147  * Constructs a new message of the given message type.
1148  * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1149  * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1150  *
1151  * Usually you want to use dbus_message_new_method_call(),
1152  * dbus_message_new_method_return(), dbus_message_new_signal(),
1153  * or dbus_message_new_error() instead.
1154  * 
1155  * @param message_type type of message
1156  * @returns new message or #NULL if no memory
1157  */
1158 DBusMessage*
1159 dbus_message_new (int message_type)
1160 {
1161   DBusMessage *message;
1162
1163   _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1164
1165   message = dbus_message_new_empty_header ();
1166   if (message == NULL)
1167     return NULL;
1168
1169   if (!_dbus_header_create (&message->header,
1170                             DBUS_COMPILER_BYTE_ORDER,
1171                             message_type,
1172                             NULL, NULL, NULL, NULL, NULL))
1173     {
1174       dbus_message_unref (message);
1175       return NULL;
1176     }
1177
1178   return message;
1179 }
1180
1181 /**
1182  * Constructs a new message to invoke a method on a remote
1183  * object. Returns #NULL if memory can't be allocated for the
1184  * message. The destination may be #NULL in which case no destination
1185  * is set; this is appropriate when using D-Bus in a peer-to-peer
1186  * context (no message bus). The interface may be #NULL, which means
1187  * that if multiple methods with the given name exist it is undefined
1188  * which one will be invoked.
1189  *
1190  * The path and method names may not be #NULL.
1191  *
1192  * Destination, path, interface, and method name can't contain
1193  * any invalid characters (see the D-Bus specification).
1194  * 
1195  * @param destination name that the message should be sent to or #NULL
1196  * @param path object path the message should be sent to
1197  * @param interface interface to invoke method on, or #NULL
1198  * @param method method to invoke
1199  *
1200  * @returns a new DBusMessage, free with dbus_message_unref()
1201  */
1202 DBusMessage*
1203 dbus_message_new_method_call (const char *destination,
1204                               const char *path,
1205                               const char *interface,
1206                               const char *method)
1207 {
1208   DBusMessage *message;
1209
1210   _dbus_return_val_if_fail (path != NULL, NULL);
1211   _dbus_return_val_if_fail (method != NULL, NULL);
1212   _dbus_return_val_if_fail (destination == NULL ||
1213                             _dbus_check_is_valid_bus_name (destination), NULL);
1214   _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1215   _dbus_return_val_if_fail (interface == NULL ||
1216                             _dbus_check_is_valid_interface (interface), NULL);
1217   _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
1218
1219   message = dbus_message_new_empty_header ();
1220   if (message == NULL)
1221     return NULL;
1222
1223   if (!_dbus_header_create (&message->header,
1224                             DBUS_COMPILER_BYTE_ORDER,
1225                             DBUS_MESSAGE_TYPE_METHOD_CALL,
1226                             destination, path, interface, method, NULL))
1227     {
1228       dbus_message_unref (message);
1229       return NULL;
1230     }
1231
1232   return message;
1233 }
1234
1235 /**
1236  * Constructs a message that is a reply to a method call. Returns
1237  * #NULL if memory can't be allocated for the message.
1238  *
1239  * @param method_call the message being replied to
1240  * @returns a new DBusMessage, free with dbus_message_unref()
1241  */
1242 DBusMessage*
1243 dbus_message_new_method_return (DBusMessage *method_call)
1244 {
1245   DBusMessage *message;
1246   const char *sender;
1247
1248   _dbus_return_val_if_fail (method_call != NULL, NULL);
1249
1250   sender = dbus_message_get_sender (method_call);
1251
1252   /* sender is allowed to be null here in peer-to-peer case */
1253
1254   message = dbus_message_new_empty_header ();
1255   if (message == NULL)
1256     return NULL;
1257
1258   if (!_dbus_header_create (&message->header,
1259                             DBUS_COMPILER_BYTE_ORDER,
1260                             DBUS_MESSAGE_TYPE_METHOD_RETURN,
1261                             sender, NULL, NULL, NULL, NULL))
1262     {
1263       dbus_message_unref (message);
1264       return NULL;
1265     }
1266
1267   dbus_message_set_no_reply (message, TRUE);
1268
1269   if (!dbus_message_set_reply_serial (message,
1270                                       dbus_message_get_serial (method_call)))
1271     {
1272       dbus_message_unref (message);
1273       return NULL;
1274     }
1275
1276   return message;
1277 }
1278
1279 /**
1280  * Constructs a new message representing a signal emission. Returns
1281  * #NULL if memory can't be allocated for the message.  A signal is
1282  * identified by its originating object path, interface, and the name
1283  * of the signal.
1284  *
1285  * Path, interface, and signal name must all be valid (the D-Bus
1286  * specification defines the syntax of these fields).
1287  * 
1288  * @param path the path to the object emitting the signal
1289  * @param interface the interface the signal is emitted from
1290  * @param name name of the signal
1291  * @returns a new DBusMessage, free with dbus_message_unref()
1292  */
1293 DBusMessage*
1294 dbus_message_new_signal (const char *path,
1295                          const char *interface,
1296                          const char *name)
1297 {
1298   DBusMessage *message;
1299
1300   _dbus_return_val_if_fail (path != NULL, NULL);
1301   _dbus_return_val_if_fail (interface != NULL, NULL);
1302   _dbus_return_val_if_fail (name != NULL, NULL);
1303   _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1304   _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
1305   _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
1306
1307   message = dbus_message_new_empty_header ();
1308   if (message == NULL)
1309     return NULL;
1310
1311   if (!_dbus_header_create (&message->header,
1312                             DBUS_COMPILER_BYTE_ORDER,
1313                             DBUS_MESSAGE_TYPE_SIGNAL,
1314                             NULL, path, interface, name, NULL))
1315     {
1316       dbus_message_unref (message);
1317       return NULL;
1318     }
1319
1320   dbus_message_set_no_reply (message, TRUE);
1321
1322   return message;
1323 }
1324
1325 /**
1326  * Creates a new message that is an error reply to another message.
1327  * Error replies are most common in response to method calls, but
1328  * can be returned in reply to any message.
1329  *
1330  * The error name must be a valid error name according to the syntax
1331  * given in the D-Bus specification. If you don't want to make
1332  * up an error name just use #DBUS_ERROR_FAILED.
1333  *
1334  * @param reply_to the message we're replying to
1335  * @param error_name the error name
1336  * @param error_message the error message string (or #NULL for none, but please give a message)
1337  * @returns a new error message object, free with dbus_message_unref()
1338  */
1339 DBusMessage*
1340 dbus_message_new_error (DBusMessage *reply_to,
1341                         const char  *error_name,
1342                         const char  *error_message)
1343 {
1344   DBusMessage *message;
1345   const char *sender;
1346   DBusMessageIter iter;
1347
1348   _dbus_return_val_if_fail (reply_to != NULL, NULL);
1349   _dbus_return_val_if_fail (error_name != NULL, NULL);
1350   _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1351
1352   sender = dbus_message_get_sender (reply_to);
1353
1354   /* sender may be NULL for non-message-bus case or
1355    * when the message bus is dealing with an unregistered
1356    * connection.
1357    */
1358   message = dbus_message_new_empty_header ();
1359   if (message == NULL)
1360     return NULL;
1361
1362   if (!_dbus_header_create (&message->header,
1363                             DBUS_COMPILER_BYTE_ORDER,
1364                             DBUS_MESSAGE_TYPE_ERROR,
1365                             sender, NULL, NULL, NULL, error_name))
1366     {
1367       dbus_message_unref (message);
1368       return NULL;
1369     }
1370
1371   dbus_message_set_no_reply (message, TRUE);
1372
1373   if (!dbus_message_set_reply_serial (message,
1374                                       dbus_message_get_serial (reply_to)))
1375     {
1376       dbus_message_unref (message);
1377       return NULL;
1378     }
1379
1380   if (error_message != NULL)
1381     {
1382       dbus_message_iter_init_append (message, &iter);
1383       if (!dbus_message_iter_append_basic (&iter,
1384                                            DBUS_TYPE_STRING,
1385                                            &error_message))
1386         {
1387           dbus_message_unref (message);
1388           return NULL;
1389         }
1390     }
1391
1392   return message;
1393 }
1394
1395 /**
1396  * Creates a new message that is an error reply to another message, allowing
1397  * you to use printf formatting.
1398  *
1399  * See dbus_message_new_error() for details - this function is the same
1400  * aside from the printf formatting.
1401  *
1402  * @todo add _DBUS_GNUC_PRINTF to this (requires moving _DBUS_GNUC_PRINTF to
1403  * public header, see DBUS_DEPRECATED for an example)
1404  * 
1405  * @param reply_to the original message
1406  * @param error_name the error name
1407  * @param error_format the error message format as with printf
1408  * @param ... format string arguments
1409  * @returns a new error message
1410  */
1411 DBusMessage*
1412 dbus_message_new_error_printf (DBusMessage *reply_to,
1413                                const char  *error_name,
1414                                const char  *error_format,
1415                                ...)
1416 {
1417   va_list args;
1418   DBusString str;
1419   DBusMessage *message;
1420
1421   _dbus_return_val_if_fail (reply_to != NULL, NULL);
1422   _dbus_return_val_if_fail (error_name != NULL, NULL);
1423   _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1424
1425   if (!_dbus_string_init (&str))
1426     return NULL;
1427
1428   va_start (args, error_format);
1429
1430   if (_dbus_string_append_printf_valist (&str, error_format, args))
1431     message = dbus_message_new_error (reply_to, error_name,
1432                                       _dbus_string_get_const_data (&str));
1433   else
1434     message = NULL;
1435
1436   _dbus_string_free (&str);
1437
1438   va_end (args);
1439
1440   return message;
1441 }
1442
1443
1444 /**
1445  * Creates a new message that is an exact replica of the message
1446  * specified, except that its refcount is set to 1, its message serial
1447  * is reset to 0, and if the original message was "locked" (in the
1448  * outgoing message queue and thus not modifiable) the new message
1449  * will not be locked.
1450  *
1451  * @todo This function can't be used in programs that try to recover from OOM errors.
1452  *
1453  * @param message the message
1454  * @returns the new message.or #NULL if not enough memory or Unix file descriptors (in case the message to copy includes Unix file descriptors) can be allocated.
1455  */
1456 DBusMessage *
1457 dbus_message_copy (const DBusMessage *message)
1458 {
1459   DBusMessage *retval;
1460
1461   _dbus_return_val_if_fail (message != NULL, NULL);
1462
1463   retval = dbus_new0 (DBusMessage, 1);
1464   if (retval == NULL)
1465     return NULL;
1466
1467   _dbus_atomic_inc (&retval->refcount);
1468
1469   retval->locked = FALSE;
1470 #ifndef DBUS_DISABLE_CHECKS
1471   retval->generation = message->generation;
1472 #endif
1473
1474   if (!_dbus_header_copy (&message->header, &retval->header))
1475     {
1476       dbus_free (retval);
1477       return NULL;
1478     }
1479
1480   if (!_dbus_string_init_preallocated (&retval->body,
1481                                        _dbus_string_get_length (&message->body)))
1482     {
1483       _dbus_header_free (&retval->header);
1484       dbus_free (retval);
1485       return NULL;
1486     }
1487
1488   if (!_dbus_string_copy (&message->body, 0,
1489                           &retval->body, 0))
1490     goto failed_copy;
1491
1492 #ifdef HAVE_UNIX_FD_PASSING
1493   retval->unix_fds = dbus_new(int, message->n_unix_fds);
1494   if (retval->unix_fds == NULL && message->n_unix_fds > 0)
1495     goto failed_copy;
1496
1497   retval->n_unix_fds_allocated = message->n_unix_fds;
1498
1499   for (retval->n_unix_fds = 0;
1500        retval->n_unix_fds < message->n_unix_fds;
1501        retval->n_unix_fds++)
1502     {
1503       retval->unix_fds[retval->n_unix_fds] = _dbus_dup(message->unix_fds[retval->n_unix_fds], NULL);
1504
1505       if (retval->unix_fds[retval->n_unix_fds] < 0)
1506         goto failed_copy;
1507     }
1508
1509 #endif
1510
1511   return retval;
1512
1513  failed_copy:
1514   _dbus_header_free (&retval->header);
1515   _dbus_string_free (&retval->body);
1516
1517 #ifdef HAVE_UNIX_FD_PASSING
1518   close_unix_fds(retval->unix_fds, &retval->n_unix_fds);
1519   dbus_free(retval->unix_fds);
1520 #endif
1521
1522   dbus_free (retval);
1523
1524   return NULL;
1525 }
1526
1527
1528 /**
1529  * Increments the reference count of a DBusMessage.
1530  *
1531  * @param message the message
1532  * @returns the message
1533  * @see dbus_message_unref
1534  */
1535 DBusMessage *
1536 dbus_message_ref (DBusMessage *message)
1537 {
1538 #ifndef DBUS_DISABLE_ASSERT
1539   dbus_int32_t old_refcount;
1540 #endif
1541
1542   _dbus_return_val_if_fail (message != NULL, NULL);
1543   _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1544   _dbus_return_val_if_fail (!message->in_cache, NULL);
1545
1546 #ifdef DBUS_DISABLE_ASSERT
1547   _dbus_atomic_inc (&message->refcount);
1548 #else
1549   old_refcount = _dbus_atomic_inc (&message->refcount);
1550   _dbus_assert (old_refcount >= 1);
1551 #endif
1552
1553   return message;
1554 }
1555
1556 /**
1557  * Decrements the reference count of a DBusMessage, freeing the
1558  * message if the count reaches 0.
1559  *
1560  * @param message the message
1561  * @see dbus_message_ref
1562  */
1563 void
1564 dbus_message_unref (DBusMessage *message)
1565 {
1566  dbus_int32_t old_refcount;
1567
1568   _dbus_return_if_fail (message != NULL);
1569   _dbus_return_if_fail (message->generation == _dbus_current_generation);
1570   _dbus_return_if_fail (!message->in_cache);
1571
1572   old_refcount = _dbus_atomic_dec (&message->refcount);
1573
1574   _dbus_assert (old_refcount >= 1);
1575
1576   if (old_refcount == 1)
1577     {
1578       /* Calls application callbacks! */
1579       dbus_message_cache_or_finalize (message);
1580     }
1581 }
1582
1583 /**
1584  * Gets the type of a message. Types include
1585  * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1586  * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1587  * types are allowed and all code must silently ignore messages of
1588  * unknown type. #DBUS_MESSAGE_TYPE_INVALID will never be returned.
1589  *
1590  * @param message the message
1591  * @returns the type of the message
1592  */
1593 int
1594 dbus_message_get_type (DBusMessage *message)
1595 {
1596   _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1597
1598   return _dbus_header_get_message_type (&message->header);
1599 }
1600
1601 /**
1602  * Appends fields to a message given a variable argument list. The
1603  * variable argument list should contain the type of each argument
1604  * followed by the value to append. Appendable types are basic types,
1605  * and arrays of fixed-length basic types (except arrays of Unix file
1606  * descriptors). To append variable-length basic types, or any more
1607  * complex value, you have to use an iterator rather than this
1608  * function.
1609  *
1610  * To append a basic type, specify its type code followed by the
1611  * address of the value. For example:
1612  *
1613  * @code
1614  *
1615  * dbus_int32_t v_INT32 = 42;
1616  * const char *v_STRING = "Hello World";
1617  * dbus_message_append_args (message,
1618  *                           DBUS_TYPE_INT32, &v_INT32,
1619  *                           DBUS_TYPE_STRING, &v_STRING,
1620  *                           DBUS_TYPE_INVALID);
1621  * @endcode
1622  *
1623  * To append an array of fixed-length basic types (except Unix file
1624  * descriptors), pass in the DBUS_TYPE_ARRAY typecode, the element
1625  * typecode, the address of the array pointer, and a 32-bit integer
1626  * giving the number of elements in the array. So for example: @code
1627  * const dbus_int32_t array[] = { 1, 2, 3 }; const dbus_int32_t
1628  * *v_ARRAY = array; dbus_message_append_args (message,
1629  * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3, DBUS_TYPE_INVALID);
1630  * @endcode
1631  *
1632  * This function does not support arrays of Unix file descriptors. If
1633  * you need those you need to manually recurse into the array.
1634  *
1635  * For Unix file descriptors this function will internally duplicate
1636  * the descriptor you passed in. Hence you may close the descriptor
1637  * immediately after this call.
1638  *
1639  * @warning in C, given "int array[]", "&array == array" (the
1640  * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1641  * So if you're using an array instead of a pointer you have to create
1642  * a pointer variable, assign the array to it, then take the address
1643  * of the pointer variable. For strings it works to write
1644  * const char *array = "Hello" and then use &array though.
1645  *
1646  * The last argument to this function must be #DBUS_TYPE_INVALID,
1647  * marking the end of the argument list. If you don't do this
1648  * then libdbus won't know to stop and will read invalid memory.
1649  *
1650  * String/signature/path arrays should be passed in as "const char***
1651  * address_of_array" and "int n_elements"
1652  *
1653  * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1654  *
1655  * @todo If this fails due to lack of memory, the message is hosed and
1656  * you have to start over building the whole message.
1657  *
1658  * @param message the message
1659  * @param first_arg_type type of the first argument
1660  * @param ... value of first argument, list of additional type-value pairs
1661  * @returns #TRUE on success
1662  */
1663 dbus_bool_t
1664 dbus_message_append_args (DBusMessage *message,
1665                           int          first_arg_type,
1666                           ...)
1667 {
1668   dbus_bool_t retval;
1669   va_list var_args;
1670
1671   _dbus_return_val_if_fail (message != NULL, FALSE);
1672
1673   va_start (var_args, first_arg_type);
1674   retval = dbus_message_append_args_valist (message,
1675                                             first_arg_type,
1676                                             var_args);
1677   va_end (var_args);
1678
1679   return retval;
1680 }
1681
1682 /**
1683  * Like dbus_message_append_args() but takes a va_list for use by language bindings.
1684  *
1685  * @todo for now, if this function fails due to OOM it will leave
1686  * the message half-written and you have to discard the message
1687  * and start over.
1688  *
1689  * @see dbus_message_append_args.
1690  * @param message the message
1691  * @param first_arg_type type of first argument
1692  * @param var_args value of first argument, then list of type/value pairs
1693  * @returns #TRUE on success
1694  */
1695 dbus_bool_t
1696 dbus_message_append_args_valist (DBusMessage *message,
1697                                  int          first_arg_type,
1698                                  va_list      var_args)
1699 {
1700   int type;
1701   DBusMessageIter iter;
1702
1703   _dbus_return_val_if_fail (message != NULL, FALSE);
1704
1705   type = first_arg_type;
1706
1707   dbus_message_iter_init_append (message, &iter);
1708
1709   while (type != DBUS_TYPE_INVALID)
1710     {
1711       if (dbus_type_is_basic (type))
1712         {
1713           const DBusBasicValue *value;
1714           value = va_arg (var_args, const DBusBasicValue*);
1715
1716           if (!dbus_message_iter_append_basic (&iter,
1717                                                type,
1718                                                value))
1719             goto failed;
1720         }
1721       else if (type == DBUS_TYPE_ARRAY)
1722         {
1723           int element_type;
1724           DBusMessageIter array;
1725           char buf[2];
1726
1727           element_type = va_arg (var_args, int);
1728               
1729           buf[0] = element_type;
1730           buf[1] = '\0';
1731           if (!dbus_message_iter_open_container (&iter,
1732                                                  DBUS_TYPE_ARRAY,
1733                                                  buf,
1734                                                  &array))
1735             goto failed;
1736
1737           if (dbus_type_is_fixed (element_type) &&
1738               element_type != DBUS_TYPE_UNIX_FD)
1739             {
1740               const DBusBasicValue **value;
1741               int n_elements;
1742
1743               value = va_arg (var_args, const DBusBasicValue**);
1744               n_elements = va_arg (var_args, int);
1745               
1746               if (!dbus_message_iter_append_fixed_array (&array,
1747                                                          element_type,
1748                                                          value,
1749                                                          n_elements)) {
1750                 dbus_message_iter_abandon_container (&iter, &array);
1751                 goto failed;
1752               }
1753             }
1754           else if (element_type == DBUS_TYPE_STRING ||
1755                    element_type == DBUS_TYPE_SIGNATURE ||
1756                    element_type == DBUS_TYPE_OBJECT_PATH)
1757             {
1758               const char ***value_p;
1759               const char **value;
1760               int n_elements;
1761               int i;
1762               
1763               value_p = va_arg (var_args, const char***);
1764               n_elements = va_arg (var_args, int);
1765
1766               value = *value_p;
1767               
1768               i = 0;
1769               while (i < n_elements)
1770                 {
1771                   if (!dbus_message_iter_append_basic (&array,
1772                                                        element_type,
1773                                                        &value[i])) {
1774                     dbus_message_iter_abandon_container (&iter, &array);
1775                     goto failed;
1776                   }
1777                   ++i;
1778                 }
1779             }
1780           else
1781             {
1782               _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1783                           _dbus_type_to_string (element_type),
1784                           _DBUS_FUNCTION_NAME);
1785               goto failed;
1786             }
1787
1788           if (!dbus_message_iter_close_container (&iter, &array))
1789             goto failed;
1790         }
1791 #ifndef DBUS_DISABLE_CHECKS
1792       else
1793         {
1794           _dbus_warn ("type %s isn't supported yet in %s\n",
1795                       _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1796           goto failed;
1797         }
1798 #endif
1799
1800       type = va_arg (var_args, int);
1801     }
1802
1803   return TRUE;
1804
1805  failed:
1806   return FALSE;
1807 }
1808
1809 /**
1810  * Gets arguments from a message given a variable argument list.  The
1811  * supported types include those supported by
1812  * dbus_message_append_args(); that is, basic types and arrays of
1813  * fixed-length basic types.  The arguments are the same as they would
1814  * be for dbus_message_iter_get_basic() or
1815  * dbus_message_iter_get_fixed_array().
1816  *
1817  * In addition to those types, arrays of string, object path, and
1818  * signature are supported; but these are returned as allocated memory
1819  * and must be freed with dbus_free_string_array(), while the other
1820  * types are returned as const references. To get a string array
1821  * pass in "char ***array_location" and "int *n_elements".
1822  *
1823  * Similar to dbus_message_get_fixed_array() this function does not
1824  * support arrays of type DBUS_TYPE_UNIX_FD. If you need to parse
1825  * messages with arrays of Unix file descriptors you need to recurse
1826  * into the array manually.
1827  *
1828  * Unix file descriptors that are read with this function will have
1829  * the FD_CLOEXEC flag set. If you need them without this flag set,
1830  * make sure to unset it with fcntl().
1831  *
1832  * The variable argument list should contain the type of the argument
1833  * followed by a pointer to where the value should be stored. The list
1834  * is terminated with #DBUS_TYPE_INVALID.
1835  *
1836  * Except for string arrays, the returned values are constant; do not
1837  * free them. They point into the #DBusMessage.
1838  *
1839  * If the requested arguments are not present, or do not have the
1840  * requested types, then an error will be set.
1841  *
1842  * If more arguments than requested are present, the requested
1843  * arguments are returned and the extra arguments are ignored.
1844  * 
1845  * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1846  *
1847  * @param message the message
1848  * @param error error to be filled in on failure
1849  * @param first_arg_type the first argument type
1850  * @param ... location for first argument value, then list of type-location pairs
1851  * @returns #FALSE if the error was set
1852  */
1853 dbus_bool_t
1854 dbus_message_get_args (DBusMessage     *message,
1855                        DBusError       *error,
1856                        int              first_arg_type,
1857                        ...)
1858 {
1859   dbus_bool_t retval;
1860   va_list var_args;
1861
1862   _dbus_return_val_if_fail (message != NULL, FALSE);
1863   _dbus_return_val_if_error_is_set (error, FALSE);
1864
1865   va_start (var_args, first_arg_type);
1866   retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1867   va_end (var_args);
1868
1869   return retval;
1870 }
1871
1872 /**
1873  * Like dbus_message_get_args but takes a va_list for use by language bindings.
1874  *
1875  * @see dbus_message_get_args
1876  * @param message the message
1877  * @param error error to be filled in
1878  * @param first_arg_type type of the first argument
1879  * @param var_args return location for first argument, followed by list of type/location pairs
1880  * @returns #FALSE if error was set
1881  */
1882 dbus_bool_t
1883 dbus_message_get_args_valist (DBusMessage     *message,
1884                               DBusError       *error,
1885                               int              first_arg_type,
1886                               va_list          var_args)
1887 {
1888   DBusMessageIter iter;
1889
1890   _dbus_return_val_if_fail (message != NULL, FALSE);
1891   _dbus_return_val_if_error_is_set (error, FALSE);
1892
1893   dbus_message_iter_init (message, &iter);
1894   return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1895 }
1896
1897 static void
1898 _dbus_message_iter_init_common (DBusMessage         *message,
1899                                 DBusMessageRealIter *real,
1900                                 int                  iter_type)
1901 {
1902   _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1903
1904   /* Since the iterator will read or write who-knows-what from the
1905    * message, we need to get in the right byte order
1906    */
1907   ensure_byte_order (message);
1908   
1909   real->message = message;
1910   real->changed_stamp = message->changed_stamp;
1911   real->iter_type = iter_type;
1912   real->sig_refcount = 0;
1913 }
1914
1915 /**
1916  * Initializes a #DBusMessageIter for reading the arguments of the
1917  * message passed in.
1918  *
1919  * When possible, dbus_message_get_args() is much more convenient.
1920  * Some types of argument can only be read with #DBusMessageIter
1921  * however.
1922  *
1923  * The easiest way to iterate is like this: 
1924  * @code
1925  * dbus_message_iter_init (message, &iter);
1926  * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1927  *   dbus_message_iter_next (&iter);
1928  * @endcode
1929  *
1930  * #DBusMessageIter contains no allocated memory; it need not be
1931  * freed, and can be copied by assignment or memcpy().
1932  * 
1933  * @param message the message
1934  * @param iter pointer to an iterator to initialize
1935  * @returns #FALSE if the message has no arguments
1936  */
1937 dbus_bool_t
1938 dbus_message_iter_init (DBusMessage     *message,
1939                         DBusMessageIter *iter)
1940 {
1941   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1942   const DBusString *type_str;
1943   int type_pos;
1944
1945   _dbus_return_val_if_fail (message != NULL, FALSE);
1946   _dbus_return_val_if_fail (iter != NULL, FALSE);
1947
1948   get_const_signature (&message->header, &type_str, &type_pos);
1949
1950   _dbus_message_iter_init_common (message, real,
1951                                   DBUS_MESSAGE_ITER_TYPE_READER);
1952
1953   _dbus_type_reader_init (&real->u.reader,
1954                           _dbus_header_get_byte_order (&message->header),
1955                           type_str, type_pos,
1956                           &message->body,
1957                           0);
1958
1959   return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1960 }
1961
1962 /**
1963  * Checks if an iterator has any more fields.
1964  *
1965  * @param iter the message iter
1966  * @returns #TRUE if there are more fields following
1967  */
1968 dbus_bool_t
1969 dbus_message_iter_has_next (DBusMessageIter *iter)
1970 {
1971   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1972
1973   _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1974   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1975
1976   return _dbus_type_reader_has_next (&real->u.reader);
1977 }
1978
1979 /**
1980  * Moves the iterator to the next field, if any. If there's no next
1981  * field, returns #FALSE. If the iterator moves forward, returns
1982  * #TRUE.
1983  *
1984  * @param iter the message iter
1985  * @returns #TRUE if the iterator was moved to the next field
1986  */
1987 dbus_bool_t
1988 dbus_message_iter_next (DBusMessageIter *iter)
1989 {
1990   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1991
1992   _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1993   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1994
1995   return _dbus_type_reader_next (&real->u.reader);
1996 }
1997
1998 /**
1999  * Returns the argument type of the argument that the message iterator
2000  * points to. If the iterator is at the end of the message, returns
2001  * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
2002  *
2003  * @code
2004  * dbus_message_iter_init (message, &iter);
2005  * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
2006  *   dbus_message_iter_next (&iter);
2007  * @endcode
2008  *
2009  * @param iter the message iter
2010  * @returns the argument type
2011  */
2012 int
2013 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
2014 {
2015   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2016
2017   _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2018   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
2019
2020   return _dbus_type_reader_get_current_type (&real->u.reader);
2021 }
2022
2023 /**
2024  * Returns the element type of the array that the message iterator
2025  * points to. Note that you need to check that the iterator points to
2026  * an array prior to using this function.
2027  *
2028  * @param iter the message iter
2029  * @returns the array element type
2030  */
2031 int
2032 dbus_message_iter_get_element_type (DBusMessageIter *iter)
2033 {
2034   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2035
2036   _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2037   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
2038   _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
2039
2040   return _dbus_type_reader_get_element_type (&real->u.reader);
2041 }
2042
2043 /**
2044  * Recurses into a container value when reading values from a message,
2045  * initializing a sub-iterator to use for traversing the child values
2046  * of the container.
2047  *
2048  * Note that this recurses into a value, not a type, so you can only
2049  * recurse if the value exists. The main implication of this is that
2050  * if you have for example an empty array of array of int32, you can
2051  * recurse into the outermost array, but it will have no values, so
2052  * you won't be able to recurse further. There's no array of int32 to
2053  * recurse into.
2054  *
2055  * If a container is an array of fixed-length types (except Unix file
2056  * descriptors), it is much more efficient to use
2057  * dbus_message_iter_get_fixed_array() to get the whole array in one
2058  * shot, rather than individually walking over the array elements.
2059  *
2060  * Be sure you have somehow checked that
2061  * dbus_message_iter_get_arg_type() matches the type you are expecting
2062  * to recurse into. Results of this function are undefined if there is
2063  * no container to recurse into at the current iterator position.
2064  *
2065  * @param iter the message iterator
2066  * @param sub the sub-iterator to initialize
2067  */
2068 void
2069 dbus_message_iter_recurse (DBusMessageIter  *iter,
2070                            DBusMessageIter  *sub)
2071 {
2072   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2073   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2074
2075   _dbus_return_if_fail (_dbus_message_iter_check (real));
2076   _dbus_return_if_fail (sub != NULL);
2077
2078   *real_sub = *real;
2079   _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
2080 }
2081
2082 /**
2083  * Returns the current signature of a message iterator.  This
2084  * is useful primarily for dealing with variants; one can
2085  * recurse into a variant and determine the signature of
2086  * the variant's value.
2087  *
2088  * The returned string must be freed with dbus_free().
2089  * 
2090  * @param iter the message iterator
2091  * @returns the contained signature, or NULL if out of memory
2092  */
2093 char *
2094 dbus_message_iter_get_signature (DBusMessageIter *iter)
2095 {
2096   const DBusString *sig;
2097   DBusString retstr;
2098   char *ret;
2099   int start, len;
2100   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2101
2102   _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
2103
2104   if (!_dbus_string_init (&retstr))
2105     return NULL;
2106
2107   _dbus_type_reader_get_signature (&real->u.reader, &sig,
2108                                    &start, &len);
2109   if (!_dbus_string_append_len (&retstr,
2110                                 _dbus_string_get_const_data (sig) + start,
2111                                 len))
2112     return NULL;
2113   if (!_dbus_string_steal_data (&retstr, &ret))
2114     return NULL;
2115   _dbus_string_free (&retstr);
2116   return ret;
2117 }
2118
2119 /**
2120  * Reads a basic-typed value from the message iterator.
2121  * Basic types are the non-containers such as integer and string.
2122  *
2123  * The value argument should be the address of a location to store
2124  * the returned value. So for int32 it should be a "dbus_int32_t*"
2125  * and for string a "const char**". The returned value is
2126  * by reference and should not be freed.
2127  *
2128  * This call duplicates Unix file descriptors when reading them. It is
2129  * your job to close them when you don't need them anymore.
2130  *
2131  * Unix file descriptors that are read with this function will have
2132  * the FD_CLOEXEC flag set. If you need them without this flag set,
2133  * make sure to unset it with fcntl().
2134  *
2135  * Be sure you have somehow checked that
2136  * dbus_message_iter_get_arg_type() matches the type you are
2137  * expecting, or you'll crash when you try to use an integer as a
2138  * string or something.
2139  *
2140  * To read any container type (array, struct, dict) you will need to
2141  * recurse into the container with dbus_message_iter_recurse().  If
2142  * the container is an array of fixed-length values (except Unix file
2143  * descriptors), you can get all the array elements at once with
2144  * dbus_message_iter_get_fixed_array(). Otherwise, you have to iterate
2145  * over the container's contents one value at a time.
2146  * 
2147  * All basic-typed values are guaranteed to fit in 8 bytes. So you can
2148  * write code like this:
2149  *
2150  * @code
2151  * dbus_uint64_t value;
2152  * int type;
2153  * dbus_message_iter_get_basic (&read_iter, &value);
2154  * type = dbus_message_iter_get_arg_type (&read_iter);
2155  * dbus_message_iter_append_basic (&write_iter, type, &value);
2156  * @endcode
2157  *
2158  * On some really obscure platforms dbus_uint64_t might not exist, if
2159  * you need to worry about this you will know.  dbus_uint64_t is just
2160  * one example of a type that's large enough to hold any possible
2161  * value, you could use a struct or char[8] instead if you like.
2162  *
2163  * @param iter the iterator
2164  * @param value location to store the value
2165  */
2166 void
2167 dbus_message_iter_get_basic (DBusMessageIter  *iter,
2168                              void             *value)
2169 {
2170   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2171
2172   _dbus_return_if_fail (_dbus_message_iter_check (real));
2173   _dbus_return_if_fail (value != NULL);
2174
2175   if (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UNIX_FD)
2176     {
2177 #ifdef HAVE_UNIX_FD_PASSING
2178       DBusBasicValue idx;
2179
2180       _dbus_type_reader_read_basic(&real->u.reader, &idx);
2181
2182       if (idx.u32 >= real->message->n_unix_fds) {
2183         /* Hmm, we cannot really signal an error here, so let's make
2184            sure to return an invalid fd. */
2185         *((int*) value) = -1;
2186         return;
2187       }
2188
2189       *((int*) value) = _dbus_dup(real->message->unix_fds[idx.u32], NULL);
2190 #else
2191       *((int*) value) = -1;
2192 #endif
2193     }
2194   else
2195     {
2196       _dbus_type_reader_read_basic (&real->u.reader,
2197                                     value);
2198     }
2199 }
2200
2201 /**
2202  * Returns the number of bytes in the array as marshaled in the wire
2203  * protocol. The iterator must currently be inside an array-typed
2204  * value.
2205  *
2206  * This function is deprecated on the grounds that it is stupid.  Why
2207  * would you want to know how many bytes are in the array as marshaled
2208  * in the wire protocol?  For now, use the n_elements returned from
2209  * dbus_message_iter_get_fixed_array() instead, or iterate over the
2210  * array values and count them.
2211  *
2212  * @todo introduce a variant of this get_n_elements that returns
2213  * the number of elements, though with a non-fixed array it will not
2214  * be very efficient, so maybe it's not good.
2215  * 
2216  * @param iter the iterator
2217  * @returns the number of bytes in the array
2218  */
2219 int
2220 dbus_message_iter_get_array_len (DBusMessageIter *iter)
2221 {
2222   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2223
2224   _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
2225
2226   return _dbus_type_reader_get_array_length (&real->u.reader);
2227 }
2228
2229 /**
2230  * Reads a block of fixed-length values from the message iterator.
2231  * Fixed-length values are those basic types that are not string-like,
2232  * such as integers, bool, double. The returned block will be from the
2233  * current position in the array until the end of the array.
2234  *
2235  * There is one exception here: although DBUS_TYPE_UNIX_FD is
2236  * considered a 'fixed' type arrays of this type may not be read with
2237  * this function.
2238  *
2239  * The message iter should be "in" the array (that is, you recurse into the
2240  * array, and then you call dbus_message_iter_get_fixed_array() on the
2241  * "sub-iterator" created by dbus_message_iter_recurse()).
2242  *
2243  * The value argument should be the address of a location to store the
2244  * returned array. So for int32 it should be a "const dbus_int32_t**"
2245  * The returned value is by reference and should not be freed.
2246  * 
2247  * This function should only be used if dbus_type_is_fixed() returns
2248  * #TRUE for the element type.
2249  *
2250  * If an array's elements are not fixed in size, you have to recurse
2251  * into the array with dbus_message_iter_recurse() and read the
2252  * elements one by one.
2253  * 
2254  * Because the array is not copied, this function runs in constant
2255  * time and is fast; it's much preferred over walking the entire array
2256  * with an iterator. (However, you can always use
2257  * dbus_message_iter_recurse(), even for fixed-length types;
2258  * dbus_message_iter_get_fixed_array() is just an optimization.)
2259  * 
2260  * @param iter the iterator
2261  * @param value location to store the block
2262  * @param n_elements number of elements in the block
2263  */
2264 void
2265 dbus_message_iter_get_fixed_array (DBusMessageIter  *iter,
2266                                    void             *value,
2267                                    int              *n_elements)
2268 {
2269   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2270   int subtype = _dbus_type_reader_get_current_type(&real->u.reader);
2271
2272   _dbus_return_if_fail (_dbus_message_iter_check (real));
2273   _dbus_return_if_fail (value != NULL);
2274   _dbus_return_if_fail ((subtype == DBUS_TYPE_INVALID) ||
2275                         (dbus_type_is_fixed (subtype) && subtype != DBUS_TYPE_UNIX_FD));
2276
2277   _dbus_type_reader_read_fixed_multi (&real->u.reader,
2278                                       value, n_elements);
2279 }
2280
2281 /**
2282  * Initializes a #DBusMessageIter for appending arguments to the end
2283  * of a message.
2284  *
2285  * @todo If appending any of the arguments fails due to lack of
2286  * memory, the message is hosed and you have to start over building
2287  * the whole message.
2288  *
2289  * @param message the message
2290  * @param iter pointer to an iterator to initialize
2291  */
2292 void
2293 dbus_message_iter_init_append (DBusMessage     *message,
2294                                DBusMessageIter *iter)
2295 {
2296   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2297
2298   _dbus_return_if_fail (message != NULL);
2299   _dbus_return_if_fail (iter != NULL);
2300
2301   _dbus_message_iter_init_common (message, real,
2302                                   DBUS_MESSAGE_ITER_TYPE_WRITER);
2303
2304   /* We create the signature string and point iterators at it "on demand"
2305    * when a value is actually appended. That means that init() never fails
2306    * due to OOM.
2307    */
2308   _dbus_type_writer_init_types_delayed (&real->u.writer,
2309                                         _dbus_header_get_byte_order (&message->header),
2310                                         &message->body,
2311                                         _dbus_string_get_length (&message->body));
2312 }
2313
2314 /**
2315  * Creates a temporary signature string containing the current
2316  * signature, stores it in the iterator, and points the iterator to
2317  * the end of it. Used any time we write to the message.
2318  *
2319  * @param real an iterator without a type_str
2320  * @returns #FALSE if no memory
2321  */
2322 static dbus_bool_t
2323 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
2324 {
2325   DBusString *str;
2326   const DBusString *current_sig;
2327   int current_sig_pos;
2328
2329   _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2330
2331   if (real->u.writer.type_str != NULL)
2332     {
2333       _dbus_assert (real->sig_refcount > 0);
2334       real->sig_refcount += 1;
2335       return TRUE;
2336     }
2337
2338   str = dbus_new (DBusString, 1);
2339   if (str == NULL)
2340     return FALSE;
2341
2342   if (!_dbus_header_get_field_raw (&real->message->header,
2343                                    DBUS_HEADER_FIELD_SIGNATURE,
2344                                    &current_sig, &current_sig_pos))
2345     current_sig = NULL;
2346
2347   if (current_sig)
2348     {
2349       int current_len;
2350
2351       current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2352       current_sig_pos += 1; /* move on to sig data */
2353
2354       if (!_dbus_string_init_preallocated (str, current_len + 4))
2355         {
2356           dbus_free (str);
2357           return FALSE;
2358         }
2359
2360       if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2361                                   str, 0))
2362         {
2363           _dbus_string_free (str);
2364           dbus_free (str);
2365           return FALSE;
2366         }
2367     }
2368   else
2369     {
2370       if (!_dbus_string_init_preallocated (str, 4))
2371         {
2372           dbus_free (str);
2373           return FALSE;
2374         }
2375     }
2376
2377   real->sig_refcount = 1;
2378
2379   _dbus_type_writer_add_types (&real->u.writer,
2380                                str, _dbus_string_get_length (str));
2381   return TRUE;
2382 }
2383
2384 /**
2385  * Sets the new signature as the message signature, frees the
2386  * signature string, and marks the iterator as not having a type_str
2387  * anymore. Frees the signature even if it fails, so you can't
2388  * really recover from failure. Kinda busted.
2389  *
2390  * @param real an iterator without a type_str
2391  * @returns #FALSE if no memory
2392  */
2393 static dbus_bool_t
2394 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2395 {
2396   DBusString *str;
2397   const char *v_STRING;
2398   dbus_bool_t retval;
2399
2400   _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2401   _dbus_assert (real->u.writer.type_str != NULL);
2402   _dbus_assert (real->sig_refcount > 0);
2403
2404   real->sig_refcount -= 1;
2405
2406   if (real->sig_refcount > 0)
2407     return TRUE;
2408   _dbus_assert (real->sig_refcount == 0);
2409
2410   retval = TRUE;
2411
2412   str = real->u.writer.type_str;
2413
2414   v_STRING = _dbus_string_get_const_data (str);
2415   if (!_dbus_header_set_field_basic (&real->message->header,
2416                                      DBUS_HEADER_FIELD_SIGNATURE,
2417                                      DBUS_TYPE_SIGNATURE,
2418                                      &v_STRING))
2419     retval = FALSE;
2420
2421   _dbus_type_writer_remove_types (&real->u.writer);
2422   _dbus_string_free (str);
2423   dbus_free (str);
2424
2425   return retval;
2426 }
2427
2428 /**
2429  * Frees the signature string and marks the iterator as not having a
2430  * type_str anymore.  Since the new signature is not set, the message
2431  * will generally be hosed after this is called.
2432  *
2433  * @param real an iterator without a type_str
2434  */
2435 static void
2436 _dbus_message_iter_abandon_signature (DBusMessageRealIter *real)
2437 {
2438   DBusString *str;
2439
2440   _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2441   _dbus_assert (real->u.writer.type_str != NULL);
2442   _dbus_assert (real->sig_refcount > 0);
2443
2444   real->sig_refcount -= 1;
2445
2446   if (real->sig_refcount > 0)
2447     return;
2448   _dbus_assert (real->sig_refcount == 0);
2449
2450   str = real->u.writer.type_str;
2451
2452   _dbus_type_writer_remove_types (&real->u.writer);
2453   _dbus_string_free (str);
2454   dbus_free (str);
2455 }
2456
2457 #ifndef DBUS_DISABLE_CHECKS
2458 static dbus_bool_t
2459 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2460 {
2461   if (!_dbus_message_iter_check (iter))
2462     return FALSE;
2463
2464   if (iter->message->locked)
2465     {
2466       _dbus_warn_check_failed ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2467       return FALSE;
2468     }
2469
2470   return TRUE;
2471 }
2472 #endif /* DBUS_DISABLE_CHECKS */
2473
2474 #ifdef HAVE_UNIX_FD_PASSING
2475 static int *
2476 expand_fd_array(DBusMessage *m,
2477                 unsigned     n)
2478 {
2479   _dbus_assert(m);
2480
2481   /* This makes space for adding n new fds to the array and returns a
2482      pointer to the place were the first fd should be put. */
2483
2484   if (m->n_unix_fds + n > m->n_unix_fds_allocated)
2485     {
2486       unsigned k;
2487       int *p;
2488
2489       /* Make twice as much space as necessary */
2490       k = (m->n_unix_fds + n) * 2;
2491
2492       /* Allocate at least four */
2493       if (k < 4)
2494         k = 4;
2495
2496       p = dbus_realloc(m->unix_fds, k * sizeof(int));
2497       if (p == NULL)
2498         return NULL;
2499
2500       m->unix_fds = p;
2501       m->n_unix_fds_allocated = k;
2502     }
2503
2504   return m->unix_fds + m->n_unix_fds;
2505 }
2506 #endif
2507
2508 /**
2509  * Appends a basic-typed value to the message. The basic types are the
2510  * non-container types such as integer and string.
2511  *
2512  * The "value" argument should be the address of a basic-typed value.
2513  * So for string, const char**. For integer, dbus_int32_t*.
2514  *
2515  * For Unix file descriptors this function will internally duplicate
2516  * the descriptor you passed in. Hence you may close the descriptor
2517  * immediately after this call.
2518  *
2519  * @todo If this fails due to lack of memory, the message is hosed and
2520  * you have to start over building the whole message.
2521  *
2522  * @param iter the append iterator
2523  * @param type the type of the value
2524  * @param value the address of the value
2525  * @returns #FALSE if not enough memory
2526  */
2527 dbus_bool_t
2528 dbus_message_iter_append_basic (DBusMessageIter *iter,
2529                                 int              type,
2530                                 const void      *value)
2531 {
2532   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2533   dbus_bool_t ret;
2534
2535   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2536   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2537   _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2538   _dbus_return_val_if_fail (value != NULL, FALSE);
2539
2540 #ifndef DBUS_DISABLE_CHECKS
2541   switch (type)
2542     {
2543       const char * const *string_p;
2544       const dbus_bool_t *bool_p;
2545
2546       case DBUS_TYPE_STRING:
2547         string_p = value;
2548         _dbus_return_val_if_fail (_dbus_check_is_valid_utf8 (*string_p), FALSE);
2549         break;
2550
2551       case DBUS_TYPE_OBJECT_PATH:
2552         string_p = value;
2553         _dbus_return_val_if_fail (_dbus_check_is_valid_path (*string_p), FALSE);
2554         break;
2555
2556       case DBUS_TYPE_SIGNATURE:
2557         string_p = value;
2558         _dbus_return_val_if_fail (_dbus_check_is_valid_signature (*string_p), FALSE);
2559         break;
2560
2561       case DBUS_TYPE_BOOLEAN:
2562         bool_p = value;
2563         _dbus_return_val_if_fail (*bool_p == 0 || *bool_p == 1, FALSE);
2564         break;
2565
2566       default:
2567           {
2568             /* nothing to check, all possible values are allowed */
2569           }
2570     }
2571 #endif
2572
2573   if (!_dbus_message_iter_open_signature (real))
2574     return FALSE;
2575
2576   if (type == DBUS_TYPE_UNIX_FD)
2577     {
2578 #ifdef HAVE_UNIX_FD_PASSING
2579       int *fds;
2580       dbus_uint32_t u;
2581
2582       /* First step, include the fd in the fd list of this message */
2583       if (!(fds = expand_fd_array(real->message, 1)))
2584         return FALSE;
2585
2586       *fds = _dbus_dup(*(int*) value, NULL);
2587       if (*fds < 0)
2588         return FALSE;
2589
2590       u = real->message->n_unix_fds;
2591
2592       /* Second step, write the index to the fd */
2593       if (!(ret = _dbus_type_writer_write_basic (&real->u.writer, DBUS_TYPE_UNIX_FD, &u))) {
2594         _dbus_close(*fds, NULL);
2595         return FALSE;
2596       }
2597
2598       real->message->n_unix_fds += 1;
2599       u += 1;
2600
2601       /* Final step, update the header accordingly */
2602       ret = _dbus_header_set_field_basic (&real->message->header,
2603                                           DBUS_HEADER_FIELD_UNIX_FDS,
2604                                           DBUS_TYPE_UINT32,
2605                                           &u);
2606
2607       /* If any of these operations fail the message is
2608          hosed. However, no memory or fds should be leaked since what
2609          has been added to message has been added to the message, and
2610          can hence be accounted for when the message is being
2611          freed. */
2612 #else
2613       ret = FALSE;
2614 #endif
2615     }
2616   else
2617     {
2618       ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2619     }
2620
2621   if (!_dbus_message_iter_close_signature (real))
2622     ret = FALSE;
2623
2624   return ret;
2625 }
2626
2627 /**
2628  * Appends a block of fixed-length values to an array. The
2629  * fixed-length types are all basic types that are not string-like. So
2630  * int32, double, bool, etc. (Unix file descriptors however are not
2631  * supported.) You must call dbus_message_iter_open_container() to
2632  * open an array of values before calling this function. You may call
2633  * this function multiple times (and intermixed with calls to
2634  * dbus_message_iter_append_basic()) for the same array.
2635  *
2636  * The "value" argument should be the address of the array.  So for
2637  * integer, "dbus_int32_t**" is expected for example.
2638  *
2639  * @warning in C, given "int array[]", "&array == array" (the
2640  * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2641  * So if you're using an array instead of a pointer you have to create
2642  * a pointer variable, assign the array to it, then take the address
2643  * of the pointer variable.
2644  * @code
2645  * const dbus_int32_t array[] = { 1, 2, 3 };
2646  * const dbus_int32_t *v_ARRAY = array;
2647  * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2648  *   fprintf (stderr, "No memory!\n");
2649  * @endcode
2650  * For strings it works to write const char *array = "Hello" and then
2651  * use &array though.
2652  *
2653  * @todo If this fails due to lack of memory, the message is hosed and
2654  * you have to start over building the whole message.
2655  *
2656  * @param iter the append iterator
2657  * @param element_type the type of the array elements
2658  * @param value the address of the array
2659  * @param n_elements the number of elements to append
2660  * @returns #FALSE if not enough memory
2661  */
2662 dbus_bool_t
2663 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2664                                       int              element_type,
2665                                       const void      *value,
2666                                       int              n_elements)
2667 {
2668   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2669   dbus_bool_t ret;
2670
2671   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2672   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2673   _dbus_return_val_if_fail (dbus_type_is_fixed (element_type) && element_type != DBUS_TYPE_UNIX_FD, FALSE);
2674   _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2675   _dbus_return_val_if_fail (value != NULL, FALSE);
2676   _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2677   _dbus_return_val_if_fail (n_elements <=
2678                             DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2679                             FALSE);
2680
2681 #ifndef DBUS_DISABLE_CHECKS
2682   if (element_type == DBUS_TYPE_BOOLEAN)
2683     {
2684       const dbus_bool_t * const *bools = value;
2685       int i;
2686
2687       for (i = 0; i < n_elements; i++)
2688         {
2689           _dbus_return_val_if_fail ((*bools)[i] == 0 || (*bools)[i] == 1, FALSE);
2690         }
2691     }
2692 #endif
2693
2694   ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2695
2696   return ret;
2697 }
2698
2699 /**
2700  * Appends a container-typed value to the message; you are required to
2701  * append the contents of the container using the returned
2702  * sub-iterator, and then call
2703  * dbus_message_iter_close_container(). Container types are for
2704  * example struct, variant, and array. For variants, the
2705  * contained_signature should be the type of the single value inside
2706  * the variant. For structs and dict entries, contained_signature
2707  * should be #NULL; it will be set to whatever types you write into
2708  * the struct.  For arrays, contained_signature should be the type of
2709  * the array elements.
2710  *
2711  * @todo If this fails due to lack of memory, the message is hosed and
2712  * you have to start over building the whole message.
2713  *
2714  * @param iter the append iterator
2715  * @param type the type of the value
2716  * @param contained_signature the type of container contents
2717  * @param sub sub-iterator to initialize
2718  * @returns #FALSE if not enough memory
2719  */
2720 dbus_bool_t
2721 dbus_message_iter_open_container (DBusMessageIter *iter,
2722                                   int              type,
2723                                   const char      *contained_signature,
2724                                   DBusMessageIter *sub)
2725 {
2726   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2727   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2728   DBusString contained_str;
2729
2730   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2731   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2732   _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
2733   _dbus_return_val_if_fail (sub != NULL, FALSE);
2734   _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2735                              contained_signature == NULL) ||
2736                             (type == DBUS_TYPE_DICT_ENTRY &&
2737                              contained_signature == NULL) ||
2738                             (type == DBUS_TYPE_VARIANT &&
2739                              contained_signature != NULL) ||
2740                             (type == DBUS_TYPE_ARRAY &&
2741                              contained_signature != NULL), FALSE);
2742   
2743   /* this would fail if the contained_signature is a dict entry, since
2744    * dict entries are invalid signatures standalone (they must be in
2745    * an array)
2746    */
2747   _dbus_return_val_if_fail ((type == DBUS_TYPE_ARRAY && contained_signature && *contained_signature == DBUS_DICT_ENTRY_BEGIN_CHAR) ||
2748                             (contained_signature == NULL ||
2749                              _dbus_check_is_valid_signature (contained_signature)),
2750                             FALSE);
2751
2752   if (!_dbus_message_iter_open_signature (real))
2753     return FALSE;
2754
2755   *real_sub = *real;
2756
2757   if (contained_signature != NULL)
2758     {
2759       _dbus_string_init_const (&contained_str, contained_signature);
2760
2761       return _dbus_type_writer_recurse (&real->u.writer,
2762                                         type,
2763                                         &contained_str, 0,
2764                                         &real_sub->u.writer);
2765     }
2766   else
2767     {
2768       return _dbus_type_writer_recurse (&real->u.writer,
2769                                         type,
2770                                         NULL, 0,
2771                                         &real_sub->u.writer);
2772     } 
2773 }
2774
2775
2776 /**
2777  * Closes a container-typed value appended to the message; may write
2778  * out more information to the message known only after the entire
2779  * container is written, and may free resources created by
2780  * dbus_message_iter_open_container().
2781  *
2782  * @todo If this fails due to lack of memory, the message is hosed and
2783  * you have to start over building the whole message.
2784  *
2785  * @param iter the append iterator
2786  * @param sub sub-iterator to close
2787  * @returns #FALSE if not enough memory
2788  */
2789 dbus_bool_t
2790 dbus_message_iter_close_container (DBusMessageIter *iter,
2791                                    DBusMessageIter *sub)
2792 {
2793   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2794   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2795   dbus_bool_t ret;
2796
2797   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2798   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2799   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2800   _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2801
2802   ret = _dbus_type_writer_unrecurse (&real->u.writer,
2803                                      &real_sub->u.writer);
2804
2805   if (!_dbus_message_iter_close_signature (real))
2806     ret = FALSE;
2807
2808   return ret;
2809 }
2810
2811 /**
2812  * Abandons creation of a contained-typed value and frees resources created
2813  * by dbus_message_iter_open_container().  Once this returns, the message
2814  * is hosed and you have to start over building the whole message.
2815  *
2816  * This should only be used to abandon creation of a message when you have
2817  * open containers.
2818  *
2819  * @param iter the append iterator
2820  * @param sub sub-iterator to close
2821  */
2822 void
2823 dbus_message_iter_abandon_container (DBusMessageIter *iter,
2824                                      DBusMessageIter *sub)
2825 {
2826   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2827   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2828
2829   _dbus_return_if_fail (_dbus_message_iter_append_check (real));
2830   _dbus_return_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2831   _dbus_return_if_fail (_dbus_message_iter_append_check (real_sub));
2832   _dbus_return_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2833
2834   _dbus_message_iter_abandon_signature (real);
2835 }
2836
2837 /**
2838  * Sets a flag indicating that the message does not want a reply; if
2839  * this flag is set, the other end of the connection may (but is not
2840  * required to) optimize by not sending method return or error
2841  * replies. If this flag is set, there is no way to know whether the
2842  * message successfully arrived at the remote end. Normally you know a
2843  * message was received when you receive the reply to it.
2844  *
2845  * The flag is #FALSE by default, that is by default the other end is
2846  * required to reply.
2847  *
2848  * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_REPLY_EXPECTED
2849  * 
2850  * @param message the message
2851  * @param no_reply #TRUE if no reply is desired
2852  */
2853 void
2854 dbus_message_set_no_reply (DBusMessage *message,
2855                            dbus_bool_t  no_reply)
2856 {
2857   _dbus_return_if_fail (message != NULL);
2858   _dbus_return_if_fail (!message->locked);
2859
2860   _dbus_header_toggle_flag (&message->header,
2861                             DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2862                             no_reply);
2863 }
2864
2865 /**
2866  * Returns #TRUE if the message does not expect
2867  * a reply.
2868  *
2869  * @param message the message
2870  * @returns #TRUE if the message sender isn't waiting for a reply
2871  */
2872 dbus_bool_t
2873 dbus_message_get_no_reply (DBusMessage *message)
2874 {
2875   _dbus_return_val_if_fail (message != NULL, FALSE);
2876
2877   return _dbus_header_get_flag (&message->header,
2878                                 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2879 }
2880
2881 /**
2882  * Sets a flag indicating that an owner for the destination name will
2883  * be automatically started before the message is delivered. When this
2884  * flag is set, the message is held until a name owner finishes
2885  * starting up, or fails to start up. In case of failure, the reply
2886  * will be an error.
2887  *
2888  * The flag is set to #TRUE by default, i.e. auto starting is the default.
2889  *
2890  * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_AUTO_START
2891  * 
2892  * @param message the message
2893  * @param auto_start #TRUE if auto-starting is desired
2894  */
2895 void
2896 dbus_message_set_auto_start (DBusMessage *message,
2897                              dbus_bool_t  auto_start)
2898 {
2899   _dbus_return_if_fail (message != NULL);
2900   _dbus_return_if_fail (!message->locked);
2901
2902   _dbus_header_toggle_flag (&message->header,
2903                             DBUS_HEADER_FLAG_NO_AUTO_START,
2904                             !auto_start);
2905 }
2906
2907 /**
2908  * Returns #TRUE if the message will cause an owner for
2909  * destination name to be auto-started.
2910  *
2911  * @param message the message
2912  * @returns #TRUE if the message will use auto-start
2913  */
2914 dbus_bool_t
2915 dbus_message_get_auto_start (DBusMessage *message)
2916 {
2917   _dbus_return_val_if_fail (message != NULL, FALSE);
2918
2919   return !_dbus_header_get_flag (&message->header,
2920                                  DBUS_HEADER_FLAG_NO_AUTO_START);
2921 }
2922
2923
2924 /**
2925  * Sets the object path this message is being sent to (for
2926  * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2927  * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2928  *
2929  * The path must contain only valid characters as defined
2930  * in the D-Bus specification.
2931  *
2932  * @param message the message
2933  * @param object_path the path or #NULL to unset
2934  * @returns #FALSE if not enough memory
2935  */
2936 dbus_bool_t
2937 dbus_message_set_path (DBusMessage   *message,
2938                        const char    *object_path)
2939 {
2940   _dbus_return_val_if_fail (message != NULL, FALSE);
2941   _dbus_return_val_if_fail (!message->locked, FALSE);
2942   _dbus_return_val_if_fail (object_path == NULL ||
2943                             _dbus_check_is_valid_path (object_path),
2944                             FALSE);
2945
2946   return set_or_delete_string_field (message,
2947                                      DBUS_HEADER_FIELD_PATH,
2948                                      DBUS_TYPE_OBJECT_PATH,
2949                                      object_path);
2950 }
2951
2952 /**
2953  * Gets the object path this message is being sent to (for
2954  * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2955  * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2956  *
2957  * See also dbus_message_get_path_decomposed().
2958  *
2959  * The returned string becomes invalid if the message is
2960  * modified, since it points into the wire-marshaled message data.
2961  * 
2962  * @param message the message
2963  * @returns the path (should not be freed) or #NULL
2964  */
2965 const char*
2966 dbus_message_get_path (DBusMessage   *message)
2967 {
2968   const char *v;
2969
2970   _dbus_return_val_if_fail (message != NULL, NULL);
2971
2972   v = NULL; /* in case field doesn't exist */
2973   _dbus_header_get_field_basic (&message->header,
2974                                 DBUS_HEADER_FIELD_PATH,
2975                                 DBUS_TYPE_OBJECT_PATH,
2976                                 (void *) &v);
2977   return v;
2978 }
2979
2980 /**
2981  * Checks if the message has a particular object path.  The object
2982  * path is the destination object for a method call or the emitting
2983  * object for a signal.
2984  *
2985  * @param message the message
2986  * @param path the path name
2987  * @returns #TRUE if there is a path field in the header
2988  */
2989 dbus_bool_t
2990 dbus_message_has_path (DBusMessage   *message,
2991                        const char    *path)
2992 {
2993   const char *msg_path;
2994   msg_path = dbus_message_get_path (message);
2995   
2996   if (msg_path == NULL)
2997     {
2998       if (path == NULL)
2999         return TRUE;
3000       else
3001         return FALSE;
3002     }
3003
3004   if (path == NULL)
3005     return FALSE;
3006    
3007   if (strcmp (msg_path, path) == 0)
3008     return TRUE;
3009
3010   return FALSE;
3011 }
3012
3013 /**
3014  * Gets the object path this message is being sent to
3015  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
3016  * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
3017  * format (one array element per path component).
3018  * Free the returned array with dbus_free_string_array().
3019  *
3020  * An empty but non-NULL path array means the path "/".
3021  * So the path "/foo/bar" becomes { "foo", "bar", NULL }
3022  * and the path "/" becomes { NULL }.
3023  *
3024  * See also dbus_message_get_path().
3025  * 
3026  * @todo this could be optimized by using the len from the message
3027  * instead of calling strlen() again
3028  *
3029  * @param message the message
3030  * @param path place to store allocated array of path components; #NULL set here if no path field exists
3031  * @returns #FALSE if no memory to allocate the array
3032  */
3033 dbus_bool_t
3034 dbus_message_get_path_decomposed (DBusMessage   *message,
3035                                   char        ***path)
3036 {
3037   const char *v;
3038
3039   _dbus_return_val_if_fail (message != NULL, FALSE);
3040   _dbus_return_val_if_fail (path != NULL, FALSE);
3041
3042   *path = NULL;
3043
3044   v = dbus_message_get_path (message);
3045   if (v != NULL)
3046     {
3047       if (!_dbus_decompose_path (v, strlen (v),
3048                                  path, NULL))
3049         return FALSE;
3050     }
3051   return TRUE;
3052 }
3053
3054 /**
3055  * Sets the interface this message is being sent to
3056  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
3057  * the interface a signal is being emitted from
3058  * (for DBUS_MESSAGE_TYPE_SIGNAL).
3059  *
3060  * The interface name must contain only valid characters as defined
3061  * in the D-Bus specification.
3062  * 
3063  * @param message the message
3064  * @param interface the interface or #NULL to unset
3065  * @returns #FALSE if not enough memory
3066  */
3067 dbus_bool_t
3068 dbus_message_set_interface (DBusMessage  *message,
3069                             const char   *interface)
3070 {
3071   _dbus_return_val_if_fail (message != NULL, FALSE);
3072   _dbus_return_val_if_fail (!message->locked, FALSE);
3073   _dbus_return_val_if_fail (interface == NULL ||
3074                             _dbus_check_is_valid_interface (interface),
3075                             FALSE);
3076
3077   return set_or_delete_string_field (message,
3078                                      DBUS_HEADER_FIELD_INTERFACE,
3079                                      DBUS_TYPE_STRING,
3080                                      interface);
3081 }
3082
3083 /**
3084  * Gets the interface this message is being sent to
3085  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
3086  * from (for DBUS_MESSAGE_TYPE_SIGNAL).
3087  * The interface name is fully-qualified (namespaced).
3088  * Returns #NULL if none.
3089  *
3090  * The returned string becomes invalid if the message is
3091  * modified, since it points into the wire-marshaled message data.
3092  *
3093  * @param message the message
3094  * @returns the message interface (should not be freed) or #NULL
3095  */
3096 const char*
3097 dbus_message_get_interface (DBusMessage *message)
3098 {
3099   const char *v;
3100
3101   _dbus_return_val_if_fail (message != NULL, NULL);
3102
3103   v = NULL; /* in case field doesn't exist */
3104   _dbus_header_get_field_basic (&message->header,
3105                                 DBUS_HEADER_FIELD_INTERFACE,
3106                                 DBUS_TYPE_STRING,
3107                                 (void *) &v);
3108   return v;
3109 }
3110
3111 /**
3112  * Checks if the message has an interface
3113  *
3114  * @param message the message
3115  * @param interface the interface name
3116  * @returns #TRUE if the interface field in the header matches
3117  */
3118 dbus_bool_t
3119 dbus_message_has_interface (DBusMessage   *message,
3120                             const char    *interface)
3121 {
3122   const char *msg_interface;
3123   msg_interface = dbus_message_get_interface (message);
3124    
3125   if (msg_interface == NULL)
3126     {
3127       if (interface == NULL)
3128         return TRUE;
3129       else
3130         return FALSE;
3131     }
3132
3133   if (interface == NULL)
3134     return FALSE;
3135      
3136   if (strcmp (msg_interface, interface) == 0)
3137     return TRUE;
3138
3139   return FALSE;
3140
3141 }
3142
3143 /**
3144  * Sets the interface member being invoked
3145  * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
3146  * (DBUS_MESSAGE_TYPE_SIGNAL).
3147  *
3148  * The member name must contain only valid characters as defined
3149  * in the D-Bus specification.
3150  *
3151  * @param message the message
3152  * @param member the member or #NULL to unset
3153  * @returns #FALSE if not enough memory
3154  */
3155 dbus_bool_t
3156 dbus_message_set_member (DBusMessage  *message,
3157                          const char   *member)
3158 {
3159   _dbus_return_val_if_fail (message != NULL, FALSE);
3160   _dbus_return_val_if_fail (!message->locked, FALSE);
3161   _dbus_return_val_if_fail (member == NULL ||
3162                             _dbus_check_is_valid_member (member),
3163                             FALSE);
3164
3165   return set_or_delete_string_field (message,
3166                                      DBUS_HEADER_FIELD_MEMBER,
3167                                      DBUS_TYPE_STRING,
3168                                      member);
3169 }
3170
3171 /**
3172  * Gets the interface member being invoked
3173  * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
3174  * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
3175  *
3176  * The returned string becomes invalid if the message is
3177  * modified, since it points into the wire-marshaled message data.
3178  * 
3179  * @param message the message
3180  * @returns the member name (should not be freed) or #NULL
3181  */
3182 const char*
3183 dbus_message_get_member (DBusMessage *message)
3184 {
3185   const char *v;
3186
3187   _dbus_return_val_if_fail (message != NULL, NULL);
3188
3189   v = NULL; /* in case field doesn't exist */
3190   _dbus_header_get_field_basic (&message->header,
3191                                 DBUS_HEADER_FIELD_MEMBER,
3192                                 DBUS_TYPE_STRING,
3193                                 (void *) &v);
3194   return v;
3195 }
3196
3197 /**
3198  * Checks if the message has an interface member
3199  *
3200  * @param message the message
3201  * @param member the member name
3202  * @returns #TRUE if there is a member field in the header
3203  */
3204 dbus_bool_t
3205 dbus_message_has_member (DBusMessage   *message,
3206                          const char    *member)
3207 {
3208   const char *msg_member;
3209   msg_member = dbus_message_get_member (message);
3210  
3211   if (msg_member == NULL)
3212     {
3213       if (member == NULL)
3214         return TRUE;
3215       else
3216         return FALSE;
3217     }
3218
3219   if (member == NULL)
3220     return FALSE;
3221     
3222   if (strcmp (msg_member, member) == 0)
3223     return TRUE;
3224
3225   return FALSE;
3226
3227 }
3228
3229 /**
3230  * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
3231  * The name is fully-qualified (namespaced).
3232  *
3233  * The error name must contain only valid characters as defined
3234  * in the D-Bus specification.
3235  *
3236  * @param message the message
3237  * @param error_name the name or #NULL to unset
3238  * @returns #FALSE if not enough memory
3239  */
3240 dbus_bool_t
3241 dbus_message_set_error_name (DBusMessage  *message,
3242                              const char   *error_name)
3243 {
3244   _dbus_return_val_if_fail (message != NULL, FALSE);
3245   _dbus_return_val_if_fail (!message->locked, FALSE);
3246   _dbus_return_val_if_fail (error_name == NULL ||
3247                             _dbus_check_is_valid_error_name (error_name),
3248                             FALSE);
3249
3250   return set_or_delete_string_field (message,
3251                                      DBUS_HEADER_FIELD_ERROR_NAME,
3252                                      DBUS_TYPE_STRING,
3253                                      error_name);
3254 }
3255
3256 /**
3257  * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
3258  * or #NULL if none.
3259  *
3260  * The returned string becomes invalid if the message is
3261  * modified, since it points into the wire-marshaled message data.
3262  * 
3263  * @param message the message
3264  * @returns the error name (should not be freed) or #NULL
3265  */
3266 const char*
3267 dbus_message_get_error_name (DBusMessage *message)
3268 {
3269   const char *v;
3270
3271   _dbus_return_val_if_fail (message != NULL, NULL);
3272
3273   v = NULL; /* in case field doesn't exist */
3274   _dbus_header_get_field_basic (&message->header,
3275                                 DBUS_HEADER_FIELD_ERROR_NAME,
3276                                 DBUS_TYPE_STRING,
3277                                 (void *) &v);
3278   return v;
3279 }
3280
3281 /**
3282  * Sets the message's destination. The destination is the name of
3283  * another connection on the bus and may be either the unique name
3284  * assigned by the bus to each connection, or a well-known name
3285  * specified in advance.
3286  *
3287  * The destination name must contain only valid characters as defined
3288  * in the D-Bus specification.
3289  * 
3290  * @param message the message
3291  * @param destination the destination name or #NULL to unset
3292  * @returns #FALSE if not enough memory
3293  */
3294 dbus_bool_t
3295 dbus_message_set_destination (DBusMessage  *message,
3296                               const char   *destination)
3297 {
3298   _dbus_return_val_if_fail (message != NULL, FALSE);
3299   _dbus_return_val_if_fail (!message->locked, FALSE);
3300   _dbus_return_val_if_fail (destination == NULL ||
3301                             _dbus_check_is_valid_bus_name (destination),
3302                             FALSE);
3303
3304   return set_or_delete_string_field (message,
3305                                      DBUS_HEADER_FIELD_DESTINATION,
3306                                      DBUS_TYPE_STRING,
3307                                      destination);
3308 }
3309
3310 /**
3311  * Gets the destination of a message or #NULL if there is none set.
3312  *
3313  * The returned string becomes invalid if the message is
3314  * modified, since it points into the wire-marshaled message data.
3315  *
3316  * @param message the message
3317  * @returns the message destination (should not be freed) or #NULL
3318  */
3319 const char*
3320 dbus_message_get_destination (DBusMessage *message)
3321 {
3322   const char *v;
3323
3324   _dbus_return_val_if_fail (message != NULL, NULL);
3325
3326   v = NULL; /* in case field doesn't exist */
3327   _dbus_header_get_field_basic (&message->header,
3328                                 DBUS_HEADER_FIELD_DESTINATION,
3329                                 DBUS_TYPE_STRING,
3330                                 (void *) &v);
3331   return v;
3332 }
3333
3334 /**
3335  * Sets the message sender.
3336  *
3337  * The sender must be a valid bus name as defined in the D-Bus
3338  * specification.
3339  *
3340  * Usually you don't want to call this. The message bus daemon will
3341  * call it to set the origin of each message. If you aren't implementing
3342  * a message bus daemon you shouldn't need to set the sender.
3343  *
3344  * @param message the message
3345  * @param sender the sender or #NULL to unset
3346  * @returns #FALSE if not enough memory
3347  */
3348 dbus_bool_t
3349 dbus_message_set_sender (DBusMessage  *message,
3350                          const char   *sender)
3351 {
3352   _dbus_return_val_if_fail (message != NULL, FALSE);
3353   _dbus_return_val_if_fail (!message->locked, FALSE);
3354   _dbus_return_val_if_fail (sender == NULL ||
3355                             _dbus_check_is_valid_bus_name (sender),
3356                             FALSE);
3357
3358   return set_or_delete_string_field (message,
3359                                      DBUS_HEADER_FIELD_SENDER,
3360                                      DBUS_TYPE_STRING,
3361                                      sender);
3362 }
3363
3364 /**
3365  * Gets the unique name of the connection which originated this
3366  * message, or #NULL if unknown or inapplicable. The sender is filled
3367  * in by the message bus.
3368  *
3369  * Note, the returned sender is always the unique bus name.
3370  * Connections may own multiple other bus names, but those
3371  * are not found in the sender field.
3372  * 
3373  * The returned string becomes invalid if the message is
3374  * modified, since it points into the wire-marshaled message data.
3375  *
3376  * @param message the message
3377  * @returns the unique name of the sender or #NULL
3378  */
3379 const char*
3380 dbus_message_get_sender (DBusMessage *message)
3381 {
3382   const char *v;
3383
3384   _dbus_return_val_if_fail (message != NULL, NULL);
3385
3386   v = NULL; /* in case field doesn't exist */
3387   _dbus_header_get_field_basic (&message->header,
3388                                 DBUS_HEADER_FIELD_SENDER,
3389                                 DBUS_TYPE_STRING,
3390                                 (void *) &v);
3391   return v;
3392 }
3393
3394 /**
3395  * Gets the type signature of the message, i.e. the arguments in the
3396  * message payload. The signature includes only "in" arguments for
3397  * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
3398  * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
3399  * what you might expect (that is, it does not include the signature of the
3400  * entire C++-style method).
3401  *
3402  * The signature is a string made up of type codes such as
3403  * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
3404  * the value of #DBUS_TYPE_INVALID).
3405  *
3406  * The returned string becomes invalid if the message is
3407  * modified, since it points into the wire-marshaled message data.
3408  *
3409  * @param message the message
3410  * @returns the type signature
3411  */
3412 const char*
3413 dbus_message_get_signature (DBusMessage *message)
3414 {
3415   const DBusString *type_str;
3416   int type_pos;
3417
3418   _dbus_return_val_if_fail (message != NULL, NULL);
3419
3420   get_const_signature (&message->header, &type_str, &type_pos);
3421
3422   return _dbus_string_get_const_data_len (type_str, type_pos, 0);
3423 }
3424
3425 static dbus_bool_t
3426 _dbus_message_has_type_interface_member (DBusMessage *message,
3427                                          int          type,
3428                                          const char  *interface,
3429                                          const char  *member)
3430 {
3431   const char *n;
3432
3433   _dbus_assert (message != NULL);
3434   _dbus_assert (interface != NULL);
3435   _dbus_assert (member != NULL);
3436
3437   if (dbus_message_get_type (message) != type)
3438     return FALSE;
3439
3440   /* Optimize by checking the short member name first
3441    * instead of the longer interface name
3442    */
3443
3444   n = dbus_message_get_member (message);
3445
3446   if (n && strcmp (n, member) == 0)
3447     {
3448       n = dbus_message_get_interface (message);
3449
3450       if (n == NULL || strcmp (n, interface) == 0)
3451         return TRUE;
3452     }
3453
3454   return FALSE;
3455 }
3456
3457 /**
3458  * Checks whether the message is a method call with the given
3459  * interface and member fields.  If the message is not
3460  * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
3461  * member field, returns #FALSE. If the interface field is missing,
3462  * then it will be assumed equal to the provided interface.  The D-Bus
3463  * protocol allows method callers to leave out the interface name.
3464  *
3465  * @param message the message
3466  * @param interface the name to check (must not be #NULL)
3467  * @param method the name to check (must not be #NULL)
3468  *
3469  * @returns #TRUE if the message is the specified method call
3470  */
3471 dbus_bool_t
3472 dbus_message_is_method_call (DBusMessage *message,
3473                              const char  *interface,
3474                              const char  *method)
3475 {
3476   _dbus_return_val_if_fail (message != NULL, FALSE);
3477   _dbus_return_val_if_fail (interface != NULL, FALSE);
3478   _dbus_return_val_if_fail (method != NULL, FALSE);
3479   /* don't check that interface/method are valid since it would be
3480    * expensive, and not catch many common errors
3481    */
3482
3483   return _dbus_message_has_type_interface_member (message,
3484                                                   DBUS_MESSAGE_TYPE_METHOD_CALL,
3485                                                   interface, method);
3486 }
3487
3488 /**
3489  * Checks whether the message is a signal with the given interface and
3490  * member fields.  If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
3491  * has a different interface or member field, returns #FALSE.
3492  *
3493  * @param message the message
3494  * @param interface the name to check (must not be #NULL)
3495  * @param signal_name the name to check (must not be #NULL)
3496  *
3497  * @returns #TRUE if the message is the specified signal
3498  */
3499 dbus_bool_t
3500 dbus_message_is_signal (DBusMessage *message,
3501                         const char  *interface,
3502                         const char  *signal_name)
3503 {
3504   _dbus_return_val_if_fail (message != NULL, FALSE);
3505   _dbus_return_val_if_fail (interface != NULL, FALSE);
3506   _dbus_return_val_if_fail (signal_name != NULL, FALSE);
3507   /* don't check that interface/name are valid since it would be
3508    * expensive, and not catch many common errors
3509    */
3510
3511   return _dbus_message_has_type_interface_member (message,
3512                                                   DBUS_MESSAGE_TYPE_SIGNAL,
3513                                                   interface, signal_name);
3514 }
3515
3516 /**
3517  * Checks whether the message is an error reply with the given error
3518  * name.  If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
3519  * different name, returns #FALSE.
3520  *
3521  * @param message the message
3522  * @param error_name the name to check (must not be #NULL)
3523  *
3524  * @returns #TRUE if the message is the specified error
3525  */
3526 dbus_bool_t
3527 dbus_message_is_error (DBusMessage *message,
3528                        const char  *error_name)
3529 {
3530   const char *n;
3531
3532   _dbus_return_val_if_fail (message != NULL, FALSE);
3533   _dbus_return_val_if_fail (error_name != NULL, FALSE);
3534   /* don't check that error_name is valid since it would be expensive,
3535    * and not catch many common errors
3536    */
3537
3538   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3539     return FALSE;
3540
3541   n = dbus_message_get_error_name (message);
3542
3543   if (n && strcmp (n, error_name) == 0)
3544     return TRUE;
3545   else
3546     return FALSE;
3547 }
3548
3549 /**
3550  * Checks whether the message was sent to the given name.  If the
3551  * message has no destination specified or has a different
3552  * destination, returns #FALSE.
3553  *
3554  * @param message the message
3555  * @param name the name to check (must not be #NULL)
3556  *
3557  * @returns #TRUE if the message has the given destination name
3558  */
3559 dbus_bool_t
3560 dbus_message_has_destination (DBusMessage  *message,
3561                               const char   *name)
3562 {
3563   const char *s;
3564
3565   _dbus_return_val_if_fail (message != NULL, FALSE);
3566   _dbus_return_val_if_fail (name != NULL, FALSE);
3567   /* don't check that name is valid since it would be expensive, and
3568    * not catch many common errors
3569    */
3570
3571   s = dbus_message_get_destination (message);
3572
3573   if (s && strcmp (s, name) == 0)
3574     return TRUE;
3575   else
3576     return FALSE;
3577 }
3578
3579 /**
3580  * Checks whether the message has the given unique name as its sender.
3581  * If the message has no sender specified or has a different sender,
3582  * returns #FALSE. Note that a peer application will always have the
3583  * unique name of the connection as the sender. So you can't use this
3584  * function to see whether a sender owned a well-known name.
3585  *
3586  * Messages from the bus itself will have #DBUS_SERVICE_DBUS
3587  * as the sender.
3588  *
3589  * @param message the message
3590  * @param name the name to check (must not be #NULL)
3591  *
3592  * @returns #TRUE if the message has the given sender
3593  */
3594 dbus_bool_t
3595 dbus_message_has_sender (DBusMessage  *message,
3596                          const char   *name)
3597 {
3598   const char *s;
3599
3600   _dbus_return_val_if_fail (message != NULL, FALSE);
3601   _dbus_return_val_if_fail (name != NULL, FALSE);
3602   /* don't check that name is valid since it would be expensive, and
3603    * not catch many common errors
3604    */
3605
3606   s = dbus_message_get_sender (message);
3607
3608   if (s && strcmp (s, name) == 0)
3609     return TRUE;
3610   else
3611     return FALSE;
3612 }
3613
3614 /**
3615  * Checks whether the message has the given signature; see
3616  * dbus_message_get_signature() for more details on what the signature
3617  * looks like.
3618  *
3619  * @param message the message
3620  * @param signature typecode array
3621  * @returns #TRUE if message has the given signature
3622 */
3623 dbus_bool_t
3624 dbus_message_has_signature (DBusMessage   *message,
3625                             const char    *signature)
3626 {
3627   const char *s;
3628
3629   _dbus_return_val_if_fail (message != NULL, FALSE);
3630   _dbus_return_val_if_fail (signature != NULL, FALSE);
3631   /* don't check that signature is valid since it would be expensive,
3632    * and not catch many common errors
3633    */
3634
3635   s = dbus_message_get_signature (message);
3636
3637   if (s && strcmp (s, signature) == 0)
3638     return TRUE;
3639   else
3640     return FALSE;
3641 }
3642
3643 /**
3644  * Sets a #DBusError based on the contents of the given
3645  * message. The error is only set if the message
3646  * is an error message, as in #DBUS_MESSAGE_TYPE_ERROR.
3647  * The name of the error is set to the name of the message,
3648  * and the error message is set to the first argument
3649  * if the argument exists and is a string.
3650  *
3651  * The return value indicates whether the error was set (the error is
3652  * set if and only if the message is an error message).  So you can
3653  * check for an error reply and convert it to DBusError in one go:
3654  * @code
3655  *  if (dbus_set_error_from_message (error, reply))
3656  *    return error;
3657  *  else
3658  *    process reply;
3659  * @endcode
3660  *
3661  * @param error the error to set
3662  * @param message the message to set it from
3663  * @returns #TRUE if the message had type #DBUS_MESSAGE_TYPE_ERROR
3664  */
3665 dbus_bool_t
3666 dbus_set_error_from_message (DBusError   *error,
3667                              DBusMessage *message)
3668 {
3669   const char *str;
3670
3671   _dbus_return_val_if_fail (message != NULL, FALSE);
3672   _dbus_return_val_if_error_is_set (error, FALSE);
3673
3674   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3675     return FALSE;
3676
3677   str = NULL;
3678   dbus_message_get_args (message, NULL,
3679                          DBUS_TYPE_STRING, &str,
3680                          DBUS_TYPE_INVALID);
3681
3682   dbus_set_error (error, dbus_message_get_error_name (message),
3683                   str ? "%s" : NULL, str);
3684
3685   return TRUE;
3686 }
3687
3688 /**
3689  * Checks whether a message contains unix fds
3690  *
3691  * @param message the message
3692  * @returns #TRUE if the message contains unix fds
3693  */
3694 dbus_bool_t
3695 dbus_message_contains_unix_fds(DBusMessage *message)
3696 {
3697 #ifdef HAVE_UNIX_FD_PASSING
3698   _dbus_assert(message);
3699
3700   return message->n_unix_fds > 0;
3701 #else
3702   return FALSE;
3703 #endif
3704 }
3705
3706 /** @} */
3707
3708 /**
3709  * @addtogroup DBusMessageInternals
3710  *
3711  * @{
3712  */
3713
3714 /**
3715  * The initial buffer size of the message loader.
3716  *
3717  * @todo this should be based on min header size plus some average
3718  * body size, or something. Or rather, the min header size only, if we
3719  * want to try to read only the header, store that in a DBusMessage,
3720  * then read only the body and store that, etc., depends on
3721  * how we optimize _dbus_message_loader_get_buffer() and what
3722  * the exact message format is.
3723  */
3724 #define INITIAL_LOADER_DATA_LEN 32
3725
3726 /**
3727  * Creates a new message loader. Returns #NULL if memory can't
3728  * be allocated.
3729  *
3730  * @returns new loader, or #NULL.
3731  */
3732 DBusMessageLoader*
3733 _dbus_message_loader_new (void)
3734 {
3735   DBusMessageLoader *loader;
3736
3737   loader = dbus_new0 (DBusMessageLoader, 1);
3738   if (loader == NULL)
3739     return NULL;
3740   
3741   loader->refcount = 1;
3742
3743   loader->corrupted = FALSE;
3744   loader->corruption_reason = DBUS_VALID;
3745
3746   /* this can be configured by the app, but defaults to the protocol max */
3747   loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3748
3749   /* We set a very relatively conservative default here since due to how
3750   SCM_RIGHTS works we need to preallocate an fd array of the maximum
3751   number of unix fds we want to receive in advance. A
3752   try-and-reallocate loop is not possible. */
3753   loader->max_message_unix_fds = 1024;
3754
3755   if (!_dbus_string_init (&loader->data))
3756     {
3757       dbus_free (loader);
3758       return NULL;
3759     }
3760
3761   /* preallocate the buffer for speed, ignore failure */
3762   _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3763   _dbus_string_set_length (&loader->data, 0);
3764
3765 #ifdef HAVE_UNIX_FD_PASSING
3766   loader->unix_fds = NULL;
3767   loader->n_unix_fds = loader->n_unix_fds_allocated = 0;
3768   loader->unix_fds_outstanding = FALSE;
3769 #endif
3770
3771   return loader;
3772 }
3773
3774 /**
3775  * Increments the reference count of the loader.
3776  *
3777  * @param loader the loader.
3778  * @returns the loader
3779  */
3780 DBusMessageLoader *
3781 _dbus_message_loader_ref (DBusMessageLoader *loader)
3782 {
3783   loader->refcount += 1;
3784
3785   return loader;
3786 }
3787
3788 /**
3789  * Decrements the reference count of the loader and finalizes the
3790  * loader when the count reaches zero.
3791  *
3792  * @param loader the loader.
3793  */
3794 void
3795 _dbus_message_loader_unref (DBusMessageLoader *loader)
3796 {
3797   loader->refcount -= 1;
3798   if (loader->refcount == 0)
3799     {
3800 #ifdef HAVE_UNIX_FD_PASSING
3801       close_unix_fds(loader->unix_fds, &loader->n_unix_fds);
3802       dbus_free(loader->unix_fds);
3803 #endif
3804       _dbus_list_foreach (&loader->messages,
3805                           (DBusForeachFunction) dbus_message_unref,
3806                           NULL);
3807       _dbus_list_clear (&loader->messages);
3808       _dbus_string_free (&loader->data);
3809       dbus_free (loader);
3810     }
3811 }
3812
3813 /**
3814  * Gets the buffer to use for reading data from the network.  Network
3815  * data is read directly into an allocated buffer, which is then used
3816  * in the DBusMessage, to avoid as many extra memcpy's as possible.
3817  * The buffer must always be returned immediately using
3818  * _dbus_message_loader_return_buffer(), even if no bytes are
3819  * successfully read.
3820  *
3821  * @todo this function can be a lot more clever. For example
3822  * it can probably always return a buffer size to read exactly
3823  * the body of the next message, thus avoiding any memory wastage
3824  * or reallocs.
3825  *
3826  * @todo we need to enforce a max length on strings in header fields.
3827  *
3828  * @param loader the message loader.
3829  * @param buffer the buffer
3830  */
3831 void
3832 _dbus_message_loader_get_buffer (DBusMessageLoader  *loader,
3833                                  DBusString        **buffer)
3834 {
3835   _dbus_assert (!loader->buffer_outstanding);
3836
3837   *buffer = &loader->data;
3838
3839   loader->buffer_outstanding = TRUE;
3840 }
3841
3842 /**
3843  * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3844  * indicating to the loader how many bytes of the buffer were filled
3845  * in. This function must always be called, even if no bytes were
3846  * successfully read.
3847  *
3848  * @param loader the loader.
3849  * @param buffer the buffer.
3850  * @param bytes_read number of bytes that were read into the buffer.
3851  */
3852 void
3853 _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
3854                                     DBusString         *buffer,
3855                                     int                 bytes_read)
3856 {
3857   _dbus_assert (loader->buffer_outstanding);
3858   _dbus_assert (buffer == &loader->data);
3859
3860   loader->buffer_outstanding = FALSE;
3861 }
3862
3863 /**
3864  * Gets the buffer to use for reading unix fds from the network.
3865  *
3866  * This works similar to _dbus_message_loader_get_buffer()
3867  *
3868  * @param loader the message loader.
3869  * @param fds the array to read fds into
3870  * @param max_n_fds how many fds to read at most
3871  * @return TRUE on success, FALSE on OOM
3872  */
3873 dbus_bool_t
3874 _dbus_message_loader_get_unix_fds(DBusMessageLoader  *loader,
3875                                   int               **fds,
3876                                   unsigned           *max_n_fds)
3877 {
3878 #ifdef HAVE_UNIX_FD_PASSING
3879   _dbus_assert (!loader->unix_fds_outstanding);
3880
3881   /* Allocate space where we can put the fds we read. We allocate
3882      space for max_message_unix_fds since this is an
3883      upper limit how many fds can be received within a single
3884      message. Since SCM_RIGHTS doesn't allow a reallocate+retry logic
3885      we are allocating the maximum possible array size right from the
3886      beginning. This sucks a bit, however unless SCM_RIGHTS is fixed
3887      there is no better way. */
3888
3889   if (loader->n_unix_fds_allocated < loader->max_message_unix_fds)
3890     {
3891       int *a = dbus_realloc(loader->unix_fds,
3892                             loader->max_message_unix_fds * sizeof(loader->unix_fds[0]));
3893
3894       if (!a)
3895         return FALSE;
3896
3897       loader->unix_fds = a;
3898       loader->n_unix_fds_allocated = loader->max_message_unix_fds;
3899     }
3900
3901   *fds = loader->unix_fds + loader->n_unix_fds;
3902   *max_n_fds = loader->n_unix_fds_allocated - loader->n_unix_fds;
3903
3904   loader->unix_fds_outstanding = TRUE;
3905   return TRUE;
3906 #else
3907   _dbus_assert_not_reached("Platform doesn't support unix fd passing");
3908   return FALSE;
3909 #endif
3910 }
3911
3912 /**
3913  * Returns a buffer obtained from _dbus_message_loader_get_unix_fds().
3914  *
3915  * This works similar to _dbus_message_loader_return_buffer()
3916  *
3917  * @param loader the message loader.
3918  * @param fds the array fds were read into
3919  * @param max_n_fds how many fds were read
3920  */
3921
3922 void
3923 _dbus_message_loader_return_unix_fds(DBusMessageLoader  *loader,
3924                                      int                *fds,
3925                                      unsigned            n_fds)
3926 {
3927 #ifdef HAVE_UNIX_FD_PASSING
3928   _dbus_assert(loader->unix_fds_outstanding);
3929   _dbus_assert(loader->unix_fds + loader->n_unix_fds == fds);
3930   _dbus_assert(loader->n_unix_fds + n_fds <= loader->n_unix_fds_allocated);
3931
3932   loader->n_unix_fds += n_fds;
3933   loader->unix_fds_outstanding = FALSE;
3934 #else
3935   _dbus_assert_not_reached("Platform doesn't support unix fd passing");
3936 #endif
3937 }
3938
3939 /*
3940  * FIXME when we move the header out of the buffer, that memmoves all
3941  * buffered messages. Kind of crappy.
3942  *
3943  * Also we copy the header and body, which is kind of crappy.  To
3944  * avoid this, we have to allow header and body to be in a single
3945  * memory block, which is good for messages we read and bad for
3946  * messages we are creating. But we could move_len() the buffer into
3947  * this single memory block, and move_len() will just swap the buffers
3948  * if you're moving the entire buffer replacing the dest string.
3949  *
3950  * We could also have the message loader tell the transport how many
3951  * bytes to read; so it would first ask for some arbitrary number like
3952  * 256, then if the message was incomplete it would use the
3953  * header/body len to ask for exactly the size of the message (or
3954  * blocks the size of a typical kernel buffer for the socket). That
3955  * way we don't get trailing bytes in the buffer that have to be
3956  * memmoved. Though I suppose we also don't have a chance of reading a
3957  * bunch of small messages at once, so the optimization may be stupid.
3958  *
3959  * Another approach would be to keep a "start" index into
3960  * loader->data and only delete it occasionally, instead of after
3961  * each message is loaded.
3962  *
3963  * load_message() returns FALSE if not enough memory OR the loader was corrupted
3964  */
3965 static dbus_bool_t
3966 load_message (DBusMessageLoader *loader,
3967               DBusMessage       *message,
3968               int                byte_order,
3969               int                fields_array_len,
3970               int                header_len,
3971               int                body_len)
3972 {
3973   dbus_bool_t oom;
3974   DBusValidity validity;
3975   const DBusString *type_str;
3976   int type_pos;
3977   DBusValidationMode mode;
3978   dbus_uint32_t n_unix_fds = 0;
3979
3980   mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3981   
3982   oom = FALSE;
3983
3984 #if 0
3985   _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3986 #endif
3987
3988   /* 1. VALIDATE AND COPY OVER HEADER */
3989   _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3990   _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3991
3992   if (!_dbus_header_load (&message->header,
3993                           mode,
3994                           &validity,
3995                           byte_order,
3996                           fields_array_len,
3997                           header_len,
3998                           body_len,
3999                           &loader->data, 0,
4000                           _dbus_string_get_length (&loader->data)))
4001     {
4002       _dbus_verbose ("Failed to load header for new message code %d\n", validity);
4003
4004       /* assert here so we can catch any code that still uses DBUS_VALID to indicate
4005          oom errors.  They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
4006       _dbus_assert (validity != DBUS_VALID);
4007
4008       if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
4009         oom = TRUE;
4010       else
4011         {
4012           loader->corrupted = TRUE;
4013           loader->corruption_reason = validity;
4014         }
4015       goto failed;
4016     }
4017
4018   _dbus_assert (validity == DBUS_VALID);
4019
4020   /* 2. VALIDATE BODY */
4021   if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
4022     {
4023       get_const_signature (&message->header, &type_str, &type_pos);
4024       
4025       /* Because the bytes_remaining arg is NULL, this validates that the
4026        * body is the right length
4027        */
4028       validity = _dbus_validate_body_with_reason (type_str,
4029                                                   type_pos,
4030                                                   byte_order,
4031                                                   NULL,
4032                                                   &loader->data,
4033                                                   header_len,
4034                                                   body_len);
4035       if (validity != DBUS_VALID)
4036         {
4037           _dbus_verbose ("Failed to validate message body code %d\n", validity);
4038
4039           loader->corrupted = TRUE;
4040           loader->corruption_reason = validity;
4041           
4042           goto failed;
4043         }
4044     }
4045
4046   /* 3. COPY OVER UNIX FDS */
4047   _dbus_header_get_field_basic(&message->header,
4048                                DBUS_HEADER_FIELD_UNIX_FDS,
4049                                DBUS_TYPE_UINT32,
4050                                &n_unix_fds);
4051
4052 #ifdef HAVE_UNIX_FD_PASSING
4053
4054   if (n_unix_fds > loader->n_unix_fds)
4055     {
4056       _dbus_verbose("Message contains references to more unix fds than were sent %u != %u\n",
4057                     n_unix_fds, loader->n_unix_fds);
4058
4059       loader->corrupted = TRUE;
4060       loader->corruption_reason = DBUS_INVALID_MISSING_UNIX_FDS;
4061       goto failed;
4062     }
4063
4064   /* If this was a recycled message there might still be
4065      some memory allocated for the fds */
4066   dbus_free(message->unix_fds);
4067
4068   if (n_unix_fds > 0)
4069     {
4070       message->unix_fds = _dbus_memdup(loader->unix_fds, n_unix_fds * sizeof(message->unix_fds[0]));
4071       if (message->unix_fds == NULL)
4072         {
4073           _dbus_verbose ("Failed to allocate file descriptor array\n");
4074           oom = TRUE;
4075           goto failed;
4076         }
4077
4078       message->n_unix_fds_allocated = message->n_unix_fds = n_unix_fds;
4079       loader->n_unix_fds -= n_unix_fds;
4080       memmove(loader->unix_fds + n_unix_fds, loader->unix_fds, loader->n_unix_fds);
4081     }
4082   else
4083     message->unix_fds = NULL;
4084
4085 #else
4086
4087   if (n_unix_fds > 0)
4088     {
4089       _dbus_verbose ("Hmm, message claims to come with file descriptors "
4090                      "but that's not supported on our platform, disconnecting.\n");
4091
4092       loader->corrupted = TRUE;
4093       loader->corruption_reason = DBUS_INVALID_MISSING_UNIX_FDS;
4094       goto failed;
4095     }
4096
4097 #endif
4098
4099   /* 3. COPY OVER BODY AND QUEUE MESSAGE */
4100
4101   if (!_dbus_list_append (&loader->messages, message))
4102     {
4103       _dbus_verbose ("Failed to append new message to loader queue\n");
4104       oom = TRUE;
4105       goto failed;
4106     }
4107
4108   _dbus_assert (_dbus_string_get_length (&message->body) == 0);
4109   _dbus_assert (_dbus_string_get_length (&loader->data) >=
4110                 (header_len + body_len));
4111
4112   if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
4113     {
4114       _dbus_verbose ("Failed to move body into new message\n");
4115       oom = TRUE;
4116       goto failed;
4117     }
4118
4119   _dbus_string_delete (&loader->data, 0, header_len + body_len);
4120
4121   /* don't waste more than 2k of memory */
4122   _dbus_string_compact (&loader->data, 2048);
4123
4124   _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
4125   _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
4126
4127   _dbus_verbose ("Loaded message %p\n", message);
4128
4129   _dbus_assert (!oom);
4130   _dbus_assert (!loader->corrupted);
4131   _dbus_assert (loader->messages != NULL);
4132   _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
4133
4134   return TRUE;
4135
4136  failed:
4137
4138   /* Clean up */
4139
4140   /* does nothing if the message isn't in the list */
4141   _dbus_list_remove_last (&loader->messages, message);
4142   
4143   if (oom)
4144     _dbus_assert (!loader->corrupted);
4145   else
4146     _dbus_assert (loader->corrupted);
4147
4148   _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
4149
4150   return FALSE;
4151 }
4152
4153 /**
4154  * Converts buffered data into messages, if we have enough data.  If
4155  * we don't have enough data, does nothing.
4156  *
4157  * @todo we need to check that the proper named header fields exist
4158  * for each message type.
4159  *
4160  * @todo If a message has unknown type, we should probably eat it
4161  * right here rather than passing it out to applications.  However
4162  * it's not an error to see messages of unknown type.
4163  *
4164  * @param loader the loader.
4165  * @returns #TRUE if we had enough memory to finish.
4166  */
4167 dbus_bool_t
4168 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
4169 {
4170   while (!loader->corrupted &&
4171          _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
4172     {
4173       DBusValidity validity;
4174       int byte_order, fields_array_len, header_len, body_len;
4175
4176       if (_dbus_header_have_message_untrusted (loader->max_message_size,
4177                                                &validity,
4178                                                &byte_order,
4179                                                &fields_array_len,
4180                                                &header_len,
4181                                                &body_len,
4182                                                &loader->data, 0,
4183                                                _dbus_string_get_length (&loader->data)))
4184         {
4185           DBusMessage *message;
4186
4187           _dbus_assert (validity == DBUS_VALID);
4188
4189           message = dbus_message_new_empty_header ();
4190           if (message == NULL)
4191             return FALSE;
4192
4193           if (!load_message (loader, message,
4194                              byte_order, fields_array_len,
4195                              header_len, body_len))
4196             {
4197               dbus_message_unref (message);
4198               /* load_message() returns false if corrupted or OOM; if
4199                * corrupted then return TRUE for not OOM
4200                */
4201               return loader->corrupted;
4202             }
4203
4204           _dbus_assert (loader->messages != NULL);
4205           _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
4206         }
4207       else
4208         {
4209           _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
4210                          validity);
4211           if (validity != DBUS_VALID)
4212             {
4213               loader->corrupted = TRUE;
4214               loader->corruption_reason = validity;
4215             }
4216           return TRUE;
4217         }
4218     }
4219
4220   return TRUE;
4221 }
4222
4223 /**
4224  * Peeks at first loaded message, returns #NULL if no messages have
4225  * been queued.
4226  *
4227  * @param loader the loader.
4228  * @returns the next message, or #NULL if none.
4229  */
4230 DBusMessage*
4231 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
4232 {
4233   if (loader->messages)
4234     return loader->messages->data;
4235   else
4236     return NULL;
4237 }
4238
4239 /**
4240  * Pops a loaded message (passing ownership of the message
4241  * to the caller). Returns #NULL if no messages have been
4242  * queued.
4243  *
4244  * @param loader the loader.
4245  * @returns the next message, or #NULL if none.
4246  */
4247 DBusMessage*
4248 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
4249 {
4250   return _dbus_list_pop_first (&loader->messages);
4251 }
4252
4253 /**
4254  * Pops a loaded message inside a list link (passing ownership of the
4255  * message and link to the caller). Returns #NULL if no messages have
4256  * been loaded.
4257  *
4258  * @param loader the loader.
4259  * @returns the next message link, or #NULL if none.
4260  */
4261 DBusList*
4262 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
4263 {
4264   return _dbus_list_pop_first_link (&loader->messages);
4265 }
4266
4267 /**
4268  * Returns a popped message link, used to undo a pop.
4269  *
4270  * @param loader the loader
4271  * @param link the link with a message in it
4272  */
4273 void
4274 _dbus_message_loader_putback_message_link (DBusMessageLoader  *loader,
4275                                            DBusList           *link)
4276 {
4277   _dbus_list_prepend_link (&loader->messages, link);
4278 }
4279
4280 /**
4281  * Checks whether the loader is confused due to bad data.
4282  * If messages are received that are invalid, the
4283  * loader gets confused and gives up permanently.
4284  * This state is called "corrupted."
4285  *
4286  * @param loader the loader
4287  * @returns #TRUE if the loader is hosed.
4288  */
4289 dbus_bool_t
4290 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
4291 {
4292   _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
4293                 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
4294   return loader->corrupted;
4295 }
4296
4297 /**
4298  * Checks what kind of bad data confused the loader.
4299  *
4300  * @param loader the loader
4301  * @returns why the loader is hosed, or DBUS_VALID if it isn't.
4302  */
4303 DBusValidity
4304 _dbus_message_loader_get_corruption_reason (DBusMessageLoader *loader)
4305 {
4306   _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
4307                 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
4308
4309   return loader->corruption_reason;
4310 }
4311
4312 /**
4313  * Sets the maximum size message we allow.
4314  *
4315  * @param loader the loader
4316  * @param size the max message size in bytes
4317  */
4318 void
4319 _dbus_message_loader_set_max_message_size (DBusMessageLoader  *loader,
4320                                            long                size)
4321 {
4322   if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
4323     {
4324       _dbus_verbose ("clamping requested max message size %ld to %d\n",
4325                      size, DBUS_MAXIMUM_MESSAGE_LENGTH);
4326       size = DBUS_MAXIMUM_MESSAGE_LENGTH;
4327     }
4328   loader->max_message_size = size;
4329 }
4330
4331 /**
4332  * Gets the maximum allowed message size in bytes.
4333  *
4334  * @param loader the loader
4335  * @returns max size in bytes
4336  */
4337 long
4338 _dbus_message_loader_get_max_message_size (DBusMessageLoader  *loader)
4339 {
4340   return loader->max_message_size;
4341 }
4342
4343 /**
4344  * Sets the maximum unix fds per message we allow.
4345  *
4346  * @param loader the loader
4347  * @param size the max number of unix fds in a message
4348  */
4349 void
4350 _dbus_message_loader_set_max_message_unix_fds (DBusMessageLoader  *loader,
4351                                                long                n)
4352 {
4353   if (n > DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
4354     {
4355       _dbus_verbose ("clamping requested max message unix_fds %ld to %d\n",
4356                      n, DBUS_MAXIMUM_MESSAGE_UNIX_FDS);
4357       n = DBUS_MAXIMUM_MESSAGE_UNIX_FDS;
4358     }
4359   loader->max_message_unix_fds = n;
4360 }
4361
4362 /**
4363  * Gets the maximum allowed number of unix fds per message
4364  *
4365  * @param loader the loader
4366  * @returns max unix fds
4367  */
4368 long
4369 _dbus_message_loader_get_max_message_unix_fds (DBusMessageLoader  *loader)
4370 {
4371   return loader->max_message_unix_fds;
4372 }
4373
4374 static DBusDataSlotAllocator slot_allocator;
4375 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
4376
4377 /**
4378  * Allocates an integer ID to be used for storing application-specific
4379  * data on any DBusMessage. The allocated ID may then be used
4380  * with dbus_message_set_data() and dbus_message_get_data().
4381  * The passed-in slot must be initialized to -1, and is filled in
4382  * with the slot ID. If the passed-in slot is not -1, it's assumed
4383  * to be already allocated, and its refcount is incremented.
4384  *
4385  * The allocated slot is global, i.e. all DBusMessage objects will
4386  * have a slot with the given integer ID reserved.
4387  *
4388  * @param slot_p address of a global variable storing the slot
4389  * @returns #FALSE on failure (no memory)
4390  */
4391 dbus_bool_t
4392 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
4393 {
4394   return _dbus_data_slot_allocator_alloc (&slot_allocator,
4395                                           &_DBUS_LOCK_NAME (message_slots),
4396                                           slot_p);
4397 }
4398
4399 /**
4400  * Deallocates a global ID for message data slots.
4401  * dbus_message_get_data() and dbus_message_set_data() may no
4402  * longer be used with this slot.  Existing data stored on existing
4403  * DBusMessage objects will be freed when the message is
4404  * finalized, but may not be retrieved (and may only be replaced if
4405  * someone else reallocates the slot).  When the refcount on the
4406  * passed-in slot reaches 0, it is set to -1.
4407  *
4408  * @param slot_p address storing the slot to deallocate
4409  */
4410 void
4411 dbus_message_free_data_slot (dbus_int32_t *slot_p)
4412 {
4413   _dbus_return_if_fail (*slot_p >= 0);
4414
4415   _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
4416 }
4417
4418 /**
4419  * Stores a pointer on a DBusMessage, along
4420  * with an optional function to be used for freeing
4421  * the data when the data is set again, or when
4422  * the message is finalized. The slot number
4423  * must have been allocated with dbus_message_allocate_data_slot().
4424  *
4425  * @param message the message
4426  * @param slot the slot number
4427  * @param data the data to store
4428  * @param free_data_func finalizer function for the data
4429  * @returns #TRUE if there was enough memory to store the data
4430  */
4431 dbus_bool_t
4432 dbus_message_set_data (DBusMessage     *message,
4433                        dbus_int32_t     slot,
4434                        void            *data,
4435                        DBusFreeFunction free_data_func)
4436 {
4437   DBusFreeFunction old_free_func;
4438   void *old_data;
4439   dbus_bool_t retval;
4440
4441   _dbus_return_val_if_fail (message != NULL, FALSE);
4442   _dbus_return_val_if_fail (slot >= 0, FALSE);
4443
4444   retval = _dbus_data_slot_list_set (&slot_allocator,
4445                                      &message->slot_list,
4446                                      slot, data, free_data_func,
4447                                      &old_free_func, &old_data);
4448
4449   if (retval)
4450     {
4451       /* Do the actual free outside the message lock */
4452       if (old_free_func)
4453         (* old_free_func) (old_data);
4454     }
4455
4456   return retval;
4457 }
4458
4459 /**
4460  * Retrieves data previously set with dbus_message_set_data().
4461  * The slot must still be allocated (must not have been freed).
4462  *
4463  * @param message the message
4464  * @param slot the slot to get data from
4465  * @returns the data, or #NULL if not found
4466  */
4467 void*
4468 dbus_message_get_data (DBusMessage   *message,
4469                        dbus_int32_t   slot)
4470 {
4471   void *res;
4472
4473   _dbus_return_val_if_fail (message != NULL, NULL);
4474
4475   res = _dbus_data_slot_list_get (&slot_allocator,
4476                                   &message->slot_list,
4477                                   slot);
4478
4479   return res;
4480 }
4481
4482 /**
4483  * Utility function to convert a machine-readable (not translated)
4484  * string into a D-Bus message type.
4485  *
4486  * @code
4487  *   "method_call"    -> DBUS_MESSAGE_TYPE_METHOD_CALL
4488  *   "method_return"  -> DBUS_MESSAGE_TYPE_METHOD_RETURN
4489  *   "signal"         -> DBUS_MESSAGE_TYPE_SIGNAL
4490  *   "error"          -> DBUS_MESSAGE_TYPE_ERROR
4491  *   anything else    -> DBUS_MESSAGE_TYPE_INVALID
4492  * @endcode
4493  *
4494  */
4495 int
4496 dbus_message_type_from_string (const char *type_str)
4497 {
4498   if (strcmp (type_str, "method_call") == 0)
4499     return DBUS_MESSAGE_TYPE_METHOD_CALL;
4500   if (strcmp (type_str, "method_return") == 0)
4501     return DBUS_MESSAGE_TYPE_METHOD_RETURN;
4502   else if (strcmp (type_str, "signal") == 0)
4503     return DBUS_MESSAGE_TYPE_SIGNAL;
4504   else if (strcmp (type_str, "error") == 0)
4505     return DBUS_MESSAGE_TYPE_ERROR;
4506   else
4507     return DBUS_MESSAGE_TYPE_INVALID;
4508 }
4509
4510 /**
4511  * Utility function to convert a D-Bus message type into a
4512  * machine-readable string (not translated).
4513  *
4514  * @code
4515  *   DBUS_MESSAGE_TYPE_METHOD_CALL    -> "method_call"
4516  *   DBUS_MESSAGE_TYPE_METHOD_RETURN  -> "method_return"
4517  *   DBUS_MESSAGE_TYPE_SIGNAL         -> "signal"
4518  *   DBUS_MESSAGE_TYPE_ERROR          -> "error"
4519  *   DBUS_MESSAGE_TYPE_INVALID        -> "invalid"
4520  * @endcode
4521  *
4522  */
4523 const char *
4524 dbus_message_type_to_string (int type)
4525 {
4526   switch (type)
4527     {
4528     case DBUS_MESSAGE_TYPE_METHOD_CALL:
4529       return "method_call";
4530     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
4531       return "method_return";
4532     case DBUS_MESSAGE_TYPE_SIGNAL:
4533       return "signal";
4534     case DBUS_MESSAGE_TYPE_ERROR:
4535       return "error";
4536     default:
4537       return "invalid";
4538     }
4539 }
4540
4541 /**
4542  * Turn a DBusMessage into the marshalled form as described in the D-Bus
4543  * specification.
4544  *
4545  * Generally, this function is only useful for encapsulating D-Bus messages in
4546  * a different protocol.
4547  *
4548  * @param msg the DBusMessage
4549  * @param marshalled_data_p the location to save the marshalled form to
4550  * @param len_p the location to save the length of the marshalled form to
4551  * @returns #FALSE if there was not enough memory
4552  */
4553 dbus_bool_t
4554 dbus_message_marshal (DBusMessage  *msg,
4555                       char        **marshalled_data_p,
4556                       int          *len_p)
4557 {
4558   DBusString tmp;
4559   dbus_bool_t was_locked;
4560
4561   _dbus_return_val_if_fail (msg != NULL, FALSE);
4562   _dbus_return_val_if_fail (marshalled_data_p != NULL, FALSE);
4563   _dbus_return_val_if_fail (len_p != NULL, FALSE);
4564   
4565   if (!_dbus_string_init (&tmp))
4566     return FALSE;
4567
4568   /* Ensure the message is locked, to ensure the length header is filled in. */
4569   was_locked = msg->locked;
4570
4571   if (!was_locked)
4572     dbus_message_lock (msg);
4573
4574   if (!_dbus_string_copy (&(msg->header.data), 0, &tmp, 0))
4575     goto fail;
4576
4577   *len_p = _dbus_string_get_length (&tmp);
4578
4579   if (!_dbus_string_copy (&(msg->body), 0, &tmp, *len_p))
4580     goto fail;
4581
4582   *len_p = _dbus_string_get_length (&tmp);
4583
4584   if (!_dbus_string_steal_data (&tmp, marshalled_data_p))
4585     goto fail;
4586
4587   _dbus_string_free (&tmp);
4588
4589   if (!was_locked)
4590     msg->locked = FALSE;
4591
4592   return TRUE;
4593
4594  fail:
4595   _dbus_string_free (&tmp);
4596
4597   if (!was_locked)
4598     msg->locked = FALSE;
4599
4600   return FALSE;
4601 }
4602
4603 /**
4604  * Demarshal a D-Bus message from the format described in the D-Bus
4605  * specification.
4606  *
4607  * Generally, this function is only useful for encapsulating D-Bus messages in
4608  * a different protocol.
4609  *
4610  * @param str the marshalled DBusMessage
4611  * @param len the length of str
4612  * @param error the location to save errors to
4613  * @returns #NULL if there was an error
4614  */
4615 DBusMessage *
4616 dbus_message_demarshal (const char *str,
4617                         int         len,
4618                         DBusError  *error)
4619 {
4620   DBusMessageLoader *loader;
4621   DBusString *buffer;
4622   DBusMessage *msg;
4623
4624   _dbus_return_val_if_fail (str != NULL, NULL);
4625
4626   loader = _dbus_message_loader_new ();
4627
4628   if (loader == NULL)
4629     return NULL;
4630
4631   _dbus_message_loader_get_buffer (loader, &buffer);
4632   _dbus_string_append_len (buffer, str, len);
4633   _dbus_message_loader_return_buffer (loader, buffer, len);
4634
4635   if (!_dbus_message_loader_queue_messages (loader))
4636     goto fail_oom;
4637
4638   if (_dbus_message_loader_get_is_corrupted (loader))
4639     goto fail_corrupt;
4640
4641   msg = _dbus_message_loader_pop_message (loader);
4642
4643   if (!msg)
4644     goto fail_oom;
4645
4646   _dbus_message_loader_unref (loader);
4647   return msg;
4648
4649  fail_corrupt:
4650   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Message is corrupted (%s)",
4651                   _dbus_validity_to_error_message (loader->corruption_reason));
4652   _dbus_message_loader_unref (loader);
4653   return NULL;
4654
4655  fail_oom:
4656   _DBUS_SET_OOM (error);
4657   _dbus_message_loader_unref (loader);
4658   return NULL;
4659 }
4660
4661 /**
4662  * Returns the number of bytes required to be in the buffer to demarshal a
4663  * D-Bus message.
4664  *
4665  * Generally, this function is only useful for encapsulating D-Bus messages in
4666  * a different protocol.
4667  *
4668  * @param str data to be marshalled
4669  * @param len the length of str
4670  * @param error the location to save errors to
4671  * @returns -1 if there was no valid data to be demarshalled, 0 if there wasn't enough data to determine how much should be demarshalled. Otherwise returns the number of bytes to be demarshalled
4672  * 
4673  */
4674 int 
4675 dbus_message_demarshal_bytes_needed(const char *buf, 
4676                                     int         len)
4677 {
4678   DBusString str;
4679   int byte_order, fields_array_len, header_len, body_len;
4680   DBusValidity validity = DBUS_VALID;
4681   int have_message;
4682
4683   if (!buf || len < DBUS_MINIMUM_HEADER_SIZE)
4684     return 0;
4685
4686   if (len > DBUS_MAXIMUM_MESSAGE_LENGTH)
4687     len = DBUS_MAXIMUM_MESSAGE_LENGTH;
4688   _dbus_string_init_const_len (&str, buf, len);
4689   
4690   validity = DBUS_VALID;
4691   have_message
4692     = _dbus_header_have_message_untrusted(DBUS_MAXIMUM_MESSAGE_LENGTH,
4693                                           &validity, &byte_order,
4694                                           &fields_array_len,
4695                                           &header_len,
4696                                           &body_len,
4697                                           &str, 0,
4698                                           len);
4699   _dbus_string_free (&str);
4700
4701   if (validity == DBUS_VALID)
4702     {
4703       _dbus_assert (have_message || (header_len + body_len) > len);
4704       return header_len + body_len;
4705     }
4706   else
4707     {
4708       return -1; /* broken! */
4709     }
4710 }
4711
4712 /** @} */
4713
4714 /* tests in dbus-message-util.c */