Fixes for (Backend)->size() changes -- internal declarations didn't include
[u/mdw/putty] / 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 static Socket s = NULL;
18 static int rlogin_bufsize;
19 static int rlogin_term_width, rlogin_term_height;
20 static void *frontend;
21
22 static void rlogin_size(int width, int height);
23
24 static void c_write(char *buf, int len)
25 {
26 int backlog = from_backend(frontend, 0, buf, len);
27 sk_set_frozen(s, backlog > RLOGIN_MAX_BACKLOG);
28 }
29
30 static int rlogin_closing(Plug plug, char *error_msg, int error_code,
31 int calling_back)
32 {
33 if (s) {
34 sk_close(s);
35 s = NULL;
36 }
37 if (error_msg) {
38 /* A socket error has occurred. */
39 logevent(error_msg);
40 connection_fatal("%s", error_msg);
41 } /* Otherwise, the remote side closed the connection normally. */
42 return 0;
43 }
44
45 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
46 {
47 if (urgent == 2) {
48 char c;
49
50 c = *data++;
51 len--;
52 if (c == '\x80')
53 rlogin_size(rlogin_term_width, rlogin_term_height);
54 /*
55 * We should flush everything (aka Telnet SYNCH) if we see
56 * 0x02, and we should turn off and on _local_ flow control
57 * on 0x10 and 0x20 respectively. I'm not convinced it's
58 * worth it...
59 */
60 } else {
61 /*
62 * Main rlogin protocol. This is really simple: the first
63 * byte is expected to be NULL and is ignored, and the rest
64 * is printed.
65 */
66 static int firstbyte = 1;
67 if (firstbyte) {
68 if (data[0] == '\0') {
69 data++;
70 len--;
71 }
72 firstbyte = 0;
73 }
74 if (len > 0)
75 c_write(data, len);
76 }
77 return 1;
78 }
79
80 static void rlogin_sent(Plug plug, int bufsize)
81 {
82 rlogin_bufsize = bufsize;
83 }
84
85 /*
86 * Called to set up the rlogin connection.
87 *
88 * Returns an error message, or NULL on success.
89 *
90 * Also places the canonical host name into `realhost'. It must be
91 * freed by the caller.
92 */
93 static char *rlogin_init(void *frontend_handle,
94 char *host, int port, char **realhost, int nodelay)
95 {
96 static struct plug_function_table fn_table = {
97 rlogin_closing,
98 rlogin_receive,
99 rlogin_sent
100 }, *fn_table_ptr = &fn_table;
101
102 SockAddr addr;
103 char *err;
104
105 frontend = frontend_handle;
106 rlogin_term_width = cfg.width;
107 rlogin_term_height = cfg.height;
108
109 /*
110 * Try to find host.
111 */
112 {
113 char buf[200];
114 sprintf(buf, "Looking up host \"%.170s\"", host);
115 logevent(buf);
116 }
117 addr = sk_namelookup(host, realhost);
118 if ((err = sk_addr_error(addr)))
119 return err;
120
121 if (port < 0)
122 port = 513; /* default rlogin port */
123
124 /*
125 * Open socket.
126 */
127 {
128 char buf[200], addrbuf[100];
129 sk_getaddr(addr, addrbuf, 100);
130 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
131 logevent(buf);
132 }
133 s = new_connection(addr, *realhost, port, 1, 0, nodelay, &fn_table_ptr);
134 if ((err = sk_socket_error(s)))
135 return err;
136
137 sk_addr_free(addr);
138
139 /*
140 * Send local username, remote username, terminal/speed
141 */
142
143 {
144 char z = 0;
145 char *p;
146 sk_write(s, &z, 1);
147 sk_write(s, cfg.localusername, strlen(cfg.localusername));
148 sk_write(s, &z, 1);
149 sk_write(s, cfg.username, strlen(cfg.username));
150 sk_write(s, &z, 1);
151 sk_write(s, cfg.termtype, strlen(cfg.termtype));
152 sk_write(s, "/", 1);
153 for (p = cfg.termspeed; isdigit(*p); p++);
154 sk_write(s, cfg.termspeed, p - cfg.termspeed);
155 rlogin_bufsize = sk_write(s, &z, 1);
156 }
157
158 return NULL;
159 }
160
161 /*
162 * Called to send data down the rlogin connection.
163 */
164 static int rlogin_send(char *buf, int len)
165 {
166 if (s == NULL)
167 return 0;
168
169 rlogin_bufsize = sk_write(s, buf, len);
170
171 return rlogin_bufsize;
172 }
173
174 /*
175 * Called to query the current socket sendability status.
176 */
177 static int rlogin_sendbuffer(void)
178 {
179 return rlogin_bufsize;
180 }
181
182 /*
183 * Called to set the size of the window
184 */
185 static void rlogin_size(int width, int height)
186 {
187 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
188
189 rlogin_term_width = width;
190 rlogin_term_height = height;
191
192 if (s == NULL)
193 return;
194
195 b[6] = rlogin_term_width >> 8;
196 b[7] = rlogin_term_width & 0xFF;
197 b[4] = rlogin_term_height >> 8;
198 b[5] = rlogin_term_height & 0xFF;
199 rlogin_bufsize = sk_write(s, b, 12);
200 return;
201 }
202
203 /*
204 * Send rlogin special codes.
205 */
206 static void rlogin_special(Telnet_Special code)
207 {
208 /* Do nothing! */
209 return;
210 }
211
212 static Socket rlogin_socket(void)
213 {
214 return s;
215 }
216
217 static int rlogin_sendok(void)
218 {
219 return 1;
220 }
221
222 static void rlogin_unthrottle(int backlog)
223 {
224 sk_set_frozen(s, backlog > RLOGIN_MAX_BACKLOG);
225 }
226
227 static int rlogin_ldisc(int option)
228 {
229 return 0;
230 }
231
232 static int rlogin_exitcode(void)
233 {
234 /* If we ever implement RSH, we'll probably need to do this properly */
235 return 0;
236 }
237
238 Backend rlogin_backend = {
239 rlogin_init,
240 rlogin_send,
241 rlogin_sendbuffer,
242 rlogin_size,
243 rlogin_special,
244 rlogin_socket,
245 rlogin_exitcode,
246 rlogin_sendok,
247 rlogin_ldisc,
248 rlogin_unthrottle,
249 1
250 };