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