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_read_msg_cb (SockInfo *source,
46 GIOCondition condition,
48 static gboolean session_read_data_cb (SockInfo *source,
49 GIOCondition condition,
51 static gboolean session_write_msg_cb (SockInfo *source,
52 GIOCondition condition,
54 static gboolean session_write_data_cb (SockInfo *source,
55 GIOCondition condition,
59 void session_init(Session *session)
61 session->type = SESSION_UNKNOWN;
63 session->server = NULL;
66 session->ssl_type = SSL_NONE;
68 session->state = SESSION_READY;
69 session->last_access_time = time(NULL);
71 gettimeofday(&session->tv_prev, NULL);
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;
87 *\brief Set up parent and child process
88 * Childloop: Read commands from parent,
89 * send to server, get answer, pass to parent
91 *\param session Contains session information
92 * server to connect to
96 * -1 : pipe / fork errors (parent)
97 * 1 : connection error (child)
99 gint session_connect(Session *session, const gchar *server, gushort port)
101 session->server = g_strdup(server);
102 session->port = port;
104 session->conn_id = sock_connect_async(server, port, session_connect_cb,
106 if (session->conn_id < 0) {
107 g_warning("can't connect to server.");
108 session_close(session);
115 static gint session_connect_cb(SockInfo *sock, gpointer data)
117 Session *session = SESSION(data);
119 session->conn_id = 0;
122 g_warning("can't connect to server.");
123 session->state = SESSION_ERROR;
127 session->sock = sock;
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;
138 sock_set_nonblocking_mode(sock, TRUE);
140 debug_print("session: connected\n");
142 session->state = SESSION_RECV;
143 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
151 *\brief child and parent: send DISCONNECT message to other process
153 *\param session Contains session information
157 gint session_disconnect(Session *session)
159 session_close(session);
166 *\param session Contains session information
168 void session_destroy(Session *session)
170 g_return_if_fail(session != NULL);
171 g_return_if_fail(session->destroy != NULL);
173 debug_print("session: session_destroy()\n");
175 session_close(session);
176 session->destroy(session);
177 g_free(session->server);
178 g_string_free(session->read_buf, TRUE);
179 g_byte_array_free(session->read_data_buf, TRUE);
180 g_free(session->read_data_terminator);
181 g_free(session->write_buf);
185 void session_set_recv_message_notify(Session *session,
186 RecvMsgNotify notify_func, gpointer data)
188 session->recv_msg_notify = notify_func;
189 session->recv_msg_notify_data = data;
192 void session_set_recv_data_progressive_notify
194 RecvDataProgressiveNotify notify_func,
197 session->recv_data_progressive_notify = notify_func,
198 session->recv_data_progressive_notify_data = data;
201 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
204 session->recv_data_notify = notify_func;
205 session->recv_data_notify_data = data;
208 void session_set_send_data_progressive_notify
210 SendDataProgressiveNotify notify_func,
213 session->send_data_progressive_notify = notify_func;
214 session->send_data_progressive_notify_data = data;
217 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
220 session->send_data_notify = notify_func;
221 session->send_data_notify_data = data;
225 *\brief child and parent cleanup (child closes first)
227 *\param session Contains session information
231 static gint session_close(Session *session)
233 g_return_val_if_fail(session != NULL, -1);
235 debug_print("session_close\n");
237 if (session->conn_id > 0) {
238 sock_connect_async_cancel(session->conn_id);
239 session->conn_id = 0;
242 if (session->io_tag > 0) {
243 g_source_remove(session->io_tag);
248 sock_close(session->sock);
249 session->sock = NULL;
250 session->state = SESSION_DISCONNECTED;
257 gint session_start_tls(Session *session)
261 nb_mode = sock_is_nonblocking_mode(session->sock);
264 sock_set_nonblocking_mode(session->sock, FALSE);
266 if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
267 g_warning("can't start TLS session.\n");
269 sock_set_nonblocking_mode(session->sock, TRUE);
274 sock_set_nonblocking_mode(session->sock, TRUE);
280 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
284 g_return_val_if_fail(session->write_buf == NULL, -1);
285 g_return_val_if_fail(msg != NULL, -1);
286 g_return_val_if_fail(msg[0] != '\0', -1);
288 session->state = SESSION_SEND;
289 session->write_buf = g_strconcat(msg, "\r\n", NULL);
290 session->write_buf_p = session->write_buf;
291 session->write_buf_len = strlen(msg) + 2;
293 ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
296 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
297 session_write_msg_cb, session);
298 else if (session->state == SESSION_ERROR)
304 gint session_recv_msg(Session *session)
306 g_return_val_if_fail(session->read_buf->len == 0, -1);
308 session->state = SESSION_RECV;
310 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
311 session_read_msg_cb, session);
317 *\brief parent (child?): send data to other process
319 *\param session Contains session information
326 gint session_send_data(Session *session, const guchar *data, guint size)
330 g_return_val_if_fail(session->write_buf == NULL, -1);
331 g_return_val_if_fail(data != NULL, -1);
332 g_return_val_if_fail(size != 0, -1);
334 session->state = SESSION_SEND;
336 session->write_buf = g_malloc(size);
337 session->write_buf_p = session->write_buf;
338 memcpy(session->write_buf, data, size);
339 session->write_buf_len = size;
340 gettimeofday(&session->tv_prev, NULL);
342 ret = session_write_data_cb(session->sock, G_IO_OUT, session);
345 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
346 session_write_data_cb,
348 else if (session->state == SESSION_ERROR)
354 gint session_recv_data(Session *session, guint size, const gchar *terminator)
356 g_return_val_if_fail(session->read_data_buf->len == 0, -1);
358 session->state = SESSION_RECV;
360 g_free(session->read_data_terminator);
361 session->read_data_terminator = g_strdup(terminator);
362 gettimeofday(&session->tv_prev, NULL);
364 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
365 session_read_data_cb, session);
370 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
373 Session *session = SESSION(data);
374 gchar buf[SESSION_BUFFSIZE];
381 g_return_val_if_fail(condition == G_IO_IN, FALSE);
383 read_len = sock_peek(session->sock, buf, sizeof(buf) - 1);
390 g_warning("sock_peek: %s\n", g_strerror(errno));
391 session->state = SESSION_ERROR;
396 if ((newline = memchr(buf, '\n', read_len)) != NULL)
397 to_read_len = newline - buf + 1;
399 to_read_len = read_len;
401 read_len = sock_read(session->sock, buf, to_read_len);
403 /* this should always succeed */
405 g_warning("sock_read: %s\n", g_strerror(errno));
406 session->state = SESSION_ERROR;
410 buf[read_len] = '\0';
412 /* incomplete read */
413 if (read_len == 0 || buf[read_len - 1] != '\n') {
414 g_string_append(session->read_buf, buf);
420 g_string_append(session->read_buf, buf);
422 if (session->io_tag > 0) {
423 g_source_remove(session->io_tag);
428 msg = g_strdup(session->read_buf->str);
429 g_string_truncate(session->read_buf, 0);
431 ret = session->recv_msg(session, msg);
432 session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
437 session->state = SESSION_ERROR;
442 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
445 Session *session = SESSION(data);
446 gchar buf[SESSION_BUFFSIZE];
447 GByteArray *data_buf;
450 gboolean complete = FALSE;
454 g_return_val_if_fail(condition == G_IO_IN, FALSE);
456 read_len = sock_read(session->sock, buf, sizeof(buf));
463 g_warning("sock_read: %s\n", g_strerror(errno));
464 session->state = SESSION_ERROR;
469 data_buf = session->read_data_buf;
471 g_byte_array_append(data_buf, buf, read_len);
472 terminator_len = strlen(session->read_data_terminator);
474 /* check if data is terminated */
475 if (read_len > 0 && data_buf->len >= terminator_len) {
476 if (memcmp(data_buf->data, session->read_data_terminator,
477 terminator_len) == 0)
479 else if (data_buf->len >= terminator_len + 2 &&
480 memcmp(data_buf->data + data_buf->len -
481 (terminator_len + 2), "\r\n", 2) == 0 &&
482 memcmp(data_buf->data + data_buf->len -
483 terminator_len, session->read_data_terminator,
484 terminator_len) == 0)
488 /* incomplete read */
490 struct timeval tv_cur;
492 gettimeofday(&tv_cur, NULL);
493 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
494 tv_cur.tv_usec - session->tv_prev.tv_usec >
495 UI_REFRESH_INTERVAL) {
496 session->recv_data_progressive_notify
497 (session, data_buf->len, 0,
498 session->recv_data_progressive_notify_data);
499 gettimeofday(&session->tv_prev, NULL);
505 if (session->io_tag > 0) {
506 g_source_remove(session->io_tag);
510 data_len = data_buf->len - terminator_len;
513 ret = session->recv_data_finished(session, (gchar *)data_buf->data,
516 g_byte_array_set_size(data_buf, 0);
518 session->recv_data_notify(session, data_len,
519 session->recv_data_notify_data);
522 session->state = SESSION_ERROR;
527 static gint session_write_buf(Session *session)
532 g_return_val_if_fail(session->write_buf != NULL, -1);
533 g_return_val_if_fail(session->write_buf_p != NULL, -1);
534 g_return_val_if_fail(session->write_buf_len > 0, -1);
536 to_write_len = session->write_buf_len -
537 (session->write_buf_p - session->write_buf);
538 to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
540 write_len = sock_write(session->sock, session->write_buf_p,
549 g_warning("sock_write: %s\n", g_strerror(errno));
550 session->state = SESSION_ERROR;
555 /* incomplete write */
556 if (session->write_buf_p - session->write_buf + write_len <
557 session->write_buf_len) {
558 session->write_buf_p += write_len;
562 g_free(session->write_buf);
563 session->write_buf = NULL;
564 session->write_buf_p = NULL;
565 session->write_buf_len = 0;
570 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
573 Session *session = SESSION(data);
576 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
577 g_return_val_if_fail(session->write_buf != NULL, FALSE);
578 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
579 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
581 ret = session_write_buf(session);
584 session->state = SESSION_ERROR;
589 if (session->io_tag > 0) {
590 g_source_remove(session->io_tag);
594 session_recv_msg(session);
599 static gboolean session_write_data_cb(SockInfo *source,
600 GIOCondition condition, gpointer data)
602 Session *session = SESSION(data);
606 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
607 g_return_val_if_fail(session->write_buf != NULL, FALSE);
608 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
609 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
611 write_buf_len = session->write_buf_len;
613 ret = session_write_buf(session);
616 session->state = SESSION_ERROR;
618 } else if (ret > 0) {
619 struct timeval tv_cur;
621 gettimeofday(&tv_cur, NULL);
622 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
623 tv_cur.tv_usec - session->tv_prev.tv_usec >
624 UI_REFRESH_INTERVAL) {
625 session->send_data_progressive_notify
627 session->write_buf_p - session->write_buf,
629 session->send_data_progressive_notify_data);
630 gettimeofday(&session->tv_prev, NULL);
635 if (session->io_tag > 0) {
636 g_source_remove(session->io_tag);
641 ret = session->send_data_finished(session, write_buf_len);
642 session->send_data_notify(session, write_buf_len,
643 session->send_data_notify_data);