2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 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.
25 #include <glib/gi18n.h>
28 #include <sys/types.h>
30 # include <winsock2.h>
32 # define EINPROGRESS WSAEINPROGRESS
36 # include <sys/wait.h>
38 # include <sys/socket.h>
39 # include <sys/stat.h>
41 # include <netinet/in.h>
42 # include <arpa/inet.h>
45 #endif /* G_OS_WIN32 */
55 # include <sys/select.h>
66 #error USE_GIO is currently not supported
76 typedef gint (*SockAddrFunc) (GList *addr_list,
79 typedef struct _SockConnectData SockConnectData;
80 typedef struct _SockLookupData SockLookupData;
81 typedef struct _SockAddrData SockAddrData;
82 typedef struct _SockSource SockSource;
84 struct _SockConnectData {
90 SockLookupData *lookup_data;
95 gchar *canonical_name;
98 struct _SockLookupData {
107 gchar *canonical_name;
110 struct _SockAddrData {
115 struct sockaddr *addr;
123 static guint io_timeout = 60;
125 static GList *sock_connect_data_list = NULL;
127 static gboolean sock_prepare (GSource *source,
129 static gboolean sock_check (GSource *source);
130 static gboolean sock_dispatch (GSource *source,
131 GSourceFunc callback,
134 GSourceFuncs sock_watch_funcs = {
141 static gint sock_connect_with_timeout (gint sock,
142 const struct sockaddr *serv_addr,
147 static gint sock_connect_by_hostname (gint sock,
148 const gchar *hostname,
151 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
155 static SockInfo *sockinfo_from_fd(const gchar *hostname,
158 static void sock_address_list_free (GList *addr_list);
160 static gboolean sock_connect_async_cb (GIOChannel *source,
161 GIOCondition condition,
163 static gint sock_connect_async_get_address_info_cb
167 static gint sock_connect_address_list_async (SockConnectData *conn_data);
169 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
170 GIOCondition condition,
172 static SockLookupData *sock_get_address_info_async
173 (const gchar *hostname,
177 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
186 result = WSAStartup(MAKEWORD(2, 2), &wsadata);
187 if (result != NO_ERROR) {
188 g_warning("WSAStartup() failed\n");
195 gint sock_cleanup(void)
203 gint sock_set_io_timeout(guint sec)
209 void refresh_resolvers(void)
212 static time_t resolv_conf_changed = (time_t)NULL;
215 /* This makes the glibc re-read resolv.conf, if it changed
216 * since our startup. Maybe that should be #ifdef'ed, I don't
217 * know if it'd work on BSDs.
218 * Why doesn't the glibc do it by itself?
220 if (stat("/etc/resolv.conf", &s) == 0) {
221 if (s.st_mtime > resolv_conf_changed) {
222 resolv_conf_changed = s.st_mtime;
226 we'll have bigger problems. */
231 /* Due to the fact that socket under Windows are not represented by
232 standard file descriptors, we sometimes need to check whether a
233 given file descriptor is actually a socket. This is done by
234 testing for an error. Returns true under W32 if FD is a socket. */
235 static int fd_is_w32_socket(gint fd)
239 gint retval = sizeof(optval);
241 return !getsockopt(fd, SOL_SOCKET, SO_TYPE, (char*)&optval, &retval);
248 gint fd_connect_unix(const gchar *path)
252 struct sockaddr_un addr;
254 sock = socket(PF_UNIX, SOCK_STREAM, 0);
256 perror("sock_connect_unix(): socket");
260 memset(&addr, 0, sizeof(addr));
261 addr.sun_family = AF_UNIX;
262 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
264 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
275 gint fd_open_unix(const gchar *path)
279 struct sockaddr_un addr;
281 sock = socket(PF_UNIX, SOCK_STREAM, 0);
284 perror("sock_open_unix(): socket");
288 memset(&addr, 0, sizeof(addr));
289 addr.sun_family = AF_UNIX;
290 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
292 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
298 if (listen(sock, 1) < 0) {
310 gint fd_accept(gint sock)
312 struct sockaddr_in caddr;
315 caddr_len = sizeof(caddr);
316 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
320 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
325 flags = fcntl(fd, F_GETFL, 0);
334 flags &= ~O_NONBLOCK;
336 return fcntl(fd, F_SETFL, flags);
342 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
344 g_return_val_if_fail(sock != NULL, -1);
346 return set_nonblocking_mode(sock->sock, nonblock);
349 static gboolean is_nonblocking_mode(gint fd)
354 flags = fcntl(fd, F_GETFL, 0);
360 return ((flags & O_NONBLOCK) != 0);
366 gboolean sock_is_nonblocking_mode(SockInfo *sock)
368 g_return_val_if_fail(sock != NULL, FALSE);
370 return is_nonblocking_mode(sock->sock);
374 static gboolean sock_prepare(GSource *source, gint *timeout)
380 static gboolean sock_check(GSource *source)
382 SockInfo *sock = ((SockSource *)source)->sock;
383 struct timeval timeout = {0, 0};
385 GIOCondition condition = sock->condition;
387 if (!sock || !sock->sock)
392 if (condition & G_IO_IN) {
393 if (SSL_pending(sock->ssl) > 0)
395 if (SSL_want_write(sock->ssl))
396 condition |= G_IO_OUT;
399 if (condition & G_IO_OUT) {
400 if (SSL_want_read(sock->ssl))
401 condition |= G_IO_IN;
407 FD_SET(sock->sock, &fds);
409 select(sock->sock + 1,
410 (condition & G_IO_IN) ? &fds : NULL,
411 (condition & G_IO_OUT) ? &fds : NULL,
414 return FD_ISSET(sock->sock, &fds) != 0;
417 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
420 SockInfo *sock = ((SockSource *)source)->sock;
422 if (!sock || !sock->callback || !sock->data)
425 return sock->callback(sock, sock->condition, sock->data);
428 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
431 SockInfo *sock = (SockInfo *)data;
433 if ((condition & sock->condition) == 0)
436 return sock->callback(sock, sock->condition, sock->data);
439 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
442 sock->callback = func;
443 sock->condition = condition;
449 GSource *source = g_source_new(&sock_watch_funcs,
451 ((SockSource *) source)->sock = sock;
452 g_source_set_priority(source, G_PRIORITY_DEFAULT);
453 g_source_set_can_recurse(source, FALSE);
454 sock->g_source = g_source_attach(source, NULL);
455 g_source_unref (source); /* Refcount back down to 1 */
456 return sock->g_source;
460 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
463 static gint fd_check_io(gint fd, GIOCondition cond)
465 struct timeval timeout;
468 if (is_nonblocking_mode(fd))
471 timeout.tv_sec = io_timeout;
477 if (cond == G_IO_IN) {
478 select(fd + 1, &fds, NULL, NULL,
479 io_timeout > 0 ? &timeout : NULL);
481 select(fd + 1, NULL, &fds, NULL,
482 io_timeout > 0 ? &timeout : NULL);
485 if (FD_ISSET(fd, &fds)) {
488 g_warning("Socket IO timeout\n");
494 static sigjmp_buf jmpenv;
496 static void timeout_handler(gint sig)
498 siglongjmp(jmpenv, 1);
502 static gint sock_connect_with_timeout(gint sock,
503 const struct sockaddr *serv_addr,
509 void (*prev_handler)(gint);
512 prev_handler = signal(SIGALRM, timeout_handler);
513 if (sigsetjmp(jmpenv, 1)) {
515 signal(SIGALRM, prev_handler);
522 ret = connect(sock, serv_addr, addrlen);
526 signal(SIGALRM, prev_handler);
532 struct hostent *my_gethostbyname(const gchar *hostname)
536 void (*prev_handler)(gint);
539 prev_handler = signal(SIGALRM, timeout_handler);
540 if (sigsetjmp(jmpenv, 1)) {
542 signal(SIGALRM, prev_handler);
543 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
550 if ((hp = gethostbyname(hostname)) == NULL) {
553 signal(SIGALRM, prev_handler);
555 fprintf(stderr, "%s: unknown host.\n", hostname);
562 signal(SIGALRM, prev_handler);
569 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
572 return inet_aton(hostname, inp);
577 inaddr = inet_addr(hostname);
579 memcpy(inp, &inaddr, sizeof(inaddr));
586 #endif /* HAVE_INET_ATON */
589 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
593 struct sockaddr_in ad;
595 memset(&ad, 0, sizeof(ad));
596 ad.sin_family = AF_INET;
597 ad.sin_port = htons(port);
601 if (!my_inet_aton(hostname, &ad.sin_addr)) {
602 if ((hp = my_gethostbyname(hostname)) == NULL) {
603 fprintf(stderr, "%s: unknown host.\n", hostname);
608 if (hp->h_length != 4 && hp->h_length != 8) {
609 fprintf(stderr, "illegal address length received for host %s\n", hostname);
614 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
617 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
618 sizeof(ad), io_timeout);
622 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
624 gint sock = -1, gai_error;
625 struct addrinfo hints, *res, *ai;
630 memset(&hints, 0, sizeof(hints));
631 /* hints.ai_flags = AI_CANONNAME; */
632 hints.ai_family = AF_UNSPEC;
633 hints.ai_socktype = SOCK_STREAM;
634 hints.ai_protocol = IPPROTO_TCP;
636 /* convert port from integer to string. */
637 g_snprintf(port_str, sizeof(port_str), "%d", port);
639 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
640 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
641 hostname, port_str, gai_strerror(gai_error));
645 for (ai = res; ai != NULL; ai = ai->ai_next) {
646 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
650 if (sock_connect_with_timeout
651 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
668 /* Open a connection using an external program. May be useful when
669 * you need to tunnel through a SOCKS or other firewall, or to
670 * establish an IMAP-over-SSH connection. */
671 /* TODO: Recreate this for sock_connect_thread() */
672 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
678 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
679 perror("socketpair");
682 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
687 dup(fd[1]); /* set onto stdin */
689 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
693 return sockinfo_from_fd(hostname, 0, fd[0]);
695 /* We would need a special implementation for W32. */
701 SockInfo *sock_connect(const gchar *hostname, gushort port)
710 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
714 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
715 g_warning("socket() failed: %d\n", WSAGetLastError());
717 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
719 #endif /* G_OS_WIN32 */
723 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
724 if (errno != 0) perror("connect");
730 return sockinfo_from_fd(hostname, port, sock);
734 static void sock_address_list_free(GList *addr_list)
738 for (cur = addr_list; cur != NULL; cur = cur->next) {
739 SockAddrData *addr_data = (SockAddrData *)cur->data;
740 g_free(addr_data->addr);
744 g_list_free(addr_list);
747 /* asynchronous TCP connection */
749 static gboolean sock_connect_async_cb(GIOChannel *source,
750 GIOCondition condition, gpointer data)
752 SockConnectData *conn_data = (SockConnectData *)data;
758 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
761 fd = g_io_channel_unix_get_fd(source);
763 conn_data->io_tag = 0;
764 conn_data->channel = NULL;
765 g_io_channel_unref(source);
768 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
769 perror("getsockopt");
771 sock_connect_address_list_async(conn_data);
777 sock_connect_address_list_async(conn_data);
781 sockinfo = g_new0(SockInfo, 1);
783 sockinfo->sock_ch = g_io_channel_unix_new(fd);
784 sockinfo->hostname = g_strdup(conn_data->hostname);
785 sockinfo->port = conn_data->port;
786 sockinfo->state = CONN_ESTABLISHED;
787 sockinfo->canonical_name = g_strdup(conn_data->canonical_name);
789 conn_data->func(sockinfo, conn_data->data);
791 sock_connect_async_cancel(conn_data->id);
796 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
799 SockConnectData *conn_data = (SockConnectData *)data;
801 conn_data->addr_list = addr_list;
802 conn_data->cur_addr = addr_list;
803 conn_data->canonical_name = conn_data->lookup_data->canonical_name;
804 conn_data->lookup_data->canonical_name = NULL;
805 conn_data->lookup_data = NULL;
807 return sock_connect_address_list_async(conn_data);
810 gint sock_connect_async(const gchar *hostname, gushort port,
811 SockConnectFunc func, gpointer data)
814 SockConnectData *conn_data;
816 conn_data = g_new0(SockConnectData, 1);
817 conn_data->id = id++;
818 conn_data->hostname = g_strdup(hostname);
819 conn_data->port = port;
820 conn_data->addr_list = NULL;
821 conn_data->cur_addr = NULL;
822 conn_data->io_tag = 0;
823 conn_data->func = func;
824 conn_data->data = data;
826 conn_data->lookup_data = sock_get_address_info_async
827 (hostname, port, sock_connect_async_get_address_info_cb,
830 if (conn_data->lookup_data == NULL) {
831 g_free(conn_data->hostname);
836 sock_connect_data_list = g_list_append(sock_connect_data_list,
839 return conn_data->id;
842 gint sock_connect_async_cancel(gint id)
844 SockConnectData *conn_data = NULL;
847 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
848 if (((SockConnectData *)cur->data)->id == id) {
849 conn_data = (SockConnectData *)cur->data;
855 sock_connect_data_list = g_list_remove(sock_connect_data_list,
858 if (conn_data->lookup_data)
859 sock_get_address_info_async_cancel
860 (conn_data->lookup_data);
862 if (conn_data->io_tag > 0)
863 g_source_remove(conn_data->io_tag);
864 if (conn_data->channel) {
865 g_io_channel_close(conn_data->channel);
866 g_io_channel_unref(conn_data->channel);
869 sock_address_list_free(conn_data->addr_list);
870 g_free(conn_data->canonical_name);
871 g_free(conn_data->hostname);
874 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
881 static gint sock_connect_address_list_async(SockConnectData *conn_data)
883 SockAddrData *addr_data;
886 for (; conn_data->cur_addr != NULL;
887 conn_data->cur_addr = conn_data->cur_addr->next) {
888 addr_data = (SockAddrData *)conn_data->cur_addr->data;
890 if ((sock = socket(addr_data->family, addr_data->socktype,
891 addr_data->protocol)) < 0) {
896 set_nonblocking_mode(sock, TRUE);
898 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
899 if (EINPROGRESS == errno) {
909 if (conn_data->cur_addr == NULL) {
910 g_warning("sock_connect_address_list_async: "
911 "connection to %s:%d failed\n",
912 conn_data->hostname, conn_data->port);
913 conn_data->func(NULL, conn_data->data);
914 sock_connect_async_cancel(conn_data->id);
918 conn_data->cur_addr = conn_data->cur_addr->next;
920 conn_data->channel = g_io_channel_unix_new(sock);
921 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
922 sock_connect_async_cb, conn_data);
927 /* asynchronous DNS lookup */
929 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
930 GIOCondition condition,
933 SockLookupData *lookup_data = (SockLookupData *)data;
934 GList *addr_list = NULL;
935 SockAddrData *addr_data;
938 struct sockaddr *addr;
939 gchar *canonical_name = NULL;
942 if (g_io_channel_read(source, &len, sizeof(len),
943 &bytes_read) == G_IO_ERROR_NONE) {
944 if (bytes_read == sizeof(len) && len > 0) {
947 canonical_name = g_malloc0(len + 1);
948 cur = canonical_name;
950 if (g_io_channel_read(source, cur, todo,
951 &bytes_read) != G_IO_ERROR_NONE) {
952 g_warning("canonical name not read\n");
953 g_free(canonical_name);
954 canonical_name = NULL;
960 if (bytes_read == 0) {
961 g_warning("canonical name not read\n");
962 g_free(canonical_name);
963 canonical_name = NULL;
970 if (g_io_channel_read(source, (gchar *)ai_member,
971 sizeof(ai_member), &bytes_read)
972 != G_IO_ERROR_NONE) {
973 g_warning("sock_get_address_info_async_cb: "
974 "address length read error\n");
978 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
981 if (ai_member[0] == AF_UNSPEC) {
982 g_warning("DNS lookup failed\n");
986 addr = g_malloc(ai_member[3]);
987 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
989 != G_IO_ERROR_NONE) {
990 g_warning("sock_get_address_info_async_cb: "
991 "address data read error\n");
996 if (bytes_read != ai_member[3]) {
997 g_warning("sock_get_address_info_async_cb: "
998 "incomplete address data\n");
1003 addr_data = g_new0(SockAddrData, 1);
1004 addr_data->family = ai_member[0];
1005 addr_data->socktype = ai_member[1];
1006 addr_data->protocol = ai_member[2];
1007 addr_data->addr_len = ai_member[3];
1008 addr_data->addr = addr;
1010 addr_list = g_list_append(addr_list, addr_data);
1013 g_io_channel_close(source);
1014 g_io_channel_unref(source);
1017 /* FIXME: We would need to cancel the thread. */
1019 kill(lookup_data->child_pid, SIGKILL);
1020 waitpid(lookup_data->child_pid, NULL, 0);
1022 lookup_data->canonical_name = canonical_name;
1024 lookup_data->func(addr_list, lookup_data->data);
1026 g_free(lookup_data->canonical_name);
1027 g_free(lookup_data->hostname);
1028 g_free(lookup_data);
1034 /* For better readability we use a separate function to implement the
1035 child code of sock_get_address_info_async. Note, that under W32
1036 this is actually not a child but a thread and this is the reason
1037 why we pass only a void pointer. */
1038 static void address_info_async_child(void *opaque)
1040 SockLookupData *parm = opaque;
1043 struct addrinfo hints, *res, *ai;
1047 gchar **addr_list_p;
1048 struct sockaddr_in ad;
1050 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
1053 close(parm->pipe_fds[0]);
1054 parm->pipe_fds[0] = -1;
1058 memset(&hints, 0, sizeof(hints));
1059 hints.ai_flags = AI_CANONNAME;
1060 hints.ai_family = AF_UNSPEC;
1061 hints.ai_socktype = SOCK_STREAM;
1062 hints.ai_protocol = IPPROTO_TCP;
1064 g_snprintf(port_str, sizeof(port_str), "%d", parm->port);
1066 gai_err = getaddrinfo(parm->hostname, port_str, &hints, &res);
1069 g_warning("getaddrinfo for %s:%s failed: %s\n",
1070 parm->hostname, port_str, gai_strerror(gai_err));
1071 fd_write_all(parm->pipe_fds[1], &len,
1073 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1075 close(parm->pipe_fds[1]);
1076 parm->pipe_fds[1] = -1;
1085 if (res->ai_canonname && strlen(res->ai_canonname) < 255) {
1086 gchar len = strlen(res->ai_canonname);
1087 fd_write_all(parm->pipe_fds[1], &len,
1089 fd_write_all(parm->pipe_fds[1], res->ai_canonname,
1093 fd_write_all(parm->pipe_fds[1], &len,
1098 fd_write_all(parm->pipe_fds[1], &len,
1102 for (ai = res; ai != NULL; ai = ai->ai_next) {
1103 ai_member[0] = ai->ai_family;
1104 ai_member[1] = ai->ai_socktype;
1105 ai_member[2] = ai->ai_protocol;
1106 ai_member[3] = ai->ai_addrlen;
1108 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1110 fd_write_all(parm->pipe_fds[1], (gchar *)ai->ai_addr,
1117 hp = my_gethostbyname(parm->hostname);
1118 if (hp == NULL || hp->h_addrtype != AF_INET) {
1120 fd_write_all(parm->pipe_fds[1], &len,
1122 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1124 close(parm->pipe_fds[1]);
1125 parm->pipe_fds[1] = -1;
1133 ai_member[0] = AF_INET;
1134 ai_member[1] = SOCK_STREAM;
1135 ai_member[2] = IPPROTO_TCP;
1136 ai_member[3] = sizeof(ad);
1138 memset(&ad, 0, sizeof(ad));
1139 ad.sin_family = AF_INET;
1140 ad.sin_port = htons(parm->port);
1142 if (hp->h_name && strlen(hp->h_name) < 255) {
1143 gchar len = strlen(hp->h_name);
1144 fd_write_all(parm->pipe_fds[1], &len,
1146 fd_write_all(parm->pipe_fds[1], hp->h_name,
1150 fd_write_all(parm->pipe_fds[1], &len,
1153 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
1155 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
1156 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
1158 fd_write_all(parm->pipe_fds[1], (gchar *)&ad, sizeof(ad));
1162 close(parm->pipe_fds[1]);
1163 parm->pipe_fds[1] = -1;
1172 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
1177 SockLookupData *lookup_data = NULL;
1179 refresh_resolvers();
1181 lookup_data = g_new0(SockLookupData, 1);
1182 lookup_data->hostname = g_strdup(hostname);
1183 lookup_data->func = func;
1184 lookup_data->data = data;
1185 lookup_data->port = port;
1186 lookup_data->child_pid = (pid_t)(-1);
1187 lookup_data->pipe_fds[0] = -1;
1188 lookup_data->pipe_fds[1] = -1;
1190 if (pipe(lookup_data->pipe_fds) < 0) {
1193 g_free (lookup_data->hostname);
1194 g_free (lookup_data);
1199 if ((lookup_data->child_pid = fork()) < 0) {
1202 g_free (lookup_data->hostname);
1203 g_free (lookup_data);
1207 if (lookup_data->child_pid == 0) {
1208 /* Child process. */
1209 address_info_async_child (lookup_data);
1210 g_assert_not_reached ();
1212 /* Parent process. */
1213 close(lookup_data->pipe_fds[1]);
1214 lookup_data->pipe_fds[1] = -1;
1215 #endif /*!G_OS_WIN32 */
1217 lookup_data->channel = g_io_channel_unix_new(lookup_data->pipe_fds[0]);
1218 lookup_data->io_tag = g_io_add_watch(lookup_data->channel, G_IO_IN,
1219 sock_get_address_info_async_cb,
1222 lookup_data->child_pid = _beginthread(
1223 address_info_async_child, 0, lookup_data);
1229 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1231 if (lookup_data->io_tag > 0)
1232 g_source_remove(lookup_data->io_tag);
1233 if (lookup_data->channel) {
1234 g_io_channel_close(lookup_data->channel);
1235 g_io_channel_unref(lookup_data->channel);
1238 if (lookup_data->child_pid > 0) {
1240 /* FIXME: Need a way to cancel the thread. */
1242 kill(lookup_data->child_pid, SIGKILL);
1243 waitpid(lookup_data->child_pid, NULL, 0);
1247 g_free(lookup_data->canonical_name);
1248 g_free(lookup_data->hostname);
1249 g_free(lookup_data);
1255 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1261 sockinfo = g_new0(SockInfo, 1);
1262 sockinfo->sock = sock;
1263 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1264 sockinfo->hostname = g_strdup(hostname);
1265 sockinfo->port = port;
1266 sockinfo->state = CONN_ESTABLISHED;
1271 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1274 gchar buf[BUFFSIZE];
1276 va_start(args, format);
1277 g_vsnprintf(buf, sizeof(buf), format, args);
1280 return sock_write_all(sock, buf, strlen(buf));
1283 gint fd_read(gint fd, gchar *buf, gint len)
1285 if (fd_check_io(fd, G_IO_IN) < 0)
1288 if (fd_is_w32_socket(fd))
1289 return recv(fd, buf, len, 0);
1290 return read(fd, buf, len);
1294 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1298 if (SSL_pending(ssl) == 0) {
1299 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1303 ret = SSL_read(ssl, buf, len);
1305 switch ((err = SSL_get_error(ssl, ret))) {
1306 case SSL_ERROR_NONE:
1308 case SSL_ERROR_WANT_READ:
1309 case SSL_ERROR_WANT_WRITE:
1312 case SSL_ERROR_ZERO_RETURN:
1315 g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
1323 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1327 g_return_val_if_fail(sock != NULL, -1);
1331 ret = ssl_read(sock->ssl, buf, len);
1334 ret = fd_read(sock->sock, buf, len);
1337 sock->state = CONN_DISCONNECTED;
1341 gint fd_write(gint fd, const gchar *buf, gint len)
1343 if (fd_check_io(fd, G_IO_OUT) < 0)
1346 if (fd_is_w32_socket (fd))
1347 return send(fd, buf, len, 0);
1348 return write(fd, buf, len);
1352 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1356 ret = SSL_write(ssl, buf, len);
1358 switch (SSL_get_error(ssl, ret)) {
1359 case SSL_ERROR_NONE:
1361 case SSL_ERROR_WANT_READ:
1362 case SSL_ERROR_WANT_WRITE:
1371 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1375 g_return_val_if_fail(sock != NULL, -1);
1379 ret = ssl_write(sock->ssl, buf, len);
1382 ret = fd_write(sock->sock, buf, len);
1385 sock->state = CONN_DISCONNECTED;
1389 gint fd_write_all(gint fd, const gchar *buf, gint len)
1394 if (fd_check_io(fd, G_IO_OUT) < 0)
1397 signal(SIGPIPE, SIG_IGN);
1399 if (fd_is_w32_socket(fd))
1400 n = send(fd, buf, len, 0);
1402 n = write(fd, buf, len);
1405 log_error(_("write on fd%d: %s\n"), fd, strerror(errno));
1417 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1422 n = ssl_write(ssl, buf, len);
1434 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1438 g_return_val_if_fail(sock != NULL, -1);
1442 ret = ssl_write_all(sock->ssl, buf, len);
1445 ret = fd_write_all(sock->sock, buf, len);
1448 sock->state = CONN_DISCONNECTED;
1452 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1454 if (fd_check_io(fd, G_IO_IN) < 0)
1457 return recv(fd, buf, len, flags);
1460 gint fd_gets(gint fd, gchar *buf, gint len)
1462 gchar *newline, *bp = buf;
1472 MSKB Article ID: Q147714
1473 Windows Sockets 2 Service Provider Interface Limitations
1474 Polling with recv(MSG_PEEK) to determine when a complete message
1476 Reason and Workaround not available.
1478 Single-byte send() and recv().
1479 Reason: Couple one-byte sends with Nagle disabled.
1480 Workaround: Send modest amounts and receive as much as possible.
1483 if (recv(fd, bp, 1, 0) <= 0)
1490 #else /*!G_OS_WIN32*/
1492 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1494 if ((newline = memchr(bp, '\n', n)) != NULL)
1495 n = newline - bp + 1;
1496 if ((n = fd_read(fd, bp, n)) < 0)
1500 } while (!newline && len);
1501 #endif /*!G_OS_WIN32*/
1508 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1510 gchar *newline, *bp = buf;
1516 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1518 if ((newline = memchr(bp, '\n', n)) != NULL)
1519 n = newline - bp + 1;
1520 if ((n = ssl_read(ssl, bp, n)) < 0)
1524 } while (!newline && len);
1531 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1535 g_return_val_if_fail(sock != NULL, -1);
1539 return ssl_gets(sock->ssl, buf, len);
1542 return fd_gets(sock->sock, buf, len);
1545 sock->state = CONN_DISCONNECTED;
1549 gint fd_getline(gint fd, gchar **str)
1551 gchar buf[BUFFSIZE];
1555 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1558 *str = g_strdup(buf);
1560 *str = g_realloc(*str, size);
1563 if (buf[len - 1] == '\n'
1564 #ifdef G_OS_WIN32 /* FIXME This does not seem to be correct. */
1565 || buf[len - 1] == '\r'
1570 if (len == -1 && *str)
1577 gint ssl_getline(SSL *ssl, gchar **str)
1579 gchar buf[BUFFSIZE];
1583 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1586 *str = g_strdup(buf);
1588 *str = g_realloc(*str, size);
1591 if (buf[len - 1] == '\n')
1594 if (len == -1 && *str)
1601 gchar *sock_getline(SockInfo *sock)
1606 g_return_val_if_fail(sock != NULL, NULL);
1610 ret = ssl_getline(sock->ssl, &str);
1613 ret = fd_getline(sock->sock, &str);
1616 sock->state = CONN_DISCONNECTED;
1620 gint sock_puts(SockInfo *sock, const gchar *buf)
1624 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1626 return sock_write_all(sock, "\r\n", 2);
1629 /* peek at the socket data without actually reading it */
1631 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1635 if (SSL_pending(ssl) == 0) {
1636 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1640 ret = SSL_peek(ssl, buf, len);
1642 switch ((err = SSL_get_error(ssl, ret))) {
1643 case SSL_ERROR_NONE:
1645 case SSL_ERROR_WANT_READ:
1646 case SSL_ERROR_WANT_WRITE:
1649 case SSL_ERROR_ZERO_RETURN:
1651 case SSL_ERROR_SYSCALL:
1652 g_warning("SSL_peek() returned syscall error. errno=%d\n", errno);
1655 g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
1663 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1665 g_return_val_if_fail(sock != NULL, -1);
1669 return ssl_peek(sock->ssl, buf, len);
1671 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1674 gint sock_close(SockInfo *sock)
1682 g_io_channel_unref(sock->sock_ch);
1686 ssl_done_socket(sock);
1687 if (sock->g_source != 0)
1688 g_source_remove(sock->g_source);
1692 shutdown(sock->sock, 1); /* complete transfer before close */
1693 ret = closesocket(sock->sock);
1695 ret = fd_close(sock->sock);
1698 g_free(sock->canonical_name);
1699 g_free(sock->hostname);
1705 gint fd_close(gint fd)