Debian Codegraph @

telnet/network.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1988, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <stdlib.h>
51
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <time.h>
56 #include <errno.h>
57
58 #include <arpa/telnet.h>
59 #include <sys/select.h>
60
61 #include "ring.h"
62
63 #include "defines.h"
64 #include "externs.h"
65
66 Ring netoring, netiring;
67 unsigned char netobuf[2 * BUFSIZ], netibuf[BUFSIZ];
68
69 /*
70 * Initialize internal network data structures.
71 */
72
73 void
74 init_network (void)
75 {
76 if (ring_init (&netoring, netobuf, sizeof netobuf) != 1)
77 {
78 exit (EXIT_FAILURE);
79 }
80 if (ring_init (&netiring, netibuf, sizeof netibuf) != 1)
81 {
82 exit (EXIT_FAILURE);
83 }
84 NetTrace = stdout;
85 }
86
87
88 /*
89 * Check to see if any out-of-band data exists on a socket (for
90 * Telnet "synch" processing).
91 */
92
93 int
94 stilloob (void)
95 {
96 static struct timeval timeout = { 0, 0 };
97 fd_set excepts;
98 int value;
99
100 do
101 {
102 FD_ZERO (&excepts);
103 FD_SET (net, &excepts);
104 value =
105 select (net + 1, (fd_set *) 0, (fd_set *) 0, &excepts, &timeout);
106 }
107 while ((value == -1) && (errno == EINTR));
108
109 if (value < 0)
110 {
111 perror ("select");
112 (void) quit (0, NULL);
113 /* NOTREACHED */
114 }
115 if (FD_ISSET (net, &excepts))
116 {
117 return 1;
118 }
119 else
120 {
121 return 0;
122 }
123 }
124
125
126 /*
127 * setneturg()
128 *
129 * Sets "neturg" to the current location.
130 */
131
132 void
133 setneturg (void)
134 {
135 ring_mark (&netoring);
136 }
137
138
139 /*
140 * netflush
141 * Send as much data as possible to the network,
142 * handling requests for urgent data.
143 *
144 * The return value indicates whether we did any
145 * useful work.
146 */
147
148
149 int
150 netflush (void)
151 {
152 int n, n1;
153
154 #ifdef ENCRYPTION
155 if (encrypt_output)
156 ring_encrypt (&netoring, encrypt_output);
157 #endif /* ENCRYPTION */
158 if ((n1 = n = ring_full_consecutive (&netoring)) > 0)
159 {
160 if (!ring_at_mark (&netoring))
161 {
162 n = send (net, (char *) netoring.consume, n, 0); /* normal write */
163 }
164 else
165 {
166 /*
167 * In 4.2 (and 4.3) systems, there is some question about
168 * what byte in a sendOOB operation is the "OOB" data.
169 * To make ourselves compatible, we only send ONE byte
170 * out of band, the one WE THINK should be OOB (though
171 * we really have more the TCP philosophy of urgent data
172 * rather than the Unix philosophy of OOB data).
173 */
174 n = send (net, (char *) netoring.consume, 1, MSG_OOB); /* URGENT data */
175 }
176 }
177 if (n < 0)
178 {
179 if (errno != ENOBUFS && errno != EWOULDBLOCK)
180 {
181 setcommandmode ();
182 perror (hostname);
183 (void) NetClose (net);
184 ring_clear_mark (&netoring);
185 longjmp (peerdied, -1);
186 }
187 n = 0;
188 }
189 if (netdata && n)
190 {
191 Dump ('>', netoring.consume, n);
192 }
193 if (n)
194 {
195 ring_consumed (&netoring, n);
196 /*
197 * If we sent all, and more to send, then recurse to pick
198 * up the other half.
199 */
200 if ((n1 == n) && ring_full_consecutive (&netoring))
201 {
202 (void) netflush ();
203 }
204 return 1;
205 }
206 else
207 {
208 return 0;
209 }
210 }