1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal.h"
27 #include "dbus-message.h"
28 #include "dbus-message-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-list.h"
31 #include "dbus-message-builder.h"
35 * @defgroup DBusMessageInternals DBusMessage implementation details
36 * @ingroup DBusInternals
37 * @brief DBusMessage private implementation details.
39 * The guts of DBusMessage and its methods.
57 static dbus_bool_t field_is_named[FIELD_LAST] =
59 FALSE, /* FIELD_HEADER_LENGTH */
60 FALSE, /* FIELD_BODY_LENGTH */
61 FALSE, /* FIELD_CLIENT_SERIAL */
62 TRUE, /* FIELD_NAME */
63 TRUE, /* FIELD_SERVICE */
64 TRUE, /* FIELD_SENDER */
65 TRUE /* FIELD_REPLY_SERIAL */
70 int offset; /**< Offset to start of field (location of name of field
76 * @brief Internals of DBusMessage
78 * Object representing a message received from or to be sent to
79 * another application. This is an opaque object, all members
84 dbus_atomic_t refcount; /**< Reference count */
86 DBusString header; /**< Header network data, stored
87 * separately from body so we can
88 * independently realloc it.
91 HeaderField header_fields[FIELD_LAST]; /**< Track the location
92 * of each field in "header"
95 dbus_int32_t client_serial; /**< Cached client serial value for speed */
96 dbus_int32_t reply_serial; /**< Cached reply serial value for speed */
98 int header_padding; /**< bytes of alignment in header */
100 DBusString body; /**< Body network data. */
102 char byte_order; /**< Message byte order. */
104 DBusCounter *size_counter; /**< Counter for the size of the message, or #NULL */
105 long size_counter_delta; /**< Size we incremented the size counter by. */
107 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
111 * @brief Internals of DBusMessageIter
113 * Object representing a position in a message. All fields are internal.
115 struct DBusMessageIter
117 int refcount; /**< Reference count */
119 int pos; /**< Current position in the string */
121 DBusMessage *message; /**< Message used */
125 * Gets the data to be sent over the network for this message.
126 * The header and then the body should be written out.
127 * This function is guaranteed to always return the same
128 * data once a message is locked (with _dbus_message_lock()).
130 * @param message the message.
131 * @param header return location for message header data.
132 * @param body return location for message body data.
135 _dbus_message_get_network_data (DBusMessage *message,
136 const DBusString **header,
137 const DBusString **body)
139 _dbus_assert (message->locked);
141 *header = &message->header;
142 *body = &message->body;
146 clear_header_padding (DBusMessage *message)
148 _dbus_string_shorten (&message->header,
149 message->header_padding);
150 message->header_padding = 0;
154 append_header_padding (DBusMessage *message)
157 old_len = _dbus_string_get_length (&message->header);
158 if (!_dbus_string_align_length (&message->header, 8))
161 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
167 adjust_field_offsets (DBusMessage *message,
177 while (i < FIELD_LAST)
179 if (message->header_fields[i].offset > offsets_after)
180 message->header_fields[i].offset += delta;
187 get_string_field (DBusMessage *message,
194 offset = message->header_fields[field].offset;
196 _dbus_assert (field < FIELD_LAST);
201 /* offset points to string length, string data follows it */
202 /* FIXME _dbus_demarshal_const_string() that returned
203 * a reference to the string plus its len might be nice.
207 *len = _dbus_demarshal_uint32 (&message->header,
212 _dbus_string_get_const_data (&message->header,
215 return data + (offset + 4);
219 get_int_field (DBusMessage *message,
224 _dbus_assert (field < FIELD_LAST);
226 offset = message->header_fields[field].offset;
229 return -1; /* useless if -1 is a valid value of course */
231 return _dbus_demarshal_int32 (&message->header,
238 append_int_field (DBusMessage *message,
245 _dbus_assert (!message->locked);
247 clear_header_padding (message);
249 orig_len = _dbus_string_get_length (&message->header);
251 if (!_dbus_string_align_length (&message->header, 4))
254 if (!_dbus_string_append_len (&message->header, name, 4))
257 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
260 if (!_dbus_string_align_length (&message->header, 4))
263 message->header_fields[FIELD_REPLY_SERIAL].offset =
264 _dbus_string_get_length (&message->header);
266 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
270 if (!append_header_padding (message))
276 message->header_fields[field].offset = -1;
277 _dbus_string_set_length (&message->header, orig_len);
279 /* this must succeed because it was allocated on function entry and
280 * DBusString doesn't ever realloc smaller
282 if (!append_header_padding (message))
283 _dbus_assert_not_reached ("failed to reappend header padding");
288 append_string_field (DBusMessage *message,
295 _dbus_assert (!message->locked);
297 clear_header_padding (message);
299 orig_len = _dbus_string_get_length (&message->header);
301 if (!_dbus_string_align_length (&message->header, 4))
304 if (!_dbus_string_append_len (&message->header, name, 4))
307 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
310 if (!_dbus_string_align_length (&message->header, 4))
313 message->header_fields[field].offset =
314 _dbus_string_get_length (&message->header);
316 if (!_dbus_marshal_string (&message->header, message->byte_order,
320 if (!append_header_padding (message))
326 message->header_fields[field].offset = -1;
327 _dbus_string_set_length (&message->header, orig_len);
329 /* this must succeed because it was allocated on function entry and
330 * DBusString doesn't ever realloc smaller
332 if (!append_header_padding (message))
333 _dbus_assert_not_reached ("failed to reappend header padding");
339 delete_int_field (DBusMessage *message,
342 int offset = message->header_fields[field].offset;
344 _dbus_assert (!message->locked);
345 _dbus_assert (field_is_named[field]);
350 clear_header_padding (message);
352 /* The field typecode and name take up 8 bytes */
353 _dbus_string_delete (&message->header,
357 message->header_fields[field].offset = -1;
359 adjust_field_offsets (message,
363 append_header_padding (message);
367 delete_string_field (DBusMessage *message,
370 int offset = message->header_fields[field].offset;
374 _dbus_assert (!message->locked);
375 _dbus_assert (field_is_named[field]);
380 clear_header_padding (message);
382 get_string_field (message, field, &len);
384 /* The field typecode and name take up 8 bytes, and the nul
385 * termination is 1 bytes, string length integer is 4 bytes
387 delete_len = 8 + 4 + 1 + len;
389 _dbus_string_delete (&message->header,
393 message->header_fields[field].offset = -1;
395 adjust_field_offsets (message,
399 append_header_padding (message);
403 set_int_field (DBusMessage *message,
407 int offset = message->header_fields[field].offset;
409 _dbus_assert (!message->locked);
413 /* need to append the field */
417 case FIELD_REPLY_SERIAL:
418 return append_int_field (message, field,
419 DBUS_HEADER_FIELD_REPLY,
422 _dbus_assert_not_reached ("appending an int field we don't support appending");
428 _dbus_marshal_set_int32 (&message->header,
437 set_uint_field (DBusMessage *message,
441 int offset = message->header_fields[field].offset;
443 _dbus_assert (!message->locked);
447 /* need to append the field */
452 _dbus_assert_not_reached ("appending a uint field we don't support appending");
458 _dbus_marshal_set_uint32 (&message->header,
467 set_string_field (DBusMessage *message,
471 int offset = message->header_fields[field].offset;
473 _dbus_assert (!message->locked);
474 _dbus_assert (value != NULL);
478 /* need to append the field */
483 return append_string_field (message, field,
484 DBUS_HEADER_FIELD_SENDER,
487 _dbus_assert_not_reached ("appending a string field we don't support appending");
497 old_len = _dbus_string_get_length (&message->header);
499 _dbus_string_init_const_len (&v, value,
500 strlen (value) + 1); /* include nul */
501 if (!_dbus_marshal_set_string (&message->header,
506 new_len = _dbus_string_get_length (&message->header);
508 adjust_field_offsets (message,
517 * Sets the client serial of a message.
518 * This can only be done once on a message.
520 * @todo client_serial should be called simply
521 * "serial"; it's in outgoing messages for both
522 * the client and the server, it's only client-specific
523 * in the message bus case. It's more like origin_serial
526 * @param message the message
527 * @param client_serial the client serial
530 _dbus_message_set_client_serial (DBusMessage *message,
531 dbus_int32_t client_serial)
533 _dbus_assert (!message->locked);
534 _dbus_assert (_dbus_message_get_client_serial (message) < 0);
536 set_int_field (message, FIELD_CLIENT_SERIAL,
538 message->client_serial = client_serial;
542 * Sets the reply serial of a message (the client serial
543 * of the message this is a reply to).
545 * @param message the message
546 * @param reply_serial the client serial
547 * @returns #FALSE if not enough memory
550 _dbus_message_set_reply_serial (DBusMessage *message,
551 dbus_int32_t reply_serial)
553 _dbus_assert (!message->locked);
555 if (set_int_field (message, FIELD_REPLY_SERIAL,
558 message->reply_serial = reply_serial;
566 * Returns the client serial of a message or
567 * -1 if none has been specified.
569 * @todo see note in _dbus_message_set_client_serial()
570 * about how client_serial is a misnomer
572 * @todo this function should be public, after renaming it.
574 * @param message the message
575 * @returns the client serial
578 _dbus_message_get_client_serial (DBusMessage *message)
580 return message->client_serial;
584 * Returns the serial that the message is
585 * a reply to or -1 if none.
587 * @param message the message
588 * @returns the reply serial
591 _dbus_message_get_reply_serial (DBusMessage *message)
593 return message->reply_serial;
597 * Adds a counter to be incremented immediately with the
598 * size of this message, and decremented by the size
599 * of this message when this message if finalized.
601 * @param message the message
602 * @param counter the counter
605 _dbus_message_add_size_counter (DBusMessage *message,
606 DBusCounter *counter)
608 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
609 * counters instead of just one
612 message->size_counter = counter;
613 _dbus_counter_ref (message->size_counter);
615 /* When we can change message size, we may want to
616 * update this each time we do so, or we may want to
617 * just KISS like this.
619 message->size_counter_delta =
620 _dbus_string_get_length (&message->header) +
621 _dbus_string_get_length (&message->body);
623 _dbus_verbose ("message has size %ld\n",
624 message->size_counter_delta);
626 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
630 dbus_message_create_header (DBusMessage *message,
636 if (!_dbus_string_append_byte (&message->header, message->byte_order))
640 if (!_dbus_string_append_byte (&message->header, flags))
643 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
646 if (!_dbus_string_append_byte (&message->header, 0))
649 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
650 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
653 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
654 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
657 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
658 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
661 /* Marshal message service */
664 if (!append_string_field (message,
666 DBUS_HEADER_FIELD_SERVICE,
671 _dbus_assert (name != NULL);
672 if (!append_string_field (message,
674 DBUS_HEADER_FIELD_NAME,
682 * Locks a message. Allows checking that applications don't keep a
683 * reference to a message in the outgoing queue and change it
684 * underneath us. Messages are locked when they enter the outgoing
685 * queue (dbus_connection_send_message()), and the library complains
686 * if the message is modified while locked.
688 * @param message the message to lock.
691 _dbus_message_lock (DBusMessage *message)
693 if (!message->locked)
695 /* Fill in our lengths */
696 set_uint_field (message,
698 _dbus_string_get_length (&message->header));
700 set_uint_field (message,
702 _dbus_string_get_length (&message->body));
704 message->locked = TRUE;
711 * @defgroup DBusMessage DBusMessage
713 * @brief Message to be sent or received over a DBusConnection.
715 * A DBusMessage is the most basic unit of communication over a
716 * DBusConnection. A DBusConnection represents a stream of messages
717 * received from a remote application, and a stream of messages
718 * sent to a remote application.
724 * @typedef DBusMessage
726 * Opaque data type representing a message received from or to be
727 * sent to another application.
731 dbus_message_new_empty_header (void)
733 DBusMessage *message;
736 message = dbus_new0 (DBusMessage, 1);
740 message->refcount = 1;
741 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
742 message->client_serial = -1;
743 message->reply_serial = -1;
746 while (i < FIELD_LAST)
748 message->header_fields[i].offset = -1;
752 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
758 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
760 _dbus_string_free (&message->header);
770 * Constructs a new message. Returns #NULL if memory can't be
771 * allocated for the message. The service may be #NULL in which case
772 * no service is set; this is appropriate when using D-BUS in a
773 * peer-to-peer context (no message bus).
775 * @todo reverse the arguments, first 'name' then 'service'
776 * as 'name' is more fundamental
778 * @param service service that the message should be sent to or #NULL
779 * @param name name of the message
780 * @returns a new DBusMessage, free with dbus_message_unref()
781 * @see dbus_message_unref()
784 dbus_message_new (const char *service,
787 DBusMessage *message;
789 message = dbus_message_new_empty_header ();
793 if (!dbus_message_create_header (message, service, name))
795 dbus_message_unref (message);
803 * Constructs a message that is a reply to some other
804 * message. Returns #NULL if memory can't be allocated
807 * @param original_message the message which the created
808 * message is a reply to.
809 * @returns a new DBusMessage, free with dbus_message_unref()
810 * @see dbus_message_new(), dbus_message_unref()
813 dbus_message_new_reply (DBusMessage *original_message)
815 DBusMessage *message;
816 const char *sender, *name;
818 sender = get_string_field (original_message,
820 name = get_string_field (original_message,
823 /* sender is allowed to be null here in peer-to-peer case */
825 message = dbus_message_new (sender, name);
830 if (!_dbus_message_set_reply_serial (message,
831 _dbus_message_get_client_serial (original_message)))
833 dbus_message_unref (message);
841 * Creates a new message that is an error reply to a certain message.
843 * @param original_message the original message
844 * @param error_name the error name
845 * @param error_message the error message string
846 * @returns a new error message
849 dbus_message_new_error_reply (DBusMessage *original_message,
850 const char *error_name,
851 const char *error_message)
853 DBusMessage *message;
856 sender = get_string_field (original_message,
859 _dbus_assert (sender != NULL);
861 message = dbus_message_new (sender, error_name);
866 if (!_dbus_message_set_reply_serial (message,
867 _dbus_message_get_client_serial (original_message)))
869 dbus_message_unref (message);
873 if (!dbus_message_append_string (message, error_message))
875 dbus_message_unref (message);
879 dbus_message_set_is_error (message, TRUE);
885 * Creates a new message that is an exact replica of the message
886 * specified, except that its refcount is set to 1.
888 * @param message the message.
889 * @returns the new message.
892 dbus_message_new_from_message (const DBusMessage *message)
897 retval = dbus_new0 (DBusMessage, 1);
901 retval->refcount = 1;
902 retval->byte_order = message->byte_order;
904 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
910 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
912 _dbus_string_free (&retval->header);
917 if (!_dbus_string_copy (&message->header, 0,
920 _dbus_string_free (&retval->header);
921 _dbus_string_free (&retval->body);
927 if (!_dbus_string_copy (&message->body, 0,
930 _dbus_string_free (&retval->header);
931 _dbus_string_free (&retval->body);
937 for (i = 0; i < FIELD_LAST; i++)
939 retval->header_fields[i].offset = message->header_fields[i].offset;
947 * Increments the reference count of a DBusMessage.
949 * @param message The message
950 * @see dbus_message_unref
953 dbus_message_ref (DBusMessage *message)
955 dbus_atomic_t refcount;
957 refcount = _dbus_atomic_inc (&message->refcount);
958 _dbus_assert (refcount > 1);
962 * Decrements the reference count of a DBusMessage.
964 * @param message The message
965 * @see dbus_message_ref
968 dbus_message_unref (DBusMessage *message)
970 dbus_atomic_t refcount;
972 refcount = _dbus_atomic_dec (&message->refcount);
974 _dbus_assert (refcount >= 0);
978 if (message->size_counter != NULL)
980 _dbus_counter_adjust (message->size_counter,
981 - message->size_counter_delta);
982 _dbus_counter_unref (message->size_counter);
985 _dbus_string_free (&message->header);
986 _dbus_string_free (&message->body);
993 * Gets the name of a message.
995 * @param message the message
996 * @returns the message name (should not be freed)
999 dbus_message_get_name (DBusMessage *message)
1001 return get_string_field (message, FIELD_NAME, NULL);
1005 * Gets the destination service of a message.
1007 * @param message the message
1008 * @returns the message destination service (should not be freed)
1011 dbus_message_get_service (DBusMessage *message)
1013 return get_string_field (message, FIELD_SERVICE, NULL);
1017 * Appends fields to a message given a variable argument
1018 * list. The variable argument list should contain the type
1019 * of the argument followed by the value to add. Array values
1020 * are specified by a pointer to the array followed by an int
1021 * giving the length of the array. The list is terminated
1024 * @param message the message
1025 * @param first_arg_type type of the first argument
1026 * @param ... value of first argument, list of additional type-value pairs
1027 * @returns #TRUE on success
1030 dbus_message_append_args (DBusMessage *message,
1037 va_start (var_args, first_arg_type);
1038 retval = dbus_message_append_args_valist (message,
1047 * This function takes a va_list for use by language bindings
1049 * @see dbus_message_append_args.
1050 * @param message the message
1051 * @param first_arg_type type of first argument
1052 * @param var_args value of first argument, then list of type/value pairs
1053 * @returns #TRUE on success
1056 dbus_message_append_args_valist (DBusMessage *message,
1062 old_len = _dbus_string_get_length (&message->body);
1064 type = first_arg_type;
1071 if (!dbus_message_append_nil (message))
1073 case DBUS_TYPE_BOOLEAN:
1074 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1076 case DBUS_TYPE_INT32:
1077 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1080 case DBUS_TYPE_UINT32:
1081 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1084 case DBUS_TYPE_DOUBLE:
1085 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1088 case DBUS_TYPE_STRING:
1089 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1092 case DBUS_TYPE_BOOLEAN_ARRAY:
1095 unsigned char *data;
1097 data = va_arg (var_args, unsigned char *);
1098 len = va_arg (var_args, int);
1100 if (!dbus_message_append_boolean_array (message, data, len))
1104 case DBUS_TYPE_INT32_ARRAY:
1109 data = va_arg (var_args, dbus_int32_t *);
1110 len = va_arg (var_args, int);
1112 if (!dbus_message_append_int32_array (message, data, len))
1116 case DBUS_TYPE_UINT32_ARRAY:
1119 dbus_uint32_t *data;
1121 data = va_arg (var_args, dbus_uint32_t *);
1122 len = va_arg (var_args, int);
1124 if (!dbus_message_append_uint32_array (message, data, len))
1128 case DBUS_TYPE_DOUBLE_ARRAY:
1133 data = va_arg (var_args, double *);
1134 len = va_arg (var_args, int);
1136 if (!dbus_message_append_double_array (message, data, len))
1140 case DBUS_TYPE_BYTE_ARRAY:
1143 unsigned char *data;
1145 data = va_arg (var_args, unsigned char *);
1146 len = va_arg (var_args, int);
1148 if (!dbus_message_append_byte_array (message, data, len))
1152 case DBUS_TYPE_STRING_ARRAY:
1157 data = va_arg (var_args, const char **);
1158 len = va_arg (var_args, int);
1160 if (!dbus_message_append_string_array (message, data, len))
1166 _dbus_warn ("Unknown field type %d\n", type);
1169 type = va_arg (var_args, int);
1179 * Appends a nil value to the message
1181 * @param message the message
1182 * @returns #TRUE on success
1185 dbus_message_append_nil (DBusMessage *message)
1187 _dbus_assert (!message->locked);
1189 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1196 * Appends a boolean value to the message
1198 * @param message the message
1199 * @param value the boolean value
1200 * @returns #TRUE on success
1203 dbus_message_append_boolean (DBusMessage *message,
1206 _dbus_assert (!message->locked);
1208 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1211 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1213 _dbus_string_shorten (&message->body, 1);
1221 * Appends a 32 bit signed integer to the message.
1223 * @param message the message
1224 * @param value the integer value
1225 * @returns #TRUE on success
1228 dbus_message_append_int32 (DBusMessage *message,
1231 _dbus_assert (!message->locked);
1233 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1236 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1238 _dbus_string_shorten (&message->body, 1);
1246 * Appends a 32 bit unsigned integer to the message.
1248 * @param message the message
1249 * @param value the integer value
1250 * @returns #TRUE on success
1253 dbus_message_append_uint32 (DBusMessage *message,
1254 dbus_uint32_t value)
1256 _dbus_assert (!message->locked);
1258 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1261 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1263 _dbus_string_shorten (&message->body, 1);
1271 * Appends a double value to the message.
1273 * @param message the message
1274 * @param value the double value
1275 * @returns #TRUE on success
1278 dbus_message_append_double (DBusMessage *message,
1281 _dbus_assert (!message->locked);
1283 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1286 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1288 _dbus_string_shorten (&message->body, 1);
1296 * Appends a UTF-8 string to the message.
1298 * @param message the message
1299 * @param value the string
1300 * @returns #TRUE on success
1303 dbus_message_append_string (DBusMessage *message,
1306 _dbus_assert (!message->locked);
1308 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1311 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1313 _dbus_string_shorten (&message->body, 1);
1321 * Appends a boolean array to the message.
1323 * @param message the message
1324 * @param value the array
1325 * @param len the length of the array
1326 * @returns #TRUE on success
1329 dbus_message_append_boolean_array (DBusMessage *message,
1330 unsigned const char *value,
1333 _dbus_assert (!message->locked);
1335 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1338 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1340 _dbus_string_shorten (&message->body, 1);
1348 * Appends a 32 bit signed integer array to the message.
1350 * @param message the message
1351 * @param value the array
1352 * @param len the length of the array
1353 * @returns #TRUE on success
1356 dbus_message_append_int32_array (DBusMessage *message,
1357 const dbus_int32_t *value,
1360 _dbus_assert (!message->locked);
1362 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1365 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1368 _dbus_string_shorten (&message->body, 1);
1376 * Appends a 32 bit unsigned 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_uint32_array (DBusMessage *message,
1385 const dbus_uint32_t *value,
1388 _dbus_assert (!message->locked);
1390 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1393 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1396 _dbus_string_shorten (&message->body, 1);
1404 * Appends a double 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_double_array (DBusMessage *message,
1413 const double *value,
1416 _dbus_assert (!message->locked);
1418 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1421 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1424 _dbus_string_shorten (&message->body, 1);
1432 * Appends a byte 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_byte_array (DBusMessage *message,
1441 unsigned const char *value,
1444 _dbus_assert (!message->locked);
1446 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1449 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1451 _dbus_string_shorten (&message->body, 1);
1459 * Appends a string array to the message.
1461 * @param message the message
1462 * @param value the array
1463 * @param len the length of the array
1464 * @returns #TRUE on success
1467 dbus_message_append_string_array (DBusMessage *message,
1471 _dbus_assert (!message->locked);
1473 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1476 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1479 _dbus_string_shorten (&message->body, 1);
1487 * Gets arguments from a message given a variable argument list.
1488 * The variable argument list should contain the type of the
1489 * argumen followed by a pointer to where the value should be
1490 * stored. The list is terminated with 0.
1492 * @param message the message
1493 * @param first_arg_type the first argument type
1494 * @param ... location for first argument value, then list of type-location pairs
1495 * @returns result code
1498 dbus_message_get_args (DBusMessage *message,
1502 DBusResultCode retval;
1505 va_start (var_args, first_arg_type);
1506 retval = dbus_message_get_args_valist (message, first_arg_type, var_args);
1513 * This function takes a va_list for use by language bindings
1515 * @todo this function (or some lower-level non-convenience function)
1516 * needs better error handling; should allow the application to
1517 * distinguish between out of memory, and bad data from the remote
1518 * app. It also needs to not leak a bunch of args when it gets
1519 * to the arg that's bad, as that would be a security hole
1520 * (allow one app to force another to leak memory)
1522 * @todo We need to free the argument data when an error occurs.
1524 * @see dbus_message_get_args
1525 * @param message the message
1526 * @param first_arg_type type of the first argument
1527 * @param var_args return location for first argument, followed by list of type/location pairs
1528 * @returns result code
1531 dbus_message_get_args_valist (DBusMessage *message,
1535 int spec_type, msg_type, i;
1536 DBusMessageIter *iter;
1538 iter = dbus_message_get_args_iter (message);
1541 return DBUS_RESULT_NO_MEMORY;
1543 spec_type = first_arg_type;
1546 while (spec_type != 0)
1548 msg_type = dbus_message_iter_get_arg_type (iter);
1550 if (msg_type != spec_type)
1552 _dbus_verbose ("Argument %d is specified to be of type \"%s\", but "
1553 "is actually of type \"%s\"\n", i,
1554 _dbus_type_to_string (spec_type),
1555 _dbus_type_to_string (msg_type));
1556 dbus_message_iter_unref (iter);
1558 return DBUS_RESULT_INVALID_ARGS;
1565 case DBUS_TYPE_BOOLEAN:
1569 ptr = va_arg (var_args, dbus_bool_t *);
1571 *ptr = dbus_message_iter_get_boolean (iter);
1574 case DBUS_TYPE_INT32:
1578 ptr = va_arg (var_args, dbus_int32_t *);
1580 *ptr = dbus_message_iter_get_int32 (iter);
1583 case DBUS_TYPE_UINT32:
1587 ptr = va_arg (var_args, dbus_uint32_t *);
1589 *ptr = dbus_message_iter_get_uint32 (iter);
1593 case DBUS_TYPE_DOUBLE:
1597 ptr = va_arg (var_args, double *);
1599 *ptr = dbus_message_iter_get_double (iter);
1603 case DBUS_TYPE_STRING:
1607 ptr = va_arg (var_args, char **);
1609 *ptr = dbus_message_iter_get_string (iter);
1612 return DBUS_RESULT_NO_MEMORY;
1617 case DBUS_TYPE_BOOLEAN_ARRAY:
1619 unsigned char **ptr;
1622 ptr = va_arg (var_args, unsigned char **);
1623 len = va_arg (var_args, int *);
1625 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1626 return DBUS_RESULT_NO_MEMORY;
1631 case DBUS_TYPE_INT32_ARRAY:
1636 ptr = va_arg (var_args, dbus_int32_t **);
1637 len = va_arg (var_args, int *);
1639 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1640 return DBUS_RESULT_NO_MEMORY;
1645 case DBUS_TYPE_UINT32_ARRAY:
1647 dbus_uint32_t **ptr;
1650 ptr = va_arg (var_args, dbus_uint32_t **);
1651 len = va_arg (var_args, int *);
1653 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1654 return DBUS_RESULT_NO_MEMORY;
1659 case DBUS_TYPE_DOUBLE_ARRAY:
1664 ptr = va_arg (var_args, double **);
1665 len = va_arg (var_args, int *);
1667 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1668 return DBUS_RESULT_NO_MEMORY;
1673 case DBUS_TYPE_BYTE_ARRAY:
1675 unsigned char **ptr;
1678 ptr = va_arg (var_args, unsigned char **);
1679 len = va_arg (var_args, int *);
1681 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1682 return DBUS_RESULT_NO_MEMORY;
1686 case DBUS_TYPE_STRING_ARRAY:
1691 ptr = va_arg (var_args, char ***);
1692 len = va_arg (var_args, int *);
1694 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1695 return DBUS_RESULT_NO_MEMORY;
1699 _dbus_warn ("Unknown field type %d\n", spec_type);
1702 spec_type = va_arg (var_args, int);
1703 if (spec_type != 0 && !dbus_message_iter_next (iter))
1705 _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
1707 dbus_message_iter_unref (iter);
1708 return DBUS_RESULT_INVALID_ARGS;
1713 dbus_message_iter_unref (iter);
1714 return DBUS_RESULT_SUCCESS;
1718 * Returns a DBusMessageIter representing the arguments of the
1719 * message passed in.
1721 * @todo IMO the message iter should follow the GtkTextIter pattern,
1722 * a static object with a "stamp" value used to detect invalid
1723 * iter uses (uninitialized or after changing the message).
1724 * ref/unref is kind of annoying to deal with, and slower too.
1725 * This implies not ref'ing the message from the iter.
1727 * @todo I'd also name this dbus_message_iter_new() or
1728 * for the static object dbus_message_iter_init() rather
1729 * than making it a method on the message
1731 * @param message the message
1732 * @returns a new iter.
1735 dbus_message_get_args_iter (DBusMessage *message)
1737 DBusMessageIter *iter;
1739 iter = dbus_new (DBusMessageIter, 1);
1743 dbus_message_ref (message);
1746 iter->message = message;
1754 * Increments the reference count of a DBusMessageIter.
1756 * @param iter the message iter
1757 * @see dbus_message_iter_unref
1760 dbus_message_iter_ref (DBusMessageIter *iter)
1762 _dbus_assert (iter->refcount > 0);
1764 iter->refcount += 1;
1768 * Decrements the reference count of a DBusMessageIter.
1770 * @param iter The message iter
1771 * @see dbus_message_iter_ref
1774 dbus_message_iter_unref (DBusMessageIter *iter)
1776 _dbus_assert (iter->refcount > 0);
1778 iter->refcount -= 1;
1780 if (iter->refcount == 0)
1782 dbus_message_unref (iter->message);
1789 * Checks if an iterator has any more fields.
1791 * @param iter the message iter
1792 * @returns #TRUE if there are more fields
1796 dbus_message_iter_has_next (DBusMessageIter *iter)
1800 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1801 iter->message->byte_order,
1802 iter->pos, &end_pos))
1805 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1812 * Moves the iterator to the next field.
1814 * @param iter The message iter
1815 * @returns #TRUE if the iterator was moved to the next field
1818 dbus_message_iter_next (DBusMessageIter *iter)
1822 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1823 iter->message->byte_order,
1824 iter->pos, &end_pos))
1827 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1830 iter->pos = end_pos;
1836 * Returns the argument type of the argument that the
1837 * message iterator points at.
1839 * @param iter the message iter
1840 * @returns the field type
1843 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1847 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1848 return DBUS_TYPE_INVALID;
1850 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1852 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1855 return DBUS_TYPE_INVALID;
1859 * Returns the string value that an iterator may point to.
1860 * Note that you need to check that the iterator points to
1861 * a string value before using this function.
1863 * @see dbus_message_iter_get_arg_type
1864 * @param iter the message iter
1865 * @returns the string
1868 dbus_message_iter_get_string (DBusMessageIter *iter)
1870 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1872 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1873 iter->pos + 1, NULL);
1877 * Returns the boolean value that an iterator may point to.
1878 * Note that you need to check that the iterator points to
1879 * a boolean value before using this function.
1881 * @see dbus_message_iter_get_arg_type
1882 * @param iter the message iter
1883 * @returns the string
1886 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1888 unsigned char value;
1890 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1892 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
1899 * Returns the 32 bit signed integer value that an iterator may point to.
1900 * Note that you need to check that the iterator points to
1901 * an integer value before using this function.
1903 * @see dbus_message_iter_get_arg_type
1904 * @param iter the message iter
1905 * @returns the integer
1908 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1910 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
1912 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1913 iter->pos + 1, NULL);
1917 * Returns the 32 bit unsigned integer value that an iterator may point to.
1918 * Note that you need to check that the iterator points to
1919 * an unsigned integer value before using this function.
1921 * @see dbus_message_iter_get_arg_type
1922 * @param iter the message iter
1923 * @returns the integer
1926 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1928 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
1930 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1931 iter->pos + 1, NULL);
1935 * Returns the double value that an iterator may point to.
1936 * Note that you need to check that the iterator points to
1937 * a string value before using this function.
1939 * @see dbus_message_iter_get_arg_type
1940 * @param iter the message iter
1941 * @returns the double
1944 dbus_message_iter_get_double (DBusMessageIter *iter)
1946 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
1948 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1949 iter->pos + 1, NULL);
1953 * Returns the boolean array that the iterator may point to. Note that
1954 * you need to check that the iterator points to an array of the
1955 * correct type prior to using this function.
1957 * @param iter the iterator
1958 * @param value return location for the array
1959 * @param len return location for the array length
1960 * @returns #TRUE on success
1963 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
1964 unsigned char **value,
1967 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
1969 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1970 iter->pos + 1, NULL, len);
1979 * Returns the 32 bit signed integer array that the iterator may point
1980 * to. Note that you need to check that the iterator points to an
1981 * array of the correct type prior to using this function.
1983 * @param iter the iterator
1984 * @param value return location for the array
1985 * @param len return location for the array length
1986 * @returns #TRUE on success
1989 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
1990 dbus_int32_t **value,
1993 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
1995 *value = _dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
1996 iter->pos + 1, NULL, len);
2005 * Returns the 32 bit unsigned integer array that the iterator may point
2006 * to. Note that you need to check that the iterator points to an
2007 * array of the correct type prior to using this function.
2009 * @param iter the iterator
2010 * @param value return location for the array
2011 * @param len return location for the array length
2012 * @returns #TRUE on success
2015 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2016 dbus_uint32_t **value,
2019 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2021 *value = _dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2022 iter->pos + 1, NULL, len);
2031 * Returns the double array that the iterator may point to. Note that
2032 * you need to check that the iterator points to an array of the
2033 * correct type prior to using this function.
2035 * @param iter the iterator
2036 * @param value return location for the array
2037 * @param len return location for the array length
2038 * @returns #TRUE on success
2041 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2045 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2047 *value = _dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2048 iter->pos + 1, NULL, len);
2057 * Returns the byte array that the iterator may point to.
2058 * Note that you need to check that the iterator points
2059 * to a byte array prior to using this function.
2061 * @param iter the iterator
2062 * @param value return location for array values
2063 * @param len return location for length of byte array
2064 * @returns the byte array
2067 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2068 unsigned char **value,
2071 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2073 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2074 iter->pos + 1, NULL, len);
2083 * Returns the string array that the iterator may point to.
2084 * Note that you need to check that the iterator points
2085 * to a byte array prior to using this function.
2087 * The returned value is a #NULL-terminated array of strings.
2088 * Each string is a separate malloc block, and the array
2089 * itself is a malloc block. You can free this type of
2090 * string array with dbus_free_string_array().
2092 * @param iter the iterator
2093 * @param value return location for string values
2094 * @param len return location for length of byte array
2095 * @returns the byte array
2098 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2102 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2104 *value = _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2105 iter->pos + 1, NULL, len);
2114 * Sets the message sender.
2116 * @param message the message
2117 * @param sender the sender
2118 * @returns #FALSE if not enough memory
2121 dbus_message_set_sender (DBusMessage *message,
2124 _dbus_assert (!message->locked);
2128 delete_string_field (message, FIELD_SENDER);
2133 return set_string_field (message,
2140 * Sets a flag indicating that the message is an error reply
2141 * message, i.e. an "exception" rather than a normal response.
2143 * @param message the message
2144 * @param is_error_reply #TRUE if this is an error message.
2147 dbus_message_set_is_error (DBusMessage *message,
2148 dbus_bool_t is_error_reply)
2152 _dbus_assert (!message->locked);
2154 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2157 *header |= DBUS_HEADER_FLAG_ERROR;
2159 *header &= ~DBUS_HEADER_FLAG_ERROR;
2163 * Returns #TRUE if the message is an error
2164 * reply to some previous message we sent.
2166 * @param message the message
2167 * @returns #TRUE if the message is an error
2170 dbus_message_get_is_error (DBusMessage *message)
2174 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2176 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2180 * Gets the service which originated this message,
2181 * or #NULL if unknown or inapplicable.
2183 * @param message the message
2184 * @returns the service name or #NULL
2187 dbus_message_get_sender (DBusMessage *message)
2189 return get_string_field (message, FIELD_SENDER, NULL);
2193 * Checks whether the message has the given name.
2194 * If the message has no name or has a different
2195 * name, returns #FALSE.
2197 * @param message the message
2198 * @param name the name to check (must not be #NULL)
2200 * @returns #TRUE if the message has the given name
2203 dbus_message_name_is (DBusMessage *message,
2208 _dbus_assert (name != NULL);
2210 n = dbus_message_get_name (message);
2212 if (n && strcmp (n, name) == 0)
2221 * @addtogroup DBusMessageInternals
2226 * @typedef DBusMessageLoader
2228 * The DBusMessageLoader object encapsulates the process of converting
2229 * a byte stream into a series of DBusMessage. It buffers the incoming
2230 * bytes as efficiently as possible, and generates a queue of
2231 * messages. DBusMessageLoader is typically used as part of a
2232 * DBusTransport implementation. The DBusTransport then hands off
2233 * the loaded messages to a DBusConnection, making the messages
2234 * visible to the application.
2238 /* we definitely use signed ints for sizes, so don't exceed
2239 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2240 * over 128M is pretty nuts anyhow.
2244 * The maximum sane message size.
2246 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2249 * Implementation details of DBusMessageLoader.
2250 * All members are private.
2252 struct DBusMessageLoader
2254 int refcount; /**< Reference count. */
2256 DBusString data; /**< Buffered data */
2258 DBusList *messages; /**< Complete messages. */
2260 long max_message_size; /**< Maximum size of a message */
2262 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2264 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2268 * The initial buffer size of the message loader.
2270 * @todo this should be based on min header size plus some average
2271 * body size, or something. Or rather, the min header size only, if we
2272 * want to try to read only the header, store that in a DBusMessage,
2273 * then read only the body and store that, etc., depends on
2274 * how we optimize _dbus_message_loader_get_buffer() and what
2275 * the exact message format is.
2277 #define INITIAL_LOADER_DATA_LEN 32
2280 * Creates a new message loader. Returns #NULL if memory can't
2283 * @returns new loader, or #NULL.
2286 _dbus_message_loader_new (void)
2288 DBusMessageLoader *loader;
2290 loader = dbus_new0 (DBusMessageLoader, 1);
2294 loader->refcount = 1;
2296 /* Try to cap message size at something that won't *totally* hose
2297 * the system if we have a couple of them.
2299 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2301 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2307 /* preallocate the buffer for speed, ignore failure */
2308 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2309 _dbus_string_set_length (&loader->data, 0);
2315 * Increments the reference count of the loader.
2317 * @param loader the loader.
2320 _dbus_message_loader_ref (DBusMessageLoader *loader)
2322 loader->refcount += 1;
2326 * Decrements the reference count of the loader and finalizes the
2327 * loader when the count reaches zero.
2329 * @param loader the loader.
2332 _dbus_message_loader_unref (DBusMessageLoader *loader)
2334 loader->refcount -= 1;
2335 if (loader->refcount == 0)
2337 _dbus_list_foreach (&loader->messages,
2338 (DBusForeachFunction) dbus_message_unref,
2340 _dbus_list_clear (&loader->messages);
2341 _dbus_string_free (&loader->data);
2347 * Gets the buffer to use for reading data from the network. Network
2348 * data is read directly into an allocated buffer, which is then used
2349 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2350 * The buffer must always be returned immediately using
2351 * _dbus_message_loader_return_buffer(), even if no bytes are
2352 * successfully read.
2354 * @todo this function can be a lot more clever. For example
2355 * it can probably always return a buffer size to read exactly
2356 * the body of the next message, thus avoiding any memory wastage
2359 * @param loader the message loader.
2360 * @param buffer the buffer
2363 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2364 DBusString **buffer)
2366 _dbus_assert (!loader->buffer_outstanding);
2368 *buffer = &loader->data;
2370 loader->buffer_outstanding = TRUE;
2374 * The smallest header size that can occur.
2375 * (It won't be valid)
2377 #define DBUS_MINIMUM_HEADER_SIZE 16
2379 /** Pack four characters as in "abcd" into a uint32 */
2380 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2381 ((((dbus_uint32_t)a) << 24) | \
2382 (((dbus_uint32_t)b) << 16) | \
2383 (((dbus_uint32_t)c) << 8) | \
2386 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2387 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2388 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2390 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2391 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2392 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2394 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2395 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2396 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2398 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2399 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2400 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2403 decode_header_data (const DBusString *data,
2406 HeaderField fields[FIELD_LAST],
2407 int *message_padding)
2413 if (header_len < 16)
2417 while (i < FIELD_LAST)
2419 fields[i].offset = -1;
2423 fields[FIELD_HEADER_LENGTH].offset = 4;
2424 fields[FIELD_BODY_LENGTH].offset = 8;
2425 fields[FIELD_CLIENT_SERIAL].offset = 12;
2427 /* Now handle the named fields. A real named field is at least 4
2428 * bytes for the name, plus a type code (1 byte) plus padding. So
2429 * if we have less than 8 bytes left, it must be alignment padding,
2430 * not a field. While >= 8 bytes can't be entirely alignment
2434 while ((pos + 7) < header_len)
2436 pos = _DBUS_ALIGN_VALUE (pos, 4);
2438 if ((pos + 4) > header_len)
2441 _dbus_string_get_const_data_len (data, &field, pos, 4);
2444 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2446 /* I believe FROM_BE is right, but if not we'll find out
2449 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2451 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2452 if (fields[FIELD_SERVICE].offset >= 0)
2454 _dbus_verbose ("%s field provided twice\n",
2455 DBUS_HEADER_FIELD_SERVICE);
2459 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2460 _dbus_verbose ("Found service name at offset %d\n",
2461 fields[FIELD_SERVICE].offset);
2464 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2465 if (fields[FIELD_NAME].offset >= 0)
2467 _dbus_verbose ("%s field provided twice\n",
2468 DBUS_HEADER_FIELD_NAME);
2472 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2474 _dbus_verbose ("Found message name at offset %d\n",
2475 fields[FIELD_NAME].offset);
2477 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2478 if (fields[FIELD_SENDER].offset >= 0)
2480 _dbus_verbose ("%s field provided twice\n",
2481 DBUS_HEADER_FIELD_SENDER);
2485 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2487 _dbus_verbose ("Found sender name at offset %d\n",
2488 fields[FIELD_NAME].offset);
2491 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2492 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2494 _dbus_verbose ("%s field provided twice\n",
2495 DBUS_HEADER_FIELD_REPLY);
2499 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2501 _dbus_verbose ("Found reply serial at offset %d\n",
2502 fields[FIELD_REPLY_SERIAL].offset);
2506 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2507 field[0], field[1], field[2], field[3], pos);
2510 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2512 _dbus_verbose ("Failed to validate argument to named header field\n");
2516 if (new_pos > header_len)
2518 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2525 if (pos < header_len)
2527 /* Alignment padding, verify that it's nul */
2528 _dbus_assert ((header_len - pos) < 8);
2530 if (!_dbus_string_validate_nul (data,
2531 pos, (header_len - pos)))
2533 _dbus_verbose ("header alignment padding is not nul\n");
2537 if (message_padding)
2538 *message_padding = header_len - pos;
2545 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2546 * indicating to the loader how many bytes of the buffer were filled
2547 * in. This function must always be called, even if no bytes were
2548 * successfully read.
2550 * @todo if we run out of memory in here, we offer no way for calling
2551 * code to handle it, i.e. they can't re-run the message parsing
2552 * attempt. Perhaps much of this code could be moved to pop_message()?
2553 * But then that may need to distinguish NULL return for no messages
2554 * from NULL return for errors.
2556 * @param loader the loader.
2557 * @param buffer the buffer.
2558 * @param bytes_read number of bytes that were read into the buffer.
2561 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2565 _dbus_assert (loader->buffer_outstanding);
2566 _dbus_assert (buffer == &loader->data);
2568 loader->buffer_outstanding = FALSE;
2570 if (loader->corrupted)
2573 while (_dbus_string_get_length (&loader->data) >= 16)
2575 DBusMessage *message;
2576 const char *header_data;
2577 int byte_order, header_len, body_len, header_padding;
2578 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2580 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2582 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2584 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2586 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2587 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2588 loader->corrupted = TRUE;
2592 byte_order = header_data[0];
2594 if (byte_order != DBUS_LITTLE_ENDIAN &&
2595 byte_order != DBUS_BIG_ENDIAN)
2597 _dbus_verbose ("Message with bad byte order '%c' received\n",
2599 loader->corrupted = TRUE;
2603 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2604 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2606 if (header_len_unsigned < 16)
2608 _dbus_verbose ("Message had broken too-small header length %u\n",
2609 header_len_unsigned);
2610 loader->corrupted = TRUE;
2614 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2615 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2617 _dbus_verbose ("Header or body length too large (%u %u)\n",
2618 header_len_unsigned,
2620 loader->corrupted = TRUE;
2624 /* Now that we know the values are in signed range, get
2625 * rid of stupid unsigned, just causes bugs
2627 header_len = header_len_unsigned;
2628 body_len = body_len_unsigned;
2630 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2632 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2634 loader->corrupted = TRUE;
2638 if (header_len + body_len > loader->max_message_size)
2640 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2641 header_len, body_len, loader->max_message_size);
2642 loader->corrupted = TRUE;
2646 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2648 HeaderField fields[FIELD_LAST];
2652 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len);
2653 if (!decode_header_data (&loader->data, header_len, byte_order,
2654 fields, &header_padding))
2656 _dbus_verbose ("Header was invalid\n");
2657 loader->corrupted = TRUE;
2661 next_arg = header_len;
2662 while (next_arg < (header_len + body_len))
2664 int prev = next_arg;
2666 if (!_dbus_marshal_validate_arg (&loader->data,
2671 loader->corrupted = TRUE;
2675 _dbus_assert (next_arg > prev);
2678 if (next_arg > (header_len + body_len))
2680 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2681 next_arg, header_len, body_len,
2682 header_len + body_len);
2683 loader->corrupted = TRUE;
2687 message = dbus_message_new_empty_header ();
2688 if (message == NULL)
2689 break; /* ugh, postpone this I guess. */
2691 message->byte_order = byte_order;
2692 message->header_padding = header_padding;
2694 /* Copy in the offsets we found */
2696 while (i < FIELD_LAST)
2698 message->header_fields[i] = fields[i];
2702 if (!_dbus_list_append (&loader->messages, message))
2704 dbus_message_unref (message);
2708 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2709 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2711 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2712 (header_len + body_len));
2714 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2716 _dbus_list_remove_last (&loader->messages, message);
2717 dbus_message_unref (message);
2721 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2725 /* put the header back, we'll try again later */
2726 result = _dbus_string_copy_len (&message->header, 0, header_len,
2728 _dbus_assert (result); /* because DBusString never reallocs smaller */
2730 _dbus_list_remove_last (&loader->messages, message);
2731 dbus_message_unref (message);
2735 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2736 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2738 /* Fill in caches */
2739 message->reply_serial = get_int_field (message,
2740 FIELD_REPLY_SERIAL);
2741 message->client_serial = get_int_field (message,
2742 FIELD_CLIENT_SERIAL);
2744 _dbus_verbose ("Loaded message %p\n", message);
2752 * Pops a loaded message (passing ownership of the message
2753 * to the caller). Returns #NULL if no messages have been
2756 * @param loader the loader.
2757 * @returns the next message, or #NULL if none.
2760 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2762 return _dbus_list_pop_first (&loader->messages);
2767 * Checks whether the loader is confused due to bad data.
2768 * If messages are received that are invalid, the
2769 * loader gets confused and gives up permanently.
2770 * This state is called "corrupted."
2772 * @param loader the loader
2773 * @returns #TRUE if the loader is hosed.
2776 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2778 return loader->corrupted;
2782 * Sets the maximum size message we allow.
2784 * @param loader the loader
2785 * @param size the max message size in bytes
2788 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2791 if (size > MAX_SANE_MESSAGE_SIZE)
2793 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2794 size, MAX_SANE_MESSAGE_SIZE);
2795 size = MAX_SANE_MESSAGE_SIZE;
2797 loader->max_message_size = size;
2801 * Gets the maximum allowed message size in bytes.
2803 * @param loader the loader
2804 * @returns max size in bytes
2807 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2809 return loader->max_message_size;
2813 #ifdef DBUS_BUILD_TESTS
2814 #include "dbus-test.h"
2818 message_iter_test (DBusMessage *message)
2820 DBusMessageIter *iter;
2823 iter = dbus_message_get_args_iter (message);
2826 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2827 _dbus_assert_not_reached ("Argument type isn't string");
2829 str = dbus_message_iter_get_string (iter);
2830 if (strcmp (str, "Test string") != 0)
2831 _dbus_assert_not_reached ("Strings differ");
2834 if (!dbus_message_iter_next (iter))
2835 _dbus_assert_not_reached ("Reached end of arguments");
2837 /* Signed integer tests */
2838 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2839 _dbus_assert_not_reached ("Argument type isn't int32");
2841 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2842 _dbus_assert_not_reached ("Signed integers differ");
2844 if (!dbus_message_iter_next (iter))
2845 _dbus_assert_not_reached ("Reached end of fields");
2847 /* Unsigned integer tests */
2848 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2849 _dbus_assert_not_reached ("Argument type isn't int32");
2851 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
2852 _dbus_assert_not_reached ("Unsigned integers differ");
2854 if (!dbus_message_iter_next (iter))
2855 _dbus_assert_not_reached ("Reached end of arguments");
2858 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
2859 _dbus_assert_not_reached ("Argument type isn't double");
2861 if (dbus_message_iter_get_double (iter) != 3.14159)
2862 _dbus_assert_not_reached ("Doubles differ");
2864 if (dbus_message_iter_next (iter))
2865 _dbus_assert_not_reached ("Didn't reach end of arguments");
2867 dbus_message_iter_unref (iter);
2871 check_message_handling (DBusMessage *message)
2873 DBusMessageIter *iter;
2876 dbus_int32_t client_serial;
2881 client_serial = _dbus_message_get_client_serial (message);
2883 /* can't use set_client_serial due to the assertions at the start of it */
2884 set_int_field (message, FIELD_CLIENT_SERIAL,
2887 if (client_serial != _dbus_message_get_client_serial (message))
2889 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
2893 /* If we implement message_set_arg (message, n, value)
2894 * then we would want to test it here
2897 iter = dbus_message_get_args_iter (message);
2898 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
2904 case DBUS_TYPE_INT32:
2905 dbus_message_iter_get_int32 (iter);
2907 case DBUS_TYPE_UINT32:
2908 dbus_message_iter_get_uint32 (iter);
2910 case DBUS_TYPE_DOUBLE:
2911 dbus_message_iter_get_double (iter);
2913 case DBUS_TYPE_STRING:
2916 str = dbus_message_iter_get_string (iter);
2920 case DBUS_TYPE_INT32_ARRAY:
2922 dbus_int32_t *values;
2925 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
2931 case DBUS_TYPE_UINT32_ARRAY:
2933 dbus_uint32_t *values;
2936 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
2942 case DBUS_TYPE_DOUBLE_ARRAY:
2947 if (!dbus_message_iter_get_double_array (iter, &values, &len))
2953 case DBUS_TYPE_STRING_ARRAY:
2958 if (!dbus_message_iter_get_string_array (iter, &values, &len))
2961 dbus_free_string_array (values);
2969 if (!dbus_message_iter_next (iter))
2977 dbus_message_iter_unref (iter);
2983 check_have_valid_message (DBusMessageLoader *loader)
2985 DBusMessage *message;
2991 if (_dbus_message_loader_get_is_corrupted (loader))
2993 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
2997 message = _dbus_message_loader_pop_message (loader);
2998 if (message == NULL)
3000 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3004 if (_dbus_string_get_length (&loader->data) > 0)
3006 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3010 /* Verify that we're able to properly deal with the message.
3011 * For example, this would detect improper handling of messages
3012 * in nonstandard byte order.
3014 if (!check_message_handling (message))
3021 dbus_message_unref (message);
3026 check_invalid_message (DBusMessageLoader *loader)
3032 if (!_dbus_message_loader_get_is_corrupted (loader))
3034 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3045 check_incomplete_message (DBusMessageLoader *loader)
3047 DBusMessage *message;
3053 if (_dbus_message_loader_get_is_corrupted (loader))
3055 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3059 message = _dbus_message_loader_pop_message (loader);
3060 if (message != NULL)
3062 _dbus_warn ("loaded message that was expected to be incomplete\n");
3070 dbus_message_unref (message);
3075 check_loader_results (DBusMessageLoader *loader,
3076 DBusMessageValidity validity)
3080 case _DBUS_MESSAGE_VALID:
3081 return check_have_valid_message (loader);
3082 case _DBUS_MESSAGE_INVALID:
3083 return check_invalid_message (loader);
3084 case _DBUS_MESSAGE_INCOMPLETE:
3085 return check_incomplete_message (loader);
3086 case _DBUS_MESSAGE_UNKNOWN:
3090 _dbus_assert_not_reached ("bad DBusMessageValidity");
3096 * Loads the message in the given message file.
3098 * @param filename filename to load
3099 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3100 * @param data string to load message into
3101 * @returns #TRUE if the message was loaded
3104 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3114 DBusResultCode result;
3116 result = _dbus_file_get_contents (data, filename);
3117 if (result != DBUS_RESULT_SUCCESS)
3120 _dbus_string_get_const_data (filename, &s);
3121 _dbus_warn ("Could not load message file %s\n", s);
3127 if (!_dbus_message_data_load (data, filename))
3130 _dbus_string_get_const_data (filename, &s);
3131 _dbus_warn ("Could not load message file %s\n", s);
3144 * Tries loading the message in the given message file
3145 * and verifies that DBusMessageLoader can handle it.
3147 * @param filename filename to load
3148 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3149 * @param expected_validity what the message has to be like to return #TRUE
3150 * @returns #TRUE if the message has the expected validity
3153 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3155 DBusMessageValidity expected_validity)
3162 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3163 _dbus_assert_not_reached ("could not allocate string\n");
3165 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3169 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3177 if (_dbus_string_get_length (&data) > 0)
3178 _dbus_verbose_bytes_of_string (&data, 0,
3179 _dbus_string_get_length (&data));
3181 _dbus_string_get_const_data (filename, &s);
3182 _dbus_warn ("Failed message loader test on %s\n",
3186 _dbus_string_free (&data);
3192 * Tries loading the given message data.
3195 * @param data the message data
3196 * @param expected_validity what the message has to be like to return #TRUE
3197 * @returns #TRUE if the message has the expected validity
3200 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3201 DBusMessageValidity expected_validity)
3203 DBusMessageLoader *loader;
3211 /* Write the data one byte at a time */
3213 loader = _dbus_message_loader_new ();
3215 len = _dbus_string_get_length (data);
3216 for (i = 0; i < len; i++)
3220 _dbus_message_loader_get_buffer (loader, &buffer);
3221 _dbus_string_append_byte (buffer,
3222 _dbus_string_get_byte (data, i));
3223 _dbus_message_loader_return_buffer (loader, buffer, 1);
3226 if (!check_loader_results (loader, expected_validity))
3229 _dbus_message_loader_unref (loader);
3232 /* Write the data all at once */
3234 loader = _dbus_message_loader_new ();
3239 _dbus_message_loader_get_buffer (loader, &buffer);
3240 _dbus_string_copy (data, 0, buffer,
3241 _dbus_string_get_length (buffer));
3242 _dbus_message_loader_return_buffer (loader, buffer, 1);
3245 if (!check_loader_results (loader, expected_validity))
3248 _dbus_message_loader_unref (loader);
3251 /* Write the data 2 bytes at a time */
3253 loader = _dbus_message_loader_new ();
3255 len = _dbus_string_get_length (data);
3256 for (i = 0; i < len; i += 2)
3260 _dbus_message_loader_get_buffer (loader, &buffer);
3261 _dbus_string_append_byte (buffer,
3262 _dbus_string_get_byte (data, i));
3264 _dbus_string_append_byte (buffer,
3265 _dbus_string_get_byte (data, i+1));
3266 _dbus_message_loader_return_buffer (loader, buffer, 1);
3269 if (!check_loader_results (loader, expected_validity))
3272 _dbus_message_loader_unref (loader);
3280 _dbus_message_loader_unref (loader);
3286 process_test_subdir (const DBusString *test_base_dir,
3288 DBusMessageValidity validity,
3289 DBusForeachMessageFileFunc function,
3292 DBusString test_directory;
3293 DBusString filename;
3296 DBusResultCode result;
3301 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3302 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3304 _dbus_string_init_const (&filename, subdir);
3306 if (!_dbus_string_copy (test_base_dir, 0,
3307 &test_directory, 0))
3308 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3310 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3311 _dbus_assert_not_reached ("couldn't allocate full path");
3313 _dbus_string_free (&filename);
3314 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3315 _dbus_assert_not_reached ("didn't allocate filename string\n");
3317 dir = _dbus_directory_open (&test_directory, &result);
3321 _dbus_string_get_const_data (&test_directory, &s);
3322 _dbus_warn ("Could not open %s: %s\n", s,
3323 dbus_result_to_string (result));
3327 printf ("Testing:\n");
3329 result = DBUS_RESULT_SUCCESS;
3331 while (_dbus_directory_get_next_file (dir, &filename, &result))
3333 DBusString full_path;
3336 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3337 _dbus_assert_not_reached ("couldn't init string");
3339 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3340 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3342 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3343 _dbus_assert_not_reached ("couldn't concat file to dir");
3345 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3347 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3351 const char *filename_c;
3352 _dbus_string_get_const_data (&filename, &filename_c);
3353 _dbus_verbose ("Skipping non-.message file %s\n",
3355 _dbus_string_free (&full_path);
3361 _dbus_string_get_const_data (&filename, &s);
3362 printf (" %s\n", s);
3365 _dbus_verbose (" expecting %s\n",
3366 validity == _DBUS_MESSAGE_VALID ? "valid" :
3367 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3368 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3370 if (! (*function) (&full_path, is_raw, validity, user_data))
3372 _dbus_string_free (&full_path);
3376 _dbus_string_free (&full_path);
3379 if (result != DBUS_RESULT_SUCCESS)
3382 _dbus_string_get_const_data (&test_directory, &s);
3383 _dbus_warn ("Could not get next file in %s: %s\n",
3384 s, dbus_result_to_string (result));
3393 _dbus_directory_close (dir);
3394 _dbus_string_free (&test_directory);
3395 _dbus_string_free (&filename);
3401 * Runs the given function on every message file in the test suite.
3402 * The function should return #FALSE on test failure or fatal error.
3404 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3405 * @param func the function to run
3406 * @param user_data data for function
3407 * @returns #FALSE if there's a failure
3410 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3411 DBusForeachMessageFileFunc func,
3414 DBusString test_directory;
3419 _dbus_string_init_const (&test_directory, test_data_dir);
3421 if (!process_test_subdir (&test_directory, "valid-messages",
3422 _DBUS_MESSAGE_VALID, func, user_data))
3425 if (!process_test_subdir (&test_directory, "invalid-messages",
3426 _DBUS_MESSAGE_INVALID, func, user_data))
3429 if (!process_test_subdir (&test_directory, "incomplete-messages",
3430 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3437 _dbus_string_free (&test_directory);
3443 * @ingroup DBusMessageInternals
3444 * Unit test for DBusMessage.
3446 * @returns #TRUE on success.
3449 _dbus_message_test (const char *test_data_dir)
3451 DBusMessage *message;
3452 DBusMessageLoader *loader;
3455 dbus_int32_t our_int;
3458 dbus_bool_t our_bool;
3460 /* Test the vararg functions */
3461 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3462 _dbus_message_set_client_serial (message, 1);
3463 dbus_message_append_args (message,
3464 DBUS_TYPE_INT32, -0x12345678,
3465 DBUS_TYPE_STRING, "Test string",
3466 DBUS_TYPE_DOUBLE, 3.14159,
3467 DBUS_TYPE_BOOLEAN, TRUE,
3469 _dbus_verbose_bytes_of_string (&message->header, 0,
3470 _dbus_string_get_length (&message->header));
3471 _dbus_verbose_bytes_of_string (&message->body, 0,
3472 _dbus_string_get_length (&message->body));
3474 if (dbus_message_get_args (message,
3475 DBUS_TYPE_INT32, &our_int,
3476 DBUS_TYPE_STRING, &our_str,
3477 DBUS_TYPE_DOUBLE, &our_double,
3478 DBUS_TYPE_BOOLEAN, &our_bool,
3479 0) != DBUS_RESULT_SUCCESS)
3480 _dbus_assert_not_reached ("Could not get arguments");
3482 if (our_int != -0x12345678)
3483 _dbus_assert_not_reached ("integers differ!");
3485 if (our_double != 3.14159)
3486 _dbus_assert_not_reached ("doubles differ!");
3488 if (strcmp (our_str, "Test string") != 0)
3489 _dbus_assert_not_reached ("strings differ!");
3492 _dbus_assert_not_reached ("booleans differ");
3494 dbus_free (our_str);
3495 dbus_message_unref (message);
3497 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3498 _dbus_message_set_client_serial (message, 1);
3499 _dbus_message_set_reply_serial (message, 0x12345678);
3501 dbus_message_append_string (message, "Test string");
3502 dbus_message_append_int32 (message, -0x12345678);
3503 dbus_message_append_uint32 (message, 0xedd1e);
3504 dbus_message_append_double (message, 3.14159);
3506 message_iter_test (message);
3508 /* Message loader test */
3509 _dbus_message_lock (message);
3510 loader = _dbus_message_loader_new ();
3512 /* Write the header data one byte at a time */
3513 _dbus_string_get_const_data (&message->header, &data);
3514 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3518 _dbus_message_loader_get_buffer (loader, &buffer);
3519 _dbus_string_append_byte (buffer, data[i]);
3520 _dbus_message_loader_return_buffer (loader, buffer, 1);
3523 /* Write the body data one byte at a time */
3524 _dbus_string_get_const_data (&message->body, &data);
3525 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3529 _dbus_message_loader_get_buffer (loader, &buffer);
3530 _dbus_string_append_byte (buffer, data[i]);
3531 _dbus_message_loader_return_buffer (loader, buffer, 1);
3534 dbus_message_unref (message);
3536 /* Now pop back the message */
3537 if (_dbus_message_loader_get_is_corrupted (loader))
3538 _dbus_assert_not_reached ("message loader corrupted");
3540 message = _dbus_message_loader_pop_message (loader);
3542 _dbus_assert_not_reached ("received a NULL message");
3544 if (_dbus_message_get_reply_serial (message) != 0x12345678)
3545 _dbus_assert_not_reached ("reply serial fields differ");
3547 message_iter_test (message);
3549 dbus_message_unref (message);
3550 _dbus_message_loader_unref (loader);
3552 /* Now load every message in test_data_dir if we have one */
3553 if (test_data_dir == NULL)
3556 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3557 (DBusForeachMessageFileFunc)
3558 dbus_internal_do_not_use_try_message_file,
3562 #endif /* DBUS_BUILD_TESTS */