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>
47 #error USE_GIO is currently not supported
53 static gint sock_connect_by_hostname (gint sock,
54 const gchar *hostname,
57 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
61 static SockInfo *sockinfo_from_fd(const gchar *hostname,
65 gint fd_connect_unix(const gchar *path)
68 struct sockaddr_un addr;
70 sock = socket(PF_UNIX, SOCK_STREAM, 0);
72 perror("sock_connect_unix(): socket");
76 memset(&addr, 0, sizeof(addr));
77 addr.sun_family = AF_UNIX;
78 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
80 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
88 gint fd_open_unix(const gchar *path)
91 struct sockaddr_un addr;
93 sock = socket(PF_UNIX, SOCK_STREAM, 0);
96 perror("sock_open_unix(): socket");
100 memset(&addr, 0, sizeof(addr));
101 addr.sun_family = AF_UNIX;
102 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
104 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
110 if (listen(sock, 1) < 0) {
119 gint fd_accept(gint sock)
121 struct sockaddr_in caddr;
124 caddr_len = sizeof(caddr);
125 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
129 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
133 flags = fcntl(fd, F_GETFL, 0);
142 flags &= ~O_NONBLOCK;
144 return fcntl(fd, F_SETFL, flags);
147 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
149 g_return_val_if_fail(sock != NULL, -1);
151 return set_nonblocking_mode(sock->sock, nonblock);
155 static gboolean is_nonblocking_mode(gint fd)
159 flags = fcntl(fd, F_GETFL, 0);
165 return ((flags & O_NONBLOCK) != 0);
168 gboolean sock_is_nonblocking_mode(SockInfo *sock)
170 g_return_val_if_fail(sock != NULL, FALSE);
172 return is_nonblocking_mode(sock->sock);
177 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
181 struct sockaddr_in ad;
182 #ifndef HAVE_INET_ATON
186 #endif /* HAVE_INET_ATON */
188 memset(&ad, 0, sizeof(ad));
189 ad.sin_family = AF_INET;
190 ad.sin_port = htons(port);
193 if (!inet_aton(hostname, &ad.sin_addr)) {
196 inaddr = inet_addr(hostname);
198 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
203 #endif /* HAVE_INET_ATON */
204 if ((hp = gethostbyname(hostname)) == NULL) {
205 fprintf(stderr, "%s: unknown host.\n", hostname);
210 if (hp->h_length != 4 && hp->h_length != 8) {
211 fprintf(stderr, "illegal address length received for host %s\n", hostname);
216 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
219 return connect(sock, (struct sockaddr *)&ad, sizeof(ad));
223 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
225 gint sock = -1, gai_error;
226 struct addrinfo hints, *res, *ai;
229 memset(&hints, 0, sizeof(hints));
230 /* hints.ai_flags = AI_CANONNAME; */
231 hints.ai_family = AF_UNSPEC;
232 hints.ai_socktype = SOCK_STREAM;
233 hints.ai_protocol = IPPROTO_TCP;
235 /* convert port from integer to string. */
236 g_snprintf(port_str, sizeof(port_str), "%d", port);
238 if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
239 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
240 hostname, port_str, gai_strerror(gai_error));
244 for (ai = res; ai != NULL; ai = ai->ai_next) {
245 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
249 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
266 /* Open a connection using an external program. May be useful when
267 * you need to tunnel through a SOCKS or other firewall, or to
268 * establish an IMAP-over-SSH connection. */
269 /* TODO: Recreate this for sock_connect_thread() */
270 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
275 if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
276 perror("socketpair");
279 log_message("launching tunnel command \"%s\"\n", tunnelcmd);
284 dup(fd[1]); /* set onto stdin */
286 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
290 return sockinfo_from_fd(hostname, 0, fd[0]);
294 SockInfo *sock_connect(const gchar *hostname, gushort port)
299 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
302 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
307 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
308 if (errno != 0) perror("connect");
314 return sockinfo_from_fd(hostname, port, sock);
318 static SockInfo *sockinfo_from_fd(const gchar *hostname,
324 sockinfo = g_new0(SockInfo, 1);
325 sockinfo->sock = sock;
326 sockinfo->hostname = g_strdup(hostname);
327 sockinfo->port = port;
328 sockinfo->state = CONN_ESTABLISHED;
335 gint sock_printf(SockInfo *sock, const gchar *format, ...)
340 va_start(args, format);
341 g_vsnprintf(buf, sizeof(buf), format, args);
344 return sock_write(sock, buf, strlen(buf));
347 gint sock_read(SockInfo *sock, gchar *buf, gint len)
349 g_return_val_if_fail(sock != NULL, -1);
353 return ssl_read(sock->ssl, buf, len);
355 return fd_read(sock->sock, buf, len);
358 gint fd_read(gint fd, gchar *buf, gint len)
360 return read(fd, buf, len);
364 gint ssl_read(SSL *ssl, gchar *buf, gint len)
366 return SSL_read(ssl, buf, len);
370 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
372 g_return_val_if_fail(sock != NULL, -1);
376 return ssl_write(sock->ssl, buf, len);
378 return fd_write(sock->sock, buf, len);
381 gint fd_write(gint fd, const gchar *buf, gint len)
386 signal(SIGPIPE, SIG_IGN);
387 n = write(fd, buf, len);
389 log_error("write on fd%d: %s\n", fd, strerror(errno));
401 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
406 n = SSL_write(ssl, buf, len);
418 gint fd_gets(gint fd, gchar *buf, gint len)
420 gchar *newline, *bp = buf;
426 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
428 if ((newline = memchr(bp, '\n', n)) != NULL)
429 n = newline - bp + 1;
430 if ((n = read(fd, bp, n)) < 0)
434 } while (!newline && len);
441 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
443 gchar *newline, *bp = buf;
449 if ((n = SSL_peek(ssl, bp, len)) <= 0)
451 if ((newline = memchr(bp, '\n', n)) != NULL)
452 n = newline - bp + 1;
453 if ((n = SSL_read(ssl, bp, n)) < 0)
457 } while (!newline && len);
464 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
466 g_return_val_if_fail(sock != NULL, -1);
470 return ssl_gets(sock->ssl, buf, len);
472 return fd_gets(sock->sock, buf, len);
475 gchar *fd_getline(gint fd)
482 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
487 str = g_realloc(str, size);
490 if (buf[len - 1] == '\n')
494 log_error("Read from socket fd%d failed: %s\n",
495 fd, strerror(errno));
505 gchar *ssl_getline(SSL *ssl)
512 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
517 str = g_realloc(str, size);
520 if (buf[len - 1] == '\n')
528 gchar *sock_getline(SockInfo *sock)
530 g_return_val_if_fail(sock != NULL, NULL);
534 return ssl_getline(sock->ssl);
536 return fd_getline(sock->sock);
539 gint sock_puts(SockInfo *sock, const gchar *buf)
543 if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
545 return sock_write(sock, "\r\n", 2);
548 /* peek at the next socket character without actually reading it */
549 gint sock_peek(SockInfo *sock)
554 g_return_val_if_fail(sock != NULL, -1);
556 if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
562 gint sock_close(SockInfo *sock)
571 ssl_done_socket(sock);
573 ret = fd_close(sock->sock);
574 g_free(sock->hostname);
580 gint fd_close(gint fd)
585 gint sock_gdk_input_add(SockInfo *sock,
586 GdkInputCondition condition,
587 GdkInputFunction function,
590 g_return_val_if_fail(sock != NULL, -1);
592 /* :WK: We have to change some things here becuse most likey
593 function() does take SockInfo * and not an gint */
594 return gdk_input_add(sock->sock, condition, function, data);