Add double width/height support. This passes all the relevant parts of
[sgt/putty] / rlogin.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
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
14#define RLOGIN_MAX_BACKLOG 4096
15
16typedef struct rlogin_tag {
17 const struct plug_function_table *fn;
18 /* the above field _must_ be first in the structure */
19
20 Socket s;
21 int bufsize;
22 int term_width, term_height;
23 void *frontend;
24} *Rlogin;
25
26static void rlogin_size(void *handle, int width, int height);
27
28static void c_write(Rlogin rlogin, char *buf, int len)
29{
30 int backlog = from_backend(rlogin->frontend, 0, buf, len);
31 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
32}
33
34static int rlogin_closing(Plug plug, char *error_msg, int error_code,
35 int calling_back)
36{
37 Rlogin rlogin = (Rlogin) plug;
38 if (rlogin->s) {
39 sk_close(rlogin->s);
40 rlogin->s = NULL;
41 }
42 if (error_msg) {
43 /* A socket error has occurred. */
44 logevent(rlogin->frontend, error_msg);
45 connection_fatal("%s", error_msg);
46 } /* Otherwise, the remote side closed the connection normally. */
47 return 0;
48}
49
50static int rlogin_receive(Plug plug, int urgent, char *data, int len)
51{
52 Rlogin rlogin = (Rlogin) plug;
53 if (urgent == 2) {
54 char c;
55
56 c = *data++;
57 len--;
58 if (c == '\x80')
59 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
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 */
66 } else {
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 }
80 if (len > 0)
81 c_write(rlogin, data, len);
82 }
83 return 1;
84}
85
86static void rlogin_sent(Plug plug, int bufsize)
87{
88 Rlogin rlogin = (Rlogin) plug;
89 rlogin->bufsize = bufsize;
90}
91
92/*
93 * Called to set up the rlogin connection.
94 *
95 * Returns an error message, or NULL on success.
96 *
97 * Also places the canonical host name into `realhost'. It must be
98 * freed by the caller.
99 */
100static char *rlogin_init(void *frontend_handle, void **backend_handle,
101 Config *cfg,
102 char *host, int port, char **realhost, int nodelay)
103{
104 static const struct plug_function_table fn_table = {
105 rlogin_closing,
106 rlogin_receive,
107 rlogin_sent
108 };
109 SockAddr addr;
110 char *err;
111 Rlogin rlogin;
112
113 rlogin = smalloc(sizeof(*rlogin));
114 rlogin->fn = &fn_table;
115 rlogin->s = NULL;
116 rlogin->frontend = frontend_handle;
117 rlogin->term_width = cfg->width;
118 rlogin->term_height = cfg->height;
119 *backend_handle = rlogin;
120
121 /*
122 * Try to find host.
123 */
124 {
125 char *buf;
126 buf = dupprintf("Looking up host \"%s\"", host);
127 logevent(rlogin->frontend, buf);
128 sfree(buf);
129 }
130 addr = name_lookup(host, port, realhost, cfg);
131 if ((err = sk_addr_error(addr)) != NULL)
132 return err;
133
134 if (port < 0)
135 port = 513; /* default rlogin port */
136
137 /*
138 * Open socket.
139 */
140 {
141 char *buf, addrbuf[100];
142 sk_getaddr(addr, addrbuf, 100);
143 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
144 logevent(rlogin->frontend, buf);
145 sfree(buf);
146 }
147 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
148 nodelay, (Plug) rlogin, cfg);
149 if ((err = sk_socket_error(rlogin->s)) != NULL)
150 return err;
151
152 sk_addr_free(addr);
153
154 /*
155 * Send local username, remote username, terminal/speed
156 */
157
158 {
159 char z = 0;
160 char *p;
161 sk_write(rlogin->s, &z, 1);
162 sk_write(rlogin->s, cfg->localusername,
163 strlen(cfg->localusername));
164 sk_write(rlogin->s, &z, 1);
165 sk_write(rlogin->s, cfg->username,
166 strlen(cfg->username));
167 sk_write(rlogin->s, &z, 1);
168 sk_write(rlogin->s, cfg->termtype,
169 strlen(cfg->termtype));
170 sk_write(rlogin->s, "/", 1);
171 for (p = cfg->termspeed; isdigit(*p); p++) continue;
172 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
173 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
174 }
175
176 return NULL;
177}
178
179/*
180 * Stub routine (we don't have any need to reconfigure this backend).
181 */
182static void rlogin_reconfig(void *handle, Config *cfg)
183{
184}
185
186/*
187 * Called to send data down the rlogin connection.
188 */
189static int rlogin_send(void *handle, char *buf, int len)
190{
191 Rlogin rlogin = (Rlogin) handle;
192
193 if (rlogin->s == NULL)
194 return 0;
195
196 rlogin->bufsize = sk_write(rlogin->s, buf, len);
197
198 return rlogin->bufsize;
199}
200
201/*
202 * Called to query the current socket sendability status.
203 */
204static int rlogin_sendbuffer(void *handle)
205{
206 Rlogin rlogin = (Rlogin) handle;
207 return rlogin->bufsize;
208}
209
210/*
211 * Called to set the size of the window
212 */
213static void rlogin_size(void *handle, int width, int height)
214{
215 Rlogin rlogin = (Rlogin) handle;
216 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
217
218 rlogin->term_width = width;
219 rlogin->term_height = height;
220
221 if (rlogin->s == NULL)
222 return;
223
224 b[6] = rlogin->term_width >> 8;
225 b[7] = rlogin->term_width & 0xFF;
226 b[4] = rlogin->term_height >> 8;
227 b[5] = rlogin->term_height & 0xFF;
228 rlogin->bufsize = sk_write(rlogin->s, b, 12);
229 return;
230}
231
232/*
233 * Send rlogin special codes.
234 */
235static void rlogin_special(void *handle, Telnet_Special code)
236{
237 /* Do nothing! */
238 return;
239}
240
241static Socket rlogin_socket(void *handle)
242{
243 Rlogin rlogin = (Rlogin) handle;
244 return rlogin->s;
245}
246
247static int rlogin_sendok(void *handle)
248{
249 /* Rlogin rlogin = (Rlogin) handle; */
250 return 1;
251}
252
253static void rlogin_unthrottle(void *handle, int backlog)
254{
255 Rlogin rlogin = (Rlogin) handle;
256 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
257}
258
259static int rlogin_ldisc(void *handle, int option)
260{
261 /* Rlogin rlogin = (Rlogin) handle; */
262 return 0;
263}
264
265static void rlogin_provide_ldisc(void *handle, void *ldisc)
266{
267 /* This is a stub. */
268}
269
270static void rlogin_provide_logctx(void *handle, void *logctx)
271{
272 /* This is a stub. */
273}
274
275static int rlogin_exitcode(void *handle)
276{
277 /* Rlogin rlogin = (Rlogin) handle; */
278 /* If we ever implement RSH, we'll probably need to do this properly */
279 return 0;
280}
281
282Backend rlogin_backend = {
283 rlogin_init,
284 rlogin_reconfig,
285 rlogin_send,
286 rlogin_sendbuffer,
287 rlogin_size,
288 rlogin_special,
289 rlogin_socket,
290 rlogin_exitcode,
291 rlogin_sendok,
292 rlogin_ldisc,
293 rlogin_provide_ldisc,
294 rlogin_provide_logctx,
295 rlogin_unthrottle,
296 1
297};