2 * @file purple-transport.c
6 * Copyright (C) 2010 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
36 #include "sipe-common.h"
38 #include "circbuffer.h"
39 #include "connection.h"
40 #include "eventloop.h"
46 /* wrappers for write() & friends for socket handling */
47 #include "win32/win32dep.h"
50 #include "purple-private.h"
52 #include "sipe-backend.h"
53 #include "sipe-core.h"
56 struct sipe_transport_purple
{
57 /* public part shared with core */
58 struct sipe_transport_connection
public;
60 /* purple private part */
61 transport_connected_cb
*connected
;
62 transport_input_cb
*input
;
63 transport_error_cb
*error
;
65 PurpleSslConnection
*gsc
;
66 PurpleCircBuffer
*transmit_buffer
;
67 guint transmit_handler
;
68 guint receive_handler
;
72 #define PURPLE_TRANSPORT ((struct sipe_transport_purple *) conn)
73 #define SIPE_TRANSPORT_CONNECTION ((struct sipe_transport_connection *) transport)
75 #define BUFFER_SIZE_INCREMENT 4096
79 /*****************************************************************************
81 * Common transport handling
83 *****************************************************************************/
84 static void transport_common_input(struct sipe_transport_purple
*transport
)
86 struct sipe_transport_connection
*conn
= SIPE_TRANSPORT_CONNECTION
;
88 gboolean firstread
= TRUE
;
90 /* Read all available data from the connection */
92 /* Increase input buffer size as needed */
93 if (conn
->buffer_length
< conn
->buffer_used
+ BUFFER_SIZE_INCREMENT
) {
94 conn
->buffer_length
+= BUFFER_SIZE_INCREMENT
;
95 conn
->buffer
= g_realloc(conn
->buffer
, conn
->buffer_length
);
96 SIPE_DEBUG_INFO("transport_input_common: new buffer length %" G_GSIZE_FORMAT
,
100 /* Try to read as much as there is space left in the buffer */
101 /* minus 1 for the string terminator */
102 readlen
= conn
->buffer_length
- conn
->buffer_used
- 1;
103 len
= transport
->gsc
?
104 (gssize
) purple_ssl_read(transport
->gsc
,
105 conn
->buffer
+ conn
->buffer_used
,
107 read(transport
->socket
,
108 conn
->buffer
+ conn
->buffer_used
,
111 if (len
< 0 && errno
== EAGAIN
) {
112 /* Try again later */
114 } else if (len
< 0) {
115 SIPE_DEBUG_ERROR("Read error: %s (%d)", strerror(errno
), errno
);
116 transport
->error(SIPE_TRANSPORT_CONNECTION
, _("Read error"));
118 } else if (firstread
&& (len
== 0)) {
119 SIPE_DEBUG_ERROR_NOFORMAT("Server has disconnected");
120 transport
->error(SIPE_TRANSPORT_CONNECTION
, _("Server has disconnected"));
124 conn
->buffer_used
+= len
;
127 /* Equivalence indicates that there is possibly more data to read */
128 } while (len
== readlen
);
130 conn
->buffer
[conn
->buffer_used
] = '\0';
131 transport
->input(conn
);
134 static void transport_ssl_input(gpointer data
,
135 PurpleSslConnection
*gsc
,
136 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
138 struct sipe_transport_purple
*transport
= data
;
140 /* NOTE: This check *IS* necessary */
141 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
)) {
142 purple_ssl_close(gsc
);
143 transport
->gsc
= NULL
;
146 transport_common_input(transport
);
149 static void transport_tcp_input(gpointer data
,
151 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
153 struct sipe_transport_purple
*transport
= data
;
155 /* NOTE: This check *IS* necessary */
156 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
)) {
158 transport
->socket
= -1;
161 transport_common_input(transport
);
164 static void transport_ssl_connect_failure(SIPE_UNUSED_PARAMETER PurpleSslConnection
*gsc
,
165 PurpleSslErrorType error
,
168 struct sipe_transport_purple
*transport
= data
;
170 /* If the connection is already disconnected
171 then we don't need to do anything else */
172 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
))
175 transport
->socket
= -1;
176 transport
->gsc
= NULL
;
177 transport
->error(SIPE_TRANSPORT_CONNECTION
,
178 purple_ssl_strerror(error
));
181 static void transport_common_connected(struct sipe_transport_purple
*transport
,
182 PurpleSslConnection
*gsc
,
185 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
))
188 purple_ssl_close(gsc
);
189 } else if (fd
>= 0) {
196 transport
->error(SIPE_TRANSPORT_CONNECTION
,
197 _("Could not connect"));
201 transport
->socket
= fd
;
202 transport
->public.client_port
= purple_network_get_port_from_fd(fd
);
205 transport
->gsc
= gsc
;
206 purple_ssl_input_add(gsc
, transport_ssl_input
, transport
);
208 transport
->receive_handler
= purple_input_add(fd
,
214 transport
->connected(SIPE_TRANSPORT_CONNECTION
);
217 static void transport_ssl_connected(gpointer data
,
218 PurpleSslConnection
*gsc
,
219 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
221 transport_common_connected(data
, gsc
, gsc
? gsc
->fd
: -1);
224 static void transport_tcp_connected(gpointer data
,
226 SIPE_UNUSED_PARAMETER
const gchar
*error_message
)
228 transport_common_connected(data
, NULL
, source
);
231 struct sipe_transport_connection
*
232 sipe_backend_transport_connect(struct sipe_core_public
*sipe_public
,
233 const sipe_connect_setup
*setup
)
235 struct sipe_transport_purple
*transport
= g_new0(struct sipe_transport_purple
, 1);
236 struct sipe_backend_private
*purple_private
= sipe_public
->backend_private
;
237 PurpleConnection
*gc
= purple_private
->gc
;
238 PurpleAccount
*account
= purple_connection_get_account(gc
);
240 SIPE_DEBUG_INFO("transport_connect - hostname: %s port: %d",
241 setup
->server_name
, setup
->server_port
);
243 transport
->public.type
= setup
->type
;
244 transport
->public.user_data
= setup
->user_data
;
245 transport
->connected
= setup
->connected
;
246 transport
->input
= setup
->input
;
247 transport
->error
= setup
->error
;
249 transport
->transmit_buffer
= purple_circ_buffer_new(0);
251 if (setup
->type
== SIPE_TRANSPORT_TLS
) {
253 SIPE_DEBUG_INFO_NOFORMAT("using SSL");
255 if (purple_ssl_connect(account
,
258 transport_ssl_connected
,
259 transport_ssl_connect_failure
,
260 transport
) == NULL
) {
261 setup
->error(SIPE_TRANSPORT_CONNECTION
,
262 _("Could not create SSL context"));
263 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
266 } else if (setup
->type
== SIPE_TRANSPORT_TCP
) {
268 SIPE_DEBUG_INFO_NOFORMAT("using TCP");
270 if (purple_proxy_connect(gc
, account
,
273 transport_tcp_connected
,
274 transport
) == NULL
) {
275 setup
->error(SIPE_TRANSPORT_CONNECTION
,
276 _("Could not create socket"));
277 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
281 setup
->error(SIPE_TRANSPORT_CONNECTION
,
282 "This should not happen...");
283 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
287 return(SIPE_TRANSPORT_CONNECTION
);
290 void sipe_backend_transport_disconnect(struct sipe_transport_connection
*conn
)
292 struct sipe_transport_purple
*transport
= PURPLE_TRANSPORT
;
294 if (!transport
) return;
296 if (transport
->gsc
) {
297 purple_ssl_close(transport
->gsc
);
298 } else if (transport
->socket
> 0) {
299 close(transport
->socket
);
302 if (transport
->transmit_handler
)
303 purple_input_remove(transport
->transmit_handler
);
304 if (transport
->receive_handler
)
305 purple_input_remove(transport
->receive_handler
);
307 if (transport
->transmit_buffer
)
308 purple_circ_buffer_destroy(transport
->transmit_buffer
);
309 g_free(transport
->public.buffer
);
314 /* returns FALSE on write error */
315 static gboolean
transport_write(struct sipe_transport_purple
*transport
)
319 max_write
= purple_circ_buffer_get_max_read(transport
->transmit_buffer
);
321 gssize written
= transport
->gsc
?
322 (gssize
) purple_ssl_write(transport
->gsc
,
323 transport
->transmit_buffer
->outptr
,
325 write(transport
->socket
,
326 transport
->transmit_buffer
->outptr
,
329 if (written
< 0 && errno
== EAGAIN
) {
331 } else if (written
<= 0) {
332 SIPE_DEBUG_ERROR("Write error: %s (%d)", strerror(errno
), errno
);
333 transport
->error(SIPE_TRANSPORT_CONNECTION
,
338 purple_circ_buffer_mark_read(transport
->transmit_buffer
,
342 /* buffer is empty -> stop sending */
343 purple_input_remove(transport
->transmit_handler
);
344 transport
->transmit_handler
= 0;
350 static void transport_canwrite_cb(gpointer data
,
351 SIPE_UNUSED_PARAMETER gint source
,
352 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
354 transport_write(data
);
357 void sipe_backend_transport_message(struct sipe_transport_connection
*conn
,
360 struct sipe_transport_purple
*transport
= PURPLE_TRANSPORT
;
362 /* add packet to circular buffer */
363 purple_circ_buffer_append(transport
->transmit_buffer
,
364 buffer
, strlen(buffer
));
366 /* initiate transmission */
367 if (!transport
->transmit_handler
) {
368 transport
->transmit_handler
= purple_input_add(transport
->socket
,
370 transport_canwrite_cb
,
375 void sipe_backend_transport_flush(struct sipe_transport_connection
*conn
)
377 struct sipe_transport_purple
*transport
= PURPLE_TRANSPORT
;
379 while ( purple_circ_buffer_get_max_read(transport
->transmit_buffer
)
380 && transport_write(transport
));