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) {
302 if (listen(sock, 1) < 0) {
314 gint fd_accept(gint sock)
316 struct sockaddr_in caddr;
319 caddr_len = sizeof(caddr);
320 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
324 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
329 flags = fcntl(fd, F_GETFL, 0);
338 flags &= ~O_NONBLOCK;
340 return fcntl(fd, F_SETFL, flags);
346 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
348 g_return_val_if_fail(sock != NULL, -1);
350 return set_nonblocking_mode(sock->sock, nonblock);
353 static gboolean is_nonblocking_mode(gint fd)
358 flags = fcntl(fd, F_GETFL, 0);
364 return ((flags & O_NONBLOCK) != 0);
370 gboolean sock_is_nonblocking_mode(SockInfo *sock)
372 g_return_val_if_fail(sock != NULL, FALSE);
374 return is_nonblocking_mode(sock->sock);
378 static gboolean sock_prepare(GSource *source, gint *timeout)
384 static gboolean sock_check(GSource *source)
386 SockInfo *sock = ((SockSource *)source)->sock;
387 struct timeval timeout = {0, 0};
389 GIOCondition condition = sock->condition;
391 if (!sock || !sock->sock)
396 if (condition & G_IO_IN) {
397 if (SSL_pending(sock->ssl) > 0)
399 if (SSL_want_write(sock->ssl))
400 condition |= G_IO_OUT;
403 if (condition & G_IO_OUT) {
404 if (SSL_want_read(sock->ssl))
405 condition |= G_IO_IN;
411 FD_SET(sock->sock, &fds);
413 select(sock->sock + 1,
414 (condition & G_IO_IN) ? &fds : NULL,
415 (condition & G_IO_OUT) ? &fds : NULL,
418 return FD_ISSET(sock->sock, &fds) != 0;
421 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
424 SockInfo *sock = ((SockSource *)source)->sock;
426 if (!sock || !sock->callback || !sock->data)
429 return sock->callback(sock, sock->condition, sock->data);
432 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
435 SockInfo *sock = (SockInfo *)data;
437 if ((condition & sock->condition) == 0)
440 return sock->callback(sock, sock->condition, sock->data);
443 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
446 sock->callback = func;
447 sock->condition = condition;
453 GSource *source = g_source_new(&sock_watch_funcs,
455 ((SockSource *) source)->sock = sock;
456 g_source_set_priority(source, G_PRIORITY_DEFAULT);
457 g_source_set_can_recurse(source, FALSE);
458 sock->g_source = g_source_attach(source, NULL);
459 g_source_unref (source); /* Refcount back down to 1 */
460 return sock->g_source;
464 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
467 static gint fd_check_io(gint fd, GIOCondition cond)
469 struct timeval timeout;
472 if (is_nonblocking_mode(fd))
475 timeout.tv_sec = io_timeout;
481 if (cond == G_IO_IN) {
482 select(fd + 1, &fds, NULL, NULL,
483 io_timeout > 0 ? &timeout : NULL);
485 select(fd + 1, NULL, &fds, NULL,
486 io_timeout > 0 ? &timeout : NULL);
489 if (FD_ISSET(fd, &fds)) {
492 g_warning("Socket IO timeout\n");
498 static sigjmp_buf jmpenv;
500 static void timeout_handler(gint sig)
502 siglongjmp(jmpenv, 1);
506 static gint sock_connect_with_timeout(gint sock,
507 const struct sockaddr *serv_addr,
513 void (*prev_handler)(gint);
516 prev_handler = signal(SIGALRM, timeout_handler);
517 if (sigsetjmp(jmpenv, 1)) {
519 signal(SIGALRM, prev_handler);
526 ret = connect(sock, serv_addr, addrlen);
530 signal(SIGALRM, prev_handler);
536 struct hostent *my_gethostbyname(const gchar *hostname)
540 void (*prev_handler)(gint);
543 prev_handler = signal(SIGALRM, timeout_handler);
544 if (sigsetjmp(jmpenv, 1)) {
546 signal(SIGALRM, prev_handler);
547 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
554 if ((hp = gethostbyname(hostname)) == NULL) {
557 signal(SIGALRM, prev_handler);
559 fprintf(stderr, "%s: unknown host.\n", hostname);
566 signal(SIGALRM, prev_handler);
573 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
576 return inet_aton(hostname, inp);
581 inaddr = inet_addr(hostname);
583 memcpy(inp, &inaddr, sizeof(inaddr));
590 #endif /* HAVE_INET_ATON */
593 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
597 struct sockaddr_in ad;
599 memset(&ad, 0, sizeof(ad));
600 ad.sin_family = AF_INET;
601 ad.sin_port = htons(port);
605 if (!my_inet_aton(hostname, &ad.sin_addr)) {
606 if ((hp = my_gethostbyname(hostname)) == NULL) {
607 fprintf(stderr, "%s: unknown host.\n", hostname);
612 if (hp->h_length != 4 && hp->h_length != 8) {
613 fprintf(stderr, "illegal address length received for host %s\n", hostname);
618 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
621 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
622 sizeof(ad), io_timeout);
626 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
628 gint sock = -1, gai_error;
629 struct addrinfo hints, *res, *ai;
634 memset(&hints, 0, sizeof(hints));
635 /* hints.ai_flags = AI_CANONNAME; */
636 hints.ai_family = AF_UNSPEC;
637 hints.ai_socktype = SOCK_STREAM;
638 hints.ai_protocol = IPPROTO_TCP;
640 /* convert port from integer to string. */
641 g_snprintf(port_str, sizeof(port_str), "%d", port);
643 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
644 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
645 hostname, port_str, gai_strerror(gai_error));
649 for (ai = res; ai != NULL; ai = ai->ai_next) {
650 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
654 if (sock_connect_with_timeout
655 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
672 /* Open a connection using an external program. May be useful when
673 * you need to tunnel through a SOCKS or other firewall, or to
674 * establish an IMAP-over-SSH connection. */
675 /* TODO: Recreate this for sock_connect_thread() */
676 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
682 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
683 perror("socketpair");
686 log_message(LOG_PROTOCOL, "launching tunnel command \"%s\"\n", tunnelcmd);
691 dup(fd[1]); /* set onto stdin */
693 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
697 return sockinfo_from_fd(hostname, 0, fd[0]);
699 /* We would need a special implementation for W32. */
705 SockInfo *sock_connect(const gchar *hostname, gushort port)
714 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
718 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
719 g_warning("socket() failed: %d\n", WSAGetLastError());
721 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
723 #endif /* G_OS_WIN32 */
727 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
728 if (errno != 0) perror("connect");
734 return sockinfo_from_fd(hostname, port, sock);
738 static void sock_address_list_free(GList *addr_list)
742 for (cur = addr_list; cur != NULL; cur = cur->next) {
743 SockAddrData *addr_data = (SockAddrData *)cur->data;
744 g_free(addr_data->addr);
748 g_list_free(addr_list);
751 /* asynchronous TCP connection */
753 static gboolean sock_connect_async_cb(GIOChannel *source,
754 GIOCondition condition, gpointer data)
756 SockConnectData *conn_data = (SockConnectData *)data;
762 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
765 fd = g_io_channel_unix_get_fd(source);
767 conn_data->io_tag = 0;
768 conn_data->channel = NULL;
769 g_io_channel_unref(source);
772 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
773 perror("getsockopt");
775 sock_connect_address_list_async(conn_data);
781 sock_connect_address_list_async(conn_data);
785 sockinfo = g_new0(SockInfo, 1);
787 sockinfo->sock_ch = g_io_channel_unix_new(fd);
788 sockinfo->hostname = g_strdup(conn_data->hostname);
789 sockinfo->port = conn_data->port;
790 sockinfo->state = CONN_ESTABLISHED;
791 sockinfo->canonical_name = g_strdup(conn_data->canonical_name);
793 conn_data->func(sockinfo, conn_data->data);
795 sock_connect_async_cancel(conn_data->id);
800 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
803 SockConnectData *conn_data = (SockConnectData *)data;
805 conn_data->addr_list = addr_list;
806 conn_data->cur_addr = addr_list;
807 conn_data->canonical_name = conn_data->lookup_data->canonical_name;
808 conn_data->lookup_data->canonical_name = NULL;
809 conn_data->lookup_data = NULL;
811 return sock_connect_address_list_async(conn_data);
814 gint sock_connect_async(const gchar *hostname, gushort port,
815 SockConnectFunc func, gpointer data)
818 SockConnectData *conn_data;
820 conn_data = g_new0(SockConnectData, 1);
821 conn_data->id = id++;
822 conn_data->hostname = g_strdup(hostname);
823 conn_data->port = port;
824 conn_data->addr_list = NULL;
825 conn_data->cur_addr = NULL;
826 conn_data->io_tag = 0;
827 conn_data->func = func;
828 conn_data->data = data;
830 conn_data->lookup_data = sock_get_address_info_async
831 (hostname, port, sock_connect_async_get_address_info_cb,
834 if (conn_data->lookup_data == NULL) {
835 g_free(conn_data->hostname);
840 sock_connect_data_list = g_list_append(sock_connect_data_list,
843 return conn_data->id;
846 gint sock_connect_async_cancel(gint id)
848 SockConnectData *conn_data = NULL;
851 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
852 if (((SockConnectData *)cur->data)->id == id) {
853 conn_data = (SockConnectData *)cur->data;
859 sock_connect_data_list = g_list_remove(sock_connect_data_list,
862 if (conn_data->lookup_data)
863 sock_get_address_info_async_cancel
864 (conn_data->lookup_data);
866 if (conn_data->io_tag > 0)
867 g_source_remove(conn_data->io_tag);
868 if (conn_data->channel) {
869 g_io_channel_close(conn_data->channel);
870 g_io_channel_unref(conn_data->channel);
873 sock_address_list_free(conn_data->addr_list);
874 g_free(conn_data->canonical_name);
875 g_free(conn_data->hostname);
878 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
885 static gint sock_connect_address_list_async(SockConnectData *conn_data)
887 SockAddrData *addr_data;
890 for (; conn_data->cur_addr != NULL;
891 conn_data->cur_addr = conn_data->cur_addr->next) {
892 addr_data = (SockAddrData *)conn_data->cur_addr->data;
894 if ((sock = socket(addr_data->family, addr_data->socktype,
895 addr_data->protocol)) < 0) {
900 set_nonblocking_mode(sock, TRUE);
902 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
903 if (EINPROGRESS == errno) {
913 if (conn_data->cur_addr == NULL) {
914 g_warning("sock_connect_address_list_async: "
915 "connection to %s:%d failed\n",
916 conn_data->hostname, conn_data->port);
917 conn_data->func(NULL, conn_data->data);
918 sock_connect_async_cancel(conn_data->id);
922 conn_data->cur_addr = conn_data->cur_addr->next;
924 conn_data->channel = g_io_channel_unix_new(sock);
925 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
926 sock_connect_async_cb, conn_data);
931 /* asynchronous DNS lookup */
933 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
934 GIOCondition condition,
937 SockLookupData *lookup_data = (SockLookupData *)data;
938 GList *addr_list = NULL;
939 SockAddrData *addr_data;
942 struct sockaddr *addr;
943 gchar *canonical_name = NULL;
946 if (g_io_channel_read(source, &len, sizeof(len),
947 &bytes_read) == G_IO_ERROR_NONE) {
948 if (bytes_read == sizeof(len) && len > 0) {
951 canonical_name = g_malloc0(len + 1);
952 cur = canonical_name;
954 if (g_io_channel_read(source, cur, todo,
955 &bytes_read) != G_IO_ERROR_NONE) {
956 g_warning("canonical name not read\n");
957 g_free(canonical_name);
958 canonical_name = NULL;
964 if (bytes_read == 0) {
965 g_warning("canonical name not read\n");
966 g_free(canonical_name);
967 canonical_name = NULL;
974 if (g_io_channel_read(source, (gchar *)ai_member,
975 sizeof(ai_member), &bytes_read)
976 != G_IO_ERROR_NONE) {
977 g_warning("sock_get_address_info_async_cb: "
978 "address length read error\n");
982 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
985 if (ai_member[0] == AF_UNSPEC) {
986 g_warning("DNS lookup failed\n");
990 addr = g_malloc(ai_member[3]);
991 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
993 != G_IO_ERROR_NONE) {
994 g_warning("sock_get_address_info_async_cb: "
995 "address data read error\n");
1000 if (bytes_read != ai_member[3]) {
1001 g_warning("sock_get_address_info_async_cb: "
1002 "incomplete address data\n");
1007 addr_data = g_new0(SockAddrData, 1);
1008 addr_data->family = ai_member[0];
1009 addr_data->socktype = ai_member[1];
1010 addr_data->protocol = ai_member[2];
1011 addr_data->addr_len = ai_member[3];
1012 addr_data->addr = addr;
1014 addr_list = g_list_append(addr_list, addr_data);
1017 g_io_channel_close(source);
1018 g_io_channel_unref(source);
1021 /* FIXME: We would need to cancel the thread. */
1023 kill(lookup_data->child_pid, SIGKILL);
1024 waitpid(lookup_data->child_pid, NULL, 0);
1026 lookup_data->canonical_name = canonical_name;
1028 lookup_data->func(addr_list, lookup_data->data);
1030 g_free(lookup_data->canonical_name);
1031 g_free(lookup_data->hostname);
1032 g_free(lookup_data);
1038 /* For better readability we use a separate function to implement the
1039 child code of sock_get_address_info_async. Note, that under W32
1040 this is actually not a child but a thread and this is the reason
1041 why we pass only a void pointer. */
1042 static void address_info_async_child(void *opaque)
1044 SockLookupData *parm = opaque;
1047 struct addrinfo hints, *res, *ai;
1051 gchar **addr_list_p;
1052 struct sockaddr_in ad;
1054 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
1057 close(parm->pipe_fds[0]);
1058 parm->pipe_fds[0] = -1;
1062 memset(&hints, 0, sizeof(hints));
1063 hints.ai_flags = AI_CANONNAME;
1064 hints.ai_family = AF_UNSPEC;
1065 hints.ai_socktype = SOCK_STREAM;
1066 hints.ai_protocol = IPPROTO_TCP;
1068 g_snprintf(port_str, sizeof(port_str), "%d", parm->port);
1070 gai_err = getaddrinfo(parm->hostname, port_str, &hints, &res);
1073 g_warning("getaddrinfo for %s:%s failed: %s\n",
1074 parm->hostname, port_str, gai_strerror(gai_err));
1075 fd_write_all(parm->pipe_fds[1], &len,
1077 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1079 close(parm->pipe_fds[1]);
1080 parm->pipe_fds[1] = -1;
1089 if (res->ai_canonname && strlen(res->ai_canonname) < 255) {
1090 gchar len = strlen(res->ai_canonname);
1091 fd_write_all(parm->pipe_fds[1], &len,
1093 fd_write_all(parm->pipe_fds[1], res->ai_canonname,
1097 fd_write_all(parm->pipe_fds[1], &len,
1102 fd_write_all(parm->pipe_fds[1], &len,
1106 for (ai = res; ai != NULL; ai = ai->ai_next) {
1107 ai_member[0] = ai->ai_family;
1108 ai_member[1] = ai->ai_socktype;
1109 ai_member[2] = ai->ai_protocol;
1110 ai_member[3] = ai->ai_addrlen;
1112 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1114 fd_write_all(parm->pipe_fds[1], (gchar *)ai->ai_addr,
1121 hp = my_gethostbyname(parm->hostname);
1122 if (hp == NULL || hp->h_addrtype != AF_INET) {
1124 fd_write_all(parm->pipe_fds[1], &len,
1126 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1128 close(parm->pipe_fds[1]);
1129 parm->pipe_fds[1] = -1;
1137 ai_member[0] = AF_INET;
1138 ai_member[1] = SOCK_STREAM;
1139 ai_member[2] = IPPROTO_TCP;
1140 ai_member[3] = sizeof(ad);
1142 memset(&ad, 0, sizeof(ad));
1143 ad.sin_family = AF_INET;
1144 ad.sin_port = htons(parm->port);
1146 if (hp->h_name && strlen(hp->h_name) < 255) {
1147 gchar len = strlen(hp->h_name);
1148 fd_write_all(parm->pipe_fds[1], &len,
1150 fd_write_all(parm->pipe_fds[1], hp->h_name,
1154 fd_write_all(parm->pipe_fds[1], &len,
1157 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
1159 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
1160 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1162 fd_write_all(parm->pipe_fds[1], (gchar *)&ad, sizeof(ad));
1166 close(parm->pipe_fds[1]);
1167 parm->pipe_fds[1] = -1;
1176 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
1181 SockLookupData *lookup_data = NULL;
1183 refresh_resolvers();
1185 lookup_data = g_new0(SockLookupData, 1);
1186 lookup_data->hostname = g_strdup(hostname);
1187 lookup_data->func = func;
1188 lookup_data->data = data;
1189 lookup_data->port = port;
1190 lookup_data->child_pid = (pid_t)(-1);
1191 lookup_data->pipe_fds[0] = -1;
1192 lookup_data->pipe_fds[1] = -1;
1194 if (pipe(lookup_data->pipe_fds) < 0) {
1197 g_free (lookup_data->hostname);
1198 g_free (lookup_data);
1203 if ((lookup_data->child_pid = fork()) < 0) {
1206 g_free (lookup_data->hostname);
1207 g_free (lookup_data);
1211 if (lookup_data->child_pid == 0) {
1212 /* Child process. */
1213 address_info_async_child (lookup_data);
1214 g_assert_not_reached ();
1216 /* Parent process. */
1217 close(lookup_data->pipe_fds[1]);
1218 lookup_data->pipe_fds[1] = -1;
1219 #endif /*!G_OS_WIN32 */
1221 lookup_data->channel = g_io_channel_unix_new(lookup_data->pipe_fds[0]);
1222 lookup_data->io_tag = g_io_add_watch(lookup_data->channel, G_IO_IN,
1223 sock_get_address_info_async_cb,
1226 lookup_data->child_pid = _beginthread(
1227 address_info_async_child, 0, lookup_data);
1233 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1235 if (lookup_data->io_tag > 0)
1236 g_source_remove(lookup_data->io_tag);
1237 if (lookup_data->channel) {
1238 g_io_channel_close(lookup_data->channel);
1239 g_io_channel_unref(lookup_data->channel);
1242 if (lookup_data->child_pid > 0) {
1244 /* FIXME: Need a way to cancel the thread. */
1246 kill(lookup_data->child_pid, SIGKILL);
1247 waitpid(lookup_data->child_pid, NULL, 0);
1251 g_free(lookup_data->canonical_name);
1252 g_free(lookup_data->hostname);
1253 g_free(lookup_data);
1259 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1265 sockinfo = g_new0(SockInfo, 1);
1266 sockinfo->sock = sock;
1267 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1268 sockinfo->hostname = g_strdup(hostname);
1269 sockinfo->port = port;
1270 sockinfo->state = CONN_ESTABLISHED;
1275 static gint fd_read(gint fd, gchar *buf, gint len)
1277 if (fd_check_io(fd, G_IO_IN) < 0)
1280 if (fd_is_w32_socket(fd))
1281 return recv(fd, buf, len, 0);
1282 return read(fd, buf, len);
1286 static gint ssl_read(SSL *ssl, gchar *buf, gint len)
1290 if (SSL_pending(ssl) == 0) {
1291 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1295 ret = SSL_read(ssl, buf, len);
1297 switch ((err = SSL_get_error(ssl, ret))) {
1298 case SSL_ERROR_NONE:
1300 case SSL_ERROR_WANT_READ:
1301 case SSL_ERROR_WANT_WRITE:
1304 case SSL_ERROR_ZERO_RETURN:
1307 g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
1315 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1319 g_return_val_if_fail(sock != NULL, -1);
1323 ret = ssl_read(sock->ssl, buf, len);
1326 ret = fd_read(sock->sock, buf, len);
1329 sock->state = CONN_DISCONNECTED;
1333 gint fd_write(gint fd, const gchar *buf, gint len)
1335 if (fd_check_io(fd, G_IO_OUT) < 0)
1338 if (fd_is_w32_socket (fd))
1339 return send(fd, buf, len, 0);
1340 return write(fd, buf, len);
1344 static gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1348 ret = SSL_write(ssl, buf, len);
1350 switch (SSL_get_error(ssl, ret)) {
1351 case SSL_ERROR_NONE:
1353 case SSL_ERROR_WANT_READ:
1354 case SSL_ERROR_WANT_WRITE:
1363 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1367 g_return_val_if_fail(sock != NULL, -1);
1371 ret = ssl_write(sock->ssl, buf, len);
1374 ret = fd_write(sock->sock, buf, len);
1377 sock->state = CONN_DISCONNECTED;
1381 gint fd_write_all(gint fd, const gchar *buf, gint len)
1386 if (fd_check_io(fd, G_IO_OUT) < 0)
1389 signal(SIGPIPE, SIG_IGN);
1391 if (fd_is_w32_socket(fd))
1392 n = send(fd, buf, len, 0);
1394 n = write(fd, buf, len);
1397 log_error(LOG_PROTOCOL, _("write on fd%d: %s\n"), fd, strerror(errno));
1409 static gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1414 n = ssl_write(ssl, buf, len);
1426 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1430 g_return_val_if_fail(sock != NULL, -1);
1434 ret = ssl_write_all(sock->ssl, buf, len);
1437 ret = fd_write_all(sock->sock, buf, len);
1440 sock->state = CONN_DISCONNECTED;
1444 static gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1446 if (fd_check_io(fd, G_IO_IN) < 0)
1449 return recv(fd, buf, len, flags);
1452 gint fd_gets(gint fd, gchar *buf, gint len)
1454 gchar *newline, *bp = buf;
1464 MSKB Article ID: Q147714
1465 Windows Sockets 2 Service Provider Interface Limitations
1466 Polling with recv(MSG_PEEK) to determine when a complete message
1468 Reason and Workaround not available.
1470 Single-byte send() and recv().
1471 Reason: Couple one-byte sends with Nagle disabled.
1472 Workaround: Send modest amounts and receive as much as possible.
1475 if (recv(fd, bp, 1, 0) <= 0)
1482 #else /*!G_OS_WIN32*/
1484 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1486 if ((newline = memchr(bp, '\n', n)) != NULL)
1487 n = newline - bp + 1;
1488 if ((n = fd_read(fd, bp, n)) < 0)
1492 } while (!newline && len);
1493 #endif /*!G_OS_WIN32*/
1500 static gint ssl_peek (SSL *ssl, gchar *buf, gint len);
1502 static gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1504 gchar *newline, *bp = buf;
1510 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1512 if ((newline = memchr(bp, '\n', n)) != NULL)
1513 n = newline - bp + 1;
1514 if ((n = ssl_read(ssl, bp, n)) < 0)
1518 } while (!newline && len);
1525 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1529 g_return_val_if_fail(sock != NULL, -1);
1533 return ssl_gets(sock->ssl, buf, len);
1536 return fd_gets(sock->sock, buf, len);
1539 sock->state = CONN_DISCONNECTED;
1543 /* peek at the socket data without actually reading it */
1545 static gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1549 if (SSL_pending(ssl) == 0) {
1550 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1554 ret = SSL_peek(ssl, buf, len);
1556 switch ((err = SSL_get_error(ssl, ret))) {
1557 case SSL_ERROR_NONE:
1559 case SSL_ERROR_WANT_READ:
1560 case SSL_ERROR_WANT_WRITE:
1563 case SSL_ERROR_ZERO_RETURN:
1565 case SSL_ERROR_SYSCALL:
1566 g_warning("SSL_peek() returned syscall error. errno=%d\n", errno);
1569 g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
1577 gint sock_close(SockInfo *sock)
1585 g_io_channel_unref(sock->sock_ch);
1589 ssl_done_socket(sock);
1590 if (sock->g_source != 0)
1591 g_source_remove(sock->g_source);
1595 shutdown(sock->sock, 1); /* complete transfer before close */
1596 ret = closesocket(sock->sock);
1598 ret = fd_close(sock->sock);
1601 g_free(sock->canonical_name);
1602 g_free(sock->hostname);
1608 gint fd_close(gint fd)