1 /* $NetBSD: network.c,v 1.17 2004/03/20 23:26:05 heas Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)network.c 8.2 (Berkeley) 12/15/93";
37 __RCSID("$NetBSD: network.c,v 1.17 2004/03/20 23:26:05 heas Exp $");
41 #include <sys/types.h>
42 #include <sys/socket.h>
49 #include <arpa/telnet.h>
55 Ring netoring
, netiring
;
56 unsigned char netobuf
[2*BUFSIZ
], netibuf
[BUFSIZ
];
59 * Initialize internal network data structures.
65 if (ring_init(&netoring
, netobuf
, sizeof netobuf
) != 1) {
68 if (ring_init(&netiring
, netibuf
, sizeof netibuf
) != 1) {
76 * Check to see if any out-of-band data exists on a socket (for
77 * Telnet "synch" processing).
87 set
[0].events
= POLLPRI
;
89 value
= poll(set
, 1, 0);
90 } while ((value
== -1) && (errno
== EINTR
));
97 if (set
[0].revents
& POLLPRI
) {
108 * Sets "neturg" to the current location.
114 ring_mark(&netoring
);
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
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 */
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 */
153 if (errno
!= ENOBUFS
&& errno
!= EWOULDBLOCK
) {
157 ring_clear_mark(&netoring
);
158 ExitString("Connection closed by foreign host.\n", 1);
164 Dump('>', netoring
.consume
, n
);
167 ring_consumed(&netoring
, n
);
169 * If we sent all, and more to send, then recurse to pick
172 if ((n1
== n
) && ring_full_consecutive(&netoring
)) {