From 62197f1d3c75224c2138ebda5dbc420bc166e3a6 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Tue, 19 May 2009 16:17:22 +0200 Subject: Look at the available socket type Look at the available socket types, and don't hardcode UNIX/LOCALHOST values. --- libempathy/empathy-tp-file.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index 3a5826868..c265e29f0 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -71,6 +72,8 @@ typedef struct { /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */ TpFileTransferState state; TpFileTransferStateChangeReason state_change_reason; + TpSocketAddressType socket_address_type; + TpSocketAccessControl socket_access_control; /* transfer properties */ gboolean incoming; @@ -123,6 +126,71 @@ tp_file_get_state_cb (TpProxy *proxy, priv->state = g_value_get_uint (value); } +static gint +uint_compare (gconstpointer a, gconstpointer b) +{ + const guint *uinta = a; + const guint *uintb = b; + + if (*uinta == *uintb) + return 0; + + return (*uinta > *uintb) ? 1 : -1; +} + +static void +tp_file_get_available_socket_types_cb (TpProxy *proxy, + const GValue *value, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + EmpathyTpFilePriv *priv = GET_PRIV (weak_object); + GHashTable *socket_types; + GArray *access_controls; + + if (error != NULL || + !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP)) + { + /* set a default value */ + priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX; + priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST; + goto out; + } + + socket_types = g_value_get_boxed (value); + + /* here UNIX is preferred to IPV4 */ + if ((access_controls = g_hash_table_lookup (socket_types, + GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_UNIX))) != NULL) + { + priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX; + priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST; + goto out; + } + + if ((access_controls = g_hash_table_lookup (socket_types, + GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL) + { + priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4; + g_array_sort (access_controls, uint_compare); + + /* here port is preferred over localhost */ + if ((g_array_index (access_controls, guint, 0) == + TP_SOCKET_ACCESS_CONTROL_LOCALHOST) && + (g_array_index (access_controls, guint, 1) == + TP_SOCKET_ACCESS_CONTROL_PORT)) + priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_PORT; + else + priv->socket_access_control = + g_array_index (access_controls, guint, 0); + } + +out: + DEBUG ("Socket address type: %d, access control %d", + priv->socket_address_type, priv->socket_access_control); +} + static void tp_file_invalidated_cb (TpProxy *proxy, guint domain, @@ -667,6 +735,10 @@ do_constructed (GObject *object) -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb, NULL, NULL, object); + tp_cli_dbus_properties_call_get (priv->channel, + -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes", + tp_file_get_available_socket_types_cb, NULL, NULL, file_obj); + priv->state_change_reason = TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE; } -- cgit v1.2.3 From 75fd5cc5365178dbaeb5d48cf5c426c46e46b867 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Wed, 20 May 2009 02:12:30 +0200 Subject: Add support for IPV4 sockets --- libempathy/empathy-tp-file.c | 93 ++++++++++++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index c265e29f0..3b0d0c30a 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -78,7 +79,7 @@ typedef struct { /* transfer properties */ gboolean incoming; time_t start_time; - GArray *unix_socket_path; + GArray *socket_path; guint64 offset; /* GCancellable we're passed when offering/accepting the transfer */ @@ -187,7 +188,7 @@ tp_file_get_available_socket_types_cb (TpProxy *proxy, } out: - DEBUG ("Socket address type: %d, access control %d", + DEBUG ("Socket address type: %u, access control %u", priv->socket_address_type, priv->socket_access_control); } @@ -274,12 +275,18 @@ splice_stream_ready_cb (GObject *source, static void tp_file_start_transfer (EmpathyTpFile *tp_file) { - gint fd; - struct sockaddr_un addr; + gint fd, domain, res = 0; GError *error = NULL; EmpathyTpFilePriv *priv = GET_PRIV (tp_file); - fd = socket (PF_UNIX, SOCK_STREAM, 0); + if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX) + domain = AF_UNIX; + + if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4) + domain = AF_INET; + + fd = socket (domain, SOCK_STREAM, 0); + if (fd < 0) { int code = errno; @@ -295,12 +302,29 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) return; } - memset (&addr, 0, sizeof (addr)); - addr.sun_family = AF_UNIX; - strncpy (addr.sun_path, priv->unix_socket_path->data, - priv->unix_socket_path->len); + if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX) + { + struct sockaddr_un addr; + + memset (&addr, 0, sizeof (addr)); + addr.sun_family = domain; + strncpy (addr.sun_path, priv->socket_path->data, + priv->socket_path->len); + + res = connect (fd, (struct sockaddr*) &addr, sizeof (addr)); + } + else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4) + { + struct sockaddr_in addr; + + memset (&addr, 0, sizeof (addr)); + addr.sin_family = domain; + addr.sin_addr.s_addr = inet_network (priv->socket_path->data); + + res = connect (fd, (struct sockaddr*) &addr, sizeof (addr)); + } - if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) + if (res < 0) { int code = errno; @@ -422,7 +446,7 @@ tp_file_state_changed_cb (TpChannel *proxy, * data transfer but are just an observer for the channel. */ if (state == TP_FILE_TRANSFER_STATE_OPEN && - priv->unix_socket_path != NULL) + priv->socket_path != NULL) tp_file_start_transfer (EMPATHY_TP_FILE (weak_object)); if (state == TP_FILE_TRANSFER_STATE_COMPLETED) @@ -489,7 +513,7 @@ ft_operation_provide_or_accept_file_cb (TpChannel *proxy, if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY) { - priv->unix_socket_path = g_value_dup_boxed (address); + priv->socket_path = g_value_dup_boxed (address); } else if (G_VALUE_TYPE (address) == G_TYPE_STRING) { @@ -498,12 +522,12 @@ ft_operation_provide_or_accept_file_cb (TpChannel *proxy, const gchar *path; path = g_value_get_string (address); - priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar), - strlen (path)); - g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path)); + priv->socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar), + strlen (path)); + g_array_insert_vals (priv->socket_path, 0, path, strlen (path)); } - DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data); + DEBUG ("Got socket path: %s", priv->socket_path->data); /* if the channel is already open, start the transfer now, otherwise, * wait for the state change signal. @@ -512,6 +536,21 @@ ft_operation_provide_or_accept_file_cb (TpChannel *proxy, tp_file_start_transfer (tp_file); } +static void +initialize_empty_ac_variant (TpSocketAccessControl ac, + GValue *val) +{ + if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST) + { + g_value_init (val, G_TYPE_STRING); + g_value_set_static_string (val, ""); + } + else if (ac == TP_SOCKET_ACCESS_CONTROL_PORT) + { + g_value_init (val, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4); + } +} + static void file_read_async_cb (GObject *source, GAsyncResult *res, @@ -536,12 +575,14 @@ file_read_async_cb (GObject *source, priv->in_stream = G_INPUT_STREAM (in_stream); - g_value_init (¬hing, G_TYPE_STRING); - g_value_set_static_string (¬hing, ""); + /* we don't impose specific interface/port requirements even + * if we're not using UNIX sockets. + */ + initialize_empty_ac_variant (priv->socket_access_control, ¬hing); tp_cli_channel_type_file_transfer_call_provide_file ( priv->channel, -1, - TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, + priv->socket_address_type, priv->socket_access_control, ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file)); } @@ -571,11 +612,13 @@ file_replace_async_cb (GObject *source, priv->out_stream = G_OUTPUT_STREAM (out_stream); - g_value_init (¬hing, G_TYPE_STRING); - g_value_set_static_string (¬hing, ""); + /* we don't impose specific interface/port requirements even + * if we're not using UNIX sockets. + */ + initialize_empty_ac_variant (priv->socket_access_control, ¬hing); tp_cli_channel_type_file_transfer_call_accept_file (priv->channel, - -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, + -1, priv->socket_address_type, priv->socket_access_control, ¬hing, priv->offset, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file)); } @@ -660,10 +703,10 @@ do_finalize (GObject *object) DEBUG ("%p", object); - if (priv->unix_socket_path != NULL) + if (priv->socket_path != NULL) { - g_array_free (priv->unix_socket_path, TRUE); - priv->unix_socket_path = NULL; + g_array_free (priv->socket_path, TRUE); + priv->socket_path = NULL; } G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object); -- cgit v1.2.3 From 87d8aa4edcb738d89e77a0d8286e47649fe164f6 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Wed, 20 May 2009 12:15:08 +0200 Subject: Initialize the address and port values The address the CM returns if we're using IPV4 is of the type TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4; use those values to setup the IPV4 socket. --- libempathy/empathy-tp-file.c | 46 ++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index 3b0d0c30a..b97d1a297 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -79,7 +79,8 @@ typedef struct { /* transfer properties */ gboolean incoming; time_t start_time; - GArray *socket_path; + GArray *socket_address; + guint port; guint64 offset; /* GCancellable we're passed when offering/accepting the transfer */ @@ -308,8 +309,8 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) memset (&addr, 0, sizeof (addr)); addr.sun_family = domain; - strncpy (addr.sun_path, priv->socket_path->data, - priv->socket_path->len); + strncpy (addr.sun_path, priv->socket_address->data, + priv->socket_address->len); res = connect (fd, (struct sockaddr*) &addr, sizeof (addr)); } @@ -319,7 +320,8 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) memset (&addr, 0, sizeof (addr)); addr.sin_family = domain; - addr.sin_addr.s_addr = inet_network (priv->socket_path->data); + inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr); + addr.sin_port = htons (priv->port); res = connect (fd, (struct sockaddr*) &addr, sizeof (addr)); } @@ -446,7 +448,7 @@ tp_file_state_changed_cb (TpChannel *proxy, * data transfer but are just an observer for the channel. */ if (state == TP_FILE_TRANSFER_STATE_OPEN && - priv->socket_path != NULL) + priv->socket_address != NULL) tp_file_start_transfer (EMPATHY_TP_FILE (weak_object)); if (state == TP_FILE_TRANSFER_STATE_COMPLETED) @@ -513,7 +515,7 @@ ft_operation_provide_or_accept_file_cb (TpChannel *proxy, if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY) { - priv->socket_path = g_value_dup_boxed (address); + priv->socket_address = g_value_dup_boxed (address); } else if (G_VALUE_TYPE (address) == G_TYPE_STRING) { @@ -522,12 +524,32 @@ ft_operation_provide_or_accept_file_cb (TpChannel *proxy, const gchar *path; path = g_value_get_string (address); - priv->socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar), + priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path)); - g_array_insert_vals (priv->socket_path, 0, path, strlen (path)); + g_array_insert_vals (priv->socket_address, 0, path, strlen (path)); + } + else if (G_VALUE_TYPE (address) == TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4) + { + GValueArray *val_array; + GValue *v; + const char *addr; + + val_array = g_value_get_boxed (address); + + /* IPV4 address */ + v = g_value_array_get_nth (val_array, 0); + addr = g_value_get_string (v); + priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar), + strlen (addr)); + g_array_insert_vals (priv->socket_address, 0, addr, strlen (addr)); + + /* port number */ + v = g_value_array_get_nth (val_array, 1); + priv->port = g_value_get_uint (v); } - DEBUG ("Got socket path: %s", priv->socket_path->data); + DEBUG ("Got socket address: %s, port (not zero if IPV4): %d", + priv->socket_address->data, priv->port); /* if the channel is already open, start the transfer now, otherwise, * wait for the state change signal. @@ -703,10 +725,10 @@ do_finalize (GObject *object) DEBUG ("%p", object); - if (priv->socket_path != NULL) + if (priv->socket_address != NULL) { - g_array_free (priv->socket_path, TRUE); - priv->socket_path = NULL; + g_array_free (priv->socket_address, TRUE); + priv->socket_address = NULL; } G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object); -- cgit v1.2.3 From 246c2f6468ab7dc1546b2793aed023035f5d88e8 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Sun, 24 May 2009 17:49:08 +0200 Subject: Build fix after rebase --- libempathy/empathy-tp-file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index b97d1a297..9ae42828b 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -802,7 +802,7 @@ do_constructed (GObject *object) tp_cli_dbus_properties_call_get (priv->channel, -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes", - tp_file_get_available_socket_types_cb, NULL, NULL, file_obj); + tp_file_get_available_socket_types_cb, NULL, NULL, object); priv->state_change_reason = TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE; -- cgit v1.2.3 From f0d5544426b06662365a3b99e05eae23e76b6ee6 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Tue, 2 Jun 2009 11:09:28 +0200 Subject: Use empathy_uint_compare --- libempathy/empathy-tp-file.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index 9ae42828b..a056699f8 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -128,18 +128,6 @@ tp_file_get_state_cb (TpProxy *proxy, priv->state = g_value_get_uint (value); } -static gint -uint_compare (gconstpointer a, gconstpointer b) -{ - const guint *uinta = a; - const guint *uintb = b; - - if (*uinta == *uintb) - return 0; - - return (*uinta > *uintb) ? 1 : -1; -} - static void tp_file_get_available_socket_types_cb (TpProxy *proxy, const GValue *value, @@ -175,7 +163,7 @@ tp_file_get_available_socket_types_cb (TpProxy *proxy, GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL) { priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4; - g_array_sort (access_controls, uint_compare); + g_array_sort (access_controls, empathy_uint_compare); /* here port is preferred over localhost */ if ((g_array_index (access_controls, guint, 0) == -- cgit v1.2.3 From d0c304bac08a910c048bee84338a97d55a5d0811 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Sun, 7 Jun 2009 15:10:23 +0200 Subject: Emit an error for socket types we don't handle Also, fix error handling when a socket fails on connect(). --- libempathy/empathy-tp-file.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index a056699f8..998de5e51 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -266,13 +266,30 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) { gint fd, domain, res = 0; GError *error = NULL; + struct sockaddr *my_addr = NULL; + size_t my_size = 0; EmpathyTpFilePriv *priv = GET_PRIV (tp_file); if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX) - domain = AF_UNIX; + { + domain = AF_UNIX; + } + else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4) + { + domain = AF_INET; + } + else + { + error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported")); - if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4) - domain = AF_INET; + DEBUG ("Socket not supported, closing channel"); + + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); + + return; + } fd = socket (domain, SOCK_STREAM, 0); @@ -300,7 +317,8 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) strncpy (addr.sun_path, priv->socket_address->data, priv->socket_address->len); - res = connect (fd, (struct sockaddr*) &addr, sizeof (addr)); + my_addr = (struct sockaddr *) &addr; + my_size = sizeof (addr); } else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4) { @@ -311,9 +329,12 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr); addr.sin_port = htons (priv->port); - res = connect (fd, (struct sockaddr*) &addr, sizeof (addr)); + my_addr = (struct sockaddr *) &addr; + my_size = sizeof (addr); } + res = connect (fd, my_addr, my_size); + if (res < 0) { int code = errno; -- cgit v1.2.3 From f399b307ebc18c2382cb09ac24b41de2f5212bf7 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Sun, 7 Jun 2009 15:11:28 +0200 Subject: Fix make check --- libempathy/empathy-tp-file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index 998de5e51..d7d51ec71 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -178,7 +178,7 @@ tp_file_get_available_socket_types_cb (TpProxy *proxy, out: DEBUG ("Socket address type: %u, access control %u", - priv->socket_address_type, priv->socket_access_control); + priv->socket_address_type, priv->socket_access_control); } static void -- cgit v1.2.3 From 40e02734065878fcfbb0a316ca2217a5676862e8 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Fri, 12 Jun 2009 11:55:52 +0200 Subject: Drop PORT access control support for now --- libempathy/empathy-tp-file.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index d7d51ec71..76882022a 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -163,17 +163,12 @@ tp_file_get_available_socket_types_cb (TpProxy *proxy, GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL) { priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4; - g_array_sort (access_controls, empathy_uint_compare); - - /* here port is preferred over localhost */ - if ((g_array_index (access_controls, guint, 0) == - TP_SOCKET_ACCESS_CONTROL_LOCALHOST) && - (g_array_index (access_controls, guint, 1) == - TP_SOCKET_ACCESS_CONTROL_PORT)) - priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_PORT; - else - priv->socket_access_control = - g_array_index (access_controls, guint, 0); + + /* TODO: we should prefer PORT over LOCALHOST when the CM will + * support it. + */ + + priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST; } out: @@ -571,15 +566,12 @@ static void initialize_empty_ac_variant (TpSocketAccessControl ac, GValue *val) { + /* TODO: we will add more types here once we support PORT access control. */ if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST) { g_value_init (val, G_TYPE_STRING); g_value_set_static_string (val, ""); } - else if (ac == TP_SOCKET_ACCESS_CONTROL_PORT) - { - g_value_init (val, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4); - } } static void -- cgit v1.2.3