3 * $Id: chan.c,v 1.4 1999/08/31 17:42:49 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.4 1999/08/31 17:42:49 mdw
33 * Use `sel_force' to avoid a `select' call between reads and writes.
35 * Revision 1.3 1999/07/27 18:30:53 mdw
36 * Various minor portability fixes.
38 * Revision 1.2 1999/07/26 23:27:52 mdw
39 * Minor modifications for new design.
41 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
46 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
60 #include <mLib/alloc.h>
61 #include <mLib/conn.h>
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @writechan@ --- *
71 * Arguments: @int fd@ = file descriptor to write to
72 * @unsigned mode@ = what the descriptor is ready for
73 * @void *vp@ = pointer to channel block
77 * Use: Writes to a channel.
80 static void writechan(int fd, unsigned mode, void *vp)
84 unsigned base = c->base;
85 unsigned len = c->len;
87 /* --- Write data from my buffer --- */
91 /* --- Do the write --- */
93 if (base + len <= CHAN_BUFSZ)
94 w = write(fd, c->buf + base, len);
97 iov[0].iov_base = c->buf + base;
98 iov[0].iov_len = CHAN_BUFSZ - base;
99 iov[1].iov_base = c->buf;
100 iov[1].iov_len = len - iov[0].iov_len;
101 w = writev(fd, iov, 2);
104 /* --- Sift through the results --- */
107 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
113 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
120 /* --- Close the output end if necessary --- */
122 if (c->len == 0 && (c->f & CHANF_CLOSE))
126 /* --- Force a close if an error occurred --- */
133 /* --- @readchan@ --- *
135 * Arguments: @int fd@ = file descriptor to read from
136 * @unsigned mode@ = what the descriptor is ready for
137 * @void *vp@ = pointer to channel block
141 * Use: Reads from a channel.
144 static void readchan(int fd, unsigned mode, void *vp)
148 unsigned base = (c->base + c->len) & (CHAN_BUFSZ - 1);
149 unsigned len = CHAN_BUFSZ - c->len;
151 /* --- Do the read --- */
153 if (base + len <= CHAN_BUFSZ)
154 r = read(fd, c->buf + base, len);
157 iov[0].iov_base = c->buf + base;
158 iov[0].iov_len = CHAN_BUFSZ - base;
159 iov[1].iov_base = c->buf;
160 iov[1].iov_len = len - iov[0].iov_len;
161 r = readv(fd, iov, 2);
164 /* --- Sift through the results --- */
167 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
173 else if (c->len == 0 && (c->f & CHANF_READY)) {
178 if (c->len == CHAN_BUFSZ)
182 /* --- Close the read end of the channel --- */
186 if (!c->len && (c->f & CHANF_READY)) {
193 /* --- @chan_close@ --- *
195 * Arguments: @chan *c@ = pointer to channel
199 * Use: Closes down a channel prematurely.
202 void chan_close(chan *c)
204 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
206 if ((c->f & CHANF_READY) && c->len != 0)
210 /* --- @chan_dest@ --- *
212 * Arguments: @chan *c@ = pointer to channel
213 * @int fd@ = destination file descriptor for channel
217 * Use: Sets the channel's destination so it knows where to put
221 void chan_dest(chan *c, int fd)
223 if (c->f & CHANF_READY)
225 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
226 if (c->len || (c->f & CHANF_CLOSE)) {
233 /* --- @chan_open@ --- *
235 * Arguments: @chan *c@ = pointer to channel to open
236 * @int from, to@ = source and destination file descriptors
237 * @void (*func)(void *p)@ = function to call on closure
238 * @void *p@ = argument to pass to function
242 * Use: Opens a channel. Data is copied from the source to the
243 * destination. The @to@ argument may be @-1@ if the file
244 * descriptor isn't known yet.
247 void chan_open(chan *c, int from, int to,
248 void (*func)(void */*p*/), void *p)
257 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
264 /*----- That's all, folks -------------------------------------------------*/