]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - rlogin.c
New timing infrastructure. There's a new function schedule_timer()
[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\"", host);
130         logevent(rlogin->frontend, buf);
131         sfree(buf);
132     }
133     addr = name_lookup(host, port, realhost, cfg);
134     if ((err = sk_addr_error(addr)) != NULL) {
135         sk_addr_free(addr);
136         return err;
137     }
138
139     if (port < 0)
140         port = 513;                    /* default rlogin port */
141
142     /*
143      * Open socket.
144      */
145     {
146         char *buf, addrbuf[100];
147         sk_getaddr(addr, addrbuf, 100);
148         buf = dupprintf("Connecting to %s port %d", addrbuf, port);
149         logevent(rlogin->frontend, buf);
150         sfree(buf);
151     }
152     rlogin->s = new_connection(addr, *realhost, port, 1, 0,
153                                nodelay, keepalive, (Plug) rlogin, cfg);
154     if ((err = sk_socket_error(rlogin->s)) != NULL)
155         return err;
156
157     /*
158      * Send local username, remote username, terminal/speed
159      */
160
161     {
162         char z = 0;
163         char *p;
164         sk_write(rlogin->s, &z, 1);
165         sk_write(rlogin->s, cfg->localusername,
166                  strlen(cfg->localusername));
167         sk_write(rlogin->s, &z, 1);
168         sk_write(rlogin->s, cfg->username,
169                  strlen(cfg->username));
170         sk_write(rlogin->s, &z, 1);
171         sk_write(rlogin->s, cfg->termtype,
172                  strlen(cfg->termtype));
173         sk_write(rlogin->s, "/", 1);
174         for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
175         sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
176         rlogin->bufsize = sk_write(rlogin->s, &z, 1);
177     }
178
179     return NULL;
180 }
181
182 static void rlogin_free(void *handle)
183 {
184     Rlogin rlogin = (Rlogin) handle;
185
186     if (rlogin->s)
187         sk_close(rlogin->s);
188     sfree(rlogin);
189 }
190
191 /*
192  * Stub routine (we don't have any need to reconfigure this backend).
193  */
194 static void rlogin_reconfig(void *handle, Config *cfg)
195 {
196 }
197
198 /*
199  * Called to send data down the rlogin connection.
200  */
201 static int rlogin_send(void *handle, char *buf, int len)
202 {
203     Rlogin rlogin = (Rlogin) handle;
204
205     if (rlogin->s == NULL)
206         return 0;
207
208     rlogin->bufsize = sk_write(rlogin->s, buf, len);
209
210     return rlogin->bufsize;
211 }
212
213 /*
214  * Called to query the current socket sendability status.
215  */
216 static int rlogin_sendbuffer(void *handle)
217 {
218     Rlogin rlogin = (Rlogin) handle;
219     return rlogin->bufsize;
220 }
221
222 /*
223  * Called to set the size of the window
224  */
225 static void rlogin_size(void *handle, int width, int height)
226 {
227     Rlogin rlogin = (Rlogin) handle;
228     char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
229
230     rlogin->term_width = width;
231     rlogin->term_height = height;
232
233     if (rlogin->s == NULL)
234         return;
235
236     b[6] = rlogin->term_width >> 8;
237     b[7] = rlogin->term_width & 0xFF;
238     b[4] = rlogin->term_height >> 8;
239     b[5] = rlogin->term_height & 0xFF;
240     rlogin->bufsize = sk_write(rlogin->s, b, 12);
241     return;
242 }
243
244 /*
245  * Send rlogin special codes.
246  */
247 static void rlogin_special(void *handle, Telnet_Special code)
248 {
249     /* Do nothing! */
250     return;
251 }
252
253 /*
254  * Return a list of the special codes that make sense in this
255  * protocol.
256  */
257 static const struct telnet_special *rlogin_get_specials(void *handle)
258 {
259     return NULL;
260 }
261
262 static Socket rlogin_socket(void *handle)
263 {
264     Rlogin rlogin = (Rlogin) handle;
265     return rlogin->s;
266 }
267
268 static int rlogin_sendok(void *handle)
269 {
270     /* Rlogin rlogin = (Rlogin) handle; */
271     return 1;
272 }
273
274 static void rlogin_unthrottle(void *handle, int backlog)
275 {
276     Rlogin rlogin = (Rlogin) handle;
277     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
278 }
279
280 static int rlogin_ldisc(void *handle, int option)
281 {
282     /* Rlogin rlogin = (Rlogin) handle; */
283     return 0;
284 }
285
286 static void rlogin_provide_ldisc(void *handle, void *ldisc)
287 {
288     /* This is a stub. */
289 }
290
291 static void rlogin_provide_logctx(void *handle, void *logctx)
292 {
293     /* This is a stub. */
294 }
295
296 static int rlogin_exitcode(void *handle)
297 {
298     Rlogin rlogin = (Rlogin) handle;
299     if (rlogin->s != NULL)
300         return -1;                     /* still connected */
301     else
302         /* If we ever implement RSH, we'll probably need to do this properly */
303         return 0;
304 }
305
306 Backend rlogin_backend = {
307     rlogin_init,
308     rlogin_free,
309     rlogin_reconfig,
310     rlogin_send,
311     rlogin_sendbuffer,
312     rlogin_size,
313     rlogin_special,
314     rlogin_get_specials,
315     rlogin_socket,
316     rlogin_exitcode,
317     rlogin_sendok,
318     rlogin_ldisc,
319     rlogin_provide_ldisc,
320     rlogin_provide_logctx,
321     rlogin_unthrottle,
322     1
323 };