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