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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <sys/types.h>
28 # include <winsock2.h>
31 # include <sys/wait.h>
33 # include <sys/socket.h>
34 # include <sys/stat.h>
36 # include <netinet/in.h>
37 # include <arpa/inet.h>
40 #endif /* G_OS_WIN32 */
50 # include <sys/select.h>
61 #error USE_GIO is currently not supported
66 typedef gint (*SockAddrFunc) (GList *addr_list,
69 typedef struct _SockConnectData SockConnectData;
70 typedef struct _SockLookupData SockLookupData;
71 typedef struct _SockAddrData SockAddrData;
72 typedef struct _SockSource SockSource;
74 struct _SockConnectData {
80 SockLookupData *lookup_data;
87 struct _SockLookupData {
96 struct _SockAddrData {
101 struct sockaddr *addr;
109 static guint io_timeout = 60;
111 static GList *sock_connect_data_list = NULL;
113 static gboolean sock_prepare (GSource *source,
115 static gboolean sock_check (GSource *source);
116 static gboolean sock_dispatch (GSource *source,
117 GSourceFunc callback,
120 GSourceFuncs sock_watch_funcs = {
127 static gint sock_connect_with_timeout (gint sock,
128 const struct sockaddr *serv_addr,
133 static gint sock_connect_by_hostname (gint sock,
134 const gchar *hostname,
137 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
141 static SockInfo *sockinfo_from_fd(const gchar *hostname,
145 static void sock_address_list_free (GList *addr_list);
147 static gboolean sock_connect_async_cb (GIOChannel *source,
148 GIOCondition condition,
150 static gint sock_connect_async_get_address_info_cb
154 static gint sock_connect_address_list_async (SockConnectData *conn_data);
156 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
157 GIOCondition condition,
159 static SockLookupData *sock_get_address_info_async
160 (const gchar *hostname,
164 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
165 #endif /* G_OS_UNIX */
174 result = WSAStartup(MAKEWORD(2, 2), &wsadata);
175 if (result != NO_ERROR) {
176 g_warning("WSAStartup() failed\n");
183 gint sock_cleanup(void)
191 gint sock_set_io_timeout(guint sec)
197 void refresh_resolvers(void)
199 static time_t resolv_conf_changed = (time_t)NULL;
202 /* This makes the glibc re-read resolv.conf, if it changed
203 * since our startup. Maybe that should be #ifdef'ed, I don't
204 * know if it'd work on BSDs.
205 * Why doesn't the glibc do it by itself?
207 if (stat("/etc/resolv.conf", &s) == 0) {
208 if (s.st_mtime > resolv_conf_changed) {
209 resolv_conf_changed = s.st_mtime;
213 we'll have bigger problems. */
216 gint fd_connect_unix(const gchar *path)
220 struct sockaddr_un addr;
222 sock = socket(PF_UNIX, SOCK_STREAM, 0);
224 perror("sock_connect_unix(): socket");
228 memset(&addr, 0, sizeof(addr));
229 addr.sun_family = AF_UNIX;
230 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
232 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
243 gint fd_open_unix(const gchar *path)
247 struct sockaddr_un addr;
249 sock = socket(PF_UNIX, SOCK_STREAM, 0);
252 perror("sock_open_unix(): socket");
256 memset(&addr, 0, sizeof(addr));
257 addr.sun_family = AF_UNIX;
258 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
260 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
266 if (listen(sock, 1) < 0) {
278 gint fd_accept(gint sock)
281 struct sockaddr_in caddr;
284 caddr_len = sizeof(caddr);
285 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
292 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
297 flags = fcntl(fd, F_GETFL, 0);
306 flags &= ~O_NONBLOCK;
308 return fcntl(fd, F_SETFL, flags);
314 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
316 g_return_val_if_fail(sock != NULL, -1);
318 return set_nonblocking_mode(sock->sock, nonblock);
321 static gboolean is_nonblocking_mode(gint fd)
326 flags = fcntl(fd, F_GETFL, 0);
332 return ((flags & O_NONBLOCK) != 0);
338 gboolean sock_is_nonblocking_mode(SockInfo *sock)
340 g_return_val_if_fail(sock != NULL, FALSE);
342 return is_nonblocking_mode(sock->sock);
346 static gboolean sock_prepare(GSource *source, gint *timeout)
352 static gboolean sock_check(GSource *source)
354 SockInfo *sock = ((SockSource *)source)->sock;
355 struct timeval timeout = {0, 0};
357 GIOCondition condition = sock->condition;
359 if (!sock || !sock->sock)
364 if (condition & G_IO_IN) {
365 if (SSL_pending(sock->ssl) > 0)
367 if (SSL_want_write(sock->ssl))
368 condition |= G_IO_OUT;
371 if (condition & G_IO_OUT) {
372 if (SSL_want_read(sock->ssl))
373 condition |= G_IO_IN;
379 FD_SET(sock->sock, &fds);
381 select(sock->sock + 1,
382 (condition & G_IO_IN) ? &fds : NULL,
383 (condition & G_IO_OUT) ? &fds : NULL,
386 return FD_ISSET(sock->sock, &fds) != 0;
389 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
392 SockInfo *sock = ((SockSource *)source)->sock;
394 if (!sock || !sock->callback || !sock->data)
397 return sock->callback(sock, sock->condition, sock->data);
400 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
403 SockInfo *sock = (SockInfo *)data;
405 if ((condition & sock->condition) == 0)
408 return sock->callback(sock, sock->condition, sock->data);
411 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
414 sock->callback = func;
415 sock->condition = condition;
421 GSource *source = g_source_new(&sock_watch_funcs,
423 ((SockSource *) source)->sock = sock;
424 g_source_set_priority(source, G_PRIORITY_DEFAULT);
425 g_source_set_can_recurse(source, FALSE);
426 sock->g_source = g_source_attach(source, NULL);
427 return sock->g_source;
431 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
434 static gint fd_check_io(gint fd, GIOCondition cond)
436 struct timeval timeout;
439 if (is_nonblocking_mode(fd))
442 timeout.tv_sec = io_timeout;
448 if (cond == G_IO_IN) {
449 select(fd + 1, &fds, NULL, NULL,
450 io_timeout > 0 ? &timeout : NULL);
452 select(fd + 1, NULL, &fds, NULL,
453 io_timeout > 0 ? &timeout : NULL);
456 if (FD_ISSET(fd, &fds)) {
459 g_warning("Socket IO timeout\n");
465 static sigjmp_buf jmpenv;
467 static void timeout_handler(gint sig)
469 siglongjmp(jmpenv, 1);
473 static gint sock_connect_with_timeout(gint sock,
474 const struct sockaddr *serv_addr,
480 void (*prev_handler)(gint);
483 prev_handler = signal(SIGALRM, timeout_handler);
484 if (sigsetjmp(jmpenv, 1)) {
486 signal(SIGALRM, prev_handler);
493 ret = connect(sock, serv_addr, addrlen);
497 signal(SIGALRM, prev_handler);
503 struct hostent *my_gethostbyname(const gchar *hostname)
507 void (*prev_handler)(gint);
510 prev_handler = signal(SIGALRM, timeout_handler);
511 if (sigsetjmp(jmpenv, 1)) {
513 signal(SIGALRM, prev_handler);
514 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
521 if ((hp = gethostbyname(hostname)) == NULL) {
524 signal(SIGALRM, prev_handler);
526 fprintf(stderr, "%s: unknown host.\n", hostname);
533 signal(SIGALRM, prev_handler);
540 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
543 return inet_aton(hostname, inp);
548 inaddr = inet_addr(hostname);
550 memcpy(inp, &inaddr, sizeof(inaddr));
557 #endif /* HAVE_INET_ATON */
560 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
564 struct sockaddr_in ad;
566 memset(&ad, 0, sizeof(ad));
567 ad.sin_family = AF_INET;
568 ad.sin_port = htons(port);
572 if (!my_inet_aton(hostname, &ad.sin_addr)) {
573 if ((hp = my_gethostbyname(hostname)) == NULL) {
574 fprintf(stderr, "%s: unknown host.\n", hostname);
579 if (hp->h_length != 4 && hp->h_length != 8) {
580 fprintf(stderr, "illegal address length received for host %s\n", hostname);
585 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
588 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
589 sizeof(ad), io_timeout);
593 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
595 gint sock = -1, gai_error;
596 struct addrinfo hints, *res, *ai;
601 memset(&hints, 0, sizeof(hints));
602 /* hints.ai_flags = AI_CANONNAME; */
603 hints.ai_family = AF_UNSPEC;
604 hints.ai_socktype = SOCK_STREAM;
605 hints.ai_protocol = IPPROTO_TCP;
607 /* convert port from integer to string. */
608 g_snprintf(port_str, sizeof(port_str), "%d", port);
610 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
611 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
612 hostname, port_str, gai_strerror(gai_error));
616 for (ai = res; ai != NULL; ai = ai->ai_next) {
617 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
621 if (sock_connect_with_timeout
622 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
639 /* Open a connection using an external program. May be useful when
640 * you need to tunnel through a SOCKS or other firewall, or to
641 * establish an IMAP-over-SSH connection. */
642 /* TODO: Recreate this for sock_connect_thread() */
643 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
648 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
649 perror("socketpair");
652 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
657 dup(fd[1]); /* set onto stdin */
659 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
663 return sockinfo_from_fd(hostname, 0, fd[0]);
667 SockInfo *sock_connect(const gchar *hostname, gushort port)
676 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
680 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
681 g_warning("socket() failed: %ld\n", WSAGetLastError());
683 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
685 #endif /* G_OS_WIN32 */
689 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
690 if (errno != 0) perror("connect");
696 return sockinfo_from_fd(hostname, port, sock);
700 static void sock_address_list_free(GList *addr_list)
704 for (cur = addr_list; cur != NULL; cur = cur->next) {
705 SockAddrData *addr_data = (SockAddrData *)cur->data;
706 g_free(addr_data->addr);
710 g_list_free(addr_list);
713 /* asynchronous TCP connection */
715 static gboolean sock_connect_async_cb(GIOChannel *source,
716 GIOCondition condition, gpointer data)
718 SockConnectData *conn_data = (SockConnectData *)data;
724 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
727 fd = g_io_channel_unix_get_fd(source);
729 conn_data->io_tag = 0;
730 conn_data->channel = NULL;
731 g_io_channel_unref(source);
734 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
735 perror("getsockopt");
737 sock_connect_address_list_async(conn_data);
743 sock_connect_address_list_async(conn_data);
747 sockinfo = g_new0(SockInfo, 1);
749 sockinfo->sock_ch = g_io_channel_unix_new(fd);
750 sockinfo->hostname = g_strdup(conn_data->hostname);
751 sockinfo->port = conn_data->port;
752 sockinfo->state = CONN_ESTABLISHED;
754 conn_data->func(sockinfo, conn_data->data);
756 sock_connect_async_cancel(conn_data->id);
761 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
764 SockConnectData *conn_data = (SockConnectData *)data;
766 conn_data->addr_list = addr_list;
767 conn_data->cur_addr = addr_list;
768 conn_data->lookup_data = NULL;
770 return sock_connect_address_list_async(conn_data);
773 gint sock_connect_async(const gchar *hostname, gushort port,
774 SockConnectFunc func, gpointer data)
777 SockConnectData *conn_data;
779 conn_data = g_new0(SockConnectData, 1);
780 conn_data->id = id++;
781 conn_data->hostname = g_strdup(hostname);
782 conn_data->port = port;
783 conn_data->addr_list = NULL;
784 conn_data->cur_addr = NULL;
785 conn_data->io_tag = 0;
786 conn_data->func = func;
787 conn_data->data = data;
789 conn_data->lookup_data = sock_get_address_info_async
790 (hostname, port, sock_connect_async_get_address_info_cb,
793 if (conn_data->lookup_data == NULL) {
794 g_free(conn_data->hostname);
799 sock_connect_data_list = g_list_append(sock_connect_data_list,
802 return conn_data->id;
805 gint sock_connect_async_cancel(gint id)
807 SockConnectData *conn_data = NULL;
810 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
811 if (((SockConnectData *)cur->data)->id == id) {
812 conn_data = (SockConnectData *)cur->data;
818 sock_connect_data_list = g_list_remove(sock_connect_data_list,
821 if (conn_data->lookup_data)
822 sock_get_address_info_async_cancel
823 (conn_data->lookup_data);
825 if (conn_data->io_tag > 0)
826 g_source_remove(conn_data->io_tag);
827 if (conn_data->channel) {
828 g_io_channel_close(conn_data->channel);
829 g_io_channel_unref(conn_data->channel);
832 sock_address_list_free(conn_data->addr_list);
833 g_free(conn_data->hostname);
836 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
843 static gint sock_connect_address_list_async(SockConnectData *conn_data)
845 SockAddrData *addr_data;
848 for (; conn_data->cur_addr != NULL;
849 conn_data->cur_addr = conn_data->cur_addr->next) {
850 addr_data = (SockAddrData *)conn_data->cur_addr->data;
852 if ((sock = socket(addr_data->family, addr_data->socktype,
853 addr_data->protocol)) < 0) {
858 set_nonblocking_mode(sock, TRUE);
860 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
861 if (EINPROGRESS == errno) {
871 if (conn_data->cur_addr == NULL) {
872 g_warning("sock_connect_address_list_async: "
873 "connection to %s:%d failed\n",
874 conn_data->hostname, conn_data->port);
875 conn_data->func(NULL, conn_data->data);
876 sock_connect_async_cancel(conn_data->id);
880 conn_data->cur_addr = conn_data->cur_addr->next;
882 conn_data->channel = g_io_channel_unix_new(sock);
883 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
884 sock_connect_async_cb, conn_data);
889 /* asynchronous DNS lookup */
891 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
892 GIOCondition condition,
895 SockLookupData *lookup_data = (SockLookupData *)data;
896 GList *addr_list = NULL;
897 SockAddrData *addr_data;
900 struct sockaddr *addr;
903 if (g_io_channel_read(source, (gchar *)ai_member,
904 sizeof(ai_member), &bytes_read)
905 != G_IO_ERROR_NONE) {
906 g_warning("sock_get_address_info_async_cb: "
907 "address length read error\n");
911 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
914 if (ai_member[0] == AF_UNSPEC) {
915 g_warning("DNS lookup failed\n");
919 addr = g_malloc(ai_member[3]);
920 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
922 != G_IO_ERROR_NONE) {
923 g_warning("sock_get_address_info_async_cb: "
924 "address data read error\n");
929 if (bytes_read != ai_member[3]) {
930 g_warning("sock_get_address_info_async_cb: "
931 "incomplete address data\n");
936 addr_data = g_new0(SockAddrData, 1);
937 addr_data->family = ai_member[0];
938 addr_data->socktype = ai_member[1];
939 addr_data->protocol = ai_member[2];
940 addr_data->addr_len = ai_member[3];
941 addr_data->addr = addr;
943 addr_list = g_list_append(addr_list, addr_data);
946 g_io_channel_close(source);
947 g_io_channel_unref(source);
949 kill(lookup_data->child_pid, SIGKILL);
950 waitpid(lookup_data->child_pid, NULL, 0);
952 lookup_data->func(addr_list, lookup_data->data);
954 g_free(lookup_data->hostname);
960 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
965 SockLookupData *lookup_data = NULL;
971 if (pipe(pipe_fds) < 0) {
977 if ((pid = fork()) < 0) {
987 struct addrinfo hints, *res, *ai;
992 struct sockaddr_in ad;
994 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
999 memset(&hints, 0, sizeof(hints));
1000 /* hints.ai_flags = AI_CANONNAME; */
1001 hints.ai_family = AF_UNSPEC;
1002 hints.ai_socktype = SOCK_STREAM;
1003 hints.ai_protocol = IPPROTO_TCP;
1005 g_snprintf(port_str, sizeof(port_str), "%d", port);
1007 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
1009 g_warning("getaddrinfo for %s:%s failed: %s\n",
1010 hostname, port_str, gai_strerror(gai_err));
1011 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1017 for (ai = res; ai != NULL; ai = ai->ai_next) {
1018 ai_member[0] = ai->ai_family;
1019 ai_member[1] = ai->ai_socktype;
1020 ai_member[2] = ai->ai_protocol;
1021 ai_member[3] = ai->ai_addrlen;
1023 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1025 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
1032 hp = my_gethostbyname(hostname);
1033 if (hp == NULL || hp->h_addrtype != AF_INET) {
1034 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1040 ai_member[0] = AF_INET;
1041 ai_member[1] = SOCK_STREAM;
1042 ai_member[2] = IPPROTO_TCP;
1043 ai_member[3] = sizeof(ad);
1045 memset(&ad, 0, sizeof(ad));
1046 ad.sin_family = AF_INET;
1047 ad.sin_port = htons(port);
1049 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
1051 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
1052 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1054 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
1064 lookup_data = g_new0(SockLookupData, 1);
1065 lookup_data->hostname = g_strdup(hostname);
1066 lookup_data->child_pid = pid;
1067 lookup_data->func = func;
1068 lookup_data->data = data;
1070 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
1071 lookup_data->io_tag = g_io_add_watch
1072 (lookup_data->channel, G_IO_IN,
1073 sock_get_address_info_async_cb, lookup_data);
1079 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1081 if (lookup_data->io_tag > 0)
1082 g_source_remove(lookup_data->io_tag);
1083 if (lookup_data->channel) {
1084 g_io_channel_close(lookup_data->channel);
1085 g_io_channel_unref(lookup_data->channel);
1088 if (lookup_data->child_pid > 0) {
1089 kill(lookup_data->child_pid, SIGKILL);
1090 waitpid(lookup_data->child_pid, NULL, 0);
1093 g_free(lookup_data->hostname);
1094 g_free(lookup_data);
1098 #endif /* G_OS_UNIX */
1101 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1107 sockinfo = g_new0(SockInfo, 1);
1108 sockinfo->sock = sock;
1109 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1110 sockinfo->hostname = g_strdup(hostname);
1111 sockinfo->port = port;
1112 sockinfo->state = CONN_ESTABLISHED;
1117 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1120 gchar buf[BUFFSIZE];
1122 va_start(args, format);
1123 g_vsnprintf(buf, sizeof(buf), format, args);
1126 return sock_write_all(sock, buf, strlen(buf));
1129 gint fd_read(gint fd, gchar *buf, gint len)
1131 if (fd_check_io(fd, G_IO_IN) < 0)
1135 return recv(fd, buf, len, 0);
1137 return read(fd, buf, len);
1142 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1146 if (SSL_pending(ssl) == 0) {
1147 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1151 ret = SSL_read(ssl, buf, len);
1153 switch ((err = SSL_get_error(ssl, ret))) {
1154 case SSL_ERROR_NONE:
1156 case SSL_ERROR_WANT_READ:
1157 case SSL_ERROR_WANT_WRITE:
1160 case SSL_ERROR_ZERO_RETURN:
1163 g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
1171 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1175 g_return_val_if_fail(sock != NULL, -1);
1179 ret = ssl_read(sock->ssl, buf, len);
1182 ret = fd_read(sock->sock, buf, len);
1185 sock->state = CONN_DISCONNECTED;
1189 gint fd_write(gint fd, const gchar *buf, gint len)
1191 if (fd_check_io(fd, G_IO_OUT) < 0)
1195 return send(fd, buf, len, 0);
1197 return write(fd, buf, len);
1202 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1206 ret = SSL_write(ssl, buf, len);
1208 switch (SSL_get_error(ssl, ret)) {
1209 case SSL_ERROR_NONE:
1211 case SSL_ERROR_WANT_READ:
1212 case SSL_ERROR_WANT_WRITE:
1221 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1225 g_return_val_if_fail(sock != NULL, -1);
1229 ret = ssl_write(sock->ssl, buf, len);
1232 ret = fd_write(sock->sock, buf, len);
1235 sock->state = CONN_DISCONNECTED;
1239 gint fd_write_all(gint fd, const gchar *buf, gint len)
1244 if (fd_check_io(fd, G_IO_OUT) < 0)
1246 signal(SIGPIPE, SIG_IGN);
1247 n = write(fd, buf, len);
1249 log_error("write on fd%d: %s\n", fd, strerror(errno));
1261 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1266 n = ssl_write(ssl, buf, len);
1278 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1282 g_return_val_if_fail(sock != NULL, -1);
1286 ret = ssl_write_all(sock->ssl, buf, len);
1289 ret = fd_write_all(sock->sock, buf, len);
1292 sock->state = CONN_DISCONNECTED;
1296 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1298 if (fd_check_io(fd, G_IO_IN) < 0)
1301 return recv(fd, buf, len, flags);
1304 gint fd_gets(gint fd, gchar *buf, gint len)
1306 gchar *newline, *bp = buf;
1312 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1314 if ((newline = memchr(bp, '\n', n)) != NULL)
1315 n = newline - bp + 1;
1316 if ((n = fd_read(fd, bp, n)) < 0)
1320 } while (!newline && len);
1327 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1329 gchar *newline, *bp = buf;
1335 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1337 if ((newline = memchr(bp, '\n', n)) != NULL)
1338 n = newline - bp + 1;
1339 if ((n = ssl_read(ssl, bp, n)) < 0)
1343 } while (!newline && len);
1350 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1354 g_return_val_if_fail(sock != NULL, -1);
1358 return ssl_gets(sock->ssl, buf, len);
1361 return fd_gets(sock->sock, buf, len);
1364 sock->state = CONN_DISCONNECTED;
1368 gint fd_getline(gint fd, gchar **str)
1370 gchar buf[BUFFSIZE];
1374 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1377 *str = g_strdup(buf);
1379 *str = g_realloc(*str, size);
1382 if (buf[len - 1] == '\n')
1385 if (len == -1 && *str)
1392 gint ssl_getline(SSL *ssl, gchar **str)
1394 gchar buf[BUFFSIZE];
1398 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1401 *str = g_strdup(buf);
1403 *str = g_realloc(*str, size);
1406 if (buf[len - 1] == '\n')
1409 if (len == -1 && *str)
1416 gchar *sock_getline(SockInfo *sock)
1421 g_return_val_if_fail(sock != NULL, NULL);
1425 ret = ssl_getline(sock->ssl, &str);
1428 ret = fd_getline(sock->sock, &str);
1431 sock->state = CONN_DISCONNECTED;
1435 gint sock_puts(SockInfo *sock, const gchar *buf)
1439 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1441 return sock_write_all(sock, "\r\n", 2);
1444 /* peek at the socket data without actually reading it */
1446 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1450 if (SSL_pending(ssl) == 0) {
1451 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1455 ret = SSL_peek(ssl, buf, len);
1457 switch ((err = SSL_get_error(ssl, ret))) {
1458 case SSL_ERROR_NONE:
1460 case SSL_ERROR_WANT_READ:
1461 case SSL_ERROR_WANT_WRITE:
1464 case SSL_ERROR_ZERO_RETURN:
1466 case SSL_ERROR_SYSCALL:
1467 g_warning("SSL_peek() returned syscall error. errno=%d\n", errno);
1470 g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
1478 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1480 g_return_val_if_fail(sock != NULL, -1);
1484 return ssl_peek(sock->ssl, buf, len);
1486 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1489 gint sock_close(SockInfo *sock)
1497 g_io_channel_unref(sock->sock_ch);
1501 ssl_done_socket(sock);
1502 if (sock->g_source != 0)
1503 g_source_remove(sock->g_source);
1506 ret = fd_close(sock->sock);
1507 g_free(sock->hostname);
1513 gint fd_close(gint fd)