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");
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);
184 void session_set_recv_message_notify(Session *session,
185 RecvMsgNotify notify_func, gpointer data)
187 session->recv_msg_notify = notify_func;
188 session->recv_msg_notify_data = data;
191 void session_set_recv_data_progressive_notify
193 RecvDataProgressiveNotify notify_func,
196 session->recv_data_progressive_notify = notify_func,
197 session->recv_data_progressive_notify_data = data;
200 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
203 session->recv_data_notify = notify_func;
204 session->recv_data_notify_data = data;
207 void session_set_send_data_progressive_notify
209 SendDataProgressiveNotify notify_func,
212 session->send_data_progressive_notify = notify_func;
213 session->send_data_progressive_notify_data = data;
216 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
219 session->send_data_notify = notify_func;
220 session->send_data_notify_data = data;
224 *\brief child and parent cleanup (child closes first)
226 *\param session Contains session information
230 static gint session_close(Session *session)
232 g_return_val_if_fail(session != NULL, -1);
234 debug_print("session_close\n");
236 if (session->conn_id > 0) {
237 sock_connect_async_cancel(session->conn_id);
238 session->conn_id = 0;
241 if (session->io_tag > 0) {
242 g_source_remove(session->io_tag);
247 sock_close(session->sock);
248 session->sock = NULL;
249 session->state = SESSION_DISCONNECTED;
256 gint session_start_tls(Session *session)
260 nb_mode = sock_is_nonblocking_mode(session->sock);
263 sock_set_nonblocking_mode(session->sock, FALSE);
265 if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
266 g_warning("can't start TLS session.\n");
268 sock_set_nonblocking_mode(session->sock, TRUE);
273 sock_set_nonblocking_mode(session->sock, TRUE);
279 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
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);
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;
292 ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
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)
303 gint session_recv_msg(Session *session)
305 g_return_val_if_fail(session->read_buf->len == 0, -1);
307 session->state = SESSION_RECV;
309 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
310 session_read_msg_cb, session);
316 *\brief parent (child?): send data to other process
318 *\param session Contains session information
325 gint session_send_data(Session *session, const guchar *data, guint size)
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);
333 session->state = SESSION_SEND;
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);
341 ret = session_write_data_cb(session->sock, G_IO_OUT, session);
344 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
345 session_write_data_cb,
347 else if (session->state == SESSION_ERROR)
353 gint session_recv_data(Session *session, guint size, const gchar *terminator)
355 g_return_val_if_fail(session->read_data_buf->len == 0, -1);
357 session->state = SESSION_RECV;
359 g_free(session->read_data_terminator);
360 session->read_data_terminator = g_strdup(terminator);
361 gettimeofday(&session->tv_prev, NULL);
363 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
364 session_read_data_cb, session);
369 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
372 Session *session = SESSION(data);
373 gchar buf[SESSION_BUFFSIZE];
380 g_return_val_if_fail(condition == G_IO_IN, FALSE);
382 read_len = sock_peek(session->sock, buf, sizeof(buf) - 1);
389 g_warning("sock_peek: %s\n", g_strerror(errno));
390 session->state = SESSION_ERROR;
395 if ((newline = memchr(buf, '\n', read_len)) != NULL)
396 to_read_len = newline - buf + 1;
398 to_read_len = read_len;
400 read_len = sock_read(session->sock, buf, to_read_len);
402 /* this should always succeed */
408 g_warning("sock_read: %s\n", g_strerror(errno));
409 session->state = SESSION_ERROR;
414 buf[read_len] = '\0';
416 /* incomplete read */
417 if (read_len == 0 || buf[read_len - 1] != '\n') {
418 g_string_append(session->read_buf, buf);
424 g_string_append(session->read_buf, buf);
426 if (session->io_tag > 0) {
427 g_source_remove(session->io_tag);
432 msg = g_strdup(session->read_buf->str);
433 g_string_truncate(session->read_buf, 0);
435 ret = session->recv_msg(session, msg);
436 session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
441 session->state = SESSION_ERROR;
446 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
449 Session *session = SESSION(data);
450 gchar buf[SESSION_BUFFSIZE];
451 GByteArray *data_buf;
454 gboolean complete = FALSE;
458 g_return_val_if_fail(condition == G_IO_IN, FALSE);
460 read_len = sock_read(session->sock, buf, sizeof(buf));
467 g_warning("sock_read: %s\n", g_strerror(errno));
468 session->state = SESSION_ERROR;
473 data_buf = session->read_data_buf;
475 g_byte_array_append(data_buf, buf, read_len);
476 terminator_len = strlen(session->read_data_terminator);
478 /* check if data is terminated */
479 if (read_len > 0 && data_buf->len >= terminator_len) {
480 if (memcmp(data_buf->data, session->read_data_terminator,
481 terminator_len) == 0)
483 else if (data_buf->len >= terminator_len + 2 &&
484 memcmp(data_buf->data + data_buf->len -
485 (terminator_len + 2), "\r\n", 2) == 0 &&
486 memcmp(data_buf->data + data_buf->len -
487 terminator_len, session->read_data_terminator,
488 terminator_len) == 0)
492 /* incomplete read */
494 struct timeval tv_cur;
496 gettimeofday(&tv_cur, NULL);
497 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
498 tv_cur.tv_usec - session->tv_prev.tv_usec >
499 UI_REFRESH_INTERVAL) {
500 session->recv_data_progressive_notify
501 (session, data_buf->len, 0,
502 session->recv_data_progressive_notify_data);
503 gettimeofday(&session->tv_prev, NULL);
509 if (session->io_tag > 0) {
510 g_source_remove(session->io_tag);
514 data_len = data_buf->len - terminator_len;
517 ret = session->recv_data_finished(session, (gchar *)data_buf->data,
520 g_byte_array_set_size(data_buf, 0);
522 session->recv_data_notify(session, data_len,
523 session->recv_data_notify_data);
526 session->state = SESSION_ERROR;
531 static gint session_write_buf(Session *session)
536 g_return_val_if_fail(session->write_buf != NULL, -1);
537 g_return_val_if_fail(session->write_buf_p != NULL, -1);
538 g_return_val_if_fail(session->write_buf_len > 0, -1);
540 to_write_len = session->write_buf_len -
541 (session->write_buf_p - session->write_buf);
542 to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
544 write_len = sock_write(session->sock, session->write_buf_p,
553 g_warning("sock_write: %s\n", g_strerror(errno));
554 session->state = SESSION_ERROR;
559 /* incomplete write */
560 if (session->write_buf_p - session->write_buf + write_len <
561 session->write_buf_len) {
562 session->write_buf_p += write_len;
566 g_free(session->write_buf);
567 session->write_buf = NULL;
568 session->write_buf_p = NULL;
569 session->write_buf_len = 0;
574 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
577 Session *session = SESSION(data);
580 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
581 g_return_val_if_fail(session->write_buf != NULL, FALSE);
582 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
583 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
585 ret = session_write_buf(session);
588 session->state = SESSION_ERROR;
593 if (session->io_tag > 0) {
594 g_source_remove(session->io_tag);
598 session_recv_msg(session);
603 static gboolean session_write_data_cb(SockInfo *source,
604 GIOCondition condition, gpointer data)
606 Session *session = SESSION(data);
610 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
611 g_return_val_if_fail(session->write_buf != NULL, FALSE);
612 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
613 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
615 write_buf_len = session->write_buf_len;
617 ret = session_write_buf(session);
620 session->state = SESSION_ERROR;
622 } else if (ret > 0) {
623 struct timeval tv_cur;
625 gettimeofday(&tv_cur, NULL);
626 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
627 tv_cur.tv_usec - session->tv_prev.tv_usec >
628 UI_REFRESH_INTERVAL) {
629 session->send_data_progressive_notify
631 session->write_buf_p - session->write_buf,
633 session->send_data_progressive_notify_data);
634 gettimeofday(&session->tv_prev, NULL);
639 if (session->io_tag > 0) {
640 g_source_remove(session->io_tag);
645 ret = session->send_data_finished(session, write_buf_len);
646 session->send_data_notify(session, write_buf_len,
647 session->send_data_notify_data);