14 static Socket s
= NULL
;
16 static void *frontend
;
17 static int telnet_term_width
, telnet_term_height
;
19 #define IAC 255 /* interpret as command: */
20 #define DONT 254 /* you are not to use option */
21 #define DO 253 /* please, you use option */
22 #define WONT 252 /* I won't use option */
23 #define WILL 251 /* I will use option */
24 #define SB 250 /* interpret as subnegotiation */
25 #define SE 240 /* end sub negotiation */
27 #define GA 249 /* you may reverse the line */
28 #define EL 248 /* erase the current line */
29 #define EC 247 /* erase the current character */
30 #define AYT 246 /* are you there */
31 #define AO 245 /* abort output--but let prog finish */
32 #define IP 244 /* interrupt process--permanently */
33 #define BREAK 243 /* break */
34 #define DM 242 /* data mark--for connect. cleaning */
35 #define NOP 241 /* nop */
36 #define EOR 239 /* end of record (transparent mode) */
37 #define ABORT 238 /* Abort process */
38 #define SUSP 237 /* Suspend process */
39 #define xEOF 236 /* End of file: EOF is already used... */
41 #define TELOPT_BINARY 0 /* 8-bit data path */
42 #define TELOPT_ECHO 1 /* echo */
43 #define TELOPT_RCP 2 /* prepare to reconnect */
44 #define TELOPT_SGA 3 /* suppress go ahead */
45 #define TELOPT_NAMS 4 /* approximate message size */
46 #define TELOPT_STATUS 5 /* give status */
47 #define TELOPT_TM 6 /* timing mark */
48 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
49 #define TELOPT_NAOL 8 /* negotiate about output line width */
50 #define TELOPT_NAOP 9 /* negotiate about output page size */
51 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
52 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
53 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
54 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
55 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
56 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
57 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
58 #define TELOPT_XASCII 17 /* extended ascic character set */
59 #define TELOPT_LOGOUT 18 /* force logout */
60 #define TELOPT_BM 19 /* byte macro */
61 #define TELOPT_DET 20 /* data entry terminal */
62 #define TELOPT_SUPDUP 21 /* supdup protocol */
63 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
64 #define TELOPT_SNDLOC 23 /* send location */
65 #define TELOPT_TTYPE 24 /* terminal type */
66 #define TELOPT_EOR 25 /* end or record */
67 #define TELOPT_TUID 26 /* TACACS user identification */
68 #define TELOPT_OUTMRK 27 /* output marking */
69 #define TELOPT_TTYLOC 28 /* terminal location number */
70 #define TELOPT_3270REGIME 29 /* 3270 regime */
71 #define TELOPT_X3PAD 30 /* X.3 PAD */
72 #define TELOPT_NAWS 31 /* window size */
73 #define TELOPT_TSPEED 32 /* terminal speed */
74 #define TELOPT_LFLOW 33 /* remote flow control */
75 #define TELOPT_LINEMODE 34 /* Linemode option */
76 #define TELOPT_XDISPLOC 35 /* X Display Location */
77 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
78 #define TELOPT_AUTHENTICATION 37 /* Authenticate */
79 #define TELOPT_ENCRYPT 38 /* Encryption option */
80 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
81 #define TELOPT_EXOPL 255 /* extended-options-list */
83 #define TELQUAL_IS 0 /* option is... */
84 #define TELQUAL_SEND 1 /* send option */
85 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
95 #define iswritable(x) ( (x) != IAC && (x) != CR )
97 static char *telopt(int opt
)
99 #define i(x) if (opt == TELOPT_ ## x) return #x;
144 static void telnet_size(void);
147 int send
; /* what we initially send */
148 int nsend
; /* -ve send if requested to stop it */
149 int ack
, nak
; /* +ve and -ve acknowledgements */
150 int option
; /* the option code */
152 REQUESTED
, ACTIVE
, INACTIVE
, REALLY_INACTIVE
156 static struct Opt o_naws
=
157 { WILL
, WONT
, DO
, DONT
, TELOPT_NAWS
, REQUESTED
};
158 static struct Opt o_tspeed
=
159 { WILL
, WONT
, DO
, DONT
, TELOPT_TSPEED
, REQUESTED
};
160 static struct Opt o_ttype
=
161 { WILL
, WONT
, DO
, DONT
, TELOPT_TTYPE
, REQUESTED
};
162 static struct Opt o_oenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_OLD_ENVIRON
,
165 static struct Opt o_nenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_NEW_ENVIRON
,
168 static struct Opt o_echo
=
169 { DO
, DONT
, WILL
, WONT
, TELOPT_ECHO
, REQUESTED
};
170 static struct Opt o_we_sga
=
171 { WILL
, WONT
, DO
, DONT
, TELOPT_SGA
, REQUESTED
};
172 static struct Opt o_they_sga
=
173 { DO
, DONT
, WILL
, WONT
, TELOPT_SGA
, REQUESTED
};
175 static struct Opt
*opts
[] = {
176 &o_naws
, &o_tspeed
, &o_ttype
, &o_oenv
, &o_nenv
, &o_echo
,
177 &o_we_sga
, &o_they_sga
, NULL
180 #define TELNET_MAX_BACKLOG 4096
182 static int echoing
= TRUE
, editing
= TRUE
;
183 static int activated
= FALSE
;
184 static int telnet_bufsize
;
186 static int sb_opt
, sb_len
;
187 static char *sb_buf
= NULL
;
188 static int sb_size
= 0;
189 #define SB_DELTA 1024
191 static void c_write1(int c
)
195 backlog
= from_backend(frontend
, 0, &cc
, 1);
196 sk_set_frozen(s
, backlog
> TELNET_MAX_BACKLOG
);
199 static void log_option(char *sender
, int cmd
, int option
)
202 sprintf(buf
, "%s:\t%s %s", sender
,
203 (cmd
== WILL ?
"WILL" : cmd
== WONT ?
"WONT" :
204 cmd
== DO ?
"DO" : cmd
== DONT ?
"DONT" : "<??>"),
209 static void send_opt(int cmd
, int option
)
216 telnet_bufsize
= sk_write(s
, b
, 3);
217 log_option("client", cmd
, option
);
220 static void deactivate_option(struct Opt
*o
)
222 if (o
->state
== REQUESTED
|| o
->state
== ACTIVE
)
223 send_opt(o
->nsend
, o
->option
);
224 o
->state
= REALLY_INACTIVE
;
228 * Generate side effects of enabling or disabling an option.
230 static void option_side_effects(struct Opt
*o
, int enabled
)
232 if (o
->option
== TELOPT_ECHO
&& o
->send
== DO
)
234 else if (o
->option
== TELOPT_SGA
&& o
->send
== DO
)
236 ldisc_send(NULL
, 0, 0); /* cause ldisc to notice the change */
238 /* Ensure we get the minimum options */
240 if (o_echo
.state
== INACTIVE
) {
241 o_echo
.state
= REQUESTED
;
242 send_opt(o_echo
.send
, o_echo
.option
);
244 if (o_we_sga
.state
== INACTIVE
) {
245 o_we_sga
.state
= REQUESTED
;
246 send_opt(o_we_sga
.send
, o_we_sga
.option
);
248 if (o_they_sga
.state
== INACTIVE
) {
249 o_they_sga
.state
= REQUESTED
;
250 send_opt(o_they_sga
.send
, o_they_sga
.option
);
256 static void activate_option(struct Opt
*o
)
258 if (o
->send
== WILL
&& o
->option
== TELOPT_NAWS
)
260 if (o
->send
== WILL
&&
261 (o
->option
== TELOPT_NEW_ENVIRON
||
262 o
->option
== TELOPT_OLD_ENVIRON
)) {
264 * We may only have one kind of ENVIRON going at a time.
265 * This is a hack, but who cares.
267 deactivate_option(o
->option
==
268 TELOPT_NEW_ENVIRON ?
&o_oenv
: &o_nenv
);
270 option_side_effects(o
, 1);
273 static void refused_option(struct Opt
*o
)
275 if (o
->send
== WILL
&& o
->option
== TELOPT_NEW_ENVIRON
&&
276 o_oenv
.state
== INACTIVE
) {
277 send_opt(WILL
, TELOPT_OLD_ENVIRON
);
278 o_oenv
.state
= REQUESTED
;
280 option_side_effects(o
, 0);
283 static void proc_rec_opt(int cmd
, int option
)
287 log_option("server", cmd
, option
);
288 for (o
= opts
; *o
; o
++) {
289 if ((*o
)->option
== option
&& (*o
)->ack
== cmd
) {
290 switch ((*o
)->state
) {
292 (*o
)->state
= ACTIVE
;
298 (*o
)->state
= ACTIVE
;
299 send_opt((*o
)->send
, option
);
302 case REALLY_INACTIVE
:
303 send_opt((*o
)->nsend
, option
);
307 } else if ((*o
)->option
== option
&& (*o
)->nak
== cmd
) {
308 switch ((*o
)->state
) {
310 (*o
)->state
= INACTIVE
;
314 (*o
)->state
= INACTIVE
;
315 send_opt((*o
)->nsend
, option
);
316 option_side_effects(*o
, 0);
319 case REALLY_INACTIVE
:
326 * If we reach here, the option was one we weren't prepared to
327 * cope with. So send a negative ack.
329 send_opt((cmd
== WILL ? DONT
: WONT
), option
);
332 static void process_subneg(void)
334 unsigned char b
[2048], *p
, *q
;
340 if (sb_len
== 1 && sb_buf
[0] == TELQUAL_SEND
) {
341 char logbuf
[sizeof(cfg
.termspeed
) + 80];
344 b
[2] = TELOPT_TSPEED
;
346 strcpy(b
+ 4, cfg
.termspeed
);
347 n
= 4 + strlen(cfg
.termspeed
);
350 telnet_bufsize
= sk_write(s
, b
, n
+ 2);
351 logevent("server:\tSB TSPEED SEND");
352 sprintf(logbuf
, "client:\tSB TSPEED IS %s", cfg
.termspeed
);
355 logevent("server:\tSB TSPEED <something weird>");
358 if (sb_len
== 1 && sb_buf
[0] == TELQUAL_SEND
) {
359 char logbuf
[sizeof(cfg
.termtype
) + 80];
364 for (n
= 0; cfg
.termtype
[n
]; n
++)
365 b
[n
+ 4] = (cfg
.termtype
[n
] >= 'a'
366 && cfg
.termtype
[n
] <=
367 'z' ? cfg
.termtype
[n
] + 'A' -
368 'a' : cfg
.termtype
[n
]);
371 telnet_bufsize
= sk_write(s
, b
, n
+ 6);
373 logevent("server:\tSB TTYPE SEND");
374 sprintf(logbuf
, "client:\tSB TTYPE IS %s", b
+ 4);
377 logevent("server:\tSB TTYPE <something weird>\r\n");
379 case TELOPT_OLD_ENVIRON
:
380 case TELOPT_NEW_ENVIRON
:
383 if (p
< q
&& *p
== TELQUAL_SEND
) {
386 sprintf(logbuf
, "server:\tSB %s SEND", telopt(sb_opt
));
388 if (sb_opt
== TELOPT_OLD_ENVIRON
) {
389 if (cfg
.rfc_environ
) {
397 * Try to guess the sense of VAR and VALUE.
403 } else if (*p
== BSD_VAR
) {
411 * With NEW_ENVIRON, the sense of VAR and VALUE
425 while (*e
&& *e
!= '\t')
447 telnet_bufsize
= sk_write(s
, b
, n
);
448 sprintf(logbuf
, "client:\tSB %s IS %s", telopt(sb_opt
),
449 n
== 6 ?
"<nothing>" : "<stuff>");
457 TOP_LEVEL
, SEENIAC
, SEENWILL
, SEENWONT
, SEENDO
, SEENDONT
,
458 SEENSB
, SUBNEGOT
, SUBNEG_IAC
, SEENCR
459 } telnet_state
= TOP_LEVEL
;
461 static void do_telnet_read(char *buf
, int len
)
465 int c
= (unsigned char) *buf
++;
467 switch (telnet_state
) {
470 if (c
== NUL
&& telnet_state
== SEENCR
)
471 telnet_state
= TOP_LEVEL
;
473 telnet_state
= SEENIAC
;
479 /* I can't get the F***ing winsock to insert the urgent IAC
480 * into the right position! Even with SO_OOBINLINE it gives
481 * it to recv too soon. And of course the DM byte (that
482 * arrives in the same packet!) appears several K later!!
484 * Oh well, we do get the DM in the right place so I'll
485 * just stop hiding on the next 0xf2 and hope for the best.
491 telnet_state
= SEENCR
;
493 telnet_state
= TOP_LEVEL
;
498 telnet_state
= SEENDO
;
500 telnet_state
= SEENDONT
;
502 telnet_state
= SEENWILL
;
504 telnet_state
= SEENWONT
;
506 telnet_state
= SEENSB
;
509 telnet_state
= TOP_LEVEL
;
511 /* ignore everything else; print it if it's IAC */
515 telnet_state
= TOP_LEVEL
;
519 proc_rec_opt(WILL
, c
);
520 telnet_state
= TOP_LEVEL
;
523 proc_rec_opt(WONT
, c
);
524 telnet_state
= TOP_LEVEL
;
528 telnet_state
= TOP_LEVEL
;
531 proc_rec_opt(DONT
, c
);
532 telnet_state
= TOP_LEVEL
;
537 telnet_state
= SUBNEGOT
;
541 telnet_state
= SUBNEG_IAC
;
544 if (sb_len
>= sb_size
) {
548 srealloc(sb_buf
, sb_size
) :
555 if (sb_len
< sb_size
)
556 sb_buf
[sb_len
++] = c
;
557 telnet_state
= SUBNEGOT
; /* in case we came here by goto */
562 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
565 telnet_state
= TOP_LEVEL
;
572 static int telnet_closing(Plug plug
, char *error_msg
, int error_code
,
580 /* A socket error has occurred. */
582 connection_fatal("%s", error_msg
);
583 } /* Otherwise, the remote side closed the connection normally. */
587 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
591 do_telnet_read(data
, len
);
595 static void telnet_sent(Plug plug
, int bufsize
)
597 telnet_bufsize
= bufsize
;
601 * Called to set up the Telnet connection.
603 * Returns an error message, or NULL on success.
605 * Also places the canonical host name into `realhost'. It must be
606 * freed by the caller.
608 static char *telnet_init(void *frontend_handle
,
609 char *host
, int port
, char **realhost
, int nodelay
)
611 static struct plug_function_table fn_table
= {
615 }, *fn_table_ptr
= &fn_table
;
620 frontend
= frontend_handle
;
621 telnet_term_width
= cfg
.width
;
622 telnet_term_height
= cfg
.height
;
629 sprintf(buf
, "Looking up host \"%.170s\"", host
);
632 addr
= sk_namelookup(host
, realhost
);
633 if ((err
= sk_addr_error(addr
)))
637 port
= 23; /* default telnet port */
643 char buf
[200], addrbuf
[100];
644 sk_getaddr(addr
, addrbuf
, 100);
645 sprintf(buf
, "Connecting to %.100s port %d", addrbuf
, port
);
648 s
= new_connection(addr
, *realhost
, port
, 0, 1, nodelay
, &fn_table_ptr
);
649 if ((err
= sk_socket_error(s
)))
655 * Initialise option states.
657 if (cfg
.passive_telnet
) {
660 for (o
= opts
; *o
; o
++)
661 if ((*o
)->state
== REQUESTED
)
662 (*o
)->state
= INACTIVE
;
666 for (o
= opts
; *o
; o
++)
667 if ((*o
)->state
== REQUESTED
)
668 send_opt((*o
)->send
, (*o
)->option
);
673 * Set up SYNCH state.
681 * Called to send data down the Telnet connection.
683 static int telnet_send(char *buf
, int len
)
686 static unsigned char iac
[2] = { IAC
, IAC
};
687 static unsigned char cr
[2] = { CR
, NUL
};
689 static unsigned char nl
[2] = { CR
, LF
};
696 while (p
< buf
+ len
) {
699 while (iswritable((unsigned char) *p
) && p
< buf
+ len
)
701 telnet_bufsize
= sk_write(s
, q
, p
- q
);
703 while (p
< buf
+ len
&& !iswritable((unsigned char) *p
)) {
705 sk_write(s
, (unsigned char) *p
== IAC ? iac
: cr
, 2);
710 return telnet_bufsize
;
714 * Called to query the current socket sendability status.
716 static int telnet_sendbuffer(void)
718 return telnet_bufsize
;
722 * Called to set the size of the window from Telnet's POV.
724 static void telnet_size(int width
, int height
)
729 telnet_term_width
= width
;
730 telnet_term_height
= height
;
732 if (s
== NULL
|| o_naws
.state
!= ACTIVE
)
737 b
[3] = telnet_term_width
>> 8;
738 b
[4] = telnet_term_width
& 0xFF;
739 b
[5] = telnet_term_height
>> 8;
740 b
[6] = telnet_term_height
& 0xFF;
743 telnet_bufsize
= sk_write(s
, b
, 9);
744 sprintf(logbuf
, "client:\tSB NAWS %d,%d",
745 ((unsigned char) b
[3] << 8) + (unsigned char) b
[4],
746 ((unsigned char) b
[5] << 8) + (unsigned char) b
[6]);
751 * Send Telnet special codes.
753 static void telnet_special(Telnet_Special code
)
764 telnet_bufsize
= sk_write(s
, b
, 2);
768 telnet_bufsize
= sk_write(s
, b
, 2);
772 telnet_bufsize
= sk_write(s
, b
, 2);
776 telnet_bufsize
= sk_write(s
, b
, 2);
780 telnet_bufsize
= sk_write(s
, b
, 2);
784 telnet_bufsize
= sk_write(s
, b
, 2);
788 telnet_bufsize
= sk_write(s
, b
, 2);
792 telnet_bufsize
= sk_write(s
, b
, 2);
796 telnet_bufsize
= sk_write(s
, b
, 2);
800 telnet_bufsize
= sk_write(s
, b
, 2);
804 telnet_bufsize
= sk_write(s
, b
, 2);
808 telnet_bufsize
= sk_write(s
, b
, 2);
811 telnet_bufsize
= sk_write(s
, "\r\n", 2);
815 telnet_bufsize
= sk_write(s
, b
, 1);
816 telnet_bufsize
= sk_write_oob(s
, b
+ 1, 1);
819 if (o_echo
.state
== INACTIVE
|| o_echo
.state
== REALLY_INACTIVE
) {
820 o_echo
.state
= REQUESTED
;
821 send_opt(o_echo
.send
, o_echo
.option
);
825 if (o_echo
.state
== ACTIVE
) {
826 o_echo
.state
= REQUESTED
;
827 send_opt(o_echo
.nsend
, o_echo
.option
);
831 if (o_they_sga
.state
== ACTIVE
) {
833 telnet_bufsize
= sk_write(s
, b
, 2);
839 static Socket
telnet_socket(void)
844 static int telnet_sendok(void)
849 static void telnet_unthrottle(int backlog
)
851 sk_set_frozen(s
, backlog
> TELNET_MAX_BACKLOG
);
854 static int telnet_ldisc(int option
)
856 if (option
== LD_ECHO
)
858 if (option
== LD_EDIT
)
863 static int telnet_exitcode(void)
865 /* Telnet doesn't transmit exit codes back to the client */
869 Backend telnet_backend
= {