3 * $Id: endpt.c,v 1.3 2000/08/01 17:58:25 mdw Exp $
5 * Generic endpoint abstraction
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 2000/08/01 17:58:25 mdw
33 * Include missing <mLib/alloc.h> header.
35 * Revision 1.2 1999/10/22 22:46:17 mdw
36 * When a non-file endpoint is attached to a file, keep the file endpoint
37 * open until the nonfile is done. This stops socket sources from
38 * resetting their connection limits too early.
40 * Revision 1.1 1999/07/26 23:33:01 mdw
41 * Infrastructure for the new design.
45 /*----- Header files ------------------------------------------------------*/
54 #include <sys/types.h>
58 #include <mLib/alloc.h>
63 /*----- Data structures ---------------------------------------------------*/
65 /* --- Pairs of channels --- *
67 * Channels are always allocated and freed in pairs. It makes sense to keep
68 * the pair together. (It also wastes less memory.)
71 typedef struct chanpair {
72 struct chanpair *next;
76 /* --- The `private data structure' --- *
78 * This is called a @tango@ because it takes two (endpoints).
81 typedef struct tango {
82 struct tango *next, *prev; /* A big list of all tangos */
83 endpt *a, *b; /* The two endpoints */
84 unsigned s; /* State of the connection */
85 chanpair *c; /* The pair of channels */
91 /*----- Static variables --------------------------------------------------*/
93 static chanpair *chans = 0; /* List of spare channel pairs */
94 static tango *tangos = 0; /* List of tangos */
96 /*----- Main code ---------------------------------------------------------*/
98 /* --- @doneab@, @doneba@ --- *
100 * Arguments: @void *p@ = pointer to a tango block
104 * Use: Handles completion of a channel.
107 static void doneab(void *p)
111 t->b->ops->wclose(t->b);
116 static void doneba(void *p)
120 t->a->ops->wclose(t->a);
125 /* --- @endpt_kill@ --- *
127 * Arguments: @endpt *a@ = an endpoint
131 * Use: Kills an endpoint. If the endpoint is joined to another, the
132 * other endpoint is also killed, as is the connection between
133 * them (and that's the tricky bit).
136 void endpt_kill(endpt *a)
141 /* --- If the endpont is unconnected, just close it --- */
151 /* --- See whether to close channels --- *
153 * I'll only have opened channels if both things are files. Also, the
154 * channel from @b@ to @a@ will only be open if @b@ is not pending.
157 if (a->f & b->f & EPF_FILE) {
159 chan_close(&t->c->ab);
160 if (!(b->f & EPF_PENDING) && (t->s & EPS_BA))
161 chan_close(&t->c->ba);
166 /* --- Now just throw the endpoints and tango block away --- */
171 t->next->prev = t->prev;
173 t->prev->next = t->next;
179 /* --- @endpt_killall@ --- *
185 * Use: Destroys all current endpoint connections. Used when
189 void endpt_killall(void)
200 /* --- @endpt_join@ --- *
202 * Arguments: @endpt *a@ = pointer to first endpoint
203 * @endpt *b@ = pointer to second endpoint
207 * Use: Joins two endpoints together. It's OK to join endpoints
208 * which are already joined; in fact, the the right thing to do
209 * when your endpoint decides that it's not pending any more is
210 * to join it to its partner again.
213 void endpt_join(endpt *a, endpt *b)
217 /* --- If there is no tango yet, create one --- */
226 t->s = EPS_AB | EPS_BA;
233 /* --- If both endpoints are unfinished, leave them for a while --- */
235 if (a->f & b->f & EPF_PENDING)
238 /* --- At least one endpoint is ready --- *
240 * If both endpoints are files then I can speculatively create the
241 * channels, which will let data start flowing a bit. Otherwise, I'll just
242 * have to wait some more.
245 if ((a->f | b->f) & EPF_PENDING) {
247 /* --- Only be interested if both things are files --- */
249 if (!t->c && (a->f & b->f & EPF_FILE)) {
251 /* --- Allocate a pair of channels --- */
254 t->c = xmalloc(sizeof(*t->c));
260 /* --- Make @a@ the endpoint which is ready --- */
262 if (a->f & EPF_PENDING) {
267 /* --- Open one channel to read from @a@ --- */
269 chan_open(&t->c->ab, a->in->fd, -1, doneab, t);
274 /* --- Both endpoints are now ready --- *
276 * There are four cases to consider here.
281 * Neither endpoint is a file. I need to make a pair of pipes and tell
282 * both endpoints to attach to them. I can then close the pipes and
286 if (!((a->f | b->f) & EPF_FILE)) {
288 reffd *rab, *wab, *rba, *wba;
290 /* --- Create the pipes --- */
292 if (pipe(pab)) goto tidy_nofile_0;
293 if (pipe(pba)) goto tidy_nofile_1;
295 /* --- Attach reference counters --- */
297 rab = reffd_init(pab[0]); wab = reffd_init(pab[1]);
298 rba = reffd_init(pba[0]); wba = reffd_init(pba[1]);
299 a->ops->attach(a, rba, wab);
300 b->ops->attach(b, rab, wba);
301 REFFD_DEC(rab); REFFD_DEC(wab);
302 REFFD_DEC(rba); REFFD_DEC(wba);
305 /* --- Tidy up after a mess --- */
308 close(pab[0]); close(pab[1]);
313 t->next->prev = t->prev;
315 t->prev->next = t->next;
324 * One endpoint is a file, the other isn't. I just attach the other
325 * endpoint to the file and stand well back.
328 if ((a->f ^ b->f) & EPF_FILE) {
330 /* --- Let @a@ be the endpoint with the file --- */
332 if (b->f & EPF_FILE) {
337 /* --- Attach the non-file endpoint to the file and run away --- *
339 * Leave it as the non-file's responsibility to close the other endpoint
340 * when it's ready. It should also close itself at that time.
343 b->ops->attach(b, a->in, a->out);
346 t->next->prev = t->prev;
348 t->prev->next = t->next;
357 * Both endpoints are files and I have a partially created channel pair. I
358 * need to create the other channel and add a destination to the first one.
359 * In this case, @t->b@ is the endpoint which has just finished setting
365 chan_open(&t->c->ba, b->in->fd, a->out->fd, doneba, t);
366 chan_dest(&t->c->ab, b->out->fd);
372 * Both endpoints are files and I don't have a partially created channel
373 * pair. I need to allocate a channel pair and create both channels.
377 t->c = xmalloc(sizeof(*t->c));
382 chan_open(&t->c->ab, a->in->fd, b->out->fd, doneab, t);
383 chan_open(&t->c->ba, b->in->fd, a->out->fd, doneba, t);
387 /*----- That's all, folks -------------------------------------------------*/