17 #define IAC 255 /* interpret as command: */
18 #define DONT 254 /* you are not to use option */
19 #define DO 253 /* please, you use option */
20 #define WONT 252 /* I won't use option */
21 #define WILL 251 /* I will use option */
22 #define SB 250 /* interpret as subnegotiation */
23 #define SE 240 /* end sub negotiation */
25 #define GA 249 /* you may reverse the line */
26 #define EL 248 /* erase the current line */
27 #define EC 247 /* erase the current character */
28 #define AYT 246 /* are you there */
29 #define AO 245 /* abort output--but let prog finish */
30 #define IP 244 /* interrupt process--permanently */
31 #define BREAK 243 /* break */
32 #define DM 242 /* data mark--for connect. cleaning */
33 #define NOP 241 /* nop */
34 #define EOR 239 /* end of record (transparent mode) */
35 #define ABORT 238 /* Abort process */
36 #define SUSP 237 /* Suspend process */
37 #define xEOF 236 /* End of file: EOF is already used... */
40 X(BINARY, 0) /* 8-bit data path */ \
41 X(ECHO, 1) /* echo */ \
42 X(RCP, 2) /* prepare to reconnect */ \
43 X(SGA, 3) /* suppress go ahead */ \
44 X(NAMS, 4) /* approximate message size */ \
45 X(STATUS, 5) /* give status */ \
46 X(TM, 6) /* timing mark */ \
47 X(RCTE, 7) /* remote controlled transmission and echo */ \
48 X(NAOL, 8) /* negotiate about output line width */ \
49 X(NAOP, 9) /* negotiate about output page size */ \
50 X(NAOCRD, 10) /* negotiate about CR disposition */ \
51 X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \
52 X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \
53 X(NAOFFD, 13) /* negotiate about formfeed disposition */ \
54 X(NAOVTS, 14) /* negotiate about vertical tab stops */ \
55 X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \
56 X(NAOLFD, 16) /* negotiate about output LF disposition */ \
57 X(XASCII, 17) /* extended ascic character set */ \
58 X(LOGOUT, 18) /* force logout */ \
59 X(BM, 19) /* byte macro */ \
60 X(DET, 20) /* data entry terminal */ \
61 X(SUPDUP, 21) /* supdup protocol */ \
62 X(SUPDUPOUTPUT, 22) /* supdup output */ \
63 X(SNDLOC, 23) /* send location */ \
64 X(TTYPE, 24) /* terminal type */ \
65 X(EOR, 25) /* end or record */ \
66 X(TUID, 26) /* TACACS user identification */ \
67 X(OUTMRK, 27) /* output marking */ \
68 X(TTYLOC, 28) /* terminal location number */ \
69 X(3270REGIME, 29) /* 3270 regime */ \
70 X(X3PAD, 30) /* X.3 PAD */ \
71 X(NAWS, 31) /* window size */ \
72 X(TSPEED, 32) /* terminal speed */ \
73 X(LFLOW, 33) /* remote flow control */ \
74 X(LINEMODE, 34) /* Linemode option */ \
75 X(XDISPLOC, 35) /* X Display Location */ \
76 X(OLD_ENVIRON, 36) /* Old - Environment variables */ \
77 X(AUTHENTICATION, 37) /* Authenticate */ \
78 X(ENCRYPT, 38) /* Encryption option */ \
79 X(NEW_ENVIRON, 39) /* New - Environment variables */ \
80 X(TN3270E, 40) /* TN3270 enhancements */ \
82 X(CHARSET, 42) /* Character set */ \
83 X(RSP, 43) /* Remote serial port */ \
84 X(COM_PORT_OPTION, 44) /* Com port control */ \
85 X(SLE, 45) /* Suppress local echo */ \
86 X(STARTTLS, 46) /* Start TLS */ \
87 X(KERMIT, 47) /* Automatic Kermit file transfer */ \
90 X(PRAGMA_LOGON, 138) \
92 X(PRAGMA_HEARTBEAT, 140) \
93 X(EXOPL, 255) /* extended-options-list */
95 #define telnet_enum(x,y) TELOPT_##x = y,
96 enum { TELOPTS(telnet_enum
) dummy
=0 };
99 #define TELQUAL_IS 0 /* option is... */
100 #define TELQUAL_SEND 1 /* send option */
101 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
111 #define iswritable(x) \
113 (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
115 static char *telopt(int opt
)
117 #define telnet_str(x,y) case TELOPT_##x: return #x;
126 static void telnet_size(void *handle
, int width
, int height
);
129 int send
; /* what we initially send */
130 int nsend
; /* -ve send if requested to stop it */
131 int ack
, nak
; /* +ve and -ve acknowledgements */
132 int option
; /* the option code */
133 int index
; /* index into telnet->opt_states[] */
135 REQUESTED
, ACTIVE
, INACTIVE
, REALLY_INACTIVE
153 static const struct Opt o_naws
=
154 { WILL
, WONT
, DO
, DONT
, TELOPT_NAWS
, OPTINDEX_NAWS
, REQUESTED
};
155 static const struct Opt o_tspeed
=
156 { WILL
, WONT
, DO
, DONT
, TELOPT_TSPEED
, OPTINDEX_TSPEED
, REQUESTED
};
157 static const struct Opt o_ttype
=
158 { WILL
, WONT
, DO
, DONT
, TELOPT_TTYPE
, OPTINDEX_TTYPE
, REQUESTED
};
159 static const struct Opt o_oenv
=
160 { WILL
, WONT
, DO
, DONT
, TELOPT_OLD_ENVIRON
, OPTINDEX_OENV
, INACTIVE
};
161 static const struct Opt o_nenv
=
162 { WILL
, WONT
, DO
, DONT
, TELOPT_NEW_ENVIRON
, OPTINDEX_NENV
, REQUESTED
};
163 static const struct Opt o_echo
=
164 { DO
, DONT
, WILL
, WONT
, TELOPT_ECHO
, OPTINDEX_ECHO
, REQUESTED
};
165 static const struct Opt o_we_sga
=
166 { WILL
, WONT
, DO
, DONT
, TELOPT_SGA
, OPTINDEX_WE_SGA
, REQUESTED
};
167 static const struct Opt o_they_sga
=
168 { DO
, DONT
, WILL
, WONT
, TELOPT_SGA
, OPTINDEX_THEY_SGA
, REQUESTED
};
169 static const struct Opt o_we_bin
=
170 { WILL
, WONT
, DO
, DONT
, TELOPT_BINARY
, OPTINDEX_WE_BIN
, INACTIVE
};
171 static const struct Opt o_they_bin
=
172 { DO
, DONT
, WILL
, WONT
, TELOPT_BINARY
, OPTINDEX_THEY_BIN
, INACTIVE
};
174 static const struct Opt
*const opts
[] = {
175 &o_naws
, &o_tspeed
, &o_ttype
, &o_oenv
, &o_nenv
, &o_echo
,
176 &o_we_sga
, &o_they_sga
, &o_we_bin
, &o_they_bin
, NULL
179 typedef struct telnet_tag
{
180 const struct plug_function_table
*fn
;
181 /* the above field _must_ be first in the structure */
187 int term_width
, term_height
;
189 int opt_states
[NUM_OPTS
];
191 int echoing
, editing
;
196 unsigned char *sb_buf
;
200 TOP_LEVEL
, SEENIAC
, SEENWILL
, SEENWONT
, SEENDO
, SEENDONT
,
201 SEENSB
, SUBNEGOT
, SUBNEG_IAC
, SEENCR
209 #define TELNET_MAX_BACKLOG 4096
211 #define SB_DELTA 1024
213 static void c_write(Telnet telnet
, char *buf
, int len
)
216 backlog
= from_backend(telnet
->frontend
, 0, buf
, len
);
217 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
220 static void log_option(Telnet telnet
, char *sender
, int cmd
, int option
)
224 * The strange-looking "<?""?>" below is there to avoid a
225 * trigraph - a double question mark followed by > maps to a
226 * closing brace character!
228 buf
= dupprintf("%s:\t%s %s", sender
,
229 (cmd
== WILL ?
"WILL" : cmd
== WONT ?
"WONT" :
230 cmd
== DO ?
"DO" : cmd
== DONT ?
"DONT" : "<?""?>"),
232 logevent(telnet
->frontend
, buf
);
236 static void send_opt(Telnet telnet
, int cmd
, int option
)
243 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 3);
244 log_option(telnet
, "client", cmd
, option
);
247 static void deactivate_option(Telnet telnet
, const struct Opt
*o
)
249 if (telnet
->opt_states
[o
->index
] == REQUESTED
||
250 telnet
->opt_states
[o
->index
] == ACTIVE
)
251 send_opt(telnet
, o
->nsend
, o
->option
);
252 telnet
->opt_states
[o
->index
] = REALLY_INACTIVE
;
256 * Generate side effects of enabling or disabling an option.
258 static void option_side_effects(Telnet telnet
, const struct Opt
*o
, int enabled
)
260 if (o
->option
== TELOPT_ECHO
&& o
->send
== DO
)
261 telnet
->echoing
= !enabled
;
262 else if (o
->option
== TELOPT_SGA
&& o
->send
== DO
)
263 telnet
->editing
= !enabled
;
264 if (telnet
->ldisc
) /* cause ldisc to notice the change */
265 ldisc_send(telnet
->ldisc
, NULL
, 0, 0);
267 /* Ensure we get the minimum options */
268 if (!telnet
->activated
) {
269 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
) {
270 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
271 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
273 if (telnet
->opt_states
[o_we_sga
.index
] == INACTIVE
) {
274 telnet
->opt_states
[o_we_sga
.index
] = REQUESTED
;
275 send_opt(telnet
, o_we_sga
.send
, o_we_sga
.option
);
277 if (telnet
->opt_states
[o_they_sga
.index
] == INACTIVE
) {
278 telnet
->opt_states
[o_they_sga
.index
] = REQUESTED
;
279 send_opt(telnet
, o_they_sga
.send
, o_they_sga
.option
);
281 telnet
->activated
= TRUE
;
285 static void activate_option(Telnet telnet
, const struct Opt
*o
)
287 if (o
->send
== WILL
&& o
->option
== TELOPT_NAWS
)
288 telnet_size(telnet
, telnet
->term_width
, telnet
->term_height
);
289 if (o
->send
== WILL
&&
290 (o
->option
== TELOPT_NEW_ENVIRON
||
291 o
->option
== TELOPT_OLD_ENVIRON
)) {
293 * We may only have one kind of ENVIRON going at a time.
294 * This is a hack, but who cares.
296 deactivate_option(telnet
, o
->option
==
297 TELOPT_NEW_ENVIRON ?
&o_oenv
: &o_nenv
);
299 option_side_effects(telnet
, o
, 1);
302 static void refused_option(Telnet telnet
, const struct Opt
*o
)
304 if (o
->send
== WILL
&& o
->option
== TELOPT_NEW_ENVIRON
&&
305 telnet
->opt_states
[o_oenv
.index
] == INACTIVE
) {
306 send_opt(telnet
, WILL
, TELOPT_OLD_ENVIRON
);
307 telnet
->opt_states
[o_oenv
.index
] = REQUESTED
;
309 option_side_effects(telnet
, o
, 0);
312 static void proc_rec_opt(Telnet telnet
, int cmd
, int option
)
314 const struct Opt
*const *o
;
316 log_option(telnet
, "server", cmd
, option
);
317 for (o
= opts
; *o
; o
++) {
318 if ((*o
)->option
== option
&& (*o
)->ack
== cmd
) {
319 switch (telnet
->opt_states
[(*o
)->index
]) {
321 telnet
->opt_states
[(*o
)->index
] = ACTIVE
;
322 activate_option(telnet
, *o
);
327 telnet
->opt_states
[(*o
)->index
] = ACTIVE
;
328 send_opt(telnet
, (*o
)->send
, option
);
329 activate_option(telnet
, *o
);
331 case REALLY_INACTIVE
:
332 send_opt(telnet
, (*o
)->nsend
, option
);
336 } else if ((*o
)->option
== option
&& (*o
)->nak
== cmd
) {
337 switch (telnet
->opt_states
[(*o
)->index
]) {
339 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
340 refused_option(telnet
, *o
);
343 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
344 send_opt(telnet
, (*o
)->nsend
, option
);
345 option_side_effects(telnet
, *o
, 0);
348 case REALLY_INACTIVE
:
355 * If we reach here, the option was one we weren't prepared to
356 * cope with. If the request was positive (WILL or DO), we send
357 * a negative ack to indicate refusal. If the request was
358 * negative (WONT / DONT), we must do nothing.
360 if (cmd
== WILL
|| cmd
== DO
)
361 send_opt(telnet
, (cmd
== WILL ? DONT
: WONT
), option
);
364 static void process_subneg(Telnet telnet
)
366 unsigned char *b
, *p
, *q
;
367 int var
, value
, n
, bsize
;
368 char *e
, *eval
, *ekey
, *user
;
370 switch (telnet
->sb_opt
) {
372 if (telnet
->sb_len
== 1 && telnet
->sb_buf
[0] == TELQUAL_SEND
) {
374 char *termspeed
= conf_get_str(telnet
->conf
, CONF_termspeed
);
375 b
= snewn(20 + strlen(termspeed
), unsigned char);
378 b
[2] = TELOPT_TSPEED
;
380 strcpy((char *)(b
+ 4), termspeed
);
381 n
= 4 + strlen(termspeed
);
384 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
+ 2);
385 logevent(telnet
->frontend
, "server:\tSB TSPEED SEND");
386 logbuf
= dupprintf("client:\tSB TSPEED IS %s", termspeed
);
387 logevent(telnet
->frontend
, logbuf
);
391 logevent(telnet
->frontend
, "server:\tSB TSPEED <something weird>");
394 if (telnet
->sb_len
== 1 && telnet
->sb_buf
[0] == TELQUAL_SEND
) {
396 char *termtype
= conf_get_str(telnet
->conf
, CONF_termtype
);
397 b
= snewn(20 + strlen(termtype
), unsigned char);
402 for (n
= 0; termtype
[n
]; n
++)
403 b
[n
+ 4] = (termtype
[n
] >= 'a' && termtype
[n
] <= 'z' ?
404 termtype
[n
] + 'A' - 'a' :
408 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
+ 6);
410 logevent(telnet
->frontend
, "server:\tSB TTYPE SEND");
411 logbuf
= dupprintf("client:\tSB TTYPE IS %s", b
+ 4);
412 logevent(telnet
->frontend
, logbuf
);
416 logevent(telnet
->frontend
, "server:\tSB TTYPE <something weird>\r\n");
418 case TELOPT_OLD_ENVIRON
:
419 case TELOPT_NEW_ENVIRON
:
421 q
= p
+ telnet
->sb_len
;
422 if (p
< q
&& *p
== TELQUAL_SEND
) {
425 logbuf
= dupprintf("server:\tSB %s SEND", telopt(telnet
->sb_opt
));
426 logevent(telnet
->frontend
, logbuf
);
428 if (telnet
->sb_opt
== TELOPT_OLD_ENVIRON
) {
429 if (conf_get_int(telnet
->conf
, CONF_rfc_environ
)) {
437 * Try to guess the sense of VAR and VALUE.
443 } else if (*p
== BSD_VAR
) {
451 * With NEW_ENVIRON, the sense of VAR and VALUE
458 for (eval
= conf_get_str_strs(telnet
->conf
, CONF_environmt
,
461 eval
= conf_get_str_strs(telnet
->conf
, CONF_environmt
,
463 bsize
+= strlen(ekey
) + strlen(eval
) + 2;
464 user
= get_remote_username(telnet
->conf
);
466 bsize
+= 6 + strlen(user
);
468 b
= snewn(bsize
, unsigned char);
471 b
[2] = telnet
->sb_opt
;
474 for (eval
= conf_get_str_strs(telnet
->conf
, CONF_environmt
,
477 eval
= conf_get_str_strs(telnet
->conf
, CONF_environmt
,
480 for (e
= ekey
; *e
; e
++)
483 for (e
= eval
; *e
; e
++)
493 for (e
= user
; *e
; e
++)
498 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
500 logbuf
= dupprintf("client:\tSB %s IS <nothing>",
501 telopt(telnet
->sb_opt
));
502 logevent(telnet
->frontend
, logbuf
);
505 logbuf
= dupprintf("client:\tSB %s IS:",
506 telopt(telnet
->sb_opt
));
507 logevent(telnet
->frontend
, logbuf
);
509 for (eval
= conf_get_str_strs(telnet
->conf
, CONF_environmt
,
512 eval
= conf_get_str_strs(telnet
->conf
, CONF_environmt
,
514 logbuf
= dupprintf("\t%s=%s", ekey
, eval
);
515 logevent(telnet
->frontend
, logbuf
);
519 logbuf
= dupprintf("\tUSER=%s", user
);
520 logevent(telnet
->frontend
, logbuf
);
531 static void do_telnet_read(Telnet telnet
, char *buf
, int len
)
534 int outbuflen
= 0, outbufsize
= 0;
536 #define ADDTOBUF(c) do { \
537 if (outbuflen >= outbufsize) { \
538 outbufsize = outbuflen + 256; \
539 outbuf = sresize(outbuf, outbufsize, char); \
541 outbuf[outbuflen++] = (c); \
545 int c
= (unsigned char) *buf
++;
547 switch (telnet
->state
) {
550 if (c
== NUL
&& telnet
->state
== SEENCR
)
551 telnet
->state
= TOP_LEVEL
;
553 telnet
->state
= SEENIAC
;
555 if (!telnet
->in_synch
)
559 /* I can't get the F***ing winsock to insert the urgent IAC
560 * into the right position! Even with SO_OOBINLINE it gives
561 * it to recv too soon. And of course the DM byte (that
562 * arrives in the same packet!) appears several K later!!
564 * Oh well, we do get the DM in the right place so I'll
565 * just stop hiding on the next 0xf2 and hope for the best.
568 telnet
->in_synch
= 0;
570 if (c
== CR
&& telnet
->opt_states
[o_they_bin
.index
] != ACTIVE
)
571 telnet
->state
= SEENCR
;
573 telnet
->state
= TOP_LEVEL
;
578 telnet
->state
= SEENDO
;
580 telnet
->state
= SEENDONT
;
582 telnet
->state
= SEENWILL
;
584 telnet
->state
= SEENWONT
;
586 telnet
->state
= SEENSB
;
588 telnet
->in_synch
= 0;
589 telnet
->state
= TOP_LEVEL
;
591 /* ignore everything else; print it if it's IAC */
595 telnet
->state
= TOP_LEVEL
;
599 proc_rec_opt(telnet
, WILL
, c
);
600 telnet
->state
= TOP_LEVEL
;
603 proc_rec_opt(telnet
, WONT
, c
);
604 telnet
->state
= TOP_LEVEL
;
607 proc_rec_opt(telnet
, DO
, c
);
608 telnet
->state
= TOP_LEVEL
;
611 proc_rec_opt(telnet
, DONT
, c
);
612 telnet
->state
= TOP_LEVEL
;
617 telnet
->state
= SUBNEGOT
;
621 telnet
->state
= SUBNEG_IAC
;
624 if (telnet
->sb_len
>= telnet
->sb_size
) {
625 telnet
->sb_size
+= SB_DELTA
;
626 telnet
->sb_buf
= sresize(telnet
->sb_buf
, telnet
->sb_size
,
629 telnet
->sb_buf
[telnet
->sb_len
++] = c
;
630 telnet
->state
= SUBNEGOT
; /* in case we came here by goto */
635 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
637 process_subneg(telnet
);
638 telnet
->state
= TOP_LEVEL
;
645 c_write(telnet
, outbuf
, outbuflen
);
649 static void telnet_log(Plug plug
, int type
, SockAddr addr
, int port
,
650 const char *error_msg
, int error_code
)
652 Telnet telnet
= (Telnet
) plug
;
653 char addrbuf
[256], *msg
;
655 sk_getaddr(addr
, addrbuf
, lenof(addrbuf
));
658 msg
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
660 msg
= dupprintf("Failed to connect to %s: %s", addrbuf
, error_msg
);
662 logevent(telnet
->frontend
, msg
);
665 static int telnet_closing(Plug plug
, const char *error_msg
, int error_code
,
668 Telnet telnet
= (Telnet
) plug
;
671 * We don't implement independent EOF in each direction for Telnet
672 * connections; as soon as we get word that the remote side has
673 * sent us EOF, we wind up the whole connection.
679 notify_remote_exit(telnet
->frontend
);
682 logevent(telnet
->frontend
, error_msg
);
683 connection_fatal(telnet
->frontend
, "%s", error_msg
);
685 /* Otherwise, the remote side closed the connection normally. */
689 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
691 Telnet telnet
= (Telnet
) plug
;
693 telnet
->in_synch
= TRUE
;
694 do_telnet_read(telnet
, data
, len
);
698 static void telnet_sent(Plug plug
, int bufsize
)
700 Telnet telnet
= (Telnet
) plug
;
701 telnet
->bufsize
= bufsize
;
705 * Called to set up the Telnet connection.
707 * Returns an error message, or NULL on success.
709 * Also places the canonical host name into `realhost'. It must be
710 * freed by the caller.
712 static const char *telnet_init(void *frontend_handle
, void **backend_handle
,
713 Conf
*conf
, char *host
, int port
,
714 char **realhost
, int nodelay
, int keepalive
)
716 static const struct plug_function_table fn_table
= {
728 telnet
= snew(struct telnet_tag
);
729 telnet
->fn
= &fn_table
;
730 telnet
->conf
= conf_copy(conf
);
732 telnet
->echoing
= TRUE
;
733 telnet
->editing
= TRUE
;
734 telnet
->activated
= FALSE
;
735 telnet
->sb_buf
= NULL
;
737 telnet
->frontend
= frontend_handle
;
738 telnet
->term_width
= conf_get_int(telnet
->conf
, CONF_width
);
739 telnet
->term_height
= conf_get_int(telnet
->conf
, CONF_height
);
740 telnet
->state
= TOP_LEVEL
;
741 telnet
->ldisc
= NULL
;
742 telnet
->pinger
= NULL
;
743 *backend_handle
= telnet
;
750 addressfamily
= conf_get_int(telnet
->conf
, CONF_addressfamily
);
751 buf
= dupprintf("Looking up host \"%s\"%s", host
,
752 (addressfamily
== ADDRTYPE_IPV4 ?
" (IPv4)" :
753 (addressfamily
== ADDRTYPE_IPV6 ?
" (IPv6)" :
755 logevent(telnet
->frontend
, buf
);
758 addr
= name_lookup(host
, port
, realhost
, telnet
->conf
, addressfamily
);
759 if ((err
= sk_addr_error(addr
)) != NULL
) {
765 port
= 23; /* default telnet port */
770 telnet
->s
= new_connection(addr
, *realhost
, port
, 0, 1,
771 nodelay
, keepalive
, (Plug
) telnet
, telnet
->conf
);
772 if ((err
= sk_socket_error(telnet
->s
)) != NULL
)
775 telnet
->pinger
= pinger_new(telnet
->conf
, &telnet_backend
, telnet
);
778 * Initialise option states.
780 if (conf_get_int(telnet
->conf
, CONF_passive_telnet
)) {
781 const struct Opt
*const *o
;
783 for (o
= opts
; *o
; o
++)
784 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
786 const struct Opt
*const *o
;
788 for (o
= opts
; *o
; o
++) {
789 telnet
->opt_states
[(*o
)->index
] = (*o
)->initial_state
;
790 if (telnet
->opt_states
[(*o
)->index
] == REQUESTED
)
791 send_opt(telnet
, (*o
)->send
, (*o
)->option
);
793 telnet
->activated
= TRUE
;
797 * Set up SYNCH state.
799 telnet
->in_synch
= FALSE
;
802 * We can send special commands from the start.
804 update_specials_menu(telnet
->frontend
);
807 * loghost overrides realhost, if specified.
809 loghost
= conf_get_str(telnet
->conf
, CONF_loghost
);
814 *realhost
= dupstr(loghost
);
815 colon
= strrchr(*realhost
, ':');
818 * FIXME: if we ever update this aspect of ssh.c for
819 * IPv6 literal management, this should change in line
829 static void telnet_free(void *handle
)
831 Telnet telnet
= (Telnet
) handle
;
833 sfree(telnet
->sb_buf
);
837 pinger_free(telnet
->pinger
);
838 conf_free(telnet
->conf
);
842 * Reconfigure the Telnet backend. There's no immediate action
843 * necessary, in this backend: we just save the fresh config for
844 * any subsequent negotiations.
846 static void telnet_reconfig(void *handle
, Conf
*conf
)
848 Telnet telnet
= (Telnet
) handle
;
849 pinger_reconfig(telnet
->pinger
, telnet
->conf
, conf
);
850 conf_free(telnet
->conf
);
851 telnet
->conf
= conf_copy(conf
);
855 * Called to send data down the Telnet connection.
857 static int telnet_send(void *handle
, char *buf
, int len
)
859 Telnet telnet
= (Telnet
) handle
;
860 unsigned char *p
, *end
;
861 static const unsigned char iac
[2] = { IAC
, IAC
};
862 static const unsigned char cr
[2] = { CR
, NUL
};
864 static const unsigned char nl
[2] = { CR
, LF
};
867 if (telnet
->s
== NULL
)
870 p
= (unsigned char *)buf
;
871 end
= (unsigned char *)(buf
+ len
);
873 unsigned char *q
= p
;
875 while (p
< end
&& iswritable(*p
))
877 telnet
->bufsize
= sk_write(telnet
->s
, (char *)q
, p
- q
);
879 while (p
< end
&& !iswritable(*p
)) {
881 sk_write(telnet
->s
, (char *)(*p
== IAC ? iac
: cr
), 2);
886 return telnet
->bufsize
;
890 * Called to query the current socket sendability status.
892 static int telnet_sendbuffer(void *handle
)
894 Telnet telnet
= (Telnet
) handle
;
895 return telnet
->bufsize
;
899 * Called to set the size of the window from Telnet's POV.
901 static void telnet_size(void *handle
, int width
, int height
)
903 Telnet telnet
= (Telnet
) handle
;
908 telnet
->term_width
= width
;
909 telnet
->term_height
= height
;
911 if (telnet
->s
== NULL
|| telnet
->opt_states
[o_naws
.index
] != ACTIVE
)
916 b
[n
++] = TELOPT_NAWS
;
917 b
[n
++] = telnet
->term_width
>> 8;
918 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
919 b
[n
++] = telnet
->term_width
& 0xFF;
920 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
921 b
[n
++] = telnet
->term_height
>> 8;
922 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
923 b
[n
++] = telnet
->term_height
& 0xFF;
924 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
927 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
928 logbuf
= dupprintf("client:\tSB NAWS %d,%d",
929 telnet
->term_width
, telnet
->term_height
);
930 logevent(telnet
->frontend
, logbuf
);
935 * Send Telnet special codes.
937 static void telnet_special(void *handle
, Telnet_Special code
)
939 Telnet telnet
= (Telnet
) handle
;
942 if (telnet
->s
== NULL
)
949 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
953 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
957 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
961 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
965 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
969 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
973 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
977 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
981 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
985 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
989 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
993 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
996 /* In BINARY mode, CR-LF becomes just CR -
997 * and without the NUL suffix too. */
998 if (telnet
->opt_states
[o_we_bin
.index
] == ACTIVE
)
999 telnet
->bufsize
= sk_write(telnet
->s
, "\r", 1);
1001 telnet
->bufsize
= sk_write(telnet
->s
, "\r\n", 2);
1005 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 1);
1006 telnet
->bufsize
= sk_write_oob(telnet
->s
, (char *)(b
+ 1), 1);
1009 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
||
1010 telnet
->opt_states
[o_echo
.index
] == REALLY_INACTIVE
) {
1011 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
1012 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
1016 if (telnet
->opt_states
[o_echo
.index
] == ACTIVE
) {
1017 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
1018 send_opt(telnet
, o_echo
.nsend
, o_echo
.option
);
1022 if (telnet
->opt_states
[o_they_sga
.index
] == ACTIVE
) {
1024 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
1028 break; /* never heard of it */
1032 static const struct telnet_special
*telnet_get_specials(void *handle
)
1034 static const struct telnet_special specials
[] = {
1035 {"Are You There", TS_AYT
},
1037 {"Synch", TS_SYNCH
},
1038 {"Erase Character", TS_EC
},
1039 {"Erase Line", TS_EL
},
1040 {"Go Ahead", TS_GA
},
1041 {"No Operation", TS_NOP
},
1043 {"Abort Process", TS_ABORT
},
1044 {"Abort Output", TS_AO
},
1045 {"Interrupt Process", TS_IP
},
1046 {"Suspend Process", TS_SUSP
},
1048 {"End Of Record", TS_EOR
},
1049 {"End Of File", TS_EOF
},
1055 static int telnet_connected(void *handle
)
1057 Telnet telnet
= (Telnet
) handle
;
1058 return telnet
->s
!= NULL
;
1061 static int telnet_sendok(void *handle
)
1063 /* Telnet telnet = (Telnet) handle; */
1067 static void telnet_unthrottle(void *handle
, int backlog
)
1069 Telnet telnet
= (Telnet
) handle
;
1070 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
1073 static int telnet_ldisc(void *handle
, int option
)
1075 Telnet telnet
= (Telnet
) handle
;
1076 if (option
== LD_ECHO
)
1077 return telnet
->echoing
;
1078 if (option
== LD_EDIT
)
1079 return telnet
->editing
;
1083 static void telnet_provide_ldisc(void *handle
, void *ldisc
)
1085 Telnet telnet
= (Telnet
) handle
;
1086 telnet
->ldisc
= ldisc
;
1089 static void telnet_provide_logctx(void *handle
, void *logctx
)
1091 /* This is a stub. */
1094 static int telnet_exitcode(void *handle
)
1096 Telnet telnet
= (Telnet
) handle
;
1097 if (telnet
->s
!= NULL
)
1098 return -1; /* still connected */
1100 /* Telnet doesn't transmit exit codes back to the client */
1105 * cfg_info for Telnet does nothing at all.
1107 static int telnet_cfg_info(void *handle
)
1112 Backend telnet_backend
= {
1120 telnet_get_specials
,
1125 telnet_provide_ldisc
,
1126 telnet_provide_logctx
,