2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Manoj Kasichainula <manoj@io.com>
4 * Copyright (C) 2001 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"
19 #include <netinet/in.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
26 /* -- data types -- */
33 /* forward declarations */
34 static int tunnel_socket_open (CONNECTION *);
35 static int tunnel_socket_close (CONNECTION *);
36 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len);
37 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
40 /* -- public functions -- */
41 int mutt_tunnel_socket_setup (CONNECTION * conn)
43 conn->conn_open = tunnel_socket_open;
44 conn->conn_close = tunnel_socket_close;
45 conn->conn_read = tunnel_socket_read;
46 conn->conn_write = tunnel_socket_write;
51 static int tunnel_socket_open (CONNECTION * conn)
58 tunnel = (TUNNEL_DATA *) safe_malloc (sizeof (TUNNEL_DATA));
59 conn->sockdata = tunnel;
61 mutt_message (_("Connecting with \"%s\"..."), Tunnel);
63 if ((rc = pipe (pin)) == -1) {
67 if ((rc = pipe (pout)) == -1) {
72 mutt_block_signals_system ();
73 if ((pid = fork ()) == 0) {
74 mutt_unblock_signals_system (0);
75 if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
81 close (STDERR_FILENO);
83 /* Don't let the subprocess think it can use the controlling tty */
86 execl (EXECSHELL, "sh", "-c", Tunnel, NULL);
89 mutt_unblock_signals_system (1);
99 if (close (pin[1]) < 0 || close (pout[0]) < 0)
100 mutt_perror ("close");
102 fcntl (pin[0], F_SETFD, FD_CLOEXEC);
103 fcntl (pout[1], F_SETFD, FD_CLOEXEC);
105 tunnel->readfd = pin[0];
106 tunnel->writefd = pout[1];
109 conn->fd = 42; /* stupid hack */
114 static int tunnel_socket_close (CONNECTION * conn)
116 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
118 close (tunnel->readfd);
119 close (tunnel->writefd);
120 waitpid (tunnel->pid, NULL, 0);
121 FREE (&conn->sockdata);
126 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len)
128 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
131 rc = read (tunnel->readfd, buf, len);
133 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
141 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
144 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
147 rc = write (tunnel->writefd, buf, len);
149 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,