2 * Copyright (c) 1993 The Regents of the University of Michigan.
5 * cache.c - local caching support for LDAP
11 static char copyright[] = "@(#) Copyright (c) 1993 The Regents of the University of Michigan.\nAll rights reserved.\n";
22 #if defined( DOS ) || defined( _WIN32 )
32 #include <sys/types.h>
33 #include <sys/socket.h>
42 static int cache_hash( BerElement *ber );
43 static LDAPMessage *msg_dup( LDAPMessage *msg );
44 static int request_cmp( BerElement *req1, BerElement *req2 );
45 static int chain_contains_dn( LDAPMessage *msg, char *dn );
46 static long msg_size( LDAPMessage *msg );
47 static void check_cache_memused( LDAPCache *lc );
48 static void uncache_entry_or_req( LDAP *ld, char *dn, int msgid );
49 #else /* NEEDPROTOS */
50 static int cache_hash();
51 static LDAPMessage *msg_dup();
52 static int request_cmp();
53 static int chain_contains_dn();
54 static long msg_size();
55 static void check_cache_memused();
56 static void uncache_entry_or_req();
57 #endif /* NEEDPROTOS */
61 ldap_enable_cache( LDAP *ld, long timeout, long maxmem )
63 if ( ld->ld_cache == NULLLDCACHE ) {
64 if (( ld->ld_cache = (LDAPCache *)malloc( sizeof( LDAPCache )))
66 ld->ld_errno = LDAP_NO_MEMORY;
69 (void) memset( ld->ld_cache, 0, sizeof( LDAPCache ));
70 ld->ld_cache->lc_memused = sizeof( LDAPCache );
73 ld->ld_cache->lc_timeout = timeout;
74 ld->ld_cache->lc_maxmem = maxmem;
75 check_cache_memused( ld->ld_cache );
76 ld->ld_cache->lc_enabled = 1;
82 ldap_disable_cache( LDAP *ld )
84 if ( ld->ld_cache != NULLLDCACHE ) {
85 ld->ld_cache->lc_enabled = 0;
92 ldap_set_cache_options( LDAP *ld, unsigned long opts )
94 if ( ld->ld_cache != NULLLDCACHE ) {
95 ld->ld_cache->lc_options = opts;
101 ldap_destroy_cache( LDAP *ld )
103 if ( ld->ld_cache != NULLLDCACHE ) {
104 ldap_flush_cache( ld );
105 free( (char *)ld->ld_cache );
106 ld->ld_cache = NULLLDCACHE;
112 ldap_flush_cache( LDAP *ld )
115 LDAPMessage *m, *next;
117 Debug( LDAP_DEBUG_TRACE, "ldap_flush_cache\n", 0, 0, 0 );
119 if ( ld->ld_cache != NULLLDCACHE ) {
120 /* delete all requests in the queue */
121 for ( m = ld->ld_cache->lc_requests; m != NULLMSG; m = next ) {
125 ld->ld_cache->lc_requests = NULLMSG;
127 /* delete all messages in the cache */
128 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
129 for ( m = ld->ld_cache->lc_buckets[ i ];
130 m != NULLMSG; m = next ) {
134 ld->ld_cache->lc_buckets[ i ] = NULLMSG;
136 ld->ld_cache->lc_memused = sizeof( LDAPCache );
142 ldap_uncache_request( LDAP *ld, int msgid )
144 Debug( LDAP_DEBUG_TRACE, "ldap_uncache_request %d ld_cache %lx\n",
145 msgid, (long) ld->ld_cache, 0 );
147 uncache_entry_or_req( ld, NULL, msgid );
152 ldap_uncache_entry( LDAP *ld, char *dn )
154 Debug( LDAP_DEBUG_TRACE, "ldap_uncache_entry %s ld_cache %lx\n",
155 dn, (long) ld->ld_cache, 0 );
157 uncache_entry_or_req( ld, dn, 0 );
162 uncache_entry_or_req( LDAP *ld,
163 char *dn, /* if non-NULL, uncache entry */
164 int msgid ) /* request to uncache (if dn == NULL) */
167 LDAPMessage *m, *prev, *next;
169 Debug( LDAP_DEBUG_TRACE,
170 "ldap_uncache_entry_or_req dn %s msgid %d ld_cache %lx\n",
171 dn, msgid, (long) ld->ld_cache );
173 if ( ld->ld_cache == NULLLDCACHE ) {
177 /* first check the request queue */
179 for ( m = ld->ld_cache->lc_requests; m != NULLMSG; m = next ) {
181 if (( dn != NULL && chain_contains_dn( m, dn )) ||
182 ( dn == NULL && m->lm_msgid == msgid )) {
183 if ( prev == NULLMSG ) {
184 ld->ld_cache->lc_requests = next;
186 prev->lm_next = next;
188 ld->ld_cache->lc_memused -= msg_size( m );
195 /* now check the rest of the cache */
196 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
198 for ( m = ld->ld_cache->lc_buckets[ i ]; m != NULLMSG;
201 if (( dn != NULL && chain_contains_dn( m, dn )) ||
202 ( dn == NULL && m->lm_msgid == msgid )) {
203 if ( prev == NULLMSG ) {
204 ld->ld_cache->lc_buckets[ i ] = next;
206 prev->lm_next = next;
208 ld->ld_cache->lc_memused -= msg_size( m );
219 ldap_add_request_to_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
224 Debug( LDAP_DEBUG_TRACE, "ldap_add_request_to_cache\n", 0, 0, 0 );
226 ld->ld_errno = LDAP_SUCCESS;
227 if ( ld->ld_cache == NULLLDCACHE ||
228 ( ld->ld_cache->lc_enabled == 0 )) {
232 if (( new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
234 if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
238 len = request->ber_ptr - request->ber_buf;
239 if (( new->lm_ber->ber_buf = (char *) malloc( (size_t)len ))
241 ber_free( new->lm_ber, 0 );
243 ld->ld_errno = LDAP_NO_MEMORY;
246 SAFEMEMCPY( new->lm_ber->ber_buf, request->ber_buf,
248 new->lm_ber->ber_ptr = new->lm_ber->ber_buf;
249 new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
250 new->lm_msgid = ld->ld_msgid;
251 new->lm_msgtype = msgtype;;
252 new->lm_next = ld->ld_cache->lc_requests;
253 ld->ld_cache->lc_requests = new;
255 ld->ld_errno = LDAP_NO_MEMORY;
261 ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
263 LDAPMessage *m, **mp, *req, *new, *prev;
266 Debug( LDAP_DEBUG_TRACE, "ldap_add_result_to_cache: id %d, type %d\n",
267 result->lm_msgid, result->lm_msgtype, 0 );
269 if ( ld->ld_cache == NULLLDCACHE ||
270 ( ld->ld_cache->lc_enabled == 0 )) {
271 Debug( LDAP_DEBUG_TRACE, "artc: cache disabled\n", 0, 0, 0 );
275 if ( result->lm_msgtype != LDAP_RES_SEARCH_ENTRY &&
276 result->lm_msgtype != LDAP_RES_SEARCH_RESULT &&
277 result->lm_msgtype != LDAP_RES_COMPARE ) {
279 * only cache search and compare operations
281 Debug( LDAP_DEBUG_TRACE,
282 "artc: only caching search & compare operations\n", 0, 0, 0 );
287 * if corresponding request is in the lc_requests list, add this
288 * result to it. if this result completes the results for the
289 * request, add the request/result chain to the cache proper.
292 for ( m = ld->ld_cache->lc_requests; m != NULL; m = m->lm_next ) {
293 if ( m->lm_msgid == result->lm_msgid ) {
299 if ( m != NULLMSG ) { /* found request; add to end of chain */
301 for ( ; m->lm_chain != NULLMSG; m = m->lm_chain )
303 if (( new = msg_dup( result )) != NULLMSG ) {
304 new->lm_chain = NULLMSG;
306 Debug( LDAP_DEBUG_TRACE,
307 "artc: result added to cache request chain\n",
310 if ( result->lm_msgtype == LDAP_RES_SEARCH_RESULT ||
311 result->lm_msgtype == LDAP_RES_COMPARE ) {
313 * this result completes the chain of results
314 * add to cache proper if appropriate
316 keep = 0; /* pessimistic */
317 err = ldap_result2error( ld, result, 0 );
318 if ( err == LDAP_SUCCESS ||
319 ( result->lm_msgtype == LDAP_RES_COMPARE &&
320 ( err == LDAP_COMPARE_FALSE ||
321 err == LDAP_COMPARE_TRUE ||
322 err == LDAP_NO_SUCH_ATTRIBUTE ))) {
326 if ( ld->ld_cache->lc_options == 0 ) {
327 if ( err == LDAP_SIZELIMIT_EXCEEDED ) {
330 } else if (( ld->ld_cache->lc_options &
331 LDAP_CACHE_OPT_CACHEALLERRS ) != 0 ) {
335 if ( prev == NULLMSG ) {
336 ld->ld_cache->lc_requests = req->lm_next;
338 prev->lm_next = req->lm_next;
342 Debug( LDAP_DEBUG_TRACE,
343 "artc: not caching result with error %d\n",
347 mp = &ld->ld_cache->lc_buckets[
348 cache_hash( req->lm_ber ) ];
351 req->lm_time = (long) time( NULL );
352 ld->ld_cache->lc_memused += msg_size( req );
353 check_cache_memused( ld->ld_cache );
354 Debug( LDAP_DEBUG_TRACE,
355 "artc: cached result with error %d\n",
360 Debug( LDAP_DEBUG_TRACE, "artc: msgid not in request list\n",
367 * look in the cache for this request
368 * return 0 if found, -1 if not
369 * if found, the corresponding result messages are added to the incoming
370 * queue with the correct (new) msgid so that subsequent ldap_result calls
374 ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
376 LDAPMessage *m, *new, *prev, *next;
379 unsigned long validtime;
381 Debug( LDAP_DEBUG_TRACE, "ldap_check_cache\n", 0, 0, 0 );
383 if ( ld->ld_cache == NULLLDCACHE ||
384 ( ld->ld_cache->lc_enabled == 0 )) {
388 reqber.ber_buf = reqber.ber_ptr = request->ber_buf;
389 reqber.ber_end = request->ber_ptr;
391 validtime = (long)time( NULL ) - ld->ld_cache->lc_timeout;
394 hash = cache_hash( &reqber );
395 for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULLMSG; m = next ) {
396 Debug( LDAP_DEBUG_TRACE,"cc: examining id %d,type %d\n",
397 m->lm_msgid, m->lm_msgtype, 0 );
398 if ( m->lm_time < validtime ) {
399 /* delete expired message */
401 if ( prev == NULL ) {
402 ld->ld_cache->lc_buckets[ hash ] = next;
404 prev->lm_next = next;
406 Debug( LDAP_DEBUG_TRACE, "cc: expired id %d\n",
408 ld->ld_cache->lc_memused -= msg_size( m );
411 if ( m->lm_msgtype == (int)msgtype &&
412 request_cmp( m->lm_ber, &reqber ) == 0 ) {
420 if ( m == NULLMSG ) {
425 * add duplicates of responses to incoming queue
428 for ( m = m->lm_chain; m != NULLMSG; m = m->lm_chain ) {
429 if (( new = msg_dup( m )) == NULLMSG ) {
433 new->lm_msgid = ld->ld_msgid;
434 new->lm_chain = NULLMSG;
436 new->lm_next = ld->ld_responses;
437 ld->ld_responses = new;
440 prev->lm_chain = new;
443 Debug( LDAP_DEBUG_TRACE, "cc: added type %d\n",
444 new->lm_msgtype, 0, 0 );
447 Debug( LDAP_DEBUG_TRACE, "cc: result returned from cache\n", 0, 0, 0 );
453 cache_hash( BerElement *ber )
459 * just take the length of the packet and mod with # of buckets
462 if ( ber_skip_tag( &bercpy, &len ) == LBER_ERROR
463 || ber_scanf( &bercpy, "x" ) == LBER_ERROR ) {
464 len = 0; /* punt: just return zero */
466 len = bercpy.ber_end - bercpy.ber_ptr;
469 Debug( LDAP_DEBUG_TRACE, "cache_hash: len is %ld, returning %ld\n",
470 len, len % LDAP_CACHE_BUCKETS, 0 );
471 return( (int) ( len % LDAP_CACHE_BUCKETS ));
476 msg_dup( LDAPMessage *msg )
481 if (( new = (LDAPMessage *)malloc( sizeof(LDAPMessage))) != NULL ) {
482 *new = *msg; /* struct copy */
483 if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULLBER ) {
487 len = msg->lm_ber->ber_end - msg->lm_ber->ber_buf;
488 if (( new->lm_ber->ber_buf = (char *) malloc(
489 (size_t)len )) == NULL ) {
490 ber_free( new->lm_ber, 0 );
494 SAFEMEMCPY( new->lm_ber->ber_buf, msg->lm_ber->ber_buf,
496 new->lm_ber->ber_ptr = new->lm_ber->ber_buf +
497 ( msg->lm_ber->ber_ptr - msg->lm_ber->ber_buf );
498 new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
506 request_cmp( BerElement *req1, BerElement *req2 )
511 r1 = *req1; /* struct copies */
515 * skip the enclosing tags (sequence markers) and the msg ids
517 if ( ber_skip_tag( &r1, &len ) == LBER_ERROR || ber_scanf( &r1, "x" )
521 if ( ber_skip_tag( &r2, &len ) == LBER_ERROR || ber_scanf( &r2, "x" )
527 * check remaining length and bytes if necessary
529 if (( len = r1.ber_end - r1.ber_ptr ) != r2.ber_end - r2.ber_ptr ) {
530 return( -1 ); /* different lengths */
532 return( memcmp( r1.ber_ptr, r2.ber_ptr, (size_t)len ));
537 chain_contains_dn( LDAPMessage *msg, char *dn )
547 * first check the base or dn of the request
549 ber = *msg->lm_ber; /* struct copy */
550 if ( ber_scanf( &ber, "{i{a", &msgid, &s ) != LBER_ERROR ) {
551 rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
558 if ( msg->lm_msgtype == LDAP_REQ_COMPARE ) {
563 * now check the dn of each search result
566 for ( m = msg->lm_chain; m != NULLMSG && rc == 0 ; m = m->lm_chain ) {
567 if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
570 ber = *m->lm_ber; /* struct copy */
571 if ( ber_scanf( &ber, "{a", &s ) != LBER_ERROR ) {
572 rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
582 msg_size( LDAPMessage *msg )
588 for ( m = msg; m != NULLMSG; m = m->lm_chain ) {
589 size += ( sizeof( LDAPMessage ) + m->lm_ber->ber_end -
590 m->lm_ber->ber_buf );
597 #define THRESHOLD_FACTOR 3 / 4
598 #define SIZE_FACTOR 2 / 3
601 check_cache_memused( LDAPCache *lc )
604 * this routine is called to check if the cache is too big (lc_maxmem >
605 * minimum cache size and lc_memused > lc_maxmem). If too big, it reduces
606 * the cache size to < SIZE_FACTOR * lc_maxmem. The algorithm is as follows:
607 * remove_threshold = lc_timeout seconds;
609 * remove everything older than remove_threshold seconds;
610 * remove_threshold = remove_threshold * THRESHOLD_FACTOR;
611 * } while ( cache size is > SIZE_FACTOR * lc_maxmem )
614 unsigned long remove_threshold, validtime;
615 LDAPMessage *m, *prev, *next;
617 Debug( LDAP_DEBUG_TRACE, "check_cache_memused: %ld bytes in use (%ld max)\n",
618 lc->lc_memused, lc->lc_maxmem, 0 );
620 if ( lc->lc_maxmem <= sizeof( LDAPCache )
621 || lc->lc_memused <= lc->lc_maxmem * SIZE_FACTOR ) {
625 remove_threshold = lc->lc_timeout;
626 while ( lc->lc_memused > lc->lc_maxmem * SIZE_FACTOR ) {
627 validtime = (long)time( NULL ) - remove_threshold;
628 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
630 for ( m = lc->lc_buckets[ i ]; m != NULLMSG;
633 if ( m->lm_time < validtime ) {
634 if ( prev == NULLMSG ) {
635 lc->lc_buckets[ i ] = next;
637 prev->lm_next = next;
639 lc->lc_memused -= msg_size( m );
640 Debug( LDAP_DEBUG_TRACE,
649 remove_threshold *= THRESHOLD_FACTOR;
652 Debug( LDAP_DEBUG_TRACE, "ccm: reduced usage to %ld bytes\n",
653 lc->lc_memused, 0, 0 );
656 #endif /* !NO_CACHE */