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_TN3270E 40 /* TN3270 enhancements */
76 #define TELOPT_XAUTH 41
77 #define TELOPT_CHARSET 42 /* Character set */
78 #define TELOPT_RSP 43 /* Remote serial port */
79 #define TELOPT_COM_PORT_OPTION 44 /* Com port control */
80 #define TELOPT_SLE 45 /* Suppress local echo */
81 #define TELOPT_STARTTLS 46 /* Start TLS */
82 #define TELOPT_KERMIT 47 /* Automatic Kermit file transfer */
83 #define TELOPT_SEND_URL 48
84 #define TELOPT_FORWARD_X 49
85 #define TELOPT_PRAGMA_LOGON 138
86 #define TELOPT_SSPI_LOGON 139
87 #define TELOPT_PRAGMA_HEARTBEAT 140
88 #define TELOPT_EXOPL 255 /* extended-options-list */
90 #define TELQUAL_IS 0 /* option is... */
91 #define TELQUAL_SEND 1 /* send option */
92 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
102 #define iswritable(x) \
104 (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
106 static char *telopt(int opt)
108 #define i(x) if (opt == TELOPT_ ## x) return #x;
166 static void telnet_size(void *handle, int width, int height);
169 int send; /* what we initially send */
170 int nsend; /* -ve send if requested to stop it */
171 int ack, nak; /* +ve and -ve acknowledgements */
172 int option; /* the option code */
173 int index; /* index into telnet->opt_states[] */
175 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
193 static const struct Opt o_naws =
194 { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
195 static const struct Opt o_tspeed =
196 { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
197 static const struct Opt o_ttype =
198 { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
199 static const struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
200 OPTINDEX_OENV, INACTIVE
202 static const struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
203 OPTINDEX_NENV, REQUESTED
205 static const struct Opt o_echo =
206 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
207 static const struct Opt o_we_sga =
208 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
209 static const struct Opt o_they_sga =
210 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
211 static const struct Opt o_we_bin =
212 { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
213 static const struct Opt o_they_bin =
214 { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
216 static const struct Opt *const opts[] = {
217 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
218 &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
221 typedef struct telnet_tag {
222 const struct plug_function_table *fn;
223 /* the above field _must_ be first in the structure */
229 int term_width, term_height;
231 int opt_states[NUM_OPTS];
233 int echoing, editing;
238 unsigned char *sb_buf;
242 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
243 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
251 #define TELNET_MAX_BACKLOG 4096
253 #define SB_DELTA 1024
255 static void c_write1(Telnet telnet, int c)
259 backlog = from_backend(telnet->frontend, 0, &cc, 1);
260 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
263 static void log_option(Telnet telnet, char *sender, int cmd, int option)
267 * The strange-looking "<?""?>" below is there to avoid a
268 * trigraph - a double question mark followed by > maps to a
269 * closing brace character!
271 buf = dupprintf("%s:\t%s %s", sender,
272 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
273 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"),
275 logevent(telnet->frontend, buf);
279 static void send_opt(Telnet telnet, int cmd, int option)
286 telnet->bufsize = sk_write(telnet->s, (char *)b, 3);
287 log_option(telnet, "client", cmd, option);
290 static void deactivate_option(Telnet telnet, const struct Opt *o)
292 if (telnet->opt_states[o->index] == REQUESTED ||
293 telnet->opt_states[o->index] == ACTIVE)
294 send_opt(telnet, o->nsend, o->option);
295 telnet->opt_states[o->index] = REALLY_INACTIVE;
299 * Generate side effects of enabling or disabling an option.
301 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
303 if (o->option == TELOPT_ECHO && o->send == DO)
304 telnet->echoing = !enabled;
305 else if (o->option == TELOPT_SGA && o->send == DO)
306 telnet->editing = !enabled;
307 if (telnet->ldisc) /* cause ldisc to notice the change */
308 ldisc_send(telnet->ldisc, NULL, 0, 0);
310 /* Ensure we get the minimum options */
311 if (!telnet->activated) {
312 if (telnet->opt_states[o_echo.index] == INACTIVE) {
313 telnet->opt_states[o_echo.index] = REQUESTED;
314 send_opt(telnet, o_echo.send, o_echo.option);
316 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
317 telnet->opt_states[o_we_sga.index] = REQUESTED;
318 send_opt(telnet, o_we_sga.send, o_we_sga.option);
320 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
321 telnet->opt_states[o_they_sga.index] = REQUESTED;
322 send_opt(telnet, o_they_sga.send, o_they_sga.option);
324 telnet->activated = TRUE;
328 static void activate_option(Telnet telnet, const struct Opt *o)
330 if (o->send == WILL && o->option == TELOPT_NAWS)
331 telnet_size(telnet, telnet->term_width, telnet->term_height);
332 if (o->send == WILL &&
333 (o->option == TELOPT_NEW_ENVIRON ||
334 o->option == TELOPT_OLD_ENVIRON)) {
336 * We may only have one kind of ENVIRON going at a time.
337 * This is a hack, but who cares.
339 deactivate_option(telnet, o->option ==
340 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
342 option_side_effects(telnet, o, 1);
345 static void refused_option(Telnet telnet, const struct Opt *o)
347 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
348 telnet->opt_states[o_oenv.index] == INACTIVE) {
349 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
350 telnet->opt_states[o_oenv.index] = REQUESTED;
352 option_side_effects(telnet, o, 0);
355 static void proc_rec_opt(Telnet telnet, int cmd, int option)
357 const struct Opt *const *o;
359 log_option(telnet, "server", cmd, option);
360 for (o = opts; *o; o++) {
361 if ((*o)->option == option && (*o)->ack == cmd) {
362 switch (telnet->opt_states[(*o)->index]) {
364 telnet->opt_states[(*o)->index] = ACTIVE;
365 activate_option(telnet, *o);
370 telnet->opt_states[(*o)->index] = ACTIVE;
371 send_opt(telnet, (*o)->send, option);
372 activate_option(telnet, *o);
374 case REALLY_INACTIVE:
375 send_opt(telnet, (*o)->nsend, option);
379 } else if ((*o)->option == option && (*o)->nak == cmd) {
380 switch (telnet->opt_states[(*o)->index]) {
382 telnet->opt_states[(*o)->index] = INACTIVE;
383 refused_option(telnet, *o);
386 telnet->opt_states[(*o)->index] = INACTIVE;
387 send_opt(telnet, (*o)->nsend, option);
388 option_side_effects(telnet, *o, 0);
391 case REALLY_INACTIVE:
398 * If we reach here, the option was one we weren't prepared to
399 * cope with. So send a negative ack.
401 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
404 static void process_subneg(Telnet telnet)
406 unsigned char b[2048], *p, *q;
410 switch (telnet->sb_opt) {
412 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
416 b[2] = TELOPT_TSPEED;
418 strcpy((char *)(b + 4), telnet->cfg.termspeed);
419 n = 4 + strlen(telnet->cfg.termspeed);
422 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2);
423 logevent(telnet->frontend, "server:\tSB TSPEED SEND");
424 logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed);
425 logevent(telnet->frontend, logbuf);
428 logevent(telnet->frontend, "server:\tSB TSPEED <something weird>");
431 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
437 for (n = 0; telnet->cfg.termtype[n]; n++)
438 b[n + 4] = (telnet->cfg.termtype[n] >= 'a'
439 && telnet->cfg.termtype[n] <=
440 'z' ? telnet->cfg.termtype[n] + 'A' -
441 'a' : telnet->cfg.termtype[n]);
444 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6);
446 logevent(telnet->frontend, "server:\tSB TTYPE SEND");
447 logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4);
448 logevent(telnet->frontend, logbuf);
451 logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n");
453 case TELOPT_OLD_ENVIRON:
454 case TELOPT_NEW_ENVIRON:
456 q = p + telnet->sb_len;
457 if (p < q && *p == TELQUAL_SEND) {
460 logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt));
461 logevent(telnet->frontend, logbuf);
463 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
464 if (telnet->cfg.rfc_environ) {
472 * Try to guess the sense of VAR and VALUE.
478 } else if (*p == BSD_VAR) {
486 * With NEW_ENVIRON, the sense of VAR and VALUE
494 b[2] = telnet->sb_opt;
497 e = telnet->cfg.environmt;
500 while (*e && *e != '\t')
509 if (*telnet->cfg.username) {
516 e = telnet->cfg.username;
522 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
523 logbuf = dupprintf("client:\tSB %s IS %s%s%s%s",
524 telopt(telnet->sb_opt),
525 *telnet->cfg.username ? "USER=" : "",
526 telnet->cfg.username,
527 *telnet->cfg.username ? " " : "",
528 n == 6 ? "<nothing>" :
529 (*telnet->cfg.environmt ? "<stuff>" : ""));
530 logevent(telnet->frontend, logbuf);
537 static void do_telnet_read(Telnet telnet, char *buf, int len)
541 int c = (unsigned char) *buf++;
543 switch (telnet->state) {
546 if (c == NUL && telnet->state == SEENCR)
547 telnet->state = TOP_LEVEL;
549 telnet->state = SEENIAC;
551 if (!telnet->in_synch)
555 /* I can't get the F***ing winsock to insert the urgent IAC
556 * into the right position! Even with SO_OOBINLINE it gives
557 * it to recv too soon. And of course the DM byte (that
558 * arrives in the same packet!) appears several K later!!
560 * Oh well, we do get the DM in the right place so I'll
561 * just stop hiding on the next 0xf2 and hope for the best.
564 telnet->in_synch = 0;
566 if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
567 telnet->state = SEENCR;
569 telnet->state = TOP_LEVEL;
574 telnet->state = SEENDO;
576 telnet->state = SEENDONT;
578 telnet->state = SEENWILL;
580 telnet->state = SEENWONT;
582 telnet->state = SEENSB;
584 telnet->in_synch = 0;
585 telnet->state = TOP_LEVEL;
587 /* ignore everything else; print it if it's IAC */
591 telnet->state = TOP_LEVEL;
595 proc_rec_opt(telnet, WILL, c);
596 telnet->state = TOP_LEVEL;
599 proc_rec_opt(telnet, WONT, c);
600 telnet->state = TOP_LEVEL;
603 proc_rec_opt(telnet, DO, c);
604 telnet->state = TOP_LEVEL;
607 proc_rec_opt(telnet, DONT, c);
608 telnet->state = TOP_LEVEL;
613 telnet->state = SUBNEGOT;
617 telnet->state = SUBNEG_IAC;
620 if (telnet->sb_len >= telnet->sb_size) {
621 telnet->sb_size += SB_DELTA;
622 telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
625 telnet->sb_buf[telnet->sb_len++] = c;
626 telnet->state = SUBNEGOT; /* in case we came here by goto */
631 goto subneg_addchar; /* yes, it's a hack, I know, but... */
633 process_subneg(telnet);
634 telnet->state = TOP_LEVEL;
641 static int telnet_closing(Plug plug, const char *error_msg, int error_code,
644 Telnet telnet = (Telnet) plug;
649 notify_remote_exit(telnet->frontend);
652 /* A socket error has occurred. */
653 logevent(telnet->frontend, error_msg);
654 connection_fatal(telnet->frontend, "%s", error_msg);
655 } /* Otherwise, the remote side closed the connection normally. */
659 static int telnet_receive(Plug plug, int urgent, char *data, int len)
661 Telnet telnet = (Telnet) plug;
663 telnet->in_synch = TRUE;
664 do_telnet_read(telnet, data, len);
668 static void telnet_sent(Plug plug, int bufsize)
670 Telnet telnet = (Telnet) plug;
671 telnet->bufsize = bufsize;
675 * Called to set up the Telnet connection.
677 * Returns an error message, or NULL on success.
679 * Also places the canonical host name into `realhost'. It must be
680 * freed by the caller.
682 static const char *telnet_init(void *frontend_handle, void **backend_handle,
684 char *host, int port, char **realhost,
685 int nodelay, int keepalive)
687 static const struct plug_function_table fn_table = {
696 telnet = snew(struct telnet_tag);
697 telnet->fn = &fn_table;
698 telnet->cfg = *cfg; /* STRUCTURE COPY */
700 telnet->echoing = TRUE;
701 telnet->editing = TRUE;
702 telnet->activated = FALSE;
703 telnet->sb_buf = NULL;
705 telnet->frontend = frontend_handle;
706 telnet->term_width = telnet->cfg.width;
707 telnet->term_height = telnet->cfg.height;
708 telnet->state = TOP_LEVEL;
709 telnet->ldisc = NULL;
710 telnet->pinger = NULL;
711 *backend_handle = telnet;
718 buf = dupprintf("Looking up host \"%s\"%s", host,
719 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
720 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
722 logevent(telnet->frontend, buf);
725 addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily);
726 if ((err = sk_addr_error(addr)) != NULL) {
732 port = 23; /* default telnet port */
738 char *buf, addrbuf[100];
739 sk_getaddr(addr, addrbuf, 100);
740 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
741 logevent(telnet->frontend, buf);
744 telnet->s = new_connection(addr, *realhost, port, 0, 1,
745 nodelay, keepalive, (Plug) telnet, &telnet->cfg);
746 if ((err = sk_socket_error(telnet->s)) != NULL)
749 telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet);
752 * Initialise option states.
754 if (telnet->cfg.passive_telnet) {
755 const struct Opt *const *o;
757 for (o = opts; *o; o++)
758 telnet->opt_states[(*o)->index] = INACTIVE;
760 const struct Opt *const *o;
762 for (o = opts; *o; o++) {
763 telnet->opt_states[(*o)->index] = (*o)->initial_state;
764 if (telnet->opt_states[(*o)->index] == REQUESTED)
765 send_opt(telnet, (*o)->send, (*o)->option);
767 telnet->activated = TRUE;
771 * Set up SYNCH state.
773 telnet->in_synch = FALSE;
776 * We can send special commands from the start.
778 update_specials_menu(telnet->frontend);
783 static void telnet_free(void *handle)
785 Telnet telnet = (Telnet) handle;
787 sfree(telnet->sb_buf);
791 pinger_free(telnet->pinger);
795 * Reconfigure the Telnet backend. There's no immediate action
796 * necessary, in this backend: we just save the fresh config for
797 * any subsequent negotiations.
799 static void telnet_reconfig(void *handle, Config *cfg)
801 Telnet telnet = (Telnet) handle;
802 pinger_reconfig(telnet->pinger, &telnet->cfg, cfg);
803 telnet->cfg = *cfg; /* STRUCTURE COPY */
807 * Called to send data down the Telnet connection.
809 static int telnet_send(void *handle, char *buf, int len)
811 Telnet telnet = (Telnet) handle;
812 unsigned char *p, *end;
813 static const unsigned char iac[2] = { IAC, IAC };
814 static const unsigned char cr[2] = { CR, NUL };
816 static const unsigned char nl[2] = { CR, LF };
819 if (telnet->s == NULL)
822 p = (unsigned char *)buf;
823 end = (unsigned char *)(buf + len);
825 unsigned char *q = p;
827 while (p < end && iswritable(*p))
829 telnet->bufsize = sk_write(telnet->s, (char *)q, p - q);
831 while (p < end && !iswritable(*p)) {
833 sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2);
838 return telnet->bufsize;
842 * Called to query the current socket sendability status.
844 static int telnet_sendbuffer(void *handle)
846 Telnet telnet = (Telnet) handle;
847 return telnet->bufsize;
851 * Called to set the size of the window from Telnet's POV.
853 static void telnet_size(void *handle, int width, int height)
855 Telnet telnet = (Telnet) handle;
860 telnet->term_width = width;
861 telnet->term_height = height;
863 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
868 b[n++] = TELOPT_NAWS;
869 b[n++] = telnet->term_width >> 8;
870 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
871 b[n++] = telnet->term_width & 0xFF;
872 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
873 b[n++] = telnet->term_height >> 8;
874 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
875 b[n++] = telnet->term_height & 0xFF;
876 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
879 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
880 logbuf = dupprintf("client:\tSB NAWS %d,%d",
881 telnet->term_width, telnet->term_height);
882 logevent(telnet->frontend, logbuf);
887 * Send Telnet special codes.
889 static void telnet_special(void *handle, Telnet_Special code)
891 Telnet telnet = (Telnet) handle;
894 if (telnet->s == NULL)
901 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
905 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
909 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
913 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
917 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
921 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
925 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
929 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
933 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
937 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
941 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
945 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
948 /* In BINARY mode, CR-LF becomes just CR -
949 * and without the NUL suffix too. */
950 if (telnet->opt_states[o_we_bin.index] == ACTIVE)
951 telnet->bufsize = sk_write(telnet->s, "\r", 1);
953 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
957 telnet->bufsize = sk_write(telnet->s, (char *)b, 1);
958 telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1);
961 if (telnet->opt_states[o_echo.index] == INACTIVE ||
962 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
963 telnet->opt_states[o_echo.index] = REQUESTED;
964 send_opt(telnet, o_echo.send, o_echo.option);
968 if (telnet->opt_states[o_echo.index] == ACTIVE) {
969 telnet->opt_states[o_echo.index] = REQUESTED;
970 send_opt(telnet, o_echo.nsend, o_echo.option);
974 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
976 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
980 break; /* never heard of it */
984 static const struct telnet_special *telnet_get_specials(void *handle)
986 static const struct telnet_special specials[] = {
987 {"Are You There", TS_AYT},
990 {"Erase Character", TS_EC},
991 {"Erase Line", TS_EL},
993 {"No Operation", TS_NOP},
995 {"Abort Process", TS_ABORT},
996 {"Abort Output", TS_AO},
997 {"Interrupt Process", TS_IP},
998 {"Suspend Process", TS_SUSP},
1000 {"End Of Record", TS_EOR},
1001 {"End Of File", TS_EOF},
1007 static Socket telnet_socket(void *handle)
1009 Telnet telnet = (Telnet) handle;
1013 static int telnet_sendok(void *handle)
1015 /* Telnet telnet = (Telnet) handle; */
1019 static void telnet_unthrottle(void *handle, int backlog)
1021 Telnet telnet = (Telnet) handle;
1022 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
1025 static int telnet_ldisc(void *handle, int option)
1027 Telnet telnet = (Telnet) handle;
1028 if (option == LD_ECHO)
1029 return telnet->echoing;
1030 if (option == LD_EDIT)
1031 return telnet->editing;
1035 static void telnet_provide_ldisc(void *handle, void *ldisc)
1037 Telnet telnet = (Telnet) handle;
1038 telnet->ldisc = ldisc;
1041 static void telnet_provide_logctx(void *handle, void *logctx)
1043 /* This is a stub. */
1046 static int telnet_exitcode(void *handle)
1048 Telnet telnet = (Telnet) handle;
1049 if (telnet->s != NULL)
1050 return -1; /* still connected */
1052 /* Telnet doesn't transmit exit codes back to the client */
1057 * cfg_info for Telnet does nothing at all.
1059 static int telnet_cfg_info(void *handle)
1064 Backend telnet_backend = {
1072 telnet_get_specials,
1077 telnet_provide_ldisc,
1078 telnet_provide_logctx,