Make our SSH2 maximum packet size into a constant, since it's used in several
[u/mdw/putty] / raw.c
CommitLineData
5e1a8e27 1#include <stdio.h>
2#include <stdlib.h>
5e1a8e27 3
4#include "putty.h"
5
6#ifndef FALSE
7#define FALSE 0
8#endif
9#ifndef TRUE
10#define TRUE 1
11#endif
12
5471d09a 13#define RAW_MAX_BACKLOG 4096
14
51470298 15typedef struct raw_backend_data {
16 const struct plug_function_table *fn;
17 /* the above field _must_ be first in the structure */
5e1a8e27 18
51470298 19 Socket s;
20 int bufsize;
21 void *frontend;
22} *Raw;
5e1a8e27 23
51470298 24static void raw_size(void *handle, int width, int height);
25
26static void c_write(Raw raw, char *buf, int len)
32874aea 27{
51470298 28 int backlog = from_backend(raw->frontend, 0, buf, len);
29 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5e1a8e27 30}
31
cbe2d68f 32static int raw_closing(Plug plug, const char *error_msg, int error_code,
32874aea 33 int calling_back)
34{
51470298 35 Raw raw = (Raw) plug;
36
37 if (raw->s) {
38 sk_close(raw->s);
39 raw->s = NULL;
39934deb 40 notify_remote_exit(raw->frontend);
f3ab576e 41 }
7e78000d 42 if (error_msg) {
32874aea 43 /* A socket error has occurred. */
a8327734 44 logevent(raw->frontend, error_msg);
971bcc0a 45 connection_fatal(raw->frontend, "%s", error_msg);
32874aea 46 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 47 return 0;
48}
49
32874aea 50static int raw_receive(Plug plug, int urgent, char *data, int len)
51{
51470298 52 Raw raw = (Raw) plug;
53 c_write(raw, data, len);
8df7a775 54 return 1;
5e1a8e27 55}
56
3ad9d396 57static void raw_sent(Plug plug, int bufsize)
58{
51470298 59 Raw raw = (Raw) plug;
60 raw->bufsize = bufsize;
3ad9d396 61}
62
5e1a8e27 63/*
8df7a775 64 * Called to set up the raw connection.
65 *
5e1a8e27 66 * Returns an error message, or NULL on success.
67 *
6e1ebb76 68 * Also places the canonical host name into `realhost'. It must be
69 * freed by the caller.
5e1a8e27 70 */
cbe2d68f 71static const char *raw_init(void *frontend_handle, void **backend_handle,
72 Config *cfg,
79bf227b 73 char *host, int port, char **realhost, int nodelay,
74 int keepalive)
32874aea 75{
51470298 76 static const struct plug_function_table fn_table = {
7e78000d 77 raw_closing,
3ad9d396 78 raw_receive,
79 raw_sent
51470298 80 };
8df7a775 81 SockAddr addr;
cbe2d68f 82 const char *err;
51470298 83 Raw raw;
5e1a8e27 84
3d88e64d 85 raw = snew(struct raw_backend_data);
51470298 86 raw->fn = &fn_table;
87 raw->s = NULL;
88 *backend_handle = raw;
89
90 raw->frontend = frontend_handle;
887035a5 91
5e1a8e27 92 /*
93 * Try to find host.
94 */
3ad9d396 95 {
57356d63 96 char *buf;
05581745 97 buf = dupprintf("Looking up host \"%s\"%s", host,
98 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
99 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
100 "")));
a8327734 101 logevent(raw->frontend, buf);
57356d63 102 sfree(buf);
3ad9d396 103 }
05581745 104 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
f85e6f6e 105 if ((err = sk_addr_error(addr)) != NULL) {
106 sk_addr_free(addr);
8df7a775 107 return err;
f85e6f6e 108 }
5e1a8e27 109
110 if (port < 0)
111 port = 23; /* default telnet port */
112
113 /*
114 * Open socket.
115 */
3ad9d396 116 {
57356d63 117 char *buf, addrbuf[100];
3ad9d396 118 sk_getaddr(addr, addrbuf, 100);
57356d63 119 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
a8327734 120 logevent(raw->frontend, buf);
57356d63 121 sfree(buf);
3ad9d396 122 }
79bf227b 123 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive,
e8fa8f62 124 (Plug) raw, cfg);
6f1e7b78 125 if ((err = sk_socket_error(raw->s)) != NULL)
8df7a775 126 return err;
5e1a8e27 127
5e1a8e27 128 return NULL;
129}
130
fabd1805 131static void raw_free(void *handle)
132{
133 Raw raw = (Raw) handle;
134
135 if (raw->s)
136 sk_close(raw->s);
137 sfree(raw);
138}
139
5e1a8e27 140/*
86916870 141 * Stub routine (we don't have any need to reconfigure this backend).
142 */
143static void raw_reconfig(void *handle, Config *cfg)
144{
145}
146
147/*
5e1a8e27 148 * Called to send data down the raw connection.
149 */
51470298 150static int raw_send(void *handle, char *buf, int len)
32874aea 151{
51470298 152 Raw raw = (Raw) handle;
153
154 if (raw->s == NULL)
b5a460cd 155 return 0;
5e1a8e27 156
51470298 157 raw->bufsize = sk_write(raw->s, buf, len);
5471d09a 158
51470298 159 return raw->bufsize;
5471d09a 160}
161
162/*
163 * Called to query the current socket sendability status.
164 */
51470298 165static int raw_sendbuffer(void *handle)
5471d09a 166{
51470298 167 Raw raw = (Raw) handle;
168 return raw->bufsize;
5e1a8e27 169}
170
171/*
172 * Called to set the size of the window
173 */
51470298 174static void raw_size(void *handle, int width, int height)
32874aea 175{
5e1a8e27 176 /* Do nothing! */
177 return;
178}
179
180/*
181 * Send raw special codes.
182 */
51470298 183static void raw_special(void *handle, Telnet_Special code)
32874aea 184{
5e1a8e27 185 /* Do nothing! */
186 return;
187}
188
125105d1 189/*
190 * Return a list of the special codes that make sense in this
191 * protocol.
192 */
193static const struct telnet_special *raw_get_specials(void *handle)
194{
195 return NULL;
196}
197
51470298 198static Socket raw_socket(void *handle)
32874aea 199{
51470298 200 Raw raw = (Raw) handle;
201 return raw->s;
32874aea 202}
8ccc75b0 203
51470298 204static int raw_sendok(void *handle)
32874aea 205{
206 return 1;
207}
4017be6d 208
51470298 209static void raw_unthrottle(void *handle, int backlog)
5471d09a 210{
51470298 211 Raw raw = (Raw) handle;
212 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5471d09a 213}
214
51470298 215static int raw_ldisc(void *handle, int option)
32874aea 216{
0965bee0 217 if (option == LD_EDIT || option == LD_ECHO)
32874aea 218 return 1;
0965bee0 219 return 0;
220}
221
b9d7bcad 222static void raw_provide_ldisc(void *handle, void *ldisc)
223{
224 /* This is a stub. */
225}
226
a8327734 227static void raw_provide_logctx(void *handle, void *logctx)
228{
229 /* This is a stub. */
230}
231
51470298 232static int raw_exitcode(void *handle)
d8d6c7e5 233{
0da1a790 234 Raw raw = (Raw) handle;
235 if (raw->s != NULL)
236 return -1; /* still connected */
237 else
238 /* Exit codes are a meaningless concept in the Raw protocol */
239 return 0;
d8d6c7e5 240}
241
f89c3294 242/*
243 * cfg_info for Raw does nothing at all.
244 */
245static int raw_cfg_info(void *handle)
246{
247 return 0;
248}
249
5e1a8e27 250Backend raw_backend = {
251 raw_init,
fabd1805 252 raw_free,
86916870 253 raw_reconfig,
5e1a8e27 254 raw_send,
5471d09a 255 raw_sendbuffer,
5e1a8e27 256 raw_size,
4017be6d 257 raw_special,
125105d1 258 raw_get_specials,
8ccc75b0 259 raw_socket,
d8d6c7e5 260 raw_exitcode,
97db3be4 261 raw_sendok,
0965bee0 262 raw_ldisc,
b9d7bcad 263 raw_provide_ldisc,
a8327734 264 raw_provide_logctx,
5471d09a 265 raw_unthrottle,
f89c3294 266 raw_cfg_info,
97db3be4 267 1
5e1a8e27 268};