3 * Generic endpoint abstraction
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.
29 /*----- Data structures ---------------------------------------------------*/
31 /* --- Pairs of channels --- *
33 * Channels are always allocated and freed in pairs. It makes sense to keep
34 * the pair together. (It also wastes less memory.)
37 typedef struct chanpair {
38 struct chanpair *next;
42 /* --- The `private data structure' --- *
44 * This is called a @tango@ because it takes two (endpoints).
47 typedef struct tango {
48 struct tango *next, *prev; /* A big list of all tangos */
49 endpt *a, *b; /* The two endpoints */
50 char *desc; /* Description of the connection */
51 unsigned s; /* State of the connection */
52 chanpair *c; /* The pair of channels */
58 /*----- Static variables --------------------------------------------------*/
60 static chanpair *chans = 0; /* List of spare channel pairs */
61 static tango *tangos = 0; /* List of tangos */
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- @doneab@, @doneba@ --- *
67 * Arguments: @void *p@ = pointer to a tango block
71 * Use: Handles completion of a channel.
74 static void doneab(void *p)
78 t->b->ops->wclose(t->b);
83 static void doneba(void *p)
87 t->a->ops->wclose(t->a);
92 /* --- @endpt_kill@ --- *
94 * Arguments: @endpt *a@ = an endpoint
98 * Use: Kills an endpoint. If the endpoint is joined to another, the
99 * other endpoint is also killed, as is the connection between
100 * them (and that's the tricky bit).
103 void endpt_kill(endpt *a)
108 /* --- If the endpont is unconnected, just close it --- */
118 /* --- See whether to close channels --- *
120 * I'll only have opened channels if both things are files. Also, the
121 * channel from @b@ to @a@ will only be open if @b@ is not pending.
124 if (a->f & b->f & EPF_FILE) {
126 fw_log(NOW, "[%s] error: %s", t->desc, strerror(t->c->ab.err));
127 else if (t->c->ba.err)
128 fw_log(NOW, "[%s] error: %s", t->desc, strerror(t->c->ba.err));
130 chan_close(&t->c->ab);
131 if (!(b->f & EPF_PENDING) && (t->s & EPS_BA))
132 chan_close(&t->c->ba);
137 /* --- Now just throw the endpoints and tango block away --- */
142 t->next->prev = t->prev;
144 t->prev->next = t->next;
152 /* --- @endpt_killall@ --- *
158 * Use: Destroys all current endpoint connections. Used when
162 void endpt_killall(void)
173 /* --- @endpt_join@ --- *
175 * Arguments: @endpt *a@ = pointer to first endpoint
176 * @endpt *b@ = pointer to second endpoint
177 * @const char *desc@ = description of connection
181 * Use: Joins two endpoints together. It's OK to join endpoints
182 * which are already joined; in fact, the the right thing to do
183 * when your endpoint decides that it's not pending any more is
184 * to join it to its partner again.
186 * If the endpoints are already connected then the description
187 * string is ignored. The endpoint manager takes a copy of the
188 * string, so you don't need to keep it around.
191 void endpt_join(endpt *a, endpt *b, const char *desc)
195 /* --- If there is no tango yet, create one --- */
204 t->s = EPS_AB | EPS_BA;
206 t->desc = xstrdup(desc);
212 /* --- If both endpoints are unfinished, leave them for a while --- */
214 if (a->f & b->f & EPF_PENDING)
217 /* --- At least one endpoint is ready --- *
219 * If both endpoints are files then I can speculatively create the
220 * channels, which will let data start flowing a bit. Otherwise, I'll just
221 * have to wait some more.
224 if ((a->f | b->f) & EPF_PENDING) {
226 /* --- Only be interested if both things are files --- */
228 if (!t->c && (a->f & b->f & EPF_FILE)) {
230 /* --- Allocate a pair of channels --- */
233 t->c = xmalloc(sizeof(*t->c));
239 /* --- Make @a@ the endpoint which is ready --- */
241 if (a->f & EPF_PENDING) {
246 /* --- Open one channel to read from @a@ --- */
248 chan_open(&t->c->ab, a->in->fd, -1, doneab, t);
253 /* --- Both endpoints are now ready --- *
255 * There are four cases to consider here.
260 * Neither endpoint is a file. I need to make a pair of pipes and tell
261 * both endpoints to attach to them. I can then close the pipes and
265 if (!((a->f | b->f) & EPF_FILE)) {
267 reffd *rab, *wab, *rba, *wba;
269 /* --- Create the pipes --- */
271 if (pipe(pab)) goto tidy_nofile_0;
272 if (pipe(pba)) goto tidy_nofile_1;
274 /* --- Attach reference counters --- */
276 rab = reffd_init(pab[0]); wab = reffd_init(pab[1]);
277 rba = reffd_init(pba[0]); wba = reffd_init(pba[1]);
278 a->ops->attach(a, rba, wab);
279 b->ops->attach(b, rab, wba);
280 REFFD_DEC(rab); REFFD_DEC(wab);
281 REFFD_DEC(rba); REFFD_DEC(wba);
284 /* --- Tidy up after a mess --- */
287 close(pab[0]); close(pab[1]);
292 t->next->prev = t->prev;
294 t->prev->next = t->next;
303 * One endpoint is a file, the other isn't. I just attach the other
304 * endpoint to the file and stand well back.
307 if ((a->f ^ b->f) & EPF_FILE) {
309 /* --- Let @a@ be the endpoint with the file --- */
311 if (b->f & EPF_FILE) {
316 /* --- Attach the non-file endpoint to the file and run away --- *
318 * Leave it as the non-file's responsibility to close the other endpoint
319 * when it's ready. It should also close itself at that time.
322 b->ops->attach(b, a->in, a->out);
325 t->next->prev = t->prev;
327 t->prev->next = t->next;
336 * Both endpoints are files and I have a partially created channel pair. I
337 * need to create the other channel and add a destination to the first one.
338 * In this case, @t->b@ is the endpoint which has just finished setting
344 chan_open(&t->c->ba, b->in->fd, a->out->fd, doneba, t);
345 chan_dest(&t->c->ab, b->out->fd);
351 * Both endpoints are files and I don't have a partially created channel
352 * pair. I need to allocate a channel pair and create both channels.
356 t->c = xmalloc(sizeof(*t->c));
361 chan_open(&t->c->ab, a->in->fd, b->out->fd, doneab, t);
362 chan_open(&t->c->ba, b->in->fd, a->out->fd, doneba, t);
366 /*----- That's all, folks -------------------------------------------------*/