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