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
;
1444 const gchar
*type_string
;
1446 type_string
= g_variant_type_peek_string (type
);
1448 #ifdef DEBUG_SERIALIZER
1451 s
= g_variant_type_dup_string (type
);
1452 g_print ("%*s%s type %s from offset 0x%04x",
1454 just_align
? "Aligning" : "Reading",
1459 #endif /* DEBUG_SERIALIZER */
1465 switch (type_string
[0])
1467 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1468 ensure_input_padding (buf
, 4);
1472 v
= g_memory_buffer_read_uint32 (buf
);
1473 ret
= g_variant_new_boolean (v
);
1477 case 'y': /* G_VARIANT_TYPE_BYTE */
1481 v
= g_memory_buffer_read_byte (buf
);
1482 ret
= g_variant_new_byte (v
);
1486 case 'n': /* G_VARIANT_TYPE_INT16 */
1487 ensure_input_padding (buf
, 2);
1491 v
= g_memory_buffer_read_int16 (buf
);
1492 ret
= g_variant_new_int16 (v
);
1496 case 'q': /* G_VARIANT_TYPE_UINT16 */
1497 ensure_input_padding (buf
, 2);
1501 v
= g_memory_buffer_read_uint16 (buf
);
1502 ret
= g_variant_new_uint16 (v
);
1506 case 'i': /* G_VARIANT_TYPE_INT32 */
1507 ensure_input_padding (buf
, 4);
1511 v
= g_memory_buffer_read_int32 (buf
);
1512 ret
= g_variant_new_int32 (v
);
1516 case 'u': /* G_VARIANT_TYPE_UINT32 */
1517 ensure_input_padding (buf
, 4);
1521 v
= g_memory_buffer_read_uint32 (buf
);
1522 ret
= g_variant_new_uint32 (v
);
1526 case 'x': /* G_VARIANT_TYPE_INT64 */
1527 ensure_input_padding (buf
, 8);
1531 v
= g_memory_buffer_read_int64 (buf
);
1532 ret
= g_variant_new_int64 (v
);
1536 case 't': /* G_VARIANT_TYPE_UINT64 */
1537 ensure_input_padding (buf
, 8);
1541 v
= g_memory_buffer_read_uint64 (buf
);
1542 ret
= g_variant_new_uint64 (v
);
1546 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1547 ensure_input_padding (buf
, 8);
1554 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
1555 u
.v_uint64
= g_memory_buffer_read_uint64 (buf
);
1556 ret
= g_variant_new_double (u
.v_double
);
1560 case 's': /* G_VARIANT_TYPE_STRING */
1561 ensure_input_padding (buf
, 4);
1566 len
= g_memory_buffer_read_uint32 (buf
);
1567 v
= read_string (buf
, (gsize
) len
, &local_error
);
1570 ret
= g_variant_new_string (v
);
1574 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1575 ensure_input_padding (buf
, 4);
1580 len
= g_memory_buffer_read_uint32 (buf
);
1581 v
= read_string (buf
, (gsize
) len
, &local_error
);
1584 if (!g_variant_is_object_path (v
))
1586 g_set_error (&local_error
,
1588 G_IO_ERROR_INVALID_ARGUMENT
,
1589 _("Parsed value “%s” is not a valid D-Bus object path"),
1593 ret
= g_variant_new_object_path (v
);
1597 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1602 len
= g_memory_buffer_read_byte (buf
);
1603 v
= read_string (buf
, (gsize
) len
, &local_error
);
1606 if (!g_variant_is_signature (v
))
1608 g_set_error (&local_error
,
1610 G_IO_ERROR_INVALID_ARGUMENT
,
1611 _("Parsed value “%s” is not a valid D-Bus signature"),
1615 ret
= g_variant_new_signature (v
);
1619 case 'h': /* G_VARIANT_TYPE_HANDLE */
1620 ensure_input_padding (buf
, 4);
1624 v
= g_memory_buffer_read_int32 (buf
);
1625 ret
= g_variant_new_handle (v
);
1629 case 'a': /* G_VARIANT_TYPE_ARRAY */
1630 ensure_input_padding (buf
, 4);
1632 /* If we are only aligning for this array type, it is the child type of
1633 * another array, which is empty. So, we do not need to add padding for
1634 * this nonexistent array's elements: we only need to align for this
1635 * array itself (4 bytes). See
1636 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1641 const GVariantType
*element_type
;
1644 array_len
= g_memory_buffer_read_uint32 (buf
);
1647 #ifdef DEBUG_SERIALIZER
1648 g_print (": array spans 0x%04x bytes\n", array_len
);
1649 #endif /* DEBUG_SERIALIZER */
1651 if (array_len
> (2<<26))
1653 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1654 g_set_error (&local_error
,
1656 G_IO_ERROR_INVALID_ARGUMENT
,
1657 g_dngettext (GETTEXT_PACKAGE
,
1658 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1659 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1665 element_type
= g_variant_type_element (type
);
1666 fixed_size
= get_type_fixed_size (element_type
);
1668 /* Fast-path the cases like 'ay', etc. */
1669 if (fixed_size
!= 0)
1671 gconstpointer array_data
;
1673 if (array_len
% fixed_size
!= 0)
1675 g_set_error (&local_error
,
1677 G_IO_ERROR_INVALID_ARGUMENT
,
1678 _("Encountered array of type “a%c”, expected to have a length a multiple "
1679 "of %u bytes, but found to be %u bytes in length"),
1680 g_variant_type_peek_string (element_type
)[0], fixed_size
, array_len
);
1684 ensure_input_padding (buf
, fixed_size
);
1685 array_data
= read_bytes (buf
, array_len
, &local_error
);
1686 if (array_data
== NULL
)
1689 ret
= g_variant_new_fixed_array (element_type
, array_data
, array_len
/ fixed_size
, fixed_size
);
1691 if (g_memory_buffer_is_byteswapped (buf
))
1693 GVariant
*tmp
= g_variant_ref_sink (ret
);
1694 ret
= g_variant_byteswap (tmp
);
1695 g_variant_unref (tmp
);
1700 GVariantBuilder builder
;
1704 g_variant_builder_init (&builder
, type
);
1709 item
= parse_value_from_blob (buf
,
1714 g_assert (item
== NULL
);
1719 target
= offset
+ array_len
;
1720 while (offset
< target
)
1723 item
= parse_value_from_blob (buf
,
1730 g_variant_builder_clear (&builder
);
1733 g_variant_builder_add_value (&builder
, item
);
1734 g_variant_unref (item
);
1739 ret
= g_variant_builder_end (&builder
);
1745 if (g_variant_type_is_dict_entry (type
))
1747 const GVariantType
*key_type
;
1748 const GVariantType
*value_type
;
1752 ensure_input_padding (buf
, 8);
1755 #ifdef DEBUG_SERIALIZER
1757 #endif /* DEBUG_SERIALIZER */
1761 key_type
= g_variant_type_key (type
);
1762 key
= parse_value_from_blob (buf
,
1769 value_type
= g_variant_type_value (type
);
1770 value
= parse_value_from_blob (buf
,
1777 g_variant_unref (key
);
1780 ret
= g_variant_new_dict_entry (key
, value
);
1781 g_variant_unref (key
);
1782 g_variant_unref (value
);
1785 else if (g_variant_type_is_tuple (type
))
1787 ensure_input_padding (buf
, 8);
1790 #ifdef DEBUG_SERIALIZER
1792 #endif /* DEBUG_SERIALIZER */
1796 const GVariantType
*element_type
;
1797 GVariantBuilder builder
;
1799 g_variant_builder_init (&builder
, type
);
1800 element_type
= g_variant_type_first (type
);
1801 while (element_type
!= NULL
)
1804 item
= parse_value_from_blob (buf
,
1811 g_variant_builder_clear (&builder
);
1814 g_variant_builder_add_value (&builder
, item
);
1815 g_variant_unref (item
);
1817 element_type
= g_variant_type_next (element_type
);
1819 ret
= g_variant_builder_end (&builder
);
1822 else if (g_variant_type_is_variant (type
))
1825 #ifdef DEBUG_SERIALIZER
1827 #endif /* DEBUG_SERIALIZER */
1833 GVariantType
*variant_type
;
1836 siglen
= g_memory_buffer_read_byte (buf
);
1837 sig
= read_string (buf
, (gsize
) siglen
, &local_error
);
1840 if (!g_variant_is_signature (sig
))
1842 g_set_error (&local_error
,
1844 G_IO_ERROR_INVALID_ARGUMENT
,
1845 _("Parsed value “%s” for variant is not a valid D-Bus signature"),
1849 variant_type
= g_variant_type_new (sig
);
1850 value
= parse_value_from_blob (buf
,
1855 g_variant_type_free (variant_type
);
1858 ret
= g_variant_new_variant (value
);
1859 g_variant_unref (value
);
1865 s
= g_variant_type_dup_string (type
);
1866 g_set_error (&local_error
,
1868 G_IO_ERROR_INVALID_ARGUMENT
,
1869 _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"),
1877 g_assert ((just_align
&& ret
== NULL
) || (!just_align
&& ret
!= NULL
));
1879 #ifdef DEBUG_SERIALIZER
1885 if (g_variant_type_equal (type
, G_VARIANT_TYPE_BYTE
))
1887 s
= g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret
), g_variant_get_byte (ret
));
1891 s
= g_variant_print (ret
, FALSE
);
1893 g_print (": %s\n", s
);
1898 is_leaf
= is_leaf
; /* To avoid -Wunused-but-set-variable */
1899 #endif /* DEBUG_SERIALIZER */
1901 /* sink the reference, if floating */
1903 g_variant_take_ref (ret
);
1907 #ifdef DEBUG_SERIALIZER
1909 "%*sFAILURE: %s (%s, %d)\n",
1911 local_error
->message
,
1912 g_quark_to_string (local_error
->domain
),
1914 #endif /* DEBUG_SERIALIZER */
1915 g_propagate_error (error
, local_error
);
1919 /* ---------------------------------------------------------------------------------------------------- */
1921 /* message_header must be at least 16 bytes */
1924 * g_dbus_message_bytes_needed:
1925 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1926 * @blob_len: The length of @blob (must be at least 16).
1927 * @error: Return location for error or %NULL.
1929 * Utility function to calculate how many bytes are needed to
1930 * completely deserialize the D-Bus message stored at @blob.
1932 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1933 * @blob contains invalid data or not enough data is available to
1934 * determine the size).
1939 g_dbus_message_bytes_needed (guchar
*blob
,
1947 g_return_val_if_fail (blob
!= NULL
, -1);
1948 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, -1);
1949 g_return_val_if_fail (blob_len
>= 16, -1);
1953 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1954 ret
= 12 + 4 + GUINT32_FROM_LE (((guint32
*) blob
)[3]);
1955 /* round up so it's a multiple of 8 */
1956 ret
= 8 * ((ret
+ 7)/8);
1957 /* finally add the body size */
1958 ret
+= GUINT32_FROM_LE (((guint32
*) blob
)[1]);
1960 else if (blob
[0] == 'B')
1962 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1963 ret
= 12 + 4 + GUINT32_FROM_BE (((guint32
*) blob
)[3]);
1964 /* round up so it's a multiple of 8 */
1965 ret
= 8 * ((ret
+ 7)/8);
1966 /* finally add the body size */
1967 ret
+= GUINT32_FROM_BE (((guint32
*) blob
)[1]);
1973 G_IO_ERROR_INVALID_ARGUMENT
,
1974 "Unable to determine message blob length - given blob is malformed");
1981 G_IO_ERROR_INVALID_ARGUMENT
,
1982 "Blob indicates that message exceeds maximum message length (128MiB)");
1989 /* ---------------------------------------------------------------------------------------------------- */
1992 * g_dbus_message_new_from_blob:
1993 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1994 * @blob_len: The length of @blob.
1995 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1996 * @error: Return location for error or %NULL.
1998 * Creates a new #GDBusMessage from the data stored at @blob. The byte
1999 * order that the message was in can be retrieved using
2000 * g_dbus_message_get_byte_order().
2002 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
2008 g_dbus_message_new_from_blob (guchar
*blob
,
2010 GDBusCapabilityFlags capabilities
,
2015 GDBusMessage
*message
;
2017 guchar major_protocol_version
;
2018 guint32 message_body_len
;
2022 GVariant
*signature
;
2024 /* TODO: check against @capabilities */
2028 g_return_val_if_fail (blob
!= NULL
, NULL
);
2029 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2030 g_return_val_if_fail (blob_len
>= 12, NULL
);
2032 message
= g_dbus_message_new ();
2034 memset (&mbuf
, 0, sizeof (mbuf
));
2035 mbuf
.data
= (gchar
*)blob
;
2036 mbuf
.len
= mbuf
.valid_len
= blob_len
;
2038 endianness
= g_memory_buffer_read_byte (&mbuf
);
2042 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
2043 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
;
2046 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
2047 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
;
2052 G_IO_ERROR_INVALID_ARGUMENT
,
2053 _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"),
2058 message
->type
= g_memory_buffer_read_byte (&mbuf
);
2059 message
->flags
= g_memory_buffer_read_byte (&mbuf
);
2060 major_protocol_version
= g_memory_buffer_read_byte (&mbuf
);
2061 if (major_protocol_version
!= 1)
2065 G_IO_ERROR_INVALID_ARGUMENT
,
2066 _("Invalid major protocol version. Expected 1 but found %d"),
2067 major_protocol_version
);
2070 message_body_len
= g_memory_buffer_read_uint32 (&mbuf
);
2071 message
->serial
= g_memory_buffer_read_uint32 (&mbuf
);
2073 #ifdef DEBUG_SERIALIZER
2074 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2077 s
= _g_dbus_hexdump ((const gchar
*) blob
, blob_len
, 2);
2078 g_print ("%s\n", s
);
2081 #endif /* DEBUG_SERIALIZER */
2083 #ifdef DEBUG_SERIALIZER
2084 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2085 #endif /* DEBUG_SERIALIZER */
2086 headers
= parse_value_from_blob (&mbuf
,
2087 G_VARIANT_TYPE ("a{yv}"),
2091 if (headers
== NULL
)
2093 g_variant_iter_init (&iter
, headers
);
2094 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2096 guchar header_field
;
2098 g_variant_get (item
,
2102 g_dbus_message_set_header (message
, header_field
, value
);
2103 g_variant_unref (value
);
2104 g_variant_unref (item
);
2106 g_variant_unref (headers
);
2108 signature
= g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
2109 if (signature
!= NULL
)
2111 const gchar
*signature_str
;
2112 gsize signature_str_len
;
2114 signature_str
= g_variant_get_string (signature
, &signature_str_len
);
2116 /* signature but no body */
2117 if (message_body_len
== 0 && signature_str_len
> 0)
2121 G_IO_ERROR_INVALID_ARGUMENT
,
2122 _("Signature header with signature “%s” found but message body is empty"),
2126 else if (signature_str_len
> 0)
2128 GVariantType
*variant_type
;
2129 gchar
*tupled_signature_str
;
2131 if (!g_variant_is_signature (signature_str
))
2135 G_IO_ERROR_INVALID_ARGUMENT
,
2136 _("Parsed value “%s” is not a valid D-Bus signature (for body)"),
2140 tupled_signature_str
= g_strdup_printf ("(%s)", signature_str
);
2141 variant_type
= g_variant_type_new (tupled_signature_str
);
2142 g_free (tupled_signature_str
);
2143 #ifdef DEBUG_SERIALIZER
2144 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2145 #endif /* DEBUG_SERIALIZER */
2146 message
->body
= parse_value_from_blob (&mbuf
,
2151 g_variant_type_free (variant_type
);
2152 if (message
->body
== NULL
)
2158 /* no signature, this is only OK if the body is empty */
2159 if (message_body_len
!= 0)
2161 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
2164 G_IO_ERROR_INVALID_ARGUMENT
,
2165 g_dngettext (GETTEXT_PACKAGE
,
2166 "No signature header in message but the message body is %u byte",
2167 "No signature header in message but the message body is %u bytes",
2174 if (!validate_headers (message
, error
))
2176 g_prefix_error (error
, _("Cannot deserialize message: "));
2189 if (message
!= NULL
)
2190 g_object_unref (message
);
2195 /* ---------------------------------------------------------------------------------------------------- */
2198 ensure_output_padding (GMemoryBuffer
*mbuf
,
2202 gsize wanted_offset
;
2203 gsize padding_needed
;
2207 wanted_offset
= ((offset
+ padding_size
- 1) / padding_size
) * padding_size
;
2208 padding_needed
= wanted_offset
- offset
;
2210 for (n
= 0; n
< padding_needed
; n
++)
2211 g_memory_buffer_put_byte (mbuf
, '\0');
2213 return padding_needed
;
2216 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
2218 append_value_to_blob (GVariant
*value
,
2219 const GVariantType
*type
,
2220 GMemoryBuffer
*mbuf
,
2221 gsize
*out_padding_added
,
2224 gsize padding_added
;
2225 const gchar
*type_string
;
2227 type_string
= g_variant_type_peek_string (type
);
2231 switch (type_string
[0])
2233 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
2234 padding_added
= ensure_output_padding (mbuf
, 4);
2237 gboolean v
= g_variant_get_boolean (value
);
2238 g_memory_buffer_put_uint32 (mbuf
, v
);
2242 case 'y': /* G_VARIANT_TYPE_BYTE */
2245 guint8 v
= g_variant_get_byte (value
);
2246 g_memory_buffer_put_byte (mbuf
, v
);
2250 case 'n': /* G_VARIANT_TYPE_INT16 */
2251 padding_added
= ensure_output_padding (mbuf
, 2);
2254 gint16 v
= g_variant_get_int16 (value
);
2255 g_memory_buffer_put_int16 (mbuf
, v
);
2259 case 'q': /* G_VARIANT_TYPE_UINT16 */
2260 padding_added
= ensure_output_padding (mbuf
, 2);
2263 guint16 v
= g_variant_get_uint16 (value
);
2264 g_memory_buffer_put_uint16 (mbuf
, v
);
2268 case 'i': /* G_VARIANT_TYPE_INT32 */
2269 padding_added
= ensure_output_padding (mbuf
, 4);
2272 gint32 v
= g_variant_get_int32 (value
);
2273 g_memory_buffer_put_int32 (mbuf
, v
);
2277 case 'u': /* G_VARIANT_TYPE_UINT32 */
2278 padding_added
= ensure_output_padding (mbuf
, 4);
2281 guint32 v
= g_variant_get_uint32 (value
);
2282 g_memory_buffer_put_uint32 (mbuf
, v
);
2286 case 'x': /* G_VARIANT_TYPE_INT64 */
2287 padding_added
= ensure_output_padding (mbuf
, 8);
2290 gint64 v
= g_variant_get_int64 (value
);
2291 g_memory_buffer_put_int64 (mbuf
, v
);
2295 case 't': /* G_VARIANT_TYPE_UINT64 */
2296 padding_added
= ensure_output_padding (mbuf
, 8);
2299 guint64 v
= g_variant_get_uint64 (value
);
2300 g_memory_buffer_put_uint64 (mbuf
, v
);
2304 case 'd': /* G_VARIANT_TYPE_DOUBLE */
2305 padding_added
= ensure_output_padding (mbuf
, 8);
2312 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
2313 u
.v_double
= g_variant_get_double (value
);
2314 g_memory_buffer_put_uint64 (mbuf
, u
.v_uint64
);
2318 case 's': /* G_VARIANT_TYPE_STRING */
2319 padding_added
= ensure_output_padding (mbuf
, 4);
2325 v
= g_variant_get_string (value
, &len
);
2326 g_assert (g_utf8_validate (v
, -1, &end
) && (end
== v
+ len
));
2327 g_memory_buffer_put_uint32 (mbuf
, len
);
2328 g_memory_buffer_put_string (mbuf
, v
);
2329 g_memory_buffer_put_byte (mbuf
, '\0');
2333 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
2334 padding_added
= ensure_output_padding (mbuf
, 4);
2338 const gchar
*v
= g_variant_get_string (value
, &len
);
2339 g_assert (g_variant_is_object_path (v
));
2340 g_memory_buffer_put_uint32 (mbuf
, len
);
2341 g_memory_buffer_put_string (mbuf
, v
);
2342 g_memory_buffer_put_byte (mbuf
, '\0');
2346 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2350 const gchar
*v
= g_variant_get_string (value
, &len
);
2351 g_assert (g_variant_is_signature (v
));
2352 g_memory_buffer_put_byte (mbuf
, len
);
2353 g_memory_buffer_put_string (mbuf
, v
);
2354 g_memory_buffer_put_byte (mbuf
, '\0');
2358 case 'h': /* G_VARIANT_TYPE_HANDLE */
2359 padding_added
= ensure_output_padding (mbuf
, 4);
2362 gint32 v
= g_variant_get_handle (value
);
2363 g_memory_buffer_put_int32 (mbuf
, v
);
2367 case 'a': /* G_VARIANT_TYPE_ARRAY */
2369 const GVariantType
*element_type
;
2372 goffset array_len_offset
;
2373 goffset array_payload_begin_offset
;
2378 padding_added
= ensure_output_padding (mbuf
, 4);
2381 /* array length - will be filled in later */
2382 array_len_offset
= mbuf
->valid_len
;
2383 g_memory_buffer_put_uint32 (mbuf
, 0xF00DFACE);
2385 /* From the D-Bus spec:
2387 * "A UINT32 giving the length of the array data in bytes,
2388 * followed by alignment padding to the alignment boundary of
2389 * the array element type, followed by each array element. The
2390 * array length is from the end of the alignment padding to
2391 * the end of the last element, i.e. it does not include the
2392 * padding after the length, or any padding after the last
2395 * Thus, we need to count how much padding the first element
2396 * contributes and subtract that from the array length.
2398 array_payload_begin_offset
= mbuf
->valid_len
;
2400 element_type
= g_variant_type_element (type
);
2401 fixed_size
= get_type_fixed_size (element_type
);
2403 if (g_variant_n_children (value
) == 0)
2405 gsize padding_added_for_item
;
2406 if (!append_value_to_blob (NULL
,
2409 &padding_added_for_item
,
2412 array_payload_begin_offset
+= padding_added_for_item
;
2414 else if (fixed_size
!= 0)
2416 GVariant
*use_value
;
2418 if (g_memory_buffer_is_byteswapped (mbuf
))
2419 use_value
= g_variant_byteswap (value
);
2421 use_value
= g_variant_ref (value
);
2423 array_payload_begin_offset
+= ensure_output_padding (mbuf
, fixed_size
);
2425 array_len
= g_variant_get_size (use_value
);
2426 g_memory_buffer_write (mbuf
, g_variant_get_data (use_value
), array_len
);
2427 g_variant_unref (use_value
);
2433 g_variant_iter_init (&iter
, value
);
2434 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2436 gsize padding_added_for_item
;
2437 if (!append_value_to_blob (item
,
2438 g_variant_get_type (item
),
2440 &padding_added_for_item
,
2443 g_variant_unref (item
);
2446 g_variant_unref (item
);
2449 array_payload_begin_offset
+= padding_added_for_item
;
2455 cur_offset
= mbuf
->valid_len
;
2456 array_len
= cur_offset
- array_payload_begin_offset
;
2457 mbuf
->pos
= array_len_offset
;
2459 g_memory_buffer_put_uint32 (mbuf
, array_len
);
2460 mbuf
->pos
= cur_offset
;
2466 if (g_variant_type_is_dict_entry (type
) || g_variant_type_is_tuple (type
))
2468 padding_added
= ensure_output_padding (mbuf
, 8);
2473 g_variant_iter_init (&iter
, value
);
2474 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2476 if (!append_value_to_blob (item
,
2477 g_variant_get_type (item
),
2482 g_variant_unref (item
);
2485 g_variant_unref (item
);
2489 else if (g_variant_type_is_variant (type
))
2494 const gchar
*signature
;
2495 child
= g_variant_get_child_value (value
, 0);
2496 signature
= g_variant_get_type_string (child
);
2497 g_memory_buffer_put_byte (mbuf
, strlen (signature
));
2498 g_memory_buffer_put_string (mbuf
, signature
);
2499 g_memory_buffer_put_byte (mbuf
, '\0');
2500 if (!append_value_to_blob (child
,
2501 g_variant_get_type (child
),
2506 g_variant_unref (child
);
2509 g_variant_unref (child
);
2516 G_IO_ERROR_INVALID_ARGUMENT
,
2517 _("Error serializing GVariant with type string “%s” to the D-Bus wire format"),
2518 g_variant_get_type_string (value
));
2524 if (out_padding_added
!= NULL
)
2525 *out_padding_added
= padding_added
;
2534 append_body_to_blob (GVariant
*value
,
2535 GMemoryBuffer
*mbuf
,
2541 if (!g_variant_is_of_type (value
, G_VARIANT_TYPE_TUPLE
))
2545 G_IO_ERROR_INVALID_ARGUMENT
,
2546 "Expected a tuple for the body of the GDBusMessage.");
2550 g_variant_iter_init (&iter
, value
);
2551 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2553 if (!append_value_to_blob (item
,
2554 g_variant_get_type (item
),
2559 g_variant_unref (item
);
2562 g_variant_unref (item
);
2570 /* ---------------------------------------------------------------------------------------------------- */
2573 * g_dbus_message_to_blob:
2574 * @message: A #GDBusMessage.
2575 * @out_size: Return location for size of generated blob.
2576 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2577 * @error: Return location for error.
2579 * Serializes @message to a blob. The byte order returned by
2580 * g_dbus_message_get_byte_order() will be used.
2582 * Returns: (array length=out_size) (transfer full): A pointer to a
2583 * valid binary D-Bus message of @out_size bytes generated by @message
2584 * or %NULL if @error is set. Free with g_free().
2589 g_dbus_message_to_blob (GDBusMessage
*message
,
2591 GDBusCapabilityFlags capabilities
,
2597 goffset body_len_offset
;
2598 goffset body_start_offset
;
2600 GVariant
*header_fields
;
2601 GVariantBuilder builder
;
2602 GHashTableIter hash_iter
;
2604 GVariant
*header_value
;
2605 GVariant
*signature
;
2606 const gchar
*signature_str
;
2607 gint num_fds_in_message
;
2608 gint num_fds_according_to_header
;
2610 /* TODO: check against @capabilities */
2614 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2615 g_return_val_if_fail (out_size
!= NULL
, NULL
);
2616 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2618 memset (&mbuf
, 0, sizeof (mbuf
));
2619 mbuf
.len
= MIN_ARRAY_SIZE
;
2620 mbuf
.data
= g_malloc (mbuf
.len
);
2622 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
;
2623 switch (message
->byte_order
)
2625 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
:
2626 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
2628 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
:
2629 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
2634 g_memory_buffer_put_byte (&mbuf
, (guchar
) message
->byte_order
);
2635 g_memory_buffer_put_byte (&mbuf
, message
->type
);
2636 g_memory_buffer_put_byte (&mbuf
, message
->flags
);
2637 g_memory_buffer_put_byte (&mbuf
, 1); /* major protocol version */
2638 body_len_offset
= mbuf
.valid_len
;
2639 /* body length - will be filled in later */
2640 g_memory_buffer_put_uint32 (&mbuf
, 0xF00DFACE);
2641 g_memory_buffer_put_uint32 (&mbuf
, message
->serial
);
2643 num_fds_in_message
= 0;
2645 if (message
->fd_list
!= NULL
)
2646 num_fds_in_message
= g_unix_fd_list_get_length (message
->fd_list
);
2648 num_fds_according_to_header
= g_dbus_message_get_num_unix_fds (message
);
2649 if (num_fds_in_message
!= num_fds_according_to_header
)
2653 G_IO_ERROR_INVALID_ARGUMENT
,
2654 _("Message has %d file descriptors but the header field indicates %d file descriptors"),
2656 num_fds_according_to_header
);
2660 if (!validate_headers (message
, error
))
2662 g_prefix_error (error
, _("Cannot serialize message: "));
2666 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("a{yv}"));
2667 g_hash_table_iter_init (&hash_iter
, message
->headers
);
2668 while (g_hash_table_iter_next (&hash_iter
, &key
, (gpointer
) &header_value
))
2670 g_variant_builder_add (&builder
,
2672 (guchar
) GPOINTER_TO_UINT (key
),
2675 header_fields
= g_variant_builder_end (&builder
);
2677 if (!append_value_to_blob (header_fields
,
2678 g_variant_get_type (header_fields
),
2683 g_variant_unref (header_fields
);
2686 g_variant_unref (header_fields
);
2688 /* header size must be a multiple of 8 */
2689 ensure_output_padding (&mbuf
, 8);
2691 body_start_offset
= mbuf
.valid_len
;
2693 signature
= g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
2694 signature_str
= NULL
;
2695 if (signature
!= NULL
)
2696 signature_str
= g_variant_get_string (signature
, NULL
);
2697 if (message
->body
!= NULL
)
2699 gchar
*tupled_signature_str
;
2700 tupled_signature_str
= g_strdup_printf ("(%s)", signature_str
);
2701 if (signature
== NULL
)
2705 G_IO_ERROR_INVALID_ARGUMENT
,
2706 _("Message body has signature “%s” but there is no signature header"),
2708 g_free (tupled_signature_str
);
2711 else if (g_strcmp0 (tupled_signature_str
, g_variant_get_type_string (message
->body
)) != 0)
2715 G_IO_ERROR_INVALID_ARGUMENT
,
2716 _("Message body has type signature “%s” but signature in the header field is “%s”"),
2717 tupled_signature_str
, g_variant_get_type_string (message
->body
));
2718 g_free (tupled_signature_str
);
2721 g_free (tupled_signature_str
);
2722 if (!append_body_to_blob (message
->body
, &mbuf
, error
))
2727 if (signature
!= NULL
&& strlen (signature_str
) > 0)
2731 G_IO_ERROR_INVALID_ARGUMENT
,
2732 _("Message body is empty but signature in the header field is “(%s)”"),
2738 /* OK, we're done writing the message - set the body length */
2739 size
= mbuf
.valid_len
;
2740 body_size
= size
- body_start_offset
;
2742 mbuf
.pos
= body_len_offset
;
2744 g_memory_buffer_put_uint32 (&mbuf
, body_size
);
2747 ret
= (guchar
*)mbuf
.data
;
2756 /* ---------------------------------------------------------------------------------------------------- */
2759 get_uint32_header (GDBusMessage
*message
,
2760 GDBusMessageHeaderField header_field
)
2766 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2767 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_UINT32
))
2768 ret
= g_variant_get_uint32 (value
);
2773 static const gchar
*
2774 get_string_header (GDBusMessage
*message
,
2775 GDBusMessageHeaderField header_field
)
2781 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2782 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
))
2783 ret
= g_variant_get_string (value
, NULL
);
2788 static const gchar
*
2789 get_object_path_header (GDBusMessage
*message
,
2790 GDBusMessageHeaderField header_field
)
2796 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2797 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_OBJECT_PATH
))
2798 ret
= g_variant_get_string (value
, NULL
);
2803 static const gchar
*
2804 get_signature_header (GDBusMessage
*message
,
2805 GDBusMessageHeaderField header_field
)
2811 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2812 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_SIGNATURE
))
2813 ret
= g_variant_get_string (value
, NULL
);
2818 /* ---------------------------------------------------------------------------------------------------- */
2821 set_uint32_header (GDBusMessage
*message
,
2822 GDBusMessageHeaderField header_field
,
2825 g_dbus_message_set_header (message
,
2827 g_variant_new_uint32 (value
));
2831 set_string_header (GDBusMessage
*message
,
2832 GDBusMessageHeaderField header_field
,
2835 g_dbus_message_set_header (message
,
2837 value
== NULL
? NULL
: g_variant_new_string (value
));
2841 set_object_path_header (GDBusMessage
*message
,
2842 GDBusMessageHeaderField header_field
,
2845 g_dbus_message_set_header (message
,
2847 value
== NULL
? NULL
: g_variant_new_object_path (value
));
2851 set_signature_header (GDBusMessage
*message
,
2852 GDBusMessageHeaderField header_field
,
2855 g_dbus_message_set_header (message
,
2857 value
== NULL
? NULL
: g_variant_new_signature (value
));
2860 /* ---------------------------------------------------------------------------------------------------- */
2863 * g_dbus_message_get_reply_serial:
2864 * @message: A #GDBusMessage.
2866 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2868 * Returns: The value.
2873 g_dbus_message_get_reply_serial (GDBusMessage
*message
)
2875 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
2876 return get_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
);
2880 * g_dbus_message_set_reply_serial:
2881 * @message: A #GDBusMessage.
2882 * @value: The value to set.
2884 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2889 g_dbus_message_set_reply_serial (GDBusMessage
*message
,
2892 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2893 set_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
, value
);
2896 /* ---------------------------------------------------------------------------------------------------- */
2899 * g_dbus_message_get_interface:
2900 * @message: A #GDBusMessage.
2902 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2904 * Returns: The value.
2909 g_dbus_message_get_interface (GDBusMessage
*message
)
2911 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2912 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
);
2916 * g_dbus_message_set_interface:
2917 * @message: A #GDBusMessage.
2918 * @value: The value to set.
2920 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2925 g_dbus_message_set_interface (GDBusMessage
*message
,
2928 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2929 g_return_if_fail (value
== NULL
|| g_dbus_is_interface_name (value
));
2930 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
, value
);
2933 /* ---------------------------------------------------------------------------------------------------- */
2936 * g_dbus_message_get_member:
2937 * @message: A #GDBusMessage.
2939 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2941 * Returns: The value.
2946 g_dbus_message_get_member (GDBusMessage
*message
)
2948 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2949 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
);
2953 * g_dbus_message_set_member:
2954 * @message: A #GDBusMessage.
2955 * @value: The value to set.
2957 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2962 g_dbus_message_set_member (GDBusMessage
*message
,
2965 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2966 g_return_if_fail (value
== NULL
|| g_dbus_is_member_name (value
));
2967 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
, value
);
2970 /* ---------------------------------------------------------------------------------------------------- */
2973 * g_dbus_message_get_path:
2974 * @message: A #GDBusMessage.
2976 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2978 * Returns: The value.
2983 g_dbus_message_get_path (GDBusMessage
*message
)
2985 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2986 return get_object_path_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
);
2990 * g_dbus_message_set_path:
2991 * @message: A #GDBusMessage.
2992 * @value: The value to set.
2994 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2999 g_dbus_message_set_path (GDBusMessage
*message
,
3002 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3003 g_return_if_fail (value
== NULL
|| g_variant_is_object_path (value
));
3004 set_object_path_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
, value
);
3007 /* ---------------------------------------------------------------------------------------------------- */
3010 * g_dbus_message_get_sender:
3011 * @message: A #GDBusMessage.
3013 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3015 * Returns: The value.
3020 g_dbus_message_get_sender (GDBusMessage
*message
)
3022 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3023 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SENDER
);
3027 * g_dbus_message_set_sender:
3028 * @message: A #GDBusMessage.
3029 * @value: The value to set.
3031 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3036 g_dbus_message_set_sender (GDBusMessage
*message
,
3039 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3040 g_return_if_fail (value
== NULL
|| g_dbus_is_name (value
));
3041 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SENDER
, value
);
3044 /* ---------------------------------------------------------------------------------------------------- */
3047 * g_dbus_message_get_destination:
3048 * @message: A #GDBusMessage.
3050 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3052 * Returns: The value.
3057 g_dbus_message_get_destination (GDBusMessage
*message
)
3059 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3060 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
);
3064 * g_dbus_message_set_destination:
3065 * @message: A #GDBusMessage.
3066 * @value: The value to set.
3068 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3073 g_dbus_message_set_destination (GDBusMessage
*message
,
3076 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3077 g_return_if_fail (value
== NULL
|| g_dbus_is_name (value
));
3078 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
, value
);
3081 /* ---------------------------------------------------------------------------------------------------- */
3084 * g_dbus_message_get_error_name:
3085 * @message: A #GDBusMessage.
3087 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3089 * Returns: The value.
3094 g_dbus_message_get_error_name (GDBusMessage
*message
)
3096 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3097 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
);
3101 * g_dbus_message_set_error_name:
3102 * @message: A #GDBusMessage.
3103 * @value: The value to set.
3105 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3110 g_dbus_message_set_error_name (GDBusMessage
*message
,
3113 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3114 g_return_if_fail (value
== NULL
|| g_dbus_is_interface_name (value
));
3115 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
, value
);
3118 /* ---------------------------------------------------------------------------------------------------- */
3121 * g_dbus_message_get_signature:
3122 * @message: A #GDBusMessage.
3124 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3126 * Returns: The value.
3131 g_dbus_message_get_signature (GDBusMessage
*message
)
3134 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3135 ret
= get_signature_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
3142 * g_dbus_message_set_signature:
3143 * @message: A #GDBusMessage.
3144 * @value: The value to set.
3146 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3151 g_dbus_message_set_signature (GDBusMessage
*message
,
3154 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3155 g_return_if_fail (value
== NULL
|| g_variant_is_signature (value
));
3156 set_signature_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
, value
);
3159 /* ---------------------------------------------------------------------------------------------------- */
3162 * g_dbus_message_get_arg0:
3163 * @message: A #GDBusMessage.
3165 * Convenience to get the first item in the body of @message.
3167 * Returns: The string item or %NULL if the first item in the body of
3168 * @message is not a string.
3173 g_dbus_message_get_arg0 (GDBusMessage
*message
)
3177 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3181 if (message
->body
!= NULL
&& g_variant_is_of_type (message
->body
, G_VARIANT_TYPE_TUPLE
))
3184 item
= g_variant_get_child_value (message
->body
, 0);
3185 if (g_variant_is_of_type (item
, G_VARIANT_TYPE_STRING
))
3186 ret
= g_variant_get_string (item
, NULL
);
3187 g_variant_unref (item
);
3193 /* ---------------------------------------------------------------------------------------------------- */
3196 * g_dbus_message_get_num_unix_fds:
3197 * @message: A #GDBusMessage.
3199 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3201 * Returns: The value.
3206 g_dbus_message_get_num_unix_fds (GDBusMessage
*message
)
3208 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
3209 return get_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS
);
3213 * g_dbus_message_set_num_unix_fds:
3214 * @message: A #GDBusMessage.
3215 * @value: The value to set.
3217 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3222 g_dbus_message_set_num_unix_fds (GDBusMessage
*message
,
3225 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3226 set_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS
, value
);
3229 /* ---------------------------------------------------------------------------------------------------- */
3232 * g_dbus_message_to_gerror:
3233 * @message: A #GDBusMessage.
3234 * @error: The #GError to set.
3236 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
3237 * nothing and returns %FALSE.
3239 * Otherwise this method encodes the error in @message as a #GError
3240 * using g_dbus_error_set_dbus_error() using the information in the
3241 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
3242 * well as the first string item in @message's body.
3244 * Returns: %TRUE if @error was set, %FALSE otherwise.
3249 g_dbus_message_to_gerror (GDBusMessage
*message
,
3253 const gchar
*error_name
;
3255 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
3258 if (message
->type
!= G_DBUS_MESSAGE_TYPE_ERROR
)
3261 error_name
= g_dbus_message_get_error_name (message
);
3262 if (error_name
!= NULL
)
3266 body
= g_dbus_message_get_body (message
);
3268 if (body
!= NULL
&& g_variant_is_of_type (body
, G_VARIANT_TYPE ("(s)")))
3270 const gchar
*error_message
;
3271 g_variant_get (body
, "(&s)", &error_message
);
3272 g_dbus_error_set_dbus_error (error
,
3279 /* these two situations are valid, yet pretty rare */
3282 g_dbus_error_set_dbus_error (error
,
3285 _("Error return with body of type “%s”"),
3286 g_variant_get_type_string (body
));
3290 g_dbus_error_set_dbus_error (error
,
3293 _("Error return with empty body"));
3299 /* TOOD: this shouldn't happen - should check this at message serialization
3300 * time and disconnect the peer.
3305 "Error return without error-name header!");
3314 /* ---------------------------------------------------------------------------------------------------- */
3317 flags_to_string (GType flags_type
, guint value
)
3323 klass
= g_type_class_ref (flags_type
);
3324 s
= g_string_new (NULL
);
3325 for (n
= 0; n
< 32; n
++)
3327 if ((value
& (1<<n
)) != 0)
3329 GFlagsValue
*flags_value
;
3330 flags_value
= g_flags_get_first_value (klass
, (1<<n
));
3332 g_string_append_c (s
, ',');
3333 if (flags_value
!= NULL
)
3334 g_string_append (s
, flags_value
->value_nick
);
3336 g_string_append_printf (s
, "unknown (bit %d)", n
);
3340 g_string_append (s
, "none");
3341 g_type_class_unref (klass
);
3342 return g_string_free (s
, FALSE
);
3346 _sort_keys_func (gconstpointer a
,
3352 ia
= GPOINTER_TO_INT (a
);
3353 ib
= GPOINTER_TO_INT (b
);
3359 * g_dbus_message_print:
3360 * @message: A #GDBusMessage.
3361 * @indent: Indentation level.
3363 * Produces a human-readable multi-line description of @message.
3365 * The contents of the description has no ABI guarantees, the contents
3366 * and formatting is subject to change at any time. Typical output
3367 * looks something like this:
3374 * path -> objectpath '/org/gtk/GDBus/TestObject'
3375 * interface -> 'org.gtk.GDBus.TestInterface'
3376 * member -> 'GimmeStdout'
3377 * destination -> ':1.146'
3379 * UNIX File Descriptors:
3384 * Type: method-return
3385 * Flags: no-reply-expected
3389 * reply-serial -> uint32 4
3390 * destination -> ':1.159'
3391 * sender -> ':1.146'
3392 * num-unix-fds -> uint32 1
3394 * UNIX File Descriptors:
3395 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3398 * Returns: A string that should be freed with g_free().
3403 g_dbus_message_print (GDBusMessage
*message
,
3411 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3413 str
= g_string_new (NULL
);
3415 s
= _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE
, message
->type
);
3416 g_string_append_printf (str
, "%*sType: %s\n", indent
, "", s
);
3418 s
= flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS
, message
->flags
);
3419 g_string_append_printf (str
, "%*sFlags: %s\n", indent
, "", s
);
3421 g_string_append_printf (str
, "%*sVersion: %d\n", indent
, "", message
->major_protocol_version
);
3422 g_string_append_printf (str
, "%*sSerial: %d\n", indent
, "", message
->serial
);
3424 g_string_append_printf (str
, "%*sHeaders:\n", indent
, "");
3425 keys
= g_hash_table_get_keys (message
->headers
);
3426 keys
= g_list_sort (keys
, _sort_keys_func
);
3429 for (l
= keys
; l
!= NULL
; l
= l
->next
)
3431 gint key
= GPOINTER_TO_INT (l
->data
);
3435 value
= g_hash_table_lookup (message
->headers
, l
->data
);
3436 g_assert (value
!= NULL
);
3438 s
= _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD
, key
);
3439 value_str
= g_variant_print (value
, TRUE
);
3440 g_string_append_printf (str
, "%*s %s -> %s\n", indent
, "", s
, value_str
);
3447 g_string_append_printf (str
, "%*s (none)\n", indent
, "");
3450 g_string_append_printf (str
, "%*sBody: ", indent
, "");
3451 if (message
->body
!= NULL
)
3453 g_variant_print_string (message
->body
,
3459 g_string_append (str
, "()");
3461 g_string_append (str
, "\n");
3463 g_string_append_printf (str
, "%*sUNIX File Descriptors:\n", indent
, "");
3464 if (message
->fd_list
!= NULL
)
3470 fds
= g_unix_fd_list_peek_fds (message
->fd_list
, &num_fds
);
3473 for (n
= 0; n
< num_fds
; n
++)
3476 struct stat statbuf
;
3477 fs
= g_string_new (NULL
);
3478 if (fstat (fds
[n
], &statbuf
) == 0)
3480 #ifndef MAJOR_MINOR_NOT_FOUND
3481 g_string_append_printf (fs
, "%s" "dev=%d:%d", fs
->len
> 0 ? "," : "",
3482 major (statbuf
.st_dev
), minor (statbuf
.st_dev
));
3484 g_string_append_printf (fs
, "%s" "mode=0%o", fs
->len
> 0 ? "," : "",
3486 g_string_append_printf (fs
, "%s" "ino=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3487 (guint64
) statbuf
.st_ino
);
3488 g_string_append_printf (fs
, "%s" "uid=%u", fs
->len
> 0 ? "," : "",
3489 (guint
) statbuf
.st_uid
);
3490 g_string_append_printf (fs
, "%s" "gid=%u", fs
->len
> 0 ? "," : "",
3491 (guint
) statbuf
.st_gid
);
3492 #ifndef MAJOR_MINOR_NOT_FOUND
3493 g_string_append_printf (fs
, "%s" "rdev=%d:%d", fs
->len
> 0 ? "," : "",
3494 major (statbuf
.st_rdev
), minor (statbuf
.st_rdev
));
3496 g_string_append_printf (fs
, "%s" "size=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3497 (guint64
) statbuf
.st_size
);
3498 g_string_append_printf (fs
, "%s" "atime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3499 (guint64
) statbuf
.st_atime
);
3500 g_string_append_printf (fs
, "%s" "mtime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3501 (guint64
) statbuf
.st_mtime
);
3502 g_string_append_printf (fs
, "%s" "ctime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3503 (guint64
) statbuf
.st_ctime
);
3508 g_string_append_printf (fs
, "(fstat failed: %s)", g_strerror (errsv
));
3510 g_string_append_printf (str
, "%*s fd %d: %s\n", indent
, "", fds
[n
], fs
->str
);
3511 g_string_free (fs
, TRUE
);
3516 g_string_append_printf (str
, "%*s (empty)\n", indent
, "");
3521 g_string_append_printf (str
, "%*s (none)\n", indent
, "");
3525 return g_string_free (str
, FALSE
);
3529 * g_dbus_message_get_locked:
3530 * @message: A #GDBusMessage.
3532 * Checks whether @message is locked. To monitor changes to this
3533 * value, conncet to the #GObject::notify signal to listen for changes
3534 * on the #GDBusMessage:locked property.
3536 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3541 g_dbus_message_get_locked (GDBusMessage
*message
)
3543 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
3544 return message
->locked
;
3548 * g_dbus_message_lock:
3549 * @message: A #GDBusMessage.
3551 * If @message is locked, does nothing. Otherwise locks the message.
3556 g_dbus_message_lock (GDBusMessage
*message
)
3558 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3560 if (message
->locked
)
3563 message
->locked
= TRUE
;
3564 g_object_notify (G_OBJECT (message
), "locked");
3571 * g_dbus_message_copy:
3572 * @message: A #GDBusMessage.
3573 * @error: Return location for error or %NULL.
3575 * Copies @message. The copy is a deep copy and the returned
3576 * #GDBusMessage is completely identical except that it is guaranteed
3579 * This operation can fail if e.g. @message contains file descriptors
3580 * and the per-process or system-wide open files limit is reached.
3582 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3583 * Free with g_object_unref().
3588 g_dbus_message_copy (GDBusMessage
*message
,
3592 GHashTableIter iter
;
3593 gpointer header_key
;
3594 GVariant
*header_value
;
3596 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3597 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
3599 ret
= g_dbus_message_new ();
3600 ret
->type
= message
->type
;
3601 ret
->flags
= message
->flags
;
3602 ret
->byte_order
= message
->byte_order
;
3603 ret
->major_protocol_version
= message
->major_protocol_version
;
3604 ret
->serial
= message
->serial
;
3607 if (message
->fd_list
!= NULL
)
3613 ret
->fd_list
= g_unix_fd_list_new ();
3614 fds
= g_unix_fd_list_peek_fds (message
->fd_list
, &num_fds
);
3615 for (n
= 0; n
< num_fds
; n
++)
3617 if (g_unix_fd_list_append (ret
->fd_list
,
3621 g_object_unref (ret
);
3629 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3630 * to just ref (as opposed to deep-copying) the GVariant instances
3632 ret
->body
= message
->body
!= NULL
? g_variant_ref (message
->body
) : NULL
;
3633 g_hash_table_iter_init (&iter
, message
->headers
);
3634 while (g_hash_table_iter_next (&iter
, &header_key
, (gpointer
) &header_value
))
3635 g_hash_table_insert (ret
->headers
, header_key
, g_variant_ref (header_value
));