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