00001 /* 00002 * Copyright (C) 2004-2007, 2009, 2011-2013 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 1999-2001 Internet Software Consortium. 00004 * 00005 * Permission to use, copy, modify, and/or distribute this software for any 00006 * purpose with or without fee is hereby granted, provided that the above 00007 * copyright notice and this permission notice appear in all copies. 00008 * 00009 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00010 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00011 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00012 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00013 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00014 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00015 * PERFORMANCE OF THIS SOFTWARE. 00016 */ 00017 00018 /* $Id: cache.h,v 1.32 2011/08/02 23:47:52 tbox Exp $ */ 00019 00020 #ifndef DNS_CACHE_H 00021 #define DNS_CACHE_H 1 00022 00023 /***** 00024 ***** Module Info 00025 *****/ 00026 00027 /*! \file dns/cache.h 00028 * \brief 00029 * Defines dns_cache_t, the cache object. 00030 * 00031 * Notes: 00032 *\li A cache object contains DNS data of a single class. 00033 * Multiple classes will be handled by creating multiple 00034 * views, each with a different class and its own cache. 00035 * 00036 * MP: 00037 *\li See notes at the individual functions. 00038 * 00039 * Reliability: 00040 * 00041 * Resources: 00042 * 00043 * Security: 00044 * 00045 * Standards: 00046 */ 00047 00048 /*** 00049 *** Imports 00050 ***/ 00051 00052 #include <isc/json.h> 00053 #include <isc/lang.h> 00054 #include <isc/stats.h> 00055 #include <isc/stdtime.h> 00056 00057 #include <dns/types.h> 00058 00059 ISC_LANG_BEGINDECLS 00060 00061 /*** 00062 *** Functions 00063 ***/ 00064 00065 isc_result_t 00066 dns_cache_create(isc_mem_t *cmctx, isc_taskmgr_t *taskmgr, 00067 isc_timermgr_t *timermgr, dns_rdataclass_t rdclass, 00068 const char *db_type, unsigned int db_argc, char **db_argv, 00069 dns_cache_t **cachep); 00070 isc_result_t 00071 dns_cache_create2(isc_mem_t *cmctx, isc_taskmgr_t *taskmgr, 00072 isc_timermgr_t *timermgr, dns_rdataclass_t rdclass, 00073 const char *cachename, const char *db_type, 00074 unsigned int db_argc, char **db_argv, dns_cache_t **cachep); 00075 isc_result_t 00076 dns_cache_create3(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr, 00077 isc_timermgr_t *timermgr, dns_rdataclass_t rdclass, 00078 const char *cachename, const char *db_type, 00079 unsigned int db_argc, char **db_argv, dns_cache_t **cachep); 00080 /*%< 00081 * Create a new DNS cache. 00082 * 00083 * dns_cache_create2() will create a named cache. 00084 * 00085 * dns_cache_create3() will create a named cache using two separate memory 00086 * contexts, one for cache data which can be cleaned and a separate one for 00087 * memory allocated for the heap (which can grow without an upper limit and 00088 * has no mechanism for shrinking). 00089 * 00090 * dns_cache_create() is a backward compatible version that internally 00091 * specifies an empty cache name and a single memory context. 00092 * 00093 * Requires: 00094 * 00095 *\li 'cmctx' (and 'hmctx' if applicable) is a valid memory context. 00096 * 00097 *\li 'taskmgr' is a valid task manager and 'timermgr' is a valid timer 00098 * manager, or both are NULL. If NULL, no periodic cleaning of the 00099 * cache will take place. 00100 * 00101 *\li 'cachename' is a valid string. This must not be NULL. 00102 * 00103 *\li 'cachep' is a valid pointer, and *cachep == NULL 00104 * 00105 * Ensures: 00106 * 00107 *\li '*cachep' is attached to the newly created cache 00108 * 00109 * Returns: 00110 * 00111 *\li #ISC_R_SUCCESS 00112 *\li #ISC_R_NOMEMORY 00113 */ 00114 00115 void 00116 dns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp); 00117 /*%< 00118 * Attach *targetp to cache. 00119 * 00120 * Requires: 00121 * 00122 *\li 'cache' is a valid cache. 00123 * 00124 *\li 'targetp' points to a NULL dns_cache_t *. 00125 * 00126 * Ensures: 00127 * 00128 *\li *targetp is attached to cache. 00129 */ 00130 00131 void 00132 dns_cache_detach(dns_cache_t **cachep); 00133 /*%< 00134 * Detach *cachep from its cache. 00135 * 00136 * Requires: 00137 * 00138 *\li 'cachep' points to a valid cache. 00139 * 00140 * Ensures: 00141 * 00142 *\li *cachep is NULL. 00143 * 00144 *\li If '*cachep' is the last reference to the cache, 00145 * all resources used by the cache will be freed 00146 */ 00147 00148 void 00149 dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp); 00150 /*%< 00151 * Attach *dbp to the cache's database. 00152 * 00153 * Notes: 00154 * 00155 *\li This may be used to get a reference to the database for 00156 * the purpose of cache lookups (XXX currently it is also 00157 * the way to add data to the cache, but having a 00158 * separate dns_cache_add() interface instead would allow 00159 * more control over memory usage). 00160 * The caller should call dns_db_detach() on the reference 00161 * when it is no longer needed. 00162 * 00163 * Requires: 00164 * 00165 *\li 'cache' is a valid cache. 00166 * 00167 *\li 'dbp' points to a NULL dns_db *. 00168 * 00169 * Ensures: 00170 * 00171 *\li *dbp is attached to the database. 00172 */ 00173 00174 00175 isc_result_t 00176 dns_cache_setfilename(dns_cache_t *cache, const char *filename); 00177 /*%< 00178 * If 'filename' is non-NULL, make the cache persistent. 00179 * The cache's data will be stored in the given file. 00180 * If 'filename' is NULL, make the cache non-persistent. 00181 * Files that are no longer used are not unlinked automatically. 00182 * 00183 * Returns: 00184 *\li #ISC_R_SUCCESS 00185 *\li #ISC_R_NOMEMORY 00186 *\li Various file-related failures 00187 */ 00188 00189 isc_result_t 00190 dns_cache_load(dns_cache_t *cache); 00191 /*%< 00192 * If the cache has a file name, load the cache contents from the file. 00193 * Previous cache contents are not discarded. 00194 * If no file name has been set, do nothing and return success. 00195 * 00196 * MT: 00197 *\li Multiple simultaneous attempts to load or dump the cache 00198 * will be serialized with respect to one another, but 00199 * the cache may be read and updated while the dump is 00200 * in progress. Updates performed during loading 00201 * may or may not be preserved, and reads may return 00202 * either the old or the newly loaded data. 00203 * 00204 * Returns: 00205 * 00206 *\li #ISC_R_SUCCESS 00207 * \li Various failures depending on the database implementation type 00208 */ 00209 00210 isc_result_t 00211 dns_cache_dump(dns_cache_t *cache); 00212 /*%< 00213 * If the cache has a file name, write the cache contents to disk, 00214 * overwriting any preexisting file. If no file name has been set, 00215 * do nothing and return success. 00216 * 00217 * MT: 00218 *\li Multiple simultaneous attempts to load or dump the cache 00219 * will be serialized with respect to one another, but 00220 * the cache may be read and updated while the dump is 00221 * in progress. Updates performed during the dump may 00222 * or may not be reflected in the dumped file. 00223 * 00224 * Returns: 00225 * 00226 *\li #ISC_R_SUCCESS 00227 * \li Various failures depending on the database implementation type 00228 */ 00229 00230 isc_result_t 00231 dns_cache_clean(dns_cache_t *cache, isc_stdtime_t now); 00232 /*%< 00233 * Force immediate cleaning of the cache, freeing all rdatasets 00234 * whose TTL has expired as of 'now' and that have no pending 00235 * references. 00236 */ 00237 00238 void 00239 dns_cache_setcleaninginterval(dns_cache_t *cache, unsigned int interval); 00240 /*%< 00241 * Set the periodic cache cleaning interval to 'interval' seconds. 00242 */ 00243 00244 unsigned int 00245 dns_cache_getcleaninginterval(dns_cache_t *cache); 00246 /*%< 00247 * Get the periodic cache cleaning interval to 'interval' seconds. 00248 */ 00249 00250 const char * 00251 dns_cache_getname(dns_cache_t *cache); 00252 /*%< 00253 * Get the cache name. 00254 */ 00255 00256 void 00257 dns_cache_setcachesize(dns_cache_t *cache, size_t size); 00258 /*%< 00259 * Set the maximum cache size. 0 means unlimited. 00260 */ 00261 00262 size_t 00263 dns_cache_getcachesize(dns_cache_t *cache); 00264 /*%< 00265 * Get the maximum cache size. 00266 */ 00267 00268 isc_result_t 00269 dns_cache_flush(dns_cache_t *cache); 00270 /*%< 00271 * Flushes all data from the cache. 00272 * 00273 * Returns: 00274 *\li #ISC_R_SUCCESS 00275 *\li #ISC_R_NOMEMORY 00276 */ 00277 00278 isc_result_t 00279 dns_cache_flushnode(dns_cache_t *cache, dns_name_t *name, 00280 isc_boolean_t tree); 00281 /* 00282 * Flush a given name from the cache. If 'tree' is true, then 00283 * also flush all names under 'name'. 00284 * 00285 * Requires: 00286 *\li 'cache' to be valid. 00287 *\li 'name' to be valid. 00288 * 00289 * Returns: 00290 *\li #ISC_R_SUCCESS 00291 *\li #ISC_R_NOMEMORY 00292 *\li other error returns. 00293 */ 00294 00295 isc_result_t 00296 dns_cache_flushname(dns_cache_t *cache, dns_name_t *name); 00297 /* 00298 * Flush a given name from the cache. Equivalent to 00299 * dns_cache_flushpartial(cache, name, ISC_FALSE). 00300 * 00301 * Requires: 00302 *\li 'cache' to be valid. 00303 *\li 'name' to be valid. 00304 * 00305 * Returns: 00306 *\li #ISC_R_SUCCESS 00307 *\li #ISC_R_NOMEMORY 00308 *\li other error returns. 00309 */ 00310 00311 isc_stats_t * 00312 dns_cache_getstats(dns_cache_t *cache); 00313 /* 00314 * Return a pointer to the stats collection object for 'cache' 00315 */ 00316 00317 void 00318 dns_cache_dumpstats(dns_cache_t *cache, FILE *fp); 00319 /* 00320 * Dump cache statistics and status in text to 'fp' 00321 */ 00322 00323 void 00324 dns_cache_updatestats(dns_cache_t *cache, isc_result_t result); 00325 /* 00326 * Update cache statistics based on result code in 'result' 00327 */ 00328 00329 #ifdef HAVE_LIBXML2 00330 int 00331 dns_cache_renderxml(dns_cache_t *cache, xmlTextWriterPtr writer); 00332 /* 00333 * Render cache statistics and status in XML for 'writer'. 00334 */ 00335 #endif /* HAVE_LIBXML2 */ 00336 00337 #ifdef HAVE_JSON 00338 isc_result_t 00339 dns_cache_renderjson(dns_cache_t *cache, json_object *cstats); 00340 /* 00341 * Render cache statistics and status in JSON 00342 */ 00343 #endif /* HAVE_JSON */ 00344 00345 ISC_LANG_ENDDECLS 00346 00347 #endif /* DNS_CACHE_H */