libsmb/namecache.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003 
00004    NetBIOS name cache module on top of gencache mechanism.
00005    
00006    Copyright (C) Tim Potter         2002
00007    Copyright (C) Rafal Szczesniak   2002
00008    
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013    
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018    
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022 */
00023 
00024 #include "includes.h"
00025 
00026 #define NBTKEY_FMT  "NBT/%s#%02X"
00027 
00028 
00029 /**
00030  * Initialise namecache system. Function calls gencache
00031  * initialisation function to perform necessary actions
00032  * 
00033  * @return true upon successful initialisation of the cache or
00034  *         false on failure
00035  **/
00036 
00037 BOOL namecache_enable(void)
00038 {
00039         /*
00040          * Check if name caching disabled by setting the name cache
00041          * timeout to zero.
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         /* Init namecache by calling gencache initialisation */
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         /* I leave it for now, though I don't think we really need this (mimir, 27.09.2002) */
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  * Shutdown namecache. Routine calls gencache close function
00066  * to safely close gencache file.
00067  *
00068  * @return true upon successful shutdown of the cache or
00069  *         false on failure
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  * Generates a key for netbios name lookups on basis of
00086  * netbios name and type.
00087  * The caller must free returned key string when finished.
00088  *
00089  * @param name netbios name string (case insensitive)
00090  * @param name_type netbios type of the name being looked up
00091  *
00092  * @return string consisted of uppercased name and appended
00093  *         type number
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  * Store a name(s) in the name cache
00107  *
00108  * @param name netbios names array
00109  * @param name_type integer netbios name type
00110  * @param num_names number of names being stored
00111  * @param ip_list array of in_addr structures containing
00112  *        ip addresses being stored
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          * we use gecache call to avoid annoying debug messages about
00125          * initialised namecache again and again...
00126          */
00127         if (!gencache_init()) return False;
00128 
00129         if (name_type > 255) {
00130                 return False; /* Don't store non-real name types. */
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          * Generate string representation of ip addresses list
00153          * First, store the number of ip addresses and then
00154          * place each single ip
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         /* set the entry */
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  * Look up a name in the cache.
00172  *
00173  * @param name netbios name to look up for
00174  * @param name_type netbios name type of @param name
00175  * @param ip_list mallocated list of IP addresses if found in the cache,
00176  *        NULL otherwise
00177  * @param num_names number of entries found
00178  *
00179  * @return true upon successful fetch or
00180  *         false if name isn't found in the cache or has expired
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         /* exit now if null pointers were passed as they're required further */
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; /* Don't fetch non-real name types. */
00197         }
00198 
00199         *num_names = 0;
00200 
00201         /* 
00202          * Use gencache interface - lookup the key
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          * Split up the stored value into the list of IP adresses
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;          /* true only if some ip has been fetched */
00226 }
00227 
00228 /**
00229  * Remove a namecache entry. Needed for site support.
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; /* Don't fetch non-real name types. */
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  * Delete single namecache entry. Look at the
00256  * gencache_iterate definition.
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  * Flush all names from the name cache.
00269  * It's done by gencache_iterate()
00270  *
00271  * @return True upon successful deletion or
00272  *         False in case of an error
00273  **/
00274 
00275 void namecache_flush(void)
00276 {
00277         if (!gencache_init())
00278                 return;
00279 
00280         /* 
00281          * iterate through each NBT cache's entry and flush it
00282          * by flush_netbios_name function
00283          */
00284         gencache_iterate(flush_netbios_name, NULL, "NBT/*");
00285         DEBUG(5, ("Namecache flushed\n"));
00286 }
00287 
00288 /* Construct a name status record key. */
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 /* Store a name status record. */
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 /* Fetch a name status record. */
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 }

Sambaに対してSat Aug 29 21:23:05 2009に生成されました。  doxygen 1.4.7