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 */
196 int term_width, term_height;
198 int opt_states[NUM_OPTS];
200 int echoing, editing;
209 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
210 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
215 #define TELNET_MAX_BACKLOG 4096
217 #define SB_DELTA 1024
219 static void c_write1(Telnet telnet, int c)
223 backlog = from_backend(telnet->frontend, 0, &cc, 1);
224 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
227 static void log_option(char *sender, int cmd, int option)
230 sprintf(buf, "%s:\t%s %s", sender,
231 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
232 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
237 static void send_opt(Telnet telnet, int cmd, int option)
244 telnet->bufsize = sk_write(telnet->s, b, 3);
245 log_option("client", cmd, option);
248 static void deactivate_option(Telnet telnet, const struct Opt *o)
250 if (telnet->opt_states[o->index] == REQUESTED ||
251 telnet->opt_states[o->index] == ACTIVE)
252 send_opt(telnet, o->nsend, o->option);
253 telnet->opt_states[o->index] = REALLY_INACTIVE;
257 * Generate side effects of enabling or disabling an option.
259 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
261 if (o->option == TELOPT_ECHO && o->send == DO)
262 telnet->echoing = !enabled;
263 else if (o->option == TELOPT_SGA && o->send == DO)
264 telnet->editing = !enabled;
265 if (telnet->ldisc) /* cause ldisc to notice the change */
266 ldisc_send(telnet->ldisc, NULL, 0, 0);
268 /* Ensure we get the minimum options */
269 if (!telnet->activated) {
270 if (telnet->opt_states[o_echo.index] == INACTIVE) {
271 telnet->opt_states[o_echo.index] = REQUESTED;
272 send_opt(telnet, o_echo.send, o_echo.option);
274 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
275 telnet->opt_states[o_we_sga.index] = REQUESTED;
276 send_opt(telnet, o_we_sga.send, o_we_sga.option);
278 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
279 telnet->opt_states[o_they_sga.index] = REQUESTED;
280 send_opt(telnet, o_they_sga.send, o_they_sga.option);
282 telnet->activated = TRUE;
286 static void activate_option(Telnet telnet, const struct Opt *o)
288 if (o->send == WILL && o->option == TELOPT_NAWS)
289 telnet_size(telnet, telnet->term_width, telnet->term_height);
290 if (o->send == WILL &&
291 (o->option == TELOPT_NEW_ENVIRON ||
292 o->option == TELOPT_OLD_ENVIRON)) {
294 * We may only have one kind of ENVIRON going at a time.
295 * This is a hack, but who cares.
297 deactivate_option(telnet, o->option ==
298 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
300 option_side_effects(telnet, o, 1);
303 static void refused_option(Telnet telnet, const struct Opt *o)
305 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
306 telnet->opt_states[o_oenv.index] == INACTIVE) {
307 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
308 telnet->opt_states[o_oenv.index] = REQUESTED;
310 option_side_effects(telnet, o, 0);
313 static void proc_rec_opt(Telnet telnet, int cmd, int option)
315 const struct Opt *const *o;
317 log_option("server", cmd, option);
318 for (o = opts; *o; o++) {
319 if ((*o)->option == option && (*o)->ack == cmd) {
320 switch (telnet->opt_states[(*o)->index]) {
322 telnet->opt_states[(*o)->index] = ACTIVE;
323 activate_option(telnet, *o);
328 telnet->opt_states[(*o)->index] = ACTIVE;
329 send_opt(telnet, (*o)->send, option);
330 activate_option(telnet, *o);
332 case REALLY_INACTIVE:
333 send_opt(telnet, (*o)->nsend, option);
337 } else if ((*o)->option == option && (*o)->nak == cmd) {
338 switch (telnet->opt_states[(*o)->index]) {
340 telnet->opt_states[(*o)->index] = INACTIVE;
341 refused_option(telnet, *o);
344 telnet->opt_states[(*o)->index] = INACTIVE;
345 send_opt(telnet, (*o)->nsend, option);
346 option_side_effects(telnet, *o, 0);
349 case REALLY_INACTIVE:
356 * If we reach here, the option was one we weren't prepared to
357 * cope with. So send a negative ack.
359 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
362 static void process_subneg(Telnet telnet)
364 unsigned char b[2048], *p, *q;
368 switch (telnet->sb_opt) {
370 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
371 char logbuf[sizeof(cfg.termspeed) + 80];
374 b[2] = TELOPT_TSPEED;
376 strcpy(b + 4, cfg.termspeed);
377 n = 4 + strlen(cfg.termspeed);
380 telnet->bufsize = sk_write(telnet->s, b, n + 2);
381 logevent("server:\tSB TSPEED SEND");
382 sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
385 logevent("server:\tSB TSPEED <something weird>");
388 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
389 char logbuf[sizeof(cfg.termtype) + 80];
394 for (n = 0; cfg.termtype[n]; n++)
395 b[n + 4] = (cfg.termtype[n] >= 'a'
396 && cfg.termtype[n] <=
397 'z' ? cfg.termtype[n] + 'A' -
398 'a' : cfg.termtype[n]);
401 telnet->bufsize = sk_write(telnet->s, b, n + 6);
403 logevent("server:\tSB TTYPE SEND");
404 sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
407 logevent("server:\tSB TTYPE <something weird>\r\n");
409 case TELOPT_OLD_ENVIRON:
410 case TELOPT_NEW_ENVIRON:
412 q = p + telnet->sb_len;
413 if (p < q && *p == TELQUAL_SEND) {
416 sprintf(logbuf, "server:\tSB %s SEND", telopt(telnet->sb_opt));
418 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
419 if (cfg.rfc_environ) {
427 * Try to guess the sense of VAR and VALUE.
433 } else if (*p == BSD_VAR) {
441 * With NEW_ENVIRON, the sense of VAR and VALUE
449 b[2] = telnet->sb_opt;
455 while (*e && *e != '\t')
477 telnet->bufsize = sk_write(telnet->s, b, n);
478 sprintf(logbuf, "client:\tSB %s IS %s", telopt(telnet->sb_opt),
479 n == 6 ? "<nothing>" : "<stuff>");
486 static void do_telnet_read(Telnet telnet, char *buf, int len)
490 int c = (unsigned char) *buf++;
492 switch (telnet->state) {
495 if (c == NUL && telnet->state == SEENCR)
496 telnet->state = TOP_LEVEL;
498 telnet->state = SEENIAC;
500 if (!telnet->in_synch)
504 /* I can't get the F***ing winsock to insert the urgent IAC
505 * into the right position! Even with SO_OOBINLINE it gives
506 * it to recv too soon. And of course the DM byte (that
507 * arrives in the same packet!) appears several K later!!
509 * Oh well, we do get the DM in the right place so I'll
510 * just stop hiding on the next 0xf2 and hope for the best.
513 telnet->in_synch = 0;
516 telnet->state = SEENCR;
518 telnet->state = TOP_LEVEL;
523 telnet->state = SEENDO;
525 telnet->state = SEENDONT;
527 telnet->state = SEENWILL;
529 telnet->state = SEENWONT;
531 telnet->state = SEENSB;
533 telnet->in_synch = 0;
534 telnet->state = TOP_LEVEL;
536 /* ignore everything else; print it if it's IAC */
540 telnet->state = TOP_LEVEL;
544 proc_rec_opt(telnet, WILL, c);
545 telnet->state = TOP_LEVEL;
548 proc_rec_opt(telnet, WONT, c);
549 telnet->state = TOP_LEVEL;
552 proc_rec_opt(telnet, DO, c);
553 telnet->state = TOP_LEVEL;
556 proc_rec_opt(telnet, DONT, c);
557 telnet->state = TOP_LEVEL;
562 telnet->state = SUBNEGOT;
566 telnet->state = SUBNEG_IAC;
569 if (telnet->sb_len >= telnet->sb_size) {
571 telnet->sb_size += SB_DELTA;
572 newbuf = (telnet->sb_buf ?
573 srealloc(telnet->sb_buf, telnet->sb_size) :
574 smalloc(telnet->sb_size));
576 telnet->sb_buf = newbuf;
578 telnet->sb_size -= SB_DELTA;
580 if (telnet->sb_len < telnet->sb_size)
581 telnet->sb_buf[telnet->sb_len++] = c;
582 telnet->state = SUBNEGOT; /* in case we came here by goto */
587 goto subneg_addchar; /* yes, it's a hack, I know, but... */
589 process_subneg(telnet);
590 telnet->state = TOP_LEVEL;
597 static int telnet_closing(Plug plug, char *error_msg, int error_code,
600 Telnet telnet = (Telnet) plug;
607 /* A socket error has occurred. */
609 connection_fatal("%s", error_msg);
610 } /* Otherwise, the remote side closed the connection normally. */
614 static int telnet_receive(Plug plug, int urgent, char *data, int len)
616 Telnet telnet = (Telnet) plug;
618 telnet->in_synch = TRUE;
619 do_telnet_read(telnet, data, len);
623 static void telnet_sent(Plug plug, int bufsize)
625 Telnet telnet = (Telnet) plug;
626 telnet->bufsize = bufsize;
630 * Called to set up the Telnet connection.
632 * Returns an error message, or NULL on success.
634 * Also places the canonical host name into `realhost'. It must be
635 * freed by the caller.
637 static char *telnet_init(void *frontend_handle, void **backend_handle,
638 char *host, int port, char **realhost, int nodelay)
640 static const struct plug_function_table fn_table = {
649 telnet = smalloc(sizeof(*telnet));
650 telnet->fn = &fn_table;
652 telnet->echoing = TRUE;
653 telnet->editing = TRUE;
654 telnet->activated = FALSE;
655 telnet->sb_buf = NULL;
657 telnet->frontend = frontend_handle;
658 telnet->term_width = cfg.width;
659 telnet->term_height = cfg.height;
660 telnet->state = TOP_LEVEL;
661 *backend_handle = telnet;
668 sprintf(buf, "Looking up host \"%.170s\"", host);
671 addr = sk_namelookup(host, realhost);
672 if ((err = sk_addr_error(addr)))
676 port = 23; /* default telnet port */
682 char buf[200], addrbuf[100];
683 sk_getaddr(addr, addrbuf, 100);
684 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
687 telnet->s = new_connection(addr, *realhost, port, 0, 1,
688 nodelay, (Plug) telnet);
689 if ((err = sk_socket_error(telnet->s)))
695 * Initialise option states.
697 if (cfg.passive_telnet) {
698 const struct Opt *const *o;
700 for (o = opts; *o; o++)
701 telnet->opt_states[(*o)->index] = INACTIVE;
703 const struct Opt *const *o;
705 for (o = opts; *o; o++) {
706 telnet->opt_states[(*o)->index] = (*o)->initial_state;
707 if (telnet->opt_states[(*o)->index] == REQUESTED)
708 send_opt(telnet, (*o)->send, (*o)->option);
710 telnet->activated = TRUE;
714 * Set up SYNCH state.
716 telnet->in_synch = FALSE;
722 * Called to send data down the Telnet connection.
724 static int telnet_send(void *handle, char *buf, int len)
726 Telnet telnet = (Telnet) handle;
728 static unsigned char iac[2] = { IAC, IAC };
729 static unsigned char cr[2] = { CR, NUL };
731 static unsigned char nl[2] = { CR, LF };
734 if (telnet->s == NULL)
738 while (p < buf + len) {
741 while (iswritable((unsigned char) *p) && p < buf + len)
743 telnet->bufsize = sk_write(telnet->s, q, p - q);
745 while (p < buf + len && !iswritable((unsigned char) *p)) {
747 sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2);
752 return telnet->bufsize;
756 * Called to query the current socket sendability status.
758 static int telnet_sendbuffer(void *handle)
760 Telnet telnet = (Telnet) handle;
761 return telnet->bufsize;
765 * Called to set the size of the window from Telnet's POV.
767 static void telnet_size(void *handle, int width, int height)
769 Telnet telnet = (Telnet) handle;
773 telnet->term_width = width;
774 telnet->term_height = height;
776 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
781 b[3] = telnet->term_width >> 8;
782 b[4] = telnet->term_width & 0xFF;
783 b[5] = telnet->term_height >> 8;
784 b[6] = telnet->term_height & 0xFF;
787 telnet->bufsize = sk_write(telnet->s, b, 9);
788 sprintf(logbuf, "client:\tSB NAWS %d,%d",
789 ((unsigned char) b[3] << 8) + (unsigned char) b[4],
790 ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
795 * Send Telnet special codes.
797 static void telnet_special(void *handle, Telnet_Special code)
799 Telnet telnet = (Telnet) handle;
802 if (telnet->s == NULL)
809 telnet->bufsize = sk_write(telnet->s, b, 2);
813 telnet->bufsize = sk_write(telnet->s, b, 2);
817 telnet->bufsize = sk_write(telnet->s, b, 2);
821 telnet->bufsize = sk_write(telnet->s, b, 2);
825 telnet->bufsize = sk_write(telnet->s, b, 2);
829 telnet->bufsize = sk_write(telnet->s, b, 2);
833 telnet->bufsize = sk_write(telnet->s, b, 2);
837 telnet->bufsize = sk_write(telnet->s, b, 2);
841 telnet->bufsize = sk_write(telnet->s, b, 2);
845 telnet->bufsize = sk_write(telnet->s, b, 2);
849 telnet->bufsize = sk_write(telnet->s, b, 2);
853 telnet->bufsize = sk_write(telnet->s, b, 2);
856 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
860 telnet->bufsize = sk_write(telnet->s, b, 1);
861 telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1);
864 if (telnet->opt_states[o_echo.index] == INACTIVE ||
865 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
866 telnet->opt_states[o_echo.index] = REQUESTED;
867 send_opt(telnet, o_echo.send, o_echo.option);
871 if (telnet->opt_states[o_echo.index] == ACTIVE) {
872 telnet->opt_states[o_echo.index] = REQUESTED;
873 send_opt(telnet, o_echo.nsend, o_echo.option);
877 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
879 telnet->bufsize = sk_write(telnet->s, b, 2);
885 static Socket telnet_socket(void *handle)
887 Telnet telnet = (Telnet) handle;
891 static int telnet_sendok(void *handle)
893 Telnet telnet = (Telnet) handle;
897 static void telnet_unthrottle(void *handle, int backlog)
899 Telnet telnet = (Telnet) handle;
900 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
903 static int telnet_ldisc(void *handle, int option)
905 Telnet telnet = (Telnet) handle;
906 if (option == LD_ECHO)
907 return telnet->echoing;
908 if (option == LD_EDIT)
909 return telnet->editing;
913 static void telnet_provide_ldisc(void *handle, void *ldisc)
915 Telnet telnet = (Telnet) handle;
916 telnet->ldisc = ldisc;
919 static int telnet_exitcode(void *handle)
921 Telnet telnet = (Telnet) handle;
922 /* Telnet doesn't transmit exit codes back to the client */
926 Backend telnet_backend = {
936 telnet_provide_ldisc,