Control of 'addr' is now handed over to {platform_,}new_connection() and
[u/mdw/putty] / rlogin.c
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
16 typedef 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 firstbyte;
23 int term_width, term_height;
24 void *frontend;
25 } *Rlogin;
26
27 static void rlogin_size(void *handle, int width, int height);
28
29 static void c_write(Rlogin rlogin, char *buf, int len)
30 {
31 int backlog = from_backend(rlogin->frontend, 0, buf, len);
32 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
33 }
34
35 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
36 int calling_back)
37 {
38 Rlogin rlogin = (Rlogin) plug;
39 if (rlogin->s) {
40 sk_close(rlogin->s);
41 rlogin->s = NULL;
42 }
43 if (error_msg) {
44 /* A socket error has occurred. */
45 logevent(rlogin->frontend, error_msg);
46 connection_fatal(rlogin->frontend, "%s", error_msg);
47 } /* Otherwise, the remote side closed the connection normally. */
48 return 0;
49 }
50
51 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
52 {
53 Rlogin rlogin = (Rlogin) plug;
54 if (urgent == 2) {
55 char c;
56
57 c = *data++;
58 len--;
59 if (c == '\x80')
60 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
61 /*
62 * We should flush everything (aka Telnet SYNCH) if we see
63 * 0x02, and we should turn off and on _local_ flow control
64 * on 0x10 and 0x20 respectively. I'm not convinced it's
65 * worth it...
66 */
67 } else {
68 /*
69 * Main rlogin protocol. This is really simple: the first
70 * byte is expected to be NULL and is ignored, and the rest
71 * is printed.
72 */
73 if (rlogin->firstbyte) {
74 if (data[0] == '\0') {
75 data++;
76 len--;
77 }
78 rlogin->firstbyte = 0;
79 }
80 if (len > 0)
81 c_write(rlogin, data, len);
82 }
83 return 1;
84 }
85
86 static 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 */
100 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
101 Config *cfg,
102 char *host, int port, char **realhost,
103 int nodelay)
104 {
105 static const struct plug_function_table fn_table = {
106 rlogin_closing,
107 rlogin_receive,
108 rlogin_sent
109 };
110 SockAddr addr;
111 const char *err;
112 Rlogin rlogin;
113
114 rlogin = snew(struct rlogin_tag);
115 rlogin->fn = &fn_table;
116 rlogin->s = NULL;
117 rlogin->frontend = frontend_handle;
118 rlogin->term_width = cfg->width;
119 rlogin->term_height = cfg->height;
120 rlogin->firstbyte = 1;
121 *backend_handle = rlogin;
122
123 /*
124 * Try to find host.
125 */
126 {
127 char *buf;
128 buf = dupprintf("Looking up host \"%s\"", host);
129 logevent(rlogin->frontend, buf);
130 sfree(buf);
131 }
132 addr = name_lookup(host, port, realhost, cfg);
133 if ((err = sk_addr_error(addr)) != NULL) {
134 sk_addr_free(addr);
135 return err;
136 }
137
138 if (port < 0)
139 port = 513; /* default rlogin port */
140
141 /*
142 * Open socket.
143 */
144 {
145 char *buf, addrbuf[100];
146 sk_getaddr(addr, addrbuf, 100);
147 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
148 logevent(rlogin->frontend, buf);
149 sfree(buf);
150 }
151 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
152 nodelay, (Plug) rlogin, cfg);
153 if ((err = sk_socket_error(rlogin->s)) != NULL)
154 return err;
155
156 /*
157 * Send local username, remote username, terminal/speed
158 */
159
160 {
161 char z = 0;
162 char *p;
163 sk_write(rlogin->s, &z, 1);
164 sk_write(rlogin->s, cfg->localusername,
165 strlen(cfg->localusername));
166 sk_write(rlogin->s, &z, 1);
167 sk_write(rlogin->s, cfg->username,
168 strlen(cfg->username));
169 sk_write(rlogin->s, &z, 1);
170 sk_write(rlogin->s, cfg->termtype,
171 strlen(cfg->termtype));
172 sk_write(rlogin->s, "/", 1);
173 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
174 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
175 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
176 }
177
178 return NULL;
179 }
180
181 static void rlogin_free(void *handle)
182 {
183 Rlogin rlogin = (Rlogin) handle;
184
185 if (rlogin->s)
186 sk_close(rlogin->s);
187 sfree(rlogin);
188 }
189
190 /*
191 * Stub routine (we don't have any need to reconfigure this backend).
192 */
193 static void rlogin_reconfig(void *handle, Config *cfg)
194 {
195 }
196
197 /*
198 * Called to send data down the rlogin connection.
199 */
200 static int rlogin_send(void *handle, char *buf, int len)
201 {
202 Rlogin rlogin = (Rlogin) handle;
203
204 if (rlogin->s == NULL)
205 return 0;
206
207 rlogin->bufsize = sk_write(rlogin->s, buf, len);
208
209 return rlogin->bufsize;
210 }
211
212 /*
213 * Called to query the current socket sendability status.
214 */
215 static int rlogin_sendbuffer(void *handle)
216 {
217 Rlogin rlogin = (Rlogin) handle;
218 return rlogin->bufsize;
219 }
220
221 /*
222 * Called to set the size of the window
223 */
224 static void rlogin_size(void *handle, int width, int height)
225 {
226 Rlogin rlogin = (Rlogin) handle;
227 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
228
229 rlogin->term_width = width;
230 rlogin->term_height = height;
231
232 if (rlogin->s == NULL)
233 return;
234
235 b[6] = rlogin->term_width >> 8;
236 b[7] = rlogin->term_width & 0xFF;
237 b[4] = rlogin->term_height >> 8;
238 b[5] = rlogin->term_height & 0xFF;
239 rlogin->bufsize = sk_write(rlogin->s, b, 12);
240 return;
241 }
242
243 /*
244 * Send rlogin special codes.
245 */
246 static void rlogin_special(void *handle, Telnet_Special code)
247 {
248 /* Do nothing! */
249 return;
250 }
251
252 /*
253 * Return a list of the special codes that make sense in this
254 * protocol.
255 */
256 static const struct telnet_special *rlogin_get_specials(void *handle)
257 {
258 return NULL;
259 }
260
261 static Socket rlogin_socket(void *handle)
262 {
263 Rlogin rlogin = (Rlogin) handle;
264 return rlogin->s;
265 }
266
267 static int rlogin_sendok(void *handle)
268 {
269 /* Rlogin rlogin = (Rlogin) handle; */
270 return 1;
271 }
272
273 static void rlogin_unthrottle(void *handle, int backlog)
274 {
275 Rlogin rlogin = (Rlogin) handle;
276 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
277 }
278
279 static int rlogin_ldisc(void *handle, int option)
280 {
281 /* Rlogin rlogin = (Rlogin) handle; */
282 return 0;
283 }
284
285 static void rlogin_provide_ldisc(void *handle, void *ldisc)
286 {
287 /* This is a stub. */
288 }
289
290 static void rlogin_provide_logctx(void *handle, void *logctx)
291 {
292 /* This is a stub. */
293 }
294
295 static int rlogin_exitcode(void *handle)
296 {
297 Rlogin rlogin = (Rlogin) handle;
298 if (rlogin->s != NULL)
299 return -1; /* still connected */
300 else
301 /* If we ever implement RSH, we'll probably need to do this properly */
302 return 0;
303 }
304
305 Backend rlogin_backend = {
306 rlogin_init,
307 rlogin_free,
308 rlogin_reconfig,
309 rlogin_send,
310 rlogin_sendbuffer,
311 rlogin_size,
312 rlogin_special,
313 rlogin_get_specials,
314 rlogin_socket,
315 rlogin_exitcode,
316 rlogin_sendok,
317 rlogin_ldisc,
318 rlogin_provide_ldisc,
319 rlogin_provide_logctx,
320 rlogin_unthrottle,
321 1
322 };