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", telopt(telnet
->sb_opt
),
522 n
== 6 ?
"<nothing>" : "<stuff>");
523 logevent(telnet
->frontend
, logbuf
);
530 static void do_telnet_read(Telnet telnet
, char *buf
, int len
)
534 int c
= (unsigned char) *buf
++;
536 switch (telnet
->state
) {
539 if (c
== NUL
&& telnet
->state
== SEENCR
)
540 telnet
->state
= TOP_LEVEL
;
542 telnet
->state
= SEENIAC
;
544 if (!telnet
->in_synch
)
548 /* I can't get the F***ing winsock to insert the urgent IAC
549 * into the right position! Even with SO_OOBINLINE it gives
550 * it to recv too soon. And of course the DM byte (that
551 * arrives in the same packet!) appears several K later!!
553 * Oh well, we do get the DM in the right place so I'll
554 * just stop hiding on the next 0xf2 and hope for the best.
557 telnet
->in_synch
= 0;
559 if (c
== CR
&& telnet
->opt_states
[o_they_bin
.index
] != ACTIVE
)
560 telnet
->state
= SEENCR
;
562 telnet
->state
= TOP_LEVEL
;
567 telnet
->state
= SEENDO
;
569 telnet
->state
= SEENDONT
;
571 telnet
->state
= SEENWILL
;
573 telnet
->state
= SEENWONT
;
575 telnet
->state
= SEENSB
;
577 telnet
->in_synch
= 0;
578 telnet
->state
= TOP_LEVEL
;
580 /* ignore everything else; print it if it's IAC */
584 telnet
->state
= TOP_LEVEL
;
588 proc_rec_opt(telnet
, WILL
, c
);
589 telnet
->state
= TOP_LEVEL
;
592 proc_rec_opt(telnet
, WONT
, c
);
593 telnet
->state
= TOP_LEVEL
;
596 proc_rec_opt(telnet
, DO
, c
);
597 telnet
->state
= TOP_LEVEL
;
600 proc_rec_opt(telnet
, DONT
, c
);
601 telnet
->state
= TOP_LEVEL
;
606 telnet
->state
= SUBNEGOT
;
610 telnet
->state
= SUBNEG_IAC
;
613 if (telnet
->sb_len
>= telnet
->sb_size
) {
614 telnet
->sb_size
+= SB_DELTA
;
615 telnet
->sb_buf
= sresize(telnet
->sb_buf
, telnet
->sb_size
,
618 telnet
->sb_buf
[telnet
->sb_len
++] = c
;
619 telnet
->state
= SUBNEGOT
; /* in case we came here by goto */
624 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
626 process_subneg(telnet
);
627 telnet
->state
= TOP_LEVEL
;
634 static int telnet_closing(Plug plug
, char *error_msg
, int error_code
,
637 Telnet telnet
= (Telnet
) plug
;
644 /* A socket error has occurred. */
645 logevent(telnet
->frontend
, error_msg
);
646 connection_fatal("%s", error_msg
);
647 } /* Otherwise, the remote side closed the connection normally. */
651 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
653 Telnet telnet
= (Telnet
) plug
;
655 telnet
->in_synch
= TRUE
;
656 do_telnet_read(telnet
, data
, len
);
660 static void telnet_sent(Plug plug
, int bufsize
)
662 Telnet telnet
= (Telnet
) plug
;
663 telnet
->bufsize
= bufsize
;
667 * Called to set up the Telnet connection.
669 * Returns an error message, or NULL on success.
671 * Also places the canonical host name into `realhost'. It must be
672 * freed by the caller.
674 static char *telnet_init(void *frontend_handle
, void **backend_handle
,
676 char *host
, int port
, char **realhost
, int nodelay
)
678 static const struct plug_function_table fn_table
= {
687 telnet
= snew(struct telnet_tag
);
688 telnet
->fn
= &fn_table
;
689 telnet
->cfg
= *cfg
; /* STRUCTURE COPY */
691 telnet
->echoing
= TRUE
;
692 telnet
->editing
= TRUE
;
693 telnet
->activated
= FALSE
;
694 telnet
->sb_buf
= NULL
;
696 telnet
->frontend
= frontend_handle
;
697 telnet
->term_width
= telnet
->cfg
.width
;
698 telnet
->term_height
= telnet
->cfg
.height
;
699 telnet
->state
= TOP_LEVEL
;
700 *backend_handle
= telnet
;
707 buf
= dupprintf("Looking up host \"%s\"", host
);
708 logevent(telnet
->frontend
, buf
);
711 addr
= name_lookup(host
, port
, realhost
, &telnet
->cfg
);
712 if ((err
= sk_addr_error(addr
)) != NULL
)
716 port
= 23; /* default telnet port */
722 char *buf
, addrbuf
[100];
723 sk_getaddr(addr
, addrbuf
, 100);
724 buf
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
725 logevent(telnet
->frontend
, buf
);
728 telnet
->s
= new_connection(addr
, *realhost
, port
, 0, 1,
729 nodelay
, (Plug
) telnet
, &telnet
->cfg
);
730 if ((err
= sk_socket_error(telnet
->s
)) != NULL
)
736 * Initialise option states.
738 if (telnet
->cfg
.passive_telnet
) {
739 const struct Opt
*const *o
;
741 for (o
= opts
; *o
; o
++)
742 telnet
->opt_states
[(*o
)->index
] = INACTIVE
;
744 const struct Opt
*const *o
;
746 for (o
= opts
; *o
; o
++) {
747 telnet
->opt_states
[(*o
)->index
] = (*o
)->initial_state
;
748 if (telnet
->opt_states
[(*o
)->index
] == REQUESTED
)
749 send_opt(telnet
, (*o
)->send
, (*o
)->option
);
751 telnet
->activated
= TRUE
;
755 * Set up SYNCH state.
757 telnet
->in_synch
= FALSE
;
762 static void telnet_free(void *handle
)
764 Telnet telnet
= (Telnet
) handle
;
766 sfree(telnet
->sb_buf
);
772 * Reconfigure the Telnet backend. There's no immediate action
773 * necessary, in this backend: we just save the fresh config for
774 * any subsequent negotiations.
776 static void telnet_reconfig(void *handle
, Config
*cfg
)
778 Telnet telnet
= (Telnet
) handle
;
779 telnet
->cfg
= *cfg
; /* STRUCTURE COPY */
783 * Called to send data down the Telnet connection.
785 static int telnet_send(void *handle
, char *buf
, int len
)
787 Telnet telnet
= (Telnet
) handle
;
788 unsigned char *p
, *end
;
789 static const unsigned char iac
[2] = { IAC
, IAC
};
790 static const unsigned char cr
[2] = { CR
, NUL
};
792 static const unsigned char nl
[2] = { CR
, LF
};
795 if (telnet
->s
== NULL
)
798 p
= (unsigned char *)buf
;
799 end
= (unsigned char *)(buf
+ len
);
801 unsigned char *q
= p
;
803 while (p
< end
&& iswritable(*p
))
805 telnet
->bufsize
= sk_write(telnet
->s
, (char *)q
, p
- q
);
807 while (p
< end
&& !iswritable(*p
)) {
809 sk_write(telnet
->s
, (char *)(*p
== IAC ? iac
: cr
), 2);
814 return telnet
->bufsize
;
818 * Called to query the current socket sendability status.
820 static int telnet_sendbuffer(void *handle
)
822 Telnet telnet
= (Telnet
) handle
;
823 return telnet
->bufsize
;
827 * Called to set the size of the window from Telnet's POV.
829 static void telnet_size(void *handle
, int width
, int height
)
831 Telnet telnet
= (Telnet
) handle
;
836 telnet
->term_width
= width
;
837 telnet
->term_height
= height
;
839 if (telnet
->s
== NULL
|| telnet
->opt_states
[o_naws
.index
] != ACTIVE
)
844 b
[n
++] = TELOPT_NAWS
;
845 b
[n
++] = telnet
->term_width
>> 8;
846 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
847 b
[n
++] = telnet
->term_width
& 0xFF;
848 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
849 b
[n
++] = telnet
->term_height
>> 8;
850 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
851 b
[n
++] = telnet
->term_height
& 0xFF;
852 if (b
[n
-1] == IAC
) b
[n
++] = IAC
; /* duplicate any IAC byte occurs */
855 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, n
);
856 logbuf
= dupprintf("client:\tSB NAWS %d,%d",
857 telnet
->term_width
, telnet
->term_height
);
858 logevent(telnet
->frontend
, logbuf
);
863 * Send Telnet special codes.
865 static void telnet_special(void *handle
, Telnet_Special code
)
867 Telnet telnet
= (Telnet
) handle
;
870 if (telnet
->s
== NULL
)
877 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
881 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
885 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
889 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
893 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
897 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
901 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
905 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
909 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
913 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
917 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
921 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
924 /* In BINARY mode, CR-LF becomes just CR -
925 * and without the NUL suffix too. */
926 if (telnet
->opt_states
[o_we_bin
.index
] == ACTIVE
)
927 telnet
->bufsize
= sk_write(telnet
->s
, "\r", 1);
929 telnet
->bufsize
= sk_write(telnet
->s
, "\r\n", 2);
933 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 1);
934 telnet
->bufsize
= sk_write_oob(telnet
->s
, (char *)(b
+ 1), 1);
937 if (telnet
->opt_states
[o_echo
.index
] == INACTIVE
||
938 telnet
->opt_states
[o_echo
.index
] == REALLY_INACTIVE
) {
939 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
940 send_opt(telnet
, o_echo
.send
, o_echo
.option
);
944 if (telnet
->opt_states
[o_echo
.index
] == ACTIVE
) {
945 telnet
->opt_states
[o_echo
.index
] = REQUESTED
;
946 send_opt(telnet
, o_echo
.nsend
, o_echo
.option
);
950 if (telnet
->opt_states
[o_they_sga
.index
] == ACTIVE
) {
952 telnet
->bufsize
= sk_write(telnet
->s
, (char *)b
, 2);
958 static Socket
telnet_socket(void *handle
)
960 Telnet telnet
= (Telnet
) handle
;
964 static int telnet_sendok(void *handle
)
966 /* Telnet telnet = (Telnet) handle; */
970 static void telnet_unthrottle(void *handle
, int backlog
)
972 Telnet telnet
= (Telnet
) handle
;
973 sk_set_frozen(telnet
->s
, backlog
> TELNET_MAX_BACKLOG
);
976 static int telnet_ldisc(void *handle
, int option
)
978 Telnet telnet
= (Telnet
) handle
;
979 if (option
== LD_ECHO
)
980 return telnet
->echoing
;
981 if (option
== LD_EDIT
)
982 return telnet
->editing
;
986 static void telnet_provide_ldisc(void *handle
, void *ldisc
)
988 Telnet telnet
= (Telnet
) handle
;
989 telnet
->ldisc
= ldisc
;
992 static void telnet_provide_logctx(void *handle
, void *logctx
)
994 /* This is a stub. */
997 static int telnet_exitcode(void *handle
)
999 /* Telnet telnet = (Telnet) handle; */
1000 /* Telnet doesn't transmit exit codes back to the client */
1004 Backend telnet_backend
= {
1016 telnet_provide_ldisc
,
1017 telnet_provide_logctx
,