14 static Socket s
= NULL
;
16 #define IAC 255 /* interpret as command: */
17 #define DONT 254 /* you are not to use option */
18 #define DO 253 /* please, you use option */
19 #define WONT 252 /* I won't use option */
20 #define WILL 251 /* I will use option */
21 #define SB 250 /* interpret as subnegotiation */
22 #define SE 240 /* end sub negotiation */
24 #define GA 249 /* you may reverse the line */
25 #define EL 248 /* erase the current line */
26 #define EC 247 /* erase the current character */
27 #define AYT 246 /* are you there */
28 #define AO 245 /* abort output--but let prog finish */
29 #define IP 244 /* interrupt process--permanently */
30 #define BREAK 243 /* break */
31 #define DM 242 /* data mark--for connect. cleaning */
32 #define NOP 241 /* nop */
33 #define EOR 239 /* end of record (transparent mode) */
34 #define ABORT 238 /* Abort process */
35 #define SUSP 237 /* Suspend process */
36 #define xEOF 236 /* End of file: EOF is already used... */
38 #define TELOPT_BINARY 0 /* 8-bit data path */
39 #define TELOPT_ECHO 1 /* echo */
40 #define TELOPT_RCP 2 /* prepare to reconnect */
41 #define TELOPT_SGA 3 /* suppress go ahead */
42 #define TELOPT_NAMS 4 /* approximate message size */
43 #define TELOPT_STATUS 5 /* give status */
44 #define TELOPT_TM 6 /* timing mark */
45 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
46 #define TELOPT_NAOL 8 /* negotiate about output line width */
47 #define TELOPT_NAOP 9 /* negotiate about output page size */
48 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
49 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
50 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
51 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
52 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
53 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
54 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
55 #define TELOPT_XASCII 17 /* extended ascic character set */
56 #define TELOPT_LOGOUT 18 /* force logout */
57 #define TELOPT_BM 19 /* byte macro */
58 #define TELOPT_DET 20 /* data entry terminal */
59 #define TELOPT_SUPDUP 21 /* supdup protocol */
60 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
61 #define TELOPT_SNDLOC 23 /* send location */
62 #define TELOPT_TTYPE 24 /* terminal type */
63 #define TELOPT_EOR 25 /* end or record */
64 #define TELOPT_TUID 26 /* TACACS user identification */
65 #define TELOPT_OUTMRK 27 /* output marking */
66 #define TELOPT_TTYLOC 28 /* terminal location number */
67 #define TELOPT_3270REGIME 29 /* 3270 regime */
68 #define TELOPT_X3PAD 30 /* X.3 PAD */
69 #define TELOPT_NAWS 31 /* window size */
70 #define TELOPT_TSPEED 32 /* terminal speed */
71 #define TELOPT_LFLOW 33 /* remote flow control */
72 #define TELOPT_LINEMODE 34 /* Linemode option */
73 #define TELOPT_XDISPLOC 35 /* X Display Location */
74 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
75 #define TELOPT_AUTHENTICATION 37 /* Authenticate */
76 #define TELOPT_ENCRYPT 38 /* Encryption option */
77 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
78 #define TELOPT_EXOPL 255 /* extended-options-list */
80 #define TELQUAL_IS 0 /* option is... */
81 #define TELQUAL_SEND 1 /* send option */
82 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
92 #define iswritable(x) ( (x) != IAC && (x) != CR )
94 static char *telopt(int opt
)
96 #define i(x) if (opt == TELOPT_ ## x) return #x;
141 static void telnet_size(void);
144 int send
; /* what we initially send */
145 int nsend
; /* -ve send if requested to stop it */
146 int ack
, nak
; /* +ve and -ve acknowledgements */
147 int option
; /* the option code */
149 REQUESTED
, ACTIVE
, INACTIVE
, REALLY_INACTIVE
153 static struct Opt o_naws
=
154 { WILL
, WONT
, DO
, DONT
, TELOPT_NAWS
, REQUESTED
};
155 static struct Opt o_tspeed
=
156 { WILL
, WONT
, DO
, DONT
, TELOPT_TSPEED
, REQUESTED
};
157 static struct Opt o_ttype
=
158 { WILL
, WONT
, DO
, DONT
, TELOPT_TTYPE
, REQUESTED
};
159 static struct Opt o_oenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_OLD_ENVIRON
,
162 static struct Opt o_nenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_NEW_ENVIRON
,
165 static struct Opt o_echo
=
166 { DO
, DONT
, WILL
, WONT
, TELOPT_ECHO
, REQUESTED
};
167 static struct Opt o_we_sga
=
168 { WILL
, WONT
, DO
, DONT
, TELOPT_SGA
, REQUESTED
};
169 static struct Opt o_they_sga
=
170 { DO
, DONT
, WILL
, WONT
, TELOPT_SGA
, REQUESTED
};
172 static struct Opt
*opts
[] = {
173 &o_naws
, &o_tspeed
, &o_ttype
, &o_oenv
, &o_nenv
, &o_echo
,
174 &o_we_sga
, &o_they_sga
, NULL
177 #define TELNET_MAX_BACKLOG 4096
179 static int echoing
= TRUE
, editing
= TRUE
;
180 static int activated
= FALSE
;
181 static int telnet_bufsize
;
183 static int sb_opt
, sb_len
;
184 static char *sb_buf
= NULL
;
185 static int sb_size
= 0;
186 #define SB_DELTA 1024
188 static void c_write1(int c
)
192 backlog
= from_backend(0, &cc
, 1);
193 sk_set_frozen(s
, backlog
> TELNET_MAX_BACKLOG
);
196 static void log_option(char *sender
, int cmd
, int option
)
199 sprintf(buf
, "%s:\t%s %s", sender
,
200 (cmd
== WILL ?
"WILL" : cmd
== WONT ?
"WONT" :
201 cmd
== DO ?
"DO" : cmd
== DONT ?
"DONT" : "<??>"),
206 static void send_opt(int cmd
, int option
)
213 telnet_bufsize
= sk_write(s
, b
, 3);
214 log_option("client", cmd
, option
);
217 static void deactivate_option(struct Opt
*o
)
219 if (o
->state
== REQUESTED
|| o
->state
== ACTIVE
)
220 send_opt(o
->nsend
, o
->option
);
221 o
->state
= REALLY_INACTIVE
;
225 * Generate side effects of enabling or disabling an option.
227 static void option_side_effects(struct Opt
*o
, int enabled
)
229 if (o
->option
== TELOPT_ECHO
&& o
->send
== DO
)
231 else if (o
->option
== TELOPT_SGA
&& o
->send
== DO
)
233 ldisc_send(NULL
, 0); /* cause ldisc to notice the change */
235 /* Ensure we get the minimum options */
237 if (o_echo
.state
== INACTIVE
) {
238 o_echo
.state
= REQUESTED
;
239 send_opt(o_echo
.send
, o_echo
.option
);
241 if (o_we_sga
.state
== INACTIVE
) {
242 o_we_sga
.state
= REQUESTED
;
243 send_opt(o_we_sga
.send
, o_we_sga
.option
);
245 if (o_they_sga
.state
== INACTIVE
) {
246 o_they_sga
.state
= REQUESTED
;
247 send_opt(o_they_sga
.send
, o_they_sga
.option
);
253 static void activate_option(struct Opt
*o
)
255 if (o
->send
== WILL
&& o
->option
== TELOPT_NAWS
)
257 if (o
->send
== WILL
&&
258 (o
->option
== TELOPT_NEW_ENVIRON
||
259 o
->option
== TELOPT_OLD_ENVIRON
)) {
261 * We may only have one kind of ENVIRON going at a time.
262 * This is a hack, but who cares.
264 deactivate_option(o
->option
==
265 TELOPT_NEW_ENVIRON ?
&o_oenv
: &o_nenv
);
267 option_side_effects(o
, 1);
270 static void refused_option(struct Opt
*o
)
272 if (o
->send
== WILL
&& o
->option
== TELOPT_NEW_ENVIRON
&&
273 o_oenv
.state
== INACTIVE
) {
274 send_opt(WILL
, TELOPT_OLD_ENVIRON
);
275 o_oenv
.state
= REQUESTED
;
277 option_side_effects(o
, 0);
280 static void proc_rec_opt(int cmd
, int option
)
284 log_option("server", cmd
, option
);
285 for (o
= opts
; *o
; o
++) {
286 if ((*o
)->option
== option
&& (*o
)->ack
== cmd
) {
287 switch ((*o
)->state
) {
289 (*o
)->state
= ACTIVE
;
295 (*o
)->state
= ACTIVE
;
296 send_opt((*o
)->send
, option
);
299 case REALLY_INACTIVE
:
300 send_opt((*o
)->nsend
, option
);
304 } else if ((*o
)->option
== option
&& (*o
)->nak
== cmd
) {
305 switch ((*o
)->state
) {
307 (*o
)->state
= INACTIVE
;
311 (*o
)->state
= INACTIVE
;
312 send_opt((*o
)->nsend
, option
);
313 option_side_effects(*o
, 0);
316 case REALLY_INACTIVE
:
323 * If we reach here, the option was one we weren't prepared to
324 * cope with. So send a negative ack.
326 send_opt((cmd
== WILL ? DONT
: WONT
), option
);
329 static void process_subneg(void)
331 unsigned char b
[2048], *p
, *q
;
337 if (sb_len
== 1 && sb_buf
[0] == TELQUAL_SEND
) {
338 char logbuf
[sizeof(cfg
.termspeed
) + 80];
341 b
[2] = TELOPT_TSPEED
;
343 strcpy(b
+ 4, cfg
.termspeed
);
344 n
= 4 + strlen(cfg
.termspeed
);
347 telnet_bufsize
= sk_write(s
, b
, n
+ 2);
348 logevent("server:\tSB TSPEED SEND");
349 sprintf(logbuf
, "client:\tSB TSPEED IS %s", cfg
.termspeed
);
352 logevent("server:\tSB TSPEED <something weird>");
355 if (sb_len
== 1 && sb_buf
[0] == TELQUAL_SEND
) {
356 char logbuf
[sizeof(cfg
.termtype
) + 80];
361 for (n
= 0; cfg
.termtype
[n
]; n
++)
362 b
[n
+ 4] = (cfg
.termtype
[n
] >= 'a'
363 && cfg
.termtype
[n
] <=
364 'z' ? cfg
.termtype
[n
] + 'A' -
365 'a' : cfg
.termtype
[n
]);
368 telnet_bufsize
= sk_write(s
, b
, n
+ 6);
370 logevent("server:\tSB TTYPE SEND");
371 sprintf(logbuf
, "client:\tSB TTYPE IS %s", b
+ 4);
374 logevent("server:\tSB TTYPE <something weird>\r\n");
376 case TELOPT_OLD_ENVIRON
:
377 case TELOPT_NEW_ENVIRON
:
380 if (p
< q
&& *p
== TELQUAL_SEND
) {
383 sprintf(logbuf
, "server:\tSB %s SEND", telopt(sb_opt
));
385 if (sb_opt
== TELOPT_OLD_ENVIRON
) {
386 if (cfg
.rfc_environ
) {
394 * Try to guess the sense of VAR and VALUE.
400 } else if (*p
== BSD_VAR
) {
408 * With NEW_ENVIRON, the sense of VAR and VALUE
422 while (*e
&& *e
!= '\t')
444 telnet_bufsize
= sk_write(s
, b
, n
);
445 sprintf(logbuf
, "client:\tSB %s IS %s", telopt(sb_opt
),
446 n
== 6 ?
"<nothing>" : "<stuff>");
454 TOPLEVEL
, SEENIAC
, SEENWILL
, SEENWONT
, SEENDO
, SEENDONT
,
455 SEENSB
, SUBNEGOT
, SUBNEG_IAC
, SEENCR
456 } telnet_state
= TOPLEVEL
;
458 static void do_telnet_read(char *buf
, int len
)
462 int c
= (unsigned char) *buf
++;
464 switch (telnet_state
) {
467 if (c
== NUL
&& telnet_state
== SEENCR
)
468 telnet_state
= TOPLEVEL
;
470 telnet_state
= SEENIAC
;
476 /* I can't get the F***ing winsock to insert the urgent IAC
477 * into the right position! Even with SO_OOBINLINE it gives
478 * it to recv too soon. And of course the DM byte (that
479 * arrives in the same packet!) appears several K later!!
481 * Oh well, we do get the DM in the right place so I'll
482 * just stop hiding on the next 0xf2 and hope for the best.
488 telnet_state
= SEENCR
;
490 telnet_state
= TOPLEVEL
;
495 telnet_state
= SEENDO
;
497 telnet_state
= SEENDONT
;
499 telnet_state
= SEENWILL
;
501 telnet_state
= SEENWONT
;
503 telnet_state
= SEENSB
;
506 telnet_state
= TOPLEVEL
;
508 /* ignore everything else; print it if it's IAC */
512 telnet_state
= TOPLEVEL
;
516 proc_rec_opt(WILL
, c
);
517 telnet_state
= TOPLEVEL
;
520 proc_rec_opt(WONT
, c
);
521 telnet_state
= TOPLEVEL
;
525 telnet_state
= TOPLEVEL
;
528 proc_rec_opt(DONT
, c
);
529 telnet_state
= TOPLEVEL
;
534 telnet_state
= SUBNEGOT
;
538 telnet_state
= SUBNEG_IAC
;
541 if (sb_len
>= sb_size
) {
545 srealloc(sb_buf
, sb_size
) :
552 if (sb_len
< sb_size
)
553 sb_buf
[sb_len
++] = c
;
554 telnet_state
= SUBNEGOT
; /* in case we came here by goto */
559 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
562 telnet_state
= TOPLEVEL
;
569 static int telnet_closing(Plug plug
, char *error_msg
, int error_code
,
577 /* A socket error has occurred. */
578 connection_fatal(error_msg
);
579 } /* Otherwise, the remote side closed the connection normally. */
583 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
587 do_telnet_read(data
, len
);
591 static void telnet_sent(Plug plug
, int bufsize
)
593 telnet_bufsize
= bufsize
;
597 * Called to set up the Telnet connection.
599 * Returns an error message, or NULL on success.
601 * Also places the canonical host name into `realhost'. It must be
602 * freed by the caller.
604 static char *telnet_init(char *host
, int port
, char **realhost
)
606 static struct plug_function_table fn_table
= {
610 }, *fn_table_ptr
= &fn_table
;
620 sprintf(buf
, "Looking up host \"%.170s\"", host
);
623 addr
= sk_namelookup(host
, realhost
);
624 if ((err
= sk_addr_error(addr
)))
628 port
= 23; /* default telnet port */
634 char buf
[200], addrbuf
[100];
635 sk_getaddr(addr
, addrbuf
, 100);
636 sprintf(buf
, "Connecting to %.100s port %d", addrbuf
, port
);
639 s
= sk_new(addr
, port
, 0, 1, &fn_table_ptr
);
640 if ((err
= sk_socket_error(s
)))
646 * Initialise option states.
648 if (cfg
.passive_telnet
) {
651 for (o
= opts
; *o
; o
++)
652 if ((*o
)->state
== REQUESTED
)
653 (*o
)->state
= INACTIVE
;
657 for (o
= opts
; *o
; o
++)
658 if ((*o
)->state
== REQUESTED
)
659 send_opt((*o
)->send
, (*o
)->option
);
664 * Set up SYNCH state.
672 * Called to send data down the Telnet connection.
674 static int telnet_send(char *buf
, int len
)
677 static unsigned char iac
[2] = { IAC
, IAC
};
678 static unsigned char cr
[2] = { CR
, NUL
};
680 static unsigned char nl
[2] = { CR
, LF
};
687 while (p
< buf
+ len
) {
690 while (iswritable((unsigned char) *p
) && p
< buf
+ len
)
692 telnet_bufsize
= sk_write(s
, q
, p
- q
);
694 while (p
< buf
+ len
&& !iswritable((unsigned char) *p
)) {
696 sk_write(s
, (unsigned char) *p
== IAC ? iac
: cr
, 2);
701 return telnet_bufsize
;
705 * Called to query the current socket sendability status.
707 static int telnet_sendbuffer(void)
709 return telnet_bufsize
;
713 * Called to set the size of the window from Telnet's POV.
715 static void telnet_size(void)
720 if (s
== NULL
|| o_naws
.state
!= ACTIVE
)
731 telnet_bufsize
= sk_write(s
, b
, 9);
732 sprintf(logbuf
, "client:\tSB NAWS %d,%d",
733 ((unsigned char) b
[3] << 8) + (unsigned char) b
[4],
734 ((unsigned char) b
[5] << 8) + (unsigned char) b
[6]);
739 * Send Telnet special codes.
741 static void telnet_special(Telnet_Special code
)
752 telnet_bufsize
= sk_write(s
, b
, 2);
756 telnet_bufsize
= sk_write(s
, b
, 2);
760 telnet_bufsize
= sk_write(s
, b
, 2);
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);
799 telnet_bufsize
= sk_write(s
, "\r\n", 2);
803 telnet_bufsize
= sk_write(s
, b
, 1);
804 telnet_bufsize
= sk_write_oob(s
, b
+ 1, 1);
807 if (o_echo
.state
== INACTIVE
|| o_echo
.state
== REALLY_INACTIVE
) {
808 o_echo
.state
= REQUESTED
;
809 send_opt(o_echo
.send
, o_echo
.option
);
813 if (o_echo
.state
== ACTIVE
) {
814 o_echo
.state
= REQUESTED
;
815 send_opt(o_echo
.nsend
, o_echo
.option
);
819 if (o_they_sga
.state
== ACTIVE
) {
821 telnet_bufsize
= sk_write(s
, b
, 2);
827 static Socket
telnet_socket(void)
832 static int telnet_sendok(void)
837 static void telnet_unthrottle(int backlog
)
839 sk_set_frozen(s
, backlog
> TELNET_MAX_BACKLOG
);
842 static int telnet_ldisc(int option
)
844 if (option
== LD_ECHO
)
846 if (option
== LD_EDIT
)
851 Backend telnet_backend
= {