3 * $Id: chan.c,v 1.6 2003/10/31 13:56:14 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.6 2003/10/31 13:56:14 mdw
33 * Fix data corruption in channel\!
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.
40 * Revision 1.4 1999/08/31 17:42:49 mdw
41 * Use `sel_force' to avoid a `select' call between reads and writes.
43 * Revision 1.3 1999/07/27 18:30:53 mdw
44 * Various minor portability fixes.
46 * Revision 1.2 1999/07/26 23:27:52 mdw
47 * Minor modifications for new design.
49 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
54 /*----- Header files ------------------------------------------------------*/
63 #include <sys/types.h>
68 #include <mLib/alloc.h>
69 #include <mLib/conn.h>
81 /*----- Main code ---------------------------------------------------------*/
83 /* --- @writechan@ --- *
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
91 * Use: Writes to a channel.
94 static void writechan(int fd, unsigned mode, void *vp)
98 unsigned base = c->base;
99 unsigned len = c->len;
101 /* --- Write data from my buffer --- */
105 /* --- Do the write --- */
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); )
110 w = write(fd, c->buf + base, len);
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;
117 D( printf("%u:%u, %u:%u",
118 base, CHAN_BUFSZ - base,
119 0, len + base - CHAN_BUFSZ); )
120 w = writev(fd, iov, 2);
122 D( printf("; returned %d\n", w); )
124 /* --- Sift through the results --- */
127 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
133 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
141 /* --- Close the output end if necessary --- */
145 if (c->f & CHANF_CLOSE)
150 /* --- Force a close if an error occurred --- */
157 /* --- @readchan@ --- *
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
165 * Use: Reads from a channel.
168 static void readchan(int fd, unsigned mode, void *vp)
172 unsigned base = c->base;
173 unsigned len = c->len;
175 /* --- Do the read --- */
177 D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); )
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);
186 iov[0].iov_base = c->buf + base + len;
187 iov[0].iov_len = CHAN_BUFSZ - base - len;
188 iov[1].iov_base = c->buf;
189 iov[1].iov_len = base;
190 D( printf("%u:%u, %u:%u",
191 base + len, CHAN_BUFSZ - base - len,
193 r = readv(fd, iov, 2);
195 D( printf("; returned %d\n", r); )
197 /* --- Sift through the results --- */
200 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
206 else if (c->len == 0 && (c->f & CHANF_READY)) {
211 if (c->len == CHAN_BUFSZ)
215 /* --- Close the read end of the channel --- */
219 if (!c->len && (c->f & CHANF_READY)) {
226 /* --- @chan_close@ --- *
228 * Arguments: @chan *c@ = pointer to channel
232 * Use: Closes down a channel prematurely.
235 void chan_close(chan *c)
237 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
239 if ((c->f & CHANF_READY) && c->len != 0)
243 /* --- @chan_dest@ --- *
245 * Arguments: @chan *c@ = pointer to channel
246 * @int fd@ = destination file descriptor for channel
250 * Use: Sets the channel's destination so it knows where to put
254 void chan_dest(chan *c, int fd)
256 if (c->f & CHANF_READY)
258 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
259 if (c->len || (c->f & CHANF_CLOSE)) {
266 /* --- @chan_open@ --- *
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
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.
280 void chan_open(chan *c, int from, int to,
281 void (*func)(void */*p*/), void *p)
290 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
297 /*----- That's all, folks -------------------------------------------------*/