xref: /openbsd/usr.bin/telnet/network.c (revision f2dfb0a4)
1 /*	$OpenBSD: network.c,v 1.6 1998/05/15 03:16:39 art Exp $	*/
2 /*	$NetBSD: network.c,v 1.5 1996/02/28 21:04:06 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include "telnet_locl.h"
38 #include <err.h>
39 
40 Ring		netoring, netiring;
41 unsigned char	netobuf[2*BUFSIZ], netibuf[BUFSIZ];
42 
43 /*
44  * Initialize internal network data structures.
45  */
46 
47     void
48 init_network()
49 {
50     if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
51 	exit(1);
52     }
53     if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
54 	exit(1);
55     }
56     NetTrace = stdout;
57 }
58 
59 
60 /*
61  * Check to see if any out-of-band data exists on a socket (for
62  * Telnet "synch" processing).
63  */
64 
65     int
66 stilloob()
67 {
68     static struct timeval timeout = { 0 };
69     fd_set *fdsp;
70     int fdsn;
71     int value;
72 
73     fdsn = howmany(net+1, NFDBITS) * sizeof(fd_mask);
74     if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
75 	err(1, "malloc");
76 
77     do {
78 	memset(fdsp, 0, fdsn);
79 	FD_SET(net, fdsp);
80 	value = select(net+1, (fd_set *)0, (fd_set *)0, fdsp, &timeout);
81     } while ((value == -1) && (errno == EINTR));
82 
83     if (value < 0) {
84 	perror("select");
85 	free(fdsp);
86 	(void) quit();
87 	/* NOTREACHED */
88     }
89     if (FD_ISSET(net, fdsp)) {
90 	free(fdsp);
91 	return 1;
92     } else {
93    	free(fdsp);
94 	return 0;
95     }
96 }
97 
98 
99 /*
100  *  setneturg()
101  *
102  *	Sets "neturg" to the current location.
103  */
104 
105     void
106 setneturg()
107 {
108     ring_mark(&netoring);
109 }
110 
111 
112 /*
113  *  netflush
114  *		Send as much data as possible to the network,
115  *	handling requests for urgent data.
116  *
117  *		The return value indicates whether we did any
118  *	useful work.
119  */
120 
121 
122     int
123 netflush()
124 {
125     register int n, n1;
126 
127 #if    defined(ENCRYPTION)
128     if (encrypt_output)
129 	ring_encrypt(&netoring, encrypt_output);
130 #endif
131     if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
132 	if (!ring_at_mark(&netoring)) {
133 	    n = send(net, (char *)netoring.consume, n, 0); /* normal write */
134 	} else {
135 	    /*
136 	     * In 4.2 (and 4.3) systems, there is some question about
137 	     * what byte in a sendOOB operation is the "OOB" data.
138 	     * To make ourselves compatible, we only send ONE byte
139 	     * out of band, the one WE THINK should be OOB (though
140 	     * we really have more the TCP philosophy of urgent data
141 	     * rather than the Unix philosophy of OOB data).
142 	     */
143 	    n = send(net, (char *)netoring.consume, 1, MSG_OOB);/* URGENT data */
144 	}
145     }
146     if (n < 0) {
147 	if (errno != ENOBUFS && errno != EWOULDBLOCK) {
148 	    setcommandmode();
149 	    perror(hostname);
150 	    (void)NetClose(net);
151 	    ring_clear_mark(&netoring);
152 	    longjmp(peerdied, -1);
153 	    /*NOTREACHED*/
154 	}
155 	n = 0;
156     }
157     if (netdata && n) {
158 	Dump('>', netoring.consume, n);
159     }
160     if (n) {
161 	ring_consumed(&netoring, n);
162 	/*
163 	 * If we sent all, and more to send, then recurse to pick
164 	 * up the other half.
165 	 */
166 	if ((n1 == n) && ring_full_consecutive(&netoring)) {
167 	    (void) netflush();
168 	}
169 	return 1;
170     } else {
171 	return 0;
172     }
173 }
174