]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winproxy.c
Post-release destabilisation! Completely remove the struct type
[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_flush(Socket s)
91 {
92     /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
93     /* do nothing */
94 }
95
96 static void sk_localproxy_set_private_ptr(Socket s, void *ptr)
97 {
98     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
99     ps->privptr = ptr;
100 }
101
102 static void *sk_localproxy_get_private_ptr(Socket s)
103 {
104     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
105     return ps->privptr;
106 }
107
108 static void sk_localproxy_set_frozen(Socket s, int is_frozen)
109 {
110     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
111
112     /*
113      * FIXME
114      */
115 }
116
117 static const char *sk_localproxy_socket_error(Socket s)
118 {
119     Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
120     return ps->error;
121 }
122
123 Socket platform_new_connection(SockAddr addr, char *hostname,
124                                int port, int privport,
125                                int oobinline, int nodelay, int keepalive,
126                                Plug plug, Conf *conf)
127 {
128     char *cmd;
129
130     static const struct socket_function_table socket_fn_table = {
131         sk_localproxy_plug,
132         sk_localproxy_close,
133         sk_localproxy_write,
134         sk_localproxy_write_oob,
135         sk_localproxy_flush,
136         sk_localproxy_set_private_ptr,
137         sk_localproxy_get_private_ptr,
138         sk_localproxy_set_frozen,
139         sk_localproxy_socket_error
140     };
141
142     Local_Proxy_Socket ret;
143     HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;
144     SECURITY_ATTRIBUTES sa;
145     STARTUPINFO si;
146     PROCESS_INFORMATION pi;
147
148     if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
149         return NULL;
150
151     cmd = format_telnet_command(addr, port, conf);
152
153     {
154         char *msg = dupprintf("Starting local proxy command: %s", cmd);
155         /* We're allowed to pass NULL here, because we're part of the Windows
156          * front end so we know logevent doesn't expect any data. */
157         logevent(NULL, msg);
158         sfree(msg);
159     }
160
161     ret = snew(struct Socket_localproxy_tag);
162     ret->fn = &socket_fn_table;
163     ret->plug = plug;
164     ret->error = NULL;
165
166     /*
167      * Create the pipes to the proxy command, and spawn the proxy
168      * command process.
169      */
170     sa.nLength = sizeof(sa);
171     sa.lpSecurityDescriptor = NULL;    /* default */
172     sa.bInheritHandle = TRUE;
173     if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
174         ret->error = dupprintf("Unable to create pipes for proxy command");
175         return (Socket)ret;
176     }
177
178     if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
179         CloseHandle(us_from_cmd);
180         CloseHandle(cmd_to_us);
181         ret->error = dupprintf("Unable to create pipes for proxy command");
182         return (Socket)ret;
183     }
184
185     SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
186     SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
187
188     si.cb = sizeof(si);
189     si.lpReserved = NULL;
190     si.lpDesktop = NULL;
191     si.lpTitle = NULL;
192     si.dwFlags = STARTF_USESTDHANDLES;
193     si.cbReserved2 = 0;
194     si.lpReserved2 = NULL;
195     si.hStdInput = cmd_from_us;
196     si.hStdOutput = cmd_to_us;
197     si.hStdError = NULL;
198     CreateProcess(NULL, cmd, NULL, NULL, TRUE,
199                   CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
200                   NULL, NULL, &si, &pi);
201
202     sfree(cmd);
203
204     CloseHandle(cmd_from_us);
205     CloseHandle(cmd_to_us);
206
207     ret->to_cmd_H = us_to_cmd;
208     ret->from_cmd_H = us_from_cmd;
209
210     ret->from_cmd_h = handle_input_new(ret->from_cmd_H, localproxy_gotdata,
211                                        ret, 0);
212     ret->to_cmd_h = handle_output_new(ret->to_cmd_H, localproxy_sentdata,
213                                       ret, 0);
214
215     /* We are responsible for this and don't need it any more */
216     sk_addr_free(addr);
217
218     return (Socket) ret;
219 }