]>
git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/slc.c
b7c892a8fe0ca41d7b1c30cb83cdba33da0dfb7a
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)slc.c 8.1 (Berkeley) 6/4/93";
38 static const char rcsid
[] =
39 "$FreeBSD: src/libexec/telnetd/slc.c,v 1.10 2001/07/20 15:14:03 ru Exp $";
48 static unsigned char *def_slcbuf
= (unsigned char *)0;
49 static int def_slclen
= 0;
50 static int slcchange
; /* change to slc is requested */
51 static unsigned char *slcptr
; /* pointer into slc buffer */
52 static unsigned char slcbuf
[NSLC
*6]; /* buffer for slc negotiation */
57 * Write out the current special characters to the client.
65 * Send out list of triplets of special characters
66 * to client. We only send info on the characters
67 * that are currently supported.
69 for (i
= 1; i
<= NSLC
; i
++) {
70 if ((slctab
[i
].defset
.flag
& SLC_LEVELBITS
) == SLC_NOSUPPORT
)
72 add_slc((unsigned char)i
, slctab
[i
].current
.flag
,
73 slctab
[i
].current
.val
);
76 } /* end of send_slc */
81 * Set pty special characters to all the defaults.
88 for (i
= 1; i
<= NSLC
; i
++) {
89 slctab
[i
].current
.val
= slctab
[i
].defset
.val
;
90 if (slctab
[i
].current
.val
== (cc_t
)(_POSIX_VDISABLE
))
91 slctab
[i
].current
.flag
= SLC_NOSUPPORT
;
93 slctab
[i
].current
.flag
= slctab
[i
].defset
.flag
;
95 *(slctab
[i
].sptr
) = slctab
[i
].defset
.val
;
100 } /* end of default_slc */
101 #endif /* LINEMODE */
106 * Initialize the slc mapping table.
115 for (i
= 1; i
<= NSLC
; i
++) {
116 slctab
[i
].defset
.flag
=
117 spcset(i
, &slctab
[i
].defset
.val
, &slctab
[i
].sptr
);
118 slctab
[i
].current
.flag
= SLC_NOSUPPORT
;
119 slctab
[i
].current
.val
= 0;
122 } /* end of get_slc_defaults */
128 * Add an slc triplet to the slc buffer.
131 add_slc(func
, flag
, val
)
132 register char func
, flag
;
136 if ((*slcptr
++ = (unsigned char)func
) == 0xff)
139 if ((*slcptr
++ = (unsigned char)flag
) == 0xff)
142 if ((*slcptr
++ = (unsigned char)val
) == 0xff)
145 } /* end of add_slc */
150 * Get ready to process incoming slc's and respond to them.
152 * The parameter getit is non-zero if it is necessary to grab a copy
153 * of the terminal control structures.
163 (void) sprintf((char *)slcbuf
, "%c%c%c%c",
164 IAC
, SB
, TELOPT_LINEMODE
, LM_SLC
);
167 } /* end of start_slc */
172 * Finish up the slc negotiation. If something to send, then send it.
176 register unsigned char **bufp
;
181 * If a change has occured, store the new terminal control
182 * structures back to the terminal driver.
189 * If the pty state has not yet been fully processed and there is a
190 * deferred slc request from the client, then do not send any
191 * sort of slc negotiation now. We will respond to the client's
194 if (def_slcbuf
&& (terminit() == 0)) {
198 if (slcptr
> (slcbuf
+ 4)) {
201 return(slcptr
- slcbuf
- 4);
203 (void) sprintf((char *)slcptr
, "%c%c", IAC
, SE
);
205 len
= slcptr
- slcbuf
;
206 output_datalen(slcbuf
, len
);
207 netflush(); /* force it out immediately */
208 DIAG(TD_OPTIONS
, printsub('>', slcbuf
+2, len
-2););
213 } /* end of end_slc */
218 * Figure out what to do about the client's slc
221 process_slc(func
, flag
, val
)
222 register unsigned char func
, flag
;
225 register int hislevel
, mylevel
, ack
;
228 * Ensure that we know something about this function
231 add_slc(func
, SLC_NOSUPPORT
, 0);
236 * Process the special case requests of 0 SLC_DEFAULT 0
237 * and 0 SLC_VARIABLE 0. Be a little forgiving here, don't
238 * worry about whether the value is actually 0 or not.
241 if ((flag
= flag
& SLC_LEVELBITS
) == SLC_DEFAULT
) {
244 } else if (flag
== SLC_VARIABLE
) {
251 * Appears to be a function that we know something about. So
252 * get on with it and see what we know.
255 hislevel
= flag
& SLC_LEVELBITS
;
256 mylevel
= slctab
[func
].current
.flag
& SLC_LEVELBITS
;
257 ack
= flag
& SLC_ACK
;
259 * ignore the command if:
260 * the function value and level are the same as what we already have;
261 * or the level is the same and the ack bit is set
263 if (hislevel
== mylevel
&& (val
== slctab
[func
].current
.val
|| ack
)) {
267 * If we get here, we got an ack, but the levels don't match.
268 * This shouldn't happen. If it does, it is probably because
269 * we have sent two requests to set a variable without getting
270 * a response between them, and this is the first response.
271 * So, ignore it, and wait for the next response.
275 change_slc(func
, flag
, val
);
278 } /* end of process_slc */
283 * Process a request to change one of our special characters.
284 * Compare client's request with what we are capable of supporting.
287 change_slc(func
, flag
, val
)
288 register char func
, flag
;
291 register int hislevel
, mylevel
;
293 hislevel
= flag
& SLC_LEVELBITS
;
294 mylevel
= slctab
[(int)func
].defset
.flag
& SLC_LEVELBITS
;
296 * If client is setting a function to NOSUPPORT
297 * or DEFAULT, then we can easily and directly
298 * accomodate the request.
300 if (hislevel
== SLC_NOSUPPORT
) {
301 slctab
[(int)func
].current
.flag
= flag
;
302 slctab
[(int)func
].current
.val
= (cc_t
)_POSIX_VDISABLE
;
304 add_slc(func
, flag
, val
);
307 if (hislevel
== SLC_DEFAULT
) {
309 * Special case here. If client tells us to use
310 * the default on a function we don't support, then
311 * return NOSUPPORT instead of what we may have as a
312 * default level of DEFAULT.
314 if (mylevel
== SLC_DEFAULT
) {
315 slctab
[(int)func
].current
.flag
= SLC_NOSUPPORT
;
317 slctab
[(int)func
].current
.flag
= slctab
[(int)func
].defset
.flag
;
319 slctab
[(int)func
].current
.val
= slctab
[(int)func
].defset
.val
;
320 add_slc(func
, slctab
[(int)func
].current
.flag
,
321 slctab
[(int)func
].current
.val
);
326 * Client wants us to change to a new value or he
327 * is telling us that he can't change to our value.
328 * Some of the slc's we support and can change,
329 * some we do support but can't change,
330 * and others we don't support at all.
331 * If we can change it then we have a pointer to
332 * the place to put the new value, so change it,
333 * otherwise, continue the negotiation.
335 if (slctab
[(int)func
].sptr
) {
337 * We can change this one.
339 slctab
[(int)func
].current
.val
= val
;
340 *(slctab
[(int)func
].sptr
) = val
;
341 slctab
[(int)func
].current
.flag
= flag
;
344 add_slc(func
, flag
, val
);
347 * It is not possible for us to support this
348 * request as he asks.
350 * If our level is DEFAULT, then just ack whatever was
353 * If he can't change and we can't change,
354 * then degenerate to NOSUPPORT.
356 * Otherwise we send our level back to him, (CANTCHANGE
357 * or NOSUPPORT) and if CANTCHANGE, send
360 if (mylevel
== SLC_DEFAULT
) {
361 slctab
[(int)func
].current
.flag
= flag
;
362 slctab
[(int)func
].current
.val
= val
;
364 } else if (hislevel
== SLC_CANTCHANGE
&&
365 mylevel
== SLC_CANTCHANGE
) {
366 flag
&= ~SLC_LEVELBITS
;
367 flag
|= SLC_NOSUPPORT
;
368 slctab
[(int)func
].current
.flag
= flag
;
370 flag
&= ~SLC_LEVELBITS
;
372 slctab
[(int)func
].current
.flag
= flag
;
373 if (mylevel
== SLC_CANTCHANGE
) {
374 slctab
[(int)func
].current
.val
=
375 slctab
[(int)func
].defset
.val
;
376 val
= slctab
[(int)func
].current
.val
;
379 add_slc(func
, flag
, val
);
382 } /* end of change_slc */
384 #if defined(USE_TERMIO) && (VEOF == VMIN)
385 cc_t oldeofc
= '\004';
391 * Check the special characters in use and notify the client if any have
392 * changed. Only those characters that are capable of being changed are
393 * likely to have changed. If a local change occurs, kick the support level
394 * and flags up to the defaults.
401 for (i
= 1; i
<= NSLC
; i
++) {
402 #if defined(USE_TERMIO) && (VEOF == VMIN)
404 * In a perfect world this would be a neat little
405 * function. But in this world, we should not notify
406 * client of changes to the VEOF char when
407 * ICANON is off, because it is not representing
408 * a special character.
411 if (!tty_isediting())
413 else if (slctab
[i
].sptr
)
414 oldeofc
= *(slctab
[i
].sptr
);
416 #endif /* defined(USE_TERMIO) && defined(SYSV_TERMIO) */
417 if (slctab
[i
].sptr
&&
418 (*(slctab
[i
].sptr
) != slctab
[i
].current
.val
)) {
419 slctab
[i
].current
.val
= *(slctab
[i
].sptr
);
420 if (*(slctab
[i
].sptr
) == (cc_t
)_POSIX_VDISABLE
)
421 slctab
[i
].current
.flag
= SLC_NOSUPPORT
;
423 slctab
[i
].current
.flag
= slctab
[i
].defset
.flag
;
424 add_slc((unsigned char)i
, slctab
[i
].current
.flag
,
425 slctab
[i
].current
.val
);
433 * Process an slc option buffer. Defer processing of incoming slc's
434 * until after the terminal state has been processed. Save the first slc
435 * request that comes along, but discard all others.
437 * ptr points to the beginning of the buffer, len is the length.
441 register unsigned char *ptr
;
444 register unsigned char func
, flag
;
446 register unsigned char *end
= ptr
+ len
;
448 if (terminit()) { /* go ahead */
451 if (ptr
>= end
) break;
453 if (ptr
>= end
) break;
456 process_slc(func
, flag
, val
);
461 * save this slc buffer if it is the first, otherwise dump
464 if (def_slcbuf
== (unsigned char *)0) {
466 def_slcbuf
= (unsigned char *)malloc((unsigned)len
);
467 if (def_slcbuf
== (unsigned char *)0)
468 return; /* too bad */
469 memmove(def_slcbuf
, ptr
, len
);
473 } /* end of do_opt_slc */
478 * Do slc stuff that was deferred.
485 do_opt_slc(def_slcbuf
, def_slclen
);
488 def_slcbuf
= (unsigned char *)0;
492 } /* end of deferslc */
494 #endif /* LINEMODE */