5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
36 #include <sys/types.h>
41 #include <mLib/alloc.h>
42 #include <mLib/conn.h>
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @writechan@ --- *
58 * Arguments: @int fd@ = file descriptor to write to
59 * @unsigned mode@ = what the descriptor is ready for
60 * @void *vp@ = pointer to channel block
64 * Use: Writes to a channel.
67 static void writechan(int fd, unsigned mode, void *vp)
71 unsigned base = c->base;
72 unsigned len = c->len;
74 /* --- Write data from my buffer --- */
78 /* --- Do the write --- */
80 D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); )
81 if (base + len <= CHAN_BUFSZ) {
82 D( printf("%u:%u", base, len); )
83 w = write(fd, c->buf + base, len);
86 iov[0].iov_base = c->buf + base;
87 iov[0].iov_len = CHAN_BUFSZ - base;
88 iov[1].iov_base = c->buf;
89 iov[1].iov_len = len - iov[0].iov_len;
90 D( printf("%u:%u, %u:%u",
91 base, CHAN_BUFSZ - base,
92 0, len + base - CHAN_BUFSZ); )
93 w = writev(fd, iov, 2);
95 D( printf("; returned %d\n", w); )
97 /* --- Sift through the results --- */
100 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
106 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
114 /* --- Close the output end if necessary --- */
118 if (c->f & CHANF_CLOSE)
123 /* --- Force a close if an error occurred --- */
130 /* --- @readchan@ --- *
132 * Arguments: @int fd@ = file descriptor to read from
133 * @unsigned mode@ = what the descriptor is ready for
134 * @void *vp@ = pointer to channel block
138 * Use: Reads from a channel.
141 static void readchan(int fd, unsigned mode, void *vp)
145 unsigned base = c->base;
146 unsigned len = c->len;
148 /* --- Do the read --- */
150 D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); )
152 D( printf("%u:%u", len, CHAN_BUFSZ - len); )
153 r = read(fd, c->buf + len, CHAN_BUFSZ - len);
154 } else if (base + len >= CHAN_BUFSZ) {
155 D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); )
156 r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len);
159 iov[0].iov_base = c->buf + base + len;
160 iov[0].iov_len = CHAN_BUFSZ - base - len;
161 iov[1].iov_base = c->buf;
162 iov[1].iov_len = base;
163 D( printf("%u:%u, %u:%u",
164 base + len, CHAN_BUFSZ - base - len,
166 r = readv(fd, iov, 2);
168 D( printf("; returned %d\n", r); )
170 /* --- Sift through the results --- */
173 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
179 else if (c->len == 0 && (c->f & CHANF_READY)) {
184 if (c->len == CHAN_BUFSZ)
188 /* --- Close the read end of the channel --- */
192 if (!c->len && (c->f & CHANF_READY)) {
199 /* --- @chan_close@ --- *
201 * Arguments: @chan *c@ = pointer to channel
205 * Use: Closes down a channel prematurely.
208 void chan_close(chan *c)
210 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
212 if ((c->f & CHANF_READY) && c->len != 0)
216 /* --- @chan_dest@ --- *
218 * Arguments: @chan *c@ = pointer to channel
219 * @int fd@ = destination file descriptor for channel
223 * Use: Sets the channel's destination so it knows where to put
227 void chan_dest(chan *c, int fd)
229 if (c->f & CHANF_READY)
231 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
232 if (c->len || (c->f & CHANF_CLOSE)) {
239 /* --- @chan_open@ --- *
241 * Arguments: @chan *c@ = pointer to channel to open
242 * @int from, to@ = source and destination file descriptors
243 * @void (*func)(void *p)@ = function to call on closure
244 * @void *p@ = argument to pass to function
248 * Use: Opens a channel. Data is copied from the source to the
249 * destination. The @to@ argument may be @-1@ if the file
250 * descriptor isn't known yet.
253 void chan_open(chan *c, int from, int to,
254 void (*func)(void */*p*/), void *p)
263 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
270 /*----- That's all, folks -------------------------------------------------*/