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);
907 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
909 if (!ensure_input_padding (mis, 4, &local_error))
915 len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
916 if (local_error != NULL)
918 v = read_string (mis, dis, (gsize) len, &local_error);
921 if (!g_variant_is_object_path (v))
923 g_set_error (&local_error,
925 G_IO_ERROR_INVALID_ARGUMENT,
926 _("Parsed value `%s' is not a valid D-Bus object path"),
930 ret = g_variant_new_object_path (v);
933 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
939 len = g_data_input_stream_read_byte (dis, NULL, &local_error);
940 if (local_error != NULL)
942 v = read_string (mis, dis, (gsize) len, &local_error);
945 if (!g_variant_is_signature (v))
947 g_set_error (&local_error,
949 G_IO_ERROR_INVALID_ARGUMENT,
950 _("Parsed value `%s' is not a valid D-Bus signature"),
954 ret = g_variant_new_signature (v);
957 else if (g_variant_type_is_array (type))
962 const GVariantType *element_type;
963 GVariantBuilder *builder;
965 if (!ensure_input_padding (mis, 4, &local_error))
967 array_len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
969 if (array_len > (2<<26))
971 g_set_error (&local_error,
973 G_IO_ERROR_INVALID_ARGUMENT,
974 _("Encountered array of length %" G_GUINT32_FORMAT " bytes. Maximum length is 2<<26 bytes."),
979 builder = g_variant_builder_new (type);
980 element_type = g_variant_type_element (type);
985 item = parse_value_from_blob (mis,
990 g_assert (item == NULL);
994 /* TODO: optimize array of primitive types */
995 offset = g_seekable_tell (G_SEEKABLE (mis));
996 target = offset + array_len;
997 while (offset < target)
1000 item = parse_value_from_blob (mis,
1007 g_variant_builder_unref (builder);
1010 g_variant_builder_add_value (builder, item);
1011 offset = g_seekable_tell (G_SEEKABLE (mis));
1017 ret = g_variant_builder_end (builder);
1021 g_variant_builder_unref (builder);
1024 else if (g_variant_type_is_dict_entry (type))
1026 const GVariantType *key_type;
1027 const GVariantType *value_type;
1031 if (!ensure_input_padding (mis, 8, &local_error))
1036 key_type = g_variant_type_key (type);
1037 key = parse_value_from_blob (mis,
1045 value_type = g_variant_type_value (type);
1046 value = parse_value_from_blob (mis,
1053 g_variant_unref (key);
1056 ret = g_variant_new_dict_entry (key, value);
1059 else if (g_variant_type_is_tuple (type))
1061 if (!ensure_input_padding (mis, 8, &local_error))
1066 const GVariantType *element_type;
1067 GVariantBuilder *builder;
1069 builder = g_variant_builder_new (type);
1070 element_type = g_variant_type_first (type);
1071 while (element_type != NULL)
1074 item = parse_value_from_blob (mis,
1081 g_variant_builder_unref (builder);
1084 g_variant_builder_add_value (builder, item);
1086 element_type = g_variant_type_next (element_type);
1088 ret = g_variant_builder_end (builder);
1091 else if (g_variant_type_is_variant (type))
1097 GVariantType *variant_type;
1100 siglen = g_data_input_stream_read_byte (dis, NULL, &local_error);
1101 if (local_error != NULL)
1103 sig = read_string (mis, dis, (gsize) siglen, &local_error);
1106 if (!g_variant_is_signature (sig))
1108 g_set_error (&local_error,
1110 G_IO_ERROR_INVALID_ARGUMENT,
1111 _("Parsed value `%s' for variant is not a valid D-Bus signature"),
1115 variant_type = g_variant_type_new (sig);
1116 value = parse_value_from_blob (mis,
1121 g_variant_type_free (variant_type);
1124 ret = g_variant_new_variant (value);
1130 s = g_variant_type_dup_string (type);
1131 g_set_error (&local_error,
1133 G_IO_ERROR_INVALID_ARGUMENT,
1134 _("Error deserializing GVariant with type-string `%s' from the D-Bus wire format"),
1140 g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
1144 g_propagate_error (error, local_error);
1148 /* ---------------------------------------------------------------------------------------------------- */
1150 /* message_header must be at least 16 bytes */
1153 * g_dbus_message_bytes_needed:
1154 * @blob: A blob represent a binary D-Bus message.
1155 * @blob_len: The length of @blob (must be at least 16).
1156 * @error: Return location for error or %NULL.
1158 * Utility function to calculate how many bytes are needed to
1159 * completely deserialize the D-Bus message stored at @blob.
1161 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1162 * @blob contains invalid data or not enough data is available to
1163 * determine the size).
1168 g_dbus_message_bytes_needed (guchar *blob,
1176 g_return_val_if_fail (blob != NULL, -1);
1177 g_return_val_if_fail (error == NULL || *error == NULL, -1);
1178 g_return_val_if_fail (blob_len >= 16, -1);
1182 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1183 ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]);
1184 /* round up so it's a multiple of 8 */
1185 ret = 8 * ((ret + 7)/8);
1186 /* finally add the body size */
1187 ret += GUINT32_FROM_LE (((guint32 *) blob)[1]);
1189 else if (blob[0] == 'B')
1192 g_assert_not_reached ();
1198 G_IO_ERROR_INVALID_ARGUMENT,
1199 "Unable to determine message blob length - given blob is malformed");
1206 G_IO_ERROR_INVALID_ARGUMENT,
1207 "Blob indicates that message exceeds maximum message length (128MiB)");
1214 /* ---------------------------------------------------------------------------------------------------- */
1217 * g_dbus_message_new_from_blob:
1218 * @blob: A blob represent a binary D-Bus message.
1219 * @blob_len: The length of @blob.
1220 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1221 * @error: Return location for error or %NULL.
1223 * Creates a new #GDBusMessage from the data stored at @blob.
1225 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
1231 g_dbus_message_new_from_blob (guchar *blob,
1233 GDBusCapabilityFlags capabilities,
1237 GMemoryInputStream *mis;
1238 GDataInputStream *dis;
1239 GDBusMessage *message;
1241 guchar major_protocol_version;
1242 GDataStreamByteOrder byte_order;
1243 guint32 message_body_len;
1247 GVariant *signature;
1249 /* TODO: check against @capabilities */
1253 g_return_val_if_fail (blob != NULL, NULL);
1254 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1255 g_return_val_if_fail (blob_len >= 12, NULL);
1257 message = g_dbus_message_new ();
1259 mis = G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (blob, blob_len, NULL));
1260 dis = g_data_input_stream_new (G_INPUT_STREAM (mis));
1262 endianness = g_data_input_stream_read_byte (dis, NULL, NULL);
1266 byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
1269 byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
1274 G_IO_ERROR_INVALID_ARGUMENT,
1275 _("Invalid endianness value. Expected 'l' or 'B' but found '%c' (%d)"),
1276 endianness, endianness);
1279 g_data_input_stream_set_byte_order (dis, byte_order);
1281 message->priv->type = g_data_input_stream_read_byte (dis, NULL, NULL);
1282 message->priv->flags = g_data_input_stream_read_byte (dis, NULL, NULL);
1283 major_protocol_version = g_data_input_stream_read_byte (dis, NULL, NULL);
1284 if (major_protocol_version != 1)
1288 G_IO_ERROR_INVALID_ARGUMENT,
1289 _("Invalid major protocol version. Expected 1 but found %d"),
1290 major_protocol_version);
1293 message_body_len = g_data_input_stream_read_uint32 (dis, NULL, NULL);
1294 message->priv->serial = g_data_input_stream_read_uint32 (dis, NULL, NULL);
1296 headers = parse_value_from_blob (mis,
1298 G_VARIANT_TYPE ("a{yv}"),
1301 if (headers == NULL)
1303 g_variant_ref_sink (headers);
1304 g_variant_iter_init (&iter, headers);
1305 while ((item = g_variant_iter_next_value (&iter)))
1307 guchar header_field;
1309 g_variant_get (item,
1313 g_dbus_message_set_header (message, header_field, value);
1315 g_variant_unref (headers);
1317 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
1318 if (signature != NULL)
1320 const gchar *signature_str;
1321 gsize signature_str_len;
1323 signature_str = g_variant_get_string (signature, NULL);
1324 signature_str_len = strlen (signature_str);
1326 /* signature but no body */
1327 if (message_body_len == 0 && signature_str_len > 0)
1331 G_IO_ERROR_INVALID_ARGUMENT,
1332 _("Signature header with signature `%s' found but message body is empty"),
1336 else if (signature_str_len > 0)
1338 GVariantType *variant_type;
1339 gchar *tupled_signature_str;
1341 if (!g_variant_is_signature (signature_str))
1345 G_IO_ERROR_INVALID_ARGUMENT,
1346 _("Parsed value `%s' is not a valid D-Bus signature (for body)"),
1350 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
1351 variant_type = g_variant_type_new (tupled_signature_str);
1352 g_free (tupled_signature_str);
1353 message->priv->body = parse_value_from_blob (mis,
1358 if (message->priv->body == NULL)
1360 g_variant_type_free (variant_type);
1363 g_variant_ref_sink (message->priv->body);
1364 g_variant_type_free (variant_type);
1369 /* no signature, this is only OK if the body is empty */
1370 if (message_body_len != 0)
1374 G_IO_ERROR_INVALID_ARGUMENT,
1375 _("No signature header in message but the message body is %" G_GUINT32_FORMAT " bytes"),
1385 g_object_unref (dis);
1386 g_object_unref (mis);
1394 if (message != NULL)
1395 g_object_unref (message);
1400 /* ---------------------------------------------------------------------------------------------------- */
1403 ensure_output_padding (GMemoryOutputStream *mos,
1404 GDataOutputStream *dos,
1408 gsize wanted_offset;
1409 gsize padding_needed;
1412 offset = g_memory_output_stream_get_data_size (mos);
1413 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
1414 padding_needed = wanted_offset - offset;
1416 for (n = 0; n < padding_needed; n++)
1417 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1419 return padding_needed;
1422 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
1424 append_value_to_blob (GVariant *value,
1425 const GVariantType *type,
1426 GMemoryOutputStream *mos,
1427 GDataOutputStream *dos,
1428 gsize *out_padding_added,
1431 gsize padding_added;
1435 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
1437 padding_added = ensure_output_padding (mos, dos, 4);
1440 gboolean v = g_variant_get_boolean (value);
1441 g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
1444 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
1448 guint8 v = g_variant_get_byte (value);
1449 g_data_output_stream_put_byte (dos, v, NULL, NULL);
1452 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
1454 padding_added = ensure_output_padding (mos, dos, 2);
1457 gint16 v = g_variant_get_int16 (value);
1458 g_data_output_stream_put_int16 (dos, v, NULL, NULL);
1461 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
1463 padding_added = ensure_output_padding (mos, dos, 2);
1466 guint16 v = g_variant_get_uint16 (value);
1467 g_data_output_stream_put_uint16 (dos, v, NULL, NULL);
1470 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
1472 padding_added = ensure_output_padding (mos, dos, 4);
1475 gint32 v = g_variant_get_int32 (value);
1476 g_data_output_stream_put_int32 (dos, v, NULL, NULL);
1479 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
1481 padding_added = ensure_output_padding (mos, dos, 4);
1484 guint32 v = g_variant_get_uint32 (value);
1485 g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
1488 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
1490 padding_added = ensure_output_padding (mos, dos, 8);
1493 gint64 v = g_variant_get_int64 (value);
1494 g_data_output_stream_put_int64 (dos, v, NULL, NULL);
1497 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
1499 padding_added = ensure_output_padding (mos, dos, 8);
1502 guint64 v = g_variant_get_uint64 (value);
1503 g_data_output_stream_put_uint64 (dos, v, NULL, NULL);
1506 else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
1508 padding_added = ensure_output_padding (mos, dos, 8);
1512 gdouble v = g_variant_get_double (value);
1514 encoded = (guint64 *) &v;
1515 g_data_output_stream_put_uint64 (dos, *encoded, NULL, NULL);
1518 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
1520 padding_added = ensure_output_padding (mos, dos, 4);
1523 const gchar *v = g_variant_get_string (value, NULL);
1526 g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
1527 g_data_output_stream_put_string (dos, v, NULL, NULL);
1528 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1531 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
1533 padding_added = ensure_output_padding (mos, dos, 4);
1536 /* TODO: validate object path */
1537 const gchar *v = g_variant_get_string (value, NULL);
1540 g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
1541 g_data_output_stream_put_string (dos, v, NULL, NULL);
1542 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1545 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
1549 /* TODO: validate signature (including max len being 255) */
1550 const gchar *v = g_variant_get_string (value, NULL);
1553 g_data_output_stream_put_byte (dos, len, NULL, NULL);
1554 g_data_output_stream_put_string (dos, v, NULL, NULL);
1555 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1558 else if (g_variant_type_is_array (type))
1562 goffset array_len_offset;
1563 goffset array_payload_begin_offset;
1567 padding_added = ensure_output_padding (mos, dos, 4);
1570 /* array length - will be filled in later */
1571 array_len_offset = g_memory_output_stream_get_data_size (mos);
1572 g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
1574 /* From the D-Bus spec:
1576 * "A UINT32 giving the length of the array data in bytes,
1577 * followed by alignment padding to the alignment boundary of
1578 * the array element type, followed by each array element. The
1579 * array length is from the end of the alignment padding to
1580 * the end of the last element, i.e. it does not include the
1581 * padding after the length, or any padding after the last
1584 * Thus, we need to count how much padding the first element
1585 * contributes and subtract that from the array length.
1587 array_payload_begin_offset = g_memory_output_stream_get_data_size (mos);
1589 if (g_variant_n_children (value) == 0)
1591 gsize padding_added_for_item;
1592 if (!append_value_to_blob (NULL,
1593 g_variant_type_element (type),
1596 &padding_added_for_item,
1599 array_payload_begin_offset += padding_added_for_item;
1605 g_variant_iter_init (&iter, value);
1606 while ((item = g_variant_iter_next_value (&iter)))
1608 gsize padding_added_for_item;
1609 if (!append_value_to_blob (item,
1610 g_variant_get_type (item),
1613 &padding_added_for_item,
1618 array_payload_begin_offset += padding_added_for_item;
1624 cur_offset = g_memory_output_stream_get_data_size (mos);
1626 array_len = cur_offset - array_payload_begin_offset;
1628 if (!g_seekable_seek (G_SEEKABLE (mos), array_len_offset, G_SEEK_SET, NULL, error))
1631 g_data_output_stream_put_uint32 (dos, array_len, NULL, NULL);
1633 if (!g_seekable_seek (G_SEEKABLE (mos), cur_offset, G_SEEK_SET, NULL, error))
1637 else if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
1639 padding_added = ensure_output_padding (mos, dos, 8);
1644 g_variant_iter_init (&iter, value);
1645 while ((item = g_variant_iter_next_value (&iter)))
1647 if (!append_value_to_blob (item,
1648 g_variant_get_type (item),
1657 else if (g_variant_type_is_variant (type))
1662 const gchar *signature;
1663 child = g_variant_get_child_value (value, 0);
1664 signature = g_variant_get_type_string (child);
1665 /* TODO: validate signature (including max len being 255) */
1666 g_data_output_stream_put_byte (dos, strlen (signature), NULL, NULL);
1667 g_data_output_stream_put_string (dos, signature, NULL, NULL);
1668 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1669 if (!append_value_to_blob (child,
1670 g_variant_get_type (child),
1676 g_variant_unref (child);
1679 g_variant_unref (child);
1686 G_IO_ERROR_INVALID_ARGUMENT,
1687 _("Error serializing GVariant with type-string `%s' to the D-Bus wire format"),
1688 g_variant_get_type_string (value));
1692 if (out_padding_added != NULL)
1693 *out_padding_added = padding_added;
1702 append_body_to_blob (GVariant *value,
1703 GMemoryOutputStream *mos,
1704 GDataOutputStream *dos,
1713 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
1717 G_IO_ERROR_INVALID_ARGUMENT,
1718 "Expected a tuple for the body of the GDBusMessage.");
1722 g_variant_iter_init (&iter, value);
1723 while ((item = g_variant_iter_next_value (&iter)))
1725 if (!append_value_to_blob (item,
1726 g_variant_get_type (item),
1739 /* ---------------------------------------------------------------------------------------------------- */
1742 * g_dbus_message_to_blob:
1743 * @message: A #GDBusMessage.
1744 * @out_size: Return location for size of generated blob.
1745 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1746 * @error: Return location for error.
1748 * Serializes @message to a blob.
1750 * Returns: A pointer to a valid binary D-Bus message of @out_size bytes
1751 * generated by @message or %NULL if @error is set. Free with g_free().
1756 g_dbus_message_to_blob (GDBusMessage *message,
1758 GDBusCapabilityFlags capabilities,
1761 GMemoryOutputStream *mos;
1762 GDataOutputStream *dos;
1765 GDataStreamByteOrder byte_order;
1766 goffset body_len_offset;
1767 goffset body_start_offset;
1769 GVariant *header_fields;
1770 GVariantBuilder *builder;
1771 GHashTableIter hash_iter;
1773 GVariant *header_value;
1774 GVariant *signature;
1775 const gchar *signature_str;
1776 gint num_fds_in_message;
1777 gint num_fds_according_to_header;
1779 /* TODO: check against @capabilities */
1783 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1784 g_return_val_if_fail (out_size != NULL, NULL);
1785 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1787 mos = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0, g_realloc, g_free));
1788 dos = g_data_output_stream_new (G_OUTPUT_STREAM (mos));
1790 /* TODO: detect endianess... */
1791 byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
1792 g_data_output_stream_set_byte_order (dos, byte_order);
1795 g_data_output_stream_put_byte (dos, byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN ? 'l' : 'B', NULL, NULL);
1796 g_data_output_stream_put_byte (dos, message->priv->type, NULL, NULL);
1797 g_data_output_stream_put_byte (dos, message->priv->flags, NULL, NULL);
1798 g_data_output_stream_put_byte (dos, 1, NULL, NULL); /* major protocol version */
1799 body_len_offset = g_memory_output_stream_get_data_size (mos);
1800 /* body length - will be filled in later */
1801 g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
1802 g_data_output_stream_put_uint32 (dos, message->priv->serial, NULL, NULL);
1804 num_fds_in_message = 0;
1806 if (message->priv->fd_list != NULL)
1807 num_fds_in_message = g_unix_fd_list_get_length (message->priv->fd_list);
1809 num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
1810 /* TODO: check we have all the right header fields and that they are the correct value etc etc */
1811 if (num_fds_in_message != num_fds_according_to_header)
1815 G_IO_ERROR_INVALID_ARGUMENT,
1816 _("Message has %d fds but the header field indicates %d fds"),
1818 num_fds_according_to_header);
1822 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{yv}"));//G_VARIANT_TYPE_ARRAY);
1823 g_hash_table_iter_init (&hash_iter, message->priv->headers);
1824 while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
1826 g_variant_builder_add (builder,
1828 (guchar) GPOINTER_TO_UINT (key),
1831 header_fields = g_variant_new ("a{yv}", builder);
1833 if (!append_value_to_blob (header_fields,
1834 g_variant_get_type (header_fields),
1839 g_variant_unref (header_fields);
1842 g_variant_unref (header_fields);
1844 /* header size must be a multiple of 8 */
1845 ensure_output_padding (mos, dos, 8);
1847 body_start_offset = g_memory_output_stream_get_data_size (mos);
1849 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
1850 signature_str = NULL;
1851 if (signature != NULL)
1852 signature_str = g_variant_get_string (signature, NULL);
1853 if (message->priv->body != NULL)
1855 gchar *tupled_signature_str;
1856 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
1857 if (signature == NULL)
1861 G_IO_ERROR_INVALID_ARGUMENT,
1862 _("Message body has signature `%s' but there is no signature header"),
1864 g_free (tupled_signature_str);
1867 else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->priv->body)) != 0)
1871 G_IO_ERROR_INVALID_ARGUMENT,
1872 _("Message body has type signature `%s' but signature in the header field is `%s'"),
1873 tupled_signature_str, g_variant_get_type_string (message->priv->body));
1874 g_free (tupled_signature_str);
1877 g_free (tupled_signature_str);
1878 if (!append_body_to_blob (message->priv->body, mos, dos, error))
1883 if (signature != NULL)
1887 G_IO_ERROR_INVALID_ARGUMENT,
1888 _("Message body is empty but signature in the header field is `(%s)'"),
1894 /* OK, we're done writing the message - set the body length */
1895 size = g_memory_output_stream_get_data_size (mos);
1896 body_size = size - body_start_offset;
1898 if (!g_seekable_seek (G_SEEKABLE (mos), body_len_offset, G_SEEK_SET, NULL, error))
1901 g_data_output_stream_put_uint32 (dos, body_size, NULL, NULL);
1904 ret = g_memdup (g_memory_output_stream_get_data (mos), size);
1907 g_object_unref (dos);
1908 g_object_unref (mos);
1913 /* ---------------------------------------------------------------------------------------------------- */
1916 get_uint32_header (GDBusMessage *message,
1917 GDBusMessageHeaderField header_field)
1923 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1924 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
1925 ret = g_variant_get_uint32 (value);
1930 static const gchar *
1931 get_string_header (GDBusMessage *message,
1932 GDBusMessageHeaderField header_field)
1938 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1939 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
1940 ret = g_variant_get_string (value, NULL);
1945 static const gchar *
1946 get_object_path_header (GDBusMessage *message,
1947 GDBusMessageHeaderField header_field)
1953 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1954 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
1955 ret = g_variant_get_string (value, NULL);
1960 static const gchar *
1961 get_signature_header (GDBusMessage *message,
1962 GDBusMessageHeaderField header_field)
1968 value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
1969 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
1970 ret = g_variant_get_string (value, NULL);
1975 /* ---------------------------------------------------------------------------------------------------- */
1978 set_uint32_header (GDBusMessage *message,
1979 GDBusMessageHeaderField header_field,
1982 g_dbus_message_set_header (message,
1984 g_variant_new_uint32 (value));
1988 set_string_header (GDBusMessage *message,
1989 GDBusMessageHeaderField header_field,
1992 g_dbus_message_set_header (message,
1994 value == NULL ? NULL : g_variant_new_string (value));
1998 set_object_path_header (GDBusMessage *message,
1999 GDBusMessageHeaderField header_field,
2002 g_dbus_message_set_header (message,
2004 value == NULL ? NULL : g_variant_new_object_path (value));
2008 set_signature_header (GDBusMessage *message,
2009 GDBusMessageHeaderField header_field,
2012 g_dbus_message_set_header (message,
2014 value == NULL ? NULL : g_variant_new_signature (value));
2017 /* ---------------------------------------------------------------------------------------------------- */
2020 * g_dbus_message_get_reply_serial:
2021 * @message: A #GDBusMessage.
2023 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2025 * Returns: The value.
2030 g_dbus_message_get_reply_serial (GDBusMessage *message)
2032 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2033 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL);
2037 * g_dbus_message_set_reply_serial:
2038 * @message: A #GDBusMessage.
2039 * @value: The value to set.
2041 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2046 g_dbus_message_set_reply_serial (GDBusMessage *message,
2049 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2050 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value);
2053 /* ---------------------------------------------------------------------------------------------------- */
2056 * g_dbus_message_get_interface:
2057 * @message: A #GDBusMessage.
2059 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2061 * Returns: The value.
2066 g_dbus_message_get_interface (GDBusMessage *message)
2068 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2069 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE);
2073 * g_dbus_message_set_interface:
2074 * @message: A #GDBusMessage.
2075 * @value: The value to set.
2077 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2082 g_dbus_message_set_interface (GDBusMessage *message,
2085 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2086 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2087 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value);
2090 /* ---------------------------------------------------------------------------------------------------- */
2093 * g_dbus_message_get_member:
2094 * @message: A #GDBusMessage.
2096 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2098 * Returns: The value.
2103 g_dbus_message_get_member (GDBusMessage *message)
2105 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2106 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER);
2110 * g_dbus_message_set_member:
2111 * @message: A #GDBusMessage.
2112 * @value: The value to set.
2114 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2119 g_dbus_message_set_member (GDBusMessage *message,
2122 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2123 g_return_if_fail (value == NULL || g_dbus_is_member_name (value));
2124 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value);
2127 /* ---------------------------------------------------------------------------------------------------- */
2130 * g_dbus_message_get_path:
2131 * @message: A #GDBusMessage.
2133 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2135 * Returns: The value.
2140 g_dbus_message_get_path (GDBusMessage *message)
2142 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2143 return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH);
2147 * g_dbus_message_set_path:
2148 * @message: A #GDBusMessage.
2149 * @value: The value to set.
2151 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2156 g_dbus_message_set_path (GDBusMessage *message,
2159 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2160 g_return_if_fail (value == NULL || g_variant_is_object_path (value));
2161 set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value);
2164 /* ---------------------------------------------------------------------------------------------------- */
2167 * g_dbus_message_get_sender:
2168 * @message: A #GDBusMessage.
2170 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2172 * Returns: The value.
2177 g_dbus_message_get_sender (GDBusMessage *message)
2179 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2180 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER);
2184 * g_dbus_message_set_sender:
2185 * @message: A #GDBusMessage.
2186 * @value: The value to set.
2188 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2193 g_dbus_message_set_sender (GDBusMessage *message,
2196 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2197 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2198 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value);
2201 /* ---------------------------------------------------------------------------------------------------- */
2204 * g_dbus_message_get_destination:
2205 * @message: A #GDBusMessage.
2207 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2209 * Returns: The value.
2214 g_dbus_message_get_destination (GDBusMessage *message)
2216 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2217 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION);
2221 * g_dbus_message_set_destination:
2222 * @message: A #GDBusMessage.
2223 * @value: The value to set.
2225 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2230 g_dbus_message_set_destination (GDBusMessage *message,
2233 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2234 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2235 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value);
2238 /* ---------------------------------------------------------------------------------------------------- */
2241 * g_dbus_message_get_error_name:
2242 * @message: A #GDBusMessage.
2244 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
2246 * Returns: The value.
2251 g_dbus_message_get_error_name (GDBusMessage *message)
2253 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2254 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME);
2258 * g_dbus_message_set_error_name:
2259 * @message: A #GDBusMessage.
2260 * @value: The value to set.
2262 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
2267 g_dbus_message_set_error_name (GDBusMessage *message,
2270 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2271 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2272 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value);
2275 /* ---------------------------------------------------------------------------------------------------- */
2278 * g_dbus_message_get_signature:
2279 * @message: A #GDBusMessage.
2281 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
2283 * Returns: The value.
2288 g_dbus_message_get_signature (GDBusMessage *message)
2291 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2292 ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2299 * g_dbus_message_set_signature:
2300 * @message: A #GDBusMessage.
2301 * @value: The value to set.
2303 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
2308 g_dbus_message_set_signature (GDBusMessage *message,
2311 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2312 g_return_if_fail (value == NULL || g_variant_is_signature (value));
2313 set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value);
2316 /* ---------------------------------------------------------------------------------------------------- */
2319 * g_dbus_message_get_arg0:
2320 * @message: A #GDBusMessage.
2322 * Convenience to get the first item in the body of @message.
2324 * Returns: The string item or %NULL if the first item in the body of
2325 * @message is not a string.
2330 g_dbus_message_get_arg0 (GDBusMessage *message)
2334 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2338 if (message->priv->body != NULL && g_variant_is_of_type (message->priv->body, G_VARIANT_TYPE_TUPLE))
2341 item = g_variant_get_child_value (message->priv->body, 0);
2342 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
2343 ret = g_variant_get_string (item, NULL);
2349 /* ---------------------------------------------------------------------------------------------------- */
2352 * g_dbus_message_get_num_unix_fds:
2353 * @message: A #GDBusMessage.
2355 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
2357 * Returns: The value.
2362 g_dbus_message_get_num_unix_fds (GDBusMessage *message)
2364 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2365 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS);
2369 * g_dbus_message_set_num_unix_fds:
2370 * @message: A #GDBusMessage.
2371 * @value: The value to set.
2373 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
2378 g_dbus_message_set_num_unix_fds (GDBusMessage *message,
2381 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2382 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value);
2385 /* ---------------------------------------------------------------------------------------------------- */
2388 * g_dbus_message_to_gerror:
2389 * @message: A #GDBusMessage.
2390 * @error: The #GError to set.
2392 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
2393 * nothing and returns %FALSE.
2395 * Otherwise this method encodes the error in @message as a #GError
2396 * using g_dbus_error_set_dbus_error() using the information in the
2397 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
2398 * well as the first string item in @message's body.
2400 * Returns: %TRUE if @error was set, %FALSE otherwise.
2405 g_dbus_message_to_gerror (GDBusMessage *message,
2409 const gchar *error_name;
2411 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
2414 if (message->priv->type != G_DBUS_MESSAGE_TYPE_ERROR)
2417 error_name = g_dbus_message_get_error_name (message);
2418 if (error_name != NULL)
2422 body = g_dbus_message_get_body (message);
2424 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)")))
2426 const gchar *error_message;
2427 g_variant_get (body, "(&s)", &error_message);
2428 g_dbus_error_set_dbus_error (error,
2435 /* these two situations are valid, yet pretty rare */
2438 g_dbus_error_set_dbus_error (error,
2441 _("Error return with body of type `%s'"),
2442 g_variant_get_type_string (body));
2446 g_dbus_error_set_dbus_error (error,
2449 _("Error return with empty body"));
2455 /* TOOD: this shouldn't happen - should check this at message serialization
2456 * time and disconnect the peer.
2461 "Error return without error-name header!");
2470 /* ---------------------------------------------------------------------------------------------------- */
2473 enum_to_string (GType enum_type, gint value)
2477 GEnumValue *enum_value;
2479 klass = g_type_class_ref (enum_type);
2480 enum_value = g_enum_get_value (klass, value);
2481 if (enum_value != NULL)
2482 ret = g_strdup (enum_value->value_nick);
2484 ret = g_strdup_printf ("unknown (value %d)", value);
2485 g_type_class_unref (klass);
2490 flags_to_string (GType flags_type, guint value)
2496 klass = g_type_class_ref (flags_type);
2497 s = g_string_new (NULL);
2498 for (n = 0; n < 32; n++)
2500 if ((value & (1<<n)) != 0)
2502 GFlagsValue *flags_value;
2503 flags_value = g_flags_get_first_value (klass, (1<<n));
2505 g_string_append_c (s, ',');
2506 if (flags_value != NULL)
2507 g_string_append (s, flags_value->value_nick);
2509 g_string_append_printf (s, "unknown (bit %d)", n);
2513 g_string_append (s, "none");
2514 g_type_class_unref (klass);
2515 return g_string_free (s, FALSE);;
2519 _sort_keys_func (gconstpointer a,
2525 ia = GPOINTER_TO_INT (a);
2526 ib = GPOINTER_TO_INT (b);
2532 * g_dbus_message_print:
2533 * @message: A #GDBusMessage.
2534 * @indent: Indentation level.
2536 * Produces a human-readable multi-line description of @message.
2538 * The contents of the description has no ABI guarantees, the contents
2539 * and formatting is subject to change at any time. Typical output
2540 * looks something like this:
2547 * path -> objectpath '/org/gtk/GDBus/TestObject'
2548 * interface -> 'org.gtk.GDBus.TestInterface'
2549 * member -> 'GimmeStdout'
2550 * destination -> ':1.146'
2552 * UNIX File Descriptors:
2557 * Type: method-return
2558 * Flags: no-reply-expected
2562 * reply-serial -> uint32 4
2563 * destination -> ':1.159'
2564 * sender -> ':1.146'
2565 * num-unix-fds -> uint32 1
2567 * UNIX File Descriptors:
2568 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
2571 * Returns: A string that should be freed with g_free().
2576 g_dbus_message_print (GDBusMessage *message,
2584 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2586 str = g_string_new (NULL);
2588 s = enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->priv->type);
2589 g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
2591 s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->priv->flags);
2592 g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
2594 g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->priv->major_protocol_version);
2595 g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->priv->serial);
2597 g_string_append_printf (str, "%*sHeaders:\n", indent, "");
2598 keys = g_hash_table_get_keys (message->priv->headers);
2599 keys = g_list_sort (keys, _sort_keys_func);
2602 for (l = keys; l != NULL; l = l->next)
2604 gint key = GPOINTER_TO_INT (l->data);
2608 value = g_hash_table_lookup (message->priv->headers, l->data);
2609 g_assert (value != NULL);
2611 s = enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
2612 value_str = g_variant_print (value, TRUE);
2613 g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
2620 g_string_append_printf (str, "%*s (none)\n", indent, "");
2622 g_string_append_printf (str, "%*sBody: ", indent, "");
2623 if (message->priv->body != NULL)
2625 g_variant_print_string (message->priv->body,
2631 g_string_append (str, "()");
2633 g_string_append (str, "\n");
2635 g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
2636 if (message->priv->fd_list != NULL)
2642 fds = g_unix_fd_list_peek_fds (message->priv->fd_list, &num_fds);
2645 for (n = 0; n < num_fds; n++)
2648 struct stat statbuf;
2649 fs = g_string_new (NULL);
2650 if (fstat (fds[n], &statbuf) == 0)
2652 g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "",
2653 major (statbuf.st_dev), minor (statbuf.st_dev));
2654 g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "",
2656 g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2657 (guint64) statbuf.st_ino);
2658 g_string_append_printf (fs, "%s" "uid=%d", fs->len > 0 ? "," : "",
2660 g_string_append_printf (fs, "%s" "gid=%d", fs->len > 0 ? "," : "",
2662 g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
2663 major (statbuf.st_rdev), minor (statbuf.st_rdev));
2664 g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2665 (guint64) statbuf.st_size);
2666 g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2667 (guint64) statbuf.st_atime);
2668 g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2669 (guint64) statbuf.st_mtime);
2670 g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
2671 (guint64) statbuf.st_ctime);
2675 g_string_append_printf (fs, "(fstat failed: %s)", strerror (errno));
2677 g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str);
2678 g_string_free (fs, TRUE);
2683 g_string_append_printf (str, "%*s (empty)\n", indent, "");
2688 g_string_append_printf (str, "%*s (none)\n", indent, "");
2692 return g_string_free (str, FALSE);
2696 #define __G_DBUS_MESSAGE_C__
2697 #include "gioaliasdef.c"