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