3 * $Id: chan.c,v 1.5 2000/07/19 17:55:43 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.5 2000/07/19 17:55:43 mdw
33 * (writechan): Pointless tweak: when the buffer is empty, reset the start
34 * pointer to the beginning. This saves doing slightly trickier
35 * @writev(2)@ calls when loading is light.
37 * Revision 1.4 1999/08/31 17:42:49 mdw
38 * Use `sel_force' to avoid a `select' call between reads and writes.
40 * Revision 1.3 1999/07/27 18:30:53 mdw
41 * Various minor portability fixes.
43 * Revision 1.2 1999/07/26 23:27:52 mdw
44 * Minor modifications for new design.
46 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
51 /*----- Header files ------------------------------------------------------*/
60 #include <sys/types.h>
65 #include <mLib/alloc.h>
66 #include <mLib/conn.h>
72 /*----- Main code ---------------------------------------------------------*/
74 /* --- @writechan@ --- *
76 * Arguments: @int fd@ = file descriptor to write to
77 * @unsigned mode@ = what the descriptor is ready for
78 * @void *vp@ = pointer to channel block
82 * Use: Writes to a channel.
85 static void writechan(int fd, unsigned mode, void *vp)
89 unsigned base = c->base;
90 unsigned len = c->len;
92 /* --- Write data from my buffer --- */
96 /* --- Do the write --- */
98 if (base + len <= CHAN_BUFSZ)
99 w = write(fd, c->buf + base, len);
102 iov[0].iov_base = c->buf + base;
103 iov[0].iov_len = CHAN_BUFSZ - base;
104 iov[1].iov_base = c->buf;
105 iov[1].iov_len = len - iov[0].iov_len;
106 w = writev(fd, iov, 2);
109 /* --- Sift through the results --- */
112 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
118 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
125 /* --- Close the output end if necessary --- */
129 if (c->f & CHANF_CLOSE)
134 /* --- Force a close if an error occurred --- */
141 /* --- @readchan@ --- *
143 * Arguments: @int fd@ = file descriptor to read from
144 * @unsigned mode@ = what the descriptor is ready for
145 * @void *vp@ = pointer to channel block
149 * Use: Reads from a channel.
152 static void readchan(int fd, unsigned mode, void *vp)
156 unsigned base = (c->base + c->len) & (CHAN_BUFSZ - 1);
157 unsigned len = CHAN_BUFSZ - c->len;
159 /* --- Do the read --- */
161 if (base + len <= CHAN_BUFSZ)
162 r = read(fd, c->buf + base, len);
165 iov[0].iov_base = c->buf + base;
166 iov[0].iov_len = CHAN_BUFSZ - base;
167 iov[1].iov_base = c->buf;
168 iov[1].iov_len = len - iov[0].iov_len;
169 r = readv(fd, iov, 2);
172 /* --- Sift through the results --- */
175 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
181 else if (c->len == 0 && (c->f & CHANF_READY)) {
186 if (c->len == CHAN_BUFSZ)
190 /* --- Close the read end of the channel --- */
194 if (!c->len && (c->f & CHANF_READY)) {
201 /* --- @chan_close@ --- *
203 * Arguments: @chan *c@ = pointer to channel
207 * Use: Closes down a channel prematurely.
210 void chan_close(chan *c)
212 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
214 if ((c->f & CHANF_READY) && c->len != 0)
218 /* --- @chan_dest@ --- *
220 * Arguments: @chan *c@ = pointer to channel
221 * @int fd@ = destination file descriptor for channel
225 * Use: Sets the channel's destination so it knows where to put
229 void chan_dest(chan *c, int fd)
231 if (c->f & CHANF_READY)
233 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
234 if (c->len || (c->f & CHANF_CLOSE)) {
241 /* --- @chan_open@ --- *
243 * Arguments: @chan *c@ = pointer to channel to open
244 * @int from, to@ = source and destination file descriptors
245 * @void (*func)(void *p)@ = function to call on closure
246 * @void *p@ = argument to pass to function
250 * Use: Opens a channel. Data is copied from the source to the
251 * destination. The @to@ argument may be @-1@ if the file
252 * descriptor isn't known yet.
255 void chan_open(chan *c, int from, int to,
256 void (*func)(void */*p*/), void *p)
265 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
272 /*----- That's all, folks -------------------------------------------------*/