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-byteswap.h"
29 #include "dbus-marshal-header.h"
30 #include "dbus-signature.h"
31 #include "dbus-message-private.h"
32 #include "dbus-object-tree.h"
33 #include "dbus-memory.h"
34 #include "dbus-list.h"
35 #include "dbus-threads-internal.h"
39 * @defgroup DBusMessageInternals DBusMessage implementation details
40 * @ingroup DBusInternals
41 * @brief DBusMessage private implementation details.
43 * The guts of DBusMessage and its methods.
48 /* Not thread locked, but strictly const/read-only so should be OK
50 /** An static string representing an empty signature */
51 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
53 /* these have wacky values to help trap uninitialized iterators;
54 * but has to fit in 3 bits
57 DBUS_MESSAGE_ITER_TYPE_READER = 3,
58 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
61 /** typedef for internals of message iterator */
62 typedef struct DBusMessageRealIter DBusMessageRealIter;
65 * @brief Internals of DBusMessageIter
67 * Object representing a position in a message. All fields are internal.
69 struct DBusMessageRealIter
71 DBusMessage *message; /**< Message used */
72 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
73 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
74 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
77 DBusTypeWriter writer; /**< writer */
78 DBusTypeReader reader; /**< reader */
79 } u; /**< the type writer or reader that does all the work */
83 get_const_signature (DBusHeader *header,
84 const DBusString **type_str_p,
87 if (_dbus_header_get_field_raw (header,
88 DBUS_HEADER_FIELD_SIGNATURE,
92 *type_pos_p += 1; /* skip the signature length which is 1 byte */
96 *type_str_p = &_dbus_empty_signature_str;
102 * Swaps the message to compiler byte order if required
104 * @param message the message
107 _dbus_message_byteswap (DBusMessage *message)
109 const DBusString *type_str;
112 if (message->byte_order == DBUS_COMPILER_BYTE_ORDER)
115 _dbus_verbose ("Swapping message into compiler byte order\n");
117 get_const_signature (&message->header, &type_str, &type_pos);
119 _dbus_marshal_byteswap (type_str, type_pos,
121 DBUS_COMPILER_BYTE_ORDER,
124 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
126 _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
129 #define ensure_byte_order(message) \
130 if (message->byte_order != DBUS_COMPILER_BYTE_ORDER) \
131 _dbus_message_byteswap (message)
134 * Gets the data to be sent over the network for this message.
135 * The header and then the body should be written out.
136 * This function is guaranteed to always return the same
137 * data once a message is locked (with _dbus_message_lock()).
139 * @param message the message.
140 * @param header return location for message header data.
141 * @param body return location for message body data.
144 _dbus_message_get_network_data (DBusMessage *message,
145 const DBusString **header,
146 const DBusString **body)
148 _dbus_assert (message->locked);
150 *header = &message->header.data;
151 *body = &message->body;
155 * Sets the serial number of a message.
156 * This can only be done once on a message.
158 * @param message the message
159 * @param serial the serial
162 _dbus_message_set_serial (DBusMessage *message,
163 dbus_uint32_t serial)
165 _dbus_assert (message != NULL);
166 _dbus_assert (!message->locked);
167 _dbus_assert (dbus_message_get_serial (message) == 0);
169 _dbus_header_set_serial (&message->header, serial);
173 * Adds a counter to be incremented immediately with the
174 * size of this message, and decremented by the size
175 * of this message when this message if finalized.
176 * The link contains a counter with its refcount already
177 * incremented, but the counter itself not incremented.
178 * Ownership of link and counter refcount is passed to
181 * @param message the message
182 * @param link link with counter as data
185 _dbus_message_add_size_counter_link (DBusMessage *message,
188 /* right now we don't recompute the delta when message
189 * size changes, and that's OK for current purposes
190 * I think, but could be important to change later.
191 * Do recompute it whenever there are no outstanding counters,
192 * since it's basically free.
194 if (message->size_counters == NULL)
196 message->size_counter_delta =
197 _dbus_string_get_length (&message->header.data) +
198 _dbus_string_get_length (&message->body);
201 _dbus_verbose ("message has size %ld\n",
202 message->size_counter_delta);
206 _dbus_list_append_link (&message->size_counters, link);
208 _dbus_counter_adjust (link->data, message->size_counter_delta);
212 * Adds a counter to be incremented immediately with the
213 * size of this message, and decremented by the size
214 * of this message when this message if finalized.
216 * @param message the message
217 * @param counter the counter
218 * @returns #FALSE if no memory
221 _dbus_message_add_size_counter (DBusMessage *message,
222 DBusCounter *counter)
226 link = _dbus_list_alloc_link (counter);
230 _dbus_counter_ref (counter);
231 _dbus_message_add_size_counter_link (message, link);
237 * Removes a counter tracking the size of this message, and decrements
238 * the counter by the size of this message.
240 * @param message the message
241 * @param link_return return the link used
242 * @param counter the counter
245 _dbus_message_remove_size_counter (DBusMessage *message,
246 DBusCounter *counter,
247 DBusList **link_return)
251 link = _dbus_list_find_last (&message->size_counters,
253 _dbus_assert (link != NULL);
255 _dbus_list_unlink (&message->size_counters,
260 _dbus_list_free_link (link);
262 _dbus_counter_adjust (counter, - message->size_counter_delta);
264 _dbus_counter_unref (counter);
268 * Locks a message. Allows checking that applications don't keep a
269 * reference to a message in the outgoing queue and change it
270 * underneath us. Messages are locked when they enter the outgoing
271 * queue (dbus_connection_send_message()), and the library complains
272 * if the message is modified while locked.
274 * @param message the message to lock.
277 _dbus_message_lock (DBusMessage *message)
279 if (!message->locked)
281 _dbus_header_update_lengths (&message->header,
282 _dbus_string_get_length (&message->body));
284 /* must have a signature if you have a body */
285 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
286 dbus_message_get_signature (message) != NULL);
288 message->locked = TRUE;
293 set_or_delete_string_field (DBusMessage *message,
299 return _dbus_header_delete_field (&message->header, field);
301 return _dbus_header_set_field_basic (&message->header,
308 /* Probably we don't need to use this */
310 * Sets the signature of the message, i.e. the arguments in the
311 * message payload. The signature includes only "in" arguments for
312 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
313 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
314 * what you might expect (it does not include the signature of the
315 * entire C++-style method).
317 * The signature is a string made up of type codes such as
318 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
319 * the value of #DBUS_TYPE_INVALID). The macros such as
320 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
321 * may find it useful to use the string forms, such as
322 * #DBUS_TYPE_INT32_AS_STRING.
324 * An "unset" or #NULL signature is considered the same as an empty
325 * signature. In fact dbus_message_get_signature() will never return
328 * @param message the message
329 * @param signature the type signature or #NULL to unset
330 * @returns #FALSE if no memory
333 _dbus_message_set_signature (DBusMessage *message,
334 const char *signature)
336 _dbus_return_val_if_fail (message != NULL, FALSE);
337 _dbus_return_val_if_fail (!message->locked, FALSE);
338 _dbus_return_val_if_fail (signature == NULL ||
339 _dbus_check_is_valid_signature (signature));
340 /* can't delete the signature if you have a message body */
341 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
344 return set_or_delete_string_field (message,
345 DBUS_HEADER_FIELD_SIGNATURE,
354 * @defgroup DBusMessage DBusMessage
356 * @brief Message to be sent or received over a DBusConnection.
358 * A DBusMessage is the most basic unit of communication over a
359 * DBusConnection. A DBusConnection represents a stream of messages
360 * received from a remote application, and a stream of messages
361 * sent to a remote application.
367 * @typedef DBusMessage
369 * Opaque data type representing a message received from or to be
370 * sent to another application.
374 * Returns the serial of a message or 0 if none has been specified.
375 * The message's serial number is provided by the application sending
376 * the message and is used to identify replies to this message. All
377 * messages received on a connection will have a serial, but messages
378 * you haven't sent yet may return 0.
380 * @param message the message
381 * @returns the client serial
384 dbus_message_get_serial (DBusMessage *message)
386 _dbus_return_val_if_fail (message != NULL, 0);
388 return _dbus_header_get_serial (&message->header);
392 * Sets the reply serial of a message (the client serial
393 * of the message this is a reply to).
395 * @param message the message
396 * @param reply_serial the client serial
397 * @returns #FALSE if not enough memory
400 dbus_message_set_reply_serial (DBusMessage *message,
401 dbus_uint32_t reply_serial)
403 _dbus_return_val_if_fail (message != NULL, FALSE);
404 _dbus_return_val_if_fail (!message->locked, FALSE);
406 return _dbus_header_set_field_basic (&message->header,
407 DBUS_HEADER_FIELD_REPLY_SERIAL,
413 * Returns the serial that the message is a reply to or 0 if none.
415 * @param message the message
416 * @returns the reply serial
419 dbus_message_get_reply_serial (DBusMessage *message)
421 dbus_uint32_t v_UINT32;
423 _dbus_return_val_if_fail (message != NULL, 0);
425 if (_dbus_header_get_field_basic (&message->header,
426 DBUS_HEADER_FIELD_REPLY_SERIAL,
435 free_size_counter (void *element,
438 DBusCounter *counter = element;
439 DBusMessage *message = data;
441 _dbus_counter_adjust (counter, - message->size_counter_delta);
443 _dbus_counter_unref (counter);
447 dbus_message_finalize (DBusMessage *message)
449 _dbus_assert (message->refcount.value == 0);
451 /* This calls application callbacks! */
452 _dbus_data_slot_list_free (&message->slot_list);
454 _dbus_list_foreach (&message->size_counters,
455 free_size_counter, message);
456 _dbus_list_clear (&message->size_counters);
458 _dbus_header_free (&message->header);
459 _dbus_string_free (&message->body);
461 _dbus_assert (message->refcount.value == 0);
468 * We cache some DBusMessage to reduce the overhead of allocating
469 * them. In my profiling this consistently made about an 8%
470 * difference. It avoids the malloc for the message, the malloc for
471 * the slot list, the malloc for the header string and body string,
472 * and the associated free() calls. It does introduce another global
473 * lock which could be a performance issue in certain cases.
475 * For the echo client/server the round trip time goes from around
476 * .000077 to .000069 with the message cache on my laptop. The sysprof
477 * change is as follows (numbers are cumulative percentage):
479 * with message cache implemented as array as it is now (0.000069 per):
480 * new_empty_header 1.46
481 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
487 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
490 * with message cache implemented as list (0.000070 per roundtrip):
491 * new_empty_header 2.72
492 * list_pop_first 1.88
496 * without cache (0.000077 per roundtrip):
497 * new_empty_header 6.7
498 * string_init_preallocated 3.43
507 * If you implement the message_cache with a list, the primary reason
508 * it's slower is that you add another thread lock (on the DBusList
512 /** Avoid caching huge messages */
513 #define MAX_MESSAGE_SIZE_TO_CACHE _DBUS_ONE_MEGABYTE
515 /** Avoid caching too many messages */
516 #define MAX_MESSAGE_CACHE_SIZE 5
518 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
519 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
520 static int message_cache_count = 0;
521 static dbus_bool_t message_cache_shutdown_registered = FALSE;
524 dbus_message_cache_shutdown (void *data)
528 _DBUS_LOCK (message_cache);
531 while (i < MAX_MESSAGE_CACHE_SIZE)
533 if (message_cache[i])
534 dbus_message_finalize (message_cache[i]);
539 message_cache_count = 0;
540 message_cache_shutdown_registered = FALSE;
542 _DBUS_UNLOCK (message_cache);
546 * Tries to get a message from the message cache. The retrieved
547 * message will have junk in it, so it still needs to be cleared out
548 * in dbus_message_new_empty_header()
550 * @returns the message, or #NULL if none cached
553 dbus_message_get_cached (void)
555 DBusMessage *message;
560 _DBUS_LOCK (message_cache);
562 _dbus_assert (message_cache_count >= 0);
564 if (message_cache_count == 0)
566 _DBUS_UNLOCK (message_cache);
570 /* This is not necessarily true unless count > 0, and
571 * message_cache is uninitialized until the shutdown is
574 _dbus_assert (message_cache_shutdown_registered);
577 while (i < MAX_MESSAGE_CACHE_SIZE)
579 if (message_cache[i])
581 message = message_cache[i];
582 message_cache[i] = NULL;
583 message_cache_count -= 1;
588 _dbus_assert (message_cache_count >= 0);
589 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
590 _dbus_assert (message != NULL);
592 _DBUS_UNLOCK (message_cache);
594 _dbus_assert (message->refcount.value == 0);
595 _dbus_assert (message->size_counters == NULL);
601 * Tries to cache a message, otherwise finalize it.
603 * @param message the message
606 dbus_message_cache_or_finalize (DBusMessage *message)
608 dbus_bool_t was_cached;
611 _dbus_assert (message->refcount.value == 0);
613 /* This calls application code and has to be done first thing
614 * without holding the lock
616 _dbus_data_slot_list_clear (&message->slot_list);
618 _dbus_list_foreach (&message->size_counters,
619 free_size_counter, message);
620 _dbus_list_clear (&message->size_counters);
624 _DBUS_LOCK (message_cache);
626 if (!message_cache_shutdown_registered)
628 _dbus_assert (message_cache_count == 0);
630 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
634 while (i < MAX_MESSAGE_CACHE_SIZE)
636 message_cache[i] = NULL;
640 message_cache_shutdown_registered = TRUE;
643 _dbus_assert (message_cache_count >= 0);
645 if ((_dbus_string_get_length (&message->header.data) +
646 _dbus_string_get_length (&message->body)) >
647 MAX_MESSAGE_SIZE_TO_CACHE)
650 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
653 /* Find empty slot */
655 while (message_cache[i] != NULL)
658 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
660 _dbus_assert (message_cache[i] == NULL);
661 message_cache[i] = message;
662 message_cache_count += 1;
664 #ifndef DBUS_DISABLE_CHECKS
665 message->in_cache = TRUE;
669 _DBUS_UNLOCK (message_cache);
671 _dbus_assert (message->refcount.value == 0);
674 dbus_message_finalize (message);
678 dbus_message_new_empty_header (void)
680 DBusMessage *message;
681 dbus_bool_t from_cache;
683 message = dbus_message_get_cached ();
692 message = dbus_new (DBusMessage, 1);
695 #ifndef DBUS_DISABLE_CHECKS
696 message->generation = _dbus_current_generation;
700 message->refcount.value = 1;
701 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
702 message->locked = FALSE;
703 #ifndef DBUS_DISABLE_CHECKS
704 message->in_cache = 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 name 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_bus_name (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);
1082 _dbus_return_val_if_fail (!message->in_cache, NULL);
1084 old_refcount = _dbus_atomic_inc (&message->refcount);
1085 _dbus_assert (old_refcount >= 1);
1091 * Decrements the reference count of a DBusMessage.
1093 * @param message The message
1094 * @see dbus_message_ref
1097 dbus_message_unref (DBusMessage *message)
1099 dbus_int32_t old_refcount;
1101 _dbus_return_if_fail (message != NULL);
1102 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1103 _dbus_return_if_fail (!message->in_cache);
1105 old_refcount = _dbus_atomic_dec (&message->refcount);
1107 _dbus_assert (old_refcount >= 0);
1109 if (old_refcount == 1)
1111 /* Calls application callbacks! */
1112 dbus_message_cache_or_finalize (message);
1117 * Gets the type of a message. Types include
1118 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1119 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1120 * types are allowed and all code must silently ignore messages of
1121 * unknown type. DBUS_MESSAGE_TYPE_INVALID will never be returned,
1125 * @param message the message
1126 * @returns the type of the message
1129 dbus_message_get_type (DBusMessage *message)
1131 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1133 return _dbus_header_get_message_type (&message->header);
1137 * Appends fields to a message given a variable argument list. The
1138 * variable argument list should contain the type of each argument
1139 * followed by the value to append. Appendable types are basic types,
1140 * and arrays of fixed-length basic types. To append variable-length
1141 * basic types, or any more complex value, you have to use an iterator
1142 * rather than this function.
1144 * To append a basic type, specify its type code followed by the
1145 * address of the value. For example:
1149 * dbus_int32_t v_INT32 = 42;
1150 * const char *v_STRING = "Hello World";
1151 * DBUS_TYPE_INT32, &v_INT32,
1152 * DBUS_TYPE_STRING, &v_STRING,
1155 * To append an array of fixed-length basic types, pass in the
1156 * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1157 * the array pointer, and a 32-bit integer giving the number of
1158 * elements in the array. So for example:
1160 * const dbus_int32_t array[] = { 1, 2, 3 };
1161 * const dbus_int32_t *v_ARRAY = array;
1162 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3
1165 * @warning in C, given "int array[]", "&array == array" (the
1166 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1167 * So if you're using an array instead of a pointer you have to create
1168 * a pointer variable, assign the array to it, then take the address
1169 * of the pointer variable. For strings it works to write
1170 * const char *array = "Hello" and then use &array though.
1172 * The last argument to this function must be #DBUS_TYPE_INVALID,
1173 * marking the end of the argument list.
1175 * String/signature/path arrays should be passed in as "const char***
1176 * address_of_array" and "int n_elements"
1178 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1180 * @todo If this fails due to lack of memory, the message is hosed and
1181 * you have to start over building the whole message.
1183 * @param message the message
1184 * @param first_arg_type type of the first argument
1185 * @param ... value of first argument, list of additional type-value pairs
1186 * @returns #TRUE on success
1189 dbus_message_append_args (DBusMessage *message,
1196 _dbus_return_val_if_fail (message != NULL, FALSE);
1198 va_start (var_args, first_arg_type);
1199 retval = dbus_message_append_args_valist (message,
1208 * This function takes a va_list for use by language bindings.
1209 * It's otherwise the same as dbus_message_append_args().
1211 * @todo for now, if this function fails due to OOM it will leave
1212 * the message half-written and you have to discard the message
1215 * @see dbus_message_append_args.
1216 * @param message the message
1217 * @param first_arg_type type of first argument
1218 * @param var_args value of first argument, then list of type/value pairs
1219 * @returns #TRUE on success
1222 dbus_message_append_args_valist (DBusMessage *message,
1227 DBusMessageIter iter;
1229 _dbus_return_val_if_fail (message != NULL, FALSE);
1231 type = first_arg_type;
1233 dbus_message_iter_init_append (message, &iter);
1235 while (type != DBUS_TYPE_INVALID)
1237 if (dbus_type_is_basic (type))
1239 const DBusBasicValue *value;
1240 value = va_arg (var_args, const DBusBasicValue*);
1242 if (!dbus_message_iter_append_basic (&iter,
1247 else if (type == DBUS_TYPE_ARRAY)
1250 DBusMessageIter array;
1253 element_type = va_arg (var_args, int);
1255 buf[0] = element_type;
1257 if (!dbus_message_iter_open_container (&iter,
1263 if (dbus_type_is_fixed (element_type))
1265 const DBusBasicValue **value;
1268 value = va_arg (var_args, const DBusBasicValue**);
1269 n_elements = va_arg (var_args, int);
1271 if (!dbus_message_iter_append_fixed_array (&array,
1277 else if (element_type == DBUS_TYPE_STRING ||
1278 element_type == DBUS_TYPE_SIGNATURE ||
1279 element_type == DBUS_TYPE_OBJECT_PATH)
1281 const char ***value_p;
1286 value_p = va_arg (var_args, const char***);
1287 n_elements = va_arg (var_args, int);
1292 while (i < n_elements)
1294 if (!dbus_message_iter_append_basic (&array,
1303 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1304 _dbus_type_to_string (element_type),
1305 _DBUS_FUNCTION_NAME);
1309 if (!dbus_message_iter_close_container (&iter, &array))
1312 #ifndef DBUS_DISABLE_CHECKS
1315 _dbus_warn ("type %s isn't supported yet in %s\n",
1316 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1321 type = va_arg (var_args, int);
1331 * Gets arguments from a message given a variable argument list. The
1332 * supported types include those supported by
1333 * dbus_message_append_args(); that is, basic types and arrays of
1334 * fixed-length basic types. The arguments are the same as they would
1335 * be for dbus_message_iter_get_basic() or
1336 * dbus_message_iter_get_fixed_array().
1338 * In addition to those types, arrays of string, object path, and
1339 * signature are supported; but these are returned as allocated memory
1340 * and must be freed with dbus_free_string_array(), while the other
1341 * types are returned as const references. To get a string array
1342 * pass in "char ***array_location" and "int *n_elements"
1344 * The variable argument list should contain the type of the argument
1345 * followed by a pointer to where the value should be stored. The list
1346 * is terminated with #DBUS_TYPE_INVALID.
1348 * The returned values are constant; do not free them. They point
1349 * into the #DBusMessage.
1351 * If the requested arguments are not present, or do not have the
1352 * requested types, then an error will be set.
1354 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1356 * @param message the message
1357 * @param error error to be filled in on failure
1358 * @param first_arg_type the first argument type
1359 * @param ... location for first argument value, then list of type-location pairs
1360 * @returns #FALSE if the error was set
1363 dbus_message_get_args (DBusMessage *message,
1371 _dbus_return_val_if_fail (message != NULL, FALSE);
1372 _dbus_return_val_if_error_is_set (error, FALSE);
1374 va_start (var_args, first_arg_type);
1375 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1382 * This function takes a va_list for use by language bindings. It is
1383 * otherwise the same as dbus_message_get_args().
1385 * @see dbus_message_get_args
1386 * @param message the message
1387 * @param error error to be filled in
1388 * @param first_arg_type type of the first argument
1389 * @param var_args return location for first argument, followed by list of type/location pairs
1390 * @returns #FALSE if error was set
1393 dbus_message_get_args_valist (DBusMessage *message,
1398 DBusMessageIter iter;
1400 _dbus_return_val_if_fail (message != NULL, FALSE);
1401 _dbus_return_val_if_error_is_set (error, FALSE);
1403 dbus_message_iter_init (message, &iter);
1404 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1408 _dbus_message_iter_init_common (DBusMessage *message,
1409 DBusMessageRealIter *real,
1412 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1414 /* Since the iterator will read or write who-knows-what from the
1415 * message, we need to get in the right byte order
1417 ensure_byte_order (message);
1419 real->message = message;
1420 real->changed_stamp = message->changed_stamp;
1421 real->iter_type = iter_type;
1422 real->sig_refcount = 0;
1426 * Initializes a #DBusMessageIter for reading the arguments of the
1427 * message passed in.
1429 * @param message the message
1430 * @param iter pointer to an iterator to initialize
1431 * @returns #FALSE if the message has no arguments
1434 dbus_message_iter_init (DBusMessage *message,
1435 DBusMessageIter *iter)
1437 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1438 const DBusString *type_str;
1441 _dbus_return_val_if_fail (message != NULL, FALSE);
1442 _dbus_return_val_if_fail (iter != NULL, FALSE);
1444 get_const_signature (&message->header, &type_str, &type_pos);
1446 _dbus_message_iter_init_common (message, real,
1447 DBUS_MESSAGE_ITER_TYPE_READER);
1449 _dbus_type_reader_init (&real->u.reader,
1450 message->byte_order,
1455 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1458 #ifndef DBUS_DISABLE_CHECKS
1460 _dbus_message_iter_check (DBusMessageRealIter *iter)
1464 _dbus_warn ("dbus message iterator is NULL\n");
1468 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
1470 if (iter->u.reader.byte_order != iter->message->byte_order)
1472 _dbus_warn ("dbus message changed byte order since iterator was created\n");
1475 /* because we swap the message into compiler order when you init an iter */
1476 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
1478 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
1480 if (iter->u.writer.byte_order != iter->message->byte_order)
1482 _dbus_warn ("dbus message changed byte order since append iterator was created\n");
1485 /* because we swap the message into compiler order when you init an iter */
1486 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
1490 _dbus_warn ("dbus message iterator looks uninitialized or corrupted\n");
1494 if (iter->changed_stamp != iter->message->changed_stamp)
1496 _dbus_warn ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
1502 #endif /* DBUS_DISABLE_CHECKS */
1505 * Checks if an iterator has any more fields.
1507 * @param iter the message iter
1508 * @returns #TRUE if there are more fields
1512 dbus_message_iter_has_next (DBusMessageIter *iter)
1514 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1516 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1517 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1519 return _dbus_type_reader_has_next (&real->u.reader);
1523 * Moves the iterator to the next field, if any. If there's no next
1524 * field, returns #FALSE. If the iterator moves forward, returns
1527 * @param iter the message iter
1528 * @returns #TRUE if the iterator was moved to the next field
1531 dbus_message_iter_next (DBusMessageIter *iter)
1533 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1535 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1536 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1538 return _dbus_type_reader_next (&real->u.reader);
1542 * Returns the argument type of the argument that the message iterator
1543 * points to. If the iterator is at the end of the message, returns
1544 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1547 * dbus_message_iter_init (&iter);
1548 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1549 * dbus_message_iter_next (&iter);
1552 * @param iter the message iter
1553 * @returns the argument type
1556 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1558 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1560 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1561 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1563 return _dbus_type_reader_get_current_type (&real->u.reader);
1567 * Returns the element type of the array that the message iterator
1568 * points to. Note that you need to check that the iterator points to
1569 * an array prior to using this function.
1571 * @param iter the message iter
1572 * @returns the array element type
1575 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1577 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1579 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1580 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1581 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1583 return _dbus_type_reader_get_element_type (&real->u.reader);
1587 * Recurses into a container value when reading values from a message,
1588 * initializing a sub-iterator to use for traversing the child values
1591 * Note that this recurses into a value, not a type, so you can only
1592 * recurse if the value exists. The main implication of this is that
1593 * if you have for example an empty array of array of int32, you can
1594 * recurse into the outermost array, but it will have no values, so
1595 * you won't be able to recurse further. There's no array of int32 to
1598 * @param iter the message iterator
1599 * @param sub the sub-iterator to initialize
1602 dbus_message_iter_recurse (DBusMessageIter *iter,
1603 DBusMessageIter *sub)
1605 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1606 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1608 _dbus_return_if_fail (_dbus_message_iter_check (real));
1609 _dbus_return_if_fail (sub != NULL);
1612 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1616 * Returns the current signature of a message iterator. This
1617 * is useful primarily for dealing with variants; one can
1618 * recurse into a variant and determine the signature of
1619 * the variant's value.
1621 * @param iter the message iterator
1622 * @returns the contained signature, or NULL if out of memory
1625 dbus_message_iter_get_signature (DBusMessageIter *iter)
1627 const DBusString *sig;
1631 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1633 _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
1635 if (!_dbus_string_init (&retstr))
1638 _dbus_type_reader_get_signature (&real->u.reader, &sig,
1640 if (!_dbus_string_append_len (&retstr,
1641 _dbus_string_get_const_data (sig) + start,
1644 if (!_dbus_string_steal_data (&retstr, &ret))
1646 _dbus_string_free (&retstr);
1651 * Reads a basic-typed value from the message iterator.
1652 * Basic types are the non-containers such as integer and string.
1654 * The value argument should be the address of a location to store
1655 * the returned value. So for int32 it should be a "dbus_int32_t*"
1656 * and for string a "const char**". The returned value is
1657 * by reference and should not be freed.
1659 * All returned values are guaranteed to fit in 8 bytes. So you can
1660 * write code like this:
1663 * #ifdef DBUS_HAVE_INT64
1664 * dbus_uint64_t value;
1666 * dbus_message_iter_get_basic (&read_iter, &value);
1667 * type = dbus_message_iter_get_arg_type (&read_iter);
1668 * dbus_message_iter_append_basic (&write_iter, type, &value);
1672 * To avoid the #DBUS_HAVE_INT64 conditional, create a struct or
1673 * something that occupies at least 8 bytes, e.g. you could use a
1674 * struct with two int32 values in it. dbus_uint64_t is just one
1675 * example of a type that's large enough to hold any possible value.
1677 * Be sure you have somehow checked that
1678 * dbus_message_iter_get_arg_type() matches the type you are
1679 * expecting, or you'll crash when you try to use an integer as a
1680 * string or something.
1682 * @param iter the iterator
1683 * @param value location to store the value
1686 dbus_message_iter_get_basic (DBusMessageIter *iter,
1689 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1691 _dbus_return_if_fail (_dbus_message_iter_check (real));
1692 _dbus_return_if_fail (value != NULL);
1694 _dbus_type_reader_read_basic (&real->u.reader,
1699 * Returns the number of elements in the array;
1701 * @param iter the iterator
1702 * @returns the number of elements in the array
1705 dbus_message_iter_get_array_len (DBusMessageIter *iter)
1707 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1709 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
1711 return _dbus_type_reader_get_array_length (&real->u.reader);
1715 * Reads a block of fixed-length values from the message iterator.
1716 * Fixed-length values are those basic types that are not string-like,
1717 * such as integers, bool, double. The block read will be from the
1718 * current position in the array until the end of the array.
1720 * This function should only be used if #dbus_type_is_fixed returns
1721 * #TRUE for the element type.
1723 * The value argument should be the address of a location to store the
1724 * returned array. So for int32 it should be a "const dbus_int32_t**"
1725 * The returned value is by reference and should not be freed.
1727 * @param iter the iterator
1728 * @param value location to store the block
1729 * @param n_elements number of elements in the block
1732 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
1736 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1738 _dbus_return_if_fail (_dbus_message_iter_check (real));
1739 _dbus_return_if_fail (value != NULL);
1740 _dbus_return_if_fail (dbus_type_is_fixed (_dbus_type_reader_get_current_type (&real->u.reader)));
1742 _dbus_type_reader_read_fixed_multi (&real->u.reader,
1747 * This function takes a va_list for use by language bindings and is
1748 * otherwise the same as dbus_message_iter_get_args().
1749 * dbus_message_get_args() is the place to go for complete
1752 * @see dbus_message_get_args
1753 * @param iter the message iter
1754 * @param error error to be filled in
1755 * @param first_arg_type type of the first argument
1756 * @param var_args return location for first argument, followed by list of type/location pairs
1757 * @returns #FALSE if error was set
1760 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1765 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1766 int spec_type, msg_type, i;
1769 _dbus_assert (_dbus_message_iter_check (real));
1773 spec_type = first_arg_type;
1776 while (spec_type != DBUS_TYPE_INVALID)
1778 msg_type = dbus_message_iter_get_arg_type (iter);
1780 if (msg_type != spec_type)
1782 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1783 "Argument %d is specified to be of type \"%s\", but "
1784 "is actually of type \"%s\"\n", i,
1785 _dbus_type_to_string (spec_type),
1786 _dbus_type_to_string (msg_type));
1791 if (dbus_type_is_basic (spec_type))
1793 DBusBasicValue *ptr;
1795 ptr = va_arg (var_args, DBusBasicValue*);
1797 _dbus_assert (ptr != NULL);
1799 _dbus_type_reader_read_basic (&real->u.reader,
1802 else if (spec_type == DBUS_TYPE_ARRAY)
1805 int spec_element_type;
1806 const DBusBasicValue **ptr;
1808 DBusTypeReader array;
1810 spec_element_type = va_arg (var_args, int);
1811 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1813 if (spec_element_type != element_type)
1815 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1816 "Argument %d is specified to be an array of \"%s\", but "
1817 "is actually an array of \"%s\"\n",
1819 _dbus_type_to_string (spec_element_type),
1820 _dbus_type_to_string (element_type));
1825 if (dbus_type_is_fixed (spec_element_type))
1827 ptr = va_arg (var_args, const DBusBasicValue**);
1828 n_elements_p = va_arg (var_args, int*);
1830 _dbus_assert (ptr != NULL);
1831 _dbus_assert (n_elements_p != NULL);
1833 _dbus_type_reader_recurse (&real->u.reader, &array);
1835 _dbus_type_reader_read_fixed_multi (&array,
1838 else if (spec_element_type == DBUS_TYPE_STRING ||
1839 spec_element_type == DBUS_TYPE_SIGNATURE ||
1840 spec_element_type == DBUS_TYPE_OBJECT_PATH)
1842 char ***str_array_p;
1846 str_array_p = va_arg (var_args, char***);
1847 n_elements_p = va_arg (var_args, int*);
1849 _dbus_assert (str_array_p != NULL);
1850 _dbus_assert (n_elements_p != NULL);
1852 /* Count elements in the array */
1853 _dbus_type_reader_recurse (&real->u.reader, &array);
1856 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
1859 _dbus_type_reader_next (&array);
1862 str_array = dbus_new0 (char*, n_elements + 1);
1863 if (str_array == NULL)
1865 _DBUS_SET_OOM (error);
1869 /* Now go through and dup each string */
1870 _dbus_type_reader_recurse (&real->u.reader, &array);
1873 while (i < n_elements)
1876 _dbus_type_reader_read_basic (&array,
1879 str_array[i] = _dbus_strdup (s);
1880 if (str_array[i] == NULL)
1882 dbus_free_string_array (str_array);
1883 _DBUS_SET_OOM (error);
1889 if (!_dbus_type_reader_next (&array))
1890 _dbus_assert (i == n_elements);
1893 _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
1894 _dbus_assert (i == n_elements);
1895 _dbus_assert (str_array[i] == NULL);
1897 *str_array_p = str_array;
1898 *n_elements_p = n_elements;
1900 #ifndef DBUS_DISABLE_CHECKS
1903 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
1904 _DBUS_FUNCTION_NAME);
1909 #ifndef DBUS_DISABLE_CHECKS
1912 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
1913 _DBUS_FUNCTION_NAME);
1918 spec_type = va_arg (var_args, int);
1919 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1921 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1922 "Message has only %d arguments, but more were expected", i);
1937 * Initializes a #DBusMessageIter for appending arguments to the end
1940 * @todo If appending any of the arguments fails due to lack of
1941 * memory, generally the message is hosed and you have to start over
1942 * building the whole message.
1944 * @param message the message
1945 * @param iter pointer to an iterator to initialize
1948 dbus_message_iter_init_append (DBusMessage *message,
1949 DBusMessageIter *iter)
1951 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1953 _dbus_return_if_fail (message != NULL);
1954 _dbus_return_if_fail (iter != NULL);
1956 _dbus_message_iter_init_common (message, real,
1957 DBUS_MESSAGE_ITER_TYPE_WRITER);
1959 /* We create the signature string and point iterators at it "on demand"
1960 * when a value is actually appended. That means that init() never fails
1963 _dbus_type_writer_init_types_delayed (&real->u.writer,
1964 message->byte_order,
1966 _dbus_string_get_length (&message->body));
1970 * Creates a temporary signature string containing the current
1971 * signature, stores it in the iterator, and points the iterator to
1972 * the end of it. Used any time we write to the message.
1974 * @param real an iterator without a type_str
1975 * @returns #FALSE if no memory
1978 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
1981 const DBusString *current_sig;
1982 int current_sig_pos;
1984 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1986 if (real->u.writer.type_str != NULL)
1988 _dbus_assert (real->sig_refcount > 0);
1989 real->sig_refcount += 1;
1993 str = dbus_new (DBusString, 1);
1997 if (!_dbus_header_get_field_raw (&real->message->header,
1998 DBUS_HEADER_FIELD_SIGNATURE,
1999 ¤t_sig, ¤t_sig_pos))
2006 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2007 current_sig_pos += 1; /* move on to sig data */
2009 if (!_dbus_string_init_preallocated (str, current_len + 4))
2015 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2018 _dbus_string_free (str);
2025 if (!_dbus_string_init_preallocated (str, 4))
2032 real->sig_refcount = 1;
2034 _dbus_type_writer_add_types (&real->u.writer,
2035 str, _dbus_string_get_length (str));
2040 * Sets the new signature as the message signature, frees the
2041 * signature string, and marks the iterator as not having a type_str
2042 * anymore. Frees the signature even if it fails, so you can't
2043 * really recover from failure. Kinda busted.
2045 * @param real an iterator without a type_str
2046 * @returns #FALSE if no memory
2049 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2052 const char *v_STRING;
2055 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2056 _dbus_assert (real->u.writer.type_str != NULL);
2057 _dbus_assert (real->sig_refcount > 0);
2059 real->sig_refcount -= 1;
2061 if (real->sig_refcount > 0)
2063 _dbus_assert (real->sig_refcount == 0);
2067 str = real->u.writer.type_str;
2069 v_STRING = _dbus_string_get_const_data (str);
2070 if (!_dbus_header_set_field_basic (&real->message->header,
2071 DBUS_HEADER_FIELD_SIGNATURE,
2072 DBUS_TYPE_SIGNATURE,
2076 _dbus_type_writer_remove_types (&real->u.writer);
2077 _dbus_string_free (str);
2083 #ifndef DBUS_DISABLE_CHECKS
2085 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2087 if (!_dbus_message_iter_check (iter))
2090 if (iter->message->locked)
2092 _dbus_warn ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2098 #endif /* DBUS_DISABLE_CHECKS */
2101 * Appends a basic-typed value to the message. The basic types are the
2102 * non-container types such as integer and string.
2104 * The "value" argument should be the address of a basic-typed value.
2105 * So for string, const char**. For integer, dbus_int32_t*.
2107 * @todo If this fails due to lack of memory, the message is hosed and
2108 * you have to start over building the whole message.
2110 * @param iter the append iterator
2111 * @param type the type of the value
2112 * @param value the address of the value
2113 * @returns #FALSE if not enough memory
2116 dbus_message_iter_append_basic (DBusMessageIter *iter,
2120 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2123 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2124 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2125 _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2126 _dbus_return_val_if_fail (value != NULL, FALSE);
2128 if (!_dbus_message_iter_open_signature (real))
2131 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2133 if (!_dbus_message_iter_close_signature (real))
2140 * Appends a block of fixed-length values to an array. The
2141 * fixed-length types are all basic types that are not string-like. So
2142 * int32, double, bool, etc. You must call
2143 * dbus_message_iter_open_container() to open an array of values
2144 * before calling this function. You may call this function multiple
2145 * times (and intermixed with calls to
2146 * dbus_message_iter_append_basic()) for the same array.
2148 * The "value" argument should be the address of the array. So for
2149 * integer, "dbus_int32_t**" is expected for example.
2151 * @warning in C, given "int array[]", "&array == array" (the
2152 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2153 * So if you're using an array instead of a pointer you have to create
2154 * a pointer variable, assign the array to it, then take the address
2155 * of the pointer variable.
2157 * const dbus_int32_t array[] = { 1, 2, 3 };
2158 * const dbus_int32_t *v_ARRAY = array;
2159 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2160 * fprintf (stderr, "No memory!\n");
2162 * For strings it works to write const char *array = "Hello" and then
2163 * use &array though.
2165 * @todo If this fails due to lack of memory, the message is hosed and
2166 * you have to start over building the whole message.
2168 * @param iter the append iterator
2169 * @param element_type the type of the array elements
2170 * @param value the address of the array
2171 * @param n_elements the number of elements to append
2172 * @returns #FALSE if not enough memory
2175 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2180 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2183 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2184 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2185 _dbus_return_val_if_fail (dbus_type_is_fixed (element_type), FALSE);
2186 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2187 _dbus_return_val_if_fail (value != NULL, FALSE);
2188 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2189 _dbus_return_val_if_fail (n_elements <=
2190 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2193 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2199 * Appends a container-typed value to the message; you are required to
2200 * append the contents of the container using the returned
2201 * sub-iterator, and then call
2202 * dbus_message_iter_close_container(). Container types are for
2203 * example struct, variant, and array. For variants, the
2204 * contained_signature should be the type of the single value inside
2205 * the variant. For structs and dict entries, contained_signature
2206 * should be #NULL; it will be set to whatever types you write into
2207 * the struct. For arrays, contained_signature should be the type of
2208 * the array elements.
2210 * @todo If this fails due to lack of memory, the message is hosed and
2211 * you have to start over building the whole message.
2213 * @param iter the append iterator
2214 * @param type the type of the value
2215 * @param contained_signature the type of container contents
2216 * @param sub sub-iterator to initialize
2217 * @returns #FALSE if not enough memory
2220 dbus_message_iter_open_container (DBusMessageIter *iter,
2222 const char *contained_signature,
2223 DBusMessageIter *sub)
2225 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2226 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2227 DBusString contained_str;
2229 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2230 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2231 _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
2232 _dbus_return_val_if_fail (sub != NULL, FALSE);
2233 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2234 contained_signature == NULL) ||
2235 (type == DBUS_TYPE_DICT_ENTRY &&
2236 contained_signature == NULL) ||
2237 contained_signature != NULL, FALSE);
2240 /* FIXME this would fail if the contained_signature is a dict entry,
2241 * since dict entries are invalid signatures standalone (they must be in
2244 _dbus_return_val_if_fail (contained_signature == NULL ||
2245 _dbus_check_is_valid_signature (contained_signature));
2248 if (!_dbus_message_iter_open_signature (real))
2253 if (contained_signature != NULL)
2255 _dbus_string_init_const (&contained_str, contained_signature);
2257 return _dbus_type_writer_recurse (&real->u.writer,
2260 &real_sub->u.writer);
2264 return _dbus_type_writer_recurse (&real->u.writer,
2267 &real_sub->u.writer);
2273 * Closes a container-typed value appended to the message; may write
2274 * out more information to the message known only after the entire
2275 * container is written, and may free resources created by
2276 * dbus_message_iter_open_container().
2278 * @todo If this fails due to lack of memory, the message is hosed and
2279 * you have to start over building the whole message.
2281 * @param iter the append iterator
2282 * @param sub sub-iterator to close
2283 * @returns #FALSE if not enough memory
2286 dbus_message_iter_close_container (DBusMessageIter *iter,
2287 DBusMessageIter *sub)
2289 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2290 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2293 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2294 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2295 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2296 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2298 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2299 &real_sub->u.writer);
2301 if (!_dbus_message_iter_close_signature (real))
2308 * Sets a flag indicating that the message does not want a reply; if
2309 * this flag is set, the other end of the connection may (but is not
2310 * required to) optimize by not sending method return or error
2311 * replies. If this flag is set, there is no way to know whether the
2312 * message successfully arrived at the remote end. Normally you know a
2313 * message was received when you receive the reply to it.
2315 * @param message the message
2316 * @param no_reply #TRUE if no reply is desired
2319 dbus_message_set_no_reply (DBusMessage *message,
2320 dbus_bool_t no_reply)
2322 _dbus_return_if_fail (message != NULL);
2323 _dbus_return_if_fail (!message->locked);
2325 _dbus_header_toggle_flag (&message->header,
2326 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2331 * Returns #TRUE if the message does not expect
2334 * @param message the message
2335 * @returns #TRUE if the message sender isn't waiting for a reply
2338 dbus_message_get_no_reply (DBusMessage *message)
2340 _dbus_return_val_if_fail (message != NULL, FALSE);
2342 return _dbus_header_get_flag (&message->header,
2343 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2347 * Sets a flag indicating that an owner for the destination name will
2348 * be automatically started before the message is delivered. When this
2349 * flag is set, the message is held until a name owner finishes
2350 * starting up, or fails to start up. In case of failure, the reply
2353 * @param message the message
2354 * @param auto_start #TRUE if auto-starting is desired
2357 dbus_message_set_auto_start (DBusMessage *message,
2358 dbus_bool_t auto_start)
2360 _dbus_return_if_fail (message != NULL);
2361 _dbus_return_if_fail (!message->locked);
2363 _dbus_header_toggle_flag (&message->header,
2364 DBUS_HEADER_FLAG_NO_AUTO_START,
2369 * Returns #TRUE if the message will cause an owner for
2370 * destination name to be auto-started.
2372 * @param message the message
2373 * @returns #TRUE if the message will use auto-start
2376 dbus_message_get_auto_start (DBusMessage *message)
2378 _dbus_return_val_if_fail (message != NULL, FALSE);
2380 return !_dbus_header_get_flag (&message->header,
2381 DBUS_HEADER_FLAG_NO_AUTO_START);
2386 * Sets the object path this message is being sent to (for
2387 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2388 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2390 * @param message the message
2391 * @param object_path the path or #NULL to unset
2392 * @returns #FALSE if not enough memory
2395 dbus_message_set_path (DBusMessage *message,
2396 const char *object_path)
2398 _dbus_return_val_if_fail (message != NULL, FALSE);
2399 _dbus_return_val_if_fail (!message->locked, FALSE);
2400 _dbus_return_val_if_fail (object_path == NULL ||
2401 _dbus_check_is_valid_path (object_path),
2404 return set_or_delete_string_field (message,
2405 DBUS_HEADER_FIELD_PATH,
2406 DBUS_TYPE_OBJECT_PATH,
2411 * Gets the object path this message is being sent to (for
2412 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2413 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2415 * @param message the message
2416 * @returns the path (should not be freed) or #NULL
2419 dbus_message_get_path (DBusMessage *message)
2423 _dbus_return_val_if_fail (message != NULL, NULL);
2425 v = NULL; /* in case field doesn't exist */
2426 _dbus_header_get_field_basic (&message->header,
2427 DBUS_HEADER_FIELD_PATH,
2428 DBUS_TYPE_OBJECT_PATH,
2434 * Gets the object path this message is being sent to
2435 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2436 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2437 * format (one array element per path component).
2438 * Free the returned array with dbus_free_string_array().
2440 * An empty but non-NULL path array means the path "/".
2441 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2442 * and the path "/" becomes { NULL }.
2444 * @todo this could be optimized by using the len from the message
2445 * instead of calling strlen() again
2447 * @param message the message
2448 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2449 * @returns #FALSE if no memory to allocate the array
2452 dbus_message_get_path_decomposed (DBusMessage *message,
2457 _dbus_return_val_if_fail (message != NULL, FALSE);
2458 _dbus_return_val_if_fail (path != NULL, FALSE);
2462 v = dbus_message_get_path (message);
2465 if (!_dbus_decompose_path (v, strlen (v),
2473 * Sets the interface this message is being sent to
2474 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2475 * the interface a signal is being emitted from
2476 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2478 * @param message the message
2479 * @param interface the interface or #NULL to unset
2480 * @returns #FALSE if not enough memory
2483 dbus_message_set_interface (DBusMessage *message,
2484 const char *interface)
2486 _dbus_return_val_if_fail (message != NULL, FALSE);
2487 _dbus_return_val_if_fail (!message->locked, FALSE);
2488 _dbus_return_val_if_fail (interface == NULL ||
2489 _dbus_check_is_valid_interface (interface),
2492 return set_or_delete_string_field (message,
2493 DBUS_HEADER_FIELD_INTERFACE,
2499 * Gets the interface this message is being sent to
2500 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2501 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2502 * The interface name is fully-qualified (namespaced).
2503 * Returns #NULL if none.
2505 * @param message the message
2506 * @returns the message interface (should not be freed) or #NULL
2509 dbus_message_get_interface (DBusMessage *message)
2513 _dbus_return_val_if_fail (message != NULL, NULL);
2515 v = NULL; /* in case field doesn't exist */
2516 _dbus_header_get_field_basic (&message->header,
2517 DBUS_HEADER_FIELD_INTERFACE,
2524 * Sets the interface member being invoked
2525 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2526 * (DBUS_MESSAGE_TYPE_SIGNAL).
2527 * The interface name is fully-qualified (namespaced).
2529 * @param message the message
2530 * @param member the member or #NULL to unset
2531 * @returns #FALSE if not enough memory
2534 dbus_message_set_member (DBusMessage *message,
2537 _dbus_return_val_if_fail (message != NULL, FALSE);
2538 _dbus_return_val_if_fail (!message->locked, FALSE);
2539 _dbus_return_val_if_fail (member == NULL ||
2540 _dbus_check_is_valid_member (member),
2543 return set_or_delete_string_field (message,
2544 DBUS_HEADER_FIELD_MEMBER,
2550 * Gets the interface member being invoked
2551 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2552 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2554 * @param message the message
2555 * @returns the member name (should not be freed) or #NULL
2558 dbus_message_get_member (DBusMessage *message)
2562 _dbus_return_val_if_fail (message != NULL, NULL);
2564 v = NULL; /* in case field doesn't exist */
2565 _dbus_header_get_field_basic (&message->header,
2566 DBUS_HEADER_FIELD_MEMBER,
2573 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2574 * The name is fully-qualified (namespaced).
2576 * @param message the message
2577 * @param error_name the name or #NULL to unset
2578 * @returns #FALSE if not enough memory
2581 dbus_message_set_error_name (DBusMessage *message,
2582 const char *error_name)
2584 _dbus_return_val_if_fail (message != NULL, FALSE);
2585 _dbus_return_val_if_fail (!message->locked, FALSE);
2586 _dbus_return_val_if_fail (error_name == NULL ||
2587 _dbus_check_is_valid_error_name (error_name),
2590 return set_or_delete_string_field (message,
2591 DBUS_HEADER_FIELD_ERROR_NAME,
2597 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2600 * @param message the message
2601 * @returns the error name (should not be freed) or #NULL
2604 dbus_message_get_error_name (DBusMessage *message)
2608 _dbus_return_val_if_fail (message != NULL, NULL);
2610 v = NULL; /* in case field doesn't exist */
2611 _dbus_header_get_field_basic (&message->header,
2612 DBUS_HEADER_FIELD_ERROR_NAME,
2619 * Sets the message's destination. The destination is the name of
2620 * another connection on the bus and may be either the unique name
2621 * assigned by the bus to each connection, or a well-known name
2622 * specified in advance.
2624 * @param message the message
2625 * @param destination the destination name or #NULL to unset
2626 * @returns #FALSE if not enough memory
2629 dbus_message_set_destination (DBusMessage *message,
2630 const char *destination)
2632 _dbus_return_val_if_fail (message != NULL, FALSE);
2633 _dbus_return_val_if_fail (!message->locked, FALSE);
2634 _dbus_return_val_if_fail (destination == NULL ||
2635 _dbus_check_is_valid_bus_name (destination),
2638 return set_or_delete_string_field (message,
2639 DBUS_HEADER_FIELD_DESTINATION,
2645 * Gets the destination of a message or #NULL if there is none set.
2647 * @param message the message
2648 * @returns the message destination (should not be freed) or #NULL
2651 dbus_message_get_destination (DBusMessage *message)
2655 _dbus_return_val_if_fail (message != NULL, NULL);
2657 v = NULL; /* in case field doesn't exist */
2658 _dbus_header_get_field_basic (&message->header,
2659 DBUS_HEADER_FIELD_DESTINATION,
2666 * Sets the message sender.
2668 * @param message the message
2669 * @param sender the sender or #NULL to unset
2670 * @returns #FALSE if not enough memory
2673 dbus_message_set_sender (DBusMessage *message,
2676 _dbus_return_val_if_fail (message != NULL, FALSE);
2677 _dbus_return_val_if_fail (!message->locked, FALSE);
2678 _dbus_return_val_if_fail (sender == NULL ||
2679 _dbus_check_is_valid_bus_name (sender),
2682 return set_or_delete_string_field (message,
2683 DBUS_HEADER_FIELD_SENDER,
2689 * Gets the unique name of the connection which originated this
2690 * message, or #NULL if unknown or inapplicable. The sender is filled
2691 * in by the message bus.
2693 * @param message the message
2694 * @returns the unique name of the sender or #NULL
2697 dbus_message_get_sender (DBusMessage *message)
2701 _dbus_return_val_if_fail (message != NULL, NULL);
2703 v = NULL; /* in case field doesn't exist */
2704 _dbus_header_get_field_basic (&message->header,
2705 DBUS_HEADER_FIELD_SENDER,
2712 * Gets the type signature of the message, i.e. the arguments in the
2713 * message payload. The signature includes only "in" arguments for
2714 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2715 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2716 * what you might expect (it does not include the signature of the
2717 * entire C++-style method).
2719 * The signature is a string made up of type codes such as
2720 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2721 * the value of #DBUS_TYPE_INVALID).
2723 * @param message the message
2724 * @returns the type signature
2727 dbus_message_get_signature (DBusMessage *message)
2729 const DBusString *type_str;
2732 _dbus_return_val_if_fail (message != NULL, NULL);
2734 get_const_signature (&message->header, &type_str, &type_pos);
2736 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
2740 _dbus_message_has_type_interface_member (DBusMessage *message,
2742 const char *interface,
2747 _dbus_assert (message != NULL);
2748 _dbus_assert (interface != NULL);
2749 _dbus_assert (member != NULL);
2751 if (dbus_message_get_type (message) != type)
2754 /* Optimize by checking the short member name first
2755 * instead of the longer interface name
2758 n = dbus_message_get_member (message);
2760 if (n && strcmp (n, member) == 0)
2762 n = dbus_message_get_interface (message);
2764 if (n == NULL || strcmp (n, interface) == 0)
2772 * Checks whether the message is a method call with the given
2773 * interface and member fields. If the message is not
2774 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
2775 * member field, returns #FALSE. If the interface field is missing,
2776 * then it will be assumed equal to the provided interface. The D-BUS
2777 * protocol allows method callers to leave out the interface name.
2779 * @param message the message
2780 * @param interface the name to check (must not be #NULL)
2781 * @param method the name to check (must not be #NULL)
2783 * @returns #TRUE if the message is the specified method call
2786 dbus_message_is_method_call (DBusMessage *message,
2787 const char *interface,
2790 _dbus_return_val_if_fail (message != NULL, FALSE);
2791 _dbus_return_val_if_fail (interface != NULL, FALSE);
2792 _dbus_return_val_if_fail (method != NULL, FALSE);
2793 /* don't check that interface/method are valid since it would be
2794 * expensive, and not catch many common errors
2797 return _dbus_message_has_type_interface_member (message,
2798 DBUS_MESSAGE_TYPE_METHOD_CALL,
2803 * Checks whether the message is a signal with the given interface and
2804 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
2805 * has a different interface or member field, returns #FALSE. If the
2806 * interface field in the message is missing, it is assumed to match
2807 * any interface you pass in to this function.
2809 * @param message the message
2810 * @param interface the name to check (must not be #NULL)
2811 * @param signal_name the name to check (must not be #NULL)
2813 * @returns #TRUE if the message is the specified signal
2816 dbus_message_is_signal (DBusMessage *message,
2817 const char *interface,
2818 const char *signal_name)
2820 _dbus_return_val_if_fail (message != NULL, FALSE);
2821 _dbus_return_val_if_fail (interface != NULL, FALSE);
2822 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
2823 /* don't check that interface/name are valid since it would be
2824 * expensive, and not catch many common errors
2827 return _dbus_message_has_type_interface_member (message,
2828 DBUS_MESSAGE_TYPE_SIGNAL,
2829 interface, signal_name);
2833 * Checks whether the message is an error reply with the given error
2834 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
2835 * different name, returns #FALSE.
2837 * @param message the message
2838 * @param error_name the name to check (must not be #NULL)
2840 * @returns #TRUE if the message is the specified error
2843 dbus_message_is_error (DBusMessage *message,
2844 const char *error_name)
2848 _dbus_return_val_if_fail (message != NULL, FALSE);
2849 _dbus_return_val_if_fail (error_name != NULL, FALSE);
2850 /* don't check that error_name is valid since it would be expensive,
2851 * and not catch many common errors
2854 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2857 n = dbus_message_get_error_name (message);
2859 if (n && strcmp (n, error_name) == 0)
2866 * Checks whether the message was sent to the given name. If the
2867 * message has no destination specified or has a different
2868 * destination, returns #FALSE.
2870 * @param message the message
2871 * @param name the name to check (must not be #NULL)
2873 * @returns #TRUE if the message has the given destination name
2876 dbus_message_has_destination (DBusMessage *message,
2881 _dbus_return_val_if_fail (message != NULL, FALSE);
2882 _dbus_return_val_if_fail (name != NULL, FALSE);
2883 /* don't check that name is valid since it would be expensive, and
2884 * not catch many common errors
2887 s = dbus_message_get_destination (message);
2889 if (s && strcmp (s, name) == 0)
2896 * Checks whether the message has the given unique name as its sender.
2897 * If the message has no sender specified or has a different sender,
2898 * returns #FALSE. Note that a peer application will always have the
2899 * unique name of the connection as the sender. So you can't use this
2900 * function to see whether a sender owned a well-known name.
2902 * Messages from the bus itself will have #DBUS_SERVICE_DBUS
2905 * @param message the message
2906 * @param name the name to check (must not be #NULL)
2908 * @returns #TRUE if the message has the given sender
2911 dbus_message_has_sender (DBusMessage *message,
2916 _dbus_return_val_if_fail (message != NULL, FALSE);
2917 _dbus_return_val_if_fail (name != NULL, FALSE);
2918 /* don't check that name is valid since it would be expensive, and
2919 * not catch many common errors
2922 s = dbus_message_get_sender (message);
2924 if (s && strcmp (s, name) == 0)
2931 * Checks whether the message has the given signature; see
2932 * dbus_message_get_signature() for more details on what the signature
2935 * @param message the message
2936 * @param signature typecode array
2937 * @returns #TRUE if message has the given signature
2940 dbus_message_has_signature (DBusMessage *message,
2941 const char *signature)
2945 _dbus_return_val_if_fail (message != NULL, FALSE);
2946 _dbus_return_val_if_fail (signature != NULL, FALSE);
2947 /* don't check that signature is valid since it would be expensive,
2948 * and not catch many common errors
2951 s = dbus_message_get_signature (message);
2953 if (s && strcmp (s, signature) == 0)
2960 * Sets a #DBusError based on the contents of the given
2961 * message. The error is only set if the message
2962 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
2963 * The name of the error is set to the name of the message,
2964 * and the error message is set to the first argument
2965 * if the argument exists and is a string.
2967 * The return value indicates whether the error was set (the error is
2968 * set if and only if the message is an error message). So you can
2969 * check for an error reply and convert it to DBusError in one go:
2971 * if (dbus_set_error_from_message (error, reply))
2977 * @param error the error to set
2978 * @param message the message to set it from
2979 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
2982 dbus_set_error_from_message (DBusError *error,
2983 DBusMessage *message)
2987 _dbus_return_val_if_fail (message != NULL, FALSE);
2988 _dbus_return_val_if_error_is_set (error, FALSE);
2990 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2994 dbus_message_get_args (message, NULL,
2995 DBUS_TYPE_STRING, &str,
2998 dbus_set_error (error, dbus_message_get_error_name (message),
2999 str ? "%s" : NULL, str);
3007 * @addtogroup DBusMessageInternals
3013 * The initial buffer size of the message loader.
3015 * @todo this should be based on min header size plus some average
3016 * body size, or something. Or rather, the min header size only, if we
3017 * want to try to read only the header, store that in a DBusMessage,
3018 * then read only the body and store that, etc., depends on
3019 * how we optimize _dbus_message_loader_get_buffer() and what
3020 * the exact message format is.
3022 #define INITIAL_LOADER_DATA_LEN 32
3025 * Creates a new message loader. Returns #NULL if memory can't
3028 * @returns new loader, or #NULL.
3031 _dbus_message_loader_new (void)
3033 DBusMessageLoader *loader;
3035 loader = dbus_new0 (DBusMessageLoader, 1);
3039 loader->refcount = 1;
3041 loader->corrupted = FALSE;
3042 loader->corruption_reason = DBUS_VALID;
3044 /* this can be configured by the app, but defaults to the protocol max */
3045 loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3047 if (!_dbus_string_init (&loader->data))
3053 /* preallocate the buffer for speed, ignore failure */
3054 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3055 _dbus_string_set_length (&loader->data, 0);
3061 * Increments the reference count of the loader.
3063 * @param loader the loader.
3064 * @returns the loader
3067 _dbus_message_loader_ref (DBusMessageLoader *loader)
3069 loader->refcount += 1;
3075 * Decrements the reference count of the loader and finalizes the
3076 * loader when the count reaches zero.
3078 * @param loader the loader.
3081 _dbus_message_loader_unref (DBusMessageLoader *loader)
3083 loader->refcount -= 1;
3084 if (loader->refcount == 0)
3086 _dbus_list_foreach (&loader->messages,
3087 (DBusForeachFunction) dbus_message_unref,
3089 _dbus_list_clear (&loader->messages);
3090 _dbus_string_free (&loader->data);
3096 * Gets the buffer to use for reading data from the network. Network
3097 * data is read directly into an allocated buffer, which is then used
3098 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3099 * The buffer must always be returned immediately using
3100 * _dbus_message_loader_return_buffer(), even if no bytes are
3101 * successfully read.
3103 * @todo this function can be a lot more clever. For example
3104 * it can probably always return a buffer size to read exactly
3105 * the body of the next message, thus avoiding any memory wastage
3108 * @todo we need to enforce a max length on strings in header fields.
3110 * @param loader the message loader.
3111 * @param buffer the buffer
3114 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3115 DBusString **buffer)
3117 _dbus_assert (!loader->buffer_outstanding);
3119 *buffer = &loader->data;
3121 loader->buffer_outstanding = TRUE;
3125 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3126 * indicating to the loader how many bytes of the buffer were filled
3127 * in. This function must always be called, even if no bytes were
3128 * successfully read.
3130 * @param loader the loader.
3131 * @param buffer the buffer.
3132 * @param bytes_read number of bytes that were read into the buffer.
3135 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3139 _dbus_assert (loader->buffer_outstanding);
3140 _dbus_assert (buffer == &loader->data);
3142 loader->buffer_outstanding = FALSE;
3146 * FIXME when we move the header out of the buffer, that memmoves all
3147 * buffered messages. Kind of crappy.
3149 * Also we copy the header and body, which is kind of crappy. To
3150 * avoid this, we have to allow header and body to be in a single
3151 * memory block, which is good for messages we read and bad for
3152 * messages we are creating. But we could move_len() the buffer into
3153 * this single memory block, and move_len() will just swap the buffers
3154 * if you're moving the entire buffer replacing the dest string.
3156 * We could also have the message loader tell the transport how many
3157 * bytes to read; so it would first ask for some arbitrary number like
3158 * 256, then if the message was incomplete it would use the
3159 * header/body len to ask for exactly the size of the message (or
3160 * blocks the size of a typical kernel buffer for the socket). That
3161 * way we don't get trailing bytes in the buffer that have to be
3162 * memmoved. Though I suppose we also don't have a chance of reading a
3163 * bunch of small messages at once, so the optimization may be stupid.
3165 * Another approach would be to keep a "start" index into
3166 * loader->data and only delete it occasionally, instead of after
3167 * each message is loaded.
3169 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3172 load_message (DBusMessageLoader *loader,
3173 DBusMessage *message,
3175 int fields_array_len,
3180 DBusValidity validity;
3181 const DBusString *type_str;
3183 DBusValidationMode mode;
3185 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3190 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3193 /* 1. VALIDATE AND COPY OVER HEADER */
3194 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3195 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3197 if (!_dbus_header_load (&message->header,
3205 _dbus_string_get_length (&loader->data)))
3207 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3208 if (validity == DBUS_VALID)
3212 loader->corrupted = TRUE;
3213 loader->corruption_reason = validity;
3218 _dbus_assert (validity == DBUS_VALID);
3220 message->byte_order = byte_order;
3222 /* 2. VALIDATE BODY */
3223 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3225 get_const_signature (&message->header, &type_str, &type_pos);
3227 /* Because the bytes_remaining arg is NULL, this validates that the
3228 * body is the right length
3230 validity = _dbus_validate_body_with_reason (type_str,
3237 if (validity != DBUS_VALID)
3239 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3241 loader->corrupted = TRUE;
3242 loader->corruption_reason = validity;
3248 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3250 if (!_dbus_list_append (&loader->messages, message))
3252 _dbus_verbose ("Failed to append new message to loader queue\n");
3257 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3258 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3259 (header_len + body_len));
3261 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3263 _dbus_verbose ("Failed to move body into new message\n");
3268 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3270 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3271 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3273 _dbus_verbose ("Loaded message %p\n", message);
3275 _dbus_assert (!oom);
3276 _dbus_assert (!loader->corrupted);
3277 _dbus_assert (loader->messages != NULL);
3278 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3286 /* does nothing if the message isn't in the list */
3287 _dbus_list_remove_last (&loader->messages, message);
3290 _dbus_assert (!loader->corrupted);
3292 _dbus_assert (loader->corrupted);
3294 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3300 * Converts buffered data into messages, if we have enough data. If
3301 * we don't have enough data, does nothing.
3303 * @todo we need to check that the proper named header fields exist
3304 * for each message type.
3306 * @todo If a message has unknown type, we should probably eat it
3307 * right here rather than passing it out to applications. However
3308 * it's not an error to see messages of unknown type.
3310 * @param loader the loader.
3311 * @returns #TRUE if we had enough memory to finish.
3314 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3316 while (!loader->corrupted &&
3317 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3319 DBusValidity validity;
3320 int byte_order, fields_array_len, header_len, body_len;
3322 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3329 _dbus_string_get_length (&loader->data)))
3331 DBusMessage *message;
3333 _dbus_assert (validity == DBUS_VALID);
3335 message = dbus_message_new_empty_header ();
3336 if (message == NULL)
3339 if (!load_message (loader, message,
3340 byte_order, fields_array_len,
3341 header_len, body_len))
3343 dbus_message_unref (message);
3344 /* load_message() returns false if corrupted or OOM; if
3345 * corrupted then return TRUE for not OOM
3347 return loader->corrupted;
3350 _dbus_assert (loader->messages != NULL);
3351 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3355 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3357 if (validity != DBUS_VALID)
3359 loader->corrupted = TRUE;
3360 loader->corruption_reason = validity;
3370 * Peeks at first loaded message, returns #NULL if no messages have
3373 * @param loader the loader.
3374 * @returns the next message, or #NULL if none.
3377 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3379 if (loader->messages)
3380 return loader->messages->data;
3386 * Pops a loaded message (passing ownership of the message
3387 * to the caller). Returns #NULL if no messages have been
3390 * @param loader the loader.
3391 * @returns the next message, or #NULL if none.
3394 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3396 return _dbus_list_pop_first (&loader->messages);
3400 * Pops a loaded message inside a list link (passing ownership of the
3401 * message and link to the caller). Returns #NULL if no messages have
3404 * @param loader the loader.
3405 * @returns the next message link, or #NULL if none.
3408 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3410 return _dbus_list_pop_first_link (&loader->messages);
3414 * Returns a popped message link, used to undo a pop.
3416 * @param loader the loader
3417 * @param link the link with a message in it
3420 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3423 _dbus_list_prepend_link (&loader->messages, link);
3427 * Checks whether the loader is confused due to bad data.
3428 * If messages are received that are invalid, the
3429 * loader gets confused and gives up permanently.
3430 * This state is called "corrupted."
3432 * @param loader the loader
3433 * @returns #TRUE if the loader is hosed.
3436 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3438 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3439 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3440 return loader->corrupted;
3444 * Sets the maximum size message we allow.
3446 * @param loader the loader
3447 * @param size the max message size in bytes
3450 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3453 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3455 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3456 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3457 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3459 loader->max_message_size = size;
3463 * Gets the maximum allowed message size in bytes.
3465 * @param loader the loader
3466 * @returns max size in bytes
3469 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3471 return loader->max_message_size;
3474 static DBusDataSlotAllocator slot_allocator;
3475 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3478 * Allocates an integer ID to be used for storing application-specific
3479 * data on any DBusMessage. The allocated ID may then be used
3480 * with dbus_message_set_data() and dbus_message_get_data().
3481 * The passed-in slot must be initialized to -1, and is filled in
3482 * with the slot ID. If the passed-in slot is not -1, it's assumed
3483 * to be already allocated, and its refcount is incremented.
3485 * The allocated slot is global, i.e. all DBusMessage objects will
3486 * have a slot with the given integer ID reserved.
3488 * @param slot_p address of a global variable storing the slot
3489 * @returns #FALSE on failure (no memory)
3492 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3494 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3495 _DBUS_LOCK_NAME (message_slots),
3500 * Deallocates a global ID for message data slots.
3501 * dbus_message_get_data() and dbus_message_set_data() may no
3502 * longer be used with this slot. Existing data stored on existing
3503 * DBusMessage objects will be freed when the message is
3504 * finalized, but may not be retrieved (and may only be replaced if
3505 * someone else reallocates the slot). When the refcount on the
3506 * passed-in slot reaches 0, it is set to -1.
3508 * @param slot_p address storing the slot to deallocate
3511 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3513 _dbus_return_if_fail (*slot_p >= 0);
3515 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3519 * Stores a pointer on a DBusMessage, along
3520 * with an optional function to be used for freeing
3521 * the data when the data is set again, or when
3522 * the message is finalized. The slot number
3523 * must have been allocated with dbus_message_allocate_data_slot().
3525 * @param message the message
3526 * @param slot the slot number
3527 * @param data the data to store
3528 * @param free_data_func finalizer function for the data
3529 * @returns #TRUE if there was enough memory to store the data
3532 dbus_message_set_data (DBusMessage *message,
3535 DBusFreeFunction free_data_func)
3537 DBusFreeFunction old_free_func;
3541 _dbus_return_val_if_fail (message != NULL, FALSE);
3542 _dbus_return_val_if_fail (slot >= 0, FALSE);
3544 retval = _dbus_data_slot_list_set (&slot_allocator,
3545 &message->slot_list,
3546 slot, data, free_data_func,
3547 &old_free_func, &old_data);
3551 /* Do the actual free outside the message lock */
3553 (* old_free_func) (old_data);
3560 * Retrieves data previously set with dbus_message_set_data().
3561 * The slot must still be allocated (must not have been freed).
3563 * @param message the message
3564 * @param slot the slot to get data from
3565 * @returns the data, or #NULL if not found
3568 dbus_message_get_data (DBusMessage *message,
3573 _dbus_return_val_if_fail (message != NULL, NULL);
3575 res = _dbus_data_slot_list_get (&slot_allocator,
3576 &message->slot_list,
3583 * Utility function to convert a machine-readable (not translated)
3584 * string into a D-BUS message type.
3587 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3588 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3589 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3590 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3591 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3596 dbus_message_type_from_string (const char *type_str)
3598 if (strcmp (type_str, "method_call") == 0)
3599 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3600 if (strcmp (type_str, "method_return") == 0)
3601 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3602 else if (strcmp (type_str, "signal") == 0)
3603 return DBUS_MESSAGE_TYPE_SIGNAL;
3604 else if (strcmp (type_str, "error") == 0)
3605 return DBUS_MESSAGE_TYPE_ERROR;
3607 return DBUS_MESSAGE_TYPE_INVALID;
3611 * Utility function to convert a D-BUS message type into a
3612 * machine-readable string (not translated).
3615 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3616 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3617 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3618 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3619 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3624 dbus_message_type_to_string (int type)
3628 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3629 return "method_call";
3630 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3631 return "method_return";
3632 case DBUS_MESSAGE_TYPE_SIGNAL:
3634 case DBUS_MESSAGE_TYPE_ERROR:
3643 /* tests in dbus-message-util.c */