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