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"
94 int header_padding; /**< bytes of alignment in header */
96 DBusString body; /**< Body network data. */
98 char byte_order; /**< Message byte order. */
100 DBusCounter *size_counter; /**< Counter for the size of the message, or #NULL */
101 long size_counter_delta; /**< Size we incremented the size counter by. */
103 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
107 * @brief Internals of DBusMessageIter
109 * Object representing a position in a message. All fields are internal.
111 struct DBusMessageIter
113 int refcount; /**< Reference count */
115 int pos; /**< Current position in the string */
117 DBusMessage *message; /**< Message used */
121 * Gets the data to be sent over the network for this message.
122 * The header and then the body should be written out.
123 * This function is guaranteed to always return the same
124 * data once a message is locked (with _dbus_message_lock()).
126 * @param message the message.
127 * @param header return location for message header data.
128 * @param body return location for message body data.
131 _dbus_message_get_network_data (DBusMessage *message,
132 const DBusString **header,
133 const DBusString **body)
135 _dbus_assert (message->locked);
137 *header = &message->header;
138 *body = &message->body;
142 clear_header_padding (DBusMessage *message)
144 _dbus_string_shorten (&message->header,
145 message->header_padding);
146 message->header_padding = 0;
150 append_header_padding (DBusMessage *message)
153 old_len = _dbus_string_get_length (&message->header);
154 if (!_dbus_string_align_length (&message->header, 8))
157 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
163 adjust_field_offsets (DBusMessage *message,
173 while (i < FIELD_LAST)
175 if (message->header_fields[i].offset > offsets_after)
176 message->header_fields[i].offset += delta;
183 get_string_field (DBusMessage *message,
187 int offset = message->header_fields[field].offset;
193 /* offset points to string length, string data follows it */
194 /* FIXME _dbus_demarshal_const_string() that returned
195 * a reference to the string plus its len might be nice.
199 *len = _dbus_demarshal_uint32 (&message->header,
204 _dbus_string_get_const_data (&message->header,
207 return data + (offset + 4);
211 get_int_field (DBusMessage *message,
214 int offset = message->header_fields[field].offset;
217 return -1; /* useless if -1 is a valid value of course */
219 return _dbus_demarshal_int32 (&message->header,
226 append_int_field (DBusMessage *message,
233 _dbus_assert (!message->locked);
235 clear_header_padding (message);
237 orig_len = _dbus_string_get_length (&message->header);
239 if (!_dbus_string_align_length (&message->header, 4))
242 if (!_dbus_string_append_len (&message->header, name, 4))
245 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
248 if (!_dbus_string_align_length (&message->header, 4))
251 message->header_fields[FIELD_REPLY_SERIAL].offset =
252 _dbus_string_get_length (&message->header);
254 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
258 if (!append_header_padding (message))
264 message->header_fields[field].offset = -1;
265 _dbus_string_set_length (&message->header, orig_len);
267 /* this must succeed because it was allocated on function entry and
268 * DBusString doesn't ever realloc smaller
270 if (!append_header_padding (message))
271 _dbus_assert_not_reached ("failed to reappend header padding");
276 append_string_field (DBusMessage *message,
283 _dbus_assert (!message->locked);
285 clear_header_padding (message);
287 orig_len = _dbus_string_get_length (&message->header);
289 if (!_dbus_string_align_length (&message->header, 4))
292 if (!_dbus_string_append_len (&message->header, name, 4))
295 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
298 if (!_dbus_string_align_length (&message->header, 4))
301 message->header_fields[field].offset =
302 _dbus_string_get_length (&message->header);
304 if (!_dbus_marshal_string (&message->header, message->byte_order,
308 if (!append_header_padding (message))
314 message->header_fields[field].offset = -1;
315 _dbus_string_set_length (&message->header, orig_len);
317 /* this must succeed because it was allocated on function entry and
318 * DBusString doesn't ever realloc smaller
320 if (!append_header_padding (message))
321 _dbus_assert_not_reached ("failed to reappend header padding");
327 delete_int_field (DBusMessage *message,
330 int offset = message->header_fields[field].offset;
332 _dbus_assert (!message->locked);
333 _dbus_assert (field_is_named[field]);
338 clear_header_padding (message);
340 /* The field typecode and name take up 8 bytes */
341 _dbus_string_delete (&message->header,
345 message->header_fields[field].offset = -1;
347 adjust_field_offsets (message,
351 append_header_padding (message);
355 delete_string_field (DBusMessage *message,
358 int offset = message->header_fields[field].offset;
362 _dbus_assert (!message->locked);
363 _dbus_assert (field_is_named[field]);
368 clear_header_padding (message);
370 get_string_field (message, field, &len);
372 /* The field typecode and name take up 8 bytes, and the nul
373 * termination is 1 bytes, string length integer is 4 bytes
375 delete_len = 8 + 4 + 1 + len;
377 _dbus_string_delete (&message->header,
381 message->header_fields[field].offset = -1;
383 adjust_field_offsets (message,
387 append_header_padding (message);
391 set_int_field (DBusMessage *message,
395 int offset = message->header_fields[field].offset;
397 _dbus_assert (!message->locked);
401 /* need to append the field */
405 case FIELD_REPLY_SERIAL:
406 return append_int_field (message, field,
407 DBUS_HEADER_FIELD_REPLY,
410 _dbus_assert_not_reached ("appending an int field we don't support appending");
416 _dbus_marshal_set_int32 (&message->header,
425 set_uint_field (DBusMessage *message,
429 int offset = message->header_fields[field].offset;
431 _dbus_assert (!message->locked);
435 /* need to append the field */
440 _dbus_assert_not_reached ("appending a uint field we don't support appending");
446 _dbus_marshal_set_uint32 (&message->header,
455 set_string_field (DBusMessage *message,
459 int offset = message->header_fields[field].offset;
461 _dbus_assert (!message->locked);
462 _dbus_assert (value != NULL);
466 /* need to append the field */
471 return append_string_field (message, field,
472 DBUS_HEADER_FIELD_SENDER,
475 _dbus_assert_not_reached ("appending a string field we don't support appending");
485 old_len = _dbus_string_get_length (&message->header);
487 _dbus_string_init_const_len (&v, value,
488 strlen (value) + 1); /* include nul */
489 if (!_dbus_marshal_set_string (&message->header,
494 new_len = _dbus_string_get_length (&message->header);
496 adjust_field_offsets (message,
505 * Sets the client serial of a message.
506 * This can only be done once on a message.
508 * @todo client_serial should be called simply
509 * "serial"; it's in outgoing messages for both
510 * the client and the server, it's only client-specific
511 * in the message bus case. It's more like origin_serial
514 * @param message the message
515 * @param client_serial the client serial
518 _dbus_message_set_client_serial (DBusMessage *message,
519 dbus_int32_t client_serial)
521 _dbus_assert (!message->locked);
522 _dbus_assert (_dbus_message_get_client_serial (message) < 0);
524 set_int_field (message, FIELD_CLIENT_SERIAL,
529 * Sets the reply serial of a message (the client serial
530 * of the message this is a reply to).
532 * @param message the message
533 * @param reply_serial the client serial
534 * @returns #FALSE if not enough memory
537 _dbus_message_set_reply_serial (DBusMessage *message,
538 dbus_int32_t reply_serial)
540 _dbus_assert (!message->locked);
542 return set_int_field (message, FIELD_REPLY_SERIAL,
547 * Returns the client serial of a message or
548 * -1 if none has been specified.
550 * @todo see note in _dbus_message_set_client_serial()
551 * about how client_serial is a misnomer
553 * @todo this function should be public, after renaming it.
555 * @param message the message
556 * @returns the client serial
559 _dbus_message_get_client_serial (DBusMessage *message)
561 return get_int_field (message, FIELD_CLIENT_SERIAL);
565 * Returns the serial that the message is
566 * a reply to or -1 if none.
568 * @param message the message
569 * @returns the reply serial
572 _dbus_message_get_reply_serial (DBusMessage *message)
574 return get_int_field (message, FIELD_REPLY_SERIAL);
578 * Adds a counter to be incremented immediately with the
579 * size of this message, and decremented by the size
580 * of this message when this message if finalized.
582 * @param message the message
583 * @param counter the counter
586 _dbus_message_add_size_counter (DBusMessage *message,
587 DBusCounter *counter)
589 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
590 * counters instead of just one
593 message->size_counter = counter;
594 _dbus_counter_ref (message->size_counter);
596 /* When we can change message size, we may want to
597 * update this each time we do so, or we may want to
598 * just KISS like this.
600 message->size_counter_delta =
601 _dbus_string_get_length (&message->header) +
602 _dbus_string_get_length (&message->body);
604 _dbus_verbose ("message has size %ld\n",
605 message->size_counter_delta);
607 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
611 dbus_message_create_header (DBusMessage *message,
617 if (!_dbus_string_append_byte (&message->header, message->byte_order))
621 if (!_dbus_string_append_byte (&message->header, flags))
624 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
627 if (!_dbus_string_append_byte (&message->header, 0))
630 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
631 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
634 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
635 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
638 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
639 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
642 /* Marshal message service */
645 if (!append_string_field (message,
647 DBUS_HEADER_FIELD_SERVICE,
652 _dbus_assert (name != NULL);
653 if (!append_string_field (message,
655 DBUS_HEADER_FIELD_NAME,
663 * Locks a message. Allows checking that applications don't keep a
664 * reference to a message in the outgoing queue and change it
665 * underneath us. Messages are locked when they enter the outgoing
666 * queue (dbus_connection_send_message()), and the library complains
667 * if the message is modified while locked.
669 * @param message the message to lock.
672 _dbus_message_lock (DBusMessage *message)
674 if (!message->locked)
676 /* Fill in our lengths */
677 set_uint_field (message,
679 _dbus_string_get_length (&message->header));
681 set_uint_field (message,
683 _dbus_string_get_length (&message->body));
685 message->locked = TRUE;
692 * @defgroup DBusMessage DBusMessage
694 * @brief Message to be sent or received over a DBusConnection.
696 * A DBusMessage is the most basic unit of communication over a
697 * DBusConnection. A DBusConnection represents a stream of messages
698 * received from a remote application, and a stream of messages
699 * sent to a remote application.
705 * @typedef DBusMessage
707 * Opaque data type representing a message received from or to be
708 * sent to another application.
712 dbus_message_new_empty_header (void)
714 DBusMessage *message;
717 message = dbus_new0 (DBusMessage, 1);
721 message->refcount = 1;
722 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
725 while (i < FIELD_LAST)
727 message->header_fields[i].offset = -1;
731 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
737 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
739 _dbus_string_free (&message->header);
749 * Constructs a new message. Returns #NULL if memory can't be
750 * allocated for the message. The service may be #NULL in which case
751 * no service is set; this is appropriate when using D-BUS in a
752 * peer-to-peer context (no message bus).
754 * @todo reverse the arguments, first 'name' then 'service'
755 * as 'name' is more fundamental
757 * @param service service that the message should be sent to or #NULL
758 * @param name name of the message
759 * @returns a new DBusMessage, free with dbus_message_unref()
760 * @see dbus_message_unref()
763 dbus_message_new (const char *service,
766 DBusMessage *message;
768 message = dbus_message_new_empty_header ();
772 if (!dbus_message_create_header (message, service, name))
774 dbus_message_unref (message);
782 * Constructs a message that is a reply to some other
783 * message. Returns #NULL if memory can't be allocated
786 * @param original_message the message which the created
787 * message is a reply to.
788 * @returns a new DBusMessage, free with dbus_message_unref()
789 * @see dbus_message_new(), dbus_message_unref()
792 dbus_message_new_reply (DBusMessage *original_message)
794 DBusMessage *message;
795 const char *sender, *name;
797 sender = get_string_field (original_message,
799 name = get_string_field (original_message,
802 _dbus_assert (sender != NULL);
804 message = dbus_message_new (sender, name);
809 if (!_dbus_message_set_reply_serial (message,
810 _dbus_message_get_client_serial (original_message)))
812 dbus_message_unref (message);
820 * Creates a new message that is an error reply to a certain message.
822 * @param original_message the original message
823 * @param error_name the error name
824 * @param error_message the error message string
825 * @returns a new error message
828 dbus_message_new_error_reply (DBusMessage *original_message,
829 const char *error_name,
830 const char *error_message)
832 DBusMessage *message;
835 sender = get_string_field (original_message,
838 _dbus_assert (sender != NULL);
840 message = dbus_message_new (sender, error_name);
845 if (!_dbus_message_set_reply_serial (message,
846 _dbus_message_get_client_serial (original_message)))
848 dbus_message_unref (message);
852 if (!dbus_message_append_string (message, error_message))
854 dbus_message_unref (message);
858 dbus_message_set_is_error (message, TRUE);
864 * Creates a new message that is an exact replica of the message
865 * specified, except that its refcount is set to 1.
867 * @param message the message.
868 * @returns the new message.
871 dbus_message_new_from_message (const DBusMessage *message)
876 retval = dbus_new0 (DBusMessage, 1);
880 retval->refcount = 1;
881 retval->byte_order = message->byte_order;
883 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
889 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
891 _dbus_string_free (&retval->header);
896 if (!_dbus_string_copy (&message->header, 0,
899 _dbus_string_free (&retval->header);
900 _dbus_string_free (&retval->body);
906 if (!_dbus_string_copy (&message->body, 0,
909 _dbus_string_free (&retval->header);
910 _dbus_string_free (&retval->body);
916 for (i = 0; i < FIELD_LAST; i++)
918 retval->header_fields[i].offset = message->header_fields[i].offset;
926 * Increments the reference count of a DBusMessage.
928 * @param message The message
929 * @see dbus_message_unref
932 dbus_message_ref (DBusMessage *message)
934 dbus_atomic_t refcount;
936 refcount = _dbus_atomic_inc (&message->refcount);
937 _dbus_assert (refcount > 1);
941 * Decrements the reference count of a DBusMessage.
943 * @param message The message
944 * @see dbus_message_ref
947 dbus_message_unref (DBusMessage *message)
949 dbus_atomic_t refcount;
951 refcount = _dbus_atomic_dec (&message->refcount);
953 _dbus_assert (refcount >= 0);
957 if (message->size_counter != NULL)
959 _dbus_counter_adjust (message->size_counter,
960 - message->size_counter_delta);
961 _dbus_counter_unref (message->size_counter);
964 _dbus_string_free (&message->header);
965 _dbus_string_free (&message->body);
972 * Gets the name of a message.
974 * @param message the message
975 * @returns the message name (should not be freed)
978 dbus_message_get_name (DBusMessage *message)
980 return get_string_field (message, FIELD_NAME, NULL);
984 * Gets the destination service of a message.
986 * @param message the message
987 * @returns the message destination service (should not be freed)
990 dbus_message_get_service (DBusMessage *message)
992 return get_string_field (message, FIELD_SERVICE, NULL);
996 * Appends fields to a message given a variable argument
997 * list. The variable argument list should contain the type
998 * of the argument followed by the value to add. Array values
999 * are specified by a pointer to the array followed by an int
1000 * giving the length of the array. The list is terminated
1003 * @param message the message
1004 * @param first_arg_type type of the first argument
1005 * @param ... value of first argument, list of additional type-value pairs
1006 * @returns #TRUE on success
1009 dbus_message_append_args (DBusMessage *message,
1016 va_start (var_args, first_arg_type);
1017 retval = dbus_message_append_args_valist (message,
1026 * This function takes a va_list for use by language bindings
1028 * @see dbus_message_append_args.
1029 * @param message the message
1030 * @param first_arg_type type of first argument
1031 * @param var_args value of first argument, then list of type/value pairs
1032 * @returns #TRUE on success
1035 dbus_message_append_args_valist (DBusMessage *message,
1041 old_len = _dbus_string_get_length (&message->body);
1043 type = first_arg_type;
1050 if (!dbus_message_append_nil (message))
1052 case DBUS_TYPE_INT32:
1053 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1056 case DBUS_TYPE_UINT32:
1057 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1060 case DBUS_TYPE_DOUBLE:
1061 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1064 case DBUS_TYPE_STRING:
1065 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1068 case DBUS_TYPE_INT32_ARRAY:
1073 data = va_arg (var_args, dbus_int32_t *);
1074 len = va_arg (var_args, int);
1076 if (!dbus_message_append_int32_array (message, data, len))
1080 case DBUS_TYPE_UINT32_ARRAY:
1083 dbus_uint32_t *data;
1085 data = va_arg (var_args, dbus_uint32_t *);
1086 len = va_arg (var_args, int);
1088 if (!dbus_message_append_uint32_array (message, data, len))
1092 case DBUS_TYPE_DOUBLE_ARRAY:
1097 data = va_arg (var_args, double *);
1098 len = va_arg (var_args, int);
1100 if (!dbus_message_append_double_array (message, data, len))
1104 case DBUS_TYPE_BYTE_ARRAY:
1107 unsigned char *data;
1109 data = va_arg (var_args, unsigned char *);
1110 len = va_arg (var_args, int);
1112 if (!dbus_message_append_byte_array (message, data, len))
1116 case DBUS_TYPE_STRING_ARRAY:
1121 data = va_arg (var_args, const char **);
1122 len = va_arg (var_args, int);
1124 if (!dbus_message_append_string_array (message, data, len))
1130 _dbus_warn ("Unknown field type %d\n", type);
1133 type = va_arg (var_args, int);
1143 * Appends a nil value to the message
1145 * @param message the message
1146 * @returns #TRUE on success
1149 dbus_message_append_nil (DBusMessage *message)
1151 _dbus_assert (!message->locked);
1153 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1160 * Appends a 32 bit signed integer to the message.
1162 * @param message the message
1163 * @param value the integer value
1164 * @returns #TRUE on success
1167 dbus_message_append_int32 (DBusMessage *message,
1170 _dbus_assert (!message->locked);
1172 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1175 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1177 _dbus_string_shorten (&message->body, 1);
1185 * Appends a 32 bit unsigned integer to the message.
1187 * @param message the message
1188 * @param value the integer value
1189 * @returns #TRUE on success
1192 dbus_message_append_uint32 (DBusMessage *message,
1193 dbus_uint32_t value)
1195 _dbus_assert (!message->locked);
1197 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1200 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1202 _dbus_string_shorten (&message->body, 1);
1210 * Appends a double value to the message.
1212 * @param message the message
1213 * @param value the double value
1214 * @returns #TRUE on success
1217 dbus_message_append_double (DBusMessage *message,
1220 _dbus_assert (!message->locked);
1222 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1225 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1227 _dbus_string_shorten (&message->body, 1);
1235 * Appends a UTF-8 string to the message.
1237 * @param message the message
1238 * @param value the string
1239 * @returns #TRUE on success
1242 dbus_message_append_string (DBusMessage *message,
1245 _dbus_assert (!message->locked);
1247 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1250 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1252 _dbus_string_shorten (&message->body, 1);
1260 * Appends a 32 bit signed integer array to the message.
1262 * @param message the message
1263 * @param value the array
1264 * @param len the length of the array
1265 * @returns #TRUE on success
1268 dbus_message_append_int32_array (DBusMessage *message,
1269 const dbus_int32_t *value,
1272 _dbus_assert (!message->locked);
1274 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1277 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1280 _dbus_string_shorten (&message->body, 1);
1288 * Appends a 32 bit unsigned integer array to the message.
1290 * @param message the message
1291 * @param value the array
1292 * @param len the length of the array
1293 * @returns #TRUE on success
1296 dbus_message_append_uint32_array (DBusMessage *message,
1297 const dbus_uint32_t *value,
1300 _dbus_assert (!message->locked);
1302 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1305 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1308 _dbus_string_shorten (&message->body, 1);
1316 * Appends a double array to the message.
1318 * @param message the message
1319 * @param value the array
1320 * @param len the length of the array
1321 * @returns #TRUE on success
1324 dbus_message_append_double_array (DBusMessage *message,
1325 const double *value,
1328 _dbus_assert (!message->locked);
1330 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1333 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1336 _dbus_string_shorten (&message->body, 1);
1344 * Appends a byte array to the message.
1346 * @param message the message
1347 * @param value the array
1348 * @param len the length of the array
1349 * @returns #TRUE on success
1352 dbus_message_append_byte_array (DBusMessage *message,
1353 unsigned const char *value,
1356 _dbus_assert (!message->locked);
1358 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1361 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1363 _dbus_string_shorten (&message->body, 1);
1371 * Appends a string array to the message.
1373 * @param message the message
1374 * @param value the array
1375 * @param len the length of the array
1376 * @returns #TRUE on success
1379 dbus_message_append_string_array (DBusMessage *message,
1383 _dbus_assert (!message->locked);
1385 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1388 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1391 _dbus_string_shorten (&message->body, 1);
1399 * Gets arguments from a message given a variable argument list.
1400 * The variable argument list should contain the type of the
1401 * argumen followed by a pointer to where the value should be
1402 * stored. The list is terminated with 0.
1404 * @param message the message
1405 * @param first_arg_type the first argument type
1406 * @param ... location for first argument value, then list of type-location pairs
1407 * @returns result code
1410 dbus_message_get_args (DBusMessage *message,
1414 DBusResultCode retval;
1417 va_start (var_args, first_arg_type);
1418 retval = dbus_message_get_args_valist (message, first_arg_type, var_args);
1425 * This function takes a va_list for use by language bindings
1427 * @todo this function (or some lower-level non-convenience function)
1428 * needs better error handling; should allow the application to
1429 * distinguish between out of memory, and bad data from the remote
1430 * app. It also needs to not leak a bunch of args when it gets
1431 * to the arg that's bad, as that would be a security hole
1432 * (allow one app to force another to leak memory)
1434 * @todo We need to free the argument data when an error occurs.
1436 * @see dbus_message_get_args
1437 * @param message the message
1438 * @param first_arg_type type of the first argument
1439 * @param var_args return location for first argument, followed by list of type/location pairs
1440 * @returns result code
1443 dbus_message_get_args_valist (DBusMessage *message,
1447 int spec_type, msg_type, i;
1448 DBusMessageIter *iter;
1450 iter = dbus_message_get_args_iter (message);
1453 return DBUS_RESULT_NO_MEMORY;
1455 spec_type = first_arg_type;
1458 while (spec_type != 0)
1460 msg_type = dbus_message_iter_get_arg_type (iter);
1462 if (msg_type != spec_type)
1464 _dbus_verbose ("Argument %d is specified to be of type \"%s\", but "
1465 "is actually of type \"%s\"\n", i,
1466 _dbus_type_to_string (spec_type),
1467 _dbus_type_to_string (msg_type));
1468 dbus_message_iter_unref (iter);
1470 return DBUS_RESULT_INVALID_ARGS;
1475 case DBUS_TYPE_INT32:
1479 ptr = va_arg (var_args, dbus_int32_t *);
1481 *ptr = dbus_message_iter_get_int32 (iter);
1484 case DBUS_TYPE_UINT32:
1488 ptr = va_arg (var_args, dbus_uint32_t *);
1490 *ptr = dbus_message_iter_get_uint32 (iter);
1494 case DBUS_TYPE_DOUBLE:
1498 ptr = va_arg (var_args, double *);
1500 *ptr = dbus_message_iter_get_double (iter);
1504 case DBUS_TYPE_STRING:
1508 ptr = va_arg (var_args, char **);
1510 *ptr = dbus_message_iter_get_string (iter);
1513 return DBUS_RESULT_NO_MEMORY;
1518 case DBUS_TYPE_INT32_ARRAY:
1523 ptr = va_arg (var_args, dbus_int32_t **);
1524 len = va_arg (var_args, int *);
1526 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1527 return DBUS_RESULT_NO_MEMORY;
1532 case DBUS_TYPE_UINT32_ARRAY:
1534 dbus_uint32_t **ptr;
1537 ptr = va_arg (var_args, dbus_uint32_t **);
1538 len = va_arg (var_args, int *);
1540 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1541 return DBUS_RESULT_NO_MEMORY;
1546 case DBUS_TYPE_DOUBLE_ARRAY:
1551 ptr = va_arg (var_args, double **);
1552 len = va_arg (var_args, int *);
1554 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1555 return DBUS_RESULT_NO_MEMORY;
1560 case DBUS_TYPE_BYTE_ARRAY:
1562 unsigned char **ptr;
1565 ptr = va_arg (var_args, unsigned char **);
1566 len = va_arg (var_args, int *);
1568 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1569 return DBUS_RESULT_NO_MEMORY;
1573 case DBUS_TYPE_STRING_ARRAY:
1578 ptr = va_arg (var_args, char ***);
1579 len = va_arg (var_args, int *);
1581 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1582 return DBUS_RESULT_NO_MEMORY;
1586 _dbus_warn ("Unknown field type %d\n", spec_type);
1589 spec_type = va_arg (var_args, int);
1590 if (spec_type != 0 && !dbus_message_iter_next (iter))
1592 _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
1594 dbus_message_iter_unref (iter);
1595 return DBUS_RESULT_INVALID_ARGS;
1600 dbus_message_iter_unref (iter);
1601 return DBUS_RESULT_SUCCESS;
1605 * Returns a DBusMessageIter representing the arguments of the
1606 * message passed in.
1608 * @todo IMO the message iter should follow the GtkTextIter pattern,
1609 * a static object with a "stamp" value used to detect invalid
1610 * iter uses (uninitialized or after changing the message).
1611 * ref/unref is kind of annoying to deal with, and slower too.
1612 * This implies not ref'ing the message from the iter.
1614 * @param message the message
1615 * @returns a new iter.
1618 dbus_message_get_args_iter (DBusMessage *message)
1620 DBusMessageIter *iter;
1622 iter = dbus_new (DBusMessageIter, 1);
1624 dbus_message_ref (message);
1627 iter->message = message;
1634 * Increments the reference count of a DBusMessageIter.
1636 * @param iter the message iter
1637 * @see dbus_message_iter_unref
1640 dbus_message_iter_ref (DBusMessageIter *iter)
1642 _dbus_assert (iter->refcount > 0);
1644 iter->refcount += 1;
1648 * Decrements the reference count of a DBusMessageIter.
1650 * @param iter The message iter
1651 * @see dbus_message_iter_ref
1654 dbus_message_iter_unref (DBusMessageIter *iter)
1656 _dbus_assert (iter->refcount > 0);
1658 iter->refcount -= 1;
1660 if (iter->refcount == 0)
1662 dbus_message_unref (iter->message);
1669 * Checks if an iterator has any more fields.
1671 * @param iter the message iter
1672 * @returns #TRUE if there are more fields
1676 dbus_message_iter_has_next (DBusMessageIter *iter)
1680 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1681 iter->message->byte_order,
1682 iter->pos, &end_pos))
1685 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1692 * Moves the iterator to the next field.
1694 * @param iter The message iter
1695 * @returns #TRUE if the iterator was moved to the next field
1698 dbus_message_iter_next (DBusMessageIter *iter)
1702 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1703 iter->message->byte_order,
1704 iter->pos, &end_pos))
1707 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1710 iter->pos = end_pos;
1716 * Returns the argument type of the argument that the
1717 * message iterator points at.
1719 * @param iter the message iter
1720 * @returns the field type
1723 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1727 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1728 return DBUS_TYPE_INVALID;
1730 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1732 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1735 return DBUS_TYPE_INVALID;
1739 * Returns the string value that an iterator may point to.
1740 * Note that you need to check that the iterator points to
1741 * a string value before using this function.
1743 * @see dbus_message_iter_get_field_type
1744 * @param iter the message iter
1745 * @returns the string
1748 dbus_message_iter_get_string (DBusMessageIter *iter)
1750 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1752 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1753 iter->pos + 1, NULL);
1757 * Returns the 32 bit signed integer value that an iterator may point to.
1758 * Note that you need to check that the iterator points to
1759 * an integer value before using this function.
1761 * @see dbus_message_iter_get_field_type
1762 * @param iter the message iter
1763 * @returns the integer
1766 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1768 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1769 iter->pos + 1, NULL);
1773 * Returns the 32 bit unsigned integer value that an iterator may point to.
1774 * Note that you need to check that the iterator points to
1775 * an unsigned integer value before using this function.
1777 * @see dbus_message_iter_get_field_type
1778 * @param iter the message iter
1779 * @returns the integer
1782 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1784 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1785 iter->pos + 1, NULL);
1789 * Returns the double value that an iterator may point to.
1790 * Note that you need to check that the iterator points to
1791 * a string value before using this function.
1793 * @see dbus_message_iter_get_field_type
1794 * @param iter the message iter
1795 * @returns the double
1798 dbus_message_iter_get_double (DBusMessageIter *iter)
1800 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1801 iter->pos + 1, NULL);
1805 * Returns the 32 bit signed integer array that the iterator may point
1806 * to. Note that you need to check that the iterator points to an
1807 * array of the correct type prior to using this function.
1809 * @param iter the iterator
1810 * @param value return location for the array
1811 * @param len return location for the array length
1812 * @returns #TRUE on success
1815 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
1816 dbus_int32_t **value,
1819 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
1821 *value = _dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
1822 iter->pos + 1, NULL, len);
1831 * Returns the 32 bit unsigned integer array that the iterator may point
1832 * to. Note that you need to check that the iterator points to an
1833 * array of the correct type prior to using this function.
1835 * @param iter the iterator
1836 * @param value return location for the array
1837 * @param len return location for the array length
1838 * @returns #TRUE on success
1841 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
1842 dbus_uint32_t **value,
1845 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
1847 *value = _dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
1848 iter->pos + 1, NULL, len);
1857 * Returns the double array that the iterator may point to. Note that
1858 * you need to check that the iterator points to an array of the
1859 * correct type prior to using this function.
1861 * @param iter the iterator
1862 * @param value return location for the array
1863 * @param len return location for the array length
1864 * @returns #TRUE on success
1867 dbus_message_iter_get_double_array (DBusMessageIter *iter,
1871 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
1873 *value = _dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
1874 iter->pos + 1, NULL, len);
1883 * Returns the byte array that the iterator may point to.
1884 * Note that you need to check that the iterator points
1885 * to a byte array prior to using this function.
1887 * @param iter the iterator
1888 * @param value return location for array values
1889 * @param len return location for length of byte array
1890 * @returns the byte array
1893 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
1894 unsigned char **value,
1897 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
1899 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1900 iter->pos + 1, NULL, len);
1909 * Returns the string array that the iterator may point to.
1910 * Note that you need to check that the iterator points
1911 * to a byte array prior to using this function.
1913 * The returned value is a #NULL-terminated array of strings.
1914 * Each string is a separate malloc block, and the array
1915 * itself is a malloc block. You can free this type of
1916 * string array with dbus_free_string_array().
1918 * @param iter the iterator
1919 * @param value return location for string values
1920 * @param len return location for length of byte array
1921 * @returns the byte array
1924 dbus_message_iter_get_string_array (DBusMessageIter *iter,
1928 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
1930 *value = _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
1931 iter->pos + 1, NULL, len);
1940 * Sets the message sender.
1942 * @param message the message
1943 * @param sender the sender
1944 * @returns #FALSE if not enough memory
1947 dbus_message_set_sender (DBusMessage *message,
1950 _dbus_assert (!message->locked);
1954 delete_string_field (message, FIELD_SENDER);
1959 return set_string_field (message,
1966 * Sets a flag indicating that the message is an error reply
1967 * message, i.e. an "exception" rather than a normal response.
1969 * @param message the message
1970 * @param is_error_reply #TRUE if this is an error message.
1973 dbus_message_set_is_error (DBusMessage *message,
1974 dbus_bool_t is_error_reply)
1978 _dbus_assert (!message->locked);
1980 _dbus_string_get_data_len (&message->header, &header, 1, 1);
1983 *header |= DBUS_HEADER_FLAG_ERROR;
1985 *header &= ~DBUS_HEADER_FLAG_ERROR;
1989 * Returns #TRUE if the message is an error
1990 * reply to some previous message we sent.
1992 * @param message the message
1993 * @returns #TRUE if the message is an error
1996 dbus_message_get_is_error (DBusMessage *message)
2000 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2002 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2006 * Gets the service which originated this message,
2007 * or #NULL if unknown or inapplicable.
2009 * @param message the message
2010 * @returns the service name or #NULL
2013 dbus_message_get_sender (DBusMessage *message)
2015 return get_string_field (message, FIELD_SENDER, NULL);
2019 * Checks whether the message has the given name.
2020 * If the message has no name or has a different
2021 * name, returns #FALSE.
2023 * @param message the message
2024 * @param name the name to check (must not be #NULL)
2026 * @returns #TRUE if the message has the given name
2029 dbus_message_name_is (DBusMessage *message,
2034 _dbus_assert (name != NULL);
2036 n = dbus_message_get_name (message);
2038 if (n && strcmp (n, name) == 0)
2047 * @addtogroup DBusMessageInternals
2052 * @typedef DBusMessageLoader
2054 * The DBusMessageLoader object encapsulates the process of converting
2055 * a byte stream into a series of DBusMessage. It buffers the incoming
2056 * bytes as efficiently as possible, and generates a queue of
2057 * messages. DBusMessageLoader is typically used as part of a
2058 * DBusTransport implementation. The DBusTransport then hands off
2059 * the loaded messages to a DBusConnection, making the messages
2060 * visible to the application.
2064 /* we definitely use signed ints for sizes, so don't exceed
2065 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2066 * over 128M is pretty nuts anyhow.
2070 * The maximum sane message size.
2072 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2075 * Implementation details of DBusMessageLoader.
2076 * All members are private.
2078 struct DBusMessageLoader
2080 int refcount; /**< Reference count. */
2082 DBusString data; /**< Buffered data */
2084 DBusList *messages; /**< Complete messages. */
2086 long max_message_size; /**< Maximum size of a message */
2088 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2090 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2094 * The initial buffer size of the message loader.
2096 * @todo this should be based on min header size plus some average
2097 * body size, or something. Or rather, the min header size only, if we
2098 * want to try to read only the header, store that in a DBusMessage,
2099 * then read only the body and store that, etc., depends on
2100 * how we optimize _dbus_message_loader_get_buffer() and what
2101 * the exact message format is.
2103 #define INITIAL_LOADER_DATA_LEN 32
2106 * Creates a new message loader. Returns #NULL if memory can't
2109 * @returns new loader, or #NULL.
2112 _dbus_message_loader_new (void)
2114 DBusMessageLoader *loader;
2116 loader = dbus_new0 (DBusMessageLoader, 1);
2120 loader->refcount = 1;
2122 /* Try to cap message size at something that won't *totally* hose
2123 * the system if we have a couple of them.
2125 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2127 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2133 /* preallocate the buffer for speed, ignore failure */
2134 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2135 _dbus_string_set_length (&loader->data, 0);
2141 * Increments the reference count of the loader.
2143 * @param loader the loader.
2146 _dbus_message_loader_ref (DBusMessageLoader *loader)
2148 loader->refcount += 1;
2152 * Decrements the reference count of the loader and finalizes the
2153 * loader when the count reaches zero.
2155 * @param loader the loader.
2158 _dbus_message_loader_unref (DBusMessageLoader *loader)
2160 loader->refcount -= 1;
2161 if (loader->refcount == 0)
2163 _dbus_list_foreach (&loader->messages,
2164 (DBusForeachFunction) dbus_message_unref,
2166 _dbus_list_clear (&loader->messages);
2167 _dbus_string_free (&loader->data);
2173 * Gets the buffer to use for reading data from the network. Network
2174 * data is read directly into an allocated buffer, which is then used
2175 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2176 * The buffer must always be returned immediately using
2177 * _dbus_message_loader_return_buffer(), even if no bytes are
2178 * successfully read.
2180 * @todo this function can be a lot more clever. For example
2181 * it can probably always return a buffer size to read exactly
2182 * the body of the next message, thus avoiding any memory wastage
2185 * @param loader the message loader.
2186 * @param buffer the buffer
2189 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2190 DBusString **buffer)
2192 _dbus_assert (!loader->buffer_outstanding);
2194 *buffer = &loader->data;
2196 loader->buffer_outstanding = TRUE;
2200 * The smallest header size that can occur.
2201 * (It won't be valid)
2203 #define DBUS_MINIMUM_HEADER_SIZE 16
2205 /** Pack four characters as in "abcd" into a uint32 */
2206 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2207 ((((dbus_uint32_t)a) << 24) | \
2208 (((dbus_uint32_t)b) << 16) | \
2209 (((dbus_uint32_t)c) << 8) | \
2212 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2213 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2214 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2216 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2217 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2218 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2220 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2221 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2222 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2224 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2225 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2226 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2229 decode_header_data (const DBusString *data,
2232 HeaderField fields[FIELD_LAST],
2233 int *message_padding)
2239 if (header_len < 16)
2243 while (i < FIELD_LAST)
2245 fields[i].offset = -1;
2249 fields[FIELD_HEADER_LENGTH].offset = 4;
2250 fields[FIELD_BODY_LENGTH].offset = 8;
2251 fields[FIELD_CLIENT_SERIAL].offset = 12;
2253 /* Now handle the named fields. A real named field is at least 4
2254 * bytes for the name, plus a type code (1 byte) plus padding. So
2255 * if we have less than 8 bytes left, it must be alignment padding,
2256 * not a field. While >= 8 bytes can't be entirely alignment
2260 while ((pos + 7) < header_len)
2262 pos = _DBUS_ALIGN_VALUE (pos, 4);
2264 if ((pos + 4) > header_len)
2267 _dbus_string_get_const_data_len (data, &field, pos, 4);
2270 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2272 /* I believe FROM_BE is right, but if not we'll find out
2275 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2277 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2278 if (fields[FIELD_SERVICE].offset >= 0)
2280 _dbus_verbose ("%s field provided twice\n",
2281 DBUS_HEADER_FIELD_SERVICE);
2285 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2286 _dbus_verbose ("Found service name at offset %d\n",
2287 fields[FIELD_SERVICE].offset);
2290 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2291 if (fields[FIELD_NAME].offset >= 0)
2293 _dbus_verbose ("%s field provided twice\n",
2294 DBUS_HEADER_FIELD_NAME);
2298 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2300 _dbus_verbose ("Found message name at offset %d\n",
2301 fields[FIELD_NAME].offset);
2303 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2304 if (fields[FIELD_SENDER].offset >= 0)
2306 _dbus_verbose ("%s field provided twice\n",
2307 DBUS_HEADER_FIELD_SENDER);
2311 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2313 _dbus_verbose ("Found sender name at offset %d\n",
2314 fields[FIELD_NAME].offset);
2317 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2318 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2320 _dbus_verbose ("%s field provided twice\n",
2321 DBUS_HEADER_FIELD_REPLY);
2325 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2327 _dbus_verbose ("Found reply serial at offset %d\n",
2328 fields[FIELD_REPLY_SERIAL].offset);
2332 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2333 field[0], field[1], field[2], field[3], pos);
2336 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2338 _dbus_verbose ("Failed to validate argument to named header field\n");
2342 if (new_pos > header_len)
2344 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2351 if (pos < header_len)
2353 /* Alignment padding, verify that it's nul */
2354 _dbus_assert ((header_len - pos) < 8);
2356 if (!_dbus_string_validate_nul (data,
2357 pos, (header_len - pos)))
2359 _dbus_verbose ("header alignment padding is not nul\n");
2363 if (message_padding)
2364 *message_padding = header_len - pos;
2371 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2372 * indicating to the loader how many bytes of the buffer were filled
2373 * in. This function must always be called, even if no bytes were
2374 * successfully read.
2376 * @todo if we run out of memory in here, we offer no way for calling
2377 * code to handle it, i.e. they can't re-run the message parsing
2378 * attempt. Perhaps much of this code could be moved to pop_message()?
2379 * But then that may need to distinguish NULL return for no messages
2380 * from NULL return for errors.
2382 * @param loader the loader.
2383 * @param buffer the buffer.
2384 * @param bytes_read number of bytes that were read into the buffer.
2387 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2391 _dbus_assert (loader->buffer_outstanding);
2392 _dbus_assert (buffer == &loader->data);
2394 loader->buffer_outstanding = FALSE;
2396 if (loader->corrupted)
2399 while (_dbus_string_get_length (&loader->data) >= 16)
2401 DBusMessage *message;
2402 const char *header_data;
2403 int byte_order, header_len, body_len, header_padding;
2404 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2406 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2408 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2410 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2412 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2413 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2414 loader->corrupted = TRUE;
2418 byte_order = header_data[0];
2420 if (byte_order != DBUS_LITTLE_ENDIAN &&
2421 byte_order != DBUS_BIG_ENDIAN)
2423 _dbus_verbose ("Message with bad byte order '%c' received\n",
2425 loader->corrupted = TRUE;
2429 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2430 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2432 if (header_len_unsigned < 16)
2434 _dbus_verbose ("Message had broken too-small header length %u\n",
2435 header_len_unsigned);
2436 loader->corrupted = TRUE;
2440 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2441 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2443 _dbus_verbose ("Header or body length too large (%u %u)\n",
2444 header_len_unsigned,
2446 loader->corrupted = TRUE;
2450 /* Now that we know the values are in signed range, get
2451 * rid of stupid unsigned, just causes bugs
2453 header_len = header_len_unsigned;
2454 body_len = body_len_unsigned;
2456 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2458 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2460 loader->corrupted = TRUE;
2464 if (header_len + body_len > loader->max_message_size)
2466 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2467 header_len, body_len, loader->max_message_size);
2468 loader->corrupted = TRUE;
2472 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2474 HeaderField fields[FIELD_LAST];
2478 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len);
2479 if (!decode_header_data (&loader->data, header_len, byte_order,
2480 fields, &header_padding))
2482 _dbus_verbose ("Header was invalid\n");
2483 loader->corrupted = TRUE;
2487 next_arg = header_len;
2488 while (next_arg < (header_len + body_len))
2490 int prev = next_arg;
2492 if (!_dbus_marshal_validate_arg (&loader->data,
2497 loader->corrupted = TRUE;
2501 _dbus_assert (next_arg > prev);
2504 if (next_arg > (header_len + body_len))
2506 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2507 next_arg, header_len, body_len,
2508 header_len + body_len);
2509 loader->corrupted = TRUE;
2513 message = dbus_message_new_empty_header ();
2514 if (message == NULL)
2515 break; /* ugh, postpone this I guess. */
2517 message->byte_order = byte_order;
2518 message->header_padding = header_padding;
2520 /* Copy in the offsets we found */
2522 while (i < FIELD_LAST)
2524 message->header_fields[i] = fields[i];
2528 if (!_dbus_list_append (&loader->messages, message))
2530 dbus_message_unref (message);
2534 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2535 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2537 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2538 (header_len + body_len));
2540 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2542 _dbus_list_remove_last (&loader->messages, message);
2543 dbus_message_unref (message);
2547 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2551 /* put the header back, we'll try again later */
2552 result = _dbus_string_copy_len (&message->header, 0, header_len,
2554 _dbus_assert (result); /* because DBusString never reallocs smaller */
2556 _dbus_list_remove_last (&loader->messages, message);
2557 dbus_message_unref (message);
2561 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2562 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2564 _dbus_verbose ("Loaded message %p\n", message);
2572 * Pops a loaded message (passing ownership of the message
2573 * to the caller). Returns #NULL if no messages have been
2576 * @param loader the loader.
2577 * @returns the next message, or #NULL if none.
2580 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2582 return _dbus_list_pop_first (&loader->messages);
2587 * Checks whether the loader is confused due to bad data.
2588 * If messages are received that are invalid, the
2589 * loader gets confused and gives up permanently.
2590 * This state is called "corrupted."
2592 * @param loader the loader
2593 * @returns #TRUE if the loader is hosed.
2596 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2598 return loader->corrupted;
2602 * Sets the maximum size message we allow.
2604 * @param loader the loader
2605 * @param size the max message size in bytes
2608 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2611 if (size > MAX_SANE_MESSAGE_SIZE)
2613 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2614 size, MAX_SANE_MESSAGE_SIZE);
2615 size = MAX_SANE_MESSAGE_SIZE;
2617 loader->max_message_size = size;
2621 * Gets the maximum allowed message size in bytes.
2623 * @param loader the loader
2624 * @returns max size in bytes
2627 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2629 return loader->max_message_size;
2633 #ifdef DBUS_BUILD_TESTS
2634 #include "dbus-test.h"
2638 message_iter_test (DBusMessage *message)
2640 DBusMessageIter *iter;
2643 iter = dbus_message_get_args_iter (message);
2646 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2647 _dbus_assert_not_reached ("Argument type isn't string");
2649 str = dbus_message_iter_get_string (iter);
2650 if (strcmp (str, "Test string") != 0)
2651 _dbus_assert_not_reached ("Strings differ");
2654 if (!dbus_message_iter_next (iter))
2655 _dbus_assert_not_reached ("Reached end of arguments");
2657 /* Signed integer tests */
2658 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2659 _dbus_assert_not_reached ("Argument type isn't int32");
2661 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2662 _dbus_assert_not_reached ("Signed integers differ");
2664 if (!dbus_message_iter_next (iter))
2665 _dbus_assert_not_reached ("Reached end of fields");
2667 /* Unsigned integer tests */
2668 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2669 _dbus_assert_not_reached ("Argument type isn't int32");
2671 if (dbus_message_iter_get_int32 (iter) != 0xedd1e)
2672 _dbus_assert_not_reached ("Unsigned integers differ");
2674 if (!dbus_message_iter_next (iter))
2675 _dbus_assert_not_reached ("Reached end of arguments");
2678 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
2679 _dbus_assert_not_reached ("Argument type isn't double");
2681 if (dbus_message_iter_get_double (iter) != 3.14159)
2682 _dbus_assert_not_reached ("Doubles differ");
2684 if (dbus_message_iter_next (iter))
2685 _dbus_assert_not_reached ("Didn't reach end of arguments");
2687 dbus_message_iter_unref (iter);
2691 check_message_handling (DBusMessage *message)
2693 DBusMessageIter *iter;
2696 dbus_int32_t client_serial;
2701 client_serial = _dbus_message_get_client_serial (message);
2703 /* can't use set_client_serial due to the assertions at the start of it */
2704 set_int_field (message, FIELD_CLIENT_SERIAL,
2707 if (client_serial != _dbus_message_get_client_serial (message))
2709 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
2713 /* If we implement message_set_arg (message, n, value)
2714 * then we would want to test it here
2717 iter = dbus_message_get_args_iter (message);
2718 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
2724 case DBUS_TYPE_INT32:
2725 dbus_message_iter_get_int32 (iter);
2727 case DBUS_TYPE_UINT32:
2728 dbus_message_iter_get_uint32 (iter);
2730 case DBUS_TYPE_DOUBLE:
2731 dbus_message_iter_get_double (iter);
2733 case DBUS_TYPE_STRING:
2736 str = dbus_message_iter_get_string (iter);
2740 case DBUS_TYPE_INT32_ARRAY:
2742 dbus_int32_t *values;
2745 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
2751 case DBUS_TYPE_UINT32_ARRAY:
2753 dbus_uint32_t *values;
2756 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
2762 case DBUS_TYPE_DOUBLE_ARRAY:
2767 if (!dbus_message_iter_get_double_array (iter, &values, &len))
2773 case DBUS_TYPE_STRING_ARRAY:
2778 if (!dbus_message_iter_get_string_array (iter, &values, &len))
2781 dbus_free_string_array (values);
2789 if (!dbus_message_iter_next (iter))
2797 dbus_message_iter_unref (iter);
2803 check_have_valid_message (DBusMessageLoader *loader)
2805 DBusMessage *message;
2811 if (_dbus_message_loader_get_is_corrupted (loader))
2813 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
2817 message = _dbus_message_loader_pop_message (loader);
2818 if (message == NULL)
2820 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
2824 if (_dbus_string_get_length (&loader->data) > 0)
2826 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
2830 /* Verify that we're able to properly deal with the message.
2831 * For example, this would detect improper handling of messages
2832 * in nonstandard byte order.
2834 if (!check_message_handling (message))
2841 dbus_message_unref (message);
2846 check_invalid_message (DBusMessageLoader *loader)
2852 if (!_dbus_message_loader_get_is_corrupted (loader))
2854 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
2865 check_incomplete_message (DBusMessageLoader *loader)
2867 DBusMessage *message;
2873 if (_dbus_message_loader_get_is_corrupted (loader))
2875 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
2879 message = _dbus_message_loader_pop_message (loader);
2880 if (message != NULL)
2882 _dbus_warn ("loaded message that was expected to be incomplete\n");
2890 dbus_message_unref (message);
2895 check_loader_results (DBusMessageLoader *loader,
2896 DBusMessageValidity validity)
2900 case _DBUS_MESSAGE_VALID:
2901 return check_have_valid_message (loader);
2902 case _DBUS_MESSAGE_INVALID:
2903 return check_invalid_message (loader);
2904 case _DBUS_MESSAGE_INCOMPLETE:
2905 return check_incomplete_message (loader);
2906 case _DBUS_MESSAGE_UNKNOWN:
2910 _dbus_assert_not_reached ("bad DBusMessageValidity");
2916 * Loads the message in the given message file.
2918 * @param filename filename to load
2919 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
2920 * @param data string to load message into
2921 * @returns #TRUE if the message was loaded
2924 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
2934 DBusResultCode result;
2936 result = _dbus_file_get_contents (data, filename);
2937 if (result != DBUS_RESULT_SUCCESS)
2940 _dbus_string_get_const_data (filename, &s);
2941 _dbus_warn ("Could not load message file %s\n", s);
2947 if (!_dbus_message_data_load (data, filename))
2950 _dbus_string_get_const_data (filename, &s);
2951 _dbus_warn ("Could not load message file %s\n", s);
2964 * Tries loading the message in the given message file
2965 * and verifies that DBusMessageLoader can handle it.
2967 * @param filename filename to load
2968 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
2969 * @param expected_validity what the message has to be like to return #TRUE
2970 * @returns #TRUE if the message has the expected validity
2973 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
2975 DBusMessageValidity expected_validity)
2982 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
2983 _dbus_assert_not_reached ("could not allocate string\n");
2985 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
2989 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
2997 if (_dbus_string_get_length (&data) > 0)
2998 _dbus_verbose_bytes_of_string (&data, 0,
2999 _dbus_string_get_length (&data));
3001 _dbus_string_get_const_data (filename, &s);
3002 _dbus_warn ("Failed message loader test on %s\n",
3006 _dbus_string_free (&data);
3012 * Tries loading the given message data.
3015 * @param data the message data
3016 * @param expected_validity what the message has to be like to return #TRUE
3017 * @returns #TRUE if the message has the expected validity
3020 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3021 DBusMessageValidity expected_validity)
3023 DBusMessageLoader *loader;
3031 /* Write the data one byte at a time */
3033 loader = _dbus_message_loader_new ();
3035 len = _dbus_string_get_length (data);
3036 for (i = 0; i < len; i++)
3040 _dbus_message_loader_get_buffer (loader, &buffer);
3041 _dbus_string_append_byte (buffer,
3042 _dbus_string_get_byte (data, i));
3043 _dbus_message_loader_return_buffer (loader, buffer, 1);
3046 if (!check_loader_results (loader, expected_validity))
3049 _dbus_message_loader_unref (loader);
3052 /* Write the data all at once */
3054 loader = _dbus_message_loader_new ();
3059 _dbus_message_loader_get_buffer (loader, &buffer);
3060 _dbus_string_copy (data, 0, buffer,
3061 _dbus_string_get_length (buffer));
3062 _dbus_message_loader_return_buffer (loader, buffer, 1);
3065 if (!check_loader_results (loader, expected_validity))
3068 _dbus_message_loader_unref (loader);
3071 /* Write the data 2 bytes at a time */
3073 loader = _dbus_message_loader_new ();
3075 len = _dbus_string_get_length (data);
3076 for (i = 0; i < len; i += 2)
3080 _dbus_message_loader_get_buffer (loader, &buffer);
3081 _dbus_string_append_byte (buffer,
3082 _dbus_string_get_byte (data, i));
3084 _dbus_string_append_byte (buffer,
3085 _dbus_string_get_byte (data, i+1));
3086 _dbus_message_loader_return_buffer (loader, buffer, 1);
3089 if (!check_loader_results (loader, expected_validity))
3092 _dbus_message_loader_unref (loader);
3100 _dbus_message_loader_unref (loader);
3106 process_test_subdir (const DBusString *test_base_dir,
3108 DBusMessageValidity validity,
3109 DBusForeachMessageFileFunc function,
3112 DBusString test_directory;
3113 DBusString filename;
3116 DBusResultCode result;
3121 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3122 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3124 _dbus_string_init_const (&filename, subdir);
3126 if (!_dbus_string_copy (test_base_dir, 0,
3127 &test_directory, 0))
3128 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3130 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3131 _dbus_assert_not_reached ("couldn't allocate full path");
3133 _dbus_string_free (&filename);
3134 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3135 _dbus_assert_not_reached ("didn't allocate filename string\n");
3137 dir = _dbus_directory_open (&test_directory, &result);
3141 _dbus_string_get_const_data (&test_directory, &s);
3142 _dbus_warn ("Could not open %s: %s\n", s,
3143 dbus_result_to_string (result));
3147 printf ("Testing:\n");
3149 result = DBUS_RESULT_SUCCESS;
3151 while (_dbus_directory_get_next_file (dir, &filename, &result))
3153 DBusString full_path;
3156 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3157 _dbus_assert_not_reached ("couldn't init string");
3159 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3160 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3162 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3163 _dbus_assert_not_reached ("couldn't concat file to dir");
3165 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3167 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3171 const char *filename_c;
3172 _dbus_string_get_const_data (&filename, &filename_c);
3173 _dbus_verbose ("Skipping non-.message file %s\n",
3175 _dbus_string_free (&full_path);
3181 _dbus_string_get_const_data (&filename, &s);
3182 printf (" %s\n", s);
3185 _dbus_verbose (" expecting %s\n",
3186 validity == _DBUS_MESSAGE_VALID ? "valid" :
3187 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3188 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3190 if (! (*function) (&full_path, is_raw, validity, user_data))
3192 _dbus_string_free (&full_path);
3196 _dbus_string_free (&full_path);
3199 if (result != DBUS_RESULT_SUCCESS)
3202 _dbus_string_get_const_data (&test_directory, &s);
3203 _dbus_warn ("Could not get next file in %s: %s\n",
3204 s, dbus_result_to_string (result));
3213 _dbus_directory_close (dir);
3214 _dbus_string_free (&test_directory);
3215 _dbus_string_free (&filename);
3221 * Runs the given function on every message file in the test suite.
3222 * The function should return #FALSE on test failure or fatal error.
3224 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3225 * @param func the function to run
3226 * @param user_data data for function
3227 * @returns #FALSE if there's a failure
3230 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3231 DBusForeachMessageFileFunc func,
3234 DBusString test_directory;
3239 _dbus_string_init_const (&test_directory, test_data_dir);
3241 if (!process_test_subdir (&test_directory, "valid-messages",
3242 _DBUS_MESSAGE_VALID, func, user_data))
3245 if (!process_test_subdir (&test_directory, "invalid-messages",
3246 _DBUS_MESSAGE_INVALID, func, user_data))
3249 if (!process_test_subdir (&test_directory, "incomplete-messages",
3250 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3257 _dbus_string_free (&test_directory);
3263 * @ingroup DBusMessageInternals
3264 * Unit test for DBusMessage.
3266 * @returns #TRUE on success.
3269 _dbus_message_test (const char *test_data_dir)
3271 DBusMessage *message;
3272 DBusMessageLoader *loader;
3275 dbus_int32_t our_int;
3279 /* Test the vararg functions */
3280 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3281 _dbus_message_set_client_serial (message, 1);
3282 dbus_message_append_args (message,
3283 DBUS_TYPE_INT32, -0x12345678,
3284 DBUS_TYPE_STRING, "Test string",
3285 DBUS_TYPE_DOUBLE, 3.14159,
3287 _dbus_verbose_bytes_of_string (&message->header, 0,
3288 _dbus_string_get_length (&message->header));
3289 _dbus_verbose_bytes_of_string (&message->body, 0,
3290 _dbus_string_get_length (&message->body));
3292 if (dbus_message_get_args (message,
3293 DBUS_TYPE_INT32, &our_int,
3294 DBUS_TYPE_STRING, &our_str,
3295 DBUS_TYPE_DOUBLE, &our_double,
3296 0) != DBUS_RESULT_SUCCESS)
3297 _dbus_assert_not_reached ("Could not get arguments");
3299 if (our_int != -0x12345678)
3300 _dbus_assert_not_reached ("integers differ!");
3302 if (our_double != 3.14159)
3303 _dbus_assert_not_reached ("doubles differ!");
3305 if (strcmp (our_str, "Test string") != 0)
3306 _dbus_assert_not_reached ("strings differ!");
3308 dbus_free (our_str);
3309 dbus_message_unref (message);
3311 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3312 _dbus_message_set_client_serial (message, 1);
3313 _dbus_message_set_reply_serial (message, 0x12345678);
3315 dbus_message_append_string (message, "Test string");
3316 dbus_message_append_int32 (message, -0x12345678);
3317 dbus_message_append_uint32 (message, 0xedd1e);
3318 dbus_message_append_double (message, 3.14159);
3320 message_iter_test (message);
3322 /* Message loader test */
3323 _dbus_message_lock (message);
3324 loader = _dbus_message_loader_new ();
3326 /* Write the header data one byte at a time */
3327 _dbus_string_get_const_data (&message->header, &data);
3328 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3332 _dbus_message_loader_get_buffer (loader, &buffer);
3333 _dbus_string_append_byte (buffer, data[i]);
3334 _dbus_message_loader_return_buffer (loader, buffer, 1);
3337 /* Write the body data one byte at a time */
3338 _dbus_string_get_const_data (&message->body, &data);
3339 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3343 _dbus_message_loader_get_buffer (loader, &buffer);
3344 _dbus_string_append_byte (buffer, data[i]);
3345 _dbus_message_loader_return_buffer (loader, buffer, 1);
3348 dbus_message_unref (message);
3350 /* Now pop back the message */
3351 if (_dbus_message_loader_get_is_corrupted (loader))
3352 _dbus_assert_not_reached ("message loader corrupted");
3354 message = _dbus_message_loader_pop_message (loader);
3356 _dbus_assert_not_reached ("received a NULL message");
3358 if (_dbus_message_get_reply_serial (message) != 0x12345678)
3359 _dbus_assert_not_reached ("reply serial fields differ");
3361 message_iter_test (message);
3363 dbus_message_unref (message);
3364 _dbus_message_loader_unref (loader);
3366 /* Now load every message in test_data_dir if we have one */
3367 if (test_data_dir == NULL)
3370 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3371 (DBusForeachMessageFileFunc)
3372 dbus_internal_do_not_use_try_message_file,
3376 #endif /* DBUS_BUILD_TESTS */