2 * @file telepathy-transport.c
6 * Copyright (C) 2012 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "sipe-backend.h"
27 #include "sipe-common.h"
28 #include "sipe-core.h"
30 #include "telepathy-private.h"
32 struct sipe_transport_telepathy
{
33 /* public part shared with core */
34 struct sipe_transport_connection
public;
36 /* telepathy private part */
37 transport_connected_cb
*connected
;
38 transport_input_cb
*input
;
39 transport_error_cb
*error
;
40 struct sipe_backend_private
*private;
41 GSocketConnection
*socket
;
44 #define TELEPATHY_TRANSPORT ((struct sipe_transport_telepathy *) conn)
45 #define SIPE_TRANSPORT_CONNECTION ((struct sipe_transport_connection *) transport)
47 static void socket_connected(GObject
*client
,
51 struct sipe_transport_telepathy
*transport
= data
;
54 transport
->socket
= g_socket_client_connect_finish(G_SOCKET_CLIENT(client
),
58 if (transport
->socket
) {
59 SIPE_DEBUG_INFO_NOFORMAT("socket_connected: success");
60 transport
->connected(SIPE_TRANSPORT_CONNECTION
);
62 SIPE_DEBUG_ERROR("socket_connected: failed: %s", error
->message
);
63 transport
->error(SIPE_TRANSPORT_CONNECTION
, error
->message
);
67 struct sipe_transport_connection
*sipe_backend_transport_connect(struct sipe_core_public
*sipe_public
,
68 const sipe_connect_setup
*setup
)
70 struct sipe_transport_telepathy
*transport
= g_new0(struct sipe_transport_telepathy
, 1);
71 struct sipe_backend_private
*telepathy_private
= sipe_public
->backend_private
;
73 SIPE_DEBUG_INFO("sipe_backend_transport_connect - hostname: %s port: %d",
74 setup
->server_name
, setup
->server_port
);
76 transport
->public.type
= setup
->type
;
77 transport
->public.user_data
= setup
->user_data
;
78 transport
->connected
= setup
->connected
;
79 transport
->input
= setup
->input
;
80 transport
->error
= setup
->error
;
81 transport
->private = telepathy_private
;
83 if ((setup
->type
== SIPE_TRANSPORT_TLS
) ||
84 (setup
->type
== SIPE_TRANSPORT_TCP
)) {
85 GSocketClient
*client
= g_socket_client_new();
87 /* request TLS connection */
88 if (setup
->type
== SIPE_TRANSPORT_TLS
) {
89 SIPE_DEBUG_INFO_NOFORMAT("using TLS");
90 g_socket_client_set_tls(client
,
91 setup
->type
== SIPE_TRANSPORT_TLS
);
92 /* @TODO certificate handling - now accept all*/
93 g_socket_client_set_tls_validation_flags(client
, 0);
95 SIPE_DEBUG_INFO_NOFORMAT("using TCP");
97 g_socket_client_connect_async(client
,
98 g_network_address_new(setup
->server_name
,
103 g_object_unref(client
);
105 setup
->error(SIPE_TRANSPORT_CONNECTION
,
106 "This should not happen...");
107 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
111 /* the first connection is always to the server */
112 if (telepathy_private
->transport
== NULL
)
113 telepathy_private
->transport
= transport
;
115 return(SIPE_TRANSPORT_CONNECTION
);
118 void sipe_backend_transport_disconnect(struct sipe_transport_connection
*conn
)
120 struct sipe_transport_telepathy
*transport
= TELEPATHY_TRANSPORT
;
122 if (!transport
) return;
124 if (transport
->socket
)
125 g_object_unref(transport
->socket
);
127 /* connection to the server dropped? */
128 if (transport
->private->transport
== transport
)
129 transport
->private->transport
= NULL
;
134 void sipe_backend_transport_message(SIPE_UNUSED_PARAMETER
struct sipe_transport_connection
*conn
,
135 SIPE_UNUSED_PARAMETER
const gchar
*buffer
)
140 void sipe_backend_transport_flush(SIPE_UNUSED_PARAMETER
struct sipe_transport_connection
*conn
)