]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winproxy.c
Revamp of EOF handling in all network connections, pipes and other
[PuTTY.git] / windows / winproxy.c
1 /*
2  * winproxy.c: Windows implementation of platform_new_connection(),
3  * supporting an OpenSSH-like proxy command via the winhandl.c
4  * mechanism.
5  */
6
7 #include <stdio.h>
8 #include <assert.h>
9
10 #define DEFINE_PLUG_METHOD_MACROS
11 #include "tree234.h"
12 #include "putty.h"
13 #include "network.h"
14 #include "proxy.h"
15
16 typedef struct Socket_localproxy_tag *Local_Proxy_Socket;
17
18 struct Socket_localproxy_tag {
19     const struct socket_function_table *fn;
20     /* the above variable absolutely *must* be the first in this structure */
21
22     HANDLE to_cmd_H, from_cmd_H;
23     struct handle *to_cmd_h, *from_cmd_h;
24
25     char *error;
26
27     Plug plug;
28
29     void *privptr;
30 };
31
32 int localproxy_gotdata(struct handle *h, void *data, int len)
33 {
34     Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
35
36     if (len < 0) {
37         return plug_closing(ps->plug, "Read error from local proxy command",
38                             0, 0);
39     } else if (len == 0) {
40         return plug_closing(ps->plug, NULL, 0, 0);
41     } else {
42         return plug_receive(ps->plug, 0, data, len);
43     }
44 }
45
46 void localproxy_sentdata(struct handle *h, int new_backlog)
47 {
48     Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
49     
50     plug_sent(ps->plug, new_backlog);
51 }
52
53 static Plug sk_localproxy_plug (Socket s, Plug p)
54 {
55     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
56     Plug ret = ps->plug;
57     if (p)
58         ps->plug = p;
59     return ret;
60 }
61
62 static void sk_localproxy_close (Socket s)
63 {
64     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
65
66     handle_free(ps->to_cmd_h);
67     handle_free(ps->from_cmd_h);
68     CloseHandle(ps->to_cmd_H);
69     CloseHandle(ps->from_cmd_H);
70
71     sfree(ps);
72 }
73
74 static int sk_localproxy_write (Socket s, const char *data, int len)
75 {
76     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
77
78     return handle_write(ps->to_cmd_h, data, len);
79 }
80
81 static int sk_localproxy_write_oob(Socket s, const char *data, int len)
82 {
83     /*
84      * oob data is treated as inband; nasty, but nothing really
85      * better we can do
86      */
87     return sk_localproxy_write(s, data, len);
88 }
89
90 static void sk_localproxy_write_eof(Socket s)
91 {
92     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
93
94     handle_write_eof(ps->to_cmd_h);
95 }
96
97 static void sk_localproxy_flush(Socket s)
98 {
99     /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
100     /* do nothing */
101 }
102
103 static void sk_localproxy_set_private_ptr(Socket s, void *ptr)
104 {
105     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
106     ps->privptr = ptr;
107 }
108
109 static void *sk_localproxy_get_private_ptr(Socket s)
110 {
111     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
112     return ps->privptr;
113 }
114
115 static void sk_localproxy_set_frozen(Socket s, int is_frozen)
116 {
117     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
118
119     /*
120      * FIXME
121      */
122 }
123
124 static const char *sk_localproxy_socket_error(Socket s)
125 {
126     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
127     return ps->error;
128 }
129
130 Socket platform_new_connection(SockAddr addr, char *hostname,
131                                int port, int privport,
132                                int oobinline, int nodelay, int keepalive,
133                                Plug plug, Conf *conf)
134 {
135     char *cmd;
136
137     static const struct socket_function_table socket_fn_table = {
138         sk_localproxy_plug,
139         sk_localproxy_close,
140         sk_localproxy_write,
141         sk_localproxy_write_oob,
142         sk_localproxy_write_eof,
143         sk_localproxy_flush,
144         sk_localproxy_set_private_ptr,
145         sk_localproxy_get_private_ptr,
146         sk_localproxy_set_frozen,
147         sk_localproxy_socket_error
148     };
149
150     Local_Proxy_Socket ret;
151     HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;
152     SECURITY_ATTRIBUTES sa;
153     STARTUPINFO si;
154     PROCESS_INFORMATION pi;
155
156     if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
157         return NULL;
158
159     cmd = format_telnet_command(addr, port, conf);
160
161     {
162         char *msg = dupprintf("Starting local proxy command: %s", cmd);
163         /* We're allowed to pass NULL here, because we're part of the Windows
164          * front end so we know logevent doesn't expect any data. */
165         logevent(NULL, msg);
166         sfree(msg);
167     }
168
169     ret = snew(struct Socket_localproxy_tag);
170     ret->fn = &socket_fn_table;
171     ret->plug = plug;
172     ret->error = NULL;
173
174     /*
175      * Create the pipes to the proxy command, and spawn the proxy
176      * command process.
177      */
178     sa.nLength = sizeof(sa);
179     sa.lpSecurityDescriptor = NULL;    /* default */
180     sa.bInheritHandle = TRUE;
181     if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
182         ret->error = dupprintf("Unable to create pipes for proxy command");
183         return (Socket)ret;
184     }
185
186     if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
187         CloseHandle(us_from_cmd);
188         CloseHandle(cmd_to_us);
189         ret->error = dupprintf("Unable to create pipes for proxy command");
190         return (Socket)ret;
191     }
192
193     SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
194     SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
195
196     si.cb = sizeof(si);
197     si.lpReserved = NULL;
198     si.lpDesktop = NULL;
199     si.lpTitle = NULL;
200     si.dwFlags = STARTF_USESTDHANDLES;
201     si.cbReserved2 = 0;
202     si.lpReserved2 = NULL;
203     si.hStdInput = cmd_from_us;
204     si.hStdOutput = cmd_to_us;
205     si.hStdError = NULL;
206     CreateProcess(NULL, cmd, NULL, NULL, TRUE,
207                   CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
208                   NULL, NULL, &si, &pi);
209
210     sfree(cmd);
211
212     CloseHandle(cmd_from_us);
213     CloseHandle(cmd_to_us);
214
215     ret->to_cmd_H = us_to_cmd;
216     ret->from_cmd_H = us_from_cmd;
217
218     ret->from_cmd_h = handle_input_new(ret->from_cmd_H, localproxy_gotdata,
219                                        ret, 0);
220     ret->to_cmd_h = handle_output_new(ret->to_cmd_H, localproxy_sentdata,
221                                       ret, 0);
222
223     /* We are responsible for this and don't need it any more */
224     sk_addr_free(addr);
225
226     return (Socket) ret;
227 }