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"
38 #include "s_newconf.h"
40 #include "rb_dictionary.h"
41 #include "rb_radixtree.h"
43 struct Dictionary
*client_connid_tree
= NULL
;
44 struct Dictionary
*client_zconnid_tree
= NULL
;
45 struct rb_radixtree
*client_id_tree
= NULL
;
46 struct rb_radixtree
*client_name_tree
= NULL
;
48 struct rb_radixtree
*channel_tree
= NULL
;
49 struct rb_radixtree
*resv_tree
= NULL
;
50 struct rb_radixtree
*hostname_tree
= NULL
;
53 * look in whowas.c for the missing ...[WW_MAX]; entry
58 * clears the various hashtables
63 client_connid_tree
= rb_dictionary_create("client connid", rb_uint32cmp
);
64 client_zconnid_tree
= rb_dictionary_create("client zconnid", rb_uint32cmp
);
65 client_id_tree
= rb_radixtree_create("client id", NULL
);
66 client_name_tree
= rb_radixtree_create("client name", irccasecanon
);
68 channel_tree
= rb_radixtree_create("channel", irccasecanon
);
69 resv_tree
= rb_radixtree_create("resv", irccasecanon
);
71 hostname_tree
= rb_radixtree_create("hostname", irccasecanon
);
75 fnv_hash_upper(const unsigned char *s
, int bits
)
77 u_int32_t h
= FNV1_32_INIT
;
81 h
^= irctoupper(*s
++);
82 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
85 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
90 fnv_hash(const unsigned char *s
, int bits
)
92 u_int32_t h
= FNV1_32_INIT
;
97 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
100 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
105 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
107 u_int32_t h
= FNV1_32_INIT
;
108 const unsigned char *x
= s
+ len
;
112 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
115 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
120 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
122 u_int32_t h
= FNV1_32_INIT
;
123 const unsigned char *x
= s
+ len
;
126 h
^= irctoupper(*s
++);
127 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
130 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
136 * adds an entry to the id hash table
139 add_to_id_hash(const char *name
, struct Client
*client_p
)
141 if(EmptyString(name
) || (client_p
== NULL
))
144 rb_radixtree_add(client_id_tree
, name
, client_p
);
147 /* add_to_client_hash()
149 * adds an entry (client/server) to the client hash table
152 add_to_client_hash(const char *name
, struct Client
*client_p
)
154 s_assert(name
!= NULL
);
155 s_assert(client_p
!= NULL
);
156 if(EmptyString(name
) || (client_p
== NULL
))
159 rb_radixtree_add(client_name_tree
, name
, client_p
);
162 /* add_to_hostname_hash()
164 * adds a client entry to the hostname hash table
167 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
171 s_assert(hostname
!= NULL
);
172 s_assert(client_p
!= NULL
);
173 if(EmptyString(hostname
) || (client_p
== NULL
))
176 list
= rb_radixtree_retrieve(hostname_tree
, hostname
);
179 rb_dlinkAddAlloc(client_p
, list
);
183 list
= rb_malloc(sizeof(*list
));
184 rb_radixtree_add(hostname_tree
, hostname
, list
);
185 rb_dlinkAddAlloc(client_p
, list
);
188 /* add_to_resv_hash()
190 * adds a resv channel entry to the resv hash table
193 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
195 s_assert(!EmptyString(name
));
196 s_assert(aconf
!= NULL
);
197 if(EmptyString(name
) || aconf
== NULL
)
200 rb_radixtree_add(resv_tree
, name
, aconf
);
203 /* del_from_id_hash()
205 * removes an id from the id hash table
208 del_from_id_hash(const char *id
, struct Client
*client_p
)
210 s_assert(id
!= NULL
);
211 s_assert(client_p
!= NULL
);
212 if(EmptyString(id
) || client_p
== NULL
)
215 rb_radixtree_delete(client_id_tree
, id
);
218 /* del_from_client_hash()
220 * removes a client/server from the client hash table
223 del_from_client_hash(const char *name
, struct Client
*client_p
)
225 /* no s_asserts, this can happen when removing a client that
228 if(EmptyString(name
) || client_p
== NULL
)
231 rb_radixtree_delete(client_name_tree
, name
);
234 /* del_from_channel_hash()
236 * removes a channel from the channel hash table
239 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
241 s_assert(name
!= NULL
);
242 s_assert(chptr
!= NULL
);
244 if(EmptyString(name
) || chptr
== NULL
)
247 rb_radixtree_delete(channel_tree
, name
);
250 /* del_from_hostname_hash()
252 * removes a client entry from the hostname hash table
255 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
259 if(hostname
== NULL
|| client_p
== NULL
)
262 list
= rb_radixtree_retrieve(hostname_tree
, hostname
);
266 rb_dlinkFindDestroy(client_p
, list
);
268 if (rb_dlink_list_length(list
) == 0)
270 rb_radixtree_delete(hostname_tree
, hostname
);
275 /* del_from_resv_hash()
277 * removes a resv entry from the resv hash table
280 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
282 s_assert(name
!= NULL
);
283 s_assert(aconf
!= NULL
);
284 if(EmptyString(name
) || aconf
== NULL
)
287 rb_radixtree_delete(resv_tree
, name
);
292 * finds a client entry from the id hash table
295 find_id(const char *name
)
297 if(EmptyString(name
))
300 return rb_radixtree_retrieve(client_id_tree
, name
);
305 * finds a client/server entry from the client hash table
308 find_client(const char *name
)
310 s_assert(name
!= NULL
);
311 if(EmptyString(name
))
314 /* hunting for an id, not a nick */
316 return (find_id(name
));
318 return rb_radixtree_retrieve(client_name_tree
, name
);
321 /* find_named_client()
323 * finds a client/server entry from the client hash table
326 find_named_client(const char *name
)
328 s_assert(name
!= NULL
);
329 if(EmptyString(name
))
332 return rb_radixtree_retrieve(client_name_tree
, name
);
337 * finds a server from the client hash table
340 find_server(struct Client
*source_p
, const char *name
)
342 struct Client
*target_p
;
344 if(EmptyString(name
))
347 if((source_p
== NULL
|| !MyClient(source_p
)) &&
348 IsDigit(*name
) && strlen(name
) == 3)
350 target_p
= find_id(name
);
354 target_p
= rb_radixtree_retrieve(client_name_tree
, name
);
355 if (target_p
!= NULL
)
357 if(IsServer(target_p
) || IsMe(target_p
))
366 * finds a hostname rb_dlink list from the hostname hash table.
367 * we return the full rb_dlink list, because you can have multiple
368 * entries with the same hostname
371 find_hostname(const char *hostname
)
373 rb_dlink_list
*hlist
;
375 if(EmptyString(hostname
))
378 hlist
= rb_radixtree_retrieve(hostname_tree
, hostname
);
387 * finds a channel from the channel hash table
390 find_channel(const char *name
)
392 s_assert(name
!= NULL
);
393 if(EmptyString(name
))
396 return rb_radixtree_retrieve(channel_tree
, name
);
400 * get_or_create_channel
401 * inputs - client pointer
403 * - pointer to int flag whether channel was newly created or not
404 * output - returns channel block or NULL if illegal name
405 * - also modifies *isnew
407 * Get Channel block for chname (and allocate a new channel
408 * block, if it didn't exist before).
411 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
413 struct Channel
*chptr
;
415 const char *s
= chname
;
424 if(IsServer(client_p
))
426 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
427 "*** Long channel name from %s (%d > %d): %s",
428 client_p
->name
, len
, CHANNELLEN
, s
);
432 *(t
+ CHANNELLEN
) = '\0';
436 chptr
= rb_radixtree_retrieve(channel_tree
, s
);
447 chptr
= allocate_channel(s
);
448 chptr
->channelts
= rb_current_time(); /* doesn't hurt to set it here */
450 rb_dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
451 rb_radixtree_add(channel_tree
, chptr
->chname
, chptr
);
458 * hunts for a resv entry in the resv hash table
461 hash_find_resv(const char *name
)
463 struct ConfItem
*aconf
;
465 s_assert(name
!= NULL
);
466 if(EmptyString(name
))
469 aconf
= rb_radixtree_retrieve(resv_tree
, name
);
480 clear_resv_hash(void)
482 struct ConfItem
*aconf
;
483 struct rb_radixtree_iteration_state iter
;
485 RB_RADIXTREE_FOREACH(aconf
, &iter
, resv_tree
)
487 /* skip temp resvs */
491 rb_radixtree_delete(resv_tree
, aconf
->host
);
497 add_to_zconnid_hash(struct Client
*client_p
)
499 rb_dictionary_add(client_zconnid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->zconnid
), client_p
);
503 del_from_zconnid_hash(struct Client
*client_p
)
505 rb_dictionary_delete(client_zconnid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->zconnid
));
509 add_to_cli_connid_hash(struct Client
*client_p
)
511 rb_dictionary_add(client_connid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->connid
), client_p
);
515 del_from_cli_connid_hash(struct Client
*client_p
)
517 rb_dictionary_delete(client_connid_tree
, RB_UINT_TO_POINTER(client_p
->localClient
->connid
));
521 find_cli_connid_hash(uint32_t connid
)
523 struct Client
*target_p
;
525 target_p
= rb_dictionary_retrieve(client_connid_tree
, RB_UINT_TO_POINTER(connid
));
526 if (target_p
!= NULL
)
529 target_p
= rb_dictionary_retrieve(client_zconnid_tree
, RB_UINT_TO_POINTER(connid
));
530 if (target_p
!= NULL
)