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