]>
git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/termstat.c
0705dec0fee0a16131c24073bccf04dbc071397a
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char sccsid
[] = "@(#)termstat.c 8.2 (Berkeley) 5/30/95";
66 int def_tspeed
= -1, def_rspeed
= -1;
68 int def_row
= 0, def_col
= 0;
71 static int _terminit
= 0;
74 #if defined(CRAY2) && defined(UNICOS5)
75 int newmap
= 1; /* nonzero if \n maps to ^M^J */
82 * This function handles all management of linemode.
84 * Linemode allows the client to do the local editing of data
85 * and send only complete lines to the server. Linemode state is
86 * based on the state of the pty driver. If the pty is set for
87 * external processing, then we can use linemode. Further, if we
88 * can use real linemode, then we can look at the edit control bits
89 * in the pty to determine what editing the client should do.
91 * Linemode support uses the following state flags to keep track of
92 * current and desired linemode state.
93 * alwayslinemode : true if -l was specified on the telnetd
94 * command line. It means to have linemode on as much as
97 * lmodetype: signifies whether the client can
98 * handle real linemode, or if use of kludgeomatic linemode
99 * is preferred. It will be set to one of the following:
100 * REAL_LINEMODE : use linemode option
101 * NO_KLUDGE : don't initiate kludge linemode.
102 * KLUDGE_LINEMODE : use kludge linemode
103 * NO_LINEMODE : client is ignorant of linemode
105 * linemode, uselinemode : linemode is true if linemode
106 * is currently on, uselinemode is the state that we wish
107 * to be in. If another function wishes to turn linemode
108 * on or off, it sets or clears uselinemode.
110 * editmode, useeditmode : like linemode/uselinemode, but
111 * these contain the edit mode states (edit and trapsig).
113 * The state variables correspond to some of the state information
116 * In real linemode, this corresponds to whether the pty
117 * expects external processing of incoming data.
118 * In kludge linemode, this more closely corresponds to the
119 * whether normal processing is on or not. (ICANON in
120 * system V, or COOKED mode in BSD.)
121 * If the -l option was specified (alwayslinemode), then
122 * an attempt is made to force external processing on at
125 * The following heuristics are applied to determine linemode
126 * handling within the server.
127 * 1) Early on in starting up the server, an attempt is made
128 * to negotiate the linemode option. If this succeeds
129 * then lmodetype is set to REAL_LINEMODE and all linemode
130 * processing occurs in the context of the linemode option.
131 * 2) If the attempt to negotiate the linemode option failed,
132 * and the "-k" (don't initiate kludge linemode) isn't set,
133 * then we try to use kludge linemode. We test for this
134 * capability by sending "do Timing Mark". If a positive
135 * response comes back, then we assume that the client
136 * understands kludge linemode (ech!) and the
137 * lmodetype flag is set to KLUDGE_LINEMODE.
138 * 3) Otherwise, linemode is not supported at all and
139 * lmodetype remains set to NO_LINEMODE (which happens
140 * to be 0 for convenience).
141 * 4) At any time a command arrives that implies a higher
142 * state of linemode support in the client, we move to that
145 * A short explanation of kludge linemode is in order here.
146 * 1) The heuristic to determine support for kludge linemode
147 * is to send a do timing mark. We assume that a client
148 * that supports timing marks also supports kludge linemode.
149 * A risky proposition at best.
150 * 2) Further negotiation of linemode is done by changing the
151 * the server's state regarding SGA. If server will SGA,
152 * then linemode is off, if server won't SGA, then linemode
159 int need_will_echo
= 0;
161 #if defined(CRAY2) && defined(UNICOS5)
163 * Keep track of that ol' CR/NL mapping while we're in the
166 newmap
= tty_isnewmap();
167 #endif /* defined(CRAY2) && defined(UNICOS5) */
170 * Check for state of BINARY options.
172 if (tty_isbinaryin()) {
173 if (his_want_state_is_wont(TELOPT_BINARY
))
174 send_do(TELOPT_BINARY
, 1);
176 if (his_want_state_is_will(TELOPT_BINARY
))
177 send_dont(TELOPT_BINARY
, 1);
180 if (tty_isbinaryout()) {
181 if (my_want_state_is_wont(TELOPT_BINARY
))
182 send_will(TELOPT_BINARY
, 1);
184 if (my_want_state_is_will(TELOPT_BINARY
))
185 send_wont(TELOPT_BINARY
, 1);
189 * Check for changes to flow control if client supports it.
194 * Check linemode on/off state
196 uselinemode
= tty_linemode();
199 * If alwayslinemode is on, and pty is changing to turn it off, then
200 * force linemode back on.
202 if (alwayslinemode
&& linemode
&& !uselinemode
) {
204 tty_setlinemode(uselinemode
);
209 * If the terminal is not echoing, but editing is enabled,
210 * something like password input is going to happen, so
211 * if we the other side is not currently sending encrypted
212 * data, ask the other side to start encrypting.
214 if (his_state_is_will(TELOPT_ENCRYPT
)) {
215 static int enc_passwd
= 0;
216 if (uselinemode
&& !tty_isecho() && tty_isediting()
217 && (enc_passwd
== 0) && !decrypt_input
) {
218 encrypt_send_request_start();
220 } else if (enc_passwd
) {
221 encrypt_send_request_end();
225 #endif /* ENCRYPTION */
228 * Do echo mode handling as soon as we know what the
229 * linemode is going to be.
230 * If the pty has echo turned off, then tell the client that
231 * the server will echo. If echo is on, then the server
232 * will echo if in character mode, but in linemode the
233 * client should do local echoing. The state machine will
234 * not send anything if it is unnecessary, so don't worry
237 * If we need to send the WILL ECHO (because echo is off),
238 * then delay that until after we have changed the MODE.
239 * This way, when the user is turning off both editing
240 * and echo, the client will get editing turned off first.
241 * This keeps the client from going into encryption mode
242 * and then right back out if it is doing auto-encryption
243 * when passwords are being typed.
247 send_wont(TELOPT_ECHO
, 1);
250 #ifdef KLUDGELINEMODE
251 if (lmodetype
== KLUDGE_OK
)
252 lmodetype
= KLUDGE_LINEMODE
;
257 * If linemode is being turned off, send appropriate
258 * command and then we're all done.
260 if (!uselinemode
&& linemode
) {
261 # ifdef KLUDGELINEMODE
262 if (lmodetype
== REAL_LINEMODE
) {
263 # endif /* KLUDGELINEMODE */
264 send_dont(TELOPT_LINEMODE
, 1);
265 # ifdef KLUDGELINEMODE
266 } else if (lmodetype
== KLUDGE_LINEMODE
)
267 send_will(TELOPT_SGA
, 1);
268 # endif /* KLUDGELINEMODE */
269 send_will(TELOPT_ECHO
, 1);
270 linemode
= uselinemode
;
274 # ifdef KLUDGELINEMODE
276 * If using real linemode check edit modes for possible later use.
277 * If we are in kludge linemode, do the SGA negotiation.
279 if (lmodetype
== REAL_LINEMODE
) {
280 # endif /* KLUDGELINEMODE */
283 useeditmode
|= MODE_EDIT
;
285 useeditmode
|= MODE_TRAPSIG
;
287 useeditmode
|= MODE_SOFT_TAB
;
289 useeditmode
|= MODE_LIT_ECHO
;
290 # ifdef KLUDGELINEMODE
291 } else if (lmodetype
== KLUDGE_LINEMODE
) {
292 if (tty_isediting() && uselinemode
)
293 send_wont(TELOPT_SGA
, 1);
295 send_will(TELOPT_SGA
, 1);
297 # endif /* KLUDGELINEMODE */
300 * Negotiate linemode on if pty state has changed to turn it on.
301 * Send appropriate command and send along edit mode, then all done.
303 if (uselinemode
&& !linemode
) {
304 # ifdef KLUDGELINEMODE
305 if (lmodetype
== KLUDGE_LINEMODE
) {
306 send_wont(TELOPT_SGA
, 1);
307 } else if (lmodetype
== REAL_LINEMODE
) {
308 # endif /* KLUDGELINEMODE */
309 send_do(TELOPT_LINEMODE
, 1);
310 /* send along edit modes */
311 (void) sprintf(nfrontp
, "%c%c%c%c%c%c%c", IAC
, SB
,
312 TELOPT_LINEMODE
, LM_MODE
, useeditmode
,
315 editmode
= useeditmode
;
316 # ifdef KLUDGELINEMODE
318 # endif /* KLUDGELINEMODE */
319 linemode
= uselinemode
;
323 # ifdef KLUDGELINEMODE
325 * None of what follows is of any value if not using
328 if (lmodetype
< REAL_LINEMODE
)
330 # endif /* KLUDGELINEMODE */
332 if (linemode
&& his_state_is_will(TELOPT_LINEMODE
)) {
334 * If edit mode changed, send edit mode.
336 if (useeditmode
!= editmode
) {
338 * Send along appropriate edit mode mask.
340 (void) sprintf(nfrontp
, "%c%c%c%c%c%c%c", IAC
, SB
,
341 TELOPT_LINEMODE
, LM_MODE
, useeditmode
,
344 editmode
= useeditmode
;
349 * Check for changes to special characters in use.
358 send_will(TELOPT_ECHO
, 1);
360 * Some things should be deferred until after the pty state has
361 * been set by the local process. Do those things that have been
362 * deferred now. This only happens once.
364 if (_terminit
== 0) {
373 } /* end of localstat */
374 #endif /* LINEMODE */
379 * Check for changes to flow control
384 if (his_state_is_will(TELOPT_LFLOW
)) {
385 if (tty_flowmode() != flowmode
) {
386 flowmode
= tty_flowmode();
387 (void) sprintf(nfrontp
, "%c%c%c%c%c%c",
388 IAC
, SB
, TELOPT_LFLOW
,
389 flowmode
? LFLOW_ON
: LFLOW_OFF
,
393 if (tty_restartany() != restartany
) {
394 restartany
= tty_restartany();
395 (void) sprintf(nfrontp
, "%c%c%c%c%c%c",
396 IAC
, SB
, TELOPT_LFLOW
,
397 restartany
? LFLOW_RESTART_ANY
408 * Process linemode related requests from the client.
409 * Client can request a change to only one of linemode, editmode or slc's
410 * at a time, and if using kludge linemode, then only linemode may be
414 clientstat(code
, parm1
, parm2
)
415 register int code
, parm1
, parm2
;
420 * Get a copy of terminal characteristics.
425 * Process request from client. code tells what it is.
429 case TELOPT_LINEMODE
:
431 * Don't do anything unless client is asking us to change
434 uselinemode
= (parm1
== WILL
);
435 if (uselinemode
!= linemode
) {
436 # ifdef KLUDGELINEMODE
438 * If using kludge linemode, make sure that
439 * we can do what the client asks.
440 * We can not turn off linemode if alwayslinemode
441 * and the ICANON bit is set.
443 if (lmodetype
== KLUDGE_LINEMODE
) {
444 if (alwayslinemode
&& tty_isediting()) {
450 * Quit now if we can't do it.
452 if (uselinemode
== linemode
)
456 * If using real linemode and linemode is being
457 * turned on, send along the edit mode mask.
459 if (lmodetype
== REAL_LINEMODE
&& uselinemode
)
460 # else /* KLUDGELINEMODE */
462 # endif /* KLUDGELINEMODE */
466 useeditmode
|= MODE_EDIT
;
468 useeditmode
|= MODE_TRAPSIG
;
470 useeditmode
|= MODE_SOFT_TAB
;
472 useeditmode
|= MODE_LIT_ECHO
;
473 (void) sprintf(nfrontp
, "%c%c%c%c%c%c%c", IAC
,
474 SB
, TELOPT_LINEMODE
, LM_MODE
,
475 useeditmode
, IAC
, SE
);
477 editmode
= useeditmode
;
481 tty_setlinemode(uselinemode
);
483 linemode
= uselinemode
;
486 send_will(TELOPT_ECHO
, 1);
492 register int ack
, changed
;
495 * Client has sent along a mode mask. If it agrees with
496 * what we are currently doing, ignore it; if not, it could
497 * be viewed as a request to change. Note that the server
498 * will change to the modes in an ack if it is different from
499 * what we currently have, but we will not ack the ack.
501 useeditmode
&= MODE_MASK
;
502 ack
= (useeditmode
& MODE_ACK
);
503 useeditmode
&= ~MODE_ACK
;
505 if (changed
= (useeditmode
^ editmode
)) {
507 * This check is for a timing problem. If the
508 * state of the tty has changed (due to the user
509 * application) we need to process that info
510 * before we write in the state contained in the
511 * ack!!! This gets out the new MODE request,
512 * and when the ack to that command comes back
513 * we'll set it and be in the right mode.
517 if (changed
& MODE_EDIT
)
518 tty_setedit(useeditmode
& MODE_EDIT
);
520 if (changed
& MODE_TRAPSIG
)
521 tty_setsig(useeditmode
& MODE_TRAPSIG
);
523 if (changed
& MODE_SOFT_TAB
)
524 tty_setsofttab(useeditmode
& MODE_SOFT_TAB
);
526 if (changed
& MODE_LIT_ECHO
)
527 tty_setlitecho(useeditmode
& MODE_LIT_ECHO
);
532 (void) sprintf(nfrontp
, "%c%c%c%c%c%c%c", IAC
,
533 SB
, TELOPT_LINEMODE
, LM_MODE
,
534 useeditmode
|MODE_ACK
,
539 editmode
= useeditmode
;
544 } /* end of case LM_MODE */
545 #endif /* LINEMODE */
556 * Defer changing window size until after terminal is
561 #endif /* LINEMODE */
564 * Change window size as requested by client.
569 (void) ioctl(pty
, TIOCSWINSZ
, (char *)&ws
);
571 #endif /* TIOCSWINSZ */
581 * Defer changing the terminal speed.
585 #endif /* LINEMODE */
587 * Change terminal speed as requested by client.
588 * We set the receive speed first, so that if we can't
589 * store seperate receive and transmit speeds, the transmit
590 * speed will take precedence.
598 } /* end of case TELOPT_TSPEED */
603 } /* end of switch */
605 #if defined(CRAY2) && defined(UNICOS5)
607 * Just in case of the likely event that we changed the pty state.
610 #endif /* defined(CRAY2) && defined(UNICOS5) */
614 } /* end of clientstat */
616 #if defined(CRAY2) && defined(UNICOS5)
631 #endif /* defined(CRAY2) && defined(UNICOS5) */
637 * Some things should not be done until after the login process has started
638 * and all the pty modes are set to what they are supposed to be. This
639 * function is called when the pty state has been processed for the first time.
640 * It calls other functions that do things that were deferred in each module.
647 * local stuff that got deferred.
649 if (def_tspeed
!= -1) {
650 clientstat(TELOPT_TSPEED
, def_tspeed
, def_rspeed
);
651 def_tspeed
= def_rspeed
= 0;
655 if (def_col
|| def_row
) {
658 memset((char *)&ws
, 0, sizeof(ws
));
661 (void) ioctl(pty
, TIOCSWINSZ
, (char *)&ws
);
666 * The only other module that currently defers anything.
670 } /* end of defer_terminit */
675 * Returns true if the pty state has been processed yet.
682 } /* end of terminit */
683 #endif /* LINEMODE */