openldap/libraries/libldap/cache.c

666 lines
15 KiB
C
Raw Normal View History

/* $OpenLDAP$ */
1998-08-09 08:43:13 +08:00
/*
1998-12-29 04:53:15 +08:00
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
/* Portions
1998-08-09 08:43:13 +08:00
* Copyright (c) 1993 The Regents of the University of Michigan.
* All rights reserved.
*
* cache.c - local caching support for LDAP
*/
1998-10-25 09:41:42 +08:00
#include "portable.h"
1998-08-09 08:43:13 +08:00
#include <stdio.h>
1999-06-03 08:37:44 +08:00
#include <ac/stdlib.h>
1998-08-21 03:42:38 +08:00
1998-10-25 09:41:42 +08:00
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
1998-08-21 03:42:38 +08:00
1998-08-09 08:43:13 +08:00
#include "ldap-int.h"
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
static int cache_hash LDAP_P(( BerElement *ber ));
static LDAPMessage *msg_dup LDAP_P(( LDAPMessage *msg ));
static int request_cmp LDAP_P(( BerElement *req1, BerElement *req2 ));
Vienna Bulk Commit This commit includes many changes. All changes compile under NT but have not been tested under UNIX. A Summary of changes (likely incomplete): NT changes: Removed lint. Clean up configuration support for "Debug", "Release", "SDebug", and "SRelease" configurations. Share output directories for clients, libraries, and slapd. (maybe they should be combined further and moved to build/{,S}{Debug,Release}). Enable threading when _MT is defined. Enable debuging when _DEBUG is defined. Disable setting of NDEBUG under Release/SRelease. Asserts are disabled in <ac/assert.h> when LDAP_DEBUG is not defined. Added 'build/main.dsp' Master project. Removed non-slapd projects from slapd.dsp (see main.dsp). Removed replaced many uses of _WIN32 macro with feature based macros. ldap_cdefs.h changes #define LDAP_CONST const (see below) #define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST To allow specifiers to be added before and after the type declaration. (For DLL handling) LBER/LDAP changes Namespace changes: s/lber_/ber_/ for here and there. s/NAME_ERROR/LDAP_NAME_ERROR/g Deleted NULLMSG and other NULL* macros for namespace reasons. "const" libraries. Installed headers (ie: lber.h, ldap.h) use LDAP_CONST macro. Normally set to 'const' when __STDC__. Can be set externally to enable/disable 'constification' of external interface. Internal interface always uses 'const'. Did not fix warnings in -lldif (in lieu of new LDIF parser). Added _ext API implementations (excepting search and bind). Need to implement ldap_int_get_controls() for reponses with controls. Added numberous assert() checks. LDAP_R _MT defines HAVE_NT_THREADS Added numberous assert() checks. Changed ldap_pthread_t back to unsigned long. Used cast to HANDLE in _join(). LDBM Replaced _WIN32 with HAVE_SYSLOG ud Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). slapd Made connection sockbuf field a pointer to a sockbuf. This removed slap.h dependency on lber-int.h. lber-int.h now only included by those files needing to mess with the sockbuf. Used ber_* functions/macros to access sockbuf internals whenever possible. Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). Removed FD_SET unsigned lint slapd/tools Used EXEEXT to added ".exe" to routines. Need to define EXEEXT under UNIX. ldappasswd Added ldappasswd.dsp. Ported to NT. Used getpid() to seed rand(). nt_debug Minor cleanup. Added "portable.h" include and used <ac/*.h> where appropriate. Added const to char* format argument.
1999-05-19 09:12:33 +08:00
static int chain_contains_dn LDAP_P(( LDAPMessage *msg, LDAP_CONST char *dn ));
static ber_len_t msg_size LDAP_P(( LDAPMessage *msg ));
1998-10-25 09:41:42 +08:00
static void check_cache_memused LDAP_P(( LDAPCache *lc ));
static void uncache_entry_or_req LDAP_P(( LDAP *ld, LDAP_CONST char *dn, ber_int_t msgid ));
1998-08-09 08:43:13 +08:00
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
int
ldap_enable_cache( LDAP *ld, long timeout, ber_len_t maxmem )
1998-08-09 08:43:13 +08:00
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
if ( ld->ld_cache == NULL ) {
if (( ld->ld_cache = (LDAPCache *)LDAP_MALLOC( sizeof( LDAPCache )))
== NULL ) {
1998-08-09 08:43:13 +08:00
ld->ld_errno = LDAP_NO_MEMORY;
return( -1 );
}
(void) memset( ld->ld_cache, 0, sizeof( LDAPCache ));
ld->ld_cache->lc_memused = sizeof( LDAPCache );
}
ld->ld_cache->lc_timeout = timeout;
ld->ld_cache->lc_maxmem = maxmem;
check_cache_memused( ld->ld_cache );
ld->ld_cache->lc_enabled = 1;
return( 0 );
1998-10-25 09:41:42 +08:00
#else
return( -1 );
#endif
1998-08-09 08:43:13 +08:00
}
void
ldap_disable_cache( LDAP *ld )
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
if ( ld->ld_cache != NULL ) {
1998-08-09 08:43:13 +08:00
ld->ld_cache->lc_enabled = 0;
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
void
ldap_set_cache_options( LDAP *ld, unsigned long opts )
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
if ( ld->ld_cache != NULL ) {
1998-08-09 08:43:13 +08:00
ld->ld_cache->lc_options = opts;
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
void
ldap_destroy_cache( LDAP *ld )
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
if ( ld->ld_cache != NULL ) {
1998-08-09 08:43:13 +08:00
ldap_flush_cache( ld );
LDAP_FREE( (char *)ld->ld_cache );
ld->ld_cache = NULL;
1998-08-09 08:43:13 +08:00
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
void
ldap_flush_cache( LDAP *ld )
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-09 08:43:13 +08:00
int i;
LDAPMessage *m, *next;
Debug( LDAP_DEBUG_TRACE, "ldap_flush_cache\n", 0, 0, 0 );
if ( ld->ld_cache != NULL ) {
1998-08-09 08:43:13 +08:00
/* delete all requests in the queue */
for ( m = ld->ld_cache->lc_requests; m != NULL; m = next ) {
1998-08-09 08:43:13 +08:00
next = m->lm_next;
ldap_msgfree( m );
}
ld->ld_cache->lc_requests = NULL;
1998-08-09 08:43:13 +08:00
/* delete all messages in the cache */
for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
for ( m = ld->ld_cache->lc_buckets[ i ];
m != NULL; m = next ) {
1998-08-09 08:43:13 +08:00
next = m->lm_next;
ldap_msgfree( m );
}
ld->ld_cache->lc_buckets[ i ] = NULL;
1998-08-09 08:43:13 +08:00
}
ld->ld_cache->lc_memused = sizeof( LDAPCache );
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
void
ldap_uncache_request( LDAP *ld, int msgid )
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-21 03:42:38 +08:00
Debug( LDAP_DEBUG_TRACE, "ldap_uncache_request %d ld_cache %lx\n",
msgid, (long) ld->ld_cache, 0 );
1998-08-09 08:43:13 +08:00
uncache_entry_or_req( ld, NULL, msgid );
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
void
Vienna Bulk Commit This commit includes many changes. All changes compile under NT but have not been tested under UNIX. A Summary of changes (likely incomplete): NT changes: Removed lint. Clean up configuration support for "Debug", "Release", "SDebug", and "SRelease" configurations. Share output directories for clients, libraries, and slapd. (maybe they should be combined further and moved to build/{,S}{Debug,Release}). Enable threading when _MT is defined. Enable debuging when _DEBUG is defined. Disable setting of NDEBUG under Release/SRelease. Asserts are disabled in <ac/assert.h> when LDAP_DEBUG is not defined. Added 'build/main.dsp' Master project. Removed non-slapd projects from slapd.dsp (see main.dsp). Removed replaced many uses of _WIN32 macro with feature based macros. ldap_cdefs.h changes #define LDAP_CONST const (see below) #define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST To allow specifiers to be added before and after the type declaration. (For DLL handling) LBER/LDAP changes Namespace changes: s/lber_/ber_/ for here and there. s/NAME_ERROR/LDAP_NAME_ERROR/g Deleted NULLMSG and other NULL* macros for namespace reasons. "const" libraries. Installed headers (ie: lber.h, ldap.h) use LDAP_CONST macro. Normally set to 'const' when __STDC__. Can be set externally to enable/disable 'constification' of external interface. Internal interface always uses 'const'. Did not fix warnings in -lldif (in lieu of new LDIF parser). Added _ext API implementations (excepting search and bind). Need to implement ldap_int_get_controls() for reponses with controls. Added numberous assert() checks. LDAP_R _MT defines HAVE_NT_THREADS Added numberous assert() checks. Changed ldap_pthread_t back to unsigned long. Used cast to HANDLE in _join(). LDBM Replaced _WIN32 with HAVE_SYSLOG ud Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). slapd Made connection sockbuf field a pointer to a sockbuf. This removed slap.h dependency on lber-int.h. lber-int.h now only included by those files needing to mess with the sockbuf. Used ber_* functions/macros to access sockbuf internals whenever possible. Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). Removed FD_SET unsigned lint slapd/tools Used EXEEXT to added ".exe" to routines. Need to define EXEEXT under UNIX. ldappasswd Added ldappasswd.dsp. Ported to NT. Used getpid() to seed rand(). nt_debug Minor cleanup. Added "portable.h" include and used <ac/*.h> where appropriate. Added const to char* format argument.
1999-05-19 09:12:33 +08:00
ldap_uncache_entry( LDAP *ld, LDAP_CONST char *dn )
1998-08-09 08:43:13 +08:00
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-21 03:42:38 +08:00
Debug( LDAP_DEBUG_TRACE, "ldap_uncache_entry %s ld_cache %lx\n",
dn, (long) ld->ld_cache, 0 );
1998-08-09 08:43:13 +08:00
uncache_entry_or_req( ld, dn, 0 );
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-09 08:43:13 +08:00
static void
uncache_entry_or_req( LDAP *ld,
Vienna Bulk Commit This commit includes many changes. All changes compile under NT but have not been tested under UNIX. A Summary of changes (likely incomplete): NT changes: Removed lint. Clean up configuration support for "Debug", "Release", "SDebug", and "SRelease" configurations. Share output directories for clients, libraries, and slapd. (maybe they should be combined further and moved to build/{,S}{Debug,Release}). Enable threading when _MT is defined. Enable debuging when _DEBUG is defined. Disable setting of NDEBUG under Release/SRelease. Asserts are disabled in <ac/assert.h> when LDAP_DEBUG is not defined. Added 'build/main.dsp' Master project. Removed non-slapd projects from slapd.dsp (see main.dsp). Removed replaced many uses of _WIN32 macro with feature based macros. ldap_cdefs.h changes #define LDAP_CONST const (see below) #define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST To allow specifiers to be added before and after the type declaration. (For DLL handling) LBER/LDAP changes Namespace changes: s/lber_/ber_/ for here and there. s/NAME_ERROR/LDAP_NAME_ERROR/g Deleted NULLMSG and other NULL* macros for namespace reasons. "const" libraries. Installed headers (ie: lber.h, ldap.h) use LDAP_CONST macro. Normally set to 'const' when __STDC__. Can be set externally to enable/disable 'constification' of external interface. Internal interface always uses 'const'. Did not fix warnings in -lldif (in lieu of new LDIF parser). Added _ext API implementations (excepting search and bind). Need to implement ldap_int_get_controls() for reponses with controls. Added numberous assert() checks. LDAP_R _MT defines HAVE_NT_THREADS Added numberous assert() checks. Changed ldap_pthread_t back to unsigned long. Used cast to HANDLE in _join(). LDBM Replaced _WIN32 with HAVE_SYSLOG ud Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). slapd Made connection sockbuf field a pointer to a sockbuf. This removed slap.h dependency on lber-int.h. lber-int.h now only included by those files needing to mess with the sockbuf. Used ber_* functions/macros to access sockbuf internals whenever possible. Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). Removed FD_SET unsigned lint slapd/tools Used EXEEXT to added ".exe" to routines. Need to define EXEEXT under UNIX. ldappasswd Added ldappasswd.dsp. Ported to NT. Used getpid() to seed rand(). nt_debug Minor cleanup. Added "portable.h" include and used <ac/*.h> where appropriate. Added const to char* format argument.
1999-05-19 09:12:33 +08:00
const char *dn, /* if non-NULL, uncache entry */
ber_int_t msgid ) /* request to uncache (if dn == NULL) */
1998-08-09 08:43:13 +08:00
{
int i;
LDAPMessage *m, *prev, *next;
Debug( LDAP_DEBUG_TRACE,
"ldap_uncache_entry_or_req dn %s msgid %ld ld_cache %lx\n",
dn, (long) msgid, (long) ld->ld_cache );
1998-08-09 08:43:13 +08:00
if ( ld->ld_cache == NULL ) {
1998-08-09 08:43:13 +08:00
return;
}
/* first check the request queue */
prev = NULL;
for ( m = ld->ld_cache->lc_requests; m != NULL; m = next ) {
1998-08-09 08:43:13 +08:00
next = m->lm_next;
if (( dn != NULL && chain_contains_dn( m, dn )) ||
( dn == NULL && m->lm_msgid == msgid )) {
if ( prev == NULL ) {
1998-08-09 08:43:13 +08:00
ld->ld_cache->lc_requests = next;
} else {
prev->lm_next = next;
}
ld->ld_cache->lc_memused -= msg_size( m );
ldap_msgfree( m );
} else {
prev = m;
}
}
/* now check the rest of the cache */
for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
prev = NULL;
for ( m = ld->ld_cache->lc_buckets[ i ]; m != NULL;
1998-08-09 08:43:13 +08:00
m = next ) {
next = m->lm_next;
if (( dn != NULL && chain_contains_dn( m, dn )) ||
( dn == NULL && m->lm_msgid == msgid )) {
if ( prev == NULL ) {
1998-08-09 08:43:13 +08:00
ld->ld_cache->lc_buckets[ i ] = next;
} else {
prev->lm_next = next;
}
ld->ld_cache->lc_memused -= msg_size( m );
ldap_msgfree( m );
} else {
prev = m;
}
}
}
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
void
ldap_add_request_to_cache( LDAP *ld, ber_tag_t msgtype, BerElement *request )
1998-08-09 08:43:13 +08:00
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-09 08:43:13 +08:00
LDAPMessage *new;
ber_len_t len;
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "ldap_add_request_to_cache\n", 0, 0, 0 );
1998-08-09 08:43:13 +08:00
ld->ld_errno = LDAP_SUCCESS;
if ( ld->ld_cache == NULL ||
1998-08-09 08:43:13 +08:00
( ld->ld_cache->lc_enabled == 0 )) {
return;
}
if (( new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
1998-08-09 08:43:13 +08:00
!= NULL ) {
if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
LDAP_FREE( (char *)new );
1998-08-09 08:43:13 +08:00
return;
}
len = request->ber_ptr - request->ber_buf;
if (( new->lm_ber->ber_buf = (char *) ber_memalloc( (size_t)len ))
1998-08-09 08:43:13 +08:00
== NULL ) {
ber_free( new->lm_ber, 0 );
LDAP_FREE( (char *)new );
1998-08-09 08:43:13 +08:00
ld->ld_errno = LDAP_NO_MEMORY;
return;
}
SAFEMEMCPY( new->lm_ber->ber_buf, request->ber_buf,
(size_t)len );
new->lm_ber->ber_ptr = new->lm_ber->ber_buf;
new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
new->lm_msgid = ld->ld_msgid;
new->lm_msgtype = msgtype;;
new->lm_next = ld->ld_cache->lc_requests;
ld->ld_cache->lc_requests = new;
} else {
ld->ld_errno = LDAP_NO_MEMORY;
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
void
ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
1998-08-09 08:43:13 +08:00
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-09 08:43:13 +08:00
LDAPMessage *m, **mp, *req, *new, *prev;
int err, keep;
Debug( LDAP_DEBUG_TRACE, "ldap_add_result_to_cache: id %ld, type %ld\n",
(long) result->lm_msgid, (long) result->lm_msgtype, 0 );
1998-08-09 08:43:13 +08:00
if ( ld->ld_cache == NULL ||
1998-08-09 08:43:13 +08:00
( ld->ld_cache->lc_enabled == 0 )) {
Debug( LDAP_DEBUG_TRACE, "artc: cache disabled\n", 0, 0, 0 );
return;
}
if ( result->lm_msgtype != LDAP_RES_SEARCH_ENTRY &&
result->lm_msgtype != LDAP_RES_SEARCH_REFERENCE &&
1998-08-09 08:43:13 +08:00
result->lm_msgtype != LDAP_RES_SEARCH_RESULT &&
result->lm_msgtype != LDAP_RES_COMPARE ) {
/*
* only cache search and compare operations
*/
Debug( LDAP_DEBUG_TRACE,
"artc: only caching search & compare operations\n", 0, 0, 0 );
return;
}
/*
* if corresponding request is in the lc_requests list, add this
* result to it. if this result completes the results for the
* request, add the request/result chain to the cache proper.
*/
prev = NULL;
1998-08-09 08:43:13 +08:00
for ( m = ld->ld_cache->lc_requests; m != NULL; m = m->lm_next ) {
if ( m->lm_msgid == result->lm_msgid ) {
break;
}
prev = m;
}
if ( m != NULL ) { /* found request; add to end of chain */
1998-08-09 08:43:13 +08:00
req = m;
for ( ; m->lm_chain != NULL; m = m->lm_chain )
1998-08-09 08:43:13 +08:00
;
if (( new = msg_dup( result )) != NULL ) {
new->lm_chain = NULL;
1998-08-09 08:43:13 +08:00
m->lm_chain = new;
Debug( LDAP_DEBUG_TRACE,
"artc: result added to cache request chain\n",
0, 0, 0 );
}
if ( result->lm_msgtype == LDAP_RES_SEARCH_RESULT ||
result->lm_msgtype == LDAP_RES_COMPARE ) {
/*
* this result completes the chain of results
* add to cache proper if appropriate
*/
keep = 0; /* pessimistic */
err = ldap_result2error( ld, result, 0 );
if ( err == LDAP_SUCCESS ||
( result->lm_msgtype == LDAP_RES_COMPARE &&
( err == LDAP_COMPARE_FALSE ||
err == LDAP_COMPARE_TRUE ||
err == LDAP_NO_SUCH_ATTRIBUTE ))) {
keep = 1;
}
if ( ld->ld_cache->lc_options == 0 ) {
if ( err == LDAP_SIZELIMIT_EXCEEDED ) {
keep = 1;
}
} else if (( ld->ld_cache->lc_options &
LDAP_CACHE_OPT_CACHEALLERRS ) != 0 ) {
keep = 1;
}
if ( prev == NULL ) {
1998-08-09 08:43:13 +08:00
ld->ld_cache->lc_requests = req->lm_next;
} else {
prev->lm_next = req->lm_next;
}
if ( !keep ) {
Debug( LDAP_DEBUG_TRACE,
"artc: not caching result with error %d\n",
err, 0, 0 );
ldap_msgfree( req );
} else {
mp = &ld->ld_cache->lc_buckets[
cache_hash( req->lm_ber ) ];
req->lm_next = *mp;
*mp = req;
req->lm_time = (long) time( NULL );
ld->ld_cache->lc_memused += msg_size( req );
check_cache_memused( ld->ld_cache );
Debug( LDAP_DEBUG_TRACE,
"artc: cached result with error %d\n",
err, 0, 0 );
}
}
} else {
Debug( LDAP_DEBUG_TRACE, "artc: msgid not in request list\n",
0, 0, 0 );
}
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
}
/*
* look in the cache for this request
* return 0 if found, -1 if not
* if found, the corresponding result messages are added to the incoming
* queue with the correct (new) msgid so that subsequent ldap_result calls
* will find them.
*/
int
ldap_check_cache( LDAP *ld, ber_tag_t msgtype, BerElement *request )
1998-08-09 08:43:13 +08:00
{
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-09 08:43:13 +08:00
LDAPMessage *m, *new, *prev, *next;
BerElement reqber;
int first, hash;
1999-05-28 06:48:14 +08:00
time_t c_time;
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "ldap_check_cache\n", 0, 0, 0 );
1998-08-09 08:43:13 +08:00
if ( ld->ld_cache == NULL ||
1998-08-09 08:43:13 +08:00
( ld->ld_cache->lc_enabled == 0 )) {
return( -1 );
}
reqber.ber_buf = reqber.ber_ptr = request->ber_buf;
reqber.ber_end = request->ber_ptr;
1999-05-28 06:48:14 +08:00
c_time = time( NULL );
1998-08-09 08:43:13 +08:00
prev = NULL;
1998-08-09 08:43:13 +08:00
hash = cache_hash( &reqber );
for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULL; m = next ) {
Debug( LDAP_DEBUG_TRACE,"cc: examining id %ld,type %ld\n",
(long) m->lm_msgid, (long) m->lm_msgtype, 0 );
1999-05-28 06:48:14 +08:00
if ( difftime(c_time, m->lm_time) > ld->ld_cache->lc_timeout ) {
1998-08-09 08:43:13 +08:00
/* delete expired message */
next = m->lm_next;
if ( prev == NULL ) {
ld->ld_cache->lc_buckets[ hash ] = next;
} else {
prev->lm_next = next;
}
Debug( LDAP_DEBUG_TRACE, "cc: expired id %d\n",
m->lm_msgid, 0, 0 );
ld->ld_cache->lc_memused -= msg_size( m );
ldap_msgfree( m );
} else {
if ( m->lm_msgtype == (int)msgtype &&
request_cmp( m->lm_ber, &reqber ) == 0 ) {
break;
}
next = m->lm_next;
prev = m;
}
}
if ( m == NULL ) {
1998-08-09 08:43:13 +08:00
return( -1 );
}
/*
* add duplicates of responses to incoming queue
*/
first = 1;
for ( m = m->lm_chain; m != NULL; m = m->lm_chain ) {
if (( new = msg_dup( m )) == NULL ) {
1998-08-09 08:43:13 +08:00
return( -1 );
}
new->lm_msgid = ld->ld_msgid;
new->lm_chain = NULL;
1998-08-09 08:43:13 +08:00
if ( first ) {
new->lm_next = ld->ld_responses;
ld->ld_responses = new;
first = 0;
} else {
prev->lm_chain = new;
}
prev = new;
Debug( LDAP_DEBUG_TRACE, "cc: added type %ld\n",
(long) new->lm_msgtype, 0, 0 );
1998-08-09 08:43:13 +08:00
}
Debug( LDAP_DEBUG_TRACE, "cc: result returned from cache\n", 0, 0, 0 );
return( 0 );
1998-10-25 09:41:42 +08:00
#else
return( -1 );
#endif
1998-08-09 08:43:13 +08:00
}
1998-10-25 09:41:42 +08:00
#ifndef LDAP_NOCACHE
1998-08-09 08:43:13 +08:00
static int
cache_hash( BerElement *ber )
{
BerElement bercpy;
ber_len_t len;
1998-08-09 08:43:13 +08:00
/*
* just take the length of the packet and mod with # of buckets
*/
bercpy = *ber;
if ( ber_skip_tag( &bercpy, &len ) == LBER_ERROR
|| ber_scanf( &bercpy, "x" ) == LBER_ERROR ) {
len = 0; /* punt: just return zero */
} else {
len = bercpy.ber_end - bercpy.ber_ptr;
}
Debug( LDAP_DEBUG_TRACE, "cache_hash: len is %ld, returning %ld\n",
len, len % LDAP_CACHE_BUCKETS, 0 );
return( (int) ( len % LDAP_CACHE_BUCKETS ));
}
static LDAPMessage *
msg_dup( LDAPMessage *msg )
{
LDAPMessage *new;
ber_len_t len;
1998-08-09 08:43:13 +08:00
if (( new = (LDAPMessage *)LDAP_MALLOC( sizeof(LDAPMessage))) != NULL ) {
1998-08-09 08:43:13 +08:00
*new = *msg; /* struct copy */
if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULL ) {
LDAP_FREE( (char *)new );
return( NULL );
1998-08-09 08:43:13 +08:00
}
len = msg->lm_ber->ber_end - msg->lm_ber->ber_buf;
if (( new->lm_ber->ber_buf = (char *) ber_memalloc(
1998-08-09 08:43:13 +08:00
(size_t)len )) == NULL ) {
ber_free( new->lm_ber, 0 );
LDAP_FREE( (char *)new );
return( NULL );
1998-08-09 08:43:13 +08:00
}
SAFEMEMCPY( new->lm_ber->ber_buf, msg->lm_ber->ber_buf,
(size_t)len );
new->lm_ber->ber_ptr = new->lm_ber->ber_buf +
( msg->lm_ber->ber_ptr - msg->lm_ber->ber_buf );
new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
}
return( new );
}
static int
request_cmp( BerElement *req1, BerElement *req2 )
{
ber_len_t len;
1998-08-09 08:43:13 +08:00
BerElement r1, r2;
r1 = *req1; /* struct copies */
r2 = *req2;
/*
* skip the enclosing tags (sequence markers) and the msg ids
*/
if ( ber_skip_tag( &r1, &len ) == LBER_ERROR || ber_scanf( &r1, "x" )
== LBER_ERROR ) {
return( -1 );
}
if ( ber_skip_tag( &r2, &len ) == LBER_ERROR || ber_scanf( &r2, "x" )
== LBER_ERROR ) {
return( -1 );
}
/*
* check remaining length and bytes if necessary
*/
1998-10-25 09:41:42 +08:00
if (( len = r1.ber_end - r1.ber_ptr ) !=
(ber_len_t) (r2.ber_end - r2.ber_ptr) )
1998-10-25 09:41:42 +08:00
{
1998-08-09 08:43:13 +08:00
return( -1 ); /* different lengths */
}
return( memcmp( r1.ber_ptr, r2.ber_ptr, (size_t)len ));
}
static int
Vienna Bulk Commit This commit includes many changes. All changes compile under NT but have not been tested under UNIX. A Summary of changes (likely incomplete): NT changes: Removed lint. Clean up configuration support for "Debug", "Release", "SDebug", and "SRelease" configurations. Share output directories for clients, libraries, and slapd. (maybe they should be combined further and moved to build/{,S}{Debug,Release}). Enable threading when _MT is defined. Enable debuging when _DEBUG is defined. Disable setting of NDEBUG under Release/SRelease. Asserts are disabled in <ac/assert.h> when LDAP_DEBUG is not defined. Added 'build/main.dsp' Master project. Removed non-slapd projects from slapd.dsp (see main.dsp). Removed replaced many uses of _WIN32 macro with feature based macros. ldap_cdefs.h changes #define LDAP_CONST const (see below) #define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST To allow specifiers to be added before and after the type declaration. (For DLL handling) LBER/LDAP changes Namespace changes: s/lber_/ber_/ for here and there. s/NAME_ERROR/LDAP_NAME_ERROR/g Deleted NULLMSG and other NULL* macros for namespace reasons. "const" libraries. Installed headers (ie: lber.h, ldap.h) use LDAP_CONST macro. Normally set to 'const' when __STDC__. Can be set externally to enable/disable 'constification' of external interface. Internal interface always uses 'const'. Did not fix warnings in -lldif (in lieu of new LDIF parser). Added _ext API implementations (excepting search and bind). Need to implement ldap_int_get_controls() for reponses with controls. Added numberous assert() checks. LDAP_R _MT defines HAVE_NT_THREADS Added numberous assert() checks. Changed ldap_pthread_t back to unsigned long. Used cast to HANDLE in _join(). LDBM Replaced _WIN32 with HAVE_SYSLOG ud Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). slapd Made connection sockbuf field a pointer to a sockbuf. This removed slap.h dependency on lber-int.h. lber-int.h now only included by those files needing to mess with the sockbuf. Used ber_* functions/macros to access sockbuf internals whenever possible. Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). Removed FD_SET unsigned lint slapd/tools Used EXEEXT to added ".exe" to routines. Need to define EXEEXT under UNIX. ldappasswd Added ldappasswd.dsp. Ported to NT. Used getpid() to seed rand(). nt_debug Minor cleanup. Added "portable.h" include and used <ac/*.h> where appropriate. Added const to char* format argument.
1999-05-19 09:12:33 +08:00
chain_contains_dn( LDAPMessage *msg, const char *dn )
1998-08-09 08:43:13 +08:00
{
LDAPMessage *m;
BerElement ber;
ber_int_t msgid;
1998-08-09 08:43:13 +08:00
char *s;
int rc;
/*
* first check the base or dn of the request
*/
ber = *msg->lm_ber; /* struct copy */
if ( ber_scanf( &ber, "{i{a" /*}}*/, &msgid, &s ) != LBER_ERROR ) {
1998-08-09 08:43:13 +08:00
rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
LDAP_FREE( s );
1998-08-09 08:43:13 +08:00
if ( rc != 0 ) {
return( rc );
}
}
if ( msg->lm_msgtype == LDAP_REQ_COMPARE ) {
return( 0 );
}
/*
* now check the dn of each search result
*/
rc = 0;
for ( m = msg->lm_chain; m != NULL && rc == 0 ; m = m->lm_chain ) {
1998-08-09 08:43:13 +08:00
if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
continue;
}
ber = *m->lm_ber; /* struct copy */
if ( ber_scanf( &ber, "{a" /*}*/, &s ) != LBER_ERROR ) {
1998-08-09 08:43:13 +08:00
rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
LDAP_FREE( s );
1998-08-09 08:43:13 +08:00
}
}
return( rc );
}
static ber_len_t
1998-08-09 08:43:13 +08:00
msg_size( LDAPMessage *msg )
{
LDAPMessage *m;
ber_len_t size;
1998-08-09 08:43:13 +08:00
size = 0;
for ( m = msg; m != NULL; m = m->lm_chain ) {
1998-08-09 08:43:13 +08:00
size += ( sizeof( LDAPMessage ) + m->lm_ber->ber_end -
m->lm_ber->ber_buf );
}
return( size );
}
#define THRESHOLD_FACTOR 3 / 4
#define SIZE_FACTOR 2 / 3
static void
check_cache_memused( LDAPCache *lc )
{
/*
* this routine is called to check if the cache is too big (lc_maxmem >
* minimum cache size and lc_memused > lc_maxmem). If too big, it reduces
* the cache size to < SIZE_FACTOR * lc_maxmem. The algorithm is as follows:
* remove_threshold = lc_timeout seconds;
* do {
* remove everything older than remove_threshold seconds;
* remove_threshold = remove_threshold * THRESHOLD_FACTOR;
* } while ( cache size is > SIZE_FACTOR * lc_maxmem )
*/
int i;
1999-05-28 06:48:14 +08:00
unsigned long remove_threshold;
time_t c_time;
1998-08-09 08:43:13 +08:00
LDAPMessage *m, *prev, *next;
Debug( LDAP_DEBUG_TRACE, "check_cache_memused: %ld bytes in use (%ld max)\n",
lc->lc_memused, lc->lc_maxmem, 0 );
1999-04-27 13:19:32 +08:00
if ( (unsigned) lc->lc_maxmem <= sizeof( LDAPCache )
1998-08-09 08:43:13 +08:00
|| lc->lc_memused <= lc->lc_maxmem * SIZE_FACTOR ) {
return;
}
remove_threshold = lc->lc_timeout;
while ( lc->lc_memused > lc->lc_maxmem * SIZE_FACTOR ) {
1999-05-28 06:48:14 +08:00
c_time = time( NULL );
1998-08-09 08:43:13 +08:00
for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
prev = NULL;
for ( m = lc->lc_buckets[ i ]; m != NULL;
1998-08-09 08:43:13 +08:00
m = next ) {
next = m->lm_next;
1999-05-28 06:48:14 +08:00
if ( difftime(c_time, m->lm_time) > remove_threshold) {
if ( prev == NULL ) {
1998-08-09 08:43:13 +08:00
lc->lc_buckets[ i ] = next;
} else {
prev->lm_next = next;
}
lc->lc_memused -= msg_size( m );
Debug( LDAP_DEBUG_TRACE,
"ccm: removed %d\n",
m->lm_msgid, 0, 0 );
ldap_msgfree( m );
} else {
prev = m;
}
}
}
remove_threshold *= THRESHOLD_FACTOR;
}
Debug( LDAP_DEBUG_TRACE, "ccm: reduced usage to %ld bytes\n",
lc->lc_memused, 0, 0 );
}
#endif /* !NO_CACHE */