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