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 data = _dbus_string_get_const_data (&message->header);
214 return data + (offset + 4);
218 get_int_field (DBusMessage *message,
223 _dbus_assert (field < FIELD_LAST);
225 offset = message->header_fields[field].offset;
228 return -1; /* useless if -1 is a valid value of course */
230 return _dbus_demarshal_int32 (&message->header,
237 append_int_field (DBusMessage *message,
244 _dbus_assert (!message->locked);
246 clear_header_padding (message);
248 orig_len = _dbus_string_get_length (&message->header);
250 if (!_dbus_string_align_length (&message->header, 4))
253 if (!_dbus_string_append_len (&message->header, name, 4))
256 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
259 if (!_dbus_string_align_length (&message->header, 4))
262 message->header_fields[FIELD_REPLY_SERIAL].offset =
263 _dbus_string_get_length (&message->header);
265 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
269 if (!append_header_padding (message))
275 message->header_fields[field].offset = -1;
276 _dbus_string_set_length (&message->header, orig_len);
278 /* this must succeed because it was allocated on function entry and
279 * DBusString doesn't ever realloc smaller
281 if (!append_header_padding (message))
282 _dbus_assert_not_reached ("failed to reappend header padding");
287 append_string_field (DBusMessage *message,
294 _dbus_assert (!message->locked);
296 clear_header_padding (message);
298 orig_len = _dbus_string_get_length (&message->header);
300 if (!_dbus_string_align_length (&message->header, 4))
303 if (!_dbus_string_append_len (&message->header, name, 4))
306 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
309 if (!_dbus_string_align_length (&message->header, 4))
312 message->header_fields[field].offset =
313 _dbus_string_get_length (&message->header);
315 if (!_dbus_marshal_string (&message->header, message->byte_order,
319 if (!append_header_padding (message))
325 message->header_fields[field].offset = -1;
326 _dbus_string_set_length (&message->header, orig_len);
328 /* this must succeed because it was allocated on function entry and
329 * DBusString doesn't ever realloc smaller
331 if (!append_header_padding (message))
332 _dbus_assert_not_reached ("failed to reappend header padding");
338 delete_int_field (DBusMessage *message,
341 int offset = message->header_fields[field].offset;
343 _dbus_assert (!message->locked);
344 _dbus_assert (field_is_named[field]);
349 clear_header_padding (message);
351 /* The field typecode and name take up 8 bytes */
352 _dbus_string_delete (&message->header,
356 message->header_fields[field].offset = -1;
358 adjust_field_offsets (message,
362 append_header_padding (message);
366 delete_string_field (DBusMessage *message,
369 int offset = message->header_fields[field].offset;
373 _dbus_assert (!message->locked);
374 _dbus_assert (field_is_named[field]);
379 clear_header_padding (message);
381 get_string_field (message, field, &len);
383 /* The field typecode and name take up 8 bytes, and the nul
384 * termination is 1 bytes, string length integer is 4 bytes
386 delete_len = 8 + 4 + 1 + len;
388 _dbus_string_delete (&message->header,
392 message->header_fields[field].offset = -1;
394 adjust_field_offsets (message,
398 append_header_padding (message);
402 set_int_field (DBusMessage *message,
406 int offset = message->header_fields[field].offset;
408 _dbus_assert (!message->locked);
412 /* need to append the field */
416 case FIELD_REPLY_SERIAL:
417 return append_int_field (message, field,
418 DBUS_HEADER_FIELD_REPLY,
421 _dbus_assert_not_reached ("appending an int field we don't support appending");
427 _dbus_marshal_set_int32 (&message->header,
436 set_uint_field (DBusMessage *message,
440 int offset = message->header_fields[field].offset;
442 _dbus_assert (!message->locked);
446 /* need to append the field */
451 _dbus_assert_not_reached ("appending a uint field we don't support appending");
457 _dbus_marshal_set_uint32 (&message->header,
466 set_string_field (DBusMessage *message,
470 int offset = message->header_fields[field].offset;
472 _dbus_assert (!message->locked);
473 _dbus_assert (value != NULL);
477 /* need to append the field */
482 return append_string_field (message, field,
483 DBUS_HEADER_FIELD_SENDER,
486 _dbus_assert_not_reached ("appending a string field we don't support appending");
497 clear_header_padding (message);
499 old_len = _dbus_string_get_length (&message->header);
501 len = strlen (value);
503 _dbus_string_init_const_len (&v, value,
504 len + 1); /* include nul */
505 if (!_dbus_marshal_set_string (&message->header,
511 new_len = _dbus_string_get_length (&message->header);
513 adjust_field_offsets (message,
517 if (!append_header_padding (message))
523 /* this must succeed because it was allocated on function entry and
524 * DBusString doesn't ever realloc smaller
526 if (!append_header_padding (message))
527 _dbus_assert_not_reached ("failed to reappend header padding");
534 * Sets the serial number of a message.
535 * This can only be done once on a message.
537 * @param message the message
538 * @param serial the serial
541 _dbus_message_set_serial (DBusMessage *message,
544 _dbus_assert (!message->locked);
545 _dbus_assert (dbus_message_get_serial (message) < 0);
547 set_int_field (message, FIELD_CLIENT_SERIAL,
549 message->client_serial = serial;
553 * Sets the reply serial of a message (the client serial
554 * of the message this is a reply to).
556 * @param message the message
557 * @param reply_serial the client serial
558 * @returns #FALSE if not enough memory
561 dbus_message_set_reply_serial (DBusMessage *message,
562 dbus_int32_t reply_serial)
564 _dbus_assert (!message->locked);
566 if (set_int_field (message, FIELD_REPLY_SERIAL,
569 message->reply_serial = reply_serial;
577 * Returns the serial of a message or -1 if none has been specified.
578 * The message's serial number is provided by the application sending
579 * the message and is used to identify replies to this message.
581 * @param message the message
582 * @returns the client serial
585 dbus_message_get_serial (DBusMessage *message)
587 return message->client_serial;
591 * Returns the serial that the message is
592 * a reply to or -1 if none.
594 * @param message the message
595 * @returns the reply serial
598 dbus_message_get_reply_serial (DBusMessage *message)
600 return message->reply_serial;
604 * Adds a counter to be incremented immediately with the
605 * size of this message, and decremented by the size
606 * of this message when this message if finalized.
608 * @param message the message
609 * @param counter the counter
612 _dbus_message_add_size_counter (DBusMessage *message,
613 DBusCounter *counter)
615 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
616 * counters instead of just one
619 message->size_counter = counter;
620 _dbus_counter_ref (message->size_counter);
622 /* When we can change message size, we may want to
623 * update this each time we do so, or we may want to
624 * just KISS like this.
626 message->size_counter_delta =
627 _dbus_string_get_length (&message->header) +
628 _dbus_string_get_length (&message->body);
631 _dbus_verbose ("message has size %ld\n",
632 message->size_counter_delta);
635 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
639 dbus_message_create_header (DBusMessage *message,
645 if (!_dbus_string_append_byte (&message->header, message->byte_order))
649 if (!_dbus_string_append_byte (&message->header, flags))
652 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
655 if (!_dbus_string_append_byte (&message->header, 0))
658 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
659 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
662 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
663 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
666 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
667 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
670 /* Marshal message service */
673 if (!append_string_field (message,
675 DBUS_HEADER_FIELD_SERVICE,
680 _dbus_assert (name != NULL);
681 if (!append_string_field (message,
683 DBUS_HEADER_FIELD_NAME,
691 * Locks a message. Allows checking that applications don't keep a
692 * reference to a message in the outgoing queue and change it
693 * underneath us. Messages are locked when they enter the outgoing
694 * queue (dbus_connection_send_message()), and the library complains
695 * if the message is modified while locked.
697 * @param message the message to lock.
700 _dbus_message_lock (DBusMessage *message)
702 if (!message->locked)
704 /* Fill in our lengths */
705 set_uint_field (message,
707 _dbus_string_get_length (&message->header));
709 set_uint_field (message,
711 _dbus_string_get_length (&message->body));
713 message->locked = TRUE;
720 * @defgroup DBusMessage DBusMessage
722 * @brief Message to be sent or received over a DBusConnection.
724 * A DBusMessage is the most basic unit of communication over a
725 * DBusConnection. A DBusConnection represents a stream of messages
726 * received from a remote application, and a stream of messages
727 * sent to a remote application.
733 * @typedef DBusMessage
735 * Opaque data type representing a message received from or to be
736 * sent to another application.
740 dbus_message_new_empty_header (void)
742 DBusMessage *message;
745 message = dbus_new0 (DBusMessage, 1);
749 message->refcount = 1;
750 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
751 message->client_serial = -1;
752 message->reply_serial = -1;
755 while (i < FIELD_LAST)
757 message->header_fields[i].offset = -1;
761 if (!_dbus_string_init (&message->header))
767 if (!_dbus_string_init (&message->body))
769 _dbus_string_free (&message->header);
779 * Constructs a new message. Returns #NULL if memory can't be
780 * allocated for the message. The service may be #NULL in which case
781 * no service is set; this is appropriate when using D-BUS in a
782 * peer-to-peer context (no message bus).
784 * @todo reverse the arguments, first 'name' then 'service'
785 * as 'name' is more fundamental
787 * @param service service that the message should be sent to or #NULL
788 * @param name name of the message
789 * @returns a new DBusMessage, free with dbus_message_unref()
790 * @see dbus_message_unref()
793 dbus_message_new (const char *service,
796 DBusMessage *message;
798 message = dbus_message_new_empty_header ();
802 if (!dbus_message_create_header (message, service, name))
804 dbus_message_unref (message);
812 * Constructs a message that is a reply to some other
813 * message. Returns #NULL if memory can't be allocated
816 * @param original_message the message which the created
817 * message is a reply to.
818 * @returns a new DBusMessage, free with dbus_message_unref()
819 * @see dbus_message_new(), dbus_message_unref()
822 dbus_message_new_reply (DBusMessage *original_message)
824 DBusMessage *message;
825 const char *sender, *name;
827 sender = get_string_field (original_message,
829 name = get_string_field (original_message,
832 /* sender is allowed to be null here in peer-to-peer case */
834 message = dbus_message_new (sender, name);
839 if (!dbus_message_set_reply_serial (message,
840 dbus_message_get_serial (original_message)))
842 dbus_message_unref (message);
850 * Creates a new message that is an error reply to a certain message.
852 * @param original_message the original message
853 * @param error_name the error name
854 * @param error_message the error message string
855 * @returns a new error message
858 dbus_message_new_error_reply (DBusMessage *original_message,
859 const char *error_name,
860 const char *error_message)
862 DBusMessage *message;
865 sender = get_string_field (original_message,
868 _dbus_assert (sender != NULL);
870 message = dbus_message_new (sender, error_name);
875 if (!dbus_message_set_reply_serial (message,
876 dbus_message_get_serial (original_message)))
878 dbus_message_unref (message);
882 if (!dbus_message_append_string (message, error_message))
884 dbus_message_unref (message);
888 dbus_message_set_is_error (message, TRUE);
894 * Creates a new message that is an exact replica of the message
895 * specified, except that its refcount is set to 1.
897 * @param message the message.
898 * @returns the new message.
901 dbus_message_copy (const DBusMessage *message)
906 retval = dbus_new0 (DBusMessage, 1);
910 retval->refcount = 1;
911 retval->byte_order = message->byte_order;
912 retval->client_serial = message->client_serial;
913 retval->reply_serial = message->reply_serial;
914 retval->header_padding = message->header_padding;
915 retval->locked = FALSE;
917 if (!_dbus_string_init (&retval->header))
923 if (!_dbus_string_init (&retval->body))
925 _dbus_string_free (&retval->header);
930 if (!_dbus_string_copy (&message->header, 0,
933 _dbus_string_free (&retval->header);
934 _dbus_string_free (&retval->body);
940 if (!_dbus_string_copy (&message->body, 0,
943 _dbus_string_free (&retval->header);
944 _dbus_string_free (&retval->body);
950 for (i = 0; i < FIELD_LAST; i++)
952 retval->header_fields[i].offset = message->header_fields[i].offset;
960 * Increments the reference count of a DBusMessage.
962 * @param message The message
963 * @see dbus_message_unref
966 dbus_message_ref (DBusMessage *message)
968 dbus_atomic_t refcount;
970 refcount = _dbus_atomic_inc (&message->refcount);
971 _dbus_assert (refcount > 1);
975 * Decrements the reference count of a DBusMessage.
977 * @param message The message
978 * @see dbus_message_ref
981 dbus_message_unref (DBusMessage *message)
983 dbus_atomic_t refcount;
985 refcount = _dbus_atomic_dec (&message->refcount);
987 _dbus_assert (refcount >= 0);
991 if (message->size_counter != NULL)
993 _dbus_counter_adjust (message->size_counter,
994 - message->size_counter_delta);
995 _dbus_counter_unref (message->size_counter);
998 _dbus_string_free (&message->header);
999 _dbus_string_free (&message->body);
1001 dbus_free (message);
1006 * Gets the name of a message.
1008 * @param message the message
1009 * @returns the message name (should not be freed)
1012 dbus_message_get_name (DBusMessage *message)
1014 return get_string_field (message, FIELD_NAME, NULL);
1018 * Gets the destination service of a message.
1020 * @todo I think if we have set_sender/get_sender,
1021 * this function might be better named set_destination/
1022 * get_destination for clarity, as the sender
1023 * is also a service name.
1025 * @param message the message
1026 * @returns the message destination service (should not be freed)
1029 dbus_message_get_service (DBusMessage *message)
1031 return get_string_field (message, FIELD_SERVICE, NULL);
1035 * Appends fields to a message given a variable argument
1036 * list. The variable argument list should contain the type
1037 * of the argument followed by the value to add. Array values
1038 * are specified by a pointer to the array followed by an int
1039 * giving the length of the array. The list is terminated
1042 * @param message the message
1043 * @param first_arg_type type of the first argument
1044 * @param ... value of first argument, list of additional type-value pairs
1045 * @returns #TRUE on success
1048 dbus_message_append_args (DBusMessage *message,
1055 va_start (var_args, first_arg_type);
1056 retval = dbus_message_append_args_valist (message,
1065 * This function takes a va_list for use by language bindings
1067 * @see dbus_message_append_args.
1068 * @param message the message
1069 * @param first_arg_type type of first argument
1070 * @param var_args value of first argument, then list of type/value pairs
1071 * @returns #TRUE on success
1074 dbus_message_append_args_valist (DBusMessage *message,
1080 old_len = _dbus_string_get_length (&message->body);
1082 type = first_arg_type;
1089 if (!dbus_message_append_nil (message))
1091 case DBUS_TYPE_BOOLEAN:
1092 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1095 case DBUS_TYPE_INT32:
1096 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1099 case DBUS_TYPE_UINT32:
1100 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1103 case DBUS_TYPE_DOUBLE:
1104 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1107 case DBUS_TYPE_STRING:
1108 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1111 case DBUS_TYPE_BOOLEAN_ARRAY:
1114 unsigned char *data;
1116 data = va_arg (var_args, unsigned char *);
1117 len = va_arg (var_args, int);
1119 if (!dbus_message_append_boolean_array (message, data, len))
1123 case DBUS_TYPE_INT32_ARRAY:
1128 data = va_arg (var_args, dbus_int32_t *);
1129 len = va_arg (var_args, int);
1131 if (!dbus_message_append_int32_array (message, data, len))
1135 case DBUS_TYPE_UINT32_ARRAY:
1138 dbus_uint32_t *data;
1140 data = va_arg (var_args, dbus_uint32_t *);
1141 len = va_arg (var_args, int);
1143 if (!dbus_message_append_uint32_array (message, data, len))
1147 case DBUS_TYPE_DOUBLE_ARRAY:
1152 data = va_arg (var_args, double *);
1153 len = va_arg (var_args, int);
1155 if (!dbus_message_append_double_array (message, data, len))
1159 case DBUS_TYPE_BYTE_ARRAY:
1162 unsigned char *data;
1164 data = va_arg (var_args, unsigned char *);
1165 len = va_arg (var_args, int);
1167 if (!dbus_message_append_byte_array (message, data, len))
1171 case DBUS_TYPE_STRING_ARRAY:
1176 data = va_arg (var_args, const char **);
1177 len = va_arg (var_args, int);
1179 if (!dbus_message_append_string_array (message, data, len))
1183 case DBUS_TYPE_DICT:
1187 dict = va_arg (var_args, DBusDict *);
1189 if (!dbus_message_append_dict (message, dict))
1194 _dbus_warn ("Unknown field type %d\n", type);
1197 type = va_arg (var_args, int);
1207 * Appends a nil value to the message
1209 * @param message the message
1210 * @returns #TRUE on success
1213 dbus_message_append_nil (DBusMessage *message)
1215 _dbus_assert (!message->locked);
1217 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1224 * Appends a boolean value to the message
1226 * @param message the message
1227 * @param value the boolean value
1228 * @returns #TRUE on success
1231 dbus_message_append_boolean (DBusMessage *message,
1234 _dbus_assert (!message->locked);
1236 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1239 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1241 _dbus_string_shorten (&message->body, 1);
1249 * Appends a 32 bit signed integer to the message.
1251 * @param message the message
1252 * @param value the integer value
1253 * @returns #TRUE on success
1256 dbus_message_append_int32 (DBusMessage *message,
1259 _dbus_assert (!message->locked);
1261 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1264 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1266 _dbus_string_shorten (&message->body, 1);
1274 * Appends a 32 bit unsigned integer to the message.
1276 * @param message the message
1277 * @param value the integer value
1278 * @returns #TRUE on success
1281 dbus_message_append_uint32 (DBusMessage *message,
1282 dbus_uint32_t value)
1284 _dbus_assert (!message->locked);
1286 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1289 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1291 _dbus_string_shorten (&message->body, 1);
1299 * Appends a double value to the message.
1301 * @param message the message
1302 * @param value the double value
1303 * @returns #TRUE on success
1306 dbus_message_append_double (DBusMessage *message,
1309 _dbus_assert (!message->locked);
1311 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1314 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1316 _dbus_string_shorten (&message->body, 1);
1324 * Appends a UTF-8 string to the message.
1326 * @param message the message
1327 * @param value the string
1328 * @returns #TRUE on success
1331 dbus_message_append_string (DBusMessage *message,
1334 _dbus_assert (!message->locked);
1336 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1339 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1341 _dbus_string_shorten (&message->body, 1);
1349 * Appends a boolean array to the message.
1351 * @param message the message
1352 * @param value the array
1353 * @param len the length of the array
1354 * @returns #TRUE on success
1357 dbus_message_append_boolean_array (DBusMessage *message,
1358 unsigned const char *value,
1361 _dbus_assert (!message->locked);
1363 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1366 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1368 _dbus_string_shorten (&message->body, 1);
1376 * Appends a 32 bit signed integer array to the message.
1378 * @param message the message
1379 * @param value the array
1380 * @param len the length of the array
1381 * @returns #TRUE on success
1384 dbus_message_append_int32_array (DBusMessage *message,
1385 const dbus_int32_t *value,
1388 _dbus_assert (!message->locked);
1390 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1393 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1396 _dbus_string_shorten (&message->body, 1);
1404 * Appends a 32 bit unsigned integer array to the message.
1406 * @param message the message
1407 * @param value the array
1408 * @param len the length of the array
1409 * @returns #TRUE on success
1412 dbus_message_append_uint32_array (DBusMessage *message,
1413 const dbus_uint32_t *value,
1416 _dbus_assert (!message->locked);
1418 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1421 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1424 _dbus_string_shorten (&message->body, 1);
1432 * Appends a double array to the message.
1434 * @param message the message
1435 * @param value the array
1436 * @param len the length of the array
1437 * @returns #TRUE on success
1440 dbus_message_append_double_array (DBusMessage *message,
1441 const double *value,
1444 _dbus_assert (!message->locked);
1446 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1449 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1452 _dbus_string_shorten (&message->body, 1);
1460 * Appends a byte array to the message.
1462 * @param message the message
1463 * @param value the array
1464 * @param len the length of the array
1465 * @returns #TRUE on success
1468 dbus_message_append_byte_array (DBusMessage *message,
1469 unsigned const char *value,
1472 _dbus_assert (!message->locked);
1474 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1477 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1479 _dbus_string_shorten (&message->body, 1);
1487 * Appends a string array to the message.
1489 * @param message the message
1490 * @param value the array
1491 * @param len the length of the array
1492 * @returns #TRUE on success
1495 dbus_message_append_string_array (DBusMessage *message,
1499 _dbus_assert (!message->locked);
1501 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1504 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1505 (const char **)value, len))
1507 _dbus_string_shorten (&message->body, 1);
1515 * Appends a dict to the message.
1517 * @param message the message
1518 * @param dict the dict
1519 * @returns #TRUE on success
1522 dbus_message_append_dict (DBusMessage *message,
1525 _dbus_assert (!message->locked);
1527 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DICT))
1530 if (!_dbus_marshal_dict (&message->body, message->byte_order, dict))
1532 _dbus_string_shorten (&message->body, 1);
1540 * Gets arguments from a message given a variable argument list.
1541 * The variable argument list should contain the type of the
1542 * argumen followed by a pointer to where the value should be
1543 * stored. The list is terminated with 0.
1545 * @param message the message
1546 * @param error error to be filled in on failure
1547 * @param first_arg_type the first argument type
1548 * @param ... location for first argument value, then list of type-location pairs
1549 * @returns #FALSE if the error was set
1552 dbus_message_get_args (DBusMessage *message,
1560 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1562 va_start (var_args, first_arg_type);
1563 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1570 * This function takes a va_list for use by language bindings
1572 * @todo this function (or some lower-level non-convenience function)
1573 * needs better error handling; should allow the application to
1574 * distinguish between out of memory, and bad data from the remote
1575 * app. It also needs to not leak a bunch of args when it gets
1576 * to the arg that's bad, as that would be a security hole
1577 * (allow one app to force another to leak memory)
1579 * @todo We need to free the argument data when an error occurs.
1581 * @see dbus_message_get_args
1582 * @param message the message
1583 * @param error error to be filled in
1584 * @param first_arg_type type of the first argument
1585 * @param var_args return location for first argument, followed by list of type/location pairs
1586 * @returns #FALSE if error was set
1589 dbus_message_get_args_valist (DBusMessage *message,
1594 int spec_type, msg_type, i;
1595 DBusMessageIter *iter;
1598 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1600 iter = dbus_message_get_args_iter (message);
1604 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1610 spec_type = first_arg_type;
1613 while (spec_type != 0)
1615 msg_type = dbus_message_iter_get_arg_type (iter);
1617 if (msg_type != spec_type)
1619 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1620 "Argument %d is specified to be of type \"%s\", but "
1621 "is actually of type \"%s\"\n", i,
1622 _dbus_type_to_string (spec_type),
1623 _dbus_type_to_string (msg_type));
1632 case DBUS_TYPE_BOOLEAN:
1636 ptr = va_arg (var_args, dbus_bool_t *);
1638 *ptr = dbus_message_iter_get_boolean (iter);
1641 case DBUS_TYPE_INT32:
1645 ptr = va_arg (var_args, dbus_int32_t *);
1647 *ptr = dbus_message_iter_get_int32 (iter);
1650 case DBUS_TYPE_UINT32:
1654 ptr = va_arg (var_args, dbus_uint32_t *);
1656 *ptr = dbus_message_iter_get_uint32 (iter);
1660 case DBUS_TYPE_DOUBLE:
1664 ptr = va_arg (var_args, double *);
1666 *ptr = dbus_message_iter_get_double (iter);
1670 case DBUS_TYPE_STRING:
1674 ptr = va_arg (var_args, char **);
1676 *ptr = dbus_message_iter_get_string (iter);
1680 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1687 case DBUS_TYPE_BOOLEAN_ARRAY:
1689 unsigned char **ptr;
1692 ptr = va_arg (var_args, unsigned char **);
1693 len = va_arg (var_args, int *);
1695 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1697 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1703 case DBUS_TYPE_INT32_ARRAY:
1708 ptr = va_arg (var_args, dbus_int32_t **);
1709 len = va_arg (var_args, int *);
1711 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1713 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1720 case DBUS_TYPE_UINT32_ARRAY:
1722 dbus_uint32_t **ptr;
1725 ptr = va_arg (var_args, dbus_uint32_t **);
1726 len = va_arg (var_args, int *);
1728 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1730 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1737 case DBUS_TYPE_DOUBLE_ARRAY:
1742 ptr = va_arg (var_args, double **);
1743 len = va_arg (var_args, int *);
1745 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1747 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1753 case DBUS_TYPE_BYTE_ARRAY:
1755 unsigned char **ptr;
1758 ptr = va_arg (var_args, unsigned char **);
1759 len = va_arg (var_args, int *);
1761 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1763 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1768 case DBUS_TYPE_STRING_ARRAY:
1773 ptr = va_arg (var_args, char ***);
1774 len = va_arg (var_args, int *);
1776 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1778 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1783 case DBUS_TYPE_DICT:
1787 dict = va_arg (var_args, DBusDict **);
1789 if (!dbus_message_iter_get_dict (iter, dict))
1791 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1797 _dbus_warn ("Unknown field type %d\n", spec_type);
1800 spec_type = va_arg (var_args, int);
1801 if (spec_type != 0 && !dbus_message_iter_next (iter))
1803 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1804 "Message has only %d arguments, but more were expected", i);
1814 dbus_message_iter_unref (iter);
1819 * Returns a DBusMessageIter representing the arguments of the
1820 * message passed in.
1822 * @todo IMO the message iter should follow the GtkTextIter pattern,
1823 * a static object with a "stamp" value used to detect invalid
1824 * iter uses (uninitialized or after changing the message).
1825 * ref/unref is kind of annoying to deal with, and slower too.
1826 * This implies not ref'ing the message from the iter.
1828 * @todo I'd also name this dbus_message_iter_new() or
1829 * for the static object dbus_message_iter_init() rather
1830 * than making it a method on the message
1832 * @param message the message
1833 * @returns a new iter.
1836 dbus_message_get_args_iter (DBusMessage *message)
1838 DBusMessageIter *iter;
1840 iter = dbus_new (DBusMessageIter, 1);
1844 dbus_message_ref (message);
1847 iter->message = message;
1855 * Increments the reference count of a DBusMessageIter.
1857 * @param iter the message iter
1858 * @see dbus_message_iter_unref
1861 dbus_message_iter_ref (DBusMessageIter *iter)
1863 _dbus_assert (iter->refcount > 0);
1865 iter->refcount += 1;
1869 * Decrements the reference count of a DBusMessageIter.
1871 * @param iter The message iter
1872 * @see dbus_message_iter_ref
1875 dbus_message_iter_unref (DBusMessageIter *iter)
1877 _dbus_assert (iter->refcount > 0);
1879 iter->refcount -= 1;
1881 if (iter->refcount == 0)
1883 dbus_message_unref (iter->message);
1890 * Checks if an iterator has any more fields.
1892 * @param iter the message iter
1893 * @returns #TRUE if there are more fields
1897 dbus_message_iter_has_next (DBusMessageIter *iter)
1901 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1902 iter->message->byte_order,
1903 iter->pos, &end_pos))
1906 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1913 * Moves the iterator to the next field.
1915 * @param iter The message iter
1916 * @returns #TRUE if the iterator was moved to the next field
1919 dbus_message_iter_next (DBusMessageIter *iter)
1923 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1924 iter->message->byte_order,
1925 iter->pos, &end_pos))
1928 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1931 iter->pos = end_pos;
1937 * Returns the argument type of the argument that the
1938 * message iterator points at.
1940 * @param iter the message iter
1941 * @returns the field type
1944 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1948 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1949 return DBUS_TYPE_INVALID;
1951 data = _dbus_string_get_const_data_len (&iter->message->body, iter->pos, 1);
1953 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_DICT)
1956 return DBUS_TYPE_INVALID;
1960 * Returns the string value that an iterator may point to.
1961 * Note that you need to check that the iterator points to
1962 * a string value before using this function.
1964 * @see dbus_message_iter_get_arg_type
1965 * @param iter the message iter
1966 * @returns the string
1969 dbus_message_iter_get_string (DBusMessageIter *iter)
1971 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1973 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1974 iter->pos + 1, NULL);
1978 * Returns the boolean value that an iterator may point to.
1979 * Note that you need to check that the iterator points to
1980 * a boolean value before using this function.
1982 * @see dbus_message_iter_get_arg_type
1983 * @param iter the message iter
1984 * @returns the string
1987 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1989 unsigned char value;
1991 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1993 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
1999 * Returns the 32 bit signed integer value that an iterator may point to.
2000 * Note that you need to check that the iterator points to
2001 * an integer value before using this function.
2003 * @see dbus_message_iter_get_arg_type
2004 * @param iter the message iter
2005 * @returns the integer
2008 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2010 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
2012 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
2013 iter->pos + 1, NULL);
2017 * Returns the 32 bit unsigned integer value that an iterator may point to.
2018 * Note that you need to check that the iterator points to
2019 * an unsigned integer value before using this function.
2021 * @see dbus_message_iter_get_arg_type
2022 * @param iter the message iter
2023 * @returns the integer
2026 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2028 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
2030 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
2031 iter->pos + 1, NULL);
2035 * Returns the double value that an iterator may point to.
2036 * Note that you need to check that the iterator points to
2037 * a string value before using this function.
2039 * @see dbus_message_iter_get_arg_type
2040 * @param iter the message iter
2041 * @returns the double
2044 dbus_message_iter_get_double (DBusMessageIter *iter)
2046 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
2048 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
2049 iter->pos + 1, NULL);
2053 * Returns the boolean array that the iterator may point to. Note that
2054 * you need to check that the iterator points to an array of the
2055 * correct type prior to using this function.
2057 * @param iter the iterator
2058 * @param value return location for the array
2059 * @param len return location for the array length
2060 * @returns #TRUE on success
2063 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2064 unsigned char **value,
2067 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
2069 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2070 iter->pos + 1, NULL, value, len))
2077 * Returns the 32 bit signed integer array that the iterator may point
2078 * to. Note that you need to check that the iterator points to an
2079 * array of the correct type prior to using this function.
2081 * @param iter the iterator
2082 * @param value return location for the array
2083 * @param len return location for the array length
2084 * @returns #TRUE on success
2087 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2088 dbus_int32_t **value,
2091 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
2093 if (!_dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
2094 iter->pos + 1, NULL, value, len))
2101 * Returns the 32 bit unsigned integer array that the iterator may point
2102 * to. Note that you need to check that the iterator points to an
2103 * array of the correct type prior to using this function.
2105 * @param iter the iterator
2106 * @param value return location for the array
2107 * @param len return location for the array length
2108 * @returns #TRUE on success
2111 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2112 dbus_uint32_t **value,
2115 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2117 if (!_dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2118 iter->pos + 1, NULL, value, len))
2125 * Returns the double array that the iterator may point to. Note that
2126 * you need to check that the iterator points to an array of the
2127 * correct type prior to using this function.
2129 * @param iter the iterator
2130 * @param value return location for the array
2131 * @param len return location for the array length
2132 * @returns #TRUE on success
2135 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2139 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2141 if (!_dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2142 iter->pos + 1, NULL, value, len))
2149 * Returns the byte array that the iterator may point to.
2150 * Note that you need to check that the iterator points
2151 * to a byte array prior to using this function.
2153 * @param iter the iterator
2154 * @param value return location for array values
2155 * @param len return location for length of byte array
2156 * @returns #TRUE on success
2159 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2160 unsigned char **value,
2163 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2165 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2166 iter->pos + 1, NULL, value, len))
2173 * Returns the string array that the iterator may point to.
2174 * Note that you need to check that the iterator points
2175 * to a byte array prior to using this function.
2177 * The returned value is a #NULL-terminated array of strings.
2178 * Each string is a separate malloc block, and the array
2179 * itself is a malloc block. You can free this type of
2180 * string array with dbus_free_string_array().
2182 * @param iter the iterator
2183 * @param value return location for string values
2184 * @param len return location for length of byte array
2185 * @returns #TRUE on success
2188 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2192 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2194 if (!_dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2195 iter->pos + 1, NULL, value, len))
2202 * Returns the dict that the iterator may point to.
2203 * Note that you need to check that the iterator points
2204 * to a dict prior to using this function.
2206 * @param iter the iterator
2207 * @param dict return location for dict
2208 * @returns #TRUE on success
2211 dbus_message_iter_get_dict (DBusMessageIter *iter,
2214 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DICT);
2216 if (!_dbus_demarshal_dict (&iter->message->body, iter->message->byte_order,
2217 iter->pos + 1, NULL, dict))
2224 * Sets the message sender.
2226 * @param message the message
2227 * @param sender the sender
2228 * @returns #FALSE if not enough memory
2231 dbus_message_set_sender (DBusMessage *message,
2234 _dbus_assert (!message->locked);
2238 delete_string_field (message, FIELD_SENDER);
2243 return set_string_field (message,
2250 * Sets a flag indicating that the message is an error reply
2251 * message, i.e. an "exception" rather than a normal response.
2253 * @param message the message
2254 * @param is_error_reply #TRUE if this is an error message.
2257 dbus_message_set_is_error (DBusMessage *message,
2258 dbus_bool_t is_error_reply)
2262 _dbus_assert (!message->locked);
2264 header = _dbus_string_get_data_len (&message->header, 1, 1);
2267 *header |= DBUS_HEADER_FLAG_ERROR;
2269 *header &= ~DBUS_HEADER_FLAG_ERROR;
2273 * Returns #TRUE if the message is an error
2274 * reply to some previous message we sent.
2276 * @param message the message
2277 * @returns #TRUE if the message is an error
2280 dbus_message_get_is_error (DBusMessage *message)
2284 header = _dbus_string_get_const_data_len (&message->header, 1, 1);
2286 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2290 * Gets the service which originated this message,
2291 * or #NULL if unknown or inapplicable.
2293 * @param message the message
2294 * @returns the service name or #NULL
2297 dbus_message_get_sender (DBusMessage *message)
2299 return get_string_field (message, FIELD_SENDER, NULL);
2303 * Checks whether the message has the given name.
2304 * If the message has no name or has a different
2305 * name, returns #FALSE.
2307 * @param message the message
2308 * @param name the name to check (must not be #NULL)
2310 * @returns #TRUE if the message has the given name
2313 dbus_message_name_is (DBusMessage *message,
2318 _dbus_assert (name != NULL);
2320 n = dbus_message_get_name (message);
2322 if (n && strcmp (n, name) == 0)
2329 * Checks whether the message was sent to the given service. If the
2330 * message has no service specified or has a different name, returns
2333 * @param message the message
2334 * @param service the service to check (must not be #NULL)
2336 * @returns #TRUE if the message has the given destination service
2339 dbus_message_service_is (DBusMessage *message,
2340 const char *service)
2344 _dbus_assert (service != NULL);
2346 s = dbus_message_get_service (message);
2348 if (s && strcmp (s, service) == 0)
2355 * Checks whether the message has the given service as its sender. If
2356 * the message has no sender specified or has a different sender,
2357 * returns #FALSE. Note that if a peer application owns multiple
2358 * services, its messages will have only one of those services as the
2359 * sender (usually the base service). So you can't use this
2360 * function to prove the sender didn't own service Foo, you can
2361 * only use it to prove that it did.
2363 * @param message the message
2364 * @param service the service to check (must not be #NULL)
2366 * @returns #TRUE if the message has the given origin service
2369 dbus_message_sender_is (DBusMessage *message,
2370 const char *service)
2374 _dbus_assert (service != NULL);
2376 s = dbus_message_get_sender (message);
2378 if (s && strcmp (s, service) == 0)
2387 * @addtogroup DBusMessageInternals
2392 * @typedef DBusMessageLoader
2394 * The DBusMessageLoader object encapsulates the process of converting
2395 * a byte stream into a series of DBusMessage. It buffers the incoming
2396 * bytes as efficiently as possible, and generates a queue of
2397 * messages. DBusMessageLoader is typically used as part of a
2398 * DBusTransport implementation. The DBusTransport then hands off
2399 * the loaded messages to a DBusConnection, making the messages
2400 * visible to the application.
2402 * @todo write tests for break-loader that a) randomly delete header
2403 * fields and b) set string fields to zero-length and other funky
2408 /* we definitely use signed ints for sizes, so don't exceed
2409 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2410 * over 128M is pretty nuts anyhow.
2414 * The maximum sane message size.
2416 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2419 * Implementation details of DBusMessageLoader.
2420 * All members are private.
2422 struct DBusMessageLoader
2424 int refcount; /**< Reference count. */
2426 DBusString data; /**< Buffered data */
2428 DBusList *messages; /**< Complete messages. */
2430 long max_message_size; /**< Maximum size of a message */
2432 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2434 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2438 * The initial buffer size of the message loader.
2440 * @todo this should be based on min header size plus some average
2441 * body size, or something. Or rather, the min header size only, if we
2442 * want to try to read only the header, store that in a DBusMessage,
2443 * then read only the body and store that, etc., depends on
2444 * how we optimize _dbus_message_loader_get_buffer() and what
2445 * the exact message format is.
2447 #define INITIAL_LOADER_DATA_LEN 32
2450 * Creates a new message loader. Returns #NULL if memory can't
2453 * @returns new loader, or #NULL.
2456 _dbus_message_loader_new (void)
2458 DBusMessageLoader *loader;
2460 loader = dbus_new0 (DBusMessageLoader, 1);
2464 loader->refcount = 1;
2466 /* Try to cap message size at something that won't *totally* hose
2467 * the system if we have a couple of them.
2469 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2471 if (!_dbus_string_init (&loader->data))
2477 /* preallocate the buffer for speed, ignore failure */
2478 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2479 _dbus_string_set_length (&loader->data, 0);
2485 * Increments the reference count of the loader.
2487 * @param loader the loader.
2490 _dbus_message_loader_ref (DBusMessageLoader *loader)
2492 loader->refcount += 1;
2496 * Decrements the reference count of the loader and finalizes the
2497 * loader when the count reaches zero.
2499 * @param loader the loader.
2502 _dbus_message_loader_unref (DBusMessageLoader *loader)
2504 loader->refcount -= 1;
2505 if (loader->refcount == 0)
2507 _dbus_list_foreach (&loader->messages,
2508 (DBusForeachFunction) dbus_message_unref,
2510 _dbus_list_clear (&loader->messages);
2511 _dbus_string_free (&loader->data);
2517 * Gets the buffer to use for reading data from the network. Network
2518 * data is read directly into an allocated buffer, which is then used
2519 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2520 * The buffer must always be returned immediately using
2521 * _dbus_message_loader_return_buffer(), even if no bytes are
2522 * successfully read.
2524 * @todo this function can be a lot more clever. For example
2525 * it can probably always return a buffer size to read exactly
2526 * the body of the next message, thus avoiding any memory wastage
2529 * @todo we need to enforce a max length on strings in header fields.
2531 * @param loader the message loader.
2532 * @param buffer the buffer
2535 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2536 DBusString **buffer)
2538 _dbus_assert (!loader->buffer_outstanding);
2540 *buffer = &loader->data;
2542 loader->buffer_outstanding = TRUE;
2546 * The smallest header size that can occur.
2547 * (It won't be valid)
2549 #define DBUS_MINIMUM_HEADER_SIZE 16
2551 /** Pack four characters as in "abcd" into a uint32 */
2552 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2553 ((((dbus_uint32_t)a) << 24) | \
2554 (((dbus_uint32_t)b) << 16) | \
2555 (((dbus_uint32_t)c) << 8) | \
2558 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2559 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2560 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2562 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2563 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2564 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2566 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2567 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2568 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2570 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2571 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2572 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2574 /* FIXME impose max length on name, srvc, sndr */
2576 decode_header_data (const DBusString *data,
2579 HeaderField fields[FIELD_LAST],
2580 int *message_padding)
2586 if (header_len < 16)
2590 while (i < FIELD_LAST)
2592 fields[i].offset = -1;
2596 fields[FIELD_HEADER_LENGTH].offset = 4;
2597 fields[FIELD_BODY_LENGTH].offset = 8;
2598 fields[FIELD_CLIENT_SERIAL].offset = 12;
2600 /* Now handle the named fields. A real named field is at least 4
2601 * bytes for the name, plus a type code (1 byte) plus padding. So
2602 * if we have less than 8 bytes left, it must be alignment padding,
2603 * not a field. While >= 8 bytes can't be entirely alignment
2607 while ((pos + 7) < header_len)
2609 pos = _DBUS_ALIGN_VALUE (pos, 4);
2611 if ((pos + 4) > header_len)
2614 field =_dbus_string_get_const_data_len (data, pos, 4);
2617 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2619 /* I believe FROM_BE is right, but if not we'll find out
2622 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2624 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2625 if (fields[FIELD_SERVICE].offset >= 0)
2627 _dbus_verbose ("%s field provided twice\n",
2628 DBUS_HEADER_FIELD_SERVICE);
2632 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2634 _dbus_verbose ("Found service name at offset %d\n",
2635 fields[FIELD_SERVICE].offset);
2639 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2640 if (fields[FIELD_NAME].offset >= 0)
2642 _dbus_verbose ("%s field provided twice\n",
2643 DBUS_HEADER_FIELD_NAME);
2647 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2650 _dbus_verbose ("Found message name at offset %d\n",
2651 fields[FIELD_NAME].offset);
2654 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2655 if (fields[FIELD_SENDER].offset >= 0)
2657 _dbus_verbose ("%s field provided twice\n",
2658 DBUS_HEADER_FIELD_SENDER);
2662 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2664 _dbus_verbose ("Found sender name at offset %d\n",
2665 fields[FIELD_NAME].offset);
2668 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2669 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2671 _dbus_verbose ("%s field provided twice\n",
2672 DBUS_HEADER_FIELD_REPLY);
2676 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2678 _dbus_verbose ("Found reply serial at offset %d\n",
2679 fields[FIELD_REPLY_SERIAL].offset);
2683 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2684 field[0], field[1], field[2], field[3], pos);
2687 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2689 _dbus_verbose ("Failed to validate argument to named header field\n");
2693 if (new_pos > header_len)
2695 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2702 if (pos < header_len)
2704 /* Alignment padding, verify that it's nul */
2705 _dbus_assert ((header_len - pos) < 8);
2707 if (!_dbus_string_validate_nul (data,
2708 pos, (header_len - pos)))
2710 _dbus_verbose ("header alignment padding is not nul\n");
2715 if (fields[FIELD_NAME].offset < 0)
2717 _dbus_verbose ("No %s field provided\n",
2718 DBUS_HEADER_FIELD_NAME);
2722 if (message_padding)
2723 *message_padding = header_len - pos;
2729 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2730 * indicating to the loader how many bytes of the buffer were filled
2731 * in. This function must always be called, even if no bytes were
2732 * successfully read.
2734 * @param loader the loader.
2735 * @param buffer the buffer.
2736 * @param bytes_read number of bytes that were read into the buffer.
2739 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2743 _dbus_assert (loader->buffer_outstanding);
2744 _dbus_assert (buffer == &loader->data);
2746 loader->buffer_outstanding = FALSE;
2750 * Converts buffered data into messages.
2752 * @param loader the loader.
2753 * @returns #TRUE if we had enough memory to finish.
2756 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
2758 if (loader->corrupted)
2761 while (_dbus_string_get_length (&loader->data) >= 16)
2763 DBusMessage *message;
2764 const char *header_data;
2765 int byte_order, header_len, body_len, header_padding;
2766 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2768 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
2770 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2772 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2774 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2775 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2776 loader->corrupted = TRUE;
2780 byte_order = header_data[0];
2782 if (byte_order != DBUS_LITTLE_ENDIAN &&
2783 byte_order != DBUS_BIG_ENDIAN)
2785 _dbus_verbose ("Message with bad byte order '%c' received\n",
2787 loader->corrupted = TRUE;
2791 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2792 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2794 if (header_len_unsigned < 16)
2796 _dbus_verbose ("Message had broken too-small header length %u\n",
2797 header_len_unsigned);
2798 loader->corrupted = TRUE;
2802 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2803 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2805 _dbus_verbose ("Header or body length too large (%u %u)\n",
2806 header_len_unsigned,
2808 loader->corrupted = TRUE;
2812 /* Now that we know the values are in signed range, get
2813 * rid of stupid unsigned, just causes bugs
2815 header_len = header_len_unsigned;
2816 body_len = body_len_unsigned;
2818 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2821 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2823 loader->corrupted = TRUE;
2827 if (header_len + body_len > loader->max_message_size)
2829 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
2830 header_len, body_len, loader->max_message_size);
2831 loader->corrupted = TRUE;
2835 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2837 HeaderField fields[FIELD_LAST];
2842 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
2844 if (!decode_header_data (&loader->data, header_len, byte_order,
2845 fields, &header_padding))
2847 _dbus_verbose ("Header was invalid\n");
2848 loader->corrupted = TRUE;
2852 next_arg = header_len;
2853 while (next_arg < (header_len + body_len))
2855 int prev = next_arg;
2857 if (!_dbus_marshal_validate_arg (&loader->data,
2862 loader->corrupted = TRUE;
2866 _dbus_assert (next_arg > prev);
2869 if (next_arg > (header_len + body_len))
2871 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2872 next_arg, header_len, body_len,
2873 header_len + body_len);
2874 loader->corrupted = TRUE;
2878 message = dbus_message_new_empty_header ();
2879 if (message == NULL)
2882 message->byte_order = byte_order;
2883 message->header_padding = header_padding;
2885 /* Copy in the offsets we found */
2887 while (i < FIELD_LAST)
2889 message->header_fields[i] = fields[i];
2893 if (!_dbus_list_append (&loader->messages, message))
2895 dbus_message_unref (message);
2899 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2900 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2902 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2903 (header_len + body_len));
2905 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2907 _dbus_list_remove_last (&loader->messages, message);
2908 dbus_message_unref (message);
2912 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2916 /* put the header back, we'll try again later */
2917 result = _dbus_string_copy_len (&message->header, 0, header_len,
2919 _dbus_assert (result); /* because DBusString never reallocs smaller */
2921 _dbus_list_remove_last (&loader->messages, message);
2922 dbus_message_unref (message);
2926 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2927 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2929 /* Fill in caches */
2930 message->reply_serial = get_int_field (message,
2931 FIELD_REPLY_SERIAL);
2932 message->client_serial = get_int_field (message,
2933 FIELD_CLIENT_SERIAL);
2935 _dbus_verbose ("Loaded message %p\n", message);
2945 * Peeks at first loaded message, returns #NULL if no messages have
2948 * @param loader the loader.
2949 * @returns the next message, or #NULL if none.
2952 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
2954 if (loader->messages)
2955 return loader->messages->data;
2961 * Pops a loaded message (passing ownership of the message
2962 * to the caller). Returns #NULL if no messages have been
2965 * @param loader the loader.
2966 * @returns the next message, or #NULL if none.
2969 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2971 return _dbus_list_pop_first (&loader->messages);
2975 * Pops a loaded message inside a list link (passing ownership of the
2976 * message and link to the caller). Returns #NULL if no messages have
2979 * @param loader the loader.
2980 * @returns the next message link, or #NULL if none.
2983 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
2985 return _dbus_list_pop_first_link (&loader->messages);
2989 * Checks whether the loader is confused due to bad data.
2990 * If messages are received that are invalid, the
2991 * loader gets confused and gives up permanently.
2992 * This state is called "corrupted."
2994 * @param loader the loader
2995 * @returns #TRUE if the loader is hosed.
2998 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
3000 return loader->corrupted;
3004 * Sets the maximum size message we allow.
3006 * @param loader the loader
3007 * @param size the max message size in bytes
3010 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
3013 if (size > MAX_SANE_MESSAGE_SIZE)
3015 _dbus_verbose ("clamping requested max message size %ld to %d\n",
3016 size, MAX_SANE_MESSAGE_SIZE);
3017 size = MAX_SANE_MESSAGE_SIZE;
3019 loader->max_message_size = size;
3023 * Gets the maximum allowed message size in bytes.
3025 * @param loader the loader
3026 * @returns max size in bytes
3029 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
3031 return loader->max_message_size;
3035 #ifdef DBUS_BUILD_TESTS
3036 #include "dbus-test.h"
3040 message_iter_test (DBusMessage *message)
3042 DBusMessageIter *iter;
3045 iter = dbus_message_get_args_iter (message);
3048 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
3049 _dbus_assert_not_reached ("Argument type isn't string");
3051 str = dbus_message_iter_get_string (iter);
3052 if (strcmp (str, "Test string") != 0)
3053 _dbus_assert_not_reached ("Strings differ");
3056 if (!dbus_message_iter_next (iter))
3057 _dbus_assert_not_reached ("Reached end of arguments");
3059 /* Signed integer tests */
3060 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
3061 _dbus_assert_not_reached ("Argument type isn't int32");
3063 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
3064 _dbus_assert_not_reached ("Signed integers differ");
3066 if (!dbus_message_iter_next (iter))
3067 _dbus_assert_not_reached ("Reached end of fields");
3069 /* Unsigned integer tests */
3070 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
3071 _dbus_assert_not_reached ("Argument type isn't int32");
3073 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
3074 _dbus_assert_not_reached ("Unsigned integers differ");
3076 if (!dbus_message_iter_next (iter))
3077 _dbus_assert_not_reached ("Reached end of arguments");
3080 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
3081 _dbus_assert_not_reached ("Argument type isn't double");
3083 if (dbus_message_iter_get_double (iter) != 3.14159)
3084 _dbus_assert_not_reached ("Doubles differ");
3086 if (dbus_message_iter_next (iter))
3087 _dbus_assert_not_reached ("Didn't reach end of arguments");
3089 dbus_message_iter_unref (iter);
3093 check_message_handling (DBusMessage *message)
3095 DBusMessageIter *iter;
3098 dbus_int32_t client_serial;
3103 client_serial = dbus_message_get_serial (message);
3105 /* can't use set_serial due to the assertions at the start of it */
3106 set_int_field (message, FIELD_CLIENT_SERIAL,
3109 if (client_serial != dbus_message_get_serial (message))
3111 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
3115 /* If we implement message_set_arg (message, n, value)
3116 * then we would want to test it here
3119 iter = dbus_message_get_args_iter (message);
3120 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
3126 case DBUS_TYPE_INT32:
3127 dbus_message_iter_get_int32 (iter);
3129 case DBUS_TYPE_UINT32:
3130 dbus_message_iter_get_uint32 (iter);
3132 case DBUS_TYPE_DOUBLE:
3133 dbus_message_iter_get_double (iter);
3135 case DBUS_TYPE_STRING:
3138 str = dbus_message_iter_get_string (iter);
3142 case DBUS_TYPE_BOOLEAN_ARRAY:
3144 unsigned char *values;
3147 if (!dbus_message_iter_get_boolean_array (iter, &values, &len))
3153 case DBUS_TYPE_INT32_ARRAY:
3155 dbus_int32_t *values;
3158 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
3164 case DBUS_TYPE_UINT32_ARRAY:
3166 dbus_uint32_t *values;
3169 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
3175 case DBUS_TYPE_DOUBLE_ARRAY:
3180 if (!dbus_message_iter_get_double_array (iter, &values, &len))
3186 case DBUS_TYPE_STRING_ARRAY:
3191 if (!dbus_message_iter_get_string_array (iter, &values, &len))
3194 dbus_free_string_array (values);
3198 case DBUS_TYPE_DICT:
3202 if (!dbus_message_iter_get_dict (iter, &dict))
3204 dbus_dict_unref (dict);
3212 if (!dbus_message_iter_next (iter))
3220 dbus_message_iter_unref (iter);
3226 check_have_valid_message (DBusMessageLoader *loader)
3228 DBusMessage *message;
3234 if (!_dbus_message_loader_queue_messages (loader))
3235 _dbus_assert_not_reached ("no memory to queue messages");
3237 if (_dbus_message_loader_get_is_corrupted (loader))
3239 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
3243 message = _dbus_message_loader_pop_message (loader);
3244 if (message == NULL)
3246 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3250 if (_dbus_string_get_length (&loader->data) > 0)
3252 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3256 /* Verify that we're able to properly deal with the message.
3257 * For example, this would detect improper handling of messages
3258 * in nonstandard byte order.
3260 if (!check_message_handling (message))
3267 dbus_message_unref (message);
3273 check_invalid_message (DBusMessageLoader *loader)
3279 if (!_dbus_message_loader_queue_messages (loader))
3280 _dbus_assert_not_reached ("no memory to queue messages");
3282 if (!_dbus_message_loader_get_is_corrupted (loader))
3284 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3295 check_incomplete_message (DBusMessageLoader *loader)
3297 DBusMessage *message;
3303 if (!_dbus_message_loader_queue_messages (loader))
3304 _dbus_assert_not_reached ("no memory to queue messages");
3306 if (_dbus_message_loader_get_is_corrupted (loader))
3308 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3312 message = _dbus_message_loader_pop_message (loader);
3313 if (message != NULL)
3315 _dbus_warn ("loaded message that was expected to be incomplete\n");
3323 dbus_message_unref (message);
3328 check_loader_results (DBusMessageLoader *loader,
3329 DBusMessageValidity validity)
3331 if (!_dbus_message_loader_queue_messages (loader))
3332 _dbus_assert_not_reached ("no memory to queue messages");
3336 case _DBUS_MESSAGE_VALID:
3337 return check_have_valid_message (loader);
3338 case _DBUS_MESSAGE_INVALID:
3339 return check_invalid_message (loader);
3340 case _DBUS_MESSAGE_INCOMPLETE:
3341 return check_incomplete_message (loader);
3342 case _DBUS_MESSAGE_UNKNOWN:
3346 _dbus_assert_not_reached ("bad DBusMessageValidity");
3352 * Loads the message in the given message file.
3354 * @param filename filename to load
3355 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3356 * @param data string to load message into
3357 * @returns #TRUE if the message was loaded
3360 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3372 dbus_error_init (&error);
3373 if (!_dbus_file_get_contents (data, filename, &error))
3375 _dbus_warn ("Could not load message file %s: %s\n",
3376 _dbus_string_get_const_data (filename),
3378 dbus_error_free (&error);
3384 if (!_dbus_message_data_load (data, filename))
3386 _dbus_warn ("Could not load message file %s\n",
3387 _dbus_string_get_const_data (filename));
3400 * Tries loading the message in the given message file
3401 * and verifies that DBusMessageLoader can handle it.
3403 * @param filename filename to load
3404 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3405 * @param expected_validity what the message has to be like to return #TRUE
3406 * @returns #TRUE if the message has the expected validity
3409 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3411 DBusMessageValidity expected_validity)
3418 if (!_dbus_string_init (&data))
3419 _dbus_assert_not_reached ("could not allocate string\n");
3421 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3425 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3431 if (_dbus_string_get_length (&data) > 0)
3432 _dbus_verbose_bytes_of_string (&data, 0,
3433 _dbus_string_get_length (&data));
3435 _dbus_warn ("Failed message loader test on %s\n",
3436 _dbus_string_get_const_data (filename));
3439 _dbus_string_free (&data);
3445 * Tries loading the given message data.
3448 * @param data the message data
3449 * @param expected_validity what the message has to be like to return #TRUE
3450 * @returns #TRUE if the message has the expected validity
3453 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3454 DBusMessageValidity expected_validity)
3456 DBusMessageLoader *loader;
3464 /* Write the data one byte at a time */
3466 loader = _dbus_message_loader_new ();
3468 len = _dbus_string_get_length (data);
3469 for (i = 0; i < len; i++)
3473 _dbus_message_loader_get_buffer (loader, &buffer);
3474 _dbus_string_append_byte (buffer,
3475 _dbus_string_get_byte (data, i));
3476 _dbus_message_loader_return_buffer (loader, buffer, 1);
3479 if (!check_loader_results (loader, expected_validity))
3482 _dbus_message_loader_unref (loader);
3485 /* Write the data all at once */
3487 loader = _dbus_message_loader_new ();
3492 _dbus_message_loader_get_buffer (loader, &buffer);
3493 _dbus_string_copy (data, 0, buffer,
3494 _dbus_string_get_length (buffer));
3495 _dbus_message_loader_return_buffer (loader, buffer, 1);
3498 if (!check_loader_results (loader, expected_validity))
3501 _dbus_message_loader_unref (loader);
3504 /* Write the data 2 bytes at a time */
3506 loader = _dbus_message_loader_new ();
3508 len = _dbus_string_get_length (data);
3509 for (i = 0; i < len; i += 2)
3513 _dbus_message_loader_get_buffer (loader, &buffer);
3514 _dbus_string_append_byte (buffer,
3515 _dbus_string_get_byte (data, i));
3517 _dbus_string_append_byte (buffer,
3518 _dbus_string_get_byte (data, i+1));
3519 _dbus_message_loader_return_buffer (loader, buffer, 1);
3522 if (!check_loader_results (loader, expected_validity))
3525 _dbus_message_loader_unref (loader);
3533 _dbus_message_loader_unref (loader);
3539 process_test_subdir (const DBusString *test_base_dir,
3541 DBusMessageValidity validity,
3542 DBusForeachMessageFileFunc function,
3545 DBusString test_directory;
3546 DBusString filename;
3554 if (!_dbus_string_init (&test_directory))
3555 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3557 _dbus_string_init_const (&filename, subdir);
3559 if (!_dbus_string_copy (test_base_dir, 0,
3560 &test_directory, 0))
3561 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3563 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3564 _dbus_assert_not_reached ("couldn't allocate full path");
3566 _dbus_string_free (&filename);
3567 if (!_dbus_string_init (&filename))
3568 _dbus_assert_not_reached ("didn't allocate filename string\n");
3570 dbus_error_init (&error);
3571 dir = _dbus_directory_open (&test_directory, &error);
3574 _dbus_warn ("Could not open %s: %s\n",
3575 _dbus_string_get_const_data (&test_directory),
3577 dbus_error_free (&error);
3581 printf ("Testing:\n");
3584 while (_dbus_directory_get_next_file (dir, &filename, &error))
3586 DBusString full_path;
3589 if (!_dbus_string_init (&full_path))
3590 _dbus_assert_not_reached ("couldn't init string");
3592 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3593 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3595 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3596 _dbus_assert_not_reached ("couldn't concat file to dir");
3598 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3600 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3604 _dbus_verbose ("Skipping non-.message file %s\n",
3605 _dbus_string_get_const_data (&filename));
3606 _dbus_string_free (&full_path);
3611 _dbus_string_get_const_data (&filename));
3613 _dbus_verbose (" expecting %s\n",
3614 validity == _DBUS_MESSAGE_VALID ? "valid" :
3615 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3616 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3618 if (! (*function) (&full_path, is_raw, validity, user_data))
3620 _dbus_string_free (&full_path);
3624 _dbus_string_free (&full_path);
3627 if (dbus_error_is_set (&error))
3629 _dbus_warn ("Could not get next file in %s: %s\n",
3630 _dbus_string_get_const_data (&test_directory),
3632 dbus_error_free (&error);
3641 _dbus_directory_close (dir);
3642 _dbus_string_free (&test_directory);
3643 _dbus_string_free (&filename);
3649 * Runs the given function on every message file in the test suite.
3650 * The function should return #FALSE on test failure or fatal error.
3652 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3653 * @param func the function to run
3654 * @param user_data data for function
3655 * @returns #FALSE if there's a failure
3658 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3659 DBusForeachMessageFileFunc func,
3662 DBusString test_directory;
3667 _dbus_string_init_const (&test_directory, test_data_dir);
3669 if (!process_test_subdir (&test_directory, "valid-messages",
3670 _DBUS_MESSAGE_VALID, func, user_data))
3673 if (!process_test_subdir (&test_directory, "invalid-messages",
3674 _DBUS_MESSAGE_INVALID, func, user_data))
3677 if (!process_test_subdir (&test_directory, "incomplete-messages",
3678 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3685 _dbus_string_free (&test_directory);
3691 verify_test_message (DBusMessage *message)
3693 dbus_int32_t our_int;
3696 dbus_bool_t our_bool;
3698 if (!dbus_message_get_args (message, NULL,
3699 DBUS_TYPE_INT32, &our_int,
3700 DBUS_TYPE_STRING, &our_str,
3701 DBUS_TYPE_DOUBLE, &our_double,
3702 DBUS_TYPE_BOOLEAN, &our_bool,
3704 _dbus_assert_not_reached ("Could not get arguments");
3706 if (our_int != -0x12345678)
3707 _dbus_assert_not_reached ("integers differ!");
3709 if (our_double != 3.14159)
3710 _dbus_assert_not_reached ("doubles differ!");
3712 if (strcmp (our_str, "Test string") != 0)
3713 _dbus_assert_not_reached ("strings differ!");
3716 _dbus_assert_not_reached ("booleans differ");
3718 dbus_free (our_str);
3722 * @ingroup DBusMessageInternals
3723 * Unit test for DBusMessage.
3725 * @returns #TRUE on success.
3728 _dbus_message_test (const char *test_data_dir)
3730 DBusMessage *message;
3731 DBusMessageLoader *loader;
3738 /* Test the vararg functions */
3739 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3740 _dbus_message_set_serial (message, 1);
3741 dbus_message_append_args (message,
3742 DBUS_TYPE_INT32, -0x12345678,
3743 DBUS_TYPE_STRING, "Test string",
3744 DBUS_TYPE_DOUBLE, 3.14159,
3745 DBUS_TYPE_BOOLEAN, TRUE,
3747 _dbus_verbose_bytes_of_string (&message->header, 0,
3748 _dbus_string_get_length (&message->header));
3749 _dbus_verbose_bytes_of_string (&message->body, 0,
3750 _dbus_string_get_length (&message->body));
3752 verify_test_message (message);
3754 copy = dbus_message_copy (message);
3756 _dbus_assert (message->client_serial == copy->client_serial);
3757 _dbus_assert (message->reply_serial == copy->reply_serial);
3758 _dbus_assert (message->header_padding == copy->header_padding);
3760 _dbus_assert (_dbus_string_get_length (&message->header) ==
3761 _dbus_string_get_length (©->header));
3763 _dbus_assert (_dbus_string_get_length (&message->body) ==
3764 _dbus_string_get_length (©->body));
3766 verify_test_message (copy);
3768 name1 = dbus_message_get_name (message);
3769 name2 = dbus_message_get_name (copy);
3771 _dbus_assert (strcmp (name1, name2) == 0);
3773 dbus_message_unref (message);
3774 dbus_message_unref (copy);
3776 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3777 _dbus_message_set_serial (message, 1);
3778 dbus_message_set_reply_serial (message, 0x12345678);
3780 dbus_message_append_string (message, "Test string");
3781 dbus_message_append_int32 (message, -0x12345678);
3782 dbus_message_append_uint32 (message, 0xedd1e);
3783 dbus_message_append_double (message, 3.14159);
3785 message_iter_test (message);
3787 /* Message loader test */
3788 _dbus_message_lock (message);
3789 loader = _dbus_message_loader_new ();
3791 /* Write the header data one byte at a time */
3792 data = _dbus_string_get_const_data (&message->header);
3793 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3797 _dbus_message_loader_get_buffer (loader, &buffer);
3798 _dbus_string_append_byte (buffer, data[i]);
3799 _dbus_message_loader_return_buffer (loader, buffer, 1);
3802 /* Write the body data one byte at a time */
3803 data = _dbus_string_get_const_data (&message->body);
3804 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3808 _dbus_message_loader_get_buffer (loader, &buffer);
3809 _dbus_string_append_byte (buffer, data[i]);
3810 _dbus_message_loader_return_buffer (loader, buffer, 1);
3813 dbus_message_unref (message);
3815 /* Now pop back the message */
3816 if (!_dbus_message_loader_queue_messages (loader))
3817 _dbus_assert_not_reached ("no memory to queue messages");
3819 if (_dbus_message_loader_get_is_corrupted (loader))
3820 _dbus_assert_not_reached ("message loader corrupted");
3822 message = _dbus_message_loader_pop_message (loader);
3824 _dbus_assert_not_reached ("received a NULL message");
3826 if (dbus_message_get_reply_serial (message) != 0x12345678)
3827 _dbus_assert_not_reached ("reply serial fields differ");
3829 message_iter_test (message);
3831 dbus_message_unref (message);
3832 _dbus_message_loader_unref (loader);
3834 /* Now load every message in test_data_dir if we have one */
3835 if (test_data_dir == NULL)
3838 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3839 (DBusForeachMessageFileFunc)
3840 dbus_internal_do_not_use_try_message_file,
3844 #endif /* DBUS_BUILD_TESTS */