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>
18 #include "mutt_socket.h"
19 #include "mutt_tunnel.h"
25 #include <netinet/in.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
32 /* -- data types -- */
39 /* forward declarations */
40 static int tunnel_socket_open (CONNECTION *);
41 static int tunnel_socket_close (CONNECTION *);
42 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len);
43 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
46 /* -- public functions -- */
47 int mutt_tunnel_socket_setup (CONNECTION * conn)
49 conn->conn_open = tunnel_socket_open;
50 conn->conn_close = tunnel_socket_close;
51 conn->conn_read = tunnel_socket_read;
52 conn->conn_write = tunnel_socket_write;
57 static int tunnel_socket_open (CONNECTION * conn)
64 tunnel = p_new(TUNNEL_DATA, 1);
65 conn->sockdata = tunnel;
67 mutt_message (_("Connecting with \"%s\"..."), Tunnel);
69 if ((rc = pipe (pin)) == -1) {
73 if ((rc = pipe (pout)) == -1) {
78 mutt_block_signals_system ();
79 if ((pid = fork ()) == 0) {
80 mutt_unblock_signals_system (0);
81 if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
87 close (STDERR_FILENO);
89 /* Don't let the subprocess think it can use the controlling tty */
92 execl (EXECSHELL, "sh", "-c", Tunnel, NULL);
95 mutt_unblock_signals_system (1);
102 mutt_perror ("fork");
105 if (close (pin[1]) < 0 || close (pout[0]) < 0)
106 mutt_perror ("close");
108 fcntl (pin[0], F_SETFD, FD_CLOEXEC);
109 fcntl (pout[1], F_SETFD, FD_CLOEXEC);
111 tunnel->readfd = pin[0];
112 tunnel->writefd = pout[1];
115 conn->fd = 42; /* stupid hack */
120 static int tunnel_socket_close (CONNECTION * conn)
122 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
125 close (tunnel->readfd);
126 close (tunnel->writefd);
127 waitpid (tunnel->pid, &status, 0);
128 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
129 mutt_error(_("Tunnel to %s returned error %d (%s)"),
130 conn->account.host, WEXITSTATUS(status),
131 NONULL(mutt_strsysexit(WEXITSTATUS(status))));
134 p_delete(&conn->sockdata);
138 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len)
140 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
143 rc = read (tunnel->readfd, buf, len);
145 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
153 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
156 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
159 rc = write (tunnel->writefd, buf, len);
161 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,