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