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;
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 g_source_attach(source, NULL);
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 fd = g_io_channel_unix_get_fd(source);
628 conn_data->io_tag = 0;
629 conn_data->channel = NULL;
630 g_io_channel_unref(source);
633 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
634 perror("getsockopt");
636 sock_connect_address_list_async(conn_data);
642 sock_connect_address_list_async(conn_data);
646 sockinfo = g_new0(SockInfo, 1);
648 sockinfo->sock_ch = g_io_channel_unix_new(fd);
649 sockinfo->hostname = g_strdup(conn_data->hostname);
650 sockinfo->port = conn_data->port;
651 sockinfo->state = CONN_ESTABLISHED;
653 conn_data->func(sockinfo, conn_data->data);
655 sock_connect_async_cancel(conn_data->id);
660 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
663 SockConnectData *conn_data = (SockConnectData *)data;
665 conn_data->addr_list = addr_list;
666 conn_data->cur_addr = addr_list;
667 conn_data->lookup_data = NULL;
669 return sock_connect_address_list_async(conn_data);
672 gint sock_connect_async(const gchar *hostname, gushort port,
673 SockConnectFunc func, gpointer data)
676 SockConnectData *conn_data;
678 conn_data = g_new0(SockConnectData, 1);
679 conn_data->id = id++;
680 conn_data->hostname = g_strdup(hostname);
681 conn_data->port = port;
682 conn_data->addr_list = NULL;
683 conn_data->cur_addr = NULL;
684 conn_data->io_tag = 0;
685 conn_data->func = func;
686 conn_data->data = data;
688 conn_data->lookup_data = sock_get_address_info_async
689 (hostname, port, sock_connect_async_get_address_info_cb,
692 if (conn_data->lookup_data == NULL) {
693 g_free(conn_data->hostname);
698 sock_connect_data_list = g_list_append(sock_connect_data_list,
701 return conn_data->id;
704 gint sock_connect_async_cancel(gint id)
706 SockConnectData *conn_data = NULL;
709 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
710 if (((SockConnectData *)cur->data)->id == id) {
711 conn_data = (SockConnectData *)cur->data;
717 sock_connect_data_list = g_list_remove(sock_connect_data_list,
720 if (conn_data->lookup_data)
721 sock_get_address_info_async_cancel
722 (conn_data->lookup_data);
724 if (conn_data->io_tag > 0)
725 g_source_remove(conn_data->io_tag);
726 if (conn_data->channel) {
727 g_io_channel_close(conn_data->channel);
728 g_io_channel_unref(conn_data->channel);
731 sock_address_list_free(conn_data->addr_list);
732 g_free(conn_data->hostname);
735 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
742 static gint sock_connect_address_list_async(SockConnectData *conn_data)
744 SockAddrData *addr_data;
747 for (; conn_data->cur_addr != NULL;
748 conn_data->cur_addr = conn_data->cur_addr->next) {
749 addr_data = (SockAddrData *)conn_data->cur_addr->data;
751 if ((sock = socket(addr_data->family, addr_data->socktype,
752 addr_data->protocol)) < 0) {
757 set_nonblocking_mode(sock, TRUE);
759 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
760 if (EINPROGRESS == errno) {
770 if (conn_data->cur_addr == NULL) {
771 g_warning("sock_connect_address_list_async: "
772 "connection to %s:%d failed\n",
773 conn_data->hostname, conn_data->port);
774 conn_data->func(NULL, conn_data->data);
775 sock_connect_async_cancel(conn_data->id);
779 conn_data->cur_addr = conn_data->cur_addr->next;
781 conn_data->channel = g_io_channel_unix_new(sock);
782 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
783 sock_connect_async_cb, conn_data);
788 /* asynchronous DNS lookup */
790 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
791 GIOCondition condition,
794 SockLookupData *lookup_data = (SockLookupData *)data;
795 GList *addr_list = NULL;
796 SockAddrData *addr_data;
799 struct sockaddr *addr;
802 if (g_io_channel_read(source, (gchar *)ai_member,
803 sizeof(ai_member), &bytes_read)
804 != G_IO_ERROR_NONE) {
805 g_warning("sock_get_address_info_async_cb: "
806 "address length read error\n");
810 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
813 if (ai_member[0] == AF_UNSPEC) {
814 g_warning("DNS lookup failed\n");
818 addr = g_malloc(ai_member[3]);
819 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
821 != G_IO_ERROR_NONE) {
822 g_warning("sock_get_address_info_async_cb: "
823 "address data read error\n");
828 if (bytes_read != ai_member[3]) {
829 g_warning("sock_get_address_info_async_cb: "
830 "incomplete address data\n");
835 addr_data = g_new0(SockAddrData, 1);
836 addr_data->family = ai_member[0];
837 addr_data->socktype = ai_member[1];
838 addr_data->protocol = ai_member[2];
839 addr_data->addr_len = ai_member[3];
840 addr_data->addr = addr;
842 addr_list = g_list_append(addr_list, addr_data);
845 g_io_channel_close(source);
846 g_io_channel_unref(source);
848 kill(lookup_data->child_pid, SIGKILL);
849 waitpid(lookup_data->child_pid, NULL, 0);
851 lookup_data->func(addr_list, lookup_data->data);
853 g_free(lookup_data->hostname);
859 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
864 SockLookupData *lookup_data = NULL;
868 if (pipe(pipe_fds) < 0) {
874 if ((pid = fork()) < 0) {
884 struct addrinfo hints, *res, *ai;
889 struct sockaddr_in ad;
891 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
896 memset(&hints, 0, sizeof(hints));
897 /* hints.ai_flags = AI_CANONNAME; */
898 hints.ai_family = AF_UNSPEC;
899 hints.ai_socktype = SOCK_STREAM;
900 hints.ai_protocol = IPPROTO_TCP;
902 g_snprintf(port_str, sizeof(port_str), "%d", port);
904 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
906 g_warning("getaddrinfo for %s:%s failed: %s\n",
907 hostname, port_str, gai_strerror(gai_err));
908 fd_write_all(pipe_fds[1], (gchar *)ai_member,
914 for (ai = res; ai != NULL; ai = ai->ai_next) {
915 ai_member[0] = ai->ai_family;
916 ai_member[1] = ai->ai_socktype;
917 ai_member[2] = ai->ai_protocol;
918 ai_member[3] = ai->ai_addrlen;
920 fd_write_all(pipe_fds[1], (gchar *)ai_member,
922 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
929 hp = my_gethostbyname(hostname);
930 if (hp == NULL || hp->h_addrtype != AF_INET) {
931 fd_write_all(pipe_fds[1], (gchar *)ai_member,
937 ai_member[0] = AF_INET;
938 ai_member[1] = SOCK_STREAM;
939 ai_member[2] = IPPROTO_TCP;
940 ai_member[3] = sizeof(ad);
942 memset(&ad, 0, sizeof(ad));
943 ad.sin_family = AF_INET;
944 ad.sin_port = htons(port);
946 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
948 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
949 fd_write_all(pipe_fds[1], (gchar *)ai_member,
951 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
961 lookup_data = g_new0(SockLookupData, 1);
962 lookup_data->hostname = g_strdup(hostname);
963 lookup_data->child_pid = pid;
964 lookup_data->func = func;
965 lookup_data->data = data;
967 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
968 lookup_data->io_tag = g_io_add_watch
969 (lookup_data->channel, G_IO_IN,
970 sock_get_address_info_async_cb, lookup_data);
976 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
978 if (lookup_data->io_tag > 0)
979 g_source_remove(lookup_data->io_tag);
980 if (lookup_data->channel) {
981 g_io_channel_close(lookup_data->channel);
982 g_io_channel_unref(lookup_data->channel);
985 if (lookup_data->child_pid > 0) {
986 kill(lookup_data->child_pid, SIGKILL);
987 waitpid(lookup_data->child_pid, NULL, 0);
990 g_free(lookup_data->hostname);
997 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1003 sockinfo = g_new0(SockInfo, 1);
1004 sockinfo->sock = sock;
1005 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1006 sockinfo->hostname = g_strdup(hostname);
1007 sockinfo->port = port;
1008 sockinfo->state = CONN_ESTABLISHED;
1015 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1018 gchar buf[BUFFSIZE];
1020 va_start(args, format);
1021 g_vsnprintf(buf, sizeof(buf), format, args);
1024 return sock_write_all(sock, buf, strlen(buf));
1027 gint fd_read(gint fd, gchar *buf, gint len)
1029 if (fd_check_io(fd, G_IO_IN) < 0)
1032 return read(fd, buf, len);
1036 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1040 ret = SSL_read(ssl, buf, len);
1042 switch (SSL_get_error(ssl, ret)) {
1043 case SSL_ERROR_NONE:
1045 case SSL_ERROR_WANT_READ:
1046 case SSL_ERROR_WANT_WRITE:
1055 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1059 g_return_val_if_fail(sock != NULL, -1);
1063 ret = ssl_read(sock->ssl, buf, len);
1066 ret = fd_read(sock->sock, buf, len);
1069 sock->state = CONN_DISCONNECTED;
1073 gint fd_write(gint fd, const gchar *buf, gint len)
1075 if (fd_check_io(fd, G_IO_OUT) < 0)
1078 return write(fd, buf, len);
1082 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1086 ret = SSL_write(ssl, buf, len);
1088 switch (SSL_get_error(ssl, ret)) {
1089 case SSL_ERROR_NONE:
1091 case SSL_ERROR_WANT_READ:
1092 case SSL_ERROR_WANT_WRITE:
1101 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1105 g_return_val_if_fail(sock != NULL, -1);
1109 ret = ssl_write(sock->ssl, buf, len);
1112 ret = fd_write(sock->sock, buf, len);
1115 sock->state = CONN_DISCONNECTED;
1119 gint fd_write_all(gint fd, const gchar *buf, gint len)
1124 if (fd_check_io(fd, G_IO_OUT) < 0)
1126 signal(SIGPIPE, SIG_IGN);
1127 n = write(fd, buf, len);
1129 log_error("write on fd%d: %s\n", fd, strerror(errno));
1141 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1146 n = ssl_write(ssl, buf, len);
1158 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1162 g_return_val_if_fail(sock != NULL, -1);
1166 ret = ssl_write_all(sock->ssl, buf, len);
1169 ret = fd_write_all(sock->sock, buf, len);
1172 sock->state = CONN_DISCONNECTED;
1176 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1178 if (fd_check_io(fd, G_IO_IN) < 0)
1181 return recv(fd, buf, len, flags);
1184 gint fd_gets(gint fd, gchar *buf, gint len)
1186 gchar *newline, *bp = buf;
1192 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1194 if ((newline = memchr(bp, '\n', n)) != NULL)
1195 n = newline - bp + 1;
1196 if ((n = fd_read(fd, bp, n)) < 0)
1200 } while (!newline && len);
1207 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1209 gchar *newline, *bp = buf;
1215 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1217 if ((newline = memchr(bp, '\n', n)) != NULL)
1218 n = newline - bp + 1;
1219 if ((n = ssl_read(ssl, bp, n)) < 0)
1223 } while (!newline && len);
1230 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1234 g_return_val_if_fail(sock != NULL, -1);
1238 return ssl_gets(sock->ssl, buf, len);
1241 return fd_gets(sock->sock, buf, len);
1244 sock->state = CONN_DISCONNECTED;
1248 gint fd_getline(gint fd, gchar **str)
1250 gchar buf[BUFFSIZE];
1254 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1257 *str = g_strdup(buf);
1259 *str = g_realloc(*str, size);
1262 if (buf[len - 1] == '\n')
1265 if (len == -1 && *str)
1272 gint ssl_getline(SSL *ssl, gchar **str)
1274 gchar buf[BUFFSIZE];
1278 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1281 *str = g_strdup(buf);
1283 *str = g_realloc(*str, size);
1286 if (buf[len - 1] == '\n')
1289 if (len == -1 && *str)
1296 gchar *sock_getline(SockInfo *sock)
1301 g_return_val_if_fail(sock != NULL, NULL);
1305 ret = ssl_getline(sock->ssl, &str);
1308 ret = fd_getline(sock->sock, &str);
1311 sock->state = CONN_DISCONNECTED;
1315 gint sock_puts(SockInfo *sock, const gchar *buf)
1319 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1321 return sock_write_all(sock, "\r\n", 2);
1324 /* peek at the socket data without actually reading it */
1326 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1330 ret = SSL_peek(ssl, buf, len);
1332 switch (SSL_get_error(ssl, ret)) {
1333 case SSL_ERROR_NONE:
1335 case SSL_ERROR_WANT_READ:
1336 case SSL_ERROR_WANT_WRITE:
1345 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1347 g_return_val_if_fail(sock != NULL, -1);
1351 return ssl_peek(sock->ssl, buf, len);
1353 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1356 gint sock_close(SockInfo *sock)
1364 g_io_channel_unref(sock->sock_ch);
1368 ssl_done_socket(sock);
1369 if (sock->g_source != 0)
1370 g_source_remove(sock->g_source);
1373 ret = fd_close(sock->sock);
1374 g_free(sock->hostname);
1380 gint fd_close(gint fd)