5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fwd' port forwarder.
12 * `fwd' 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 * `fwd' 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 `fwd'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @writechan@ --- *
39 * Arguments: @int fd@ = file descriptor to write to
40 * @unsigned mode@ = what the descriptor is ready for
41 * @void *vp@ = pointer to channel block
45 * Use: Writes to a channel.
48 static void writechan(int fd, unsigned mode, void *vp)
52 unsigned base = c->base;
53 unsigned len = c->len;
55 /* --- Write data from my buffer --- */
59 /* --- Do the write --- */
61 D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); )
62 if (base + len <= CHAN_BUFSZ) {
63 D( printf("%u:%u", base, len); )
64 w = write(fd, c->buf + base, len);
67 iov[0].iov_base = c->buf + base;
68 iov[0].iov_len = CHAN_BUFSZ - base;
69 iov[1].iov_base = c->buf;
70 iov[1].iov_len = len - iov[0].iov_len;
71 D( printf("%u:%u, %u:%u",
72 base, CHAN_BUFSZ - base,
73 0, len + base - CHAN_BUFSZ); )
74 w = writev(fd, iov, 2);
76 D( printf("; returned %d\n", w); )
78 /* --- Sift through the results --- */
81 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
87 else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE))
95 /* --- Close the output end if necessary --- */
99 if (c->f & CHANF_CLOSE)
104 /* --- Force a close if an error occurred --- */
111 /* --- @readchan@ --- *
113 * Arguments: @int fd@ = file descriptor to read from
114 * @unsigned mode@ = what the descriptor is ready for
115 * @void *vp@ = pointer to channel block
119 * Use: Reads from a channel.
122 static void readchan(int fd, unsigned mode, void *vp)
126 unsigned base = c->base;
127 unsigned len = c->len;
129 /* --- Do the read --- */
131 D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); )
133 D( printf("%u:%u", len, CHAN_BUFSZ - len); )
134 r = read(fd, c->buf + len, CHAN_BUFSZ - len);
135 } else if (base + len >= CHAN_BUFSZ) {
136 D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); )
137 r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len);
140 iov[0].iov_base = c->buf + base + len;
141 iov[0].iov_len = CHAN_BUFSZ - base - len;
142 iov[1].iov_base = c->buf;
143 iov[1].iov_len = base;
144 D( printf("%u:%u, %u:%u",
145 base + len, CHAN_BUFSZ - base - len,
147 r = readv(fd, iov, 2);
149 D( printf("; returned %d\n", r); )
151 /* --- Sift through the results --- */
154 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
160 else if (c->len == 0 && (c->f & CHANF_READY)) {
165 if (c->len == CHAN_BUFSZ)
169 /* --- Close the read end of the channel --- */
173 if (!c->len && (c->f & CHANF_READY)) {
180 /* --- @chan_close@ --- *
182 * Arguments: @chan *c@ = pointer to channel
186 * Use: Closes down a channel prematurely.
189 void chan_close(chan *c)
191 if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ)
193 if ((c->f & CHANF_READY) && c->len != 0)
197 /* --- @chan_dest@ --- *
199 * Arguments: @chan *c@ = pointer to channel
200 * @int fd@ = destination file descriptor for channel
204 * Use: Sets the channel's destination so it knows where to put
208 void chan_dest(chan *c, int fd)
210 if (c->f & CHANF_READY)
212 sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c);
213 if (c->len || (c->f & CHANF_CLOSE)) {
220 /* --- @chan_open@ --- *
222 * Arguments: @chan *c@ = pointer to channel to open
223 * @int from, to@ = source and destination file descriptors
224 * @void (*func)(void *p)@ = function to call on closure
225 * @void *p@ = argument to pass to function
229 * Use: Opens a channel. Data is copied from the source to the
230 * destination. The @to@ argument may be @-1@ if the file
231 * descriptor isn't known yet.
234 void chan_open(chan *c, int from, int to,
235 void (*func)(void */*p*/), void *p)
244 sel_initfile(sel, &c->r, from, SEL_READ, readchan, c);
251 /*----- That's all, folks -------------------------------------------------*/