14 #define IAC 255 /* interpret as command: */
15 #define DONT 254 /* you are not to use option */
16 #define DO 253 /* please, you use option */
17 #define WONT 252 /* I won't use option */
18 #define WILL 251 /* I will use option */
19 #define SB 250 /* interpret as subnegotiation */
20 #define SE 240 /* end sub negotiation */
22 #define GA 249 /* you may reverse the line */
23 #define EL 248 /* erase the current line */
24 #define EC 247 /* erase the current character */
25 #define AYT 246 /* are you there */
26 #define AO 245 /* abort output--but let prog finish */
27 #define IP 244 /* interrupt process--permanently */
28 #define BREAK 243 /* break */
29 #define DM 242 /* data mark--for connect. cleaning */
30 #define NOP 241 /* nop */
31 #define EOR 239 /* end of record (transparent mode) */
32 #define ABORT 238 /* Abort process */
33 #define SUSP 237 /* Suspend process */
34 #define xEOF 236 /* End of file: EOF is already used... */
36 #define TELOPT_BINARY 0 /* 8-bit data path */
37 #define TELOPT_ECHO 1 /* echo */
38 #define TELOPT_RCP 2 /* prepare to reconnect */
39 #define TELOPT_SGA 3 /* suppress go ahead */
40 #define TELOPT_NAMS 4 /* approximate message size */
41 #define TELOPT_STATUS 5 /* give status */
42 #define TELOPT_TM 6 /* timing mark */
43 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
44 #define TELOPT_NAOL 8 /* negotiate about output line width */
45 #define TELOPT_NAOP 9 /* negotiate about output page size */
46 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
47 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
48 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
49 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
50 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
51 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
52 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
53 #define TELOPT_XASCII 17 /* extended ascic character set */
54 #define TELOPT_LOGOUT 18 /* force logout */
55 #define TELOPT_BM 19 /* byte macro */
56 #define TELOPT_DET 20 /* data entry terminal */
57 #define TELOPT_SUPDUP 21 /* supdup protocol */
58 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
59 #define TELOPT_SNDLOC 23 /* send location */
60 #define TELOPT_TTYPE 24 /* terminal type */
61 #define TELOPT_EOR 25 /* end or record */
62 #define TELOPT_TUID 26 /* TACACS user identification */
63 #define TELOPT_OUTMRK 27 /* output marking */
64 #define TELOPT_TTYLOC 28 /* terminal location number */
65 #define TELOPT_3270REGIME 29 /* 3270 regime */
66 #define TELOPT_X3PAD 30 /* X.3 PAD */
67 #define TELOPT_NAWS 31 /* window size */
68 #define TELOPT_TSPEED 32 /* terminal speed */
69 #define TELOPT_LFLOW 33 /* remote flow control */
70 #define TELOPT_LINEMODE 34 /* Linemode option */
71 #define TELOPT_XDISPLOC 35 /* X Display Location */
72 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
73 #define TELOPT_AUTHENTICATION 37 /* Authenticate */
74 #define TELOPT_ENCRYPT 38 /* Encryption option */
75 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
76 #define TELOPT_EXOPL 255 /* extended-options-list */
78 #define TELQUAL_IS 0 /* option is... */
79 #define TELQUAL_SEND 1 /* send option */
80 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
90 #define iswritable(x) ( (x) != IAC && (x) != CR )
92 static char *telopt(int opt)
94 #define i(x) if (opt == TELOPT_ ## x) return #x;
139 static void telnet_size(void *handle, int width, int height);
142 int send; /* what we initially send */
143 int nsend; /* -ve send if requested to stop it */
144 int ack, nak; /* +ve and -ve acknowledgements */
145 int option; /* the option code */
146 int index; /* index into telnet->opt_states[] */
148 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
164 static const struct Opt o_naws =
165 { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
166 static const struct Opt o_tspeed =
167 { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
168 static const struct Opt o_ttype =
169 { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
170 static const struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
171 OPTINDEX_OENV, INACTIVE
173 static const struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
174 OPTINDEX_NENV, REQUESTED
176 static const struct Opt o_echo =
177 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
178 static const struct Opt o_we_sga =
179 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
180 static const struct Opt o_they_sga =
181 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
183 static const struct Opt *const opts[] = {
184 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
185 &o_we_sga, &o_they_sga, NULL
188 typedef struct telnet_tag {
189 const struct plug_function_table *fn;
190 /* the above field _must_ be first in the structure */
195 int term_width, term_height;
197 int opt_states[NUM_OPTS];
199 int echoing, editing;
208 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
209 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
214 #define TELNET_MAX_BACKLOG 4096
216 #define SB_DELTA 1024
218 static void c_write1(Telnet telnet, int c)
222 backlog = from_backend(telnet->frontend, 0, &cc, 1);
223 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
226 static void log_option(char *sender, int cmd, int option)
229 sprintf(buf, "%s:\t%s %s", sender,
230 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
231 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
236 static void send_opt(Telnet telnet, int cmd, int option)
243 telnet->bufsize = sk_write(telnet->s, b, 3);
244 log_option("client", cmd, option);
247 static void deactivate_option(Telnet telnet, const struct Opt *o)
249 if (telnet->opt_states[o->index] == REQUESTED ||
250 telnet->opt_states[o->index] == ACTIVE)
251 send_opt(telnet, o->nsend, o->option);
252 telnet->opt_states[o->index] = REALLY_INACTIVE;
256 * Generate side effects of enabling or disabling an option.
258 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
260 if (o->option == TELOPT_ECHO && o->send == DO)
261 telnet->echoing = !enabled;
262 else if (o->option == TELOPT_SGA && o->send == DO)
263 telnet->editing = !enabled;
264 ldisc_send(NULL, 0, 0); /* cause ldisc to notice the change */
266 /* Ensure we get the minimum options */
267 if (!telnet->activated) {
268 if (telnet->opt_states[o_echo.index] == INACTIVE) {
269 telnet->opt_states[o_echo.index] = REQUESTED;
270 send_opt(telnet, o_echo.send, o_echo.option);
272 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
273 telnet->opt_states[o_we_sga.index] = REQUESTED;
274 send_opt(telnet, o_we_sga.send, o_we_sga.option);
276 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
277 telnet->opt_states[o_they_sga.index] = REQUESTED;
278 send_opt(telnet, o_they_sga.send, o_they_sga.option);
280 telnet->activated = TRUE;
284 static void activate_option(Telnet telnet, const struct Opt *o)
286 if (o->send == WILL && o->option == TELOPT_NAWS)
287 telnet_size(telnet, telnet->term_width, telnet->term_height);
288 if (o->send == WILL &&
289 (o->option == TELOPT_NEW_ENVIRON ||
290 o->option == TELOPT_OLD_ENVIRON)) {
292 * We may only have one kind of ENVIRON going at a time.
293 * This is a hack, but who cares.
295 deactivate_option(telnet, o->option ==
296 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
298 option_side_effects(telnet, o, 1);
301 static void refused_option(Telnet telnet, const struct Opt *o)
303 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
304 telnet->opt_states[o_oenv.index] == INACTIVE) {
305 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
306 telnet->opt_states[o_oenv.index] = REQUESTED;
308 option_side_effects(telnet, o, 0);
311 static void proc_rec_opt(Telnet telnet, int cmd, int option)
313 const struct Opt *const *o;
315 log_option("server", cmd, option);
316 for (o = opts; *o; o++) {
317 if ((*o)->option == option && (*o)->ack == cmd) {
318 switch (telnet->opt_states[(*o)->index]) {
320 telnet->opt_states[(*o)->index] = ACTIVE;
321 activate_option(telnet, *o);
326 telnet->opt_states[(*o)->index] = ACTIVE;
327 send_opt(telnet, (*o)->send, option);
328 activate_option(telnet, *o);
330 case REALLY_INACTIVE:
331 send_opt(telnet, (*o)->nsend, option);
335 } else if ((*o)->option == option && (*o)->nak == cmd) {
336 switch (telnet->opt_states[(*o)->index]) {
338 telnet->opt_states[(*o)->index] = INACTIVE;
339 refused_option(telnet, *o);
342 telnet->opt_states[(*o)->index] = INACTIVE;
343 send_opt(telnet, (*o)->nsend, option);
344 option_side_effects(telnet, *o, 0);
347 case REALLY_INACTIVE:
354 * If we reach here, the option was one we weren't prepared to
355 * cope with. So send a negative ack.
357 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
360 static void process_subneg(Telnet telnet)
362 unsigned char b[2048], *p, *q;
366 switch (telnet->sb_opt) {
368 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
369 char logbuf[sizeof(cfg.termspeed) + 80];
372 b[2] = TELOPT_TSPEED;
374 strcpy(b + 4, cfg.termspeed);
375 n = 4 + strlen(cfg.termspeed);
378 telnet->bufsize = sk_write(telnet->s, b, n + 2);
379 logevent("server:\tSB TSPEED SEND");
380 sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
383 logevent("server:\tSB TSPEED <something weird>");
386 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
387 char logbuf[sizeof(cfg.termtype) + 80];
392 for (n = 0; cfg.termtype[n]; n++)
393 b[n + 4] = (cfg.termtype[n] >= 'a'
394 && cfg.termtype[n] <=
395 'z' ? cfg.termtype[n] + 'A' -
396 'a' : cfg.termtype[n]);
399 telnet->bufsize = sk_write(telnet->s, b, n + 6);
401 logevent("server:\tSB TTYPE SEND");
402 sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
405 logevent("server:\tSB TTYPE <something weird>\r\n");
407 case TELOPT_OLD_ENVIRON:
408 case TELOPT_NEW_ENVIRON:
410 q = p + telnet->sb_len;
411 if (p < q && *p == TELQUAL_SEND) {
414 sprintf(logbuf, "server:\tSB %s SEND", telopt(telnet->sb_opt));
416 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
417 if (cfg.rfc_environ) {
425 * Try to guess the sense of VAR and VALUE.
431 } else if (*p == BSD_VAR) {
439 * With NEW_ENVIRON, the sense of VAR and VALUE
447 b[2] = telnet->sb_opt;
453 while (*e && *e != '\t')
475 telnet->bufsize = sk_write(telnet->s, b, n);
476 sprintf(logbuf, "client:\tSB %s IS %s", telopt(telnet->sb_opt),
477 n == 6 ? "<nothing>" : "<stuff>");
484 static void do_telnet_read(Telnet telnet, char *buf, int len)
488 int c = (unsigned char) *buf++;
490 switch (telnet->state) {
493 if (c == NUL && telnet->state == SEENCR)
494 telnet->state = TOP_LEVEL;
496 telnet->state = SEENIAC;
498 if (!telnet->in_synch)
502 /* I can't get the F***ing winsock to insert the urgent IAC
503 * into the right position! Even with SO_OOBINLINE it gives
504 * it to recv too soon. And of course the DM byte (that
505 * arrives in the same packet!) appears several K later!!
507 * Oh well, we do get the DM in the right place so I'll
508 * just stop hiding on the next 0xf2 and hope for the best.
511 telnet->in_synch = 0;
514 telnet->state = SEENCR;
516 telnet->state = TOP_LEVEL;
521 telnet->state = SEENDO;
523 telnet->state = SEENDONT;
525 telnet->state = SEENWILL;
527 telnet->state = SEENWONT;
529 telnet->state = SEENSB;
531 telnet->in_synch = 0;
532 telnet->state = TOP_LEVEL;
534 /* ignore everything else; print it if it's IAC */
538 telnet->state = TOP_LEVEL;
542 proc_rec_opt(telnet, WILL, c);
543 telnet->state = TOP_LEVEL;
546 proc_rec_opt(telnet, WONT, c);
547 telnet->state = TOP_LEVEL;
550 proc_rec_opt(telnet, DO, c);
551 telnet->state = TOP_LEVEL;
554 proc_rec_opt(telnet, DONT, c);
555 telnet->state = TOP_LEVEL;
560 telnet->state = SUBNEGOT;
564 telnet->state = SUBNEG_IAC;
567 if (telnet->sb_len >= telnet->sb_size) {
569 telnet->sb_size += SB_DELTA;
570 newbuf = (telnet->sb_buf ?
571 srealloc(telnet->sb_buf, telnet->sb_size) :
572 smalloc(telnet->sb_size));
574 telnet->sb_buf = newbuf;
576 telnet->sb_size -= SB_DELTA;
578 if (telnet->sb_len < telnet->sb_size)
579 telnet->sb_buf[telnet->sb_len++] = c;
580 telnet->state = SUBNEGOT; /* in case we came here by goto */
585 goto subneg_addchar; /* yes, it's a hack, I know, but... */
587 process_subneg(telnet);
588 telnet->state = TOP_LEVEL;
595 static int telnet_closing(Plug plug, char *error_msg, int error_code,
598 Telnet telnet = (Telnet) plug;
605 /* A socket error has occurred. */
607 connection_fatal("%s", error_msg);
608 } /* Otherwise, the remote side closed the connection normally. */
612 static int telnet_receive(Plug plug, int urgent, char *data, int len)
614 Telnet telnet = (Telnet) plug;
616 telnet->in_synch = TRUE;
617 do_telnet_read(telnet, data, len);
621 static void telnet_sent(Plug plug, int bufsize)
623 Telnet telnet = (Telnet) plug;
624 telnet->bufsize = bufsize;
628 * Called to set up the Telnet connection.
630 * Returns an error message, or NULL on success.
632 * Also places the canonical host name into `realhost'. It must be
633 * freed by the caller.
635 static char *telnet_init(void *frontend_handle, void **backend_handle,
636 char *host, int port, char **realhost, int nodelay)
638 static const struct plug_function_table fn_table = {
647 telnet = smalloc(sizeof(*telnet));
648 telnet->fn = &fn_table;
650 telnet->echoing = TRUE;
651 telnet->editing = TRUE;
652 telnet->activated = FALSE;
653 telnet->sb_buf = NULL;
655 telnet->frontend = frontend_handle;
656 telnet->term_width = cfg.width;
657 telnet->term_height = cfg.height;
658 telnet->state = TOP_LEVEL;
659 *backend_handle = telnet;
666 sprintf(buf, "Looking up host \"%.170s\"", host);
669 addr = sk_namelookup(host, realhost);
670 if ((err = sk_addr_error(addr)))
674 port = 23; /* default telnet port */
680 char buf[200], addrbuf[100];
681 sk_getaddr(addr, addrbuf, 100);
682 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
685 telnet->s = new_connection(addr, *realhost, port, 0, 1,
686 nodelay, (Plug) telnet);
687 if ((err = sk_socket_error(telnet->s)))
693 * Initialise option states.
695 if (cfg.passive_telnet) {
696 const struct Opt *const *o;
698 for (o = opts; *o; o++)
699 telnet->opt_states[(*o)->index] = INACTIVE;
701 const struct Opt *const *o;
703 for (o = opts; *o; o++) {
704 telnet->opt_states[(*o)->index] = (*o)->initial_state;
705 if (telnet->opt_states[(*o)->index] == REQUESTED)
706 send_opt(telnet, (*o)->send, (*o)->option);
708 telnet->activated = TRUE;
712 * Set up SYNCH state.
714 telnet->in_synch = FALSE;
720 * Called to send data down the Telnet connection.
722 static int telnet_send(void *handle, char *buf, int len)
724 Telnet telnet = (Telnet) handle;
726 static unsigned char iac[2] = { IAC, IAC };
727 static unsigned char cr[2] = { CR, NUL };
729 static unsigned char nl[2] = { CR, LF };
732 if (telnet->s == NULL)
736 while (p < buf + len) {
739 while (iswritable((unsigned char) *p) && p < buf + len)
741 telnet->bufsize = sk_write(telnet->s, q, p - q);
743 while (p < buf + len && !iswritable((unsigned char) *p)) {
745 sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2);
750 return telnet->bufsize;
754 * Called to query the current socket sendability status.
756 static int telnet_sendbuffer(void *handle)
758 Telnet telnet = (Telnet) handle;
759 return telnet->bufsize;
763 * Called to set the size of the window from Telnet's POV.
765 static void telnet_size(void *handle, int width, int height)
767 Telnet telnet = (Telnet) handle;
771 telnet->term_width = width;
772 telnet->term_height = height;
774 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
779 b[3] = telnet->term_width >> 8;
780 b[4] = telnet->term_width & 0xFF;
781 b[5] = telnet->term_height >> 8;
782 b[6] = telnet->term_height & 0xFF;
785 telnet->bufsize = sk_write(telnet->s, b, 9);
786 sprintf(logbuf, "client:\tSB NAWS %d,%d",
787 ((unsigned char) b[3] << 8) + (unsigned char) b[4],
788 ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
793 * Send Telnet special codes.
795 static void telnet_special(void *handle, Telnet_Special code)
797 Telnet telnet = (Telnet) handle;
800 if (telnet->s == NULL)
807 telnet->bufsize = sk_write(telnet->s, b, 2);
811 telnet->bufsize = sk_write(telnet->s, b, 2);
815 telnet->bufsize = sk_write(telnet->s, b, 2);
819 telnet->bufsize = sk_write(telnet->s, b, 2);
823 telnet->bufsize = sk_write(telnet->s, b, 2);
827 telnet->bufsize = sk_write(telnet->s, b, 2);
831 telnet->bufsize = sk_write(telnet->s, b, 2);
835 telnet->bufsize = sk_write(telnet->s, b, 2);
839 telnet->bufsize = sk_write(telnet->s, b, 2);
843 telnet->bufsize = sk_write(telnet->s, b, 2);
847 telnet->bufsize = sk_write(telnet->s, b, 2);
851 telnet->bufsize = sk_write(telnet->s, b, 2);
854 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
858 telnet->bufsize = sk_write(telnet->s, b, 1);
859 telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1);
862 if (telnet->opt_states[o_echo.index] == INACTIVE ||
863 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
864 telnet->opt_states[o_echo.index] = REQUESTED;
865 send_opt(telnet, o_echo.send, o_echo.option);
869 if (telnet->opt_states[o_echo.index] == ACTIVE) {
870 telnet->opt_states[o_echo.index] = REQUESTED;
871 send_opt(telnet, o_echo.nsend, o_echo.option);
875 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
877 telnet->bufsize = sk_write(telnet->s, b, 2);
883 static Socket telnet_socket(void *handle)
885 Telnet telnet = (Telnet) handle;
889 static int telnet_sendok(void *handle)
891 Telnet telnet = (Telnet) handle;
895 static void telnet_unthrottle(void *handle, int backlog)
897 Telnet telnet = (Telnet) handle;
898 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
901 static int telnet_ldisc(void *handle, int option)
903 Telnet telnet = (Telnet) handle;
904 if (option == LD_ECHO)
905 return telnet->echoing;
906 if (option == LD_EDIT)
907 return telnet->editing;
911 static int telnet_exitcode(void *handle)
913 Telnet telnet = (Telnet) handle;
914 /* Telnet doesn't transmit exit codes back to the client */
918 Backend telnet_backend = {