2 * ircd-ratbox: A slightly useful ircd.
3 * hash.c: Maintains hashtables.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include "ircd_defs.h"
37 #include "s_newconf.h"
39 #include "rb_dictionary.h"
40 #include "rb_radixtree.h"
42 rb_dictionary
*client_connid_tree
= NULL
;
43 rb_dictionary
*client_zconnid_tree
= NULL
;
44 rb_radixtree
*client_id_tree
= NULL
;
45 rb_radixtree
*client_name_tree
= NULL
;
47 rb_radixtree
*channel_tree
= NULL
;
48 rb_radixtree
*resv_tree
= NULL
;
49 rb_radixtree
*hostname_tree
= NULL
;
52 * look in whowas.c for the missing ...[WW_MAX]; entry
57 * clears the various hashtables
62 client_connid_tree
= rb_dictionary_create("client connid", rb_uint32cmp
);
63 client_zconnid_tree
= rb_dictionary_create("client zconnid", rb_uint32cmp
);
64 client_id_tree
= rb_radixtree_create("client id", NULL
);
65 client_name_tree
= rb_radixtree_create("client name", irccasecanon
);
67 channel_tree
= rb_radixtree_create("channel", irccasecanon
);
68 resv_tree
= rb_radixtree_create("resv", irccasecanon
);
70 hostname_tree
= rb_radixtree_create("hostname", irccasecanon
);
74 fnv_hash_upper(const unsigned char *s
, int bits
)
76 uint32_t h
= FNV1_32_INIT
;
80 h
^= irctoupper(*s
++);
81 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
84 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
89 fnv_hash(const unsigned char *s
, int bits
)
91 uint32_t h
= FNV1_32_INIT
;
96 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
99 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
104 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
106 uint32_t h
= FNV1_32_INIT
;
107 const unsigned char *x
= s
+ len
;
111 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
114 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
119 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
121 uint32_t h
= FNV1_32_INIT
;
122 const unsigned char *x
= s
+ len
;
125 h
^= irctoupper(*s
++);
126 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
129 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
135 * adds an entry to the id hash table
138 add_to_id_hash(const char *name
, struct Client
*client_p
)
140 if(EmptyString(name
) || (client_p
== NULL
))
143 rb_radixtree_add(client_id_tree
, name
, client_p
);
146 /* add_to_client_hash()
148 * adds an entry (client/server) to the client hash table
151 add_to_client_hash(const char *name
, struct Client
*client_p
)
153 s_assert(name
!= NULL
);
154 s_assert(client_p
!= NULL
);
155 if(EmptyString(name
) || (client_p
== NULL
))
158 rb_radixtree_add(client_name_tree
, name
, client_p
);
161 /* add_to_hostname_hash()
163 * adds a client entry to the hostname hash table
166 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
170 s_assert(hostname
!= NULL
);
171 s_assert(client_p
!= NULL
);
172 if(EmptyString(hostname
) || (client_p
== NULL
))
175 list
= rb_radixtree_retrieve(hostname_tree
, hostname
);
178 rb_dlinkAddAlloc(client_p
, list
);
182 list
= rb_malloc(sizeof(*list
));
183 rb_radixtree_add(hostname_tree
, hostname
, list
);
184 rb_dlinkAddAlloc(client_p
, list
);
187 /* add_to_resv_hash()
189 * adds a resv channel entry to the resv hash table
192 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
194 s_assert(!EmptyString(name
));
195 s_assert(aconf
!= NULL
);
196 if(EmptyString(name
) || aconf
== NULL
)
199 rb_radixtree_add(resv_tree
, name
, aconf
);
202 /* del_from_id_hash()
204 * removes an id from the id hash table
207 del_from_id_hash(const char *id
, struct Client
*client_p
)
209 s_assert(id
!= NULL
);
210 s_assert(client_p
!= NULL
);
211 if(EmptyString(id
) || client_p
== NULL
)
214 rb_radixtree_delete(client_id_tree
, id
);
217 /* del_from_client_hash()
219 * removes a client/server from the client hash table
222 del_from_client_hash(const char *name
, struct Client
*client_p
)
224 /* no s_asserts, this can happen when removing a client that
227 if(EmptyString(name
) || client_p
== NULL
)
230 rb_radixtree_delete(client_name_tree
, name
);
233 /* del_from_channel_hash()
235 * removes a channel from the channel hash table
238 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
240 s_assert(name
!= NULL
);
241 s_assert(chptr
!= NULL
);
243 if(EmptyString(name
) || chptr
== NULL
)
246 rb_radixtree_delete(channel_tree
, name
);
249 /* del_from_hostname_hash()
251 * removes a client entry from the hostname hash table
254 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
258 if(hostname
== NULL
|| client_p
== NULL
)
261 list
= rb_radixtree_retrieve(hostname_tree
, hostname
);
265 rb_dlinkFindDestroy(client_p
, list
);
267 if (rb_dlink_list_length(list
) == 0)
269 rb_radixtree_delete(hostname_tree
, hostname
);
274 /* del_from_resv_hash()
276 * removes a resv entry from the resv hash table
279 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
281 s_assert(name
!= NULL
);
282 s_assert(aconf
!= NULL
);
283 if(EmptyString(name
) || aconf
== NULL
)
286 rb_radixtree_delete(resv_tree
, name
);
291 * finds a client entry from the id hash table
294 find_id(const char *name
)
296 if(EmptyString(name
))
299 return rb_radixtree_retrieve(client_id_tree
, name
);
304 * finds a client/server entry from the client hash table
307 find_client(const char *name
)
309 s_assert(name
!= NULL
);
310 if(EmptyString(name
))
313 /* hunting for an id, not a nick */
315 return (find_id(name
));
317 return rb_radixtree_retrieve(client_name_tree
, name
);
320 /* find_named_client()
322 * finds a client/server entry from the client hash table
325 find_named_client(const char *name
)
327 s_assert(name
!= NULL
);
328 if(EmptyString(name
))
331 return rb_radixtree_retrieve(client_name_tree
, name
);
336 * finds a server from the client hash table
339 find_server(struct Client
*source_p
, const char *name
)
341 struct Client
*target_p
;
343 if(EmptyString(name
))
346 if((source_p
== NULL
|| !MyClient(source_p
)) &&
347 IsDigit(*name
) && strlen(name
) == 3)
349 target_p
= find_id(name
);
353 target_p
= rb_radixtree_retrieve(client_name_tree
, name
);
354 if (target_p
!= NULL
)
356 if(IsServer(target_p
) || IsMe(target_p
))
365 * finds a hostname rb_dlink list from the hostname hash table.
366 * we return the full rb_dlink list, because you can have multiple
367 * entries with the same hostname
370 find_hostname(const char *hostname
)
372 rb_dlink_list
*hlist
;
374 if(EmptyString(hostname
))
377 hlist
= rb_radixtree_retrieve(hostname_tree
, hostname
);
386 * finds a channel from the channel hash table
389 find_channel(const char *name
)
391 s_assert(name
!= NULL
);
392 if(EmptyString(name
))
395 return rb_radixtree_retrieve(channel_tree
, name
);
399 * get_or_create_channel
400 * inputs - client pointer
402 * - pointer to int flag whether channel was newly created or not
403 * output - returns channel block or NULL if illegal name
404 * - also modifies *isnew
406 * Get Channel block for chname (and allocate a new channel
407 * block, if it didn't exist before).
410 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
412 struct Channel
*chptr
;
414 const char *s
= chname
;
423 if(IsServer(client_p
))
425 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
426 "*** Long channel name from %s (%d > %d): %s",
427 client_p
->name
, len
, CHANNELLEN
, s
);
431 *(t
+ CHANNELLEN
) = '\0';
435 chptr
= rb_radixtree_retrieve(channel_tree
, s
);
446 chptr
= allocate_channel(s
);
447 chptr
->channelts
= rb_current_time(); /* doesn't hurt to set it here */
449 rb_dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
450 rb_radixtree_add(channel_tree
, chptr
->chname
, chptr
);
457 * hunts for a resv entry in the resv hash table
460 hash_find_resv(const char *name
)
462 struct ConfItem
*aconf
;
464 s_assert(name
!= NULL
);
465 if(EmptyString(name
))
468 aconf
= rb_radixtree_retrieve(resv_tree
, name
);
479 clear_resv_hash(void)
481 struct ConfItem
*aconf
;
482 rb_radixtree_iteration_state iter
;
484 RB_RADIXTREE_FOREACH(aconf
, &iter
, resv_tree
)
486 /* skip temp resvs */
490 rb_radixtree_delete(resv_tree
, aconf
->host
);
496 add_to_zconnid_hash(struct Client
*client_p
)
498 rb_dictionary_add(client_zconnid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->zconnid
), client_p
);
502 del_from_zconnid_hash(struct Client
*client_p
)
504 rb_dictionary_delete(client_zconnid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->zconnid
));
508 add_to_cli_connid_hash(struct Client
*client_p
)
510 rb_dictionary_add(client_connid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->connid
), client_p
);
514 del_from_cli_connid_hash(struct Client
*client_p
)
516 rb_dictionary_delete(client_connid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->connid
));
520 find_cli_connid_hash(uint32_t connid
)
522 struct Client
*target_p
;
524 target_p
= rb_dictionary_retrieve(client_connid_tree
, RB_UINT_TO_POINTER(connid
));
525 if (target_p
!= NULL
)
528 target_p
= rb_dictionary_retrieve(client_zconnid_tree
, RB_UINT_TO_POINTER(connid
));
529 if (target_p
!= NULL
)