try again
[claws.git] / src / common / session.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <errno.h>
34
35 #include "session.h"
36 #include "utils.h"
37
38 static gint session_connect_cb          (SockInfo       *sock,
39                                          gpointer        data);
40 static gint session_close               (Session        *session);
41
42 static gboolean session_timeout_cb      (gpointer        data);
43
44 static gboolean session_recv_msg_idle_cb        (gpointer        data);
45 static gboolean session_recv_data_idle_cb       (gpointer        data);
46
47 static gboolean session_read_msg_cb     (SockInfo       *source,
48                                          GIOCondition    condition,
49                                          gpointer        data);
50 static gboolean session_read_data_cb    (SockInfo       *source,
51                                          GIOCondition    condition,
52                                          gpointer        data);
53 static gboolean session_write_msg_cb    (SockInfo       *source,
54                                          GIOCondition    condition,
55                                          gpointer        data);
56 static gboolean session_write_data_cb   (SockInfo       *source,
57                                          GIOCondition    condition,
58                                          gpointer        data);
59
60
61 void session_init(Session *session)
62 {
63         session->type = SESSION_UNKNOWN;
64         session->sock = NULL;
65         session->server = NULL;
66         session->port = 0;
67 #if USE_OPENSSL
68         session->ssl_type = SSL_NONE;
69 #endif
70         session->nonblocking = TRUE;
71         session->state = SESSION_READY;
72         session->last_access_time = time(NULL);
73
74         g_get_current_time(&session->tv_prev);
75
76         session->conn_id = 0;
77
78         session->io_tag = 0;
79
80         session->read_buf_p = session->read_buf;
81         session->read_buf_len = 0;
82
83         session->read_msg_buf = g_string_sized_new(1024);
84         session->read_data_buf = g_byte_array_new();
85
86         session->write_buf = NULL;
87         session->write_buf_p = NULL;
88         session->write_buf_len = 0;
89
90         session->write_data = NULL;
91         session->write_data_p = NULL;
92         session->write_data_len = 0;
93
94         session->timeout_tag = 0;
95         session->timeout_interval = 0;
96
97         session->data = NULL;
98 }
99
100 /*!
101  *\brief        Set up parent and child process
102  *              Childloop: Read commands from parent,
103  *              send to server, get answer, pass to parent
104  *
105  *\param        session Contains session information
106  *              server to connect to
107  *              port to connect to
108  *
109  *\return        0 : success
110  *              -1 : pipe / fork errors (parent)
111  *               1 : connection error (child)
112  */
113 gint session_connect(Session *session, const gchar *server, gushort port)
114 {
115 #ifdef G_OS_UNIX
116         session->server = g_strdup(server);
117         session->port = port;
118
119         session->conn_id = sock_connect_async(server, port, session_connect_cb,
120                                               session);
121         if (session->conn_id < 0) {
122                 g_warning("can't connect to server.");
123                 session_close(session);
124                 return -1;
125         }
126
127         return 0;
128 #else
129         SockInfo *sock;
130
131         session->server = g_strdup(server);
132         session->port = port;
133
134         sock = sock_connect(server, port);
135         if (sock == NULL) {
136                 g_warning("can't connect to server.");
137                 session_close(session);
138                 return -1;
139         }
140
141         return session_connect_cb(sock, session);
142 #endif
143 }
144
145 static gint session_connect_cb(SockInfo *sock, gpointer data)
146 {
147         Session *session = SESSION(data);
148
149         session->conn_id = 0;
150
151         if (!sock) {
152                 g_warning("can't connect to server.");
153                 session->state = SESSION_ERROR;
154                 return -1;
155         }
156
157         session->sock = sock;
158
159 #if USE_OPENSSL
160         if (session->ssl_type == SSL_TUNNEL) {
161                 sock_set_nonblocking_mode(sock, FALSE);
162                 if (!ssl_init_socket(sock)) {
163                         g_warning("can't initialize SSL.");
164                         session->state = SESSION_ERROR;
165                         return -1;
166                 }
167         }
168 #endif
169
170         sock_set_nonblocking_mode(sock, session->nonblocking);
171
172         debug_print("session (%p): connected\n", session);
173
174         session->state = SESSION_RECV;
175         session->io_tag = sock_add_watch(session->sock, G_IO_IN,
176                                          session_read_msg_cb,
177                                          session);
178
179         return 0;
180 }
181
182 /*!
183  *\brief        child and parent: send DISCONNECT message to other process
184  *
185  *\param        session Contains session information
186  *
187  *\return        0 : success
188  */
189 gint session_disconnect(Session *session)
190 {
191         session_close(session);
192         return 0;
193 }
194
195 /*!
196  *\brief        parent ?
197  *
198  *\param        session Contains session information
199  */
200 void session_destroy(Session *session)
201 {
202         g_return_if_fail(session != NULL);
203         g_return_if_fail(session->destroy != NULL);
204
205         session_close(session);
206         session->destroy(session);
207         g_free(session->server);
208         g_string_free(session->read_msg_buf, TRUE);
209         g_byte_array_free(session->read_data_buf, TRUE);
210         g_free(session->read_data_terminator);
211         g_free(session->write_buf);
212
213         debug_print("session (%p): destroyed\n", session);
214
215         g_free(session);
216 }
217
218 gboolean session_is_connected(Session *session)
219 {
220         return (session->state == SESSION_READY ||
221                 session->state == SESSION_SEND ||
222                 session->state == SESSION_RECV);
223 }
224
225 void session_set_access_time(Session *session)
226 {
227         session->last_access_time = time(NULL);
228 }
229
230 void session_set_timeout(Session *session, guint interval)
231 {
232         if (session->timeout_tag > 0)
233                 g_source_remove(session->timeout_tag);
234
235         session->timeout_interval = interval;
236         if (interval > 0)
237                 session->timeout_tag =
238                         g_timeout_add(interval, session_timeout_cb, session);
239         else
240                 session->timeout_tag = 0;
241 }
242
243 static gboolean session_timeout_cb(gpointer data)
244 {
245         Session *session = SESSION(data);
246
247         g_warning("session timeout.\n");
248
249         if (session->io_tag > 0) {
250                 g_source_remove(session->io_tag);
251                 session->io_tag = 0;
252         }
253
254         session->timeout_tag = 0;
255         session->state = SESSION_TIMEOUT;
256
257         return FALSE;
258 }
259
260 void session_set_recv_message_notify(Session *session,
261                                      RecvMsgNotify notify_func, gpointer data)
262 {
263         session->recv_msg_notify = notify_func;
264         session->recv_msg_notify_data = data;
265 }
266
267 void session_set_recv_data_progressive_notify
268                                         (Session *session,
269                                          RecvDataProgressiveNotify notify_func,
270                                          gpointer data)
271 {
272         session->recv_data_progressive_notify = notify_func,
273         session->recv_data_progressive_notify_data = data;
274 }
275
276 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
277                                   gpointer data)
278 {
279         session->recv_data_notify = notify_func;
280         session->recv_data_notify_data = data;
281 }
282
283 void session_set_send_data_progressive_notify
284                                         (Session *session,
285                                          SendDataProgressiveNotify notify_func,
286                                          gpointer data)
287 {
288         session->send_data_progressive_notify = notify_func;
289         session->send_data_progressive_notify_data = data;
290 }
291
292 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
293                                   gpointer data)
294 {
295         session->send_data_notify = notify_func;
296         session->send_data_notify_data = data;
297 }
298
299 /*!
300  *\brief        child and parent cleanup (child closes first)
301  *
302  *\param        session Contains session information
303  *
304  *\return        0 : success
305  */
306 static gint session_close(Session *session)
307 {
308         g_return_val_if_fail(session != NULL, -1);
309
310 #ifdef G_OS_UNIX
311         if (session->conn_id > 0) {
312                 sock_connect_async_cancel(session->conn_id);
313                 session->conn_id = 0;
314                 debug_print("session (%p): connection cancelled\n", session);
315         }
316 #endif
317
318         session_set_timeout(session, 0);
319
320         if (session->io_tag > 0) {
321                 g_source_remove(session->io_tag);
322                 session->io_tag = 0;
323         }
324
325         if (session->sock) {
326                 sock_close(session->sock);
327                 session->sock = NULL;
328                 session->state = SESSION_DISCONNECTED;
329                 debug_print("session (%p): closed\n", session);
330         }
331
332         return 0;
333 }
334
335 #if USE_OPENSSL
336 gint session_start_tls(Session *session)
337 {
338         gboolean nb_mode;
339
340         nb_mode = sock_is_nonblocking_mode(session->sock);
341
342         if (nb_mode)
343                 sock_set_nonblocking_mode(session->sock, FALSE);
344
345         if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
346                 g_warning("couldn't start TLS session.\n");
347                 if (nb_mode)
348                         sock_set_nonblocking_mode(session->sock, TRUE);
349                 return -1;
350         }
351
352         if (nb_mode)
353                 sock_set_nonblocking_mode(session->sock, session->nonblocking);
354
355         return 0;
356 }
357 #endif
358
359 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
360 {
361         gboolean ret;
362
363         g_return_val_if_fail(session->write_buf == NULL, -1);
364         g_return_val_if_fail(msg != NULL, -1);
365         g_return_val_if_fail(msg[0] != '\0', -1);
366
367         session->state = SESSION_SEND;
368         session->write_buf = g_strconcat(msg, "\r\n", NULL);
369         session->write_buf_p = session->write_buf;
370         session->write_buf_len = strlen(msg) + 2;
371
372         ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
373
374         if (ret == TRUE)
375                 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
376                                                  session_write_msg_cb, session);
377         else if (session->state == SESSION_ERROR)
378                 return -1;
379
380         return 0;
381 }
382
383 gint session_recv_msg(Session *session)
384 {
385         g_return_val_if_fail(session->read_msg_buf->len == 0, -1);
386
387         session->state = SESSION_RECV;
388
389         if (session->read_buf_len > 0)
390                 g_idle_add(session_recv_msg_idle_cb, session);
391         else
392                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
393                                                  session_read_msg_cb, session);
394
395         return 0;
396 }
397
398 static gboolean session_recv_msg_idle_cb(gpointer data)
399 {
400         Session *session = SESSION(data);
401         gboolean ret;
402
403         ret = session_read_msg_cb(session->sock, G_IO_IN, session);
404
405         if (ret == TRUE)
406                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
407                                                  session_read_msg_cb, session);
408
409         return FALSE;
410 }
411
412 /*!
413  *\brief        parent (child?): send data to other process
414  *
415  *\param        session Contains session information
416  *              data Data to send
417  *              size Bytes to send
418  *
419  *\return        0 : success
420  *              -1 : error
421  */
422 gint session_send_data(Session *session, const guchar *data, guint size)
423 {
424         gboolean ret;
425
426         g_return_val_if_fail(session->write_data == NULL, -1);
427         g_return_val_if_fail(data != NULL, -1);
428         g_return_val_if_fail(size != 0, -1);
429
430         session->state = SESSION_SEND;
431
432         session->write_data = data;
433         session->write_data_p = session->write_data;
434         session->write_data_len = size;
435         g_get_current_time(&session->tv_prev);
436
437         ret = session_write_data_cb(session->sock, G_IO_OUT, session);
438
439         if (ret == TRUE)
440                 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
441                                                  session_write_data_cb,
442                                                  session);
443         else if (session->state == SESSION_ERROR)
444                 return -1;
445
446         return 0;
447 }
448
449 gint session_recv_data(Session *session, guint size, const gchar *terminator)
450 {
451         g_return_val_if_fail(session->read_data_buf->len == 0, -1);
452
453         session->state = SESSION_RECV;
454
455         g_free(session->read_data_terminator);
456         session->read_data_terminator = g_strdup(terminator);
457         g_get_current_time(&session->tv_prev);
458
459         if (session->read_buf_len > 0)
460                 g_idle_add(session_recv_data_idle_cb, session);
461         else
462                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
463                                                  session_read_data_cb, session);
464
465         return 0;
466 }
467
468 static gboolean session_recv_data_idle_cb(gpointer data)
469 {
470         Session *session = SESSION(data);
471         gboolean ret;
472
473         ret = session_read_data_cb(session->sock, G_IO_IN, session);
474
475         if (ret == TRUE)
476                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
477                                                  session_read_data_cb, session);
478
479         return FALSE;
480 }
481
482 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
483                                     gpointer data)
484 {
485         Session *session = SESSION(data);
486         gchar buf[SESSION_BUFFSIZE];
487         gint line_len;
488         gchar *newline;
489         gchar *msg;
490         gint ret;
491
492         g_return_val_if_fail(condition == G_IO_IN, FALSE);
493
494         session_set_timeout(session, session->timeout_interval);
495
496         if (session->read_buf_len == 0) {
497                 gint read_len;
498
499                 read_len = sock_read(session->sock, session->read_buf,
500                                      SESSION_BUFFSIZE - 1);
501
502                 if (read_len == -1 && session->state == SESSION_DISCONNECTED) {
503                         g_warning ("sock_read: session disconnected\n");
504                         if (session->io_tag > 0) {
505                                 g_source_remove(session->io_tag);
506                                 session->io_tag = 0;
507                         }
508                         return FALSE;
509                 }
510                 
511                 if (read_len == 0) {
512                         g_warning("sock_read: received EOF\n");
513                         session->state = SESSION_EOF;
514                         return FALSE;
515                 }
516
517                 if (read_len < 0) {
518                         switch (errno) {
519                         case EAGAIN:
520                                 return TRUE;
521                         default:
522                                 g_warning("sock_read: %s\n", g_strerror(errno));
523                                 session->state = SESSION_ERROR;
524                                 return FALSE;
525                         }
526                 }
527
528                 session->read_buf_len = read_len;
529         }
530
531         if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
532                 != NULL)
533                 line_len = newline - session->read_buf_p + 1;
534         else
535                 line_len = session->read_buf_len;
536
537         if (line_len == 0)
538                 return TRUE;
539
540         memcpy(buf, session->read_buf_p, line_len);
541         buf[line_len] = '\0';
542
543         g_string_append(session->read_msg_buf, buf);
544
545         session->read_buf_len -= line_len;
546         if (session->read_buf_len == 0)
547                 session->read_buf_p = session->read_buf;
548         else
549                 session->read_buf_p += line_len;
550
551         /* incomplete read */
552         if (buf[line_len - 1] != '\n')
553                 return TRUE;
554
555         /* complete */
556         if (session->io_tag > 0) {
557                 g_source_remove(session->io_tag);
558                 session->io_tag = 0;
559         }
560
561         /* callback */
562         msg = g_strdup(session->read_msg_buf->str);
563         strretchomp(msg);
564         g_string_truncate(session->read_msg_buf, 0);
565
566         ret = session->recv_msg(session, msg);
567         session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
568
569         g_free(msg);
570
571         if (ret < 0)
572                 session->state = SESSION_ERROR;
573
574         return FALSE;
575 }
576
577 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
578                                      gpointer data)
579 {
580         Session *session = SESSION(data);
581         GByteArray *data_buf;
582         gint terminator_len;
583         gboolean complete = FALSE;
584         guint data_len;
585         gint ret;
586
587         g_return_val_if_fail(condition == G_IO_IN, FALSE);
588
589         session_set_timeout(session, session->timeout_interval);
590
591         if (session->read_buf_len == 0) {
592                 gint read_len;
593
594                 read_len = sock_read(session->sock, session->read_buf,
595                                      SESSION_BUFFSIZE);
596
597                 if (read_len == 0) {
598                         g_warning("sock_read: received EOF\n");
599                         session->state = SESSION_EOF;
600                         return FALSE;
601                 }
602
603                 if (read_len < 0) {
604                         switch (errno) {
605                         case EAGAIN:
606                                 return TRUE;
607                         default:
608                                 g_warning("sock_read: %s\n", g_strerror(errno));
609                                 session->state = SESSION_ERROR;
610                                 return FALSE;
611                         }
612                 }
613
614                 session->read_buf_len = read_len;
615         }
616
617         data_buf = session->read_data_buf;
618         terminator_len = strlen(session->read_data_terminator);
619
620         if (session->read_buf_len == 0)
621                 return TRUE;
622
623         g_byte_array_append(data_buf, session->read_buf_p,
624                             session->read_buf_len);
625
626         session->read_buf_len = 0;
627         session->read_buf_p = session->read_buf;
628
629         /* check if data is terminated */
630         if (data_buf->len >= terminator_len) {
631                 if (memcmp(data_buf->data, session->read_data_terminator,
632                            terminator_len) == 0)
633                         complete = TRUE;
634                 else if (data_buf->len >= terminator_len + 2 &&
635                          memcmp(data_buf->data + data_buf->len -
636                                 (terminator_len + 2), "\r\n", 2) == 0 &&
637                          memcmp(data_buf->data + data_buf->len -
638                                 terminator_len, session->read_data_terminator,
639                                 terminator_len) == 0)
640                         complete = TRUE;
641         }
642
643         /* incomplete read */
644         if (!complete) {
645                 GTimeVal tv_cur;
646
647                 g_get_current_time(&tv_cur);
648                 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
649                     tv_cur.tv_usec - session->tv_prev.tv_usec >
650                     UI_REFRESH_INTERVAL) {
651                         session->recv_data_progressive_notify
652                                 (session, data_buf->len, 0,
653                                  session->recv_data_progressive_notify_data);
654                         g_get_current_time(&session->tv_prev);
655                 }
656                 return TRUE;
657         }
658
659         /* complete */
660         if (session->io_tag > 0) {
661                 g_source_remove(session->io_tag);
662                 session->io_tag = 0;
663         }
664
665         data_len = data_buf->len - terminator_len;
666
667         /* callback */
668         ret = session->recv_data_finished(session, (gchar *)data_buf->data,
669                                           data_len);
670
671         g_byte_array_set_size(data_buf, 0);
672
673         session->recv_data_notify(session, data_len,
674                                   session->recv_data_notify_data);
675
676         if (ret < 0)
677                 session->state = SESSION_ERROR;
678
679         return FALSE;
680 }
681
682 static gint session_write_buf(Session *session)
683 {
684         gint write_len;
685         gint to_write_len;
686
687         g_return_val_if_fail(session->write_buf != NULL, -1);
688         g_return_val_if_fail(session->write_buf_p != NULL, -1);
689         g_return_val_if_fail(session->write_buf_len > 0, -1);
690
691         to_write_len = session->write_buf_len -
692                 (session->write_buf_p - session->write_buf);
693         to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
694
695         write_len = sock_write(session->sock, session->write_buf_p,
696                                to_write_len);
697
698         if (write_len < 0) {
699                 switch (errno) {
700                 case EAGAIN:
701                         write_len = 0;
702                         break;
703                 default:
704                         g_warning("sock_write: %s\n", g_strerror(errno));
705                         session->state = SESSION_ERROR;
706                         return -1;
707                 }
708         }
709
710         /* incomplete write */
711         if (session->write_buf_p - session->write_buf + write_len <
712             session->write_buf_len) {
713                 session->write_buf_p += write_len;
714                 return 1;
715         }
716
717         g_free(session->write_buf);
718         session->write_buf = NULL;
719         session->write_buf_p = NULL;
720         session->write_buf_len = 0;
721
722         return 0;
723 }
724
725 static gint session_write_data(Session *session)
726 {
727         gint write_len;
728         gint to_write_len;
729
730         g_return_val_if_fail(session->write_data != NULL, -1);
731         g_return_val_if_fail(session->write_data_p != NULL, -1);
732         g_return_val_if_fail(session->write_data_len > 0, -1);
733
734         to_write_len = session->write_data_len -
735                 (session->write_data_p - session->write_data);
736         to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
737
738         write_len = sock_write(session->sock, session->write_data_p,
739                                to_write_len);
740
741         if (write_len < 0) {
742                 switch (errno) {
743                 case EAGAIN:
744                         write_len = 0;
745                         break;
746                 default:
747                         g_warning("sock_write: %s\n", g_strerror(errno));
748                         session->state = SESSION_ERROR;
749                         return -1;
750                 }
751         }
752
753         /* incomplete write */
754         if (session->write_data_p - session->write_data + write_len <
755             session->write_data_len) {
756                 session->write_data_p += write_len;
757                 return 1;
758         }
759
760         session->write_data = NULL;
761         session->write_data_p = NULL;
762         session->write_data_len = 0;
763
764         return 0;
765 }
766
767 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
768                                      gpointer data)
769 {
770         Session *session = SESSION(data);
771         gint ret;
772
773         g_return_val_if_fail(condition == G_IO_OUT, FALSE);
774         g_return_val_if_fail(session->write_buf != NULL, FALSE);
775         g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
776         g_return_val_if_fail(session->write_buf_len > 0, FALSE);
777
778         ret = session_write_buf(session);
779
780         if (ret < 0) {
781                 session->state = SESSION_ERROR;
782                 return FALSE;
783         } else if (ret > 0)
784                 return TRUE;
785
786         if (session->io_tag > 0) {
787                 g_source_remove(session->io_tag);
788                 session->io_tag = 0;
789         }
790
791         session_recv_msg(session);
792
793         return FALSE;
794 }
795
796 static gboolean session_write_data_cb(SockInfo *source,
797                                       GIOCondition condition, gpointer data)
798 {
799         Session *session = SESSION(data);
800         guint write_data_len;
801         gint ret;
802
803         g_return_val_if_fail(condition == G_IO_OUT, FALSE);
804         g_return_val_if_fail(session->write_data != NULL, FALSE);
805         g_return_val_if_fail(session->write_data_p != NULL, FALSE);
806         g_return_val_if_fail(session->write_data_len > 0, FALSE);
807
808         write_data_len = session->write_data_len;
809
810         ret = session_write_data(session);
811
812         if (ret < 0) {
813                 session->state = SESSION_ERROR;
814                 return FALSE;
815         } else if (ret > 0) {
816                 GTimeVal tv_cur;
817
818                 g_get_current_time(&tv_cur);
819                 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
820                     tv_cur.tv_usec - session->tv_prev.tv_usec >
821                     UI_REFRESH_INTERVAL) {
822                         session_set_timeout(session, session->timeout_interval);
823                         session->send_data_progressive_notify
824                                 (session,
825                                  session->write_data_p - session->write_data,
826                                  write_data_len,
827                                  session->send_data_progressive_notify_data);
828                         g_get_current_time(&session->tv_prev);
829                 }
830                 return TRUE;
831         }
832
833         if (session->io_tag > 0) {
834                 g_source_remove(session->io_tag);
835                 session->io_tag = 0;
836         }
837
838         /* callback */
839         ret = session->send_data_finished(session, write_data_len);
840         session->send_data_notify(session, write_data_len,
841                                   session->send_data_notify_data);
842
843         return FALSE;
844 }