00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "includes.h"
00025
00026 #define NBTKEY_FMT "NBT/%s#%02X"
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 BOOL namecache_enable(void)
00038 {
00039
00040
00041
00042
00043
00044 if (lp_name_cache_timeout() == 0) {
00045 DEBUG(5, ("namecache_enable: disabling netbios name cache\n"));
00046 return False;
00047 }
00048
00049
00050
00051 if (!gencache_init()) {
00052 DEBUG(2, ("namecache_enable: Couldn't initialise namecache on top of gencache.\n"));
00053 return False;
00054 }
00055
00056
00057 DEBUG(5, ("namecache_enable: enabling netbios namecache, timeout %d "
00058 "seconds\n", lp_name_cache_timeout()));
00059
00060 return True;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 BOOL namecache_shutdown(void)
00073 {
00074 if (!gencache_shutdown()) {
00075 DEBUG(2, ("namecache_shutdown: Couldn't close namecache on top of gencache.\n"));
00076 return False;
00077 }
00078
00079 DEBUG(5, ("namecache_shutdown: netbios namecache closed successfully.\n"));
00080 return True;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 static char* namecache_key(const char *name, int name_type)
00097 {
00098 char *keystr;
00099 asprintf(&keystr, NBTKEY_FMT, strupper_static(name), name_type);
00100
00101 return keystr;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 BOOL namecache_store(const char *name, int name_type,
00116 int num_names, struct ip_service *ip_list)
00117 {
00118 time_t expiry;
00119 char *key, *value_string;
00120 int i;
00121 BOOL ret;
00122
00123
00124
00125
00126
00127 if (!gencache_init()) return False;
00128
00129 if (name_type > 255) {
00130 return False;
00131 }
00132
00133 if ( DEBUGLEVEL >= 5 ) {
00134 DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ",
00135 num_names, num_names == 1 ? "": "es", name, name_type));
00136
00137 for (i = 0; i < num_names; i++)
00138 DEBUGADD(5, ("%s:%d%s", inet_ntoa(ip_list[i].ip),
00139 ip_list[i].port, (i == (num_names - 1) ? "" : ",")));
00140
00141 DEBUGADD(5, ("\n"));
00142 }
00143
00144 key = namecache_key(name, name_type);
00145 if (!key) {
00146 return False;
00147 }
00148
00149 expiry = time(NULL) + lp_name_cache_timeout();
00150
00151
00152
00153
00154
00155
00156 if (!ipstr_list_make(&value_string, ip_list, num_names)) {
00157 SAFE_FREE(key);
00158 SAFE_FREE(value_string);
00159 return False;
00160 }
00161
00162
00163 ret = gencache_set(key, value_string, expiry);
00164 SAFE_FREE(key);
00165 SAFE_FREE(value_string);
00166 return ret;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 BOOL namecache_fetch(const char *name, int name_type, struct ip_service **ip_list,
00184 int *num_names)
00185 {
00186 char *key, *value;
00187 time_t timeout;
00188
00189
00190 if (!ip_list || !num_names) return False;
00191
00192 if (!gencache_init())
00193 return False;
00194
00195 if (name_type > 255) {
00196 return False;
00197 }
00198
00199 *num_names = 0;
00200
00201
00202
00203
00204 key = namecache_key(name, name_type);
00205 if (!key) {
00206 return False;
00207 }
00208
00209 if (!gencache_get(key, &value, &timeout)) {
00210 DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type));
00211 SAFE_FREE(key);
00212 return False;
00213 } else {
00214 DEBUG(5, ("name %s#%02X found.\n", name, name_type));
00215 }
00216
00217
00218
00219
00220 *num_names = ipstr_list_parse(value, ip_list);
00221
00222 SAFE_FREE(key);
00223 SAFE_FREE(value);
00224
00225 return *num_names > 0;
00226 }
00227
00228
00229
00230
00231
00232
00233 BOOL namecache_delete(const char *name, int name_type)
00234 {
00235 BOOL ret;
00236 char *key;
00237
00238 if (!gencache_init())
00239 return False;
00240
00241 if (name_type > 255) {
00242 return False;
00243 }
00244
00245 key = namecache_key(name, name_type);
00246 if (!key) {
00247 return False;
00248 }
00249 ret = gencache_del(key);
00250 SAFE_FREE(key);
00251 return ret;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 static void flush_netbios_name(const char* key, const char *value, time_t timeout, void* dptr)
00261 {
00262 gencache_del(key);
00263 DEBUG(5, ("Deleting entry %s\n", key));
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 void namecache_flush(void)
00276 {
00277 if (!gencache_init())
00278 return;
00279
00280
00281
00282
00283
00284 gencache_iterate(flush_netbios_name, NULL, "NBT/*");
00285 DEBUG(5, ("Namecache flushed\n"));
00286 }
00287
00288
00289
00290 static char *namecache_status_record_key(const char *name, int name_type1,
00291 int name_type2, struct in_addr keyip)
00292 {
00293 char *keystr;
00294
00295 asprintf(&keystr, "NBT/%s#%02X.%02X.%s",
00296 strupper_static(name), name_type1, name_type2, inet_ntoa(keyip));
00297 return keystr;
00298 }
00299
00300
00301
00302 BOOL namecache_status_store(const char *keyname, int keyname_type,
00303 int name_type, struct in_addr keyip,
00304 const char *srvname)
00305 {
00306 char *key;
00307 time_t expiry;
00308 BOOL ret;
00309
00310 if (!gencache_init())
00311 return False;
00312
00313 key = namecache_status_record_key(keyname, keyname_type, name_type, keyip);
00314 if (!key)
00315 return False;
00316
00317 expiry = time(NULL) + lp_name_cache_timeout();
00318 ret = gencache_set(key, srvname, expiry);
00319
00320 if (ret)
00321 DEBUG(5, ("namecache_status_store: entry %s -> %s\n", key, srvname ));
00322 else
00323 DEBUG(5, ("namecache_status_store: entry %s store failed.\n", key ));
00324
00325 SAFE_FREE(key);
00326 return ret;
00327 }
00328
00329
00330
00331 BOOL namecache_status_fetch(const char *keyname, int keyname_type,
00332 int name_type, struct in_addr keyip, char *srvname_out)
00333 {
00334 char *key = NULL;
00335 char *value = NULL;
00336 time_t timeout;
00337
00338 if (!gencache_init())
00339 return False;
00340
00341 key = namecache_status_record_key(keyname, keyname_type, name_type, keyip);
00342 if (!key)
00343 return False;
00344
00345 if (!gencache_get(key, &value, &timeout)) {
00346 DEBUG(5, ("namecache_status_fetch: no entry for %s found.\n", key));
00347 SAFE_FREE(key);
00348 return False;
00349 } else {
00350 DEBUG(5, ("namecache_status_fetch: key %s -> %s\n", key, value ));
00351 }
00352
00353 strlcpy(srvname_out, value, 16);
00354 SAFE_FREE(key);
00355 SAFE_FREE(value);
00356 return True;
00357 }