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