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