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.
26 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
42 # include <sys/select.h>
53 #error USE_GIO is currently not supported
58 typedef gint (*SockAddrFunc) (GList *addr_list,
61 typedef struct _SockConnectData SockConnectData;
62 typedef struct _SockLookupData SockLookupData;
63 typedef struct _SockAddrData SockAddrData;
65 struct _SockConnectData {
71 SockLookupData *lookup_data;
78 struct _SockLookupData {
87 struct _SockAddrData {
92 struct sockaddr *addr;
95 static guint io_timeout = 60;
97 static GList *sock_connect_data_list = NULL;
99 static gboolean sock_prepare (gpointer source_data,
100 GTimeVal *current_time,
103 static gboolean sock_check (gpointer source_data,
104 GTimeVal *current_time,
106 static gboolean sock_dispatch (gpointer source_data,
107 GTimeVal *current_time,
110 GSourceFuncs sock_watch_funcs = {
117 static gint sock_connect_with_timeout (gint sock,
118 const struct sockaddr *serv_addr,
123 static gint sock_connect_by_hostname (gint sock,
124 const gchar *hostname,
127 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
131 static SockInfo *sockinfo_from_fd(const gchar *hostname,
134 static void sock_address_list_free (GList *addr_list);
136 static gboolean sock_connect_async_cb (GIOChannel *source,
137 GIOCondition condition,
139 static gint sock_connect_async_get_address_info_cb
143 static gint sock_connect_address_list_async (SockConnectData *conn_data);
145 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
146 GIOCondition condition,
148 static SockLookupData *sock_get_address_info_async
149 (const gchar *hostname,
153 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
156 gint sock_set_io_timeout(guint sec)
162 gint fd_connect_unix(const gchar *path)
165 struct sockaddr_un addr;
167 sock = socket(PF_UNIX, SOCK_STREAM, 0);
169 perror("sock_connect_unix(): socket");
173 memset(&addr, 0, sizeof(addr));
174 addr.sun_family = AF_UNIX;
175 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
177 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
185 gint fd_open_unix(const gchar *path)
188 struct sockaddr_un addr;
190 sock = socket(PF_UNIX, SOCK_STREAM, 0);
193 perror("sock_open_unix(): socket");
197 memset(&addr, 0, sizeof(addr));
198 addr.sun_family = AF_UNIX;
199 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
201 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
207 if (listen(sock, 1) < 0) {
216 gint fd_accept(gint sock)
218 struct sockaddr_in caddr;
221 caddr_len = sizeof(caddr);
222 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
226 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
230 flags = fcntl(fd, F_GETFL, 0);
239 flags &= ~O_NONBLOCK;
241 return fcntl(fd, F_SETFL, flags);
244 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
246 g_return_val_if_fail(sock != NULL, -1);
248 return set_nonblocking_mode(sock->sock, nonblock);
251 static gboolean is_nonblocking_mode(gint fd)
255 flags = fcntl(fd, F_GETFL, 0);
261 return ((flags & O_NONBLOCK) != 0);
264 gboolean sock_is_nonblocking_mode(SockInfo *sock)
266 g_return_val_if_fail(sock != NULL, FALSE);
268 return is_nonblocking_mode(sock->sock);
272 static gboolean sock_prepare(gpointer source_data, GTimeVal *current_time,
273 gint *timeout, gpointer data)
279 static gboolean sock_check(gpointer source_data, GTimeVal *current_time,
282 SockInfo *sock = (SockInfo *)source_data;
283 struct timeval timeout = {0, 0};
285 GIOCondition condition = sock->condition;
289 if (condition & G_IO_IN) {
290 if (SSL_pending(sock->ssl) > 0)
292 if (SSL_want_write(sock->ssl))
293 condition |= G_IO_OUT;
296 if (condition & G_IO_OUT) {
297 if (SSL_want_read(sock->ssl))
298 condition |= G_IO_IN;
304 FD_SET(sock->sock, &fds);
306 select(sock->sock + 1,
307 (condition & G_IO_IN) ? &fds : NULL,
308 (condition & G_IO_OUT) ? &fds : NULL,
311 return FD_ISSET(sock->sock, &fds) != 0;
314 static gboolean sock_dispatch(gpointer source_data, GTimeVal *current_time,
317 SockInfo *sock = (SockInfo *)source_data;
319 return sock->callback(sock, sock->condition, user_data);
322 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
325 SockInfo *sock = (SockInfo *)data;
327 return sock->callback(sock, condition, sock->data);
330 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
333 sock->callback = func;
334 sock->condition = condition;
339 return g_source_add(G_PRIORITY_DEFAULT, FALSE,
340 &sock_watch_funcs, sock, data, NULL);
343 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
346 static gint fd_check_io(gint fd, GIOCondition cond)
348 struct timeval timeout;
351 if (is_nonblocking_mode(fd))
354 timeout.tv_sec = io_timeout;
360 if (cond == G_IO_IN) {
361 select(fd + 1, &fds, NULL, NULL,
362 io_timeout > 0 ? &timeout : NULL);
364 select(fd + 1, NULL, &fds, NULL,
365 io_timeout > 0 ? &timeout : NULL);
368 if (FD_ISSET(fd, &fds)) {
371 g_warning("Socket IO timeout\n");
376 static sigjmp_buf jmpenv;
378 static void timeout_handler(gint sig)
380 siglongjmp(jmpenv, 1);
383 static gint sock_connect_with_timeout(gint sock,
384 const struct sockaddr *serv_addr,
389 void (*prev_handler)(gint);
392 prev_handler = signal(SIGALRM, timeout_handler);
393 if (sigsetjmp(jmpenv, 1)) {
395 signal(SIGALRM, prev_handler);
401 ret = connect(sock, serv_addr, addrlen);
404 signal(SIGALRM, prev_handler);
409 struct hostent *my_gethostbyname(const gchar *hostname)
412 void (*prev_handler)(gint);
415 prev_handler = signal(SIGALRM, timeout_handler);
416 if (sigsetjmp(jmpenv, 1)) {
418 signal(SIGALRM, prev_handler);
419 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
425 if ((hp = gethostbyname(hostname)) == NULL) {
427 signal(SIGALRM, prev_handler);
428 fprintf(stderr, "%s: unknown host.\n", hostname);
434 signal(SIGALRM, prev_handler);
440 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
443 return inet_aton(hostname, inp);
448 inaddr = inet_addr(hostname);
450 memcpy(inp, &inaddr, sizeof(inaddr));
457 #endif /* HAVE_INET_ATON */
460 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
464 struct sockaddr_in ad;
466 memset(&ad, 0, sizeof(ad));
467 ad.sin_family = AF_INET;
468 ad.sin_port = htons(port);
470 if (!my_inet_aton(hostname, &ad.sin_addr)) {
471 if ((hp = my_gethostbyname(hostname)) == NULL) {
472 fprintf(stderr, "%s: unknown host.\n", hostname);
477 if (hp->h_length != 4 && hp->h_length != 8) {
478 fprintf(stderr, "illegal address length received for host %s\n", hostname);
483 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
486 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
487 sizeof(ad), io_timeout);
491 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
493 gint sock = -1, gai_error;
494 struct addrinfo hints, *res, *ai;
497 memset(&hints, 0, sizeof(hints));
498 /* hints.ai_flags = AI_CANONNAME; */
499 hints.ai_family = AF_UNSPEC;
500 hints.ai_socktype = SOCK_STREAM;
501 hints.ai_protocol = IPPROTO_TCP;
503 /* convert port from integer to string. */
504 g_snprintf(port_str, sizeof(port_str), "%d", port);
506 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
507 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
508 hostname, port_str, gai_strerror(gai_error));
512 for (ai = res; ai != NULL; ai = ai->ai_next) {
513 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
517 if (sock_connect_with_timeout
518 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
535 /* Open a connection using an external program. May be useful when
536 * you need to tunnel through a SOCKS or other firewall, or to
537 * establish an IMAP-over-SSH connection. */
538 /* TODO: Recreate this for sock_connect_thread() */
539 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
544 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
545 perror("socketpair");
548 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
553 dup(fd[1]); /* set onto stdin */
555 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
559 return sockinfo_from_fd(hostname, 0, fd[0]);
563 SockInfo *sock_connect(const gchar *hostname, gushort port)
568 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
571 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
576 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
577 if (errno != 0) perror("connect");
583 return sockinfo_from_fd(hostname, port, sock);
586 static void sock_address_list_free(GList *addr_list)
590 for (cur = addr_list; cur != NULL; cur = cur->next) {
591 SockAddrData *addr_data = (SockAddrData *)cur->data;
592 g_free(addr_data->addr);
596 g_list_free(addr_list);
599 /* asynchronous TCP connection */
601 static gboolean sock_connect_async_cb(GIOChannel *source,
602 GIOCondition condition, gpointer data)
604 SockConnectData *conn_data = (SockConnectData *)data;
610 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
613 fd = g_io_channel_unix_get_fd(source);
615 conn_data->io_tag = 0;
616 conn_data->channel = NULL;
617 g_io_channel_unref(source);
620 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
621 perror("getsockopt");
623 sock_connect_address_list_async(conn_data);
629 sock_connect_address_list_async(conn_data);
633 sockinfo = g_new0(SockInfo, 1);
635 sockinfo->sock_ch = g_io_channel_unix_new(fd);
636 sockinfo->hostname = g_strdup(conn_data->hostname);
637 sockinfo->port = conn_data->port;
638 sockinfo->state = CONN_ESTABLISHED;
640 conn_data->func(sockinfo, conn_data->data);
642 sock_connect_async_cancel(conn_data->id);
647 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
650 SockConnectData *conn_data = (SockConnectData *)data;
652 conn_data->addr_list = addr_list;
653 conn_data->cur_addr = addr_list;
654 conn_data->lookup_data = NULL;
656 return sock_connect_address_list_async(conn_data);
659 gint sock_connect_async(const gchar *hostname, gushort port,
660 SockConnectFunc func, gpointer data)
663 SockConnectData *conn_data;
665 conn_data = g_new0(SockConnectData, 1);
666 conn_data->id = id++;
667 conn_data->hostname = g_strdup(hostname);
668 conn_data->port = port;
669 conn_data->addr_list = NULL;
670 conn_data->cur_addr = NULL;
671 conn_data->io_tag = 0;
672 conn_data->func = func;
673 conn_data->data = data;
675 conn_data->lookup_data = sock_get_address_info_async
676 (hostname, port, sock_connect_async_get_address_info_cb,
679 if (conn_data->lookup_data == NULL) {
680 g_free(conn_data->hostname);
685 sock_connect_data_list = g_list_append(sock_connect_data_list,
688 return conn_data->id;
691 gint sock_connect_async_cancel(gint id)
693 SockConnectData *conn_data = NULL;
696 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
697 if (((SockConnectData *)cur->data)->id == id) {
698 conn_data = (SockConnectData *)cur->data;
704 sock_connect_data_list = g_list_remove(sock_connect_data_list,
707 if (conn_data->lookup_data)
708 sock_get_address_info_async_cancel
709 (conn_data->lookup_data);
711 if (conn_data->io_tag > 0)
712 g_source_remove(conn_data->io_tag);
713 if (conn_data->channel) {
714 g_io_channel_close(conn_data->channel);
715 g_io_channel_unref(conn_data->channel);
718 sock_address_list_free(conn_data->addr_list);
719 g_free(conn_data->hostname);
722 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
729 static gint sock_connect_address_list_async(SockConnectData *conn_data)
731 SockAddrData *addr_data;
734 for (; conn_data->cur_addr != NULL;
735 conn_data->cur_addr = conn_data->cur_addr->next) {
736 addr_data = (SockAddrData *)conn_data->cur_addr->data;
738 if ((sock = socket(addr_data->family, addr_data->socktype,
739 addr_data->protocol)) < 0) {
744 set_nonblocking_mode(sock, TRUE);
746 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
747 if (EINPROGRESS == errno) {
757 if (conn_data->cur_addr == NULL) {
758 g_warning("sock_connect_address_list_async: "
759 "connection to %s:%d failed\n",
760 conn_data->hostname, conn_data->port);
761 conn_data->func(NULL, conn_data->data);
762 sock_connect_async_cancel(conn_data->id);
766 conn_data->cur_addr = conn_data->cur_addr->next;
768 conn_data->channel = g_io_channel_unix_new(sock);
769 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
770 sock_connect_async_cb, conn_data);
775 /* asynchronous DNS lookup */
777 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
778 GIOCondition condition,
781 SockLookupData *lookup_data = (SockLookupData *)data;
782 GList *addr_list = NULL;
783 SockAddrData *addr_data;
786 struct sockaddr *addr;
789 if (g_io_channel_read(source, (gchar *)ai_member,
790 sizeof(ai_member), &bytes_read)
791 != G_IO_ERROR_NONE) {
792 g_warning("sock_get_address_info_async_cb: "
793 "address length read error\n");
797 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
800 if (ai_member[0] == AF_UNSPEC) {
801 g_warning("DNS lookup failed\n");
805 addr = g_malloc(ai_member[3]);
806 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
808 != G_IO_ERROR_NONE) {
809 g_warning("sock_get_address_info_async_cb: "
810 "address data read error\n");
815 if (bytes_read != ai_member[3]) {
816 g_warning("sock_get_address_info_async_cb: "
817 "incomplete address data\n");
822 addr_data = g_new0(SockAddrData, 1);
823 addr_data->family = ai_member[0];
824 addr_data->socktype = ai_member[1];
825 addr_data->protocol = ai_member[2];
826 addr_data->addr_len = ai_member[3];
827 addr_data->addr = addr;
829 addr_list = g_list_append(addr_list, addr_data);
832 g_io_channel_close(source);
833 g_io_channel_unref(source);
835 kill(lookup_data->child_pid, SIGKILL);
836 waitpid(lookup_data->child_pid, NULL, 0);
838 lookup_data->func(addr_list, lookup_data->data);
840 g_free(lookup_data->hostname);
846 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
851 SockLookupData *lookup_data = NULL;
855 if (pipe(pipe_fds) < 0) {
861 if ((pid = fork()) < 0) {
871 struct addrinfo hints, *res, *ai;
876 struct sockaddr_in ad;
878 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
883 memset(&hints, 0, sizeof(hints));
884 /* hints.ai_flags = AI_CANONNAME; */
885 hints.ai_family = AF_UNSPEC;
886 hints.ai_socktype = SOCK_STREAM;
887 hints.ai_protocol = IPPROTO_TCP;
889 g_snprintf(port_str, sizeof(port_str), "%d", port);
891 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
893 g_warning("getaddrinfo for %s:%s failed: %s\n",
894 hostname, port_str, gai_strerror(gai_err));
895 fd_write_all(pipe_fds[1], (gchar *)ai_member,
901 for (ai = res; ai != NULL; ai = ai->ai_next) {
902 ai_member[0] = ai->ai_family;
903 ai_member[1] = ai->ai_socktype;
904 ai_member[2] = ai->ai_protocol;
905 ai_member[3] = ai->ai_addrlen;
907 fd_write_all(pipe_fds[1], (gchar *)ai_member,
909 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
916 hp = my_gethostbyname(hostname);
917 if (hp == NULL || hp->h_addrtype != AF_INET) {
918 fd_write_all(pipe_fds[1], (gchar *)ai_member,
924 ai_member[0] = AF_INET;
925 ai_member[1] = SOCK_STREAM;
926 ai_member[2] = IPPROTO_TCP;
927 ai_member[3] = sizeof(ad);
929 memset(&ad, 0, sizeof(ad));
930 ad.sin_family = AF_INET;
931 ad.sin_port = htons(port);
933 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
935 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
936 fd_write_all(pipe_fds[1], (gchar *)ai_member,
938 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
948 lookup_data = g_new0(SockLookupData, 1);
949 lookup_data->hostname = g_strdup(hostname);
950 lookup_data->child_pid = pid;
951 lookup_data->func = func;
952 lookup_data->data = data;
954 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
955 lookup_data->io_tag = g_io_add_watch
956 (lookup_data->channel, G_IO_IN,
957 sock_get_address_info_async_cb, lookup_data);
963 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
965 if (lookup_data->io_tag > 0)
966 g_source_remove(lookup_data->io_tag);
967 if (lookup_data->channel) {
968 g_io_channel_close(lookup_data->channel);
969 g_io_channel_unref(lookup_data->channel);
972 if (lookup_data->child_pid > 0) {
973 kill(lookup_data->child_pid, SIGKILL);
974 waitpid(lookup_data->child_pid, NULL, 0);
977 g_free(lookup_data->hostname);
984 static SockInfo *sockinfo_from_fd(const gchar *hostname,
990 sockinfo = g_new0(SockInfo, 1);
991 sockinfo->sock = sock;
992 sockinfo->sock_ch = g_io_channel_unix_new(sock);
993 sockinfo->hostname = g_strdup(hostname);
994 sockinfo->port = port;
995 sockinfo->state = CONN_ESTABLISHED;
1002 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1005 gchar buf[BUFFSIZE];
1007 va_start(args, format);
1008 g_vsnprintf(buf, sizeof(buf), format, args);
1011 return sock_write_all(sock, buf, strlen(buf));
1014 gint fd_read(gint fd, gchar *buf, gint len)
1016 if (fd_check_io(fd, G_IO_IN) < 0)
1019 return read(fd, buf, len);
1023 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1027 if (SSL_pending(ssl) == 0) {
1028 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1032 ret = SSL_read(ssl, buf, len);
1034 switch (SSL_get_error(ssl, ret)) {
1035 case SSL_ERROR_NONE:
1037 case SSL_ERROR_WANT_READ:
1038 case SSL_ERROR_WANT_WRITE:
1047 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1051 g_return_val_if_fail(sock != NULL, -1);
1055 ret = ssl_read(sock->ssl, buf, len);
1058 ret = fd_read(sock->sock, buf, len);
1061 sock->state = CONN_DISCONNECTED;
1065 gint fd_write(gint fd, const gchar *buf, gint len)
1067 if (fd_check_io(fd, G_IO_OUT) < 0)
1070 return write(fd, buf, len);
1074 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1078 ret = SSL_write(ssl, buf, len);
1080 switch (SSL_get_error(ssl, ret)) {
1081 case SSL_ERROR_NONE:
1083 case SSL_ERROR_WANT_READ:
1084 case SSL_ERROR_WANT_WRITE:
1093 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1097 g_return_val_if_fail(sock != NULL, -1);
1101 ret = ssl_write(sock->ssl, buf, len);
1104 ret = fd_write(sock->sock, buf, len);
1107 sock->state = CONN_DISCONNECTED;
1111 gint fd_write_all(gint fd, const gchar *buf, gint len)
1116 if (fd_check_io(fd, G_IO_OUT) < 0)
1118 signal(SIGPIPE, SIG_IGN);
1119 n = write(fd, buf, len);
1121 log_error("write on fd%d: %s\n", fd, strerror(errno));
1133 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1138 n = ssl_write(ssl, buf, len);
1150 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1154 g_return_val_if_fail(sock != NULL, -1);
1158 ret = ssl_write_all(sock->ssl, buf, len);
1161 ret = fd_write_all(sock->sock, buf, len);
1164 sock->state = CONN_DISCONNECTED;
1168 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1170 if (fd_check_io(fd, G_IO_IN) < 0)
1173 return recv(fd, buf, len, flags);
1176 gint fd_gets(gint fd, gchar *buf, gint len)
1178 gchar *newline, *bp = buf;
1184 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1186 if ((newline = memchr(bp, '\n', n)) != NULL)
1187 n = newline - bp + 1;
1188 if ((n = fd_read(fd, bp, n)) < 0)
1192 } while (!newline && len);
1199 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1201 gchar *newline, *bp = buf;
1207 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1209 if ((newline = memchr(bp, '\n', n)) != NULL)
1210 n = newline - bp + 1;
1211 if ((n = ssl_read(ssl, bp, n)) < 0)
1215 } while (!newline && len);
1222 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1226 g_return_val_if_fail(sock != NULL, -1);
1230 return ssl_gets(sock->ssl, buf, len);
1233 return fd_gets(sock->sock, buf, len);
1236 sock->state = CONN_DISCONNECTED;
1240 gint fd_getline(gint fd, gchar **str)
1242 gchar buf[BUFFSIZE];
1246 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1249 *str = g_strdup(buf);
1251 *str = g_realloc(*str, size);
1254 if (buf[len - 1] == '\n')
1257 if (len == -1 && *str)
1264 gint ssl_getline(SSL *ssl, gchar **str)
1266 gchar buf[BUFFSIZE];
1270 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1273 *str = g_strdup(buf);
1275 *str = g_realloc(*str, size);
1278 if (buf[len - 1] == '\n')
1281 if (len == -1 && *str)
1288 gchar *sock_getline(SockInfo *sock)
1293 g_return_val_if_fail(sock != NULL, NULL);
1297 ret = ssl_getline(sock->ssl, &str);
1300 ret = fd_getline(sock->sock, &str);
1303 sock->state = CONN_DISCONNECTED;
1307 gint sock_puts(SockInfo *sock, const gchar *buf)
1311 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1313 return sock_write_all(sock, "\r\n", 2);
1316 /* peek at the socket data without actually reading it */
1318 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1322 if (SSL_pending(ssl) == 0) {
1323 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1327 ret = SSL_peek(ssl, buf, len);
1329 switch (SSL_get_error(ssl, ret)) {
1330 case SSL_ERROR_NONE:
1332 case SSL_ERROR_WANT_READ:
1333 case SSL_ERROR_WANT_WRITE:
1342 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1344 g_return_val_if_fail(sock != NULL, -1);
1348 return ssl_peek(sock->ssl, buf, len);
1350 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1353 gint sock_close(SockInfo *sock)
1361 g_io_channel_unref(sock->sock_ch);
1365 ssl_done_socket(sock);
1367 ret = fd_close(sock->sock);
1368 g_free(sock->hostname);
1374 gint fd_close(gint fd)