Borland makefile needs to define _WINDOWS; apparently this makefile
[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;
22 int term_width, term_height;
23 void *frontend;
24} *Rlogin;
c91409da 25
51470298 26static void rlogin_size(void *handle, int width, int height);
27
28static void c_write(Rlogin rlogin, char *buf, int len)
32874aea 29{
51470298 30 int backlog = from_backend(rlogin->frontend, 0, buf, len);
31 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
c91409da 32}
33
32874aea 34static int rlogin_closing(Plug plug, char *error_msg, int error_code,
35 int calling_back)
36{
51470298 37 Rlogin rlogin = (Rlogin) plug;
38 if (rlogin->s) {
39 sk_close(rlogin->s);
40 rlogin->s = NULL;
f3ab576e 41 }
7e78000d 42 if (error_msg) {
32874aea 43 /* A socket error has occurred. */
a8327734 44 logevent(rlogin->frontend, error_msg);
247308b5 45 connection_fatal("%s", error_msg);
32874aea 46 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 47 return 0;
48}
49
32874aea 50static int rlogin_receive(Plug plug, int urgent, char *data, int len)
51{
51470298 52 Rlogin rlogin = (Rlogin) plug;
c91409da 53 if (urgent == 2) {
32874aea 54 char c;
55
56 c = *data++;
57 len--;
58 if (c == '\x80')
51470298 59 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
32874aea 60 /*
61 * We should flush everything (aka Telnet SYNCH) if we see
62 * 0x02, and we should turn off and on _local_ flow control
63 * on 0x10 and 0x20 respectively. I'm not convinced it's
64 * worth it...
65 */
8a5fb9e3 66 } else {
32874aea 67 /*
68 * Main rlogin protocol. This is really simple: the first
69 * byte is expected to be NULL and is ignored, and the rest
70 * is printed.
71 */
72 static int firstbyte = 1;
73 if (firstbyte) {
74 if (data[0] == '\0') {
75 data++;
76 len--;
77 }
78 firstbyte = 0;
79 }
2b0c045b 80 if (len > 0)
51470298 81 c_write(rlogin, data, len);
c91409da 82 }
c91409da 83 return 1;
84}
85
3ad9d396 86static void rlogin_sent(Plug plug, int bufsize)
87{
51470298 88 Rlogin rlogin = (Rlogin) plug;
89 rlogin->bufsize = bufsize;
3ad9d396 90}
91
c91409da 92/*
93 * Called to set up the rlogin connection.
94 *
95 * Returns an error message, or NULL on success.
96 *
6e1ebb76 97 * Also places the canonical host name into `realhost'. It must be
98 * freed by the caller.
c91409da 99 */
51470298 100static char *rlogin_init(void *frontend_handle, void **backend_handle,
887035a5 101 char *host, int port, char **realhost, int nodelay)
32874aea 102{
51470298 103 static const struct plug_function_table fn_table = {
7e78000d 104 rlogin_closing,
3ad9d396 105 rlogin_receive,
106 rlogin_sent
51470298 107 };
c91409da 108 SockAddr addr;
109 char *err;
51470298 110 Rlogin rlogin;
c91409da 111
51470298 112 rlogin = smalloc(sizeof(*rlogin));
113 rlogin->fn = &fn_table;
114 rlogin->s = NULL;
115 rlogin->frontend = frontend_handle;
116 rlogin->term_width = cfg.width;
117 rlogin->term_height = cfg.height;
118 *backend_handle = rlogin;
887035a5 119
c91409da 120 /*
121 * Try to find host.
122 */
3ad9d396 123 {
124 char buf[200];
125 sprintf(buf, "Looking up host \"%.170s\"", host);
a8327734 126 logevent(rlogin->frontend, buf);
3ad9d396 127 }
c91409da 128 addr = sk_namelookup(host, realhost);
32874aea 129 if ((err = sk_addr_error(addr)))
c91409da 130 return err;
131
132 if (port < 0)
133 port = 513; /* default rlogin port */
134
135 /*
136 * Open socket.
137 */
3ad9d396 138 {
139 char buf[200], addrbuf[100];
140 sk_getaddr(addr, addrbuf, 100);
141 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
a8327734 142 logevent(rlogin->frontend, buf);
3ad9d396 143 }
51470298 144 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
145 nodelay, (Plug) rlogin);
146 if ((err = sk_socket_error(rlogin->s)))
c91409da 147 return err;
148
149 sk_addr_free(addr);
150
151 /*
152 * Send local username, remote username, terminal/speed
153 */
154
155 {
32874aea 156 char z = 0;
157 char *p;
51470298 158 sk_write(rlogin->s, &z, 1);
159 sk_write(rlogin->s, cfg.localusername, strlen(cfg.localusername));
160 sk_write(rlogin->s, &z, 1);
161 sk_write(rlogin->s, cfg.username, strlen(cfg.username));
162 sk_write(rlogin->s, &z, 1);
163 sk_write(rlogin->s, cfg.termtype, strlen(cfg.termtype));
164 sk_write(rlogin->s, "/", 1);
32874aea 165 for (p = cfg.termspeed; isdigit(*p); p++);
51470298 166 sk_write(rlogin->s, cfg.termspeed, p - cfg.termspeed);
167 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
c91409da 168 }
169
c91409da 170 return NULL;
171}
172
173/*
174 * Called to send data down the rlogin connection.
175 */
51470298 176static int rlogin_send(void *handle, char *buf, int len)
32874aea 177{
51470298 178 Rlogin rlogin = (Rlogin) handle;
179
180 if (rlogin->s == NULL)
b5a460cd 181 return 0;
c91409da 182
51470298 183 rlogin->bufsize = sk_write(rlogin->s, buf, len);
5471d09a 184
51470298 185 return rlogin->bufsize;
5471d09a 186}
187
188/*
189 * Called to query the current socket sendability status.
190 */
51470298 191static int rlogin_sendbuffer(void *handle)
5471d09a 192{
51470298 193 Rlogin rlogin = (Rlogin) handle;
194 return rlogin->bufsize;
c91409da 195}
196
197/*
198 * Called to set the size of the window
199 */
51470298 200static void rlogin_size(void *handle, int width, int height)
32874aea 201{
51470298 202 Rlogin rlogin = (Rlogin) handle;
e418595a 203 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 204
51470298 205 rlogin->term_width = width;
206 rlogin->term_height = height;
f278d6f8 207
51470298 208 if (rlogin->s == NULL)
cf293dd4 209 return;
f278d6f8 210
51470298 211 b[6] = rlogin->term_width >> 8;
212 b[7] = rlogin->term_width & 0xFF;
213 b[4] = rlogin->term_height >> 8;
214 b[5] = rlogin->term_height & 0xFF;
215 rlogin->bufsize = sk_write(rlogin->s, b, 12);
c91409da 216 return;
217}
218
219/*
220 * Send rlogin special codes.
221 */
51470298 222static void rlogin_special(void *handle, Telnet_Special code)
32874aea 223{
c91409da 224 /* Do nothing! */
225 return;
226}
227
51470298 228static Socket rlogin_socket(void *handle)
32874aea 229{
51470298 230 Rlogin rlogin = (Rlogin) handle;
231 return rlogin->s;
32874aea 232}
c91409da 233
51470298 234static int rlogin_sendok(void *handle)
32874aea 235{
68a49acb 236 /* Rlogin rlogin = (Rlogin) handle; */
32874aea 237 return 1;
238}
c91409da 239
51470298 240static void rlogin_unthrottle(void *handle, int backlog)
5471d09a 241{
51470298 242 Rlogin rlogin = (Rlogin) handle;
243 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
5471d09a 244}
245
51470298 246static int rlogin_ldisc(void *handle, int option)
32874aea 247{
68a49acb 248 /* Rlogin rlogin = (Rlogin) handle; */
0965bee0 249 return 0;
250}
251
b9d7bcad 252static void rlogin_provide_ldisc(void *handle, void *ldisc)
253{
254 /* This is a stub. */
255}
256
a8327734 257static void rlogin_provide_logctx(void *handle, void *logctx)
258{
259 /* This is a stub. */
260}
261
51470298 262static int rlogin_exitcode(void *handle)
d8d6c7e5 263{
68a49acb 264 /* Rlogin rlogin = (Rlogin) handle; */
d8d6c7e5 265 /* If we ever implement RSH, we'll probably need to do this properly */
266 return 0;
267}
268
c91409da 269Backend rlogin_backend = {
270 rlogin_init,
271 rlogin_send,
5471d09a 272 rlogin_sendbuffer,
c91409da 273 rlogin_size,
274 rlogin_special,
275 rlogin_socket,
d8d6c7e5 276 rlogin_exitcode,
c91409da 277 rlogin_sendok,
0965bee0 278 rlogin_ldisc,
b9d7bcad 279 rlogin_provide_ldisc,
a8327734 280 rlogin_provide_logctx,
5471d09a 281 rlogin_unthrottle,
c91409da 282 1
283};