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
8 Copyright (C) Jeremy Allison 2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define NBTKEY_FMT "NBT/%s#%02X"
29 * Initialise namecache system. Function calls gencache
30 * initialisation function to perform necessary actions
32 * @return true upon successful initialisation of the cache or
36 bool namecache_enable(void)
39 * Check if name caching disabled by setting the name cache
43 if (lp_name_cache_timeout() == 0) {
44 DEBUG(5, ("namecache_enable: disabling netbios name cache\n"));
48 /* Init namecache by calling gencache initialisation */
50 if (!gencache_init()) {
51 DEBUG(2, ("namecache_enable: "
52 "Couldn't initialise namecache on top of gencache.\n"));
56 /* I leave it for now, though I don't think we really
57 * need this (mimir, 27.09.2002) */
58 DEBUG(5, ("namecache_enable: enabling netbios namecache, timeout %d "
59 "seconds\n", lp_name_cache_timeout()));
65 * Generates a key for netbios name lookups on basis of
66 * netbios name and type.
67 * The caller must free returned key string when finished.
69 * @param name netbios name string (case insensitive)
70 * @param name_type netbios type of the name being looked up
72 * @return string consisted of uppercased name and appended
76 static char* namecache_key(const char *name
,
80 asprintf_strupper_m(&keystr
, NBTKEY_FMT
, name
, name_type
);
86 * Store a name(s) in the name cache
88 * @param name netbios names array
89 * @param name_type integer netbios name type
90 * @param num_names number of names being stored
91 * @param ip_list array of in_addr structures containing
92 * ip addresses being stored
95 bool namecache_store(const char *name
,
98 struct ip_service
*ip_list
)
101 char *key
, *value_string
;
106 * we use gecache call to avoid annoying debug messages about
107 * initialised namecache again and again...
109 if (!gencache_init()) {
113 if (name_type
> 255) {
114 return False
; /* Don't store non-real name types. */
117 if ( DEBUGLEVEL
>= 5 ) {
118 TALLOC_CTX
*ctx
= talloc_stackframe();
121 DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
122 num_names
, num_names
== 1 ? "": "es", name
, name_type
));
124 for (i
= 0; i
< num_names
; i
++) {
125 addr
= print_canonical_sockaddr(ctx
,
130 DEBUGADD(5, ("%s%s", addr
,
131 (i
== (num_names
- 1) ? "" : ",")));
138 key
= namecache_key(name
, name_type
);
143 expiry
= time(NULL
) + lp_name_cache_timeout();
146 * Generate string representation of ip addresses list
147 * First, store the number of ip addresses and then
148 * place each single ip
150 if (!ipstr_list_make(&value_string
, ip_list
, num_names
)) {
152 SAFE_FREE(value_string
);
157 ret
= gencache_set(key
, value_string
, expiry
);
159 SAFE_FREE(value_string
);
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(const char *name
,
178 struct ip_service
**ip_list
,
184 /* exit now if null pointers were passed as they're required further */
185 if (!ip_list
|| !num_names
) {
189 if (!gencache_init()) {
193 if (name_type
> 255) {
194 return False
; /* Don't fetch non-real name types. */
200 * Use gencache interface - lookup the key
202 key
= namecache_key(name
, name_type
);
207 if (!gencache_get(key
, &value
, &timeout
)) {
208 DEBUG(5, ("no entry for %s#%02X found.\n", name
, name_type
));
212 DEBUG(5, ("name %s#%02X found.\n", name
, name_type
));
216 * Split up the stored value into the list of IP adresses
218 *num_names
= ipstr_list_parse(value
, ip_list
);
223 return *num_names
> 0; /* true only if some ip has been fetched */
227 * Remove a namecache entry. Needed for site support.
231 bool namecache_delete(const char *name
, int name_type
)
236 if (!gencache_init())
239 if (name_type
> 255) {
240 return False
; /* Don't fetch non-real name types. */
243 key
= namecache_key(name
, name_type
);
247 ret
= gencache_del(key
);
253 * Delete single namecache entry. Look at the
254 * gencache_iterate definition.
258 static void flush_netbios_name(const char *key
,
264 DEBUG(5, ("Deleting entry %s\n", key
));
268 * Flush all names from the name cache.
269 * It's done by gencache_iterate()
271 * @return true upon successful deletion or
272 * false in case of an error
275 void namecache_flush(void)
277 if (!gencache_init()) {
282 * iterate through each NBT cache's entry and flush it
283 * by flush_netbios_name function
285 gencache_iterate(flush_netbios_name
, NULL
, "NBT/*");
286 DEBUG(5, ("Namecache flushed\n"));
289 /* Construct a name status record key. */
291 static char *namecache_status_record_key(const char *name
,
294 const struct sockaddr_storage
*keyip
)
296 char addr
[INET6_ADDRSTRLEN
];
299 print_sockaddr(addr
, sizeof(addr
), keyip
);
300 asprintf_strupper_m(&keystr
, "NBT/%s#%02X.%02X.%s", name
,
301 name_type1
, name_type2
, addr
);
305 /* Store a name status record. */
307 bool namecache_status_store(const char *keyname
, int keyname_type
,
308 int name_type
, const struct sockaddr_storage
*keyip
,
315 if (!gencache_init()) {
319 key
= namecache_status_record_key(keyname
, keyname_type
,
324 expiry
= time(NULL
) + lp_name_cache_timeout();
325 ret
= gencache_set(key
, srvname
, expiry
);
328 DEBUG(5, ("namecache_status_store: entry %s -> %s\n",
331 DEBUG(5, ("namecache_status_store: entry %s store failed.\n",
339 /* Fetch a name status record. */
341 bool namecache_status_fetch(const char *keyname
,
344 const struct sockaddr_storage
*keyip
,
351 if (!gencache_init())
354 key
= namecache_status_record_key(keyname
, keyname_type
,
359 if (!gencache_get(key
, &value
, &timeout
)) {
360 DEBUG(5, ("namecache_status_fetch: no entry for %s found.\n",
365 DEBUG(5, ("namecache_status_fetch: key %s -> %s\n",
369 strlcpy(srvname_out
, value
, 16);