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-message-private.h"
31 #include "dbus-object-tree.h"
32 #include "dbus-memory.h"
33 #include "dbus-list.h"
37 * @defgroup DBusMessageInternals DBusMessage implementation details
38 * @ingroup DBusInternals
39 * @brief DBusMessage private implementation details.
41 * The guts of DBusMessage and its methods.
46 /* Not thread locked, but strictly const/read-only so should be OK
48 /** An static string representing an empty signature */
49 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
51 /* these have wacky values to help trap uninitialized iterators;
52 * but has to fit in 3 bits
55 DBUS_MESSAGE_ITER_TYPE_READER = 3,
56 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
59 /** typedef for internals of message iterator */
60 typedef struct DBusMessageRealIter DBusMessageRealIter;
63 * @brief Internals of DBusMessageIter
65 * Object representing a position in a message. All fields are internal.
67 struct DBusMessageRealIter
69 DBusMessage *message; /**< Message used */
70 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
71 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
72 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
75 DBusTypeWriter writer; /**< writer */
76 DBusTypeReader reader; /**< reader */
77 } u; /**< the type writer or reader that does all the work */
81 get_const_signature (DBusHeader *header,
82 const DBusString **type_str_p,
85 if (_dbus_header_get_field_raw (header,
86 DBUS_HEADER_FIELD_SIGNATURE,
90 *type_pos_p += 1; /* skip the signature length which is 1 byte */
94 *type_str_p = &_dbus_empty_signature_str;
100 * Swaps the message to compiler byte order if required
102 * @param message the message
105 _dbus_message_byteswap (DBusMessage *message)
107 const DBusString *type_str;
110 if (message->byte_order == DBUS_COMPILER_BYTE_ORDER)
113 _dbus_verbose ("Swapping message into compiler byte order\n");
115 get_const_signature (&message->header, &type_str, &type_pos);
117 _dbus_marshal_byteswap (type_str, type_pos,
119 DBUS_COMPILER_BYTE_ORDER,
122 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
124 _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
127 #define ensure_byte_order(message) \
128 if (message->byte_order != DBUS_COMPILER_BYTE_ORDER) \
129 _dbus_message_byteswap (message)
132 * Gets the data to be sent over the network for this message.
133 * The header and then the body should be written out.
134 * This function is guaranteed to always return the same
135 * data once a message is locked (with _dbus_message_lock()).
137 * @param message the message.
138 * @param header return location for message header data.
139 * @param body return location for message body data.
142 _dbus_message_get_network_data (DBusMessage *message,
143 const DBusString **header,
144 const DBusString **body)
146 _dbus_assert (message->locked);
148 *header = &message->header.data;
149 *body = &message->body;
153 * Sets the serial number of a message.
154 * This can only be done once on a message.
156 * @param message the message
157 * @param serial the serial
160 _dbus_message_set_serial (DBusMessage *message,
161 dbus_uint32_t serial)
163 _dbus_assert (message != NULL);
164 _dbus_assert (!message->locked);
165 _dbus_assert (dbus_message_get_serial (message) == 0);
167 _dbus_header_set_serial (&message->header, serial);
171 * Adds a counter to be incremented immediately with the
172 * size of this message, and decremented by the size
173 * of this message when this message if finalized.
174 * The link contains a counter with its refcount already
175 * incremented, but the counter itself not incremented.
176 * Ownership of link and counter refcount is passed to
179 * @param message the message
180 * @param link link with counter as data
183 _dbus_message_add_size_counter_link (DBusMessage *message,
186 /* right now we don't recompute the delta when message
187 * size changes, and that's OK for current purposes
188 * I think, but could be important to change later.
189 * Do recompute it whenever there are no outstanding counters,
190 * since it's basically free.
192 if (message->size_counters == NULL)
194 message->size_counter_delta =
195 _dbus_string_get_length (&message->header.data) +
196 _dbus_string_get_length (&message->body);
199 _dbus_verbose ("message has size %ld\n",
200 message->size_counter_delta);
204 _dbus_list_append_link (&message->size_counters, link);
206 _dbus_counter_adjust (link->data, message->size_counter_delta);
210 * Adds a counter to be incremented immediately with the
211 * size of this message, and decremented by the size
212 * of this message when this message if finalized.
214 * @param message the message
215 * @param counter the counter
216 * @returns #FALSE if no memory
219 _dbus_message_add_size_counter (DBusMessage *message,
220 DBusCounter *counter)
224 link = _dbus_list_alloc_link (counter);
228 _dbus_counter_ref (counter);
229 _dbus_message_add_size_counter_link (message, link);
235 * Removes a counter tracking the size of this message, and decrements
236 * the counter by the size of this message.
238 * @param message the message
239 * @param link_return return the link used
240 * @param counter the counter
243 _dbus_message_remove_size_counter (DBusMessage *message,
244 DBusCounter *counter,
245 DBusList **link_return)
249 link = _dbus_list_find_last (&message->size_counters,
251 _dbus_assert (link != NULL);
253 _dbus_list_unlink (&message->size_counters,
258 _dbus_list_free_link (link);
260 _dbus_counter_adjust (counter, - message->size_counter_delta);
262 _dbus_counter_unref (counter);
266 * Locks a message. Allows checking that applications don't keep a
267 * reference to a message in the outgoing queue and change it
268 * underneath us. Messages are locked when they enter the outgoing
269 * queue (dbus_connection_send_message()), and the library complains
270 * if the message is modified while locked.
272 * @param message the message to lock.
275 _dbus_message_lock (DBusMessage *message)
277 if (!message->locked)
279 _dbus_header_update_lengths (&message->header,
280 _dbus_string_get_length (&message->body));
282 /* must have a signature if you have a body */
283 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
284 dbus_message_get_signature (message) != NULL);
286 message->locked = TRUE;
291 set_or_delete_string_field (DBusMessage *message,
297 return _dbus_header_delete_field (&message->header, field);
299 return _dbus_header_set_field_basic (&message->header,
306 /* Probably we don't need to use this */
308 * Sets the signature of the message, i.e. the arguments in the
309 * message payload. The signature includes only "in" arguments for
310 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
311 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
312 * what you might expect (it does not include the signature of the
313 * entire C++-style method).
315 * The signature is a string made up of type codes such as
316 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
317 * the value of #DBUS_TYPE_INVALID). The macros such as
318 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
319 * may find it useful to use the string forms, such as
320 * #DBUS_TYPE_INT32_AS_STRING.
322 * An "unset" or #NULL signature is considered the same as an empty
323 * signature. In fact dbus_message_get_signature() will never return
326 * @param message the message
327 * @param signature the type signature or #NULL to unset
328 * @returns #FALSE if no memory
331 _dbus_message_set_signature (DBusMessage *message,
332 const char *signature)
334 _dbus_return_val_if_fail (message != NULL, FALSE);
335 _dbus_return_val_if_fail (!message->locked, FALSE);
336 _dbus_return_val_if_fail (signature == NULL ||
337 _dbus_check_is_valid_signature (signature));
338 /* can't delete the signature if you have a message body */
339 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
342 return set_or_delete_string_field (message,
343 DBUS_HEADER_FIELD_SIGNATURE,
352 * @defgroup DBusMessage DBusMessage
354 * @brief Message to be sent or received over a DBusConnection.
356 * A DBusMessage is the most basic unit of communication over a
357 * DBusConnection. A DBusConnection represents a stream of messages
358 * received from a remote application, and a stream of messages
359 * sent to a remote application.
365 * @typedef DBusMessage
367 * Opaque data type representing a message received from or to be
368 * sent to another application.
372 * Returns the serial of a message or 0 if none has been specified.
373 * The message's serial number is provided by the application sending
374 * the message and is used to identify replies to this message. All
375 * messages received on a connection will have a serial, but messages
376 * you haven't sent yet may return 0.
378 * @param message the message
379 * @returns the client serial
382 dbus_message_get_serial (DBusMessage *message)
384 _dbus_return_val_if_fail (message != NULL, 0);
386 return _dbus_header_get_serial (&message->header);
390 * Sets the reply serial of a message (the client serial
391 * of the message this is a reply to).
393 * @param message the message
394 * @param reply_serial the client serial
395 * @returns #FALSE if not enough memory
398 dbus_message_set_reply_serial (DBusMessage *message,
399 dbus_uint32_t reply_serial)
401 _dbus_return_val_if_fail (message != NULL, FALSE);
402 _dbus_return_val_if_fail (!message->locked, FALSE);
404 return _dbus_header_set_field_basic (&message->header,
405 DBUS_HEADER_FIELD_REPLY_SERIAL,
411 * Returns the serial that the message is a reply to or 0 if none.
413 * @param message the message
414 * @returns the reply serial
417 dbus_message_get_reply_serial (DBusMessage *message)
419 dbus_uint32_t v_UINT32;
421 _dbus_return_val_if_fail (message != NULL, 0);
423 if (_dbus_header_get_field_basic (&message->header,
424 DBUS_HEADER_FIELD_REPLY_SERIAL,
433 free_size_counter (void *element,
436 DBusCounter *counter = element;
437 DBusMessage *message = data;
439 _dbus_counter_adjust (counter, - message->size_counter_delta);
441 _dbus_counter_unref (counter);
445 dbus_message_finalize (DBusMessage *message)
447 _dbus_assert (message->refcount.value == 0);
449 /* This calls application callbacks! */
450 _dbus_data_slot_list_free (&message->slot_list);
452 _dbus_list_foreach (&message->size_counters,
453 free_size_counter, message);
454 _dbus_list_clear (&message->size_counters);
456 _dbus_header_free (&message->header);
457 _dbus_string_free (&message->body);
464 * We cache some DBusMessage to reduce the overhead of allocating
465 * them. In my profiling this consistently made about an 8%
466 * difference. It avoids the malloc for the message, the malloc for
467 * the slot list, the malloc for the header string and body string,
468 * and the associated free() calls. It does introduce another global
469 * lock which could be a performance issue in certain cases.
471 * For the echo client/server the round trip time goes from around
472 * .000077 to .000069 with the message cache on my laptop. The sysprof
473 * change is as follows (numbers are cumulative percentage):
475 * with message cache implemented as array as it is now (0.000069 per):
476 * new_empty_header 1.46
477 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
483 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
486 * with message cache implemented as list (0.000070 per roundtrip):
487 * new_empty_header 2.72
488 * list_pop_first 1.88
492 * without cache (0.000077 per roundtrip):
493 * new_empty_header 6.7
494 * string_init_preallocated 3.43
503 * If you implement the message_cache with a list, the primary reason
504 * it's slower is that you add another thread lock (on the DBusList
508 /** Avoid caching huge messages */
509 #define MAX_MESSAGE_SIZE_TO_CACHE _DBUS_ONE_MEGABYTE
511 /** Avoid caching too many messages */
512 #define MAX_MESSAGE_CACHE_SIZE 5
514 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
515 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
516 static int message_cache_count = 0;
517 static dbus_bool_t message_cache_shutdown_registered = FALSE;
520 dbus_message_cache_shutdown (void *data)
524 _DBUS_LOCK (message_cache);
527 while (i < MAX_MESSAGE_CACHE_SIZE)
529 if (message_cache[i])
530 dbus_message_finalize (message_cache[i]);
535 message_cache_count = 0;
536 message_cache_shutdown_registered = FALSE;
538 _DBUS_UNLOCK (message_cache);
542 * Tries to get a message from the message cache. The retrieved
543 * message will have junk in it, so it still needs to be cleared out
544 * in dbus_message_new_empty_header()
546 * @returns the message, or #NULL if none cached
549 dbus_message_get_cached (void)
551 DBusMessage *message;
556 _DBUS_LOCK (message_cache);
558 _dbus_assert (message_cache_count >= 0);
560 if (message_cache_count == 0)
562 _DBUS_UNLOCK (message_cache);
566 /* This is not necessarily true unless count > 0, and
567 * message_cache is uninitialized until the shutdown is
570 _dbus_assert (message_cache_shutdown_registered);
573 while (i < MAX_MESSAGE_CACHE_SIZE)
575 if (message_cache[i])
577 message = message_cache[i];
578 message_cache[i] = NULL;
579 message_cache_count -= 1;
584 _dbus_assert (message_cache_count >= 0);
585 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
586 _dbus_assert (message != NULL);
588 _DBUS_UNLOCK (message_cache);
590 _dbus_assert (message->refcount.value == 0);
591 _dbus_assert (message->size_counters == NULL);
597 * Tries to cache a message, otherwise finalize it.
599 * @param message the message
602 dbus_message_cache_or_finalize (DBusMessage *message)
604 dbus_bool_t was_cached;
607 _dbus_assert (message->refcount.value == 0);
609 /* This calls application code and has to be done first thing
610 * without holding the lock
612 _dbus_data_slot_list_clear (&message->slot_list);
614 _dbus_list_foreach (&message->size_counters,
615 free_size_counter, message);
616 _dbus_list_clear (&message->size_counters);
620 _DBUS_LOCK (message_cache);
622 if (!message_cache_shutdown_registered)
624 _dbus_assert (message_cache_count == 0);
626 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
630 while (i < MAX_MESSAGE_CACHE_SIZE)
632 message_cache[i] = NULL;
636 message_cache_shutdown_registered = TRUE;
639 _dbus_assert (message_cache_count >= 0);
641 if ((_dbus_string_get_length (&message->header.data) +
642 _dbus_string_get_length (&message->body)) >
643 MAX_MESSAGE_SIZE_TO_CACHE)
646 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
649 /* Find empty slot */
651 while (message_cache[i] != NULL)
654 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
656 _dbus_assert (message_cache[i] == NULL);
657 message_cache[i] = message;
658 message_cache_count += 1;
662 _DBUS_UNLOCK (message_cache);
665 dbus_message_finalize (message);
669 dbus_message_new_empty_header (void)
671 DBusMessage *message;
672 dbus_bool_t from_cache;
674 message = dbus_message_get_cached ();
683 message = dbus_new (DBusMessage, 1);
686 #ifndef DBUS_DISABLE_CHECKS
687 message->generation = _dbus_current_generation;
691 message->refcount.value = 1;
692 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
693 message->locked = FALSE;
694 message->size_counters = NULL;
695 message->size_counter_delta = 0;
696 message->changed_stamp = 0;
699 _dbus_data_slot_list_init (&message->slot_list);
703 _dbus_header_reinit (&message->header, message->byte_order);
704 _dbus_string_set_length (&message->body, 0);
708 if (!_dbus_header_init (&message->header, message->byte_order))
714 if (!_dbus_string_init_preallocated (&message->body, 32))
716 _dbus_header_free (&message->header);
726 * Constructs a new message of the given message type.
727 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
728 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
730 * @param message_type type of message
731 * @returns new message or #NULL If no memory
734 dbus_message_new (int message_type)
736 DBusMessage *message;
738 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
740 message = dbus_message_new_empty_header ();
744 if (!_dbus_header_create (&message->header,
746 NULL, NULL, NULL, NULL, NULL))
748 dbus_message_unref (message);
756 * Constructs a new message to invoke a method on a remote
757 * object. Returns #NULL if memory can't be allocated for the
758 * message. The destination may be #NULL in which case no destination
759 * is set; this is appropriate when using D-BUS in a peer-to-peer
760 * context (no message bus). The interface may be #NULL, which means
761 * that if multiple methods with the given name exist it is undefined
762 * which one will be invoked.
764 * @param destination name that the message should be sent to or #NULL
765 * @param path object path the message should be sent to
766 * @param interface interface to invoke method on
767 * @param method method to invoke
769 * @returns a new DBusMessage, free with dbus_message_unref()
770 * @see dbus_message_unref()
773 dbus_message_new_method_call (const char *destination,
775 const char *interface,
778 DBusMessage *message;
780 _dbus_return_val_if_fail (path != NULL, NULL);
781 _dbus_return_val_if_fail (method != NULL, NULL);
782 _dbus_return_val_if_fail (destination == NULL ||
783 _dbus_check_is_valid_bus_name (destination), NULL);
784 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
785 _dbus_return_val_if_fail (interface == NULL ||
786 _dbus_check_is_valid_interface (interface), NULL);
787 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
789 message = dbus_message_new_empty_header ();
793 if (!_dbus_header_create (&message->header,
794 DBUS_MESSAGE_TYPE_METHOD_CALL,
795 destination, path, interface, method, NULL))
797 dbus_message_unref (message);
805 * Constructs a message that is a reply to a method call. Returns
806 * #NULL if memory can't be allocated for the message.
808 * @param method_call the message which the created
809 * message is a reply to.
810 * @returns a new DBusMessage, free with dbus_message_unref()
811 * @see dbus_message_new_method_call(), dbus_message_unref()
814 dbus_message_new_method_return (DBusMessage *method_call)
816 DBusMessage *message;
819 _dbus_return_val_if_fail (method_call != NULL, NULL);
821 sender = dbus_message_get_sender (method_call);
823 /* sender is allowed to be null here in peer-to-peer case */
825 message = dbus_message_new_empty_header ();
829 if (!_dbus_header_create (&message->header,
830 DBUS_MESSAGE_TYPE_METHOD_RETURN,
831 sender, NULL, NULL, NULL, NULL))
833 dbus_message_unref (message);
837 dbus_message_set_no_reply (message, TRUE);
839 if (!dbus_message_set_reply_serial (message,
840 dbus_message_get_serial (method_call)))
842 dbus_message_unref (message);
850 * Constructs a new message representing a signal emission. Returns
851 * #NULL if memory can't be allocated for the message. A signal is
852 * identified by its originating interface, and the name of the
855 * @param path the path to the object emitting the signal
856 * @param interface the interface the signal is emitted from
857 * @param name name of the signal
858 * @returns a new DBusMessage, free with dbus_message_unref()
859 * @see dbus_message_unref()
862 dbus_message_new_signal (const char *path,
863 const char *interface,
866 DBusMessage *message;
868 _dbus_return_val_if_fail (path != NULL, NULL);
869 _dbus_return_val_if_fail (interface != NULL, NULL);
870 _dbus_return_val_if_fail (name != NULL, NULL);
871 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
872 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
873 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
875 message = dbus_message_new_empty_header ();
879 if (!_dbus_header_create (&message->header,
880 DBUS_MESSAGE_TYPE_SIGNAL,
881 NULL, path, interface, name, NULL))
883 dbus_message_unref (message);
887 dbus_message_set_no_reply (message, TRUE);
893 * Creates a new message that is an error reply to a certain message.
894 * Error replies are possible in response to method calls primarily.
896 * @param reply_to the original message
897 * @param error_name the error name
898 * @param error_message the error message string or #NULL for none
899 * @returns a new error message
902 dbus_message_new_error (DBusMessage *reply_to,
903 const char *error_name,
904 const char *error_message)
906 DBusMessage *message;
908 DBusMessageIter iter;
910 _dbus_return_val_if_fail (reply_to != NULL, NULL);
911 _dbus_return_val_if_fail (error_name != NULL, NULL);
912 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
914 sender = dbus_message_get_sender (reply_to);
916 /* sender may be NULL for non-message-bus case or
917 * when the message bus is dealing with an unregistered
920 message = dbus_message_new_empty_header ();
924 if (!_dbus_header_create (&message->header,
925 DBUS_MESSAGE_TYPE_ERROR,
926 sender, NULL, NULL, NULL, error_name))
928 dbus_message_unref (message);
932 dbus_message_set_no_reply (message, TRUE);
934 if (!dbus_message_set_reply_serial (message,
935 dbus_message_get_serial (reply_to)))
937 dbus_message_unref (message);
941 if (error_message != NULL)
943 dbus_message_iter_init_append (message, &iter);
944 if (!dbus_message_iter_append_basic (&iter,
948 dbus_message_unref (message);
957 * Creates a new message that is an error reply to a certain message.
958 * Error replies are possible in response to method calls primarily.
960 * @param reply_to the original message
961 * @param error_name the error name
962 * @param error_format the error message format as with printf
963 * @param ... format string arguments
964 * @returns a new error message
967 dbus_message_new_error_printf (DBusMessage *reply_to,
968 const char *error_name,
969 const char *error_format,
974 DBusMessage *message;
976 _dbus_return_val_if_fail (reply_to != NULL, NULL);
977 _dbus_return_val_if_fail (error_name != NULL, NULL);
978 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
980 if (!_dbus_string_init (&str))
983 va_start (args, error_format);
985 if (_dbus_string_append_printf_valist (&str, error_format, args))
986 message = dbus_message_new_error (reply_to, error_name,
987 _dbus_string_get_const_data (&str));
991 _dbus_string_free (&str);
1000 * Creates a new message that is an exact replica of the message
1001 * specified, except that its refcount is set to 1, its message serial
1002 * is reset to 0, and if the original message was "locked" (in the
1003 * outgoing message queue and thus not modifiable) the new message
1004 * will not be locked.
1006 * @param message the message.
1007 * @returns the new message.
1010 dbus_message_copy (const DBusMessage *message)
1012 DBusMessage *retval;
1014 _dbus_return_val_if_fail (message != NULL, NULL);
1016 retval = dbus_new0 (DBusMessage, 1);
1020 retval->refcount.value = 1;
1021 retval->byte_order = message->byte_order;
1022 retval->locked = FALSE;
1023 #ifndef DBUS_DISABLE_CHECKS
1024 retval->generation = message->generation;
1027 if (!_dbus_header_copy (&message->header, &retval->header))
1033 if (!_dbus_string_init_preallocated (&retval->body,
1034 _dbus_string_get_length (&message->body)))
1036 _dbus_header_free (&retval->header);
1041 if (!_dbus_string_copy (&message->body, 0,
1048 _dbus_header_free (&retval->header);
1049 _dbus_string_free (&retval->body);
1057 * Increments the reference count of a DBusMessage.
1059 * @param message The message
1060 * @returns the message
1061 * @see dbus_message_unref
1064 dbus_message_ref (DBusMessage *message)
1066 dbus_int32_t old_refcount;
1068 _dbus_return_val_if_fail (message != NULL, NULL);
1069 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1071 old_refcount = _dbus_atomic_inc (&message->refcount);
1072 _dbus_assert (old_refcount >= 1);
1078 * Decrements the reference count of a DBusMessage.
1080 * @param message The message
1081 * @see dbus_message_ref
1084 dbus_message_unref (DBusMessage *message)
1086 dbus_int32_t old_refcount;
1088 _dbus_return_if_fail (message != NULL);
1089 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1091 old_refcount = _dbus_atomic_dec (&message->refcount);
1093 _dbus_assert (old_refcount >= 0);
1095 if (old_refcount == 1)
1097 /* Calls application callbacks! */
1098 dbus_message_cache_or_finalize (message);
1103 * Gets the type of a message. Types include
1104 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1105 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1106 * types are allowed and all code must silently ignore messages of
1107 * unknown type. DBUS_MESSAGE_TYPE_INVALID will never be returned,
1111 * @param message the message
1112 * @returns the type of the message
1115 dbus_message_get_type (DBusMessage *message)
1117 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1119 return _dbus_header_get_message_type (&message->header);
1123 * Appends fields to a message given a variable argument list. The
1124 * variable argument list should contain the type of each argument
1125 * followed by the value to append. Appendable types are basic types,
1126 * and arrays of fixed-length basic types. To append variable-length
1127 * basic types, or any more complex value, you have to use an iterator
1128 * rather than this function.
1130 * To append a basic type, specify its type code followed by the
1131 * value. For example:
1134 * DBUS_TYPE_INT32, 42,
1135 * DBUS_TYPE_STRING, "Hello World"
1139 * dbus_int32_t val = 42;
1140 * DBUS_TYPE_INT32, val
1143 * Be sure that your provided value is the right size. For example, this
1146 * DBUS_TYPE_INT64, 42
1148 * Because the "42" will be a 32-bit integer. You need to cast to
1151 * To append an array of fixed-length basic types, pass in the
1152 * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1153 * the array pointer, and a 32-bit integer giving the number of
1154 * elements in the array. So for example:
1156 * const dbus_int32_t array[] = { 1, 2, 3 };
1157 * const dbus_int32_t *v_ARRAY = array;
1158 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3
1161 * @warning in C, given "int array[]", "&array == array" (the
1162 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1163 * So if you're using an array instead of a pointer you have to create
1164 * a pointer variable, assign the array to it, then take the address
1165 * of the pointer variable. For strings it works to write
1166 * const char *array = "Hello" and then use &array though.
1168 * The last argument to this function must be #DBUS_TYPE_INVALID,
1169 * marking the end of the argument list.
1171 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1173 * @todo If this fails due to lack of memory, the message is hosed and
1174 * you have to start over building the whole message.
1176 * @param message the message
1177 * @param first_arg_type type of the first argument
1178 * @param ... value of first argument, list of additional type-value pairs
1179 * @returns #TRUE on success
1182 dbus_message_append_args (DBusMessage *message,
1189 _dbus_return_val_if_fail (message != NULL, FALSE);
1191 va_start (var_args, first_arg_type);
1192 retval = dbus_message_append_args_valist (message,
1201 * This function takes a va_list for use by language bindings.
1202 * It's otherwise the same as dbus_message_append_args().
1204 * @todo for now, if this function fails due to OOM it will leave
1205 * the message half-written and you have to discard the message
1208 * @see dbus_message_append_args.
1209 * @param message the message
1210 * @param first_arg_type type of first argument
1211 * @param var_args value of first argument, then list of type/value pairs
1212 * @returns #TRUE on success
1215 dbus_message_append_args_valist (DBusMessage *message,
1220 DBusMessageIter iter;
1222 _dbus_return_val_if_fail (message != NULL, FALSE);
1224 type = first_arg_type;
1226 dbus_message_iter_init_append (message, &iter);
1228 while (type != DBUS_TYPE_INVALID)
1230 if (_dbus_type_is_basic (type))
1232 const DBusBasicValue *value;
1233 value = va_arg (var_args, const DBusBasicValue*);
1235 if (!dbus_message_iter_append_basic (&iter,
1240 else if (type == DBUS_TYPE_ARRAY)
1243 const DBusBasicValue **value;
1245 DBusMessageIter array;
1248 element_type = va_arg (var_args, int);
1250 #ifndef DBUS_DISABLE_CHECKS
1251 if (!_dbus_type_is_fixed (element_type))
1253 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1254 _dbus_type_to_string (element_type),
1255 _DBUS_FUNCTION_NAME);
1260 value = va_arg (var_args, const DBusBasicValue**);
1261 n_elements = va_arg (var_args, int);
1263 buf[0] = element_type;
1265 if (!dbus_message_iter_open_container (&iter,
1271 if (!dbus_message_iter_append_fixed_array (&array,
1277 if (!dbus_message_iter_close_container (&iter, &array))
1280 #ifndef DBUS_DISABLE_CHECKS
1283 _dbus_warn ("type %s isn't supported yet in %s\n",
1284 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1289 type = va_arg (var_args, int);
1299 * Gets arguments from a message given a variable argument list. The
1300 * supported types include those supported by
1301 * dbus_message_append_args(); that is, basic types and arrays of
1302 * fixed-length basic types. The arguments are the same as they would
1303 * be for dbus_message_iter_get_basic() or
1304 * dbus_message_iter_get_fixed_array().
1306 * In addition to those types, arrays of string, object path, and
1307 * signature are supported; but these are returned as allocated memory
1308 * and must be freed with dbus_free_string_array(), while the other
1309 * types are returned as const references.
1311 * The variable argument list should contain the type of the argument
1312 * followed by a pointer to where the value should be stored. The list
1313 * is terminated with #DBUS_TYPE_INVALID.
1315 * The returned values are constant; do not free them. They point
1316 * into the #DBusMessage.
1318 * If the requested arguments are not present, or do not have the
1319 * requested types, then an error will be set.
1321 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1323 * @param message the message
1324 * @param error error to be filled in on failure
1325 * @param first_arg_type the first argument type
1326 * @param ... location for first argument value, then list of type-location pairs
1327 * @returns #FALSE if the error was set
1330 dbus_message_get_args (DBusMessage *message,
1338 _dbus_return_val_if_fail (message != NULL, FALSE);
1339 _dbus_return_val_if_error_is_set (error, FALSE);
1341 va_start (var_args, first_arg_type);
1342 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1349 * This function takes a va_list for use by language bindings. It is
1350 * otherwise the same as dbus_message_get_args().
1352 * @see dbus_message_get_args
1353 * @param message the message
1354 * @param error error to be filled in
1355 * @param first_arg_type type of the first argument
1356 * @param var_args return location for first argument, followed by list of type/location pairs
1357 * @returns #FALSE if error was set
1360 dbus_message_get_args_valist (DBusMessage *message,
1365 DBusMessageIter iter;
1367 _dbus_return_val_if_fail (message != NULL, FALSE);
1368 _dbus_return_val_if_error_is_set (error, FALSE);
1370 dbus_message_iter_init (message, &iter);
1371 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1375 _dbus_message_iter_init_common (DBusMessage *message,
1376 DBusMessageRealIter *real,
1379 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1381 /* Since the iterator will read or write who-knows-what from the
1382 * message, we need to get in the right byte order
1384 ensure_byte_order (message);
1386 real->message = message;
1387 real->changed_stamp = message->changed_stamp;
1388 real->iter_type = iter_type;
1389 real->sig_refcount = 0;
1393 * Initializes a #DBusMessageIter for reading the arguments of the
1394 * message passed in.
1396 * @param message the message
1397 * @param iter pointer to an iterator to initialize
1398 * @returns #FALSE if the message has no arguments
1401 dbus_message_iter_init (DBusMessage *message,
1402 DBusMessageIter *iter)
1404 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1405 const DBusString *type_str;
1408 _dbus_return_val_if_fail (message != NULL, FALSE);
1409 _dbus_return_val_if_fail (iter != NULL, FALSE);
1411 get_const_signature (&message->header, &type_str, &type_pos);
1413 _dbus_message_iter_init_common (message, real,
1414 DBUS_MESSAGE_ITER_TYPE_READER);
1416 _dbus_type_reader_init (&real->u.reader,
1417 message->byte_order,
1422 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1425 #ifndef DBUS_DISABLE_CHECKS
1427 _dbus_message_iter_check (DBusMessageRealIter *iter)
1431 _dbus_warn ("dbus message iterator is NULL\n");
1435 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
1437 if (iter->u.reader.byte_order != iter->message->byte_order)
1439 _dbus_warn ("dbus message changed byte order since iterator was created\n");
1442 /* because we swap the message into compiler order when you init an iter */
1443 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
1445 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
1447 if (iter->u.writer.byte_order != iter->message->byte_order)
1449 _dbus_warn ("dbus message changed byte order since append iterator was created\n");
1452 /* because we swap the message into compiler order when you init an iter */
1453 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
1457 _dbus_warn ("dbus message iterator looks uninitialized or corrupted\n");
1461 if (iter->changed_stamp != iter->message->changed_stamp)
1463 _dbus_warn ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
1469 #endif /* DBUS_DISABLE_CHECKS */
1472 * Checks if an iterator has any more fields.
1474 * @param iter the message iter
1475 * @returns #TRUE if there are more fields
1479 dbus_message_iter_has_next (DBusMessageIter *iter)
1481 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1483 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1484 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1486 return _dbus_type_reader_has_next (&real->u.reader);
1490 * Moves the iterator to the next field, if any. If there's no next
1491 * field, returns #FALSE. If the iterator moves forward, returns
1494 * @param iter the message iter
1495 * @returns #TRUE if the iterator was moved to the next field
1498 dbus_message_iter_next (DBusMessageIter *iter)
1500 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1502 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1503 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1505 return _dbus_type_reader_next (&real->u.reader);
1509 * Returns the argument type of the argument that the message iterator
1510 * points to. If the iterator is at the end of the message, returns
1511 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1514 * dbus_message_iter_init (&iter);
1515 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1516 * dbus_message_iter_next (&iter);
1519 * @param iter the message iter
1520 * @returns the argument type
1523 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1525 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1527 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1528 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1530 return _dbus_type_reader_get_current_type (&real->u.reader);
1534 * Returns the element type of the array that the message iterator
1535 * points to. Note that you need to check that the iterator points to
1536 * an array prior to using this function.
1538 * @param iter the message iter
1539 * @returns the array element type
1542 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1544 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1546 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1547 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1548 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1550 return _dbus_type_reader_get_element_type (&real->u.reader);
1554 * Recurses into a container value when reading values from a message,
1555 * initializing a sub-iterator to use for traversing the child values
1558 * Note that this recurses into a value, not a type, so you can only
1559 * recurse if the value exists. The main implication of this is that
1560 * if you have for example an empty array of array of int32, you can
1561 * recurse into the outermost array, but it will have no values, so
1562 * you won't be able to recurse further. There's no array of int32 to
1565 * @param iter the message iterator
1566 * @param sub the sub-iterator to initialize
1569 dbus_message_iter_recurse (DBusMessageIter *iter,
1570 DBusMessageIter *sub)
1572 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1573 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1575 _dbus_return_if_fail (_dbus_message_iter_check (real));
1576 _dbus_return_if_fail (sub != NULL);
1579 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1583 * Reads a basic-typed value from the message iterator.
1584 * Basic types are the non-containers such as integer and string.
1586 * The value argument should be the address of a location to store
1587 * the returned value. So for int32 it should be a "dbus_int32_t*"
1588 * and for string a "const char**". The returned value is
1589 * by reference and should not be freed.
1591 * All returned values are guaranteed to fit in 8 bytes. So you can
1592 * write code like this:
1595 * #ifdef DBUS_HAVE_INT64
1596 * dbus_uint64_t value;
1598 * dbus_message_iter_get_basic (&read_iter, &value);
1599 * type = dbus_message_iter_get_arg_type (&read_iter);
1600 * dbus_message_iter_append_basic (&write_iter, type, &value);
1604 * To avoid the #DBUS_HAVE_INT64 conditional, create a struct or
1605 * something that occupies at least 8 bytes, e.g. you could use a
1606 * struct with two int32 values in it. dbus_uint64_t is just one
1607 * example of a type that's large enough to hold any possible value.
1609 * Be sure you have somehow checked that
1610 * dbus_message_iter_get_arg_type() matches the type you are
1611 * expecting, or you'll crash when you try to use an integer as a
1612 * string or something.
1614 * @param iter the iterator
1615 * @param value location to store the value
1618 dbus_message_iter_get_basic (DBusMessageIter *iter,
1621 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1623 _dbus_return_if_fail (_dbus_message_iter_check (real));
1624 _dbus_return_if_fail (value != NULL);
1626 _dbus_type_reader_read_basic (&real->u.reader,
1631 * Reads a block of fixed-length values from the message iterator.
1632 * Fixed-length values are those basic types that are not string-like,
1633 * such as integers, bool, double. The block read will be from the
1634 * current position in the array until the end of the array.
1636 * The value argument should be the address of a location to store the
1637 * returned array. So for int32 it should be a "const dbus_int32_t**"
1638 * The returned value is by reference and should not be freed.
1640 * @param iter the iterator
1641 * @param value location to store the block
1642 * @param n_elements number of elements in the block
1645 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
1649 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1651 _dbus_return_if_fail (_dbus_message_iter_check (real));
1652 _dbus_return_if_fail (value != NULL);
1653 _dbus_return_if_fail (_dbus_type_is_fixed (_dbus_type_reader_get_element_type (&real->u.reader)));
1655 _dbus_type_reader_read_fixed_multi (&real->u.reader,
1660 * This function takes a va_list for use by language bindings and is
1661 * otherwise the same as dbus_message_iter_get_args().
1662 * dbus_message_get_args() is the place to go for complete
1665 * @see dbus_message_get_args
1666 * @param iter the message iter
1667 * @param error error to be filled in
1668 * @param first_arg_type type of the first argument
1669 * @param var_args return location for first argument, followed by list of type/location pairs
1670 * @returns #FALSE if error was set
1673 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1678 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1679 int spec_type, msg_type, i;
1682 _dbus_assert (_dbus_message_iter_check (real));
1686 spec_type = first_arg_type;
1689 while (spec_type != DBUS_TYPE_INVALID)
1691 msg_type = dbus_message_iter_get_arg_type (iter);
1693 if (msg_type != spec_type)
1695 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1696 "Argument %d is specified to be of type \"%s\", but "
1697 "is actually of type \"%s\"\n", i,
1698 _dbus_type_to_string (spec_type),
1699 _dbus_type_to_string (msg_type));
1704 if (_dbus_type_is_basic (spec_type))
1706 DBusBasicValue *ptr;
1708 ptr = va_arg (var_args, DBusBasicValue*);
1710 _dbus_assert (ptr != NULL);
1712 _dbus_type_reader_read_basic (&real->u.reader,
1715 else if (spec_type == DBUS_TYPE_ARRAY)
1718 int spec_element_type;
1719 const DBusBasicValue **ptr;
1721 DBusTypeReader array;
1723 spec_element_type = va_arg (var_args, int);
1724 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1726 if (spec_element_type != element_type)
1728 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1729 "Argument %d is specified to be an array of \"%s\", but "
1730 "is actually an array of \"%s\"\n",
1732 _dbus_type_to_string (spec_element_type),
1733 _dbus_type_to_string (element_type));
1738 if (_dbus_type_is_fixed (spec_element_type))
1740 ptr = va_arg (var_args, const DBusBasicValue**);
1741 n_elements_p = va_arg (var_args, int*);
1743 _dbus_assert (ptr != NULL);
1744 _dbus_assert (n_elements_p != NULL);
1746 _dbus_type_reader_recurse (&real->u.reader, &array);
1748 _dbus_type_reader_read_fixed_multi (&array,
1751 else if (spec_element_type == DBUS_TYPE_STRING ||
1752 spec_element_type == DBUS_TYPE_SIGNATURE ||
1753 spec_element_type == DBUS_TYPE_OBJECT_PATH)
1755 char ***str_array_p;
1759 str_array_p = va_arg (var_args, char***);
1760 n_elements_p = va_arg (var_args, int*);
1762 _dbus_assert (str_array_p != NULL);
1763 _dbus_assert (n_elements_p != NULL);
1765 /* Count elements in the array */
1766 _dbus_type_reader_recurse (&real->u.reader, &array);
1769 if (_dbus_type_reader_has_next (&array))
1771 while (_dbus_type_reader_next (&array))
1775 str_array = dbus_new0 (char*, i + 1);
1776 if (str_array == NULL)
1778 _DBUS_SET_OOM (error);
1782 /* Now go through and dup each string */
1783 _dbus_type_reader_recurse (&real->u.reader, &array);
1786 if (_dbus_type_reader_has_next (&array))
1791 _dbus_type_reader_read_basic (&array,
1794 str_array[i] = _dbus_strdup (s);
1795 if (str_array[i] == NULL)
1797 dbus_free_string_array (str_array);
1798 _DBUS_SET_OOM (error);
1804 while (_dbus_type_reader_next (&array));
1807 *str_array_p = str_array;
1810 #ifndef DBUS_DISABLE_CHECKS
1813 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
1814 _DBUS_FUNCTION_NAME);
1819 #ifndef DBUS_DISABLE_CHECKS
1822 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
1823 _DBUS_FUNCTION_NAME);
1828 spec_type = va_arg (var_args, int);
1829 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1831 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1832 "Message has only %d arguments, but more were expected", i);
1847 * Initializes a #DBusMessageIter for appending arguments to the end
1850 * @todo If appending any of the arguments fails due to lack of
1851 * memory, generally the message is hosed and you have to start over
1852 * building the whole message.
1854 * @param message the message
1855 * @param iter pointer to an iterator to initialize
1858 dbus_message_iter_init_append (DBusMessage *message,
1859 DBusMessageIter *iter)
1861 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1863 _dbus_return_if_fail (message != NULL);
1864 _dbus_return_if_fail (iter != NULL);
1866 _dbus_message_iter_init_common (message, real,
1867 DBUS_MESSAGE_ITER_TYPE_WRITER);
1869 /* We create the signature string and point iterators at it "on demand"
1870 * when a value is actually appended. That means that init() never fails
1873 _dbus_type_writer_init_types_delayed (&real->u.writer,
1874 message->byte_order,
1876 _dbus_string_get_length (&message->body));
1880 * Creates a temporary signature string containing the current
1881 * signature, stores it in the iterator, and points the iterator to
1882 * the end of it. Used any time we write to the message.
1884 * @param real an iterator without a type_str
1885 * @returns #FALSE if no memory
1888 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
1891 const DBusString *current_sig;
1892 int current_sig_pos;
1894 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1896 if (real->u.writer.type_str != NULL)
1898 _dbus_assert (real->sig_refcount > 0);
1899 real->sig_refcount += 1;
1903 str = dbus_new (DBusString, 1);
1907 if (!_dbus_header_get_field_raw (&real->message->header,
1908 DBUS_HEADER_FIELD_SIGNATURE,
1909 ¤t_sig, ¤t_sig_pos))
1916 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
1917 current_sig_pos += 1; /* move on to sig data */
1919 if (!_dbus_string_init_preallocated (str, current_len + 4))
1925 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
1928 _dbus_string_free (str);
1935 if (!_dbus_string_init_preallocated (str, 4))
1942 real->sig_refcount = 1;
1944 _dbus_type_writer_add_types (&real->u.writer,
1945 str, _dbus_string_get_length (str));
1950 * Sets the new signature as the message signature, frees the
1951 * signature string, and marks the iterator as not having a type_str
1952 * anymore. Frees the signature even if it fails, so you can't
1953 * really recover from failure. Kinda busted.
1955 * @param real an iterator without a type_str
1956 * @returns #FALSE if no memory
1959 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
1962 const char *v_STRING;
1965 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
1966 _dbus_assert (real->u.writer.type_str != NULL);
1967 _dbus_assert (real->sig_refcount > 0);
1969 real->sig_refcount -= 1;
1971 if (real->sig_refcount > 0)
1973 _dbus_assert (real->sig_refcount == 0);
1977 str = real->u.writer.type_str;
1979 v_STRING = _dbus_string_get_const_data (str);
1980 if (!_dbus_header_set_field_basic (&real->message->header,
1981 DBUS_HEADER_FIELD_SIGNATURE,
1982 DBUS_TYPE_SIGNATURE,
1986 _dbus_type_writer_remove_types (&real->u.writer);
1987 _dbus_string_free (str);
1993 #ifndef DBUS_DISABLE_CHECKS
1995 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
1997 if (!_dbus_message_iter_check (iter))
2000 if (iter->message->locked)
2002 _dbus_warn ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2008 #endif /* DBUS_DISABLE_CHECKS */
2011 * Appends a basic-typed value to the message. The basic types are the
2012 * non-container types such as integer and string.
2014 * The "value" argument should be the address of a basic-typed value.
2015 * So for string, const char**. For integer, dbus_int32_t*.
2017 * @todo If this fails due to lack of memory, the message is hosed and
2018 * you have to start over building the whole message.
2020 * @param iter the append iterator
2021 * @param type the type of the value
2022 * @param value the address of the value
2023 * @returns #FALSE if not enough memory
2026 dbus_message_iter_append_basic (DBusMessageIter *iter,
2030 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2033 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2034 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2035 _dbus_return_val_if_fail (_dbus_type_is_basic (type), FALSE);
2036 _dbus_return_val_if_fail (value != NULL, FALSE);
2038 if (!_dbus_message_iter_open_signature (real))
2041 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2043 if (!_dbus_message_iter_close_signature (real))
2050 * Appends a block of fixed-length values to an array. The
2051 * fixed-length types are all basic types that are not string-like. So
2052 * int32, double, bool, etc. You must call
2053 * dbus_message_iter_open_container() to open an array of values
2054 * before calling this function. You may call this function multiple
2055 * times (and intermixed with calls to
2056 * dbus_message_iter_append_basic()) for the same array.
2058 * The "value" argument should be the address of the array. So for
2059 * integer, "dbus_int32_t**" is expected for example.
2061 * @warning in C, given "int array[]", "&array == array" (the
2062 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2063 * So if you're using an array instead of a pointer you have to create
2064 * a pointer variable, assign the array to it, then take the address
2065 * of the pointer variable.
2067 * const dbus_int32_t array[] = { 1, 2, 3 };
2068 * const dbus_int32_t *v_ARRAY = array;
2069 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2070 * fprintf (stderr, "No memory!\n");
2072 * For strings it works to write const char *array = "Hello" and then
2073 * use &array though.
2075 * @todo If this fails due to lack of memory, the message is hosed and
2076 * you have to start over building the whole message.
2078 * @param iter the append iterator
2079 * @param element_type the type of the array elements
2080 * @param value the address of the array
2081 * @param n_elements the number of elements to append
2082 * @returns #FALSE if not enough memory
2085 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2090 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2093 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2094 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2095 _dbus_return_val_if_fail (_dbus_type_is_fixed (element_type), FALSE);
2096 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2097 _dbus_return_val_if_fail (value != NULL, FALSE);
2098 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2099 _dbus_return_val_if_fail (n_elements <=
2100 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2103 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2109 * Appends a container-typed value to the message; you are required to
2110 * append the contents of the container using the returned
2111 * sub-iterator, and then call
2112 * dbus_message_iter_close_container(). Container types are for
2113 * example struct, variant, and array. For variants, the
2114 * contained_signature should be the type of the single value inside
2115 * the variant. For structs, contained_signature should be #NULL; it
2116 * will be set to whatever types you write into the struct. For
2117 * arrays, contained_signature should be the type of the array
2120 * @todo If this fails due to lack of memory, the message is hosed and
2121 * you have to start over building the whole message.
2123 * @param iter the append iterator
2124 * @param type the type of the value
2125 * @param contained_signature the type of container contents
2126 * @param sub sub-iterator to initialize
2127 * @returns #FALSE if not enough memory
2130 dbus_message_iter_open_container (DBusMessageIter *iter,
2132 const char *contained_signature,
2133 DBusMessageIter *sub)
2135 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2136 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2137 DBusString contained_str;
2139 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2140 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2141 _dbus_return_val_if_fail (_dbus_type_is_container (type), FALSE);
2142 _dbus_return_val_if_fail (sub != NULL, FALSE);
2143 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2144 contained_signature == NULL) ||
2145 contained_signature != NULL, FALSE);
2147 if (!_dbus_message_iter_open_signature (real))
2150 _dbus_string_init_const (&contained_str, contained_signature);
2153 return _dbus_type_writer_recurse (&real->u.writer,
2156 &real_sub->u.writer);
2161 * Closes a container-typed value appended to the message; may write
2162 * out more information to the message known only after the entire
2163 * container is written, and may free resources created by
2164 * dbus_message_iter_open_container().
2166 * @todo If this fails due to lack of memory, the message is hosed and
2167 * you have to start over building the whole message.
2169 * @param iter the append iterator
2170 * @param sub sub-iterator to close
2171 * @returns #FALSE if not enough memory
2174 dbus_message_iter_close_container (DBusMessageIter *iter,
2175 DBusMessageIter *sub)
2177 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2178 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2181 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2182 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2183 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2184 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2186 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2187 &real_sub->u.writer);
2189 if (!_dbus_message_iter_close_signature (real))
2196 * Sets a flag indicating that the message does not want a reply; if
2197 * this flag is set, the other end of the connection may (but is not
2198 * required to) optimize by not sending method return or error
2199 * replies. If this flag is set, there is no way to know whether the
2200 * message successfully arrived at the remote end. Normally you know a
2201 * message was received when you receive the reply to it.
2203 * @param message the message
2204 * @param no_reply #TRUE if no reply is desired
2207 dbus_message_set_no_reply (DBusMessage *message,
2208 dbus_bool_t no_reply)
2210 _dbus_return_if_fail (message != NULL);
2211 _dbus_return_if_fail (!message->locked);
2213 _dbus_header_toggle_flag (&message->header,
2214 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2219 * Returns #TRUE if the message does not expect
2222 * @param message the message
2223 * @returns #TRUE if the message sender isn't waiting for a reply
2226 dbus_message_get_no_reply (DBusMessage *message)
2228 _dbus_return_val_if_fail (message != NULL, FALSE);
2230 return _dbus_header_get_flag (&message->header,
2231 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2235 * Sets a flag indicating that an owner for the destination name will
2236 * be automatically started before the message is delivered. When this
2237 * flag is set, the message is held until a name owner finishes
2238 * starting up, or fails to start up. In case of failure, the reply
2241 * @param message the message
2242 * @param auto_start #TRUE if auto-starting is desired
2245 dbus_message_set_auto_start (DBusMessage *message,
2246 dbus_bool_t auto_start)
2248 _dbus_return_if_fail (message != NULL);
2249 _dbus_return_if_fail (!message->locked);
2251 _dbus_header_toggle_flag (&message->header,
2252 DBUS_HEADER_FLAG_NO_AUTO_START,
2257 * Returns #TRUE if the message will cause an owner for
2258 * destination name to be auto-started.
2260 * @param message the message
2261 * @returns #TRUE if the message will use auto-start
2264 dbus_message_get_auto_start (DBusMessage *message)
2266 _dbus_return_val_if_fail (message != NULL, FALSE);
2268 return !_dbus_header_get_flag (&message->header,
2269 DBUS_HEADER_FLAG_NO_AUTO_START);
2274 * Sets the object path this message is being sent to (for
2275 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2276 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2278 * @param message the message
2279 * @param object_path the path or #NULL to unset
2280 * @returns #FALSE if not enough memory
2283 dbus_message_set_path (DBusMessage *message,
2284 const char *object_path)
2286 _dbus_return_val_if_fail (message != NULL, FALSE);
2287 _dbus_return_val_if_fail (!message->locked, FALSE);
2288 _dbus_return_val_if_fail (object_path == NULL ||
2289 _dbus_check_is_valid_path (object_path),
2292 return set_or_delete_string_field (message,
2293 DBUS_HEADER_FIELD_PATH,
2294 DBUS_TYPE_OBJECT_PATH,
2299 * Gets the object path this message is being sent to (for
2300 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2301 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2303 * @param message the message
2304 * @returns the path (should not be freed) or #NULL
2307 dbus_message_get_path (DBusMessage *message)
2311 _dbus_return_val_if_fail (message != NULL, NULL);
2313 v = NULL; /* in case field doesn't exist */
2314 _dbus_header_get_field_basic (&message->header,
2315 DBUS_HEADER_FIELD_PATH,
2316 DBUS_TYPE_OBJECT_PATH,
2322 * Gets the object path this message is being sent to
2323 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2324 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2325 * format (one array element per path component).
2326 * Free the returned array with dbus_free_string_array().
2328 * An empty but non-NULL path array means the path "/".
2329 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2330 * and the path "/" becomes { NULL }.
2332 * @todo this could be optimized by using the len from the message
2333 * instead of calling strlen() again
2335 * @param message the message
2336 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2337 * @returns #FALSE if no memory to allocate the array
2340 dbus_message_get_path_decomposed (DBusMessage *message,
2345 _dbus_return_val_if_fail (message != NULL, FALSE);
2346 _dbus_return_val_if_fail (path != NULL, FALSE);
2350 v = dbus_message_get_path (message);
2353 if (!_dbus_decompose_path (v, strlen (v),
2361 * Sets the interface this message is being sent to
2362 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2363 * the interface a signal is being emitted from
2364 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2366 * @param message the message
2367 * @param interface the interface or #NULL to unset
2368 * @returns #FALSE if not enough memory
2371 dbus_message_set_interface (DBusMessage *message,
2372 const char *interface)
2374 _dbus_return_val_if_fail (message != NULL, FALSE);
2375 _dbus_return_val_if_fail (!message->locked, FALSE);
2376 _dbus_return_val_if_fail (interface == NULL ||
2377 _dbus_check_is_valid_interface (interface),
2380 return set_or_delete_string_field (message,
2381 DBUS_HEADER_FIELD_INTERFACE,
2387 * Gets the interface this message is being sent to
2388 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2389 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2390 * The interface name is fully-qualified (namespaced).
2391 * Returns #NULL if none.
2393 * @param message the message
2394 * @returns the message interface (should not be freed) or #NULL
2397 dbus_message_get_interface (DBusMessage *message)
2401 _dbus_return_val_if_fail (message != NULL, NULL);
2403 v = NULL; /* in case field doesn't exist */
2404 _dbus_header_get_field_basic (&message->header,
2405 DBUS_HEADER_FIELD_INTERFACE,
2412 * Sets the interface member being invoked
2413 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2414 * (DBUS_MESSAGE_TYPE_SIGNAL).
2415 * The interface name is fully-qualified (namespaced).
2417 * @param message the message
2418 * @param member the member or #NULL to unset
2419 * @returns #FALSE if not enough memory
2422 dbus_message_set_member (DBusMessage *message,
2425 _dbus_return_val_if_fail (message != NULL, FALSE);
2426 _dbus_return_val_if_fail (!message->locked, FALSE);
2427 _dbus_return_val_if_fail (member == NULL ||
2428 _dbus_check_is_valid_member (member),
2431 return set_or_delete_string_field (message,
2432 DBUS_HEADER_FIELD_MEMBER,
2438 * Gets the interface member being invoked
2439 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2440 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2442 * @param message the message
2443 * @returns the member name (should not be freed) or #NULL
2446 dbus_message_get_member (DBusMessage *message)
2450 _dbus_return_val_if_fail (message != NULL, NULL);
2452 v = NULL; /* in case field doesn't exist */
2453 _dbus_header_get_field_basic (&message->header,
2454 DBUS_HEADER_FIELD_MEMBER,
2461 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2462 * The name is fully-qualified (namespaced).
2464 * @param message the message
2465 * @param error_name the name or #NULL to unset
2466 * @returns #FALSE if not enough memory
2469 dbus_message_set_error_name (DBusMessage *message,
2470 const char *error_name)
2472 _dbus_return_val_if_fail (message != NULL, FALSE);
2473 _dbus_return_val_if_fail (!message->locked, FALSE);
2474 _dbus_return_val_if_fail (error_name == NULL ||
2475 _dbus_check_is_valid_error_name (error_name),
2478 return set_or_delete_string_field (message,
2479 DBUS_HEADER_FIELD_ERROR_NAME,
2485 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2488 * @param message the message
2489 * @returns the error name (should not be freed) or #NULL
2492 dbus_message_get_error_name (DBusMessage *message)
2496 _dbus_return_val_if_fail (message != NULL, NULL);
2498 v = NULL; /* in case field doesn't exist */
2499 _dbus_header_get_field_basic (&message->header,
2500 DBUS_HEADER_FIELD_ERROR_NAME,
2507 * Sets the message's destination. The destination is the name of
2508 * another connection on the bus and may be either the unique name
2509 * assigned by the bus to each connection, or a well-known name
2510 * specified in advance.
2512 * @param message the message
2513 * @param destination the destination name or #NULL to unset
2514 * @returns #FALSE if not enough memory
2517 dbus_message_set_destination (DBusMessage *message,
2518 const char *destination)
2520 _dbus_return_val_if_fail (message != NULL, FALSE);
2521 _dbus_return_val_if_fail (!message->locked, FALSE);
2522 _dbus_return_val_if_fail (destination == NULL ||
2523 _dbus_check_is_valid_bus_name (destination),
2526 return set_or_delete_string_field (message,
2527 DBUS_HEADER_FIELD_DESTINATION,
2533 * Gets the destination of a message or #NULL if there is none set.
2535 * @param message the message
2536 * @returns the message destination (should not be freed) or #NULL
2539 dbus_message_get_destination (DBusMessage *message)
2543 _dbus_return_val_if_fail (message != NULL, NULL);
2545 v = NULL; /* in case field doesn't exist */
2546 _dbus_header_get_field_basic (&message->header,
2547 DBUS_HEADER_FIELD_DESTINATION,
2554 * Sets the message sender.
2556 * @param message the message
2557 * @param sender the sender or #NULL to unset
2558 * @returns #FALSE if not enough memory
2561 dbus_message_set_sender (DBusMessage *message,
2564 _dbus_return_val_if_fail (message != NULL, FALSE);
2565 _dbus_return_val_if_fail (!message->locked, FALSE);
2566 _dbus_return_val_if_fail (sender == NULL ||
2567 _dbus_check_is_valid_bus_name (sender),
2570 return set_or_delete_string_field (message,
2571 DBUS_HEADER_FIELD_SENDER,
2577 * Gets the unique name of the connection which originated this
2578 * message, or #NULL if unknown or inapplicable. The sender is filled
2579 * in by the message bus.
2581 * @param message the message
2582 * @returns the unique name of the sender or #NULL
2585 dbus_message_get_sender (DBusMessage *message)
2589 _dbus_return_val_if_fail (message != NULL, NULL);
2591 v = NULL; /* in case field doesn't exist */
2592 _dbus_header_get_field_basic (&message->header,
2593 DBUS_HEADER_FIELD_SENDER,
2600 * Gets the type signature of the message, i.e. the arguments in the
2601 * message payload. The signature includes only "in" arguments for
2602 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2603 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2604 * what you might expect (it does not include the signature of the
2605 * entire C++-style method).
2607 * The signature is a string made up of type codes such as
2608 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2609 * the value of #DBUS_TYPE_INVALID).
2611 * @param message the message
2612 * @returns the type signature
2615 dbus_message_get_signature (DBusMessage *message)
2617 const DBusString *type_str;
2620 _dbus_return_val_if_fail (message != NULL, NULL);
2622 get_const_signature (&message->header, &type_str, &type_pos);
2624 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
2628 _dbus_message_has_type_interface_member (DBusMessage *message,
2630 const char *interface,
2635 _dbus_assert (message != NULL);
2636 _dbus_assert (interface != NULL);
2637 _dbus_assert (member != NULL);
2639 if (dbus_message_get_type (message) != type)
2642 /* Optimize by checking the short member name first
2643 * instead of the longer interface name
2646 n = dbus_message_get_member (message);
2648 if (n && strcmp (n, member) == 0)
2650 n = dbus_message_get_interface (message);
2652 if (n == NULL || strcmp (n, interface) == 0)
2660 * Checks whether the message is a method call with the given
2661 * interface and member fields. If the message is not
2662 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
2663 * member field, returns #FALSE. If the interface field is missing,
2664 * then it will be assumed equal to the provided interface. The D-BUS
2665 * protocol allows method callers to leave out the interface name.
2667 * @param message the message
2668 * @param interface the name to check (must not be #NULL)
2669 * @param method the name to check (must not be #NULL)
2671 * @returns #TRUE if the message is the specified method call
2674 dbus_message_is_method_call (DBusMessage *message,
2675 const char *interface,
2678 _dbus_return_val_if_fail (message != NULL, FALSE);
2679 _dbus_return_val_if_fail (interface != NULL, FALSE);
2680 _dbus_return_val_if_fail (method != NULL, FALSE);
2681 /* don't check that interface/method are valid since it would be
2682 * expensive, and not catch many common errors
2685 return _dbus_message_has_type_interface_member (message,
2686 DBUS_MESSAGE_TYPE_METHOD_CALL,
2691 * Checks whether the message is a signal with the given interface and
2692 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
2693 * has a different interface or member field, returns #FALSE. If the
2694 * interface field in the message is missing, it is assumed to match
2695 * any interface you pass in to this function.
2697 * @param message the message
2698 * @param interface the name to check (must not be #NULL)
2699 * @param signal_name the name to check (must not be #NULL)
2701 * @returns #TRUE if the message is the specified signal
2704 dbus_message_is_signal (DBusMessage *message,
2705 const char *interface,
2706 const char *signal_name)
2708 _dbus_return_val_if_fail (message != NULL, FALSE);
2709 _dbus_return_val_if_fail (interface != NULL, FALSE);
2710 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
2711 /* don't check that interface/name are valid since it would be
2712 * expensive, and not catch many common errors
2715 return _dbus_message_has_type_interface_member (message,
2716 DBUS_MESSAGE_TYPE_SIGNAL,
2717 interface, signal_name);
2721 * Checks whether the message is an error reply with the given error
2722 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
2723 * different name, returns #FALSE.
2725 * @param message the message
2726 * @param error_name the name to check (must not be #NULL)
2728 * @returns #TRUE if the message is the specified error
2731 dbus_message_is_error (DBusMessage *message,
2732 const char *error_name)
2736 _dbus_return_val_if_fail (message != NULL, FALSE);
2737 _dbus_return_val_if_fail (error_name != NULL, FALSE);
2738 /* don't check that error_name is valid since it would be expensive,
2739 * and not catch many common errors
2742 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2745 n = dbus_message_get_error_name (message);
2747 if (n && strcmp (n, error_name) == 0)
2754 * Checks whether the message was sent to the given name. If the
2755 * message has no destination specified or has a different
2756 * destination, returns #FALSE.
2758 * @param message the message
2759 * @param name the name to check (must not be #NULL)
2761 * @returns #TRUE if the message has the given destination name
2764 dbus_message_has_destination (DBusMessage *message,
2769 _dbus_return_val_if_fail (message != NULL, FALSE);
2770 _dbus_return_val_if_fail (name != NULL, FALSE);
2771 /* don't check that name is valid since it would be expensive, and
2772 * not catch many common errors
2775 s = dbus_message_get_destination (message);
2777 if (s && strcmp (s, name) == 0)
2784 * Checks whether the message has the given unique name as its sender.
2785 * If the message has no sender specified or has a different sender,
2786 * returns #FALSE. Note that a peer application will always have the
2787 * unique name of the connection as the sender. So you can't use this
2788 * function to see whether a sender owned a well-known name.
2790 * Messages from the bus itself will have #DBUS_SERVICE_ORG_FREEDESKTOP_DBUS
2793 * @param message the message
2794 * @param name the name to check (must not be #NULL)
2796 * @returns #TRUE if the message has the given sender
2799 dbus_message_has_sender (DBusMessage *message,
2804 _dbus_return_val_if_fail (message != NULL, FALSE);
2805 _dbus_return_val_if_fail (name != NULL, FALSE);
2806 /* don't check that name is valid since it would be expensive, and
2807 * not catch many common errors
2810 s = dbus_message_get_sender (message);
2812 if (s && strcmp (s, name) == 0)
2819 * Checks whether the message has the given signature; see
2820 * dbus_message_get_signature() for more details on what the signature
2823 * @param message the message
2824 * @param signature typecode array
2825 * @returns #TRUE if message has the given signature
2828 dbus_message_has_signature (DBusMessage *message,
2829 const char *signature)
2833 _dbus_return_val_if_fail (message != NULL, FALSE);
2834 _dbus_return_val_if_fail (signature != NULL, FALSE);
2835 /* don't check that signature is valid since it would be expensive,
2836 * and not catch many common errors
2839 s = dbus_message_get_signature (message);
2841 if (s && strcmp (s, signature) == 0)
2848 * Sets a #DBusError based on the contents of the given
2849 * message. The error is only set if the message
2850 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
2851 * The name of the error is set to the name of the message,
2852 * and the error message is set to the first argument
2853 * if the argument exists and is a string.
2855 * The return value indicates whether the error was set (the error is
2856 * set if and only if the message is an error message). So you can
2857 * check for an error reply and convert it to DBusError in one go:
2859 * if (dbus_set_error_from_message (error, reply))
2865 * @param error the error to set
2866 * @param message the message to set it from
2867 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
2870 dbus_set_error_from_message (DBusError *error,
2871 DBusMessage *message)
2875 _dbus_return_val_if_fail (message != NULL, FALSE);
2876 _dbus_return_val_if_error_is_set (error, FALSE);
2878 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2882 dbus_message_get_args (message, NULL,
2883 DBUS_TYPE_STRING, &str,
2886 dbus_set_error (error, dbus_message_get_error_name (message),
2887 str ? "%s" : NULL, str);
2895 * @addtogroup DBusMessageInternals
2901 * The initial buffer size of the message loader.
2903 * @todo this should be based on min header size plus some average
2904 * body size, or something. Or rather, the min header size only, if we
2905 * want to try to read only the header, store that in a DBusMessage,
2906 * then read only the body and store that, etc., depends on
2907 * how we optimize _dbus_message_loader_get_buffer() and what
2908 * the exact message format is.
2910 #define INITIAL_LOADER_DATA_LEN 32
2913 * Creates a new message loader. Returns #NULL if memory can't
2916 * @returns new loader, or #NULL.
2919 _dbus_message_loader_new (void)
2921 DBusMessageLoader *loader;
2923 loader = dbus_new0 (DBusMessageLoader, 1);
2927 loader->refcount = 1;
2929 loader->corrupted = FALSE;
2930 loader->corruption_reason = DBUS_VALID;
2932 /* Try to cap message size at something that won't *totally* hose
2933 * the system if we have a couple of them.
2935 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2937 if (!_dbus_string_init (&loader->data))
2943 /* preallocate the buffer for speed, ignore failure */
2944 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2945 _dbus_string_set_length (&loader->data, 0);
2951 * Increments the reference count of the loader.
2953 * @param loader the loader.
2954 * @returns the loader
2957 _dbus_message_loader_ref (DBusMessageLoader *loader)
2959 loader->refcount += 1;
2965 * Decrements the reference count of the loader and finalizes the
2966 * loader when the count reaches zero.
2968 * @param loader the loader.
2971 _dbus_message_loader_unref (DBusMessageLoader *loader)
2973 loader->refcount -= 1;
2974 if (loader->refcount == 0)
2976 _dbus_list_foreach (&loader->messages,
2977 (DBusForeachFunction) dbus_message_unref,
2979 _dbus_list_clear (&loader->messages);
2980 _dbus_string_free (&loader->data);
2986 * Gets the buffer to use for reading data from the network. Network
2987 * data is read directly into an allocated buffer, which is then used
2988 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2989 * The buffer must always be returned immediately using
2990 * _dbus_message_loader_return_buffer(), even if no bytes are
2991 * successfully read.
2993 * @todo this function can be a lot more clever. For example
2994 * it can probably always return a buffer size to read exactly
2995 * the body of the next message, thus avoiding any memory wastage
2998 * @todo we need to enforce a max length on strings in header fields.
3000 * @param loader the message loader.
3001 * @param buffer the buffer
3004 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3005 DBusString **buffer)
3007 _dbus_assert (!loader->buffer_outstanding);
3009 *buffer = &loader->data;
3011 loader->buffer_outstanding = TRUE;
3015 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3016 * indicating to the loader how many bytes of the buffer were filled
3017 * in. This function must always be called, even if no bytes were
3018 * successfully read.
3020 * @param loader the loader.
3021 * @param buffer the buffer.
3022 * @param bytes_read number of bytes that were read into the buffer.
3025 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3029 _dbus_assert (loader->buffer_outstanding);
3030 _dbus_assert (buffer == &loader->data);
3032 loader->buffer_outstanding = FALSE;
3036 * FIXME when we move the header out of the buffer, that memmoves all
3037 * buffered messages. Kind of crappy.
3039 * Also we copy the header and body, which is kind of crappy. To
3040 * avoid this, we have to allow header and body to be in a single
3041 * memory block, which is good for messages we read and bad for
3042 * messages we are creating. But we could move_len() the buffer into
3043 * this single memory block, and move_len() will just swap the buffers
3044 * if you're moving the entire buffer replacing the dest string.
3046 * We could also have the message loader tell the transport how many
3047 * bytes to read; so it would first ask for some arbitrary number like
3048 * 256, then if the message was incomplete it would use the
3049 * header/body len to ask for exactly the size of the message (or
3050 * blocks the size of a typical kernel buffer for the socket). That
3051 * way we don't get trailing bytes in the buffer that have to be
3052 * memmoved. Though I suppose we also don't have a chance of reading a
3053 * bunch of small messages at once, so the optimization may be stupid.
3055 * Another approach would be to keep a "start" index into
3056 * loader->data and only delete it occasionally, instead of after
3057 * each message is loaded.
3059 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3062 load_message (DBusMessageLoader *loader,
3063 DBusMessage *message,
3065 int fields_array_len,
3070 DBusValidity validity;
3071 const DBusString *type_str;
3073 DBusValidationMode mode;
3075 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3080 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3083 /* 1. VALIDATE AND COPY OVER HEADER */
3084 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3085 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3087 if (!_dbus_header_load (&message->header,
3095 _dbus_string_get_length (&loader->data)))
3097 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3098 if (validity == DBUS_VALID)
3102 loader->corrupted = TRUE;
3103 loader->corruption_reason = validity;
3108 _dbus_assert (validity == DBUS_VALID);
3110 message->byte_order = byte_order;
3112 /* 2. VALIDATE BODY */
3113 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3115 get_const_signature (&message->header, &type_str, &type_pos);
3117 /* Because the bytes_remaining arg is NULL, this validates that the
3118 * body is the right length
3120 validity = _dbus_validate_body_with_reason (type_str,
3127 if (validity != DBUS_VALID)
3129 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3131 loader->corrupted = TRUE;
3132 loader->corruption_reason = validity;
3138 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3140 if (!_dbus_list_append (&loader->messages, message))
3142 _dbus_verbose ("Failed to append new message to loader queue\n");
3147 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3148 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3149 (header_len + body_len));
3151 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3153 _dbus_verbose ("Failed to move body into new message\n");
3158 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3160 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3161 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3163 _dbus_verbose ("Loaded message %p\n", message);
3165 _dbus_assert (!oom);
3166 _dbus_assert (!loader->corrupted);
3167 _dbus_assert (loader->messages != NULL);
3168 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3176 /* does nothing if the message isn't in the list */
3177 _dbus_list_remove_last (&loader->messages, message);
3180 _dbus_assert (!loader->corrupted);
3182 _dbus_assert (loader->corrupted);
3184 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3190 * Converts buffered data into messages, if we have enough data. If
3191 * we don't have enough data, does nothing.
3193 * @todo we need to check that the proper named header fields exist
3194 * for each message type.
3196 * @todo If a message has unknown type, we should probably eat it
3197 * right here rather than passing it out to applications. However
3198 * it's not an error to see messages of unknown type.
3200 * @param loader the loader.
3201 * @returns #TRUE if we had enough memory to finish.
3204 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3206 while (!loader->corrupted &&
3207 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3209 DBusValidity validity;
3210 int byte_order, fields_array_len, header_len, body_len;
3212 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3219 _dbus_string_get_length (&loader->data)))
3221 DBusMessage *message;
3223 _dbus_assert (validity == DBUS_VALID);
3225 message = dbus_message_new_empty_header ();
3226 if (message == NULL)
3229 if (!load_message (loader, message,
3230 byte_order, fields_array_len,
3231 header_len, body_len))
3233 dbus_message_unref (message);
3234 /* load_message() returns false if corrupted or OOM; if
3235 * corrupted then return TRUE for not OOM
3237 return loader->corrupted;
3240 _dbus_assert (loader->messages != NULL);
3241 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3245 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3247 if (validity != DBUS_VALID)
3249 loader->corrupted = TRUE;
3250 loader->corruption_reason = validity;
3260 * Peeks at first loaded message, returns #NULL if no messages have
3263 * @param loader the loader.
3264 * @returns the next message, or #NULL if none.
3267 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3269 if (loader->messages)
3270 return loader->messages->data;
3276 * Pops a loaded message (passing ownership of the message
3277 * to the caller). Returns #NULL if no messages have been
3280 * @param loader the loader.
3281 * @returns the next message, or #NULL if none.
3284 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3286 return _dbus_list_pop_first (&loader->messages);
3290 * Pops a loaded message inside a list link (passing ownership of the
3291 * message and link to the caller). Returns #NULL if no messages have
3294 * @param loader the loader.
3295 * @returns the next message link, or #NULL if none.
3298 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3300 return _dbus_list_pop_first_link (&loader->messages);
3304 * Returns a popped message link, used to undo a pop.
3306 * @param loader the loader
3307 * @param link the link with a message in it
3310 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3313 _dbus_list_prepend_link (&loader->messages, link);
3317 * Checks whether the loader is confused due to bad data.
3318 * If messages are received that are invalid, the
3319 * loader gets confused and gives up permanently.
3320 * This state is called "corrupted."
3322 * @param loader the loader
3323 * @returns #TRUE if the loader is hosed.
3326 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3328 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3329 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3330 return loader->corrupted;
3334 * Sets the maximum size message we allow.
3336 * @param loader the loader
3337 * @param size the max message size in bytes
3340 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3343 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3345 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3346 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3347 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3349 loader->max_message_size = size;
3353 * Gets the maximum allowed message size in bytes.
3355 * @param loader the loader
3356 * @returns max size in bytes
3359 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3361 return loader->max_message_size;
3364 static DBusDataSlotAllocator slot_allocator;
3365 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3368 * Allocates an integer ID to be used for storing application-specific
3369 * data on any DBusMessage. The allocated ID may then be used
3370 * with dbus_message_set_data() and dbus_message_get_data().
3371 * The passed-in slot must be initialized to -1, and is filled in
3372 * with the slot ID. If the passed-in slot is not -1, it's assumed
3373 * to be already allocated, and its refcount is incremented.
3375 * The allocated slot is global, i.e. all DBusMessage objects will
3376 * have a slot with the given integer ID reserved.
3378 * @param slot_p address of a global variable storing the slot
3379 * @returns #FALSE on failure (no memory)
3382 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3384 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3385 _DBUS_LOCK_NAME (message_slots),
3390 * Deallocates a global ID for message data slots.
3391 * dbus_message_get_data() and dbus_message_set_data() may no
3392 * longer be used with this slot. Existing data stored on existing
3393 * DBusMessage objects will be freed when the message is
3394 * finalized, but may not be retrieved (and may only be replaced if
3395 * someone else reallocates the slot). When the refcount on the
3396 * passed-in slot reaches 0, it is set to -1.
3398 * @param slot_p address storing the slot to deallocate
3401 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3403 _dbus_return_if_fail (*slot_p >= 0);
3405 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3409 * Stores a pointer on a DBusMessage, along
3410 * with an optional function to be used for freeing
3411 * the data when the data is set again, or when
3412 * the message is finalized. The slot number
3413 * must have been allocated with dbus_message_allocate_data_slot().
3415 * @param message the message
3416 * @param slot the slot number
3417 * @param data the data to store
3418 * @param free_data_func finalizer function for the data
3419 * @returns #TRUE if there was enough memory to store the data
3422 dbus_message_set_data (DBusMessage *message,
3425 DBusFreeFunction free_data_func)
3427 DBusFreeFunction old_free_func;
3431 _dbus_return_val_if_fail (message != NULL, FALSE);
3432 _dbus_return_val_if_fail (slot >= 0, FALSE);
3434 retval = _dbus_data_slot_list_set (&slot_allocator,
3435 &message->slot_list,
3436 slot, data, free_data_func,
3437 &old_free_func, &old_data);
3441 /* Do the actual free outside the message lock */
3443 (* old_free_func) (old_data);
3450 * Retrieves data previously set with dbus_message_set_data().
3451 * The slot must still be allocated (must not have been freed).
3453 * @param message the message
3454 * @param slot the slot to get data from
3455 * @returns the data, or #NULL if not found
3458 dbus_message_get_data (DBusMessage *message,
3463 _dbus_return_val_if_fail (message != NULL, NULL);
3465 res = _dbus_data_slot_list_get (&slot_allocator,
3466 &message->slot_list,
3473 * Utility function to convert a machine-readable (not translated)
3474 * string into a D-BUS message type.
3477 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3478 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3479 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3480 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3481 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3486 dbus_message_type_from_string (const char *type_str)
3488 if (strcmp (type_str, "method_call") == 0)
3489 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3490 if (strcmp (type_str, "method_return") == 0)
3491 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3492 else if (strcmp (type_str, "signal") == 0)
3493 return DBUS_MESSAGE_TYPE_SIGNAL;
3494 else if (strcmp (type_str, "error") == 0)
3495 return DBUS_MESSAGE_TYPE_ERROR;
3497 return DBUS_MESSAGE_TYPE_INVALID;
3501 * Utility function to convert a D-BUS message type into a
3502 * machine-readable string (not translated).
3505 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3506 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3507 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3508 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3509 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3514 dbus_message_type_to_string (int type)
3518 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3519 return "method_call";
3520 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3521 return "method_return";
3522 case DBUS_MESSAGE_TYPE_SIGNAL:
3524 case DBUS_MESSAGE_TYPE_ERROR:
3533 /* tests in dbus-message-util.c */