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 =
200 { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE };
201 static const struct Opt o_nenv =
202 { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED };
203 static const struct Opt o_echo =
204 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
205 static const struct Opt o_we_sga =
206 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
207 static const struct Opt o_they_sga =
208 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
209 static const struct Opt o_we_bin =
210 { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
211 static const struct Opt o_they_bin =
212 { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
214 static const struct Opt *const opts[] = {
215 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
216 &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
219 typedef struct telnet_tag {
220 const struct plug_function_table *fn;
221 /* the above field _must_ be first in the structure */
227 int term_width, term_height;
229 int opt_states[NUM_OPTS];
231 int echoing, editing;
236 unsigned char *sb_buf;
240 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
241 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
249 #define TELNET_MAX_BACKLOG 4096
251 #define SB_DELTA 1024
253 static void c_write1(Telnet telnet, int c)
257 backlog = from_backend(telnet->frontend, 0, &cc, 1);
258 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
261 static void log_option(Telnet telnet, char *sender, int cmd, int option)
265 * The strange-looking "<?""?>" below is there to avoid a
266 * trigraph - a double question mark followed by > maps to a
267 * closing brace character!
269 buf = dupprintf("%s:\t%s %s", sender,
270 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
271 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"),
273 logevent(telnet->frontend, buf);
277 static void send_opt(Telnet telnet, int cmd, int option)
284 telnet->bufsize = sk_write(telnet->s, (char *)b, 3);
285 log_option(telnet, "client", cmd, option);
288 static void deactivate_option(Telnet telnet, const struct Opt *o)
290 if (telnet->opt_states[o->index] == REQUESTED ||
291 telnet->opt_states[o->index] == ACTIVE)
292 send_opt(telnet, o->nsend, o->option);
293 telnet->opt_states[o->index] = REALLY_INACTIVE;
297 * Generate side effects of enabling or disabling an option.
299 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
301 if (o->option == TELOPT_ECHO && o->send == DO)
302 telnet->echoing = !enabled;
303 else if (o->option == TELOPT_SGA && o->send == DO)
304 telnet->editing = !enabled;
305 if (telnet->ldisc) /* cause ldisc to notice the change */
306 ldisc_send(telnet->ldisc, NULL, 0, 0);
308 /* Ensure we get the minimum options */
309 if (!telnet->activated) {
310 if (telnet->opt_states[o_echo.index] == INACTIVE) {
311 telnet->opt_states[o_echo.index] = REQUESTED;
312 send_opt(telnet, o_echo.send, o_echo.option);
314 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
315 telnet->opt_states[o_we_sga.index] = REQUESTED;
316 send_opt(telnet, o_we_sga.send, o_we_sga.option);
318 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
319 telnet->opt_states[o_they_sga.index] = REQUESTED;
320 send_opt(telnet, o_they_sga.send, o_they_sga.option);
322 telnet->activated = TRUE;
326 static void activate_option(Telnet telnet, const struct Opt *o)
328 if (o->send == WILL && o->option == TELOPT_NAWS)
329 telnet_size(telnet, telnet->term_width, telnet->term_height);
330 if (o->send == WILL &&
331 (o->option == TELOPT_NEW_ENVIRON ||
332 o->option == TELOPT_OLD_ENVIRON)) {
334 * We may only have one kind of ENVIRON going at a time.
335 * This is a hack, but who cares.
337 deactivate_option(telnet, o->option ==
338 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
340 option_side_effects(telnet, o, 1);
343 static void refused_option(Telnet telnet, const struct Opt *o)
345 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
346 telnet->opt_states[o_oenv.index] == INACTIVE) {
347 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
348 telnet->opt_states[o_oenv.index] = REQUESTED;
350 option_side_effects(telnet, o, 0);
353 static void proc_rec_opt(Telnet telnet, int cmd, int option)
355 const struct Opt *const *o;
357 log_option(telnet, "server", cmd, option);
358 for (o = opts; *o; o++) {
359 if ((*o)->option == option && (*o)->ack == cmd) {
360 switch (telnet->opt_states[(*o)->index]) {
362 telnet->opt_states[(*o)->index] = ACTIVE;
363 activate_option(telnet, *o);
368 telnet->opt_states[(*o)->index] = ACTIVE;
369 send_opt(telnet, (*o)->send, option);
370 activate_option(telnet, *o);
372 case REALLY_INACTIVE:
373 send_opt(telnet, (*o)->nsend, option);
377 } else if ((*o)->option == option && (*o)->nak == cmd) {
378 switch (telnet->opt_states[(*o)->index]) {
380 telnet->opt_states[(*o)->index] = INACTIVE;
381 refused_option(telnet, *o);
384 telnet->opt_states[(*o)->index] = INACTIVE;
385 send_opt(telnet, (*o)->nsend, option);
386 option_side_effects(telnet, *o, 0);
389 case REALLY_INACTIVE:
396 * If we reach here, the option was one we weren't prepared to
397 * cope with. If the request was positive (WILL or DO), we send
398 * a negative ack to indicate refusal. If the request was
399 * negative (WONT / DONT), we must do nothing.
401 if (cmd == WILL || cmd == DO)
402 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
405 static void process_subneg(Telnet telnet)
407 unsigned char b[2048], *p, *q;
411 switch (telnet->sb_opt) {
413 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
417 b[2] = TELOPT_TSPEED;
419 strcpy((char *)(b + 4), telnet->cfg.termspeed);
420 n = 4 + strlen(telnet->cfg.termspeed);
423 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2);
424 logevent(telnet->frontend, "server:\tSB TSPEED SEND");
425 logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed);
426 logevent(telnet->frontend, logbuf);
429 logevent(telnet->frontend, "server:\tSB TSPEED <something weird>");
432 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
438 for (n = 0; telnet->cfg.termtype[n]; n++)
439 b[n + 4] = (telnet->cfg.termtype[n] >= 'a'
440 && telnet->cfg.termtype[n] <=
441 'z' ? telnet->cfg.termtype[n] + 'A' -
442 'a' : telnet->cfg.termtype[n]);
445 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6);
447 logevent(telnet->frontend, "server:\tSB TTYPE SEND");
448 logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4);
449 logevent(telnet->frontend, logbuf);
452 logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n");
454 case TELOPT_OLD_ENVIRON:
455 case TELOPT_NEW_ENVIRON:
457 q = p + telnet->sb_len;
458 if (p < q && *p == TELQUAL_SEND) {
461 logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt));
462 logevent(telnet->frontend, logbuf);
464 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
465 if (telnet->cfg.rfc_environ) {
473 * Try to guess the sense of VAR and VALUE.
479 } else if (*p == BSD_VAR) {
487 * With NEW_ENVIRON, the sense of VAR and VALUE
495 b[2] = telnet->sb_opt;
498 e = telnet->cfg.environmt;
501 while (*e && *e != '\t')
510 if (*telnet->cfg.username) {
517 e = telnet->cfg.username;
523 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
524 logbuf = dupprintf("client:\tSB %s IS %s%s%s%s",
525 telopt(telnet->sb_opt),
526 *telnet->cfg.username ? "USER=" : "",
527 telnet->cfg.username,
528 *telnet->cfg.username ? " " : "",
529 n == 6 ? "<nothing>" :
530 (*telnet->cfg.environmt ? "<stuff>" : ""));
531 logevent(telnet->frontend, logbuf);
538 static void do_telnet_read(Telnet telnet, char *buf, int len)
542 int c = (unsigned char) *buf++;
544 switch (telnet->state) {
547 if (c == NUL && telnet->state == SEENCR)
548 telnet->state = TOP_LEVEL;
550 telnet->state = SEENIAC;
552 if (!telnet->in_synch)
556 /* I can't get the F***ing winsock to insert the urgent IAC
557 * into the right position! Even with SO_OOBINLINE it gives
558 * it to recv too soon. And of course the DM byte (that
559 * arrives in the same packet!) appears several K later!!
561 * Oh well, we do get the DM in the right place so I'll
562 * just stop hiding on the next 0xf2 and hope for the best.
565 telnet->in_synch = 0;
567 if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
568 telnet->state = SEENCR;
570 telnet->state = TOP_LEVEL;
575 telnet->state = SEENDO;
577 telnet->state = SEENDONT;
579 telnet->state = SEENWILL;
581 telnet->state = SEENWONT;
583 telnet->state = SEENSB;
585 telnet->in_synch = 0;
586 telnet->state = TOP_LEVEL;
588 /* ignore everything else; print it if it's IAC */
592 telnet->state = TOP_LEVEL;
596 proc_rec_opt(telnet, WILL, c);
597 telnet->state = TOP_LEVEL;
600 proc_rec_opt(telnet, WONT, c);
601 telnet->state = TOP_LEVEL;
604 proc_rec_opt(telnet, DO, c);
605 telnet->state = TOP_LEVEL;
608 proc_rec_opt(telnet, DONT, c);
609 telnet->state = TOP_LEVEL;
614 telnet->state = SUBNEGOT;
618 telnet->state = SUBNEG_IAC;
621 if (telnet->sb_len >= telnet->sb_size) {
622 telnet->sb_size += SB_DELTA;
623 telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
626 telnet->sb_buf[telnet->sb_len++] = c;
627 telnet->state = SUBNEGOT; /* in case we came here by goto */
632 goto subneg_addchar; /* yes, it's a hack, I know, but... */
634 process_subneg(telnet);
635 telnet->state = TOP_LEVEL;
642 static void telnet_log(Plug plug, int type, SockAddr addr, int port,
643 const char *error_msg, int error_code)
645 Telnet telnet = (Telnet) plug;
646 char addrbuf[256], *msg;
648 sk_getaddr(addr, addrbuf, lenof(addrbuf));
651 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
653 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
655 logevent(telnet->frontend, msg);
658 static int telnet_closing(Plug plug, const char *error_msg, int error_code,
661 Telnet telnet = (Telnet) plug;
666 notify_remote_exit(telnet->frontend);
669 logevent(telnet->frontend, error_msg);
670 connection_fatal(telnet->frontend, "%s", error_msg);
672 /* Otherwise, the remote side closed the connection normally. */
676 static int telnet_receive(Plug plug, int urgent, char *data, int len)
678 Telnet telnet = (Telnet) plug;
680 telnet->in_synch = TRUE;
681 do_telnet_read(telnet, data, len);
685 static void telnet_sent(Plug plug, int bufsize)
687 Telnet telnet = (Telnet) plug;
688 telnet->bufsize = bufsize;
692 * Called to set up the Telnet connection.
694 * Returns an error message, or NULL on success.
696 * Also places the canonical host name into `realhost'. It must be
697 * freed by the caller.
699 static const char *telnet_init(void *frontend_handle, void **backend_handle,
701 char *host, int port, char **realhost,
702 int nodelay, int keepalive)
704 static const struct plug_function_table fn_table = {
714 telnet = snew(struct telnet_tag);
715 telnet->fn = &fn_table;
716 telnet->cfg = *cfg; /* STRUCTURE COPY */
718 telnet->echoing = TRUE;
719 telnet->editing = TRUE;
720 telnet->activated = FALSE;
721 telnet->sb_buf = NULL;
723 telnet->frontend = frontend_handle;
724 telnet->term_width = telnet->cfg.width;
725 telnet->term_height = telnet->cfg.height;
726 telnet->state = TOP_LEVEL;
727 telnet->ldisc = NULL;
728 telnet->pinger = NULL;
729 *backend_handle = telnet;
736 buf = dupprintf("Looking up host \"%s\"%s", host,
737 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
738 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
740 logevent(telnet->frontend, buf);
743 addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily);
744 if ((err = sk_addr_error(addr)) != NULL) {
750 port = 23; /* default telnet port */
755 telnet->s = new_connection(addr, *realhost, port, 0, 1,
756 nodelay, keepalive, (Plug) telnet, &telnet->cfg);
757 if ((err = sk_socket_error(telnet->s)) != NULL)
760 telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet);
763 * Initialise option states.
765 if (telnet->cfg.passive_telnet) {
766 const struct Opt *const *o;
768 for (o = opts; *o; o++)
769 telnet->opt_states[(*o)->index] = INACTIVE;
771 const struct Opt *const *o;
773 for (o = opts; *o; o++) {
774 telnet->opt_states[(*o)->index] = (*o)->initial_state;
775 if (telnet->opt_states[(*o)->index] == REQUESTED)
776 send_opt(telnet, (*o)->send, (*o)->option);
778 telnet->activated = TRUE;
782 * Set up SYNCH state.
784 telnet->in_synch = FALSE;
787 * We can send special commands from the start.
789 update_specials_menu(telnet->frontend);
794 static void telnet_free(void *handle)
796 Telnet telnet = (Telnet) handle;
798 sfree(telnet->sb_buf);
802 pinger_free(telnet->pinger);
806 * Reconfigure the Telnet backend. There's no immediate action
807 * necessary, in this backend: we just save the fresh config for
808 * any subsequent negotiations.
810 static void telnet_reconfig(void *handle, Config *cfg)
812 Telnet telnet = (Telnet) handle;
813 pinger_reconfig(telnet->pinger, &telnet->cfg, cfg);
814 telnet->cfg = *cfg; /* STRUCTURE COPY */
818 * Called to send data down the Telnet connection.
820 static int telnet_send(void *handle, char *buf, int len)
822 Telnet telnet = (Telnet) handle;
823 unsigned char *p, *end;
824 static const unsigned char iac[2] = { IAC, IAC };
825 static const unsigned char cr[2] = { CR, NUL };
827 static const unsigned char nl[2] = { CR, LF };
830 if (telnet->s == NULL)
833 p = (unsigned char *)buf;
834 end = (unsigned char *)(buf + len);
836 unsigned char *q = p;
838 while (p < end && iswritable(*p))
840 telnet->bufsize = sk_write(telnet->s, (char *)q, p - q);
842 while (p < end && !iswritable(*p)) {
844 sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2);
849 return telnet->bufsize;
853 * Called to query the current socket sendability status.
855 static int telnet_sendbuffer(void *handle)
857 Telnet telnet = (Telnet) handle;
858 return telnet->bufsize;
862 * Called to set the size of the window from Telnet's POV.
864 static void telnet_size(void *handle, int width, int height)
866 Telnet telnet = (Telnet) handle;
871 telnet->term_width = width;
872 telnet->term_height = height;
874 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
879 b[n++] = TELOPT_NAWS;
880 b[n++] = telnet->term_width >> 8;
881 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
882 b[n++] = telnet->term_width & 0xFF;
883 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
884 b[n++] = telnet->term_height >> 8;
885 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
886 b[n++] = telnet->term_height & 0xFF;
887 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
890 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
891 logbuf = dupprintf("client:\tSB NAWS %d,%d",
892 telnet->term_width, telnet->term_height);
893 logevent(telnet->frontend, logbuf);
898 * Send Telnet special codes.
900 static void telnet_special(void *handle, Telnet_Special code)
902 Telnet telnet = (Telnet) handle;
905 if (telnet->s == NULL)
912 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
916 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
920 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
924 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
928 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
932 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
936 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
940 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
944 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
948 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
952 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
956 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
959 /* In BINARY mode, CR-LF becomes just CR -
960 * and without the NUL suffix too. */
961 if (telnet->opt_states[o_we_bin.index] == ACTIVE)
962 telnet->bufsize = sk_write(telnet->s, "\r", 1);
964 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
968 telnet->bufsize = sk_write(telnet->s, (char *)b, 1);
969 telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1);
972 if (telnet->opt_states[o_echo.index] == INACTIVE ||
973 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
974 telnet->opt_states[o_echo.index] = REQUESTED;
975 send_opt(telnet, o_echo.send, o_echo.option);
979 if (telnet->opt_states[o_echo.index] == ACTIVE) {
980 telnet->opt_states[o_echo.index] = REQUESTED;
981 send_opt(telnet, o_echo.nsend, o_echo.option);
985 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
987 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
991 break; /* never heard of it */
995 static const struct telnet_special *telnet_get_specials(void *handle)
997 static const struct telnet_special specials[] = {
998 {"Are You There", TS_AYT},
1000 {"Synch", TS_SYNCH},
1001 {"Erase Character", TS_EC},
1002 {"Erase Line", TS_EL},
1003 {"Go Ahead", TS_GA},
1004 {"No Operation", TS_NOP},
1006 {"Abort Process", TS_ABORT},
1007 {"Abort Output", TS_AO},
1008 {"Interrupt Process", TS_IP},
1009 {"Suspend Process", TS_SUSP},
1011 {"End Of Record", TS_EOR},
1012 {"End Of File", TS_EOF},
1018 static Socket telnet_socket(void *handle)
1020 Telnet telnet = (Telnet) handle;
1024 static int telnet_sendok(void *handle)
1026 /* Telnet telnet = (Telnet) handle; */
1030 static void telnet_unthrottle(void *handle, int backlog)
1032 Telnet telnet = (Telnet) handle;
1033 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
1036 static int telnet_ldisc(void *handle, int option)
1038 Telnet telnet = (Telnet) handle;
1039 if (option == LD_ECHO)
1040 return telnet->echoing;
1041 if (option == LD_EDIT)
1042 return telnet->editing;
1046 static void telnet_provide_ldisc(void *handle, void *ldisc)
1048 Telnet telnet = (Telnet) handle;
1049 telnet->ldisc = ldisc;
1052 static void telnet_provide_logctx(void *handle, void *logctx)
1054 /* This is a stub. */
1057 static int telnet_exitcode(void *handle)
1059 Telnet telnet = (Telnet) handle;
1060 if (telnet->s != NULL)
1061 return -1; /* still connected */
1063 /* Telnet doesn't transmit exit codes back to the client */
1068 * cfg_info for Telnet does nothing at all.
1070 static int telnet_cfg_info(void *handle)
1075 Backend telnet_backend = {
1083 telnet_get_specials,
1088 telnet_provide_ldisc,
1089 telnet_provide_logctx,