1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
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.h"
27 #include "dbus-message.h"
28 #include "dbus-message-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-list.h"
31 #include "dbus-message-builder.h"
35 * @defgroup DBusMessageInternals DBusMessage implementation details
36 * @ingroup DBusInternals
37 * @brief DBusMessage private implementation details.
39 * The guts of DBusMessage and its methods.
57 static dbus_bool_t field_is_named[FIELD_LAST] =
59 FALSE, /* FIELD_HEADER_LENGTH */
60 FALSE, /* FIELD_BODY_LENGTH */
61 FALSE, /* FIELD_CLIENT_SERIAL */
62 TRUE, /* FIELD_NAME */
63 TRUE, /* FIELD_SERVICE */
64 TRUE, /* FIELD_SENDER */
65 TRUE /* FIELD_REPLY_SERIAL */
70 int offset; /**< Offset to start of field (location of name of field
76 * @brief Internals of DBusMessage
78 * Object representing a message received from or to be sent to
79 * another application. This is an opaque object, all members
84 dbus_atomic_t refcount; /**< Reference count */
86 DBusString header; /**< Header network data, stored
87 * separately from body so we can
88 * independently realloc it.
91 HeaderField header_fields[FIELD_LAST]; /**< Track the location
92 * of each field in "header"
95 dbus_int32_t client_serial; /**< Cached client serial value for speed */
96 dbus_int32_t reply_serial; /**< Cached reply serial value for speed */
98 int header_padding; /**< bytes of alignment in header */
100 DBusString body; /**< Body network data. */
102 char byte_order; /**< Message byte order. */
104 DBusCounter *size_counter; /**< Counter for the size of the message, or #NULL */
105 long size_counter_delta; /**< Size we incremented the size counter by. */
107 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
111 * @brief Internals of DBusMessageIter
113 * Object representing a position in a message. All fields are internal.
115 struct DBusMessageIter
117 int refcount; /**< Reference count */
119 int pos; /**< Current position in the string */
121 DBusMessage *message; /**< Message used */
125 * Gets the data to be sent over the network for this message.
126 * The header and then the body should be written out.
127 * This function is guaranteed to always return the same
128 * data once a message is locked (with _dbus_message_lock()).
130 * @param message the message.
131 * @param header return location for message header data.
132 * @param body return location for message body data.
135 _dbus_message_get_network_data (DBusMessage *message,
136 const DBusString **header,
137 const DBusString **body)
139 _dbus_assert (message->locked);
141 *header = &message->header;
142 *body = &message->body;
146 clear_header_padding (DBusMessage *message)
148 _dbus_string_shorten (&message->header,
149 message->header_padding);
150 message->header_padding = 0;
154 append_header_padding (DBusMessage *message)
157 old_len = _dbus_string_get_length (&message->header);
158 if (!_dbus_string_align_length (&message->header, 8))
161 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
167 adjust_field_offsets (DBusMessage *message,
177 while (i < FIELD_LAST)
179 if (message->header_fields[i].offset > offsets_after)
180 message->header_fields[i].offset += delta;
187 get_string_field (DBusMessage *message,
194 offset = message->header_fields[field].offset;
196 _dbus_assert (field < FIELD_LAST);
201 /* offset points to string length, string data follows it */
202 /* FIXME _dbus_demarshal_const_string() that returned
203 * a reference to the string plus its len might be nice.
207 *len = _dbus_demarshal_uint32 (&message->header,
212 _dbus_string_get_const_data (&message->header,
215 return data + (offset + 4);
219 get_int_field (DBusMessage *message,
224 _dbus_assert (field < FIELD_LAST);
226 offset = message->header_fields[field].offset;
229 return -1; /* useless if -1 is a valid value of course */
231 return _dbus_demarshal_int32 (&message->header,
238 append_int_field (DBusMessage *message,
245 _dbus_assert (!message->locked);
247 clear_header_padding (message);
249 orig_len = _dbus_string_get_length (&message->header);
251 if (!_dbus_string_align_length (&message->header, 4))
254 if (!_dbus_string_append_len (&message->header, name, 4))
257 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
260 if (!_dbus_string_align_length (&message->header, 4))
263 message->header_fields[FIELD_REPLY_SERIAL].offset =
264 _dbus_string_get_length (&message->header);
266 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
270 if (!append_header_padding (message))
276 message->header_fields[field].offset = -1;
277 _dbus_string_set_length (&message->header, orig_len);
279 /* this must succeed because it was allocated on function entry and
280 * DBusString doesn't ever realloc smaller
282 if (!append_header_padding (message))
283 _dbus_assert_not_reached ("failed to reappend header padding");
288 append_string_field (DBusMessage *message,
295 _dbus_assert (!message->locked);
297 clear_header_padding (message);
299 orig_len = _dbus_string_get_length (&message->header);
301 if (!_dbus_string_align_length (&message->header, 4))
304 if (!_dbus_string_append_len (&message->header, name, 4))
307 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
310 if (!_dbus_string_align_length (&message->header, 4))
313 message->header_fields[field].offset =
314 _dbus_string_get_length (&message->header);
316 if (!_dbus_marshal_string (&message->header, message->byte_order,
320 if (!append_header_padding (message))
326 message->header_fields[field].offset = -1;
327 _dbus_string_set_length (&message->header, orig_len);
329 /* this must succeed because it was allocated on function entry and
330 * DBusString doesn't ever realloc smaller
332 if (!append_header_padding (message))
333 _dbus_assert_not_reached ("failed to reappend header padding");
339 delete_int_field (DBusMessage *message,
342 int offset = message->header_fields[field].offset;
344 _dbus_assert (!message->locked);
345 _dbus_assert (field_is_named[field]);
350 clear_header_padding (message);
352 /* The field typecode and name take up 8 bytes */
353 _dbus_string_delete (&message->header,
357 message->header_fields[field].offset = -1;
359 adjust_field_offsets (message,
363 append_header_padding (message);
367 delete_string_field (DBusMessage *message,
370 int offset = message->header_fields[field].offset;
374 _dbus_assert (!message->locked);
375 _dbus_assert (field_is_named[field]);
380 clear_header_padding (message);
382 get_string_field (message, field, &len);
384 /* The field typecode and name take up 8 bytes, and the nul
385 * termination is 1 bytes, string length integer is 4 bytes
387 delete_len = 8 + 4 + 1 + len;
389 _dbus_string_delete (&message->header,
393 message->header_fields[field].offset = -1;
395 adjust_field_offsets (message,
399 append_header_padding (message);
403 set_int_field (DBusMessage *message,
407 int offset = message->header_fields[field].offset;
409 _dbus_assert (!message->locked);
413 /* need to append the field */
417 case FIELD_REPLY_SERIAL:
418 return append_int_field (message, field,
419 DBUS_HEADER_FIELD_REPLY,
422 _dbus_assert_not_reached ("appending an int field we don't support appending");
428 _dbus_marshal_set_int32 (&message->header,
437 set_uint_field (DBusMessage *message,
441 int offset = message->header_fields[field].offset;
443 _dbus_assert (!message->locked);
447 /* need to append the field */
452 _dbus_assert_not_reached ("appending a uint field we don't support appending");
458 _dbus_marshal_set_uint32 (&message->header,
467 set_string_field (DBusMessage *message,
471 int offset = message->header_fields[field].offset;
473 _dbus_assert (!message->locked);
474 _dbus_assert (value != NULL);
478 /* need to append the field */
483 return append_string_field (message, field,
484 DBUS_HEADER_FIELD_SENDER,
487 _dbus_assert_not_reached ("appending a string field we don't support appending");
498 clear_header_padding (message);
500 old_len = _dbus_string_get_length (&message->header);
502 len = strlen (value);
504 _dbus_string_init_const_len (&v, value,
505 len + 1); /* include nul */
506 if (!_dbus_marshal_set_string (&message->header,
512 new_len = _dbus_string_get_length (&message->header);
514 adjust_field_offsets (message,
518 if (!append_header_padding (message))
524 /* this must succeed because it was allocated on function entry and
525 * DBusString doesn't ever realloc smaller
527 if (!append_header_padding (message))
528 _dbus_assert_not_reached ("failed to reappend header padding");
535 * Sets the serial number of a message.
536 * This can only be done once on a message.
538 * @param message the message
539 * @param serial the serial
542 _dbus_message_set_serial (DBusMessage *message,
545 _dbus_assert (!message->locked);
546 _dbus_assert (dbus_message_get_serial (message) < 0);
548 set_int_field (message, FIELD_CLIENT_SERIAL,
550 message->client_serial = serial;
554 * Sets the reply serial of a message (the client serial
555 * of the message this is a reply to).
557 * @param message the message
558 * @param reply_serial the client serial
559 * @returns #FALSE if not enough memory
562 dbus_message_set_reply_serial (DBusMessage *message,
563 dbus_int32_t reply_serial)
565 _dbus_assert (!message->locked);
567 if (set_int_field (message, FIELD_REPLY_SERIAL,
570 message->reply_serial = reply_serial;
578 * Returns the serial of a message or -1 if none has been specified.
579 * The message's serial number is provided by the application sending
580 * the message and is used to identify replies to this message.
582 * @param message the message
583 * @returns the client serial
586 dbus_message_get_serial (DBusMessage *message)
588 return message->client_serial;
592 * Returns the serial that the message is
593 * a reply to or -1 if none.
595 * @param message the message
596 * @returns the reply serial
599 dbus_message_get_reply_serial (DBusMessage *message)
601 return message->reply_serial;
605 * Adds a counter to be incremented immediately with the
606 * size of this message, and decremented by the size
607 * of this message when this message if finalized.
609 * @param message the message
610 * @param counter the counter
613 _dbus_message_add_size_counter (DBusMessage *message,
614 DBusCounter *counter)
616 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
617 * counters instead of just one
620 message->size_counter = counter;
621 _dbus_counter_ref (message->size_counter);
623 /* When we can change message size, we may want to
624 * update this each time we do so, or we may want to
625 * just KISS like this.
627 message->size_counter_delta =
628 _dbus_string_get_length (&message->header) +
629 _dbus_string_get_length (&message->body);
632 _dbus_verbose ("message has size %ld\n",
633 message->size_counter_delta);
636 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
640 dbus_message_create_header (DBusMessage *message,
646 if (!_dbus_string_append_byte (&message->header, message->byte_order))
650 if (!_dbus_string_append_byte (&message->header, flags))
653 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
656 if (!_dbus_string_append_byte (&message->header, 0))
659 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
660 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
663 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
664 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
667 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
668 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
671 /* Marshal message service */
674 if (!append_string_field (message,
676 DBUS_HEADER_FIELD_SERVICE,
681 _dbus_assert (name != NULL);
682 if (!append_string_field (message,
684 DBUS_HEADER_FIELD_NAME,
692 * Locks a message. Allows checking that applications don't keep a
693 * reference to a message in the outgoing queue and change it
694 * underneath us. Messages are locked when they enter the outgoing
695 * queue (dbus_connection_send_message()), and the library complains
696 * if the message is modified while locked.
698 * @param message the message to lock.
701 _dbus_message_lock (DBusMessage *message)
703 if (!message->locked)
705 /* Fill in our lengths */
706 set_uint_field (message,
708 _dbus_string_get_length (&message->header));
710 set_uint_field (message,
712 _dbus_string_get_length (&message->body));
714 message->locked = TRUE;
721 * @defgroup DBusMessage DBusMessage
723 * @brief Message to be sent or received over a DBusConnection.
725 * A DBusMessage is the most basic unit of communication over a
726 * DBusConnection. A DBusConnection represents a stream of messages
727 * received from a remote application, and a stream of messages
728 * sent to a remote application.
734 * @typedef DBusMessage
736 * Opaque data type representing a message received from or to be
737 * sent to another application.
741 dbus_message_new_empty_header (void)
743 DBusMessage *message;
746 message = dbus_new0 (DBusMessage, 1);
750 message->refcount = 1;
751 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
752 message->client_serial = -1;
753 message->reply_serial = -1;
756 while (i < FIELD_LAST)
758 message->header_fields[i].offset = -1;
762 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
768 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
770 _dbus_string_free (&message->header);
780 * Constructs a new message. Returns #NULL if memory can't be
781 * allocated for the message. The service may be #NULL in which case
782 * no service is set; this is appropriate when using D-BUS in a
783 * peer-to-peer context (no message bus).
785 * @todo reverse the arguments, first 'name' then 'service'
786 * as 'name' is more fundamental
788 * @param service service that the message should be sent to or #NULL
789 * @param name name of the message
790 * @returns a new DBusMessage, free with dbus_message_unref()
791 * @see dbus_message_unref()
794 dbus_message_new (const char *service,
797 DBusMessage *message;
799 message = dbus_message_new_empty_header ();
803 if (!dbus_message_create_header (message, service, name))
805 dbus_message_unref (message);
813 * Constructs a message that is a reply to some other
814 * message. Returns #NULL if memory can't be allocated
817 * @param original_message the message which the created
818 * message is a reply to.
819 * @returns a new DBusMessage, free with dbus_message_unref()
820 * @see dbus_message_new(), dbus_message_unref()
823 dbus_message_new_reply (DBusMessage *original_message)
825 DBusMessage *message;
826 const char *sender, *name;
828 sender = get_string_field (original_message,
830 name = get_string_field (original_message,
833 /* sender is allowed to be null here in peer-to-peer case */
835 message = dbus_message_new (sender, name);
840 if (!dbus_message_set_reply_serial (message,
841 dbus_message_get_serial (original_message)))
843 dbus_message_unref (message);
851 * Creates a new message that is an error reply to a certain message.
853 * @param original_message the original message
854 * @param error_name the error name
855 * @param error_message the error message string
856 * @returns a new error message
859 dbus_message_new_error_reply (DBusMessage *original_message,
860 const char *error_name,
861 const char *error_message)
863 DBusMessage *message;
866 sender = get_string_field (original_message,
869 _dbus_assert (sender != NULL);
871 message = dbus_message_new (sender, error_name);
876 if (!dbus_message_set_reply_serial (message,
877 dbus_message_get_serial (original_message)))
879 dbus_message_unref (message);
883 if (!dbus_message_append_string (message, error_message))
885 dbus_message_unref (message);
889 dbus_message_set_is_error (message, TRUE);
895 * Creates a new message that is an exact replica of the message
896 * specified, except that its refcount is set to 1.
898 * @param message the message.
899 * @returns the new message.
902 dbus_message_copy (const DBusMessage *message)
907 retval = dbus_new0 (DBusMessage, 1);
911 retval->refcount = 1;
912 retval->byte_order = message->byte_order;
913 retval->client_serial = message->client_serial;
914 retval->reply_serial = message->reply_serial;
915 retval->header_padding = message->header_padding;
916 retval->locked = FALSE;
918 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
924 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
926 _dbus_string_free (&retval->header);
931 if (!_dbus_string_copy (&message->header, 0,
934 _dbus_string_free (&retval->header);
935 _dbus_string_free (&retval->body);
941 if (!_dbus_string_copy (&message->body, 0,
944 _dbus_string_free (&retval->header);
945 _dbus_string_free (&retval->body);
951 for (i = 0; i < FIELD_LAST; i++)
953 retval->header_fields[i].offset = message->header_fields[i].offset;
961 * Increments the reference count of a DBusMessage.
963 * @param message The message
964 * @see dbus_message_unref
967 dbus_message_ref (DBusMessage *message)
969 dbus_atomic_t refcount;
971 refcount = _dbus_atomic_inc (&message->refcount);
972 _dbus_assert (refcount > 1);
976 * Decrements the reference count of a DBusMessage.
978 * @param message The message
979 * @see dbus_message_ref
982 dbus_message_unref (DBusMessage *message)
984 dbus_atomic_t refcount;
986 refcount = _dbus_atomic_dec (&message->refcount);
988 _dbus_assert (refcount >= 0);
992 if (message->size_counter != NULL)
994 _dbus_counter_adjust (message->size_counter,
995 - message->size_counter_delta);
996 _dbus_counter_unref (message->size_counter);
999 _dbus_string_free (&message->header);
1000 _dbus_string_free (&message->body);
1002 dbus_free (message);
1007 * Gets the name of a message.
1009 * @param message the message
1010 * @returns the message name (should not be freed)
1013 dbus_message_get_name (DBusMessage *message)
1015 return get_string_field (message, FIELD_NAME, NULL);
1019 * Gets the destination service of a message.
1021 * @todo I think if we have set_sender/get_sender,
1022 * this function might be better named set_destination/
1023 * get_destination for clarity, as the sender
1024 * is also a service name.
1026 * @param message the message
1027 * @returns the message destination service (should not be freed)
1030 dbus_message_get_service (DBusMessage *message)
1032 return get_string_field (message, FIELD_SERVICE, NULL);
1036 * Appends fields to a message given a variable argument
1037 * list. The variable argument list should contain the type
1038 * of the argument followed by the value to add. Array values
1039 * are specified by a pointer to the array followed by an int
1040 * giving the length of the array. The list is terminated
1043 * @param message the message
1044 * @param first_arg_type type of the first argument
1045 * @param ... value of first argument, list of additional type-value pairs
1046 * @returns #TRUE on success
1049 dbus_message_append_args (DBusMessage *message,
1056 va_start (var_args, first_arg_type);
1057 retval = dbus_message_append_args_valist (message,
1066 * This function takes a va_list for use by language bindings
1068 * @see dbus_message_append_args.
1069 * @param message the message
1070 * @param first_arg_type type of first argument
1071 * @param var_args value of first argument, then list of type/value pairs
1072 * @returns #TRUE on success
1075 dbus_message_append_args_valist (DBusMessage *message,
1081 old_len = _dbus_string_get_length (&message->body);
1083 type = first_arg_type;
1090 if (!dbus_message_append_nil (message))
1092 case DBUS_TYPE_BOOLEAN:
1093 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1096 case DBUS_TYPE_INT32:
1097 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1100 case DBUS_TYPE_UINT32:
1101 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1104 case DBUS_TYPE_DOUBLE:
1105 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1108 case DBUS_TYPE_STRING:
1109 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1112 case DBUS_TYPE_BOOLEAN_ARRAY:
1115 unsigned char *data;
1117 data = va_arg (var_args, unsigned char *);
1118 len = va_arg (var_args, int);
1120 if (!dbus_message_append_boolean_array (message, data, len))
1124 case DBUS_TYPE_INT32_ARRAY:
1129 data = va_arg (var_args, dbus_int32_t *);
1130 len = va_arg (var_args, int);
1132 if (!dbus_message_append_int32_array (message, data, len))
1136 case DBUS_TYPE_UINT32_ARRAY:
1139 dbus_uint32_t *data;
1141 data = va_arg (var_args, dbus_uint32_t *);
1142 len = va_arg (var_args, int);
1144 if (!dbus_message_append_uint32_array (message, data, len))
1148 case DBUS_TYPE_DOUBLE_ARRAY:
1153 data = va_arg (var_args, double *);
1154 len = va_arg (var_args, int);
1156 if (!dbus_message_append_double_array (message, data, len))
1160 case DBUS_TYPE_BYTE_ARRAY:
1163 unsigned char *data;
1165 data = va_arg (var_args, unsigned char *);
1166 len = va_arg (var_args, int);
1168 if (!dbus_message_append_byte_array (message, data, len))
1172 case DBUS_TYPE_STRING_ARRAY:
1177 data = va_arg (var_args, const char **);
1178 len = va_arg (var_args, int);
1180 if (!dbus_message_append_string_array (message, data, len))
1184 case DBUS_TYPE_DICT:
1188 dict = va_arg (var_args, DBusDict *);
1190 if (!dbus_message_append_dict (message, dict))
1195 _dbus_warn ("Unknown field type %d\n", type);
1198 type = va_arg (var_args, int);
1208 * Appends a nil value to the message
1210 * @param message the message
1211 * @returns #TRUE on success
1214 dbus_message_append_nil (DBusMessage *message)
1216 _dbus_assert (!message->locked);
1218 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1225 * Appends a boolean value to the message
1227 * @param message the message
1228 * @param value the boolean value
1229 * @returns #TRUE on success
1232 dbus_message_append_boolean (DBusMessage *message,
1235 _dbus_assert (!message->locked);
1237 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1240 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1242 _dbus_string_shorten (&message->body, 1);
1250 * Appends a 32 bit signed integer to the message.
1252 * @param message the message
1253 * @param value the integer value
1254 * @returns #TRUE on success
1257 dbus_message_append_int32 (DBusMessage *message,
1260 _dbus_assert (!message->locked);
1262 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1265 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1267 _dbus_string_shorten (&message->body, 1);
1275 * Appends a 32 bit unsigned integer to the message.
1277 * @param message the message
1278 * @param value the integer value
1279 * @returns #TRUE on success
1282 dbus_message_append_uint32 (DBusMessage *message,
1283 dbus_uint32_t value)
1285 _dbus_assert (!message->locked);
1287 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1290 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1292 _dbus_string_shorten (&message->body, 1);
1300 * Appends a double value to the message.
1302 * @param message the message
1303 * @param value the double value
1304 * @returns #TRUE on success
1307 dbus_message_append_double (DBusMessage *message,
1310 _dbus_assert (!message->locked);
1312 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1315 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1317 _dbus_string_shorten (&message->body, 1);
1325 * Appends a UTF-8 string to the message.
1327 * @param message the message
1328 * @param value the string
1329 * @returns #TRUE on success
1332 dbus_message_append_string (DBusMessage *message,
1335 _dbus_assert (!message->locked);
1337 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1340 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1342 _dbus_string_shorten (&message->body, 1);
1350 * Appends a boolean array to the message.
1352 * @param message the message
1353 * @param value the array
1354 * @param len the length of the array
1355 * @returns #TRUE on success
1358 dbus_message_append_boolean_array (DBusMessage *message,
1359 unsigned const char *value,
1362 _dbus_assert (!message->locked);
1364 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1367 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1369 _dbus_string_shorten (&message->body, 1);
1377 * Appends a 32 bit signed integer array to the message.
1379 * @param message the message
1380 * @param value the array
1381 * @param len the length of the array
1382 * @returns #TRUE on success
1385 dbus_message_append_int32_array (DBusMessage *message,
1386 const dbus_int32_t *value,
1389 _dbus_assert (!message->locked);
1391 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1394 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1397 _dbus_string_shorten (&message->body, 1);
1405 * Appends a 32 bit unsigned integer array to the message.
1407 * @param message the message
1408 * @param value the array
1409 * @param len the length of the array
1410 * @returns #TRUE on success
1413 dbus_message_append_uint32_array (DBusMessage *message,
1414 const dbus_uint32_t *value,
1417 _dbus_assert (!message->locked);
1419 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1422 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1425 _dbus_string_shorten (&message->body, 1);
1433 * Appends a double array to the message.
1435 * @param message the message
1436 * @param value the array
1437 * @param len the length of the array
1438 * @returns #TRUE on success
1441 dbus_message_append_double_array (DBusMessage *message,
1442 const double *value,
1445 _dbus_assert (!message->locked);
1447 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1450 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1453 _dbus_string_shorten (&message->body, 1);
1461 * Appends a byte array to the message.
1463 * @param message the message
1464 * @param value the array
1465 * @param len the length of the array
1466 * @returns #TRUE on success
1469 dbus_message_append_byte_array (DBusMessage *message,
1470 unsigned const char *value,
1473 _dbus_assert (!message->locked);
1475 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1478 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1480 _dbus_string_shorten (&message->body, 1);
1488 * Appends a string array to the message.
1490 * @param message the message
1491 * @param value the array
1492 * @param len the length of the array
1493 * @returns #TRUE on success
1496 dbus_message_append_string_array (DBusMessage *message,
1500 _dbus_assert (!message->locked);
1502 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1505 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1506 (const char **)value, len))
1508 _dbus_string_shorten (&message->body, 1);
1516 * Appends a dict to the message.
1518 * @param message the message
1519 * @param dict the dict
1520 * @returns #TRUE on success
1523 dbus_message_append_dict (DBusMessage *message,
1526 _dbus_assert (!message->locked);
1528 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DICT))
1531 if (!_dbus_marshal_dict (&message->body, message->byte_order, dict))
1533 _dbus_string_shorten (&message->body, 1);
1541 * Gets arguments from a message given a variable argument list.
1542 * The variable argument list should contain the type of the
1543 * argumen followed by a pointer to where the value should be
1544 * stored. The list is terminated with 0.
1546 * @param message the message
1547 * @param error error to be filled in on failure
1548 * @param first_arg_type the first argument type
1549 * @param ... location for first argument value, then list of type-location pairs
1550 * @returns #FALSE if the error was set
1553 dbus_message_get_args (DBusMessage *message,
1558 DBusResultCode retval;
1561 va_start (var_args, first_arg_type);
1562 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1569 * This function takes a va_list for use by language bindings
1571 * @todo this function (or some lower-level non-convenience function)
1572 * needs better error handling; should allow the application to
1573 * distinguish between out of memory, and bad data from the remote
1574 * app. It also needs to not leak a bunch of args when it gets
1575 * to the arg that's bad, as that would be a security hole
1576 * (allow one app to force another to leak memory)
1578 * @todo We need to free the argument data when an error occurs.
1580 * @see dbus_message_get_args
1581 * @param message the message
1582 * @param error error to be filled in
1583 * @param first_arg_type type of the first argument
1584 * @param var_args return location for first argument, followed by list of type/location pairs
1585 * @returns #FALSE if error was set
1588 dbus_message_get_args_valist (DBusMessage *message,
1593 int spec_type, msg_type, i;
1594 DBusMessageIter *iter;
1597 iter = dbus_message_get_args_iter (message);
1601 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1602 "No memory to get message arguments");
1608 spec_type = first_arg_type;
1611 while (spec_type != 0)
1613 msg_type = dbus_message_iter_get_arg_type (iter);
1615 if (msg_type != spec_type)
1617 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1618 "Argument %d is specified to be of type \"%s\", but "
1619 "is actually of type \"%s\"\n", i,
1620 _dbus_type_to_string (spec_type),
1621 _dbus_type_to_string (msg_type));
1630 case DBUS_TYPE_BOOLEAN:
1634 ptr = va_arg (var_args, dbus_bool_t *);
1636 *ptr = dbus_message_iter_get_boolean (iter);
1639 case DBUS_TYPE_INT32:
1643 ptr = va_arg (var_args, dbus_int32_t *);
1645 *ptr = dbus_message_iter_get_int32 (iter);
1648 case DBUS_TYPE_UINT32:
1652 ptr = va_arg (var_args, dbus_uint32_t *);
1654 *ptr = dbus_message_iter_get_uint32 (iter);
1658 case DBUS_TYPE_DOUBLE:
1662 ptr = va_arg (var_args, double *);
1664 *ptr = dbus_message_iter_get_double (iter);
1668 case DBUS_TYPE_STRING:
1672 ptr = va_arg (var_args, char **);
1674 *ptr = dbus_message_iter_get_string (iter);
1678 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1679 "No memory for argument %d", i);
1686 case DBUS_TYPE_BOOLEAN_ARRAY:
1688 unsigned char **ptr;
1691 ptr = va_arg (var_args, unsigned char **);
1692 len = va_arg (var_args, int *);
1694 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1696 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1697 "No memory for argument %d", i);
1703 case DBUS_TYPE_INT32_ARRAY:
1708 ptr = va_arg (var_args, dbus_int32_t **);
1709 len = va_arg (var_args, int *);
1711 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1713 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1714 "No memory for argument %d", i);
1721 case DBUS_TYPE_UINT32_ARRAY:
1723 dbus_uint32_t **ptr;
1726 ptr = va_arg (var_args, dbus_uint32_t **);
1727 len = va_arg (var_args, int *);
1729 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1731 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1732 "No memory for argument %d", i);
1739 case DBUS_TYPE_DOUBLE_ARRAY:
1744 ptr = va_arg (var_args, double **);
1745 len = va_arg (var_args, int *);
1747 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1749 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1750 "No memory for argument %d", i);
1756 case DBUS_TYPE_BYTE_ARRAY:
1758 unsigned char **ptr;
1761 ptr = va_arg (var_args, unsigned char **);
1762 len = va_arg (var_args, int *);
1764 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1766 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1767 "No memory for argument %d", i);
1772 case DBUS_TYPE_STRING_ARRAY:
1777 ptr = va_arg (var_args, char ***);
1778 len = va_arg (var_args, int *);
1780 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1782 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1783 "No memory for argument %d", i);
1788 case DBUS_TYPE_DICT:
1792 dict = va_arg (var_args, DBusDict **);
1794 if (!dbus_message_iter_get_dict (iter, dict))
1796 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1797 "No memory for argument %d", i);
1803 _dbus_warn ("Unknown field type %d\n", spec_type);
1806 spec_type = va_arg (var_args, int);
1807 if (spec_type != 0 && !dbus_message_iter_next (iter))
1809 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1810 "Message has only %d arguments, but more were expected", i);
1820 dbus_message_iter_unref (iter);
1825 * Returns a DBusMessageIter representing the arguments of the
1826 * message passed in.
1828 * @todo IMO the message iter should follow the GtkTextIter pattern,
1829 * a static object with a "stamp" value used to detect invalid
1830 * iter uses (uninitialized or after changing the message).
1831 * ref/unref is kind of annoying to deal with, and slower too.
1832 * This implies not ref'ing the message from the iter.
1834 * @todo I'd also name this dbus_message_iter_new() or
1835 * for the static object dbus_message_iter_init() rather
1836 * than making it a method on the message
1838 * @param message the message
1839 * @returns a new iter.
1842 dbus_message_get_args_iter (DBusMessage *message)
1844 DBusMessageIter *iter;
1846 iter = dbus_new (DBusMessageIter, 1);
1850 dbus_message_ref (message);
1853 iter->message = message;
1861 * Increments the reference count of a DBusMessageIter.
1863 * @param iter the message iter
1864 * @see dbus_message_iter_unref
1867 dbus_message_iter_ref (DBusMessageIter *iter)
1869 _dbus_assert (iter->refcount > 0);
1871 iter->refcount += 1;
1875 * Decrements the reference count of a DBusMessageIter.
1877 * @param iter The message iter
1878 * @see dbus_message_iter_ref
1881 dbus_message_iter_unref (DBusMessageIter *iter)
1883 _dbus_assert (iter->refcount > 0);
1885 iter->refcount -= 1;
1887 if (iter->refcount == 0)
1889 dbus_message_unref (iter->message);
1896 * Checks if an iterator has any more fields.
1898 * @param iter the message iter
1899 * @returns #TRUE if there are more fields
1903 dbus_message_iter_has_next (DBusMessageIter *iter)
1907 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1908 iter->message->byte_order,
1909 iter->pos, &end_pos))
1912 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1919 * Moves the iterator to the next field.
1921 * @param iter The message iter
1922 * @returns #TRUE if the iterator was moved to the next field
1925 dbus_message_iter_next (DBusMessageIter *iter)
1929 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1930 iter->message->byte_order,
1931 iter->pos, &end_pos))
1934 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1937 iter->pos = end_pos;
1943 * Returns the argument type of the argument that the
1944 * message iterator points at.
1946 * @param iter the message iter
1947 * @returns the field type
1950 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1954 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1955 return DBUS_TYPE_INVALID;
1957 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1959 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_DICT)
1962 return DBUS_TYPE_INVALID;
1966 * Returns the string value that an iterator may point to.
1967 * Note that you need to check that the iterator points to
1968 * a string value before using this function.
1970 * @see dbus_message_iter_get_arg_type
1971 * @param iter the message iter
1972 * @returns the string
1975 dbus_message_iter_get_string (DBusMessageIter *iter)
1977 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1979 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1980 iter->pos + 1, NULL);
1984 * Returns the boolean value that an iterator may point to.
1985 * Note that you need to check that the iterator points to
1986 * a boolean value before using this function.
1988 * @see dbus_message_iter_get_arg_type
1989 * @param iter the message iter
1990 * @returns the string
1993 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1995 unsigned char value;
1997 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1999 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
2005 * Returns the 32 bit signed integer value that an iterator may point to.
2006 * Note that you need to check that the iterator points to
2007 * an integer value before using this function.
2009 * @see dbus_message_iter_get_arg_type
2010 * @param iter the message iter
2011 * @returns the integer
2014 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2016 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
2018 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
2019 iter->pos + 1, NULL);
2023 * Returns the 32 bit unsigned integer value that an iterator may point to.
2024 * Note that you need to check that the iterator points to
2025 * an unsigned integer value before using this function.
2027 * @see dbus_message_iter_get_arg_type
2028 * @param iter the message iter
2029 * @returns the integer
2032 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2034 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
2036 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
2037 iter->pos + 1, NULL);
2041 * Returns the double value that an iterator may point to.
2042 * Note that you need to check that the iterator points to
2043 * a string value before using this function.
2045 * @see dbus_message_iter_get_arg_type
2046 * @param iter the message iter
2047 * @returns the double
2050 dbus_message_iter_get_double (DBusMessageIter *iter)
2052 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
2054 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
2055 iter->pos + 1, NULL);
2059 * Returns the boolean array that the iterator may point to. Note that
2060 * you need to check that the iterator points to an array of the
2061 * correct type prior to using this function.
2063 * @param iter the iterator
2064 * @param value return location for the array
2065 * @param len return location for the array length
2066 * @returns #TRUE on success
2069 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2070 unsigned char **value,
2073 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
2075 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2076 iter->pos + 1, NULL, value, len))
2083 * Returns the 32 bit signed integer array that the iterator may point
2084 * to. Note that you need to check that the iterator points to an
2085 * array of the correct type prior to using this function.
2087 * @param iter the iterator
2088 * @param value return location for the array
2089 * @param len return location for the array length
2090 * @returns #TRUE on success
2093 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2094 dbus_int32_t **value,
2097 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
2099 if (!_dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
2100 iter->pos + 1, NULL, value, len))
2107 * Returns the 32 bit unsigned integer array that the iterator may point
2108 * to. Note that you need to check that the iterator points to an
2109 * array of the correct type prior to using this function.
2111 * @param iter the iterator
2112 * @param value return location for the array
2113 * @param len return location for the array length
2114 * @returns #TRUE on success
2117 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2118 dbus_uint32_t **value,
2121 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2123 if (!_dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2124 iter->pos + 1, NULL, value, len))
2131 * Returns the double array that the iterator may point to. Note that
2132 * you need to check that the iterator points to an array of the
2133 * correct type prior to using this function.
2135 * @param iter the iterator
2136 * @param value return location for the array
2137 * @param len return location for the array length
2138 * @returns #TRUE on success
2141 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2145 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2147 if (!_dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2148 iter->pos + 1, NULL, value, len))
2155 * Returns the byte array that the iterator may point to.
2156 * Note that you need to check that the iterator points
2157 * to a byte array prior to using this function.
2159 * @param iter the iterator
2160 * @param value return location for array values
2161 * @param len return location for length of byte array
2162 * @returns #TRUE on success
2165 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2166 unsigned char **value,
2169 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2171 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2172 iter->pos + 1, NULL, value, len))
2179 * Returns the string array that the iterator may point to.
2180 * Note that you need to check that the iterator points
2181 * to a byte array prior to using this function.
2183 * The returned value is a #NULL-terminated array of strings.
2184 * Each string is a separate malloc block, and the array
2185 * itself is a malloc block. You can free this type of
2186 * string array with dbus_free_string_array().
2188 * @param iter the iterator
2189 * @param value return location for string values
2190 * @param len return location for length of byte array
2191 * @returns #TRUE on success
2194 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2198 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2200 if (!_dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2201 iter->pos + 1, NULL, value, len))
2208 * Returns the dict that the iterator may point to.
2209 * Note that you need to check that the iterator points
2210 * to a dict prior to using this function.
2212 * @param iter the iterator
2213 * @param dict return location for dict
2214 * @returns #TRUE on success
2217 dbus_message_iter_get_dict (DBusMessageIter *iter,
2220 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DICT);
2222 if (!_dbus_demarshal_dict (&iter->message->body, iter->message->byte_order,
2223 iter->pos + 1, NULL, dict))
2230 * Sets the message sender.
2232 * @param message the message
2233 * @param sender the sender
2234 * @returns #FALSE if not enough memory
2237 dbus_message_set_sender (DBusMessage *message,
2240 _dbus_assert (!message->locked);
2244 delete_string_field (message, FIELD_SENDER);
2249 return set_string_field (message,
2256 * Sets a flag indicating that the message is an error reply
2257 * message, i.e. an "exception" rather than a normal response.
2259 * @param message the message
2260 * @param is_error_reply #TRUE if this is an error message.
2263 dbus_message_set_is_error (DBusMessage *message,
2264 dbus_bool_t is_error_reply)
2268 _dbus_assert (!message->locked);
2270 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2273 *header |= DBUS_HEADER_FLAG_ERROR;
2275 *header &= ~DBUS_HEADER_FLAG_ERROR;
2279 * Returns #TRUE if the message is an error
2280 * reply to some previous message we sent.
2282 * @param message the message
2283 * @returns #TRUE if the message is an error
2286 dbus_message_get_is_error (DBusMessage *message)
2290 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2292 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2296 * Gets the service which originated this message,
2297 * or #NULL if unknown or inapplicable.
2299 * @param message the message
2300 * @returns the service name or #NULL
2303 dbus_message_get_sender (DBusMessage *message)
2305 return get_string_field (message, FIELD_SENDER, NULL);
2309 * Checks whether the message has the given name.
2310 * If the message has no name or has a different
2311 * name, returns #FALSE.
2313 * @param message the message
2314 * @param name the name to check (must not be #NULL)
2316 * @returns #TRUE if the message has the given name
2319 dbus_message_name_is (DBusMessage *message,
2324 _dbus_assert (name != NULL);
2326 n = dbus_message_get_name (message);
2328 if (n && strcmp (n, name) == 0)
2335 * Checks whether the message was sent to the given service. If the
2336 * message has no service specified or has a different name, returns
2339 * @param message the message
2340 * @param service the service to check (must not be #NULL)
2342 * @returns #TRUE if the message has the given destination service
2345 dbus_message_service_is (DBusMessage *message,
2346 const char *service)
2350 _dbus_assert (service != NULL);
2352 s = dbus_message_get_service (message);
2354 if (s && strcmp (s, service) == 0)
2361 * Checks whether the message has the given service as its sender. If
2362 * the message has no sender specified or has a different sender,
2363 * returns #FALSE. Note that if a peer application owns multiple
2364 * services, its messages will have only one of those services as the
2365 * sender (usually the base service). So you can't use this
2366 * function to prove the sender didn't own service Foo, you can
2367 * only use it to prove that it did.
2369 * @param message the message
2370 * @param service the service to check (must not be #NULL)
2372 * @returns #TRUE if the message has the given origin service
2375 dbus_message_sender_is (DBusMessage *message,
2376 const char *service)
2380 _dbus_assert (service != NULL);
2382 s = dbus_message_get_sender (message);
2384 if (s && strcmp (s, service) == 0)
2393 * @addtogroup DBusMessageInternals
2398 * @typedef DBusMessageLoader
2400 * The DBusMessageLoader object encapsulates the process of converting
2401 * a byte stream into a series of DBusMessage. It buffers the incoming
2402 * bytes as efficiently as possible, and generates a queue of
2403 * messages. DBusMessageLoader is typically used as part of a
2404 * DBusTransport implementation. The DBusTransport then hands off
2405 * the loaded messages to a DBusConnection, making the messages
2406 * visible to the application.
2408 * @todo write tests for break-loader that a) randomly delete header
2409 * fields and b) set string fields to zero-length and other funky
2414 /* we definitely use signed ints for sizes, so don't exceed
2415 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2416 * over 128M is pretty nuts anyhow.
2420 * The maximum sane message size.
2422 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2425 * Implementation details of DBusMessageLoader.
2426 * All members are private.
2428 struct DBusMessageLoader
2430 int refcount; /**< Reference count. */
2432 DBusString data; /**< Buffered data */
2434 DBusList *messages; /**< Complete messages. */
2436 long max_message_size; /**< Maximum size of a message */
2438 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2440 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2444 * The initial buffer size of the message loader.
2446 * @todo this should be based on min header size plus some average
2447 * body size, or something. Or rather, the min header size only, if we
2448 * want to try to read only the header, store that in a DBusMessage,
2449 * then read only the body and store that, etc., depends on
2450 * how we optimize _dbus_message_loader_get_buffer() and what
2451 * the exact message format is.
2453 #define INITIAL_LOADER_DATA_LEN 32
2456 * Creates a new message loader. Returns #NULL if memory can't
2459 * @returns new loader, or #NULL.
2462 _dbus_message_loader_new (void)
2464 DBusMessageLoader *loader;
2466 loader = dbus_new0 (DBusMessageLoader, 1);
2470 loader->refcount = 1;
2472 /* Try to cap message size at something that won't *totally* hose
2473 * the system if we have a couple of them.
2475 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2477 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2483 /* preallocate the buffer for speed, ignore failure */
2484 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2485 _dbus_string_set_length (&loader->data, 0);
2491 * Increments the reference count of the loader.
2493 * @param loader the loader.
2496 _dbus_message_loader_ref (DBusMessageLoader *loader)
2498 loader->refcount += 1;
2502 * Decrements the reference count of the loader and finalizes the
2503 * loader when the count reaches zero.
2505 * @param loader the loader.
2508 _dbus_message_loader_unref (DBusMessageLoader *loader)
2510 loader->refcount -= 1;
2511 if (loader->refcount == 0)
2513 _dbus_list_foreach (&loader->messages,
2514 (DBusForeachFunction) dbus_message_unref,
2516 _dbus_list_clear (&loader->messages);
2517 _dbus_string_free (&loader->data);
2523 * Gets the buffer to use for reading data from the network. Network
2524 * data is read directly into an allocated buffer, which is then used
2525 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2526 * The buffer must always be returned immediately using
2527 * _dbus_message_loader_return_buffer(), even if no bytes are
2528 * successfully read.
2530 * @todo this function can be a lot more clever. For example
2531 * it can probably always return a buffer size to read exactly
2532 * the body of the next message, thus avoiding any memory wastage
2535 * @todo we need to enforce a max length on strings in header fields.
2537 * @param loader the message loader.
2538 * @param buffer the buffer
2541 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2542 DBusString **buffer)
2544 _dbus_assert (!loader->buffer_outstanding);
2546 *buffer = &loader->data;
2548 loader->buffer_outstanding = TRUE;
2552 * The smallest header size that can occur.
2553 * (It won't be valid)
2555 #define DBUS_MINIMUM_HEADER_SIZE 16
2557 /** Pack four characters as in "abcd" into a uint32 */
2558 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2559 ((((dbus_uint32_t)a) << 24) | \
2560 (((dbus_uint32_t)b) << 16) | \
2561 (((dbus_uint32_t)c) << 8) | \
2564 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2565 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2566 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2568 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2569 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2570 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2572 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2573 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2574 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2576 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2577 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2578 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2580 /* FIXME impose max length on name, srvc, sndr */
2582 decode_header_data (const DBusString *data,
2585 HeaderField fields[FIELD_LAST],
2586 int *message_padding)
2592 if (header_len < 16)
2596 while (i < FIELD_LAST)
2598 fields[i].offset = -1;
2602 fields[FIELD_HEADER_LENGTH].offset = 4;
2603 fields[FIELD_BODY_LENGTH].offset = 8;
2604 fields[FIELD_CLIENT_SERIAL].offset = 12;
2606 /* Now handle the named fields. A real named field is at least 4
2607 * bytes for the name, plus a type code (1 byte) plus padding. So
2608 * if we have less than 8 bytes left, it must be alignment padding,
2609 * not a field. While >= 8 bytes can't be entirely alignment
2613 while ((pos + 7) < header_len)
2615 pos = _DBUS_ALIGN_VALUE (pos, 4);
2617 if ((pos + 4) > header_len)
2620 _dbus_string_get_const_data_len (data, &field, pos, 4);
2623 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2625 /* I believe FROM_BE is right, but if not we'll find out
2628 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2630 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2631 if (fields[FIELD_SERVICE].offset >= 0)
2633 _dbus_verbose ("%s field provided twice\n",
2634 DBUS_HEADER_FIELD_SERVICE);
2638 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2640 _dbus_verbose ("Found service name at offset %d\n",
2641 fields[FIELD_SERVICE].offset);
2645 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2646 if (fields[FIELD_NAME].offset >= 0)
2648 _dbus_verbose ("%s field provided twice\n",
2649 DBUS_HEADER_FIELD_NAME);
2653 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2656 _dbus_verbose ("Found message name at offset %d\n",
2657 fields[FIELD_NAME].offset);
2660 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2661 if (fields[FIELD_SENDER].offset >= 0)
2663 _dbus_verbose ("%s field provided twice\n",
2664 DBUS_HEADER_FIELD_SENDER);
2668 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2670 _dbus_verbose ("Found sender name at offset %d\n",
2671 fields[FIELD_NAME].offset);
2674 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2675 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2677 _dbus_verbose ("%s field provided twice\n",
2678 DBUS_HEADER_FIELD_REPLY);
2682 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2684 _dbus_verbose ("Found reply serial at offset %d\n",
2685 fields[FIELD_REPLY_SERIAL].offset);
2689 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2690 field[0], field[1], field[2], field[3], pos);
2693 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2695 _dbus_verbose ("Failed to validate argument to named header field\n");
2699 if (new_pos > header_len)
2701 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2708 if (pos < header_len)
2710 /* Alignment padding, verify that it's nul */
2711 _dbus_assert ((header_len - pos) < 8);
2713 if (!_dbus_string_validate_nul (data,
2714 pos, (header_len - pos)))
2716 _dbus_verbose ("header alignment padding is not nul\n");
2721 if (fields[FIELD_NAME].offset < 0)
2723 _dbus_verbose ("No %s field provided\n",
2724 DBUS_HEADER_FIELD_NAME);
2728 if (message_padding)
2729 *message_padding = header_len - pos;
2735 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2736 * indicating to the loader how many bytes of the buffer were filled
2737 * in. This function must always be called, even if no bytes were
2738 * successfully read.
2740 * @param loader the loader.
2741 * @param buffer the buffer.
2742 * @param bytes_read number of bytes that were read into the buffer.
2745 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2749 _dbus_assert (loader->buffer_outstanding);
2750 _dbus_assert (buffer == &loader->data);
2752 loader->buffer_outstanding = FALSE;
2756 * Converts buffered data into messages.
2758 * @param loader the loader.
2759 * @returns #TRUE if we had enough memory to finish.
2762 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
2764 if (loader->corrupted)
2767 while (_dbus_string_get_length (&loader->data) >= 16)
2769 DBusMessage *message;
2770 const char *header_data;
2771 int byte_order, header_len, body_len, header_padding;
2772 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2774 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2776 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2778 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2780 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2781 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2782 loader->corrupted = TRUE;
2786 byte_order = header_data[0];
2788 if (byte_order != DBUS_LITTLE_ENDIAN &&
2789 byte_order != DBUS_BIG_ENDIAN)
2791 _dbus_verbose ("Message with bad byte order '%c' received\n",
2793 loader->corrupted = TRUE;
2797 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2798 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2800 if (header_len_unsigned < 16)
2802 _dbus_verbose ("Message had broken too-small header length %u\n",
2803 header_len_unsigned);
2804 loader->corrupted = TRUE;
2808 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2809 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2811 _dbus_verbose ("Header or body length too large (%u %u)\n",
2812 header_len_unsigned,
2814 loader->corrupted = TRUE;
2818 /* Now that we know the values are in signed range, get
2819 * rid of stupid unsigned, just causes bugs
2821 header_len = header_len_unsigned;
2822 body_len = body_len_unsigned;
2824 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2827 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2829 loader->corrupted = TRUE;
2833 if (header_len + body_len > loader->max_message_size)
2835 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
2836 header_len, body_len, loader->max_message_size);
2837 loader->corrupted = TRUE;
2841 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2843 HeaderField fields[FIELD_LAST];
2848 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
2850 if (!decode_header_data (&loader->data, header_len, byte_order,
2851 fields, &header_padding))
2853 _dbus_verbose ("Header was invalid\n");
2854 loader->corrupted = TRUE;
2858 next_arg = header_len;
2859 while (next_arg < (header_len + body_len))
2861 int prev = next_arg;
2863 if (!_dbus_marshal_validate_arg (&loader->data,
2868 loader->corrupted = TRUE;
2872 _dbus_assert (next_arg > prev);
2875 if (next_arg > (header_len + body_len))
2877 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2878 next_arg, header_len, body_len,
2879 header_len + body_len);
2880 loader->corrupted = TRUE;
2884 message = dbus_message_new_empty_header ();
2885 if (message == NULL)
2888 message->byte_order = byte_order;
2889 message->header_padding = header_padding;
2891 /* Copy in the offsets we found */
2893 while (i < FIELD_LAST)
2895 message->header_fields[i] = fields[i];
2899 if (!_dbus_list_append (&loader->messages, message))
2901 dbus_message_unref (message);
2905 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2906 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2908 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2909 (header_len + body_len));
2911 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2913 _dbus_list_remove_last (&loader->messages, message);
2914 dbus_message_unref (message);
2918 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2922 /* put the header back, we'll try again later */
2923 result = _dbus_string_copy_len (&message->header, 0, header_len,
2925 _dbus_assert (result); /* because DBusString never reallocs smaller */
2927 _dbus_list_remove_last (&loader->messages, message);
2928 dbus_message_unref (message);
2932 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2933 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2935 /* Fill in caches */
2936 message->reply_serial = get_int_field (message,
2937 FIELD_REPLY_SERIAL);
2938 message->client_serial = get_int_field (message,
2939 FIELD_CLIENT_SERIAL);
2941 _dbus_verbose ("Loaded message %p\n", message);
2951 * Peeks at first loaded message, returns #NULL if no messages have
2954 * @param loader the loader.
2955 * @returns the next message, or #NULL if none.
2958 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
2960 if (loader->messages)
2961 return loader->messages->data;
2967 * Pops a loaded message (passing ownership of the message
2968 * to the caller). Returns #NULL if no messages have been
2971 * @param loader the loader.
2972 * @returns the next message, or #NULL if none.
2975 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2977 return _dbus_list_pop_first (&loader->messages);
2981 * Pops a loaded message inside a list link (passing ownership of the
2982 * message and link to the caller). Returns #NULL if no messages have
2985 * @param loader the loader.
2986 * @returns the next message link, or #NULL if none.
2989 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
2991 return _dbus_list_pop_first_link (&loader->messages);
2995 * Checks whether the loader is confused due to bad data.
2996 * If messages are received that are invalid, the
2997 * loader gets confused and gives up permanently.
2998 * This state is called "corrupted."
3000 * @param loader the loader
3001 * @returns #TRUE if the loader is hosed.
3004 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3006 return loader->corrupted;
3010 * Sets the maximum size message we allow.
3012 * @param loader the loader
3013 * @param size the max message size in bytes
3016 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3019 if (size > MAX_SANE_MESSAGE_SIZE)
3021 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3022 size, MAX_SANE_MESSAGE_SIZE);
3023 size = MAX_SANE_MESSAGE_SIZE;
3025 loader->max_message_size = size;
3029 * Gets the maximum allowed message size in bytes.
3031 * @param loader the loader
3032 * @returns max size in bytes
3035 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3037 return loader->max_message_size;
3041 #ifdef DBUS_BUILD_TESTS
3042 #include "dbus-test.h"
3046 message_iter_test (DBusMessage *message)
3048 DBusMessageIter *iter;
3051 iter = dbus_message_get_args_iter (message);
3054 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
3055 _dbus_assert_not_reached ("Argument type isn't string");
3057 str = dbus_message_iter_get_string (iter);
3058 if (strcmp (str, "Test string") != 0)
3059 _dbus_assert_not_reached ("Strings differ");
3062 if (!dbus_message_iter_next (iter))
3063 _dbus_assert_not_reached ("Reached end of arguments");
3065 /* Signed integer tests */
3066 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
3067 _dbus_assert_not_reached ("Argument type isn't int32");
3069 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
3070 _dbus_assert_not_reached ("Signed integers differ");
3072 if (!dbus_message_iter_next (iter))
3073 _dbus_assert_not_reached ("Reached end of fields");
3075 /* Unsigned integer tests */
3076 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
3077 _dbus_assert_not_reached ("Argument type isn't int32");
3079 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
3080 _dbus_assert_not_reached ("Unsigned integers differ");
3082 if (!dbus_message_iter_next (iter))
3083 _dbus_assert_not_reached ("Reached end of arguments");
3086 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
3087 _dbus_assert_not_reached ("Argument type isn't double");
3089 if (dbus_message_iter_get_double (iter) != 3.14159)
3090 _dbus_assert_not_reached ("Doubles differ");
3092 if (dbus_message_iter_next (iter))
3093 _dbus_assert_not_reached ("Didn't reach end of arguments");
3095 dbus_message_iter_unref (iter);
3099 check_message_handling (DBusMessage *message)
3101 DBusMessageIter *iter;
3104 dbus_int32_t client_serial;
3109 client_serial = dbus_message_get_serial (message);
3111 /* can't use set_serial due to the assertions at the start of it */
3112 set_int_field (message, FIELD_CLIENT_SERIAL,
3115 if (client_serial != dbus_message_get_serial (message))
3117 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
3121 /* If we implement message_set_arg (message, n, value)
3122 * then we would want to test it here
3125 iter = dbus_message_get_args_iter (message);
3126 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
3132 case DBUS_TYPE_INT32:
3133 dbus_message_iter_get_int32 (iter);
3135 case DBUS_TYPE_UINT32:
3136 dbus_message_iter_get_uint32 (iter);
3138 case DBUS_TYPE_DOUBLE:
3139 dbus_message_iter_get_double (iter);
3141 case DBUS_TYPE_STRING:
3144 str = dbus_message_iter_get_string (iter);
3148 case DBUS_TYPE_BOOLEAN_ARRAY:
3150 unsigned char *values;
3153 if (!dbus_message_iter_get_boolean_array (iter, &values, &len))
3159 case DBUS_TYPE_INT32_ARRAY:
3161 dbus_int32_t *values;
3164 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
3170 case DBUS_TYPE_UINT32_ARRAY:
3172 dbus_uint32_t *values;
3175 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
3181 case DBUS_TYPE_DOUBLE_ARRAY:
3186 if (!dbus_message_iter_get_double_array (iter, &values, &len))
3192 case DBUS_TYPE_STRING_ARRAY:
3197 if (!dbus_message_iter_get_string_array (iter, &values, &len))
3200 dbus_free_string_array (values);
3204 case DBUS_TYPE_DICT:
3208 if (!dbus_message_iter_get_dict (iter, &dict))
3210 dbus_dict_unref (dict);
3218 if (!dbus_message_iter_next (iter))
3226 dbus_message_iter_unref (iter);
3232 check_have_valid_message (DBusMessageLoader *loader)
3234 DBusMessage *message;
3240 if (!_dbus_message_loader_queue_messages (loader))
3241 _dbus_assert_not_reached ("no memory to queue messages");
3243 if (_dbus_message_loader_get_is_corrupted (loader))
3245 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
3249 message = _dbus_message_loader_pop_message (loader);
3250 if (message == NULL)
3252 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3256 if (_dbus_string_get_length (&loader->data) > 0)
3258 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3262 /* Verify that we're able to properly deal with the message.
3263 * For example, this would detect improper handling of messages
3264 * in nonstandard byte order.
3266 if (!check_message_handling (message))
3273 dbus_message_unref (message);
3279 check_invalid_message (DBusMessageLoader *loader)
3285 if (!_dbus_message_loader_queue_messages (loader))
3286 _dbus_assert_not_reached ("no memory to queue messages");
3288 if (!_dbus_message_loader_get_is_corrupted (loader))
3290 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3301 check_incomplete_message (DBusMessageLoader *loader)
3303 DBusMessage *message;
3309 if (!_dbus_message_loader_queue_messages (loader))
3310 _dbus_assert_not_reached ("no memory to queue messages");
3312 if (_dbus_message_loader_get_is_corrupted (loader))
3314 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3318 message = _dbus_message_loader_pop_message (loader);
3319 if (message != NULL)
3321 _dbus_warn ("loaded message that was expected to be incomplete\n");
3329 dbus_message_unref (message);
3334 check_loader_results (DBusMessageLoader *loader,
3335 DBusMessageValidity validity)
3337 if (!_dbus_message_loader_queue_messages (loader))
3338 _dbus_assert_not_reached ("no memory to queue messages");
3342 case _DBUS_MESSAGE_VALID:
3343 return check_have_valid_message (loader);
3344 case _DBUS_MESSAGE_INVALID:
3345 return check_invalid_message (loader);
3346 case _DBUS_MESSAGE_INCOMPLETE:
3347 return check_incomplete_message (loader);
3348 case _DBUS_MESSAGE_UNKNOWN:
3352 _dbus_assert_not_reached ("bad DBusMessageValidity");
3358 * Loads the message in the given message file.
3360 * @param filename filename to load
3361 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3362 * @param data string to load message into
3363 * @returns #TRUE if the message was loaded
3366 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3378 dbus_error_init (&error);
3379 if (!_dbus_file_get_contents (data, filename, &error))
3382 _dbus_string_get_const_data (filename, &s);
3383 _dbus_warn ("Could not load message file %s: %s\n", s, error.message);
3384 dbus_error_free (&error);
3390 if (!_dbus_message_data_load (data, filename))
3393 _dbus_string_get_const_data (filename, &s);
3394 _dbus_warn ("Could not load message file %s\n", s);
3407 * Tries loading the message in the given message file
3408 * and verifies that DBusMessageLoader can handle it.
3410 * @param filename filename to load
3411 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3412 * @param expected_validity what the message has to be like to return #TRUE
3413 * @returns #TRUE if the message has the expected validity
3416 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3418 DBusMessageValidity expected_validity)
3425 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3426 _dbus_assert_not_reached ("could not allocate string\n");
3428 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3432 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3440 if (_dbus_string_get_length (&data) > 0)
3441 _dbus_verbose_bytes_of_string (&data, 0,
3442 _dbus_string_get_length (&data));
3444 _dbus_string_get_const_data (filename, &s);
3445 _dbus_warn ("Failed message loader test on %s\n",
3449 _dbus_string_free (&data);
3455 * Tries loading the given message data.
3458 * @param data the message data
3459 * @param expected_validity what the message has to be like to return #TRUE
3460 * @returns #TRUE if the message has the expected validity
3463 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3464 DBusMessageValidity expected_validity)
3466 DBusMessageLoader *loader;
3474 /* Write the data one byte at a time */
3476 loader = _dbus_message_loader_new ();
3478 len = _dbus_string_get_length (data);
3479 for (i = 0; i < len; i++)
3483 _dbus_message_loader_get_buffer (loader, &buffer);
3484 _dbus_string_append_byte (buffer,
3485 _dbus_string_get_byte (data, i));
3486 _dbus_message_loader_return_buffer (loader, buffer, 1);
3489 if (!check_loader_results (loader, expected_validity))
3492 _dbus_message_loader_unref (loader);
3495 /* Write the data all at once */
3497 loader = _dbus_message_loader_new ();
3502 _dbus_message_loader_get_buffer (loader, &buffer);
3503 _dbus_string_copy (data, 0, buffer,
3504 _dbus_string_get_length (buffer));
3505 _dbus_message_loader_return_buffer (loader, buffer, 1);
3508 if (!check_loader_results (loader, expected_validity))
3511 _dbus_message_loader_unref (loader);
3514 /* Write the data 2 bytes at a time */
3516 loader = _dbus_message_loader_new ();
3518 len = _dbus_string_get_length (data);
3519 for (i = 0; i < len; i += 2)
3523 _dbus_message_loader_get_buffer (loader, &buffer);
3524 _dbus_string_append_byte (buffer,
3525 _dbus_string_get_byte (data, i));
3527 _dbus_string_append_byte (buffer,
3528 _dbus_string_get_byte (data, i+1));
3529 _dbus_message_loader_return_buffer (loader, buffer, 1);
3532 if (!check_loader_results (loader, expected_validity))
3535 _dbus_message_loader_unref (loader);
3543 _dbus_message_loader_unref (loader);
3549 process_test_subdir (const DBusString *test_base_dir,
3551 DBusMessageValidity validity,
3552 DBusForeachMessageFileFunc function,
3555 DBusString test_directory;
3556 DBusString filename;
3564 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3565 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3567 _dbus_string_init_const (&filename, subdir);
3569 if (!_dbus_string_copy (test_base_dir, 0,
3570 &test_directory, 0))
3571 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3573 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3574 _dbus_assert_not_reached ("couldn't allocate full path");
3576 _dbus_string_free (&filename);
3577 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3578 _dbus_assert_not_reached ("didn't allocate filename string\n");
3580 dbus_error_init (&error);
3581 dir = _dbus_directory_open (&test_directory, &error);
3585 _dbus_string_get_const_data (&test_directory, &s);
3586 _dbus_warn ("Could not open %s: %s\n", s,
3588 dbus_error_free (&error);
3592 printf ("Testing:\n");
3595 while (_dbus_directory_get_next_file (dir, &filename, &error))
3597 DBusString full_path;
3600 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3601 _dbus_assert_not_reached ("couldn't init string");
3603 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3604 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3606 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3607 _dbus_assert_not_reached ("couldn't concat file to dir");
3609 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3611 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3615 const char *filename_c;
3616 _dbus_string_get_const_data (&filename, &filename_c);
3617 _dbus_verbose ("Skipping non-.message file %s\n",
3619 _dbus_string_free (&full_path);
3625 _dbus_string_get_const_data (&filename, &s);
3626 printf (" %s\n", s);
3629 _dbus_verbose (" expecting %s\n",
3630 validity == _DBUS_MESSAGE_VALID ? "valid" :
3631 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3632 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3634 if (! (*function) (&full_path, is_raw, validity, user_data))
3636 _dbus_string_free (&full_path);
3640 _dbus_string_free (&full_path);
3643 if (dbus_error_is_set (&error))
3646 _dbus_string_get_const_data (&test_directory, &s);
3647 _dbus_warn ("Could not get next file in %s: %s\n",
3649 dbus_error_free (&error);
3658 _dbus_directory_close (dir);
3659 _dbus_string_free (&test_directory);
3660 _dbus_string_free (&filename);
3666 * Runs the given function on every message file in the test suite.
3667 * The function should return #FALSE on test failure or fatal error.
3669 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3670 * @param func the function to run
3671 * @param user_data data for function
3672 * @returns #FALSE if there's a failure
3675 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3676 DBusForeachMessageFileFunc func,
3679 DBusString test_directory;
3684 _dbus_string_init_const (&test_directory, test_data_dir);
3686 if (!process_test_subdir (&test_directory, "valid-messages",
3687 _DBUS_MESSAGE_VALID, func, user_data))
3690 if (!process_test_subdir (&test_directory, "invalid-messages",
3691 _DBUS_MESSAGE_INVALID, func, user_data))
3694 if (!process_test_subdir (&test_directory, "incomplete-messages",
3695 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3702 _dbus_string_free (&test_directory);
3708 verify_test_message (DBusMessage *message)
3710 dbus_int32_t our_int;
3713 dbus_bool_t our_bool;
3715 if (!dbus_message_get_args (message, NULL,
3716 DBUS_TYPE_INT32, &our_int,
3717 DBUS_TYPE_STRING, &our_str,
3718 DBUS_TYPE_DOUBLE, &our_double,
3719 DBUS_TYPE_BOOLEAN, &our_bool,
3721 _dbus_assert_not_reached ("Could not get arguments");
3723 if (our_int != -0x12345678)
3724 _dbus_assert_not_reached ("integers differ!");
3726 if (our_double != 3.14159)
3727 _dbus_assert_not_reached ("doubles differ!");
3729 if (strcmp (our_str, "Test string") != 0)
3730 _dbus_assert_not_reached ("strings differ!");
3733 _dbus_assert_not_reached ("booleans differ");
3735 dbus_free (our_str);
3739 * @ingroup DBusMessageInternals
3740 * Unit test for DBusMessage.
3742 * @returns #TRUE on success.
3745 _dbus_message_test (const char *test_data_dir)
3747 DBusMessage *message;
3748 DBusMessageLoader *loader;
3755 /* Test the vararg functions */
3756 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3757 _dbus_message_set_serial (message, 1);
3758 dbus_message_append_args (message,
3759 DBUS_TYPE_INT32, -0x12345678,
3760 DBUS_TYPE_STRING, "Test string",
3761 DBUS_TYPE_DOUBLE, 3.14159,
3762 DBUS_TYPE_BOOLEAN, TRUE,
3764 _dbus_verbose_bytes_of_string (&message->header, 0,
3765 _dbus_string_get_length (&message->header));
3766 _dbus_verbose_bytes_of_string (&message->body, 0,
3767 _dbus_string_get_length (&message->body));
3769 verify_test_message (message);
3771 copy = dbus_message_copy (message);
3773 _dbus_assert (message->client_serial == copy->client_serial);
3774 _dbus_assert (message->reply_serial == copy->reply_serial);
3775 _dbus_assert (message->header_padding == copy->header_padding);
3777 _dbus_assert (_dbus_string_get_length (&message->header) ==
3778 _dbus_string_get_length (©->header));
3780 _dbus_assert (_dbus_string_get_length (&message->body) ==
3781 _dbus_string_get_length (©->body));
3783 verify_test_message (copy);
3785 name1 = dbus_message_get_name (message);
3786 name2 = dbus_message_get_name (copy);
3788 _dbus_assert (strcmp (name1, name2) == 0);
3790 dbus_message_unref (message);
3791 dbus_message_unref (copy);
3793 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3794 _dbus_message_set_serial (message, 1);
3795 dbus_message_set_reply_serial (message, 0x12345678);
3797 dbus_message_append_string (message, "Test string");
3798 dbus_message_append_int32 (message, -0x12345678);
3799 dbus_message_append_uint32 (message, 0xedd1e);
3800 dbus_message_append_double (message, 3.14159);
3802 message_iter_test (message);
3804 /* Message loader test */
3805 _dbus_message_lock (message);
3806 loader = _dbus_message_loader_new ();
3808 /* Write the header data one byte at a time */
3809 _dbus_string_get_const_data (&message->header, &data);
3810 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3814 _dbus_message_loader_get_buffer (loader, &buffer);
3815 _dbus_string_append_byte (buffer, data[i]);
3816 _dbus_message_loader_return_buffer (loader, buffer, 1);
3819 /* Write the body data one byte at a time */
3820 _dbus_string_get_const_data (&message->body, &data);
3821 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3825 _dbus_message_loader_get_buffer (loader, &buffer);
3826 _dbus_string_append_byte (buffer, data[i]);
3827 _dbus_message_loader_return_buffer (loader, buffer, 1);
3830 dbus_message_unref (message);
3832 /* Now pop back the message */
3833 if (!_dbus_message_loader_queue_messages (loader))
3834 _dbus_assert_not_reached ("no memory to queue messages");
3836 if (_dbus_message_loader_get_is_corrupted (loader))
3837 _dbus_assert_not_reached ("message loader corrupted");
3839 message = _dbus_message_loader_pop_message (loader);
3841 _dbus_assert_not_reached ("received a NULL message");
3843 if (dbus_message_get_reply_serial (message) != 0x12345678)
3844 _dbus_assert_not_reached ("reply serial fields differ");
3846 message_iter_test (message);
3848 dbus_message_unref (message);
3849 _dbus_message_loader_unref (loader);
3851 /* Now load every message in test_data_dir if we have one */
3852 if (test_data_dir == NULL)
3855 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3856 (DBusForeachMessageFileFunc)
3857 dbus_internal_do_not_use_try_message_file,
3861 #endif /* DBUS_BUILD_TESTS */