xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/telnet/network.c (revision 1767:c145a59c44b9)
10Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
20Sstevel@tonic-gate 
30Sstevel@tonic-gate /*
40Sstevel@tonic-gate  * Copyright (c) 1988, 1993
50Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
80Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
90Sstevel@tonic-gate  * are met:
100Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
110Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
120Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
140Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
150Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
160Sstevel@tonic-gate  *    must display the following acknowledgement:
170Sstevel@tonic-gate  *	This product includes software developed by the University of
180Sstevel@tonic-gate  *	California, Berkeley and its contributors.
190Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
200Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
210Sstevel@tonic-gate  *    without specific prior written permission.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
240Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
250Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
260Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
270Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
280Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
290Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
300Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
310Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
320Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
330Sstevel@tonic-gate  * SUCH DAMAGE.
340Sstevel@tonic-gate  *
35*1767Swyllys  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
360Sstevel@tonic-gate  * Use is subject to license terms.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #ifndef lint
400Sstevel@tonic-gate static char sccsid[] = "@(#)network.c	8.1 (Berkeley) 6/6/93";
410Sstevel@tonic-gate #endif /* not lint */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/socket.h>
450Sstevel@tonic-gate #include <sys/time.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <errno.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <arpa/telnet.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include "ring.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "defines.h"
540Sstevel@tonic-gate #include "externs.h"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate Ring	netoring;
570Sstevel@tonic-gate Ring	netiring;
58*1767Swyllys 
59*1767Swyllys /*
60*1767Swyllys  * Use large buffers to handle larger tickets when doing
61*1767Swyllys  * Kerberos authentication.  Microsoft generates large
62*1767Swyllys  * tickets that exceed the buffer size which causes authentication
63*1767Swyllys  * to fail.
64*1767Swyllys  */
65*1767Swyllys static unsigned char netobuf[10*BUFSIZ];
66*1767Swyllys static unsigned char netibuf[10*BUFSIZ];
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Initialize internal network data structures.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate void
init_network()730Sstevel@tonic-gate init_network()
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	if (ring_init(&netoring, netobuf, sizeof (netobuf)) != 1) {
760Sstevel@tonic-gate 		exit(1);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 	if (ring_init(&netiring, netibuf, sizeof (netibuf)) != 1) {
790Sstevel@tonic-gate 		exit(1);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	NetTrace = stdout;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * Check to see if any out-of-band data exists on a socket (for
870Sstevel@tonic-gate  * Telnet "synch" processing).
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int
stilloob()910Sstevel@tonic-gate stilloob()
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	static struct timeval timeout = { 0 };
940Sstevel@tonic-gate 	fd_set	excepts;
950Sstevel@tonic-gate 	int value;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	do {
980Sstevel@tonic-gate 		FD_ZERO(&excepts);
990Sstevel@tonic-gate 		FD_SET(net, &excepts);
1000Sstevel@tonic-gate 		value = select(net+1, NULL, NULL, &excepts, &timeout);
1010Sstevel@tonic-gate 	} while ((value == -1) && (errno == EINTR));
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (value < 0) {
1040Sstevel@tonic-gate 		perror("select");
1050Sstevel@tonic-gate 		(void) quit();
1060Sstevel@tonic-gate 		/* NOTREACHED */
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	if (FD_ISSET(net, &excepts)) {
1090Sstevel@tonic-gate 		return (1);
1100Sstevel@tonic-gate 	} else {
1110Sstevel@tonic-gate 		return (0);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  *  setneturg()
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  *	Sets "neturg" to the current location.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate void
setneturg()1230Sstevel@tonic-gate setneturg()
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	ring_mark(&netoring);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  *  netflush
1310Sstevel@tonic-gate  *		Send as much data as possible to the network,
1320Sstevel@tonic-gate  *	handling requests for urgent data.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  *		The return value indicates whether we did any
1350Sstevel@tonic-gate  *	useful work.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
netflush()1400Sstevel@tonic-gate netflush()
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	register int n, n1;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (encrypt_output)
1450Sstevel@tonic-gate 		ring_encrypt(&netoring, encrypt_output);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
1480Sstevel@tonic-gate 		if (!ring_at_mark(&netoring)) {
1490Sstevel@tonic-gate 			/* normal write */
1500Sstevel@tonic-gate 			n = send(net, netoring.consume, n, 0);
1510Sstevel@tonic-gate 		} else {
1520Sstevel@tonic-gate 			/*
1530Sstevel@tonic-gate 			 * In 4.2(and 4.3) systems, there is some question about
1540Sstevel@tonic-gate 			 * what byte in a sendOOB operation is the "OOB" data.
1550Sstevel@tonic-gate 			 * To make ourselves compatible, we only send ONE byte
1560Sstevel@tonic-gate 			 * out of band, the one WE THINK should be OOB (though
1570Sstevel@tonic-gate 			 * we really have more the TCP philosophy of urgent data
1580Sstevel@tonic-gate 			 * rather than the Unix philosophy of OOB data).
1590Sstevel@tonic-gate 			 */
1600Sstevel@tonic-gate 			n = send(net, netoring.consume, 1, MSG_OOB);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	if (n < 0) {
1640Sstevel@tonic-gate 		if (errno != ENOBUFS && errno != EWOULDBLOCK) {
1650Sstevel@tonic-gate 			setcommandmode();
1660Sstevel@tonic-gate 			perror(hostname);
1670Sstevel@tonic-gate 			ring_clear_mark(&netoring);
1680Sstevel@tonic-gate 			longjmp(peerdied, -1);
1690Sstevel@tonic-gate 			/*NOTREACHED*/
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 		n = 0;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	if (netdata && n) {
1740Sstevel@tonic-gate 		Dump('>', netoring.consume, n);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 	if (n) {
1770Sstevel@tonic-gate 		ring_consumed(&netoring, n);
1780Sstevel@tonic-gate 		/*
1790Sstevel@tonic-gate 		 * If we sent all, and more to send, then recurse to pick
1800Sstevel@tonic-gate 		 * up the other half.
1810Sstevel@tonic-gate 		 */
1820Sstevel@tonic-gate 		if ((n1 == n) && ring_full_consecutive(&netoring)) {
1830Sstevel@tonic-gate 			(void) netflush();
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 		return (1);
1860Sstevel@tonic-gate 	} else {
1870Sstevel@tonic-gate 		return (0);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate }
190