summaryrefslogtreecommitdiff
path: root/server/class.c
blob: 2f919341fde0904389be2b7a8fe6e2d3294c99d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains functions for the Zephyr server class manager subsystem.
 *
 *	Created by:	John T. Kohl
 *
 *	$Source$
 *	$Author$
 *
 *	Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <zephyr/mit-copyright.h>
#include "zserver.h"			/* includes zephyr/zephyr.h */
#include <assert.h>

#if !defined (lint) && !defined (SABER)
static const char rcsid_class_c[] =
"$Id$";
#endif

/*
 * Class manager subsystem.
 *
 *
 * External functions are:
 *
 * Code_t triplet_register(client, subs, realm)
 *
 * Code_t triplet_deregister(client, subs, realm)
 *
 * Client *triplet_lookup(subs)
 *	Client *client;
 *	Destlist *subs;
 *
 * Acl *class_get_acl(class_name)
 *	String *class_name;
 *
 * Code_t class_restrict(class_name, acl)
 *	char *class_name;
 *	Acl *acl;
 *
 * Code_t class_setup_restricted(class_name, acl)
 *	char *class_name;
 *	Acl *acl;
 *
 * and several Destination methods.
 */

/*
 * The data structure used for the class manager is an array of hash buckets
 * each containing a pointer to a doubly linked circular list (in the style
 * of insque/remque).  Each element of this list contains a class.instance
 * name (which hashes into the bucket associated with this list) and a
 * doubly linked list of clients which are interested in this class.
 * The data pointed to by these clients is owned by other modules.  Care
 * must be taken by the caller not to a free()'d client
 * structure.
 *
 * If any hash bucket is empty, the pointer is null.
 *
 * The first element in the hash bucket is a special header unused for
 * storing classes, and is used for finding the end of the list.
 *
 * If any list of interested clients is empty, the class name is garbage
 * collected, unless the class has been registered as restricted.
 */

/* Private variables */ 
#define	EMPTY_CLASS	2000

#define ALLOC_OFFSET	8	/* Allocate 32 bytes less than a power of 2. */
#define ALLOC_INIT	8	/* Initial number of subscriptions. */

#define	HASHSIZE	1023
#define HASHVAL(c, i, r) (((c)->hash_val ^ (i)->hash_val ^ (r)->hash_val) \
			  % HASHSIZE)
#define DEST_HASHVAL(dest) HASHVAL((dest).classname, (dest).inst, (dest).recip)

static Triplet *triplet_bucket[HASHSIZE]; /* the hash table of pointers */

static Code_t remove_client(Triplet *triplet, Client *client, ZRealm *realm);
static Code_t insert_client(Triplet *triplet, Client *client, ZRealm *realm);
static Triplet *triplet_alloc(String *classname, String *inst,
			      String *recipient);
static void free_triplet(Triplet *);

/* public routines */

/*
 * Determine if two destination triplets are equal.  Note the backup
 * case-insensitive recipient check in the third term.  Recipients are
 * not downcased at subscription time (in order to preserve case for,
 * say, "zctl ret"), but traditional zephyr server behavior has not
 * been case-sensitive in the recipient string.  In most cases, a
 * failed match will fail on the classname or instance, and a successful
 * match will succeed on the (d1->recip == d2->recip) check, so this
 * shouldn't affect performance.
 */

int
ZDest_eq(Destination *d1,
	 Destination *d2)
{
    return((d1->classname == d2->classname) &&
	   (d1->inst == d2->inst) &&
	   (d1->recip == d2->recip ||
	    strcasecmp(d1->recip->string, d2->recip->string) == 0));
}


/* the client as interested in a triplet */

Code_t
triplet_register(Client *client,
		 Destination *dest,
		 ZRealm *realm)
{
    Triplet *triplet;
    unsigned long hashval;

    hashval = DEST_HASHVAL(*dest);
    for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) {
	if (ZDest_eq(&triplet->dest, dest))
	    return insert_client(triplet, client, realm);
    }

    /* Triplet not present in hash table, insert it. */
    triplet = triplet_alloc(dest->classname, dest->inst, dest->recip);
    Triplet_insert(&triplet_bucket[hashval], triplet);
    return insert_client(triplet, client, realm);
}

/* dissociate client from the class, garbage collecting if appropriate */

Code_t
triplet_deregister(Client *client,
		   Destination *dest,
		   ZRealm *realm)
{
    Triplet *triplet;
    int retval;
    unsigned long hashval;

    hashval = DEST_HASHVAL(*dest);
    for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) {
	if (ZDest_eq(&triplet->dest, dest)) {
	    retval = remove_client(triplet, client, realm);
	    if (retval != ZERR_NONE)
		return retval;
	    if (*triplet->clients == NULL && !triplet->acl) {
		Triplet_delete(triplet);
		free_triplet(triplet);
		return ZSRV_EMPTYCLASS;
	    }
	    return ZERR_NONE;
	}
    }
    return(ZSRV_BADASSOC);
}
	
/* return a linked list of what clients are interested in this triplet */

Client **
triplet_lookup(Destination *dest)
{
    Triplet *triplet;
    unsigned long hashval;

    hashval = DEST_HASHVAL(*dest);
    for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) {
	if (ZDest_eq(&triplet->dest, dest))
	    return triplet->clients;
    }
    return NULL;
}

/*
 * return the acl structure associated with class, or NULL if there is
 * no such acl struct
 */

Acl *
class_get_acl(String *class_name)
{
    Triplet *triplet;
    unsigned long hashval;

    hashval = HASHVAL(class_name, empty, empty);
    for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) {
	if (triplet->dest.classname == class_name &&
	    triplet->dest.inst == empty && triplet->dest.recip == empty)
	    return triplet->acl;
    }

    /* No acl found, not restricted. */
    return NULL;
}

