| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: chan.c,v 1.6 2003/10/31 13:56:14 mdw Exp $ |
| 4 | * |
| 5 | * Channel management |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the `fw' port forwarder. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: chan.c,v $ |
| 32 | * Revision 1.6 2003/10/31 13:56:14 mdw |
| 33 | * Fix data corruption in channel\! |
| 34 | * |
| 35 | * Revision 1.5 2000/07/19 17:55:43 mdw |
| 36 | * (writechan): Pointless tweak: when the buffer is empty, reset the start |
| 37 | * pointer to the beginning. This saves doing slightly trickier |
| 38 | * @writev(2)@ calls when loading is light. |
| 39 | * |
| 40 | * Revision 1.4 1999/08/31 17:42:49 mdw |
| 41 | * Use `sel_force' to avoid a `select' call between reads and writes. |
| 42 | * |
| 43 | * Revision 1.3 1999/07/27 18:30:53 mdw |
| 44 | * Various minor portability fixes. |
| 45 | * |
| 46 | * Revision 1.2 1999/07/26 23:27:52 mdw |
| 47 | * Minor modifications for new design. |
| 48 | * |
| 49 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
| 50 | * Initial revision. |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | /*----- Header files ------------------------------------------------------*/ |
| 55 | |
| 56 | #include "config.h" |
| 57 | |
| 58 | #include <errno.h> |
| 59 | #include <stdio.h> |
| 60 | #include <stdlib.h> |
| 61 | #include <string.h> |
| 62 | |
| 63 | #include <sys/types.h> |
| 64 | #include <sys/time.h> |
| 65 | #include <unistd.h> |
| 66 | #include <sys/uio.h> |
| 67 | |
| 68 | #include <mLib/alloc.h> |
| 69 | #include <mLib/conn.h> |
| 70 | #include <mLib/sel.h> |
| 71 | |
| 72 | #include "chan.h" |
| 73 | #include "fw.h" |
| 74 | |
| 75 | #ifdef CHAN_DEBUG |
| 76 | # define D(x) x |
| 77 | #else |
| 78 | # define D(x) |
| 79 | #endif |
| 80 | |
| 81 | /*----- Main code ---------------------------------------------------------*/ |
| 82 | |
| 83 | /* --- @writechan@ --- * |
| 84 | * |
| 85 | * Arguments: @int fd@ = file descriptor to write to |
| 86 | * @unsigned mode@ = what the descriptor is ready for |
| 87 | * @void *vp@ = pointer to channel block |
| 88 | * |
| 89 | * Returns: --- |
| 90 | * |
| 91 | * Use: Writes to a channel. |
| 92 | */ |
| 93 | |
| 94 | static void writechan(int fd, unsigned mode, void *vp) |
| 95 | { |
| 96 | chan *c = vp; |
| 97 | int w; |
| 98 | unsigned base = c->base; |
| 99 | unsigned len = c->len; |
| 100 | |
| 101 | /* --- Write data from my buffer --- */ |
| 102 | |
| 103 | if (len) { |
| 104 | |
| 105 | /* --- Do the write --- */ |
| 106 | |
| 107 | D( printf("writechan %d: base = %u, len = %u; ", fd, base, len); ) |
| 108 | if (base + len <= CHAN_BUFSZ) { |
| 109 | D( printf("%u:%u", base, len); ) |
| 110 | w = write(fd, c->buf + base, len); |
| 111 | } else { |
| 112 | struct iovec iov[2]; |
| 113 | iov[0].iov_base = c->buf + base; |
| 114 | iov[0].iov_len = CHAN_BUFSZ - base; |
| 115 | iov[1].iov_base = c->buf; |
| 116 | iov[1].iov_len = len - iov[0].iov_len; |
| 117 | D( printf("%u:%u, %u:%u", |
| 118 | base, CHAN_BUFSZ - base, |
| 119 | 0, len + base - CHAN_BUFSZ); ) |
| 120 | w = writev(fd, iov, 2); |
| 121 | } |
| 122 | D( printf("; returned %d\n", w); ) |
| 123 | |
| 124 | /* --- Sift through the results --- */ |
| 125 | |
| 126 | if (w < 0) { |
| 127 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
| 128 | return; |
| 129 | goto close; |
| 130 | } |
| 131 | else if (w == 0) |
| 132 | goto close; |
| 133 | else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE)) |
| 134 | sel_addfile(&c->r); |
| 135 | c->len -= w; |
| 136 | c->base += w; |
| 137 | } |
| 138 | if (c->len == 0) |
| 139 | sel_rmfile(&c->w); |
| 140 | |
| 141 | /* --- Close the output end if necessary --- */ |
| 142 | |
| 143 | if (c->len == 0) { |
| 144 | c->base = 0; |
| 145 | if (c->f & CHANF_CLOSE) |
| 146 | c->func(c->p); |
| 147 | } |
| 148 | return; |
| 149 | |
| 150 | /* --- Force a close if an error occurred --- */ |
| 151 | |
| 152 | close: |
| 153 | chan_close(c); |
| 154 | c->func(c->p); |
| 155 | } |
| 156 | |
| 157 | /* --- @readchan@ --- * |
| 158 | * |
| 159 | * Arguments: @int fd@ = file descriptor to read from |
| 160 | * @unsigned mode@ = what the descriptor is ready for |
| 161 | * @void *vp@ = pointer to channel block |
| 162 | * |
| 163 | * Returns: --- |
| 164 | * |
| 165 | * Use: Reads from a channel. |
| 166 | */ |
| 167 | |
| 168 | static void readchan(int fd, unsigned mode, void *vp) |
| 169 | { |
| 170 | chan *c = vp; |
| 171 | int r; |
| 172 | unsigned base = c->base; |
| 173 | unsigned len = c->len; |
| 174 | |
| 175 | /* --- Do the read --- */ |
| 176 | |
| 177 | D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); ) |
| 178 | if (base == 0) { |
| 179 | D( printf("%u:%u", len, CHAN_BUFSZ - len); ) |
| 180 | r = read(fd, c->buf + len, CHAN_BUFSZ - len); |
| 181 | } else if (base + len >= CHAN_BUFSZ) { |
| 182 | D( printf("%u:%u", base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); ) |
| 183 | r = read(fd, c->buf + base + len - CHAN_BUFSZ, CHAN_BUFSZ - len); |
| 184 | } else { |
| 185 | struct iovec iov[2]; |
| 186 | iov[0].iov_base = c->buf + base + len; |
| 187 | iov[0].iov_len = CHAN_BUFSZ - base - len; |
| 188 | iov[1].iov_base = c->buf; |
| 189 | iov[1].iov_len = base; |
| 190 | D( printf("%u:%u, %u:%u", |
| 191 | base + len, CHAN_BUFSZ - base - len, |
| 192 | 0, base); ) |
| 193 | r = readv(fd, iov, 2); |
| 194 | } |
| 195 | D( printf("; returned %d\n", r); ) |
| 196 | |
| 197 | /* --- Sift through the results --- */ |
| 198 | |
| 199 | if (r < 0) { |
| 200 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
| 201 | return; |
| 202 | goto close; |
| 203 | } |
| 204 | else if (r == 0) |
| 205 | goto close; |
| 206 | else if (c->len == 0 && (c->f & CHANF_READY)) { |
| 207 | sel_addfile(&c->w); |
| 208 | sel_force(&c->w); |
| 209 | } |
| 210 | c->len += r; |
| 211 | if (c->len == CHAN_BUFSZ) |
| 212 | sel_rmfile(&c->r); |
| 213 | return; |
| 214 | |
| 215 | /* --- Close the read end of the channel --- */ |
| 216 | |
| 217 | close: |
| 218 | c->f |= CHANF_CLOSE; |
| 219 | if (!c->len && (c->f & CHANF_READY)) { |
| 220 | sel_addfile(&c->w); |
| 221 | sel_force(&c->w); |
| 222 | } |
| 223 | sel_rmfile(&c->r); |
| 224 | } |
| 225 | |
| 226 | /* --- @chan_close@ --- * |
| 227 | * |
| 228 | * Arguments: @chan *c@ = pointer to channel |
| 229 | * |
| 230 | * Returns: --- |
| 231 | * |
| 232 | * Use: Closes down a channel prematurely. |
| 233 | */ |
| 234 | |
| 235 | void chan_close(chan *c) |
| 236 | { |
| 237 | if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ) |
| 238 | sel_rmfile(&c->r); |
| 239 | if ((c->f & CHANF_READY) && c->len != 0) |
| 240 | sel_rmfile(&c->w); |
| 241 | } |
| 242 | |
| 243 | /* --- @chan_dest@ --- * |
| 244 | * |
| 245 | * Arguments: @chan *c@ = pointer to channel |
| 246 | * @int fd@ = destination file descriptor for channel |
| 247 | * |
| 248 | * Returns: --- |
| 249 | * |
| 250 | * Use: Sets the channel's destination so it knows where to put |
| 251 | * data. |
| 252 | */ |
| 253 | |
| 254 | void chan_dest(chan *c, int fd) |
| 255 | { |
| 256 | if (c->f & CHANF_READY) |
| 257 | return; |
| 258 | sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c); |
| 259 | if (c->len || (c->f & CHANF_CLOSE)) { |
| 260 | sel_addfile(&c->w); |
| 261 | sel_force(&c->w); |
| 262 | } |
| 263 | c->f |= CHANF_READY; |
| 264 | } |
| 265 | |
| 266 | /* --- @chan_open@ --- * |
| 267 | * |
| 268 | * Arguments: @chan *c@ = pointer to channel to open |
| 269 | * @int from, to@ = source and destination file descriptors |
| 270 | * @void (*func)(void *p)@ = function to call on closure |
| 271 | * @void *p@ = argument to pass to function |
| 272 | * |
| 273 | * Returns: --- |
| 274 | * |
| 275 | * Use: Opens a channel. Data is copied from the source to the |
| 276 | * destination. The @to@ argument may be @-1@ if the file |
| 277 | * descriptor isn't known yet. |
| 278 | */ |
| 279 | |
| 280 | void chan_open(chan *c, int from, int to, |
| 281 | void (*func)(void */*p*/), void *p) |
| 282 | { |
| 283 | c->func = func; |
| 284 | c->p = p; |
| 285 | |
| 286 | c->base = 0; |
| 287 | c->len = 0; |
| 288 | c->f = 0; |
| 289 | |
| 290 | sel_initfile(sel, &c->r, from, SEL_READ, readchan, c); |
| 291 | sel_addfile(&c->r); |
| 292 | |
| 293 | if (to != -1) |
| 294 | chan_dest(c, to); |
| 295 | } |
| 296 | |
| 297 | /*----- That's all, folks -------------------------------------------------*/ |