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,
1561 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1563 va_start (var_args, first_arg_type);
1564 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1571 * This function takes a va_list for use by language bindings
1573 * @todo this function (or some lower-level non-convenience function)
1574 * needs better error handling; should allow the application to
1575 * distinguish between out of memory, and bad data from the remote
1576 * app. It also needs to not leak a bunch of args when it gets
1577 * to the arg that's bad, as that would be a security hole
1578 * (allow one app to force another to leak memory)
1580 * @todo We need to free the argument data when an error occurs.
1582 * @see dbus_message_get_args
1583 * @param message the message
1584 * @param error error to be filled in
1585 * @param first_arg_type type of the first argument
1586 * @param var_args return location for first argument, followed by list of type/location pairs
1587 * @returns #FALSE if error was set
1590 dbus_message_get_args_valist (DBusMessage *message,
1595 int spec_type, msg_type, i;
1596 DBusMessageIter *iter;
1599 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1601 iter = dbus_message_get_args_iter (message);
1605 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1611 spec_type = first_arg_type;
1614 while (spec_type != 0)
1616 msg_type = dbus_message_iter_get_arg_type (iter);
1618 if (msg_type != spec_type)
1620 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1621 "Argument %d is specified to be of type \"%s\", but "
1622 "is actually of type \"%s\"\n", i,
1623 _dbus_type_to_string (spec_type),
1624 _dbus_type_to_string (msg_type));
1633 case DBUS_TYPE_BOOLEAN:
1637 ptr = va_arg (var_args, dbus_bool_t *);
1639 *ptr = dbus_message_iter_get_boolean (iter);
1642 case DBUS_TYPE_INT32:
1646 ptr = va_arg (var_args, dbus_int32_t *);
1648 *ptr = dbus_message_iter_get_int32 (iter);
1651 case DBUS_TYPE_UINT32:
1655 ptr = va_arg (var_args, dbus_uint32_t *);
1657 *ptr = dbus_message_iter_get_uint32 (iter);
1661 case DBUS_TYPE_DOUBLE:
1665 ptr = va_arg (var_args, double *);
1667 *ptr = dbus_message_iter_get_double (iter);
1671 case DBUS_TYPE_STRING:
1675 ptr = va_arg (var_args, char **);
1677 *ptr = dbus_message_iter_get_string (iter);
1681 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1688 case DBUS_TYPE_BOOLEAN_ARRAY:
1690 unsigned char **ptr;
1693 ptr = va_arg (var_args, unsigned char **);
1694 len = va_arg (var_args, int *);
1696 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1698 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1704 case DBUS_TYPE_INT32_ARRAY:
1709 ptr = va_arg (var_args, dbus_int32_t **);
1710 len = va_arg (var_args, int *);
1712 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1714 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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, NULL);
1738 case DBUS_TYPE_DOUBLE_ARRAY:
1743 ptr = va_arg (var_args, double **);
1744 len = va_arg (var_args, int *);
1746 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1748 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1754 case DBUS_TYPE_BYTE_ARRAY:
1756 unsigned char **ptr;
1759 ptr = va_arg (var_args, unsigned char **);
1760 len = va_arg (var_args, int *);
1762 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1764 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1769 case DBUS_TYPE_STRING_ARRAY:
1774 ptr = va_arg (var_args, char ***);
1775 len = va_arg (var_args, int *);
1777 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1779 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1784 case DBUS_TYPE_DICT:
1788 dict = va_arg (var_args, DBusDict **);
1790 if (!dbus_message_iter_get_dict (iter, dict))
1792 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1798 _dbus_warn ("Unknown field type %d\n", spec_type);
1801 spec_type = va_arg (var_args, int);
1802 if (spec_type != 0 && !dbus_message_iter_next (iter))
1804 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1805 "Message has only %d arguments, but more were expected", i);
1815 dbus_message_iter_unref (iter);
1820 * Returns a DBusMessageIter representing the arguments of the
1821 * message passed in.
1823 * @todo IMO the message iter should follow the GtkTextIter pattern,
1824 * a static object with a "stamp" value used to detect invalid
1825 * iter uses (uninitialized or after changing the message).
1826 * ref/unref is kind of annoying to deal with, and slower too.
1827 * This implies not ref'ing the message from the iter.
1829 * @todo I'd also name this dbus_message_iter_new() or
1830 * for the static object dbus_message_iter_init() rather
1831 * than making it a method on the message
1833 * @param message the message
1834 * @returns a new iter.
1837 dbus_message_get_args_iter (DBusMessage *message)
1839 DBusMessageIter *iter;
1841 iter = dbus_new (DBusMessageIter, 1);
1845 dbus_message_ref (message);
1848 iter->message = message;
1856 * Increments the reference count of a DBusMessageIter.
1858 * @param iter the message iter
1859 * @see dbus_message_iter_unref
1862 dbus_message_iter_ref (DBusMessageIter *iter)
1864 _dbus_assert (iter->refcount > 0);
1866 iter->refcount += 1;
1870 * Decrements the reference count of a DBusMessageIter.
1872 * @param iter The message iter
1873 * @see dbus_message_iter_ref
1876 dbus_message_iter_unref (DBusMessageIter *iter)
1878 _dbus_assert (iter->refcount > 0);
1880 iter->refcount -= 1;
1882 if (iter->refcount == 0)
1884 dbus_message_unref (iter->message);
1891 * Checks if an iterator has any more fields.
1893 * @param iter the message iter
1894 * @returns #TRUE if there are more fields
1898 dbus_message_iter_has_next (DBusMessageIter *iter)
1902 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1903 iter->message->byte_order,
1904 iter->pos, &end_pos))
1907 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1914 * Moves the iterator to the next field.
1916 * @param iter The message iter
1917 * @returns #TRUE if the iterator was moved to the next field
1920 dbus_message_iter_next (DBusMessageIter *iter)
1924 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1925 iter->message->byte_order,
1926 iter->pos, &end_pos))
1929 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1932 iter->pos = end_pos;
1938 * Returns the argument type of the argument that the
1939 * message iterator points at.
1941 * @param iter the message iter
1942 * @returns the field type
1945 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1949 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1950 return DBUS_TYPE_INVALID;
1952 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1954 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_DICT)
1957 return DBUS_TYPE_INVALID;
1961 * Returns the string value that an iterator may point to.
1962 * Note that you need to check that the iterator points to
1963 * a string value before using this function.
1965 * @see dbus_message_iter_get_arg_type
1966 * @param iter the message iter
1967 * @returns the string
1970 dbus_message_iter_get_string (DBusMessageIter *iter)
1972 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1974 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1975 iter->pos + 1, NULL);
1979 * Returns the boolean value that an iterator may point to.
1980 * Note that you need to check that the iterator points to
1981 * a boolean value before using this function.
1983 * @see dbus_message_iter_get_arg_type
1984 * @param iter the message iter
1985 * @returns the string
1988 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1990 unsigned char value;
1992 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1994 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
2000 * Returns the 32 bit signed integer value that an iterator may point to.
2001 * Note that you need to check that the iterator points to
2002 * an integer value before using this function.
2004 * @see dbus_message_iter_get_arg_type
2005 * @param iter the message iter
2006 * @returns the integer
2009 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2011 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
2013 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
2014 iter->pos + 1, NULL);
2018 * Returns the 32 bit unsigned integer value that an iterator may point to.
2019 * Note that you need to check that the iterator points to
2020 * an unsigned integer value before using this function.
2022 * @see dbus_message_iter_get_arg_type
2023 * @param iter the message iter
2024 * @returns the integer
2027 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2029 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
2031 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
2032 iter->pos + 1, NULL);
2036 * Returns the double value that an iterator may point to.
2037 * Note that you need to check that the iterator points to
2038 * a string value before using this function.
2040 * @see dbus_message_iter_get_arg_type
2041 * @param iter the message iter
2042 * @returns the double
2045 dbus_message_iter_get_double (DBusMessageIter *iter)
2047 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
2049 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
2050 iter->pos + 1, NULL);
2054 * Returns the boolean array that the iterator may point to. Note that
2055 * you need to check that the iterator points to an array of the
2056 * correct type prior to using this function.
2058 * @param iter the iterator
2059 * @param value return location for the array
2060 * @param len return location for the array length
2061 * @returns #TRUE on success
2064 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2065 unsigned char **value,
2068 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
2070 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2071 iter->pos + 1, NULL, value, len))
2078 * Returns the 32 bit signed integer array that the iterator may point
2079 * to. Note that you need to check that the iterator points to an
2080 * array of the correct type prior to using this function.
2082 * @param iter the iterator
2083 * @param value return location for the array
2084 * @param len return location for the array length
2085 * @returns #TRUE on success
2088 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2089 dbus_int32_t **value,
2092 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
2094 if (!_dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
2095 iter->pos + 1, NULL, value, len))
2102 * Returns the 32 bit unsigned integer array that the iterator may point
2103 * to. Note that you need to check that the iterator points to an
2104 * array of the correct type prior to using this function.
2106 * @param iter the iterator
2107 * @param value return location for the array
2108 * @param len return location for the array length
2109 * @returns #TRUE on success
2112 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2113 dbus_uint32_t **value,
2116 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2118 if (!_dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2119 iter->pos + 1, NULL, value, len))
2126 * Returns the double array that the iterator may point to. Note that
2127 * you need to check that the iterator points to an array of the
2128 * correct type prior to using this function.
2130 * @param iter the iterator
2131 * @param value return location for the array
2132 * @param len return location for the array length
2133 * @returns #TRUE on success
2136 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2140 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2142 if (!_dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2143 iter->pos + 1, NULL, value, len))
2150 * Returns the byte array that the iterator may point to.
2151 * Note that you need to check that the iterator points
2152 * to a byte array prior to using this function.
2154 * @param iter the iterator
2155 * @param value return location for array values
2156 * @param len return location for length of byte array
2157 * @returns #TRUE on success
2160 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2161 unsigned char **value,
2164 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2166 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2167 iter->pos + 1, NULL, value, len))
2174 * Returns the string array that the iterator may point to.
2175 * Note that you need to check that the iterator points
2176 * to a byte array prior to using this function.
2178 * The returned value is a #NULL-terminated array of strings.
2179 * Each string is a separate malloc block, and the array
2180 * itself is a malloc block. You can free this type of
2181 * string array with dbus_free_string_array().
2183 * @param iter the iterator
2184 * @param value return location for string values
2185 * @param len return location for length of byte array
2186 * @returns #TRUE on success
2189 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2193 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2195 if (!_dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2196 iter->pos + 1, NULL, value, len))
2203 * Returns the dict that the iterator may point to.
2204 * Note that you need to check that the iterator points
2205 * to a dict prior to using this function.
2207 * @param iter the iterator
2208 * @param dict return location for dict
2209 * @returns #TRUE on success
2212 dbus_message_iter_get_dict (DBusMessageIter *iter,
2215 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DICT);
2217 if (!_dbus_demarshal_dict (&iter->message->body, iter->message->byte_order,
2218 iter->pos + 1, NULL, dict))
2225 * Sets the message sender.
2227 * @param message the message
2228 * @param sender the sender
2229 * @returns #FALSE if not enough memory
2232 dbus_message_set_sender (DBusMessage *message,
2235 _dbus_assert (!message->locked);
2239 delete_string_field (message, FIELD_SENDER);
2244 return set_string_field (message,
2251 * Sets a flag indicating that the message is an error reply
2252 * message, i.e. an "exception" rather than a normal response.
2254 * @param message the message
2255 * @param is_error_reply #TRUE if this is an error message.
2258 dbus_message_set_is_error (DBusMessage *message,
2259 dbus_bool_t is_error_reply)
2263 _dbus_assert (!message->locked);
2265 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2268 *header |= DBUS_HEADER_FLAG_ERROR;
2270 *header &= ~DBUS_HEADER_FLAG_ERROR;
2274 * Returns #TRUE if the message is an error
2275 * reply to some previous message we sent.
2277 * @param message the message
2278 * @returns #TRUE if the message is an error
2281 dbus_message_get_is_error (DBusMessage *message)
2285 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2287 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2291 * Gets the service which originated this message,
2292 * or #NULL if unknown or inapplicable.
2294 * @param message the message
2295 * @returns the service name or #NULL
2298 dbus_message_get_sender (DBusMessage *message)
2300 return get_string_field (message, FIELD_SENDER, NULL);
2304 * Checks whether the message has the given name.
2305 * If the message has no name or has a different
2306 * name, returns #FALSE.
2308 * @param message the message
2309 * @param name the name to check (must not be #NULL)
2311 * @returns #TRUE if the message has the given name
2314 dbus_message_name_is (DBusMessage *message,
2319 _dbus_assert (name != NULL);
2321 n = dbus_message_get_name (message);
2323 if (n && strcmp (n, name) == 0)
2330 * Checks whether the message was sent to the given service. If the
2331 * message has no service specified or has a different name, returns
2334 * @param message the message
2335 * @param service the service to check (must not be #NULL)
2337 * @returns #TRUE if the message has the given destination service
2340 dbus_message_service_is (DBusMessage *message,
2341 const char *service)
2345 _dbus_assert (service != NULL);
2347 s = dbus_message_get_service (message);
2349 if (s && strcmp (s, service) == 0)
2356 * Checks whether the message has the given service as its sender. If
2357 * the message has no sender specified or has a different sender,
2358 * returns #FALSE. Note that if a peer application owns multiple
2359 * services, its messages will have only one of those services as the
2360 * sender (usually the base service). So you can't use this
2361 * function to prove the sender didn't own service Foo, you can
2362 * only use it to prove that it did.
2364 * @param message the message
2365 * @param service the service to check (must not be #NULL)
2367 * @returns #TRUE if the message has the given origin service
2370 dbus_message_sender_is (DBusMessage *message,
2371 const char *service)
2375 _dbus_assert (service != NULL);
2377 s = dbus_message_get_sender (message);
2379 if (s && strcmp (s, service) == 0)
2388 * @addtogroup DBusMessageInternals
2393 * @typedef DBusMessageLoader
2395 * The DBusMessageLoader object encapsulates the process of converting
2396 * a byte stream into a series of DBusMessage. It buffers the incoming
2397 * bytes as efficiently as possible, and generates a queue of
2398 * messages. DBusMessageLoader is typically used as part of a
2399 * DBusTransport implementation. The DBusTransport then hands off
2400 * the loaded messages to a DBusConnection, making the messages
2401 * visible to the application.
2403 * @todo write tests for break-loader that a) randomly delete header
2404 * fields and b) set string fields to zero-length and other funky
2409 /* we definitely use signed ints for sizes, so don't exceed
2410 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2411 * over 128M is pretty nuts anyhow.
2415 * The maximum sane message size.
2417 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2420 * Implementation details of DBusMessageLoader.
2421 * All members are private.
2423 struct DBusMessageLoader
2425 int refcount; /**< Reference count. */
2427 DBusString data; /**< Buffered data */
2429 DBusList *messages; /**< Complete messages. */
2431 long max_message_size; /**< Maximum size of a message */
2433 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2435 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2439 * The initial buffer size of the message loader.
2441 * @todo this should be based on min header size plus some average
2442 * body size, or something. Or rather, the min header size only, if we
2443 * want to try to read only the header, store that in a DBusMessage,
2444 * then read only the body and store that, etc., depends on
2445 * how we optimize _dbus_message_loader_get_buffer() and what
2446 * the exact message format is.
2448 #define INITIAL_LOADER_DATA_LEN 32
2451 * Creates a new message loader. Returns #NULL if memory can't
2454 * @returns new loader, or #NULL.
2457 _dbus_message_loader_new (void)
2459 DBusMessageLoader *loader;
2461 loader = dbus_new0 (DBusMessageLoader, 1);
2465 loader->refcount = 1;
2467 /* Try to cap message size at something that won't *totally* hose
2468 * the system if we have a couple of them.
2470 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2472 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2478 /* preallocate the buffer for speed, ignore failure */
2479 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2480 _dbus_string_set_length (&loader->data, 0);
2486 * Increments the reference count of the loader.
2488 * @param loader the loader.
2491 _dbus_message_loader_ref (DBusMessageLoader *loader)
2493 loader->refcount += 1;
2497 * Decrements the reference count of the loader and finalizes the
2498 * loader when the count reaches zero.
2500 * @param loader the loader.
2503 _dbus_message_loader_unref (DBusMessageLoader *loader)
2505 loader->refcount -= 1;
2506 if (loader->refcount == 0)
2508 _dbus_list_foreach (&loader->messages,
2509 (DBusForeachFunction) dbus_message_unref,
2511 _dbus_list_clear (&loader->messages);
2512 _dbus_string_free (&loader->data);
2518 * Gets the buffer to use for reading data from the network. Network
2519 * data is read directly into an allocated buffer, which is then used
2520 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2521 * The buffer must always be returned immediately using
2522 * _dbus_message_loader_return_buffer(), even if no bytes are
2523 * successfully read.
2525 * @todo this function can be a lot more clever. For example
2526 * it can probably always return a buffer size to read exactly
2527 * the body of the next message, thus avoiding any memory wastage
2530 * @todo we need to enforce a max length on strings in header fields.
2532 * @param loader the message loader.
2533 * @param buffer the buffer
2536 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2537 DBusString **buffer)
2539 _dbus_assert (!loader->buffer_outstanding);
2541 *buffer = &loader->data;
2543 loader->buffer_outstanding = TRUE;
2547 * The smallest header size that can occur.
2548 * (It won't be valid)
2550 #define DBUS_MINIMUM_HEADER_SIZE 16
2552 /** Pack four characters as in "abcd" into a uint32 */
2553 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2554 ((((dbus_uint32_t)a) << 24) | \
2555 (((dbus_uint32_t)b) << 16) | \
2556 (((dbus_uint32_t)c) << 8) | \
2559 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2560 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2561 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2563 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2564 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2565 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2567 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2568 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2569 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2571 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2572 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2573 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2575 /* FIXME impose max length on name, srvc, sndr */
2577 decode_header_data (const DBusString *data,
2580 HeaderField fields[FIELD_LAST],
2581 int *message_padding)
2587 if (header_len < 16)
2591 while (i < FIELD_LAST)
2593 fields[i].offset = -1;
2597 fields[FIELD_HEADER_LENGTH].offset = 4;
2598 fields[FIELD_BODY_LENGTH].offset = 8;
2599 fields[FIELD_CLIENT_SERIAL].offset = 12;
2601 /* Now handle the named fields. A real named field is at least 4
2602 * bytes for the name, plus a type code (1 byte) plus padding. So
2603 * if we have less than 8 bytes left, it must be alignment padding,
2604 * not a field. While >= 8 bytes can't be entirely alignment
2608 while ((pos + 7) < header_len)
2610 pos = _DBUS_ALIGN_VALUE (pos, 4);
2612 if ((pos + 4) > header_len)
2615 _dbus_string_get_const_data_len (data, &field, pos, 4);
2618 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2620 /* I believe FROM_BE is right, but if not we'll find out
2623 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2625 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2626 if (fields[FIELD_SERVICE].offset >= 0)
2628 _dbus_verbose ("%s field provided twice\n",
2629 DBUS_HEADER_FIELD_SERVICE);
2633 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2635 _dbus_verbose ("Found service name at offset %d\n",
2636 fields[FIELD_SERVICE].offset);
2640 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2641 if (fields[FIELD_NAME].offset >= 0)
2643 _dbus_verbose ("%s field provided twice\n",
2644 DBUS_HEADER_FIELD_NAME);
2648 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2651 _dbus_verbose ("Found message name at offset %d\n",
2652 fields[FIELD_NAME].offset);
2655 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2656 if (fields[FIELD_SENDER].offset >= 0)
2658 _dbus_verbose ("%s field provided twice\n",
2659 DBUS_HEADER_FIELD_SENDER);
2663 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2665 _dbus_verbose ("Found sender name at offset %d\n",
2666 fields[FIELD_NAME].offset);
2669 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2670 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2672 _dbus_verbose ("%s field provided twice\n",
2673 DBUS_HEADER_FIELD_REPLY);
2677 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2679 _dbus_verbose ("Found reply serial at offset %d\n",
2680 fields[FIELD_REPLY_SERIAL].offset);
2684 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2685 field[0], field[1], field[2], field[3], pos);
2688 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2690 _dbus_verbose ("Failed to validate argument to named header field\n");
2694 if (new_pos > header_len)
2696 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2703 if (pos < header_len)
2705 /* Alignment padding, verify that it's nul */
2706 _dbus_assert ((header_len - pos) < 8);
2708 if (!_dbus_string_validate_nul (data,
2709 pos, (header_len - pos)))
2711 _dbus_verbose ("header alignment padding is not nul\n");
2716 if (fields[FIELD_NAME].offset < 0)
2718 _dbus_verbose ("No %s field provided\n",
2719 DBUS_HEADER_FIELD_NAME);
2723 if (message_padding)
2724 *message_padding = header_len - pos;
2730 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2731 * indicating to the loader how many bytes of the buffer were filled
2732 * in. This function must always be called, even if no bytes were
2733 * successfully read.
2735 * @param loader the loader.
2736 * @param buffer the buffer.
2737 * @param bytes_read number of bytes that were read into the buffer.
2740 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2744 _dbus_assert (loader->buffer_outstanding);
2745 _dbus_assert (buffer == &loader->data);
2747 loader->buffer_outstanding = FALSE;
2751 * Converts buffered data into messages.
2753 * @param loader the loader.
2754 * @returns #TRUE if we had enough memory to finish.
2757 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
2759 if (loader->corrupted)
2762 while (_dbus_string_get_length (&loader->data) >= 16)
2764 DBusMessage *message;
2765 const char *header_data;
2766 int byte_order, header_len, body_len, header_padding;
2767 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2769 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2771 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2773 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2775 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2776 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2777 loader->corrupted = TRUE;
2781 byte_order = header_data[0];
2783 if (byte_order != DBUS_LITTLE_ENDIAN &&
2784 byte_order != DBUS_BIG_ENDIAN)
2786 _dbus_verbose ("Message with bad byte order '%c' received\n",
2788 loader->corrupted = TRUE;
2792 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2793 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2795 if (header_len_unsigned < 16)
2797 _dbus_verbose ("Message had broken too-small header length %u\n",
2798 header_len_unsigned);
2799 loader->corrupted = TRUE;
2803 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2804 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2806 _dbus_verbose ("Header or body length too large (%u %u)\n",
2807 header_len_unsigned,
2809 loader->corrupted = TRUE;
2813 /* Now that we know the values are in signed range, get
2814 * rid of stupid unsigned, just causes bugs
2816 header_len = header_len_unsigned;
2817 body_len = body_len_unsigned;
2819 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2822 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2824 loader->corrupted = TRUE;
2828 if (header_len + body_len > loader->max_message_size)
2830 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
2831 header_len, body_len, loader->max_message_size);
2832 loader->corrupted = TRUE;
2836 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2838 HeaderField fields[FIELD_LAST];
2843 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
2845 if (!decode_header_data (&loader->data, header_len, byte_order,
2846 fields, &header_padding))
2848 _dbus_verbose ("Header was invalid\n");
2849 loader->corrupted = TRUE;
2853 next_arg = header_len;
2854 while (next_arg < (header_len + body_len))
2856 int prev = next_arg;
2858 if (!_dbus_marshal_validate_arg (&loader->data,
2863 loader->corrupted = TRUE;
2867 _dbus_assert (next_arg > prev);
2870 if (next_arg > (header_len + body_len))
2872 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2873 next_arg, header_len, body_len,
2874 header_len + body_len);
2875 loader->corrupted = TRUE;
2879 message = dbus_message_new_empty_header ();
2880 if (message == NULL)
2883 message->byte_order = byte_order;
2884 message->header_padding = header_padding;
2886 /* Copy in the offsets we found */
2888 while (i < FIELD_LAST)
2890 message->header_fields[i] = fields[i];
2894 if (!_dbus_list_append (&loader->messages, message))
2896 dbus_message_unref (message);
2900 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2901 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2903 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2904 (header_len + body_len));
2906 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2908 _dbus_list_remove_last (&loader->messages, message);
2909 dbus_message_unref (message);
2913 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2917 /* put the header back, we'll try again later */
2918 result = _dbus_string_copy_len (&message->header, 0, header_len,
2920 _dbus_assert (result); /* because DBusString never reallocs smaller */
2922 _dbus_list_remove_last (&loader->messages, message);
2923 dbus_message_unref (message);
2927 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2928 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2930 /* Fill in caches */
2931 message->reply_serial = get_int_field (message,
2932 FIELD_REPLY_SERIAL);
2933 message->client_serial = get_int_field (message,
2934 FIELD_CLIENT_SERIAL);
2936 _dbus_verbose ("Loaded message %p\n", message);
2946 * Peeks at first loaded message, returns #NULL if no messages have
2949 * @param loader the loader.
2950 * @returns the next message, or #NULL if none.
2953 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
2955 if (loader->messages)
2956 return loader->messages->data;
2962 * Pops a loaded message (passing ownership of the message
2963 * to the caller). Returns #NULL if no messages have been
2966 * @param loader the loader.
2967 * @returns the next message, or #NULL if none.
2970 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2972 return _dbus_list_pop_first (&loader->messages);
2976 * Pops a loaded message inside a list link (passing ownership of the
2977 * message and link to the caller). Returns #NULL if no messages have
2980 * @param loader the loader.
2981 * @returns the next message link, or #NULL if none.
2984 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
2986 return _dbus_list_pop_first_link (&loader->messages);
2990 * Checks whether the loader is confused due to bad data.
2991 * If messages are received that are invalid, the
2992 * loader gets confused and gives up permanently.
2993 * This state is called "corrupted."
2995 * @param loader the loader
2996 * @returns #TRUE if the loader is hosed.
2999 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3001 return loader->corrupted;
3005 * Sets the maximum size message we allow.
3007 * @param loader the loader
3008 * @param size the max message size in bytes
3011 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3014 if (size > MAX_SANE_MESSAGE_SIZE)
3016 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3017 size, MAX_SANE_MESSAGE_SIZE);
3018 size = MAX_SANE_MESSAGE_SIZE;
3020 loader->max_message_size = size;
3024 * Gets the maximum allowed message size in bytes.
3026 * @param loader the loader
3027 * @returns max size in bytes
3030 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3032 return loader->max_message_size;
3036 #ifdef DBUS_BUILD_TESTS
3037 #include "dbus-test.h"
3041 message_iter_test (DBusMessage *message)
3043 DBusMessageIter *iter;
3046 iter = dbus_message_get_args_iter (message);
3049 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
3050 _dbus_assert_not_reached ("Argument type isn't string");
3052 str = dbus_message_iter_get_string (iter);
3053 if (strcmp (str, "Test string") != 0)
3054 _dbus_assert_not_reached ("Strings differ");
3057 if (!dbus_message_iter_next (iter))
3058 _dbus_assert_not_reached ("Reached end of arguments");
3060 /* Signed integer tests */
3061 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
3062 _dbus_assert_not_reached ("Argument type isn't int32");
3064 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
3065 _dbus_assert_not_reached ("Signed integers differ");
3067 if (!dbus_message_iter_next (iter))
3068 _dbus_assert_not_reached ("Reached end of fields");
3070 /* Unsigned integer tests */
3071 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
3072 _dbus_assert_not_reached ("Argument type isn't int32");
3074 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
3075 _dbus_assert_not_reached ("Unsigned integers differ");
3077 if (!dbus_message_iter_next (iter))
3078 _dbus_assert_not_reached ("Reached end of arguments");
3081 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
3082 _dbus_assert_not_reached ("Argument type isn't double");
3084 if (dbus_message_iter_get_double (iter) != 3.14159)
3085 _dbus_assert_not_reached ("Doubles differ");
3087 if (dbus_message_iter_next (iter))
3088 _dbus_assert_not_reached ("Didn't reach end of arguments");
3090 dbus_message_iter_unref (iter);
3094 check_message_handling (DBusMessage *message)
3096 DBusMessageIter *iter;
3099 dbus_int32_t client_serial;
3104 client_serial = dbus_message_get_serial (message);
3106 /* can't use set_serial due to the assertions at the start of it */
3107 set_int_field (message, FIELD_CLIENT_SERIAL,
3110 if (client_serial != dbus_message_get_serial (message))
3112 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
3116 /* If we implement message_set_arg (message, n, value)
3117 * then we would want to test it here
3120 iter = dbus_message_get_args_iter (message);
3121 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
3127 case DBUS_TYPE_INT32:
3128 dbus_message_iter_get_int32 (iter);
3130 case DBUS_TYPE_UINT32:
3131 dbus_message_iter_get_uint32 (iter);
3133 case DBUS_TYPE_DOUBLE:
3134 dbus_message_iter_get_double (iter);
3136 case DBUS_TYPE_STRING:
3139 str = dbus_message_iter_get_string (iter);
3143 case DBUS_TYPE_BOOLEAN_ARRAY:
3145 unsigned char *values;
3148 if (!dbus_message_iter_get_boolean_array (iter, &values, &len))
3154 case DBUS_TYPE_INT32_ARRAY:
3156 dbus_int32_t *values;
3159 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
3165 case DBUS_TYPE_UINT32_ARRAY:
3167 dbus_uint32_t *values;
3170 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
3176 case DBUS_TYPE_DOUBLE_ARRAY:
3181 if (!dbus_message_iter_get_double_array (iter, &values, &len))
3187 case DBUS_TYPE_STRING_ARRAY:
3192 if (!dbus_message_iter_get_string_array (iter, &values, &len))
3195 dbus_free_string_array (values);
3199 case DBUS_TYPE_DICT:
3203 if (!dbus_message_iter_get_dict (iter, &dict))
3205 dbus_dict_unref (dict);
3213 if (!dbus_message_iter_next (iter))
3221 dbus_message_iter_unref (iter);
3227 check_have_valid_message (DBusMessageLoader *loader)
3229 DBusMessage *message;
3235 if (!_dbus_message_loader_queue_messages (loader))
3236 _dbus_assert_not_reached ("no memory to queue messages");
3238 if (_dbus_message_loader_get_is_corrupted (loader))
3240 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
3244 message = _dbus_message_loader_pop_message (loader);
3245 if (message == NULL)
3247 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3251 if (_dbus_string_get_length (&loader->data) > 0)
3253 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3257 /* Verify that we're able to properly deal with the message.
3258 * For example, this would detect improper handling of messages
3259 * in nonstandard byte order.
3261 if (!check_message_handling (message))
3268 dbus_message_unref (message);
3274 check_invalid_message (DBusMessageLoader *loader)
3280 if (!_dbus_message_loader_queue_messages (loader))
3281 _dbus_assert_not_reached ("no memory to queue messages");
3283 if (!_dbus_message_loader_get_is_corrupted (loader))
3285 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3296 check_incomplete_message (DBusMessageLoader *loader)
3298 DBusMessage *message;
3304 if (!_dbus_message_loader_queue_messages (loader))
3305 _dbus_assert_not_reached ("no memory to queue messages");
3307 if (_dbus_message_loader_get_is_corrupted (loader))
3309 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3313 message = _dbus_message_loader_pop_message (loader);
3314 if (message != NULL)
3316 _dbus_warn ("loaded message that was expected to be incomplete\n");
3324 dbus_message_unref (message);
3329 check_loader_results (DBusMessageLoader *loader,
3330 DBusMessageValidity validity)
3332 if (!_dbus_message_loader_queue_messages (loader))
3333 _dbus_assert_not_reached ("no memory to queue messages");
3337 case _DBUS_MESSAGE_VALID:
3338 return check_have_valid_message (loader);
3339 case _DBUS_MESSAGE_INVALID:
3340 return check_invalid_message (loader);
3341 case _DBUS_MESSAGE_INCOMPLETE:
3342 return check_incomplete_message (loader);
3343 case _DBUS_MESSAGE_UNKNOWN:
3347 _dbus_assert_not_reached ("bad DBusMessageValidity");
3353 * Loads the message in the given message file.
3355 * @param filename filename to load
3356 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3357 * @param data string to load message into
3358 * @returns #TRUE if the message was loaded
3361 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3373 dbus_error_init (&error);
3374 if (!_dbus_file_get_contents (data, filename, &error))
3377 _dbus_string_get_const_data (filename, &s);
3378 _dbus_warn ("Could not load message file %s: %s\n", s, error.message);
3379 dbus_error_free (&error);
3385 if (!_dbus_message_data_load (data, filename))
3388 _dbus_string_get_const_data (filename, &s);
3389 _dbus_warn ("Could not load message file %s\n", s);
3402 * Tries loading the message in the given message file
3403 * and verifies that DBusMessageLoader can handle it.
3405 * @param filename filename to load
3406 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3407 * @param expected_validity what the message has to be like to return #TRUE
3408 * @returns #TRUE if the message has the expected validity
3411 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3413 DBusMessageValidity expected_validity)
3420 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3421 _dbus_assert_not_reached ("could not allocate string\n");
3423 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3427 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3435 if (_dbus_string_get_length (&data) > 0)
3436 _dbus_verbose_bytes_of_string (&data, 0,
3437 _dbus_string_get_length (&data));
3439 _dbus_string_get_const_data (filename, &s);
3440 _dbus_warn ("Failed message loader test on %s\n",
3444 _dbus_string_free (&data);
3450 * Tries loading the given message data.
3453 * @param data the message data
3454 * @param expected_validity what the message has to be like to return #TRUE
3455 * @returns #TRUE if the message has the expected validity
3458 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3459 DBusMessageValidity expected_validity)
3461 DBusMessageLoader *loader;
3469 /* Write the data one byte at a time */
3471 loader = _dbus_message_loader_new ();
3473 len = _dbus_string_get_length (data);
3474 for (i = 0; i < len; i++)
3478 _dbus_message_loader_get_buffer (loader, &buffer);
3479 _dbus_string_append_byte (buffer,
3480 _dbus_string_get_byte (data, i));
3481 _dbus_message_loader_return_buffer (loader, buffer, 1);
3484 if (!check_loader_results (loader, expected_validity))
3487 _dbus_message_loader_unref (loader);
3490 /* Write the data all at once */
3492 loader = _dbus_message_loader_new ();
3497 _dbus_message_loader_get_buffer (loader, &buffer);
3498 _dbus_string_copy (data, 0, buffer,
3499 _dbus_string_get_length (buffer));
3500 _dbus_message_loader_return_buffer (loader, buffer, 1);
3503 if (!check_loader_results (loader, expected_validity))
3506 _dbus_message_loader_unref (loader);
3509 /* Write the data 2 bytes at a time */
3511 loader = _dbus_message_loader_new ();
3513 len = _dbus_string_get_length (data);
3514 for (i = 0; i < len; i += 2)
3518 _dbus_message_loader_get_buffer (loader, &buffer);
3519 _dbus_string_append_byte (buffer,
3520 _dbus_string_get_byte (data, i));
3522 _dbus_string_append_byte (buffer,
3523 _dbus_string_get_byte (data, i+1));
3524 _dbus_message_loader_return_buffer (loader, buffer, 1);
3527 if (!check_loader_results (loader, expected_validity))
3530 _dbus_message_loader_unref (loader);
3538 _dbus_message_loader_unref (loader);
3544 process_test_subdir (const DBusString *test_base_dir,
3546 DBusMessageValidity validity,
3547 DBusForeachMessageFileFunc function,
3550 DBusString test_directory;
3551 DBusString filename;
3559 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3560 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3562 _dbus_string_init_const (&filename, subdir);
3564 if (!_dbus_string_copy (test_base_dir, 0,
3565 &test_directory, 0))
3566 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3568 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3569 _dbus_assert_not_reached ("couldn't allocate full path");
3571 _dbus_string_free (&filename);
3572 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3573 _dbus_assert_not_reached ("didn't allocate filename string\n");
3575 dbus_error_init (&error);
3576 dir = _dbus_directory_open (&test_directory, &error);
3580 _dbus_string_get_const_data (&test_directory, &s);
3581 _dbus_warn ("Could not open %s: %s\n", s,
3583 dbus_error_free (&error);
3587 printf ("Testing:\n");
3590 while (_dbus_directory_get_next_file (dir, &filename, &error))
3592 DBusString full_path;
3595 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3596 _dbus_assert_not_reached ("couldn't init string");
3598 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3599 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3601 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3602 _dbus_assert_not_reached ("couldn't concat file to dir");
3604 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3606 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3610 const char *filename_c;
3611 _dbus_string_get_const_data (&filename, &filename_c);
3612 _dbus_verbose ("Skipping non-.message file %s\n",
3614 _dbus_string_free (&full_path);
3620 _dbus_string_get_const_data (&filename, &s);
3621 printf (" %s\n", s);
3624 _dbus_verbose (" expecting %s\n",
3625 validity == _DBUS_MESSAGE_VALID ? "valid" :
3626 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3627 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3629 if (! (*function) (&full_path, is_raw, validity, user_data))
3631 _dbus_string_free (&full_path);
3635 _dbus_string_free (&full_path);
3638 if (dbus_error_is_set (&error))
3641 _dbus_string_get_const_data (&test_directory, &s);
3642 _dbus_warn ("Could not get next file in %s: %s\n",
3644 dbus_error_free (&error);
3653 _dbus_directory_close (dir);
3654 _dbus_string_free (&test_directory);
3655 _dbus_string_free (&filename);
3661 * Runs the given function on every message file in the test suite.
3662 * The function should return #FALSE on test failure or fatal error.
3664 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3665 * @param func the function to run
3666 * @param user_data data for function
3667 * @returns #FALSE if there's a failure
3670 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3671 DBusForeachMessageFileFunc func,
3674 DBusString test_directory;
3679 _dbus_string_init_const (&test_directory, test_data_dir);
3681 if (!process_test_subdir (&test_directory, "valid-messages",
3682 _DBUS_MESSAGE_VALID, func, user_data))
3685 if (!process_test_subdir (&test_directory, "invalid-messages",
3686 _DBUS_MESSAGE_INVALID, func, user_data))
3689 if (!process_test_subdir (&test_directory, "incomplete-messages",
3690 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3697 _dbus_string_free (&test_directory);
3703 verify_test_message (DBusMessage *message)
3705 dbus_int32_t our_int;
3708 dbus_bool_t our_bool;
3710 if (!dbus_message_get_args (message, NULL,
3711 DBUS_TYPE_INT32, &our_int,
3712 DBUS_TYPE_STRING, &our_str,
3713 DBUS_TYPE_DOUBLE, &our_double,
3714 DBUS_TYPE_BOOLEAN, &our_bool,
3716 _dbus_assert_not_reached ("Could not get arguments");
3718 if (our_int != -0x12345678)
3719 _dbus_assert_not_reached ("integers differ!");
3721 if (our_double != 3.14159)
3722 _dbus_assert_not_reached ("doubles differ!");
3724 if (strcmp (our_str, "Test string") != 0)
3725 _dbus_assert_not_reached ("strings differ!");
3728 _dbus_assert_not_reached ("booleans differ");
3730 dbus_free (our_str);
3734 * @ingroup DBusMessageInternals
3735 * Unit test for DBusMessage.
3737 * @returns #TRUE on success.
3740 _dbus_message_test (const char *test_data_dir)
3742 DBusMessage *message;
3743 DBusMessageLoader *loader;
3750 /* Test the vararg functions */
3751 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3752 _dbus_message_set_serial (message, 1);
3753 dbus_message_append_args (message,
3754 DBUS_TYPE_INT32, -0x12345678,
3755 DBUS_TYPE_STRING, "Test string",
3756 DBUS_TYPE_DOUBLE, 3.14159,
3757 DBUS_TYPE_BOOLEAN, TRUE,
3759 _dbus_verbose_bytes_of_string (&message->header, 0,
3760 _dbus_string_get_length (&message->header));
3761 _dbus_verbose_bytes_of_string (&message->body, 0,
3762 _dbus_string_get_length (&message->body));
3764 verify_test_message (message);
3766 copy = dbus_message_copy (message);
3768 _dbus_assert (message->client_serial == copy->client_serial);
3769 _dbus_assert (message->reply_serial == copy->reply_serial);
3770 _dbus_assert (message->header_padding == copy->header_padding);
3772 _dbus_assert (_dbus_string_get_length (&message->header) ==
3773 _dbus_string_get_length (©->header));
3775 _dbus_assert (_dbus_string_get_length (&message->body) ==
3776 _dbus_string_get_length (©->body));
3778 verify_test_message (copy);
3780 name1 = dbus_message_get_name (message);
3781 name2 = dbus_message_get_name (copy);
3783 _dbus_assert (strcmp (name1, name2) == 0);
3785 dbus_message_unref (message);
3786 dbus_message_unref (copy);
3788 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3789 _dbus_message_set_serial (message, 1);
3790 dbus_message_set_reply_serial (message, 0x12345678);
3792 dbus_message_append_string (message, "Test string");
3793 dbus_message_append_int32 (message, -0x12345678);
3794 dbus_message_append_uint32 (message, 0xedd1e);
3795 dbus_message_append_double (message, 3.14159);
3797 message_iter_test (message);
3799 /* Message loader test */
3800 _dbus_message_lock (message);
3801 loader = _dbus_message_loader_new ();
3803 /* Write the header data one byte at a time */
3804 _dbus_string_get_const_data (&message->header, &data);
3805 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3809 _dbus_message_loader_get_buffer (loader, &buffer);
3810 _dbus_string_append_byte (buffer, data[i]);
3811 _dbus_message_loader_return_buffer (loader, buffer, 1);
3814 /* Write the body data one byte at a time */
3815 _dbus_string_get_const_data (&message->body, &data);
3816 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3820 _dbus_message_loader_get_buffer (loader, &buffer);
3821 _dbus_string_append_byte (buffer, data[i]);
3822 _dbus_message_loader_return_buffer (loader, buffer, 1);
3825 dbus_message_unref (message);
3827 /* Now pop back the message */
3828 if (!_dbus_message_loader_queue_messages (loader))
3829 _dbus_assert_not_reached ("no memory to queue messages");
3831 if (_dbus_message_loader_get_is_corrupted (loader))
3832 _dbus_assert_not_reached ("message loader corrupted");
3834 message = _dbus_message_loader_pop_message (loader);
3836 _dbus_assert_not_reached ("received a NULL message");
3838 if (dbus_message_get_reply_serial (message) != 0x12345678)
3839 _dbus_assert_not_reached ("reply serial fields differ");
3841 message_iter_test (message);
3843 dbus_message_unref (message);
3844 _dbus_message_loader_unref (loader);
3846 /* Now load every message in test_data_dir if we have one */
3847 if (test_data_dir == NULL)
3850 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3851 (DBusForeachMessageFileFunc)
3852 dbus_internal_do_not_use_try_message_file,
3856 #endif /* DBUS_BUILD_TESTS */