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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 /* Uncomment to debug serializer code */
22 /* #define DEBUG_SERIALIZER */
28 #include <sys/types.h>
32 #include <sys/mkdev.h>
33 #elif MAJOR_IN_SYSMACROS
34 #include <sys/sysmacros.h>
37 #include "gdbusutils.h"
38 #include "gdbusmessage.h"
39 #include "gdbuserror.h"
40 #include "gioenumtypes.h"
41 #include "ginputstream.h"
42 #include "gdatainputstream.h"
43 #include "gmemoryinputstream.h"
44 #include "goutputstream.h"
45 #include "gdataoutputstream.h"
46 #include "gmemoryoutputstream.h"
47 #include "gseekable.h"
49 #include "gdbusprivate.h"
52 #include "gunixfdlist.h"
57 typedef struct _GMemoryBuffer GMemoryBuffer;
64 GDataStreamByteOrder byte_order;
68 g_memory_buffer_read_byte (GMemoryBuffer *mbuf)
70 if (mbuf->pos >= mbuf->valid_len)
72 return mbuf->data [mbuf->pos++];
76 g_memory_buffer_read_int16 (GMemoryBuffer *mbuf)
80 if (mbuf->pos > mbuf->valid_len - 2)
82 mbuf->pos = mbuf->valid_len;
86 memcpy (&v, mbuf->data + mbuf->pos, 2);
88 switch (mbuf->byte_order)
90 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
91 v = GINT16_FROM_BE (v);
93 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
94 v = GINT16_FROM_LE (v);
96 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
104 g_memory_buffer_read_uint16 (GMemoryBuffer *mbuf)
108 if (mbuf->pos > mbuf->valid_len - 2)
110 mbuf->pos = mbuf->valid_len;
114 memcpy (&v, mbuf->data + mbuf->pos, 2);
116 switch (mbuf->byte_order)
118 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
119 v = GINT16_FROM_BE (v);
121 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
122 v = GINT16_FROM_LE (v);
124 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
132 g_memory_buffer_read_int32 (GMemoryBuffer *mbuf)
136 if (mbuf->pos > mbuf->valid_len - 4)
138 mbuf->pos = mbuf->valid_len;
142 memcpy (&v, mbuf->data + mbuf->pos, 4);
144 switch (mbuf->byte_order)
146 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
147 v = GINT32_FROM_BE (v);
149 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
150 v = GINT32_FROM_LE (v);
152 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
160 g_memory_buffer_read_uint32 (GMemoryBuffer *mbuf)
164 if (mbuf->pos > mbuf->valid_len - 4)
166 mbuf->pos = mbuf->valid_len;
170 memcpy (&v, mbuf->data + mbuf->pos, 4);
172 switch (mbuf->byte_order)
174 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
175 v = GUINT32_FROM_BE (v);
177 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
178 v = GUINT32_FROM_LE (v);
180 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
188 g_memory_buffer_read_int64 (GMemoryBuffer *mbuf)
192 if (mbuf->pos > mbuf->valid_len - 8)
194 mbuf->pos = mbuf->valid_len;
198 memcpy (&v, mbuf->data + mbuf->pos, 8);
200 switch (mbuf->byte_order)
202 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
203 v = GINT64_FROM_BE (v);
205 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
206 v = GINT64_FROM_LE (v);
208 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
216 g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf)
220 if (mbuf->pos > mbuf->valid_len - 8)
222 mbuf->pos = mbuf->valid_len;
226 memcpy (&v, mbuf->data + mbuf->pos, 8);
228 switch (mbuf->byte_order)
230 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
231 v = GUINT64_FROM_BE (v);
233 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
234 v = GUINT64_FROM_LE (v);
236 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
243 #define MIN_ARRAY_SIZE 128
246 g_nearest_pow (gint num)
257 array_resize (GMemoryBuffer *mbuf,
263 if (mbuf->len == size)
267 data = g_realloc (mbuf->data, size);
270 memset ((guint8 *)data + len, 0, size - len);
275 if (mbuf->len < mbuf->valid_len)
276 mbuf->valid_len = mbuf->len;
280 g_memory_buffer_write (GMemoryBuffer *mbuf,
290 /* Check for address space overflow, but only if the buffer is resizable.
291 Otherwise we just do a short write and don't worry. */
292 if (mbuf->pos + count < mbuf->pos)
295 if (mbuf->pos + count > mbuf->len)
297 /* At least enought to fit the write, rounded up
298 for greater than linear growth.
299 TODO: This wastes a lot of memory at large buffer sizes.
300 Figure out a more rational allocation strategy. */
301 new_size = g_nearest_pow (mbuf->pos + count);
302 /* Check for overflow again. We have only checked if
303 pos + count > G_MAXSIZE, but it only catches the case of writing
304 more than 4GiB total on a 32-bit system. There's still the problem
305 of g_nearest_pow overflowing above 0x7fffffff, so we're
306 effectively limited to 2GiB. */
307 if (new_size < mbuf->len)
310 new_size = MAX (new_size, MIN_ARRAY_SIZE);
311 array_resize (mbuf, new_size);
314 dest = (guint8 *)mbuf->data + mbuf->pos;
315 memcpy (dest, buffer, count);
318 if (mbuf->pos > mbuf->valid_len)
319 mbuf->valid_len = mbuf->pos;
325 g_memory_buffer_put_byte (GMemoryBuffer *mbuf,
328 return g_memory_buffer_write (mbuf, &data, 1);
332 g_memory_buffer_put_int16 (GMemoryBuffer *mbuf,
335 switch (mbuf->byte_order)
337 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
338 data = GINT16_TO_BE (data);
340 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
341 data = GINT16_TO_LE (data);
343 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
348 return g_memory_buffer_write (mbuf, &data, 2);
352 g_memory_buffer_put_uint16 (GMemoryBuffer *mbuf,
355 switch (mbuf->byte_order)
357 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
358 data = GUINT16_TO_BE (data);
360 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
361 data = GUINT16_TO_LE (data);
363 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
368 return g_memory_buffer_write (mbuf, &data, 2);
372 g_memory_buffer_put_int32 (GMemoryBuffer *mbuf,
375 switch (mbuf->byte_order)
377 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
378 data = GINT32_TO_BE (data);
380 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
381 data = GINT32_TO_LE (data);
383 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
388 return g_memory_buffer_write (mbuf, &data, 4);
392 g_memory_buffer_put_uint32 (GMemoryBuffer *mbuf,
395 switch (mbuf->byte_order)
397 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
398 data = GUINT32_TO_BE (data);
400 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
401 data = GUINT32_TO_LE (data);
403 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
408 return g_memory_buffer_write (mbuf, &data, 4);
412 g_memory_buffer_put_int64 (GMemoryBuffer *mbuf,
415 switch (mbuf->byte_order)
417 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
418 data = GINT64_TO_BE (data);
420 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
421 data = GINT64_TO_LE (data);
423 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
428 return g_memory_buffer_write (mbuf, &data, 8);
432 g_memory_buffer_put_uint64 (GMemoryBuffer *mbuf,
435 switch (mbuf->byte_order)
437 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
438 data = GUINT64_TO_BE (data);
440 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
441 data = GUINT64_TO_LE (data);
443 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
448 return g_memory_buffer_write (mbuf, &data, 8);
452 g_memory_buffer_put_string (GMemoryBuffer *mbuf,
455 g_return_val_if_fail (str != NULL, FALSE);
457 return g_memory_buffer_write (mbuf, str, strlen (str));
462 * SECTION:gdbusmessage
463 * @short_description: D-Bus Message
464 * @include: gio/gio.h
466 * A type for representing D-Bus messages that can be sent or received
467 * on a #GDBusConnection.
470 typedef struct _GDBusMessageClass GDBusMessageClass;
475 * Class structure for #GDBusMessage.
479 struct _GDBusMessageClass
482 GObjectClass parent_class;
488 * The #GDBusMessage structure contains only private data and should
489 * only be accessed using the provided API.
496 GObject parent_instance;
498 GDBusMessageType type;
499 GDBusMessageFlags flags;
501 GDBusMessageByteOrder byte_order;
502 guchar major_protocol_version;
507 GUnixFDList *fd_list;
517 G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT);
520 g_dbus_message_finalize (GObject *object)
522 GDBusMessage *message = G_DBUS_MESSAGE (object);
524 if (message->headers != NULL)
525 g_hash_table_unref (message->headers);
526 if (message->body != NULL)
527 g_variant_unref (message->body);
529 if (message->fd_list != NULL)
530 g_object_unref (message->fd_list);
533 if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL)
534 G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object);
538 g_dbus_message_get_property (GObject *object,
543 GDBusMessage *message = G_DBUS_MESSAGE (object);
548 g_value_set_boolean (value, g_dbus_message_get_locked (message));
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 g_dbus_message_class_init (GDBusMessageClass *klass)
560 GObjectClass *gobject_class;
562 gobject_class = G_OBJECT_CLASS (klass);
563 gobject_class->finalize = g_dbus_message_finalize;
564 gobject_class->get_property = g_dbus_message_get_property;
567 * GDBusConnection:locked:
569 * A boolean specifying whether the message is locked.
573 g_object_class_install_property (gobject_class,
575 g_param_spec_boolean ("locked",
577 P_("Whether the message is locked"),
580 G_PARAM_STATIC_NAME |
581 G_PARAM_STATIC_BLURB |
582 G_PARAM_STATIC_NICK));
586 g_dbus_message_init (GDBusMessage *message)
588 /* Any D-Bus implementation is supposed to handle both Big and
589 * Little Endian encodings and the Endianness is part of the D-Bus
590 * message - we prefer to use Big Endian (since it's Network Byte
591 * Order and just easier to read for humans) but if the machine is
592 * Little Endian we use that for performance reasons.
594 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
595 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
597 /* this could also be G_PDP_ENDIAN */
598 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
600 message->headers = g_hash_table_new_full (g_direct_hash,
603 (GDestroyNotify) g_variant_unref);
607 * g_dbus_message_new:
609 * Creates a new empty #GDBusMessage.
611 * Returns: A #GDBusMessage. Free with g_object_unref().
616 g_dbus_message_new (void)
618 return g_object_new (G_TYPE_DBUS_MESSAGE, NULL);
622 * g_dbus_message_new_method_call:
623 * @name: (allow-none): A valid D-Bus name or %NULL.
624 * @path: A valid object path.
625 * @interface_: (allow-none): A valid D-Bus interface name or %NULL.
626 * @method: A valid method name.
628 * Creates a new #GDBusMessage for a method call.
630 * Returns: A #GDBusMessage. Free with g_object_unref().
635 g_dbus_message_new_method_call (const gchar *name,
637 const gchar *interface_,
640 GDBusMessage *message;
642 g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL);
643 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
644 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
645 g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
647 message = g_dbus_message_new ();
648 message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
651 g_dbus_message_set_destination (message, name);
652 g_dbus_message_set_path (message, path);
653 g_dbus_message_set_member (message, method);
654 if (interface_ != NULL)
655 g_dbus_message_set_interface (message, interface_);
661 * g_dbus_message_new_signal:
662 * @path: A valid object path.
663 * @interface_: A valid D-Bus interface name.
664 * @signal: A valid signal name.
666 * Creates a new #GDBusMessage for a signal emission.
668 * Returns: A #GDBusMessage. Free with g_object_unref().
673 g_dbus_message_new_signal (const gchar *path,
674 const gchar *interface_,
677 GDBusMessage *message;
679 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
680 g_return_val_if_fail (g_dbus_is_member_name (signal), NULL);
681 g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL);
683 message = g_dbus_message_new ();
684 message->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
685 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
687 g_dbus_message_set_path (message, path);
688 g_dbus_message_set_member (message, signal);
689 g_dbus_message_set_interface (message, interface_);
696 * g_dbus_message_new_method_reply:
697 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
698 * create a reply message to.
700 * Creates a new #GDBusMessage that is a reply to @method_call_message.
702 * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
707 g_dbus_message_new_method_reply (GDBusMessage *method_call_message)
709 GDBusMessage *message;
712 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
713 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
714 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
716 message = g_dbus_message_new ();
717 message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
718 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
719 /* reply with same endianness */
720 message->byte_order = method_call_message->byte_order;
722 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
723 sender = g_dbus_message_get_sender (method_call_message);
725 g_dbus_message_set_destination (message, sender);
731 * g_dbus_message_new_method_error:
732 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
733 * create a reply message to.
734 * @error_name: A valid D-Bus error name.
735 * @error_message_format: The D-Bus error message in a printf() format.
736 * @...: Arguments for @error_message_format.
738 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
740 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
745 g_dbus_message_new_method_error (GDBusMessage *method_call_message,
746 const gchar *error_name,
747 const gchar *error_message_format,
753 va_start (var_args, error_message_format);
754 ret = g_dbus_message_new_method_error_valist (method_call_message,
756 error_message_format,
764 * g_dbus_message_new_method_error_literal:
765 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
766 * create a reply message to.
767 * @error_name: A valid D-Bus error name.
768 * @error_message: The D-Bus error message.
770 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
772 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
777 g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message,
778 const gchar *error_name,
779 const gchar *error_message)
781 GDBusMessage *message;
784 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
785 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
786 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
787 g_return_val_if_fail (g_dbus_is_name (error_name), NULL);
788 g_return_val_if_fail (error_message != NULL, NULL);
790 message = g_dbus_message_new ();
791 message->type = G_DBUS_MESSAGE_TYPE_ERROR;
792 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
793 /* reply with same endianness */
794 message->byte_order = method_call_message->byte_order;
796 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
797 g_dbus_message_set_error_name (message, error_name);
798 g_dbus_message_set_body (message, g_variant_new ("(s)", error_message));
800 sender = g_dbus_message_get_sender (method_call_message);
802 g_dbus_message_set_destination (message, sender);
808 * g_dbus_message_new_method_error_valist:
809 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
810 * create a reply message to.
811 * @error_name: A valid D-Bus error name.
812 * @error_message_format: The D-Bus error message in a printf() format.
813 * @var_args: Arguments for @error_message_format.
815 * Like g_dbus_message_new_method_error() but intended for language bindings.
817 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
823 g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message,
824 const gchar *error_name,
825 const gchar *error_message_format,
829 gchar *error_message;
830 error_message = g_strdup_vprintf (error_message_format, var_args);
831 ret = g_dbus_message_new_method_error_literal (method_call_message,
834 g_free (error_message);
838 /* ---------------------------------------------------------------------------------------------------- */
841 * g_dbus_message_get_byte_order:
842 * @message: A #GDBusMessage.
844 * Gets the byte order of @message.
846 * Returns: The byte order.
848 GDBusMessageByteOrder
849 g_dbus_message_get_byte_order (GDBusMessage *message)
851 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0);
852 return message->byte_order;
856 * g_dbus_message_set_byte_order:
857 * @message: A #GDBusMessage.
858 * @byte_order: The byte order.
860 * Sets the byte order of @message.
863 g_dbus_message_set_byte_order (GDBusMessage *message,
864 GDBusMessageByteOrder byte_order)
866 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
870 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
874 message->byte_order = byte_order;
877 /* ---------------------------------------------------------------------------------------------------- */
879 /* TODO: need GI annotations to specify that any guchar value goes for the type */
882 * g_dbus_message_get_message_type:
883 * @message: A #GDBusMessage.
885 * Gets the type of @message.
887 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
892 g_dbus_message_get_message_type (GDBusMessage *message)
894 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID);
895 return message->type;
899 * g_dbus_message_set_message_type:
900 * @message: A #GDBusMessage.
901 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
903 * Sets @message to be of @type.
908 g_dbus_message_set_message_type (GDBusMessage *message,
909 GDBusMessageType type)
911 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
912 g_return_if_fail (type >=0 && type < 256);
916 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
920 message->type = type;
923 /* ---------------------------------------------------------------------------------------------------- */
925 /* TODO: need GI annotations to specify that any guchar value goes for flags */
928 * g_dbus_message_get_flags:
929 * @message: A #GDBusMessage.
931 * Gets the flags for @message.
933 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
938 g_dbus_message_get_flags (GDBusMessage *message)
940 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE);
941 return message->flags;
945 * g_dbus_message_set_flags:
946 * @message: A #GDBusMessage.
947 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
948 * enumeration bitwise ORed together).
950 * Sets the flags to set on @message.
955 g_dbus_message_set_flags (GDBusMessage *message,
956 GDBusMessageFlags flags)
958 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
959 g_return_if_fail (flags >=0 && flags < 256);
963 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
967 message->flags = flags;
970 /* ---------------------------------------------------------------------------------------------------- */
973 * g_dbus_message_get_serial:
974 * @message: A #GDBusMessage.
976 * Gets the serial for @message.
978 * Returns: A #guint32.
983 g_dbus_message_get_serial (GDBusMessage *message)
985 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
986 return message->serial;
990 * g_dbus_message_set_serial:
991 * @message: A #GDBusMessage.
992 * @serial: A #guint32.
994 * Sets the serial for @message.
999 g_dbus_message_set_serial (GDBusMessage *message,
1002 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1004 if (message->locked)
1006 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1010 message->serial = serial;
1013 /* ---------------------------------------------------------------------------------------------------- */
1015 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
1018 * g_dbus_message_get_header:
1019 * @message: A #GDBusMessage.
1020 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1022 * Gets a header field on @message.
1024 * Returns: A #GVariant with the value if the header was found, %NULL
1025 * otherwise. Do not free, it is owned by @message.
1030 g_dbus_message_get_header (GDBusMessage *message,
1031 GDBusMessageHeaderField header_field)
1033 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1034 g_return_val_if_fail (header_field >=0 && header_field < 256, NULL);
1035 return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
1039 * g_dbus_message_set_header:
1040 * @message: A #GDBusMessage.
1041 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1042 * @value: (allow-none): A #GVariant to set the header field or %NULL to clear the header field.
1044 * Sets a header field on @message.
1046 * If @value is floating, @message assumes ownership of @value.
1051 g_dbus_message_set_header (GDBusMessage *message,
1052 GDBusMessageHeaderField header_field,
1055 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1056 g_return_if_fail (header_field >=0 && header_field < 256);
1058 if (message->locked)
1060 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1066 g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field));
1070 g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
1075 * g_dbus_message_get_header_fields:
1076 * @message: A #GDBusMessage.
1078 * Gets an array of all header fields on @message that are set.
1080 * Returns: (array zero-terminated=1): An array of header fields
1081 * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
1082 * is a #guchar. Free with g_free().
1087 g_dbus_message_get_header_fields (GDBusMessage *message)
1095 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1097 keys = g_hash_table_get_keys (message->headers);
1098 num_keys = g_list_length (keys);
1099 ret = g_new (guchar, num_keys + 1);
1100 for (l = keys, n = 0; l != NULL; l = l->next, n++)
1101 ret[n] = GPOINTER_TO_UINT (l->data);
1102 g_assert (n == num_keys);
1103 ret[n] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID;
1109 /* ---------------------------------------------------------------------------------------------------- */
1112 * g_dbus_message_get_body:
1113 * @message: A #GDBusMessage.
1115 * Gets the body of a message.
1117 * Returns: A #GVariant or %NULL if the body is empty. Do not free, it is owned by @message.
1122 g_dbus_message_get_body (GDBusMessage *message)
1124 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1125 return message->body;
1129 * g_dbus_message_set_body:
1130 * @message: A #GDBusMessage.
1131 * @body: Either %NULL or a #GVariant that is a tuple.
1133 * Sets the body @message. As a side-effect the
1134 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
1135 * type string of @body (or cleared if @body is %NULL).
1137 * If @body is floating, @message assumes ownership of @body.
1142 g_dbus_message_set_body (GDBusMessage *message,
1145 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1146 g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE));
1148 if (message->locked)
1150 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1154 if (message->body != NULL)
1155 g_variant_unref (message->body);
1158 message->body = NULL;
1159 g_dbus_message_set_signature (message, NULL);
1163 const gchar *type_string;
1164 gsize type_string_len;
1167 message->body = g_variant_ref_sink (body);
1169 type_string = g_variant_get_type_string (body);
1170 type_string_len = strlen (type_string);
1171 g_assert (type_string_len >= 2);
1172 signature = g_strndup (type_string + 1, type_string_len - 2);
1173 g_dbus_message_set_signature (message, signature);
1178 /* ---------------------------------------------------------------------------------------------------- */
1182 * g_dbus_message_get_unix_fd_list:
1183 * @message: A #GDBusMessage.
1185 * Gets the UNIX file descriptors associated with @message, if any.
1187 * This method is only available on UNIX.
1189 * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
1190 * associated. Do not free, this object is owned by @message.
1195 g_dbus_message_get_unix_fd_list (GDBusMessage *message)
1197 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1198 return message->fd_list;
1202 * g_dbus_message_set_unix_fd_list:
1203 * @message: A #GDBusMessage.
1204 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
1206 * Sets the UNIX file descriptors associated with @message. As a
1207 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
1208 * field is set to the number of fds in @fd_list (or cleared if
1209 * @fd_list is %NULL).
1211 * This method is only available on UNIX.
1216 g_dbus_message_set_unix_fd_list (GDBusMessage *message,
1217 GUnixFDList *fd_list)
1219 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1220 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
1222 if (message->locked)
1224 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1228 if (message->fd_list != NULL)
1229 g_object_unref (message->fd_list);
1230 if (fd_list != NULL)
1232 message->fd_list = g_object_ref (fd_list);
1233 g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list));
1237 message->fd_list = NULL;
1238 g_dbus_message_set_num_unix_fds (message, 0);
1243 /* ---------------------------------------------------------------------------------------------------- */
1246 validate_headers (GDBusMessage *message,
1251 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1252 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1256 switch (message->type)
1258 case G_DBUS_MESSAGE_TYPE_INVALID:
1259 g_set_error_literal (error,
1261 G_IO_ERROR_INVALID_ARGUMENT,
1262 _("type is INVALID"));
1266 case G_DBUS_MESSAGE_TYPE_METHOD_CALL:
1267 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
1268 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
1270 g_set_error_literal (error,
1272 G_IO_ERROR_INVALID_ARGUMENT,
1273 _("METHOD_CALL message: PATH or MEMBER header field is missing"));
1278 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
1279 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
1281 g_set_error_literal (error,
1283 G_IO_ERROR_INVALID_ARGUMENT,
1284 _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
1289 case G_DBUS_MESSAGE_TYPE_ERROR:
1290 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME) == NULL ||
1291 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
1293 g_set_error_literal (error,
1295 G_IO_ERROR_INVALID_ARGUMENT,
1296 _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
1301 case G_DBUS_MESSAGE_TYPE_SIGNAL:
1302 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
1303 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE) == NULL ||
1304 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
1306 g_set_error_literal (error,
1308 G_IO_ERROR_INVALID_ARGUMENT,
1309 _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
1312 if (g_strcmp0 (g_dbus_message_get_path (message), "/org/freedesktop/DBus/Local") == 0)
1314 g_set_error_literal (error,
1316 G_IO_ERROR_INVALID_ARGUMENT,
1317 _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
1320 if (g_strcmp0 (g_dbus_message_get_interface (message), "org.freedesktop.DBus.Local") == 0)
1322 g_set_error_literal (error,
1324 G_IO_ERROR_INVALID_ARGUMENT,
1325 _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
1331 /* hitherto unknown type - nothing to check */
1338 g_assert (ret || (error == NULL || *error != NULL));
1342 /* ---------------------------------------------------------------------------------------------------- */
1345 ensure_input_padding (GMemoryBuffer *buf,
1349 gsize wanted_offset;
1352 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
1353 buf->pos = wanted_offset;
1357 static const gchar *
1358 read_string (GMemoryBuffer *mbuf,
1363 const gchar *end_valid;
1365 if (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos)
1367 mbuf->pos = mbuf->valid_len;
1368 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1371 G_IO_ERROR_INVALID_ARGUMENT,
1372 g_dngettext (GETTEXT_PACKAGE,
1373 "Wanted to read %lu byte but only got %lu",
1374 "Wanted to read %lu bytes but only got %lu",
1377 (gulong)(mbuf->valid_len - mbuf->pos));
1381 if (mbuf->data[mbuf->pos + len] != '\0')
1383 str = g_strndup (mbuf->data + mbuf->pos, len);
1386 G_IO_ERROR_INVALID_ARGUMENT,
1387 _("Expected NUL byte after the string '%s' but found byte %d"),
1388 str, mbuf->data[mbuf->pos + len]);
1390 mbuf->pos += len + 1;
1394 str = mbuf->data + mbuf->pos;
1395 mbuf->pos += len + 1;
1397 if (!g_utf8_validate (str, -1, &end_valid))
1401 offset = (gint) (end_valid - str);
1402 valid_str = g_strndup (str, offset);
1405 G_IO_ERROR_INVALID_ARGUMENT,
1406 _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
1407 "The valid UTF-8 string up until that point was '%s'"),
1418 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
1420 /* returns a non-floating GVariant! */
1422 parse_value_from_blob (GMemoryBuffer *buf,
1423 const GVariantType *type,
1424 gboolean just_align,
1429 GError *local_error;
1431 const gchar *type_string;
1433 type_string = g_variant_type_peek_string (type);
1435 #ifdef DEBUG_SERIALIZER
1438 s = g_variant_type_dup_string (type);
1439 g_print ("%*s%s type %s from offset 0x%04x",
1441 just_align ? "Aligning" : "Reading",
1443 (gint) g_seekable_tell (G_SEEKABLE (buf)));
1446 #endif /* DEBUG_SERIALIZER */
1452 switch (type_string[0])
1454 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1455 ensure_input_padding (buf, 4);
1459 v = g_memory_buffer_read_uint32 (buf);
1460 ret = g_variant_new_boolean (v);
1464 case 'y': /* G_VARIANT_TYPE_BYTE */
1468 v = g_memory_buffer_read_byte (buf);
1469 ret = g_variant_new_byte (v);
1473 case 'n': /* G_VARIANT_TYPE_INT16 */
1474 ensure_input_padding (buf, 2);
1478 v = g_memory_buffer_read_int16 (buf);
1479 ret = g_variant_new_int16 (v);
1483 case 'q': /* G_VARIANT_TYPE_UINT16 */
1484 ensure_input_padding (buf, 2);
1488 v = g_memory_buffer_read_uint16 (buf);
1489 ret = g_variant_new_uint16 (v);
1493 case 'i': /* G_VARIANT_TYPE_INT32 */
1494 ensure_input_padding (buf, 4);
1498 v = g_memory_buffer_read_int32 (buf);
1499 ret = g_variant_new_int32 (v);
1503 case 'u': /* G_VARIANT_TYPE_UINT32 */
1504 ensure_input_padding (buf, 4);
1508 v = g_memory_buffer_read_uint32 (buf);
1509 ret = g_variant_new_uint32 (v);
1513 case 'x': /* G_VARIANT_TYPE_INT64 */
1514 ensure_input_padding (buf, 8);
1518 v = g_memory_buffer_read_int64 (buf);
1519 ret = g_variant_new_int64 (v);
1523 case 't': /* G_VARIANT_TYPE_UINT64 */
1524 ensure_input_padding (buf, 8);
1528 v = g_memory_buffer_read_uint64 (buf);
1529 ret = g_variant_new_uint64 (v);
1533 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1534 ensure_input_padding (buf, 8);
1541 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
1542 u.v_uint64 = g_memory_buffer_read_uint64 (buf);
1543 ret = g_variant_new_double (u.v_double);
1547 case 's': /* G_VARIANT_TYPE_STRING */
1548 ensure_input_padding (buf, 4);
1553 len = g_memory_buffer_read_uint32 (buf);
1554 v = read_string (buf, (gsize) len, &local_error);
1557 ret = g_variant_new_string (v);
1561 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1562 ensure_input_padding (buf, 4);
1567 len = g_memory_buffer_read_uint32 (buf);
1568 v = read_string (buf, (gsize) len, &local_error);
1571 if (!g_variant_is_object_path (v))
1573 g_set_error (&local_error,
1575 G_IO_ERROR_INVALID_ARGUMENT,
1576 _("Parsed value '%s' is not a valid D-Bus object path"),
1580 ret = g_variant_new_object_path (v);
1584 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1589 len = g_memory_buffer_read_byte (buf);
1590 v = read_string (buf, (gsize) len, &local_error);
1593 if (!g_variant_is_signature (v))
1595 g_set_error (&local_error,
1597 G_IO_ERROR_INVALID_ARGUMENT,
1598 _("Parsed value '%s' is not a valid D-Bus signature"),
1602 ret = g_variant_new_signature (v);
1606 case 'h': /* G_VARIANT_TYPE_HANDLE */
1607 ensure_input_padding (buf, 4);
1611 v = g_memory_buffer_read_int32 (buf);
1612 ret = g_variant_new_handle (v);
1616 case 'a': /* G_VARIANT_TYPE_ARRAY */
1617 ensure_input_padding (buf, 4);
1619 /* If we are only aligning for this array type, it is the child type of
1620 * another array, which is empty. So, we do not need to add padding for
1621 * this nonexistent array's elements: we only need to align for this
1622 * array itself (4 bytes). See
1623 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1630 const GVariantType *element_type;
1631 GVariantBuilder builder;
1633 array_len = g_memory_buffer_read_uint32 (buf);
1636 #ifdef DEBUG_SERIALIZER
1637 g_print (": array spans 0x%04x bytes\n", array_len);
1638 #endif /* DEBUG_SERIALIZER */
1640 if (array_len > (2<<26))
1642 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1643 g_set_error (&local_error,
1645 G_IO_ERROR_INVALID_ARGUMENT,
1646 g_dngettext (GETTEXT_PACKAGE,
1647 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1648 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1654 g_variant_builder_init (&builder, type);
1655 element_type = g_variant_type_element (type);
1660 item = parse_value_from_blob (buf,
1665 g_assert (item == NULL);
1669 /* TODO: optimize array of primitive types */
1671 target = offset + array_len;
1672 while (offset < target)
1675 item = parse_value_from_blob (buf,
1682 g_variant_builder_clear (&builder);
1685 g_variant_builder_add_value (&builder, item);
1686 g_variant_unref (item);
1691 ret = g_variant_builder_end (&builder);
1696 if (g_variant_type_is_dict_entry (type))
1698 const GVariantType *key_type;
1699 const GVariantType *value_type;
1703 ensure_input_padding (buf, 8);
1706 #ifdef DEBUG_SERIALIZER
1708 #endif /* DEBUG_SERIALIZER */
1712 key_type = g_variant_type_key (type);
1713 key = parse_value_from_blob (buf,
1720 value_type = g_variant_type_value (type);
1721 value = parse_value_from_blob (buf,
1728 g_variant_unref (key);
1731 ret = g_variant_new_dict_entry (key, value);
1732 g_variant_unref (key);
1733 g_variant_unref (value);
1736 else if (g_variant_type_is_tuple (type))
1738 ensure_input_padding (buf, 8);
1741 #ifdef DEBUG_SERIALIZER
1743 #endif /* DEBUG_SERIALIZER */
1747 const GVariantType *element_type;
1748 GVariantBuilder builder;
1750 g_variant_builder_init (&builder, type);
1751 element_type = g_variant_type_first (type);
1752 while (element_type != NULL)
1755 item = parse_value_from_blob (buf,
1762 g_variant_builder_clear (&builder);
1765 g_variant_builder_add_value (&builder, item);
1766 g_variant_unref (item);
1768 element_type = g_variant_type_next (element_type);
1770 ret = g_variant_builder_end (&builder);
1773 else if (g_variant_type_is_variant (type))
1776 #ifdef DEBUG_SERIALIZER
1778 #endif /* DEBUG_SERIALIZER */
1784 GVariantType *variant_type;
1787 siglen = g_memory_buffer_read_byte (buf);
1788 sig = read_string (buf, (gsize) siglen, &local_error);
1791 if (!g_variant_is_signature (sig))
1793 g_set_error (&local_error,
1795 G_IO_ERROR_INVALID_ARGUMENT,
1796 _("Parsed value '%s' for variant is not a valid D-Bus signature"),
1800 variant_type = g_variant_type_new (sig);
1801 value = parse_value_from_blob (buf,
1806 g_variant_type_free (variant_type);
1809 ret = g_variant_new_variant (value);
1810 g_variant_unref (value);
1816 s = g_variant_type_dup_string (type);
1817 g_set_error (&local_error,
1819 G_IO_ERROR_INVALID_ARGUMENT,
1820 _("Error deserializing GVariant with type string '%s' from the D-Bus wire format"),
1828 g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
1830 #ifdef DEBUG_SERIALIZER
1836 if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
1838 s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret));
1842 s = g_variant_print (ret, FALSE);
1844 g_print (": %s\n", s);
1849 is_leaf = is_leaf; /* To avoid -Wunused-but-set-variable */
1850 #endif /* DEBUG_SERIALIZER */
1852 /* sink the reference */
1855 g_assert (g_variant_is_floating (ret));
1856 g_variant_ref_sink (ret);
1861 #ifdef DEBUG_SERIALIZER
1863 "%*sFAILURE: %s (%s, %d)\n",
1865 local_error->message,
1866 g_quark_to_string (local_error->domain),
1868 #endif /* DEBUG_SERIALIZER */
1869 g_propagate_error (error, local_error);
1873 /* ---------------------------------------------------------------------------------------------------- */
1875 /* message_header must be at least 16 bytes */
1878 * g_dbus_message_bytes_needed:
1879 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1880 * @blob_len: The length of @blob (must be at least 16).
1881 * @error: Return location for error or %NULL.
1883 * Utility function to calculate how many bytes are needed to
1884 * completely deserialize the D-Bus message stored at @blob.
1886 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1887 * @blob contains invalid data or not enough data is available to
1888 * determine the size).
1893 g_dbus_message_bytes_needed (guchar *blob,
1901 g_return_val_if_fail (blob != NULL, -1);
1902 g_return_val_if_fail (error == NULL || *error == NULL, -1);
1903 g_return_val_if_fail (blob_len >= 16, -1);
1907 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1908 ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]);
1909 /* round up so it's a multiple of 8 */
1910 ret = 8 * ((ret + 7)/8);
1911 /* finally add the body size */
1912 ret += GUINT32_FROM_LE (((guint32 *) blob)[1]);
1914 else if (blob[0] == 'B')
1916 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1917 ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]);
1918 /* round up so it's a multiple of 8 */
1919 ret = 8 * ((ret + 7)/8);
1920 /* finally add the body size */
1921 ret += GUINT32_FROM_BE (((guint32 *) blob)[1]);
1927 G_IO_ERROR_INVALID_ARGUMENT,
1928 "Unable to determine message blob length - given blob is malformed");
1935 G_IO_ERROR_INVALID_ARGUMENT,
1936 "Blob indicates that message exceeds maximum message length (128MiB)");
1943 /* ---------------------------------------------------------------------------------------------------- */
1946 * g_dbus_message_new_from_blob:
1947 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1948 * @blob_len: The length of @blob.
1949 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1950 * @error: Return location for error or %NULL.
1952 * Creates a new #GDBusMessage from the data stored at @blob. The byte
1953 * order that the message was in can be retrieved using
1954 * g_dbus_message_get_byte_order().
1956 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
1962 g_dbus_message_new_from_blob (guchar *blob,
1964 GDBusCapabilityFlags capabilities,
1969 GDBusMessage *message;
1971 guchar major_protocol_version;
1972 guint32 message_body_len;
1976 GVariant *signature;
1978 /* TODO: check against @capabilities */
1982 g_return_val_if_fail (blob != NULL, NULL);
1983 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1984 g_return_val_if_fail (blob_len >= 12, NULL);
1986 message = g_dbus_message_new ();
1988 memset (&mbuf, 0, sizeof (mbuf));
1989 mbuf.data = (gchar *)blob;
1990 mbuf.len = mbuf.valid_len = blob_len;
1992 endianness = g_memory_buffer_read_byte (&mbuf);
1996 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
1997 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
2000 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2001 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
2006 G_IO_ERROR_INVALID_ARGUMENT,
2007 _("Invalid endianness value. Expected 0x6c ('l') or 0x42 ('B') but found value 0x%02x"),
2012 message->type = g_memory_buffer_read_byte (&mbuf);
2013 message->flags = g_memory_buffer_read_byte (&mbuf);
2014 major_protocol_version = g_memory_buffer_read_byte (&mbuf);
2015 if (major_protocol_version != 1)
2019 G_IO_ERROR_INVALID_ARGUMENT,
2020 _("Invalid major protocol version. Expected 1 but found %d"),
2021 major_protocol_version);
2024 message_body_len = g_memory_buffer_read_uint32 (&mbuf);
2025 message->serial = g_memory_buffer_read_uint32 (&mbuf);
2027 #ifdef DEBUG_SERIALIZER
2028 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2031 s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2);
2032 g_print ("%s\n", s);
2035 #endif /* DEBUG_SERIALIZER */
2037 #ifdef DEBUG_SERIALIZER
2038 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2039 #endif /* DEBUG_SERIALIZER */
2040 headers = parse_value_from_blob (&mbuf,
2041 G_VARIANT_TYPE ("a{yv}"),
2045 if (headers == NULL)
2047 g_variant_iter_init (&iter, headers);
2048 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2050 guchar header_field;
2052 g_variant_get (item,
2056 g_dbus_message_set_header (message, header_field, value);
2057 g_variant_unref (value);
2058 g_variant_unref (item);
2060 g_variant_unref (headers);
2062 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2063 if (signature != NULL)
2065 const gchar *signature_str;
2066 gsize signature_str_len;
2068 signature_str = g_variant_get_string (signature, &signature_str_len);
2070 /* signature but no body */
2071 if (message_body_len == 0 && signature_str_len > 0)
2075 G_IO_ERROR_INVALID_ARGUMENT,
2076 _("Signature header with signature '%s' found but message body is empty"),
2080 else if (signature_str_len > 0)
2082 GVariantType *variant_type;
2083 gchar *tupled_signature_str;
2085 if (!g_variant_is_signature (signature_str))
2089 G_IO_ERROR_INVALID_ARGUMENT,
2090 _("Parsed value '%s' is not a valid D-Bus signature (for body)"),
2094 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2095 variant_type = g_variant_type_new (tupled_signature_str);
2096 g_free (tupled_signature_str);
2097 #ifdef DEBUG_SERIALIZER
2098 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2099 #endif /* DEBUG_SERIALIZER */
2100 message->body = parse_value_from_blob (&mbuf,
2105 g_variant_type_free (variant_type);
2106 if (message->body == NULL)
2112 /* no signature, this is only OK if the body is empty */
2113 if (message_body_len != 0)
2115 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
2118 G_IO_ERROR_INVALID_ARGUMENT,
2119 g_dngettext (GETTEXT_PACKAGE,
2120 "No signature header in message but the message body is %u byte",
2121 "No signature header in message but the message body is %u bytes",
2128 if (!validate_headers (message, error))
2130 g_prefix_error (error, _("Cannot deserialize message: "));
2143 if (message != NULL)
2144 g_object_unref (message);
2149 /* ---------------------------------------------------------------------------------------------------- */
2152 ensure_output_padding (GMemoryBuffer *mbuf,
2156 gsize wanted_offset;
2157 gsize padding_needed;
2161 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
2162 padding_needed = wanted_offset - offset;
2164 for (n = 0; n < padding_needed; n++)
2165 g_memory_buffer_put_byte (mbuf, '\0');
2167 return padding_needed;
2170 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
2172 append_value_to_blob (GVariant *value,
2173 const GVariantType *type,
2174 GMemoryBuffer *mbuf,
2175 gsize *out_padding_added,
2178 gsize padding_added;
2179 const gchar *type_string;
2181 type_string = g_variant_type_peek_string (type);
2185 switch (type_string[0])
2187 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
2188 padding_added = ensure_output_padding (mbuf, 4);
2191 gboolean v = g_variant_get_boolean (value);
2192 g_memory_buffer_put_uint32 (mbuf, v);
2196 case 'y': /* G_VARIANT_TYPE_BYTE */
2199 guint8 v = g_variant_get_byte (value);
2200 g_memory_buffer_put_byte (mbuf, v);
2204 case 'n': /* G_VARIANT_TYPE_INT16 */
2205 padding_added = ensure_output_padding (mbuf, 2);
2208 gint16 v = g_variant_get_int16 (value);
2209 g_memory_buffer_put_int16 (mbuf, v);
2213 case 'q': /* G_VARIANT_TYPE_UINT16 */
2214 padding_added = ensure_output_padding (mbuf, 2);
2217 guint16 v = g_variant_get_uint16 (value);
2218 g_memory_buffer_put_uint16 (mbuf, v);
2222 case 'i': /* G_VARIANT_TYPE_INT32 */
2223 padding_added = ensure_output_padding (mbuf, 4);
2226 gint32 v = g_variant_get_int32 (value);
2227 g_memory_buffer_put_int32 (mbuf, v);
2231 case 'u': /* G_VARIANT_TYPE_UINT32 */
2232 padding_added = ensure_output_padding (mbuf, 4);
2235 guint32 v = g_variant_get_uint32 (value);
2236 g_memory_buffer_put_uint32 (mbuf, v);
2240 case 'x': /* G_VARIANT_TYPE_INT64 */
2241 padding_added = ensure_output_padding (mbuf, 8);
2244 gint64 v = g_variant_get_int64 (value);
2245 g_memory_buffer_put_int64 (mbuf, v);
2249 case 't': /* G_VARIANT_TYPE_UINT64 */
2250 padding_added = ensure_output_padding (mbuf, 8);
2253 guint64 v = g_variant_get_uint64 (value);
2254 g_memory_buffer_put_uint64 (mbuf, v);
2258 case 'd': /* G_VARIANT_TYPE_DOUBLE */
2259 padding_added = ensure_output_padding (mbuf, 8);
2266 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
2267 u.v_double = g_variant_get_double (value);
2268 g_memory_buffer_put_uint64 (mbuf, u.v_uint64);
2272 case 's': /* G_VARIANT_TYPE_STRING */
2273 padding_added = ensure_output_padding (mbuf, 4);
2279 v = g_variant_get_string (value, &len);
2280 g_assert (g_utf8_validate (v, -1, &end) && (end == v + len));
2281 g_memory_buffer_put_uint32 (mbuf, len);
2282 g_memory_buffer_put_string (mbuf, v);
2283 g_memory_buffer_put_byte (mbuf, '\0');
2287 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
2288 padding_added = ensure_output_padding (mbuf, 4);
2292 const gchar *v = g_variant_get_string (value, &len);
2293 g_assert (g_variant_is_object_path (v));
2294 g_memory_buffer_put_uint32 (mbuf, len);
2295 g_memory_buffer_put_string (mbuf, v);
2296 g_memory_buffer_put_byte (mbuf, '\0');
2300 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2304 const gchar *v = g_variant_get_string (value, &len);
2305 g_assert (g_variant_is_signature (v));
2306 g_memory_buffer_put_byte (mbuf, len);
2307 g_memory_buffer_put_string (mbuf, v);
2308 g_memory_buffer_put_byte (mbuf, '\0');
2312 case 'h': /* G_VARIANT_TYPE_HANDLE */
2313 padding_added = ensure_output_padding (mbuf, 4);
2316 gint32 v = g_variant_get_handle (value);
2317 g_memory_buffer_put_int32 (mbuf, v);
2321 case 'a': /* G_VARIANT_TYPE_ARRAY */
2325 goffset array_len_offset;
2326 goffset array_payload_begin_offset;
2330 padding_added = ensure_output_padding (mbuf, 4);
2333 /* array length - will be filled in later */
2334 array_len_offset = mbuf->valid_len;
2335 g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE);
2337 /* From the D-Bus spec:
2339 * "A UINT32 giving the length of the array data in bytes,
2340 * followed by alignment padding to the alignment boundary of
2341 * the array element type, followed by each array element. The
2342 * array length is from the end of the alignment padding to
2343 * the end of the last element, i.e. it does not include the
2344 * padding after the length, or any padding after the last
2347 * Thus, we need to count how much padding the first element
2348 * contributes and subtract that from the array length.
2350 array_payload_begin_offset = mbuf->valid_len;
2352 if (g_variant_n_children (value) == 0)
2354 gsize padding_added_for_item;
2355 if (!append_value_to_blob (NULL,
2356 g_variant_type_element (type),
2358 &padding_added_for_item,
2361 array_payload_begin_offset += padding_added_for_item;
2367 g_variant_iter_init (&iter, value);
2368 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2370 gsize padding_added_for_item;
2371 if (!append_value_to_blob (item,
2372 g_variant_get_type (item),
2374 &padding_added_for_item,
2377 g_variant_unref (item);
2380 g_variant_unref (item);
2383 array_payload_begin_offset += padding_added_for_item;
2389 cur_offset = mbuf->valid_len;
2390 array_len = cur_offset - array_payload_begin_offset;
2391 mbuf->pos = array_len_offset;
2393 g_memory_buffer_put_uint32 (mbuf, array_len);
2394 mbuf->pos = cur_offset;
2400 if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
2402 padding_added = ensure_output_padding (mbuf, 8);
2407 g_variant_iter_init (&iter, value);
2408 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2410 if (!append_value_to_blob (item,
2411 g_variant_get_type (item),
2416 g_variant_unref (item);
2419 g_variant_unref (item);
2423 else if (g_variant_type_is_variant (type))
2428 const gchar *signature;
2429 child = g_variant_get_child_value (value, 0);
2430 signature = g_variant_get_type_string (child);
2431 g_memory_buffer_put_byte (mbuf, strlen (signature));
2432 g_memory_buffer_put_string (mbuf, signature);
2433 g_memory_buffer_put_byte (mbuf, '\0');
2434 if (!append_value_to_blob (child,
2435 g_variant_get_type (child),
2440 g_variant_unref (child);
2443 g_variant_unref (child);
2450 G_IO_ERROR_INVALID_ARGUMENT,
2451 _("Error serializing GVariant with type string '%s' to the D-Bus wire format"),
2452 g_variant_get_type_string (value));
2458 if (out_padding_added != NULL)
2459 *out_padding_added = padding_added;
2468 append_body_to_blob (GVariant *value,
2469 GMemoryBuffer *mbuf,
2475 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
2479 G_IO_ERROR_INVALID_ARGUMENT,
2480 "Expected a tuple for the body of the GDBusMessage.");
2484 g_variant_iter_init (&iter, value);
2485 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2487 if (!append_value_to_blob (item,
2488 g_variant_get_type (item),
2493 g_variant_unref (item);
2496 g_variant_unref (item);
2504 /* ---------------------------------------------------------------------------------------------------- */
2507 * g_dbus_message_to_blob:
2508 * @message: A #GDBusMessage.
2509 * @out_size: Return location for size of generated blob.
2510 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2511 * @error: Return location for error.
2513 * Serializes @message to a blob. The byte order returned by
2514 * g_dbus_message_get_byte_order() will be used.
2516 * Returns: (array length=out_size) (transfer full): A pointer to a
2517 * valid binary D-Bus message of @out_size bytes generated by @message
2518 * or %NULL if @error is set. Free with g_free().
2523 g_dbus_message_to_blob (GDBusMessage *message,
2525 GDBusCapabilityFlags capabilities,
2531 goffset body_len_offset;
2532 goffset body_start_offset;
2534 GVariant *header_fields;
2535 GVariantBuilder builder;
2536 GHashTableIter hash_iter;
2538 GVariant *header_value;
2539 GVariant *signature;
2540 const gchar *signature_str;
2541 gint num_fds_in_message;
2542 gint num_fds_according_to_header;
2544 /* TODO: check against @capabilities */
2548 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2549 g_return_val_if_fail (out_size != NULL, NULL);
2550 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2552 memset (&mbuf, 0, sizeof (mbuf));
2553 mbuf.len = MIN_ARRAY_SIZE;
2554 mbuf.data = g_malloc (mbuf.len);
2556 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
2557 switch (message->byte_order)
2559 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
2560 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2562 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
2563 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
2568 g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order);
2569 g_memory_buffer_put_byte (&mbuf, message->type);
2570 g_memory_buffer_put_byte (&mbuf, message->flags);
2571 g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */
2572 body_len_offset = mbuf.valid_len;
2573 /* body length - will be filled in later */
2574 g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE);
2575 g_memory_buffer_put_uint32 (&mbuf, message->serial);
2577 num_fds_in_message = 0;
2579 if (message->fd_list != NULL)
2580 num_fds_in_message = g_unix_fd_list_get_length (message->fd_list);
2582 num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
2583 if (num_fds_in_message != num_fds_according_to_header)
2587 G_IO_ERROR_INVALID_ARGUMENT,
2588 _("Message has %d file descriptors but the header field indicates %d file descriptors"),
2590 num_fds_according_to_header);
2594 if (!validate_headers (message, error))
2596 g_prefix_error (error, _("Cannot serialize message: "));
2600 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
2601 g_hash_table_iter_init (&hash_iter, message->headers);
2602 while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
2604 g_variant_builder_add (&builder,
2606 (guchar) GPOINTER_TO_UINT (key),
2609 header_fields = g_variant_builder_end (&builder);
2611 if (!append_value_to_blob (header_fields,
2612 g_variant_get_type (header_fields),
2617 g_variant_unref (header_fields);
2620 g_variant_unref (header_fields);
2622 /* header size must be a multiple of 8 */
2623 ensure_output_padding (&mbuf, 8);
2625 body_start_offset = mbuf.valid_len;
2627 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2628 signature_str = NULL;
2629 if (signature != NULL)
2630 signature_str = g_variant_get_string (signature, NULL);
2631 if (message->body != NULL)
2633 gchar *tupled_signature_str;
2634 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2635 if (signature == NULL)
2639 G_IO_ERROR_INVALID_ARGUMENT,
2640 _("Message body has signature '%s' but there is no signature header"),
2642 g_free (tupled_signature_str);
2645 else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0)
2649 G_IO_ERROR_INVALID_ARGUMENT,
2650 _("Message body has type signature '%s' but signature in the header field is '%s'"),
2651 tupled_signature_str, g_variant_get_type_string (message->body));
2652 g_free (tupled_signature_str);
2655 g_free (tupled_signature_str);
2656 if (!append_body_to_blob (message->body, &mbuf, error))
2661 if (signature != NULL && strlen (signature_str) > 0)
2665 G_IO_ERROR_INVALID_ARGUMENT,
2666 _("Message body is empty but signature in the header field is '(%s)'"),
2672 /* OK, we're done writing the message - set the body length */
2673 size = mbuf.valid_len;
2674 body_size = size - body_start_offset;
2676 mbuf.pos = body_len_offset;
2678 g_memory_buffer_put_uint32 (&mbuf, body_size);
2681 ret = (guchar *)mbuf.data;
2687 /* ---------------------------------------------------------------------------------------------------- */
2690 get_uint32_header (GDBusMessage *message,
2691 GDBusMessageHeaderField header_field)
2697 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2698 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
2699 ret = g_variant_get_uint32 (value);
2704 static const gchar *
2705 get_string_header (GDBusMessage *message,
2706 GDBusMessageHeaderField header_field)
2712 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2713 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
2714 ret = g_variant_get_string (value, NULL);
2719 static const gchar *
2720 get_object_path_header (GDBusMessage *message,
2721 GDBusMessageHeaderField header_field)
2727 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2728 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
2729 ret = g_variant_get_string (value, NULL);
2734 static const gchar *
2735 get_signature_header (GDBusMessage *message,
2736 GDBusMessageHeaderField header_field)
2742 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2743 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
2744 ret = g_variant_get_string (value, NULL);
2749 /* ---------------------------------------------------------------------------------------------------- */
2752 set_uint32_header (GDBusMessage *message,
2753 GDBusMessageHeaderField header_field,
2756 g_dbus_message_set_header (message,
2758 g_variant_new_uint32 (value));
2762 set_string_header (GDBusMessage *message,
2763 GDBusMessageHeaderField header_field,
2766 g_dbus_message_set_header (message,
2768 value == NULL ? NULL : g_variant_new_string (value));
2772 set_object_path_header (GDBusMessage *message,
2773 GDBusMessageHeaderField header_field,
2776 g_dbus_message_set_header (message,
2778 value == NULL ? NULL : g_variant_new_object_path (value));
2782 set_signature_header (GDBusMessage *message,
2783 GDBusMessageHeaderField header_field,
2786 g_dbus_message_set_header (message,
2788 value == NULL ? NULL : g_variant_new_signature (value));
2791 /* ---------------------------------------------------------------------------------------------------- */
2794 * g_dbus_message_get_reply_serial:
2795 * @message: A #GDBusMessage.
2797 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2799 * Returns: The value.
2804 g_dbus_message_get_reply_serial (GDBusMessage *message)
2806 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2807 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL);
2811 * g_dbus_message_set_reply_serial:
2812 * @message: A #GDBusMessage.
2813 * @value: The value to set.
2815 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2820 g_dbus_message_set_reply_serial (GDBusMessage *message,
2823 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2824 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value);
2827 /* ---------------------------------------------------------------------------------------------------- */
2830 * g_dbus_message_get_interface:
2831 * @message: A #GDBusMessage.
2833 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2835 * Returns: The value.
2840 g_dbus_message_get_interface (GDBusMessage *message)
2842 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2843 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE);
2847 * g_dbus_message_set_interface:
2848 * @message: A #GDBusMessage.
2849 * @value: The value to set.
2851 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2856 g_dbus_message_set_interface (GDBusMessage *message,
2859 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2860 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2861 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value);
2864 /* ---------------------------------------------------------------------------------------------------- */
2867 * g_dbus_message_get_member:
2868 * @message: A #GDBusMessage.
2870 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2872 * Returns: The value.
2877 g_dbus_message_get_member (GDBusMessage *message)
2879 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2880 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER);
2884 * g_dbus_message_set_member:
2885 * @message: A #GDBusMessage.
2886 * @value: The value to set.
2888 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2893 g_dbus_message_set_member (GDBusMessage *message,
2896 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2897 g_return_if_fail (value == NULL || g_dbus_is_member_name (value));
2898 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value);
2901 /* ---------------------------------------------------------------------------------------------------- */
2904 * g_dbus_message_get_path:
2905 * @message: A #GDBusMessage.
2907 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2909 * Returns: The value.
2914 g_dbus_message_get_path (GDBusMessage *message)
2916 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2917 return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH);
2921 * g_dbus_message_set_path:
2922 * @message: A #GDBusMessage.
2923 * @value: The value to set.
2925 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2930 g_dbus_message_set_path (GDBusMessage *message,
2933 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2934 g_return_if_fail (value == NULL || g_variant_is_object_path (value));
2935 set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value);
2938 /* ---------------------------------------------------------------------------------------------------- */
2941 * g_dbus_message_get_sender:
2942 * @message: A #GDBusMessage.
2944 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2946 * Returns: The value.
2951 g_dbus_message_get_sender (GDBusMessage *message)
2953 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2954 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER);
2958 * g_dbus_message_set_sender:
2959 * @message: A #GDBusMessage.
2960 * @value: The value to set.
2962 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
2967 g_dbus_message_set_sender (GDBusMessage *message,
2970 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2971 g_return_if_fail (value == NULL || g_dbus_is_name (value));
2972 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value);
2975 /* ---------------------------------------------------------------------------------------------------- */
2978 * g_dbus_message_get_destination:
2979 * @message: A #GDBusMessage.
2981 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
2983 * Returns: The value.
2988 g_dbus_message_get_destination (GDBusMessage *message)
2990 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2991 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION);
2995 * g_dbus_message_set_destination:
2996 * @message: A #GDBusMessage.
2997 * @value: The value to set.
2999 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3004 g_dbus_message_set_destination (GDBusMessage *message,
3007 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3008 g_return_if_fail (value == NULL || g_dbus_is_name (value));
3009 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value);
3012 /* ---------------------------------------------------------------------------------------------------- */
3015 * g_dbus_message_get_error_name:
3016 * @message: A #GDBusMessage.
3018 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3020 * Returns: The value.
3025 g_dbus_message_get_error_name (GDBusMessage *message)
3027 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3028 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME);
3032 * g_dbus_message_set_error_name:
3033 * @message: A #GDBusMessage.
3034 * @value: The value to set.
3036 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3041 g_dbus_message_set_error_name (GDBusMessage *message,
3044 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3045 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
3046 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value);
3049 /* ---------------------------------------------------------------------------------------------------- */
3052 * g_dbus_message_get_signature:
3053 * @message: A #GDBusMessage.
3055 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3057 * Returns: The value.
3062 g_dbus_message_get_signature (GDBusMessage *message)
3065 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3066 ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
3073 * g_dbus_message_set_signature:
3074 * @message: A #GDBusMessage.
3075 * @value: The value to set.
3077 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3082 g_dbus_message_set_signature (GDBusMessage *message,
3085 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3086 g_return_if_fail (value == NULL || g_variant_is_signature (value));
3087 set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value);
3090 /* ---------------------------------------------------------------------------------------------------- */
3093 * g_dbus_message_get_arg0:
3094 * @message: A #GDBusMessage.
3096 * Convenience to get the first item in the body of @message.
3098 * Returns: The string item or %NULL if the first item in the body of
3099 * @message is not a string.
3104 g_dbus_message_get_arg0 (GDBusMessage *message)
3108 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3112 if (message->body != NULL && g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE))
3115 item = g_variant_get_child_value (message->body, 0);
3116 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
3117 ret = g_variant_get_string (item, NULL);
3118 g_variant_unref (item);
3124 /* ---------------------------------------------------------------------------------------------------- */
3127 * g_dbus_message_get_num_unix_fds:
3128 * @message: A #GDBusMessage.
3130 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3132 * Returns: The value.
3137 g_dbus_message_get_num_unix_fds (GDBusMessage *message)
3139 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
3140 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS);
3144 * g_dbus_message_set_num_unix_fds:
3145 * @message: A #GDBusMessage.
3146 * @value: The value to set.
3148 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3153 g_dbus_message_set_num_unix_fds (GDBusMessage *message,
3156 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3157 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value);
3160 /* ---------------------------------------------------------------------------------------------------- */
3163 * g_dbus_message_to_gerror:
3164 * @message: A #GDBusMessage.
3165 * @error: The #GError to set.
3167 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
3168 * nothing and returns %FALSE.
3170 * Otherwise this method encodes the error in @message as a #GError
3171 * using g_dbus_error_set_dbus_error() using the information in the
3172 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
3173 * well as the first string item in @message's body.
3175 * Returns: %TRUE if @error was set, %FALSE otherwise.
3180 g_dbus_message_to_gerror (GDBusMessage *message,
3184 const gchar *error_name;
3186 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3189 if (message->type != G_DBUS_MESSAGE_TYPE_ERROR)
3192 error_name = g_dbus_message_get_error_name (message);
3193 if (error_name != NULL)
3197 body = g_dbus_message_get_body (message);
3199 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)")))
3201 const gchar *error_message;
3202 g_variant_get (body, "(&s)", &error_message);
3203 g_dbus_error_set_dbus_error (error,
3210 /* these two situations are valid, yet pretty rare */
3213 g_dbus_error_set_dbus_error (error,
3216 _("Error return with body of type '%s'"),
3217 g_variant_get_type_string (body));
3221 g_dbus_error_set_dbus_error (error,
3224 _("Error return with empty body"));
3230 /* TOOD: this shouldn't happen - should check this at message serialization
3231 * time and disconnect the peer.
3236 "Error return without error-name header!");
3245 /* ---------------------------------------------------------------------------------------------------- */
3248 flags_to_string (GType flags_type, guint value)
3254 klass = g_type_class_ref (flags_type);
3255 s = g_string_new (NULL);
3256 for (n = 0; n < 32; n++)
3258 if ((value & (1<<n)) != 0)
3260 GFlagsValue *flags_value;
3261 flags_value = g_flags_get_first_value (klass, (1<<n));
3263 g_string_append_c (s, ',');
3264 if (flags_value != NULL)
3265 g_string_append (s, flags_value->value_nick);
3267 g_string_append_printf (s, "unknown (bit %d)", n);
3271 g_string_append (s, "none");
3272 g_type_class_unref (klass);
3273 return g_string_free (s, FALSE);
3277 _sort_keys_func (gconstpointer a,
3283 ia = GPOINTER_TO_INT (a);
3284 ib = GPOINTER_TO_INT (b);
3290 * g_dbus_message_print:
3291 * @message: A #GDBusMessage.
3292 * @indent: Indentation level.
3294 * Produces a human-readable multi-line description of @message.
3296 * The contents of the description has no ABI guarantees, the contents
3297 * and formatting is subject to change at any time. Typical output
3298 * looks something like this:
3305 * path -> objectpath '/org/gtk/GDBus/TestObject'
3306 * interface -> 'org.gtk.GDBus.TestInterface'
3307 * member -> 'GimmeStdout'
3308 * destination -> ':1.146'
3310 * UNIX File Descriptors:
3315 * Type: method-return
3316 * Flags: no-reply-expected
3320 * reply-serial -> uint32 4
3321 * destination -> ':1.159'
3322 * sender -> ':1.146'
3323 * num-unix-fds -> uint32 1
3325 * UNIX File Descriptors:
3326 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3329 * Returns: A string that should be freed with g_free().
3334 g_dbus_message_print (GDBusMessage *message,
3342 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3344 str = g_string_new (NULL);
3346 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type);
3347 g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
3349 s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags);
3350 g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
3352 g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version);
3353 g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial);
3355 g_string_append_printf (str, "%*sHeaders:\n", indent, "");
3356 keys = g_hash_table_get_keys (message->headers);
3357 keys = g_list_sort (keys, _sort_keys_func);
3360 for (l = keys; l != NULL; l = l->next)
3362 gint key = GPOINTER_TO_INT (l->data);
3366 value = g_hash_table_lookup (message->headers, l->data);
3367 g_assert (value != NULL);
3369 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
3370 value_str = g_variant_print (value, TRUE);
3371 g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
3378 g_string_append_printf (str, "%*s (none)\n", indent, "");
3380 g_string_append_printf (str, "%*sBody: ", indent, "");
3381 if (message->body != NULL)
3383 g_variant_print_string (message->body,
3389 g_string_append (str, "()");
3391 g_string_append (str, "\n");
3393 g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
3394 if (message->fd_list != NULL)
3400 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3403 for (n = 0; n < num_fds; n++)
3406 struct stat statbuf;
3407 fs = g_string_new (NULL);
3408 if (fstat (fds[n], &statbuf) == 0)
3410 g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "",
3411 major (statbuf.st_dev), minor (statbuf.st_dev));
3412 g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "",
3414 g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3415 (guint64) statbuf.st_ino);
3416 g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "",
3417 (guint) statbuf.st_uid);
3418 g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "",
3419 (guint) statbuf.st_gid);
3420 g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
3421 major (statbuf.st_rdev), minor (statbuf.st_rdev));
3422 g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3423 (guint64) statbuf.st_size);
3424 g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3425 (guint64) statbuf.st_atime);
3426 g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3427 (guint64) statbuf.st_mtime);
3428 g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3429 (guint64) statbuf.st_ctime);
3433 g_string_append_printf (fs, "(fstat failed: %s)", strerror (errno));
3435 g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str);
3436 g_string_free (fs, TRUE);
3441 g_string_append_printf (str, "%*s (empty)\n", indent, "");
3446 g_string_append_printf (str, "%*s (none)\n", indent, "");
3450 return g_string_free (str, FALSE);
3454 * g_dbus_message_get_locked:
3455 * @message: A #GDBusMessage.
3457 * Checks whether @message is locked. To monitor changes to this
3458 * value, conncet to the #GObject::notify signal to listen for changes
3459 * on the #GDBusMessage:locked property.
3461 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3466 g_dbus_message_get_locked (GDBusMessage *message)
3468 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3469 return message->locked;
3473 * g_dbus_message_lock:
3474 * @message: A #GDBusMessage.
3476 * If @message is locked, does nothing. Otherwise locks the message.
3481 g_dbus_message_lock (GDBusMessage *message)
3483 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3485 if (message->locked)
3488 message->locked = TRUE;
3489 g_object_notify (G_OBJECT (message), "locked");
3496 * g_dbus_message_copy:
3497 * @message: A #GDBusMessage.
3498 * @error: Return location for error or %NULL.
3500 * Copies @message. The copy is a deep copy and the returned
3501 * #GDBusMessage is completely identical except that it is guaranteed
3504 * This operation can fail if e.g. @message contains file descriptors
3505 * and the per-process or system-wide open files limit is reached.
3507 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3508 * Free with g_object_unref().
3513 g_dbus_message_copy (GDBusMessage *message,
3517 GHashTableIter iter;
3518 gpointer header_key;
3519 GVariant *header_value;
3521 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3522 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3524 ret = g_dbus_message_new ();
3525 ret->type = message->type;
3526 ret->flags = message->flags;
3527 ret->byte_order = message->byte_order;
3528 ret->major_protocol_version = message->major_protocol_version;
3529 ret->serial = message->serial;
3532 if (message->fd_list != NULL)
3538 ret->fd_list = g_unix_fd_list_new ();
3539 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3540 for (n = 0; n < num_fds; n++)
3542 if (g_unix_fd_list_append (ret->fd_list,
3546 g_object_unref (ret);
3554 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3555 * to just ref (as opposed to deep-copying) the GVariant instances
3557 ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL;
3558 g_hash_table_iter_init (&iter, message->headers);
3559 while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value))
3560 g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value));