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>
51 #error USE_GIO is currently not supported
57 static gint sock_connect_with_timeout (gint sock,
58 const struct sockaddr *serv_addr,
63 static gint sock_connect_by_hostname (gint sock,
64 const gchar *hostname,
67 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
71 static SockInfo *sockinfo_from_fd(const gchar *hostname,
75 gint fd_connect_unix(const gchar *path)
78 struct sockaddr_un addr;
80 sock = socket(PF_UNIX, SOCK_STREAM, 0);
82 perror("sock_connect_unix(): socket");
86 memset(&addr, 0, sizeof(addr));
87 addr.sun_family = AF_UNIX;
88 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
90 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
98 gint fd_open_unix(const gchar *path)
101 struct sockaddr_un addr;
103 sock = socket(PF_UNIX, SOCK_STREAM, 0);
106 perror("sock_open_unix(): socket");
110 memset(&addr, 0, sizeof(addr));
111 addr.sun_family = AF_UNIX;
112 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
114 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
120 if (listen(sock, 1) < 0) {
129 gint fd_accept(gint sock)
131 struct sockaddr_in caddr;
134 caddr_len = sizeof(caddr);
135 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
139 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
143 flags = fcntl(fd, F_GETFL, 0);
152 flags &= ~O_NONBLOCK;
154 return fcntl(fd, F_SETFL, flags);
157 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
159 g_return_val_if_fail(sock != NULL, -1);
161 return set_nonblocking_mode(sock->sock, nonblock);
165 static gboolean is_nonblocking_mode(gint fd)
169 flags = fcntl(fd, F_GETFL, 0);
175 return ((flags & O_NONBLOCK) != 0);
178 gboolean sock_is_nonblocking_mode(SockInfo *sock)
180 g_return_val_if_fail(sock != NULL, FALSE);
182 return is_nonblocking_mode(sock->sock);
185 static gint fd_check_io(gint fd, GIOCondition cond)
187 struct timeval timeout;
190 timeout.tv_sec = IO_TIMEOUT;
196 if (cond == G_IO_IN) {
197 select(fd + 1, &fds, NULL, NULL, &timeout);
199 select(fd + 1, NULL, &fds, NULL, &timeout);
202 if (FD_ISSET(fd, &fds)) {
205 g_warning("Socket IO timeout\n");
210 static sigjmp_buf jmpenv;
212 static void timeout_handler(gint sig)
214 siglongjmp(jmpenv, 1);
217 static gint sock_connect_with_timeout(gint sock,
218 const struct sockaddr *serv_addr,
223 void (*prev_handler)(gint);
226 prev_handler = signal(SIGALRM, timeout_handler);
227 if (sigsetjmp(jmpenv, 1)) {
229 signal(SIGALRM, prev_handler);
235 ret = connect(sock, serv_addr, addrlen);
238 signal(SIGALRM, prev_handler);
243 struct hostent *my_gethostbyname(const gchar *hostname)
246 void (*prev_handler)(gint);
247 guint timeout_secs = IO_TIMEOUT;
250 prev_handler = signal(SIGALRM, timeout_handler);
251 if (sigsetjmp(jmpenv, 1)) {
253 signal(SIGALRM, prev_handler);
254 fprintf(stderr, "%s: host lookup timed out.\n", hostname);
260 if ((hp = gethostbyname(hostname)) == NULL) {
262 signal(SIGALRM, prev_handler);
263 fprintf(stderr, "%s: unknown host.\n", hostname);
269 signal(SIGALRM, prev_handler);
275 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
278 return inet_aton(hostname, inp);
283 inaddr = inet_addr(hostname);
285 memcpy(inp, &inaddr, sizeof(inaddr));
292 #endif /* HAVE_INET_ATON */
295 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
299 struct sockaddr_in ad;
300 guint timeout_secs = IO_TIMEOUT;
302 memset(&ad, 0, sizeof(ad));
303 ad.sin_family = AF_INET;
304 ad.sin_port = htons(port);
306 if (!my_inet_aton(hostname, &ad.sin_addr)) {
307 if ((hp = my_gethostbyname(hostname)) == NULL) {
308 fprintf(stderr, "%s: unknown host.\n", hostname);
313 if (hp->h_length != 4 && hp->h_length != 8) {
314 fprintf(stderr, "illegal address length received for host %s\n", hostname);
319 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
322 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
323 sizeof(ad), timeout_secs);
327 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
329 gint sock = -1, gai_error;
330 struct addrinfo hints, *res, *ai;
331 guint timeout_secs = IO_TIMEOUT;
334 memset(&hints, 0, sizeof(hints));
335 /* hints.ai_flags = AI_CANONNAME; */
336 hints.ai_family = AF_UNSPEC;
337 hints.ai_socktype = SOCK_STREAM;
338 hints.ai_protocol = IPPROTO_TCP;
340 /* convert port from integer to string. */
341 g_snprintf(port_str, sizeof(port_str), "%d", port);
343 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
344 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
345 hostname, port_str, gai_strerror(gai_error));
349 for (ai = res; ai != NULL; ai = ai->ai_next) {
350 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
354 if (sock_connect_with_timeout
355 (sock, ai->ai_addr, ai->ai_addrlen, timeout_secs) == 0)
372 /* Open a connection using an external program. May be useful when
373 * you need to tunnel through a SOCKS or other firewall, or to
374 * establish an IMAP-over-SSH connection. */
375 /* TODO: Recreate this for sock_connect_thread() */
376 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
381 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
382 perror("socketpair");
385 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
390 dup(fd[1]); /* set onto stdin */
392 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
396 return sockinfo_from_fd(hostname, 0, fd[0]);
400 SockInfo *sock_connect(const gchar *hostname, gushort port)
405 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
408 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
413 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
414 if (errno != 0) perror("connect");
420 return sockinfo_from_fd(hostname, port, sock);
424 static SockInfo *sockinfo_from_fd(const gchar *hostname,
430 sockinfo = g_new0(SockInfo, 1);
431 sockinfo->sock = sock;
432 sockinfo->hostname = g_strdup(hostname);
433 sockinfo->port = port;
434 sockinfo->state = CONN_ESTABLISHED;
441 gint sock_printf(SockInfo *sock, const gchar *format, ...)
446 va_start(args, format);
447 g_vsnprintf(buf, sizeof(buf), format, args);
450 return sock_write(sock, buf, strlen(buf));
453 gint sock_read(SockInfo *sock, gchar *buf, gint len)
455 g_return_val_if_fail(sock != NULL, -1);
459 return ssl_read(sock->ssl, buf, len);
461 return fd_read(sock->sock, buf, len);
464 gint fd_read(gint fd, gchar *buf, gint len)
466 if (fd_check_io(fd, G_IO_IN) < 0)
469 return read(fd, buf, len);
473 gint ssl_read(SSL *ssl, gchar *buf, gint len)
475 return SSL_read(ssl, buf, len);
479 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
481 g_return_val_if_fail(sock != NULL, -1);
485 return ssl_write(sock->ssl, buf, len);
487 return fd_write(sock->sock, buf, len);
490 gint fd_write(gint fd, const gchar *buf, gint len)
495 if (fd_check_io(fd, G_IO_OUT) < 0)
497 signal(SIGPIPE, SIG_IGN);
498 n = write(fd, buf, len);
500 log_error("write on fd%d: %s\n", fd, strerror(errno));
512 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
517 n = SSL_write(ssl, buf, len);
529 gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
531 if (fd_check_io(fd, G_IO_IN) < 0)
534 return recv(fd, buf, len, flags);
537 gint fd_gets(gint fd, gchar *buf, gint len)
539 gchar *newline, *bp = buf;
545 if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
547 if ((newline = memchr(bp, '\n', n)) != NULL)
548 n = newline - bp + 1;
549 if ((n = fd_read(fd, bp, n)) < 0)
553 } while (!newline && len);
560 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
562 gchar *newline, *bp = buf;
568 if ((n = SSL_peek(ssl, bp, len)) <= 0)
570 if ((newline = memchr(bp, '\n', n)) != NULL)
571 n = newline - bp + 1;
572 if ((n = SSL_read(ssl, bp, n)) < 0)
576 } while (!newline && len);
583 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
585 g_return_val_if_fail(sock != NULL, -1);
589 return ssl_gets(sock->ssl, buf, len);
591 return fd_gets(sock->sock, buf, len);
594 gchar *fd_getline(gint fd)
601 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
606 str = g_realloc(str, size);
609 if (buf[len - 1] == '\n')
613 log_error("Read from socket fd%d failed: %s\n",
614 fd, strerror(errno));
624 gchar *ssl_getline(SSL *ssl)
631 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
636 str = g_realloc(str, size);
639 if (buf[len - 1] == '\n')
647 gchar *sock_getline(SockInfo *sock)
649 g_return_val_if_fail(sock != NULL, NULL);
653 return ssl_getline(sock->ssl);
655 return fd_getline(sock->sock);
658 gint sock_puts(SockInfo *sock, const gchar *buf)
662 if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
664 return sock_write(sock, "\r\n", 2);
667 /* peek at the next socket character without actually reading it */
668 gint sock_peek(SockInfo *sock)
673 g_return_val_if_fail(sock != NULL, -1);
677 if ((n = SSL_peek(sock->ssl, &ch, 1)) < 0)
683 if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
689 gint sock_close(SockInfo *sock)
698 ssl_done_socket(sock);
700 ret = fd_close(sock->sock);
701 g_free(sock->hostname);
707 gint fd_close(gint fd)
712 gint sock_gdk_input_add(SockInfo *sock,
713 GdkInputCondition condition,
714 GdkInputFunction function,
717 g_return_val_if_fail(sock != NULL, -1);
719 /* :WK: We have to change some things here becuse most likey
720 function() does take SockInfo * and not an gint */
721 return gdk_input_add(sock->sock, condition, function, data);