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