2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
42 # include <sys/select.h>
53 #error USE_GIO is currently not supported
58 typedef gint (*SockAddrFunc) (GList *addr_list,
61 typedef struct _SockConnectData SockConnectData;
62 typedef struct _SockLookupData SockLookupData;
63 typedef struct _SockAddrData SockAddrData;
65 struct _SockConnectData {
71 SockLookupData *lookup_data;
78 struct _SockLookupData {
87 struct _SockAddrData {
92 struct sockaddr *addr;
95 static guint io_timeout = 60;
97 static GList *sock_connect_data_list = NULL;
99 static gboolean sock_prepare (gpointer source_data,
100 GTimeVal *current_time,
103 static gboolean sock_check (gpointer source_data,
104 GTimeVal *current_time,
106 static gboolean sock_dispatch (gpointer source_data,
107 GTimeVal *current_time,
110 GSourceFuncs sock_watch_funcs = {
117 static gint sock_connect_with_timeout (gint sock,
118 const struct sockaddr *serv_addr,
123 static gint sock_connect_by_hostname (gint sock,
124 const gchar *hostname,
127 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
131 static SockInfo *sockinfo_from_fd(const gchar *hostname,
134 static void sock_address_list_free (GList *addr_list);
136 static gboolean sock_connect_async_cb (GIOChannel *source,
137 GIOCondition condition,
139 static gint sock_connect_async_get_address_info_cb
143 static gint sock_connect_address_list_async (SockConnectData *conn_data);
145 static gboolean sock_get_address_info_async_cb (GIOChannel *source,
146 GIOCondition condition,
148 static SockLookupData *sock_get_address_info_async
149 (const gchar *hostname,
153 static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
156 gint sock_set_io_timeout(guint sec)
162 gint fd_connect_unix(const gchar *path)
165 struct sockaddr_un addr;
167 sock = socket(PF_UNIX, SOCK_STREAM, 0);
169 perror("sock_connect_unix(): socket");
173 memset(&addr, 0, sizeof(addr));
174 addr.sun_family = AF_UNIX;
175 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
177 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
185 gint fd_open_unix(const gchar *path)
188 struct sockaddr_un addr;
190 sock = socket(PF_UNIX, SOCK_STREAM, 0);
193 perror("sock_open_unix(): socket");
197 memset(&addr, 0, sizeof(addr));
198 addr.sun_family = AF_UNIX;
199 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
201 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
207 if (listen(sock, 1) < 0) {
216 gint fd_accept(gint sock)
218 struct sockaddr_in caddr;
221 caddr_len = sizeof(caddr);
222 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
226 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
230 flags = fcntl(fd, F_GETFL, 0);
239 flags &= ~O_NONBLOCK;
241 return fcntl(fd, F_SETFL, flags);
244 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
246 g_return_val_if_fail(sock != NULL, -1);
248 return set_nonblocking_mode(sock->sock, nonblock);
251 static gboolean is_nonblocking_mode(gint fd)
255 flags = fcntl(fd, F_GETFL, 0);
261 return ((flags & O_NONBLOCK) != 0);
264 gboolean sock_is_nonblocking_mode(SockInfo *sock)
266 g_return_val_if_fail(sock != NULL, FALSE);
268 return is_nonblocking_mode(sock->sock);
272 static gboolean sock_prepare(gpointer source_data, GTimeVal *current_time,
273 gint *timeout, gpointer data)
279 static gboolean sock_check(gpointer source_data, GTimeVal *current_time,
282 SockInfo *sock = (SockInfo *)source_data;
283 struct timeval timeout = {0, 0};
285 GIOCondition condition = sock->condition;
289 if (condition & G_IO_IN) {
290 if (SSL_pending(sock->ssl) > 0)
292 if (SSL_want_write(sock->ssl))
293 condition |= G_IO_OUT;
296 if (condition & G_IO_OUT) {
297 if (SSL_want_read(sock->ssl))
298 condition |= G_IO_IN;
304 FD_SET(sock->sock, &fds);
306 select(sock->sock + 1,
307 (condition & G_IO_IN) ? &fds : NULL,
308 (condition & G_IO_OUT) ? &fds : NULL,
311 return FD_ISSET(sock->sock, &fds) != 0;
314 static gboolean sock_dispatch(gpointer source_data, GTimeVal *current_time,
317 SockInfo *sock = (SockInfo *)source_data;
319 return sock->callback(sock, sock->condition, user_data);
322 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
325 SockInfo *sock = (SockInfo *)data;
327 return sock->callback(sock, condition, sock->data);
330 guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
333 sock->callback = func;
334 sock->condition = condition;
339 return g_source_add(G_PRIORITY_DEFAULT, FALSE,
340 &sock_watch_funcs, sock, data, NULL);
343 return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
346 static gint fd_check_io(gint fd, GIOCondition cond)
348 struct timeval timeout;
351 if (is_nonblocking_mode(fd))
354 timeout.tv_sec = io_timeout;
360 if (cond == G_IO_IN) {
361 select(fd + 1, &fds, NULL, NULL,
362 io_timeout > 0 ? &timeout : NULL);
364 select(fd + 1, NULL, &fds, NULL,
365 io_timeout > 0 ? &timeout : NULL);
368 if (FD_ISSET(fd, &fds)) {
371 g_warning("Socket IO timeout\n");
376 static sigjmp_buf jmpenv;
378 static void timeout_handler(gint sig)
380 siglongjmp(jmpenv, 1);
383 static gint sock_connect_with_timeout(gint sock,
384 const struct sockaddr *serv_addr,
389 void (*prev_handler)(gint);
392 prev_handler = signal(SIGALRM, timeout_handler);
393 if (sigsetjmp(jmpenv, 1)) {
395 signal(SIGALRM, prev_handler);
401 ret = connect(sock, serv_addr, addrlen);
404 signal(SIGALRM, prev_handler);
409 struct hostent *my_gethostbyname(const gchar *hostname)
412 void (*prev_handler)(gint);
415 prev_handler = signal(SIGALRM, timeout_handler);
416 if (sigsetjmp(jmpenv, 1)) {
418 signal(SIGALRM, prev_handler);
419 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
425 if ((hp = gethostbyname(hostname)) == NULL) {
427 signal(SIGALRM, prev_handler);
428 fprintf(stderr, "%s: unknown host.\n", hostname);
434 signal(SIGALRM, prev_handler);
440 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
443 return inet_aton(hostname, inp);
448 inaddr = inet_addr(hostname);
450 memcpy(inp, &inaddr, sizeof(inaddr));
457 #endif /* HAVE_INET_ATON */
460 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
464 struct sockaddr_in ad;
466 memset(&ad, 0, sizeof(ad));
467 ad.sin_family = AF_INET;
468 ad.sin_port = htons(port);
470 if (!my_inet_aton(hostname, &ad.sin_addr)) {
471 if ((hp = my_gethostbyname(hostname)) == NULL) {
472 fprintf(stderr, "%s: unknown host.\n", hostname);
477 if (hp->h_length != 4 && hp->h_length != 8) {
478 fprintf(stderr, "illegal address length received for host %s\n", hostname);
483 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
486 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
487 sizeof(ad), io_timeout);
491 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
493 gint sock = -1, gai_error;
494 struct addrinfo hints, *res, *ai;
497 memset(&hints, 0, sizeof(hints));
498 /* hints.ai_flags = AI_CANONNAME; */
499 hints.ai_family = AF_UNSPEC;
500 hints.ai_socktype = SOCK_STREAM;
501 hints.ai_protocol = IPPROTO_TCP;
503 /* convert port from integer to string. */
504 g_snprintf(port_str, sizeof(port_str), "%d", port);
506 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
507 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
508 hostname, port_str, gai_strerror(gai_error));
512 for (ai = res; ai != NULL; ai = ai->ai_next) {
513 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
517 if (sock_connect_with_timeout
518 (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
535 /* Open a connection using an external program. May be useful when
536 * you need to tunnel through a SOCKS or other firewall, or to
537 * establish an IMAP-over-SSH connection. */
538 /* TODO: Recreate this for sock_connect_thread() */
539 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
544 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
545 perror("socketpair");
548 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
553 dup(fd[1]); /* set onto stdin */
555 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
559 return sockinfo_from_fd(hostname, 0, fd[0]);
563 SockInfo *sock_connect(const gchar *hostname, gushort port)
568 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
571 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
576 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
577 if (errno != 0) perror("connect");
583 return sockinfo_from_fd(hostname, port, sock);
586 static void sock_address_list_free(GList *addr_list)
590 for (cur = addr_list; cur != NULL; cur = cur->next) {
591 SockAddrData *addr_data = (SockAddrData *)cur->data;
592 g_free(addr_data->addr);
596 g_list_free(addr_list);
599 /* asynchronous TCP connection */
601 static gboolean sock_connect_async_cb(GIOChannel *source,
602 GIOCondition condition, gpointer data)
604 SockConnectData *conn_data = (SockConnectData *)data;
610 fd = g_io_channel_unix_get_fd(source);
612 conn_data->io_tag = 0;
613 conn_data->channel = NULL;
614 g_io_channel_unref(source);
617 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
618 perror("getsockopt");
620 sock_connect_address_list_async(conn_data);
626 sock_connect_address_list_async(conn_data);
630 sockinfo = g_new0(SockInfo, 1);
632 sockinfo->sock_ch = g_io_channel_unix_new(fd);
633 sockinfo->hostname = g_strdup(conn_data->hostname);
634 sockinfo->port = conn_data->port;
635 sockinfo->state = CONN_ESTABLISHED;
637 conn_data->func(sockinfo, conn_data->data);
639 sock_connect_async_cancel(conn_data->id);
644 static gint sock_connect_async_get_address_info_cb(GList *addr_list,
647 SockConnectData *conn_data = (SockConnectData *)data;
649 conn_data->addr_list = addr_list;
650 conn_data->cur_addr = addr_list;
651 conn_data->lookup_data = NULL;
653 return sock_connect_address_list_async(conn_data);
656 gint sock_connect_async(const gchar *hostname, gushort port,
657 SockConnectFunc func, gpointer data)
660 SockConnectData *conn_data;
662 conn_data = g_new0(SockConnectData, 1);
663 conn_data->id = id++;
664 conn_data->hostname = g_strdup(hostname);
665 conn_data->port = port;
666 conn_data->addr_list = NULL;
667 conn_data->cur_addr = NULL;
668 conn_data->io_tag = 0;
669 conn_data->func = func;
670 conn_data->data = data;
672 conn_data->lookup_data = sock_get_address_info_async
673 (hostname, port, sock_connect_async_get_address_info_cb,
676 if (conn_data->lookup_data == NULL) {
677 g_free(conn_data->hostname);
682 sock_connect_data_list = g_list_append(sock_connect_data_list,
685 return conn_data->id;
688 gint sock_connect_async_cancel(gint id)
690 SockConnectData *conn_data = NULL;
693 for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
694 if (((SockConnectData *)cur->data)->id == id) {
695 conn_data = (SockConnectData *)cur->data;
701 sock_connect_data_list = g_list_remove(sock_connect_data_list,
704 if (conn_data->lookup_data)
705 sock_get_address_info_async_cancel
706 (conn_data->lookup_data);
708 if (conn_data->io_tag > 0)
709 g_source_remove(conn_data->io_tag);
710 if (conn_data->channel) {
711 g_io_channel_close(conn_data->channel);
712 g_io_channel_unref(conn_data->channel);
715 sock_address_list_free(conn_data->addr_list);
716 g_free(conn_data->hostname);
719 g_warning("sock_connect_async_cancel: id %d not found.\n", id);
726 static gint sock_connect_address_list_async(SockConnectData *conn_data)
728 SockAddrData *addr_data;
731 for (; conn_data->cur_addr != NULL;
732 conn_data->cur_addr = conn_data->cur_addr->next) {
733 addr_data = (SockAddrData *)conn_data->cur_addr->data;
735 if ((sock = socket(addr_data->family, addr_data->socktype,
736 addr_data->protocol)) < 0) {
741 set_nonblocking_mode(sock, TRUE);
743 if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
744 if (EINPROGRESS == errno) {
754 if (conn_data->cur_addr == NULL) {
755 g_warning("sock_connect_address_list_async: "
756 "connection to %s:%d failed\n",
757 conn_data->hostname, conn_data->port);
758 conn_data->func(NULL, conn_data->data);
759 sock_connect_async_cancel(conn_data->id);
763 conn_data->cur_addr = conn_data->cur_addr->next;
765 conn_data->channel = g_io_channel_unix_new(sock);
766 conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
767 sock_connect_async_cb, conn_data);
772 /* asynchronous DNS lookup */
774 static gboolean sock_get_address_info_async_cb(GIOChannel *source,
775 GIOCondition condition,
778 SockLookupData *lookup_data = (SockLookupData *)data;
779 GList *addr_list = NULL;
780 SockAddrData *addr_data;
783 struct sockaddr *addr;
786 if (g_io_channel_read(source, (gchar *)ai_member,
787 sizeof(ai_member), &bytes_read)
788 != G_IO_ERROR_NONE) {
789 g_warning("sock_get_address_info_async_cb: "
790 "address length read error\n");
794 if (bytes_read == 0 || bytes_read != sizeof(ai_member))
797 if (ai_member[0] == AF_UNSPEC) {
798 g_warning("DNS lookup failed\n");
802 addr = g_malloc(ai_member[3]);
803 if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
805 != G_IO_ERROR_NONE) {
806 g_warning("sock_get_address_info_async_cb: "
807 "address data read error\n");
812 if (bytes_read != ai_member[3]) {
813 g_warning("sock_get_address_info_async_cb: "
814 "incomplete address data\n");
819 addr_data = g_new0(SockAddrData, 1);
820 addr_data->family = ai_member[0];
821 addr_data->socktype = ai_member[1];
822 addr_data->protocol = ai_member[2];
823 addr_data->addr_len = ai_member[3];
824 addr_data->addr = addr;
826 addr_list = g_list_append(addr_list, addr_data);
829 g_io_channel_close(source);
830 g_io_channel_unref(source);
832 kill(lookup_data->child_pid, SIGKILL);
833 waitpid(lookup_data->child_pid, NULL, 0);
835 lookup_data->func(addr_list, lookup_data->data);
837 g_free(lookup_data->hostname);
843 static SockLookupData *sock_get_address_info_async(const gchar *hostname,
848 SockLookupData *lookup_data = NULL;
852 if (pipe(pipe_fds) < 0) {
858 if ((pid = fork()) < 0) {
868 struct addrinfo hints, *res, *ai;
873 struct sockaddr_in ad;
875 gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
880 memset(&hints, 0, sizeof(hints));
881 /* hints.ai_flags = AI_CANONNAME; */
882 hints.ai_family = AF_UNSPEC;
883 hints.ai_socktype = SOCK_STREAM;
884 hints.ai_protocol = IPPROTO_TCP;
886 g_snprintf(port_str, sizeof(port_str), "%d", port);
888 gai_err = getaddrinfo(hostname, port_str, &hints, &res);
890 g_warning("getaddrinfo for %s:%s failed: %s\n",
891 hostname, port_str, gai_strerror(gai_err));
892 fd_write_all(pipe_fds[1], (gchar *)ai_member,
898 for (ai = res; ai != NULL; ai = ai->ai_next) {
899 ai_member[0] = ai->ai_family;
900 ai_member[1] = ai->ai_socktype;
901 ai_member[2] = ai->ai_protocol;
902 ai_member[3] = ai->ai_addrlen;
904 fd_write_all(pipe_fds[1], (gchar *)ai_member,
906 fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
913 hp = my_gethostbyname(hostname);
914 if (hp == NULL || hp->h_addrtype != AF_INET) {
915 fd_write_all(pipe_fds[1], (gchar *)ai_member,
921 ai_member[0] = AF_INET;
922 ai_member[1] = SOCK_STREAM;
923 ai_member[2] = IPPROTO_TCP;
924 ai_member[3] = sizeof(ad);
926 memset(&ad, 0, sizeof(ad));
927 ad.sin_family = AF_INET;
928 ad.sin_port = htons(port);
930 for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
932 memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
933 fd_write_all(pipe_fds[1], (gchar *)ai_member,
935 fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
945 lookup_data = g_new0(SockLookupData, 1);
946 lookup_data->hostname = g_strdup(hostname);
947 lookup_data->child_pid = pid;
948 lookup_data->func = func;
949 lookup_data->data = data;
951 lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
952 lookup_data->io_tag = g_io_add_watch
953 (lookup_data->channel, G_IO_IN,
954 sock_get_address_info_async_cb, lookup_data);
960 static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
962 if (lookup_data->io_tag > 0)
963 g_source_remove(lookup_data->io_tag);
964 if (lookup_data->channel) {
965 g_io_channel_close(lookup_data->channel);
966 g_io_channel_unref(lookup_data->channel);
969 if (lookup_data->child_pid > 0) {
970 kill(lookup_data->child_pid, SIGKILL);
971 waitpid(lookup_data->child_pid, NULL, 0);
974 g_free(lookup_data->hostname);
981 static SockInfo *sockinfo_from_fd(const gchar *hostname,
987 sockinfo = g_new0(SockInfo, 1);
988 sockinfo->sock = sock;
989 sockinfo->sock_ch = g_io_channel_unix_new(sock);
990 sockinfo->hostname = g_strdup(hostname);
991 sockinfo->port = port;
992 sockinfo->state = CONN_ESTABLISHED;
999 gint sock_printf(SockInfo *sock, const gchar *format, ...)
1002 gchar buf[BUFFSIZE];
1004 va_start(args, format);
1005 g_vsnprintf(buf, sizeof(buf), format, args);
1008 return sock_write_all(sock, buf, strlen(buf));
1011 gint fd_read(gint fd, gchar *buf, gint len)
1013 if (fd_check_io(fd, G_IO_IN) < 0)
1016 return read(fd, buf, len);
1020 gint ssl_read(SSL *ssl, gchar *buf, gint len)
1024 ret = SSL_read(ssl, buf, len);
1026 switch (SSL_get_error(ssl, ret)) {
1027 case SSL_ERROR_NONE:
1029 case SSL_ERROR_WANT_READ:
1030 case SSL_ERROR_WANT_WRITE:
1039 gint sock_read(SockInfo *sock, gchar *buf, gint len)
1043 g_return_val_if_fail(sock != NULL, -1);
1047 ret = ssl_read(sock->ssl, buf, len);
1050 ret = fd_read(sock->sock, buf, len);
1053 sock->state = CONN_DISCONNECTED;
1057 gint fd_write(gint fd, const gchar *buf, gint len)
1059 if (fd_check_io(fd, G_IO_OUT) < 0)
1062 return write(fd, buf, len);
1066 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
1070 ret = SSL_write(ssl, buf, len);
1072 switch (SSL_get_error(ssl, ret)) {
1073 case SSL_ERROR_NONE:
1075 case SSL_ERROR_WANT_READ:
1076 case SSL_ERROR_WANT_WRITE:
1085 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
1089 g_return_val_if_fail(sock != NULL, -1);
1093 ret = ssl_write(sock->ssl, buf, len);
1096 ret = fd_write(sock->sock, buf, len);
1099 sock->state = CONN_DISCONNECTED;
1103 gint fd_write_all(gint fd, const gchar *buf, gint len)
1108 if (fd_check_io(fd, G_IO_OUT) < 0)
1110 signal(SIGPIPE, SIG_IGN);
1111 n = write(fd, buf, len);
1113 log_error("write on fd%d: %s\n", fd, strerror(errno));
1125 gint ssl_write_all(SSL *ssl, const gchar *buf, gint len)
1130 n = ssl_write(ssl, buf, len);
1142 gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
1146 g_return_val_if_fail(sock != NULL, -1);
1150 ret = ssl_write_all(sock->ssl, buf, len);
1153 ret = fd_write_all(sock->sock, buf, len);
1156 sock->state = CONN_DISCONNECTED;
1160 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
1162 if (fd_check_io(fd, G_IO_IN) < 0)
1165 return recv(fd, buf, len, flags);
1168 gint fd_gets(gint fd, gchar *buf, gint len)
1170 gchar *newline, *bp = buf;
1176 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
1178 if ((newline = memchr(bp, '\n', n)) != NULL)
1179 n = newline - bp + 1;
1180 if ((n = fd_read(fd, bp, n)) < 0)
1184 } while (!newline && len);
1191 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
1193 gchar *newline, *bp = buf;
1199 if ((n = ssl_peek(ssl, bp, len)) <= 0)
1201 if ((newline = memchr(bp, '\n', n)) != NULL)
1202 n = newline - bp + 1;
1203 if ((n = ssl_read(ssl, bp, n)) < 0)
1207 } while (!newline && len);
1214 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
1218 g_return_val_if_fail(sock != NULL, -1);
1222 return ssl_gets(sock->ssl, buf, len);
1225 return fd_gets(sock->sock, buf, len);
1228 sock->state = CONN_DISCONNECTED;
1232 gint fd_getline(gint fd, gchar **str)
1234 gchar buf[BUFFSIZE];
1238 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
1241 *str = g_strdup(buf);
1243 *str = g_realloc(*str, size);
1246 if (buf[len - 1] == '\n')
1249 if (len == -1 && *str)
1256 gint ssl_getline(SSL *ssl, gchar **str)
1258 gchar buf[BUFFSIZE];
1262 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
1265 *str = g_strdup(buf);
1267 *str = g_realloc(*str, size);
1270 if (buf[len - 1] == '\n')
1273 if (len == -1 && *str)
1280 gchar *sock_getline(SockInfo *sock)
1285 g_return_val_if_fail(sock != NULL, NULL);
1289 ret = ssl_getline(sock->ssl, &str);
1292 ret = fd_getline(sock->sock, &str);
1295 sock->state = CONN_DISCONNECTED;
1299 gint sock_puts(SockInfo *sock, const gchar *buf)
1303 if ((ret = sock_write_all(sock, buf, strlen(buf))) < 0)
1305 return sock_write_all(sock, "\r\n", 2);
1308 /* peek at the socket data without actually reading it */
1310 gint ssl_peek(SSL *ssl, gchar *buf, gint len)
1314 ret = SSL_peek(ssl, buf, len);
1316 switch (SSL_get_error(ssl, ret)) {
1317 case SSL_ERROR_NONE:
1319 case SSL_ERROR_WANT_READ:
1320 case SSL_ERROR_WANT_WRITE:
1329 gint sock_peek(SockInfo *sock, gchar *buf, gint len)
1331 g_return_val_if_fail(sock != NULL, -1);
1335 return ssl_peek(sock->ssl, buf, len);
1337 return fd_recv(sock->sock, buf, len, MSG_PEEK);
1340 gint sock_close(SockInfo *sock)
1348 g_io_channel_unref(sock->sock_ch);
1352 ssl_done_socket(sock);
1354 ret = fd_close(sock->sock);
1355 g_free(sock->hostname);
1361 gint fd_close(gint fd)