2 * uxproxy.c: Unix implementation of platform_new_connection(),
3 * supporting an OpenSSH-like proxy command.
12 #define DEFINE_PLUG_METHOD_MACROS
18 typedef struct Socket_localproxy_tag * Local_Proxy_Socket;
20 struct Socket_localproxy_tag {
21 const struct socket_function_table *fn;
22 /* the above variable absolutely *must* be the first in this structure */
24 int to_cmd, from_cmd; /* fds */
30 bufchain pending_output_data;
31 bufchain pending_input_data;
32 enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
35 static int localproxy_select_result(int fd, int event);
38 * Trees to look up the pipe fds in.
40 static tree234 *localproxy_by_fromfd, *localproxy_by_tofd;
41 static int localproxy_fromfd_cmp(void *av, void *bv)
43 Local_Proxy_Socket a = (Local_Proxy_Socket)av;
44 Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
45 if (a->from_cmd < b->from_cmd)
47 if (a->from_cmd > b->from_cmd)
51 static int localproxy_fromfd_find(void *av, void *bv)
54 Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
61 static int localproxy_tofd_cmp(void *av, void *bv)
63 Local_Proxy_Socket a = (Local_Proxy_Socket)av;
64 Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
65 if (a->to_cmd < b->to_cmd)
67 if (a->to_cmd > b->to_cmd)
71 static int localproxy_tofd_find(void *av, void *bv)
74 Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
82 /* basic proxy socket functions */
84 static Plug sk_localproxy_plug (Socket s, Plug p)
86 Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
93 static void sk_localproxy_close (Socket s)
95 Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
97 if (ps->to_cmd >= 0) {
98 del234(localproxy_by_tofd, ps);
99 uxsel_del(ps->to_cmd);
103 del234(localproxy_by_fromfd, ps);
104 uxsel_del(ps->from_cmd);
110 static int localproxy_try_send(Local_Proxy_Socket ps)
114 while (bufchain_size(&ps->pending_output_data) > 0) {
118 bufchain_prefix(&ps->pending_output_data, &data, &len);
119 ret = write(ps->to_cmd, data, len);
120 if (ret < 0 && errno != EWOULDBLOCK) {
121 /* We're inside the Unix frontend here, so we know
122 * that the frontend handle is unnecessary. */
123 logevent(NULL, strerror(errno));
124 fatalbox("%s", strerror(errno));
125 } else if (ret <= 0) {
128 bufchain_consume(&ps->pending_output_data, ret);
133 if (ps->outgoingeof == EOF_PENDING) {
134 del234(localproxy_by_tofd, ps);
136 uxsel_del(ps->to_cmd);
138 ps->outgoingeof = EOF_SENT;
141 if (bufchain_size(&ps->pending_output_data) == 0)
142 uxsel_del(ps->to_cmd);
144 uxsel_set(ps->to_cmd, 2, localproxy_select_result);
149 static int sk_localproxy_write (Socket s, const char *data, int len)
151 Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
153 assert(ps->outgoingeof == EOF_NO);
155 bufchain_add(&ps->pending_output_data, data, len);
157 localproxy_try_send(ps);
159 return bufchain_size(&ps->pending_output_data);
162 static int sk_localproxy_write_oob (Socket s, const char *data, int len)
165 * oob data is treated as inband; nasty, but nothing really
168 return sk_localproxy_write(s, data, len);
171 static void sk_localproxy_write_eof (Socket s)
173 Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
175 assert(ps->outgoingeof == EOF_NO);
176 ps->outgoingeof = EOF_PENDING;
178 localproxy_try_send(ps);
181 static void sk_localproxy_flush (Socket s)
183 /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
187 static void sk_localproxy_set_frozen (Socket s, int is_frozen)
189 Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
192 uxsel_del(ps->from_cmd);
194 uxsel_set(ps->from_cmd, 1, localproxy_select_result);
197 static const char * sk_localproxy_socket_error (Socket s)
199 Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
203 static int localproxy_select_result(int fd, int event)
205 Local_Proxy_Socket s;
209 if (!(s = find234(localproxy_by_fromfd, &fd, localproxy_fromfd_find)) &&
210 !(s = find234(localproxy_by_tofd, &fd, localproxy_tofd_find)) )
211 return 1; /* boggle */
214 assert(fd == s->from_cmd);
215 ret = read(fd, buf, sizeof(buf));
217 return plug_closing(s->plug, strerror(errno), errno, 0);
218 } else if (ret == 0) {
219 return plug_closing(s->plug, NULL, 0, 0);
221 return plug_receive(s->plug, 0, buf, ret);
223 } else if (event == 2) {
224 assert(fd == s->to_cmd);
225 if (localproxy_try_send(s))
226 plug_sent(s->plug, bufchain_size(&s->pending_output_data));
233 Socket platform_new_connection(SockAddr addr, const char *hostname,
234 int port, int privport,
235 int oobinline, int nodelay, int keepalive,
236 Plug plug, Conf *conf)
240 static const struct socket_function_table socket_fn_table = {
244 sk_localproxy_write_oob,
245 sk_localproxy_write_eof,
247 sk_localproxy_set_frozen,
248 sk_localproxy_socket_error,
249 NULL, /* peer_info */
252 Local_Proxy_Socket ret;
253 int to_cmd_pipe[2], from_cmd_pipe[2], pid;
255 if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
258 cmd = format_telnet_command(addr, port, conf);
260 ret = snew(struct Socket_localproxy_tag);
261 ret->fn = &socket_fn_table;
264 ret->outgoingeof = EOF_NO;
266 bufchain_init(&ret->pending_input_data);
267 bufchain_init(&ret->pending_output_data);
270 * Create the pipes to the proxy command, and spawn the proxy
273 if (pipe(to_cmd_pipe) < 0 ||
274 pipe(from_cmd_pipe) < 0) {
275 ret->error = dupprintf("pipe: %s", strerror(errno));
279 cloexec(to_cmd_pipe[1]);
280 cloexec(from_cmd_pipe[0]);
285 ret->error = dupprintf("fork: %s", strerror(errno));
288 } else if (pid == 0) {
291 dup2(to_cmd_pipe[0], 0);
292 dup2(from_cmd_pipe[1], 1);
293 close(to_cmd_pipe[0]);
294 close(from_cmd_pipe[1]);
297 execl("/bin/sh", "sh", "-c", cmd, (void *)NULL);
303 close(to_cmd_pipe[0]);
304 close(from_cmd_pipe[1]);
306 ret->to_cmd = to_cmd_pipe[1];
307 ret->from_cmd = from_cmd_pipe[0];
309 if (!localproxy_by_fromfd)
310 localproxy_by_fromfd = newtree234(localproxy_fromfd_cmp);
311 if (!localproxy_by_tofd)
312 localproxy_by_tofd = newtree234(localproxy_tofd_cmp);
314 add234(localproxy_by_fromfd, ret);
315 add234(localproxy_by_tofd, ret);
317 uxsel_set(ret->from_cmd, 1, localproxy_select_result);
319 /* We are responsible for this and don't need it any more */