1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
25 #include "dbus-internals.h"
26 #include "dbus-marshal-recursive.h"
27 #include "dbus-marshal-validate.h"
28 #include "dbus-marshal-header.h"
29 #include "dbus-message.h"
30 #include "dbus-message-internal.h"
31 #include "dbus-object-tree.h"
32 #include "dbus-memory.h"
33 #include "dbus-list.h"
34 #include "dbus-dataslot.h"
38 * @defgroup DBusMessageInternals DBusMessage implementation details
39 * @ingroup DBusInternals
40 * @brief DBusMessage private implementation details.
42 * The guts of DBusMessage and its methods.
47 static dbus_bool_t dbus_message_iter_get_args (DBusMessageIter *iter,
51 static dbus_bool_t dbus_message_iter_get_args_valist (DBusMessageIter *iter,
56 /* Not thread locked, but strictly const/read-only so should be OK
58 /** An static string representing an empty signature */
59 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
61 /** How many bits are in the changed_stamp used to validate iterators */
62 #define CHANGED_STAMP_BITS 21
65 * @brief Internals of DBusMessage
67 * Object representing a message received from or to be sent to
68 * another application. This is an opaque object, all members
73 DBusAtomic refcount; /**< Reference count */
75 DBusHeader header; /**< Header network data and associated cache */
77 DBusString body; /**< Body network data. */
79 char byte_order; /**< Message byte order. */
81 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
83 DBusList *size_counters; /**< 0-N DBusCounter used to track message size. */
84 long size_counter_delta; /**< Size we incremented the size counters by. */
86 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< Incremented when iterators are invalidated. */
88 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
90 #ifndef DBUS_DISABLE_CHECKS
91 int generation; /**< _dbus_current_generation when message was created */
95 /* these have wacky values to help trap uninitialized iterators;
96 * but has to fit in 3 bits
99 DBUS_MESSAGE_ITER_TYPE_READER = 3,
100 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
103 /** typedef for internals of message iterator */
104 typedef struct DBusMessageRealIter DBusMessageRealIter;
107 * @brief Internals of DBusMessageIter
109 * Object representing a position in a message. All fields are internal.
111 struct DBusMessageRealIter
113 DBusMessage *message; /**< Message used */
114 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
115 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
116 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
119 DBusTypeWriter writer; /**< writer */
120 DBusTypeReader reader; /**< reader */
121 } u; /**< the type writer or reader that does all the work */
125 * Gets the data to be sent over the network for this message.
126 * The header and then the body should be written out.
127 * This function is guaranteed to always return the same
128 * data once a message is locked (with _dbus_message_lock()).
130 * @param message the message.
131 * @param header return location for message header data.
132 * @param body return location for message body data.
135 _dbus_message_get_network_data (DBusMessage *message,
136 const DBusString **header,
137 const DBusString **body)
139 _dbus_assert (message->locked);
141 *header = &message->header.data;
142 *body = &message->body;
146 * Sets the serial number of a message.
147 * This can only be done once on a message.
149 * @param message the message
150 * @param serial the serial
153 _dbus_message_set_serial (DBusMessage *message,
154 dbus_uint32_t serial)
156 _dbus_return_if_fail (message != NULL);
157 _dbus_return_if_fail (!message->locked);
158 _dbus_return_if_fail (dbus_message_get_serial (message) == 0);
160 _dbus_header_set_serial (&message->header, serial);
164 * Adds a counter to be incremented immediately with the
165 * size of this message, and decremented by the size
166 * of this message when this message if finalized.
167 * The link contains a counter with its refcount already
168 * incremented, but the counter itself not incremented.
169 * Ownership of link and counter refcount is passed to
172 * @param message the message
173 * @param link link with counter as data
176 _dbus_message_add_size_counter_link (DBusMessage *message,
179 /* right now we don't recompute the delta when message
180 * size changes, and that's OK for current purposes
181 * I think, but could be important to change later.
182 * Do recompute it whenever there are no outstanding counters,
183 * since it's basically free.
185 if (message->size_counters == NULL)
187 message->size_counter_delta =
188 _dbus_string_get_length (&message->header.data) +
189 _dbus_string_get_length (&message->body);
192 _dbus_verbose ("message has size %ld\n",
193 message->size_counter_delta);
197 _dbus_list_append_link (&message->size_counters, link);
199 _dbus_counter_adjust (link->data, message->size_counter_delta);
203 * Adds a counter to be incremented immediately with the
204 * size of this message, and decremented by the size
205 * of this message when this message if finalized.
207 * @param message the message
208 * @param counter the counter
209 * @returns #FALSE if no memory
212 _dbus_message_add_size_counter (DBusMessage *message,
213 DBusCounter *counter)
217 link = _dbus_list_alloc_link (counter);
221 _dbus_counter_ref (counter);
222 _dbus_message_add_size_counter_link (message, link);
228 * Removes a counter tracking the size of this message, and decrements
229 * the counter by the size of this message.
231 * @param message the message
232 * @param link_return return the link used
233 * @param counter the counter
236 _dbus_message_remove_size_counter (DBusMessage *message,
237 DBusCounter *counter,
238 DBusList **link_return)
242 link = _dbus_list_find_last (&message->size_counters,
244 _dbus_assert (link != NULL);
246 _dbus_list_unlink (&message->size_counters,
251 _dbus_list_free_link (link);
253 _dbus_counter_adjust (counter, - message->size_counter_delta);
255 _dbus_counter_unref (counter);
259 * Locks a message. Allows checking that applications don't keep a
260 * reference to a message in the outgoing queue and change it
261 * underneath us. Messages are locked when they enter the outgoing
262 * queue (dbus_connection_send_message()), and the library complains
263 * if the message is modified while locked.
265 * @param message the message to lock.
268 _dbus_message_lock (DBusMessage *message)
270 if (!message->locked)
272 _dbus_header_update_lengths (&message->header,
273 _dbus_string_get_length (&message->body));
275 /* must have a signature if you have a body */
276 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
277 dbus_message_get_signature (message) != NULL);
279 message->locked = TRUE;
284 set_or_delete_string_field (DBusMessage *message,
290 return _dbus_header_delete_field (&message->header, field);
292 return _dbus_header_set_field_basic (&message->header,
299 get_const_signature (DBusHeader *header,
300 const DBusString **type_str_p,
303 if (_dbus_header_get_field_raw (header,
304 DBUS_HEADER_FIELD_SIGNATURE,
308 *type_pos_p += 1; /* skip the signature length which is 1 byte */
312 *type_str_p = &_dbus_empty_signature_str;
318 /* Probably we don't need to use this */
320 * Sets the signature of the message, i.e. the arguments in the
321 * message payload. The signature includes only "in" arguments for
322 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
323 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
324 * what you might expect (it does not include the signature of the
325 * entire C++-style method).
327 * The signature is a string made up of type codes such as
328 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
329 * the value of #DBUS_TYPE_INVALID). The macros such as
330 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
331 * may find it useful to use the string forms, such as
332 * #DBUS_TYPE_INT32_AS_STRING.
334 * An "unset" or #NULL signature is considered the same as an empty
335 * signature. In fact dbus_message_get_signature() will never return
338 * @param message the message
339 * @param signature the type signature or #NULL to unset
340 * @returns #FALSE if no memory
343 _dbus_message_set_signature (DBusMessage *message,
344 const char *signature)
346 _dbus_return_val_if_fail (message != NULL, FALSE);
347 _dbus_return_val_if_fail (!message->locked, FALSE);
348 _dbus_return_val_if_fail (signature == NULL ||
349 _dbus_check_is_valid_signature (signature));
350 /* can't delete the signature if you have a message body */
351 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
354 return set_or_delete_string_field (message,
355 DBUS_HEADER_FIELD_SIGNATURE,
364 * @defgroup DBusMessage DBusMessage
366 * @brief Message to be sent or received over a DBusConnection.
368 * A DBusMessage is the most basic unit of communication over a
369 * DBusConnection. A DBusConnection represents a stream of messages
370 * received from a remote application, and a stream of messages
371 * sent to a remote application.
377 * @typedef DBusMessage
379 * Opaque data type representing a message received from or to be
380 * sent to another application.
384 * Returns the serial of a message or 0 if none has been specified.
385 * The message's serial number is provided by the application sending
386 * the message and is used to identify replies to this message. All
387 * messages received on a connection will have a serial, but messages
388 * you haven't sent yet may return 0.
390 * @param message the message
391 * @returns the client serial
394 dbus_message_get_serial (DBusMessage *message)
396 _dbus_return_val_if_fail (message != NULL, 0);
398 return _dbus_header_get_serial (&message->header);
402 * Sets the reply serial of a message (the client serial
403 * of the message this is a reply to).
405 * @param message the message
406 * @param reply_serial the client serial
407 * @returns #FALSE if not enough memory
410 dbus_message_set_reply_serial (DBusMessage *message,
411 dbus_uint32_t reply_serial)
413 _dbus_return_val_if_fail (message != NULL, FALSE);
414 _dbus_return_val_if_fail (!message->locked, FALSE);
416 return _dbus_header_set_field_basic (&message->header,
417 DBUS_HEADER_FIELD_REPLY_SERIAL,
423 * Returns the serial that the message is a reply to or 0 if none.
425 * @param message the message
426 * @returns the reply serial
429 dbus_message_get_reply_serial (DBusMessage *message)
431 dbus_uint32_t v_UINT32;
433 _dbus_return_val_if_fail (message != NULL, 0);
435 if (_dbus_header_get_field_basic (&message->header,
436 DBUS_HEADER_FIELD_REPLY_SERIAL,
445 free_size_counter (void *element,
448 DBusCounter *counter = element;
449 DBusMessage *message = data;
451 _dbus_counter_adjust (counter, - message->size_counter_delta);
453 _dbus_counter_unref (counter);
457 dbus_message_finalize (DBusMessage *message)
459 _dbus_assert (message->refcount.value == 0);
461 /* This calls application callbacks! */
462 _dbus_data_slot_list_free (&message->slot_list);
464 _dbus_list_foreach (&message->size_counters,
465 free_size_counter, message);
466 _dbus_list_clear (&message->size_counters);
468 _dbus_header_free (&message->header);
469 _dbus_string_free (&message->body);
476 * We cache some DBusMessage to reduce the overhead of allocating
477 * them. In my profiling this consistently made about an 8%
478 * difference. It avoids the malloc for the message, the malloc for
479 * the slot list, the malloc for the header string and body string,
480 * and the associated free() calls. It does introduce another global
481 * lock which could be a performance issue in certain cases.
483 * For the echo client/server the round trip time goes from around
484 * .000077 to .000069 with the message cache on my laptop. The sysprof
485 * change is as follows (numbers are cumulative percentage):
487 * with message cache implemented as array as it is now (0.000069 per):
488 * new_empty_header 1.46
489 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
495 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
498 * with message cache implemented as list (0.000070 per roundtrip):
499 * new_empty_header 2.72
500 * list_pop_first 1.88
504 * without cache (0.000077 per roundtrip):
505 * new_empty_header 6.7
506 * string_init_preallocated 3.43
515 * If you implement the message_cache with a list, the primary reason
516 * it's slower is that you add another thread lock (on the DBusList
520 /** Avoid caching huge messages */
521 #define MAX_MESSAGE_SIZE_TO_CACHE _DBUS_ONE_MEGABYTE
523 /** Avoid caching too many messages */
524 #define MAX_MESSAGE_CACHE_SIZE 5
526 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
527 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
528 static int message_cache_count = 0;
529 static dbus_bool_t message_cache_shutdown_registered = FALSE;
532 dbus_message_cache_shutdown (void *data)
536 _DBUS_LOCK (message_cache);
539 while (i < MAX_MESSAGE_CACHE_SIZE)
541 if (message_cache[i])
542 dbus_message_finalize (message_cache[i]);
547 message_cache_count = 0;
548 message_cache_shutdown_registered = FALSE;
550 _DBUS_UNLOCK (message_cache);
554 * Tries to get a message from the message cache. The retrieved
555 * message will have junk in it, so it still needs to be cleared out
556 * in dbus_message_new_empty_header()
558 * @returns the message, or #NULL if none cached
561 dbus_message_get_cached (void)
563 DBusMessage *message;
568 _DBUS_LOCK (message_cache);
570 _dbus_assert (message_cache_count >= 0);
572 if (message_cache_count == 0)
574 _DBUS_UNLOCK (message_cache);
578 /* This is not necessarily true unless count > 0, and
579 * message_cache is uninitialized until the shutdown is
582 _dbus_assert (message_cache_shutdown_registered);
585 while (i < MAX_MESSAGE_CACHE_SIZE)
587 if (message_cache[i])
589 message = message_cache[i];
590 message_cache[i] = NULL;
591 message_cache_count -= 1;
596 _dbus_assert (message_cache_count >= 0);
597 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
598 _dbus_assert (message != NULL);
600 _DBUS_UNLOCK (message_cache);
602 _dbus_assert (message->refcount.value == 0);
603 _dbus_assert (message->size_counters == NULL);
609 * Tries to cache a message, otherwise finalize it.
611 * @param message the message
614 dbus_message_cache_or_finalize (DBusMessage *message)
616 dbus_bool_t was_cached;
619 _dbus_assert (message->refcount.value == 0);
621 /* This calls application code and has to be done first thing
622 * without holding the lock
624 _dbus_data_slot_list_clear (&message->slot_list);
626 _dbus_list_foreach (&message->size_counters,
627 free_size_counter, message);
628 _dbus_list_clear (&message->size_counters);
632 _DBUS_LOCK (message_cache);
634 if (!message_cache_shutdown_registered)
636 _dbus_assert (message_cache_count == 0);
638 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
642 while (i < MAX_MESSAGE_CACHE_SIZE)
644 message_cache[i] = NULL;
648 message_cache_shutdown_registered = TRUE;
651 _dbus_assert (message_cache_count >= 0);
653 if ((_dbus_string_get_length (&message->header.data) +
654 _dbus_string_get_length (&message->body)) >
655 MAX_MESSAGE_SIZE_TO_CACHE)
658 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
661 /* Find empty slot */
663 while (message_cache[i] != NULL)
666 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
668 _dbus_assert (message_cache[i] == NULL);
669 message_cache[i] = message;
670 message_cache_count += 1;
674 _DBUS_UNLOCK (message_cache);
677 dbus_message_finalize (message);
681 dbus_message_new_empty_header (void)
683 DBusMessage *message;
684 dbus_bool_t from_cache;
686 message = dbus_message_get_cached ();
695 message = dbus_new (DBusMessage, 1);
698 #ifndef DBUS_DISABLE_CHECKS
699 message->generation = _dbus_current_generation;
703 message->refcount.value = 1;
704 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
705 message->locked = FALSE;
706 message->size_counters = NULL;
707 message->size_counter_delta = 0;
708 message->changed_stamp = 0;
711 _dbus_data_slot_list_init (&message->slot_list);
715 _dbus_header_reinit (&message->header, message->byte_order);
716 _dbus_string_set_length (&message->body, 0);
720 if (!_dbus_header_init (&message->header, message->byte_order))
726 if (!_dbus_string_init_preallocated (&message->body, 32))
728 _dbus_header_free (&message->header);
738 * Constructs a new message of the given message type.
739 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
740 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
742 * @param message_type type of message
743 * @returns new message or #NULL If no memory
746 dbus_message_new (int message_type)
748 DBusMessage *message;
750 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
752 message = dbus_message_new_empty_header ();
756 if (!_dbus_header_create (&message->header,
758 NULL, NULL, NULL, NULL, NULL))
760 dbus_message_unref (message);
768 * Constructs a new message to invoke a method on a remote
769 * object. Returns #NULL if memory can't be allocated for the
770 * message. The destination may be #NULL in which case no destination
771 * is set; this is appropriate when using D-BUS in a peer-to-peer
772 * context (no message bus). The interface may be #NULL, which means
773 * that if multiple methods with the given name exist it is undefined
774 * which one will be invoked.
776 * @param destination service that the message should be sent to or #NULL
777 * @param path object path the message should be sent to
778 * @param interface interface to invoke method on
779 * @param method method to invoke
781 * @returns a new DBusMessage, free with dbus_message_unref()
782 * @see dbus_message_unref()
785 dbus_message_new_method_call (const char *destination,
787 const char *interface,
790 DBusMessage *message;
792 _dbus_return_val_if_fail (path != NULL, NULL);
793 _dbus_return_val_if_fail (method != NULL, NULL);
794 _dbus_return_val_if_fail (destination == NULL ||
795 _dbus_check_is_valid_service (destination), NULL);
796 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
797 _dbus_return_val_if_fail (interface == NULL ||
798 _dbus_check_is_valid_interface (interface), NULL);
799 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
801 message = dbus_message_new_empty_header ();
805 if (!_dbus_header_create (&message->header,
806 DBUS_MESSAGE_TYPE_METHOD_CALL,
807 destination, path, interface, method, NULL))
809 dbus_message_unref (message);
817 * Constructs a message that is a reply to a method call. Returns
818 * #NULL if memory can't be allocated for the message.
820 * @param method_call the message which the created
821 * message is a reply to.
822 * @returns a new DBusMessage, free with dbus_message_unref()
823 * @see dbus_message_new_method_call(), dbus_message_unref()
826 dbus_message_new_method_return (DBusMessage *method_call)
828 DBusMessage *message;
831 _dbus_return_val_if_fail (method_call != NULL, NULL);
833 sender = dbus_message_get_sender (method_call);
835 /* sender is allowed to be null here in peer-to-peer case */
837 message = dbus_message_new_empty_header ();
841 if (!_dbus_header_create (&message->header,
842 DBUS_MESSAGE_TYPE_METHOD_RETURN,
843 sender, NULL, NULL, NULL, NULL))
845 dbus_message_unref (message);
849 dbus_message_set_no_reply (message, TRUE);
851 if (!dbus_message_set_reply_serial (message,
852 dbus_message_get_serial (method_call)))
854 dbus_message_unref (message);
862 * Constructs a new message representing a signal emission. Returns
863 * #NULL if memory can't be allocated for the message. A signal is
864 * identified by its originating interface, and the name of the
867 * @param path the path to the object emitting the signal
868 * @param interface the interface the signal is emitted from
869 * @param name name of the signal
870 * @returns a new DBusMessage, free with dbus_message_unref()
871 * @see dbus_message_unref()
874 dbus_message_new_signal (const char *path,
875 const char *interface,
878 DBusMessage *message;
880 _dbus_return_val_if_fail (path != NULL, NULL);
881 _dbus_return_val_if_fail (interface != NULL, NULL);
882 _dbus_return_val_if_fail (name != NULL, NULL);
883 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
884 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
885 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
887 message = dbus_message_new_empty_header ();
891 if (!_dbus_header_create (&message->header,
892 DBUS_MESSAGE_TYPE_SIGNAL,
893 NULL, path, interface, name, NULL))
895 dbus_message_unref (message);
899 dbus_message_set_no_reply (message, TRUE);
905 * Creates a new message that is an error reply to a certain message.
906 * Error replies are possible in response to method calls primarily.
908 * @param reply_to the original message
909 * @param error_name the error name
910 * @param error_message the error message string or #NULL for none
911 * @returns a new error message
914 dbus_message_new_error (DBusMessage *reply_to,
915 const char *error_name,
916 const char *error_message)
918 DBusMessage *message;
920 DBusMessageIter iter;
922 _dbus_return_val_if_fail (reply_to != NULL, NULL);
923 _dbus_return_val_if_fail (error_name != NULL, NULL);
924 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
926 sender = dbus_message_get_sender (reply_to);
928 /* sender may be NULL for non-message-bus case or
929 * when the message bus is dealing with an unregistered
932 message = dbus_message_new_empty_header ();
936 if (!_dbus_header_create (&message->header,
937 DBUS_MESSAGE_TYPE_ERROR,
938 sender, NULL, NULL, NULL, error_name))
940 dbus_message_unref (message);
944 dbus_message_set_no_reply (message, TRUE);
946 if (!dbus_message_set_reply_serial (message,
947 dbus_message_get_serial (reply_to)))
949 dbus_message_unref (message);
953 if (error_message != NULL)
955 dbus_message_iter_init_append (message, &iter);
956 if (!dbus_message_iter_append_basic (&iter,
960 dbus_message_unref (message);
969 * Creates a new message that is an error reply to a certain message.
970 * Error replies are possible in response to method calls primarily.
972 * @param reply_to the original message
973 * @param error_name the error name
974 * @param error_format the error message format as with printf
975 * @param ... format string arguments
976 * @returns a new error message
979 dbus_message_new_error_printf (DBusMessage *reply_to,
980 const char *error_name,
981 const char *error_format,
986 DBusMessage *message;
988 _dbus_return_val_if_fail (reply_to != NULL, NULL);
989 _dbus_return_val_if_fail (error_name != NULL, NULL);
990 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
992 if (!_dbus_string_init (&str))
995 va_start (args, error_format);
997 if (_dbus_string_append_printf_valist (&str, error_format, args))
998 message = dbus_message_new_error (reply_to, error_name,
999 _dbus_string_get_const_data (&str));
1003 _dbus_string_free (&str);
1012 * Creates a new message that is an exact replica of the message
1013 * specified, except that its refcount is set to 1, its message serial
1014 * is reset to 0, and if the original message was "locked" (in the
1015 * outgoing message queue and thus not modifiable) the new message
1016 * will not be locked.
1018 * @param message the message.
1019 * @returns the new message.
1022 dbus_message_copy (const DBusMessage *message)
1024 DBusMessage *retval;
1026 _dbus_return_val_if_fail (message != NULL, NULL);
1028 retval = dbus_new0 (DBusMessage, 1);
1032 retval->refcount.value = 1;
1033 retval->byte_order = message->byte_order;
1034 retval->locked = FALSE;
1035 #ifndef DBUS_DISABLE_CHECKS
1036 retval->generation = message->generation;
1039 if (!_dbus_header_copy (&message->header, &retval->header))
1045 if (!_dbus_string_init_preallocated (&retval->body,
1046 _dbus_string_get_length (&message->body)))
1048 _dbus_header_free (&retval->header);
1053 if (!_dbus_string_copy (&message->body, 0,
1060 _dbus_header_free (&retval->header);
1061 _dbus_string_free (&retval->body);
1069 * Increments the reference count of a DBusMessage.
1071 * @param message The message
1072 * @returns the message
1073 * @see dbus_message_unref
1076 dbus_message_ref (DBusMessage *message)
1078 dbus_int32_t old_refcount;
1080 _dbus_return_val_if_fail (message != NULL, NULL);
1081 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1083 old_refcount = _dbus_atomic_inc (&message->refcount);
1084 _dbus_assert (old_refcount >= 1);
1090 * Decrements the reference count of a DBusMessage.
1092 * @param message The message
1093 * @see dbus_message_ref
1096 dbus_message_unref (DBusMessage *message)
1098 dbus_int32_t old_refcount;
1100 _dbus_return_if_fail (message != NULL);
1101 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1103 old_refcount = _dbus_atomic_dec (&message->refcount);
1105 _dbus_assert (old_refcount >= 0);
1107 if (old_refcount == 1)
1109 /* Calls application callbacks! */
1110 dbus_message_cache_or_finalize (message);
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,
1123 * @param message the message
1124 * @returns the type of the message
1127 dbus_message_get_type (DBusMessage *message)
1129 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1131 return _dbus_header_get_message_type (&message->header);
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.
1142 * To append a basic type, specify its type code followed by the
1143 * value. For example:
1146 * DBUS_TYPE_INT32, 42,
1147 * DBUS_TYPE_STRING, "Hello World"
1151 * dbus_int32_t val = 42;
1152 * DBUS_TYPE_INT32, val
1155 * Be sure that your provided value is the right size. For example, this
1158 * DBUS_TYPE_INT64, 42
1160 * Because the "42" will be a 32-bit integer. You need to cast to
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:
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
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.
1180 * The last argument to this function must be #DBUS_TYPE_INVALID,
1181 * marking the end of the argument list.
1183 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1185 * @todo If this fails due to lack of memory, the message is hosed and
1186 * you have to start over building the whole message.
1188 * @param message the message
1189 * @param first_arg_type type of the first argument
1190 * @param ... value of first argument, list of additional type-value pairs
1191 * @returns #TRUE on success
1194 dbus_message_append_args (DBusMessage *message,
1201 _dbus_return_val_if_fail (message != NULL, FALSE);
1203 va_start (var_args, first_arg_type);
1204 retval = dbus_message_append_args_valist (message,
1213 * This function takes a va_list for use by language bindings.
1214 * It's otherwise the same as dbus_message_append_args().
1216 * @todo for now, if this function fails due to OOM it will leave
1217 * the message half-written and you have to discard the message
1220 * @see dbus_message_append_args.
1221 * @param message the message
1222 * @param first_arg_type type of first argument
1223 * @param var_args value of first argument, then list of type/value pairs
1224 * @returns #TRUE on success
1227 dbus_message_append_args_valist (DBusMessage *message,
1232 DBusMessageIter iter;
1234 _dbus_return_val_if_fail (message != NULL, FALSE);
1236 type = first_arg_type;
1238 dbus_message_iter_init_append (message, &iter);
1240 while (type != DBUS_TYPE_INVALID)
1242 if (_dbus_type_is_basic (type))
1244 const DBusBasicValue *value;
1245 value = va_arg (var_args, const DBusBasicValue*);
1247 if (!dbus_message_iter_append_basic (&iter,
1252 else if (type == DBUS_TYPE_ARRAY)
1255 const DBusBasicValue **value;
1257 DBusMessageIter array;
1260 element_type = va_arg (var_args, int);
1262 #ifndef DBUS_DISABLE_CHECKS
1263 if (!_dbus_type_is_fixed (element_type))
1265 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1266 _dbus_type_to_string (element_type),
1267 _DBUS_FUNCTION_NAME);
1272 value = va_arg (var_args, const DBusBasicValue**);
1273 n_elements = va_arg (var_args, int);
1275 buf[0] = element_type;
1277 if (!dbus_message_iter_open_container (&iter,
1283 if (!dbus_message_iter_append_fixed_array (&array,
1289 if (!dbus_message_iter_close_container (&iter, &array))
1292 #ifndef DBUS_DISABLE_CHECKS
1295 _dbus_warn ("type %s isn't supported yet in %s\n",
1296 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1301 type = va_arg (var_args, int);
1311 * Gets arguments from a message given a variable argument list. The
1312 * supported types include those supported by
1313 * dbus_message_append_args(); that is, basic types and arrays of
1314 * fixed-length basic types. The arguments are the same as they would
1315 * be for dbus_message_iter_get_basic() or
1316 * dbus_message_iter_get_fixed_array().
1318 * In addition to those types, arrays of string, object path, and
1319 * signature are supported; but these are returned as allocated memory
1320 * and must be freed with dbus_free_string_array(), while the other
1321 * types are returned as const references.
1323 * The variable argument list should contain the type of the argument
1324 * followed by a pointer to where the value should be stored. The list
1325 * is terminated with #DBUS_TYPE_INVALID.
1327 * The returned values are constant; do not free them. They point
1328 * into the #DBusMessage.
1330 * If the requested arguments are not present, or do not have the
1331 * requested types, then an error will be set.
1333 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1335 * @param message the message
1336 * @param error error to be filled in on failure
1337 * @param first_arg_type the first argument type
1338 * @param ... location for first argument value, then list of type-location pairs
1339 * @returns #FALSE if the error was set
1342 dbus_message_get_args (DBusMessage *message,
1350 _dbus_return_val_if_fail (message != NULL, FALSE);
1351 _dbus_return_val_if_error_is_set (error, FALSE);
1353 va_start (var_args, first_arg_type);
1354 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1361 * This function takes a va_list for use by language bindings. It is
1362 * otherwise the same as dbus_message_get_args().
1364 * @see dbus_message_get_args
1365 * @param message the message
1366 * @param error error to be filled in
1367 * @param first_arg_type type of the first argument
1368 * @param var_args return location for first argument, followed by list of type/location pairs
1369 * @returns #FALSE if error was set
1372 dbus_message_get_args_valist (DBusMessage *message,
1377 DBusMessageIter iter;
1379 _dbus_return_val_if_fail (message != NULL, FALSE);
1380 _dbus_return_val_if_error_is_set (error, FALSE);
1382 dbus_message_iter_init (message, &iter);
1383 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1387 * Reads arguments from a message iterator given a variable argument
1388 * list. Only arguments of basic type and arrays of fixed-length
1389 * basic type may be read with this function. See
1390 * dbus_message_get_args() for more details.
1392 * @todo this is static for now because there's no corresponding
1393 * iter_append_args() and I'm not sure we need this function to be
1394 * public since dbus_message_get_args() is what you usually want
1396 * @param iter the message iterator
1397 * @param error error to be filled in on failure
1398 * @param first_arg_type the first argument type
1399 * @param ... location for first argument value, then list of type-location pairs
1400 * @returns #FALSE if the error was set
1403 dbus_message_iter_get_args (DBusMessageIter *iter,
1411 _dbus_return_val_if_fail (iter != NULL, FALSE);
1412 _dbus_return_val_if_error_is_set (error, FALSE);
1414 va_start (var_args, first_arg_type);
1415 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
1422 _dbus_message_iter_init_common (DBusMessage *message,
1423 DBusMessageRealIter *real,
1426 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1428 real->message = message;
1429 real->changed_stamp = message->changed_stamp;
1430 real->iter_type = iter_type;
1431 real->sig_refcount = 0;
1435 * Initializes a #DBusMessageIter for reading the arguments of the
1436 * message passed in.
1438 * @param message the message
1439 * @param iter pointer to an iterator to initialize
1440 * @returns #FALSE if the message has no arguments
1443 dbus_message_iter_init (DBusMessage *message,
1444 DBusMessageIter *iter)
1446 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1447 const DBusString *type_str;
1450 _dbus_return_val_if_fail (message != NULL, FALSE);
1451 _dbus_return_val_if_fail (iter != NULL, FALSE);
1453 get_const_signature (&message->header, &type_str, &type_pos);
1455 _dbus_message_iter_init_common (message, real,
1456 DBUS_MESSAGE_ITER_TYPE_READER);
1458 _dbus_type_reader_init (&real->u.reader,
1459 message->byte_order,
1464 return _dbus_type_reader_has_next (&real->u.reader);
1467 #ifndef DBUS_DISABLE_CHECKS
1469 _dbus_message_iter_check (DBusMessageRealIter *iter)
1473 _dbus_warn ("dbus message iterator is NULL\n");
1477 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
1479 if (iter->u.reader.byte_order != iter->message->byte_order)
1481 _dbus_warn ("dbus message changed byte order since iterator was created\n");
1485 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
1487 if (iter->u.writer.byte_order != iter->message->byte_order)
1489 _dbus_warn ("dbus message changed byte order since append iterator was created\n");
1495 _dbus_warn ("dbus message iterator looks uninitialized or corrupted\n");
1499 if (iter->changed_stamp != iter->message->changed_stamp)
1501 _dbus_warn ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
1507 #endif /* DBUS_DISABLE_CHECKS */
1510 * Checks if an iterator has any more fields.
1512 * @param iter the message iter
1513 * @returns #TRUE if there are more fields
1517 dbus_message_iter_has_next (DBusMessageIter *iter)
1519 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1521 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1522 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1524 return _dbus_type_reader_has_next (&real->u.reader);
1528 * Moves the iterator to the next field, if any. If there's no next
1529 * field, returns #FALSE. If the iterator moves forward, returns
1532 * @param iter the message iter
1533 * @returns #TRUE if the iterator was moved to the next field
1536 dbus_message_iter_next (DBusMessageIter *iter)
1538 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1540 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1541 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1543 return _dbus_type_reader_next (&real->u.reader);
1547 * Returns the argument type of the argument that the message iterator
1548 * points to. If the iterator is at the end of the message, returns
1549 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1552 * dbus_message_iter_init (&iter);
1553 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1554 * dbus_message_iter_next (&iter);
1557 * @param iter the message iter
1558 * @returns the argument type
1561 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1563 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1565 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1566 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1568 return _dbus_type_reader_get_current_type (&real->u.reader);
1572 * Returns the element type of the array that the message iterator
1573 * points to. Note that you need to check that the iterator points to
1574 * an array prior to using this function.
1576 * @param iter the message iter
1577 * @returns the array element type
1580 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1582 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1584 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1585 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1586 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1588 return _dbus_type_reader_get_element_type (&real->u.reader);
1592 * Recurses into a container value when reading values from a message,
1593 * initializing a sub-iterator to use for traversing the child values
1596 * Note that this recurses into a value, not a type, so you can only
1597 * recurse if the value exists. The main implication of this is that
1598 * if you have for example an empty array of array of int32, you can
1599 * recurse into the outermost array, but it will have no values, so
1600 * you won't be able to recurse further. There's no array of int32 to
1603 * @param iter the message iterator
1604 * @param sub the sub-iterator to initialize
1607 dbus_message_iter_recurse (DBusMessageIter *iter,
1608 DBusMessageIter *sub)
1610 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1611 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1613 _dbus_return_if_fail (_dbus_message_iter_check (real));
1614 _dbus_return_if_fail (sub != NULL);
1617 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1621 * Reads a basic-typed value from the message iterator.
1622 * Basic types are the non-containers such as integer and string.
1624 * The value argument should be the address of a location to store
1625 * the returned value. So for int32 it should be a "dbus_int32_t*"
1626 * and for string a "const char**". The returned value is
1627 * by reference and should not be freed.
1629 * All returned values are guaranteed to fit in 8 bytes. So you can
1630 * write code like this:
1633 * #ifdef DBUS_HAVE_INT64
1634 * dbus_uint64_t value;
1636 * dbus_message_iter_get_basic (&read_iter, &value);
1637 * type = dbus_message_iter_get_arg_type (&read_iter);
1638 * dbus_message_iter_append_basic (&write_iter, type, &value);
1642 * To avoid the #DBUS_HAVE_INT64 conditional, create a struct or
1643 * something that occupies at least 8 bytes, e.g. you could use a
1644 * struct with two int32 values in it. dbus_uint64_t is just one
1645 * example of a type that's large enough to hold any possible value.
1647 * Be sure you have somehow checked that
1648 * dbus_message_iter_get_arg_type() matches the type you are
1649 * expecting, or you'll crash when you try to use an integer as a
1650 * string or something.
1652 * @param iter the iterator
1653 * @param value location to store the value
1656 dbus_message_iter_get_basic (DBusMessageIter *iter,
1659 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1661 _dbus_return_if_fail (_dbus_message_iter_check (real));
1662 _dbus_return_if_fail (value != NULL);
1664 _dbus_type_reader_read_basic (&real->u.reader,
1669 * Reads a block of fixed-length values from the message iterator.
1670 * Fixed-length values are those basic types that are not string-like,
1671 * such as integers, bool, double. The block read will be from the
1672 * current position in the array until the end of the array.
1674 * The value argument should be the address of a location to store the
1675 * returned array. So for int32 it should be a "const dbus_int32_t**"
1676 * The returned value is by reference and should not be freed.
1678 * @param iter the iterator
1679 * @param value location to store the block
1680 * @param n_elements number of elements in the block
1683 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
1687 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1689 _dbus_return_if_fail (_dbus_message_iter_check (real));
1690 _dbus_return_if_fail (value != NULL);
1691 _dbus_return_if_fail (_dbus_type_is_fixed (_dbus_type_reader_get_element_type (&real->u.reader)));
1693 _dbus_type_reader_read_fixed_multi (&real->u.reader,
1698 * This function takes a va_list for use by language bindings and is
1699 * otherwise the same as dbus_message_iter_get_args().
1700 * dbus_message_get_args() is the place to go for complete
1703 * @todo this is static for now, should be public if
1704 * dbus_message_iter_get_args_valist() is made public.
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
1714 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1719 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1720 int spec_type, msg_type, i;
1723 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1724 _dbus_return_val_if_error_is_set (error, FALSE);
1728 spec_type = first_arg_type;
1731 while (spec_type != DBUS_TYPE_INVALID)
1733 msg_type = dbus_message_iter_get_arg_type (iter);
1735 if (msg_type != spec_type)
1737 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1738 "Argument %d is specified to be of type \"%s\", but "
1739 "is actually of type \"%s\"\n", i,
1740 _dbus_type_to_string (spec_type),
1741 _dbus_type_to_string (msg_type));
1746 if (_dbus_type_is_basic (spec_type))
1748 DBusBasicValue *ptr;
1750 ptr = va_arg (var_args, DBusBasicValue*);
1752 _dbus_return_val_if_fail (ptr != NULL, FALSE);
1754 _dbus_type_reader_read_basic (&real->u.reader,
1757 else if (spec_type == DBUS_TYPE_ARRAY)
1760 int spec_element_type;
1761 const DBusBasicValue **ptr;
1763 DBusTypeReader array;
1765 spec_element_type = va_arg (var_args, int);
1766 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1768 if (spec_element_type != element_type)
1770 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1771 "Argument %d is specified to be an array of \"%s\", but "
1772 "is actually an array of \"%s\"\n",
1774 _dbus_type_to_string (spec_element_type),
1775 _dbus_type_to_string (element_type));
1780 if (_dbus_type_is_fixed (spec_element_type))
1782 ptr = va_arg (var_args, const DBusBasicValue**);
1783 n_elements_p = va_arg (var_args, int*);
1785 _dbus_return_val_if_fail (ptr != NULL, FALSE);
1786 _dbus_return_val_if_fail (n_elements_p != NULL, FALSE);
1788 _dbus_type_reader_recurse (&real->u.reader, &array);
1790 _dbus_type_reader_read_fixed_multi (&array,
1793 else if (spec_element_type == DBUS_TYPE_STRING ||
1794 spec_element_type == DBUS_TYPE_SIGNATURE ||
1795 spec_element_type == DBUS_TYPE_OBJECT_PATH)
1797 char ***str_array_p;
1801 str_array_p = va_arg (var_args, char***);
1802 n_elements_p = va_arg (var_args, int*);
1804 _dbus_return_val_if_fail (str_array_p != NULL, FALSE);
1805 _dbus_return_val_if_fail (n_elements_p != NULL, FALSE);
1807 /* Count elements in the array */
1808 _dbus_type_reader_recurse (&real->u.reader, &array);
1811 if (_dbus_type_reader_has_next (&array))
1813 while (_dbus_type_reader_next (&array))
1817 str_array = dbus_new0 (char*, i + 1);
1818 if (str_array == NULL)
1820 _DBUS_SET_OOM (error);
1824 /* Now go through and dup each string */
1825 _dbus_type_reader_recurse (&real->u.reader, &array);
1828 if (_dbus_type_reader_has_next (&array))
1833 _dbus_type_reader_read_basic (&array,
1836 str_array[i] = _dbus_strdup (s);
1837 if (str_array[i] == NULL)
1839 dbus_free_string_array (str_array);
1840 _DBUS_SET_OOM (error);
1846 while (_dbus_type_reader_next (&array));
1849 *str_array_p = str_array;
1852 #ifndef DBUS_DISABLE_CHECKS
1855 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
1856 _DBUS_FUNCTION_NAME);
1861 #ifndef DBUS_DISABLE_CHECKS
1864 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
1865 _DBUS_FUNCTION_NAME);
1870 spec_type = va_arg (var_args, int);
1871 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1873 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1874 "Message has only %d arguments, but more were expected", i);
1889 * Initializes a #DBusMessageIter for appending arguments to the end
1892 * @todo If appending any of the arguments fails due to lack of
1893 * memory, generally the message is hosed and you have to start over
1894 * building the whole message.
1896 * @param message the message
1897 * @param iter pointer to an iterator to initialize
1900 dbus_message_iter_init_append (DBusMessage *message,
1901 DBusMessageIter *iter)
1903 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1905 _dbus_return_if_fail (message != NULL);
1906 _dbus_return_if_fail (iter != NULL);
1908 _dbus_message_iter_init_common (message, real,
1909 DBUS_MESSAGE_ITER_TYPE_WRITER);
1911 /* We create the signature string and point iterators at it "on demand"
1912 * when a value is actually appended. That means that init() never fails
1915 _dbus_type_writer_init_types_delayed (&real->u.writer,
1916 message->byte_order,
1918 _dbus_string_get_length (&message->body));
1922 * Creates a temporary signature string containing the current
1923 * signature, stores it in the iterator, and points the iterator to
1924 * the end of it. Used any time we write to the message.
1926 * @param real an iterator without a type_str
1927 * @returns #FALSE if no memory
1930 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
1933 const DBusString *current_sig;
1934 int current_sig_pos;
1936 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1938 if (real->u.writer.type_str != NULL)
1940 _dbus_assert (real->sig_refcount > 0);
1941 real->sig_refcount += 1;
1945 str = dbus_new (DBusString, 1);
1949 if (!_dbus_header_get_field_raw (&real->message->header,
1950 DBUS_HEADER_FIELD_SIGNATURE,
1951 ¤t_sig, ¤t_sig_pos))
1958 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
1959 current_sig_pos += 1; /* move on to sig data */
1961 if (!_dbus_string_init_preallocated (str, current_len + 4))
1967 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
1970 _dbus_string_free (str);
1977 if (!_dbus_string_init_preallocated (str, 4))
1984 real->sig_refcount = 1;
1986 _dbus_type_writer_add_types (&real->u.writer,
1987 str, _dbus_string_get_length (str));
1992 * Sets the new signature as the message signature, frees the
1993 * signature string, and marks the iterator as not having a type_str
1994 * anymore. Frees the signature even if it fails, so you can't
1995 * really recover from failure. Kinda busted.
1997 * @param real an iterator without a type_str
1998 * @returns #FALSE if no memory
2001 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2004 const char *v_STRING;
2007 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2008 _dbus_assert (real->u.writer.type_str != NULL);
2009 _dbus_assert (real->sig_refcount > 0);
2011 real->sig_refcount -= 1;
2013 if (real->sig_refcount > 0)
2015 _dbus_assert (real->sig_refcount == 0);
2019 str = real->u.writer.type_str;
2021 v_STRING = _dbus_string_get_const_data (str);
2022 if (!_dbus_header_set_field_basic (&real->message->header,
2023 DBUS_HEADER_FIELD_SIGNATURE,
2024 DBUS_TYPE_SIGNATURE,
2028 _dbus_type_writer_remove_types (&real->u.writer);
2029 _dbus_string_free (str);
2035 #ifndef DBUS_DISABLE_CHECKS
2037 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2039 if (!_dbus_message_iter_check (iter))
2042 if (iter->message->locked)
2044 _dbus_warn ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2050 #endif /* DBUS_DISABLE_CHECKS */
2053 * Appends a basic-typed value to the message. The basic types are the
2054 * non-container types such as integer and string.
2056 * The "value" argument should be the address of a basic-typed value.
2057 * So for string, const char**. For integer, dbus_int32_t*.
2059 * @todo If this fails due to lack of memory, the message is hosed and
2060 * you have to start over building the whole message.
2062 * @param iter the append iterator
2063 * @param type the type of the value
2064 * @param value the address of the value
2065 * @returns #FALSE if not enough memory
2068 dbus_message_iter_append_basic (DBusMessageIter *iter,
2072 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2075 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2076 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2077 _dbus_return_val_if_fail (_dbus_type_is_basic (type), FALSE);
2078 _dbus_return_val_if_fail (value != NULL, FALSE);
2080 if (!_dbus_message_iter_open_signature (real))
2083 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2085 if (!_dbus_message_iter_close_signature (real))
2092 * Appends a block of fixed-length values to an array. The
2093 * fixed-length types are all basic types that are not string-like. So
2094 * int32, double, bool, etc. You must call
2095 * dbus_message_iter_open_container() to open an array of values
2096 * before calling this function. You may call this function multiple
2097 * times (and intermixed with calls to
2098 * dbus_message_iter_append_basic()) for the same array.
2100 * The "value" argument should be the address of the array. So for
2101 * integer, "dbus_int32_t**" is expected for example.
2103 * @warning in C, given "int array[]", "&array == array" (the
2104 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2105 * So if you're using an array instead of a pointer you have to create
2106 * a pointer variable, assign the array to it, then take the address
2107 * of the pointer variable.
2109 * const dbus_int32_t array[] = { 1, 2, 3 };
2110 * const dbus_int32_t *v_ARRAY = array;
2111 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2112 * fprintf (stderr, "No memory!\n");
2114 * For strings it works to write const char *array = "Hello" and then
2115 * use &array though.
2117 * @todo If this fails due to lack of memory, the message is hosed and
2118 * you have to start over building the whole message.
2120 * @param iter the append iterator
2121 * @param element_type the type of the array elements
2122 * @param value the address of the array
2123 * @param n_elements the number of elements to append
2124 * @returns #FALSE if not enough memory
2127 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2132 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2135 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2136 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2137 _dbus_return_val_if_fail (_dbus_type_is_fixed (element_type), FALSE);
2138 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2139 _dbus_return_val_if_fail (value != NULL, FALSE);
2140 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2141 _dbus_return_val_if_fail (n_elements <=
2142 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2145 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2151 * Appends a container-typed value to the message; you are required to
2152 * append the contents of the container using the returned
2153 * sub-iterator, and then call
2154 * dbus_message_iter_close_container(). Container types are for
2155 * example struct, variant, and array. For variants, the
2156 * contained_signature should be the type of the single value inside
2157 * the variant. For structs, contained_signature should be #NULL; it
2158 * will be set to whatever types you write into the struct. For
2159 * arrays, contained_signature should be the type of the array
2162 * @todo If this fails due to lack of memory, the message is hosed and
2163 * you have to start over building the whole message.
2165 * @param iter the append iterator
2166 * @param type the type of the value
2167 * @param contained_signature the type of container contents
2168 * @param sub sub-iterator to initialize
2169 * @returns #FALSE if not enough memory
2172 dbus_message_iter_open_container (DBusMessageIter *iter,
2174 const char *contained_signature,
2175 DBusMessageIter *sub)
2177 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2178 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2179 DBusString contained_str;
2181 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2182 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2183 _dbus_return_val_if_fail (_dbus_type_is_container (type), FALSE);
2184 _dbus_return_val_if_fail (sub != NULL, FALSE);
2185 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2186 contained_signature == NULL) ||
2187 contained_signature != NULL, FALSE);
2189 if (!_dbus_message_iter_open_signature (real))
2192 _dbus_string_init_const (&contained_str, contained_signature);
2195 return _dbus_type_writer_recurse (&real->u.writer,
2198 &real_sub->u.writer);
2203 * Closes a container-typed value appended to the message; may write
2204 * out more information to the message known only after the entire
2205 * container is written, and may free resources created by
2206 * dbus_message_iter_open_container().
2208 * @todo If this fails due to lack of memory, the message is hosed and
2209 * you have to start over building the whole message.
2211 * @param iter the append iterator
2212 * @param sub sub-iterator to close
2213 * @returns #FALSE if not enough memory
2216 dbus_message_iter_close_container (DBusMessageIter *iter,
2217 DBusMessageIter *sub)
2219 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2220 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2223 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2224 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2225 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2226 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2228 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2229 &real_sub->u.writer);
2231 if (!_dbus_message_iter_close_signature (real))
2238 * Sets a flag indicating that the message does not want a reply; if
2239 * this flag is set, the other end of the connection may (but is not
2240 * required to) optimize by not sending method return or error
2241 * replies. If this flag is set, there is no way to know whether the
2242 * message successfully arrived at the remote end. Normally you know a
2243 * message was received when you receive the reply to it.
2245 * @param message the message
2246 * @param no_reply #TRUE if no reply is desired
2249 dbus_message_set_no_reply (DBusMessage *message,
2250 dbus_bool_t no_reply)
2252 _dbus_return_if_fail (message != NULL);
2253 _dbus_return_if_fail (!message->locked);
2255 _dbus_header_toggle_flag (&message->header,
2256 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2261 * Returns #TRUE if the message does not expect
2264 * @param message the message
2265 * @returns #TRUE if the message sender isn't waiting for a reply
2268 dbus_message_get_no_reply (DBusMessage *message)
2270 _dbus_return_val_if_fail (message != NULL, FALSE);
2272 return _dbus_header_get_flag (&message->header,
2273 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2277 * Sets a flag indicating that the addressed service will be
2278 * auto-activated before the message is delivered. When this flag is
2279 * set, the message is held until the service is succesfully activated
2280 * or fails to activate. In case of failure, the reply will be an
2281 * activation error. If this flag is not set (the default
2283 * @param message the message
2284 * @param auto_activation #TRUE if auto-activation is desired
2287 dbus_message_set_auto_activation (DBusMessage *message,
2288 dbus_bool_t auto_activation)
2290 _dbus_return_if_fail (message != NULL);
2291 _dbus_return_if_fail (!message->locked);
2293 _dbus_header_toggle_flag (&message->header,
2294 DBUS_HEADER_FLAG_AUTO_ACTIVATION,
2299 * Returns #TRUE if the message will cause the addressed service to be
2302 * @param message the message
2303 * @returns #TRUE if the message will use auto-activation
2306 dbus_message_get_auto_activation (DBusMessage *message)
2308 _dbus_return_val_if_fail (message != NULL, FALSE);
2310 return _dbus_header_get_flag (&message->header,
2311 DBUS_HEADER_FLAG_AUTO_ACTIVATION);
2316 * Sets the object path this message is being sent to (for
2317 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2318 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2320 * @param message the message
2321 * @param object_path the path or #NULL to unset
2322 * @returns #FALSE if not enough memory
2325 dbus_message_set_path (DBusMessage *message,
2326 const char *object_path)
2328 _dbus_return_val_if_fail (message != NULL, FALSE);
2329 _dbus_return_val_if_fail (!message->locked, FALSE);
2330 _dbus_return_val_if_fail (object_path == NULL ||
2331 _dbus_check_is_valid_path (object_path),
2334 return set_or_delete_string_field (message,
2335 DBUS_HEADER_FIELD_PATH,
2336 DBUS_TYPE_OBJECT_PATH,
2341 * Gets the object path this message is being sent to (for
2342 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2343 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2345 * @param message the message
2346 * @returns the path (should not be freed) or #NULL
2349 dbus_message_get_path (DBusMessage *message)
2353 _dbus_return_val_if_fail (message != NULL, NULL);
2355 v = NULL; /* in case field doesn't exist */
2356 _dbus_header_get_field_basic (&message->header,
2357 DBUS_HEADER_FIELD_PATH,
2358 DBUS_TYPE_OBJECT_PATH,
2364 * Gets the object path this message is being sent to
2365 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2366 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2367 * format (one array element per path component).
2368 * Free the returned array with dbus_free_string_array().
2370 * An empty but non-NULL path array means the path "/".
2371 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2372 * and the path "/" becomes { NULL }.
2374 * @todo this could be optimized by using the len from the message
2375 * instead of calling strlen() again
2377 * @param message the message
2378 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2379 * @returns #FALSE if no memory to allocate the array
2382 dbus_message_get_path_decomposed (DBusMessage *message,
2387 _dbus_return_val_if_fail (message != NULL, FALSE);
2388 _dbus_return_val_if_fail (path != NULL, FALSE);
2392 v = dbus_message_get_path (message);
2395 if (!_dbus_decompose_path (v, strlen (v),
2403 * Sets the interface this message is being sent to
2404 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2405 * the interface a signal is being emitted from
2406 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2408 * @param message the message
2409 * @param interface the interface or #NULL to unset
2410 * @returns #FALSE if not enough memory
2413 dbus_message_set_interface (DBusMessage *message,
2414 const char *interface)
2416 _dbus_return_val_if_fail (message != NULL, FALSE);
2417 _dbus_return_val_if_fail (!message->locked, FALSE);
2418 _dbus_return_val_if_fail (interface == NULL ||
2419 _dbus_check_is_valid_interface (interface),
2422 return set_or_delete_string_field (message,
2423 DBUS_HEADER_FIELD_INTERFACE,
2429 * Gets the interface this message is being sent to
2430 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2431 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2432 * The interface name is fully-qualified (namespaced).
2433 * Returns #NULL if none.
2435 * @param message the message
2436 * @returns the message interface (should not be freed) or #NULL
2439 dbus_message_get_interface (DBusMessage *message)
2443 _dbus_return_val_if_fail (message != NULL, NULL);
2445 v = NULL; /* in case field doesn't exist */
2446 _dbus_header_get_field_basic (&message->header,
2447 DBUS_HEADER_FIELD_INTERFACE,
2454 * Sets the interface member being invoked
2455 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2456 * (DBUS_MESSAGE_TYPE_SIGNAL).
2457 * The interface name is fully-qualified (namespaced).
2459 * @param message the message
2460 * @param member the member or #NULL to unset
2461 * @returns #FALSE if not enough memory
2464 dbus_message_set_member (DBusMessage *message,
2467 _dbus_return_val_if_fail (message != NULL, FALSE);
2468 _dbus_return_val_if_fail (!message->locked, FALSE);
2469 _dbus_return_val_if_fail (member == NULL ||
2470 _dbus_check_is_valid_member (member),
2473 return set_or_delete_string_field (message,
2474 DBUS_HEADER_FIELD_MEMBER,
2480 * Gets the interface member being invoked
2481 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2482 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2484 * @param message the message
2485 * @returns the member name (should not be freed) or #NULL
2488 dbus_message_get_member (DBusMessage *message)
2492 _dbus_return_val_if_fail (message != NULL, NULL);
2494 v = NULL; /* in case field doesn't exist */
2495 _dbus_header_get_field_basic (&message->header,
2496 DBUS_HEADER_FIELD_MEMBER,
2503 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2504 * The name is fully-qualified (namespaced).
2506 * @param message the message
2507 * @param error_name the name or #NULL to unset
2508 * @returns #FALSE if not enough memory
2511 dbus_message_set_error_name (DBusMessage *message,
2512 const char *error_name)
2514 _dbus_return_val_if_fail (message != NULL, FALSE);
2515 _dbus_return_val_if_fail (!message->locked, FALSE);
2516 _dbus_return_val_if_fail (error_name == NULL ||
2517 _dbus_check_is_valid_error_name (error_name),
2520 return set_or_delete_string_field (message,
2521 DBUS_HEADER_FIELD_ERROR_NAME,
2527 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2530 * @param message the message
2531 * @returns the error name (should not be freed) or #NULL
2534 dbus_message_get_error_name (DBusMessage *message)
2538 _dbus_return_val_if_fail (message != NULL, NULL);
2540 v = NULL; /* in case field doesn't exist */
2541 _dbus_header_get_field_basic (&message->header,
2542 DBUS_HEADER_FIELD_ERROR_NAME,
2549 * Sets the message's destination service.
2551 * @param message the message
2552 * @param destination the destination service name or #NULL to unset
2553 * @returns #FALSE if not enough memory
2556 dbus_message_set_destination (DBusMessage *message,
2557 const char *destination)
2559 _dbus_return_val_if_fail (message != NULL, FALSE);
2560 _dbus_return_val_if_fail (!message->locked, FALSE);
2561 _dbus_return_val_if_fail (destination == NULL ||
2562 _dbus_check_is_valid_service (destination),
2565 return set_or_delete_string_field (message,
2566 DBUS_HEADER_FIELD_DESTINATION,
2572 * Gets the destination service of a message or #NULL if there is
2575 * @param message the message
2576 * @returns the message destination service (should not be freed) or #NULL
2579 dbus_message_get_destination (DBusMessage *message)
2583 _dbus_return_val_if_fail (message != NULL, NULL);
2585 v = NULL; /* in case field doesn't exist */
2586 _dbus_header_get_field_basic (&message->header,
2587 DBUS_HEADER_FIELD_DESTINATION,
2594 * Sets the message sender.
2596 * @param message the message
2597 * @param sender the sender or #NULL to unset
2598 * @returns #FALSE if not enough memory
2601 dbus_message_set_sender (DBusMessage *message,
2604 _dbus_return_val_if_fail (message != NULL, FALSE);
2605 _dbus_return_val_if_fail (!message->locked, FALSE);
2606 _dbus_return_val_if_fail (sender == NULL ||
2607 _dbus_check_is_valid_service (sender),
2610 return set_or_delete_string_field (message,
2611 DBUS_HEADER_FIELD_SENDER,
2617 * Gets the service which originated this message,
2618 * or #NULL if unknown or inapplicable.
2620 * @param message the message
2621 * @returns the service name or #NULL
2624 dbus_message_get_sender (DBusMessage *message)
2628 _dbus_return_val_if_fail (message != NULL, NULL);
2630 v = NULL; /* in case field doesn't exist */
2631 _dbus_header_get_field_basic (&message->header,
2632 DBUS_HEADER_FIELD_SENDER,
2639 * Gets the type signature of the message, i.e. the arguments in the
2640 * message payload. The signature includes only "in" arguments for
2641 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2642 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2643 * what you might expect (it does not include the signature of the
2644 * entire C++-style method).
2646 * The signature is a string made up of type codes such as
2647 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2648 * the value of #DBUS_TYPE_INVALID).
2650 * @param message the message
2651 * @returns the type signature
2654 dbus_message_get_signature (DBusMessage *message)
2656 const DBusString *type_str;
2659 _dbus_return_val_if_fail (message != NULL, NULL);
2661 get_const_signature (&message->header, &type_str, &type_pos);
2663 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
2667 _dbus_message_has_type_interface_member (DBusMessage *message,
2669 const char *interface,
2674 _dbus_assert (message != NULL);
2675 _dbus_assert (interface != NULL);
2676 _dbus_assert (member != NULL);
2678 if (dbus_message_get_type (message) != type)
2681 /* Optimize by checking the short member name first
2682 * instead of the longer interface name
2685 n = dbus_message_get_member (message);
2687 if (n && strcmp (n, member) == 0)
2689 n = dbus_message_get_interface (message);
2691 if (n == NULL || strcmp (n, interface) == 0)
2699 * Checks whether the message is a method call with the given
2700 * interface and member fields. If the message is not
2701 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
2702 * member field, returns #FALSE. If the interface field is missing,
2703 * then it will be assumed equal to the provided interface. The D-BUS
2704 * protocol allows method callers to leave out the interface name.
2706 * @param message the message
2707 * @param interface the name to check (must not be #NULL)
2708 * @param method the name to check (must not be #NULL)
2710 * @returns #TRUE if the message is the specified method call
2713 dbus_message_is_method_call (DBusMessage *message,
2714 const char *interface,
2717 _dbus_return_val_if_fail (message != NULL, FALSE);
2718 _dbus_return_val_if_fail (interface != NULL, FALSE);
2719 _dbus_return_val_if_fail (method != NULL, FALSE);
2720 /* don't check that interface/method are valid since it would be
2721 * expensive, and not catch many common errors
2724 return _dbus_message_has_type_interface_member (message,
2725 DBUS_MESSAGE_TYPE_METHOD_CALL,
2730 * Checks whether the message is a signal with the given interface and
2731 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
2732 * has a different interface or member field, returns #FALSE. If the
2733 * interface field in the message is missing, it is assumed to match
2734 * any interface you pass in to this function.
2736 * @param message the message
2737 * @param interface the name to check (must not be #NULL)
2738 * @param signal_name the name to check (must not be #NULL)
2740 * @returns #TRUE if the message is the specified signal
2743 dbus_message_is_signal (DBusMessage *message,
2744 const char *interface,
2745 const char *signal_name)
2747 _dbus_return_val_if_fail (message != NULL, FALSE);
2748 _dbus_return_val_if_fail (interface != NULL, FALSE);
2749 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
2750 /* don't check that interface/name are valid since it would be
2751 * expensive, and not catch many common errors
2754 return _dbus_message_has_type_interface_member (message,
2755 DBUS_MESSAGE_TYPE_SIGNAL,
2756 interface, signal_name);
2760 * Checks whether the message is an error reply with the given error
2761 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
2762 * different name, returns #FALSE.
2764 * @param message the message
2765 * @param error_name the name to check (must not be #NULL)
2767 * @returns #TRUE if the message is the specified error
2770 dbus_message_is_error (DBusMessage *message,
2771 const char *error_name)
2775 _dbus_return_val_if_fail (message != NULL, FALSE);
2776 _dbus_return_val_if_fail (error_name != NULL, FALSE);
2777 /* don't check that error_name is valid since it would be expensive,
2778 * and not catch many common errors
2781 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2784 n = dbus_message_get_error_name (message);
2786 if (n && strcmp (n, error_name) == 0)
2793 * Checks whether the message was sent to the given service. If the
2794 * message has no service specified or has a different name, returns
2797 * @param message the message
2798 * @param service the service to check (must not be #NULL)
2800 * @returns #TRUE if the message has the given destination service
2803 dbus_message_has_destination (DBusMessage *message,
2804 const char *service)
2808 _dbus_return_val_if_fail (message != NULL, FALSE);
2809 _dbus_return_val_if_fail (service != NULL, FALSE);
2810 /* don't check that service name is valid since it would be expensive,
2811 * and not catch many common errors
2814 s = dbus_message_get_destination (message);
2816 if (s && strcmp (s, service) == 0)
2823 * Checks whether the message has the given service as its sender. If
2824 * the message has no sender specified or has a different sender,
2825 * returns #FALSE. Note that if a peer application owns multiple
2826 * services, its messages will have only one of those services as the
2827 * sender (usually the base service). So you can't use this
2828 * function to prove the sender didn't own service Foo, you can
2829 * only use it to prove that it did.
2831 * @todo this function is probably useless unless we make a hard guarantee
2832 * that the sender field in messages will always be the base service name
2834 * @param message the message
2835 * @param service the service to check (must not be #NULL)
2837 * @returns #TRUE if the message has the given origin service
2840 dbus_message_has_sender (DBusMessage *message,
2841 const char *service)
2845 _dbus_return_val_if_fail (message != NULL, FALSE);
2846 _dbus_return_val_if_fail (service != NULL, FALSE);
2847 /* don't check that service name is valid since it would be expensive,
2848 * and not catch many common errors
2851 s = dbus_message_get_sender (message);
2853 if (s && strcmp (s, service) == 0)
2860 * Checks whether the message has the given signature; see
2861 * dbus_message_get_signature() for more details on what the signature
2864 * @param message the message
2865 * @param signature typecode array
2866 * @returns #TRUE if message has the given signature
2869 dbus_message_has_signature (DBusMessage *message,
2870 const char *signature)
2874 _dbus_return_val_if_fail (message != NULL, FALSE);
2875 _dbus_return_val_if_fail (signature != NULL, FALSE);
2876 /* don't check that signature is valid since it would be expensive,
2877 * and not catch many common errors
2880 s = dbus_message_get_signature (message);
2882 if (s && strcmp (s, signature) == 0)
2889 * Sets a #DBusError based on the contents of the given
2890 * message. The error is only set if the message
2891 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
2892 * The name of the error is set to the name of the message,
2893 * and the error message is set to the first argument
2894 * if the argument exists and is a string.
2896 * The return value indicates whether the error was set (the error is
2897 * set if and only if the message is an error message). So you can
2898 * check for an error reply and convert it to DBusError in one go:
2900 * if (dbus_set_error_from_message (error, reply))
2906 * @param error the error to set
2907 * @param message the message to set it from
2908 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
2911 dbus_set_error_from_message (DBusError *error,
2912 DBusMessage *message)
2916 _dbus_return_val_if_fail (message != NULL, FALSE);
2917 _dbus_return_val_if_error_is_set (error, FALSE);
2919 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2923 dbus_message_get_args (message, NULL,
2924 DBUS_TYPE_STRING, &str,
2927 dbus_set_error (error, dbus_message_get_error_name (message),
2928 str ? "%s" : NULL, str);
2936 * @addtogroup DBusMessageInternals
2941 * @typedef DBusMessageLoader
2943 * The DBusMessageLoader object encapsulates the process of converting
2944 * a byte stream into a series of DBusMessage. It buffers the incoming
2945 * bytes as efficiently as possible, and generates a queue of
2946 * messages. DBusMessageLoader is typically used as part of a
2947 * DBusTransport implementation. The DBusTransport then hands off
2948 * the loaded messages to a DBusConnection, making the messages
2949 * visible to the application.
2951 * @todo write tests for break-loader that a) randomly delete header
2952 * fields and b) set string fields to zero-length and other funky
2958 * Implementation details of DBusMessageLoader.
2959 * All members are private.
2961 struct DBusMessageLoader
2963 int refcount; /**< Reference count. */
2965 DBusString data; /**< Buffered data */
2967 DBusList *messages; /**< Complete messages. */
2969 long max_message_size; /**< Maximum size of a message */
2971 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2973 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2977 * The initial buffer size of the message loader.
2979 * @todo this should be based on min header size plus some average
2980 * body size, or something. Or rather, the min header size only, if we
2981 * want to try to read only the header, store that in a DBusMessage,
2982 * then read only the body and store that, etc., depends on
2983 * how we optimize _dbus_message_loader_get_buffer() and what
2984 * the exact message format is.
2986 #define INITIAL_LOADER_DATA_LEN 32
2989 * Creates a new message loader. Returns #NULL if memory can't
2992 * @returns new loader, or #NULL.
2995 _dbus_message_loader_new (void)
2997 DBusMessageLoader *loader;
2999 loader = dbus_new0 (DBusMessageLoader, 1);
3003 loader->refcount = 1;
3005 /* Try to cap message size at something that won't *totally* hose
3006 * the system if we have a couple of them.
3008 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
3010 if (!_dbus_string_init (&loader->data))
3016 /* preallocate the buffer for speed, ignore failure */
3017 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3018 _dbus_string_set_length (&loader->data, 0);
3024 * Increments the reference count of the loader.
3026 * @param loader the loader.
3027 * @returns the loader
3030 _dbus_message_loader_ref (DBusMessageLoader *loader)
3032 loader->refcount += 1;
3038 * Decrements the reference count of the loader and finalizes the
3039 * loader when the count reaches zero.
3041 * @param loader the loader.
3044 _dbus_message_loader_unref (DBusMessageLoader *loader)
3046 loader->refcount -= 1;
3047 if (loader->refcount == 0)
3049 _dbus_list_foreach (&loader->messages,
3050 (DBusForeachFunction) dbus_message_unref,
3052 _dbus_list_clear (&loader->messages);
3053 _dbus_string_free (&loader->data);
3059 * Gets the buffer to use for reading data from the network. Network
3060 * data is read directly into an allocated buffer, which is then used
3061 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3062 * The buffer must always be returned immediately using
3063 * _dbus_message_loader_return_buffer(), even if no bytes are
3064 * successfully read.
3066 * @todo this function can be a lot more clever. For example
3067 * it can probably always return a buffer size to read exactly
3068 * the body of the next message, thus avoiding any memory wastage
3071 * @todo we need to enforce a max length on strings in header fields.
3073 * @param loader the message loader.
3074 * @param buffer the buffer
3077 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3078 DBusString **buffer)
3080 _dbus_assert (!loader->buffer_outstanding);
3082 *buffer = &loader->data;
3084 loader->buffer_outstanding = TRUE;
3088 * The smallest header size that can occur. (It won't be valid due to
3089 * missing required header fields.) This is 4 bytes, two uint32, an
3092 #define DBUS_MINIMUM_HEADER_SIZE 16
3095 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3096 * indicating to the loader how many bytes of the buffer were filled
3097 * in. This function must always be called, even if no bytes were
3098 * successfully read.
3100 * @param loader the loader.
3101 * @param buffer the buffer.
3102 * @param bytes_read number of bytes that were read into the buffer.
3105 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3109 _dbus_assert (loader->buffer_outstanding);
3110 _dbus_assert (buffer == &loader->data);
3112 loader->buffer_outstanding = FALSE;
3116 * FIXME when we move the header out of the buffer, that memmoves all
3117 * buffered messages. Kind of crappy.
3119 * Also we copy the header and body, which is kind of crappy. To
3120 * avoid this, we have to allow header and body to be in a single
3121 * memory block, which is good for messages we read and bad for
3122 * messages we are creating. But we could move_len() the buffer into
3123 * this single memory block, and move_len() will just swap the buffers
3124 * if you're moving the entire buffer replacing the dest string.
3126 * We could also have the message loader tell the transport how many
3127 * bytes to read; so it would first ask for some arbitrary number like
3128 * 256, then if the message was incomplete it would use the
3129 * header/body len to ask for exactly the size of the message (or
3130 * blocks the size of a typical kernel buffer for the socket). That
3131 * way we don't get trailing bytes in the buffer that have to be
3132 * memmoved. Though I suppose we also don't have a chance of reading a
3133 * bunch of small messages at once, so the optimization may be stupid.
3135 * Another approach would be to keep a "start" index into
3136 * loader->data and only delete it occasionally, instead of after
3137 * each message is loaded.
3139 * load_message() returns FALSE if not enough memory
3142 load_message (DBusMessageLoader *loader,
3143 DBusMessage *message,
3145 int fields_array_len,
3150 DBusValidity validity;
3151 const DBusString *type_str;
3157 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3160 /* 1. VALIDATE AND COPY OVER HEADER */
3161 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3162 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3164 if (!_dbus_header_load_untrusted (&message->header,
3171 _dbus_string_get_length (&loader->data)))
3173 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3174 if (validity == DBUS_VALID)
3179 _dbus_assert (validity == DBUS_VALID);
3181 message->byte_order = byte_order;
3183 /* 2. VALIDATE BODY */
3185 get_const_signature (&message->header, &type_str, &type_pos);
3187 /* Because the bytes_remaining arg is NULL, this validates that the
3188 * body is the right length
3190 validity = _dbus_validate_body_with_reason (type_str,
3197 if (validity != DBUS_VALID)
3199 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3203 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3205 if (!_dbus_list_append (&loader->messages, message))
3207 _dbus_verbose ("Failed to append new message to loader queue\n");
3212 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3213 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3214 (header_len + body_len));
3216 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3218 _dbus_verbose ("Failed to move body into new message\n");
3223 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3225 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3226 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3228 _dbus_verbose ("Loaded message %p\n", message);
3230 _dbus_assert (!oom);
3231 _dbus_assert (!loader->corrupted);
3239 /* does nothing if the message isn't in the list */
3240 _dbus_list_remove_last (&loader->messages, message);
3243 loader->corrupted = TRUE;
3245 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3251 * Converts buffered data into messages, if we have enough data. If
3252 * we don't have enough data, does nothing.
3254 * @todo we need to check that the proper named header fields exist
3255 * for each message type.
3257 * @todo If a message has unknown type, we should probably eat it
3258 * right here rather than passing it out to applications. However
3259 * it's not an error to see messages of unknown type.
3261 * @param loader the loader.
3262 * @returns #TRUE if we had enough memory to finish.
3265 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3267 while (!loader->corrupted &&
3268 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3270 DBusValidity validity;
3271 int byte_order, fields_array_len, header_len, body_len;
3273 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3280 _dbus_string_get_length (&loader->data)))
3282 DBusMessage *message;
3284 _dbus_assert (validity == DBUS_VALID);
3286 message = dbus_message_new_empty_header ();
3287 if (message == NULL)
3290 if (!load_message (loader, message,
3291 byte_order, fields_array_len,
3292 header_len, body_len))
3294 dbus_message_unref (message);
3300 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3302 if (validity != DBUS_VALID)
3303 loader->corrupted = TRUE;
3312 * Peeks at first loaded message, returns #NULL if no messages have
3315 * @param loader the loader.
3316 * @returns the next message, or #NULL if none.
3319 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3321 if (loader->messages)
3322 return loader->messages->data;
3328 * Pops a loaded message (passing ownership of the message
3329 * to the caller). Returns #NULL if no messages have been
3332 * @param loader the loader.
3333 * @returns the next message, or #NULL if none.
3336 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3338 return _dbus_list_pop_first (&loader->messages);
3342 * Pops a loaded message inside a list link (passing ownership of the
3343 * message and link to the caller). Returns #NULL if no messages have
3346 * @param loader the loader.
3347 * @returns the next message link, or #NULL if none.
3350 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3352 return _dbus_list_pop_first_link (&loader->messages);
3356 * Returns a popped message link, used to undo a pop.
3358 * @param loader the loader
3359 * @param link the link with a message in it
3362 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3365 _dbus_list_prepend_link (&loader->messages, link);
3369 * Checks whether the loader is confused due to bad data.
3370 * If messages are received that are invalid, the
3371 * loader gets confused and gives up permanently.
3372 * This state is called "corrupted."
3374 * @param loader the loader
3375 * @returns #TRUE if the loader is hosed.
3378 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3380 return loader->corrupted;
3384 * Sets the maximum size message we allow.
3386 * @param loader the loader
3387 * @param size the max message size in bytes
3390 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3393 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3395 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3396 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3397 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3399 loader->max_message_size = size;
3403 * Gets the maximum allowed message size in bytes.
3405 * @param loader the loader
3406 * @returns max size in bytes
3409 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3411 return loader->max_message_size;
3414 static DBusDataSlotAllocator slot_allocator;
3415 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3418 * Allocates an integer ID to be used for storing application-specific
3419 * data on any DBusMessage. The allocated ID may then be used
3420 * with dbus_message_set_data() and dbus_message_get_data().
3421 * The passed-in slot must be initialized to -1, and is filled in
3422 * with the slot ID. If the passed-in slot is not -1, it's assumed
3423 * to be already allocated, and its refcount is incremented.
3425 * The allocated slot is global, i.e. all DBusMessage objects will
3426 * have a slot with the given integer ID reserved.
3428 * @param slot_p address of a global variable storing the slot
3429 * @returns #FALSE on failure (no memory)
3432 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3434 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3435 _DBUS_LOCK_NAME (message_slots),
3440 * Deallocates a global ID for message data slots.
3441 * dbus_message_get_data() and dbus_message_set_data() may no
3442 * longer be used with this slot. Existing data stored on existing
3443 * DBusMessage objects will be freed when the message is
3444 * finalized, but may not be retrieved (and may only be replaced if
3445 * someone else reallocates the slot). When the refcount on the
3446 * passed-in slot reaches 0, it is set to -1.
3448 * @param slot_p address storing the slot to deallocate
3451 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3453 _dbus_return_if_fail (*slot_p >= 0);
3455 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3459 * Stores a pointer on a DBusMessage, along
3460 * with an optional function to be used for freeing
3461 * the data when the data is set again, or when
3462 * the message is finalized. The slot number
3463 * must have been allocated with dbus_message_allocate_data_slot().
3465 * @param message the message
3466 * @param slot the slot number
3467 * @param data the data to store
3468 * @param free_data_func finalizer function for the data
3469 * @returns #TRUE if there was enough memory to store the data
3472 dbus_message_set_data (DBusMessage *message,
3475 DBusFreeFunction free_data_func)
3477 DBusFreeFunction old_free_func;
3481 _dbus_return_val_if_fail (message != NULL, FALSE);
3482 _dbus_return_val_if_fail (slot >= 0, FALSE);
3484 retval = _dbus_data_slot_list_set (&slot_allocator,
3485 &message->slot_list,
3486 slot, data, free_data_func,
3487 &old_free_func, &old_data);
3491 /* Do the actual free outside the message lock */
3493 (* old_free_func) (old_data);
3500 * Retrieves data previously set with dbus_message_set_data().
3501 * The slot must still be allocated (must not have been freed).
3503 * @param message the message
3504 * @param slot the slot to get data from
3505 * @returns the data, or #NULL if not found
3508 dbus_message_get_data (DBusMessage *message,
3513 _dbus_return_val_if_fail (message != NULL, NULL);
3515 res = _dbus_data_slot_list_get (&slot_allocator,
3516 &message->slot_list,
3523 * Utility function to convert a machine-readable (not translated)
3524 * string into a D-BUS message type.
3527 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3528 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3529 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3530 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3531 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3536 dbus_message_type_from_string (const char *type_str)
3538 if (strcmp (type_str, "method_call") == 0)
3539 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3540 if (strcmp (type_str, "method_return") == 0)
3541 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3542 else if (strcmp (type_str, "signal") == 0)
3543 return DBUS_MESSAGE_TYPE_SIGNAL;
3544 else if (strcmp (type_str, "error") == 0)
3545 return DBUS_MESSAGE_TYPE_ERROR;
3547 return DBUS_MESSAGE_TYPE_INVALID;
3551 * Utility function to convert a D-BUS message type into a
3552 * machine-readable string (not translated).
3555 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3556 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3557 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3558 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3559 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3564 dbus_message_type_to_string (int type)
3568 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3569 return "method_call";
3570 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3571 return "method_return";
3572 case DBUS_MESSAGE_TYPE_SIGNAL:
3574 case DBUS_MESSAGE_TYPE_ERROR:
3582 #ifdef DBUS_BUILD_TESTS
3583 #include "dbus-test.h"
3588 check_have_valid_message (DBusMessageLoader *loader)
3590 DBusMessage *message;
3596 if (!_dbus_message_loader_queue_messages (loader))
3597 _dbus_assert_not_reached ("no memory to queue messages");
3599 if (_dbus_message_loader_get_is_corrupted (loader))
3601 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
3605 message = _dbus_message_loader_pop_message (loader);
3606 if (message == NULL)
3608 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3612 if (_dbus_string_get_length (&loader->data) > 0)
3614 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3620 /* Verify that we're able to properly deal with the message.
3621 * For example, this would detect improper handling of messages
3622 * in nonstandard byte order.
3624 if (!check_message_handling (message))
3632 dbus_message_unref (message);
3638 check_invalid_message (DBusMessageLoader *loader)
3644 if (!_dbus_message_loader_queue_messages (loader))
3645 _dbus_assert_not_reached ("no memory to queue messages");
3647 if (!_dbus_message_loader_get_is_corrupted (loader))
3649 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3660 check_incomplete_message (DBusMessageLoader *loader)
3662 DBusMessage *message;
3668 if (!_dbus_message_loader_queue_messages (loader))
3669 _dbus_assert_not_reached ("no memory to queue messages");
3671 if (_dbus_message_loader_get_is_corrupted (loader))
3673 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3677 message = _dbus_message_loader_pop_message (loader);
3678 if (message != NULL)
3680 _dbus_warn ("loaded message that was expected to be incomplete\n");
3688 dbus_message_unref (message);
3693 check_loader_results (DBusMessageLoader *loader,
3694 DBusMessageValidity validity)
3696 if (!_dbus_message_loader_queue_messages (loader))
3697 _dbus_assert_not_reached ("no memory to queue messages");
3701 case _DBUS_MESSAGE_VALID:
3702 return check_have_valid_message (loader);
3703 case _DBUS_MESSAGE_INVALID:
3704 return check_invalid_message (loader);
3705 case _DBUS_MESSAGE_INCOMPLETE:
3706 return check_incomplete_message (loader);
3707 case _DBUS_MESSAGE_UNKNOWN:
3711 _dbus_assert_not_reached ("bad DBusMessageValidity");
3717 * Loads the message in the given message file.
3719 * @param filename filename to load
3720 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3721 * @param data string to load message into
3722 * @returns #TRUE if the message was loaded
3725 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3737 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
3738 dbus_error_init (&error);
3739 if (!_dbus_file_get_contents (data, filename, &error))
3741 _dbus_warn ("Could not load message file %s: %s\n",
3742 _dbus_string_get_const_data (filename),
3744 dbus_error_free (&error);
3750 if (FALSE) /* Message builder disabled, probably permanently,
3751 * I want to do it another way
3754 _dbus_warn ("Could not load message file %s\n",
3755 _dbus_string_get_const_data (filename));
3768 * Tries loading the message in the given message file
3769 * and verifies that DBusMessageLoader can handle it.
3771 * @param filename filename to load
3772 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3773 * @param expected_validity what the message has to be like to return #TRUE
3774 * @returns #TRUE if the message has the expected validity
3777 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3779 DBusMessageValidity expected_validity)
3786 if (!_dbus_string_init (&data))
3787 _dbus_assert_not_reached ("could not allocate string\n");
3789 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3793 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3799 if (_dbus_string_get_length (&data) > 0)
3800 _dbus_verbose_bytes_of_string (&data, 0,
3801 _dbus_string_get_length (&data));
3803 _dbus_warn ("Failed message loader test on %s\n",
3804 _dbus_string_get_const_data (filename));
3807 _dbus_string_free (&data);
3813 * Tries loading the given message data.
3816 * @param data the message data
3817 * @param expected_validity what the message has to be like to return #TRUE
3818 * @returns #TRUE if the message has the expected validity
3821 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3822 DBusMessageValidity expected_validity)
3824 DBusMessageLoader *loader;
3832 /* Write the data one byte at a time */
3834 loader = _dbus_message_loader_new ();
3836 /* check some trivial loader functions */
3837 _dbus_message_loader_ref (loader);
3838 _dbus_message_loader_unref (loader);
3839 _dbus_message_loader_get_max_message_size (loader);
3841 len = _dbus_string_get_length (data);
3842 for (i = 0; i < len; i++)
3846 _dbus_message_loader_get_buffer (loader, &buffer);
3847 _dbus_string_append_byte (buffer,
3848 _dbus_string_get_byte (data, i));
3849 _dbus_message_loader_return_buffer (loader, buffer, 1);
3852 if (!check_loader_results (loader, expected_validity))
3855 _dbus_message_loader_unref (loader);
3858 /* Write the data all at once */
3860 loader = _dbus_message_loader_new ();
3865 _dbus_message_loader_get_buffer (loader, &buffer);
3866 _dbus_string_copy (data, 0, buffer,
3867 _dbus_string_get_length (buffer));
3868 _dbus_message_loader_return_buffer (loader, buffer, 1);
3871 if (!check_loader_results (loader, expected_validity))
3874 _dbus_message_loader_unref (loader);
3877 /* Write the data 2 bytes at a time */
3879 loader = _dbus_message_loader_new ();
3881 len = _dbus_string_get_length (data);
3882 for (i = 0; i < len; i += 2)
3886 _dbus_message_loader_get_buffer (loader, &buffer);
3887 _dbus_string_append_byte (buffer,
3888 _dbus_string_get_byte (data, i));
3890 _dbus_string_append_byte (buffer,
3891 _dbus_string_get_byte (data, i+1));
3892 _dbus_message_loader_return_buffer (loader, buffer, 1);
3895 if (!check_loader_results (loader, expected_validity))
3898 _dbus_message_loader_unref (loader);
3906 _dbus_message_loader_unref (loader);
3912 process_test_subdir (const DBusString *test_base_dir,
3914 DBusMessageValidity validity,
3915 DBusForeachMessageFileFunc function,
3918 DBusString test_directory;
3919 DBusString filename;
3927 if (!_dbus_string_init (&test_directory))
3928 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3930 _dbus_string_init_const (&filename, subdir);
3932 if (!_dbus_string_copy (test_base_dir, 0,
3933 &test_directory, 0))
3934 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3936 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3937 _dbus_assert_not_reached ("couldn't allocate full path");
3939 _dbus_string_free (&filename);
3940 if (!_dbus_string_init (&filename))
3941 _dbus_assert_not_reached ("didn't allocate filename string\n");
3943 dbus_error_init (&error);
3944 dir = _dbus_directory_open (&test_directory, &error);
3947 _dbus_warn ("Could not open %s: %s\n",
3948 _dbus_string_get_const_data (&test_directory),
3950 dbus_error_free (&error);
3954 printf ("Testing %s:\n", subdir);
3957 while (_dbus_directory_get_next_file (dir, &filename, &error))
3959 DBusString full_path;
3962 if (!_dbus_string_init (&full_path))
3963 _dbus_assert_not_reached ("couldn't init string");
3965 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3966 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3968 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3969 _dbus_assert_not_reached ("couldn't concat file to dir");
3971 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3973 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3977 _dbus_verbose ("Skipping non-.message file %s\n",
3978 _dbus_string_get_const_data (&filename));
3979 _dbus_string_free (&full_path);
3984 _dbus_string_get_const_data (&filename));
3986 _dbus_verbose (" expecting %s for %s\n",
3987 validity == _DBUS_MESSAGE_VALID ? "valid" :
3988 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3989 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")),
3990 _dbus_string_get_const_data (&filename));
3992 if (! (*function) (&full_path, is_raw, validity, user_data))
3994 _dbus_string_free (&full_path);
3998 _dbus_string_free (&full_path);
4001 if (dbus_error_is_set (&error))
4003 _dbus_warn ("Could not get next file in %s: %s\n",
4004 _dbus_string_get_const_data (&test_directory),
4006 dbus_error_free (&error);
4015 _dbus_directory_close (dir);
4016 _dbus_string_free (&test_directory);
4017 _dbus_string_free (&filename);
4023 * Runs the given function on every message file in the test suite.
4024 * The function should return #FALSE on test failure or fatal error.
4026 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
4027 * @param func the function to run
4028 * @param user_data data for function
4029 * @returns #FALSE if there's a failure
4032 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
4033 DBusForeachMessageFileFunc func,
4036 DBusString test_directory;
4041 _dbus_string_init_const (&test_directory, test_data_dir);
4043 if (!process_test_subdir (&test_directory, "valid-messages",
4044 _DBUS_MESSAGE_VALID, func, user_data))
4047 if (!process_test_subdir (&test_directory, "invalid-messages",
4048 _DBUS_MESSAGE_INVALID, func, user_data))
4051 if (!process_test_subdir (&test_directory, "incomplete-messages",
4052 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
4059 _dbus_string_free (&test_directory);
4064 #define GET_AND_CHECK(iter, typename, literal) \
4066 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
4067 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
4068 dbus_message_iter_get_basic (&iter, &v_##typename); \
4069 if (v_##typename != literal) \
4070 _dbus_assert_not_reached ("got wrong value from message iter"); \
4073 #define GET_AND_CHECK_STRCMP(iter, typename, literal) \
4075 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
4076 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
4077 dbus_message_iter_get_basic (&iter, &v_##typename); \
4078 if (strcmp (v_##typename, literal) != 0) \
4079 _dbus_assert_not_reached ("got wrong value from message iter"); \
4082 #define GET_AND_CHECK_AND_NEXT(iter, typename, literal) \
4084 GET_AND_CHECK(iter, typename, literal); \
4085 if (!dbus_message_iter_next (&iter)) \
4086 _dbus_assert_not_reached ("failed to move iter to next"); \
4089 #define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal) \
4091 GET_AND_CHECK_STRCMP(iter, typename, literal); \
4092 if (!dbus_message_iter_next (&iter)) \
4093 _dbus_assert_not_reached ("failed to move iter to next"); \
4097 message_iter_test (DBusMessage *message)
4099 DBusMessageIter iter, array, array2;
4100 const char *v_STRING;
4102 dbus_int32_t v_INT32;
4103 dbus_uint32_t v_UINT32;
4104 #ifdef DBUS_HAVE_INT64
4105 dbus_int64_t v_INT64;
4106 dbus_uint64_t v_UINT64;
4108 unsigned char v_BYTE;
4109 unsigned char v_BOOLEAN;
4111 const dbus_int32_t *our_int_array;
4114 dbus_message_iter_init (message, &iter);
4116 GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
4117 GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
4118 GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
4119 GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
4121 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
4122 _dbus_assert_not_reached ("Argument type not an array");
4124 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
4125 _dbus_assert_not_reached ("Array type not double");
4127 dbus_message_iter_recurse (&iter, &array);
4129 GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
4130 GET_AND_CHECK (array, DOUBLE, 2.5);
4132 if (dbus_message_iter_next (&array))
4133 _dbus_assert_not_reached ("Didn't reach end of array");
4135 if (!dbus_message_iter_next (&iter))
4136 _dbus_assert_not_reached ("Reached end of arguments");
4138 GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
4140 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
4141 _dbus_assert_not_reached ("no array");
4143 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_INT32)
4144 _dbus_assert_not_reached ("Array type not int32");
4147 dbus_message_iter_recurse (&iter, &array);
4149 if (dbus_message_iter_next (&array))
4150 _dbus_assert_not_reached ("Didn't reach end of array");
4152 if (!dbus_message_iter_next (&iter))
4153 _dbus_assert_not_reached ("Reached end of arguments");
4155 GET_AND_CHECK (iter, BYTE, 0xF0);
4157 if (dbus_message_iter_next (&iter))
4158 _dbus_assert_not_reached ("Didn't reach end of arguments");
4162 verify_test_message (DBusMessage *message)
4164 DBusMessageIter iter;
4166 dbus_int32_t our_int;
4167 const char *our_str;
4169 unsigned char our_bool;
4170 unsigned char our_byte_1, our_byte_2;
4171 dbus_uint32_t our_uint32;
4172 const dbus_int32_t *our_uint32_array = (void*)0xdeadbeef;
4173 int our_uint32_array_len;
4174 dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
4175 int our_int32_array_len;
4176 #ifdef DBUS_HAVE_INT64
4177 dbus_int64_t our_int64;
4178 dbus_uint64_t our_uint64;
4179 dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
4180 int our_uint64_array_len;
4181 const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
4182 int our_int64_array_len;
4184 const double *our_double_array = (void*)0xdeadbeef;
4185 int our_double_array_len;
4186 const unsigned char *our_byte_array = (void*)0xdeadbeef;
4187 int our_byte_array_len;
4188 const unsigned char *our_boolean_array = (void*)0xdeadbeef;
4189 int our_boolean_array_len;
4191 dbus_message_iter_init (message, &iter);
4193 dbus_error_init (&error);
4194 if (!dbus_message_iter_get_args (&iter, &error,
4195 DBUS_TYPE_INT32, &our_int,
4196 #ifdef DBUS_HAVE_INT64
4197 DBUS_TYPE_INT64, &our_int64,
4198 DBUS_TYPE_UINT64, &our_uint64,
4200 DBUS_TYPE_STRING, &our_str,
4201 DBUS_TYPE_DOUBLE, &our_double,
4202 DBUS_TYPE_BOOLEAN, &our_bool,
4203 DBUS_TYPE_BYTE, &our_byte_1,
4204 DBUS_TYPE_BYTE, &our_byte_2,
4205 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
4206 &our_uint32_array, &our_uint32_array_len,
4207 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
4208 &our_int32_array, &our_int32_array_len,
4209 #ifdef DBUS_HAVE_INT64
4210 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
4211 &our_uint64_array, &our_uint64_array_len,
4212 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
4213 &our_int64_array, &our_int64_array_len,
4215 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
4216 &our_double_array, &our_double_array_len,
4217 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
4218 &our_byte_array, &our_byte_array_len,
4219 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
4220 &our_boolean_array, &our_boolean_array_len,
4223 _dbus_warn ("error: %s - %s\n", error.name,
4224 (error.message != NULL) ? error.message : "no message");
4225 _dbus_assert_not_reached ("Could not get arguments");
4228 if (our_int != -0x12345678)
4229 _dbus_assert_not_reached ("integers differ!");
4231 #ifdef DBUS_HAVE_INT64
4232 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
4233 _dbus_assert_not_reached ("64-bit integers differ!");
4234 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
4235 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
4238 if (our_double != 3.14159)
4239 _dbus_assert_not_reached ("doubles differ!");
4241 if (strcmp (our_str, "Test string") != 0)
4242 _dbus_assert_not_reached ("strings differ!");
4245 _dbus_assert_not_reached ("booleans differ");
4247 if (our_byte_1 != 42)
4248 _dbus_assert_not_reached ("bytes differ!");
4250 if (our_byte_2 != 24)
4251 _dbus_assert_not_reached ("bytes differ!");
4253 if (our_uint32_array_len != 4 ||
4254 our_uint32_array[0] != 0x12345678 ||
4255 our_uint32_array[1] != 0x23456781 ||
4256 our_uint32_array[2] != 0x34567812 ||
4257 our_uint32_array[3] != 0x45678123)
4258 _dbus_assert_not_reached ("uint array differs");
4260 if (our_int32_array_len != 4 ||
4261 our_int32_array[0] != 0x12345678 ||
4262 our_int32_array[1] != -0x23456781 ||
4263 our_int32_array[2] != 0x34567812 ||
4264 our_int32_array[3] != -0x45678123)
4265 _dbus_assert_not_reached ("int array differs");
4267 #ifdef DBUS_HAVE_INT64
4268 if (our_uint64_array_len != 4 ||
4269 our_uint64_array[0] != 0x12345678 ||
4270 our_uint64_array[1] != 0x23456781 ||
4271 our_uint64_array[2] != 0x34567812 ||
4272 our_uint64_array[3] != 0x45678123)
4273 _dbus_assert_not_reached ("uint64 array differs");
4275 if (our_int64_array_len != 4 ||
4276 our_int64_array[0] != 0x12345678 ||
4277 our_int64_array[1] != -0x23456781 ||
4278 our_int64_array[2] != 0x34567812 ||
4279 our_int64_array[3] != -0x45678123)
4280 _dbus_assert_not_reached ("int64 array differs");
4281 #endif /* DBUS_HAVE_INT64 */
4283 if (our_double_array_len != 3)
4284 _dbus_assert_not_reached ("double array had wrong length");
4286 /* On all IEEE machines (i.e. everything sane) exact equality
4287 * should be preserved over the wire
4289 if (our_double_array[0] != 0.1234 ||
4290 our_double_array[1] != 9876.54321 ||
4291 our_double_array[2] != -300.0)
4292 _dbus_assert_not_reached ("double array had wrong values");
4294 if (our_byte_array_len != 4)
4295 _dbus_assert_not_reached ("byte array had wrong length");
4297 if (our_byte_array[0] != 'a' ||
4298 our_byte_array[1] != 'b' ||
4299 our_byte_array[2] != 'c' ||
4300 our_byte_array[3] != 234)
4301 _dbus_assert_not_reached ("byte array had wrong values");
4303 if (our_boolean_array_len != 5)
4304 _dbus_assert_not_reached ("bool array had wrong length");
4306 if (our_boolean_array[0] != TRUE ||
4307 our_boolean_array[1] != FALSE ||
4308 our_boolean_array[2] != TRUE ||
4309 our_boolean_array[3] != TRUE ||
4310 our_boolean_array[4] != FALSE)
4311 _dbus_assert_not_reached ("bool array had wrong values");
4313 if (dbus_message_iter_next (&iter))
4314 _dbus_assert_not_reached ("Didn't reach end of arguments");
4318 * @ingroup DBusMessageInternals
4319 * Unit test for DBusMessage.
4321 * @returns #TRUE on success.
4324 _dbus_message_test (const char *test_data_dir)
4326 DBusMessage *message;
4327 DBusMessageLoader *loader;
4328 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
4334 const dbus_uint32_t our_uint32_array[] =
4335 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
4336 const dbus_uint32_t our_int32_array[] =
4337 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
4338 const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
4339 const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
4340 #ifdef DBUS_HAVE_INT64
4341 const dbus_uint64_t our_uint64_array[] =
4342 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
4343 const dbus_uint64_t our_int64_array[] =
4344 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
4345 const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
4346 const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
4348 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
4349 const char **v_ARRAY_STRING = our_string_array;
4350 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
4351 const double *v_ARRAY_DOUBLE = our_double_array;
4352 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
4353 const unsigned char *v_ARRAY_BYTE = our_byte_array;
4354 const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
4355 const unsigned char *v_ARRAY_BOOLEAN = our_boolean_array;
4360 const char *v_STRING;
4362 dbus_int32_t v_INT32;
4363 dbus_uint32_t v_UINT32;
4364 #ifdef DBUS_HAVE_INT64
4365 dbus_int64_t v_INT64;
4366 dbus_uint64_t v_UINT64;
4368 unsigned char v_BYTE;
4369 unsigned char v2_BYTE;
4370 unsigned char v_BOOLEAN;
4372 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
4374 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
4375 "/org/freedesktop/TestPath",
4376 "Foo.TestInterface",
4378 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
4379 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
4381 _dbus_assert (strcmp (dbus_message_get_path (message),
4382 "/org/freedesktop/TestPath") == 0);
4383 _dbus_message_set_serial (message, 1234);
4385 /* string length including nul byte not a multiple of 4 */
4386 if (!dbus_message_set_sender (message, "org.foo.bar1"))
4387 _dbus_assert_not_reached ("out of memory");
4389 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
4390 dbus_message_set_reply_serial (message, 5678);
4392 _dbus_verbose_bytes_of_string (&message->header.data, 0,
4393 _dbus_string_get_length (&message->header.data));
4394 _dbus_verbose_bytes_of_string (&message->body, 0,
4395 _dbus_string_get_length (&message->body));
4397 if (!dbus_message_set_sender (message, NULL))
4398 _dbus_assert_not_reached ("out of memory");
4401 _dbus_verbose_bytes_of_string (&message->header.data, 0,
4402 _dbus_string_get_length (&message->header.data));
4403 _dbus_verbose_bytes_of_string (&message->body, 0,
4404 _dbus_string_get_length (&message->body));
4407 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
4408 _dbus_assert (dbus_message_get_serial (message) == 1234);
4409 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
4410 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
4412 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
4413 dbus_message_set_no_reply (message, TRUE);
4414 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
4415 dbus_message_set_no_reply (message, FALSE);
4416 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
4418 /* Set/get some header fields */
4420 if (!dbus_message_set_path (message, "/foo"))
4421 _dbus_assert_not_reached ("out of memory");
4422 _dbus_assert (strcmp (dbus_message_get_path (message),
4425 if (!dbus_message_set_interface (message, "org.Foo"))
4426 _dbus_assert_not_reached ("out of memory");
4427 _dbus_assert (strcmp (dbus_message_get_interface (message),
4430 if (!dbus_message_set_member (message, "Bar"))
4431 _dbus_assert_not_reached ("out of memory");
4432 _dbus_assert (strcmp (dbus_message_get_member (message),
4435 /* Set/get them with longer values */
4436 if (!dbus_message_set_path (message, "/foo/bar"))
4437 _dbus_assert_not_reached ("out of memory");
4438 _dbus_assert (strcmp (dbus_message_get_path (message),
4441 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
4442 _dbus_assert_not_reached ("out of memory");
4443 _dbus_assert (strcmp (dbus_message_get_interface (message),
4444 "org.Foo.Bar") == 0);
4446 if (!dbus_message_set_member (message, "BarFoo"))
4447 _dbus_assert_not_reached ("out of memory");
4448 _dbus_assert (strcmp (dbus_message_get_member (message),
4451 /* Realloc shorter again */
4453 if (!dbus_message_set_path (message, "/foo"))
4454 _dbus_assert_not_reached ("out of memory");
4455 _dbus_assert (strcmp (dbus_message_get_path (message),
4458 if (!dbus_message_set_interface (message, "org.Foo"))
4459 _dbus_assert_not_reached ("out of memory");
4460 _dbus_assert (strcmp (dbus_message_get_interface (message),
4463 if (!dbus_message_set_member (message, "Bar"))
4464 _dbus_assert_not_reached ("out of memory");
4465 _dbus_assert (strcmp (dbus_message_get_member (message),
4468 dbus_message_unref (message);
4470 /* Test the vararg functions */
4471 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
4472 "/org/freedesktop/TestPath",
4473 "Foo.TestInterface",
4475 _dbus_message_set_serial (message, 1);
4477 v_INT32 = -0x12345678;
4478 #ifdef DBUS_HAVE_INT64
4479 v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
4480 v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
4482 v_STRING = "Test string";
4488 dbus_message_append_args (message,
4489 DBUS_TYPE_INT32, &v_INT32,
4490 #ifdef DBUS_HAVE_INT64
4491 DBUS_TYPE_INT64, &v_INT64,
4492 DBUS_TYPE_UINT64, &v_UINT64,
4494 DBUS_TYPE_STRING, &v_STRING,
4495 DBUS_TYPE_DOUBLE, &v_DOUBLE,
4496 DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
4497 DBUS_TYPE_BYTE, &v_BYTE,
4498 DBUS_TYPE_BYTE, &v2_BYTE,
4499 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
4500 _DBUS_N_ELEMENTS (our_uint32_array),
4501 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
4502 _DBUS_N_ELEMENTS (our_int32_array),
4503 #ifdef DBUS_HAVE_INT64
4504 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
4505 _DBUS_N_ELEMENTS (our_uint64_array),
4506 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
4507 _DBUS_N_ELEMENTS (our_int64_array),
4509 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
4510 _DBUS_N_ELEMENTS (our_double_array),
4511 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
4512 _DBUS_N_ELEMENTS (our_byte_array),
4513 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
4514 _DBUS_N_ELEMENTS (our_boolean_array),
4518 sig[i++] = DBUS_TYPE_INT32;
4519 #ifdef DBUS_HAVE_INT64
4520 sig[i++] = DBUS_TYPE_INT64;
4521 sig[i++] = DBUS_TYPE_UINT64;
4523 sig[i++] = DBUS_TYPE_STRING;
4524 sig[i++] = DBUS_TYPE_DOUBLE;
4525 sig[i++] = DBUS_TYPE_BOOLEAN;
4526 sig[i++] = DBUS_TYPE_BYTE;
4527 sig[i++] = DBUS_TYPE_BYTE;
4528 sig[i++] = DBUS_TYPE_ARRAY;
4529 sig[i++] = DBUS_TYPE_UINT32;
4530 sig[i++] = DBUS_TYPE_ARRAY;
4531 sig[i++] = DBUS_TYPE_INT32;
4532 #ifdef DBUS_HAVE_INT64
4533 sig[i++] = DBUS_TYPE_ARRAY;
4534 sig[i++] = DBUS_TYPE_UINT64;
4535 sig[i++] = DBUS_TYPE_ARRAY;
4536 sig[i++] = DBUS_TYPE_INT64;
4538 sig[i++] = DBUS_TYPE_ARRAY;
4539 sig[i++] = DBUS_TYPE_DOUBLE;
4540 sig[i++] = DBUS_TYPE_ARRAY;
4541 sig[i++] = DBUS_TYPE_BYTE;
4542 sig[i++] = DBUS_TYPE_ARRAY;
4543 sig[i++] = DBUS_TYPE_BOOLEAN;
4544 sig[i++] = DBUS_TYPE_INVALID;
4546 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
4548 _dbus_verbose ("HEADER\n");
4549 _dbus_verbose_bytes_of_string (&message->header.data, 0,
4550 _dbus_string_get_length (&message->header.data));
4551 _dbus_verbose ("BODY\n");
4552 _dbus_verbose_bytes_of_string (&message->body, 0,
4553 _dbus_string_get_length (&message->body));
4555 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
4556 sig, dbus_message_get_signature (message));
4558 s = dbus_message_get_signature (message);
4560 _dbus_assert (dbus_message_has_signature (message, sig));
4561 _dbus_assert (strcmp (s, sig) == 0);
4563 verify_test_message (message);
4565 copy = dbus_message_copy (message);
4567 _dbus_assert (dbus_message_get_reply_serial (message) ==
4568 dbus_message_get_reply_serial (copy));
4569 _dbus_assert (message->header.padding == copy->header.padding);
4571 _dbus_assert (_dbus_string_get_length (&message->header.data) ==
4572 _dbus_string_get_length (©->header.data));
4574 _dbus_assert (_dbus_string_get_length (&message->body) ==
4575 _dbus_string_get_length (©->body));
4577 verify_test_message (copy);
4579 name1 = dbus_message_get_interface (message);
4580 name2 = dbus_message_get_interface (copy);
4582 _dbus_assert (strcmp (name1, name2) == 0);
4584 name1 = dbus_message_get_member (message);
4585 name2 = dbus_message_get_member (copy);
4587 _dbus_assert (strcmp (name1, name2) == 0);
4589 dbus_message_unref (message);
4590 dbus_message_unref (copy);
4594 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
4595 "/org/freedesktop/TestPath",
4596 "Foo.TestInterface",
4599 _dbus_message_set_serial (message, 1);
4600 dbus_message_set_reply_serial (message, 0x12345678);
4602 dbus_message_iter_init_append (message, &iter);
4603 dbus_message_iter_append_string (&iter, "Test string");
4604 dbus_message_iter_append_int32 (&iter, -0x12345678);
4605 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
4606 dbus_message_iter_append_double (&iter, 3.14159);
4608 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
4609 dbus_message_iter_append_double (&child_iter, 1.5);
4610 dbus_message_iter_append_double (&child_iter, 2.5);
4613 dbus_message_iter_append_dict (&iter, &child_iter);
4614 dbus_message_iter_append_dict_key (&child_iter, "test");
4615 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
4617 /* dict (in dict) */
4618 dbus_message_iter_append_dict_key (&child_iter, "testdict");
4619 dbus_message_iter_append_dict (&child_iter, &child_iter2);
4621 dbus_message_iter_append_dict_key (&child_iter2, "dictkey");
4622 dbus_message_iter_append_string (&child_iter2, "dictvalue");
4624 /* array of array of int32 (in dict) */
4625 dbus_message_iter_append_dict_key (&child_iter, "array");
4626 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
4627 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
4628 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
4629 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
4630 _dbus_warn ("next call expected to fail with wrong array type\n");
4631 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
4632 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
4633 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
4634 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
4635 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
4637 dbus_message_iter_append_byte (&iter, 0xF0);
4639 dbus_message_iter_append_nil (&iter);
4641 dbus_message_iter_append_custom (&iter, "MyTypeName",
4644 dbus_message_iter_append_byte (&iter, 0xF0);
4646 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_INT32);
4648 dbus_message_iter_append_byte (&iter, 0xF0);
4650 dbus_message_iter_append_dict (&iter, &child_iter);
4652 dbus_message_iter_append_byte (&iter, 0xF0);
4654 message_iter_test (message);
4656 /* Message loader test */
4657 _dbus_message_lock (message);
4658 loader = _dbus_message_loader_new ();
4660 /* check ref/unref */
4661 _dbus_message_loader_ref (loader);
4662 _dbus_message_loader_unref (loader);
4664 /* Write the header data one byte at a time */
4665 data = _dbus_string_get_const_data (&message->header);
4666 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
4670 _dbus_message_loader_get_buffer (loader, &buffer);
4671 _dbus_string_append_byte (buffer, data[i]);
4672 _dbus_message_loader_return_buffer (loader, buffer, 1);
4675 /* Write the body data one byte at a time */
4676 data = _dbus_string_get_const_data (&message->body);
4677 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
4681 _dbus_message_loader_get_buffer (loader, &buffer);
4682 _dbus_string_append_byte (buffer, data[i]);
4683 _dbus_message_loader_return_buffer (loader, buffer, 1);
4686 copy = dbus_message_copy (message); /* save for tests below */
4687 dbus_message_unref (message);
4689 /* Now pop back the message */
4690 if (!_dbus_message_loader_queue_messages (loader))
4691 _dbus_assert_not_reached ("no memory to queue messages");
4693 if (_dbus_message_loader_get_is_corrupted (loader))
4694 _dbus_assert_not_reached ("message loader corrupted");
4696 message = _dbus_message_loader_pop_message (loader);
4698 _dbus_assert_not_reached ("received a NULL message");
4700 if (dbus_message_get_reply_serial (message) != 0x12345678)
4701 _dbus_assert_not_reached ("reply serial fields differ");
4703 message_iter_test (message);
4705 dbus_message_unref (message);
4706 _dbus_message_loader_unref (loader);
4708 message = dbus_message_new_method_return (copy);
4709 if (message == NULL)
4710 _dbus_assert_not_reached ("out of memory\n");
4711 dbus_message_unref (copy);
4713 if (!dbus_message_append_args (message,
4714 DBUS_TYPE_STRING, "hello",
4716 _dbus_assert_not_reached ("no memory");
4718 if (!dbus_message_has_signature (message, "s"))
4719 _dbus_assert_not_reached ("method return has wrong signature");
4721 dbus_error_init (&error);
4722 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
4723 &t, DBUS_TYPE_INVALID))
4726 _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
4731 dbus_message_unref (message);
4733 /* This ServiceAcquired message used to trigger a bug in
4734 * setting header fields, adding to regression test.
4736 message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_DBUS,
4737 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
4740 if (message == NULL)
4741 _dbus_assert_not_reached ("out of memory");
4743 _dbus_verbose ("Bytes after creation\n");
4744 _dbus_verbose_bytes_of_string (&message->header, 0,
4745 _dbus_string_get_length (&message->header));
4747 if (!dbus_message_set_destination (message, ":1.0") ||
4748 !dbus_message_append_args (message,
4749 DBUS_TYPE_STRING, ":1.0",
4751 _dbus_assert_not_reached ("out of memory");
4753 _dbus_verbose ("Bytes after set_destination() and append_args()\n");
4754 _dbus_verbose_bytes_of_string (&message->header, 0,
4755 _dbus_string_get_length (&message->header));
4757 if (!dbus_message_set_sender (message, "org.freedesktop.DBus"))
4758 _dbus_assert_not_reached ("out of memory");
4760 _dbus_verbose ("Bytes after set_sender()\n");
4761 _dbus_verbose_bytes_of_string (&message->header, 0,
4762 _dbus_string_get_length (&message->header));
4764 /* When the bug happened the above set_destination() would
4765 * corrupt the signature
4767 if (!dbus_message_has_signature (message, "s"))
4769 _dbus_warn ("Signature should be 's' but is '%s'\n",
4770 dbus_message_get_signature (message));
4771 _dbus_assert_not_reached ("signal has wrong signature");
4774 /* have to set destination again to reproduce the bug */
4775 if (!dbus_message_set_destination (message, ":1.0"))
4776 _dbus_assert_not_reached ("out of memory");
4778 _dbus_verbose ("Bytes after set_destination()\n");
4779 _dbus_verbose_bytes_of_string (&message->header, 0,
4780 _dbus_string_get_length (&message->header));
4782 /* When the bug happened the above set_destination() would
4783 * corrupt the signature
4785 if (!dbus_message_has_signature (message, "s"))
4787 _dbus_warn ("Signature should be 's' but is '%s'\n",
4788 dbus_message_get_signature (message));
4789 _dbus_assert_not_reached ("signal has wrong signature");
4792 dbus_error_init (&error);
4793 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
4794 &t, DBUS_TYPE_INVALID))
4797 _dbus_warn ("Failed to get expected string arg for signal: %s\n", error.message);
4802 dbus_message_unref (message);
4804 /* Now load every message in test_data_dir if we have one */
4805 if (test_data_dir == NULL)
4808 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
4809 (DBusForeachMessageFileFunc)
4810 dbus_internal_do_not_use_try_message_file,
4813 #endif /* Commented out most tests for now */
4818 #endif /* DBUS_BUILD_TESTS */