chiark / gitweb /
fwd: Improve `source' and `target' lifecycle management.
[fwd] / source.c
1 /* -*-c-*-
2  *
3  * Standard routines for forwarding sources
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the `fwd' port forwarder.
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #include "fwd.h"
28
29 /*----- Static variables --------------------------------------------------*/
30
31 static source *sources = 0;
32
33 /*----- Main code ---------------------------------------------------------*/
34
35 /* --- @source_add@ --- *
36  *
37  * Arguments:   @source *s@ = pointer to a source
38  *
39  * Returns:     ---
40  *
41  * Use:         Adds a source to the master list.  Only do this for passive
42  *              sources (e.g., listening sockets), not active sources (e.g.,
43  *              executable programs).
44  */
45
46 void source_add(source *s)
47 {
48   assert(s->ops->shutdown);
49   assert(!(s->f&SF_ACTIVE));
50   s->next = sources;
51   s->prev = 0;
52   if (sources)
53     sources->prev = s;
54   sources = s;
55   s->f |= SF_ACTIVE;
56   source_inc(s);
57 }
58
59 /* --- @source_remove@ --- *
60  *
61  * Arguments:   @source *s@ = pointer to a source
62  *
63  * Returns:     ---
64  *
65  * Use:         Removes a source from the master list.
66  */
67
68 void source_remove(source *s)
69 {
70   assert(s->f&SF_ACTIVE);
71   if (s->next)
72     s->next->prev = s->prev;
73   if (s->prev)
74     s->prev->next = s->next;
75   else
76     sources = s->next;
77   s->f &= ~SF_ACTIVE;
78   source_dec(s);
79 }
80
81 /* --- @source_inc@ --- *
82  *
83  * Arguments:   @source *s@ = pointer to a source
84  *
85  * Returns:     ---
86  *
87  * Use:         Increments a source's refcount.
88  */
89
90 void source_inc(source *s) { s->ref++; }
91
92 /* --- @source_dec@ --- *
93  *
94  * Arguments:   @source *s@ = pointer to a source
95  *
96  * Returns:     ---
97  *
98  * Use:         Decrements a source's refcount, destroying it if necessary.
99  */
100
101 void source_dec(source *s)
102 {
103   assert(s->ref > 0);
104   s->ref--;
105   if (!s->ref) {
106     if (s->f&SF_ACTIVE) s->ops->shutdown(s);
107     s->ops->destroy(s);
108   }
109 }
110
111 /* --- @source_killall@ --- *
112  *
113  * Arguments:   ---
114  *
115  * Returns:     ---
116  *
117  * Use:         Frees all sources.
118  */
119
120 void source_killall(void)
121 {
122   source *s = sources;
123   while (s) {
124     source *ss = s;
125     s = s->next;
126     ss->ops->shutdown(ss);
127   }
128   sources = 0;
129 }
130
131 /*----- That's all, folks -------------------------------------------------*/