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.
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;
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(GSource *source, GSourceFunc callback,
317 SockInfo *sock = ((SockSource *)source)->sock;
319 if (!sock || !sock->callback || !sock->data)
322 return sock->callback(sock, sock->condition, sock->data);
325 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
328 SockInfo *sock = (SockInfo *)data;
330 if (!sock || !sock->callback || !sock->data)
333 return sock->callback(sock, condition, sock->data);
336 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
339 sock->callback = func;
340 sock->condition = condition;
346 GSource *source = g_source_new(&sock_watch_funcs,
348 ((SockSource *) source)->sock = sock;
349 g_source_set_priority(source, G_PRIORITY_DEFAULT);
350 g_source_set_can_recurse(source, FALSE);
351 g_source_attach(source, NULL);
355 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
358 static gint fd_check_io(gint fd, GIOCondition cond)
360 struct timeval timeout;
363 if (is_nonblocking_mode(fd))
366 timeout.tv_sec = io_timeout;
372 if (cond == G_IO_IN) {
373 select(fd + 1, &fds, NULL, NULL,
374 io_timeout > 0 ? &timeout : NULL);
376 select(fd + 1, NULL, &fds, NULL,
377 io_timeout > 0 ? &timeout : NULL);
380 if (FD_ISSET(fd, &fds)) {
383 g_warning("Socket IO timeout\n");
388 static sigjmp_buf jmpenv;
390 static void timeout_handler(gint sig)
392 siglongjmp(jmpenv, 1);
395 static gint sock_connect_with_timeout(gint sock,
396 const struct sockaddr *serv_addr,
401 void (*prev_handler)(gint);
404 prev_handler = signal(SIGALRM, timeout_handler);
405 if (sigsetjmp(jmpenv, 1)) {
407 signal(SIGALRM, prev_handler);
413 ret = connect(sock, serv_addr, addrlen);
416 signal(SIGALRM, prev_handler);
421 struct hostent *my_gethostbyname(const gchar *hostname)
424 void (*prev_handler)(gint);
427 prev_handler = signal(SIGALRM, timeout_handler);
428 if (sigsetjmp(jmpenv, 1)) {
430 signal(SIGALRM, prev_handler);
431 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
437 if ((hp = gethostbyname(hostname)) == NULL) {
439 signal(SIGALRM, prev_handler);
440 fprintf(stderr, "%s: unknown host.\n", hostname);
446 signal(SIGALRM, prev_handler);
452 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
455 return inet_aton(hostname, inp);
460 inaddr = inet_addr(hostname);
462 memcpy(inp, &inaddr, sizeof(inaddr));
469 #endif /* HAVE_INET_ATON */
472 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
476 struct sockaddr_in ad;
478 memset(&ad, 0, sizeof(ad));
479 ad.sin_family = AF_INET;
480 ad.sin_port = htons(port);
482 if (!my_inet_aton(hostname, &ad.sin_addr)) {
483 if ((hp = my_gethostbyname(hostname)) == NULL) {
484 fprintf(stderr, "%s: unknown host.\n", hostname);
489 if (hp->h_length != 4 && hp->h_length != 8) {
490 fprintf(stderr, "illegal address length received for host %s\n", hostname);
495 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
498 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
499 sizeof(ad), io_timeout);
503 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
505 gint sock = -1, gai_error;
506 struct addrinfo hints, *res, *ai;
509 memset(&hints, 0, sizeof(hints));
510 /* hints.ai_flags = AI_CANONNAME; */
511 hints.ai_family = AF_UNSPEC;
512 hints.ai_socktype = SOCK_STREAM;
513 hints.ai_protocol = IPPROTO_TCP;
515 /* convert port from integer to string. */
516 g_snprintf(port_str, sizeof(port_str), "%d", port);
518 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
519 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
520 hostname, port_str, gai_strerror(gai_error));
524 for (ai = res; ai != NULL; ai = ai->ai_next) {
525 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
529 if (sock_connect_with_timeout
530 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
547 /* Open a connection using an external program. May be useful when
548 * you need to tunnel through a SOCKS or other firewall, or to
549 * establish an IMAP-over-SSH connection. */
550 /* TODO: Recreate this for sock_connect_thread() */
551 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
556 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
557 perror("socketpair");
560 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
565 dup(fd[1]); /* set onto stdin */
567 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
571 return sockinfo_from_fd(hostname, 0, fd[0]);
575 SockInfo *sock_connect(const gchar *hostname, gushort port)
580 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
583 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
588 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
589 if (errno != 0) perror("connect");
595 return sockinfo_from_fd(hostname, port, sock);
598 static void sock_address_list_free(GList *addr_list)
602 for (cur = addr_list; cur != NULL; cur = cur->next) {
603 SockAddrData *addr_data = (SockAddrData *)cur->data;
604 g_free(addr_data->addr);
608 g_list_free(addr_list);
611 /* asynchronous TCP connection */
613 static gboolean sock_connect_async_cb(GIOChannel *source,
614 GIOCondition condition, gpointer data)
616 SockConnectData *conn_data = (SockConnectData *)data;
622 fd = g_io_channel_unix_get_fd(source);
624 conn_data->io_tag = 0;
625 conn_data->channel = NULL;
626 g_io_channel_unref(source);
629 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
630 perror("getsockopt");
632 sock_connect_address_list_async(conn_data);
638 sock_connect_address_list_async(conn_data);
642 sockinfo = g_new0(SockInfo, 1);
644 sockinfo->sock_ch = g_io_channel_unix_new(fd);
645 sockinfo->hostname = g_strdup(conn_data->hostname);
646 sockinfo->port = conn_data->port;
647 sockinfo->state = CONN_ESTABLISHED;
649 conn_data->func(sockinfo, conn_data->data);
651 sock_connect_async_cancel(conn_data->id);
656 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
659 SockConnectData *conn_data = (SockConnectData *)data;
661 conn_data->addr_list = addr_list;
662 conn_data->cur_addr = addr_list;
663 conn_data->lookup_data = NULL;
665 return sock_connect_address_list_async(conn_data);
668 gint sock_connect_async(const gchar *hostname, gushort port,
669 SockConnectFunc func, gpointer data)
672 SockConnectData *conn_data;
674 conn_data = g_new0(SockConnectData, 1);
675 conn_data->id = id++;
676 conn_data->hostname = g_strdup(hostname);
677 conn_data->port = port;
678 conn_data->addr_list = NULL;
679 conn_data->cur_addr = NULL;
680 conn_data->io_tag = 0;
681 conn_data->func = func;
682 conn_data->data = data;
684 conn_data->lookup_data = sock_get_address_info_async
685 (hostname, port, sock_connect_async_get_address_info_cb,
688 if (conn_data->lookup_data == NULL) {
689 g_free(conn_data->hostname);
694 sock_connect_data_list = g_list_append(sock_connect_data_list,
697 return conn_data->id;
700 gint sock_connect_async_cancel(gint id)
702 SockConnectData *conn_data = NULL;
705 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
706 if (((SockConnectData *)cur->data)->id == id) {
707 conn_data = (SockConnectData *)cur->data;
713 sock_connect_data_list = g_list_remove(sock_connect_data_list,
716 if (conn_data->lookup_data)
717 sock_get_address_info_async_cancel
718 (conn_data->lookup_data);
720 if (conn_data->io_tag > 0)
721 g_source_remove(conn_data->io_tag);
722 if (conn_data->channel) {
723 g_io_channel_close(conn_data->channel);
724 g_io_channel_unref(conn_data->channel);
727 sock_address_list_free(conn_data->addr_list);
728 g_free(conn_data->hostname);
731 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
738 static gint sock_connect_address_list_async(SockConnectData *conn_data)
740 SockAddrData *addr_data;
743 for (; conn_data->cur_addr != NULL;
744 conn_data->cur_addr = conn_data->cur_addr->next) {
745 addr_data = (SockAddrData *)conn_data->cur_addr->data;
747 if ((sock = socket(addr_data->family, addr_data->socktype,
748 addr_data->protocol)) < 0) {
753 set_nonblocking_mode(sock, TRUE);
755 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
756 if (EINPROGRESS == errno) {
766 if (conn_data->cur_addr == NULL) {
767 g_warning("sock_connect_address_list_async: "
768 "connection to %s:%d failed\n",
769 conn_data->hostname, conn_data->port);
770 conn_data->func(NULL, conn_data->data);
771 sock_connect_async_cancel(conn_data->id);
775 conn_data->cur_addr = conn_data->cur_addr->next;
777 conn_data->channel = g_io_channel_unix_new(sock);
778 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
779 sock_connect_async_cb, conn_data);
784 /* asynchronous DNS lookup */
786 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
787 GIOCondition condition,
790 SockLookupData *lookup_data = (SockLookupData *)data;
791 GList *addr_list = NULL;
792 SockAddrData *addr_data;
795 struct sockaddr *addr;
798 if (g_io_channel_read(source, (gchar *)ai_member,
799 sizeof(ai_member), &bytes_read)
800 != G_IO_ERROR_NONE) {
801 g_warning("sock_get_address_info_async_cb: "
802 "address length read error\n");
806 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
809 if (ai_member[0] == AF_UNSPEC) {
810 g_warning("DNS lookup failed\n");
814 addr = g_malloc(ai_member[3]);
815 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
817 != G_IO_ERROR_NONE) {
818 g_warning("sock_get_address_info_async_cb: "
819 "address data read error\n");
824 if (bytes_read != ai_member[3]) {
825 g_warning("sock_get_address_info_async_cb: "
826 "incomplete address data\n");
831 addr_data = g_new0(SockAddrData, 1);
832 addr_data->family = ai_member[0];
833 addr_data->socktype = ai_member[1];
834 addr_data->protocol = ai_member[2];
835 addr_data->addr_len = ai_member[3];
836 addr_data->addr = addr;
838 addr_list = g_list_append(addr_list, addr_data);
841 g_io_channel_close(source);
842 g_io_channel_unref(source);
844 kill(lookup_data->child_pid, SIGKILL);
845 waitpid(lookup_data->child_pid, NULL, 0);
847 lookup_data->func(addr_list, lookup_data->data);
849 g_free(lookup_data->hostname);
855 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
860 SockLookupData *lookup_data = NULL;
864 if (pipe(pipe_fds) < 0) {
870 if ((pid = fork()) < 0) {
880 struct addrinfo hints, *res, *ai;
885 struct sockaddr_in ad;
887 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
892 memset(&hints, 0, sizeof(hints));
893 /* hints.ai_flags = AI_CANONNAME; */
894 hints.ai_family = AF_UNSPEC;
895 hints.ai_socktype = SOCK_STREAM;
896 hints.ai_protocol = IPPROTO_TCP;
898 g_snprintf(port_str, sizeof(port_str), "%d", port);
900 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
902 g_warning("getaddrinfo for %s:%s failed: %s\n",
903 hostname, port_str, gai_strerror(gai_err));
904 fd_write_all(pipe_fds[1], (gchar *)ai_member,
910 for (ai = res; ai != NULL; ai = ai->ai_next) {
911 ai_member[0] = ai->ai_family;
912 ai_member[1] = ai->ai_socktype;
913 ai_member[2] = ai->ai_protocol;
914 ai_member[3] = ai->ai_addrlen;
916 fd_write_all(pipe_fds[1], (gchar *)ai_member,
918 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
925 hp = my_gethostbyname(hostname);
926 if (hp == NULL || hp->h_addrtype != AF_INET) {
927 fd_write_all(pipe_fds[1], (gchar *)ai_member,
933 ai_member[0] = AF_INET;
934 ai_member[1] = SOCK_STREAM;
935 ai_member[2] = IPPROTO_TCP;
936 ai_member[3] = sizeof(ad);
938 memset(&ad, 0, sizeof(ad));
939 ad.sin_family = AF_INET;
940 ad.sin_port = htons(port);
942 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
944 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
945 fd_write_all(pipe_fds[1], (gchar *)ai_member,
947 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
957 lookup_data = g_new0(SockLookupData, 1);
958 lookup_data->hostname = g_strdup(hostname);
959 lookup_data->child_pid = pid;
960 lookup_data->func = func;
961 lookup_data->data = data;
963 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
964 lookup_data->io_tag = g_io_add_watch
965 (lookup_data->channel, G_IO_IN,
966 sock_get_address_info_async_cb, lookup_data);
972 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
974 if (lookup_data->io_tag > 0)
975 g_source_remove(lookup_data->io_tag);
976 if (lookup_data->channel) {
977 g_io_channel_close(lookup_data->channel);
978 g_io_channel_unref(lookup_data->channel);
981 if (lookup_data->child_pid > 0) {
982 kill(lookup_data->child_pid, SIGKILL);
983 waitpid(lookup_data->child_pid, NULL, 0);
986 g_free(lookup_data->hostname);
993 static SockInfo *sockinfo_from_fd(const gchar *hostname,
999 sockinfo = g_new0(SockInfo, 1);
1000 sockinfo->sock = sock;
1001 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1002 sockinfo->hostname = g_strdup(hostname);
1003 sockinfo->port = port;
1004 sockinfo->state = CONN_ESTABLISHED;
1011 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1014 gchar buf[BUFFSIZE];
1016 va_start(args, format);
1017 g_vsnprintf(buf, sizeof(buf), format, args);
1020 return sock_write_all(sock, buf, strlen(buf));
1023 gint fd_read(gint fd, gchar *buf, gint len)
1025 if (fd_check_io(fd, G_IO_IN) < 0)
1028 return read(fd, buf, len);
1032 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1036 ret = SSL_read(ssl, buf, len);
1038 switch (SSL_get_error(ssl, ret)) {
1039 case SSL_ERROR_NONE:
1041 case SSL_ERROR_WANT_READ:
1042 case SSL_ERROR_WANT_WRITE:
1051 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1055 g_return_val_if_fail(sock != NULL, -1);
1059 ret = ssl_read(sock->ssl, buf, len);
1062 ret = fd_read(sock->sock, buf, len);
1065 sock->state = CONN_DISCONNECTED;
1069 gint fd_write(gint fd, const gchar *buf, gint len)
1071 if (fd_check_io(fd, G_IO_OUT) < 0)
1074 return write(fd, buf, len);
1078 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1082 ret = SSL_write(ssl, buf, len);
1084 switch (SSL_get_error(ssl, ret)) {
1085 case SSL_ERROR_NONE:
1087 case SSL_ERROR_WANT_READ:
1088 case SSL_ERROR_WANT_WRITE:
1097 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1101 g_return_val_if_fail(sock != NULL, -1);
1105 ret = ssl_write(sock->ssl, buf, len);
1108 ret = fd_write(sock->sock, buf, len);
1111 sock->state = CONN_DISCONNECTED;
1115 gint fd_write_all(gint fd, const gchar *buf, gint len)
1120 if (fd_check_io(fd, G_IO_OUT) < 0)
1122 signal(SIGPIPE, SIG_IGN);
1123 n = write(fd, buf, len);
1125 log_error("write on fd%d: %s\n", fd, strerror(errno));
1137 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1142 n = ssl_write(ssl, buf, len);
1154 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1158 g_return_val_if_fail(sock != NULL, -1);
1162 ret = ssl_write_all(sock->ssl, buf, len);
1165 ret = fd_write_all(sock->sock, buf, len);
1168 sock->state = CONN_DISCONNECTED;
1172 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1174 if (fd_check_io(fd, G_IO_IN) < 0)
1177 return recv(fd, buf, len, flags);
1180 gint fd_gets(gint fd, gchar *buf, gint len)
1182 gchar *newline, *bp = buf;
1188 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1190 if ((newline = memchr(bp, '\n', n)) != NULL)
1191 n = newline - bp + 1;
1192 if ((n = fd_read(fd, bp, n)) < 0)
1196 } while (!newline && len);
1203 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1205 gchar *newline, *bp = buf;
1211 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1213 if ((newline = memchr(bp, '\n', n)) != NULL)
1214 n = newline - bp + 1;
1215 if ((n = ssl_read(ssl, bp, n)) < 0)
1219 } while (!newline && len);
1226 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1230 g_return_val_if_fail(sock != NULL, -1);
1234 return ssl_gets(sock->ssl, buf, len);
1237 return fd_gets(sock->sock, buf, len);
1240 sock->state = CONN_DISCONNECTED;
1244 gint fd_getline(gint fd, gchar **str)
1246 gchar buf[BUFFSIZE];
1250 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1253 *str = g_strdup(buf);
1255 *str = g_realloc(*str, size);
1258 if (buf[len - 1] == '\n')
1261 if (len == -1 && *str)
1268 gint ssl_getline(SSL *ssl, gchar **str)
1270 gchar buf[BUFFSIZE];
1274 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1277 *str = g_strdup(buf);
1279 *str = g_realloc(*str, size);
1282 if (buf[len - 1] == '\n')
1285 if (len == -1 && *str)
1292 gchar *sock_getline(SockInfo *sock)
1297 g_return_val_if_fail(sock != NULL, NULL);
1301 ret = ssl_getline(sock->ssl, &str);
1304 ret = fd_getline(sock->sock, &str);
1307 sock->state = CONN_DISCONNECTED;
1311 gint sock_puts(SockInfo *sock, const gchar *buf)
1315 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1317 return sock_write_all(sock, "\r\n", 2);
1320 /* peek at the socket data without actually reading it */
1322 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1326 ret = SSL_peek(ssl, buf, len);
1328 switch (SSL_get_error(ssl, ret)) {
1329 case SSL_ERROR_NONE:
1331 case SSL_ERROR_WANT_READ:
1332 case SSL_ERROR_WANT_WRITE:
1341 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1343 g_return_val_if_fail(sock != NULL, -1);
1347 return ssl_peek(sock->ssl, buf, len);
1349 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1352 gint sock_close(SockInfo *sock)
1360 g_io_channel_unref(sock->sock_ch);
1364 ssl_done_socket(sock);
1366 ret = fd_close(sock->sock);
1367 g_free(sock->hostname);
1373 gint fd_close(gint fd)