1 /* hash.h - Manage hashes for cached dns records
2 
3    Copyright (C) 2000 Thomas Moestl
4    Copyright (C) 2003, 2005 Paul A. Rombouts
5 
6   This file is part of the pdnsd package.
7 
8   pdnsd is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12 
13   pdnsd is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17 
18   You should have received a copy of the GNU General Public License
19   along with pdnsd; see the file COPYING. If not, see
20   <http://www.gnu.org/licenses/>.
21 */
22 
23 
24 #ifndef _HASH_H_
25 #define _HASH_H_
26 #include <config.h>
27 #include "cache.h"
28 
29 typedef struct dns_hash_ent_s {
30 	struct dns_hash_ent_s	*next;
31 	unsigned long		rhash; /* this is a better hash */
32 	dns_cent_t		*data;
33 } dns_hash_ent_t;
34 
35 /* Redefine this if you want another hash size. Should work ;-).
36  * The number of hash buckets is computed as power of two;
37  * so, e.g. HASH_SZ set to 10 yields 1024 hash rows (2^10 or 1<<10).
38  * Only powers of two are possible conveniently.
39  * HASH_SZ may not be bigger than 32 (if you set it even close to that value,
40  * you are nuts.) */
41 /* #define HASH_SZ       10 */  /* Now defined in config.h */
42 #define HASH_NUM_BUCKETS (1<<HASH_SZ)
43 
44 #define HASH_BITMASK     (HASH_NUM_BUCKETS-1)
45 
46 extern dns_hash_ent_t *hash_buckets[];
47 
48 /* A type for remembering the position in the hash table where a new entry can be inserted. */
49 typedef struct {
50 	dns_hash_ent_t **pos;      /* pointer to the location in the hash table */
51 	unsigned long  rhash;      /* long hash */
52 } dns_hash_loc_t;
53 
54 /* A type for position specification for fetch_first and fetch_next */
55 typedef struct {
56 	int            bucket;     /* bucket chain we are in */
57 	dns_hash_ent_t *ent;       /* entry */
58 } dns_hash_pos_t;
59 
60 inline static void mk_dns_hash()  __attribute__((always_inline));
mk_dns_hash()61 inline static void mk_dns_hash()
62 {
63 	int i;
64 	for(i=0;i<HASH_NUM_BUCKETS;i++)
65 		hash_buckets[i]=NULL;
66 }
67 
68 dns_cent_t *dns_lookup(const unsigned char *key, dns_hash_loc_t *loc);
69 int add_dns_hash(dns_cent_t *data, dns_hash_loc_t *loc);
70 dns_cent_t *del_dns_hash_ent(dns_hash_loc_t *loc);
71 dns_cent_t *del_dns_hash(const unsigned char *key);
72 void free_dns_hash_bucket(int i);
73 void free_dns_hash_selected(int i, slist_array sla);
74 void free_dns_hash();
75 
76 dns_cent_t *fetch_first(dns_hash_pos_t *pos);
77 dns_cent_t *fetch_next(dns_hash_pos_t *pos);
78 
79 #ifdef DEBUG_HASH
80 void dumphash();
81 #endif
82 
83 #endif
84