3 * $Id: chan.c,v 1.7 2004/04/08 01:36:25 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 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
43 #include <mLib/alloc.h>
44 #include <mLib/conn.h>
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @writechan@ --- *
60 * Arguments: @int fd@ = file descriptor to write to
61 * @unsigned mode@ = what the descriptor is ready for
62 * @void *vp@ = pointer to channel block
66 * Use: Writes to a channel.
69 static void writechan(int fd, unsigned mode, void *vp)
73 unsigned base = c->base;
74 unsigned len = c->len;
76 /* --- Write data from my buffer --- */
80 /* --- Do the write --- */
82 D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); )
83 if (base + len <= CHAN_BUFSZ) {
84 D( printf("%u:%u", base, len); )
85 w = write(fd, c->buf + base, len);
88 iov[0].iov_base = c->buf + base;
89 iov[0].iov_len = CHAN_BUFSZ - base;
90 iov[1].iov_base = c->buf;
91 iov[1].iov_len = len - iov[0].iov_len;
92 D( printf("%u:%u, %u:%u",
93 base, CHAN_BUFSZ - base,
94 0, len + base - CHAN_BUFSZ); )
95 w = writev(fd, iov, 2);
97 D( printf("; returned %d\n", w); )
99 /* --- Sift through the results --- */
102 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
108 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
116 /* --- Close the output end if necessary --- */
120 if (c->f & CHANF_CLOSE)
125 /* --- Force a close if an error occurred --- */
132 /* --- @readchan@ --- *
134 * Arguments: @int fd@ = file descriptor to read from
135 * @unsigned mode@ = what the descriptor is ready for
136 * @void *vp@ = pointer to channel block
140 * Use: Reads from a channel.
143 static void readchan(int fd, unsigned mode, void *vp)
147 unsigned base = c->base;
148 unsigned len = c->len;
150 /* --- Do the read --- */
152 D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); )
154 D( printf("%u:%u", len, CHAN_BUFSZ - len); )
155 r = read(fd, c->buf + len, CHAN_BUFSZ - len);
156 } else if (base + len >= CHAN_BUFSZ) {
157 D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); )
158 r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len);
161 iov[0].iov_base = c->buf + base + len;
162 iov[0].iov_len = CHAN_BUFSZ - base - len;
163 iov[1].iov_base = c->buf;
164 iov[1].iov_len = base;
165 D( printf("%u:%u, %u:%u",
166 base + len, CHAN_BUFSZ - base - len,
168 r = readv(fd, iov, 2);
170 D( printf("; returned %d\n", r); )
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 -------------------------------------------------*/