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>
23 /* Uncomment to debug serializer code */
24 /* #define DEBUG_SERIALIZER */
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_MKDEV_H
33 #include <sys/mkdev.h>
39 #include "gdbusutils.h"
40 #include "gdbusmessage.h"
41 #include "gdbuserror.h"
42 #include "gioenumtypes.h"
43 #include "ginputstream.h"
44 #include "gdatainputstream.h"
45 #include "gmemoryinputstream.h"
46 #include "goutputstream.h"
47 #include "gdataoutputstream.h"
48 #include "gmemoryoutputstream.h"
49 #include "gseekable.h"
51 #include "gdbusprivate.h"
54 #include "gunixfdlist.h"
60 * SECTION:gdbusmessage
61 * @short_description: D-Bus Message
64 * A type for representing D-Bus messages that can be sent or received
65 * on a #GDBusConnection.
68 typedef struct _GDBusMessageClass GDBusMessageClass;
73 * Class structure for #GDBusMessage.
77 struct _GDBusMessageClass
80 GObjectClass parent_class;
86 * The #GDBusMessage structure contains only private data and should
87 * only be accessed using the provided API.
94 GObject parent_instance;
96 GDBusMessageType type;
97 GDBusMessageFlags flags;
99 GDBusMessageByteOrder byte_order;
100 guchar major_protocol_version;
105 GUnixFDList *fd_list;
115 G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT);
118 g_dbus_message_finalize (GObject *object)
120 GDBusMessage *message = G_DBUS_MESSAGE (object);
122 if (message->headers != NULL)
123 g_hash_table_unref (message->headers);
124 if (message->body != NULL)
125 g_variant_unref (message->body);
127 if (message->fd_list != NULL)
128 g_object_unref (message->fd_list);
131 if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL)
132 G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object);
136 g_dbus_message_get_property (GObject *object,
141 GDBusMessage *message = G_DBUS_MESSAGE (object);
146 g_value_set_boolean (value, g_dbus_message_get_locked (message));
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156 g_dbus_message_class_init (GDBusMessageClass *klass)
158 GObjectClass *gobject_class;
160 gobject_class = G_OBJECT_CLASS (klass);
161 gobject_class->finalize = g_dbus_message_finalize;
162 gobject_class->get_property = g_dbus_message_get_property;
165 * GDBusConnection:locked:
167 * A boolean specifying whether the message is locked.
171 g_object_class_install_property (gobject_class,
173 g_param_spec_boolean ("locked",
175 P_("Whether the message is locked"),
178 G_PARAM_STATIC_NAME |
179 G_PARAM_STATIC_BLURB |
180 G_PARAM_STATIC_NICK));
184 g_dbus_message_init (GDBusMessage *message)
186 /* Any D-Bus implementation is supposed to handle both Big and
187 * Little Endian encodings and the Endianness is part of the D-Bus
188 * message - we prefer to use Big Endian (since it's Network Byte
189 * Order and just easier to read for humans) but if the machine is
190 * Little Endian we use that for performance reasons.
192 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
193 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
195 /* this could also be G_PDP_ENDIAN */
196 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
198 message->headers = g_hash_table_new_full (g_direct_hash,
201 (GDestroyNotify) g_variant_unref);
205 * g_dbus_message_new:
207 * Creates a new empty #GDBusMessage.
209 * Returns: A #GDBusMessage. Free with g_object_unref().
214 g_dbus_message_new (void)
216 return g_object_new (G_TYPE_DBUS_MESSAGE, NULL);
220 * g_dbus_message_new_method_call:
221 * @name: (allow-none): A valid D-Bus name or %NULL.
222 * @path: A valid object path.
223 * @interface_: (allow-none): A valid D-Bus interface name or %NULL.
224 * @method: A valid method name.
226 * Creates a new #GDBusMessage for a method call.
228 * Returns: A #GDBusMessage. Free with g_object_unref().
233 g_dbus_message_new_method_call (const gchar *name,
235 const gchar *interface_,
238 GDBusMessage *message;
240 g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL);
241 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
242 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
243 g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
245 message = g_dbus_message_new ();
246 message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
249 g_dbus_message_set_destination (message, name);
250 g_dbus_message_set_path (message, path);
251 g_dbus_message_set_member (message, method);
252 if (interface_ != NULL)
253 g_dbus_message_set_interface (message, interface_);
259 * g_dbus_message_new_signal:
260 * @path: A valid object path.
261 * @interface_: A valid D-Bus interface name.
262 * @signal: A valid signal name.
264 * Creates a new #GDBusMessage for a signal emission.
266 * Returns: A #GDBusMessage. Free with g_object_unref().
271 g_dbus_message_new_signal (const gchar *path,
272 const gchar *interface_,
275 GDBusMessage *message;
277 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
278 g_return_val_if_fail (g_dbus_is_member_name (signal), NULL);
279 g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL);
281 message = g_dbus_message_new ();
282 message->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
283 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
285 g_dbus_message_set_path (message, path);
286 g_dbus_message_set_member (message, signal);
287 g_dbus_message_set_interface (message, interface_);
294 * g_dbus_message_new_method_reply:
295 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
296 * create a reply message to.
298 * Creates a new #GDBusMessage that is a reply to @method_call_message.
300 * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
305 g_dbus_message_new_method_reply (GDBusMessage *method_call_message)
307 GDBusMessage *message;
310 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
311 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
312 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
314 message = g_dbus_message_new ();
315 message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
316 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
317 /* reply with same endianness */
318 message->byte_order = method_call_message->byte_order;
320 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
321 sender = g_dbus_message_get_sender (method_call_message);
323 g_dbus_message_set_destination (message, sender);
329 * g_dbus_message_new_method_error:
330 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
331 * create a reply message to.
332 * @error_name: A valid D-Bus error name.
333 * @error_message_format: The D-Bus error message in a printf() format.
334 * @...: Arguments for @error_message_format.
336 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
338 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
343 g_dbus_message_new_method_error (GDBusMessage *method_call_message,
344 const gchar *error_name,
345 const gchar *error_message_format,
351 va_start (var_args, error_message_format);
352 ret = g_dbus_message_new_method_error_valist (method_call_message,
354 error_message_format,
362 * g_dbus_message_new_method_error_literal:
363 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
364 * create a reply message to.
365 * @error_name: A valid D-Bus error name.
366 * @error_message: The D-Bus error message.
368 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
370 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
375 g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message,
376 const gchar *error_name,
377 const gchar *error_message)
379 GDBusMessage *message;
382 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
383 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
384 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
385 g_return_val_if_fail (g_dbus_is_name (error_name), NULL);
386 g_return_val_if_fail (error_message != NULL, NULL);
388 message = g_dbus_message_new ();
389 message->type = G_DBUS_MESSAGE_TYPE_ERROR;
390 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
391 /* reply with same endianness */
392 message->byte_order = method_call_message->byte_order;
394 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
395 g_dbus_message_set_error_name (message, error_name);
396 g_dbus_message_set_body (message, g_variant_new ("(s)", error_message));
398 sender = g_dbus_message_get_sender (method_call_message);
400 g_dbus_message_set_destination (message, sender);
406 * g_dbus_message_new_method_error_valist:
407 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
408 * create a reply message to.
409 * @error_name: A valid D-Bus error name.
410 * @error_message_format: The D-Bus error message in a printf() format.
411 * @var_args: Arguments for @error_message_format.
413 * Like g_dbus_message_new_method_error() but intended for language bindings.
415 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
420 g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message,
421 const gchar *error_name,
422 const gchar *error_message_format,
426 gchar *error_message;
427 error_message = g_strdup_vprintf (error_message_format, var_args);
428 ret = g_dbus_message_new_method_error_literal (method_call_message,
431 g_free (error_message);
435 /* ---------------------------------------------------------------------------------------------------- */
438 * g_dbus_message_get_byte_order:
439 * @message: A #GDBusMessage.
441 * Gets the byte order of @message.
443 * Returns: The byte order.
445 GDBusMessageByteOrder
446 g_dbus_message_get_byte_order (GDBusMessage *message)
448 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0);
449 return message->byte_order;
453 * g_dbus_message_set_byte_order:
454 * @message: A #GDBusMessage.
455 * @byte_order: The byte order.
457 * Sets the byte order of @message.
460 g_dbus_message_set_byte_order (GDBusMessage *message,
461 GDBusMessageByteOrder byte_order)
463 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
467 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
471 message->byte_order = byte_order;
474 /* ---------------------------------------------------------------------------------------------------- */
476 /* TODO: need GI annotations to specify that any guchar value goes for the type */
479 * g_dbus_message_get_message_type:
480 * @message: A #GDBusMessage.
482 * Gets the type of @message.
484 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
489 g_dbus_message_get_message_type (GDBusMessage *message)
491 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID);
492 return message->type;
496 * g_dbus_message_set_message_type:
497 * @message: A #GDBusMessage.
498 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
500 * Sets @message to be of @type.
505 g_dbus_message_set_message_type (GDBusMessage *message,
506 GDBusMessageType type)
508 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
509 g_return_if_fail (type >=0 && type < 256);
513 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
517 message->type = type;
520 /* ---------------------------------------------------------------------------------------------------- */
522 /* TODO: need GI annotations to specify that any guchar value goes for flags */
525 * g_dbus_message_get_flags:
526 * @message: A #GDBusMessage.
528 * Gets the flags for @message.
530 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
535 g_dbus_message_get_flags (GDBusMessage *message)
537 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE);
538 return message->flags;
542 * g_dbus_message_set_flags:
543 * @message: A #GDBusMessage.
544 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
545 * enumeration bitwise ORed together).
547 * Sets the flags to set on @message.
552 g_dbus_message_set_flags (GDBusMessage *message,
553 GDBusMessageFlags flags)
555 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
556 g_return_if_fail (flags >=0 && flags < 256);
560 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
564 message->flags = flags;
567 /* ---------------------------------------------------------------------------------------------------- */
570 * g_dbus_message_get_serial:
571 * @message: A #GDBusMessage.
573 * Gets the serial for @message.
575 * Returns: A #guint32.
580 g_dbus_message_get_serial (GDBusMessage *message)
582 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
583 return message->serial;
587 * g_dbus_message_set_serial:
588 * @message: A #GDBusMessage.
589 * @serial: A #guint32.
591 * Sets the serial for @message.
596 g_dbus_message_set_serial (GDBusMessage *message,
599 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
603 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
607 message->serial = serial;
610 /* ---------------------------------------------------------------------------------------------------- */
612 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
615 * g_dbus_message_get_header:
616 * @message: A #GDBusMessage.
617 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
619 * Gets a header field on @message.
621 * Returns: A #GVariant with the value if the header was found, %NULL
622 * otherwise. Do not free, it is owned by @message.
627 g_dbus_message_get_header (GDBusMessage *message,
628 GDBusMessageHeaderField header_field)
630 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
631 g_return_val_if_fail (header_field >=0 && header_field < 256, NULL);
632 return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
636 * g_dbus_message_set_header:
637 * @message: A #GDBusMessage.
638 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
639 * @value: (allow-none): A #GVariant to set the header field or %NULL to clear the header field.
641 * Sets a header field on @message.
643 * If @value is floating, @message assumes ownership of @value.
648 g_dbus_message_set_header (GDBusMessage *message,
649 GDBusMessageHeaderField header_field,
652 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
653 g_return_if_fail (header_field >=0 && header_field < 256);
657 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
663 g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field));
667 g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
672 * g_dbus_message_get_header_fields:
673 * @message: A #GDBusMessage.
675 * Gets an array of all header fields on @message that are set.
677 * Returns: (array zero-terminated=1): An array of header fields
678 * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
679 * is a #guchar. Free with g_free().
684 g_dbus_message_get_header_fields (GDBusMessage *message)
692 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
694 keys = g_hash_table_get_keys (message->headers);
695 num_keys = g_list_length (keys);
696 ret = g_new (guchar, num_keys + 1);
697 for (l = keys, n = 0; l != NULL; l = l->next, n++)
698 ret[n] = GPOINTER_TO_UINT (l->data);
699 g_assert (n == num_keys);
700 ret[n] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID;
706 /* ---------------------------------------------------------------------------------------------------- */
709 * g_dbus_message_get_body:
710 * @message: A #GDBusMessage.
712 * Gets the body of a message.
714 * Returns: A #GVariant or %NULL if the body is empty. Do not free, it is owned by @message.
719 g_dbus_message_get_body (GDBusMessage *message)
721 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
722 return message->body;
726 * g_dbus_message_set_body:
727 * @message: A #GDBusMessage.
728 * @body: Either %NULL or a #GVariant that is a tuple.
730 * Sets the body @message. As a side-effect the
731 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
732 * type string of @body (or cleared if @body is %NULL).
734 * If @body is floating, @message assumes ownership of @body.
739 g_dbus_message_set_body (GDBusMessage *message,
742 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
743 g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE));
747 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
751 if (message->body != NULL)
752 g_variant_unref (message->body);
755 message->body = NULL;
756 g_dbus_message_set_signature (message, NULL);
760 const gchar *type_string;
761 gsize type_string_len;
764 message->body = g_variant_ref_sink (body);
766 type_string = g_variant_get_type_string (body);
767 type_string_len = strlen (type_string);
768 g_assert (type_string_len >= 2);
769 signature = g_strndup (type_string + 1, type_string_len - 2);
770 g_dbus_message_set_signature (message, signature);
775 /* ---------------------------------------------------------------------------------------------------- */
779 * g_dbus_message_get_unix_fd_list:
780 * @message: A #GDBusMessage.
782 * Gets the UNIX file descriptors associated with @message, if any.
784 * This method is only available on UNIX.
786 * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
787 * associated. Do not free, this object is owned by @message.
792 g_dbus_message_get_unix_fd_list (GDBusMessage *message)
794 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
795 return message->fd_list;
799 * g_dbus_message_set_unix_fd_list:
800 * @message: A #GDBusMessage.
801 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
803 * Sets the UNIX file descriptors associated with @message. As a
804 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
805 * field is set to the number of fds in @fd_list (or cleared if
806 * @fd_list is %NULL).
808 * This method is only available on UNIX.
813 g_dbus_message_set_unix_fd_list (GDBusMessage *message,
814 GUnixFDList *fd_list)
816 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
817 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
821 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
825 if (message->fd_list != NULL)
826 g_object_unref (message->fd_list);
829 message->fd_list = g_object_ref (fd_list);
830 g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list));
834 message->fd_list = NULL;
835 g_dbus_message_set_num_unix_fds (message, 0);
840 /* ---------------------------------------------------------------------------------------------------- */
843 validate_headers (GDBusMessage *message,
848 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
849 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
853 switch (message->type)
855 case G_DBUS_MESSAGE_TYPE_INVALID:
856 g_set_error_literal (error,
858 G_IO_ERROR_INVALID_ARGUMENT,
859 _("type is INVALID"));
863 case G_DBUS_MESSAGE_TYPE_METHOD_CALL:
864 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
865 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
867 g_set_error_literal (error,
869 G_IO_ERROR_INVALID_ARGUMENT,
870 _("METHOD_CALL message: PATH or MEMBER header field is missing"));
875 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
876 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
878 g_set_error_literal (error,
880 G_IO_ERROR_INVALID_ARGUMENT,
881 _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
886 case G_DBUS_MESSAGE_TYPE_ERROR:
887 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME) == NULL ||
888 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
890 g_set_error_literal (error,
892 G_IO_ERROR_INVALID_ARGUMENT,
893 _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
898 case G_DBUS_MESSAGE_TYPE_SIGNAL:
899 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
900 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE) == NULL ||
901 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
903 g_set_error_literal (error,
905 G_IO_ERROR_INVALID_ARGUMENT,
906 _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
909 if (g_strcmp0 (g_dbus_message_get_path (message), "/org/freedesktop/DBus/Local") == 0)
911 g_set_error_literal (error,
913 G_IO_ERROR_INVALID_ARGUMENT,
914 _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
917 if (g_strcmp0 (g_dbus_message_get_interface (message), "org.freedesktop.DBus.Local") == 0)
919 g_set_error_literal (error,
921 G_IO_ERROR_INVALID_ARGUMENT,
922 _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
928 /* hitherto unknown type - nothing to check */
935 g_assert (ret || (error == NULL || *error != NULL));
939 /* ---------------------------------------------------------------------------------------------------- */
942 ensure_input_padding (GMemoryInputStream *mis,
949 offset = g_seekable_tell (G_SEEKABLE (mis));
950 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
952 if (offset != wanted_offset)
954 return g_seekable_seek (G_SEEKABLE (mis), wanted_offset, G_SEEK_SET, NULL, error);
963 read_string (GMemoryInputStream *mis,
964 GDataInputStream *dis,
973 const gchar *end_valid;
975 s = g_string_new (NULL);
978 while (remaining > 0)
983 to_read = MIN (remaining, sizeof (buf));
984 num_read = g_input_stream_read (G_INPUT_STREAM (mis),
993 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
996 G_IO_ERROR_INVALID_ARGUMENT,
997 g_dngettext (GETTEXT_PACKAGE,
998 "Wanted to read %lu byte but got EOF",
999 "Wanted to read %lu bytes but got EOF",
1005 remaining -= num_read;
1006 g_string_append_len (s, buf, num_read);
1010 nul = g_data_input_stream_read_byte (dis, NULL, &local_error);
1011 if (local_error != NULL)
1013 g_propagate_error (error, local_error);
1016 if (!g_utf8_validate (s->str, -1, &end_valid))
1020 offset = (gint) (end_valid - s->str);
1021 valid_str = g_strndup (s->str, offset);
1024 G_IO_ERROR_INVALID_ARGUMENT,
1025 _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
1026 "The valid UTF-8 string up until that point was `%s'"),
1037 G_IO_ERROR_INVALID_ARGUMENT,
1038 _("Expected NUL byte after the string `%s' but found byte %d"),
1043 return g_string_free (s, FALSE);
1046 g_string_free (s, TRUE);
1050 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
1052 /* returns a non-floating GVariant! */
1054 parse_value_from_blob (GMemoryInputStream *mis,
1055 GDataInputStream *dis,
1056 const GVariantType *type,
1057 gboolean just_align,
1062 GError *local_error;
1064 const gchar *type_string;
1066 type_string = g_variant_type_peek_string (type);
1068 #ifdef DEBUG_SERIALIZER
1071 s = g_variant_type_dup_string (type);
1072 g_print ("%*s%s type %s from offset 0x%04x",
1074 just_align ? "Aligning" : "Reading",
1076 (gint) g_seekable_tell (G_SEEKABLE (mis)));
1079 #endif /* DEBUG_SERIALIZER */
1085 switch (type_string[0])
1087 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1088 if (!ensure_input_padding (mis, 4, &local_error))
1093 v = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
1094 if (local_error != NULL)
1096 ret = g_variant_new_boolean (v);
1100 case 'y': /* G_VARIANT_TYPE_BYTE */
1104 v = g_data_input_stream_read_byte (dis, NULL, &local_error);
1105 if (local_error != NULL)
1107 ret = g_variant_new_byte (v);
1111 case 'n': /* G_VARIANT_TYPE_INT16 */
1112 if (!ensure_input_padding (mis, 2, &local_error))
1117 v = g_data_input_stream_read_int16 (dis, NULL, &local_error);
1118 if (local_error != NULL)
1120 ret = g_variant_new_int16 (v);
1124 case 'q': /* G_VARIANT_TYPE_UINT16 */
1125 if (!ensure_input_padding (mis, 2, &local_error))
1130 v = g_data_input_stream_read_uint16 (dis, NULL, &local_error);
1131 if (local_error != NULL)
1133 ret = g_variant_new_uint16 (v);
1137 case 'i': /* G_VARIANT_TYPE_INT32 */
1138 if (!ensure_input_padding (mis, 4, &local_error))
1143 v = g_data_input_stream_read_int32 (dis, NULL, &local_error);
1144 if (local_error != NULL)
1146 ret = g_variant_new_int32 (v);
1150 case 'u': /* G_VARIANT_TYPE_UINT32 */
1151 if (!ensure_input_padding (mis, 4, &local_error))
1156 v = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
1157 if (local_error != NULL)
1159 ret = g_variant_new_uint32 (v);
1163 case 'x': /* G_VARIANT_TYPE_INT64 */
1164 if (!ensure_input_padding (mis, 8, &local_error))
1169 v = g_data_input_stream_read_int64 (dis, NULL, &local_error);
1170 if (local_error != NULL)
1172 ret = g_variant_new_int64 (v);
1176 case 't': /* G_VARIANT_TYPE_UINT64 */
1177 if (!ensure_input_padding (mis, 8, &local_error))
1182 v = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
1183 if (local_error != NULL)
1185 ret = g_variant_new_uint64 (v);
1189 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1190 if (!ensure_input_padding (mis, 8, &local_error))
1198 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
1199 u.v_uint64 = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
1200 if (local_error != NULL)
1202 ret = g_variant_new_double (u.v_double);
1206 case 's': /* G_VARIANT_TYPE_STRING */
1207 if (!ensure_input_padding (mis, 4, &local_error))
1213 len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
1214 if (local_error != NULL)
1216 v = read_string (mis, dis, (gsize) len, &local_error);
1219 ret = g_variant_new_string (v);
1224 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1225 if (!ensure_input_padding (mis, 4, &local_error))
1231 len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
1232 if (local_error != NULL)
1234 v = read_string (mis, dis, (gsize) len, &local_error);
1237 if (!g_variant_is_object_path (v))
1239 g_set_error (&local_error,
1241 G_IO_ERROR_INVALID_ARGUMENT,
1242 _("Parsed value `%s' is not a valid D-Bus object path"),
1247 ret = g_variant_new_object_path (v);
1252 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1257 len = g_data_input_stream_read_byte (dis, NULL, &local_error);
1258 if (local_error != NULL)
1260 v = read_string (mis, dis, (gsize) len, &local_error);
1263 if (!g_variant_is_signature (v))
1265 g_set_error (&local_error,
1267 G_IO_ERROR_INVALID_ARGUMENT,
1268 _("Parsed value `%s' is not a valid D-Bus signature"),
1273 ret = g_variant_new_signature (v);
1278 case 'h': /* G_VARIANT_TYPE_HANDLE */
1279 if (!ensure_input_padding (mis, 4, &local_error))
1284 v = g_data_input_stream_read_int32 (dis, NULL, &local_error);
1285 if (local_error != NULL)
1287 ret = g_variant_new_handle (v);
1291 case 'a': /* G_VARIANT_TYPE_ARRAY */
1292 if (!ensure_input_padding (mis, 4, &local_error))
1295 /* If we are only aligning for this array type, it is the child type of
1296 * another array, which is empty. So, we do not need to add padding for
1297 * this nonexistent array's elements: we only need to align for this
1298 * array itself (4 bytes). See
1299 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1306 const GVariantType *element_type;
1307 GVariantBuilder builder;
1309 array_len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
1310 if (local_error != NULL)
1314 #ifdef DEBUG_SERIALIZER
1315 g_print (": array spans 0x%04x bytes\n", array_len);
1316 #endif /* DEBUG_SERIALIZER */
1318 if (array_len > (2<<26))
1320 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1321 g_set_error (&local_error,
1323 G_IO_ERROR_INVALID_ARGUMENT,
1324 g_dngettext (GETTEXT_PACKAGE,
1325 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1326 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1332 g_variant_builder_init (&builder, type);
1333 element_type = g_variant_type_element (type);
1338 item = parse_value_from_blob (mis,
1344 g_assert (item == NULL);
1348 /* TODO: optimize array of primitive types */
1349 offset = g_seekable_tell (G_SEEKABLE (mis));
1350 target = offset + array_len;
1351 while (offset < target)
1354 item = parse_value_from_blob (mis,
1362 g_variant_builder_clear (&builder);
1365 g_variant_builder_add_value (&builder, item);
1366 g_variant_unref (item);
1367 offset = g_seekable_tell (G_SEEKABLE (mis));
1371 ret = g_variant_builder_end (&builder);
1376 if (g_variant_type_is_dict_entry (type))
1378 const GVariantType *key_type;
1379 const GVariantType *value_type;
1383 if (!ensure_input_padding (mis, 8, &local_error))
1387 #ifdef DEBUG_SERIALIZER
1389 #endif /* DEBUG_SERIALIZER */
1393 key_type = g_variant_type_key (type);
1394 key = parse_value_from_blob (mis,
1402 value_type = g_variant_type_value (type);
1403 value = parse_value_from_blob (mis,
1411 g_variant_unref (key);
1414 ret = g_variant_new_dict_entry (key, value);
1415 g_variant_unref (key);
1416 g_variant_unref (value);
1419 else if (g_variant_type_is_tuple (type))
1421 if (!ensure_input_padding (mis, 8, &local_error))
1425 #ifdef DEBUG_SERIALIZER
1427 #endif /* DEBUG_SERIALIZER */
1431 const GVariantType *element_type;
1432 GVariantBuilder builder;
1434 g_variant_builder_init (&builder, type);
1435 element_type = g_variant_type_first (type);
1436 while (element_type != NULL)
1439 item = parse_value_from_blob (mis,
1447 g_variant_builder_clear (&builder);
1450 g_variant_builder_add_value (&builder, item);
1451 g_variant_unref (item);
1453 element_type = g_variant_type_next (element_type);
1455 ret = g_variant_builder_end (&builder);
1458 else if (g_variant_type_is_variant (type))
1461 #ifdef DEBUG_SERIALIZER
1463 #endif /* DEBUG_SERIALIZER */
1469 GVariantType *variant_type;
1472 siglen = g_data_input_stream_read_byte (dis, NULL, &local_error);
1473 if (local_error != NULL)
1475 sig = read_string (mis, dis, (gsize) siglen, &local_error);
1478 if (!g_variant_is_signature (sig))
1480 g_set_error (&local_error,
1482 G_IO_ERROR_INVALID_ARGUMENT,
1483 _("Parsed value `%s' for variant is not a valid D-Bus signature"),
1488 variant_type = g_variant_type_new (sig);
1490 value = parse_value_from_blob (mis,
1496 g_variant_type_free (variant_type);
1499 ret = g_variant_new_variant (value);
1500 g_variant_unref (value);
1506 s = g_variant_type_dup_string (type);
1507 g_set_error (&local_error,
1509 G_IO_ERROR_INVALID_ARGUMENT,
1510 _("Error deserializing GVariant with type string `%s' from the D-Bus wire format"),
1518 g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
1520 #ifdef DEBUG_SERIALIZER
1526 if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
1528 s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret));
1532 s = g_variant_print (ret, FALSE);
1534 g_print (": %s\n", s);
1539 is_leaf = is_leaf; /* To avoid -Wunused-but-set-variable */
1540 #endif /* DEBUG_SERIALIZER */
1542 /* sink the reference */
1545 g_assert (g_variant_is_floating (ret));
1546 g_variant_ref_sink (ret);
1551 #ifdef DEBUG_SERIALIZER
1553 "%*sFAILURE: %s (%s, %d)\n",
1555 local_error->message,
1556 g_quark_to_string (local_error->domain),
1558 #endif /* DEBUG_SERIALIZER */
1559 g_propagate_error (error, local_error);
1563 /* ---------------------------------------------------------------------------------------------------- */
1565 /* message_header must be at least 16 bytes */
1568 * g_dbus_message_bytes_needed:
1569 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1570 * @blob_len: The length of @blob (must be at least 16).
1571 * @error: Return location for error or %NULL.
1573 * Utility function to calculate how many bytes are needed to
1574 * completely deserialize the D-Bus message stored at @blob.
1576 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1577 * @blob contains invalid data or not enough data is available to
1578 * determine the size).
1583 g_dbus_message_bytes_needed (guchar *blob,
1591 g_return_val_if_fail (blob != NULL, -1);
1592 g_return_val_if_fail (error == NULL || *error == NULL, -1);
1593 g_return_val_if_fail (blob_len >= 16, -1);
1597 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1598 ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]);
1599 /* round up so it's a multiple of 8 */
1600 ret = 8 * ((ret + 7)/8);
1601 /* finally add the body size */
1602 ret += GUINT32_FROM_LE (((guint32 *) blob)[1]);
1604 else if (blob[0] == 'B')
1606 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1607 ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]);
1608 /* round up so it's a multiple of 8 */
1609 ret = 8 * ((ret + 7)/8);
1610 /* finally add the body size */
1611 ret += GUINT32_FROM_BE (((guint32 *) blob)[1]);
1617 G_IO_ERROR_INVALID_ARGUMENT,
1618 "Unable to determine message blob length - given blob is malformed");
1625 G_IO_ERROR_INVALID_ARGUMENT,
1626 "Blob indicates that message exceeds maximum message length (128MiB)");
1633 /* ---------------------------------------------------------------------------------------------------- */
1636 * g_dbus_message_new_from_blob:
1637 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1638 * @blob_len: The length of @blob.
1639 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1640 * @error: Return location for error or %NULL.
1642 * Creates a new #GDBusMessage from the data stored at @blob. The byte
1643 * order that the message was in can be retrieved using
1644 * g_dbus_message_get_byte_order().
1646 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
1652 g_dbus_message_new_from_blob (guchar *blob,
1654 GDBusCapabilityFlags capabilities,
1658 GMemoryInputStream *mis;
1659 GDataInputStream *dis;
1660 GDBusMessage *message;
1662 guchar major_protocol_version;
1663 GDataStreamByteOrder byte_order;
1664 guint32 message_body_len;
1668 GVariant *signature;
1670 /* TODO: check against @capabilities */
1674 g_return_val_if_fail (blob != NULL, NULL);
1675 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1676 g_return_val_if_fail (blob_len >= 12, NULL);
1678 message = g_dbus_message_new ();
1680 mis = G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (blob, blob_len, NULL));
1681 dis = g_data_input_stream_new (G_INPUT_STREAM (mis));
1683 endianness = g_data_input_stream_read_byte (dis, NULL, NULL);
1687 byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
1688 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
1691 byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
1692 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
1697 G_IO_ERROR_INVALID_ARGUMENT,
1698 _("Invalid endianness value. Expected 0x6c ('l') or 0x42 ('B') but found value 0x%02x"),
1702 g_data_input_stream_set_byte_order (dis, byte_order);
1704 message->type = g_data_input_stream_read_byte (dis, NULL, NULL);
1705 message->flags = g_data_input_stream_read_byte (dis, NULL, NULL);
1706 major_protocol_version = g_data_input_stream_read_byte (dis, NULL, NULL);
1707 if (major_protocol_version != 1)
1711 G_IO_ERROR_INVALID_ARGUMENT,
1712 _("Invalid major protocol version. Expected 1 but found %d"),
1713 major_protocol_version);
1716 message_body_len = g_data_input_stream_read_uint32 (dis, NULL, NULL);
1717 message->serial = g_data_input_stream_read_uint32 (dis, NULL, NULL);
1719 #ifdef DEBUG_SERIALIZER
1720 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len);
1723 s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2);
1724 g_print ("%s\n", s);
1727 #endif /* DEBUG_SERIALIZER */
1729 #ifdef DEBUG_SERIALIZER
1730 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len);
1731 #endif /* DEBUG_SERIALIZER */
1732 headers = parse_value_from_blob (mis,
1734 G_VARIANT_TYPE ("a{yv}"),
1738 if (headers == NULL)
1740 g_variant_iter_init (&iter, headers);
1741 while ((item = g_variant_iter_next_value (&iter)) != NULL)
1743 guchar header_field;
1745 g_variant_get (item,
1749 g_dbus_message_set_header (message, header_field, value);
1750 g_variant_unref (value);
1751 g_variant_unref (item);
1753 g_variant_unref (headers);
1755 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
1756 if (signature != NULL)
1758 const gchar *signature_str;
1759 gsize signature_str_len;
1761 signature_str = g_variant_get_string (signature, &signature_str_len);
1763 /* signature but no body */
1764 if (message_body_len == 0 && signature_str_len > 0)
1768 G_IO_ERROR_INVALID_ARGUMENT,
1769 _("Signature header with signature `%s' found but message body is empty"),
1773 else if (signature_str_len > 0)
1775 GVariantType *variant_type;
1776 gchar *tupled_signature_str;
1778 if (!g_variant_is_signature (signature_str))
1782 G_IO_ERROR_INVALID_ARGUMENT,
1783 _("Parsed value `%s' is not a valid D-Bus signature (for body)"),
1787 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
1788 variant_type = g_variant_type_new (tupled_signature_str);
1789 g_free (tupled_signature_str);
1790 #ifdef DEBUG_SERIALIZER
1791 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len);
1792 #endif /* DEBUG_SERIALIZER */
1793 message->body = parse_value_from_blob (mis,
1799 g_variant_type_free (variant_type);
1800 if (message->body == NULL)
1806 /* no signature, this is only OK if the body is empty */
1807 if (message_body_len != 0)
1809 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
1812 G_IO_ERROR_INVALID_ARGUMENT,
1813 g_dngettext (GETTEXT_PACKAGE,
1814 "No signature header in message but the message body is %u byte",
1815 "No signature header in message but the message body is %u bytes",
1822 if (!validate_headers (message, error))
1824 g_prefix_error (error, _("Cannot deserialize message: "));
1831 g_object_unref (dis);
1832 g_object_unref (mis);
1840 if (message != NULL)
1841 g_object_unref (message);
1846 /* ---------------------------------------------------------------------------------------------------- */
1849 ensure_output_padding (GMemoryOutputStream *mos,
1850 GDataOutputStream *dos,
1854 gsize wanted_offset;
1855 gsize padding_needed;
1858 offset = g_memory_output_stream_get_data_size (mos);
1859 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
1860 padding_needed = wanted_offset - offset;
1862 for (n = 0; n < padding_needed; n++)
1863 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1865 return padding_needed;
1868 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
1870 append_value_to_blob (GVariant *value,
1871 const GVariantType *type,
1872 GMemoryOutputStream *mos,
1873 GDataOutputStream *dos,
1874 gsize *out_padding_added,
1877 gsize padding_added;
1878 const gchar *type_string;
1880 type_string = g_variant_type_peek_string (type);
1884 switch (type_string[0])
1886 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1887 padding_added = ensure_output_padding (mos, dos, 4);
1890 gboolean v = g_variant_get_boolean (value);
1891 g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
1895 case 'y': /* G_VARIANT_TYPE_BYTE */
1898 guint8 v = g_variant_get_byte (value);
1899 g_data_output_stream_put_byte (dos, v, NULL, NULL);
1903 case 'n': /* G_VARIANT_TYPE_INT16 */
1904 padding_added = ensure_output_padding (mos, dos, 2);
1907 gint16 v = g_variant_get_int16 (value);
1908 g_data_output_stream_put_int16 (dos, v, NULL, NULL);
1912 case 'q': /* G_VARIANT_TYPE_UINT16 */
1913 padding_added = ensure_output_padding (mos, dos, 2);
1916 guint16 v = g_variant_get_uint16 (value);
1917 g_data_output_stream_put_uint16 (dos, v, NULL, NULL);
1921 case 'i': /* G_VARIANT_TYPE_INT32 */
1922 padding_added = ensure_output_padding (mos, dos, 4);
1925 gint32 v = g_variant_get_int32 (value);
1926 g_data_output_stream_put_int32 (dos, v, NULL, NULL);
1930 case 'u': /* G_VARIANT_TYPE_UINT32 */
1931 padding_added = ensure_output_padding (mos, dos, 4);
1934 guint32 v = g_variant_get_uint32 (value);
1935 g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
1939 case 'x': /* G_VARIANT_TYPE_INT64 */
1940 padding_added = ensure_output_padding (mos, dos, 8);
1943 gint64 v = g_variant_get_int64 (value);
1944 g_data_output_stream_put_int64 (dos, v, NULL, NULL);
1948 case 't': /* G_VARIANT_TYPE_UINT64 */
1949 padding_added = ensure_output_padding (mos, dos, 8);
1952 guint64 v = g_variant_get_uint64 (value);
1953 g_data_output_stream_put_uint64 (dos, v, NULL, NULL);
1957 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1958 padding_added = ensure_output_padding (mos, dos, 8);
1965 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
1966 u.v_double = g_variant_get_double (value);
1967 g_data_output_stream_put_uint64 (dos, u.v_uint64, NULL, NULL);
1971 case 's': /* G_VARIANT_TYPE_STRING */
1972 padding_added = ensure_output_padding (mos, dos, 4);
1978 v = g_variant_get_string (value, &len);
1979 g_assert (g_utf8_validate (v, -1, &end) && (end == v + len));
1980 g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
1981 g_data_output_stream_put_string (dos, v, NULL, NULL);
1982 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1986 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1987 padding_added = ensure_output_padding (mos, dos, 4);
1991 const gchar *v = g_variant_get_string (value, &len);
1992 g_assert (g_variant_is_object_path (v));
1993 g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
1994 g_data_output_stream_put_string (dos, v, NULL, NULL);
1995 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
1999 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2003 const gchar *v = g_variant_get_string (value, &len);
2004 g_assert (g_variant_is_signature (v));
2005 g_data_output_stream_put_byte (dos, len, NULL, NULL);
2006 g_data_output_stream_put_string (dos, v, NULL, NULL);
2007 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
2011 case 'h': /* G_VARIANT_TYPE_HANDLE */
2012 padding_added = ensure_output_padding (mos, dos, 4);
2015 gint32 v = g_variant_get_handle (value);
2016 g_data_output_stream_put_int32 (dos, v, NULL, NULL);
2020 case 'a': /* G_VARIANT_TYPE_ARRAY */
2024 goffset array_len_offset;
2025 goffset array_payload_begin_offset;
2029 padding_added = ensure_output_padding (mos, dos, 4);
2032 /* array length - will be filled in later */
2033 array_len_offset = g_memory_output_stream_get_data_size (mos);
2034 g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
2036 /* From the D-Bus spec:
2038 * "A UINT32 giving the length of the array data in bytes,
2039 * followed by alignment padding to the alignment boundary of
2040 * the array element type, followed by each array element. The
2041 * array length is from the end of the alignment padding to
2042 * the end of the last element, i.e. it does not include the
2043 * padding after the length, or any padding after the last
2046 * Thus, we need to count how much padding the first element
2047 * contributes and subtract that from the array length.
2049 array_payload_begin_offset = g_memory_output_stream_get_data_size (mos);
2051 if (g_variant_n_children (value) == 0)
2053 gsize padding_added_for_item;
2054 if (!append_value_to_blob (NULL,
2055 g_variant_type_element (type),
2058 &padding_added_for_item,
2061 array_payload_begin_offset += padding_added_for_item;
2067 g_variant_iter_init (&iter, value);
2068 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2070 gsize padding_added_for_item;
2071 if (!append_value_to_blob (item,
2072 g_variant_get_type (item),
2075 &padding_added_for_item,
2078 g_variant_unref (item);
2081 g_variant_unref (item);
2084 array_payload_begin_offset += padding_added_for_item;
2090 cur_offset = g_memory_output_stream_get_data_size (mos);
2091 array_len = cur_offset - array_payload_begin_offset;
2092 if (!g_seekable_seek (G_SEEKABLE (mos), array_len_offset, G_SEEK_SET, NULL, error))
2095 g_data_output_stream_put_uint32 (dos, array_len, NULL, NULL);
2096 if (!g_seekable_seek (G_SEEKABLE (mos), cur_offset, G_SEEK_SET, NULL, error))
2103 if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
2105 padding_added = ensure_output_padding (mos, dos, 8);
2110 g_variant_iter_init (&iter, value);
2111 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2113 if (!append_value_to_blob (item,
2114 g_variant_get_type (item),
2120 g_variant_unref (item);
2123 g_variant_unref (item);
2127 else if (g_variant_type_is_variant (type))
2132 const gchar *signature;
2133 child = g_variant_get_child_value (value, 0);
2134 signature = g_variant_get_type_string (child);
2135 g_data_output_stream_put_byte (dos, strlen (signature), NULL, NULL);
2136 g_data_output_stream_put_string (dos, signature, NULL, NULL);
2137 g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
2138 if (!append_value_to_blob (child,
2139 g_variant_get_type (child),
2145 g_variant_unref (child);
2148 g_variant_unref (child);
2155 G_IO_ERROR_INVALID_ARGUMENT,
2156 _("Error serializing GVariant with type string `%s' to the D-Bus wire format"),
2157 g_variant_get_type_string (value));
2163 if (out_padding_added != NULL)
2164 *out_padding_added = padding_added;
2173 append_body_to_blob (GVariant *value,
2174 GMemoryOutputStream *mos,
2175 GDataOutputStream *dos,
2181 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
2185 G_IO_ERROR_INVALID_ARGUMENT,
2186 "Expected a tuple for the body of the GDBusMessage.");
2190 g_variant_iter_init (&iter, value);
2191 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2193 if (!append_value_to_blob (item,
2194 g_variant_get_type (item),
2200 g_variant_unref (item);
2203 g_variant_unref (item);
2211 /* ---------------------------------------------------------------------------------------------------- */
2214 * g_dbus_message_to_blob:
2215 * @message: A #GDBusMessage.
2216 * @out_size: Return location for size of generated blob.
2217 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2218 * @error: Return location for error.
2220 * Serializes @message to a blob. The byte order returned by
2221 * g_dbus_message_get_byte_order() will be used.
2223 * Returns: (array length=out_size) (transfer full): A pointer to a
2224 * valid binary D-Bus message of @out_size bytes generated by @message
2225 * or %NULL if @error is set. Free with g_free().
2230 g_dbus_message_to_blob (GDBusMessage *message,
2232 GDBusCapabilityFlags capabilities,
2235 GMemoryOutputStream *mos;
2236 GDataOutputStream *dos;
2239 GDataStreamByteOrder byte_order;
2240 goffset body_len_offset;
2241 goffset body_start_offset;
2243 GVariant *header_fields;
2244 GVariantBuilder builder;
2245 GHashTableIter hash_iter;
2247 GVariant *header_value;
2248 GVariant *signature;
2249 const gchar *signature_str;
2250 gint num_fds_in_message;
2251 gint num_fds_according_to_header;
2253 /* TODO: check against @capabilities */
2257 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2258 g_return_val_if_fail (out_size != NULL, NULL);
2259 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2261 mos = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0, g_realloc, g_free));
2262 dos = g_data_output_stream_new (G_OUTPUT_STREAM (mos));
2264 byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
2265 switch (message->byte_order)
2267 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
2268 byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2270 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
2271 byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
2274 g_data_output_stream_set_byte_order (dos, byte_order);
2277 g_data_output_stream_put_byte (dos, (guchar) message->byte_order, NULL, NULL);
2278 g_data_output_stream_put_byte (dos, message->type, NULL, NULL);
2279 g_data_output_stream_put_byte (dos, message->flags, NULL, NULL);
2280 g_data_output_stream_put_byte (dos, 1, NULL, NULL); /* major protocol version */
2281 body_len_offset = g_memory_output_stream_get_data_size (mos);
2282 /* body length - will be filled in later */
2283 g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
2284 g_data_output_stream_put_uint32 (dos, message->serial, NULL, NULL);
2286 num_fds_in_message = 0;
2288 if (message->fd_list != NULL)
2289 num_fds_in_message = g_unix_fd_list_get_length (message->fd_list);
2291 num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
2292 if (num_fds_in_message != num_fds_according_to_header)
2296 G_IO_ERROR_INVALID_ARGUMENT,
2297 _("Message has %d file descriptors but the header field indicates %d file descriptors"),
2299 num_fds_according_to_header);
2303 if (!validate_headers (message, error))
2305 g_prefix_error (error, _("Cannot serialize message: "));
2309 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
2310 g_hash_table_iter_init (&hash_iter, message->headers);
2311 while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
2313 g_variant_builder_add (&builder,
2315 (guchar) GPOINTER_TO_UINT (key),
2318 header_fields = g_variant_builder_end (&builder);
2320 if (!append_value_to_blob (header_fields,
2321 g_variant_get_type (header_fields),
2326 g_variant_unref (header_fields);
2329 g_variant_unref (header_fields);
2331 /* header size must be a multiple of 8 */
2332 ensure_output_padding (mos, dos, 8);
2334 body_start_offset = g_memory_output_stream_get_data_size (mos);
2336 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2337 signature_str = NULL;
2338 if (signature != NULL)
2339 signature_str = g_variant_get_string (signature, NULL);
2340 if (message->body != NULL)
2342 gchar *tupled_signature_str;
2343 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2344 if (signature == NULL)
2348 G_IO_ERROR_INVALID_ARGUMENT,
2349 _("Message body has signature `%s' but there is no signature header"),
2351 g_free (tupled_signature_str);
2354 else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0)
2358 G_IO_ERROR_INVALID_ARGUMENT,
2359 _("Message body has type signature `%s' but signature in the header field is `%s'"),
2360 tupled_signature_str, g_variant_get_type_string (message->body));
2361 g_free (tupled_signature_str);
2364 g_free (tupled_signature_str);
2365 if (!append_body_to_blob (message->body, mos, dos, error))
2370 if (signature != NULL && strlen (signature_str) > 0)
2374 G_IO_ERROR_INVALID_ARGUMENT,
2375 _("Message body is empty but signature in the header field is `(%s)'"),
2381 /* OK, we're done writing the message - set the body length */
2382 size = g_memory_output_stream_get_data_size (mos);
2383 body_size = size - body_start_offset;
2385 if (!g_seekable_seek (G_SEEKABLE (mos), body_len_offset, G_SEEK_SET, NULL, error))
2388 g_data_output_stream_put_uint32 (dos, body_size, NULL, NULL);
2390 if (!g_output_stream_close (G_OUTPUT_STREAM (dos), NULL, error))
2394 ret = g_memory_output_stream_steal_data (mos);
2397 g_object_unref (dos);
2398 g_object_unref (mos);
2403 /* ---------------------------------------------------------------------------------------------------- */
2406 get_uint32_header (GDBusMessage *message,
2407 GDBusMessageHeaderField header_field)
2413 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2414 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
2415 ret = g_variant_get_uint32 (value);
2420 static const gchar *
2421 get_string_header (GDBusMessage *message,
2422 GDBusMessageHeaderField header_field)
2428 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2429 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
2430 ret = g_variant_get_string (value, NULL);
2435 static const gchar *
2436 get_object_path_header (GDBusMessage *message,
2437 GDBusMessageHeaderField header_field)
2443 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2444 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
2445 ret = g_variant_get_string (value, NULL);
2450 static const gchar *
2451 get_signature_header (GDBusMessage *message,
2452 GDBusMessageHeaderField header_field)
2458 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2459 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
2460 ret = g_variant_get_string (value, NULL);
2465 /* ---------------------------------------------------------------------------------------------------- */
2468 set_uint32_header (GDBusMessage *message,
2469 GDBusMessageHeaderField header_field,
2472 g_dbus_message_set_header (message,
2474 g_variant_new_uint32 (value));
2478 set_string_header (GDBusMessage *message,
2479 GDBusMessageHeaderField header_field,
2482 g_dbus_message_set_header (message,
2484 value == NULL ? NULL : g_variant_new_string (value));
2488 set_object_path_header (GDBusMessage *message,
2489 GDBusMessageHeaderField header_field,
2492 g_dbus_message_set_header (message,
2494 value == NULL ? NULL : g_variant_new_object_path (value));
2498 set_signature_header (GDBusMessage *message,
2499 GDBusMessageHeaderField header_field,
2502 g_dbus_message_set_header (message,
2504 value == NULL ? NULL : g_variant_new_signature (value));
2507 /* ---------------------------------------------------------------------------------------------------- */
2510 * g_dbus_message_get_reply_serial:
2511 * @message: A #GDBusMessage.
2513 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2515 * Returns: The value.
2520 g_dbus_message_get_reply_serial (GDBusMessage *message)
2522 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2523 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL);
2527 * g_dbus_message_set_reply_serial:
2528 * @message: A #GDBusMessage.
2529 * @value: The value to set.
2531 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2536 g_dbus_message_set_reply_serial (GDBusMessage *message,
2539 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2540 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value);
2543 /* ---------------------------------------------------------------------------------------------------- */
2546 * g_dbus_message_get_interface:
2547 * @message: A #GDBusMessage.
2549 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2551 * Returns: The value.
2556 g_dbus_message_get_interface (GDBusMessage *message)
2558 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2559 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE);
2563 * g_dbus_message_set_interface:
2564 * @message: A #GDBusMessage.
2565 * @value: The value to set.
2567 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2572 g_dbus_message_set_interface (GDBusMessage *message,
2575 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2576 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2577 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value);
2580 /* ---------------------------------------------------------------------------------------------------- */
2583 * g_dbus_message_get_member:
2584 * @message: A #GDBusMessage.
2586 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2588 * Returns: The value.
2593 g_dbus_message_get_member (GDBusMessage *message)
2595 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2596 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER);
2600 * g_dbus_message_set_member:
2601 * @message: A #GDBusMessage.
2602 * @value: The value to set.
2604 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2609 g_dbus_message_set_member (GDBusMessage *message,
2612 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2613 g_return_if_fail (value == NULL || g_dbus_is_member_name (value));
2614 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value);
2617 /* ---------------------------------------------------------------------------------------------------- */
2620 * g_dbus_message_get_path:
2621 * @message: A #GDBusMessage.
2623 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2625 * Returns: The value.
2630 g_dbus_message_get_path (GDBusMessage *message)
2632 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2633 return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH);
2637 * g_dbus_message_set_path:
2638 * @message: A #GDBusMessage.
2639 * @value: The value to set.
2641 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2646 g_dbus_message_set_path (GDBusMessage *message,
2649 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2650 g_return_if_fail (value == NULL || g_variant_is_object_path (value));
2651 set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value);
2654 /* ---------------------------------------------------------------------------------------------------- */
2657 * g_dbus_message_get_sender:
2658 * @message: A #GDBusMessage.
2660 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2662 * Returns: The value.
2667 g_dbus_message_get_sender (GDBusMessage *message)
2669 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2670 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER);
2674 * g_dbus_message_set_sender:
2675 * @message: A #GDBusMessage.
2676 * @value: The value to set.
2678 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2683 g_dbus_message_set_sender (GDBusMessage *message,
2686 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2687 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2688 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value);
2691 /* ---------------------------------------------------------------------------------------------------- */
2694 * g_dbus_message_get_destination:
2695 * @message: A #GDBusMessage.
2697 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2699 * Returns: The value.
2704 g_dbus_message_get_destination (GDBusMessage *message)
2706 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2707 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION);
2711 * g_dbus_message_set_destination:
2712 * @message: A #GDBusMessage.
2713 * @value: The value to set.
2715 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2720 g_dbus_message_set_destination (GDBusMessage *message,
2723 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2724 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2725 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value);
2728 /* ---------------------------------------------------------------------------------------------------- */
2731 * g_dbus_message_get_error_name:
2732 * @message: A #GDBusMessage.
2734 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
2736 * Returns: The value.
2741 g_dbus_message_get_error_name (GDBusMessage *message)
2743 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2744 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME);
2748 * g_dbus_message_set_error_name:
2749 * @message: A #GDBusMessage.
2750 * @value: The value to set.
2752 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
2757 g_dbus_message_set_error_name (GDBusMessage *message,
2760 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2761 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2762 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value);
2765 /* ---------------------------------------------------------------------------------------------------- */
2768 * g_dbus_message_get_signature:
2769 * @message: A #GDBusMessage.
2771 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
2773 * Returns: The value.
2778 g_dbus_message_get_signature (GDBusMessage *message)
2781 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2782 ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2789 * g_dbus_message_set_signature:
2790 * @message: A #GDBusMessage.
2791 * @value: The value to set.
2793 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
2798 g_dbus_message_set_signature (GDBusMessage *message,
2801 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2802 g_return_if_fail (value == NULL || g_variant_is_signature (value));
2803 set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value);
2806 /* ---------------------------------------------------------------------------------------------------- */
2809 * g_dbus_message_get_arg0:
2810 * @message: A #GDBusMessage.
2812 * Convenience to get the first item in the body of @message.
2814 * Returns: The string item or %NULL if the first item in the body of
2815 * @message is not a string.
2820 g_dbus_message_get_arg0 (GDBusMessage *message)
2824 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2828 if (message->body != NULL && g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE))
2831 item = g_variant_get_child_value (message->body, 0);
2832 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
2833 ret = g_variant_get_string (item, NULL);
2834 g_variant_unref (item);
2840 /* ---------------------------------------------------------------------------------------------------- */
2843 * g_dbus_message_get_num_unix_fds:
2844 * @message: A #GDBusMessage.
2846 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
2848 * Returns: The value.
2853 g_dbus_message_get_num_unix_fds (GDBusMessage *message)
2855 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2856 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS);
2860 * g_dbus_message_set_num_unix_fds:
2861 * @message: A #GDBusMessage.
2862 * @value: The value to set.
2864 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
2869 g_dbus_message_set_num_unix_fds (GDBusMessage *message,
2872 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2873 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value);
2876 /* ---------------------------------------------------------------------------------------------------- */
2879 * g_dbus_message_to_gerror:
2880 * @message: A #GDBusMessage.
2881 * @error: The #GError to set.
2883 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
2884 * nothing and returns %FALSE.
2886 * Otherwise this method encodes the error in @message as a #GError
2887 * using g_dbus_error_set_dbus_error() using the information in the
2888 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
2889 * well as the first string item in @message's body.
2891 * Returns: %TRUE if @error was set, %FALSE otherwise.
2896 g_dbus_message_to_gerror (GDBusMessage *message,
2900 const gchar *error_name;
2902 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
2905 if (message->type != G_DBUS_MESSAGE_TYPE_ERROR)
2908 error_name = g_dbus_message_get_error_name (message);
2909 if (error_name != NULL)
2913 body = g_dbus_message_get_body (message);
2915 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)")))
2917 const gchar *error_message;
2918 g_variant_get (body, "(&s)", &error_message);
2919 g_dbus_error_set_dbus_error (error,
2926 /* these two situations are valid, yet pretty rare */
2929 g_dbus_error_set_dbus_error (error,
2932 _("Error return with body of type `%s'"),
2933 g_variant_get_type_string (body));
2937 g_dbus_error_set_dbus_error (error,
2940 _("Error return with empty body"));
2946 /* TOOD: this shouldn't happen - should check this at message serialization
2947 * time and disconnect the peer.
2952 "Error return without error-name header!");
2961 /* ---------------------------------------------------------------------------------------------------- */
2964 flags_to_string (GType flags_type, guint value)
2970 klass = g_type_class_ref (flags_type);
2971 s = g_string_new (NULL);
2972 for (n = 0; n < 32; n++)
2974 if ((value & (1<<n)) != 0)
2976 GFlagsValue *flags_value;
2977 flags_value = g_flags_get_first_value (klass, (1<<n));
2979 g_string_append_c (s, ',');
2980 if (flags_value != NULL)
2981 g_string_append (s, flags_value->value_nick);
2983 g_string_append_printf (s, "unknown (bit %d)", n);
2987 g_string_append (s, "none");
2988 g_type_class_unref (klass);
2989 return g_string_free (s, FALSE);
2993 _sort_keys_func (gconstpointer a,
2999 ia = GPOINTER_TO_INT (a);
3000 ib = GPOINTER_TO_INT (b);
3006 * g_dbus_message_print:
3007 * @message: A #GDBusMessage.
3008 * @indent: Indentation level.
3010 * Produces a human-readable multi-line description of @message.
3012 * The contents of the description has no ABI guarantees, the contents
3013 * and formatting is subject to change at any time. Typical output
3014 * looks something like this:
3016 * Type: method-call
3021 * path -> objectpath '/org/gtk/GDBus/TestObject'
3022 * interface -> 'org.gtk.GDBus.TestInterface'
3023 * member -> 'GimmeStdout'
3024 * destination -> ':1.146'
3026 * UNIX File Descriptors:
3031 * Type: method-return
3032 * Flags: no-reply-expected
3036 * reply-serial -> uint32 4
3037 * destination -> ':1.159'
3038 * sender -> ':1.146'
3039 * num-unix-fds -> uint32 1
3041 * UNIX File Descriptors:
3042 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3045 * Returns: A string that should be freed with g_free().
3050 g_dbus_message_print (GDBusMessage *message,
3058 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3060 str = g_string_new (NULL);
3062 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type);
3063 g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
3065 s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags);
3066 g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
3068 g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version);
3069 g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial);
3071 g_string_append_printf (str, "%*sHeaders:\n", indent, "");
3072 keys = g_hash_table_get_keys (message->headers);
3073 keys = g_list_sort (keys, _sort_keys_func);
3076 for (l = keys; l != NULL; l = l->next)
3078 gint key = GPOINTER_TO_INT (l->data);
3082 value = g_hash_table_lookup (message->headers, l->data);
3083 g_assert (value != NULL);
3085 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
3086 value_str = g_variant_print (value, TRUE);
3087 g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
3094 g_string_append_printf (str, "%*s (none)\n", indent, "");
3096 g_string_append_printf (str, "%*sBody: ", indent, "");
3097 if (message->body != NULL)
3099 g_variant_print_string (message->body,
3105 g_string_append (str, "()");
3107 g_string_append (str, "\n");
3109 g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
3110 if (message->fd_list != NULL)
3116 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3119 for (n = 0; n < num_fds; n++)
3122 struct stat statbuf;
3123 fs = g_string_new (NULL);
3124 if (fstat (fds[n], &statbuf) == 0)
3126 g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "",
3127 major (statbuf.st_dev), minor (statbuf.st_dev));
3128 g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "",
3130 g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3131 (guint64) statbuf.st_ino);
3132 g_string_append_printf (fs, "%s" "uid=%d", fs->len > 0 ? "," : "",
3134 g_string_append_printf (fs, "%s" "gid=%d", fs->len > 0 ? "," : "",
3136 g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
3137 major (statbuf.st_rdev), minor (statbuf.st_rdev));
3138 g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3139 (guint64) statbuf.st_size);
3140 g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3141 (guint64) statbuf.st_atime);
3142 g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3143 (guint64) statbuf.st_mtime);
3144 g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3145 (guint64) statbuf.st_ctime);
3149 g_string_append_printf (fs, "(fstat failed: %s)", strerror (errno));
3151 g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str);
3152 g_string_free (fs, TRUE);
3157 g_string_append_printf (str, "%*s (empty)\n", indent, "");
3162 g_string_append_printf (str, "%*s (none)\n", indent, "");
3166 return g_string_free (str, FALSE);
3170 * g_dbus_message_get_locked:
3171 * @message: A #GDBusMessage.
3173 * Checks whether @message is locked. To monitor changes to this
3174 * value, conncet to the #GObject::notify signal to listen for changes
3175 * on the #GDBusMessage:locked property.
3177 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3182 g_dbus_message_get_locked (GDBusMessage *message)
3184 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3185 return message->locked;
3189 * g_dbus_message_lock:
3190 * @message: A #GDBusMessage.
3192 * If @message is locked, does nothing. Otherwise locks the message.
3197 g_dbus_message_lock (GDBusMessage *message)
3199 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3201 if (message->locked)
3204 message->locked = TRUE;
3205 g_object_notify (G_OBJECT (message), "locked");
3212 * g_dbus_message_copy:
3213 * @message: A #GDBusMessage.
3214 * @error: Return location for error or %NULL.
3216 * Copies @message. The copy is a deep copy and the returned
3217 * #GDBusMessage is completely identical except that it is guaranteed
3220 * This operation can fail if e.g. @message contains file descriptors
3221 * and the per-process or system-wide open files limit is reached.
3223 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3224 * Free with g_object_unref().
3229 g_dbus_message_copy (GDBusMessage *message,
3233 GHashTableIter iter;
3234 gpointer header_key;
3235 GVariant *header_value;
3237 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3238 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3240 ret = g_dbus_message_new ();
3241 ret->type = message->type;
3242 ret->flags = message->flags;
3243 ret->byte_order = message->byte_order;
3244 ret->major_protocol_version = message->major_protocol_version;
3245 ret->serial = message->serial;
3248 if (message->fd_list != NULL)
3254 ret->fd_list = g_unix_fd_list_new ();
3255 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3256 for (n = 0; n < num_fds; n++)
3258 if (g_unix_fd_list_append (ret->fd_list,
3262 g_object_unref (ret);
3270 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3271 * to just ref (as opposed to deep-copying) the GVariant instances
3273 ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL;
3274 g_hash_table_iter_init (&iter, message->headers);
3275 while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value))
3276 g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value));