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"
59 typedef struct _GMemoryBuffer GMemoryBuffer;
66 GDataStreamByteOrder byte_order;
70 g_memory_buffer_read_byte (GMemoryBuffer *mbuf,
73 if (mbuf->pos >= mbuf->valid_len)
75 return mbuf->data [mbuf->pos++];
79 g_memory_buffer_read_int16 (GMemoryBuffer *mbuf,
84 if (mbuf->pos > mbuf->valid_len - 2)
86 mbuf->pos = mbuf->valid_len;
90 memcpy (&v, mbuf->data + mbuf->pos, 2);
92 switch (mbuf->byte_order)
94 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
95 v = GINT16_FROM_BE (v);
97 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
98 v = GINT16_FROM_LE (v);
100 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
108 g_memory_buffer_read_uint16 (GMemoryBuffer *mbuf,
113 if (mbuf->pos > mbuf->valid_len - 2)
115 mbuf->pos = mbuf->valid_len;
119 memcpy (&v, mbuf->data + mbuf->pos, 2);
121 switch (mbuf->byte_order)
123 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
124 v = GINT16_FROM_BE (v);
126 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
127 v = GINT16_FROM_LE (v);
129 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
137 g_memory_buffer_read_int32 (GMemoryBuffer *mbuf,
142 if (mbuf->pos > mbuf->valid_len - 4)
144 mbuf->pos = mbuf->valid_len;
148 memcpy (&v, mbuf->data + mbuf->pos, 4);
150 switch (mbuf->byte_order)
152 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
153 v = GINT32_FROM_BE (v);
155 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
156 v = GINT32_FROM_LE (v);
158 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
166 g_memory_buffer_read_uint32 (GMemoryBuffer *mbuf,
171 if (mbuf->pos > mbuf->valid_len - 4)
173 mbuf->pos = mbuf->valid_len;
177 memcpy (&v, mbuf->data + mbuf->pos, 4);
179 switch (mbuf->byte_order)
181 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
182 v = GUINT32_FROM_BE (v);
184 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
185 v = GUINT32_FROM_LE (v);
187 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
195 g_memory_buffer_read_int64 (GMemoryBuffer *mbuf,
200 if (mbuf->pos > mbuf->valid_len - 8)
202 mbuf->pos = mbuf->valid_len;
206 memcpy (&v, mbuf->data + mbuf->pos, 8);
208 switch (mbuf->byte_order)
210 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
211 v = GINT64_FROM_BE (v);
213 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
214 v = GINT64_FROM_LE (v);
216 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
224 g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf,
229 if (mbuf->pos > mbuf->valid_len - 8)
231 mbuf->pos = mbuf->valid_len;
235 memcpy (&v, mbuf->data + mbuf->pos, 8);
237 switch (mbuf->byte_order)
239 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
240 v = GUINT64_FROM_BE (v);
242 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
243 v = GUINT64_FROM_LE (v);
245 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
252 #define MIN_ARRAY_SIZE 128
255 g_nearest_pow (gint num)
266 array_resize (GMemoryBuffer *mbuf,
272 if (mbuf->len == size)
276 data = g_realloc (mbuf->data, size);
279 memset ((guint8 *)data + len, 0, size - len);
284 if (mbuf->len < mbuf->valid_len)
285 mbuf->valid_len = mbuf->len;
289 g_memory_buffer_write (GMemoryBuffer *mbuf,
299 /* Check for address space overflow, but only if the buffer is resizable.
300 Otherwise we just do a short write and don't worry. */
301 if (mbuf->pos + count < mbuf->pos)
304 if (mbuf->pos + count > mbuf->len)
306 /* At least enought to fit the write, rounded up
307 for greater than linear growth.
308 TODO: This wastes a lot of memory at large buffer sizes.
309 Figure out a more rational allocation strategy. */
310 new_size = g_nearest_pow (mbuf->pos + count);
311 /* Check for overflow again. We have only checked if
312 pos + count > G_MAXSIZE, but it only catches the case of writing
313 more than 4GiB total on a 32-bit system. There's still the problem
314 of g_nearest_pow overflowing above 0x7fffffff, so we're
315 effectively limited to 2GiB. */
316 if (new_size < mbuf->len)
319 new_size = MAX (new_size, MIN_ARRAY_SIZE);
320 array_resize (mbuf, new_size);
323 dest = (guint8 *)mbuf->data + mbuf->pos;
324 memcpy (dest, buffer, count);
327 if (mbuf->pos > mbuf->valid_len)
328 mbuf->valid_len = mbuf->pos;
334 g_memory_buffer_put_byte (GMemoryBuffer *mbuf,
337 return g_memory_buffer_write (mbuf, &data, 1);
341 g_memory_buffer_put_int16 (GMemoryBuffer *mbuf,
344 switch (mbuf->byte_order)
346 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
347 data = GINT16_TO_BE (data);
349 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
350 data = GINT16_TO_LE (data);
352 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
357 return g_memory_buffer_write (mbuf, &data, 2);
361 g_memory_buffer_put_uint16 (GMemoryBuffer *mbuf,
364 switch (mbuf->byte_order)
366 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
367 data = GUINT16_TO_BE (data);
369 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
370 data = GUINT16_TO_LE (data);
372 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
377 return g_memory_buffer_write (mbuf, &data, 2);
381 g_memory_buffer_put_int32 (GMemoryBuffer *mbuf,
384 switch (mbuf->byte_order)
386 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
387 data = GINT32_TO_BE (data);
389 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
390 data = GINT32_TO_LE (data);
392 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
397 return g_memory_buffer_write (mbuf, &data, 4);
401 g_memory_buffer_put_uint32 (GMemoryBuffer *mbuf,
404 switch (mbuf->byte_order)
406 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
407 data = GUINT32_TO_BE (data);
409 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
410 data = GUINT32_TO_LE (data);
412 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
417 return g_memory_buffer_write (mbuf, &data, 4);
421 g_memory_buffer_put_int64 (GMemoryBuffer *mbuf,
424 switch (mbuf->byte_order)
426 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
427 data = GINT64_TO_BE (data);
429 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
430 data = GINT64_TO_LE (data);
432 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
437 return g_memory_buffer_write (mbuf, &data, 8);
441 g_memory_buffer_put_uint64 (GMemoryBuffer *mbuf,
444 switch (mbuf->byte_order)
446 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
447 data = GUINT64_TO_BE (data);
449 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
450 data = GUINT64_TO_LE (data);
452 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
457 return g_memory_buffer_write (mbuf, &data, 8);
461 g_memory_buffer_put_string (GMemoryBuffer *mbuf,
464 g_return_val_if_fail (str != NULL, FALSE);
466 return g_memory_buffer_write (mbuf, str, strlen (str));
471 * SECTION:gdbusmessage
472 * @short_description: D-Bus Message
473 * @include: gio/gio.h
475 * A type for representing D-Bus messages that can be sent or received
476 * on a #GDBusConnection.
479 typedef struct _GDBusMessageClass GDBusMessageClass;
484 * Class structure for #GDBusMessage.
488 struct _GDBusMessageClass
491 GObjectClass parent_class;
497 * The #GDBusMessage structure contains only private data and should
498 * only be accessed using the provided API.
505 GObject parent_instance;
507 GDBusMessageType type;
508 GDBusMessageFlags flags;
510 GDBusMessageByteOrder byte_order;
511 guchar major_protocol_version;
516 GUnixFDList *fd_list;
526 G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT);
529 g_dbus_message_finalize (GObject *object)
531 GDBusMessage *message = G_DBUS_MESSAGE (object);
533 if (message->headers != NULL)
534 g_hash_table_unref (message->headers);
535 if (message->body != NULL)
536 g_variant_unref (message->body);
538 if (message->fd_list != NULL)
539 g_object_unref (message->fd_list);
542 if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL)
543 G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object);
547 g_dbus_message_get_property (GObject *object,
552 GDBusMessage *message = G_DBUS_MESSAGE (object);
557 g_value_set_boolean (value, g_dbus_message_get_locked (message));
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
567 g_dbus_message_class_init (GDBusMessageClass *klass)
569 GObjectClass *gobject_class;
571 gobject_class = G_OBJECT_CLASS (klass);
572 gobject_class->finalize = g_dbus_message_finalize;
573 gobject_class->get_property = g_dbus_message_get_property;
576 * GDBusConnection:locked:
578 * A boolean specifying whether the message is locked.
582 g_object_class_install_property (gobject_class,
584 g_param_spec_boolean ("locked",
586 P_("Whether the message is locked"),
589 G_PARAM_STATIC_NAME |
590 G_PARAM_STATIC_BLURB |
591 G_PARAM_STATIC_NICK));
595 g_dbus_message_init (GDBusMessage *message)
597 /* Any D-Bus implementation is supposed to handle both Big and
598 * Little Endian encodings and the Endianness is part of the D-Bus
599 * message - we prefer to use Big Endian (since it's Network Byte
600 * Order and just easier to read for humans) but if the machine is
601 * Little Endian we use that for performance reasons.
603 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
604 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
606 /* this could also be G_PDP_ENDIAN */
607 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
609 message->headers = g_hash_table_new_full (g_direct_hash,
612 (GDestroyNotify) g_variant_unref);
616 * g_dbus_message_new:
618 * Creates a new empty #GDBusMessage.
620 * Returns: A #GDBusMessage. Free with g_object_unref().
625 g_dbus_message_new (void)
627 return g_object_new (G_TYPE_DBUS_MESSAGE, NULL);
631 * g_dbus_message_new_method_call:
632 * @name: (allow-none): A valid D-Bus name or %NULL.
633 * @path: A valid object path.
634 * @interface_: (allow-none): A valid D-Bus interface name or %NULL.
635 * @method: A valid method name.
637 * Creates a new #GDBusMessage for a method call.
639 * Returns: A #GDBusMessage. Free with g_object_unref().
644 g_dbus_message_new_method_call (const gchar *name,
646 const gchar *interface_,
649 GDBusMessage *message;
651 g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL);
652 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
653 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
654 g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
656 message = g_dbus_message_new ();
657 message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
660 g_dbus_message_set_destination (message, name);
661 g_dbus_message_set_path (message, path);
662 g_dbus_message_set_member (message, method);
663 if (interface_ != NULL)
664 g_dbus_message_set_interface (message, interface_);
670 * g_dbus_message_new_signal:
671 * @path: A valid object path.
672 * @interface_: A valid D-Bus interface name.
673 * @signal: A valid signal name.
675 * Creates a new #GDBusMessage for a signal emission.
677 * Returns: A #GDBusMessage. Free with g_object_unref().
682 g_dbus_message_new_signal (const gchar *path,
683 const gchar *interface_,
686 GDBusMessage *message;
688 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
689 g_return_val_if_fail (g_dbus_is_member_name (signal), NULL);
690 g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL);
692 message = g_dbus_message_new ();
693 message->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
694 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
696 g_dbus_message_set_path (message, path);
697 g_dbus_message_set_member (message, signal);
698 g_dbus_message_set_interface (message, interface_);
705 * g_dbus_message_new_method_reply:
706 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
707 * create a reply message to.
709 * Creates a new #GDBusMessage that is a reply to @method_call_message.
711 * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
716 g_dbus_message_new_method_reply (GDBusMessage *method_call_message)
718 GDBusMessage *message;
721 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
722 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
723 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
725 message = g_dbus_message_new ();
726 message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
727 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
728 /* reply with same endianness */
729 message->byte_order = method_call_message->byte_order;
731 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
732 sender = g_dbus_message_get_sender (method_call_message);
734 g_dbus_message_set_destination (message, sender);
740 * g_dbus_message_new_method_error:
741 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
742 * create a reply message to.
743 * @error_name: A valid D-Bus error name.
744 * @error_message_format: The D-Bus error message in a printf() format.
745 * @...: Arguments for @error_message_format.
747 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
749 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
754 g_dbus_message_new_method_error (GDBusMessage *method_call_message,
755 const gchar *error_name,
756 const gchar *error_message_format,
762 va_start (var_args, error_message_format);
763 ret = g_dbus_message_new_method_error_valist (method_call_message,
765 error_message_format,
773 * g_dbus_message_new_method_error_literal:
774 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
775 * create a reply message to.
776 * @error_name: A valid D-Bus error name.
777 * @error_message: The D-Bus error message.
779 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
781 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
786 g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message,
787 const gchar *error_name,
788 const gchar *error_message)
790 GDBusMessage *message;
793 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
794 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
795 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
796 g_return_val_if_fail (g_dbus_is_name (error_name), NULL);
797 g_return_val_if_fail (error_message != NULL, NULL);
799 message = g_dbus_message_new ();
800 message->type = G_DBUS_MESSAGE_TYPE_ERROR;
801 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
802 /* reply with same endianness */
803 message->byte_order = method_call_message->byte_order;
805 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
806 g_dbus_message_set_error_name (message, error_name);
807 g_dbus_message_set_body (message, g_variant_new ("(s)", error_message));
809 sender = g_dbus_message_get_sender (method_call_message);
811 g_dbus_message_set_destination (message, sender);
817 * g_dbus_message_new_method_error_valist:
818 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
819 * create a reply message to.
820 * @error_name: A valid D-Bus error name.
821 * @error_message_format: The D-Bus error message in a printf() format.
822 * @var_args: Arguments for @error_message_format.
824 * Like g_dbus_message_new_method_error() but intended for language bindings.
826 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
832 g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message,
833 const gchar *error_name,
834 const gchar *error_message_format,
838 gchar *error_message;
839 error_message = g_strdup_vprintf (error_message_format, var_args);
840 ret = g_dbus_message_new_method_error_literal (method_call_message,
843 g_free (error_message);
847 /* ---------------------------------------------------------------------------------------------------- */
850 * g_dbus_message_get_byte_order:
851 * @message: A #GDBusMessage.
853 * Gets the byte order of @message.
855 * Returns: The byte order.
857 GDBusMessageByteOrder
858 g_dbus_message_get_byte_order (GDBusMessage *message)
860 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0);
861 return message->byte_order;
865 * g_dbus_message_set_byte_order:
866 * @message: A #GDBusMessage.
867 * @byte_order: The byte order.
869 * Sets the byte order of @message.
872 g_dbus_message_set_byte_order (GDBusMessage *message,
873 GDBusMessageByteOrder byte_order)
875 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
879 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
883 message->byte_order = byte_order;
886 /* ---------------------------------------------------------------------------------------------------- */
888 /* TODO: need GI annotations to specify that any guchar value goes for the type */
891 * g_dbus_message_get_message_type:
892 * @message: A #GDBusMessage.
894 * Gets the type of @message.
896 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
901 g_dbus_message_get_message_type (GDBusMessage *message)
903 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID);
904 return message->type;
908 * g_dbus_message_set_message_type:
909 * @message: A #GDBusMessage.
910 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
912 * Sets @message to be of @type.
917 g_dbus_message_set_message_type (GDBusMessage *message,
918 GDBusMessageType type)
920 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
921 g_return_if_fail (type >=0 && type < 256);
925 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
929 message->type = type;
932 /* ---------------------------------------------------------------------------------------------------- */
934 /* TODO: need GI annotations to specify that any guchar value goes for flags */
937 * g_dbus_message_get_flags:
938 * @message: A #GDBusMessage.
940 * Gets the flags for @message.
942 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
947 g_dbus_message_get_flags (GDBusMessage *message)
949 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE);
950 return message->flags;
954 * g_dbus_message_set_flags:
955 * @message: A #GDBusMessage.
956 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
957 * enumeration bitwise ORed together).
959 * Sets the flags to set on @message.
964 g_dbus_message_set_flags (GDBusMessage *message,
965 GDBusMessageFlags flags)
967 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
968 g_return_if_fail (flags >=0 && flags < 256);
972 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
976 message->flags = flags;
979 /* ---------------------------------------------------------------------------------------------------- */
982 * g_dbus_message_get_serial:
983 * @message: A #GDBusMessage.
985 * Gets the serial for @message.
987 * Returns: A #guint32.
992 g_dbus_message_get_serial (GDBusMessage *message)
994 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
995 return message->serial;
999 * g_dbus_message_set_serial:
1000 * @message: A #GDBusMessage.
1001 * @serial: A #guint32.
1003 * Sets the serial for @message.
1008 g_dbus_message_set_serial (GDBusMessage *message,
1011 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1013 if (message->locked)
1015 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1019 message->serial = serial;
1022 /* ---------------------------------------------------------------------------------------------------- */
1024 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
1027 * g_dbus_message_get_header:
1028 * @message: A #GDBusMessage.
1029 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1031 * Gets a header field on @message.
1033 * Returns: A #GVariant with the value if the header was found, %NULL
1034 * otherwise. Do not free, it is owned by @message.
1039 g_dbus_message_get_header (GDBusMessage *message,
1040 GDBusMessageHeaderField header_field)
1042 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1043 g_return_val_if_fail (header_field >=0 && header_field < 256, NULL);
1044 return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
1048 * g_dbus_message_set_header:
1049 * @message: A #GDBusMessage.
1050 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1051 * @value: (allow-none): A #GVariant to set the header field or %NULL to clear the header field.
1053 * Sets a header field on @message.
1055 * If @value is floating, @message assumes ownership of @value.
1060 g_dbus_message_set_header (GDBusMessage *message,
1061 GDBusMessageHeaderField header_field,
1064 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1065 g_return_if_fail (header_field >=0 && header_field < 256);
1067 if (message->locked)
1069 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1075 g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field));
1079 g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
1084 * g_dbus_message_get_header_fields:
1085 * @message: A #GDBusMessage.
1087 * Gets an array of all header fields on @message that are set.
1089 * Returns: (array zero-terminated=1): An array of header fields
1090 * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
1091 * is a #guchar. Free with g_free().
1096 g_dbus_message_get_header_fields (GDBusMessage *message)
1104 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1106 keys = g_hash_table_get_keys (message->headers);
1107 num_keys = g_list_length (keys);
1108 ret = g_new (guchar, num_keys + 1);
1109 for (l = keys, n = 0; l != NULL; l = l->next, n++)
1110 ret[n] = GPOINTER_TO_UINT (l->data);
1111 g_assert (n == num_keys);
1112 ret[n] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID;
1118 /* ---------------------------------------------------------------------------------------------------- */
1121 * g_dbus_message_get_body:
1122 * @message: A #GDBusMessage.
1124 * Gets the body of a message.
1126 * Returns: A #GVariant or %NULL if the body is empty. Do not free, it is owned by @message.
1131 g_dbus_message_get_body (GDBusMessage *message)
1133 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1134 return message->body;
1138 * g_dbus_message_set_body:
1139 * @message: A #GDBusMessage.
1140 * @body: Either %NULL or a #GVariant that is a tuple.
1142 * Sets the body @message. As a side-effect the
1143 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
1144 * type string of @body (or cleared if @body is %NULL).
1146 * If @body is floating, @message assumes ownership of @body.
1151 g_dbus_message_set_body (GDBusMessage *message,
1154 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1155 g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE));
1157 if (message->locked)
1159 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1163 if (message->body != NULL)
1164 g_variant_unref (message->body);
1167 message->body = NULL;
1168 g_dbus_message_set_signature (message, NULL);
1172 const gchar *type_string;
1173 gsize type_string_len;
1176 message->body = g_variant_ref_sink (body);
1178 type_string = g_variant_get_type_string (body);
1179 type_string_len = strlen (type_string);
1180 g_assert (type_string_len >= 2);
1181 signature = g_strndup (type_string + 1, type_string_len - 2);
1182 g_dbus_message_set_signature (message, signature);
1187 /* ---------------------------------------------------------------------------------------------------- */
1191 * g_dbus_message_get_unix_fd_list:
1192 * @message: A #GDBusMessage.
1194 * Gets the UNIX file descriptors associated with @message, if any.
1196 * This method is only available on UNIX.
1198 * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
1199 * associated. Do not free, this object is owned by @message.
1204 g_dbus_message_get_unix_fd_list (GDBusMessage *message)
1206 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1207 return message->fd_list;
1211 * g_dbus_message_set_unix_fd_list:
1212 * @message: A #GDBusMessage.
1213 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
1215 * Sets the UNIX file descriptors associated with @message. As a
1216 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
1217 * field is set to the number of fds in @fd_list (or cleared if
1218 * @fd_list is %NULL).
1220 * This method is only available on UNIX.
1225 g_dbus_message_set_unix_fd_list (GDBusMessage *message,
1226 GUnixFDList *fd_list)
1228 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1229 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
1231 if (message->locked)
1233 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1237 if (message->fd_list != NULL)
1238 g_object_unref (message->fd_list);
1239 if (fd_list != NULL)
1241 message->fd_list = g_object_ref (fd_list);
1242 g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list));
1246 message->fd_list = NULL;
1247 g_dbus_message_set_num_unix_fds (message, 0);
1252 /* ---------------------------------------------------------------------------------------------------- */
1255 validate_headers (GDBusMessage *message,
1260 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1261 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1265 switch (message->type)
1267 case G_DBUS_MESSAGE_TYPE_INVALID:
1268 g_set_error_literal (error,
1270 G_IO_ERROR_INVALID_ARGUMENT,
1271 _("type is INVALID"));
1275 case G_DBUS_MESSAGE_TYPE_METHOD_CALL:
1276 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
1277 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
1279 g_set_error_literal (error,
1281 G_IO_ERROR_INVALID_ARGUMENT,
1282 _("METHOD_CALL message: PATH or MEMBER header field is missing"));
1287 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
1288 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
1290 g_set_error_literal (error,
1292 G_IO_ERROR_INVALID_ARGUMENT,
1293 _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
1298 case G_DBUS_MESSAGE_TYPE_ERROR:
1299 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME) == NULL ||
1300 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
1302 g_set_error_literal (error,
1304 G_IO_ERROR_INVALID_ARGUMENT,
1305 _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
1310 case G_DBUS_MESSAGE_TYPE_SIGNAL:
1311 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
1312 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE) == NULL ||
1313 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
1315 g_set_error_literal (error,
1317 G_IO_ERROR_INVALID_ARGUMENT,
1318 _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
1321 if (g_strcmp0 (g_dbus_message_get_path (message), "/org/freedesktop/DBus/Local") == 0)
1323 g_set_error_literal (error,
1325 G_IO_ERROR_INVALID_ARGUMENT,
1326 _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
1329 if (g_strcmp0 (g_dbus_message_get_interface (message), "org.freedesktop.DBus.Local") == 0)
1331 g_set_error_literal (error,
1333 G_IO_ERROR_INVALID_ARGUMENT,
1334 _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
1340 /* hitherto unknown type - nothing to check */
1347 g_assert (ret || (error == NULL || *error != NULL));
1351 /* ---------------------------------------------------------------------------------------------------- */
1354 ensure_input_padding (GMemoryBuffer *buf,
1359 gsize wanted_offset;
1362 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
1363 buf->pos = wanted_offset;
1367 static const gchar *
1368 read_string (GMemoryBuffer *mbuf,
1373 const gchar *end_valid;
1375 if (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos)
1377 mbuf->pos = mbuf->valid_len;
1378 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1381 G_IO_ERROR_INVALID_ARGUMENT,
1382 g_dngettext (GETTEXT_PACKAGE,
1383 "Wanted to read %lu byte but only got %lu",
1384 "Wanted to read %lu bytes but only got %lu",
1387 (gulong)(mbuf->valid_len - mbuf->pos));
1388 mbuf->pos = mbuf->valid_len;
1392 if (mbuf->data[mbuf->pos + len] != '\0')
1394 str = g_strndup (mbuf->data + mbuf->pos, len);
1397 G_IO_ERROR_INVALID_ARGUMENT,
1398 _("Expected NUL byte after the string '%s' but found byte %d"),
1399 str, mbuf->data[mbuf->pos + len]);
1401 mbuf->pos += len + 1;
1405 str = mbuf->data + mbuf->pos;
1406 mbuf->pos += len + 1;
1408 if (!g_utf8_validate (str, -1, &end_valid))
1412 offset = (gint) (end_valid - str);
1413 valid_str = g_strndup (str, offset);
1416 G_IO_ERROR_INVALID_ARGUMENT,
1417 _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
1418 "The valid UTF-8 string up until that point was '%s'"),
1429 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
1431 /* returns a non-floating GVariant! */
1433 parse_value_from_blob (GMemoryBuffer *buf,
1434 const GVariantType *type,
1435 gboolean just_align,
1440 GError *local_error;
1442 const gchar *type_string;
1444 type_string = g_variant_type_peek_string (type);
1446 #ifdef DEBUG_SERIALIZER
1449 s = g_variant_type_dup_string (type);
1450 g_print ("%*s%s type %s from offset 0x%04x",
1452 just_align ? "Aligning" : "Reading",
1454 (gint) g_seekable_tell (G_SEEKABLE (buf)));
1457 #endif /* DEBUG_SERIALIZER */
1463 switch (type_string[0])
1465 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1466 if (!ensure_input_padding (buf, 4, &local_error))
1471 v = g_memory_buffer_read_uint32 (buf, &local_error);
1472 if (local_error != NULL)
1474 ret = g_variant_new_boolean (v);
1478 case 'y': /* G_VARIANT_TYPE_BYTE */
1482 v = g_memory_buffer_read_byte (buf, &local_error);
1483 if (local_error != NULL)
1485 ret = g_variant_new_byte (v);
1489 case 'n': /* G_VARIANT_TYPE_INT16 */
1490 if (!ensure_input_padding (buf, 2, &local_error))
1495 v = g_memory_buffer_read_int16 (buf, &local_error);
1496 if (local_error != NULL)
1498 ret = g_variant_new_int16 (v);
1502 case 'q': /* G_VARIANT_TYPE_UINT16 */
1503 if (!ensure_input_padding (buf, 2, &local_error))
1508 v = g_memory_buffer_read_uint16 (buf, &local_error);
1509 if (local_error != NULL)
1511 ret = g_variant_new_uint16 (v);
1515 case 'i': /* G_VARIANT_TYPE_INT32 */
1516 if (!ensure_input_padding (buf, 4, &local_error))
1521 v = g_memory_buffer_read_int32 (buf, &local_error);
1522 if (local_error != NULL)
1524 ret = g_variant_new_int32 (v);
1528 case 'u': /* G_VARIANT_TYPE_UINT32 */
1529 if (!ensure_input_padding (buf, 4, &local_error))
1534 v = g_memory_buffer_read_uint32 (buf, &local_error);
1535 if (local_error != NULL)
1537 ret = g_variant_new_uint32 (v);
1541 case 'x': /* G_VARIANT_TYPE_INT64 */
1542 if (!ensure_input_padding (buf, 8, &local_error))
1547 v = g_memory_buffer_read_int64 (buf, &local_error);
1548 if (local_error != NULL)
1550 ret = g_variant_new_int64 (v);
1554 case 't': /* G_VARIANT_TYPE_UINT64 */
1555 if (!ensure_input_padding (buf, 8, &local_error))
1560 v = g_memory_buffer_read_uint64 (buf, &local_error);
1561 if (local_error != NULL)
1563 ret = g_variant_new_uint64 (v);
1567 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1568 if (!ensure_input_padding (buf, 8, &local_error))
1576 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
1577 u.v_uint64 = g_memory_buffer_read_uint64 (buf, &local_error);
1578 if (local_error != NULL)
1580 ret = g_variant_new_double (u.v_double);
1584 case 's': /* G_VARIANT_TYPE_STRING */
1585 if (!ensure_input_padding (buf, 4, &local_error))
1591 len = g_memory_buffer_read_uint32 (buf, &local_error);
1592 if (local_error != NULL)
1594 v = read_string (buf, (gsize) len, &local_error);
1597 ret = g_variant_new_string (v);
1601 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1602 if (!ensure_input_padding (buf, 4, &local_error))
1608 len = g_memory_buffer_read_uint32 (buf, &local_error);
1609 if (local_error != NULL)
1611 v = read_string (buf, (gsize) len, &local_error);
1614 if (!g_variant_is_object_path (v))
1616 g_set_error (&local_error,
1618 G_IO_ERROR_INVALID_ARGUMENT,
1619 _("Parsed value '%s' is not a valid D-Bus object path"),
1623 ret = g_variant_new_object_path (v);
1627 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1632 len = g_memory_buffer_read_byte (buf, &local_error);
1633 if (local_error != NULL)
1635 v = read_string (buf, (gsize) len, &local_error);
1638 if (!g_variant_is_signature (v))
1640 g_set_error (&local_error,
1642 G_IO_ERROR_INVALID_ARGUMENT,
1643 _("Parsed value '%s' is not a valid D-Bus signature"),
1647 ret = g_variant_new_signature (v);
1651 case 'h': /* G_VARIANT_TYPE_HANDLE */
1652 if (!ensure_input_padding (buf, 4, &local_error))
1657 v = g_memory_buffer_read_int32 (buf, &local_error);
1658 if (local_error != NULL)
1660 ret = g_variant_new_handle (v);
1664 case 'a': /* G_VARIANT_TYPE_ARRAY */
1665 if (!ensure_input_padding (buf, 4, &local_error))
1668 /* If we are only aligning for this array type, it is the child type of
1669 * another array, which is empty. So, we do not need to add padding for
1670 * this nonexistent array's elements: we only need to align for this
1671 * array itself (4 bytes). See
1672 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1679 const GVariantType *element_type;
1680 GVariantBuilder builder;
1682 array_len = g_memory_buffer_read_uint32 (buf, &local_error);
1683 if (local_error != NULL)
1687 #ifdef DEBUG_SERIALIZER
1688 g_print (": array spans 0x%04x bytes\n", array_len);
1689 #endif /* DEBUG_SERIALIZER */
1691 if (array_len > (2<<26))
1693 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1694 g_set_error (&local_error,
1696 G_IO_ERROR_INVALID_ARGUMENT,
1697 g_dngettext (GETTEXT_PACKAGE,
1698 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1699 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1705 g_variant_builder_init (&builder, type);
1706 element_type = g_variant_type_element (type);
1711 item = parse_value_from_blob (buf,
1716 g_assert (item == NULL);
1720 /* TODO: optimize array of primitive types */
1722 target = offset + array_len;
1723 while (offset < target)
1726 item = parse_value_from_blob (buf,
1733 g_variant_builder_clear (&builder);
1736 g_variant_builder_add_value (&builder, item);
1737 g_variant_unref (item);
1742 ret = g_variant_builder_end (&builder);
1747 if (g_variant_type_is_dict_entry (type))
1749 const GVariantType *key_type;
1750 const GVariantType *value_type;
1754 if (!ensure_input_padding (buf, 8, &local_error))
1758 #ifdef DEBUG_SERIALIZER
1760 #endif /* DEBUG_SERIALIZER */
1764 key_type = g_variant_type_key (type);
1765 key = parse_value_from_blob (buf,
1772 value_type = g_variant_type_value (type);
1773 value = parse_value_from_blob (buf,
1780 g_variant_unref (key);
1783 ret = g_variant_new_dict_entry (key, value);
1784 g_variant_unref (key);
1785 g_variant_unref (value);
1788 else if (g_variant_type_is_tuple (type))
1790 if (!ensure_input_padding (buf, 8, &local_error))
1794 #ifdef DEBUG_SERIALIZER
1796 #endif /* DEBUG_SERIALIZER */
1800 const GVariantType *element_type;
1801 GVariantBuilder builder;
1803 g_variant_builder_init (&builder, type);
1804 element_type = g_variant_type_first (type);
1805 while (element_type != NULL)
1808 item = parse_value_from_blob (buf,
1815 g_variant_builder_clear (&builder);
1818 g_variant_builder_add_value (&builder, item);
1819 g_variant_unref (item);
1821 element_type = g_variant_type_next (element_type);
1823 ret = g_variant_builder_end (&builder);
1826 else if (g_variant_type_is_variant (type))
1829 #ifdef DEBUG_SERIALIZER
1831 #endif /* DEBUG_SERIALIZER */
1837 GVariantType *variant_type;
1840 siglen = g_memory_buffer_read_byte (buf, &local_error);
1841 if (local_error != NULL)
1843 sig = read_string (buf, (gsize) siglen, &local_error);
1846 if (!g_variant_is_signature (sig))
1848 g_set_error (&local_error,
1850 G_IO_ERROR_INVALID_ARGUMENT,
1851 _("Parsed value '%s' for variant is not a valid D-Bus signature"),
1855 variant_type = g_variant_type_new (sig);
1856 value = parse_value_from_blob (buf,
1861 g_variant_type_free (variant_type);
1864 ret = g_variant_new_variant (value);
1865 g_variant_unref (value);
1871 s = g_variant_type_dup_string (type);
1872 g_set_error (&local_error,
1874 G_IO_ERROR_INVALID_ARGUMENT,
1875 _("Error deserializing GVariant with type string '%s' from the D-Bus wire format"),
1883 g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
1885 #ifdef DEBUG_SERIALIZER
1891 if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
1893 s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret));
1897 s = g_variant_print (ret, FALSE);
1899 g_print (": %s\n", s);
1904 is_leaf = is_leaf; /* To avoid -Wunused-but-set-variable */
1905 #endif /* DEBUG_SERIALIZER */
1907 /* sink the reference */
1910 g_assert (g_variant_is_floating (ret));
1911 g_variant_ref_sink (ret);
1916 #ifdef DEBUG_SERIALIZER
1918 "%*sFAILURE: %s (%s, %d)\n",
1920 local_error->message,
1921 g_quark_to_string (local_error->domain),
1923 #endif /* DEBUG_SERIALIZER */
1924 g_propagate_error (error, local_error);
1928 /* ---------------------------------------------------------------------------------------------------- */
1930 /* message_header must be at least 16 bytes */
1933 * g_dbus_message_bytes_needed:
1934 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1935 * @blob_len: The length of @blob (must be at least 16).
1936 * @error: Return location for error or %NULL.
1938 * Utility function to calculate how many bytes are needed to
1939 * completely deserialize the D-Bus message stored at @blob.
1941 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1942 * @blob contains invalid data or not enough data is available to
1943 * determine the size).
1948 g_dbus_message_bytes_needed (guchar *blob,
1956 g_return_val_if_fail (blob != NULL, -1);
1957 g_return_val_if_fail (error == NULL || *error == NULL, -1);
1958 g_return_val_if_fail (blob_len >= 16, -1);
1962 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1963 ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]);
1964 /* round up so it's a multiple of 8 */
1965 ret = 8 * ((ret + 7)/8);
1966 /* finally add the body size */
1967 ret += GUINT32_FROM_LE (((guint32 *) blob)[1]);
1969 else if (blob[0] == 'B')
1971 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1972 ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]);
1973 /* round up so it's a multiple of 8 */
1974 ret = 8 * ((ret + 7)/8);
1975 /* finally add the body size */
1976 ret += GUINT32_FROM_BE (((guint32 *) blob)[1]);
1982 G_IO_ERROR_INVALID_ARGUMENT,
1983 "Unable to determine message blob length - given blob is malformed");
1990 G_IO_ERROR_INVALID_ARGUMENT,
1991 "Blob indicates that message exceeds maximum message length (128MiB)");
1998 /* ---------------------------------------------------------------------------------------------------- */
2001 * g_dbus_message_new_from_blob:
2002 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
2003 * @blob_len: The length of @blob.
2004 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2005 * @error: Return location for error or %NULL.
2007 * Creates a new #GDBusMessage from the data stored at @blob. The byte
2008 * order that the message was in can be retrieved using
2009 * g_dbus_message_get_byte_order().
2011 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
2017 g_dbus_message_new_from_blob (guchar *blob,
2019 GDBusCapabilityFlags capabilities,
2024 GDBusMessage *message;
2026 guchar major_protocol_version;
2027 guint32 message_body_len;
2031 GVariant *signature;
2033 /* TODO: check against @capabilities */
2037 g_return_val_if_fail (blob != NULL, NULL);
2038 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2039 g_return_val_if_fail (blob_len >= 12, NULL);
2041 message = g_dbus_message_new ();
2043 memset (&mbuf, 0, sizeof (mbuf));
2044 mbuf.data = (gchar *)blob;
2045 mbuf.len = mbuf.valid_len = blob_len;
2047 endianness = g_memory_buffer_read_byte (&mbuf, NULL);
2051 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
2052 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
2055 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2056 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
2061 G_IO_ERROR_INVALID_ARGUMENT,
2062 _("Invalid endianness value. Expected 0x6c ('l') or 0x42 ('B') but found value 0x%02x"),
2067 message->type = g_memory_buffer_read_byte (&mbuf, NULL);
2068 message->flags = g_memory_buffer_read_byte (&mbuf, NULL);
2069 major_protocol_version = g_memory_buffer_read_byte (&mbuf, NULL);
2070 if (major_protocol_version != 1)
2074 G_IO_ERROR_INVALID_ARGUMENT,
2075 _("Invalid major protocol version. Expected 1 but found %d"),
2076 major_protocol_version);
2079 message_body_len = g_memory_buffer_read_uint32 (&mbuf, NULL);
2080 message->serial = g_memory_buffer_read_uint32 (&mbuf, NULL);
2082 #ifdef DEBUG_SERIALIZER
2083 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2086 s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2);
2087 g_print ("%s\n", s);
2090 #endif /* DEBUG_SERIALIZER */
2092 #ifdef DEBUG_SERIALIZER
2093 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2094 #endif /* DEBUG_SERIALIZER */
2095 headers = parse_value_from_blob (&mbuf,
2096 G_VARIANT_TYPE ("a{yv}"),
2100 if (headers == NULL)
2102 g_variant_iter_init (&iter, headers);
2103 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2105 guchar header_field;
2107 g_variant_get (item,
2111 g_dbus_message_set_header (message, header_field, value);
2112 g_variant_unref (value);
2113 g_variant_unref (item);
2115 g_variant_unref (headers);
2117 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2118 if (signature != NULL)
2120 const gchar *signature_str;
2121 gsize signature_str_len;
2123 signature_str = g_variant_get_string (signature, &signature_str_len);
2125 /* signature but no body */
2126 if (message_body_len == 0 && signature_str_len > 0)
2130 G_IO_ERROR_INVALID_ARGUMENT,
2131 _("Signature header with signature '%s' found but message body is empty"),
2135 else if (signature_str_len > 0)
2137 GVariantType *variant_type;
2138 gchar *tupled_signature_str;
2140 if (!g_variant_is_signature (signature_str))
2144 G_IO_ERROR_INVALID_ARGUMENT,
2145 _("Parsed value '%s' is not a valid D-Bus signature (for body)"),
2149 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2150 variant_type = g_variant_type_new (tupled_signature_str);
2151 g_free (tupled_signature_str);
2152 #ifdef DEBUG_SERIALIZER
2153 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2154 #endif /* DEBUG_SERIALIZER */
2155 message->body = parse_value_from_blob (&mbuf,
2160 g_variant_type_free (variant_type);
2161 if (message->body == NULL)
2167 /* no signature, this is only OK if the body is empty */
2168 if (message_body_len != 0)
2170 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
2173 G_IO_ERROR_INVALID_ARGUMENT,
2174 g_dngettext (GETTEXT_PACKAGE,
2175 "No signature header in message but the message body is %u byte",
2176 "No signature header in message but the message body is %u bytes",
2183 if (!validate_headers (message, error))
2185 g_prefix_error (error, _("Cannot deserialize message: "));
2198 if (message != NULL)
2199 g_object_unref (message);
2204 /* ---------------------------------------------------------------------------------------------------- */
2207 ensure_output_padding (GMemoryBuffer *mbuf,
2211 gsize wanted_offset;
2212 gsize padding_needed;
2216 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
2217 padding_needed = wanted_offset - offset;
2219 for (n = 0; n < padding_needed; n++)
2220 g_memory_buffer_put_byte (mbuf, '\0');
2222 return padding_needed;
2225 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
2227 append_value_to_blob (GVariant *value,
2228 const GVariantType *type,
2229 GMemoryBuffer *mbuf,
2230 gsize *out_padding_added,
2233 gsize padding_added;
2234 const gchar *type_string;
2236 type_string = g_variant_type_peek_string (type);
2240 switch (type_string[0])
2242 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
2243 padding_added = ensure_output_padding (mbuf, 4);
2246 gboolean v = g_variant_get_boolean (value);
2247 g_memory_buffer_put_uint32 (mbuf, v);
2251 case 'y': /* G_VARIANT_TYPE_BYTE */
2254 guint8 v = g_variant_get_byte (value);
2255 g_memory_buffer_put_byte (mbuf, v);
2259 case 'n': /* G_VARIANT_TYPE_INT16 */
2260 padding_added = ensure_output_padding (mbuf, 2);
2263 gint16 v = g_variant_get_int16 (value);
2264 g_memory_buffer_put_int16 (mbuf, v);
2268 case 'q': /* G_VARIANT_TYPE_UINT16 */
2269 padding_added = ensure_output_padding (mbuf, 2);
2272 guint16 v = g_variant_get_uint16 (value);
2273 g_memory_buffer_put_uint16 (mbuf, v);
2277 case 'i': /* G_VARIANT_TYPE_INT32 */
2278 padding_added = ensure_output_padding (mbuf, 4);
2281 gint32 v = g_variant_get_int32 (value);
2282 g_memory_buffer_put_int32 (mbuf, v);
2286 case 'u': /* G_VARIANT_TYPE_UINT32 */
2287 padding_added = ensure_output_padding (mbuf, 4);
2290 guint32 v = g_variant_get_uint32 (value);
2291 g_memory_buffer_put_uint32 (mbuf, v);
2295 case 'x': /* G_VARIANT_TYPE_INT64 */
2296 padding_added = ensure_output_padding (mbuf, 8);
2299 gint64 v = g_variant_get_int64 (value);
2300 g_memory_buffer_put_int64 (mbuf, v);
2304 case 't': /* G_VARIANT_TYPE_UINT64 */
2305 padding_added = ensure_output_padding (mbuf, 8);
2308 guint64 v = g_variant_get_uint64 (value);
2309 g_memory_buffer_put_uint64 (mbuf, v);
2313 case 'd': /* G_VARIANT_TYPE_DOUBLE */
2314 padding_added = ensure_output_padding (mbuf, 8);
2321 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
2322 u.v_double = g_variant_get_double (value);
2323 g_memory_buffer_put_uint64 (mbuf, u.v_uint64);
2327 case 's': /* G_VARIANT_TYPE_STRING */
2328 padding_added = ensure_output_padding (mbuf, 4);
2334 v = g_variant_get_string (value, &len);
2335 g_assert (g_utf8_validate (v, -1, &end) && (end == v + len));
2336 g_memory_buffer_put_uint32 (mbuf, len);
2337 g_memory_buffer_put_string (mbuf, v);
2338 g_memory_buffer_put_byte (mbuf, '\0');
2342 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
2343 padding_added = ensure_output_padding (mbuf, 4);
2347 const gchar *v = g_variant_get_string (value, &len);
2348 g_assert (g_variant_is_object_path (v));
2349 g_memory_buffer_put_uint32 (mbuf, len);
2350 g_memory_buffer_put_string (mbuf, v);
2351 g_memory_buffer_put_byte (mbuf, '\0');
2355 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2359 const gchar *v = g_variant_get_string (value, &len);
2360 g_assert (g_variant_is_signature (v));
2361 g_memory_buffer_put_byte (mbuf, len);
2362 g_memory_buffer_put_string (mbuf, v);
2363 g_memory_buffer_put_byte (mbuf, '\0');
2367 case 'h': /* G_VARIANT_TYPE_HANDLE */
2368 padding_added = ensure_output_padding (mbuf, 4);
2371 gint32 v = g_variant_get_handle (value);
2372 g_memory_buffer_put_int32 (mbuf, v);
2376 case 'a': /* G_VARIANT_TYPE_ARRAY */
2380 goffset array_len_offset;
2381 goffset array_payload_begin_offset;
2385 padding_added = ensure_output_padding (mbuf, 4);
2388 /* array length - will be filled in later */
2389 array_len_offset = mbuf->valid_len;
2390 g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE);
2392 /* From the D-Bus spec:
2394 * "A UINT32 giving the length of the array data in bytes,
2395 * followed by alignment padding to the alignment boundary of
2396 * the array element type, followed by each array element. The
2397 * array length is from the end of the alignment padding to
2398 * the end of the last element, i.e. it does not include the
2399 * padding after the length, or any padding after the last
2402 * Thus, we need to count how much padding the first element
2403 * contributes and subtract that from the array length.
2405 array_payload_begin_offset = mbuf->valid_len;
2407 if (g_variant_n_children (value) == 0)
2409 gsize padding_added_for_item;
2410 if (!append_value_to_blob (NULL,
2411 g_variant_type_element (type),
2413 &padding_added_for_item,
2416 array_payload_begin_offset += padding_added_for_item;
2422 g_variant_iter_init (&iter, value);
2423 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2425 gsize padding_added_for_item;
2426 if (!append_value_to_blob (item,
2427 g_variant_get_type (item),
2429 &padding_added_for_item,
2432 g_variant_unref (item);
2435 g_variant_unref (item);
2438 array_payload_begin_offset += padding_added_for_item;
2444 cur_offset = mbuf->valid_len;
2445 array_len = cur_offset - array_payload_begin_offset;
2446 mbuf->pos = array_len_offset;
2448 g_memory_buffer_put_uint32 (mbuf, array_len);
2449 mbuf->pos = cur_offset;
2455 if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
2457 padding_added = ensure_output_padding (mbuf, 8);
2462 g_variant_iter_init (&iter, value);
2463 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2465 if (!append_value_to_blob (item,
2466 g_variant_get_type (item),
2471 g_variant_unref (item);
2474 g_variant_unref (item);
2478 else if (g_variant_type_is_variant (type))
2483 const gchar *signature;
2484 child = g_variant_get_child_value (value, 0);
2485 signature = g_variant_get_type_string (child);
2486 g_memory_buffer_put_byte (mbuf, strlen (signature));
2487 g_memory_buffer_put_string (mbuf, signature);
2488 g_memory_buffer_put_byte (mbuf, '\0');
2489 if (!append_value_to_blob (child,
2490 g_variant_get_type (child),
2495 g_variant_unref (child);
2498 g_variant_unref (child);
2505 G_IO_ERROR_INVALID_ARGUMENT,
2506 _("Error serializing GVariant with type string '%s' to the D-Bus wire format"),
2507 g_variant_get_type_string (value));
2513 if (out_padding_added != NULL)
2514 *out_padding_added = padding_added;
2523 append_body_to_blob (GVariant *value,
2524 GMemoryBuffer *mbuf,
2530 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
2534 G_IO_ERROR_INVALID_ARGUMENT,
2535 "Expected a tuple for the body of the GDBusMessage.");
2539 g_variant_iter_init (&iter, value);
2540 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2542 if (!append_value_to_blob (item,
2543 g_variant_get_type (item),
2548 g_variant_unref (item);
2551 g_variant_unref (item);
2559 /* ---------------------------------------------------------------------------------------------------- */
2562 * g_dbus_message_to_blob:
2563 * @message: A #GDBusMessage.
2564 * @out_size: Return location for size of generated blob.
2565 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2566 * @error: Return location for error.
2568 * Serializes @message to a blob. The byte order returned by
2569 * g_dbus_message_get_byte_order() will be used.
2571 * Returns: (array length=out_size) (transfer full): A pointer to a
2572 * valid binary D-Bus message of @out_size bytes generated by @message
2573 * or %NULL if @error is set. Free with g_free().
2578 g_dbus_message_to_blob (GDBusMessage *message,
2580 GDBusCapabilityFlags capabilities,
2586 goffset body_len_offset;
2587 goffset body_start_offset;
2589 GVariant *header_fields;
2590 GVariantBuilder builder;
2591 GHashTableIter hash_iter;
2593 GVariant *header_value;
2594 GVariant *signature;
2595 const gchar *signature_str;
2596 gint num_fds_in_message;
2597 gint num_fds_according_to_header;
2599 /* TODO: check against @capabilities */
2603 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2604 g_return_val_if_fail (out_size != NULL, NULL);
2605 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2607 memset (&mbuf, 0, sizeof (mbuf));
2608 mbuf.len = MIN_ARRAY_SIZE;
2609 mbuf.data = g_malloc (mbuf.len);
2611 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
2612 switch (message->byte_order)
2614 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
2615 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2617 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
2618 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
2623 g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order);
2624 g_memory_buffer_put_byte (&mbuf, message->type);
2625 g_memory_buffer_put_byte (&mbuf, message->flags);
2626 g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */
2627 body_len_offset = mbuf.valid_len;
2628 /* body length - will be filled in later */
2629 g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE);
2630 g_memory_buffer_put_uint32 (&mbuf, message->serial);
2632 num_fds_in_message = 0;
2634 if (message->fd_list != NULL)
2635 num_fds_in_message = g_unix_fd_list_get_length (message->fd_list);
2637 num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
2638 if (num_fds_in_message != num_fds_according_to_header)
2642 G_IO_ERROR_INVALID_ARGUMENT,
2643 _("Message has %d file descriptors but the header field indicates %d file descriptors"),
2645 num_fds_according_to_header);
2649 if (!validate_headers (message, error))
2651 g_prefix_error (error, _("Cannot serialize message: "));
2655 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
2656 g_hash_table_iter_init (&hash_iter, message->headers);
2657 while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
2659 g_variant_builder_add (&builder,
2661 (guchar) GPOINTER_TO_UINT (key),
2664 header_fields = g_variant_builder_end (&builder);
2666 if (!append_value_to_blob (header_fields,
2667 g_variant_get_type (header_fields),
2672 g_variant_unref (header_fields);
2675 g_variant_unref (header_fields);
2677 /* header size must be a multiple of 8 */
2678 ensure_output_padding (&mbuf, 8);
2680 body_start_offset = mbuf.valid_len;
2682 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2683 signature_str = NULL;
2684 if (signature != NULL)
2685 signature_str = g_variant_get_string (signature, NULL);
2686 if (message->body != NULL)
2688 gchar *tupled_signature_str;
2689 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2690 if (signature == NULL)
2694 G_IO_ERROR_INVALID_ARGUMENT,
2695 _("Message body has signature '%s' but there is no signature header"),
2697 g_free (tupled_signature_str);
2700 else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0)
2704 G_IO_ERROR_INVALID_ARGUMENT,
2705 _("Message body has type signature '%s' but signature in the header field is '%s'"),
2706 tupled_signature_str, g_variant_get_type_string (message->body));
2707 g_free (tupled_signature_str);
2710 g_free (tupled_signature_str);
2711 if (!append_body_to_blob (message->body, &mbuf, error))
2716 if (signature != NULL && strlen (signature_str) > 0)
2720 G_IO_ERROR_INVALID_ARGUMENT,
2721 _("Message body is empty but signature in the header field is '(%s)'"),
2727 /* OK, we're done writing the message - set the body length */
2728 size = mbuf.valid_len;
2729 body_size = size - body_start_offset;
2731 mbuf.pos = body_len_offset;
2733 g_memory_buffer_put_uint32 (&mbuf, body_size);
2736 ret = (guchar *)mbuf.data;
2742 /* ---------------------------------------------------------------------------------------------------- */
2745 get_uint32_header (GDBusMessage *message,
2746 GDBusMessageHeaderField header_field)
2752 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2753 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
2754 ret = g_variant_get_uint32 (value);
2759 static const gchar *
2760 get_string_header (GDBusMessage *message,
2761 GDBusMessageHeaderField header_field)
2767 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2768 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
2769 ret = g_variant_get_string (value, NULL);
2774 static const gchar *
2775 get_object_path_header (GDBusMessage *message,
2776 GDBusMessageHeaderField header_field)
2782 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2783 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
2784 ret = g_variant_get_string (value, NULL);
2789 static const gchar *
2790 get_signature_header (GDBusMessage *message,
2791 GDBusMessageHeaderField header_field)
2797 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2798 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
2799 ret = g_variant_get_string (value, NULL);
2804 /* ---------------------------------------------------------------------------------------------------- */
2807 set_uint32_header (GDBusMessage *message,
2808 GDBusMessageHeaderField header_field,
2811 g_dbus_message_set_header (message,
2813 g_variant_new_uint32 (value));
2817 set_string_header (GDBusMessage *message,
2818 GDBusMessageHeaderField header_field,
2821 g_dbus_message_set_header (message,
2823 value == NULL ? NULL : g_variant_new_string (value));
2827 set_object_path_header (GDBusMessage *message,
2828 GDBusMessageHeaderField header_field,
2831 g_dbus_message_set_header (message,
2833 value == NULL ? NULL : g_variant_new_object_path (value));
2837 set_signature_header (GDBusMessage *message,
2838 GDBusMessageHeaderField header_field,
2841 g_dbus_message_set_header (message,
2843 value == NULL ? NULL : g_variant_new_signature (value));
2846 /* ---------------------------------------------------------------------------------------------------- */
2849 * g_dbus_message_get_reply_serial:
2850 * @message: A #GDBusMessage.
2852 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2854 * Returns: The value.
2859 g_dbus_message_get_reply_serial (GDBusMessage *message)
2861 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2862 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL);
2866 * g_dbus_message_set_reply_serial:
2867 * @message: A #GDBusMessage.
2868 * @value: The value to set.
2870 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2875 g_dbus_message_set_reply_serial (GDBusMessage *message,
2878 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2879 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value);
2882 /* ---------------------------------------------------------------------------------------------------- */
2885 * g_dbus_message_get_interface:
2886 * @message: A #GDBusMessage.
2888 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2890 * Returns: The value.
2895 g_dbus_message_get_interface (GDBusMessage *message)
2897 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2898 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE);
2902 * g_dbus_message_set_interface:
2903 * @message: A #GDBusMessage.
2904 * @value: The value to set.
2906 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2911 g_dbus_message_set_interface (GDBusMessage *message,
2914 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2915 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2916 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value);
2919 /* ---------------------------------------------------------------------------------------------------- */
2922 * g_dbus_message_get_member:
2923 * @message: A #GDBusMessage.
2925 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2927 * Returns: The value.
2932 g_dbus_message_get_member (GDBusMessage *message)
2934 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2935 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER);
2939 * g_dbus_message_set_member:
2940 * @message: A #GDBusMessage.
2941 * @value: The value to set.
2943 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2948 g_dbus_message_set_member (GDBusMessage *message,
2951 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2952 g_return_if_fail (value == NULL || g_dbus_is_member_name (value));
2953 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value);
2956 /* ---------------------------------------------------------------------------------------------------- */
2959 * g_dbus_message_get_path:
2960 * @message: A #GDBusMessage.
2962 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2964 * Returns: The value.
2969 g_dbus_message_get_path (GDBusMessage *message)
2971 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2972 return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH);
2976 * g_dbus_message_set_path:
2977 * @message: A #GDBusMessage.
2978 * @value: The value to set.
2980 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2985 g_dbus_message_set_path (GDBusMessage *message,
2988 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2989 g_return_if_fail (value == NULL || g_variant_is_object_path (value));
2990 set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value);
2993 /* ---------------------------------------------------------------------------------------------------- */
2996 * g_dbus_message_get_sender:
2997 * @message: A #GDBusMessage.
2999 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3001 * Returns: The value.
3006 g_dbus_message_get_sender (GDBusMessage *message)
3008 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3009 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER);
3013 * g_dbus_message_set_sender:
3014 * @message: A #GDBusMessage.
3015 * @value: The value to set.
3017 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3022 g_dbus_message_set_sender (GDBusMessage *message,
3025 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3026 g_return_if_fail (value == NULL || g_dbus_is_name (value));
3027 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value);
3030 /* ---------------------------------------------------------------------------------------------------- */
3033 * g_dbus_message_get_destination:
3034 * @message: A #GDBusMessage.
3036 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3038 * Returns: The value.
3043 g_dbus_message_get_destination (GDBusMessage *message)
3045 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3046 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION);
3050 * g_dbus_message_set_destination:
3051 * @message: A #GDBusMessage.
3052 * @value: The value to set.
3054 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3059 g_dbus_message_set_destination (GDBusMessage *message,
3062 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3063 g_return_if_fail (value == NULL || g_dbus_is_name (value));
3064 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value);
3067 /* ---------------------------------------------------------------------------------------------------- */
3070 * g_dbus_message_get_error_name:
3071 * @message: A #GDBusMessage.
3073 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3075 * Returns: The value.
3080 g_dbus_message_get_error_name (GDBusMessage *message)
3082 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3083 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME);
3087 * g_dbus_message_set_error_name:
3088 * @message: A #GDBusMessage.
3089 * @value: The value to set.
3091 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3096 g_dbus_message_set_error_name (GDBusMessage *message,
3099 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3100 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
3101 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value);
3104 /* ---------------------------------------------------------------------------------------------------- */
3107 * g_dbus_message_get_signature:
3108 * @message: A #GDBusMessage.
3110 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3112 * Returns: The value.
3117 g_dbus_message_get_signature (GDBusMessage *message)
3120 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3121 ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
3128 * g_dbus_message_set_signature:
3129 * @message: A #GDBusMessage.
3130 * @value: The value to set.
3132 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3137 g_dbus_message_set_signature (GDBusMessage *message,
3140 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3141 g_return_if_fail (value == NULL || g_variant_is_signature (value));
3142 set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value);
3145 /* ---------------------------------------------------------------------------------------------------- */
3148 * g_dbus_message_get_arg0:
3149 * @message: A #GDBusMessage.
3151 * Convenience to get the first item in the body of @message.
3153 * Returns: The string item or %NULL if the first item in the body of
3154 * @message is not a string.
3159 g_dbus_message_get_arg0 (GDBusMessage *message)
3163 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3167 if (message->body != NULL && g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE))
3170 item = g_variant_get_child_value (message->body, 0);
3171 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
3172 ret = g_variant_get_string (item, NULL);
3173 g_variant_unref (item);
3179 /* ---------------------------------------------------------------------------------------------------- */
3182 * g_dbus_message_get_num_unix_fds:
3183 * @message: A #GDBusMessage.
3185 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3187 * Returns: The value.
3192 g_dbus_message_get_num_unix_fds (GDBusMessage *message)
3194 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
3195 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS);
3199 * g_dbus_message_set_num_unix_fds:
3200 * @message: A #GDBusMessage.
3201 * @value: The value to set.
3203 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3208 g_dbus_message_set_num_unix_fds (GDBusMessage *message,
3211 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3212 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value);
3215 /* ---------------------------------------------------------------------------------------------------- */
3218 * g_dbus_message_to_gerror:
3219 * @message: A #GDBusMessage.
3220 * @error: The #GError to set.
3222 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
3223 * nothing and returns %FALSE.
3225 * Otherwise this method encodes the error in @message as a #GError
3226 * using g_dbus_error_set_dbus_error() using the information in the
3227 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
3228 * well as the first string item in @message's body.
3230 * Returns: %TRUE if @error was set, %FALSE otherwise.
3235 g_dbus_message_to_gerror (GDBusMessage *message,
3239 const gchar *error_name;
3241 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3244 if (message->type != G_DBUS_MESSAGE_TYPE_ERROR)
3247 error_name = g_dbus_message_get_error_name (message);
3248 if (error_name != NULL)
3252 body = g_dbus_message_get_body (message);
3254 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)")))
3256 const gchar *error_message;
3257 g_variant_get (body, "(&s)", &error_message);
3258 g_dbus_error_set_dbus_error (error,
3265 /* these two situations are valid, yet pretty rare */
3268 g_dbus_error_set_dbus_error (error,
3271 _("Error return with body of type '%s'"),
3272 g_variant_get_type_string (body));
3276 g_dbus_error_set_dbus_error (error,
3279 _("Error return with empty body"));
3285 /* TOOD: this shouldn't happen - should check this at message serialization
3286 * time and disconnect the peer.
3291 "Error return without error-name header!");
3300 /* ---------------------------------------------------------------------------------------------------- */
3303 flags_to_string (GType flags_type, guint value)
3309 klass = g_type_class_ref (flags_type);
3310 s = g_string_new (NULL);
3311 for (n = 0; n < 32; n++)
3313 if ((value & (1<<n)) != 0)
3315 GFlagsValue *flags_value;
3316 flags_value = g_flags_get_first_value (klass, (1<<n));
3318 g_string_append_c (s, ',');
3319 if (flags_value != NULL)
3320 g_string_append (s, flags_value->value_nick);
3322 g_string_append_printf (s, "unknown (bit %d)", n);
3326 g_string_append (s, "none");
3327 g_type_class_unref (klass);
3328 return g_string_free (s, FALSE);
3332 _sort_keys_func (gconstpointer a,
3338 ia = GPOINTER_TO_INT (a);
3339 ib = GPOINTER_TO_INT (b);
3345 * g_dbus_message_print:
3346 * @message: A #GDBusMessage.
3347 * @indent: Indentation level.
3349 * Produces a human-readable multi-line description of @message.
3351 * The contents of the description has no ABI guarantees, the contents
3352 * and formatting is subject to change at any time. Typical output
3353 * looks something like this:
3355 * Type: method-call
3360 * path -> objectpath '/org/gtk/GDBus/TestObject'
3361 * interface -> 'org.gtk.GDBus.TestInterface'
3362 * member -> 'GimmeStdout'
3363 * destination -> ':1.146'
3365 * UNIX File Descriptors:
3370 * Type: method-return
3371 * Flags: no-reply-expected
3375 * reply-serial -> uint32 4
3376 * destination -> ':1.159'
3377 * sender -> ':1.146'
3378 * num-unix-fds -> uint32 1
3380 * UNIX File Descriptors:
3381 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3384 * Returns: A string that should be freed with g_free().
3389 g_dbus_message_print (GDBusMessage *message,
3397 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3399 str = g_string_new (NULL);
3401 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type);
3402 g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
3404 s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags);
3405 g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
3407 g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version);
3408 g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial);
3410 g_string_append_printf (str, "%*sHeaders:\n", indent, "");
3411 keys = g_hash_table_get_keys (message->headers);
3412 keys = g_list_sort (keys, _sort_keys_func);
3415 for (l = keys; l != NULL; l = l->next)
3417 gint key = GPOINTER_TO_INT (l->data);
3421 value = g_hash_table_lookup (message->headers, l->data);
3422 g_assert (value != NULL);
3424 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
3425 value_str = g_variant_print (value, TRUE);
3426 g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
3433 g_string_append_printf (str, "%*s (none)\n", indent, "");
3435 g_string_append_printf (str, "%*sBody: ", indent, "");
3436 if (message->body != NULL)
3438 g_variant_print_string (message->body,
3444 g_string_append (str, "()");
3446 g_string_append (str, "\n");
3448 g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
3449 if (message->fd_list != NULL)
3455 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3458 for (n = 0; n < num_fds; n++)
3461 struct stat statbuf;
3462 fs = g_string_new (NULL);
3463 if (fstat (fds[n], &statbuf) == 0)
3465 g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "",
3466 major (statbuf.st_dev), minor (statbuf.st_dev));
3467 g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "",
3469 g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3470 (guint64) statbuf.st_ino);
3471 g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "",
3472 (guint) statbuf.st_uid);
3473 g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "",
3474 (guint) statbuf.st_gid);
3475 g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
3476 major (statbuf.st_rdev), minor (statbuf.st_rdev));
3477 g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3478 (guint64) statbuf.st_size);
3479 g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3480 (guint64) statbuf.st_atime);
3481 g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3482 (guint64) statbuf.st_mtime);
3483 g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3484 (guint64) statbuf.st_ctime);
3488 g_string_append_printf (fs, "(fstat failed: %s)", strerror (errno));
3490 g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str);
3491 g_string_free (fs, TRUE);
3496 g_string_append_printf (str, "%*s (empty)\n", indent, "");
3501 g_string_append_printf (str, "%*s (none)\n", indent, "");
3505 return g_string_free (str, FALSE);
3509 * g_dbus_message_get_locked:
3510 * @message: A #GDBusMessage.
3512 * Checks whether @message is locked. To monitor changes to this
3513 * value, conncet to the #GObject::notify signal to listen for changes
3514 * on the #GDBusMessage:locked property.
3516 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3521 g_dbus_message_get_locked (GDBusMessage *message)
3523 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3524 return message->locked;
3528 * g_dbus_message_lock:
3529 * @message: A #GDBusMessage.
3531 * If @message is locked, does nothing. Otherwise locks the message.
3536 g_dbus_message_lock (GDBusMessage *message)
3538 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3540 if (message->locked)
3543 message->locked = TRUE;
3544 g_object_notify (G_OBJECT (message), "locked");
3551 * g_dbus_message_copy:
3552 * @message: A #GDBusMessage.
3553 * @error: Return location for error or %NULL.
3555 * Copies @message. The copy is a deep copy and the returned
3556 * #GDBusMessage is completely identical except that it is guaranteed
3559 * This operation can fail if e.g. @message contains file descriptors
3560 * and the per-process or system-wide open files limit is reached.
3562 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3563 * Free with g_object_unref().
3568 g_dbus_message_copy (GDBusMessage *message,
3572 GHashTableIter iter;
3573 gpointer header_key;
3574 GVariant *header_value;
3576 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3577 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3579 ret = g_dbus_message_new ();
3580 ret->type = message->type;
3581 ret->flags = message->flags;
3582 ret->byte_order = message->byte_order;
3583 ret->major_protocol_version = message->major_protocol_version;
3584 ret->serial = message->serial;
3587 if (message->fd_list != NULL)
3593 ret->fd_list = g_unix_fd_list_new ();
3594 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3595 for (n = 0; n < num_fds; n++)
3597 if (g_unix_fd_list_append (ret->fd_list,
3601 g_object_unref (ret);
3609 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3610 * to just ref (as opposed to deep-copying) the GVariant instances
3612 ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL;
3613 g_hash_table_iter_init (&iter, message->headers);
3614 while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value))
3615 g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value));