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");
446 g_warning("sock_read: received EOF\n");
447 session->state = SESSION_EOF;
456 g_warning("sock_read: %s\n", g_strerror(errno));
457 session->state = SESSION_ERROR;
462 session->read_buf_len = read_len;
465 if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
467 line_len = newline - session->read_buf_p + 1;
469 line_len = session->read_buf_len;
474 memcpy(buf, session->read_buf_p, line_len);
475 buf[line_len] = '\0';
477 g_string_append(session->read_msg_buf, buf);
479 session->read_buf_len -= line_len;
480 if (session->read_buf_len == 0)
481 session->read_buf_p = session->read_buf;
483 session->read_buf_p += line_len;
485 /* incomplete read */
486 if (buf[line_len - 1] != '\n')
490 if (session->io_tag > 0) {
491 g_source_remove(session->io_tag);
496 msg = g_strdup(session->read_msg_buf->str);
498 g_string_truncate(session->read_msg_buf, 0);
500 ret = session->recv_msg(session, msg);
501 session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
506 session->state = SESSION_ERROR;
511 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
514 Session *session = SESSION(data);
515 GByteArray *data_buf;
517 gboolean complete = FALSE;
521 g_return_val_if_fail(condition == G_IO_IN, FALSE);
523 if (session->read_buf_len == 0) {
526 read_len = sock_read(session->sock, session->read_buf,
530 g_warning("sock_read: received EOF\n");
531 session->state = SESSION_EOF;
540 g_warning("sock_read: %s\n", g_strerror(errno));
541 session->state = SESSION_ERROR;
546 session->read_buf_len = read_len;
549 data_buf = session->read_data_buf;
550 terminator_len = strlen(session->read_data_terminator);
552 if (session->read_buf_len == 0)
555 g_byte_array_append(data_buf, session->read_buf_p,
556 session->read_buf_len);
558 session->read_buf_len = 0;
559 session->read_buf_p = session->read_buf;
561 /* check if data is terminated */
562 if (data_buf->len >= terminator_len) {
563 if (memcmp(data_buf->data, session->read_data_terminator,
564 terminator_len) == 0)
566 else if (data_buf->len >= terminator_len + 2 &&
567 memcmp(data_buf->data + data_buf->len -
568 (terminator_len + 2), "\r\n", 2) == 0 &&
569 memcmp(data_buf->data + data_buf->len -
570 terminator_len, session->read_data_terminator,
571 terminator_len) == 0)
575 /* incomplete read */
577 struct timeval tv_cur;
579 gettimeofday(&tv_cur, NULL);
580 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
581 tv_cur.tv_usec - session->tv_prev.tv_usec >
582 UI_REFRESH_INTERVAL) {
583 session->recv_data_progressive_notify
584 (session, data_buf->len, 0,
585 session->recv_data_progressive_notify_data);
586 gettimeofday(&session->tv_prev, NULL);
592 if (session->io_tag > 0) {
593 g_source_remove(session->io_tag);
597 data_len = data_buf->len - terminator_len;
600 ret = session->recv_data_finished(session, (gchar *)data_buf->data,
603 g_byte_array_set_size(data_buf, 0);
605 session->recv_data_notify(session, data_len,
606 session->recv_data_notify_data);
609 session->state = SESSION_ERROR;
614 static gint session_write_buf(Session *session)
619 g_return_val_if_fail(session->write_buf != NULL, -1);
620 g_return_val_if_fail(session->write_buf_p != NULL, -1);
621 g_return_val_if_fail(session->write_buf_len > 0, -1);
623 to_write_len = session->write_buf_len -
624 (session->write_buf_p - session->write_buf);
625 to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
627 write_len = sock_write(session->sock, session->write_buf_p,
636 g_warning("sock_write: %s\n", g_strerror(errno));
637 session->state = SESSION_ERROR;
642 /* incomplete write */
643 if (session->write_buf_p - session->write_buf + write_len <
644 session->write_buf_len) {
645 session->write_buf_p += write_len;
649 g_free(session->write_buf);
650 session->write_buf = NULL;
651 session->write_buf_p = NULL;
652 session->write_buf_len = 0;
657 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
660 Session *session = SESSION(data);
663 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
664 g_return_val_if_fail(session->write_buf != NULL, FALSE);
665 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
666 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
668 ret = session_write_buf(session);
671 session->state = SESSION_ERROR;
676 if (session->io_tag > 0) {
677 g_source_remove(session->io_tag);
681 session_recv_msg(session);
686 static gboolean session_write_data_cb(SockInfo *source,
687 GIOCondition condition, gpointer data)
689 Session *session = SESSION(data);
693 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
694 g_return_val_if_fail(session->write_buf != NULL, FALSE);
695 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
696 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
698 write_buf_len = session->write_buf_len;
700 ret = session_write_buf(session);
703 session->state = SESSION_ERROR;
705 } else if (ret > 0) {
706 struct timeval tv_cur;
708 gettimeofday(&tv_cur, NULL);
709 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
710 tv_cur.tv_usec - session->tv_prev.tv_usec >
711 UI_REFRESH_INTERVAL) {
712 session->send_data_progressive_notify
714 session->write_buf_p - session->write_buf,
716 session->send_data_progressive_notify_data);
717 gettimeofday(&session->tv_prev, NULL);
722 if (session->io_tag > 0) {
723 g_source_remove(session->io_tag);
728 ret = session->send_data_finished(session, write_buf_len);
729 session->send_data_notify(session, write_buf_len,
730 session->send_data_notify_data);