Attempt to ensure that everything passed to connection_fatal() is
[u/mdw/putty] / telnet.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
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
14 static Socket s = NULL;
15
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 */
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
94 static char *telopt(int opt)
95 {
96 #define i(x) if (opt == TELOPT_ ## x) return #x;
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);
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
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 };
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
177 #define TELNET_MAX_BACKLOG 4096
178
179 static int echoing = TRUE, editing = TRUE;
180 static int activated = FALSE;
181 static int telnet_bufsize;
182 static int in_synch;
183 static int sb_opt, sb_len;
184 static char *sb_buf = NULL;
185 static int sb_size = 0;
186 #define SB_DELTA 1024
187
188 static void c_write1(int c)
189 {
190 int backlog;
191 char cc = (char) c;
192 backlog = from_backend(0, &cc, 1);
193 sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG);
194 }
195
196 static void log_option(char *sender, int cmd, int option)
197 {
198 char buf[50];
199 sprintf(buf, "%s:\t%s %s", sender,
200 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
201 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
202 telopt(option));
203 logevent(buf);
204 }
205
206 static void send_opt(int cmd, int option)
207 {
208 unsigned char b[3];
209
210 b[0] = IAC;
211 b[1] = cmd;
212 b[2] = option;
213 telnet_bufsize = sk_write(s, b, 3);
214 log_option("client", cmd, option);
215 }
216
217 static void deactivate_option(struct Opt *o)
218 {
219 if (o->state == REQUESTED || o->state == ACTIVE)
220 send_opt(o->nsend, o->option);
221 o->state = REALLY_INACTIVE;
222 }
223
224 /*
225 * Generate side effects of enabling or disabling an option.
226 */
227 static void option_side_effects(struct Opt *o, int enabled)
228 {
229 if (o->option == TELOPT_ECHO && o->send == DO)
230 echoing = !enabled;
231 else if (o->option == TELOPT_SGA && o->send == DO)
232 editing = !enabled;
233 ldisc_send(NULL, 0, 0); /* cause ldisc to notice the change */
234
235 /* Ensure we get the minimum options */
236 if (!activated) {
237 if (o_echo.state == INACTIVE) {
238 o_echo.state = REQUESTED;
239 send_opt(o_echo.send, o_echo.option);
240 }
241 if (o_we_sga.state == INACTIVE) {
242 o_we_sga.state = REQUESTED;
243 send_opt(o_we_sga.send, o_we_sga.option);
244 }
245 if (o_they_sga.state == INACTIVE) {
246 o_they_sga.state = REQUESTED;
247 send_opt(o_they_sga.send, o_they_sga.option);
248 }
249 activated = TRUE;
250 }
251 }
252
253 static void activate_option(struct Opt *o)
254 {
255 if (o->send == WILL && o->option == TELOPT_NAWS)
256 telnet_size();
257 if (o->send == WILL &&
258 (o->option == TELOPT_NEW_ENVIRON ||
259 o->option == TELOPT_OLD_ENVIRON)) {
260 /*
261 * We may only have one kind of ENVIRON going at a time.
262 * This is a hack, but who cares.
263 */
264 deactivate_option(o->option ==
265 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
266 }
267 option_side_effects(o, 1);
268 }
269
270 static void refused_option(struct Opt *o)
271 {
272 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
273 o_oenv.state == INACTIVE) {
274 send_opt(WILL, TELOPT_OLD_ENVIRON);
275 o_oenv.state = REQUESTED;
276 }
277 option_side_effects(o, 0);
278 }
279
280 static void proc_rec_opt(int cmd, int option)
281 {
282 struct Opt **o;
283
284 log_option("server", cmd, option);
285 for (o = opts; *o; o++) {
286 if ((*o)->option == option && (*o)->ack == cmd) {
287 switch ((*o)->state) {
288 case REQUESTED:
289 (*o)->state = ACTIVE;
290 activate_option(*o);
291 break;
292 case ACTIVE:
293 break;
294 case INACTIVE:
295 (*o)->state = ACTIVE;
296 send_opt((*o)->send, option);
297 activate_option(*o);
298 break;
299 case REALLY_INACTIVE:
300 send_opt((*o)->nsend, option);
301 break;
302 }
303 return;
304 } else if ((*o)->option == option && (*o)->nak == cmd) {
305 switch ((*o)->state) {
306 case REQUESTED:
307 (*o)->state = INACTIVE;
308 refused_option(*o);
309 break;
310 case ACTIVE:
311 (*o)->state = INACTIVE;
312 send_opt((*o)->nsend, option);
313 option_side_effects(*o, 0);
314 break;
315 case INACTIVE:
316 case REALLY_INACTIVE:
317 break;
318 }
319 return;
320 }
321 }
322 /*
323 * If we reach here, the option was one we weren't prepared to
324 * cope with. So send a negative ack.
325 */
326 send_opt((cmd == WILL ? DONT : WONT), option);
327 }
328
329 static void process_subneg(void)
330 {
331 unsigned char b[2048], *p, *q;
332 int var, value, n;
333 char *e;
334
335 switch (sb_opt) {
336 case TELOPT_TSPEED:
337 if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
338 char logbuf[sizeof(cfg.termspeed) + 80];
339 b[0] = IAC;
340 b[1] = SB;
341 b[2] = TELOPT_TSPEED;
342 b[3] = TELQUAL_IS;
343 strcpy(b + 4, cfg.termspeed);
344 n = 4 + strlen(cfg.termspeed);
345 b[n] = IAC;
346 b[n + 1] = SE;
347 telnet_bufsize = sk_write(s, b, n + 2);
348 logevent("server:\tSB TSPEED SEND");
349 sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
350 logevent(logbuf);
351 } else
352 logevent("server:\tSB TSPEED <something weird>");
353 break;
354 case TELOPT_TTYPE:
355 if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
356 char logbuf[sizeof(cfg.termtype) + 80];
357 b[0] = IAC;
358 b[1] = SB;
359 b[2] = TELOPT_TTYPE;
360 b[3] = TELQUAL_IS;
361 for (n = 0; cfg.termtype[n]; n++)
362 b[n + 4] = (cfg.termtype[n] >= 'a'
363 && cfg.termtype[n] <=
364 'z' ? cfg.termtype[n] + 'A' -
365 'a' : cfg.termtype[n]);
366 b[n + 4] = IAC;
367 b[n + 5] = SE;
368 telnet_bufsize = sk_write(s, b, n + 6);
369 b[n + 4] = 0;
370 logevent("server:\tSB TTYPE SEND");
371 sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
372 logevent(logbuf);
373 } else
374 logevent("server:\tSB TTYPE <something weird>\r\n");
375 break;
376 case TELOPT_OLD_ENVIRON:
377 case TELOPT_NEW_ENVIRON:
378 p = sb_buf;
379 q = p + sb_len;
380 if (p < q && *p == TELQUAL_SEND) {
381 char logbuf[50];
382 p++;
383 sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt));
384 logevent(logbuf);
385 if (sb_opt == TELOPT_OLD_ENVIRON) {
386 if (cfg.rfc_environ) {
387 value = RFC_VALUE;
388 var = RFC_VAR;
389 } else {
390 value = BSD_VALUE;
391 var = BSD_VAR;
392 }
393 /*
394 * Try to guess the sense of VAR and VALUE.
395 */
396 while (p < q) {
397 if (*p == RFC_VAR) {
398 value = RFC_VALUE;
399 var = RFC_VAR;
400 } else if (*p == BSD_VAR) {
401 value = BSD_VALUE;
402 var = BSD_VAR;
403 }
404 p++;
405 }
406 } else {
407 /*
408 * With NEW_ENVIRON, the sense of VAR and VALUE
409 * isn't in doubt.
410 */
411 value = RFC_VALUE;
412 var = RFC_VAR;
413 }
414 b[0] = IAC;
415 b[1] = SB;
416 b[2] = sb_opt;
417 b[3] = TELQUAL_IS;
418 n = 4;
419 e = cfg.environmt;
420 while (*e) {
421 b[n++] = var;
422 while (*e && *e != '\t')
423 b[n++] = *e++;
424 if (*e == '\t')
425 e++;
426 b[n++] = value;
427 while (*e)
428 b[n++] = *e++;
429 e++;
430 }
431 if (*cfg.username) {
432 b[n++] = var;
433 b[n++] = 'U';
434 b[n++] = 'S';
435 b[n++] = 'E';
436 b[n++] = 'R';
437 b[n++] = value;
438 e = cfg.username;
439 while (*e)
440 b[n++] = *e++;
441 }
442 b[n++] = IAC;
443 b[n++] = SE;
444 telnet_bufsize = sk_write(s, b, n);
445 sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
446 n == 6 ? "<nothing>" : "<stuff>");
447 logevent(logbuf);
448 }
449 break;
450 }
451 }
452
453 static enum {
454 TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
455 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
456 } telnet_state = TOPLEVEL;
457
458 static void do_telnet_read(char *buf, int len)
459 {
460
461 while (len--) {
462 int c = (unsigned char) *buf++;
463
464 switch (telnet_state) {
465 case TOPLEVEL:
466 case SEENCR:
467 if (c == NUL && telnet_state == SEENCR)
468 telnet_state = TOPLEVEL;
469 else if (c == IAC)
470 telnet_state = SEENIAC;
471 else {
472 if (!in_synch)
473 c_write1(c);
474
475 #if 1
476 /* I can't get the F***ing winsock to insert the urgent IAC
477 * into the right position! Even with SO_OOBINLINE it gives
478 * it to recv too soon. And of course the DM byte (that
479 * arrives in the same packet!) appears several K later!!
480 *
481 * Oh well, we do get the DM in the right place so I'll
482 * just stop hiding on the next 0xf2 and hope for the best.
483 */
484 else if (c == DM)
485 in_synch = 0;
486 #endif
487 if (c == CR)
488 telnet_state = SEENCR;
489 else
490 telnet_state = TOPLEVEL;
491 }
492 break;
493 case SEENIAC:
494 if (c == DO)
495 telnet_state = SEENDO;
496 else if (c == DONT)
497 telnet_state = SEENDONT;
498 else if (c == WILL)
499 telnet_state = SEENWILL;
500 else if (c == WONT)
501 telnet_state = SEENWONT;
502 else if (c == SB)
503 telnet_state = SEENSB;
504 else if (c == DM) {
505 in_synch = 0;
506 telnet_state = TOPLEVEL;
507 } else {
508 /* ignore everything else; print it if it's IAC */
509 if (c == IAC) {
510 c_write1(c);
511 }
512 telnet_state = TOPLEVEL;
513 }
514 break;
515 case SEENWILL:
516 proc_rec_opt(WILL, c);
517 telnet_state = TOPLEVEL;
518 break;
519 case SEENWONT:
520 proc_rec_opt(WONT, c);
521 telnet_state = TOPLEVEL;
522 break;
523 case SEENDO:
524 proc_rec_opt(DO, c);
525 telnet_state = TOPLEVEL;
526 break;
527 case SEENDONT:
528 proc_rec_opt(DONT, c);
529 telnet_state = TOPLEVEL;
530 break;
531 case SEENSB:
532 sb_opt = c;
533 sb_len = 0;
534 telnet_state = SUBNEGOT;
535 break;
536 case SUBNEGOT:
537 if (c == IAC)
538 telnet_state = SUBNEG_IAC;
539 else {
540 subneg_addchar:
541 if (sb_len >= sb_size) {
542 char *newbuf;
543 sb_size += SB_DELTA;
544 newbuf = (sb_buf ?
545 srealloc(sb_buf, sb_size) :
546 smalloc(sb_size));
547 if (newbuf)
548 sb_buf = newbuf;
549 else
550 sb_size -= SB_DELTA;
551 }
552 if (sb_len < sb_size)
553 sb_buf[sb_len++] = c;
554 telnet_state = SUBNEGOT; /* in case we came here by goto */
555 }
556 break;
557 case SUBNEG_IAC:
558 if (c != SE)
559 goto subneg_addchar; /* yes, it's a hack, I know, but... */
560 else {
561 process_subneg();
562 telnet_state = TOPLEVEL;
563 }
564 break;
565 }
566 }
567 }
568
569 static int telnet_closing(Plug plug, char *error_msg, int error_code,
570 int calling_back)
571 {
572 if (s) {
573 sk_close(s);
574 s = NULL;
575 }
576 if (error_msg) {
577 /* A socket error has occurred. */
578 logevent(error_msg);
579 connection_fatal("%s", error_msg);
580 } /* Otherwise, the remote side closed the connection normally. */
581 return 0;
582 }
583
584 static int telnet_receive(Plug plug, int urgent, char *data, int len)
585 {
586 if (urgent)
587 in_synch = TRUE;
588 do_telnet_read(data, len);
589 return 1;
590 }
591
592 static void telnet_sent(Plug plug, int bufsize)
593 {
594 telnet_bufsize = bufsize;
595 }
596
597 /*
598 * Called to set up the Telnet connection.
599 *
600 * Returns an error message, or NULL on success.
601 *
602 * Also places the canonical host name into `realhost'. It must be
603 * freed by the caller.
604 */
605 static char *telnet_init(char *host, int port, char **realhost, int nodelay)
606 {
607 static struct plug_function_table fn_table = {
608 telnet_closing,
609 telnet_receive,
610 telnet_sent
611 }, *fn_table_ptr = &fn_table;
612
613 SockAddr addr;
614 char *err;
615
616 /*
617 * Try to find host.
618 */
619 {
620 char buf[200];
621 sprintf(buf, "Looking up host \"%.170s\"", host);
622 logevent(buf);
623 }
624 addr = sk_namelookup(host, realhost);
625 if ((err = sk_addr_error(addr)))
626 return err;
627
628 if (port < 0)
629 port = 23; /* default telnet port */
630
631 /*
632 * Open socket.
633 */
634 {
635 char buf[200], addrbuf[100];
636 sk_getaddr(addr, addrbuf, 100);
637 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
638 logevent(buf);
639 }
640 s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr);
641 if ((err = sk_socket_error(s)))
642 return err;
643
644 sk_addr_free(addr);
645
646 /*
647 * Initialise option states.
648 */
649 if (cfg.passive_telnet) {
650 struct Opt **o;
651
652 for (o = opts; *o; o++)
653 if ((*o)->state == REQUESTED)
654 (*o)->state = INACTIVE;
655 } else {
656 struct Opt **o;
657
658 for (o = opts; *o; o++)
659 if ((*o)->state == REQUESTED)
660 send_opt((*o)->send, (*o)->option);
661 activated = TRUE;
662 }
663
664 /*
665 * Set up SYNCH state.
666 */
667 in_synch = FALSE;
668
669 return NULL;
670 }
671
672 /*
673 * Called to send data down the Telnet connection.
674 */
675 static int telnet_send(char *buf, int len)
676 {
677 char *p;
678 static unsigned char iac[2] = { IAC, IAC };
679 static unsigned char cr[2] = { CR, NUL };
680 #if 0
681 static unsigned char nl[2] = { CR, LF };
682 #endif
683
684 if (s == NULL)
685 return 0;
686
687 p = buf;
688 while (p < buf + len) {
689 char *q = p;
690
691 while (iswritable((unsigned char) *p) && p < buf + len)
692 p++;
693 telnet_bufsize = sk_write(s, q, p - q);
694
695 while (p < buf + len && !iswritable((unsigned char) *p)) {
696 telnet_bufsize =
697 sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2);
698 p++;
699 }
700 }
701
702 return telnet_bufsize;
703 }
704
705 /*
706 * Called to query the current socket sendability status.
707 */
708 static int telnet_sendbuffer(void)
709 {
710 return telnet_bufsize;
711 }
712
713 /*
714 * Called to set the size of the window from Telnet's POV.
715 */
716 static void telnet_size(void)
717 {
718 unsigned char b[16];
719 char logbuf[50];
720
721 if (s == NULL || o_naws.state != ACTIVE)
722 return;
723 b[0] = IAC;
724 b[1] = SB;
725 b[2] = TELOPT_NAWS;
726 b[3] = cols >> 8;
727 b[4] = cols & 0xFF;
728 b[5] = rows >> 8;
729 b[6] = rows & 0xFF;
730 b[7] = IAC;
731 b[8] = SE;
732 telnet_bufsize = sk_write(s, b, 9);
733 sprintf(logbuf, "client:\tSB NAWS %d,%d",
734 ((unsigned char) b[3] << 8) + (unsigned char) b[4],
735 ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
736 logevent(logbuf);
737 }
738
739 /*
740 * Send Telnet special codes.
741 */
742 static void telnet_special(Telnet_Special code)
743 {
744 unsigned char b[2];
745
746 if (s == NULL)
747 return;
748
749 b[0] = IAC;
750 switch (code) {
751 case TS_AYT:
752 b[1] = AYT;
753 telnet_bufsize = sk_write(s, b, 2);
754 break;
755 case TS_BRK:
756 b[1] = BREAK;
757 telnet_bufsize = sk_write(s, b, 2);
758 break;
759 case TS_EC:
760 b[1] = EC;
761 telnet_bufsize = sk_write(s, b, 2);
762 break;
763 case TS_EL:
764 b[1] = EL;
765 telnet_bufsize = sk_write(s, b, 2);
766 break;
767 case TS_GA:
768 b[1] = GA;
769 telnet_bufsize = sk_write(s, b, 2);
770 break;
771 case TS_NOP:
772 b[1] = NOP;
773 telnet_bufsize = sk_write(s, b, 2);
774 break;
775 case TS_ABORT:
776 b[1] = ABORT;
777 telnet_bufsize = sk_write(s, b, 2);
778 break;
779 case TS_AO:
780 b[1] = AO;
781 telnet_bufsize = sk_write(s, b, 2);
782 break;
783 case TS_IP:
784 b[1] = IP;
785 telnet_bufsize = sk_write(s, b, 2);
786 break;
787 case TS_SUSP:
788 b[1] = SUSP;
789 telnet_bufsize = sk_write(s, b, 2);
790 break;
791 case TS_EOR:
792 b[1] = EOR;
793 telnet_bufsize = sk_write(s, b, 2);
794 break;
795 case TS_EOF:
796 b[1] = xEOF;
797 telnet_bufsize = sk_write(s, b, 2);
798 break;
799 case TS_EOL:
800 telnet_bufsize = sk_write(s, "\r\n", 2);
801 break;
802 case TS_SYNCH:
803 b[1] = DM;
804 telnet_bufsize = sk_write(s, b, 1);
805 telnet_bufsize = sk_write_oob(s, b + 1, 1);
806 break;
807 case TS_RECHO:
808 if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) {
809 o_echo.state = REQUESTED;
810 send_opt(o_echo.send, o_echo.option);
811 }
812 break;
813 case TS_LECHO:
814 if (o_echo.state == ACTIVE) {
815 o_echo.state = REQUESTED;
816 send_opt(o_echo.nsend, o_echo.option);
817 }
818 break;
819 case TS_PING:
820 if (o_they_sga.state == ACTIVE) {
821 b[1] = NOP;
822 telnet_bufsize = sk_write(s, b, 2);
823 }
824 break;
825 }
826 }
827
828 static Socket telnet_socket(void)
829 {
830 return s;
831 }
832
833 static int telnet_sendok(void)
834 {
835 return 1;
836 }
837
838 static void telnet_unthrottle(int backlog)
839 {
840 sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG);
841 }
842
843 static int telnet_ldisc(int option)
844 {
845 if (option == LD_ECHO)
846 return echoing;
847 if (option == LD_EDIT)
848 return editing;
849 return FALSE;
850 }
851
852 static int telnet_exitcode(void)
853 {
854 /* Telnet doesn't transmit exit codes back to the client */
855 return 0;
856 }
857
858 Backend telnet_backend = {
859 telnet_init,
860 telnet_send,
861 telnet_sendbuffer,
862 telnet_size,
863 telnet_special,
864 telnet_socket,
865 telnet_exitcode,
866 telnet_sendok,
867 telnet_ldisc,
868 telnet_unthrottle,
869 23
870 };