2 Unix SMB/CIFS implementation.
4 NetBIOS name cache module on top of gencache mechanism.
6 Copyright (C) Tim Potter 2002
7 Copyright (C) Rafal Szczesniak 2002
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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/time.h"
27 #define NBTKEY_FMT "NBT/%s#%02X"
31 * Initialise namecache system. Function calls gencache
32 * initialisation function to perform necessary actions
34 * @return true upon successful initialisation of the cache or
38 BOOL namecache_enableTODO(void)
41 * Check if name caching disabled by setting the name cache
45 if (lp_name_cache_timeout() == 0) {
46 DEBUG(5, ("namecache_enable: disabling netbios name cache\n"));
50 /* Init namecache by calling gencache initialisation */
52 if (!gencache_init()) {
53 DEBUG(2, ("namecache_enable: Couldn't initialise namecache on top of gencache.\n"));
57 /* I leave it for now, though I don't think we really need this (mimir, 27.09.2002) */
58 DEBUG(5, ("namecache_enable: enabling netbios namecache, timeout %d "
59 "seconds\n", lp_name_cache_timeout()));
66 * Shutdown namecache. Routine calls gencache close function
67 * to safely close gencache file.
69 * @return true upon successful shutdown of the cache or
73 BOOL namecache_shutdownTODO(void)
75 if (!gencache_shutdown()) {
76 DEBUG(2, ("namecache_shutdown: Couldn't close namecache on top of gencache.\n"));
80 DEBUG(5, ("namecache_shutdown: netbios namecache closed successfully.\n"));
86 * Generates a key for netbios name lookups on basis of
87 * netbios name and type.
88 * The caller must free returned key string when finished.
90 * @param name netbios name string (case insensitive)
91 * @param name_type netbios type of the name being looked up
93 * @return string consisted of uppercased name and appended
97 static char* namecache_key(TALLOC_CTX *mem_ctx, const char *name, int name_type)
100 asprintf(&keystr, NBTKEY_FMT, strupper_talloc(mem_ctx, name), name_type);
107 * Store a name(s) in the name cache
109 * @param name netbios names array
110 * @param name_type integer netbios name type
111 * @param num_names number of names being stored
112 * @param ip_list array of in_addr structures containing
113 * ip addresses being stored
116 BOOL namecache_store(TALLOC_CTX *mem_ctx, const char *name, int name_type,
117 int num_names, struct ipv4_addr *ip_list)
120 char *key, *value_string;
124 * we use gecache call to avoid annoying debug messages about
125 * initialised namecache again and again...
127 if (!gencache_init()) return False;
129 DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
130 num_names, num_names == 1 ? "": "es", name, name_type));
132 for (i = 0; i < num_names; i++)
133 DEBUGADD(5, ("%s%s", sys_inet_ntoa(ip_list[i]),
134 i == (num_names - 1) ? "" : ", "));
138 key = namecache_key(mem_ctx, name, name_type);
141 * Cache pdc location or dc lists for only a little while
142 * otherwise if we lock on to a bad DC we can potentially be
143 * out of action for the entire cache timeout time!
146 if (name_type == 0x1b || name_type == 0x1c)
147 expiry = time(NULL) + 10;
149 expiry = time(NULL) + lp_name_cache_timeout();
152 * Generate string representation of ip addresses list
153 * First, store the number of ip addresses and then
154 * place each single ip
156 ipstr_list_make(&value_string, ip_list, num_names);
159 return (gencache_set(key, value_string, expiry));
164 * Look up a name in the cache.
166 * @param name netbios name to look up for
167 * @param name_type netbios name type of @param name
168 * @param ip_list mallocated list of IP addresses if found in the cache,
170 * @param num_names number of entries found
172 * @return true upon successful fetch or
173 * false if name isn't found in the cache or has expired
176 BOOL namecache_fetch(TALLOC_CTX *mem_ctx, const char *name, int name_type, struct ipv4_addr **ip_list,
184 /* exit now if null pointers were passed as they're required further */
185 if (!ip_list || !num_names) return False;
187 if (!gencache_init())
191 * Use gencache interface - lookup the key
193 key = namecache_key(mem_ctx, name, name_type);
195 if (!gencache_get(key, &value, &timeout)) {
196 DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
200 DEBUG(5, ("name %s#%02X found.\n", name, name_type));
204 * Split up the stored value into the list of IP adresses
206 *num_names = ipstr_list_parse(value, ip_list);
210 return *num_names > 0; /* true only if some ip has been fetched */
215 * Delete single namecache entry. Look at the
216 * gencache_iterate definition.
220 static void flush_netbios_name(const char* key, const char *value, time_t timeout, void* dptr)
223 DEBUG(5, ("Deleting entry %s\n", key));
228 * Flush all names from the name cache.
229 * It's done by gencache_iterate()
231 * @return True upon successful deletion or
232 * False in case of an error
235 void namecache_flush(void)
237 if (!gencache_init())
241 * iterate through each NBT cache's entry and flush it
242 * by flush_netbios_name function
244 gencache_iterate(flush_netbios_name, NULL, "NBT/*");
245 DEBUG(5, ("Namecache flushed\n"));