| 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 `fw' port forwarder. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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. |
| 25 | */ |
| 26 | |
| 27 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include "config.h" |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <unistd.h> |
| 39 | #include <sys/uio.h> |
| 40 | |
| 41 | #include <mLib/alloc.h> |
| 42 | #include <mLib/conn.h> |
| 43 | #include <mLib/sel.h> |
| 44 | |
| 45 | #include "chan.h" |
| 46 | #include "fw.h" |
| 47 | |
| 48 | #ifdef CHAN_DEBUG |
| 49 | # define D(x) x |
| 50 | #else |
| 51 | # define D(x) |
| 52 | #endif |
| 53 | |
| 54 | /*----- Main code ---------------------------------------------------------*/ |
| 55 | |
| 56 | /* --- @writechan@ --- * |
| 57 | * |
| 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 |
| 61 | * |
| 62 | * Returns: --- |
| 63 | * |
| 64 | * Use: Writes to a channel. |
| 65 | */ |
| 66 | |
| 67 | static void writechan(int fd, unsigned mode, void *vp) |
| 68 | { |
| 69 | chan *c = vp; |
| 70 | int w; |
| 71 | unsigned base = c->base; |
| 72 | unsigned len = c->len; |
| 73 | |
| 74 | /* --- Write data from my buffer --- */ |
| 75 | |
| 76 | if (len) { |
| 77 | |
| 78 | /* --- Do the write --- */ |
| 79 | |
| 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); |
| 84 | } else { |
| 85 | struct iovec iov[2]; |
| 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); |
| 94 | } |
| 95 | D( printf("; returned %d\n", w); ) |
| 96 | |
| 97 | /* --- Sift through the results --- */ |
| 98 | |
| 99 | if (w < 0) { |
| 100 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
| 101 | return; |
| 102 | goto close; |
| 103 | } |
| 104 | else if (w == 0) |
| 105 | goto close; |
| 106 | else if (c->len == CHAN_BUFSZ && !(c->f & CHANF_CLOSE)) |
| 107 | sel_addfile(&c->r); |
| 108 | c->len -= w; |
| 109 | c->base += w; |
| 110 | } |
| 111 | if (c->len == 0) |
| 112 | sel_rmfile(&c->w); |
| 113 | |
| 114 | /* --- Close the output end if necessary --- */ |
| 115 | |
| 116 | if (c->len == 0) { |
| 117 | c->base = 0; |
| 118 | if (c->f & CHANF_CLOSE) |
| 119 | c->func(c->p); |
| 120 | } |
| 121 | return; |
| 122 | |
| 123 | /* --- Force a close if an error occurred --- */ |
| 124 | |
| 125 | close: |
| 126 | chan_close(c); |
| 127 | c->func(c->p); |
| 128 | } |
| 129 | |
| 130 | /* --- @readchan@ --- * |
| 131 | * |
| 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 |
| 135 | * |
| 136 | * Returns: --- |
| 137 | * |
| 138 | * Use: Reads from a channel. |
| 139 | */ |
| 140 | |
| 141 | static void readchan(int fd, unsigned mode, void *vp) |
| 142 | { |
| 143 | chan *c = vp; |
| 144 | int r; |
| 145 | unsigned base = c->base; |
| 146 | unsigned len = c->len; |
| 147 | |
| 148 | /* --- Do the read --- */ |
| 149 | |
| 150 | D( printf("readchan %d: base = %u, len = %u; ", fd, base, len); ) |
| 151 | if (base == 0) { |
| 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); |
| 157 | } else { |
| 158 | struct iovec iov[2]; |
| 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, |
| 165 | 0, base); ) |
| 166 | r = readv(fd, iov, 2); |
| 167 | } |
| 168 | D( printf("; returned %d\n", r); ) |
| 169 | |
| 170 | /* --- Sift through the results --- */ |
| 171 | |
| 172 | if (r < 0) { |
| 173 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
| 174 | return; |
| 175 | goto close; |
| 176 | } |
| 177 | else if (r == 0) |
| 178 | goto close; |
| 179 | else if (c->len == 0 && (c->f & CHANF_READY)) { |
| 180 | sel_addfile(&c->w); |
| 181 | sel_force(&c->w); |
| 182 | } |
| 183 | c->len += r; |
| 184 | if (c->len == CHAN_BUFSZ) |
| 185 | sel_rmfile(&c->r); |
| 186 | return; |
| 187 | |
| 188 | /* --- Close the read end of the channel --- */ |
| 189 | |
| 190 | close: |
| 191 | c->f |= CHANF_CLOSE; |
| 192 | if (!c->len && (c->f & CHANF_READY)) { |
| 193 | sel_addfile(&c->w); |
| 194 | sel_force(&c->w); |
| 195 | } |
| 196 | sel_rmfile(&c->r); |
| 197 | } |
| 198 | |
| 199 | /* --- @chan_close@ --- * |
| 200 | * |
| 201 | * Arguments: @chan *c@ = pointer to channel |
| 202 | * |
| 203 | * Returns: --- |
| 204 | * |
| 205 | * Use: Closes down a channel prematurely. |
| 206 | */ |
| 207 | |
| 208 | void chan_close(chan *c) |
| 209 | { |
| 210 | if (!(c->f & CHANF_CLOSE) && c->len != CHAN_BUFSZ) |
| 211 | sel_rmfile(&c->r); |
| 212 | if ((c->f & CHANF_READY) && c->len != 0) |
| 213 | sel_rmfile(&c->w); |
| 214 | } |
| 215 | |
| 216 | /* --- @chan_dest@ --- * |
| 217 | * |
| 218 | * Arguments: @chan *c@ = pointer to channel |
| 219 | * @int fd@ = destination file descriptor for channel |
| 220 | * |
| 221 | * Returns: --- |
| 222 | * |
| 223 | * Use: Sets the channel's destination so it knows where to put |
| 224 | * data. |
| 225 | */ |
| 226 | |
| 227 | void chan_dest(chan *c, int fd) |
| 228 | { |
| 229 | if (c->f & CHANF_READY) |
| 230 | return; |
| 231 | sel_initfile(sel, &c->w, fd, SEL_WRITE, writechan, c); |
| 232 | if (c->len || (c->f & CHANF_CLOSE)) { |
| 233 | sel_addfile(&c->w); |
| 234 | sel_force(&c->w); |
| 235 | } |
| 236 | c->f |= CHANF_READY; |
| 237 | } |
| 238 | |
| 239 | /* --- @chan_open@ --- * |
| 240 | * |
| 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 |
| 245 | * |
| 246 | * Returns: --- |
| 247 | * |
| 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. |
| 251 | */ |
| 252 | |
| 253 | void chan_open(chan *c, int from, int to, |
| 254 | void (*func)(void */*p*/), void *p) |
| 255 | { |
| 256 | c->func = func; |
| 257 | c->p = p; |
| 258 | |
| 259 | c->base = 0; |
| 260 | c->len = 0; |
| 261 | c->f = 0; |
| 262 | |
| 263 | sel_initfile(sel, &c->r, from, SEL_READ, readchan, c); |
| 264 | sel_addfile(&c->r); |
| 265 | |
| 266 | if (to != -1) |
| 267 | chan_dest(c, to); |
| 268 | } |
| 269 | |
| 270 | /*----- That's all, folks -------------------------------------------------*/ |