The latest unfix.org IPv6 patch contains these apparently
[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;
129 buf = dupprintf("Looking up host \"%s\"", host);
a8327734 130 logevent(rlogin->frontend, buf);
57356d63 131 sfree(buf);
3ad9d396 132 }
e8fa8f62 133 addr = name_lookup(host, port, realhost, cfg);
f85e6f6e 134 if ((err = sk_addr_error(addr)) != NULL) {
135 sk_addr_free(addr);
c91409da 136 return err;
f85e6f6e 137 }
c91409da 138
139 if (port < 0)
140 port = 513; /* default rlogin port */
141
142 /*
143 * Open socket.
144 */
3ad9d396 145 {
57356d63 146 char *buf, addrbuf[100];
3ad9d396 147 sk_getaddr(addr, addrbuf, 100);
57356d63 148 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
a8327734 149 logevent(rlogin->frontend, buf);
57356d63 150 sfree(buf);
3ad9d396 151 }
51470298 152 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
79bf227b 153 nodelay, keepalive, (Plug) rlogin, cfg);
6f1e7b78 154 if ((err = sk_socket_error(rlogin->s)) != NULL)
c91409da 155 return err;
156
c91409da 157 /*
158 * Send local username, remote username, terminal/speed
159 */
160
161 {
32874aea 162 char z = 0;
163 char *p;
51470298 164 sk_write(rlogin->s, &z, 1);
86916870 165 sk_write(rlogin->s, cfg->localusername,
166 strlen(cfg->localusername));
51470298 167 sk_write(rlogin->s, &z, 1);
86916870 168 sk_write(rlogin->s, cfg->username,
169 strlen(cfg->username));
51470298 170 sk_write(rlogin->s, &z, 1);
86916870 171 sk_write(rlogin->s, cfg->termtype,
172 strlen(cfg->termtype));
51470298 173 sk_write(rlogin->s, "/", 1);
e93ed432 174 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
86916870 175 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
51470298 176 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
c91409da 177 }
178
c91409da 179 return NULL;
180}
181
fabd1805 182static void rlogin_free(void *handle)
183{
184 Rlogin rlogin = (Rlogin) handle;
185
186 if (rlogin->s)
187 sk_close(rlogin->s);
188 sfree(rlogin);
189}
190
c91409da 191/*
86916870 192 * Stub routine (we don't have any need to reconfigure this backend).
193 */
194static void rlogin_reconfig(void *handle, Config *cfg)
195{
196}
197
198/*
c91409da 199 * Called to send data down the rlogin connection.
200 */
51470298 201static int rlogin_send(void *handle, char *buf, int len)
32874aea 202{
51470298 203 Rlogin rlogin = (Rlogin) handle;
204
205 if (rlogin->s == NULL)
b5a460cd 206 return 0;
c91409da 207
51470298 208 rlogin->bufsize = sk_write(rlogin->s, buf, len);
5471d09a 209
51470298 210 return rlogin->bufsize;
5471d09a 211}
212
213/*
214 * Called to query the current socket sendability status.
215 */
51470298 216static int rlogin_sendbuffer(void *handle)
5471d09a 217{
51470298 218 Rlogin rlogin = (Rlogin) handle;
219 return rlogin->bufsize;
c91409da 220}
221
222/*
223 * Called to set the size of the window
224 */
51470298 225static void rlogin_size(void *handle, int width, int height)
32874aea 226{
51470298 227 Rlogin rlogin = (Rlogin) handle;
e418595a 228 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 229
51470298 230 rlogin->term_width = width;
231 rlogin->term_height = height;
f278d6f8 232
51470298 233 if (rlogin->s == NULL)
cf293dd4 234 return;
f278d6f8 235
51470298 236 b[6] = rlogin->term_width >> 8;
237 b[7] = rlogin->term_width & 0xFF;
238 b[4] = rlogin->term_height >> 8;
239 b[5] = rlogin->term_height & 0xFF;
240 rlogin->bufsize = sk_write(rlogin->s, b, 12);
c91409da 241 return;
242}
243
244/*
245 * Send rlogin special codes.
246 */
51470298 247static void rlogin_special(void *handle, Telnet_Special code)
32874aea 248{
c91409da 249 /* Do nothing! */
250 return;
251}
252
125105d1 253/*
254 * Return a list of the special codes that make sense in this
255 * protocol.
256 */
257static const struct telnet_special *rlogin_get_specials(void *handle)
258{
259 return NULL;
260}
261
51470298 262static Socket rlogin_socket(void *handle)
32874aea 263{
51470298 264 Rlogin rlogin = (Rlogin) handle;
265 return rlogin->s;
32874aea 266}
c91409da 267
51470298 268static int rlogin_sendok(void *handle)
32874aea 269{
68a49acb 270 /* Rlogin rlogin = (Rlogin) handle; */
32874aea 271 return 1;
272}
c91409da 273
51470298 274static void rlogin_unthrottle(void *handle, int backlog)
5471d09a 275{
51470298 276 Rlogin rlogin = (Rlogin) handle;
277 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
5471d09a 278}
279
51470298 280static int rlogin_ldisc(void *handle, int option)
32874aea 281{
68a49acb 282 /* Rlogin rlogin = (Rlogin) handle; */
0965bee0 283 return 0;
284}
285
b9d7bcad 286static void rlogin_provide_ldisc(void *handle, void *ldisc)
287{
288 /* This is a stub. */
289}
290
a8327734 291static void rlogin_provide_logctx(void *handle, void *logctx)
292{
293 /* This is a stub. */
294}
295
51470298 296static int rlogin_exitcode(void *handle)
d8d6c7e5 297{
0da1a790 298 Rlogin rlogin = (Rlogin) handle;
299 if (rlogin->s != NULL)
300 return -1; /* still connected */
301 else
302 /* If we ever implement RSH, we'll probably need to do this properly */
303 return 0;
d8d6c7e5 304}
305
c91409da 306Backend rlogin_backend = {
307 rlogin_init,
fabd1805 308 rlogin_free,
86916870 309 rlogin_reconfig,
c91409da 310 rlogin_send,
5471d09a 311 rlogin_sendbuffer,
c91409da 312 rlogin_size,
313 rlogin_special,
125105d1 314 rlogin_get_specials,
c91409da 315 rlogin_socket,
d8d6c7e5 316 rlogin_exitcode,
c91409da 317 rlogin_sendok,
0965bee0 318 rlogin_ldisc,
b9d7bcad 319 rlogin_provide_ldisc,
a8327734 320 rlogin_provide_logctx,
5471d09a 321 rlogin_unthrottle,
c91409da 322 1
323};