13 #define IAC 255 /* interpret as command: */
14 #define DONT 254 /* you are not to use option */
15 #define DO 253 /* please, you use option */
16 #define WONT 252 /* I won't use option */
17 #define WILL 251 /* I will use option */
18 #define SB 250 /* interpret as subnegotiation */
19 #define SE 240 /* end sub negotiation */
21 #define GA 249 /* you may reverse the line */
22 #define EL 248 /* erase the current line */
23 #define EC 247 /* erase the current character */
24 #define AYT 246 /* are you there */
25 #define AO 245 /* abort output--but let prog finish */
26 #define IP 244 /* interrupt process--permanently */
27 #define BREAK 243 /* break */
28 #define DM 242 /* data mark--for connect. cleaning */
29 #define NOP 241 /* nop */
30 #define EOR 239 /* end of record (transparent mode) */
31 #define ABORT 238 /* Abort process */
32 #define SUSP 237 /* Suspend process */
33 #define xEOF 236 /* End of file: EOF is already used... */
35 #define TELOPT_BINARY 0 /* 8-bit data path */
36 #define TELOPT_ECHO 1 /* echo */
37 #define TELOPT_RCP 2 /* prepare to reconnect */
38 #define TELOPT_SGA 3 /* suppress go ahead */
39 #define TELOPT_NAMS 4 /* approximate message size */
40 #define TELOPT_STATUS 5 /* give status */
41 #define TELOPT_TM 6 /* timing mark */
42 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
43 #define TELOPT_NAOL 8 /* negotiate about output line width */
44 #define TELOPT_NAOP 9 /* negotiate about output page size */
45 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
46 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
47 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
48 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
49 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
50 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
51 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
52 #define TELOPT_XASCII 17 /* extended ascic character set */
53 #define TELOPT_LOGOUT 18 /* force logout */
54 #define TELOPT_BM 19 /* byte macro */
55 #define TELOPT_DET 20 /* data entry terminal */
56 #define TELOPT_SUPDUP 21 /* supdup protocol */
57 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
58 #define TELOPT_SNDLOC 23 /* send location */
59 #define TELOPT_TTYPE 24 /* terminal type */
60 #define TELOPT_EOR 25 /* end or record */
61 #define TELOPT_TUID 26 /* TACACS user identification */
62 #define TELOPT_OUTMRK 27 /* output marking */
63 #define TELOPT_TTYLOC 28 /* terminal location number */
64 #define TELOPT_3270REGIME 29 /* 3270 regime */
65 #define TELOPT_X3PAD 30 /* X.3 PAD */
66 #define TELOPT_NAWS 31 /* window size */
67 #define TELOPT_TSPEED 32 /* terminal speed */
68 #define TELOPT_LFLOW 33 /* remote flow control */
69 #define TELOPT_LINEMODE 34 /* Linemode option */
70 #define TELOPT_XDISPLOC 35 /* X Display Location */
71 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
72 #define TELOPT_AUTHENTICATION 37 /* Authenticate */
73 #define TELOPT_ENCRYPT 38 /* Encryption option */
74 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
75 #define TELOPT_TN3270E 40 /* TN3270 enhancements */
76 #define TELOPT_XAUTH 41
77 #define TELOPT_CHARSET 42 /* Character set */
78 #define TELOPT_RSP 43 /* Remote serial port */
79 #define TELOPT_COM_PORT_OPTION 44 /* Com port control */
80 #define TELOPT_SLE 45 /* Suppress local echo */
81 #define TELOPT_STARTTLS 46 /* Start TLS */
82 #define TELOPT_KERMIT 47 /* Automatic Kermit file transfer */
83 #define TELOPT_SEND_URL 48
84 #define TELOPT_FORWARD_X 49
85 #define TELOPT_PRAGMA_LOGON 138
86 #define TELOPT_SSPI_LOGON 139
87 #define TELOPT_PRAGMA_HEARTBEAT 140
88 #define TELOPT_EXOPL 255 /* extended-options-list */
90 #define TELQUAL_IS 0 /* option is... */
91 #define TELQUAL_SEND 1 /* send option */
92 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
102 #define iswritable(x) \
104 (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
106 static char *telopt(int opt
)
108 #define i(x) if (opt == TELOPT_ ## x) return #x;
166 static void telnet_size(void *handle
, int width
, int height
);
169 int send
; /* what we initially send */
170 int nsend
; /* -ve send if requested to stop it */
171 int ack
, nak
; /* +ve and -ve acknowledgements */
172 int option
; /* the option code */
173 int index
; /* index into telnet->opt_states[] */
175 REQUESTED
, ACTIVE
, INACTIVE
, REALLY_INACTIVE
193 static const struct Opt o_naws
=
194 { WILL
, WONT
, DO
, DONT
, TELOPT_NAWS
, OPTINDEX_NAWS
, REQUESTED
};
195 static const struct Opt o_tspeed
=
196 { WILL
, WONT
, DO
, DONT
, TELOPT_TSPEED
, OPTINDEX_TSPEED
, REQUESTED
};
197 static const struct Opt o_ttype
=
198 { WILL
, WONT
, DO
, DONT
, TELOPT_TTYPE
, OPTINDEX_TTYPE
, REQUESTED
};
199 static const struct Opt o_oenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_OLD_ENVIRON
,
200 OPTINDEX_OENV
, INACTIVE
202 static const struct Opt o_nenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_NEW_ENVIRON
,
203 OPTINDEX_NENV
, REQUESTED
205 static const struct Opt o_echo
=
206 { DO
, DONT
, WILL
, WONT
, TELOPT_ECHO
, OPTINDEX_ECHO
, REQUESTED
};
207 static const struct Opt o_we_sga
=
208 { WILL
, WONT
, DO
, DONT
, TELOPT_SGA
, OPTINDEX_WE_SGA
, REQUESTED
};
209 static const struct Opt o_they_sga
=
210 { DO
, DONT
, WILL
, WONT
, TELOPT_SGA
, OPTINDEX_THEY_SGA
, REQUESTED
};
211 static const struct Opt o_we_bin
=
212 { WILL
, WONT
, DO
, DONT
, TELOPT_BINARY
, OPTINDEX_WE_BIN
, INACTIVE
};
213 static const struct Opt o_they_bin
=
214 { DO
, DONT
, WILL
, WONT
, TELOPT_BINARY
, OPTINDEX_THEY_BIN
, INACTIVE
};
216 static const struct Opt
*const opts
[] = {
217 &o_naws
, &o_tspeed
, &o_ttype
, &o_oenv
, &o_nenv
, &o_echo
,
218 &o_we_sga
, &o_they_sga
, &o_we_bin
, &o_they_bin
, NULL
221 typedef struct telnet_tag
{
222 const struct plug_function_table
*fn
;
223 /* the above field _must_ be first in the structure */
229 int term_width
, term_height
;
231 int opt_states
[NUM_OPTS
];
233 int echoing
, editing
;
238 unsigned char *sb_buf
;
242 TOP_LEVEL
, SEENIAC
, SEENWILL
, SEENWONT
, SEENDO
, SEENDONT
,
243 SEENSB
, SUBNEGOT
, SUBNEG_IAC
, SEENCR
251 #define TELNET_MAX_BACKLOG 4096
253 #define SB_DELTA 1024
255 static void c_write1(Telnet telnet
, int c
)
259 backlog
= from_backend(telnet
->frontend
, 0, &cc
, 1);
260 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
263 static void log_option(Telnet telnet
, char *sender
, int cmd
, int option
)
267 * The strange-looking "<?""?>" below is there to avoid a
268 * trigraph - a double question mark followed by > maps to a
269 * closing brace character!
271 buf
= dupprintf("%s:\t%s %s", sender
,
272 (cmd
== WILL ?
"WILL" : cmd
== WONT ?
"WONT" :
273 cmd
== DO ?
"DO" : cmd
== DONT ?
"DONT" : "<?""?>"),
275 logevent(telnet
->frontend
, buf
);
279 static void send_opt(Telnet telnet
, int cmd
, int option
)
286 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 3);
287 log_option(telnet
, "client", cmd
, option
);
290 static void deactivate_option(Telnet telnet
, const struct Opt
*o
)
292 if (telnet
->opt_states
[o
->index
] == REQUESTED
||
293 telnet
->opt_states
[o
->index
] == ACTIVE
)
294 send_opt(telnet
, o
->nsend
, o
->option
);
295 telnet
->opt_states
[o
->index
] = REALLY_INACTIVE
;
299 * Generate side effects of enabling or disabling an option.
301 static void option_side_effects(Telnet telnet
, const struct Opt
*o
, int enabled
)
303 if (o
->option
== TELOPT_ECHO
&& o
->send
== DO
)
304 telnet
->echoing
= !enabled
;
305 else if (o
->option
== TELOPT_SGA
&& o
->send
== DO
)
306 telnet
->editing
= !enabled
;
307 if (telnet
->ldisc
) /* cause ldisc to notice the change */
308 ldisc_send(telnet
->ldisc
, NULL
, 0, 0);
310 /* Ensure we get the minimum options */
311 if (!telnet
->activated
) {
312 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
) {
313 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
314 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
316 if (telnet
->opt_states
[o_we_sga
.index
] == INACTIVE
) {
317 telnet
->opt_states
[o_we_sga
.index
] = REQUESTED
;
318 send_opt(telnet
, o_we_sga
.send
, o_we_sga
.option
);
320 if (telnet
->opt_states
[o_they_sga
.index
] == INACTIVE
) {
321 telnet
->opt_states
[o_they_sga
.index
] = REQUESTED
;
322 send_opt(telnet
, o_they_sga
.send
, o_they_sga
.option
);
324 telnet
->activated
= TRUE
;
328 static void activate_option(Telnet telnet
, const struct Opt
*o
)
330 if (o
->send
== WILL
&& o
->option
== TELOPT_NAWS
)
331 telnet_size(telnet
, telnet
->term_width
, telnet
->term_height
);
332 if (o
->send
== WILL
&&
333 (o
->option
== TELOPT_NEW_ENVIRON
||
334 o
->option
== TELOPT_OLD_ENVIRON
)) {
336 * We may only have one kind of ENVIRON going at a time.
337 * This is a hack, but who cares.
339 deactivate_option(telnet
, o
->option
==
340 TELOPT_NEW_ENVIRON ?
&o_oenv
: &o_nenv
);
342 option_side_effects(telnet
, o
, 1);
345 static void refused_option(Telnet telnet
, const struct Opt
*o
)
347 if (o
->send
== WILL
&& o
->option
== TELOPT_NEW_ENVIRON
&&
348 telnet
->opt_states
[o_oenv
.index
] == INACTIVE
) {
349 send_opt(telnet
, WILL
, TELOPT_OLD_ENVIRON
);
350 telnet
->opt_states
[o_oenv
.index
] = REQUESTED
;
352 option_side_effects(telnet
, o
, 0);
355 static void proc_rec_opt(Telnet telnet
, int cmd
, int option
)
357 const struct Opt
*const *o
;
359 log_option(telnet
, "server", cmd
, option
);
360 for (o
= opts
; *o
; o
++) {
361 if ((*o
)->option
== option
&& (*o
)->ack
== cmd
) {
362 switch (telnet
->opt_states
[(*o
)->index
]) {
364 telnet
->opt_states
[(*o
)->index
] = ACTIVE
;
365 activate_option(telnet
, *o
);
370 telnet
->opt_states
[(*o
)->index
] = ACTIVE
;
371 send_opt(telnet
, (*o
)->send
, option
);
372 activate_option(telnet
, *o
);
374 case REALLY_INACTIVE
:
375 send_opt(telnet
, (*o
)->nsend
, option
);
379 } else if ((*o
)->option
== option
&& (*o
)->nak
== cmd
) {
380 switch (telnet
->opt_states
[(*o
)->index
]) {
382 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
383 refused_option(telnet
, *o
);
386 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
387 send_opt(telnet
, (*o
)->nsend
, option
);
388 option_side_effects(telnet
, *o
, 0);
391 case REALLY_INACTIVE
:
398 * If we reach here, the option was one we weren't prepared to
399 * cope with. So send a negative ack.
401 send_opt(telnet
, (cmd
== WILL ? DONT
: WONT
), option
);
404 static void process_subneg(Telnet telnet
)
406 unsigned char b
[2048], *p
, *q
;
410 switch (telnet
->sb_opt
) {
412 if (telnet
->sb_len
== 1 && telnet
->sb_buf
[0] == TELQUAL_SEND
) {
416 b
[2] = TELOPT_TSPEED
;
418 strcpy((char *)(b
+ 4), telnet
->cfg
.termspeed
);
419 n
= 4 + strlen(telnet
->cfg
.termspeed
);
422 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
+ 2);
423 logevent(telnet
->frontend
, "server:\tSB TSPEED SEND");
424 logbuf
= dupprintf("client:\tSB TSPEED IS %s", telnet
->cfg
.termspeed
);
425 logevent(telnet
->frontend
, logbuf
);
428 logevent(telnet
->frontend
, "server:\tSB TSPEED <something weird>");
431 if (telnet
->sb_len
== 1 && telnet
->sb_buf
[0] == TELQUAL_SEND
) {
437 for (n
= 0; telnet
->cfg
.termtype
[n
]; n
++)
438 b
[n
+ 4] = (telnet
->cfg
.termtype
[n
] >= 'a'
439 && telnet
->cfg
.termtype
[n
] <=
440 'z' ? telnet
->cfg
.termtype
[n
] + 'A' -
441 'a' : telnet
->cfg
.termtype
[n
]);
444 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
+ 6);
446 logevent(telnet
->frontend
, "server:\tSB TTYPE SEND");
447 logbuf
= dupprintf("client:\tSB TTYPE IS %s", b
+ 4);
448 logevent(telnet
->frontend
, logbuf
);
451 logevent(telnet
->frontend
, "server:\tSB TTYPE <something weird>\r\n");
453 case TELOPT_OLD_ENVIRON
:
454 case TELOPT_NEW_ENVIRON
:
456 q
= p
+ telnet
->sb_len
;
457 if (p
< q
&& *p
== TELQUAL_SEND
) {
460 logbuf
= dupprintf("server:\tSB %s SEND", telopt(telnet
->sb_opt
));
461 logevent(telnet
->frontend
, logbuf
);
463 if (telnet
->sb_opt
== TELOPT_OLD_ENVIRON
) {
464 if (telnet
->cfg
.rfc_environ
) {
472 * Try to guess the sense of VAR and VALUE.
478 } else if (*p
== BSD_VAR
) {
486 * With NEW_ENVIRON, the sense of VAR and VALUE
494 b
[2] = telnet
->sb_opt
;
497 e
= telnet
->cfg
.environmt
;
500 while (*e
&& *e
!= '\t')
509 if (*telnet
->cfg
.username
) {
516 e
= telnet
->cfg
.username
;
522 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
523 logbuf
= dupprintf("client:\tSB %s IS %s%s%s%s",
524 telopt(telnet
->sb_opt
),
525 *telnet
->cfg
.username ?
"USER=" : "",
526 telnet
->cfg
.username
,
527 *telnet
->cfg
.username ?
" " : "",
528 n
== 6 ?
"<nothing>" :
529 (*telnet
->cfg
.environmt ?
"<stuff>" : ""));
530 logevent(telnet
->frontend
, logbuf
);
537 static void do_telnet_read(Telnet telnet
, char *buf
, int len
)
541 int c
= (unsigned char) *buf
++;
543 switch (telnet
->state
) {
546 if (c
== NUL
&& telnet
->state
== SEENCR
)
547 telnet
->state
= TOP_LEVEL
;
549 telnet
->state
= SEENIAC
;
551 if (!telnet
->in_synch
)
555 /* I can't get the F***ing winsock to insert the urgent IAC
556 * into the right position! Even with SO_OOBINLINE it gives
557 * it to recv too soon. And of course the DM byte (that
558 * arrives in the same packet!) appears several K later!!
560 * Oh well, we do get the DM in the right place so I'll
561 * just stop hiding on the next 0xf2 and hope for the best.
564 telnet
->in_synch
= 0;
566 if (c
== CR
&& telnet
->opt_states
[o_they_bin
.index
] != ACTIVE
)
567 telnet
->state
= SEENCR
;
569 telnet
->state
= TOP_LEVEL
;
574 telnet
->state
= SEENDO
;
576 telnet
->state
= SEENDONT
;
578 telnet
->state
= SEENWILL
;
580 telnet
->state
= SEENWONT
;
582 telnet
->state
= SEENSB
;
584 telnet
->in_synch
= 0;
585 telnet
->state
= TOP_LEVEL
;
587 /* ignore everything else; print it if it's IAC */
591 telnet
->state
= TOP_LEVEL
;
595 proc_rec_opt(telnet
, WILL
, c
);
596 telnet
->state
= TOP_LEVEL
;
599 proc_rec_opt(telnet
, WONT
, c
);
600 telnet
->state
= TOP_LEVEL
;
603 proc_rec_opt(telnet
, DO
, c
);
604 telnet
->state
= TOP_LEVEL
;
607 proc_rec_opt(telnet
, DONT
, c
);
608 telnet
->state
= TOP_LEVEL
;
613 telnet
->state
= SUBNEGOT
;
617 telnet
->state
= SUBNEG_IAC
;
620 if (telnet
->sb_len
>= telnet
->sb_size
) {
621 telnet
->sb_size
+= SB_DELTA
;
622 telnet
->sb_buf
= sresize(telnet
->sb_buf
, telnet
->sb_size
,
625 telnet
->sb_buf
[telnet
->sb_len
++] = c
;
626 telnet
->state
= SUBNEGOT
; /* in case we came here by goto */
631 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
633 process_subneg(telnet
);
634 telnet
->state
= TOP_LEVEL
;
641 static void telnet_log(Plug plug
, int type
, SockAddr addr
, int port
,
642 const char *error_msg
, int error_code
)
644 Telnet telnet
= (Telnet
) plug
;
645 char addrbuf
[256], *msg
;
647 sk_getaddr(addr
, addrbuf
, lenof(addrbuf
));
650 msg
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
652 msg
= dupprintf("Failed to connect to %s: %s", addrbuf
, error_msg
);
654 logevent(telnet
->frontend
, msg
);
657 static int telnet_closing(Plug plug
, const char *error_msg
, int error_code
,
660 Telnet telnet
= (Telnet
) plug
;
665 notify_remote_exit(telnet
->frontend
);
668 logevent(telnet
->frontend
, error_msg
);
669 connection_fatal(telnet
->frontend
, "%s", error_msg
);
671 /* Otherwise, the remote side closed the connection normally. */
675 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
677 Telnet telnet
= (Telnet
) plug
;
679 telnet
->in_synch
= TRUE
;
680 do_telnet_read(telnet
, data
, len
);
684 static void telnet_sent(Plug plug
, int bufsize
)
686 Telnet telnet
= (Telnet
) plug
;
687 telnet
->bufsize
= bufsize
;
691 * Called to set up the Telnet connection.
693 * Returns an error message, or NULL on success.
695 * Also places the canonical host name into `realhost'. It must be
696 * freed by the caller.
698 static const char *telnet_init(void *frontend_handle
, void **backend_handle
,
700 char *host
, int port
, char **realhost
,
701 int nodelay
, int keepalive
)
703 static const struct plug_function_table fn_table
= {
713 telnet
= snew(struct telnet_tag
);
714 telnet
->fn
= &fn_table
;
715 telnet
->cfg
= *cfg
; /* STRUCTURE COPY */
717 telnet
->echoing
= TRUE
;
718 telnet
->editing
= TRUE
;
719 telnet
->activated
= FALSE
;
720 telnet
->sb_buf
= NULL
;
722 telnet
->frontend
= frontend_handle
;
723 telnet
->term_width
= telnet
->cfg
.width
;
724 telnet
->term_height
= telnet
->cfg
.height
;
725 telnet
->state
= TOP_LEVEL
;
726 telnet
->ldisc
= NULL
;
727 telnet
->pinger
= NULL
;
728 *backend_handle
= telnet
;
735 buf
= dupprintf("Looking up host \"%s\"%s", host
,
736 (cfg
->addressfamily
== ADDRTYPE_IPV4 ?
" (IPv4)" :
737 (cfg
->addressfamily
== ADDRTYPE_IPV6 ?
" (IPv6)" :
739 logevent(telnet
->frontend
, buf
);
742 addr
= name_lookup(host
, port
, realhost
, &telnet
->cfg
, cfg
->addressfamily
);
743 if ((err
= sk_addr_error(addr
)) != NULL
) {
749 port
= 23; /* default telnet port */
754 telnet
->s
= new_connection(addr
, *realhost
, port
, 0, 1,
755 nodelay
, keepalive
, (Plug
) telnet
, &telnet
->cfg
);
756 if ((err
= sk_socket_error(telnet
->s
)) != NULL
)
759 telnet
->pinger
= pinger_new(&telnet
->cfg
, &telnet_backend
, telnet
);
762 * Initialise option states.
764 if (telnet
->cfg
.passive_telnet
) {
765 const struct Opt
*const *o
;
767 for (o
= opts
; *o
; o
++)
768 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
770 const struct Opt
*const *o
;
772 for (o
= opts
; *o
; o
++) {
773 telnet
->opt_states
[(*o
)->index
] = (*o
)->initial_state
;
774 if (telnet
->opt_states
[(*o
)->index
] == REQUESTED
)
775 send_opt(telnet
, (*o
)->send
, (*o
)->option
);
777 telnet
->activated
= TRUE
;
781 * Set up SYNCH state.
783 telnet
->in_synch
= FALSE
;
786 * We can send special commands from the start.
788 update_specials_menu(telnet
->frontend
);
793 static void telnet_free(void *handle
)
795 Telnet telnet
= (Telnet
) handle
;
797 sfree(telnet
->sb_buf
);
801 pinger_free(telnet
->pinger
);
805 * Reconfigure the Telnet backend. There's no immediate action
806 * necessary, in this backend: we just save the fresh config for
807 * any subsequent negotiations.
809 static void telnet_reconfig(void *handle
, Config
*cfg
)
811 Telnet telnet
= (Telnet
) handle
;
812 pinger_reconfig(telnet
->pinger
, &telnet
->cfg
, cfg
);
813 telnet
->cfg
= *cfg
; /* STRUCTURE COPY */
817 * Called to send data down the Telnet connection.
819 static int telnet_send(void *handle
, char *buf
, int len
)
821 Telnet telnet
= (Telnet
) handle
;
822 unsigned char *p
, *end
;
823 static const unsigned char iac
[2] = { IAC
, IAC
};
824 static const unsigned char cr
[2] = { CR
, NUL
};
826 static const unsigned char nl
[2] = { CR
, LF
};
829 if (telnet
->s
== NULL
)
832 p
= (unsigned char *)buf
;
833 end
= (unsigned char *)(buf
+ len
);
835 unsigned char *q
= p
;
837 while (p
< end
&& iswritable(*p
))
839 telnet
->bufsize
= sk_write(telnet
->s
, (char *)q
, p
- q
);
841 while (p
< end
&& !iswritable(*p
)) {
843 sk_write(telnet
->s
, (char *)(*p
== IAC ? iac
: cr
), 2);
848 return telnet
->bufsize
;
852 * Called to query the current socket sendability status.
854 static int telnet_sendbuffer(void *handle
)
856 Telnet telnet
= (Telnet
) handle
;
857 return telnet
->bufsize
;
861 * Called to set the size of the window from Telnet's POV.
863 static void telnet_size(void *handle
, int width
, int height
)
865 Telnet telnet
= (Telnet
) handle
;
870 telnet
->term_width
= width
;
871 telnet
->term_height
= height
;
873 if (telnet
->s
== NULL
|| telnet
->opt_states
[o_naws
.index
] != ACTIVE
)
878 b
[n
++] = TELOPT_NAWS
;
879 b
[n
++] = telnet
->term_width
>> 8;
880 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
881 b
[n
++] = telnet
->term_width
& 0xFF;
882 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
883 b
[n
++] = telnet
->term_height
>> 8;
884 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
885 b
[n
++] = telnet
->term_height
& 0xFF;
886 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
889 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
890 logbuf
= dupprintf("client:\tSB NAWS %d,%d",
891 telnet
->term_width
, telnet
->term_height
);
892 logevent(telnet
->frontend
, logbuf
);
897 * Send Telnet special codes.
899 static void telnet_special(void *handle
, Telnet_Special code
)
901 Telnet telnet
= (Telnet
) handle
;
904 if (telnet
->s
== NULL
)
911 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
915 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
919 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
923 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
927 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
931 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
935 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
939 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
943 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
947 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
951 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
955 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
958 /* In BINARY mode, CR-LF becomes just CR -
959 * and without the NUL suffix too. */
960 if (telnet
->opt_states
[o_we_bin
.index
] == ACTIVE
)
961 telnet
->bufsize
= sk_write(telnet
->s
, "\r", 1);
963 telnet
->bufsize
= sk_write(telnet
->s
, "\r\n", 2);
967 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 1);
968 telnet
->bufsize
= sk_write_oob(telnet
->s
, (char *)(b
+ 1), 1);
971 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
||
972 telnet
->opt_states
[o_echo
.index
] == REALLY_INACTIVE
) {
973 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
974 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
978 if (telnet
->opt_states
[o_echo
.index
] == ACTIVE
) {
979 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
980 send_opt(telnet
, o_echo
.nsend
, o_echo
.option
);
984 if (telnet
->opt_states
[o_they_sga
.index
] == ACTIVE
) {
986 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
990 break; /* never heard of it */
994 static const struct telnet_special
*telnet_get_specials(void *handle
)
996 static const struct telnet_special specials
[] = {
997 {"Are You There", TS_AYT
},
1000 {"Erase Character", TS_EC
},
1001 {"Erase Line", TS_EL
},
1002 {"Go Ahead", TS_GA
},
1003 {"No Operation", TS_NOP
},
1005 {"Abort Process", TS_ABORT
},
1006 {"Abort Output", TS_AO
},
1007 {"Interrupt Process", TS_IP
},
1008 {"Suspend Process", TS_SUSP
},
1010 {"End Of Record", TS_EOR
},
1011 {"End Of File", TS_EOF
},
1017 static Socket
telnet_socket(void *handle
)
1019 Telnet telnet
= (Telnet
) handle
;
1023 static int telnet_sendok(void *handle
)
1025 /* Telnet telnet = (Telnet) handle; */
1029 static void telnet_unthrottle(void *handle
, int backlog
)
1031 Telnet telnet
= (Telnet
) handle
;
1032 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
1035 static int telnet_ldisc(void *handle
, int option
)
1037 Telnet telnet
= (Telnet
) handle
;
1038 if (option
== LD_ECHO
)
1039 return telnet
->echoing
;
1040 if (option
== LD_EDIT
)
1041 return telnet
->editing
;
1045 static void telnet_provide_ldisc(void *handle
, void *ldisc
)
1047 Telnet telnet
= (Telnet
) handle
;
1048 telnet
->ldisc
= ldisc
;
1051 static void telnet_provide_logctx(void *handle
, void *logctx
)
1053 /* This is a stub. */
1056 static int telnet_exitcode(void *handle
)
1058 Telnet telnet
= (Telnet
) handle
;
1059 if (telnet
->s
!= NULL
)
1060 return -1; /* still connected */
1062 /* Telnet doesn't transmit exit codes back to the client */
1067 * cfg_info for Telnet does nothing at all.
1069 static int telnet_cfg_info(void *handle
)
1074 Backend telnet_backend
= {
1082 telnet_get_specials
,
1087 telnet_provide_ldisc
,
1088 telnet_provide_logctx
,