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);
405 _dbus_return_val_if_fail (reply_serial != 0, FALSE); /* 0 is invalid */
407 return _dbus_header_set_field_basic (&message->header,
408 DBUS_HEADER_FIELD_REPLY_SERIAL,
414 * Returns the serial that the message is a reply to or 0 if none.
416 * @param message the message
417 * @returns the reply serial
420 dbus_message_get_reply_serial (DBusMessage *message)
422 dbus_uint32_t v_UINT32;
424 _dbus_return_val_if_fail (message != NULL, 0);
426 if (_dbus_header_get_field_basic (&message->header,
427 DBUS_HEADER_FIELD_REPLY_SERIAL,
436 free_size_counter (void *element,
439 DBusCounter *counter = element;
440 DBusMessage *message = data;
442 _dbus_counter_adjust (counter, - message->size_counter_delta);
444 _dbus_counter_unref (counter);
448 dbus_message_finalize (DBusMessage *message)
450 _dbus_assert (message->refcount.value == 0);
452 /* This calls application callbacks! */
453 _dbus_data_slot_list_free (&message->slot_list);
455 _dbus_list_foreach (&message->size_counters,
456 free_size_counter, message);
457 _dbus_list_clear (&message->size_counters);
459 _dbus_header_free (&message->header);
460 _dbus_string_free (&message->body);
462 _dbus_assert (message->refcount.value == 0);
469 * We cache some DBusMessage to reduce the overhead of allocating
470 * them. In my profiling this consistently made about an 8%
471 * difference. It avoids the malloc for the message, the malloc for
472 * the slot list, the malloc for the header string and body string,
473 * and the associated free() calls. It does introduce another global
474 * lock which could be a performance issue in certain cases.
476 * For the echo client/server the round trip time goes from around
477 * .000077 to .000069 with the message cache on my laptop. The sysprof
478 * change is as follows (numbers are cumulative percentage):
480 * with message cache implemented as array as it is now (0.000069 per):
481 * new_empty_header 1.46
482 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
488 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
491 * with message cache implemented as list (0.000070 per roundtrip):
492 * new_empty_header 2.72
493 * list_pop_first 1.88
497 * without cache (0.000077 per roundtrip):
498 * new_empty_header 6.7
499 * string_init_preallocated 3.43
508 * If you implement the message_cache with a list, the primary reason
509 * it's slower is that you add another thread lock (on the DBusList
513 /** Avoid caching huge messages */
514 #define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
516 /** Avoid caching too many messages */
517 #define MAX_MESSAGE_CACHE_SIZE 5
519 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
520 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
521 static int message_cache_count = 0;
522 static dbus_bool_t message_cache_shutdown_registered = FALSE;
525 dbus_message_cache_shutdown (void *data)
529 _DBUS_LOCK (message_cache);
532 while (i < MAX_MESSAGE_CACHE_SIZE)
534 if (message_cache[i])
535 dbus_message_finalize (message_cache[i]);
540 message_cache_count = 0;
541 message_cache_shutdown_registered = FALSE;
543 _DBUS_UNLOCK (message_cache);
547 * Tries to get a message from the message cache. The retrieved
548 * message will have junk in it, so it still needs to be cleared out
549 * in dbus_message_new_empty_header()
551 * @returns the message, or #NULL if none cached
554 dbus_message_get_cached (void)
556 DBusMessage *message;
561 _DBUS_LOCK (message_cache);
563 _dbus_assert (message_cache_count >= 0);
565 if (message_cache_count == 0)
567 _DBUS_UNLOCK (message_cache);
571 /* This is not necessarily true unless count > 0, and
572 * message_cache is uninitialized until the shutdown is
575 _dbus_assert (message_cache_shutdown_registered);
578 while (i < MAX_MESSAGE_CACHE_SIZE)
580 if (message_cache[i])
582 message = message_cache[i];
583 message_cache[i] = NULL;
584 message_cache_count -= 1;
589 _dbus_assert (message_cache_count >= 0);
590 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
591 _dbus_assert (message != NULL);
593 _DBUS_UNLOCK (message_cache);
595 _dbus_assert (message->refcount.value == 0);
596 _dbus_assert (message->size_counters == NULL);
602 * Tries to cache a message, otherwise finalize it.
604 * @param message the message
607 dbus_message_cache_or_finalize (DBusMessage *message)
609 dbus_bool_t was_cached;
612 _dbus_assert (message->refcount.value == 0);
614 /* This calls application code and has to be done first thing
615 * without holding the lock
617 _dbus_data_slot_list_clear (&message->slot_list);
619 _dbus_list_foreach (&message->size_counters,
620 free_size_counter, message);
621 _dbus_list_clear (&message->size_counters);
625 _DBUS_LOCK (message_cache);
627 if (!message_cache_shutdown_registered)
629 _dbus_assert (message_cache_count == 0);
631 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
635 while (i < MAX_MESSAGE_CACHE_SIZE)
637 message_cache[i] = NULL;
641 message_cache_shutdown_registered = TRUE;
644 _dbus_assert (message_cache_count >= 0);
646 if ((_dbus_string_get_length (&message->header.data) +
647 _dbus_string_get_length (&message->body)) >
648 MAX_MESSAGE_SIZE_TO_CACHE)
651 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
654 /* Find empty slot */
656 while (message_cache[i] != NULL)
659 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
661 _dbus_assert (message_cache[i] == NULL);
662 message_cache[i] = message;
663 message_cache_count += 1;
665 #ifndef DBUS_DISABLE_CHECKS
666 message->in_cache = TRUE;
670 _DBUS_UNLOCK (message_cache);
672 _dbus_assert (message->refcount.value == 0);
675 dbus_message_finalize (message);
679 dbus_message_new_empty_header (void)
681 DBusMessage *message;
682 dbus_bool_t from_cache;
684 message = dbus_message_get_cached ();
693 message = dbus_new (DBusMessage, 1);
696 #ifndef DBUS_DISABLE_CHECKS
697 message->generation = _dbus_current_generation;
701 message->refcount.value = 1;
702 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
703 message->locked = FALSE;
704 #ifndef DBUS_DISABLE_CHECKS
705 message->in_cache = FALSE;
707 message->size_counters = NULL;
708 message->size_counter_delta = 0;
709 message->changed_stamp = 0;
712 _dbus_data_slot_list_init (&message->slot_list);
716 _dbus_header_reinit (&message->header, message->byte_order);
717 _dbus_string_set_length (&message->body, 0);
721 if (!_dbus_header_init (&message->header, message->byte_order))
727 if (!_dbus_string_init_preallocated (&message->body, 32))
729 _dbus_header_free (&message->header);
739 * Constructs a new message of the given message type.
740 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
741 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
743 * @param message_type type of message
744 * @returns new message or #NULL If no memory
747 dbus_message_new (int message_type)
749 DBusMessage *message;
751 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
753 message = dbus_message_new_empty_header ();
757 if (!_dbus_header_create (&message->header,
759 NULL, NULL, NULL, NULL, NULL))
761 dbus_message_unref (message);
769 * Constructs a new message to invoke a method on a remote
770 * object. Returns #NULL if memory can't be allocated for the
771 * message. The destination may be #NULL in which case no destination
772 * is set; this is appropriate when using D-Bus in a peer-to-peer
773 * context (no message bus). The interface may be #NULL, which means
774 * that if multiple methods with the given name exist it is undefined
775 * which one will be invoked.
777 * @param destination name that the message should be sent to or #NULL
778 * @param path object path the message should be sent to
779 * @param interface interface to invoke method on
780 * @param method method to invoke
782 * @returns a new DBusMessage, free with dbus_message_unref()
783 * @see dbus_message_unref()
786 dbus_message_new_method_call (const char *destination,
788 const char *interface,
791 DBusMessage *message;
793 _dbus_return_val_if_fail (path != NULL, NULL);
794 _dbus_return_val_if_fail (method != NULL, NULL);
795 _dbus_return_val_if_fail (destination == NULL ||
796 _dbus_check_is_valid_bus_name (destination), NULL);
797 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
798 _dbus_return_val_if_fail (interface == NULL ||
799 _dbus_check_is_valid_interface (interface), NULL);
800 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
802 message = dbus_message_new_empty_header ();
806 if (!_dbus_header_create (&message->header,
807 DBUS_MESSAGE_TYPE_METHOD_CALL,
808 destination, path, interface, method, NULL))
810 dbus_message_unref (message);
818 * Constructs a message that is a reply to a method call. Returns
819 * #NULL if memory can't be allocated for the message.
821 * @param method_call the message which the created
822 * message is a reply to.
823 * @returns a new DBusMessage, free with dbus_message_unref()
824 * @see dbus_message_new_method_call(), dbus_message_unref()
827 dbus_message_new_method_return (DBusMessage *method_call)
829 DBusMessage *message;
832 _dbus_return_val_if_fail (method_call != NULL, NULL);
834 sender = dbus_message_get_sender (method_call);
836 /* sender is allowed to be null here in peer-to-peer case */
838 message = dbus_message_new_empty_header ();
842 if (!_dbus_header_create (&message->header,
843 DBUS_MESSAGE_TYPE_METHOD_RETURN,
844 sender, NULL, NULL, NULL, NULL))
846 dbus_message_unref (message);
850 dbus_message_set_no_reply (message, TRUE);
852 if (!dbus_message_set_reply_serial (message,
853 dbus_message_get_serial (method_call)))
855 dbus_message_unref (message);
863 * Constructs a new message representing a signal emission. Returns
864 * #NULL if memory can't be allocated for the message. A signal is
865 * identified by its originating interface, and the name of the
868 * @param path the path to the object emitting the signal
869 * @param interface the interface the signal is emitted from
870 * @param name name of the signal
871 * @returns a new DBusMessage, free with dbus_message_unref()
872 * @see dbus_message_unref()
875 dbus_message_new_signal (const char *path,
876 const char *interface,
879 DBusMessage *message;
881 _dbus_return_val_if_fail (path != NULL, NULL);
882 _dbus_return_val_if_fail (interface != NULL, NULL);
883 _dbus_return_val_if_fail (name != NULL, NULL);
884 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
885 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
886 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
888 message = dbus_message_new_empty_header ();
892 if (!_dbus_header_create (&message->header,
893 DBUS_MESSAGE_TYPE_SIGNAL,
894 NULL, path, interface, name, NULL))
896 dbus_message_unref (message);
900 dbus_message_set_no_reply (message, TRUE);
906 * Creates a new message that is an error reply to a certain message.
907 * Error replies are possible in response to method calls primarily.
909 * @param reply_to the original message
910 * @param error_name the error name
911 * @param error_message the error message string or #NULL for none
912 * @returns a new error message
915 dbus_message_new_error (DBusMessage *reply_to,
916 const char *error_name,
917 const char *error_message)
919 DBusMessage *message;
921 DBusMessageIter iter;
923 _dbus_return_val_if_fail (reply_to != NULL, NULL);
924 _dbus_return_val_if_fail (error_name != NULL, NULL);
925 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
927 sender = dbus_message_get_sender (reply_to);
929 /* sender may be NULL for non-message-bus case or
930 * when the message bus is dealing with an unregistered
933 message = dbus_message_new_empty_header ();
937 if (!_dbus_header_create (&message->header,
938 DBUS_MESSAGE_TYPE_ERROR,
939 sender, NULL, NULL, NULL, error_name))
941 dbus_message_unref (message);
945 dbus_message_set_no_reply (message, TRUE);
947 if (!dbus_message_set_reply_serial (message,
948 dbus_message_get_serial (reply_to)))
950 dbus_message_unref (message);
954 if (error_message != NULL)
956 dbus_message_iter_init_append (message, &iter);
957 if (!dbus_message_iter_append_basic (&iter,
961 dbus_message_unref (message);
970 * Creates a new message that is an error reply to a certain message.
971 * Error replies are possible in response to method calls primarily.
973 * @param reply_to the original message
974 * @param error_name the error name
975 * @param error_format the error message format as with printf
976 * @param ... format string arguments
977 * @returns a new error message
980 dbus_message_new_error_printf (DBusMessage *reply_to,
981 const char *error_name,
982 const char *error_format,
987 DBusMessage *message;
989 _dbus_return_val_if_fail (reply_to != NULL, NULL);
990 _dbus_return_val_if_fail (error_name != NULL, NULL);
991 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
993 if (!_dbus_string_init (&str))
996 va_start (args, error_format);
998 if (_dbus_string_append_printf_valist (&str, error_format, args))
999 message = dbus_message_new_error (reply_to, error_name,
1000 _dbus_string_get_const_data (&str));
1004 _dbus_string_free (&str);
1013 * Creates a new message that is an exact replica of the message
1014 * specified, except that its refcount is set to 1, its message serial
1015 * is reset to 0, and if the original message was "locked" (in the
1016 * outgoing message queue and thus not modifiable) the new message
1017 * will not be locked.
1019 * @param message the message.
1020 * @returns the new message.
1023 dbus_message_copy (const DBusMessage *message)
1025 DBusMessage *retval;
1027 _dbus_return_val_if_fail (message != NULL, NULL);
1029 retval = dbus_new0 (DBusMessage, 1);
1033 retval->refcount.value = 1;
1034 retval->byte_order = message->byte_order;
1035 retval->locked = FALSE;
1036 #ifndef DBUS_DISABLE_CHECKS
1037 retval->generation = message->generation;
1040 if (!_dbus_header_copy (&message->header, &retval->header))
1046 if (!_dbus_string_init_preallocated (&retval->body,
1047 _dbus_string_get_length (&message->body)))
1049 _dbus_header_free (&retval->header);
1054 if (!_dbus_string_copy (&message->body, 0,
1061 _dbus_header_free (&retval->header);
1062 _dbus_string_free (&retval->body);
1070 * Increments the reference count of a DBusMessage.
1072 * @param message The message
1073 * @returns the message
1074 * @see dbus_message_unref
1077 dbus_message_ref (DBusMessage *message)
1079 dbus_int32_t old_refcount;
1081 _dbus_return_val_if_fail (message != NULL, NULL);
1082 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1083 _dbus_return_val_if_fail (!message->in_cache, NULL);
1085 old_refcount = _dbus_atomic_inc (&message->refcount);
1086 _dbus_assert (old_refcount >= 1);
1092 * Decrements the reference count of a DBusMessage.
1094 * @param message The message
1095 * @see dbus_message_ref
1098 dbus_message_unref (DBusMessage *message)
1100 dbus_int32_t old_refcount;
1102 _dbus_return_if_fail (message != NULL);
1103 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1104 _dbus_return_if_fail (!message->in_cache);
1106 old_refcount = _dbus_atomic_dec (&message->refcount);
1108 _dbus_assert (old_refcount >= 0);
1110 if (old_refcount == 1)
1112 /* Calls application callbacks! */
1113 dbus_message_cache_or_finalize (message);
1118 * Gets the type of a message. Types include
1119 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1120 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1121 * types are allowed and all code must silently ignore messages of
1122 * unknown type. DBUS_MESSAGE_TYPE_INVALID will never be returned,
1126 * @param message the message
1127 * @returns the type of the message
1130 dbus_message_get_type (DBusMessage *message)
1132 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1134 return _dbus_header_get_message_type (&message->header);
1138 * Appends fields to a message given a variable argument list. The
1139 * variable argument list should contain the type of each argument
1140 * followed by the value to append. Appendable types are basic types,
1141 * and arrays of fixed-length basic types. To append variable-length
1142 * basic types, or any more complex value, you have to use an iterator
1143 * rather than this function.
1145 * To append a basic type, specify its type code followed by the
1146 * address of the value. For example:
1150 * dbus_int32_t v_INT32 = 42;
1151 * const char *v_STRING = "Hello World";
1152 * DBUS_TYPE_INT32, &v_INT32,
1153 * DBUS_TYPE_STRING, &v_STRING,
1156 * To append an array of fixed-length basic types, pass in the
1157 * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1158 * the array pointer, and a 32-bit integer giving the number of
1159 * elements in the array. So for example:
1161 * const dbus_int32_t array[] = { 1, 2, 3 };
1162 * const dbus_int32_t *v_ARRAY = array;
1163 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3
1166 * @warning in C, given "int array[]", "&array == array" (the
1167 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1168 * So if you're using an array instead of a pointer you have to create
1169 * a pointer variable, assign the array to it, then take the address
1170 * of the pointer variable. For strings it works to write
1171 * const char *array = "Hello" and then use &array though.
1173 * The last argument to this function must be #DBUS_TYPE_INVALID,
1174 * marking the end of the argument list.
1176 * String/signature/path arrays should be passed in as "const char***
1177 * address_of_array" and "int n_elements"
1179 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1181 * @todo If this fails due to lack of memory, the message is hosed and
1182 * you have to start over building the whole message.
1184 * @param message the message
1185 * @param first_arg_type type of the first argument
1186 * @param ... value of first argument, list of additional type-value pairs
1187 * @returns #TRUE on success
1190 dbus_message_append_args (DBusMessage *message,
1197 _dbus_return_val_if_fail (message != NULL, FALSE);
1199 va_start (var_args, first_arg_type);
1200 retval = dbus_message_append_args_valist (message,
1209 * This function takes a va_list for use by language bindings.
1210 * It's otherwise the same as dbus_message_append_args().
1212 * @todo for now, if this function fails due to OOM it will leave
1213 * the message half-written and you have to discard the message
1216 * @see dbus_message_append_args.
1217 * @param message the message
1218 * @param first_arg_type type of first argument
1219 * @param var_args value of first argument, then list of type/value pairs
1220 * @returns #TRUE on success
1223 dbus_message_append_args_valist (DBusMessage *message,
1228 DBusMessageIter iter;
1230 _dbus_return_val_if_fail (message != NULL, FALSE);
1232 type = first_arg_type;
1234 dbus_message_iter_init_append (message, &iter);
1236 while (type != DBUS_TYPE_INVALID)
1238 if (dbus_type_is_basic (type))
1240 const DBusBasicValue *value;
1241 value = va_arg (var_args, const DBusBasicValue*);
1243 if (!dbus_message_iter_append_basic (&iter,
1248 else if (type == DBUS_TYPE_ARRAY)
1251 DBusMessageIter array;
1254 element_type = va_arg (var_args, int);
1256 buf[0] = element_type;
1258 if (!dbus_message_iter_open_container (&iter,
1264 if (dbus_type_is_fixed (element_type))
1266 const DBusBasicValue **value;
1269 value = va_arg (var_args, const DBusBasicValue**);
1270 n_elements = va_arg (var_args, int);
1272 if (!dbus_message_iter_append_fixed_array (&array,
1278 else if (element_type == DBUS_TYPE_STRING ||
1279 element_type == DBUS_TYPE_SIGNATURE ||
1280 element_type == DBUS_TYPE_OBJECT_PATH)
1282 const char ***value_p;
1287 value_p = va_arg (var_args, const char***);
1288 n_elements = va_arg (var_args, int);
1293 while (i < n_elements)
1295 if (!dbus_message_iter_append_basic (&array,
1304 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1305 _dbus_type_to_string (element_type),
1306 _DBUS_FUNCTION_NAME);
1310 if (!dbus_message_iter_close_container (&iter, &array))
1313 #ifndef DBUS_DISABLE_CHECKS
1316 _dbus_warn ("type %s isn't supported yet in %s\n",
1317 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1322 type = va_arg (var_args, int);
1332 * Gets arguments from a message given a variable argument list. The
1333 * supported types include those supported by
1334 * dbus_message_append_args(); that is, basic types and arrays of
1335 * fixed-length basic types. The arguments are the same as they would
1336 * be for dbus_message_iter_get_basic() or
1337 * dbus_message_iter_get_fixed_array().
1339 * In addition to those types, arrays of string, object path, and
1340 * signature are supported; but these are returned as allocated memory
1341 * and must be freed with dbus_free_string_array(), while the other
1342 * types are returned as const references. To get a string array
1343 * pass in "char ***array_location" and "int *n_elements"
1345 * The variable argument list should contain the type of the argument
1346 * followed by a pointer to where the value should be stored. The list
1347 * is terminated with #DBUS_TYPE_INVALID.
1349 * The returned values are constant; do not free them. They point
1350 * into the #DBusMessage.
1352 * If the requested arguments are not present, or do not have the
1353 * requested types, then an error will be set.
1355 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1357 * @param message the message
1358 * @param error error to be filled in on failure
1359 * @param first_arg_type the first argument type
1360 * @param ... location for first argument value, then list of type-location pairs
1361 * @returns #FALSE if the error was set
1364 dbus_message_get_args (DBusMessage *message,
1372 _dbus_return_val_if_fail (message != NULL, FALSE);
1373 _dbus_return_val_if_error_is_set (error, FALSE);
1375 va_start (var_args, first_arg_type);
1376 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1383 * This function takes a va_list for use by language bindings. It is
1384 * otherwise the same as dbus_message_get_args().
1386 * @see dbus_message_get_args
1387 * @param message the message
1388 * @param error error to be filled in
1389 * @param first_arg_type type of the first argument
1390 * @param var_args return location for first argument, followed by list of type/location pairs
1391 * @returns #FALSE if error was set
1394 dbus_message_get_args_valist (DBusMessage *message,
1399 DBusMessageIter iter;
1401 _dbus_return_val_if_fail (message != NULL, FALSE);
1402 _dbus_return_val_if_error_is_set (error, FALSE);
1404 dbus_message_iter_init (message, &iter);
1405 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1409 _dbus_message_iter_init_common (DBusMessage *message,
1410 DBusMessageRealIter *real,
1413 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1415 /* Since the iterator will read or write who-knows-what from the
1416 * message, we need to get in the right byte order
1418 ensure_byte_order (message);
1420 real->message = message;
1421 real->changed_stamp = message->changed_stamp;
1422 real->iter_type = iter_type;
1423 real->sig_refcount = 0;
1427 * Initializes a #DBusMessageIter for reading the arguments of the
1428 * message passed in.
1430 * @param message the message
1431 * @param iter pointer to an iterator to initialize
1432 * @returns #FALSE if the message has no arguments
1435 dbus_message_iter_init (DBusMessage *message,
1436 DBusMessageIter *iter)
1438 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1439 const DBusString *type_str;
1442 _dbus_return_val_if_fail (message != NULL, FALSE);
1443 _dbus_return_val_if_fail (iter != NULL, FALSE);
1445 get_const_signature (&message->header, &type_str, &type_pos);
1447 _dbus_message_iter_init_common (message, real,
1448 DBUS_MESSAGE_ITER_TYPE_READER);
1450 _dbus_type_reader_init (&real->u.reader,
1451 message->byte_order,
1456 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1459 #ifndef DBUS_DISABLE_CHECKS
1461 _dbus_message_iter_check (DBusMessageRealIter *iter)
1465 _dbus_warn ("dbus message iterator is NULL\n");
1469 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
1471 if (iter->u.reader.byte_order != iter->message->byte_order)
1473 _dbus_warn ("dbus message changed byte order since iterator was created\n");
1476 /* because we swap the message into compiler order when you init an iter */
1477 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
1479 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
1481 if (iter->u.writer.byte_order != iter->message->byte_order)
1483 _dbus_warn ("dbus message changed byte order since append iterator was created\n");
1486 /* because we swap the message into compiler order when you init an iter */
1487 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
1491 _dbus_warn ("dbus message iterator looks uninitialized or corrupted\n");
1495 if (iter->changed_stamp != iter->message->changed_stamp)
1497 _dbus_warn ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
1503 #endif /* DBUS_DISABLE_CHECKS */
1506 * Checks if an iterator has any more fields.
1508 * @param iter the message iter
1509 * @returns #TRUE if there are more fields
1513 dbus_message_iter_has_next (DBusMessageIter *iter)
1515 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1517 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1518 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1520 return _dbus_type_reader_has_next (&real->u.reader);
1524 * Moves the iterator to the next field, if any. If there's no next
1525 * field, returns #FALSE. If the iterator moves forward, returns
1528 * @param iter the message iter
1529 * @returns #TRUE if the iterator was moved to the next field
1532 dbus_message_iter_next (DBusMessageIter *iter)
1534 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1536 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1537 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1539 return _dbus_type_reader_next (&real->u.reader);
1543 * Returns the argument type of the argument that the message iterator
1544 * points to. If the iterator is at the end of the message, returns
1545 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1548 * dbus_message_iter_init (&iter);
1549 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1550 * dbus_message_iter_next (&iter);
1553 * @param iter the message iter
1554 * @returns the argument type
1557 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1559 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1561 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1562 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1564 return _dbus_type_reader_get_current_type (&real->u.reader);
1568 * Returns the element type of the array that the message iterator
1569 * points to. Note that you need to check that the iterator points to
1570 * an array prior to using this function.
1572 * @param iter the message iter
1573 * @returns the array element type
1576 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1578 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1580 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1581 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1582 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1584 return _dbus_type_reader_get_element_type (&real->u.reader);
1588 * Recurses into a container value when reading values from a message,
1589 * initializing a sub-iterator to use for traversing the child values
1592 * Note that this recurses into a value, not a type, so you can only
1593 * recurse if the value exists. The main implication of this is that
1594 * if you have for example an empty array of array of int32, you can
1595 * recurse into the outermost array, but it will have no values, so
1596 * you won't be able to recurse further. There's no array of int32 to
1599 * @param iter the message iterator
1600 * @param sub the sub-iterator to initialize
1603 dbus_message_iter_recurse (DBusMessageIter *iter,
1604 DBusMessageIter *sub)
1606 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1607 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1609 _dbus_return_if_fail (_dbus_message_iter_check (real));
1610 _dbus_return_if_fail (sub != NULL);
1613 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1617 * Returns the current signature of a message iterator. This
1618 * is useful primarily for dealing with variants; one can
1619 * recurse into a variant and determine the signature of
1620 * the variant's value.
1622 * @param iter the message iterator
1623 * @returns the contained signature, or NULL if out of memory
1626 dbus_message_iter_get_signature (DBusMessageIter *iter)
1628 const DBusString *sig;
1632 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1634 _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
1636 if (!_dbus_string_init (&retstr))
1639 _dbus_type_reader_get_signature (&real->u.reader, &sig,
1641 if (!_dbus_string_append_len (&retstr,
1642 _dbus_string_get_const_data (sig) + start,
1645 if (!_dbus_string_steal_data (&retstr, &ret))
1647 _dbus_string_free (&retstr);
1652 * Reads a basic-typed value from the message iterator.
1653 * Basic types are the non-containers such as integer and string.
1655 * The value argument should be the address of a location to store
1656 * the returned value. So for int32 it should be a "dbus_int32_t*"
1657 * and for string a "const char**". The returned value is
1658 * by reference and should not be freed.
1660 * All returned values are guaranteed to fit in 8 bytes. So you can
1661 * write code like this:
1664 * #ifdef DBUS_HAVE_INT64
1665 * dbus_uint64_t value;
1667 * dbus_message_iter_get_basic (&read_iter, &value);
1668 * type = dbus_message_iter_get_arg_type (&read_iter);
1669 * dbus_message_iter_append_basic (&write_iter, type, &value);
1673 * To avoid the #DBUS_HAVE_INT64 conditional, create a struct or
1674 * something that occupies at least 8 bytes, e.g. you could use a
1675 * struct with two int32 values in it. dbus_uint64_t is just one
1676 * example of a type that's large enough to hold any possible value.
1678 * Be sure you have somehow checked that
1679 * dbus_message_iter_get_arg_type() matches the type you are
1680 * expecting, or you'll crash when you try to use an integer as a
1681 * string or something.
1683 * @param iter the iterator
1684 * @param value location to store the value
1687 dbus_message_iter_get_basic (DBusMessageIter *iter,
1690 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1692 _dbus_return_if_fail (_dbus_message_iter_check (real));
1693 _dbus_return_if_fail (value != NULL);
1695 _dbus_type_reader_read_basic (&real->u.reader,
1700 * Returns the number of elements in the array;
1702 * @param iter the iterator
1703 * @returns the number of elements in the array
1706 dbus_message_iter_get_array_len (DBusMessageIter *iter)
1708 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1710 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
1712 return _dbus_type_reader_get_array_length (&real->u.reader);
1716 * Reads a block of fixed-length values from the message iterator.
1717 * Fixed-length values are those basic types that are not string-like,
1718 * such as integers, bool, double. The block read will be from the
1719 * current position in the array until the end of the array.
1721 * This function should only be used if #dbus_type_is_fixed returns
1722 * #TRUE for the element type.
1724 * The value argument should be the address of a location to store the
1725 * returned array. So for int32 it should be a "const dbus_int32_t**"
1726 * The returned value is by reference and should not be freed.
1728 * @param iter the iterator
1729 * @param value location to store the block
1730 * @param n_elements number of elements in the block
1733 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
1737 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1738 int subtype = _dbus_type_reader_get_current_type(&real->u.reader);
1740 _dbus_return_if_fail (_dbus_message_iter_check (real));
1741 _dbus_return_if_fail (value != NULL);
1742 _dbus_return_if_fail ((subtype == DBUS_TYPE_INVALID) ||
1743 dbus_type_is_fixed (subtype));
1745 _dbus_type_reader_read_fixed_multi (&real->u.reader,
1750 * This function takes a va_list for use by language bindings and is
1751 * otherwise the same as dbus_message_iter_get_args().
1752 * dbus_message_get_args() is the place to go for complete
1755 * @see dbus_message_get_args
1756 * @param iter the message iter
1757 * @param error error to be filled in
1758 * @param first_arg_type type of the first argument
1759 * @param var_args return location for first argument, followed by list of type/location pairs
1760 * @returns #FALSE if error was set
1763 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1768 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1769 int spec_type, msg_type, i;
1772 _dbus_assert (_dbus_message_iter_check (real));
1776 spec_type = first_arg_type;
1779 while (spec_type != DBUS_TYPE_INVALID)
1781 msg_type = dbus_message_iter_get_arg_type (iter);
1783 if (msg_type != spec_type)
1785 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1786 "Argument %d is specified to be of type \"%s\", but "
1787 "is actually of type \"%s\"\n", i,
1788 _dbus_type_to_string (spec_type),
1789 _dbus_type_to_string (msg_type));
1794 if (dbus_type_is_basic (spec_type))
1796 DBusBasicValue *ptr;
1798 ptr = va_arg (var_args, DBusBasicValue*);
1800 _dbus_assert (ptr != NULL);
1802 _dbus_type_reader_read_basic (&real->u.reader,
1805 else if (spec_type == DBUS_TYPE_ARRAY)
1808 int spec_element_type;
1809 const DBusBasicValue **ptr;
1811 DBusTypeReader array;
1813 spec_element_type = va_arg (var_args, int);
1814 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1816 if (spec_element_type != element_type)
1818 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1819 "Argument %d is specified to be an array of \"%s\", but "
1820 "is actually an array of \"%s\"\n",
1822 _dbus_type_to_string (spec_element_type),
1823 _dbus_type_to_string (element_type));
1828 if (dbus_type_is_fixed (spec_element_type))
1830 ptr = va_arg (var_args, const DBusBasicValue**);
1831 n_elements_p = va_arg (var_args, int*);
1833 _dbus_assert (ptr != NULL);
1834 _dbus_assert (n_elements_p != NULL);
1836 _dbus_type_reader_recurse (&real->u.reader, &array);
1838 _dbus_type_reader_read_fixed_multi (&array,
1841 else if (spec_element_type == DBUS_TYPE_STRING ||
1842 spec_element_type == DBUS_TYPE_SIGNATURE ||
1843 spec_element_type == DBUS_TYPE_OBJECT_PATH)
1845 char ***str_array_p;
1849 str_array_p = va_arg (var_args, char***);
1850 n_elements_p = va_arg (var_args, int*);
1852 _dbus_assert (str_array_p != NULL);
1853 _dbus_assert (n_elements_p != NULL);
1855 /* Count elements in the array */
1856 _dbus_type_reader_recurse (&real->u.reader, &array);
1859 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
1862 _dbus_type_reader_next (&array);
1865 str_array = dbus_new0 (char*, n_elements + 1);
1866 if (str_array == NULL)
1868 _DBUS_SET_OOM (error);
1872 /* Now go through and dup each string */
1873 _dbus_type_reader_recurse (&real->u.reader, &array);
1876 while (i < n_elements)
1879 _dbus_type_reader_read_basic (&array,
1882 str_array[i] = _dbus_strdup (s);
1883 if (str_array[i] == NULL)
1885 dbus_free_string_array (str_array);
1886 _DBUS_SET_OOM (error);
1892 if (!_dbus_type_reader_next (&array))
1893 _dbus_assert (i == n_elements);
1896 _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
1897 _dbus_assert (i == n_elements);
1898 _dbus_assert (str_array[i] == NULL);
1900 *str_array_p = str_array;
1901 *n_elements_p = n_elements;
1903 #ifndef DBUS_DISABLE_CHECKS
1906 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
1907 _DBUS_FUNCTION_NAME);
1912 #ifndef DBUS_DISABLE_CHECKS
1915 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
1916 _DBUS_FUNCTION_NAME);
1921 spec_type = va_arg (var_args, int);
1922 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1924 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1925 "Message has only %d arguments, but more were expected", i);
1940 * Initializes a #DBusMessageIter for appending arguments to the end
1943 * @todo If appending any of the arguments fails due to lack of
1944 * memory, generally the message is hosed and you have to start over
1945 * building the whole message.
1947 * @param message the message
1948 * @param iter pointer to an iterator to initialize
1951 dbus_message_iter_init_append (DBusMessage *message,
1952 DBusMessageIter *iter)
1954 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1956 _dbus_return_if_fail (message != NULL);
1957 _dbus_return_if_fail (iter != NULL);
1959 _dbus_message_iter_init_common (message, real,
1960 DBUS_MESSAGE_ITER_TYPE_WRITER);
1962 /* We create the signature string and point iterators at it "on demand"
1963 * when a value is actually appended. That means that init() never fails
1966 _dbus_type_writer_init_types_delayed (&real->u.writer,
1967 message->byte_order,
1969 _dbus_string_get_length (&message->body));
1973 * Creates a temporary signature string containing the current
1974 * signature, stores it in the iterator, and points the iterator to
1975 * the end of it. Used any time we write to the message.
1977 * @param real an iterator without a type_str
1978 * @returns #FALSE if no memory
1981 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
1984 const DBusString *current_sig;
1985 int current_sig_pos;
1987 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1989 if (real->u.writer.type_str != NULL)
1991 _dbus_assert (real->sig_refcount > 0);
1992 real->sig_refcount += 1;
1996 str = dbus_new (DBusString, 1);
2000 if (!_dbus_header_get_field_raw (&real->message->header,
2001 DBUS_HEADER_FIELD_SIGNATURE,
2002 ¤t_sig, ¤t_sig_pos))
2009 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2010 current_sig_pos += 1; /* move on to sig data */
2012 if (!_dbus_string_init_preallocated (str, current_len + 4))
2018 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2021 _dbus_string_free (str);
2028 if (!_dbus_string_init_preallocated (str, 4))
2035 real->sig_refcount = 1;
2037 _dbus_type_writer_add_types (&real->u.writer,
2038 str, _dbus_string_get_length (str));
2043 * Sets the new signature as the message signature, frees the
2044 * signature string, and marks the iterator as not having a type_str
2045 * anymore. Frees the signature even if it fails, so you can't
2046 * really recover from failure. Kinda busted.
2048 * @param real an iterator without a type_str
2049 * @returns #FALSE if no memory
2052 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2055 const char *v_STRING;
2058 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2059 _dbus_assert (real->u.writer.type_str != NULL);
2060 _dbus_assert (real->sig_refcount > 0);
2062 real->sig_refcount -= 1;
2064 if (real->sig_refcount > 0)
2066 _dbus_assert (real->sig_refcount == 0);
2070 str = real->u.writer.type_str;
2072 v_STRING = _dbus_string_get_const_data (str);
2073 if (!_dbus_header_set_field_basic (&real->message->header,
2074 DBUS_HEADER_FIELD_SIGNATURE,
2075 DBUS_TYPE_SIGNATURE,
2079 _dbus_type_writer_remove_types (&real->u.writer);
2080 _dbus_string_free (str);
2086 #ifndef DBUS_DISABLE_CHECKS
2088 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2090 if (!_dbus_message_iter_check (iter))
2093 if (iter->message->locked)
2095 _dbus_warn ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2101 #endif /* DBUS_DISABLE_CHECKS */
2104 * Appends a basic-typed value to the message. The basic types are the
2105 * non-container types such as integer and string.
2107 * The "value" argument should be the address of a basic-typed value.
2108 * So for string, const char**. For integer, dbus_int32_t*.
2110 * @todo If this fails due to lack of memory, the message is hosed and
2111 * you have to start over building the whole message.
2113 * @param iter the append iterator
2114 * @param type the type of the value
2115 * @param value the address of the value
2116 * @returns #FALSE if not enough memory
2119 dbus_message_iter_append_basic (DBusMessageIter *iter,
2123 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2126 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2127 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2128 _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2129 _dbus_return_val_if_fail (value != NULL, FALSE);
2131 if (!_dbus_message_iter_open_signature (real))
2134 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2136 if (!_dbus_message_iter_close_signature (real))
2143 * Appends a block of fixed-length values to an array. The
2144 * fixed-length types are all basic types that are not string-like. So
2145 * int32, double, bool, etc. You must call
2146 * dbus_message_iter_open_container() to open an array of values
2147 * before calling this function. You may call this function multiple
2148 * times (and intermixed with calls to
2149 * dbus_message_iter_append_basic()) for the same array.
2151 * The "value" argument should be the address of the array. So for
2152 * integer, "dbus_int32_t**" is expected for example.
2154 * @warning in C, given "int array[]", "&array == array" (the
2155 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2156 * So if you're using an array instead of a pointer you have to create
2157 * a pointer variable, assign the array to it, then take the address
2158 * of the pointer variable.
2160 * const dbus_int32_t array[] = { 1, 2, 3 };
2161 * const dbus_int32_t *v_ARRAY = array;
2162 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2163 * fprintf (stderr, "No memory!\n");
2165 * For strings it works to write const char *array = "Hello" and then
2166 * use &array though.
2168 * @todo If this fails due to lack of memory, the message is hosed and
2169 * you have to start over building the whole message.
2171 * @param iter the append iterator
2172 * @param element_type the type of the array elements
2173 * @param value the address of the array
2174 * @param n_elements the number of elements to append
2175 * @returns #FALSE if not enough memory
2178 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2183 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2186 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2187 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2188 _dbus_return_val_if_fail (dbus_type_is_fixed (element_type), FALSE);
2189 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2190 _dbus_return_val_if_fail (value != NULL, FALSE);
2191 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2192 _dbus_return_val_if_fail (n_elements <=
2193 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2196 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2202 * Appends a container-typed value to the message; you are required to
2203 * append the contents of the container using the returned
2204 * sub-iterator, and then call
2205 * dbus_message_iter_close_container(). Container types are for
2206 * example struct, variant, and array. For variants, the
2207 * contained_signature should be the type of the single value inside
2208 * the variant. For structs and dict entries, contained_signature
2209 * should be #NULL; it will be set to whatever types you write into
2210 * the struct. For arrays, contained_signature should be the type of
2211 * the array elements.
2213 * @todo If this fails due to lack of memory, the message is hosed and
2214 * you have to start over building the whole message.
2216 * @param iter the append iterator
2217 * @param type the type of the value
2218 * @param contained_signature the type of container contents
2219 * @param sub sub-iterator to initialize
2220 * @returns #FALSE if not enough memory
2223 dbus_message_iter_open_container (DBusMessageIter *iter,
2225 const char *contained_signature,
2226 DBusMessageIter *sub)
2228 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2229 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2230 DBusString contained_str;
2232 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2233 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2234 _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
2235 _dbus_return_val_if_fail (sub != NULL, FALSE);
2236 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2237 contained_signature == NULL) ||
2238 (type == DBUS_TYPE_DICT_ENTRY &&
2239 contained_signature == NULL) ||
2240 contained_signature != NULL, FALSE);
2243 /* FIXME this would fail if the contained_signature is a dict entry,
2244 * since dict entries are invalid signatures standalone (they must be in
2247 _dbus_return_val_if_fail (contained_signature == NULL ||
2248 _dbus_check_is_valid_signature (contained_signature));
2251 if (!_dbus_message_iter_open_signature (real))
2256 if (contained_signature != NULL)
2258 _dbus_string_init_const (&contained_str, contained_signature);
2260 return _dbus_type_writer_recurse (&real->u.writer,
2263 &real_sub->u.writer);
2267 return _dbus_type_writer_recurse (&real->u.writer,
2270 &real_sub->u.writer);
2276 * Closes a container-typed value appended to the message; may write
2277 * out more information to the message known only after the entire
2278 * container is written, and may free resources created by
2279 * dbus_message_iter_open_container().
2281 * @todo If this fails due to lack of memory, the message is hosed and
2282 * you have to start over building the whole message.
2284 * @param iter the append iterator
2285 * @param sub sub-iterator to close
2286 * @returns #FALSE if not enough memory
2289 dbus_message_iter_close_container (DBusMessageIter *iter,
2290 DBusMessageIter *sub)
2292 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2293 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2296 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2297 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2298 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2299 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2301 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2302 &real_sub->u.writer);
2304 if (!_dbus_message_iter_close_signature (real))
2311 * Sets a flag indicating that the message does not want a reply; if
2312 * this flag is set, the other end of the connection may (but is not
2313 * required to) optimize by not sending method return or error
2314 * replies. If this flag is set, there is no way to know whether the
2315 * message successfully arrived at the remote end. Normally you know a
2316 * message was received when you receive the reply to it.
2318 * @param message the message
2319 * @param no_reply #TRUE if no reply is desired
2322 dbus_message_set_no_reply (DBusMessage *message,
2323 dbus_bool_t no_reply)
2325 _dbus_return_if_fail (message != NULL);
2326 _dbus_return_if_fail (!message->locked);
2328 _dbus_header_toggle_flag (&message->header,
2329 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2334 * Returns #TRUE if the message does not expect
2337 * @param message the message
2338 * @returns #TRUE if the message sender isn't waiting for a reply
2341 dbus_message_get_no_reply (DBusMessage *message)
2343 _dbus_return_val_if_fail (message != NULL, FALSE);
2345 return _dbus_header_get_flag (&message->header,
2346 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2350 * Sets a flag indicating that an owner for the destination name will
2351 * be automatically started before the message is delivered. When this
2352 * flag is set, the message is held until a name owner finishes
2353 * starting up, or fails to start up. In case of failure, the reply
2356 * @param message the message
2357 * @param auto_start #TRUE if auto-starting is desired
2360 dbus_message_set_auto_start (DBusMessage *message,
2361 dbus_bool_t auto_start)
2363 _dbus_return_if_fail (message != NULL);
2364 _dbus_return_if_fail (!message->locked);
2366 _dbus_header_toggle_flag (&message->header,
2367 DBUS_HEADER_FLAG_NO_AUTO_START,
2372 * Returns #TRUE if the message will cause an owner for
2373 * destination name to be auto-started.
2375 * @param message the message
2376 * @returns #TRUE if the message will use auto-start
2379 dbus_message_get_auto_start (DBusMessage *message)
2381 _dbus_return_val_if_fail (message != NULL, FALSE);
2383 return !_dbus_header_get_flag (&message->header,
2384 DBUS_HEADER_FLAG_NO_AUTO_START);
2389 * Sets the object path this message is being sent to (for
2390 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2391 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2393 * @param message the message
2394 * @param object_path the path or #NULL to unset
2395 * @returns #FALSE if not enough memory
2398 dbus_message_set_path (DBusMessage *message,
2399 const char *object_path)
2401 _dbus_return_val_if_fail (message != NULL, FALSE);
2402 _dbus_return_val_if_fail (!message->locked, FALSE);
2403 _dbus_return_val_if_fail (object_path == NULL ||
2404 _dbus_check_is_valid_path (object_path),
2407 return set_or_delete_string_field (message,
2408 DBUS_HEADER_FIELD_PATH,
2409 DBUS_TYPE_OBJECT_PATH,
2414 * Gets the object path this message is being sent to (for
2415 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2416 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2418 * @param message the message
2419 * @returns the path (should not be freed) or #NULL
2422 dbus_message_get_path (DBusMessage *message)
2426 _dbus_return_val_if_fail (message != NULL, NULL);
2428 v = NULL; /* in case field doesn't exist */
2429 _dbus_header_get_field_basic (&message->header,
2430 DBUS_HEADER_FIELD_PATH,
2431 DBUS_TYPE_OBJECT_PATH,
2437 * Checks if the message has a path
2439 * @param message the message
2440 * @param path the path name
2441 * @returns #TRUE if there is a path field in the header
2444 dbus_message_has_path (DBusMessage *message,
2447 const char *msg_path;
2448 msg_path = dbus_message_get_path (message);
2450 if (msg_path == NULL)
2461 if (strcmp (msg_path, path) == 0)
2468 * Gets the object path this message is being sent to
2469 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2470 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2471 * format (one array element per path component).
2472 * Free the returned array with dbus_free_string_array().
2474 * An empty but non-NULL path array means the path "/".
2475 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2476 * and the path "/" becomes { NULL }.
2478 * @todo this could be optimized by using the len from the message
2479 * instead of calling strlen() again
2481 * @param message the message
2482 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2483 * @returns #FALSE if no memory to allocate the array
2486 dbus_message_get_path_decomposed (DBusMessage *message,
2491 _dbus_return_val_if_fail (message != NULL, FALSE);
2492 _dbus_return_val_if_fail (path != NULL, FALSE);
2496 v = dbus_message_get_path (message);
2499 if (!_dbus_decompose_path (v, strlen (v),
2507 * Sets the interface this message is being sent to
2508 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2509 * the interface a signal is being emitted from
2510 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2512 * @param message the message
2513 * @param interface the interface or #NULL to unset
2514 * @returns #FALSE if not enough memory
2517 dbus_message_set_interface (DBusMessage *message,
2518 const char *interface)
2520 _dbus_return_val_if_fail (message != NULL, FALSE);
2521 _dbus_return_val_if_fail (!message->locked, FALSE);
2522 _dbus_return_val_if_fail (interface == NULL ||
2523 _dbus_check_is_valid_interface (interface),
2526 return set_or_delete_string_field (message,
2527 DBUS_HEADER_FIELD_INTERFACE,
2533 * Gets the interface this message is being sent to
2534 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2535 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2536 * The interface name is fully-qualified (namespaced).
2537 * Returns #NULL if none.
2539 * @param message the message
2540 * @returns the message interface (should not be freed) or #NULL
2543 dbus_message_get_interface (DBusMessage *message)
2547 _dbus_return_val_if_fail (message != NULL, NULL);
2549 v = NULL; /* in case field doesn't exist */
2550 _dbus_header_get_field_basic (&message->header,
2551 DBUS_HEADER_FIELD_INTERFACE,
2558 * Checks if the message has an interface
2560 * @param message the message
2561 * @param interface the interface name
2562 * @returns #TRUE if there is a interface field in the header
2565 dbus_message_has_interface (DBusMessage *message,
2566 const char *interface)
2568 const char *msg_interface;
2569 msg_interface = dbus_message_get_interface (message);
2571 if (msg_interface == NULL)
2573 if (interface == NULL)
2579 if (interface == NULL)
2582 if (strcmp (msg_interface, interface) == 0)
2590 * Sets the interface member being invoked
2591 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2592 * (DBUS_MESSAGE_TYPE_SIGNAL).
2593 * The interface name is fully-qualified (namespaced).
2595 * @param message the message
2596 * @param member the member or #NULL to unset
2597 * @returns #FALSE if not enough memory
2600 dbus_message_set_member (DBusMessage *message,
2603 _dbus_return_val_if_fail (message != NULL, FALSE);
2604 _dbus_return_val_if_fail (!message->locked, FALSE);
2605 _dbus_return_val_if_fail (member == NULL ||
2606 _dbus_check_is_valid_member (member),
2609 return set_or_delete_string_field (message,
2610 DBUS_HEADER_FIELD_MEMBER,
2616 * Gets the interface member being invoked
2617 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2618 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2620 * @param message the message
2621 * @returns the member name (should not be freed) or #NULL
2624 dbus_message_get_member (DBusMessage *message)
2628 _dbus_return_val_if_fail (message != NULL, NULL);
2630 v = NULL; /* in case field doesn't exist */
2631 _dbus_header_get_field_basic (&message->header,
2632 DBUS_HEADER_FIELD_MEMBER,
2639 * Checks if the message has an interface member
2641 * @param message the message
2642 * @param member the member name
2643 * @returns #TRUE if there is a member field in the header
2646 dbus_message_has_member (DBusMessage *message,
2649 const char *msg_member;
2650 msg_member = dbus_message_get_member (message);
2652 if (msg_member == NULL)
2663 if (strcmp (msg_member, member) == 0)
2671 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2672 * The name is fully-qualified (namespaced).
2674 * @param message the message
2675 * @param error_name the name or #NULL to unset
2676 * @returns #FALSE if not enough memory
2679 dbus_message_set_error_name (DBusMessage *message,
2680 const char *error_name)
2682 _dbus_return_val_if_fail (message != NULL, FALSE);
2683 _dbus_return_val_if_fail (!message->locked, FALSE);
2684 _dbus_return_val_if_fail (error_name == NULL ||
2685 _dbus_check_is_valid_error_name (error_name),
2688 return set_or_delete_string_field (message,
2689 DBUS_HEADER_FIELD_ERROR_NAME,
2695 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2698 * @param message the message
2699 * @returns the error name (should not be freed) or #NULL
2702 dbus_message_get_error_name (DBusMessage *message)
2706 _dbus_return_val_if_fail (message != NULL, NULL);
2708 v = NULL; /* in case field doesn't exist */
2709 _dbus_header_get_field_basic (&message->header,
2710 DBUS_HEADER_FIELD_ERROR_NAME,
2717 * Sets the message's destination. The destination is the name of
2718 * another connection on the bus and may be either the unique name
2719 * assigned by the bus to each connection, or a well-known name
2720 * specified in advance.
2722 * @param message the message
2723 * @param destination the destination name or #NULL to unset
2724 * @returns #FALSE if not enough memory
2727 dbus_message_set_destination (DBusMessage *message,
2728 const char *destination)
2730 _dbus_return_val_if_fail (message != NULL, FALSE);
2731 _dbus_return_val_if_fail (!message->locked, FALSE);
2732 _dbus_return_val_if_fail (destination == NULL ||
2733 _dbus_check_is_valid_bus_name (destination),
2736 return set_or_delete_string_field (message,
2737 DBUS_HEADER_FIELD_DESTINATION,
2743 * Gets the destination of a message or #NULL if there is none set.
2745 * @param message the message
2746 * @returns the message destination (should not be freed) or #NULL
2749 dbus_message_get_destination (DBusMessage *message)
2753 _dbus_return_val_if_fail (message != NULL, NULL);
2755 v = NULL; /* in case field doesn't exist */
2756 _dbus_header_get_field_basic (&message->header,
2757 DBUS_HEADER_FIELD_DESTINATION,
2764 * Sets the message sender.
2766 * @param message the message
2767 * @param sender the sender or #NULL to unset
2768 * @returns #FALSE if not enough memory
2771 dbus_message_set_sender (DBusMessage *message,
2774 _dbus_return_val_if_fail (message != NULL, FALSE);
2775 _dbus_return_val_if_fail (!message->locked, FALSE);
2776 _dbus_return_val_if_fail (sender == NULL ||
2777 _dbus_check_is_valid_bus_name (sender),
2780 return set_or_delete_string_field (message,
2781 DBUS_HEADER_FIELD_SENDER,
2787 * Gets the unique name of the connection which originated this
2788 * message, or #NULL if unknown or inapplicable. The sender is filled
2789 * in by the message bus.
2791 * @param message the message
2792 * @returns the unique name of the sender or #NULL
2795 dbus_message_get_sender (DBusMessage *message)
2799 _dbus_return_val_if_fail (message != NULL, NULL);
2801 v = NULL; /* in case field doesn't exist */
2802 _dbus_header_get_field_basic (&message->header,
2803 DBUS_HEADER_FIELD_SENDER,
2810 * Gets the type signature of the message, i.e. the arguments in the
2811 * message payload. The signature includes only "in" arguments for
2812 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2813 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2814 * what you might expect (it does not include the signature of the
2815 * entire C++-style method).
2817 * The signature is a string made up of type codes such as
2818 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2819 * the value of #DBUS_TYPE_INVALID).
2821 * @param message the message
2822 * @returns the type signature
2825 dbus_message_get_signature (DBusMessage *message)
2827 const DBusString *type_str;
2830 _dbus_return_val_if_fail (message != NULL, NULL);
2832 get_const_signature (&message->header, &type_str, &type_pos);
2834 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
2838 _dbus_message_has_type_interface_member (DBusMessage *message,
2840 const char *interface,
2845 _dbus_assert (message != NULL);
2846 _dbus_assert (interface != NULL);
2847 _dbus_assert (member != NULL);
2849 if (dbus_message_get_type (message) != type)
2852 /* Optimize by checking the short member name first
2853 * instead of the longer interface name
2856 n = dbus_message_get_member (message);
2858 if (n && strcmp (n, member) == 0)
2860 n = dbus_message_get_interface (message);
2862 if (n == NULL || strcmp (n, interface) == 0)
2870 * Checks whether the message is a method call with the given
2871 * interface and member fields. If the message is not
2872 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
2873 * member field, returns #FALSE. If the interface field is missing,
2874 * then it will be assumed equal to the provided interface. The D-Bus
2875 * protocol allows method callers to leave out the interface name.
2877 * @param message the message
2878 * @param interface the name to check (must not be #NULL)
2879 * @param method the name to check (must not be #NULL)
2881 * @returns #TRUE if the message is the specified method call
2884 dbus_message_is_method_call (DBusMessage *message,
2885 const char *interface,
2888 _dbus_return_val_if_fail (message != NULL, FALSE);
2889 _dbus_return_val_if_fail (interface != NULL, FALSE);
2890 _dbus_return_val_if_fail (method != NULL, FALSE);
2891 /* don't check that interface/method are valid since it would be
2892 * expensive, and not catch many common errors
2895 return _dbus_message_has_type_interface_member (message,
2896 DBUS_MESSAGE_TYPE_METHOD_CALL,
2901 * Checks whether the message is a signal with the given interface and
2902 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
2903 * has a different interface or member field, returns #FALSE. If the
2904 * interface field in the message is missing, it is assumed to match
2905 * any interface you pass in to this function.
2907 * @param message the message
2908 * @param interface the name to check (must not be #NULL)
2909 * @param signal_name the name to check (must not be #NULL)
2911 * @returns #TRUE if the message is the specified signal
2914 dbus_message_is_signal (DBusMessage *message,
2915 const char *interface,
2916 const char *signal_name)
2918 _dbus_return_val_if_fail (message != NULL, FALSE);
2919 _dbus_return_val_if_fail (interface != NULL, FALSE);
2920 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
2921 /* don't check that interface/name are valid since it would be
2922 * expensive, and not catch many common errors
2925 return _dbus_message_has_type_interface_member (message,
2926 DBUS_MESSAGE_TYPE_SIGNAL,
2927 interface, signal_name);
2931 * Checks whether the message is an error reply with the given error
2932 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
2933 * different name, returns #FALSE.
2935 * @param message the message
2936 * @param error_name the name to check (must not be #NULL)
2938 * @returns #TRUE if the message is the specified error
2941 dbus_message_is_error (DBusMessage *message,
2942 const char *error_name)
2946 _dbus_return_val_if_fail (message != NULL, FALSE);
2947 _dbus_return_val_if_fail (error_name != NULL, FALSE);
2948 /* don't check that error_name is valid since it would be expensive,
2949 * and not catch many common errors
2952 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2955 n = dbus_message_get_error_name (message);
2957 if (n && strcmp (n, error_name) == 0)
2964 * Checks whether the message was sent to the given name. If the
2965 * message has no destination specified or has a different
2966 * destination, returns #FALSE.
2968 * @param message the message
2969 * @param name the name to check (must not be #NULL)
2971 * @returns #TRUE if the message has the given destination name
2974 dbus_message_has_destination (DBusMessage *message,
2979 _dbus_return_val_if_fail (message != NULL, FALSE);
2980 _dbus_return_val_if_fail (name != NULL, FALSE);
2981 /* don't check that name is valid since it would be expensive, and
2982 * not catch many common errors
2985 s = dbus_message_get_destination (message);
2987 if (s && strcmp (s, name) == 0)
2994 * Checks whether the message has the given unique name as its sender.
2995 * If the message has no sender specified or has a different sender,
2996 * returns #FALSE. Note that a peer application will always have the
2997 * unique name of the connection as the sender. So you can't use this
2998 * function to see whether a sender owned a well-known name.
3000 * Messages from the bus itself will have #DBUS_SERVICE_DBUS
3003 * @param message the message
3004 * @param name the name to check (must not be #NULL)
3006 * @returns #TRUE if the message has the given sender
3009 dbus_message_has_sender (DBusMessage *message,
3014 _dbus_return_val_if_fail (message != NULL, FALSE);
3015 _dbus_return_val_if_fail (name != NULL, FALSE);
3016 /* don't check that name is valid since it would be expensive, and
3017 * not catch many common errors
3020 s = dbus_message_get_sender (message);
3022 if (s && strcmp (s, name) == 0)
3029 * Checks whether the message has the given signature; see
3030 * dbus_message_get_signature() for more details on what the signature
3033 * @param message the message
3034 * @param signature typecode array
3035 * @returns #TRUE if message has the given signature
3038 dbus_message_has_signature (DBusMessage *message,
3039 const char *signature)
3043 _dbus_return_val_if_fail (message != NULL, FALSE);
3044 _dbus_return_val_if_fail (signature != NULL, FALSE);
3045 /* don't check that signature is valid since it would be expensive,
3046 * and not catch many common errors
3049 s = dbus_message_get_signature (message);
3051 if (s && strcmp (s, signature) == 0)
3058 * Sets a #DBusError based on the contents of the given
3059 * message. The error is only set if the message
3060 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
3061 * The name of the error is set to the name of the message,
3062 * and the error message is set to the first argument
3063 * if the argument exists and is a string.
3065 * The return value indicates whether the error was set (the error is
3066 * set if and only if the message is an error message). So you can
3067 * check for an error reply and convert it to DBusError in one go:
3069 * if (dbus_set_error_from_message (error, reply))
3075 * @param error the error to set
3076 * @param message the message to set it from
3077 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
3080 dbus_set_error_from_message (DBusError *error,
3081 DBusMessage *message)
3085 _dbus_return_val_if_fail (message != NULL, FALSE);
3086 _dbus_return_val_if_error_is_set (error, FALSE);
3088 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3092 dbus_message_get_args (message, NULL,
3093 DBUS_TYPE_STRING, &str,
3096 dbus_set_error (error, dbus_message_get_error_name (message),
3097 str ? "%s" : NULL, str);
3105 * @addtogroup DBusMessageInternals
3111 * The initial buffer size of the message loader.
3113 * @todo this should be based on min header size plus some average
3114 * body size, or something. Or rather, the min header size only, if we
3115 * want to try to read only the header, store that in a DBusMessage,
3116 * then read only the body and store that, etc., depends on
3117 * how we optimize _dbus_message_loader_get_buffer() and what
3118 * the exact message format is.
3120 #define INITIAL_LOADER_DATA_LEN 32
3123 * Creates a new message loader. Returns #NULL if memory can't
3126 * @returns new loader, or #NULL.
3129 _dbus_message_loader_new (void)
3131 DBusMessageLoader *loader;
3133 loader = dbus_new0 (DBusMessageLoader, 1);
3137 loader->refcount = 1;
3139 loader->corrupted = FALSE;
3140 loader->corruption_reason = DBUS_VALID;
3142 /* this can be configured by the app, but defaults to the protocol max */
3143 loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3145 if (!_dbus_string_init (&loader->data))
3151 /* preallocate the buffer for speed, ignore failure */
3152 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3153 _dbus_string_set_length (&loader->data, 0);
3159 * Increments the reference count of the loader.
3161 * @param loader the loader.
3162 * @returns the loader
3165 _dbus_message_loader_ref (DBusMessageLoader *loader)
3167 loader->refcount += 1;
3173 * Decrements the reference count of the loader and finalizes the
3174 * loader when the count reaches zero.
3176 * @param loader the loader.
3179 _dbus_message_loader_unref (DBusMessageLoader *loader)
3181 loader->refcount -= 1;
3182 if (loader->refcount == 0)
3184 _dbus_list_foreach (&loader->messages,
3185 (DBusForeachFunction) dbus_message_unref,
3187 _dbus_list_clear (&loader->messages);
3188 _dbus_string_free (&loader->data);
3194 * Gets the buffer to use for reading data from the network. Network
3195 * data is read directly into an allocated buffer, which is then used
3196 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3197 * The buffer must always be returned immediately using
3198 * _dbus_message_loader_return_buffer(), even if no bytes are
3199 * successfully read.
3201 * @todo this function can be a lot more clever. For example
3202 * it can probably always return a buffer size to read exactly
3203 * the body of the next message, thus avoiding any memory wastage
3206 * @todo we need to enforce a max length on strings in header fields.
3208 * @param loader the message loader.
3209 * @param buffer the buffer
3212 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3213 DBusString **buffer)
3215 _dbus_assert (!loader->buffer_outstanding);
3217 *buffer = &loader->data;
3219 loader->buffer_outstanding = TRUE;
3223 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3224 * indicating to the loader how many bytes of the buffer were filled
3225 * in. This function must always be called, even if no bytes were
3226 * successfully read.
3228 * @param loader the loader.
3229 * @param buffer the buffer.
3230 * @param bytes_read number of bytes that were read into the buffer.
3233 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3237 _dbus_assert (loader->buffer_outstanding);
3238 _dbus_assert (buffer == &loader->data);
3240 loader->buffer_outstanding = FALSE;
3244 * FIXME when we move the header out of the buffer, that memmoves all
3245 * buffered messages. Kind of crappy.
3247 * Also we copy the header and body, which is kind of crappy. To
3248 * avoid this, we have to allow header and body to be in a single
3249 * memory block, which is good for messages we read and bad for
3250 * messages we are creating. But we could move_len() the buffer into
3251 * this single memory block, and move_len() will just swap the buffers
3252 * if you're moving the entire buffer replacing the dest string.
3254 * We could also have the message loader tell the transport how many
3255 * bytes to read; so it would first ask for some arbitrary number like
3256 * 256, then if the message was incomplete it would use the
3257 * header/body len to ask for exactly the size of the message (or
3258 * blocks the size of a typical kernel buffer for the socket). That
3259 * way we don't get trailing bytes in the buffer that have to be
3260 * memmoved. Though I suppose we also don't have a chance of reading a
3261 * bunch of small messages at once, so the optimization may be stupid.
3263 * Another approach would be to keep a "start" index into
3264 * loader->data and only delete it occasionally, instead of after
3265 * each message is loaded.
3267 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3270 load_message (DBusMessageLoader *loader,
3271 DBusMessage *message,
3273 int fields_array_len,
3278 DBusValidity validity;
3279 const DBusString *type_str;
3281 DBusValidationMode mode;
3283 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3288 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3291 /* 1. VALIDATE AND COPY OVER HEADER */
3292 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3293 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3295 if (!_dbus_header_load (&message->header,
3303 _dbus_string_get_length (&loader->data)))
3305 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3307 /* assert here so we can catch any code that still uses DBUS_VALID to indicate
3308 oom errors. They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
3309 _dbus_assert (validity != DBUS_VALID);
3311 if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
3315 loader->corrupted = TRUE;
3316 loader->corruption_reason = validity;
3321 _dbus_assert (validity == DBUS_VALID);
3323 message->byte_order = byte_order;
3325 /* 2. VALIDATE BODY */
3326 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3328 get_const_signature (&message->header, &type_str, &type_pos);
3330 /* Because the bytes_remaining arg is NULL, this validates that the
3331 * body is the right length
3333 validity = _dbus_validate_body_with_reason (type_str,
3340 if (validity != DBUS_VALID)
3342 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3344 loader->corrupted = TRUE;
3345 loader->corruption_reason = validity;
3351 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3353 if (!_dbus_list_append (&loader->messages, message))
3355 _dbus_verbose ("Failed to append new message to loader queue\n");
3360 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3361 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3362 (header_len + body_len));
3364 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3366 _dbus_verbose ("Failed to move body into new message\n");
3371 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3373 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3374 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3376 _dbus_verbose ("Loaded message %p\n", message);
3378 _dbus_assert (!oom);
3379 _dbus_assert (!loader->corrupted);
3380 _dbus_assert (loader->messages != NULL);
3381 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3389 /* does nothing if the message isn't in the list */
3390 _dbus_list_remove_last (&loader->messages, message);
3393 _dbus_assert (!loader->corrupted);
3395 _dbus_assert (loader->corrupted);
3397 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3403 * Converts buffered data into messages, if we have enough data. If
3404 * we don't have enough data, does nothing.
3406 * @todo we need to check that the proper named header fields exist
3407 * for each message type.
3409 * @todo If a message has unknown type, we should probably eat it
3410 * right here rather than passing it out to applications. However
3411 * it's not an error to see messages of unknown type.
3413 * @param loader the loader.
3414 * @returns #TRUE if we had enough memory to finish.
3417 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3419 while (!loader->corrupted &&
3420 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3422 DBusValidity validity;
3423 int byte_order, fields_array_len, header_len, body_len;
3425 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3432 _dbus_string_get_length (&loader->data)))
3434 DBusMessage *message;
3436 _dbus_assert (validity == DBUS_VALID);
3438 message = dbus_message_new_empty_header ();
3439 if (message == NULL)
3442 if (!load_message (loader, message,
3443 byte_order, fields_array_len,
3444 header_len, body_len))
3446 dbus_message_unref (message);
3447 /* load_message() returns false if corrupted or OOM; if
3448 * corrupted then return TRUE for not OOM
3450 return loader->corrupted;
3453 _dbus_assert (loader->messages != NULL);
3454 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3458 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3460 if (validity != DBUS_VALID)
3462 loader->corrupted = TRUE;
3463 loader->corruption_reason = validity;
3473 * Peeks at first loaded message, returns #NULL if no messages have
3476 * @param loader the loader.
3477 * @returns the next message, or #NULL if none.
3480 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3482 if (loader->messages)
3483 return loader->messages->data;
3489 * Pops a loaded message (passing ownership of the message
3490 * to the caller). Returns #NULL if no messages have been
3493 * @param loader the loader.
3494 * @returns the next message, or #NULL if none.
3497 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3499 return _dbus_list_pop_first (&loader->messages);
3503 * Pops a loaded message inside a list link (passing ownership of the
3504 * message and link to the caller). Returns #NULL if no messages have
3507 * @param loader the loader.
3508 * @returns the next message link, or #NULL if none.
3511 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3513 return _dbus_list_pop_first_link (&loader->messages);
3517 * Returns a popped message link, used to undo a pop.
3519 * @param loader the loader
3520 * @param link the link with a message in it
3523 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3526 _dbus_list_prepend_link (&loader->messages, link);
3530 * Checks whether the loader is confused due to bad data.
3531 * If messages are received that are invalid, the
3532 * loader gets confused and gives up permanently.
3533 * This state is called "corrupted."
3535 * @param loader the loader
3536 * @returns #TRUE if the loader is hosed.
3539 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3541 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3542 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3543 return loader->corrupted;
3547 * Sets the maximum size message we allow.
3549 * @param loader the loader
3550 * @param size the max message size in bytes
3553 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3556 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3558 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3559 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3560 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3562 loader->max_message_size = size;
3566 * Gets the maximum allowed message size in bytes.
3568 * @param loader the loader
3569 * @returns max size in bytes
3572 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3574 return loader->max_message_size;
3577 static DBusDataSlotAllocator slot_allocator;
3578 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3581 * Allocates an integer ID to be used for storing application-specific
3582 * data on any DBusMessage. The allocated ID may then be used
3583 * with dbus_message_set_data() and dbus_message_get_data().
3584 * The passed-in slot must be initialized to -1, and is filled in
3585 * with the slot ID. If the passed-in slot is not -1, it's assumed
3586 * to be already allocated, and its refcount is incremented.
3588 * The allocated slot is global, i.e. all DBusMessage objects will
3589 * have a slot with the given integer ID reserved.
3591 * @param slot_p address of a global variable storing the slot
3592 * @returns #FALSE on failure (no memory)
3595 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3597 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3598 _DBUS_LOCK_NAME (message_slots),
3603 * Deallocates a global ID for message data slots.
3604 * dbus_message_get_data() and dbus_message_set_data() may no
3605 * longer be used with this slot. Existing data stored on existing
3606 * DBusMessage objects will be freed when the message is
3607 * finalized, but may not be retrieved (and may only be replaced if
3608 * someone else reallocates the slot). When the refcount on the
3609 * passed-in slot reaches 0, it is set to -1.
3611 * @param slot_p address storing the slot to deallocate
3614 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3616 _dbus_return_if_fail (*slot_p >= 0);
3618 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3622 * Stores a pointer on a DBusMessage, along
3623 * with an optional function to be used for freeing
3624 * the data when the data is set again, or when
3625 * the message is finalized. The slot number
3626 * must have been allocated with dbus_message_allocate_data_slot().
3628 * @param message the message
3629 * @param slot the slot number
3630 * @param data the data to store
3631 * @param free_data_func finalizer function for the data
3632 * @returns #TRUE if there was enough memory to store the data
3635 dbus_message_set_data (DBusMessage *message,
3638 DBusFreeFunction free_data_func)
3640 DBusFreeFunction old_free_func;
3644 _dbus_return_val_if_fail (message != NULL, FALSE);
3645 _dbus_return_val_if_fail (slot >= 0, FALSE);
3647 retval = _dbus_data_slot_list_set (&slot_allocator,
3648 &message->slot_list,
3649 slot, data, free_data_func,
3650 &old_free_func, &old_data);
3654 /* Do the actual free outside the message lock */
3656 (* old_free_func) (old_data);
3663 * Retrieves data previously set with dbus_message_set_data().
3664 * The slot must still be allocated (must not have been freed).
3666 * @param message the message
3667 * @param slot the slot to get data from
3668 * @returns the data, or #NULL if not found
3671 dbus_message_get_data (DBusMessage *message,
3676 _dbus_return_val_if_fail (message != NULL, NULL);
3678 res = _dbus_data_slot_list_get (&slot_allocator,
3679 &message->slot_list,
3686 * Utility function to convert a machine-readable (not translated)
3687 * string into a D-Bus message type.
3690 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3691 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3692 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3693 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3694 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3699 dbus_message_type_from_string (const char *type_str)
3701 if (strcmp (type_str, "method_call") == 0)
3702 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3703 if (strcmp (type_str, "method_return") == 0)
3704 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3705 else if (strcmp (type_str, "signal") == 0)
3706 return DBUS_MESSAGE_TYPE_SIGNAL;
3707 else if (strcmp (type_str, "error") == 0)
3708 return DBUS_MESSAGE_TYPE_ERROR;
3710 return DBUS_MESSAGE_TYPE_INVALID;
3714 * Utility function to convert a D-Bus message type into a
3715 * machine-readable string (not translated).
3718 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3719 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3720 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3721 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3722 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3727 dbus_message_type_to_string (int type)
3731 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3732 return "method_call";
3733 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3734 return "method_return";
3735 case DBUS_MESSAGE_TYPE_SIGNAL:
3737 case DBUS_MESSAGE_TYPE_ERROR:
3746 /* tests in dbus-message-util.c */