2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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.
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.
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.
31 #include <sys/types.h>
33 #include <sys/signal.h>
41 static gint session_connect_cb (SockInfo *sock,
43 static gint session_close (Session *session);
45 static gboolean session_timeout_cb (gpointer data);
47 static gboolean session_recv_msg_idle_cb (gpointer data);
48 static gboolean session_recv_data_idle_cb (gpointer data);
50 static gboolean session_read_msg_cb (SockInfo *source,
51 GIOCondition condition,
53 static gboolean session_read_data_cb (SockInfo *source,
54 GIOCondition condition,
56 static gboolean session_write_msg_cb (SockInfo *source,
57 GIOCondition condition,
59 static gboolean session_write_data_cb (SockInfo *source,
60 GIOCondition condition,
64 void session_init(Session *session)
66 session->type = SESSION_UNKNOWN;
68 session->server = NULL;
71 session->ssl_type = SSL_NONE;
73 session->nonblocking = TRUE;
74 session->state = SESSION_READY;
75 session->last_access_time = time(NULL);
77 gettimeofday(&session->tv_prev, NULL);
83 session->read_buf_p = session->read_buf;
84 session->read_buf_len = 0;
86 session->read_msg_buf = g_string_sized_new(1024);
87 session->read_data_buf = g_byte_array_new();
89 session->write_buf = NULL;
90 session->write_buf_p = NULL;
91 session->write_buf_len = 0;
93 session->timeout_tag = 0;
94 session->timeout_interval = 0;
100 *\brief Set up parent and child process
101 * Childloop: Read commands from parent,
102 * send to server, get answer, pass to parent
104 *\param session Contains session information
105 * server to connect to
109 * -1 : pipe / fork errors (parent)
110 * 1 : connection error (child)
112 gint session_connect(Session *session, const gchar *server, gushort port)
114 session->server = g_strdup(server);
115 session->port = port;
117 session->conn_id = sock_connect_async(server, port, session_connect_cb,
119 if (session->conn_id < 0) {
120 g_warning("can't connect to server.");
121 session_close(session);
128 static gint session_connect_cb(SockInfo *sock, gpointer data)
130 Session *session = SESSION(data);
132 session->conn_id = 0;
135 g_warning("can't connect to server.");
136 session->state = SESSION_ERROR;
140 session->sock = sock;
143 if (session->ssl_type == SSL_TUNNEL) {
144 sock_set_nonblocking_mode(sock, FALSE);
145 if (!ssl_init_socket(sock)) {
146 g_warning("can't initialize SSL.");
147 session->state = SESSION_ERROR;
153 sock_set_nonblocking_mode(sock, session->nonblocking);
155 debug_print("session (%p): connected\n", session);
157 session->state = SESSION_RECV;
158 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
166 *\brief child and parent: send DISCONNECT message to other process
168 *\param session Contains session information
172 gint session_disconnect(Session *session)
174 session_close(session);
181 *\param session Contains session information
183 void session_destroy(Session *session)
185 g_return_if_fail(session != NULL);
186 g_return_if_fail(session->destroy != NULL);
188 session_close(session);
189 session->destroy(session);
190 g_free(session->server);
191 g_string_free(session->read_msg_buf, TRUE);
192 g_byte_array_free(session->read_data_buf, TRUE);
193 g_free(session->read_data_terminator);
194 g_free(session->write_buf);
196 debug_print("session (%p): destroyed\n", session);
201 gboolean session_is_connected(Session *session)
203 return (session->state == SESSION_READY ||
204 session->state == SESSION_SEND ||
205 session->state == SESSION_RECV);
208 void session_set_timeout(Session *session, guint interval)
210 if (session->timeout_tag > 0)
211 g_source_remove(session->timeout_tag);
213 session->timeout_interval = interval;
215 session->timeout_tag =
216 g_timeout_add(interval, session_timeout_cb, session);
219 static gboolean session_timeout_cb(gpointer data)
221 Session *session = SESSION(data);
223 g_warning("session timeout.\n");
225 if (session->io_tag > 0) {
226 g_source_remove(session->io_tag);
230 session->timeout_tag = 0;
231 session->state = SESSION_TIMEOUT;
236 void session_set_recv_message_notify(Session *session,
237 RecvMsgNotify notify_func, gpointer data)
239 session->recv_msg_notify = notify_func;
240 session->recv_msg_notify_data = data;
243 void session_set_recv_data_progressive_notify
245 RecvDataProgressiveNotify notify_func,
248 session->recv_data_progressive_notify = notify_func,
249 session->recv_data_progressive_notify_data = data;
252 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
255 session->recv_data_notify = notify_func;
256 session->recv_data_notify_data = data;
259 void session_set_send_data_progressive_notify
261 SendDataProgressiveNotify notify_func,
264 session->send_data_progressive_notify = notify_func;
265 session->send_data_progressive_notify_data = data;
268 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
271 session->send_data_notify = notify_func;
272 session->send_data_notify_data = data;
276 *\brief child and parent cleanup (child closes first)
278 *\param session Contains session information
282 static gint session_close(Session *session)
284 g_return_val_if_fail(session != NULL, -1);
286 if (session->conn_id > 0) {
287 sock_connect_async_cancel(session->conn_id);
288 session->conn_id = 0;
289 debug_print("session (%p): connection cancelled\n", session);
292 session_set_timeout(session, 0);
294 if (session->io_tag > 0) {
295 g_source_remove(session->io_tag);
300 sock_close(session->sock);
301 session->sock = NULL;
302 session->state = SESSION_DISCONNECTED;
303 debug_print("session (%p): closed\n", session);
310 gint session_start_tls(Session *session)
314 nb_mode = sock_is_nonblocking_mode(session->sock);
317 sock_set_nonblocking_mode(session->sock, FALSE);
319 if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
320 g_warning("can't start TLS session.\n");
322 sock_set_nonblocking_mode(session->sock, TRUE);
327 sock_set_nonblocking_mode(session->sock, session->nonblocking);
333 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
337 g_return_val_if_fail(session->write_buf == NULL, -1);
338 g_return_val_if_fail(msg != NULL, -1);
339 g_return_val_if_fail(msg[0] != '\0', -1);
341 session->state = SESSION_SEND;
342 session->write_buf = g_strconcat(msg, "\r\n", NULL);
343 session->write_buf_p = session->write_buf;
344 session->write_buf_len = strlen(msg) + 2;
346 ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
349 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
350 session_write_msg_cb, session);
351 else if (session->state == SESSION_ERROR)
357 gint session_recv_msg(Session *session)
359 g_return_val_if_fail(session->read_msg_buf->len == 0, -1);
361 session->state = SESSION_RECV;
363 if (session->read_buf_len > 0)
364 g_idle_add(session_recv_msg_idle_cb, session);
366 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
367 session_read_msg_cb, session);
372 static gboolean session_recv_msg_idle_cb(gpointer data)
374 Session *session = SESSION(data);
377 ret = session_read_msg_cb(session->sock, G_IO_IN, session);
380 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
381 session_read_msg_cb, session);
387 *\brief parent (child?): send data to other process
389 *\param session Contains session information
396 gint session_send_data(Session *session, const guchar *data, guint size)
400 g_return_val_if_fail(session->write_buf == NULL, -1);
401 g_return_val_if_fail(data != NULL, -1);
402 g_return_val_if_fail(size != 0, -1);
404 session->state = SESSION_SEND;
406 session->write_buf = g_malloc(size);
407 session->write_buf_p = session->write_buf;
408 memcpy(session->write_buf, data, size);
409 session->write_buf_len = size;
410 gettimeofday(&session->tv_prev, NULL);
412 ret = session_write_data_cb(session->sock, G_IO_OUT, session);
415 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
416 session_write_data_cb,
418 else if (session->state == SESSION_ERROR)
424 gint session_recv_data(Session *session, guint size, const gchar *terminator)
426 g_return_val_if_fail(session->read_data_buf->len == 0, -1);
428 session->state = SESSION_RECV;
430 g_free(session->read_data_terminator);
431 session->read_data_terminator = g_strdup(terminator);
432 gettimeofday(&session->tv_prev, NULL);
434 if (session->read_buf_len > 0)
435 g_idle_add(session_recv_data_idle_cb, session);
437 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
438 session_read_data_cb, session);
443 static gboolean session_recv_data_idle_cb(gpointer data)
445 Session *session = SESSION(data);
448 ret = session_read_data_cb(session->sock, G_IO_IN, session);
451 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
452 session_read_data_cb, session);
457 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
460 Session *session = SESSION(data);
461 gchar buf[SESSION_BUFFSIZE];
467 g_return_val_if_fail(condition == G_IO_IN, FALSE);
469 session_set_timeout(session, session->timeout_interval);
471 if (session->read_buf_len == 0) {
474 read_len = sock_read(session->sock, session->read_buf,
475 SESSION_BUFFSIZE - 1);
478 g_warning("sock_read: received EOF\n");
479 session->state = SESSION_EOF;
488 g_warning("sock_read: %s\n", g_strerror(errno));
489 session->state = SESSION_ERROR;
494 session->read_buf_len = read_len;
497 if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
499 line_len = newline - session->read_buf_p + 1;
501 line_len = session->read_buf_len;
506 memcpy(buf, session->read_buf_p, line_len);
507 buf[line_len] = '\0';
509 g_string_append(session->read_msg_buf, buf);
511 session->read_buf_len -= line_len;
512 if (session->read_buf_len == 0)
513 session->read_buf_p = session->read_buf;
515 session->read_buf_p += line_len;
517 /* incomplete read */
518 if (buf[line_len - 1] != '\n')
522 if (session->io_tag > 0) {
523 g_source_remove(session->io_tag);
528 msg = g_strdup(session->read_msg_buf->str);
530 g_string_truncate(session->read_msg_buf, 0);
532 ret = session->recv_msg(session, msg);
533 session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
538 session->state = SESSION_ERROR;
543 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
546 Session *session = SESSION(data);
547 GByteArray *data_buf;
549 gboolean complete = FALSE;
553 g_return_val_if_fail(condition == G_IO_IN, FALSE);
555 session_set_timeout(session, session->timeout_interval);
557 if (session->read_buf_len == 0) {
560 read_len = sock_read(session->sock, session->read_buf,
564 g_warning("sock_read: received EOF\n");
565 session->state = SESSION_EOF;
574 g_warning("sock_read: %s\n", g_strerror(errno));
575 session->state = SESSION_ERROR;
580 session->read_buf_len = read_len;
583 data_buf = session->read_data_buf;
584 terminator_len = strlen(session->read_data_terminator);
586 if (session->read_buf_len == 0)
589 g_byte_array_append(data_buf, session->read_buf_p,
590 session->read_buf_len);
592 session->read_buf_len = 0;
593 session->read_buf_p = session->read_buf;
595 /* check if data is terminated */
596 if (data_buf->len >= terminator_len) {
597 if (memcmp(data_buf->data, session->read_data_terminator,
598 terminator_len) == 0)
600 else if (data_buf->len >= terminator_len + 2 &&
601 memcmp(data_buf->data + data_buf->len -
602 (terminator_len + 2), "\r\n", 2) == 0 &&
603 memcmp(data_buf->data + data_buf->len -
604 terminator_len, session->read_data_terminator,
605 terminator_len) == 0)
609 /* incomplete read */
611 struct timeval tv_cur;
613 gettimeofday(&tv_cur, NULL);
614 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
615 tv_cur.tv_usec - session->tv_prev.tv_usec >
616 UI_REFRESH_INTERVAL) {
617 session->recv_data_progressive_notify
618 (session, data_buf->len, 0,
619 session->recv_data_progressive_notify_data);
620 gettimeofday(&session->tv_prev, NULL);
626 if (session->io_tag > 0) {
627 g_source_remove(session->io_tag);
631 data_len = data_buf->len - terminator_len;
634 ret = session->recv_data_finished(session, (gchar *)data_buf->data,
637 g_byte_array_set_size(data_buf, 0);
639 session->recv_data_notify(session, data_len,
640 session->recv_data_notify_data);
643 session->state = SESSION_ERROR;
648 static gint session_write_buf(Session *session)
653 g_return_val_if_fail(session->write_buf != NULL, -1);
654 g_return_val_if_fail(session->write_buf_p != NULL, -1);
655 g_return_val_if_fail(session->write_buf_len > 0, -1);
657 to_write_len = session->write_buf_len -
658 (session->write_buf_p - session->write_buf);
659 to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
661 write_len = sock_write(session->sock, session->write_buf_p,
670 g_warning("sock_write: %s\n", g_strerror(errno));
671 session->state = SESSION_ERROR;
676 /* incomplete write */
677 if (session->write_buf_p - session->write_buf + write_len <
678 session->write_buf_len) {
679 session->write_buf_p += write_len;
683 g_free(session->write_buf);
684 session->write_buf = NULL;
685 session->write_buf_p = NULL;
686 session->write_buf_len = 0;
691 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
694 Session *session = SESSION(data);
697 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
698 g_return_val_if_fail(session->write_buf != NULL, FALSE);
699 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
700 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
702 ret = session_write_buf(session);
705 session->state = SESSION_ERROR;
710 if (session->io_tag > 0) {
711 g_source_remove(session->io_tag);
715 session_recv_msg(session);
720 static gboolean session_write_data_cb(SockInfo *source,
721 GIOCondition condition, gpointer data)
723 Session *session = SESSION(data);
727 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
728 g_return_val_if_fail(session->write_buf != NULL, FALSE);
729 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
730 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
732 write_buf_len = session->write_buf_len;
734 ret = session_write_buf(session);
737 session->state = SESSION_ERROR;
739 } else if (ret > 0) {
740 struct timeval tv_cur;
742 gettimeofday(&tv_cur, NULL);
743 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
744 tv_cur.tv_usec - session->tv_prev.tv_usec >
745 UI_REFRESH_INTERVAL) {
746 session_set_timeout(session, session->timeout_interval);
747 session->send_data_progressive_notify
749 session->write_buf_p - session->write_buf,
751 session->send_data_progressive_notify_data);
752 gettimeofday(&session->tv_prev, NULL);
757 if (session->io_tag > 0) {
758 g_source_remove(session->io_tag);
763 ret = session->send_data_finished(session, write_buf_len);
764 session->send_data_notify(session, write_buf_len,
765 session->send_data_notify_data);