2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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.
24 #if (defined (_XOPEN_SOURCE) && !defined (_BSD_SOURCE))
29 #include <glib/gi18n.h>
32 #include <sys/types.h>
34 # include <winsock2.h>
36 # define EINPROGRESS WSAEINPROGRESS
40 # include <sys/wait.h>
42 # include <sys/socket.h>
43 # include <sys/stat.h>
45 # include <netinet/in.h>
46 # include <arpa/inet.h>
49 #endif /* G_OS_WIN32 */
59 # include <sys/select.h>
70 #error USE_GIO is currently not supported
80 typedef gint (*SockAddrFunc) (GList *addr_list,
83 typedef struct _SockConnectData SockConnectData;
84 typedef struct _SockLookupData SockLookupData;
85 typedef struct _SockAddrData SockAddrData;
86 typedef struct _SockSource SockSource;
88 struct _SockConnectData {
94 SockLookupData *lookup_data;
99 gchar *canonical_name;
102 struct _SockLookupData {
111 gchar *canonical_name;
114 struct _SockAddrData {
119 struct sockaddr *addr;
127 static guint io_timeout = 60;
129 static GList *sock_connect_data_list = NULL;
131 static gboolean sock_prepare (GSource *source,
133 static gboolean sock_check (GSource *source);
134 static gboolean sock_dispatch (GSource *source,
135 GSourceFunc callback,
138 GSourceFuncs sock_watch_funcs = {
145 static gint sock_connect_with_timeout (gint sock,
146 const struct sockaddr *serv_addr,
151 static gint sock_connect_by_hostname (gint sock,
152 const gchar *hostname,
155 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
159 static SockInfo *sockinfo_from_fd(const gchar *hostname,
162 static void sock_address_list_free (GList *addr_list);
164 static gboolean sock_connect_async_cb (GIOChannel *source,
165 GIOCondition condition,
167 static gint sock_connect_async_get_address_info_cb
171 static gint sock_connect_address_list_async (SockConnectData *conn_data);
173 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
174 GIOCondition condition,
176 static SockLookupData *sock_get_address_info_async
177 (const gchar *hostname,
181 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
190 result = WSAStartup(MAKEWORD(2, 2), &wsadata);
191 if (result != NO_ERROR) {
192 g_warning("WSAStartup() failed\n");
199 gint sock_cleanup(void)
207 gint sock_set_io_timeout(guint sec)
213 void refresh_resolvers(void)
216 static time_t resolv_conf_changed = (time_t)NULL;
219 /* This makes the glibc re-read resolv.conf, if it changed
220 * since our startup. Maybe that should be #ifdef'ed, I don't
221 * know if it'd work on BSDs.
222 * Why doesn't the glibc do it by itself?
224 if (stat("/etc/resolv.conf", &s) == 0) {
225 if (s.st_mtime > resolv_conf_changed) {
226 resolv_conf_changed = s.st_mtime;
230 we'll have bigger problems. */
235 /* Due to the fact that socket under Windows are not represented by
236 standard file descriptors, we sometimes need to check whether a
237 given file descriptor is actually a socket. This is done by
238 testing for an error. Returns true under W32 if FD is a socket. */
239 static int fd_is_w32_socket(gint fd)
243 gint retval = sizeof(optval);
245 return !getsockopt(fd, SOL_SOCKET, SO_TYPE, (char*)&optval, &retval);
252 gint fd_connect_unix(const gchar *path)
256 struct sockaddr_un addr;
258 sock = socket(PF_UNIX, SOCK_STREAM, 0);
260 perror("sock_connect_unix(): socket");
264 memset(&addr, 0, sizeof(addr));
265 addr.sun_family = AF_UNIX;
266 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
268 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
279 gint fd_open_unix(const gchar *path)
283 struct sockaddr_un addr;
285 sock = socket(PF_UNIX, SOCK_STREAM, 0);
288 perror("sock_open_unix(): socket");
292 memset(&addr, 0, sizeof(addr));
293 addr.sun_family = AF_UNIX;
294 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
296 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
297 gchar *buf = g_strdup_printf("can't bind to %s", path);
304 if (listen(sock, 1) < 0) {
305 gchar *buf = g_strdup_printf("can't listen on %s", path);
318 gint fd_accept(gint sock)
320 struct sockaddr_in caddr;
323 caddr_len = sizeof(caddr);
324 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
328 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
333 flags = fcntl(fd, F_GETFL, 0);
342 flags &= ~O_NONBLOCK;
344 return fcntl(fd, F_SETFL, flags);
350 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
352 g_return_val_if_fail(sock != NULL, -1);
354 return set_nonblocking_mode(sock->sock, nonblock);
357 static gboolean is_nonblocking_mode(gint fd)
362 flags = fcntl(fd, F_GETFL, 0);
368 return ((flags & O_NONBLOCK) != 0);
374 gboolean sock_is_nonblocking_mode(SockInfo *sock)
376 g_return_val_if_fail(sock != NULL, FALSE);
378 return is_nonblocking_mode(sock->sock);
382 static gboolean sock_prepare(GSource *source, gint *timeout)
388 static gboolean sock_check(GSource *source)
390 SockInfo *sock = ((SockSource *)source)->sock;
391 struct timeval timeout = {0, 0};
393 GIOCondition condition = sock->condition;
395 if (!sock || !sock->sock)
400 if (condition & G_IO_IN) {
401 if (SSL_pending(sock->ssl) > 0)
403 if (SSL_want_write(sock->ssl))
404 condition |= G_IO_OUT;
407 if (condition & G_IO_OUT) {
408 if (SSL_want_read(sock->ssl))
409 condition |= G_IO_IN;
415 FD_SET(sock->sock, &fds);
417 select(sock->sock + 1,
418 (condition & G_IO_IN) ? &fds : NULL,
419 (condition & G_IO_OUT) ? &fds : NULL,
422 return FD_ISSET(sock->sock, &fds) != 0;
425 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
428 SockInfo *sock = ((SockSource *)source)->sock;
430 if (!sock || !sock->callback || !sock->data)
433 return sock->callback(sock, sock->condition, sock->data);
436 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
439 SockInfo *sock = (SockInfo *)data;
441 if ((condition & sock->condition) == 0)
444 return sock->callback(sock, sock->condition, sock->data);
447 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
450 sock->callback = func;
451 sock->condition = condition;
457 GSource *source = g_source_new(&sock_watch_funcs,
459 ((SockSource *) source)->sock = sock;
460 g_source_set_priority(source, G_PRIORITY_DEFAULT);
461 g_source_set_can_recurse(source, FALSE);
462 sock->g_source = g_source_attach(source, NULL);
463 g_source_unref (source); /* Refcount back down to 1 */
464 return sock->g_source;
468 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
471 static gint fd_check_io(gint fd, GIOCondition cond)
473 struct timeval timeout;
476 if (is_nonblocking_mode(fd))
479 timeout.tv_sec = io_timeout;
485 if (cond == G_IO_IN) {
486 select(fd + 1, &fds, NULL, NULL,
487 io_timeout > 0 ? &timeout : NULL);
489 select(fd + 1, NULL, &fds, NULL,
490 io_timeout > 0 ? &timeout : NULL);
493 if (FD_ISSET(fd, &fds)) {
496 g_warning("Socket IO timeout\n");
502 static sigjmp_buf jmpenv;
504 static void timeout_handler(gint sig)
506 siglongjmp(jmpenv, 1);
510 static gint sock_connect_with_timeout(gint sock,
511 const struct sockaddr *serv_addr,
517 void (*prev_handler)(gint);
520 prev_handler = signal(SIGALRM, timeout_handler);
521 if (sigsetjmp(jmpenv, 1)) {
523 signal(SIGALRM, prev_handler);
530 ret = connect(sock, serv_addr, addrlen);
534 signal(SIGALRM, prev_handler);
540 struct hostent *my_gethostbyname(const gchar *hostname)
544 void (*prev_handler)(gint);
547 prev_handler = signal(SIGALRM, timeout_handler);
548 if (sigsetjmp(jmpenv, 1)) {
550 signal(SIGALRM, prev_handler);
551 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
558 if ((hp = gethostbyname(hostname)) == NULL) {
561 signal(SIGALRM, prev_handler);
563 fprintf(stderr, "%s: unknown host.\n", hostname);
570 signal(SIGALRM, prev_handler);
577 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
580 return inet_aton(hostname, inp);
585 inaddr = inet_addr(hostname);
587 memcpy(inp, &inaddr, sizeof(inaddr));
594 #endif /* HAVE_INET_ATON */
597 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
601 struct sockaddr_in ad;
603 memset(&ad, 0, sizeof(ad));
604 ad.sin_family = AF_INET;
605 ad.sin_port = htons(port);
609 if (!my_inet_aton(hostname, &ad.sin_addr)) {
610 if ((hp = my_gethostbyname(hostname)) == NULL) {
611 fprintf(stderr, "%s: unknown host.\n", hostname);
616 if (hp->h_length != 4 && hp->h_length != 8) {
617 fprintf(stderr, "illegal address length received for host %s\n", hostname);
622 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
625 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
626 sizeof(ad), io_timeout);
630 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
632 gint sock = -1, gai_error;
633 struct addrinfo hints, *res, *ai;
638 memset(&hints, 0, sizeof(hints));
639 /* hints.ai_flags = AI_CANONNAME; */
640 hints.ai_family = AF_UNSPEC;
641 hints.ai_socktype = SOCK_STREAM;
642 hints.ai_protocol = IPPROTO_TCP;
644 /* convert port from integer to string. */
645 g_snprintf(port_str, sizeof(port_str), "%d", port);
647 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
648 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
649 hostname, port_str, gai_strerror(gai_error));
653 for (ai = res; ai != NULL; ai = ai->ai_next) {
654 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
658 if (sock_connect_with_timeout
659 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
676 /* Open a connection using an external program. May be useful when
677 * you need to tunnel through a SOCKS or other firewall, or to
678 * establish an IMAP-over-SSH connection. */
679 /* TODO: Recreate this for sock_connect_thread() */
680 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
686 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
687 perror("socketpair");
690 log_message(LOG_PROTOCOL, "launching tunnel command \"%s\"\n", tunnelcmd);
695 dup(fd[1]); /* set onto stdin */
697 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
701 return sockinfo_from_fd(hostname, 0, fd[0]);
703 /* We would need a special implementation for W32. */
709 SockInfo *sock_connect(const gchar *hostname, gushort port)
718 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
722 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
723 g_warning("socket() failed: %d\n", WSAGetLastError());
725 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
727 #endif /* G_OS_WIN32 */
731 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
732 if (errno != 0) perror("connect");
738 return sockinfo_from_fd(hostname, port, sock);
742 static void sock_address_list_free(GList *addr_list)
746 for (cur = addr_list; cur != NULL; cur = cur->next) {
747 SockAddrData *addr_data = (SockAddrData *)cur->data;
748 g_free(addr_data->addr);
752 g_list_free(addr_list);
755 /* asynchronous TCP connection */
757 static gboolean sock_connect_async_cb(GIOChannel *source,
758 GIOCondition condition, gpointer data)
760 SockConnectData *conn_data = (SockConnectData *)data;
766 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
769 fd = g_io_channel_unix_get_fd(source);
771 conn_data->io_tag = 0;
772 conn_data->channel = NULL;
773 g_io_channel_unref(source);
776 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
777 perror("getsockopt");
779 sock_connect_address_list_async(conn_data);
785 sock_connect_address_list_async(conn_data);
789 sockinfo = g_new0(SockInfo, 1);
791 sockinfo->sock_ch = g_io_channel_unix_new(fd);
792 sockinfo->hostname = g_strdup(conn_data->hostname);
793 sockinfo->port = conn_data->port;
794 sockinfo->state = CONN_ESTABLISHED;
795 sockinfo->canonical_name = g_strdup(conn_data->canonical_name);
797 conn_data->func(sockinfo, conn_data->data);
799 sock_connect_async_cancel(conn_data->id);
804 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
807 SockConnectData *conn_data = (SockConnectData *)data;
809 conn_data->addr_list = addr_list;
810 conn_data->cur_addr = addr_list;
811 conn_data->canonical_name = conn_data->lookup_data->canonical_name;
812 conn_data->lookup_data->canonical_name = NULL;
813 conn_data->lookup_data = NULL;
815 return sock_connect_address_list_async(conn_data);
818 gint sock_connect_async(const gchar *hostname, gushort port,
819 SockConnectFunc func, gpointer data)
822 SockConnectData *conn_data;
824 conn_data = g_new0(SockConnectData, 1);
825 conn_data->id = id++;
826 conn_data->hostname = g_strdup(hostname);
827 conn_data->port = port;
828 conn_data->addr_list = NULL;
829 conn_data->cur_addr = NULL;
830 conn_data->io_tag = 0;
831 conn_data->func = func;
832 conn_data->data = data;
834 conn_data->lookup_data = sock_get_address_info_async
835 (hostname, port, sock_connect_async_get_address_info_cb,
838 if (conn_data->lookup_data == NULL) {
839 g_free(conn_data->hostname);
844 sock_connect_data_list = g_list_append(sock_connect_data_list,
847 return conn_data->id;
850 gint sock_connect_async_cancel(gint id)
852 SockConnectData *conn_data = NULL;
855 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
856 if (((SockConnectData *)cur->data)->id == id) {
857 conn_data = (SockConnectData *)cur->data;
863 sock_connect_data_list = g_list_remove(sock_connect_data_list,
866 if (conn_data->lookup_data)
867 sock_get_address_info_async_cancel
868 (conn_data->lookup_data);
870 if (conn_data->io_tag > 0)
871 g_source_remove(conn_data->io_tag);
872 if (conn_data->channel) {
873 g_io_channel_close(conn_data->channel);
874 g_io_channel_unref(conn_data->channel);
877 sock_address_list_free(conn_data->addr_list);
878 g_free(conn_data->canonical_name);
879 g_free(conn_data->hostname);
882 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
889 static gint sock_connect_address_list_async(SockConnectData *conn_data)
891 SockAddrData *addr_data;
894 for (; conn_data->cur_addr != NULL;
895 conn_data->cur_addr = conn_data->cur_addr->next) {
896 addr_data = (SockAddrData *)conn_data->cur_addr->data;
898 if ((sock = socket(addr_data->family, addr_data->socktype,
899 addr_data->protocol)) < 0) {
904 set_nonblocking_mode(sock, TRUE);
906 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
907 if (EINPROGRESS == errno) {
917 if (conn_data->cur_addr == NULL) {
918 g_warning("sock_connect_address_list_async: "
919 "connection to %s:%d failed\n",
920 conn_data->hostname, conn_data->port);
921 conn_data->func(NULL, conn_data->data);
922 sock_connect_async_cancel(conn_data->id);
926 conn_data->cur_addr = conn_data->cur_addr->next;
928 conn_data->channel = g_io_channel_unix_new(sock);
929 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
930 sock_connect_async_cb, conn_data);
935 /* asynchronous DNS lookup */
937 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
938 GIOCondition condition,
941 SockLookupData *lookup_data = (SockLookupData *)data;
942 GList *addr_list = NULL;
943 SockAddrData *addr_data;
946 struct sockaddr *addr;
947 gchar *canonical_name = NULL;
950 if (g_io_channel_read(source, &len, sizeof(len),
951 &bytes_read) == G_IO_ERROR_NONE) {
952 if (bytes_read == sizeof(len) && len > 0) {
955 canonical_name = g_malloc0(len + 1);
956 cur = canonical_name;
958 if (g_io_channel_read(source, cur, todo,
959 &bytes_read) != G_IO_ERROR_NONE) {
960 g_warning("canonical name not read\n");
961 g_free(canonical_name);
962 canonical_name = NULL;
968 if (bytes_read == 0) {
969 g_warning("canonical name not read\n");
970 g_free(canonical_name);
971 canonical_name = NULL;
978 if (g_io_channel_read(source, (gchar *)ai_member,
979 sizeof(ai_member), &bytes_read)
980 != G_IO_ERROR_NONE) {
981 g_warning("sock_get_address_info_async_cb: "
982 "address length read error\n");
986 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
989 if (ai_member[0] == AF_UNSPEC) {
990 g_warning("DNS lookup failed\n");
994 addr = g_malloc(ai_member[3]);
995 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
997 != G_IO_ERROR_NONE) {
998 g_warning("sock_get_address_info_async_cb: "
999 "address data read error\n");
1004 if (bytes_read != ai_member[3]) {
1005 g_warning("sock_get_address_info_async_cb: "
1006 "incomplete address data\n");
1011 addr_data = g_new0(SockAddrData, 1);
1012 addr_data->family = ai_member[0];
1013 addr_data->socktype = ai_member[1];
1014 addr_data->protocol = ai_member[2];
1015 addr_data->addr_len = ai_member[3];
1016 addr_data->addr = addr;
1018 addr_list = g_list_append(addr_list, addr_data);
1021 g_io_channel_close(source);
1022 g_io_channel_unref(source);
1025 /* FIXME: We would need to cancel the thread. */
1027 kill(lookup_data->child_pid, SIGKILL);
1028 waitpid(lookup_data->child_pid, NULL, 0);
1030 lookup_data->canonical_name = canonical_name;
1032 lookup_data->func(addr_list, lookup_data->data);
1034 g_free(lookup_data->canonical_name);
1035 g_free(lookup_data->hostname);
1036 g_free(lookup_data);
1042 /* For better readability we use a separate function to implement the
1043 child code of sock_get_address_info_async. Note, that under W32
1044 this is actually not a child but a thread and this is the reason
1045 why we pass only a void pointer. */
1046 static void address_info_async_child(void *opaque)
1048 SockLookupData *parm = opaque;
1051 struct addrinfo hints, *res, *ai;
1055 gchar **addr_list_p;
1056 struct sockaddr_in ad;
1058 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
1061 close(parm->pipe_fds[0]);
1062 parm->pipe_fds[0] = -1;
1066 memset(&hints, 0, sizeof(hints));
1067 hints.ai_flags = AI_CANONNAME;
1068 hints.ai_family = AF_UNSPEC;
1069 hints.ai_socktype = SOCK_STREAM;
1070 hints.ai_protocol = IPPROTO_TCP;
1072 g_snprintf(port_str, sizeof(port_str), "%d", parm->port);
1074 gai_err = getaddrinfo(parm->hostname, port_str, &hints, &res);
1077 g_warning("getaddrinfo for %s:%s failed: %s\n",
1078 parm->hostname, port_str, gai_strerror(gai_err));
1079 fd_write_all(parm->pipe_fds[1], &len,
1081 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1083 close(parm->pipe_fds[1]);
1084 parm->pipe_fds[1] = -1;
1093 if (res->ai_canonname && strlen(res->ai_canonname) < 255) {
1094 gchar len = strlen(res->ai_canonname);
1095 fd_write_all(parm->pipe_fds[1], &len,
1097 fd_write_all(parm->pipe_fds[1], res->ai_canonname,
1101 fd_write_all(parm->pipe_fds[1], &len,
1106 fd_write_all(parm->pipe_fds[1], &len,
1110 for (ai = res; ai != NULL; ai = ai->ai_next) {
1111 ai_member[0] = ai->ai_family;
1112 ai_member[1] = ai->ai_socktype;
1113 ai_member[2] = ai->ai_protocol;
1114 ai_member[3] = ai->ai_addrlen;
1116 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1118 fd_write_all(parm->pipe_fds[1], (gchar *)ai->ai_addr,
1125 hp = my_gethostbyname(parm->hostname);
1126 if (hp == NULL || hp->h_addrtype != AF_INET) {
1128 fd_write_all(parm->pipe_fds[1], &len,
1130 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1132 close(parm->pipe_fds[1]);
1133 parm->pipe_fds[1] = -1;
1141 ai_member[0] = AF_INET;
1142 ai_member[1] = SOCK_STREAM;
1143 ai_member[2] = IPPROTO_TCP;
1144 ai_member[3] = sizeof(ad);
1146 memset(&ad, 0, sizeof(ad));
1147 ad.sin_family = AF_INET;
1148 ad.sin_port = htons(parm->port);
1150 if (hp->h_name && strlen(hp->h_name) < 255) {
1151 gchar len = strlen(hp->h_name);
1152 fd_write_all(parm->pipe_fds[1], &len,
1154 fd_write_all(parm->pipe_fds[1], hp->h_name,
1158 fd_write_all(parm->pipe_fds[1], &len,
1161 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
1163 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
1164 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1166 fd_write_all(parm->pipe_fds[1], (gchar *)&ad, sizeof(ad));
1170 close(parm->pipe_fds[1]);
1171 parm->pipe_fds[1] = -1;
1180 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
1185 SockLookupData *lookup_data = NULL;
1187 refresh_resolvers();
1189 lookup_data = g_new0(SockLookupData, 1);
1190 lookup_data->hostname = g_strdup(hostname);
1191 lookup_data->func = func;
1192 lookup_data->data = data;
1193 lookup_data->port = port;
1194 lookup_data->child_pid = (pid_t)(-1);
1195 lookup_data->pipe_fds[0] = -1;
1196 lookup_data->pipe_fds[1] = -1;
1198 if (pipe(lookup_data->pipe_fds) < 0) {
1201 g_free (lookup_data->hostname);
1202 g_free (lookup_data);
1207 if ((lookup_data->child_pid = fork()) < 0) {
1210 g_free (lookup_data->hostname);
1211 g_free (lookup_data);
1215 if (lookup_data->child_pid == 0) {
1216 /* Child process. */
1217 address_info_async_child (lookup_data);
1218 g_assert_not_reached ();
1220 /* Parent process. */
1221 close(lookup_data->pipe_fds[1]);
1222 lookup_data->pipe_fds[1] = -1;
1223 #endif /*!G_OS_WIN32 */
1225 lookup_data->channel = g_io_channel_unix_new(lookup_data->pipe_fds[0]);
1226 lookup_data->io_tag = g_io_add_watch(lookup_data->channel, G_IO_IN,
1227 sock_get_address_info_async_cb,
1230 lookup_data->child_pid = _beginthread(
1231 address_info_async_child, 0, lookup_data);
1237 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1239 if (lookup_data->io_tag > 0)
1240 g_source_remove(lookup_data->io_tag);
1241 if (lookup_data->channel) {
1242 g_io_channel_close(lookup_data->channel);
1243 g_io_channel_unref(lookup_data->channel);
1246 if (lookup_data->child_pid > 0) {
1248 /* FIXME: Need a way to cancel the thread. */
1250 kill(lookup_data->child_pid, SIGKILL);
1251 waitpid(lookup_data->child_pid, NULL, 0);
1255 g_free(lookup_data->canonical_name);
1256 g_free(lookup_data->hostname);
1257 g_free(lookup_data);
1263 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1269 sockinfo = g_new0(SockInfo, 1);
1270 sockinfo->sock = sock;
1271 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1272 sockinfo->hostname = g_strdup(hostname);
1273 sockinfo->port = port;
1274 sockinfo->state = CONN_ESTABLISHED;
1279 static gint fd_read(gint fd, gchar *buf, gint len)
1281 if (fd_check_io(fd, G_IO_IN) < 0)
1284 if (fd_is_w32_socket(fd))
1285 return recv(fd, buf, len, 0);
1286 return read(fd, buf, len);
1290 static gint ssl_read(SSL *ssl, gchar *buf, gint len)
1294 if (SSL_pending(ssl) == 0) {
1295 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1299 ret = SSL_read(ssl, buf, len);
1301 switch ((err = SSL_get_error(ssl, ret))) {
1302 case SSL_ERROR_NONE:
1304 case SSL_ERROR_WANT_READ:
1305 case SSL_ERROR_WANT_WRITE:
1308 case SSL_ERROR_ZERO_RETURN:
1311 g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
1319 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1323 g_return_val_if_fail(sock != NULL, -1);
1327 ret = ssl_read(sock->ssl, buf, len);
1330 ret = fd_read(sock->sock, buf, len);
1333 sock->state = CONN_DISCONNECTED;
1337 gint fd_write(gint fd, const gchar *buf, gint len)
1339 if (fd_check_io(fd, G_IO_OUT) < 0)
1342 if (fd_is_w32_socket (fd))
1343 return send(fd, buf, len, 0);
1344 return write(fd, buf, len);
1348 static gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1352 ret = SSL_write(ssl, buf, len);
1354 switch (SSL_get_error(ssl, ret)) {
1355 case SSL_ERROR_NONE:
1357 case SSL_ERROR_WANT_READ:
1358 case SSL_ERROR_WANT_WRITE:
1367 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1371 g_return_val_if_fail(sock != NULL, -1);
1375 ret = ssl_write(sock->ssl, buf, len);
1378 ret = fd_write(sock->sock, buf, len);
1381 sock->state = CONN_DISCONNECTED;
1385 gint fd_write_all(gint fd, const gchar *buf, gint len)
1390 if (fd_check_io(fd, G_IO_OUT) < 0)
1393 signal(SIGPIPE, SIG_IGN);
1395 if (fd_is_w32_socket(fd))
1396 n = send(fd, buf, len, 0);
1398 n = write(fd, buf, len);
1401 log_error(LOG_PROTOCOL, _("write on fd%d: %s\n"), fd, strerror(errno));
1413 static gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1418 n = ssl_write(ssl, buf, len);
1430 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1434 g_return_val_if_fail(sock != NULL, -1);
1438 ret = ssl_write_all(sock->ssl, buf, len);
1441 ret = fd_write_all(sock->sock, buf, len);
1444 sock->state = CONN_DISCONNECTED;
1448 static gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1450 if (fd_check_io(fd, G_IO_IN) < 0)
1453 return recv(fd, buf, len, flags);
1456 gint fd_gets(gint fd, gchar *buf, gint len)
1458 gchar *newline, *bp = buf;
1468 MSKB Article ID: Q147714
1469 Windows Sockets 2 Service Provider Interface Limitations
1470 Polling with recv(MSG_PEEK) to determine when a complete message
1472 Reason and Workaround not available.
1474 Single-byte send() and recv().
1475 Reason: Couple one-byte sends with Nagle disabled.
1476 Workaround: Send modest amounts and receive as much as possible.
1479 if (recv(fd, bp, 1, 0) <= 0)
1486 #else /*!G_OS_WIN32*/
1488 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1490 if ((newline = memchr(bp, '\n', n)) != NULL)
1491 n = newline - bp + 1;
1492 if ((n = fd_read(fd, bp, n)) < 0)
1496 } while (!newline && len);
1497 #endif /*!G_OS_WIN32*/
1504 static gint ssl_peek (SSL *ssl, gchar *buf, gint len);
1506 static gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1508 gchar *newline, *bp = buf;
1514 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1516 if ((newline = memchr(bp, '\n', n)) != NULL)
1517 n = newline - bp + 1;
1518 if ((n = ssl_read(ssl, bp, n)) < 0)
1522 } while (!newline && len);
1529 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1533 g_return_val_if_fail(sock != NULL, -1);
1537 return ssl_gets(sock->ssl, buf, len);
1540 return fd_gets(sock->sock, buf, len);
1543 sock->state = CONN_DISCONNECTED;
1547 /* peek at the socket data without actually reading it */
1549 static gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1553 if (SSL_pending(ssl) == 0) {
1554 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1558 ret = SSL_peek(ssl, buf, len);
1560 switch ((err = SSL_get_error(ssl, ret))) {
1561 case SSL_ERROR_NONE:
1563 case SSL_ERROR_WANT_READ:
1564 case SSL_ERROR_WANT_WRITE:
1567 case SSL_ERROR_ZERO_RETURN:
1569 case SSL_ERROR_SYSCALL:
1570 g_warning("SSL_peek() returned syscall error. errno=%d\n", errno);
1573 g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
1581 gint sock_close(SockInfo *sock)
1589 g_io_channel_unref(sock->sock_ch);
1593 ssl_done_socket(sock);
1594 if (sock->g_source != 0)
1595 g_source_remove(sock->g_source);
1599 shutdown(sock->sock, 1); /* complete transfer before close */
1600 ret = closesocket(sock->sock);
1602 ret = fd_close(sock->sock);
1605 g_free(sock->canonical_name);
1606 g_free(sock->hostname);
1612 gint fd_close(gint fd)