1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal-recursive.h"
27 #include "dbus-marshal-validate.h"
28 #include "dbus-marshal-byteswap.h"
29 #include "dbus-marshal-header.h"
30 #include "dbus-signature.h"
31 #include "dbus-message-private.h"
32 #include "dbus-object-tree.h"
33 #include "dbus-memory.h"
34 #include "dbus-list.h"
35 #include "dbus-threads-internal.h"
38 static void dbus_message_finalize (DBusMessage *message);
41 * @defgroup DBusMessageInternals DBusMessage implementation details
42 * @ingroup DBusInternals
43 * @brief DBusMessage private implementation details.
45 * The guts of DBusMessage and its methods.
50 /* Not thread locked, but strictly const/read-only so should be OK
52 /** An static string representing an empty signature */
53 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
55 /* these have wacky values to help trap uninitialized iterators;
56 * but has to fit in 3 bits
59 DBUS_MESSAGE_ITER_TYPE_READER = 3,
60 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
63 /** typedef for internals of message iterator */
64 typedef struct DBusMessageRealIter DBusMessageRealIter;
67 * @brief Internals of DBusMessageIter
69 * Object representing a position in a message. All fields are internal.
71 struct DBusMessageRealIter
73 DBusMessage *message; /**< Message used */
74 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
75 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
76 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
79 DBusTypeWriter writer; /**< writer */
80 DBusTypeReader reader; /**< reader */
81 } u; /**< the type writer or reader that does all the work */
85 get_const_signature (DBusHeader *header,
86 const DBusString **type_str_p,
89 if (_dbus_header_get_field_raw (header,
90 DBUS_HEADER_FIELD_SIGNATURE,
94 *type_pos_p += 1; /* skip the signature length which is 1 byte */
98 *type_str_p = &_dbus_empty_signature_str;
104 * Swaps the message to compiler byte order if required
106 * @param message the message
109 _dbus_message_byteswap (DBusMessage *message)
111 const DBusString *type_str;
114 if (message->byte_order == DBUS_COMPILER_BYTE_ORDER)
117 _dbus_verbose ("Swapping message into compiler byte order\n");
119 get_const_signature (&message->header, &type_str, &type_pos);
121 _dbus_marshal_byteswap (type_str, type_pos,
123 DBUS_COMPILER_BYTE_ORDER,
126 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
128 _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
131 /** byte-swap the message if it doesn't match our byte order.
132 * Called only when we need the message in our own byte order,
133 * normally when reading arrays of integers or doubles.
134 * Otherwise should not be called since it would do needless
137 #define ensure_byte_order(message) \
138 if (message->byte_order != DBUS_COMPILER_BYTE_ORDER) \
139 _dbus_message_byteswap (message)
142 * Gets the data to be sent over the network for this message.
143 * The header and then the body should be written out.
144 * This function is guaranteed to always return the same
145 * data once a message is locked (with _dbus_message_lock()).
147 * @param message the message.
148 * @param header return location for message header data.
149 * @param body return location for message body data.
152 _dbus_message_get_network_data (DBusMessage *message,
153 const DBusString **header,
154 const DBusString **body)
156 _dbus_assert (message->locked);
158 *header = &message->header.data;
159 *body = &message->body;
163 * Sets the serial number of a message.
164 * This can only be done once on a message.
166 * @param message the message
167 * @param serial the serial
170 _dbus_message_set_serial (DBusMessage *message,
171 dbus_uint32_t serial)
173 _dbus_assert (message != NULL);
174 _dbus_assert (!message->locked);
175 _dbus_assert (dbus_message_get_serial (message) == 0);
177 _dbus_header_set_serial (&message->header, serial);
181 * Adds a counter to be incremented immediately with the
182 * size of this message, and decremented by the size
183 * of this message when this message if finalized.
184 * The link contains a counter with its refcount already
185 * incremented, but the counter itself not incremented.
186 * Ownership of link and counter refcount is passed to
189 * @param message the message
190 * @param link link with counter as data
193 _dbus_message_add_size_counter_link (DBusMessage *message,
196 /* right now we don't recompute the delta when message
197 * size changes, and that's OK for current purposes
198 * I think, but could be important to change later.
199 * Do recompute it whenever there are no outstanding counters,
200 * since it's basically free.
202 if (message->size_counters == NULL)
204 message->size_counter_delta =
205 _dbus_string_get_length (&message->header.data) +
206 _dbus_string_get_length (&message->body);
209 _dbus_verbose ("message has size %ld\n",
210 message->size_counter_delta);
214 _dbus_list_append_link (&message->size_counters, link);
216 _dbus_counter_adjust (link->data, message->size_counter_delta);
220 * Adds a counter to be incremented immediately with the
221 * size of this message, and decremented by the size
222 * of this message when this message if finalized.
224 * @param message the message
225 * @param counter the counter
226 * @returns #FALSE if no memory
229 _dbus_message_add_size_counter (DBusMessage *message,
230 DBusCounter *counter)
234 link = _dbus_list_alloc_link (counter);
238 _dbus_counter_ref (counter);
239 _dbus_message_add_size_counter_link (message, link);
245 * Removes a counter tracking the size of this message, and decrements
246 * the counter by the size of this message.
248 * @param message the message
249 * @param link_return return the link used
250 * @param counter the counter
253 _dbus_message_remove_size_counter (DBusMessage *message,
254 DBusCounter *counter,
255 DBusList **link_return)
259 link = _dbus_list_find_last (&message->size_counters,
261 _dbus_assert (link != NULL);
263 _dbus_list_unlink (&message->size_counters,
268 _dbus_list_free_link (link);
270 _dbus_counter_adjust (counter, - message->size_counter_delta);
272 _dbus_counter_unref (counter);
276 * Locks a message. Allows checking that applications don't keep a
277 * reference to a message in the outgoing queue and change it
278 * underneath us. Messages are locked when they enter the outgoing
279 * queue (dbus_connection_send_message()), and the library complains
280 * if the message is modified while locked.
282 * @param message the message to lock.
285 _dbus_message_lock (DBusMessage *message)
287 if (!message->locked)
289 _dbus_header_update_lengths (&message->header,
290 _dbus_string_get_length (&message->body));
292 /* must have a signature if you have a body */
293 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
294 dbus_message_get_signature (message) != NULL);
296 message->locked = TRUE;
301 set_or_delete_string_field (DBusMessage *message,
307 return _dbus_header_delete_field (&message->header, field);
309 return _dbus_header_set_field_basic (&message->header,
316 /* Probably we don't need to use this */
318 * Sets the signature of the message, i.e. the arguments in the
319 * message payload. The signature includes only "in" arguments for
320 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
321 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
322 * what you might expect (it does not include the signature of the
323 * entire C++-style method).
325 * The signature is a string made up of type codes such as
326 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
327 * the value of #DBUS_TYPE_INVALID). The macros such as
328 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
329 * may find it useful to use the string forms, such as
330 * #DBUS_TYPE_INT32_AS_STRING.
332 * An "unset" or #NULL signature is considered the same as an empty
333 * signature. In fact dbus_message_get_signature() will never return
336 * @param message the message
337 * @param signature the type signature or #NULL to unset
338 * @returns #FALSE if no memory
341 _dbus_message_set_signature (DBusMessage *message,
342 const char *signature)
344 _dbus_return_val_if_fail (message != NULL, FALSE);
345 _dbus_return_val_if_fail (!message->locked, FALSE);
346 _dbus_return_val_if_fail (signature == NULL ||
347 _dbus_check_is_valid_signature (signature));
348 /* can't delete the signature if you have a message body */
349 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
352 return set_or_delete_string_field (message,
353 DBUS_HEADER_FIELD_SIGNATURE,
361 * We cache some DBusMessage to reduce the overhead of allocating
362 * them. In my profiling this consistently made about an 8%
363 * difference. It avoids the malloc for the message, the malloc for
364 * the slot list, the malloc for the header string and body string,
365 * and the associated free() calls. It does introduce another global
366 * lock which could be a performance issue in certain cases.
368 * For the echo client/server the round trip time goes from around
369 * .000077 to .000069 with the message cache on my laptop. The sysprof
370 * change is as follows (numbers are cumulative percentage):
372 * with message cache implemented as array as it is now (0.000069 per):
373 * new_empty_header 1.46
374 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
380 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
383 * with message cache implemented as list (0.000070 per roundtrip):
384 * new_empty_header 2.72
385 * list_pop_first 1.88
389 * without cache (0.000077 per roundtrip):
390 * new_empty_header 6.7
391 * string_init_preallocated 3.43
400 * If you implement the message_cache with a list, the primary reason
401 * it's slower is that you add another thread lock (on the DBusList
405 /** Avoid caching huge messages */
406 #define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
408 /** Avoid caching too many messages */
409 #define MAX_MESSAGE_CACHE_SIZE 5
411 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
412 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
413 static int message_cache_count = 0;
414 static dbus_bool_t message_cache_shutdown_registered = FALSE;
417 dbus_message_cache_shutdown (void *data)
421 _DBUS_LOCK (message_cache);
424 while (i < MAX_MESSAGE_CACHE_SIZE)
426 if (message_cache[i])
427 dbus_message_finalize (message_cache[i]);
432 message_cache_count = 0;
433 message_cache_shutdown_registered = FALSE;
435 _DBUS_UNLOCK (message_cache);
439 * Tries to get a message from the message cache. The retrieved
440 * message will have junk in it, so it still needs to be cleared out
441 * in dbus_message_new_empty_header()
443 * @returns the message, or #NULL if none cached
446 dbus_message_get_cached (void)
448 DBusMessage *message;
453 _DBUS_LOCK (message_cache);
455 _dbus_assert (message_cache_count >= 0);
457 if (message_cache_count == 0)
459 _DBUS_UNLOCK (message_cache);
463 /* This is not necessarily true unless count > 0, and
464 * message_cache is uninitialized until the shutdown is
467 _dbus_assert (message_cache_shutdown_registered);
470 while (i < MAX_MESSAGE_CACHE_SIZE)
472 if (message_cache[i])
474 message = message_cache[i];
475 message_cache[i] = NULL;
476 message_cache_count -= 1;
481 _dbus_assert (message_cache_count >= 0);
482 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
483 _dbus_assert (message != NULL);
485 _DBUS_UNLOCK (message_cache);
487 _dbus_assert (message->refcount.value == 0);
488 _dbus_assert (message->size_counters == NULL);
494 free_size_counter (void *element,
497 DBusCounter *counter = element;
498 DBusMessage *message = data;
500 _dbus_counter_adjust (counter, - message->size_counter_delta);
502 _dbus_counter_unref (counter);
506 * Tries to cache a message, otherwise finalize it.
508 * @param message the message
511 dbus_message_cache_or_finalize (DBusMessage *message)
513 dbus_bool_t was_cached;
516 _dbus_assert (message->refcount.value == 0);
518 /* This calls application code and has to be done first thing
519 * without holding the lock
521 _dbus_data_slot_list_clear (&message->slot_list);
523 _dbus_list_foreach (&message->size_counters,
524 free_size_counter, message);
525 _dbus_list_clear (&message->size_counters);
529 _DBUS_LOCK (message_cache);
531 if (!message_cache_shutdown_registered)
533 _dbus_assert (message_cache_count == 0);
535 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
539 while (i < MAX_MESSAGE_CACHE_SIZE)
541 message_cache[i] = NULL;
545 message_cache_shutdown_registered = TRUE;
548 _dbus_assert (message_cache_count >= 0);
550 if ((_dbus_string_get_length (&message->header.data) +
551 _dbus_string_get_length (&message->body)) >
552 MAX_MESSAGE_SIZE_TO_CACHE)
555 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
558 /* Find empty slot */
560 while (message_cache[i] != NULL)
563 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
565 _dbus_assert (message_cache[i] == NULL);
566 message_cache[i] = message;
567 message_cache_count += 1;
569 #ifndef DBUS_DISABLE_CHECKS
570 message->in_cache = TRUE;
574 _DBUS_UNLOCK (message_cache);
576 _dbus_assert (message->refcount.value == 0);
579 dbus_message_finalize (message);
582 #ifndef DBUS_DISABLE_CHECKS
584 _dbus_message_iter_check (DBusMessageRealIter *iter)
588 _dbus_warn_check_failed ("dbus message iterator is NULL\n");
592 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
594 if (iter->u.reader.byte_order != iter->message->byte_order)
596 _dbus_warn_check_failed ("dbus message changed byte order since iterator was created\n");
599 /* because we swap the message into compiler order when you init an iter */
600 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
602 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
604 if (iter->u.writer.byte_order != iter->message->byte_order)
606 _dbus_warn_check_failed ("dbus message changed byte order since append iterator was created\n");
609 /* because we swap the message into compiler order when you init an iter */
610 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
614 _dbus_warn_check_failed ("dbus message iterator looks uninitialized or corrupted\n");
618 if (iter->changed_stamp != iter->message->changed_stamp)
620 _dbus_warn_check_failed ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
626 #endif /* DBUS_DISABLE_CHECKS */
629 * Implementation of the varargs arg-getting functions.
630 * dbus_message_get_args() is the place to go for complete
633 * @see dbus_message_get_args
634 * @param iter the message iter
635 * @param error error to be filled in
636 * @param first_arg_type type of the first argument
637 * @param var_args return location for first argument, followed by list of type/location pairs
638 * @returns #FALSE if error was set
641 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
646 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
647 int spec_type, msg_type, i;
650 _dbus_assert (_dbus_message_iter_check (real));
654 spec_type = first_arg_type;
657 while (spec_type != DBUS_TYPE_INVALID)
659 msg_type = dbus_message_iter_get_arg_type (iter);
661 if (msg_type != spec_type)
663 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
664 "Argument %d is specified to be of type \"%s\", but "
665 "is actually of type \"%s\"\n", i,
666 _dbus_type_to_string (spec_type),
667 _dbus_type_to_string (msg_type));
672 if (dbus_type_is_basic (spec_type))
676 ptr = va_arg (var_args, DBusBasicValue*);
678 _dbus_assert (ptr != NULL);
680 _dbus_type_reader_read_basic (&real->u.reader,
683 else if (spec_type == DBUS_TYPE_ARRAY)
686 int spec_element_type;
687 const DBusBasicValue **ptr;
689 DBusTypeReader array;
691 spec_element_type = va_arg (var_args, int);
692 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
694 if (spec_element_type != element_type)
696 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
697 "Argument %d is specified to be an array of \"%s\", but "
698 "is actually an array of \"%s\"\n",
700 _dbus_type_to_string (spec_element_type),
701 _dbus_type_to_string (element_type));
706 if (dbus_type_is_fixed (spec_element_type))
708 ptr = va_arg (var_args, const DBusBasicValue**);
709 n_elements_p = va_arg (var_args, int*);
711 _dbus_assert (ptr != NULL);
712 _dbus_assert (n_elements_p != NULL);
714 _dbus_type_reader_recurse (&real->u.reader, &array);
716 _dbus_type_reader_read_fixed_multi (&array,
719 else if (spec_element_type == DBUS_TYPE_STRING ||
720 spec_element_type == DBUS_TYPE_SIGNATURE ||
721 spec_element_type == DBUS_TYPE_OBJECT_PATH)
727 str_array_p = va_arg (var_args, char***);
728 n_elements_p = va_arg (var_args, int*);
730 _dbus_assert (str_array_p != NULL);
731 _dbus_assert (n_elements_p != NULL);
733 /* Count elements in the array */
734 _dbus_type_reader_recurse (&real->u.reader, &array);
737 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
740 _dbus_type_reader_next (&array);
743 str_array = dbus_new0 (char*, n_elements + 1);
744 if (str_array == NULL)
746 _DBUS_SET_OOM (error);
750 /* Now go through and dup each string */
751 _dbus_type_reader_recurse (&real->u.reader, &array);
754 while (i < n_elements)
757 _dbus_type_reader_read_basic (&array,
760 str_array[i] = _dbus_strdup (s);
761 if (str_array[i] == NULL)
763 dbus_free_string_array (str_array);
764 _DBUS_SET_OOM (error);
770 if (!_dbus_type_reader_next (&array))
771 _dbus_assert (i == n_elements);
774 _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
775 _dbus_assert (i == n_elements);
776 _dbus_assert (str_array[i] == NULL);
778 *str_array_p = str_array;
779 *n_elements_p = n_elements;
781 #ifndef DBUS_DISABLE_CHECKS
784 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
785 _DBUS_FUNCTION_NAME);
790 #ifndef DBUS_DISABLE_CHECKS
793 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
794 _DBUS_FUNCTION_NAME);
799 spec_type = va_arg (var_args, int);
800 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
802 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
803 "Message has only %d arguments, but more were expected", i);
820 * @defgroup DBusMessage DBusMessage
822 * @brief Message to be sent or received over a #DBusConnection.
824 * A DBusMessage is the most basic unit of communication over a
825 * DBusConnection. A DBusConnection represents a stream of messages
826 * received from a remote application, and a stream of messages
827 * sent to a remote application.
829 * A message has a message type, returned from
830 * dbus_message_get_type(). This indicates whether the message is a
831 * method call, a reply to a method call, a signal, or an error reply.
833 * A message has header fields such as the sender, destination, method
834 * or signal name, and so forth. DBusMessage has accessor functions for
835 * these, such as dbus_message_get_member().
837 * Convenience functions dbus_message_is_method_call(), dbus_message_is_signal(),
838 * and dbus_message_is_error() check several header fields at once and are
839 * slightly more efficient than checking the header fields with individual
840 * accessor functions.
842 * Finally, a message has arguments. The number and types of arguments
843 * are in the message's signature header field (accessed with
844 * dbus_message_get_signature()). Simple argument values are usually
845 * retrieved with dbus_message_get_args() but more complex values such
846 * as structs may require the use of #DBusMessageIter.
848 * The D-Bus specification goes into some more detail about header fields and
855 * @typedef DBusMessage
857 * Opaque data type representing a message received from or to be
858 * sent to another application.
862 * Returns the serial of a message or 0 if none has been specified.
863 * The message's serial number is provided by the application sending
864 * the message and is used to identify replies to this message.
866 * All messages received on a connection will have a serial provided
867 * by the remote application.
869 * For messages you're sending, dbus_connection_send() will assign a
870 * serial and return it to you.
872 * @param message the message
873 * @returns the serial
876 dbus_message_get_serial (DBusMessage *message)
878 _dbus_return_val_if_fail (message != NULL, 0);
880 return _dbus_header_get_serial (&message->header);
884 * Sets the reply serial of a message (the serial of the message this
887 * @param message the message
888 * @param reply_serial the serial we're replying to
889 * @returns #FALSE if not enough memory
892 dbus_message_set_reply_serial (DBusMessage *message,
893 dbus_uint32_t reply_serial)
895 _dbus_return_val_if_fail (message != NULL, FALSE);
896 _dbus_return_val_if_fail (!message->locked, FALSE);
897 _dbus_return_val_if_fail (reply_serial != 0, FALSE); /* 0 is invalid */
899 return _dbus_header_set_field_basic (&message->header,
900 DBUS_HEADER_FIELD_REPLY_SERIAL,
906 * Returns the serial that the message is a reply to or 0 if none.
908 * @param message the message
909 * @returns the reply serial
912 dbus_message_get_reply_serial (DBusMessage *message)
914 dbus_uint32_t v_UINT32;
916 _dbus_return_val_if_fail (message != NULL, 0);
918 if (_dbus_header_get_field_basic (&message->header,
919 DBUS_HEADER_FIELD_REPLY_SERIAL,
928 dbus_message_finalize (DBusMessage *message)
930 _dbus_assert (message->refcount.value == 0);
932 /* This calls application callbacks! */
933 _dbus_data_slot_list_free (&message->slot_list);
935 _dbus_list_foreach (&message->size_counters,
936 free_size_counter, message);
937 _dbus_list_clear (&message->size_counters);
939 _dbus_header_free (&message->header);
940 _dbus_string_free (&message->body);
942 _dbus_assert (message->refcount.value == 0);
948 dbus_message_new_empty_header (void)
950 DBusMessage *message;
951 dbus_bool_t from_cache;
953 message = dbus_message_get_cached ();
962 message = dbus_new (DBusMessage, 1);
965 #ifndef DBUS_DISABLE_CHECKS
966 message->generation = _dbus_current_generation;
970 message->refcount.value = 1;
971 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
972 message->locked = FALSE;
973 #ifndef DBUS_DISABLE_CHECKS
974 message->in_cache = FALSE;
976 message->size_counters = NULL;
977 message->size_counter_delta = 0;
978 message->changed_stamp = 0;
981 _dbus_data_slot_list_init (&message->slot_list);
985 _dbus_header_reinit (&message->header, message->byte_order);
986 _dbus_string_set_length (&message->body, 0);
990 if (!_dbus_header_init (&message->header, message->byte_order))
996 if (!_dbus_string_init_preallocated (&message->body, 32))
998 _dbus_header_free (&message->header);
1008 * Constructs a new message of the given message type.
1009 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1010 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1012 * Usually you want to use dbus_message_new_method_call(),
1013 * dbus_message_new_method_return(), dbus_message_new_signal(),
1014 * or dbus_message_new_error() instead.
1016 * @param message_type type of message
1017 * @returns new message or #NULL if no memory
1020 dbus_message_new (int message_type)
1022 DBusMessage *message;
1024 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1026 message = dbus_message_new_empty_header ();
1027 if (message == NULL)
1030 if (!_dbus_header_create (&message->header,
1032 NULL, NULL, NULL, NULL, NULL))
1034 dbus_message_unref (message);
1042 * Constructs a new message to invoke a method on a remote
1043 * object. Returns #NULL if memory can't be allocated for the
1044 * message. The destination may be #NULL in which case no destination
1045 * is set; this is appropriate when using D-Bus in a peer-to-peer
1046 * context (no message bus). The interface may be #NULL, which means
1047 * that if multiple methods with the given name exist it is undefined
1048 * which one will be invoked.
1050 * The path and method names may not be #NULL.
1052 * Destination, path, interface, and method name can't contain
1053 * any invalid characters (see the D-Bus specification).
1055 * @param destination name that the message should be sent to or #NULL
1056 * @param path object path the message should be sent to
1057 * @param interface interface to invoke method on, or #NULL
1058 * @param method method to invoke
1060 * @returns a new DBusMessage, free with dbus_message_unref()
1063 dbus_message_new_method_call (const char *destination,
1065 const char *interface,
1068 DBusMessage *message;
1070 _dbus_return_val_if_fail (path != NULL, NULL);
1071 _dbus_return_val_if_fail (method != NULL, NULL);
1072 _dbus_return_val_if_fail (destination == NULL ||
1073 _dbus_check_is_valid_bus_name (destination), NULL);
1074 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1075 _dbus_return_val_if_fail (interface == NULL ||
1076 _dbus_check_is_valid_interface (interface), NULL);
1077 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
1079 message = dbus_message_new_empty_header ();
1080 if (message == NULL)
1083 if (!_dbus_header_create (&message->header,
1084 DBUS_MESSAGE_TYPE_METHOD_CALL,
1085 destination, path, interface, method, NULL))
1087 dbus_message_unref (message);
1095 * Constructs a message that is a reply to a method call. Returns
1096 * #NULL if memory can't be allocated for the message.
1098 * @param method_call the message being replied to
1099 * @returns a new DBusMessage, free with dbus_message_unref()
1102 dbus_message_new_method_return (DBusMessage *method_call)
1104 DBusMessage *message;
1107 _dbus_return_val_if_fail (method_call != NULL, NULL);
1109 sender = dbus_message_get_sender (method_call);
1111 /* sender is allowed to be null here in peer-to-peer case */
1113 message = dbus_message_new_empty_header ();
1114 if (message == NULL)
1117 if (!_dbus_header_create (&message->header,
1118 DBUS_MESSAGE_TYPE_METHOD_RETURN,
1119 sender, NULL, NULL, NULL, NULL))
1121 dbus_message_unref (message);
1125 dbus_message_set_no_reply (message, TRUE);
1127 if (!dbus_message_set_reply_serial (message,
1128 dbus_message_get_serial (method_call)))
1130 dbus_message_unref (message);
1138 * Constructs a new message representing a signal emission. Returns
1139 * #NULL if memory can't be allocated for the message. A signal is
1140 * identified by its originating object path, interface, and the name
1143 * Path, interface, and signal name must all be valid (the D-Bus
1144 * specification defines the syntax of these fields).
1146 * @param path the path to the object emitting the signal
1147 * @param interface the interface the signal is emitted from
1148 * @param name name of the signal
1149 * @returns a new DBusMessage, free with dbus_message_unref()
1152 dbus_message_new_signal (const char *path,
1153 const char *interface,
1156 DBusMessage *message;
1158 _dbus_return_val_if_fail (path != NULL, NULL);
1159 _dbus_return_val_if_fail (interface != NULL, NULL);
1160 _dbus_return_val_if_fail (name != NULL, NULL);
1161 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1162 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
1163 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
1165 message = dbus_message_new_empty_header ();
1166 if (message == NULL)
1169 if (!_dbus_header_create (&message->header,
1170 DBUS_MESSAGE_TYPE_SIGNAL,
1171 NULL, path, interface, name, NULL))
1173 dbus_message_unref (message);
1177 dbus_message_set_no_reply (message, TRUE);
1183 * Creates a new message that is an error reply to another message.
1184 * Error replies are most common in response to method calls, but
1185 * can be returned in reply to any message.
1187 * The error name must be a valid error name according to the syntax
1188 * given in the D-Bus specification. If you don't want to make
1189 * up an error name just use #DBUS_ERROR_FAILED.
1191 * @param reply_to the message we're replying to
1192 * @param error_name the error name
1193 * @param error_message the error message string (or #NULL for none, but please give a message)
1194 * @returns a new error message object, free with dbus_message_unref()
1197 dbus_message_new_error (DBusMessage *reply_to,
1198 const char *error_name,
1199 const char *error_message)
1201 DBusMessage *message;
1203 DBusMessageIter iter;
1205 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1206 _dbus_return_val_if_fail (error_name != NULL, NULL);
1207 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1209 sender = dbus_message_get_sender (reply_to);
1211 /* sender may be NULL for non-message-bus case or
1212 * when the message bus is dealing with an unregistered
1215 message = dbus_message_new_empty_header ();
1216 if (message == NULL)
1219 if (!_dbus_header_create (&message->header,
1220 DBUS_MESSAGE_TYPE_ERROR,
1221 sender, NULL, NULL, NULL, error_name))
1223 dbus_message_unref (message);
1227 dbus_message_set_no_reply (message, TRUE);
1229 if (!dbus_message_set_reply_serial (message,
1230 dbus_message_get_serial (reply_to)))
1232 dbus_message_unref (message);
1236 if (error_message != NULL)
1238 dbus_message_iter_init_append (message, &iter);
1239 if (!dbus_message_iter_append_basic (&iter,
1243 dbus_message_unref (message);
1252 * Creates a new message that is an error reply to another message, allowing
1253 * you to use printf formatting.
1255 * See dbus_message_new_error() for details - this function is the same
1256 * aside from the printf formatting.
1258 * @todo add _DBUS_GNUC_PRINTF to this (requires moving _DBUS_GNUC_PRINTF to
1259 * public header, see DBUS_GNUC_DEPRECATED for an example)
1261 * @param reply_to the original message
1262 * @param error_name the error name
1263 * @param error_format the error message format as with printf
1264 * @param ... format string arguments
1265 * @returns a new error message
1268 dbus_message_new_error_printf (DBusMessage *reply_to,
1269 const char *error_name,
1270 const char *error_format,
1275 DBusMessage *message;
1277 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1278 _dbus_return_val_if_fail (error_name != NULL, NULL);
1279 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1281 if (!_dbus_string_init (&str))
1284 va_start (args, error_format);
1286 if (_dbus_string_append_printf_valist (&str, error_format, args))
1287 message = dbus_message_new_error (reply_to, error_name,
1288 _dbus_string_get_const_data (&str));
1292 _dbus_string_free (&str);
1301 * Creates a new message that is an exact replica of the message
1302 * specified, except that its refcount is set to 1, its message serial
1303 * is reset to 0, and if the original message was "locked" (in the
1304 * outgoing message queue and thus not modifiable) the new message
1305 * will not be locked.
1307 * @param message the message
1308 * @returns the new message.or #NULL if not enough memory
1311 dbus_message_copy (const DBusMessage *message)
1313 DBusMessage *retval;
1315 _dbus_return_val_if_fail (message != NULL, NULL);
1317 retval = dbus_new0 (DBusMessage, 1);
1321 retval->refcount.value = 1;
1322 retval->byte_order = message->byte_order;
1323 retval->locked = FALSE;
1324 #ifndef DBUS_DISABLE_CHECKS
1325 retval->generation = message->generation;
1328 if (!_dbus_header_copy (&message->header, &retval->header))
1334 if (!_dbus_string_init_preallocated (&retval->body,
1335 _dbus_string_get_length (&message->body)))
1337 _dbus_header_free (&retval->header);
1342 if (!_dbus_string_copy (&message->body, 0,
1349 _dbus_header_free (&retval->header);
1350 _dbus_string_free (&retval->body);
1358 * Increments the reference count of a DBusMessage.
1360 * @param message the message
1361 * @returns the message
1362 * @see dbus_message_unref
1365 dbus_message_ref (DBusMessage *message)
1367 dbus_int32_t old_refcount;
1369 _dbus_return_val_if_fail (message != NULL, NULL);
1370 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1371 _dbus_return_val_if_fail (!message->in_cache, NULL);
1373 old_refcount = _dbus_atomic_inc (&message->refcount);
1374 _dbus_assert (old_refcount >= 1);
1380 * Decrements the reference count of a DBusMessage, freeing the
1381 * message if the count reaches 0.
1383 * @param message the message
1384 * @see dbus_message_ref
1387 dbus_message_unref (DBusMessage *message)
1389 dbus_int32_t old_refcount;
1391 _dbus_return_if_fail (message != NULL);
1392 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1393 _dbus_return_if_fail (!message->in_cache);
1395 old_refcount = _dbus_atomic_dec (&message->refcount);
1397 _dbus_assert (old_refcount >= 0);
1399 if (old_refcount == 1)
1401 /* Calls application callbacks! */
1402 dbus_message_cache_or_finalize (message);
1407 * Gets the type of a message. Types include
1408 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1409 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1410 * types are allowed and all code must silently ignore messages of
1411 * unknown type. #DBUS_MESSAGE_TYPE_INVALID will never be returned.
1413 * @param message the message
1414 * @returns the type of the message
1417 dbus_message_get_type (DBusMessage *message)
1419 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1421 return _dbus_header_get_message_type (&message->header);
1425 * Appends fields to a message given a variable argument list. The
1426 * variable argument list should contain the type of each argument
1427 * followed by the value to append. Appendable types are basic types,
1428 * and arrays of fixed-length basic types. To append variable-length
1429 * basic types, or any more complex value, you have to use an iterator
1430 * rather than this function.
1432 * To append a basic type, specify its type code followed by the
1433 * address of the value. For example:
1437 * dbus_int32_t v_INT32 = 42;
1438 * const char *v_STRING = "Hello World";
1439 * dbus_message_append_args (message,
1440 * DBUS_TYPE_INT32, &v_INT32,
1441 * DBUS_TYPE_STRING, &v_STRING,
1442 * DBUS_TYPE_INVALID);
1445 * To append an array of fixed-length basic types, pass in the
1446 * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1447 * the array pointer, and a 32-bit integer giving the number of
1448 * elements in the array. So for example:
1450 * const dbus_int32_t array[] = { 1, 2, 3 };
1451 * const dbus_int32_t *v_ARRAY = array;
1452 * dbus_message_append_args (message,
1453 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3,
1454 * DBUS_TYPE_INVALID);
1457 * @warning in C, given "int array[]", "&array == array" (the
1458 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1459 * So if you're using an array instead of a pointer you have to create
1460 * a pointer variable, assign the array to it, then take the address
1461 * of the pointer variable. For strings it works to write
1462 * const char *array = "Hello" and then use &array though.
1464 * The last argument to this function must be #DBUS_TYPE_INVALID,
1465 * marking the end of the argument list. If you don't do this
1466 * then libdbus won't know to stop and will read invalid memory.
1468 * String/signature/path arrays should be passed in as "const char***
1469 * address_of_array" and "int n_elements"
1471 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1473 * @todo If this fails due to lack of memory, the message is hosed and
1474 * you have to start over building the whole message.
1476 * @param message the message
1477 * @param first_arg_type type of the first argument
1478 * @param ... value of first argument, list of additional type-value pairs
1479 * @returns #TRUE on success
1482 dbus_message_append_args (DBusMessage *message,
1489 _dbus_return_val_if_fail (message != NULL, FALSE);
1491 va_start (var_args, first_arg_type);
1492 retval = dbus_message_append_args_valist (message,
1501 * Like dbus_message_append_args() but takes a va_list for use by language bindings.
1503 * @todo for now, if this function fails due to OOM it will leave
1504 * the message half-written and you have to discard the message
1507 * @see dbus_message_append_args.
1508 * @param message the message
1509 * @param first_arg_type type of first argument
1510 * @param var_args value of first argument, then list of type/value pairs
1511 * @returns #TRUE on success
1514 dbus_message_append_args_valist (DBusMessage *message,
1519 DBusMessageIter iter;
1521 _dbus_return_val_if_fail (message != NULL, FALSE);
1523 type = first_arg_type;
1525 dbus_message_iter_init_append (message, &iter);
1527 while (type != DBUS_TYPE_INVALID)
1529 if (dbus_type_is_basic (type))
1531 const DBusBasicValue *value;
1532 value = va_arg (var_args, const DBusBasicValue*);
1534 if (!dbus_message_iter_append_basic (&iter,
1539 else if (type == DBUS_TYPE_ARRAY)
1542 DBusMessageIter array;
1545 element_type = va_arg (var_args, int);
1547 buf[0] = element_type;
1549 if (!dbus_message_iter_open_container (&iter,
1555 if (dbus_type_is_fixed (element_type))
1557 const DBusBasicValue **value;
1560 value = va_arg (var_args, const DBusBasicValue**);
1561 n_elements = va_arg (var_args, int);
1563 if (!dbus_message_iter_append_fixed_array (&array,
1569 else if (element_type == DBUS_TYPE_STRING ||
1570 element_type == DBUS_TYPE_SIGNATURE ||
1571 element_type == DBUS_TYPE_OBJECT_PATH)
1573 const char ***value_p;
1578 value_p = va_arg (var_args, const char***);
1579 n_elements = va_arg (var_args, int);
1584 while (i < n_elements)
1586 if (!dbus_message_iter_append_basic (&array,
1595 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1596 _dbus_type_to_string (element_type),
1597 _DBUS_FUNCTION_NAME);
1601 if (!dbus_message_iter_close_container (&iter, &array))
1604 #ifndef DBUS_DISABLE_CHECKS
1607 _dbus_warn ("type %s isn't supported yet in %s\n",
1608 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1613 type = va_arg (var_args, int);
1623 * Gets arguments from a message given a variable argument list. The
1624 * supported types include those supported by
1625 * dbus_message_append_args(); that is, basic types and arrays of
1626 * fixed-length basic types. The arguments are the same as they would
1627 * be for dbus_message_iter_get_basic() or
1628 * dbus_message_iter_get_fixed_array().
1630 * In addition to those types, arrays of string, object path, and
1631 * signature are supported; but these are returned as allocated memory
1632 * and must be freed with dbus_free_string_array(), while the other
1633 * types are returned as const references. To get a string array
1634 * pass in "char ***array_location" and "int *n_elements"
1636 * The variable argument list should contain the type of the argument
1637 * followed by a pointer to where the value should be stored. The list
1638 * is terminated with #DBUS_TYPE_INVALID.
1640 * Except for string arrays, the returned values are constant; do not
1641 * free them. They point into the #DBusMessage.
1643 * If the requested arguments are not present, or do not have the
1644 * requested types, then an error will be set.
1646 * If more arguments than requested are present, the requested
1647 * arguments are returned and the extra arguments are ignored.
1649 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1651 * @param message the message
1652 * @param error error to be filled in on failure
1653 * @param first_arg_type the first argument type
1654 * @param ... location for first argument value, then list of type-location pairs
1655 * @returns #FALSE if the error was set
1658 dbus_message_get_args (DBusMessage *message,
1666 _dbus_return_val_if_fail (message != NULL, FALSE);
1667 _dbus_return_val_if_error_is_set (error, FALSE);
1669 va_start (var_args, first_arg_type);
1670 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1677 * Like dbus_message_get_args but takes a va_list for use by language bindings.
1679 * @see dbus_message_get_args
1680 * @param message the message
1681 * @param error error to be filled in
1682 * @param first_arg_type type of the first argument
1683 * @param var_args return location for first argument, followed by list of type/location pairs
1684 * @returns #FALSE if error was set
1687 dbus_message_get_args_valist (DBusMessage *message,
1692 DBusMessageIter iter;
1694 _dbus_return_val_if_fail (message != NULL, FALSE);
1695 _dbus_return_val_if_error_is_set (error, FALSE);
1697 dbus_message_iter_init (message, &iter);
1698 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1702 _dbus_message_iter_init_common (DBusMessage *message,
1703 DBusMessageRealIter *real,
1706 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1708 /* Since the iterator will read or write who-knows-what from the
1709 * message, we need to get in the right byte order
1711 ensure_byte_order (message);
1713 real->message = message;
1714 real->changed_stamp = message->changed_stamp;
1715 real->iter_type = iter_type;
1716 real->sig_refcount = 0;
1720 * Initializes a #DBusMessageIter for reading the arguments of the
1721 * message passed in.
1723 * When possible, dbus_message_get_args() is much more convenient.
1724 * Some types of argument can only be read with #DBusMessageIter
1727 * The easiest way to iterate is like this:
1729 * dbus_message_iter_init (&iter);
1730 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1731 * dbus_message_iter_next (&iter);
1734 * #DBusMessageIter contains no allocated memory; it need not be
1735 * freed, and can be copied by assignment or memcpy().
1737 * @param message the message
1738 * @param iter pointer to an iterator to initialize
1739 * @returns #FALSE if the message has no arguments
1742 dbus_message_iter_init (DBusMessage *message,
1743 DBusMessageIter *iter)
1745 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1746 const DBusString *type_str;
1749 _dbus_return_val_if_fail (message != NULL, FALSE);
1750 _dbus_return_val_if_fail (iter != NULL, FALSE);
1752 get_const_signature (&message->header, &type_str, &type_pos);
1754 _dbus_message_iter_init_common (message, real,
1755 DBUS_MESSAGE_ITER_TYPE_READER);
1757 _dbus_type_reader_init (&real->u.reader,
1758 message->byte_order,
1763 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1767 * Checks if an iterator has any more fields.
1769 * @param iter the message iter
1770 * @returns #TRUE if there are more fields following
1773 dbus_message_iter_has_next (DBusMessageIter *iter)
1775 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1777 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1778 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1780 return _dbus_type_reader_has_next (&real->u.reader);
1784 * Moves the iterator to the next field, if any. If there's no next
1785 * field, returns #FALSE. If the iterator moves forward, returns
1788 * @param iter the message iter
1789 * @returns #TRUE if the iterator was moved to the next field
1792 dbus_message_iter_next (DBusMessageIter *iter)
1794 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1796 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1797 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1799 return _dbus_type_reader_next (&real->u.reader);
1803 * Returns the argument type of the argument that the message iterator
1804 * points to. If the iterator is at the end of the message, returns
1805 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1808 * dbus_message_iter_init (&iter);
1809 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1810 * dbus_message_iter_next (&iter);
1813 * @param iter the message iter
1814 * @returns the argument type
1817 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1819 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1821 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1822 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1824 return _dbus_type_reader_get_current_type (&real->u.reader);
1828 * Returns the element type of the array that the message iterator
1829 * points to. Note that you need to check that the iterator points to
1830 * an array prior to using this function.
1832 * @param iter the message iter
1833 * @returns the array element type
1836 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1838 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1840 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1841 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1842 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1844 return _dbus_type_reader_get_element_type (&real->u.reader);
1848 * Recurses into a container value when reading values from a message,
1849 * initializing a sub-iterator to use for traversing the child values
1852 * Note that this recurses into a value, not a type, so you can only
1853 * recurse if the value exists. The main implication of this is that
1854 * if you have for example an empty array of array of int32, you can
1855 * recurse into the outermost array, but it will have no values, so
1856 * you won't be able to recurse further. There's no array of int32 to
1859 * If a container is an array of fixed-length types, it is much more
1860 * efficient to use dbus_message_iter_get_fixed_array() to get the
1861 * whole array in one shot, rather than individually walking over the
1864 * Be sure you have somehow checked that
1865 * dbus_message_iter_get_arg_type() matches the type you are expecting
1866 * to recurse into. Results of this function are undefined if there is
1867 * no container to recurse into at the current iterator position.
1869 * @param iter the message iterator
1870 * @param sub the sub-iterator to initialize
1873 dbus_message_iter_recurse (DBusMessageIter *iter,
1874 DBusMessageIter *sub)
1876 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1877 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1879 _dbus_return_if_fail (_dbus_message_iter_check (real));
1880 _dbus_return_if_fail (sub != NULL);
1883 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1887 * Returns the current signature of a message iterator. This
1888 * is useful primarily for dealing with variants; one can
1889 * recurse into a variant and determine the signature of
1890 * the variant's value.
1892 * The returned string must be freed with dbus_free().
1894 * @param iter the message iterator
1895 * @returns the contained signature, or NULL if out of memory
1898 dbus_message_iter_get_signature (DBusMessageIter *iter)
1900 const DBusString *sig;
1904 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1906 _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
1908 if (!_dbus_string_init (&retstr))
1911 _dbus_type_reader_get_signature (&real->u.reader, &sig,
1913 if (!_dbus_string_append_len (&retstr,
1914 _dbus_string_get_const_data (sig) + start,
1917 if (!_dbus_string_steal_data (&retstr, &ret))
1919 _dbus_string_free (&retstr);
1924 * Reads a basic-typed value from the message iterator.
1925 * Basic types are the non-containers such as integer and string.
1927 * The value argument should be the address of a location to store
1928 * the returned value. So for int32 it should be a "dbus_int32_t*"
1929 * and for string a "const char**". The returned value is
1930 * by reference and should not be freed.
1932 * Be sure you have somehow checked that
1933 * dbus_message_iter_get_arg_type() matches the type you are
1934 * expecting, or you'll crash when you try to use an integer as a
1935 * string or something.
1937 * To read any container type (array, struct, dict) you will need
1938 * to recurse into the container with dbus_message_iter_recurse().
1939 * If the container is an array of fixed-length values, you can
1940 * get all the array elements at once with
1941 * dbus_message_iter_get_fixed_array(). Otherwise, you have to
1942 * iterate over the container's contents one value at a time.
1944 * All basic-typed values are guaranteed to fit in 8 bytes. So you can
1945 * write code like this:
1948 * dbus_uint64_t value;
1950 * dbus_message_iter_get_basic (&read_iter, &value);
1951 * type = dbus_message_iter_get_arg_type (&read_iter);
1952 * dbus_message_iter_append_basic (&write_iter, type, &value);
1955 * On some really obscure platforms dbus_uint64_t might not exist, if
1956 * you need to worry about this you will know. dbus_uint64_t is just
1957 * one example of a type that's large enough to hold any possible
1958 * value, you could use a struct or char[8] instead if you like.
1960 * @param iter the iterator
1961 * @param value location to store the value
1964 dbus_message_iter_get_basic (DBusMessageIter *iter,
1967 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1969 _dbus_return_if_fail (_dbus_message_iter_check (real));
1970 _dbus_return_if_fail (value != NULL);
1972 _dbus_type_reader_read_basic (&real->u.reader,
1977 * Returns the number of bytes in the array as marshaled in the wire
1978 * protocol. The iterator must currently be inside an array-typed
1981 * This function is deprecated on the grounds that it is stupid. Why
1982 * would you want to know how many bytes are in the array as marshaled
1983 * in the wire protocol? For now, use the n_elements returned from
1984 * dbus_message_iter_get_fixed_array() instead, or iterate over the
1985 * array values and count them.
1987 * @todo introduce a variant of this get_n_elements that returns
1988 * the number of elements, though with a non-fixed array it will not
1989 * be very efficient, so maybe it's not good.
1991 * @param iter the iterator
1992 * @returns the number of bytes in the array
1995 dbus_message_iter_get_array_len (DBusMessageIter *iter)
1997 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1999 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
2001 return _dbus_type_reader_get_array_length (&real->u.reader);
2005 * Reads a block of fixed-length values from the message iterator.
2006 * Fixed-length values are those basic types that are not string-like,
2007 * such as integers, bool, double. The returned block will be from the
2008 * current position in the array until the end of the array.
2010 * The message iter should be "in" the array (that is, you recurse into the
2011 * array, and then you call dbus_message_iter_get_fixed_array() on the
2012 * "sub-iterator" created by dbus_message_iter_recurse()).
2014 * The value argument should be the address of a location to store the
2015 * returned array. So for int32 it should be a "const dbus_int32_t**"
2016 * The returned value is by reference and should not be freed.
2018 * This function should only be used if dbus_type_is_fixed() returns
2019 * #TRUE for the element type.
2021 * If an array's elements are not fixed in size, you have to recurse
2022 * into the array with dbus_message_iter_recurse() and read the
2023 * elements one by one.
2025 * Because the array is not copied, this function runs in constant
2026 * time and is fast; it's much preferred over walking the entire array
2027 * with an iterator. (However, you can always use
2028 * dbus_message_iter_recurse(), even for fixed-length types;
2029 * dbus_message_iter_get_fixed_array() is just an optimization.)
2031 * @param iter the iterator
2032 * @param value location to store the block
2033 * @param n_elements number of elements in the block
2036 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
2040 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2041 int subtype = _dbus_type_reader_get_current_type(&real->u.reader);
2043 _dbus_return_if_fail (_dbus_message_iter_check (real));
2044 _dbus_return_if_fail (value != NULL);
2045 _dbus_return_if_fail ((subtype == DBUS_TYPE_INVALID) ||
2046 dbus_type_is_fixed (subtype));
2048 _dbus_type_reader_read_fixed_multi (&real->u.reader,
2053 * Initializes a #DBusMessageIter for appending arguments to the end
2056 * @todo If appending any of the arguments fails due to lack of
2057 * memory, the message is hosed and you have to start over building
2058 * the whole message.
2060 * @param message the message
2061 * @param iter pointer to an iterator to initialize
2064 dbus_message_iter_init_append (DBusMessage *message,
2065 DBusMessageIter *iter)
2067 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2069 _dbus_return_if_fail (message != NULL);
2070 _dbus_return_if_fail (iter != NULL);
2072 _dbus_message_iter_init_common (message, real,
2073 DBUS_MESSAGE_ITER_TYPE_WRITER);
2075 /* We create the signature string and point iterators at it "on demand"
2076 * when a value is actually appended. That means that init() never fails
2079 _dbus_type_writer_init_types_delayed (&real->u.writer,
2080 message->byte_order,
2082 _dbus_string_get_length (&message->body));
2086 * Creates a temporary signature string containing the current
2087 * signature, stores it in the iterator, and points the iterator to
2088 * the end of it. Used any time we write to the message.
2090 * @param real an iterator without a type_str
2091 * @returns #FALSE if no memory
2094 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
2097 const DBusString *current_sig;
2098 int current_sig_pos;
2100 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2102 if (real->u.writer.type_str != NULL)
2104 _dbus_assert (real->sig_refcount > 0);
2105 real->sig_refcount += 1;
2109 str = dbus_new (DBusString, 1);
2113 if (!_dbus_header_get_field_raw (&real->message->header,
2114 DBUS_HEADER_FIELD_SIGNATURE,
2115 ¤t_sig, ¤t_sig_pos))
2122 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2123 current_sig_pos += 1; /* move on to sig data */
2125 if (!_dbus_string_init_preallocated (str, current_len + 4))
2131 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2134 _dbus_string_free (str);
2141 if (!_dbus_string_init_preallocated (str, 4))
2148 real->sig_refcount = 1;
2150 _dbus_type_writer_add_types (&real->u.writer,
2151 str, _dbus_string_get_length (str));
2156 * Sets the new signature as the message signature, frees the
2157 * signature string, and marks the iterator as not having a type_str
2158 * anymore. Frees the signature even if it fails, so you can't
2159 * really recover from failure. Kinda busted.
2161 * @param real an iterator without a type_str
2162 * @returns #FALSE if no memory
2165 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2168 const char *v_STRING;
2171 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2172 _dbus_assert (real->u.writer.type_str != NULL);
2173 _dbus_assert (real->sig_refcount > 0);
2175 real->sig_refcount -= 1;
2177 if (real->sig_refcount > 0)
2179 _dbus_assert (real->sig_refcount == 0);
2183 str = real->u.writer.type_str;
2185 v_STRING = _dbus_string_get_const_data (str);
2186 if (!_dbus_header_set_field_basic (&real->message->header,
2187 DBUS_HEADER_FIELD_SIGNATURE,
2188 DBUS_TYPE_SIGNATURE,
2192 _dbus_type_writer_remove_types (&real->u.writer);
2193 _dbus_string_free (str);
2199 #ifndef DBUS_DISABLE_CHECKS
2201 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2203 if (!_dbus_message_iter_check (iter))
2206 if (iter->message->locked)
2208 _dbus_warn_check_failed ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2214 #endif /* DBUS_DISABLE_CHECKS */
2217 * Appends a basic-typed value to the message. The basic types are the
2218 * non-container types such as integer and string.
2220 * The "value" argument should be the address of a basic-typed value.
2221 * So for string, const char**. For integer, dbus_int32_t*.
2223 * @todo If this fails due to lack of memory, the message is hosed and
2224 * you have to start over building the whole message.
2226 * @param iter the append iterator
2227 * @param type the type of the value
2228 * @param value the address of the value
2229 * @returns #FALSE if not enough memory
2232 dbus_message_iter_append_basic (DBusMessageIter *iter,
2236 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2239 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2240 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2241 _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2242 _dbus_return_val_if_fail (value != NULL, FALSE);
2244 if (!_dbus_message_iter_open_signature (real))
2247 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2249 if (!_dbus_message_iter_close_signature (real))
2256 * Appends a block of fixed-length values to an array. The
2257 * fixed-length types are all basic types that are not string-like. So
2258 * int32, double, bool, etc. You must call
2259 * dbus_message_iter_open_container() to open an array of values
2260 * before calling this function. You may call this function multiple
2261 * times (and intermixed with calls to
2262 * dbus_message_iter_append_basic()) for the same array.
2264 * The "value" argument should be the address of the array. So for
2265 * integer, "dbus_int32_t**" is expected for example.
2267 * @warning in C, given "int array[]", "&array == array" (the
2268 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2269 * So if you're using an array instead of a pointer you have to create
2270 * a pointer variable, assign the array to it, then take the address
2271 * of the pointer variable.
2273 * const dbus_int32_t array[] = { 1, 2, 3 };
2274 * const dbus_int32_t *v_ARRAY = array;
2275 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2276 * fprintf (stderr, "No memory!\n");
2278 * For strings it works to write const char *array = "Hello" and then
2279 * use &array though.
2281 * @todo If this fails due to lack of memory, the message is hosed and
2282 * you have to start over building the whole message.
2284 * @param iter the append iterator
2285 * @param element_type the type of the array elements
2286 * @param value the address of the array
2287 * @param n_elements the number of elements to append
2288 * @returns #FALSE if not enough memory
2291 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2296 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2299 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2300 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2301 _dbus_return_val_if_fail (dbus_type_is_fixed (element_type), FALSE);
2302 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2303 _dbus_return_val_if_fail (value != NULL, FALSE);
2304 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2305 _dbus_return_val_if_fail (n_elements <=
2306 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2309 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2315 * Appends a container-typed value to the message; you are required to
2316 * append the contents of the container using the returned
2317 * sub-iterator, and then call
2318 * dbus_message_iter_close_container(). Container types are for
2319 * example struct, variant, and array. For variants, the
2320 * contained_signature should be the type of the single value inside
2321 * the variant. For structs and dict entries, contained_signature
2322 * should be #NULL; it will be set to whatever types you write into
2323 * the struct. For arrays, contained_signature should be the type of
2324 * the array elements.
2326 * @todo If this fails due to lack of memory, the message is hosed and
2327 * you have to start over building the whole message.
2329 * @param iter the append iterator
2330 * @param type the type of the value
2331 * @param contained_signature the type of container contents
2332 * @param sub sub-iterator to initialize
2333 * @returns #FALSE if not enough memory
2336 dbus_message_iter_open_container (DBusMessageIter *iter,
2338 const char *contained_signature,
2339 DBusMessageIter *sub)
2341 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2342 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2343 DBusString contained_str;
2345 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2346 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2347 _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
2348 _dbus_return_val_if_fail (sub != NULL, FALSE);
2349 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2350 contained_signature == NULL) ||
2351 (type == DBUS_TYPE_DICT_ENTRY &&
2352 contained_signature == NULL) ||
2353 (type == DBUS_TYPE_VARIANT &&
2354 contained_signature != NULL) ||
2355 (type == DBUS_TYPE_ARRAY &&
2356 contained_signature != NULL), FALSE);
2358 /* this would fail if the contained_signature is a dict entry, since
2359 * dict entries are invalid signatures standalone (they must be in
2362 _dbus_return_val_if_fail ((type == DBUS_TYPE_ARRAY && contained_signature && *contained_signature == DBUS_DICT_ENTRY_BEGIN_CHAR) ||
2363 (contained_signature == NULL ||
2364 _dbus_check_is_valid_signature (contained_signature)),
2367 if (!_dbus_message_iter_open_signature (real))
2372 if (contained_signature != NULL)
2374 _dbus_string_init_const (&contained_str, contained_signature);
2376 return _dbus_type_writer_recurse (&real->u.writer,
2379 &real_sub->u.writer);
2383 return _dbus_type_writer_recurse (&real->u.writer,
2386 &real_sub->u.writer);
2392 * Closes a container-typed value appended to the message; may write
2393 * out more information to the message known only after the entire
2394 * container is written, and may free resources created by
2395 * dbus_message_iter_open_container().
2397 * @todo If this fails due to lack of memory, the message is hosed and
2398 * you have to start over building the whole message.
2400 * @param iter the append iterator
2401 * @param sub sub-iterator to close
2402 * @returns #FALSE if not enough memory
2405 dbus_message_iter_close_container (DBusMessageIter *iter,
2406 DBusMessageIter *sub)
2408 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2409 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2412 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2413 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2414 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2415 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2417 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2418 &real_sub->u.writer);
2420 if (!_dbus_message_iter_close_signature (real))
2427 * Sets a flag indicating that the message does not want a reply; if
2428 * this flag is set, the other end of the connection may (but is not
2429 * required to) optimize by not sending method return or error
2430 * replies. If this flag is set, there is no way to know whether the
2431 * message successfully arrived at the remote end. Normally you know a
2432 * message was received when you receive the reply to it.
2434 * The flag is #FALSE by default, that is by default the other end is
2435 * required to reply.
2437 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_REPLY_EXPECTED
2439 * @param message the message
2440 * @param no_reply #TRUE if no reply is desired
2443 dbus_message_set_no_reply (DBusMessage *message,
2444 dbus_bool_t no_reply)
2446 _dbus_return_if_fail (message != NULL);
2447 _dbus_return_if_fail (!message->locked);
2449 _dbus_header_toggle_flag (&message->header,
2450 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2455 * Returns #TRUE if the message does not expect
2458 * @param message the message
2459 * @returns #TRUE if the message sender isn't waiting for a reply
2462 dbus_message_get_no_reply (DBusMessage *message)
2464 _dbus_return_val_if_fail (message != NULL, FALSE);
2466 return _dbus_header_get_flag (&message->header,
2467 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2471 * Sets a flag indicating that an owner for the destination name will
2472 * be automatically started before the message is delivered. When this
2473 * flag is set, the message is held until a name owner finishes
2474 * starting up, or fails to start up. In case of failure, the reply
2477 * The flag is set to #TRUE by default, i.e. auto starting is the default.
2479 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_AUTO_START
2481 * @param message the message
2482 * @param auto_start #TRUE if auto-starting is desired
2485 dbus_message_set_auto_start (DBusMessage *message,
2486 dbus_bool_t auto_start)
2488 _dbus_return_if_fail (message != NULL);
2489 _dbus_return_if_fail (!message->locked);
2491 _dbus_header_toggle_flag (&message->header,
2492 DBUS_HEADER_FLAG_NO_AUTO_START,
2497 * Returns #TRUE if the message will cause an owner for
2498 * destination name to be auto-started.
2500 * @param message the message
2501 * @returns #TRUE if the message will use auto-start
2504 dbus_message_get_auto_start (DBusMessage *message)
2506 _dbus_return_val_if_fail (message != NULL, FALSE);
2508 return !_dbus_header_get_flag (&message->header,
2509 DBUS_HEADER_FLAG_NO_AUTO_START);
2514 * Sets the object path this message is being sent to (for
2515 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2516 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2518 * The path must contain only valid characters as defined
2519 * in the D-Bus specification.
2521 * @param message the message
2522 * @param object_path the path or #NULL to unset
2523 * @returns #FALSE if not enough memory
2526 dbus_message_set_path (DBusMessage *message,
2527 const char *object_path)
2529 _dbus_return_val_if_fail (message != NULL, FALSE);
2530 _dbus_return_val_if_fail (!message->locked, FALSE);
2531 _dbus_return_val_if_fail (object_path == NULL ||
2532 _dbus_check_is_valid_path (object_path),
2535 return set_or_delete_string_field (message,
2536 DBUS_HEADER_FIELD_PATH,
2537 DBUS_TYPE_OBJECT_PATH,
2542 * Gets the object path this message is being sent to (for
2543 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2544 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2546 * See also dbus_message_get_path_decomposed().
2548 * The returned string becomes invalid if the message is
2549 * modified, since it points into the wire-marshaled message data.
2551 * @param message the message
2552 * @returns the path (should not be freed) or #NULL
2555 dbus_message_get_path (DBusMessage *message)
2559 _dbus_return_val_if_fail (message != NULL, NULL);
2561 v = NULL; /* in case field doesn't exist */
2562 _dbus_header_get_field_basic (&message->header,
2563 DBUS_HEADER_FIELD_PATH,
2564 DBUS_TYPE_OBJECT_PATH,
2570 * Checks if the message has a particular object path. The object
2571 * path is the destination object for a method call or the emitting
2572 * object for a signal.
2574 * @param message the message
2575 * @param path the path name
2576 * @returns #TRUE if there is a path field in the header
2579 dbus_message_has_path (DBusMessage *message,
2582 const char *msg_path;
2583 msg_path = dbus_message_get_path (message);
2585 if (msg_path == NULL)
2596 if (strcmp (msg_path, path) == 0)
2603 * Gets the object path this message is being sent to
2604 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2605 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2606 * format (one array element per path component).
2607 * Free the returned array with dbus_free_string_array().
2609 * An empty but non-NULL path array means the path "/".
2610 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2611 * and the path "/" becomes { NULL }.
2613 * See also dbus_message_get_path().
2615 * @todo this could be optimized by using the len from the message
2616 * instead of calling strlen() again
2618 * @param message the message
2619 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2620 * @returns #FALSE if no memory to allocate the array
2623 dbus_message_get_path_decomposed (DBusMessage *message,
2628 _dbus_return_val_if_fail (message != NULL, FALSE);
2629 _dbus_return_val_if_fail (path != NULL, FALSE);
2633 v = dbus_message_get_path (message);
2636 if (!_dbus_decompose_path (v, strlen (v),
2644 * Sets the interface this message is being sent to
2645 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2646 * the interface a signal is being emitted from
2647 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2649 * The interface name must contain only valid characters as defined
2650 * in the D-Bus specification.
2652 * @param message the message
2653 * @param interface the interface or #NULL to unset
2654 * @returns #FALSE if not enough memory
2657 dbus_message_set_interface (DBusMessage *message,
2658 const char *interface)
2660 _dbus_return_val_if_fail (message != NULL, FALSE);
2661 _dbus_return_val_if_fail (!message->locked, FALSE);
2662 _dbus_return_val_if_fail (interface == NULL ||
2663 _dbus_check_is_valid_interface (interface),
2666 return set_or_delete_string_field (message,
2667 DBUS_HEADER_FIELD_INTERFACE,
2673 * Gets the interface this message is being sent to
2674 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2675 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2676 * The interface name is fully-qualified (namespaced).
2677 * Returns #NULL if none.
2679 * The returned string becomes invalid if the message is
2680 * modified, since it points into the wire-marshaled message data.
2682 * @param message the message
2683 * @returns the message interface (should not be freed) or #NULL
2686 dbus_message_get_interface (DBusMessage *message)
2690 _dbus_return_val_if_fail (message != NULL, NULL);
2692 v = NULL; /* in case field doesn't exist */
2693 _dbus_header_get_field_basic (&message->header,
2694 DBUS_HEADER_FIELD_INTERFACE,
2701 * Checks if the message has an interface
2703 * @param message the message
2704 * @param interface the interface name
2705 * @returns #TRUE if the interface field in the header matches
2708 dbus_message_has_interface (DBusMessage *message,
2709 const char *interface)
2711 const char *msg_interface;
2712 msg_interface = dbus_message_get_interface (message);
2714 if (msg_interface == NULL)
2716 if (interface == NULL)
2722 if (interface == NULL)
2725 if (strcmp (msg_interface, interface) == 0)
2733 * Sets the interface member being invoked
2734 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2735 * (DBUS_MESSAGE_TYPE_SIGNAL).
2737 * The member name must contain only valid characters as defined
2738 * in the D-Bus specification.
2740 * @param message the message
2741 * @param member the member or #NULL to unset
2742 * @returns #FALSE if not enough memory
2745 dbus_message_set_member (DBusMessage *message,
2748 _dbus_return_val_if_fail (message != NULL, FALSE);
2749 _dbus_return_val_if_fail (!message->locked, FALSE);
2750 _dbus_return_val_if_fail (member == NULL ||
2751 _dbus_check_is_valid_member (member),
2754 return set_or_delete_string_field (message,
2755 DBUS_HEADER_FIELD_MEMBER,
2761 * Gets the interface member being invoked
2762 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2763 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2765 * The returned string becomes invalid if the message is
2766 * modified, since it points into the wire-marshaled message data.
2768 * @param message the message
2769 * @returns the member name (should not be freed) or #NULL
2772 dbus_message_get_member (DBusMessage *message)
2776 _dbus_return_val_if_fail (message != NULL, NULL);
2778 v = NULL; /* in case field doesn't exist */
2779 _dbus_header_get_field_basic (&message->header,
2780 DBUS_HEADER_FIELD_MEMBER,
2787 * Checks if the message has an interface member
2789 * @param message the message
2790 * @param member the member name
2791 * @returns #TRUE if there is a member field in the header
2794 dbus_message_has_member (DBusMessage *message,
2797 const char *msg_member;
2798 msg_member = dbus_message_get_member (message);
2800 if (msg_member == NULL)
2811 if (strcmp (msg_member, member) == 0)
2819 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2820 * The name is fully-qualified (namespaced).
2822 * The error name must contain only valid characters as defined
2823 * in the D-Bus specification.
2825 * @param message the message
2826 * @param error_name the name or #NULL to unset
2827 * @returns #FALSE if not enough memory
2830 dbus_message_set_error_name (DBusMessage *message,
2831 const char *error_name)
2833 _dbus_return_val_if_fail (message != NULL, FALSE);
2834 _dbus_return_val_if_fail (!message->locked, FALSE);
2835 _dbus_return_val_if_fail (error_name == NULL ||
2836 _dbus_check_is_valid_error_name (error_name),
2839 return set_or_delete_string_field (message,
2840 DBUS_HEADER_FIELD_ERROR_NAME,
2846 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2849 * The returned string becomes invalid if the message is
2850 * modified, since it points into the wire-marshaled message data.
2852 * @param message the message
2853 * @returns the error name (should not be freed) or #NULL
2856 dbus_message_get_error_name (DBusMessage *message)
2860 _dbus_return_val_if_fail (message != NULL, NULL);
2862 v = NULL; /* in case field doesn't exist */
2863 _dbus_header_get_field_basic (&message->header,
2864 DBUS_HEADER_FIELD_ERROR_NAME,
2871 * Sets the message's destination. The destination is the name of
2872 * another connection on the bus and may be either the unique name
2873 * assigned by the bus to each connection, or a well-known name
2874 * specified in advance.
2876 * The destination name must contain only valid characters as defined
2877 * in the D-Bus specification.
2879 * @param message the message
2880 * @param destination the destination name or #NULL to unset
2881 * @returns #FALSE if not enough memory
2884 dbus_message_set_destination (DBusMessage *message,
2885 const char *destination)
2887 _dbus_return_val_if_fail (message != NULL, FALSE);
2888 _dbus_return_val_if_fail (!message->locked, FALSE);
2889 _dbus_return_val_if_fail (destination == NULL ||
2890 _dbus_check_is_valid_bus_name (destination),
2893 return set_or_delete_string_field (message,
2894 DBUS_HEADER_FIELD_DESTINATION,
2900 * Gets the destination of a message or #NULL if there is none set.
2902 * The returned string becomes invalid if the message is
2903 * modified, since it points into the wire-marshaled message data.
2905 * @param message the message
2906 * @returns the message destination (should not be freed) or #NULL
2909 dbus_message_get_destination (DBusMessage *message)
2913 _dbus_return_val_if_fail (message != NULL, NULL);
2915 v = NULL; /* in case field doesn't exist */
2916 _dbus_header_get_field_basic (&message->header,
2917 DBUS_HEADER_FIELD_DESTINATION,
2924 * Sets the message sender.
2926 * The sender must be a valid bus name as defined in the D-Bus
2929 * Usually you don't want to call this. The message bus daemon will
2930 * call it to set the origin of each message. If you aren't implementing
2931 * a message bus daemon you shouldn't need to set the sender.
2933 * @param message the message
2934 * @param sender the sender or #NULL to unset
2935 * @returns #FALSE if not enough memory
2938 dbus_message_set_sender (DBusMessage *message,
2941 _dbus_return_val_if_fail (message != NULL, FALSE);
2942 _dbus_return_val_if_fail (!message->locked, FALSE);
2943 _dbus_return_val_if_fail (sender == NULL ||
2944 _dbus_check_is_valid_bus_name (sender),
2947 return set_or_delete_string_field (message,
2948 DBUS_HEADER_FIELD_SENDER,
2954 * Gets the unique name of the connection which originated this
2955 * message, or #NULL if unknown or inapplicable. The sender is filled
2956 * in by the message bus.
2958 * Note, the returned sender is always the unique bus name.
2959 * Connections may own multiple other bus names, but those
2960 * are not found in the sender field.
2962 * The returned string becomes invalid if the message is
2963 * modified, since it points into the wire-marshaled message data.
2965 * @param message the message
2966 * @returns the unique name of the sender or #NULL
2969 dbus_message_get_sender (DBusMessage *message)
2973 _dbus_return_val_if_fail (message != NULL, NULL);
2975 v = NULL; /* in case field doesn't exist */
2976 _dbus_header_get_field_basic (&message->header,
2977 DBUS_HEADER_FIELD_SENDER,
2984 * Gets the type signature of the message, i.e. the arguments in the
2985 * message payload. The signature includes only "in" arguments for
2986 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2987 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2988 * what you might expect (that is, it does not include the signature of the
2989 * entire C++-style method).
2991 * The signature is a string made up of type codes such as
2992 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2993 * the value of #DBUS_TYPE_INVALID).
2995 * The returned string becomes invalid if the message is
2996 * modified, since it points into the wire-marshaled message data.
2998 * @param message the message
2999 * @returns the type signature
3002 dbus_message_get_signature (DBusMessage *message)
3004 const DBusString *type_str;
3007 _dbus_return_val_if_fail (message != NULL, NULL);
3009 get_const_signature (&message->header, &type_str, &type_pos);
3011 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
3015 _dbus_message_has_type_interface_member (DBusMessage *message,
3017 const char *interface,
3022 _dbus_assert (message != NULL);
3023 _dbus_assert (interface != NULL);
3024 _dbus_assert (member != NULL);
3026 if (dbus_message_get_type (message) != type)
3029 /* Optimize by checking the short member name first
3030 * instead of the longer interface name
3033 n = dbus_message_get_member (message);
3035 if (n && strcmp (n, member) == 0)
3037 n = dbus_message_get_interface (message);
3039 if (n == NULL || strcmp (n, interface) == 0)
3047 * Checks whether the message is a method call with the given
3048 * interface and member fields. If the message is not
3049 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
3050 * member field, returns #FALSE. If the interface field is missing,
3051 * then it will be assumed equal to the provided interface. The D-Bus
3052 * protocol allows method callers to leave out the interface name.
3054 * @param message the message
3055 * @param interface the name to check (must not be #NULL)
3056 * @param method the name to check (must not be #NULL)
3058 * @returns #TRUE if the message is the specified method call
3061 dbus_message_is_method_call (DBusMessage *message,
3062 const char *interface,
3065 _dbus_return_val_if_fail (message != NULL, FALSE);
3066 _dbus_return_val_if_fail (interface != NULL, FALSE);
3067 _dbus_return_val_if_fail (method != NULL, FALSE);
3068 /* don't check that interface/method are valid since it would be
3069 * expensive, and not catch many common errors
3072 return _dbus_message_has_type_interface_member (message,
3073 DBUS_MESSAGE_TYPE_METHOD_CALL,
3078 * Checks whether the message is a signal with the given interface and
3079 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
3080 * has a different interface or member field, returns #FALSE.
3082 * @param message the message
3083 * @param interface the name to check (must not be #NULL)
3084 * @param signal_name the name to check (must not be #NULL)
3086 * @returns #TRUE if the message is the specified signal
3089 dbus_message_is_signal (DBusMessage *message,
3090 const char *interface,
3091 const char *signal_name)
3093 _dbus_return_val_if_fail (message != NULL, FALSE);
3094 _dbus_return_val_if_fail (interface != NULL, FALSE);
3095 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
3096 /* don't check that interface/name are valid since it would be
3097 * expensive, and not catch many common errors
3100 return _dbus_message_has_type_interface_member (message,
3101 DBUS_MESSAGE_TYPE_SIGNAL,
3102 interface, signal_name);
3106 * Checks whether the message is an error reply with the given error
3107 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
3108 * different name, returns #FALSE.
3110 * @param message the message
3111 * @param error_name the name to check (must not be #NULL)
3113 * @returns #TRUE if the message is the specified error
3116 dbus_message_is_error (DBusMessage *message,
3117 const char *error_name)
3121 _dbus_return_val_if_fail (message != NULL, FALSE);
3122 _dbus_return_val_if_fail (error_name != NULL, FALSE);
3123 /* don't check that error_name is valid since it would be expensive,
3124 * and not catch many common errors
3127 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3130 n = dbus_message_get_error_name (message);
3132 if (n && strcmp (n, error_name) == 0)
3139 * Checks whether the message was sent to the given name. If the
3140 * message has no destination specified or has a different
3141 * destination, returns #FALSE.
3143 * @param message the message
3144 * @param name the name to check (must not be #NULL)
3146 * @returns #TRUE if the message has the given destination name
3149 dbus_message_has_destination (DBusMessage *message,
3154 _dbus_return_val_if_fail (message != NULL, FALSE);
3155 _dbus_return_val_if_fail (name != NULL, FALSE);
3156 /* don't check that name is valid since it would be expensive, and
3157 * not catch many common errors
3160 s = dbus_message_get_destination (message);
3162 if (s && strcmp (s, name) == 0)
3169 * Checks whether the message has the given unique name as its sender.
3170 * If the message has no sender specified or has a different sender,
3171 * returns #FALSE. Note that a peer application will always have the
3172 * unique name of the connection as the sender. So you can't use this
3173 * function to see whether a sender owned a well-known name.
3175 * Messages from the bus itself will have #DBUS_SERVICE_DBUS
3178 * @param message the message
3179 * @param name the name to check (must not be #NULL)
3181 * @returns #TRUE if the message has the given sender
3184 dbus_message_has_sender (DBusMessage *message,
3189 _dbus_return_val_if_fail (message != NULL, FALSE);
3190 _dbus_return_val_if_fail (name != NULL, FALSE);
3191 /* don't check that name is valid since it would be expensive, and
3192 * not catch many common errors
3195 s = dbus_message_get_sender (message);
3197 if (s && strcmp (s, name) == 0)
3204 * Checks whether the message has the given signature; see
3205 * dbus_message_get_signature() for more details on what the signature
3208 * @param message the message
3209 * @param signature typecode array
3210 * @returns #TRUE if message has the given signature
3213 dbus_message_has_signature (DBusMessage *message,
3214 const char *signature)
3218 _dbus_return_val_if_fail (message != NULL, FALSE);
3219 _dbus_return_val_if_fail (signature != NULL, FALSE);
3220 /* don't check that signature is valid since it would be expensive,
3221 * and not catch many common errors
3224 s = dbus_message_get_signature (message);
3226 if (s && strcmp (s, signature) == 0)
3233 * Sets a #DBusError based on the contents of the given
3234 * message. The error is only set if the message
3235 * is an error message, as in #DBUS_MESSAGE_TYPE_ERROR.
3236 * The name of the error is set to the name of the message,
3237 * and the error message is set to the first argument
3238 * if the argument exists and is a string.
3240 * The return value indicates whether the error was set (the error is
3241 * set if and only if the message is an error message). So you can
3242 * check for an error reply and convert it to DBusError in one go:
3244 * if (dbus_set_error_from_message (error, reply))
3250 * @param error the error to set
3251 * @param message the message to set it from
3252 * @returns #TRUE if the message had type #DBUS_MESSAGE_TYPE_ERROR
3255 dbus_set_error_from_message (DBusError *error,
3256 DBusMessage *message)
3260 _dbus_return_val_if_fail (message != NULL, FALSE);
3261 _dbus_return_val_if_error_is_set (error, FALSE);
3263 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3267 dbus_message_get_args (message, NULL,
3268 DBUS_TYPE_STRING, &str,
3271 dbus_set_error (error, dbus_message_get_error_name (message),
3272 str ? "%s" : NULL, str);
3280 * @addtogroup DBusMessageInternals
3286 * The initial buffer size of the message loader.
3288 * @todo this should be based on min header size plus some average
3289 * body size, or something. Or rather, the min header size only, if we
3290 * want to try to read only the header, store that in a DBusMessage,
3291 * then read only the body and store that, etc., depends on
3292 * how we optimize _dbus_message_loader_get_buffer() and what
3293 * the exact message format is.
3295 #define INITIAL_LOADER_DATA_LEN 32
3298 * Creates a new message loader. Returns #NULL if memory can't
3301 * @returns new loader, or #NULL.
3304 _dbus_message_loader_new (void)
3306 DBusMessageLoader *loader;
3308 loader = dbus_new0 (DBusMessageLoader, 1);
3312 loader->refcount = 1;
3314 loader->corrupted = FALSE;
3315 loader->corruption_reason = DBUS_VALID;
3317 /* this can be configured by the app, but defaults to the protocol max */
3318 loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3320 if (!_dbus_string_init (&loader->data))
3326 /* preallocate the buffer for speed, ignore failure */
3327 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3328 _dbus_string_set_length (&loader->data, 0);
3334 * Increments the reference count of the loader.
3336 * @param loader the loader.
3337 * @returns the loader
3340 _dbus_message_loader_ref (DBusMessageLoader *loader)
3342 loader->refcount += 1;
3348 * Decrements the reference count of the loader and finalizes the
3349 * loader when the count reaches zero.
3351 * @param loader the loader.
3354 _dbus_message_loader_unref (DBusMessageLoader *loader)
3356 loader->refcount -= 1;
3357 if (loader->refcount == 0)
3359 _dbus_list_foreach (&loader->messages,
3360 (DBusForeachFunction) dbus_message_unref,
3362 _dbus_list_clear (&loader->messages);
3363 _dbus_string_free (&loader->data);
3369 * Gets the buffer to use for reading data from the network. Network
3370 * data is read directly into an allocated buffer, which is then used
3371 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3372 * The buffer must always be returned immediately using
3373 * _dbus_message_loader_return_buffer(), even if no bytes are
3374 * successfully read.
3376 * @todo this function can be a lot more clever. For example
3377 * it can probably always return a buffer size to read exactly
3378 * the body of the next message, thus avoiding any memory wastage
3381 * @todo we need to enforce a max length on strings in header fields.
3383 * @param loader the message loader.
3384 * @param buffer the buffer
3387 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3388 DBusString **buffer)
3390 _dbus_assert (!loader->buffer_outstanding);
3392 *buffer = &loader->data;
3394 loader->buffer_outstanding = TRUE;
3398 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3399 * indicating to the loader how many bytes of the buffer were filled
3400 * in. This function must always be called, even if no bytes were
3401 * successfully read.
3403 * @param loader the loader.
3404 * @param buffer the buffer.
3405 * @param bytes_read number of bytes that were read into the buffer.
3408 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3412 _dbus_assert (loader->buffer_outstanding);
3413 _dbus_assert (buffer == &loader->data);
3415 loader->buffer_outstanding = FALSE;
3419 * FIXME when we move the header out of the buffer, that memmoves all
3420 * buffered messages. Kind of crappy.
3422 * Also we copy the header and body, which is kind of crappy. To
3423 * avoid this, we have to allow header and body to be in a single
3424 * memory block, which is good for messages we read and bad for
3425 * messages we are creating. But we could move_len() the buffer into
3426 * this single memory block, and move_len() will just swap the buffers
3427 * if you're moving the entire buffer replacing the dest string.
3429 * We could also have the message loader tell the transport how many
3430 * bytes to read; so it would first ask for some arbitrary number like
3431 * 256, then if the message was incomplete it would use the
3432 * header/body len to ask for exactly the size of the message (or
3433 * blocks the size of a typical kernel buffer for the socket). That
3434 * way we don't get trailing bytes in the buffer that have to be
3435 * memmoved. Though I suppose we also don't have a chance of reading a
3436 * bunch of small messages at once, so the optimization may be stupid.
3438 * Another approach would be to keep a "start" index into
3439 * loader->data and only delete it occasionally, instead of after
3440 * each message is loaded.
3442 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3445 load_message (DBusMessageLoader *loader,
3446 DBusMessage *message,
3448 int fields_array_len,
3453 DBusValidity validity;
3454 const DBusString *type_str;
3456 DBusValidationMode mode;
3458 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3463 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3466 /* 1. VALIDATE AND COPY OVER HEADER */
3467 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3468 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3470 if (!_dbus_header_load (&message->header,
3478 _dbus_string_get_length (&loader->data)))
3480 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3482 /* assert here so we can catch any code that still uses DBUS_VALID to indicate
3483 oom errors. They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
3484 _dbus_assert (validity != DBUS_VALID);
3486 if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
3490 loader->corrupted = TRUE;
3491 loader->corruption_reason = validity;
3496 _dbus_assert (validity == DBUS_VALID);
3498 message->byte_order = byte_order;
3500 /* 2. VALIDATE BODY */
3501 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3503 get_const_signature (&message->header, &type_str, &type_pos);
3505 /* Because the bytes_remaining arg is NULL, this validates that the
3506 * body is the right length
3508 validity = _dbus_validate_body_with_reason (type_str,
3515 if (validity != DBUS_VALID)
3517 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3519 loader->corrupted = TRUE;
3520 loader->corruption_reason = validity;
3526 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3528 if (!_dbus_list_append (&loader->messages, message))
3530 _dbus_verbose ("Failed to append new message to loader queue\n");
3535 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3536 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3537 (header_len + body_len));
3539 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3541 _dbus_verbose ("Failed to move body into new message\n");
3546 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3548 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3549 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3551 _dbus_verbose ("Loaded message %p\n", message);
3553 _dbus_assert (!oom);
3554 _dbus_assert (!loader->corrupted);
3555 _dbus_assert (loader->messages != NULL);
3556 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3564 /* does nothing if the message isn't in the list */
3565 _dbus_list_remove_last (&loader->messages, message);
3568 _dbus_assert (!loader->corrupted);
3570 _dbus_assert (loader->corrupted);
3572 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3578 * Converts buffered data into messages, if we have enough data. If
3579 * we don't have enough data, does nothing.
3581 * @todo we need to check that the proper named header fields exist
3582 * for each message type.
3584 * @todo If a message has unknown type, we should probably eat it
3585 * right here rather than passing it out to applications. However
3586 * it's not an error to see messages of unknown type.
3588 * @param loader the loader.
3589 * @returns #TRUE if we had enough memory to finish.
3592 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3594 while (!loader->corrupted &&
3595 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3597 DBusValidity validity;
3598 int byte_order, fields_array_len, header_len, body_len;
3600 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3607 _dbus_string_get_length (&loader->data)))
3609 DBusMessage *message;
3611 _dbus_assert (validity == DBUS_VALID);
3613 message = dbus_message_new_empty_header ();
3614 if (message == NULL)
3617 if (!load_message (loader, message,
3618 byte_order, fields_array_len,
3619 header_len, body_len))
3621 dbus_message_unref (message);
3622 /* load_message() returns false if corrupted or OOM; if
3623 * corrupted then return TRUE for not OOM
3625 return loader->corrupted;
3628 _dbus_assert (loader->messages != NULL);
3629 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3633 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3635 if (validity != DBUS_VALID)
3637 loader->corrupted = TRUE;
3638 loader->corruption_reason = validity;
3648 * Peeks at first loaded message, returns #NULL if no messages have
3651 * @param loader the loader.
3652 * @returns the next message, or #NULL if none.
3655 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3657 if (loader->messages)
3658 return loader->messages->data;
3664 * Pops a loaded message (passing ownership of the message
3665 * to the caller). Returns #NULL if no messages have been
3668 * @param loader the loader.
3669 * @returns the next message, or #NULL if none.
3672 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3674 return _dbus_list_pop_first (&loader->messages);
3678 * Pops a loaded message inside a list link (passing ownership of the
3679 * message and link to the caller). Returns #NULL if no messages have
3682 * @param loader the loader.
3683 * @returns the next message link, or #NULL if none.
3686 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3688 return _dbus_list_pop_first_link (&loader->messages);
3692 * Returns a popped message link, used to undo a pop.
3694 * @param loader the loader
3695 * @param link the link with a message in it
3698 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3701 _dbus_list_prepend_link (&loader->messages, link);
3705 * Checks whether the loader is confused due to bad data.
3706 * If messages are received that are invalid, the
3707 * loader gets confused and gives up permanently.
3708 * This state is called "corrupted."
3710 * @param loader the loader
3711 * @returns #TRUE if the loader is hosed.
3714 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3716 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3717 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3718 return loader->corrupted;
3722 * Sets the maximum size message we allow.
3724 * @param loader the loader
3725 * @param size the max message size in bytes
3728 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3731 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3733 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3734 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3735 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3737 loader->max_message_size = size;
3741 * Gets the maximum allowed message size in bytes.
3743 * @param loader the loader
3744 * @returns max size in bytes
3747 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3749 return loader->max_message_size;
3752 static DBusDataSlotAllocator slot_allocator;
3753 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3756 * Allocates an integer ID to be used for storing application-specific
3757 * data on any DBusMessage. The allocated ID may then be used
3758 * with dbus_message_set_data() and dbus_message_get_data().
3759 * The passed-in slot must be initialized to -1, and is filled in
3760 * with the slot ID. If the passed-in slot is not -1, it's assumed
3761 * to be already allocated, and its refcount is incremented.
3763 * The allocated slot is global, i.e. all DBusMessage objects will
3764 * have a slot with the given integer ID reserved.
3766 * @param slot_p address of a global variable storing the slot
3767 * @returns #FALSE on failure (no memory)
3770 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3772 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3773 &_DBUS_LOCK_NAME (message_slots),
3778 * Deallocates a global ID for message data slots.
3779 * dbus_message_get_data() and dbus_message_set_data() may no
3780 * longer be used with this slot. Existing data stored on existing
3781 * DBusMessage objects will be freed when the message is
3782 * finalized, but may not be retrieved (and may only be replaced if
3783 * someone else reallocates the slot). When the refcount on the
3784 * passed-in slot reaches 0, it is set to -1.
3786 * @param slot_p address storing the slot to deallocate
3789 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3791 _dbus_return_if_fail (*slot_p >= 0);
3793 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3797 * Stores a pointer on a DBusMessage, along
3798 * with an optional function to be used for freeing
3799 * the data when the data is set again, or when
3800 * the message is finalized. The slot number
3801 * must have been allocated with dbus_message_allocate_data_slot().
3803 * @param message the message
3804 * @param slot the slot number
3805 * @param data the data to store
3806 * @param free_data_func finalizer function for the data
3807 * @returns #TRUE if there was enough memory to store the data
3810 dbus_message_set_data (DBusMessage *message,
3813 DBusFreeFunction free_data_func)
3815 DBusFreeFunction old_free_func;
3819 _dbus_return_val_if_fail (message != NULL, FALSE);
3820 _dbus_return_val_if_fail (slot >= 0, FALSE);
3822 retval = _dbus_data_slot_list_set (&slot_allocator,
3823 &message->slot_list,
3824 slot, data, free_data_func,
3825 &old_free_func, &old_data);
3829 /* Do the actual free outside the message lock */
3831 (* old_free_func) (old_data);
3838 * Retrieves data previously set with dbus_message_set_data().
3839 * The slot must still be allocated (must not have been freed).
3841 * @param message the message
3842 * @param slot the slot to get data from
3843 * @returns the data, or #NULL if not found
3846 dbus_message_get_data (DBusMessage *message,
3851 _dbus_return_val_if_fail (message != NULL, NULL);
3853 res = _dbus_data_slot_list_get (&slot_allocator,
3854 &message->slot_list,
3861 * Utility function to convert a machine-readable (not translated)
3862 * string into a D-Bus message type.
3865 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3866 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3867 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3868 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3869 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3874 dbus_message_type_from_string (const char *type_str)
3876 if (strcmp (type_str, "method_call") == 0)
3877 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3878 if (strcmp (type_str, "method_return") == 0)
3879 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3880 else if (strcmp (type_str, "signal") == 0)
3881 return DBUS_MESSAGE_TYPE_SIGNAL;
3882 else if (strcmp (type_str, "error") == 0)
3883 return DBUS_MESSAGE_TYPE_ERROR;
3885 return DBUS_MESSAGE_TYPE_INVALID;
3889 * Utility function to convert a D-Bus message type into a
3890 * machine-readable string (not translated).
3893 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3894 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3895 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3896 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3897 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3902 dbus_message_type_to_string (int type)
3906 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3907 return "method_call";
3908 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3909 return "method_return";
3910 case DBUS_MESSAGE_TYPE_SIGNAL:
3912 case DBUS_MESSAGE_TYPE_ERROR:
3920 * Turn a DBusMessage into the marshalled form as described in the D-Bus
3923 * Generally, this function is only useful for encapsulating D-Bus messages in
3924 * a different protocol.
3926 * @param msg the DBusMessage
3927 * @param marshalled_data_p the location to save the marshalled form to
3928 * @param len_p the location to save the length of the marshalled form to
3929 * @returns #FALSE if there was not enough memory
3932 dbus_message_marshal (DBusMessage *msg,
3933 char **marshalled_data_p,
3938 _dbus_return_val_if_fail (msg != NULL, FALSE);
3939 _dbus_return_val_if_fail (marshalled_data_p != NULL, FALSE);
3940 _dbus_return_val_if_fail (len_p != NULL, FALSE);
3942 if (!_dbus_string_init (&tmp))
3945 if (!_dbus_string_copy (&(msg->header.data), 0, &tmp, 0))
3948 *len_p = _dbus_string_get_length (&tmp);
3950 if (!_dbus_string_copy (&(msg->body), 0, &tmp, *len_p))
3953 *len_p = _dbus_string_get_length (&tmp);
3955 if (!_dbus_string_steal_data (&tmp, marshalled_data_p))
3958 _dbus_string_free (&tmp);
3962 _dbus_string_free (&tmp);
3967 * Demarshal a D-Bus message from the format described in the D-Bus
3970 * Generally, this function is only useful for encapsulating D-Bus messages in
3971 * a different protocol.
3973 * @param str the marshalled DBusMessage
3974 * @param len the length of str
3975 * @param error the location to save errors to
3976 * @returns #NULL if there was an error
3979 dbus_message_demarshal (const char *str,
3983 DBusMessageLoader *loader;
3987 _dbus_return_val_if_fail (str != NULL, NULL);
3989 loader = _dbus_message_loader_new ();
3994 _dbus_message_loader_get_buffer (loader, &buffer);
3995 _dbus_string_append_len (buffer, str, len);
3996 _dbus_message_loader_return_buffer (loader, buffer, len);
3998 if (!_dbus_message_loader_queue_messages (loader))
4001 if (_dbus_message_loader_get_is_corrupted (loader))
4004 msg = _dbus_message_loader_pop_message (loader);
4009 _dbus_message_loader_unref (loader);
4013 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Message is corrupted");
4014 _dbus_message_loader_unref (loader);
4018 _DBUS_SET_OOM (error);
4019 _dbus_message_loader_unref (loader);
4025 /* tests in dbus-message-util.c */