Fix bug in identification timout handling.
[fwd] / chan.c
CommitLineData
e82f7154 1/* -*-c-*-
2 *
08cb0dd8 3 * $Id: chan.c,v 1.6 2003/10/31 13:56:14 mdw Exp $
e82f7154 4 *
5 * Channel management
6 *
e5398e09 7 * (c) 1999 Straylight/Edgeware
e82f7154 8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: chan.c,v $
08cb0dd8 32 * Revision 1.6 2003/10/31 13:56:14 mdw
33 * Fix data corruption in channel\!
34 *
046a152e 35 * Revision 1.5 2000/07/19 17:55:43 mdw
36 * (writechan): Pointless tweak: when the buffer is empty, reset the start
37 * pointer to the beginning. This saves doing slightly trickier
38 * @writev(2)@ calls when loading is light.
39 *
3eb9f39e 40 * Revision 1.4 1999/08/31 17:42:49 mdw
41 * Use `sel_force' to avoid a `select' call between reads and writes.
42 *
e0ce9d38 43 * Revision 1.3 1999/07/27 18:30:53 mdw
44 * Various minor portability fixes.
45 *
e5398e09 46 * Revision 1.2 1999/07/26 23:27:52 mdw
47 * Minor modifications for new design.
48 *
49 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
50 * Initial revision.
e82f7154 51 *
52 */
53
54/*----- Header files ------------------------------------------------------*/
55
56#include "config.h"
57
58#include <errno.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include <sys/types.h>
64#include <sys/time.h>
65#include <unistd.h>
66#include <sys/uio.h>
67
e82f7154 68#include <mLib/alloc.h>
69#include <mLib/conn.h>
70#include <mLib/sel.h>
71
72#include "chan.h"
73#include "fw.h"
74
08cb0dd8 75#ifdef CHAN_DEBUG
76# define D(x) x
77#else
78# define D(x)
79#endif
80
e82f7154 81/*----- Main code ---------------------------------------------------------*/
82
83/* --- @writechan@ --- *
84 *
85 * Arguments: @int fd@ = file descriptor to write to
86 * @unsigned mode@ = what the descriptor is ready for
87 * @void *vp@ = pointer to channel block
88 *
89 * Returns: ---
90 *
91 * Use: Writes to a channel.
92 */
93
94static void writechan(int fd, unsigned mode, void *vp)
95{
96 chan *c = vp;
97 int w;
98 unsigned base = c->base;
99 unsigned len = c->len;
100
101 /* --- Write data from my buffer --- */
102
103 if (len) {
104
105 /* --- Do the write --- */
106
08cb0dd8 107 D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); )
108 if (base + len <= CHAN_BUFSZ) {
109 D( printf("%u:%u", base, len); )
e82f7154 110 w = write(fd, c->buf + base, len);
08cb0dd8 111 } else {
e82f7154 112 struct iovec iov[2];
113 iov[0].iov_base = c->buf + base;
114 iov[0].iov_len = CHAN_BUFSZ - base;
115 iov[1].iov_base = c->buf;
116 iov[1].iov_len = len - iov[0].iov_len;
08cb0dd8 117 D( printf("%u:%u, %u:%u",
118 base, CHAN_BUFSZ - base,
119 0, len + base - CHAN_BUFSZ); )
e82f7154 120 w = writev(fd, iov, 2);
121 }
08cb0dd8 122 D( printf("; returned %d\n", w); )
e82f7154 123
124 /* --- Sift through the results --- */
125
126 if (w < 0) {
127 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
128 return;
129 goto close;
130 }
131 else if (w == 0)
132 goto close;
133 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
134 sel_addfile(&c->r);
135 c->len -= w;
08cb0dd8 136 c->base += w;
e82f7154 137 }
138 if (c->len == 0)
139 sel_rmfile(&c->w);
140
141 /* --- Close the output end if necessary --- */
142
046a152e 143 if (c->len == 0) {
144 c->base = 0;
145 if (c->f & CHANF_CLOSE)
146 c->func(c->p);
147 }
e82f7154 148 return;
149
150 /* --- Force a close if an error occurred --- */
151
152close:
153 chan_close(c);
154 c->func(c->p);
155}
156
157/* --- @readchan@ --- *
158 *
159 * Arguments: @int fd@ = file descriptor to read from
160 * @unsigned mode@ = what the descriptor is ready for
161 * @void *vp@ = pointer to channel block
162 *
163 * Returns: ---
164 *
165 * Use: Reads from a channel.
166 */
167
168static void readchan(int fd, unsigned mode, void *vp)
169{
170 chan *c = vp;
171 int r;
08cb0dd8 172 unsigned base = c->base;
173 unsigned len = c->len;
e82f7154 174
175 /* --- Do the read --- */
176
08cb0dd8 177 D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); )
178 if (base == 0) {
179 D( printf("%u:%u", len, CHAN_BUFSZ - len); )
180 r = read(fd, c->buf + len, CHAN_BUFSZ - len);
181 } else if (base + len >= CHAN_BUFSZ) {
182 D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); )
183 r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len);
184 } else {
e82f7154 185 struct iovec iov[2];
08cb0dd8 186 iov[0].iov_base = c->buf + base + len;
187 iov[0].iov_len = CHAN_BUFSZ - base - len;
e82f7154 188 iov[1].iov_base = c->buf;
08cb0dd8 189 iov[1].iov_len = base;
190 D( printf("%u:%u, %u:%u",
191 base + len, CHAN_BUFSZ - base - len,
192 0, base); )
e82f7154 193 r = readv(fd, iov, 2);
194 }
08cb0dd8 195 D( printf("; returned %d\n", r); )
e82f7154 196
197 /* --- Sift through the results --- */
198
199 if (r < 0) {
200 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
201 return;
202 goto close;
203 }
204 else if (r == 0)
205 goto close;
3eb9f39e 206 else if (c->len == 0 && (c->f & CHANF_READY)) {
e82f7154 207 sel_addfile(&c->w);
3eb9f39e 208 sel_force(&c->w);
209 }
e82f7154 210 c->len += r;
211 if (c->len == CHAN_BUFSZ)
212 sel_rmfile(&c->r);
213 return;
214
215 /* --- Close the read end of the channel --- */
216
217close:
218 c->f |= CHANF_CLOSE;
3eb9f39e 219 if (!c->len && (c->f & CHANF_READY)) {
e82f7154 220 sel_addfile(&c->w);
3eb9f39e 221 sel_force(&c->w);
222 }
e82f7154 223 sel_rmfile(&c->r);
224}
225
226/* --- @chan_close@ --- *
227 *
228 * Arguments: @chan *c@ = pointer to channel
229 *
230 * Returns: ---
231 *
232 * Use: Closes down a channel prematurely.
233 */
234
235void chan_close(chan *c)
236{
237 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
238 sel_rmfile(&c->r);
239 if ((c->f & CHANF_READY) && c->len != 0)
240 sel_rmfile(&c->w);
241}
242
243/* --- @chan_dest@ --- *
244 *
245 * Arguments: @chan *c@ = pointer to channel
246 * @int fd@ = destination file descriptor for channel
247 *
248 * Returns: ---
249 *
250 * Use: Sets the channel's destination so it knows where to put
251 * data.
252 */
253
254void chan_dest(chan *c, int fd)
255{
256 if (c->f & CHANF_READY)
257 return;
258 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
3eb9f39e 259 if (c->len || (c->f & CHANF_CLOSE)) {
e82f7154 260 sel_addfile(&c->w);
3eb9f39e 261 sel_force(&c->w);
262 }
e5398e09 263 c->f |= CHANF_READY;
e82f7154 264}
265
266/* --- @chan_open@ --- *
267 *
268 * Arguments: @chan *c@ = pointer to channel to open
269 * @int from, to@ = source and destination file descriptors
270 * @void (*func)(void *p)@ = function to call on closure
271 * @void *p@ = argument to pass to function
272 *
273 * Returns: ---
274 *
275 * Use: Opens a channel. Data is copied from the source to the
276 * destination. The @to@ argument may be @-1@ if the file
277 * descriptor isn't known yet.
278 */
279
280void chan_open(chan *c, int from, int to,
281 void (*func)(void */*p*/), void *p)
282{
283 c->func = func;
284 c->p = p;
285
286 c->base = 0;
287 c->len = 0;
288 c->f = 0;
289
290 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
291 sel_addfile(&c->r);
292
293 if (to != -1)
294 chan_dest(c, to);
295}
296
297/*----- That's all, folks -------------------------------------------------*/