Make our SSH2 maximum packet size into a constant, since it's used in several
[u/mdw/putty] / rlogin.c
CommitLineData
c91409da 1#include <stdio.h>
2#include <stdlib.h>
b6c680d4 3#include <ctype.h>
c91409da 4
5#include "putty.h"
6
7#ifndef FALSE
8#define FALSE 0
9#endif
10#ifndef TRUE
11#define TRUE 1
12#endif
13
5471d09a 14#define RLOGIN_MAX_BACKLOG 4096
15
51470298 16typedef struct rlogin_tag {
17 const struct plug_function_table *fn;
18 /* the above field _must_ be first in the structure */
c91409da 19
51470298 20 Socket s;
21 int bufsize;
c85623f9 22 int firstbyte;
51470298 23 int term_width, term_height;
24 void *frontend;
25} *Rlogin;
c91409da 26
51470298 27static void rlogin_size(void *handle, int width, int height);
28
29static void c_write(Rlogin rlogin, char *buf, int len)
32874aea 30{
51470298 31 int backlog = from_backend(rlogin->frontend, 0, buf, len);
32 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
c91409da 33}
34
cbe2d68f 35static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
32874aea 36 int calling_back)
37{
51470298 38 Rlogin rlogin = (Rlogin) plug;
39 if (rlogin->s) {
40 sk_close(rlogin->s);
41 rlogin->s = NULL;
39934deb 42 notify_remote_exit(rlogin->frontend);
f3ab576e 43 }
7e78000d 44 if (error_msg) {
32874aea 45 /* A socket error has occurred. */
a8327734 46 logevent(rlogin->frontend, error_msg);
971bcc0a 47 connection_fatal(rlogin->frontend, "%s", error_msg);
32874aea 48 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 49 return 0;
50}
51
32874aea 52static int rlogin_receive(Plug plug, int urgent, char *data, int len)
53{
51470298 54 Rlogin rlogin = (Rlogin) plug;
c91409da 55 if (urgent == 2) {
32874aea 56 char c;
57
58 c = *data++;
59 len--;
60 if (c == '\x80')
51470298 61 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
32874aea 62 /*
63 * We should flush everything (aka Telnet SYNCH) if we see
64 * 0x02, and we should turn off and on _local_ flow control
65 * on 0x10 and 0x20 respectively. I'm not convinced it's
66 * worth it...
67 */
8a5fb9e3 68 } else {
32874aea 69 /*
70 * Main rlogin protocol. This is really simple: the first
71 * byte is expected to be NULL and is ignored, and the rest
72 * is printed.
73 */
c85623f9 74 if (rlogin->firstbyte) {
32874aea 75 if (data[0] == '\0') {
76 data++;
77 len--;
78 }
c85623f9 79 rlogin->firstbyte = 0;
32874aea 80 }
2b0c045b 81 if (len > 0)
51470298 82 c_write(rlogin, data, len);
c91409da 83 }
c91409da 84 return 1;
85}
86
3ad9d396 87static void rlogin_sent(Plug plug, int bufsize)
88{
51470298 89 Rlogin rlogin = (Rlogin) plug;
90 rlogin->bufsize = bufsize;
3ad9d396 91}
92
c91409da 93/*
94 * Called to set up the rlogin connection.
95 *
96 * Returns an error message, or NULL on success.
97 *
6e1ebb76 98 * Also places the canonical host name into `realhost'. It must be
99 * freed by the caller.
c91409da 100 */
cbe2d68f 101static const char *rlogin_init(void *frontend_handle, void **backend_handle,
102 Config *cfg,
103 char *host, int port, char **realhost,
79bf227b 104 int nodelay, int keepalive)
32874aea 105{
51470298 106 static const struct plug_function_table fn_table = {
7e78000d 107 rlogin_closing,
3ad9d396 108 rlogin_receive,
109 rlogin_sent
51470298 110 };
c91409da 111 SockAddr addr;
cbe2d68f 112 const char *err;
51470298 113 Rlogin rlogin;
c91409da 114
3d88e64d 115 rlogin = snew(struct rlogin_tag);
51470298 116 rlogin->fn = &fn_table;
117 rlogin->s = NULL;
118 rlogin->frontend = frontend_handle;
86916870 119 rlogin->term_width = cfg->width;
120 rlogin->term_height = cfg->height;
c85623f9 121 rlogin->firstbyte = 1;
51470298 122 *backend_handle = rlogin;
887035a5 123
c91409da 124 /*
125 * Try to find host.
126 */
3ad9d396 127 {
57356d63 128 char *buf;
05581745 129 buf = dupprintf("Looking up host \"%s\"%s", host,
130 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
131 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
132 "")));
a8327734 133 logevent(rlogin->frontend, buf);
57356d63 134 sfree(buf);
3ad9d396 135 }
05581745 136 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
f85e6f6e 137 if ((err = sk_addr_error(addr)) != NULL) {
138 sk_addr_free(addr);
c91409da 139 return err;
f85e6f6e 140 }
c91409da 141
142 if (port < 0)
143 port = 513; /* default rlogin port */
144
145 /*
146 * Open socket.
147 */
3ad9d396 148 {
57356d63 149 char *buf, addrbuf[100];
3ad9d396 150 sk_getaddr(addr, addrbuf, 100);
57356d63 151 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
a8327734 152 logevent(rlogin->frontend, buf);
57356d63 153 sfree(buf);
3ad9d396 154 }
51470298 155 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
79bf227b 156 nodelay, keepalive, (Plug) rlogin, cfg);
6f1e7b78 157 if ((err = sk_socket_error(rlogin->s)) != NULL)
c91409da 158 return err;
159
c91409da 160 /*
161 * Send local username, remote username, terminal/speed
162 */
163
164 {
32874aea 165 char z = 0;
166 char *p;
51470298 167 sk_write(rlogin->s, &z, 1);
86916870 168 sk_write(rlogin->s, cfg->localusername,
169 strlen(cfg->localusername));
51470298 170 sk_write(rlogin->s, &z, 1);
86916870 171 sk_write(rlogin->s, cfg->username,
172 strlen(cfg->username));
51470298 173 sk_write(rlogin->s, &z, 1);
86916870 174 sk_write(rlogin->s, cfg->termtype,
175 strlen(cfg->termtype));
51470298 176 sk_write(rlogin->s, "/", 1);
e93ed432 177 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
86916870 178 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
51470298 179 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
c91409da 180 }
181
c91409da 182 return NULL;
183}
184
fabd1805 185static void rlogin_free(void *handle)
186{
187 Rlogin rlogin = (Rlogin) handle;
188
189 if (rlogin->s)
190 sk_close(rlogin->s);
191 sfree(rlogin);
192}
193
c91409da 194/*
86916870 195 * Stub routine (we don't have any need to reconfigure this backend).
196 */
197static void rlogin_reconfig(void *handle, Config *cfg)
198{
199}
200
201/*
c91409da 202 * Called to send data down the rlogin connection.
203 */
51470298 204static int rlogin_send(void *handle, char *buf, int len)
32874aea 205{
51470298 206 Rlogin rlogin = (Rlogin) handle;
207
208 if (rlogin->s == NULL)
b5a460cd 209 return 0;
c91409da 210
51470298 211 rlogin->bufsize = sk_write(rlogin->s, buf, len);
5471d09a 212
51470298 213 return rlogin->bufsize;
5471d09a 214}
215
216/*
217 * Called to query the current socket sendability status.
218 */
51470298 219static int rlogin_sendbuffer(void *handle)
5471d09a 220{
51470298 221 Rlogin rlogin = (Rlogin) handle;
222 return rlogin->bufsize;
c91409da 223}
224
225/*
226 * Called to set the size of the window
227 */
51470298 228static void rlogin_size(void *handle, int width, int height)
32874aea 229{
51470298 230 Rlogin rlogin = (Rlogin) handle;
e418595a 231 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 232
51470298 233 rlogin->term_width = width;
234 rlogin->term_height = height;
f278d6f8 235
51470298 236 if (rlogin->s == NULL)
cf293dd4 237 return;
f278d6f8 238
51470298 239 b[6] = rlogin->term_width >> 8;
240 b[7] = rlogin->term_width & 0xFF;
241 b[4] = rlogin->term_height >> 8;
242 b[5] = rlogin->term_height & 0xFF;
243 rlogin->bufsize = sk_write(rlogin->s, b, 12);
c91409da 244 return;
245}
246
247/*
248 * Send rlogin special codes.
249 */
51470298 250static void rlogin_special(void *handle, Telnet_Special code)
32874aea 251{
c91409da 252 /* Do nothing! */
253 return;
254}
255
125105d1 256/*
257 * Return a list of the special codes that make sense in this
258 * protocol.
259 */
260static const struct telnet_special *rlogin_get_specials(void *handle)
261{
262 return NULL;
263}
264
51470298 265static Socket rlogin_socket(void *handle)
32874aea 266{
51470298 267 Rlogin rlogin = (Rlogin) handle;
268 return rlogin->s;
32874aea 269}
c91409da 270
51470298 271static int rlogin_sendok(void *handle)
32874aea 272{
68a49acb 273 /* Rlogin rlogin = (Rlogin) handle; */
32874aea 274 return 1;
275}
c91409da 276
51470298 277static void rlogin_unthrottle(void *handle, int backlog)
5471d09a 278{
51470298 279 Rlogin rlogin = (Rlogin) handle;
280 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
5471d09a 281}
282
51470298 283static int rlogin_ldisc(void *handle, int option)
32874aea 284{
68a49acb 285 /* Rlogin rlogin = (Rlogin) handle; */
0965bee0 286 return 0;
287}
288
b9d7bcad 289static void rlogin_provide_ldisc(void *handle, void *ldisc)
290{
291 /* This is a stub. */
292}
293
a8327734 294static void rlogin_provide_logctx(void *handle, void *logctx)
295{
296 /* This is a stub. */
297}
298
51470298 299static int rlogin_exitcode(void *handle)
d8d6c7e5 300{
0da1a790 301 Rlogin rlogin = (Rlogin) handle;
302 if (rlogin->s != NULL)
303 return -1; /* still connected */
304 else
305 /* If we ever implement RSH, we'll probably need to do this properly */
306 return 0;
d8d6c7e5 307}
308
f89c3294 309/*
310 * cfg_info for rlogin does nothing at all.
311 */
312static int rlogin_cfg_info(void *handle)
313{
314 return 0;
315}
316
c91409da 317Backend rlogin_backend = {
318 rlogin_init,
fabd1805 319 rlogin_free,
86916870 320 rlogin_reconfig,
c91409da 321 rlogin_send,
5471d09a 322 rlogin_sendbuffer,
c91409da 323 rlogin_size,
324 rlogin_special,
125105d1 325 rlogin_get_specials,
c91409da 326 rlogin_socket,
d8d6c7e5 327 rlogin_exitcode,
c91409da 328 rlogin_sendok,
0965bee0 329 rlogin_ldisc,
b9d7bcad 330 rlogin_provide_ldisc,
a8327734 331 rlogin_provide_logctx,
5471d09a 332 rlogin_unthrottle,
f89c3294 333 rlogin_cfg_info,
c91409da 334 1
335};