2005-02-10 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-message.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dbus-internals.h"
26 #include "dbus-marshal-recursive.h"
27 #include "dbus-marshal-validate.h"
28 #include "dbus-marshal-byteswap.h"
29 #include "dbus-marshal-header.h"
30 #include "dbus-message-private.h"
31 #include "dbus-object-tree.h"
32 #include "dbus-memory.h"
33 #include "dbus-list.h"
34 #include <string.h>
35
36 /**
37  * @defgroup DBusMessageInternals DBusMessage implementation details
38  * @ingroup DBusInternals
39  * @brief DBusMessage private implementation details.
40  *
41  * The guts of DBusMessage and its methods.
42  *
43  * @{
44  */
45
46 /* Not thread locked, but strictly const/read-only so should be OK
47  */
48 /** An static string representing an empty signature */
49 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str,  "");
50
51 /* these have wacky values to help trap uninitialized iterators;
52  * but has to fit in 3 bits
53  */
54 enum {
55   DBUS_MESSAGE_ITER_TYPE_READER = 3,
56   DBUS_MESSAGE_ITER_TYPE_WRITER = 7
57 };
58
59 /** typedef for internals of message iterator */
60 typedef struct DBusMessageRealIter DBusMessageRealIter;
61
62 /**
63  * @brief Internals of DBusMessageIter
64  *
65  * Object representing a position in a message. All fields are internal.
66  */
67 struct DBusMessageRealIter
68 {
69   DBusMessage *message; /**< Message used */
70   dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
71   dbus_uint32_t iter_type : 3;      /**< whether this is a reader or writer iter */
72   dbus_uint32_t sig_refcount : 8;   /**< depth of open_signature() */
73   union
74   {
75     DBusTypeWriter writer; /**< writer */
76     DBusTypeReader reader; /**< reader */
77   } u; /**< the type writer or reader that does all the work */
78 };
79
80 static void
81 get_const_signature (DBusHeader        *header,
82                      const DBusString **type_str_p,
83                      int               *type_pos_p)
84 {
85   if (_dbus_header_get_field_raw (header,
86                                   DBUS_HEADER_FIELD_SIGNATURE,
87                                   type_str_p,
88                                   type_pos_p))
89     {
90       *type_pos_p += 1; /* skip the signature length which is 1 byte */
91     }
92   else
93     {
94       *type_str_p = &_dbus_empty_signature_str;
95       *type_pos_p = 0;
96     }
97 }
98
99 /**
100  * Swaps the message to compiler byte order if required
101  *
102  * @param message the message
103  */
104 static void
105 _dbus_message_byteswap (DBusMessage *message)
106 {
107   const DBusString *type_str;
108   int type_pos;
109   
110   if (message->byte_order == DBUS_COMPILER_BYTE_ORDER)
111     return;
112
113   _dbus_verbose ("Swapping message into compiler byte order\n");
114   
115   get_const_signature (&message->header, &type_str, &type_pos);
116   
117   _dbus_marshal_byteswap (type_str, type_pos,
118                           message->byte_order,
119                           DBUS_COMPILER_BYTE_ORDER,
120                           &message->body, 0);
121
122   message->byte_order = DBUS_COMPILER_BYTE_ORDER;
123   
124   _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
125 }
126
127 #define ensure_byte_order(message)                      \
128  if (message->byte_order != DBUS_COMPILER_BYTE_ORDER)   \
129    _dbus_message_byteswap (message)
130
131 /**
132  * Gets the data to be sent over the network for this message.
133  * The header and then the body should be written out.
134  * This function is guaranteed to always return the same
135  * data once a message is locked (with _dbus_message_lock()).
136  *
137  * @param message the message.
138  * @param header return location for message header data.
139  * @param body return location for message body data.
140  */
141 void
142 _dbus_message_get_network_data (DBusMessage          *message,
143                                 const DBusString    **header,
144                                 const DBusString    **body)
145 {
146   _dbus_assert (message->locked);
147
148   *header = &message->header.data;
149   *body = &message->body;
150 }
151
152 /**
153  * Sets the serial number of a message.
154  * This can only be done once on a message.
155  *
156  * @param message the message
157  * @param serial the serial
158  */
159 void
160 _dbus_message_set_serial (DBusMessage   *message,
161                           dbus_uint32_t  serial)
162 {
163   _dbus_assert (message != NULL);
164   _dbus_assert (!message->locked);
165   _dbus_assert (dbus_message_get_serial (message) == 0);
166
167   _dbus_header_set_serial (&message->header, serial);
168 }
169
170 /**
171  * Adds a counter to be incremented immediately with the
172  * size of this message, and decremented by the size
173  * of this message when this message if finalized.
174  * The link contains a counter with its refcount already
175  * incremented, but the counter itself not incremented.
176  * Ownership of link and counter refcount is passed to
177  * the message.
178  *
179  * @param message the message
180  * @param link link with counter as data
181  */
182 void
183 _dbus_message_add_size_counter_link (DBusMessage  *message,
184                                      DBusList     *link)
185 {
186   /* right now we don't recompute the delta when message
187    * size changes, and that's OK for current purposes
188    * I think, but could be important to change later.
189    * Do recompute it whenever there are no outstanding counters,
190    * since it's basically free.
191    */
192   if (message->size_counters == NULL)
193     {
194       message->size_counter_delta =
195         _dbus_string_get_length (&message->header.data) +
196         _dbus_string_get_length (&message->body);
197
198 #if 0
199       _dbus_verbose ("message has size %ld\n",
200                      message->size_counter_delta);
201 #endif
202     }
203
204   _dbus_list_append_link (&message->size_counters, link);
205
206   _dbus_counter_adjust (link->data, message->size_counter_delta);
207 }
208
209 /**
210  * Adds a counter to be incremented immediately with the
211  * size of this message, and decremented by the size
212  * of this message when this message if finalized.
213  *
214  * @param message the message
215  * @param counter the counter
216  * @returns #FALSE if no memory
217  */
218 dbus_bool_t
219 _dbus_message_add_size_counter (DBusMessage *message,
220                                 DBusCounter *counter)
221 {
222   DBusList *link;
223
224   link = _dbus_list_alloc_link (counter);
225   if (link == NULL)
226     return FALSE;
227
228   _dbus_counter_ref (counter);
229   _dbus_message_add_size_counter_link (message, link);
230
231   return TRUE;
232 }
233
234 /**
235  * Removes a counter tracking the size of this message, and decrements
236  * the counter by the size of this message.
237  *
238  * @param message the message
239  * @param link_return return the link used
240  * @param counter the counter
241  */
242 void
243 _dbus_message_remove_size_counter (DBusMessage  *message,
244                                    DBusCounter  *counter,
245                                    DBusList    **link_return)
246 {
247   DBusList *link;
248
249   link = _dbus_list_find_last (&message->size_counters,
250                                counter);
251   _dbus_assert (link != NULL);
252
253   _dbus_list_unlink (&message->size_counters,
254                      link);
255   if (link_return)
256     *link_return = link;
257   else
258     _dbus_list_free_link (link);
259
260   _dbus_counter_adjust (counter, - message->size_counter_delta);
261
262   _dbus_counter_unref (counter);
263 }
264
265 /**
266  * Locks a message. Allows checking that applications don't keep a
267  * reference to a message in the outgoing queue and change it
268  * underneath us. Messages are locked when they enter the outgoing
269  * queue (dbus_connection_send_message()), and the library complains
270  * if the message is modified while locked.
271  *
272  * @param message the message to lock.
273  */
274 void
275 _dbus_message_lock (DBusMessage  *message)
276 {
277   if (!message->locked)
278     {
279       _dbus_header_update_lengths (&message->header,
280                                    _dbus_string_get_length (&message->body));
281
282       /* must have a signature if you have a body */
283       _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
284                     dbus_message_get_signature (message) != NULL);
285
286       message->locked = TRUE;
287     }
288 }
289
290 static dbus_bool_t
291 set_or_delete_string_field (DBusMessage *message,
292                             int          field,
293                             int          typecode,
294                             const char  *value)
295 {
296   if (value == NULL)
297     return _dbus_header_delete_field (&message->header, field);
298   else
299     return _dbus_header_set_field_basic (&message->header,
300                                          field,
301                                          typecode,
302                                          &value);
303 }
304
305 #if 0
306 /* Probably we don't need to use this */
307 /**
308  * Sets the signature of the message, i.e. the arguments in the
309  * message payload. The signature includes only "in" arguments for
310  * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
311  * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
312  * what you might expect (it does not include the signature of the
313  * entire C++-style method).
314  *
315  * The signature is a string made up of type codes such as
316  * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
317  * the value of #DBUS_TYPE_INVALID). The macros such as
318  * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
319  * may find it useful to use the string forms, such as
320  * #DBUS_TYPE_INT32_AS_STRING.
321  *
322  * An "unset" or #NULL signature is considered the same as an empty
323  * signature. In fact dbus_message_get_signature() will never return
324  * #NULL.
325  *
326  * @param message the message
327  * @param signature the type signature or #NULL to unset
328  * @returns #FALSE if no memory
329  */
330 static dbus_bool_t
331 _dbus_message_set_signature (DBusMessage *message,
332                              const char  *signature)
333 {
334   _dbus_return_val_if_fail (message != NULL, FALSE);
335   _dbus_return_val_if_fail (!message->locked, FALSE);
336   _dbus_return_val_if_fail (signature == NULL ||
337                             _dbus_check_is_valid_signature (signature));
338   /* can't delete the signature if you have a message body */
339   _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
340                             signature != NULL);
341
342   return set_or_delete_string_field (message,
343                                      DBUS_HEADER_FIELD_SIGNATURE,
344                                      DBUS_TYPE_SIGNATURE,
345                                      signature);
346 }
347 #endif
348
349 /** @} */
350
351 /**
352  * @defgroup DBusMessage DBusMessage
353  * @ingroup  DBus
354  * @brief Message to be sent or received over a DBusConnection.
355  *
356  * A DBusMessage is the most basic unit of communication over a
357  * DBusConnection. A DBusConnection represents a stream of messages
358  * received from a remote application, and a stream of messages
359  * sent to a remote application.
360  *
361  * @{
362  */
363
364 /**
365  * @typedef DBusMessage
366  *
367  * Opaque data type representing a message received from or to be
368  * sent to another application.
369  */
370
371 /**
372  * Returns the serial of a message or 0 if none has been specified.
373  * The message's serial number is provided by the application sending
374  * the message and is used to identify replies to this message.  All
375  * messages received on a connection will have a serial, but messages
376  * you haven't sent yet may return 0.
377  *
378  * @param message the message
379  * @returns the client serial
380  */
381 dbus_uint32_t
382 dbus_message_get_serial (DBusMessage *message)
383 {
384   _dbus_return_val_if_fail (message != NULL, 0);
385
386   return _dbus_header_get_serial (&message->header);
387 }
388
389 /**
390  * Sets the reply serial of a message (the client serial
391  * of the message this is a reply to).
392  *
393  * @param message the message
394  * @param reply_serial the client serial
395  * @returns #FALSE if not enough memory
396  */
397 dbus_bool_t
398 dbus_message_set_reply_serial (DBusMessage   *message,
399                                dbus_uint32_t  reply_serial)
400 {
401   _dbus_return_val_if_fail (message != NULL, FALSE);
402   _dbus_return_val_if_fail (!message->locked, FALSE);
403
404   return _dbus_header_set_field_basic (&message->header,
405                                        DBUS_HEADER_FIELD_REPLY_SERIAL,
406                                        DBUS_TYPE_UINT32,
407                                        &reply_serial);
408 }
409
410 /**
411  * Returns the serial that the message is a reply to or 0 if none.
412  *
413  * @param message the message
414  * @returns the reply serial
415  */
416 dbus_uint32_t
417 dbus_message_get_reply_serial  (DBusMessage *message)
418 {
419   dbus_uint32_t v_UINT32;
420
421   _dbus_return_val_if_fail (message != NULL, 0);
422
423   if (_dbus_header_get_field_basic (&message->header,
424                                     DBUS_HEADER_FIELD_REPLY_SERIAL,
425                                     DBUS_TYPE_UINT32,
426                                     &v_UINT32))
427     return v_UINT32;
428   else
429     return 0;
430 }
431
432 static void
433 free_size_counter (void *element,
434                    void *data)
435 {
436   DBusCounter *counter = element;
437   DBusMessage *message = data;
438
439   _dbus_counter_adjust (counter, - message->size_counter_delta);
440
441   _dbus_counter_unref (counter);
442 }
443
444 static void
445 dbus_message_finalize (DBusMessage *message)
446 {
447   _dbus_assert (message->refcount.value == 0);
448
449   /* This calls application callbacks! */
450   _dbus_data_slot_list_free (&message->slot_list);
451
452   _dbus_list_foreach (&message->size_counters,
453                       free_size_counter, message);
454   _dbus_list_clear (&message->size_counters);
455
456   _dbus_header_free (&message->header);
457   _dbus_string_free (&message->body);
458
459   _dbus_assert (message->refcount.value == 0);
460   
461   dbus_free (message);
462 }
463
464 /* Message Cache
465  *
466  * We cache some DBusMessage to reduce the overhead of allocating
467  * them.  In my profiling this consistently made about an 8%
468  * difference.  It avoids the malloc for the message, the malloc for
469  * the slot list, the malloc for the header string and body string,
470  * and the associated free() calls. It does introduce another global
471  * lock which could be a performance issue in certain cases.
472  *
473  * For the echo client/server the round trip time goes from around
474  * .000077 to .000069 with the message cache on my laptop. The sysprof
475  * change is as follows (numbers are cumulative percentage):
476  *
477  *  with message cache implemented as array as it is now (0.000069 per):
478  *    new_empty_header           1.46
479  *      mutex_lock               0.56    # i.e. _DBUS_LOCK(message_cache)
480  *      mutex_unlock             0.25
481  *      self                     0.41
482  *    unref                      2.24
483  *      self                     0.68
484  *      list_clear               0.43
485  *      mutex_lock               0.33    # i.e. _DBUS_LOCK(message_cache)
486  *      mutex_unlock             0.25
487  *
488  *  with message cache implemented as list (0.000070 per roundtrip):
489  *    new_empty_header           2.72
490  *      list_pop_first           1.88
491  *    unref                      3.3
492  *      list_prepend             1.63
493  *
494  * without cache (0.000077 per roundtrip):
495  *    new_empty_header           6.7
496  *      string_init_preallocated 3.43
497  *        dbus_malloc            2.43
498  *      dbus_malloc0             2.59
499  *
500  *    unref                      4.02
501  *      string_free              1.82
502  *        dbus_free              1.63
503  *      dbus_free                0.71
504  *
505  * If you implement the message_cache with a list, the primary reason
506  * it's slower is that you add another thread lock (on the DBusList
507  * mempool).
508  */
509
510 /** Avoid caching huge messages */
511 #define MAX_MESSAGE_SIZE_TO_CACHE _DBUS_ONE_MEGABYTE
512
513 /** Avoid caching too many messages */
514 #define MAX_MESSAGE_CACHE_SIZE    5
515
516 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
517 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
518 static int message_cache_count = 0;
519 static dbus_bool_t message_cache_shutdown_registered = FALSE;
520
521 static void
522 dbus_message_cache_shutdown (void *data)
523 {
524   int i;
525
526   _DBUS_LOCK (message_cache);
527
528   i = 0;
529   while (i < MAX_MESSAGE_CACHE_SIZE)
530     {
531       if (message_cache[i])
532         dbus_message_finalize (message_cache[i]);
533
534       ++i;
535     }
536
537   message_cache_count = 0;
538   message_cache_shutdown_registered = FALSE;
539
540   _DBUS_UNLOCK (message_cache);
541 }
542
543 /**
544  * Tries to get a message from the message cache.  The retrieved
545  * message will have junk in it, so it still needs to be cleared out
546  * in dbus_message_new_empty_header()
547  *
548  * @returns the message, or #NULL if none cached
549  */
550 static DBusMessage*
551 dbus_message_get_cached (void)
552 {
553   DBusMessage *message;
554   int i;
555
556   message = NULL;
557
558   _DBUS_LOCK (message_cache);
559
560   _dbus_assert (message_cache_count >= 0);
561
562   if (message_cache_count == 0)
563     {
564       _DBUS_UNLOCK (message_cache);
565       return NULL;
566     }
567
568   /* This is not necessarily true unless count > 0, and
569    * message_cache is uninitialized until the shutdown is
570    * registered
571    */
572   _dbus_assert (message_cache_shutdown_registered);
573
574   i = 0;
575   while (i < MAX_MESSAGE_CACHE_SIZE)
576     {
577       if (message_cache[i])
578         {
579           message = message_cache[i];
580           message_cache[i] = NULL;
581           message_cache_count -= 1;
582           break;
583         }
584       ++i;
585     }
586   _dbus_assert (message_cache_count >= 0);
587   _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
588   _dbus_assert (message != NULL);
589
590   _DBUS_UNLOCK (message_cache);
591
592   _dbus_assert (message->refcount.value == 0);
593   _dbus_assert (message->size_counters == NULL);
594
595   return message;
596 }
597
598 /**
599  * Tries to cache a message, otherwise finalize it.
600  *
601  * @param message the message
602  */
603 static void
604 dbus_message_cache_or_finalize (DBusMessage *message)
605 {
606   dbus_bool_t was_cached;
607   int i;
608   
609   _dbus_assert (message->refcount.value == 0);
610
611   /* This calls application code and has to be done first thing
612    * without holding the lock
613    */
614   _dbus_data_slot_list_clear (&message->slot_list);
615
616   _dbus_list_foreach (&message->size_counters,
617                       free_size_counter, message);
618   _dbus_list_clear (&message->size_counters);
619
620   was_cached = FALSE;
621
622   _DBUS_LOCK (message_cache);
623
624   if (!message_cache_shutdown_registered)
625     {
626       _dbus_assert (message_cache_count == 0);
627
628       if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
629         goto out;
630
631       i = 0;
632       while (i < MAX_MESSAGE_CACHE_SIZE)
633         {
634           message_cache[i] = NULL;
635           ++i;
636         }
637
638       message_cache_shutdown_registered = TRUE;
639     }
640
641   _dbus_assert (message_cache_count >= 0);
642
643   if ((_dbus_string_get_length (&message->header.data) +
644        _dbus_string_get_length (&message->body)) >
645       MAX_MESSAGE_SIZE_TO_CACHE)
646     goto out;
647
648   if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
649     goto out;
650
651   /* Find empty slot */
652   i = 0;
653   while (message_cache[i] != NULL)
654     ++i;
655
656   _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
657
658   _dbus_assert (message_cache[i] == NULL);
659   message_cache[i] = message;
660   message_cache_count += 1;
661   was_cached = TRUE;
662 #ifndef DBUS_DISABLE_CHECKS
663   message->in_cache = TRUE;
664 #endif
665
666  out:
667   _DBUS_UNLOCK (message_cache);
668
669   _dbus_assert (message->refcount.value == 0);
670   
671   if (!was_cached)
672     dbus_message_finalize (message);
673 }
674
675 static DBusMessage*
676 dbus_message_new_empty_header (void)
677 {
678   DBusMessage *message;
679   dbus_bool_t from_cache;
680
681   message = dbus_message_get_cached ();
682
683   if (message != NULL)
684     {
685       from_cache = TRUE;
686     }
687   else
688     {
689       from_cache = FALSE;
690       message = dbus_new (DBusMessage, 1);
691       if (message == NULL)
692         return NULL;
693 #ifndef DBUS_DISABLE_CHECKS
694       message->generation = _dbus_current_generation;
695 #endif
696     }
697   
698   message->refcount.value = 1;
699   message->byte_order = DBUS_COMPILER_BYTE_ORDER;
700   message->locked = FALSE;
701 #ifndef DBUS_DISABLE_CHECKS
702   message->in_cache = FALSE;
703 #endif
704   message->size_counters = NULL;
705   message->size_counter_delta = 0;
706   message->changed_stamp = 0;
707
708   if (!from_cache)
709     _dbus_data_slot_list_init (&message->slot_list);
710
711   if (from_cache)
712     {
713       _dbus_header_reinit (&message->header, message->byte_order);
714       _dbus_string_set_length (&message->body, 0);
715     }
716   else
717     {
718       if (!_dbus_header_init (&message->header, message->byte_order))
719         {
720           dbus_free (message);
721           return NULL;
722         }
723
724       if (!_dbus_string_init_preallocated (&message->body, 32))
725         {
726           _dbus_header_free (&message->header);
727           dbus_free (message);
728           return NULL;
729         }
730     }
731
732   return message;
733 }
734
735 /**
736  * Constructs a new message of the given message type.
737  * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
738  * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
739  *
740  * @param message_type type of message
741  * @returns new message or #NULL If no memory
742  */
743 DBusMessage*
744 dbus_message_new (int message_type)
745 {
746   DBusMessage *message;
747
748   _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
749
750   message = dbus_message_new_empty_header ();
751   if (message == NULL)
752     return NULL;
753
754   if (!_dbus_header_create (&message->header,
755                             message_type,
756                             NULL, NULL, NULL, NULL, NULL))
757     {
758       dbus_message_unref (message);
759       return NULL;
760     }
761
762   return message;
763 }
764
765 /**
766  * Constructs a new message to invoke a method on a remote
767  * object. Returns #NULL if memory can't be allocated for the
768  * message. The destination may be #NULL in which case no destination
769  * is set; this is appropriate when using D-BUS in a peer-to-peer
770  * context (no message bus). The interface may be #NULL, which means
771  * that if multiple methods with the given name exist it is undefined
772  * which one will be invoked.
773   *
774  * @param destination name that the message should be sent to or #NULL
775  * @param path object path the message should be sent to
776  * @param interface interface to invoke method on
777  * @param method method to invoke
778  *
779  * @returns a new DBusMessage, free with dbus_message_unref()
780  * @see dbus_message_unref()
781  */
782 DBusMessage*
783 dbus_message_new_method_call (const char *destination,
784                               const char *path,
785                               const char *interface,
786                               const char *method)
787 {
788   DBusMessage *message;
789
790   _dbus_return_val_if_fail (path != NULL, NULL);
791   _dbus_return_val_if_fail (method != NULL, NULL);
792   _dbus_return_val_if_fail (destination == NULL ||
793                             _dbus_check_is_valid_bus_name (destination), NULL);
794   _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
795   _dbus_return_val_if_fail (interface == NULL ||
796                             _dbus_check_is_valid_interface (interface), NULL);
797   _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
798
799   message = dbus_message_new_empty_header ();
800   if (message == NULL)
801     return NULL;
802
803   if (!_dbus_header_create (&message->header,
804                             DBUS_MESSAGE_TYPE_METHOD_CALL,
805                             destination, path, interface, method, NULL))
806     {
807       dbus_message_unref (message);
808       return NULL;
809     }
810
811   return message;
812 }
813
814 /**
815  * Constructs a message that is a reply to a method call. Returns
816  * #NULL if memory can't be allocated for the message.
817  *
818  * @param method_call the message which the created
819  * message is a reply to.
820  * @returns a new DBusMessage, free with dbus_message_unref()
821  * @see dbus_message_new_method_call(), dbus_message_unref()
822  */
823 DBusMessage*
824 dbus_message_new_method_return (DBusMessage *method_call)
825 {
826   DBusMessage *message;
827   const char *sender;
828
829   _dbus_return_val_if_fail (method_call != NULL, NULL);
830
831   sender = dbus_message_get_sender (method_call);
832
833   /* sender is allowed to be null here in peer-to-peer case */
834
835   message = dbus_message_new_empty_header ();
836   if (message == NULL)
837     return NULL;
838
839   if (!_dbus_header_create (&message->header,
840                             DBUS_MESSAGE_TYPE_METHOD_RETURN,
841                             sender, NULL, NULL, NULL, NULL))
842     {
843       dbus_message_unref (message);
844       return NULL;
845     }
846
847   dbus_message_set_no_reply (message, TRUE);
848
849   if (!dbus_message_set_reply_serial (message,
850                                       dbus_message_get_serial (method_call)))
851     {
852       dbus_message_unref (message);
853       return NULL;
854     }
855
856   return message;
857 }
858
859 /**
860  * Constructs a new message representing a signal emission. Returns
861  * #NULL if memory can't be allocated for the message.  A signal is
862  * identified by its originating interface, and the name of the
863  * signal.
864  *
865  * @param path the path to the object emitting the signal
866  * @param interface the interface the signal is emitted from
867  * @param name name of the signal
868  * @returns a new DBusMessage, free with dbus_message_unref()
869  * @see dbus_message_unref()
870  */
871 DBusMessage*
872 dbus_message_new_signal (const char *path,
873                          const char *interface,
874                          const char *name)
875 {
876   DBusMessage *message;
877
878   _dbus_return_val_if_fail (path != NULL, NULL);
879   _dbus_return_val_if_fail (interface != NULL, NULL);
880   _dbus_return_val_if_fail (name != NULL, NULL);
881   _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
882   _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
883   _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
884
885   message = dbus_message_new_empty_header ();
886   if (message == NULL)
887     return NULL;
888
889   if (!_dbus_header_create (&message->header,
890                             DBUS_MESSAGE_TYPE_SIGNAL,
891                             NULL, path, interface, name, NULL))
892     {
893       dbus_message_unref (message);
894       return NULL;
895     }
896
897   dbus_message_set_no_reply (message, TRUE);
898
899   return message;
900 }
901
902 /**
903  * Creates a new message that is an error reply to a certain message.
904  * Error replies are possible in response to method calls primarily.
905  *
906  * @param reply_to the original message
907  * @param error_name the error name
908  * @param error_message the error message string or #NULL for none
909  * @returns a new error message
910  */
911 DBusMessage*
912 dbus_message_new_error (DBusMessage *reply_to,
913                         const char  *error_name,
914                         const char  *error_message)
915 {
916   DBusMessage *message;
917   const char *sender;
918   DBusMessageIter iter;
919
920   _dbus_return_val_if_fail (reply_to != NULL, NULL);
921   _dbus_return_val_if_fail (error_name != NULL, NULL);
922   _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
923
924   sender = dbus_message_get_sender (reply_to);
925
926   /* sender may be NULL for non-message-bus case or
927    * when the message bus is dealing with an unregistered
928    * connection.
929    */
930   message = dbus_message_new_empty_header ();
931   if (message == NULL)
932     return NULL;
933
934   if (!_dbus_header_create (&message->header,
935                             DBUS_MESSAGE_TYPE_ERROR,
936                             sender, NULL, NULL, NULL, error_name))
937     {
938       dbus_message_unref (message);
939       return NULL;
940     }
941
942   dbus_message_set_no_reply (message, TRUE);
943
944   if (!dbus_message_set_reply_serial (message,
945                                       dbus_message_get_serial (reply_to)))
946     {
947       dbus_message_unref (message);
948       return NULL;
949     }
950
951   if (error_message != NULL)
952     {
953       dbus_message_iter_init_append (message, &iter);
954       if (!dbus_message_iter_append_basic (&iter,
955                                            DBUS_TYPE_STRING,
956                                            &error_message))
957         {
958           dbus_message_unref (message);
959           return NULL;
960         }
961     }
962
963   return message;
964 }
965
966 /**
967  * Creates a new message that is an error reply to a certain message.
968  * Error replies are possible in response to method calls primarily.
969  *
970  * @param reply_to the original message
971  * @param error_name the error name
972  * @param error_format the error message format as with printf
973  * @param ... format string arguments
974  * @returns a new error message
975  */
976 DBusMessage*
977 dbus_message_new_error_printf (DBusMessage *reply_to,
978                                const char  *error_name,
979                                const char  *error_format,
980                                ...)
981 {
982   va_list args;
983   DBusString str;
984   DBusMessage *message;
985
986   _dbus_return_val_if_fail (reply_to != NULL, NULL);
987   _dbus_return_val_if_fail (error_name != NULL, NULL);
988   _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
989
990   if (!_dbus_string_init (&str))
991     return NULL;
992
993   va_start (args, error_format);
994
995   if (_dbus_string_append_printf_valist (&str, error_format, args))
996     message = dbus_message_new_error (reply_to, error_name,
997                                       _dbus_string_get_const_data (&str));
998   else
999     message = NULL;
1000
1001   _dbus_string_free (&str);
1002
1003   va_end (args);
1004
1005   return message;
1006 }
1007
1008
1009 /**
1010  * Creates a new message that is an exact replica of the message
1011  * specified, except that its refcount is set to 1, its message serial
1012  * is reset to 0, and if the original message was "locked" (in the
1013  * outgoing message queue and thus not modifiable) the new message
1014  * will not be locked.
1015  *
1016  * @param message the message.
1017  * @returns the new message.
1018  */
1019 DBusMessage *
1020 dbus_message_copy (const DBusMessage *message)
1021 {
1022   DBusMessage *retval;
1023
1024   _dbus_return_val_if_fail (message != NULL, NULL);
1025
1026   retval = dbus_new0 (DBusMessage, 1);
1027   if (retval == NULL)
1028     return NULL;
1029
1030   retval->refcount.value = 1;
1031   retval->byte_order = message->byte_order;
1032   retval->locked = FALSE;
1033 #ifndef DBUS_DISABLE_CHECKS
1034   retval->generation = message->generation;
1035 #endif
1036
1037   if (!_dbus_header_copy (&message->header, &retval->header))
1038     {
1039       dbus_free (retval);
1040       return NULL;
1041     }
1042
1043   if (!_dbus_string_init_preallocated (&retval->body,
1044                                        _dbus_string_get_length (&message->body)))
1045     {
1046       _dbus_header_free (&retval->header);
1047       dbus_free (retval);
1048       return NULL;
1049     }
1050
1051   if (!_dbus_string_copy (&message->body, 0,
1052                           &retval->body, 0))
1053     goto failed_copy;
1054
1055   return retval;
1056
1057  failed_copy:
1058   _dbus_header_free (&retval->header);
1059   _dbus_string_free (&retval->body);
1060   dbus_free (retval);
1061
1062   return NULL;
1063 }
1064
1065
1066 /**
1067  * Increments the reference count of a DBusMessage.
1068  *
1069  * @param message The message
1070  * @returns the message
1071  * @see dbus_message_unref
1072  */
1073 DBusMessage *
1074 dbus_message_ref (DBusMessage *message)
1075 {
1076   dbus_int32_t old_refcount;
1077
1078   _dbus_return_val_if_fail (message != NULL, NULL);
1079   _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1080   _dbus_return_val_if_fail (!message->in_cache, NULL);
1081   
1082   old_refcount = _dbus_atomic_inc (&message->refcount);
1083   _dbus_assert (old_refcount >= 1);
1084
1085   return message;
1086 }
1087
1088 /**
1089  * Decrements the reference count of a DBusMessage.
1090  *
1091  * @param message The message
1092  * @see dbus_message_ref
1093  */
1094 void
1095 dbus_message_unref (DBusMessage *message)
1096 {
1097  dbus_int32_t old_refcount;
1098
1099   _dbus_return_if_fail (message != NULL);
1100   _dbus_return_if_fail (message->generation == _dbus_current_generation);
1101   _dbus_return_if_fail (!message->in_cache);
1102
1103   old_refcount = _dbus_atomic_dec (&message->refcount);
1104
1105   _dbus_assert (old_refcount >= 0);
1106
1107   if (old_refcount == 1)
1108     {
1109       /* Calls application callbacks! */
1110       dbus_message_cache_or_finalize (message);
1111     }
1112 }
1113
1114 /**
1115  * Gets the type of a message. Types include
1116  * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1117  * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1118  * types are allowed and all code must silently ignore messages of
1119  * unknown type. DBUS_MESSAGE_TYPE_INVALID will never be returned,
1120  * however.
1121  *
1122  *
1123  * @param message the message
1124  * @returns the type of the message
1125  */
1126 int
1127 dbus_message_get_type (DBusMessage *message)
1128 {
1129   _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1130
1131   return _dbus_header_get_message_type (&message->header);
1132 }
1133
1134 /**
1135  * Appends fields to a message given a variable argument list. The
1136  * variable argument list should contain the type of each argument
1137  * followed by the value to append. Appendable types are basic types,
1138  * and arrays of fixed-length basic types. To append variable-length
1139  * basic types, or any more complex value, you have to use an iterator
1140  * rather than this function.
1141  *
1142  * To append a basic type, specify its type code followed by the
1143  * value. For example:
1144  *
1145  * @code
1146  * DBUS_TYPE_INT32, 42,
1147  * DBUS_TYPE_STRING, "Hello World"
1148  * @endcode
1149  * or
1150  * @code
1151  * dbus_int32_t val = 42;
1152  * DBUS_TYPE_INT32, val
1153  * @endcode
1154  *
1155  * Be sure that your provided value is the right size. For example, this
1156  * won't work:
1157  * @code
1158  * DBUS_TYPE_INT64, 42
1159  * @endcode
1160  * Because the "42" will be a 32-bit integer. You need to cast to
1161  * 64-bit.
1162  *
1163  * To append an array of fixed-length basic types, pass in the
1164  * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1165  * the array pointer, and a 32-bit integer giving the number of
1166  * elements in the array. So for example:
1167  * @code
1168  * const dbus_int32_t array[] = { 1, 2, 3 };
1169  * const dbus_int32_t *v_ARRAY = array;
1170  * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3
1171  * @endcode
1172  *
1173  * @warning in C, given "int array[]", "&array == array" (the
1174  * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1175  * So if you're using an array instead of a pointer you have to create
1176  * a pointer variable, assign the array to it, then take the address
1177  * of the pointer variable. For strings it works to write
1178  * const char *array = "Hello" and then use &array though.
1179  *
1180  * The last argument to this function must be #DBUS_TYPE_INVALID,
1181  * marking the end of the argument list.
1182  *
1183  * String/signature/path arrays should be passed in as "const char***
1184  * address_of_array" and "int n_elements"
1185  *
1186  * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1187  *
1188  * @todo If this fails due to lack of memory, the message is hosed and
1189  * you have to start over building the whole message.
1190  *
1191  * @param message the message
1192  * @param first_arg_type type of the first argument
1193  * @param ... value of first argument, list of additional type-value pairs
1194  * @returns #TRUE on success
1195  */
1196 dbus_bool_t
1197 dbus_message_append_args (DBusMessage *message,
1198                           int          first_arg_type,
1199                           ...)
1200 {
1201   dbus_bool_t retval;
1202   va_list var_args;
1203
1204   _dbus_return_val_if_fail (message != NULL, FALSE);
1205
1206   va_start (var_args, first_arg_type);
1207   retval = dbus_message_append_args_valist (message,
1208                                             first_arg_type,
1209                                             var_args);
1210   va_end (var_args);
1211
1212   return retval;
1213 }
1214
1215 /**
1216  * This function takes a va_list for use by language bindings.
1217  * It's otherwise the same as dbus_message_append_args().
1218  *
1219  * @todo for now, if this function fails due to OOM it will leave
1220  * the message half-written and you have to discard the message
1221  * and start over.
1222  *
1223  * @see dbus_message_append_args.
1224  * @param message the message
1225  * @param first_arg_type type of first argument
1226  * @param var_args value of first argument, then list of type/value pairs
1227  * @returns #TRUE on success
1228  */
1229 dbus_bool_t
1230 dbus_message_append_args_valist (DBusMessage *message,
1231                                  int          first_arg_type,
1232                                  va_list      var_args)
1233 {
1234   int type;
1235   DBusMessageIter iter;
1236
1237   _dbus_return_val_if_fail (message != NULL, FALSE);
1238
1239   type = first_arg_type;
1240
1241   dbus_message_iter_init_append (message, &iter);
1242
1243   while (type != DBUS_TYPE_INVALID)
1244     {
1245       if (_dbus_type_is_basic (type))
1246         {
1247           const DBusBasicValue *value;
1248           value = va_arg (var_args, const DBusBasicValue*);
1249
1250           if (!dbus_message_iter_append_basic (&iter,
1251                                                type,
1252                                                value))
1253             goto failed;
1254         }
1255       else if (type == DBUS_TYPE_ARRAY)
1256         {
1257           int element_type;
1258           DBusMessageIter array;
1259           char buf[2];
1260
1261           element_type = va_arg (var_args, int);
1262               
1263           buf[0] = element_type;
1264           buf[1] = '\0';
1265           if (!dbus_message_iter_open_container (&iter,
1266                                                  DBUS_TYPE_ARRAY,
1267                                                  buf,
1268                                                  &array))
1269             goto failed;
1270           
1271           if (_dbus_type_is_fixed (element_type))
1272             {
1273               const DBusBasicValue **value;
1274               int n_elements;
1275
1276               value = va_arg (var_args, const DBusBasicValue**);
1277               n_elements = va_arg (var_args, int);
1278               
1279               if (!dbus_message_iter_append_fixed_array (&array,
1280                                                          element_type,
1281                                                          value,
1282                                                          n_elements))
1283                 goto failed;
1284             }
1285           else if (element_type == DBUS_TYPE_STRING ||
1286                    element_type == DBUS_TYPE_SIGNATURE ||
1287                    element_type == DBUS_TYPE_OBJECT_PATH)
1288             {
1289               const char ***value_p;
1290               const char **value;
1291               int n_elements;
1292               int i;
1293               
1294               value_p = va_arg (var_args, const char***);
1295               n_elements = va_arg (var_args, int);
1296
1297               value = *value_p;
1298               
1299               i = 0;
1300               while (i < n_elements)
1301                 {
1302                   if (!dbus_message_iter_append_basic (&array,
1303                                                        element_type,
1304                                                        &value[i]))
1305                     goto failed;
1306                   ++i;
1307                 }
1308             }
1309           else
1310             {
1311               _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1312                           _dbus_type_to_string (element_type),
1313                           _DBUS_FUNCTION_NAME);
1314               goto failed;
1315             }
1316
1317           if (!dbus_message_iter_close_container (&iter, &array))
1318             goto failed;
1319         }
1320 #ifndef DBUS_DISABLE_CHECKS
1321       else
1322         {
1323           _dbus_warn ("type %s isn't supported yet in %s\n",
1324                       _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1325           goto failed;
1326         }
1327 #endif
1328
1329       type = va_arg (var_args, int);
1330     }
1331
1332   return TRUE;
1333
1334  failed:
1335   return FALSE;
1336 }
1337
1338 /**
1339  * Gets arguments from a message given a variable argument list.  The
1340  * supported types include those supported by
1341  * dbus_message_append_args(); that is, basic types and arrays of
1342  * fixed-length basic types.  The arguments are the same as they would
1343  * be for dbus_message_iter_get_basic() or
1344  * dbus_message_iter_get_fixed_array().
1345  *
1346  * In addition to those types, arrays of string, object path, and
1347  * signature are supported; but these are returned as allocated memory
1348  * and must be freed with dbus_free_string_array(), while the other
1349  * types are returned as const references. To get a string array
1350  * pass in "char ***array_location" and "int *n_elements"
1351  *
1352  * The variable argument list should contain the type of the argument
1353  * followed by a pointer to where the value should be stored. The list
1354  * is terminated with #DBUS_TYPE_INVALID.
1355  *
1356  * The returned values are constant; do not free them. They point
1357  * into the #DBusMessage.
1358  *
1359  * If the requested arguments are not present, or do not have the
1360  * requested types, then an error will be set.
1361  *
1362  * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1363  *
1364  * @param message the message
1365  * @param error error to be filled in on failure
1366  * @param first_arg_type the first argument type
1367  * @param ... location for first argument value, then list of type-location pairs
1368  * @returns #FALSE if the error was set
1369  */
1370 dbus_bool_t
1371 dbus_message_get_args (DBusMessage     *message,
1372                        DBusError       *error,
1373                        int              first_arg_type,
1374                        ...)
1375 {
1376   dbus_bool_t retval;
1377   va_list var_args;
1378
1379   _dbus_return_val_if_fail (message != NULL, FALSE);
1380   _dbus_return_val_if_error_is_set (error, FALSE);
1381
1382   va_start (var_args, first_arg_type);
1383   retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1384   va_end (var_args);
1385
1386   return retval;
1387 }
1388
1389 /**
1390  * This function takes a va_list for use by language bindings. It is
1391  * otherwise the same as dbus_message_get_args().
1392  *
1393  * @see dbus_message_get_args
1394  * @param message the message
1395  * @param error error to be filled in
1396  * @param first_arg_type type of the first argument
1397  * @param var_args return location for first argument, followed by list of type/location pairs
1398  * @returns #FALSE if error was set
1399  */
1400 dbus_bool_t
1401 dbus_message_get_args_valist (DBusMessage     *message,
1402                               DBusError       *error,
1403                               int              first_arg_type,
1404                               va_list          var_args)
1405 {
1406   DBusMessageIter iter;
1407
1408   _dbus_return_val_if_fail (message != NULL, FALSE);
1409   _dbus_return_val_if_error_is_set (error, FALSE);
1410
1411   dbus_message_iter_init (message, &iter);
1412   return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1413 }
1414
1415 static void
1416 _dbus_message_iter_init_common (DBusMessage         *message,
1417                                 DBusMessageRealIter *real,
1418                                 int                  iter_type)
1419 {
1420   _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1421
1422   /* Since the iterator will read or write who-knows-what from the
1423    * message, we need to get in the right byte order
1424    */
1425   ensure_byte_order (message);
1426   
1427   real->message = message;
1428   real->changed_stamp = message->changed_stamp;
1429   real->iter_type = iter_type;
1430   real->sig_refcount = 0;
1431 }
1432
1433 /**
1434  * Initializes a #DBusMessageIter for reading the arguments of the
1435  * message passed in.
1436  *
1437  * @param message the message
1438  * @param iter pointer to an iterator to initialize
1439  * @returns #FALSE if the message has no arguments
1440  */
1441 dbus_bool_t
1442 dbus_message_iter_init (DBusMessage     *message,
1443                         DBusMessageIter *iter)
1444 {
1445   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1446   const DBusString *type_str;
1447   int type_pos;
1448
1449   _dbus_return_val_if_fail (message != NULL, FALSE);
1450   _dbus_return_val_if_fail (iter != NULL, FALSE);
1451
1452   get_const_signature (&message->header, &type_str, &type_pos);
1453
1454   _dbus_message_iter_init_common (message, real,
1455                                   DBUS_MESSAGE_ITER_TYPE_READER);
1456
1457   _dbus_type_reader_init (&real->u.reader,
1458                           message->byte_order,
1459                           type_str, type_pos,
1460                           &message->body,
1461                           0);
1462
1463   return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1464 }
1465
1466 #ifndef DBUS_DISABLE_CHECKS
1467 static dbus_bool_t
1468 _dbus_message_iter_check (DBusMessageRealIter *iter)
1469 {
1470   if (iter == NULL)
1471     {
1472       _dbus_warn ("dbus message iterator is NULL\n");
1473       return FALSE;
1474     }
1475
1476   if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
1477     {
1478       if (iter->u.reader.byte_order != iter->message->byte_order)
1479         {
1480           _dbus_warn ("dbus message changed byte order since iterator was created\n");
1481           return FALSE;
1482         }
1483       /* because we swap the message into compiler order when you init an iter */
1484       _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
1485     }
1486   else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
1487     {
1488       if (iter->u.writer.byte_order != iter->message->byte_order)
1489         {
1490           _dbus_warn ("dbus message changed byte order since append iterator was created\n");
1491           return FALSE;
1492         }
1493       /* because we swap the message into compiler order when you init an iter */
1494       _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
1495     }
1496   else
1497     {
1498       _dbus_warn ("dbus message iterator looks uninitialized or corrupted\n");
1499       return FALSE;
1500     }
1501
1502   if (iter->changed_stamp != iter->message->changed_stamp)
1503     {
1504       _dbus_warn ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
1505       return FALSE;
1506     }
1507
1508   return TRUE;
1509 }
1510 #endif /* DBUS_DISABLE_CHECKS */
1511
1512 /**
1513  * Checks if an iterator has any more fields.
1514  *
1515  * @param iter the message iter
1516  * @returns #TRUE if there are more fields
1517  * following
1518  */
1519 dbus_bool_t
1520 dbus_message_iter_has_next (DBusMessageIter *iter)
1521 {
1522   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1523
1524   _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1525   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1526
1527   return _dbus_type_reader_has_next (&real->u.reader);
1528 }
1529
1530 /**
1531  * Moves the iterator to the next field, if any. If there's no next
1532  * field, returns #FALSE. If the iterator moves forward, returns
1533  * #TRUE.
1534  *
1535  * @param iter the message iter
1536  * @returns #TRUE if the iterator was moved to the next field
1537  */
1538 dbus_bool_t
1539 dbus_message_iter_next (DBusMessageIter *iter)
1540 {
1541   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1542
1543   _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1544   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1545
1546   return _dbus_type_reader_next (&real->u.reader);
1547 }
1548
1549 /**
1550  * Returns the argument type of the argument that the message iterator
1551  * points to. If the iterator is at the end of the message, returns
1552  * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1553  *
1554  * @code
1555  * dbus_message_iter_init (&iter);
1556  * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1557  *   dbus_message_iter_next (&iter);
1558  * @endcode
1559  *
1560  * @param iter the message iter
1561  * @returns the argument type
1562  */
1563 int
1564 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1565 {
1566   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1567
1568   _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1569   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1570
1571   return _dbus_type_reader_get_current_type (&real->u.reader);
1572 }
1573
1574 /**
1575  * Returns the element type of the array that the message iterator
1576  * points to. Note that you need to check that the iterator points to
1577  * an array prior to using this function.
1578  *
1579  * @param iter the message iter
1580  * @returns the array element type
1581  */
1582 int
1583 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1584 {
1585   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1586
1587   _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1588   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1589   _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1590
1591   return _dbus_type_reader_get_element_type (&real->u.reader);
1592 }
1593
1594 /**
1595  * Recurses into a container value when reading values from a message,
1596  * initializing a sub-iterator to use for traversing the child values
1597  * of the container.
1598  *
1599  * Note that this recurses into a value, not a type, so you can only
1600  * recurse if the value exists. The main implication of this is that
1601  * if you have for example an empty array of array of int32, you can
1602  * recurse into the outermost array, but it will have no values, so
1603  * you won't be able to recurse further. There's no array of int32 to
1604  * recurse into.
1605  *
1606  * @param iter the message iterator
1607  * @param sub the sub-iterator to initialize
1608  */
1609 void
1610 dbus_message_iter_recurse (DBusMessageIter  *iter,
1611                            DBusMessageIter  *sub)
1612 {
1613   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1614   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1615
1616   _dbus_return_if_fail (_dbus_message_iter_check (real));
1617   _dbus_return_if_fail (sub != NULL);
1618
1619   *real_sub = *real;
1620   _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1621 }
1622
1623 /**
1624  * Reads a basic-typed value from the message iterator.
1625  * Basic types are the non-containers such as integer and string.
1626  *
1627  * The value argument should be the address of a location to store
1628  * the returned value. So for int32 it should be a "dbus_int32_t*"
1629  * and for string a "const char**". The returned value is
1630  * by reference and should not be freed.
1631  *
1632  * All returned values are guaranteed to fit in 8 bytes. So you can
1633  * write code like this:
1634  *
1635  * @code
1636  * #ifdef DBUS_HAVE_INT64
1637  * dbus_uint64_t value;
1638  * int type;
1639  * dbus_message_iter_get_basic (&read_iter, &value);
1640  * type = dbus_message_iter_get_arg_type (&read_iter);
1641  * dbus_message_iter_append_basic (&write_iter, type, &value);
1642  * #endif
1643  * @endcode
1644  *
1645  * To avoid the #DBUS_HAVE_INT64 conditional, create a struct or
1646  * something that occupies at least 8 bytes, e.g. you could use a
1647  * struct with two int32 values in it. dbus_uint64_t is just one
1648  * example of a type that's large enough to hold any possible value.
1649  *
1650  * Be sure you have somehow checked that
1651  * dbus_message_iter_get_arg_type() matches the type you are
1652  * expecting, or you'll crash when you try to use an integer as a
1653  * string or something.
1654  *
1655  * @param iter the iterator
1656  * @param value location to store the value
1657  */
1658 void
1659 dbus_message_iter_get_basic (DBusMessageIter  *iter,
1660                              void             *value)
1661 {
1662   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1663
1664   _dbus_return_if_fail (_dbus_message_iter_check (real));
1665   _dbus_return_if_fail (value != NULL);
1666
1667   _dbus_type_reader_read_basic (&real->u.reader,
1668                                 value);
1669 }
1670
1671 /**
1672  * Reads a block of fixed-length values from the message iterator.
1673  * Fixed-length values are those basic types that are not string-like,
1674  * such as integers, bool, double. The block read will be from the
1675  * current position in the array until the end of the array.
1676  *
1677  * The value argument should be the address of a location to store the
1678  * returned array. So for int32 it should be a "const dbus_int32_t**"
1679  * The returned value is by reference and should not be freed.
1680  *
1681  * @param iter the iterator
1682  * @param value location to store the block
1683  * @param n_elements number of elements in the block
1684  */
1685 void
1686 dbus_message_iter_get_fixed_array (DBusMessageIter  *iter,
1687                                    void             *value,
1688                                    int              *n_elements)
1689 {
1690   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1691
1692   _dbus_return_if_fail (_dbus_message_iter_check (real));
1693   _dbus_return_if_fail (value != NULL);
1694   _dbus_return_if_fail (_dbus_type_is_fixed (_dbus_type_reader_get_element_type (&real->u.reader)));
1695
1696   _dbus_type_reader_read_fixed_multi (&real->u.reader,
1697                                       value, n_elements);
1698 }
1699
1700 /**
1701  * This function takes a va_list for use by language bindings and is
1702  * otherwise the same as dbus_message_iter_get_args().
1703  * dbus_message_get_args() is the place to go for complete
1704  * documentation.
1705  *
1706  * @see dbus_message_get_args
1707  * @param iter the message iter
1708  * @param error error to be filled in
1709  * @param first_arg_type type of the first argument
1710  * @param var_args return location for first argument, followed by list of type/location pairs
1711  * @returns #FALSE if error was set
1712  */
1713 dbus_bool_t
1714 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1715                                     DBusError       *error,
1716                                     int              first_arg_type,
1717                                     va_list          var_args)
1718 {
1719   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1720   int spec_type, msg_type, i;
1721   dbus_bool_t retval;
1722
1723   _dbus_assert (_dbus_message_iter_check (real));
1724
1725   retval = FALSE;
1726
1727   spec_type = first_arg_type;
1728   i = 0;
1729
1730   while (spec_type != DBUS_TYPE_INVALID)
1731     {
1732       msg_type = dbus_message_iter_get_arg_type (iter);
1733
1734       if (msg_type != spec_type)
1735         {
1736           dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1737                           "Argument %d is specified to be of type \"%s\", but "
1738                           "is actually of type \"%s\"\n", i,
1739                           _dbus_type_to_string (spec_type),
1740                           _dbus_type_to_string (msg_type));
1741
1742           goto out;
1743         }
1744
1745       if (_dbus_type_is_basic (spec_type))
1746         {
1747           DBusBasicValue *ptr;
1748
1749           ptr = va_arg (var_args, DBusBasicValue*);
1750
1751           _dbus_assert (ptr != NULL);
1752
1753           _dbus_type_reader_read_basic (&real->u.reader,
1754                                         ptr);
1755         }
1756       else if (spec_type == DBUS_TYPE_ARRAY)
1757         {
1758           int element_type;
1759           int spec_element_type;
1760           const DBusBasicValue **ptr;
1761           int *n_elements_p;
1762           DBusTypeReader array;
1763
1764           spec_element_type = va_arg (var_args, int);
1765           element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1766
1767           if (spec_element_type != element_type)
1768             {
1769               dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1770                               "Argument %d is specified to be an array of \"%s\", but "
1771                               "is actually an array of \"%s\"\n",
1772                               i,
1773                               _dbus_type_to_string (spec_element_type),
1774                               _dbus_type_to_string (element_type));
1775
1776               goto out;
1777             }
1778
1779           if (_dbus_type_is_fixed (spec_element_type))
1780             {
1781               ptr = va_arg (var_args, const DBusBasicValue**);
1782               n_elements_p = va_arg (var_args, int*);
1783
1784               _dbus_assert (ptr != NULL);
1785               _dbus_assert (n_elements_p != NULL);
1786
1787               _dbus_type_reader_recurse (&real->u.reader, &array);
1788
1789               _dbus_type_reader_read_fixed_multi (&array,
1790                                                   ptr, n_elements_p);
1791             }
1792           else if (spec_element_type == DBUS_TYPE_STRING ||
1793                    spec_element_type == DBUS_TYPE_SIGNATURE ||
1794                    spec_element_type == DBUS_TYPE_OBJECT_PATH)
1795             {
1796               char ***str_array_p;
1797               int n_elements;
1798               char **str_array;
1799
1800               str_array_p = va_arg (var_args, char***);
1801               n_elements_p = va_arg (var_args, int*);
1802
1803               _dbus_assert (str_array_p != NULL);
1804               _dbus_assert (n_elements_p != NULL);
1805
1806               /* Count elements in the array */
1807               _dbus_type_reader_recurse (&real->u.reader, &array);
1808
1809               n_elements = 0;
1810               while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
1811                 {
1812                   ++n_elements;
1813                   _dbus_type_reader_next (&array);
1814                 }
1815
1816               str_array = dbus_new0 (char*, n_elements + 1);
1817               if (str_array == NULL)
1818                 {
1819                   _DBUS_SET_OOM (error);
1820                   goto out;
1821                 }
1822
1823               /* Now go through and dup each string */
1824               _dbus_type_reader_recurse (&real->u.reader, &array);
1825
1826               i = 0;
1827               while (i < n_elements)
1828                 {
1829                   const char *s;
1830                   _dbus_type_reader_read_basic (&array,
1831                                                 &s);
1832                   
1833                   str_array[i] = _dbus_strdup (s);
1834                   if (str_array[i] == NULL)
1835                     {
1836                       dbus_free_string_array (str_array);
1837                       _DBUS_SET_OOM (error);
1838                       goto out;
1839                     }
1840                   
1841                   ++i;
1842                   
1843                   if (!_dbus_type_reader_next (&array))
1844                     _dbus_assert (i == n_elements);
1845                 }
1846
1847               _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
1848               _dbus_assert (i == n_elements);
1849               _dbus_assert (str_array[i] == NULL);
1850
1851               *str_array_p = str_array;
1852               *n_elements_p = n_elements;
1853             }
1854 #ifndef DBUS_DISABLE_CHECKS
1855           else
1856             {
1857               _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
1858                           _DBUS_FUNCTION_NAME);
1859               goto out;
1860             }
1861 #endif
1862         }
1863 #ifndef DBUS_DISABLE_CHECKS
1864       else
1865         {
1866           _dbus_warn ("you can only read arrays and basic types with %s for now\n",
1867                       _DBUS_FUNCTION_NAME);
1868           goto out;
1869         }
1870 #endif
1871
1872       spec_type = va_arg (var_args, int);
1873       if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1874         {
1875           dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1876                           "Message has only %d arguments, but more were expected", i);
1877           goto out;
1878         }
1879
1880       i++;
1881     }
1882
1883   retval = TRUE;
1884
1885  out:
1886
1887   return retval;
1888 }
1889
1890 /**
1891  * Initializes a #DBusMessageIter for appending arguments to the end
1892  * of a message.
1893  *
1894  * @todo If appending any of the arguments fails due to lack of
1895  * memory, generally the message is hosed and you have to start over
1896  * building the whole message.
1897  *
1898  * @param message the message
1899  * @param iter pointer to an iterator to initialize
1900  */
1901 void
1902 dbus_message_iter_init_append (DBusMessage     *message,
1903                                DBusMessageIter *iter)
1904 {
1905   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1906
1907   _dbus_return_if_fail (message != NULL);
1908   _dbus_return_if_fail (iter != NULL);
1909
1910   _dbus_message_iter_init_common (message, real,
1911                                   DBUS_MESSAGE_ITER_TYPE_WRITER);
1912
1913   /* We create the signature string and point iterators at it "on demand"
1914    * when a value is actually appended. That means that init() never fails
1915    * due to OOM.
1916    */
1917   _dbus_type_writer_init_types_delayed (&real->u.writer,
1918                                         message->byte_order,
1919                                         &message->body,
1920                                         _dbus_string_get_length (&message->body));
1921 }
1922
1923 /**
1924  * Creates a temporary signature string containing the current
1925  * signature, stores it in the iterator, and points the iterator to
1926  * the end of it. Used any time we write to the message.
1927  *
1928  * @param real an iterator without a type_str
1929  * @returns #FALSE if no memory
1930  */
1931 static dbus_bool_t
1932 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
1933 {
1934   DBusString *str;
1935   const DBusString *current_sig;
1936   int current_sig_pos;
1937
1938   _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1939
1940   if (real->u.writer.type_str != NULL)
1941     {
1942       _dbus_assert (real->sig_refcount > 0);
1943       real->sig_refcount += 1;
1944       return TRUE;
1945     }
1946
1947   str = dbus_new (DBusString, 1);
1948   if (str == NULL)
1949     return FALSE;
1950
1951   if (!_dbus_header_get_field_raw (&real->message->header,
1952                                    DBUS_HEADER_FIELD_SIGNATURE,
1953                                    &current_sig, &current_sig_pos))
1954     current_sig = NULL;
1955
1956   if (current_sig)
1957     {
1958       int current_len;
1959
1960       current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
1961       current_sig_pos += 1; /* move on to sig data */
1962
1963       if (!_dbus_string_init_preallocated (str, current_len + 4))
1964         {
1965           dbus_free (str);
1966           return FALSE;
1967         }
1968
1969       if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
1970                                   str, 0))
1971         {
1972           _dbus_string_free (str);
1973           dbus_free (str);
1974           return FALSE;
1975         }
1976     }
1977   else
1978     {
1979       if (!_dbus_string_init_preallocated (str, 4))
1980         {
1981           dbus_free (str);
1982           return FALSE;
1983         }
1984     }
1985
1986   real->sig_refcount = 1;
1987
1988   _dbus_type_writer_add_types (&real->u.writer,
1989                                str, _dbus_string_get_length (str));
1990   return TRUE;
1991 }
1992
1993 /**
1994  * Sets the new signature as the message signature, frees the
1995  * signature string, and marks the iterator as not having a type_str
1996  * anymore. Frees the signature even if it fails, so you can't
1997  * really recover from failure. Kinda busted.
1998  *
1999  * @param real an iterator without a type_str
2000  * @returns #FALSE if no memory
2001  */
2002 static dbus_bool_t
2003 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2004 {
2005   DBusString *str;
2006   const char *v_STRING;
2007   dbus_bool_t retval;
2008
2009   _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2010   _dbus_assert (real->u.writer.type_str != NULL);
2011   _dbus_assert (real->sig_refcount > 0);
2012
2013   real->sig_refcount -= 1;
2014
2015   if (real->sig_refcount > 0)
2016     return TRUE;
2017   _dbus_assert (real->sig_refcount == 0);
2018
2019   retval = TRUE;
2020
2021   str = real->u.writer.type_str;
2022
2023   v_STRING = _dbus_string_get_const_data (str);
2024   if (!_dbus_header_set_field_basic (&real->message->header,
2025                                      DBUS_HEADER_FIELD_SIGNATURE,
2026                                      DBUS_TYPE_SIGNATURE,
2027                                      &v_STRING))
2028     retval = FALSE;
2029
2030   _dbus_type_writer_remove_types (&real->u.writer);
2031   _dbus_string_free (str);
2032   dbus_free (str);
2033
2034   return retval;
2035 }
2036
2037 #ifndef DBUS_DISABLE_CHECKS
2038 static dbus_bool_t
2039 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2040 {
2041   if (!_dbus_message_iter_check (iter))
2042     return FALSE;
2043
2044   if (iter->message->locked)
2045     {
2046       _dbus_warn ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2047       return FALSE;
2048     }
2049
2050   return TRUE;
2051 }
2052 #endif /* DBUS_DISABLE_CHECKS */
2053
2054 /**
2055  * Appends a basic-typed value to the message. The basic types are the
2056  * non-container types such as integer and string.
2057  *
2058  * The "value" argument should be the address of a basic-typed value.
2059  * So for string, const char**. For integer, dbus_int32_t*.
2060  *
2061  * @todo If this fails due to lack of memory, the message is hosed and
2062  * you have to start over building the whole message.
2063  *
2064  * @param iter the append iterator
2065  * @param type the type of the value
2066  * @param value the address of the value
2067  * @returns #FALSE if not enough memory
2068  */
2069 dbus_bool_t
2070 dbus_message_iter_append_basic (DBusMessageIter *iter,
2071                                 int              type,
2072                                 const void      *value)
2073 {
2074   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2075   dbus_bool_t ret;
2076
2077   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2078   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2079   _dbus_return_val_if_fail (_dbus_type_is_basic (type), FALSE);
2080   _dbus_return_val_if_fail (value != NULL, FALSE);
2081
2082   if (!_dbus_message_iter_open_signature (real))
2083     return FALSE;
2084
2085   ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2086
2087   if (!_dbus_message_iter_close_signature (real))
2088     ret = FALSE;
2089
2090   return ret;
2091 }
2092
2093 /**
2094  * Appends a block of fixed-length values to an array. The
2095  * fixed-length types are all basic types that are not string-like. So
2096  * int32, double, bool, etc. You must call
2097  * dbus_message_iter_open_container() to open an array of values
2098  * before calling this function. You may call this function multiple
2099  * times (and intermixed with calls to
2100  * dbus_message_iter_append_basic()) for the same array.
2101  *
2102  * The "value" argument should be the address of the array.  So for
2103  * integer, "dbus_int32_t**" is expected for example.
2104  *
2105  * @warning in C, given "int array[]", "&array == array" (the
2106  * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2107  * So if you're using an array instead of a pointer you have to create
2108  * a pointer variable, assign the array to it, then take the address
2109  * of the pointer variable.
2110  * @code
2111  * const dbus_int32_t array[] = { 1, 2, 3 };
2112  * const dbus_int32_t *v_ARRAY = array;
2113  * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2114  *   fprintf (stderr, "No memory!\n");
2115  * @endcode
2116  * For strings it works to write const char *array = "Hello" and then
2117  * use &array though.
2118  *
2119  * @todo If this fails due to lack of memory, the message is hosed and
2120  * you have to start over building the whole message.
2121  *
2122  * @param iter the append iterator
2123  * @param element_type the type of the array elements
2124  * @param value the address of the array
2125  * @param n_elements the number of elements to append
2126  * @returns #FALSE if not enough memory
2127  */
2128 dbus_bool_t
2129 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2130                                       int              element_type,
2131                                       const void      *value,
2132                                       int              n_elements)
2133 {
2134   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2135   dbus_bool_t ret;
2136
2137   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2138   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2139   _dbus_return_val_if_fail (_dbus_type_is_fixed (element_type), FALSE);
2140   _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2141   _dbus_return_val_if_fail (value != NULL, FALSE);
2142   _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2143   _dbus_return_val_if_fail (n_elements <=
2144                             DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2145                             FALSE);
2146
2147   ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2148
2149   return ret;
2150 }
2151
2152 /**
2153  * Appends a container-typed value to the message; you are required to
2154  * append the contents of the container using the returned
2155  * sub-iterator, and then call
2156  * dbus_message_iter_close_container(). Container types are for
2157  * example struct, variant, and array. For variants, the
2158  * contained_signature should be the type of the single value inside
2159  * the variant. For structs and dict entries, contained_signature
2160  * should be #NULL; it will be set to whatever types you write into
2161  * the struct.  For arrays, contained_signature should be the type of
2162  * the array elements.
2163  *
2164  * @todo If this fails due to lack of memory, the message is hosed and
2165  * you have to start over building the whole message.
2166  *
2167  * @param iter the append iterator
2168  * @param type the type of the value
2169  * @param contained_signature the type of container contents
2170  * @param sub sub-iterator to initialize
2171  * @returns #FALSE if not enough memory
2172  */
2173 dbus_bool_t
2174 dbus_message_iter_open_container (DBusMessageIter *iter,
2175                                   int              type,
2176                                   const char      *contained_signature,
2177                                   DBusMessageIter *sub)
2178 {
2179   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2180   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2181   DBusString contained_str;
2182
2183   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2184   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2185   _dbus_return_val_if_fail (_dbus_type_is_container (type), FALSE);
2186   _dbus_return_val_if_fail (sub != NULL, FALSE);
2187   _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2188                              contained_signature == NULL) ||
2189                             (type == DBUS_TYPE_DICT_ENTRY &&
2190                              contained_signature == NULL) ||
2191                             contained_signature != NULL, FALSE);
2192   
2193 #if 0
2194   /* FIXME this would fail if the contained_signature is a dict entry,
2195    * since dict entries are invalid signatures standalone (they must be in
2196    * an array)
2197    */
2198   _dbus_return_val_if_fail (contained_signature == NULL ||
2199                             _dbus_check_is_valid_signature (contained_signature));
2200 #endif
2201
2202   if (!_dbus_message_iter_open_signature (real))
2203     return FALSE;
2204
2205   *real_sub = *real;
2206
2207   if (contained_signature != NULL)
2208     {
2209       _dbus_string_init_const (&contained_str, contained_signature);
2210
2211       return _dbus_type_writer_recurse (&real->u.writer,
2212                                         type,
2213                                         &contained_str, 0,
2214                                         &real_sub->u.writer);
2215     }
2216   else
2217     {
2218       return _dbus_type_writer_recurse (&real->u.writer,
2219                                         type,
2220                                         NULL, 0,
2221                                         &real_sub->u.writer);
2222     } 
2223 }
2224
2225
2226 /**
2227  * Closes a container-typed value appended to the message; may write
2228  * out more information to the message known only after the entire
2229  * container is written, and may free resources created by
2230  * dbus_message_iter_open_container().
2231  *
2232  * @todo If this fails due to lack of memory, the message is hosed and
2233  * you have to start over building the whole message.
2234  *
2235  * @param iter the append iterator
2236  * @param sub sub-iterator to close
2237  * @returns #FALSE if not enough memory
2238  */
2239 dbus_bool_t
2240 dbus_message_iter_close_container (DBusMessageIter *iter,
2241                                    DBusMessageIter *sub)
2242 {
2243   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2244   DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2245   dbus_bool_t ret;
2246
2247   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2248   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2249   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2250   _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2251
2252   ret = _dbus_type_writer_unrecurse (&real->u.writer,
2253                                      &real_sub->u.writer);
2254
2255   if (!_dbus_message_iter_close_signature (real))
2256     ret = FALSE;
2257
2258   return ret;
2259 }
2260
2261 /**
2262  * Sets a flag indicating that the message does not want a reply; if
2263  * this flag is set, the other end of the connection may (but is not
2264  * required to) optimize by not sending method return or error
2265  * replies. If this flag is set, there is no way to know whether the
2266  * message successfully arrived at the remote end. Normally you know a
2267  * message was received when you receive the reply to it.
2268  *
2269  * @param message the message
2270  * @param no_reply #TRUE if no reply is desired
2271  */
2272 void
2273 dbus_message_set_no_reply (DBusMessage *message,
2274                            dbus_bool_t  no_reply)
2275 {
2276   _dbus_return_if_fail (message != NULL);
2277   _dbus_return_if_fail (!message->locked);
2278
2279   _dbus_header_toggle_flag (&message->header,
2280                             DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2281                             no_reply);
2282 }
2283
2284 /**
2285  * Returns #TRUE if the message does not expect
2286  * a reply.
2287  *
2288  * @param message the message
2289  * @returns #TRUE if the message sender isn't waiting for a reply
2290  */
2291 dbus_bool_t
2292 dbus_message_get_no_reply (DBusMessage *message)
2293 {
2294   _dbus_return_val_if_fail (message != NULL, FALSE);
2295
2296   return _dbus_header_get_flag (&message->header,
2297                                 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2298 }
2299
2300 /**
2301  * Sets a flag indicating that an owner for the destination name will
2302  * be automatically started before the message is delivered. When this
2303  * flag is set, the message is held until a name owner finishes
2304  * starting up, or fails to start up. In case of failure, the reply
2305  * will be an error.
2306  *
2307  * @param message the message
2308  * @param auto_start #TRUE if auto-starting is desired
2309  */
2310 void
2311 dbus_message_set_auto_start (DBusMessage *message,
2312                              dbus_bool_t  auto_start)
2313 {
2314   _dbus_return_if_fail (message != NULL);
2315   _dbus_return_if_fail (!message->locked);
2316
2317   _dbus_header_toggle_flag (&message->header,
2318                             DBUS_HEADER_FLAG_NO_AUTO_START,
2319                             !auto_start);
2320 }
2321
2322 /**
2323  * Returns #TRUE if the message will cause an owner for
2324  * destination name to be auto-started.
2325  *
2326  * @param message the message
2327  * @returns #TRUE if the message will use auto-start
2328  */
2329 dbus_bool_t
2330 dbus_message_get_auto_start (DBusMessage *message)
2331 {
2332   _dbus_return_val_if_fail (message != NULL, FALSE);
2333
2334   return !_dbus_header_get_flag (&message->header,
2335                                  DBUS_HEADER_FLAG_NO_AUTO_START);
2336 }
2337
2338
2339 /**
2340  * Sets the object path this message is being sent to (for
2341  * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2342  * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2343  *
2344  * @param message the message
2345  * @param object_path the path or #NULL to unset
2346  * @returns #FALSE if not enough memory
2347  */
2348 dbus_bool_t
2349 dbus_message_set_path (DBusMessage   *message,
2350                        const char    *object_path)
2351 {
2352   _dbus_return_val_if_fail (message != NULL, FALSE);
2353   _dbus_return_val_if_fail (!message->locked, FALSE);
2354   _dbus_return_val_if_fail (object_path == NULL ||
2355                             _dbus_check_is_valid_path (object_path),
2356                             FALSE);
2357
2358   return set_or_delete_string_field (message,
2359                                      DBUS_HEADER_FIELD_PATH,
2360                                      DBUS_TYPE_OBJECT_PATH,
2361                                      object_path);
2362 }
2363
2364 /**
2365  * Gets the object path this message is being sent to (for
2366  * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2367  * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2368  *
2369  * @param message the message
2370  * @returns the path (should not be freed) or #NULL
2371  */
2372 const char*
2373 dbus_message_get_path (DBusMessage   *message)
2374 {
2375   const char *v;
2376
2377   _dbus_return_val_if_fail (message != NULL, NULL);
2378
2379   v = NULL; /* in case field doesn't exist */
2380   _dbus_header_get_field_basic (&message->header,
2381                                 DBUS_HEADER_FIELD_PATH,
2382                                 DBUS_TYPE_OBJECT_PATH,
2383                                 &v);
2384   return v;
2385 }
2386
2387 /**
2388  * Gets the object path this message is being sent to
2389  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2390  * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2391  * format (one array element per path component).
2392  * Free the returned array with dbus_free_string_array().
2393  *
2394  * An empty but non-NULL path array means the path "/".
2395  * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2396  * and the path "/" becomes { NULL }.
2397  *
2398  * @todo this could be optimized by using the len from the message
2399  * instead of calling strlen() again
2400  *
2401  * @param message the message
2402  * @param path place to store allocated array of path components; #NULL set here if no path field exists
2403  * @returns #FALSE if no memory to allocate the array
2404  */
2405 dbus_bool_t
2406 dbus_message_get_path_decomposed (DBusMessage   *message,
2407                                   char        ***path)
2408 {
2409   const char *v;
2410
2411   _dbus_return_val_if_fail (message != NULL, FALSE);
2412   _dbus_return_val_if_fail (path != NULL, FALSE);
2413
2414   *path = NULL;
2415
2416   v = dbus_message_get_path (message);
2417   if (v != NULL)
2418     {
2419       if (!_dbus_decompose_path (v, strlen (v),
2420                                  path, NULL))
2421         return FALSE;
2422     }
2423   return TRUE;
2424 }
2425
2426 /**
2427  * Sets the interface this message is being sent to
2428  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2429  * the interface a signal is being emitted from
2430  * (for DBUS_MESSAGE_TYPE_SIGNAL).
2431  *
2432  * @param message the message
2433  * @param interface the interface or #NULL to unset
2434  * @returns #FALSE if not enough memory
2435  */
2436 dbus_bool_t
2437 dbus_message_set_interface (DBusMessage  *message,
2438                             const char   *interface)
2439 {
2440   _dbus_return_val_if_fail (message != NULL, FALSE);
2441   _dbus_return_val_if_fail (!message->locked, FALSE);
2442   _dbus_return_val_if_fail (interface == NULL ||
2443                             _dbus_check_is_valid_interface (interface),
2444                             FALSE);
2445
2446   return set_or_delete_string_field (message,
2447                                      DBUS_HEADER_FIELD_INTERFACE,
2448                                      DBUS_TYPE_STRING,
2449                                      interface);
2450 }
2451
2452 /**
2453  * Gets the interface this message is being sent to
2454  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2455  * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2456  * The interface name is fully-qualified (namespaced).
2457  * Returns #NULL if none.
2458  *
2459  * @param message the message
2460  * @returns the message interface (should not be freed) or #NULL
2461  */
2462 const char*
2463 dbus_message_get_interface (DBusMessage *message)
2464 {
2465   const char *v;
2466
2467   _dbus_return_val_if_fail (message != NULL, NULL);
2468
2469   v = NULL; /* in case field doesn't exist */
2470   _dbus_header_get_field_basic (&message->header,
2471                                 DBUS_HEADER_FIELD_INTERFACE,
2472                                 DBUS_TYPE_STRING,
2473                                 &v);
2474   return v;
2475 }
2476
2477 /**
2478  * Sets the interface member being invoked
2479  * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2480  * (DBUS_MESSAGE_TYPE_SIGNAL).
2481  * The interface name is fully-qualified (namespaced).
2482  *
2483  * @param message the message
2484  * @param member the member or #NULL to unset
2485  * @returns #FALSE if not enough memory
2486  */
2487 dbus_bool_t
2488 dbus_message_set_member (DBusMessage  *message,
2489                          const char   *member)
2490 {
2491   _dbus_return_val_if_fail (message != NULL, FALSE);
2492   _dbus_return_val_if_fail (!message->locked, FALSE);
2493   _dbus_return_val_if_fail (member == NULL ||
2494                             _dbus_check_is_valid_member (member),
2495                             FALSE);
2496
2497   return set_or_delete_string_field (message,
2498                                      DBUS_HEADER_FIELD_MEMBER,
2499                                      DBUS_TYPE_STRING,
2500                                      member);
2501 }
2502
2503 /**
2504  * Gets the interface member being invoked
2505  * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2506  * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2507  *
2508  * @param message the message
2509  * @returns the member name (should not be freed) or #NULL
2510  */
2511 const char*
2512 dbus_message_get_member (DBusMessage *message)
2513 {
2514   const char *v;
2515
2516   _dbus_return_val_if_fail (message != NULL, NULL);
2517
2518   v = NULL; /* in case field doesn't exist */
2519   _dbus_header_get_field_basic (&message->header,
2520                                 DBUS_HEADER_FIELD_MEMBER,
2521                                 DBUS_TYPE_STRING,
2522                                 &v);
2523   return v;
2524 }
2525
2526 /**
2527  * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2528  * The name is fully-qualified (namespaced).
2529  *
2530  * @param message the message
2531  * @param error_name the name or #NULL to unset
2532  * @returns #FALSE if not enough memory
2533  */
2534 dbus_bool_t
2535 dbus_message_set_error_name (DBusMessage  *message,
2536                              const char   *error_name)
2537 {
2538   _dbus_return_val_if_fail (message != NULL, FALSE);
2539   _dbus_return_val_if_fail (!message->locked, FALSE);
2540   _dbus_return_val_if_fail (error_name == NULL ||
2541                             _dbus_check_is_valid_error_name (error_name),
2542                             FALSE);
2543
2544   return set_or_delete_string_field (message,
2545                                      DBUS_HEADER_FIELD_ERROR_NAME,
2546                                      DBUS_TYPE_STRING,
2547                                      error_name);
2548 }
2549
2550 /**
2551  * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2552  * or #NULL if none.
2553  *
2554  * @param message the message
2555  * @returns the error name (should not be freed) or #NULL
2556  */
2557 const char*
2558 dbus_message_get_error_name (DBusMessage *message)
2559 {
2560   const char *v;
2561
2562   _dbus_return_val_if_fail (message != NULL, NULL);
2563
2564   v = NULL; /* in case field doesn't exist */
2565   _dbus_header_get_field_basic (&message->header,
2566                                 DBUS_HEADER_FIELD_ERROR_NAME,
2567                                 DBUS_TYPE_STRING,
2568                                 &v);
2569   return v;
2570 }
2571
2572 /**
2573  * Sets the message's destination. The destination is the name of
2574  * another connection on the bus and may be either the unique name
2575  * assigned by the bus to each connection, or a well-known name
2576  * specified in advance.
2577  *
2578  * @param message the message
2579  * @param destination the destination name or #NULL to unset
2580  * @returns #FALSE if not enough memory
2581  */
2582 dbus_bool_t
2583 dbus_message_set_destination (DBusMessage  *message,
2584                               const char   *destination)
2585 {
2586   _dbus_return_val_if_fail (message != NULL, FALSE);
2587   _dbus_return_val_if_fail (!message->locked, FALSE);
2588   _dbus_return_val_if_fail (destination == NULL ||
2589                             _dbus_check_is_valid_bus_name (destination),
2590                             FALSE);
2591
2592   return set_or_delete_string_field (message,
2593                                      DBUS_HEADER_FIELD_DESTINATION,
2594                                      DBUS_TYPE_STRING,
2595                                      destination);
2596 }
2597
2598 /**
2599  * Gets the destination of a message or #NULL if there is none set.
2600  *
2601  * @param message the message
2602  * @returns the message destination (should not be freed) or #NULL
2603  */
2604 const char*
2605 dbus_message_get_destination (DBusMessage *message)
2606 {
2607   const char *v;
2608
2609   _dbus_return_val_if_fail (message != NULL, NULL);
2610
2611   v = NULL; /* in case field doesn't exist */
2612   _dbus_header_get_field_basic (&message->header,
2613                                 DBUS_HEADER_FIELD_DESTINATION,
2614                                 DBUS_TYPE_STRING,
2615                                 &v);
2616   return v;
2617 }
2618
2619 /**
2620  * Sets the message sender.
2621  *
2622  * @param message the message
2623  * @param sender the sender or #NULL to unset
2624  * @returns #FALSE if not enough memory
2625  */
2626 dbus_bool_t
2627 dbus_message_set_sender (DBusMessage  *message,
2628                          const char   *sender)
2629 {
2630   _dbus_return_val_if_fail (message != NULL, FALSE);
2631   _dbus_return_val_if_fail (!message->locked, FALSE);
2632   _dbus_return_val_if_fail (sender == NULL ||
2633                             _dbus_check_is_valid_bus_name (sender),
2634                             FALSE);
2635
2636   return set_or_delete_string_field (message,
2637                                      DBUS_HEADER_FIELD_SENDER,
2638                                      DBUS_TYPE_STRING,
2639                                      sender);
2640 }
2641
2642 /**
2643  * Gets the unique name of the connection which originated this
2644  * message, or #NULL if unknown or inapplicable. The sender is filled
2645  * in by the message bus.
2646  *
2647  * @param message the message
2648  * @returns the unique name of the sender or #NULL
2649  */
2650 const char*
2651 dbus_message_get_sender (DBusMessage *message)
2652 {
2653   const char *v;
2654
2655   _dbus_return_val_if_fail (message != NULL, NULL);
2656
2657   v = NULL; /* in case field doesn't exist */
2658   _dbus_header_get_field_basic (&message->header,
2659                                 DBUS_HEADER_FIELD_SENDER,
2660                                 DBUS_TYPE_STRING,
2661                                 &v);
2662   return v;
2663 }
2664
2665 /**
2666  * Gets the type signature of the message, i.e. the arguments in the
2667  * message payload. The signature includes only "in" arguments for
2668  * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2669  * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2670  * what you might expect (it does not include the signature of the
2671  * entire C++-style method).
2672  *
2673  * The signature is a string made up of type codes such as
2674  * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2675  * the value of #DBUS_TYPE_INVALID).
2676  *
2677  * @param message the message
2678  * @returns the type signature
2679  */
2680 const char*
2681 dbus_message_get_signature (DBusMessage *message)
2682 {
2683   const DBusString *type_str;
2684   int type_pos;
2685
2686   _dbus_return_val_if_fail (message != NULL, NULL);
2687
2688   get_const_signature (&message->header, &type_str, &type_pos);
2689
2690   return _dbus_string_get_const_data_len (type_str, type_pos, 0);
2691 }
2692
2693 static dbus_bool_t
2694 _dbus_message_has_type_interface_member (DBusMessage *message,
2695                                          int          type,
2696                                          const char  *interface,
2697                                          const char  *member)
2698 {
2699   const char *n;
2700
2701   _dbus_assert (message != NULL);
2702   _dbus_assert (interface != NULL);
2703   _dbus_assert (member != NULL);
2704
2705   if (dbus_message_get_type (message) != type)
2706     return FALSE;
2707
2708   /* Optimize by checking the short member name first
2709    * instead of the longer interface name
2710    */
2711
2712   n = dbus_message_get_member (message);
2713
2714   if (n && strcmp (n, member) == 0)
2715     {
2716       n = dbus_message_get_interface (message);
2717
2718       if (n == NULL || strcmp (n, interface) == 0)
2719         return TRUE;
2720     }
2721
2722   return FALSE;
2723 }
2724
2725 /**
2726  * Checks whether the message is a method call with the given
2727  * interface and member fields.  If the message is not
2728  * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
2729  * member field, returns #FALSE. If the interface field is missing,
2730  * then it will be assumed equal to the provided interface.  The D-BUS
2731  * protocol allows method callers to leave out the interface name.
2732  *
2733  * @param message the message
2734  * @param interface the name to check (must not be #NULL)
2735  * @param method the name to check (must not be #NULL)
2736  *
2737  * @returns #TRUE if the message is the specified method call
2738  */
2739 dbus_bool_t
2740 dbus_message_is_method_call (DBusMessage *message,
2741                              const char  *interface,
2742                              const char  *method)
2743 {
2744   _dbus_return_val_if_fail (message != NULL, FALSE);
2745   _dbus_return_val_if_fail (interface != NULL, FALSE);
2746   _dbus_return_val_if_fail (method != NULL, FALSE);
2747   /* don't check that interface/method are valid since it would be
2748    * expensive, and not catch many common errors
2749    */
2750
2751   return _dbus_message_has_type_interface_member (message,
2752                                                   DBUS_MESSAGE_TYPE_METHOD_CALL,
2753                                                   interface, method);
2754 }
2755
2756 /**
2757  * Checks whether the message is a signal with the given interface and
2758  * member fields.  If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
2759  * has a different interface or member field, returns #FALSE.  If the
2760  * interface field in the message is missing, it is assumed to match
2761  * any interface you pass in to this function.
2762  *
2763  * @param message the message
2764  * @param interface the name to check (must not be #NULL)
2765  * @param signal_name the name to check (must not be #NULL)
2766  *
2767  * @returns #TRUE if the message is the specified signal
2768  */
2769 dbus_bool_t
2770 dbus_message_is_signal (DBusMessage *message,
2771                         const char  *interface,
2772                         const char  *signal_name)
2773 {
2774   _dbus_return_val_if_fail (message != NULL, FALSE);
2775   _dbus_return_val_if_fail (interface != NULL, FALSE);
2776   _dbus_return_val_if_fail (signal_name != NULL, FALSE);
2777   /* don't check that interface/name are valid since it would be
2778    * expensive, and not catch many common errors
2779    */
2780
2781   return _dbus_message_has_type_interface_member (message,
2782                                                   DBUS_MESSAGE_TYPE_SIGNAL,
2783                                                   interface, signal_name);
2784 }
2785
2786 /**
2787  * Checks whether the message is an error reply with the given error
2788  * name.  If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
2789  * different name, returns #FALSE.
2790  *
2791  * @param message the message
2792  * @param error_name the name to check (must not be #NULL)
2793  *
2794  * @returns #TRUE if the message is the specified error
2795  */
2796 dbus_bool_t
2797 dbus_message_is_error (DBusMessage *message,
2798                        const char  *error_name)
2799 {
2800   const char *n;
2801
2802   _dbus_return_val_if_fail (message != NULL, FALSE);
2803   _dbus_return_val_if_fail (error_name != NULL, FALSE);
2804   /* don't check that error_name is valid since it would be expensive,
2805    * and not catch many common errors
2806    */
2807
2808   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2809     return FALSE;
2810
2811   n = dbus_message_get_error_name (message);
2812
2813   if (n && strcmp (n, error_name) == 0)
2814     return TRUE;
2815   else
2816     return FALSE;
2817 }
2818
2819 /**
2820  * Checks whether the message was sent to the given name.  If the
2821  * message has no destination specified or has a different
2822  * destination, returns #FALSE.
2823  *
2824  * @param message the message
2825  * @param name the name to check (must not be #NULL)
2826  *
2827  * @returns #TRUE if the message has the given destination name
2828  */
2829 dbus_bool_t
2830 dbus_message_has_destination (DBusMessage  *message,
2831                               const char   *name)
2832 {
2833   const char *s;
2834
2835   _dbus_return_val_if_fail (message != NULL, FALSE);
2836   _dbus_return_val_if_fail (name != NULL, FALSE);
2837   /* don't check that name is valid since it would be expensive, and
2838    * not catch many common errors
2839    */
2840
2841   s = dbus_message_get_destination (message);
2842
2843   if (s && strcmp (s, name) == 0)
2844     return TRUE;
2845   else
2846     return FALSE;
2847 }
2848
2849 /**
2850  * Checks whether the message has the given unique name as its sender.
2851  * If the message has no sender specified or has a different sender,
2852  * returns #FALSE. Note that a peer application will always have the
2853  * unique name of the connection as the sender. So you can't use this
2854  * function to see whether a sender owned a well-known name.
2855  *
2856  * Messages from the bus itself will have #DBUS_SERVICE_ORG_FREEDESKTOP_DBUS
2857  * as the sender.
2858  *
2859  * @param message the message
2860  * @param name the name to check (must not be #NULL)
2861  *
2862  * @returns #TRUE if the message has the given sender
2863  */
2864 dbus_bool_t
2865 dbus_message_has_sender (DBusMessage  *message,
2866                          const char   *name)
2867 {
2868   const char *s;
2869
2870   _dbus_return_val_if_fail (message != NULL, FALSE);
2871   _dbus_return_val_if_fail (name != NULL, FALSE);
2872   /* don't check that name is valid since it would be expensive, and
2873    * not catch many common errors
2874    */
2875
2876   s = dbus_message_get_sender (message);
2877
2878   if (s && strcmp (s, name) == 0)
2879     return TRUE;
2880   else
2881     return FALSE;
2882 }
2883
2884 /**
2885  * Checks whether the message has the given signature; see
2886  * dbus_message_get_signature() for more details on what the signature
2887  * looks like.
2888  *
2889  * @param message the message
2890  * @param signature typecode array
2891  * @returns #TRUE if message has the given signature
2892 */
2893 dbus_bool_t
2894 dbus_message_has_signature (DBusMessage   *message,
2895                             const char    *signature)
2896 {
2897   const char *s;
2898
2899   _dbus_return_val_if_fail (message != NULL, FALSE);
2900   _dbus_return_val_if_fail (signature != NULL, FALSE);
2901   /* don't check that signature is valid since it would be expensive,
2902    * and not catch many common errors
2903    */
2904
2905   s = dbus_message_get_signature (message);
2906
2907   if (s && strcmp (s, signature) == 0)
2908     return TRUE;
2909   else
2910     return FALSE;
2911 }
2912
2913 /**
2914  * Sets a #DBusError based on the contents of the given
2915  * message. The error is only set if the message
2916  * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
2917  * The name of the error is set to the name of the message,
2918  * and the error message is set to the first argument
2919  * if the argument exists and is a string.
2920  *
2921  * The return value indicates whether the error was set (the error is
2922  * set if and only if the message is an error message).  So you can
2923  * check for an error reply and convert it to DBusError in one go:
2924  * @code
2925  *  if (dbus_set_error_from_message (error, reply))
2926  *    return error;
2927  *  else
2928  *    process reply;
2929  * @endcode
2930  *
2931  * @param error the error to set
2932  * @param message the message to set it from
2933  * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
2934  */
2935 dbus_bool_t
2936 dbus_set_error_from_message (DBusError   *error,
2937                              DBusMessage *message)
2938 {
2939   const char *str;
2940
2941   _dbus_return_val_if_fail (message != NULL, FALSE);
2942   _dbus_return_val_if_error_is_set (error, FALSE);
2943
2944   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2945     return FALSE;
2946
2947   str = NULL;
2948   dbus_message_get_args (message, NULL,
2949                          DBUS_TYPE_STRING, &str,
2950                          DBUS_TYPE_INVALID);
2951
2952   dbus_set_error (error, dbus_message_get_error_name (message),
2953                   str ? "%s" : NULL, str);
2954
2955   return TRUE;
2956 }
2957
2958 /** @} */
2959
2960 /**
2961  * @addtogroup DBusMessageInternals
2962  *
2963  * @{
2964  */
2965
2966 /**
2967  * The initial buffer size of the message loader.
2968  *
2969  * @todo this should be based on min header size plus some average
2970  * body size, or something. Or rather, the min header size only, if we
2971  * want to try to read only the header, store that in a DBusMessage,
2972  * then read only the body and store that, etc., depends on
2973  * how we optimize _dbus_message_loader_get_buffer() and what
2974  * the exact message format is.
2975  */
2976 #define INITIAL_LOADER_DATA_LEN 32
2977
2978 /**
2979  * Creates a new message loader. Returns #NULL if memory can't
2980  * be allocated.
2981  *
2982  * @returns new loader, or #NULL.
2983  */
2984 DBusMessageLoader*
2985 _dbus_message_loader_new (void)
2986 {
2987   DBusMessageLoader *loader;
2988
2989   loader = dbus_new0 (DBusMessageLoader, 1);
2990   if (loader == NULL)
2991     return NULL;
2992   
2993   loader->refcount = 1;
2994
2995   loader->corrupted = FALSE;
2996   loader->corruption_reason = DBUS_VALID;
2997
2998   /* this can be configured by the app, but defaults to the protocol max */
2999   loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3000
3001   if (!_dbus_string_init (&loader->data))
3002     {
3003       dbus_free (loader);
3004       return NULL;
3005     }
3006
3007   /* preallocate the buffer for speed, ignore failure */
3008   _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3009   _dbus_string_set_length (&loader->data, 0);
3010
3011   return loader;
3012 }
3013
3014 /**
3015  * Increments the reference count of the loader.
3016  *
3017  * @param loader the loader.
3018  * @returns the loader
3019  */
3020 DBusMessageLoader *
3021 _dbus_message_loader_ref (DBusMessageLoader *loader)
3022 {
3023   loader->refcount += 1;
3024
3025   return loader;
3026 }
3027
3028 /**
3029  * Decrements the reference count of the loader and finalizes the
3030  * loader when the count reaches zero.
3031  *
3032  * @param loader the loader.
3033  */
3034 void
3035 _dbus_message_loader_unref (DBusMessageLoader *loader)
3036 {
3037   loader->refcount -= 1;
3038   if (loader->refcount == 0)
3039     {
3040       _dbus_list_foreach (&loader->messages,
3041                           (DBusForeachFunction) dbus_message_unref,
3042                           NULL);
3043       _dbus_list_clear (&loader->messages);
3044       _dbus_string_free (&loader->data);
3045       dbus_free (loader);
3046     }
3047 }
3048
3049 /**
3050  * Gets the buffer to use for reading data from the network.  Network
3051  * data is read directly into an allocated buffer, which is then used
3052  * in the DBusMessage, to avoid as many extra memcpy's as possible.
3053  * The buffer must always be returned immediately using
3054  * _dbus_message_loader_return_buffer(), even if no bytes are
3055  * successfully read.
3056  *
3057  * @todo this function can be a lot more clever. For example
3058  * it can probably always return a buffer size to read exactly
3059  * the body of the next message, thus avoiding any memory wastage
3060  * or reallocs.
3061  *
3062  * @todo we need to enforce a max length on strings in header fields.
3063  *
3064  * @param loader the message loader.
3065  * @param buffer the buffer
3066  */
3067 void
3068 _dbus_message_loader_get_buffer (DBusMessageLoader  *loader,
3069                                  DBusString        **buffer)
3070 {
3071   _dbus_assert (!loader->buffer_outstanding);
3072
3073   *buffer = &loader->data;
3074
3075   loader->buffer_outstanding = TRUE;
3076 }
3077
3078 /**
3079  * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3080  * indicating to the loader how many bytes of the buffer were filled
3081  * in. This function must always be called, even if no bytes were
3082  * successfully read.
3083  *
3084  * @param loader the loader.
3085  * @param buffer the buffer.
3086  * @param bytes_read number of bytes that were read into the buffer.
3087  */
3088 void
3089 _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
3090                                     DBusString         *buffer,
3091                                     int                 bytes_read)
3092 {
3093   _dbus_assert (loader->buffer_outstanding);
3094   _dbus_assert (buffer == &loader->data);
3095
3096   loader->buffer_outstanding = FALSE;
3097 }
3098
3099 /*
3100  * FIXME when we move the header out of the buffer, that memmoves all
3101  * buffered messages. Kind of crappy.
3102  *
3103  * Also we copy the header and body, which is kind of crappy.  To
3104  * avoid this, we have to allow header and body to be in a single
3105  * memory block, which is good for messages we read and bad for
3106  * messages we are creating. But we could move_len() the buffer into
3107  * this single memory block, and move_len() will just swap the buffers
3108  * if you're moving the entire buffer replacing the dest string.
3109  *
3110  * We could also have the message loader tell the transport how many
3111  * bytes to read; so it would first ask for some arbitrary number like
3112  * 256, then if the message was incomplete it would use the
3113  * header/body len to ask for exactly the size of the message (or
3114  * blocks the size of a typical kernel buffer for the socket). That
3115  * way we don't get trailing bytes in the buffer that have to be
3116  * memmoved. Though I suppose we also don't have a chance of reading a
3117  * bunch of small messages at once, so the optimization may be stupid.
3118  *
3119  * Another approach would be to keep a "start" index into
3120  * loader->data and only delete it occasionally, instead of after
3121  * each message is loaded.
3122  *
3123  * load_message() returns FALSE if not enough memory OR the loader was corrupted
3124  */
3125 static dbus_bool_t
3126 load_message (DBusMessageLoader *loader,
3127               DBusMessage       *message,
3128               int                byte_order,
3129               int                fields_array_len,
3130               int                header_len,
3131               int                body_len)
3132 {
3133   dbus_bool_t oom;
3134   DBusValidity validity;
3135   const DBusString *type_str;
3136   int type_pos;
3137   DBusValidationMode mode;
3138
3139   mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3140   
3141   oom = FALSE;
3142
3143 #if 0
3144   _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3145 #endif
3146
3147   /* 1. VALIDATE AND COPY OVER HEADER */
3148   _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3149   _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3150
3151   if (!_dbus_header_load (&message->header,
3152                           mode,
3153                           &validity,
3154                           byte_order,
3155                           fields_array_len,
3156                           header_len,
3157                           body_len,
3158                           &loader->data, 0,
3159                           _dbus_string_get_length (&loader->data)))
3160     {
3161       _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3162       if (validity == DBUS_VALID)
3163         oom = TRUE;
3164       else
3165         {
3166           loader->corrupted = TRUE;
3167           loader->corruption_reason = validity;
3168         }
3169       goto failed;
3170     }
3171
3172   _dbus_assert (validity == DBUS_VALID);
3173
3174   message->byte_order = byte_order;
3175
3176   /* 2. VALIDATE BODY */
3177   if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3178     {
3179       get_const_signature (&message->header, &type_str, &type_pos);
3180       
3181       /* Because the bytes_remaining arg is NULL, this validates that the
3182        * body is the right length
3183        */
3184       validity = _dbus_validate_body_with_reason (type_str,
3185                                                   type_pos,
3186                                                   byte_order,
3187                                                   NULL,
3188                                                   &loader->data,
3189                                                   header_len,
3190                                                   body_len);
3191       if (validity != DBUS_VALID)
3192         {
3193           _dbus_verbose ("Failed to validate message body code %d\n", validity);
3194
3195           loader->corrupted = TRUE;
3196           loader->corruption_reason = validity;
3197           
3198           goto failed;
3199         }
3200     }
3201
3202   /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3203
3204   if (!_dbus_list_append (&loader->messages, message))
3205     {
3206       _dbus_verbose ("Failed to append new message to loader queue\n");
3207       oom = TRUE;
3208       goto failed;
3209     }
3210
3211   _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3212   _dbus_assert (_dbus_string_get_length (&loader->data) >=
3213                 (header_len + body_len));
3214
3215   if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3216     {
3217       _dbus_verbose ("Failed to move body into new message\n");
3218       oom = TRUE;
3219       goto failed;
3220     }
3221
3222   _dbus_string_delete (&loader->data, 0, header_len + body_len);
3223
3224   _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3225   _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3226
3227   _dbus_verbose ("Loaded message %p\n", message);
3228
3229   _dbus_assert (!oom);
3230   _dbus_assert (!loader->corrupted);
3231   _dbus_assert (loader->messages != NULL);
3232   _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3233
3234   return TRUE;
3235
3236  failed:
3237
3238   /* Clean up */
3239
3240   /* does nothing if the message isn't in the list */
3241   _dbus_list_remove_last (&loader->messages, message);
3242   
3243   if (oom)
3244     _dbus_assert (!loader->corrupted);
3245   else
3246     _dbus_assert (loader->corrupted);
3247
3248   _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3249
3250   return FALSE;
3251 }
3252
3253 /**
3254  * Converts buffered data into messages, if we have enough data.  If
3255  * we don't have enough data, does nothing.
3256  *
3257  * @todo we need to check that the proper named header fields exist
3258  * for each message type.
3259  *
3260  * @todo If a message has unknown type, we should probably eat it
3261  * right here rather than passing it out to applications.  However
3262  * it's not an error to see messages of unknown type.
3263  *
3264  * @param loader the loader.
3265  * @returns #TRUE if we had enough memory to finish.
3266  */
3267 dbus_bool_t
3268 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3269 {
3270   while (!loader->corrupted &&
3271          _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3272     {
3273       DBusValidity validity;
3274       int byte_order, fields_array_len, header_len, body_len;
3275
3276       if (_dbus_header_have_message_untrusted (loader->max_message_size,
3277                                                &validity,
3278                                                &byte_order,
3279                                                &fields_array_len,
3280                                                &header_len,
3281                                                &body_len,
3282                                                &loader->data, 0,
3283                                                _dbus_string_get_length (&loader->data)))
3284         {
3285           DBusMessage *message;
3286
3287           _dbus_assert (validity == DBUS_VALID);
3288
3289           message = dbus_message_new_empty_header ();
3290           if (message == NULL)
3291             return FALSE;
3292
3293           if (!load_message (loader, message,
3294                              byte_order, fields_array_len,
3295                              header_len, body_len))
3296             {
3297               dbus_message_unref (message);
3298               /* load_message() returns false if corrupted or OOM; if
3299                * corrupted then return TRUE for not OOM
3300                */
3301               return loader->corrupted;
3302             }
3303
3304           _dbus_assert (loader->messages != NULL);
3305           _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3306         }
3307       else
3308         {
3309           _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3310                          validity);
3311           if (validity != DBUS_VALID)
3312             {
3313               loader->corrupted = TRUE;
3314               loader->corruption_reason = validity;
3315             }
3316           return TRUE;
3317         }
3318     }
3319
3320   return TRUE;
3321 }
3322
3323 /**
3324  * Peeks at first loaded message, returns #NULL if no messages have
3325  * been queued.
3326  *
3327  * @param loader the loader.
3328  * @returns the next message, or #NULL if none.
3329  */
3330 DBusMessage*
3331 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3332 {
3333   if (loader->messages)
3334     return loader->messages->data;
3335   else
3336     return NULL;
3337 }
3338
3339 /**
3340  * Pops a loaded message (passing ownership of the message
3341  * to the caller). Returns #NULL if no messages have been
3342  * queued.
3343  *
3344  * @param loader the loader.
3345  * @returns the next message, or #NULL if none.
3346  */
3347 DBusMessage*
3348 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3349 {
3350   return _dbus_list_pop_first (&loader->messages);
3351 }
3352
3353 /**
3354  * Pops a loaded message inside a list link (passing ownership of the
3355  * message and link to the caller). Returns #NULL if no messages have
3356  * been loaded.
3357  *
3358  * @param loader the loader.
3359  * @returns the next message link, or #NULL if none.
3360  */
3361 DBusList*
3362 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3363 {
3364   return _dbus_list_pop_first_link (&loader->messages);
3365 }
3366
3367 /**
3368  * Returns a popped message link, used to undo a pop.
3369  *
3370  * @param loader the loader
3371  * @param link the link with a message in it
3372  */
3373 void
3374 _dbus_message_loader_putback_message_link (DBusMessageLoader  *loader,
3375                                            DBusList           *link)
3376 {
3377   _dbus_list_prepend_link (&loader->messages, link);
3378 }
3379
3380 /**
3381  * Checks whether the loader is confused due to bad data.
3382  * If messages are received that are invalid, the
3383  * loader gets confused and gives up permanently.
3384  * This state is called "corrupted."
3385  *
3386  * @param loader the loader
3387  * @returns #TRUE if the loader is hosed.
3388  */
3389 dbus_bool_t
3390 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3391 {
3392   _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3393                 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3394   return loader->corrupted;
3395 }
3396
3397 /**
3398  * Sets the maximum size message we allow.
3399  *
3400  * @param loader the loader
3401  * @param size the max message size in bytes
3402  */
3403 void
3404 _dbus_message_loader_set_max_message_size (DBusMessageLoader  *loader,
3405                                            long                size)
3406 {
3407   if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3408     {
3409       _dbus_verbose ("clamping requested max message size %ld to %d\n",
3410                      size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3411       size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3412     }
3413   loader->max_message_size = size;
3414 }
3415
3416 /**
3417  * Gets the maximum allowed message size in bytes.
3418  *
3419  * @param loader the loader
3420  * @returns max size in bytes
3421  */
3422 long
3423 _dbus_message_loader_get_max_message_size (DBusMessageLoader  *loader)
3424 {
3425   return loader->max_message_size;
3426 }
3427
3428 static DBusDataSlotAllocator slot_allocator;
3429 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3430
3431 /**
3432  * Allocates an integer ID to be used for storing application-specific
3433  * data on any DBusMessage. The allocated ID may then be used
3434  * with dbus_message_set_data() and dbus_message_get_data().
3435  * The passed-in slot must be initialized to -1, and is filled in
3436  * with the slot ID. If the passed-in slot is not -1, it's assumed
3437  * to be already allocated, and its refcount is incremented.
3438  *
3439  * The allocated slot is global, i.e. all DBusMessage objects will
3440  * have a slot with the given integer ID reserved.
3441  *
3442  * @param slot_p address of a global variable storing the slot
3443  * @returns #FALSE on failure (no memory)
3444  */
3445 dbus_bool_t
3446 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3447 {
3448   return _dbus_data_slot_allocator_alloc (&slot_allocator,
3449                                           _DBUS_LOCK_NAME (message_slots),
3450                                           slot_p);
3451 }
3452
3453 /**
3454  * Deallocates a global ID for message data slots.
3455  * dbus_message_get_data() and dbus_message_set_data() may no
3456  * longer be used with this slot.  Existing data stored on existing
3457  * DBusMessage objects will be freed when the message is
3458  * finalized, but may not be retrieved (and may only be replaced if
3459  * someone else reallocates the slot).  When the refcount on the
3460  * passed-in slot reaches 0, it is set to -1.
3461  *
3462  * @param slot_p address storing the slot to deallocate
3463  */
3464 void
3465 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3466 {
3467   _dbus_return_if_fail (*slot_p >= 0);
3468
3469   _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3470 }
3471
3472 /**
3473  * Stores a pointer on a DBusMessage, along
3474  * with an optional function to be used for freeing
3475  * the data when the data is set again, or when
3476  * the message is finalized. The slot number
3477  * must have been allocated with dbus_message_allocate_data_slot().
3478  *
3479  * @param message the message
3480  * @param slot the slot number
3481  * @param data the data to store
3482  * @param free_data_func finalizer function for the data
3483  * @returns #TRUE if there was enough memory to store the data
3484  */
3485 dbus_bool_t
3486 dbus_message_set_data (DBusMessage     *message,
3487                        dbus_int32_t     slot,
3488                        void            *data,
3489                        DBusFreeFunction free_data_func)
3490 {
3491   DBusFreeFunction old_free_func;
3492   void *old_data;
3493   dbus_bool_t retval;
3494
3495   _dbus_return_val_if_fail (message != NULL, FALSE);
3496   _dbus_return_val_if_fail (slot >= 0, FALSE);
3497
3498   retval = _dbus_data_slot_list_set (&slot_allocator,
3499                                      &message->slot_list,
3500                                      slot, data, free_data_func,
3501                                      &old_free_func, &old_data);
3502
3503   if (retval)
3504     {
3505       /* Do the actual free outside the message lock */
3506       if (old_free_func)
3507         (* old_free_func) (old_data);
3508     }
3509
3510   return retval;
3511 }
3512
3513 /**
3514  * Retrieves data previously set with dbus_message_set_data().
3515  * The slot must still be allocated (must not have been freed).
3516  *
3517  * @param message the message
3518  * @param slot the slot to get data from
3519  * @returns the data, or #NULL if not found
3520  */
3521 void*
3522 dbus_message_get_data (DBusMessage   *message,
3523                        dbus_int32_t   slot)
3524 {
3525   void *res;
3526
3527   _dbus_return_val_if_fail (message != NULL, NULL);
3528
3529   res = _dbus_data_slot_list_get (&slot_allocator,
3530                                   &message->slot_list,
3531                                   slot);
3532
3533   return res;
3534 }
3535
3536 /**
3537  * Utility function to convert a machine-readable (not translated)
3538  * string into a D-BUS message type.
3539  *
3540  * @code
3541  *   "method_call"    -> DBUS_MESSAGE_TYPE_METHOD_CALL
3542  *   "method_return"  -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3543  *   "signal"         -> DBUS_MESSAGE_TYPE_SIGNAL
3544  *   "error"          -> DBUS_MESSAGE_TYPE_ERROR
3545  *   anything else    -> DBUS_MESSAGE_TYPE_INVALID
3546  * @endcode
3547  *
3548  */
3549 int
3550 dbus_message_type_from_string (const char *type_str)
3551 {
3552   if (strcmp (type_str, "method_call") == 0)
3553     return DBUS_MESSAGE_TYPE_METHOD_CALL;
3554   if (strcmp (type_str, "method_return") == 0)
3555     return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3556   else if (strcmp (type_str, "signal") == 0)
3557     return DBUS_MESSAGE_TYPE_SIGNAL;
3558   else if (strcmp (type_str, "error") == 0)
3559     return DBUS_MESSAGE_TYPE_ERROR;
3560   else
3561     return DBUS_MESSAGE_TYPE_INVALID;
3562 }
3563
3564 /**
3565  * Utility function to convert a D-BUS message type into a
3566  * machine-readable string (not translated).
3567  *
3568  * @code
3569  *   DBUS_MESSAGE_TYPE_METHOD_CALL    -> "method_call"
3570  *   DBUS_MESSAGE_TYPE_METHOD_RETURN  -> "method_return"
3571  *   DBUS_MESSAGE_TYPE_SIGNAL         -> "signal"
3572  *   DBUS_MESSAGE_TYPE_ERROR          -> "error"
3573  *   DBUS_MESSAGE_TYPE_INVALID        -> "invalid"
3574  * @endcode
3575  *
3576  */
3577 const char *
3578 dbus_message_type_to_string (int type)
3579 {
3580   switch (type)
3581     {
3582     case DBUS_MESSAGE_TYPE_METHOD_CALL:
3583       return "method_call";
3584     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3585       return "method_return";
3586     case DBUS_MESSAGE_TYPE_SIGNAL:
3587       return "signal";
3588     case DBUS_MESSAGE_TYPE_ERROR:
3589       return "error";
3590     default:
3591       return "invalid";
3592     }
3593 }
3594
3595 /** @} */
3596
3597 /* tests in dbus-message-util.c */