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