| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Channel management |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the `fwd' port forwarder. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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. |
| 25 | */ |
| 26 | |
| 27 | #include "fwd.h" |
| 28 | |
| 29 | #ifdef CHAN_DEBUG |
| 30 | # define D(x) x |
| 31 | #else |
| 32 | # define D(x) |
| 33 | #endif |
| 34 | |
| 35 | /*----- Main code ---------------------------------------------------------*/ |
| 36 | |
| 37 | /* --- @writechan@ --- * |
| 38 | * |
| 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 |
| 42 | * |
| 43 | * Returns: --- |
| 44 | * |
| 45 | * Use: Writes to a channel. |
| 46 | */ |
| 47 | |
| 48 | static void writechan(int fd, unsigned mode, void *vp) |
| 49 | { |
| 50 | chan *c = vp; |
| 51 | int w; |
| 52 | unsigned base = c->base; |
| 53 | unsigned len = c->len; |
| 54 | |
| 55 | /* --- Write data from my buffer --- */ |
| 56 | |
| 57 | if (len) { |
| 58 | |
| 59 | /* --- Do the write --- */ |
| 60 | |
| 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); |
| 65 | } else { |
| 66 | struct iovec iov[2]; |
| 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); |
| 75 | } |
| 76 | D( printf("; returned %d\n", w); ) |
| 77 | |
| 78 | /* --- Sift through the results --- */ |
| 79 | |
| 80 | if (w < 0) { |
| 81 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
| 82 | return; |
| 83 | c->err = errno; |
| 84 | goto close; |
| 85 | } else if (w == 0) |
| 86 | goto close; |
| 87 | else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE)) |
| 88 | sel_addfile(&c->r); |
| 89 | c->len -= w; |
| 90 | c->base += w; |
| 91 | } |
| 92 | if (c->len == 0) |
| 93 | sel_rmfile(&c->w); |
| 94 | |
| 95 | /* --- Close the output end if necessary --- */ |
| 96 | |
| 97 | if (c->len == 0) { |
| 98 | c->base = 0; |
| 99 | if (c->f & CHANF_CLOSE) |
| 100 | c->func(c->p); |
| 101 | } |
| 102 | return; |
| 103 | |
| 104 | /* --- Force a close if an error occurred --- */ |
| 105 | |
| 106 | close: |
| 107 | chan_close(c); |
| 108 | c->func(c->p); |
| 109 | } |
| 110 | |
| 111 | /* --- @readchan@ --- * |
| 112 | * |
| 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 |
| 116 | * |
| 117 | * Returns: --- |
| 118 | * |
| 119 | * Use: Reads from a channel. |
| 120 | */ |
| 121 | |
| 122 | static void readchan(int fd, unsigned mode, void *vp) |
| 123 | { |
| 124 | chan *c = vp; |
| 125 | int r; |
| 126 | unsigned base = c->base; |
| 127 | unsigned len = c->len; |
| 128 | |
| 129 | /* --- Do the read --- */ |
| 130 | |
| 131 | D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); ) |
| 132 | if (base == 0) { |
| 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); |
| 138 | } else { |
| 139 | struct iovec iov[2]; |
| 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, |
| 146 | 0, base); ) |
| 147 | r = readv(fd, iov, 2); |
| 148 | } |
| 149 | D( printf("; returned %d\n", r); ) |
| 150 | |
| 151 | /* --- Sift through the results --- */ |
| 152 | |
| 153 | if (r < 0) { |
| 154 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
| 155 | return; |
| 156 | c->err = errno; |
| 157 | goto close; |
| 158 | } else if (r == 0) |
| 159 | goto close; |
| 160 | else if (c->len == 0 && (c->f & CHANF_READY)) { |
| 161 | sel_addfile(&c->w); |
| 162 | sel_force(&c->w); |
| 163 | } |
| 164 | c->len += r; |
| 165 | if (c->len == CHAN_BUFSZ) |
| 166 | sel_rmfile(&c->r); |
| 167 | return; |
| 168 | |
| 169 | /* --- Close the read end of the channel --- */ |
| 170 | |
| 171 | close: |
| 172 | c->f |= CHANF_CLOSE; |
| 173 | if (!c->len && (c->f & CHANF_READY)) { |
| 174 | sel_addfile(&c->w); |
| 175 | sel_force(&c->w); |
| 176 | } |
| 177 | sel_rmfile(&c->r); |
| 178 | } |
| 179 | |
| 180 | /* --- @chan_close@ --- * |
| 181 | * |
| 182 | * Arguments: @chan *c@ = pointer to channel |
| 183 | * |
| 184 | * Returns: --- |
| 185 | * |
| 186 | * Use: Closes down a channel prematurely. |
| 187 | */ |
| 188 | |
| 189 | void chan_close(chan *c) |
| 190 | { |
| 191 | if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ) |
| 192 | sel_rmfile(&c->r); |
| 193 | if ((c->f & CHANF_READY) && c->len != 0) |
| 194 | sel_rmfile(&c->w); |
| 195 | } |
| 196 | |
| 197 | /* --- @chan_dest@ --- * |
| 198 | * |
| 199 | * Arguments: @chan *c@ = pointer to channel |
| 200 | * @int fd@ = destination file descriptor for channel |
| 201 | * |
| 202 | * Returns: --- |
| 203 | * |
| 204 | * Use: Sets the channel's destination so it knows where to put |
| 205 | * data. |
| 206 | */ |
| 207 | |
| 208 | void chan_dest(chan *c, int fd) |
| 209 | { |
| 210 | if (c->f & CHANF_READY) |
| 211 | return; |
| 212 | sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c); |
| 213 | if (c->len || (c->f & CHANF_CLOSE)) { |
| 214 | sel_addfile(&c->w); |
| 215 | sel_force(&c->w); |
| 216 | } |
| 217 | c->f |= CHANF_READY; |
| 218 | } |
| 219 | |
| 220 | /* --- @chan_open@ --- * |
| 221 | * |
| 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 |
| 226 | * |
| 227 | * Returns: --- |
| 228 | * |
| 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. |
| 232 | */ |
| 233 | |
| 234 | void chan_open(chan *c, int from, int to, |
| 235 | void (*func)(void */*p*/), void *p) |
| 236 | { |
| 237 | c->func = func; |
| 238 | c->p = p; |
| 239 | |
| 240 | c->base = 0; |
| 241 | c->len = 0; |
| 242 | c->f = 0; |
| 243 | c->err = 0; |
| 244 | |
| 245 | sel_initfile(sel, &c->r, from, SEL_READ, readchan, c); |
| 246 | sel_addfile(&c->r); |
| 247 | |
| 248 | if (to != -1) |
| 249 | chan_dest(c, to); |
| 250 | } |
| 251 | |
| 252 | /*----- That's all, folks -------------------------------------------------*/ |