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>
48 #error USE_GIO is currently not supported
53 static gint sock_connect_with_timeout (gint sock,
54 const struct sockaddr *serv_addr,
59 static gint sock_connect_by_hostname (gint sock,
60 const gchar *hostname,
63 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
67 static SockInfo *sockinfo_from_fd(const gchar *hostname,
71 gint fd_connect_unix(const gchar *path)
74 struct sockaddr_un addr;
76 sock = socket(PF_UNIX, SOCK_STREAM, 0);
78 perror("sock_connect_unix(): socket");
82 memset(&addr, 0, sizeof(addr));
83 addr.sun_family = AF_UNIX;
84 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
86 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
94 gint fd_open_unix(const gchar *path)
97 struct sockaddr_un addr;
99 sock = socket(PF_UNIX, SOCK_STREAM, 0);
102 perror("sock_open_unix(): socket");
106 memset(&addr, 0, sizeof(addr));
107 addr.sun_family = AF_UNIX;
108 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
110 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
116 if (listen(sock, 1) < 0) {
125 gint fd_accept(gint sock)
127 struct sockaddr_in caddr;
130 caddr_len = sizeof(caddr);
131 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
135 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
139 flags = fcntl(fd, F_GETFL, 0);
148 flags &= ~O_NONBLOCK;
150 return fcntl(fd, F_SETFL, flags);
153 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
155 g_return_val_if_fail(sock != NULL, -1);
157 return set_nonblocking_mode(sock->sock, nonblock);
161 static gboolean is_nonblocking_mode(gint fd)
165 flags = fcntl(fd, F_GETFL, 0);
171 return ((flags & O_NONBLOCK) != 0);
174 gboolean sock_is_nonblocking_mode(SockInfo *sock)
176 g_return_val_if_fail(sock != NULL, FALSE);
178 return is_nonblocking_mode(sock->sock);
181 static sigjmp_buf jmpenv;
183 static void timeout_handler(gint sig)
185 siglongjmp(jmpenv, 1);
188 static gint sock_connect_with_timeout(gint sock,
189 const struct sockaddr *serv_addr,
194 void (*prev_handler)(gint);
197 prev_handler = signal(SIGALRM, timeout_handler);
198 if (sigsetjmp(jmpenv, 1)) {
200 signal(SIGALRM, prev_handler);
206 ret = connect(sock, serv_addr, addrlen);
209 signal(SIGALRM, prev_handler);
215 static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
218 return inet_aton(hostname, inp);
223 inaddr = inet_addr(hostname);
225 memcpy(inp, &inaddr, sizeof(inaddr));
232 #endif /* HAVE_INET_ATON */
235 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
239 struct sockaddr_in ad;
240 guint timeout_secs = 30;
242 memset(&ad, 0, sizeof(ad));
243 ad.sin_family = AF_INET;
244 ad.sin_port = htons(port);
246 if (!my_inet_aton(hostname, &ad.sin_addr)) {
247 void (*prev_handler)(gint);
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);
271 if (hp->h_length != 4 && hp->h_length != 8) {
272 fprintf(stderr, "illegal address length received for host %s\n", hostname);
277 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
280 return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
281 sizeof(ad), timeout_secs);
285 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
287 gint sock = -1, gai_error;
288 struct addrinfo hints, *res, *ai;
289 guint timeout_secs = 30;
292 memset(&hints, 0, sizeof(hints));
293 /* hints.ai_flags = AI_CANONNAME; */
294 hints.ai_family = AF_UNSPEC;
295 hints.ai_socktype = SOCK_STREAM;
296 hints.ai_protocol = IPPROTO_TCP;
298 /* convert port from integer to string. */
299 g_snprintf(port_str, sizeof(port_str), "%d", port);
301 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
302 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
303 hostname, port_str, gai_strerror(gai_error));
307 for (ai = res; ai != NULL; ai = ai->ai_next) {
308 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
312 if (sock_connect_with_timeout
313 (sock, ai->ai_addr, ai->ai_addrlen, timeout_secs) == 0)
330 /* Open a connection using an external program. May be useful when
331 * you need to tunnel through a SOCKS or other firewall, or to
332 * establish an IMAP-over-SSH connection. */
333 /* TODO: Recreate this for sock_connect_thread() */
334 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
339 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
340 perror("socketpair");
343 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
348 dup(fd[1]); /* set onto stdin */
350 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
354 return sockinfo_from_fd(hostname, 0, fd[0]);
358 SockInfo *sock_connect(const gchar *hostname, gushort port)
363 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
366 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
371 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
372 if (errno != 0) perror("connect");
378 return sockinfo_from_fd(hostname, port, sock);
382 static SockInfo *sockinfo_from_fd(const gchar *hostname,
388 sockinfo = g_new0(SockInfo, 1);
389 sockinfo->sock = sock;
390 sockinfo->hostname = g_strdup(hostname);
391 sockinfo->port = port;
392 sockinfo->state = CONN_ESTABLISHED;
399 gint sock_printf(SockInfo *sock, const gchar *format, ...)
404 va_start(args, format);
405 g_vsnprintf(buf, sizeof(buf), format, args);
408 return sock_write(sock, buf, strlen(buf));
411 gint sock_read(SockInfo *sock, gchar *buf, gint len)
413 g_return_val_if_fail(sock != NULL, -1);
417 return ssl_read(sock->ssl, buf, len);
419 return fd_read(sock->sock, buf, len);
422 gint fd_read(gint fd, gchar *buf, gint len)
424 return read(fd, buf, len);
428 gint ssl_read(SSL *ssl, gchar *buf, gint len)
430 return SSL_read(ssl, buf, len);
434 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
436 g_return_val_if_fail(sock != NULL, -1);
440 return ssl_write(sock->ssl, buf, len);
442 return fd_write(sock->sock, buf, len);
445 gint fd_write(gint fd, const gchar *buf, gint len)
450 signal(SIGPIPE, SIG_IGN);
451 n = write(fd, buf, len);
453 log_error("write on fd%d: %s\n", fd, strerror(errno));
465 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
470 n = SSL_write(ssl, buf, len);
482 gint fd_gets(gint fd, gchar *buf, gint len)
484 gchar *newline, *bp = buf;
490 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
492 if ((newline = memchr(bp, '\n', n)) != NULL)
493 n = newline - bp + 1;
494 if ((n = read(fd, bp, n)) < 0)
498 } while (!newline && len);
505 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
507 gchar *newline, *bp = buf;
513 if ((n = SSL_peek(ssl, bp, len)) <= 0)
515 if ((newline = memchr(bp, '\n', n)) != NULL)
516 n = newline - bp + 1;
517 if ((n = SSL_read(ssl, bp, n)) < 0)
521 } while (!newline && len);
528 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
530 g_return_val_if_fail(sock != NULL, -1);
534 return ssl_gets(sock->ssl, buf, len);
536 return fd_gets(sock->sock, buf, len);
539 gchar *fd_getline(gint fd)
546 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
551 str = g_realloc(str, size);
554 if (buf[len - 1] == '\n')
558 log_error("Read from socket fd%d failed: %s\n",
559 fd, strerror(errno));
569 gchar *ssl_getline(SSL *ssl)
576 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
581 str = g_realloc(str, size);
584 if (buf[len - 1] == '\n')
592 gchar *sock_getline(SockInfo *sock)
594 g_return_val_if_fail(sock != NULL, NULL);
598 return ssl_getline(sock->ssl);
600 return fd_getline(sock->sock);
603 gint sock_puts(SockInfo *sock, const gchar *buf)
607 if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
609 return sock_write(sock, "\r\n", 2);
612 /* peek at the next socket character without actually reading it */
613 gint sock_peek(SockInfo *sock)
618 g_return_val_if_fail(sock != NULL, -1);
622 if ((n = SSL_peek(sock->ssl, &ch, 1)) < 0)
628 if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
634 gint sock_close(SockInfo *sock)
643 ssl_done_socket(sock);
645 ret = fd_close(sock->sock);
646 g_free(sock->hostname);
652 gint fd_close(gint fd)
657 gint sock_gdk_input_add(SockInfo *sock,
658 GdkInputCondition condition,
659 GdkInputFunction function,
662 g_return_val_if_fail(sock != NULL, -1);
664 /* :WK: We have to change some things here becuse most likey
665 function() does take SockInfo * and not an gint */
666 return gdk_input_add(sock->sock, condition, function, data);