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