Oops. Forgot to check in terminal.h from yesterday's work. There's
[u/mdw/putty] / telnet.c
CommitLineData
374330e2 1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
374330e2 4
5#include "putty.h"
887035a5 6#include "terminal.h"
374330e2 7
8#ifndef FALSE
9#define FALSE 0
10#endif
11#ifndef TRUE
12#define TRUE 1
13#endif
14
8df7a775 15static Socket s = NULL;
374330e2 16
887035a5 17static void *frontend;
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 97static 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
144static void telnet_size(void);
145
146struct 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 156static struct Opt o_naws =
157 { WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED };
158static struct Opt o_tspeed =
159 { WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED };
160static struct Opt o_ttype =
161 { WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED };
162static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
163 INACTIVE
164};
165static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
166 REQUESTED
167};
168static struct Opt o_echo =
169 { DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED };
170static struct Opt o_we_sga =
171 { WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED };
172static struct Opt o_they_sga =
173 { DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED };
374330e2 174
175static 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 182static int echoing = TRUE, editing = TRUE;
8faa456c 183static int activated = FALSE;
5471d09a 184static int telnet_bufsize;
374330e2 185static int in_synch;
374330e2 186static int sb_opt, sb_len;
187static char *sb_buf = NULL;
188static int sb_size = 0;
189#define SB_DELTA 1024
190
32874aea 191static 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 199static 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 209static 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 220static 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 230static 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 256static void activate_option(struct Opt *o)
257{
374330e2 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 */
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 273static 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 283static 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 332static 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
456static 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 461static 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 572static 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 587static 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 595static 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 608static 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;
621
374330e2 622 /*
623 * Try to find host.
624 */
3ad9d396 625 {
626 char buf[200];
627 sprintf(buf, "Looking up host \"%.170s\"", host);
628 logevent(buf);
629 }
8df7a775 630 addr = sk_namelookup(host, realhost);
32874aea 631 if ((err = sk_addr_error(addr)))
8df7a775 632 return err;
374330e2 633
634 if (port < 0)
635 port = 23; /* default telnet port */
636
637 /*
638 * Open socket.
639 */
3ad9d396 640 {
641 char buf[200], addrbuf[100];
642 sk_getaddr(addr, addrbuf, 100);
643 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
644 logevent(buf);
645 }
8eebd221 646 s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr);
32874aea 647 if ((err = sk_socket_error(s)))
8df7a775 648 return err;
374330e2 649
8df7a775 650 sk_addr_free(addr);
374330e2 651
652 /*
653 * Initialise option states.
654 */
8faa456c 655 if (cfg.passive_telnet) {
656 struct Opt **o;
657
658 for (o = opts; *o; o++)
659 if ((*o)->state == REQUESTED)
660 (*o)->state = INACTIVE;
661 } else {
374330e2 662 struct Opt **o;
663
664 for (o = opts; *o; o++)
665 if ((*o)->state == REQUESTED)
32874aea 666 send_opt((*o)->send, (*o)->option);
8faa456c 667 activated = TRUE;
374330e2 668 }
669
374330e2 670 /*
671 * Set up SYNCH state.
672 */
673 in_synch = FALSE;
6f34e365 674
374330e2 675 return NULL;
676}
677
678/*
374330e2 679 * Called to send data down the Telnet connection.
680 */
5471d09a 681static int telnet_send(char *buf, int len)
32874aea 682{
374330e2 683 char *p;
684 static unsigned char iac[2] = { IAC, IAC };
685 static unsigned char cr[2] = { CR, NUL };
a5f3e637 686#if 0
eb5e1db9 687 static unsigned char nl[2] = { CR, LF };
a5f3e637 688#endif
374330e2 689
8df7a775 690 if (s == NULL)
5471d09a 691 return 0;
374330e2 692
693 p = buf;
32874aea 694 while (p < buf + len) {
374330e2 695 char *q = p;
696
32874aea 697 while (iswritable((unsigned char) *p) && p < buf + len)
698 p++;
5471d09a 699 telnet_bufsize = sk_write(s, q, p - q);
374330e2 700
32874aea 701 while (p < buf + len && !iswritable((unsigned char) *p)) {
5471d09a 702 telnet_bufsize =
703 sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2);
374330e2 704 p++;
705 }
706 }
5471d09a 707
708 return telnet_bufsize;
709}
710
711/*
712 * Called to query the current socket sendability status.
713 */
714static int telnet_sendbuffer(void)
715{
716 return telnet_bufsize;
374330e2 717}
718
719/*
720 * Called to set the size of the window from Telnet's POV.
721 */
32874aea 722static void telnet_size(void)
723{
374330e2 724 unsigned char b[16];
725 char logbuf[50];
726
887035a5 727 if (s == NULL || term == NULL || o_naws.state != ACTIVE)
374330e2 728 return;
32874aea 729 b[0] = IAC;
730 b[1] = SB;
731 b[2] = TELOPT_NAWS;
887035a5 732 b[3] = term->cols >> 8;
733 b[4] = term->cols & 0xFF;
734 b[5] = term->rows >> 8;
735 b[6] = term->rows & 0xFF;
32874aea 736 b[7] = IAC;
737 b[8] = SE;
5471d09a 738 telnet_bufsize = sk_write(s, b, 9);
374330e2 739 sprintf(logbuf, "client:\tSB NAWS %d,%d",
32874aea 740 ((unsigned char) b[3] << 8) + (unsigned char) b[4],
741 ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
742 logevent(logbuf);
374330e2 743}
744
745/*
746 * Send Telnet special codes.
747 */
32874aea 748static void telnet_special(Telnet_Special code)
749{
374330e2 750 unsigned char b[2];
751
8df7a775 752 if (s == NULL)
374330e2 753 return;
754
755 b[0] = IAC;
756 switch (code) {
32874aea 757 case TS_AYT:
758 b[1] = AYT;
5471d09a 759 telnet_bufsize = sk_write(s, b, 2);
32874aea 760 break;
761 case TS_BRK:
762 b[1] = BREAK;
5471d09a 763 telnet_bufsize = sk_write(s, b, 2);
32874aea 764 break;
765 case TS_EC:
766 b[1] = EC;
5471d09a 767 telnet_bufsize = sk_write(s, b, 2);
32874aea 768 break;
769 case TS_EL:
770 b[1] = EL;
5471d09a 771 telnet_bufsize = sk_write(s, b, 2);
32874aea 772 break;
773 case TS_GA:
774 b[1] = GA;
5471d09a 775 telnet_bufsize = sk_write(s, b, 2);
32874aea 776 break;
777 case TS_NOP:
778 b[1] = NOP;
5471d09a 779 telnet_bufsize = sk_write(s, b, 2);
32874aea 780 break;
781 case TS_ABORT:
782 b[1] = ABORT;
5471d09a 783 telnet_bufsize = sk_write(s, b, 2);
32874aea 784 break;
785 case TS_AO:
786 b[1] = AO;
5471d09a 787 telnet_bufsize = sk_write(s, b, 2);
32874aea 788 break;
789 case TS_IP:
790 b[1] = IP;
5471d09a 791 telnet_bufsize = sk_write(s, b, 2);
32874aea 792 break;
793 case TS_SUSP:
794 b[1] = SUSP;
5471d09a 795 telnet_bufsize = sk_write(s, b, 2);
32874aea 796 break;
797 case TS_EOR:
798 b[1] = EOR;
5471d09a 799 telnet_bufsize = sk_write(s, b, 2);
32874aea 800 break;
801 case TS_EOF:
802 b[1] = xEOF;
5471d09a 803 telnet_bufsize = sk_write(s, b, 2);
32874aea 804 break;
a5f3e637 805 case TS_EOL:
5471d09a 806 telnet_bufsize = sk_write(s, "\r\n", 2);
a5f3e637 807 break;
374330e2 808 case TS_SYNCH:
32874aea 809 b[1] = DM;
5471d09a 810 telnet_bufsize = sk_write(s, b, 1);
811 telnet_bufsize = sk_write_oob(s, b + 1, 1);
32874aea 812 break;
684d367c 813 case TS_RECHO:
814 if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) {
815 o_echo.state = REQUESTED;
32874aea 816 send_opt(o_echo.send, o_echo.option);
684d367c 817 }
818 break;
819 case TS_LECHO:
820 if (o_echo.state == ACTIVE) {
821 o_echo.state = REQUESTED;
32874aea 822 send_opt(o_echo.nsend, o_echo.option);
684d367c 823 }
374330e2 824 break;
32874aea 825 case TS_PING:
ec55b220 826 if (o_they_sga.state == ACTIVE) {
32874aea 827 b[1] = NOP;
5471d09a 828 telnet_bufsize = sk_write(s, b, 2);
ec55b220 829 }
32874aea 830 break;
374330e2 831 }
832}
833
32874aea 834static Socket telnet_socket(void)
835{
836 return s;
837}
8ccc75b0 838
32874aea 839static int telnet_sendok(void)
840{
841 return 1;
842}
4017be6d 843
5471d09a 844static void telnet_unthrottle(int backlog)
845{
846 sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG);
847}
848
32874aea 849static int telnet_ldisc(int option)
850{
851 if (option == LD_ECHO)
852 return echoing;
853 if (option == LD_EDIT)
854 return editing;
0965bee0 855 return FALSE;
856}
857
d8d6c7e5 858static int telnet_exitcode(void)
859{
860 /* Telnet doesn't transmit exit codes back to the client */
861 return 0;
862}
863
374330e2 864Backend telnet_backend = {
865 telnet_init,
374330e2 866 telnet_send,
5471d09a 867 telnet_sendbuffer,
374330e2 868 telnet_size,
4017be6d 869 telnet_special,
8ccc75b0 870 telnet_socket,
d8d6c7e5 871 telnet_exitcode,
97db3be4 872 telnet_sendok,
0965bee0 873 telnet_ldisc,
5471d09a 874 telnet_unthrottle,
97db3be4 875 23
374330e2 876};