2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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_recv_msg_idle_cb (gpointer data);
46 static gboolean session_recv_data_idle_cb (gpointer data);
48 static gboolean session_read_msg_cb (SockInfo *source,
49 GIOCondition condition,
51 static gboolean session_read_data_cb (SockInfo *source,
52 GIOCondition condition,
54 static gboolean session_write_msg_cb (SockInfo *source,
55 GIOCondition condition,
57 static gboolean session_write_data_cb (SockInfo *source,
58 GIOCondition condition,
62 void session_init(Session *session)
64 session->type = SESSION_UNKNOWN;
66 session->server = NULL;
69 session->ssl_type = SSL_NONE;
71 session->nonblocking = TRUE;
72 session->state = SESSION_READY;
73 session->last_access_time = time(NULL);
75 gettimeofday(&session->tv_prev, NULL);
81 session->read_buf_p = session->read_buf;
82 session->read_buf_len = 0;
84 session->read_msg_buf = g_string_sized_new(1024);
85 session->read_data_buf = g_byte_array_new();
87 session->write_buf = NULL;
88 session->write_buf_p = NULL;
89 session->write_buf_len = 0;
95 *\brief Set up parent and child process
96 * Childloop: Read commands from parent,
97 * send to server, get answer, pass to parent
99 *\param session Contains session information
100 * server to connect to
104 * -1 : pipe / fork errors (parent)
105 * 1 : connection error (child)
107 gint session_connect(Session *session, const gchar *server, gushort port)
109 session->server = g_strdup(server);
110 session->port = port;
112 session->conn_id = sock_connect_async(server, port, session_connect_cb,
114 if (session->conn_id < 0) {
115 g_warning("can't connect to server.");
116 session_close(session);
123 static gint session_connect_cb(SockInfo *sock, gpointer data)
125 Session *session = SESSION(data);
127 session->conn_id = 0;
130 g_warning("can't connect to server.");
131 session->state = SESSION_ERROR;
135 session->sock = sock;
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;
148 sock_set_nonblocking_mode(sock, session->nonblocking);
150 debug_print("session (%p): connected\n", session);
152 session->state = SESSION_RECV;
153 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
161 *\brief child and parent: send DISCONNECT message to other process
163 *\param session Contains session information
167 gint session_disconnect(Session *session)
169 session_close(session);
176 *\param session Contains session information
178 void session_destroy(Session *session)
180 g_return_if_fail(session != NULL);
181 g_return_if_fail(session->destroy != NULL);
183 session_close(session);
184 session->destroy(session);
185 g_free(session->server);
186 g_string_free(session->read_msg_buf, TRUE);
187 g_byte_array_free(session->read_data_buf, TRUE);
188 g_free(session->read_data_terminator);
189 g_free(session->write_buf);
191 debug_print("session (%p): destroyed\n", session);
196 gboolean session_is_connected(Session *session)
198 return (session->state == SESSION_READY ||
199 session->state == SESSION_SEND ||
200 session->state == SESSION_RECV);
203 void session_set_recv_message_notify(Session *session,
204 RecvMsgNotify notify_func, gpointer data)
206 session->recv_msg_notify = notify_func;
207 session->recv_msg_notify_data = data;
210 void session_set_recv_data_progressive_notify
212 RecvDataProgressiveNotify notify_func,
215 session->recv_data_progressive_notify = notify_func,
216 session->recv_data_progressive_notify_data = data;
219 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
222 session->recv_data_notify = notify_func;
223 session->recv_data_notify_data = data;
226 void session_set_send_data_progressive_notify
228 SendDataProgressiveNotify notify_func,
231 session->send_data_progressive_notify = notify_func;
232 session->send_data_progressive_notify_data = data;
235 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
238 session->send_data_notify = notify_func;
239 session->send_data_notify_data = data;
243 *\brief child and parent cleanup (child closes first)
245 *\param session Contains session information
249 static gint session_close(Session *session)
251 g_return_val_if_fail(session != NULL, -1);
253 if (session->conn_id > 0) {
254 sock_connect_async_cancel(session->conn_id);
255 session->conn_id = 0;
258 if (session->io_tag > 0) {
259 g_source_remove(session->io_tag);
264 sock_close(session->sock);
265 session->sock = NULL;
266 session->state = SESSION_DISCONNECTED;
269 debug_print("session (%p): closed\n", session);
275 gint session_start_tls(Session *session)
279 nb_mode = sock_is_nonblocking_mode(session->sock);
282 sock_set_nonblocking_mode(session->sock, FALSE);
284 if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
285 g_warning("can't start TLS session.\n");
287 sock_set_nonblocking_mode(session->sock, TRUE);
292 sock_set_nonblocking_mode(session->sock, session->nonblocking);
298 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
302 g_return_val_if_fail(session->write_buf == NULL, -1);
303 g_return_val_if_fail(msg != NULL, -1);
304 g_return_val_if_fail(msg[0] != '\0', -1);
306 session->state = SESSION_SEND;
307 session->write_buf = g_strconcat(msg, "\r\n", NULL);
308 session->write_buf_p = session->write_buf;
309 session->write_buf_len = strlen(msg) + 2;
311 ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
314 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
315 session_write_msg_cb, session);
316 else if (session->state == SESSION_ERROR)
322 gint session_recv_msg(Session *session)
324 g_return_val_if_fail(session->read_msg_buf->len == 0, -1);
326 session->state = SESSION_RECV;
328 if (session->read_buf_len > 0)
329 g_idle_add(session_recv_msg_idle_cb, session);
331 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
332 session_read_msg_cb, session);
337 static gboolean session_recv_msg_idle_cb(gpointer data)
339 Session *session = SESSION(data);
342 ret = session_read_msg_cb(session->sock, G_IO_IN, session);
345 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
346 session_read_msg_cb, session);
352 *\brief parent (child?): send data to other process
354 *\param session Contains session information
361 gint session_send_data(Session *session, const guchar *data, guint size)
365 g_return_val_if_fail(session->write_buf == NULL, -1);
366 g_return_val_if_fail(data != NULL, -1);
367 g_return_val_if_fail(size != 0, -1);
369 session->state = SESSION_SEND;
371 session->write_buf = g_malloc(size);
372 session->write_buf_p = session->write_buf;
373 memcpy(session->write_buf, data, size);
374 session->write_buf_len = size;
375 gettimeofday(&session->tv_prev, NULL);
377 ret = session_write_data_cb(session->sock, G_IO_OUT, session);
380 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
381 session_write_data_cb,
383 else if (session->state == SESSION_ERROR)
389 gint session_recv_data(Session *session, guint size, const gchar *terminator)
391 g_return_val_if_fail(session->read_data_buf->len == 0, -1);
393 session->state = SESSION_RECV;
395 g_free(session->read_data_terminator);
396 session->read_data_terminator = g_strdup(terminator);
397 gettimeofday(&session->tv_prev, NULL);
399 if (session->read_buf_len > 0)
400 g_idle_add(session_recv_data_idle_cb, session);
402 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
403 session_read_data_cb, session);
408 static gboolean session_recv_data_idle_cb(gpointer data)
410 Session *session = SESSION(data);
413 ret = session_read_data_cb(session->sock, G_IO_IN, session);
416 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
417 session_read_data_cb, session);
422 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
425 Session *session = SESSION(data);
426 gchar buf[SESSION_BUFFSIZE];
432 g_return_val_if_fail(condition == G_IO_IN, FALSE);
434 if (session->read_buf_len == 0) {
437 read_len = sock_read(session->sock, session->read_buf,
438 SESSION_BUFFSIZE - 1);
440 if (read_len == -1 && session->state == SESSION_DISCONNECTED) {
441 g_warning ("sock_read: session disconnected\n");
442 if (session->io_tag > 0) {
443 g_source_remove(session->io_tag);
450 g_warning("sock_read: received EOF\n");
451 session->state = SESSION_EOF;
460 g_warning("sock_read: %s\n", g_strerror(errno));
461 session->state = SESSION_ERROR;
466 session->read_buf_len = read_len;
469 if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
471 line_len = newline - session->read_buf_p + 1;
473 line_len = session->read_buf_len;
478 memcpy(buf, session->read_buf_p, line_len);
479 buf[line_len] = '\0';
481 g_string_append(session->read_msg_buf, buf);
483 session->read_buf_len -= line_len;
484 if (session->read_buf_len == 0)
485 session->read_buf_p = session->read_buf;
487 session->read_buf_p += line_len;
489 /* incomplete read */
490 if (buf[line_len - 1] != '\n')
494 if (session->io_tag > 0) {
495 g_source_remove(session->io_tag);
500 msg = g_strdup(session->read_msg_buf->str);
502 g_string_truncate(session->read_msg_buf, 0);
504 ret = session->recv_msg(session, msg);
505 session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
510 session->state = SESSION_ERROR;
515 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
518 Session *session = SESSION(data);
519 GByteArray *data_buf;
521 gboolean complete = FALSE;
525 g_return_val_if_fail(condition == G_IO_IN, FALSE);
527 if (session->read_buf_len == 0) {
530 read_len = sock_read(session->sock, session->read_buf,
534 g_warning("sock_read: received EOF\n");
535 session->state = SESSION_EOF;
544 g_warning("sock_read: %s\n", g_strerror(errno));
545 session->state = SESSION_ERROR;
550 session->read_buf_len = read_len;
553 data_buf = session->read_data_buf;
554 terminator_len = strlen(session->read_data_terminator);
556 if (session->read_buf_len == 0)
559 g_byte_array_append(data_buf, session->read_buf_p,
560 session->read_buf_len);
562 session->read_buf_len = 0;
563 session->read_buf_p = session->read_buf;
565 /* check if data is terminated */
566 if (data_buf->len >= terminator_len) {
567 if (memcmp(data_buf->data, session->read_data_terminator,
568 terminator_len) == 0)
570 else if (data_buf->len >= terminator_len + 2 &&
571 memcmp(data_buf->data + data_buf->len -
572 (terminator_len + 2), "\r\n", 2) == 0 &&
573 memcmp(data_buf->data + data_buf->len -
574 terminator_len, session->read_data_terminator,
575 terminator_len) == 0)
579 /* incomplete read */
581 struct timeval tv_cur;
583 gettimeofday(&tv_cur, NULL);
584 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
585 tv_cur.tv_usec - session->tv_prev.tv_usec >
586 UI_REFRESH_INTERVAL) {
587 session->recv_data_progressive_notify
588 (session, data_buf->len, 0,
589 session->recv_data_progressive_notify_data);
590 gettimeofday(&session->tv_prev, NULL);
596 if (session->io_tag > 0) {
597 g_source_remove(session->io_tag);
601 data_len = data_buf->len - terminator_len;
604 ret = session->recv_data_finished(session, (gchar *)data_buf->data,
607 g_byte_array_set_size(data_buf, 0);
609 session->recv_data_notify(session, data_len,
610 session->recv_data_notify_data);
613 session->state = SESSION_ERROR;
618 static gint session_write_buf(Session *session)
623 g_return_val_if_fail(session->write_buf != NULL, -1);
624 g_return_val_if_fail(session->write_buf_p != NULL, -1);
625 g_return_val_if_fail(session->write_buf_len > 0, -1);
627 to_write_len = session->write_buf_len -
628 (session->write_buf_p - session->write_buf);
629 to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
631 write_len = sock_write(session->sock, session->write_buf_p,
640 g_warning("sock_write: %s\n", g_strerror(errno));
641 session->state = SESSION_ERROR;
646 /* incomplete write */
647 if (session->write_buf_p - session->write_buf + write_len <
648 session->write_buf_len) {
649 session->write_buf_p += write_len;
653 g_free(session->write_buf);
654 session->write_buf = NULL;
655 session->write_buf_p = NULL;
656 session->write_buf_len = 0;
661 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
664 Session *session = SESSION(data);
667 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
668 g_return_val_if_fail(session->write_buf != NULL, FALSE);
669 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
670 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
672 ret = session_write_buf(session);
675 session->state = SESSION_ERROR;
680 if (session->io_tag > 0) {
681 g_source_remove(session->io_tag);
685 session_recv_msg(session);
690 static gboolean session_write_data_cb(SockInfo *source,
691 GIOCondition condition, gpointer data)
693 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 write_buf_len = session->write_buf_len;
704 ret = session_write_buf(session);
707 session->state = SESSION_ERROR;
709 } else if (ret > 0) {
710 struct timeval tv_cur;
712 gettimeofday(&tv_cur, NULL);
713 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
714 tv_cur.tv_usec - session->tv_prev.tv_usec >
715 UI_REFRESH_INTERVAL) {
716 session->send_data_progressive_notify
718 session->write_buf_p - session->write_buf,
720 session->send_data_progressive_notify_data);
721 gettimeofday(&session->tv_prev, NULL);
726 if (session->io_tag > 0) {
727 g_source_remove(session->io_tag);
732 ret = session->send_data_finished(session, write_buf_len);
733 session->send_data_notify(session, write_buf_len,
734 session->send_data_notify_data);