1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include <sys/types.h>
33 #include "gdbusutils.h"
34 #include "gdbusmessage.h"
35 #include "gdbuserror.h"
36 #include "gioenumtypes.h"
37 #include "ginputstream.h"
38 #include "gdatainputstream.h"
39 #include "gmemoryinputstream.h"
40 #include "goutputstream.h"
41 #include "gdataoutputstream.h"
42 #include "gmemoryoutputstream.h"
43 #include "gseekable.h"
47 #include "gunixfdlist.h"
54 * SECTION:gdbusmessage
55 * @short_description: D-Bus Message
58 * A type for representing D-Bus messages that can be sent or received
59 * on a #GDBusConnection.
62 struct _GDBusMessagePrivate
64 GDBusMessageType type;
65 GDBusMessageFlags flags;
66 guchar major_protocol_version;
75 G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT);
78 g_dbus_message_finalize (GObject *object)
80 GDBusMessage *message = G_DBUS_MESSAGE (object);
82 if (message->priv->headers != NULL)
83 g_hash_table_unref (message->priv->headers);
84 if (message->priv->body != NULL)
85 g_variant_unref (message->priv->body);
87 if (message->priv->fd_list != NULL)
88 g_object_unref (message->priv->fd_list);
91 if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL)
92 G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object);
96 g_dbus_message_class_init (GDBusMessageClass *klass)
98 GObjectClass *gobject_class;
100 g_type_class_add_private (klass, sizeof (GDBusMessagePrivate));
102 gobject_class = G_OBJECT_CLASS (klass);
104 gobject_class->finalize = g_dbus_message_finalize;
108 g_dbus_message_init (GDBusMessage *message)
110 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message, G_TYPE_DBUS_MESSAGE, GDBusMessagePrivate);
112 message->priv->headers = g_hash_table_new_full (g_direct_hash,
115 (GDestroyNotify) g_variant_unref);
119 * g_dbus_message_new:
121 * Creates a new empty #GDBusMessage.
123 * Returns: A #GDBusMessage. Free with g_object_unref().
128 g_dbus_message_new (void)
130 return g_object_new (G_TYPE_DBUS_MESSAGE, NULL);
134 * g_dbus_message_new_method_call:
135 * @name: A valid D-Bus name or %NULL.
136 * @path: A valid object path.
137 * @interface_: A valid D-Bus interface name or %NULL.
138 * @method: A valid method name.
140 * Creates a new #GDBusMessage for a method call.
142 * Returns: A #GDBusMessage. Free with g_object_unref().
147 g_dbus_message_new_method_call (const gchar *name,
149 const gchar *interface_,
152 GDBusMessage *message;
154 g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL);
155 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
156 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
157 g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
159 message = g_dbus_message_new ();
160 message->priv->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
163 g_dbus_message_set_destination (message, name);
164 g_dbus_message_set_path (message, path);
165 g_dbus_message_set_member (message, method);
166 if (interface_ != NULL)
167 g_dbus_message_set_interface (message, interface_);
173 * g_dbus_message_new_signal:
174 * @path: A valid object path.
175 * @interface_: A valid D-Bus interface name or %NULL.
176 * @signal: A valid signal name.
178 * Creates a new #GDBusMessage for a signal emission.
180 * Returns: A #GDBusMessage. Free with g_object_unref().
185 g_dbus_message_new_signal (const gchar *path,
186 const gchar *interface_,
189 GDBusMessage *message;
191 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
192 g_return_val_if_fail (g_dbus_is_member_name (signal), NULL);
193 g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
195 message = g_dbus_message_new ();
196 message->priv->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
197 message->priv->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
199 g_dbus_message_set_path (message, path);
200 g_dbus_message_set_member (message, signal);
202 if (interface_ != NULL)
203 g_dbus_message_set_interface (message, interface_);
210 * g_dbus_message_new_method_reply:
211 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
212 * create a reply message to.
214 * Creates a new #GDBusMessage that is a reply to @method_call_message.
216 * Returns: A #GDBusMessage. Free with g_object_unref().
221 g_dbus_message_new_method_reply (GDBusMessage *method_call_message)
223 GDBusMessage *message;
226 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
227 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
228 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
230 message = g_dbus_message_new ();
231 message->priv->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
232 message->priv->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
234 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
235 sender = g_dbus_message_get_sender (method_call_message);
237 g_dbus_message_set_destination (message, sender);
243 * g_dbus_message_new_method_error:
244 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
245 * create a reply message to.
246 * @error_name: A valid D-Bus error name.
247 * @error_message_format: The D-Bus error message in a printf() format.
248 * @...: Arguments for @error_message_format.
250 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
252 * Returns: A #GDBusMessage. Free with g_object_unref().
257 g_dbus_message_new_method_error (GDBusMessage *method_call_message,
258 const gchar *error_name,
259 const gchar *error_message_format,
265 va_start (var_args, error_message_format);
266 ret = g_dbus_message_new_method_error_valist (method_call_message,
268 error_message_format,
276 * g_dbus_message_new_method_error_literal:
277 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
278 * create a reply message to.
279 * @error_name: A valid D-Bus error name.
280 * @error_message: The D-Bus error message.
282 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
284 * Returns: A #GDBusMessage. Free with g_object_unref().
289 g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message,
290 const gchar *error_name,
291 const gchar *error_message)
293 GDBusMessage *message;
296 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
297 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
298 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
299 g_return_val_if_fail (g_dbus_is_name (error_name), NULL);
300 g_return_val_if_fail (error_message != NULL, NULL);
302 message = g_dbus_message_new ();
303 message->priv->type = G_DBUS_MESSAGE_TYPE_ERROR;
304 message->priv->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
306 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
307 g_dbus_message_set_error_name (message, error_name);
308 g_dbus_message_set_body (message, g_variant_new ("(s)", error_message));
310 sender = g_dbus_message_get_sender (method_call_message);
312 g_dbus_message_set_destination (message, sender);
318 * g_dbus_message_new_method_error_valist:
319 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
320 * create a reply message to.
321 * @error_name: A valid D-Bus error name.
322 * @error_message_format: The D-Bus error message in a printf() format.
323 * @var_args: Arguments for @error_message_format.
325 * Like g_dbus_message_new_method_error() but intended for language bindings.
327 * Returns: A #GDBusMessage. Free with g_object_unref().
332 g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message,
333 const gchar *error_name,
334 const gchar *error_message_format,
338 gchar *error_message;
339 error_message = g_strdup_vprintf (error_message_format, var_args);
340 ret = g_dbus_message_new_method_error_literal (method_call_message,
343 g_free (error_message);
347 /* ---------------------------------------------------------------------------------------------------- */
349 /* TODO: need GI annotations to specify that any guchar value goes for the type */
352 * g_dbus_message_get_message_type:
353 * @message: A #GDBusMessage.
355 * Gets the type of @message.
357 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
362 g_dbus_message_get_message_type (GDBusMessage *message)
364 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID);
365 return message->priv->type;
369 * g_dbus_message_set_message_type:
370 * @message: A #GDBusMessage.
371 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
373 * Sets @message to be of @type.
378 g_dbus_message_set_message_type (GDBusMessage *message,
379 GDBusMessageType type)
381 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
382 g_return_if_fail (type >=0 && type < 256);
383 message->priv->type = type;
386 /* ---------------------------------------------------------------------------------------------------- */
388 /* TODO: need GI annotations to specify that any guchar value goes for flags */
391 * g_dbus_message_get_flags:
392 * @message: A #GDBusMessage.
394 * Gets the flags for @message.
396 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
401 g_dbus_message_get_flags (GDBusMessage *message)
403 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE);
404 return message->priv->flags;
408 * g_dbus_message_set_flags:
409 * @message: A #GDBusMessage.
410 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
411 * enumeration bitwise ORed together).
413 * Sets the flags to set on @message.
418 g_dbus_message_set_flags (GDBusMessage *message,
419 GDBusMessageFlags flags)
421 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
422 g_return_if_fail (flags >=0 && flags < 256);
423 message->priv->flags = flags;
426 /* ---------------------------------------------------------------------------------------------------- */
429 * g_dbus_message_get_serial:
430 * @message: A #GDBusMessage.
432 * Gets the serial for @message.
434 * Returns: A #guint32.
439 g_dbus_message_get_serial (GDBusMessage *message)
441 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
442 return message->priv->serial;
446 * g_dbus_message_set_serial:
447 * @message: A #GDBusMessage.
448 * @serial: A #guint32.
450 * Sets the serial for @message.
455 g_dbus_message_set_serial (GDBusMessage *message,
458 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
459 message->priv->serial = serial;
462 /* ---------------------------------------------------------------------------------------------------- */
464 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
467 * g_dbus_message_get_header:
468 * @message: A #GDBusMessage.
469 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
471 * Gets a header field on @message.
473 * Returns: A #GVariant with the value if the header was found, %NULL
474 * otherwise. Do not free, it is owned by @message.
479 g_dbus_message_get_header (GDBusMessage *message,
480 GDBusMessageHeaderField header_field)
482 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
483 g_return_val_if_fail (header_field >=0 && header_field < 256, NULL);
484 return g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
488 * g_dbus_message_set_header:
489 * @message: A #GDBusMessage.
490 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
491 * @value: A #GVariant to set the header field or %NULL to clear the header field.
493 * Sets a header field on @message.
495 * If @value is floating, @message assumes ownership of @value.
500 g_dbus_message_set_header (GDBusMessage *message,
501 GDBusMessageHeaderField header_field,
504 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
505 g_return_if_fail (header_field >=0 && header_field < 256);
508 g_hash_table_remove (message->priv->headers, GUINT_TO_POINTER (header_field));
512 g_hash_table_insert (message->priv->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
517 * g_dbus_message_get_header_fields:
518 * @message: A #GDBusMessage.
520 * Gets an array of all header fields on @message that are set.
522 * Returns: An array of header fields terminated by
523 * %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element is a
524 * #guchar. Free with g_free().
529 g_dbus_message_get_header_fields (GDBusMessage *message)
537 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
539 keys = g_hash_table_get_keys (message->priv->headers);
540 num_keys = g_list_length (keys);
541 ret = g_new (guchar, num_keys + 1);
542 for (l = keys, n = 0; l != NULL; l = l->next, n++)
543 ret[n] = GPOINTER_TO_UINT (l->data);
544 g_assert (n == num_keys);
545 ret[n] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID;
551 /* ---------------------------------------------------------------------------------------------------- */
554 * g_dbus_message_get_body:
555 * @message: A #GDBusMessage.
557 * Gets the body of a message.
559 * Returns: A #GVariant or %NULL if the body is empty. Do not free, it is owned by @message.
564 g_dbus_message_get_body (GDBusMessage *message)
566 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
567 return message->priv->body;
571 * g_dbus_message_set_body:
572 * @message: A #GDBusMessage.
573 * @body: Either %NULL or a #GVariant that is a tuple.
575 * Sets the body @message. As a side-effect the
576 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
577 * type string of @body (or cleared if @body is %NULL).
579 * If @body is floating, @message assumes ownership of @body.
584 g_dbus_message_set_body (GDBusMessage *message,
587 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
588 g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE));
590 if (message->priv->body != NULL)
591 g_variant_unref (message->priv->body);
594 message->priv->body = NULL;
595 g_dbus_message_set_signature (message, NULL);
599 const gchar *type_string;
600 gsize type_string_len;
603 message->priv->body = g_variant_ref_sink (body);
605 type_string = g_variant_get_type_string (body);
606 type_string_len = strlen (type_string);
607 g_assert (type_string_len >= 2);
608 signature = g_strndup (type_string + 1, type_string_len - 2);
609 g_dbus_message_set_signature (message, signature);
614 /* ---------------------------------------------------------------------------------------------------- */
618 * g_dbus_message_get_unix_fd_list:
619 * @message: A #GDBusMessage.
621 * Gets the UNIX file descriptors associated with @message, if any.
623 * This method is only available on UNIX.
625 * Returns: A #GUnixFDList or %NULL if no file descriptors are
626 * associated. Do not free, this object is owned by @message.
631 g_dbus_message_get_unix_fd_list (GDBusMessage *message)
633 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
634 return message->priv->fd_list;
638 * g_dbus_message_set_unix_fd_list:
639 * @message: A #GDBusMessage.
640 * @fd_list: A #GUnixFDList or %NULL.
642 * Sets the UNIX file descriptors associated with @message. As a
643 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
644 * field is set to the number of fds in @fd_list (or cleared if
645 * @fd_list is %NULL).
647 * This method is only available on UNIX.
652 g_dbus_message_set_unix_fd_list (GDBusMessage *message,
653 GUnixFDList *fd_list)
655 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
656 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
657 if (message->priv->fd_list != NULL)
658 g_object_unref (message->priv->fd_list);
661 message->priv->fd_list = g_object_ref (fd_list);
662 g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list));
666 message->priv->fd_list = NULL;
667 g_dbus_message_set_num_unix_fds (message, 0);
672 /* ---------------------------------------------------------------------------------------------------- */
675 ensure_input_padding (GMemoryInputStream *mis,
682 offset = g_seekable_tell (G_SEEKABLE (mis));
683 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
685 /*g_debug ("ensure_input_padding(%d) pushed offset 0x%04x to 0x%04x", (gint) padding_size, (gint) offset, (gint) wanted_offset);*/
687 return g_seekable_seek (G_SEEKABLE (mis), wanted_offset, G_SEEK_SET, NULL, error);
691 read_string (GMemoryInputStream *mis,
692 GDataInputStream *dis,
702 s = g_string_new (NULL);
705 while (remaining > 0)
710 to_read = MIN (remaining, sizeof (buf));
711 num_read = g_input_stream_read (G_INPUT_STREAM (mis),
722 G_IO_ERROR_INVALID_ARGUMENT,
723 _("Wanted to read %" G_GSIZE_FORMAT " bytes but got EOF"),
728 remaining -= num_read;
729 g_string_append_len (s, buf, num_read);
733 nul = g_data_input_stream_read_byte (dis, NULL, &local_error);
734 if (local_error != NULL)
736 g_propagate_error (error, local_error);
743 G_IO_ERROR_INVALID_ARGUMENT,
744 _("Expected NUL byte after the string `%s' but found `%c' (%d)"),
749 return g_string_free (s, FALSE);
752 g_string_free (s, TRUE);
756 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
758 parse_value_from_blob (GMemoryInputStream *mis,
759 GDataInputStream *dis,
760 const GVariantType *type,
767 /*g_debug ("Reading type %s from offset 0x%04x", g_variant_type_dup_string (type), (gint) g_seekable_tell (G_SEEKABLE (mis)));*/
772 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
774 if (!ensure_input_padding (mis, 4, &local_error))
779 v = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
780 if (local_error != NULL)
782 ret = g_variant_new_boolean (v);
785 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
790 v = g_data_input_stream_read_byte (dis, NULL, &local_error);
791 if (local_error != NULL)
793 ret = g_variant_new_byte (v);
796 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
798 if (!ensure_input_padding (mis, 2, &local_error))
803 v = g_data_input_stream_read_int16 (dis, NULL, &local_error);
804 if (local_error != NULL)
806 ret = g_variant_new_int16 (v);
809 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
811 if (!ensure_input_padding (mis, 2, &local_error))
816 v = g_data_input_stream_read_uint16 (dis, NULL, &local_error);
817 if (local_error != NULL)
819 ret = g_variant_new_uint16 (v);
822 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
824 if (!ensure_input_padding (mis, 4, &local_error))
829 v = g_data_input_stream_read_int32 (dis, NULL, &local_error);
830 if (local_error != NULL)
832 ret = g_variant_new_int32 (v);
835 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
837 if (!ensure_input_padding (mis, 4, &local_error))
842 v = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
843 if (local_error != NULL)
845 ret = g_variant_new_uint32 (v);
848 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
850 if (!ensure_input_padding (mis, 8, &local_error))
855 v = g_data_input_stream_read_int64 (dis, NULL, &local_error);
856 if (local_error != NULL)
858 ret = g_variant_new_int64 (v);
861 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
863 if (!ensure_input_padding (mis, 8, &local_error))
868 v = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
869 if (local_error != NULL)
871 ret = g_variant_new_uint64 (v);
874 else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
876 if (!ensure_input_padding (mis, 8, &local_error))
882 v = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
883 if (local_error != NULL)
886 encoded = (gdouble *) &v;
887 ret = g_variant_new_double (*encoded);
890 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
892 if (!ensure_input_padding (mis, 4, &local_error))
898 len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
899 if (local_error != NULL)
901 v = read_string (mis, dis, (gsize) len, &local_error);
904 ret = g_variant_new_string (v);
908 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
910 if (!ensure_input_padding (mis, 4, &local_error))
916 len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
917 if (local_error != NULL)
919 v = read_string (mis, dis, (gsize) len, &local_error);
922 if (!g_variant_is_object_path (v))
924 g_set_error (&local_error,
926 G_IO_ERROR_INVALID_ARGUMENT,
927 _("Parsed value `%s' is not a valid D-Bus object path"),
932 ret = g_variant_new_object_path (v);
936 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
942 len = g_data_input_stream_read_byte (dis, NULL, &local_error);
943 if (local_error != NULL)
945 v = read_string (mis, dis, (gsize) len, &local_error);
948 if (!g_variant_is_signature (v))
950 g_set_error (&local_error,
952 G_IO_ERROR_INVALID_ARGUMENT,
953 _("Parsed value `%s' is not a valid D-Bus signature"),
958 ret = g_variant_new_signature (v);
962 else if (g_variant_type_is_array (type))
967 const GVariantType *element_type;
968 GVariantBuilder builder;
970 if (!ensure_input_padding (mis, 4, &local_error))
972 array_len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
974 if (array_len > (2<<26))
976 g_set_error (&local_error,
978 G_IO_ERROR_INVALID_ARGUMENT,
979 _("Encountered array of length %" G_GUINT32_FORMAT " bytes. Maximum length is 2<<26 bytes."),
984 g_variant_builder_init (&builder, type);
985 element_type = g_variant_type_element (type);
990 item = parse_value_from_blob (mis,
995 g_assert (item == NULL);
999 /* TODO: optimize array of primitive types */
1000 offset = g_seekable_tell (G_SEEKABLE (mis));
1001 target = offset + array_len;
1002 while (offset < target)
1005 item = parse_value_from_blob (mis,
1012 g_variant_builder_clear (&builder);
1015 g_variant_builder_add_value (&builder, item);
1016 offset = g_seekable_tell (G_SEEKABLE (mis));
1022 ret = g_variant_builder_end (&builder);
1026 g_variant_builder_clear (&builder);
1029 else if (g_variant_type_is_dict_entry (type))
1031 const GVariantType *key_type;
1032 const GVariantType *value_type;
1036 if (!ensure_input_padding (mis, 8, &local_error))
1041 key_type = g_variant_type_key (type);
1042 key = parse_value_from_blob (mis,
1050 value_type = g_variant_type_value (type);
1051 value = parse_value_from_blob (mis,
1058 g_variant_unref (key);
1061 ret = g_variant_new_dict_entry (key, value);
1064 else if (g_variant_type_is_tuple (type))
1066 if (!ensure_input_padding (mis, 8, &local_error))
1071 const GVariantType *element_type;
1072 GVariantBuilder builder;
1074 g_variant_builder_init (&builder, type);
1075 element_type = g_variant_type_first (type);
1076 while (element_type != NULL)
1079 item = parse_value_from_blob (mis,
1086 g_variant_builder_clear (&builder);
1089 g_variant_builder_add_value (&builder, item);
1091 element_type = g_variant_type_next (element_type);
1093 ret = g_variant_builder_end (&builder);
1096 else if (g_variant_type_is_variant (type))
1102 GVariantType *variant_type;
1105 siglen = g_data_input_stream_read_byte (dis, NULL, &local_error);
1106 if (local_error != NULL)
1108 sig = read_string (mis, dis, (gsize) siglen, &local_error);
1111 if (!g_variant_is_signature (sig))
1113 g_set_error (&local_error,
1115 G_IO_ERROR_INVALID_ARGUMENT,
1116 _("Parsed value `%s' for variant is not a valid D-Bus signature"),
1121 variant_type = g_variant_type_new (sig);
1123 value = parse_value_from_blob (mis,
1128 g_variant_type_free (variant_type);
1131 ret = g_variant_new_variant (value);
1137 s = g_variant_type_dup_string (type);
1138 g_set_error (&local_error,
1140 G_IO_ERROR_INVALID_ARGUMENT,
1141 _("Error deserializing GVariant with type-string `%s' from the D-Bus wire format"),
1147 g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
1151 g_propagate_error (error, local_error);
1155 /* ---------------------------------------------------------------------------------------------------- */
1157 /* message_header must be at least 16 bytes */
1160 * g_dbus_message_bytes_needed:
1161 * @blob: A blob represent a binary D-Bus message.
1162 * @blob_len: The length of @blob (must be at least 16).
1163 * @error: Return location for error or %NULL.
1165 * Utility function to calculate how many bytes are needed to
1166 * completely deserialize the D-Bus message stored at @blob.
1168 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1169 * @blob contains invalid data or not enough data is available to
1170 * determine the size).
1175 g_dbus_message_bytes_needed (guchar *blob,
1183 g_return_val_if_fail (blob != NULL, -1);
1184 g_return_val_if_fail (error == NULL || *error == NULL, -1);
1185 g_return_val_if_fail (blob_len >= 16, -1);
1189 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1190 ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]);
1191 /* round up so it's a multiple of 8 */
1192 ret = 8 * ((ret + 7)/8);
1193 /* finally add the body size */
1194 ret += GUINT32_FROM_LE (((guint32 *) blob)[1]);
1196 else if (blob[0] == 'B')
1198 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1199 ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]);
1200 /* round up so it's a multiple of 8 */
1201 ret = 8 * ((ret + 7)/8);
1202 /* finally add the body size */
1203 ret += GUINT32_FROM_BE (((guint32 *) blob)[1]);
1209 G_IO_ERROR_INVALID_ARGUMENT,
1210 "Unable to determine message blob length - given blob is malformed");
1217 G_IO_ERROR_INVALID_ARGUMENT,
1218 "Blob indicates that message exceeds maximum message length (128MiB)");
1225 /* ---------------------------------------------------------------------------------------------------- */
1228 * g_dbus_message_new_from_blob:
1229 * @blob: A blob represent a binary D-Bus message.
1230 * @blob_len: The length of @blob.
1231 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1232 * @error: Return location for error or %NULL.
1234 * Creates a new #GDBusMessage from the data stored at @blob.
1236 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
1242 g_dbus_message_new_from_blob (guchar *blob,
1244 GDBusCapabilityFlags capabilities,
1248 GMemoryInputStream *mis;
1249 GDataInputStream *dis;
1250 GDBusMessage *message;
1252 guchar major_protocol_version;
1253 GDataStreamByteOrder byte_order;
1254 guint32 message_body_len;
1258 GVariant *signature;
1260 /* TODO: check against @capabilities */
1264 g_return_val_if_fail (blob != NULL, NULL);
1265 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1266 g_return_val_if_fail (blob_len >= 12, NULL);
1268 message = g_dbus_message_new ();
1270 mis = G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (blob, blob_len, NULL));
1271 dis = g_data_input_stream_new (G_INPUT_STREAM (mis));
1273 endianness = g_data_input_stream_read_byte (dis, NULL, NULL);
1277 byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
1280 byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
1285 G_IO_ERROR_INVALID_ARGUMENT,
1286 _("Invalid endianness value. Expected 'l' or 'B' but found '%c' (%d)"),
1287 endianness, endianness);
1290 g_data_input_stream_set_byte_order (dis, byte_order);
1292 message->priv->type = g_data_input_stream_read_byte (dis, NULL, NULL);
1293 message->priv->flags = g_data_input_stream_read_byte (dis, NULL, NULL);
1294 major_protocol_version = g_data_input_stream_read_byte (dis, NULL, NULL);
1295 if (major_protocol_version != 1)
1299 G_IO_ERROR_INVALID_ARGUMENT,
1300 _("Invalid major protocol version. Expected 1 but found %d"),
1301 major_protocol_version);
1304 message_body_len = g_data_input_stream_read_uint32 (dis, NULL, NULL);
1305 message->priv->serial = g_data_input_stream_read_uint32 (dis, NULL, NULL);
1307 headers = parse_value_from_blob (mis,
1309 G_VARIANT_TYPE ("a{yv}"),
1312 if (headers == NULL)
1314 g_variant_ref_sink (headers);
1315 g_variant_iter_init (&iter, headers);
1316 while ((item = g_variant_iter_next_value (&iter)))
1318 guchar header_field;
1320 g_variant_get (item,
1324 g_dbus_message_set_header (message, header_field, value);
1326 g_variant_unref (headers);
1328 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
1329 if (signature != NULL)
1331 const gchar *signature_str;
1332 gsize signature_str_len;
1334 signature_str = g_variant_get_string (signature, &signature_str_len);
1336 /* signature but no body */
1337 if (message_body_len == 0 && signature_str_len > 0)
1341 G_IO_ERROR_INVALID_ARGUMENT,
1342 _("Signature header with signature `%s' found but message body is empty"),
1346 else if (signature_str_len > 0)
1348 GVariantType *variant_type;
1349 gchar *tupled_signature_str;
1351 if (!g_variant_is_signature (signature_str))
1355 G_IO_ERROR_INVALID_ARGUMENT,
1356 _("Parsed value `%s' is not a valid D-Bus signature (for body)"),
1360 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
1361 variant_type = g_variant_type_new (tupled_signature_str);
1362 g_free (tupled_signature_str);
1363 message->priv->body = parse_value_from_blob (mis,
1368 if (message->priv->body == NULL)
1370 g_variant_type_free (variant_type);
1373 g_variant_ref_sink (message->priv->body);
1374 g_variant_type_free (variant_type);
1379 /* no signature, this is only OK if the body is empty */
1380 if (message_body_len != 0)
1384 G_IO_ERROR_INVALID_ARGUMENT,
1385 _("No signature header in message but the message body is %" G_GUINT32_FORMAT " bytes"),
1395 g_object_unref (dis);
1396 g_object_unref (mis);
1404 if (message != NULL)
1405 g_object_unref (message);
1410 /* ---------------------------------------------------------------------------------------------------- */
1413 ensure_output_padding (GMemoryOutputStream *mos,
1414 GDataOutputStream *dos,
1418 gsize wanted_offset;
1419 gsize padding_needed;
1422 offset = g_memory_output_stream_get_data_size (mos);
1423 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
1424 padding_needed = wanted_offset - offset;
1426 for (n = 0; n < padding_needed; n++)
1427 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1429 return padding_needed;
1432 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
1434 append_value_to_blob (GVariant *value,
1435 const GVariantType *type,
1436 GMemoryOutputStream *mos,
1437 GDataOutputStream *dos,
1438 gsize *out_padding_added,
1441 gsize padding_added;
1445 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
1447 padding_added = ensure_output_padding (mos, dos, 4);
1450 gboolean v = g_variant_get_boolean (value);
1451 g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
1454 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
1458 guint8 v = g_variant_get_byte (value);
1459 g_data_output_stream_put_byte (dos, v, NULL, NULL);
1462 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
1464 padding_added = ensure_output_padding (mos, dos, 2);
1467 gint16 v = g_variant_get_int16 (value);
1468 g_data_output_stream_put_int16 (dos, v, NULL, NULL);
1471 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
1473 padding_added = ensure_output_padding (mos, dos, 2);
1476 guint16 v = g_variant_get_uint16 (value);
1477 g_data_output_stream_put_uint16 (dos, v, NULL, NULL);
1480 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
1482 padding_added = ensure_output_padding (mos, dos, 4);
1485 gint32 v = g_variant_get_int32 (value);
1486 g_data_output_stream_put_int32 (dos, v, NULL, NULL);
1489 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
1491 padding_added = ensure_output_padding (mos, dos, 4);
1494 guint32 v = g_variant_get_uint32 (value);
1495 g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
1498 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
1500 padding_added = ensure_output_padding (mos, dos, 8);
1503 gint64 v = g_variant_get_int64 (value);
1504 g_data_output_stream_put_int64 (dos, v, NULL, NULL);
1507 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
1509 padding_added = ensure_output_padding (mos, dos, 8);
1512 guint64 v = g_variant_get_uint64 (value);
1513 g_data_output_stream_put_uint64 (dos, v, NULL, NULL);
1516 else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
1518 padding_added = ensure_output_padding (mos, dos, 8);
1522 gdouble v = g_variant_get_double (value);
1524 encoded = (guint64 *) &v;
1525 g_data_output_stream_put_uint64 (dos, *encoded, NULL, NULL);
1528 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
1530 padding_added = ensure_output_padding (mos, dos, 4);
1534 const gchar *v = g_variant_get_string (value, &len);
1535 g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
1536 g_data_output_stream_put_string (dos, v, NULL, NULL);
1537 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1540 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
1542 padding_added = ensure_output_padding (mos, dos, 4);
1545 /* TODO: validate object path */
1547 const gchar *v = g_variant_get_string (value, &len);
1548 g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
1549 g_data_output_stream_put_string (dos, v, NULL, NULL);
1550 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1553 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
1557 /* TODO: validate signature (including max len being 255) */
1559 const gchar *v = g_variant_get_string (value, &len);
1560 g_data_output_stream_put_byte (dos, len, NULL, NULL);
1561 g_data_output_stream_put_string (dos, v, NULL, NULL);
1562 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1565 else if (g_variant_type_is_array (type))
1569 goffset array_len_offset;
1570 goffset array_payload_begin_offset;
1574 padding_added = ensure_output_padding (mos, dos, 4);
1577 /* array length - will be filled in later */
1578 array_len_offset = g_memory_output_stream_get_data_size (mos);
1579 g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
1581 /* From the D-Bus spec:
1583 * "A UINT32 giving the length of the array data in bytes,
1584 * followed by alignment padding to the alignment boundary of
1585 * the array element type, followed by each array element. The
1586 * array length is from the end of the alignment padding to
1587 * the end of the last element, i.e. it does not include the
1588 * padding after the length, or any padding after the last
1591 * Thus, we need to count how much padding the first element
1592 * contributes and subtract that from the array length.
1594 array_payload_begin_offset = g_memory_output_stream_get_data_size (mos);
1596 if (g_variant_n_children (value) == 0)
1598 gsize padding_added_for_item;
1599 if (!append_value_to_blob (NULL,
1600 g_variant_type_element (type),
1603 &padding_added_for_item,
1606 array_payload_begin_offset += padding_added_for_item;
1612 g_variant_iter_init (&iter, value);
1613 while ((item = g_variant_iter_next_value (&iter)))
1615 gsize padding_added_for_item;
1616 if (!append_value_to_blob (item,
1617 g_variant_get_type (item),
1620 &padding_added_for_item,
1625 array_payload_begin_offset += padding_added_for_item;
1631 cur_offset = g_memory_output_stream_get_data_size (mos);
1633 array_len = cur_offset - array_payload_begin_offset;
1635 if (!g_seekable_seek (G_SEEKABLE (mos), array_len_offset, G_SEEK_SET, NULL, error))
1638 g_data_output_stream_put_uint32 (dos, array_len, NULL, NULL);
1640 if (!g_seekable_seek (G_SEEKABLE (mos), cur_offset, G_SEEK_SET, NULL, error))
1644 else if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
1646 padding_added = ensure_output_padding (mos, dos, 8);
1651 g_variant_iter_init (&iter, value);
1652 while ((item = g_variant_iter_next_value (&iter)))
1654 if (!append_value_to_blob (item,
1655 g_variant_get_type (item),
1664 else if (g_variant_type_is_variant (type))
1669 const gchar *signature;
1670 child = g_variant_get_child_value (value, 0);
1671 signature = g_variant_get_type_string (child);
1672 /* TODO: validate signature (including max len being 255) */
1673 g_data_output_stream_put_byte (dos, strlen (signature), NULL, NULL);
1674 g_data_output_stream_put_string (dos, signature, NULL, NULL);
1675 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1676 if (!append_value_to_blob (child,
1677 g_variant_get_type (child),
1683 g_variant_unref (child);
1686 g_variant_unref (child);
1693 G_IO_ERROR_INVALID_ARGUMENT,
1694 _("Error serializing GVariant with type-string `%s' to the D-Bus wire format"),
1695 g_variant_get_type_string (value));
1699 if (out_padding_added != NULL)
1700 *out_padding_added = padding_added;
1709 append_body_to_blob (GVariant *value,
1710 GMemoryOutputStream *mos,
1711 GDataOutputStream *dos,
1720 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
1724 G_IO_ERROR_INVALID_ARGUMENT,
1725 "Expected a tuple for the body of the GDBusMessage.");
1729 g_variant_iter_init (&iter, value);
1730 while ((item = g_variant_iter_next_value (&iter)))
1732 if (!append_value_to_blob (item,
1733 g_variant_get_type (item),
1746 /* ---------------------------------------------------------------------------------------------------- */
1749 * g_dbus_message_to_blob:
1750 * @message: A #GDBusMessage.
1751 * @out_size: Return location for size of generated blob.
1752 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1753 * @error: Return location for error.
1755 * Serializes @message to a blob.
1757 * Returns: A pointer to a valid binary D-Bus message of @out_size bytes
1758 * generated by @message or %NULL if @error is set. Free with g_free().
1763 g_dbus_message_to_blob (GDBusMessage *message,
1765 GDBusCapabilityFlags capabilities,
1768 GMemoryOutputStream *mos;
1769 GDataOutputStream *dos;
1772 GDataStreamByteOrder byte_order;
1773 goffset body_len_offset;
1774 goffset body_start_offset;
1776 GVariant *header_fields;
1777 GVariantBuilder builder;
1778 GHashTableIter hash_iter;
1780 GVariant *header_value;
1781 GVariant *signature;
1782 const gchar *signature_str;
1783 gint num_fds_in_message;
1784 gint num_fds_according_to_header;
1786 /* TODO: check against @capabilities */
1790 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1791 g_return_val_if_fail (out_size != NULL, NULL);
1792 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1794 mos = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0, g_realloc, g_free));
1795 dos = g_data_output_stream_new (G_OUTPUT_STREAM (mos));
1797 /* TODO: detect endianess... */
1798 byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
1799 g_data_output_stream_set_byte_order (dos, byte_order);
1802 g_data_output_stream_put_byte (dos, byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN ? 'l' : 'B', NULL, NULL);
1803 g_data_output_stream_put_byte (dos, message->priv->type, NULL, NULL);
1804 g_data_output_stream_put_byte (dos, message->priv->flags, NULL, NULL);
1805 g_data_output_stream_put_byte (dos, 1, NULL, NULL); /* major protocol version */
1806 body_len_offset = g_memory_output_stream_get_data_size (mos);
1807 /* body length - will be filled in later */
1808 g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
1809 g_data_output_stream_put_uint32 (dos, message->priv->serial, NULL, NULL);
1811 num_fds_in_message = 0;
1813 if (message->priv->fd_list != NULL)
1814 num_fds_in_message = g_unix_fd_list_get_length (message->priv->fd_list);
1816 num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
1817 /* TODO: check we have all the right header fields and that they are the correct value etc etc */
1818 if (num_fds_in_message != num_fds_according_to_header)
1822 G_IO_ERROR_INVALID_ARGUMENT,
1823 _("Message has %d fds but the header field indicates %d fds"),
1825 num_fds_according_to_header);
1829 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
1830 g_hash_table_iter_init (&hash_iter, message->priv->headers);
1831 while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
1833 g_variant_builder_add (&builder,
1835 (guchar) GPOINTER_TO_UINT (key),
1838 header_fields = g_variant_builder_end (&builder);
1840 if (!append_value_to_blob (header_fields,
1841 g_variant_get_type (header_fields),
1846 g_variant_unref (header_fields);
1849 g_variant_unref (header_fields);
1851 /* header size must be a multiple of 8 */
1852 ensure_output_padding (mos, dos, 8);
1854 body_start_offset = g_memory_output_stream_get_data_size (mos);
1856 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
1857 signature_str = NULL;
1858 if (signature != NULL)
1859 signature_str = g_variant_get_string (signature, NULL);
1860 if (message->priv->body != NULL)
1862 gchar *tupled_signature_str;
1863 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
1864 if (signature == NULL)
1868 G_IO_ERROR_INVALID_ARGUMENT,
1869 _("Message body has signature `%s' but there is no signature header"),
1871 g_free (tupled_signature_str);
1874 else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->priv->body)) != 0)
1878 G_IO_ERROR_INVALID_ARGUMENT,
1879 _("Message body has type signature `%s' but signature in the header field is `%s'"),
1880 tupled_signature_str, g_variant_get_type_string (message->priv->body));
1881 g_free (tupled_signature_str);
1884 g_free (tupled_signature_str);
1885 if (!append_body_to_blob (message->priv->body, mos, dos, error))
1890 if (signature != NULL)
1894 G_IO_ERROR_INVALID_ARGUMENT,
1895 _("Message body is empty but signature in the header field is `(%s)'"),
1901 /* OK, we're done writing the message - set the body length */
1902 size = g_memory_output_stream_get_data_size (mos);
1903 body_size = size - body_start_offset;
1905 if (!g_seekable_seek (G_SEEKABLE (mos), body_len_offset, G_SEEK_SET, NULL, error))
1908 g_data_output_stream_put_uint32 (dos, body_size, NULL, NULL);
1911 ret = g_memdup (g_memory_output_stream_get_data (mos), size);
1914 g_object_unref (dos);
1915 g_object_unref (mos);
1920 /* ---------------------------------------------------------------------------------------------------- */
1923 get_uint32_header (GDBusMessage *message,
1924 GDBusMessageHeaderField header_field)
1930 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1931 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
1932 ret = g_variant_get_uint32 (value);
1937 static const gchar *
1938 get_string_header (GDBusMessage *message,
1939 GDBusMessageHeaderField header_field)
1945 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1946 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
1947 ret = g_variant_get_string (value, NULL);
1952 static const gchar *
1953 get_object_path_header (GDBusMessage *message,
1954 GDBusMessageHeaderField header_field)
1960 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1961 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
1962 ret = g_variant_get_string (value, NULL);
1967 static const gchar *
1968 get_signature_header (GDBusMessage *message,
1969 GDBusMessageHeaderField header_field)
1975 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1976 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
1977 ret = g_variant_get_string (value, NULL);
1982 /* ---------------------------------------------------------------------------------------------------- */
1985 set_uint32_header (GDBusMessage *message,
1986 GDBusMessageHeaderField header_field,
1989 g_dbus_message_set_header (message,
1991 g_variant_new_uint32 (value));
1995 set_string_header (GDBusMessage *message,
1996 GDBusMessageHeaderField header_field,
1999 g_dbus_message_set_header (message,
2001 value == NULL ? NULL : g_variant_new_string (value));
2005 set_object_path_header (GDBusMessage *message,
2006 GDBusMessageHeaderField header_field,
2009 g_dbus_message_set_header (message,
2011 value == NULL ? NULL : g_variant_new_object_path (value));
2015 set_signature_header (GDBusMessage *message,
2016 GDBusMessageHeaderField header_field,
2019 g_dbus_message_set_header (message,
2021 value == NULL ? NULL : g_variant_new_signature (value));
2024 /* ---------------------------------------------------------------------------------------------------- */
2027 * g_dbus_message_get_reply_serial:
2028 * @message: A #GDBusMessage.
2030 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2032 * Returns: The value.
2037 g_dbus_message_get_reply_serial (GDBusMessage *message)
2039 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2040 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL);
2044 * g_dbus_message_set_reply_serial:
2045 * @message: A #GDBusMessage.
2046 * @value: The value to set.
2048 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2053 g_dbus_message_set_reply_serial (GDBusMessage *message,
2056 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2057 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value);
2060 /* ---------------------------------------------------------------------------------------------------- */
2063 * g_dbus_message_get_interface:
2064 * @message: A #GDBusMessage.
2066 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2068 * Returns: The value.
2073 g_dbus_message_get_interface (GDBusMessage *message)
2075 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2076 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE);
2080 * g_dbus_message_set_interface:
2081 * @message: A #GDBusMessage.
2082 * @value: The value to set.
2084 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2089 g_dbus_message_set_interface (GDBusMessage *message,
2092 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2093 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2094 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value);
2097 /* ---------------------------------------------------------------------------------------------------- */
2100 * g_dbus_message_get_member:
2101 * @message: A #GDBusMessage.
2103 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2105 * Returns: The value.
2110 g_dbus_message_get_member (GDBusMessage *message)
2112 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2113 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER);
2117 * g_dbus_message_set_member:
2118 * @message: A #GDBusMessage.
2119 * @value: The value to set.
2121 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2126 g_dbus_message_set_member (GDBusMessage *message,
2129 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2130 g_return_if_fail (value == NULL || g_dbus_is_member_name (value));
2131 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value);
2134 /* ---------------------------------------------------------------------------------------------------- */
2137 * g_dbus_message_get_path:
2138 * @message: A #GDBusMessage.
2140 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2142 * Returns: The value.
2147 g_dbus_message_get_path (GDBusMessage *message)
2149 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2150 return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH);
2154 * g_dbus_message_set_path:
2155 * @message: A #GDBusMessage.
2156 * @value: The value to set.
2158 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2163 g_dbus_message_set_path (GDBusMessage *message,
2166 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2167 g_return_if_fail (value == NULL || g_variant_is_object_path (value));
2168 set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value);
2171 /* ---------------------------------------------------------------------------------------------------- */
2174 * g_dbus_message_get_sender:
2175 * @message: A #GDBusMessage.
2177 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2179 * Returns: The value.
2184 g_dbus_message_get_sender (GDBusMessage *message)
2186 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2187 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER);
2191 * g_dbus_message_set_sender:
2192 * @message: A #GDBusMessage.
2193 * @value: The value to set.
2195 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2200 g_dbus_message_set_sender (GDBusMessage *message,
2203 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2204 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2205 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value);
2208 /* ---------------------------------------------------------------------------------------------------- */
2211 * g_dbus_message_get_destination:
2212 * @message: A #GDBusMessage.
2214 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2216 * Returns: The value.
2221 g_dbus_message_get_destination (GDBusMessage *message)
2223 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2224 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION);
2228 * g_dbus_message_set_destination:
2229 * @message: A #GDBusMessage.
2230 * @value: The value to set.
2232 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2237 g_dbus_message_set_destination (GDBusMessage *message,
2240 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2241 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2242 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value);
2245 /* ---------------------------------------------------------------------------------------------------- */
2248 * g_dbus_message_get_error_name:
2249 * @message: A #GDBusMessage.
2251 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
2253 * Returns: The value.
2258 g_dbus_message_get_error_name (GDBusMessage *message)
2260 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2261 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME);
2265 * g_dbus_message_set_error_name:
2266 * @message: A #GDBusMessage.
2267 * @value: The value to set.
2269 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
2274 g_dbus_message_set_error_name (GDBusMessage *message,
2277 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2278 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2279 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value);
2282 /* ---------------------------------------------------------------------------------------------------- */
2285 * g_dbus_message_get_signature:
2286 * @message: A #GDBusMessage.
2288 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
2290 * Returns: The value.
2295 g_dbus_message_get_signature (GDBusMessage *message)
2298 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2299 ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2306 * g_dbus_message_set_signature:
2307 * @message: A #GDBusMessage.
2308 * @value: The value to set.
2310 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
2315 g_dbus_message_set_signature (GDBusMessage *message,
2318 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2319 g_return_if_fail (value == NULL || g_variant_is_signature (value));
2320 set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value);
2323 /* ---------------------------------------------------------------------------------------------------- */
2326 * g_dbus_message_get_arg0:
2327 * @message: A #GDBusMessage.
2329 * Convenience to get the first item in the body of @message.
2331 * Returns: The string item or %NULL if the first item in the body of
2332 * @message is not a string.
2337 g_dbus_message_get_arg0 (GDBusMessage *message)
2341 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2345 if (message->priv->body != NULL && g_variant_is_of_type (message->priv->body, G_VARIANT_TYPE_TUPLE))
2348 item = g_variant_get_child_value (message->priv->body, 0);
2349 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
2350 ret = g_variant_get_string (item, NULL);
2356 /* ---------------------------------------------------------------------------------------------------- */
2359 * g_dbus_message_get_num_unix_fds:
2360 * @message: A #GDBusMessage.
2362 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
2364 * Returns: The value.
2369 g_dbus_message_get_num_unix_fds (GDBusMessage *message)
2371 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2372 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS);
2376 * g_dbus_message_set_num_unix_fds:
2377 * @message: A #GDBusMessage.
2378 * @value: The value to set.
2380 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
2385 g_dbus_message_set_num_unix_fds (GDBusMessage *message,
2388 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2389 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value);
2392 /* ---------------------------------------------------------------------------------------------------- */
2395 * g_dbus_message_to_gerror:
2396 * @message: A #GDBusMessage.
2397 * @error: The #GError to set.
2399 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
2400 * nothing and returns %FALSE.
2402 * Otherwise this method encodes the error in @message as a #GError
2403 * using g_dbus_error_set_dbus_error() using the information in the
2404 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
2405 * well as the first string item in @message's body.
2407 * Returns: %TRUE if @error was set, %FALSE otherwise.
2412 g_dbus_message_to_gerror (GDBusMessage *message,
2416 const gchar *error_name;
2418 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
2421 if (message->priv->type != G_DBUS_MESSAGE_TYPE_ERROR)
2424 error_name = g_dbus_message_get_error_name (message);
2425 if (error_name != NULL)
2429 body = g_dbus_message_get_body (message);
2431 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)")))
2433 const gchar *error_message;
2434 g_variant_get (body, "(&s)", &error_message);
2435 g_dbus_error_set_dbus_error (error,
2442 /* these two situations are valid, yet pretty rare */
2445 g_dbus_error_set_dbus_error (error,
2448 _("Error return with body of type `%s'"),
2449 g_variant_get_type_string (body));
2453 g_dbus_error_set_dbus_error (error,
2456 _("Error return with empty body"));
2462 /* TOOD: this shouldn't happen - should check this at message serialization
2463 * time and disconnect the peer.
2468 "Error return without error-name header!");
2477 /* ---------------------------------------------------------------------------------------------------- */
2480 enum_to_string (GType enum_type, gint value)
2484 GEnumValue *enum_value;
2486 klass = g_type_class_ref (enum_type);
2487 enum_value = g_enum_get_value (klass, value);
2488 if (enum_value != NULL)
2489 ret = g_strdup (enum_value->value_nick);
2491 ret = g_strdup_printf ("unknown (value %d)", value);
2492 g_type_class_unref (klass);
2497 flags_to_string (GType flags_type, guint value)
2503 klass = g_type_class_ref (flags_type);
2504 s = g_string_new (NULL);
2505 for (n = 0; n < 32; n++)
2507 if ((value & (1<<n)) != 0)
2509 GFlagsValue *flags_value;
2510 flags_value = g_flags_get_first_value (klass, (1<<n));
2512 g_string_append_c (s, ',');
2513 if (flags_value != NULL)
2514 g_string_append (s, flags_value->value_nick);
2516 g_string_append_printf (s, "unknown (bit %d)", n);
2520 g_string_append (s, "none");
2521 g_type_class_unref (klass);
2522 return g_string_free (s, FALSE);;
2526 _sort_keys_func (gconstpointer a,
2532 ia = GPOINTER_TO_INT (a);
2533 ib = GPOINTER_TO_INT (b);
2539 * g_dbus_message_print:
2540 * @message: A #GDBusMessage.
2541 * @indent: Indentation level.
2543 * Produces a human-readable multi-line description of @message.
2545 * The contents of the description has no ABI guarantees, the contents
2546 * and formatting is subject to change at any time. Typical output
2547 * looks something like this:
2554 * path -> objectpath '/org/gtk/GDBus/TestObject'
2555 * interface -> 'org.gtk.GDBus.TestInterface'
2556 * member -> 'GimmeStdout'
2557 * destination -> ':1.146'
2559 * UNIX File Descriptors:
2564 * Type: method-return
2565 * Flags: no-reply-expected
2569 * reply-serial -> uint32 4
2570 * destination -> ':1.159'
2571 * sender -> ':1.146'
2572 * num-unix-fds -> uint32 1
2574 * UNIX File Descriptors:
2575 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
2578 * Returns: A string that should be freed with g_free().
2583 g_dbus_message_print (GDBusMessage *message,
2591 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2593 str = g_string_new (NULL);
2595 s = enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->priv->type);
2596 g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
2598 s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->priv->flags);
2599 g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
2601 g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->priv->major_protocol_version);
2602 g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->priv->serial);
2604 g_string_append_printf (str, "%*sHeaders:\n", indent, "");
2605 keys = g_hash_table_get_keys (message->priv->headers);
2606 keys = g_list_sort (keys, _sort_keys_func);
2609 for (l = keys; l != NULL; l = l->next)
2611 gint key = GPOINTER_TO_INT (l->data);
2615 value = g_hash_table_lookup (message->priv->headers, l->data);
2616 g_assert (value != NULL);
2618 s = enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
2619 value_str = g_variant_print (value, TRUE);
2620 g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
2627 g_string_append_printf (str, "%*s (none)\n", indent, "");
2629 g_string_append_printf (str, "%*sBody: ", indent, "");
2630 if (message->priv->body != NULL)
2632 g_variant_print_string (message->priv->body,
2638 g_string_append (str, "()");
2640 g_string_append (str, "\n");
2642 g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
2643 if (message->priv->fd_list != NULL)
2649 fds = g_unix_fd_list_peek_fds (message->priv->fd_list, &num_fds);
2652 for (n = 0; n < num_fds; n++)
2655 struct stat statbuf;
2656 fs = g_string_new (NULL);
2657 if (fstat (fds[n], &statbuf) == 0)
2659 g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "",
2660 major (statbuf.st_dev), minor (statbuf.st_dev));
2661 g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "",
2663 g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2664 (guint64) statbuf.st_ino);
2665 g_string_append_printf (fs, "%s" "uid=%d", fs->len > 0 ? "," : "",
2667 g_string_append_printf (fs, "%s" "gid=%d", fs->len > 0 ? "," : "",
2669 g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
2670 major (statbuf.st_rdev), minor (statbuf.st_rdev));
2671 g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2672 (guint64) statbuf.st_size);
2673 g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2674 (guint64) statbuf.st_atime);
2675 g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2676 (guint64) statbuf.st_mtime);
2677 g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2678 (guint64) statbuf.st_ctime);
2682 g_string_append_printf (fs, "(fstat failed: %s)", strerror (errno));
2684 g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str);
2685 g_string_free (fs, TRUE);
2690 g_string_append_printf (str, "%*s (empty)\n", indent, "");
2695 g_string_append_printf (str, "%*s (none)\n", indent, "");
2699 return g_string_free (str, FALSE);
2703 #define __G_DBUS_MESSAGE_C__
2704 #include "gioaliasdef.c"