c91409da |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
b6c680d4 |
4 | #include <ctype.h> |
c91409da |
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 | |
5471d09a |
15 | #define RLOGIN_MAX_BACKLOG 4096 |
16 | |
51470298 |
17 | typedef struct rlogin_tag { |
18 | const struct plug_function_table *fn; |
19 | /* the above field _must_ be first in the structure */ |
c91409da |
20 | |
51470298 |
21 | Socket s; |
22 | int bufsize; |
23 | int term_width, term_height; |
24 | void *frontend; |
25 | } *Rlogin; |
c91409da |
26 | |
51470298 |
27 | static void rlogin_size(void *handle, int width, int height); |
28 | |
29 | static void c_write(Rlogin rlogin, char *buf, int len) |
32874aea |
30 | { |
51470298 |
31 | int backlog = from_backend(rlogin->frontend, 0, buf, len); |
32 | sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG); |
c91409da |
33 | } |
34 | |
32874aea |
35 | static int rlogin_closing(Plug plug, char *error_msg, int error_code, |
36 | int calling_back) |
37 | { |
51470298 |
38 | Rlogin rlogin = (Rlogin) plug; |
39 | if (rlogin->s) { |
40 | sk_close(rlogin->s); |
41 | rlogin->s = NULL; |
f3ab576e |
42 | } |
7e78000d |
43 | if (error_msg) { |
32874aea |
44 | /* A socket error has occurred. */ |
247308b5 |
45 | logevent(error_msg); |
46 | connection_fatal("%s", error_msg); |
32874aea |
47 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
48 | return 0; |
49 | } |
50 | |
32874aea |
51 | static int rlogin_receive(Plug plug, int urgent, char *data, int len) |
52 | { |
51470298 |
53 | Rlogin rlogin = (Rlogin) plug; |
c91409da |
54 | if (urgent == 2) { |
32874aea |
55 | char c; |
56 | |
57 | c = *data++; |
58 | len--; |
59 | if (c == '\x80') |
51470298 |
60 | rlogin_size(rlogin, rlogin->term_width, rlogin->term_height); |
32874aea |
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 | */ |
8a5fb9e3 |
67 | } else { |
32874aea |
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 | } |
2b0c045b |
81 | if (len > 0) |
51470298 |
82 | c_write(rlogin, data, len); |
c91409da |
83 | } |
c91409da |
84 | return 1; |
85 | } |
86 | |
3ad9d396 |
87 | static void rlogin_sent(Plug plug, int bufsize) |
88 | { |
51470298 |
89 | Rlogin rlogin = (Rlogin) plug; |
90 | rlogin->bufsize = bufsize; |
3ad9d396 |
91 | } |
92 | |
c91409da |
93 | /* |
94 | * Called to set up the rlogin connection. |
95 | * |
96 | * Returns an error message, or NULL on success. |
97 | * |
6e1ebb76 |
98 | * Also places the canonical host name into `realhost'. It must be |
99 | * freed by the caller. |
c91409da |
100 | */ |
51470298 |
101 | static char *rlogin_init(void *frontend_handle, void **backend_handle, |
887035a5 |
102 | char *host, int port, char **realhost, int nodelay) |
32874aea |
103 | { |
51470298 |
104 | static const struct plug_function_table fn_table = { |
7e78000d |
105 | rlogin_closing, |
3ad9d396 |
106 | rlogin_receive, |
107 | rlogin_sent |
51470298 |
108 | }; |
c91409da |
109 | SockAddr addr; |
110 | char *err; |
51470298 |
111 | Rlogin rlogin; |
c91409da |
112 | |
51470298 |
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; |
887035a5 |
120 | |
c91409da |
121 | /* |
122 | * Try to find host. |
123 | */ |
3ad9d396 |
124 | { |
125 | char buf[200]; |
126 | sprintf(buf, "Looking up host \"%.170s\"", host); |
127 | logevent(buf); |
128 | } |
c91409da |
129 | addr = sk_namelookup(host, realhost); |
32874aea |
130 | if ((err = sk_addr_error(addr))) |
c91409da |
131 | return err; |
132 | |
133 | if (port < 0) |
134 | port = 513; /* default rlogin port */ |
135 | |
136 | /* |
137 | * Open socket. |
138 | */ |
3ad9d396 |
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 | } |
51470298 |
145 | rlogin->s = new_connection(addr, *realhost, port, 1, 0, |
146 | nodelay, (Plug) rlogin); |
147 | if ((err = sk_socket_error(rlogin->s))) |
c91409da |
148 | return err; |
149 | |
150 | sk_addr_free(addr); |
151 | |
152 | /* |
153 | * Send local username, remote username, terminal/speed |
154 | */ |
155 | |
156 | { |
32874aea |
157 | char z = 0; |
158 | char *p; |
51470298 |
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); |
32874aea |
166 | for (p = cfg.termspeed; isdigit(*p); p++); |
51470298 |
167 | sk_write(rlogin->s, cfg.termspeed, p - cfg.termspeed); |
168 | rlogin->bufsize = sk_write(rlogin->s, &z, 1); |
c91409da |
169 | } |
170 | |
c91409da |
171 | return NULL; |
172 | } |
173 | |
174 | /* |
175 | * Called to send data down the rlogin connection. |
176 | */ |
51470298 |
177 | static int rlogin_send(void *handle, char *buf, int len) |
32874aea |
178 | { |
51470298 |
179 | Rlogin rlogin = (Rlogin) handle; |
180 | |
181 | if (rlogin->s == NULL) |
b5a460cd |
182 | return 0; |
c91409da |
183 | |
51470298 |
184 | rlogin->bufsize = sk_write(rlogin->s, buf, len); |
5471d09a |
185 | |
51470298 |
186 | return rlogin->bufsize; |
5471d09a |
187 | } |
188 | |
189 | /* |
190 | * Called to query the current socket sendability status. |
191 | */ |
51470298 |
192 | static int rlogin_sendbuffer(void *handle) |
5471d09a |
193 | { |
51470298 |
194 | Rlogin rlogin = (Rlogin) handle; |
195 | return rlogin->bufsize; |
c91409da |
196 | } |
197 | |
198 | /* |
199 | * Called to set the size of the window |
200 | */ |
51470298 |
201 | static void rlogin_size(void *handle, int width, int height) |
32874aea |
202 | { |
51470298 |
203 | Rlogin rlogin = (Rlogin) handle; |
e418595a |
204 | char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 }; |
c91409da |
205 | |
51470298 |
206 | rlogin->term_width = width; |
207 | rlogin->term_height = height; |
f278d6f8 |
208 | |
51470298 |
209 | if (rlogin->s == NULL) |
cf293dd4 |
210 | return; |
f278d6f8 |
211 | |
51470298 |
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); |
c91409da |
217 | return; |
218 | } |
219 | |
220 | /* |
221 | * Send rlogin special codes. |
222 | */ |
51470298 |
223 | static void rlogin_special(void *handle, Telnet_Special code) |
32874aea |
224 | { |
c91409da |
225 | /* Do nothing! */ |
226 | return; |
227 | } |
228 | |
51470298 |
229 | static Socket rlogin_socket(void *handle) |
32874aea |
230 | { |
51470298 |
231 | Rlogin rlogin = (Rlogin) handle; |
232 | return rlogin->s; |
32874aea |
233 | } |
c91409da |
234 | |
51470298 |
235 | static int rlogin_sendok(void *handle) |
32874aea |
236 | { |
51470298 |
237 | Rlogin rlogin = (Rlogin) handle; |
32874aea |
238 | return 1; |
239 | } |
c91409da |
240 | |
51470298 |
241 | static void rlogin_unthrottle(void *handle, int backlog) |
5471d09a |
242 | { |
51470298 |
243 | Rlogin rlogin = (Rlogin) handle; |
244 | sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG); |
5471d09a |
245 | } |
246 | |
51470298 |
247 | static int rlogin_ldisc(void *handle, int option) |
32874aea |
248 | { |
51470298 |
249 | Rlogin rlogin = (Rlogin) handle; |
0965bee0 |
250 | return 0; |
251 | } |
252 | |
b9d7bcad |
253 | static void rlogin_provide_ldisc(void *handle, void *ldisc) |
254 | { |
255 | /* This is a stub. */ |
256 | } |
257 | |
51470298 |
258 | static int rlogin_exitcode(void *handle) |
d8d6c7e5 |
259 | { |
51470298 |
260 | Rlogin rlogin = (Rlogin) handle; |
d8d6c7e5 |
261 | /* If we ever implement RSH, we'll probably need to do this properly */ |
262 | return 0; |
263 | } |
264 | |
c91409da |
265 | Backend rlogin_backend = { |
266 | rlogin_init, |
267 | rlogin_send, |
5471d09a |
268 | rlogin_sendbuffer, |
c91409da |
269 | rlogin_size, |
270 | rlogin_special, |
271 | rlogin_socket, |
d8d6c7e5 |
272 | rlogin_exitcode, |
c91409da |
273 | rlogin_sendok, |
0965bee0 |
274 | rlogin_ldisc, |
b9d7bcad |
275 | rlogin_provide_ldisc, |
5471d09a |
276 | rlogin_unthrottle, |
c91409da |
277 | 1 |
278 | }; |