/*
 * restrict class by associating it with the acl structure acl.
 * return ZERR_NONE if no error, or ZSRV_NOCLASS if there is no such
 * class, or ZSRV_CLASSRESTRICTED if it is already restricted.
 */

Code_t
class_restrict(char *class_name,
	       Acl *acl)
{
    Triplet *triplet;
    String *d;
    unsigned long hashval;

    d = make_string(class_name,1);
    hashval = HASHVAL(d, empty, empty);
    for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) {
	if (triplet->dest.classname == d && triplet->dest.inst == empty &&
	    triplet->dest.recip == empty) {
	    if (triplet->acl)
		return ZSRV_CLASSRESTRICTED;
	    triplet->acl = acl;
	    free_string(d);
	    return ZERR_NONE;
	}
    }

    free_string(d);
    return ZSRV_NOCLASS;
}

/*
 * restrict class by registering it and  associating it with the acl
 * structure acl.  return ZERR_NONE if no error, or ZSRV_CLASSXISTS
 * if the class is already registered, or ENOMEM in case of malloc failure.
 */

Code_t
class_setup_restricted(char *class_name,
		       Acl *acl)
{
    Triplet *triplet;
    String *d;
    unsigned long hashval;

    d = make_string(class_name,1);
    hashval = HASHVAL(d, empty, empty);
    for (triplet = triplet_bucket[hashval]; triplet; triplet = triplet->next) {
	if (triplet->dest.classname == d && triplet->dest.inst == empty &&
	    triplet->dest.recip == d) {
	    free_string(d);
	    return ZSRV_CLASSXISTS;
	}
    }

    /* Triplet not present in hash table, insert it. */
    triplet = triplet_alloc(d, empty, empty);
    free_string(d);
    if (!triplet)
	return ENOMEM;
    triplet->acl = acl;
    Triplet_insert(&triplet_bucket[hashval], triplet);
    return ZERR_NONE;
}

/* private routines */

/* allocate space for a class structure */

static Triplet *
triplet_alloc(String *classname,
	      String *inst,
	      String *recipient)
{
    Triplet *triplet;

    triplet = (Triplet *) malloc(sizeof(Triplet));
    if (!triplet)
	return NULL;

    triplet->dest.classname = dup_string(classname);
    triplet->dest.inst = dup_string(inst);
    triplet->dest.recip = dup_string(recipient);
    triplet->clients = NULL;
    triplet->acl = NULL;

    return triplet;
}

/* insert a client into the list associated with the class *ptr */

static Code_t
insert_client(Triplet *triplet,
	      Client *client,
	      ZRealm *realm)
{
    Client **clientp, **newclients;
    int new_size;

    if (triplet->clients) {
	/* Avoid duplication. */
	for (clientp = triplet->clients; *clientp; clientp++) {
	    if (*clientp == client || (realm && (*clientp)->realm == realm))
		return ZSRV_CLASSXISTS;
	}

	if (clientp + 1 - triplet->clients >= triplet->clients_size) {
	    new_size = triplet->clients_size * 2 + ALLOC_OFFSET;
	    newclients = (Client **) realloc(triplet->clients,
					     new_size * sizeof(Client *));
	    if (newclients == NULL)
		return ENOMEM;
	    clientp = newclients + (clientp - triplet->clients);
	    triplet->clients = newclients;
	    triplet->clients_size = new_size;
	}
    } else {
	/* Allocate an initial list of client pointers. */
	triplet->clients = (Client **) malloc(ALLOC_INIT * sizeof(Client *));
	if (triplet->clients == NULL)
	    return ENOMEM;
	triplet->clients_size = ALLOC_INIT;
	clientp = triplet->clients;
    }

    *clientp = client;
    clientp[1] = NULL;
    return ZERR_NONE;
}

/* 
 * remove the client from the list associated with class *ptr, garbage
 * collecting if appropriate
 */

static Code_t
remove_client(Triplet *triplet,
	      Client *client,
	      ZRealm *realm)
{
    Client **clientp;

    for (clientp = triplet->clients; *clientp; clientp++) {
	if (*clientp == client || (realm && (*clientp)->realm == realm)) {
	    for (; *clientp; clientp++)
		*clientp = clientp[1];
	    return ZERR_NONE;
	}
    }

    return ZSRV_BADASSOC;
}

static void
free_triplet(Triplet *triplet)
{
    if (triplet->clients)
	free(triplet->clients);
    free_string(triplet->dest.classname);
    free_string(triplet->dest.inst);
    free_string(triplet->dest.recip);
    free(triplet);
}

void
triplet_dump_subs(FILE *fp)
{
    int i;
    Triplet *triplet;
    Client **clientp;

    for (i = 0; i < HASHSIZE; i++) {
	for (triplet = triplet_bucket[i]; triplet; triplet = triplet->next) {
	    fputs("Triplet '", fp);
	    dump_quote(triplet->dest.classname->string, fp);
	    fputs("' '", fp);
	    dump_quote(triplet->dest.inst->string, fp);
	    fputs("' '", fp);
	    dump_quote(triplet->dest.recip->string, fp);
	    fputs("':\n", fp);
	    if (triplet->clients) {
		for (clientp = triplet->clients; *clientp; clientp++) {
		    fprintf(fp, "\t%s %d (%s)\n",
			    inet_ntoa((*clientp)->addr.sin_addr),
			    ntohs((*clientp)->addr.sin_port),
			    (*clientp)->principal->string);
		}
	    }
	}
    }
}