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 */
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... */
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 */
77 #define TELQUAL_IS 0 /* option is... */
78 #define TELQUAL_SEND 1 /* send option */
79 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
89 #define iswritable(x) ( (x) != IAC && (x) != CR )
91 static char *telopt(int opt)
93 #define i(x) if (opt == TELOPT_ ## x) return #x;
138 static void telnet_size(void *handle, int width, int height);
141 int send; /* what we initially send */
142 int nsend; /* -ve send if requested to stop it */
143 int ack, nak; /* +ve and -ve acknowledgements */
144 int option; /* the option code */
145 int index; /* index into telnet->opt_states[] */
147 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
163 static const struct Opt o_naws =
164 { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
165 static const struct Opt o_tspeed =
166 { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
167 static const struct Opt o_ttype =
168 { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
169 static const struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
170 OPTINDEX_OENV, INACTIVE
172 static const struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
173 OPTINDEX_NENV, REQUESTED
175 static const struct Opt o_echo =
176 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
177 static const struct Opt o_we_sga =
178 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
179 static const struct Opt o_they_sga =
180 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
182 static const struct Opt *const opts[] = {
183 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
184 &o_we_sga, &o_they_sga, NULL
187 typedef struct telnet_tag {
188 const struct plug_function_table *fn;
189 /* 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(Telnet telnet, 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" : "<??>"),
233 logevent(telnet->frontend, buf);
236 static void send_opt(Telnet telnet, int cmd, int option)
243 telnet->bufsize = sk_write(telnet->s, b, 3);
244 log_option(telnet, "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 if (telnet->ldisc) /* cause ldisc to notice the change */
265 ldisc_send(telnet->ldisc, NULL, 0, 0);
267 /* Ensure we get the minimum options */
268 if (!telnet->activated) {
269 if (telnet->opt_states[o_echo.index] == INACTIVE) {
270 telnet->opt_states[o_echo.index] = REQUESTED;
271 send_opt(telnet, o_echo.send, o_echo.option);
273 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
274 telnet->opt_states[o_we_sga.index] = REQUESTED;
275 send_opt(telnet, o_we_sga.send, o_we_sga.option);
277 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
278 telnet->opt_states[o_they_sga.index] = REQUESTED;
279 send_opt(telnet, o_they_sga.send, o_they_sga.option);
281 telnet->activated = TRUE;
285 static void activate_option(Telnet telnet, const struct Opt *o)
287 if (o->send == WILL && o->option == TELOPT_NAWS)
288 telnet_size(telnet, telnet->term_width, telnet->term_height);
289 if (o->send == WILL &&
290 (o->option == TELOPT_NEW_ENVIRON ||
291 o->option == TELOPT_OLD_ENVIRON)) {
293 * We may only have one kind of ENVIRON going at a time.
294 * This is a hack, but who cares.
296 deactivate_option(telnet, o->option ==
297 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
299 option_side_effects(telnet, o, 1);
302 static void refused_option(Telnet telnet, const struct Opt *o)
304 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
305 telnet->opt_states[o_oenv.index] == INACTIVE) {
306 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
307 telnet->opt_states[o_oenv.index] = REQUESTED;
309 option_side_effects(telnet, o, 0);
312 static void proc_rec_opt(Telnet telnet, int cmd, int option)
314 const struct Opt *const *o;
316 log_option(telnet, "server", cmd, option);
317 for (o = opts; *o; o++) {
318 if ((*o)->option == option && (*o)->ack == cmd) {
319 switch (telnet->opt_states[(*o)->index]) {
321 telnet->opt_states[(*o)->index] = ACTIVE;
322 activate_option(telnet, *o);
327 telnet->opt_states[(*o)->index] = ACTIVE;
328 send_opt(telnet, (*o)->send, option);
329 activate_option(telnet, *o);
331 case REALLY_INACTIVE:
332 send_opt(telnet, (*o)->nsend, option);
336 } else if ((*o)->option == option && (*o)->nak == cmd) {
337 switch (telnet->opt_states[(*o)->index]) {
339 telnet->opt_states[(*o)->index] = INACTIVE;
340 refused_option(telnet, *o);
343 telnet->opt_states[(*o)->index] = INACTIVE;
344 send_opt(telnet, (*o)->nsend, option);
345 option_side_effects(telnet, *o, 0);
348 case REALLY_INACTIVE:
355 * If we reach here, the option was one we weren't prepared to
356 * cope with. So send a negative ack.
358 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
361 static void process_subneg(Telnet telnet)
363 unsigned char b[2048], *p, *q;
367 switch (telnet->sb_opt) {
369 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
370 char logbuf[sizeof(cfg.termspeed) + 80];
373 b[2] = TELOPT_TSPEED;
375 strcpy(b + 4, cfg.termspeed);
376 n = 4 + strlen(cfg.termspeed);
379 telnet->bufsize = sk_write(telnet->s, b, n + 2);
380 logevent(telnet->frontend, "server:\tSB TSPEED SEND");
381 sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
382 logevent(telnet->frontend, logbuf);
384 logevent(telnet->frontend, "server:\tSB TSPEED <something weird>");
387 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
388 char logbuf[sizeof(cfg.termtype) + 80];
393 for (n = 0; cfg.termtype[n]; n++)
394 b[n + 4] = (cfg.termtype[n] >= 'a'
395 && cfg.termtype[n] <=
396 'z' ? cfg.termtype[n] + 'A' -
397 'a' : cfg.termtype[n]);
400 telnet->bufsize = sk_write(telnet->s, b, n + 6);
402 logevent(telnet->frontend, "server:\tSB TTYPE SEND");
403 sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
404 logevent(telnet->frontend, logbuf);
406 logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n");
408 case TELOPT_OLD_ENVIRON:
409 case TELOPT_NEW_ENVIRON:
411 q = p + telnet->sb_len;
412 if (p < q && *p == TELQUAL_SEND) {
415 sprintf(logbuf, "server:\tSB %s SEND", telopt(telnet->sb_opt));
416 logevent(telnet->frontend, logbuf);
417 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
418 if (cfg.rfc_environ) {
426 * Try to guess the sense of VAR and VALUE.
432 } else if (*p == BSD_VAR) {
440 * With NEW_ENVIRON, the sense of VAR and VALUE
448 b[2] = telnet->sb_opt;
454 while (*e && *e != '\t')
476 telnet->bufsize = sk_write(telnet->s, b, n);
477 sprintf(logbuf, "client:\tSB %s IS %s", telopt(telnet->sb_opt),
478 n == 6 ? "<nothing>" : "<stuff>");
479 logevent(telnet->frontend, logbuf);
485 static void do_telnet_read(Telnet telnet, char *buf, int len)
489 int c = (unsigned char) *buf++;
491 switch (telnet->state) {
494 if (c == NUL && telnet->state == SEENCR)
495 telnet->state = TOP_LEVEL;
497 telnet->state = SEENIAC;
499 if (!telnet->in_synch)
503 /* I can't get the F***ing winsock to insert the urgent IAC
504 * into the right position! Even with SO_OOBINLINE it gives
505 * it to recv too soon. And of course the DM byte (that
506 * arrives in the same packet!) appears several K later!!
508 * Oh well, we do get the DM in the right place so I'll
509 * just stop hiding on the next 0xf2 and hope for the best.
512 telnet->in_synch = 0;
515 telnet->state = SEENCR;
517 telnet->state = TOP_LEVEL;
522 telnet->state = SEENDO;
524 telnet->state = SEENDONT;
526 telnet->state = SEENWILL;
528 telnet->state = SEENWONT;
530 telnet->state = SEENSB;
532 telnet->in_synch = 0;
533 telnet->state = TOP_LEVEL;
535 /* ignore everything else; print it if it's IAC */
539 telnet->state = TOP_LEVEL;
543 proc_rec_opt(telnet, WILL, c);
544 telnet->state = TOP_LEVEL;
547 proc_rec_opt(telnet, WONT, c);
548 telnet->state = TOP_LEVEL;
551 proc_rec_opt(telnet, DO, c);
552 telnet->state = TOP_LEVEL;
555 proc_rec_opt(telnet, DONT, c);
556 telnet->state = TOP_LEVEL;
561 telnet->state = SUBNEGOT;
565 telnet->state = SUBNEG_IAC;
568 if (telnet->sb_len >= telnet->sb_size) {
570 telnet->sb_size += SB_DELTA;
571 newbuf = (telnet->sb_buf ?
572 srealloc(telnet->sb_buf, telnet->sb_size) :
573 smalloc(telnet->sb_size));
575 telnet->sb_buf = newbuf;
577 telnet->sb_size -= SB_DELTA;
579 if (telnet->sb_len < telnet->sb_size)
580 telnet->sb_buf[telnet->sb_len++] = c;
581 telnet->state = SUBNEGOT; /* in case we came here by goto */
586 goto subneg_addchar; /* yes, it's a hack, I know, but... */
588 process_subneg(telnet);
589 telnet->state = TOP_LEVEL;
596 static int telnet_closing(Plug plug, char *error_msg, int error_code,
599 Telnet telnet = (Telnet) plug;
606 /* A socket error has occurred. */
607 logevent(telnet->frontend, error_msg);
608 connection_fatal("%s", error_msg);
609 } /* Otherwise, the remote side closed the connection normally. */
613 static int telnet_receive(Plug plug, int urgent, char *data, int len)
615 Telnet telnet = (Telnet) plug;
617 telnet->in_synch = TRUE;
618 do_telnet_read(telnet, data, len);
622 static void telnet_sent(Plug plug, int bufsize)
624 Telnet telnet = (Telnet) plug;
625 telnet->bufsize = bufsize;
629 * Called to set up the Telnet connection.
631 * Returns an error message, or NULL on success.
633 * Also places the canonical host name into `realhost'. It must be
634 * freed by the caller.
636 static char *telnet_init(void *frontend_handle, void **backend_handle,
637 char *host, int port, char **realhost, int nodelay)
639 static const struct plug_function_table fn_table = {
648 telnet = smalloc(sizeof(*telnet));
649 telnet->fn = &fn_table;
651 telnet->echoing = TRUE;
652 telnet->editing = TRUE;
653 telnet->activated = FALSE;
654 telnet->sb_buf = NULL;
656 telnet->frontend = frontend_handle;
657 telnet->term_width = cfg.width;
658 telnet->term_height = cfg.height;
659 telnet->state = TOP_LEVEL;
660 *backend_handle = telnet;
667 sprintf(buf, "Looking up host \"%.170s\"", host);
668 logevent(telnet->frontend, buf);
670 addr = sk_namelookup(host, realhost);
671 if ((err = sk_addr_error(addr)))
675 port = 23; /* default telnet port */
681 char buf[200], addrbuf[100];
682 sk_getaddr(addr, addrbuf, 100);
683 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
684 logevent(telnet->frontend, buf);
686 telnet->s = new_connection(addr, *realhost, port, 0, 1,
687 nodelay, (Plug) telnet);
688 if ((err = sk_socket_error(telnet->s)))
694 * Initialise option states.
696 if (cfg.passive_telnet) {
697 const struct Opt *const *o;
699 for (o = opts; *o; o++)
700 telnet->opt_states[(*o)->index] = INACTIVE;
702 const struct Opt *const *o;
704 for (o = opts; *o; o++) {
705 telnet->opt_states[(*o)->index] = (*o)->initial_state;
706 if (telnet->opt_states[(*o)->index] == REQUESTED)
707 send_opt(telnet, (*o)->send, (*o)->option);
709 telnet->activated = TRUE;
713 * Set up SYNCH state.
715 telnet->in_synch = FALSE;
721 * Called to send data down the Telnet connection.
723 static int telnet_send(void *handle, char *buf, int len)
725 Telnet telnet = (Telnet) handle;
727 static unsigned char iac[2] = { IAC, IAC };
728 static unsigned char cr[2] = { CR, NUL };
730 static unsigned char nl[2] = { CR, LF };
733 if (telnet->s == NULL)
737 while (p < buf + len) {
740 while (iswritable((unsigned char) *p) && p < buf + len)
742 telnet->bufsize = sk_write(telnet->s, q, p - q);
744 while (p < buf + len && !iswritable((unsigned char) *p)) {
746 sk_write(telnet->s, (unsigned char) *p == IAC ? iac : cr, 2);
751 return telnet->bufsize;
755 * Called to query the current socket sendability status.
757 static int telnet_sendbuffer(void *handle)
759 Telnet telnet = (Telnet) handle;
760 return telnet->bufsize;
764 * Called to set the size of the window from Telnet's POV.
766 static void telnet_size(void *handle, int width, int height)
768 Telnet telnet = (Telnet) handle;
772 telnet->term_width = width;
773 telnet->term_height = height;
775 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
780 b[3] = telnet->term_width >> 8;
781 b[4] = telnet->term_width & 0xFF;
782 b[5] = telnet->term_height >> 8;
783 b[6] = telnet->term_height & 0xFF;
786 telnet->bufsize = sk_write(telnet->s, b, 9);
787 sprintf(logbuf, "client:\tSB NAWS %d,%d",
788 ((unsigned char) b[3] << 8) + (unsigned char) b[4],
789 ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
790 logevent(telnet->frontend, logbuf);
794 * Send Telnet special codes.
796 static void telnet_special(void *handle, Telnet_Special code)
798 Telnet telnet = (Telnet) handle;
801 if (telnet->s == NULL)
808 telnet->bufsize = sk_write(telnet->s, b, 2);
812 telnet->bufsize = sk_write(telnet->s, b, 2);
816 telnet->bufsize = sk_write(telnet->s, b, 2);
820 telnet->bufsize = sk_write(telnet->s, b, 2);
824 telnet->bufsize = sk_write(telnet->s, b, 2);
828 telnet->bufsize = sk_write(telnet->s, b, 2);
832 telnet->bufsize = sk_write(telnet->s, b, 2);
836 telnet->bufsize = sk_write(telnet->s, b, 2);
840 telnet->bufsize = sk_write(telnet->s, b, 2);
844 telnet->bufsize = sk_write(telnet->s, b, 2);
848 telnet->bufsize = sk_write(telnet->s, b, 2);
852 telnet->bufsize = sk_write(telnet->s, b, 2);
855 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
859 telnet->bufsize = sk_write(telnet->s, b, 1);
860 telnet->bufsize = sk_write_oob(telnet->s, b + 1, 1);
863 if (telnet->opt_states[o_echo.index] == INACTIVE ||
864 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
865 telnet->opt_states[o_echo.index] = REQUESTED;
866 send_opt(telnet, o_echo.send, o_echo.option);
870 if (telnet->opt_states[o_echo.index] == ACTIVE) {
871 telnet->opt_states[o_echo.index] = REQUESTED;
872 send_opt(telnet, o_echo.nsend, o_echo.option);
876 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
878 telnet->bufsize = sk_write(telnet->s, b, 2);
884 static Socket telnet_socket(void *handle)
886 Telnet telnet = (Telnet) handle;
890 static int telnet_sendok(void *handle)
892 /* Telnet telnet = (Telnet) handle; */
896 static void telnet_unthrottle(void *handle, int backlog)
898 Telnet telnet = (Telnet) handle;
899 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
902 static int telnet_ldisc(void *handle, int option)
904 Telnet telnet = (Telnet) handle;
905 if (option == LD_ECHO)
906 return telnet->echoing;
907 if (option == LD_EDIT)
908 return telnet->editing;
912 static void telnet_provide_ldisc(void *handle, void *ldisc)
914 Telnet telnet = (Telnet) handle;
915 telnet->ldisc = ldisc;
918 static void telnet_provide_logctx(void *handle, void *logctx)
920 /* This is a stub. */
923 static int telnet_exitcode(void *handle)
925 /* Telnet telnet = (Telnet) handle; */
926 /* Telnet doesn't transmit exit codes back to the client */
930 Backend telnet_backend = {
940 telnet_provide_ldisc,
941 telnet_provide_logctx,