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 #include <sys/types.h>
38 #define MAJOR_MINOR_NOT_FOUND 1
41 #include "gdbusutils.h"
42 #include "gdbusmessage.h"
43 #include "gdbuserror.h"
44 #include "gioenumtypes.h"
45 #include "ginputstream.h"
46 #include "gdatainputstream.h"
47 #include "gmemoryinputstream.h"
48 #include "goutputstream.h"
49 #include "gdataoutputstream.h"
50 #include "gmemoryoutputstream.h"
51 #include "gseekable.h"
53 #include "gdbusprivate.h"
56 #include "gunixfdlist.h"
61 typedef struct _GMemoryBuffer GMemoryBuffer
;
68 GDataStreamByteOrder byte_order
;
72 g_memory_buffer_is_byteswapped (GMemoryBuffer
*mbuf
)
74 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
75 return mbuf
->byte_order
== G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
77 return mbuf
->byte_order
== G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
82 g_memory_buffer_read_byte (GMemoryBuffer
*mbuf
)
84 if (mbuf
->pos
>= mbuf
->valid_len
)
86 return mbuf
->data
[mbuf
->pos
++];
90 g_memory_buffer_read_int16 (GMemoryBuffer
*mbuf
)
94 if (mbuf
->pos
> mbuf
->valid_len
- 2)
96 mbuf
->pos
= mbuf
->valid_len
;
100 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 2);
103 if (g_memory_buffer_is_byteswapped (mbuf
))
104 v
= GUINT16_SWAP_LE_BE (v
);
110 g_memory_buffer_read_uint16 (GMemoryBuffer
*mbuf
)
114 if (mbuf
->pos
> mbuf
->valid_len
- 2)
116 mbuf
->pos
= mbuf
->valid_len
;
120 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 2);
123 if (g_memory_buffer_is_byteswapped (mbuf
))
124 v
= GUINT16_SWAP_LE_BE (v
);
130 g_memory_buffer_read_int32 (GMemoryBuffer
*mbuf
)
134 if (mbuf
->pos
> mbuf
->valid_len
- 4)
136 mbuf
->pos
= mbuf
->valid_len
;
140 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 4);
143 if (g_memory_buffer_is_byteswapped (mbuf
))
144 v
= GUINT32_SWAP_LE_BE (v
);
150 g_memory_buffer_read_uint32 (GMemoryBuffer
*mbuf
)
154 if (mbuf
->pos
> mbuf
->valid_len
- 4)
156 mbuf
->pos
= mbuf
->valid_len
;
160 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 4);
163 if (g_memory_buffer_is_byteswapped (mbuf
))
164 v
= GUINT32_SWAP_LE_BE (v
);
170 g_memory_buffer_read_int64 (GMemoryBuffer
*mbuf
)
174 if (mbuf
->pos
> mbuf
->valid_len
- 8)
176 mbuf
->pos
= mbuf
->valid_len
;
180 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 8);
183 if (g_memory_buffer_is_byteswapped (mbuf
))
184 v
= GUINT64_SWAP_LE_BE (v
);
190 g_memory_buffer_read_uint64 (GMemoryBuffer
*mbuf
)
194 if (mbuf
->pos
> mbuf
->valid_len
- 8)
196 mbuf
->pos
= mbuf
->valid_len
;
200 memcpy (&v
, mbuf
->data
+ mbuf
->pos
, 8);
203 if (g_memory_buffer_is_byteswapped (mbuf
))
204 v
= GUINT64_SWAP_LE_BE (v
);
209 #define MIN_ARRAY_SIZE 128
212 g_nearest_pow (gsize num
)
216 while (n
< num
&& n
> 0)
223 array_resize (GMemoryBuffer
*mbuf
,
229 if (mbuf
->len
== size
)
233 data
= g_realloc (mbuf
->data
, size
);
236 memset ((guint8
*)data
+ len
, 0, size
- len
);
241 if (mbuf
->len
< mbuf
->valid_len
)
242 mbuf
->valid_len
= mbuf
->len
;
246 g_memory_buffer_write (GMemoryBuffer
*mbuf
,
256 /* Check for address space overflow, but only if the buffer is resizable.
257 Otherwise we just do a short write and don't worry. */
258 if (mbuf
->pos
+ count
< mbuf
->pos
)
261 if (mbuf
->pos
+ count
> mbuf
->len
)
263 /* At least enought to fit the write, rounded up
264 for greater than linear growth.
265 TODO: This wastes a lot of memory at large buffer sizes.
266 Figure out a more rational allocation strategy. */
267 new_size
= g_nearest_pow (mbuf
->pos
+ count
);
268 /* Check for overflow again. We have checked if
269 pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
274 new_size
= MAX (new_size
, MIN_ARRAY_SIZE
);
275 array_resize (mbuf
, new_size
);
278 dest
= (guint8
*)mbuf
->data
+ mbuf
->pos
;
279 memcpy (dest
, buffer
, count
);
282 if (mbuf
->pos
> mbuf
->valid_len
)
283 mbuf
->valid_len
= mbuf
->pos
;
289 g_memory_buffer_put_byte (GMemoryBuffer
*mbuf
,
292 return g_memory_buffer_write (mbuf
, &data
, 1);
296 g_memory_buffer_put_int16 (GMemoryBuffer
*mbuf
,
299 switch (mbuf
->byte_order
)
301 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
302 data
= GINT16_TO_BE (data
);
304 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
305 data
= GINT16_TO_LE (data
);
307 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
312 return g_memory_buffer_write (mbuf
, &data
, 2);
316 g_memory_buffer_put_uint16 (GMemoryBuffer
*mbuf
,
319 switch (mbuf
->byte_order
)
321 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
322 data
= GUINT16_TO_BE (data
);
324 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
325 data
= GUINT16_TO_LE (data
);
327 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
332 return g_memory_buffer_write (mbuf
, &data
, 2);
336 g_memory_buffer_put_int32 (GMemoryBuffer
*mbuf
,
339 switch (mbuf
->byte_order
)
341 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
342 data
= GINT32_TO_BE (data
);
344 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
345 data
= GINT32_TO_LE (data
);
347 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
352 return g_memory_buffer_write (mbuf
, &data
, 4);
356 g_memory_buffer_put_uint32 (GMemoryBuffer
*mbuf
,
359 switch (mbuf
->byte_order
)
361 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
362 data
= GUINT32_TO_BE (data
);
364 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
365 data
= GUINT32_TO_LE (data
);
367 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
372 return g_memory_buffer_write (mbuf
, &data
, 4);
376 g_memory_buffer_put_int64 (GMemoryBuffer
*mbuf
,
379 switch (mbuf
->byte_order
)
381 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
382 data
= GINT64_TO_BE (data
);
384 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
385 data
= GINT64_TO_LE (data
);
387 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
392 return g_memory_buffer_write (mbuf
, &data
, 8);
396 g_memory_buffer_put_uint64 (GMemoryBuffer
*mbuf
,
399 switch (mbuf
->byte_order
)
401 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
:
402 data
= GUINT64_TO_BE (data
);
404 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
:
405 data
= GUINT64_TO_LE (data
);
407 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
:
412 return g_memory_buffer_write (mbuf
, &data
, 8);
416 g_memory_buffer_put_string (GMemoryBuffer
*mbuf
,
419 g_return_val_if_fail (str
!= NULL
, FALSE
);
421 return g_memory_buffer_write (mbuf
, str
, strlen (str
));
426 * SECTION:gdbusmessage
427 * @short_description: D-Bus Message
428 * @include: gio/gio.h
430 * A type for representing D-Bus messages that can be sent or received
431 * on a #GDBusConnection.
434 typedef struct _GDBusMessageClass GDBusMessageClass
;
439 * Class structure for #GDBusMessage.
443 struct _GDBusMessageClass
446 GObjectClass parent_class
;
452 * The #GDBusMessage structure contains only private data and should
453 * only be accessed using the provided API.
460 GObject parent_instance
;
462 GDBusMessageType type
;
463 GDBusMessageFlags flags
;
465 GDBusMessageByteOrder byte_order
;
466 guchar major_protocol_version
;
471 GUnixFDList
*fd_list
;
481 G_DEFINE_TYPE (GDBusMessage
, g_dbus_message
, G_TYPE_OBJECT
)
484 g_dbus_message_finalize (GObject
*object
)
486 GDBusMessage
*message
= G_DBUS_MESSAGE (object
);
488 if (message
->headers
!= NULL
)
489 g_hash_table_unref (message
->headers
);
490 if (message
->body
!= NULL
)
491 g_variant_unref (message
->body
);
493 if (message
->fd_list
!= NULL
)
494 g_object_unref (message
->fd_list
);
497 if (G_OBJECT_CLASS (g_dbus_message_parent_class
)->finalize
!= NULL
)
498 G_OBJECT_CLASS (g_dbus_message_parent_class
)->finalize (object
);
502 g_dbus_message_get_property (GObject
*object
,
507 GDBusMessage
*message
= G_DBUS_MESSAGE (object
);
512 g_value_set_boolean (value
, g_dbus_message_get_locked (message
));
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
522 g_dbus_message_class_init (GDBusMessageClass
*klass
)
524 GObjectClass
*gobject_class
;
526 gobject_class
= G_OBJECT_CLASS (klass
);
527 gobject_class
->finalize
= g_dbus_message_finalize
;
528 gobject_class
->get_property
= g_dbus_message_get_property
;
531 * GDBusConnection:locked:
533 * A boolean specifying whether the message is locked.
537 g_object_class_install_property (gobject_class
,
539 g_param_spec_boolean ("locked",
541 P_("Whether the message is locked"),
544 G_PARAM_STATIC_NAME
|
545 G_PARAM_STATIC_BLURB
|
546 G_PARAM_STATIC_NICK
));
550 g_dbus_message_init (GDBusMessage
*message
)
552 /* Any D-Bus implementation is supposed to handle both Big and
553 * Little Endian encodings and the Endianness is part of the D-Bus
554 * message - we prefer to use Big Endian (since it's Network Byte
555 * Order and just easier to read for humans) but if the machine is
556 * Little Endian we use that for performance reasons.
558 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
559 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
;
561 /* this could also be G_PDP_ENDIAN */
562 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
;
564 message
->headers
= g_hash_table_new_full (g_direct_hash
,
567 (GDestroyNotify
) g_variant_unref
);
571 * g_dbus_message_new:
573 * Creates a new empty #GDBusMessage.
575 * Returns: A #GDBusMessage. Free with g_object_unref().
580 g_dbus_message_new (void)
582 return g_object_new (G_TYPE_DBUS_MESSAGE
, NULL
);
586 * g_dbus_message_new_method_call:
587 * @name: (nullable): A valid D-Bus name or %NULL.
588 * @path: A valid object path.
589 * @interface_: (nullable): A valid D-Bus interface name or %NULL.
590 * @method: A valid method name.
592 * Creates a new #GDBusMessage for a method call.
594 * Returns: A #GDBusMessage. Free with g_object_unref().
599 g_dbus_message_new_method_call (const gchar
*name
,
601 const gchar
*interface_
,
604 GDBusMessage
*message
;
606 g_return_val_if_fail (name
== NULL
|| g_dbus_is_name (name
), NULL
);
607 g_return_val_if_fail (g_variant_is_object_path (path
), NULL
);
608 g_return_val_if_fail (g_dbus_is_member_name (method
), NULL
);
609 g_return_val_if_fail (interface_
== NULL
|| g_dbus_is_interface_name (interface_
), NULL
);
611 message
= g_dbus_message_new ();
612 message
->type
= G_DBUS_MESSAGE_TYPE_METHOD_CALL
;
615 g_dbus_message_set_destination (message
, name
);
616 g_dbus_message_set_path (message
, path
);
617 g_dbus_message_set_member (message
, method
);
618 if (interface_
!= NULL
)
619 g_dbus_message_set_interface (message
, interface_
);
625 * g_dbus_message_new_signal:
626 * @path: A valid object path.
627 * @interface_: A valid D-Bus interface name.
628 * @signal: A valid signal name.
630 * Creates a new #GDBusMessage for a signal emission.
632 * Returns: A #GDBusMessage. Free with g_object_unref().
637 g_dbus_message_new_signal (const gchar
*path
,
638 const gchar
*interface_
,
641 GDBusMessage
*message
;
643 g_return_val_if_fail (g_variant_is_object_path (path
), NULL
);
644 g_return_val_if_fail (g_dbus_is_member_name (signal
), NULL
);
645 g_return_val_if_fail (g_dbus_is_interface_name (interface_
), NULL
);
647 message
= g_dbus_message_new ();
648 message
->type
= G_DBUS_MESSAGE_TYPE_SIGNAL
;
649 message
->flags
= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
651 g_dbus_message_set_path (message
, path
);
652 g_dbus_message_set_member (message
, signal
);
653 g_dbus_message_set_interface (message
, interface_
);
660 * g_dbus_message_new_method_reply:
661 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
662 * create a reply message to.
664 * Creates a new #GDBusMessage that is a reply to @method_call_message.
666 * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
671 g_dbus_message_new_method_reply (GDBusMessage
*method_call_message
)
673 GDBusMessage
*message
;
676 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message
), NULL
);
677 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
, NULL
);
678 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message
) != 0, NULL
);
680 message
= g_dbus_message_new ();
681 message
->type
= G_DBUS_MESSAGE_TYPE_METHOD_RETURN
;
682 message
->flags
= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
683 /* reply with same endianness */
684 message
->byte_order
= method_call_message
->byte_order
;
686 g_dbus_message_set_reply_serial (message
, g_dbus_message_get_serial (method_call_message
));
687 sender
= g_dbus_message_get_sender (method_call_message
);
689 g_dbus_message_set_destination (message
, sender
);
695 * g_dbus_message_new_method_error:
696 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
697 * create a reply message to.
698 * @error_name: A valid D-Bus error name.
699 * @error_message_format: The D-Bus error message in a printf() format.
700 * @...: Arguments for @error_message_format.
702 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
704 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
709 g_dbus_message_new_method_error (GDBusMessage
*method_call_message
,
710 const gchar
*error_name
,
711 const gchar
*error_message_format
,
717 va_start (var_args
, error_message_format
);
718 ret
= g_dbus_message_new_method_error_valist (method_call_message
,
720 error_message_format
,
728 * g_dbus_message_new_method_error_literal:
729 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
730 * create a reply message to.
731 * @error_name: A valid D-Bus error name.
732 * @error_message: The D-Bus error message.
734 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
736 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
741 g_dbus_message_new_method_error_literal (GDBusMessage
*method_call_message
,
742 const gchar
*error_name
,
743 const gchar
*error_message
)
745 GDBusMessage
*message
;
748 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message
), NULL
);
749 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
, NULL
);
750 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message
) != 0, NULL
);
751 g_return_val_if_fail (g_dbus_is_name (error_name
), NULL
);
752 g_return_val_if_fail (error_message
!= NULL
, NULL
);
754 message
= g_dbus_message_new ();
755 message
->type
= G_DBUS_MESSAGE_TYPE_ERROR
;
756 message
->flags
= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
757 /* reply with same endianness */
758 message
->byte_order
= method_call_message
->byte_order
;
760 g_dbus_message_set_reply_serial (message
, g_dbus_message_get_serial (method_call_message
));
761 g_dbus_message_set_error_name (message
, error_name
);
762 g_dbus_message_set_body (message
, g_variant_new ("(s)", error_message
));
764 sender
= g_dbus_message_get_sender (method_call_message
);
766 g_dbus_message_set_destination (message
, sender
);
772 * g_dbus_message_new_method_error_valist:
773 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
774 * create a reply message to.
775 * @error_name: A valid D-Bus error name.
776 * @error_message_format: The D-Bus error message in a printf() format.
777 * @var_args: Arguments for @error_message_format.
779 * Like g_dbus_message_new_method_error() but intended for language bindings.
781 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
787 g_dbus_message_new_method_error_valist (GDBusMessage
*method_call_message
,
788 const gchar
*error_name
,
789 const gchar
*error_message_format
,
793 gchar
*error_message
;
794 error_message
= g_strdup_vprintf (error_message_format
, var_args
);
795 ret
= g_dbus_message_new_method_error_literal (method_call_message
,
798 g_free (error_message
);
802 /* ---------------------------------------------------------------------------------------------------- */
805 * g_dbus_message_get_byte_order:
806 * @message: A #GDBusMessage.
808 * Gets the byte order of @message.
810 * Returns: The byte order.
812 GDBusMessageByteOrder
813 g_dbus_message_get_byte_order (GDBusMessage
*message
)
815 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), (GDBusMessageByteOrder
) 0);
816 return message
->byte_order
;
820 * g_dbus_message_set_byte_order:
821 * @message: A #GDBusMessage.
822 * @byte_order: The byte order.
824 * Sets the byte order of @message.
827 g_dbus_message_set_byte_order (GDBusMessage
*message
,
828 GDBusMessageByteOrder byte_order
)
830 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
834 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
838 message
->byte_order
= byte_order
;
841 /* ---------------------------------------------------------------------------------------------------- */
843 /* TODO: need GI annotations to specify that any guchar value goes for the type */
846 * g_dbus_message_get_message_type:
847 * @message: A #GDBusMessage.
849 * Gets the type of @message.
851 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
856 g_dbus_message_get_message_type (GDBusMessage
*message
)
858 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), G_DBUS_MESSAGE_TYPE_INVALID
);
859 return message
->type
;
863 * g_dbus_message_set_message_type:
864 * @message: A #GDBusMessage.
865 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
867 * Sets @message to be of @type.
872 g_dbus_message_set_message_type (GDBusMessage
*message
,
873 GDBusMessageType type
)
875 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
876 g_return_if_fail (type
>=0 && type
< 256);
880 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
884 message
->type
= type
;
887 /* ---------------------------------------------------------------------------------------------------- */
889 /* TODO: need GI annotations to specify that any guchar value goes for flags */
892 * g_dbus_message_get_flags:
893 * @message: A #GDBusMessage.
895 * Gets the flags for @message.
897 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
902 g_dbus_message_get_flags (GDBusMessage
*message
)
904 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), G_DBUS_MESSAGE_FLAGS_NONE
);
905 return message
->flags
;
909 * g_dbus_message_set_flags:
910 * @message: A #GDBusMessage.
911 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
912 * enumeration bitwise ORed together).
914 * Sets the flags to set on @message.
919 g_dbus_message_set_flags (GDBusMessage
*message
,
920 GDBusMessageFlags flags
)
922 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
923 g_return_if_fail (flags
>=0 && flags
< 256);
927 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
931 message
->flags
= flags
;
934 /* ---------------------------------------------------------------------------------------------------- */
937 * g_dbus_message_get_serial:
938 * @message: A #GDBusMessage.
940 * Gets the serial for @message.
942 * Returns: A #guint32.
947 g_dbus_message_get_serial (GDBusMessage
*message
)
949 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
950 return message
->serial
;
954 * g_dbus_message_set_serial:
955 * @message: A #GDBusMessage.
956 * @serial: A #guint32.
958 * Sets the serial for @message.
963 g_dbus_message_set_serial (GDBusMessage
*message
,
966 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
970 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
974 message
->serial
= serial
;
977 /* ---------------------------------------------------------------------------------------------------- */
979 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
982 * g_dbus_message_get_header:
983 * @message: A #GDBusMessage.
984 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
986 * Gets a header field on @message.
988 * Returns: A #GVariant with the value if the header was found, %NULL
989 * otherwise. Do not free, it is owned by @message.
994 g_dbus_message_get_header (GDBusMessage
*message
,
995 GDBusMessageHeaderField header_field
)
997 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
998 g_return_val_if_fail (header_field
>=0 && header_field
< 256, NULL
);
999 return g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
1003 * g_dbus_message_set_header:
1004 * @message: A #GDBusMessage.
1005 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1006 * @value: (nullable): A #GVariant to set the header field or %NULL to clear the header field.
1008 * Sets a header field on @message.
1010 * If @value is floating, @message assumes ownership of @value.
1015 g_dbus_message_set_header (GDBusMessage
*message
,
1016 GDBusMessageHeaderField header_field
,
1019 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1020 g_return_if_fail (header_field
>=0 && header_field
< 256);
1022 if (message
->locked
)
1024 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
1030 g_hash_table_remove (message
->headers
, GUINT_TO_POINTER (header_field
));
1034 g_hash_table_insert (message
->headers
, GUINT_TO_POINTER (header_field
), g_variant_ref_sink (value
));
1039 * g_dbus_message_get_header_fields:
1040 * @message: A #GDBusMessage.
1042 * Gets an array of all header fields on @message that are set.
1044 * Returns: (array zero-terminated=1): An array of header fields
1045 * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
1046 * is a #guchar. Free with g_free().
1051 g_dbus_message_get_header_fields (GDBusMessage
*message
)
1059 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
1061 keys
= g_hash_table_get_keys (message
->headers
);
1062 num_keys
= g_list_length (keys
);
1063 ret
= g_new (guchar
, num_keys
+ 1);
1064 for (l
= keys
, n
= 0; l
!= NULL
; l
= l
->next
, n
++)
1065 ret
[n
] = GPOINTER_TO_UINT (l
->data
);
1066 g_assert (n
== num_keys
);
1067 ret
[n
] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID
;
1073 /* ---------------------------------------------------------------------------------------------------- */
1076 * g_dbus_message_get_body:
1077 * @message: A #GDBusMessage.
1079 * Gets the body of a message.
1081 * Returns: (transfer none): A #GVariant or %NULL if the body is
1082 * empty. Do not free, it is owned by @message.
1087 g_dbus_message_get_body (GDBusMessage
*message
)
1089 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
1090 return message
->body
;
1094 * g_dbus_message_set_body:
1095 * @message: A #GDBusMessage.
1096 * @body: Either %NULL or a #GVariant that is a tuple.
1098 * Sets the body @message. As a side-effect the
1099 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
1100 * type string of @body (or cleared if @body is %NULL).
1102 * If @body is floating, @message assumes ownership of @body.
1107 g_dbus_message_set_body (GDBusMessage
*message
,
1110 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1111 g_return_if_fail ((body
== NULL
) || g_variant_is_of_type (body
, G_VARIANT_TYPE_TUPLE
));
1113 if (message
->locked
)
1115 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
1119 if (message
->body
!= NULL
)
1120 g_variant_unref (message
->body
);
1123 message
->body
= NULL
;
1124 g_dbus_message_set_signature (message
, NULL
);
1128 const gchar
*type_string
;
1129 gsize type_string_len
;
1132 message
->body
= g_variant_ref_sink (body
);
1134 type_string
= g_variant_get_type_string (body
);
1135 type_string_len
= strlen (type_string
);
1136 g_assert (type_string_len
>= 2);
1137 signature
= g_strndup (type_string
+ 1, type_string_len
- 2);
1138 g_dbus_message_set_signature (message
, signature
);
1143 /* ---------------------------------------------------------------------------------------------------- */
1147 * g_dbus_message_get_unix_fd_list:
1148 * @message: A #GDBusMessage.
1150 * Gets the UNIX file descriptors associated with @message, if any.
1152 * This method is only available on UNIX.
1154 * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
1155 * associated. Do not free, this object is owned by @message.
1160 g_dbus_message_get_unix_fd_list (GDBusMessage
*message
)
1162 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
1163 return message
->fd_list
;
1167 * g_dbus_message_set_unix_fd_list:
1168 * @message: A #GDBusMessage.
1169 * @fd_list: (nullable): A #GUnixFDList or %NULL.
1171 * Sets the UNIX file descriptors associated with @message. As a
1172 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
1173 * field is set to the number of fds in @fd_list (or cleared if
1174 * @fd_list is %NULL).
1176 * This method is only available on UNIX.
1181 g_dbus_message_set_unix_fd_list (GDBusMessage
*message
,
1182 GUnixFDList
*fd_list
)
1184 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
1185 g_return_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
));
1187 if (message
->locked
)
1189 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC
);
1193 if (message
->fd_list
!= NULL
)
1194 g_object_unref (message
->fd_list
);
1195 if (fd_list
!= NULL
)
1197 message
->fd_list
= g_object_ref (fd_list
);
1198 g_dbus_message_set_num_unix_fds (message
, g_unix_fd_list_get_length (fd_list
));
1202 message
->fd_list
= NULL
;
1203 g_dbus_message_set_num_unix_fds (message
, 0);
1208 /* ---------------------------------------------------------------------------------------------------- */
1211 get_type_fixed_size (const GVariantType
*type
)
1213 /* NB: we do not treat 'b' as fixed-size here because GVariant and
1214 * D-Bus disagree about the size.
1216 switch (*g_variant_type_peek_string (type
))
1222 case 'i': case 'u': case 'h':
1224 case 'x': case 't': case 'd':
1232 validate_headers (GDBusMessage
*message
,
1237 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1238 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1242 switch (message
->type
)
1244 case G_DBUS_MESSAGE_TYPE_INVALID
:
1245 g_set_error_literal (error
,
1247 G_IO_ERROR_INVALID_ARGUMENT
,
1248 _("type is INVALID"));
1252 case G_DBUS_MESSAGE_TYPE_METHOD_CALL
:
1253 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
) == NULL
||
1254 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
) == NULL
)
1256 g_set_error_literal (error
,
1258 G_IO_ERROR_INVALID_ARGUMENT
,
1259 _("METHOD_CALL message: PATH or MEMBER header field is missing"));
1264 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN
:
1265 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
) == NULL
)
1267 g_set_error_literal (error
,
1269 G_IO_ERROR_INVALID_ARGUMENT
,
1270 _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
1275 case G_DBUS_MESSAGE_TYPE_ERROR
:
1276 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
) == NULL
||
1277 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
) == NULL
)
1279 g_set_error_literal (error
,
1281 G_IO_ERROR_INVALID_ARGUMENT
,
1282 _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
1287 case G_DBUS_MESSAGE_TYPE_SIGNAL
:
1288 if (g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
) == NULL
||
1289 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
) == NULL
||
1290 g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
) == NULL
)
1292 g_set_error_literal (error
,
1294 G_IO_ERROR_INVALID_ARGUMENT
,
1295 _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
1298 if (g_strcmp0 (g_dbus_message_get_path (message
), "/org/freedesktop/DBus/Local") == 0)
1300 g_set_error_literal (error
,
1302 G_IO_ERROR_INVALID_ARGUMENT
,
1303 _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
1306 if (g_strcmp0 (g_dbus_message_get_interface (message
), "org.freedesktop.DBus.Local") == 0)
1308 g_set_error_literal (error
,
1310 G_IO_ERROR_INVALID_ARGUMENT
,
1311 _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
1317 /* hitherto unknown type - nothing to check */
1324 g_assert (ret
|| (error
== NULL
|| *error
!= NULL
));
1328 /* ---------------------------------------------------------------------------------------------------- */
1331 ensure_input_padding (GMemoryBuffer
*buf
,
1335 gsize wanted_offset
;
1338 wanted_offset
= ((offset
+ padding_size
- 1) / padding_size
) * padding_size
;
1339 buf
->pos
= wanted_offset
;
1343 static const gchar
*
1344 read_string (GMemoryBuffer
*mbuf
,
1349 const gchar
*end_valid
;
1351 if G_UNLIKELY (mbuf
->pos
+ len
>= mbuf
->valid_len
|| mbuf
->pos
+ len
< mbuf
->pos
)
1353 mbuf
->pos
= mbuf
->valid_len
;
1354 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1357 G_IO_ERROR_INVALID_ARGUMENT
,
1358 g_dngettext (GETTEXT_PACKAGE
,
1359 "Wanted to read %lu byte but only got %lu",
1360 "Wanted to read %lu bytes but only got %lu",
1363 (gulong
)(mbuf
->valid_len
- mbuf
->pos
));
1367 if G_UNLIKELY (mbuf
->data
[mbuf
->pos
+ len
] != '\0')
1369 str
= g_strndup (mbuf
->data
+ mbuf
->pos
, len
);
1372 G_IO_ERROR_INVALID_ARGUMENT
,
1373 _("Expected NUL byte after the string “%s” but found byte %d"),
1374 str
, mbuf
->data
[mbuf
->pos
+ len
]);
1376 mbuf
->pos
+= len
+ 1;
1380 str
= mbuf
->data
+ mbuf
->pos
;
1381 mbuf
->pos
+= len
+ 1;
1383 if G_UNLIKELY (!g_utf8_validate (str
, -1, &end_valid
))
1387 offset
= (gint
) (end_valid
- str
);
1388 valid_str
= g_strndup (str
, offset
);
1391 G_IO_ERROR_INVALID_ARGUMENT
,
1392 _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
1393 "The valid UTF-8 string up until that point was “%s”"),
1404 static gconstpointer
1405 read_bytes (GMemoryBuffer
*mbuf
,
1409 gconstpointer result
;
1411 if G_UNLIKELY (mbuf
->pos
+ len
> mbuf
->valid_len
|| mbuf
->pos
+ len
< mbuf
->pos
)
1413 mbuf
->pos
= mbuf
->valid_len
;
1414 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1417 G_IO_ERROR_INVALID_ARGUMENT
,
1418 g_dngettext (GETTEXT_PACKAGE
,
1419 "Wanted to read %lu byte but only got %lu",
1420 "Wanted to read %lu bytes but only got %lu",
1423 (gulong
)(mbuf
->valid_len
- mbuf
->pos
));
1427 result
= mbuf
->data
+ mbuf
->pos
;
1433 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
1435 /* returns a non-floating GVariant! */
1437 parse_value_from_blob (GMemoryBuffer
*buf
,
1438 const GVariantType
*type
,
1439 gboolean just_align
,
1444 GError
*local_error
;
1445 #ifdef DEBUG_SERIALIZER
1447 #endif /* DEBUG_SERIALIZER */
1448 const gchar
*type_string
;
1450 type_string
= g_variant_type_peek_string (type
);
1452 #ifdef DEBUG_SERIALIZER
1455 s
= g_variant_type_dup_string (type
);
1456 g_print ("%*s%s type %s from offset 0x%04x",
1458 just_align
? "Aligning" : "Reading",
1463 #endif /* DEBUG_SERIALIZER */
1467 #ifdef DEBUG_SERIALIZER
1469 #endif /* DEBUG_SERIALIZER */
1471 switch (type_string
[0])
1473 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1474 ensure_input_padding (buf
, 4);
1478 v
= g_memory_buffer_read_uint32 (buf
);
1479 ret
= g_variant_new_boolean (v
);
1483 case 'y': /* G_VARIANT_TYPE_BYTE */
1487 v
= g_memory_buffer_read_byte (buf
);
1488 ret
= g_variant_new_byte (v
);
1492 case 'n': /* G_VARIANT_TYPE_INT16 */
1493 ensure_input_padding (buf
, 2);
1497 v
= g_memory_buffer_read_int16 (buf
);
1498 ret
= g_variant_new_int16 (v
);
1502 case 'q': /* G_VARIANT_TYPE_UINT16 */
1503 ensure_input_padding (buf
, 2);
1507 v
= g_memory_buffer_read_uint16 (buf
);
1508 ret
= g_variant_new_uint16 (v
);
1512 case 'i': /* G_VARIANT_TYPE_INT32 */
1513 ensure_input_padding (buf
, 4);
1517 v
= g_memory_buffer_read_int32 (buf
);
1518 ret
= g_variant_new_int32 (v
);
1522 case 'u': /* G_VARIANT_TYPE_UINT32 */
1523 ensure_input_padding (buf
, 4);
1527 v
= g_memory_buffer_read_uint32 (buf
);
1528 ret
= g_variant_new_uint32 (v
);
1532 case 'x': /* G_VARIANT_TYPE_INT64 */
1533 ensure_input_padding (buf
, 8);
1537 v
= g_memory_buffer_read_int64 (buf
);
1538 ret
= g_variant_new_int64 (v
);
1542 case 't': /* G_VARIANT_TYPE_UINT64 */
1543 ensure_input_padding (buf
, 8);
1547 v
= g_memory_buffer_read_uint64 (buf
);
1548 ret
= g_variant_new_uint64 (v
);
1552 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1553 ensure_input_padding (buf
, 8);
1560 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
1561 u
.v_uint64
= g_memory_buffer_read_uint64 (buf
);
1562 ret
= g_variant_new_double (u
.v_double
);
1566 case 's': /* G_VARIANT_TYPE_STRING */
1567 ensure_input_padding (buf
, 4);
1572 len
= g_memory_buffer_read_uint32 (buf
);
1573 v
= read_string (buf
, (gsize
) len
, &local_error
);
1576 ret
= g_variant_new_string (v
);
1580 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1581 ensure_input_padding (buf
, 4);
1586 len
= g_memory_buffer_read_uint32 (buf
);
1587 v
= read_string (buf
, (gsize
) len
, &local_error
);
1590 if (!g_variant_is_object_path (v
))
1592 g_set_error (&local_error
,
1594 G_IO_ERROR_INVALID_ARGUMENT
,
1595 _("Parsed value “%s” is not a valid D-Bus object path"),
1599 ret
= g_variant_new_object_path (v
);
1603 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1608 len
= g_memory_buffer_read_byte (buf
);
1609 v
= read_string (buf
, (gsize
) len
, &local_error
);
1612 if (!g_variant_is_signature (v
))
1614 g_set_error (&local_error
,
1616 G_IO_ERROR_INVALID_ARGUMENT
,
1617 _("Parsed value “%s” is not a valid D-Bus signature"),
1621 ret
= g_variant_new_signature (v
);
1625 case 'h': /* G_VARIANT_TYPE_HANDLE */
1626 ensure_input_padding (buf
, 4);
1630 v
= g_memory_buffer_read_int32 (buf
);
1631 ret
= g_variant_new_handle (v
);
1635 case 'a': /* G_VARIANT_TYPE_ARRAY */
1636 ensure_input_padding (buf
, 4);
1638 /* If we are only aligning for this array type, it is the child type of
1639 * another array, which is empty. So, we do not need to add padding for
1640 * this nonexistent array's elements: we only need to align for this
1641 * array itself (4 bytes). See
1642 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1647 const GVariantType
*element_type
;
1650 array_len
= g_memory_buffer_read_uint32 (buf
);
1652 #ifdef DEBUG_SERIALIZER
1654 g_print (": array spans 0x%04x bytes\n", array_len
);
1655 #endif /* DEBUG_SERIALIZER */
1657 if (array_len
> (2<<26))
1659 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1660 g_set_error (&local_error
,
1662 G_IO_ERROR_INVALID_ARGUMENT
,
1663 g_dngettext (GETTEXT_PACKAGE
,
1664 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1665 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1671 element_type
= g_variant_type_element (type
);
1672 fixed_size
= get_type_fixed_size (element_type
);
1674 /* Fast-path the cases like 'ay', etc. */
1675 if (fixed_size
!= 0)
1677 gconstpointer array_data
;
1679 if (array_len
% fixed_size
!= 0)
1681 g_set_error (&local_error
,
1683 G_IO_ERROR_INVALID_ARGUMENT
,
1684 _("Encountered array of type “a%c”, expected to have a length a multiple "
1685 "of %u bytes, but found to be %u bytes in length"),
1686 g_variant_type_peek_string (element_type
)[0], fixed_size
, array_len
);
1690 ensure_input_padding (buf
, fixed_size
);
1691 array_data
= read_bytes (buf
, array_len
, &local_error
);
1692 if (array_data
== NULL
)
1695 ret
= g_variant_new_fixed_array (element_type
, array_data
, array_len
/ fixed_size
, fixed_size
);
1697 if (g_memory_buffer_is_byteswapped (buf
))
1699 GVariant
*tmp
= g_variant_ref_sink (ret
);
1700 ret
= g_variant_byteswap (tmp
);
1701 g_variant_unref (tmp
);
1706 GVariantBuilder builder
;
1710 g_variant_builder_init (&builder
, type
);
1715 item
= parse_value_from_blob (buf
,
1720 g_assert (item
== NULL
);
1725 target
= offset
+ array_len
;
1726 while (offset
< target
)
1729 item
= parse_value_from_blob (buf
,
1736 g_variant_builder_clear (&builder
);
1739 g_variant_builder_add_value (&builder
, item
);
1740 g_variant_unref (item
);
1745 ret
= g_variant_builder_end (&builder
);
1751 if (g_variant_type_is_dict_entry (type
))
1753 const GVariantType
*key_type
;
1754 const GVariantType
*value_type
;
1758 ensure_input_padding (buf
, 8);
1760 #ifdef DEBUG_SERIALIZER
1763 #endif /* DEBUG_SERIALIZER */
1767 key_type
= g_variant_type_key (type
);
1768 key
= parse_value_from_blob (buf
,
1775 value_type
= g_variant_type_value (type
);
1776 value
= parse_value_from_blob (buf
,
1783 g_variant_unref (key
);
1786 ret
= g_variant_new_dict_entry (key
, value
);
1787 g_variant_unref (key
);
1788 g_variant_unref (value
);
1791 else if (g_variant_type_is_tuple (type
))
1793 ensure_input_padding (buf
, 8);
1795 #ifdef DEBUG_SERIALIZER
1798 #endif /* DEBUG_SERIALIZER */
1802 const GVariantType
*element_type
;
1803 GVariantBuilder builder
;
1805 g_variant_builder_init (&builder
, type
);
1806 element_type
= g_variant_type_first (type
);
1807 while (element_type
!= NULL
)
1810 item
= parse_value_from_blob (buf
,
1817 g_variant_builder_clear (&builder
);
1820 g_variant_builder_add_value (&builder
, item
);
1821 g_variant_unref (item
);
1823 element_type
= g_variant_type_next (element_type
);
1825 ret
= g_variant_builder_end (&builder
);
1828 else if (g_variant_type_is_variant (type
))
1830 #ifdef DEBUG_SERIALIZER
1833 #endif /* DEBUG_SERIALIZER */
1839 GVariantType
*variant_type
;
1842 siglen
= g_memory_buffer_read_byte (buf
);
1843 sig
= read_string (buf
, (gsize
) siglen
, &local_error
);
1846 if (!g_variant_is_signature (sig
))
1848 g_set_error (&local_error
,
1850 G_IO_ERROR_INVALID_ARGUMENT
,
1851 _("Parsed value “%s” for variant is not a valid D-Bus signature"),
1855 variant_type
= g_variant_type_new (sig
);
1856 value
= parse_value_from_blob (buf
,
1861 g_variant_type_free (variant_type
);
1864 ret
= g_variant_new_variant (value
);
1865 g_variant_unref (value
);
1871 s
= g_variant_type_dup_string (type
);
1872 g_set_error (&local_error
,
1874 G_IO_ERROR_INVALID_ARGUMENT
,
1875 _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"),
1883 g_assert ((just_align
&& ret
== NULL
) || (!just_align
&& ret
!= NULL
));
1885 #ifdef DEBUG_SERIALIZER
1891 if (g_variant_type_equal (type
, G_VARIANT_TYPE_BYTE
))
1893 s
= g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret
), g_variant_get_byte (ret
));
1897 s
= g_variant_print (ret
, FALSE
);
1899 g_print (": %s\n", s
);
1903 #endif /* DEBUG_SERIALIZER */
1905 /* sink the reference, if floating */
1907 g_variant_take_ref (ret
);
1911 #ifdef DEBUG_SERIALIZER
1913 "%*sFAILURE: %s (%s, %d)\n",
1915 local_error
->message
,
1916 g_quark_to_string (local_error
->domain
),
1918 #endif /* DEBUG_SERIALIZER */
1919 g_propagate_error (error
, local_error
);
1923 /* ---------------------------------------------------------------------------------------------------- */
1925 /* message_header must be at least 16 bytes */
1928 * g_dbus_message_bytes_needed:
1929 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1930 * @blob_len: The length of @blob (must be at least 16).
1931 * @error: Return location for error or %NULL.
1933 * Utility function to calculate how many bytes are needed to
1934 * completely deserialize the D-Bus message stored at @blob.
1936 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1937 * @blob contains invalid data or not enough data is available to
1938 * determine the size).
1943 g_dbus_message_bytes_needed (guchar
*blob
,
1951 g_return_val_if_fail (blob
!= NULL
, -1);
1952 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, -1);
1953 g_return_val_if_fail (blob_len
>= 16, -1);
1957 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1958 ret
= 12 + 4 + GUINT32_FROM_LE (((guint32
*) blob
)[3]);
1959 /* round up so it's a multiple of 8 */
1960 ret
= 8 * ((ret
+ 7)/8);
1961 /* finally add the body size */
1962 ret
+= GUINT32_FROM_LE (((guint32
*) blob
)[1]);
1964 else if (blob
[0] == 'B')
1966 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1967 ret
= 12 + 4 + GUINT32_FROM_BE (((guint32
*) blob
)[3]);
1968 /* round up so it's a multiple of 8 */
1969 ret
= 8 * ((ret
+ 7)/8);
1970 /* finally add the body size */
1971 ret
+= GUINT32_FROM_BE (((guint32
*) blob
)[1]);
1977 G_IO_ERROR_INVALID_ARGUMENT
,
1978 "Unable to determine message blob length - given blob is malformed");
1985 G_IO_ERROR_INVALID_ARGUMENT
,
1986 "Blob indicates that message exceeds maximum message length (128MiB)");
1993 /* ---------------------------------------------------------------------------------------------------- */
1996 * g_dbus_message_new_from_blob:
1997 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1998 * @blob_len: The length of @blob.
1999 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2000 * @error: Return location for error or %NULL.
2002 * Creates a new #GDBusMessage from the data stored at @blob. The byte
2003 * order that the message was in can be retrieved using
2004 * g_dbus_message_get_byte_order().
2006 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
2012 g_dbus_message_new_from_blob (guchar
*blob
,
2014 GDBusCapabilityFlags capabilities
,
2019 GDBusMessage
*message
;
2021 guchar major_protocol_version
;
2022 guint32 message_body_len
;
2026 GVariant
*signature
;
2028 /* TODO: check against @capabilities */
2032 g_return_val_if_fail (blob
!= NULL
, NULL
);
2033 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2034 g_return_val_if_fail (blob_len
>= 12, NULL
);
2036 message
= g_dbus_message_new ();
2038 memset (&mbuf
, 0, sizeof (mbuf
));
2039 mbuf
.data
= (gchar
*)blob
;
2040 mbuf
.len
= mbuf
.valid_len
= blob_len
;
2042 endianness
= g_memory_buffer_read_byte (&mbuf
);
2046 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
2047 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
;
2050 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
2051 message
->byte_order
= G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
;
2056 G_IO_ERROR_INVALID_ARGUMENT
,
2057 _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"),
2062 message
->type
= g_memory_buffer_read_byte (&mbuf
);
2063 message
->flags
= g_memory_buffer_read_byte (&mbuf
);
2064 major_protocol_version
= g_memory_buffer_read_byte (&mbuf
);
2065 if (major_protocol_version
!= 1)
2069 G_IO_ERROR_INVALID_ARGUMENT
,
2070 _("Invalid major protocol version. Expected 1 but found %d"),
2071 major_protocol_version
);
2074 message_body_len
= g_memory_buffer_read_uint32 (&mbuf
);
2075 message
->serial
= g_memory_buffer_read_uint32 (&mbuf
);
2077 #ifdef DEBUG_SERIALIZER
2078 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2081 s
= _g_dbus_hexdump ((const gchar
*) blob
, blob_len
, 2);
2082 g_print ("%s\n", s
);
2085 #endif /* DEBUG_SERIALIZER */
2087 #ifdef DEBUG_SERIALIZER
2088 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2089 #endif /* DEBUG_SERIALIZER */
2090 headers
= parse_value_from_blob (&mbuf
,
2091 G_VARIANT_TYPE ("a{yv}"),
2095 if (headers
== NULL
)
2097 g_variant_iter_init (&iter
, headers
);
2098 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2100 guchar header_field
;
2102 g_variant_get (item
,
2106 g_dbus_message_set_header (message
, header_field
, value
);
2107 g_variant_unref (value
);
2108 g_variant_unref (item
);
2110 g_variant_unref (headers
);
2112 signature
= g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
2113 if (signature
!= NULL
)
2115 const gchar
*signature_str
;
2116 gsize signature_str_len
;
2118 signature_str
= g_variant_get_string (signature
, &signature_str_len
);
2120 /* signature but no body */
2121 if (message_body_len
== 0 && signature_str_len
> 0)
2125 G_IO_ERROR_INVALID_ARGUMENT
,
2126 _("Signature header with signature “%s” found but message body is empty"),
2130 else if (signature_str_len
> 0)
2132 GVariantType
*variant_type
;
2133 gchar
*tupled_signature_str
;
2135 if (!g_variant_is_signature (signature_str
))
2139 G_IO_ERROR_INVALID_ARGUMENT
,
2140 _("Parsed value “%s” is not a valid D-Bus signature (for body)"),
2144 tupled_signature_str
= g_strdup_printf ("(%s)", signature_str
);
2145 variant_type
= g_variant_type_new (tupled_signature_str
);
2146 g_free (tupled_signature_str
);
2147 #ifdef DEBUG_SERIALIZER
2148 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint
) blob_len
);
2149 #endif /* DEBUG_SERIALIZER */
2150 message
->body
= parse_value_from_blob (&mbuf
,
2155 g_variant_type_free (variant_type
);
2156 if (message
->body
== NULL
)
2162 /* no signature, this is only OK if the body is empty */
2163 if (message_body_len
!= 0)
2165 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
2168 G_IO_ERROR_INVALID_ARGUMENT
,
2169 g_dngettext (GETTEXT_PACKAGE
,
2170 "No signature header in message but the message body is %u byte",
2171 "No signature header in message but the message body is %u bytes",
2178 if (!validate_headers (message
, error
))
2180 g_prefix_error (error
, _("Cannot deserialize message: "));
2193 if (message
!= NULL
)
2194 g_object_unref (message
);
2199 /* ---------------------------------------------------------------------------------------------------- */
2202 ensure_output_padding (GMemoryBuffer
*mbuf
,
2206 gsize wanted_offset
;
2207 gsize padding_needed
;
2211 wanted_offset
= ((offset
+ padding_size
- 1) / padding_size
) * padding_size
;
2212 padding_needed
= wanted_offset
- offset
;
2214 for (n
= 0; n
< padding_needed
; n
++)
2215 g_memory_buffer_put_byte (mbuf
, '\0');
2217 return padding_needed
;
2220 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
2222 append_value_to_blob (GVariant
*value
,
2223 const GVariantType
*type
,
2224 GMemoryBuffer
*mbuf
,
2225 gsize
*out_padding_added
,
2228 gsize padding_added
;
2229 const gchar
*type_string
;
2231 type_string
= g_variant_type_peek_string (type
);
2235 switch (type_string
[0])
2237 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
2238 padding_added
= ensure_output_padding (mbuf
, 4);
2241 gboolean v
= g_variant_get_boolean (value
);
2242 g_memory_buffer_put_uint32 (mbuf
, v
);
2246 case 'y': /* G_VARIANT_TYPE_BYTE */
2249 guint8 v
= g_variant_get_byte (value
);
2250 g_memory_buffer_put_byte (mbuf
, v
);
2254 case 'n': /* G_VARIANT_TYPE_INT16 */
2255 padding_added
= ensure_output_padding (mbuf
, 2);
2258 gint16 v
= g_variant_get_int16 (value
);
2259 g_memory_buffer_put_int16 (mbuf
, v
);
2263 case 'q': /* G_VARIANT_TYPE_UINT16 */
2264 padding_added
= ensure_output_padding (mbuf
, 2);
2267 guint16 v
= g_variant_get_uint16 (value
);
2268 g_memory_buffer_put_uint16 (mbuf
, v
);
2272 case 'i': /* G_VARIANT_TYPE_INT32 */
2273 padding_added
= ensure_output_padding (mbuf
, 4);
2276 gint32 v
= g_variant_get_int32 (value
);
2277 g_memory_buffer_put_int32 (mbuf
, v
);
2281 case 'u': /* G_VARIANT_TYPE_UINT32 */
2282 padding_added
= ensure_output_padding (mbuf
, 4);
2285 guint32 v
= g_variant_get_uint32 (value
);
2286 g_memory_buffer_put_uint32 (mbuf
, v
);
2290 case 'x': /* G_VARIANT_TYPE_INT64 */
2291 padding_added
= ensure_output_padding (mbuf
, 8);
2294 gint64 v
= g_variant_get_int64 (value
);
2295 g_memory_buffer_put_int64 (mbuf
, v
);
2299 case 't': /* G_VARIANT_TYPE_UINT64 */
2300 padding_added
= ensure_output_padding (mbuf
, 8);
2303 guint64 v
= g_variant_get_uint64 (value
);
2304 g_memory_buffer_put_uint64 (mbuf
, v
);
2308 case 'd': /* G_VARIANT_TYPE_DOUBLE */
2309 padding_added
= ensure_output_padding (mbuf
, 8);
2316 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
2317 u
.v_double
= g_variant_get_double (value
);
2318 g_memory_buffer_put_uint64 (mbuf
, u
.v_uint64
);
2322 case 's': /* G_VARIANT_TYPE_STRING */
2323 padding_added
= ensure_output_padding (mbuf
, 4);
2329 v
= g_variant_get_string (value
, &len
);
2330 g_assert (g_utf8_validate (v
, -1, &end
) && (end
== v
+ len
));
2331 g_memory_buffer_put_uint32 (mbuf
, len
);
2332 g_memory_buffer_put_string (mbuf
, v
);
2333 g_memory_buffer_put_byte (mbuf
, '\0');
2337 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
2338 padding_added
= ensure_output_padding (mbuf
, 4);
2342 const gchar
*v
= g_variant_get_string (value
, &len
);
2343 g_assert (g_variant_is_object_path (v
));
2344 g_memory_buffer_put_uint32 (mbuf
, len
);
2345 g_memory_buffer_put_string (mbuf
, v
);
2346 g_memory_buffer_put_byte (mbuf
, '\0');
2350 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2354 const gchar
*v
= g_variant_get_string (value
, &len
);
2355 g_assert (g_variant_is_signature (v
));
2356 g_memory_buffer_put_byte (mbuf
, len
);
2357 g_memory_buffer_put_string (mbuf
, v
);
2358 g_memory_buffer_put_byte (mbuf
, '\0');
2362 case 'h': /* G_VARIANT_TYPE_HANDLE */
2363 padding_added
= ensure_output_padding (mbuf
, 4);
2366 gint32 v
= g_variant_get_handle (value
);
2367 g_memory_buffer_put_int32 (mbuf
, v
);
2371 case 'a': /* G_VARIANT_TYPE_ARRAY */
2373 const GVariantType
*element_type
;
2376 goffset array_len_offset
;
2377 goffset array_payload_begin_offset
;
2382 padding_added
= ensure_output_padding (mbuf
, 4);
2385 /* array length - will be filled in later */
2386 array_len_offset
= mbuf
->valid_len
;
2387 g_memory_buffer_put_uint32 (mbuf
, 0xF00DFACE);
2389 /* From the D-Bus spec:
2391 * "A UINT32 giving the length of the array data in bytes,
2392 * followed by alignment padding to the alignment boundary of
2393 * the array element type, followed by each array element. The
2394 * array length is from the end of the alignment padding to
2395 * the end of the last element, i.e. it does not include the
2396 * padding after the length, or any padding after the last
2399 * Thus, we need to count how much padding the first element
2400 * contributes and subtract that from the array length.
2402 array_payload_begin_offset
= mbuf
->valid_len
;
2404 element_type
= g_variant_type_element (type
);
2405 fixed_size
= get_type_fixed_size (element_type
);
2407 if (g_variant_n_children (value
) == 0)
2409 gsize padding_added_for_item
;
2410 if (!append_value_to_blob (NULL
,
2413 &padding_added_for_item
,
2416 array_payload_begin_offset
+= padding_added_for_item
;
2418 else if (fixed_size
!= 0)
2420 GVariant
*use_value
;
2422 if (g_memory_buffer_is_byteswapped (mbuf
))
2423 use_value
= g_variant_byteswap (value
);
2425 use_value
= g_variant_ref (value
);
2427 array_payload_begin_offset
+= ensure_output_padding (mbuf
, fixed_size
);
2429 array_len
= g_variant_get_size (use_value
);
2430 g_memory_buffer_write (mbuf
, g_variant_get_data (use_value
), array_len
);
2431 g_variant_unref (use_value
);
2437 g_variant_iter_init (&iter
, value
);
2438 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2440 gsize padding_added_for_item
;
2441 if (!append_value_to_blob (item
,
2442 g_variant_get_type (item
),
2444 &padding_added_for_item
,
2447 g_variant_unref (item
);
2450 g_variant_unref (item
);
2453 array_payload_begin_offset
+= padding_added_for_item
;
2459 cur_offset
= mbuf
->valid_len
;
2460 array_len
= cur_offset
- array_payload_begin_offset
;
2461 mbuf
->pos
= array_len_offset
;
2463 g_memory_buffer_put_uint32 (mbuf
, array_len
);
2464 mbuf
->pos
= cur_offset
;
2470 if (g_variant_type_is_dict_entry (type
) || g_variant_type_is_tuple (type
))
2472 padding_added
= ensure_output_padding (mbuf
, 8);
2477 g_variant_iter_init (&iter
, value
);
2478 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2480 if (!append_value_to_blob (item
,
2481 g_variant_get_type (item
),
2486 g_variant_unref (item
);
2489 g_variant_unref (item
);
2493 else if (g_variant_type_is_variant (type
))
2498 const gchar
*signature
;
2499 child
= g_variant_get_child_value (value
, 0);
2500 signature
= g_variant_get_type_string (child
);
2501 g_memory_buffer_put_byte (mbuf
, strlen (signature
));
2502 g_memory_buffer_put_string (mbuf
, signature
);
2503 g_memory_buffer_put_byte (mbuf
, '\0');
2504 if (!append_value_to_blob (child
,
2505 g_variant_get_type (child
),
2510 g_variant_unref (child
);
2513 g_variant_unref (child
);
2520 G_IO_ERROR_INVALID_ARGUMENT
,
2521 _("Error serializing GVariant with type string “%s” to the D-Bus wire format"),
2522 g_variant_get_type_string (value
));
2528 if (out_padding_added
!= NULL
)
2529 *out_padding_added
= padding_added
;
2538 append_body_to_blob (GVariant
*value
,
2539 GMemoryBuffer
*mbuf
,
2545 if (!g_variant_is_of_type (value
, G_VARIANT_TYPE_TUPLE
))
2549 G_IO_ERROR_INVALID_ARGUMENT
,
2550 "Expected a tuple for the body of the GDBusMessage.");
2554 g_variant_iter_init (&iter
, value
);
2555 while ((item
= g_variant_iter_next_value (&iter
)) != NULL
)
2557 if (!append_value_to_blob (item
,
2558 g_variant_get_type (item
),
2563 g_variant_unref (item
);
2566 g_variant_unref (item
);
2574 /* ---------------------------------------------------------------------------------------------------- */
2577 * g_dbus_message_to_blob:
2578 * @message: A #GDBusMessage.
2579 * @out_size: Return location for size of generated blob.
2580 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2581 * @error: Return location for error.
2583 * Serializes @message to a blob. The byte order returned by
2584 * g_dbus_message_get_byte_order() will be used.
2586 * Returns: (array length=out_size) (transfer full): A pointer to a
2587 * valid binary D-Bus message of @out_size bytes generated by @message
2588 * or %NULL if @error is set. Free with g_free().
2593 g_dbus_message_to_blob (GDBusMessage
*message
,
2595 GDBusCapabilityFlags capabilities
,
2601 goffset body_len_offset
;
2602 goffset body_start_offset
;
2604 GVariant
*header_fields
;
2605 GVariantBuilder builder
;
2606 GHashTableIter hash_iter
;
2608 GVariant
*header_value
;
2609 GVariant
*signature
;
2610 const gchar
*signature_str
;
2611 gint num_fds_in_message
;
2612 gint num_fds_according_to_header
;
2614 /* TODO: check against @capabilities */
2618 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2619 g_return_val_if_fail (out_size
!= NULL
, NULL
);
2620 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2622 memset (&mbuf
, 0, sizeof (mbuf
));
2623 mbuf
.len
= MIN_ARRAY_SIZE
;
2624 mbuf
.data
= g_malloc (mbuf
.len
);
2626 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
;
2627 switch (message
->byte_order
)
2629 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN
:
2630 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
;
2632 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN
:
2633 mbuf
.byte_order
= G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
2638 g_memory_buffer_put_byte (&mbuf
, (guchar
) message
->byte_order
);
2639 g_memory_buffer_put_byte (&mbuf
, message
->type
);
2640 g_memory_buffer_put_byte (&mbuf
, message
->flags
);
2641 g_memory_buffer_put_byte (&mbuf
, 1); /* major protocol version */
2642 body_len_offset
= mbuf
.valid_len
;
2643 /* body length - will be filled in later */
2644 g_memory_buffer_put_uint32 (&mbuf
, 0xF00DFACE);
2645 g_memory_buffer_put_uint32 (&mbuf
, message
->serial
);
2647 num_fds_in_message
= 0;
2649 if (message
->fd_list
!= NULL
)
2650 num_fds_in_message
= g_unix_fd_list_get_length (message
->fd_list
);
2652 num_fds_according_to_header
= g_dbus_message_get_num_unix_fds (message
);
2653 if (num_fds_in_message
!= num_fds_according_to_header
)
2657 G_IO_ERROR_INVALID_ARGUMENT
,
2658 _("Number of file descriptors in message (%d) differs from header field (%d)"),
2660 num_fds_according_to_header
);
2664 if (!validate_headers (message
, error
))
2666 g_prefix_error (error
, _("Cannot serialize message: "));
2670 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("a{yv}"));
2671 g_hash_table_iter_init (&hash_iter
, message
->headers
);
2672 while (g_hash_table_iter_next (&hash_iter
, &key
, (gpointer
) &header_value
))
2674 g_variant_builder_add (&builder
,
2676 (guchar
) GPOINTER_TO_UINT (key
),
2679 header_fields
= g_variant_builder_end (&builder
);
2681 if (!append_value_to_blob (header_fields
,
2682 g_variant_get_type (header_fields
),
2687 g_variant_unref (header_fields
);
2690 g_variant_unref (header_fields
);
2692 /* header size must be a multiple of 8 */
2693 ensure_output_padding (&mbuf
, 8);
2695 body_start_offset
= mbuf
.valid_len
;
2697 signature
= g_dbus_message_get_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
2698 signature_str
= NULL
;
2699 if (signature
!= NULL
)
2700 signature_str
= g_variant_get_string (signature
, NULL
);
2701 if (message
->body
!= NULL
)
2703 gchar
*tupled_signature_str
;
2704 tupled_signature_str
= g_strdup_printf ("(%s)", signature_str
);
2705 if (signature
== NULL
)
2709 G_IO_ERROR_INVALID_ARGUMENT
,
2710 _("Message body has signature “%s” but there is no signature header"),
2712 g_free (tupled_signature_str
);
2715 else if (g_strcmp0 (tupled_signature_str
, g_variant_get_type_string (message
->body
)) != 0)
2719 G_IO_ERROR_INVALID_ARGUMENT
,
2720 _("Message body has type signature “%s” but signature in the header field is “%s”"),
2721 tupled_signature_str
, g_variant_get_type_string (message
->body
));
2722 g_free (tupled_signature_str
);
2725 g_free (tupled_signature_str
);
2726 if (!append_body_to_blob (message
->body
, &mbuf
, error
))
2731 if (signature
!= NULL
&& strlen (signature_str
) > 0)
2735 G_IO_ERROR_INVALID_ARGUMENT
,
2736 _("Message body is empty but signature in the header field is “(%s)”"),
2742 /* OK, we're done writing the message - set the body length */
2743 size
= mbuf
.valid_len
;
2744 body_size
= size
- body_start_offset
;
2746 mbuf
.pos
= body_len_offset
;
2748 g_memory_buffer_put_uint32 (&mbuf
, body_size
);
2751 ret
= (guchar
*)mbuf
.data
;
2760 /* ---------------------------------------------------------------------------------------------------- */
2763 get_uint32_header (GDBusMessage
*message
,
2764 GDBusMessageHeaderField header_field
)
2770 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2771 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_UINT32
))
2772 ret
= g_variant_get_uint32 (value
);
2777 static const gchar
*
2778 get_string_header (GDBusMessage
*message
,
2779 GDBusMessageHeaderField header_field
)
2785 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2786 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
))
2787 ret
= g_variant_get_string (value
, NULL
);
2792 static const gchar
*
2793 get_object_path_header (GDBusMessage
*message
,
2794 GDBusMessageHeaderField header_field
)
2800 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2801 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_OBJECT_PATH
))
2802 ret
= g_variant_get_string (value
, NULL
);
2807 static const gchar
*
2808 get_signature_header (GDBusMessage
*message
,
2809 GDBusMessageHeaderField header_field
)
2815 value
= g_hash_table_lookup (message
->headers
, GUINT_TO_POINTER (header_field
));
2816 if (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_SIGNATURE
))
2817 ret
= g_variant_get_string (value
, NULL
);
2822 /* ---------------------------------------------------------------------------------------------------- */
2825 set_uint32_header (GDBusMessage
*message
,
2826 GDBusMessageHeaderField header_field
,
2829 g_dbus_message_set_header (message
,
2831 g_variant_new_uint32 (value
));
2835 set_string_header (GDBusMessage
*message
,
2836 GDBusMessageHeaderField header_field
,
2839 g_dbus_message_set_header (message
,
2841 value
== NULL
? NULL
: g_variant_new_string (value
));
2845 set_object_path_header (GDBusMessage
*message
,
2846 GDBusMessageHeaderField header_field
,
2849 g_dbus_message_set_header (message
,
2851 value
== NULL
? NULL
: g_variant_new_object_path (value
));
2855 set_signature_header (GDBusMessage
*message
,
2856 GDBusMessageHeaderField header_field
,
2859 g_dbus_message_set_header (message
,
2861 value
== NULL
? NULL
: g_variant_new_signature (value
));
2864 /* ---------------------------------------------------------------------------------------------------- */
2867 * g_dbus_message_get_reply_serial:
2868 * @message: A #GDBusMessage.
2870 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2872 * Returns: The value.
2877 g_dbus_message_get_reply_serial (GDBusMessage
*message
)
2879 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
2880 return get_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
);
2884 * g_dbus_message_set_reply_serial:
2885 * @message: A #GDBusMessage.
2886 * @value: The value to set.
2888 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2893 g_dbus_message_set_reply_serial (GDBusMessage
*message
,
2896 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2897 set_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL
, value
);
2900 /* ---------------------------------------------------------------------------------------------------- */
2903 * g_dbus_message_get_interface:
2904 * @message: A #GDBusMessage.
2906 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2908 * Returns: The value.
2913 g_dbus_message_get_interface (GDBusMessage
*message
)
2915 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2916 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
);
2920 * g_dbus_message_set_interface:
2921 * @message: A #GDBusMessage.
2922 * @value: The value to set.
2924 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2929 g_dbus_message_set_interface (GDBusMessage
*message
,
2932 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2933 g_return_if_fail (value
== NULL
|| g_dbus_is_interface_name (value
));
2934 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE
, value
);
2937 /* ---------------------------------------------------------------------------------------------------- */
2940 * g_dbus_message_get_member:
2941 * @message: A #GDBusMessage.
2943 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2945 * Returns: The value.
2950 g_dbus_message_get_member (GDBusMessage
*message
)
2952 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2953 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
);
2957 * g_dbus_message_set_member:
2958 * @message: A #GDBusMessage.
2959 * @value: The value to set.
2961 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2966 g_dbus_message_set_member (GDBusMessage
*message
,
2969 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2970 g_return_if_fail (value
== NULL
|| g_dbus_is_member_name (value
));
2971 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER
, value
);
2974 /* ---------------------------------------------------------------------------------------------------- */
2977 * g_dbus_message_get_path:
2978 * @message: A #GDBusMessage.
2980 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2982 * Returns: The value.
2987 g_dbus_message_get_path (GDBusMessage
*message
)
2989 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2990 return get_object_path_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
);
2994 * g_dbus_message_set_path:
2995 * @message: A #GDBusMessage.
2996 * @value: The value to set.
2998 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
3003 g_dbus_message_set_path (GDBusMessage
*message
,
3006 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3007 g_return_if_fail (value
== NULL
|| g_variant_is_object_path (value
));
3008 set_object_path_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_PATH
, value
);
3011 /* ---------------------------------------------------------------------------------------------------- */
3014 * g_dbus_message_get_sender:
3015 * @message: A #GDBusMessage.
3017 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3019 * Returns: The value.
3024 g_dbus_message_get_sender (GDBusMessage
*message
)
3026 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3027 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SENDER
);
3031 * g_dbus_message_set_sender:
3032 * @message: A #GDBusMessage.
3033 * @value: The value to set.
3035 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3040 g_dbus_message_set_sender (GDBusMessage
*message
,
3043 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3044 g_return_if_fail (value
== NULL
|| g_dbus_is_name (value
));
3045 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SENDER
, value
);
3048 /* ---------------------------------------------------------------------------------------------------- */
3051 * g_dbus_message_get_destination:
3052 * @message: A #GDBusMessage.
3054 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3056 * Returns: The value.
3061 g_dbus_message_get_destination (GDBusMessage
*message
)
3063 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3064 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
);
3068 * g_dbus_message_set_destination:
3069 * @message: A #GDBusMessage.
3070 * @value: The value to set.
3072 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3077 g_dbus_message_set_destination (GDBusMessage
*message
,
3080 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3081 g_return_if_fail (value
== NULL
|| g_dbus_is_name (value
));
3082 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
, value
);
3085 /* ---------------------------------------------------------------------------------------------------- */
3088 * g_dbus_message_get_error_name:
3089 * @message: A #GDBusMessage.
3091 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3093 * Returns: The value.
3098 g_dbus_message_get_error_name (GDBusMessage
*message
)
3100 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3101 return get_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
);
3105 * g_dbus_message_set_error_name:
3106 * @message: A #GDBusMessage.
3107 * @value: The value to set.
3109 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3114 g_dbus_message_set_error_name (GDBusMessage
*message
,
3117 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3118 g_return_if_fail (value
== NULL
|| g_dbus_is_interface_name (value
));
3119 set_string_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME
, value
);
3122 /* ---------------------------------------------------------------------------------------------------- */
3125 * g_dbus_message_get_signature:
3126 * @message: A #GDBusMessage.
3128 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3130 * Returns: The value.
3135 g_dbus_message_get_signature (GDBusMessage
*message
)
3138 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3139 ret
= get_signature_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
);
3146 * g_dbus_message_set_signature:
3147 * @message: A #GDBusMessage.
3148 * @value: The value to set.
3150 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3155 g_dbus_message_set_signature (GDBusMessage
*message
,
3158 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3159 g_return_if_fail (value
== NULL
|| g_variant_is_signature (value
));
3160 set_signature_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE
, value
);
3163 /* ---------------------------------------------------------------------------------------------------- */
3166 * g_dbus_message_get_arg0:
3167 * @message: A #GDBusMessage.
3169 * Convenience to get the first item in the body of @message.
3171 * Returns: The string item or %NULL if the first item in the body of
3172 * @message is not a string.
3177 g_dbus_message_get_arg0 (GDBusMessage
*message
)
3181 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3185 if (message
->body
!= NULL
&& g_variant_is_of_type (message
->body
, G_VARIANT_TYPE_TUPLE
))
3188 item
= g_variant_get_child_value (message
->body
, 0);
3189 if (g_variant_is_of_type (item
, G_VARIANT_TYPE_STRING
))
3190 ret
= g_variant_get_string (item
, NULL
);
3191 g_variant_unref (item
);
3197 /* ---------------------------------------------------------------------------------------------------- */
3200 * g_dbus_message_get_num_unix_fds:
3201 * @message: A #GDBusMessage.
3203 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3205 * Returns: The value.
3210 g_dbus_message_get_num_unix_fds (GDBusMessage
*message
)
3212 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), 0);
3213 return get_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS
);
3217 * g_dbus_message_set_num_unix_fds:
3218 * @message: A #GDBusMessage.
3219 * @value: The value to set.
3221 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3226 g_dbus_message_set_num_unix_fds (GDBusMessage
*message
,
3229 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3230 set_uint32_header (message
, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS
, value
);
3233 /* ---------------------------------------------------------------------------------------------------- */
3236 * g_dbus_message_to_gerror:
3237 * @message: A #GDBusMessage.
3238 * @error: The #GError to set.
3240 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
3241 * nothing and returns %FALSE.
3243 * Otherwise this method encodes the error in @message as a #GError
3244 * using g_dbus_error_set_dbus_error() using the information in the
3245 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
3246 * well as the first string item in @message's body.
3248 * Returns: %TRUE if @error was set, %FALSE otherwise.
3253 g_dbus_message_to_gerror (GDBusMessage
*message
,
3257 const gchar
*error_name
;
3259 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
3262 if (message
->type
!= G_DBUS_MESSAGE_TYPE_ERROR
)
3265 error_name
= g_dbus_message_get_error_name (message
);
3266 if (error_name
!= NULL
)
3270 body
= g_dbus_message_get_body (message
);
3272 if (body
!= NULL
&& g_variant_is_of_type (body
, G_VARIANT_TYPE ("(s)")))
3274 const gchar
*error_message
;
3275 g_variant_get (body
, "(&s)", &error_message
);
3276 g_dbus_error_set_dbus_error (error
,
3283 /* these two situations are valid, yet pretty rare */
3286 g_dbus_error_set_dbus_error (error
,
3289 _("Error return with body of type “%s”"),
3290 g_variant_get_type_string (body
));
3294 g_dbus_error_set_dbus_error (error
,
3297 _("Error return with empty body"));
3303 /* TOOD: this shouldn't happen - should check this at message serialization
3304 * time and disconnect the peer.
3309 "Error return without error-name header!");
3318 /* ---------------------------------------------------------------------------------------------------- */
3321 flags_to_string (GType flags_type
, guint value
)
3327 klass
= g_type_class_ref (flags_type
);
3328 s
= g_string_new (NULL
);
3329 for (n
= 0; n
< 32; n
++)
3331 if ((value
& (1<<n
)) != 0)
3333 GFlagsValue
*flags_value
;
3334 flags_value
= g_flags_get_first_value (klass
, (1<<n
));
3336 g_string_append_c (s
, ',');
3337 if (flags_value
!= NULL
)
3338 g_string_append (s
, flags_value
->value_nick
);
3340 g_string_append_printf (s
, "unknown (bit %d)", n
);
3344 g_string_append (s
, "none");
3345 g_type_class_unref (klass
);
3346 return g_string_free (s
, FALSE
);
3350 _sort_keys_func (gconstpointer a
,
3356 ia
= GPOINTER_TO_INT (a
);
3357 ib
= GPOINTER_TO_INT (b
);
3363 * g_dbus_message_print:
3364 * @message: A #GDBusMessage.
3365 * @indent: Indentation level.
3367 * Produces a human-readable multi-line description of @message.
3369 * The contents of the description has no ABI guarantees, the contents
3370 * and formatting is subject to change at any time. Typical output
3371 * looks something like this:
3378 * path -> objectpath '/org/gtk/GDBus/TestObject'
3379 * interface -> 'org.gtk.GDBus.TestInterface'
3380 * member -> 'GimmeStdout'
3381 * destination -> ':1.146'
3383 * UNIX File Descriptors:
3388 * Type: method-return
3389 * Flags: no-reply-expected
3393 * reply-serial -> uint32 4
3394 * destination -> ':1.159'
3395 * sender -> ':1.146'
3396 * num-unix-fds -> uint32 1
3398 * UNIX File Descriptors:
3399 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3402 * Returns: A string that should be freed with g_free().
3407 g_dbus_message_print (GDBusMessage
*message
,
3415 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3417 str
= g_string_new (NULL
);
3419 s
= _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE
, message
->type
);
3420 g_string_append_printf (str
, "%*sType: %s\n", indent
, "", s
);
3422 s
= flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS
, message
->flags
);
3423 g_string_append_printf (str
, "%*sFlags: %s\n", indent
, "", s
);
3425 g_string_append_printf (str
, "%*sVersion: %d\n", indent
, "", message
->major_protocol_version
);
3426 g_string_append_printf (str
, "%*sSerial: %d\n", indent
, "", message
->serial
);
3428 g_string_append_printf (str
, "%*sHeaders:\n", indent
, "");
3429 keys
= g_hash_table_get_keys (message
->headers
);
3430 keys
= g_list_sort (keys
, _sort_keys_func
);
3433 for (l
= keys
; l
!= NULL
; l
= l
->next
)
3435 gint key
= GPOINTER_TO_INT (l
->data
);
3439 value
= g_hash_table_lookup (message
->headers
, l
->data
);
3440 g_assert (value
!= NULL
);
3442 s
= _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD
, key
);
3443 value_str
= g_variant_print (value
, TRUE
);
3444 g_string_append_printf (str
, "%*s %s -> %s\n", indent
, "", s
, value_str
);
3451 g_string_append_printf (str
, "%*s (none)\n", indent
, "");
3454 g_string_append_printf (str
, "%*sBody: ", indent
, "");
3455 if (message
->body
!= NULL
)
3457 g_variant_print_string (message
->body
,
3463 g_string_append (str
, "()");
3465 g_string_append (str
, "\n");
3467 g_string_append_printf (str
, "%*sUNIX File Descriptors:\n", indent
, "");
3468 if (message
->fd_list
!= NULL
)
3474 fds
= g_unix_fd_list_peek_fds (message
->fd_list
, &num_fds
);
3477 for (n
= 0; n
< num_fds
; n
++)
3480 struct stat statbuf
;
3481 fs
= g_string_new (NULL
);
3482 if (fstat (fds
[n
], &statbuf
) == 0)
3484 #ifndef MAJOR_MINOR_NOT_FOUND
3485 g_string_append_printf (fs
, "%s" "dev=%d:%d", fs
->len
> 0 ? "," : "",
3486 (gint
) major (statbuf
.st_dev
), (gint
) minor (statbuf
.st_dev
));
3488 g_string_append_printf (fs
, "%s" "mode=0%o", fs
->len
> 0 ? "," : "",
3489 (guint
) statbuf
.st_mode
);
3490 g_string_append_printf (fs
, "%s" "ino=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3491 (guint64
) statbuf
.st_ino
);
3492 g_string_append_printf (fs
, "%s" "uid=%u", fs
->len
> 0 ? "," : "",
3493 (guint
) statbuf
.st_uid
);
3494 g_string_append_printf (fs
, "%s" "gid=%u", fs
->len
> 0 ? "," : "",
3495 (guint
) statbuf
.st_gid
);
3496 #ifndef MAJOR_MINOR_NOT_FOUND
3497 g_string_append_printf (fs
, "%s" "rdev=%d:%d", fs
->len
> 0 ? "," : "",
3498 (gint
) major (statbuf
.st_rdev
), (gint
) minor (statbuf
.st_rdev
));
3500 g_string_append_printf (fs
, "%s" "size=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3501 (guint64
) statbuf
.st_size
);
3502 g_string_append_printf (fs
, "%s" "atime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3503 (guint64
) statbuf
.st_atime
);
3504 g_string_append_printf (fs
, "%s" "mtime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3505 (guint64
) statbuf
.st_mtime
);
3506 g_string_append_printf (fs
, "%s" "ctime=%" G_GUINT64_FORMAT
, fs
->len
> 0 ? "," : "",
3507 (guint64
) statbuf
.st_ctime
);
3512 g_string_append_printf (fs
, "(fstat failed: %s)", g_strerror (errsv
));
3514 g_string_append_printf (str
, "%*s fd %d: %s\n", indent
, "", fds
[n
], fs
->str
);
3515 g_string_free (fs
, TRUE
);
3520 g_string_append_printf (str
, "%*s (empty)\n", indent
, "");
3525 g_string_append_printf (str
, "%*s (none)\n", indent
, "");
3529 return g_string_free (str
, FALSE
);
3533 * g_dbus_message_get_locked:
3534 * @message: A #GDBusMessage.
3536 * Checks whether @message is locked. To monitor changes to this
3537 * value, conncet to the #GObject::notify signal to listen for changes
3538 * on the #GDBusMessage:locked property.
3540 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3545 g_dbus_message_get_locked (GDBusMessage
*message
)
3547 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
3548 return message
->locked
;
3552 * g_dbus_message_lock:
3553 * @message: A #GDBusMessage.
3555 * If @message is locked, does nothing. Otherwise locks the message.
3560 g_dbus_message_lock (GDBusMessage
*message
)
3562 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
3564 if (message
->locked
)
3567 message
->locked
= TRUE
;
3568 g_object_notify (G_OBJECT (message
), "locked");
3575 * g_dbus_message_copy:
3576 * @message: A #GDBusMessage.
3577 * @error: Return location for error or %NULL.
3579 * Copies @message. The copy is a deep copy and the returned
3580 * #GDBusMessage is completely identical except that it is guaranteed
3583 * This operation can fail if e.g. @message contains file descriptors
3584 * and the per-process or system-wide open files limit is reached.
3586 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3587 * Free with g_object_unref().
3592 g_dbus_message_copy (GDBusMessage
*message
,
3596 GHashTableIter iter
;
3597 gpointer header_key
;
3598 GVariant
*header_value
;
3600 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
3601 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
3603 ret
= g_dbus_message_new ();
3604 ret
->type
= message
->type
;
3605 ret
->flags
= message
->flags
;
3606 ret
->byte_order
= message
->byte_order
;
3607 ret
->major_protocol_version
= message
->major_protocol_version
;
3608 ret
->serial
= message
->serial
;
3611 if (message
->fd_list
!= NULL
)
3617 ret
->fd_list
= g_unix_fd_list_new ();
3618 fds
= g_unix_fd_list_peek_fds (message
->fd_list
, &num_fds
);
3619 for (n
= 0; n
< num_fds
; n
++)
3621 if (g_unix_fd_list_append (ret
->fd_list
,
3625 g_object_unref (ret
);
3633 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3634 * to just ref (as opposed to deep-copying) the GVariant instances
3636 ret
->body
= message
->body
!= NULL
? g_variant_ref (message
->body
) : NULL
;
3637 g_hash_table_iter_init (&iter
, message
->headers
);
3638 while (g_hash_table_iter_next (&iter
, &header_key
, (gpointer
) &header_value
))
3639 g_hash_table_insert (ret
->headers
, header_key
, g_variant_ref (header_value
));