Commit | Line | Data |
---|---|---|
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 | |
14 | static char sccsid[] = "@(#)network.c 1.11 (Berkeley) %G%"; | |
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 |
31 | Ring netoring, netiring; |
32 | char netobuf[2*BUFSIZ], netibuf[BUFSIZ]; | |
8bca9c52 GM |
33 | |
34 | /* | |
35 | * Initialize internal network data structures. | |
36 | */ | |
37 | ||
38 | init_network() | |
39 | { | |
115a5494 | 40 | ring_init(&netoring, netobuf, sizeof netobuf); |
b307f09e | 41 | ring_init(&netiring, netibuf, sizeof netibuf); |
8bca9c52 GM |
42 | NetTrace = stdout; |
43 | } | |
44 | ||
45 | ||
46 | /* | |
47 | * Check to see if any out-of-band data exists on a socket (for | |
48 | * Telnet "synch" processing). | |
49 | */ | |
50 | ||
51 | int | |
ddfe1126 | 52 | stilloob() |
8bca9c52 GM |
53 | { |
54 | static struct timeval timeout = { 0 }; | |
55 | fd_set excepts; | |
56 | int value; | |
57 | ||
58 | do { | |
59 | FD_ZERO(&excepts); | |
ddfe1126 GM |
60 | FD_SET(net, &excepts); |
61 | value = select(net+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout); | |
8bca9c52 GM |
62 | } while ((value == -1) && (errno == EINTR)); |
63 | ||
64 | if (value < 0) { | |
65 | perror("select"); | |
66 | quit(); | |
67 | } | |
ddfe1126 | 68 | if (FD_ISSET(net, &excepts)) { |
8bca9c52 GM |
69 | return 1; |
70 | } else { | |
71 | return 0; | |
72 | } | |
73 | } | |
74 | ||
75 | ||
d732be39 GM |
76 | /* |
77 | * setneturg() | |
78 | * | |
79 | * Sets "neturg" to the current location. | |
80 | */ | |
81 | ||
82 | void | |
83 | setneturg() | |
84 | { | |
115a5494 | 85 | ring_mark(&netoring); |
d732be39 GM |
86 | } |
87 | ||
88 | ||
8bca9c52 GM |
89 | /* |
90 | * netflush | |
91 | * Send as much data as possible to the network, | |
92 | * handling requests for urgent data. | |
93 | * | |
94 | * The return value indicates whether we did any | |
95 | * useful work. | |
96 | */ | |
97 | ||
98 | ||
99 | int | |
100 | netflush() | |
101 | { | |
218b1a4c | 102 | register int n, n1; |
8bca9c52 | 103 | |
ad1c581e | 104 | if ((n1 = n = ring_full_consecutive(&netoring)) > 0) { |
115a5494 | 105 | if (!ring_at_mark(&netoring)) { |
8b6750f5 | 106 | n = send(net, netoring.consume, n, 0); /* normal write */ |
8bca9c52 | 107 | } else { |
8bca9c52 GM |
108 | /* |
109 | * In 4.2 (and 4.3) systems, there is some question about | |
110 | * what byte in a sendOOB operation is the "OOB" data. | |
111 | * To make ourselves compatible, we only send ONE byte | |
112 | * out of band, the one WE THINK should be OOB (though | |
113 | * we really have more the TCP philosophy of urgent data | |
114 | * rather than the Unix philosophy of OOB data). | |
115 | */ | |
8b6750f5 | 116 | n = send(net, netoring.consume, 1, MSG_OOB);/* URGENT data */ |
8bca9c52 GM |
117 | } |
118 | } | |
119 | if (n < 0) { | |
120 | if (errno != ENOBUFS && errno != EWOULDBLOCK) { | |
121 | setcommandmode(); | |
122 | perror(hostname); | |
123 | NetClose(net); | |
115a5494 | 124 | ring_clear_mark(&netoring); |
8bca9c52 GM |
125 | longjmp(peerdied, -1); |
126 | /*NOTREACHED*/ | |
127 | } | |
128 | n = 0; | |
129 | } | |
130 | if (netdata && n) { | |
8b6750f5 | 131 | Dump('>', netoring.consume, n); |
8bca9c52 | 132 | } |
ad1c581e | 133 | if (n) { |
ad1c581e | 134 | ring_consumed(&netoring, n); |
218b1a4c GM |
135 | /* |
136 | * If we sent all, and more to send, then recurse to pick | |
137 | * up the other half. | |
138 | */ | |
139 | if ((n1 == n) && ring_full_consecutive(&netoring)) { | |
140 | (void) netflush(); | |
141 | } | |
142 | return 1; | |
143 | } else { | |
144 | return 0; | |
ad1c581e | 145 | } |
8bca9c52 | 146 | } |