18 #define IAC 255 /* interpret as command: */
\r
19 #define DONT 254 /* you are not to use option */
\r
20 #define DO 253 /* please, you use option */
\r
21 #define WONT 252 /* I won't use option */
\r
22 #define WILL 251 /* I will use option */
\r
23 #define SB 250 /* interpret as subnegotiation */
\r
24 #define SE 240 /* end sub negotiation */
\r
26 #define GA 249 /* you may reverse the line */
\r
27 #define EL 248 /* erase the current line */
\r
28 #define EC 247 /* erase the current character */
\r
29 #define AYT 246 /* are you there */
\r
30 #define AO 245 /* abort output--but let prog finish */
\r
31 #define IP 244 /* interrupt process--permanently */
\r
32 #define BREAK 243 /* break */
\r
33 #define DM 242 /* data mark--for connect. cleaning */
\r
34 #define NOP 241 /* nop */
\r
35 #define EOR 239 /* end of record (transparent mode) */
\r
36 #define ABORT 238 /* Abort process */
\r
37 #define SUSP 237 /* Suspend process */
\r
38 #define xEOF 236 /* End of file: EOF is already used... */
\r
40 #define TELOPTS(X) \
\r
41 X(BINARY, 0) /* 8-bit data path */ \
\r
42 X(ECHO, 1) /* echo */ \
\r
43 X(RCP, 2) /* prepare to reconnect */ \
\r
44 X(SGA, 3) /* suppress go ahead */ \
\r
45 X(NAMS, 4) /* approximate message size */ \
\r
46 X(STATUS, 5) /* give status */ \
\r
47 X(TM, 6) /* timing mark */ \
\r
48 X(RCTE, 7) /* remote controlled transmission and echo */ \
\r
49 X(NAOL, 8) /* negotiate about output line width */ \
\r
50 X(NAOP, 9) /* negotiate about output page size */ \
\r
51 X(NAOCRD, 10) /* negotiate about CR disposition */ \
\r
52 X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \
\r
53 X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \
\r
54 X(NAOFFD, 13) /* negotiate about formfeed disposition */ \
\r
55 X(NAOVTS, 14) /* negotiate about vertical tab stops */ \
\r
56 X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \
\r
57 X(NAOLFD, 16) /* negotiate about output LF disposition */ \
\r
58 X(XASCII, 17) /* extended ascic character set */ \
\r
59 X(LOGOUT, 18) /* force logout */ \
\r
60 X(BM, 19) /* byte macro */ \
\r
61 X(DET, 20) /* data entry terminal */ \
\r
62 X(SUPDUP, 21) /* supdup protocol */ \
\r
63 X(SUPDUPOUTPUT, 22) /* supdup output */ \
\r
64 X(SNDLOC, 23) /* send location */ \
\r
65 X(TTYPE, 24) /* terminal type */ \
\r
66 X(EOR, 25) /* end or record */ \
\r
67 X(TUID, 26) /* TACACS user identification */ \
\r
68 X(OUTMRK, 27) /* output marking */ \
\r
69 X(TTYLOC, 28) /* terminal location number */ \
\r
70 X(3270REGIME, 29) /* 3270 regime */ \
\r
71 X(X3PAD, 30) /* X.3 PAD */ \
\r
72 X(NAWS, 31) /* window size */ \
\r
73 X(TSPEED, 32) /* terminal speed */ \
\r
74 X(LFLOW, 33) /* remote flow control */ \
\r
75 X(LINEMODE, 34) /* Linemode option */ \
\r
76 X(XDISPLOC, 35) /* X Display Location */ \
\r
77 X(OLD_ENVIRON, 36) /* Old - Environment variables */ \
\r
78 X(AUTHENTICATION, 37) /* Authenticate */ \
\r
79 X(ENCRYPT, 38) /* Encryption option */ \
\r
80 X(NEW_ENVIRON, 39) /* New - Environment variables */ \
\r
81 X(TN3270E, 40) /* TN3270 enhancements */ \
\r
83 X(CHARSET, 42) /* Character set */ \
\r
84 X(RSP, 43) /* Remote serial port */ \
\r
85 X(COM_PORT_OPTION, 44) /* Com port control */ \
\r
86 X(SLE, 45) /* Suppress local echo */ \
\r
87 X(STARTTLS, 46) /* Start TLS */ \
\r
88 X(KERMIT, 47) /* Automatic Kermit file transfer */ \
\r
91 X(PRAGMA_LOGON, 138) \
\r
92 X(SSPI_LOGON, 139) \
\r
93 X(PRAGMA_HEARTBEAT, 140) \
\r
94 X(EXOPL, 255) /* extended-options-list */
\r
96 #define telnet_enum(x,y) TELOPT_##x = y,
\r
97 enum { TELOPTS(telnet_enum) dummy=0 };
\r
100 #define TELQUAL_IS 0 /* option is... */
\r
101 #define TELQUAL_SEND 1 /* send option */
\r
102 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
\r
104 #define BSD_VALUE 0
\r
106 #define RFC_VALUE 1
\r
112 #define iswritable(x) \
\r
114 (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
\r
116 static char *telopt(int opt)
\r
118 #define telnet_str(x,y) case TELOPT_##x: return #x;
\r
120 TELOPTS(telnet_str)
\r
122 return "<unknown>";
\r
127 static void telnet_size(void *handle, int width, int height);
\r
130 int send; /* what we initially send */
\r
131 int nsend; /* -ve send if requested to stop it */
\r
132 int ack, nak; /* +ve and -ve acknowledgements */
\r
133 int option; /* the option code */
\r
134 int index; /* index into telnet->opt_states[] */
\r
136 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
\r
154 static const struct Opt o_naws =
\r
155 { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
\r
156 static const struct Opt o_tspeed =
\r
157 { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED };
\r
158 static const struct Opt o_ttype =
\r
159 { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
\r
160 static const struct Opt o_oenv =
\r
161 { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE };
\r
162 static const struct Opt o_nenv =
\r
163 { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED };
\r
164 static const struct Opt o_echo =
\r
165 { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
\r
166 static const struct Opt o_we_sga =
\r
167 { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
\r
168 static const struct Opt o_they_sga =
\r
169 { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
\r
170 static const struct Opt o_we_bin =
\r
171 { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
\r
172 static const struct Opt o_they_bin =
\r
173 { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
\r
175 static const struct Opt *const opts[] = {
\r
176 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
\r
177 &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
\r
180 typedef struct telnet_tag {
\r
181 const struct plug_function_table *fn;
\r
182 /* the above field _must_ be first in the structure */
\r
185 int closed_on_socket_error;
\r
189 int term_width, term_height;
\r
191 int opt_states[NUM_OPTS];
\r
193 int echoing, editing;
\r
197 int sb_opt, sb_len;
\r
198 unsigned char *sb_buf;
\r
202 TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
\r
203 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
\r
211 #define TELNET_MAX_BACKLOG 4096
\r
213 #define SB_DELTA 1024
\r
215 static void c_write(Telnet telnet, char *buf, int len)
\r
218 backlog = from_backend(telnet->frontend, 0, buf, len);
\r
219 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
\r
222 static void log_option(Telnet telnet, char *sender, int cmd, int option)
\r
226 * The strange-looking "<?""?>" below is there to avoid a
\r
227 * trigraph - a double question mark followed by > maps to a
\r
228 * closing brace character!
\r
230 buf = dupprintf("%s:\t%s %s", sender,
\r
231 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
\r
232 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"),
\r
234 logevent(telnet->frontend, buf);
\r
238 static void send_opt(Telnet telnet, int cmd, int option)
\r
240 unsigned char b[3];
\r
245 telnet->bufsize = sk_write(telnet->s, (char *)b, 3);
\r
246 log_option(telnet, "client", cmd, option);
\r
249 static void deactivate_option(Telnet telnet, const struct Opt *o)
\r
251 if (telnet->opt_states[o->index] == REQUESTED ||
\r
252 telnet->opt_states[o->index] == ACTIVE)
\r
253 send_opt(telnet, o->nsend, o->option);
\r
254 telnet->opt_states[o->index] = REALLY_INACTIVE;
\r
258 * Generate side effects of enabling or disabling an option.
\r
260 static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
\r
262 if (o->option == TELOPT_ECHO && o->send == DO)
\r
263 telnet->echoing = !enabled;
\r
264 else if (o->option == TELOPT_SGA && o->send == DO)
\r
265 telnet->editing = !enabled;
\r
266 if (telnet->ldisc) /* cause ldisc to notice the change */
\r
267 ldisc_send(telnet->ldisc, NULL, 0, 0);
\r
269 /* Ensure we get the minimum options */
\r
270 if (!telnet->activated) {
\r
271 if (telnet->opt_states[o_echo.index] == INACTIVE) {
\r
272 telnet->opt_states[o_echo.index] = REQUESTED;
\r
273 send_opt(telnet, o_echo.send, o_echo.option);
\r
275 if (telnet->opt_states[o_we_sga.index] == INACTIVE) {
\r
276 telnet->opt_states[o_we_sga.index] = REQUESTED;
\r
277 send_opt(telnet, o_we_sga.send, o_we_sga.option);
\r
279 if (telnet->opt_states[o_they_sga.index] == INACTIVE) {
\r
280 telnet->opt_states[o_they_sga.index] = REQUESTED;
\r
281 send_opt(telnet, o_they_sga.send, o_they_sga.option);
\r
283 telnet->activated = TRUE;
\r
287 static void activate_option(Telnet telnet, const struct Opt *o)
\r
289 if (o->send == WILL && o->option == TELOPT_NAWS)
\r
290 telnet_size(telnet, telnet->term_width, telnet->term_height);
\r
291 if (o->send == WILL &&
\r
292 (o->option == TELOPT_NEW_ENVIRON ||
\r
293 o->option == TELOPT_OLD_ENVIRON)) {
\r
295 * We may only have one kind of ENVIRON going at a time.
\r
296 * This is a hack, but who cares.
\r
298 deactivate_option(telnet, o->option ==
\r
299 TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
\r
301 option_side_effects(telnet, o, 1);
\r
304 static void refused_option(Telnet telnet, const struct Opt *o)
\r
306 if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
\r
307 telnet->opt_states[o_oenv.index] == INACTIVE) {
\r
308 send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
\r
309 telnet->opt_states[o_oenv.index] = REQUESTED;
\r
311 option_side_effects(telnet, o, 0);
\r
314 static void proc_rec_opt(Telnet telnet, int cmd, int option)
\r
316 const struct Opt *const *o;
\r
318 log_option(telnet, "server", cmd, option);
\r
319 for (o = opts; *o; o++) {
\r
320 if ((*o)->option == option && (*o)->ack == cmd) {
\r
321 switch (telnet->opt_states[(*o)->index]) {
\r
323 telnet->opt_states[(*o)->index] = ACTIVE;
\r
324 activate_option(telnet, *o);
\r
329 telnet->opt_states[(*o)->index] = ACTIVE;
\r
330 send_opt(telnet, (*o)->send, option);
\r
331 activate_option(telnet, *o);
\r
333 case REALLY_INACTIVE:
\r
334 send_opt(telnet, (*o)->nsend, option);
\r
338 } else if ((*o)->option == option && (*o)->nak == cmd) {
\r
339 switch (telnet->opt_states[(*o)->index]) {
\r
341 telnet->opt_states[(*o)->index] = INACTIVE;
\r
342 refused_option(telnet, *o);
\r
345 telnet->opt_states[(*o)->index] = INACTIVE;
\r
346 send_opt(telnet, (*o)->nsend, option);
\r
347 option_side_effects(telnet, *o, 0);
\r
350 case REALLY_INACTIVE:
\r
357 * If we reach here, the option was one we weren't prepared to
\r
358 * cope with. If the request was positive (WILL or DO), we send
\r
359 * a negative ack to indicate refusal. If the request was
\r
360 * negative (WONT / DONT), we must do nothing.
\r
362 if (cmd == WILL || cmd == DO)
\r
363 send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
\r
366 static void process_subneg(Telnet telnet)
\r
368 unsigned char *b, *p, *q;
\r
369 int var, value, n, bsize;
\r
370 char *e, *eval, *ekey, *user;
\r
372 switch (telnet->sb_opt) {
\r
373 case TELOPT_TSPEED:
\r
374 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
\r
376 char *termspeed = conf_get_str(telnet->conf, CONF_termspeed);
\r
377 b = snewn(20 + strlen(termspeed), unsigned char);
\r
380 b[2] = TELOPT_TSPEED;
\r
382 strcpy((char *)(b + 4), termspeed);
\r
383 n = 4 + strlen(termspeed);
\r
386 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2);
\r
387 logevent(telnet->frontend, "server:\tSB TSPEED SEND");
\r
388 logbuf = dupprintf("client:\tSB TSPEED IS %s", termspeed);
\r
389 logevent(telnet->frontend, logbuf);
\r
393 logevent(telnet->frontend, "server:\tSB TSPEED <something weird>");
\r
396 if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) {
\r
398 char *termtype = conf_get_str(telnet->conf, CONF_termtype);
\r
399 b = snewn(20 + strlen(termtype), unsigned char);
\r
402 b[2] = TELOPT_TTYPE;
\r
404 for (n = 0; termtype[n]; n++)
\r
405 b[n + 4] = (termtype[n] >= 'a' && termtype[n] <= 'z' ?
\r
406 termtype[n] + 'A' - 'a' :
\r
410 telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6);
\r
412 logevent(telnet->frontend, "server:\tSB TTYPE SEND");
\r
413 logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4);
\r
414 logevent(telnet->frontend, logbuf);
\r
418 logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n");
\r
420 case TELOPT_OLD_ENVIRON:
\r
421 case TELOPT_NEW_ENVIRON:
\r
422 p = telnet->sb_buf;
\r
423 q = p + telnet->sb_len;
\r
424 if (p < q && *p == TELQUAL_SEND) {
\r
427 logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt));
\r
428 logevent(telnet->frontend, logbuf);
\r
430 if (telnet->sb_opt == TELOPT_OLD_ENVIRON) {
\r
431 if (conf_get_int(telnet->conf, CONF_rfc_environ)) {
\r
439 * Try to guess the sense of VAR and VALUE.
\r
442 if (*p == RFC_VAR) {
\r
445 } else if (*p == BSD_VAR) {
\r
453 * With NEW_ENVIRON, the sense of VAR and VALUE
\r
460 for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
\r
463 eval = conf_get_str_strs(telnet->conf, CONF_environmt,
\r
465 bsize += strlen(ekey) + strlen(eval) + 2;
\r
466 user = get_remote_username(telnet->conf);
\r
468 bsize += 6 + strlen(user);
\r
470 b = snewn(bsize, unsigned char);
\r
473 b[2] = telnet->sb_opt;
\r
476 for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
\r
479 eval = conf_get_str_strs(telnet->conf, CONF_environmt,
\r
482 for (e = ekey; *e; e++)
\r
485 for (e = eval; *e; e++)
\r
495 for (e = user; *e; e++)
\r
500 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
\r
502 logbuf = dupprintf("client:\tSB %s IS <nothing>",
\r
503 telopt(telnet->sb_opt));
\r
504 logevent(telnet->frontend, logbuf);
\r
507 logbuf = dupprintf("client:\tSB %s IS:",
\r
508 telopt(telnet->sb_opt));
\r
509 logevent(telnet->frontend, logbuf);
\r
511 for (eval = conf_get_str_strs(telnet->conf, CONF_environmt,
\r
514 eval = conf_get_str_strs(telnet->conf, CONF_environmt,
\r
516 logbuf = dupprintf("\t%s=%s", ekey, eval);
\r
517 logevent(telnet->frontend, logbuf);
\r
521 logbuf = dupprintf("\tUSER=%s", user);
\r
522 logevent(telnet->frontend, logbuf);
\r
533 static void do_telnet_read(Telnet telnet, char *buf, int len)
\r
535 char *outbuf = NULL;
\r
536 int outbuflen = 0, outbufsize = 0;
\r
538 #define ADDTOBUF(c) do { \
\r
539 if (outbuflen >= outbufsize) { \
\r
540 outbufsize = outbuflen + 256; \
\r
541 outbuf = sresize(outbuf, outbufsize, char); \
\r
543 outbuf[outbuflen++] = (c); \
\r
547 int c = (unsigned char) *buf++;
\r
549 switch (telnet->state) {
\r
552 if (c == NUL && telnet->state == SEENCR)
\r
553 telnet->state = TOP_LEVEL;
\r
555 telnet->state = SEENIAC;
\r
557 if (!telnet->in_synch)
\r
561 /* I can't get the F***ing winsock to insert the urgent IAC
\r
562 * into the right position! Even with SO_OOBINLINE it gives
\r
563 * it to recv too soon. And of course the DM byte (that
\r
564 * arrives in the same packet!) appears several K later!!
\r
566 * Oh well, we do get the DM in the right place so I'll
\r
567 * just stop hiding on the next 0xf2 and hope for the best.
\r
570 telnet->in_synch = 0;
\r
572 if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
\r
573 telnet->state = SEENCR;
\r
575 telnet->state = TOP_LEVEL;
\r
580 telnet->state = SEENDO;
\r
581 else if (c == DONT)
\r
582 telnet->state = SEENDONT;
\r
583 else if (c == WILL)
\r
584 telnet->state = SEENWILL;
\r
585 else if (c == WONT)
\r
586 telnet->state = SEENWONT;
\r
588 telnet->state = SEENSB;
\r
589 else if (c == DM) {
\r
590 telnet->in_synch = 0;
\r
591 telnet->state = TOP_LEVEL;
\r
593 /* ignore everything else; print it if it's IAC */
\r
597 telnet->state = TOP_LEVEL;
\r
601 proc_rec_opt(telnet, WILL, c);
\r
602 telnet->state = TOP_LEVEL;
\r
605 proc_rec_opt(telnet, WONT, c);
\r
606 telnet->state = TOP_LEVEL;
\r
609 proc_rec_opt(telnet, DO, c);
\r
610 telnet->state = TOP_LEVEL;
\r
613 proc_rec_opt(telnet, DONT, c);
\r
614 telnet->state = TOP_LEVEL;
\r
617 telnet->sb_opt = c;
\r
618 telnet->sb_len = 0;
\r
619 telnet->state = SUBNEGOT;
\r
623 telnet->state = SUBNEG_IAC;
\r
626 if (telnet->sb_len >= telnet->sb_size) {
\r
627 telnet->sb_size += SB_DELTA;
\r
628 telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
\r
631 telnet->sb_buf[telnet->sb_len++] = c;
\r
632 telnet->state = SUBNEGOT; /* in case we came here by goto */
\r
637 goto subneg_addchar; /* yes, it's a hack, I know, but... */
\r
639 process_subneg(telnet);
\r
640 telnet->state = TOP_LEVEL;
\r
647 c_write(telnet, outbuf, outbuflen);
\r
651 static void telnet_log(Plug plug, int type, SockAddr addr, int port,
\r
652 const char *error_msg, int error_code)
\r
654 Telnet telnet = (Telnet) plug;
\r
655 char addrbuf[256], *msg;
\r
657 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
660 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
662 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
664 logevent(telnet->frontend, msg);
\r
668 static int telnet_closing(Plug plug, const char *error_msg, int error_code,
\r
671 Telnet telnet = (Telnet) plug;
\r
674 * We don't implement independent EOF in each direction for Telnet
\r
675 * connections; as soon as we get word that the remote side has
\r
676 * sent us EOF, we wind up the whole connection.
\r
680 sk_close(telnet->s);
\r
683 telnet->closed_on_socket_error = TRUE;
\r
684 notify_remote_exit(telnet->frontend);
\r
687 logevent(telnet->frontend, error_msg);
\r
688 connection_fatal(telnet->frontend, "%s", error_msg);
\r
690 /* Otherwise, the remote side closed the connection normally. */
\r
694 static int telnet_receive(Plug plug, int urgent, char *data, int len)
\r
696 Telnet telnet = (Telnet) plug;
\r
698 telnet->in_synch = TRUE;
\r
699 do_telnet_read(telnet, data, len);
\r
703 static void telnet_sent(Plug plug, int bufsize)
\r
705 Telnet telnet = (Telnet) plug;
\r
706 telnet->bufsize = bufsize;
\r
710 * Called to set up the Telnet connection.
\r
712 * Returns an error message, or NULL on success.
\r
714 * Also places the canonical host name into `realhost'. It must be
\r
715 * freed by the caller.
\r
717 static const char *telnet_init(void *frontend_handle, void **backend_handle,
\r
718 Conf *conf, char *host, int port,
\r
719 char **realhost, int nodelay, int keepalive)
\r
721 static const struct plug_function_table fn_table = {
\r
733 telnet = snew(struct telnet_tag);
\r
734 telnet->fn = &fn_table;
\r
735 telnet->conf = conf_copy(conf);
\r
737 telnet->closed_on_socket_error = FALSE;
\r
738 telnet->echoing = TRUE;
\r
739 telnet->editing = TRUE;
\r
740 telnet->activated = FALSE;
\r
741 telnet->sb_buf = NULL;
\r
742 telnet->sb_size = 0;
\r
743 telnet->frontend = frontend_handle;
\r
744 telnet->term_width = conf_get_int(telnet->conf, CONF_width);
\r
745 telnet->term_height = conf_get_int(telnet->conf, CONF_height);
\r
746 telnet->state = TOP_LEVEL;
\r
747 telnet->ldisc = NULL;
\r
748 telnet->pinger = NULL;
\r
749 *backend_handle = telnet;
\r
752 * Try to find host.
\r
756 addressfamily = conf_get_int(telnet->conf, CONF_addressfamily);
\r
757 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
758 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
759 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
761 logevent(telnet->frontend, buf);
\r
764 addr = name_lookup(host, port, realhost, telnet->conf, addressfamily);
\r
765 if ((err = sk_addr_error(addr)) != NULL) {
\r
766 sk_addr_free(addr);
\r
771 port = 23; /* default telnet port */
\r
776 telnet->s = new_connection(addr, *realhost, port, 0, 1,
\r
777 nodelay, keepalive, (Plug) telnet, telnet->conf);
\r
778 if ((err = sk_socket_error(telnet->s)) != NULL)
\r
781 telnet->pinger = pinger_new(telnet->conf, &telnet_backend, telnet);
\r
784 * Initialise option states.
\r
786 if (conf_get_int(telnet->conf, CONF_passive_telnet)) {
\r
787 const struct Opt *const *o;
\r
789 for (o = opts; *o; o++)
\r
790 telnet->opt_states[(*o)->index] = INACTIVE;
\r
792 const struct Opt *const *o;
\r
794 for (o = opts; *o; o++) {
\r
795 telnet->opt_states[(*o)->index] = (*o)->initial_state;
\r
796 if (telnet->opt_states[(*o)->index] == REQUESTED)
\r
797 send_opt(telnet, (*o)->send, (*o)->option);
\r
799 telnet->activated = TRUE;
\r
803 * Set up SYNCH state.
\r
805 telnet->in_synch = FALSE;
\r
808 * We can send special commands from the start.
\r
810 update_specials_menu(telnet->frontend);
\r
813 * loghost overrides realhost, if specified.
\r
815 loghost = conf_get_str(telnet->conf, CONF_loghost);
\r
820 *realhost = dupstr(loghost);
\r
822 colon = host_strrchr(*realhost, ':');
\r
830 static void telnet_free(void *handle)
\r
832 Telnet telnet = (Telnet) handle;
\r
834 sfree(telnet->sb_buf);
\r
836 sk_close(telnet->s);
\r
837 if (telnet->pinger)
\r
838 pinger_free(telnet->pinger);
\r
839 conf_free(telnet->conf);
\r
843 * Reconfigure the Telnet backend. There's no immediate action
\r
844 * necessary, in this backend: we just save the fresh config for
\r
845 * any subsequent negotiations.
\r
847 static void telnet_reconfig(void *handle, Conf *conf)
\r
849 Telnet telnet = (Telnet) handle;
\r
850 pinger_reconfig(telnet->pinger, telnet->conf, conf);
\r
851 conf_free(telnet->conf);
\r
852 telnet->conf = conf_copy(conf);
\r
856 * Called to send data down the Telnet connection.
\r
858 static int telnet_send(void *handle, char *buf, int len)
\r
860 Telnet telnet = (Telnet) handle;
\r
861 unsigned char *p, *end;
\r
862 static const unsigned char iac[2] = { IAC, IAC };
\r
863 static const unsigned char cr[2] = { CR, NUL };
\r
865 static const unsigned char nl[2] = { CR, LF };
\r
868 if (telnet->s == NULL)
\r
871 p = (unsigned char *)buf;
\r
872 end = (unsigned char *)(buf + len);
\r
874 unsigned char *q = p;
\r
876 while (p < end && iswritable(*p))
\r
878 telnet->bufsize = sk_write(telnet->s, (char *)q, p - q);
\r
880 while (p < end && !iswritable(*p)) {
\r
882 sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2);
\r
887 return telnet->bufsize;
\r
891 * Called to query the current socket sendability status.
\r
893 static int telnet_sendbuffer(void *handle)
\r
895 Telnet telnet = (Telnet) handle;
\r
896 return telnet->bufsize;
\r
900 * Called to set the size of the window from Telnet's POV.
\r
902 static void telnet_size(void *handle, int width, int height)
\r
904 Telnet telnet = (Telnet) handle;
\r
905 unsigned char b[24];
\r
909 telnet->term_width = width;
\r
910 telnet->term_height = height;
\r
912 if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
\r
917 b[n++] = TELOPT_NAWS;
\r
918 b[n++] = telnet->term_width >> 8;
\r
919 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
920 b[n++] = telnet->term_width & 0xFF;
\r
921 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
922 b[n++] = telnet->term_height >> 8;
\r
923 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
924 b[n++] = telnet->term_height & 0xFF;
\r
925 if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */
\r
928 telnet->bufsize = sk_write(telnet->s, (char *)b, n);
\r
929 logbuf = dupprintf("client:\tSB NAWS %d,%d",
\r
930 telnet->term_width, telnet->term_height);
\r
931 logevent(telnet->frontend, logbuf);
\r
936 * Send Telnet special codes.
\r
938 static void telnet_special(void *handle, Telnet_Special code)
\r
940 Telnet telnet = (Telnet) handle;
\r
941 unsigned char b[2];
\r
943 if (telnet->s == NULL)
\r
950 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
954 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
958 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
962 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
966 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
970 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
974 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
978 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
982 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
986 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
990 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
994 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
997 /* In BINARY mode, CR-LF becomes just CR -
\r
998 * and without the NUL suffix too. */
\r
999 if (telnet->opt_states[o_we_bin.index] == ACTIVE)
\r
1000 telnet->bufsize = sk_write(telnet->s, "\r", 1);
\r
1002 telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
\r
1006 telnet->bufsize = sk_write(telnet->s, (char *)b, 1);
\r
1007 telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1);
\r
1010 if (telnet->opt_states[o_echo.index] == INACTIVE ||
\r
1011 telnet->opt_states[o_echo.index] == REALLY_INACTIVE) {
\r
1012 telnet->opt_states[o_echo.index] = REQUESTED;
\r
1013 send_opt(telnet, o_echo.send, o_echo.option);
\r
1017 if (telnet->opt_states[o_echo.index] == ACTIVE) {
\r
1018 telnet->opt_states[o_echo.index] = REQUESTED;
\r
1019 send_opt(telnet, o_echo.nsend, o_echo.option);
\r
1023 if (telnet->opt_states[o_they_sga.index] == ACTIVE) {
\r
1025 telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
\r
1029 break; /* never heard of it */
\r
1033 static const struct telnet_special *telnet_get_specials(void *handle)
\r
1035 static const struct telnet_special specials[] = {
\r
1036 {"Are You There", TS_AYT},
\r
1037 {"Break", TS_BRK},
\r
1038 {"Synch", TS_SYNCH},
\r
1039 {"Erase Character", TS_EC},
\r
1040 {"Erase Line", TS_EL},
\r
1041 {"Go Ahead", TS_GA},
\r
1042 {"No Operation", TS_NOP},
\r
1044 {"Abort Process", TS_ABORT},
\r
1045 {"Abort Output", TS_AO},
\r
1046 {"Interrupt Process", TS_IP},
\r
1047 {"Suspend Process", TS_SUSP},
\r
1049 {"End Of Record", TS_EOR},
\r
1050 {"End Of File", TS_EOF},
\r
1051 {NULL, TS_EXITMENU}
\r
1056 static int telnet_connected(void *handle)
\r
1058 Telnet telnet = (Telnet) handle;
\r
1059 return telnet->s != NULL;
\r
1062 static int telnet_sendok(void *handle)
\r
1064 /* Telnet telnet = (Telnet) handle; */
\r
1068 static void telnet_unthrottle(void *handle, int backlog)
\r
1070 Telnet telnet = (Telnet) handle;
\r
1071 sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
\r
1074 static int telnet_ldisc(void *handle, int option)
\r
1076 Telnet telnet = (Telnet) handle;
\r
1077 if (option == LD_ECHO)
\r
1078 return telnet->echoing;
\r
1079 if (option == LD_EDIT)
\r
1080 return telnet->editing;
\r
1084 static void telnet_provide_ldisc(void *handle, void *ldisc)
\r
1086 Telnet telnet = (Telnet) handle;
\r
1087 telnet->ldisc = ldisc;
\r
1090 static void telnet_provide_logctx(void *handle, void *logctx)
\r
1092 /* This is a stub. */
\r
1095 static int telnet_exitcode(void *handle)
\r
1097 Telnet telnet = (Telnet) handle;
\r
1098 if (telnet->s != NULL)
\r
1099 return -1; /* still connected */
\r
1100 else if (telnet->closed_on_socket_error)
\r
1101 return INT_MAX; /* a socket error counts as an unclean exit */
\r
1103 /* Telnet doesn't transmit exit codes back to the client */
\r
1108 * cfg_info for Telnet does nothing at all.
\r
1110 static int telnet_cfg_info(void *handle)
\r
1115 Backend telnet_backend = {
\r
1120 telnet_sendbuffer,
\r
1123 telnet_get_specials,
\r
1128 telnet_provide_ldisc,
\r
1129 telnet_provide_logctx,
\r
1130 telnet_unthrottle,
\r