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
34 #include "sipe-common.h"
36 #include "circbuffer.h"
37 #include "connection.h"
38 #include "eventloop.h"
43 #include "purple-private.h"
45 #include "sipe-backend.h"
46 #include "sipe-core.h"
49 struct sipe_transport_purple
{
50 /* public part shared with core */
51 struct sipe_transport_connection
public;
53 /* purple private part */
54 transport_connected_cb
*connected
;
55 transport_input_cb
*input
;
56 transport_error_cb
*error
;
58 PurpleSslConnection
*gsc
;
59 PurpleCircBuffer
*transmit_buffer
;
60 guint transmit_handler
;
61 guint receive_handler
;
65 #define PURPLE_TRANSPORT ((struct sipe_transport_purple *) conn)
66 #define SIPE_TRANSPORT_CONNECTION ((struct sipe_transport_connection *) transport)
68 #define BUFFER_SIZE_INCREMENT 4096
72 /*****************************************************************************
74 * Common transport handling
76 *****************************************************************************/
77 static void transport_common_input(struct sipe_transport_purple
*transport
)
79 struct sipe_transport_connection
*conn
= SIPE_TRANSPORT_CONNECTION
;
81 gboolean firstread
= TRUE
;
83 /* Read all available data from the connection */
85 /* Increase input buffer size as needed */
86 if (conn
->buffer_length
< conn
->buffer_used
+ BUFFER_SIZE_INCREMENT
) {
87 conn
->buffer_length
+= BUFFER_SIZE_INCREMENT
;
88 conn
->buffer
= g_realloc(conn
->buffer
, conn
->buffer_length
);
89 SIPE_DEBUG_INFO("transport_input_common: new buffer length %" G_GSIZE_FORMAT
,
93 /* Try to read as much as there is space left in the buffer */
94 /* minus 1 for the string terminator */
95 readlen
= conn
->buffer_length
- conn
->buffer_used
- 1;
96 len
= transport
->gsc
?
97 (gssize
) purple_ssl_read(transport
->gsc
,
98 conn
->buffer
+ conn
->buffer_used
,
100 read(transport
->socket
,
101 conn
->buffer
+ conn
->buffer_used
,
104 if (len
< 0 && errno
== EAGAIN
) {
105 /* Try again later */
107 } else if (len
< 0) {
108 SIPE_DEBUG_ERROR("Read error: %s (%d)", strerror(errno
), errno
);
109 transport
->error(SIPE_TRANSPORT_CONNECTION
, _("Read error"));
111 } else if (firstread
&& (len
== 0)) {
112 SIPE_DEBUG_ERROR_NOFORMAT("Server has disconnected");
113 transport
->error(SIPE_TRANSPORT_CONNECTION
, _("Server has disconnected"));
117 conn
->buffer_used
+= len
;
120 /* Equivalence indicates that there is possibly more data to read */
121 } while (len
== readlen
);
123 conn
->buffer
[conn
->buffer_used
] = '\0';
124 transport
->input(conn
);
127 static void transport_ssl_input(gpointer data
,
128 PurpleSslConnection
*gsc
,
129 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
131 struct sipe_transport_purple
*transport
= data
;
133 /* NOTE: This check *IS* necessary */
134 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
)) {
135 purple_ssl_close(gsc
);
136 transport
->gsc
= NULL
;
139 transport_common_input(transport
);
142 static void transport_tcp_input(gpointer data
,
144 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
146 struct sipe_transport_purple
*transport
= data
;
148 /* NOTE: This check *IS* necessary */
149 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
)) {
151 transport
->socket
= -1;
154 transport_common_input(transport
);
157 static void transport_ssl_connect_failure(SIPE_UNUSED_PARAMETER PurpleSslConnection
*gsc
,
158 PurpleSslErrorType error
,
161 struct sipe_transport_purple
*transport
= data
;
163 /* If the connection is already disconnected
164 then we don't need to do anything else */
165 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
))
168 transport
->socket
= -1;
169 transport
->gsc
= NULL
;
170 transport
->error(SIPE_TRANSPORT_CONNECTION
,
171 purple_ssl_strerror(error
));
174 static void transport_common_connected(struct sipe_transport_purple
*transport
,
175 PurpleSslConnection
*gsc
,
178 if (!PURPLE_CONNECTION_IS_VALID(transport
->gc
))
181 purple_ssl_close(gsc
);
182 } else if (fd
>= 0) {
189 transport
->error(SIPE_TRANSPORT_CONNECTION
,
190 _("Could not connect"));
194 transport
->socket
= fd
;
195 transport
->public.client_port
= purple_network_get_port_from_fd(fd
);
198 transport
->gsc
= gsc
;
199 purple_ssl_input_add(gsc
, transport_ssl_input
, transport
);
201 transport
->receive_handler
= purple_input_add(fd
,
207 transport
->connected(SIPE_TRANSPORT_CONNECTION
);
210 static void transport_ssl_connected(gpointer data
,
211 PurpleSslConnection
*gsc
,
212 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
214 transport_common_connected(data
, gsc
, gsc
? gsc
->fd
: -1);
217 static void transport_tcp_connected(gpointer data
,
219 SIPE_UNUSED_PARAMETER
const gchar
*error_message
)
221 transport_common_connected(data
, NULL
, source
);
224 struct sipe_transport_connection
*
225 sipe_backend_transport_connect(struct sipe_core_public
*sipe_public
,
226 const sipe_connect_setup
*setup
)
228 struct sipe_transport_purple
*transport
= g_new0(struct sipe_transport_purple
, 1);
229 struct sipe_backend_private
*purple_private
= sipe_public
->backend_private
;
230 PurpleConnection
*gc
= purple_private
->gc
;
231 PurpleAccount
*account
= purple_connection_get_account(gc
);
233 SIPE_DEBUG_INFO("transport_connect - hostname: %s port: %d",
234 setup
->server_name
, setup
->server_port
);
236 transport
->public.type
= setup
->type
;
237 transport
->public.user_data
= setup
->user_data
;
238 transport
->connected
= setup
->connected
;
239 transport
->input
= setup
->input
;
240 transport
->error
= setup
->error
;
242 transport
->transmit_buffer
= purple_circ_buffer_new(0);
244 if (setup
->type
== SIPE_TRANSPORT_TLS
) {
246 SIPE_DEBUG_INFO_NOFORMAT("using SSL");
248 if (purple_ssl_connect(account
,
251 transport_ssl_connected
,
252 transport_ssl_connect_failure
,
253 transport
) == NULL
) {
254 setup
->error(SIPE_TRANSPORT_CONNECTION
,
255 _("Could not create SSL context"));
256 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
259 } else if (setup
->type
== SIPE_TRANSPORT_TCP
) {
261 SIPE_DEBUG_INFO_NOFORMAT("using TCP");
263 if (purple_proxy_connect(gc
, account
,
266 transport_tcp_connected
,
267 transport
) == NULL
) {
268 setup
->error(SIPE_TRANSPORT_CONNECTION
,
269 _("Could not create socket"));
270 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
274 setup
->error(SIPE_TRANSPORT_CONNECTION
,
275 "This should not happen...");
276 sipe_backend_transport_disconnect(SIPE_TRANSPORT_CONNECTION
);
280 return(SIPE_TRANSPORT_CONNECTION
);
283 void sipe_backend_transport_disconnect(struct sipe_transport_connection
*conn
)
285 struct sipe_transport_purple
*transport
= PURPLE_TRANSPORT
;
287 if (!transport
) return;
289 if (transport
->gsc
) {
290 purple_ssl_close(transport
->gsc
);
291 } else if (transport
->socket
> 0) {
292 close(transport
->socket
);
295 if (transport
->transmit_handler
)
296 purple_input_remove(transport
->transmit_handler
);
297 if (transport
->receive_handler
)
298 purple_input_remove(transport
->receive_handler
);
300 if (transport
->transmit_buffer
)
301 purple_circ_buffer_destroy(transport
->transmit_buffer
);
302 g_free(transport
->public.buffer
);
307 /* returns FALSE on write error */
308 static gboolean
transport_write(struct sipe_transport_purple
*transport
)
312 max_write
= purple_circ_buffer_get_max_read(transport
->transmit_buffer
);
314 gssize written
= transport
->gsc
?
315 (gssize
) purple_ssl_write(transport
->gsc
,
316 transport
->transmit_buffer
->outptr
,
318 write(transport
->socket
,
319 transport
->transmit_buffer
->outptr
,
322 if (written
< 0 && errno
== EAGAIN
) {
324 } else if (written
<= 0) {
325 SIPE_DEBUG_ERROR("Write error: %s (%d)", strerror(errno
), errno
);
326 transport
->error(SIPE_TRANSPORT_CONNECTION
,
331 purple_circ_buffer_mark_read(transport
->transmit_buffer
,
335 /* buffer is empty -> stop sending */
336 purple_input_remove(transport
->transmit_handler
);
337 transport
->transmit_handler
= 0;
343 static void transport_canwrite_cb(gpointer data
,
344 SIPE_UNUSED_PARAMETER gint source
,
345 SIPE_UNUSED_PARAMETER PurpleInputCondition cond
)
347 transport_write(data
);
350 void sipe_backend_transport_message(struct sipe_transport_connection
*conn
,
353 struct sipe_transport_purple
*transport
= PURPLE_TRANSPORT
;
355 /* add packet to circular buffer */
356 purple_circ_buffer_append(transport
->transmit_buffer
,
357 buffer
, strlen(buffer
));
359 /* initiate transmission */
360 if (!transport
->transmit_handler
) {
361 transport
->transmit_handler
= purple_input_add(transport
->socket
,
363 transport_canwrite_cb
,
368 void sipe_backend_transport_flush(struct sipe_transport_connection
*conn
)
370 struct sipe_transport_purple
*transport
= PURPLE_TRANSPORT
;
372 while ( purple_circ_buffer_get_max_read(transport
->transmit_buffer
)
373 && transport_write(transport
));