2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <sys/types.h>
28 # include <winsock2.h>
31 # include <sys/wait.h>
33 # include <sys/socket.h>
34 # include <sys/stat.h>
36 # include <netinet/in.h>
37 # include <arpa/inet.h>
40 #endif /* G_OS_WIN32 */
50 # include <sys/select.h>
61 #error USE_GIO is currently not supported
66 typedef gint (*SockAddrFunc) (GList *addr_list,
69 typedef struct _SockConnectData SockConnectData;
70 typedef struct _SockLookupData SockLookupData;
71 typedef struct _SockAddrData SockAddrData;
72 typedef struct _SockSource SockSource;
74 struct _SockConnectData {
80 SockLookupData *lookup_data;
87 struct _SockLookupData {
96 struct _SockAddrData {
101 struct sockaddr *addr;
109 static guint io_timeout = 60;
111 static GList *sock_connect_data_list = NULL;
113 static gboolean sock_prepare (GSource *source,
115 static gboolean sock_check (GSource *source);
116 static gboolean sock_dispatch (GSource *source,
117 GSourceFunc callback,
120 GSourceFuncs sock_watch_funcs = {
127 static gint sock_connect_with_timeout (gint sock,
128 const struct sockaddr *serv_addr,
133 static gint sock_connect_by_hostname (gint sock,
134 const gchar *hostname,
137 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
141 static SockInfo *sockinfo_from_fd(const gchar *hostname,
145 static void sock_address_list_free (GList *addr_list);
147 static gboolean sock_connect_async_cb (GIOChannel *source,
148 GIOCondition condition,
150 static gint sock_connect_async_get_address_info_cb
154 static gint sock_connect_address_list_async (SockConnectData *conn_data);
156 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
157 GIOCondition condition,
159 static SockLookupData *sock_get_address_info_async
160 (const gchar *hostname,
164 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
165 #endif /* G_OS_UNIX */
174 result = WSAStartup(MAKEWORD(2, 2), &wsadata);
175 if (result != NO_ERROR) {
176 g_warning("WSAStartup() failed\n");
183 gint sock_cleanup(void)
191 gint sock_set_io_timeout(guint sec)
197 void refresh_resolvers(void)
199 static time_t resolv_conf_changed = (time_t)NULL;
202 /* This makes the glibc re-read resolv.conf, if it changed
203 * since our startup. Maybe that should be #ifdef'ed, I don't
204 * know if it'd work on BSDs.
205 * Why doesn't the glibc do it by itself?
207 if (stat("/etc/resolv.conf", &s) == 0) {
208 if (s.st_mtime > resolv_conf_changed) {
209 resolv_conf_changed = s.st_mtime;
213 we'll have bigger problems. */
216 gint fd_connect_unix(const gchar *path)
220 struct sockaddr_un addr;
222 sock = socket(PF_UNIX, SOCK_STREAM, 0);
224 perror("sock_connect_unix(): socket");
228 memset(&addr, 0, sizeof(addr));
229 addr.sun_family = AF_UNIX;
230 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
232 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
243 gint fd_open_unix(const gchar *path)
247 struct sockaddr_un addr;
249 sock = socket(PF_UNIX, SOCK_STREAM, 0);
252 perror("sock_open_unix(): socket");
256 memset(&addr, 0, sizeof(addr));
257 addr.sun_family = AF_UNIX;
258 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
260 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
266 if (listen(sock, 1) < 0) {
278 gint fd_accept(gint sock)
281 struct sockaddr_in caddr;
284 caddr_len = sizeof(caddr);
285 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
292 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
297 flags = fcntl(fd, F_GETFL, 0);
306 flags &= ~O_NONBLOCK;
308 return fcntl(fd, F_SETFL, flags);
314 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
316 g_return_val_if_fail(sock != NULL, -1);
318 return set_nonblocking_mode(sock->sock, nonblock);
321 static gboolean is_nonblocking_mode(gint fd)
326 flags = fcntl(fd, F_GETFL, 0);
332 return ((flags & O_NONBLOCK) != 0);
338 gboolean sock_is_nonblocking_mode(SockInfo *sock)
340 g_return_val_if_fail(sock != NULL, FALSE);
342 return is_nonblocking_mode(sock->sock);
346 static gboolean sock_prepare(GSource *source, gint *timeout)
352 static gboolean sock_check(GSource *source)
354 SockInfo *sock = ((SockSource *)source)->sock;
355 struct timeval timeout = {0, 0};
357 GIOCondition condition = sock->condition;
359 if (!sock || !sock->sock)
364 if (condition & G_IO_IN) {
365 if (SSL_pending(sock->ssl) > 0)
367 if (SSL_want_write(sock->ssl))
368 condition |= G_IO_OUT;
371 if (condition & G_IO_OUT) {
372 if (SSL_want_read(sock->ssl))
373 condition |= G_IO_IN;
379 FD_SET(sock->sock, &fds);
381 select(sock->sock + 1,
382 (condition & G_IO_IN) ? &fds : NULL,
383 (condition & G_IO_OUT) ? &fds : NULL,
386 return FD_ISSET(sock->sock, &fds) != 0;
389 static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
392 SockInfo *sock = ((SockSource *)source)->sock;
394 if (!sock || !sock->callback || !sock->data)
397 return sock->callback(sock, sock->condition, sock->data);
400 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
403 SockInfo *sock = (SockInfo *)data;
405 if ((condition & sock->condition) == 0)
408 return sock->callback(sock, sock->condition, sock->data);
411 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
414 sock->callback = func;
415 sock->condition = condition;
421 GSource *source = g_source_new(&sock_watch_funcs,
423 ((SockSource *) source)->sock = sock;
424 g_source_set_priority(source, G_PRIORITY_DEFAULT);
425 g_source_set_can_recurse(source, FALSE);
426 sock->g_source = g_source_attach(source, NULL);
427 g_source_unref (source); /* Refcount back down to 1 */
428 return sock->g_source;
432 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
435 static gint fd_check_io(gint fd, GIOCondition cond)
437 struct timeval timeout;
440 if (is_nonblocking_mode(fd))
443 timeout.tv_sec = io_timeout;
449 if (cond == G_IO_IN) {
450 select(fd + 1, &fds, NULL, NULL,
451 io_timeout > 0 ? &timeout : NULL);
453 select(fd + 1, NULL, &fds, NULL,
454 io_timeout > 0 ? &timeout : NULL);
457 if (FD_ISSET(fd, &fds)) {
460 g_warning("Socket IO timeout\n");
466 static sigjmp_buf jmpenv;
468 static void timeout_handler(gint sig)
470 siglongjmp(jmpenv, 1);
474 static gint sock_connect_with_timeout(gint sock,
475 const struct sockaddr *serv_addr,
481 void (*prev_handler)(gint);
484 prev_handler = signal(SIGALRM, timeout_handler);
485 if (sigsetjmp(jmpenv, 1)) {
487 signal(SIGALRM, prev_handler);
494 ret = connect(sock, serv_addr, addrlen);
498 signal(SIGALRM, prev_handler);
504 struct hostent *my_gethostbyname(const gchar *hostname)
508 void (*prev_handler)(gint);
511 prev_handler = signal(SIGALRM, timeout_handler);
512 if (sigsetjmp(jmpenv, 1)) {
514 signal(SIGALRM, prev_handler);
515 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
522 if ((hp = gethostbyname(hostname)) == NULL) {
525 signal(SIGALRM, prev_handler);
527 fprintf(stderr, "%s: unknown host.\n", hostname);
534 signal(SIGALRM, prev_handler);
541 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
544 return inet_aton(hostname, inp);
549 inaddr = inet_addr(hostname);
551 memcpy(inp, &inaddr, sizeof(inaddr));
558 #endif /* HAVE_INET_ATON */
561 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
565 struct sockaddr_in ad;
567 memset(&ad, 0, sizeof(ad));
568 ad.sin_family = AF_INET;
569 ad.sin_port = htons(port);
573 if (!my_inet_aton(hostname, &ad.sin_addr)) {
574 if ((hp = my_gethostbyname(hostname)) == NULL) {
575 fprintf(stderr, "%s: unknown host.\n", hostname);
580 if (hp->h_length != 4 && hp->h_length != 8) {
581 fprintf(stderr, "illegal address length received for host %s\n", hostname);
586 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
589 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
590 sizeof(ad), io_timeout);
594 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
596 gint sock = -1, gai_error;
597 struct addrinfo hints, *res, *ai;
602 memset(&hints, 0, sizeof(hints));
603 /* hints.ai_flags = AI_CANONNAME; */
604 hints.ai_family = AF_UNSPEC;
605 hints.ai_socktype = SOCK_STREAM;
606 hints.ai_protocol = IPPROTO_TCP;
608 /* convert port from integer to string. */
609 g_snprintf(port_str, sizeof(port_str), "%d", port);
611 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
612 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
613 hostname, port_str, gai_strerror(gai_error));
617 for (ai = res; ai != NULL; ai = ai->ai_next) {
618 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
622 if (sock_connect_with_timeout
623 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
640 /* Open a connection using an external program. May be useful when
641 * you need to tunnel through a SOCKS or other firewall, or to
642 * establish an IMAP-over-SSH connection. */
643 /* TODO: Recreate this for sock_connect_thread() */
644 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
649 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
650 perror("socketpair");
653 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
658 dup(fd[1]); /* set onto stdin */
660 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
664 return sockinfo_from_fd(hostname, 0, fd[0]);
668 SockInfo *sock_connect(const gchar *hostname, gushort port)
677 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
681 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
682 g_warning("socket() failed: %ld\n", WSAGetLastError());
684 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
686 #endif /* G_OS_WIN32 */
690 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
691 if (errno != 0) perror("connect");
697 return sockinfo_from_fd(hostname, port, sock);
701 static void sock_address_list_free(GList *addr_list)
705 for (cur = addr_list; cur != NULL; cur = cur->next) {
706 SockAddrData *addr_data = (SockAddrData *)cur->data;
707 g_free(addr_data->addr);
711 g_list_free(addr_list);
714 /* asynchronous TCP connection */
716 static gboolean sock_connect_async_cb(GIOChannel *source,
717 GIOCondition condition, gpointer data)
719 SockConnectData *conn_data = (SockConnectData *)data;
725 if (conn_data->io_tag == 0 && conn_data->channel == NULL)
728 fd = g_io_channel_unix_get_fd(source);
730 conn_data->io_tag = 0;
731 conn_data->channel = NULL;
732 g_io_channel_unref(source);
735 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
736 perror("getsockopt");
738 sock_connect_address_list_async(conn_data);
744 sock_connect_address_list_async(conn_data);
748 sockinfo = g_new0(SockInfo, 1);
750 sockinfo->sock_ch = g_io_channel_unix_new(fd);
751 sockinfo->hostname = g_strdup(conn_data->hostname);
752 sockinfo->port = conn_data->port;
753 sockinfo->state = CONN_ESTABLISHED;
755 conn_data->func(sockinfo, conn_data->data);
757 sock_connect_async_cancel(conn_data->id);
762 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
765 SockConnectData *conn_data = (SockConnectData *)data;
767 conn_data->addr_list = addr_list;
768 conn_data->cur_addr = addr_list;
769 conn_data->lookup_data = NULL;
771 return sock_connect_address_list_async(conn_data);
774 gint sock_connect_async(const gchar *hostname, gushort port,
775 SockConnectFunc func, gpointer data)
778 SockConnectData *conn_data;
780 conn_data = g_new0(SockConnectData, 1);
781 conn_data->id = id++;
782 conn_data->hostname = g_strdup(hostname);
783 conn_data->port = port;
784 conn_data->addr_list = NULL;
785 conn_data->cur_addr = NULL;
786 conn_data->io_tag = 0;
787 conn_data->func = func;
788 conn_data->data = data;
790 conn_data->lookup_data = sock_get_address_info_async
791 (hostname, port, sock_connect_async_get_address_info_cb,
794 if (conn_data->lookup_data == NULL) {
795 g_free(conn_data->hostname);
800 sock_connect_data_list = g_list_append(sock_connect_data_list,
803 return conn_data->id;
806 gint sock_connect_async_cancel(gint id)
808 SockConnectData *conn_data = NULL;
811 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
812 if (((SockConnectData *)cur->data)->id == id) {
813 conn_data = (SockConnectData *)cur->data;
819 sock_connect_data_list = g_list_remove(sock_connect_data_list,
822 if (conn_data->lookup_data)
823 sock_get_address_info_async_cancel
824 (conn_data->lookup_data);
826 if (conn_data->io_tag > 0)
827 g_source_remove(conn_data->io_tag);
828 if (conn_data->channel) {
829 g_io_channel_close(conn_data->channel);
830 g_io_channel_unref(conn_data->channel);
833 sock_address_list_free(conn_data->addr_list);
834 g_free(conn_data->hostname);
837 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
844 static gint sock_connect_address_list_async(SockConnectData *conn_data)
846 SockAddrData *addr_data;
849 for (; conn_data->cur_addr != NULL;
850 conn_data->cur_addr = conn_data->cur_addr->next) {
851 addr_data = (SockAddrData *)conn_data->cur_addr->data;
853 if ((sock = socket(addr_data->family, addr_data->socktype,
854 addr_data->protocol)) < 0) {
859 set_nonblocking_mode(sock, TRUE);
861 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
862 if (EINPROGRESS == errno) {
872 if (conn_data->cur_addr == NULL) {
873 g_warning("sock_connect_address_list_async: "
874 "connection to %s:%d failed\n",
875 conn_data->hostname, conn_data->port);
876 conn_data->func(NULL, conn_data->data);
877 sock_connect_async_cancel(conn_data->id);
881 conn_data->cur_addr = conn_data->cur_addr->next;
883 conn_data->channel = g_io_channel_unix_new(sock);
884 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
885 sock_connect_async_cb, conn_data);
890 /* asynchronous DNS lookup */
892 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
893 GIOCondition condition,
896 SockLookupData *lookup_data = (SockLookupData *)data;
897 GList *addr_list = NULL;
898 SockAddrData *addr_data;
901 struct sockaddr *addr;
904 if (g_io_channel_read(source, (gchar *)ai_member,
905 sizeof(ai_member), &bytes_read)
906 != G_IO_ERROR_NONE) {
907 g_warning("sock_get_address_info_async_cb: "
908 "address length read error\n");
912 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
915 if (ai_member[0] == AF_UNSPEC) {
916 g_warning("DNS lookup failed\n");
920 addr = g_malloc(ai_member[3]);
921 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
923 != G_IO_ERROR_NONE) {
924 g_warning("sock_get_address_info_async_cb: "
925 "address data read error\n");
930 if (bytes_read != ai_member[3]) {
931 g_warning("sock_get_address_info_async_cb: "
932 "incomplete address data\n");
937 addr_data = g_new0(SockAddrData, 1);
938 addr_data->family = ai_member[0];
939 addr_data->socktype = ai_member[1];
940 addr_data->protocol = ai_member[2];
941 addr_data->addr_len = ai_member[3];
942 addr_data->addr = addr;
944 addr_list = g_list_append(addr_list, addr_data);
947 g_io_channel_close(source);
948 g_io_channel_unref(source);
950 kill(lookup_data->child_pid, SIGKILL);
951 waitpid(lookup_data->child_pid, NULL, 0);
953 lookup_data->func(addr_list, lookup_data->data);
955 g_free(lookup_data->hostname);
961 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
966 SockLookupData *lookup_data = NULL;
972 if (pipe(pipe_fds) < 0) {
978 if ((pid = fork()) < 0) {
988 struct addrinfo hints, *res, *ai;
993 struct sockaddr_in ad;
995 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
1000 memset(&hints, 0, sizeof(hints));
1001 /* hints.ai_flags = AI_CANONNAME; */
1002 hints.ai_family = AF_UNSPEC;
1003 hints.ai_socktype = SOCK_STREAM;
1004 hints.ai_protocol = IPPROTO_TCP;
1006 g_snprintf(port_str, sizeof(port_str), "%d", port);
1008 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
1010 g_warning("getaddrinfo for %s:%s failed: %s\n",
1011 hostname, port_str, gai_strerror(gai_err));
1012 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1018 for (ai = res; ai != NULL; ai = ai->ai_next) {
1019 ai_member[0] = ai->ai_family;
1020 ai_member[1] = ai->ai_socktype;
1021 ai_member[2] = ai->ai_protocol;
1022 ai_member[3] = ai->ai_addrlen;
1024 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1026 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
1033 hp = my_gethostbyname(hostname);
1034 if (hp == NULL || hp->h_addrtype != AF_INET) {
1035 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1041 ai_member[0] = AF_INET;
1042 ai_member[1] = SOCK_STREAM;
1043 ai_member[2] = IPPROTO_TCP;
1044 ai_member[3] = sizeof(ad);
1046 memset(&ad, 0, sizeof(ad));
1047 ad.sin_family = AF_INET;
1048 ad.sin_port = htons(port);
1050 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
1052 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
1053 fd_write_all(pipe_fds[1], (gchar *)ai_member,
1055 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
1065 lookup_data = g_new0(SockLookupData, 1);
1066 lookup_data->hostname = g_strdup(hostname);
1067 lookup_data->child_pid = pid;
1068 lookup_data->func = func;
1069 lookup_data->data = data;
1071 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
1072 lookup_data->io_tag = g_io_add_watch
1073 (lookup_data->channel, G_IO_IN,
1074 sock_get_address_info_async_cb, lookup_data);
1080 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
1082 if (lookup_data->io_tag > 0)
1083 g_source_remove(lookup_data->io_tag);
1084 if (lookup_data->channel) {
1085 g_io_channel_close(lookup_data->channel);
1086 g_io_channel_unref(lookup_data->channel);
1089 if (lookup_data->child_pid > 0) {
1090 kill(lookup_data->child_pid, SIGKILL);
1091 waitpid(lookup_data->child_pid, NULL, 0);
1094 g_free(lookup_data->hostname);
1095 g_free(lookup_data);
1099 #endif /* G_OS_UNIX */
1102 static SockInfo *sockinfo_from_fd(const gchar *hostname,
1108 sockinfo = g_new0(SockInfo, 1);
1109 sockinfo->sock = sock;
1110 sockinfo->sock_ch = g_io_channel_unix_new(sock);
1111 sockinfo->hostname = g_strdup(hostname);
1112 sockinfo->port = port;
1113 sockinfo->state = CONN_ESTABLISHED;
1118 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1121 gchar buf[BUFFSIZE];
1123 va_start(args, format);
1124 g_vsnprintf(buf, sizeof(buf), format, args);
1127 return sock_write_all(sock, buf, strlen(buf));
1130 gint fd_read(gint fd, gchar *buf, gint len)
1132 if (fd_check_io(fd, G_IO_IN) < 0)
1136 return recv(fd, buf, len, 0);
1138 return read(fd, buf, len);
1143 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1147 if (SSL_pending(ssl) == 0) {
1148 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1152 ret = SSL_read(ssl, buf, len);
1154 switch ((err = SSL_get_error(ssl, ret))) {
1155 case SSL_ERROR_NONE:
1157 case SSL_ERROR_WANT_READ:
1158 case SSL_ERROR_WANT_WRITE:
1161 case SSL_ERROR_ZERO_RETURN:
1164 g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
1172 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1176 g_return_val_if_fail(sock != NULL, -1);
1180 ret = ssl_read(sock->ssl, buf, len);
1183 ret = fd_read(sock->sock, buf, len);
1186 sock->state = CONN_DISCONNECTED;
1190 gint fd_write(gint fd, const gchar *buf, gint len)
1192 if (fd_check_io(fd, G_IO_OUT) < 0)
1196 return send(fd, buf, len, 0);
1198 return write(fd, buf, len);
1203 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1207 ret = SSL_write(ssl, buf, len);
1209 switch (SSL_get_error(ssl, ret)) {
1210 case SSL_ERROR_NONE:
1212 case SSL_ERROR_WANT_READ:
1213 case SSL_ERROR_WANT_WRITE:
1222 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1226 g_return_val_if_fail(sock != NULL, -1);
1230 ret = ssl_write(sock->ssl, buf, len);
1233 ret = fd_write(sock->sock, buf, len);
1236 sock->state = CONN_DISCONNECTED;
1240 gint fd_write_all(gint fd, const gchar *buf, gint len)
1245 if (fd_check_io(fd, G_IO_OUT) < 0)
1247 signal(SIGPIPE, SIG_IGN);
1248 n = write(fd, buf, len);
1250 log_error("write on fd%d: %s\n", fd, strerror(errno));
1262 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1267 n = ssl_write(ssl, buf, len);
1279 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1283 g_return_val_if_fail(sock != NULL, -1);
1287 ret = ssl_write_all(sock->ssl, buf, len);
1290 ret = fd_write_all(sock->sock, buf, len);
1293 sock->state = CONN_DISCONNECTED;
1297 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1299 if (fd_check_io(fd, G_IO_IN) < 0)
1302 return recv(fd, buf, len, flags);
1305 gint fd_gets(gint fd, gchar *buf, gint len)
1307 gchar *newline, *bp = buf;
1313 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1315 if ((newline = memchr(bp, '\n', n)) != NULL)
1316 n = newline - bp + 1;
1317 if ((n = fd_read(fd, bp, n)) < 0)
1321 } while (!newline && len);
1328 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1330 gchar *newline, *bp = buf;
1336 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1338 if ((newline = memchr(bp, '\n', n)) != NULL)
1339 n = newline - bp + 1;
1340 if ((n = ssl_read(ssl, bp, n)) < 0)
1344 } while (!newline && len);
1351 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1355 g_return_val_if_fail(sock != NULL, -1);
1359 return ssl_gets(sock->ssl, buf, len);
1362 return fd_gets(sock->sock, buf, len);
1365 sock->state = CONN_DISCONNECTED;
1369 gint fd_getline(gint fd, gchar **str)
1371 gchar buf[BUFFSIZE];
1375 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1378 *str = g_strdup(buf);
1380 *str = g_realloc(*str, size);
1383 if (buf[len - 1] == '\n')
1386 if (len == -1 && *str)
1393 gint ssl_getline(SSL *ssl, gchar **str)
1395 gchar buf[BUFFSIZE];
1399 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1402 *str = g_strdup(buf);
1404 *str = g_realloc(*str, size);
1407 if (buf[len - 1] == '\n')
1410 if (len == -1 && *str)
1417 gchar *sock_getline(SockInfo *sock)
1422 g_return_val_if_fail(sock != NULL, NULL);
1426 ret = ssl_getline(sock->ssl, &str);
1429 ret = fd_getline(sock->sock, &str);
1432 sock->state = CONN_DISCONNECTED;
1436 gint sock_puts(SockInfo *sock, const gchar *buf)
1440 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1442 return sock_write_all(sock, "\r\n", 2);
1445 /* peek at the socket data without actually reading it */
1447 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1451 if (SSL_pending(ssl) == 0) {
1452 if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
1456 ret = SSL_peek(ssl, buf, len);
1458 switch ((err = SSL_get_error(ssl, ret))) {
1459 case SSL_ERROR_NONE:
1461 case SSL_ERROR_WANT_READ:
1462 case SSL_ERROR_WANT_WRITE:
1465 case SSL_ERROR_ZERO_RETURN:
1467 case SSL_ERROR_SYSCALL:
1468 g_warning("SSL_peek() returned syscall error. errno=%d\n", errno);
1471 g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
1479 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1481 g_return_val_if_fail(sock != NULL, -1);
1485 return ssl_peek(sock->ssl, buf, len);
1487 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1490 gint sock_close(SockInfo *sock)
1498 g_io_channel_unref(sock->sock_ch);
1502 ssl_done_socket(sock);
1503 if (sock->g_source != 0)
1504 g_source_remove(sock->g_source);
1507 ret = fd_close(sock->sock);
1508 g_free(sock->hostname);
1514 gint fd_close(gint fd)