2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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>
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
41 # include <sys/select.h>
52 #error USE_GIO is currently not supported
58 static gint sock_connect_with_timeout (gint sock,
59 const struct sockaddr *serv_addr,
64 static gint sock_connect_by_hostname (gint sock,
65 const gchar *hostname,
68 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
72 static SockInfo *sockinfo_from_fd(const gchar *hostname,
76 gint fd_connect_unix(const gchar *path)
79 struct sockaddr_un addr;
81 sock = socket(PF_UNIX, SOCK_STREAM, 0);
83 perror("sock_connect_unix(): socket");
87 memset(&addr, 0, sizeof(addr));
88 addr.sun_family = AF_UNIX;
89 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
91 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
99 gint fd_open_unix(const gchar *path)
102 struct sockaddr_un addr;
104 sock = socket(PF_UNIX, SOCK_STREAM, 0);
107 perror("sock_open_unix(): socket");
111 memset(&addr, 0, sizeof(addr));
112 addr.sun_family = AF_UNIX;
113 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
115 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
121 if (listen(sock, 1) < 0) {
130 gint fd_accept(gint sock)
132 struct sockaddr_in caddr;
135 caddr_len = sizeof(caddr);
136 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
140 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
144 flags = fcntl(fd, F_GETFL, 0);
153 flags &= ~O_NONBLOCK;
155 return fcntl(fd, F_SETFL, flags);
158 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
160 g_return_val_if_fail(sock != NULL, -1);
162 return set_nonblocking_mode(sock->sock, nonblock);
166 static gboolean is_nonblocking_mode(gint fd)
170 flags = fcntl(fd, F_GETFL, 0);
176 return ((flags & O_NONBLOCK) != 0);
179 gboolean sock_is_nonblocking_mode(SockInfo *sock)
181 g_return_val_if_fail(sock != NULL, FALSE);
183 return is_nonblocking_mode(sock->sock);
186 static gint fd_check_io(gint fd, GIOCondition cond)
188 struct timeval timeout;
191 timeout.tv_sec = IO_TIMEOUT;
197 if (cond == G_IO_IN) {
198 select(fd + 1, &fds, NULL, NULL, &timeout);
200 select(fd + 1, NULL, &fds, NULL, &timeout);
203 if (FD_ISSET(fd, &fds)) {
206 g_warning("Socket IO timeout\n");
211 static sigjmp_buf jmpenv;
213 static void timeout_handler(gint sig)
215 siglongjmp(jmpenv, 1);
218 static gint sock_connect_with_timeout(gint sock,
219 const struct sockaddr *serv_addr,
224 void (*prev_handler)(gint);
227 prev_handler = signal(SIGALRM, timeout_handler);
228 if (sigsetjmp(jmpenv, 1)) {
230 signal(SIGALRM, prev_handler);
236 ret = connect(sock, serv_addr, addrlen);
239 signal(SIGALRM, prev_handler);
244 struct hostent *my_gethostbyname(const gchar *hostname)
247 void (*prev_handler)(gint);
248 guint timeout_secs = IO_TIMEOUT;
251 prev_handler = signal(SIGALRM, timeout_handler);
252 if (sigsetjmp(jmpenv, 1)) {
254 signal(SIGALRM, prev_handler);
255 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
261 if ((hp = gethostbyname(hostname)) == NULL) {
263 signal(SIGALRM, prev_handler);
264 fprintf(stderr, "%s: unknown host.\n", hostname);
270 signal(SIGALRM, prev_handler);
276 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
279 return inet_aton(hostname, inp);
284 inaddr = inet_addr(hostname);
286 memcpy(inp, &inaddr, sizeof(inaddr));
293 #endif /* HAVE_INET_ATON */
296 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
300 struct sockaddr_in ad;
301 guint timeout_secs = IO_TIMEOUT;
303 memset(&ad, 0, sizeof(ad));
304 ad.sin_family = AF_INET;
305 ad.sin_port = htons(port);
307 if (!my_inet_aton(hostname, &ad.sin_addr)) {
308 if ((hp = my_gethostbyname(hostname)) == NULL) {
309 fprintf(stderr, "%s: unknown host.\n", hostname);
314 if (hp->h_length != 4 && hp->h_length != 8) {
315 fprintf(stderr, "illegal address length received for host %s\n", hostname);
320 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
323 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
324 sizeof(ad), timeout_secs);
328 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
330 gint sock = -1, gai_error;
331 struct addrinfo hints, *res, *ai;
332 guint timeout_secs = IO_TIMEOUT;
335 memset(&hints, 0, sizeof(hints));
336 /* hints.ai_flags = AI_CANONNAME; */
337 hints.ai_family = AF_UNSPEC;
338 hints.ai_socktype = SOCK_STREAM;
339 hints.ai_protocol = IPPROTO_TCP;
341 /* convert port from integer to string. */
342 g_snprintf(port_str, sizeof(port_str), "%d", port);
344 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
345 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
346 hostname, port_str, gai_strerror(gai_error));
350 for (ai = res; ai != NULL; ai = ai->ai_next) {
351 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
355 if (sock_connect_with_timeout
356 (sock, ai->ai_addr, ai->ai_addrlen, timeout_secs) == 0)
373 /* Open a connection using an external program. May be useful when
374 * you need to tunnel through a SOCKS or other firewall, or to
375 * establish an IMAP-over-SSH connection. */
376 /* TODO: Recreate this for sock_connect_thread() */
377 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
382 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
383 perror("socketpair");
386 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
391 dup(fd[1]); /* set onto stdin */
393 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
397 return sockinfo_from_fd(hostname, 0, fd[0]);
401 SockInfo *sock_connect(const gchar *hostname, gushort port)
406 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
409 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
414 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
415 if (errno != 0) perror("connect");
421 return sockinfo_from_fd(hostname, port, sock);
425 static SockInfo *sockinfo_from_fd(const gchar *hostname,
431 sockinfo = g_new0(SockInfo, 1);
432 sockinfo->sock = sock;
433 sockinfo->hostname = g_strdup(hostname);
434 sockinfo->port = port;
435 sockinfo->state = CONN_ESTABLISHED;
442 gint sock_printf(SockInfo *sock, const gchar *format, ...)
447 va_start(args, format);
448 g_vsnprintf(buf, sizeof(buf), format, args);
451 return sock_write(sock, buf, strlen(buf));
454 gint sock_read(SockInfo *sock, gchar *buf, gint len)
456 g_return_val_if_fail(sock != NULL, -1);
460 return ssl_read(sock->ssl, buf, len);
462 return fd_read(sock->sock, buf, len);
465 gint fd_read(gint fd, gchar *buf, gint len)
467 if (fd_check_io(fd, G_IO_IN) < 0)
470 return read(fd, buf, len);
474 gint ssl_read(SSL *ssl, gchar *buf, gint len)
476 return SSL_read(ssl, buf, len);
480 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
482 g_return_val_if_fail(sock != NULL, -1);
486 return ssl_write(sock->ssl, buf, len);
488 return fd_write(sock->sock, buf, len);
491 gint fd_write(gint fd, const gchar *buf, gint len)
496 if (fd_check_io(fd, G_IO_OUT) < 0)
498 signal(SIGPIPE, SIG_IGN);
499 n = write(fd, buf, len);
501 log_error("write on fd%d: %s\n", fd, strerror(errno));
513 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
518 n = SSL_write(ssl, buf, len);
530 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
532 if (fd_check_io(fd, G_IO_IN) < 0)
535 return recv(fd, buf, len, flags);
538 gint fd_gets(gint fd, gchar *buf, gint len)
540 gchar *newline, *bp = buf;
546 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
548 if ((newline = memchr(bp, '\n', n)) != NULL)
549 n = newline - bp + 1;
550 if ((n = fd_read(fd, bp, n)) < 0)
554 } while (!newline && len);
561 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
563 gchar *newline, *bp = buf;
569 if ((n = SSL_peek(ssl, bp, len)) <= 0)
571 if ((newline = memchr(bp, '\n', n)) != NULL)
572 n = newline - bp + 1;
573 if ((n = SSL_read(ssl, bp, n)) < 0)
577 } while (!newline && len);
584 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
586 g_return_val_if_fail(sock != NULL, -1);
590 return ssl_gets(sock->ssl, buf, len);
592 return fd_gets(sock->sock, buf, len);
595 gchar *fd_getline(gint fd)
602 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
607 str = g_realloc(str, size);
610 if (buf[len - 1] == '\n')
614 log_error("Read from socket fd%d failed: %s\n",
615 fd, strerror(errno));
625 gchar *ssl_getline(SSL *ssl)
632 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
637 str = g_realloc(str, size);
640 if (buf[len - 1] == '\n')
648 gchar *sock_getline(SockInfo *sock)
650 g_return_val_if_fail(sock != NULL, NULL);
654 return ssl_getline(sock->ssl);
656 return fd_getline(sock->sock);
659 gint sock_puts(SockInfo *sock, const gchar *buf)
663 if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
665 return sock_write(sock, "\r\n", 2);
668 /* peek at the next socket character without actually reading it */
669 gint sock_peek(SockInfo *sock)
674 g_return_val_if_fail(sock != NULL, -1);
678 if ((n = SSL_peek(sock->ssl, &ch, 1)) < 0)
684 if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
690 gint sock_close(SockInfo *sock)
699 ssl_done_socket(sock);
701 ret = fd_close(sock->sock);
702 g_free(sock->hostname);
708 gint fd_close(gint fd)
713 gint sock_input_add(SockInfo *sock,
714 GIOCondition condition,
721 g_return_val_if_fail(sock != NULL, -1);
723 channel = g_io_channel_unix_new(sock->sock);
724 /* :WK: We have to change some things here becuse most likey
725 function() does take SockInfo * and not an gint */
726 result = g_io_add_watch(channel, condition, function, data);
727 g_io_channel_unref(channel);