1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-internals.h"
27 #include "dbus-marshal-recursive.h"
28 #include "dbus-marshal-validate.h"
29 #include "dbus-marshal-byteswap.h"
30 #include "dbus-marshal-header.h"
31 #include "dbus-signature.h"
32 #include "dbus-message-private.h"
33 #include "dbus-object-tree.h"
34 #include "dbus-memory.h"
35 #include "dbus-list.h"
36 #include "dbus-threads-internal.h"
37 #ifdef HAVE_UNIX_FD_PASSING
38 #include "dbus-sysdeps-unix.h"
43 static void dbus_message_finalize (DBusMessage *message);
46 * @defgroup DBusMessageInternals DBusMessage implementation details
47 * @ingroup DBusInternals
48 * @brief DBusMessage private implementation details.
50 * The guts of DBusMessage and its methods.
55 /* Not thread locked, but strictly const/read-only so should be OK
57 /** An static string representing an empty signature */
58 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
60 /* these have wacky values to help trap uninitialized iterators;
61 * but has to fit in 3 bits
64 DBUS_MESSAGE_ITER_TYPE_READER = 3,
65 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
68 /** typedef for internals of message iterator */
69 typedef struct DBusMessageRealIter DBusMessageRealIter;
72 * @brief Internals of DBusMessageIter
74 * Object representing a position in a message. All fields are internal.
76 struct DBusMessageRealIter
78 DBusMessage *message; /**< Message used */
79 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
80 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
81 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
84 DBusTypeWriter writer; /**< writer */
85 DBusTypeReader reader; /**< reader */
86 } u; /**< the type writer or reader that does all the work */
90 get_const_signature (DBusHeader *header,
91 const DBusString **type_str_p,
94 if (_dbus_header_get_field_raw (header,
95 DBUS_HEADER_FIELD_SIGNATURE,
99 *type_pos_p += 1; /* skip the signature length which is 1 byte */
103 *type_str_p = &_dbus_empty_signature_str;
109 * Swaps the message to compiler byte order if required
111 * @param message the message
114 _dbus_message_byteswap (DBusMessage *message)
116 const DBusString *type_str;
119 if (message->byte_order == DBUS_COMPILER_BYTE_ORDER)
122 _dbus_verbose ("Swapping message into compiler byte order\n");
124 get_const_signature (&message->header, &type_str, &type_pos);
126 _dbus_marshal_byteswap (type_str, type_pos,
128 DBUS_COMPILER_BYTE_ORDER,
131 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
133 _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
136 /** byte-swap the message if it doesn't match our byte order.
137 * Called only when we need the message in our own byte order,
138 * normally when reading arrays of integers or doubles.
139 * Otherwise should not be called since it would do needless
142 #define ensure_byte_order(message) \
143 if (message->byte_order != DBUS_COMPILER_BYTE_ORDER) \
144 _dbus_message_byteswap (message)
147 * Gets the data to be sent over the network for this message.
148 * The header and then the body should be written out.
149 * This function is guaranteed to always return the same
150 * data once a message is locked (with dbus_message_lock()).
152 * @param message the message.
153 * @param header return location for message header data.
154 * @param body return location for message body data.
157 _dbus_message_get_network_data (DBusMessage *message,
158 const DBusString **header,
159 const DBusString **body)
161 _dbus_assert (message->locked);
163 *header = &message->header.data;
164 *body = &message->body;
168 * Gets the unix fds to be sent over the network for this message.
169 * This function is guaranteed to always return the same data once a
170 * message is locked (with dbus_message_lock()).
172 * @param message the message.
173 * @param fds return location of unix fd array
174 * @param n_fds return number of entries in array
176 void _dbus_message_get_unix_fds(DBusMessage *message,
180 _dbus_assert (message->locked);
182 #ifdef HAVE_UNIX_FD_PASSING
183 *fds = message->unix_fds;
184 *n_fds = message->n_unix_fds;
192 * Sets the serial number of a message.
193 * This can only be done once on a message.
195 * DBusConnection will automatically set the serial to an appropriate value
196 * when the message is sent; this function is only needed when encapsulating
197 * messages in another protocol, or otherwise bypassing DBusConnection.
199 * @param message the message
200 * @param serial the serial
203 dbus_message_set_serial (DBusMessage *message,
204 dbus_uint32_t serial)
206 _dbus_return_if_fail (message != NULL);
207 _dbus_return_if_fail (!message->locked);
209 _dbus_header_set_serial (&message->header, serial);
213 * Adds a counter to be incremented immediately with the size/unix fds
214 * of this message, and decremented by the size/unix fds of this
215 * message when this message if finalized. The link contains a
216 * counter with its refcount already incremented, but the counter
217 * itself not incremented. Ownership of link and counter refcount is
218 * passed to the message.
220 * @param message the message
221 * @param link link with counter as data
224 _dbus_message_add_counter_link (DBusMessage *message,
227 /* right now we don't recompute the delta when message
228 * size changes, and that's OK for current purposes
229 * I think, but could be important to change later.
230 * Do recompute it whenever there are no outstanding counters,
231 * since it's basically free.
233 if (message->counters == NULL)
235 message->size_counter_delta =
236 _dbus_string_get_length (&message->header.data) +
237 _dbus_string_get_length (&message->body);
239 #ifdef HAVE_UNIX_FD_PASSING
240 message->unix_fd_counter_delta = message->n_unix_fds;
244 _dbus_verbose ("message has size %ld\n",
245 message->size_counter_delta);
249 _dbus_list_append_link (&message->counters, link);
251 _dbus_counter_adjust_size (link->data, message->size_counter_delta);
253 #ifdef HAVE_UNIX_FD_PASSING
254 _dbus_counter_adjust_unix_fd (link->data, message->unix_fd_counter_delta);
259 * Adds a counter to be incremented immediately with the size/unix fds
260 * of this message, and decremented by the size/unix fds of this
261 * message when this message if finalized.
263 * @param message the message
264 * @param counter the counter
265 * @returns #FALSE if no memory
268 _dbus_message_add_counter (DBusMessage *message,
269 DBusCounter *counter)
273 link = _dbus_list_alloc_link (counter);
277 _dbus_counter_ref (counter);
278 _dbus_message_add_counter_link (message, link);
284 * Removes a counter tracking the size/unix fds of this message, and
285 * decrements the counter by the size/unix fds of this message.
287 * @param message the message
288 * @param link_return return the link used
289 * @param counter the counter
292 _dbus_message_remove_counter (DBusMessage *message,
293 DBusCounter *counter,
294 DBusList **link_return)
298 link = _dbus_list_find_last (&message->counters,
300 _dbus_assert (link != NULL);
302 _dbus_list_unlink (&message->counters,
307 _dbus_list_free_link (link);
309 _dbus_counter_adjust_size (counter, - message->size_counter_delta);
311 #ifdef HAVE_UNIX_FD_PASSING
312 _dbus_counter_adjust_unix_fd (counter, - message->unix_fd_counter_delta);
315 _dbus_counter_unref (counter);
319 * Locks a message. Allows checking that applications don't keep a
320 * reference to a message in the outgoing queue and change it
321 * underneath us. Messages are locked when they enter the outgoing
322 * queue (dbus_connection_send_message()), and the library complains
323 * if the message is modified while locked. This function may also
324 * called externally, for applications wrapping D-Bus in another protocol.
326 * @param message the message to lock.
329 dbus_message_lock (DBusMessage *message)
331 if (!message->locked)
333 _dbus_header_update_lengths (&message->header,
334 _dbus_string_get_length (&message->body));
336 /* must have a signature if you have a body */
337 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
338 dbus_message_get_signature (message) != NULL);
340 message->locked = TRUE;
345 set_or_delete_string_field (DBusMessage *message,
351 return _dbus_header_delete_field (&message->header, field);
353 return _dbus_header_set_field_basic (&message->header,
360 /* Probably we don't need to use this */
362 * Sets the signature of the message, i.e. the arguments in the
363 * message payload. The signature includes only "in" arguments for
364 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
365 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
366 * what you might expect (it does not include the signature of the
367 * entire C++-style method).
369 * The signature is a string made up of type codes such as
370 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
371 * the value of #DBUS_TYPE_INVALID). The macros such as
372 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
373 * may find it useful to use the string forms, such as
374 * #DBUS_TYPE_INT32_AS_STRING.
376 * An "unset" or #NULL signature is considered the same as an empty
377 * signature. In fact dbus_message_get_signature() will never return
380 * @param message the message
381 * @param signature the type signature or #NULL to unset
382 * @returns #FALSE if no memory
385 _dbus_message_set_signature (DBusMessage *message,
386 const char *signature)
388 _dbus_return_val_if_fail (message != NULL, FALSE);
389 _dbus_return_val_if_fail (!message->locked, FALSE);
390 _dbus_return_val_if_fail (signature == NULL ||
391 _dbus_check_is_valid_signature (signature));
392 /* can't delete the signature if you have a message body */
393 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
396 return set_or_delete_string_field (message,
397 DBUS_HEADER_FIELD_SIGNATURE,
405 * We cache some DBusMessage to reduce the overhead of allocating
406 * them. In my profiling this consistently made about an 8%
407 * difference. It avoids the malloc for the message, the malloc for
408 * the slot list, the malloc for the header string and body string,
409 * and the associated free() calls. It does introduce another global
410 * lock which could be a performance issue in certain cases.
412 * For the echo client/server the round trip time goes from around
413 * .000077 to .000069 with the message cache on my laptop. The sysprof
414 * change is as follows (numbers are cumulative percentage):
416 * with message cache implemented as array as it is now (0.000069 per):
417 * new_empty_header 1.46
418 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
424 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
427 * with message cache implemented as list (0.000070 per roundtrip):
428 * new_empty_header 2.72
429 * list_pop_first 1.88
433 * without cache (0.000077 per roundtrip):
434 * new_empty_header 6.7
435 * string_init_preallocated 3.43
444 * If you implement the message_cache with a list, the primary reason
445 * it's slower is that you add another thread lock (on the DBusList
449 /** Avoid caching huge messages */
450 #define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
452 /** Avoid caching too many messages */
453 #define MAX_MESSAGE_CACHE_SIZE 5
455 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
456 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
457 static int message_cache_count = 0;
458 static dbus_bool_t message_cache_shutdown_registered = FALSE;
461 dbus_message_cache_shutdown (void *data)
465 _DBUS_LOCK (message_cache);
468 while (i < MAX_MESSAGE_CACHE_SIZE)
470 if (message_cache[i])
471 dbus_message_finalize (message_cache[i]);
476 message_cache_count = 0;
477 message_cache_shutdown_registered = FALSE;
479 _DBUS_UNLOCK (message_cache);
483 * Tries to get a message from the message cache. The retrieved
484 * message will have junk in it, so it still needs to be cleared out
485 * in dbus_message_new_empty_header()
487 * @returns the message, or #NULL if none cached
490 dbus_message_get_cached (void)
492 DBusMessage *message;
497 _DBUS_LOCK (message_cache);
499 _dbus_assert (message_cache_count >= 0);
501 if (message_cache_count == 0)
503 _DBUS_UNLOCK (message_cache);
507 /* This is not necessarily true unless count > 0, and
508 * message_cache is uninitialized until the shutdown is
511 _dbus_assert (message_cache_shutdown_registered);
514 while (i < MAX_MESSAGE_CACHE_SIZE)
516 if (message_cache[i])
518 message = message_cache[i];
519 message_cache[i] = NULL;
520 message_cache_count -= 1;
525 _dbus_assert (message_cache_count >= 0);
526 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
527 _dbus_assert (message != NULL);
529 _dbus_assert (message->refcount.value == 0);
530 _dbus_assert (message->counters == NULL);
532 _DBUS_UNLOCK (message_cache);
537 #ifdef HAVE_UNIX_FD_PASSING
539 close_unix_fds(int *fds, unsigned *n_fds)
549 for (i = 0; i < *n_fds; i++)
551 if (!_dbus_close(fds[i], &e))
553 _dbus_warn("Failed to close file descriptor: %s\n", e.message);
560 /* We don't free the array here, in case we can recycle it later */
565 free_counter (void *element,
568 DBusCounter *counter = element;
569 DBusMessage *message = data;
571 _dbus_counter_adjust_size (counter, - message->size_counter_delta);
572 #ifdef HAVE_UNIX_FD_PASSING
573 _dbus_counter_adjust_unix_fd (counter, - message->unix_fd_counter_delta);
576 _dbus_counter_unref (counter);
580 * Tries to cache a message, otherwise finalize it.
582 * @param message the message
585 dbus_message_cache_or_finalize (DBusMessage *message)
587 dbus_bool_t was_cached;
590 _dbus_assert (message->refcount.value == 0);
592 /* This calls application code and has to be done first thing
593 * without holding the lock
595 _dbus_data_slot_list_clear (&message->slot_list);
597 _dbus_list_foreach (&message->counters,
598 free_counter, message);
599 _dbus_list_clear (&message->counters);
601 #ifdef HAVE_UNIX_FD_PASSING
602 close_unix_fds(message->unix_fds, &message->n_unix_fds);
607 _DBUS_LOCK (message_cache);
609 if (!message_cache_shutdown_registered)
611 _dbus_assert (message_cache_count == 0);
613 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
617 while (i < MAX_MESSAGE_CACHE_SIZE)
619 message_cache[i] = NULL;
623 message_cache_shutdown_registered = TRUE;
626 _dbus_assert (message_cache_count >= 0);
628 if ((_dbus_string_get_length (&message->header.data) +
629 _dbus_string_get_length (&message->body)) >
630 MAX_MESSAGE_SIZE_TO_CACHE)
633 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
636 /* Find empty slot */
638 while (message_cache[i] != NULL)
641 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
643 _dbus_assert (message_cache[i] == NULL);
644 message_cache[i] = message;
645 message_cache_count += 1;
647 #ifndef DBUS_DISABLE_CHECKS
648 message->in_cache = TRUE;
652 _dbus_assert (message->refcount.value == 0);
654 _DBUS_UNLOCK (message_cache);
657 dbus_message_finalize (message);
660 #ifndef DBUS_DISABLE_CHECKS
662 _dbus_message_iter_check (DBusMessageRealIter *iter)
666 _dbus_warn_check_failed ("dbus message iterator is NULL\n");
670 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
672 if (iter->u.reader.byte_order != iter->message->byte_order)
674 _dbus_warn_check_failed ("dbus message changed byte order since iterator was created\n");
677 /* because we swap the message into compiler order when you init an iter */
678 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
680 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
682 if (iter->u.writer.byte_order != iter->message->byte_order)
684 _dbus_warn_check_failed ("dbus message changed byte order since append iterator was created\n");
687 /* because we swap the message into compiler order when you init an iter */
688 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
692 _dbus_warn_check_failed ("dbus message iterator looks uninitialized or corrupted\n");
696 if (iter->changed_stamp != iter->message->changed_stamp)
698 _dbus_warn_check_failed ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
704 #endif /* DBUS_DISABLE_CHECKS */
707 * Implementation of the varargs arg-getting functions.
708 * dbus_message_get_args() is the place to go for complete
711 * @todo This may leak memory and file descriptors if parsing fails. See #21259
713 * @see dbus_message_get_args
714 * @param iter the message iter
715 * @param error error to be filled in
716 * @param first_arg_type type of the first argument
717 * @param var_args return location for first argument, followed by list of type/location pairs
718 * @returns #FALSE if error was set
721 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
726 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
727 int spec_type, msg_type, i;
730 _dbus_assert (_dbus_message_iter_check (real));
734 spec_type = first_arg_type;
737 while (spec_type != DBUS_TYPE_INVALID)
739 msg_type = dbus_message_iter_get_arg_type (iter);
741 if (msg_type != spec_type)
743 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
744 "Argument %d is specified to be of type \"%s\", but "
745 "is actually of type \"%s\"\n", i,
746 _dbus_type_to_string (spec_type),
747 _dbus_type_to_string (msg_type));
752 if (spec_type == DBUS_TYPE_UNIX_FD)
754 #ifdef HAVE_UNIX_FD_PASSING
758 pfd = va_arg (var_args, int*);
761 _dbus_type_reader_read_basic(&real->u.reader, &idx);
763 if (idx.u32 >= real->message->n_unix_fds)
765 dbus_set_error (error, DBUS_ERROR_INCONSISTENT_MESSAGE,
766 "Message refers to file descriptor at index %i,"
767 "but has only %i descriptors attached.\n",
769 real->message->n_unix_fds);
773 if ((nfd = _dbus_dup(real->message->unix_fds[idx.u32], error)) < 0)
778 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
779 "Platform does not support file desciptor passing.\n");
783 else if (dbus_type_is_basic (spec_type))
787 ptr = va_arg (var_args, DBusBasicValue*);
789 _dbus_assert (ptr != NULL);
791 _dbus_type_reader_read_basic (&real->u.reader,
794 else if (spec_type == DBUS_TYPE_ARRAY)
797 int spec_element_type;
798 const DBusBasicValue **ptr;
800 DBusTypeReader array;
802 spec_element_type = va_arg (var_args, int);
803 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
805 if (spec_element_type != element_type)
807 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
808 "Argument %d is specified to be an array of \"%s\", but "
809 "is actually an array of \"%s\"\n",
811 _dbus_type_to_string (spec_element_type),
812 _dbus_type_to_string (element_type));
817 if (dbus_type_is_fixed (spec_element_type) &&
818 element_type != DBUS_TYPE_UNIX_FD)
820 ptr = va_arg (var_args, const DBusBasicValue**);
821 n_elements_p = va_arg (var_args, int*);
823 _dbus_assert (ptr != NULL);
824 _dbus_assert (n_elements_p != NULL);
826 _dbus_type_reader_recurse (&real->u.reader, &array);
828 _dbus_type_reader_read_fixed_multi (&array,
829 (void *) ptr, n_elements_p);
831 else if (spec_element_type == DBUS_TYPE_STRING ||
832 spec_element_type == DBUS_TYPE_SIGNATURE ||
833 spec_element_type == DBUS_TYPE_OBJECT_PATH)
839 str_array_p = va_arg (var_args, char***);
840 n_elements_p = va_arg (var_args, int*);
842 _dbus_assert (str_array_p != NULL);
843 _dbus_assert (n_elements_p != NULL);
845 /* Count elements in the array */
846 _dbus_type_reader_recurse (&real->u.reader, &array);
849 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
852 _dbus_type_reader_next (&array);
855 str_array = dbus_new0 (char*, n_elements + 1);
856 if (str_array == NULL)
858 _DBUS_SET_OOM (error);
862 /* Now go through and dup each string */
863 _dbus_type_reader_recurse (&real->u.reader, &array);
866 while (i < n_elements)
869 _dbus_type_reader_read_basic (&array,
872 str_array[i] = _dbus_strdup (s);
873 if (str_array[i] == NULL)
875 dbus_free_string_array (str_array);
876 _DBUS_SET_OOM (error);
882 if (!_dbus_type_reader_next (&array))
883 _dbus_assert (i == n_elements);
886 _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
887 _dbus_assert (i == n_elements);
888 _dbus_assert (str_array[i] == NULL);
890 *str_array_p = str_array;
891 *n_elements_p = n_elements;
893 #ifndef DBUS_DISABLE_CHECKS
896 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
897 _DBUS_FUNCTION_NAME);
902 #ifndef DBUS_DISABLE_CHECKS
905 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
906 _DBUS_FUNCTION_NAME);
911 spec_type = va_arg (var_args, int);
912 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
914 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
915 "Message has only %d arguments, but more were expected", i);
932 * @defgroup DBusMessage DBusMessage
934 * @brief Message to be sent or received over a #DBusConnection.
936 * A DBusMessage is the most basic unit of communication over a
937 * DBusConnection. A DBusConnection represents a stream of messages
938 * received from a remote application, and a stream of messages
939 * sent to a remote application.
941 * A message has a message type, returned from
942 * dbus_message_get_type(). This indicates whether the message is a
943 * method call, a reply to a method call, a signal, or an error reply.
945 * A message has header fields such as the sender, destination, method
946 * or signal name, and so forth. DBusMessage has accessor functions for
947 * these, such as dbus_message_get_member().
949 * Convenience functions dbus_message_is_method_call(), dbus_message_is_signal(),
950 * and dbus_message_is_error() check several header fields at once and are
951 * slightly more efficient than checking the header fields with individual
952 * accessor functions.
954 * Finally, a message has arguments. The number and types of arguments
955 * are in the message's signature header field (accessed with
956 * dbus_message_get_signature()). Simple argument values are usually
957 * retrieved with dbus_message_get_args() but more complex values such
958 * as structs may require the use of #DBusMessageIter.
960 * The D-Bus specification goes into some more detail about header fields and
967 * @typedef DBusMessage
969 * Opaque data type representing a message received from or to be
970 * sent to another application.
974 * Returns the serial of a message or 0 if none has been specified.
975 * The message's serial number is provided by the application sending
976 * the message and is used to identify replies to this message.
978 * All messages received on a connection will have a serial provided
979 * by the remote application.
981 * For messages you're sending, dbus_connection_send() will assign a
982 * serial and return it to you.
984 * @param message the message
985 * @returns the serial
988 dbus_message_get_serial (DBusMessage *message)
990 _dbus_return_val_if_fail (message != NULL, 0);
992 return _dbus_header_get_serial (&message->header);
996 * Sets the reply serial of a message (the serial of the message this
999 * @param message the message
1000 * @param reply_serial the serial we're replying to
1001 * @returns #FALSE if not enough memory
1004 dbus_message_set_reply_serial (DBusMessage *message,
1005 dbus_uint32_t reply_serial)
1007 _dbus_return_val_if_fail (message != NULL, FALSE);
1008 _dbus_return_val_if_fail (!message->locked, FALSE);
1009 _dbus_return_val_if_fail (reply_serial != 0, FALSE); /* 0 is invalid */
1011 return _dbus_header_set_field_basic (&message->header,
1012 DBUS_HEADER_FIELD_REPLY_SERIAL,
1018 * Returns the serial that the message is a reply to or 0 if none.
1020 * @param message the message
1021 * @returns the reply serial
1024 dbus_message_get_reply_serial (DBusMessage *message)
1026 dbus_uint32_t v_UINT32;
1028 _dbus_return_val_if_fail (message != NULL, 0);
1030 if (_dbus_header_get_field_basic (&message->header,
1031 DBUS_HEADER_FIELD_REPLY_SERIAL,
1040 dbus_message_finalize (DBusMessage *message)
1042 _dbus_assert (message->refcount.value == 0);
1044 /* This calls application callbacks! */
1045 _dbus_data_slot_list_free (&message->slot_list);
1047 _dbus_list_foreach (&message->counters,
1048 free_counter, message);
1049 _dbus_list_clear (&message->counters);
1051 _dbus_header_free (&message->header);
1052 _dbus_string_free (&message->body);
1054 #ifdef HAVE_UNIX_FD_PASSING
1055 close_unix_fds(message->unix_fds, &message->n_unix_fds);
1056 dbus_free(message->unix_fds);
1059 _dbus_assert (message->refcount.value == 0);
1061 dbus_free (message);
1065 dbus_message_new_empty_header (void)
1067 DBusMessage *message;
1068 dbus_bool_t from_cache;
1070 message = dbus_message_get_cached ();
1072 if (message != NULL)
1079 message = dbus_new (DBusMessage, 1);
1080 if (message == NULL)
1082 #ifndef DBUS_DISABLE_CHECKS
1083 message->generation = _dbus_current_generation;
1086 #ifdef HAVE_UNIX_FD_PASSING
1087 message->unix_fds = NULL;
1088 message->n_unix_fds_allocated = 0;
1092 message->refcount.value = 1;
1093 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
1094 message->locked = FALSE;
1095 #ifndef DBUS_DISABLE_CHECKS
1096 message->in_cache = FALSE;
1098 message->counters = NULL;
1099 message->size_counter_delta = 0;
1100 message->changed_stamp = 0;
1102 #ifdef HAVE_UNIX_FD_PASSING
1103 message->n_unix_fds = 0;
1104 message->unix_fd_counter_delta = 0;
1108 _dbus_data_slot_list_init (&message->slot_list);
1112 _dbus_header_reinit (&message->header, message->byte_order);
1113 _dbus_string_set_length (&message->body, 0);
1117 if (!_dbus_header_init (&message->header, message->byte_order))
1119 dbus_free (message);
1123 if (!_dbus_string_init_preallocated (&message->body, 32))
1125 _dbus_header_free (&message->header);
1126 dbus_free (message);
1135 * Constructs a new message of the given message type.
1136 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1137 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1139 * Usually you want to use dbus_message_new_method_call(),
1140 * dbus_message_new_method_return(), dbus_message_new_signal(),
1141 * or dbus_message_new_error() instead.
1143 * @param message_type type of message
1144 * @returns new message or #NULL if no memory
1147 dbus_message_new (int message_type)
1149 DBusMessage *message;
1151 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1153 message = dbus_message_new_empty_header ();
1154 if (message == NULL)
1157 if (!_dbus_header_create (&message->header,
1159 NULL, NULL, NULL, NULL, NULL))
1161 dbus_message_unref (message);
1169 * Constructs a new message to invoke a method on a remote
1170 * object. Returns #NULL if memory can't be allocated for the
1171 * message. The destination may be #NULL in which case no destination
1172 * is set; this is appropriate when using D-Bus in a peer-to-peer
1173 * context (no message bus). The interface may be #NULL, which means
1174 * that if multiple methods with the given name exist it is undefined
1175 * which one will be invoked.
1177 * The path and method names may not be #NULL.
1179 * Destination, path, interface, and method name can't contain
1180 * any invalid characters (see the D-Bus specification).
1182 * @param destination name that the message should be sent to or #NULL
1183 * @param path object path the message should be sent to
1184 * @param interface interface to invoke method on, or #NULL
1185 * @param method method to invoke
1187 * @returns a new DBusMessage, free with dbus_message_unref()
1190 dbus_message_new_method_call (const char *destination,
1192 const char *interface,
1195 DBusMessage *message;
1197 _dbus_return_val_if_fail (path != NULL, NULL);
1198 _dbus_return_val_if_fail (method != NULL, NULL);
1199 _dbus_return_val_if_fail (destination == NULL ||
1200 _dbus_check_is_valid_bus_name (destination), NULL);
1201 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1202 _dbus_return_val_if_fail (interface == NULL ||
1203 _dbus_check_is_valid_interface (interface), NULL);
1204 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
1206 message = dbus_message_new_empty_header ();
1207 if (message == NULL)
1210 if (!_dbus_header_create (&message->header,
1211 DBUS_MESSAGE_TYPE_METHOD_CALL,
1212 destination, path, interface, method, NULL))
1214 dbus_message_unref (message);
1222 * Constructs a message that is a reply to a method call. Returns
1223 * #NULL if memory can't be allocated for the message.
1225 * @param method_call the message being replied to
1226 * @returns a new DBusMessage, free with dbus_message_unref()
1229 dbus_message_new_method_return (DBusMessage *method_call)
1231 DBusMessage *message;
1234 _dbus_return_val_if_fail (method_call != NULL, NULL);
1236 sender = dbus_message_get_sender (method_call);
1238 /* sender is allowed to be null here in peer-to-peer case */
1240 message = dbus_message_new_empty_header ();
1241 if (message == NULL)
1244 if (!_dbus_header_create (&message->header,
1245 DBUS_MESSAGE_TYPE_METHOD_RETURN,
1246 sender, NULL, NULL, NULL, NULL))
1248 dbus_message_unref (message);
1252 dbus_message_set_no_reply (message, TRUE);
1254 if (!dbus_message_set_reply_serial (message,
1255 dbus_message_get_serial (method_call)))
1257 dbus_message_unref (message);
1265 * Constructs a new message representing a signal emission. Returns
1266 * #NULL if memory can't be allocated for the message. A signal is
1267 * identified by its originating object path, interface, and the name
1270 * Path, interface, and signal name must all be valid (the D-Bus
1271 * specification defines the syntax of these fields).
1273 * @param path the path to the object emitting the signal
1274 * @param interface the interface the signal is emitted from
1275 * @param name name of the signal
1276 * @returns a new DBusMessage, free with dbus_message_unref()
1279 dbus_message_new_signal (const char *path,
1280 const char *interface,
1283 DBusMessage *message;
1285 _dbus_return_val_if_fail (path != NULL, NULL);
1286 _dbus_return_val_if_fail (interface != NULL, NULL);
1287 _dbus_return_val_if_fail (name != NULL, NULL);
1288 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1289 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
1290 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
1292 message = dbus_message_new_empty_header ();
1293 if (message == NULL)
1296 if (!_dbus_header_create (&message->header,
1297 DBUS_MESSAGE_TYPE_SIGNAL,
1298 NULL, path, interface, name, NULL))
1300 dbus_message_unref (message);
1304 dbus_message_set_no_reply (message, TRUE);
1310 * Creates a new message that is an error reply to another message.
1311 * Error replies are most common in response to method calls, but
1312 * can be returned in reply to any message.
1314 * The error name must be a valid error name according to the syntax
1315 * given in the D-Bus specification. If you don't want to make
1316 * up an error name just use #DBUS_ERROR_FAILED.
1318 * @param reply_to the message we're replying to
1319 * @param error_name the error name
1320 * @param error_message the error message string (or #NULL for none, but please give a message)
1321 * @returns a new error message object, free with dbus_message_unref()
1324 dbus_message_new_error (DBusMessage *reply_to,
1325 const char *error_name,
1326 const char *error_message)
1328 DBusMessage *message;
1330 DBusMessageIter iter;
1332 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1333 _dbus_return_val_if_fail (error_name != NULL, NULL);
1334 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1336 sender = dbus_message_get_sender (reply_to);
1338 /* sender may be NULL for non-message-bus case or
1339 * when the message bus is dealing with an unregistered
1342 message = dbus_message_new_empty_header ();
1343 if (message == NULL)
1346 if (!_dbus_header_create (&message->header,
1347 DBUS_MESSAGE_TYPE_ERROR,
1348 sender, NULL, NULL, NULL, error_name))
1350 dbus_message_unref (message);
1354 dbus_message_set_no_reply (message, TRUE);
1356 if (!dbus_message_set_reply_serial (message,
1357 dbus_message_get_serial (reply_to)))
1359 dbus_message_unref (message);
1363 if (error_message != NULL)
1365 dbus_message_iter_init_append (message, &iter);
1366 if (!dbus_message_iter_append_basic (&iter,
1370 dbus_message_unref (message);
1379 * Creates a new message that is an error reply to another message, allowing
1380 * you to use printf formatting.
1382 * See dbus_message_new_error() for details - this function is the same
1383 * aside from the printf formatting.
1385 * @todo add _DBUS_GNUC_PRINTF to this (requires moving _DBUS_GNUC_PRINTF to
1386 * public header, see DBUS_DEPRECATED for an example)
1388 * @param reply_to the original message
1389 * @param error_name the error name
1390 * @param error_format the error message format as with printf
1391 * @param ... format string arguments
1392 * @returns a new error message
1395 dbus_message_new_error_printf (DBusMessage *reply_to,
1396 const char *error_name,
1397 const char *error_format,
1402 DBusMessage *message;
1404 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1405 _dbus_return_val_if_fail (error_name != NULL, NULL);
1406 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1408 if (!_dbus_string_init (&str))
1411 va_start (args, error_format);
1413 if (_dbus_string_append_printf_valist (&str, error_format, args))
1414 message = dbus_message_new_error (reply_to, error_name,
1415 _dbus_string_get_const_data (&str));
1419 _dbus_string_free (&str);
1428 * Creates a new message that is an exact replica of the message
1429 * specified, except that its refcount is set to 1, its message serial
1430 * is reset to 0, and if the original message was "locked" (in the
1431 * outgoing message queue and thus not modifiable) the new message
1432 * will not be locked.
1434 * @todo This function can't be used in programs that try to recover from OOM errors.
1436 * @param message the message
1437 * @returns the new message.or #NULL if not enough memory or Unix file descriptors (in case the message to copy includes Unix file descriptors) can be allocated.
1440 dbus_message_copy (const DBusMessage *message)
1442 DBusMessage *retval;
1444 _dbus_return_val_if_fail (message != NULL, NULL);
1446 retval = dbus_new0 (DBusMessage, 1);
1450 retval->refcount.value = 1;
1451 retval->byte_order = message->byte_order;
1452 retval->locked = FALSE;
1453 #ifndef DBUS_DISABLE_CHECKS
1454 retval->generation = message->generation;
1457 if (!_dbus_header_copy (&message->header, &retval->header))
1463 if (!_dbus_string_init_preallocated (&retval->body,
1464 _dbus_string_get_length (&message->body)))
1466 _dbus_header_free (&retval->header);
1471 if (!_dbus_string_copy (&message->body, 0,
1475 #ifdef HAVE_UNIX_FD_PASSING
1476 retval->unix_fds = dbus_new(int, message->n_unix_fds);
1477 if (retval->unix_fds == NULL && message->n_unix_fds > 0)
1480 retval->n_unix_fds_allocated = message->n_unix_fds;
1482 for (retval->n_unix_fds = 0;
1483 retval->n_unix_fds < message->n_unix_fds;
1484 retval->n_unix_fds++)
1486 retval->unix_fds[retval->n_unix_fds] = _dbus_dup(message->unix_fds[retval->n_unix_fds], NULL);
1488 if (retval->unix_fds[retval->n_unix_fds] < 0)
1497 _dbus_header_free (&retval->header);
1498 _dbus_string_free (&retval->body);
1500 #ifdef HAVE_UNIX_FD_PASSING
1501 close_unix_fds(retval->unix_fds, &retval->n_unix_fds);
1502 dbus_free(retval->unix_fds);
1512 * Increments the reference count of a DBusMessage.
1514 * @param message the message
1515 * @returns the message
1516 * @see dbus_message_unref
1519 dbus_message_ref (DBusMessage *message)
1521 dbus_int32_t old_refcount;
1523 _dbus_return_val_if_fail (message != NULL, NULL);
1524 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1525 _dbus_return_val_if_fail (!message->in_cache, NULL);
1527 old_refcount = _dbus_atomic_inc (&message->refcount);
1528 _dbus_assert (old_refcount >= 1);
1534 * Decrements the reference count of a DBusMessage, freeing the
1535 * message if the count reaches 0.
1537 * @param message the message
1538 * @see dbus_message_ref
1541 dbus_message_unref (DBusMessage *message)
1543 dbus_int32_t old_refcount;
1545 _dbus_return_if_fail (message != NULL);
1546 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1547 _dbus_return_if_fail (!message->in_cache);
1549 old_refcount = _dbus_atomic_dec (&message->refcount);
1551 _dbus_assert (old_refcount >= 0);
1553 if (old_refcount == 1)
1555 /* Calls application callbacks! */
1556 dbus_message_cache_or_finalize (message);
1561 * Gets the type of a message. Types include
1562 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1563 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1564 * types are allowed and all code must silently ignore messages of
1565 * unknown type. #DBUS_MESSAGE_TYPE_INVALID will never be returned.
1567 * @param message the message
1568 * @returns the type of the message
1571 dbus_message_get_type (DBusMessage *message)
1573 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1575 return _dbus_header_get_message_type (&message->header);
1579 * Appends fields to a message given a variable argument list. The
1580 * variable argument list should contain the type of each argument
1581 * followed by the value to append. Appendable types are basic types,
1582 * and arrays of fixed-length basic types (except arrays of Unix file
1583 * descriptors). To append variable-length basic types, or any more
1584 * complex value, you have to use an iterator rather than this
1587 * To append a basic type, specify its type code followed by the
1588 * address of the value. For example:
1592 * dbus_int32_t v_INT32 = 42;
1593 * const char *v_STRING = "Hello World";
1594 * dbus_message_append_args (message,
1595 * DBUS_TYPE_INT32, &v_INT32,
1596 * DBUS_TYPE_STRING, &v_STRING,
1597 * DBUS_TYPE_INVALID);
1600 * To append an array of fixed-length basic types (except Unix file
1601 * descriptors), pass in the DBUS_TYPE_ARRAY typecode, the element
1602 * typecode, the address of the array pointer, and a 32-bit integer
1603 * giving the number of elements in the array. So for example: @code
1604 * const dbus_int32_t array[] = { 1, 2, 3 }; const dbus_int32_t
1605 * *v_ARRAY = array; dbus_message_append_args (message,
1606 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3, DBUS_TYPE_INVALID);
1609 * This function does not support arrays of Unix file descriptors. If
1610 * you need those you need to manually recurse into the array.
1612 * For Unix file descriptors this function will internally duplicate
1613 * the descriptor you passed in. Hence you may close the descriptor
1614 * immediately after this call.
1616 * @warning in C, given "int array[]", "&array == array" (the
1617 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1618 * So if you're using an array instead of a pointer you have to create
1619 * a pointer variable, assign the array to it, then take the address
1620 * of the pointer variable. For strings it works to write
1621 * const char *array = "Hello" and then use &array though.
1623 * The last argument to this function must be #DBUS_TYPE_INVALID,
1624 * marking the end of the argument list. If you don't do this
1625 * then libdbus won't know to stop and will read invalid memory.
1627 * String/signature/path arrays should be passed in as "const char***
1628 * address_of_array" and "int n_elements"
1630 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1632 * @todo If this fails due to lack of memory, the message is hosed and
1633 * you have to start over building the whole message.
1635 * @param message the message
1636 * @param first_arg_type type of the first argument
1637 * @param ... value of first argument, list of additional type-value pairs
1638 * @returns #TRUE on success
1641 dbus_message_append_args (DBusMessage *message,
1648 _dbus_return_val_if_fail (message != NULL, FALSE);
1650 va_start (var_args, first_arg_type);
1651 retval = dbus_message_append_args_valist (message,
1660 * Like dbus_message_append_args() but takes a va_list for use by language bindings.
1662 * @todo for now, if this function fails due to OOM it will leave
1663 * the message half-written and you have to discard the message
1666 * @see dbus_message_append_args.
1667 * @param message the message
1668 * @param first_arg_type type of first argument
1669 * @param var_args value of first argument, then list of type/value pairs
1670 * @returns #TRUE on success
1673 dbus_message_append_args_valist (DBusMessage *message,
1678 DBusMessageIter iter;
1680 _dbus_return_val_if_fail (message != NULL, FALSE);
1682 type = first_arg_type;
1684 dbus_message_iter_init_append (message, &iter);
1686 while (type != DBUS_TYPE_INVALID)
1688 if (dbus_type_is_basic (type))
1690 const DBusBasicValue *value;
1691 value = va_arg (var_args, const DBusBasicValue*);
1693 if (!dbus_message_iter_append_basic (&iter,
1698 else if (type == DBUS_TYPE_ARRAY)
1701 DBusMessageIter array;
1704 element_type = va_arg (var_args, int);
1706 buf[0] = element_type;
1708 if (!dbus_message_iter_open_container (&iter,
1714 if (dbus_type_is_fixed (element_type) &&
1715 element_type != DBUS_TYPE_UNIX_FD)
1717 const DBusBasicValue **value;
1720 value = va_arg (var_args, const DBusBasicValue**);
1721 n_elements = va_arg (var_args, int);
1723 if (!dbus_message_iter_append_fixed_array (&array,
1727 dbus_message_iter_abandon_container (&iter, &array);
1731 else if (element_type == DBUS_TYPE_STRING ||
1732 element_type == DBUS_TYPE_SIGNATURE ||
1733 element_type == DBUS_TYPE_OBJECT_PATH)
1735 const char ***value_p;
1740 value_p = va_arg (var_args, const char***);
1741 n_elements = va_arg (var_args, int);
1746 while (i < n_elements)
1748 if (!dbus_message_iter_append_basic (&array,
1751 dbus_message_iter_abandon_container (&iter, &array);
1759 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1760 _dbus_type_to_string (element_type),
1761 _DBUS_FUNCTION_NAME);
1765 if (!dbus_message_iter_close_container (&iter, &array))
1768 #ifndef DBUS_DISABLE_CHECKS
1771 _dbus_warn ("type %s isn't supported yet in %s\n",
1772 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1777 type = va_arg (var_args, int);
1787 * Gets arguments from a message given a variable argument list. The
1788 * supported types include those supported by
1789 * dbus_message_append_args(); that is, basic types and arrays of
1790 * fixed-length basic types. The arguments are the same as they would
1791 * be for dbus_message_iter_get_basic() or
1792 * dbus_message_iter_get_fixed_array().
1794 * In addition to those types, arrays of string, object path, and
1795 * signature are supported; but these are returned as allocated memory
1796 * and must be freed with dbus_free_string_array(), while the other
1797 * types are returned as const references. To get a string array
1798 * pass in "char ***array_location" and "int *n_elements".
1800 * Similar to dbus_message_get_fixed_array() this function does not
1801 * support arrays of type DBUS_TYPE_UNIX_FD. If you need to parse
1802 * messages with arrays of Unix file descriptors you need to recurse
1803 * into the array manually.
1805 * Unix file descriptors that are read with this function will have
1806 * the FD_CLOEXEC flag set. If you need them without this flag set,
1807 * make sure to unset it with fcntl().
1809 * The variable argument list should contain the type of the argument
1810 * followed by a pointer to where the value should be stored. The list
1811 * is terminated with #DBUS_TYPE_INVALID.
1813 * Except for string arrays, the returned values are constant; do not
1814 * free them. They point into the #DBusMessage.
1816 * If the requested arguments are not present, or do not have the
1817 * requested types, then an error will be set.
1819 * If more arguments than requested are present, the requested
1820 * arguments are returned and the extra arguments are ignored.
1822 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1824 * @param message the message
1825 * @param error error to be filled in on failure
1826 * @param first_arg_type the first argument type
1827 * @param ... location for first argument value, then list of type-location pairs
1828 * @returns #FALSE if the error was set
1831 dbus_message_get_args (DBusMessage *message,
1839 _dbus_return_val_if_fail (message != NULL, FALSE);
1840 _dbus_return_val_if_error_is_set (error, FALSE);
1842 va_start (var_args, first_arg_type);
1843 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1850 * Like dbus_message_get_args but takes a va_list for use by language bindings.
1852 * @see dbus_message_get_args
1853 * @param message the message
1854 * @param error error to be filled in
1855 * @param first_arg_type type of the first argument
1856 * @param var_args return location for first argument, followed by list of type/location pairs
1857 * @returns #FALSE if error was set
1860 dbus_message_get_args_valist (DBusMessage *message,
1865 DBusMessageIter iter;
1867 _dbus_return_val_if_fail (message != NULL, FALSE);
1868 _dbus_return_val_if_error_is_set (error, FALSE);
1870 dbus_message_iter_init (message, &iter);
1871 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1875 _dbus_message_iter_init_common (DBusMessage *message,
1876 DBusMessageRealIter *real,
1879 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1881 /* Since the iterator will read or write who-knows-what from the
1882 * message, we need to get in the right byte order
1884 ensure_byte_order (message);
1886 real->message = message;
1887 real->changed_stamp = message->changed_stamp;
1888 real->iter_type = iter_type;
1889 real->sig_refcount = 0;
1893 * Initializes a #DBusMessageIter for reading the arguments of the
1894 * message passed in.
1896 * When possible, dbus_message_get_args() is much more convenient.
1897 * Some types of argument can only be read with #DBusMessageIter
1900 * The easiest way to iterate is like this:
1902 * dbus_message_iter_init (message, &iter);
1903 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1904 * dbus_message_iter_next (&iter);
1907 * #DBusMessageIter contains no allocated memory; it need not be
1908 * freed, and can be copied by assignment or memcpy().
1910 * @param message the message
1911 * @param iter pointer to an iterator to initialize
1912 * @returns #FALSE if the message has no arguments
1915 dbus_message_iter_init (DBusMessage *message,
1916 DBusMessageIter *iter)
1918 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1919 const DBusString *type_str;
1922 _dbus_return_val_if_fail (message != NULL, FALSE);
1923 _dbus_return_val_if_fail (iter != NULL, FALSE);
1925 get_const_signature (&message->header, &type_str, &type_pos);
1927 _dbus_message_iter_init_common (message, real,
1928 DBUS_MESSAGE_ITER_TYPE_READER);
1930 _dbus_type_reader_init (&real->u.reader,
1931 message->byte_order,
1936 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1940 * Checks if an iterator has any more fields.
1942 * @param iter the message iter
1943 * @returns #TRUE if there are more fields following
1946 dbus_message_iter_has_next (DBusMessageIter *iter)
1948 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1950 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1951 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1953 return _dbus_type_reader_has_next (&real->u.reader);
1957 * Moves the iterator to the next field, if any. If there's no next
1958 * field, returns #FALSE. If the iterator moves forward, returns
1961 * @param iter the message iter
1962 * @returns #TRUE if the iterator was moved to the next field
1965 dbus_message_iter_next (DBusMessageIter *iter)
1967 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1969 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1970 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1972 return _dbus_type_reader_next (&real->u.reader);
1976 * Returns the argument type of the argument that the message iterator
1977 * points to. If the iterator is at the end of the message, returns
1978 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1981 * dbus_message_iter_init (&iter);
1982 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1983 * dbus_message_iter_next (&iter);
1986 * @param iter the message iter
1987 * @returns the argument type
1990 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1992 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1994 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1995 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1997 return _dbus_type_reader_get_current_type (&real->u.reader);
2001 * Returns the element type of the array that the message iterator
2002 * points to. Note that you need to check that the iterator points to
2003 * an array prior to using this function.
2005 * @param iter the message iter
2006 * @returns the array element type
2009 dbus_message_iter_get_element_type (DBusMessageIter *iter)
2011 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2013 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2014 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
2015 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
2017 return _dbus_type_reader_get_element_type (&real->u.reader);
2021 * Recurses into a container value when reading values from a message,
2022 * initializing a sub-iterator to use for traversing the child values
2025 * Note that this recurses into a value, not a type, so you can only
2026 * recurse if the value exists. The main implication of this is that
2027 * if you have for example an empty array of array of int32, you can
2028 * recurse into the outermost array, but it will have no values, so
2029 * you won't be able to recurse further. There's no array of int32 to
2032 * If a container is an array of fixed-length types (except Unix file
2033 * descriptors), it is much more efficient to use
2034 * dbus_message_iter_get_fixed_array() to get the whole array in one
2035 * shot, rather than individually walking over the array elements.
2037 * Be sure you have somehow checked that
2038 * dbus_message_iter_get_arg_type() matches the type you are expecting
2039 * to recurse into. Results of this function are undefined if there is
2040 * no container to recurse into at the current iterator position.
2042 * @param iter the message iterator
2043 * @param sub the sub-iterator to initialize
2046 dbus_message_iter_recurse (DBusMessageIter *iter,
2047 DBusMessageIter *sub)
2049 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2050 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2052 _dbus_return_if_fail (_dbus_message_iter_check (real));
2053 _dbus_return_if_fail (sub != NULL);
2056 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
2060 * Returns the current signature of a message iterator. This
2061 * is useful primarily for dealing with variants; one can
2062 * recurse into a variant and determine the signature of
2063 * the variant's value.
2065 * The returned string must be freed with dbus_free().
2067 * @param iter the message iterator
2068 * @returns the contained signature, or NULL if out of memory
2071 dbus_message_iter_get_signature (DBusMessageIter *iter)
2073 const DBusString *sig;
2077 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2079 _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
2081 if (!_dbus_string_init (&retstr))
2084 _dbus_type_reader_get_signature (&real->u.reader, &sig,
2086 if (!_dbus_string_append_len (&retstr,
2087 _dbus_string_get_const_data (sig) + start,
2090 if (!_dbus_string_steal_data (&retstr, &ret))
2092 _dbus_string_free (&retstr);
2097 * Reads a basic-typed value from the message iterator.
2098 * Basic types are the non-containers such as integer and string.
2100 * The value argument should be the address of a location to store
2101 * the returned value. So for int32 it should be a "dbus_int32_t*"
2102 * and for string a "const char**". The returned value is
2103 * by reference and should not be freed.
2105 * This call duplicates Unix file descriptors when reading them. It is
2106 * your job to close them when you don't need them anymore.
2108 * Unix file descriptors that are read with this function will have
2109 * the FD_CLOEXEC flag set. If you need them without this flag set,
2110 * make sure to unset it with fcntl().
2112 * Be sure you have somehow checked that
2113 * dbus_message_iter_get_arg_type() matches the type you are
2114 * expecting, or you'll crash when you try to use an integer as a
2115 * string or something.
2117 * To read any container type (array, struct, dict) you will need to
2118 * recurse into the container with dbus_message_iter_recurse(). If
2119 * the container is an array of fixed-length values (except Unix file
2120 * descriptors), you can get all the array elements at once with
2121 * dbus_message_iter_get_fixed_array(). Otherwise, you have to iterate
2122 * over the container's contents one value at a time.
2124 * All basic-typed values are guaranteed to fit in 8 bytes. So you can
2125 * write code like this:
2128 * dbus_uint64_t value;
2130 * dbus_message_iter_get_basic (&read_iter, &value);
2131 * type = dbus_message_iter_get_arg_type (&read_iter);
2132 * dbus_message_iter_append_basic (&write_iter, type, &value);
2135 * On some really obscure platforms dbus_uint64_t might not exist, if
2136 * you need to worry about this you will know. dbus_uint64_t is just
2137 * one example of a type that's large enough to hold any possible
2138 * value, you could use a struct or char[8] instead if you like.
2140 * @param iter the iterator
2141 * @param value location to store the value
2144 dbus_message_iter_get_basic (DBusMessageIter *iter,
2147 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2149 _dbus_return_if_fail (_dbus_message_iter_check (real));
2150 _dbus_return_if_fail (value != NULL);
2152 if (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UNIX_FD)
2154 #ifdef HAVE_UNIX_FD_PASSING
2157 _dbus_type_reader_read_basic(&real->u.reader, &idx);
2159 if (idx.u32 >= real->message->n_unix_fds) {
2160 /* Hmm, we cannot really signal an error here, so let's make
2161 sure to return an invalid fd. */
2162 *((int*) value) = -1;
2166 *((int*) value) = _dbus_dup(real->message->unix_fds[idx.u32], NULL);
2168 *((int*) value) = -1;
2173 _dbus_type_reader_read_basic (&real->u.reader,
2179 * Returns the number of bytes in the array as marshaled in the wire
2180 * protocol. The iterator must currently be inside an array-typed
2183 * This function is deprecated on the grounds that it is stupid. Why
2184 * would you want to know how many bytes are in the array as marshaled
2185 * in the wire protocol? For now, use the n_elements returned from
2186 * dbus_message_iter_get_fixed_array() instead, or iterate over the
2187 * array values and count them.
2189 * @todo introduce a variant of this get_n_elements that returns
2190 * the number of elements, though with a non-fixed array it will not
2191 * be very efficient, so maybe it's not good.
2193 * @param iter the iterator
2194 * @returns the number of bytes in the array
2197 dbus_message_iter_get_array_len (DBusMessageIter *iter)
2199 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2201 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
2203 return _dbus_type_reader_get_array_length (&real->u.reader);
2207 * Reads a block of fixed-length values from the message iterator.
2208 * Fixed-length values are those basic types that are not string-like,
2209 * such as integers, bool, double. The returned block will be from the
2210 * current position in the array until the end of the array.
2212 * There is one exception here: although DBUS_TYPE_UNIX_FD is
2213 * considered a 'fixed' type arrays of this type may not be read with
2216 * The message iter should be "in" the array (that is, you recurse into the
2217 * array, and then you call dbus_message_iter_get_fixed_array() on the
2218 * "sub-iterator" created by dbus_message_iter_recurse()).
2220 * The value argument should be the address of a location to store the
2221 * returned array. So for int32 it should be a "const dbus_int32_t**"
2222 * The returned value is by reference and should not be freed.
2224 * This function should only be used if dbus_type_is_fixed() returns
2225 * #TRUE for the element type.
2227 * If an array's elements are not fixed in size, you have to recurse
2228 * into the array with dbus_message_iter_recurse() and read the
2229 * elements one by one.
2231 * Because the array is not copied, this function runs in constant
2232 * time and is fast; it's much preferred over walking the entire array
2233 * with an iterator. (However, you can always use
2234 * dbus_message_iter_recurse(), even for fixed-length types;
2235 * dbus_message_iter_get_fixed_array() is just an optimization.)
2237 * @param iter the iterator
2238 * @param value location to store the block
2239 * @param n_elements number of elements in the block
2242 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
2246 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2247 int subtype = _dbus_type_reader_get_current_type(&real->u.reader);
2249 _dbus_return_if_fail (_dbus_message_iter_check (real));
2250 _dbus_return_if_fail (value != NULL);
2251 _dbus_return_if_fail ((subtype == DBUS_TYPE_INVALID) ||
2252 (dbus_type_is_fixed (subtype) && subtype != DBUS_TYPE_UNIX_FD));
2254 _dbus_type_reader_read_fixed_multi (&real->u.reader,
2259 * Initializes a #DBusMessageIter for appending arguments to the end
2262 * @todo If appending any of the arguments fails due to lack of
2263 * memory, the message is hosed and you have to start over building
2264 * the whole message.
2266 * @param message the message
2267 * @param iter pointer to an iterator to initialize
2270 dbus_message_iter_init_append (DBusMessage *message,
2271 DBusMessageIter *iter)
2273 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2275 _dbus_return_if_fail (message != NULL);
2276 _dbus_return_if_fail (iter != NULL);
2278 _dbus_message_iter_init_common (message, real,
2279 DBUS_MESSAGE_ITER_TYPE_WRITER);
2281 /* We create the signature string and point iterators at it "on demand"
2282 * when a value is actually appended. That means that init() never fails
2285 _dbus_type_writer_init_types_delayed (&real->u.writer,
2286 message->byte_order,
2288 _dbus_string_get_length (&message->body));
2292 * Creates a temporary signature string containing the current
2293 * signature, stores it in the iterator, and points the iterator to
2294 * the end of it. Used any time we write to the message.
2296 * @param real an iterator without a type_str
2297 * @returns #FALSE if no memory
2300 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
2303 const DBusString *current_sig;
2304 int current_sig_pos;
2306 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2308 if (real->u.writer.type_str != NULL)
2310 _dbus_assert (real->sig_refcount > 0);
2311 real->sig_refcount += 1;
2315 str = dbus_new (DBusString, 1);
2319 if (!_dbus_header_get_field_raw (&real->message->header,
2320 DBUS_HEADER_FIELD_SIGNATURE,
2321 ¤t_sig, ¤t_sig_pos))
2328 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2329 current_sig_pos += 1; /* move on to sig data */
2331 if (!_dbus_string_init_preallocated (str, current_len + 4))
2337 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2340 _dbus_string_free (str);
2347 if (!_dbus_string_init_preallocated (str, 4))
2354 real->sig_refcount = 1;
2356 _dbus_type_writer_add_types (&real->u.writer,
2357 str, _dbus_string_get_length (str));
2362 * Sets the new signature as the message signature, frees the
2363 * signature string, and marks the iterator as not having a type_str
2364 * anymore. Frees the signature even if it fails, so you can't
2365 * really recover from failure. Kinda busted.
2367 * @param real an iterator without a type_str
2368 * @returns #FALSE if no memory
2371 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2374 const char *v_STRING;
2377 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2378 _dbus_assert (real->u.writer.type_str != NULL);
2379 _dbus_assert (real->sig_refcount > 0);
2381 real->sig_refcount -= 1;
2383 if (real->sig_refcount > 0)
2385 _dbus_assert (real->sig_refcount == 0);
2389 str = real->u.writer.type_str;
2391 v_STRING = _dbus_string_get_const_data (str);
2392 if (!_dbus_header_set_field_basic (&real->message->header,
2393 DBUS_HEADER_FIELD_SIGNATURE,
2394 DBUS_TYPE_SIGNATURE,
2398 _dbus_type_writer_remove_types (&real->u.writer);
2399 _dbus_string_free (str);
2406 * Frees the signature string and marks the iterator as not having a
2407 * type_str anymore. Since the new signature is not set, the message
2408 * will generally be hosed after this is called.
2410 * @param real an iterator without a type_str
2413 _dbus_message_iter_abandon_signature (DBusMessageRealIter *real)
2417 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2418 _dbus_assert (real->u.writer.type_str != NULL);
2419 _dbus_assert (real->sig_refcount > 0);
2421 real->sig_refcount -= 1;
2423 if (real->sig_refcount > 0)
2425 _dbus_assert (real->sig_refcount == 0);
2427 str = real->u.writer.type_str;
2429 _dbus_type_writer_remove_types (&real->u.writer);
2430 _dbus_string_free (str);
2434 #ifndef DBUS_DISABLE_CHECKS
2436 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2438 if (!_dbus_message_iter_check (iter))
2441 if (iter->message->locked)
2443 _dbus_warn_check_failed ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2449 #endif /* DBUS_DISABLE_CHECKS */
2451 #ifdef HAVE_UNIX_FD_PASSING
2453 expand_fd_array(DBusMessage *m,
2458 /* This makes space for adding n new fds to the array and returns a
2459 pointer to the place were the first fd should be put. */
2461 if (m->n_unix_fds + n > m->n_unix_fds_allocated)
2466 /* Make twice as much space as necessary */
2467 k = (m->n_unix_fds + n) * 2;
2469 /* Allocate at least four */
2473 p = dbus_realloc(m->unix_fds, k * sizeof(int));
2478 m->n_unix_fds_allocated = k;
2481 return m->unix_fds + m->n_unix_fds;
2486 * Appends a basic-typed value to the message. The basic types are the
2487 * non-container types such as integer and string.
2489 * The "value" argument should be the address of a basic-typed value.
2490 * So for string, const char**. For integer, dbus_int32_t*.
2492 * For Unix file descriptors this function will internally duplicate
2493 * the descriptor you passed in. Hence you may close the descriptor
2494 * immediately after this call.
2496 * @todo If this fails due to lack of memory, the message is hosed and
2497 * you have to start over building the whole message.
2499 * @param iter the append iterator
2500 * @param type the type of the value
2501 * @param value the address of the value
2502 * @returns #FALSE if not enough memory
2505 dbus_message_iter_append_basic (DBusMessageIter *iter,
2509 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2512 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2513 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2514 _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2515 _dbus_return_val_if_fail (value != NULL, FALSE);
2517 if (!_dbus_message_iter_open_signature (real))
2520 if (type == DBUS_TYPE_UNIX_FD)
2522 #ifdef HAVE_UNIX_FD_PASSING
2526 /* First step, include the fd in the fd list of this message */
2527 if (!(fds = expand_fd_array(real->message, 1)))
2530 *fds = _dbus_dup(*(int*) value, NULL);
2534 u = real->message->n_unix_fds;
2536 /* Second step, write the index to the fd */
2537 if (!(ret = _dbus_type_writer_write_basic (&real->u.writer, DBUS_TYPE_UNIX_FD, &u))) {
2538 _dbus_close(*fds, NULL);
2542 real->message->n_unix_fds += 1;
2545 /* Final step, update the header accordingly */
2546 ret = _dbus_header_set_field_basic (&real->message->header,
2547 DBUS_HEADER_FIELD_UNIX_FDS,
2551 /* If any of these operations fail the message is
2552 hosed. However, no memory or fds should be leaked since what
2553 has been added to message has been added to the message, and
2554 can hence be accounted for when the message is being
2562 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2565 if (!_dbus_message_iter_close_signature (real))
2572 * Appends a block of fixed-length values to an array. The
2573 * fixed-length types are all basic types that are not string-like. So
2574 * int32, double, bool, etc. (Unix file descriptors however are not
2575 * supported.) You must call dbus_message_iter_open_container() to
2576 * open an array of values before calling this function. You may call
2577 * this function multiple times (and intermixed with calls to
2578 * dbus_message_iter_append_basic()) for the same array.
2580 * The "value" argument should be the address of the array. So for
2581 * integer, "dbus_int32_t**" is expected for example.
2583 * @warning in C, given "int array[]", "&array == array" (the
2584 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2585 * So if you're using an array instead of a pointer you have to create
2586 * a pointer variable, assign the array to it, then take the address
2587 * of the pointer variable.
2589 * const dbus_int32_t array[] = { 1, 2, 3 };
2590 * const dbus_int32_t *v_ARRAY = array;
2591 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2592 * fprintf (stderr, "No memory!\n");
2594 * For strings it works to write const char *array = "Hello" and then
2595 * use &array though.
2597 * @todo If this fails due to lack of memory, the message is hosed and
2598 * you have to start over building the whole message.
2600 * For Unix file descriptors this function will internally duplicate
2601 * the descriptor you passed in. Hence you may close the descriptor
2602 * immediately after this call.
2604 * @param iter the append iterator
2605 * @param element_type the type of the array elements
2606 * @param value the address of the array
2607 * @param n_elements the number of elements to append
2608 * @returns #FALSE if not enough memory
2611 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2616 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2619 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2620 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2621 _dbus_return_val_if_fail (dbus_type_is_fixed (element_type) && element_type != DBUS_TYPE_UNIX_FD, FALSE);
2622 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2623 _dbus_return_val_if_fail (value != NULL, FALSE);
2624 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2625 _dbus_return_val_if_fail (n_elements <=
2626 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2629 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2635 * Appends a container-typed value to the message; you are required to
2636 * append the contents of the container using the returned
2637 * sub-iterator, and then call
2638 * dbus_message_iter_close_container(). Container types are for
2639 * example struct, variant, and array. For variants, the
2640 * contained_signature should be the type of the single value inside
2641 * the variant. For structs and dict entries, contained_signature
2642 * should be #NULL; it will be set to whatever types you write into
2643 * the struct. For arrays, contained_signature should be the type of
2644 * the array elements.
2646 * @todo If this fails due to lack of memory, the message is hosed and
2647 * you have to start over building the whole message.
2649 * @param iter the append iterator
2650 * @param type the type of the value
2651 * @param contained_signature the type of container contents
2652 * @param sub sub-iterator to initialize
2653 * @returns #FALSE if not enough memory
2656 dbus_message_iter_open_container (DBusMessageIter *iter,
2658 const char *contained_signature,
2659 DBusMessageIter *sub)
2661 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2662 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2663 DBusString contained_str;
2665 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2666 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2667 _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
2668 _dbus_return_val_if_fail (sub != NULL, FALSE);
2669 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2670 contained_signature == NULL) ||
2671 (type == DBUS_TYPE_DICT_ENTRY &&
2672 contained_signature == NULL) ||
2673 (type == DBUS_TYPE_VARIANT &&
2674 contained_signature != NULL) ||
2675 (type == DBUS_TYPE_ARRAY &&
2676 contained_signature != NULL), FALSE);
2678 /* this would fail if the contained_signature is a dict entry, since
2679 * dict entries are invalid signatures standalone (they must be in
2682 _dbus_return_val_if_fail ((type == DBUS_TYPE_ARRAY && contained_signature && *contained_signature == DBUS_DICT_ENTRY_BEGIN_CHAR) ||
2683 (contained_signature == NULL ||
2684 _dbus_check_is_valid_signature (contained_signature)),
2687 if (!_dbus_message_iter_open_signature (real))
2692 if (contained_signature != NULL)
2694 _dbus_string_init_const (&contained_str, contained_signature);
2696 return _dbus_type_writer_recurse (&real->u.writer,
2699 &real_sub->u.writer);
2703 return _dbus_type_writer_recurse (&real->u.writer,
2706 &real_sub->u.writer);
2712 * Closes a container-typed value appended to the message; may write
2713 * out more information to the message known only after the entire
2714 * container is written, and may free resources created by
2715 * dbus_message_iter_open_container().
2717 * @todo If this fails due to lack of memory, the message is hosed and
2718 * you have to start over building the whole message.
2720 * @param iter the append iterator
2721 * @param sub sub-iterator to close
2722 * @returns #FALSE if not enough memory
2725 dbus_message_iter_close_container (DBusMessageIter *iter,
2726 DBusMessageIter *sub)
2728 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2729 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2732 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2733 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2734 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2735 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2737 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2738 &real_sub->u.writer);
2740 if (!_dbus_message_iter_close_signature (real))
2747 * Abandons creation of a contained-typed value and frees resources created
2748 * by dbus_message_iter_open_container(). Once this returns, the message
2749 * is hosed and you have to start over building the whole message.
2751 * This should only be used to abandon creation of a message when you have
2754 * @param iter the append iterator
2755 * @param sub sub-iterator to close
2758 dbus_message_iter_abandon_container (DBusMessageIter *iter,
2759 DBusMessageIter *sub)
2761 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2762 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2764 _dbus_return_if_fail (_dbus_message_iter_append_check (real));
2765 _dbus_return_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2766 _dbus_return_if_fail (_dbus_message_iter_append_check (real_sub));
2767 _dbus_return_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2769 _dbus_message_iter_abandon_signature (real);
2773 * Sets a flag indicating that the message does not want a reply; if
2774 * this flag is set, the other end of the connection may (but is not
2775 * required to) optimize by not sending method return or error
2776 * replies. If this flag is set, there is no way to know whether the
2777 * message successfully arrived at the remote end. Normally you know a
2778 * message was received when you receive the reply to it.
2780 * The flag is #FALSE by default, that is by default the other end is
2781 * required to reply.
2783 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_REPLY_EXPECTED
2785 * @param message the message
2786 * @param no_reply #TRUE if no reply is desired
2789 dbus_message_set_no_reply (DBusMessage *message,
2790 dbus_bool_t no_reply)
2792 _dbus_return_if_fail (message != NULL);
2793 _dbus_return_if_fail (!message->locked);
2795 _dbus_header_toggle_flag (&message->header,
2796 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2801 * Returns #TRUE if the message does not expect
2804 * @param message the message
2805 * @returns #TRUE if the message sender isn't waiting for a reply
2808 dbus_message_get_no_reply (DBusMessage *message)
2810 _dbus_return_val_if_fail (message != NULL, FALSE);
2812 return _dbus_header_get_flag (&message->header,
2813 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2817 * Sets a flag indicating that an owner for the destination name will
2818 * be automatically started before the message is delivered. When this
2819 * flag is set, the message is held until a name owner finishes
2820 * starting up, or fails to start up. In case of failure, the reply
2823 * The flag is set to #TRUE by default, i.e. auto starting is the default.
2825 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_AUTO_START
2827 * @param message the message
2828 * @param auto_start #TRUE if auto-starting is desired
2831 dbus_message_set_auto_start (DBusMessage *message,
2832 dbus_bool_t auto_start)
2834 _dbus_return_if_fail (message != NULL);
2835 _dbus_return_if_fail (!message->locked);
2837 _dbus_header_toggle_flag (&message->header,
2838 DBUS_HEADER_FLAG_NO_AUTO_START,
2843 * Returns #TRUE if the message will cause an owner for
2844 * destination name to be auto-started.
2846 * @param message the message
2847 * @returns #TRUE if the message will use auto-start
2850 dbus_message_get_auto_start (DBusMessage *message)
2852 _dbus_return_val_if_fail (message != NULL, FALSE);
2854 return !_dbus_header_get_flag (&message->header,
2855 DBUS_HEADER_FLAG_NO_AUTO_START);
2860 * Sets the object path this message is being sent to (for
2861 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2862 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2864 * The path must contain only valid characters as defined
2865 * in the D-Bus specification.
2867 * @param message the message
2868 * @param object_path the path or #NULL to unset
2869 * @returns #FALSE if not enough memory
2872 dbus_message_set_path (DBusMessage *message,
2873 const char *object_path)
2875 _dbus_return_val_if_fail (message != NULL, FALSE);
2876 _dbus_return_val_if_fail (!message->locked, FALSE);
2877 _dbus_return_val_if_fail (object_path == NULL ||
2878 _dbus_check_is_valid_path (object_path),
2881 return set_or_delete_string_field (message,
2882 DBUS_HEADER_FIELD_PATH,
2883 DBUS_TYPE_OBJECT_PATH,
2888 * Gets the object path this message is being sent to (for
2889 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2890 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2892 * See also dbus_message_get_path_decomposed().
2894 * The returned string becomes invalid if the message is
2895 * modified, since it points into the wire-marshaled message data.
2897 * @param message the message
2898 * @returns the path (should not be freed) or #NULL
2901 dbus_message_get_path (DBusMessage *message)
2905 _dbus_return_val_if_fail (message != NULL, NULL);
2907 v = NULL; /* in case field doesn't exist */
2908 _dbus_header_get_field_basic (&message->header,
2909 DBUS_HEADER_FIELD_PATH,
2910 DBUS_TYPE_OBJECT_PATH,
2916 * Checks if the message has a particular object path. The object
2917 * path is the destination object for a method call or the emitting
2918 * object for a signal.
2920 * @param message the message
2921 * @param path the path name
2922 * @returns #TRUE if there is a path field in the header
2925 dbus_message_has_path (DBusMessage *message,
2928 const char *msg_path;
2929 msg_path = dbus_message_get_path (message);
2931 if (msg_path == NULL)
2942 if (strcmp (msg_path, path) == 0)
2949 * Gets the object path this message is being sent to
2950 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2951 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2952 * format (one array element per path component).
2953 * Free the returned array with dbus_free_string_array().
2955 * An empty but non-NULL path array means the path "/".
2956 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2957 * and the path "/" becomes { NULL }.
2959 * See also dbus_message_get_path().
2961 * @todo this could be optimized by using the len from the message
2962 * instead of calling strlen() again
2964 * @param message the message
2965 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2966 * @returns #FALSE if no memory to allocate the array
2969 dbus_message_get_path_decomposed (DBusMessage *message,
2974 _dbus_return_val_if_fail (message != NULL, FALSE);
2975 _dbus_return_val_if_fail (path != NULL, FALSE);
2979 v = dbus_message_get_path (message);
2982 if (!_dbus_decompose_path (v, strlen (v),
2990 * Sets the interface this message is being sent to
2991 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2992 * the interface a signal is being emitted from
2993 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2995 * The interface name must contain only valid characters as defined
2996 * in the D-Bus specification.
2998 * @param message the message
2999 * @param interface the interface or #NULL to unset
3000 * @returns #FALSE if not enough memory
3003 dbus_message_set_interface (DBusMessage *message,
3004 const char *interface)
3006 _dbus_return_val_if_fail (message != NULL, FALSE);
3007 _dbus_return_val_if_fail (!message->locked, FALSE);
3008 _dbus_return_val_if_fail (interface == NULL ||
3009 _dbus_check_is_valid_interface (interface),
3012 return set_or_delete_string_field (message,
3013 DBUS_HEADER_FIELD_INTERFACE,
3019 * Gets the interface this message is being sent to
3020 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
3021 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
3022 * The interface name is fully-qualified (namespaced).
3023 * Returns #NULL if none.
3025 * The returned string becomes invalid if the message is
3026 * modified, since it points into the wire-marshaled message data.
3028 * @param message the message
3029 * @returns the message interface (should not be freed) or #NULL
3032 dbus_message_get_interface (DBusMessage *message)
3036 _dbus_return_val_if_fail (message != NULL, NULL);
3038 v = NULL; /* in case field doesn't exist */
3039 _dbus_header_get_field_basic (&message->header,
3040 DBUS_HEADER_FIELD_INTERFACE,
3047 * Checks if the message has an interface
3049 * @param message the message
3050 * @param interface the interface name
3051 * @returns #TRUE if the interface field in the header matches
3054 dbus_message_has_interface (DBusMessage *message,
3055 const char *interface)
3057 const char *msg_interface;
3058 msg_interface = dbus_message_get_interface (message);
3060 if (msg_interface == NULL)
3062 if (interface == NULL)
3068 if (interface == NULL)
3071 if (strcmp (msg_interface, interface) == 0)
3079 * Sets the interface member being invoked
3080 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
3081 * (DBUS_MESSAGE_TYPE_SIGNAL).
3083 * The member name must contain only valid characters as defined
3084 * in the D-Bus specification.
3086 * @param message the message
3087 * @param member the member or #NULL to unset
3088 * @returns #FALSE if not enough memory
3091 dbus_message_set_member (DBusMessage *message,
3094 _dbus_return_val_if_fail (message != NULL, FALSE);
3095 _dbus_return_val_if_fail (!message->locked, FALSE);
3096 _dbus_return_val_if_fail (member == NULL ||
3097 _dbus_check_is_valid_member (member),
3100 return set_or_delete_string_field (message,
3101 DBUS_HEADER_FIELD_MEMBER,
3107 * Gets the interface member being invoked
3108 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
3109 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
3111 * The returned string becomes invalid if the message is
3112 * modified, since it points into the wire-marshaled message data.
3114 * @param message the message
3115 * @returns the member name (should not be freed) or #NULL
3118 dbus_message_get_member (DBusMessage *message)
3122 _dbus_return_val_if_fail (message != NULL, NULL);
3124 v = NULL; /* in case field doesn't exist */
3125 _dbus_header_get_field_basic (&message->header,
3126 DBUS_HEADER_FIELD_MEMBER,
3133 * Checks if the message has an interface member
3135 * @param message the message
3136 * @param member the member name
3137 * @returns #TRUE if there is a member field in the header
3140 dbus_message_has_member (DBusMessage *message,
3143 const char *msg_member;
3144 msg_member = dbus_message_get_member (message);
3146 if (msg_member == NULL)
3157 if (strcmp (msg_member, member) == 0)
3165 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
3166 * The name is fully-qualified (namespaced).
3168 * The error name must contain only valid characters as defined
3169 * in the D-Bus specification.
3171 * @param message the message
3172 * @param error_name the name or #NULL to unset
3173 * @returns #FALSE if not enough memory
3176 dbus_message_set_error_name (DBusMessage *message,
3177 const char *error_name)
3179 _dbus_return_val_if_fail (message != NULL, FALSE);
3180 _dbus_return_val_if_fail (!message->locked, FALSE);
3181 _dbus_return_val_if_fail (error_name == NULL ||
3182 _dbus_check_is_valid_error_name (error_name),
3185 return set_or_delete_string_field (message,
3186 DBUS_HEADER_FIELD_ERROR_NAME,
3192 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
3195 * The returned string becomes invalid if the message is
3196 * modified, since it points into the wire-marshaled message data.
3198 * @param message the message
3199 * @returns the error name (should not be freed) or #NULL
3202 dbus_message_get_error_name (DBusMessage *message)
3206 _dbus_return_val_if_fail (message != NULL, NULL);
3208 v = NULL; /* in case field doesn't exist */
3209 _dbus_header_get_field_basic (&message->header,
3210 DBUS_HEADER_FIELD_ERROR_NAME,
3217 * Sets the message's destination. The destination is the name of
3218 * another connection on the bus and may be either the unique name
3219 * assigned by the bus to each connection, or a well-known name
3220 * specified in advance.
3222 * The destination name must contain only valid characters as defined
3223 * in the D-Bus specification.
3225 * @param message the message
3226 * @param destination the destination name or #NULL to unset
3227 * @returns #FALSE if not enough memory
3230 dbus_message_set_destination (DBusMessage *message,
3231 const char *destination)
3233 _dbus_return_val_if_fail (message != NULL, FALSE);
3234 _dbus_return_val_if_fail (!message->locked, FALSE);
3235 _dbus_return_val_if_fail (destination == NULL ||
3236 _dbus_check_is_valid_bus_name (destination),
3239 return set_or_delete_string_field (message,
3240 DBUS_HEADER_FIELD_DESTINATION,
3246 * Gets the destination of a message or #NULL if there is none set.
3248 * The returned string becomes invalid if the message is
3249 * modified, since it points into the wire-marshaled message data.
3251 * @param message the message
3252 * @returns the message destination (should not be freed) or #NULL
3255 dbus_message_get_destination (DBusMessage *message)
3259 _dbus_return_val_if_fail (message != NULL, NULL);
3261 v = NULL; /* in case field doesn't exist */
3262 _dbus_header_get_field_basic (&message->header,
3263 DBUS_HEADER_FIELD_DESTINATION,
3270 * Sets the message sender.
3272 * The sender must be a valid bus name as defined in the D-Bus
3275 * Usually you don't want to call this. The message bus daemon will
3276 * call it to set the origin of each message. If you aren't implementing
3277 * a message bus daemon you shouldn't need to set the sender.
3279 * @param message the message
3280 * @param sender the sender or #NULL to unset
3281 * @returns #FALSE if not enough memory
3284 dbus_message_set_sender (DBusMessage *message,
3287 _dbus_return_val_if_fail (message != NULL, FALSE);
3288 _dbus_return_val_if_fail (!message->locked, FALSE);
3289 _dbus_return_val_if_fail (sender == NULL ||
3290 _dbus_check_is_valid_bus_name (sender),
3293 return set_or_delete_string_field (message,
3294 DBUS_HEADER_FIELD_SENDER,
3300 * Gets the unique name of the connection which originated this
3301 * message, or #NULL if unknown or inapplicable. The sender is filled
3302 * in by the message bus.
3304 * Note, the returned sender is always the unique bus name.
3305 * Connections may own multiple other bus names, but those
3306 * are not found in the sender field.
3308 * The returned string becomes invalid if the message is
3309 * modified, since it points into the wire-marshaled message data.
3311 * @param message the message
3312 * @returns the unique name of the sender or #NULL
3315 dbus_message_get_sender (DBusMessage *message)
3319 _dbus_return_val_if_fail (message != NULL, NULL);
3321 v = NULL; /* in case field doesn't exist */
3322 _dbus_header_get_field_basic (&message->header,
3323 DBUS_HEADER_FIELD_SENDER,
3330 * Gets the type signature of the message, i.e. the arguments in the
3331 * message payload. The signature includes only "in" arguments for
3332 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
3333 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
3334 * what you might expect (that is, it does not include the signature of the
3335 * entire C++-style method).
3337 * The signature is a string made up of type codes such as
3338 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
3339 * the value of #DBUS_TYPE_INVALID).
3341 * The returned string becomes invalid if the message is
3342 * modified, since it points into the wire-marshaled message data.
3344 * @param message the message
3345 * @returns the type signature
3348 dbus_message_get_signature (DBusMessage *message)
3350 const DBusString *type_str;
3353 _dbus_return_val_if_fail (message != NULL, NULL);
3355 get_const_signature (&message->header, &type_str, &type_pos);
3357 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
3361 _dbus_message_has_type_interface_member (DBusMessage *message,
3363 const char *interface,
3368 _dbus_assert (message != NULL);
3369 _dbus_assert (interface != NULL);
3370 _dbus_assert (member != NULL);
3372 if (dbus_message_get_type (message) != type)
3375 /* Optimize by checking the short member name first
3376 * instead of the longer interface name
3379 n = dbus_message_get_member (message);
3381 if (n && strcmp (n, member) == 0)
3383 n = dbus_message_get_interface (message);
3385 if (n == NULL || strcmp (n, interface) == 0)
3393 * Checks whether the message is a method call with the given
3394 * interface and member fields. If the message is not
3395 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
3396 * member field, returns #FALSE. If the interface field is missing,
3397 * then it will be assumed equal to the provided interface. The D-Bus
3398 * protocol allows method callers to leave out the interface name.
3400 * @param message the message
3401 * @param interface the name to check (must not be #NULL)
3402 * @param method the name to check (must not be #NULL)
3404 * @returns #TRUE if the message is the specified method call
3407 dbus_message_is_method_call (DBusMessage *message,
3408 const char *interface,
3411 _dbus_return_val_if_fail (message != NULL, FALSE);
3412 _dbus_return_val_if_fail (interface != NULL, FALSE);
3413 _dbus_return_val_if_fail (method != NULL, FALSE);
3414 /* don't check that interface/method are valid since it would be
3415 * expensive, and not catch many common errors
3418 return _dbus_message_has_type_interface_member (message,
3419 DBUS_MESSAGE_TYPE_METHOD_CALL,
3424 * Checks whether the message is a signal with the given interface and
3425 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
3426 * has a different interface or member field, returns #FALSE.
3428 * @param message the message
3429 * @param interface the name to check (must not be #NULL)
3430 * @param signal_name the name to check (must not be #NULL)
3432 * @returns #TRUE if the message is the specified signal
3435 dbus_message_is_signal (DBusMessage *message,
3436 const char *interface,
3437 const char *signal_name)
3439 _dbus_return_val_if_fail (message != NULL, FALSE);
3440 _dbus_return_val_if_fail (interface != NULL, FALSE);
3441 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
3442 /* don't check that interface/name are valid since it would be
3443 * expensive, and not catch many common errors
3446 return _dbus_message_has_type_interface_member (message,
3447 DBUS_MESSAGE_TYPE_SIGNAL,
3448 interface, signal_name);
3452 * Checks whether the message is an error reply with the given error
3453 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
3454 * different name, returns #FALSE.
3456 * @param message the message
3457 * @param error_name the name to check (must not be #NULL)
3459 * @returns #TRUE if the message is the specified error
3462 dbus_message_is_error (DBusMessage *message,
3463 const char *error_name)
3467 _dbus_return_val_if_fail (message != NULL, FALSE);
3468 _dbus_return_val_if_fail (error_name != NULL, FALSE);
3469 /* don't check that error_name is valid since it would be expensive,
3470 * and not catch many common errors
3473 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3476 n = dbus_message_get_error_name (message);
3478 if (n && strcmp (n, error_name) == 0)
3485 * Checks whether the message was sent to the given name. If the
3486 * message has no destination specified or has a different
3487 * destination, returns #FALSE.
3489 * @param message the message
3490 * @param name the name to check (must not be #NULL)
3492 * @returns #TRUE if the message has the given destination name
3495 dbus_message_has_destination (DBusMessage *message,
3500 _dbus_return_val_if_fail (message != NULL, FALSE);
3501 _dbus_return_val_if_fail (name != NULL, FALSE);
3502 /* don't check that name is valid since it would be expensive, and
3503 * not catch many common errors
3506 s = dbus_message_get_destination (message);
3508 if (s && strcmp (s, name) == 0)
3515 * Checks whether the message has the given unique name as its sender.
3516 * If the message has no sender specified or has a different sender,
3517 * returns #FALSE. Note that a peer application will always have the
3518 * unique name of the connection as the sender. So you can't use this
3519 * function to see whether a sender owned a well-known name.
3521 * Messages from the bus itself will have #DBUS_SERVICE_DBUS
3524 * @param message the message
3525 * @param name the name to check (must not be #NULL)
3527 * @returns #TRUE if the message has the given sender
3530 dbus_message_has_sender (DBusMessage *message,
3535 _dbus_return_val_if_fail (message != NULL, FALSE);
3536 _dbus_return_val_if_fail (name != NULL, FALSE);
3537 /* don't check that name is valid since it would be expensive, and
3538 * not catch many common errors
3541 s = dbus_message_get_sender (message);
3543 if (s && strcmp (s, name) == 0)
3550 * Checks whether the message has the given signature; see
3551 * dbus_message_get_signature() for more details on what the signature
3554 * @param message the message
3555 * @param signature typecode array
3556 * @returns #TRUE if message has the given signature
3559 dbus_message_has_signature (DBusMessage *message,
3560 const char *signature)
3564 _dbus_return_val_if_fail (message != NULL, FALSE);
3565 _dbus_return_val_if_fail (signature != NULL, FALSE);
3566 /* don't check that signature is valid since it would be expensive,
3567 * and not catch many common errors
3570 s = dbus_message_get_signature (message);
3572 if (s && strcmp (s, signature) == 0)
3579 * Sets a #DBusError based on the contents of the given
3580 * message. The error is only set if the message
3581 * is an error message, as in #DBUS_MESSAGE_TYPE_ERROR.
3582 * The name of the error is set to the name of the message,
3583 * and the error message is set to the first argument
3584 * if the argument exists and is a string.
3586 * The return value indicates whether the error was set (the error is
3587 * set if and only if the message is an error message). So you can
3588 * check for an error reply and convert it to DBusError in one go:
3590 * if (dbus_set_error_from_message (error, reply))
3596 * @param error the error to set
3597 * @param message the message to set it from
3598 * @returns #TRUE if the message had type #DBUS_MESSAGE_TYPE_ERROR
3601 dbus_set_error_from_message (DBusError *error,
3602 DBusMessage *message)
3606 _dbus_return_val_if_fail (message != NULL, FALSE);
3607 _dbus_return_val_if_error_is_set (error, FALSE);
3609 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3613 dbus_message_get_args (message, NULL,
3614 DBUS_TYPE_STRING, &str,
3617 dbus_set_error (error, dbus_message_get_error_name (message),
3618 str ? "%s" : NULL, str);
3624 * Checks whether a message contains unix fds
3626 * @param message the message
3627 * @returns #TRUE if the message contains unix fds
3630 dbus_message_contains_unix_fds(DBusMessage *message)
3632 #ifdef HAVE_UNIX_FD_PASSING
3633 _dbus_assert(message);
3635 return message->n_unix_fds > 0;
3644 * @addtogroup DBusMessageInternals
3650 * The initial buffer size of the message loader.
3652 * @todo this should be based on min header size plus some average
3653 * body size, or something. Or rather, the min header size only, if we
3654 * want to try to read only the header, store that in a DBusMessage,
3655 * then read only the body and store that, etc., depends on
3656 * how we optimize _dbus_message_loader_get_buffer() and what
3657 * the exact message format is.
3659 #define INITIAL_LOADER_DATA_LEN 32
3662 * Creates a new message loader. Returns #NULL if memory can't
3665 * @returns new loader, or #NULL.
3668 _dbus_message_loader_new (void)
3670 DBusMessageLoader *loader;
3672 loader = dbus_new0 (DBusMessageLoader, 1);
3676 loader->refcount = 1;
3678 loader->corrupted = FALSE;
3679 loader->corruption_reason = DBUS_VALID;
3681 /* this can be configured by the app, but defaults to the protocol max */
3682 loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3684 /* We set a very relatively conservative default here since due to how
3685 SCM_RIGHTS works we need to preallocate an fd array of the maximum
3686 number of unix fds we want to receive in advance. A
3687 try-and-reallocate loop is not possible. */
3688 loader->max_message_unix_fds = 1024;
3690 if (!_dbus_string_init (&loader->data))
3696 /* preallocate the buffer for speed, ignore failure */
3697 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3698 _dbus_string_set_length (&loader->data, 0);
3700 #ifdef HAVE_UNIX_FD_PASSING
3701 loader->unix_fds = NULL;
3702 loader->n_unix_fds = loader->n_unix_fds_allocated = 0;
3703 loader->unix_fds_outstanding = FALSE;
3710 * Increments the reference count of the loader.
3712 * @param loader the loader.
3713 * @returns the loader
3716 _dbus_message_loader_ref (DBusMessageLoader *loader)
3718 loader->refcount += 1;
3724 * Decrements the reference count of the loader and finalizes the
3725 * loader when the count reaches zero.
3727 * @param loader the loader.
3730 _dbus_message_loader_unref (DBusMessageLoader *loader)
3732 loader->refcount -= 1;
3733 if (loader->refcount == 0)
3735 #ifdef HAVE_UNIX_FD_PASSING
3736 close_unix_fds(loader->unix_fds, &loader->n_unix_fds);
3737 dbus_free(loader->unix_fds);
3739 _dbus_list_foreach (&loader->messages,
3740 (DBusForeachFunction) dbus_message_unref,
3742 _dbus_list_clear (&loader->messages);
3743 _dbus_string_free (&loader->data);
3749 * Gets the buffer to use for reading data from the network. Network
3750 * data is read directly into an allocated buffer, which is then used
3751 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3752 * The buffer must always be returned immediately using
3753 * _dbus_message_loader_return_buffer(), even if no bytes are
3754 * successfully read.
3756 * @todo this function can be a lot more clever. For example
3757 * it can probably always return a buffer size to read exactly
3758 * the body of the next message, thus avoiding any memory wastage
3761 * @todo we need to enforce a max length on strings in header fields.
3763 * @param loader the message loader.
3764 * @param buffer the buffer
3767 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3768 DBusString **buffer)
3770 _dbus_assert (!loader->buffer_outstanding);
3772 *buffer = &loader->data;
3774 loader->buffer_outstanding = TRUE;
3778 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3779 * indicating to the loader how many bytes of the buffer were filled
3780 * in. This function must always be called, even if no bytes were
3781 * successfully read.
3783 * @param loader the loader.
3784 * @param buffer the buffer.
3785 * @param bytes_read number of bytes that were read into the buffer.
3788 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3792 _dbus_assert (loader->buffer_outstanding);
3793 _dbus_assert (buffer == &loader->data);
3795 loader->buffer_outstanding = FALSE;
3799 * Gets the buffer to use for reading unix fds from the network.
3801 * This works similar to _dbus_message_loader_get_buffer()
3803 * @param loader the message loader.
3804 * @param fds the array to read fds into
3805 * @param max_n_fds how many fds to read at most
3806 * @return TRUE on success, FALSE on OOM
3809 _dbus_message_loader_get_unix_fds(DBusMessageLoader *loader,
3811 unsigned *max_n_fds)
3813 #ifdef HAVE_UNIX_FD_PASSING
3814 _dbus_assert (!loader->unix_fds_outstanding);
3816 /* Allocate space where we can put the fds we read. We allocate
3817 space for max_message_unix_fds since this is an
3818 upper limit how many fds can be received within a single
3819 message. Since SCM_RIGHTS doesn't allow a reallocate+retry logic
3820 we are allocating the maximum possible array size right from the
3821 beginning. This sucks a bit, however unless SCM_RIGHTS is fixed
3822 there is no better way. */
3824 if (loader->n_unix_fds_allocated < loader->max_message_unix_fds)
3826 int *a = dbus_realloc(loader->unix_fds,
3827 loader->max_message_unix_fds * sizeof(loader->unix_fds[0]));
3832 loader->unix_fds = a;
3833 loader->n_unix_fds_allocated = loader->max_message_unix_fds;
3836 *fds = loader->unix_fds + loader->n_unix_fds;
3837 *max_n_fds = loader->n_unix_fds_allocated - loader->n_unix_fds;
3839 loader->unix_fds_outstanding = TRUE;
3842 _dbus_assert_not_reached("Platform doesn't support unix fd passing");
3848 * Returns a buffer obtained from _dbus_message_loader_get_unix_fds().
3850 * This works similar to _dbus_message_loader_return_buffer()
3852 * @param loader the message loader.
3853 * @param fds the array fds were read into
3854 * @param max_n_fds how many fds were read
3858 _dbus_message_loader_return_unix_fds(DBusMessageLoader *loader,
3862 #ifdef HAVE_UNIX_FD_PASSING
3863 _dbus_assert(loader->unix_fds_outstanding);
3864 _dbus_assert(loader->unix_fds + loader->n_unix_fds == fds);
3865 _dbus_assert(loader->n_unix_fds + n_fds <= loader->n_unix_fds_allocated);
3867 loader->n_unix_fds += n_fds;
3868 loader->unix_fds_outstanding = FALSE;
3870 _dbus_assert_not_reached("Platform doesn't support unix fd passing");
3875 * FIXME when we move the header out of the buffer, that memmoves all
3876 * buffered messages. Kind of crappy.
3878 * Also we copy the header and body, which is kind of crappy. To
3879 * avoid this, we have to allow header and body to be in a single
3880 * memory block, which is good for messages we read and bad for
3881 * messages we are creating. But we could move_len() the buffer into
3882 * this single memory block, and move_len() will just swap the buffers
3883 * if you're moving the entire buffer replacing the dest string.
3885 * We could also have the message loader tell the transport how many
3886 * bytes to read; so it would first ask for some arbitrary number like
3887 * 256, then if the message was incomplete it would use the
3888 * header/body len to ask for exactly the size of the message (or
3889 * blocks the size of a typical kernel buffer for the socket). That
3890 * way we don't get trailing bytes in the buffer that have to be
3891 * memmoved. Though I suppose we also don't have a chance of reading a
3892 * bunch of small messages at once, so the optimization may be stupid.
3894 * Another approach would be to keep a "start" index into
3895 * loader->data and only delete it occasionally, instead of after
3896 * each message is loaded.
3898 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3901 load_message (DBusMessageLoader *loader,
3902 DBusMessage *message,
3904 int fields_array_len,
3909 DBusValidity validity;
3910 const DBusString *type_str;
3912 DBusValidationMode mode;
3913 dbus_uint32_t n_unix_fds = 0;
3915 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3920 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3923 /* 1. VALIDATE AND COPY OVER HEADER */
3924 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3925 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3927 if (!_dbus_header_load (&message->header,
3935 _dbus_string_get_length (&loader->data)))
3937 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3939 /* assert here so we can catch any code that still uses DBUS_VALID to indicate
3940 oom errors. They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
3941 _dbus_assert (validity != DBUS_VALID);
3943 if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
3947 loader->corrupted = TRUE;
3948 loader->corruption_reason = validity;
3953 _dbus_assert (validity == DBUS_VALID);
3955 message->byte_order = byte_order;
3957 /* 2. VALIDATE BODY */
3958 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3960 get_const_signature (&message->header, &type_str, &type_pos);
3962 /* Because the bytes_remaining arg is NULL, this validates that the
3963 * body is the right length
3965 validity = _dbus_validate_body_with_reason (type_str,
3972 if (validity != DBUS_VALID)
3974 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3976 loader->corrupted = TRUE;
3977 loader->corruption_reason = validity;
3983 /* 3. COPY OVER UNIX FDS */
3984 _dbus_header_get_field_basic(&message->header,
3985 DBUS_HEADER_FIELD_UNIX_FDS,
3989 #ifdef HAVE_UNIX_FD_PASSING
3991 if (n_unix_fds > loader->n_unix_fds)
3993 _dbus_verbose("Message contains references to more unix fds than were sent %u != %u\n",
3994 n_unix_fds, loader->n_unix_fds);
3996 loader->corrupted = TRUE;
3997 loader->corruption_reason = DBUS_INVALID_MISSING_UNIX_FDS;
4001 /* If this was a recycled message there might still be
4002 some memory allocated for the fds */
4003 dbus_free(message->unix_fds);
4007 message->unix_fds = _dbus_memdup(loader->unix_fds, n_unix_fds * sizeof(message->unix_fds[0]));
4008 if (message->unix_fds == NULL)
4010 _dbus_verbose ("Failed to allocate file descriptor array\n");
4015 message->n_unix_fds_allocated = message->n_unix_fds = n_unix_fds;
4016 loader->n_unix_fds -= n_unix_fds;
4017 memmove(loader->unix_fds + n_unix_fds, loader->unix_fds, loader->n_unix_fds);
4020 message->unix_fds = NULL;
4026 _dbus_verbose ("Hmm, message claims to come with file descriptors "
4027 "but that's not supported on our platform, disconnecting.\n");
4029 loader->corrupted = TRUE;
4030 loader->corruption_reason = DBUS_INVALID_MISSING_UNIX_FDS;
4036 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
4038 if (!_dbus_list_append (&loader->messages, message))
4040 _dbus_verbose ("Failed to append new message to loader queue\n");
4045 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
4046 _dbus_assert (_dbus_string_get_length (&loader->data) >=
4047 (header_len + body_len));
4049 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
4051 _dbus_verbose ("Failed to move body into new message\n");
4056 _dbus_string_delete (&loader->data, 0, header_len + body_len);
4058 /* don't waste more than 2k of memory */
4059 _dbus_string_compact (&loader->data, 2048);
4061 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
4062 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
4064 _dbus_verbose ("Loaded message %p\n", message);
4066 _dbus_assert (!oom);
4067 _dbus_assert (!loader->corrupted);
4068 _dbus_assert (loader->messages != NULL);
4069 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
4077 /* does nothing if the message isn't in the list */
4078 _dbus_list_remove_last (&loader->messages, message);
4081 _dbus_assert (!loader->corrupted);
4083 _dbus_assert (loader->corrupted);
4085 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
4091 * Converts buffered data into messages, if we have enough data. If
4092 * we don't have enough data, does nothing.
4094 * @todo we need to check that the proper named header fields exist
4095 * for each message type.
4097 * @todo If a message has unknown type, we should probably eat it
4098 * right here rather than passing it out to applications. However
4099 * it's not an error to see messages of unknown type.
4101 * @param loader the loader.
4102 * @returns #TRUE if we had enough memory to finish.
4105 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
4107 while (!loader->corrupted &&
4108 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
4110 DBusValidity validity;
4111 int byte_order, fields_array_len, header_len, body_len;
4113 if (_dbus_header_have_message_untrusted (loader->max_message_size,
4120 _dbus_string_get_length (&loader->data)))
4122 DBusMessage *message;
4124 _dbus_assert (validity == DBUS_VALID);
4126 message = dbus_message_new_empty_header ();
4127 if (message == NULL)
4130 if (!load_message (loader, message,
4131 byte_order, fields_array_len,
4132 header_len, body_len))
4134 dbus_message_unref (message);
4135 /* load_message() returns false if corrupted or OOM; if
4136 * corrupted then return TRUE for not OOM
4138 return loader->corrupted;
4141 _dbus_assert (loader->messages != NULL);
4142 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
4146 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
4148 if (validity != DBUS_VALID)
4150 loader->corrupted = TRUE;
4151 loader->corruption_reason = validity;
4161 * Peeks at first loaded message, returns #NULL if no messages have
4164 * @param loader the loader.
4165 * @returns the next message, or #NULL if none.
4168 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
4170 if (loader->messages)
4171 return loader->messages->data;
4177 * Pops a loaded message (passing ownership of the message
4178 * to the caller). Returns #NULL if no messages have been
4181 * @param loader the loader.
4182 * @returns the next message, or #NULL if none.
4185 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
4187 return _dbus_list_pop_first (&loader->messages);
4191 * Pops a loaded message inside a list link (passing ownership of the
4192 * message and link to the caller). Returns #NULL if no messages have
4195 * @param loader the loader.
4196 * @returns the next message link, or #NULL if none.
4199 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
4201 return _dbus_list_pop_first_link (&loader->messages);
4205 * Returns a popped message link, used to undo a pop.
4207 * @param loader the loader
4208 * @param link the link with a message in it
4211 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
4214 _dbus_list_prepend_link (&loader->messages, link);
4218 * Checks whether the loader is confused due to bad data.
4219 * If messages are received that are invalid, the
4220 * loader gets confused and gives up permanently.
4221 * This state is called "corrupted."
4223 * @param loader the loader
4224 * @returns #TRUE if the loader is hosed.
4227 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
4229 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
4230 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
4231 return loader->corrupted;
4235 * Checks what kind of bad data confused the loader.
4237 * @param loader the loader
4238 * @returns why the loader is hosed, or DBUS_VALID if it isn't.
4241 _dbus_message_loader_get_corruption_reason (DBusMessageLoader *loader)
4243 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
4244 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
4246 return loader->corruption_reason;
4250 * Sets the maximum size message we allow.
4252 * @param loader the loader
4253 * @param size the max message size in bytes
4256 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
4259 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
4261 _dbus_verbose ("clamping requested max message size %ld to %d\n",
4262 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
4263 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
4265 loader->max_message_size = size;
4269 * Gets the maximum allowed message size in bytes.
4271 * @param loader the loader
4272 * @returns max size in bytes
4275 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
4277 return loader->max_message_size;
4281 * Sets the maximum unix fds per message we allow.
4283 * @param loader the loader
4284 * @param size the max number of unix fds in a message
4287 _dbus_message_loader_set_max_message_unix_fds (DBusMessageLoader *loader,
4290 if (n > DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
4292 _dbus_verbose ("clamping requested max message unix_fds %ld to %d\n",
4293 n, DBUS_MAXIMUM_MESSAGE_UNIX_FDS);
4294 n = DBUS_MAXIMUM_MESSAGE_UNIX_FDS;
4296 loader->max_message_unix_fds = n;
4300 * Gets the maximum allowed number of unix fds per message
4302 * @param loader the loader
4303 * @returns max unix fds
4306 _dbus_message_loader_get_max_message_unix_fds (DBusMessageLoader *loader)
4308 return loader->max_message_unix_fds;
4311 static DBusDataSlotAllocator slot_allocator;
4312 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
4315 * Allocates an integer ID to be used for storing application-specific
4316 * data on any DBusMessage. The allocated ID may then be used
4317 * with dbus_message_set_data() and dbus_message_get_data().
4318 * The passed-in slot must be initialized to -1, and is filled in
4319 * with the slot ID. If the passed-in slot is not -1, it's assumed
4320 * to be already allocated, and its refcount is incremented.
4322 * The allocated slot is global, i.e. all DBusMessage objects will
4323 * have a slot with the given integer ID reserved.
4325 * @param slot_p address of a global variable storing the slot
4326 * @returns #FALSE on failure (no memory)
4329 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
4331 return _dbus_data_slot_allocator_alloc (&slot_allocator,
4332 &_DBUS_LOCK_NAME (message_slots),
4337 * Deallocates a global ID for message data slots.
4338 * dbus_message_get_data() and dbus_message_set_data() may no
4339 * longer be used with this slot. Existing data stored on existing
4340 * DBusMessage objects will be freed when the message is
4341 * finalized, but may not be retrieved (and may only be replaced if
4342 * someone else reallocates the slot). When the refcount on the
4343 * passed-in slot reaches 0, it is set to -1.
4345 * @param slot_p address storing the slot to deallocate
4348 dbus_message_free_data_slot (dbus_int32_t *slot_p)
4350 _dbus_return_if_fail (*slot_p >= 0);
4352 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
4356 * Stores a pointer on a DBusMessage, along
4357 * with an optional function to be used for freeing
4358 * the data when the data is set again, or when
4359 * the message is finalized. The slot number
4360 * must have been allocated with dbus_message_allocate_data_slot().
4362 * @param message the message
4363 * @param slot the slot number
4364 * @param data the data to store
4365 * @param free_data_func finalizer function for the data
4366 * @returns #TRUE if there was enough memory to store the data
4369 dbus_message_set_data (DBusMessage *message,
4372 DBusFreeFunction free_data_func)
4374 DBusFreeFunction old_free_func;
4378 _dbus_return_val_if_fail (message != NULL, FALSE);
4379 _dbus_return_val_if_fail (slot >= 0, FALSE);
4381 retval = _dbus_data_slot_list_set (&slot_allocator,
4382 &message->slot_list,
4383 slot, data, free_data_func,
4384 &old_free_func, &old_data);
4388 /* Do the actual free outside the message lock */
4390 (* old_free_func) (old_data);
4397 * Retrieves data previously set with dbus_message_set_data().
4398 * The slot must still be allocated (must not have been freed).
4400 * @param message the message
4401 * @param slot the slot to get data from
4402 * @returns the data, or #NULL if not found
4405 dbus_message_get_data (DBusMessage *message,
4410 _dbus_return_val_if_fail (message != NULL, NULL);
4412 res = _dbus_data_slot_list_get (&slot_allocator,
4413 &message->slot_list,
4420 * Utility function to convert a machine-readable (not translated)
4421 * string into a D-Bus message type.
4424 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
4425 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
4426 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
4427 * "error" -> DBUS_MESSAGE_TYPE_ERROR
4428 * anything else -> DBUS_MESSAGE_TYPE_INVALID
4433 dbus_message_type_from_string (const char *type_str)
4435 if (strcmp (type_str, "method_call") == 0)
4436 return DBUS_MESSAGE_TYPE_METHOD_CALL;
4437 if (strcmp (type_str, "method_return") == 0)
4438 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
4439 else if (strcmp (type_str, "signal") == 0)
4440 return DBUS_MESSAGE_TYPE_SIGNAL;
4441 else if (strcmp (type_str, "error") == 0)
4442 return DBUS_MESSAGE_TYPE_ERROR;
4444 return DBUS_MESSAGE_TYPE_INVALID;
4448 * Utility function to convert a D-Bus message type into a
4449 * machine-readable string (not translated).
4452 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
4453 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
4454 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
4455 * DBUS_MESSAGE_TYPE_ERROR -> "error"
4456 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
4461 dbus_message_type_to_string (int type)
4465 case DBUS_MESSAGE_TYPE_METHOD_CALL:
4466 return "method_call";
4467 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
4468 return "method_return";
4469 case DBUS_MESSAGE_TYPE_SIGNAL:
4471 case DBUS_MESSAGE_TYPE_ERROR:
4479 * Turn a DBusMessage into the marshalled form as described in the D-Bus
4482 * Generally, this function is only useful for encapsulating D-Bus messages in
4483 * a different protocol.
4485 * @param msg the DBusMessage
4486 * @param marshalled_data_p the location to save the marshalled form to
4487 * @param len_p the location to save the length of the marshalled form to
4488 * @returns #FALSE if there was not enough memory
4491 dbus_message_marshal (DBusMessage *msg,
4492 char **marshalled_data_p,
4496 dbus_bool_t was_locked;
4498 _dbus_return_val_if_fail (msg != NULL, FALSE);
4499 _dbus_return_val_if_fail (marshalled_data_p != NULL, FALSE);
4500 _dbus_return_val_if_fail (len_p != NULL, FALSE);
4502 if (!_dbus_string_init (&tmp))
4505 /* Ensure the message is locked, to ensure the length header is filled in. */
4506 was_locked = msg->locked;
4509 dbus_message_lock (msg);
4511 if (!_dbus_string_copy (&(msg->header.data), 0, &tmp, 0))
4514 *len_p = _dbus_string_get_length (&tmp);
4516 if (!_dbus_string_copy (&(msg->body), 0, &tmp, *len_p))
4519 *len_p = _dbus_string_get_length (&tmp);
4521 if (!_dbus_string_steal_data (&tmp, marshalled_data_p))
4524 _dbus_string_free (&tmp);
4527 msg->locked = FALSE;
4532 _dbus_string_free (&tmp);
4535 msg->locked = FALSE;
4541 * Demarshal a D-Bus message from the format described in the D-Bus
4544 * Generally, this function is only useful for encapsulating D-Bus messages in
4545 * a different protocol.
4547 * @param str the marshalled DBusMessage
4548 * @param len the length of str
4549 * @param error the location to save errors to
4550 * @returns #NULL if there was an error
4553 dbus_message_demarshal (const char *str,
4557 DBusMessageLoader *loader;
4561 _dbus_return_val_if_fail (str != NULL, NULL);
4563 loader = _dbus_message_loader_new ();
4568 _dbus_message_loader_get_buffer (loader, &buffer);
4569 _dbus_string_append_len (buffer, str, len);
4570 _dbus_message_loader_return_buffer (loader, buffer, len);
4572 if (!_dbus_message_loader_queue_messages (loader))
4575 if (_dbus_message_loader_get_is_corrupted (loader))
4578 msg = _dbus_message_loader_pop_message (loader);
4583 _dbus_message_loader_unref (loader);
4587 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Message is corrupted (%s)",
4588 _dbus_validity_to_error_message (loader->corruption_reason));
4589 _dbus_message_loader_unref (loader);
4593 _DBUS_SET_OOM (error);
4594 _dbus_message_loader_unref (loader);
4599 * Returns the number of bytes required to be in the buffer to demarshal a
4602 * Generally, this function is only useful for encapsulating D-Bus messages in
4603 * a different protocol.
4605 * @param str data to be marshalled
4606 * @param len the length of str
4607 * @param error the location to save errors to
4608 * @returns -1 if there was no valid data to be demarshalled, 0 if there wasn't enough data to determine how much should be demarshalled. Otherwise returns the number of bytes to be demarshalled
4612 dbus_message_demarshal_bytes_needed(const char *buf,
4616 int byte_order, fields_array_len, header_len, body_len;
4617 DBusValidity validity = DBUS_VALID;
4620 if (!buf || len < DBUS_MINIMUM_HEADER_SIZE)
4623 if (len > DBUS_MAXIMUM_MESSAGE_LENGTH)
4624 len = DBUS_MAXIMUM_MESSAGE_LENGTH;
4625 _dbus_string_init_const_len (&str, buf, len);
4627 validity = DBUS_VALID;
4629 = _dbus_header_have_message_untrusted(DBUS_MAXIMUM_MESSAGE_LENGTH,
4630 &validity, &byte_order,
4636 _dbus_string_free (&str);
4638 if (validity == DBUS_VALID)
4640 _dbus_assert(have_message);
4641 return header_len + body_len;
4645 return -1; /* broken! */
4651 /* tests in dbus-message-util.c */