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