Cleanups from yesterday's destabilisation: lots of stuff in
[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 static void *frontend;
17 static int telnet_term_width, telnet_term_height;
18
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 */
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
97 static char *telopt(int opt)
98 {
99 #define i(x) if (opt == TELOPT_ ## x) return #x;
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);
140 #undef i
141 return "<unknown>";
142 }
143
144 static void telnet_size(void);
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
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 };
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
180 #define TELNET_MAX_BACKLOG 4096
181
182 static int echoing = TRUE, editing = TRUE;
183 static int activated = FALSE;
184 static int telnet_bufsize;
185 static int in_synch;
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
191 static void c_write1(int c)
192 {
193 int backlog;
194 char cc = (char) c;
195 backlog = from_backend(frontend, 0, &cc, 1);
196 sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG);
197 }
198
199 static void log_option(char *sender, int cmd, int option)
200 {
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));
206 logevent(buf);
207 }
208
209 static void send_opt(int cmd, int option)
210 {
211 unsigned char b[3];
212
213 b[0] = IAC;
214 b[1] = cmd;
215 b[2] = option;
216 telnet_bufsize = sk_write(s, b, 3);
217 log_option("client", cmd, option);
218 }
219
220 static void deactivate_option(struct Opt *o)
221 {
222 if (o->state == REQUESTED || o->state == ACTIVE)
223 send_opt(o->nsend, o->option);
224 o->state = REALLY_INACTIVE;
225 }
226
227 /*
228 * Generate side effects of enabling or disabling an option.
229 */
230 static void option_side_effects(struct Opt *o, int enabled)
231 {
232 if (o->option == TELOPT_ECHO && o->send == DO)
233 echoing = !enabled;
234 else if (o->option == TELOPT_SGA && o->send == DO)
235 editing = !enabled;
236 ldisc_send(NULL, 0, 0); /* cause ldisc to notice the change */
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 }
254 }
255
256 static void activate_option(struct Opt *o)
257 {
258 if (o->send == WILL && o->option == TELOPT_NAWS)
259 telnet_size();
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 */
267 deactivate_option(o->option ==
268 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
269 }
270 option_side_effects(o, 1);
271 }
272
273 static void refused_option(struct Opt *o)
274 {
275 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
276 o_oenv.state == INACTIVE) {
277 send_opt(WILL, TELOPT_OLD_ENVIRON);
278 o_oenv.state = REQUESTED;
279 }
280 option_side_effects(o, 0);
281 }
282
283 static void proc_rec_opt(int cmd, int option)
284 {
285 struct Opt **o;
286
287 log_option("server", cmd, option);
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;
293 activate_option(*o);
294 break;
295 case ACTIVE:
296 break;
297 case INACTIVE:
298 (*o)->state = ACTIVE;
299 send_opt((*o)->send, option);
300 activate_option(*o);
301 break;
302 case REALLY_INACTIVE:
303 send_opt((*o)->nsend, option);
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;
311 refused_option(*o);
312 break;
313 case ACTIVE:
314 (*o)->state = INACTIVE;
315 send_opt((*o)->nsend, option);
316 option_side_effects(*o, 0);
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 */
329 send_opt((cmd == WILL ? DONT : WONT), option);
330 }
331
332 static void process_subneg(void)
333 {
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) {
341 char logbuf[sizeof(cfg.termspeed) + 80];
342 b[0] = IAC;
343 b[1] = SB;
344 b[2] = TELOPT_TSPEED;
345 b[3] = TELQUAL_IS;
346 strcpy(b + 4, cfg.termspeed);
347 n = 4 + strlen(cfg.termspeed);
348 b[n] = IAC;
349 b[n + 1] = SE;
350 telnet_bufsize = sk_write(s, b, n + 2);
351 logevent("server:\tSB TSPEED SEND");
352 sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
353 logevent(logbuf);
354 } else
355 logevent("server:\tSB TSPEED <something weird>");
356 break;
357 case TELOPT_TTYPE:
358 if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
359 char logbuf[sizeof(cfg.termtype) + 80];
360 b[0] = IAC;
361 b[1] = SB;
362 b[2] = TELOPT_TTYPE;
363 b[3] = TELQUAL_IS;
364 for (n = 0; cfg.termtype[n]; n++)
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;
371 telnet_bufsize = sk_write(s, b, n + 6);
372 b[n + 4] = 0;
373 logevent("server:\tSB TTYPE SEND");
374 sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
375 logevent(logbuf);
376 } else
377 logevent("server:\tSB TTYPE <something weird>\r\n");
378 break;
379 case TELOPT_OLD_ENVIRON:
380 case TELOPT_NEW_ENVIRON:
381 p = sb_buf;
382 q = p + sb_len;
383 if (p < q && *p == TELQUAL_SEND) {
384 char logbuf[50];
385 p++;
386 sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt));
387 logevent(logbuf);
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 }
417 b[0] = IAC;
418 b[1] = SB;
419 b[2] = sb_opt;
420 b[3] = TELQUAL_IS;
421 n = 4;
422 e = cfg.environmt;
423 while (*e) {
424 b[n++] = var;
425 while (*e && *e != '\t')
426 b[n++] = *e++;
427 if (*e == '\t')
428 e++;
429 b[n++] = value;
430 while (*e)
431 b[n++] = *e++;
432 e++;
433 }
434 if (*cfg.username) {
435 b[n++] = var;
436 b[n++] = 'U';
437 b[n++] = 'S';
438 b[n++] = 'E';
439 b[n++] = 'R';
440 b[n++] = value;
441 e = cfg.username;
442 while (*e)
443 b[n++] = *e++;
444 }
445 b[n++] = IAC;
446 b[n++] = SE;
447 telnet_bufsize = sk_write(s, b, n);
448 sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
449 n == 6 ? "<nothing>" : "<stuff>");
450 logevent(logbuf);
451 }
452 break;
453 }
454 }
455
456 static enum {
457 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
458 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
459 } telnet_state = TOP_LEVEL;
460
461 static void do_telnet_read(char *buf, int len)
462 {
463
464 while (len--) {
465 int c = (unsigned char) *buf++;
466
467 switch (telnet_state) {
468 case TOP_LEVEL:
469 case SEENCR:
470 if (c == NUL && telnet_state == SEENCR)
471 telnet_state = TOP_LEVEL;
472 else if (c == IAC)
473 telnet_state = SEENIAC;
474 else {
475 if (!in_synch)
476 c_write1(c);
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 */
487 else if (c == DM)
488 in_synch = 0;
489 #endif
490 if (c == CR)
491 telnet_state = SEENCR;
492 else
493 telnet_state = TOP_LEVEL;
494 }
495 break;
496 case SEENIAC:
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;
507 else if (c == DM) {
508 in_synch = 0;
509 telnet_state = TOP_LEVEL;
510 } else {
511 /* ignore everything else; print it if it's IAC */
512 if (c == IAC) {
513 c_write1(c);
514 }
515 telnet_state = TOP_LEVEL;
516 }
517 break;
518 case SEENWILL:
519 proc_rec_opt(WILL, c);
520 telnet_state = TOP_LEVEL;
521 break;
522 case SEENWONT:
523 proc_rec_opt(WONT, c);
524 telnet_state = TOP_LEVEL;
525 break;
526 case SEENDO:
527 proc_rec_opt(DO, c);
528 telnet_state = TOP_LEVEL;
529 break;
530 case SEENDONT:
531 proc_rec_opt(DONT, c);
532 telnet_state = TOP_LEVEL;
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 {
543 subneg_addchar:
544 if (sb_len >= sb_size) {
545 char *newbuf;
546 sb_size += SB_DELTA;
547 newbuf = (sb_buf ?
548 srealloc(sb_buf, sb_size) :
549 smalloc(sb_size));
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;
557 telnet_state = SUBNEGOT; /* in case we came here by goto */
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();
565 telnet_state = TOP_LEVEL;
566 }
567 break;
568 }
569 }
570 }
571
572 static int telnet_closing(Plug plug, char *error_msg, int error_code,
573 int calling_back)
574 {
575 if (s) {
576 sk_close(s);
577 s = NULL;
578 }
579 if (error_msg) {
580 /* A socket error has occurred. */
581 logevent(error_msg);
582 connection_fatal("%s", error_msg);
583 } /* Otherwise, the remote side closed the connection normally. */
584 return 0;
585 }
586
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);
592 return 1;
593 }
594
595 static void telnet_sent(Plug plug, int bufsize)
596 {
597 telnet_bufsize = bufsize;
598 }
599
600 /*
601 * Called to set up the Telnet connection.
602 *
603 * Returns an error message, or NULL on success.
604 *
605 * Also places the canonical host name into `realhost'. It must be
606 * freed by the caller.
607 */
608 static char *telnet_init(void *frontend_handle,
609 char *host, int port, char **realhost, int nodelay)
610 {
611 static struct plug_function_table fn_table = {
612 telnet_closing,
613 telnet_receive,
614 telnet_sent
615 }, *fn_table_ptr = &fn_table;
616
617 SockAddr addr;
618 char *err;
619
620 frontend = frontend_handle;
621 telnet_term_width = cfg.width;
622 telnet_term_height = cfg.height;
623
624 /*
625 * Try to find host.
626 */
627 {
628 char buf[200];
629 sprintf(buf, "Looking up host \"%.170s\"", host);
630 logevent(buf);
631 }
632 addr = sk_namelookup(host, realhost);
633 if ((err = sk_addr_error(addr)))
634 return err;
635
636 if (port < 0)
637 port = 23; /* default telnet port */
638
639 /*
640 * Open socket.
641 */
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 }
648 s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr);
649 if ((err = sk_socket_error(s)))
650 return err;
651
652 sk_addr_free(addr);
653
654 /*
655 * Initialise option states.
656 */
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 {
664 struct Opt **o;
665
666 for (o = opts; *o; o++)
667 if ((*o)->state == REQUESTED)
668 send_opt((*o)->send, (*o)->option);
669 activated = TRUE;
670 }
671
672 /*
673 * Set up SYNCH state.
674 */
675 in_synch = FALSE;
676
677 return NULL;
678 }
679
680 /*
681 * Called to send data down the Telnet connection.
682 */
683 static int telnet_send(char *buf, int len)
684 {
685 char *p;
686 static unsigned char iac[2] = { IAC, IAC };
687 static unsigned char cr[2] = { CR, NUL };
688 #if 0
689 static unsigned char nl[2] = { CR, LF };
690 #endif
691
692 if (s == NULL)
693 return 0;
694
695 p = buf;
696 while (p < buf + len) {
697 char *q = p;
698
699 while (iswritable((unsigned char) *p) && p < buf + len)
700 p++;
701 telnet_bufsize = sk_write(s, q, p - q);
702
703 while (p < buf + len && !iswritable((unsigned char) *p)) {
704 telnet_bufsize =
705 sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2);
706 p++;
707 }
708 }
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;
719 }
720
721 /*
722 * Called to set the size of the window from Telnet's POV.
723 */
724 static void telnet_size(int width, int height)
725 {
726 unsigned char b[16];
727 char logbuf[50];
728
729 telnet_term_width = width;
730 telnet_term_height = height;
731
732 if (s == NULL || o_naws.state != ACTIVE)
733 return;
734 b[0] = IAC;
735 b[1] = SB;
736 b[2] = TELOPT_NAWS;
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;
741 b[7] = IAC;
742 b[8] = SE;
743 telnet_bufsize = sk_write(s, b, 9);
744 sprintf(logbuf, "client:\tSB NAWS %d,%d",
745 ((unsigned char) b[3] << 8) + (unsigned char) b[4],
746 ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
747 logevent(logbuf);
748 }
749
750 /*
751 * Send Telnet special codes.
752 */
753 static void telnet_special(Telnet_Special code)
754 {
755 unsigned char b[2];
756
757 if (s == NULL)
758 return;
759
760 b[0] = IAC;
761 switch (code) {
762 case TS_AYT:
763 b[1] = AYT;
764 telnet_bufsize = sk_write(s, b, 2);
765 break;
766 case TS_BRK:
767 b[1] = BREAK;
768 telnet_bufsize = sk_write(s, b, 2);
769 break;
770 case TS_EC:
771 b[1] = EC;
772 telnet_bufsize = sk_write(s, b, 2);
773 break;
774 case TS_EL:
775 b[1] = EL;
776 telnet_bufsize = sk_write(s, b, 2);
777 break;
778 case TS_GA:
779 b[1] = GA;
780 telnet_bufsize = sk_write(s, b, 2);
781 break;
782 case TS_NOP:
783 b[1] = NOP;
784 telnet_bufsize = sk_write(s, b, 2);
785 break;
786 case TS_ABORT:
787 b[1] = ABORT;
788 telnet_bufsize = sk_write(s, b, 2);
789 break;
790 case TS_AO:
791 b[1] = AO;
792 telnet_bufsize = sk_write(s, b, 2);
793 break;
794 case TS_IP:
795 b[1] = IP;
796 telnet_bufsize = sk_write(s, b, 2);
797 break;
798 case TS_SUSP:
799 b[1] = SUSP;
800 telnet_bufsize = sk_write(s, b, 2);
801 break;
802 case TS_EOR:
803 b[1] = EOR;
804 telnet_bufsize = sk_write(s, b, 2);
805 break;
806 case TS_EOF:
807 b[1] = xEOF;
808 telnet_bufsize = sk_write(s, b, 2);
809 break;
810 case TS_EOL:
811 telnet_bufsize = sk_write(s, "\r\n", 2);
812 break;
813 case TS_SYNCH:
814 b[1] = DM;
815 telnet_bufsize = sk_write(s, b, 1);
816 telnet_bufsize = sk_write_oob(s, b + 1, 1);
817 break;
818 case TS_RECHO:
819 if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) {
820 o_echo.state = REQUESTED;
821 send_opt(o_echo.send, o_echo.option);
822 }
823 break;
824 case TS_LECHO:
825 if (o_echo.state == ACTIVE) {
826 o_echo.state = REQUESTED;
827 send_opt(o_echo.nsend, o_echo.option);
828 }
829 break;
830 case TS_PING:
831 if (o_they_sga.state == ACTIVE) {
832 b[1] = NOP;
833 telnet_bufsize = sk_write(s, b, 2);
834 }
835 break;
836 }
837 }
838
839 static Socket telnet_socket(void)
840 {
841 return s;
842 }
843
844 static int telnet_sendok(void)
845 {
846 return 1;
847 }
848
849 static void telnet_unthrottle(int backlog)
850 {
851 sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG);
852 }
853
854 static int telnet_ldisc(int option)
855 {
856 if (option == LD_ECHO)
857 return echoing;
858 if (option == LD_EDIT)
859 return editing;
860 return FALSE;
861 }
862
863 static int telnet_exitcode(void)
864 {
865 /* Telnet doesn't transmit exit codes back to the client */
866 return 0;
867 }
868
869 Backend telnet_backend = {
870 telnet_init,
871 telnet_send,
872 telnet_sendbuffer,
873 telnet_size,
874 telnet_special,
875 telnet_socket,
876 telnet_exitcode,
877 telnet_sendok,
878 telnet_ldisc,
879 telnet_unthrottle,
880 23
881 };