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);
631 _dbus_verbose ("message has size %ld\n",
632 message->size_counter_delta);
634 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
638 dbus_message_create_header (DBusMessage *message,
644 if (!_dbus_string_append_byte (&message->header, message->byte_order))
648 if (!_dbus_string_append_byte (&message->header, flags))
651 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
654 if (!_dbus_string_append_byte (&message->header, 0))
657 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
658 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
661 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
662 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
665 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
666 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
669 /* Marshal message service */
672 if (!append_string_field (message,
674 DBUS_HEADER_FIELD_SERVICE,
679 _dbus_assert (name != NULL);
680 if (!append_string_field (message,
682 DBUS_HEADER_FIELD_NAME,
690 * Locks a message. Allows checking that applications don't keep a
691 * reference to a message in the outgoing queue and change it
692 * underneath us. Messages are locked when they enter the outgoing
693 * queue (dbus_connection_send_message()), and the library complains
694 * if the message is modified while locked.
696 * @param message the message to lock.
699 _dbus_message_lock (DBusMessage *message)
701 if (!message->locked)
703 /* Fill in our lengths */
704 set_uint_field (message,
706 _dbus_string_get_length (&message->header));
708 set_uint_field (message,
710 _dbus_string_get_length (&message->body));
712 message->locked = TRUE;
719 * @defgroup DBusMessage DBusMessage
721 * @brief Message to be sent or received over a DBusConnection.
723 * A DBusMessage is the most basic unit of communication over a
724 * DBusConnection. A DBusConnection represents a stream of messages
725 * received from a remote application, and a stream of messages
726 * sent to a remote application.
732 * @typedef DBusMessage
734 * Opaque data type representing a message received from or to be
735 * sent to another application.
739 dbus_message_new_empty_header (void)
741 DBusMessage *message;
744 message = dbus_new0 (DBusMessage, 1);
748 message->refcount = 1;
749 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
750 message->client_serial = -1;
751 message->reply_serial = -1;
754 while (i < FIELD_LAST)
756 message->header_fields[i].offset = -1;
760 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
766 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
768 _dbus_string_free (&message->header);
778 * Constructs a new message. Returns #NULL if memory can't be
779 * allocated for the message. The service may be #NULL in which case
780 * no service is set; this is appropriate when using D-BUS in a
781 * peer-to-peer context (no message bus).
783 * @todo reverse the arguments, first 'name' then 'service'
784 * as 'name' is more fundamental
786 * @param service service that the message should be sent to or #NULL
787 * @param name name of the message
788 * @returns a new DBusMessage, free with dbus_message_unref()
789 * @see dbus_message_unref()
792 dbus_message_new (const char *service,
795 DBusMessage *message;
797 message = dbus_message_new_empty_header ();
801 if (!dbus_message_create_header (message, service, name))
803 dbus_message_unref (message);
811 * Constructs a message that is a reply to some other
812 * message. Returns #NULL if memory can't be allocated
815 * @param original_message the message which the created
816 * message is a reply to.
817 * @returns a new DBusMessage, free with dbus_message_unref()
818 * @see dbus_message_new(), dbus_message_unref()
821 dbus_message_new_reply (DBusMessage *original_message)
823 DBusMessage *message;
824 const char *sender, *name;
826 sender = get_string_field (original_message,
828 name = get_string_field (original_message,
831 /* sender is allowed to be null here in peer-to-peer case */
833 message = dbus_message_new (sender, name);
838 if (!dbus_message_set_reply_serial (message,
839 dbus_message_get_serial (original_message)))
841 dbus_message_unref (message);
849 * Creates a new message that is an error reply to a certain message.
851 * @param original_message the original message
852 * @param error_name the error name
853 * @param error_message the error message string
854 * @returns a new error message
857 dbus_message_new_error_reply (DBusMessage *original_message,
858 const char *error_name,
859 const char *error_message)
861 DBusMessage *message;
864 sender = get_string_field (original_message,
867 _dbus_assert (sender != NULL);
869 message = dbus_message_new (sender, error_name);
874 if (!dbus_message_set_reply_serial (message,
875 dbus_message_get_serial (original_message)))
877 dbus_message_unref (message);
881 if (!dbus_message_append_string (message, error_message))
883 dbus_message_unref (message);
887 dbus_message_set_is_error (message, TRUE);
893 * Creates a new message that is an exact replica of the message
894 * specified, except that its refcount is set to 1.
896 * @param message the message.
897 * @returns the new message.
900 dbus_message_new_from_message (const DBusMessage *message)
905 retval = dbus_new0 (DBusMessage, 1);
909 retval->refcount = 1;
910 retval->byte_order = message->byte_order;
912 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
918 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
920 _dbus_string_free (&retval->header);
925 if (!_dbus_string_copy (&message->header, 0,
928 _dbus_string_free (&retval->header);
929 _dbus_string_free (&retval->body);
935 if (!_dbus_string_copy (&message->body, 0,
938 _dbus_string_free (&retval->header);
939 _dbus_string_free (&retval->body);
945 for (i = 0; i < FIELD_LAST; i++)
947 retval->header_fields[i].offset = message->header_fields[i].offset;
955 * Increments the reference count of a DBusMessage.
957 * @param message The message
958 * @see dbus_message_unref
961 dbus_message_ref (DBusMessage *message)
963 dbus_atomic_t refcount;
965 refcount = _dbus_atomic_inc (&message->refcount);
966 _dbus_assert (refcount > 1);
970 * Decrements the reference count of a DBusMessage.
972 * @param message The message
973 * @see dbus_message_ref
976 dbus_message_unref (DBusMessage *message)
978 dbus_atomic_t refcount;
980 refcount = _dbus_atomic_dec (&message->refcount);
982 _dbus_assert (refcount >= 0);
986 if (message->size_counter != NULL)
988 _dbus_counter_adjust (message->size_counter,
989 - message->size_counter_delta);
990 _dbus_counter_unref (message->size_counter);
993 _dbus_string_free (&message->header);
994 _dbus_string_free (&message->body);
1001 * Gets the name of a message.
1003 * @param message the message
1004 * @returns the message name (should not be freed)
1007 dbus_message_get_name (DBusMessage *message)
1009 return get_string_field (message, FIELD_NAME, NULL);
1013 * Gets the destination service of a message.
1015 * @param message the message
1016 * @returns the message destination service (should not be freed)
1019 dbus_message_get_service (DBusMessage *message)
1021 return get_string_field (message, FIELD_SERVICE, NULL);
1025 * Appends fields to a message given a variable argument
1026 * list. The variable argument list should contain the type
1027 * of the argument followed by the value to add. Array values
1028 * are specified by a pointer to the array followed by an int
1029 * giving the length of the array. The list is terminated
1032 * @param message the message
1033 * @param first_arg_type type of the first argument
1034 * @param ... value of first argument, list of additional type-value pairs
1035 * @returns #TRUE on success
1038 dbus_message_append_args (DBusMessage *message,
1045 va_start (var_args, first_arg_type);
1046 retval = dbus_message_append_args_valist (message,
1055 * This function takes a va_list for use by language bindings
1057 * @see dbus_message_append_args.
1058 * @param message the message
1059 * @param first_arg_type type of first argument
1060 * @param var_args value of first argument, then list of type/value pairs
1061 * @returns #TRUE on success
1064 dbus_message_append_args_valist (DBusMessage *message,
1070 old_len = _dbus_string_get_length (&message->body);
1072 type = first_arg_type;
1079 if (!dbus_message_append_nil (message))
1081 case DBUS_TYPE_BOOLEAN:
1082 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1085 case DBUS_TYPE_INT32:
1086 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1089 case DBUS_TYPE_UINT32:
1090 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1093 case DBUS_TYPE_DOUBLE:
1094 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1097 case DBUS_TYPE_STRING:
1098 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1101 case DBUS_TYPE_BOOLEAN_ARRAY:
1104 unsigned char *data;
1106 data = va_arg (var_args, unsigned char *);
1107 len = va_arg (var_args, int);
1109 if (!dbus_message_append_boolean_array (message, data, len))
1113 case DBUS_TYPE_INT32_ARRAY:
1118 data = va_arg (var_args, dbus_int32_t *);
1119 len = va_arg (var_args, int);
1121 if (!dbus_message_append_int32_array (message, data, len))
1125 case DBUS_TYPE_UINT32_ARRAY:
1128 dbus_uint32_t *data;
1130 data = va_arg (var_args, dbus_uint32_t *);
1131 len = va_arg (var_args, int);
1133 if (!dbus_message_append_uint32_array (message, data, len))
1137 case DBUS_TYPE_DOUBLE_ARRAY:
1142 data = va_arg (var_args, double *);
1143 len = va_arg (var_args, int);
1145 if (!dbus_message_append_double_array (message, data, len))
1149 case DBUS_TYPE_BYTE_ARRAY:
1152 unsigned char *data;
1154 data = va_arg (var_args, unsigned char *);
1155 len = va_arg (var_args, int);
1157 if (!dbus_message_append_byte_array (message, data, len))
1161 case DBUS_TYPE_STRING_ARRAY:
1166 data = va_arg (var_args, const char **);
1167 len = va_arg (var_args, int);
1169 if (!dbus_message_append_string_array (message, data, len))
1173 case DBUS_TYPE_DICT:
1177 dict = va_arg (var_args, DBusDict *);
1179 if (!dbus_message_append_dict (message, dict))
1183 _dbus_warn ("Unknown field type %d\n", type);
1186 type = va_arg (var_args, int);
1196 * Appends a nil value to the message
1198 * @param message the message
1199 * @returns #TRUE on success
1202 dbus_message_append_nil (DBusMessage *message)
1204 _dbus_assert (!message->locked);
1206 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1213 * Appends a boolean value to the message
1215 * @param message the message
1216 * @param value the boolean value
1217 * @returns #TRUE on success
1220 dbus_message_append_boolean (DBusMessage *message,
1223 _dbus_assert (!message->locked);
1225 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1228 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1230 _dbus_string_shorten (&message->body, 1);
1238 * Appends a 32 bit signed integer to the message.
1240 * @param message the message
1241 * @param value the integer value
1242 * @returns #TRUE on success
1245 dbus_message_append_int32 (DBusMessage *message,
1248 _dbus_assert (!message->locked);
1250 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1253 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1255 _dbus_string_shorten (&message->body, 1);
1263 * Appends a 32 bit unsigned integer to the message.
1265 * @param message the message
1266 * @param value the integer value
1267 * @returns #TRUE on success
1270 dbus_message_append_uint32 (DBusMessage *message,
1271 dbus_uint32_t value)
1273 _dbus_assert (!message->locked);
1275 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1278 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1280 _dbus_string_shorten (&message->body, 1);
1288 * Appends a double value to the message.
1290 * @param message the message
1291 * @param value the double value
1292 * @returns #TRUE on success
1295 dbus_message_append_double (DBusMessage *message,
1298 _dbus_assert (!message->locked);
1300 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1303 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1305 _dbus_string_shorten (&message->body, 1);
1313 * Appends a UTF-8 string to the message.
1315 * @param message the message
1316 * @param value the string
1317 * @returns #TRUE on success
1320 dbus_message_append_string (DBusMessage *message,
1323 _dbus_assert (!message->locked);
1325 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1328 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1330 _dbus_string_shorten (&message->body, 1);
1338 * Appends a boolean array to the message.
1340 * @param message the message
1341 * @param value the array
1342 * @param len the length of the array
1343 * @returns #TRUE on success
1346 dbus_message_append_boolean_array (DBusMessage *message,
1347 unsigned const char *value,
1350 _dbus_assert (!message->locked);
1352 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1355 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1357 _dbus_string_shorten (&message->body, 1);
1365 * Appends a 32 bit signed integer array to the message.
1367 * @param message the message
1368 * @param value the array
1369 * @param len the length of the array
1370 * @returns #TRUE on success
1373 dbus_message_append_int32_array (DBusMessage *message,
1374 const dbus_int32_t *value,
1377 _dbus_assert (!message->locked);
1379 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1382 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1385 _dbus_string_shorten (&message->body, 1);
1393 * Appends a 32 bit unsigned integer array to the message.
1395 * @param message the message
1396 * @param value the array
1397 * @param len the length of the array
1398 * @returns #TRUE on success
1401 dbus_message_append_uint32_array (DBusMessage *message,
1402 const dbus_uint32_t *value,
1405 _dbus_assert (!message->locked);
1407 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1410 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1413 _dbus_string_shorten (&message->body, 1);
1421 * Appends a double array to the message.
1423 * @param message the message
1424 * @param value the array
1425 * @param len the length of the array
1426 * @returns #TRUE on success
1429 dbus_message_append_double_array (DBusMessage *message,
1430 const double *value,
1433 _dbus_assert (!message->locked);
1435 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1438 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1441 _dbus_string_shorten (&message->body, 1);
1449 * Appends a byte array to the message.
1451 * @param message the message
1452 * @param value the array
1453 * @param len the length of the array
1454 * @returns #TRUE on success
1457 dbus_message_append_byte_array (DBusMessage *message,
1458 unsigned const char *value,
1461 _dbus_assert (!message->locked);
1463 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1466 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1468 _dbus_string_shorten (&message->body, 1);
1476 * Appends a string array to the message.
1478 * @param message the message
1479 * @param value the array
1480 * @param len the length of the array
1481 * @returns #TRUE on success
1484 dbus_message_append_string_array (DBusMessage *message,
1488 _dbus_assert (!message->locked);
1490 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1493 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1496 _dbus_string_shorten (&message->body, 1);
1504 * Appends a dict to the message.
1506 * @param message the message
1507 * @param dict the dict
1508 * @returns #TRUE on success
1511 dbus_message_append_dict (DBusMessage *message,
1514 _dbus_assert (!message->locked);
1516 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DICT))
1519 if (!_dbus_marshal_dict (&message->body, message->byte_order, dict))
1521 _dbus_string_shorten (&message->body, 1);
1529 * Gets arguments from a message given a variable argument list.
1530 * The variable argument list should contain the type of the
1531 * argumen followed by a pointer to where the value should be
1532 * stored. The list is terminated with 0.
1534 * @param message the message
1535 * @param error error to be filled in on failure
1536 * @param first_arg_type the first argument type
1537 * @param ... location for first argument value, then list of type-location pairs
1538 * @returns #FALSE if the error was set
1541 dbus_message_get_args (DBusMessage *message,
1546 DBusResultCode retval;
1549 va_start (var_args, first_arg_type);
1550 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1557 * This function takes a va_list for use by language bindings
1559 * @todo this function (or some lower-level non-convenience function)
1560 * needs better error handling; should allow the application to
1561 * distinguish between out of memory, and bad data from the remote
1562 * app. It also needs to not leak a bunch of args when it gets
1563 * to the arg that's bad, as that would be a security hole
1564 * (allow one app to force another to leak memory)
1566 * @todo We need to free the argument data when an error occurs.
1568 * @see dbus_message_get_args
1569 * @param message the message
1570 * @param error error to be filled in
1571 * @param first_arg_type type of the first argument
1572 * @param var_args return location for first argument, followed by list of type/location pairs
1573 * @returns #FALSE if error was set
1576 dbus_message_get_args_valist (DBusMessage *message,
1581 int spec_type, msg_type, i;
1582 DBusMessageIter *iter;
1585 iter = dbus_message_get_args_iter (message);
1589 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1590 "No memory to get message arguments");
1596 spec_type = first_arg_type;
1599 while (spec_type != 0)
1601 msg_type = dbus_message_iter_get_arg_type (iter);
1603 if (msg_type != spec_type)
1605 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1606 "Argument %d is specified to be of type \"%s\", but "
1607 "is actually of type \"%s\"\n", i,
1608 _dbus_type_to_string (spec_type),
1609 _dbus_type_to_string (msg_type));
1618 case DBUS_TYPE_BOOLEAN:
1622 ptr = va_arg (var_args, dbus_bool_t *);
1624 *ptr = dbus_message_iter_get_boolean (iter);
1627 case DBUS_TYPE_INT32:
1631 ptr = va_arg (var_args, dbus_int32_t *);
1633 *ptr = dbus_message_iter_get_int32 (iter);
1636 case DBUS_TYPE_UINT32:
1640 ptr = va_arg (var_args, dbus_uint32_t *);
1642 *ptr = dbus_message_iter_get_uint32 (iter);
1646 case DBUS_TYPE_DOUBLE:
1650 ptr = va_arg (var_args, double *);
1652 *ptr = dbus_message_iter_get_double (iter);
1656 case DBUS_TYPE_STRING:
1660 ptr = va_arg (var_args, char **);
1662 *ptr = dbus_message_iter_get_string (iter);
1666 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1667 "No memory for argument %d", i);
1674 case DBUS_TYPE_BOOLEAN_ARRAY:
1676 unsigned char **ptr;
1679 ptr = va_arg (var_args, unsigned char **);
1680 len = va_arg (var_args, int *);
1682 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1684 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1685 "No memory for argument %d", i);
1691 case DBUS_TYPE_INT32_ARRAY:
1696 ptr = va_arg (var_args, dbus_int32_t **);
1697 len = va_arg (var_args, int *);
1699 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1701 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1702 "No memory for argument %d", i);
1709 case DBUS_TYPE_UINT32_ARRAY:
1711 dbus_uint32_t **ptr;
1714 ptr = va_arg (var_args, dbus_uint32_t **);
1715 len = va_arg (var_args, int *);
1717 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1719 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1720 "No memory for argument %d", i);
1727 case DBUS_TYPE_DOUBLE_ARRAY:
1732 ptr = va_arg (var_args, double **);
1733 len = va_arg (var_args, int *);
1735 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1737 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1738 "No memory for argument %d", i);
1744 case DBUS_TYPE_BYTE_ARRAY:
1746 unsigned char **ptr;
1749 ptr = va_arg (var_args, unsigned char **);
1750 len = va_arg (var_args, int *);
1752 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1754 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1755 "No memory for argument %d", i);
1760 case DBUS_TYPE_STRING_ARRAY:
1765 ptr = va_arg (var_args, char ***);
1766 len = va_arg (var_args, int *);
1768 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1770 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1771 "No memory for argument %d", i);
1776 case DBUS_TYPE_DICT:
1780 dict = va_arg (var_args, DBusDict **);
1782 if (!dbus_message_iter_get_dict (iter, dict))
1784 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1785 "No memory for argument %d", i);
1791 _dbus_warn ("Unknown field type %d\n", spec_type);
1794 spec_type = va_arg (var_args, int);
1795 if (spec_type != 0 && !dbus_message_iter_next (iter))
1797 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1798 "Message has only %d arguments, but more were expected", i);
1808 dbus_message_iter_unref (iter);
1813 * Returns a DBusMessageIter representing the arguments of the
1814 * message passed in.
1816 * @todo IMO the message iter should follow the GtkTextIter pattern,
1817 * a static object with a "stamp" value used to detect invalid
1818 * iter uses (uninitialized or after changing the message).
1819 * ref/unref is kind of annoying to deal with, and slower too.
1820 * This implies not ref'ing the message from the iter.
1822 * @todo I'd also name this dbus_message_iter_new() or
1823 * for the static object dbus_message_iter_init() rather
1824 * than making it a method on the message
1826 * @param message the message
1827 * @returns a new iter.
1830 dbus_message_get_args_iter (DBusMessage *message)
1832 DBusMessageIter *iter;
1834 iter = dbus_new (DBusMessageIter, 1);
1838 dbus_message_ref (message);
1841 iter->message = message;
1849 * Increments the reference count of a DBusMessageIter.
1851 * @param iter the message iter
1852 * @see dbus_message_iter_unref
1855 dbus_message_iter_ref (DBusMessageIter *iter)
1857 _dbus_assert (iter->refcount > 0);
1859 iter->refcount += 1;
1863 * Decrements the reference count of a DBusMessageIter.
1865 * @param iter The message iter
1866 * @see dbus_message_iter_ref
1869 dbus_message_iter_unref (DBusMessageIter *iter)
1871 _dbus_assert (iter->refcount > 0);
1873 iter->refcount -= 1;
1875 if (iter->refcount == 0)
1877 dbus_message_unref (iter->message);
1884 * Checks if an iterator has any more fields.
1886 * @param iter the message iter
1887 * @returns #TRUE if there are more fields
1891 dbus_message_iter_has_next (DBusMessageIter *iter)
1895 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1896 iter->message->byte_order,
1897 iter->pos, &end_pos))
1900 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1907 * Moves the iterator to the next field.
1909 * @param iter The message iter
1910 * @returns #TRUE if the iterator was moved to the next field
1913 dbus_message_iter_next (DBusMessageIter *iter)
1917 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1918 iter->message->byte_order,
1919 iter->pos, &end_pos))
1922 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1925 iter->pos = end_pos;
1931 * Returns the argument type of the argument that the
1932 * message iterator points at.
1934 * @param iter the message iter
1935 * @returns the field type
1938 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1942 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1943 return DBUS_TYPE_INVALID;
1945 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1947 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_DICT)
1950 return DBUS_TYPE_INVALID;
1954 * Returns the string value that an iterator may point to.
1955 * Note that you need to check that the iterator points to
1956 * a string value before using this function.
1958 * @see dbus_message_iter_get_arg_type
1959 * @param iter the message iter
1960 * @returns the string
1963 dbus_message_iter_get_string (DBusMessageIter *iter)
1965 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1967 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1968 iter->pos + 1, NULL);
1972 * Returns the boolean value that an iterator may point to.
1973 * Note that you need to check that the iterator points to
1974 * a boolean value before using this function.
1976 * @see dbus_message_iter_get_arg_type
1977 * @param iter the message iter
1978 * @returns the string
1981 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1983 unsigned char value;
1985 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1987 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
1994 * Returns the 32 bit signed integer value that an iterator may point to.
1995 * Note that you need to check that the iterator points to
1996 * an integer value before using this function.
1998 * @see dbus_message_iter_get_arg_type
1999 * @param iter the message iter
2000 * @returns the integer
2003 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2005 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
2007 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
2008 iter->pos + 1, NULL);
2012 * Returns the 32 bit unsigned integer value that an iterator may point to.
2013 * Note that you need to check that the iterator points to
2014 * an unsigned integer value before using this function.
2016 * @see dbus_message_iter_get_arg_type
2017 * @param iter the message iter
2018 * @returns the integer
2021 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2023 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
2025 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
2026 iter->pos + 1, NULL);
2030 * Returns the double value that an iterator may point to.
2031 * Note that you need to check that the iterator points to
2032 * a string value before using this function.
2034 * @see dbus_message_iter_get_arg_type
2035 * @param iter the message iter
2036 * @returns the double
2039 dbus_message_iter_get_double (DBusMessageIter *iter)
2041 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
2043 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
2044 iter->pos + 1, NULL);
2048 * Returns the boolean array that the iterator may point to. Note that
2049 * you need to check that the iterator points to an array of the
2050 * correct type prior to using this function.
2052 * @param iter the iterator
2053 * @param value return location for the array
2054 * @param len return location for the array length
2055 * @returns #TRUE on success
2058 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2059 unsigned char **value,
2062 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
2064 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2065 iter->pos + 1, NULL, len);
2074 * Returns the 32 bit signed integer array that the iterator may point
2075 * to. Note that you need to check that the iterator points to an
2076 * array of the correct type prior to using this function.
2078 * @param iter the iterator
2079 * @param value return location for the array
2080 * @param len return location for the array length
2081 * @returns #TRUE on success
2084 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2085 dbus_int32_t **value,
2088 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
2090 *value = _dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
2091 iter->pos + 1, NULL, len);
2100 * Returns the 32 bit unsigned integer array that the iterator may point
2101 * to. Note that you need to check that the iterator points to an
2102 * array of the correct type prior to using this function.
2104 * @param iter the iterator
2105 * @param value return location for the array
2106 * @param len return location for the array length
2107 * @returns #TRUE on success
2110 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2111 dbus_uint32_t **value,
2114 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2116 *value = _dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2117 iter->pos + 1, NULL, 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 *value = _dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2143 iter->pos + 1, NULL, len);
2152 * Returns the byte array that the iterator may point to.
2153 * Note that you need to check that the iterator points
2154 * to a byte array prior to using this function.
2156 * @param iter the iterator
2157 * @param value return location for array values
2158 * @param len return location for length of byte array
2159 * @returns #TRUE on success
2162 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2163 unsigned char **value,
2166 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2168 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2169 iter->pos + 1, NULL, len);
2178 * Returns the string array that the iterator may point to.
2179 * Note that you need to check that the iterator points
2180 * to a byte array prior to using this function.
2182 * The returned value is a #NULL-terminated array of strings.
2183 * Each string is a separate malloc block, and the array
2184 * itself is a malloc block. You can free this type of
2185 * string array with dbus_free_string_array().
2187 * @param iter the iterator
2188 * @param value return location for string values
2189 * @param len return location for length of byte array
2190 * @returns #TRUE on success
2193 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2197 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2199 *value = _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2200 iter->pos + 1, NULL, len);
2209 * Returns the dict that the iterator may point to.
2210 * Note that you need to check that the iterator points
2211 * to a dict prior to using this function.
2213 * @param iter the iterator
2214 * @param dict return location for dict
2215 * @returns #TRUE on success
2218 dbus_message_iter_get_dict (DBusMessageIter *iter,
2221 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DICT);
2223 *dict = _dbus_demarshal_dict (&iter->message->body, iter->message->byte_order,
2224 iter->pos + 1, NULL);
2233 * Sets the message sender.
2235 * @param message the message
2236 * @param sender the sender
2237 * @returns #FALSE if not enough memory
2240 dbus_message_set_sender (DBusMessage *message,
2243 _dbus_assert (!message->locked);
2247 delete_string_field (message, FIELD_SENDER);
2252 return set_string_field (message,
2259 * Sets a flag indicating that the message is an error reply
2260 * message, i.e. an "exception" rather than a normal response.
2262 * @param message the message
2263 * @param is_error_reply #TRUE if this is an error message.
2266 dbus_message_set_is_error (DBusMessage *message,
2267 dbus_bool_t is_error_reply)
2271 _dbus_assert (!message->locked);
2273 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2276 *header |= DBUS_HEADER_FLAG_ERROR;
2278 *header &= ~DBUS_HEADER_FLAG_ERROR;
2282 * Returns #TRUE if the message is an error
2283 * reply to some previous message we sent.
2285 * @param message the message
2286 * @returns #TRUE if the message is an error
2289 dbus_message_get_is_error (DBusMessage *message)
2293 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2295 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2299 * Gets the service which originated this message,
2300 * or #NULL if unknown or inapplicable.
2302 * @param message the message
2303 * @returns the service name or #NULL
2306 dbus_message_get_sender (DBusMessage *message)
2308 return get_string_field (message, FIELD_SENDER, NULL);
2312 * Checks whether the message has the given name.
2313 * If the message has no name or has a different
2314 * name, returns #FALSE.
2316 * @param message the message
2317 * @param name the name to check (must not be #NULL)
2319 * @returns #TRUE if the message has the given name
2322 dbus_message_name_is (DBusMessage *message,
2327 _dbus_assert (name != NULL);
2329 n = dbus_message_get_name (message);
2331 if (n && strcmp (n, name) == 0)
2340 * @addtogroup DBusMessageInternals
2345 * @typedef DBusMessageLoader
2347 * The DBusMessageLoader object encapsulates the process of converting
2348 * a byte stream into a series of DBusMessage. It buffers the incoming
2349 * bytes as efficiently as possible, and generates a queue of
2350 * messages. DBusMessageLoader is typically used as part of a
2351 * DBusTransport implementation. The DBusTransport then hands off
2352 * the loaded messages to a DBusConnection, making the messages
2353 * visible to the application.
2355 * @todo write tests for break-loader that a) randomly delete header
2356 * fields and b) set string fields to zero-length and other funky
2361 /* we definitely use signed ints for sizes, so don't exceed
2362 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2363 * over 128M is pretty nuts anyhow.
2367 * The maximum sane message size.
2369 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2372 * Implementation details of DBusMessageLoader.
2373 * All members are private.
2375 struct DBusMessageLoader
2377 int refcount; /**< Reference count. */
2379 DBusString data; /**< Buffered data */
2381 DBusList *messages; /**< Complete messages. */
2383 long max_message_size; /**< Maximum size of a message */
2385 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2387 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2391 * The initial buffer size of the message loader.
2393 * @todo this should be based on min header size plus some average
2394 * body size, or something. Or rather, the min header size only, if we
2395 * want to try to read only the header, store that in a DBusMessage,
2396 * then read only the body and store that, etc., depends on
2397 * how we optimize _dbus_message_loader_get_buffer() and what
2398 * the exact message format is.
2400 #define INITIAL_LOADER_DATA_LEN 32
2403 * Creates a new message loader. Returns #NULL if memory can't
2406 * @returns new loader, or #NULL.
2409 _dbus_message_loader_new (void)
2411 DBusMessageLoader *loader;
2413 loader = dbus_new0 (DBusMessageLoader, 1);
2417 loader->refcount = 1;
2419 /* Try to cap message size at something that won't *totally* hose
2420 * the system if we have a couple of them.
2422 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2424 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2430 /* preallocate the buffer for speed, ignore failure */
2431 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2432 _dbus_string_set_length (&loader->data, 0);
2438 * Increments the reference count of the loader.
2440 * @param loader the loader.
2443 _dbus_message_loader_ref (DBusMessageLoader *loader)
2445 loader->refcount += 1;
2449 * Decrements the reference count of the loader and finalizes the
2450 * loader when the count reaches zero.
2452 * @param loader the loader.
2455 _dbus_message_loader_unref (DBusMessageLoader *loader)
2457 loader->refcount -= 1;
2458 if (loader->refcount == 0)
2460 _dbus_list_foreach (&loader->messages,
2461 (DBusForeachFunction) dbus_message_unref,
2463 _dbus_list_clear (&loader->messages);
2464 _dbus_string_free (&loader->data);
2470 * Gets the buffer to use for reading data from the network. Network
2471 * data is read directly into an allocated buffer, which is then used
2472 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2473 * The buffer must always be returned immediately using
2474 * _dbus_message_loader_return_buffer(), even if no bytes are
2475 * successfully read.
2477 * @todo this function can be a lot more clever. For example
2478 * it can probably always return a buffer size to read exactly
2479 * the body of the next message, thus avoiding any memory wastage
2482 * @todo we need to enforce a max length on strings in header fields.
2484 * @param loader the message loader.
2485 * @param buffer the buffer
2488 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2489 DBusString **buffer)
2491 _dbus_assert (!loader->buffer_outstanding);
2493 *buffer = &loader->data;
2495 loader->buffer_outstanding = TRUE;
2499 * The smallest header size that can occur.
2500 * (It won't be valid)
2502 #define DBUS_MINIMUM_HEADER_SIZE 16
2504 /** Pack four characters as in "abcd" into a uint32 */
2505 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2506 ((((dbus_uint32_t)a) << 24) | \
2507 (((dbus_uint32_t)b) << 16) | \
2508 (((dbus_uint32_t)c) << 8) | \
2511 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2512 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2513 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2515 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2516 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2517 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2519 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2520 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2521 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2523 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2524 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2525 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2527 /* FIXME impose max length on name, srvc, sndr */
2529 decode_header_data (const DBusString *data,
2532 HeaderField fields[FIELD_LAST],
2533 int *message_padding)
2539 if (header_len < 16)
2543 while (i < FIELD_LAST)
2545 fields[i].offset = -1;
2549 fields[FIELD_HEADER_LENGTH].offset = 4;
2550 fields[FIELD_BODY_LENGTH].offset = 8;
2551 fields[FIELD_CLIENT_SERIAL].offset = 12;
2553 /* Now handle the named fields. A real named field is at least 4
2554 * bytes for the name, plus a type code (1 byte) plus padding. So
2555 * if we have less than 8 bytes left, it must be alignment padding,
2556 * not a field. While >= 8 bytes can't be entirely alignment
2560 while ((pos + 7) < header_len)
2562 pos = _DBUS_ALIGN_VALUE (pos, 4);
2564 if ((pos + 4) > header_len)
2567 _dbus_string_get_const_data_len (data, &field, pos, 4);
2570 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2572 /* I believe FROM_BE is right, but if not we'll find out
2575 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2577 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2578 if (fields[FIELD_SERVICE].offset >= 0)
2580 _dbus_verbose ("%s field provided twice\n",
2581 DBUS_HEADER_FIELD_SERVICE);
2585 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2586 _dbus_verbose ("Found service name at offset %d\n",
2587 fields[FIELD_SERVICE].offset);
2590 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2591 if (fields[FIELD_NAME].offset >= 0)
2593 _dbus_verbose ("%s field provided twice\n",
2594 DBUS_HEADER_FIELD_NAME);
2598 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2600 _dbus_verbose ("Found message name at offset %d\n",
2601 fields[FIELD_NAME].offset);
2603 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2604 if (fields[FIELD_SENDER].offset >= 0)
2606 _dbus_verbose ("%s field provided twice\n",
2607 DBUS_HEADER_FIELD_SENDER);
2611 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2613 _dbus_verbose ("Found sender name at offset %d\n",
2614 fields[FIELD_NAME].offset);
2617 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2618 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2620 _dbus_verbose ("%s field provided twice\n",
2621 DBUS_HEADER_FIELD_REPLY);
2625 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2627 _dbus_verbose ("Found reply serial at offset %d\n",
2628 fields[FIELD_REPLY_SERIAL].offset);
2632 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2633 field[0], field[1], field[2], field[3], pos);
2636 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2638 _dbus_verbose ("Failed to validate argument to named header field\n");
2642 if (new_pos > header_len)
2644 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2651 if (pos < header_len)
2653 /* Alignment padding, verify that it's nul */
2654 _dbus_assert ((header_len - pos) < 8);
2656 if (!_dbus_string_validate_nul (data,
2657 pos, (header_len - pos)))
2659 _dbus_verbose ("header alignment padding is not nul\n");
2664 if (fields[FIELD_NAME].offset < 0)
2666 _dbus_verbose ("No %s field provided\n",
2667 DBUS_HEADER_FIELD_NAME);
2671 if (message_padding)
2672 *message_padding = header_len - pos;
2678 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2679 * indicating to the loader how many bytes of the buffer were filled
2680 * in. This function must always be called, even if no bytes were
2681 * successfully read.
2683 * @todo if we run out of memory in here, we offer no way for calling
2684 * code to handle it, i.e. they can't re-run the message parsing
2685 * attempt. Perhaps much of this code could be moved to pop_message()?
2686 * But then that may need to distinguish NULL return for no messages
2687 * from NULL return for errors.
2689 * @param loader the loader.
2690 * @param buffer the buffer.
2691 * @param bytes_read number of bytes that were read into the buffer.
2694 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2698 _dbus_assert (loader->buffer_outstanding);
2699 _dbus_assert (buffer == &loader->data);
2701 loader->buffer_outstanding = FALSE;
2703 if (loader->corrupted)
2706 while (_dbus_string_get_length (&loader->data) >= 16)
2708 DBusMessage *message;
2709 const char *header_data;
2710 int byte_order, header_len, body_len, header_padding;
2711 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2713 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2715 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2717 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2719 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2720 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2721 loader->corrupted = TRUE;
2725 byte_order = header_data[0];
2727 if (byte_order != DBUS_LITTLE_ENDIAN &&
2728 byte_order != DBUS_BIG_ENDIAN)
2730 _dbus_verbose ("Message with bad byte order '%c' received\n",
2732 loader->corrupted = TRUE;
2736 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2737 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2739 if (header_len_unsigned < 16)
2741 _dbus_verbose ("Message had broken too-small header length %u\n",
2742 header_len_unsigned);
2743 loader->corrupted = TRUE;
2747 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2748 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2750 _dbus_verbose ("Header or body length too large (%u %u)\n",
2751 header_len_unsigned,
2753 loader->corrupted = TRUE;
2757 /* Now that we know the values are in signed range, get
2758 * rid of stupid unsigned, just causes bugs
2760 header_len = header_len_unsigned;
2761 body_len = body_len_unsigned;
2763 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2766 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2768 loader->corrupted = TRUE;
2772 if (header_len + body_len > loader->max_message_size)
2774 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2775 header_len, body_len, loader->max_message_size);
2776 loader->corrupted = TRUE;
2780 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2782 HeaderField fields[FIELD_LAST];
2787 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
2789 if (!decode_header_data (&loader->data, header_len, byte_order,
2790 fields, &header_padding))
2792 _dbus_verbose ("Header was invalid\n");
2793 loader->corrupted = TRUE;
2797 next_arg = header_len;
2798 while (next_arg < (header_len + body_len))
2800 int prev = next_arg;
2802 if (!_dbus_marshal_validate_arg (&loader->data,
2807 loader->corrupted = TRUE;
2811 _dbus_assert (next_arg > prev);
2814 if (next_arg > (header_len + body_len))
2816 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2817 next_arg, header_len, body_len,
2818 header_len + body_len);
2819 loader->corrupted = TRUE;
2823 message = dbus_message_new_empty_header ();
2824 if (message == NULL)
2825 break; /* ugh, postpone this I guess. */
2827 message->byte_order = byte_order;
2828 message->header_padding = header_padding;
2830 /* Copy in the offsets we found */
2832 while (i < FIELD_LAST)
2834 message->header_fields[i] = fields[i];
2838 if (!_dbus_list_append (&loader->messages, message))
2840 dbus_message_unref (message);
2844 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2845 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2847 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2848 (header_len + body_len));
2850 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2852 _dbus_list_remove_last (&loader->messages, message);
2853 dbus_message_unref (message);
2857 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2861 /* put the header back, we'll try again later */
2862 result = _dbus_string_copy_len (&message->header, 0, header_len,
2864 _dbus_assert (result); /* because DBusString never reallocs smaller */
2866 _dbus_list_remove_last (&loader->messages, message);
2867 dbus_message_unref (message);
2871 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2872 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2874 /* Fill in caches */
2875 message->reply_serial = get_int_field (message,
2876 FIELD_REPLY_SERIAL);
2877 message->client_serial = get_int_field (message,
2878 FIELD_CLIENT_SERIAL);
2880 _dbus_verbose ("Loaded message %p\n", message);
2888 * Pops a loaded message (passing ownership of the message
2889 * to the caller). Returns #NULL if no messages have been
2892 * @param loader the loader.
2893 * @returns the next message, or #NULL if none.
2896 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2898 return _dbus_list_pop_first (&loader->messages);
2903 * Checks whether the loader is confused due to bad data.
2904 * If messages are received that are invalid, the
2905 * loader gets confused and gives up permanently.
2906 * This state is called "corrupted."
2908 * @param loader the loader
2909 * @returns #TRUE if the loader is hosed.
2912 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2914 return loader->corrupted;
2918 * Sets the maximum size message we allow.
2920 * @param loader the loader
2921 * @param size the max message size in bytes
2924 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2927 if (size > MAX_SANE_MESSAGE_SIZE)
2929 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2930 size, MAX_SANE_MESSAGE_SIZE);
2931 size = MAX_SANE_MESSAGE_SIZE;
2933 loader->max_message_size = size;
2937 * Gets the maximum allowed message size in bytes.
2939 * @param loader the loader
2940 * @returns max size in bytes
2943 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2945 return loader->max_message_size;
2949 #ifdef DBUS_BUILD_TESTS
2950 #include "dbus-test.h"
2954 message_iter_test (DBusMessage *message)
2956 DBusMessageIter *iter;
2959 iter = dbus_message_get_args_iter (message);
2962 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2963 _dbus_assert_not_reached ("Argument type isn't string");
2965 str = dbus_message_iter_get_string (iter);
2966 if (strcmp (str, "Test string") != 0)
2967 _dbus_assert_not_reached ("Strings differ");
2970 if (!dbus_message_iter_next (iter))
2971 _dbus_assert_not_reached ("Reached end of arguments");
2973 /* Signed integer tests */
2974 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2975 _dbus_assert_not_reached ("Argument type isn't int32");
2977 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2978 _dbus_assert_not_reached ("Signed integers differ");
2980 if (!dbus_message_iter_next (iter))
2981 _dbus_assert_not_reached ("Reached end of fields");
2983 /* Unsigned integer tests */
2984 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2985 _dbus_assert_not_reached ("Argument type isn't int32");
2987 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
2988 _dbus_assert_not_reached ("Unsigned integers differ");
2990 if (!dbus_message_iter_next (iter))
2991 _dbus_assert_not_reached ("Reached end of arguments");
2994 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
2995 _dbus_assert_not_reached ("Argument type isn't double");
2997 if (dbus_message_iter_get_double (iter) != 3.14159)
2998 _dbus_assert_not_reached ("Doubles differ");
3000 if (dbus_message_iter_next (iter))
3001 _dbus_assert_not_reached ("Didn't reach end of arguments");
3003 dbus_message_iter_unref (iter);
3007 check_message_handling (DBusMessage *message)
3009 DBusMessageIter *iter;
3012 dbus_int32_t client_serial;
3017 client_serial = dbus_message_get_serial (message);
3019 /* can't use set_serial due to the assertions at the start of it */
3020 set_int_field (message, FIELD_CLIENT_SERIAL,
3023 if (client_serial != dbus_message_get_serial (message))
3025 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
3029 /* If we implement message_set_arg (message, n, value)
3030 * then we would want to test it here
3033 iter = dbus_message_get_args_iter (message);
3034 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
3040 case DBUS_TYPE_INT32:
3041 dbus_message_iter_get_int32 (iter);
3043 case DBUS_TYPE_UINT32:
3044 dbus_message_iter_get_uint32 (iter);
3046 case DBUS_TYPE_DOUBLE:
3047 dbus_message_iter_get_double (iter);
3049 case DBUS_TYPE_STRING:
3052 str = dbus_message_iter_get_string (iter);
3056 case DBUS_TYPE_INT32_ARRAY:
3058 dbus_int32_t *values;
3061 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
3067 case DBUS_TYPE_UINT32_ARRAY:
3069 dbus_uint32_t *values;
3072 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
3078 case DBUS_TYPE_DOUBLE_ARRAY:
3083 if (!dbus_message_iter_get_double_array (iter, &values, &len))
3089 case DBUS_TYPE_STRING_ARRAY:
3094 if (!dbus_message_iter_get_string_array (iter, &values, &len))
3097 dbus_free_string_array (values);
3101 case DBUS_TYPE_DICT:
3105 if (!dbus_message_iter_get_dict (iter, &dict))
3108 dbus_dict_unref (dict);
3116 if (!dbus_message_iter_next (iter))
3124 dbus_message_iter_unref (iter);
3130 check_have_valid_message (DBusMessageLoader *loader)
3132 DBusMessage *message;
3138 if (_dbus_message_loader_get_is_corrupted (loader))
3140 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
3144 message = _dbus_message_loader_pop_message (loader);
3145 if (message == NULL)
3147 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3151 if (_dbus_string_get_length (&loader->data) > 0)
3153 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3157 /* Verify that we're able to properly deal with the message.
3158 * For example, this would detect improper handling of messages
3159 * in nonstandard byte order.
3161 if (!check_message_handling (message))
3168 dbus_message_unref (message);
3174 check_invalid_message (DBusMessageLoader *loader)
3180 if (!_dbus_message_loader_get_is_corrupted (loader))
3182 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3193 check_incomplete_message (DBusMessageLoader *loader)
3195 DBusMessage *message;
3201 if (_dbus_message_loader_get_is_corrupted (loader))
3203 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3207 message = _dbus_message_loader_pop_message (loader);
3208 if (message != NULL)
3210 _dbus_warn ("loaded message that was expected to be incomplete\n");
3218 dbus_message_unref (message);
3223 check_loader_results (DBusMessageLoader *loader,
3224 DBusMessageValidity validity)
3228 case _DBUS_MESSAGE_VALID:
3229 return check_have_valid_message (loader);
3230 case _DBUS_MESSAGE_INVALID:
3231 return check_invalid_message (loader);
3232 case _DBUS_MESSAGE_INCOMPLETE:
3233 return check_incomplete_message (loader);
3234 case _DBUS_MESSAGE_UNKNOWN:
3238 _dbus_assert_not_reached ("bad DBusMessageValidity");
3244 * Loads the message in the given message file.
3246 * @param filename filename to load
3247 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3248 * @param data string to load message into
3249 * @returns #TRUE if the message was loaded
3252 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3264 dbus_error_init (&error);
3265 if (!_dbus_file_get_contents (data, filename, &error))
3268 _dbus_string_get_const_data (filename, &s);
3269 _dbus_warn ("Could not load message file %s: %s\n", s, error.message);
3270 dbus_error_free (&error);
3276 if (!_dbus_message_data_load (data, filename))
3279 _dbus_string_get_const_data (filename, &s);
3280 _dbus_warn ("Could not load message file %s\n", s);
3293 * Tries loading the message in the given message file
3294 * and verifies that DBusMessageLoader can handle it.
3296 * @param filename filename to load
3297 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3298 * @param expected_validity what the message has to be like to return #TRUE
3299 * @returns #TRUE if the message has the expected validity
3302 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3304 DBusMessageValidity expected_validity)
3311 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3312 _dbus_assert_not_reached ("could not allocate string\n");
3314 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3318 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3326 if (_dbus_string_get_length (&data) > 0)
3327 _dbus_verbose_bytes_of_string (&data, 0,
3328 _dbus_string_get_length (&data));
3330 _dbus_string_get_const_data (filename, &s);
3331 _dbus_warn ("Failed message loader test on %s\n",
3335 _dbus_string_free (&data);
3341 * Tries loading the given message data.
3344 * @param data the message data
3345 * @param expected_validity what the message has to be like to return #TRUE
3346 * @returns #TRUE if the message has the expected validity
3349 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3350 DBusMessageValidity expected_validity)
3352 DBusMessageLoader *loader;
3360 /* Write the data one byte at a time */
3362 loader = _dbus_message_loader_new ();
3364 len = _dbus_string_get_length (data);
3365 for (i = 0; i < len; i++)
3369 _dbus_message_loader_get_buffer (loader, &buffer);
3370 _dbus_string_append_byte (buffer,
3371 _dbus_string_get_byte (data, i));
3372 _dbus_message_loader_return_buffer (loader, buffer, 1);
3375 if (!check_loader_results (loader, expected_validity))
3378 _dbus_message_loader_unref (loader);
3381 /* Write the data all at once */
3383 loader = _dbus_message_loader_new ();
3388 _dbus_message_loader_get_buffer (loader, &buffer);
3389 _dbus_string_copy (data, 0, buffer,
3390 _dbus_string_get_length (buffer));
3391 _dbus_message_loader_return_buffer (loader, buffer, 1);
3394 if (!check_loader_results (loader, expected_validity))
3397 _dbus_message_loader_unref (loader);
3400 /* Write the data 2 bytes at a time */
3402 loader = _dbus_message_loader_new ();
3404 len = _dbus_string_get_length (data);
3405 for (i = 0; i < len; i += 2)
3409 _dbus_message_loader_get_buffer (loader, &buffer);
3410 _dbus_string_append_byte (buffer,
3411 _dbus_string_get_byte (data, i));
3413 _dbus_string_append_byte (buffer,
3414 _dbus_string_get_byte (data, i+1));
3415 _dbus_message_loader_return_buffer (loader, buffer, 1);
3418 if (!check_loader_results (loader, expected_validity))
3421 _dbus_message_loader_unref (loader);
3429 _dbus_message_loader_unref (loader);
3435 process_test_subdir (const DBusString *test_base_dir,
3437 DBusMessageValidity validity,
3438 DBusForeachMessageFileFunc function,
3441 DBusString test_directory;
3442 DBusString filename;
3450 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3451 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3453 _dbus_string_init_const (&filename, subdir);
3455 if (!_dbus_string_copy (test_base_dir, 0,
3456 &test_directory, 0))
3457 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3459 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3460 _dbus_assert_not_reached ("couldn't allocate full path");
3462 _dbus_string_free (&filename);
3463 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3464 _dbus_assert_not_reached ("didn't allocate filename string\n");
3466 dbus_error_init (&error);
3467 dir = _dbus_directory_open (&test_directory, &error);
3471 _dbus_string_get_const_data (&test_directory, &s);
3472 _dbus_warn ("Could not open %s: %s\n", s,
3474 dbus_error_free (&error);
3478 printf ("Testing:\n");
3481 while (_dbus_directory_get_next_file (dir, &filename, &error))
3483 DBusString full_path;
3486 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3487 _dbus_assert_not_reached ("couldn't init string");
3489 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3490 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3492 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3493 _dbus_assert_not_reached ("couldn't concat file to dir");
3495 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3497 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3501 const char *filename_c;
3502 _dbus_string_get_const_data (&filename, &filename_c);
3503 _dbus_verbose ("Skipping non-.message file %s\n",
3505 _dbus_string_free (&full_path);
3511 _dbus_string_get_const_data (&filename, &s);
3512 printf (" %s\n", s);
3515 _dbus_verbose (" expecting %s\n",
3516 validity == _DBUS_MESSAGE_VALID ? "valid" :
3517 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3518 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3520 if (! (*function) (&full_path, is_raw, validity, user_data))
3522 _dbus_string_free (&full_path);
3526 _dbus_string_free (&full_path);
3529 if (dbus_error_is_set (&error))
3532 _dbus_string_get_const_data (&test_directory, &s);
3533 _dbus_warn ("Could not get next file in %s: %s\n",
3535 dbus_error_free (&error);
3544 _dbus_directory_close (dir);
3545 _dbus_string_free (&test_directory);
3546 _dbus_string_free (&filename);
3552 * Runs the given function on every message file in the test suite.
3553 * The function should return #FALSE on test failure or fatal error.
3555 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3556 * @param func the function to run
3557 * @param user_data data for function
3558 * @returns #FALSE if there's a failure
3561 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3562 DBusForeachMessageFileFunc func,
3565 DBusString test_directory;
3570 _dbus_string_init_const (&test_directory, test_data_dir);
3572 if (!process_test_subdir (&test_directory, "valid-messages",
3573 _DBUS_MESSAGE_VALID, func, user_data))
3576 if (!process_test_subdir (&test_directory, "invalid-messages",
3577 _DBUS_MESSAGE_INVALID, func, user_data))
3580 if (!process_test_subdir (&test_directory, "incomplete-messages",
3581 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3588 _dbus_string_free (&test_directory);
3594 * @ingroup DBusMessageInternals
3595 * Unit test for DBusMessage.
3597 * @returns #TRUE on success.
3600 _dbus_message_test (const char *test_data_dir)
3602 DBusMessage *message;
3603 DBusMessageLoader *loader;
3606 dbus_int32_t our_int;
3609 dbus_bool_t our_bool;
3611 /* Test the vararg functions */
3612 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3613 _dbus_message_set_serial (message, 1);
3614 dbus_message_append_args (message,
3615 DBUS_TYPE_INT32, -0x12345678,
3616 DBUS_TYPE_STRING, "Test string",
3617 DBUS_TYPE_DOUBLE, 3.14159,
3618 DBUS_TYPE_BOOLEAN, TRUE,
3620 _dbus_verbose_bytes_of_string (&message->header, 0,
3621 _dbus_string_get_length (&message->header));
3622 _dbus_verbose_bytes_of_string (&message->body, 0,
3623 _dbus_string_get_length (&message->body));
3625 if (!dbus_message_get_args (message, NULL,
3626 DBUS_TYPE_INT32, &our_int,
3627 DBUS_TYPE_STRING, &our_str,
3628 DBUS_TYPE_DOUBLE, &our_double,
3629 DBUS_TYPE_BOOLEAN, &our_bool,
3631 _dbus_assert_not_reached ("Could not get arguments");
3633 if (our_int != -0x12345678)
3634 _dbus_assert_not_reached ("integers differ!");
3636 if (our_double != 3.14159)
3637 _dbus_assert_not_reached ("doubles differ!");
3639 if (strcmp (our_str, "Test string") != 0)
3640 _dbus_assert_not_reached ("strings differ!");
3643 _dbus_assert_not_reached ("booleans differ");
3645 dbus_free (our_str);
3646 dbus_message_unref (message);
3648 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3649 _dbus_message_set_serial (message, 1);
3650 dbus_message_set_reply_serial (message, 0x12345678);
3652 dbus_message_append_string (message, "Test string");
3653 dbus_message_append_int32 (message, -0x12345678);
3654 dbus_message_append_uint32 (message, 0xedd1e);
3655 dbus_message_append_double (message, 3.14159);
3657 message_iter_test (message);
3659 /* Message loader test */
3660 _dbus_message_lock (message);
3661 loader = _dbus_message_loader_new ();
3663 /* Write the header data one byte at a time */
3664 _dbus_string_get_const_data (&message->header, &data);
3665 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3669 _dbus_message_loader_get_buffer (loader, &buffer);
3670 _dbus_string_append_byte (buffer, data[i]);
3671 _dbus_message_loader_return_buffer (loader, buffer, 1);
3674 /* Write the body data one byte at a time */
3675 _dbus_string_get_const_data (&message->body, &data);
3676 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3680 _dbus_message_loader_get_buffer (loader, &buffer);
3681 _dbus_string_append_byte (buffer, data[i]);
3682 _dbus_message_loader_return_buffer (loader, buffer, 1);
3685 dbus_message_unref (message);
3687 /* Now pop back the message */
3688 if (_dbus_message_loader_get_is_corrupted (loader))
3689 _dbus_assert_not_reached ("message loader corrupted");
3691 message = _dbus_message_loader_pop_message (loader);
3693 _dbus_assert_not_reached ("received a NULL message");
3695 if (dbus_message_get_reply_serial (message) != 0x12345678)
3696 _dbus_assert_not_reached ("reply serial fields differ");
3698 message_iter_test (message);
3700 dbus_message_unref (message);
3701 _dbus_message_loader_unref (loader);
3703 /* Now load every message in test_data_dir if we have one */
3704 if (test_data_dir == NULL)
3707 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3708 (DBusForeachMessageFileFunc)
3709 dbus_internal_do_not_use_try_message_file,
3713 #endif /* DBUS_BUILD_TESTS */