2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Manoj Kasichainula <manoj@io.com>
4 * Copyright (C) 2001,2005 Brendan Cully <brendan@kublai.com>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
15 #include <lib-lib/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
20 #include "mutt_socket.h"
21 #include "mutt_tunnel.h"
24 #include <netinet/in.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
31 /* -- data types -- */
38 /* forward declarations */
39 static int tunnel_socket_open (CONNECTION *);
40 static int tunnel_socket_close (CONNECTION *);
41 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len);
42 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
45 /* -- public functions -- */
46 int mutt_tunnel_socket_setup (CONNECTION * conn)
48 conn->conn_open = tunnel_socket_open;
49 conn->conn_close = tunnel_socket_close;
50 conn->conn_read = tunnel_socket_read;
51 conn->conn_write = tunnel_socket_write;
56 static int tunnel_socket_open (CONNECTION * conn)
63 tunnel = p_new(TUNNEL_DATA, 1);
64 conn->sockdata = tunnel;
66 mutt_message (_("Connecting with \"%s\"..."), Tunnel);
68 if ((rc = pipe (pin)) == -1) {
72 if ((rc = pipe (pout)) == -1) {
77 mutt_block_signals_system ();
78 if ((pid = fork ()) == 0) {
79 mutt_unblock_signals_system (0);
80 if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
86 close (STDERR_FILENO);
88 /* Don't let the subprocess think it can use the controlling tty */
91 execl ("/bin/sh", "sh", "-c", Tunnel, NULL);
94 mutt_unblock_signals_system (1);
101 mutt_perror ("fork");
104 if (close (pin[1]) < 0 || close (pout[0]) < 0)
105 mutt_perror ("close");
107 fcntl (pin[0], F_SETFD, FD_CLOEXEC);
108 fcntl (pout[1], F_SETFD, FD_CLOEXEC);
110 tunnel->readfd = pin[0];
111 tunnel->writefd = pout[1];
114 conn->fd = 42; /* stupid hack */
119 static int tunnel_socket_close (CONNECTION * conn)
121 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
124 close (tunnel->readfd);
125 close (tunnel->writefd);
126 waitpid (tunnel->pid, &status, 0);
127 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
128 mutt_error(_("Tunnel to %s returned error %d (%s)"),
129 conn->account.host, WEXITSTATUS(status),
130 m_strsysexit(WEXITSTATUS(status)));
133 p_delete(&conn->sockdata);
137 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len)
139 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
142 rc = read (tunnel->readfd, buf, len);
144 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
152 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
155 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
158 rc = write (tunnel->writefd, buf, len);
160 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,