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-private.h"
30 #include "dbus-object-tree.h"
31 #include "dbus-memory.h"
32 #include "dbus-list.h"
36 * @defgroup DBusMessageInternals DBusMessage implementation details
37 * @ingroup DBusInternals
38 * @brief DBusMessage private implementation details.
40 * The guts of DBusMessage and its methods.
45 /* Not thread locked, but strictly const/read-only so should be OK
47 /** An static string representing an empty signature */
48 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
50 /* these have wacky values to help trap uninitialized iterators;
51 * but has to fit in 3 bits
54 DBUS_MESSAGE_ITER_TYPE_READER = 3,
55 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
58 /** typedef for internals of message iterator */
59 typedef struct DBusMessageRealIter DBusMessageRealIter;
62 * @brief Internals of DBusMessageIter
64 * Object representing a position in a message. All fields are internal.
66 struct DBusMessageRealIter
68 DBusMessage *message; /**< Message used */
69 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
70 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
71 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
74 DBusTypeWriter writer; /**< writer */
75 DBusTypeReader reader; /**< reader */
76 } u; /**< the type writer or reader that does all the work */
80 * Gets the data to be sent over the network for this message.
81 * The header and then the body should be written out.
82 * This function is guaranteed to always return the same
83 * data once a message is locked (with _dbus_message_lock()).
85 * @param message the message.
86 * @param header return location for message header data.
87 * @param body return location for message body data.
90 _dbus_message_get_network_data (DBusMessage *message,
91 const DBusString **header,
92 const DBusString **body)
94 _dbus_assert (message->locked);
96 *header = &message->header.data;
97 *body = &message->body;
101 * Sets the serial number of a message.
102 * This can only be done once on a message.
104 * @param message the message
105 * @param serial the serial
108 _dbus_message_set_serial (DBusMessage *message,
109 dbus_uint32_t serial)
111 _dbus_assert (message != NULL);
112 _dbus_assert (!message->locked);
113 _dbus_assert (dbus_message_get_serial (message) == 0);
115 _dbus_header_set_serial (&message->header, serial);
119 * Adds a counter to be incremented immediately with the
120 * size of this message, and decremented by the size
121 * of this message when this message if finalized.
122 * The link contains a counter with its refcount already
123 * incremented, but the counter itself not incremented.
124 * Ownership of link and counter refcount is passed to
127 * @param message the message
128 * @param link link with counter as data
131 _dbus_message_add_size_counter_link (DBusMessage *message,
134 /* right now we don't recompute the delta when message
135 * size changes, and that's OK for current purposes
136 * I think, but could be important to change later.
137 * Do recompute it whenever there are no outstanding counters,
138 * since it's basically free.
140 if (message->size_counters == NULL)
142 message->size_counter_delta =
143 _dbus_string_get_length (&message->header.data) +
144 _dbus_string_get_length (&message->body);
147 _dbus_verbose ("message has size %ld\n",
148 message->size_counter_delta);
152 _dbus_list_append_link (&message->size_counters, link);
154 _dbus_counter_adjust (link->data, message->size_counter_delta);
158 * Adds a counter to be incremented immediately with the
159 * size of this message, and decremented by the size
160 * of this message when this message if finalized.
162 * @param message the message
163 * @param counter the counter
164 * @returns #FALSE if no memory
167 _dbus_message_add_size_counter (DBusMessage *message,
168 DBusCounter *counter)
172 link = _dbus_list_alloc_link (counter);
176 _dbus_counter_ref (counter);
177 _dbus_message_add_size_counter_link (message, link);
183 * Removes a counter tracking the size of this message, and decrements
184 * the counter by the size of this message.
186 * @param message the message
187 * @param link_return return the link used
188 * @param counter the counter
191 _dbus_message_remove_size_counter (DBusMessage *message,
192 DBusCounter *counter,
193 DBusList **link_return)
197 link = _dbus_list_find_last (&message->size_counters,
199 _dbus_assert (link != NULL);
201 _dbus_list_unlink (&message->size_counters,
206 _dbus_list_free_link (link);
208 _dbus_counter_adjust (counter, - message->size_counter_delta);
210 _dbus_counter_unref (counter);
214 * Locks a message. Allows checking that applications don't keep a
215 * reference to a message in the outgoing queue and change it
216 * underneath us. Messages are locked when they enter the outgoing
217 * queue (dbus_connection_send_message()), and the library complains
218 * if the message is modified while locked.
220 * @param message the message to lock.
223 _dbus_message_lock (DBusMessage *message)
225 if (!message->locked)
227 _dbus_header_update_lengths (&message->header,
228 _dbus_string_get_length (&message->body));
230 /* must have a signature if you have a body */
231 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
232 dbus_message_get_signature (message) != NULL);
234 message->locked = TRUE;
239 set_or_delete_string_field (DBusMessage *message,
245 return _dbus_header_delete_field (&message->header, field);
247 return _dbus_header_set_field_basic (&message->header,
254 get_const_signature (DBusHeader *header,
255 const DBusString **type_str_p,
258 if (_dbus_header_get_field_raw (header,
259 DBUS_HEADER_FIELD_SIGNATURE,
263 *type_pos_p += 1; /* skip the signature length which is 1 byte */
267 *type_str_p = &_dbus_empty_signature_str;
273 /* Probably we don't need to use this */
275 * Sets the signature of the message, i.e. the arguments in the
276 * message payload. The signature includes only "in" arguments for
277 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
278 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
279 * what you might expect (it does not include the signature of the
280 * entire C++-style method).
282 * The signature is a string made up of type codes such as
283 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
284 * the value of #DBUS_TYPE_INVALID). The macros such as
285 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
286 * may find it useful to use the string forms, such as
287 * #DBUS_TYPE_INT32_AS_STRING.
289 * An "unset" or #NULL signature is considered the same as an empty
290 * signature. In fact dbus_message_get_signature() will never return
293 * @param message the message
294 * @param signature the type signature or #NULL to unset
295 * @returns #FALSE if no memory
298 _dbus_message_set_signature (DBusMessage *message,
299 const char *signature)
301 _dbus_return_val_if_fail (message != NULL, FALSE);
302 _dbus_return_val_if_fail (!message->locked, FALSE);
303 _dbus_return_val_if_fail (signature == NULL ||
304 _dbus_check_is_valid_signature (signature));
305 /* can't delete the signature if you have a message body */
306 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
309 return set_or_delete_string_field (message,
310 DBUS_HEADER_FIELD_SIGNATURE,
319 * @defgroup DBusMessage DBusMessage
321 * @brief Message to be sent or received over a DBusConnection.
323 * A DBusMessage is the most basic unit of communication over a
324 * DBusConnection. A DBusConnection represents a stream of messages
325 * received from a remote application, and a stream of messages
326 * sent to a remote application.
332 * @typedef DBusMessage
334 * Opaque data type representing a message received from or to be
335 * sent to another application.
339 * Returns the serial of a message or 0 if none has been specified.
340 * The message's serial number is provided by the application sending
341 * the message and is used to identify replies to this message. All
342 * messages received on a connection will have a serial, but messages
343 * you haven't sent yet may return 0.
345 * @param message the message
346 * @returns the client serial
349 dbus_message_get_serial (DBusMessage *message)
351 _dbus_return_val_if_fail (message != NULL, 0);
353 return _dbus_header_get_serial (&message->header);
357 * Sets the reply serial of a message (the client serial
358 * of the message this is a reply to).
360 * @param message the message
361 * @param reply_serial the client serial
362 * @returns #FALSE if not enough memory
365 dbus_message_set_reply_serial (DBusMessage *message,
366 dbus_uint32_t reply_serial)
368 _dbus_return_val_if_fail (message != NULL, FALSE);
369 _dbus_return_val_if_fail (!message->locked, FALSE);
371 return _dbus_header_set_field_basic (&message->header,
372 DBUS_HEADER_FIELD_REPLY_SERIAL,
378 * Returns the serial that the message is a reply to or 0 if none.
380 * @param message the message
381 * @returns the reply serial
384 dbus_message_get_reply_serial (DBusMessage *message)
386 dbus_uint32_t v_UINT32;
388 _dbus_return_val_if_fail (message != NULL, 0);
390 if (_dbus_header_get_field_basic (&message->header,
391 DBUS_HEADER_FIELD_REPLY_SERIAL,
400 free_size_counter (void *element,
403 DBusCounter *counter = element;
404 DBusMessage *message = data;
406 _dbus_counter_adjust (counter, - message->size_counter_delta);
408 _dbus_counter_unref (counter);
412 dbus_message_finalize (DBusMessage *message)
414 _dbus_assert (message->refcount.value == 0);
416 /* This calls application callbacks! */
417 _dbus_data_slot_list_free (&message->slot_list);
419 _dbus_list_foreach (&message->size_counters,
420 free_size_counter, message);
421 _dbus_list_clear (&message->size_counters);
423 _dbus_header_free (&message->header);
424 _dbus_string_free (&message->body);
431 * We cache some DBusMessage to reduce the overhead of allocating
432 * them. In my profiling this consistently made about an 8%
433 * difference. It avoids the malloc for the message, the malloc for
434 * the slot list, the malloc for the header string and body string,
435 * and the associated free() calls. It does introduce another global
436 * lock which could be a performance issue in certain cases.
438 * For the echo client/server the round trip time goes from around
439 * .000077 to .000069 with the message cache on my laptop. The sysprof
440 * change is as follows (numbers are cumulative percentage):
442 * with message cache implemented as array as it is now (0.000069 per):
443 * new_empty_header 1.46
444 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
450 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
453 * with message cache implemented as list (0.000070 per roundtrip):
454 * new_empty_header 2.72
455 * list_pop_first 1.88
459 * without cache (0.000077 per roundtrip):
460 * new_empty_header 6.7
461 * string_init_preallocated 3.43
470 * If you implement the message_cache with a list, the primary reason
471 * it's slower is that you add another thread lock (on the DBusList
475 /** Avoid caching huge messages */
476 #define MAX_MESSAGE_SIZE_TO_CACHE _DBUS_ONE_MEGABYTE
478 /** Avoid caching too many messages */
479 #define MAX_MESSAGE_CACHE_SIZE 5
481 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
482 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
483 static int message_cache_count = 0;
484 static dbus_bool_t message_cache_shutdown_registered = FALSE;
487 dbus_message_cache_shutdown (void *data)
491 _DBUS_LOCK (message_cache);
494 while (i < MAX_MESSAGE_CACHE_SIZE)
496 if (message_cache[i])
497 dbus_message_finalize (message_cache[i]);
502 message_cache_count = 0;
503 message_cache_shutdown_registered = FALSE;
505 _DBUS_UNLOCK (message_cache);
509 * Tries to get a message from the message cache. The retrieved
510 * message will have junk in it, so it still needs to be cleared out
511 * in dbus_message_new_empty_header()
513 * @returns the message, or #NULL if none cached
516 dbus_message_get_cached (void)
518 DBusMessage *message;
523 _DBUS_LOCK (message_cache);
525 _dbus_assert (message_cache_count >= 0);
527 if (message_cache_count == 0)
529 _DBUS_UNLOCK (message_cache);
533 /* This is not necessarily true unless count > 0, and
534 * message_cache is uninitialized until the shutdown is
537 _dbus_assert (message_cache_shutdown_registered);
540 while (i < MAX_MESSAGE_CACHE_SIZE)
542 if (message_cache[i])
544 message = message_cache[i];
545 message_cache[i] = NULL;
546 message_cache_count -= 1;
551 _dbus_assert (message_cache_count >= 0);
552 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
553 _dbus_assert (message != NULL);
555 _DBUS_UNLOCK (message_cache);
557 _dbus_assert (message->refcount.value == 0);
558 _dbus_assert (message->size_counters == NULL);
564 * Tries to cache a message, otherwise finalize it.
566 * @param message the message
569 dbus_message_cache_or_finalize (DBusMessage *message)
571 dbus_bool_t was_cached;
574 _dbus_assert (message->refcount.value == 0);
576 /* This calls application code and has to be done first thing
577 * without holding the lock
579 _dbus_data_slot_list_clear (&message->slot_list);
581 _dbus_list_foreach (&message->size_counters,
582 free_size_counter, message);
583 _dbus_list_clear (&message->size_counters);
587 _DBUS_LOCK (message_cache);
589 if (!message_cache_shutdown_registered)
591 _dbus_assert (message_cache_count == 0);
593 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
597 while (i < MAX_MESSAGE_CACHE_SIZE)
599 message_cache[i] = NULL;
603 message_cache_shutdown_registered = TRUE;
606 _dbus_assert (message_cache_count >= 0);
608 if ((_dbus_string_get_length (&message->header.data) +
609 _dbus_string_get_length (&message->body)) >
610 MAX_MESSAGE_SIZE_TO_CACHE)
613 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
616 /* Find empty slot */
618 while (message_cache[i] != NULL)
621 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
623 _dbus_assert (message_cache[i] == NULL);
624 message_cache[i] = message;
625 message_cache_count += 1;
629 _DBUS_UNLOCK (message_cache);
632 dbus_message_finalize (message);
636 dbus_message_new_empty_header (void)
638 DBusMessage *message;
639 dbus_bool_t from_cache;
641 message = dbus_message_get_cached ();
650 message = dbus_new (DBusMessage, 1);
653 #ifndef DBUS_DISABLE_CHECKS
654 message->generation = _dbus_current_generation;
658 message->refcount.value = 1;
659 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
660 message->locked = FALSE;
661 message->size_counters = NULL;
662 message->size_counter_delta = 0;
663 message->changed_stamp = 0;
666 _dbus_data_slot_list_init (&message->slot_list);
670 _dbus_header_reinit (&message->header, message->byte_order);
671 _dbus_string_set_length (&message->body, 0);
675 if (!_dbus_header_init (&message->header, message->byte_order))
681 if (!_dbus_string_init_preallocated (&message->body, 32))
683 _dbus_header_free (&message->header);
693 * Constructs a new message of the given message type.
694 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
695 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
697 * @param message_type type of message
698 * @returns new message or #NULL If no memory
701 dbus_message_new (int message_type)
703 DBusMessage *message;
705 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
707 message = dbus_message_new_empty_header ();
711 if (!_dbus_header_create (&message->header,
713 NULL, NULL, NULL, NULL, NULL))
715 dbus_message_unref (message);
723 * Constructs a new message to invoke a method on a remote
724 * object. Returns #NULL if memory can't be allocated for the
725 * message. The destination may be #NULL in which case no destination
726 * is set; this is appropriate when using D-BUS in a peer-to-peer
727 * context (no message bus). The interface may be #NULL, which means
728 * that if multiple methods with the given name exist it is undefined
729 * which one will be invoked.
731 * @param destination name that the message should be sent to or #NULL
732 * @param path object path the message should be sent to
733 * @param interface interface to invoke method on
734 * @param method method to invoke
736 * @returns a new DBusMessage, free with dbus_message_unref()
737 * @see dbus_message_unref()
740 dbus_message_new_method_call (const char *destination,
742 const char *interface,
745 DBusMessage *message;
747 _dbus_return_val_if_fail (path != NULL, NULL);
748 _dbus_return_val_if_fail (method != NULL, NULL);
749 _dbus_return_val_if_fail (destination == NULL ||
750 _dbus_check_is_valid_bus_name (destination), NULL);
751 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
752 _dbus_return_val_if_fail (interface == NULL ||
753 _dbus_check_is_valid_interface (interface), NULL);
754 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
756 message = dbus_message_new_empty_header ();
760 if (!_dbus_header_create (&message->header,
761 DBUS_MESSAGE_TYPE_METHOD_CALL,
762 destination, path, interface, method, NULL))
764 dbus_message_unref (message);
772 * Constructs a message that is a reply to a method call. Returns
773 * #NULL if memory can't be allocated for the message.
775 * @param method_call the message which the created
776 * message is a reply to.
777 * @returns a new DBusMessage, free with dbus_message_unref()
778 * @see dbus_message_new_method_call(), dbus_message_unref()
781 dbus_message_new_method_return (DBusMessage *method_call)
783 DBusMessage *message;
786 _dbus_return_val_if_fail (method_call != NULL, NULL);
788 sender = dbus_message_get_sender (method_call);
790 /* sender is allowed to be null here in peer-to-peer case */
792 message = dbus_message_new_empty_header ();
796 if (!_dbus_header_create (&message->header,
797 DBUS_MESSAGE_TYPE_METHOD_RETURN,
798 sender, NULL, NULL, NULL, NULL))
800 dbus_message_unref (message);
804 dbus_message_set_no_reply (message, TRUE);
806 if (!dbus_message_set_reply_serial (message,
807 dbus_message_get_serial (method_call)))
809 dbus_message_unref (message);
817 * Constructs a new message representing a signal emission. Returns
818 * #NULL if memory can't be allocated for the message. A signal is
819 * identified by its originating interface, and the name of the
822 * @param path the path to the object emitting the signal
823 * @param interface the interface the signal is emitted from
824 * @param name name of the signal
825 * @returns a new DBusMessage, free with dbus_message_unref()
826 * @see dbus_message_unref()
829 dbus_message_new_signal (const char *path,
830 const char *interface,
833 DBusMessage *message;
835 _dbus_return_val_if_fail (path != NULL, NULL);
836 _dbus_return_val_if_fail (interface != NULL, NULL);
837 _dbus_return_val_if_fail (name != NULL, NULL);
838 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
839 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
840 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
842 message = dbus_message_new_empty_header ();
846 if (!_dbus_header_create (&message->header,
847 DBUS_MESSAGE_TYPE_SIGNAL,
848 NULL, path, interface, name, NULL))
850 dbus_message_unref (message);
854 dbus_message_set_no_reply (message, TRUE);
860 * Creates a new message that is an error reply to a certain message.
861 * Error replies are possible in response to method calls primarily.
863 * @param reply_to the original message
864 * @param error_name the error name
865 * @param error_message the error message string or #NULL for none
866 * @returns a new error message
869 dbus_message_new_error (DBusMessage *reply_to,
870 const char *error_name,
871 const char *error_message)
873 DBusMessage *message;
875 DBusMessageIter iter;
877 _dbus_return_val_if_fail (reply_to != NULL, NULL);
878 _dbus_return_val_if_fail (error_name != NULL, NULL);
879 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
881 sender = dbus_message_get_sender (reply_to);
883 /* sender may be NULL for non-message-bus case or
884 * when the message bus is dealing with an unregistered
887 message = dbus_message_new_empty_header ();
891 if (!_dbus_header_create (&message->header,
892 DBUS_MESSAGE_TYPE_ERROR,
893 sender, NULL, NULL, NULL, error_name))
895 dbus_message_unref (message);
899 dbus_message_set_no_reply (message, TRUE);
901 if (!dbus_message_set_reply_serial (message,
902 dbus_message_get_serial (reply_to)))
904 dbus_message_unref (message);
908 if (error_message != NULL)
910 dbus_message_iter_init_append (message, &iter);
911 if (!dbus_message_iter_append_basic (&iter,
915 dbus_message_unref (message);
924 * Creates a new message that is an error reply to a certain message.
925 * Error replies are possible in response to method calls primarily.
927 * @param reply_to the original message
928 * @param error_name the error name
929 * @param error_format the error message format as with printf
930 * @param ... format string arguments
931 * @returns a new error message
934 dbus_message_new_error_printf (DBusMessage *reply_to,
935 const char *error_name,
936 const char *error_format,
941 DBusMessage *message;
943 _dbus_return_val_if_fail (reply_to != NULL, NULL);
944 _dbus_return_val_if_fail (error_name != NULL, NULL);
945 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
947 if (!_dbus_string_init (&str))
950 va_start (args, error_format);
952 if (_dbus_string_append_printf_valist (&str, error_format, args))
953 message = dbus_message_new_error (reply_to, error_name,
954 _dbus_string_get_const_data (&str));
958 _dbus_string_free (&str);
967 * Creates a new message that is an exact replica of the message
968 * specified, except that its refcount is set to 1, its message serial
969 * is reset to 0, and if the original message was "locked" (in the
970 * outgoing message queue and thus not modifiable) the new message
971 * will not be locked.
973 * @param message the message.
974 * @returns the new message.
977 dbus_message_copy (const DBusMessage *message)
981 _dbus_return_val_if_fail (message != NULL, NULL);
983 retval = dbus_new0 (DBusMessage, 1);
987 retval->refcount.value = 1;
988 retval->byte_order = message->byte_order;
989 retval->locked = FALSE;
990 #ifndef DBUS_DISABLE_CHECKS
991 retval->generation = message->generation;
994 if (!_dbus_header_copy (&message->header, &retval->header))
1000 if (!_dbus_string_init_preallocated (&retval->body,
1001 _dbus_string_get_length (&message->body)))
1003 _dbus_header_free (&retval->header);
1008 if (!_dbus_string_copy (&message->body, 0,
1015 _dbus_header_free (&retval->header);
1016 _dbus_string_free (&retval->body);
1024 * Increments the reference count of a DBusMessage.
1026 * @param message The message
1027 * @returns the message
1028 * @see dbus_message_unref
1031 dbus_message_ref (DBusMessage *message)
1033 dbus_int32_t old_refcount;
1035 _dbus_return_val_if_fail (message != NULL, NULL);
1036 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1038 old_refcount = _dbus_atomic_inc (&message->refcount);
1039 _dbus_assert (old_refcount >= 1);
1045 * Decrements the reference count of a DBusMessage.
1047 * @param message The message
1048 * @see dbus_message_ref
1051 dbus_message_unref (DBusMessage *message)
1053 dbus_int32_t old_refcount;
1055 _dbus_return_if_fail (message != NULL);
1056 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1058 old_refcount = _dbus_atomic_dec (&message->refcount);
1060 _dbus_assert (old_refcount >= 0);
1062 if (old_refcount == 1)
1064 /* Calls application callbacks! */
1065 dbus_message_cache_or_finalize (message);
1070 * Gets the type of a message. Types include
1071 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1072 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1073 * types are allowed and all code must silently ignore messages of
1074 * unknown type. DBUS_MESSAGE_TYPE_INVALID will never be returned,
1078 * @param message the message
1079 * @returns the type of the message
1082 dbus_message_get_type (DBusMessage *message)
1084 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1086 return _dbus_header_get_message_type (&message->header);
1090 * Appends fields to a message given a variable argument list. The
1091 * variable argument list should contain the type of each argument
1092 * followed by the value to append. Appendable types are basic types,
1093 * and arrays of fixed-length basic types. To append variable-length
1094 * basic types, or any more complex value, you have to use an iterator
1095 * rather than this function.
1097 * To append a basic type, specify its type code followed by the
1098 * value. For example:
1101 * DBUS_TYPE_INT32, 42,
1102 * DBUS_TYPE_STRING, "Hello World"
1106 * dbus_int32_t val = 42;
1107 * DBUS_TYPE_INT32, val
1110 * Be sure that your provided value is the right size. For example, this
1113 * DBUS_TYPE_INT64, 42
1115 * Because the "42" will be a 32-bit integer. You need to cast to
1118 * To append an array of fixed-length basic types, pass in the
1119 * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1120 * the array pointer, and a 32-bit integer giving the number of
1121 * elements in the array. So for example:
1123 * const dbus_int32_t array[] = { 1, 2, 3 };
1124 * const dbus_int32_t *v_ARRAY = array;
1125 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3
1128 * @warning in C, given "int array[]", "&array == array" (the
1129 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1130 * So if you're using an array instead of a pointer you have to create
1131 * a pointer variable, assign the array to it, then take the address
1132 * of the pointer variable. For strings it works to write
1133 * const char *array = "Hello" and then use &array though.
1135 * The last argument to this function must be #DBUS_TYPE_INVALID,
1136 * marking the end of the argument list.
1138 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1140 * @todo If this fails due to lack of memory, the message is hosed and
1141 * you have to start over building the whole message.
1143 * @param message the message
1144 * @param first_arg_type type of the first argument
1145 * @param ... value of first argument, list of additional type-value pairs
1146 * @returns #TRUE on success
1149 dbus_message_append_args (DBusMessage *message,
1156 _dbus_return_val_if_fail (message != NULL, FALSE);
1158 va_start (var_args, first_arg_type);
1159 retval = dbus_message_append_args_valist (message,
1168 * This function takes a va_list for use by language bindings.
1169 * It's otherwise the same as dbus_message_append_args().
1171 * @todo for now, if this function fails due to OOM it will leave
1172 * the message half-written and you have to discard the message
1175 * @see dbus_message_append_args.
1176 * @param message the message
1177 * @param first_arg_type type of first argument
1178 * @param var_args value of first argument, then list of type/value pairs
1179 * @returns #TRUE on success
1182 dbus_message_append_args_valist (DBusMessage *message,
1187 DBusMessageIter iter;
1189 _dbus_return_val_if_fail (message != NULL, FALSE);
1191 type = first_arg_type;
1193 dbus_message_iter_init_append (message, &iter);
1195 while (type != DBUS_TYPE_INVALID)
1197 if (_dbus_type_is_basic (type))
1199 const DBusBasicValue *value;
1200 value = va_arg (var_args, const DBusBasicValue*);
1202 if (!dbus_message_iter_append_basic (&iter,
1207 else if (type == DBUS_TYPE_ARRAY)
1210 const DBusBasicValue **value;
1212 DBusMessageIter array;
1215 element_type = va_arg (var_args, int);
1217 #ifndef DBUS_DISABLE_CHECKS
1218 if (!_dbus_type_is_fixed (element_type))
1220 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1221 _dbus_type_to_string (element_type),
1222 _DBUS_FUNCTION_NAME);
1227 value = va_arg (var_args, const DBusBasicValue**);
1228 n_elements = va_arg (var_args, int);
1230 buf[0] = element_type;
1232 if (!dbus_message_iter_open_container (&iter,
1238 if (!dbus_message_iter_append_fixed_array (&array,
1244 if (!dbus_message_iter_close_container (&iter, &array))
1247 #ifndef DBUS_DISABLE_CHECKS
1250 _dbus_warn ("type %s isn't supported yet in %s\n",
1251 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1256 type = va_arg (var_args, int);
1266 * Gets arguments from a message given a variable argument list. The
1267 * supported types include those supported by
1268 * dbus_message_append_args(); that is, basic types and arrays of
1269 * fixed-length basic types. The arguments are the same as they would
1270 * be for dbus_message_iter_get_basic() or
1271 * dbus_message_iter_get_fixed_array().
1273 * In addition to those types, arrays of string, object path, and
1274 * signature are supported; but these are returned as allocated memory
1275 * and must be freed with dbus_free_string_array(), while the other
1276 * types are returned as const references.
1278 * The variable argument list should contain the type of the argument
1279 * followed by a pointer to where the value should be stored. The list
1280 * is terminated with #DBUS_TYPE_INVALID.
1282 * The returned values are constant; do not free them. They point
1283 * into the #DBusMessage.
1285 * If the requested arguments are not present, or do not have the
1286 * requested types, then an error will be set.
1288 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1290 * @param message the message
1291 * @param error error to be filled in on failure
1292 * @param first_arg_type the first argument type
1293 * @param ... location for first argument value, then list of type-location pairs
1294 * @returns #FALSE if the error was set
1297 dbus_message_get_args (DBusMessage *message,
1305 _dbus_return_val_if_fail (message != NULL, FALSE);
1306 _dbus_return_val_if_error_is_set (error, FALSE);
1308 va_start (var_args, first_arg_type);
1309 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1316 * This function takes a va_list for use by language bindings. It is
1317 * otherwise the same as dbus_message_get_args().
1319 * @see dbus_message_get_args
1320 * @param message the message
1321 * @param error error to be filled in
1322 * @param first_arg_type type of the first argument
1323 * @param var_args return location for first argument, followed by list of type/location pairs
1324 * @returns #FALSE if error was set
1327 dbus_message_get_args_valist (DBusMessage *message,
1332 DBusMessageIter iter;
1334 _dbus_return_val_if_fail (message != NULL, FALSE);
1335 _dbus_return_val_if_error_is_set (error, FALSE);
1337 dbus_message_iter_init (message, &iter);
1338 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1342 _dbus_message_iter_init_common (DBusMessage *message,
1343 DBusMessageRealIter *real,
1346 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1348 real->message = message;
1349 real->changed_stamp = message->changed_stamp;
1350 real->iter_type = iter_type;
1351 real->sig_refcount = 0;
1355 * Initializes a #DBusMessageIter for reading the arguments of the
1356 * message passed in.
1358 * @param message the message
1359 * @param iter pointer to an iterator to initialize
1360 * @returns #FALSE if the message has no arguments
1363 dbus_message_iter_init (DBusMessage *message,
1364 DBusMessageIter *iter)
1366 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1367 const DBusString *type_str;
1370 _dbus_return_val_if_fail (message != NULL, FALSE);
1371 _dbus_return_val_if_fail (iter != NULL, FALSE);
1373 get_const_signature (&message->header, &type_str, &type_pos);
1375 _dbus_message_iter_init_common (message, real,
1376 DBUS_MESSAGE_ITER_TYPE_READER);
1378 _dbus_type_reader_init (&real->u.reader,
1379 message->byte_order,
1384 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1387 #ifndef DBUS_DISABLE_CHECKS
1389 _dbus_message_iter_check (DBusMessageRealIter *iter)
1393 _dbus_warn ("dbus message iterator is NULL\n");
1397 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
1399 if (iter->u.reader.byte_order != iter->message->byte_order)
1401 _dbus_warn ("dbus message changed byte order since iterator was created\n");
1405 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
1407 if (iter->u.writer.byte_order != iter->message->byte_order)
1409 _dbus_warn ("dbus message changed byte order since append iterator was created\n");
1415 _dbus_warn ("dbus message iterator looks uninitialized or corrupted\n");
1419 if (iter->changed_stamp != iter->message->changed_stamp)
1421 _dbus_warn ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
1427 #endif /* DBUS_DISABLE_CHECKS */
1430 * Checks if an iterator has any more fields.
1432 * @param iter the message iter
1433 * @returns #TRUE if there are more fields
1437 dbus_message_iter_has_next (DBusMessageIter *iter)
1439 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1441 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1442 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1444 return _dbus_type_reader_has_next (&real->u.reader);
1448 * Moves the iterator to the next field, if any. If there's no next
1449 * field, returns #FALSE. If the iterator moves forward, returns
1452 * @param iter the message iter
1453 * @returns #TRUE if the iterator was moved to the next field
1456 dbus_message_iter_next (DBusMessageIter *iter)
1458 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1460 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1461 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1463 return _dbus_type_reader_next (&real->u.reader);
1467 * Returns the argument type of the argument that the message iterator
1468 * points to. If the iterator is at the end of the message, returns
1469 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1472 * dbus_message_iter_init (&iter);
1473 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1474 * dbus_message_iter_next (&iter);
1477 * @param iter the message iter
1478 * @returns the argument type
1481 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1483 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1485 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1486 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1488 return _dbus_type_reader_get_current_type (&real->u.reader);
1492 * Returns the element type of the array that the message iterator
1493 * points to. Note that you need to check that the iterator points to
1494 * an array prior to using this function.
1496 * @param iter the message iter
1497 * @returns the array element type
1500 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1502 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1504 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1505 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1506 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1508 return _dbus_type_reader_get_element_type (&real->u.reader);
1512 * Recurses into a container value when reading values from a message,
1513 * initializing a sub-iterator to use for traversing the child values
1516 * Note that this recurses into a value, not a type, so you can only
1517 * recurse if the value exists. The main implication of this is that
1518 * if you have for example an empty array of array of int32, you can
1519 * recurse into the outermost array, but it will have no values, so
1520 * you won't be able to recurse further. There's no array of int32 to
1523 * @param iter the message iterator
1524 * @param sub the sub-iterator to initialize
1527 dbus_message_iter_recurse (DBusMessageIter *iter,
1528 DBusMessageIter *sub)
1530 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1531 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1533 _dbus_return_if_fail (_dbus_message_iter_check (real));
1534 _dbus_return_if_fail (sub != NULL);
1537 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1541 * Reads a basic-typed value from the message iterator.
1542 * Basic types are the non-containers such as integer and string.
1544 * The value argument should be the address of a location to store
1545 * the returned value. So for int32 it should be a "dbus_int32_t*"
1546 * and for string a "const char**". The returned value is
1547 * by reference and should not be freed.
1549 * All returned values are guaranteed to fit in 8 bytes. So you can
1550 * write code like this:
1553 * #ifdef DBUS_HAVE_INT64
1554 * dbus_uint64_t value;
1556 * dbus_message_iter_get_basic (&read_iter, &value);
1557 * type = dbus_message_iter_get_arg_type (&read_iter);
1558 * dbus_message_iter_append_basic (&write_iter, type, &value);
1562 * To avoid the #DBUS_HAVE_INT64 conditional, create a struct or
1563 * something that occupies at least 8 bytes, e.g. you could use a
1564 * struct with two int32 values in it. dbus_uint64_t is just one
1565 * example of a type that's large enough to hold any possible value.
1567 * Be sure you have somehow checked that
1568 * dbus_message_iter_get_arg_type() matches the type you are
1569 * expecting, or you'll crash when you try to use an integer as a
1570 * string or something.
1572 * @param iter the iterator
1573 * @param value location to store the value
1576 dbus_message_iter_get_basic (DBusMessageIter *iter,
1579 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1581 _dbus_return_if_fail (_dbus_message_iter_check (real));
1582 _dbus_return_if_fail (value != NULL);
1584 _dbus_type_reader_read_basic (&real->u.reader,
1589 * Reads a block of fixed-length values from the message iterator.
1590 * Fixed-length values are those basic types that are not string-like,
1591 * such as integers, bool, double. The block read will be from the
1592 * current position in the array until the end of the array.
1594 * The value argument should be the address of a location to store the
1595 * returned array. So for int32 it should be a "const dbus_int32_t**"
1596 * The returned value is by reference and should not be freed.
1598 * @param iter the iterator
1599 * @param value location to store the block
1600 * @param n_elements number of elements in the block
1603 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
1607 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1609 _dbus_return_if_fail (_dbus_message_iter_check (real));
1610 _dbus_return_if_fail (value != NULL);
1611 _dbus_return_if_fail (_dbus_type_is_fixed (_dbus_type_reader_get_element_type (&real->u.reader)));
1613 _dbus_type_reader_read_fixed_multi (&real->u.reader,
1618 * This function takes a va_list for use by language bindings and is
1619 * otherwise the same as dbus_message_iter_get_args().
1620 * dbus_message_get_args() is the place to go for complete
1623 * @see dbus_message_get_args
1624 * @param iter the message iter
1625 * @param error error to be filled in
1626 * @param first_arg_type type of the first argument
1627 * @param var_args return location for first argument, followed by list of type/location pairs
1628 * @returns #FALSE if error was set
1631 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1636 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1637 int spec_type, msg_type, i;
1640 _dbus_assert (_dbus_message_iter_check (real));
1644 spec_type = first_arg_type;
1647 while (spec_type != DBUS_TYPE_INVALID)
1649 msg_type = dbus_message_iter_get_arg_type (iter);
1651 if (msg_type != spec_type)
1653 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1654 "Argument %d is specified to be of type \"%s\", but "
1655 "is actually of type \"%s\"\n", i,
1656 _dbus_type_to_string (spec_type),
1657 _dbus_type_to_string (msg_type));
1662 if (_dbus_type_is_basic (spec_type))
1664 DBusBasicValue *ptr;
1666 ptr = va_arg (var_args, DBusBasicValue*);
1668 _dbus_assert (ptr != NULL);
1670 _dbus_type_reader_read_basic (&real->u.reader,
1673 else if (spec_type == DBUS_TYPE_ARRAY)
1676 int spec_element_type;
1677 const DBusBasicValue **ptr;
1679 DBusTypeReader array;
1681 spec_element_type = va_arg (var_args, int);
1682 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1684 if (spec_element_type != element_type)
1686 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1687 "Argument %d is specified to be an array of \"%s\", but "
1688 "is actually an array of \"%s\"\n",
1690 _dbus_type_to_string (spec_element_type),
1691 _dbus_type_to_string (element_type));
1696 if (_dbus_type_is_fixed (spec_element_type))
1698 ptr = va_arg (var_args, const DBusBasicValue**);
1699 n_elements_p = va_arg (var_args, int*);
1701 _dbus_assert (ptr != NULL);
1702 _dbus_assert (n_elements_p != NULL);
1704 _dbus_type_reader_recurse (&real->u.reader, &array);
1706 _dbus_type_reader_read_fixed_multi (&array,
1709 else if (spec_element_type == DBUS_TYPE_STRING ||
1710 spec_element_type == DBUS_TYPE_SIGNATURE ||
1711 spec_element_type == DBUS_TYPE_OBJECT_PATH)
1713 char ***str_array_p;
1717 str_array_p = va_arg (var_args, char***);
1718 n_elements_p = va_arg (var_args, int*);
1720 _dbus_assert (str_array_p != NULL);
1721 _dbus_assert (n_elements_p != NULL);
1723 /* Count elements in the array */
1724 _dbus_type_reader_recurse (&real->u.reader, &array);
1727 if (_dbus_type_reader_has_next (&array))
1729 while (_dbus_type_reader_next (&array))
1733 str_array = dbus_new0 (char*, i + 1);
1734 if (str_array == NULL)
1736 _DBUS_SET_OOM (error);
1740 /* Now go through and dup each string */
1741 _dbus_type_reader_recurse (&real->u.reader, &array);
1744 if (_dbus_type_reader_has_next (&array))
1749 _dbus_type_reader_read_basic (&array,
1752 str_array[i] = _dbus_strdup (s);
1753 if (str_array[i] == NULL)
1755 dbus_free_string_array (str_array);
1756 _DBUS_SET_OOM (error);
1762 while (_dbus_type_reader_next (&array));
1765 *str_array_p = str_array;
1768 #ifndef DBUS_DISABLE_CHECKS
1771 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
1772 _DBUS_FUNCTION_NAME);
1777 #ifndef DBUS_DISABLE_CHECKS
1780 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
1781 _DBUS_FUNCTION_NAME);
1786 spec_type = va_arg (var_args, int);
1787 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1789 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1790 "Message has only %d arguments, but more were expected", i);
1805 * Initializes a #DBusMessageIter for appending arguments to the end
1808 * @todo If appending any of the arguments fails due to lack of
1809 * memory, generally the message is hosed and you have to start over
1810 * building the whole message.
1812 * @param message the message
1813 * @param iter pointer to an iterator to initialize
1816 dbus_message_iter_init_append (DBusMessage *message,
1817 DBusMessageIter *iter)
1819 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1821 _dbus_return_if_fail (message != NULL);
1822 _dbus_return_if_fail (iter != NULL);
1824 _dbus_message_iter_init_common (message, real,
1825 DBUS_MESSAGE_ITER_TYPE_WRITER);
1827 /* We create the signature string and point iterators at it "on demand"
1828 * when a value is actually appended. That means that init() never fails
1831 _dbus_type_writer_init_types_delayed (&real->u.writer,
1832 message->byte_order,
1834 _dbus_string_get_length (&message->body));
1838 * Creates a temporary signature string containing the current
1839 * signature, stores it in the iterator, and points the iterator to
1840 * the end of it. Used any time we write to the message.
1842 * @param real an iterator without a type_str
1843 * @returns #FALSE if no memory
1846 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
1849 const DBusString *current_sig;
1850 int current_sig_pos;
1852 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1854 if (real->u.writer.type_str != NULL)
1856 _dbus_assert (real->sig_refcount > 0);
1857 real->sig_refcount += 1;
1861 str = dbus_new (DBusString, 1);
1865 if (!_dbus_header_get_field_raw (&real->message->header,
1866 DBUS_HEADER_FIELD_SIGNATURE,
1867 ¤t_sig, ¤t_sig_pos))
1874 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
1875 current_sig_pos += 1; /* move on to sig data */
1877 if (!_dbus_string_init_preallocated (str, current_len + 4))
1883 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
1886 _dbus_string_free (str);
1893 if (!_dbus_string_init_preallocated (str, 4))
1900 real->sig_refcount = 1;
1902 _dbus_type_writer_add_types (&real->u.writer,
1903 str, _dbus_string_get_length (str));
1908 * Sets the new signature as the message signature, frees the
1909 * signature string, and marks the iterator as not having a type_str
1910 * anymore. Frees the signature even if it fails, so you can't
1911 * really recover from failure. Kinda busted.
1913 * @param real an iterator without a type_str
1914 * @returns #FALSE if no memory
1917 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
1920 const char *v_STRING;
1923 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1924 _dbus_assert (real->u.writer.type_str != NULL);
1925 _dbus_assert (real->sig_refcount > 0);
1927 real->sig_refcount -= 1;
1929 if (real->sig_refcount > 0)
1931 _dbus_assert (real->sig_refcount == 0);
1935 str = real->u.writer.type_str;
1937 v_STRING = _dbus_string_get_const_data (str);
1938 if (!_dbus_header_set_field_basic (&real->message->header,
1939 DBUS_HEADER_FIELD_SIGNATURE,
1940 DBUS_TYPE_SIGNATURE,
1944 _dbus_type_writer_remove_types (&real->u.writer);
1945 _dbus_string_free (str);
1951 #ifndef DBUS_DISABLE_CHECKS
1953 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
1955 if (!_dbus_message_iter_check (iter))
1958 if (iter->message->locked)
1960 _dbus_warn ("dbus append iterator can't be used: message is locked (has already been sent)\n");
1966 #endif /* DBUS_DISABLE_CHECKS */
1969 * Appends a basic-typed value to the message. The basic types are the
1970 * non-container types such as integer and string.
1972 * The "value" argument should be the address of a basic-typed value.
1973 * So for string, const char**. For integer, dbus_int32_t*.
1975 * @todo If this fails due to lack of memory, the message is hosed and
1976 * you have to start over building the whole message.
1978 * @param iter the append iterator
1979 * @param type the type of the value
1980 * @param value the address of the value
1981 * @returns #FALSE if not enough memory
1984 dbus_message_iter_append_basic (DBusMessageIter *iter,
1988 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1991 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
1992 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
1993 _dbus_return_val_if_fail (_dbus_type_is_basic (type), FALSE);
1994 _dbus_return_val_if_fail (value != NULL, FALSE);
1996 if (!_dbus_message_iter_open_signature (real))
1999 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2001 if (!_dbus_message_iter_close_signature (real))
2008 * Appends a block of fixed-length values to an array. The
2009 * fixed-length types are all basic types that are not string-like. So
2010 * int32, double, bool, etc. You must call
2011 * dbus_message_iter_open_container() to open an array of values
2012 * before calling this function. You may call this function multiple
2013 * times (and intermixed with calls to
2014 * dbus_message_iter_append_basic()) for the same array.
2016 * The "value" argument should be the address of the array. So for
2017 * integer, "dbus_int32_t**" is expected for example.
2019 * @warning in C, given "int array[]", "&array == array" (the
2020 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2021 * So if you're using an array instead of a pointer you have to create
2022 * a pointer variable, assign the array to it, then take the address
2023 * of the pointer variable.
2025 * const dbus_int32_t array[] = { 1, 2, 3 };
2026 * const dbus_int32_t *v_ARRAY = array;
2027 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2028 * fprintf (stderr, "No memory!\n");
2030 * For strings it works to write const char *array = "Hello" and then
2031 * use &array though.
2033 * @todo If this fails due to lack of memory, the message is hosed and
2034 * you have to start over building the whole message.
2036 * @param iter the append iterator
2037 * @param element_type the type of the array elements
2038 * @param value the address of the array
2039 * @param n_elements the number of elements to append
2040 * @returns #FALSE if not enough memory
2043 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2048 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2051 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2052 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2053 _dbus_return_val_if_fail (_dbus_type_is_fixed (element_type), FALSE);
2054 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2055 _dbus_return_val_if_fail (value != NULL, FALSE);
2056 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2057 _dbus_return_val_if_fail (n_elements <=
2058 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2061 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2067 * Appends a container-typed value to the message; you are required to
2068 * append the contents of the container using the returned
2069 * sub-iterator, and then call
2070 * dbus_message_iter_close_container(). Container types are for
2071 * example struct, variant, and array. For variants, the
2072 * contained_signature should be the type of the single value inside
2073 * the variant. For structs, contained_signature should be #NULL; it
2074 * will be set to whatever types you write into the struct. For
2075 * arrays, contained_signature should be the type of the array
2078 * @todo If this fails due to lack of memory, the message is hosed and
2079 * you have to start over building the whole message.
2081 * @param iter the append iterator
2082 * @param type the type of the value
2083 * @param contained_signature the type of container contents
2084 * @param sub sub-iterator to initialize
2085 * @returns #FALSE if not enough memory
2088 dbus_message_iter_open_container (DBusMessageIter *iter,
2090 const char *contained_signature,
2091 DBusMessageIter *sub)
2093 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2094 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2095 DBusString contained_str;
2097 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2098 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2099 _dbus_return_val_if_fail (_dbus_type_is_container (type), FALSE);
2100 _dbus_return_val_if_fail (sub != NULL, FALSE);
2101 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2102 contained_signature == NULL) ||
2103 contained_signature != NULL, FALSE);
2105 if (!_dbus_message_iter_open_signature (real))
2108 _dbus_string_init_const (&contained_str, contained_signature);
2111 return _dbus_type_writer_recurse (&real->u.writer,
2114 &real_sub->u.writer);
2119 * Closes a container-typed value appended to the message; may write
2120 * out more information to the message known only after the entire
2121 * container is written, and may free resources created by
2122 * dbus_message_iter_open_container().
2124 * @todo If this fails due to lack of memory, the message is hosed and
2125 * you have to start over building the whole message.
2127 * @param iter the append iterator
2128 * @param sub sub-iterator to close
2129 * @returns #FALSE if not enough memory
2132 dbus_message_iter_close_container (DBusMessageIter *iter,
2133 DBusMessageIter *sub)
2135 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2136 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2139 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2140 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2141 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2142 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2144 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2145 &real_sub->u.writer);
2147 if (!_dbus_message_iter_close_signature (real))
2154 * Sets a flag indicating that the message does not want a reply; if
2155 * this flag is set, the other end of the connection may (but is not
2156 * required to) optimize by not sending method return or error
2157 * replies. If this flag is set, there is no way to know whether the
2158 * message successfully arrived at the remote end. Normally you know a
2159 * message was received when you receive the reply to it.
2161 * @param message the message
2162 * @param no_reply #TRUE if no reply is desired
2165 dbus_message_set_no_reply (DBusMessage *message,
2166 dbus_bool_t no_reply)
2168 _dbus_return_if_fail (message != NULL);
2169 _dbus_return_if_fail (!message->locked);
2171 _dbus_header_toggle_flag (&message->header,
2172 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2177 * Returns #TRUE if the message does not expect
2180 * @param message the message
2181 * @returns #TRUE if the message sender isn't waiting for a reply
2184 dbus_message_get_no_reply (DBusMessage *message)
2186 _dbus_return_val_if_fail (message != NULL, FALSE);
2188 return _dbus_header_get_flag (&message->header,
2189 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2193 * Sets a flag indicating that an owner for the destination name will
2194 * be automatically started before the message is delivered. When this
2195 * flag is set, the message is held until a name owner finishes
2196 * starting up, or fails to start up. In case of failure, the reply
2199 * @param message the message
2200 * @param auto_start #TRUE if auto-starting is desired
2203 dbus_message_set_auto_start (DBusMessage *message,
2204 dbus_bool_t auto_start)
2206 _dbus_return_if_fail (message != NULL);
2207 _dbus_return_if_fail (!message->locked);
2209 _dbus_header_toggle_flag (&message->header,
2210 DBUS_HEADER_FLAG_NO_AUTO_START,
2215 * Returns #TRUE if the message will cause an owner for
2216 * destination name to be auto-started.
2218 * @param message the message
2219 * @returns #TRUE if the message will use auto-start
2222 dbus_message_get_auto_start (DBusMessage *message)
2224 _dbus_return_val_if_fail (message != NULL, FALSE);
2226 return !_dbus_header_get_flag (&message->header,
2227 DBUS_HEADER_FLAG_NO_AUTO_START);
2232 * Sets the object path this message is being sent to (for
2233 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2234 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2236 * @param message the message
2237 * @param object_path the path or #NULL to unset
2238 * @returns #FALSE if not enough memory
2241 dbus_message_set_path (DBusMessage *message,
2242 const char *object_path)
2244 _dbus_return_val_if_fail (message != NULL, FALSE);
2245 _dbus_return_val_if_fail (!message->locked, FALSE);
2246 _dbus_return_val_if_fail (object_path == NULL ||
2247 _dbus_check_is_valid_path (object_path),
2250 return set_or_delete_string_field (message,
2251 DBUS_HEADER_FIELD_PATH,
2252 DBUS_TYPE_OBJECT_PATH,
2257 * Gets the object path this message is being sent to (for
2258 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2259 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2261 * @param message the message
2262 * @returns the path (should not be freed) or #NULL
2265 dbus_message_get_path (DBusMessage *message)
2269 _dbus_return_val_if_fail (message != NULL, NULL);
2271 v = NULL; /* in case field doesn't exist */
2272 _dbus_header_get_field_basic (&message->header,
2273 DBUS_HEADER_FIELD_PATH,
2274 DBUS_TYPE_OBJECT_PATH,
2280 * Gets the object path this message is being sent to
2281 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2282 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2283 * format (one array element per path component).
2284 * Free the returned array with dbus_free_string_array().
2286 * An empty but non-NULL path array means the path "/".
2287 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2288 * and the path "/" becomes { NULL }.
2290 * @todo this could be optimized by using the len from the message
2291 * instead of calling strlen() again
2293 * @param message the message
2294 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2295 * @returns #FALSE if no memory to allocate the array
2298 dbus_message_get_path_decomposed (DBusMessage *message,
2303 _dbus_return_val_if_fail (message != NULL, FALSE);
2304 _dbus_return_val_if_fail (path != NULL, FALSE);
2308 v = dbus_message_get_path (message);
2311 if (!_dbus_decompose_path (v, strlen (v),
2319 * Sets the interface this message is being sent to
2320 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2321 * the interface a signal is being emitted from
2322 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2324 * @param message the message
2325 * @param interface the interface or #NULL to unset
2326 * @returns #FALSE if not enough memory
2329 dbus_message_set_interface (DBusMessage *message,
2330 const char *interface)
2332 _dbus_return_val_if_fail (message != NULL, FALSE);
2333 _dbus_return_val_if_fail (!message->locked, FALSE);
2334 _dbus_return_val_if_fail (interface == NULL ||
2335 _dbus_check_is_valid_interface (interface),
2338 return set_or_delete_string_field (message,
2339 DBUS_HEADER_FIELD_INTERFACE,
2345 * Gets the interface this message is being sent to
2346 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2347 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2348 * The interface name is fully-qualified (namespaced).
2349 * Returns #NULL if none.
2351 * @param message the message
2352 * @returns the message interface (should not be freed) or #NULL
2355 dbus_message_get_interface (DBusMessage *message)
2359 _dbus_return_val_if_fail (message != NULL, NULL);
2361 v = NULL; /* in case field doesn't exist */
2362 _dbus_header_get_field_basic (&message->header,
2363 DBUS_HEADER_FIELD_INTERFACE,
2370 * Sets the interface member being invoked
2371 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2372 * (DBUS_MESSAGE_TYPE_SIGNAL).
2373 * The interface name is fully-qualified (namespaced).
2375 * @param message the message
2376 * @param member the member or #NULL to unset
2377 * @returns #FALSE if not enough memory
2380 dbus_message_set_member (DBusMessage *message,
2383 _dbus_return_val_if_fail (message != NULL, FALSE);
2384 _dbus_return_val_if_fail (!message->locked, FALSE);
2385 _dbus_return_val_if_fail (member == NULL ||
2386 _dbus_check_is_valid_member (member),
2389 return set_or_delete_string_field (message,
2390 DBUS_HEADER_FIELD_MEMBER,
2396 * Gets the interface member being invoked
2397 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2398 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2400 * @param message the message
2401 * @returns the member name (should not be freed) or #NULL
2404 dbus_message_get_member (DBusMessage *message)
2408 _dbus_return_val_if_fail (message != NULL, NULL);
2410 v = NULL; /* in case field doesn't exist */
2411 _dbus_header_get_field_basic (&message->header,
2412 DBUS_HEADER_FIELD_MEMBER,
2419 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2420 * The name is fully-qualified (namespaced).
2422 * @param message the message
2423 * @param error_name the name or #NULL to unset
2424 * @returns #FALSE if not enough memory
2427 dbus_message_set_error_name (DBusMessage *message,
2428 const char *error_name)
2430 _dbus_return_val_if_fail (message != NULL, FALSE);
2431 _dbus_return_val_if_fail (!message->locked, FALSE);
2432 _dbus_return_val_if_fail (error_name == NULL ||
2433 _dbus_check_is_valid_error_name (error_name),
2436 return set_or_delete_string_field (message,
2437 DBUS_HEADER_FIELD_ERROR_NAME,
2443 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2446 * @param message the message
2447 * @returns the error name (should not be freed) or #NULL
2450 dbus_message_get_error_name (DBusMessage *message)
2454 _dbus_return_val_if_fail (message != NULL, NULL);
2456 v = NULL; /* in case field doesn't exist */
2457 _dbus_header_get_field_basic (&message->header,
2458 DBUS_HEADER_FIELD_ERROR_NAME,
2465 * Sets the message's destination. The destination is the name of
2466 * another connection on the bus and may be either the unique name
2467 * assigned by the bus to each connection, or a well-known name
2468 * specified in advance.
2470 * @param message the message
2471 * @param destination the destination name or #NULL to unset
2472 * @returns #FALSE if not enough memory
2475 dbus_message_set_destination (DBusMessage *message,
2476 const char *destination)
2478 _dbus_return_val_if_fail (message != NULL, FALSE);
2479 _dbus_return_val_if_fail (!message->locked, FALSE);
2480 _dbus_return_val_if_fail (destination == NULL ||
2481 _dbus_check_is_valid_bus_name (destination),
2484 return set_or_delete_string_field (message,
2485 DBUS_HEADER_FIELD_DESTINATION,
2491 * Gets the destination of a message or #NULL if there is none set.
2493 * @param message the message
2494 * @returns the message destination (should not be freed) or #NULL
2497 dbus_message_get_destination (DBusMessage *message)
2501 _dbus_return_val_if_fail (message != NULL, NULL);
2503 v = NULL; /* in case field doesn't exist */
2504 _dbus_header_get_field_basic (&message->header,
2505 DBUS_HEADER_FIELD_DESTINATION,
2512 * Sets the message sender.
2514 * @param message the message
2515 * @param sender the sender or #NULL to unset
2516 * @returns #FALSE if not enough memory
2519 dbus_message_set_sender (DBusMessage *message,
2522 _dbus_return_val_if_fail (message != NULL, FALSE);
2523 _dbus_return_val_if_fail (!message->locked, FALSE);
2524 _dbus_return_val_if_fail (sender == NULL ||
2525 _dbus_check_is_valid_bus_name (sender),
2528 return set_or_delete_string_field (message,
2529 DBUS_HEADER_FIELD_SENDER,
2535 * Gets the unique name of the connection which originated this
2536 * message, or #NULL if unknown or inapplicable. The sender is filled
2537 * in by the message bus.
2539 * @param message the message
2540 * @returns the unique name of the sender or #NULL
2543 dbus_message_get_sender (DBusMessage *message)
2547 _dbus_return_val_if_fail (message != NULL, NULL);
2549 v = NULL; /* in case field doesn't exist */
2550 _dbus_header_get_field_basic (&message->header,
2551 DBUS_HEADER_FIELD_SENDER,
2558 * Gets the type signature of the message, i.e. the arguments in the
2559 * message payload. The signature includes only "in" arguments for
2560 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2561 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2562 * what you might expect (it does not include the signature of the
2563 * entire C++-style method).
2565 * The signature is a string made up of type codes such as
2566 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2567 * the value of #DBUS_TYPE_INVALID).
2569 * @param message the message
2570 * @returns the type signature
2573 dbus_message_get_signature (DBusMessage *message)
2575 const DBusString *type_str;
2578 _dbus_return_val_if_fail (message != NULL, NULL);
2580 get_const_signature (&message->header, &type_str, &type_pos);
2582 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
2586 _dbus_message_has_type_interface_member (DBusMessage *message,
2588 const char *interface,
2593 _dbus_assert (message != NULL);
2594 _dbus_assert (interface != NULL);
2595 _dbus_assert (member != NULL);
2597 if (dbus_message_get_type (message) != type)
2600 /* Optimize by checking the short member name first
2601 * instead of the longer interface name
2604 n = dbus_message_get_member (message);
2606 if (n && strcmp (n, member) == 0)
2608 n = dbus_message_get_interface (message);
2610 if (n == NULL || strcmp (n, interface) == 0)
2618 * Checks whether the message is a method call with the given
2619 * interface and member fields. If the message is not
2620 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
2621 * member field, returns #FALSE. If the interface field is missing,
2622 * then it will be assumed equal to the provided interface. The D-BUS
2623 * protocol allows method callers to leave out the interface name.
2625 * @param message the message
2626 * @param interface the name to check (must not be #NULL)
2627 * @param method the name to check (must not be #NULL)
2629 * @returns #TRUE if the message is the specified method call
2632 dbus_message_is_method_call (DBusMessage *message,
2633 const char *interface,
2636 _dbus_return_val_if_fail (message != NULL, FALSE);
2637 _dbus_return_val_if_fail (interface != NULL, FALSE);
2638 _dbus_return_val_if_fail (method != NULL, FALSE);
2639 /* don't check that interface/method are valid since it would be
2640 * expensive, and not catch many common errors
2643 return _dbus_message_has_type_interface_member (message,
2644 DBUS_MESSAGE_TYPE_METHOD_CALL,
2649 * Checks whether the message is a signal with the given interface and
2650 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
2651 * has a different interface or member field, returns #FALSE. If the
2652 * interface field in the message is missing, it is assumed to match
2653 * any interface you pass in to this function.
2655 * @param message the message
2656 * @param interface the name to check (must not be #NULL)
2657 * @param signal_name the name to check (must not be #NULL)
2659 * @returns #TRUE if the message is the specified signal
2662 dbus_message_is_signal (DBusMessage *message,
2663 const char *interface,
2664 const char *signal_name)
2666 _dbus_return_val_if_fail (message != NULL, FALSE);
2667 _dbus_return_val_if_fail (interface != NULL, FALSE);
2668 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
2669 /* don't check that interface/name are valid since it would be
2670 * expensive, and not catch many common errors
2673 return _dbus_message_has_type_interface_member (message,
2674 DBUS_MESSAGE_TYPE_SIGNAL,
2675 interface, signal_name);
2679 * Checks whether the message is an error reply with the given error
2680 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
2681 * different name, returns #FALSE.
2683 * @param message the message
2684 * @param error_name the name to check (must not be #NULL)
2686 * @returns #TRUE if the message is the specified error
2689 dbus_message_is_error (DBusMessage *message,
2690 const char *error_name)
2694 _dbus_return_val_if_fail (message != NULL, FALSE);
2695 _dbus_return_val_if_fail (error_name != NULL, FALSE);
2696 /* don't check that error_name is valid since it would be expensive,
2697 * and not catch many common errors
2700 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2703 n = dbus_message_get_error_name (message);
2705 if (n && strcmp (n, error_name) == 0)
2712 * Checks whether the message was sent to the given name. If the
2713 * message has no destination specified or has a different
2714 * destination, returns #FALSE.
2716 * @param message the message
2717 * @param name the name to check (must not be #NULL)
2719 * @returns #TRUE if the message has the given destination name
2722 dbus_message_has_destination (DBusMessage *message,
2727 _dbus_return_val_if_fail (message != NULL, FALSE);
2728 _dbus_return_val_if_fail (name != NULL, FALSE);
2729 /* don't check that name is valid since it would be expensive, and
2730 * not catch many common errors
2733 s = dbus_message_get_destination (message);
2735 if (s && strcmp (s, name) == 0)
2742 * Checks whether the message has the given unique name as its sender.
2743 * If the message has no sender specified or has a different sender,
2744 * returns #FALSE. Note that a peer application will always have the
2745 * unique name of the connection as the sender. So you can't use this
2746 * function to see whether a sender owned a well-known name.
2748 * Messages from the bus itself will have #DBUS_SERVICE_ORG_FREEDESKTOP_DBUS
2751 * @param message the message
2752 * @param name the name to check (must not be #NULL)
2754 * @returns #TRUE if the message has the given sender
2757 dbus_message_has_sender (DBusMessage *message,
2762 _dbus_return_val_if_fail (message != NULL, FALSE);
2763 _dbus_return_val_if_fail (name != NULL, FALSE);
2764 /* don't check that name is valid since it would be expensive, and
2765 * not catch many common errors
2768 s = dbus_message_get_sender (message);
2770 if (s && strcmp (s, name) == 0)
2777 * Checks whether the message has the given signature; see
2778 * dbus_message_get_signature() for more details on what the signature
2781 * @param message the message
2782 * @param signature typecode array
2783 * @returns #TRUE if message has the given signature
2786 dbus_message_has_signature (DBusMessage *message,
2787 const char *signature)
2791 _dbus_return_val_if_fail (message != NULL, FALSE);
2792 _dbus_return_val_if_fail (signature != NULL, FALSE);
2793 /* don't check that signature is valid since it would be expensive,
2794 * and not catch many common errors
2797 s = dbus_message_get_signature (message);
2799 if (s && strcmp (s, signature) == 0)
2806 * Sets a #DBusError based on the contents of the given
2807 * message. The error is only set if the message
2808 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
2809 * The name of the error is set to the name of the message,
2810 * and the error message is set to the first argument
2811 * if the argument exists and is a string.
2813 * The return value indicates whether the error was set (the error is
2814 * set if and only if the message is an error message). So you can
2815 * check for an error reply and convert it to DBusError in one go:
2817 * if (dbus_set_error_from_message (error, reply))
2823 * @param error the error to set
2824 * @param message the message to set it from
2825 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
2828 dbus_set_error_from_message (DBusError *error,
2829 DBusMessage *message)
2833 _dbus_return_val_if_fail (message != NULL, FALSE);
2834 _dbus_return_val_if_error_is_set (error, FALSE);
2836 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2840 dbus_message_get_args (message, NULL,
2841 DBUS_TYPE_STRING, &str,
2844 dbus_set_error (error, dbus_message_get_error_name (message),
2845 str ? "%s" : NULL, str);
2853 * @addtogroup DBusMessageInternals
2859 * The initial buffer size of the message loader.
2861 * @todo this should be based on min header size plus some average
2862 * body size, or something. Or rather, the min header size only, if we
2863 * want to try to read only the header, store that in a DBusMessage,
2864 * then read only the body and store that, etc., depends on
2865 * how we optimize _dbus_message_loader_get_buffer() and what
2866 * the exact message format is.
2868 #define INITIAL_LOADER_DATA_LEN 32
2871 * Creates a new message loader. Returns #NULL if memory can't
2874 * @returns new loader, or #NULL.
2877 _dbus_message_loader_new (void)
2879 DBusMessageLoader *loader;
2881 loader = dbus_new0 (DBusMessageLoader, 1);
2885 loader->refcount = 1;
2887 loader->corrupted = FALSE;
2888 loader->corruption_reason = DBUS_VALID;
2890 /* Try to cap message size at something that won't *totally* hose
2891 * the system if we have a couple of them.
2893 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2895 if (!_dbus_string_init (&loader->data))
2901 /* preallocate the buffer for speed, ignore failure */
2902 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2903 _dbus_string_set_length (&loader->data, 0);
2909 * Increments the reference count of the loader.
2911 * @param loader the loader.
2912 * @returns the loader
2915 _dbus_message_loader_ref (DBusMessageLoader *loader)
2917 loader->refcount += 1;
2923 * Decrements the reference count of the loader and finalizes the
2924 * loader when the count reaches zero.
2926 * @param loader the loader.
2929 _dbus_message_loader_unref (DBusMessageLoader *loader)
2931 loader->refcount -= 1;
2932 if (loader->refcount == 0)
2934 _dbus_list_foreach (&loader->messages,
2935 (DBusForeachFunction) dbus_message_unref,
2937 _dbus_list_clear (&loader->messages);
2938 _dbus_string_free (&loader->data);
2944 * Gets the buffer to use for reading data from the network. Network
2945 * data is read directly into an allocated buffer, which is then used
2946 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2947 * The buffer must always be returned immediately using
2948 * _dbus_message_loader_return_buffer(), even if no bytes are
2949 * successfully read.
2951 * @todo this function can be a lot more clever. For example
2952 * it can probably always return a buffer size to read exactly
2953 * the body of the next message, thus avoiding any memory wastage
2956 * @todo we need to enforce a max length on strings in header fields.
2958 * @param loader the message loader.
2959 * @param buffer the buffer
2962 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2963 DBusString **buffer)
2965 _dbus_assert (!loader->buffer_outstanding);
2967 *buffer = &loader->data;
2969 loader->buffer_outstanding = TRUE;
2973 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2974 * indicating to the loader how many bytes of the buffer were filled
2975 * in. This function must always be called, even if no bytes were
2976 * successfully read.
2978 * @param loader the loader.
2979 * @param buffer the buffer.
2980 * @param bytes_read number of bytes that were read into the buffer.
2983 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2987 _dbus_assert (loader->buffer_outstanding);
2988 _dbus_assert (buffer == &loader->data);
2990 loader->buffer_outstanding = FALSE;
2994 * FIXME when we move the header out of the buffer, that memmoves all
2995 * buffered messages. Kind of crappy.
2997 * Also we copy the header and body, which is kind of crappy. To
2998 * avoid this, we have to allow header and body to be in a single
2999 * memory block, which is good for messages we read and bad for
3000 * messages we are creating. But we could move_len() the buffer into
3001 * this single memory block, and move_len() will just swap the buffers
3002 * if you're moving the entire buffer replacing the dest string.
3004 * We could also have the message loader tell the transport how many
3005 * bytes to read; so it would first ask for some arbitrary number like
3006 * 256, then if the message was incomplete it would use the
3007 * header/body len to ask for exactly the size of the message (or
3008 * blocks the size of a typical kernel buffer for the socket). That
3009 * way we don't get trailing bytes in the buffer that have to be
3010 * memmoved. Though I suppose we also don't have a chance of reading a
3011 * bunch of small messages at once, so the optimization may be stupid.
3013 * Another approach would be to keep a "start" index into
3014 * loader->data and only delete it occasionally, instead of after
3015 * each message is loaded.
3017 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3020 load_message (DBusMessageLoader *loader,
3021 DBusMessage *message,
3023 int fields_array_len,
3028 DBusValidity validity;
3029 const DBusString *type_str;
3031 DBusValidationMode mode;
3033 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3038 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3041 /* 1. VALIDATE AND COPY OVER HEADER */
3042 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3043 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3045 if (!_dbus_header_load (&message->header,
3053 _dbus_string_get_length (&loader->data)))
3055 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3056 if (validity == DBUS_VALID)
3060 loader->corrupted = TRUE;
3061 loader->corruption_reason = validity;
3066 _dbus_assert (validity == DBUS_VALID);
3068 message->byte_order = byte_order;
3070 /* 2. VALIDATE BODY */
3071 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3073 get_const_signature (&message->header, &type_str, &type_pos);
3075 /* Because the bytes_remaining arg is NULL, this validates that the
3076 * body is the right length
3078 validity = _dbus_validate_body_with_reason (type_str,
3085 if (validity != DBUS_VALID)
3087 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3089 loader->corrupted = TRUE;
3090 loader->corruption_reason = validity;
3096 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3098 if (!_dbus_list_append (&loader->messages, message))
3100 _dbus_verbose ("Failed to append new message to loader queue\n");
3105 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3106 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3107 (header_len + body_len));
3109 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3111 _dbus_verbose ("Failed to move body into new message\n");
3116 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3118 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3119 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3121 _dbus_verbose ("Loaded message %p\n", message);
3123 _dbus_assert (!oom);
3124 _dbus_assert (!loader->corrupted);
3125 _dbus_assert (loader->messages != NULL);
3126 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3134 /* does nothing if the message isn't in the list */
3135 _dbus_list_remove_last (&loader->messages, message);
3138 _dbus_assert (!loader->corrupted);
3140 _dbus_assert (loader->corrupted);
3142 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3148 * Converts buffered data into messages, if we have enough data. If
3149 * we don't have enough data, does nothing.
3151 * @todo we need to check that the proper named header fields exist
3152 * for each message type.
3154 * @todo If a message has unknown type, we should probably eat it
3155 * right here rather than passing it out to applications. However
3156 * it's not an error to see messages of unknown type.
3158 * @param loader the loader.
3159 * @returns #TRUE if we had enough memory to finish.
3162 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3164 while (!loader->corrupted &&
3165 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3167 DBusValidity validity;
3168 int byte_order, fields_array_len, header_len, body_len;
3170 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3177 _dbus_string_get_length (&loader->data)))
3179 DBusMessage *message;
3181 _dbus_assert (validity == DBUS_VALID);
3183 message = dbus_message_new_empty_header ();
3184 if (message == NULL)
3187 if (!load_message (loader, message,
3188 byte_order, fields_array_len,
3189 header_len, body_len))
3191 dbus_message_unref (message);
3192 /* load_message() returns false if corrupted or OOM; if
3193 * corrupted then return TRUE for not OOM
3195 return loader->corrupted;
3198 _dbus_assert (loader->messages != NULL);
3199 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3203 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3205 if (validity != DBUS_VALID)
3207 loader->corrupted = TRUE;
3208 loader->corruption_reason = validity;
3218 * Peeks at first loaded message, returns #NULL if no messages have
3221 * @param loader the loader.
3222 * @returns the next message, or #NULL if none.
3225 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3227 if (loader->messages)
3228 return loader->messages->data;
3234 * Pops a loaded message (passing ownership of the message
3235 * to the caller). Returns #NULL if no messages have been
3238 * @param loader the loader.
3239 * @returns the next message, or #NULL if none.
3242 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3244 return _dbus_list_pop_first (&loader->messages);
3248 * Pops a loaded message inside a list link (passing ownership of the
3249 * message and link to the caller). Returns #NULL if no messages have
3252 * @param loader the loader.
3253 * @returns the next message link, or #NULL if none.
3256 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3258 return _dbus_list_pop_first_link (&loader->messages);
3262 * Returns a popped message link, used to undo a pop.
3264 * @param loader the loader
3265 * @param link the link with a message in it
3268 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3271 _dbus_list_prepend_link (&loader->messages, link);
3275 * Checks whether the loader is confused due to bad data.
3276 * If messages are received that are invalid, the
3277 * loader gets confused and gives up permanently.
3278 * This state is called "corrupted."
3280 * @param loader the loader
3281 * @returns #TRUE if the loader is hosed.
3284 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3286 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3287 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3288 return loader->corrupted;
3292 * Sets the maximum size message we allow.
3294 * @param loader the loader
3295 * @param size the max message size in bytes
3298 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3301 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3303 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3304 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3305 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3307 loader->max_message_size = size;
3311 * Gets the maximum allowed message size in bytes.
3313 * @param loader the loader
3314 * @returns max size in bytes
3317 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3319 return loader->max_message_size;
3322 static DBusDataSlotAllocator slot_allocator;
3323 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3326 * Allocates an integer ID to be used for storing application-specific
3327 * data on any DBusMessage. The allocated ID may then be used
3328 * with dbus_message_set_data() and dbus_message_get_data().
3329 * The passed-in slot must be initialized to -1, and is filled in
3330 * with the slot ID. If the passed-in slot is not -1, it's assumed
3331 * to be already allocated, and its refcount is incremented.
3333 * The allocated slot is global, i.e. all DBusMessage objects will
3334 * have a slot with the given integer ID reserved.
3336 * @param slot_p address of a global variable storing the slot
3337 * @returns #FALSE on failure (no memory)
3340 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3342 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3343 _DBUS_LOCK_NAME (message_slots),
3348 * Deallocates a global ID for message data slots.
3349 * dbus_message_get_data() and dbus_message_set_data() may no
3350 * longer be used with this slot. Existing data stored on existing
3351 * DBusMessage objects will be freed when the message is
3352 * finalized, but may not be retrieved (and may only be replaced if
3353 * someone else reallocates the slot). When the refcount on the
3354 * passed-in slot reaches 0, it is set to -1.
3356 * @param slot_p address storing the slot to deallocate
3359 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3361 _dbus_return_if_fail (*slot_p >= 0);
3363 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3367 * Stores a pointer on a DBusMessage, along
3368 * with an optional function to be used for freeing
3369 * the data when the data is set again, or when
3370 * the message is finalized. The slot number
3371 * must have been allocated with dbus_message_allocate_data_slot().
3373 * @param message the message
3374 * @param slot the slot number
3375 * @param data the data to store
3376 * @param free_data_func finalizer function for the data
3377 * @returns #TRUE if there was enough memory to store the data
3380 dbus_message_set_data (DBusMessage *message,
3383 DBusFreeFunction free_data_func)
3385 DBusFreeFunction old_free_func;
3389 _dbus_return_val_if_fail (message != NULL, FALSE);
3390 _dbus_return_val_if_fail (slot >= 0, FALSE);
3392 retval = _dbus_data_slot_list_set (&slot_allocator,
3393 &message->slot_list,
3394 slot, data, free_data_func,
3395 &old_free_func, &old_data);
3399 /* Do the actual free outside the message lock */
3401 (* old_free_func) (old_data);
3408 * Retrieves data previously set with dbus_message_set_data().
3409 * The slot must still be allocated (must not have been freed).
3411 * @param message the message
3412 * @param slot the slot to get data from
3413 * @returns the data, or #NULL if not found
3416 dbus_message_get_data (DBusMessage *message,
3421 _dbus_return_val_if_fail (message != NULL, NULL);
3423 res = _dbus_data_slot_list_get (&slot_allocator,
3424 &message->slot_list,
3431 * Utility function to convert a machine-readable (not translated)
3432 * string into a D-BUS message type.
3435 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3436 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3437 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3438 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3439 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3444 dbus_message_type_from_string (const char *type_str)
3446 if (strcmp (type_str, "method_call") == 0)
3447 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3448 if (strcmp (type_str, "method_return") == 0)
3449 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3450 else if (strcmp (type_str, "signal") == 0)
3451 return DBUS_MESSAGE_TYPE_SIGNAL;
3452 else if (strcmp (type_str, "error") == 0)
3453 return DBUS_MESSAGE_TYPE_ERROR;
3455 return DBUS_MESSAGE_TYPE_INVALID;
3459 * Utility function to convert a D-BUS message type into a
3460 * machine-readable string (not translated).
3463 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3464 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3465 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3466 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3467 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3472 dbus_message_type_to_string (int type)
3476 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3477 return "method_call";
3478 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3479 return "method_return";
3480 case DBUS_MESSAGE_TYPE_SIGNAL:
3482 case DBUS_MESSAGE_TYPE_ERROR:
3491 /* tests in dbus-message-util.c */