include ultrix -> vaxuba
[unix-history] / usr / src / usr.bin / telnet / network.c
CommitLineData
897ce52e
KB
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
11 */
12
13#ifndef lint
80a47e22 14static char sccsid[] = "@(#)network.c 1.12 (Berkeley) %G%";
897ce52e
KB
15#endif /* not lint */
16
8bca9c52
GM
17#include <sys/types.h>
18#include <sys/socket.h>
19#include <sys/time.h>
20
21#include <errno.h>
22
23#include <arpa/telnet.h>
24
115a5494
GM
25#include "ring.h"
26
8bca9c52
GM
27#include "defines.h"
28#include "externs.h"
ad1c581e 29#include "fdset.h"
8bca9c52 30
b307f09e
GM
31Ring netoring, netiring;
32char netobuf[2*BUFSIZ], netibuf[BUFSIZ];
8bca9c52
GM
33
34/*
35 * Initialize internal network data structures.
36 */
37
38init_network()
39{
80a47e22
GM
40 if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
41 exit(1);
42 }
43 if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
44 exit(1);
45 }
8bca9c52
GM
46 NetTrace = stdout;
47}
48
49
50/*
51 * Check to see if any out-of-band data exists on a socket (for
52 * Telnet "synch" processing).
53 */
54
55int
ddfe1126 56stilloob()
8bca9c52
GM
57{
58 static struct timeval timeout = { 0 };
59 fd_set excepts;
60 int value;
61
62 do {
63 FD_ZERO(&excepts);
ddfe1126
GM
64 FD_SET(net, &excepts);
65 value = select(net+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
8bca9c52
GM
66 } while ((value == -1) && (errno == EINTR));
67
68 if (value < 0) {
69 perror("select");
80a47e22 70 (void) quit();
8bca9c52 71 }
ddfe1126 72 if (FD_ISSET(net, &excepts)) {
8bca9c52
GM
73 return 1;
74 } else {
75 return 0;
76 }
77}
78
79
d732be39
GM
80/*
81 * setneturg()
82 *
83 * Sets "neturg" to the current location.
84 */
85
86void
87setneturg()
88{
115a5494 89 ring_mark(&netoring);
d732be39
GM
90}
91
92
8bca9c52
GM
93/*
94 * netflush
95 * Send as much data as possible to the network,
96 * handling requests for urgent data.
97 *
98 * The return value indicates whether we did any
99 * useful work.
100 */
101
102
103int
104netflush()
105{
218b1a4c 106 register int n, n1;
8bca9c52 107
ad1c581e 108 if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
115a5494 109 if (!ring_at_mark(&netoring)) {
8b6750f5 110 n = send(net, netoring.consume, n, 0); /* normal write */
8bca9c52 111 } else {
8bca9c52
GM
112 /*
113 * In 4.2 (and 4.3) systems, there is some question about
114 * what byte in a sendOOB operation is the "OOB" data.
115 * To make ourselves compatible, we only send ONE byte
116 * out of band, the one WE THINK should be OOB (though
117 * we really have more the TCP philosophy of urgent data
118 * rather than the Unix philosophy of OOB data).
119 */
8b6750f5 120 n = send(net, netoring.consume, 1, MSG_OOB);/* URGENT data */
8bca9c52
GM
121 }
122 }
123 if (n < 0) {
124 if (errno != ENOBUFS && errno != EWOULDBLOCK) {
125 setcommandmode();
126 perror(hostname);
127 NetClose(net);
115a5494 128 ring_clear_mark(&netoring);
8bca9c52
GM
129 longjmp(peerdied, -1);
130 /*NOTREACHED*/
131 }
132 n = 0;
133 }
134 if (netdata && n) {
8b6750f5 135 Dump('>', netoring.consume, n);
8bca9c52 136 }
ad1c581e 137 if (n) {
ad1c581e 138 ring_consumed(&netoring, n);
218b1a4c
GM
139 /*
140 * If we sent all, and more to send, then recurse to pick
141 * up the other half.
142 */
143 if ((n1 == n) && ring_full_consecutive(&netoring)) {
144 (void) netflush();
145 }
146 return 1;
147 } else {
148 return 0;
ad1c581e 149 }
8bca9c52 150}