]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - rlogin.c
Line discipline module now uses dynamically allocated data. Also
[PuTTY.git] / rlogin.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5
6 #include "putty.h"
7
8 #ifndef FALSE
9 #define FALSE 0
10 #endif
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14
15 #define RLOGIN_MAX_BACKLOG 4096
16
17 typedef struct rlogin_tag {
18     const struct plug_function_table *fn;
19     /* the above field _must_ be first in the structure */
20
21     Socket s;
22     int bufsize;
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(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         static int firstbyte = 1;
74         if (firstbyte) {
75             if (data[0] == '\0') {
76                 data++;
77                 len--;
78             }
79             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 char *rlogin_init(void *frontend_handle, void **backend_handle,
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 = smalloc(sizeof(*rlogin));
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     *backend_handle = rlogin;
120
121     /*
122      * Try to find host.
123      */
124     {
125         char buf[200];
126         sprintf(buf, "Looking up host \"%.170s\"", host);
127         logevent(buf);
128     }
129     addr = sk_namelookup(host, realhost);
130     if ((err = sk_addr_error(addr)))
131         return err;
132
133     if (port < 0)
134         port = 513;                    /* default rlogin port */
135
136     /*
137      * Open socket.
138      */
139     {
140         char buf[200], addrbuf[100];
141         sk_getaddr(addr, addrbuf, 100);
142         sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
143         logevent(buf);
144     }
145     rlogin->s = new_connection(addr, *realhost, port, 1, 0,
146                                nodelay, (Plug) rlogin);
147     if ((err = sk_socket_error(rlogin->s)))
148         return err;
149
150     sk_addr_free(addr);
151
152     /*
153      * Send local username, remote username, terminal/speed
154      */
155
156     {
157         char z = 0;
158         char *p;
159         sk_write(rlogin->s, &z, 1);
160         sk_write(rlogin->s, cfg.localusername, strlen(cfg.localusername));
161         sk_write(rlogin->s, &z, 1);
162         sk_write(rlogin->s, cfg.username, strlen(cfg.username));
163         sk_write(rlogin->s, &z, 1);
164         sk_write(rlogin->s, cfg.termtype, strlen(cfg.termtype));
165         sk_write(rlogin->s, "/", 1);
166         for (p = cfg.termspeed; isdigit(*p); p++);
167         sk_write(rlogin->s, cfg.termspeed, p - cfg.termspeed);
168         rlogin->bufsize = sk_write(rlogin->s, &z, 1);
169     }
170
171     return NULL;
172 }
173
174 /*
175  * Called to send data down the rlogin connection.
176  */
177 static int rlogin_send(void *handle, char *buf, int len)
178 {
179     Rlogin rlogin = (Rlogin) handle;
180
181     if (rlogin->s == NULL)
182         return 0;
183
184     rlogin->bufsize = sk_write(rlogin->s, buf, len);
185
186     return rlogin->bufsize;
187 }
188
189 /*
190  * Called to query the current socket sendability status.
191  */
192 static int rlogin_sendbuffer(void *handle)
193 {
194     Rlogin rlogin = (Rlogin) handle;
195     return rlogin->bufsize;
196 }
197
198 /*
199  * Called to set the size of the window
200  */
201 static void rlogin_size(void *handle, int width, int height)
202 {
203     Rlogin rlogin = (Rlogin) handle;
204     char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
205
206     rlogin->term_width = width;
207     rlogin->term_height = height;
208
209     if (rlogin->s == NULL)
210         return;
211
212     b[6] = rlogin->term_width >> 8;
213     b[7] = rlogin->term_width & 0xFF;
214     b[4] = rlogin->term_height >> 8;
215     b[5] = rlogin->term_height & 0xFF;
216     rlogin->bufsize = sk_write(rlogin->s, b, 12);
217     return;
218 }
219
220 /*
221  * Send rlogin special codes.
222  */
223 static void rlogin_special(void *handle, Telnet_Special code)
224 {
225     /* Do nothing! */
226     return;
227 }
228
229 static Socket rlogin_socket(void *handle)
230 {
231     Rlogin rlogin = (Rlogin) handle;
232     return rlogin->s;
233 }
234
235 static int rlogin_sendok(void *handle)
236 {
237     Rlogin rlogin = (Rlogin) handle;
238     return 1;
239 }
240
241 static void rlogin_unthrottle(void *handle, int backlog)
242 {
243     Rlogin rlogin = (Rlogin) handle;
244     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
245 }
246
247 static int rlogin_ldisc(void *handle, int option)
248 {
249     Rlogin rlogin = (Rlogin) handle;
250     return 0;
251 }
252
253 static void rlogin_provide_ldisc(void *handle, void *ldisc)
254 {
255     /* This is a stub. */
256 }
257
258 static int rlogin_exitcode(void *handle)
259 {
260     Rlogin rlogin = (Rlogin) handle;
261     /* If we ever implement RSH, we'll probably need to do this properly */
262     return 0;
263 }
264
265 Backend rlogin_backend = {
266     rlogin_init,
267     rlogin_send,
268     rlogin_sendbuffer,
269     rlogin_size,
270     rlogin_special,
271     rlogin_socket,
272     rlogin_exitcode,
273     rlogin_sendok,
274     rlogin_ldisc,
275     rlogin_provide_ldisc,
276     rlogin_unthrottle,
277     1
278 };