2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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>
51 #error USE_GIO is currently not supported
57 static gint sock_connect_by_hostname (gint sock,
58 const gchar *hostname,
61 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
65 static SockInfo *sockinfo_from_fd(const gchar *hostname,
69 gint fd_connect_unix(const gchar *path)
72 struct sockaddr_un addr;
74 sock = socket(PF_UNIX, SOCK_STREAM, 0);
76 perror("sock_connect_unix(): socket");
80 memset(&addr, 0, sizeof(addr));
81 addr.sun_family = AF_UNIX;
82 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
84 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
92 gint fd_open_unix(const gchar *path)
95 struct sockaddr_un addr;
97 sock = socket(PF_UNIX, SOCK_STREAM, 0);
100 perror("sock_open_unix(): socket");
104 memset(&addr, 0, sizeof(addr));
105 addr.sun_family = AF_UNIX;
106 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
108 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
114 if (listen(sock, 1) < 0) {
123 gint fd_accept(gint sock)
125 struct sockaddr_in caddr;
128 caddr_len = sizeof(caddr);
129 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
133 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
137 flags = fcntl(fd, F_GETFL, 0);
146 flags &= ~O_NONBLOCK;
148 return fcntl(fd, F_SETFL, flags);
151 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
153 g_return_val_if_fail(sock != NULL, -1);
155 return set_nonblocking_mode(sock->sock, nonblock);
159 static gboolean is_nonblocking_mode(gint fd)
163 flags = fcntl(fd, F_GETFL, 0);
169 return ((flags & O_NONBLOCK) != 0);
172 gboolean sock_is_nonblocking_mode(SockInfo *sock)
174 g_return_val_if_fail(sock != NULL, FALSE);
176 return is_nonblocking_mode(sock->sock);
181 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
185 struct sockaddr_in ad;
186 #ifndef HAVE_INET_ATON
190 #endif /* HAVE_INET_ATON */
192 memset(&ad, 0, sizeof(ad));
193 ad.sin_family = AF_INET;
194 ad.sin_port = htons(port);
197 if (!inet_aton(hostname, &ad.sin_addr)) {
200 inaddr = inet_addr(hostname);
202 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
207 #endif /* HAVE_INET_ATON */
208 if ((hp = gethostbyname(hostname)) == NULL) {
209 fprintf(stderr, "%s: unknown host.\n", hostname);
214 if (hp->h_length != 4 && hp->h_length != 8) {
215 fprintf(stderr, "illegal address length received for host %s\n", hostname);
220 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
223 return connect(sock, (struct sockaddr *)&ad, sizeof(ad));
227 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
229 gint sock = -1, gai_error;
230 struct addrinfo hints, *res, *ai;
233 memset(&hints, 0, sizeof(hints));
234 /* hints.ai_flags = AI_CANONNAME; */
235 hints.ai_family = AF_UNSPEC;
236 hints.ai_socktype = SOCK_STREAM;
237 hints.ai_protocol = IPPROTO_TCP;
239 /* convert port from integer to string. */
240 g_snprintf(port_str, sizeof(port_str), "%d", port);
242 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
243 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
244 hostname, port_str, gai_strerror(gai_error));
248 for (ai = res; ai != NULL; ai = ai->ai_next) {
249 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
253 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
270 /* Open a connection using an external program. May be useful when
271 * you need to tunnel through a SOCKS or other firewall, or to
272 * establish an IMAP-over-SSH connection. */
273 /* TODO: Recreate this for sock_connect_thread() */
274 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
279 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
280 perror("socketpair");
283 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
288 dup(fd[1]); /* set onto stdin */
290 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
294 return sockinfo_from_fd(hostname, 0, fd[0]);
298 SockInfo *sock_connect(const gchar *hostname, gushort port)
303 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
306 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
311 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
312 if (errno != 0) perror("connect");
318 return sockinfo_from_fd(hostname, port, sock);
322 static SockInfo *sockinfo_from_fd(const gchar *hostname,
328 sockinfo = g_new0(SockInfo, 1);
329 sockinfo->sock = sock;
330 sockinfo->hostname = g_strdup(hostname);
331 sockinfo->port = port;
332 sockinfo->state = CONN_ESTABLISHED;
340 static void sock_connect_thread(SockInfo *sockinfo)
343 if ((sockinfo->sock = sock_connect_by_getaddrinfo
344 (sockinfo->hostname, sockinfo->port)) < 0)
345 pthread_exit((void *)1);
347 if (sock_connect_by_hostname(sockinfo->sock, sockinfo->hostname,
348 sockinfo->port) < 0) {
349 if (errno != 0) perror("connect");
350 sockinfo->state = CONN_FAILED;
351 pthread_exit((void *)1);
354 sockinfo->state = CONN_ESTABLISHED;
359 SockInfo *sock_connect_with_thread(const gchar *hostname, gushort port)
365 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
371 sockinfo = g_new0(SockInfo, 1);
372 sockinfo->sock = sock;
373 sockinfo->hostname = g_strdup(hostname);
374 sockinfo->port = port;
375 sockinfo->state = CONN_READY;
377 pthread_create(&sockinfo->connect_thr, NULL,
378 (void *)sock_connect_thread,
380 pthread_mutex_init(&sockinfo->mutex, NULL);
381 pthread_detach(sockinfo->connect_thr);
388 gint sock_printf(SockInfo *sock, const gchar *format, ...)
393 va_start(args, format);
394 g_vsnprintf(buf, sizeof(buf), format, args);
397 return sock_write(sock, buf, strlen(buf));
400 gint sock_read(SockInfo *sock, gchar *buf, gint len)
402 g_return_val_if_fail(sock != NULL, -1);
406 return ssl_read(sock->ssl, buf, len);
408 return fd_read(sock->sock, buf, len);
411 gint fd_read(gint fd, gchar *buf, gint len)
413 return read(fd, buf, len);
417 gint ssl_read(SSL *ssl, gchar *buf, gint len)
419 return SSL_read(ssl, buf, len);
423 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
425 g_return_val_if_fail(sock != NULL, -1);
429 return ssl_write(sock->ssl, buf, len);
431 return fd_write(sock->sock, buf, len);
434 gint fd_write(gint fd, const gchar *buf, gint len)
439 signal(SIGPIPE, SIG_IGN);
440 n = write(fd, buf, len);
442 log_error("write on fd%d: %s\n", fd, strerror(errno));
454 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
459 n = SSL_write(ssl, buf, len);
471 gint fd_gets(gint fd, gchar *buf, gint len)
473 gchar *newline, *bp = buf;
479 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
481 if ((newline = memchr(bp, '\n', n)) != NULL)
482 n = newline - bp + 1;
483 if ((n = read(fd, bp, n)) < 0)
487 } while (!newline && len);
494 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
496 gchar *newline, *bp = buf;
502 if ((n = SSL_peek(ssl, bp, len)) <= 0)
504 if ((newline = memchr(bp, '\n', n)) != NULL)
505 n = newline - bp + 1;
506 if ((n = SSL_read(ssl, bp, n)) < 0)
510 } while (!newline && len);
517 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
519 g_return_val_if_fail(sock != NULL, -1);
523 return ssl_gets(sock->ssl, buf, len);
525 return fd_gets(sock->sock, buf, len);
528 gchar *fd_getline(gint fd)
535 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
540 str = g_realloc(str, size);
543 if (buf[len - 1] == '\n')
547 log_error("Read from socket fd%d failed: %s\n",
548 fd, strerror(errno));
558 gchar *ssl_getline(SSL *ssl)
565 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
570 str = g_realloc(str, size);
573 if (buf[len - 1] == '\n')
581 gchar *sock_getline(SockInfo *sock)
583 g_return_val_if_fail(sock != NULL, NULL);
587 return ssl_getline(sock->ssl);
589 return fd_getline(sock->sock);
592 gint sock_puts(SockInfo *sock, const gchar *buf)
596 if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
598 return sock_write(sock, "\r\n", 2);
601 /* peek at the next socket character without actually reading it */
602 gint sock_peek(SockInfo *sock)
607 g_return_val_if_fail(sock != NULL, -1);
609 if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
615 gint sock_close(SockInfo *sock)
624 ssl_done_socket(sock);
626 ret = fd_close(sock->sock);
627 g_free(sock->hostname);
633 gint fd_close(gint fd)
638 gint sock_gdk_input_add(SockInfo *sock,
639 GdkInputCondition condition,
640 GdkInputFunction function,
643 g_return_val_if_fail(sock != NULL, -1);
645 /* :WK: We have to change some things here becuse most likey
646 function() does take SockInfo * and not an gint */
647 return gdk_input_add(sock->sock, condition, function, data);