#endif
#include "glibintl.h"
-#include "gioalias.h"
/**
* SECTION:gdbusmessage
* on a #GDBusConnection.
*/
-struct _GDBusMessagePrivate
+typedef struct _GDBusMessageClass GDBusMessageClass;
+
+/**
+ * GDBusMessageClass:
+ *
+ * Class structure for #GDBusMessage.
+ *
+ * Since: 2.26
+ */
+struct _GDBusMessageClass
+{
+ /*< private >*/
+ GObjectClass parent_class;
+};
+
+/**
+ * GDBusMessage:
+ *
+ * The #GDBusMessage structure contains only private data and should
+ * only be accessed using the provided API.
+ *
+ * Since: 2.26
+ */
+struct _GDBusMessage
{
+ /*< private >*/
+ GObject parent_instance;
+
GDBusMessageType type;
GDBusMessageFlags flags;
+ gboolean locked;
+ GDBusMessageByteOrder byte_order;
guchar major_protocol_version;
guint32 serial;
GHashTable *headers;
#endif
};
+enum
+{
+ PROP_0,
+ PROP_LOCKED
+};
+
G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT);
static void
{
GDBusMessage *message = G_DBUS_MESSAGE (object);
- if (message->priv->headers != NULL)
- g_hash_table_unref (message->priv->headers);
- if (message->priv->body != NULL)
- g_variant_unref (message->priv->body);
+ if (message->headers != NULL)
+ g_hash_table_unref (message->headers);
+ if (message->body != NULL)
+ g_variant_unref (message->body);
#ifdef G_OS_UNIX
- if (message->priv->fd_list != NULL)
- g_object_unref (message->priv->fd_list);
+ if (message->fd_list != NULL)
+ g_object_unref (message->fd_list);
#endif
if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL)
}
static void
+g_dbus_message_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GDBusMessage *message = G_DBUS_MESSAGE (object);
+
+ switch (prop_id)
+ {
+ case PROP_LOCKED:
+ g_value_set_boolean (value, g_dbus_message_get_locked (message));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
g_dbus_message_class_init (GDBusMessageClass *klass)
{
GObjectClass *gobject_class;
- g_type_class_add_private (klass, sizeof (GDBusMessagePrivate));
-
gobject_class = G_OBJECT_CLASS (klass);
-
gobject_class->finalize = g_dbus_message_finalize;
+ gobject_class->get_property = g_dbus_message_get_property;
+
+ /**
+ * GDBusConnection:locked:
+ *
+ * A boolean specifying whether the message is locked.
+ *
+ * Since: 2.26
+ */
+ g_object_class_install_property (gobject_class,
+ PROP_LOCKED,
+ g_param_spec_boolean ("locked",
+ P_("Locked"),
+ P_("Whether the message is locked"),
+ FALSE,
+ G_PARAM_READABLE |
+ G_PARAM_STATIC_NAME |
+ G_PARAM_STATIC_BLURB |
+ G_PARAM_STATIC_NICK));
}
static void
g_dbus_message_init (GDBusMessage *message)
{
- message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message, G_TYPE_DBUS_MESSAGE, GDBusMessagePrivate);
-
- message->priv->headers = g_hash_table_new_full (g_direct_hash,
- g_direct_equal,
- NULL,
- (GDestroyNotify) g_variant_unref);
+ /* Any D-Bus implementation is supposed to handle both Big and
+ * Little Endian encodings and the Endianness is part of the D-Bus
+ * message - we prefer to use Big Endian (since it's Network Byte
+ * Order and just easier to read for humans) but if the machine is
+ * Little Endian we use that for performance reasons.
+ */
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+ message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
+#else
+ /* this could also be G_PDP_ENDIAN */
+ message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
+#endif
+ message->headers = g_hash_table_new_full (g_direct_hash,
+ g_direct_equal,
+ NULL,
+ (GDestroyNotify) g_variant_unref);
}
/**
g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
message = g_dbus_message_new ();
- message->priv->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
+ message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
if (name != NULL)
g_dbus_message_set_destination (message, name);
/**
* g_dbus_message_new_signal:
* @path: A valid object path.
- * @interface_: A valid D-Bus interface name or %NULL.
+ * @interface_: A valid D-Bus interface name.
* @signal: A valid signal name.
*
* Creates a new #GDBusMessage for a signal emission.
g_return_val_if_fail (g_variant_is_object_path (path), NULL);
g_return_val_if_fail (g_dbus_is_member_name (signal), NULL);
- g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
+ g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL);
message = g_dbus_message_new ();
- message->priv->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
- message->priv->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
+ message->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
+ message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
g_dbus_message_set_path (message, path);
g_dbus_message_set_member (message, signal);
-
- if (interface_ != NULL)
- g_dbus_message_set_interface (message, interface_);
+ g_dbus_message_set_interface (message, interface_);
return message;
}
*
* Creates a new #GDBusMessage that is a reply to @method_call_message.
*
- * Returns: A #GDBusMessage. Free with g_object_unref().
+ * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
*
* Since: 2.26
*/
g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
message = g_dbus_message_new ();
- message->priv->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
- message->priv->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
+ message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
+ message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
+ /* reply with same endianness */
+ message->byte_order = method_call_message->byte_order;
g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
sender = g_dbus_message_get_sender (method_call_message);
*
* Creates a new #GDBusMessage that is an error reply to @method_call_message.
*
- * Returns: A #GDBusMessage. Free with g_object_unref().
+ * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
*
* Since: 2.26
*/
*
* Creates a new #GDBusMessage that is an error reply to @method_call_message.
*
- * Returns: A #GDBusMessage. Free with g_object_unref().
+ * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
*
* Since: 2.26
*/
g_return_val_if_fail (error_message != NULL, NULL);
message = g_dbus_message_new ();
- message->priv->type = G_DBUS_MESSAGE_TYPE_ERROR;
- message->priv->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
+ message->type = G_DBUS_MESSAGE_TYPE_ERROR;
+ message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
+ /* reply with same endianness */
+ message->byte_order = method_call_message->byte_order;
g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
g_dbus_message_set_error_name (message, error_name);
*
* Like g_dbus_message_new_method_error() but intended for language bindings.
*
- * Returns: A #GDBusMessage. Free with g_object_unref().
+ * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
*
* Since: 2.26
*/
/* ---------------------------------------------------------------------------------------------------- */
+/**
+ * g_dbus_message_get_byte_order:
+ * @message: A #GDBusMessage.
+ *
+ * Gets the byte order of @message.
+ *
+ * Returns: The byte order.
+ */
+GDBusMessageByteOrder
+g_dbus_message_get_byte_order (GDBusMessage *message)
+{
+ g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0);
+ return message->byte_order;
+}
+
+/**
+ * g_dbus_message_set_byte_order:
+ * @message: A #GDBusMessage.
+ * @byte_order: The byte order.
+ *
+ * Sets the byte order of @message.
+ */
+void
+g_dbus_message_set_byte_order (GDBusMessage *message,
+ GDBusMessageByteOrder byte_order)
+{
+ g_return_if_fail (G_IS_DBUS_MESSAGE (message));
+
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
+ message->byte_order = byte_order;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
/* TODO: need GI annotations to specify that any guchar value goes for the type */
/**
g_dbus_message_get_message_type (GDBusMessage *message)
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID);
- return message->priv->type;
+ return message->type;
}
/**
{
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
g_return_if_fail (type >=0 && type < 256);
- message->priv->type = type;
+
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
+ message->type = type;
}
/* ---------------------------------------------------------------------------------------------------- */
g_dbus_message_get_flags (GDBusMessage *message)
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE);
- return message->priv->flags;
+ return message->flags;
}
/**
{
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
g_return_if_fail (flags >=0 && flags < 256);
- message->priv->flags = flags;
+
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
+ message->flags = flags;
}
/* ---------------------------------------------------------------------------------------------------- */
g_dbus_message_get_serial (GDBusMessage *message)
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
- return message->priv->serial;
+ return message->serial;
}
/**
guint32 serial)
{
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
- message->priv->serial = serial;
+
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
+ message->serial = serial;
}
/* ---------------------------------------------------------------------------------------------------- */
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
g_return_val_if_fail (header_field >=0 && header_field < 256, NULL);
- return g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
+ return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
}
/**
{
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
g_return_if_fail (header_field >=0 && header_field < 256);
+
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
if (value == NULL)
{
- g_hash_table_remove (message->priv->headers, GUINT_TO_POINTER (header_field));
+ g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field));
}
else
{
- g_hash_table_insert (message->priv->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
+ g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
}
}
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
- keys = g_hash_table_get_keys (message->priv->headers);
+ keys = g_hash_table_get_keys (message->headers);
num_keys = g_list_length (keys);
ret = g_new (guchar, num_keys + 1);
for (l = keys, n = 0; l != NULL; l = l->next, n++)
g_dbus_message_get_body (GDBusMessage *message)
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
- return message->priv->body;
+ return message->body;
}
/**
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE));
- if (message->priv->body != NULL)
- g_variant_unref (message->priv->body);
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
+ if (message->body != NULL)
+ g_variant_unref (message->body);
if (body == NULL)
{
- message->priv->body = NULL;
+ message->body = NULL;
g_dbus_message_set_signature (message, NULL);
}
else
gsize type_string_len;
gchar *signature;
- message->priv->body = g_variant_ref_sink (body);
+ message->body = g_variant_ref_sink (body);
type_string = g_variant_get_type_string (body);
type_string_len = strlen (type_string);
*
* This method is only available on UNIX.
*
- * Returns: A #GUnixFDList or %NULL if no file descriptors are
+ * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
* associated. Do not free, this object is owned by @message.
*
* Since: 2.26
g_dbus_message_get_unix_fd_list (GDBusMessage *message)
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
- return message->priv->fd_list;
+ return message->fd_list;
}
/**
* g_dbus_message_set_unix_fd_list:
* @message: A #GDBusMessage.
- * @fd_list: A #GUnixFDList or %NULL.
+ * @fd_list: (allow-none): A #GUnixFDList or %NULL.
*
* Sets the UNIX file descriptors associated with @message. As a
* side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
{
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
- if (message->priv->fd_list != NULL)
- g_object_unref (message->priv->fd_list);
+
+ if (message->locked)
+ {
+ g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
+ return;
+ }
+
+ if (message->fd_list != NULL)
+ g_object_unref (message->fd_list);
if (fd_list != NULL)
{
- message->priv->fd_list = g_object_ref (fd_list);
+ message->fd_list = g_object_ref (fd_list);
g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list));
}
else
{
- message->priv->fd_list = NULL;
+ message->fd_list = NULL;
g_dbus_message_set_num_unix_fds (message, 0);
}
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
+validate_headers (GDBusMessage *message,
+ GError **error)
+{
+ gboolean ret;
+
+ g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
+ g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+ ret = FALSE;
+
+ switch (message->type)
+ {
+ case G_DBUS_MESSAGE_TYPE_INVALID:
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("type is INVALID"));
+ goto out;
+ break;
+
+ case G_DBUS_MESSAGE_TYPE_METHOD_CALL:
+ if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
+ g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("METHOD_CALL message: PATH or MEMBER header field is missing"));
+ goto out;
+ }
+ break;
+
+ case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
+ if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
+ goto out;
+ }
+ break;
+
+ case G_DBUS_MESSAGE_TYPE_ERROR:
+ if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME) == NULL ||
+ g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
+ goto out;
+ }
+ break;
+
+ case G_DBUS_MESSAGE_TYPE_SIGNAL:
+ if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
+ g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE) == NULL ||
+ g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
+ goto out;
+ }
+ if (g_strcmp0 (g_dbus_message_get_path (message), "/org/freedesktop/DBus/Local") == 0)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
+ goto out;
+ }
+ if (g_strcmp0 (g_dbus_message_get_interface (message), "org.freedesktop.DBus.Local") == 0)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
+ goto out;
+ }
+ break;
+
+ default:
+ /* hitherto unknown type - nothing to check */
+ break;
+ }
+
+ ret = TRUE;
+
+ out:
+ g_assert (ret || (error == NULL || *error != NULL));
+ return ret;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static gboolean
ensure_input_padding (GMemoryInputStream *mis,
gsize padding_size,
GError **error)
gsize remaining;
guchar nul;
GError *local_error;
+ const gchar *end_valid;
s = g_string_new (NULL);
goto fail;
if (num_read == 0)
{
+ /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Wanted to read %" G_GSIZE_FORMAT " bytes but got EOF"),
- to_read);
+ _("Wanted to read %lu bytes but got EOF"),
+ (gulong)to_read);
goto fail;
}
g_propagate_error (error, local_error);
goto fail;
}
+ if (!g_utf8_validate (s->str, -1, &end_valid))
+ {
+ gint offset;
+ gchar *valid_str;
+ offset = (gint) (end_valid - s->str);
+ valid_str = g_strndup (s->str, offset);
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
+ "The valid UTF-8 string up until that point was `%s'"),
+ offset,
+ (gint) s->len,
+ valid_str);
+ g_free (valid_str);
+ goto fail;
+ }
if (nul != '\0')
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Expected NUL byte after the string `%s' but found `%c' (%d)"),
- s->str, nul, nul);
+ _("Expected NUL byte after the string `%s' but found byte %d"),
+ s->str, nul);
goto fail;
}
v = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
if (local_error != NULL)
goto fail;
- /* TODO: hmm */
+ G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
encoded = (gdouble *) &v;
ret = g_variant_new_double (*encoded);
}
g_free (v);
}
}
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
+ {
+ if (!ensure_input_padding (mis, 4, &local_error))
+ goto fail;
+ if (!just_align)
+ {
+ gint32 v;
+ v = g_data_input_stream_read_int32 (dis, NULL, &local_error);
+ if (local_error != NULL)
+ goto fail;
+ ret = g_variant_new_handle (v);
+ }
+ }
else if (g_variant_type_is_array (type))
{
guint32 array_len;
if (array_len > (2<<26))
{
+ /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Encountered array of length %" G_GUINT32_FORMAT " bytes. Maximum length is 2<<26 bytes."),
+ _("Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB)."),
array_len);
goto fail;
}
g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Error deserializing GVariant with type-string `%s' from the D-Bus wire format"),
+ _("Error deserializing GVariant with type string `%s' from the D-Bus wire format"),
s);
g_free (s);
goto fail;
* @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
* @error: Return location for error or %NULL.
*
- * Creates a new #GDBusMessage from the data stored at @blob.
+ * Creates a new #GDBusMessage from the data stored at @blob. The byte
+ * order that the message was in can be retrieved using
+ * g_dbus_message_get_byte_order().
*
* Returns: A new #GDBusMessage or %NULL if @error is set. Free with
* g_object_unref().
{
case 'l':
byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
+ message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
break;
case 'B':
byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
+ message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
break;
default:
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Invalid endianness value. Expected 'l' or 'B' but found '%c' (%d)"),
- endianness, endianness);
+ _("Invalid endianness value. Expected 0x6c ('l') or 0x42 ('B') but found value 0x%02x"),
+ endianness);
goto out;
}
g_data_input_stream_set_byte_order (dis, byte_order);
- message->priv->type = g_data_input_stream_read_byte (dis, NULL, NULL);
- message->priv->flags = g_data_input_stream_read_byte (dis, NULL, NULL);
+ message->type = g_data_input_stream_read_byte (dis, NULL, NULL);
+ message->flags = g_data_input_stream_read_byte (dis, NULL, NULL);
major_protocol_version = g_data_input_stream_read_byte (dis, NULL, NULL);
if (major_protocol_version != 1)
{
goto out;
}
message_body_len = g_data_input_stream_read_uint32 (dis, NULL, NULL);
- message->priv->serial = g_data_input_stream_read_uint32 (dis, NULL, NULL);
+ message->serial = g_data_input_stream_read_uint32 (dis, NULL, NULL);
#ifdef DEBUG_SERIALIZER
g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len);
#ifdef DEBUG_SERIALIZER
g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len);
#endif /* DEBUG_SERIALIZER */
- message->priv->body = parse_value_from_blob (mis,
- dis,
- variant_type,
- FALSE,
- 2,
- error);
+ message->body = parse_value_from_blob (mis,
+ dis,
+ variant_type,
+ FALSE,
+ 2,
+ error);
g_variant_type_free (variant_type);
- if (message->priv->body == NULL)
+ if (message->body == NULL)
goto out;
}
}
/* no signature, this is only OK if the body is empty */
if (message_body_len != 0)
{
+ /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("No signature header in message but the message body is %" G_GUINT32_FORMAT " bytes"),
+ _("No signature header in message but the message body is %u bytes"),
message_body_len);
goto out;
}
}
+ if (!validate_headers (message, error))
+ {
+ g_prefix_error (error, _("Cannot deserialize message: "));
+ goto out;
+ }
ret = TRUE;
{
guint64 *encoded;
gdouble v = g_variant_get_double (value);
- /* TODO: hmm */
+ G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
encoded = (guint64 *) &v;
g_data_output_stream_put_uint64 (dos, *encoded, NULL, NULL);
}
if (value != NULL)
{
gsize len;
- const gchar *v = g_variant_get_string (value, &len);
+ const gchar *v;
+ const gchar *end;
+ v = g_variant_get_string (value, &len);
+ g_assert (g_utf8_validate (v, -1, &end) && (end == v + len));
g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
g_data_output_stream_put_string (dos, v, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
- /* TODO: validate object path */
gsize len;
const gchar *v = g_variant_get_string (value, &len);
+ g_assert (g_variant_is_object_path (v));
g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
g_data_output_stream_put_string (dos, v, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
{
if (value != NULL)
{
- /* TODO: validate signature (including max len being 255) */
gsize len;
const gchar *v = g_variant_get_string (value, &len);
+ g_assert (g_variant_is_signature (v));
g_data_output_stream_put_byte (dos, len, NULL, NULL);
g_data_output_stream_put_string (dos, v, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
}
}
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
+ {
+ padding_added = ensure_output_padding (mos, dos, 4);
+ if (value != NULL)
+ {
+ gint32 v = g_variant_get_handle (value);
+ g_data_output_stream_put_int32 (dos, v, NULL, NULL);
+ }
+ }
else if (g_variant_type_is_array (type))
{
GVariant *item;
const gchar *signature;
child = g_variant_get_child_value (value, 0);
signature = g_variant_get_type_string (child);
- /* TODO: validate signature (including max len being 255) */
g_data_output_stream_put_byte (dos, strlen (signature), NULL, NULL);
g_data_output_stream_put_string (dos, signature, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Error serializing GVariant with type-string `%s' to the D-Bus wire format"),
+ _("Error serializing GVariant with type string `%s' to the D-Bus wire format"),
g_variant_get_type_string (value));
goto fail;
}
* @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
* @error: Return location for error.
*
- * Serializes @message to a blob.
+ * Serializes @message to a blob. The byte order returned by
+ * g_dbus_message_get_byte_order() will be used.
*
* Returns: A pointer to a valid binary D-Bus message of @out_size bytes
* generated by @message or %NULL if @error is set. Free with g_free().
mos = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0, g_realloc, g_free));
dos = g_data_output_stream_new (G_OUTPUT_STREAM (mos));
- /* TODO: detect endianess... */
- byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
+ byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
+ switch (message->byte_order)
+ {
+ case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
+ byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
+ break;
+ case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
+ byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
+ break;
+ }
g_data_output_stream_set_byte_order (dos, byte_order);
/* Core header */
- g_data_output_stream_put_byte (dos, byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN ? 'l' : 'B', NULL, NULL);
- g_data_output_stream_put_byte (dos, message->priv->type, NULL, NULL);
- g_data_output_stream_put_byte (dos, message->priv->flags, NULL, NULL);
+ g_data_output_stream_put_byte (dos, (guchar) message->byte_order, NULL, NULL);
+ g_data_output_stream_put_byte (dos, message->type, NULL, NULL);
+ g_data_output_stream_put_byte (dos, message->flags, NULL, NULL);
g_data_output_stream_put_byte (dos, 1, NULL, NULL); /* major protocol version */
body_len_offset = g_memory_output_stream_get_data_size (mos);
/* body length - will be filled in later */
g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
- g_data_output_stream_put_uint32 (dos, message->priv->serial, NULL, NULL);
+ g_data_output_stream_put_uint32 (dos, message->serial, NULL, NULL);
num_fds_in_message = 0;
#ifdef G_OS_UNIX
- if (message->priv->fd_list != NULL)
- num_fds_in_message = g_unix_fd_list_get_length (message->priv->fd_list);
+ if (message->fd_list != NULL)
+ num_fds_in_message = g_unix_fd_list_get_length (message->fd_list);
#endif
num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
- /* TODO: check we have all the right header fields and that they are the correct value etc etc */
if (num_fds_in_message != num_fds_according_to_header)
{
g_set_error (error,
goto out;
}
+ if (!validate_headers (message, error))
+ {
+ g_prefix_error (error, _("Cannot serialize message: "));
+ goto out;
+ }
+
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
- g_hash_table_iter_init (&hash_iter, message->priv->headers);
+ g_hash_table_iter_init (&hash_iter, message->headers);
while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
{
g_variant_builder_add (&builder,
signature_str = NULL;
if (signature != NULL)
signature_str = g_variant_get_string (signature, NULL);
- if (message->priv->body != NULL)
+ if (message->body != NULL)
{
gchar *tupled_signature_str;
tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
g_free (tupled_signature_str);
goto out;
}
- else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->priv->body)) != 0)
+ else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0)
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Message body has type signature `%s' but signature in the header field is `%s'"),
- tupled_signature_str, g_variant_get_type_string (message->priv->body));
+ tupled_signature_str, g_variant_get_type_string (message->body));
g_free (tupled_signature_str);
goto out;
}
g_free (tupled_signature_str);
- if (!append_body_to_blob (message->priv->body, mos, dos, error))
+ if (!append_body_to_blob (message->body, mos, dos, error))
goto out;
}
else
{
- if (signature != NULL)
+ if (signature != NULL && strlen (signature_str) > 0)
{
g_set_error (error,
G_IO_ERROR,
g_data_output_stream_put_uint32 (dos, body_size, NULL, NULL);
+ if (!g_output_stream_close (G_OUTPUT_STREAM (dos), NULL, error))
+ goto out;
+
*out_size = size;
- ret = g_memdup (g_memory_output_stream_get_data (mos), size);
+ ret = g_memory_output_stream_steal_data (mos);
out:
g_object_unref (dos);
guint32 ret;
ret = 0;
- value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
+ value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
ret = g_variant_get_uint32 (value);
const gchar *ret;
ret = NULL;
- value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
+ value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
ret = g_variant_get_string (value, NULL);
const gchar *ret;
ret = NULL;
- value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
+ value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
ret = g_variant_get_string (value, NULL);
const gchar *ret;
ret = NULL;
- value = g_hash_table_lookup (message->priv->headers, GUINT_TO_POINTER (header_field));
+ value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
ret = g_variant_get_string (value, NULL);
ret = NULL;
- if (message->priv->body != NULL && g_variant_is_of_type (message->priv->body, G_VARIANT_TYPE_TUPLE))
+ if (message->body != NULL && g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE))
{
GVariant *item;
- item = g_variant_get_child_value (message->priv->body, 0);
+ item = g_variant_get_child_value (message->body, 0);
if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
ret = g_variant_get_string (item, NULL);
g_variant_unref (item);
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
ret = FALSE;
- if (message->priv->type != G_DBUS_MESSAGE_TYPE_ERROR)
+ if (message->type != G_DBUS_MESSAGE_TYPE_ERROR)
goto out;
error_name = g_dbus_message_get_error_name (message);
/* ---------------------------------------------------------------------------------------------------- */
static gchar *
-enum_to_string (GType enum_type, gint value)
-{
- gchar *ret;
- GEnumClass *klass;
- GEnumValue *enum_value;
-
- klass = g_type_class_ref (enum_type);
- enum_value = g_enum_get_value (klass, value);
- if (enum_value != NULL)
- ret = g_strdup (enum_value->value_nick);
- else
- ret = g_strdup_printf ("unknown (value %d)", value);
- g_type_class_unref (klass);
- return ret;
-}
-
-static gchar *
flags_to_string (GType flags_type, guint value)
{
GString *s;
if (s->len == 0)
g_string_append (s, "none");
g_type_class_unref (klass);
- return g_string_free (s, FALSE);;
+ return g_string_free (s, FALSE);
}
static gint
str = g_string_new (NULL);
- s = enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->priv->type);
+ s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type);
g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
g_free (s);
- s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->priv->flags);
+ s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags);
g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
g_free (s);
- g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->priv->major_protocol_version);
- g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->priv->serial);
+ g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version);
+ g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial);
g_string_append_printf (str, "%*sHeaders:\n", indent, "");
- keys = g_hash_table_get_keys (message->priv->headers);
+ keys = g_hash_table_get_keys (message->headers);
keys = g_list_sort (keys, _sort_keys_func);
if (keys != NULL)
{
GVariant *value;
gchar *value_str;
- value = g_hash_table_lookup (message->priv->headers, l->data);
+ value = g_hash_table_lookup (message->headers, l->data);
g_assert (value != NULL);
- s = enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
+ s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
value_str = g_variant_print (value, TRUE);
g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
g_free (s);
g_string_append_printf (str, "%*s (none)\n", indent, "");
}
g_string_append_printf (str, "%*sBody: ", indent, "");
- if (message->priv->body != NULL)
+ if (message->body != NULL)
{
- g_variant_print_string (message->priv->body,
+ g_variant_print_string (message->body,
str,
TRUE);
}
g_string_append (str, "\n");
#ifdef G_OS_UNIX
g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
- if (message->priv->fd_list != NULL)
+ if (message->fd_list != NULL)
{
gint num_fds;
const gint *fds;
gint n;
- fds = g_unix_fd_list_peek_fds (message->priv->fd_list, &num_fds);
+ fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
if (num_fds > 0)
{
for (n = 0; n < num_fds; n++)
return g_string_free (str, FALSE);
}
+/**
+ * g_dbus_message_get_locked:
+ * @message: A #GDBusMessage.
+ *
+ * Checks whether @message is locked. To monitor changes to this
+ * value, conncet to the #GObject::notify signal to listen for changes
+ * on the #GDBusMessage:locked property.
+ *
+ * Returns: %TRUE if @message is locked, %FALSE otherwise.
+ *
+ * Since: 2.26
+ */
+gboolean
+g_dbus_message_get_locked (GDBusMessage *message)
+{
+ g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
+ return message->locked;
+}
+
+/**
+ * g_dbus_message_lock:
+ * @message: A #GDBusMessage.
+ *
+ * If @message is locked, does nothing. Otherwise locks the message.
+ *
+ * Since: 2.26
+ */
+void
+g_dbus_message_lock (GDBusMessage *message)
+{
+ g_return_if_fail (G_IS_DBUS_MESSAGE (message));
+
+ if (message->locked)
+ goto out;
+
+ message->locked = TRUE;
+ g_object_notify (G_OBJECT (message), "locked");
+
+ out:
+ ;
+}
+
+/**
+ * g_dbus_message_copy:
+ * @message: A #GDBusMessage.
+ * @error: Return location for error or %NULL.
+ *
+ * Copies @message. The copy is a deep copy and the returned
+ * #GDBusMessage is completely identical except that it is guaranteed
+ * to not be locked.
+ *
+ * This operation can fail if e.g. @message contains file descriptors
+ * and the per-process or system-wide open files limit is reached.
+ *
+ * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set. Free with
+ * g_object_unref().
+ *
+ * Since: 2.26
+ */
+GDBusMessage *
+g_dbus_message_copy (GDBusMessage *message,
+ GError **error)
+{
+ GDBusMessage *ret;
+ GHashTableIter iter;
+ gpointer header_key;
+ GVariant *header_value;
+
+ g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+ ret = g_dbus_message_new ();
+ ret->type = message->type;
+ ret->flags = message->flags;
+ ret->byte_order = message->byte_order;
+ ret->major_protocol_version = message->major_protocol_version;
+ ret->serial = message->serial;
+
+#ifdef G_OS_UNIX
+ if (message->fd_list != NULL)
+ {
+ gint n;
+ gint num_fds;
+ const gint *fds;
+
+ ret->fd_list = g_unix_fd_list_new ();
+ fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
+ for (n = 0; n < num_fds; n++)
+ {
+ if (g_unix_fd_list_append (ret->fd_list,
+ fds[n],
+ error) == -1)
+ {
+ g_object_unref (ret);
+ ret = NULL;
+ goto out;
+ }
+ }
+ }
+#endif
+
+ /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
+ * to just ref (as opposed to deep-copying) the GVariant instances
+ */
+ ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL;
+ g_hash_table_iter_init (&iter, message->headers);
+ while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value))
+ g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value));
+
+ out:
+ return ret;
+}
-#define __G_DBUS_MESSAGE_C__
-#include "gioaliasdef.c"