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.1 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>
36 #define MAJOR_MINOR_NOT_FOUND 1
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_is_byteswapped (GMemoryBuffer
*mbuf
)
72 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
73 return mbuf
->byte_order
== G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
75 return mbuf
->byte_order
== G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
80 g_memory_buffer_read_byte (GMemoryBuffer
*mbuf
)
82 if (mbuf
->pos
>= mbuf
->valid_len
)
84 return mbuf
->data
[mbuf
->pos
++];
88 g_memory_buffer_read_int16 (GMemoryBuffer
*mbuf
)
92 if (mbuf
->pos
> mbuf
->valid_len
- 2)
94 mbuf
->pos
= mbuf
->valid_len
;
98 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 2);
101 if (g_memory_buffer_is_byteswapped (mbuf
))
102 v
= GUINT16_SWAP_LE_BE (v
);
108 g_memory_buffer_read_uint16 (GMemoryBuffer
*mbuf
)
112 if (mbuf
->pos
> mbuf
->valid_len
- 2)
114 mbuf
->pos
= mbuf
->valid_len
;
118 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 2);
121 if (g_memory_buffer_is_byteswapped (mbuf
))
122 v
= GUINT16_SWAP_LE_BE (v
);
128 g_memory_buffer_read_int32 (GMemoryBuffer
*mbuf
)
132 if (mbuf
->pos
> mbuf
->valid_len
- 4)
134 mbuf
->pos
= mbuf
->valid_len
;
138 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 4);
141 if (g_memory_buffer_is_byteswapped (mbuf
))
142 v
= GUINT32_SWAP_LE_BE (v
);
148 g_memory_buffer_read_uint32 (GMemoryBuffer
*mbuf
)
152 if (mbuf
->pos
> mbuf
->valid_len
- 4)
154 mbuf
->pos
= mbuf
->valid_len
;
158 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 4);
161 if (g_memory_buffer_is_byteswapped (mbuf
))
162 v
= GUINT32_SWAP_LE_BE (v
);
168 g_memory_buffer_read_int64 (GMemoryBuffer
*mbuf
)
172 if (mbuf
->pos
> mbuf
->valid_len
- 8)
174 mbuf
->pos
= mbuf
->valid_len
;
178 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 8);
181 if (g_memory_buffer_is_byteswapped (mbuf
))
182 v
= GUINT64_SWAP_LE_BE (v
);
188 g_memory_buffer_read_uint64 (GMemoryBuffer
*mbuf
)
192 if (mbuf
->pos
> mbuf
->valid_len
- 8)
194 mbuf
->pos
= mbuf
->valid_len
;
198 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 8);
201 if (g_memory_buffer_is_byteswapped (mbuf
))
202 v
= GUINT64_SWAP_LE_BE (v
);
207 #define MIN_ARRAY_SIZE 128
210 g_nearest_pow (gsize num
)
214 while (n
< num
&& n
> 0)
221 array_resize (GMemoryBuffer
*mbuf
,
227 if (mbuf
->len
== size
)
231 data
= g_realloc (mbuf
->data
, size
);
234 memset ((guint8
*)data
+ len
, 0, size
- len
);
239 if (mbuf
->len
< mbuf
->valid_len
)
240 mbuf
->valid_len
= mbuf
->len
;
244 g_memory_buffer_write (GMemoryBuffer
*mbuf
,
254 /* Check for address space overflow, but only if the buffer is resizable.
255 Otherwise we just do a short write and don't worry. */
256 if (mbuf
->pos
+ count
< mbuf
->pos
)
259 if (mbuf
->pos
+ count
> mbuf
->len
)
261 /* At least enought to fit the write, rounded up
262 for greater than linear growth.
263 TODO: This wastes a lot of memory at large buffer sizes.
264 Figure out a more rational allocation strategy. */
265 new_size
= g_nearest_pow (mbuf
->pos
+ count
);
266 /* Check for overflow again. We have checked if
267 pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
272 new_size
= MAX (new_size
, MIN_ARRAY_SIZE
);
273 array_resize (mbuf
, new_size
);
276 dest
= (guint8
*)mbuf
->data
+ mbuf
->pos
;
277 memcpy (dest
, buffer
, count
);
280 if (mbuf
->pos
> mbuf
->valid_len
)
281 mbuf
->valid_len
= mbuf
->pos
;
287 g_memory_buffer_put_byte (GMemoryBuffer
*mbuf
,
290 return g_memory_buffer_write (mbuf
, &data
, 1);
294 g_memory_buffer_put_int16 (GMemoryBuffer
*mbuf
,
297 switch (mbuf
->byte_order
)
299 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
300 data
= GINT16_TO_BE (data
);
302 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
303 data
= GINT16_TO_LE (data
);
305 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
310 return g_memory_buffer_write (mbuf
, &data
, 2);
314 g_memory_buffer_put_uint16 (GMemoryBuffer
*mbuf
,
317 switch (mbuf
->byte_order
)
319 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
320 data
= GUINT16_TO_BE (data
);
322 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
323 data
= GUINT16_TO_LE (data
);
325 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
330 return g_memory_buffer_write (mbuf
, &data
, 2);
334 g_memory_buffer_put_int32 (GMemoryBuffer
*mbuf
,
337 switch (mbuf
->byte_order
)
339 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
340 data
= GINT32_TO_BE (data
);
342 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
343 data
= GINT32_TO_LE (data
);
345 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
350 return g_memory_buffer_write (mbuf
, &data
, 4);
354 g_memory_buffer_put_uint32 (GMemoryBuffer
*mbuf
,
357 switch (mbuf
->byte_order
)
359 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
360 data
= GUINT32_TO_BE (data
);
362 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
363 data
= GUINT32_TO_LE (data
);
365 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
370 return g_memory_buffer_write (mbuf
, &data
, 4);
374 g_memory_buffer_put_int64 (GMemoryBuffer
*mbuf
,
377 switch (mbuf
->byte_order
)
379 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
380 data
= GINT64_TO_BE (data
);
382 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
383 data
= GINT64_TO_LE (data
);
385 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
390 return g_memory_buffer_write (mbuf
, &data
, 8);
394 g_memory_buffer_put_uint64 (GMemoryBuffer
*mbuf
,
397 switch (mbuf
->byte_order
)
399 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
400 data
= GUINT64_TO_BE (data
);
402 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
403 data
= GUINT64_TO_LE (data
);
405 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
410 return g_memory_buffer_write (mbuf
, &data
, 8);
414 g_memory_buffer_put_string (GMemoryBuffer
*mbuf
,
417 g_return_val_if_fail (str
!= NULL
, FALSE
);
419 return g_memory_buffer_write (mbuf
, str
, strlen (str
));
424 * SECTION:gdbusmessage
425 * @short_description: D-Bus Message
426 * @include: gio/gio.h
428 * A type for representing D-Bus messages that can be sent or received
429 * on a #GDBusConnection.
432 typedef struct _GDBusMessageClass GDBusMessageClass
;
437 * Class structure for #GDBusMessage.
441 struct _GDBusMessageClass
444 GObjectClass parent_class
;
450 * The #GDBusMessage structure contains only private data and should
451 * only be accessed using the provided API.
458 GObject parent_instance
;
460 GDBusMessageType type
;
461 GDBusMessageFlags flags
;
463 GDBusMessageByteOrder byte_order
;
464 guchar major_protocol_version
;
469 GUnixFDList
*fd_list
;
479 G_DEFINE_TYPE (GDBusMessage
, g_dbus_message
, G_TYPE_OBJECT
)
482 g_dbus_message_finalize (GObject
*object
)
484 GDBusMessage
*message
= G_DBUS_MESSAGE (object
);
486 if (message
->headers
!= NULL
)
487 g_hash_table_unref (message
->headers
);
488 if (message
->body
!= NULL
)
489 g_variant_unref (message
->body
);
491 if (message
->fd_list
!= NULL
)
492 g_object_unref (message
->fd_list
);
495 if (G_OBJECT_CLASS (g_dbus_message_parent_class
)->finalize
!= NULL
)
496 G_OBJECT_CLASS (g_dbus_message_parent_class
)->finalize (object
);
500 g_dbus_message_get_property (GObject
*object
,
505 GDBusMessage
*message
= G_DBUS_MESSAGE (object
);
510 g_value_set_boolean (value
, g_dbus_message_get_locked (message
));
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
520 g_dbus_message_class_init (GDBusMessageClass
*klass
)
522 GObjectClass
*gobject_class
;
524 gobject_class
= G_OBJECT_CLASS (klass
);
525 gobject_class
->finalize
= g_dbus_message_finalize
;
526 gobject_class
->get_property
= g_dbus_message_get_property
;
529 * GDBusConnection:locked:
531 * A boolean specifying whether the message is locked.
535 g_object_class_install_property (gobject_class
,
537 g_param_spec_boolean ("locked",
539 P_("Whether the message is locked"),
542 G_PARAM_STATIC_NAME
|
543 G_PARAM_STATIC_BLURB
|
544 G_PARAM_STATIC_NICK
));
548 g_dbus_message_init (GDBusMessage
*message
)
550 /* Any D-Bus implementation is supposed to handle both Big and
551 * Little Endian encodings and the Endianness is part of the D-Bus
552 * message - we prefer to use Big Endian (since it's Network Byte
553 * Order and just easier to read for humans) but if the machine is
554 * Little Endian we use that for performance reasons.
556 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
557 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
;
559 /* this could also be G_PDP_ENDIAN */
560 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
;
562 message
->headers
= g_hash_table_new_full (g_direct_hash
,
565 (GDestroyNotify
) g_variant_unref
);
569 * g_dbus_message_new:
571 * Creates a new empty #GDBusMessage.
573 * Returns: A #GDBusMessage. Free with g_object_unref().
578 g_dbus_message_new (void)
580 return g_object_new (G_TYPE_DBUS_MESSAGE
, NULL
);
584 * g_dbus_message_new_method_call:
585 * @name: (nullable): A valid D-Bus name or %NULL.
586 * @path: A valid object path.
587 * @interface_: (nullable): A valid D-Bus interface name or %NULL.
588 * @method: A valid method name.
590 * Creates a new #GDBusMessage for a method call.
592 * Returns: A #GDBusMessage. Free with g_object_unref().
597 g_dbus_message_new_method_call (const gchar
*name
,
599 const gchar
*interface_
,
602 GDBusMessage
*message
;
604 g_return_val_if_fail (name
== NULL
|| g_dbus_is_name (name
), NULL
);
605 g_return_val_if_fail (g_variant_is_object_path (path
), NULL
);
606 g_return_val_if_fail (g_dbus_is_member_name (method
), NULL
);
607 g_return_val_if_fail (interface_
== NULL
|| g_dbus_is_interface_name (interface_
), NULL
);
609 message
= g_dbus_message_new ();
610 message
->type
= G_DBUS_MESSAGE_TYPE_METHOD_CALL
;
613 g_dbus_message_set_destination (message
, name
);
614 g_dbus_message_set_path (message
, path
);
615 g_dbus_message_set_member (message
, method
);
616 if (interface_
!= NULL
)
617 g_dbus_message_set_interface (message
, interface_
);
623 * g_dbus_message_new_signal:
624 * @path: A valid object path.
625 * @interface_: A valid D-Bus interface name.
626 * @signal: A valid signal name.
628 * Creates a new #GDBusMessage for a signal emission.
630 * Returns: A #GDBusMessage. Free with g_object_unref().
635 g_dbus_message_new_signal (const gchar
*path
,
636 const gchar
*interface_
,
639 GDBusMessage
*message
;
641 g_return_val_if_fail (g_variant_is_object_path (path
), NULL
);
642 g_return_val_if_fail (g_dbus_is_member_name (signal
), NULL
);
643 g_return_val_if_fail (g_dbus_is_interface_name (interface_
), NULL
);
645 message
= g_dbus_message_new ();
646 message
->type
= G_DBUS_MESSAGE_TYPE_SIGNAL
;
647 message
->flags
= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
649 g_dbus_message_set_path (message
, path
);
650 g_dbus_message_set_member (message
, signal
);
651 g_dbus_message_set_interface (message
, interface_
);
658 * g_dbus_message_new_method_reply:
659 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
660 * create a reply message to.
662 * Creates a new #GDBusMessage that is a reply to @method_call_message.
664 * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
669 g_dbus_message_new_method_reply (GDBusMessage
*method_call_message
)
671 GDBusMessage
*message
;
674 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message
), NULL
);
675 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
, NULL
);
676 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message
) != 0, NULL
);
678 message
= g_dbus_message_new ();
679 message
->type
= G_DBUS_MESSAGE_TYPE_METHOD_RETURN
;
680 message
->flags
= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
681 /* reply with same endianness */
682 message
->byte_order
= method_call_message
->byte_order
;
684 g_dbus_message_set_reply_serial (message
, g_dbus_message_get_serial (method_call_message
));
685 sender
= g_dbus_message_get_sender (method_call_message
);
687 g_dbus_message_set_destination (message
, sender
);
693 * g_dbus_message_new_method_error:
694 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
695 * create a reply message to.
696 * @error_name: A valid D-Bus error name.
697 * @error_message_format: The D-Bus error message in a printf() format.
698 * @...: Arguments for @error_message_format.
700 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
702 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
707 g_dbus_message_new_method_error (GDBusMessage
*method_call_message
,
708 const gchar
*error_name
,
709 const gchar
*error_message_format
,
715 va_start (var_args
, error_message_format
);
716 ret
= g_dbus_message_new_method_error_valist (method_call_message
,
718 error_message_format
,
726 * g_dbus_message_new_method_error_literal:
727 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
728 * create a reply message to.
729 * @error_name: A valid D-Bus error name.
730 * @error_message: The D-Bus error message.
732 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
734 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
739 g_dbus_message_new_method_error_literal (GDBusMessage
*method_call_message
,
740 const gchar
*error_name
,
741 const gchar
*error_message
)
743 GDBusMessage
*message
;
746 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message
), NULL
);
747 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
, NULL
);
748 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message
) != 0, NULL
);
749 g_return_val_if_fail (g_dbus_is_name (error_name
), NULL
);
750 g_return_val_if_fail (error_message
!= NULL
, NULL
);
752 message
= g_dbus_message_new ();
753 message
->type
= G_DBUS_MESSAGE_TYPE_ERROR
;
754 message
->flags
= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
755 /* reply with same endianness */
756 message
->byte_order
= method_call_message
->byte_order
;
758 g_dbus_message_set_reply_serial (message
, g_dbus_message_get_serial (method_call_message
));
759 g_dbus_message_set_error_name (message
, error_name
);
760 g_dbus_message_set_body (message
, g_variant_new ("(s)", error_message
));
762 sender
= g_dbus_message_get_sender (method_call_message
);
764 g_dbus_message_set_destination (message
, sender
);
770 * g_dbus_message_new_method_error_valist:
771 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
772 * create a reply message to.
773 * @error_name: A valid D-Bus error name.
774 * @error_message_format: The D-Bus error message in a printf() format.
775 * @var_args: Arguments for @error_message_format.
777 * Like g_dbus_message_new_method_error() but intended for language bindings.
779 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
785 g_dbus_message_new_method_error_valist (GDBusMessage
*method_call_message
,
786 const gchar
*error_name
,
787 const gchar
*error_message_format
,
791 gchar
*error_message
;
792 error_message
= g_strdup_vprintf (error_message_format
, var_args
);
793 ret
= g_dbus_message_new_method_error_literal (method_call_message
,
796 g_free (error_message
);
800 /* ---------------------------------------------------------------------------------------------------- */
803 * g_dbus_message_get_byte_order:
804 * @message: A #GDBusMessage.
806 * Gets the byte order of @message.
808 * Returns: The byte order.
810 GDBusMessageByteOrder
811 g_dbus_message_get_byte_order (GDBusMessage
*message
)
813 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), (GDBusMessageByteOrder
) 0);
814 return message
->byte_order
;
818 * g_dbus_message_set_byte_order:
819 * @message: A #GDBusMessage.
820 * @byte_order: The byte order.
822 * Sets the byte order of @message.
825 g_dbus_message_set_byte_order (GDBusMessage
*message
,
826 GDBusMessageByteOrder byte_order
)
828 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
832 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
836 message
->byte_order
= byte_order
;
839 /* ---------------------------------------------------------------------------------------------------- */
841 /* TODO: need GI annotations to specify that any guchar value goes for the type */
844 * g_dbus_message_get_message_type:
845 * @message: A #GDBusMessage.
847 * Gets the type of @message.
849 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
854 g_dbus_message_get_message_type (GDBusMessage
*message
)
856 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), G_DBUS_MESSAGE_TYPE_INVALID
);
857 return message
->type
;
861 * g_dbus_message_set_message_type:
862 * @message: A #GDBusMessage.
863 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
865 * Sets @message to be of @type.
870 g_dbus_message_set_message_type (GDBusMessage
*message
,
871 GDBusMessageType type
)
873 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
874 g_return_if_fail (type
>=0 && type
< 256);
878 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
882 message
->type
= type
;
885 /* ---------------------------------------------------------------------------------------------------- */
887 /* TODO: need GI annotations to specify that any guchar value goes for flags */
890 * g_dbus_message_get_flags:
891 * @message: A #GDBusMessage.
893 * Gets the flags for @message.
895 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
900 g_dbus_message_get_flags (GDBusMessage
*message
)
902 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), G_DBUS_MESSAGE_FLAGS_NONE
);
903 return message
->flags
;
907 * g_dbus_message_set_flags:
908 * @message: A #GDBusMessage.
909 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
910 * enumeration bitwise ORed together).
912 * Sets the flags to set on @message.
917 g_dbus_message_set_flags (GDBusMessage
*message
,
918 GDBusMessageFlags flags
)
920 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
921 g_return_if_fail (flags
>=0 && flags
< 256);
925 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
929 message
->flags
= flags
;
932 /* ---------------------------------------------------------------------------------------------------- */
935 * g_dbus_message_get_serial:
936 * @message: A #GDBusMessage.
938 * Gets the serial for @message.
940 * Returns: A #guint32.
945 g_dbus_message_get_serial (GDBusMessage
*message
)
947 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
948 return message
->serial
;
952 * g_dbus_message_set_serial:
953 * @message: A #GDBusMessage.
954 * @serial: A #guint32.
956 * Sets the serial for @message.
961 g_dbus_message_set_serial (GDBusMessage
*message
,
964 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
968 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
972 message
->serial
= serial
;
975 /* ---------------------------------------------------------------------------------------------------- */
977 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
980 * g_dbus_message_get_header:
981 * @message: A #GDBusMessage.
982 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
984 * Gets a header field on @message.
986 * Returns: A #GVariant with the value if the header was found, %NULL
987 * otherwise. Do not free, it is owned by @message.
992 g_dbus_message_get_header (GDBusMessage
*message
,
993 GDBusMessageHeaderField header_field
)
995 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
996 g_return_val_if_fail (header_field
>=0 && header_field
< 256, NULL
);
997 return g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
1001 * g_dbus_message_set_header:
1002 * @message: A #GDBusMessage.
1003 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1004 * @value: (nullable): A #GVariant to set the header field or %NULL to clear the header field.
1006 * Sets a header field on @message.
1008 * If @value is floating, @message assumes ownership of @value.
1013 g_dbus_message_set_header (GDBusMessage
*message
,
1014 GDBusMessageHeaderField header_field
,
1017 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1018 g_return_if_fail (header_field
>=0 && header_field
< 256);
1020 if (message
->locked
)
1022 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
1028 g_hash_table_remove (message
->headers
, GUINT_TO_POINTER (header_field
));
1032 g_hash_table_insert (message
->headers
, GUINT_TO_POINTER (header_field
), g_variant_ref_sink (value
));
1037 * g_dbus_message_get_header_fields:
1038 * @message: A #GDBusMessage.
1040 * Gets an array of all header fields on @message that are set.
1042 * Returns: (array zero-terminated=1): An array of header fields
1043 * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
1044 * is a #guchar. Free with g_free().
1049 g_dbus_message_get_header_fields (GDBusMessage
*message
)
1057 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
1059 keys
= g_hash_table_get_keys (message
->headers
);
1060 num_keys
= g_list_length (keys
);
1061 ret
= g_new (guchar
, num_keys
+ 1);
1062 for (l
= keys
, n
= 0; l
!= NULL
; l
= l
->next
, n
++)
1063 ret
[n
] = GPOINTER_TO_UINT (l
->data
);
1064 g_assert (n
== num_keys
);
1065 ret
[n
] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID
;
1071 /* ---------------------------------------------------------------------------------------------------- */
1074 * g_dbus_message_get_body:
1075 * @message: A #GDBusMessage.
1077 * Gets the body of a message.
1079 * Returns: (transfer none): A #GVariant or %NULL if the body is
1080 * empty. Do not free, it is owned by @message.
1085 g_dbus_message_get_body (GDBusMessage
*message
)
1087 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
1088 return message
->body
;
1092 * g_dbus_message_set_body:
1093 * @message: A #GDBusMessage.
1094 * @body: Either %NULL or a #GVariant that is a tuple.
1096 * Sets the body @message. As a side-effect the
1097 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
1098 * type string of @body (or cleared if @body is %NULL).
1100 * If @body is floating, @message assumes ownership of @body.
1105 g_dbus_message_set_body (GDBusMessage
*message
,
1108 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1109 g_return_if_fail ((body
== NULL
) || g_variant_is_of_type (body
, G_VARIANT_TYPE_TUPLE
));
1111 if (message
->locked
)
1113 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
1117 if (message
->body
!= NULL
)
1118 g_variant_unref (message
->body
);
1121 message
->body
= NULL
;
1122 g_dbus_message_set_signature (message
, NULL
);
1126 const gchar
*type_string
;
1127 gsize type_string_len
;
1130 message
->body
= g_variant_ref_sink (body
);
1132 type_string
= g_variant_get_type_string (body
);
1133 type_string_len
= strlen (type_string
);
1134 g_assert (type_string_len
>= 2);
1135 signature
= g_strndup (type_string
+ 1, type_string_len
- 2);
1136 g_dbus_message_set_signature (message
, signature
);
1141 /* ---------------------------------------------------------------------------------------------------- */
1145 * g_dbus_message_get_unix_fd_list:
1146 * @message: A #GDBusMessage.
1148 * Gets the UNIX file descriptors associated with @message, if any.
1150 * This method is only available on UNIX.
1152 * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
1153 * associated. Do not free, this object is owned by @message.
1158 g_dbus_message_get_unix_fd_list (GDBusMessage
*message
)
1160 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
1161 return message
->fd_list
;
1165 * g_dbus_message_set_unix_fd_list:
1166 * @message: A #GDBusMessage.
1167 * @fd_list: (nullable): A #GUnixFDList or %NULL.
1169 * Sets the UNIX file descriptors associated with @message. As a
1170 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
1171 * field is set to the number of fds in @fd_list (or cleared if
1172 * @fd_list is %NULL).
1174 * This method is only available on UNIX.
1179 g_dbus_message_set_unix_fd_list (GDBusMessage
*message
,
1180 GUnixFDList
*fd_list
)
1182 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1183 g_return_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
));
1185 if (message
->locked
)
1187 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
1191 if (message
->fd_list
!= NULL
)
1192 g_object_unref (message
->fd_list
);
1193 if (fd_list
!= NULL
)
1195 message
->fd_list
= g_object_ref (fd_list
);
1196 g_dbus_message_set_num_unix_fds (message
, g_unix_fd_list_get_length (fd_list
));
1200 message
->fd_list
= NULL
;
1201 g_dbus_message_set_num_unix_fds (message
, 0);
1206 /* ---------------------------------------------------------------------------------------------------- */
1209 get_type_fixed_size (const GVariantType
*type
)
1211 /* NB: we do not treat 'b' as fixed-size here because GVariant and
1212 * D-Bus disagree about the size.
1214 switch (*g_variant_type_peek_string (type
))
1220 case 'i': case 'u': case 'h':
1222 case 'x': case 't': case 'd':
1230 validate_headers (GDBusMessage
*message
,
1235 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1236 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1240 switch (message
->type
)
1242 case G_DBUS_MESSAGE_TYPE_INVALID
:
1243 g_set_error_literal (error
,
1245 G_IO_ERROR_INVALID_ARGUMENT
,
1246 _("type is INVALID"));
1250 case G_DBUS_MESSAGE_TYPE_METHOD_CALL
:
1251 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
) == NULL
||
1252 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
) == NULL
)
1254 g_set_error_literal (error
,
1256 G_IO_ERROR_INVALID_ARGUMENT
,
1257 _("METHOD_CALL message: PATH or MEMBER header field is missing"));
1262 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN
:
1263 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
) == NULL
)
1265 g_set_error_literal (error
,
1267 G_IO_ERROR_INVALID_ARGUMENT
,
1268 _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
1273 case G_DBUS_MESSAGE_TYPE_ERROR
:
1274 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
) == NULL
||
1275 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
) == NULL
)
1277 g_set_error_literal (error
,
1279 G_IO_ERROR_INVALID_ARGUMENT
,
1280 _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
1285 case G_DBUS_MESSAGE_TYPE_SIGNAL
:
1286 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
) == NULL
||
1287 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
) == NULL
||
1288 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
) == NULL
)
1290 g_set_error_literal (error
,
1292 G_IO_ERROR_INVALID_ARGUMENT
,
1293 _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
1296 if (g_strcmp0 (g_dbus_message_get_path (message
), "/org/freedesktop/DBus/Local") == 0)
1298 g_set_error_literal (error
,
1300 G_IO_ERROR_INVALID_ARGUMENT
,
1301 _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
1304 if (g_strcmp0 (g_dbus_message_get_interface (message
), "org.freedesktop.DBus.Local") == 0)
1306 g_set_error_literal (error
,
1308 G_IO_ERROR_INVALID_ARGUMENT
,
1309 _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
1315 /* hitherto unknown type - nothing to check */
1322 g_assert (ret
|| (error
== NULL
|| *error
!= NULL
));
1326 /* ---------------------------------------------------------------------------------------------------- */
1329 ensure_input_padding (GMemoryBuffer
*buf
,
1333 gsize wanted_offset
;
1336 wanted_offset
= ((offset
+ padding_size
- 1) / padding_size
) * padding_size
;
1337 buf
->pos
= wanted_offset
;
1341 static const gchar
*
1342 read_string (GMemoryBuffer
*mbuf
,
1347 const gchar
*end_valid
;
1349 if G_UNLIKELY (mbuf
->pos
+ len
>= mbuf
->valid_len
|| mbuf
->pos
+ len
< mbuf
->pos
)
1351 mbuf
->pos
= mbuf
->valid_len
;
1352 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1355 G_IO_ERROR_INVALID_ARGUMENT
,
1356 g_dngettext (GETTEXT_PACKAGE
,
1357 "Wanted to read %lu byte but only got %lu",
1358 "Wanted to read %lu bytes but only got %lu",
1361 (gulong
)(mbuf
->valid_len
- mbuf
->pos
));
1365 if G_UNLIKELY (mbuf
->data
[mbuf
->pos
+ len
] != '\0')
1367 str
= g_strndup (mbuf
->data
+ mbuf
->pos
, len
);
1370 G_IO_ERROR_INVALID_ARGUMENT
,
1371 _("Expected NUL byte after the string “%s” but found byte %d"),
1372 str
, mbuf
->data
[mbuf
->pos
+ len
]);
1374 mbuf
->pos
+= len
+ 1;
1378 str
= mbuf
->data
+ mbuf
->pos
;
1379 mbuf
->pos
+= len
+ 1;
1381 if G_UNLIKELY (!g_utf8_validate (str
, -1, &end_valid
))
1385 offset
= (gint
) (end_valid
- str
);
1386 valid_str
= g_strndup (str
, offset
);
1389 G_IO_ERROR_INVALID_ARGUMENT
,
1390 _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
1391 "The valid UTF-8 string up until that point was “%s”"),
1402 static gconstpointer
1403 read_bytes (GMemoryBuffer
*mbuf
,
1407 gconstpointer result
;
1409 if G_UNLIKELY (mbuf
->pos
+ len
> mbuf
->valid_len
|| mbuf
->pos
+ len
< mbuf
->pos
)
1411 mbuf
->pos
= mbuf
->valid_len
;
1412 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1415 G_IO_ERROR_INVALID_ARGUMENT
,
1416 g_dngettext (GETTEXT_PACKAGE
,
1417 "Wanted to read %lu byte but only got %lu",
1418 "Wanted to read %lu bytes but only got %lu",
1421 (gulong
)(mbuf
->valid_len
- mbuf
->pos
));
1425 result
= mbuf
->data
+ mbuf
->pos
;
1431 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
1433 /* returns a non-floating GVariant! */
1435 parse_value_from_blob (GMemoryBuffer
*buf
,
1436 const GVariantType
*type
,
1437 gboolean just_align
,
1442 GError
*local_error
;
1443 #ifdef DEBUG_SERIALIZER
1445 #endif /* DEBUG_SERIALIZER */
1446 const gchar
*type_string
;
1448 type_string
= g_variant_type_peek_string (type
);
1450 #ifdef DEBUG_SERIALIZER
1453 s
= g_variant_type_dup_string (type
);
1454 g_print ("%*s%s type %s from offset 0x%04x",
1456 just_align
? "Aligning" : "Reading",
1461 #endif /* DEBUG_SERIALIZER */
1465 #ifdef DEBUG_SERIALIZER
1467 #endif /* DEBUG_SERIALIZER */
1469 switch (type_string
[0])
1471 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1472 ensure_input_padding (buf
, 4);
1476 v
= g_memory_buffer_read_uint32 (buf
);
1477 ret
= g_variant_new_boolean (v
);
1481 case 'y': /* G_VARIANT_TYPE_BYTE */
1485 v
= g_memory_buffer_read_byte (buf
);
1486 ret
= g_variant_new_byte (v
);
1490 case 'n': /* G_VARIANT_TYPE_INT16 */
1491 ensure_input_padding (buf
, 2);
1495 v
= g_memory_buffer_read_int16 (buf
);
1496 ret
= g_variant_new_int16 (v
);
1500 case 'q': /* G_VARIANT_TYPE_UINT16 */
1501 ensure_input_padding (buf
, 2);
1505 v
= g_memory_buffer_read_uint16 (buf
);
1506 ret
= g_variant_new_uint16 (v
);
1510 case 'i': /* G_VARIANT_TYPE_INT32 */
1511 ensure_input_padding (buf
, 4);
1515 v
= g_memory_buffer_read_int32 (buf
);
1516 ret
= g_variant_new_int32 (v
);
1520 case 'u': /* G_VARIANT_TYPE_UINT32 */
1521 ensure_input_padding (buf
, 4);
1525 v
= g_memory_buffer_read_uint32 (buf
);
1526 ret
= g_variant_new_uint32 (v
);
1530 case 'x': /* G_VARIANT_TYPE_INT64 */
1531 ensure_input_padding (buf
, 8);
1535 v
= g_memory_buffer_read_int64 (buf
);
1536 ret
= g_variant_new_int64 (v
);
1540 case 't': /* G_VARIANT_TYPE_UINT64 */
1541 ensure_input_padding (buf
, 8);
1545 v
= g_memory_buffer_read_uint64 (buf
);
1546 ret
= g_variant_new_uint64 (v
);
1550 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1551 ensure_input_padding (buf
, 8);
1558 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
1559 u
.v_uint64
= g_memory_buffer_read_uint64 (buf
);
1560 ret
= g_variant_new_double (u
.v_double
);
1564 case 's': /* G_VARIANT_TYPE_STRING */
1565 ensure_input_padding (buf
, 4);
1570 len
= g_memory_buffer_read_uint32 (buf
);
1571 v
= read_string (buf
, (gsize
) len
, &local_error
);
1574 ret
= g_variant_new_string (v
);
1578 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1579 ensure_input_padding (buf
, 4);
1584 len
= g_memory_buffer_read_uint32 (buf
);
1585 v
= read_string (buf
, (gsize
) len
, &local_error
);
1588 if (!g_variant_is_object_path (v
))
1590 g_set_error (&local_error
,
1592 G_IO_ERROR_INVALID_ARGUMENT
,
1593 _("Parsed value “%s” is not a valid D-Bus object path"),
1597 ret
= g_variant_new_object_path (v
);
1601 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1606 len
= g_memory_buffer_read_byte (buf
);
1607 v
= read_string (buf
, (gsize
) len
, &local_error
);
1610 if (!g_variant_is_signature (v
))
1612 g_set_error (&local_error
,
1614 G_IO_ERROR_INVALID_ARGUMENT
,
1615 _("Parsed value “%s” is not a valid D-Bus signature"),
1619 ret
= g_variant_new_signature (v
);
1623 case 'h': /* G_VARIANT_TYPE_HANDLE */
1624 ensure_input_padding (buf
, 4);
1628 v
= g_memory_buffer_read_int32 (buf
);
1629 ret
= g_variant_new_handle (v
);
1633 case 'a': /* G_VARIANT_TYPE_ARRAY */
1634 ensure_input_padding (buf
, 4);
1636 /* If we are only aligning for this array type, it is the child type of
1637 * another array, which is empty. So, we do not need to add padding for
1638 * this nonexistent array's elements: we only need to align for this
1639 * array itself (4 bytes). See
1640 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1645 const GVariantType
*element_type
;
1648 array_len
= g_memory_buffer_read_uint32 (buf
);
1650 #ifdef DEBUG_SERIALIZER
1652 g_print (": array spans 0x%04x bytes\n", array_len
);
1653 #endif /* DEBUG_SERIALIZER */
1655 if (array_len
> (2<<26))
1657 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1658 g_set_error (&local_error
,
1660 G_IO_ERROR_INVALID_ARGUMENT
,
1661 g_dngettext (GETTEXT_PACKAGE
,
1662 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1663 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1669 element_type
= g_variant_type_element (type
);
1670 fixed_size
= get_type_fixed_size (element_type
);
1672 /* Fast-path the cases like 'ay', etc. */
1673 if (fixed_size
!= 0)
1675 gconstpointer array_data
;
1677 if (array_len
% fixed_size
!= 0)
1679 g_set_error (&local_error
,
1681 G_IO_ERROR_INVALID_ARGUMENT
,
1682 _("Encountered array of type “a%c”, expected to have a length a multiple "
1683 "of %u bytes, but found to be %u bytes in length"),
1684 g_variant_type_peek_string (element_type
)[0], fixed_size
, array_len
);
1688 ensure_input_padding (buf
, fixed_size
);
1689 array_data
= read_bytes (buf
, array_len
, &local_error
);
1690 if (array_data
== NULL
)
1693 ret
= g_variant_new_fixed_array (element_type
, array_data
, array_len
/ fixed_size
, fixed_size
);
1695 if (g_memory_buffer_is_byteswapped (buf
))
1697 GVariant
*tmp
= g_variant_ref_sink (ret
);
1698 ret
= g_variant_byteswap (tmp
);
1699 g_variant_unref (tmp
);
1704 GVariantBuilder builder
;
1708 g_variant_builder_init (&builder
, type
);
1713 item
= parse_value_from_blob (buf
,
1718 g_assert (item
== NULL
);
1723 target
= offset
+ array_len
;
1724 while (offset
< target
)
1727 item
= parse_value_from_blob (buf
,
1734 g_variant_builder_clear (&builder
);
1737 g_variant_builder_add_value (&builder
, item
);
1738 g_variant_unref (item
);
1743 ret
= g_variant_builder_end (&builder
);
1749 if (g_variant_type_is_dict_entry (type
))
1751 const GVariantType
*key_type
;
1752 const GVariantType
*value_type
;
1756 ensure_input_padding (buf
, 8);
1758 #ifdef DEBUG_SERIALIZER
1761 #endif /* DEBUG_SERIALIZER */
1765 key_type
= g_variant_type_key (type
);
1766 key
= parse_value_from_blob (buf
,
1773 value_type
= g_variant_type_value (type
);
1774 value
= parse_value_from_blob (buf
,
1781 g_variant_unref (key
);
1784 ret
= g_variant_new_dict_entry (key
, value
);
1785 g_variant_unref (key
);
1786 g_variant_unref (value
);
1789 else if (g_variant_type_is_tuple (type
))
1791 ensure_input_padding (buf
, 8);
1793 #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
))
1828 #ifdef DEBUG_SERIALIZER
1831 #endif /* DEBUG_SERIALIZER */
1837 GVariantType
*variant_type
;
1840 siglen
= g_memory_buffer_read_byte (buf
);
1841 sig
= read_string (buf
, (gsize
) siglen
, &local_error
);
1844 if (!g_variant_is_signature (sig
))
1846 g_set_error (&local_error
,
1848 G_IO_ERROR_INVALID_ARGUMENT
,
1849 _("Parsed value “%s” for variant is not a valid D-Bus signature"),
1853 variant_type
= g_variant_type_new (sig
);
1854 value
= parse_value_from_blob (buf
,
1859 g_variant_type_free (variant_type
);
1862 ret
= g_variant_new_variant (value
);
1863 g_variant_unref (value
);
1869 s
= g_variant_type_dup_string (type
);
1870 g_set_error (&local_error
,
1872 G_IO_ERROR_INVALID_ARGUMENT
,
1873 _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"),
1881 g_assert ((just_align
&& ret
== NULL
) || (!just_align
&& ret
!= NULL
));
1883 #ifdef DEBUG_SERIALIZER
1889 if (g_variant_type_equal (type
, G_VARIANT_TYPE_BYTE
))
1891 s
= g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret
), g_variant_get_byte (ret
));
1895 s
= g_variant_print (ret
, FALSE
);
1897 g_print (": %s\n", s
);
1901 #endif /* DEBUG_SERIALIZER */
1903 /* sink the reference, if floating */
1905 g_variant_take_ref (ret
);
1909 #ifdef DEBUG_SERIALIZER
1911 "%*sFAILURE: %s (%s, %d)\n",
1913 local_error
->message
,
1914 g_quark_to_string (local_error
->domain
),
1916 #endif /* DEBUG_SERIALIZER */
1917 g_propagate_error (error
, local_error
);
1921 /* ---------------------------------------------------------------------------------------------------- */
1923 /* message_header must be at least 16 bytes */
1926 * g_dbus_message_bytes_needed:
1927 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1928 * @blob_len: The length of @blob (must be at least 16).
1929 * @error: Return location for error or %NULL.
1931 * Utility function to calculate how many bytes are needed to
1932 * completely deserialize the D-Bus message stored at @blob.
1934 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1935 * @blob contains invalid data or not enough data is available to
1936 * determine the size).
1941 g_dbus_message_bytes_needed (guchar
*blob
,
1949 g_return_val_if_fail (blob
!= NULL
, -1);
1950 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, -1);
1951 g_return_val_if_fail (blob_len
>= 16, -1);
1955 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1956 ret
= 12 + 4 + GUINT32_FROM_LE (((guint32
*) blob
)[3]);
1957 /* round up so it's a multiple of 8 */
1958 ret
= 8 * ((ret
+ 7)/8);
1959 /* finally add the body size */
1960 ret
+= GUINT32_FROM_LE (((guint32
*) blob
)[1]);
1962 else if (blob
[0] == 'B')
1964 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1965 ret
= 12 + 4 + GUINT32_FROM_BE (((guint32
*) blob
)[3]);
1966 /* round up so it's a multiple of 8 */
1967 ret
= 8 * ((ret
+ 7)/8);
1968 /* finally add the body size */
1969 ret
+= GUINT32_FROM_BE (((guint32
*) blob
)[1]);
1975 G_IO_ERROR_INVALID_ARGUMENT
,
1976 "Unable to determine message blob length - given blob is malformed");
1983 G_IO_ERROR_INVALID_ARGUMENT
,
1984 "Blob indicates that message exceeds maximum message length (128MiB)");
1991 /* ---------------------------------------------------------------------------------------------------- */
1994 * g_dbus_message_new_from_blob:
1995 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1996 * @blob_len: The length of @blob.
1997 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1998 * @error: Return location for error or %NULL.
2000 * Creates a new #GDBusMessage from the data stored at @blob. The byte
2001 * order that the message was in can be retrieved using
2002 * g_dbus_message_get_byte_order().
2004 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
2010 g_dbus_message_new_from_blob (guchar
*blob
,
2012 GDBusCapabilityFlags capabilities
,
2017 GDBusMessage
*message
;
2019 guchar major_protocol_version
;
2020 guint32 message_body_len
;
2024 GVariant
*signature
;
2026 /* TODO: check against @capabilities */
2030 g_return_val_if_fail (blob
!= NULL
, NULL
);
2031 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2032 g_return_val_if_fail (blob_len
>= 12, NULL
);
2034 message
= g_dbus_message_new ();
2036 memset (&mbuf
, 0, sizeof (mbuf
));
2037 mbuf
.data
= (gchar
*)blob
;
2038 mbuf
.len
= mbuf
.valid_len
= blob_len
;
2040 endianness
= g_memory_buffer_read_byte (&mbuf
);
2044 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
2045 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
;
2048 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
2049 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
;
2054 G_IO_ERROR_INVALID_ARGUMENT
,
2055 _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"),
2060 message
->type
= g_memory_buffer_read_byte (&mbuf
);
2061 message
->flags
= g_memory_buffer_read_byte (&mbuf
);
2062 major_protocol_version
= g_memory_buffer_read_byte (&mbuf
);
2063 if (major_protocol_version
!= 1)
2067 G_IO_ERROR_INVALID_ARGUMENT
,
2068 _("Invalid major protocol version. Expected 1 but found %d"),
2069 major_protocol_version
);
2072 message_body_len
= g_memory_buffer_read_uint32 (&mbuf
);
2073 message
->serial
= g_memory_buffer_read_uint32 (&mbuf
);
2075 #ifdef DEBUG_SERIALIZER
2076 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2079 s
= _g_dbus_hexdump ((const gchar
*) blob
, blob_len
, 2);
2080 g_print ("%s\n", s
);
2083 #endif /* DEBUG_SERIALIZER */
2085 #ifdef DEBUG_SERIALIZER
2086 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2087 #endif /* DEBUG_SERIALIZER */
2088 headers
= parse_value_from_blob (&mbuf
,
2089 G_VARIANT_TYPE ("a{yv}"),
2093 if (headers
== NULL
)
2095 g_variant_iter_init (&iter
, headers
);
2096 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2098 guchar header_field
;
2100 g_variant_get (item
,
2104 g_dbus_message_set_header (message
, header_field
, value
);
2105 g_variant_unref (value
);
2106 g_variant_unref (item
);
2108 g_variant_unref (headers
);
2110 signature
= g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
2111 if (signature
!= NULL
)
2113 const gchar
*signature_str
;
2114 gsize signature_str_len
;
2116 signature_str
= g_variant_get_string (signature
, &signature_str_len
);
2118 /* signature but no body */
2119 if (message_body_len
== 0 && signature_str_len
> 0)
2123 G_IO_ERROR_INVALID_ARGUMENT
,
2124 _("Signature header with signature “%s” found but message body is empty"),
2128 else if (signature_str_len
> 0)
2130 GVariantType
*variant_type
;
2131 gchar
*tupled_signature_str
;
2133 if (!g_variant_is_signature (signature_str
))
2137 G_IO_ERROR_INVALID_ARGUMENT
,
2138 _("Parsed value “%s” is not a valid D-Bus signature (for body)"),
2142 tupled_signature_str
= g_strdup_printf ("(%s)", signature_str
);
2143 variant_type
= g_variant_type_new (tupled_signature_str
);
2144 g_free (tupled_signature_str
);
2145 #ifdef DEBUG_SERIALIZER
2146 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2147 #endif /* DEBUG_SERIALIZER */
2148 message
->body
= parse_value_from_blob (&mbuf
,
2153 g_variant_type_free (variant_type
);
2154 if (message
->body
== NULL
)
2160 /* no signature, this is only OK if the body is empty */
2161 if (message_body_len
!= 0)
2163 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
2166 G_IO_ERROR_INVALID_ARGUMENT
,
2167 g_dngettext (GETTEXT_PACKAGE
,
2168 "No signature header in message but the message body is %u byte",
2169 "No signature header in message but the message body is %u bytes",
2176 if (!validate_headers (message
, error
))
2178 g_prefix_error (error
, _("Cannot deserialize message: "));
2191 if (message
!= NULL
)
2192 g_object_unref (message
);
2197 /* ---------------------------------------------------------------------------------------------------- */
2200 ensure_output_padding (GMemoryBuffer
*mbuf
,
2204 gsize wanted_offset
;
2205 gsize padding_needed
;
2209 wanted_offset
= ((offset
+ padding_size
- 1) / padding_size
) * padding_size
;
2210 padding_needed
= wanted_offset
- offset
;
2212 for (n
= 0; n
< padding_needed
; n
++)
2213 g_memory_buffer_put_byte (mbuf
, '\0');
2215 return padding_needed
;
2218 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
2220 append_value_to_blob (GVariant
*value
,
2221 const GVariantType
*type
,
2222 GMemoryBuffer
*mbuf
,
2223 gsize
*out_padding_added
,
2226 gsize padding_added
;
2227 const gchar
*type_string
;
2229 type_string
= g_variant_type_peek_string (type
);
2233 switch (type_string
[0])
2235 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
2236 padding_added
= ensure_output_padding (mbuf
, 4);
2239 gboolean v
= g_variant_get_boolean (value
);
2240 g_memory_buffer_put_uint32 (mbuf
, v
);
2244 case 'y': /* G_VARIANT_TYPE_BYTE */
2247 guint8 v
= g_variant_get_byte (value
);
2248 g_memory_buffer_put_byte (mbuf
, v
);
2252 case 'n': /* G_VARIANT_TYPE_INT16 */
2253 padding_added
= ensure_output_padding (mbuf
, 2);
2256 gint16 v
= g_variant_get_int16 (value
);
2257 g_memory_buffer_put_int16 (mbuf
, v
);
2261 case 'q': /* G_VARIANT_TYPE_UINT16 */
2262 padding_added
= ensure_output_padding (mbuf
, 2);
2265 guint16 v
= g_variant_get_uint16 (value
);
2266 g_memory_buffer_put_uint16 (mbuf
, v
);
2270 case 'i': /* G_VARIANT_TYPE_INT32 */
2271 padding_added
= ensure_output_padding (mbuf
, 4);
2274 gint32 v
= g_variant_get_int32 (value
);
2275 g_memory_buffer_put_int32 (mbuf
, v
);
2279 case 'u': /* G_VARIANT_TYPE_UINT32 */
2280 padding_added
= ensure_output_padding (mbuf
, 4);
2283 guint32 v
= g_variant_get_uint32 (value
);
2284 g_memory_buffer_put_uint32 (mbuf
, v
);
2288 case 'x': /* G_VARIANT_TYPE_INT64 */
2289 padding_added
= ensure_output_padding (mbuf
, 8);
2292 gint64 v
= g_variant_get_int64 (value
);
2293 g_memory_buffer_put_int64 (mbuf
, v
);
2297 case 't': /* G_VARIANT_TYPE_UINT64 */
2298 padding_added
= ensure_output_padding (mbuf
, 8);
2301 guint64 v
= g_variant_get_uint64 (value
);
2302 g_memory_buffer_put_uint64 (mbuf
, v
);
2306 case 'd': /* G_VARIANT_TYPE_DOUBLE */
2307 padding_added
= ensure_output_padding (mbuf
, 8);
2314 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
2315 u
.v_double
= g_variant_get_double (value
);
2316 g_memory_buffer_put_uint64 (mbuf
, u
.v_uint64
);
2320 case 's': /* G_VARIANT_TYPE_STRING */
2321 padding_added
= ensure_output_padding (mbuf
, 4);
2327 v
= g_variant_get_string (value
, &len
);
2328 g_assert (g_utf8_validate (v
, -1, &end
) && (end
== v
+ len
));
2329 g_memory_buffer_put_uint32 (mbuf
, len
);
2330 g_memory_buffer_put_string (mbuf
, v
);
2331 g_memory_buffer_put_byte (mbuf
, '\0');
2335 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
2336 padding_added
= ensure_output_padding (mbuf
, 4);
2340 const gchar
*v
= g_variant_get_string (value
, &len
);
2341 g_assert (g_variant_is_object_path (v
));
2342 g_memory_buffer_put_uint32 (mbuf
, len
);
2343 g_memory_buffer_put_string (mbuf
, v
);
2344 g_memory_buffer_put_byte (mbuf
, '\0');
2348 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2352 const gchar
*v
= g_variant_get_string (value
, &len
);
2353 g_assert (g_variant_is_signature (v
));
2354 g_memory_buffer_put_byte (mbuf
, len
);
2355 g_memory_buffer_put_string (mbuf
, v
);
2356 g_memory_buffer_put_byte (mbuf
, '\0');
2360 case 'h': /* G_VARIANT_TYPE_HANDLE */
2361 padding_added
= ensure_output_padding (mbuf
, 4);
2364 gint32 v
= g_variant_get_handle (value
);
2365 g_memory_buffer_put_int32 (mbuf
, v
);
2369 case 'a': /* G_VARIANT_TYPE_ARRAY */
2371 const GVariantType
*element_type
;
2374 goffset array_len_offset
;
2375 goffset array_payload_begin_offset
;
2380 padding_added
= ensure_output_padding (mbuf
, 4);
2383 /* array length - will be filled in later */
2384 array_len_offset
= mbuf
->valid_len
;
2385 g_memory_buffer_put_uint32 (mbuf
, 0xF00DFACE);
2387 /* From the D-Bus spec:
2389 * "A UINT32 giving the length of the array data in bytes,
2390 * followed by alignment padding to the alignment boundary of
2391 * the array element type, followed by each array element. The
2392 * array length is from the end of the alignment padding to
2393 * the end of the last element, i.e. it does not include the
2394 * padding after the length, or any padding after the last
2397 * Thus, we need to count how much padding the first element
2398 * contributes and subtract that from the array length.
2400 array_payload_begin_offset
= mbuf
->valid_len
;
2402 element_type
= g_variant_type_element (type
);
2403 fixed_size
= get_type_fixed_size (element_type
);
2405 if (g_variant_n_children (value
) == 0)
2407 gsize padding_added_for_item
;
2408 if (!append_value_to_blob (NULL
,
2411 &padding_added_for_item
,
2414 array_payload_begin_offset
+= padding_added_for_item
;
2416 else if (fixed_size
!= 0)
2418 GVariant
*use_value
;
2420 if (g_memory_buffer_is_byteswapped (mbuf
))
2421 use_value
= g_variant_byteswap (value
);
2423 use_value
= g_variant_ref (value
);
2425 array_payload_begin_offset
+= ensure_output_padding (mbuf
, fixed_size
);
2427 array_len
= g_variant_get_size (use_value
);
2428 g_memory_buffer_write (mbuf
, g_variant_get_data (use_value
), array_len
);
2429 g_variant_unref (use_value
);
2435 g_variant_iter_init (&iter
, value
);
2436 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2438 gsize padding_added_for_item
;
2439 if (!append_value_to_blob (item
,
2440 g_variant_get_type (item
),
2442 &padding_added_for_item
,
2445 g_variant_unref (item
);
2448 g_variant_unref (item
);
2451 array_payload_begin_offset
+= padding_added_for_item
;
2457 cur_offset
= mbuf
->valid_len
;
2458 array_len
= cur_offset
- array_payload_begin_offset
;
2459 mbuf
->pos
= array_len_offset
;
2461 g_memory_buffer_put_uint32 (mbuf
, array_len
);
2462 mbuf
->pos
= cur_offset
;
2468 if (g_variant_type_is_dict_entry (type
) || g_variant_type_is_tuple (type
))
2470 padding_added
= ensure_output_padding (mbuf
, 8);
2475 g_variant_iter_init (&iter
, value
);
2476 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2478 if (!append_value_to_blob (item
,
2479 g_variant_get_type (item
),
2484 g_variant_unref (item
);
2487 g_variant_unref (item
);
2491 else if (g_variant_type_is_variant (type
))
2496 const gchar
*signature
;
2497 child
= g_variant_get_child_value (value
, 0);
2498 signature
= g_variant_get_type_string (child
);
2499 g_memory_buffer_put_byte (mbuf
, strlen (signature
));
2500 g_memory_buffer_put_string (mbuf
, signature
);
2501 g_memory_buffer_put_byte (mbuf
, '\0');
2502 if (!append_value_to_blob (child
,
2503 g_variant_get_type (child
),
2508 g_variant_unref (child
);
2511 g_variant_unref (child
);
2518 G_IO_ERROR_INVALID_ARGUMENT
,
2519 _("Error serializing GVariant with type string “%s” to the D-Bus wire format"),
2520 g_variant_get_type_string (value
));
2526 if (out_padding_added
!= NULL
)
2527 *out_padding_added
= padding_added
;
2536 append_body_to_blob (GVariant
*value
,
2537 GMemoryBuffer
*mbuf
,
2543 if (!g_variant_is_of_type (value
, G_VARIANT_TYPE_TUPLE
))
2547 G_IO_ERROR_INVALID_ARGUMENT
,
2548 "Expected a tuple for the body of the GDBusMessage.");
2552 g_variant_iter_init (&iter
, value
);
2553 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2555 if (!append_value_to_blob (item
,
2556 g_variant_get_type (item
),
2561 g_variant_unref (item
);
2564 g_variant_unref (item
);
2572 /* ---------------------------------------------------------------------------------------------------- */
2575 * g_dbus_message_to_blob:
2576 * @message: A #GDBusMessage.
2577 * @out_size: Return location for size of generated blob.
2578 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2579 * @error: Return location for error.
2581 * Serializes @message to a blob. The byte order returned by
2582 * g_dbus_message_get_byte_order() will be used.
2584 * Returns: (array length=out_size) (transfer full): A pointer to a
2585 * valid binary D-Bus message of @out_size bytes generated by @message
2586 * or %NULL if @error is set. Free with g_free().
2591 g_dbus_message_to_blob (GDBusMessage
*message
,
2593 GDBusCapabilityFlags capabilities
,
2599 goffset body_len_offset
;
2600 goffset body_start_offset
;
2602 GVariant
*header_fields
;
2603 GVariantBuilder builder
;
2604 GHashTableIter hash_iter
;
2606 GVariant
*header_value
;
2607 GVariant
*signature
;
2608 const gchar
*signature_str
;
2609 gint num_fds_in_message
;
2610 gint num_fds_according_to_header
;
2612 /* TODO: check against @capabilities */
2616 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2617 g_return_val_if_fail (out_size
!= NULL
, NULL
);
2618 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2620 memset (&mbuf
, 0, sizeof (mbuf
));
2621 mbuf
.len
= MIN_ARRAY_SIZE
;
2622 mbuf
.data
= g_malloc (mbuf
.len
);
2624 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
;
2625 switch (message
->byte_order
)
2627 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
:
2628 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
2630 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
:
2631 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
2636 g_memory_buffer_put_byte (&mbuf
, (guchar
) message
->byte_order
);
2637 g_memory_buffer_put_byte (&mbuf
, message
->type
);
2638 g_memory_buffer_put_byte (&mbuf
, message
->flags
);
2639 g_memory_buffer_put_byte (&mbuf
, 1); /* major protocol version */
2640 body_len_offset
= mbuf
.valid_len
;
2641 /* body length - will be filled in later */
2642 g_memory_buffer_put_uint32 (&mbuf
, 0xF00DFACE);
2643 g_memory_buffer_put_uint32 (&mbuf
, message
->serial
);
2645 num_fds_in_message
= 0;
2647 if (message
->fd_list
!= NULL
)
2648 num_fds_in_message
= g_unix_fd_list_get_length (message
->fd_list
);
2650 num_fds_according_to_header
= g_dbus_message_get_num_unix_fds (message
);
2651 if (num_fds_in_message
!= num_fds_according_to_header
)
2655 G_IO_ERROR_INVALID_ARGUMENT
,
2656 _("Message has %d file descriptors but the header field indicates %d file descriptors"),
2658 num_fds_according_to_header
);
2662 if (!validate_headers (message
, error
))
2664 g_prefix_error (error
, _("Cannot serialize message: "));
2668 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("a{yv}"));
2669 g_hash_table_iter_init (&hash_iter
, message
->headers
);
2670 while (g_hash_table_iter_next (&hash_iter
, &key
, (gpointer
) &header_value
))
2672 g_variant_builder_add (&builder
,
2674 (guchar
) GPOINTER_TO_UINT (key
),
2677 header_fields
= g_variant_builder_end (&builder
);
2679 if (!append_value_to_blob (header_fields
,
2680 g_variant_get_type (header_fields
),
2685 g_variant_unref (header_fields
);
2688 g_variant_unref (header_fields
);
2690 /* header size must be a multiple of 8 */
2691 ensure_output_padding (&mbuf
, 8);
2693 body_start_offset
= mbuf
.valid_len
;
2695 signature
= g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
2696 signature_str
= NULL
;
2697 if (signature
!= NULL
)
2698 signature_str
= g_variant_get_string (signature
, NULL
);
2699 if (message
->body
!= NULL
)
2701 gchar
*tupled_signature_str
;
2702 tupled_signature_str
= g_strdup_printf ("(%s)", signature_str
);
2703 if (signature
== NULL
)
2707 G_IO_ERROR_INVALID_ARGUMENT
,
2708 _("Message body has signature “%s” but there is no signature header"),
2710 g_free (tupled_signature_str
);
2713 else if (g_strcmp0 (tupled_signature_str
, g_variant_get_type_string (message
->body
)) != 0)
2717 G_IO_ERROR_INVALID_ARGUMENT
,
2718 _("Message body has type signature “%s” but signature in the header field is “%s”"),
2719 tupled_signature_str
, g_variant_get_type_string (message
->body
));
2720 g_free (tupled_signature_str
);
2723 g_free (tupled_signature_str
);
2724 if (!append_body_to_blob (message
->body
, &mbuf
, error
))
2729 if (signature
!= NULL
&& strlen (signature_str
) > 0)
2733 G_IO_ERROR_INVALID_ARGUMENT
,
2734 _("Message body is empty but signature in the header field is “(%s)”"),
2740 /* OK, we're done writing the message - set the body length */
2741 size
= mbuf
.valid_len
;
2742 body_size
= size
- body_start_offset
;
2744 mbuf
.pos
= body_len_offset
;
2746 g_memory_buffer_put_uint32 (&mbuf
, body_size
);
2749 ret
= (guchar
*)mbuf
.data
;
2758 /* ---------------------------------------------------------------------------------------------------- */
2761 get_uint32_header (GDBusMessage
*message
,
2762 GDBusMessageHeaderField header_field
)
2768 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2769 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_UINT32
))
2770 ret
= g_variant_get_uint32 (value
);
2775 static const gchar
*
2776 get_string_header (GDBusMessage
*message
,
2777 GDBusMessageHeaderField header_field
)
2783 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2784 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
))
2785 ret
= g_variant_get_string (value
, NULL
);
2790 static const gchar
*
2791 get_object_path_header (GDBusMessage
*message
,
2792 GDBusMessageHeaderField header_field
)
2798 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2799 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_OBJECT_PATH
))
2800 ret
= g_variant_get_string (value
, NULL
);
2805 static const gchar
*
2806 get_signature_header (GDBusMessage
*message
,
2807 GDBusMessageHeaderField header_field
)
2813 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2814 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_SIGNATURE
))
2815 ret
= g_variant_get_string (value
, NULL
);
2820 /* ---------------------------------------------------------------------------------------------------- */
2823 set_uint32_header (GDBusMessage
*message
,
2824 GDBusMessageHeaderField header_field
,
2827 g_dbus_message_set_header (message
,
2829 g_variant_new_uint32 (value
));
2833 set_string_header (GDBusMessage
*message
,
2834 GDBusMessageHeaderField header_field
,
2837 g_dbus_message_set_header (message
,
2839 value
== NULL
? NULL
: g_variant_new_string (value
));
2843 set_object_path_header (GDBusMessage
*message
,
2844 GDBusMessageHeaderField header_field
,
2847 g_dbus_message_set_header (message
,
2849 value
== NULL
? NULL
: g_variant_new_object_path (value
));
2853 set_signature_header (GDBusMessage
*message
,
2854 GDBusMessageHeaderField header_field
,
2857 g_dbus_message_set_header (message
,
2859 value
== NULL
? NULL
: g_variant_new_signature (value
));
2862 /* ---------------------------------------------------------------------------------------------------- */
2865 * g_dbus_message_get_reply_serial:
2866 * @message: A #GDBusMessage.
2868 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2870 * Returns: The value.
2875 g_dbus_message_get_reply_serial (GDBusMessage
*message
)
2877 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
2878 return get_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
);
2882 * g_dbus_message_set_reply_serial:
2883 * @message: A #GDBusMessage.
2884 * @value: The value to set.
2886 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2891 g_dbus_message_set_reply_serial (GDBusMessage
*message
,
2894 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2895 set_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
, value
);
2898 /* ---------------------------------------------------------------------------------------------------- */
2901 * g_dbus_message_get_interface:
2902 * @message: A #GDBusMessage.
2904 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2906 * Returns: The value.
2911 g_dbus_message_get_interface (GDBusMessage
*message
)
2913 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2914 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
);
2918 * g_dbus_message_set_interface:
2919 * @message: A #GDBusMessage.
2920 * @value: The value to set.
2922 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2927 g_dbus_message_set_interface (GDBusMessage
*message
,
2930 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2931 g_return_if_fail (value
== NULL
|| g_dbus_is_interface_name (value
));
2932 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
, value
);
2935 /* ---------------------------------------------------------------------------------------------------- */
2938 * g_dbus_message_get_member:
2939 * @message: A #GDBusMessage.
2941 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2943 * Returns: The value.
2948 g_dbus_message_get_member (GDBusMessage
*message
)
2950 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2951 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
);
2955 * g_dbus_message_set_member:
2956 * @message: A #GDBusMessage.
2957 * @value: The value to set.
2959 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2964 g_dbus_message_set_member (GDBusMessage
*message
,
2967 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2968 g_return_if_fail (value
== NULL
|| g_dbus_is_member_name (value
));
2969 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
, value
);
2972 /* ---------------------------------------------------------------------------------------------------- */
2975 * g_dbus_message_get_path:
2976 * @message: A #GDBusMessage.
2978 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2980 * Returns: The value.
2985 g_dbus_message_get_path (GDBusMessage
*message
)
2987 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2988 return get_object_path_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
);
2992 * g_dbus_message_set_path:
2993 * @message: A #GDBusMessage.
2994 * @value: The value to set.
2996 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
3001 g_dbus_message_set_path (GDBusMessage
*message
,
3004 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3005 g_return_if_fail (value
== NULL
|| g_variant_is_object_path (value
));
3006 set_object_path_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
, value
);
3009 /* ---------------------------------------------------------------------------------------------------- */
3012 * g_dbus_message_get_sender:
3013 * @message: A #GDBusMessage.
3015 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3017 * Returns: The value.
3022 g_dbus_message_get_sender (GDBusMessage
*message
)
3024 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3025 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SENDER
);
3029 * g_dbus_message_set_sender:
3030 * @message: A #GDBusMessage.
3031 * @value: The value to set.
3033 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3038 g_dbus_message_set_sender (GDBusMessage
*message
,
3041 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3042 g_return_if_fail (value
== NULL
|| g_dbus_is_name (value
));
3043 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SENDER
, value
);
3046 /* ---------------------------------------------------------------------------------------------------- */
3049 * g_dbus_message_get_destination:
3050 * @message: A #GDBusMessage.
3052 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3054 * Returns: The value.
3059 g_dbus_message_get_destination (GDBusMessage
*message
)
3061 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3062 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
);
3066 * g_dbus_message_set_destination:
3067 * @message: A #GDBusMessage.
3068 * @value: The value to set.
3070 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3075 g_dbus_message_set_destination (GDBusMessage
*message
,
3078 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3079 g_return_if_fail (value
== NULL
|| g_dbus_is_name (value
));
3080 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
, value
);
3083 /* ---------------------------------------------------------------------------------------------------- */
3086 * g_dbus_message_get_error_name:
3087 * @message: A #GDBusMessage.
3089 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3091 * Returns: The value.
3096 g_dbus_message_get_error_name (GDBusMessage
*message
)
3098 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3099 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
);
3103 * g_dbus_message_set_error_name:
3104 * @message: A #GDBusMessage.
3105 * @value: The value to set.
3107 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3112 g_dbus_message_set_error_name (GDBusMessage
*message
,
3115 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3116 g_return_if_fail (value
== NULL
|| g_dbus_is_interface_name (value
));
3117 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
, value
);
3120 /* ---------------------------------------------------------------------------------------------------- */
3123 * g_dbus_message_get_signature:
3124 * @message: A #GDBusMessage.
3126 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3128 * Returns: The value.
3133 g_dbus_message_get_signature (GDBusMessage
*message
)
3136 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3137 ret
= get_signature_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
3144 * g_dbus_message_set_signature:
3145 * @message: A #GDBusMessage.
3146 * @value: The value to set.
3148 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3153 g_dbus_message_set_signature (GDBusMessage
*message
,
3156 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3157 g_return_if_fail (value
== NULL
|| g_variant_is_signature (value
));
3158 set_signature_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
, value
);
3161 /* ---------------------------------------------------------------------------------------------------- */
3164 * g_dbus_message_get_arg0:
3165 * @message: A #GDBusMessage.
3167 * Convenience to get the first item in the body of @message.
3169 * Returns: The string item or %NULL if the first item in the body of
3170 * @message is not a string.
3175 g_dbus_message_get_arg0 (GDBusMessage
*message
)
3179 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3183 if (message
->body
!= NULL
&& g_variant_is_of_type (message
->body
, G_VARIANT_TYPE_TUPLE
))
3186 item
= g_variant_get_child_value (message
->body
, 0);
3187 if (g_variant_is_of_type (item
, G_VARIANT_TYPE_STRING
))
3188 ret
= g_variant_get_string (item
, NULL
);
3189 g_variant_unref (item
);
3195 /* ---------------------------------------------------------------------------------------------------- */
3198 * g_dbus_message_get_num_unix_fds:
3199 * @message: A #GDBusMessage.
3201 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3203 * Returns: The value.
3208 g_dbus_message_get_num_unix_fds (GDBusMessage
*message
)
3210 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
3211 return get_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS
);
3215 * g_dbus_message_set_num_unix_fds:
3216 * @message: A #GDBusMessage.
3217 * @value: The value to set.
3219 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3224 g_dbus_message_set_num_unix_fds (GDBusMessage
*message
,
3227 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3228 set_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS
, value
);
3231 /* ---------------------------------------------------------------------------------------------------- */
3234 * g_dbus_message_to_gerror:
3235 * @message: A #GDBusMessage.
3236 * @error: The #GError to set.
3238 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
3239 * nothing and returns %FALSE.
3241 * Otherwise this method encodes the error in @message as a #GError
3242 * using g_dbus_error_set_dbus_error() using the information in the
3243 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
3244 * well as the first string item in @message's body.
3246 * Returns: %TRUE if @error was set, %FALSE otherwise.
3251 g_dbus_message_to_gerror (GDBusMessage
*message
,
3255 const gchar
*error_name
;
3257 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
3260 if (message
->type
!= G_DBUS_MESSAGE_TYPE_ERROR
)
3263 error_name
= g_dbus_message_get_error_name (message
);
3264 if (error_name
!= NULL
)
3268 body
= g_dbus_message_get_body (message
);
3270 if (body
!= NULL
&& g_variant_is_of_type (body
, G_VARIANT_TYPE ("(s)")))
3272 const gchar
*error_message
;
3273 g_variant_get (body
, "(&s)", &error_message
);
3274 g_dbus_error_set_dbus_error (error
,
3281 /* these two situations are valid, yet pretty rare */
3284 g_dbus_error_set_dbus_error (error
,
3287 _("Error return with body of type “%s”"),
3288 g_variant_get_type_string (body
));
3292 g_dbus_error_set_dbus_error (error
,
3295 _("Error return with empty body"));
3301 /* TOOD: this shouldn't happen - should check this at message serialization
3302 * time and disconnect the peer.
3307 "Error return without error-name header!");
3316 /* ---------------------------------------------------------------------------------------------------- */
3319 flags_to_string (GType flags_type
, guint value
)
3325 klass
= g_type_class_ref (flags_type
);
3326 s
= g_string_new (NULL
);
3327 for (n
= 0; n
< 32; n
++)
3329 if ((value
& (1<<n
)) != 0)
3331 GFlagsValue
*flags_value
;
3332 flags_value
= g_flags_get_first_value (klass
, (1<<n
));
3334 g_string_append_c (s
, ',');
3335 if (flags_value
!= NULL
)
3336 g_string_append (s
, flags_value
->value_nick
);
3338 g_string_append_printf (s
, "unknown (bit %d)", n
);
3342 g_string_append (s
, "none");
3343 g_type_class_unref (klass
);
3344 return g_string_free (s
, FALSE
);
3348 _sort_keys_func (gconstpointer a
,
3354 ia
= GPOINTER_TO_INT (a
);
3355 ib
= GPOINTER_TO_INT (b
);
3361 * g_dbus_message_print:
3362 * @message: A #GDBusMessage.
3363 * @indent: Indentation level.
3365 * Produces a human-readable multi-line description of @message.
3367 * The contents of the description has no ABI guarantees, the contents
3368 * and formatting is subject to change at any time. Typical output
3369 * looks something like this:
3376 * path -> objectpath '/org/gtk/GDBus/TestObject'
3377 * interface -> 'org.gtk.GDBus.TestInterface'
3378 * member -> 'GimmeStdout'
3379 * destination -> ':1.146'
3381 * UNIX File Descriptors:
3386 * Type: method-return
3387 * Flags: no-reply-expected
3391 * reply-serial -> uint32 4
3392 * destination -> ':1.159'
3393 * sender -> ':1.146'
3394 * num-unix-fds -> uint32 1
3396 * UNIX File Descriptors:
3397 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3400 * Returns: A string that should be freed with g_free().
3405 g_dbus_message_print (GDBusMessage
*message
,
3413 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3415 str
= g_string_new (NULL
);
3417 s
= _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE
, message
->type
);
3418 g_string_append_printf (str
, "%*sType: %s\n", indent
, "", s
);
3420 s
= flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS
, message
->flags
);
3421 g_string_append_printf (str
, "%*sFlags: %s\n", indent
, "", s
);
3423 g_string_append_printf (str
, "%*sVersion: %d\n", indent
, "", message
->major_protocol_version
);
3424 g_string_append_printf (str
, "%*sSerial: %d\n", indent
, "", message
->serial
);
3426 g_string_append_printf (str
, "%*sHeaders:\n", indent
, "");
3427 keys
= g_hash_table_get_keys (message
->headers
);
3428 keys
= g_list_sort (keys
, _sort_keys_func
);
3431 for (l
= keys
; l
!= NULL
; l
= l
->next
)
3433 gint key
= GPOINTER_TO_INT (l
->data
);
3437 value
= g_hash_table_lookup (message
->headers
, l
->data
);
3438 g_assert (value
!= NULL
);
3440 s
= _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD
, key
);
3441 value_str
= g_variant_print (value
, TRUE
);
3442 g_string_append_printf (str
, "%*s %s -> %s\n", indent
, "", s
, value_str
);
3449 g_string_append_printf (str
, "%*s (none)\n", indent
, "");
3452 g_string_append_printf (str
, "%*sBody: ", indent
, "");
3453 if (message
->body
!= NULL
)
3455 g_variant_print_string (message
->body
,
3461 g_string_append (str
, "()");
3463 g_string_append (str
, "\n");
3465 g_string_append_printf (str
, "%*sUNIX File Descriptors:\n", indent
, "");
3466 if (message
->fd_list
!= NULL
)
3472 fds
= g_unix_fd_list_peek_fds (message
->fd_list
, &num_fds
);
3475 for (n
= 0; n
< num_fds
; n
++)
3478 struct stat statbuf
;
3479 fs
= g_string_new (NULL
);
3480 if (fstat (fds
[n
], &statbuf
) == 0)
3482 #ifndef MAJOR_MINOR_NOT_FOUND
3483 g_string_append_printf (fs
, "%s" "dev=%d:%d", fs
->len
> 0 ? "," : "",
3484 (gint
) major (statbuf
.st_dev
), (gint
) minor (statbuf
.st_dev
));
3486 g_string_append_printf (fs
, "%s" "mode=0%o", fs
->len
> 0 ? "," : "",
3487 (guint
) statbuf
.st_mode
);
3488 g_string_append_printf (fs
, "%s" "ino=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3489 (guint64
) statbuf
.st_ino
);
3490 g_string_append_printf (fs
, "%s" "uid=%u", fs
->len
> 0 ? "," : "",
3491 (guint
) statbuf
.st_uid
);
3492 g_string_append_printf (fs
, "%s" "gid=%u", fs
->len
> 0 ? "," : "",
3493 (guint
) statbuf
.st_gid
);
3494 #ifndef MAJOR_MINOR_NOT_FOUND
3495 g_string_append_printf (fs
, "%s" "rdev=%d:%d", fs
->len
> 0 ? "," : "",
3496 (gint
) major (statbuf
.st_rdev
), (gint
) minor (statbuf
.st_rdev
));
3498 g_string_append_printf (fs
, "%s" "size=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3499 (guint64
) statbuf
.st_size
);
3500 g_string_append_printf (fs
, "%s" "atime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3501 (guint64
) statbuf
.st_atime
);
3502 g_string_append_printf (fs
, "%s" "mtime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3503 (guint64
) statbuf
.st_mtime
);
3504 g_string_append_printf (fs
, "%s" "ctime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3505 (guint64
) statbuf
.st_ctime
);
3510 g_string_append_printf (fs
, "(fstat failed: %s)", g_strerror (errsv
));
3512 g_string_append_printf (str
, "%*s fd %d: %s\n", indent
, "", fds
[n
], fs
->str
);
3513 g_string_free (fs
, TRUE
);
3518 g_string_append_printf (str
, "%*s (empty)\n", indent
, "");
3523 g_string_append_printf (str
, "%*s (none)\n", indent
, "");
3527 return g_string_free (str
, FALSE
);
3531 * g_dbus_message_get_locked:
3532 * @message: A #GDBusMessage.
3534 * Checks whether @message is locked. To monitor changes to this
3535 * value, conncet to the #GObject::notify signal to listen for changes
3536 * on the #GDBusMessage:locked property.
3538 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3543 g_dbus_message_get_locked (GDBusMessage
*message
)
3545 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
3546 return message
->locked
;
3550 * g_dbus_message_lock:
3551 * @message: A #GDBusMessage.
3553 * If @message is locked, does nothing. Otherwise locks the message.
3558 g_dbus_message_lock (GDBusMessage
*message
)
3560 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3562 if (message
->locked
)
3565 message
->locked
= TRUE
;
3566 g_object_notify (G_OBJECT (message
), "locked");
3573 * g_dbus_message_copy:
3574 * @message: A #GDBusMessage.
3575 * @error: Return location for error or %NULL.
3577 * Copies @message. The copy is a deep copy and the returned
3578 * #GDBusMessage is completely identical except that it is guaranteed
3581 * This operation can fail if e.g. @message contains file descriptors
3582 * and the per-process or system-wide open files limit is reached.
3584 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3585 * Free with g_object_unref().
3590 g_dbus_message_copy (GDBusMessage
*message
,
3594 GHashTableIter iter
;
3595 gpointer header_key
;
3596 GVariant
*header_value
;
3598 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3599 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
3601 ret
= g_dbus_message_new ();
3602 ret
->type
= message
->type
;
3603 ret
->flags
= message
->flags
;
3604 ret
->byte_order
= message
->byte_order
;
3605 ret
->major_protocol_version
= message
->major_protocol_version
;
3606 ret
->serial
= message
->serial
;
3609 if (message
->fd_list
!= NULL
)
3615 ret
->fd_list
= g_unix_fd_list_new ();
3616 fds
= g_unix_fd_list_peek_fds (message
->fd_list
, &num_fds
);
3617 for (n
= 0; n
< num_fds
; n
++)
3619 if (g_unix_fd_list_append (ret
->fd_list
,
3623 g_object_unref (ret
);
3631 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3632 * to just ref (as opposed to deep-copying) the GVariant instances
3634 ret
->body
= message
->body
!= NULL
? g_variant_ref (message
->body
) : NULL
;
3635 g_hash_table_iter_init (&iter
, message
->headers
);
3636 while (g_hash_table_iter_next (&iter
, &header_key
, (gpointer
) &header_value
))
3637 g_hash_table_insert (ret
->headers
, header_key
, g_variant_ref (header_value
));