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.
16 #include "mutt_socket.h"
17 #include "mutt_tunnel.h"
23 #include <netinet/in.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
30 /* -- data types -- */
37 /* forward declarations */
38 static int tunnel_socket_open (CONNECTION *);
39 static int tunnel_socket_close (CONNECTION *);
40 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len);
41 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
44 /* -- public functions -- */
45 int mutt_tunnel_socket_setup (CONNECTION * conn)
47 conn->conn_open = tunnel_socket_open;
48 conn->conn_close = tunnel_socket_close;
49 conn->conn_read = tunnel_socket_read;
50 conn->conn_write = tunnel_socket_write;
55 static int tunnel_socket_open (CONNECTION * conn)
62 tunnel = (TUNNEL_DATA *) mem_malloc (sizeof (TUNNEL_DATA));
63 conn->sockdata = tunnel;
65 mutt_message (_("Connecting with \"%s\"..."), Tunnel);
67 if ((rc = pipe (pin)) == -1) {
71 if ((rc = pipe (pout)) == -1) {
76 mutt_block_signals_system ();
77 if ((pid = fork ()) == 0) {
78 mutt_unblock_signals_system (0);
79 if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
85 close (STDERR_FILENO);
87 /* Don't let the subprocess think it can use the controlling tty */
90 execl (EXECSHELL, "sh", "-c", Tunnel, NULL);
93 mutt_unblock_signals_system (1);
100 mutt_perror ("fork");
103 if (close (pin[1]) < 0 || close (pout[0]) < 0)
104 mutt_perror ("close");
106 fcntl (pin[0], F_SETFD, FD_CLOEXEC);
107 fcntl (pout[1], F_SETFD, FD_CLOEXEC);
109 tunnel->readfd = pin[0];
110 tunnel->writefd = pout[1];
113 conn->fd = 42; /* stupid hack */
118 static int tunnel_socket_close (CONNECTION * conn)
120 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
123 close (tunnel->readfd);
124 close (tunnel->writefd);
125 waitpid (tunnel->pid, &status, 0);
126 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
127 mutt_error(_("Tunnel to %s returned error %d (%s)"),
128 conn->account.host, WEXITSTATUS(status),
129 NONULL(mutt_strsysexit(WEXITSTATUS(status))));
132 mem_free (&conn->sockdata);
136 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len)
138 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
141 rc = read (tunnel->readfd, buf, len);
143 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
151 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
154 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
157 rc = write (tunnel->writefd, buf, len);
159 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,