1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
adx |
30 |
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file hash.c |
23 |
|
|
* \brief Hash table management. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
michael |
1011 |
#include "list.h" |
29 |
michael |
1309 |
#include "conf.h" |
30 |
adx |
30 |
#include "channel.h" |
31 |
|
|
#include "channel_mode.h" |
32 |
|
|
#include "client.h" |
33 |
|
|
#include "modules.h" |
34 |
|
|
#include "hash.h" |
35 |
|
|
#include "resv.h" |
36 |
michael |
982 |
#include "rng_mt.h" |
37 |
adx |
30 |
#include "userhost.h" |
38 |
|
|
#include "irc_string.h" |
39 |
|
|
#include "ircd.h" |
40 |
|
|
#include "numeric.h" |
41 |
|
|
#include "send.h" |
42 |
|
|
#include "memory.h" |
43 |
michael |
1654 |
#include "mempool.h" |
44 |
adx |
30 |
#include "dbuf.h" |
45 |
michael |
3347 |
#include "user.h" |
46 |
adx |
30 |
|
47 |
|
|
|
48 |
michael |
1654 |
static mp_pool_t *userhost_pool = NULL; |
49 |
|
|
static mp_pool_t *namehost_pool = NULL; |
50 |
adx |
30 |
|
51 |
michael |
982 |
static unsigned int hashf_xor_key = 0; |
52 |
adx |
30 |
|
53 |
|
|
/* The actual hash tables, They MUST be of the same HASHSIZE, variable |
54 |
|
|
* size tables could be supported but the rehash routine should also |
55 |
michael |
2916 |
* rebuild the transformation maps, I kept the tables of equal size |
56 |
adx |
30 |
* so that I can use one hash function. |
57 |
|
|
*/ |
58 |
|
|
static struct Client *idTable[HASHSIZE]; |
59 |
|
|
static struct Client *clientTable[HASHSIZE]; |
60 |
|
|
static struct Channel *channelTable[HASHSIZE]; |
61 |
|
|
static struct UserHost *userhostTable[HASHSIZE]; |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/* init_hash() |
65 |
|
|
* |
66 |
|
|
* inputs - NONE |
67 |
|
|
* output - NONE |
68 |
|
|
* side effects - Initialize the maps used by hash |
69 |
|
|
* functions and clear the tables |
70 |
|
|
*/ |
71 |
|
|
void |
72 |
michael |
1798 |
hash_init(void) |
73 |
adx |
30 |
{ |
74 |
michael |
1964 |
userhost_pool = mp_pool_new(sizeof(struct UserHost), MP_CHUNK_SIZE_USERHOST); |
75 |
|
|
namehost_pool = mp_pool_new(sizeof(struct NameHost), MP_CHUNK_SIZE_NAMEHOST); |
76 |
adx |
30 |
|
77 |
michael |
982 |
hashf_xor_key = genrand_int32() % 256; /* better than nothing --adx */ |
78 |
adx |
30 |
} |
79 |
|
|
|
80 |
|
|
/* |
81 |
|
|
* New hash function based on the Fowler/Noll/Vo (FNV) algorithm from |
82 |
|
|
* http://www.isthe.com/chongo/tech/comp/fnv/ |
83 |
|
|
* |
84 |
|
|
* Here, we use the FNV-1 method, which gives slightly better results |
85 |
|
|
* than FNV-1a. -Michael |
86 |
|
|
*/ |
87 |
|
|
unsigned int |
88 |
|
|
strhash(const char *name) |
89 |
|
|
{ |
90 |
|
|
const unsigned char *p = (const unsigned char *)name; |
91 |
|
|
unsigned int hval = FNV1_32_INIT; |
92 |
|
|
|
93 |
michael |
1826 |
if (EmptyString(p)) |
94 |
adx |
30 |
return 0; |
95 |
michael |
4890 |
|
96 |
|
|
for (; *p; ++p) |
97 |
adx |
30 |
{ |
98 |
|
|
hval += (hval << 1) + (hval << 4) + (hval << 7) + |
99 |
|
|
(hval << 8) + (hval << 24); |
100 |
michael |
982 |
hval ^= (ToLower(*p) ^ hashf_xor_key); |
101 |
adx |
30 |
} |
102 |
|
|
|
103 |
michael |
982 |
return (hval >> FNV1_32_BITS) ^ (hval & ((1 << FNV1_32_BITS) - 1)); |
104 |
adx |
30 |
} |
105 |
|
|
|
106 |
|
|
/************************** Externally visible functions ********************/ |
107 |
|
|
|
108 |
|
|
/* Optimization note: in these functions I supposed that the CSE optimization |
109 |
|
|
* (Common Subexpression Elimination) does its work decently, this means that |
110 |
|
|
* I avoided introducing new variables to do the work myself and I did let |
111 |
|
|
* the optimizer play with more free registers, actual tests proved this |
112 |
|
|
* solution to be faster than doing things like tmp2=tmp->hnext... and then |
113 |
|
|
* use tmp2 myself which would have given less freedom to the optimizer. |
114 |
|
|
*/ |
115 |
|
|
|
116 |
|
|
/* hash_add_client() |
117 |
|
|
* |
118 |
|
|
* inputs - pointer to client |
119 |
|
|
* output - NONE |
120 |
|
|
* side effects - Adds a client's name in the proper hash linked |
121 |
|
|
* list, can't fail, client_p must have a non-null |
122 |
|
|
* name or expect a coredump, the name is infact |
123 |
|
|
* taken from client_p->name |
124 |
|
|
*/ |
125 |
|
|
void |
126 |
|
|
hash_add_client(struct Client *client_p) |
127 |
|
|
{ |
128 |
|
|
unsigned int hashv = strhash(client_p->name); |
129 |
|
|
|
130 |
|
|
client_p->hnext = clientTable[hashv]; |
131 |
|
|
clientTable[hashv] = client_p; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
/* hash_add_channel() |
135 |
|
|
* |
136 |
|
|
* inputs - pointer to channel |
137 |
|
|
* output - NONE |
138 |
|
|
* side effects - Adds a channel's name in the proper hash linked |
139 |
|
|
* list, can't fail. chptr must have a non-null name |
140 |
|
|
* or expect a coredump. As before the name is taken |
141 |
|
|
* from chptr->name, we do hash its entire lenght |
142 |
|
|
* since this proved to be statistically faster |
143 |
|
|
*/ |
144 |
|
|
void |
145 |
|
|
hash_add_channel(struct Channel *chptr) |
146 |
|
|
{ |
147 |
michael |
4618 |
unsigned int hashv = strhash(chptr->name); |
148 |
adx |
30 |
|
149 |
|
|
chptr->hnextch = channelTable[hashv]; |
150 |
|
|
channelTable[hashv] = chptr; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
void |
154 |
|
|
hash_add_userhost(struct UserHost *userhost) |
155 |
|
|
{ |
156 |
|
|
unsigned int hashv = strhash(userhost->host); |
157 |
|
|
|
158 |
|
|
userhost->next = userhostTable[hashv]; |
159 |
|
|
userhostTable[hashv] = userhost; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
void |
163 |
|
|
hash_add_id(struct Client *client_p) |
164 |
|
|
{ |
165 |
|
|
unsigned int hashv = strhash(client_p->id); |
166 |
|
|
|
167 |
|
|
client_p->idhnext = idTable[hashv]; |
168 |
|
|
idTable[hashv] = client_p; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
/* hash_del_id() |
172 |
|
|
* |
173 |
|
|
* inputs - pointer to client |
174 |
|
|
* output - NONE |
175 |
|
|
* side effects - Removes an ID from the hash linked list |
176 |
|
|
*/ |
177 |
|
|
void |
178 |
|
|
hash_del_id(struct Client *client_p) |
179 |
|
|
{ |
180 |
|
|
unsigned int hashv = strhash(client_p->id); |
181 |
|
|
struct Client *tmp = idTable[hashv]; |
182 |
|
|
|
183 |
michael |
3241 |
if (tmp) |
184 |
adx |
30 |
{ |
185 |
|
|
if (tmp == client_p) |
186 |
|
|
{ |
187 |
|
|
idTable[hashv] = client_p->idhnext; |
188 |
|
|
client_p->idhnext = client_p; |
189 |
|
|
} |
190 |
|
|
else |
191 |
|
|
{ |
192 |
|
|
while (tmp->idhnext != client_p) |
193 |
|
|
if ((tmp = tmp->idhnext) == NULL) |
194 |
|
|
return; |
195 |
|
|
|
196 |
|
|
tmp->idhnext = tmp->idhnext->idhnext; |
197 |
|
|
client_p->idhnext = client_p; |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
/* hash_del_client() |
203 |
|
|
* |
204 |
|
|
* inputs - pointer to client |
205 |
|
|
* output - NONE |
206 |
|
|
* side effects - Removes a Client's name from the hash linked list |
207 |
|
|
*/ |
208 |
|
|
void |
209 |
|
|
hash_del_client(struct Client *client_p) |
210 |
|
|
{ |
211 |
|
|
unsigned int hashv = strhash(client_p->name); |
212 |
|
|
struct Client *tmp = clientTable[hashv]; |
213 |
|
|
|
214 |
michael |
3241 |
if (tmp) |
215 |
adx |
30 |
{ |
216 |
|
|
if (tmp == client_p) |
217 |
|
|
{ |
218 |
|
|
clientTable[hashv] = client_p->hnext; |
219 |
|
|
client_p->hnext = client_p; |
220 |
|
|
} |
221 |
|
|
else |
222 |
|
|
{ |
223 |
|
|
while (tmp->hnext != client_p) |
224 |
|
|
if ((tmp = tmp->hnext) == NULL) |
225 |
|
|
return; |
226 |
|
|
|
227 |
|
|
tmp->hnext = tmp->hnext->hnext; |
228 |
|
|
client_p->hnext = client_p; |
229 |
|
|
} |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
/* hash_del_userhost() |
234 |
|
|
* |
235 |
|
|
* inputs - pointer to userhost |
236 |
|
|
* output - NONE |
237 |
|
|
* side effects - Removes a userhost from the hash linked list |
238 |
|
|
*/ |
239 |
|
|
void |
240 |
|
|
hash_del_userhost(struct UserHost *userhost) |
241 |
|
|
{ |
242 |
|
|
unsigned int hashv = strhash(userhost->host); |
243 |
|
|
struct UserHost *tmp = userhostTable[hashv]; |
244 |
|
|
|
245 |
michael |
3241 |
if (tmp) |
246 |
adx |
30 |
{ |
247 |
|
|
if (tmp == userhost) |
248 |
|
|
{ |
249 |
|
|
userhostTable[hashv] = userhost->next; |
250 |
|
|
userhost->next = userhost; |
251 |
|
|
} |
252 |
|
|
else |
253 |
|
|
{ |
254 |
|
|
while (tmp->next != userhost) |
255 |
|
|
if ((tmp = tmp->next) == NULL) |
256 |
|
|
return; |
257 |
|
|
|
258 |
|
|
tmp->next = tmp->next->next; |
259 |
|
|
userhost->next = userhost; |
260 |
|
|
} |
261 |
|
|
} |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
/* hash_del_channel() |
265 |
|
|
* |
266 |
|
|
* inputs - pointer to client |
267 |
|
|
* output - NONE |
268 |
|
|
* side effects - Removes the channel's name from the corresponding |
269 |
|
|
* hash linked list |
270 |
|
|
*/ |
271 |
|
|
void |
272 |
|
|
hash_del_channel(struct Channel *chptr) |
273 |
|
|
{ |
274 |
michael |
4618 |
unsigned int hashv = strhash(chptr->name); |
275 |
adx |
30 |
struct Channel *tmp = channelTable[hashv]; |
276 |
|
|
|
277 |
michael |
3241 |
if (tmp) |
278 |
adx |
30 |
{ |
279 |
|
|
if (tmp == chptr) |
280 |
|
|
{ |
281 |
|
|
channelTable[hashv] = chptr->hnextch; |
282 |
|
|
chptr->hnextch = chptr; |
283 |
|
|
} |
284 |
|
|
else |
285 |
|
|
{ |
286 |
|
|
while (tmp->hnextch != chptr) |
287 |
|
|
if ((tmp = tmp->hnextch) == NULL) |
288 |
|
|
return; |
289 |
|
|
|
290 |
|
|
tmp->hnextch = tmp->hnextch->hnextch; |
291 |
|
|
chptr->hnextch = chptr; |
292 |
|
|
} |
293 |
|
|
} |
294 |
|
|
} |
295 |
|
|
|
296 |
michael |
1169 |
/* hash_find_client() |
297 |
adx |
30 |
* |
298 |
|
|
* inputs - pointer to name |
299 |
|
|
* output - NONE |
300 |
|
|
* side effects - New semantics: finds a client whose name is 'name' |
301 |
|
|
* if can't find one returns NULL. If it finds one moves |
302 |
|
|
* it to the top of the list and returns it. |
303 |
|
|
*/ |
304 |
|
|
struct Client * |
305 |
michael |
1169 |
hash_find_client(const char *name) |
306 |
adx |
30 |
{ |
307 |
|
|
unsigned int hashv = strhash(name); |
308 |
|
|
struct Client *client_p; |
309 |
|
|
|
310 |
michael |
3241 |
if ((client_p = clientTable[hashv])) |
311 |
adx |
30 |
{ |
312 |
|
|
if (irccmp(name, client_p->name)) |
313 |
|
|
{ |
314 |
|
|
struct Client *prev; |
315 |
|
|
|
316 |
michael |
3241 |
while (prev = client_p, (client_p = client_p->hnext)) |
317 |
adx |
30 |
{ |
318 |
|
|
if (!irccmp(name, client_p->name)) |
319 |
|
|
{ |
320 |
|
|
prev->hnext = client_p->hnext; |
321 |
|
|
client_p->hnext = clientTable[hashv]; |
322 |
|
|
clientTable[hashv] = client_p; |
323 |
|
|
break; |
324 |
|
|
} |
325 |
|
|
} |
326 |
|
|
} |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
return client_p; |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
struct Client * |
333 |
|
|
hash_find_id(const char *name) |
334 |
|
|
{ |
335 |
|
|
unsigned int hashv = strhash(name); |
336 |
|
|
struct Client *client_p; |
337 |
|
|
|
338 |
michael |
3242 |
if ((client_p = idTable[hashv])) |
339 |
adx |
30 |
{ |
340 |
michael |
880 |
if (strcmp(name, client_p->id)) |
341 |
adx |
30 |
{ |
342 |
|
|
struct Client *prev; |
343 |
|
|
|
344 |
michael |
3242 |
while (prev = client_p, (client_p = client_p->idhnext)) |
345 |
adx |
30 |
{ |
346 |
michael |
880 |
if (!strcmp(name, client_p->id)) |
347 |
adx |
30 |
{ |
348 |
|
|
prev->idhnext = client_p->idhnext; |
349 |
|
|
client_p->idhnext = idTable[hashv]; |
350 |
|
|
idTable[hashv] = client_p; |
351 |
|
|
break; |
352 |
|
|
} |
353 |
|
|
} |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
return client_p; |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
struct Client * |
361 |
michael |
1169 |
hash_find_server(const char *name) |
362 |
adx |
30 |
{ |
363 |
|
|
unsigned int hashv = strhash(name); |
364 |
|
|
struct Client *client_p = NULL; |
365 |
|
|
|
366 |
|
|
if (IsDigit(*name) && strlen(name) == IRC_MAXSID) |
367 |
michael |
1397 |
return hash_find_id(name); |
368 |
adx |
30 |
|
369 |
michael |
3242 |
if ((client_p = clientTable[hashv])) |
370 |
adx |
30 |
{ |
371 |
|
|
if ((!IsServer(client_p) && !IsMe(client_p)) || |
372 |
|
|
irccmp(name, client_p->name)) |
373 |
|
|
{ |
374 |
|
|
struct Client *prev; |
375 |
|
|
|
376 |
michael |
3242 |
while (prev = client_p, (client_p = client_p->hnext)) |
377 |
adx |
30 |
{ |
378 |
|
|
if ((IsServer(client_p) || IsMe(client_p)) && |
379 |
|
|
!irccmp(name, client_p->name)) |
380 |
|
|
{ |
381 |
|
|
prev->hnext = client_p->hnext; |
382 |
|
|
client_p->hnext = clientTable[hashv]; |
383 |
|
|
clientTable[hashv] = client_p; |
384 |
|
|
break; |
385 |
|
|
} |
386 |
|
|
} |
387 |
|
|
} |
388 |
|
|
} |
389 |
|
|
|
390 |
michael |
1118 |
return client_p; |
391 |
adx |
30 |
} |
392 |
|
|
|
393 |
|
|
/* hash_find_channel() |
394 |
|
|
* |
395 |
|
|
* inputs - pointer to name |
396 |
|
|
* output - NONE |
397 |
michael |
2916 |
* side effects - New semantics: finds a channel whose name is 'name', |
398 |
adx |
30 |
* if can't find one returns NULL, if can find it moves |
399 |
|
|
* it to the top of the list and returns it. |
400 |
|
|
*/ |
401 |
|
|
struct Channel * |
402 |
|
|
hash_find_channel(const char *name) |
403 |
|
|
{ |
404 |
|
|
unsigned int hashv = strhash(name); |
405 |
|
|
struct Channel *chptr = NULL; |
406 |
|
|
|
407 |
michael |
3242 |
if ((chptr = channelTable[hashv])) |
408 |
adx |
30 |
{ |
409 |
michael |
4618 |
if (irccmp(name, chptr->name)) |
410 |
adx |
30 |
{ |
411 |
|
|
struct Channel *prev; |
412 |
|
|
|
413 |
michael |
3242 |
while (prev = chptr, (chptr = chptr->hnextch)) |
414 |
adx |
30 |
{ |
415 |
michael |
4618 |
if (!irccmp(name, chptr->name)) |
416 |
adx |
30 |
{ |
417 |
|
|
prev->hnextch = chptr->hnextch; |
418 |
|
|
chptr->hnextch = channelTable[hashv]; |
419 |
|
|
channelTable[hashv] = chptr; |
420 |
|
|
break; |
421 |
|
|
} |
422 |
|
|
} |
423 |
|
|
} |
424 |
|
|
} |
425 |
|
|
|
426 |
|
|
return chptr; |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
/* hash_get_bucket(int type, unsigned int hashv) |
430 |
|
|
* |
431 |
|
|
* inputs - hash value (must be between 0 and HASHSIZE - 1) |
432 |
|
|
* output - NONE |
433 |
|
|
* returns - pointer to first channel in channelTable[hashv] |
434 |
|
|
* if that exists; |
435 |
|
|
* NULL if there is no channel in that place; |
436 |
|
|
* NULL if hashv is an invalid number. |
437 |
|
|
* side effects - NONE |
438 |
|
|
*/ |
439 |
|
|
void * |
440 |
|
|
hash_get_bucket(int type, unsigned int hashv) |
441 |
|
|
{ |
442 |
|
|
assert(hashv < HASHSIZE); |
443 |
|
|
if (hashv >= HASHSIZE) |
444 |
|
|
return NULL; |
445 |
|
|
|
446 |
|
|
switch (type) |
447 |
|
|
{ |
448 |
|
|
case HASH_TYPE_ID: |
449 |
|
|
return idTable[hashv]; |
450 |
|
|
break; |
451 |
|
|
case HASH_TYPE_CHANNEL: |
452 |
|
|
return channelTable[hashv]; |
453 |
|
|
break; |
454 |
|
|
case HASH_TYPE_CLIENT: |
455 |
|
|
return clientTable[hashv]; |
456 |
|
|
break; |
457 |
|
|
case HASH_TYPE_USERHOST: |
458 |
|
|
return userhostTable[hashv]; |
459 |
|
|
break; |
460 |
|
|
default: |
461 |
|
|
assert(0); |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
return NULL; |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
struct UserHost * |
468 |
|
|
hash_find_userhost(const char *host) |
469 |
|
|
{ |
470 |
|
|
unsigned int hashv = strhash(host); |
471 |
|
|
struct UserHost *userhost; |
472 |
|
|
|
473 |
|
|
if ((userhost = userhostTable[hashv])) |
474 |
|
|
{ |
475 |
|
|
if (irccmp(host, userhost->host)) |
476 |
|
|
{ |
477 |
|
|
struct UserHost *prev; |
478 |
|
|
|
479 |
michael |
3242 |
while (prev = userhost, (userhost = userhost->next)) |
480 |
adx |
30 |
{ |
481 |
|
|
if (!irccmp(host, userhost->host)) |
482 |
|
|
{ |
483 |
|
|
prev->next = userhost->next; |
484 |
|
|
userhost->next = userhostTable[hashv]; |
485 |
|
|
userhostTable[hashv] = userhost; |
486 |
|
|
break; |
487 |
|
|
} |
488 |
|
|
} |
489 |
|
|
} |
490 |
|
|
} |
491 |
|
|
|
492 |
|
|
return userhost; |
493 |
|
|
} |
494 |
|
|
|
495 |
|
|
/* count_user_host() |
496 |
|
|
* |
497 |
|
|
* inputs - user name |
498 |
|
|
* - hostname |
499 |
|
|
* - int flag 1 if global, 0 if local |
500 |
|
|
* - pointer to where global count should go |
501 |
|
|
* - pointer to where local count should go |
502 |
|
|
* - pointer to where identd count should go (local clients only) |
503 |
|
|
* output - none |
504 |
|
|
* side effects - |
505 |
|
|
*/ |
506 |
|
|
void |
507 |
michael |
1644 |
count_user_host(const char *user, const char *host, unsigned int *global_p, |
508 |
|
|
unsigned int *local_p, unsigned int *icount_p) |
509 |
adx |
30 |
{ |
510 |
michael |
4815 |
dlink_node *node = NULL; |
511 |
adx |
30 |
struct UserHost *found_userhost; |
512 |
|
|
struct NameHost *nameh; |
513 |
|
|
|
514 |
|
|
if ((found_userhost = hash_find_userhost(host)) == NULL) |
515 |
|
|
return; |
516 |
|
|
|
517 |
michael |
4815 |
DLINK_FOREACH(node, found_userhost->list.head) |
518 |
adx |
30 |
{ |
519 |
michael |
4815 |
nameh = node->data; |
520 |
adx |
30 |
|
521 |
|
|
if (!irccmp(user, nameh->name)) |
522 |
|
|
{ |
523 |
michael |
3242 |
if (global_p) |
524 |
adx |
30 |
*global_p = nameh->gcount; |
525 |
michael |
3242 |
if (local_p) |
526 |
adx |
30 |
*local_p = nameh->lcount; |
527 |
michael |
3242 |
if (icount_p) |
528 |
adx |
30 |
*icount_p = nameh->icount; |
529 |
michael |
4890 |
|
530 |
adx |
30 |
return; |
531 |
|
|
} |
532 |
|
|
} |
533 |
|
|
} |
534 |
|
|
|
535 |
michael |
1396 |
/* find_or_add_userhost() |
536 |
|
|
* |
537 |
|
|
* inputs - host name |
538 |
|
|
* output - none |
539 |
|
|
* side effects - find UserHost * for given host name |
540 |
|
|
*/ |
541 |
|
|
static struct UserHost * |
542 |
|
|
find_or_add_userhost(const char *host) |
543 |
|
|
{ |
544 |
michael |
3242 |
struct UserHost *userhost = NULL; |
545 |
michael |
1396 |
|
546 |
michael |
3242 |
if ((userhost = hash_find_userhost(host))) |
547 |
michael |
1396 |
return userhost; |
548 |
|
|
|
549 |
michael |
1654 |
userhost = mp_pool_get(userhost_pool); |
550 |
|
|
|
551 |
michael |
1396 |
strlcpy(userhost->host, host, sizeof(userhost->host)); |
552 |
|
|
hash_add_userhost(userhost); |
553 |
|
|
|
554 |
|
|
return userhost; |
555 |
|
|
} |
556 |
|
|
|
557 |
adx |
30 |
/* add_user_host() |
558 |
|
|
* |
559 |
|
|
* inputs - user name |
560 |
|
|
* - hostname |
561 |
|
|
* - int flag 1 if global, 0 if local |
562 |
|
|
* output - none |
563 |
|
|
* side effects - add given user@host to hash tables |
564 |
|
|
*/ |
565 |
|
|
void |
566 |
|
|
add_user_host(const char *user, const char *host, int global) |
567 |
|
|
{ |
568 |
michael |
4815 |
dlink_node *node = NULL; |
569 |
adx |
30 |
struct UserHost *found_userhost; |
570 |
|
|
struct NameHost *nameh; |
571 |
michael |
3908 |
unsigned int hasident = 1; |
572 |
adx |
30 |
|
573 |
|
|
if (*user == '~') |
574 |
|
|
{ |
575 |
|
|
hasident = 0; |
576 |
|
|
++user; |
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
if ((found_userhost = find_or_add_userhost(host)) == NULL) |
580 |
|
|
return; |
581 |
|
|
|
582 |
michael |
4815 |
DLINK_FOREACH(node, found_userhost->list.head) |
583 |
adx |
30 |
{ |
584 |
michael |
4815 |
nameh = node->data; |
585 |
adx |
30 |
|
586 |
|
|
if (!irccmp(user, nameh->name)) |
587 |
|
|
{ |
588 |
|
|
nameh->gcount++; |
589 |
michael |
1396 |
|
590 |
adx |
30 |
if (!global) |
591 |
|
|
{ |
592 |
michael |
1396 |
if (hasident) |
593 |
|
|
nameh->icount++; |
594 |
|
|
nameh->lcount++; |
595 |
adx |
30 |
} |
596 |
michael |
1396 |
|
597 |
adx |
30 |
return; |
598 |
|
|
} |
599 |
|
|
} |
600 |
|
|
|
601 |
michael |
1654 |
nameh = mp_pool_get(namehost_pool); |
602 |
michael |
4086 |
|
603 |
adx |
30 |
strlcpy(nameh->name, user, sizeof(nameh->name)); |
604 |
|
|
|
605 |
|
|
nameh->gcount = 1; |
606 |
michael |
1396 |
|
607 |
adx |
30 |
if (!global) |
608 |
|
|
{ |
609 |
|
|
if (hasident) |
610 |
|
|
nameh->icount = 1; |
611 |
michael |
4890 |
|
612 |
adx |
30 |
nameh->lcount = 1; |
613 |
|
|
} |
614 |
|
|
|
615 |
|
|
dlinkAdd(nameh, &nameh->node, &found_userhost->list); |
616 |
|
|
} |
617 |
|
|
|
618 |
|
|
/* delete_user_host() |
619 |
|
|
* |
620 |
|
|
* inputs - user name |
621 |
|
|
* - hostname |
622 |
|
|
* - int flag 1 if global, 0 if local |
623 |
|
|
* output - none |
624 |
|
|
* side effects - delete given user@host to hash tables |
625 |
|
|
*/ |
626 |
|
|
void |
627 |
|
|
delete_user_host(const char *user, const char *host, int global) |
628 |
|
|
{ |
629 |
michael |
4815 |
dlink_node *node = NULL; |
630 |
michael |
3287 |
struct UserHost *found_userhost = NULL; |
631 |
|
|
unsigned int hasident = 1; |
632 |
adx |
30 |
|
633 |
|
|
if (*user == '~') |
634 |
|
|
{ |
635 |
|
|
hasident = 0; |
636 |
|
|
++user; |
637 |
|
|
} |
638 |
|
|
|
639 |
|
|
if ((found_userhost = hash_find_userhost(host)) == NULL) |
640 |
|
|
return; |
641 |
|
|
|
642 |
michael |
4815 |
DLINK_FOREACH(node, found_userhost->list.head) |
643 |
adx |
30 |
{ |
644 |
michael |
4815 |
struct NameHost *nameh = node->data; |
645 |
adx |
30 |
|
646 |
|
|
if (!irccmp(user, nameh->name)) |
647 |
|
|
{ |
648 |
|
|
if (nameh->gcount > 0) |
649 |
|
|
nameh->gcount--; |
650 |
michael |
4890 |
|
651 |
adx |
30 |
if (!global) |
652 |
|
|
{ |
653 |
michael |
1396 |
if (nameh->lcount > 0) |
654 |
|
|
nameh->lcount--; |
655 |
michael |
4890 |
|
656 |
michael |
1396 |
if (hasident && nameh->icount > 0) |
657 |
|
|
nameh->icount--; |
658 |
adx |
30 |
} |
659 |
|
|
|
660 |
|
|
if (nameh->gcount == 0 && nameh->lcount == 0) |
661 |
|
|
{ |
662 |
michael |
1396 |
dlinkDelete(&nameh->node, &found_userhost->list); |
663 |
michael |
1654 |
mp_pool_release(nameh); |
664 |
adx |
30 |
} |
665 |
|
|
|
666 |
|
|
if (dlink_list_length(&found_userhost->list) == 0) |
667 |
|
|
{ |
668 |
michael |
1396 |
hash_del_userhost(found_userhost); |
669 |
michael |
1654 |
mp_pool_release(found_userhost); |
670 |
adx |
30 |
} |
671 |
|
|
|
672 |
|
|
return; |
673 |
|
|
} |
674 |
|
|
} |
675 |
|
|
} |
676 |
|
|
|
677 |
|
|
/* |
678 |
|
|
* Safe list code. |
679 |
|
|
* |
680 |
|
|
* The idea is really quite simple. As the link lists pointed to in |
681 |
|
|
* each "bucket" of the channel hash table are traversed atomically |
682 |
|
|
* there is no locking needed. Overall, yes, inconsistent reported |
683 |
|
|
* state can still happen, but normally this isn't a big deal. |
684 |
|
|
* I don't like sticking the code into hash.c but oh well. Moreover, |
685 |
|
|
* if a hash isn't used in future, oops. |
686 |
|
|
* |
687 |
|
|
* - Dianora |
688 |
|
|
*/ |
689 |
|
|
|
690 |
|
|
/* exceeding_sendq() |
691 |
|
|
* |
692 |
|
|
* inputs - pointer to client to check |
693 |
|
|
* output - 1 if client is in danger of blowing its sendq |
694 |
|
|
* 0 if it is not. |
695 |
|
|
* side effects - |
696 |
|
|
* |
697 |
|
|
* Sendq limit is fairly conservative at 1/2 (In original anyway) |
698 |
|
|
*/ |
699 |
|
|
static int |
700 |
michael |
2757 |
exceeding_sendq(const struct Client *to) |
701 |
adx |
30 |
{ |
702 |
michael |
4588 |
if (dbuf_length(&to->connection->buf_sendq) > (get_sendq(&to->connection->confs) / 2)) |
703 |
adx |
30 |
return 1; |
704 |
|
|
else |
705 |
|
|
return 0; |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
void |
709 |
michael |
3289 |
free_list_task(struct Client *source_p) |
710 |
adx |
30 |
{ |
711 |
michael |
4870 |
struct ListTask *const lt = source_p->connection->list_task; |
712 |
michael |
4815 |
dlink_node *node = NULL, *node_next = NULL; |
713 |
adx |
30 |
|
714 |
michael |
4815 |
if ((node = dlinkFindDelete(&listing_client_list, source_p))) |
715 |
|
|
free_dlink_node(node); |
716 |
adx |
30 |
|
717 |
michael |
4815 |
DLINK_FOREACH_SAFE(node, node_next, lt->show_mask.head) |
718 |
adx |
30 |
{ |
719 |
michael |
4815 |
MyFree(node->data); |
720 |
|
|
free_dlink_node(node); |
721 |
adx |
30 |
} |
722 |
|
|
|
723 |
michael |
4815 |
DLINK_FOREACH_SAFE(node, node_next, lt->hide_mask.head) |
724 |
adx |
30 |
{ |
725 |
michael |
4815 |
MyFree(node->data); |
726 |
|
|
free_dlink_node(node); |
727 |
adx |
30 |
} |
728 |
|
|
|
729 |
|
|
MyFree(lt); |
730 |
michael |
4868 |
source_p->connection->list_task = NULL; |
731 |
adx |
30 |
} |
732 |
|
|
|
733 |
|
|
/* list_allow_channel() |
734 |
|
|
* |
735 |
|
|
* inputs - channel name |
736 |
|
|
* - pointer to a list task |
737 |
|
|
* output - 1 if the channel is to be displayed |
738 |
|
|
* 0 otherwise |
739 |
|
|
* side effects - |
740 |
|
|
*/ |
741 |
|
|
static int |
742 |
michael |
4618 |
list_allow_channel(const char *name, const struct ListTask *lt) |
743 |
adx |
30 |
{ |
744 |
michael |
4815 |
const dlink_node *node = NULL; |
745 |
adx |
30 |
|
746 |
michael |
4815 |
DLINK_FOREACH(node, lt->show_mask.head) |
747 |
|
|
if (match(node->data, name) != 0) |
748 |
adx |
30 |
return 0; |
749 |
|
|
|
750 |
michael |
4815 |
DLINK_FOREACH(node, lt->hide_mask.head) |
751 |
|
|
if (match(node->data, name) == 0) |
752 |
adx |
30 |
return 0; |
753 |
|
|
|
754 |
|
|
return 1; |
755 |
|
|
} |
756 |
|
|
|
757 |
|
|
/* list_one_channel() |
758 |
|
|
* |
759 |
|
|
* inputs - client pointer to return result to |
760 |
|
|
* - pointer to channel to list |
761 |
|
|
* - pointer to ListTask structure |
762 |
|
|
* output - none |
763 |
|
|
* side effects - |
764 |
|
|
*/ |
765 |
|
|
static void |
766 |
michael |
3288 |
list_one_channel(struct Client *source_p, struct Channel *chptr) |
767 |
adx |
30 |
{ |
768 |
michael |
4870 |
const struct ListTask *const lt = source_p->connection->list_task; |
769 |
michael |
2616 |
char listbuf[MODEBUFLEN] = ""; |
770 |
|
|
char modebuf[MODEBUFLEN] = ""; |
771 |
|
|
char parabuf[MODEBUFLEN] = ""; |
772 |
|
|
|
773 |
michael |
2495 |
if (SecretChannel(chptr) && |
774 |
michael |
3428 |
!(HasUMode(source_p, UMODE_ADMIN) || IsMember(source_p, chptr))) |
775 |
adx |
30 |
return; |
776 |
michael |
4890 |
|
777 |
michael |
3288 |
if (dlink_list_length(&chptr->members) < lt->users_min || |
778 |
|
|
dlink_list_length(&chptr->members) > lt->users_max || |
779 |
michael |
4818 |
(chptr->creationtime != 0 && |
780 |
|
|
((unsigned int)chptr->creationtime < lt->created_min || |
781 |
|
|
(unsigned int)chptr->creationtime > lt->created_max)) || |
782 |
michael |
3288 |
(unsigned int)chptr->topic_time < lt->topicts_min || |
783 |
adx |
30 |
(chptr->topic_time ? (unsigned int)chptr->topic_time : UINT_MAX) > |
784 |
michael |
3288 |
lt->topicts_max) |
785 |
adx |
30 |
return; |
786 |
|
|
|
787 |
michael |
4489 |
if (lt->topic[0] && match(lt->topic, chptr->topic)) |
788 |
|
|
return; |
789 |
|
|
|
790 |
michael |
4618 |
if (!list_allow_channel(chptr->name, lt)) |
791 |
adx |
30 |
return; |
792 |
michael |
2616 |
|
793 |
michael |
4486 |
channel_modes(chptr, source_p, modebuf, parabuf); |
794 |
michael |
2616 |
|
795 |
michael |
4486 |
if (chptr->topic[0]) |
796 |
|
|
snprintf(listbuf, sizeof(listbuf), "[%s] ", modebuf); |
797 |
|
|
else |
798 |
|
|
snprintf(listbuf, sizeof(listbuf), "[%s]", modebuf); |
799 |
michael |
2616 |
|
800 |
michael |
4618 |
sendto_one_numeric(source_p, &me, RPL_LIST, chptr->name, |
801 |
michael |
3109 |
dlink_list_length(&chptr->members), |
802 |
|
|
listbuf, chptr->topic); |
803 |
adx |
30 |
} |
804 |
|
|
|
805 |
|
|
/* safe_list_channels() |
806 |
|
|
* |
807 |
|
|
* inputs - pointer to client requesting list |
808 |
|
|
* output - 0/1 |
809 |
|
|
* side effects - safely list all channels to source_p |
810 |
|
|
* |
811 |
|
|
* Walk the channel buckets, ensure all pointers in a bucket are |
812 |
|
|
* traversed before blocking on a sendq. This means, no locking is needed. |
813 |
|
|
* |
814 |
|
|
* - Dianora |
815 |
|
|
*/ |
816 |
|
|
void |
817 |
michael |
3288 |
safe_list_channels(struct Client *source_p, int only_unmasked_channels) |
818 |
adx |
30 |
{ |
819 |
michael |
4870 |
struct ListTask *const lt = source_p->connection->list_task; |
820 |
adx |
30 |
struct Channel *chptr = NULL; |
821 |
|
|
|
822 |
|
|
if (!only_unmasked_channels) |
823 |
|
|
{ |
824 |
michael |
3288 |
for (unsigned int i = lt->hash_index; i < HASHSIZE; ++i) |
825 |
adx |
30 |
{ |
826 |
michael |
3291 |
if (exceeding_sendq(source_p)) |
827 |
adx |
30 |
{ |
828 |
michael |
3288 |
lt->hash_index = i; |
829 |
|
|
return; /* Still more to do */ |
830 |
adx |
30 |
} |
831 |
|
|
|
832 |
|
|
for (chptr = channelTable[i]; chptr; chptr = chptr->hnextch) |
833 |
michael |
3288 |
list_one_channel(source_p, chptr); |
834 |
adx |
30 |
} |
835 |
|
|
} |
836 |
|
|
else |
837 |
|
|
{ |
838 |
michael |
4815 |
dlink_node *node = NULL; |
839 |
adx |
30 |
|
840 |
michael |
4815 |
DLINK_FOREACH(node, lt->show_mask.head) |
841 |
|
|
if ((chptr = hash_find_channel(node->data))) |
842 |
michael |
3288 |
list_one_channel(source_p, chptr); |
843 |
adx |
30 |
} |
844 |
|
|
|
845 |
michael |
3289 |
free_list_task(source_p); |
846 |
michael |
3109 |
sendto_one_numeric(source_p, &me, RPL_LISTEND); |
847 |
adx |
30 |
} |