7d9a6c79ff1b6194d3a8916e12b26e6f8f366b27
[become] / src / class.h
1 /* -*-c-*-
2 *
3 * $Id: class.h,v 1.4 1998/01/12 16:45:53 mdw Exp $
4 *
5 * Handling classes of things nicely
6 *
7 * (c) 1998 EBI
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of `become'
13 *
14 * `Become' 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 * `Become' 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 `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: class.h,v $
32 * Revision 1.4 1998/01/12 16:45:53 mdw
33 * Fix copyright date.
34 *
35 * Revision 1.3 1997/09/17 10:14:56 mdw
36 * Complete rewrite to support class trees. Makes the behaviour of the set
37 * operators much more logical.
38 *
39 * Revision 1.2 1997/08/04 10:24:21 mdw
40 * Sources placed under CVS control.
41 *
42 * Revision 1.1 1997/07/21 13:47:52 mdw
43 * Initial revision
44 *
45 */
46
47 #ifndef CLASS_H
48 #define CLASS_H
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 /*----- Required headers --------------------------------------------------*/
55
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60
61 #ifndef SYM_H
62 # include "sym.h"
63 #endif
64
65 /*----- Data structures ---------------------------------------------------*/
66
67 /* --- Class types --- */
68
69 enum {
70
71 /* --- Data types (user visible) --- *
72 *
73 * These bits encode what the user can think of as `types'. They get used
74 * for typechecking and things.
75 */
76
77 clType_user = 0x01, /* Class of users */
78 clType_command = 0x02, /* Class of command strings */
79 clType_host = 0x04, /* Class of host names */
80 clType_all = 0x0F, /* All of the above (maintain me) */
81 clType_mask = 0x0F, /* Mask for type flags */
82
83 /* --- Node types --- *
84 *
85 * A class actually looks suspiciously like a tree once things start
86 * getting complicated. These bits encode the type of node we've got here.
87 */
88
89 clNode_any = 0x10, /* Magic type for the `all' class */
90 clNode_immed = 0x20, /* Immediate data item */
91 clNode_hash = 0x30, /* Hashtable of values */
92 clNode_union = 0x40, /* Union of two classes */
93 clNode_binop = 0x50, /* Binary operations start here */
94 clNode_diff = 0x50, /* Difference of two classes */
95 clNode_isect = 0x60, /* Intersection of two classes */
96 clNode_mask = 0xF0, /* Mask for picking these out */
97
98 /* --- Other useful flags --- */
99
100 clFlag_friendly = 0x100 /* Item can be hashed with others */
101 };
102
103 /* --- Class block --- */
104
105 typedef struct class_node {
106 unsigned type; /* Class and node type, and flags */
107 unsigned ref; /* Reference count */
108 union {
109 uid_t u; /* Immediate user id number */
110 char *s; /* Immediate string value */
111 sym_table t; /* Hash table for this class */
112 struct {
113 struct class_node *l, *r; /* Left and right operands */
114 } c; /* Class operands for binops */
115 } v; /* Value of this class node */
116 } class_node;
117
118 /*----- Global variables --------------------------------------------------*/
119
120 extern class_node *class_all; /* The match-everything class */
121 extern class_node *class_none; /* The match-nothing class */
122
123 /*----- Functions provided ------------------------------------------------*/
124
125 /* --- @class_fromString@ --- *
126 *
127 * Arguments: @unsigned type@ = a type field
128 * @const char *s@ = pointer to string to copy
129 *
130 * Returns: A pointer to a class node containing that string, typed to
131 * be the right thing.
132 *
133 * Use: Given a string, wrap a class node around it. The node has
134 * one reference (the one you get returned). The string is
135 * copied, so you can get rid of your original one if you like.
136 */
137
138 extern class_node *class_fromString(unsigned /*type*/, const char */*s*/);
139
140 /* --- @class_fromUser@ --- *
141 *
142 * Arguments: @unsigned type@ = a type field
143 * @uid_t u@ = a user-id number
144 *
145 * Returns: A pointer to a class node containing the uid, typed to be
146 * the thing you asked for. Hopefully this will be
147 * @clType_user@.
148 *
149 * Use: Given a uid, wrap a class node around it.
150 */
151
152 extern class_node *class_fromUser(unsigned /*type*/, uid_t /*u*/);
153
154 /* --- @class_inc@ --- *
155 *
156 * Arguments: @class_node *c@ = pointer to a class block
157 *
158 * Returns: ---
159 *
160 * Use: Adds a reference to the class definition.
161 */
162
163 extern void class_inc(class_node */*c*/);
164
165 /* --- @class_dec@ --- *
166 *
167 * Arguments: @class_node *c@ = pointer to a class block
168 *
169 * Returns: ---
170 *
171 * Use: Removes a reference to a class block.
172 */
173
174 extern void class_dec(class_node */*c*/);
175
176 /* --- @class_mod@ --- *
177 *
178 * Arguments: @class_node *c@ = pointer to a class node
179 *
180 * Returns: A pointer to a class node, maybe the same one, maybe not,
181 * with a reference count of 1, containing the same data.
182 *
183 * Use: Gives you a node which you can modify. Don't call this
184 * for @class_all@ or @class_none@.
185 */
186
187 extern class_node *class_mod(class_node */*c*/);
188
189 /* --- @class_union@ --- *
190 *
191 * Arguments: @class_node *l@ = left argument
192 * @class_node *r@ = right argument
193 *
194 * Returns: A class node representing the union of the two classes.
195 *
196 * Use: Performs the union operation on classes. If the types don't
197 * match, then a null pointer is returned. Both @l@ and @r@
198 * may be modified, and will be decremented before they get
199 * returned to you. If you don't want that to happen, ensure
200 * that you've claimed a reference to the original versions.
201 */
202
203 extern class_node *class_union(class_node */*l*/, class_node */*r*/);
204
205 /* --- @class_diff@ --- *
206 *
207 * Arguments: @class_node *l@ = left argument
208 * @class_node *r@ = right argument
209 *
210 * Returns: A class node representing the difference of the two classes.
211 *
212 * Use: Performs the set difference operation on classes. If the
213 * types don't match, then a null pointer is returned. Both
214 * @l@ and @r@ may be modified, and will be decremented before
215 * they get returned to you. If you don't want that to happen,
216 * ensure that you've claimed a reference to the original
217 * versions.
218 */
219
220 extern class_node *class_diff(class_node */*l*/, class_node */*r*/);
221
222 /* --- @class_isect@ --- *
223 *
224 * Arguments: @class_node *l@ = left argument
225 * @class_node *r@ = right argument
226 *
227 * Returns: A class node representing the intersection of the two
228 * classes.
229 *
230 * Use: Performs the intersecion operation on classes. If the types
231 * don't match, then a null pointer is returned. Both @l@ and
232 * @r@ may be modified, and will be decremented before they get
233 * returned to you. If you don't want that to happen, ensure
234 * that you've claimed a reference to the original versions.
235 */
236
237 extern class_node *class_isect(class_node */*l*/, class_node */*r*/);
238
239 /* --- @class_addUser@ --- *
240 *
241 * Arguments: @class_node *c@ = pointer to a class node (may be null)
242 * @uid_t u@ = user id number
243 *
244 * Returns: Pointer to the combined node.
245 *
246 * Use: Adds a user to a node, maybe hashifying it.
247 */
248
249 extern class_node *class_addUser(class_node */*c*/, uid_t /*u*/);
250
251 /* --- @class_addString@ --- *
252 *
253 * Arguments: @class_node *c@ = pointer to a class node (may be null)
254 * @const char *s@ = pointer to a string
255 *
256 * Returns: Pointer to the combined node.
257 *
258 * Use: Adds a user to a node, maybe hashifying it.
259 */
260
261 extern class_node *class_addString(class_node */*c*/, const char */*s*/);
262
263 /* --- @class_matchUser@ --- *
264 *
265 * Arguments: @class_node *c@ = pointer to root class node
266 * @uid_t u@ = user id number
267 *
268 * Returns: Nonzero if it matches, zero if it doesn't.
269 *
270 * Use: Determines whether a user is matched by a class. Assumes
271 * that the types are correct.
272 */
273
274 extern int class_matchUser(class_node */*c*/, uid_t /*u*/);
275
276 /* --- @class_matchCommand@ --- *
277 *
278 * Arguments: @class_node *c@ = pointer to root class node
279 * @const char *s@ = pointer to a string
280 *
281 * Returns: Nonzero if it matches, zero if it doesn't.
282 *
283 * Use: Determines whether a string is matched by a class. Assumes
284 * that the types are correct.
285 */
286
287 extern int class_matchCommand(class_node */*c*/, const char */*s*/);
288
289 /* --- @class_matchHost@ --- *
290 *
291 * Arguments: @class_node *c@ = pointer to root class node
292 * @struct in_addr a@ = IP address to match
293 *
294 * Returns: Nonzero if it matches, zero if it doesn't.
295 *
296 * Use: Determines whether a host matches a host class. Assumes
297 * that the types are correct. The actual mechanism is a bit
298 * odd here, but I think this is the Right Thing. At each stage
299 * I try to match %%@/all/%% of the possible names for the host.
300 * Thus host `splat' with address 1.2.3.4 would fail to match
301 * the class "1.2.*" - "splat". This seems to be what the
302 * author intuitively expects. It's just a bit weird.
303 */
304
305 extern int class_matchHost(class_node */*c*/, struct in_addr /*a*/);
306
307 /* --- @class_dump@ --- *
308 *
309 * Argumemnts: @class_node *c@ = pointer to root node
310 * @int indent@ = indent depth
311 *
312 * Returns: ---
313 *
314 * Use: Dumps a class to the trace output.
315 */
316
317 extern void class_dump(class_node */*c*/, int /*indent*/);
318
319 /*----- That's all, folks -------------------------------------------------*/
320
321 #ifdef __cplusplus
322 }
323 #endif
324
325 #endif
326