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 int 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
750 * can't be allocated for the message.
752 * @todo use DBusString internally to store service and name.
754 * @param service service that the message should be sent to
755 * @param name name of the message
756 * @returns a new DBusMessage, free with dbus_message_unref()
757 * @see dbus_message_unref()
760 dbus_message_new (const char *service,
763 DBusMessage *message;
765 message = dbus_message_new_empty_header ();
769 if (!dbus_message_create_header (message, service, name))
771 dbus_message_unref (message);
779 * Constructs a message that is a reply to some other
780 * message. Returns #NULL if memory can't be allocated
783 * @param name the name of the message
784 * @param original_message the message which the created
785 * message is a reply to.
786 * @returns a new DBusMessage, free with dbus_message_unref()
787 * @see dbus_message_new(), dbus_message_unref()
790 dbus_message_new_reply (const char *name,
791 DBusMessage *original_message)
793 DBusMessage *message;
796 sender = get_string_field (original_message,
799 _dbus_assert (sender != NULL);
801 message = dbus_message_new (sender, name);
806 if (!_dbus_message_set_reply_serial (message,
807 _dbus_message_get_client_serial (original_message)))
809 dbus_message_unref (message);
817 * Creates a new message that is an exact replica of the message
818 * specified, except that its refcount is set to 1.
820 * @param message the message.
821 * @returns the new message.
824 dbus_message_new_from_message (const DBusMessage *message)
829 retval = dbus_new0 (DBusMessage, 1);
833 retval->refcount = 1;
834 retval->byte_order = message->byte_order;
836 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
842 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
844 _dbus_string_free (&retval->header);
849 if (!_dbus_string_copy (&message->header, 0,
852 _dbus_string_free (&retval->header);
853 _dbus_string_free (&retval->body);
859 if (!_dbus_string_copy (&message->body, 0,
862 _dbus_string_free (&retval->header);
863 _dbus_string_free (&retval->body);
869 for (i = 0; i < FIELD_LAST; i++)
871 retval->header_fields[i].offset = message->header_fields[i].offset;
879 * Increments the reference count of a DBusMessage.
881 * @param message The message
882 * @see dbus_message_unref
885 dbus_message_ref (DBusMessage *message)
887 _dbus_assert (message->refcount > 0);
889 message->refcount += 1;
893 * Decrements the reference count of a DBusMessage.
895 * @param message The message
896 * @see dbus_message_ref
899 dbus_message_unref (DBusMessage *message)
901 _dbus_assert (message->refcount > 0);
903 message->refcount -= 1;
904 if (message->refcount == 0)
906 if (message->size_counter != NULL)
908 _dbus_counter_adjust (message->size_counter,
909 - message->size_counter_delta);
910 _dbus_counter_unref (message->size_counter);
913 _dbus_string_free (&message->header);
914 _dbus_string_free (&message->body);
921 * Gets the name of a message.
923 * @param message the message
924 * @returns the message name (should not be freed)
927 dbus_message_get_name (DBusMessage *message)
929 return get_string_field (message, FIELD_NAME, NULL);
933 * Gets the destination service of a message.
935 * @param message the message
936 * @returns the message destination service (should not be freed)
939 dbus_message_get_service (DBusMessage *message)
941 return get_string_field (message, FIELD_SERVICE, NULL);
945 * Appends fields to a message given a variable argument
946 * list. The variable argument list should contain the type
947 * of the argument followed by the value to add. Array values
948 * are specified by an int giving the length of the array
949 * followed by a pointer to the array. The list is terminated
952 * @param message the message
953 * @param first_field_type type of the first field
954 * @param ... value of first field, list of additional type-value pairs
955 * @returns #TRUE on success
958 dbus_message_append_fields (DBusMessage *message,
959 int first_field_type,
965 va_start (var_args, first_field_type);
966 retval = dbus_message_append_fields_valist (message,
975 * This function takes a va_list for use by language bindings
977 * @see dbus_message_append_fields.
978 * @param message the message
979 * @param first_field_type type of first field
980 * @param var_args value of first field, then list of type/value pairs
981 * @returns #TRUE on success
984 dbus_message_append_fields_valist (DBusMessage *message,
985 int first_field_type,
990 old_len = _dbus_string_get_length (&message->body);
992 type = first_field_type;
998 case DBUS_TYPE_INT32:
999 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1002 case DBUS_TYPE_UINT32:
1003 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1006 case DBUS_TYPE_DOUBLE:
1007 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1010 case DBUS_TYPE_STRING:
1011 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1014 case DBUS_TYPE_BYTE_ARRAY:
1017 unsigned char *data;
1019 data = va_arg (var_args, unsigned char *);
1020 len = va_arg (var_args, int);
1022 if (!dbus_message_append_byte_array (message, data, len))
1026 case DBUS_TYPE_STRING_ARRAY:
1031 data = va_arg (var_args, const char **);
1032 len = va_arg (var_args, int);
1034 if (!dbus_message_append_string_array (message, data, len))
1040 _dbus_warn ("Unknown field type %d\n", type);
1043 type = va_arg (var_args, int);
1049 _dbus_string_set_length (&message->body, old_len);
1054 * Appends a 32 bit signed integer to the message.
1056 * @param message the message
1057 * @param value the integer value
1058 * @returns #TRUE on success
1061 dbus_message_append_int32 (DBusMessage *message,
1064 _dbus_assert (!message->locked);
1066 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1068 _dbus_string_shorten (&message->body, 1);
1072 return _dbus_marshal_int32 (&message->body,
1073 message->byte_order, value);
1077 * Appends a 32 bit unsigned integer to the message.
1079 * @param message the message
1080 * @param value the integer value
1081 * @returns #TRUE on success
1084 dbus_message_append_uint32 (DBusMessage *message,
1085 dbus_uint32_t value)
1087 _dbus_assert (!message->locked);
1089 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1091 _dbus_string_shorten (&message->body, 1);
1095 return _dbus_marshal_uint32 (&message->body,
1096 message->byte_order, value);
1100 * Appends a double value to the message.
1102 * @param message the message
1103 * @param value the double value
1104 * @returns #TRUE on success
1107 dbus_message_append_double (DBusMessage *message,
1110 _dbus_assert (!message->locked);
1112 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1114 _dbus_string_shorten (&message->body, 1);
1118 return _dbus_marshal_double (&message->body,
1119 message->byte_order, value);
1123 * Appends a UTF-8 string to the message.
1125 * @param message the message
1126 * @param value the string
1127 * @returns #TRUE on success
1130 dbus_message_append_string (DBusMessage *message,
1133 _dbus_assert (!message->locked);
1135 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1137 _dbus_string_shorten (&message->body, 1);
1141 return _dbus_marshal_string (&message->body,
1142 message->byte_order, value);
1146 * Appends a byte array to the message.
1148 * @param message the message
1149 * @param value the array
1150 * @param len the length of the array
1151 * @returns #TRUE on success
1154 dbus_message_append_byte_array (DBusMessage *message,
1155 unsigned const char *value,
1158 _dbus_assert (!message->locked);
1160 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1162 _dbus_string_shorten (&message->body, 1);
1166 return _dbus_marshal_byte_array (&message->body,
1167 message->byte_order, value, len);
1171 * Appends a string array to the message.
1173 * @param message the message
1174 * @param value the array
1175 * @param len the length of the array
1176 * @returns #TRUE on success
1179 dbus_message_append_string_array (DBusMessage *message,
1183 _dbus_assert (!message->locked);
1185 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1187 _dbus_string_shorten (&message->body, 1);
1191 return _dbus_marshal_string_array (&message->body,
1192 message->byte_order, value, len);
1196 * Gets fields from a message given a variable argument list.
1197 * The variable argument list should contain the type of the
1198 * field followed by a pointer to where the value should be
1199 * stored. The list is terminated with 0.
1201 * @todo rename get_args to avoid confusion with header fields
1203 * @param message the message
1204 * @param first_field_type the first field type
1205 * @param ... location for first field value, then list of type-location pairs
1206 * @returns result code
1209 dbus_message_get_fields (DBusMessage *message,
1210 int first_field_type,
1213 DBusResultCode retval;
1216 va_start (var_args, first_field_type);
1217 retval = dbus_message_get_fields_valist (message, first_field_type, var_args);
1224 * This function takes a va_list for use by language bindings
1226 * @todo this function (or some lower-level non-convenience function)
1227 * needs better error handling; should allow the application to
1228 * distinguish between out of memory, and bad data from the remote
1229 * app. It also needs to not leak a bunch of args when it gets
1230 * to the arg that's bad, as that would be a security hole
1231 * (allow one app to force another to leak memory)
1233 * @todo We need to free the field data when an error occurs.
1235 * @todo rename get_args_valist to avoid confusion with header fields
1237 * @see dbus_message_get_fields
1238 * @param message the message
1239 * @param first_field_type type of the first field
1240 * @param var_args return location for first field, followed by list of type/location pairs
1241 * @returns result code
1244 dbus_message_get_fields_valist (DBusMessage *message,
1245 int first_field_type,
1248 int spec_type, msg_type, i;
1249 DBusMessageIter *iter;
1251 iter = dbus_message_get_fields_iter (message);
1254 return DBUS_RESULT_NO_MEMORY;
1256 spec_type = first_field_type;
1259 while (spec_type != 0)
1261 msg_type = dbus_message_iter_get_field_type (iter);
1263 if (msg_type != spec_type)
1265 _dbus_verbose ("Field %d is specified to be of type \"%s\", but "
1266 "is actually of type \"%s\"\n", i,
1267 _dbus_type_to_string (spec_type),
1268 _dbus_type_to_string (msg_type));
1269 dbus_message_iter_unref (iter);
1271 return DBUS_RESULT_INVALID_FIELDS;
1276 case DBUS_TYPE_INT32:
1280 ptr = va_arg (var_args, dbus_int32_t *);
1282 *ptr = dbus_message_iter_get_int32 (iter);
1285 case DBUS_TYPE_UINT32:
1289 ptr = va_arg (var_args, dbus_uint32_t *);
1291 *ptr = dbus_message_iter_get_uint32 (iter);
1295 case DBUS_TYPE_DOUBLE:
1299 ptr = va_arg (var_args, double *);
1301 *ptr = dbus_message_iter_get_double (iter);
1305 case DBUS_TYPE_STRING:
1309 ptr = va_arg (var_args, char **);
1311 *ptr = dbus_message_iter_get_string (iter);
1314 return DBUS_RESULT_NO_MEMORY;
1319 case DBUS_TYPE_BYTE_ARRAY:
1321 unsigned char **ptr;
1324 ptr = va_arg (var_args, unsigned char **);
1325 len = va_arg (var_args, int *);
1327 *ptr = dbus_message_iter_get_byte_array (iter, len);
1330 return DBUS_RESULT_NO_MEMORY;
1334 case DBUS_TYPE_STRING_ARRAY:
1339 ptr = va_arg (var_args, char ***);
1340 len = va_arg (var_args, int *);
1342 *ptr = dbus_message_iter_get_string_array (iter, len);
1345 return DBUS_RESULT_NO_MEMORY;
1350 _dbus_warn ("Unknown field type %d\n", spec_type);
1353 spec_type = va_arg (var_args, int);
1354 if (spec_type != 0 && !dbus_message_iter_next (iter))
1356 _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
1358 dbus_message_iter_unref (iter);
1359 return DBUS_RESULT_INVALID_FIELDS;
1364 dbus_message_iter_unref (iter);
1365 return DBUS_RESULT_SUCCESS;
1369 * Returns a DBusMessageIter representing the fields of the
1370 * message passed in.
1372 * @todo IMO the message iter should follow the GtkTextIter pattern,
1373 * a static object with a "stamp" value used to detect invalid
1374 * iter uses (uninitialized or after changing the message).
1375 * ref/unref is kind of annoying to deal with, and slower too.
1376 * This implies not ref'ing the message from the iter.
1378 * @todo rename get_args_iter to avoid confusion with header fields
1380 * @param message the message
1381 * @returns a new iter.
1384 dbus_message_get_fields_iter (DBusMessage *message)
1386 DBusMessageIter *iter;
1388 iter = dbus_new (DBusMessageIter, 1);
1390 dbus_message_ref (message);
1393 iter->message = message;
1400 * Increments the reference count of a DBusMessageIter.
1402 * @param iter the message iter
1403 * @see dbus_message_iter_unref
1406 dbus_message_iter_ref (DBusMessageIter *iter)
1408 _dbus_assert (iter->refcount > 0);
1410 iter->refcount += 1;
1414 * Decrements the reference count of a DBusMessageIter.
1416 * @param iter The message iter
1417 * @see dbus_message_iter_ref
1420 dbus_message_iter_unref (DBusMessageIter *iter)
1422 _dbus_assert (iter->refcount > 0);
1424 iter->refcount -= 1;
1426 if (iter->refcount == 0)
1428 dbus_message_unref (iter->message);
1435 * Checks if an iterator has any more fields.
1437 * @param iter the message iter
1438 * @returns #TRUE if there are more fields
1442 dbus_message_iter_has_next (DBusMessageIter *iter)
1446 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1447 iter->message->byte_order,
1448 iter->pos, &end_pos))
1451 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1458 * Moves the iterator to the next field.
1460 * @param iter The message iter
1461 * @returns #TRUE if the iterator was moved to the next field
1464 dbus_message_iter_next (DBusMessageIter *iter)
1468 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1469 iter->message->byte_order,
1470 iter->pos, &end_pos))
1473 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1476 iter->pos = end_pos;
1482 * Returns the field type of the field that the
1483 * message iterator points at.
1485 * @param iter the message iter
1486 * @returns the field type
1489 dbus_message_iter_get_field_type (DBusMessageIter *iter)
1493 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1494 return DBUS_TYPE_INVALID;
1496 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1498 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1501 return DBUS_TYPE_INVALID;
1505 * Returns the string value that an iterator may point to.
1506 * Note that you need to check that the iterator points to
1507 * a string value before using this function.
1509 * @see dbus_message_iter_get_field_type
1510 * @param iter the message iter
1511 * @returns the string
1514 dbus_message_iter_get_string (DBusMessageIter *iter)
1516 _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_STRING);
1518 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1519 iter->pos + 1, NULL);
1523 * Returns the 32 bit signed integer value that an iterator may point to.
1524 * Note that you need to check that the iterator points to
1525 * a string value before using this function.
1527 * @see dbus_message_iter_get_field_type
1528 * @param iter the message iter
1529 * @returns the integer
1532 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1534 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1535 iter->pos + 1, NULL);
1539 * Returns the 32 bit unsigned integer value that an iterator may point to.
1540 * Note that you need to check that the iterator points to
1541 * a string value before using this function.
1543 * @see dbus_message_iter_get_field_type
1544 * @param iter the message iter
1545 * @returns the integer
1548 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1550 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1551 iter->pos + 1, NULL);
1555 * Returns the double value that an iterator may point to.
1556 * Note that you need to check that the iterator points to
1557 * a string value before using this function.
1559 * @see dbus_message_iter_get_field_type
1560 * @param iter the message iter
1561 * @returns the double
1564 dbus_message_iter_get_double (DBusMessageIter *iter)
1566 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1567 iter->pos + 1, NULL);
1571 * Returns the byte array that the iterator may point to.
1572 * Note that you need to check that the iterator points
1573 * to a byte array prior to using this function.
1575 * @todo this function should probably take "unsigned char **" as
1576 * an out param argument, and return boolean or result code.
1578 * @param iter the iterator
1579 * @param len return location for length of byte array
1580 * @returns the byte array
1583 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
1586 _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_BYTE_ARRAY);
1588 return _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1589 iter->pos + 1, NULL, len);
1593 * Returns the string array that the iterator may point to.
1594 * Note that you need to check that the iterator points
1595 * to a byte array prior to using this function.
1597 * @todo this function should probably take "char **" as
1598 * an out param argument, and return boolean or result code.
1600 * @param iter the iterator
1601 * @param len return location for length of byte array
1602 * @returns the byte array
1605 dbus_message_iter_get_string_array (DBusMessageIter *iter,
1608 _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_STRING_ARRAY);
1610 return _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
1611 iter->pos + 1, NULL, len);
1615 * Sets the message sender.
1617 * @param message the message
1618 * @param sender the sender
1619 * @returns #FALSE if not enough memory
1622 dbus_message_set_sender (DBusMessage *message,
1625 _dbus_assert (!message->locked);
1629 delete_string_field (message, FIELD_SENDER);
1634 return set_string_field (message,
1641 * Sets a flag indicating that the message is an error reply
1642 * message, i.e. an "exception" rather than a normal response.
1644 * @param message the message
1645 * @param is_error_reply #TRUE if this is an error message.
1648 dbus_message_set_is_error (DBusMessage *message,
1649 dbus_bool_t is_error_reply)
1653 _dbus_assert (!message->locked);
1655 _dbus_string_get_data_len (&message->header, &header, 1, 1);
1658 *header |= DBUS_HEADER_FLAG_ERROR;
1660 *header &= ~DBUS_HEADER_FLAG_ERROR;
1664 * Returns #TRUE if the message is an error
1665 * reply to some previous message we sent.
1667 * @param message the message
1668 * @returns #TRUE if the message is an error
1671 dbus_message_get_is_error (DBusMessage *message)
1675 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
1677 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
1681 * Gets the service which originated this message,
1682 * or #NULL if unknown or inapplicable.
1684 * @param message the message
1685 * @returns the service name or #NULL
1688 dbus_message_get_sender (DBusMessage *message)
1690 return get_string_field (message, FIELD_SENDER, NULL);
1696 * @addtogroup DBusMessageInternals
1701 * @typedef DBusMessageLoader
1703 * The DBusMessageLoader object encapsulates the process of converting
1704 * a byte stream into a series of DBusMessage. It buffers the incoming
1705 * bytes as efficiently as possible, and generates a queue of
1706 * messages. DBusMessageLoader is typically used as part of a
1707 * DBusTransport implementation. The DBusTransport then hands off
1708 * the loaded messages to a DBusConnection, making the messages
1709 * visible to the application.
1713 /* we definitely use signed ints for sizes, so don't exceed
1714 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
1715 * over 128M is pretty nuts anyhow.
1719 * The maximum sane message size.
1721 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
1724 * Implementation details of DBusMessageLoader.
1725 * All members are private.
1727 struct DBusMessageLoader
1729 int refcount; /**< Reference count. */
1731 DBusString data; /**< Buffered data */
1733 DBusList *messages; /**< Complete messages. */
1735 long max_message_size; /**< Maximum size of a message */
1737 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
1739 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
1743 * The initial buffer size of the message loader.
1745 * @todo this should be based on min header size plus some average
1746 * body size, or something. Or rather, the min header size only, if we
1747 * want to try to read only the header, store that in a DBusMessage,
1748 * then read only the body and store that, etc., depends on
1749 * how we optimize _dbus_message_loader_get_buffer() and what
1750 * the exact message format is.
1752 #define INITIAL_LOADER_DATA_LEN 32
1755 * Creates a new message loader. Returns #NULL if memory can't
1758 * @returns new loader, or #NULL.
1761 _dbus_message_loader_new (void)
1763 DBusMessageLoader *loader;
1765 loader = dbus_new0 (DBusMessageLoader, 1);
1769 loader->refcount = 1;
1771 /* Try to cap message size at something that won't *totally* hose
1772 * the system if we have a couple of them.
1774 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
1776 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
1782 /* preallocate the buffer for speed, ignore failure */
1783 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
1784 _dbus_string_set_length (&loader->data, 0);
1790 * Increments the reference count of the loader.
1792 * @param loader the loader.
1795 _dbus_message_loader_ref (DBusMessageLoader *loader)
1797 loader->refcount += 1;
1801 * Decrements the reference count of the loader and finalizes the
1802 * loader when the count reaches zero.
1804 * @param loader the loader.
1807 _dbus_message_loader_unref (DBusMessageLoader *loader)
1809 loader->refcount -= 1;
1810 if (loader->refcount == 0)
1812 _dbus_list_foreach (&loader->messages,
1813 (DBusForeachFunction) dbus_message_unref,
1815 _dbus_list_clear (&loader->messages);
1816 _dbus_string_free (&loader->data);
1822 * Gets the buffer to use for reading data from the network. Network
1823 * data is read directly into an allocated buffer, which is then used
1824 * in the DBusMessage, to avoid as many extra memcpy's as possible.
1825 * The buffer must always be returned immediately using
1826 * _dbus_message_loader_return_buffer(), even if no bytes are
1827 * successfully read.
1829 * @todo this function can be a lot more clever. For example
1830 * it can probably always return a buffer size to read exactly
1831 * the body of the next message, thus avoiding any memory wastage
1834 * @param loader the message loader.
1835 * @param buffer the buffer
1838 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
1839 DBusString **buffer)
1841 _dbus_assert (!loader->buffer_outstanding);
1843 *buffer = &loader->data;
1845 loader->buffer_outstanding = TRUE;
1849 * The smallest header size that can occur.
1850 * (It won't be valid)
1852 #define DBUS_MINIMUM_HEADER_SIZE 16
1854 /** Pack four characters as in "abcd" into a uint32 */
1855 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
1856 ((((dbus_uint32_t)a) << 24) | \
1857 (((dbus_uint32_t)b) << 16) | \
1858 (((dbus_uint32_t)c) << 8) | \
1861 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
1862 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
1863 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
1865 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
1866 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
1867 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
1869 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
1870 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
1871 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
1873 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
1874 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
1875 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
1878 decode_header_data (const DBusString *data,
1881 HeaderField fields[FIELD_LAST])
1887 if (header_len < 16)
1891 while (i < FIELD_LAST)
1893 fields[i].offset = -1;
1897 fields[FIELD_HEADER_LENGTH].offset = 4;
1898 fields[FIELD_BODY_LENGTH].offset = 8;
1899 fields[FIELD_CLIENT_SERIAL].offset = 12;
1901 /* Now handle the named fields. A real named field is at least 4
1902 * bytes for the name, plus a type code (1 byte) plus padding. So
1903 * if we have less than 8 bytes left, it must be alignment padding,
1904 * not a field. While >= 8 bytes can't be entirely alignment
1908 while ((pos + 7) < header_len)
1910 pos = _DBUS_ALIGN_VALUE (pos, 4);
1912 if ((pos + 4) > header_len)
1915 _dbus_string_get_const_data_len (data, &field, pos, 4);
1918 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
1920 /* I believe FROM_BE is right, but if not we'll find out
1923 switch (DBUS_UINT32_FROM_BE (*(int*)field))
1925 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
1926 if (fields[FIELD_SERVICE].offset >= 0)
1928 _dbus_verbose ("%s field provided twice\n",
1929 DBUS_HEADER_FIELD_SERVICE);
1933 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1934 _dbus_verbose ("Found service name at offset %d\n",
1935 fields[FIELD_SERVICE].offset);
1938 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
1939 if (fields[FIELD_NAME].offset >= 0)
1941 _dbus_verbose ("%s field provided twice\n",
1942 DBUS_HEADER_FIELD_NAME);
1946 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1948 _dbus_verbose ("Found message name at offset %d\n",
1949 fields[FIELD_NAME].offset);
1951 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
1952 if (fields[FIELD_SENDER].offset >= 0)
1954 _dbus_verbose ("%s field provided twice\n",
1955 DBUS_HEADER_FIELD_SENDER);
1959 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1961 _dbus_verbose ("Found sender name at offset %d\n",
1962 fields[FIELD_NAME].offset);
1965 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
1966 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
1968 _dbus_verbose ("%s field provided twice\n",
1969 DBUS_HEADER_FIELD_REPLY);
1973 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1975 _dbus_verbose ("Found reply serial at offset %d\n",
1976 fields[FIELD_REPLY_SERIAL].offset);
1980 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c\n",
1981 field[0], field[1], field[2], field[3]);
1984 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
1987 if (new_pos > header_len)
1993 if (pos < header_len)
1995 /* Alignment padding, verify that it's nul */
1996 _dbus_assert ((header_len - pos) < 8);
1998 if (!_dbus_string_validate_nul (data,
1999 pos, (header_len - pos)))
2001 _dbus_verbose ("header alignment padding is not nul\n");
2010 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2011 * indicating to the loader how many bytes of the buffer were filled
2012 * in. This function must always be called, even if no bytes were
2013 * successfully read.
2015 * @param loader the loader.
2016 * @param buffer the buffer.
2017 * @param bytes_read number of bytes that were read into the buffer.
2020 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2024 _dbus_assert (loader->buffer_outstanding);
2025 _dbus_assert (buffer == &loader->data);
2027 loader->buffer_outstanding = FALSE;
2029 if (loader->corrupted)
2032 while (_dbus_string_get_length (&loader->data) >= 16)
2034 DBusMessage *message;
2035 const char *header_data;
2036 int byte_order, header_len, body_len;
2037 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2039 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2041 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2043 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2045 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2046 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2047 loader->corrupted = TRUE;
2051 byte_order = header_data[0];
2053 if (byte_order != DBUS_LITTLE_ENDIAN &&
2054 byte_order != DBUS_BIG_ENDIAN)
2056 _dbus_verbose ("Message with bad byte order '%c' received\n",
2058 loader->corrupted = TRUE;
2062 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2063 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2065 if (header_len_unsigned < 16)
2067 _dbus_verbose ("Message had broken too-small header length %u\n",
2068 header_len_unsigned);
2069 loader->corrupted = TRUE;
2073 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2074 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2076 _dbus_verbose ("Header or body length too large (%u %u)\n",
2077 header_len_unsigned,
2079 loader->corrupted = TRUE;
2083 /* Now that we know the values are in signed range, get
2084 * rid of stupid unsigned, just causes bugs
2086 header_len = header_len_unsigned;
2087 body_len = body_len_unsigned;
2089 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2091 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2093 loader->corrupted = TRUE;
2097 if (header_len + body_len > loader->max_message_size)
2099 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2100 header_len, body_len, loader->max_message_size);
2101 loader->corrupted = TRUE;
2105 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2107 HeaderField fields[FIELD_LAST];
2111 if (!decode_header_data (&loader->data, header_len, byte_order,
2114 loader->corrupted = TRUE;
2118 next_arg = header_len;
2119 while (next_arg < (header_len + body_len))
2121 int prev = next_arg;
2123 if (!_dbus_marshal_validate_arg (&loader->data,
2128 loader->corrupted = TRUE;
2132 _dbus_assert (next_arg > prev);
2135 if (next_arg > (header_len + body_len))
2137 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2138 next_arg, header_len, body_len,
2139 header_len + body_len);
2140 loader->corrupted = TRUE;
2144 message = dbus_message_new_empty_header ();
2145 if (message == NULL)
2146 break; /* ugh, postpone this I guess. */
2148 message->byte_order = byte_order;
2150 /* Copy in the offsets we found */
2152 while (i < FIELD_LAST)
2154 message->header_fields[i] = fields[i];
2158 if (!_dbus_list_append (&loader->messages, message))
2160 dbus_message_unref (message);
2164 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2165 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2167 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2168 (header_len + body_len));
2170 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2172 _dbus_list_remove_last (&loader->messages, message);
2173 dbus_message_unref (message);
2177 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2181 /* put the header back, we'll try again later */
2182 result = _dbus_string_copy_len (&message->header, 0, header_len,
2184 _dbus_assert (result); /* because DBusString never reallocs smaller */
2186 _dbus_list_remove_last (&loader->messages, message);
2187 dbus_message_unref (message);
2191 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2192 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2194 _dbus_verbose ("Loaded message %p\n", message);
2202 * Pops a loaded message (passing ownership of the message
2203 * to the caller). Returns #NULL if no messages have been
2206 * @param loader the loader.
2207 * @returns the next message, or #NULL if none.
2210 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2212 return _dbus_list_pop_first (&loader->messages);
2217 * Checks whether the loader is confused due to bad data.
2218 * If messages are received that are invalid, the
2219 * loader gets confused and gives up permanently.
2220 * This state is called "corrupted."
2222 * @param loader the loader
2223 * @returns #TRUE if the loader is hosed.
2226 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2228 return loader->corrupted;
2232 * Sets the maximum size message we allow.
2234 * @param loader the loader
2235 * @param size the max message size in bytes
2238 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2241 if (size > MAX_SANE_MESSAGE_SIZE)
2243 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2244 size, MAX_SANE_MESSAGE_SIZE);
2245 size = MAX_SANE_MESSAGE_SIZE;
2247 loader->max_message_size = size;
2251 * Gets the maximum allowed message size in bytes.
2253 * @param loader the loader
2254 * @returns max size in bytes
2257 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2259 return loader->max_message_size;
2263 #ifdef DBUS_BUILD_TESTS
2264 #include "dbus-test.h"
2268 message_iter_test (DBusMessage *message)
2270 DBusMessageIter *iter;
2273 iter = dbus_message_get_fields_iter (message);
2276 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_STRING)
2277 _dbus_assert_not_reached ("Field type isn't string");
2279 str = dbus_message_iter_get_string (iter);
2280 if (strcmp (str, "Test string") != 0)
2281 _dbus_assert_not_reached ("Strings differ");
2284 if (!dbus_message_iter_next (iter))
2285 _dbus_assert_not_reached ("Reached end of fields");
2287 /* Signed integer tests */
2288 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_INT32)
2289 _dbus_assert_not_reached ("Field type isn't int32");
2291 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2292 _dbus_assert_not_reached ("Signed integers differ");
2294 if (!dbus_message_iter_next (iter))
2295 _dbus_assert_not_reached ("Reached end of fields");
2297 /* Unsigned integer tests */
2298 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_UINT32)
2299 _dbus_assert_not_reached ("Field type isn't int32");
2301 if (dbus_message_iter_get_int32 (iter) != 0xedd1e)
2302 _dbus_assert_not_reached ("Unsigned integers differ");
2304 if (!dbus_message_iter_next (iter))
2305 _dbus_assert_not_reached ("Reached end of fields");
2308 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_DOUBLE)
2309 _dbus_assert_not_reached ("Field type isn't double");
2311 if (dbus_message_iter_get_double (iter) != 3.14159)
2312 _dbus_assert_not_reached ("Doubles differ");
2314 if (dbus_message_iter_next (iter))
2315 _dbus_assert_not_reached ("Didn't reach end of fields");
2317 dbus_message_iter_unref (iter);
2321 check_message_handling (DBusMessage *message)
2323 DBusMessageIter *iter;
2326 dbus_int32_t client_serial;
2331 client_serial = _dbus_message_get_client_serial (message);
2333 /* can't use set_client_serial due to the assertions at the start of it */
2334 set_int_field (message, FIELD_CLIENT_SERIAL,
2337 if (client_serial != _dbus_message_get_client_serial (message))
2339 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
2343 /* If we implement message_set_field (message, n, value)
2344 * then we would want to test it here
2347 iter = dbus_message_get_fields_iter (message);
2348 while ((type = dbus_message_iter_get_field_type (iter)) != DBUS_TYPE_INVALID)
2352 case DBUS_TYPE_STRING:
2355 str = dbus_message_iter_get_string (iter);
2361 if (!dbus_message_iter_next (iter))
2369 dbus_message_iter_unref (iter);
2375 check_have_valid_message (DBusMessageLoader *loader)
2377 DBusMessage *message;
2383 if (_dbus_message_loader_get_is_corrupted (loader))
2385 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
2389 message = _dbus_message_loader_pop_message (loader);
2390 if (message == NULL)
2392 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
2396 if (_dbus_string_get_length (&loader->data) > 0)
2398 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
2402 /* Verify that we're able to properly deal with the message.
2403 * For example, this would detect improper handling of messages
2404 * in nonstandard byte order.
2406 if (!check_message_handling (message))
2413 dbus_message_unref (message);
2418 check_invalid_message (DBusMessageLoader *loader)
2424 if (!_dbus_message_loader_get_is_corrupted (loader))
2426 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
2437 check_incomplete_message (DBusMessageLoader *loader)
2439 DBusMessage *message;
2445 if (_dbus_message_loader_get_is_corrupted (loader))
2447 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
2451 message = _dbus_message_loader_pop_message (loader);
2452 if (message != NULL)
2454 _dbus_warn ("loaded message that was expected to be incomplete\n");
2462 dbus_message_unref (message);
2467 check_loader_results (DBusMessageLoader *loader,
2468 DBusMessageValidity validity)
2472 case _DBUS_MESSAGE_VALID:
2473 return check_have_valid_message (loader);
2474 case _DBUS_MESSAGE_INVALID:
2475 return check_invalid_message (loader);
2476 case _DBUS_MESSAGE_INCOMPLETE:
2477 return check_incomplete_message (loader);
2478 case _DBUS_MESSAGE_UNKNOWN:
2482 _dbus_assert_not_reached ("bad DBusMessageValidity");
2488 * Loads the message in the given message file.
2490 * @param filename filename to load
2491 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
2492 * @param data string to load message into
2493 * @returns #TRUE if the message was loaded
2496 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
2506 DBusResultCode result;
2508 result = _dbus_file_get_contents (data, filename);
2509 if (result != DBUS_RESULT_SUCCESS)
2512 _dbus_string_get_const_data (filename, &s);
2513 _dbus_warn ("Could not load message file %s\n", s);
2519 if (!_dbus_message_data_load (data, filename))
2522 _dbus_string_get_const_data (filename, &s);
2523 _dbus_warn ("Could not load message file %s\n", s);
2536 * Tries loading the message in the given message file
2537 * and verifies that DBusMessageLoader can handle it.
2539 * @param filename filename to load
2540 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
2541 * @param expected_validity what the message has to be like to return #TRUE
2542 * @returns #TRUE if the message has the expected validity
2545 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
2547 DBusMessageValidity expected_validity)
2554 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
2555 _dbus_assert_not_reached ("could not allocate string\n");
2557 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
2561 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
2569 if (_dbus_string_get_length (&data) > 0)
2570 _dbus_verbose_bytes_of_string (&data, 0,
2571 _dbus_string_get_length (&data));
2573 _dbus_string_get_const_data (filename, &s);
2574 _dbus_warn ("Failed message loader test on %s\n",
2578 _dbus_string_free (&data);
2584 * Tries loading the given message data.
2587 * @param data the message data
2588 * @param expected_validity what the message has to be like to return #TRUE
2589 * @returns #TRUE if the message has the expected validity
2592 dbus_internal_do_not_use_try_message_data (const DBusString *data,
2593 DBusMessageValidity expected_validity)
2595 DBusMessageLoader *loader;
2603 /* Write the data one byte at a time */
2605 loader = _dbus_message_loader_new ();
2607 len = _dbus_string_get_length (data);
2608 for (i = 0; i < len; i++)
2612 _dbus_message_loader_get_buffer (loader, &buffer);
2613 _dbus_string_append_byte (buffer,
2614 _dbus_string_get_byte (data, i));
2615 _dbus_message_loader_return_buffer (loader, buffer, 1);
2618 if (!check_loader_results (loader, expected_validity))
2621 _dbus_message_loader_unref (loader);
2624 /* Write the data all at once */
2626 loader = _dbus_message_loader_new ();
2631 _dbus_message_loader_get_buffer (loader, &buffer);
2632 _dbus_string_copy (data, 0, buffer,
2633 _dbus_string_get_length (buffer));
2634 _dbus_message_loader_return_buffer (loader, buffer, 1);
2637 if (!check_loader_results (loader, expected_validity))
2640 _dbus_message_loader_unref (loader);
2643 /* Write the data 2 bytes at a time */
2645 loader = _dbus_message_loader_new ();
2647 len = _dbus_string_get_length (data);
2648 for (i = 0; i < len; i += 2)
2652 _dbus_message_loader_get_buffer (loader, &buffer);
2653 _dbus_string_append_byte (buffer,
2654 _dbus_string_get_byte (data, i));
2656 _dbus_string_append_byte (buffer,
2657 _dbus_string_get_byte (data, i+1));
2658 _dbus_message_loader_return_buffer (loader, buffer, 1);
2661 if (!check_loader_results (loader, expected_validity))
2664 _dbus_message_loader_unref (loader);
2672 _dbus_message_loader_unref (loader);
2678 process_test_subdir (const DBusString *test_base_dir,
2680 DBusMessageValidity validity,
2681 DBusForeachMessageFileFunc function,
2684 DBusString test_directory;
2685 DBusString filename;
2688 DBusResultCode result;
2693 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
2694 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2696 _dbus_string_init_const (&filename, subdir);
2698 if (!_dbus_string_copy (test_base_dir, 0,
2699 &test_directory, 0))
2700 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2702 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2703 _dbus_assert_not_reached ("couldn't allocate full path");
2705 _dbus_string_free (&filename);
2706 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
2707 _dbus_assert_not_reached ("didn't allocate filename string\n");
2709 dir = _dbus_directory_open (&test_directory, &result);
2713 _dbus_string_get_const_data (&test_directory, &s);
2714 _dbus_warn ("Could not open %s: %s\n", s,
2715 dbus_result_to_string (result));
2719 printf ("Testing:\n");
2721 result = DBUS_RESULT_SUCCESS;
2723 while (_dbus_directory_get_next_file (dir, &filename, &result))
2725 DBusString full_path;
2728 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
2729 _dbus_assert_not_reached ("couldn't init string");
2731 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2732 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2734 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2735 _dbus_assert_not_reached ("couldn't concat file to dir");
2737 if (_dbus_string_ends_with_c_str (&filename, ".message"))
2739 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
2743 const char *filename_c;
2744 _dbus_string_get_const_data (&filename, &filename_c);
2745 _dbus_verbose ("Skipping non-.message file %s\n",
2752 _dbus_string_get_const_data (&filename, &s);
2753 printf (" %s\n", s);
2756 _dbus_verbose (" expecting %s\n",
2757 validity == _DBUS_MESSAGE_VALID ? "valid" :
2758 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
2759 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
2761 if (! (*function) (&full_path, is_raw, validity, user_data))
2763 _dbus_string_free (&full_path);
2767 _dbus_string_free (&full_path);
2770 if (result != DBUS_RESULT_SUCCESS)
2773 _dbus_string_get_const_data (&test_directory, &s);
2774 _dbus_warn ("Could not get next file in %s: %s\n",
2775 s, dbus_result_to_string (result));
2784 _dbus_directory_close (dir);
2785 _dbus_string_free (&test_directory);
2786 _dbus_string_free (&filename);
2792 * Runs the given function on every message file in the test suite.
2793 * The function should return #FALSE on test failure or fatal error.
2795 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
2796 * @param func the function to run
2797 * @param user_data data for function
2798 * @returns #FALSE if there's a failure
2801 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
2802 DBusForeachMessageFileFunc func,
2805 DBusString test_directory;
2810 _dbus_string_init_const (&test_directory, test_data_dir);
2812 if (!process_test_subdir (&test_directory, "valid-messages",
2813 _DBUS_MESSAGE_VALID, func, user_data))
2816 if (!process_test_subdir (&test_directory, "invalid-messages",
2817 _DBUS_MESSAGE_INVALID, func, user_data))
2820 if (!process_test_subdir (&test_directory, "incomplete-messages",
2821 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
2828 _dbus_string_free (&test_directory);
2834 * @ingroup DBusMessageInternals
2835 * Unit test for DBusMessage.
2837 * @returns #TRUE on success.
2840 _dbus_message_test (const char *test_data_dir)
2842 DBusMessage *message;
2843 DBusMessageLoader *loader;
2846 dbus_int32_t our_int;
2850 /* Test the vararg functions */
2851 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
2852 _dbus_message_set_client_serial (message, 1);
2853 dbus_message_append_fields (message,
2854 DBUS_TYPE_INT32, -0x12345678,
2855 DBUS_TYPE_STRING, "Test string",
2856 DBUS_TYPE_DOUBLE, 3.14159,
2858 _dbus_verbose_bytes_of_string (&message->header, 0,
2859 _dbus_string_get_length (&message->header));
2860 _dbus_verbose_bytes_of_string (&message->body, 0,
2861 _dbus_string_get_length (&message->body));
2863 if (dbus_message_get_fields (message,
2864 DBUS_TYPE_INT32, &our_int,
2865 DBUS_TYPE_STRING, &our_str,
2866 DBUS_TYPE_DOUBLE, &our_double,
2867 0) != DBUS_RESULT_SUCCESS)
2868 _dbus_assert_not_reached ("Could not get fields");
2870 if (our_int != -0x12345678)
2871 _dbus_assert_not_reached ("integers differ!");
2873 if (our_double != 3.14159)
2874 _dbus_assert_not_reached ("doubles differ!");
2876 if (strcmp (our_str, "Test string") != 0)
2877 _dbus_assert_not_reached ("strings differ!");
2879 dbus_free (our_str);
2880 dbus_message_unref (message);
2882 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
2883 _dbus_message_set_client_serial (message, 1);
2884 _dbus_message_set_reply_serial (message, 0x12345678);
2886 dbus_message_append_string (message, "Test string");
2887 dbus_message_append_int32 (message, -0x12345678);
2888 dbus_message_append_uint32 (message, 0xedd1e);
2889 dbus_message_append_double (message, 3.14159);
2891 message_iter_test (message);
2893 /* Message loader test */
2894 _dbus_message_lock (message);
2895 loader = _dbus_message_loader_new ();
2897 /* Write the header data one byte at a time */
2898 _dbus_string_get_const_data (&message->header, &data);
2899 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
2903 _dbus_message_loader_get_buffer (loader, &buffer);
2904 _dbus_string_append_byte (buffer, data[i]);
2905 _dbus_message_loader_return_buffer (loader, buffer, 1);
2908 /* Write the body data one byte at a time */
2909 _dbus_string_get_const_data (&message->body, &data);
2910 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
2914 _dbus_message_loader_get_buffer (loader, &buffer);
2915 _dbus_string_append_byte (buffer, data[i]);
2916 _dbus_message_loader_return_buffer (loader, buffer, 1);
2919 dbus_message_unref (message);
2921 /* Now pop back the message */
2922 if (_dbus_message_loader_get_is_corrupted (loader))
2923 _dbus_assert_not_reached ("message loader corrupted");
2925 message = _dbus_message_loader_pop_message (loader);
2927 _dbus_assert_not_reached ("received a NULL message");
2929 if (_dbus_message_get_reply_serial (message) != 0x12345678)
2930 _dbus_assert_not_reached ("reply serial fields differ");
2932 message_iter_test (message);
2934 dbus_message_unref (message);
2935 _dbus_message_loader_unref (loader);
2937 /* Now load every message in test_data_dir if we have one */
2938 if (test_data_dir == NULL)
2941 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
2942 (DBusForeachMessageFileFunc)
2943 dbus_internal_do_not_use_try_message_file,
2947 #endif /* DBUS_BUILD_TESTS */