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 TELOPTS(X) \
\r
40 X(BINARY, 0) /* 8-bit data path */ \
\r
41 X(ECHO, 1) /* echo */ \
\r
42 X(RCP, 2) /* prepare to reconnect */ \
\r
43 X(SGA, 3) /* suppress go ahead */ \
\r
44 X(NAMS, 4) /* approximate message size */ \
\r
45 X(STATUS, 5) /* give status */ \
\r
46 X(TM, 6) /* timing mark */ \
\r
47 X(RCTE, 7) /* remote controlled transmission and echo */ \
\r
48 X(NAOL, 8) /* negotiate about output line width */ \
\r
49 X(NAOP, 9) /* negotiate about output page size */ \
\r
50 X(NAOCRD, 10) /* negotiate about CR disposition */ \
\r
51 X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \
\r
52 X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \
\r
53 X(NAOFFD, 13) /* negotiate about formfeed disposition */ \
\r
54 X(NAOVTS, 14) /* negotiate about vertical tab stops */ \
\r
55 X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \
\r
56 X(NAOLFD, 16) /* negotiate about output LF disposition */ \
\r
57 X(XASCII, 17) /* extended ascic character set */ \
\r
58 X(LOGOUT, 18) /* force logout */ \
\r
59 X(BM, 19) /* byte macro */ \
\r
60 X(DET, 20) /* data entry terminal */ \
\r
61 X(SUPDUP, 21) /* supdup protocol */ \
\r
62 X(SUPDUPOUTPUT, 22) /* supdup output */ \
\r
63 X(SNDLOC, 23) /* send location */ \
\r
64 X(TTYPE, 24) /* terminal type */ \
\r
65 X(EOR, 25) /* end or record */ \
\r
66 X(TUID, 26) /* TACACS user identification */ \
\r
67 X(OUTMRK, 27) /* output marking */ \
\r
68 X(TTYLOC, 28) /* terminal location number */ \
\r
69 X(3270REGIME, 29) /* 3270 regime */ \
\r
70 X(X3PAD, 30) /* X.3 PAD */ \
\r
71 X(NAWS, 31) /* window size */ \
\r
72 X(TSPEED, 32) /* terminal speed */ \
\r
73 X(LFLOW, 33) /* remote flow control */ \
\r
74 X(LINEMODE, 34) /* Linemode option */ \
\r
75 X(XDISPLOC, 35) /* X Display Location */ \
\r
76 X(OLD_ENVIRON, 36) /* Old - Environment variables */ \
\r
77 X(AUTHENTICATION, 37) /* Authenticate */ \
\r
78 X(ENCRYPT, 38) /* Encryption option */ \
\r
79 X(NEW_ENVIRON, 39) /* New - Environment variables */ \
\r
80 X(TN3270E, 40) /* TN3270 enhancements */ \
\r
82 X(CHARSET, 42) /* Character set */ \
\r
83 X(RSP, 43) /* Remote serial port */ \
\r
84 X(COM_PORT_OPTION, 44) /* Com port control */ \
\r
85 X(SLE, 45) /* Suppress local echo */ \
\r
86 X(STARTTLS, 46) /* Start TLS */ \
\r
87 X(KERMIT, 47) /* Automatic Kermit file transfer */ \
\r
90 X(PRAGMA_LOGON, 138) \
\r
91 X(SSPI_LOGON, 139) \
\r
92 X(PRAGMA_HEARTBEAT, 140) \
\r
93 X(EXOPL, 255) /* extended-options-list */
\r
95 #define telnet_enum(x,y) TELOPT_##x = y,
\r
96 enum { TELOPTS(telnet_enum) dummy=0 };
\r
99 #define TELQUAL_IS 0 /* option is... */
\r
100 #define TELQUAL_SEND 1 /* send option */
\r
101 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
\r
103 #define BSD_VALUE 0
\r
105 #define RFC_VALUE 1
\r
111 #define iswritable(x) \
\r
113 (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
\r
115 static char *telopt(int opt)
\r
117 #define telnet_str(x,y) case TELOPT_##x: return #x;
\r
119 TELOPTS(telnet_str)
\r
121 return "<unknown>";
\r
126 static void telnet_size(void *handle, int width, int height);
\r
129 int send; /* what we initially send */
\r
130 int nsend; /* -ve send if requested to stop it */
\r
131 int ack, nak; /* +ve and -ve acknowledgements */
\r
132 int option; /* the option code */
\r
133 int index; /* index into telnet->opt_states[] */
\r
135 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
\r
153 static const struct Opt o_naws =
\r
154 { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
\r
155 static const struct Opt o_tspeed =
\r
156 { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
\r
157 static const struct Opt o_ttype =
\r
158 { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
\r
159 static const struct Opt o_oenv =
\r
160 { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE };
\r
161 static const struct Opt o_nenv =
\r
162 { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED };
\r
163 static const struct Opt o_echo =
\r
164 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
\r
165 static const struct Opt o_we_sga =
\r
166 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
\r
167 static const struct Opt o_they_sga =
\r
168 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
\r
169 static const struct Opt o_we_bin =
\r
170 { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
\r
171 static const struct Opt o_they_bin =
\r
172 { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
\r
174 static const struct Opt *const opts[] = {
\r
175 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
\r
176 &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
\r
179 typedef struct telnet_tag {
\r
180 const struct plug_function_table *fn;
\r
181 /* the above field _must_ be first in the structure */
\r
187 int term_width, term_height;
\r
189 int opt_states[NUM_OPTS];
\r
191 int echoing, editing;
\r
195 int sb_opt, sb_len;
\r
196 unsigned char *sb_buf;
\r
200 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
\r
201 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
\r
209 #define TELNET_MAX_BACKLOG 4096
\r
211 #define SB_DELTA 1024
\r
213 static void c_write(Telnet telnet, char *buf, int len)
\r
216 backlog = from_backend(telnet->frontend, 0, buf, len);
\r
217 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
\r
220 static void log_option(Telnet telnet, char *sender, int cmd, int option)
\r
224 * The strange-looking "<?""?>" below is there to avoid a
\r
225 * trigraph - a double question mark followed by > maps to a
\r
226 * closing brace character!
\r
228 buf = dupprintf("%s:\t%s %s", sender,
\r
229 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
\r
230 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"),
\r
232 logevent(telnet->frontend, buf);
\r
236 static void send_opt(Telnet telnet, int cmd, int option)
\r
238 unsigned char b[3];
\r
243 telnet->bufsize = sk_write(telnet->s, (char *)b, 3);
\r
244 log_option(telnet, "client", cmd, option);
\r
247 static void deactivate_option(Telnet telnet, const struct Opt *o)
\r
249 if (telnet->opt_states[o->index] == REQUESTED ||
\r
250 telnet->opt_states[o->index] == ACTIVE)
\r
251 send_opt(telnet, o->nsend, o->option);
\r
252 telnet->opt_states[o->index] = REALLY_INACTIVE;
\r
256 * Generate side effects of enabling or disabling an option.
\r
258 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
\r
260 if (o->option == TELOPT_ECHO && o->send == DO)
\r
261 telnet->echoing = !enabled;
\r
262 else if (o->option == TELOPT_SGA && o->send == DO)
\r
263 telnet->editing = !enabled;
\r
264 if (telnet->ldisc) /* cause ldisc to notice the change */
\r
265 ldisc_send(telnet->ldisc, NULL, 0, 0);
\r
267 /* Ensure we get the minimum options */
\r
268 if (!telnet->activated) {
\r
269 if (telnet->opt_states[o_echo.index] == INACTIVE) {
\r
270 telnet->opt_states[o_echo.index] = REQUESTED;
\r
271 send_opt(telnet, o_echo.send, o_echo.option);
\r
273 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
\r
274 telnet->opt_states[o_we_sga.index] = REQUESTED;
\r
275 send_opt(telnet, o_we_sga.send, o_we_sga.option);
\r
277 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
\r
278 telnet->opt_states[o_they_sga.index] = REQUESTED;
\r
279 send_opt(telnet, o_they_sga.send, o_they_sga.option);
\r
281 telnet->activated = TRUE;
\r
285 static void activate_option(Telnet telnet, const struct Opt *o)
\r
287 if (o->send == WILL && o->option == TELOPT_NAWS)
\r
288 telnet_size(telnet, telnet->term_width, telnet->term_height);
\r
289 if (o->send == WILL &&
\r
290 (o->option == TELOPT_NEW_ENVIRON ||
\r
291 o->option == TELOPT_OLD_ENVIRON)) {
\r
293 * We may only have one kind of ENVIRON going at a time.
\r
294 * This is a hack, but who cares.
\r
296 deactivate_option(telnet, o->option ==
\r
297 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
\r
299 option_side_effects(telnet, o, 1);
\r
302 static void refused_option(Telnet telnet, const struct Opt *o)
\r
304 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
\r
305 telnet->opt_states[o_oenv.index] == INACTIVE) {
\r
306 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
\r
307 telnet->opt_states[o_oenv.index] = REQUESTED;
\r
309 option_side_effects(telnet, o, 0);
\r
312 static void proc_rec_opt(Telnet telnet, int cmd, int option)
\r
314 const struct Opt *const *o;
\r
316 log_option(telnet, "server", cmd, option);
\r
317 for (o = opts; *o; o++) {
\r
318 if ((*o)->option == option && (*o)->ack == cmd) {
\r
319 switch (telnet->opt_states[(*o)->index]) {
\r
321 telnet->opt_states[(*o)->index] = ACTIVE;
\r
322 activate_option(telnet, *o);
\r
327 telnet->opt_states[(*o)->index] = ACTIVE;
\r
328 send_opt(telnet, (*o)->send, option);
\r
329 activate_option(telnet, *o);
\r
331 case REALLY_INACTIVE:
\r
332 send_opt(telnet, (*o)->nsend, option);
\r
336 } else if ((*o)->option == option && (*o)->nak == cmd) {
\r
337 switch (telnet->opt_states[(*o)->index]) {
\r
339 telnet->opt_states[(*o)->index] = INACTIVE;
\r
340 refused_option(telnet, *o);
\r
343 telnet->opt_states[(*o)->index] = INACTIVE;
\r
344 send_opt(telnet, (*o)->nsend, option);
\r
345 option_side_effects(telnet, *o, 0);
\r
348 case REALLY_INACTIVE:
\r
355 * If we reach here, the option was one we weren't prepared to
\r
356 * cope with. If the request was positive (WILL or DO), we send
\r
357 * a negative ack to indicate refusal. If the request was
\r
358 * negative (WONT / DONT), we must do nothing.
\r
360 if (cmd == WILL || cmd == DO)
\r
361 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
\r
364 static void process_subneg(Telnet telnet)
\r
366 unsigned char b[2048], *p, *q;
\r
370 switch (telnet->sb_opt) {
\r
371 case TELOPT_TSPEED:
\r
372 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
\r
376 b[2] = TELOPT_TSPEED;
\r
378 strcpy((char *)(b + 4), telnet->cfg.termspeed);
\r
379 n = 4 + strlen(telnet->cfg.termspeed);
\r
382 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2);
\r
383 logevent(telnet->frontend, "server:\tSB TSPEED SEND");
\r
384 logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed);
\r
385 logevent(telnet->frontend, logbuf);
\r
388 logevent(telnet->frontend, "server:\tSB TSPEED <something weird>");
\r
391 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
\r
395 b[2] = TELOPT_TTYPE;
\r
397 for (n = 0; telnet->cfg.termtype[n]; n++)
\r
398 b[n + 4] = (telnet->cfg.termtype[n] >= 'a'
\r
399 && telnet->cfg.termtype[n] <=
\r
400 'z' ? telnet->cfg.termtype[n] + 'A' -
\r
401 'a' : telnet->cfg.termtype[n]);
\r
404 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6);
\r
406 logevent(telnet->frontend, "server:\tSB TTYPE SEND");
\r
407 logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4);
\r
408 logevent(telnet->frontend, logbuf);
\r
411 logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n");
\r
413 case TELOPT_OLD_ENVIRON:
\r
414 case TELOPT_NEW_ENVIRON:
\r
415 p = telnet->sb_buf;
\r
416 q = p + telnet->sb_len;
\r
417 if (p < q && *p == TELQUAL_SEND) {
\r
420 logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt));
\r
421 logevent(telnet->frontend, logbuf);
\r
423 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
\r
424 if (telnet->cfg.rfc_environ) {
\r
432 * Try to guess the sense of VAR and VALUE.
\r
435 if (*p == RFC_VAR) {
\r
438 } else if (*p == BSD_VAR) {
\r
446 * With NEW_ENVIRON, the sense of VAR and VALUE
\r
454 b[2] = telnet->sb_opt;
\r
457 e = telnet->cfg.environmt;
\r
460 while (*e && *e != '\t')
\r
470 char user[sizeof(telnet->cfg.username)];
\r
471 (void) get_remote_username(&telnet->cfg, user, sizeof(user));
\r
485 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
\r
486 logbuf = dupprintf("client:\tSB %s IS %s%s%s%s",
\r
487 telopt(telnet->sb_opt),
\r
488 *user ? "USER=" : "",
\r
491 n == 6 ? "<nothing>" :
\r
492 (*telnet->cfg.environmt ? "<stuff>" : ""));
\r
493 logevent(telnet->frontend, logbuf);
\r
501 static void do_telnet_read(Telnet telnet, char *buf, int len)
\r
503 char *outbuf = NULL;
\r
504 int outbuflen = 0, outbufsize = 0;
\r
506 #define ADDTOBUF(c) do { \
\r
507 if (outbuflen >= outbufsize) { \
\r
508 outbufsize = outbuflen + 256; \
\r
509 outbuf = sresize(outbuf, outbufsize, char); \
\r
511 outbuf[outbuflen++] = (c); \
\r
515 int c = (unsigned char) *buf++;
\r
517 switch (telnet->state) {
\r
520 if (c == NUL && telnet->state == SEENCR)
\r
521 telnet->state = TOP_LEVEL;
\r
523 telnet->state = SEENIAC;
\r
525 if (!telnet->in_synch)
\r
529 /* I can't get the F***ing winsock to insert the urgent IAC
\r
530 * into the right position! Even with SO_OOBINLINE it gives
\r
531 * it to recv too soon. And of course the DM byte (that
\r
532 * arrives in the same packet!) appears several K later!!
\r
534 * Oh well, we do get the DM in the right place so I'll
\r
535 * just stop hiding on the next 0xf2 and hope for the best.
\r
538 telnet->in_synch = 0;
\r
540 if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
\r
541 telnet->state = SEENCR;
\r
543 telnet->state = TOP_LEVEL;
\r
548 telnet->state = SEENDO;
\r
549 else if (c == DONT)
\r
550 telnet->state = SEENDONT;
\r
551 else if (c == WILL)
\r
552 telnet->state = SEENWILL;
\r
553 else if (c == WONT)
\r
554 telnet->state = SEENWONT;
\r
556 telnet->state = SEENSB;
\r
557 else if (c == DM) {
\r
558 telnet->in_synch = 0;
\r
559 telnet->state = TOP_LEVEL;
\r
561 /* ignore everything else; print it if it's IAC */
\r
565 telnet->state = TOP_LEVEL;
\r
569 proc_rec_opt(telnet, WILL, c);
\r
570 telnet->state = TOP_LEVEL;
\r
573 proc_rec_opt(telnet, WONT, c);
\r
574 telnet->state = TOP_LEVEL;
\r
577 proc_rec_opt(telnet, DO, c);
\r
578 telnet->state = TOP_LEVEL;
\r
581 proc_rec_opt(telnet, DONT, c);
\r
582 telnet->state = TOP_LEVEL;
\r
585 telnet->sb_opt = c;
\r
586 telnet->sb_len = 0;
\r
587 telnet->state = SUBNEGOT;
\r
591 telnet->state = SUBNEG_IAC;
\r
594 if (telnet->sb_len >= telnet->sb_size) {
\r
595 telnet->sb_size += SB_DELTA;
\r
596 telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
\r
599 telnet->sb_buf[telnet->sb_len++] = c;
\r
600 telnet->state = SUBNEGOT; /* in case we came here by goto */
\r
605 goto subneg_addchar; /* yes, it's a hack, I know, but... */
\r
607 process_subneg(telnet);
\r
608 telnet->state = TOP_LEVEL;
\r
615 c_write(telnet, outbuf, outbuflen);
\r
619 static void telnet_log(Plug plug, int type, SockAddr addr, int port,
\r
620 const char *error_msg, int error_code)
\r
622 Telnet telnet = (Telnet) plug;
\r
623 char addrbuf[256], *msg;
\r
625 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
628 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
630 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
632 logevent(telnet->frontend, msg);
\r
635 static int telnet_closing(Plug plug, const char *error_msg, int error_code,
\r
638 Telnet telnet = (Telnet) plug;
\r
641 sk_close(telnet->s);
\r
643 notify_remote_exit(telnet->frontend);
\r
646 logevent(telnet->frontend, error_msg);
\r
647 connection_fatal(telnet->frontend, "%s", error_msg);
\r
649 /* Otherwise, the remote side closed the connection normally. */
\r
653 static int telnet_receive(Plug plug, int urgent, char *data, int len)
\r
655 Telnet telnet = (Telnet) plug;
\r
657 telnet->in_synch = TRUE;
\r
658 do_telnet_read(telnet, data, len);
\r
662 static void telnet_sent(Plug plug, int bufsize)
\r
664 Telnet telnet = (Telnet) plug;
\r
665 telnet->bufsize = bufsize;
\r
669 * Called to set up the Telnet connection.
\r
671 * Returns an error message, or NULL on success.
\r
673 * Also places the canonical host name into `realhost'. It must be
\r
674 * freed by the caller.
\r
676 static const char *telnet_init(void *frontend_handle, void **backend_handle,
\r
678 char *host, int port, char **realhost,
\r
679 int nodelay, int keepalive)
\r
681 static const struct plug_function_table fn_table = {
\r
691 telnet = snew(struct telnet_tag);
\r
692 telnet->fn = &fn_table;
\r
693 telnet->cfg = *cfg; /* STRUCTURE COPY */
\r
695 telnet->echoing = TRUE;
\r
696 telnet->editing = TRUE;
\r
697 telnet->activated = FALSE;
\r
698 telnet->sb_buf = NULL;
\r
699 telnet->sb_size = 0;
\r
700 telnet->frontend = frontend_handle;
\r
701 telnet->term_width = telnet->cfg.width;
\r
702 telnet->term_height = telnet->cfg.height;
\r
703 telnet->state = TOP_LEVEL;
\r
704 telnet->ldisc = NULL;
\r
705 telnet->pinger = NULL;
\r
706 *backend_handle = telnet;
\r
709 * Try to find host.
\r
713 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
714 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
715 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
717 logevent(telnet->frontend, buf);
\r
720 addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily);
\r
721 if ((err = sk_addr_error(addr)) != NULL) {
\r
722 sk_addr_free(addr);
\r
727 port = 23; /* default telnet port */
\r
732 telnet->s = new_connection(addr, *realhost, port, 0, 1,
\r
733 nodelay, keepalive, (Plug) telnet, &telnet->cfg);
\r
734 if ((err = sk_socket_error(telnet->s)) != NULL)
\r
737 telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet);
\r
740 * Initialise option states.
\r
742 if (telnet->cfg.passive_telnet) {
\r
743 const struct Opt *const *o;
\r
745 for (o = opts; *o; o++)
\r
746 telnet->opt_states[(*o)->index] = INACTIVE;
\r
748 const struct Opt *const *o;
\r
750 for (o = opts; *o; o++) {
\r
751 telnet->opt_states[(*o)->index] = (*o)->initial_state;
\r
752 if (telnet->opt_states[(*o)->index] == REQUESTED)
\r
753 send_opt(telnet, (*o)->send, (*o)->option);
\r
755 telnet->activated = TRUE;
\r
759 * Set up SYNCH state.
\r
761 telnet->in_synch = FALSE;
\r
764 * We can send special commands from the start.
\r
766 update_specials_menu(telnet->frontend);
\r
769 * loghost overrides realhost, if specified.
\r
771 if (*telnet->cfg.loghost) {
\r
775 *realhost = dupstr(telnet->cfg.loghost);
\r
776 colon = strrchr(*realhost, ':');
\r
779 * FIXME: if we ever update this aspect of ssh.c for
\r
780 * IPv6 literal management, this should change in line
\r
790 static void telnet_free(void *handle)
\r
792 Telnet telnet = (Telnet) handle;
\r
794 sfree(telnet->sb_buf);
\r
796 sk_close(telnet->s);
\r
797 if (telnet->pinger)
\r
798 pinger_free(telnet->pinger);
\r
802 * Reconfigure the Telnet backend. There's no immediate action
\r
803 * necessary, in this backend: we just save the fresh config for
\r
804 * any subsequent negotiations.
\r
806 static void telnet_reconfig(void *handle, Config *cfg)
\r
808 Telnet telnet = (Telnet) handle;
\r
809 pinger_reconfig(telnet->pinger, &telnet->cfg, cfg);
\r
810 telnet->cfg = *cfg; /* STRUCTURE COPY */
\r
814 * Called to send data down the Telnet connection.
\r
816 static int telnet_send(void *handle, char *buf, int len)
\r
818 Telnet telnet = (Telnet) handle;
\r
819 unsigned char *p, *end;
\r
820 static const unsigned char iac[2] = { IAC, IAC };
\r
821 static const unsigned char cr[2] = { CR, NUL };
\r
823 static const unsigned char nl[2] = { CR, LF };
\r
826 if (telnet->s == NULL)
\r
829 p = (unsigned char *)buf;
\r
830 end = (unsigned char *)(buf + len);
\r
832 unsigned char *q = p;
\r
834 while (p < end && iswritable(*p))
\r
836 telnet->bufsize = sk_write(telnet->s, (char *)q, p - q);
\r
838 while (p < end && !iswritable(*p)) {
\r
840 sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2);
\r
845 return telnet->bufsize;
\r
849 * Called to query the current socket sendability status.
\r
851 static int telnet_sendbuffer(void *handle)
\r
853 Telnet telnet = (Telnet) handle;
\r
854 return telnet->bufsize;
\r
858 * Called to set the size of the window from Telnet's POV.
\r
860 static void telnet_size(void *handle, int width, int height)
\r
862 Telnet telnet = (Telnet) handle;
\r
863 unsigned char b[24];
\r
867 telnet->term_width = width;
\r
868 telnet->term_height = height;
\r
870 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
\r
875 b[n++] = TELOPT_NAWS;
\r
876 b[n++] = telnet->term_width >> 8;
\r
877 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
878 b[n++] = telnet->term_width & 0xFF;
\r
879 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
880 b[n++] = telnet->term_height >> 8;
\r
881 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
882 b[n++] = telnet->term_height & 0xFF;
\r
883 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
886 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
\r
887 logbuf = dupprintf("client:\tSB NAWS %d,%d",
\r
888 telnet->term_width, telnet->term_height);
\r
889 logevent(telnet->frontend, logbuf);
\r
894 * Send Telnet special codes.
\r
896 static void telnet_special(void *handle, Telnet_Special code)
\r
898 Telnet telnet = (Telnet) handle;
\r
899 unsigned char b[2];
\r
901 if (telnet->s == NULL)
\r
908 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
912 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
916 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
920 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
924 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
928 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
932 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
936 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
940 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
944 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
948 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
952 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
955 /* In BINARY mode, CR-LF becomes just CR -
\r
956 * and without the NUL suffix too. */
\r
957 if (telnet->opt_states[o_we_bin.index] == ACTIVE)
\r
958 telnet->bufsize = sk_write(telnet->s, "\r", 1);
\r
960 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
\r
964 telnet->bufsize = sk_write(telnet->s, (char *)b, 1);
\r
965 telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1);
\r
968 if (telnet->opt_states[o_echo.index] == INACTIVE ||
\r
969 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
\r
970 telnet->opt_states[o_echo.index] = REQUESTED;
\r
971 send_opt(telnet, o_echo.send, o_echo.option);
\r
975 if (telnet->opt_states[o_echo.index] == ACTIVE) {
\r
976 telnet->opt_states[o_echo.index] = REQUESTED;
\r
977 send_opt(telnet, o_echo.nsend, o_echo.option);
\r
981 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
\r
983 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
987 break; /* never heard of it */
\r
991 static const struct telnet_special *telnet_get_specials(void *handle)
\r
993 static const struct telnet_special specials[] = {
\r
994 {"Are You There", TS_AYT},
\r
996 {"Synch", TS_SYNCH},
\r
997 {"Erase Character", TS_EC},
\r
998 {"Erase Line", TS_EL},
\r
999 {"Go Ahead", TS_GA},
\r
1000 {"No Operation", TS_NOP},
\r
1002 {"Abort Process", TS_ABORT},
\r
1003 {"Abort Output", TS_AO},
\r
1004 {"Interrupt Process", TS_IP},
\r
1005 {"Suspend Process", TS_SUSP},
\r
1007 {"End Of Record", TS_EOR},
\r
1008 {"End Of File", TS_EOF},
\r
1009 {NULL, TS_EXITMENU}
\r
1014 static int telnet_connected(void *handle)
\r
1016 Telnet telnet = (Telnet) handle;
\r
1017 return telnet->s != NULL;
\r
1020 static int telnet_sendok(void *handle)
\r
1022 /* Telnet telnet = (Telnet) handle; */
\r
1026 static void telnet_unthrottle(void *handle, int backlog)
\r
1028 Telnet telnet = (Telnet) handle;
\r
1029 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
\r
1032 static int telnet_ldisc(void *handle, int option)
\r
1034 Telnet telnet = (Telnet) handle;
\r
1035 if (option == LD_ECHO)
\r
1036 return telnet->echoing;
\r
1037 if (option == LD_EDIT)
\r
1038 return telnet->editing;
\r
1042 static void telnet_provide_ldisc(void *handle, void *ldisc)
\r
1044 Telnet telnet = (Telnet) handle;
\r
1045 telnet->ldisc = ldisc;
\r
1048 static void telnet_provide_logctx(void *handle, void *logctx)
\r
1050 /* This is a stub. */
\r
1053 static int telnet_exitcode(void *handle)
\r
1055 Telnet telnet = (Telnet) handle;
\r
1056 if (telnet->s != NULL)
\r
1057 return -1; /* still connected */
\r
1059 /* Telnet doesn't transmit exit codes back to the client */
\r
1064 * cfg_info for Telnet does nothing at all.
\r
1066 static int telnet_cfg_info(void *handle)
\r
1071 Backend telnet_backend = {
\r
1076 telnet_sendbuffer,
\r
1079 telnet_get_specials,
\r
1084 telnet_provide_ldisc,
\r
1085 telnet_provide_logctx,
\r
1086 telnet_unthrottle,
\r