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