xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c (revision 473:9c6ea6da29de)
10Sstevel@tonic-gate /*
2*473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  * usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
120Sstevel@tonic-gate /*
130Sstevel@tonic-gate  * Copyright (c) 1988, 1993
140Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
170Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
180Sstevel@tonic-gate  * are met:
190Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
200Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
210Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
220Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
230Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
240Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
250Sstevel@tonic-gate  *    must display the following acknowledgement:
260Sstevel@tonic-gate  *	This product includes software developed by the University of
270Sstevel@tonic-gate  *	California, Berkeley and its contributors.
280Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
290Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
300Sstevel@tonic-gate  *    without specific prior written permission.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
330Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
340Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
350Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
360Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
370Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
380Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
390Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
400Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
410Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
420Sstevel@tonic-gate  * SUCH DAMAGE.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #ifndef lint
460Sstevel@tonic-gate static char sccsid[] = "@(#)ring.c	8.1 (Berkeley) 6/6/93";
470Sstevel@tonic-gate #endif /* not lint */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * This defines a structure for a ring buffer.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * The circular buffer has two parts:
530Sstevel@tonic-gate  * (((
540Sstevel@tonic-gate  *	full:	[consume, supply)
550Sstevel@tonic-gate  *	empty:	[supply, consume)
560Sstevel@tonic-gate  * ]]]
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include	<stdio.h>
610Sstevel@tonic-gate #include	<errno.h>
620Sstevel@tonic-gate #include	<string.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include	<sys/types.h>
650Sstevel@tonic-gate #include	<sys/socket.h>
660Sstevel@tonic-gate #include	<sys/sysmacros.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include	"ring.h"
690Sstevel@tonic-gate #include	"general.h"
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	ring_subtract(d, a, b)	(((a)-(b) >= 0)? \
730Sstevel@tonic-gate 					(a)-(b): (((a)-(b))+(d)->size))
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	ring_increment(d, a, c)	(((a)+(c) < (d)->top)? \
760Sstevel@tonic-gate 					(a)+(c) : (((a)+(c))-(d)->size))
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	ring_decrement(d, a, c)	(((a)-(c) >= (d)->bottom)? \
790Sstevel@tonic-gate 					(a)-(c) : (((a)-(c))-(d)->size))
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * The following is a clock, used to determine full, empty, etc.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * There is some trickiness here.  Since the ring buffers are initialized
860Sstevel@tonic-gate  * to ZERO on allocation, we need to make sure, when interpreting the
870Sstevel@tonic-gate  * clock, that when the times are EQUAL, then the buffer is FULL.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate ulong_t ring_clock = 0;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	ring_empty(d) (((d)->consume == (d)->supply) && \
930Sstevel@tonic-gate 				((d)->consumetime >= (d)->supplytime))
940Sstevel@tonic-gate #define	ring_full(d) (((d)->supply == (d)->consume) && \
950Sstevel@tonic-gate 				((d)->supplytime > (d)->consumetime))
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* Buffer state transition routines */
1020Sstevel@tonic-gate 
103*473Sbw int
ring_init(ring,buffer,count)1040Sstevel@tonic-gate     ring_init(ring, buffer, count)
1050Sstevel@tonic-gate Ring *ring;
1060Sstevel@tonic-gate     unsigned char *buffer;
1070Sstevel@tonic-gate     int count;
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	(void) memset(ring, 0, sizeof (*ring));
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	ring->size = count;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	ring->supply = ring->consume = ring->bottom = buffer;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	ring->top = ring->bottom+ring->size;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	ring->clearto = 0;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	return (1);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* Mark routines */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * Mark the most recently supplied byte.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate void
ring_mark(ring)1290Sstevel@tonic-gate ring_mark(ring)
1300Sstevel@tonic-gate 	Ring *ring;
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	ring->mark = ring_decrement(ring, ring->supply, 1);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * Is the ring pointing to the mark?
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
ring_at_mark(ring)1400Sstevel@tonic-gate ring_at_mark(ring)
1410Sstevel@tonic-gate 	Ring *ring;
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	if (ring->mark == ring->consume) {
1440Sstevel@tonic-gate 		return (1);
1450Sstevel@tonic-gate 	} else {
1460Sstevel@tonic-gate 		return (0);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Clear any mark set on the ring.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate void
ring_clear_mark(ring)1550Sstevel@tonic-gate ring_clear_mark(ring)
1560Sstevel@tonic-gate 	Ring *ring;
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	ring->mark = 0;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * Add characters from current segment to ring buffer.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate     void
ring_supplied(ring,count)1650Sstevel@tonic-gate ring_supplied(ring, count)
1660Sstevel@tonic-gate     Ring *ring;
1670Sstevel@tonic-gate     int count;
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate     ring->supply = ring_increment(ring, ring->supply, count);
1700Sstevel@tonic-gate     ring->supplytime = ++ring_clock;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * We have just consumed "c" bytes.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate void
ring_consumed(ring,count)1770Sstevel@tonic-gate ring_consumed(ring, count)
1780Sstevel@tonic-gate 	Ring *ring;
1790Sstevel@tonic-gate 	int count;
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	if (count == 0)	/* don't update anything */
1820Sstevel@tonic-gate 		return;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (ring->mark &&
1850Sstevel@tonic-gate 	    (ring_subtract(ring, ring->mark, ring->consume) < count)) {
1860Sstevel@tonic-gate 		ring->mark = 0;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (ring->consume < ring->clearto &&
1900Sstevel@tonic-gate 	    ring->clearto <= ring->consume + count)
1910Sstevel@tonic-gate 		ring->clearto = 0;
1920Sstevel@tonic-gate 	else if (ring->consume + count > ring->top &&
1930Sstevel@tonic-gate 	    ring->bottom <= ring->clearto &&
1940Sstevel@tonic-gate 	    ring->bottom + ((ring->consume + count) - ring->top))
1950Sstevel@tonic-gate 		ring->clearto = 0;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	ring->consume = ring_increment(ring, ring->consume, count);
1980Sstevel@tonic-gate 	ring->consumetime = ++ring_clock;
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * Try to encourage "ring_empty_consecutive()" to be large.
2010Sstevel@tonic-gate 	 */
2020Sstevel@tonic-gate 	if (ring_empty(ring)) {
2030Sstevel@tonic-gate 		ring->consume = ring->supply = ring->bottom;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /* Buffer state query routines */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /* Number of bytes that may be supplied */
2130Sstevel@tonic-gate int
ring_empty_count(ring)2140Sstevel@tonic-gate ring_empty_count(ring)
2150Sstevel@tonic-gate 	Ring *ring;
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	if (ring_empty(ring)) {	/* if empty */
2180Sstevel@tonic-gate 		return (ring->size);
2190Sstevel@tonic-gate 	} else {
2200Sstevel@tonic-gate 		return (ring_subtract(ring, ring->consume, ring->supply));
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /* number of CONSECUTIVE bytes that may be supplied */
2250Sstevel@tonic-gate int
ring_empty_consecutive(ring)2260Sstevel@tonic-gate ring_empty_consecutive(ring)
2270Sstevel@tonic-gate 	Ring *ring;
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	if ((ring->consume < ring->supply) || ring_empty(ring)) {
2300Sstevel@tonic-gate 		/*
2310Sstevel@tonic-gate 		 * if consume is "below" supply, or empty, then
2320Sstevel@tonic-gate 		 * return distance to the top
2330Sstevel@tonic-gate 		 */
2340Sstevel@tonic-gate 		return (ring_subtract(ring, ring->top, ring->supply));
2350Sstevel@tonic-gate 	} else {
2360Sstevel@tonic-gate 		/*
2370Sstevel@tonic-gate 		 * else, return what we may.
2380Sstevel@tonic-gate 		 */
2390Sstevel@tonic-gate 		return (ring_subtract(ring, ring->consume, ring->supply));
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Return the number of bytes that are available for consuming
2450Sstevel@tonic-gate  * (but don't give more than enough to get to cross over set mark)
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate int
ring_full_count(ring)2490Sstevel@tonic-gate ring_full_count(ring)
2500Sstevel@tonic-gate 	Ring *ring;
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	if ((ring->mark == 0) || (ring->mark == ring->consume)) {
2530Sstevel@tonic-gate 		if (ring_full(ring)) {
2540Sstevel@tonic-gate 			return (ring->size);	/* nothing consumed, but full */
2550Sstevel@tonic-gate 		} else {
2560Sstevel@tonic-gate 			return (ring_subtract(ring, ring->supply,
2570Sstevel@tonic-gate 			    ring->consume));
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 	} else {
2600Sstevel@tonic-gate 		return (ring_subtract(ring, ring->mark, ring->consume));
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * Return the number of CONSECUTIVE bytes available for consuming.
2660Sstevel@tonic-gate  * However, don't return more than enough to cross over set mark.
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate int
ring_full_consecutive(ring)2690Sstevel@tonic-gate ring_full_consecutive(ring)
2700Sstevel@tonic-gate 	Ring *ring;
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	if ((ring->mark == 0) || (ring->mark == ring->consume)) {
2730Sstevel@tonic-gate 		if ((ring->supply < ring->consume) || ring_full(ring)) {
2740Sstevel@tonic-gate 			return (ring_subtract(ring, ring->top, ring->consume));
2750Sstevel@tonic-gate 		} else {
2760Sstevel@tonic-gate 			return (ring_subtract(ring, ring->supply,
2770Sstevel@tonic-gate 			    ring->consume));
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 	} else {
2800Sstevel@tonic-gate 		if (ring->mark < ring->consume) {
2810Sstevel@tonic-gate 			return (ring_subtract(ring, ring->top, ring->consume));
2820Sstevel@tonic-gate 		} else {	/* Else, distance to mark */
2830Sstevel@tonic-gate 			return (ring_subtract(ring, ring->mark, ring->consume));
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Move data into the "supply" portion of of the ring buffer.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate void
ring_supply_data(ring,buffer,count)2920Sstevel@tonic-gate ring_supply_data(ring, buffer, count)
2930Sstevel@tonic-gate 	Ring *ring;
2940Sstevel@tonic-gate 	unsigned char *buffer;
2950Sstevel@tonic-gate 	int count;
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	int i;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	while (count) {
3000Sstevel@tonic-gate 		i = MIN(count, ring_empty_consecutive(ring));
3010Sstevel@tonic-gate 		(void) memcpy(ring->supply, buffer, i);
3020Sstevel@tonic-gate 		ring_supplied(ring, i);
3030Sstevel@tonic-gate 		count -= i;
3040Sstevel@tonic-gate 		buffer += i;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate #ifdef notdef
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Move data from the "consume" portion of the ring buffer
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate void
ring_consume_data(ring,buffer,count)3140Sstevel@tonic-gate ring_consume_data(ring, buffer, count)
3150Sstevel@tonic-gate 	Ring *ring;
3160Sstevel@tonic-gate 	unsigned char *buffer;
3170Sstevel@tonic-gate 	int count;
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	int i;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	while (count) {
3220Sstevel@tonic-gate 		i = MIN(count, ring_full_consecutive(ring));
3230Sstevel@tonic-gate 		memcpy(buffer, ring->consume, i);
3240Sstevel@tonic-gate 		ring_consumed(ring, i);
3250Sstevel@tonic-gate 		count -= i;
3260Sstevel@tonic-gate 		buffer += i;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate #endif
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate void
ring_encrypt(ring,encryptor)3320Sstevel@tonic-gate ring_encrypt(ring, encryptor)
3330Sstevel@tonic-gate 	Ring *ring;
3340Sstevel@tonic-gate 	void (*encryptor)();
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	unsigned char *s, *c;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (ring_empty(ring) || ring->clearto == ring->supply)
3390Sstevel@tonic-gate 		return;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if ((c = ring->clearto) == NULL)
3420Sstevel@tonic-gate 		c = ring->consume;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	s = ring->supply;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (s <= c) {
3470Sstevel@tonic-gate 		(*encryptor)(c, ring->top - c);
3480Sstevel@tonic-gate 		(*encryptor)(ring->bottom, s - ring->bottom);
3490Sstevel@tonic-gate 	} else
3500Sstevel@tonic-gate 		(*encryptor)(c, s - c);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	ring->clearto = ring->supply;
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate     void
ring_clearto(ring)3560Sstevel@tonic-gate ring_clearto(ring)
3570Sstevel@tonic-gate     Ring *ring;
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate     if (!ring_empty(ring))
3600Sstevel@tonic-gate 	ring->clearto = ring->supply;
3610Sstevel@tonic-gate     else
3620Sstevel@tonic-gate 	ring->clearto = 0;
3630Sstevel@tonic-gate }
364