]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - raw.c
first pass
[PuTTY.git] / raw.c
1 /*
2  * "Raw" backend.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <limits.h>
8
9 #include "putty.h"
10
11 #ifndef FALSE
12 #define FALSE 0
13 #endif
14 #ifndef TRUE
15 #define TRUE 1
16 #endif
17
18 #define RAW_MAX_BACKLOG 4096
19
20 typedef struct raw_backend_data {
21     const struct plug_function_table *fn;
22     /* the above field _must_ be first in the structure */
23
24     Socket s;
25     int closed_on_socket_error;
26     int bufsize;
27     void *frontend;
28     int sent_console_eof, sent_socket_eof, session_started;
29
30     Conf *conf;
31 } *Raw;
32
33 static void raw_size(void *handle, int width, int height);
34
35 static void c_write(Raw raw, char *buf, int len)
36 {
37     int backlog = from_backend(raw->frontend, 0, buf, len);
38     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
39 }
40
41 static void raw_log(Plug plug, int type, SockAddr addr, int port,
42                     const char *error_msg, int error_code)
43 {
44     Raw raw = (Raw) plug;
45     backend_socket_log(raw->frontend, type, addr, port,
46                        error_msg, error_code, raw->conf, raw->session_started);
47 }
48
49 static void raw_check_close(Raw raw)
50 {
51     /*
52      * Called after we send EOF on either the socket or the console.
53      * Its job is to wind up the session once we have sent EOF on both.
54      */
55     if (raw->sent_console_eof && raw->sent_socket_eof) {
56         if (raw->s) {
57             sk_close(raw->s);
58             raw->s = NULL;
59             notify_remote_exit(raw->frontend);
60         }
61     }
62 }
63
64 static int raw_closing(Plug plug, const char *error_msg, int error_code,
65                        int calling_back)
66 {
67     Raw raw = (Raw) plug;
68
69     if (error_msg) {
70         /* A socket error has occurred. */
71         if (raw->s) {
72             sk_close(raw->s);
73             raw->s = NULL;
74             raw->closed_on_socket_error = TRUE;
75             notify_remote_exit(raw->frontend);
76         }
77         logevent(raw->frontend, error_msg);
78         connection_fatal(raw->frontend, "%s", error_msg);
79     } else {
80         /* Otherwise, the remote side closed the connection normally. */
81         if (!raw->sent_console_eof && from_backend_eof(raw->frontend)) {
82             /*
83              * The front end wants us to close the outgoing side of the
84              * connection as soon as we see EOF from the far end.
85              */
86             if (!raw->sent_socket_eof) {
87                 if (raw->s)
88                     sk_write_eof(raw->s);
89                 raw->sent_socket_eof= TRUE;
90             }
91         }
92         raw->sent_console_eof = TRUE;
93         raw_check_close(raw);
94     }
95     return 0;
96 }
97
98 static int raw_receive(Plug plug, int urgent, char *data, int len)
99 {
100     Raw raw = (Raw) plug;
101     c_write(raw, data, len);
102     /* We count 'session start', for proxy logging purposes, as being
103      * when data is received from the network and printed. */
104     raw->session_started = TRUE;
105     return 1;
106 }
107
108 static void raw_sent(Plug plug, int bufsize)
109 {
110     Raw raw = (Raw) plug;
111     raw->bufsize = bufsize;
112 }
113
114 /*
115  * Called to set up the raw connection.
116  * 
117  * Returns an error message, or NULL on success.
118  *
119  * Also places the canonical host name into `realhost'. It must be
120  * freed by the caller.
121  */
122 static const char *raw_init(void *frontend_handle, void **backend_handle,
123                             Conf *conf,
124                             const char *host, int port, char **realhost,
125                             int nodelay, int keepalive)
126 {
127     static const struct plug_function_table fn_table = {
128         raw_log,
129         raw_closing,
130         raw_receive,
131         raw_sent
132     };
133     SockAddr addr;
134     const char *err;
135     Raw raw;
136     int addressfamily;
137     char *loghost;
138
139     raw = snew(struct raw_backend_data);
140     raw->fn = &fn_table;
141     raw->s = NULL;
142     raw->closed_on_socket_error = FALSE;
143     *backend_handle = raw;
144     raw->sent_console_eof = raw->sent_socket_eof = FALSE;
145     raw->bufsize = 0;
146     raw->session_started = FALSE;
147     raw->conf = conf_copy(conf);
148
149     raw->frontend = frontend_handle;
150
151     addressfamily = conf_get_int(conf, CONF_addressfamily);
152     /*
153      * Try to find host.
154      */
155     addr = name_lookup(host, port, realhost, conf, addressfamily,
156                        raw->frontend, "main connection");
157     if ((err = sk_addr_error(addr)) != NULL) {
158         sk_addr_free(addr);
159         return err;
160     }
161
162     if (port < 0)
163         port = 23;                     /* default telnet port */
164
165     /*
166      * Open socket.
167      */
168     raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
169                             (Plug) raw, conf);
170     if ((err = sk_socket_error(raw->s)) != NULL)
171         return err;
172
173     loghost = conf_get_str(conf, CONF_loghost);
174     if (*loghost) {
175         char *colon;
176
177         sfree(*realhost);
178         *realhost = dupstr(loghost);
179
180         colon = host_strrchr(*realhost, ':');
181         if (colon)
182             *colon++ = '\0';
183     }
184
185     return NULL;
186 }
187
188 static void raw_free(void *handle)
189 {
190     Raw raw = (Raw) handle;
191
192     if (raw->s)
193         sk_close(raw->s);
194     conf_free(raw->conf);
195     sfree(raw);
196 }
197
198 /*
199  * Stub routine (we don't have any need to reconfigure this backend).
200  */
201 static void raw_reconfig(void *handle, Conf *conf)
202 {
203 }
204
205 /*
206  * Called to send data down the raw connection.
207  */
208 static int raw_send(void *handle, const char *buf, int len)
209 {
210     Raw raw = (Raw) handle;
211
212     if (raw->s == NULL)
213         return 0;
214
215     raw->bufsize = sk_write(raw->s, buf, len);
216
217     return raw->bufsize;
218 }
219
220 /*
221  * Called to query the current socket sendability status.
222  */
223 static int raw_sendbuffer(void *handle)
224 {
225     Raw raw = (Raw) handle;
226     return raw->bufsize;
227 }
228
229 /*
230  * Called to set the size of the window
231  */
232 static void raw_size(void *handle, int width, int height)
233 {
234     /* Do nothing! */
235     return;
236 }
237
238 /*
239  * Send raw special codes. We only handle outgoing EOF here.
240  */
241 static void raw_special(void *handle, Telnet_Special code)
242 {
243     Raw raw = (Raw) handle;
244     if (code == TS_EOF && raw->s) {
245         sk_write_eof(raw->s);
246         raw->sent_socket_eof= TRUE;
247         raw_check_close(raw);
248     }
249
250     return;
251 }
252
253 /*
254  * Return a list of the special codes that make sense in this
255  * protocol.
256  */
257 static const struct telnet_special *raw_get_specials(void *handle)
258 {
259     return NULL;
260 }
261
262 static int raw_connected(void *handle)
263 {
264     Raw raw = (Raw) handle;
265     return raw->s != NULL;
266 }
267
268 static int raw_sendok(void *handle)
269 {
270     return 1;
271 }
272
273 static void raw_unthrottle(void *handle, int backlog)
274 {
275     Raw raw = (Raw) handle;
276     sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
277 }
278
279 static int raw_ldisc(void *handle, int option)
280 {
281     if (option == LD_EDIT || option == LD_ECHO)
282         return 1;
283     return 0;
284 }
285
286 static void raw_provide_ldisc(void *handle, void *ldisc)
287 {
288     /* This is a stub. */
289 }
290
291 static void raw_provide_logctx(void *handle, void *logctx)
292 {
293     /* This is a stub. */
294 }
295
296 static int raw_exitcode(void *handle)
297 {
298     Raw raw = (Raw) handle;
299     if (raw->s != NULL)
300         return -1;                     /* still connected */
301     else if (raw->closed_on_socket_error)
302         return INT_MAX;     /* a socket error counts as an unclean exit */
303     else
304         /* Exit codes are a meaningless concept in the Raw protocol */
305         return 0;
306 }
307
308 /*
309  * cfg_info for Raw does nothing at all.
310  */
311 static int raw_cfg_info(void *handle)
312 {
313     return 0;
314 }
315
316 Backend raw_backend = {
317     raw_init,
318     raw_free,
319     raw_reconfig,
320     raw_send,
321     raw_sendbuffer,
322     raw_size,
323     raw_special,
324     raw_get_specials,
325     raw_connected,
326     raw_exitcode,
327     raw_sendok,
328     raw_ldisc,
329     raw_provide_ldisc,
330     raw_provide_logctx,
331     raw_unthrottle,
332     raw_cfg_info,
333     NULL /* test_for_upstream */,
334     "raw",
335     PROT_RAW,
336     0
337 };