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