17 #define IAC 255 /* interpret as command: */
\r
18 #define DONT 254 /* you are not to use option */
\r
19 #define DO 253 /* please, you use option */
\r
20 #define WONT 252 /* I won't use option */
\r
21 #define WILL 251 /* I will use option */
\r
22 #define SB 250 /* interpret as subnegotiation */
\r
23 #define SE 240 /* end sub negotiation */
\r
25 #define GA 249 /* you may reverse the line */
\r
26 #define EL 248 /* erase the current line */
\r
27 #define EC 247 /* erase the current character */
\r
28 #define AYT 246 /* are you there */
\r
29 #define AO 245 /* abort output--but let prog finish */
\r
30 #define IP 244 /* interrupt process--permanently */
\r
31 #define BREAK 243 /* break */
\r
32 #define DM 242 /* data mark--for connect. cleaning */
\r
33 #define NOP 241 /* nop */
\r
34 #define EOR 239 /* end of record (transparent mode) */
\r
35 #define ABORT 238 /* Abort process */
\r
36 #define SUSP 237 /* Suspend process */
\r
37 #define xEOF 236 /* End of file: EOF is already used... */
\r
39 #define TELOPT_BINARY 0 /* 8-bit data path */
\r
40 #define TELOPT_ECHO 1 /* echo */
\r
41 #define TELOPT_RCP 2 /* prepare to reconnect */
\r
42 #define TELOPT_SGA 3 /* suppress go ahead */
\r
43 #define TELOPT_NAMS 4 /* approximate message size */
\r
44 #define TELOPT_STATUS 5 /* give status */
\r
45 #define TELOPT_TM 6 /* timing mark */
\r
46 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
\r
47 #define TELOPT_NAOL 8 /* negotiate about output line width */
\r
48 #define TELOPT_NAOP 9 /* negotiate about output page size */
\r
49 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
\r
50 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
\r
51 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
\r
52 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
\r
53 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
\r
54 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
\r
55 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
\r
56 #define TELOPT_XASCII 17 /* extended ascic character set */
\r
57 #define TELOPT_LOGOUT 18 /* force logout */
\r
58 #define TELOPT_BM 19 /* byte macro */
\r
59 #define TELOPT_DET 20 /* data entry terminal */
\r
60 #define TELOPT_SUPDUP 21 /* supdup protocol */
\r
61 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
\r
62 #define TELOPT_SNDLOC 23 /* send location */
\r
63 #define TELOPT_TTYPE 24 /* terminal type */
\r
64 #define TELOPT_EOR 25 /* end or record */
\r
65 #define TELOPT_TUID 26 /* TACACS user identification */
\r
66 #define TELOPT_OUTMRK 27 /* output marking */
\r
67 #define TELOPT_TTYLOC 28 /* terminal location number */
\r
68 #define TELOPT_3270REGIME 29 /* 3270 regime */
\r
69 #define TELOPT_X3PAD 30 /* X.3 PAD */
\r
70 #define TELOPT_NAWS 31 /* window size */
\r
71 #define TELOPT_TSPEED 32 /* terminal speed */
\r
72 #define TELOPT_LFLOW 33 /* remote flow control */
\r
73 #define TELOPT_LINEMODE 34 /* Linemode option */
\r
74 #define TELOPT_XDISPLOC 35 /* X Display Location */
\r
75 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
\r
76 #define TELOPT_AUTHENTICATION 37 /* Authenticate */
\r
77 #define TELOPT_ENCRYPT 38 /* Encryption option */
\r
78 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
\r
79 #define TELOPT_TN3270E 40 /* TN3270 enhancements */
\r
80 #define TELOPT_XAUTH 41
\r
81 #define TELOPT_CHARSET 42 /* Character set */
\r
82 #define TELOPT_RSP 43 /* Remote serial port */
\r
83 #define TELOPT_COM_PORT_OPTION 44 /* Com port control */
\r
84 #define TELOPT_SLE 45 /* Suppress local echo */
\r
85 #define TELOPT_STARTTLS 46 /* Start TLS */
\r
86 #define TELOPT_KERMIT 47 /* Automatic Kermit file transfer */
\r
87 #define TELOPT_SEND_URL 48
\r
88 #define TELOPT_FORWARD_X 49
\r
89 #define TELOPT_PRAGMA_LOGON 138
\r
90 #define TELOPT_SSPI_LOGON 139
\r
91 #define TELOPT_PRAGMA_HEARTBEAT 140
\r
92 #define TELOPT_EXOPL 255 /* extended-options-list */
\r
94 #define TELQUAL_IS 0 /* option is... */
\r
95 #define TELQUAL_SEND 1 /* send option */
\r
96 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
\r
100 #define RFC_VALUE 1
\r
106 #define iswritable(x) \
\r
108 (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
\r
110 static char *telopt(int opt)
\r
112 #define i(x) if (opt == TELOPT_ ## x) return #x;
\r
156 i(COM_PORT_OPTION);
\r
164 i(PRAGMA_HEARTBEAT);
\r
167 return "<unknown>";
\r
170 static void telnet_size(void *handle, int width, int height);
\r
173 int send; /* what we initially send */
\r
174 int nsend; /* -ve send if requested to stop it */
\r
175 int ack, nak; /* +ve and -ve acknowledgements */
\r
176 int option; /* the option code */
\r
177 int index; /* index into telnet->opt_states[] */
\r
179 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
\r
197 static const struct Opt o_naws =
\r
198 { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
\r
199 static const struct Opt o_tspeed =
\r
200 { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
\r
201 static const struct Opt o_ttype =
\r
202 { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
\r
203 static const struct Opt o_oenv =
\r
204 { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE };
\r
205 static const struct Opt o_nenv =
\r
206 { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED };
\r
207 static const struct Opt o_echo =
\r
208 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
\r
209 static const struct Opt o_we_sga =
\r
210 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
\r
211 static const struct Opt o_they_sga =
\r
212 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
\r
213 static const struct Opt o_we_bin =
\r
214 { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
\r
215 static const struct Opt o_they_bin =
\r
216 { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
\r
218 static const struct Opt *const opts[] = {
\r
219 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
\r
220 &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
\r
223 typedef struct telnet_tag {
\r
224 const struct plug_function_table *fn;
\r
225 /* the above field _must_ be first in the structure */
\r
231 int term_width, term_height;
\r
233 int opt_states[NUM_OPTS];
\r
235 int echoing, editing;
\r
239 int sb_opt, sb_len;
\r
240 unsigned char *sb_buf;
\r
244 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
\r
245 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
\r
253 #define TELNET_MAX_BACKLOG 4096
\r
255 #define SB_DELTA 1024
\r
257 static void c_write(Telnet telnet, char *buf, int len)
\r
260 backlog = from_backend(telnet->frontend, 0, buf, len);
\r
261 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
\r
264 static void log_option(Telnet telnet, char *sender, int cmd, int option)
\r
268 * The strange-looking "<?""?>" below is there to avoid a
\r
269 * trigraph - a double question mark followed by > maps to a
\r
270 * closing brace character!
\r
272 buf = dupprintf("%s:\t%s %s", sender,
\r
273 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
\r
274 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"),
\r
276 logevent(telnet->frontend, buf);
\r
280 static void send_opt(Telnet telnet, int cmd, int option)
\r
282 unsigned char b[3];
\r
287 telnet->bufsize = sk_write(telnet->s, (char *)b, 3);
\r
288 log_option(telnet, "client", cmd, option);
\r
291 static void deactivate_option(Telnet telnet, const struct Opt *o)
\r
293 if (telnet->opt_states[o->index] == REQUESTED ||
\r
294 telnet->opt_states[o->index] == ACTIVE)
\r
295 send_opt(telnet, o->nsend, o->option);
\r
296 telnet->opt_states[o->index] = REALLY_INACTIVE;
\r
300 * Generate side effects of enabling or disabling an option.
\r
302 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
\r
304 if (o->option == TELOPT_ECHO && o->send == DO)
\r
305 telnet->echoing = !enabled;
\r
306 else if (o->option == TELOPT_SGA && o->send == DO)
\r
307 telnet->editing = !enabled;
\r
308 if (telnet->ldisc) /* cause ldisc to notice the change */
\r
309 ldisc_send(telnet->ldisc, NULL, 0, 0);
\r
311 /* Ensure we get the minimum options */
\r
312 if (!telnet->activated) {
\r
313 if (telnet->opt_states[o_echo.index] == INACTIVE) {
\r
314 telnet->opt_states[o_echo.index] = REQUESTED;
\r
315 send_opt(telnet, o_echo.send, o_echo.option);
\r
317 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
\r
318 telnet->opt_states[o_we_sga.index] = REQUESTED;
\r
319 send_opt(telnet, o_we_sga.send, o_we_sga.option);
\r
321 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
\r
322 telnet->opt_states[o_they_sga.index] = REQUESTED;
\r
323 send_opt(telnet, o_they_sga.send, o_they_sga.option);
\r
325 telnet->activated = TRUE;
\r
329 static void activate_option(Telnet telnet, const struct Opt *o)
\r
331 if (o->send == WILL && o->option == TELOPT_NAWS)
\r
332 telnet_size(telnet, telnet->term_width, telnet->term_height);
\r
333 if (o->send == WILL &&
\r
334 (o->option == TELOPT_NEW_ENVIRON ||
\r
335 o->option == TELOPT_OLD_ENVIRON)) {
\r
337 * We may only have one kind of ENVIRON going at a time.
\r
338 * This is a hack, but who cares.
\r
340 deactivate_option(telnet, o->option ==
\r
341 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
\r
343 option_side_effects(telnet, o, 1);
\r
346 static void refused_option(Telnet telnet, const struct Opt *o)
\r
348 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
\r
349 telnet->opt_states[o_oenv.index] == INACTIVE) {
\r
350 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
\r
351 telnet->opt_states[o_oenv.index] = REQUESTED;
\r
353 option_side_effects(telnet, o, 0);
\r
356 static void proc_rec_opt(Telnet telnet, int cmd, int option)
\r
358 const struct Opt *const *o;
\r
360 log_option(telnet, "server", cmd, option);
\r
361 for (o = opts; *o; o++) {
\r
362 if ((*o)->option == option && (*o)->ack == cmd) {
\r
363 switch (telnet->opt_states[(*o)->index]) {
\r
365 telnet->opt_states[(*o)->index] = ACTIVE;
\r
366 activate_option(telnet, *o);
\r
371 telnet->opt_states[(*o)->index] = ACTIVE;
\r
372 send_opt(telnet, (*o)->send, option);
\r
373 activate_option(telnet, *o);
\r
375 case REALLY_INACTIVE:
\r
376 send_opt(telnet, (*o)->nsend, option);
\r
380 } else if ((*o)->option == option && (*o)->nak == cmd) {
\r
381 switch (telnet->opt_states[(*o)->index]) {
\r
383 telnet->opt_states[(*o)->index] = INACTIVE;
\r
384 refused_option(telnet, *o);
\r
387 telnet->opt_states[(*o)->index] = INACTIVE;
\r
388 send_opt(telnet, (*o)->nsend, option);
\r
389 option_side_effects(telnet, *o, 0);
\r
392 case REALLY_INACTIVE:
\r
399 * If we reach here, the option was one we weren't prepared to
\r
400 * cope with. If the request was positive (WILL or DO), we send
\r
401 * a negative ack to indicate refusal. If the request was
\r
402 * negative (WONT / DONT), we must do nothing.
\r
404 if (cmd == WILL || cmd == DO)
\r
405 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
\r
408 static void process_subneg(Telnet telnet)
\r
410 unsigned char b[2048], *p, *q;
\r
414 switch (telnet->sb_opt) {
\r
415 case TELOPT_TSPEED:
\r
416 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
\r
420 b[2] = TELOPT_TSPEED;
\r
422 strcpy((char *)(b + 4), telnet->cfg.termspeed);
\r
423 n = 4 + strlen(telnet->cfg.termspeed);
\r
426 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2);
\r
427 logevent(telnet->frontend, "server:\tSB TSPEED SEND");
\r
428 logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed);
\r
429 logevent(telnet->frontend, logbuf);
\r
432 logevent(telnet->frontend, "server:\tSB TSPEED <something weird>");
\r
435 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
\r
439 b[2] = TELOPT_TTYPE;
\r
441 for (n = 0; telnet->cfg.termtype[n]; n++)
\r
442 b[n + 4] = (telnet->cfg.termtype[n] >= 'a'
\r
443 && telnet->cfg.termtype[n] <=
\r
444 'z' ? telnet->cfg.termtype[n] + 'A' -
\r
445 'a' : telnet->cfg.termtype[n]);
\r
448 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6);
\r
450 logevent(telnet->frontend, "server:\tSB TTYPE SEND");
\r
451 logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4);
\r
452 logevent(telnet->frontend, logbuf);
\r
455 logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n");
\r
457 case TELOPT_OLD_ENVIRON:
\r
458 case TELOPT_NEW_ENVIRON:
\r
459 p = telnet->sb_buf;
\r
460 q = p + telnet->sb_len;
\r
461 if (p < q && *p == TELQUAL_SEND) {
\r
464 logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt));
\r
465 logevent(telnet->frontend, logbuf);
\r
467 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
\r
468 if (telnet->cfg.rfc_environ) {
\r
476 * Try to guess the sense of VAR and VALUE.
\r
479 if (*p == RFC_VAR) {
\r
482 } else if (*p == BSD_VAR) {
\r
490 * With NEW_ENVIRON, the sense of VAR and VALUE
\r
498 b[2] = telnet->sb_opt;
\r
501 e = telnet->cfg.environmt;
\r
504 while (*e && *e != '\t')
\r
513 if (*telnet->cfg.username) {
\r
520 e = telnet->cfg.username;
\r
526 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
\r
527 logbuf = dupprintf("client:\tSB %s IS %s%s%s%s",
\r
528 telopt(telnet->sb_opt),
\r
529 *telnet->cfg.username ? "USER=" : "",
\r
530 telnet->cfg.username,
\r
531 *telnet->cfg.username ? " " : "",
\r
532 n == 6 ? "<nothing>" :
\r
533 (*telnet->cfg.environmt ? "<stuff>" : ""));
\r
534 logevent(telnet->frontend, logbuf);
\r
541 static void do_telnet_read(Telnet telnet, char *buf, int len)
\r
543 char *outbuf = NULL;
\r
544 int outbuflen = 0, outbufsize = 0;
\r
546 #define ADDTOBUF(c) do { \
\r
547 if (outbuflen >= outbufsize) { \
\r
548 outbufsize = outbuflen + 256; \
\r
549 outbuf = sresize(outbuf, outbufsize, char); \
\r
551 outbuf[outbuflen++] = (c); \
\r
555 int c = (unsigned char) *buf++;
\r
557 switch (telnet->state) {
\r
560 if (c == NUL && telnet->state == SEENCR)
\r
561 telnet->state = TOP_LEVEL;
\r
563 telnet->state = SEENIAC;
\r
565 if (!telnet->in_synch)
\r
569 /* I can't get the F***ing winsock to insert the urgent IAC
\r
570 * into the right position! Even with SO_OOBINLINE it gives
\r
571 * it to recv too soon. And of course the DM byte (that
\r
572 * arrives in the same packet!) appears several K later!!
\r
574 * Oh well, we do get the DM in the right place so I'll
\r
575 * just stop hiding on the next 0xf2 and hope for the best.
\r
578 telnet->in_synch = 0;
\r
580 if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
\r
581 telnet->state = SEENCR;
\r
583 telnet->state = TOP_LEVEL;
\r
588 telnet->state = SEENDO;
\r
589 else if (c == DONT)
\r
590 telnet->state = SEENDONT;
\r
591 else if (c == WILL)
\r
592 telnet->state = SEENWILL;
\r
593 else if (c == WONT)
\r
594 telnet->state = SEENWONT;
\r
596 telnet->state = SEENSB;
\r
597 else if (c == DM) {
\r
598 telnet->in_synch = 0;
\r
599 telnet->state = TOP_LEVEL;
\r
601 /* ignore everything else; print it if it's IAC */
\r
605 telnet->state = TOP_LEVEL;
\r
609 proc_rec_opt(telnet, WILL, c);
\r
610 telnet->state = TOP_LEVEL;
\r
613 proc_rec_opt(telnet, WONT, c);
\r
614 telnet->state = TOP_LEVEL;
\r
617 proc_rec_opt(telnet, DO, c);
\r
618 telnet->state = TOP_LEVEL;
\r
621 proc_rec_opt(telnet, DONT, c);
\r
622 telnet->state = TOP_LEVEL;
\r
625 telnet->sb_opt = c;
\r
626 telnet->sb_len = 0;
\r
627 telnet->state = SUBNEGOT;
\r
631 telnet->state = SUBNEG_IAC;
\r
634 if (telnet->sb_len >= telnet->sb_size) {
\r
635 telnet->sb_size += SB_DELTA;
\r
636 telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
\r
639 telnet->sb_buf[telnet->sb_len++] = c;
\r
640 telnet->state = SUBNEGOT; /* in case we came here by goto */
\r
645 goto subneg_addchar; /* yes, it's a hack, I know, but... */
\r
647 process_subneg(telnet);
\r
648 telnet->state = TOP_LEVEL;
\r
655 c_write(telnet, outbuf, outbuflen);
\r
659 static void telnet_log(Plug plug, int type, SockAddr addr, int port,
\r
660 const char *error_msg, int error_code)
\r
662 Telnet telnet = (Telnet) plug;
\r
663 char addrbuf[256], *msg;
\r
665 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
668 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
670 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
672 logevent(telnet->frontend, msg);
\r
675 static int telnet_closing(Plug plug, const char *error_msg, int error_code,
\r
678 Telnet telnet = (Telnet) plug;
\r
681 sk_close(telnet->s);
\r
683 notify_remote_exit(telnet->frontend);
\r
686 logevent(telnet->frontend, error_msg);
\r
687 connection_fatal(telnet->frontend, "%s", error_msg);
\r
689 /* Otherwise, the remote side closed the connection normally. */
\r
693 static int telnet_receive(Plug plug, int urgent, char *data, int len)
\r
695 Telnet telnet = (Telnet) plug;
\r
697 telnet->in_synch = TRUE;
\r
698 do_telnet_read(telnet, data, len);
\r
702 static void telnet_sent(Plug plug, int bufsize)
\r
704 Telnet telnet = (Telnet) plug;
\r
705 telnet->bufsize = bufsize;
\r
709 * Called to set up the Telnet connection.
\r
711 * Returns an error message, or NULL on success.
\r
713 * Also places the canonical host name into `realhost'. It must be
\r
714 * freed by the caller.
\r
716 static const char *telnet_init(void *frontend_handle, void **backend_handle,
\r
718 char *host, int port, char **realhost,
\r
719 int nodelay, int keepalive)
\r
721 static const struct plug_function_table fn_table = {
\r
731 telnet = snew(struct telnet_tag);
\r
732 telnet->fn = &fn_table;
\r
733 telnet->cfg = *cfg; /* STRUCTURE COPY */
\r
735 telnet->echoing = TRUE;
\r
736 telnet->editing = TRUE;
\r
737 telnet->activated = FALSE;
\r
738 telnet->sb_buf = NULL;
\r
739 telnet->sb_size = 0;
\r
740 telnet->frontend = frontend_handle;
\r
741 telnet->term_width = telnet->cfg.width;
\r
742 telnet->term_height = telnet->cfg.height;
\r
743 telnet->state = TOP_LEVEL;
\r
744 telnet->ldisc = NULL;
\r
745 telnet->pinger = NULL;
\r
746 *backend_handle = telnet;
\r
749 * Try to find host.
\r
753 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
754 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
755 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
757 logevent(telnet->frontend, buf);
\r
760 addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily);
\r
761 if ((err = sk_addr_error(addr)) != NULL) {
\r
762 sk_addr_free(addr);
\r
767 port = 23; /* default telnet port */
\r
772 telnet->s = new_connection(addr, *realhost, port, 0, 1,
\r
773 nodelay, keepalive, (Plug) telnet, &telnet->cfg);
\r
774 if ((err = sk_socket_error(telnet->s)) != NULL)
\r
777 telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet);
\r
780 * Initialise option states.
\r
782 if (telnet->cfg.passive_telnet) {
\r
783 const struct Opt *const *o;
\r
785 for (o = opts; *o; o++)
\r
786 telnet->opt_states[(*o)->index] = INACTIVE;
\r
788 const struct Opt *const *o;
\r
790 for (o = opts; *o; o++) {
\r
791 telnet->opt_states[(*o)->index] = (*o)->initial_state;
\r
792 if (telnet->opt_states[(*o)->index] == REQUESTED)
\r
793 send_opt(telnet, (*o)->send, (*o)->option);
\r
795 telnet->activated = TRUE;
\r
799 * Set up SYNCH state.
\r
801 telnet->in_synch = FALSE;
\r
804 * We can send special commands from the start.
\r
806 update_specials_menu(telnet->frontend);
\r
811 static void telnet_free(void *handle)
\r
813 Telnet telnet = (Telnet) handle;
\r
815 sfree(telnet->sb_buf);
\r
817 sk_close(telnet->s);
\r
818 if (telnet->pinger)
\r
819 pinger_free(telnet->pinger);
\r
823 * Reconfigure the Telnet backend. There's no immediate action
\r
824 * necessary, in this backend: we just save the fresh config for
\r
825 * any subsequent negotiations.
\r
827 static void telnet_reconfig(void *handle, Config *cfg)
\r
829 Telnet telnet = (Telnet) handle;
\r
830 pinger_reconfig(telnet->pinger, &telnet->cfg, cfg);
\r
831 telnet->cfg = *cfg; /* STRUCTURE COPY */
\r
835 * Called to send data down the Telnet connection.
\r
837 static int telnet_send(void *handle, char *buf, int len)
\r
839 Telnet telnet = (Telnet) handle;
\r
840 unsigned char *p, *end;
\r
841 static const unsigned char iac[2] = { IAC, IAC };
\r
842 static const unsigned char cr[2] = { CR, NUL };
\r
844 static const unsigned char nl[2] = { CR, LF };
\r
847 if (telnet->s == NULL)
\r
850 p = (unsigned char *)buf;
\r
851 end = (unsigned char *)(buf + len);
\r
853 unsigned char *q = p;
\r
855 while (p < end && iswritable(*p))
\r
857 telnet->bufsize = sk_write(telnet->s, (char *)q, p - q);
\r
859 while (p < end && !iswritable(*p)) {
\r
861 sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2);
\r
866 return telnet->bufsize;
\r
870 * Called to query the current socket sendability status.
\r
872 static int telnet_sendbuffer(void *handle)
\r
874 Telnet telnet = (Telnet) handle;
\r
875 return telnet->bufsize;
\r
879 * Called to set the size of the window from Telnet's POV.
\r
881 static void telnet_size(void *handle, int width, int height)
\r
883 Telnet telnet = (Telnet) handle;
\r
884 unsigned char b[24];
\r
888 telnet->term_width = width;
\r
889 telnet->term_height = height;
\r
891 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
\r
896 b[n++] = TELOPT_NAWS;
\r
897 b[n++] = telnet->term_width >> 8;
\r
898 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
899 b[n++] = telnet->term_width & 0xFF;
\r
900 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
901 b[n++] = telnet->term_height >> 8;
\r
902 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
903 b[n++] = telnet->term_height & 0xFF;
\r
904 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
907 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
\r
908 logbuf = dupprintf("client:\tSB NAWS %d,%d",
\r
909 telnet->term_width, telnet->term_height);
\r
910 logevent(telnet->frontend, logbuf);
\r
915 * Send Telnet special codes.
\r
917 static void telnet_special(void *handle, Telnet_Special code)
\r
919 Telnet telnet = (Telnet) handle;
\r
920 unsigned char b[2];
\r
922 if (telnet->s == NULL)
\r
929 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
933 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
937 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
941 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
945 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
949 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
953 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
957 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
961 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
965 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
969 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
973 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
976 /* In BINARY mode, CR-LF becomes just CR -
\r
977 * and without the NUL suffix too. */
\r
978 if (telnet->opt_states[o_we_bin.index] == ACTIVE)
\r
979 telnet->bufsize = sk_write(telnet->s, "\r", 1);
\r
981 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
\r
985 telnet->bufsize = sk_write(telnet->s, (char *)b, 1);
\r
986 telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1);
\r
989 if (telnet->opt_states[o_echo.index] == INACTIVE ||
\r
990 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
\r
991 telnet->opt_states[o_echo.index] = REQUESTED;
\r
992 send_opt(telnet, o_echo.send, o_echo.option);
\r
996 if (telnet->opt_states[o_echo.index] == ACTIVE) {
\r
997 telnet->opt_states[o_echo.index] = REQUESTED;
\r
998 send_opt(telnet, o_echo.nsend, o_echo.option);
\r
1002 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
\r
1004 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
1008 break; /* never heard of it */
\r
1012 static const struct telnet_special *telnet_get_specials(void *handle)
\r
1014 static const struct telnet_special specials[] = {
\r
1015 {"Are You There", TS_AYT},
\r
1016 {"Break", TS_BRK},
\r
1017 {"Synch", TS_SYNCH},
\r
1018 {"Erase Character", TS_EC},
\r
1019 {"Erase Line", TS_EL},
\r
1020 {"Go Ahead", TS_GA},
\r
1021 {"No Operation", TS_NOP},
\r
1023 {"Abort Process", TS_ABORT},
\r
1024 {"Abort Output", TS_AO},
\r
1025 {"Interrupt Process", TS_IP},
\r
1026 {"Suspend Process", TS_SUSP},
\r
1028 {"End Of Record", TS_EOR},
\r
1029 {"End Of File", TS_EOF},
\r
1030 {NULL, TS_EXITMENU}
\r
1035 static int telnet_connected(void *handle)
\r
1037 Telnet telnet = (Telnet) handle;
\r
1038 return telnet->s != NULL;
\r
1041 static int telnet_sendok(void *handle)
\r
1043 /* Telnet telnet = (Telnet) handle; */
\r
1047 static void telnet_unthrottle(void *handle, int backlog)
\r
1049 Telnet telnet = (Telnet) handle;
\r
1050 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
\r
1053 static int telnet_ldisc(void *handle, int option)
\r
1055 Telnet telnet = (Telnet) handle;
\r
1056 if (option == LD_ECHO)
\r
1057 return telnet->echoing;
\r
1058 if (option == LD_EDIT)
\r
1059 return telnet->editing;
\r
1063 static void telnet_provide_ldisc(void *handle, void *ldisc)
\r
1065 Telnet telnet = (Telnet) handle;
\r
1066 telnet->ldisc = ldisc;
\r
1069 static void telnet_provide_logctx(void *handle, void *logctx)
\r
1071 /* This is a stub. */
\r
1074 static int telnet_exitcode(void *handle)
\r
1076 Telnet telnet = (Telnet) handle;
\r
1077 if (telnet->s != NULL)
\r
1078 return -1; /* still connected */
\r
1080 /* Telnet doesn't transmit exit codes back to the client */
\r
1085 * cfg_info for Telnet does nothing at all.
\r
1087 static int telnet_cfg_info(void *handle)
\r
1092 Backend telnet_backend = {
\r
1097 telnet_sendbuffer,
\r
1100 telnet_get_specials,
\r
1105 telnet_provide_ldisc,
\r
1106 telnet_provide_logctx,
\r
1107 telnet_unthrottle,
\r