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