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>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
44 # include <sys/select.h>
55 #error USE_GIO is currently not supported
60 typedef gint (*SockAddrFunc) (GList *addr_list,
63 typedef struct _SockConnectData SockConnectData;
64 typedef struct _SockLookupData SockLookupData;
65 typedef struct _SockAddrData SockAddrData;
66 typedef struct _SockSource SockSource;
68 struct _SockConnectData {
74 SockLookupData *lookup_data;
81 struct _SockLookupData {
90 struct _SockAddrData {
95 struct sockaddr *addr;
103 static guint io_timeout = 60;
105 static GList *sock_connect_data_list = NULL;
107 static gboolean sock_prepare (GSource *source,
109 static gboolean sock_check (GSource *source);
110 static gboolean sock_dispatch (GSource *source,
111 GSourceFunc callback,
114 GSourceFuncs sock_watch_funcs = {
121 static gint sock_connect_with_timeout (gint sock,
122 const struct sockaddr *serv_addr,
127 static gint sock_connect_by_hostname (gint sock,
128 const gchar *hostname,
131 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
135 static SockInfo *sockinfo_from_fd(const gchar *hostname,
138 static void sock_address_list_free (GList *addr_list);
140 static gboolean sock_connect_async_cb (GIOChannel *source,
141 GIOCondition condition,
143 static gint sock_connect_async_get_address_info_cb
147 static gint sock_connect_address_list_async (SockConnectData *conn_data);
149 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
150 GIOCondition condition,
152 static SockLookupData *sock_get_address_info_async
153 (const gchar *hostname,
157 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
160 gint sock_set_io_timeout(guint sec)
166 void refresh_resolvers(void)
168 static time_t resolv_conf_changed = (time_t)NULL;
171 /* This makes the glibc re-read resolv.conf, if it changed
172 * since our startup. Maybe that should be #ifdef'ed, I don't
173 * know if it'd work on BSDs.
174 * Why doesn't the glibc do it by itself?
176 if (stat("/etc/resolv.conf", &s) == 0) {
177 if (s.st_mtime > resolv_conf_changed) {
178 resolv_conf_changed = s.st_mtime;
182 we'll have bigger problems. */
185 gint fd_connect_unix(const gchar *path)
188 struct sockaddr_un addr;
190 sock = socket(PF_UNIX, SOCK_STREAM, 0);
192 perror("sock_connect_unix(): socket");
196 memset(&addr, 0, sizeof(addr));
197 addr.sun_family = AF_UNIX;
198 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
200 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
208 gint fd_open_unix(const gchar *path)
211 struct sockaddr_un addr;
213 sock = socket(PF_UNIX, SOCK_STREAM, 0);
216 perror("sock_open_unix(): socket");
220 memset(&addr, 0, sizeof(addr));
221 addr.sun_family = AF_UNIX;
222 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
224 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
230 if (listen(sock, 1) < 0) {
239 gint fd_accept(gint sock)
241 struct sockaddr_in caddr;
244 caddr_len = sizeof(caddr);
245 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
249 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
253 flags = fcntl(fd, F_GETFL, 0);
262 flags &= ~O_NONBLOCK;
264 return fcntl(fd, F_SETFL, flags);
267 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
269 g_return_val_if_fail(sock != NULL, -1);
271 return set_nonblocking_mode(sock->sock, nonblock);
274 static gboolean is_nonblocking_mode(gint fd)
278 flags = fcntl(fd, F_GETFL, 0);
284 return ((flags & O_NONBLOCK) != 0);
287 gboolean sock_is_nonblocking_mode(SockInfo *sock)
289 g_return_val_if_fail(sock != NULL, FALSE);
291 return is_nonblocking_mode(sock->sock);
295 static gboolean sock_prepare(GSource *source, gint *timeout)
301 static gboolean sock_check(GSource *source)
303 SockInfo *sock = ((SockSource *)source)->sock;
304 struct timeval timeout = {0, 0};
306 GIOCondition condition = sock->condition;
308 if (!sock || !sock->sock)
313 if (condition & G_IO_IN) {
314 if (SSL_pending(sock->ssl) > 0)
316 if (SSL_want_write(sock->ssl))
317 condition |= G_IO_OUT;
320 if (condition & G_IO_OUT) {
321 if (SSL_want_read(sock->ssl))
322 condition |= G_IO_IN;
328 FD_SET(sock->sock, &fds);
330 select(sock->sock + 1,
331 (condition & G_IO_IN) ? &fds : NULL,
332 (condition & G_IO_OUT) ? &fds : NULL,
335 return FD_ISSET(sock->sock, &fds) != 0;
338 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
341 SockInfo *sock = ((SockSource *)source)->sock;
343 if (!sock || !sock->callback || !sock->data)
346 return sock->callback(sock, sock->condition, sock->data);
349 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
352 SockInfo *sock = (SockInfo *)data;
354 if (!sock || !sock->callback || !sock->data) {
357 return sock->callback(sock, condition, sock->data);
360 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
363 sock->callback = func;
364 sock->condition = condition;
370 GSource *source = g_source_new(&sock_watch_funcs,
372 ((SockSource *) source)->sock = sock;
373 g_source_set_priority(source, G_PRIORITY_DEFAULT);
374 g_source_set_can_recurse(source, FALSE);
375 sock->g_source = g_source_attach(source, NULL);
376 return sock->g_source;
380 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
383 static gint fd_check_io(gint fd, GIOCondition cond)
385 struct timeval timeout;
388 if (is_nonblocking_mode(fd))
391 timeout.tv_sec = io_timeout;
397 if (cond == G_IO_IN) {
398 select(fd + 1, &fds, NULL, NULL,
399 io_timeout > 0 ? &timeout : NULL);
401 select(fd + 1, NULL, &fds, NULL,
402 io_timeout > 0 ? &timeout : NULL);
405 if (FD_ISSET(fd, &fds)) {
408 g_warning("Socket IO timeout\n");
413 static sigjmp_buf jmpenv;
415 static void timeout_handler(gint sig)
417 siglongjmp(jmpenv, 1);
420 static gint sock_connect_with_timeout(gint sock,
421 const struct sockaddr *serv_addr,
426 void (*prev_handler)(gint);
429 prev_handler = signal(SIGALRM, timeout_handler);
430 if (sigsetjmp(jmpenv, 1)) {
432 signal(SIGALRM, prev_handler);
438 ret = connect(sock, serv_addr, addrlen);
441 signal(SIGALRM, prev_handler);
446 struct hostent *my_gethostbyname(const gchar *hostname)
449 void (*prev_handler)(gint);
452 prev_handler = signal(SIGALRM, timeout_handler);
453 if (sigsetjmp(jmpenv, 1)) {
455 signal(SIGALRM, prev_handler);
456 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
462 if ((hp = gethostbyname(hostname)) == NULL) {
464 signal(SIGALRM, prev_handler);
465 fprintf(stderr, "%s: unknown host.\n", hostname);
471 signal(SIGALRM, prev_handler);
477 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
480 return inet_aton(hostname, inp);
485 inaddr = inet_addr(hostname);
487 memcpy(inp, &inaddr, sizeof(inaddr));
494 #endif /* HAVE_INET_ATON */
497 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
501 struct sockaddr_in ad;
503 memset(&ad, 0, sizeof(ad));
504 ad.sin_family = AF_INET;
505 ad.sin_port = htons(port);
509 if (!my_inet_aton(hostname, &ad.sin_addr)) {
510 if ((hp = my_gethostbyname(hostname)) == NULL) {
511 fprintf(stderr, "%s: unknown host.\n", hostname);
516 if (hp->h_length != 4 && hp->h_length != 8) {
517 fprintf(stderr, "illegal address length received for host %s\n", hostname);
522 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
525 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
526 sizeof(ad), io_timeout);
530 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
532 gint sock = -1, gai_error;
533 struct addrinfo hints, *res, *ai;
538 memset(&hints, 0, sizeof(hints));
539 /* hints.ai_flags = AI_CANONNAME; */
540 hints.ai_family = AF_UNSPEC;
541 hints.ai_socktype = SOCK_STREAM;
542 hints.ai_protocol = IPPROTO_TCP;
544 /* convert port from integer to string. */
545 g_snprintf(port_str, sizeof(port_str), "%d", port);
547 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
548 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
549 hostname, port_str, gai_strerror(gai_error));
553 for (ai = res; ai != NULL; ai = ai->ai_next) {
554 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
558 if (sock_connect_with_timeout
559 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
576 /* Open a connection using an external program. May be useful when
577 * you need to tunnel through a SOCKS or other firewall, or to
578 * establish an IMAP-over-SSH connection. */
579 /* TODO: Recreate this for sock_connect_thread() */
580 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
585 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
586 perror("socketpair");
589 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
594 dup(fd[1]); /* set onto stdin */
596 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
600 return sockinfo_from_fd(hostname, 0, fd[0]);
604 SockInfo *sock_connect(const gchar *hostname, gushort port)
609 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
612 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
617 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
618 if (errno != 0) perror("connect");
624 return sockinfo_from_fd(hostname, port, sock);
627 static void sock_address_list_free(GList *addr_list)
631 for (cur = addr_list; cur != NULL; cur = cur->next) {
632 SockAddrData *addr_data = (SockAddrData *)cur->data;
633 g_free(addr_data->addr);
637 g_list_free(addr_list);
640 /* asynchronous TCP connection */
642 static gboolean sock_connect_async_cb(GIOChannel *source,
643 GIOCondition condition, gpointer data)
645 SockConnectData *conn_data = (SockConnectData *)data;
651 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
654 fd = g_io_channel_unix_get_fd(source);
656 conn_data->io_tag = 0;
657 conn_data->channel = NULL;
658 g_io_channel_unref(source);
661 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
662 perror("getsockopt");
664 sock_connect_address_list_async(conn_data);
670 sock_connect_address_list_async(conn_data);
674 sockinfo = g_new0(SockInfo, 1);
676 sockinfo->sock_ch = g_io_channel_unix_new(fd);
677 sockinfo->hostname = g_strdup(conn_data->hostname);
678 sockinfo->port = conn_data->port;
679 sockinfo->state = CONN_ESTABLISHED;
681 conn_data->func(sockinfo, conn_data->data);
683 sock_connect_async_cancel(conn_data->id);
688 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
691 SockConnectData *conn_data = (SockConnectData *)data;
693 conn_data->addr_list = addr_list;
694 conn_data->cur_addr = addr_list;
695 conn_data->lookup_data = NULL;
697 return sock_connect_address_list_async(conn_data);
700 gint sock_connect_async(const gchar *hostname, gushort port,
701 SockConnectFunc func, gpointer data)
704 SockConnectData *conn_data;
706 conn_data = g_new0(SockConnectData, 1);
707 conn_data->id = id++;
708 conn_data->hostname = g_strdup(hostname);
709 conn_data->port = port;
710 conn_data->addr_list = NULL;
711 conn_data->cur_addr = NULL;
712 conn_data->io_tag = 0;
713 conn_data->func = func;
714 conn_data->data = data;
716 conn_data->lookup_data = sock_get_address_info_async
717 (hostname, port, sock_connect_async_get_address_info_cb,
720 if (conn_data->lookup_data == NULL) {
721 g_free(conn_data->hostname);
726 sock_connect_data_list = g_list_append(sock_connect_data_list,
729 return conn_data->id;
732 gint sock_connect_async_cancel(gint id)
734 SockConnectData *conn_data = NULL;
737 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
738 if (((SockConnectData *)cur->data)->id == id) {
739 conn_data = (SockConnectData *)cur->data;
745 sock_connect_data_list = g_list_remove(sock_connect_data_list,
748 if (conn_data->lookup_data)
749 sock_get_address_info_async_cancel
750 (conn_data->lookup_data);
752 if (conn_data->io_tag > 0)
753 g_source_remove(conn_data->io_tag);
754 if (conn_data->channel) {
755 g_io_channel_close(conn_data->channel);
756 g_io_channel_unref(conn_data->channel);
759 sock_address_list_free(conn_data->addr_list);
760 g_free(conn_data->hostname);
763 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
770 static gint sock_connect_address_list_async(SockConnectData *conn_data)
772 SockAddrData *addr_data;
775 for (; conn_data->cur_addr != NULL;
776 conn_data->cur_addr = conn_data->cur_addr->next) {
777 addr_data = (SockAddrData *)conn_data->cur_addr->data;
779 if ((sock = socket(addr_data->family, addr_data->socktype,
780 addr_data->protocol)) < 0) {
785 set_nonblocking_mode(sock, TRUE);
787 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
788 if (EINPROGRESS == errno) {
798 if (conn_data->cur_addr == NULL) {
799 g_warning("sock_connect_address_list_async: "
800 "connection to %s:%d failed\n",
801 conn_data->hostname, conn_data->port);
802 conn_data->func(NULL, conn_data->data);
803 sock_connect_async_cancel(conn_data->id);
807 conn_data->cur_addr = conn_data->cur_addr->next;
809 conn_data->channel = g_io_channel_unix_new(sock);
810 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
811 sock_connect_async_cb, conn_data);
816 /* asynchronous DNS lookup */
818 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
819 GIOCondition condition,
822 SockLookupData *lookup_data = (SockLookupData *)data;
823 GList *addr_list = NULL;
824 SockAddrData *addr_data;
827 struct sockaddr *addr;
830 if (g_io_channel_read(source, (gchar *)ai_member,
831 sizeof(ai_member), &bytes_read)
832 != G_IO_ERROR_NONE) {
833 g_warning("sock_get_address_info_async_cb: "
834 "address length read error\n");
838 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
841 if (ai_member[0] == AF_UNSPEC) {
842 g_warning("DNS lookup failed\n");
846 addr = g_malloc(ai_member[3]);
847 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
849 != G_IO_ERROR_NONE) {
850 g_warning("sock_get_address_info_async_cb: "
851 "address data read error\n");
856 if (bytes_read != ai_member[3]) {
857 g_warning("sock_get_address_info_async_cb: "
858 "incomplete address data\n");
863 addr_data = g_new0(SockAddrData, 1);
864 addr_data->family = ai_member[0];
865 addr_data->socktype = ai_member[1];
866 addr_data->protocol = ai_member[2];
867 addr_data->addr_len = ai_member[3];
868 addr_data->addr = addr;
870 addr_list = g_list_append(addr_list, addr_data);
873 g_io_channel_close(source);
874 g_io_channel_unref(source);
876 kill(lookup_data->child_pid, SIGKILL);
877 waitpid(lookup_data->child_pid, NULL, 0);
879 lookup_data->func(addr_list, lookup_data->data);
881 g_free(lookup_data->hostname);
887 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
892 SockLookupData *lookup_data = NULL;
898 if (pipe(pipe_fds) < 0) {
904 if ((pid = fork()) < 0) {
914 struct addrinfo hints, *res, *ai;
919 struct sockaddr_in ad;
921 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
926 memset(&hints, 0, sizeof(hints));
927 /* hints.ai_flags = AI_CANONNAME; */
928 hints.ai_family = AF_UNSPEC;
929 hints.ai_socktype = SOCK_STREAM;
930 hints.ai_protocol = IPPROTO_TCP;
932 g_snprintf(port_str, sizeof(port_str), "%d", port);
934 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
936 g_warning("getaddrinfo for %s:%s failed: %s\n",
937 hostname, port_str, gai_strerror(gai_err));
938 fd_write_all(pipe_fds[1], (gchar *)ai_member,
944 for (ai = res; ai != NULL; ai = ai->ai_next) {
945 ai_member[0] = ai->ai_family;
946 ai_member[1] = ai->ai_socktype;
947 ai_member[2] = ai->ai_protocol;
948 ai_member[3] = ai->ai_addrlen;
950 fd_write_all(pipe_fds[1], (gchar *)ai_member,
952 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
959 hp = my_gethostbyname(hostname);
960 if (hp == NULL || hp->h_addrtype != AF_INET) {
961 fd_write_all(pipe_fds[1], (gchar *)ai_member,
967 ai_member[0] = AF_INET;
968 ai_member[1] = SOCK_STREAM;
969 ai_member[2] = IPPROTO_TCP;
970 ai_member[3] = sizeof(ad);
972 memset(&ad, 0, sizeof(ad));
973 ad.sin_family = AF_INET;
974 ad.sin_port = htons(port);
976 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
978 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
979 fd_write_all(pipe_fds[1], (gchar *)ai_member,
981 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
991 lookup_data = g_new0(SockLookupData, 1);
992 lookup_data->hostname = g_strdup(hostname);
993 lookup_data->child_pid = pid;
994 lookup_data->func = func;
995 lookup_data->data = data;
997 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
998 lookup_data->io_tag = g_io_add_watch
999 (lookup_data->channel, G_IO_IN,
1000 sock_get_address_info_async_cb, lookup_data);
1006 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1008 if (lookup_data->io_tag > 0)
1009 g_source_remove(lookup_data->io_tag);
1010 if (lookup_data->channel) {
1011 g_io_channel_close(lookup_data->channel);
1012 g_io_channel_unref(lookup_data->channel);
1015 if (lookup_data->child_pid > 0) {
1016 kill(lookup_data->child_pid, SIGKILL);
1017 waitpid(lookup_data->child_pid, NULL, 0);
1020 g_free(lookup_data->hostname);
1021 g_free(lookup_data);
1027 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1033 sockinfo = g_new0(SockInfo, 1);
1034 sockinfo->sock = sock;
1035 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1036 sockinfo->hostname = g_strdup(hostname);
1037 sockinfo->port = port;
1038 sockinfo->state = CONN_ESTABLISHED;
1043 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1046 gchar buf[BUFFSIZE];
1048 va_start(args, format);
1049 g_vsnprintf(buf, sizeof(buf), format, args);
1052 return sock_write_all(sock, buf, strlen(buf));
1055 gint fd_read(gint fd, gchar *buf, gint len)
1057 if (fd_check_io(fd, G_IO_IN) < 0)
1060 return read(fd, buf, len);
1064 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1068 if (SSL_pending(ssl) == 0) {
1069 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1073 ret = SSL_read(ssl, buf, len);
1075 switch (SSL_get_error(ssl, ret)) {
1076 case SSL_ERROR_NONE:
1078 case SSL_ERROR_WANT_READ:
1079 case SSL_ERROR_WANT_WRITE:
1088 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1092 g_return_val_if_fail(sock != NULL, -1);
1096 ret = ssl_read(sock->ssl, buf, len);
1099 ret = fd_read(sock->sock, buf, len);
1102 sock->state = CONN_DISCONNECTED;
1106 gint fd_write(gint fd, const gchar *buf, gint len)
1108 if (fd_check_io(fd, G_IO_OUT) < 0)
1111 return write(fd, buf, len);
1115 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1119 ret = SSL_write(ssl, buf, len);
1121 switch (SSL_get_error(ssl, ret)) {
1122 case SSL_ERROR_NONE:
1124 case SSL_ERROR_WANT_READ:
1125 case SSL_ERROR_WANT_WRITE:
1134 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1138 g_return_val_if_fail(sock != NULL, -1);
1142 ret = ssl_write(sock->ssl, buf, len);
1145 ret = fd_write(sock->sock, buf, len);
1148 sock->state = CONN_DISCONNECTED;
1152 gint fd_write_all(gint fd, const gchar *buf, gint len)
1157 if (fd_check_io(fd, G_IO_OUT) < 0)
1159 signal(SIGPIPE, SIG_IGN);
1160 n = write(fd, buf, len);
1162 log_error("write on fd%d: %s\n", fd, strerror(errno));
1174 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1179 n = ssl_write(ssl, buf, len);
1191 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1195 g_return_val_if_fail(sock != NULL, -1);
1199 ret = ssl_write_all(sock->ssl, buf, len);
1202 ret = fd_write_all(sock->sock, buf, len);
1205 sock->state = CONN_DISCONNECTED;
1209 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1211 if (fd_check_io(fd, G_IO_IN) < 0)
1214 return recv(fd, buf, len, flags);
1217 gint fd_gets(gint fd, gchar *buf, gint len)
1219 gchar *newline, *bp = buf;
1225 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1227 if ((newline = memchr(bp, '\n', n)) != NULL)
1228 n = newline - bp + 1;
1229 if ((n = fd_read(fd, bp, n)) < 0)
1233 } while (!newline && len);
1240 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1242 gchar *newline, *bp = buf;
1248 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1250 if ((newline = memchr(bp, '\n', n)) != NULL)
1251 n = newline - bp + 1;
1252 if ((n = ssl_read(ssl, bp, n)) < 0)
1256 } while (!newline && len);
1263 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1267 g_return_val_if_fail(sock != NULL, -1);
1271 return ssl_gets(sock->ssl, buf, len);
1274 return fd_gets(sock->sock, buf, len);
1277 sock->state = CONN_DISCONNECTED;
1281 gint fd_getline(gint fd, gchar **str)
1283 gchar buf[BUFFSIZE];
1287 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1290 *str = g_strdup(buf);
1292 *str = g_realloc(*str, size);
1295 if (buf[len - 1] == '\n')
1298 if (len == -1 && *str)
1305 gint ssl_getline(SSL *ssl, gchar **str)
1307 gchar buf[BUFFSIZE];
1311 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1314 *str = g_strdup(buf);
1316 *str = g_realloc(*str, size);
1319 if (buf[len - 1] == '\n')
1322 if (len == -1 && *str)
1329 gchar *sock_getline(SockInfo *sock)
1334 g_return_val_if_fail(sock != NULL, NULL);
1338 ret = ssl_getline(sock->ssl, &str);
1341 ret = fd_getline(sock->sock, &str);
1344 sock->state = CONN_DISCONNECTED;
1348 gint sock_puts(SockInfo *sock, const gchar *buf)
1352 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1354 return sock_write_all(sock, "\r\n", 2);
1357 /* peek at the socket data without actually reading it */
1359 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1363 if (SSL_pending(ssl) == 0) {
1364 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1368 ret = SSL_peek(ssl, buf, len);
1370 switch (SSL_get_error(ssl, ret)) {
1371 case SSL_ERROR_NONE:
1373 case SSL_ERROR_WANT_READ:
1374 case SSL_ERROR_WANT_WRITE:
1383 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1385 g_return_val_if_fail(sock != NULL, -1);
1389 return ssl_peek(sock->ssl, buf, len);
1391 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1394 gint sock_close(SockInfo *sock)
1402 g_io_channel_unref(sock->sock_ch);
1406 ssl_done_socket(sock);
1407 if (sock->g_source != 0)
1408 g_source_remove(sock->g_source);
1411 ret = fd_close(sock->sock);
1412 g_free(sock->hostname);
1418 gint fd_close(gint fd)