1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 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 * DBusConnection will automatically set the serial to an appropriate value
167 * when the message is sent; this function is only needed when encapsulating
168 * messages in another protocol, or otherwise bypassing DBusConnection.
170 * @param message the message
171 * @param serial the serial
174 dbus_message_set_serial (DBusMessage *message,
175 dbus_uint32_t serial)
177 _dbus_return_if_fail (message != NULL);
178 _dbus_return_if_fail (!message->locked);
180 _dbus_header_set_serial (&message->header, serial);
184 * Adds a counter to be incremented immediately with the
185 * size of this message, and decremented by the size
186 * of this message when this message if finalized.
187 * The link contains a counter with its refcount already
188 * incremented, but the counter itself not incremented.
189 * Ownership of link and counter refcount is passed to
192 * @param message the message
193 * @param link link with counter as data
196 _dbus_message_add_size_counter_link (DBusMessage *message,
199 /* right now we don't recompute the delta when message
200 * size changes, and that's OK for current purposes
201 * I think, but could be important to change later.
202 * Do recompute it whenever there are no outstanding counters,
203 * since it's basically free.
205 if (message->size_counters == NULL)
207 message->size_counter_delta =
208 _dbus_string_get_length (&message->header.data) +
209 _dbus_string_get_length (&message->body);
212 _dbus_verbose ("message has size %ld\n",
213 message->size_counter_delta);
217 _dbus_list_append_link (&message->size_counters, link);
219 _dbus_counter_adjust (link->data, message->size_counter_delta);
223 * Adds a counter to be incremented immediately with the
224 * size of this message, and decremented by the size
225 * of this message when this message if finalized.
227 * @param message the message
228 * @param counter the counter
229 * @returns #FALSE if no memory
232 _dbus_message_add_size_counter (DBusMessage *message,
233 DBusCounter *counter)
237 link = _dbus_list_alloc_link (counter);
241 _dbus_counter_ref (counter);
242 _dbus_message_add_size_counter_link (message, link);
248 * Removes a counter tracking the size of this message, and decrements
249 * the counter by the size of this message.
251 * @param message the message
252 * @param link_return return the link used
253 * @param counter the counter
256 _dbus_message_remove_size_counter (DBusMessage *message,
257 DBusCounter *counter,
258 DBusList **link_return)
262 link = _dbus_list_find_last (&message->size_counters,
264 _dbus_assert (link != NULL);
266 _dbus_list_unlink (&message->size_counters,
271 _dbus_list_free_link (link);
273 _dbus_counter_adjust (counter, - message->size_counter_delta);
275 _dbus_counter_unref (counter);
279 * Locks a message. Allows checking that applications don't keep a
280 * reference to a message in the outgoing queue and change it
281 * underneath us. Messages are locked when they enter the outgoing
282 * queue (dbus_connection_send_message()), and the library complains
283 * if the message is modified while locked. This function may also
284 * called externally, for applications wrapping D-Bus in another protocol.
286 * @param message the message to lock.
289 dbus_message_lock (DBusMessage *message)
291 if (!message->locked)
293 _dbus_header_update_lengths (&message->header,
294 _dbus_string_get_length (&message->body));
296 /* must have a signature if you have a body */
297 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
298 dbus_message_get_signature (message) != NULL);
300 message->locked = TRUE;
305 set_or_delete_string_field (DBusMessage *message,
311 return _dbus_header_delete_field (&message->header, field);
313 return _dbus_header_set_field_basic (&message->header,
320 /* Probably we don't need to use this */
322 * Sets the signature of the message, i.e. the arguments in the
323 * message payload. The signature includes only "in" arguments for
324 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
325 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
326 * what you might expect (it does not include the signature of the
327 * entire C++-style method).
329 * The signature is a string made up of type codes such as
330 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
331 * the value of #DBUS_TYPE_INVALID). The macros such as
332 * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
333 * may find it useful to use the string forms, such as
334 * #DBUS_TYPE_INT32_AS_STRING.
336 * An "unset" or #NULL signature is considered the same as an empty
337 * signature. In fact dbus_message_get_signature() will never return
340 * @param message the message
341 * @param signature the type signature or #NULL to unset
342 * @returns #FALSE if no memory
345 _dbus_message_set_signature (DBusMessage *message,
346 const char *signature)
348 _dbus_return_val_if_fail (message != NULL, FALSE);
349 _dbus_return_val_if_fail (!message->locked, FALSE);
350 _dbus_return_val_if_fail (signature == NULL ||
351 _dbus_check_is_valid_signature (signature));
352 /* can't delete the signature if you have a message body */
353 _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
356 return set_or_delete_string_field (message,
357 DBUS_HEADER_FIELD_SIGNATURE,
365 * We cache some DBusMessage to reduce the overhead of allocating
366 * them. In my profiling this consistently made about an 8%
367 * difference. It avoids the malloc for the message, the malloc for
368 * the slot list, the malloc for the header string and body string,
369 * and the associated free() calls. It does introduce another global
370 * lock which could be a performance issue in certain cases.
372 * For the echo client/server the round trip time goes from around
373 * .000077 to .000069 with the message cache on my laptop. The sysprof
374 * change is as follows (numbers are cumulative percentage):
376 * with message cache implemented as array as it is now (0.000069 per):
377 * new_empty_header 1.46
378 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
384 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
387 * with message cache implemented as list (0.000070 per roundtrip):
388 * new_empty_header 2.72
389 * list_pop_first 1.88
393 * without cache (0.000077 per roundtrip):
394 * new_empty_header 6.7
395 * string_init_preallocated 3.43
404 * If you implement the message_cache with a list, the primary reason
405 * it's slower is that you add another thread lock (on the DBusList
409 /** Avoid caching huge messages */
410 #define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
412 /** Avoid caching too many messages */
413 #define MAX_MESSAGE_CACHE_SIZE 5
415 _DBUS_DEFINE_GLOBAL_LOCK (message_cache);
416 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
417 static int message_cache_count = 0;
418 static dbus_bool_t message_cache_shutdown_registered = FALSE;
421 dbus_message_cache_shutdown (void *data)
425 _DBUS_LOCK (message_cache);
428 while (i < MAX_MESSAGE_CACHE_SIZE)
430 if (message_cache[i])
431 dbus_message_finalize (message_cache[i]);
436 message_cache_count = 0;
437 message_cache_shutdown_registered = FALSE;
439 _DBUS_UNLOCK (message_cache);
443 * Tries to get a message from the message cache. The retrieved
444 * message will have junk in it, so it still needs to be cleared out
445 * in dbus_message_new_empty_header()
447 * @returns the message, or #NULL if none cached
450 dbus_message_get_cached (void)
452 DBusMessage *message;
457 _DBUS_LOCK (message_cache);
459 _dbus_assert (message_cache_count >= 0);
461 if (message_cache_count == 0)
463 _DBUS_UNLOCK (message_cache);
467 /* This is not necessarily true unless count > 0, and
468 * message_cache is uninitialized until the shutdown is
471 _dbus_assert (message_cache_shutdown_registered);
474 while (i < MAX_MESSAGE_CACHE_SIZE)
476 if (message_cache[i])
478 message = message_cache[i];
479 message_cache[i] = NULL;
480 message_cache_count -= 1;
485 _dbus_assert (message_cache_count >= 0);
486 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
487 _dbus_assert (message != NULL);
489 _dbus_assert (message->refcount.value == 0);
490 _dbus_assert (message->size_counters == NULL);
492 _DBUS_UNLOCK (message_cache);
498 free_size_counter (void *element,
501 DBusCounter *counter = element;
502 DBusMessage *message = data;
504 _dbus_counter_adjust (counter, - message->size_counter_delta);
506 _dbus_counter_unref (counter);
510 * Tries to cache a message, otherwise finalize it.
512 * @param message the message
515 dbus_message_cache_or_finalize (DBusMessage *message)
517 dbus_bool_t was_cached;
520 _dbus_assert (message->refcount.value == 0);
522 /* This calls application code and has to be done first thing
523 * without holding the lock
525 _dbus_data_slot_list_clear (&message->slot_list);
527 _dbus_list_foreach (&message->size_counters,
528 free_size_counter, message);
529 _dbus_list_clear (&message->size_counters);
533 _DBUS_LOCK (message_cache);
535 if (!message_cache_shutdown_registered)
537 _dbus_assert (message_cache_count == 0);
539 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
543 while (i < MAX_MESSAGE_CACHE_SIZE)
545 message_cache[i] = NULL;
549 message_cache_shutdown_registered = TRUE;
552 _dbus_assert (message_cache_count >= 0);
554 if ((_dbus_string_get_length (&message->header.data) +
555 _dbus_string_get_length (&message->body)) >
556 MAX_MESSAGE_SIZE_TO_CACHE)
559 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
562 /* Find empty slot */
564 while (message_cache[i] != NULL)
567 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
569 _dbus_assert (message_cache[i] == NULL);
570 message_cache[i] = message;
571 message_cache_count += 1;
573 #ifndef DBUS_DISABLE_CHECKS
574 message->in_cache = TRUE;
578 _dbus_assert (message->refcount.value == 0);
580 _DBUS_UNLOCK (message_cache);
583 dbus_message_finalize (message);
586 #ifndef DBUS_DISABLE_CHECKS
588 _dbus_message_iter_check (DBusMessageRealIter *iter)
592 _dbus_warn_check_failed ("dbus message iterator is NULL\n");
596 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
598 if (iter->u.reader.byte_order != iter->message->byte_order)
600 _dbus_warn_check_failed ("dbus message changed byte order since iterator was created\n");
603 /* because we swap the message into compiler order when you init an iter */
604 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
606 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
608 if (iter->u.writer.byte_order != iter->message->byte_order)
610 _dbus_warn_check_failed ("dbus message changed byte order since append iterator was created\n");
613 /* because we swap the message into compiler order when you init an iter */
614 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
618 _dbus_warn_check_failed ("dbus message iterator looks uninitialized or corrupted\n");
622 if (iter->changed_stamp != iter->message->changed_stamp)
624 _dbus_warn_check_failed ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)\n");
630 #endif /* DBUS_DISABLE_CHECKS */
633 * Implementation of the varargs arg-getting functions.
634 * dbus_message_get_args() is the place to go for complete
637 * @see dbus_message_get_args
638 * @param iter the message iter
639 * @param error error to be filled in
640 * @param first_arg_type type of the first argument
641 * @param var_args return location for first argument, followed by list of type/location pairs
642 * @returns #FALSE if error was set
645 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
650 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
651 int spec_type, msg_type, i;
654 _dbus_assert (_dbus_message_iter_check (real));
658 spec_type = first_arg_type;
661 while (spec_type != DBUS_TYPE_INVALID)
663 msg_type = dbus_message_iter_get_arg_type (iter);
665 if (msg_type != spec_type)
667 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
668 "Argument %d is specified to be of type \"%s\", but "
669 "is actually of type \"%s\"\n", i,
670 _dbus_type_to_string (spec_type),
671 _dbus_type_to_string (msg_type));
676 if (dbus_type_is_basic (spec_type))
680 ptr = va_arg (var_args, DBusBasicValue*);
682 _dbus_assert (ptr != NULL);
684 _dbus_type_reader_read_basic (&real->u.reader,
687 else if (spec_type == DBUS_TYPE_ARRAY)
690 int spec_element_type;
691 const DBusBasicValue **ptr;
693 DBusTypeReader array;
695 spec_element_type = va_arg (var_args, int);
696 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
698 if (spec_element_type != element_type)
700 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
701 "Argument %d is specified to be an array of \"%s\", but "
702 "is actually an array of \"%s\"\n",
704 _dbus_type_to_string (spec_element_type),
705 _dbus_type_to_string (element_type));
710 if (dbus_type_is_fixed (spec_element_type))
712 ptr = va_arg (var_args, const DBusBasicValue**);
713 n_elements_p = va_arg (var_args, int*);
715 _dbus_assert (ptr != NULL);
716 _dbus_assert (n_elements_p != NULL);
718 _dbus_type_reader_recurse (&real->u.reader, &array);
720 _dbus_type_reader_read_fixed_multi (&array,
723 else if (spec_element_type == DBUS_TYPE_STRING ||
724 spec_element_type == DBUS_TYPE_SIGNATURE ||
725 spec_element_type == DBUS_TYPE_OBJECT_PATH)
731 str_array_p = va_arg (var_args, char***);
732 n_elements_p = va_arg (var_args, int*);
734 _dbus_assert (str_array_p != NULL);
735 _dbus_assert (n_elements_p != NULL);
737 /* Count elements in the array */
738 _dbus_type_reader_recurse (&real->u.reader, &array);
741 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
744 _dbus_type_reader_next (&array);
747 str_array = dbus_new0 (char*, n_elements + 1);
748 if (str_array == NULL)
750 _DBUS_SET_OOM (error);
754 /* Now go through and dup each string */
755 _dbus_type_reader_recurse (&real->u.reader, &array);
758 while (i < n_elements)
761 _dbus_type_reader_read_basic (&array,
764 str_array[i] = _dbus_strdup (s);
765 if (str_array[i] == NULL)
767 dbus_free_string_array (str_array);
768 _DBUS_SET_OOM (error);
774 if (!_dbus_type_reader_next (&array))
775 _dbus_assert (i == n_elements);
778 _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
779 _dbus_assert (i == n_elements);
780 _dbus_assert (str_array[i] == NULL);
782 *str_array_p = str_array;
783 *n_elements_p = n_elements;
785 #ifndef DBUS_DISABLE_CHECKS
788 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now\n",
789 _DBUS_FUNCTION_NAME);
794 #ifndef DBUS_DISABLE_CHECKS
797 _dbus_warn ("you can only read arrays and basic types with %s for now\n",
798 _DBUS_FUNCTION_NAME);
803 spec_type = va_arg (var_args, int);
804 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
806 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
807 "Message has only %d arguments, but more were expected", i);
824 * @defgroup DBusMessage DBusMessage
826 * @brief Message to be sent or received over a #DBusConnection.
828 * A DBusMessage is the most basic unit of communication over a
829 * DBusConnection. A DBusConnection represents a stream of messages
830 * received from a remote application, and a stream of messages
831 * sent to a remote application.
833 * A message has a message type, returned from
834 * dbus_message_get_type(). This indicates whether the message is a
835 * method call, a reply to a method call, a signal, or an error reply.
837 * A message has header fields such as the sender, destination, method
838 * or signal name, and so forth. DBusMessage has accessor functions for
839 * these, such as dbus_message_get_member().
841 * Convenience functions dbus_message_is_method_call(), dbus_message_is_signal(),
842 * and dbus_message_is_error() check several header fields at once and are
843 * slightly more efficient than checking the header fields with individual
844 * accessor functions.
846 * Finally, a message has arguments. The number and types of arguments
847 * are in the message's signature header field (accessed with
848 * dbus_message_get_signature()). Simple argument values are usually
849 * retrieved with dbus_message_get_args() but more complex values such
850 * as structs may require the use of #DBusMessageIter.
852 * The D-Bus specification goes into some more detail about header fields and
859 * @typedef DBusMessage
861 * Opaque data type representing a message received from or to be
862 * sent to another application.
866 * Returns the serial of a message or 0 if none has been specified.
867 * The message's serial number is provided by the application sending
868 * the message and is used to identify replies to this message.
870 * All messages received on a connection will have a serial provided
871 * by the remote application.
873 * For messages you're sending, dbus_connection_send() will assign a
874 * serial and return it to you.
876 * @param message the message
877 * @returns the serial
880 dbus_message_get_serial (DBusMessage *message)
882 _dbus_return_val_if_fail (message != NULL, 0);
884 return _dbus_header_get_serial (&message->header);
888 * Sets the reply serial of a message (the serial of the message this
891 * @param message the message
892 * @param reply_serial the serial we're replying to
893 * @returns #FALSE if not enough memory
896 dbus_message_set_reply_serial (DBusMessage *message,
897 dbus_uint32_t reply_serial)
899 _dbus_return_val_if_fail (message != NULL, FALSE);
900 _dbus_return_val_if_fail (!message->locked, FALSE);
901 _dbus_return_val_if_fail (reply_serial != 0, FALSE); /* 0 is invalid */
903 return _dbus_header_set_field_basic (&message->header,
904 DBUS_HEADER_FIELD_REPLY_SERIAL,
910 * Returns the serial that the message is a reply to or 0 if none.
912 * @param message the message
913 * @returns the reply serial
916 dbus_message_get_reply_serial (DBusMessage *message)
918 dbus_uint32_t v_UINT32;
920 _dbus_return_val_if_fail (message != NULL, 0);
922 if (_dbus_header_get_field_basic (&message->header,
923 DBUS_HEADER_FIELD_REPLY_SERIAL,
932 dbus_message_finalize (DBusMessage *message)
934 _dbus_assert (message->refcount.value == 0);
936 /* This calls application callbacks! */
937 _dbus_data_slot_list_free (&message->slot_list);
939 _dbus_list_foreach (&message->size_counters,
940 free_size_counter, message);
941 _dbus_list_clear (&message->size_counters);
943 _dbus_header_free (&message->header);
944 _dbus_string_free (&message->body);
946 _dbus_assert (message->refcount.value == 0);
952 dbus_message_new_empty_header (void)
954 DBusMessage *message;
955 dbus_bool_t from_cache;
957 message = dbus_message_get_cached ();
966 message = dbus_new (DBusMessage, 1);
969 #ifndef DBUS_DISABLE_CHECKS
970 message->generation = _dbus_current_generation;
974 message->refcount.value = 1;
975 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
976 message->locked = FALSE;
977 #ifndef DBUS_DISABLE_CHECKS
978 message->in_cache = FALSE;
980 message->size_counters = NULL;
981 message->size_counter_delta = 0;
982 message->changed_stamp = 0;
985 _dbus_data_slot_list_init (&message->slot_list);
989 _dbus_header_reinit (&message->header, message->byte_order);
990 _dbus_string_set_length (&message->body, 0);
994 if (!_dbus_header_init (&message->header, message->byte_order))
1000 if (!_dbus_string_init_preallocated (&message->body, 32))
1002 _dbus_header_free (&message->header);
1003 dbus_free (message);
1012 * Constructs a new message of the given message type.
1013 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1014 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1016 * Usually you want to use dbus_message_new_method_call(),
1017 * dbus_message_new_method_return(), dbus_message_new_signal(),
1018 * or dbus_message_new_error() instead.
1020 * @param message_type type of message
1021 * @returns new message or #NULL if no memory
1024 dbus_message_new (int message_type)
1026 DBusMessage *message;
1028 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1030 message = dbus_message_new_empty_header ();
1031 if (message == NULL)
1034 if (!_dbus_header_create (&message->header,
1036 NULL, NULL, NULL, NULL, NULL))
1038 dbus_message_unref (message);
1046 * Constructs a new message to invoke a method on a remote
1047 * object. Returns #NULL if memory can't be allocated for the
1048 * message. The destination may be #NULL in which case no destination
1049 * is set; this is appropriate when using D-Bus in a peer-to-peer
1050 * context (no message bus). The interface may be #NULL, which means
1051 * that if multiple methods with the given name exist it is undefined
1052 * which one will be invoked.
1054 * The path and method names may not be #NULL.
1056 * Destination, path, interface, and method name can't contain
1057 * any invalid characters (see the D-Bus specification).
1059 * @param destination name that the message should be sent to or #NULL
1060 * @param path object path the message should be sent to
1061 * @param interface interface to invoke method on, or #NULL
1062 * @param method method to invoke
1064 * @returns a new DBusMessage, free with dbus_message_unref()
1067 dbus_message_new_method_call (const char *destination,
1069 const char *interface,
1072 DBusMessage *message;
1074 _dbus_return_val_if_fail (path != NULL, NULL);
1075 _dbus_return_val_if_fail (method != NULL, NULL);
1076 _dbus_return_val_if_fail (destination == NULL ||
1077 _dbus_check_is_valid_bus_name (destination), NULL);
1078 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1079 _dbus_return_val_if_fail (interface == NULL ||
1080 _dbus_check_is_valid_interface (interface), NULL);
1081 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
1083 message = dbus_message_new_empty_header ();
1084 if (message == NULL)
1087 if (!_dbus_header_create (&message->header,
1088 DBUS_MESSAGE_TYPE_METHOD_CALL,
1089 destination, path, interface, method, NULL))
1091 dbus_message_unref (message);
1099 * Constructs a message that is a reply to a method call. Returns
1100 * #NULL if memory can't be allocated for the message.
1102 * @param method_call the message being replied to
1103 * @returns a new DBusMessage, free with dbus_message_unref()
1106 dbus_message_new_method_return (DBusMessage *method_call)
1108 DBusMessage *message;
1111 _dbus_return_val_if_fail (method_call != NULL, NULL);
1113 sender = dbus_message_get_sender (method_call);
1115 /* sender is allowed to be null here in peer-to-peer case */
1117 message = dbus_message_new_empty_header ();
1118 if (message == NULL)
1121 if (!_dbus_header_create (&message->header,
1122 DBUS_MESSAGE_TYPE_METHOD_RETURN,
1123 sender, NULL, NULL, NULL, NULL))
1125 dbus_message_unref (message);
1129 dbus_message_set_no_reply (message, TRUE);
1131 if (!dbus_message_set_reply_serial (message,
1132 dbus_message_get_serial (method_call)))
1134 dbus_message_unref (message);
1142 * Constructs a new message representing a signal emission. Returns
1143 * #NULL if memory can't be allocated for the message. A signal is
1144 * identified by its originating object path, interface, and the name
1147 * Path, interface, and signal name must all be valid (the D-Bus
1148 * specification defines the syntax of these fields).
1150 * @param path the path to the object emitting the signal
1151 * @param interface the interface the signal is emitted from
1152 * @param name name of the signal
1153 * @returns a new DBusMessage, free with dbus_message_unref()
1156 dbus_message_new_signal (const char *path,
1157 const char *interface,
1160 DBusMessage *message;
1162 _dbus_return_val_if_fail (path != NULL, NULL);
1163 _dbus_return_val_if_fail (interface != NULL, NULL);
1164 _dbus_return_val_if_fail (name != NULL, NULL);
1165 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1166 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (interface), NULL);
1167 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
1169 message = dbus_message_new_empty_header ();
1170 if (message == NULL)
1173 if (!_dbus_header_create (&message->header,
1174 DBUS_MESSAGE_TYPE_SIGNAL,
1175 NULL, path, interface, name, NULL))
1177 dbus_message_unref (message);
1181 dbus_message_set_no_reply (message, TRUE);
1187 * Creates a new message that is an error reply to another message.
1188 * Error replies are most common in response to method calls, but
1189 * can be returned in reply to any message.
1191 * The error name must be a valid error name according to the syntax
1192 * given in the D-Bus specification. If you don't want to make
1193 * up an error name just use #DBUS_ERROR_FAILED.
1195 * @param reply_to the message we're replying to
1196 * @param error_name the error name
1197 * @param error_message the error message string (or #NULL for none, but please give a message)
1198 * @returns a new error message object, free with dbus_message_unref()
1201 dbus_message_new_error (DBusMessage *reply_to,
1202 const char *error_name,
1203 const char *error_message)
1205 DBusMessage *message;
1207 DBusMessageIter iter;
1209 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1210 _dbus_return_val_if_fail (error_name != NULL, NULL);
1211 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1213 sender = dbus_message_get_sender (reply_to);
1215 /* sender may be NULL for non-message-bus case or
1216 * when the message bus is dealing with an unregistered
1219 message = dbus_message_new_empty_header ();
1220 if (message == NULL)
1223 if (!_dbus_header_create (&message->header,
1224 DBUS_MESSAGE_TYPE_ERROR,
1225 sender, NULL, NULL, NULL, error_name))
1227 dbus_message_unref (message);
1231 dbus_message_set_no_reply (message, TRUE);
1233 if (!dbus_message_set_reply_serial (message,
1234 dbus_message_get_serial (reply_to)))
1236 dbus_message_unref (message);
1240 if (error_message != NULL)
1242 dbus_message_iter_init_append (message, &iter);
1243 if (!dbus_message_iter_append_basic (&iter,
1247 dbus_message_unref (message);
1256 * Creates a new message that is an error reply to another message, allowing
1257 * you to use printf formatting.
1259 * See dbus_message_new_error() for details - this function is the same
1260 * aside from the printf formatting.
1262 * @todo add _DBUS_GNUC_PRINTF to this (requires moving _DBUS_GNUC_PRINTF to
1263 * public header, see DBUS_DEPRECATED for an example)
1265 * @param reply_to the original message
1266 * @param error_name the error name
1267 * @param error_format the error message format as with printf
1268 * @param ... format string arguments
1269 * @returns a new error message
1272 dbus_message_new_error_printf (DBusMessage *reply_to,
1273 const char *error_name,
1274 const char *error_format,
1279 DBusMessage *message;
1281 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1282 _dbus_return_val_if_fail (error_name != NULL, NULL);
1283 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1285 if (!_dbus_string_init (&str))
1288 va_start (args, error_format);
1290 if (_dbus_string_append_printf_valist (&str, error_format, args))
1291 message = dbus_message_new_error (reply_to, error_name,
1292 _dbus_string_get_const_data (&str));
1296 _dbus_string_free (&str);
1305 * Creates a new message that is an exact replica of the message
1306 * specified, except that its refcount is set to 1, its message serial
1307 * is reset to 0, and if the original message was "locked" (in the
1308 * outgoing message queue and thus not modifiable) the new message
1309 * will not be locked.
1311 * @param message the message
1312 * @returns the new message.or #NULL if not enough memory
1315 dbus_message_copy (const DBusMessage *message)
1317 DBusMessage *retval;
1319 _dbus_return_val_if_fail (message != NULL, NULL);
1321 retval = dbus_new0 (DBusMessage, 1);
1325 retval->refcount.value = 1;
1326 retval->byte_order = message->byte_order;
1327 retval->locked = FALSE;
1328 #ifndef DBUS_DISABLE_CHECKS
1329 retval->generation = message->generation;
1332 if (!_dbus_header_copy (&message->header, &retval->header))
1338 if (!_dbus_string_init_preallocated (&retval->body,
1339 _dbus_string_get_length (&message->body)))
1341 _dbus_header_free (&retval->header);
1346 if (!_dbus_string_copy (&message->body, 0,
1353 _dbus_header_free (&retval->header);
1354 _dbus_string_free (&retval->body);
1362 * Increments the reference count of a DBusMessage.
1364 * @param message the message
1365 * @returns the message
1366 * @see dbus_message_unref
1369 dbus_message_ref (DBusMessage *message)
1371 dbus_int32_t old_refcount;
1373 _dbus_return_val_if_fail (message != NULL, NULL);
1374 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1375 _dbus_return_val_if_fail (!message->in_cache, NULL);
1377 old_refcount = _dbus_atomic_inc (&message->refcount);
1378 _dbus_assert (old_refcount >= 1);
1384 * Decrements the reference count of a DBusMessage, freeing the
1385 * message if the count reaches 0.
1387 * @param message the message
1388 * @see dbus_message_ref
1391 dbus_message_unref (DBusMessage *message)
1393 dbus_int32_t old_refcount;
1395 _dbus_return_if_fail (message != NULL);
1396 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1397 _dbus_return_if_fail (!message->in_cache);
1399 old_refcount = _dbus_atomic_dec (&message->refcount);
1401 _dbus_assert (old_refcount >= 0);
1403 if (old_refcount == 1)
1405 /* Calls application callbacks! */
1406 dbus_message_cache_or_finalize (message);
1411 * Gets the type of a message. Types include
1412 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1413 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1414 * types are allowed and all code must silently ignore messages of
1415 * unknown type. #DBUS_MESSAGE_TYPE_INVALID will never be returned.
1417 * @param message the message
1418 * @returns the type of the message
1421 dbus_message_get_type (DBusMessage *message)
1423 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1425 return _dbus_header_get_message_type (&message->header);
1429 * Appends fields to a message given a variable argument list. The
1430 * variable argument list should contain the type of each argument
1431 * followed by the value to append. Appendable types are basic types,
1432 * and arrays of fixed-length basic types. To append variable-length
1433 * basic types, or any more complex value, you have to use an iterator
1434 * rather than this function.
1436 * To append a basic type, specify its type code followed by the
1437 * address of the value. For example:
1441 * dbus_int32_t v_INT32 = 42;
1442 * const char *v_STRING = "Hello World";
1443 * dbus_message_append_args (message,
1444 * DBUS_TYPE_INT32, &v_INT32,
1445 * DBUS_TYPE_STRING, &v_STRING,
1446 * DBUS_TYPE_INVALID);
1449 * To append an array of fixed-length basic types, pass in the
1450 * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
1451 * the array pointer, and a 32-bit integer giving the number of
1452 * elements in the array. So for example:
1454 * const dbus_int32_t array[] = { 1, 2, 3 };
1455 * const dbus_int32_t *v_ARRAY = array;
1456 * dbus_message_append_args (message,
1457 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3,
1458 * DBUS_TYPE_INVALID);
1461 * @warning in C, given "int array[]", "&array == array" (the
1462 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1463 * So if you're using an array instead of a pointer you have to create
1464 * a pointer variable, assign the array to it, then take the address
1465 * of the pointer variable. For strings it works to write
1466 * const char *array = "Hello" and then use &array though.
1468 * The last argument to this function must be #DBUS_TYPE_INVALID,
1469 * marking the end of the argument list. If you don't do this
1470 * then libdbus won't know to stop and will read invalid memory.
1472 * String/signature/path arrays should be passed in as "const char***
1473 * address_of_array" and "int n_elements"
1475 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1477 * @todo If this fails due to lack of memory, the message is hosed and
1478 * you have to start over building the whole message.
1480 * @param message the message
1481 * @param first_arg_type type of the first argument
1482 * @param ... value of first argument, list of additional type-value pairs
1483 * @returns #TRUE on success
1486 dbus_message_append_args (DBusMessage *message,
1493 _dbus_return_val_if_fail (message != NULL, FALSE);
1495 va_start (var_args, first_arg_type);
1496 retval = dbus_message_append_args_valist (message,
1505 * Like dbus_message_append_args() but takes a va_list for use by language bindings.
1507 * @todo for now, if this function fails due to OOM it will leave
1508 * the message half-written and you have to discard the message
1511 * @see dbus_message_append_args.
1512 * @param message the message
1513 * @param first_arg_type type of first argument
1514 * @param var_args value of first argument, then list of type/value pairs
1515 * @returns #TRUE on success
1518 dbus_message_append_args_valist (DBusMessage *message,
1523 DBusMessageIter iter;
1525 _dbus_return_val_if_fail (message != NULL, FALSE);
1527 type = first_arg_type;
1529 dbus_message_iter_init_append (message, &iter);
1531 while (type != DBUS_TYPE_INVALID)
1533 if (dbus_type_is_basic (type))
1535 const DBusBasicValue *value;
1536 value = va_arg (var_args, const DBusBasicValue*);
1538 if (!dbus_message_iter_append_basic (&iter,
1543 else if (type == DBUS_TYPE_ARRAY)
1546 DBusMessageIter array;
1549 element_type = va_arg (var_args, int);
1551 buf[0] = element_type;
1553 if (!dbus_message_iter_open_container (&iter,
1559 if (dbus_type_is_fixed (element_type))
1561 const DBusBasicValue **value;
1564 value = va_arg (var_args, const DBusBasicValue**);
1565 n_elements = va_arg (var_args, int);
1567 if (!dbus_message_iter_append_fixed_array (&array,
1573 else if (element_type == DBUS_TYPE_STRING ||
1574 element_type == DBUS_TYPE_SIGNATURE ||
1575 element_type == DBUS_TYPE_OBJECT_PATH)
1577 const char ***value_p;
1582 value_p = va_arg (var_args, const char***);
1583 n_elements = va_arg (var_args, int);
1588 while (i < n_elements)
1590 if (!dbus_message_iter_append_basic (&array,
1599 _dbus_warn ("arrays of %s can't be appended with %s for now\n",
1600 _dbus_type_to_string (element_type),
1601 _DBUS_FUNCTION_NAME);
1605 if (!dbus_message_iter_close_container (&iter, &array))
1608 #ifndef DBUS_DISABLE_CHECKS
1611 _dbus_warn ("type %s isn't supported yet in %s\n",
1612 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
1617 type = va_arg (var_args, int);
1627 * Gets arguments from a message given a variable argument list. The
1628 * supported types include those supported by
1629 * dbus_message_append_args(); that is, basic types and arrays of
1630 * fixed-length basic types. The arguments are the same as they would
1631 * be for dbus_message_iter_get_basic() or
1632 * dbus_message_iter_get_fixed_array().
1634 * In addition to those types, arrays of string, object path, and
1635 * signature are supported; but these are returned as allocated memory
1636 * and must be freed with dbus_free_string_array(), while the other
1637 * types are returned as const references. To get a string array
1638 * pass in "char ***array_location" and "int *n_elements"
1640 * The variable argument list should contain the type of the argument
1641 * followed by a pointer to where the value should be stored. The list
1642 * is terminated with #DBUS_TYPE_INVALID.
1644 * Except for string arrays, the returned values are constant; do not
1645 * free them. They point into the #DBusMessage.
1647 * If the requested arguments are not present, or do not have the
1648 * requested types, then an error will be set.
1650 * If more arguments than requested are present, the requested
1651 * arguments are returned and the extra arguments are ignored.
1653 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1655 * @param message the message
1656 * @param error error to be filled in on failure
1657 * @param first_arg_type the first argument type
1658 * @param ... location for first argument value, then list of type-location pairs
1659 * @returns #FALSE if the error was set
1662 dbus_message_get_args (DBusMessage *message,
1670 _dbus_return_val_if_fail (message != NULL, FALSE);
1671 _dbus_return_val_if_error_is_set (error, FALSE);
1673 va_start (var_args, first_arg_type);
1674 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1681 * Like dbus_message_get_args but takes a va_list for use by language bindings.
1683 * @see dbus_message_get_args
1684 * @param message the message
1685 * @param error error to be filled in
1686 * @param first_arg_type type of the first argument
1687 * @param var_args return location for first argument, followed by list of type/location pairs
1688 * @returns #FALSE if error was set
1691 dbus_message_get_args_valist (DBusMessage *message,
1696 DBusMessageIter iter;
1698 _dbus_return_val_if_fail (message != NULL, FALSE);
1699 _dbus_return_val_if_error_is_set (error, FALSE);
1701 dbus_message_iter_init (message, &iter);
1702 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1706 _dbus_message_iter_init_common (DBusMessage *message,
1707 DBusMessageRealIter *real,
1710 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1712 /* Since the iterator will read or write who-knows-what from the
1713 * message, we need to get in the right byte order
1715 ensure_byte_order (message);
1717 real->message = message;
1718 real->changed_stamp = message->changed_stamp;
1719 real->iter_type = iter_type;
1720 real->sig_refcount = 0;
1724 * Initializes a #DBusMessageIter for reading the arguments of the
1725 * message passed in.
1727 * When possible, dbus_message_get_args() is much more convenient.
1728 * Some types of argument can only be read with #DBusMessageIter
1731 * The easiest way to iterate is like this:
1733 * dbus_message_iter_init (message, &iter);
1734 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1735 * dbus_message_iter_next (&iter);
1738 * #DBusMessageIter contains no allocated memory; it need not be
1739 * freed, and can be copied by assignment or memcpy().
1741 * @param message the message
1742 * @param iter pointer to an iterator to initialize
1743 * @returns #FALSE if the message has no arguments
1746 dbus_message_iter_init (DBusMessage *message,
1747 DBusMessageIter *iter)
1749 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1750 const DBusString *type_str;
1753 _dbus_return_val_if_fail (message != NULL, FALSE);
1754 _dbus_return_val_if_fail (iter != NULL, FALSE);
1756 get_const_signature (&message->header, &type_str, &type_pos);
1758 _dbus_message_iter_init_common (message, real,
1759 DBUS_MESSAGE_ITER_TYPE_READER);
1761 _dbus_type_reader_init (&real->u.reader,
1762 message->byte_order,
1767 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
1771 * Checks if an iterator has any more fields.
1773 * @param iter the message iter
1774 * @returns #TRUE if there are more fields following
1777 dbus_message_iter_has_next (DBusMessageIter *iter)
1779 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1781 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1782 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1784 return _dbus_type_reader_has_next (&real->u.reader);
1788 * Moves the iterator to the next field, if any. If there's no next
1789 * field, returns #FALSE. If the iterator moves forward, returns
1792 * @param iter the message iter
1793 * @returns #TRUE if the iterator was moved to the next field
1796 dbus_message_iter_next (DBusMessageIter *iter)
1798 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1800 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
1801 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1803 return _dbus_type_reader_next (&real->u.reader);
1807 * Returns the argument type of the argument that the message iterator
1808 * points to. If the iterator is at the end of the message, returns
1809 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
1812 * dbus_message_iter_init (&iter);
1813 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
1814 * dbus_message_iter_next (&iter);
1817 * @param iter the message iter
1818 * @returns the argument type
1821 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1823 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1825 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1826 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
1828 return _dbus_type_reader_get_current_type (&real->u.reader);
1832 * Returns the element type of the array that the message iterator
1833 * points to. Note that you need to check that the iterator points to
1834 * an array prior to using this function.
1836 * @param iter the message iter
1837 * @returns the array element type
1840 dbus_message_iter_get_element_type (DBusMessageIter *iter)
1842 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1844 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1845 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
1846 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
1848 return _dbus_type_reader_get_element_type (&real->u.reader);
1852 * Recurses into a container value when reading values from a message,
1853 * initializing a sub-iterator to use for traversing the child values
1856 * Note that this recurses into a value, not a type, so you can only
1857 * recurse if the value exists. The main implication of this is that
1858 * if you have for example an empty array of array of int32, you can
1859 * recurse into the outermost array, but it will have no values, so
1860 * you won't be able to recurse further. There's no array of int32 to
1863 * If a container is an array of fixed-length types, it is much more
1864 * efficient to use dbus_message_iter_get_fixed_array() to get the
1865 * whole array in one shot, rather than individually walking over the
1868 * Be sure you have somehow checked that
1869 * dbus_message_iter_get_arg_type() matches the type you are expecting
1870 * to recurse into. Results of this function are undefined if there is
1871 * no container to recurse into at the current iterator position.
1873 * @param iter the message iterator
1874 * @param sub the sub-iterator to initialize
1877 dbus_message_iter_recurse (DBusMessageIter *iter,
1878 DBusMessageIter *sub)
1880 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1881 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
1883 _dbus_return_if_fail (_dbus_message_iter_check (real));
1884 _dbus_return_if_fail (sub != NULL);
1887 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
1891 * Returns the current signature of a message iterator. This
1892 * is useful primarily for dealing with variants; one can
1893 * recurse into a variant and determine the signature of
1894 * the variant's value.
1896 * The returned string must be freed with dbus_free().
1898 * @param iter the message iterator
1899 * @returns the contained signature, or NULL if out of memory
1902 dbus_message_iter_get_signature (DBusMessageIter *iter)
1904 const DBusString *sig;
1908 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1910 _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
1912 if (!_dbus_string_init (&retstr))
1915 _dbus_type_reader_get_signature (&real->u.reader, &sig,
1917 if (!_dbus_string_append_len (&retstr,
1918 _dbus_string_get_const_data (sig) + start,
1921 if (!_dbus_string_steal_data (&retstr, &ret))
1923 _dbus_string_free (&retstr);
1928 * Reads a basic-typed value from the message iterator.
1929 * Basic types are the non-containers such as integer and string.
1931 * The value argument should be the address of a location to store
1932 * the returned value. So for int32 it should be a "dbus_int32_t*"
1933 * and for string a "const char**". The returned value is
1934 * by reference and should not be freed.
1936 * Be sure you have somehow checked that
1937 * dbus_message_iter_get_arg_type() matches the type you are
1938 * expecting, or you'll crash when you try to use an integer as a
1939 * string or something.
1941 * To read any container type (array, struct, dict) you will need
1942 * to recurse into the container with dbus_message_iter_recurse().
1943 * If the container is an array of fixed-length values, you can
1944 * get all the array elements at once with
1945 * dbus_message_iter_get_fixed_array(). Otherwise, you have to
1946 * iterate over the container's contents one value at a time.
1948 * All basic-typed values are guaranteed to fit in 8 bytes. So you can
1949 * write code like this:
1952 * dbus_uint64_t value;
1954 * dbus_message_iter_get_basic (&read_iter, &value);
1955 * type = dbus_message_iter_get_arg_type (&read_iter);
1956 * dbus_message_iter_append_basic (&write_iter, type, &value);
1959 * On some really obscure platforms dbus_uint64_t might not exist, if
1960 * you need to worry about this you will know. dbus_uint64_t is just
1961 * one example of a type that's large enough to hold any possible
1962 * value, you could use a struct or char[8] instead if you like.
1964 * @param iter the iterator
1965 * @param value location to store the value
1968 dbus_message_iter_get_basic (DBusMessageIter *iter,
1971 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1973 _dbus_return_if_fail (_dbus_message_iter_check (real));
1974 _dbus_return_if_fail (value != NULL);
1976 _dbus_type_reader_read_basic (&real->u.reader,
1981 * Returns the number of bytes in the array as marshaled in the wire
1982 * protocol. The iterator must currently be inside an array-typed
1985 * This function is deprecated on the grounds that it is stupid. Why
1986 * would you want to know how many bytes are in the array as marshaled
1987 * in the wire protocol? For now, use the n_elements returned from
1988 * dbus_message_iter_get_fixed_array() instead, or iterate over the
1989 * array values and count them.
1991 * @todo introduce a variant of this get_n_elements that returns
1992 * the number of elements, though with a non-fixed array it will not
1993 * be very efficient, so maybe it's not good.
1995 * @param iter the iterator
1996 * @returns the number of bytes in the array
1999 dbus_message_iter_get_array_len (DBusMessageIter *iter)
2001 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2003 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
2005 return _dbus_type_reader_get_array_length (&real->u.reader);
2009 * Reads a block of fixed-length values from the message iterator.
2010 * Fixed-length values are those basic types that are not string-like,
2011 * such as integers, bool, double. The returned block will be from the
2012 * current position in the array until the end of the array.
2014 * The message iter should be "in" the array (that is, you recurse into the
2015 * array, and then you call dbus_message_iter_get_fixed_array() on the
2016 * "sub-iterator" created by dbus_message_iter_recurse()).
2018 * The value argument should be the address of a location to store the
2019 * returned array. So for int32 it should be a "const dbus_int32_t**"
2020 * The returned value is by reference and should not be freed.
2022 * This function should only be used if dbus_type_is_fixed() returns
2023 * #TRUE for the element type.
2025 * If an array's elements are not fixed in size, you have to recurse
2026 * into the array with dbus_message_iter_recurse() and read the
2027 * elements one by one.
2029 * Because the array is not copied, this function runs in constant
2030 * time and is fast; it's much preferred over walking the entire array
2031 * with an iterator. (However, you can always use
2032 * dbus_message_iter_recurse(), even for fixed-length types;
2033 * dbus_message_iter_get_fixed_array() is just an optimization.)
2035 * @param iter the iterator
2036 * @param value location to store the block
2037 * @param n_elements number of elements in the block
2040 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
2044 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2045 int subtype = _dbus_type_reader_get_current_type(&real->u.reader);
2047 _dbus_return_if_fail (_dbus_message_iter_check (real));
2048 _dbus_return_if_fail (value != NULL);
2049 _dbus_return_if_fail ((subtype == DBUS_TYPE_INVALID) ||
2050 dbus_type_is_fixed (subtype));
2052 _dbus_type_reader_read_fixed_multi (&real->u.reader,
2057 * Initializes a #DBusMessageIter for appending arguments to the end
2060 * @todo If appending any of the arguments fails due to lack of
2061 * memory, the message is hosed and you have to start over building
2062 * the whole message.
2064 * @param message the message
2065 * @param iter pointer to an iterator to initialize
2068 dbus_message_iter_init_append (DBusMessage *message,
2069 DBusMessageIter *iter)
2071 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2073 _dbus_return_if_fail (message != NULL);
2074 _dbus_return_if_fail (iter != NULL);
2076 _dbus_message_iter_init_common (message, real,
2077 DBUS_MESSAGE_ITER_TYPE_WRITER);
2079 /* We create the signature string and point iterators at it "on demand"
2080 * when a value is actually appended. That means that init() never fails
2083 _dbus_type_writer_init_types_delayed (&real->u.writer,
2084 message->byte_order,
2086 _dbus_string_get_length (&message->body));
2090 * Creates a temporary signature string containing the current
2091 * signature, stores it in the iterator, and points the iterator to
2092 * the end of it. Used any time we write to the message.
2094 * @param real an iterator without a type_str
2095 * @returns #FALSE if no memory
2098 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
2101 const DBusString *current_sig;
2102 int current_sig_pos;
2104 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2106 if (real->u.writer.type_str != NULL)
2108 _dbus_assert (real->sig_refcount > 0);
2109 real->sig_refcount += 1;
2113 str = dbus_new (DBusString, 1);
2117 if (!_dbus_header_get_field_raw (&real->message->header,
2118 DBUS_HEADER_FIELD_SIGNATURE,
2119 ¤t_sig, ¤t_sig_pos))
2126 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2127 current_sig_pos += 1; /* move on to sig data */
2129 if (!_dbus_string_init_preallocated (str, current_len + 4))
2135 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2138 _dbus_string_free (str);
2145 if (!_dbus_string_init_preallocated (str, 4))
2152 real->sig_refcount = 1;
2154 _dbus_type_writer_add_types (&real->u.writer,
2155 str, _dbus_string_get_length (str));
2160 * Sets the new signature as the message signature, frees the
2161 * signature string, and marks the iterator as not having a type_str
2162 * anymore. Frees the signature even if it fails, so you can't
2163 * really recover from failure. Kinda busted.
2165 * @param real an iterator without a type_str
2166 * @returns #FALSE if no memory
2169 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2172 const char *v_STRING;
2175 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2176 _dbus_assert (real->u.writer.type_str != NULL);
2177 _dbus_assert (real->sig_refcount > 0);
2179 real->sig_refcount -= 1;
2181 if (real->sig_refcount > 0)
2183 _dbus_assert (real->sig_refcount == 0);
2187 str = real->u.writer.type_str;
2189 v_STRING = _dbus_string_get_const_data (str);
2190 if (!_dbus_header_set_field_basic (&real->message->header,
2191 DBUS_HEADER_FIELD_SIGNATURE,
2192 DBUS_TYPE_SIGNATURE,
2196 _dbus_type_writer_remove_types (&real->u.writer);
2197 _dbus_string_free (str);
2203 #ifndef DBUS_DISABLE_CHECKS
2205 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2207 if (!_dbus_message_iter_check (iter))
2210 if (iter->message->locked)
2212 _dbus_warn_check_failed ("dbus append iterator can't be used: message is locked (has already been sent)\n");
2218 #endif /* DBUS_DISABLE_CHECKS */
2221 * Appends a basic-typed value to the message. The basic types are the
2222 * non-container types such as integer and string.
2224 * The "value" argument should be the address of a basic-typed value.
2225 * So for string, const char**. For integer, dbus_int32_t*.
2227 * @todo If this fails due to lack of memory, the message is hosed and
2228 * you have to start over building the whole message.
2230 * @param iter the append iterator
2231 * @param type the type of the value
2232 * @param value the address of the value
2233 * @returns #FALSE if not enough memory
2236 dbus_message_iter_append_basic (DBusMessageIter *iter,
2240 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2243 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2244 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2245 _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2246 _dbus_return_val_if_fail (value != NULL, FALSE);
2248 if (!_dbus_message_iter_open_signature (real))
2251 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
2253 if (!_dbus_message_iter_close_signature (real))
2260 * Appends a block of fixed-length values to an array. The
2261 * fixed-length types are all basic types that are not string-like. So
2262 * int32, double, bool, etc. You must call
2263 * dbus_message_iter_open_container() to open an array of values
2264 * before calling this function. You may call this function multiple
2265 * times (and intermixed with calls to
2266 * dbus_message_iter_append_basic()) for the same array.
2268 * The "value" argument should be the address of the array. So for
2269 * integer, "dbus_int32_t**" is expected for example.
2271 * @warning in C, given "int array[]", "&array == array" (the
2272 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
2273 * So if you're using an array instead of a pointer you have to create
2274 * a pointer variable, assign the array to it, then take the address
2275 * of the pointer variable.
2277 * const dbus_int32_t array[] = { 1, 2, 3 };
2278 * const dbus_int32_t *v_ARRAY = array;
2279 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
2280 * fprintf (stderr, "No memory!\n");
2282 * For strings it works to write const char *array = "Hello" and then
2283 * use &array though.
2285 * @todo If this fails due to lack of memory, the message is hosed and
2286 * you have to start over building the whole message.
2288 * @param iter the append iterator
2289 * @param element_type the type of the array elements
2290 * @param value the address of the array
2291 * @param n_elements the number of elements to append
2292 * @returns #FALSE if not enough memory
2295 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
2300 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2303 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2304 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2305 _dbus_return_val_if_fail (dbus_type_is_fixed (element_type), FALSE);
2306 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
2307 _dbus_return_val_if_fail (value != NULL, FALSE);
2308 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
2309 _dbus_return_val_if_fail (n_elements <=
2310 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
2313 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
2319 * Appends a container-typed value to the message; you are required to
2320 * append the contents of the container using the returned
2321 * sub-iterator, and then call
2322 * dbus_message_iter_close_container(). Container types are for
2323 * example struct, variant, and array. For variants, the
2324 * contained_signature should be the type of the single value inside
2325 * the variant. For structs and dict entries, contained_signature
2326 * should be #NULL; it will be set to whatever types you write into
2327 * the struct. For arrays, contained_signature should be the type of
2328 * the array elements.
2330 * @todo If this fails due to lack of memory, the message is hosed and
2331 * you have to start over building the whole message.
2333 * @param iter the append iterator
2334 * @param type the type of the value
2335 * @param contained_signature the type of container contents
2336 * @param sub sub-iterator to initialize
2337 * @returns #FALSE if not enough memory
2340 dbus_message_iter_open_container (DBusMessageIter *iter,
2342 const char *contained_signature,
2343 DBusMessageIter *sub)
2345 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2346 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2347 DBusString contained_str;
2349 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2350 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2351 _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
2352 _dbus_return_val_if_fail (sub != NULL, FALSE);
2353 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
2354 contained_signature == NULL) ||
2355 (type == DBUS_TYPE_DICT_ENTRY &&
2356 contained_signature == NULL) ||
2357 (type == DBUS_TYPE_VARIANT &&
2358 contained_signature != NULL) ||
2359 (type == DBUS_TYPE_ARRAY &&
2360 contained_signature != NULL), FALSE);
2362 /* this would fail if the contained_signature is a dict entry, since
2363 * dict entries are invalid signatures standalone (they must be in
2366 _dbus_return_val_if_fail ((type == DBUS_TYPE_ARRAY && contained_signature && *contained_signature == DBUS_DICT_ENTRY_BEGIN_CHAR) ||
2367 (contained_signature == NULL ||
2368 _dbus_check_is_valid_signature (contained_signature)),
2371 if (!_dbus_message_iter_open_signature (real))
2376 if (contained_signature != NULL)
2378 _dbus_string_init_const (&contained_str, contained_signature);
2380 return _dbus_type_writer_recurse (&real->u.writer,
2383 &real_sub->u.writer);
2387 return _dbus_type_writer_recurse (&real->u.writer,
2390 &real_sub->u.writer);
2396 * Closes a container-typed value appended to the message; may write
2397 * out more information to the message known only after the entire
2398 * container is written, and may free resources created by
2399 * dbus_message_iter_open_container().
2401 * @todo If this fails due to lack of memory, the message is hosed and
2402 * you have to start over building the whole message.
2404 * @param iter the append iterator
2405 * @param sub sub-iterator to close
2406 * @returns #FALSE if not enough memory
2409 dbus_message_iter_close_container (DBusMessageIter *iter,
2410 DBusMessageIter *sub)
2412 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2413 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2416 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2417 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2418 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
2419 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2421 ret = _dbus_type_writer_unrecurse (&real->u.writer,
2422 &real_sub->u.writer);
2424 if (!_dbus_message_iter_close_signature (real))
2431 * Sets a flag indicating that the message does not want a reply; if
2432 * this flag is set, the other end of the connection may (but is not
2433 * required to) optimize by not sending method return or error
2434 * replies. If this flag is set, there is no way to know whether the
2435 * message successfully arrived at the remote end. Normally you know a
2436 * message was received when you receive the reply to it.
2438 * The flag is #FALSE by default, that is by default the other end is
2439 * required to reply.
2441 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_REPLY_EXPECTED
2443 * @param message the message
2444 * @param no_reply #TRUE if no reply is desired
2447 dbus_message_set_no_reply (DBusMessage *message,
2448 dbus_bool_t no_reply)
2450 _dbus_return_if_fail (message != NULL);
2451 _dbus_return_if_fail (!message->locked);
2453 _dbus_header_toggle_flag (&message->header,
2454 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
2459 * Returns #TRUE if the message does not expect
2462 * @param message the message
2463 * @returns #TRUE if the message sender isn't waiting for a reply
2466 dbus_message_get_no_reply (DBusMessage *message)
2468 _dbus_return_val_if_fail (message != NULL, FALSE);
2470 return _dbus_header_get_flag (&message->header,
2471 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
2475 * Sets a flag indicating that an owner for the destination name will
2476 * be automatically started before the message is delivered. When this
2477 * flag is set, the message is held until a name owner finishes
2478 * starting up, or fails to start up. In case of failure, the reply
2481 * The flag is set to #TRUE by default, i.e. auto starting is the default.
2483 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_AUTO_START
2485 * @param message the message
2486 * @param auto_start #TRUE if auto-starting is desired
2489 dbus_message_set_auto_start (DBusMessage *message,
2490 dbus_bool_t auto_start)
2492 _dbus_return_if_fail (message != NULL);
2493 _dbus_return_if_fail (!message->locked);
2495 _dbus_header_toggle_flag (&message->header,
2496 DBUS_HEADER_FLAG_NO_AUTO_START,
2501 * Returns #TRUE if the message will cause an owner for
2502 * destination name to be auto-started.
2504 * @param message the message
2505 * @returns #TRUE if the message will use auto-start
2508 dbus_message_get_auto_start (DBusMessage *message)
2510 _dbus_return_val_if_fail (message != NULL, FALSE);
2512 return !_dbus_header_get_flag (&message->header,
2513 DBUS_HEADER_FLAG_NO_AUTO_START);
2518 * Sets the object path this message is being sent to (for
2519 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
2520 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
2522 * The path must contain only valid characters as defined
2523 * in the D-Bus specification.
2525 * @param message the message
2526 * @param object_path the path or #NULL to unset
2527 * @returns #FALSE if not enough memory
2530 dbus_message_set_path (DBusMessage *message,
2531 const char *object_path)
2533 _dbus_return_val_if_fail (message != NULL, FALSE);
2534 _dbus_return_val_if_fail (!message->locked, FALSE);
2535 _dbus_return_val_if_fail (object_path == NULL ||
2536 _dbus_check_is_valid_path (object_path),
2539 return set_or_delete_string_field (message,
2540 DBUS_HEADER_FIELD_PATH,
2541 DBUS_TYPE_OBJECT_PATH,
2546 * Gets the object path this message is being sent to (for
2547 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
2548 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2550 * See also dbus_message_get_path_decomposed().
2552 * The returned string becomes invalid if the message is
2553 * modified, since it points into the wire-marshaled message data.
2555 * @param message the message
2556 * @returns the path (should not be freed) or #NULL
2559 dbus_message_get_path (DBusMessage *message)
2563 _dbus_return_val_if_fail (message != NULL, NULL);
2565 v = NULL; /* in case field doesn't exist */
2566 _dbus_header_get_field_basic (&message->header,
2567 DBUS_HEADER_FIELD_PATH,
2568 DBUS_TYPE_OBJECT_PATH,
2574 * Checks if the message has a particular object path. The object
2575 * path is the destination object for a method call or the emitting
2576 * object for a signal.
2578 * @param message the message
2579 * @param path the path name
2580 * @returns #TRUE if there is a path field in the header
2583 dbus_message_has_path (DBusMessage *message,
2586 const char *msg_path;
2587 msg_path = dbus_message_get_path (message);
2589 if (msg_path == NULL)
2600 if (strcmp (msg_path, path) == 0)
2607 * Gets the object path this message is being sent to
2608 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2609 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
2610 * format (one array element per path component).
2611 * Free the returned array with dbus_free_string_array().
2613 * An empty but non-NULL path array means the path "/".
2614 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
2615 * and the path "/" becomes { NULL }.
2617 * See also dbus_message_get_path().
2619 * @todo this could be optimized by using the len from the message
2620 * instead of calling strlen() again
2622 * @param message the message
2623 * @param path place to store allocated array of path components; #NULL set here if no path field exists
2624 * @returns #FALSE if no memory to allocate the array
2627 dbus_message_get_path_decomposed (DBusMessage *message,
2632 _dbus_return_val_if_fail (message != NULL, FALSE);
2633 _dbus_return_val_if_fail (path != NULL, FALSE);
2637 v = dbus_message_get_path (message);
2640 if (!_dbus_decompose_path (v, strlen (v),
2648 * Sets the interface this message is being sent to
2649 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
2650 * the interface a signal is being emitted from
2651 * (for DBUS_MESSAGE_TYPE_SIGNAL).
2653 * The interface name must contain only valid characters as defined
2654 * in the D-Bus specification.
2656 * @param message the message
2657 * @param interface the interface or #NULL to unset
2658 * @returns #FALSE if not enough memory
2661 dbus_message_set_interface (DBusMessage *message,
2662 const char *interface)
2664 _dbus_return_val_if_fail (message != NULL, FALSE);
2665 _dbus_return_val_if_fail (!message->locked, FALSE);
2666 _dbus_return_val_if_fail (interface == NULL ||
2667 _dbus_check_is_valid_interface (interface),
2670 return set_or_delete_string_field (message,
2671 DBUS_HEADER_FIELD_INTERFACE,
2677 * Gets the interface this message is being sent to
2678 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
2679 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
2680 * The interface name is fully-qualified (namespaced).
2681 * Returns #NULL if none.
2683 * The returned string becomes invalid if the message is
2684 * modified, since it points into the wire-marshaled message data.
2686 * @param message the message
2687 * @returns the message interface (should not be freed) or #NULL
2690 dbus_message_get_interface (DBusMessage *message)
2694 _dbus_return_val_if_fail (message != NULL, NULL);
2696 v = NULL; /* in case field doesn't exist */
2697 _dbus_header_get_field_basic (&message->header,
2698 DBUS_HEADER_FIELD_INTERFACE,
2705 * Checks if the message has an interface
2707 * @param message the message
2708 * @param interface the interface name
2709 * @returns #TRUE if the interface field in the header matches
2712 dbus_message_has_interface (DBusMessage *message,
2713 const char *interface)
2715 const char *msg_interface;
2716 msg_interface = dbus_message_get_interface (message);
2718 if (msg_interface == NULL)
2720 if (interface == NULL)
2726 if (interface == NULL)
2729 if (strcmp (msg_interface, interface) == 0)
2737 * Sets the interface member being invoked
2738 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2739 * (DBUS_MESSAGE_TYPE_SIGNAL).
2741 * The member name must contain only valid characters as defined
2742 * in the D-Bus specification.
2744 * @param message the message
2745 * @param member the member or #NULL to unset
2746 * @returns #FALSE if not enough memory
2749 dbus_message_set_member (DBusMessage *message,
2752 _dbus_return_val_if_fail (message != NULL, FALSE);
2753 _dbus_return_val_if_fail (!message->locked, FALSE);
2754 _dbus_return_val_if_fail (member == NULL ||
2755 _dbus_check_is_valid_member (member),
2758 return set_or_delete_string_field (message,
2759 DBUS_HEADER_FIELD_MEMBER,
2765 * Gets the interface member being invoked
2766 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
2767 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
2769 * The returned string becomes invalid if the message is
2770 * modified, since it points into the wire-marshaled message data.
2772 * @param message the message
2773 * @returns the member name (should not be freed) or #NULL
2776 dbus_message_get_member (DBusMessage *message)
2780 _dbus_return_val_if_fail (message != NULL, NULL);
2782 v = NULL; /* in case field doesn't exist */
2783 _dbus_header_get_field_basic (&message->header,
2784 DBUS_HEADER_FIELD_MEMBER,
2791 * Checks if the message has an interface member
2793 * @param message the message
2794 * @param member the member name
2795 * @returns #TRUE if there is a member field in the header
2798 dbus_message_has_member (DBusMessage *message,
2801 const char *msg_member;
2802 msg_member = dbus_message_get_member (message);
2804 if (msg_member == NULL)
2815 if (strcmp (msg_member, member) == 0)
2823 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
2824 * The name is fully-qualified (namespaced).
2826 * The error name must contain only valid characters as defined
2827 * in the D-Bus specification.
2829 * @param message the message
2830 * @param error_name the name or #NULL to unset
2831 * @returns #FALSE if not enough memory
2834 dbus_message_set_error_name (DBusMessage *message,
2835 const char *error_name)
2837 _dbus_return_val_if_fail (message != NULL, FALSE);
2838 _dbus_return_val_if_fail (!message->locked, FALSE);
2839 _dbus_return_val_if_fail (error_name == NULL ||
2840 _dbus_check_is_valid_error_name (error_name),
2843 return set_or_delete_string_field (message,
2844 DBUS_HEADER_FIELD_ERROR_NAME,
2850 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
2853 * The returned string becomes invalid if the message is
2854 * modified, since it points into the wire-marshaled message data.
2856 * @param message the message
2857 * @returns the error name (should not be freed) or #NULL
2860 dbus_message_get_error_name (DBusMessage *message)
2864 _dbus_return_val_if_fail (message != NULL, NULL);
2866 v = NULL; /* in case field doesn't exist */
2867 _dbus_header_get_field_basic (&message->header,
2868 DBUS_HEADER_FIELD_ERROR_NAME,
2875 * Sets the message's destination. The destination is the name of
2876 * another connection on the bus and may be either the unique name
2877 * assigned by the bus to each connection, or a well-known name
2878 * specified in advance.
2880 * The destination name must contain only valid characters as defined
2881 * in the D-Bus specification.
2883 * @param message the message
2884 * @param destination the destination name or #NULL to unset
2885 * @returns #FALSE if not enough memory
2888 dbus_message_set_destination (DBusMessage *message,
2889 const char *destination)
2891 _dbus_return_val_if_fail (message != NULL, FALSE);
2892 _dbus_return_val_if_fail (!message->locked, FALSE);
2893 _dbus_return_val_if_fail (destination == NULL ||
2894 _dbus_check_is_valid_bus_name (destination),
2897 return set_or_delete_string_field (message,
2898 DBUS_HEADER_FIELD_DESTINATION,
2904 * Gets the destination of a message or #NULL if there is none set.
2906 * The returned string becomes invalid if the message is
2907 * modified, since it points into the wire-marshaled message data.
2909 * @param message the message
2910 * @returns the message destination (should not be freed) or #NULL
2913 dbus_message_get_destination (DBusMessage *message)
2917 _dbus_return_val_if_fail (message != NULL, NULL);
2919 v = NULL; /* in case field doesn't exist */
2920 _dbus_header_get_field_basic (&message->header,
2921 DBUS_HEADER_FIELD_DESTINATION,
2928 * Sets the message sender.
2930 * The sender must be a valid bus name as defined in the D-Bus
2933 * Usually you don't want to call this. The message bus daemon will
2934 * call it to set the origin of each message. If you aren't implementing
2935 * a message bus daemon you shouldn't need to set the sender.
2937 * @param message the message
2938 * @param sender the sender or #NULL to unset
2939 * @returns #FALSE if not enough memory
2942 dbus_message_set_sender (DBusMessage *message,
2945 _dbus_return_val_if_fail (message != NULL, FALSE);
2946 _dbus_return_val_if_fail (!message->locked, FALSE);
2947 _dbus_return_val_if_fail (sender == NULL ||
2948 _dbus_check_is_valid_bus_name (sender),
2951 return set_or_delete_string_field (message,
2952 DBUS_HEADER_FIELD_SENDER,
2958 * Gets the unique name of the connection which originated this
2959 * message, or #NULL if unknown or inapplicable. The sender is filled
2960 * in by the message bus.
2962 * Note, the returned sender is always the unique bus name.
2963 * Connections may own multiple other bus names, but those
2964 * are not found in the sender field.
2966 * The returned string becomes invalid if the message is
2967 * modified, since it points into the wire-marshaled message data.
2969 * @param message the message
2970 * @returns the unique name of the sender or #NULL
2973 dbus_message_get_sender (DBusMessage *message)
2977 _dbus_return_val_if_fail (message != NULL, NULL);
2979 v = NULL; /* in case field doesn't exist */
2980 _dbus_header_get_field_basic (&message->header,
2981 DBUS_HEADER_FIELD_SENDER,
2988 * Gets the type signature of the message, i.e. the arguments in the
2989 * message payload. The signature includes only "in" arguments for
2990 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
2991 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
2992 * what you might expect (that is, it does not include the signature of the
2993 * entire C++-style method).
2995 * The signature is a string made up of type codes such as
2996 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
2997 * the value of #DBUS_TYPE_INVALID).
2999 * The returned string becomes invalid if the message is
3000 * modified, since it points into the wire-marshaled message data.
3002 * @param message the message
3003 * @returns the type signature
3006 dbus_message_get_signature (DBusMessage *message)
3008 const DBusString *type_str;
3011 _dbus_return_val_if_fail (message != NULL, NULL);
3013 get_const_signature (&message->header, &type_str, &type_pos);
3015 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
3019 _dbus_message_has_type_interface_member (DBusMessage *message,
3021 const char *interface,
3026 _dbus_assert (message != NULL);
3027 _dbus_assert (interface != NULL);
3028 _dbus_assert (member != NULL);
3030 if (dbus_message_get_type (message) != type)
3033 /* Optimize by checking the short member name first
3034 * instead of the longer interface name
3037 n = dbus_message_get_member (message);
3039 if (n && strcmp (n, member) == 0)
3041 n = dbus_message_get_interface (message);
3043 if (n == NULL || strcmp (n, interface) == 0)
3051 * Checks whether the message is a method call with the given
3052 * interface and member fields. If the message is not
3053 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
3054 * member field, returns #FALSE. If the interface field is missing,
3055 * then it will be assumed equal to the provided interface. The D-Bus
3056 * protocol allows method callers to leave out the interface name.
3058 * @param message the message
3059 * @param interface the name to check (must not be #NULL)
3060 * @param method the name to check (must not be #NULL)
3062 * @returns #TRUE if the message is the specified method call
3065 dbus_message_is_method_call (DBusMessage *message,
3066 const char *interface,
3069 _dbus_return_val_if_fail (message != NULL, FALSE);
3070 _dbus_return_val_if_fail (interface != NULL, FALSE);
3071 _dbus_return_val_if_fail (method != NULL, FALSE);
3072 /* don't check that interface/method are valid since it would be
3073 * expensive, and not catch many common errors
3076 return _dbus_message_has_type_interface_member (message,
3077 DBUS_MESSAGE_TYPE_METHOD_CALL,
3082 * Checks whether the message is a signal with the given interface and
3083 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
3084 * has a different interface or member field, returns #FALSE.
3086 * @param message the message
3087 * @param interface the name to check (must not be #NULL)
3088 * @param signal_name the name to check (must not be #NULL)
3090 * @returns #TRUE if the message is the specified signal
3093 dbus_message_is_signal (DBusMessage *message,
3094 const char *interface,
3095 const char *signal_name)
3097 _dbus_return_val_if_fail (message != NULL, FALSE);
3098 _dbus_return_val_if_fail (interface != NULL, FALSE);
3099 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
3100 /* don't check that interface/name are valid since it would be
3101 * expensive, and not catch many common errors
3104 return _dbus_message_has_type_interface_member (message,
3105 DBUS_MESSAGE_TYPE_SIGNAL,
3106 interface, signal_name);
3110 * Checks whether the message is an error reply with the given error
3111 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
3112 * different name, returns #FALSE.
3114 * @param message the message
3115 * @param error_name the name to check (must not be #NULL)
3117 * @returns #TRUE if the message is the specified error
3120 dbus_message_is_error (DBusMessage *message,
3121 const char *error_name)
3125 _dbus_return_val_if_fail (message != NULL, FALSE);
3126 _dbus_return_val_if_fail (error_name != NULL, FALSE);
3127 /* don't check that error_name is valid since it would be expensive,
3128 * and not catch many common errors
3131 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3134 n = dbus_message_get_error_name (message);
3136 if (n && strcmp (n, error_name) == 0)
3143 * Checks whether the message was sent to the given name. If the
3144 * message has no destination specified or has a different
3145 * destination, returns #FALSE.
3147 * @param message the message
3148 * @param name the name to check (must not be #NULL)
3150 * @returns #TRUE if the message has the given destination name
3153 dbus_message_has_destination (DBusMessage *message,
3158 _dbus_return_val_if_fail (message != NULL, FALSE);
3159 _dbus_return_val_if_fail (name != NULL, FALSE);
3160 /* don't check that name is valid since it would be expensive, and
3161 * not catch many common errors
3164 s = dbus_message_get_destination (message);
3166 if (s && strcmp (s, name) == 0)
3173 * Checks whether the message has the given unique name as its sender.
3174 * If the message has no sender specified or has a different sender,
3175 * returns #FALSE. Note that a peer application will always have the
3176 * unique name of the connection as the sender. So you can't use this
3177 * function to see whether a sender owned a well-known name.
3179 * Messages from the bus itself will have #DBUS_SERVICE_DBUS
3182 * @param message the message
3183 * @param name the name to check (must not be #NULL)
3185 * @returns #TRUE if the message has the given sender
3188 dbus_message_has_sender (DBusMessage *message,
3193 _dbus_return_val_if_fail (message != NULL, FALSE);
3194 _dbus_return_val_if_fail (name != NULL, FALSE);
3195 /* don't check that name is valid since it would be expensive, and
3196 * not catch many common errors
3199 s = dbus_message_get_sender (message);
3201 if (s && strcmp (s, name) == 0)
3208 * Checks whether the message has the given signature; see
3209 * dbus_message_get_signature() for more details on what the signature
3212 * @param message the message
3213 * @param signature typecode array
3214 * @returns #TRUE if message has the given signature
3217 dbus_message_has_signature (DBusMessage *message,
3218 const char *signature)
3222 _dbus_return_val_if_fail (message != NULL, FALSE);
3223 _dbus_return_val_if_fail (signature != NULL, FALSE);
3224 /* don't check that signature is valid since it would be expensive,
3225 * and not catch many common errors
3228 s = dbus_message_get_signature (message);
3230 if (s && strcmp (s, signature) == 0)
3237 * Sets a #DBusError based on the contents of the given
3238 * message. The error is only set if the message
3239 * is an error message, as in #DBUS_MESSAGE_TYPE_ERROR.
3240 * The name of the error is set to the name of the message,
3241 * and the error message is set to the first argument
3242 * if the argument exists and is a string.
3244 * The return value indicates whether the error was set (the error is
3245 * set if and only if the message is an error message). So you can
3246 * check for an error reply and convert it to DBusError in one go:
3248 * if (dbus_set_error_from_message (error, reply))
3254 * @param error the error to set
3255 * @param message the message to set it from
3256 * @returns #TRUE if the message had type #DBUS_MESSAGE_TYPE_ERROR
3259 dbus_set_error_from_message (DBusError *error,
3260 DBusMessage *message)
3264 _dbus_return_val_if_fail (message != NULL, FALSE);
3265 _dbus_return_val_if_error_is_set (error, FALSE);
3267 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
3271 dbus_message_get_args (message, NULL,
3272 DBUS_TYPE_STRING, &str,
3275 dbus_set_error (error, dbus_message_get_error_name (message),
3276 str ? "%s" : NULL, str);
3284 * @addtogroup DBusMessageInternals
3290 * The initial buffer size of the message loader.
3292 * @todo this should be based on min header size plus some average
3293 * body size, or something. Or rather, the min header size only, if we
3294 * want to try to read only the header, store that in a DBusMessage,
3295 * then read only the body and store that, etc., depends on
3296 * how we optimize _dbus_message_loader_get_buffer() and what
3297 * the exact message format is.
3299 #define INITIAL_LOADER_DATA_LEN 32
3302 * Creates a new message loader. Returns #NULL if memory can't
3305 * @returns new loader, or #NULL.
3308 _dbus_message_loader_new (void)
3310 DBusMessageLoader *loader;
3312 loader = dbus_new0 (DBusMessageLoader, 1);
3316 loader->refcount = 1;
3318 loader->corrupted = FALSE;
3319 loader->corruption_reason = DBUS_VALID;
3321 /* this can be configured by the app, but defaults to the protocol max */
3322 loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3324 if (!_dbus_string_init (&loader->data))
3330 /* preallocate the buffer for speed, ignore failure */
3331 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3332 _dbus_string_set_length (&loader->data, 0);
3338 * Increments the reference count of the loader.
3340 * @param loader the loader.
3341 * @returns the loader
3344 _dbus_message_loader_ref (DBusMessageLoader *loader)
3346 loader->refcount += 1;
3352 * Decrements the reference count of the loader and finalizes the
3353 * loader when the count reaches zero.
3355 * @param loader the loader.
3358 _dbus_message_loader_unref (DBusMessageLoader *loader)
3360 loader->refcount -= 1;
3361 if (loader->refcount == 0)
3363 _dbus_list_foreach (&loader->messages,
3364 (DBusForeachFunction) dbus_message_unref,
3366 _dbus_list_clear (&loader->messages);
3367 _dbus_string_free (&loader->data);
3373 * Gets the buffer to use for reading data from the network. Network
3374 * data is read directly into an allocated buffer, which is then used
3375 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3376 * The buffer must always be returned immediately using
3377 * _dbus_message_loader_return_buffer(), even if no bytes are
3378 * successfully read.
3380 * @todo this function can be a lot more clever. For example
3381 * it can probably always return a buffer size to read exactly
3382 * the body of the next message, thus avoiding any memory wastage
3385 * @todo we need to enforce a max length on strings in header fields.
3387 * @param loader the message loader.
3388 * @param buffer the buffer
3391 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3392 DBusString **buffer)
3394 _dbus_assert (!loader->buffer_outstanding);
3396 *buffer = &loader->data;
3398 loader->buffer_outstanding = TRUE;
3402 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3403 * indicating to the loader how many bytes of the buffer were filled
3404 * in. This function must always be called, even if no bytes were
3405 * successfully read.
3407 * @param loader the loader.
3408 * @param buffer the buffer.
3409 * @param bytes_read number of bytes that were read into the buffer.
3412 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3416 _dbus_assert (loader->buffer_outstanding);
3417 _dbus_assert (buffer == &loader->data);
3419 loader->buffer_outstanding = FALSE;
3423 * FIXME when we move the header out of the buffer, that memmoves all
3424 * buffered messages. Kind of crappy.
3426 * Also we copy the header and body, which is kind of crappy. To
3427 * avoid this, we have to allow header and body to be in a single
3428 * memory block, which is good for messages we read and bad for
3429 * messages we are creating. But we could move_len() the buffer into
3430 * this single memory block, and move_len() will just swap the buffers
3431 * if you're moving the entire buffer replacing the dest string.
3433 * We could also have the message loader tell the transport how many
3434 * bytes to read; so it would first ask for some arbitrary number like
3435 * 256, then if the message was incomplete it would use the
3436 * header/body len to ask for exactly the size of the message (or
3437 * blocks the size of a typical kernel buffer for the socket). That
3438 * way we don't get trailing bytes in the buffer that have to be
3439 * memmoved. Though I suppose we also don't have a chance of reading a
3440 * bunch of small messages at once, so the optimization may be stupid.
3442 * Another approach would be to keep a "start" index into
3443 * loader->data and only delete it occasionally, instead of after
3444 * each message is loaded.
3446 * load_message() returns FALSE if not enough memory OR the loader was corrupted
3449 load_message (DBusMessageLoader *loader,
3450 DBusMessage *message,
3452 int fields_array_len,
3457 DBusValidity validity;
3458 const DBusString *type_str;
3460 DBusValidationMode mode;
3462 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
3467 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
3470 /* 1. VALIDATE AND COPY OVER HEADER */
3471 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
3472 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
3474 if (!_dbus_header_load (&message->header,
3482 _dbus_string_get_length (&loader->data)))
3484 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
3486 /* assert here so we can catch any code that still uses DBUS_VALID to indicate
3487 oom errors. They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
3488 _dbus_assert (validity != DBUS_VALID);
3490 if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
3494 loader->corrupted = TRUE;
3495 loader->corruption_reason = validity;
3500 _dbus_assert (validity == DBUS_VALID);
3502 message->byte_order = byte_order;
3504 /* 2. VALIDATE BODY */
3505 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
3507 get_const_signature (&message->header, &type_str, &type_pos);
3509 /* Because the bytes_remaining arg is NULL, this validates that the
3510 * body is the right length
3512 validity = _dbus_validate_body_with_reason (type_str,
3519 if (validity != DBUS_VALID)
3521 _dbus_verbose ("Failed to validate message body code %d\n", validity);
3523 loader->corrupted = TRUE;
3524 loader->corruption_reason = validity;
3530 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
3532 if (!_dbus_list_append (&loader->messages, message))
3534 _dbus_verbose ("Failed to append new message to loader queue\n");
3539 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3540 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3541 (header_len + body_len));
3543 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
3545 _dbus_verbose ("Failed to move body into new message\n");
3550 _dbus_string_delete (&loader->data, 0, header_len + body_len);
3552 /* don't waste more than 2k of memory */
3553 _dbus_string_compact (&loader->data, 2048);
3555 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
3556 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
3558 _dbus_verbose ("Loaded message %p\n", message);
3560 _dbus_assert (!oom);
3561 _dbus_assert (!loader->corrupted);
3562 _dbus_assert (loader->messages != NULL);
3563 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3571 /* does nothing if the message isn't in the list */
3572 _dbus_list_remove_last (&loader->messages, message);
3575 _dbus_assert (!loader->corrupted);
3577 _dbus_assert (loader->corrupted);
3579 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
3585 * Converts buffered data into messages, if we have enough data. If
3586 * we don't have enough data, does nothing.
3588 * @todo we need to check that the proper named header fields exist
3589 * for each message type.
3591 * @todo If a message has unknown type, we should probably eat it
3592 * right here rather than passing it out to applications. However
3593 * it's not an error to see messages of unknown type.
3595 * @param loader the loader.
3596 * @returns #TRUE if we had enough memory to finish.
3599 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3601 while (!loader->corrupted &&
3602 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
3604 DBusValidity validity;
3605 int byte_order, fields_array_len, header_len, body_len;
3607 if (_dbus_header_have_message_untrusted (loader->max_message_size,
3614 _dbus_string_get_length (&loader->data)))
3616 DBusMessage *message;
3618 _dbus_assert (validity == DBUS_VALID);
3620 message = dbus_message_new_empty_header ();
3621 if (message == NULL)
3624 if (!load_message (loader, message,
3625 byte_order, fields_array_len,
3626 header_len, body_len))
3628 dbus_message_unref (message);
3629 /* load_message() returns false if corrupted or OOM; if
3630 * corrupted then return TRUE for not OOM
3632 return loader->corrupted;
3635 _dbus_assert (loader->messages != NULL);
3636 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
3640 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
3642 if (validity != DBUS_VALID)
3644 loader->corrupted = TRUE;
3645 loader->corruption_reason = validity;
3655 * Peeks at first loaded message, returns #NULL if no messages have
3658 * @param loader the loader.
3659 * @returns the next message, or #NULL if none.
3662 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
3664 if (loader->messages)
3665 return loader->messages->data;
3671 * Pops a loaded message (passing ownership of the message
3672 * to the caller). Returns #NULL if no messages have been
3675 * @param loader the loader.
3676 * @returns the next message, or #NULL if none.
3679 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
3681 return _dbus_list_pop_first (&loader->messages);
3685 * Pops a loaded message inside a list link (passing ownership of the
3686 * message and link to the caller). Returns #NULL if no messages have
3689 * @param loader the loader.
3690 * @returns the next message link, or #NULL if none.
3693 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
3695 return _dbus_list_pop_first_link (&loader->messages);
3699 * Returns a popped message link, used to undo a pop.
3701 * @param loader the loader
3702 * @param link the link with a message in it
3705 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
3708 _dbus_list_prepend_link (&loader->messages, link);
3712 * Checks whether the loader is confused due to bad data.
3713 * If messages are received that are invalid, the
3714 * loader gets confused and gives up permanently.
3715 * This state is called "corrupted."
3717 * @param loader the loader
3718 * @returns #TRUE if the loader is hosed.
3721 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3723 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
3724 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
3725 return loader->corrupted;
3729 * Sets the maximum size message we allow.
3731 * @param loader the loader
3732 * @param size the max message size in bytes
3735 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3738 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
3740 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3741 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
3742 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
3744 loader->max_message_size = size;
3748 * Gets the maximum allowed message size in bytes.
3750 * @param loader the loader
3751 * @returns max size in bytes
3754 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3756 return loader->max_message_size;
3759 static DBusDataSlotAllocator slot_allocator;
3760 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
3763 * Allocates an integer ID to be used for storing application-specific
3764 * data on any DBusMessage. The allocated ID may then be used
3765 * with dbus_message_set_data() and dbus_message_get_data().
3766 * The passed-in slot must be initialized to -1, and is filled in
3767 * with the slot ID. If the passed-in slot is not -1, it's assumed
3768 * to be already allocated, and its refcount is incremented.
3770 * The allocated slot is global, i.e. all DBusMessage objects will
3771 * have a slot with the given integer ID reserved.
3773 * @param slot_p address of a global variable storing the slot
3774 * @returns #FALSE on failure (no memory)
3777 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
3779 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3780 &_DBUS_LOCK_NAME (message_slots),
3785 * Deallocates a global ID for message data slots.
3786 * dbus_message_get_data() and dbus_message_set_data() may no
3787 * longer be used with this slot. Existing data stored on existing
3788 * DBusMessage objects will be freed when the message is
3789 * finalized, but may not be retrieved (and may only be replaced if
3790 * someone else reallocates the slot). When the refcount on the
3791 * passed-in slot reaches 0, it is set to -1.
3793 * @param slot_p address storing the slot to deallocate
3796 dbus_message_free_data_slot (dbus_int32_t *slot_p)
3798 _dbus_return_if_fail (*slot_p >= 0);
3800 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3804 * Stores a pointer on a DBusMessage, along
3805 * with an optional function to be used for freeing
3806 * the data when the data is set again, or when
3807 * the message is finalized. The slot number
3808 * must have been allocated with dbus_message_allocate_data_slot().
3810 * @param message the message
3811 * @param slot the slot number
3812 * @param data the data to store
3813 * @param free_data_func finalizer function for the data
3814 * @returns #TRUE if there was enough memory to store the data
3817 dbus_message_set_data (DBusMessage *message,
3820 DBusFreeFunction free_data_func)
3822 DBusFreeFunction old_free_func;
3826 _dbus_return_val_if_fail (message != NULL, FALSE);
3827 _dbus_return_val_if_fail (slot >= 0, FALSE);
3829 retval = _dbus_data_slot_list_set (&slot_allocator,
3830 &message->slot_list,
3831 slot, data, free_data_func,
3832 &old_free_func, &old_data);
3836 /* Do the actual free outside the message lock */
3838 (* old_free_func) (old_data);
3845 * Retrieves data previously set with dbus_message_set_data().
3846 * The slot must still be allocated (must not have been freed).
3848 * @param message the message
3849 * @param slot the slot to get data from
3850 * @returns the data, or #NULL if not found
3853 dbus_message_get_data (DBusMessage *message,
3858 _dbus_return_val_if_fail (message != NULL, NULL);
3860 res = _dbus_data_slot_list_get (&slot_allocator,
3861 &message->slot_list,
3868 * Utility function to convert a machine-readable (not translated)
3869 * string into a D-Bus message type.
3872 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
3873 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
3874 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
3875 * "error" -> DBUS_MESSAGE_TYPE_ERROR
3876 * anything else -> DBUS_MESSAGE_TYPE_INVALID
3881 dbus_message_type_from_string (const char *type_str)
3883 if (strcmp (type_str, "method_call") == 0)
3884 return DBUS_MESSAGE_TYPE_METHOD_CALL;
3885 if (strcmp (type_str, "method_return") == 0)
3886 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
3887 else if (strcmp (type_str, "signal") == 0)
3888 return DBUS_MESSAGE_TYPE_SIGNAL;
3889 else if (strcmp (type_str, "error") == 0)
3890 return DBUS_MESSAGE_TYPE_ERROR;
3892 return DBUS_MESSAGE_TYPE_INVALID;
3896 * Utility function to convert a D-Bus message type into a
3897 * machine-readable string (not translated).
3900 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
3901 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
3902 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
3903 * DBUS_MESSAGE_TYPE_ERROR -> "error"
3904 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
3909 dbus_message_type_to_string (int type)
3913 case DBUS_MESSAGE_TYPE_METHOD_CALL:
3914 return "method_call";
3915 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
3916 return "method_return";
3917 case DBUS_MESSAGE_TYPE_SIGNAL:
3919 case DBUS_MESSAGE_TYPE_ERROR:
3927 * Turn a DBusMessage into the marshalled form as described in the D-Bus
3930 * Generally, this function is only useful for encapsulating D-Bus messages in
3931 * a different protocol.
3933 * @param msg the DBusMessage
3934 * @param marshalled_data_p the location to save the marshalled form to
3935 * @param len_p the location to save the length of the marshalled form to
3936 * @returns #FALSE if there was not enough memory
3939 dbus_message_marshal (DBusMessage *msg,
3940 char **marshalled_data_p,
3945 _dbus_return_val_if_fail (msg != NULL, FALSE);
3946 _dbus_return_val_if_fail (marshalled_data_p != NULL, FALSE);
3947 _dbus_return_val_if_fail (len_p != NULL, FALSE);
3949 if (!_dbus_string_init (&tmp))
3952 if (!_dbus_string_copy (&(msg->header.data), 0, &tmp, 0))
3955 *len_p = _dbus_string_get_length (&tmp);
3957 if (!_dbus_string_copy (&(msg->body), 0, &tmp, *len_p))
3960 *len_p = _dbus_string_get_length (&tmp);
3962 if (!_dbus_string_steal_data (&tmp, marshalled_data_p))
3965 _dbus_string_free (&tmp);
3969 _dbus_string_free (&tmp);
3974 * Demarshal a D-Bus message from the format described in the D-Bus
3977 * Generally, this function is only useful for encapsulating D-Bus messages in
3978 * a different protocol.
3980 * @param str the marshalled DBusMessage
3981 * @param len the length of str
3982 * @param error the location to save errors to
3983 * @returns #NULL if there was an error
3986 dbus_message_demarshal (const char *str,
3990 DBusMessageLoader *loader;
3994 _dbus_return_val_if_fail (str != NULL, NULL);
3996 loader = _dbus_message_loader_new ();
4001 _dbus_message_loader_get_buffer (loader, &buffer);
4002 _dbus_string_append_len (buffer, str, len);
4003 _dbus_message_loader_return_buffer (loader, buffer, len);
4005 if (!_dbus_message_loader_queue_messages (loader))
4008 if (_dbus_message_loader_get_is_corrupted (loader))
4011 msg = _dbus_message_loader_pop_message (loader);
4016 _dbus_message_loader_unref (loader);
4020 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Message is corrupted");
4021 _dbus_message_loader_unref (loader);
4025 _DBUS_SET_OOM (error);
4026 _dbus_message_loader_unref (loader);
4031 * Returns the number of bytes required to be in the buffer to demarshal a
4034 * Generally, this function is only useful for encapsulating D-Bus messages in
4035 * a different protocol.
4037 * @param str data to be marshalled
4038 * @param len the length of str
4039 * @param error the location to save errors to
4040 * @returns -1 if there was no valid data to be demarshalled, 0 if there wasn't enough data to determine how much should be demarshalled. Otherwise returns the number of bytes to be demarshalled
4044 dbus_message_demarshal_bytes_needed(const char *buf,
4048 int byte_order, fields_array_len, header_len, body_len;
4049 DBusValidity validity = DBUS_VALID;
4052 if (!buf || len < DBUS_MINIMUM_HEADER_SIZE)
4055 if (len > DBUS_MAXIMUM_MESSAGE_LENGTH)
4056 len = DBUS_MAXIMUM_MESSAGE_LENGTH;
4057 _dbus_string_init_const_len (&str, buf, len);
4059 validity = DBUS_VALID;
4061 = _dbus_header_have_message_untrusted(DBUS_MAXIMUM_MESSAGE_LENGTH,
4062 &validity, &byte_order,
4068 _dbus_string_free (&str);
4070 if (validity == DBUS_VALID)
4072 _dbus_assert(have_message);
4073 return header_len + body_len;
4077 return -1; /* broken! */
4083 /* tests in dbus-message-util.c */