Annotation of src/usr.bin/telnet/network.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: network.c,v 1.3 1996/12/12 11:35:59 robin Exp $ */
1.2 niklas 2: /* $NetBSD: network.c,v 1.5 1996/02/28 21:04:06 thorpej Exp $ */
3:
1.1 deraadt 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: #ifndef lint
1.2 niklas 38: #if 0
39: static char sccsid[] = "@(#)network.c 8.2 (Berkeley) 12/15/93";
40: static char rcsid[] = "$NetBSD: network.c,v 1.5 1996/02/28 21:04:06 thorpej Exp $";
41: #else
1.4 ! deraadt 42: static char rcsid[] = "$OpenBSD: network.c,v 1.3 1996/12/12 11:35:59 robin Exp $";
1.2 niklas 43: #endif
1.1 deraadt 44: #endif /* not lint */
45:
46: #include <sys/types.h>
47: #include <sys/socket.h>
48: #include <sys/time.h>
1.3 robin 49: #include <unistd.h>
1.1 deraadt 50: #include <errno.h>
51:
52: #include <arpa/telnet.h>
53:
54: #include "ring.h"
55:
56: #include "defines.h"
57: #include "externs.h"
58: #include "fdset.h"
59:
60: Ring netoring, netiring;
61: unsigned char netobuf[2*BUFSIZ], netibuf[BUFSIZ];
62:
63: /*
64: * Initialize internal network data structures.
65: */
66:
67: void
68: init_network()
69: {
70: if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
71: exit(1);
72: }
73: if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
74: exit(1);
75: }
76: NetTrace = stdout;
77: }
78:
79:
80: /*
81: * Check to see if any out-of-band data exists on a socket (for
82: * Telnet "synch" processing).
83: */
84:
85: int
86: stilloob()
87: {
88: static struct timeval timeout = { 0 };
1.4 ! deraadt 89: fd_set *fdsp;
! 90: int fdsn;
1.1 deraadt 91: int value;
92:
1.4 ! deraadt 93: fdsn = howmany(net+1, NFDBITS) * sizeof(fd_mask);
! 94: if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
! 95: err(1, "malloc");
! 96:
1.1 deraadt 97: do {
1.4 ! deraadt 98: memset(fdsp, 0, fdsn);
! 99: FD_SET(net, fdsp);
! 100: value = select(net+1, (fd_set *)0, (fd_set *)0, fdsp, &timeout);
1.1 deraadt 101: } while ((value == -1) && (errno == EINTR));
102:
103: if (value < 0) {
104: perror("select");
1.4 ! deraadt 105: free(fdsp);
1.1 deraadt 106: (void) quit();
107: /* NOTREACHED */
108: }
1.4 ! deraadt 109: if (FD_ISSET(net, fdsp)) {
! 110: free(fdsp);
1.1 deraadt 111: return 1;
112: } else {
1.4 ! deraadt 113: free(fdsp);
1.1 deraadt 114: return 0;
115: }
116: }
117:
118:
119: /*
120: * setneturg()
121: *
122: * Sets "neturg" to the current location.
123: */
124:
125: void
126: setneturg()
127: {
128: ring_mark(&netoring);
129: }
130:
131:
132: /*
133: * netflush
134: * Send as much data as possible to the network,
135: * handling requests for urgent data.
136: *
137: * The return value indicates whether we did any
138: * useful work.
139: */
140:
141:
142: int
143: netflush()
144: {
145: register int n, n1;
146:
147: if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
148: if (!ring_at_mark(&netoring)) {
149: n = send(net, (char *)netoring.consume, n, 0); /* normal write */
150: } else {
151: /*
152: * In 4.2 (and 4.3) systems, there is some question about
153: * what byte in a sendOOB operation is the "OOB" data.
154: * To make ourselves compatible, we only send ONE byte
155: * out of band, the one WE THINK should be OOB (though
156: * we really have more the TCP philosophy of urgent data
157: * rather than the Unix philosophy of OOB data).
158: */
159: n = send(net, (char *)netoring.consume, 1, MSG_OOB);/* URGENT data */
160: }
161: }
162: if (n < 0) {
163: if (errno != ENOBUFS && errno != EWOULDBLOCK) {
164: setcommandmode();
165: perror(hostname);
166: (void)NetClose(net);
167: ring_clear_mark(&netoring);
168: longjmp(peerdied, -1);
169: /*NOTREACHED*/
170: }
171: n = 0;
172: }
173: if (netdata && n) {
174: Dump('>', netoring.consume, n);
175: }
176: if (n) {
177: ring_consumed(&netoring, n);
178: /*
179: * If we sent all, and more to send, then recurse to pick
180: * up the other half.
181: */
182: if ((n1 == n) && ring_full_consecutive(&netoring)) {
183: (void) netflush();
184: }
185: return 1;
186: } else {
187: return 0;
188: }
189: }