Major destabilisation, phase 1. In this phase I've moved (I think)
[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 #include "terminal.h"
8
9 #ifndef FALSE
10 #define FALSE 0
11 #endif
12 #ifndef TRUE
13 #define TRUE 1
14 #endif
15
16 #define RLOGIN_MAX_BACKLOG 4096
17
18 static Socket s = NULL;
19 static int rlogin_bufsize;
20 static void *frontend;
21
22 static void rlogin_size(void);
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();
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
107 /*
108 * Try to find host.
109 */
110 {
111 char buf[200];
112 sprintf(buf, "Looking up host \"%.170s\"", host);
113 logevent(buf);
114 }
115 addr = sk_namelookup(host, realhost);
116 if ((err = sk_addr_error(addr)))
117 return err;
118
119 if (port < 0)
120 port = 513; /* default rlogin port */
121
122 /*
123 * Open socket.
124 */
125 {
126 char buf[200], addrbuf[100];
127 sk_getaddr(addr, addrbuf, 100);
128 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
129 logevent(buf);
130 }
131 s = new_connection(addr, *realhost, port, 1, 0, nodelay, &fn_table_ptr);
132 if ((err = sk_socket_error(s)))
133 return err;
134
135 sk_addr_free(addr);
136
137 /*
138 * Send local username, remote username, terminal/speed
139 */
140
141 {
142 char z = 0;
143 char *p;
144 sk_write(s, &z, 1);
145 sk_write(s, cfg.localusername, strlen(cfg.localusername));
146 sk_write(s, &z, 1);
147 sk_write(s, cfg.username, strlen(cfg.username));
148 sk_write(s, &z, 1);
149 sk_write(s, cfg.termtype, strlen(cfg.termtype));
150 sk_write(s, "/", 1);
151 for (p = cfg.termspeed; isdigit(*p); p++);
152 sk_write(s, cfg.termspeed, p - cfg.termspeed);
153 rlogin_bufsize = sk_write(s, &z, 1);
154 }
155
156 return NULL;
157 }
158
159 /*
160 * Called to send data down the rlogin connection.
161 */
162 static int rlogin_send(char *buf, int len)
163 {
164 if (s == NULL)
165 return 0;
166
167 rlogin_bufsize = sk_write(s, buf, len);
168
169 return rlogin_bufsize;
170 }
171
172 /*
173 * Called to query the current socket sendability status.
174 */
175 static int rlogin_sendbuffer(void)
176 {
177 return rlogin_bufsize;
178 }
179
180 /*
181 * Called to set the size of the window
182 */
183 static void rlogin_size(void)
184 {
185 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
186
187 if (s == NULL || term == NULL)
188 return;
189
190 b[6] = term->cols >> 8;
191 b[7] = term->cols & 0xFF;
192 b[4] = term->rows >> 8;
193 b[5] = term->rows & 0xFF;
194 rlogin_bufsize = sk_write(s, b, 12);
195 return;
196 }
197
198 /*
199 * Send rlogin special codes.
200 */
201 static void rlogin_special(Telnet_Special code)
202 {
203 /* Do nothing! */
204 return;
205 }
206
207 static Socket rlogin_socket(void)
208 {
209 return s;
210 }
211
212 static int rlogin_sendok(void)
213 {
214 return 1;
215 }
216
217 static void rlogin_unthrottle(int backlog)
218 {
219 sk_set_frozen(s, backlog > RLOGIN_MAX_BACKLOG);
220 }
221
222 static int rlogin_ldisc(int option)
223 {
224 return 0;
225 }
226
227 static int rlogin_exitcode(void)
228 {
229 /* If we ever implement RSH, we'll probably need to do this properly */
230 return 0;
231 }
232
233 Backend rlogin_backend = {
234 rlogin_init,
235 rlogin_send,
236 rlogin_sendbuffer,
237 rlogin_size,
238 rlogin_special,
239 rlogin_socket,
240 rlogin_exitcode,
241 rlogin_sendok,
242 rlogin_ldisc,
243 rlogin_unthrottle,
244 1
245 };