374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
374330e2 |
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 | |
8df7a775 |
14 | static Socket s = NULL; |
374330e2 |
15 | |
887035a5 |
16 | static void *frontend; |
f278d6f8 |
17 | static int telnet_term_width, telnet_term_height; |
887035a5 |
18 | |
32874aea |
19 | #define IAC 255 /* interpret as command: */ |
20 | #define DONT 254 /* you are not to use option */ |
21 | #define DO 253 /* please, you use option */ |
22 | #define WONT 252 /* I won't use option */ |
23 | #define WILL 251 /* I will use option */ |
24 | #define SB 250 /* interpret as subnegotiation */ |
25 | #define SE 240 /* end sub negotiation */ |
26 | |
27 | #define GA 249 /* you may reverse the line */ |
28 | #define EL 248 /* erase the current line */ |
29 | #define EC 247 /* erase the current character */ |
30 | #define AYT 246 /* are you there */ |
31 | #define AO 245 /* abort output--but let prog finish */ |
32 | #define IP 244 /* interrupt process--permanently */ |
33 | #define BREAK 243 /* break */ |
34 | #define DM 242 /* data mark--for connect. cleaning */ |
35 | #define NOP 241 /* nop */ |
36 | #define EOR 239 /* end of record (transparent mode) */ |
37 | #define ABORT 238 /* Abort process */ |
38 | #define SUSP 237 /* Suspend process */ |
39 | #define xEOF 236 /* End of file: EOF is already used... */ |
40 | |
41 | #define TELOPT_BINARY 0 /* 8-bit data path */ |
42 | #define TELOPT_ECHO 1 /* echo */ |
43 | #define TELOPT_RCP 2 /* prepare to reconnect */ |
44 | #define TELOPT_SGA 3 /* suppress go ahead */ |
45 | #define TELOPT_NAMS 4 /* approximate message size */ |
46 | #define TELOPT_STATUS 5 /* give status */ |
47 | #define TELOPT_TM 6 /* timing mark */ |
48 | #define TELOPT_RCTE 7 /* remote controlled transmission and echo */ |
49 | #define TELOPT_NAOL 8 /* negotiate about output line width */ |
50 | #define TELOPT_NAOP 9 /* negotiate about output page size */ |
51 | #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */ |
52 | #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */ |
53 | #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */ |
54 | #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */ |
55 | #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */ |
56 | #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */ |
57 | #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */ |
58 | #define TELOPT_XASCII 17 /* extended ascic character set */ |
59 | #define TELOPT_LOGOUT 18 /* force logout */ |
60 | #define TELOPT_BM 19 /* byte macro */ |
61 | #define TELOPT_DET 20 /* data entry terminal */ |
62 | #define TELOPT_SUPDUP 21 /* supdup protocol */ |
63 | #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */ |
64 | #define TELOPT_SNDLOC 23 /* send location */ |
65 | #define TELOPT_TTYPE 24 /* terminal type */ |
66 | #define TELOPT_EOR 25 /* end or record */ |
67 | #define TELOPT_TUID 26 /* TACACS user identification */ |
68 | #define TELOPT_OUTMRK 27 /* output marking */ |
69 | #define TELOPT_TTYLOC 28 /* terminal location number */ |
70 | #define TELOPT_3270REGIME 29 /* 3270 regime */ |
71 | #define TELOPT_X3PAD 30 /* X.3 PAD */ |
72 | #define TELOPT_NAWS 31 /* window size */ |
73 | #define TELOPT_TSPEED 32 /* terminal speed */ |
74 | #define TELOPT_LFLOW 33 /* remote flow control */ |
75 | #define TELOPT_LINEMODE 34 /* Linemode option */ |
76 | #define TELOPT_XDISPLOC 35 /* X Display Location */ |
77 | #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */ |
78 | #define TELOPT_AUTHENTICATION 37 /* Authenticate */ |
79 | #define TELOPT_ENCRYPT 38 /* Encryption option */ |
80 | #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ |
81 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
82 | |
83 | #define TELQUAL_IS 0 /* option is... */ |
84 | #define TELQUAL_SEND 1 /* send option */ |
85 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
374330e2 |
86 | #define BSD_VAR 1 |
87 | #define BSD_VALUE 0 |
88 | #define RFC_VAR 0 |
89 | #define RFC_VALUE 1 |
90 | |
91 | #define CR 13 |
92 | #define LF 10 |
93 | #define NUL 0 |
94 | |
95 | #define iswritable(x) ( (x) != IAC && (x) != CR ) |
96 | |
32874aea |
97 | static char *telopt(int opt) |
98 | { |
374330e2 |
99 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
32874aea |
100 | i(BINARY); |
101 | i(ECHO); |
102 | i(RCP); |
103 | i(SGA); |
104 | i(NAMS); |
105 | i(STATUS); |
106 | i(TM); |
107 | i(RCTE); |
108 | i(NAOL); |
109 | i(NAOP); |
110 | i(NAOCRD); |
111 | i(NAOHTS); |
112 | i(NAOHTD); |
113 | i(NAOFFD); |
114 | i(NAOVTS); |
115 | i(NAOVTD); |
116 | i(NAOLFD); |
117 | i(XASCII); |
118 | i(LOGOUT); |
119 | i(BM); |
120 | i(DET); |
121 | i(SUPDUP); |
122 | i(SUPDUPOUTPUT); |
123 | i(SNDLOC); |
124 | i(TTYPE); |
125 | i(EOR); |
126 | i(TUID); |
127 | i(OUTMRK); |
128 | i(TTYLOC); |
129 | i(X3PAD); |
130 | i(NAWS); |
131 | i(TSPEED); |
132 | i(LFLOW); |
133 | i(LINEMODE); |
134 | i(XDISPLOC); |
135 | i(OLD_ENVIRON); |
136 | i(AUTHENTICATION); |
137 | i(ENCRYPT); |
138 | i(NEW_ENVIRON); |
139 | i(EXOPL); |
374330e2 |
140 | #undef i |
141 | return "<unknown>"; |
142 | } |
143 | |
818ab3bb |
144 | static void telnet_size(int width, int height); |
374330e2 |
145 | |
146 | struct Opt { |
147 | int send; /* what we initially send */ |
148 | int nsend; /* -ve send if requested to stop it */ |
149 | int ack, nak; /* +ve and -ve acknowledgements */ |
150 | int option; /* the option code */ |
151 | enum { |
152 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
153 | } state; |
154 | }; |
155 | |
32874aea |
156 | static struct Opt o_naws = |
157 | { WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED }; |
158 | static struct Opt o_tspeed = |
159 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED }; |
160 | static struct Opt o_ttype = |
161 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED }; |
162 | static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, |
163 | INACTIVE |
164 | }; |
165 | static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
166 | REQUESTED |
167 | }; |
168 | static struct Opt o_echo = |
169 | { DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED }; |
170 | static struct Opt o_we_sga = |
171 | { WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED }; |
172 | static struct Opt o_they_sga = |
173 | { DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED }; |
374330e2 |
174 | |
175 | static struct Opt *opts[] = { |
176 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
177 | &o_we_sga, &o_they_sga, NULL |
178 | }; |
179 | |
5471d09a |
180 | #define TELNET_MAX_BACKLOG 4096 |
181 | |
0965bee0 |
182 | static int echoing = TRUE, editing = TRUE; |
8faa456c |
183 | static int activated = FALSE; |
5471d09a |
184 | static int telnet_bufsize; |
374330e2 |
185 | static int in_synch; |
374330e2 |
186 | static int sb_opt, sb_len; |
187 | static char *sb_buf = NULL; |
188 | static int sb_size = 0; |
189 | #define SB_DELTA 1024 |
190 | |
32874aea |
191 | static void c_write1(int c) |
192 | { |
5471d09a |
193 | int backlog; |
32874aea |
194 | char cc = (char) c; |
887035a5 |
195 | backlog = from_backend(frontend, 0, &cc, 1); |
5471d09a |
196 | sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG); |
fe50e814 |
197 | } |
198 | |
32874aea |
199 | static void log_option(char *sender, int cmd, int option) |
200 | { |
374330e2 |
201 | char buf[50]; |
202 | sprintf(buf, "%s:\t%s %s", sender, |
203 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
204 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
205 | telopt(option)); |
c5e9c988 |
206 | logevent(buf); |
374330e2 |
207 | } |
208 | |
32874aea |
209 | static void send_opt(int cmd, int option) |
210 | { |
374330e2 |
211 | unsigned char b[3]; |
212 | |
32874aea |
213 | b[0] = IAC; |
214 | b[1] = cmd; |
215 | b[2] = option; |
5471d09a |
216 | telnet_bufsize = sk_write(s, b, 3); |
374330e2 |
217 | log_option("client", cmd, option); |
218 | } |
219 | |
32874aea |
220 | static void deactivate_option(struct Opt *o) |
221 | { |
374330e2 |
222 | if (o->state == REQUESTED || o->state == ACTIVE) |
32874aea |
223 | send_opt(o->nsend, o->option); |
374330e2 |
224 | o->state = REALLY_INACTIVE; |
225 | } |
226 | |
708bbbbe |
227 | /* |
228 | * Generate side effects of enabling or disabling an option. |
229 | */ |
32874aea |
230 | static void option_side_effects(struct Opt *o, int enabled) |
231 | { |
0965bee0 |
232 | if (o->option == TELOPT_ECHO && o->send == DO) |
32874aea |
233 | echoing = !enabled; |
b6c680d4 |
234 | else if (o->option == TELOPT_SGA && o->send == DO) |
32874aea |
235 | editing = !enabled; |
760e88b2 |
236 | ldisc_send(NULL, 0, 0); /* cause ldisc to notice the change */ |
8faa456c |
237 | |
238 | /* Ensure we get the minimum options */ |
239 | if (!activated) { |
240 | if (o_echo.state == INACTIVE) { |
241 | o_echo.state = REQUESTED; |
242 | send_opt(o_echo.send, o_echo.option); |
243 | } |
244 | if (o_we_sga.state == INACTIVE) { |
245 | o_we_sga.state = REQUESTED; |
246 | send_opt(o_we_sga.send, o_we_sga.option); |
247 | } |
248 | if (o_they_sga.state == INACTIVE) { |
249 | o_they_sga.state = REQUESTED; |
250 | send_opt(o_they_sga.send, o_they_sga.option); |
251 | } |
252 | activated = TRUE; |
253 | } |
708bbbbe |
254 | } |
255 | |
32874aea |
256 | static void activate_option(struct Opt *o) |
257 | { |
374330e2 |
258 | if (o->send == WILL && o->option == TELOPT_NAWS) |
818ab3bb |
259 | telnet_size(telnet_term_width, telnet_term_height); |
374330e2 |
260 | if (o->send == WILL && |
261 | (o->option == TELOPT_NEW_ENVIRON || |
262 | o->option == TELOPT_OLD_ENVIRON)) { |
263 | /* |
264 | * We may only have one kind of ENVIRON going at a time. |
265 | * This is a hack, but who cares. |
266 | */ |
32874aea |
267 | deactivate_option(o->option == |
268 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
269 | } |
708bbbbe |
270 | option_side_effects(o, 1); |
374330e2 |
271 | } |
272 | |
32874aea |
273 | static void refused_option(struct Opt *o) |
274 | { |
374330e2 |
275 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
276 | o_oenv.state == INACTIVE) { |
32874aea |
277 | send_opt(WILL, TELOPT_OLD_ENVIRON); |
374330e2 |
278 | o_oenv.state = REQUESTED; |
279 | } |
708bbbbe |
280 | option_side_effects(o, 0); |
374330e2 |
281 | } |
282 | |
32874aea |
283 | static void proc_rec_opt(int cmd, int option) |
284 | { |
374330e2 |
285 | struct Opt **o; |
286 | |
32874aea |
287 | log_option("server", cmd, option); |
374330e2 |
288 | for (o = opts; *o; o++) { |
289 | if ((*o)->option == option && (*o)->ack == cmd) { |
290 | switch ((*o)->state) { |
291 | case REQUESTED: |
292 | (*o)->state = ACTIVE; |
32874aea |
293 | activate_option(*o); |
374330e2 |
294 | break; |
295 | case ACTIVE: |
296 | break; |
297 | case INACTIVE: |
298 | (*o)->state = ACTIVE; |
32874aea |
299 | send_opt((*o)->send, option); |
300 | activate_option(*o); |
374330e2 |
301 | break; |
302 | case REALLY_INACTIVE: |
32874aea |
303 | send_opt((*o)->nsend, option); |
374330e2 |
304 | break; |
305 | } |
306 | return; |
307 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
308 | switch ((*o)->state) { |
309 | case REQUESTED: |
310 | (*o)->state = INACTIVE; |
32874aea |
311 | refused_option(*o); |
374330e2 |
312 | break; |
313 | case ACTIVE: |
314 | (*o)->state = INACTIVE; |
32874aea |
315 | send_opt((*o)->nsend, option); |
316 | option_side_effects(*o, 0); |
374330e2 |
317 | break; |
318 | case INACTIVE: |
319 | case REALLY_INACTIVE: |
320 | break; |
321 | } |
322 | return; |
323 | } |
324 | } |
325 | /* |
326 | * If we reach here, the option was one we weren't prepared to |
327 | * cope with. So send a negative ack. |
328 | */ |
32874aea |
329 | send_opt((cmd == WILL ? DONT : WONT), option); |
374330e2 |
330 | } |
331 | |
32874aea |
332 | static void process_subneg(void) |
333 | { |
374330e2 |
334 | unsigned char b[2048], *p, *q; |
335 | int var, value, n; |
336 | char *e; |
337 | |
338 | switch (sb_opt) { |
339 | case TELOPT_TSPEED: |
340 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
32874aea |
341 | char logbuf[sizeof(cfg.termspeed) + 80]; |
342 | b[0] = IAC; |
343 | b[1] = SB; |
344 | b[2] = TELOPT_TSPEED; |
374330e2 |
345 | b[3] = TELQUAL_IS; |
32874aea |
346 | strcpy(b + 4, cfg.termspeed); |
374330e2 |
347 | n = 4 + strlen(cfg.termspeed); |
32874aea |
348 | b[n] = IAC; |
349 | b[n + 1] = SE; |
5471d09a |
350 | telnet_bufsize = sk_write(s, b, n + 2); |
c5e9c988 |
351 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
352 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
32874aea |
353 | logevent(logbuf); |
374330e2 |
354 | } else |
32874aea |
355 | logevent("server:\tSB TSPEED <something weird>"); |
374330e2 |
356 | break; |
357 | case TELOPT_TTYPE: |
358 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
32874aea |
359 | char logbuf[sizeof(cfg.termtype) + 80]; |
360 | b[0] = IAC; |
361 | b[1] = SB; |
362 | b[2] = TELOPT_TTYPE; |
374330e2 |
363 | b[3] = TELQUAL_IS; |
364 | for (n = 0; cfg.termtype[n]; n++) |
32874aea |
365 | b[n + 4] = (cfg.termtype[n] >= 'a' |
366 | && cfg.termtype[n] <= |
367 | 'z' ? cfg.termtype[n] + 'A' - |
368 | 'a' : cfg.termtype[n]); |
369 | b[n + 4] = IAC; |
370 | b[n + 5] = SE; |
5471d09a |
371 | telnet_bufsize = sk_write(s, b, n + 6); |
32874aea |
372 | b[n + 4] = 0; |
c5e9c988 |
373 | logevent("server:\tSB TTYPE SEND"); |
32874aea |
374 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); |
c5e9c988 |
375 | logevent(logbuf); |
374330e2 |
376 | } else |
c5e9c988 |
377 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
378 | break; |
379 | case TELOPT_OLD_ENVIRON: |
32874aea |
380 | case TELOPT_NEW_ENVIRON: |
374330e2 |
381 | p = sb_buf; |
382 | q = p + sb_len; |
383 | if (p < q && *p == TELQUAL_SEND) { |
384 | char logbuf[50]; |
385 | p++; |
32874aea |
386 | sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
387 | logevent(logbuf); |
374330e2 |
388 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
389 | if (cfg.rfc_environ) { |
390 | value = RFC_VALUE; |
391 | var = RFC_VAR; |
392 | } else { |
393 | value = BSD_VALUE; |
394 | var = BSD_VAR; |
395 | } |
396 | /* |
397 | * Try to guess the sense of VAR and VALUE. |
398 | */ |
399 | while (p < q) { |
400 | if (*p == RFC_VAR) { |
401 | value = RFC_VALUE; |
402 | var = RFC_VAR; |
403 | } else if (*p == BSD_VAR) { |
404 | value = BSD_VALUE; |
405 | var = BSD_VAR; |
406 | } |
407 | p++; |
408 | } |
409 | } else { |
410 | /* |
411 | * With NEW_ENVIRON, the sense of VAR and VALUE |
412 | * isn't in doubt. |
413 | */ |
414 | value = RFC_VALUE; |
415 | var = RFC_VAR; |
416 | } |
32874aea |
417 | b[0] = IAC; |
418 | b[1] = SB; |
419 | b[2] = sb_opt; |
374330e2 |
420 | b[3] = TELQUAL_IS; |
421 | n = 4; |
32874aea |
422 | e = cfg.environmt; |
374330e2 |
423 | while (*e) { |
424 | b[n++] = var; |
32874aea |
425 | while (*e && *e != '\t') |
426 | b[n++] = *e++; |
427 | if (*e == '\t') |
428 | e++; |
374330e2 |
429 | b[n++] = value; |
32874aea |
430 | while (*e) |
431 | b[n++] = *e++; |
374330e2 |
432 | e++; |
433 | } |
434 | if (*cfg.username) { |
32874aea |
435 | b[n++] = var; |
436 | b[n++] = 'U'; |
437 | b[n++] = 'S'; |
438 | b[n++] = 'E'; |
439 | b[n++] = 'R'; |
440 | b[n++] = value; |
374330e2 |
441 | e = cfg.username; |
32874aea |
442 | while (*e) |
443 | b[n++] = *e++; |
374330e2 |
444 | } |
32874aea |
445 | b[n++] = IAC; |
446 | b[n++] = SE; |
5471d09a |
447 | telnet_bufsize = sk_write(s, b, n); |
374330e2 |
448 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
32874aea |
449 | n == 6 ? "<nothing>" : "<stuff>"); |
450 | logevent(logbuf); |
374330e2 |
451 | } |
452 | break; |
453 | } |
454 | } |
455 | |
456 | static enum { |
887035a5 |
457 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
374330e2 |
458 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
887035a5 |
459 | } telnet_state = TOP_LEVEL; |
374330e2 |
460 | |
32874aea |
461 | static void do_telnet_read(char *buf, int len) |
462 | { |
374330e2 |
463 | |
464 | while (len--) { |
465 | int c = (unsigned char) *buf++; |
466 | |
467 | switch (telnet_state) { |
887035a5 |
468 | case TOP_LEVEL: |
374330e2 |
469 | case SEENCR: |
470 | if (c == NUL && telnet_state == SEENCR) |
887035a5 |
471 | telnet_state = TOP_LEVEL; |
374330e2 |
472 | else if (c == IAC) |
473 | telnet_state = SEENIAC; |
474 | else { |
374330e2 |
475 | if (!in_synch) |
c9def1b8 |
476 | c_write1(c); |
2f938b83 |
477 | |
478 | #if 1 |
479 | /* I can't get the F***ing winsock to insert the urgent IAC |
480 | * into the right position! Even with SO_OOBINLINE it gives |
481 | * it to recv too soon. And of course the DM byte (that |
482 | * arrives in the same packet!) appears several K later!! |
483 | * |
484 | * Oh well, we do get the DM in the right place so I'll |
485 | * just stop hiding on the next 0xf2 and hope for the best. |
486 | */ |
32874aea |
487 | else if (c == DM) |
488 | in_synch = 0; |
2f938b83 |
489 | #endif |
374330e2 |
490 | if (c == CR) |
491 | telnet_state = SEENCR; |
492 | else |
887035a5 |
493 | telnet_state = TOP_LEVEL; |
374330e2 |
494 | } |
495 | break; |
496 | case SEENIAC: |
32874aea |
497 | if (c == DO) |
498 | telnet_state = SEENDO; |
499 | else if (c == DONT) |
500 | telnet_state = SEENDONT; |
501 | else if (c == WILL) |
502 | telnet_state = SEENWILL; |
503 | else if (c == WONT) |
504 | telnet_state = SEENWONT; |
505 | else if (c == SB) |
506 | telnet_state = SEENSB; |
2f938b83 |
507 | else if (c == DM) { |
32874aea |
508 | in_synch = 0; |
887035a5 |
509 | telnet_state = TOP_LEVEL; |
32874aea |
510 | } else { |
ded38628 |
511 | /* ignore everything else; print it if it's IAC */ |
512 | if (c == IAC) { |
c9def1b8 |
513 | c_write1(c); |
ded38628 |
514 | } |
887035a5 |
515 | telnet_state = TOP_LEVEL; |
300d41b0 |
516 | } |
374330e2 |
517 | break; |
518 | case SEENWILL: |
32874aea |
519 | proc_rec_opt(WILL, c); |
887035a5 |
520 | telnet_state = TOP_LEVEL; |
374330e2 |
521 | break; |
522 | case SEENWONT: |
32874aea |
523 | proc_rec_opt(WONT, c); |
887035a5 |
524 | telnet_state = TOP_LEVEL; |
374330e2 |
525 | break; |
526 | case SEENDO: |
32874aea |
527 | proc_rec_opt(DO, c); |
887035a5 |
528 | telnet_state = TOP_LEVEL; |
374330e2 |
529 | break; |
530 | case SEENDONT: |
32874aea |
531 | proc_rec_opt(DONT, c); |
887035a5 |
532 | telnet_state = TOP_LEVEL; |
374330e2 |
533 | break; |
534 | case SEENSB: |
535 | sb_opt = c; |
536 | sb_len = 0; |
537 | telnet_state = SUBNEGOT; |
538 | break; |
539 | case SUBNEGOT: |
540 | if (c == IAC) |
541 | telnet_state = SUBNEG_IAC; |
542 | else { |
32874aea |
543 | subneg_addchar: |
374330e2 |
544 | if (sb_len >= sb_size) { |
545 | char *newbuf; |
546 | sb_size += SB_DELTA; |
547 | newbuf = (sb_buf ? |
c9def1b8 |
548 | srealloc(sb_buf, sb_size) : |
549 | smalloc(sb_size)); |
374330e2 |
550 | if (newbuf) |
551 | sb_buf = newbuf; |
552 | else |
553 | sb_size -= SB_DELTA; |
554 | } |
555 | if (sb_len < sb_size) |
556 | sb_buf[sb_len++] = c; |
32874aea |
557 | telnet_state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
558 | } |
559 | break; |
560 | case SUBNEG_IAC: |
561 | if (c != SE) |
562 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
563 | else { |
564 | process_subneg(); |
887035a5 |
565 | telnet_state = TOP_LEVEL; |
374330e2 |
566 | } |
567 | break; |
568 | } |
569 | } |
570 | } |
571 | |
32874aea |
572 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
573 | int calling_back) |
574 | { |
f3ab576e |
575 | if (s) { |
576 | sk_close(s); |
577 | s = NULL; |
578 | } |
7e78000d |
579 | if (error_msg) { |
32874aea |
580 | /* A socket error has occurred. */ |
247308b5 |
581 | logevent(error_msg); |
582 | connection_fatal("%s", error_msg); |
32874aea |
583 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
584 | return 0; |
585 | } |
586 | |
32874aea |
587 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
588 | { |
589 | if (urgent) |
590 | in_synch = TRUE; |
591 | do_telnet_read(data, len); |
8df7a775 |
592 | return 1; |
593 | } |
594 | |
3ad9d396 |
595 | static void telnet_sent(Plug plug, int bufsize) |
596 | { |
597 | telnet_bufsize = bufsize; |
598 | } |
599 | |
374330e2 |
600 | /* |
8df7a775 |
601 | * Called to set up the Telnet connection. |
374330e2 |
602 | * |
603 | * Returns an error message, or NULL on success. |
604 | * |
6e1ebb76 |
605 | * Also places the canonical host name into `realhost'. It must be |
606 | * freed by the caller. |
374330e2 |
607 | */ |
887035a5 |
608 | static char *telnet_init(void *frontend_handle, |
609 | char *host, int port, char **realhost, int nodelay) |
32874aea |
610 | { |
7e78000d |
611 | static struct plug_function_table fn_table = { |
612 | telnet_closing, |
3ad9d396 |
613 | telnet_receive, |
614 | telnet_sent |
7e78000d |
615 | }, *fn_table_ptr = &fn_table; |
616 | |
8df7a775 |
617 | SockAddr addr; |
618 | char *err; |
374330e2 |
619 | |
887035a5 |
620 | frontend = frontend_handle; |
f278d6f8 |
621 | telnet_term_width = cfg.width; |
622 | telnet_term_height = cfg.height; |
887035a5 |
623 | |
374330e2 |
624 | /* |
625 | * Try to find host. |
626 | */ |
3ad9d396 |
627 | { |
628 | char buf[200]; |
629 | sprintf(buf, "Looking up host \"%.170s\"", host); |
630 | logevent(buf); |
631 | } |
8df7a775 |
632 | addr = sk_namelookup(host, realhost); |
32874aea |
633 | if ((err = sk_addr_error(addr))) |
8df7a775 |
634 | return err; |
374330e2 |
635 | |
636 | if (port < 0) |
637 | port = 23; /* default telnet port */ |
638 | |
639 | /* |
640 | * Open socket. |
641 | */ |
3ad9d396 |
642 | { |
643 | char buf[200], addrbuf[100]; |
644 | sk_getaddr(addr, addrbuf, 100); |
645 | sprintf(buf, "Connecting to %.100s port %d", addrbuf, port); |
646 | logevent(buf); |
647 | } |
8eebd221 |
648 | s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr); |
32874aea |
649 | if ((err = sk_socket_error(s))) |
8df7a775 |
650 | return err; |
374330e2 |
651 | |
8df7a775 |
652 | sk_addr_free(addr); |
374330e2 |
653 | |
654 | /* |
655 | * Initialise option states. |
656 | */ |
8faa456c |
657 | if (cfg.passive_telnet) { |
658 | struct Opt **o; |
659 | |
660 | for (o = opts; *o; o++) |
661 | if ((*o)->state == REQUESTED) |
662 | (*o)->state = INACTIVE; |
663 | } else { |
374330e2 |
664 | struct Opt **o; |
665 | |
666 | for (o = opts; *o; o++) |
667 | if ((*o)->state == REQUESTED) |
32874aea |
668 | send_opt((*o)->send, (*o)->option); |
8faa456c |
669 | activated = TRUE; |
374330e2 |
670 | } |
671 | |
374330e2 |
672 | /* |
673 | * Set up SYNCH state. |
674 | */ |
675 | in_synch = FALSE; |
6f34e365 |
676 | |
374330e2 |
677 | return NULL; |
678 | } |
679 | |
680 | /* |
374330e2 |
681 | * Called to send data down the Telnet connection. |
682 | */ |
5471d09a |
683 | static int telnet_send(char *buf, int len) |
32874aea |
684 | { |
374330e2 |
685 | char *p; |
686 | static unsigned char iac[2] = { IAC, IAC }; |
687 | static unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
688 | #if 0 |
eb5e1db9 |
689 | static unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
690 | #endif |
374330e2 |
691 | |
8df7a775 |
692 | if (s == NULL) |
5471d09a |
693 | return 0; |
374330e2 |
694 | |
695 | p = buf; |
32874aea |
696 | while (p < buf + len) { |
374330e2 |
697 | char *q = p; |
698 | |
32874aea |
699 | while (iswritable((unsigned char) *p) && p < buf + len) |
700 | p++; |
5471d09a |
701 | telnet_bufsize = sk_write(s, q, p - q); |
374330e2 |
702 | |
32874aea |
703 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
5471d09a |
704 | telnet_bufsize = |
705 | sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2); |
374330e2 |
706 | p++; |
707 | } |
708 | } |
5471d09a |
709 | |
710 | return telnet_bufsize; |
711 | } |
712 | |
713 | /* |
714 | * Called to query the current socket sendability status. |
715 | */ |
716 | static int telnet_sendbuffer(void) |
717 | { |
718 | return telnet_bufsize; |
374330e2 |
719 | } |
720 | |
721 | /* |
722 | * Called to set the size of the window from Telnet's POV. |
723 | */ |
f278d6f8 |
724 | static void telnet_size(int width, int height) |
32874aea |
725 | { |
374330e2 |
726 | unsigned char b[16]; |
727 | char logbuf[50]; |
728 | |
f278d6f8 |
729 | telnet_term_width = width; |
730 | telnet_term_height = height; |
731 | |
732 | if (s == NULL || o_naws.state != ACTIVE) |
374330e2 |
733 | return; |
32874aea |
734 | b[0] = IAC; |
735 | b[1] = SB; |
736 | b[2] = TELOPT_NAWS; |
f278d6f8 |
737 | b[3] = telnet_term_width >> 8; |
738 | b[4] = telnet_term_width & 0xFF; |
739 | b[5] = telnet_term_height >> 8; |
740 | b[6] = telnet_term_height & 0xFF; |
32874aea |
741 | b[7] = IAC; |
742 | b[8] = SE; |
5471d09a |
743 | telnet_bufsize = sk_write(s, b, 9); |
374330e2 |
744 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
32874aea |
745 | ((unsigned char) b[3] << 8) + (unsigned char) b[4], |
746 | ((unsigned char) b[5] << 8) + (unsigned char) b[6]); |
747 | logevent(logbuf); |
374330e2 |
748 | } |
749 | |
750 | /* |
751 | * Send Telnet special codes. |
752 | */ |
32874aea |
753 | static void telnet_special(Telnet_Special code) |
754 | { |
374330e2 |
755 | unsigned char b[2]; |
756 | |
8df7a775 |
757 | if (s == NULL) |
374330e2 |
758 | return; |
759 | |
760 | b[0] = IAC; |
761 | switch (code) { |
32874aea |
762 | case TS_AYT: |
763 | b[1] = AYT; |
5471d09a |
764 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
765 | break; |
766 | case TS_BRK: |
767 | b[1] = BREAK; |
5471d09a |
768 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
769 | break; |
770 | case TS_EC: |
771 | b[1] = EC; |
5471d09a |
772 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
773 | break; |
774 | case TS_EL: |
775 | b[1] = EL; |
5471d09a |
776 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
777 | break; |
778 | case TS_GA: |
779 | b[1] = GA; |
5471d09a |
780 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
781 | break; |
782 | case TS_NOP: |
783 | b[1] = NOP; |
5471d09a |
784 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
785 | break; |
786 | case TS_ABORT: |
787 | b[1] = ABORT; |
5471d09a |
788 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
789 | break; |
790 | case TS_AO: |
791 | b[1] = AO; |
5471d09a |
792 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
793 | break; |
794 | case TS_IP: |
795 | b[1] = IP; |
5471d09a |
796 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
797 | break; |
798 | case TS_SUSP: |
799 | b[1] = SUSP; |
5471d09a |
800 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
801 | break; |
802 | case TS_EOR: |
803 | b[1] = EOR; |
5471d09a |
804 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
805 | break; |
806 | case TS_EOF: |
807 | b[1] = xEOF; |
5471d09a |
808 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
809 | break; |
a5f3e637 |
810 | case TS_EOL: |
5471d09a |
811 | telnet_bufsize = sk_write(s, "\r\n", 2); |
a5f3e637 |
812 | break; |
374330e2 |
813 | case TS_SYNCH: |
32874aea |
814 | b[1] = DM; |
5471d09a |
815 | telnet_bufsize = sk_write(s, b, 1); |
816 | telnet_bufsize = sk_write_oob(s, b + 1, 1); |
32874aea |
817 | break; |
684d367c |
818 | case TS_RECHO: |
819 | if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) { |
820 | o_echo.state = REQUESTED; |
32874aea |
821 | send_opt(o_echo.send, o_echo.option); |
684d367c |
822 | } |
823 | break; |
824 | case TS_LECHO: |
825 | if (o_echo.state == ACTIVE) { |
826 | o_echo.state = REQUESTED; |
32874aea |
827 | send_opt(o_echo.nsend, o_echo.option); |
684d367c |
828 | } |
374330e2 |
829 | break; |
32874aea |
830 | case TS_PING: |
ec55b220 |
831 | if (o_they_sga.state == ACTIVE) { |
32874aea |
832 | b[1] = NOP; |
5471d09a |
833 | telnet_bufsize = sk_write(s, b, 2); |
ec55b220 |
834 | } |
32874aea |
835 | break; |
374330e2 |
836 | } |
837 | } |
838 | |
32874aea |
839 | static Socket telnet_socket(void) |
840 | { |
841 | return s; |
842 | } |
8ccc75b0 |
843 | |
32874aea |
844 | static int telnet_sendok(void) |
845 | { |
846 | return 1; |
847 | } |
4017be6d |
848 | |
5471d09a |
849 | static void telnet_unthrottle(int backlog) |
850 | { |
851 | sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG); |
852 | } |
853 | |
32874aea |
854 | static int telnet_ldisc(int option) |
855 | { |
856 | if (option == LD_ECHO) |
857 | return echoing; |
858 | if (option == LD_EDIT) |
859 | return editing; |
0965bee0 |
860 | return FALSE; |
861 | } |
862 | |
d8d6c7e5 |
863 | static int telnet_exitcode(void) |
864 | { |
865 | /* Telnet doesn't transmit exit codes back to the client */ |
866 | return 0; |
867 | } |
868 | |
374330e2 |
869 | Backend telnet_backend = { |
870 | telnet_init, |
374330e2 |
871 | telnet_send, |
5471d09a |
872 | telnet_sendbuffer, |
374330e2 |
873 | telnet_size, |
4017be6d |
874 | telnet_special, |
8ccc75b0 |
875 | telnet_socket, |
d8d6c7e5 |
876 | telnet_exitcode, |
97db3be4 |
877 | telnet_sendok, |
0965bee0 |
878 | telnet_ldisc, |
5471d09a |
879 | telnet_unthrottle, |
97db3be4 |
880 | 23 |
374330e2 |
881 | }; |