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