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"
34 * @defgroup DBusMessageInternals DBusMessage implementation details
35 * @ingroup DBusInternals
36 * @brief DBusMessage private implementation details.
38 * The guts of DBusMessage and its methods.
56 static dbus_bool_t field_is_named[FIELD_LAST] =
58 FALSE, /* FIELD_HEADER_LENGTH */
59 FALSE, /* FIELD_BODY_LENGTH */
60 FALSE, /* FIELD_CLIENT_SERIAL */
61 TRUE, /* FIELD_NAME */
62 TRUE, /* FIELD_SERVICE */
63 TRUE, /* FIELD_SENDER */
64 TRUE /* FIELD_REPLY_SERIAL */
69 int offset; /**< Offset to start of field (location of name of field
75 * @brief Internals of DBusMessage
77 * Object representing a message received from or to be sent to
78 * another application. This is an opaque object, all members
83 int refcount; /**< Reference count */
85 DBusString header; /**< Header network data, stored
86 * separately from body so we can
87 * independently realloc it.
90 HeaderField header_fields[FIELD_LAST]; /**< Track the location
91 * of each field in "header"
94 DBusString body; /**< Body network data. */
96 char byte_order; /**< Message byte order. */
98 DBusCounter *size_counter; /**< Counter for the size of the message, or #NULL */
99 long size_counter_delta; /**< Size we incremented the size counter by. */
101 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
105 * @brief Internals of DBusMessageIter
107 * Object representing a position in a message. All fields are internal.
109 struct DBusMessageIter
111 int refcount; /**< Reference count */
113 int pos; /**< Current position in the string */
115 DBusMessage *message; /**< Message used */
119 * Gets the data to be sent over the network for this message.
120 * The header and then the body should be written out.
121 * This function is guaranteed to always return the same
122 * data once a message is locked (with _dbus_message_lock()).
124 * @param message the message.
125 * @param header return location for message header data.
126 * @param body return location for message body data.
129 _dbus_message_get_network_data (DBusMessage *message,
130 const DBusString **header,
131 const DBusString **body)
133 _dbus_assert (message->locked);
135 *header = &message->header;
136 *body = &message->body;
140 adjust_field_offsets (DBusMessage *message,
150 while (i < FIELD_LAST)
152 if (message->header_fields[i].offset > offsets_after)
153 message->header_fields[i].offset += delta;
160 get_string_field (DBusMessage *message,
164 int offset = message->header_fields[field].offset;
170 /* offset points to string length, string data follows it */
171 /* FIXME _dbus_demarshal_const_string() that returned
172 * a reference to the string plus its len might be nice.
176 *len = _dbus_demarshal_uint32 (&message->header,
181 _dbus_string_get_const_data (&message->header,
184 return data + (offset + 4);
188 get_int_field (DBusMessage *message,
191 int offset = message->header_fields[field].offset;
194 return -1; /* useless if -1 is a valid value of course */
196 return _dbus_demarshal_int32 (&message->header,
203 append_int_field (DBusMessage *message,
210 _dbus_assert (!message->locked);
212 orig_len = _dbus_string_get_length (&message->header);
214 if (!_dbus_string_align_length (&message->header, 4))
217 if (!_dbus_string_append_len (&message->header, name, 4))
220 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
223 if (!_dbus_string_align_length (&message->header, 4))
226 message->header_fields[FIELD_REPLY_SERIAL].offset =
227 _dbus_string_get_length (&message->header);
229 if (!_dbus_marshal_int32 (&message->header, DBUS_COMPILER_BYTE_ORDER,
236 message->header_fields[field].offset = -1;
237 _dbus_string_set_length (&message->header, orig_len);
242 append_string_field (DBusMessage *message,
249 _dbus_assert (!message->locked);
251 orig_len = _dbus_string_get_length (&message->header);
253 if (!_dbus_string_align_length (&message->header, 4))
256 if (!_dbus_string_append_len (&message->header, name, 4))
259 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
262 if (!_dbus_string_align_length (&message->header, 4))
265 message->header_fields[field].offset =
266 _dbus_string_get_length (&message->header);
268 if (!_dbus_marshal_string (&message->header, DBUS_COMPILER_BYTE_ORDER,
275 message->header_fields[field].offset = -1;
276 _dbus_string_set_length (&message->header, orig_len);
281 delete_int_field (DBusMessage *message,
284 int offset = message->header_fields[field].offset;
286 _dbus_assert (!message->locked);
287 _dbus_assert (field_is_named[field]);
292 /* The field typecode and name take up 8 bytes */
293 _dbus_string_delete (&message->header,
297 message->header_fields[field].offset = -1;
299 adjust_field_offsets (message,
305 delete_string_field (DBusMessage *message,
308 int offset = message->header_fields[field].offset;
312 _dbus_assert (!message->locked);
313 _dbus_assert (field_is_named[field]);
318 get_string_field (message, field, &len);
320 /* The field typecode and name take up 8 bytes, and the nul
321 * termination is 1 bytes, string length integer is 4 bytes
323 delete_len = 8 + 4 + 1 + len;
325 _dbus_string_delete (&message->header,
329 message->header_fields[field].offset = -1;
331 adjust_field_offsets (message,
337 set_int_field (DBusMessage *message,
341 int offset = message->header_fields[field].offset;
343 _dbus_assert (!message->locked);
347 /* need to append the field */
351 case FIELD_REPLY_SERIAL:
352 return append_int_field (message, field,
353 DBUS_HEADER_FIELD_REPLY,
356 _dbus_assert_not_reached ("appending an int field we don't support appending");
362 _dbus_marshal_set_int32 (&message->header,
371 set_string_field (DBusMessage *message,
375 int offset = message->header_fields[field].offset;
377 _dbus_assert (!message->locked);
378 _dbus_assert (value != NULL);
382 /* need to append the field */
387 return append_string_field (message, field,
388 DBUS_HEADER_FIELD_SENDER,
391 _dbus_assert_not_reached ("appending a string field we don't support appending");
401 old_len = _dbus_string_get_length (&message->header);
403 _dbus_string_init_const_len (&v, value,
404 strlen (value) + 1); /* include nul */
405 if (!_dbus_marshal_set_string (&message->header,
410 new_len = _dbus_string_get_length (&message->header);
412 adjust_field_offsets (message,
421 * Sets the client serial of a message.
422 * This can only be done once on a message.
424 * @param message the message
425 * @param client_serial the client serial
428 _dbus_message_set_client_serial (DBusMessage *message,
429 dbus_int32_t client_serial)
431 _dbus_assert (!message->locked);
432 _dbus_assert (_dbus_message_get_client_serial (message) < 0);
434 set_int_field (message, FIELD_CLIENT_SERIAL,
439 * Sets the reply serial of a message (the client serial
440 * of the message this is a reply to).
442 * @param message the message
443 * @param reply_serial the client serial
444 * @returns #FALSE if not enough memory
447 _dbus_message_set_reply_serial (DBusMessage *message,
448 dbus_int32_t reply_serial)
450 _dbus_assert (!message->locked);
452 return set_int_field (message, FIELD_REPLY_SERIAL,
457 * Returns the client serial of a message or
458 * -1 if none has been specified.
460 * @param message the message
461 * @returns the client serial
464 _dbus_message_get_client_serial (DBusMessage *message)
466 return get_int_field (message, FIELD_CLIENT_SERIAL);
470 * Returns the serial that the message is
471 * a reply to or -1 if none.
473 * @param message the message
474 * @returns the reply serial
477 _dbus_message_get_reply_serial (DBusMessage *message)
479 return get_int_field (message, FIELD_REPLY_SERIAL);
483 * Adds a counter to be incremented immediately with the
484 * size of this message, and decremented by the size
485 * of this message when this message if finalized.
487 * @param message the message
488 * @param counter the counter
491 _dbus_message_add_size_counter (DBusMessage *message,
492 DBusCounter *counter)
494 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
495 * counters instead of just one
498 message->size_counter = counter;
499 _dbus_counter_ref (message->size_counter);
501 /* When we can change message size, we may want to
502 * update this each time we do so, or we may want to
503 * just KISS like this.
505 message->size_counter_delta =
506 _dbus_string_get_length (&message->header) +
507 _dbus_string_get_length (&message->body);
509 _dbus_verbose ("message has size %ld\n",
510 message->size_counter_delta);
512 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
516 dbus_message_create_header (DBusMessage *message,
520 if (!_dbus_string_append_byte (&message->header, DBUS_COMPILER_BYTE_ORDER))
523 if (!_dbus_string_append_len (&message->header, "\0\0\0", 3))
526 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
527 if (!_dbus_marshal_int32 (&message->header, message->byte_order, 0))
530 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
531 if (!_dbus_marshal_int32 (&message->header, message->byte_order, 0))
534 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
535 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
538 /* Marshal message service */
541 if (!append_string_field (message,
543 DBUS_HEADER_FIELD_SERVICE,
548 _dbus_assert (name != NULL);
549 if (!append_string_field (message,
551 DBUS_HEADER_FIELD_NAME,
559 * Locks a message. Allows checking that applications don't keep a
560 * reference to a message in the outgoing queue and change it
561 * underneath us. Messages are locked when they enter the outgoing
562 * queue (dbus_connection_send_message()), and the library complains
563 * if the message is modified while locked.
565 * @param message the message to lock.
568 _dbus_message_lock (DBusMessage *message)
570 if (!message->locked)
572 /* Fill in our lengths */
573 set_int_field (message,
575 _dbus_string_get_length (&message->header));
577 set_int_field (message,
579 _dbus_string_get_length (&message->body));
581 message->locked = TRUE;
588 * @defgroup DBusMessage DBusMessage
590 * @brief Message to be sent or received over a DBusConnection.
592 * A DBusMessage is the most basic unit of communication over a
593 * DBusConnection. A DBusConnection represents a stream of messages
594 * received from a remote application, and a stream of messages
595 * sent to a remote application.
601 * @typedef DBusMessage
603 * Opaque data type representing a message received from or to be
604 * sent to another application.
608 dbus_message_new_empty_header (void)
610 DBusMessage *message;
613 message = dbus_new0 (DBusMessage, 1);
617 message->refcount = 1;
618 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
621 while (i < FIELD_LAST)
623 message->header_fields[i].offset = -1;
627 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
633 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
635 _dbus_string_free (&message->header);
645 * Constructs a new message. Returns #NULL if memory
646 * can't be allocated for the message.
648 * @todo use DBusString internally to store service and name.
650 * @param service service that the message should be sent to
651 * @param name name of the message
652 * @returns a new DBusMessage, free with dbus_message_unref()
653 * @see dbus_message_unref()
656 dbus_message_new (const char *service,
659 DBusMessage *message;
661 message = dbus_message_new_empty_header ();
665 if (!dbus_message_create_header (message, service, name))
667 dbus_message_unref (message);
675 * Constructs a message that is a reply to some other
676 * message. Returns #NULL if memory can't be allocated
679 * @param name the name of the message
680 * @param original_message the message which the created
681 * message is a reply to.
682 * @returns a new DBusMessage, free with dbus_message_unref()
683 * @see dbus_message_new(), dbus_message_unref()
686 dbus_message_new_reply (const char *name,
687 DBusMessage *original_message)
689 DBusMessage *message;
692 sender = get_string_field (original_message,
695 _dbus_assert (sender != NULL);
697 message = dbus_message_new (sender, name);
702 if (!_dbus_message_set_reply_serial (message,
703 _dbus_message_get_client_serial (original_message)))
705 dbus_message_unref (message);
714 * Increments the reference count of a DBusMessage.
716 * @param message The message
717 * @see dbus_message_unref
720 dbus_message_ref (DBusMessage *message)
722 _dbus_assert (message->refcount > 0);
724 message->refcount += 1;
728 * Decrements the reference count of a DBusMessage.
730 * @param message The message
731 * @see dbus_message_ref
734 dbus_message_unref (DBusMessage *message)
736 _dbus_assert (message->refcount > 0);
738 message->refcount -= 1;
739 if (message->refcount == 0)
741 if (message->size_counter != NULL)
743 _dbus_counter_adjust (message->size_counter,
744 - message->size_counter_delta);
745 _dbus_counter_unref (message->size_counter);
748 _dbus_string_free (&message->header);
749 _dbus_string_free (&message->body);
756 * Gets the name of a message.
758 * @param message the message
759 * @returns the message name (should not be freed)
762 dbus_message_get_name (DBusMessage *message)
764 return get_string_field (message, FIELD_NAME, NULL);
768 * Gets the destination service of a message.
770 * @param message the message
771 * @returns the message destination service (should not be freed)
774 dbus_message_get_service (DBusMessage *message)
776 return get_string_field (message, FIELD_SERVICE, NULL);
780 * Appends fields to a message given a variable argument
781 * list. The variable argument list should contain the type
782 * of the field followed by the value to add.
783 * The list is terminated with 0.
785 * @param message the message
786 * @param first_field_type type of the first field
787 * @param ... value of first field, list of additional type-value pairs
788 * @returns #TRUE on success
791 dbus_message_append_fields (DBusMessage *message,
792 int first_field_type,
798 va_start (var_args, first_field_type);
799 retval = dbus_message_append_fields_valist (message,
808 * This function takes a va_list for use by language bindings
810 * @see dbus_message_append_fields.
811 * @param message the message
812 * @param first_field_type type of first field
813 * @param var_args value of first field, then list of type/value pairs
814 * @returns #TRUE on success
817 dbus_message_append_fields_valist (DBusMessage *message,
818 int first_field_type,
823 old_len = _dbus_string_get_length (&message->body);
825 type = first_field_type;
831 case DBUS_TYPE_INT32:
832 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
835 case DBUS_TYPE_UINT32:
836 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
839 case DBUS_TYPE_DOUBLE:
840 if (!dbus_message_append_double (message, va_arg (var_args, double)))
843 case DBUS_TYPE_STRING:
844 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
847 case DBUS_TYPE_BYTE_ARRAY:
852 data = va_arg (var_args, unsigned char *);
853 len = va_arg (var_args, int);
855 if (!dbus_message_append_byte_array (message, data, len))
859 case DBUS_TYPE_STRING_ARRAY:
864 data = va_arg (var_args, const char **);
865 len = va_arg (var_args, int);
867 if (!dbus_message_append_string_array (message, data, len))
873 _dbus_warn ("Unknown field type %d\n", type);
876 type = va_arg (var_args, int);
882 _dbus_string_set_length (&message->body, old_len);
887 * Appends a 32 bit signed integer to the message.
889 * @param message the message
890 * @param value the integer value
891 * @returns #TRUE on success
894 dbus_message_append_int32 (DBusMessage *message,
897 _dbus_assert (!message->locked);
899 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
901 _dbus_string_shorten (&message->body, 1);
905 return _dbus_marshal_int32 (&message->body,
906 DBUS_COMPILER_BYTE_ORDER, value);
910 * Appends a 32 bit unsigned integer to the message.
912 * @param message the message
913 * @param value the integer value
914 * @returns #TRUE on success
917 dbus_message_append_uint32 (DBusMessage *message,
920 _dbus_assert (!message->locked);
922 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
924 _dbus_string_shorten (&message->body, 1);
928 return _dbus_marshal_uint32 (&message->body,
929 DBUS_COMPILER_BYTE_ORDER, value);
933 * Appends a double value to the message.
935 * @param message the message
936 * @param value the double value
937 * @returns #TRUE on success
940 dbus_message_append_double (DBusMessage *message,
943 _dbus_assert (!message->locked);
945 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
947 _dbus_string_shorten (&message->body, 1);
951 return _dbus_marshal_double (&message->body,
952 DBUS_COMPILER_BYTE_ORDER, value);
956 * Appends a UTF-8 string to the message.
958 * @param message the message
959 * @param value the string
960 * @returns #TRUE on success
963 dbus_message_append_string (DBusMessage *message,
966 _dbus_assert (!message->locked);
968 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
970 _dbus_string_shorten (&message->body, 1);
974 return _dbus_marshal_string (&message->body,
975 DBUS_COMPILER_BYTE_ORDER, value);
979 * Appends a byte array to the message.
981 * @param message the message
982 * @param value the array
983 * @param len the length of the array
984 * @returns #TRUE on success
987 dbus_message_append_byte_array (DBusMessage *message,
988 unsigned const char *value,
991 _dbus_assert (!message->locked);
993 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
995 _dbus_string_shorten (&message->body, 1);
999 return _dbus_marshal_byte_array (&message->body,
1000 DBUS_COMPILER_BYTE_ORDER, value, len);
1004 * Appends a string array to the message.
1006 * @param message the message
1007 * @param value the array
1008 * @param len the length of the array
1009 * @returns #TRUE on success
1012 dbus_message_append_string_array (DBusMessage *message,
1016 _dbus_assert (!message->locked);
1018 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1020 _dbus_string_shorten (&message->body, 1);
1024 return _dbus_marshal_string_array (&message->body,
1025 DBUS_COMPILER_BYTE_ORDER, value, len);
1029 * Gets fields from a message given a variable argument list.
1030 * The variable argument list should contain the type of the
1031 * field followed by a pointer to where the value should be
1032 * stored. The list is terminated with 0.
1034 * @todo rename get_args to avoid confusion with header fields
1036 * @param message the message
1037 * @param first_field_type the first field type
1038 * @param ... location for first field value, then list of type-location pairs
1039 * @returns result code
1042 dbus_message_get_fields (DBusMessage *message,
1043 int first_field_type,
1046 DBusResultCode retval;
1049 va_start (var_args, first_field_type);
1050 retval = dbus_message_get_fields_valist (message, first_field_type, var_args);
1057 * This function takes a va_list for use by language bindings
1059 * @todo this function (or some lower-level non-convenience function)
1060 * needs better error handling; should allow the application to
1061 * distinguish between out of memory, and bad data from the remote
1062 * app. It also needs to not leak a bunch of args when it gets
1063 * to the arg that's bad, as that would be a security hole
1064 * (allow one app to force another to leak memory)
1066 * @todo We need to free the field data when an error occurs.
1068 * @todo rename get_args_valist to avoid confusion with header fields
1070 * @see dbus_message_get_fields
1071 * @param message the message
1072 * @param first_field_type type of the first field
1073 * @param var_args return location for first field, followed by list of type/location pairs
1074 * @returns result code
1077 dbus_message_get_fields_valist (DBusMessage *message,
1078 int first_field_type,
1081 int spec_type, msg_type, i;
1082 DBusMessageIter *iter;
1084 iter = dbus_message_get_fields_iter (message);
1087 return DBUS_RESULT_NO_MEMORY;
1089 spec_type = first_field_type;
1092 while (spec_type != 0)
1094 msg_type = dbus_message_iter_get_field_type (iter);
1096 if (msg_type != spec_type)
1098 _dbus_verbose ("Field %d is specified to be of type \"%s\", but "
1099 "is actually of type \"%s\"\n", i,
1100 _dbus_type_to_string (spec_type),
1101 _dbus_type_to_string (msg_type));
1102 dbus_message_iter_unref (iter);
1104 return DBUS_RESULT_INVALID_FIELDS;
1109 case DBUS_TYPE_INT32:
1113 ptr = va_arg (var_args, dbus_int32_t *);
1115 *ptr = dbus_message_iter_get_int32 (iter);
1118 case DBUS_TYPE_UINT32:
1122 ptr = va_arg (var_args, dbus_uint32_t *);
1124 *ptr = dbus_message_iter_get_uint32 (iter);
1128 case DBUS_TYPE_DOUBLE:
1132 ptr = va_arg (var_args, double *);
1134 *ptr = dbus_message_iter_get_double (iter);
1138 case DBUS_TYPE_STRING:
1142 ptr = va_arg (var_args, char **);
1144 *ptr = dbus_message_iter_get_string (iter);
1147 return DBUS_RESULT_NO_MEMORY;
1152 case DBUS_TYPE_BYTE_ARRAY:
1154 unsigned char **ptr;
1157 ptr = va_arg (var_args, unsigned char **);
1158 len = va_arg (var_args, int *);
1160 *ptr = dbus_message_iter_get_byte_array (iter, len);
1163 return DBUS_RESULT_NO_MEMORY;
1167 case DBUS_TYPE_STRING_ARRAY:
1172 ptr = va_arg (var_args, char ***);
1173 len = va_arg (var_args, int *);
1175 *ptr = dbus_message_iter_get_string_array (iter, len);
1178 return DBUS_RESULT_NO_MEMORY;
1183 _dbus_warn ("Unknown field type %d\n", spec_type);
1186 spec_type = va_arg (var_args, int);
1187 if (spec_type != 0 && !dbus_message_iter_next (iter))
1189 _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
1191 dbus_message_iter_unref (iter);
1192 return DBUS_RESULT_INVALID_FIELDS;
1197 dbus_message_iter_unref (iter);
1198 return DBUS_RESULT_SUCCESS;
1202 * Returns a DBusMessageIter representing the fields of the
1203 * message passed in.
1205 * @todo IMO the message iter should follow the GtkTextIter pattern,
1206 * a static object with a "stamp" value used to detect invalid
1207 * iter uses (uninitialized or after changing the message).
1208 * ref/unref is kind of annoying to deal with, and slower too.
1209 * This implies not ref'ing the message from the iter.
1211 * @todo rename get_args_iter to avoid confusion with header fields
1213 * @param message the message
1214 * @returns a new iter.
1217 dbus_message_get_fields_iter (DBusMessage *message)
1219 DBusMessageIter *iter;
1221 iter = dbus_new (DBusMessageIter, 1);
1223 dbus_message_ref (message);
1226 iter->message = message;
1233 * Increments the reference count of a DBusMessageIter.
1235 * @param iter the message iter
1236 * @see dbus_message_iter_unref
1239 dbus_message_iter_ref (DBusMessageIter *iter)
1241 _dbus_assert (iter->refcount > 0);
1243 iter->refcount += 1;
1247 * Decrements the reference count of a DBusMessageIter.
1249 * @param iter The message iter
1250 * @see dbus_message_iter_ref
1253 dbus_message_iter_unref (DBusMessageIter *iter)
1255 _dbus_assert (iter->refcount > 0);
1257 iter->refcount -= 1;
1259 if (iter->refcount == 0)
1261 dbus_message_unref (iter->message);
1268 * Checks if an iterator has any more fields.
1270 * @param iter the message iter
1271 * @returns #TRUE if there are more fields
1275 dbus_message_iter_has_next (DBusMessageIter *iter)
1279 if (!_dbus_marshal_get_field_end_pos (&iter->message->body, iter->message->byte_order,
1280 iter->pos, &end_pos))
1283 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1290 * Moves the iterator to the next field.
1292 * @param iter The message iter
1293 * @returns #TRUE if the iterator was moved to the next field
1296 dbus_message_iter_next (DBusMessageIter *iter)
1300 if (!_dbus_marshal_get_field_end_pos (&iter->message->body, iter->message->byte_order,
1301 iter->pos, &end_pos))
1304 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1307 iter->pos = end_pos;
1313 * Returns the field type of the field that the
1314 * message iterator points at.
1316 * @param iter the message iter
1317 * @returns the field type
1320 dbus_message_iter_get_field_type (DBusMessageIter *iter)
1324 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1325 return DBUS_TYPE_INVALID;
1327 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1329 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1332 return DBUS_TYPE_INVALID;
1336 * Returns the string value that an iterator may point to.
1337 * Note that you need to check that the iterator points to
1338 * a string value before using this function.
1340 * @see dbus_message_iter_get_field_type
1341 * @param iter the message iter
1342 * @returns the string
1345 dbus_message_iter_get_string (DBusMessageIter *iter)
1347 _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_STRING);
1349 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1350 iter->pos + 1, NULL);
1354 * Returns the 32 bit signed integer value that an iterator may point to.
1355 * Note that you need to check that the iterator points to
1356 * a string value before using this function.
1358 * @see dbus_message_iter_get_field_type
1359 * @param iter the message iter
1360 * @returns the integer
1363 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1365 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1366 iter->pos + 1, NULL);
1370 * Returns the 32 bit unsigned integer value that an iterator may point to.
1371 * Note that you need to check that the iterator points to
1372 * a string value before using this function.
1374 * @see dbus_message_iter_get_field_type
1375 * @param iter the message iter
1376 * @returns the integer
1379 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1381 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1382 iter->pos + 1, NULL);
1386 * Returns the double value that an iterator may point to.
1387 * Note that you need to check that the iterator points to
1388 * a string value before using this function.
1390 * @see dbus_message_iter_get_field_type
1391 * @param iter the message iter
1392 * @returns the double
1395 dbus_message_iter_get_double (DBusMessageIter *iter)
1397 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1398 iter->pos + 1, NULL);
1402 * Returns the byte array that the iterator may point to.
1403 * Note that you need to check that the iterator points
1404 * to a byte array prior to using this function.
1406 * @todo this function should probably take "unsigned char **" as
1407 * an out param argument, and return boolean or result code.
1409 * @param iter the iterator
1410 * @param len return location for length of byte array
1411 * @returns the byte array
1414 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
1417 _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_BYTE_ARRAY);
1419 return _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1420 iter->pos + 1, NULL, len);
1424 * Returns the string array that the iterator may point to.
1425 * Note that you need to check that the iterator points
1426 * to a byte array prior to using this function.
1428 * @todo this function should probably take "char **" as
1429 * an out param argument, and return boolean or result code.
1431 * @param iter the iterator
1432 * @param len return location for length of byte array
1433 * @returns the byte array
1436 dbus_message_iter_get_string_array (DBusMessageIter *iter,
1439 _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_STRING_ARRAY);
1441 return _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
1442 iter->pos + 1, NULL, len);
1446 * Sets the message sender.
1448 * @todo implement #NULL sender to unset
1450 * @param message the message
1451 * @param sender the sender
1452 * @returns #FALSE if not enough memory
1455 dbus_message_set_sender (DBusMessage *message,
1458 _dbus_assert (!message->locked);
1462 delete_string_field (message, FIELD_SENDER);
1467 return set_string_field (message,
1474 * Gets the service which originated this message,
1475 * or #NULL if unknown or inapplicable.
1477 * @param message the message
1478 * @returns the service name or #NULL
1481 dbus_message_get_sender (DBusMessage *message)
1483 return get_string_field (message, FIELD_SENDER, NULL);
1489 * @addtogroup DBusMessageInternals
1494 * @typedef DBusMessageLoader
1496 * The DBusMessageLoader object encapsulates the process of converting
1497 * a byte stream into a series of DBusMessage. It buffers the incoming
1498 * bytes as efficiently as possible, and generates a queue of
1499 * messages. DBusMessageLoader is typically used as part of a
1500 * DBusTransport implementation. The DBusTransport then hands off
1501 * the loaded messages to a DBusConnection, making the messages
1502 * visible to the application.
1507 * Implementation details of DBusMessageLoader.
1508 * All members are private.
1510 struct DBusMessageLoader
1512 int refcount; /**< Reference count. */
1514 DBusString data; /**< Buffered data */
1516 DBusList *messages; /**< Complete messages. */
1518 long max_message_size; /**< Maximum size of a message */
1520 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
1522 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
1526 * The initial buffer size of the message loader.
1528 * @todo this should be based on min header size plus some average
1529 * body size, or something. Or rather, the min header size only, if we
1530 * want to try to read only the header, store that in a DBusMessage,
1531 * then read only the body and store that, etc., depends on
1532 * how we optimize _dbus_message_loader_get_buffer() and what
1533 * the exact message format is.
1535 #define INITIAL_LOADER_DATA_LEN 32
1538 * Creates a new message loader. Returns #NULL if memory can't
1541 * @returns new loader, or #NULL.
1544 _dbus_message_loader_new (void)
1546 DBusMessageLoader *loader;
1548 loader = dbus_new0 (DBusMessageLoader, 1);
1552 loader->refcount = 1;
1554 /* Try to cap message size at something that won't *totally* hose
1555 * the system if we have a couple of them.
1557 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
1559 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
1565 /* preallocate the buffer for speed, ignore failure */
1566 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
1567 _dbus_string_set_length (&loader->data, 0);
1573 * Increments the reference count of the loader.
1575 * @param loader the loader.
1578 _dbus_message_loader_ref (DBusMessageLoader *loader)
1580 loader->refcount += 1;
1584 * Decrements the reference count of the loader and finalizes the
1585 * loader when the count reaches zero.
1587 * @param loader the loader.
1590 _dbus_message_loader_unref (DBusMessageLoader *loader)
1592 loader->refcount -= 1;
1593 if (loader->refcount == 0)
1595 _dbus_list_foreach (&loader->messages,
1596 (DBusForeachFunction) dbus_message_unref,
1598 _dbus_list_clear (&loader->messages);
1599 _dbus_string_free (&loader->data);
1605 * Gets the buffer to use for reading data from the network. Network
1606 * data is read directly into an allocated buffer, which is then used
1607 * in the DBusMessage, to avoid as many extra memcpy's as possible.
1608 * The buffer must always be returned immediately using
1609 * _dbus_message_loader_return_buffer(), even if no bytes are
1610 * successfully read.
1612 * @todo this function can be a lot more clever. For example
1613 * it can probably always return a buffer size to read exactly
1614 * the body of the next message, thus avoiding any memory wastage
1617 * @param loader the message loader.
1618 * @param buffer the buffer
1621 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
1622 DBusString **buffer)
1624 _dbus_assert (!loader->buffer_outstanding);
1626 *buffer = &loader->data;
1628 loader->buffer_outstanding = TRUE;
1632 * The smallest header size that can occur.
1633 * (It won't be valid)
1635 #define DBUS_MINIMUM_HEADER_SIZE 16
1637 /** Pack four characters as in "abcd" into a uint32 */
1638 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
1639 ((((dbus_uint32_t)a) << 24) | \
1640 (((dbus_uint32_t)b) << 16) | \
1641 (((dbus_uint32_t)c) << 8) | \
1644 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
1645 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
1646 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
1648 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
1649 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
1650 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
1652 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
1653 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
1654 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
1656 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
1657 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
1658 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
1661 /* FIXME should be using DBusString for the stuff we demarshal. char*
1662 * evil. Also, out of memory handling here seems suboptimal.
1663 * Should probably report it as a distinct error from "corrupt message,"
1664 * so we can postpone parsing this message. Also, we aren't
1665 * checking for demarshal failure everywhere.
1668 decode_header_data (DBusString *data,
1671 HeaderField fields[FIELD_LAST])
1677 if (header_len < 16)
1681 while (i < FIELD_LAST)
1683 fields[i].offset = -1;
1687 fields[FIELD_HEADER_LENGTH].offset = 4;
1688 fields[FIELD_BODY_LENGTH].offset = 8;
1689 fields[FIELD_CLIENT_SERIAL].offset = 12;
1691 /* Now handle the fields */
1693 while (pos < header_len)
1695 pos = _DBUS_ALIGN_VALUE (pos, 4);
1697 if ((pos + 4) > header_len)
1700 _dbus_string_get_const_data_len (data, &field, pos, 4);
1703 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
1705 /* I believe FROM_BE is right, but if not we'll find out
1708 switch (DBUS_UINT32_FROM_BE (*(int*)field))
1710 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
1711 if (fields[FIELD_SERVICE].offset >= 0)
1713 _dbus_verbose ("%s field provided twice\n",
1714 DBUS_HEADER_FIELD_SERVICE);
1718 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1719 _dbus_verbose ("Found service name at offset %d\n",
1720 fields[FIELD_SERVICE].offset);
1723 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
1724 if (fields[FIELD_NAME].offset >= 0)
1726 _dbus_verbose ("%s field provided twice\n",
1727 DBUS_HEADER_FIELD_NAME);
1731 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1733 _dbus_verbose ("Found message name at offset %d\n",
1734 fields[FIELD_NAME].offset);
1736 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
1737 if (fields[FIELD_SENDER].offset >= 0)
1739 _dbus_verbose ("%s field provided twice\n",
1740 DBUS_HEADER_FIELD_SENDER);
1744 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1746 _dbus_verbose ("Found sender name at offset %d\n",
1747 fields[FIELD_NAME].offset);
1750 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
1751 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
1753 _dbus_verbose ("%s field provided twice\n",
1754 DBUS_HEADER_FIELD_REPLY);
1758 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
1760 _dbus_verbose ("Found reply serial at offset %d\n",
1761 fields[FIELD_REPLY_SERIAL].offset);
1765 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c\n",
1766 field[0], field[1], field[2], field[3]);
1769 /* This function has to validate the fields */
1770 if (!_dbus_marshal_get_field_end_pos (data, byte_order, pos, &new_pos))
1773 if (new_pos > header_len)
1783 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
1784 * indicating to the loader how many bytes of the buffer were filled
1785 * in. This function must always be called, even if no bytes were
1786 * successfully read.
1788 * @param loader the loader.
1789 * @param buffer the buffer.
1790 * @param bytes_read number of bytes that were read into the buffer.
1793 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
1797 _dbus_assert (loader->buffer_outstanding);
1798 _dbus_assert (buffer == &loader->data);
1800 loader->buffer_outstanding = FALSE;
1802 if (loader->corrupted)
1805 while (_dbus_string_get_length (&loader->data) >= 16)
1807 DBusMessage *message;
1808 const char *header_data;
1809 int byte_order, header_len, body_len;
1811 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
1813 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
1815 byte_order = header_data[0];
1817 if (byte_order != DBUS_LITTLE_ENDIAN &&
1818 byte_order != DBUS_BIG_ENDIAN)
1820 _dbus_verbose ("Message with bad byte order '%c' received\n",
1822 loader->corrupted = TRUE;
1826 header_len = _dbus_unpack_int32 (byte_order, header_data + 4);
1827 body_len = _dbus_unpack_int32 (byte_order, header_data + 8);
1829 if (header_len + body_len > loader->max_message_size)
1831 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
1832 header_len, body_len, loader->max_message_size);
1833 loader->corrupted = TRUE;
1837 if (_dbus_string_get_length (&loader->data) >= header_len + body_len)
1839 HeaderField fields[FIELD_LAST];
1842 /* FIXME right now if this doesn't have enough memory, the
1843 * loader becomes corrupted. Instead we should just not
1844 * parse this message for now.
1846 if (!decode_header_data (&loader->data, header_len, byte_order,
1849 loader->corrupted = TRUE;
1853 message = dbus_message_new_empty_header ();
1854 if (message == NULL)
1855 break; /* ugh, postpone this I guess. */
1857 /* Copy in the offsets we found */
1859 while (i < FIELD_LAST)
1861 message->header_fields[i] = fields[i];
1865 if (!_dbus_list_append (&loader->messages, message))
1867 dbus_message_unref (message);
1871 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
1872 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
1874 _dbus_assert (_dbus_string_get_length (&loader->data) >=
1875 (header_len + body_len));
1877 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
1879 _dbus_list_remove_last (&loader->messages, message);
1880 dbus_message_unref (message);
1884 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
1888 /* put the header back, we'll try again later */
1889 result = _dbus_string_copy_len (&message->header, 0, header_len,
1891 _dbus_assert (result); /* because DBusString never reallocs smaller */
1893 _dbus_list_remove_last (&loader->messages, message);
1894 dbus_message_unref (message);
1898 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
1899 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
1901 _dbus_verbose ("Loaded message %p\n", message);
1909 * Pops a loaded message (passing ownership of the message
1910 * to the caller). Returns #NULL if no messages have been
1913 * @param loader the loader.
1914 * @returns the next message, or #NULL if none.
1917 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
1919 return _dbus_list_pop_first (&loader->messages);
1924 * Checks whether the loader is confused due to bad data.
1925 * If messages are received that are invalid, the
1926 * loader gets confused and gives up permanently.
1927 * This state is called "corrupted."
1929 * @param loader the loader
1930 * @returns #TRUE if the loader is hosed.
1933 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
1935 return loader->corrupted;
1939 * Sets the maximum size message we allow.
1941 * @param loader the loader
1942 * @param size the max message size in bytes
1945 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
1948 loader->max_message_size = size;
1952 * Gets the maximum allowed message size in bytes.
1954 * @param loader the loader
1955 * @returns max size in bytes
1958 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
1960 return loader->max_message_size;
1964 #ifdef DBUS_BUILD_TESTS
1965 #include "dbus-test.h"
1969 message_iter_test (DBusMessage *message)
1971 DBusMessageIter *iter;
1974 iter = dbus_message_get_fields_iter (message);
1977 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_STRING)
1978 _dbus_assert_not_reached ("Field type isn't string");
1980 str = dbus_message_iter_get_string (iter);
1981 if (strcmp (str, "Test string") != 0)
1982 _dbus_assert_not_reached ("Strings differ");
1985 if (!dbus_message_iter_next (iter))
1986 _dbus_assert_not_reached ("Reached end of fields");
1988 /* Signed integer tests */
1989 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_INT32)
1990 _dbus_assert_not_reached ("Field type isn't int32");
1992 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
1993 _dbus_assert_not_reached ("Signed integers differ");
1995 if (!dbus_message_iter_next (iter))
1996 _dbus_assert_not_reached ("Reached end of fields");
1998 /* Unsigned integer tests */
1999 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_UINT32)
2000 _dbus_assert_not_reached ("Field type isn't int32");
2002 if (dbus_message_iter_get_int32 (iter) != 0xedd1e)
2003 _dbus_assert_not_reached ("Unsigned integers differ");
2005 if (!dbus_message_iter_next (iter))
2006 _dbus_assert_not_reached ("Reached end of fields");
2009 if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_DOUBLE)
2010 _dbus_assert_not_reached ("Field type isn't double");
2012 if (dbus_message_iter_get_double (iter) != 3.14159)
2013 _dbus_assert_not_reached ("Doubles differ");
2015 if (dbus_message_iter_next (iter))
2016 _dbus_assert_not_reached ("Didn't reach end of fields");
2018 dbus_message_iter_unref (iter);
2022 * @ingroup DBusMessageInternals
2023 * Unit test for DBusMessage.
2025 * @returns #TRUE on success.
2028 _dbus_message_test (void)
2030 DBusMessage *message;
2031 DBusMessageLoader *loader;
2034 dbus_int32_t our_int;
2038 /* Test the vararg functions */
2039 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
2040 _dbus_message_set_client_serial (message, 1);
2041 dbus_message_append_fields (message,
2042 DBUS_TYPE_INT32, -0x12345678,
2043 DBUS_TYPE_STRING, "Test string",
2044 DBUS_TYPE_DOUBLE, 3.14159,
2046 _dbus_verbose_bytes_of_string (&message->header, 0,
2047 _dbus_string_get_length (&message->header));
2048 _dbus_verbose_bytes_of_string (&message->body, 0,
2049 _dbus_string_get_length (&message->body));
2051 if (dbus_message_get_fields (message,
2052 DBUS_TYPE_INT32, &our_int,
2053 DBUS_TYPE_STRING, &our_str,
2054 DBUS_TYPE_DOUBLE, &our_double,
2055 0) != DBUS_RESULT_SUCCESS)
2056 _dbus_assert_not_reached ("Could not get fields");
2058 if (our_int != -0x12345678)
2059 _dbus_assert_not_reached ("integers differ!");
2061 if (our_double != 3.14159)
2062 _dbus_assert_not_reached ("doubles differ!");
2064 if (strcmp (our_str, "Test string") != 0)
2065 _dbus_assert_not_reached ("strings differ!");
2067 dbus_free (our_str);
2068 dbus_message_unref (message);
2070 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
2071 _dbus_message_set_client_serial (message, 1);
2072 _dbus_message_set_reply_serial (message, 0x12345678);
2074 dbus_message_append_string (message, "Test string");
2075 dbus_message_append_int32 (message, -0x12345678);
2076 dbus_message_append_uint32 (message, 0xedd1e);
2077 dbus_message_append_double (message, 3.14159);
2079 message_iter_test (message);
2081 /* Message loader test */
2082 _dbus_message_lock (message);
2083 loader = _dbus_message_loader_new ();
2085 /* Write the header data one byte at a time */
2086 _dbus_string_get_const_data (&message->header, &data);
2087 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
2091 _dbus_message_loader_get_buffer (loader, &buffer);
2092 _dbus_string_append_byte (buffer, data[i]);
2093 _dbus_message_loader_return_buffer (loader, buffer, 1);
2096 /* Write the body data one byte at a time */
2097 _dbus_string_get_const_data (&message->body, &data);
2098 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
2102 _dbus_message_loader_get_buffer (loader, &buffer);
2103 _dbus_string_append_byte (buffer, data[i]);
2104 _dbus_message_loader_return_buffer (loader, buffer, 1);
2107 dbus_message_unref (message);
2109 /* Now pop back the message */
2110 if (_dbus_message_loader_get_is_corrupted (loader))
2111 _dbus_assert_not_reached ("message loader corrupted");
2113 message = _dbus_message_loader_pop_message (loader);
2115 _dbus_assert_not_reached ("received a NULL message");
2117 if (_dbus_message_get_reply_serial (message) != 0x12345678)
2118 _dbus_assert_not_reached ("reply serial fields differ");
2120 message_iter_test (message);
2122 dbus_message_unref (message);
2123 _dbus_message_loader_unref (loader);
2128 #endif /* DBUS_BUILD_TESTS */