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;
64 typedef struct _SockSource SockSource;
66 struct _SockConnectData {
72 SockLookupData *lookup_data;
79 struct _SockLookupData {
88 struct _SockAddrData {
93 struct sockaddr *addr;
101 static guint io_timeout = 60;
103 static GList *sock_connect_data_list = NULL;
105 static gboolean sock_prepare (GSource *source,
107 static gboolean sock_check (GSource *source);
108 static gboolean sock_dispatch (GSource *source,
109 GSourceFunc callback,
112 GSourceFuncs sock_watch_funcs = {
119 static gint sock_connect_with_timeout (gint sock,
120 const struct sockaddr *serv_addr,
125 static gint sock_connect_by_hostname (gint sock,
126 const gchar *hostname,
129 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
133 static SockInfo *sockinfo_from_fd(const gchar *hostname,
136 static void sock_address_list_free (GList *addr_list);
138 static gboolean sock_connect_async_cb (GIOChannel *source,
139 GIOCondition condition,
141 static gint sock_connect_async_get_address_info_cb
145 static gint sock_connect_address_list_async (SockConnectData *conn_data);
147 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
148 GIOCondition condition,
150 static SockLookupData *sock_get_address_info_async
151 (const gchar *hostname,
155 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
158 gint sock_set_io_timeout(guint sec)
164 gint fd_connect_unix(const gchar *path)
167 struct sockaddr_un addr;
169 sock = socket(PF_UNIX, SOCK_STREAM, 0);
171 perror("sock_connect_unix(): socket");
175 memset(&addr, 0, sizeof(addr));
176 addr.sun_family = AF_UNIX;
177 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
179 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
187 gint fd_open_unix(const gchar *path)
190 struct sockaddr_un addr;
192 sock = socket(PF_UNIX, SOCK_STREAM, 0);
195 perror("sock_open_unix(): socket");
199 memset(&addr, 0, sizeof(addr));
200 addr.sun_family = AF_UNIX;
201 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
203 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
209 if (listen(sock, 1) < 0) {
218 gint fd_accept(gint sock)
220 struct sockaddr_in caddr;
223 caddr_len = sizeof(caddr);
224 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
228 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
232 flags = fcntl(fd, F_GETFL, 0);
241 flags &= ~O_NONBLOCK;
243 return fcntl(fd, F_SETFL, flags);
246 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
248 g_return_val_if_fail(sock != NULL, -1);
250 return set_nonblocking_mode(sock->sock, nonblock);
253 static gboolean is_nonblocking_mode(gint fd)
257 flags = fcntl(fd, F_GETFL, 0);
263 return ((flags & O_NONBLOCK) != 0);
266 gboolean sock_is_nonblocking_mode(SockInfo *sock)
268 g_return_val_if_fail(sock != NULL, FALSE);
270 return is_nonblocking_mode(sock->sock);
274 static gboolean sock_prepare(GSource *source, gint *timeout)
280 static gboolean sock_check(GSource *source)
282 SockInfo *sock = ((SockSource *)source)->sock;
283 struct timeval timeout = {0, 0};
285 GIOCondition condition = sock->condition;
287 if (!sock || !sock->sock)
292 if (condition & G_IO_IN) {
293 if (SSL_pending(sock->ssl) > 0)
295 if (SSL_want_write(sock->ssl))
296 condition |= G_IO_OUT;
299 if (condition & G_IO_OUT) {
300 if (SSL_want_read(sock->ssl))
301 condition |= G_IO_IN;
307 FD_SET(sock->sock, &fds);
309 select(sock->sock + 1,
310 (condition & G_IO_IN) ? &fds : NULL,
311 (condition & G_IO_OUT) ? &fds : NULL,
314 return FD_ISSET(sock->sock, &fds) != 0;
317 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
320 SockInfo *sock = ((SockSource *)source)->sock;
322 if (!sock || !sock->callback || !sock->data)
325 return sock->callback(sock, sock->condition, sock->data);
328 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
331 SockInfo *sock = (SockInfo *)data;
333 if (!sock || !sock->callback || !sock->data) {
336 return sock->callback(sock, condition, sock->data);
339 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
342 sock->callback = func;
343 sock->condition = condition;
349 GSource *source = g_source_new(&sock_watch_funcs,
351 ((SockSource *) source)->sock = sock;
352 g_source_set_priority(source, G_PRIORITY_DEFAULT);
353 g_source_set_can_recurse(source, FALSE);
354 sock->g_source = g_source_attach(source, NULL);
355 return sock->g_source;
359 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
362 static gint fd_check_io(gint fd, GIOCondition cond)
364 struct timeval timeout;
367 if (is_nonblocking_mode(fd))
370 timeout.tv_sec = io_timeout;
376 if (cond == G_IO_IN) {
377 select(fd + 1, &fds, NULL, NULL,
378 io_timeout > 0 ? &timeout : NULL);
380 select(fd + 1, NULL, &fds, NULL,
381 io_timeout > 0 ? &timeout : NULL);
384 if (FD_ISSET(fd, &fds)) {
387 g_warning("Socket IO timeout\n");
392 static sigjmp_buf jmpenv;
394 static void timeout_handler(gint sig)
396 siglongjmp(jmpenv, 1);
399 static gint sock_connect_with_timeout(gint sock,
400 const struct sockaddr *serv_addr,
405 void (*prev_handler)(gint);
408 prev_handler = signal(SIGALRM, timeout_handler);
409 if (sigsetjmp(jmpenv, 1)) {
411 signal(SIGALRM, prev_handler);
417 ret = connect(sock, serv_addr, addrlen);
420 signal(SIGALRM, prev_handler);
425 struct hostent *my_gethostbyname(const gchar *hostname)
428 void (*prev_handler)(gint);
431 prev_handler = signal(SIGALRM, timeout_handler);
432 if (sigsetjmp(jmpenv, 1)) {
434 signal(SIGALRM, prev_handler);
435 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
441 if ((hp = gethostbyname(hostname)) == NULL) {
443 signal(SIGALRM, prev_handler);
444 fprintf(stderr, "%s: unknown host.\n", hostname);
450 signal(SIGALRM, prev_handler);
456 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
459 return inet_aton(hostname, inp);
464 inaddr = inet_addr(hostname);
466 memcpy(inp, &inaddr, sizeof(inaddr));
473 #endif /* HAVE_INET_ATON */
476 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
480 struct sockaddr_in ad;
482 memset(&ad, 0, sizeof(ad));
483 ad.sin_family = AF_INET;
484 ad.sin_port = htons(port);
486 if (!my_inet_aton(hostname, &ad.sin_addr)) {
487 if ((hp = my_gethostbyname(hostname)) == NULL) {
488 fprintf(stderr, "%s: unknown host.\n", hostname);
493 if (hp->h_length != 4 && hp->h_length != 8) {
494 fprintf(stderr, "illegal address length received for host %s\n", hostname);
499 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
502 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
503 sizeof(ad), io_timeout);
507 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
509 gint sock = -1, gai_error;
510 struct addrinfo hints, *res, *ai;
513 memset(&hints, 0, sizeof(hints));
514 /* hints.ai_flags = AI_CANONNAME; */
515 hints.ai_family = AF_UNSPEC;
516 hints.ai_socktype = SOCK_STREAM;
517 hints.ai_protocol = IPPROTO_TCP;
519 /* convert port from integer to string. */
520 g_snprintf(port_str, sizeof(port_str), "%d", port);
522 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
523 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
524 hostname, port_str, gai_strerror(gai_error));
528 for (ai = res; ai != NULL; ai = ai->ai_next) {
529 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
533 if (sock_connect_with_timeout
534 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
551 /* Open a connection using an external program. May be useful when
552 * you need to tunnel through a SOCKS or other firewall, or to
553 * establish an IMAP-over-SSH connection. */
554 /* TODO: Recreate this for sock_connect_thread() */
555 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
560 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
561 perror("socketpair");
564 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
569 dup(fd[1]); /* set onto stdin */
571 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
575 return sockinfo_from_fd(hostname, 0, fd[0]);
579 SockInfo *sock_connect(const gchar *hostname, gushort port)
584 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
587 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
592 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
593 if (errno != 0) perror("connect");
599 return sockinfo_from_fd(hostname, port, sock);
602 static void sock_address_list_free(GList *addr_list)
606 for (cur = addr_list; cur != NULL; cur = cur->next) {
607 SockAddrData *addr_data = (SockAddrData *)cur->data;
608 g_free(addr_data->addr);
612 g_list_free(addr_list);
615 /* asynchronous TCP connection */
617 static gboolean sock_connect_async_cb(GIOChannel *source,
618 GIOCondition condition, gpointer data)
620 SockConnectData *conn_data = (SockConnectData *)data;
626 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
629 fd = g_io_channel_unix_get_fd(source);
631 conn_data->io_tag = 0;
632 conn_data->channel = NULL;
633 g_io_channel_unref(source);
636 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
637 perror("getsockopt");
639 sock_connect_address_list_async(conn_data);
645 sock_connect_address_list_async(conn_data);
649 sockinfo = g_new0(SockInfo, 1);
651 sockinfo->sock_ch = g_io_channel_unix_new(fd);
652 sockinfo->hostname = g_strdup(conn_data->hostname);
653 sockinfo->port = conn_data->port;
654 sockinfo->state = CONN_ESTABLISHED;
656 conn_data->func(sockinfo, conn_data->data);
658 sock_connect_async_cancel(conn_data->id);
663 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
666 SockConnectData *conn_data = (SockConnectData *)data;
668 conn_data->addr_list = addr_list;
669 conn_data->cur_addr = addr_list;
670 conn_data->lookup_data = NULL;
672 return sock_connect_address_list_async(conn_data);
675 gint sock_connect_async(const gchar *hostname, gushort port,
676 SockConnectFunc func, gpointer data)
679 SockConnectData *conn_data;
681 conn_data = g_new0(SockConnectData, 1);
682 conn_data->id = id++;
683 conn_data->hostname = g_strdup(hostname);
684 conn_data->port = port;
685 conn_data->addr_list = NULL;
686 conn_data->cur_addr = NULL;
687 conn_data->io_tag = 0;
688 conn_data->func = func;
689 conn_data->data = data;
691 conn_data->lookup_data = sock_get_address_info_async
692 (hostname, port, sock_connect_async_get_address_info_cb,
695 if (conn_data->lookup_data == NULL) {
696 g_free(conn_data->hostname);
701 sock_connect_data_list = g_list_append(sock_connect_data_list,
704 return conn_data->id;
707 gint sock_connect_async_cancel(gint id)
709 SockConnectData *conn_data = NULL;
712 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
713 if (((SockConnectData *)cur->data)->id == id) {
714 conn_data = (SockConnectData *)cur->data;
720 sock_connect_data_list = g_list_remove(sock_connect_data_list,
723 if (conn_data->lookup_data)
724 sock_get_address_info_async_cancel
725 (conn_data->lookup_data);
727 if (conn_data->io_tag > 0)
728 g_source_remove(conn_data->io_tag);
729 if (conn_data->channel) {
730 g_io_channel_close(conn_data->channel);
731 g_io_channel_unref(conn_data->channel);
734 sock_address_list_free(conn_data->addr_list);
735 g_free(conn_data->hostname);
738 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
745 static gint sock_connect_address_list_async(SockConnectData *conn_data)
747 SockAddrData *addr_data;
750 for (; conn_data->cur_addr != NULL;
751 conn_data->cur_addr = conn_data->cur_addr->next) {
752 addr_data = (SockAddrData *)conn_data->cur_addr->data;
754 if ((sock = socket(addr_data->family, addr_data->socktype,
755 addr_data->protocol)) < 0) {
760 set_nonblocking_mode(sock, TRUE);
762 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
763 if (EINPROGRESS == errno) {
773 if (conn_data->cur_addr == NULL) {
774 g_warning("sock_connect_address_list_async: "
775 "connection to %s:%d failed\n",
776 conn_data->hostname, conn_data->port);
777 conn_data->func(NULL, conn_data->data);
778 sock_connect_async_cancel(conn_data->id);
782 conn_data->cur_addr = conn_data->cur_addr->next;
784 conn_data->channel = g_io_channel_unix_new(sock);
785 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
786 sock_connect_async_cb, conn_data);
791 /* asynchronous DNS lookup */
793 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
794 GIOCondition condition,
797 SockLookupData *lookup_data = (SockLookupData *)data;
798 GList *addr_list = NULL;
799 SockAddrData *addr_data;
802 struct sockaddr *addr;
805 if (g_io_channel_read(source, (gchar *)ai_member,
806 sizeof(ai_member), &bytes_read)
807 != G_IO_ERROR_NONE) {
808 g_warning("sock_get_address_info_async_cb: "
809 "address length read error\n");
813 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
816 if (ai_member[0] == AF_UNSPEC) {
817 g_warning("DNS lookup failed\n");
821 addr = g_malloc(ai_member[3]);
822 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
824 != G_IO_ERROR_NONE) {
825 g_warning("sock_get_address_info_async_cb: "
826 "address data read error\n");
831 if (bytes_read != ai_member[3]) {
832 g_warning("sock_get_address_info_async_cb: "
833 "incomplete address data\n");
838 addr_data = g_new0(SockAddrData, 1);
839 addr_data->family = ai_member[0];
840 addr_data->socktype = ai_member[1];
841 addr_data->protocol = ai_member[2];
842 addr_data->addr_len = ai_member[3];
843 addr_data->addr = addr;
845 addr_list = g_list_append(addr_list, addr_data);
848 g_io_channel_close(source);
849 g_io_channel_unref(source);
851 kill(lookup_data->child_pid, SIGKILL);
852 waitpid(lookup_data->child_pid, NULL, 0);
854 lookup_data->func(addr_list, lookup_data->data);
856 g_free(lookup_data->hostname);
862 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
867 SockLookupData *lookup_data = NULL;
871 if (pipe(pipe_fds) < 0) {
877 if ((pid = fork()) < 0) {
887 struct addrinfo hints, *res, *ai;
892 struct sockaddr_in ad;
894 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
899 memset(&hints, 0, sizeof(hints));
900 /* hints.ai_flags = AI_CANONNAME; */
901 hints.ai_family = AF_UNSPEC;
902 hints.ai_socktype = SOCK_STREAM;
903 hints.ai_protocol = IPPROTO_TCP;
905 g_snprintf(port_str, sizeof(port_str), "%d", port);
907 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
909 g_warning("getaddrinfo for %s:%s failed: %s\n",
910 hostname, port_str, gai_strerror(gai_err));
911 fd_write_all(pipe_fds[1], (gchar *)ai_member,
917 for (ai = res; ai != NULL; ai = ai->ai_next) {
918 ai_member[0] = ai->ai_family;
919 ai_member[1] = ai->ai_socktype;
920 ai_member[2] = ai->ai_protocol;
921 ai_member[3] = ai->ai_addrlen;
923 fd_write_all(pipe_fds[1], (gchar *)ai_member,
925 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
932 hp = my_gethostbyname(hostname);
933 if (hp == NULL || hp->h_addrtype != AF_INET) {
934 fd_write_all(pipe_fds[1], (gchar *)ai_member,
940 ai_member[0] = AF_INET;
941 ai_member[1] = SOCK_STREAM;
942 ai_member[2] = IPPROTO_TCP;
943 ai_member[3] = sizeof(ad);
945 memset(&ad, 0, sizeof(ad));
946 ad.sin_family = AF_INET;
947 ad.sin_port = htons(port);
949 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
951 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
952 fd_write_all(pipe_fds[1], (gchar *)ai_member,
954 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
964 lookup_data = g_new0(SockLookupData, 1);
965 lookup_data->hostname = g_strdup(hostname);
966 lookup_data->child_pid = pid;
967 lookup_data->func = func;
968 lookup_data->data = data;
970 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
971 lookup_data->io_tag = g_io_add_watch
972 (lookup_data->channel, G_IO_IN,
973 sock_get_address_info_async_cb, lookup_data);
979 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
981 if (lookup_data->io_tag > 0)
982 g_source_remove(lookup_data->io_tag);
983 if (lookup_data->channel) {
984 g_io_channel_close(lookup_data->channel);
985 g_io_channel_unref(lookup_data->channel);
988 if (lookup_data->child_pid > 0) {
989 kill(lookup_data->child_pid, SIGKILL);
990 waitpid(lookup_data->child_pid, NULL, 0);
993 g_free(lookup_data->hostname);
1000 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1006 sockinfo = g_new0(SockInfo, 1);
1007 sockinfo->sock = sock;
1008 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1009 sockinfo->hostname = g_strdup(hostname);
1010 sockinfo->port = port;
1011 sockinfo->state = CONN_ESTABLISHED;
1018 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1021 gchar buf[BUFFSIZE];
1023 va_start(args, format);
1024 g_vsnprintf(buf, sizeof(buf), format, args);
1027 return sock_write_all(sock, buf, strlen(buf));
1030 gint fd_read(gint fd, gchar *buf, gint len)
1032 if (fd_check_io(fd, G_IO_IN) < 0)
1035 return read(fd, buf, len);
1039 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1043 if (SSL_pending(ssl) == 0) {
1044 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1048 ret = SSL_read(ssl, buf, len);
1050 switch (SSL_get_error(ssl, ret)) {
1051 case SSL_ERROR_NONE:
1053 case SSL_ERROR_WANT_READ:
1054 case SSL_ERROR_WANT_WRITE:
1063 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1067 g_return_val_if_fail(sock != NULL, -1);
1071 ret = ssl_read(sock->ssl, buf, len);
1074 ret = fd_read(sock->sock, buf, len);
1077 sock->state = CONN_DISCONNECTED;
1081 gint fd_write(gint fd, const gchar *buf, gint len)
1083 if (fd_check_io(fd, G_IO_OUT) < 0)
1086 return write(fd, buf, len);
1090 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1094 ret = SSL_write(ssl, buf, len);
1096 switch (SSL_get_error(ssl, ret)) {
1097 case SSL_ERROR_NONE:
1099 case SSL_ERROR_WANT_READ:
1100 case SSL_ERROR_WANT_WRITE:
1109 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1113 g_return_val_if_fail(sock != NULL, -1);
1117 ret = ssl_write(sock->ssl, buf, len);
1120 ret = fd_write(sock->sock, buf, len);
1123 sock->state = CONN_DISCONNECTED;
1127 gint fd_write_all(gint fd, const gchar *buf, gint len)
1132 if (fd_check_io(fd, G_IO_OUT) < 0)
1134 signal(SIGPIPE, SIG_IGN);
1135 n = write(fd, buf, len);
1137 log_error("write on fd%d: %s\n", fd, strerror(errno));
1149 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1154 n = ssl_write(ssl, buf, len);
1166 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1170 g_return_val_if_fail(sock != NULL, -1);
1174 ret = ssl_write_all(sock->ssl, buf, len);
1177 ret = fd_write_all(sock->sock, buf, len);
1180 sock->state = CONN_DISCONNECTED;
1184 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1186 if (fd_check_io(fd, G_IO_IN) < 0)
1189 return recv(fd, buf, len, flags);
1192 gint fd_gets(gint fd, gchar *buf, gint len)
1194 gchar *newline, *bp = buf;
1200 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1202 if ((newline = memchr(bp, '\n', n)) != NULL)
1203 n = newline - bp + 1;
1204 if ((n = fd_read(fd, bp, n)) < 0)
1208 } while (!newline && len);
1215 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1217 gchar *newline, *bp = buf;
1223 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1225 if ((newline = memchr(bp, '\n', n)) != NULL)
1226 n = newline - bp + 1;
1227 if ((n = ssl_read(ssl, bp, n)) < 0)
1231 } while (!newline && len);
1238 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1242 g_return_val_if_fail(sock != NULL, -1);
1246 return ssl_gets(sock->ssl, buf, len);
1249 return fd_gets(sock->sock, buf, len);
1252 sock->state = CONN_DISCONNECTED;
1256 gint fd_getline(gint fd, gchar **str)
1258 gchar buf[BUFFSIZE];
1262 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1265 *str = g_strdup(buf);
1267 *str = g_realloc(*str, size);
1270 if (buf[len - 1] == '\n')
1273 if (len == -1 && *str)
1280 gint ssl_getline(SSL *ssl, gchar **str)
1282 gchar buf[BUFFSIZE];
1286 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1289 *str = g_strdup(buf);
1291 *str = g_realloc(*str, size);
1294 if (buf[len - 1] == '\n')
1297 if (len == -1 && *str)
1304 gchar *sock_getline(SockInfo *sock)
1309 g_return_val_if_fail(sock != NULL, NULL);
1313 ret = ssl_getline(sock->ssl, &str);
1316 ret = fd_getline(sock->sock, &str);
1319 sock->state = CONN_DISCONNECTED;
1323 gint sock_puts(SockInfo *sock, const gchar *buf)
1327 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1329 return sock_write_all(sock, "\r\n", 2);
1332 /* peek at the socket data without actually reading it */
1334 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1338 if (SSL_pending(ssl) == 0) {
1339 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1343 ret = SSL_peek(ssl, buf, len);
1345 switch (SSL_get_error(ssl, ret)) {
1346 case SSL_ERROR_NONE:
1348 case SSL_ERROR_WANT_READ:
1349 case SSL_ERROR_WANT_WRITE:
1358 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1360 g_return_val_if_fail(sock != NULL, -1);
1364 return ssl_peek(sock->ssl, buf, len);
1366 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1369 gint sock_close(SockInfo *sock)
1377 g_io_channel_unref(sock->sock_ch);
1381 ssl_done_socket(sock);
1382 if (sock->g_source != 0)
1383 g_source_remove(sock->g_source);
1386 ret = fd_close(sock->sock);
1387 g_free(sock->hostname);
1393 gint fd_close(gint fd)