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
249 #define TELNET_MAX_BACKLOG 4096
251 #define SB_DELTA 1024
253 static void c_write1(Telnet telnet
, int c
)
257 backlog
= from_backend(telnet
->frontend
, 0, &cc
, 1);
258 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
261 static void log_option(Telnet telnet
, char *sender
, int cmd
, int option
)
265 * The strange-looking "<?""?>" below is there to avoid a
266 * trigraph - a double question mark followed by > maps to a
267 * closing brace character!
269 buf
= dupprintf("%s:\t%s %s", sender
,
270 (cmd
== WILL ?
"WILL" : cmd
== WONT ?
"WONT" :
271 cmd
== DO ?
"DO" : cmd
== DONT ?
"DONT" : "<?""?>"),
273 logevent(telnet
->frontend
, buf
);
277 static void send_opt(Telnet telnet
, int cmd
, int option
)
284 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 3);
285 log_option(telnet
, "client", cmd
, option
);
288 static void deactivate_option(Telnet telnet
, const struct Opt
*o
)
290 if (telnet
->opt_states
[o
->index
] == REQUESTED
||
291 telnet
->opt_states
[o
->index
] == ACTIVE
)
292 send_opt(telnet
, o
->nsend
, o
->option
);
293 telnet
->opt_states
[o
->index
] = REALLY_INACTIVE
;
297 * Generate side effects of enabling or disabling an option.
299 static void option_side_effects(Telnet telnet
, const struct Opt
*o
, int enabled
)
301 if (o
->option
== TELOPT_ECHO
&& o
->send
== DO
)
302 telnet
->echoing
= !enabled
;
303 else if (o
->option
== TELOPT_SGA
&& o
->send
== DO
)
304 telnet
->editing
= !enabled
;
305 if (telnet
->ldisc
) /* cause ldisc to notice the change */
306 ldisc_send(telnet
->ldisc
, NULL
, 0, 0);
308 /* Ensure we get the minimum options */
309 if (!telnet
->activated
) {
310 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
) {
311 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
312 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
314 if (telnet
->opt_states
[o_we_sga
.index
] == INACTIVE
) {
315 telnet
->opt_states
[o_we_sga
.index
] = REQUESTED
;
316 send_opt(telnet
, o_we_sga
.send
, o_we_sga
.option
);
318 if (telnet
->opt_states
[o_they_sga
.index
] == INACTIVE
) {
319 telnet
->opt_states
[o_they_sga
.index
] = REQUESTED
;
320 send_opt(telnet
, o_they_sga
.send
, o_they_sga
.option
);
322 telnet
->activated
= TRUE
;
326 static void activate_option(Telnet telnet
, const struct Opt
*o
)
328 if (o
->send
== WILL
&& o
->option
== TELOPT_NAWS
)
329 telnet_size(telnet
, telnet
->term_width
, telnet
->term_height
);
330 if (o
->send
== WILL
&&
331 (o
->option
== TELOPT_NEW_ENVIRON
||
332 o
->option
== TELOPT_OLD_ENVIRON
)) {
334 * We may only have one kind of ENVIRON going at a time.
335 * This is a hack, but who cares.
337 deactivate_option(telnet
, o
->option
==
338 TELOPT_NEW_ENVIRON ?
&o_oenv
: &o_nenv
);
340 option_side_effects(telnet
, o
, 1);
343 static void refused_option(Telnet telnet
, const struct Opt
*o
)
345 if (o
->send
== WILL
&& o
->option
== TELOPT_NEW_ENVIRON
&&
346 telnet
->opt_states
[o_oenv
.index
] == INACTIVE
) {
347 send_opt(telnet
, WILL
, TELOPT_OLD_ENVIRON
);
348 telnet
->opt_states
[o_oenv
.index
] = REQUESTED
;
350 option_side_effects(telnet
, o
, 0);
353 static void proc_rec_opt(Telnet telnet
, int cmd
, int option
)
355 const struct Opt
*const *o
;
357 log_option(telnet
, "server", cmd
, option
);
358 for (o
= opts
; *o
; o
++) {
359 if ((*o
)->option
== option
&& (*o
)->ack
== cmd
) {
360 switch (telnet
->opt_states
[(*o
)->index
]) {
362 telnet
->opt_states
[(*o
)->index
] = ACTIVE
;
363 activate_option(telnet
, *o
);
368 telnet
->opt_states
[(*o
)->index
] = ACTIVE
;
369 send_opt(telnet
, (*o
)->send
, option
);
370 activate_option(telnet
, *o
);
372 case REALLY_INACTIVE
:
373 send_opt(telnet
, (*o
)->nsend
, option
);
377 } else if ((*o
)->option
== option
&& (*o
)->nak
== cmd
) {
378 switch (telnet
->opt_states
[(*o
)->index
]) {
380 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
381 refused_option(telnet
, *o
);
384 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
385 send_opt(telnet
, (*o
)->nsend
, option
);
386 option_side_effects(telnet
, *o
, 0);
389 case REALLY_INACTIVE
:
396 * If we reach here, the option was one we weren't prepared to
397 * cope with. So send a negative ack.
399 send_opt(telnet
, (cmd
== WILL ? DONT
: WONT
), option
);
402 static void process_subneg(Telnet telnet
)
404 unsigned char b
[2048], *p
, *q
;
408 switch (telnet
->sb_opt
) {
410 if (telnet
->sb_len
== 1 && telnet
->sb_buf
[0] == TELQUAL_SEND
) {
414 b
[2] = TELOPT_TSPEED
;
416 strcpy((char *)(b
+ 4), telnet
->cfg
.termspeed
);
417 n
= 4 + strlen(telnet
->cfg
.termspeed
);
420 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
+ 2);
421 logevent(telnet
->frontend
, "server:\tSB TSPEED SEND");
422 logbuf
= dupprintf("client:\tSB TSPEED IS %s", telnet
->cfg
.termspeed
);
423 logevent(telnet
->frontend
, logbuf
);
426 logevent(telnet
->frontend
, "server:\tSB TSPEED <something weird>");
429 if (telnet
->sb_len
== 1 && telnet
->sb_buf
[0] == TELQUAL_SEND
) {
435 for (n
= 0; telnet
->cfg
.termtype
[n
]; n
++)
436 b
[n
+ 4] = (telnet
->cfg
.termtype
[n
] >= 'a'
437 && telnet
->cfg
.termtype
[n
] <=
438 'z' ? telnet
->cfg
.termtype
[n
] + 'A' -
439 'a' : telnet
->cfg
.termtype
[n
]);
442 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
+ 6);
444 logevent(telnet
->frontend
, "server:\tSB TTYPE SEND");
445 logbuf
= dupprintf("client:\tSB TTYPE IS %s", b
+ 4);
446 logevent(telnet
->frontend
, logbuf
);
449 logevent(telnet
->frontend
, "server:\tSB TTYPE <something weird>\r\n");
451 case TELOPT_OLD_ENVIRON
:
452 case TELOPT_NEW_ENVIRON
:
454 q
= p
+ telnet
->sb_len
;
455 if (p
< q
&& *p
== TELQUAL_SEND
) {
458 logbuf
= dupprintf("server:\tSB %s SEND", telopt(telnet
->sb_opt
));
459 logevent(telnet
->frontend
, logbuf
);
461 if (telnet
->sb_opt
== TELOPT_OLD_ENVIRON
) {
462 if (telnet
->cfg
.rfc_environ
) {
470 * Try to guess the sense of VAR and VALUE.
476 } else if (*p
== BSD_VAR
) {
484 * With NEW_ENVIRON, the sense of VAR and VALUE
492 b
[2] = telnet
->sb_opt
;
495 e
= telnet
->cfg
.environmt
;
498 while (*e
&& *e
!= '\t')
507 if (*telnet
->cfg
.username
) {
514 e
= telnet
->cfg
.username
;
520 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
521 logbuf
= dupprintf("client:\tSB %s IS %s%s%s%s",
522 telopt(telnet
->sb_opt
),
523 *telnet
->cfg
.username ?
"USER=" : "",
524 telnet
->cfg
.username
,
525 *telnet
->cfg
.username ?
" " : "",
526 n
== 6 ?
"<nothing>" :
527 (*telnet
->cfg
.environmt ?
"<stuff>" : ""));
528 logevent(telnet
->frontend
, logbuf
);
535 static void do_telnet_read(Telnet telnet
, char *buf
, int len
)
539 int c
= (unsigned char) *buf
++;
541 switch (telnet
->state
) {
544 if (c
== NUL
&& telnet
->state
== SEENCR
)
545 telnet
->state
= TOP_LEVEL
;
547 telnet
->state
= SEENIAC
;
549 if (!telnet
->in_synch
)
553 /* I can't get the F***ing winsock to insert the urgent IAC
554 * into the right position! Even with SO_OOBINLINE it gives
555 * it to recv too soon. And of course the DM byte (that
556 * arrives in the same packet!) appears several K later!!
558 * Oh well, we do get the DM in the right place so I'll
559 * just stop hiding on the next 0xf2 and hope for the best.
562 telnet
->in_synch
= 0;
564 if (c
== CR
&& telnet
->opt_states
[o_they_bin
.index
] != ACTIVE
)
565 telnet
->state
= SEENCR
;
567 telnet
->state
= TOP_LEVEL
;
572 telnet
->state
= SEENDO
;
574 telnet
->state
= SEENDONT
;
576 telnet
->state
= SEENWILL
;
578 telnet
->state
= SEENWONT
;
580 telnet
->state
= SEENSB
;
582 telnet
->in_synch
= 0;
583 telnet
->state
= TOP_LEVEL
;
585 /* ignore everything else; print it if it's IAC */
589 telnet
->state
= TOP_LEVEL
;
593 proc_rec_opt(telnet
, WILL
, c
);
594 telnet
->state
= TOP_LEVEL
;
597 proc_rec_opt(telnet
, WONT
, c
);
598 telnet
->state
= TOP_LEVEL
;
601 proc_rec_opt(telnet
, DO
, c
);
602 telnet
->state
= TOP_LEVEL
;
605 proc_rec_opt(telnet
, DONT
, c
);
606 telnet
->state
= TOP_LEVEL
;
611 telnet
->state
= SUBNEGOT
;
615 telnet
->state
= SUBNEG_IAC
;
618 if (telnet
->sb_len
>= telnet
->sb_size
) {
619 telnet
->sb_size
+= SB_DELTA
;
620 telnet
->sb_buf
= sresize(telnet
->sb_buf
, telnet
->sb_size
,
623 telnet
->sb_buf
[telnet
->sb_len
++] = c
;
624 telnet
->state
= SUBNEGOT
; /* in case we came here by goto */
629 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
631 process_subneg(telnet
);
632 telnet
->state
= TOP_LEVEL
;
639 static int telnet_closing(Plug plug
, const char *error_msg
, int error_code
,
642 Telnet telnet
= (Telnet
) plug
;
649 /* A socket error has occurred. */
650 logevent(telnet
->frontend
, error_msg
);
651 connection_fatal(telnet
->frontend
, "%s", error_msg
);
652 } /* Otherwise, the remote side closed the connection normally. */
656 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
658 Telnet telnet
= (Telnet
) plug
;
660 telnet
->in_synch
= TRUE
;
661 do_telnet_read(telnet
, data
, len
);
665 static void telnet_sent(Plug plug
, int bufsize
)
667 Telnet telnet
= (Telnet
) plug
;
668 telnet
->bufsize
= bufsize
;
672 * Called to set up the Telnet connection.
674 * Returns an error message, or NULL on success.
676 * Also places the canonical host name into `realhost'. It must be
677 * freed by the caller.
679 static const char *telnet_init(void *frontend_handle
, void **backend_handle
,
681 char *host
, int port
, char **realhost
,
682 int nodelay
, int keepalive
)
684 static const struct plug_function_table fn_table
= {
693 telnet
= snew(struct telnet_tag
);
694 telnet
->fn
= &fn_table
;
695 telnet
->cfg
= *cfg
; /* STRUCTURE COPY */
697 telnet
->echoing
= TRUE
;
698 telnet
->editing
= TRUE
;
699 telnet
->activated
= FALSE
;
700 telnet
->sb_buf
= NULL
;
702 telnet
->frontend
= frontend_handle
;
703 telnet
->term_width
= telnet
->cfg
.width
;
704 telnet
->term_height
= telnet
->cfg
.height
;
705 telnet
->state
= TOP_LEVEL
;
706 *backend_handle
= telnet
;
713 buf
= dupprintf("Looking up host \"%s\"", host
);
714 logevent(telnet
->frontend
, buf
);
717 addr
= name_lookup(host
, port
, realhost
, &telnet
->cfg
);
718 if ((err
= sk_addr_error(addr
)) != NULL
) {
724 port
= 23; /* default telnet port */
730 char *buf
, addrbuf
[100];
731 sk_getaddr(addr
, addrbuf
, 100);
732 buf
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
733 logevent(telnet
->frontend
, buf
);
736 telnet
->s
= new_connection(addr
, *realhost
, port
, 0, 1,
737 nodelay
, keepalive
, (Plug
) telnet
, &telnet
->cfg
);
738 if ((err
= sk_socket_error(telnet
->s
)) != NULL
)
742 * Initialise option states.
744 if (telnet
->cfg
.passive_telnet
) {
745 const struct Opt
*const *o
;
747 for (o
= opts
; *o
; o
++)
748 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
750 const struct Opt
*const *o
;
752 for (o
= opts
; *o
; o
++) {
753 telnet
->opt_states
[(*o
)->index
] = (*o
)->initial_state
;
754 if (telnet
->opt_states
[(*o
)->index
] == REQUESTED
)
755 send_opt(telnet
, (*o
)->send
, (*o
)->option
);
757 telnet
->activated
= TRUE
;
761 * Set up SYNCH state.
763 telnet
->in_synch
= FALSE
;
766 * We can send special commands from the start.
768 update_specials_menu(telnet
->frontend
);
773 static void telnet_free(void *handle
)
775 Telnet telnet
= (Telnet
) handle
;
777 sfree(telnet
->sb_buf
);
783 * Reconfigure the Telnet backend. There's no immediate action
784 * necessary, in this backend: we just save the fresh config for
785 * any subsequent negotiations.
787 static void telnet_reconfig(void *handle
, Config
*cfg
)
789 Telnet telnet
= (Telnet
) handle
;
790 telnet
->cfg
= *cfg
; /* STRUCTURE COPY */
794 * Called to send data down the Telnet connection.
796 static int telnet_send(void *handle
, char *buf
, int len
)
798 Telnet telnet
= (Telnet
) handle
;
799 unsigned char *p
, *end
;
800 static const unsigned char iac
[2] = { IAC
, IAC
};
801 static const unsigned char cr
[2] = { CR
, NUL
};
803 static const unsigned char nl
[2] = { CR
, LF
};
806 if (telnet
->s
== NULL
)
809 p
= (unsigned char *)buf
;
810 end
= (unsigned char *)(buf
+ len
);
812 unsigned char *q
= p
;
814 while (p
< end
&& iswritable(*p
))
816 telnet
->bufsize
= sk_write(telnet
->s
, (char *)q
, p
- q
);
818 while (p
< end
&& !iswritable(*p
)) {
820 sk_write(telnet
->s
, (char *)(*p
== IAC ? iac
: cr
), 2);
825 return telnet
->bufsize
;
829 * Called to query the current socket sendability status.
831 static int telnet_sendbuffer(void *handle
)
833 Telnet telnet
= (Telnet
) handle
;
834 return telnet
->bufsize
;
838 * Called to set the size of the window from Telnet's POV.
840 static void telnet_size(void *handle
, int width
, int height
)
842 Telnet telnet
= (Telnet
) handle
;
847 telnet
->term_width
= width
;
848 telnet
->term_height
= height
;
850 if (telnet
->s
== NULL
|| telnet
->opt_states
[o_naws
.index
] != ACTIVE
)
855 b
[n
++] = TELOPT_NAWS
;
856 b
[n
++] = telnet
->term_width
>> 8;
857 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
858 b
[n
++] = telnet
->term_width
& 0xFF;
859 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
860 b
[n
++] = telnet
->term_height
>> 8;
861 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
862 b
[n
++] = telnet
->term_height
& 0xFF;
863 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
866 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
867 logbuf
= dupprintf("client:\tSB NAWS %d,%d",
868 telnet
->term_width
, telnet
->term_height
);
869 logevent(telnet
->frontend
, logbuf
);
874 * Send Telnet special codes.
876 static void telnet_special(void *handle
, Telnet_Special code
)
878 Telnet telnet
= (Telnet
) handle
;
881 if (telnet
->s
== NULL
)
888 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
892 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
896 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
900 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
904 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
908 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
912 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
916 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
920 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
924 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
928 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
932 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
935 /* In BINARY mode, CR-LF becomes just CR -
936 * and without the NUL suffix too. */
937 if (telnet
->opt_states
[o_we_bin
.index
] == ACTIVE
)
938 telnet
->bufsize
= sk_write(telnet
->s
, "\r", 1);
940 telnet
->bufsize
= sk_write(telnet
->s
, "\r\n", 2);
944 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 1);
945 telnet
->bufsize
= sk_write_oob(telnet
->s
, (char *)(b
+ 1), 1);
948 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
||
949 telnet
->opt_states
[o_echo
.index
] == REALLY_INACTIVE
) {
950 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
951 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
955 if (telnet
->opt_states
[o_echo
.index
] == ACTIVE
) {
956 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
957 send_opt(telnet
, o_echo
.nsend
, o_echo
.option
);
961 if (telnet
->opt_states
[o_they_sga
.index
] == ACTIVE
) {
963 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
967 break; /* never heard of it */
971 static const struct telnet_special
*telnet_get_specials(void *handle
)
973 static const struct telnet_special specials
[] = {
974 {"Are You There", TS_AYT
},
977 {"Erase Character", TS_EC
},
978 {"Erase Line", TS_EL
},
980 {"No Operation", TS_NOP
},
982 {"Abort Process", TS_ABORT
},
983 {"Abort Output", TS_AO
},
984 {"Interrupt Process", TS_IP
},
985 {"Suspend Process", TS_SUSP
},
987 {"End Of Record", TS_EOR
},
988 {"End Of File", TS_EOF
},
994 static Socket
telnet_socket(void *handle
)
996 Telnet telnet
= (Telnet
) handle
;
1000 static int telnet_sendok(void *handle
)
1002 /* Telnet telnet = (Telnet) handle; */
1006 static void telnet_unthrottle(void *handle
, int backlog
)
1008 Telnet telnet
= (Telnet
) handle
;
1009 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
1012 static int telnet_ldisc(void *handle
, int option
)
1014 Telnet telnet
= (Telnet
) handle
;
1015 if (option
== LD_ECHO
)
1016 return telnet
->echoing
;
1017 if (option
== LD_EDIT
)
1018 return telnet
->editing
;
1022 static void telnet_provide_ldisc(void *handle
, void *ldisc
)
1024 Telnet telnet
= (Telnet
) handle
;
1025 telnet
->ldisc
= ldisc
;
1028 static void telnet_provide_logctx(void *handle
, void *logctx
)
1030 /* This is a stub. */
1033 static int telnet_exitcode(void *handle
)
1035 Telnet telnet
= (Telnet
) handle
;
1036 if (telnet
->s
!= NULL
)
1037 return -1; /* still connected */
1039 /* Telnet doesn't transmit exit codes back to the client */
1043 Backend telnet_backend
= {
1051 telnet_get_specials
,
1056 telnet_provide_ldisc
,
1057 telnet_provide_logctx
,