Drop main() prototype. Syncs with NetBSD-8
[minix.git] / usr.bin / telnet / network.c
blobd8fe3e0bdd75747c22d03b80962336a0da729e9c
1 /* $NetBSD: network.c,v 1.17 2004/03/20 23:26:05 heas Exp $ */
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)network.c 8.2 (Berkeley) 12/15/93";
36 #else
37 __RCSID("$NetBSD: network.c,v 1.17 2004/03/20 23:26:05 heas Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/time.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <poll.h>
49 #include <arpa/telnet.h>
51 #include "ring.h"
52 #include "defines.h"
53 #include "externs.h"
55 Ring netoring, netiring;
56 unsigned char netobuf[2*BUFSIZ], netibuf[BUFSIZ];
59 * Initialize internal network data structures.
62 void
63 init_network(void)
65 if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
66 exit(1);
68 if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
69 exit(1);
71 NetTrace = stdout;
76 * Check to see if any out-of-band data exists on a socket (for
77 * Telnet "synch" processing).
80 int
81 stilloob(void)
83 struct pollfd set[1];
84 int value;
86 set[0].fd = net;
87 set[0].events = POLLPRI;
88 do {
89 value = poll(set, 1, 0);
90 } while ((value == -1) && (errno == EINTR));
92 if (value < 0) {
93 perror("poll");
94 (void) quit(0, NULL);
95 /* NOTREACHED */
97 if (set[0].revents & POLLPRI) {
98 return 1;
99 } else {
100 return 0;
106 * setneturg()
108 * Sets "neturg" to the current location.
111 void
112 setneturg(void)
114 ring_mark(&netoring);
119 * netflush
120 * Send as much data as possible to the network,
121 * handling requests for urgent data.
123 * The return value indicates whether we did any
124 * useful work.
129 netflush(void)
131 int n, n1;
133 #ifdef ENCRYPTION
134 if (encrypt_output)
135 ring_encrypt(&netoring, encrypt_output);
136 #endif /* ENCRYPTION */
137 if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
138 if (!ring_at_mark(&netoring)) {
139 n = send(net, (char *)netoring.consume, n, 0); /* normal write */
140 } else {
142 * In 4.2 (and 4.3) systems, there is some question about
143 * what byte in a sendOOB operation is the "OOB" data.
144 * To make ourselves compatible, we only send ONE byte
145 * out of band, the one WE THINK should be OOB (though
146 * we really have more the TCP philosophy of urgent data
147 * rather than the Unix philosophy of OOB data).
149 n = send(net, (char *)netoring.consume, 1, MSG_OOB);/* URGENT data */
152 if (n < 0) {
153 if (errno != ENOBUFS && errno != EWOULDBLOCK) {
154 setcommandmode();
155 perror(hostname);
156 (void)NetClose(net);
157 ring_clear_mark(&netoring);
158 ExitString("Connection closed by foreign host.\n", 1);
159 /*NOTREACHED*/
161 n = 0;
163 if (netdata && n) {
164 Dump('>', netoring.consume, n);
166 if (n) {
167 ring_consumed(&netoring, n);
169 * If we sent all, and more to send, then recurse to pick
170 * up the other half.
172 if ((n1 == n) && ring_full_consecutive(&netoring)) {
173 (void) netflush();
175 return 1;
176 } else {
177 return 0;