2 Unix SMB/CIFS implementation.
4 NetBIOS name cache module.
6 Copyright (C) Tim Potter, 2002
8 This program 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 2 of the License, or
11 (at your option) any later version.
13 This program 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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static BOOL done_namecache_init
;
26 static BOOL enable_namecache
;
27 static TDB_CONTEXT
*namecache_tdb
;
30 time_t expiry
; /* When entry expires */
31 int count
; /* Number of addresses */
32 struct in_addr ip_list
[1]; /* Address list */
35 /* Initialise namecache system */
37 void namecache_enable(void)
39 /* Check if we have been here before, or name caching disabled
40 by setting the name cache timeout to zero. */
42 if (done_namecache_init
)
45 done_namecache_init
= True
;
47 if (lp_name_cache_timeout() == 0) {
48 DEBUG(5, ("namecache_init: disabling netbios name cache\n"));
52 /* Open namecache tdb in read/write or readonly mode */
54 namecache_tdb
= tdb_open_log(
55 lock_path("namecache.tdb"), 0,
56 TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
59 DEBUG(5, ("namecache_init: could not open %s\n",
60 lock_path("namecache.tdb")));
64 DEBUG(5, ("namecache_init: enabling netbios namecache, timeout %d "
65 "seconds\n", lp_name_cache_timeout()));
67 enable_namecache
= True
;
70 /* Return a key for a name and name type. The caller must free
71 retval.dptr when finished. */
73 static TDB_DATA
namecache_key(const char *name
, int name_type
)
78 asprintf(&keystr
, "%s#%02X", strupper_static(name
), name_type
);
80 retval
.dsize
= strlen(keystr
) + 1;
86 /* Return a data value for an IP list. The caller must free
87 retval.dptr when finished. */
89 static TDB_DATA
namecache_value(struct in_addr
*ip_list
, int num_names
,
93 struct nc_value
*value
;
96 size
= sizeof(struct nc_value
) + sizeof(struct in_addr
) *
99 value
= (struct nc_value
*)malloc(size
);
101 value
->expiry
= expiry
;
102 value
->count
= num_names
;
104 memcpy(value
->ip_list
, ip_list
, sizeof(*ip_list
));
106 retval
.dptr
= (char *)value
;
112 /* Store a name in the name cache */
114 void namecache_store(const char *name
, int name_type
,
115 int num_names
, struct in_addr
*ip_list
)
121 if (!enable_namecache
)
124 DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
125 num_names
, num_names
== 1 ? "": "es", name
, name_type
));
127 for (i
= 0; i
< num_names
; i
++)
128 DEBUGADD(5, ("%s%s", inet_ntoa(ip_list
[i
]),
129 i
== (num_names
- 1) ? "" : ", "));
133 key
= namecache_key(name
, name_type
);
135 /* Cache pdc location or dc lists for only a little while
136 otherwise if we lock on to a bad DC we can potentially be
137 out of action for the entire cache timeout time! */
139 if (name_type
!= 0x1b || name_type
!= 0x1c)
140 expiry
= time(NULL
) + 10;
142 expiry
= time(NULL
) + lp_name_cache_timeout();
144 value
= namecache_value(ip_list
, num_names
, expiry
);
146 tdb_store(namecache_tdb
, key
, value
, TDB_REPLACE
);
152 /* Look up a name in the name cache. Return a mallocated list of IP
153 addresses if the name is contained in the cache. */
155 BOOL
namecache_fetch(const char *name
, int name_type
, struct in_addr
**ip_list
,
159 struct nc_value
*data
;
163 if (!enable_namecache
)
168 key
= namecache_key(name
, name_type
);
170 value
= tdb_fetch(namecache_tdb
, key
);
173 DEBUG(5, ("namecache_fetch: %s#%02x not found\n",
178 data
= (struct nc_value
*)value
.dptr
;
180 /* Check expiry time */
184 if (now
> data
->expiry
) {
186 DEBUG(5, ("namecache_fetch: entry for %s#%02x expired\n",
189 tdb_delete(namecache_tdb
, key
);
196 if ((data
->expiry
- now
) > lp_name_cache_timeout()) {
198 /* Someone may have changed the system time on us */
200 DEBUG(5, ("namecache_fetch: entry for %s#%02x has bad expiry\n",
203 tdb_delete(namecache_tdb
, key
);
210 /* Extract and return namelist */
212 *ip_list
= (struct in_addr
*)malloc(
213 sizeof(struct in_addr
) * (data
->count
-1));
215 memcpy(*ip_list
, data
->ip_list
, sizeof(struct in_addr
) *
218 *num_names
= data
->count
;
220 DEBUG(5, ("namecache_fetch: returning %d address%s for %s#%02x: ",
221 *num_names
, *num_names
== 1 ? "" : "es", name
, name_type
));
223 for (i
= 0; i
< *num_names
; i
++)
224 DEBUGADD(5, ("%s%s", inet_ntoa((*ip_list
)[i
]),
225 i
== (*num_names
- 1) ? "" : ", "));
231 SAFE_FREE(value
.dptr
);
233 return value
.dsize
> 0;
236 /* Flush all names from the name cache */
238 void namecache_flush(void)
245 result
= tdb_traverse(namecache_tdb
, tdb_traverse_delete_fn
, NULL
);
248 DEBUG(5, ("namecache_flush: error deleting cache entries\n"));
250 DEBUG(5, ("namecache_flush: deleted %d cache entr%s\n",
251 result
, result
== 1 ? "y" : "ies"));