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,
453 sock->callback = func;
454 sock->condition = condition;
460 GSource *source = g_source_new(&sock_watch_funcs,
462 ((SockSource *) source)->sock = sock;
463 g_source_set_priority(source, G_PRIORITY_DEFAULT);
464 g_source_set_can_recurse(source, FALSE);
465 sock->g_source = g_source_attach(source, NULL);
466 g_source_unref (source); /* Refcount back down to 1 */
467 return sock->g_source;
471 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
474 static gint fd_check_io(gint fd, GIOCondition cond)
476 struct timeval timeout;
479 if (is_nonblocking_mode(fd))
482 timeout.tv_sec = io_timeout;
488 if (cond == G_IO_IN) {
489 select(fd + 1, &fds, NULL, NULL,
490 io_timeout > 0 ? &timeout : NULL);
492 select(fd + 1, NULL, &fds, NULL,
493 io_timeout > 0 ? &timeout : NULL);
496 if (FD_ISSET(fd, &fds)) {
499 g_warning("Socket IO timeout\n");
505 static sigjmp_buf jmpenv;
507 static void timeout_handler(gint sig)
509 siglongjmp(jmpenv, 1);
513 static gint sock_connect_with_timeout(gint sock,
514 const struct sockaddr *serv_addr,
520 void (*prev_handler)(gint);
523 prev_handler = signal(SIGALRM, timeout_handler);
524 if (sigsetjmp(jmpenv, 1)) {
526 signal(SIGALRM, prev_handler);
533 ret = connect(sock, serv_addr, addrlen);
537 signal(SIGALRM, prev_handler);
543 struct hostent *my_gethostbyname(const gchar *hostname)
547 void (*prev_handler)(gint);
550 prev_handler = signal(SIGALRM, timeout_handler);
551 if (sigsetjmp(jmpenv, 1)) {
553 signal(SIGALRM, prev_handler);
554 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
561 if ((hp = gethostbyname(hostname)) == NULL) {
564 signal(SIGALRM, prev_handler);
566 fprintf(stderr, "%s: unknown host.\n", hostname);
573 signal(SIGALRM, prev_handler);
580 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
583 return inet_aton(hostname, inp);
588 inaddr = inet_addr(hostname);
590 memcpy(inp, &inaddr, sizeof(inaddr));
597 #endif /* HAVE_INET_ATON */
600 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
604 struct sockaddr_in ad;
606 memset(&ad, 0, sizeof(ad));
607 ad.sin_family = AF_INET;
608 ad.sin_port = htons(port);
612 if (!my_inet_aton(hostname, &ad.sin_addr)) {
613 if ((hp = my_gethostbyname(hostname)) == NULL) {
614 fprintf(stderr, "%s: unknown host.\n", hostname);
619 if (hp->h_length != 4 && hp->h_length != 8) {
620 fprintf(stderr, "illegal address length received for host %s\n", hostname);
625 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
628 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
629 sizeof(ad), io_timeout);
633 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
635 gint sock = -1, gai_error;
636 struct addrinfo hints, *res, *ai;
641 memset(&hints, 0, sizeof(hints));
642 /* hints.ai_flags = AI_CANONNAME; */
643 hints.ai_family = AF_UNSPEC;
644 hints.ai_socktype = SOCK_STREAM;
645 hints.ai_protocol = IPPROTO_TCP;
647 /* convert port from integer to string. */
648 g_snprintf(port_str, sizeof(port_str), "%d", port);
650 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
651 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
652 hostname, port_str, gai_strerror(gai_error));
656 for (ai = res; ai != NULL; ai = ai->ai_next) {
657 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
661 if (sock_connect_with_timeout
662 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
679 /* Open a connection using an external program. May be useful when
680 * you need to tunnel through a SOCKS or other firewall, or to
681 * establish an IMAP-over-SSH connection. */
682 /* TODO: Recreate this for sock_connect_thread() */
683 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
689 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
690 perror("socketpair");
693 log_message(LOG_PROTOCOL, "launching tunnel command \"%s\"\n", tunnelcmd);
698 dup(fd[1]); /* set onto stdin */
700 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
704 return sockinfo_from_fd(hostname, 0, fd[0]);
706 /* We would need a special implementation for W32. */
712 SockInfo *sock_connect(const gchar *hostname, gushort port)
721 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
725 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
726 g_warning("socket() failed: %d\n", WSAGetLastError());
728 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
730 #endif /* G_OS_WIN32 */
734 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
735 if (errno != 0) perror("connect");
741 return sockinfo_from_fd(hostname, port, sock);
745 static void sock_address_list_free(GList *addr_list)
749 for (cur = addr_list; cur != NULL; cur = cur->next) {
750 SockAddrData *addr_data = (SockAddrData *)cur->data;
751 g_free(addr_data->addr);
755 g_list_free(addr_list);
758 /* asynchronous TCP connection */
760 static gboolean sock_connect_async_cb(GIOChannel *source,
761 GIOCondition condition, gpointer data)
763 SockConnectData *conn_data = (SockConnectData *)data;
769 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
772 fd = g_io_channel_unix_get_fd(source);
774 conn_data->io_tag = 0;
775 conn_data->channel = NULL;
776 g_io_channel_unref(source);
779 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
780 perror("getsockopt");
782 sock_connect_address_list_async(conn_data);
788 sock_connect_address_list_async(conn_data);
792 sockinfo = g_new0(SockInfo, 1);
794 sockinfo->sock_ch = g_io_channel_unix_new(fd);
795 sockinfo->hostname = g_strdup(conn_data->hostname);
796 sockinfo->port = conn_data->port;
797 sockinfo->state = CONN_ESTABLISHED;
798 sockinfo->canonical_name = g_strdup(conn_data->canonical_name);
800 conn_data->func(sockinfo, conn_data->data);
802 sock_connect_async_cancel(conn_data->id);
807 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
810 SockConnectData *conn_data = (SockConnectData *)data;
812 conn_data->addr_list = addr_list;
813 conn_data->cur_addr = addr_list;
814 conn_data->canonical_name = conn_data->lookup_data->canonical_name;
815 conn_data->lookup_data->canonical_name = NULL;
816 conn_data->lookup_data = NULL;
818 return sock_connect_address_list_async(conn_data);
821 gint sock_connect_async(const gchar *hostname, gushort port,
822 SockConnectFunc func, gpointer data)
825 SockConnectData *conn_data;
827 conn_data = g_new0(SockConnectData, 1);
828 conn_data->id = id++;
829 conn_data->hostname = g_strdup(hostname);
830 conn_data->port = port;
831 conn_data->addr_list = NULL;
832 conn_data->cur_addr = NULL;
833 conn_data->io_tag = 0;
834 conn_data->func = func;
835 conn_data->data = data;
837 conn_data->lookup_data = sock_get_address_info_async
838 (hostname, port, sock_connect_async_get_address_info_cb,
841 if (conn_data->lookup_data == NULL) {
842 g_free(conn_data->hostname);
847 sock_connect_data_list = g_list_append(sock_connect_data_list,
850 return conn_data->id;
853 gint sock_connect_async_cancel(gint id)
855 SockConnectData *conn_data = NULL;
858 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
859 if (((SockConnectData *)cur->data)->id == id) {
860 conn_data = (SockConnectData *)cur->data;
866 sock_connect_data_list = g_list_remove(sock_connect_data_list,
869 if (conn_data->lookup_data)
870 sock_get_address_info_async_cancel
871 (conn_data->lookup_data);
873 if (conn_data->io_tag > 0)
874 g_source_remove(conn_data->io_tag);
875 if (conn_data->channel) {
876 g_io_channel_close(conn_data->channel);
877 g_io_channel_unref(conn_data->channel);
880 sock_address_list_free(conn_data->addr_list);
881 g_free(conn_data->canonical_name);
882 g_free(conn_data->hostname);
885 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
892 static gint sock_connect_address_list_async(SockConnectData *conn_data)
894 SockAddrData *addr_data;
897 for (; conn_data->cur_addr != NULL;
898 conn_data->cur_addr = conn_data->cur_addr->next) {
899 addr_data = (SockAddrData *)conn_data->cur_addr->data;
901 if ((sock = socket(addr_data->family, addr_data->socktype,
902 addr_data->protocol)) < 0) {
907 set_nonblocking_mode(sock, TRUE);
909 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
910 if (EINPROGRESS == errno) {
920 if (conn_data->cur_addr == NULL) {
921 g_warning("sock_connect_address_list_async: "
922 "connection to %s:%d failed\n",
923 conn_data->hostname, conn_data->port);
924 conn_data->func(NULL, conn_data->data);
925 sock_connect_async_cancel(conn_data->id);
929 conn_data->cur_addr = conn_data->cur_addr->next;
931 conn_data->channel = g_io_channel_unix_new(sock);
932 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
933 sock_connect_async_cb, conn_data);
938 /* asynchronous DNS lookup */
940 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
941 GIOCondition condition,
944 SockLookupData *lookup_data = (SockLookupData *)data;
945 GList *addr_list = NULL;
946 SockAddrData *addr_data;
949 struct sockaddr *addr;
950 gchar *canonical_name = NULL;
953 if (g_io_channel_read(source, &len, sizeof(len),
954 &bytes_read) == G_IO_ERROR_NONE) {
955 if (bytes_read == sizeof(len) && len > 0) {
958 canonical_name = g_malloc0(len + 1);
959 cur = canonical_name;
961 if (g_io_channel_read(source, cur, todo,
962 &bytes_read) != G_IO_ERROR_NONE) {
963 g_warning("canonical name not read\n");
964 g_free(canonical_name);
965 canonical_name = NULL;
971 if (bytes_read == 0) {
972 g_warning("canonical name not read\n");
973 g_free(canonical_name);
974 canonical_name = NULL;
981 if (g_io_channel_read(source, (gchar *)ai_member,
982 sizeof(ai_member), &bytes_read)
983 != G_IO_ERROR_NONE) {
984 g_warning("sock_get_address_info_async_cb: "
985 "address length read error\n");
989 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
992 if (ai_member[0] == AF_UNSPEC) {
993 g_warning("DNS lookup failed\n");
997 addr = g_malloc(ai_member[3]);
998 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
1000 != G_IO_ERROR_NONE) {
1001 g_warning("sock_get_address_info_async_cb: "
1002 "address data read error\n");
1007 if (bytes_read != ai_member[3]) {
1008 g_warning("sock_get_address_info_async_cb: "
1009 "incomplete address data\n");
1014 addr_data = g_new0(SockAddrData, 1);
1015 addr_data->family = ai_member[0];
1016 addr_data->socktype = ai_member[1];
1017 addr_data->protocol = ai_member[2];
1018 addr_data->addr_len = ai_member[3];
1019 addr_data->addr = addr;
1021 addr_list = g_list_append(addr_list, addr_data);
1024 g_io_channel_close(source);
1025 g_io_channel_unref(source);
1028 /* FIXME: We would need to cancel the thread. */
1030 kill(lookup_data->child_pid, SIGKILL);
1031 waitpid(lookup_data->child_pid, NULL, 0);
1033 lookup_data->canonical_name = canonical_name;
1035 lookup_data->func(addr_list, lookup_data->data);
1037 g_free(lookup_data->canonical_name);
1038 g_free(lookup_data->hostname);
1039 g_free(lookup_data);
1045 /* For better readability we use a separate function to implement the
1046 child code of sock_get_address_info_async. Note, that under W32
1047 this is actually not a child but a thread and this is the reason
1048 why we pass only a void pointer. */
1049 static void address_info_async_child(void *opaque)
1051 SockLookupData *parm = opaque;
1054 struct addrinfo hints, *res, *ai;
1058 gchar **addr_list_p;
1059 struct sockaddr_in ad;
1061 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
1064 close(parm->pipe_fds[0]);
1065 parm->pipe_fds[0] = -1;
1069 memset(&hints, 0, sizeof(hints));
1070 hints.ai_flags = AI_CANONNAME;
1071 hints.ai_family = AF_UNSPEC;
1072 hints.ai_socktype = SOCK_STREAM;
1073 hints.ai_protocol = IPPROTO_TCP;
1075 g_snprintf(port_str, sizeof(port_str), "%d", parm->port);
1077 gai_err = getaddrinfo(parm->hostname, port_str, &hints, &res);
1080 g_warning("getaddrinfo for %s:%s failed: %s\n",
1081 parm->hostname, port_str, gai_strerror(gai_err));
1082 fd_write_all(parm->pipe_fds[1], &len,
1084 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1086 close(parm->pipe_fds[1]);
1087 parm->pipe_fds[1] = -1;
1096 if (res->ai_canonname && strlen(res->ai_canonname) < 255) {
1097 gchar len = strlen(res->ai_canonname);
1098 fd_write_all(parm->pipe_fds[1], &len,
1100 fd_write_all(parm->pipe_fds[1], res->ai_canonname,
1104 fd_write_all(parm->pipe_fds[1], &len,
1109 fd_write_all(parm->pipe_fds[1], &len,
1113 for (ai = res; ai != NULL; ai = ai->ai_next) {
1114 ai_member[0] = ai->ai_family;
1115 ai_member[1] = ai->ai_socktype;
1116 ai_member[2] = ai->ai_protocol;
1117 ai_member[3] = ai->ai_addrlen;
1119 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1121 fd_write_all(parm->pipe_fds[1], (gchar *)ai->ai_addr,
1128 hp = my_gethostbyname(parm->hostname);
1129 if (hp == NULL || hp->h_addrtype != AF_INET) {
1131 fd_write_all(parm->pipe_fds[1], &len,
1133 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1135 close(parm->pipe_fds[1]);
1136 parm->pipe_fds[1] = -1;
1144 ai_member[0] = AF_INET;
1145 ai_member[1] = SOCK_STREAM;
1146 ai_member[2] = IPPROTO_TCP;
1147 ai_member[3] = sizeof(ad);
1149 memset(&ad, 0, sizeof(ad));
1150 ad.sin_family = AF_INET;
1151 ad.sin_port = htons(parm->port);
1153 if (hp->h_name && strlen(hp->h_name) < 255) {
1154 gchar len = strlen(hp->h_name);
1155 fd_write_all(parm->pipe_fds[1], &len,
1157 fd_write_all(parm->pipe_fds[1], hp->h_name,
1161 fd_write_all(parm->pipe_fds[1], &len,
1164 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
1166 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
1167 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1169 fd_write_all(parm->pipe_fds[1], (gchar *)&ad, sizeof(ad));
1173 close(parm->pipe_fds[1]);
1174 parm->pipe_fds[1] = -1;
1183 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
1188 SockLookupData *lookup_data = NULL;
1190 refresh_resolvers();
1192 lookup_data = g_new0(SockLookupData, 1);
1193 lookup_data->hostname = g_strdup(hostname);
1194 lookup_data->func = func;
1195 lookup_data->data = data;
1196 lookup_data->port = port;
1197 lookup_data->child_pid = (pid_t)(-1);
1198 lookup_data->pipe_fds[0] = -1;
1199 lookup_data->pipe_fds[1] = -1;
1201 if (pipe(lookup_data->pipe_fds) < 0) {
1204 g_free (lookup_data->hostname);
1205 g_free (lookup_data);
1210 if ((lookup_data->child_pid = fork()) < 0) {
1213 g_free (lookup_data->hostname);
1214 g_free (lookup_data);
1218 if (lookup_data->child_pid == 0) {
1219 /* Child process. */
1220 address_info_async_child (lookup_data);
1221 g_assert_not_reached ();
1223 /* Parent process. */
1224 close(lookup_data->pipe_fds[1]);
1225 lookup_data->pipe_fds[1] = -1;
1226 #endif /*!G_OS_WIN32 */
1228 lookup_data->channel = g_io_channel_unix_new(lookup_data->pipe_fds[0]);
1229 lookup_data->io_tag = g_io_add_watch(lookup_data->channel, G_IO_IN,
1230 sock_get_address_info_async_cb,
1233 lookup_data->child_pid = _beginthread(
1234 address_info_async_child, 0, lookup_data);
1240 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1242 if (lookup_data->io_tag > 0)
1243 g_source_remove(lookup_data->io_tag);
1244 if (lookup_data->channel) {
1245 g_io_channel_close(lookup_data->channel);
1246 g_io_channel_unref(lookup_data->channel);
1249 if (lookup_data->child_pid > 0) {
1251 /* FIXME: Need a way to cancel the thread. */
1253 kill(lookup_data->child_pid, SIGKILL);
1254 waitpid(lookup_data->child_pid, NULL, 0);
1258 g_free(lookup_data->canonical_name);
1259 g_free(lookup_data->hostname);
1260 g_free(lookup_data);
1266 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1272 sockinfo = g_new0(SockInfo, 1);
1273 sockinfo->sock = sock;
1274 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1275 sockinfo->hostname = g_strdup(hostname);
1276 sockinfo->port = port;
1277 sockinfo->state = CONN_ESTABLISHED;
1282 static gint fd_read(gint fd, gchar *buf, gint len)
1284 if (fd_check_io(fd, G_IO_IN) < 0)
1287 if (fd_is_w32_socket(fd))
1288 return recv(fd, buf, len, 0);
1289 return read(fd, buf, len);
1293 static gint ssl_read(SSL *ssl, gchar *buf, gint len)
1297 if (SSL_pending(ssl) == 0) {
1298 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1302 ret = SSL_read(ssl, buf, len);
1304 switch ((err = SSL_get_error(ssl, ret))) {
1305 case SSL_ERROR_NONE:
1307 case SSL_ERROR_WANT_READ:
1308 case SSL_ERROR_WANT_WRITE:
1311 case SSL_ERROR_ZERO_RETURN:
1314 g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
1322 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1326 g_return_val_if_fail(sock != NULL, -1);
1330 ret = ssl_read(sock->ssl, buf, len);
1333 ret = fd_read(sock->sock, buf, len);
1336 sock->state = CONN_DISCONNECTED;
1340 gint fd_write(gint fd, const gchar *buf, gint len)
1342 if (fd_check_io(fd, G_IO_OUT) < 0)
1345 if (fd_is_w32_socket (fd))
1346 return send(fd, buf, len, 0);
1347 return write(fd, buf, len);
1351 static gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1355 ret = SSL_write(ssl, buf, len);
1357 switch (SSL_get_error(ssl, ret)) {
1358 case SSL_ERROR_NONE:
1360 case SSL_ERROR_WANT_READ:
1361 case SSL_ERROR_WANT_WRITE:
1370 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1374 g_return_val_if_fail(sock != NULL, -1);
1378 ret = ssl_write(sock->ssl, buf, len);
1381 ret = fd_write(sock->sock, buf, len);
1384 sock->state = CONN_DISCONNECTED;
1388 gint fd_write_all(gint fd, const gchar *buf, gint len)
1393 if (fd_check_io(fd, G_IO_OUT) < 0)
1396 signal(SIGPIPE, SIG_IGN);
1398 if (fd_is_w32_socket(fd))
1399 n = send(fd, buf, len, 0);
1401 n = write(fd, buf, len);
1404 log_error(LOG_PROTOCOL, _("write on fd%d: %s\n"), fd, strerror(errno));
1416 static gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1421 n = ssl_write(ssl, buf, len);
1433 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1437 g_return_val_if_fail(sock != NULL, -1);
1441 ret = ssl_write_all(sock->ssl, buf, len);
1444 ret = fd_write_all(sock->sock, buf, len);
1447 sock->state = CONN_DISCONNECTED;
1451 static gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1453 if (fd_check_io(fd, G_IO_IN) < 0)
1456 return recv(fd, buf, len, flags);
1459 gint fd_gets(gint fd, gchar *buf, gint len)
1461 gchar *newline, *bp = buf;
1471 MSKB Article ID: Q147714
1472 Windows Sockets 2 Service Provider Interface Limitations
1473 Polling with recv(MSG_PEEK) to determine when a complete message
1475 Reason and Workaround not available.
1477 Single-byte send() and recv().
1478 Reason: Couple one-byte sends with Nagle disabled.
1479 Workaround: Send modest amounts and receive as much as possible.
1482 if (recv(fd, bp, 1, 0) <= 0)
1489 #else /*!G_OS_WIN32*/
1491 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1493 if ((newline = memchr(bp, '\n', n)) != NULL)
1494 n = newline - bp + 1;
1495 if ((n = fd_read(fd, bp, n)) < 0)
1499 } while (!newline && len);
1500 #endif /*!G_OS_WIN32*/
1507 static gint ssl_peek (SSL *ssl, gchar *buf, gint len);
1509 static gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1511 gchar *newline, *bp = buf;
1517 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1519 if ((newline = memchr(bp, '\n', n)) != NULL)
1520 n = newline - bp + 1;
1521 if ((n = ssl_read(ssl, bp, n)) < 0)
1525 } while (!newline && len);
1532 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1536 g_return_val_if_fail(sock != NULL, -1);
1540 return ssl_gets(sock->ssl, buf, len);
1543 return fd_gets(sock->sock, buf, len);
1546 sock->state = CONN_DISCONNECTED;
1550 /* peek at the socket data without actually reading it */
1552 static gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1556 if (SSL_pending(ssl) == 0) {
1557 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1561 ret = SSL_peek(ssl, buf, len);
1563 switch ((err = SSL_get_error(ssl, ret))) {
1564 case SSL_ERROR_NONE:
1566 case SSL_ERROR_WANT_READ:
1567 case SSL_ERROR_WANT_WRITE:
1570 case SSL_ERROR_ZERO_RETURN:
1572 case SSL_ERROR_SYSCALL:
1573 g_warning("SSL_peek() returned syscall error. errno=%d\n", errno);
1576 g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
1584 gint sock_close(SockInfo *sock)
1592 g_io_channel_unref(sock->sock_ch);
1596 ssl_done_socket(sock);
1597 if (sock->g_source != 0)
1598 g_source_remove(sock->g_source);
1602 shutdown(sock->sock, 1); /* complete transfer before close */
1603 ret = closesocket(sock->sock);
1605 ret = fd_close(sock->sock);
1608 g_free(sock->canonical_name);
1609 g_free(sock->hostname);
1615 gint fd_close(gint fd)