xref: /titanic_41/usr/src/lib/libldap5/sources/ldap/common/memcache.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
6*7c478bd9Sstevel@tonic-gate 
7*7c478bd9Sstevel@tonic-gate /*
8*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
9*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
10*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
11*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
14*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
16*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
17*7c478bd9Sstevel@tonic-gate  *
18*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
19*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
20*7c478bd9Sstevel@tonic-gate  *
21*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
22*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
23*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
24*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * Contributor(s):
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  *  memcache.c - routines that implement an in-memory cache.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  *  To Do:  1) ber_dup_ext().
33*7c478bd9Sstevel@tonic-gate  *	    2) referrals and reference?
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <assert.h>
37*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * Extra size allocated to BerElement.
41*7c478bd9Sstevel@tonic-gate  * XXXmcs: must match EXBUFSIZ in liblber/io.c?
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate #define EXTRA_SIZE		    1024
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /* Mode constants for function memcache_access() */
46*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_ADD	    0
47*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_APPEND	    1
48*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_APPEND_LAST 2
49*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_FIND	    3
50*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_DELETE	    4
51*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_DELETE_ALL  5
52*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_UPDATE	    6
53*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_FLUSH	    7
54*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_FLUSH_ALL   8
55*7c478bd9Sstevel@tonic-gate #define MEMCACHE_ACCESS_FLUSH_LRU   9
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /* Mode constants for function memcache_adj_size */
58*7c478bd9Sstevel@tonic-gate #define MEMCACHE_SIZE_DEDUCT	    0
59*7c478bd9Sstevel@tonic-gate #define MEMCACHE_SIZE_ADD	    1
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #define MEMCACHE_SIZE_ENTRIES       1
62*7c478bd9Sstevel@tonic-gate #define MEMCACHE_SIZE_NON_ENTRIES   2
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /* Size used for calculation if given size of cache is 0 */
65*7c478bd9Sstevel@tonic-gate #define MEMCACHE_DEF_SIZE	    131072		/* 128K bytes */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /* Index into different list structure */
68*7c478bd9Sstevel@tonic-gate #define LIST_TTL		    0
69*7c478bd9Sstevel@tonic-gate #define LIST_LRU		    1
70*7c478bd9Sstevel@tonic-gate #define LIST_TMP		    2
71*7c478bd9Sstevel@tonic-gate #define LIST_TOTAL		    3
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static char *emptyStr = "";
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /* Macros to make code more readable */
77*7c478bd9Sstevel@tonic-gate #define NSLDAPI_VALID_MEMCACHE_POINTER( cp )	( (cp) != NULL )
78*7c478bd9Sstevel@tonic-gate #define NSLDAPI_STR_NONNULL( s )		( (s) ? (s) : emptyStr )
79*7c478bd9Sstevel@tonic-gate #define NSLDAPI_SAFE_STRLEN( s )		( (s) ? strlen((s)) + 1 : 1 )
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /* Macros dealing with mutex */
82*7c478bd9Sstevel@tonic-gate #define LDAP_MEMCACHE_MUTEX_LOCK( c ) \
83*7c478bd9Sstevel@tonic-gate 	if ( (c) && ((c)->ldmemc_lock_fns).ltf_mutex_lock ) { \
84*7c478bd9Sstevel@tonic-gate 	    ((c)->ldmemc_lock_fns).ltf_mutex_lock( (c)->ldmemc_lock ); \
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate #define LDAP_MEMCACHE_MUTEX_UNLOCK( c ) \
88*7c478bd9Sstevel@tonic-gate 	if ( (c) && ((c)->ldmemc_lock_fns).ltf_mutex_unlock ) { \
89*7c478bd9Sstevel@tonic-gate 	    ((c)->ldmemc_lock_fns).ltf_mutex_unlock( (c)->ldmemc_lock ); \
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate #define LDAP_MEMCACHE_MUTEX_ALLOC( c ) \
93*7c478bd9Sstevel@tonic-gate 	((c) && ((c)->ldmemc_lock_fns).ltf_mutex_alloc ? \
94*7c478bd9Sstevel@tonic-gate 	    ((c)->ldmemc_lock_fns).ltf_mutex_alloc() : NULL)
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define LDAP_MEMCACHE_MUTEX_FREE( c ) \
97*7c478bd9Sstevel@tonic-gate 	if ( (c) && ((c)->ldmemc_lock_fns).ltf_mutex_free ) { \
98*7c478bd9Sstevel@tonic-gate 	    ((c)->ldmemc_lock_fns).ltf_mutex_free( (c)->ldmemc_lock ); \
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /* Macros used for triming unnecessary spaces in a basedn */
102*7c478bd9Sstevel@tonic-gate #define NSLDAPI_IS_SPACE( c ) \
103*7c478bd9Sstevel@tonic-gate 	(((c) == ' ') || ((c) == '\t') || ((c) == '\n'))
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate #define NSLDAPI_IS_SEPARATER( c ) \
106*7c478bd9Sstevel@tonic-gate 	((c) == ',')
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /* Hash table callback function pointer definition */
109*7c478bd9Sstevel@tonic-gate typedef int (*HashFuncPtr)(int table_size, void *key);
110*7c478bd9Sstevel@tonic-gate typedef int (*PutDataPtr)(void **ppTableData, void *key, void *pData);
111*7c478bd9Sstevel@tonic-gate typedef int (*GetDataPtr)(void *pTableData, void *key, void **ppData);
112*7c478bd9Sstevel@tonic-gate typedef int (*RemoveDataPtr)(void **ppTableData, void *key, void **ppData);
113*7c478bd9Sstevel@tonic-gate typedef int (*MiscFuncPtr)(void **ppTableData, void *key, void *pData);
114*7c478bd9Sstevel@tonic-gate typedef void (*ClrTableNodePtr)(void **ppTableData, void *pData);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate /* Structure of a node in a hash table */
117*7c478bd9Sstevel@tonic-gate typedef struct HashTableNode_struct {
118*7c478bd9Sstevel@tonic-gate     void *pData;
119*7c478bd9Sstevel@tonic-gate } HashTableNode;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /* Structure of a hash table */
122*7c478bd9Sstevel@tonic-gate typedef struct HashTable_struct {
123*7c478bd9Sstevel@tonic-gate     HashTableNode   *table;
124*7c478bd9Sstevel@tonic-gate     int		    size;
125*7c478bd9Sstevel@tonic-gate     HashFuncPtr	    hashfunc;
126*7c478bd9Sstevel@tonic-gate     PutDataPtr	    putdata;
127*7c478bd9Sstevel@tonic-gate     GetDataPtr	    getdata;
128*7c478bd9Sstevel@tonic-gate     MiscFuncPtr     miscfunc;
129*7c478bd9Sstevel@tonic-gate     RemoveDataPtr   removedata;
130*7c478bd9Sstevel@tonic-gate     ClrTableNodePtr clrtablenode;
131*7c478bd9Sstevel@tonic-gate } HashTable;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /* Structure uniquely identifies a search request */
134*7c478bd9Sstevel@tonic-gate typedef struct ldapmemcacheReqId_struct {
135*7c478bd9Sstevel@tonic-gate     LDAP				*ldmemcrid_ld;
136*7c478bd9Sstevel@tonic-gate     int					ldmemcrid_msgid;
137*7c478bd9Sstevel@tonic-gate } ldapmemcacheReqId;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /* Structure representing a ldap handle associated to memcache */
140*7c478bd9Sstevel@tonic-gate typedef struct ldapmemcacheld_struct {
141*7c478bd9Sstevel@tonic-gate     LDAP		    		*ldmemcl_ld;
142*7c478bd9Sstevel@tonic-gate     struct ldapmemcacheld_struct	*ldmemcl_next;
143*7c478bd9Sstevel@tonic-gate } ldapmemcacheld;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /* Structure representing header of a search result */
146*7c478bd9Sstevel@tonic-gate typedef struct ldapmemcacheRes_struct {
147*7c478bd9Sstevel@tonic-gate     char				*ldmemcr_basedn;
148*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemcr_crc_key;
149*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemcr_resSize;
150*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemcr_timestamp;
151*7c478bd9Sstevel@tonic-gate     LDAPMessage				*ldmemcr_resHead;
152*7c478bd9Sstevel@tonic-gate     LDAPMessage				*ldmemcr_resTail;
153*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId			ldmemcr_req_id;
154*7c478bd9Sstevel@tonic-gate     struct ldapmemcacheRes_struct	*ldmemcr_next[LIST_TOTAL];
155*7c478bd9Sstevel@tonic-gate     struct ldapmemcacheRes_struct	*ldmemcr_prev[LIST_TOTAL];
156*7c478bd9Sstevel@tonic-gate     struct ldapmemcacheRes_struct	*ldmemcr_htable_next;
157*7c478bd9Sstevel@tonic-gate } ldapmemcacheRes;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate /* Structure for memcache statistics */
160*7c478bd9Sstevel@tonic-gate typedef struct ldapmemcacheStats_struct {
161*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemcstat_tries;
162*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemcstat_hits;
163*7c478bd9Sstevel@tonic-gate } ldapmemcacheStats;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate /* Structure of a memcache object */
166*7c478bd9Sstevel@tonic-gate struct ldapmemcache {
167*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemc_ttl;
168*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemc_size;
169*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemc_size_used;
170*7c478bd9Sstevel@tonic-gate     unsigned long			ldmemc_size_entries;
171*7c478bd9Sstevel@tonic-gate     char				**ldmemc_basedns;
172*7c478bd9Sstevel@tonic-gate     void				*ldmemc_lock;
173*7c478bd9Sstevel@tonic-gate     ldapmemcacheld			*ldmemc_lds;
174*7c478bd9Sstevel@tonic-gate     HashTable				*ldmemc_resTmp;
175*7c478bd9Sstevel@tonic-gate     HashTable				*ldmemc_resLookup;
176*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes			*ldmemc_resHead[LIST_TOTAL];
177*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes			*ldmemc_resTail[LIST_TOTAL];
178*7c478bd9Sstevel@tonic-gate     struct ldap_thread_fns		ldmemc_lock_fns;
179*7c478bd9Sstevel@tonic-gate     ldapmemcacheStats			ldmemc_stats;
180*7c478bd9Sstevel@tonic-gate };
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /* Function prototypes */
183*7c478bd9Sstevel@tonic-gate static int memcache_exist(LDAP *ld);
184*7c478bd9Sstevel@tonic-gate static int memcache_add_to_ld(LDAP *ld, int msgid, LDAPMessage *pMsg);
185*7c478bd9Sstevel@tonic-gate static int memcache_compare_dn(const char *main_dn, const char *dn, int scope);
186*7c478bd9Sstevel@tonic-gate static int memcache_dup_message(LDAPMessage *res, int msgid, int fromcache,
187*7c478bd9Sstevel@tonic-gate 				LDAPMessage **ppResCopy, unsigned long *pSize);
188*7c478bd9Sstevel@tonic-gate static BerElement* memcache_ber_dup(BerElement* pBer, unsigned long *pSize);
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate static void memcache_trim_basedn_spaces(char *basedn);
191*7c478bd9Sstevel@tonic-gate static int memcache_validate_basedn(LDAPMemCache *cache, const char *basedn);
192*7c478bd9Sstevel@tonic-gate static int memcache_get_ctrls_len(LDAPControl **ctrls);
193*7c478bd9Sstevel@tonic-gate static void memcache_append_ctrls(char *buf, LDAPControl **serverCtrls,
194*7c478bd9Sstevel@tonic-gate 				  LDAPControl **clientCtrls);
195*7c478bd9Sstevel@tonic-gate static int memcache_adj_size(LDAPMemCache *cache, unsigned long size,
196*7c478bd9Sstevel@tonic-gate                              int usageFlags, int bAdd);
197*7c478bd9Sstevel@tonic-gate static int memcache_free_entry(LDAPMemCache *cache, ldapmemcacheRes *pRes);
198*7c478bd9Sstevel@tonic-gate static int memcache_expired(LDAPMemCache *cache, ldapmemcacheRes *pRes,
199*7c478bd9Sstevel@tonic-gate 			    unsigned long curTime);
200*7c478bd9Sstevel@tonic-gate static int memcache_add_to_list(LDAPMemCache *cache, ldapmemcacheRes *pRes,
201*7c478bd9Sstevel@tonic-gate 				int index);
202*7c478bd9Sstevel@tonic-gate static int memcache_add_res_to_list(ldapmemcacheRes *pRes, LDAPMessage *pMsg,
203*7c478bd9Sstevel@tonic-gate 				    unsigned long size);
204*7c478bd9Sstevel@tonic-gate static int memcache_free_from_list(LDAPMemCache *cache, ldapmemcacheRes *pRes,
205*7c478bd9Sstevel@tonic-gate 				   int index);
206*7c478bd9Sstevel@tonic-gate static int memcache_search(LDAP *ld, unsigned long key, LDAPMessage **ppRes);
207*7c478bd9Sstevel@tonic-gate static int memcache_add(LDAP *ld, unsigned long key, int msgid,
208*7c478bd9Sstevel@tonic-gate 			const char *basedn);
209*7c478bd9Sstevel@tonic-gate static int memcache_append(LDAP *ld, int msgid, LDAPMessage *pRes);
210*7c478bd9Sstevel@tonic-gate static int memcache_append_last(LDAP *ld, int msgid, LDAPMessage *pRes);
211*7c478bd9Sstevel@tonic-gate static int memcache_remove(LDAP *ld, int msgid);
212*7c478bd9Sstevel@tonic-gate #if 0	/* function not used */
213*7c478bd9Sstevel@tonic-gate static int memcache_remove_all(LDAP *ld);
214*7c478bd9Sstevel@tonic-gate #endif /* 0 */
215*7c478bd9Sstevel@tonic-gate static int memcache_access(LDAPMemCache *cache, int mode,
216*7c478bd9Sstevel@tonic-gate 			   void *pData1, void *pData2, void *pData3);
217*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
218*7c478bd9Sstevel@tonic-gate static void memcache_print_list( LDAPMemCache *cache, int index );
219*7c478bd9Sstevel@tonic-gate static void memcache_report_statistics( LDAPMemCache *cache );
220*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate static int htable_calculate_size(int sizelimit);
223*7c478bd9Sstevel@tonic-gate static int htable_sizeinbytes(HashTable *pTable);
224*7c478bd9Sstevel@tonic-gate static int htable_put(HashTable *pTable, void *key, void *pData);
225*7c478bd9Sstevel@tonic-gate static int htable_get(HashTable *pTable, void *key, void **ppData);
226*7c478bd9Sstevel@tonic-gate static int htable_misc(HashTable *pTable, void *key, void *pData);
227*7c478bd9Sstevel@tonic-gate static int htable_remove(HashTable *pTable, void *key, void **ppData);
228*7c478bd9Sstevel@tonic-gate static int htable_removeall(HashTable *pTable, void *pData);
229*7c478bd9Sstevel@tonic-gate static int htable_create(int size_limit, HashFuncPtr hashf,
230*7c478bd9Sstevel@tonic-gate                          PutDataPtr putDataf, GetDataPtr getDataf,
231*7c478bd9Sstevel@tonic-gate 			 RemoveDataPtr removeDataf, ClrTableNodePtr clrNodef,
232*7c478bd9Sstevel@tonic-gate 			 MiscFuncPtr miscOpf, HashTable **ppTable);
233*7c478bd9Sstevel@tonic-gate static int htable_free(HashTable *pTable);
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate static int msgid_hashf(int table_size, void *key);
236*7c478bd9Sstevel@tonic-gate static int msgid_putdata(void **ppTableData, void *key, void *pData);
237*7c478bd9Sstevel@tonic-gate static int msgid_getdata(void *pTableData, void *key, void **ppData);
238*7c478bd9Sstevel@tonic-gate static int msgid_removedata(void **ppTableData, void *key, void **ppData);
239*7c478bd9Sstevel@tonic-gate static int msgid_clear_ld_items(void **ppTableData, void *key, void *pData);
240*7c478bd9Sstevel@tonic-gate static void msgid_clearnode(void **ppTableData, void *pData);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate static int attrkey_hashf(int table_size, void *key);
243*7c478bd9Sstevel@tonic-gate static int attrkey_putdata(void **ppTableData, void *key, void *pData);
244*7c478bd9Sstevel@tonic-gate static int attrkey_getdata(void *pTableData, void *key, void **ppData);
245*7c478bd9Sstevel@tonic-gate static int attrkey_removedata(void **ppTableData, void *key, void **ppData);
246*7c478bd9Sstevel@tonic-gate static void attrkey_clearnode(void **ppTableData, void *pData);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate static unsigned long crc32_convert(char *buf, int len);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /* Create a memcache object. */
251*7c478bd9Sstevel@tonic-gate int
252*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_memcache_init(unsigned long ttl,unsigned long size,char ** baseDNs,struct ldap_thread_fns * thread_fns,LDAPMemCache ** cachep)253*7c478bd9Sstevel@tonic-gate ldap_memcache_init( unsigned long ttl, unsigned long size,
254*7c478bd9Sstevel@tonic-gate                     char **baseDNs, struct ldap_thread_fns *thread_fns,
255*7c478bd9Sstevel@tonic-gate 		    LDAPMemCache **cachep )
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate     unsigned long total_size = 0;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_init\n", 0, 0, 0 );
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate     if ( cachep == NULL ) {
262*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
263*7c478bd9Sstevel@tonic-gate     }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate     if ((*cachep = (LDAPMemCache*)NSLDAPI_CALLOC(1,
266*7c478bd9Sstevel@tonic-gate 	    sizeof(LDAPMemCache))) == NULL) {
267*7c478bd9Sstevel@tonic-gate 	return ( LDAP_NO_MEMORY );
268*7c478bd9Sstevel@tonic-gate     }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate     total_size += sizeof(LDAPMemCache);
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate     (*cachep)->ldmemc_ttl = ttl;
273*7c478bd9Sstevel@tonic-gate     (*cachep)->ldmemc_size = size;
274*7c478bd9Sstevel@tonic-gate     (*cachep)->ldmemc_lds = NULL;
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate     /* Non-zero default size needed for calculating size of hash tables */
277*7c478bd9Sstevel@tonic-gate     size = (size ? size : MEMCACHE_DEF_SIZE);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate     if (thread_fns) {
280*7c478bd9Sstevel@tonic-gate 	memcpy(&((*cachep)->ldmemc_lock_fns), thread_fns,
281*7c478bd9Sstevel@tonic-gate            sizeof(struct ldap_thread_fns));
282*7c478bd9Sstevel@tonic-gate     } else {
283*7c478bd9Sstevel@tonic-gate 	memset(&((*cachep)->ldmemc_lock_fns), 0,
284*7c478bd9Sstevel@tonic-gate 	   sizeof(struct ldap_thread_fns));
285*7c478bd9Sstevel@tonic-gate     }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate     (*cachep)->ldmemc_lock = LDAP_MEMCACHE_MUTEX_ALLOC( *cachep );
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate     /* Cache basedns */
290*7c478bd9Sstevel@tonic-gate     if (baseDNs != NULL) {
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	int i;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	for (i = 0; baseDNs[i]; i++) {
295*7c478bd9Sstevel@tonic-gate 		;
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	(*cachep)->ldmemc_basedns = (char**)NSLDAPI_CALLOC(i + 1,
299*7c478bd9Sstevel@tonic-gate 		sizeof(char*));
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if ((*cachep)->ldmemc_basedns == NULL) {
302*7c478bd9Sstevel@tonic-gate     	    ldap_memcache_destroy(*cachep);
303*7c478bd9Sstevel@tonic-gate 	    *cachep = NULL;
304*7c478bd9Sstevel@tonic-gate 	    return ( LDAP_NO_MEMORY );
305*7c478bd9Sstevel@tonic-gate 	}
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	total_size += (i + 1) * sizeof(char*);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	for (i = 0; baseDNs[i]; i++) {
310*7c478bd9Sstevel@tonic-gate 	    (*cachep)->ldmemc_basedns[i] = nsldapi_strdup(baseDNs[i]);
311*7c478bd9Sstevel@tonic-gate 	    if ((*cachep)->ldmemc_basedns[i] == NULL)
312*7c478bd9Sstevel@tonic-gate 			return ( LDAP_NO_MEMORY );
313*7c478bd9Sstevel@tonic-gate 	    total_size += strlen(baseDNs[i]) + 1;
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	(*cachep)->ldmemc_basedns[i] = NULL;
317*7c478bd9Sstevel@tonic-gate     }
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate     /* Create hash table for temporary cache */
320*7c478bd9Sstevel@tonic-gate     if (htable_create(size, msgid_hashf, msgid_putdata, msgid_getdata,
321*7c478bd9Sstevel@tonic-gate                       msgid_removedata, msgid_clearnode, msgid_clear_ld_items,
322*7c478bd9Sstevel@tonic-gate 		      &((*cachep)->ldmemc_resTmp)) != LDAP_SUCCESS) {
323*7c478bd9Sstevel@tonic-gate 	ldap_memcache_destroy(*cachep);
324*7c478bd9Sstevel@tonic-gate 	*cachep = NULL;
325*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_MEMORY );
326*7c478bd9Sstevel@tonic-gate     }
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate     total_size += htable_sizeinbytes((*cachep)->ldmemc_resTmp);
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate     /* Create hash table for primary cache */
331*7c478bd9Sstevel@tonic-gate     if (htable_create(size, attrkey_hashf, attrkey_putdata,
332*7c478bd9Sstevel@tonic-gate 	              attrkey_getdata, attrkey_removedata, attrkey_clearnode,
333*7c478bd9Sstevel@tonic-gate 		      NULL, &((*cachep)->ldmemc_resLookup)) != LDAP_SUCCESS) {
334*7c478bd9Sstevel@tonic-gate 	ldap_memcache_destroy(*cachep);
335*7c478bd9Sstevel@tonic-gate 	*cachep = NULL;
336*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_MEMORY );
337*7c478bd9Sstevel@tonic-gate     }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate     total_size += htable_sizeinbytes((*cachep)->ldmemc_resLookup);
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate     /* See if there is enough room so far */
342*7c478bd9Sstevel@tonic-gate     if (memcache_adj_size(*cachep, total_size, MEMCACHE_SIZE_NON_ENTRIES,
343*7c478bd9Sstevel@tonic-gate 	                  MEMCACHE_SIZE_ADD) != LDAP_SUCCESS) {
344*7c478bd9Sstevel@tonic-gate 	ldap_memcache_destroy(*cachep);
345*7c478bd9Sstevel@tonic-gate 	*cachep = NULL;
346*7c478bd9Sstevel@tonic-gate 	return( LDAP_SIZELIMIT_EXCEEDED );
347*7c478bd9Sstevel@tonic-gate     }
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_init new cache 0x%x\n",
350*7c478bd9Sstevel@tonic-gate 	    *cachep, 0, 0 );
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate /* Associates a ldap handle to a memcache object. */
356*7c478bd9Sstevel@tonic-gate int
357*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_memcache_set(LDAP * ld,LDAPMemCache * cache)358*7c478bd9Sstevel@tonic-gate ldap_memcache_set( LDAP *ld, LDAPMemCache *cache )
359*7c478bd9Sstevel@tonic-gate {
360*7c478bd9Sstevel@tonic-gate     int nRes = LDAP_SUCCESS;
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_set\n", 0, 0, 0 );
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) )
365*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate     if (ld->ld_memcache != cache) {
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate         LDAPMemCache *c = ld->ld_memcache;
372*7c478bd9Sstevel@tonic-gate 	ldapmemcacheld *pCur = NULL;
373*7c478bd9Sstevel@tonic-gate 	ldapmemcacheld *pPrev = NULL;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	/* First dissociate handle from old cache */
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	LDAP_MEMCACHE_MUTEX_LOCK( c );
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	pCur = (c ? c->ldmemc_lds : NULL);
380*7c478bd9Sstevel@tonic-gate 	for (; pCur; pCur = pCur->ldmemcl_next) {
381*7c478bd9Sstevel@tonic-gate 	    if (pCur->ldmemcl_ld == ld)
382*7c478bd9Sstevel@tonic-gate 		break;
383*7c478bd9Sstevel@tonic-gate 	    pPrev = pCur;
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	if (pCur) {
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	    ldapmemcacheReqId reqid;
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	    reqid.ldmemcrid_ld = ld;
391*7c478bd9Sstevel@tonic-gate 	    reqid.ldmemcrid_msgid = -1;
392*7c478bd9Sstevel@tonic-gate 	    htable_misc(c->ldmemc_resTmp, (void*)&reqid, (void*)c);
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	    if (pPrev)
395*7c478bd9Sstevel@tonic-gate 		pPrev->ldmemcl_next = pCur->ldmemcl_next;
396*7c478bd9Sstevel@tonic-gate 	    else
397*7c478bd9Sstevel@tonic-gate 		c->ldmemc_lds = pCur->ldmemcl_next;
398*7c478bd9Sstevel@tonic-gate 	    NSLDAPI_FREE(pCur);
399*7c478bd9Sstevel@tonic-gate 	    pCur = NULL;
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	    memcache_adj_size(c, sizeof(ldapmemcacheld),
402*7c478bd9Sstevel@tonic-gate 	                MEMCACHE_SIZE_NON_ENTRIES, MEMCACHE_SIZE_DEDUCT);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	LDAP_MEMCACHE_MUTEX_UNLOCK( c );
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	ld->ld_memcache = NULL;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	/* Exit if no new cache is specified */
410*7c478bd9Sstevel@tonic-gate 	if (cache == NULL) {
411*7c478bd9Sstevel@tonic-gate 	    LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
412*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	/* Then associate handle with new cache */
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate         LDAP_MEMCACHE_MUTEX_LOCK( cache );
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	if ((nRes = memcache_adj_size(cache, sizeof(ldapmemcacheld),
420*7c478bd9Sstevel@tonic-gate 	       MEMCACHE_SIZE_NON_ENTRIES, MEMCACHE_SIZE_ADD)) != LDAP_SUCCESS) {
421*7c478bd9Sstevel@tonic-gate 	    LDAP_MEMCACHE_MUTEX_UNLOCK( cache );
422*7c478bd9Sstevel@tonic-gate 	    LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
423*7c478bd9Sstevel@tonic-gate 	    return nRes;
424*7c478bd9Sstevel@tonic-gate 	}
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	pCur = (ldapmemcacheld*)NSLDAPI_CALLOC(1, sizeof(ldapmemcacheld));
427*7c478bd9Sstevel@tonic-gate 	if (pCur == NULL) {
428*7c478bd9Sstevel@tonic-gate 	    memcache_adj_size(cache, sizeof(ldapmemcacheld),
429*7c478bd9Sstevel@tonic-gate 		              MEMCACHE_SIZE_NON_ENTRIES, MEMCACHE_SIZE_DEDUCT);
430*7c478bd9Sstevel@tonic-gate 	    nRes = LDAP_NO_MEMORY;
431*7c478bd9Sstevel@tonic-gate 	} else {
432*7c478bd9Sstevel@tonic-gate 	    pCur->ldmemcl_ld = ld;
433*7c478bd9Sstevel@tonic-gate 	    pCur->ldmemcl_next = cache->ldmemc_lds;
434*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_lds = pCur;
435*7c478bd9Sstevel@tonic-gate 	    ld->ld_memcache = cache;
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	LDAP_MEMCACHE_MUTEX_UNLOCK( cache );
439*7c478bd9Sstevel@tonic-gate     }
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate     return nRes;
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate /* Retrieves memcache with which the ldap handle has been associated. */
447*7c478bd9Sstevel@tonic-gate int
448*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_memcache_get(LDAP * ld,LDAPMemCache ** cachep)449*7c478bd9Sstevel@tonic-gate ldap_memcache_get( LDAP *ld, LDAPMemCache **cachep )
450*7c478bd9Sstevel@tonic-gate {
451*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_get ld: 0x%x\n", ld, 0, 0 );
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || cachep == NULL ) {
454*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
455*7c478bd9Sstevel@tonic-gate     }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
458*7c478bd9Sstevel@tonic-gate     *cachep = ld->ld_memcache;
459*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
462*7c478bd9Sstevel@tonic-gate }
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate /*
465*7c478bd9Sstevel@tonic-gate  * Function that stays inside libldap and proactively expires items from
466*7c478bd9Sstevel@tonic-gate  * the given cache.  This should be called from a newly created thread since
467*7c478bd9Sstevel@tonic-gate  * it will not return until after ldap_memcache_destroy() is called.
468*7c478bd9Sstevel@tonic-gate  */
469*7c478bd9Sstevel@tonic-gate void
470*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_memcache_update(LDAPMemCache * cache)471*7c478bd9Sstevel@tonic-gate ldap_memcache_update( LDAPMemCache *cache )
472*7c478bd9Sstevel@tonic-gate {
473*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_update: cache 0x%x\n",
474*7c478bd9Sstevel@tonic-gate 	    cache, 0, 0 );
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_MEMCACHE_POINTER( cache )) {
477*7c478bd9Sstevel@tonic-gate 	return;
478*7c478bd9Sstevel@tonic-gate     }
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( cache );
481*7c478bd9Sstevel@tonic-gate     memcache_access(cache, MEMCACHE_ACCESS_UPDATE, NULL, NULL, NULL);
482*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( cache );
483*7c478bd9Sstevel@tonic-gate }
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate /* Removes specified entries from given memcache. */
486*7c478bd9Sstevel@tonic-gate void
487*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_memcache_flush(LDAPMemCache * cache,char * dn,int scope)488*7c478bd9Sstevel@tonic-gate ldap_memcache_flush( LDAPMemCache *cache, char *dn, int scope )
489*7c478bd9Sstevel@tonic-gate {
490*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE,
491*7c478bd9Sstevel@tonic-gate 	    "ldap_memcache_flush( cache: 0x%x, dn: %s, scope: %d)\n",
492*7c478bd9Sstevel@tonic-gate 	    cache, ( dn == NULL ) ? "(null)" : dn, scope );
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_MEMCACHE_POINTER( cache )) {
495*7c478bd9Sstevel@tonic-gate 	return;
496*7c478bd9Sstevel@tonic-gate     }
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( cache );
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate     if (!dn) {
501*7c478bd9Sstevel@tonic-gate 	memcache_access(cache, MEMCACHE_ACCESS_FLUSH_ALL, NULL, NULL, NULL);
502*7c478bd9Sstevel@tonic-gate     } else {
503*7c478bd9Sstevel@tonic-gate 	memcache_access(cache, MEMCACHE_ACCESS_FLUSH,
504*7c478bd9Sstevel@tonic-gate 	                (void*)dn, (void*)(uintptr_t)scope, NULL);
505*7c478bd9Sstevel@tonic-gate     }
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( cache );
508*7c478bd9Sstevel@tonic-gate }
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate /* Destroys the given memcache. */
511*7c478bd9Sstevel@tonic-gate void
512*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_memcache_destroy(LDAPMemCache * cache)513*7c478bd9Sstevel@tonic-gate ldap_memcache_destroy( LDAPMemCache *cache )
514*7c478bd9Sstevel@tonic-gate {
515*7c478bd9Sstevel@tonic-gate     int i = 0;
516*7c478bd9Sstevel@tonic-gate     unsigned long size = sizeof(LDAPMemCache);
517*7c478bd9Sstevel@tonic-gate     ldapmemcacheld *pNode = NULL, *pNextNode = NULL;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_destroy( 0x%x )\n",
520*7c478bd9Sstevel@tonic-gate 	    cache, 0, 0 );
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_MEMCACHE_POINTER( cache )) {
523*7c478bd9Sstevel@tonic-gate 	return;
524*7c478bd9Sstevel@tonic-gate     }
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate     /* Dissociate all ldap handes from this cache. */
527*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( cache );
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate     for (pNode = cache->ldmemc_lds; pNode; pNode = pNextNode, i++) {
530*7c478bd9Sstevel@tonic-gate         LDAP_MUTEX_LOCK( pNode->ldmemcl_ld, LDAP_MEMCACHE_LOCK );
531*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_lds = pNode->ldmemcl_next;
532*7c478bd9Sstevel@tonic-gate 	pNode->ldmemcl_ld->ld_memcache = NULL;
533*7c478bd9Sstevel@tonic-gate         LDAP_MUTEX_UNLOCK( pNode->ldmemcl_ld, LDAP_MEMCACHE_LOCK );
534*7c478bd9Sstevel@tonic-gate 	pNextNode = pNode->ldmemcl_next;
535*7c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE(pNode);
536*7c478bd9Sstevel@tonic-gate     }
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate     size += i * sizeof(ldapmemcacheld);
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( cache );
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate     /* Free array of basedns */
543*7c478bd9Sstevel@tonic-gate     if (cache->ldmemc_basedns) {
544*7c478bd9Sstevel@tonic-gate 	for (i = 0; cache->ldmemc_basedns[i]; i++) {
545*7c478bd9Sstevel@tonic-gate 	    size += strlen(cache->ldmemc_basedns[i]) + 1;
546*7c478bd9Sstevel@tonic-gate 	    NSLDAPI_FREE(cache->ldmemc_basedns[i]);
547*7c478bd9Sstevel@tonic-gate 	}
548*7c478bd9Sstevel@tonic-gate 	size += (i + 1) * sizeof(char*);
549*7c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE(cache->ldmemc_basedns);
550*7c478bd9Sstevel@tonic-gate     }
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate     /* Free hash table used for temporary cache */
553*7c478bd9Sstevel@tonic-gate     if (cache->ldmemc_resTmp) {
554*7c478bd9Sstevel@tonic-gate 	size += htable_sizeinbytes(cache->ldmemc_resTmp);
555*7c478bd9Sstevel@tonic-gate 	memcache_access(cache, MEMCACHE_ACCESS_DELETE_ALL, NULL, NULL, NULL);
556*7c478bd9Sstevel@tonic-gate 	htable_free(cache->ldmemc_resTmp);
557*7c478bd9Sstevel@tonic-gate     }
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate     /* Free hash table used for primary cache */
560*7c478bd9Sstevel@tonic-gate     if (cache->ldmemc_resLookup) {
561*7c478bd9Sstevel@tonic-gate 	size += htable_sizeinbytes(cache->ldmemc_resLookup);
562*7c478bd9Sstevel@tonic-gate 	memcache_access(cache, MEMCACHE_ACCESS_FLUSH_ALL, NULL, NULL, NULL);
563*7c478bd9Sstevel@tonic-gate 	htable_free(cache->ldmemc_resLookup);
564*7c478bd9Sstevel@tonic-gate     }
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate     memcache_adj_size(cache, size, MEMCACHE_SIZE_NON_ENTRIES,
567*7c478bd9Sstevel@tonic-gate 	              MEMCACHE_SIZE_DEDUCT);
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_FREE( cache );
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate     NSLDAPI_FREE(cache);
572*7c478bd9Sstevel@tonic-gate }
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate /************************* Internal API Functions ****************************/
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate /* Creates an integer key by applying the Cyclic Reduntency Check algorithm on
577*7c478bd9Sstevel@tonic-gate    a long string formed by concatenating all the search parameters plus the
578*7c478bd9Sstevel@tonic-gate    current bind DN.  The key is used in the cache for looking up cached
579*7c478bd9Sstevel@tonic-gate    entries.  It is assumed that the CRC algorithm will generate
580*7c478bd9Sstevel@tonic-gate    different integers from different byte strings. */
581*7c478bd9Sstevel@tonic-gate int
ldap_memcache_createkey(LDAP * ld,const char * base,int scope,const char * filter,char ** attrs,int attrsonly,LDAPControl ** serverctrls,LDAPControl ** clientctrls,unsigned long * keyp)582*7c478bd9Sstevel@tonic-gate ldap_memcache_createkey(LDAP *ld, const char *base, int scope,
583*7c478bd9Sstevel@tonic-gate 			const char *filter, char **attrs,
584*7c478bd9Sstevel@tonic-gate                         int attrsonly, LDAPControl **serverctrls,
585*7c478bd9Sstevel@tonic-gate                         LDAPControl **clientctrls, unsigned long *keyp)
586*7c478bd9Sstevel@tonic-gate {
587*7c478bd9Sstevel@tonic-gate     int nRes, i, j, i_smallest;
588*7c478bd9Sstevel@tonic-gate     int len;
589*7c478bd9Sstevel@tonic-gate     int defport;
590*7c478bd9Sstevel@tonic-gate     char buf[50];
591*7c478bd9Sstevel@tonic-gate     char *tmp, *defhost, *binddn, *keystr, *tmpbase;
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || (keyp == NULL) )
594*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate     *keyp = 0;
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
599*7c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
602*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( ld->ld_memcache );
603*7c478bd9Sstevel@tonic-gate     nRes = memcache_validate_basedn(ld->ld_memcache, base);
604*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( ld->ld_memcache );
605*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate     if (nRes != LDAP_SUCCESS)
608*7c478bd9Sstevel@tonic-gate 	return nRes;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate     defhost = NSLDAPI_STR_NONNULL(ld->ld_defhost);
611*7c478bd9Sstevel@tonic-gate     defport = ld->ld_defport;
612*7c478bd9Sstevel@tonic-gate     tmpbase = nsldapi_strdup(NSLDAPI_STR_NONNULL(base));
613*7c478bd9Sstevel@tonic-gate 	if (tmpbase == NULL)
614*7c478bd9Sstevel@tonic-gate 		return( LDAP_LOCAL_ERROR );
615*7c478bd9Sstevel@tonic-gate     memcache_trim_basedn_spaces(tmpbase);
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate     if ((binddn = nsldapi_get_binddn(ld)) == NULL)
618*7c478bd9Sstevel@tonic-gate 	binddn = "";
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate     sprintf(buf, "%i\n%i\n%i\n", defport, scope, (attrsonly ? 1 : 0));
621*7c478bd9Sstevel@tonic-gate     len = NSLDAPI_SAFE_STRLEN(buf) + NSLDAPI_SAFE_STRLEN(tmpbase) +
622*7c478bd9Sstevel@tonic-gate 	  NSLDAPI_SAFE_STRLEN(filter) + NSLDAPI_SAFE_STRLEN(defhost) +
623*7c478bd9Sstevel@tonic-gate 	  NSLDAPI_SAFE_STRLEN(binddn);
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate     if (attrs) {
626*7c478bd9Sstevel@tonic-gate 	for (i = 0; attrs[i]; i++) {
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	    for (i_smallest = j = i; attrs[j]; j++) {
629*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrs[i_smallest], attrs[j]) > 0)
630*7c478bd9Sstevel@tonic-gate 		    i_smallest = j;
631*7c478bd9Sstevel@tonic-gate 	    }
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	    if (i != i_smallest) {
634*7c478bd9Sstevel@tonic-gate 		tmp = attrs[i];
635*7c478bd9Sstevel@tonic-gate 		attrs[i] = attrs[i_smallest];
636*7c478bd9Sstevel@tonic-gate 		attrs[i_smallest] = tmp;
637*7c478bd9Sstevel@tonic-gate 	    }
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	    len += NSLDAPI_SAFE_STRLEN(attrs[i]);
640*7c478bd9Sstevel@tonic-gate 	}
641*7c478bd9Sstevel@tonic-gate     } else {
642*7c478bd9Sstevel@tonic-gate 	len += 1;
643*7c478bd9Sstevel@tonic-gate     }
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate     len += memcache_get_ctrls_len(serverctrls) +
646*7c478bd9Sstevel@tonic-gate 	   memcache_get_ctrls_len(clientctrls) + 1;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate     if ((keystr = (char*)NSLDAPI_CALLOC(len, sizeof(char))) == NULL) {
649*7c478bd9Sstevel@tonic-gate 	if (defhost != emptyStr)
650*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE(defhost);
651*7c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE(tmpbase);
652*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_MEMORY );
653*7c478bd9Sstevel@tonic-gate     }
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate     sprintf(keystr, "%s\n%s\n%s\n%s\n%s\n", binddn, tmpbase,
656*7c478bd9Sstevel@tonic-gate 	    NSLDAPI_STR_NONNULL(defhost), NSLDAPI_STR_NONNULL(filter),
657*7c478bd9Sstevel@tonic-gate 	    NSLDAPI_STR_NONNULL(buf));
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate     if (attrs) {
660*7c478bd9Sstevel@tonic-gate 	for (i = 0; attrs[i]; i++) {
661*7c478bd9Sstevel@tonic-gate 	    strcat(keystr, NSLDAPI_STR_NONNULL(attrs[i]));
662*7c478bd9Sstevel@tonic-gate 	    strcat(keystr, "\n");
663*7c478bd9Sstevel@tonic-gate 	}
664*7c478bd9Sstevel@tonic-gate     } else {
665*7c478bd9Sstevel@tonic-gate 	strcat(keystr, "\n");
666*7c478bd9Sstevel@tonic-gate     }
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate     for (tmp = keystr; *tmp;
669*7c478bd9Sstevel@tonic-gate          *tmp += (*tmp >= 'a' && *tmp <= 'z' ? 'A'-'a' : 0), tmp++) {
670*7c478bd9Sstevel@tonic-gate 		;
671*7c478bd9Sstevel@tonic-gate 	}
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate     memcache_append_ctrls(keystr, serverctrls, clientctrls);
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate     /* CRC algorithm */
676*7c478bd9Sstevel@tonic-gate     *keyp = crc32_convert(keystr, len);
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate     NSLDAPI_FREE(keystr);
679*7c478bd9Sstevel@tonic-gate     NSLDAPI_FREE(tmpbase);
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate     return LDAP_SUCCESS;
682*7c478bd9Sstevel@tonic-gate }
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate /* Searches the cache for the right cached entries, and if found, attaches
685*7c478bd9Sstevel@tonic-gate    them to the given ldap handle.  This function relies on locking by the
686*7c478bd9Sstevel@tonic-gate    caller. */
687*7c478bd9Sstevel@tonic-gate int
ldap_memcache_result(LDAP * ld,int msgid,unsigned long key)688*7c478bd9Sstevel@tonic-gate ldap_memcache_result(LDAP *ld, int msgid, unsigned long key)
689*7c478bd9Sstevel@tonic-gate {
690*7c478bd9Sstevel@tonic-gate     int nRes;
691*7c478bd9Sstevel@tonic-gate     LDAPMessage *pMsg = NULL;
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE,
694*7c478bd9Sstevel@tonic-gate 	    "ldap_memcache_result( ld: 0x%x, msgid: %d, key: 0x%8.8lx)\n",
695*7c478bd9Sstevel@tonic-gate 	    ld, msgid, key );
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || (msgid < 0) ) {
698*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
699*7c478bd9Sstevel@tonic-gate     }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld)) {
702*7c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );
703*7c478bd9Sstevel@tonic-gate     }
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
706*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( ld->ld_memcache );
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate     /* Search the cache and append the results to ld if found */
709*7c478bd9Sstevel@tonic-gate     ++ld->ld_memcache->ldmemc_stats.ldmemcstat_tries;
710*7c478bd9Sstevel@tonic-gate     if ((nRes = memcache_search(ld, key, &pMsg)) == LDAP_SUCCESS) {
711*7c478bd9Sstevel@tonic-gate 	nRes = memcache_add_to_ld(ld, msgid, pMsg);
712*7c478bd9Sstevel@tonic-gate 	++ld->ld_memcache->ldmemc_stats.ldmemcstat_hits;
713*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE,
714*7c478bd9Sstevel@tonic-gate 		"ldap_memcache_result: key 0x%8.8lx found in cache\n",
715*7c478bd9Sstevel@tonic-gate 		key, 0, 0 );
716*7c478bd9Sstevel@tonic-gate     } else {
717*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE,
718*7c478bd9Sstevel@tonic-gate 		"ldap_memcache_result: key 0x%8.8lx not found in cache\n",
719*7c478bd9Sstevel@tonic-gate 		key, 0, 0 );
720*7c478bd9Sstevel@tonic-gate     }
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
723*7c478bd9Sstevel@tonic-gate     memcache_print_list( ld->ld_memcache, LIST_LRU );
724*7c478bd9Sstevel@tonic-gate     memcache_report_statistics( ld->ld_memcache );
725*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( ld->ld_memcache );
728*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate     return nRes;
731*7c478bd9Sstevel@tonic-gate }
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate /* Creates a new header in the cache so that entries arriving from the
734*7c478bd9Sstevel@tonic-gate    directory server can later be cached under the header. */
735*7c478bd9Sstevel@tonic-gate int
ldap_memcache_new(LDAP * ld,int msgid,unsigned long key,const char * basedn)736*7c478bd9Sstevel@tonic-gate ldap_memcache_new(LDAP *ld, int msgid, unsigned long key, const char *basedn)
737*7c478bd9Sstevel@tonic-gate {
738*7c478bd9Sstevel@tonic-gate     int nRes;
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) ) {
741*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
742*7c478bd9Sstevel@tonic-gate     }
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld)) {
747*7c478bd9Sstevel@tonic-gate         LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
748*7c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );
749*7c478bd9Sstevel@tonic-gate     }
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( ld->ld_memcache );
752*7c478bd9Sstevel@tonic-gate     nRes = memcache_add(ld, key, msgid, basedn);
753*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( ld->ld_memcache );
754*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate     return nRes;
757*7c478bd9Sstevel@tonic-gate }
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate /* Appends a chain of entries to an existing cache header.  Parameter "bLast"
760*7c478bd9Sstevel@tonic-gate    indicates whether there will be more entries arriving for the search in
761*7c478bd9Sstevel@tonic-gate    question. */
762*7c478bd9Sstevel@tonic-gate int
ldap_memcache_append(LDAP * ld,int msgid,int bLast,LDAPMessage * result)763*7c478bd9Sstevel@tonic-gate ldap_memcache_append(LDAP *ld, int msgid, int bLast, LDAPMessage *result)
764*7c478bd9Sstevel@tonic-gate {
765*7c478bd9Sstevel@tonic-gate     int nRes = LDAP_SUCCESS;
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_memcache_append( ld: 0x%x, ", ld, 0, 0 );
768*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "msgid %d, bLast: %d, result: 0x%x)\n",
769*7c478bd9Sstevel@tonic-gate 	    msgid, bLast, result );
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || !result ) {
772*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
773*7c478bd9Sstevel@tonic-gate     }
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
776*7c478bd9Sstevel@tonic-gate 
777*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld)) {
778*7c478bd9Sstevel@tonic-gate         LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
779*7c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );
780*7c478bd9Sstevel@tonic-gate     }
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( ld->ld_memcache );
783*7c478bd9Sstevel@tonic-gate 
784*7c478bd9Sstevel@tonic-gate     if (!bLast)
785*7c478bd9Sstevel@tonic-gate 	nRes = memcache_append(ld, msgid, result);
786*7c478bd9Sstevel@tonic-gate     else
787*7c478bd9Sstevel@tonic-gate 	nRes = memcache_append_last(ld, msgid, result);
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE,
790*7c478bd9Sstevel@tonic-gate 	    "ldap_memcache_append: %s result for msgid %d\n",
791*7c478bd9Sstevel@tonic-gate 	    ( nRes == LDAP_SUCCESS ) ? "added" : "failed to add", msgid , 0 );
792*7c478bd9Sstevel@tonic-gate 
793*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( ld->ld_memcache );
794*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate     return nRes;
797*7c478bd9Sstevel@tonic-gate }
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate /* Removes partially cached results for a search as a result of calling
800*7c478bd9Sstevel@tonic-gate    ldap_abandon() by the client. */
801*7c478bd9Sstevel@tonic-gate int
ldap_memcache_abandon(LDAP * ld,int msgid)802*7c478bd9Sstevel@tonic-gate ldap_memcache_abandon(LDAP *ld, int msgid)
803*7c478bd9Sstevel@tonic-gate {
804*7c478bd9Sstevel@tonic-gate     int nRes;
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || (msgid < 0) ) {
807*7c478bd9Sstevel@tonic-gate 	return( LDAP_PARAM_ERROR );
808*7c478bd9Sstevel@tonic-gate     }
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_LOCK( ld, LDAP_MEMCACHE_LOCK );
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld)) {
813*7c478bd9Sstevel@tonic-gate         LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
814*7c478bd9Sstevel@tonic-gate 	return( LDAP_LOCAL_ERROR );
815*7c478bd9Sstevel@tonic-gate     }
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_LOCK( ld->ld_memcache );
818*7c478bd9Sstevel@tonic-gate     nRes = memcache_remove(ld, msgid);
819*7c478bd9Sstevel@tonic-gate     LDAP_MEMCACHE_MUTEX_UNLOCK( ld->ld_memcache );
820*7c478bd9Sstevel@tonic-gate     LDAP_MUTEX_UNLOCK( ld, LDAP_MEMCACHE_LOCK );
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate     return nRes;
823*7c478bd9Sstevel@tonic-gate }
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate /*************************** helper functions *******************************/
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate /* Removes extraneous spaces in a basedn so that basedns differ by only those
828*7c478bd9Sstevel@tonic-gate    spaces will be treated as equal.  Extraneous spaces are those that
829*7c478bd9Sstevel@tonic-gate    precedes the basedn and those that follow a comma. */
830*7c478bd9Sstevel@tonic-gate /*
831*7c478bd9Sstevel@tonic-gate  * XXXmcs: this is a bit too agressive... we need to deal with the fact that
832*7c478bd9Sstevel@tonic-gate  * commas and spaces may be quoted, in which case it is wrong to remove them.
833*7c478bd9Sstevel@tonic-gate  */
834*7c478bd9Sstevel@tonic-gate static void
memcache_trim_basedn_spaces(char * basedn)835*7c478bd9Sstevel@tonic-gate memcache_trim_basedn_spaces(char *basedn)
836*7c478bd9Sstevel@tonic-gate {
837*7c478bd9Sstevel@tonic-gate     char *pRead, *pWrite;
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate     if (!basedn)
840*7c478bd9Sstevel@tonic-gate 	return;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate     for (pWrite = pRead = basedn; *pRead; ) {
843*7c478bd9Sstevel@tonic-gate 	for (; *pRead && NSLDAPI_IS_SPACE(*pRead); pRead++) {
844*7c478bd9Sstevel@tonic-gate 		;
845*7c478bd9Sstevel@tonic-gate 	}
846*7c478bd9Sstevel@tonic-gate 	for (; *pRead && !NSLDAPI_IS_SEPARATER(*pRead);
847*7c478bd9Sstevel@tonic-gate 	    *(pWrite++) = *(pRead++)) {
848*7c478bd9Sstevel@tonic-gate 	    ;
849*7c478bd9Sstevel@tonic-gate 	}
850*7c478bd9Sstevel@tonic-gate 	*(pWrite++) = (*pRead ? *(pRead++) : *pRead);
851*7c478bd9Sstevel@tonic-gate     }
852*7c478bd9Sstevel@tonic-gate }
853*7c478bd9Sstevel@tonic-gate 
854*7c478bd9Sstevel@tonic-gate /* Verifies whether the results of a search should be cached or not by
855*7c478bd9Sstevel@tonic-gate    checking if the search's basedn falls under any of the basedns for which
856*7c478bd9Sstevel@tonic-gate    the memcache is responsible. */
857*7c478bd9Sstevel@tonic-gate static int
memcache_validate_basedn(LDAPMemCache * cache,const char * basedn)858*7c478bd9Sstevel@tonic-gate memcache_validate_basedn(LDAPMemCache *cache, const char *basedn)
859*7c478bd9Sstevel@tonic-gate {
860*7c478bd9Sstevel@tonic-gate     int i;
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate     if ( cache->ldmemc_basedns == NULL ) {
863*7c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
864*7c478bd9Sstevel@tonic-gate     }
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate #if 1
867*7c478bd9Sstevel@tonic-gate     if (basedn == NULL) {
868*7c478bd9Sstevel@tonic-gate 	basedn = "";
869*7c478bd9Sstevel@tonic-gate     }
870*7c478bd9Sstevel@tonic-gate #else
871*7c478bd9Sstevel@tonic-gate     /* XXXmcs: I do not understand this code... */
872*7c478bd9Sstevel@tonic-gate     if (basedn == NULL)
873*7c478bd9Sstevel@tonic-gate 	return (cache->ldmemc_basedns && cache->ldmemc_basedns[0] ?
874*7c478bd9Sstevel@tonic-gate 	                             LDAP_OPERATIONS_ERROR : LDAP_SUCCESS);
875*7c478bd9Sstevel@tonic-gate     }
876*7c478bd9Sstevel@tonic-gate #endif
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate     for (i = 0; cache->ldmemc_basedns[i]; i++) {
879*7c478bd9Sstevel@tonic-gate 	if (memcache_compare_dn(basedn, cache->ldmemc_basedns[i],
880*7c478bd9Sstevel@tonic-gate 	                        LDAP_SCOPE_SUBTREE) == LDAP_COMPARE_TRUE) {
881*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
882*7c478bd9Sstevel@tonic-gate 	}
883*7c478bd9Sstevel@tonic-gate     }
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate     return( LDAP_OPERATIONS_ERROR );
886*7c478bd9Sstevel@tonic-gate }
887*7c478bd9Sstevel@tonic-gate 
888*7c478bd9Sstevel@tonic-gate /* Calculates the length of the buffer needed to concatenate the contents of
889*7c478bd9Sstevel@tonic-gate    a ldap control. */
890*7c478bd9Sstevel@tonic-gate static int
891*7c478bd9Sstevel@tonic-gate memcache_get_ctrls_len(LDAPControl **ctrls)
892*7c478bd9Sstevel@tonic-gate {
893*7c478bd9Sstevel@tonic-gate     int len = 0, i;
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate     if (ctrls) {
896*7c478bd9Sstevel@tonic-gate 	for (i = 0; ctrls[i]; i++) {
897*7c478bd9Sstevel@tonic-gate 	    len += strlen(NSLDAPI_STR_NONNULL(ctrls[i]->ldctl_oid)) +
898*7c478bd9Sstevel@tonic-gate 	           (ctrls[i]->ldctl_value).bv_len + 4;
899*7c478bd9Sstevel@tonic-gate 	}
900*7c478bd9Sstevel@tonic-gate     }
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate     return len;
903*7c478bd9Sstevel@tonic-gate }
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate /* Contenates the contents of client and server controls to a buffer. */
906*7c478bd9Sstevel@tonic-gate static void
907*7c478bd9Sstevel@tonic-gate memcache_append_ctrls(char *buf, LDAPControl **serverCtrls,
908*7c478bd9Sstevel@tonic-gate 				  LDAPControl **clientCtrls)
909*7c478bd9Sstevel@tonic-gate {
910*7c478bd9Sstevel@tonic-gate     int i, j;
911*7c478bd9Sstevel@tonic-gate     char *pCh = buf + strlen(buf);
912*7c478bd9Sstevel@tonic-gate     LDAPControl **ctrls;
913*7c478bd9Sstevel@tonic-gate 
914*7c478bd9Sstevel@tonic-gate     for (j = 0; j < 2; j++) {
915*7c478bd9Sstevel@tonic-gate 
916*7c478bd9Sstevel@tonic-gate 	if ((ctrls = (j ? clientCtrls : serverCtrls)) == NULL)
917*7c478bd9Sstevel@tonic-gate 	    continue;
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	for (i = 0; ctrls[i]; i++) {
920*7c478bd9Sstevel@tonic-gate 	    sprintf(pCh, "%s\n", NSLDAPI_STR_NONNULL(ctrls[i]->ldctl_oid));
921*7c478bd9Sstevel@tonic-gate 	    pCh += strlen(NSLDAPI_STR_NONNULL(ctrls[i]->ldctl_oid)) + 1;
922*7c478bd9Sstevel@tonic-gate 	    if ((ctrls[i]->ldctl_value).bv_len > 0) {
923*7c478bd9Sstevel@tonic-gate 		memcpy(pCh, (ctrls[i]->ldctl_value).bv_val,
924*7c478bd9Sstevel@tonic-gate 		       (ctrls[i]->ldctl_value).bv_len);
925*7c478bd9Sstevel@tonic-gate 		pCh += (ctrls[i]->ldctl_value).bv_len;
926*7c478bd9Sstevel@tonic-gate 	    }
927*7c478bd9Sstevel@tonic-gate 	    sprintf(pCh, "\n%i\n", (ctrls[i]->ldctl_iscritical ? 1 : 0));
928*7c478bd9Sstevel@tonic-gate 	    pCh += 3;
929*7c478bd9Sstevel@tonic-gate 	}
930*7c478bd9Sstevel@tonic-gate     }
931*7c478bd9Sstevel@tonic-gate }
932*7c478bd9Sstevel@tonic-gate 
933*7c478bd9Sstevel@tonic-gate /* Increases or decreases the size (in bytes) the given memcache currently
934*7c478bd9Sstevel@tonic-gate    uses.  If the size goes over the limit, the function returns an error. */
935*7c478bd9Sstevel@tonic-gate static int
936*7c478bd9Sstevel@tonic-gate memcache_adj_size(LDAPMemCache *cache, unsigned long size,
937*7c478bd9Sstevel@tonic-gate                   int usageFlags, int bAdd)
938*7c478bd9Sstevel@tonic-gate {
939*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE,
940*7c478bd9Sstevel@tonic-gate 	    "memcache_adj_size: attempting to %s %ld %s bytes...\n",
941*7c478bd9Sstevel@tonic-gate 	    bAdd ? "add" : "remove", size,
942*7c478bd9Sstevel@tonic-gate 	    ( usageFlags & MEMCACHE_SIZE_ENTRIES ) ? "entry" : "non-entry" );
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate     if (bAdd) {
945*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_size_used += size;
946*7c478bd9Sstevel@tonic-gate 	if ((cache->ldmemc_size > 0) &&
947*7c478bd9Sstevel@tonic-gate 	    (cache->ldmemc_size_used > cache->ldmemc_size)) {
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate 	    if (size > cache->ldmemc_size_entries) {
950*7c478bd9Sstevel@tonic-gate 	        cache->ldmemc_size_used -= size;
951*7c478bd9Sstevel@tonic-gate 		LDAPDebug( LDAP_DEBUG_TRACE,
952*7c478bd9Sstevel@tonic-gate 			"memcache_adj_size: failed (size > size_entries %ld).\n",
953*7c478bd9Sstevel@tonic-gate 			cache->ldmemc_size_entries, 0, 0 );
954*7c478bd9Sstevel@tonic-gate 		return( LDAP_SIZELIMIT_EXCEEDED );
955*7c478bd9Sstevel@tonic-gate 	    }
956*7c478bd9Sstevel@tonic-gate 
957*7c478bd9Sstevel@tonic-gate 	    while (cache->ldmemc_size_used > cache->ldmemc_size) {
958*7c478bd9Sstevel@tonic-gate 		if (memcache_access(cache, MEMCACHE_ACCESS_FLUSH_LRU,
959*7c478bd9Sstevel@tonic-gate 		                    NULL, NULL, NULL) != LDAP_SUCCESS) {
960*7c478bd9Sstevel@tonic-gate 	            cache->ldmemc_size_used -= size;
961*7c478bd9Sstevel@tonic-gate 		    LDAPDebug( LDAP_DEBUG_TRACE,
962*7c478bd9Sstevel@tonic-gate 			    "memcache_adj_size: failed (LRU flush failed).\n",
963*7c478bd9Sstevel@tonic-gate 			    0, 0, 0 );
964*7c478bd9Sstevel@tonic-gate 		    return( LDAP_SIZELIMIT_EXCEEDED );
965*7c478bd9Sstevel@tonic-gate 	        }
966*7c478bd9Sstevel@tonic-gate 	    }
967*7c478bd9Sstevel@tonic-gate 	}
968*7c478bd9Sstevel@tonic-gate 	if (usageFlags & MEMCACHE_SIZE_ENTRIES)
969*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_size_entries += size;
970*7c478bd9Sstevel@tonic-gate     } else {
971*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_size_used -= size;
972*7c478bd9Sstevel@tonic-gate 	assert(cache->ldmemc_size_used >= 0);
973*7c478bd9Sstevel@tonic-gate 	if (usageFlags & MEMCACHE_SIZE_ENTRIES)
974*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_size_entries -= size;
975*7c478bd9Sstevel@tonic-gate     }
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
978*7c478bd9Sstevel@tonic-gate     if ( cache->ldmemc_size == 0 ) {	/* no size limit */
979*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE,
980*7c478bd9Sstevel@tonic-gate 		"memcache_adj_size: succeeded (new size: %ld bytes).\n",
981*7c478bd9Sstevel@tonic-gate 		cache->ldmemc_size_used, 0, 0 );
982*7c478bd9Sstevel@tonic-gate     } else {
983*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE,
984*7c478bd9Sstevel@tonic-gate 		"memcache_adj_size: succeeded (new size: %ld bytes, "
985*7c478bd9Sstevel@tonic-gate 		"free space: %ld bytes).\n", cache->ldmemc_size_used,
986*7c478bd9Sstevel@tonic-gate 		cache->ldmemc_size - cache->ldmemc_size_used, 0 );
987*7c478bd9Sstevel@tonic-gate     }
988*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */
989*7c478bd9Sstevel@tonic-gate 
990*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
991*7c478bd9Sstevel@tonic-gate }
992*7c478bd9Sstevel@tonic-gate 
993*7c478bd9Sstevel@tonic-gate /* Searches the cache for results for a particular search identified by
994*7c478bd9Sstevel@tonic-gate    parameter "key", which was generated ldap_memcache_createkey(). */
995*7c478bd9Sstevel@tonic-gate static int
996*7c478bd9Sstevel@tonic-gate memcache_search(LDAP *ld, unsigned long key, LDAPMessage **ppRes)
997*7c478bd9Sstevel@tonic-gate {
998*7c478bd9Sstevel@tonic-gate     int nRes;
999*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pRes;
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate     *ppRes = NULL;
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
1004*7c478bd9Sstevel@tonic-gate         return LDAP_LOCAL_ERROR;
1005*7c478bd9Sstevel@tonic-gate 
1006*7c478bd9Sstevel@tonic-gate     nRes = memcache_access(ld->ld_memcache, MEMCACHE_ACCESS_FIND,
1007*7c478bd9Sstevel@tonic-gate 	                   (void*)&key, (void*)(&pRes), NULL);
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate     if (nRes != LDAP_SUCCESS)
1010*7c478bd9Sstevel@tonic-gate 	return nRes;
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate     *ppRes = pRes->ldmemcr_resHead;
1013*7c478bd9Sstevel@tonic-gate     assert((pRes->ldmemcr_req_id).ldmemcrid_msgid == -1);
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1016*7c478bd9Sstevel@tonic-gate }
1017*7c478bd9Sstevel@tonic-gate 
1018*7c478bd9Sstevel@tonic-gate /* Adds a new header into the cache as a place holder for entries
1019*7c478bd9Sstevel@tonic-gate    arriving later. */
1020*7c478bd9Sstevel@tonic-gate static int
1021*7c478bd9Sstevel@tonic-gate memcache_add(LDAP *ld, unsigned long key, int msgid,
1022*7c478bd9Sstevel@tonic-gate 			const char *basedn)
1023*7c478bd9Sstevel@tonic-gate {
1024*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId reqid;
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
1027*7c478bd9Sstevel@tonic-gate         return LDAP_LOCAL_ERROR;
1028*7c478bd9Sstevel@tonic-gate 
1029*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_msgid = msgid;
1030*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_ld = ld;
1031*7c478bd9Sstevel@tonic-gate 
1032*7c478bd9Sstevel@tonic-gate     return memcache_access(ld->ld_memcache, MEMCACHE_ACCESS_ADD,
1033*7c478bd9Sstevel@tonic-gate 	                   (void*)&key, (void*)&reqid, (void*)basedn);
1034*7c478bd9Sstevel@tonic-gate }
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate /* Appends search entries arriving from the dir server to the cache. */
1037*7c478bd9Sstevel@tonic-gate static int
1038*7c478bd9Sstevel@tonic-gate memcache_append(LDAP *ld, int msgid, LDAPMessage *pRes)
1039*7c478bd9Sstevel@tonic-gate {
1040*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId reqid;
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
1043*7c478bd9Sstevel@tonic-gate         return LDAP_LOCAL_ERROR;
1044*7c478bd9Sstevel@tonic-gate 
1045*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_msgid = msgid;
1046*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_ld = ld;
1047*7c478bd9Sstevel@tonic-gate 
1048*7c478bd9Sstevel@tonic-gate     return memcache_access(ld->ld_memcache, MEMCACHE_ACCESS_APPEND,
1049*7c478bd9Sstevel@tonic-gate 	                   (void*)&reqid, (void*)pRes, NULL);
1050*7c478bd9Sstevel@tonic-gate }
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate /* Same as memcache_append(), but the entries being appended are the
1053*7c478bd9Sstevel@tonic-gate    last from the dir server.  Once all entries for a search have arrived,
1054*7c478bd9Sstevel@tonic-gate    the entries are moved from secondary to primary cache, and a time
1055*7c478bd9Sstevel@tonic-gate    stamp is given to the entries. */
1056*7c478bd9Sstevel@tonic-gate static int
1057*7c478bd9Sstevel@tonic-gate memcache_append_last(LDAP *ld, int msgid, LDAPMessage *pRes)
1058*7c478bd9Sstevel@tonic-gate {
1059*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId reqid;
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
1062*7c478bd9Sstevel@tonic-gate         return LDAP_LOCAL_ERROR;
1063*7c478bd9Sstevel@tonic-gate 
1064*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_msgid = msgid;
1065*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_ld = ld;
1066*7c478bd9Sstevel@tonic-gate 
1067*7c478bd9Sstevel@tonic-gate     return memcache_access(ld->ld_memcache, MEMCACHE_ACCESS_APPEND_LAST,
1068*7c478bd9Sstevel@tonic-gate 	                   (void*)&reqid, (void*)pRes, NULL);
1069*7c478bd9Sstevel@tonic-gate }
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate /* Removes entries from the temporary cache. */
1072*7c478bd9Sstevel@tonic-gate static int
1073*7c478bd9Sstevel@tonic-gate memcache_remove(LDAP *ld, int msgid)
1074*7c478bd9Sstevel@tonic-gate {
1075*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId reqid;
1076*7c478bd9Sstevel@tonic-gate 
1077*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
1078*7c478bd9Sstevel@tonic-gate         return LDAP_LOCAL_ERROR;
1079*7c478bd9Sstevel@tonic-gate 
1080*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_msgid = msgid;
1081*7c478bd9Sstevel@tonic-gate     reqid.ldmemcrid_ld = ld;
1082*7c478bd9Sstevel@tonic-gate 
1083*7c478bd9Sstevel@tonic-gate     return memcache_access(ld->ld_memcache, MEMCACHE_ACCESS_DELETE,
1084*7c478bd9Sstevel@tonic-gate 	                   (void*)&reqid, NULL, NULL);
1085*7c478bd9Sstevel@tonic-gate }
1086*7c478bd9Sstevel@tonic-gate 
1087*7c478bd9Sstevel@tonic-gate #if 0 /* this function is not used */
1088*7c478bd9Sstevel@tonic-gate /* Wipes out everything in the temporary cache directory. */
1089*7c478bd9Sstevel@tonic-gate static int
1090*7c478bd9Sstevel@tonic-gate memcache_remove_all(LDAP *ld)
1091*7c478bd9Sstevel@tonic-gate {
1092*7c478bd9Sstevel@tonic-gate     if (!memcache_exist(ld))
1093*7c478bd9Sstevel@tonic-gate         return LDAP_LOCAL_ERROR;
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate     return memcache_access(ld->ld_memcache, MEMCACHE_ACCESS_DELETE_ALL,
1096*7c478bd9Sstevel@tonic-gate 	                   NULL, NULL, NULL);
1097*7c478bd9Sstevel@tonic-gate }
1098*7c478bd9Sstevel@tonic-gate #endif /* 0 */
1099*7c478bd9Sstevel@tonic-gate 
1100*7c478bd9Sstevel@tonic-gate /* Returns TRUE or FALSE */
1101*7c478bd9Sstevel@tonic-gate static int
1102*7c478bd9Sstevel@tonic-gate memcache_exist(LDAP *ld)
1103*7c478bd9Sstevel@tonic-gate {
1104*7c478bd9Sstevel@tonic-gate     return (ld->ld_memcache != NULL);
1105*7c478bd9Sstevel@tonic-gate }
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate /* Attaches cached entries to an ldap handle. */
1108*7c478bd9Sstevel@tonic-gate static int
1109*7c478bd9Sstevel@tonic-gate memcache_add_to_ld(LDAP *ld, int msgid, LDAPMessage *pMsg)
1110*7c478bd9Sstevel@tonic-gate {
1111*7c478bd9Sstevel@tonic-gate     int nRes = LDAP_SUCCESS;
1112*7c478bd9Sstevel@tonic-gate     LDAPMessage **r;
1113*7c478bd9Sstevel@tonic-gate     LDAPMessage *pCopy;
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate     nRes = memcache_dup_message(pMsg, msgid, 1, &pCopy, NULL);
1116*7c478bd9Sstevel@tonic-gate     if (nRes != LDAP_SUCCESS)
1117*7c478bd9Sstevel@tonic-gate 	return nRes;
1118*7c478bd9Sstevel@tonic-gate 
1119*7c478bd9Sstevel@tonic-gate     for (r = &(ld->ld_responses); *r; r = &((*r)->lm_next))
1120*7c478bd9Sstevel@tonic-gate 	if ((*r)->lm_msgid == msgid)
1121*7c478bd9Sstevel@tonic-gate 	    break;
1122*7c478bd9Sstevel@tonic-gate 
1123*7c478bd9Sstevel@tonic-gate     if (*r)
1124*7c478bd9Sstevel@tonic-gate 	for (r = &((*r)->lm_chain); *r; r = &((*r)->lm_chain)) {
1125*7c478bd9Sstevel@tonic-gate 		;
1126*7c478bd9Sstevel@tonic-gate 	}
1127*7c478bd9Sstevel@tonic-gate 
1128*7c478bd9Sstevel@tonic-gate     *r = pCopy;
1129*7c478bd9Sstevel@tonic-gate 
1130*7c478bd9Sstevel@tonic-gate     return nRes;
1131*7c478bd9Sstevel@tonic-gate }
1132*7c478bd9Sstevel@tonic-gate 
1133*7c478bd9Sstevel@tonic-gate /* Check if main_dn is included in {dn, scope} */
1134*7c478bd9Sstevel@tonic-gate static int
1135*7c478bd9Sstevel@tonic-gate memcache_compare_dn(const char *main_dn, const char *dn, int scope)
1136*7c478bd9Sstevel@tonic-gate {
1137*7c478bd9Sstevel@tonic-gate     int nRes;
1138*7c478bd9Sstevel@tonic-gate     char **components = NULL;
1139*7c478bd9Sstevel@tonic-gate     char **main_components = NULL;
1140*7c478bd9Sstevel@tonic-gate 
1141*7c478bd9Sstevel@tonic-gate     components = ldap_explode_dn(dn, 0);
1142*7c478bd9Sstevel@tonic-gate     main_components = ldap_explode_dn(main_dn, 0);
1143*7c478bd9Sstevel@tonic-gate 
1144*7c478bd9Sstevel@tonic-gate     if (!components || !main_components) {
1145*7c478bd9Sstevel@tonic-gate 	nRes = LDAP_COMPARE_TRUE;
1146*7c478bd9Sstevel@tonic-gate     }
1147*7c478bd9Sstevel@tonic-gate     else {
1148*7c478bd9Sstevel@tonic-gate 
1149*7c478bd9Sstevel@tonic-gate 	int i, main_i;
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate 	main_i = ldap_count_values(main_components) - 1;
1152*7c478bd9Sstevel@tonic-gate 	i = ldap_count_values(components) - 1;
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate 	for (; i >= 0 && main_i >= 0; i--, main_i--) {
1155*7c478bd9Sstevel@tonic-gate 	    if (strcasecmp(main_components[main_i], components[i]))
1156*7c478bd9Sstevel@tonic-gate 		break;
1157*7c478bd9Sstevel@tonic-gate 	}
1158*7c478bd9Sstevel@tonic-gate 
1159*7c478bd9Sstevel@tonic-gate 	if (i >= 0 && main_i >= 0) {
1160*7c478bd9Sstevel@tonic-gate 	    nRes = LDAP_COMPARE_FALSE;
1161*7c478bd9Sstevel@tonic-gate 	}
1162*7c478bd9Sstevel@tonic-gate 	else if (i < 0 && main_i < 0) {
1163*7c478bd9Sstevel@tonic-gate 	    if (scope != LDAP_SCOPE_ONELEVEL)
1164*7c478bd9Sstevel@tonic-gate 		nRes = LDAP_COMPARE_TRUE;
1165*7c478bd9Sstevel@tonic-gate 	    else
1166*7c478bd9Sstevel@tonic-gate 		nRes = LDAP_COMPARE_FALSE;
1167*7c478bd9Sstevel@tonic-gate 	}
1168*7c478bd9Sstevel@tonic-gate 	else if (main_i < 0) {
1169*7c478bd9Sstevel@tonic-gate 	    nRes = LDAP_COMPARE_FALSE;
1170*7c478bd9Sstevel@tonic-gate 	}
1171*7c478bd9Sstevel@tonic-gate 	else {
1172*7c478bd9Sstevel@tonic-gate 	    if (scope == LDAP_SCOPE_BASE)
1173*7c478bd9Sstevel@tonic-gate 		nRes = LDAP_COMPARE_FALSE;
1174*7c478bd9Sstevel@tonic-gate 	    else if (scope == LDAP_SCOPE_SUBTREE)
1175*7c478bd9Sstevel@tonic-gate 		nRes = LDAP_COMPARE_TRUE;
1176*7c478bd9Sstevel@tonic-gate 	    else if (main_i == 0)
1177*7c478bd9Sstevel@tonic-gate 		nRes = LDAP_COMPARE_TRUE;
1178*7c478bd9Sstevel@tonic-gate 	    else
1179*7c478bd9Sstevel@tonic-gate 		nRes = LDAP_COMPARE_FALSE;
1180*7c478bd9Sstevel@tonic-gate 	}
1181*7c478bd9Sstevel@tonic-gate     }
1182*7c478bd9Sstevel@tonic-gate 
1183*7c478bd9Sstevel@tonic-gate     if (components)
1184*7c478bd9Sstevel@tonic-gate 	ldap_value_free(components);
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate     if (main_components)
1187*7c478bd9Sstevel@tonic-gate 	ldap_value_free(main_components);
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate     return nRes;
1190*7c478bd9Sstevel@tonic-gate }
1191*7c478bd9Sstevel@tonic-gate 
1192*7c478bd9Sstevel@tonic-gate /* Dup a complete separate copy of a berelement, including the buffers
1193*7c478bd9Sstevel@tonic-gate    the berelement points to. */
1194*7c478bd9Sstevel@tonic-gate static BerElement*
1195*7c478bd9Sstevel@tonic-gate memcache_ber_dup(BerElement* pBer, unsigned long *pSize)
1196*7c478bd9Sstevel@tonic-gate {
1197*7c478bd9Sstevel@tonic-gate     BerElement *p = ber_dup(pBer);
1198*7c478bd9Sstevel@tonic-gate 
1199*7c478bd9Sstevel@tonic-gate     *pSize = 0;
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate     if (p) {
1202*7c478bd9Sstevel@tonic-gate 
1203*7c478bd9Sstevel@tonic-gate 	*pSize += sizeof(BerElement) + EXTRA_SIZE;
1204*7c478bd9Sstevel@tonic-gate 
1205*7c478bd9Sstevel@tonic-gate 	if (p->ber_len <= EXTRA_SIZE) {
1206*7c478bd9Sstevel@tonic-gate 	    p->ber_flags |= LBER_FLAG_NO_FREE_BUFFER;
1207*7c478bd9Sstevel@tonic-gate 	    p->ber_buf = (char*)p + sizeof(BerElement);
1208*7c478bd9Sstevel@tonic-gate 	} else {
1209*7c478bd9Sstevel@tonic-gate 	    p->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER;
1210*7c478bd9Sstevel@tonic-gate 	    p->ber_buf = (char*)NSLDAPI_CALLOC(1, p->ber_len);
1211*7c478bd9Sstevel@tonic-gate 	    *pSize += (p->ber_buf ? p->ber_len : 0);
1212*7c478bd9Sstevel@tonic-gate 	}
1213*7c478bd9Sstevel@tonic-gate 
1214*7c478bd9Sstevel@tonic-gate 	if (p->ber_buf) {
1215*7c478bd9Sstevel@tonic-gate 	    p->ber_ptr = p->ber_buf + (pBer->ber_ptr - pBer->ber_buf);
1216*7c478bd9Sstevel@tonic-gate 	    p->ber_end = p->ber_buf + p->ber_len;
1217*7c478bd9Sstevel@tonic-gate 	    memcpy(p->ber_buf, pBer->ber_buf, p->ber_len);
1218*7c478bd9Sstevel@tonic-gate 	} else {
1219*7c478bd9Sstevel@tonic-gate 	    ber_free(p, 0);
1220*7c478bd9Sstevel@tonic-gate 	    p = NULL;
1221*7c478bd9Sstevel@tonic-gate 	    *pSize = 0;
1222*7c478bd9Sstevel@tonic-gate 	}
1223*7c478bd9Sstevel@tonic-gate     }
1224*7c478bd9Sstevel@tonic-gate 
1225*7c478bd9Sstevel@tonic-gate     return p;
1226*7c478bd9Sstevel@tonic-gate }
1227*7c478bd9Sstevel@tonic-gate 
1228*7c478bd9Sstevel@tonic-gate /* Dup a entry or a chain of entries. */
1229*7c478bd9Sstevel@tonic-gate static int
1230*7c478bd9Sstevel@tonic-gate memcache_dup_message(LDAPMessage *res, int msgid, int fromcache,
1231*7c478bd9Sstevel@tonic-gate 				LDAPMessage **ppResCopy, unsigned long *pSize)
1232*7c478bd9Sstevel@tonic-gate {
1233*7c478bd9Sstevel@tonic-gate     int nRes = LDAP_SUCCESS;
1234*7c478bd9Sstevel@tonic-gate     unsigned long ber_size;
1235*7c478bd9Sstevel@tonic-gate     LDAPMessage *pCur;
1236*7c478bd9Sstevel@tonic-gate     LDAPMessage **ppCurNew;
1237*7c478bd9Sstevel@tonic-gate 
1238*7c478bd9Sstevel@tonic-gate     *ppResCopy = NULL;
1239*7c478bd9Sstevel@tonic-gate 
1240*7c478bd9Sstevel@tonic-gate     if (pSize)
1241*7c478bd9Sstevel@tonic-gate 	*pSize = 0;
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate     /* Make a copy of res */
1244*7c478bd9Sstevel@tonic-gate     for (pCur = res, ppCurNew = ppResCopy; pCur;
1245*7c478bd9Sstevel@tonic-gate          pCur = pCur->lm_chain, ppCurNew = &((*ppCurNew)->lm_chain)) {
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate 	if ((*ppCurNew = (LDAPMessage*)NSLDAPI_CALLOC(1,
1248*7c478bd9Sstevel@tonic-gate 	                                     sizeof(LDAPMessage))) == NULL) {
1249*7c478bd9Sstevel@tonic-gate 	    nRes = LDAP_NO_MEMORY;
1250*7c478bd9Sstevel@tonic-gate 	    break;
1251*7c478bd9Sstevel@tonic-gate 	}
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate 	memcpy(*ppCurNew, pCur, sizeof(LDAPMessage));
1254*7c478bd9Sstevel@tonic-gate 	(*ppCurNew)->lm_next = NULL;
1255*7c478bd9Sstevel@tonic-gate 	(*ppCurNew)->lm_ber = memcache_ber_dup(pCur->lm_ber, &ber_size);
1256*7c478bd9Sstevel@tonic-gate 	(*ppCurNew)->lm_msgid = msgid;
1257*7c478bd9Sstevel@tonic-gate 	(*ppCurNew)->lm_fromcache = (fromcache != 0);
1258*7c478bd9Sstevel@tonic-gate 
1259*7c478bd9Sstevel@tonic-gate 	if (pSize)
1260*7c478bd9Sstevel@tonic-gate 	    *pSize += sizeof(LDAPMessage) + ber_size;
1261*7c478bd9Sstevel@tonic-gate     }
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate     if ((nRes != LDAP_SUCCESS) && (*ppResCopy != NULL)) {
1264*7c478bd9Sstevel@tonic-gate 	ldap_msgfree(*ppResCopy);
1265*7c478bd9Sstevel@tonic-gate 	*ppResCopy = NULL;
1266*7c478bd9Sstevel@tonic-gate 	if (pSize)
1267*7c478bd9Sstevel@tonic-gate 	    *pSize = 0;
1268*7c478bd9Sstevel@tonic-gate     }
1269*7c478bd9Sstevel@tonic-gate 
1270*7c478bd9Sstevel@tonic-gate     return nRes;
1271*7c478bd9Sstevel@tonic-gate }
1272*7c478bd9Sstevel@tonic-gate 
1273*7c478bd9Sstevel@tonic-gate /************************* Cache Functions ***********************/
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate /* Frees a cache header. */
1276*7c478bd9Sstevel@tonic-gate static int
1277*7c478bd9Sstevel@tonic-gate memcache_free_entry(LDAPMemCache *cache, ldapmemcacheRes *pRes)
1278*7c478bd9Sstevel@tonic-gate {
1279*7c478bd9Sstevel@tonic-gate     if (pRes) {
1280*7c478bd9Sstevel@tonic-gate 
1281*7c478bd9Sstevel@tonic-gate 	unsigned long size = sizeof(ldapmemcacheRes);
1282*7c478bd9Sstevel@tonic-gate 
1283*7c478bd9Sstevel@tonic-gate 	if (pRes->ldmemcr_basedn) {
1284*7c478bd9Sstevel@tonic-gate 	    size += strlen(pRes->ldmemcr_basedn) + 1;
1285*7c478bd9Sstevel@tonic-gate 	    NSLDAPI_FREE(pRes->ldmemcr_basedn);
1286*7c478bd9Sstevel@tonic-gate 	}
1287*7c478bd9Sstevel@tonic-gate 
1288*7c478bd9Sstevel@tonic-gate 	if (pRes->ldmemcr_resHead) {
1289*7c478bd9Sstevel@tonic-gate 	    size += pRes->ldmemcr_resSize;
1290*7c478bd9Sstevel@tonic-gate 	    ldap_msgfree(pRes->ldmemcr_resHead);
1291*7c478bd9Sstevel@tonic-gate 	}
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE(pRes);
1294*7c478bd9Sstevel@tonic-gate 
1295*7c478bd9Sstevel@tonic-gate 	memcache_adj_size(cache, size, MEMCACHE_SIZE_ENTRIES,
1296*7c478bd9Sstevel@tonic-gate 	                  MEMCACHE_SIZE_DEDUCT);
1297*7c478bd9Sstevel@tonic-gate     }
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1300*7c478bd9Sstevel@tonic-gate }
1301*7c478bd9Sstevel@tonic-gate 
1302*7c478bd9Sstevel@tonic-gate /* Detaches a cache header from the list of headers. */
1303*7c478bd9Sstevel@tonic-gate static int
1304*7c478bd9Sstevel@tonic-gate memcache_free_from_list(LDAPMemCache *cache, ldapmemcacheRes *pRes, int index)
1305*7c478bd9Sstevel@tonic-gate {
1306*7c478bd9Sstevel@tonic-gate     if (pRes->ldmemcr_prev[index])
1307*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_prev[index]->ldmemcr_next[index] =
1308*7c478bd9Sstevel@tonic-gate 	                                     pRes->ldmemcr_next[index];
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate     if (pRes->ldmemcr_next[index])
1311*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_next[index]->ldmemcr_prev[index] =
1312*7c478bd9Sstevel@tonic-gate 	                                     pRes->ldmemcr_prev[index];
1313*7c478bd9Sstevel@tonic-gate 
1314*7c478bd9Sstevel@tonic-gate     if (cache->ldmemc_resHead[index] == pRes)
1315*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_resHead[index] = pRes->ldmemcr_next[index];
1316*7c478bd9Sstevel@tonic-gate 
1317*7c478bd9Sstevel@tonic-gate     if (cache->ldmemc_resTail[index] == pRes)
1318*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_resTail[index] = pRes->ldmemcr_prev[index];
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate     pRes->ldmemcr_prev[index] = NULL;
1321*7c478bd9Sstevel@tonic-gate     pRes->ldmemcr_next[index] = NULL;
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1324*7c478bd9Sstevel@tonic-gate }
1325*7c478bd9Sstevel@tonic-gate 
1326*7c478bd9Sstevel@tonic-gate /* Inserts a new cache header to a list of headers. */
1327*7c478bd9Sstevel@tonic-gate static int
1328*7c478bd9Sstevel@tonic-gate memcache_add_to_list(LDAPMemCache *cache, ldapmemcacheRes *pRes, int index)
1329*7c478bd9Sstevel@tonic-gate {
1330*7c478bd9Sstevel@tonic-gate     if (cache->ldmemc_resHead[index])
1331*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_resHead[index]->ldmemcr_prev[index] = pRes;
1332*7c478bd9Sstevel@tonic-gate     else
1333*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_resTail[index] = pRes;
1334*7c478bd9Sstevel@tonic-gate 
1335*7c478bd9Sstevel@tonic-gate     pRes->ldmemcr_prev[index] = NULL;
1336*7c478bd9Sstevel@tonic-gate     pRes->ldmemcr_next[index] = cache->ldmemc_resHead[index];
1337*7c478bd9Sstevel@tonic-gate     cache->ldmemc_resHead[index] = pRes;
1338*7c478bd9Sstevel@tonic-gate 
1339*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1340*7c478bd9Sstevel@tonic-gate }
1341*7c478bd9Sstevel@tonic-gate 
1342*7c478bd9Sstevel@tonic-gate /* Appends a chain of entries to the given cache header. */
1343*7c478bd9Sstevel@tonic-gate static int
1344*7c478bd9Sstevel@tonic-gate memcache_add_res_to_list(ldapmemcacheRes *pRes, LDAPMessage *pMsg,
1345*7c478bd9Sstevel@tonic-gate 				    unsigned long size)
1346*7c478bd9Sstevel@tonic-gate {
1347*7c478bd9Sstevel@tonic-gate     if (pRes->ldmemcr_resTail)
1348*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_resTail->lm_chain = pMsg;
1349*7c478bd9Sstevel@tonic-gate     else
1350*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_resHead = pMsg;
1351*7c478bd9Sstevel@tonic-gate 
1352*7c478bd9Sstevel@tonic-gate     for (pRes->ldmemcr_resTail = pMsg;
1353*7c478bd9Sstevel@tonic-gate          pRes->ldmemcr_resTail->lm_chain;
1354*7c478bd9Sstevel@tonic-gate          pRes->ldmemcr_resTail = pRes->ldmemcr_resTail->lm_chain) {
1355*7c478bd9Sstevel@tonic-gate 		;
1356*7c478bd9Sstevel@tonic-gate 	}
1357*7c478bd9Sstevel@tonic-gate 
1358*7c478bd9Sstevel@tonic-gate     pRes->ldmemcr_resSize += size;
1359*7c478bd9Sstevel@tonic-gate 
1360*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1361*7c478bd9Sstevel@tonic-gate }
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate 
1364*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
1365*7c478bd9Sstevel@tonic-gate static void
1366*7c478bd9Sstevel@tonic-gate memcache_print_list( LDAPMemCache *cache, int index )
1367*7c478bd9Sstevel@tonic-gate {
1368*7c478bd9Sstevel@tonic-gate     char		*name;
1369*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes	*restmp;
1370*7c478bd9Sstevel@tonic-gate 
1371*7c478bd9Sstevel@tonic-gate     switch( index ) {
1372*7c478bd9Sstevel@tonic-gate     case LIST_TTL:
1373*7c478bd9Sstevel@tonic-gate 	name = "TTL";
1374*7c478bd9Sstevel@tonic-gate 	break;
1375*7c478bd9Sstevel@tonic-gate     case LIST_LRU:
1376*7c478bd9Sstevel@tonic-gate 	name = "LRU";
1377*7c478bd9Sstevel@tonic-gate 	break;
1378*7c478bd9Sstevel@tonic-gate     case LIST_TMP:
1379*7c478bd9Sstevel@tonic-gate 	name = "TMP";
1380*7c478bd9Sstevel@tonic-gate 	break;
1381*7c478bd9Sstevel@tonic-gate     case LIST_TOTAL:
1382*7c478bd9Sstevel@tonic-gate 	name = "TOTAL";
1383*7c478bd9Sstevel@tonic-gate 	break;
1384*7c478bd9Sstevel@tonic-gate     default:
1385*7c478bd9Sstevel@tonic-gate 	name = "unknown";
1386*7c478bd9Sstevel@tonic-gate     }
1387*7c478bd9Sstevel@tonic-gate 
1388*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "memcache 0x%x %s list:\n",
1389*7c478bd9Sstevel@tonic-gate 	    cache, name, 0 );
1390*7c478bd9Sstevel@tonic-gate     for ( restmp = cache->ldmemc_resHead[index]; restmp != NULL;
1391*7c478bd9Sstevel@tonic-gate 	    restmp = restmp->ldmemcr_next[index] ) {
1392*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE,
1393*7c478bd9Sstevel@tonic-gate 		"    key: 0x%8.8lx, ld: 0x%x, msgid: %d\n",
1394*7c478bd9Sstevel@tonic-gate 		restmp->ldmemcr_crc_key,
1395*7c478bd9Sstevel@tonic-gate 		restmp->ldmemcr_req_id.ldmemcrid_ld,
1396*7c478bd9Sstevel@tonic-gate 		restmp->ldmemcr_req_id.ldmemcrid_msgid );
1397*7c478bd9Sstevel@tonic-gate     }
1398*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "memcache 0x%x end of %s list.\n",
1399*7c478bd9Sstevel@tonic-gate 	    cache, name, 0 );
1400*7c478bd9Sstevel@tonic-gate }
1401*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */
1402*7c478bd9Sstevel@tonic-gate 
1403*7c478bd9Sstevel@tonic-gate /* Tells whether a cached result has expired. */
1404*7c478bd9Sstevel@tonic-gate static int
1405*7c478bd9Sstevel@tonic-gate memcache_expired(LDAPMemCache *cache, ldapmemcacheRes *pRes,
1406*7c478bd9Sstevel@tonic-gate                  unsigned long curTime)
1407*7c478bd9Sstevel@tonic-gate {
1408*7c478bd9Sstevel@tonic-gate     if (!cache->ldmemc_ttl)
1409*7c478bd9Sstevel@tonic-gate 	return 0;
1410*7c478bd9Sstevel@tonic-gate 
1411*7c478bd9Sstevel@tonic-gate     return ((unsigned long)difftime(
1412*7c478bd9Sstevel@tonic-gate 	                     (time_t)curTime,
1413*7c478bd9Sstevel@tonic-gate 			     (time_t)(pRes->ldmemcr_timestamp)) >=
1414*7c478bd9Sstevel@tonic-gate 			                          cache->ldmemc_ttl);
1415*7c478bd9Sstevel@tonic-gate }
1416*7c478bd9Sstevel@tonic-gate 
1417*7c478bd9Sstevel@tonic-gate /* Operates the cache in a central place. */
1418*7c478bd9Sstevel@tonic-gate static int
1419*7c478bd9Sstevel@tonic-gate memcache_access(LDAPMemCache *cache, int mode,
1420*7c478bd9Sstevel@tonic-gate 			   void *pData1, void *pData2, void *pData3)
1421*7c478bd9Sstevel@tonic-gate {
1422*7c478bd9Sstevel@tonic-gate     int nRes = LDAP_SUCCESS;
1423*7c478bd9Sstevel@tonic-gate     unsigned long size = 0;
1424*7c478bd9Sstevel@tonic-gate 
1425*7c478bd9Sstevel@tonic-gate     /* Add a new cache header to the cache. */
1426*7c478bd9Sstevel@tonic-gate     if (mode == MEMCACHE_ACCESS_ADD) {
1427*7c478bd9Sstevel@tonic-gate 	unsigned long key = *((unsigned long*)pData1);
1428*7c478bd9Sstevel@tonic-gate 	char *basedn = (char*)pData3;
1429*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pRes = NULL;
1430*7c478bd9Sstevel@tonic-gate 
1431*7c478bd9Sstevel@tonic-gate 	nRes = htable_get(cache->ldmemc_resTmp, pData2, (void**)&pRes);
1432*7c478bd9Sstevel@tonic-gate 	if (nRes == LDAP_SUCCESS)
1433*7c478bd9Sstevel@tonic-gate 	    return( LDAP_ALREADY_EXISTS );
1434*7c478bd9Sstevel@tonic-gate 
1435*7c478bd9Sstevel@tonic-gate 	pRes = (ldapmemcacheRes*)NSLDAPI_CALLOC(1, sizeof(ldapmemcacheRes));
1436*7c478bd9Sstevel@tonic-gate 	if (pRes == NULL)
1437*7c478bd9Sstevel@tonic-gate 	    return( LDAP_NO_MEMORY );
1438*7c478bd9Sstevel@tonic-gate 
1439*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_crc_key = key;
1440*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_req_id = *((ldapmemcacheReqId*)pData2);
1441*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_basedn = (basedn ? nsldapi_strdup(basedn) : NULL);
1442*7c478bd9Sstevel@tonic-gate 
1443*7c478bd9Sstevel@tonic-gate 	size += sizeof(ldapmemcacheRes) + strlen(basedn) + 1;
1444*7c478bd9Sstevel@tonic-gate 	nRes = memcache_adj_size(cache, size, MEMCACHE_SIZE_ENTRIES,
1445*7c478bd9Sstevel@tonic-gate 	                         MEMCACHE_SIZE_ADD);
1446*7c478bd9Sstevel@tonic-gate 	if (nRes == LDAP_SUCCESS)
1447*7c478bd9Sstevel@tonic-gate 	    nRes = htable_put(cache->ldmemc_resTmp, pData2, (void*)pRes);
1448*7c478bd9Sstevel@tonic-gate 	if (nRes == LDAP_SUCCESS)
1449*7c478bd9Sstevel@tonic-gate 	    memcache_add_to_list(cache, pRes, LIST_TMP);
1450*7c478bd9Sstevel@tonic-gate 	else
1451*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pRes);
1452*7c478bd9Sstevel@tonic-gate     }
1453*7c478bd9Sstevel@tonic-gate     /* Append entries to an existing cache header. */
1454*7c478bd9Sstevel@tonic-gate     else if ((mode == MEMCACHE_ACCESS_APPEND) ||
1455*7c478bd9Sstevel@tonic-gate 	     (mode == MEMCACHE_ACCESS_APPEND_LAST)) {
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate 	LDAPMessage *pMsg = (LDAPMessage*)pData2;
1458*7c478bd9Sstevel@tonic-gate 	LDAPMessage *pCopy = NULL;
1459*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pRes = NULL;
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 	nRes = htable_get(cache->ldmemc_resTmp, pData1, (void**)&pRes);
1462*7c478bd9Sstevel@tonic-gate 	if (nRes != LDAP_SUCCESS)
1463*7c478bd9Sstevel@tonic-gate 	    return nRes;
1464*7c478bd9Sstevel@tonic-gate 
1465*7c478bd9Sstevel@tonic-gate 	nRes = memcache_dup_message(pMsg, pMsg->lm_msgid, 0, &pCopy, &size);
1466*7c478bd9Sstevel@tonic-gate 	if (nRes != LDAP_SUCCESS) {
1467*7c478bd9Sstevel@tonic-gate 	    nRes = htable_remove(cache->ldmemc_resTmp, pData1, NULL);
1468*7c478bd9Sstevel@tonic-gate 	    assert(nRes == LDAP_SUCCESS);
1469*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pRes, LIST_TMP);
1470*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pRes);
1471*7c478bd9Sstevel@tonic-gate 	    return nRes;
1472*7c478bd9Sstevel@tonic-gate 	}
1473*7c478bd9Sstevel@tonic-gate 
1474*7c478bd9Sstevel@tonic-gate 	nRes = memcache_adj_size(cache, size, MEMCACHE_SIZE_ENTRIES,
1475*7c478bd9Sstevel@tonic-gate 	                         MEMCACHE_SIZE_ADD);
1476*7c478bd9Sstevel@tonic-gate 	if (nRes != LDAP_SUCCESS) {
1477*7c478bd9Sstevel@tonic-gate 	    ldap_msgfree(pCopy);
1478*7c478bd9Sstevel@tonic-gate 	    nRes = htable_remove(cache->ldmemc_resTmp, pData1, NULL);
1479*7c478bd9Sstevel@tonic-gate 	    assert(nRes == LDAP_SUCCESS);
1480*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pRes, LIST_TMP);
1481*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pRes);
1482*7c478bd9Sstevel@tonic-gate 	    return nRes;
1483*7c478bd9Sstevel@tonic-gate 	}
1484*7c478bd9Sstevel@tonic-gate 
1485*7c478bd9Sstevel@tonic-gate 	memcache_add_res_to_list(pRes, pCopy, size);
1486*7c478bd9Sstevel@tonic-gate 
1487*7c478bd9Sstevel@tonic-gate 	if (mode == MEMCACHE_ACCESS_APPEND)
1488*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
1489*7c478bd9Sstevel@tonic-gate 
1490*7c478bd9Sstevel@tonic-gate 	nRes = htable_remove(cache->ldmemc_resTmp, pData1, NULL);
1491*7c478bd9Sstevel@tonic-gate 	assert(nRes == LDAP_SUCCESS);
1492*7c478bd9Sstevel@tonic-gate 	memcache_free_from_list(cache, pRes, LIST_TMP);
1493*7c478bd9Sstevel@tonic-gate 	(pRes->ldmemcr_req_id).ldmemcrid_ld = NULL;
1494*7c478bd9Sstevel@tonic-gate 	(pRes->ldmemcr_req_id).ldmemcrid_msgid = -1;
1495*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_timestamp = (unsigned long)time(NULL);
1496*7c478bd9Sstevel@tonic-gate 
1497*7c478bd9Sstevel@tonic-gate 	if ((nRes = htable_put(cache->ldmemc_resLookup,
1498*7c478bd9Sstevel@tonic-gate 	                       (void*)&(pRes->ldmemcr_crc_key),
1499*7c478bd9Sstevel@tonic-gate                                (void*)pRes)) == LDAP_SUCCESS) {
1500*7c478bd9Sstevel@tonic-gate 	    memcache_add_to_list(cache, pRes, LIST_TTL);
1501*7c478bd9Sstevel@tonic-gate 	    memcache_add_to_list(cache, pRes, LIST_LRU);
1502*7c478bd9Sstevel@tonic-gate 	} else {
1503*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pRes);
1504*7c478bd9Sstevel@tonic-gate 	}
1505*7c478bd9Sstevel@tonic-gate     }
1506*7c478bd9Sstevel@tonic-gate     /* Search for cached entries for a particular search. */
1507*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_FIND) {
1508*7c478bd9Sstevel@tonic-gate 
1509*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes **ppRes = (ldapmemcacheRes**)pData2;
1510*7c478bd9Sstevel@tonic-gate 
1511*7c478bd9Sstevel@tonic-gate 	nRes = htable_get(cache->ldmemc_resLookup, pData1, (void**)ppRes);
1512*7c478bd9Sstevel@tonic-gate 	if (nRes != LDAP_SUCCESS)
1513*7c478bd9Sstevel@tonic-gate 	    return nRes;
1514*7c478bd9Sstevel@tonic-gate 
1515*7c478bd9Sstevel@tonic-gate 	if (!memcache_expired(cache, *ppRes, (unsigned long)time(0))) {
1516*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, *ppRes, LIST_LRU);
1517*7c478bd9Sstevel@tonic-gate 	    memcache_add_to_list(cache, *ppRes, LIST_LRU);
1518*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
1519*7c478bd9Sstevel@tonic-gate 	}
1520*7c478bd9Sstevel@tonic-gate 
1521*7c478bd9Sstevel@tonic-gate 	nRes = htable_remove(cache->ldmemc_resLookup, pData1, NULL);
1522*7c478bd9Sstevel@tonic-gate 	assert(nRes == LDAP_SUCCESS);
1523*7c478bd9Sstevel@tonic-gate 	memcache_free_from_list(cache, *ppRes, LIST_TTL);
1524*7c478bd9Sstevel@tonic-gate 	memcache_free_from_list(cache, *ppRes, LIST_LRU);
1525*7c478bd9Sstevel@tonic-gate 	memcache_free_entry(cache, *ppRes);
1526*7c478bd9Sstevel@tonic-gate 	nRes = LDAP_NO_SUCH_OBJECT;
1527*7c478bd9Sstevel@tonic-gate 	*ppRes = NULL;
1528*7c478bd9Sstevel@tonic-gate     }
1529*7c478bd9Sstevel@tonic-gate     /* Remove cached entries in the temporary cache. */
1530*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_DELETE) {
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pCurRes = NULL;
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate 	if ((nRes = htable_remove(cache->ldmemc_resTmp, pData1,
1535*7c478bd9Sstevel@tonic-gate 	                          (void**)&pCurRes)) == LDAP_SUCCESS) {
1536*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pCurRes, LIST_TMP);
1537*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pCurRes);
1538*7c478bd9Sstevel@tonic-gate 	}
1539*7c478bd9Sstevel@tonic-gate     }
1540*7c478bd9Sstevel@tonic-gate     /* Wipe out the temporary cache. */
1541*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_DELETE_ALL) {
1542*7c478bd9Sstevel@tonic-gate 
1543*7c478bd9Sstevel@tonic-gate 	nRes = htable_removeall(cache->ldmemc_resTmp, (void*)cache);
1544*7c478bd9Sstevel@tonic-gate     }
1545*7c478bd9Sstevel@tonic-gate     /* Remove expired entries from primary cache. */
1546*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_UPDATE) {
1547*7c478bd9Sstevel@tonic-gate 
1548*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pCurRes = cache->ldmemc_resTail[LIST_TTL];
1549*7c478bd9Sstevel@tonic-gate 	unsigned long curTime = (unsigned long)time(NULL);
1550*7c478bd9Sstevel@tonic-gate 
1551*7c478bd9Sstevel@tonic-gate 	for (; pCurRes; pCurRes = cache->ldmemc_resTail[LIST_TTL]) {
1552*7c478bd9Sstevel@tonic-gate 
1553*7c478bd9Sstevel@tonic-gate 	    if (!memcache_expired(cache, pCurRes, curTime))
1554*7c478bd9Sstevel@tonic-gate 		break;
1555*7c478bd9Sstevel@tonic-gate 
1556*7c478bd9Sstevel@tonic-gate 	    nRes = htable_remove(cache->ldmemc_resLookup,
1557*7c478bd9Sstevel@tonic-gate 		          (void*)&(pCurRes->ldmemcr_crc_key), NULL);
1558*7c478bd9Sstevel@tonic-gate 	    assert(nRes == LDAP_SUCCESS);
1559*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pCurRes, LIST_TTL);
1560*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pCurRes, LIST_LRU);
1561*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pCurRes);
1562*7c478bd9Sstevel@tonic-gate 	}
1563*7c478bd9Sstevel@tonic-gate     }
1564*7c478bd9Sstevel@tonic-gate     /* Wipe out the primary cache. */
1565*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_FLUSH_ALL) {
1566*7c478bd9Sstevel@tonic-gate 
1567*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pCurRes = cache->ldmemc_resHead[LIST_TTL];
1568*7c478bd9Sstevel@tonic-gate 
1569*7c478bd9Sstevel@tonic-gate 	nRes = htable_removeall(cache->ldmemc_resLookup, (void*)cache);
1570*7c478bd9Sstevel@tonic-gate 
1571*7c478bd9Sstevel@tonic-gate 	for (; pCurRes; pCurRes = cache->ldmemc_resHead[LIST_TTL]) {
1572*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pCurRes, LIST_LRU);
1573*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_resHead[LIST_TTL] =
1574*7c478bd9Sstevel@tonic-gate 		      cache->ldmemc_resHead[LIST_TTL]->ldmemcr_next[LIST_TTL];
1575*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pCurRes);
1576*7c478bd9Sstevel@tonic-gate 	}
1577*7c478bd9Sstevel@tonic-gate 	cache->ldmemc_resTail[LIST_TTL] = NULL;
1578*7c478bd9Sstevel@tonic-gate     }
1579*7c478bd9Sstevel@tonic-gate     /* Remove cached entries in both primary and temporary cache. */
1580*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_FLUSH) {
1581*7c478bd9Sstevel@tonic-gate 
1582*7c478bd9Sstevel@tonic-gate 	int i, list_id, bDone;
1583*7c478bd9Sstevel@tonic-gate 	int scope = (int)(uintptr_t)pData2;
1584*7c478bd9Sstevel@tonic-gate 	char *dn = (char*)pData1;
1585*7c478bd9Sstevel@tonic-gate 	char *dnTmp;
1586*7c478bd9Sstevel@tonic-gate 	BerElement ber;
1587*7c478bd9Sstevel@tonic-gate 	LDAPMessage *pMsg;
1588*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pRes;
1589*7c478bd9Sstevel@tonic-gate 
1590*7c478bd9Sstevel@tonic-gate 	if (cache->ldmemc_basedns) {
1591*7c478bd9Sstevel@tonic-gate 	    for (i = 0; cache->ldmemc_basedns[i]; i++) {
1592*7c478bd9Sstevel@tonic-gate 		if ((memcache_compare_dn(cache->ldmemc_basedns[i], dn,
1593*7c478bd9Sstevel@tonic-gate 			    LDAP_SCOPE_SUBTREE) == LDAP_COMPARE_TRUE) ||
1594*7c478bd9Sstevel@tonic-gate 		    (memcache_compare_dn(dn, cache->ldmemc_basedns[i],
1595*7c478bd9Sstevel@tonic-gate 			    LDAP_SCOPE_SUBTREE) == LDAP_COMPARE_TRUE))
1596*7c478bd9Sstevel@tonic-gate 		    break;
1597*7c478bd9Sstevel@tonic-gate 	    }
1598*7c478bd9Sstevel@tonic-gate 	    if (cache->ldmemc_basedns[i] == NULL)
1599*7c478bd9Sstevel@tonic-gate 		return( LDAP_SUCCESS );
1600*7c478bd9Sstevel@tonic-gate 	}
1601*7c478bd9Sstevel@tonic-gate 
1602*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
1603*7c478bd9Sstevel@tonic-gate 
1604*7c478bd9Sstevel@tonic-gate 	    list_id = (i == 0 ? LIST_TTL : LIST_TMP);
1605*7c478bd9Sstevel@tonic-gate 
1606*7c478bd9Sstevel@tonic-gate 	    for (pRes = cache->ldmemc_resHead[list_id]; pRes != NULL;
1607*7c478bd9Sstevel@tonic-gate 		 pRes = pRes->ldmemcr_next[list_id]) {
1608*7c478bd9Sstevel@tonic-gate 
1609*7c478bd9Sstevel@tonic-gate 		if ((memcache_compare_dn(pRes->ldmemcr_basedn, dn,
1610*7c478bd9Sstevel@tonic-gate 				 LDAP_SCOPE_SUBTREE) != LDAP_COMPARE_TRUE) &&
1611*7c478bd9Sstevel@tonic-gate 		    (memcache_compare_dn(dn, pRes->ldmemcr_basedn,
1612*7c478bd9Sstevel@tonic-gate 				 LDAP_SCOPE_SUBTREE) != LDAP_COMPARE_TRUE))
1613*7c478bd9Sstevel@tonic-gate 		    continue;
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate 		for (pMsg = pRes->ldmemcr_resHead, bDone = 0;
1616*7c478bd9Sstevel@tonic-gate 		     !bDone && pMsg; pMsg = pMsg->lm_chain) {
1617*7c478bd9Sstevel@tonic-gate 
1618*7c478bd9Sstevel@tonic-gate 		    if (!NSLDAPI_IS_SEARCH_ENTRY( pMsg->lm_msgtype ))
1619*7c478bd9Sstevel@tonic-gate 			continue;
1620*7c478bd9Sstevel@tonic-gate 
1621*7c478bd9Sstevel@tonic-gate 		    ber = *(pMsg->lm_ber);
1622*7c478bd9Sstevel@tonic-gate 		    if (ber_scanf(&ber, "{a", &dnTmp) != LBER_ERROR) {
1623*7c478bd9Sstevel@tonic-gate 			bDone = (memcache_compare_dn(dnTmp, dn, scope) ==
1624*7c478bd9Sstevel@tonic-gate 							    LDAP_COMPARE_TRUE);
1625*7c478bd9Sstevel@tonic-gate 			ldap_memfree(dnTmp);
1626*7c478bd9Sstevel@tonic-gate 		    }
1627*7c478bd9Sstevel@tonic-gate 		}
1628*7c478bd9Sstevel@tonic-gate 
1629*7c478bd9Sstevel@tonic-gate 		if (!bDone)
1630*7c478bd9Sstevel@tonic-gate 		    continue;
1631*7c478bd9Sstevel@tonic-gate 
1632*7c478bd9Sstevel@tonic-gate 		if (list_id == LIST_TTL) {
1633*7c478bd9Sstevel@tonic-gate 		    nRes = htable_remove(cache->ldmemc_resLookup,
1634*7c478bd9Sstevel@tonic-gate 			  	 (void*)&(pRes->ldmemcr_crc_key), NULL);
1635*7c478bd9Sstevel@tonic-gate 		    assert(nRes == LDAP_SUCCESS);
1636*7c478bd9Sstevel@tonic-gate 		    memcache_free_from_list(cache, pRes, LIST_TTL);
1637*7c478bd9Sstevel@tonic-gate 		    memcache_free_from_list(cache, pRes, LIST_LRU);
1638*7c478bd9Sstevel@tonic-gate 		} else {
1639*7c478bd9Sstevel@tonic-gate 		    nRes = htable_remove(cache->ldmemc_resTmp,
1640*7c478bd9Sstevel@tonic-gate 				  (void*)&(pRes->ldmemcr_req_id), NULL);
1641*7c478bd9Sstevel@tonic-gate 		    assert(nRes == LDAP_SUCCESS);
1642*7c478bd9Sstevel@tonic-gate 		    memcache_free_from_list(cache, pRes, LIST_TMP);
1643*7c478bd9Sstevel@tonic-gate 		}
1644*7c478bd9Sstevel@tonic-gate 		memcache_free_entry(cache, pRes);
1645*7c478bd9Sstevel@tonic-gate 	    }
1646*7c478bd9Sstevel@tonic-gate 	}
1647*7c478bd9Sstevel@tonic-gate     }
1648*7c478bd9Sstevel@tonic-gate     /* Flush least recently used entries from cache */
1649*7c478bd9Sstevel@tonic-gate     else if (mode == MEMCACHE_ACCESS_FLUSH_LRU) {
1650*7c478bd9Sstevel@tonic-gate 
1651*7c478bd9Sstevel@tonic-gate 	ldapmemcacheRes *pRes = cache->ldmemc_resTail[LIST_LRU];
1652*7c478bd9Sstevel@tonic-gate 
1653*7c478bd9Sstevel@tonic-gate 	if (pRes == NULL)
1654*7c478bd9Sstevel@tonic-gate 	    return LDAP_NO_SUCH_OBJECT;
1655*7c478bd9Sstevel@tonic-gate 
1656*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE,
1657*7c478bd9Sstevel@tonic-gate 		"memcache_access FLUSH_LRU: removing key 0x%8.8lx\n",
1658*7c478bd9Sstevel@tonic-gate 		pRes->ldmemcr_crc_key, 0, 0 );
1659*7c478bd9Sstevel@tonic-gate 	nRes = htable_remove(cache->ldmemc_resLookup,
1660*7c478bd9Sstevel@tonic-gate 	              (void*)&(pRes->ldmemcr_crc_key), NULL);
1661*7c478bd9Sstevel@tonic-gate 	assert(nRes == LDAP_SUCCESS);
1662*7c478bd9Sstevel@tonic-gate 	memcache_free_from_list(cache, pRes, LIST_TTL);
1663*7c478bd9Sstevel@tonic-gate 	memcache_free_from_list(cache, pRes, LIST_LRU);
1664*7c478bd9Sstevel@tonic-gate 	memcache_free_entry(cache, pRes);
1665*7c478bd9Sstevel@tonic-gate     }
1666*7c478bd9Sstevel@tonic-gate     /* Unknown command */
1667*7c478bd9Sstevel@tonic-gate     else {
1668*7c478bd9Sstevel@tonic-gate 	nRes = LDAP_PARAM_ERROR;
1669*7c478bd9Sstevel@tonic-gate     }
1670*7c478bd9Sstevel@tonic-gate 
1671*7c478bd9Sstevel@tonic-gate     return nRes;
1672*7c478bd9Sstevel@tonic-gate }
1673*7c478bd9Sstevel@tonic-gate 
1674*7c478bd9Sstevel@tonic-gate 
1675*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
1676*7c478bd9Sstevel@tonic-gate static void
1677*7c478bd9Sstevel@tonic-gate memcache_report_statistics( LDAPMemCache *cache )
1678*7c478bd9Sstevel@tonic-gate {
1679*7c478bd9Sstevel@tonic-gate     unsigned long	hitrate;
1680*7c478bd9Sstevel@tonic-gate 
1681*7c478bd9Sstevel@tonic-gate     if ( cache->ldmemc_stats.ldmemcstat_tries == 0 ) {
1682*7c478bd9Sstevel@tonic-gate 	hitrate = 0;
1683*7c478bd9Sstevel@tonic-gate     } else {
1684*7c478bd9Sstevel@tonic-gate 	hitrate = ( 100L * cache->ldmemc_stats.ldmemcstat_hits ) /
1685*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_stats.ldmemcstat_tries;
1686*7c478bd9Sstevel@tonic-gate     }
1687*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_STATS, "memcache 0x%x:\n", cache, 0, 0 );
1688*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_STATS, "    tries: %ld  hits: %ld  hitrate: %ld%%\n",
1689*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_stats.ldmemcstat_tries,
1690*7c478bd9Sstevel@tonic-gate 	    cache->ldmemc_stats.ldmemcstat_hits, hitrate );
1691*7c478bd9Sstevel@tonic-gate     if ( cache->ldmemc_size <= 0 ) {	/* no size limit */
1692*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_STATS, "    memory bytes used: %ld\n",
1693*7c478bd9Sstevel@tonic-gate 		cache->ldmemc_size_used, 0, 0 );
1694*7c478bd9Sstevel@tonic-gate     } else {
1695*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_STATS, "    memory bytes used: %ld free: %ld\n",
1696*7c478bd9Sstevel@tonic-gate 		cache->ldmemc_size_used,
1697*7c478bd9Sstevel@tonic-gate 		cache->ldmemc_size - cache->ldmemc_size_used, 0 );
1698*7c478bd9Sstevel@tonic-gate     }
1699*7c478bd9Sstevel@tonic-gate }
1700*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */
1701*7c478bd9Sstevel@tonic-gate 
1702*7c478bd9Sstevel@tonic-gate /************************ Hash Table Functions *****************************/
1703*7c478bd9Sstevel@tonic-gate 
1704*7c478bd9Sstevel@tonic-gate /* Calculates size (# of entries) of hash table given the size limit for
1705*7c478bd9Sstevel@tonic-gate    the cache. */
1706*7c478bd9Sstevel@tonic-gate static int
1707*7c478bd9Sstevel@tonic-gate htable_calculate_size(int sizelimit)
1708*7c478bd9Sstevel@tonic-gate {
1709*7c478bd9Sstevel@tonic-gate     int i, j;
1710*7c478bd9Sstevel@tonic-gate     int size = (int)(((double)sizelimit /
1711*7c478bd9Sstevel@tonic-gate 	                (double)(sizeof(BerElement) + EXTRA_SIZE)) / 1.5);
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate     /* Get a prime # */
1714*7c478bd9Sstevel@tonic-gate     size = (size & 0x1 ? size : size + 1);
1715*7c478bd9Sstevel@tonic-gate     for (i = 3, j = size / 2; i < j; i++) {
1716*7c478bd9Sstevel@tonic-gate 	if ((size % i) == 0) {
1717*7c478bd9Sstevel@tonic-gate 	    size += 2;
1718*7c478bd9Sstevel@tonic-gate 	    i = 3;
1719*7c478bd9Sstevel@tonic-gate 	    j = size / 2;
1720*7c478bd9Sstevel@tonic-gate 	}
1721*7c478bd9Sstevel@tonic-gate     }
1722*7c478bd9Sstevel@tonic-gate 
1723*7c478bd9Sstevel@tonic-gate     return size;
1724*7c478bd9Sstevel@tonic-gate }
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate /* Returns the size in bytes of the given hash table. */
1727*7c478bd9Sstevel@tonic-gate static int
1728*7c478bd9Sstevel@tonic-gate htable_sizeinbytes(HashTable *pTable)
1729*7c478bd9Sstevel@tonic-gate {
1730*7c478bd9Sstevel@tonic-gate     if (!pTable)
1731*7c478bd9Sstevel@tonic-gate 	return 0;
1732*7c478bd9Sstevel@tonic-gate 
1733*7c478bd9Sstevel@tonic-gate     return (pTable->size * sizeof(HashTableNode));
1734*7c478bd9Sstevel@tonic-gate }
1735*7c478bd9Sstevel@tonic-gate 
1736*7c478bd9Sstevel@tonic-gate /* Inserts an item into the hash table. */
1737*7c478bd9Sstevel@tonic-gate static int
1738*7c478bd9Sstevel@tonic-gate htable_put(HashTable *pTable, void *key, void *pData)
1739*7c478bd9Sstevel@tonic-gate {
1740*7c478bd9Sstevel@tonic-gate     int index = pTable->hashfunc(pTable->size, key);
1741*7c478bd9Sstevel@tonic-gate 
1742*7c478bd9Sstevel@tonic-gate     if (index >= 0 && index < pTable->size)
1743*7c478bd9Sstevel@tonic-gate 	return pTable->putdata(&(pTable->table[index].pData), key, pData);
1744*7c478bd9Sstevel@tonic-gate 
1745*7c478bd9Sstevel@tonic-gate     return( LDAP_OPERATIONS_ERROR );
1746*7c478bd9Sstevel@tonic-gate }
1747*7c478bd9Sstevel@tonic-gate 
1748*7c478bd9Sstevel@tonic-gate /* Retrieves an item from the hash table. */
1749*7c478bd9Sstevel@tonic-gate static int
1750*7c478bd9Sstevel@tonic-gate htable_get(HashTable *pTable, void *key, void **ppData)
1751*7c478bd9Sstevel@tonic-gate {
1752*7c478bd9Sstevel@tonic-gate     int index = pTable->hashfunc(pTable->size, key);
1753*7c478bd9Sstevel@tonic-gate 
1754*7c478bd9Sstevel@tonic-gate     *ppData = NULL;
1755*7c478bd9Sstevel@tonic-gate 
1756*7c478bd9Sstevel@tonic-gate     if (index >= 0 && index < pTable->size)
1757*7c478bd9Sstevel@tonic-gate 	return pTable->getdata(pTable->table[index].pData, key, ppData);
1758*7c478bd9Sstevel@tonic-gate 
1759*7c478bd9Sstevel@tonic-gate     return( LDAP_OPERATIONS_ERROR );
1760*7c478bd9Sstevel@tonic-gate }
1761*7c478bd9Sstevel@tonic-gate 
1762*7c478bd9Sstevel@tonic-gate /* Performs a miscellaneous operation on a hash table entry. */
1763*7c478bd9Sstevel@tonic-gate static int
1764*7c478bd9Sstevel@tonic-gate htable_misc(HashTable *pTable, void *key, void *pData)
1765*7c478bd9Sstevel@tonic-gate {
1766*7c478bd9Sstevel@tonic-gate     if (pTable->miscfunc) {
1767*7c478bd9Sstevel@tonic-gate 	int index = pTable->hashfunc(pTable->size, key);
1768*7c478bd9Sstevel@tonic-gate 	if (index >= 0 && index < pTable->size)
1769*7c478bd9Sstevel@tonic-gate 	    return pTable->miscfunc(&(pTable->table[index].pData), key, pData);
1770*7c478bd9Sstevel@tonic-gate     }
1771*7c478bd9Sstevel@tonic-gate 
1772*7c478bd9Sstevel@tonic-gate     return( LDAP_OPERATIONS_ERROR );
1773*7c478bd9Sstevel@tonic-gate }
1774*7c478bd9Sstevel@tonic-gate 
1775*7c478bd9Sstevel@tonic-gate /* Removes an item from the hash table. */
1776*7c478bd9Sstevel@tonic-gate static int
1777*7c478bd9Sstevel@tonic-gate htable_remove(HashTable *pTable, void *key, void **ppData)
1778*7c478bd9Sstevel@tonic-gate {
1779*7c478bd9Sstevel@tonic-gate     int index = pTable->hashfunc(pTable->size, key);
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate     if (ppData)
1782*7c478bd9Sstevel@tonic-gate 	*ppData = NULL;
1783*7c478bd9Sstevel@tonic-gate 
1784*7c478bd9Sstevel@tonic-gate     if (index >= 0 && index < pTable->size)
1785*7c478bd9Sstevel@tonic-gate 	return pTable->removedata(&(pTable->table[index].pData), key, ppData);
1786*7c478bd9Sstevel@tonic-gate 
1787*7c478bd9Sstevel@tonic-gate     return( LDAP_OPERATIONS_ERROR );
1788*7c478bd9Sstevel@tonic-gate }
1789*7c478bd9Sstevel@tonic-gate 
1790*7c478bd9Sstevel@tonic-gate /* Removes everything in the hash table. */
1791*7c478bd9Sstevel@tonic-gate static int
1792*7c478bd9Sstevel@tonic-gate htable_removeall(HashTable *pTable, void *pData)
1793*7c478bd9Sstevel@tonic-gate {
1794*7c478bd9Sstevel@tonic-gate     int i;
1795*7c478bd9Sstevel@tonic-gate 
1796*7c478bd9Sstevel@tonic-gate     for (i = 0; i < pTable->size; i++)
1797*7c478bd9Sstevel@tonic-gate 	pTable->clrtablenode(&(pTable->table[i].pData), pData);
1798*7c478bd9Sstevel@tonic-gate 
1799*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1800*7c478bd9Sstevel@tonic-gate }
1801*7c478bd9Sstevel@tonic-gate 
1802*7c478bd9Sstevel@tonic-gate /* Creates a new hash table. */
1803*7c478bd9Sstevel@tonic-gate static int
1804*7c478bd9Sstevel@tonic-gate htable_create(int size_limit, HashFuncPtr hashf,
1805*7c478bd9Sstevel@tonic-gate 			 PutDataPtr putDataf, GetDataPtr getDataf,
1806*7c478bd9Sstevel@tonic-gate 			 RemoveDataPtr removeDataf, ClrTableNodePtr clrNodef,
1807*7c478bd9Sstevel@tonic-gate 			 MiscFuncPtr miscOpf, HashTable **ppTable)
1808*7c478bd9Sstevel@tonic-gate {
1809*7c478bd9Sstevel@tonic-gate     size_limit = htable_calculate_size(size_limit);
1810*7c478bd9Sstevel@tonic-gate 
1811*7c478bd9Sstevel@tonic-gate     if ((*ppTable = (HashTable*)NSLDAPI_CALLOC(1, sizeof(HashTable))) == NULL)
1812*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_MEMORY );
1813*7c478bd9Sstevel@tonic-gate 
1814*7c478bd9Sstevel@tonic-gate     (*ppTable)->table = (HashTableNode*)NSLDAPI_CALLOC(size_limit,
1815*7c478bd9Sstevel@tonic-gate 	                                       sizeof(HashTableNode));
1816*7c478bd9Sstevel@tonic-gate     if ((*ppTable)->table == NULL) {
1817*7c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE(*ppTable);
1818*7c478bd9Sstevel@tonic-gate 	*ppTable = NULL;
1819*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_MEMORY );
1820*7c478bd9Sstevel@tonic-gate     }
1821*7c478bd9Sstevel@tonic-gate 
1822*7c478bd9Sstevel@tonic-gate     (*ppTable)->size = size_limit;
1823*7c478bd9Sstevel@tonic-gate     (*ppTable)->hashfunc = hashf;
1824*7c478bd9Sstevel@tonic-gate     (*ppTable)->putdata = putDataf;
1825*7c478bd9Sstevel@tonic-gate     (*ppTable)->getdata = getDataf;
1826*7c478bd9Sstevel@tonic-gate     (*ppTable)->miscfunc = miscOpf;
1827*7c478bd9Sstevel@tonic-gate     (*ppTable)->removedata = removeDataf;
1828*7c478bd9Sstevel@tonic-gate     (*ppTable)->clrtablenode = clrNodef;
1829*7c478bd9Sstevel@tonic-gate 
1830*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1831*7c478bd9Sstevel@tonic-gate }
1832*7c478bd9Sstevel@tonic-gate 
1833*7c478bd9Sstevel@tonic-gate /* Destroys a hash table. */
1834*7c478bd9Sstevel@tonic-gate static int
1835*7c478bd9Sstevel@tonic-gate htable_free(HashTable *pTable)
1836*7c478bd9Sstevel@tonic-gate {
1837*7c478bd9Sstevel@tonic-gate     NSLDAPI_FREE(pTable->table);
1838*7c478bd9Sstevel@tonic-gate     NSLDAPI_FREE(pTable);
1839*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1840*7c478bd9Sstevel@tonic-gate }
1841*7c478bd9Sstevel@tonic-gate 
1842*7c478bd9Sstevel@tonic-gate /**************** Hash table callbacks for temporary cache ****************/
1843*7c478bd9Sstevel@tonic-gate 
1844*7c478bd9Sstevel@tonic-gate /* Hash function */
1845*7c478bd9Sstevel@tonic-gate static int
1846*7c478bd9Sstevel@tonic-gate msgid_hashf(int table_size, void *key)
1847*7c478bd9Sstevel@tonic-gate {
1848*7c478bd9Sstevel@tonic-gate     uint_t code = (uint_t)(uintptr_t)((ldapmemcacheReqId*)key)->ldmemcrid_ld;
1849*7c478bd9Sstevel@tonic-gate     return (((code << 20) + (code >> 12)) % table_size);
1850*7c478bd9Sstevel@tonic-gate }
1851*7c478bd9Sstevel@tonic-gate 
1852*7c478bd9Sstevel@tonic-gate /* Called by hash table to insert an item. */
1853*7c478bd9Sstevel@tonic-gate static int
1854*7c478bd9Sstevel@tonic-gate msgid_putdata(void **ppTableData, void *key, void *pData)
1855*7c478bd9Sstevel@tonic-gate {
1856*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId *pReqId = (ldapmemcacheReqId*)key;
1857*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pRes = (ldapmemcacheRes*)pData;
1858*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes **ppHead = (ldapmemcacheRes**)ppTableData;
1859*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pCurRes = *ppHead;
1860*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pPrev = NULL;
1861*7c478bd9Sstevel@tonic-gate 
1862*7c478bd9Sstevel@tonic-gate     for (; pCurRes; pCurRes = pCurRes->ldmemcr_htable_next) {
1863*7c478bd9Sstevel@tonic-gate 	if ((pCurRes->ldmemcr_req_id).ldmemcrid_ld == pReqId->ldmemcrid_ld)
1864*7c478bd9Sstevel@tonic-gate 	    break;
1865*7c478bd9Sstevel@tonic-gate 	pPrev = pCurRes;
1866*7c478bd9Sstevel@tonic-gate     }
1867*7c478bd9Sstevel@tonic-gate 
1868*7c478bd9Sstevel@tonic-gate     if (pCurRes) {
1869*7c478bd9Sstevel@tonic-gate 	for (; pCurRes; pCurRes = pCurRes->ldmemcr_next[LIST_TTL]) {
1870*7c478bd9Sstevel@tonic-gate 	    if ((pCurRes->ldmemcr_req_id).ldmemcrid_msgid ==
1871*7c478bd9Sstevel@tonic-gate 		                               pReqId->ldmemcrid_msgid)
1872*7c478bd9Sstevel@tonic-gate 		return( LDAP_ALREADY_EXISTS );
1873*7c478bd9Sstevel@tonic-gate 	    pPrev = pCurRes;
1874*7c478bd9Sstevel@tonic-gate 	}
1875*7c478bd9Sstevel@tonic-gate 	pPrev->ldmemcr_next[LIST_TTL] = pRes;
1876*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_prev[LIST_TTL] = pPrev;
1877*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_next[LIST_TTL] = NULL;
1878*7c478bd9Sstevel@tonic-gate     } else {
1879*7c478bd9Sstevel@tonic-gate 	if (pPrev)
1880*7c478bd9Sstevel@tonic-gate 	    pPrev->ldmemcr_htable_next = pRes;
1881*7c478bd9Sstevel@tonic-gate 	else
1882*7c478bd9Sstevel@tonic-gate 	    *ppHead = pRes;
1883*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_htable_next = NULL;
1884*7c478bd9Sstevel@tonic-gate     }
1885*7c478bd9Sstevel@tonic-gate 
1886*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1887*7c478bd9Sstevel@tonic-gate }
1888*7c478bd9Sstevel@tonic-gate 
1889*7c478bd9Sstevel@tonic-gate /* Called by hash table to retrieve an item. */
1890*7c478bd9Sstevel@tonic-gate static int
1891*7c478bd9Sstevel@tonic-gate msgid_getdata(void *pTableData, void *key, void **ppData)
1892*7c478bd9Sstevel@tonic-gate {
1893*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId *pReqId = (ldapmemcacheReqId*)key;
1894*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pCurRes = (ldapmemcacheRes*)pTableData;
1895*7c478bd9Sstevel@tonic-gate 
1896*7c478bd9Sstevel@tonic-gate     *ppData = NULL;
1897*7c478bd9Sstevel@tonic-gate 
1898*7c478bd9Sstevel@tonic-gate     for (; pCurRes; pCurRes = pCurRes->ldmemcr_htable_next) {
1899*7c478bd9Sstevel@tonic-gate 	if ((pCurRes->ldmemcr_req_id).ldmemcrid_ld == pReqId->ldmemcrid_ld)
1900*7c478bd9Sstevel@tonic-gate 	    break;
1901*7c478bd9Sstevel@tonic-gate     }
1902*7c478bd9Sstevel@tonic-gate 
1903*7c478bd9Sstevel@tonic-gate     if (!pCurRes)
1904*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_SUCH_OBJECT );
1905*7c478bd9Sstevel@tonic-gate 
1906*7c478bd9Sstevel@tonic-gate     for (; pCurRes; pCurRes = pCurRes->ldmemcr_next[LIST_TTL]) {
1907*7c478bd9Sstevel@tonic-gate 	if ((pCurRes->ldmemcr_req_id).ldmemcrid_msgid ==
1908*7c478bd9Sstevel@tonic-gate 	                                  pReqId->ldmemcrid_msgid) {
1909*7c478bd9Sstevel@tonic-gate 	    *ppData = (void*)pCurRes;
1910*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
1911*7c478bd9Sstevel@tonic-gate 	}
1912*7c478bd9Sstevel@tonic-gate     }
1913*7c478bd9Sstevel@tonic-gate 
1914*7c478bd9Sstevel@tonic-gate     return( LDAP_NO_SUCH_OBJECT );
1915*7c478bd9Sstevel@tonic-gate }
1916*7c478bd9Sstevel@tonic-gate 
1917*7c478bd9Sstevel@tonic-gate /* Called by hash table to remove an item. */
1918*7c478bd9Sstevel@tonic-gate static int
1919*7c478bd9Sstevel@tonic-gate msgid_removedata(void **ppTableData, void *key, void **ppData)
1920*7c478bd9Sstevel@tonic-gate {
1921*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pHead = *((ldapmemcacheRes**)ppTableData);
1922*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pCurRes = NULL;
1923*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pPrev = NULL;
1924*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId *pReqId = (ldapmemcacheReqId*)key;
1925*7c478bd9Sstevel@tonic-gate 
1926*7c478bd9Sstevel@tonic-gate     if (ppData)
1927*7c478bd9Sstevel@tonic-gate 	*ppData = NULL;
1928*7c478bd9Sstevel@tonic-gate 
1929*7c478bd9Sstevel@tonic-gate     for (; pHead; pHead = pHead->ldmemcr_htable_next) {
1930*7c478bd9Sstevel@tonic-gate 	if ((pHead->ldmemcr_req_id).ldmemcrid_ld == pReqId->ldmemcrid_ld)
1931*7c478bd9Sstevel@tonic-gate 	    break;
1932*7c478bd9Sstevel@tonic-gate 	pPrev = pHead;
1933*7c478bd9Sstevel@tonic-gate     }
1934*7c478bd9Sstevel@tonic-gate 
1935*7c478bd9Sstevel@tonic-gate     if (!pHead)
1936*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_SUCH_OBJECT );
1937*7c478bd9Sstevel@tonic-gate 
1938*7c478bd9Sstevel@tonic-gate     for (pCurRes = pHead; pCurRes; pCurRes = pCurRes->ldmemcr_next[LIST_TTL]) {
1939*7c478bd9Sstevel@tonic-gate 	if ((pCurRes->ldmemcr_req_id).ldmemcrid_msgid ==
1940*7c478bd9Sstevel@tonic-gate 	                                        pReqId->ldmemcrid_msgid)
1941*7c478bd9Sstevel@tonic-gate 	    break;
1942*7c478bd9Sstevel@tonic-gate     }
1943*7c478bd9Sstevel@tonic-gate 
1944*7c478bd9Sstevel@tonic-gate     if (!pCurRes)
1945*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_SUCH_OBJECT );
1946*7c478bd9Sstevel@tonic-gate 
1947*7c478bd9Sstevel@tonic-gate     if (ppData) {
1948*7c478bd9Sstevel@tonic-gate 	pCurRes->ldmemcr_next[LIST_TTL] = NULL;
1949*7c478bd9Sstevel@tonic-gate 	pCurRes->ldmemcr_prev[LIST_TTL] = NULL;
1950*7c478bd9Sstevel@tonic-gate 	pCurRes->ldmemcr_htable_next = NULL;
1951*7c478bd9Sstevel@tonic-gate 	*ppData = (void*)pCurRes;
1952*7c478bd9Sstevel@tonic-gate     }
1953*7c478bd9Sstevel@tonic-gate 
1954*7c478bd9Sstevel@tonic-gate     if (pCurRes != pHead) {
1955*7c478bd9Sstevel@tonic-gate 	if (pCurRes->ldmemcr_prev[LIST_TTL])
1956*7c478bd9Sstevel@tonic-gate 	    pCurRes->ldmemcr_prev[LIST_TTL]->ldmemcr_next[LIST_TTL] =
1957*7c478bd9Sstevel@tonic-gate 	                                     pCurRes->ldmemcr_next[LIST_TTL];
1958*7c478bd9Sstevel@tonic-gate 	if (pCurRes->ldmemcr_next[LIST_TTL])
1959*7c478bd9Sstevel@tonic-gate 	    pCurRes->ldmemcr_next[LIST_TTL]->ldmemcr_prev[LIST_TTL] =
1960*7c478bd9Sstevel@tonic-gate 	                                     pCurRes->ldmemcr_prev[LIST_TTL];
1961*7c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
1962*7c478bd9Sstevel@tonic-gate     }
1963*7c478bd9Sstevel@tonic-gate 
1964*7c478bd9Sstevel@tonic-gate     if (pPrev) {
1965*7c478bd9Sstevel@tonic-gate 	if (pHead->ldmemcr_next[LIST_TTL]) {
1966*7c478bd9Sstevel@tonic-gate 	    pPrev->ldmemcr_htable_next = pHead->ldmemcr_next[LIST_TTL];
1967*7c478bd9Sstevel@tonic-gate 	    pHead->ldmemcr_next[LIST_TTL]->ldmemcr_htable_next =
1968*7c478bd9Sstevel@tonic-gate 			                   pHead->ldmemcr_htable_next;
1969*7c478bd9Sstevel@tonic-gate 	} else {
1970*7c478bd9Sstevel@tonic-gate 	    pPrev->ldmemcr_htable_next = pHead->ldmemcr_htable_next;
1971*7c478bd9Sstevel@tonic-gate 	}
1972*7c478bd9Sstevel@tonic-gate     } else {
1973*7c478bd9Sstevel@tonic-gate 	if (pHead->ldmemcr_next[LIST_TTL]) {
1974*7c478bd9Sstevel@tonic-gate 	    *((ldapmemcacheRes**)ppTableData) = pHead->ldmemcr_next[LIST_TTL];
1975*7c478bd9Sstevel@tonic-gate 	    pHead->ldmemcr_next[LIST_TTL]->ldmemcr_htable_next =
1976*7c478bd9Sstevel@tonic-gate 			                   pHead->ldmemcr_htable_next;
1977*7c478bd9Sstevel@tonic-gate 	} else {
1978*7c478bd9Sstevel@tonic-gate 	    *((ldapmemcacheRes**)ppTableData) = pHead->ldmemcr_htable_next;
1979*7c478bd9Sstevel@tonic-gate 	}
1980*7c478bd9Sstevel@tonic-gate     }
1981*7c478bd9Sstevel@tonic-gate 
1982*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
1983*7c478bd9Sstevel@tonic-gate }
1984*7c478bd9Sstevel@tonic-gate 
1985*7c478bd9Sstevel@tonic-gate /* Called by hash table to remove all cached entries associated to searches
1986*7c478bd9Sstevel@tonic-gate    being performed using the given ldap handle. */
1987*7c478bd9Sstevel@tonic-gate static int
1988*7c478bd9Sstevel@tonic-gate msgid_clear_ld_items(void **ppTableData, void *key, void *pData)
1989*7c478bd9Sstevel@tonic-gate {
1990*7c478bd9Sstevel@tonic-gate     LDAPMemCache *cache = (LDAPMemCache*)pData;
1991*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pHead = *((ldapmemcacheRes**)ppTableData);
1992*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pPrev = NULL;
1993*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pCurRes = NULL;
1994*7c478bd9Sstevel@tonic-gate     ldapmemcacheReqId *pReqId = (ldapmemcacheReqId*)key;
1995*7c478bd9Sstevel@tonic-gate 
1996*7c478bd9Sstevel@tonic-gate     for (; pHead; pHead = pHead->ldmemcr_htable_next) {
1997*7c478bd9Sstevel@tonic-gate 	if ((pHead->ldmemcr_req_id).ldmemcrid_ld == pReqId->ldmemcrid_ld)
1998*7c478bd9Sstevel@tonic-gate 	    break;
1999*7c478bd9Sstevel@tonic-gate 	pPrev = pHead;
2000*7c478bd9Sstevel@tonic-gate     }
2001*7c478bd9Sstevel@tonic-gate 
2002*7c478bd9Sstevel@tonic-gate     if (!pHead)
2003*7c478bd9Sstevel@tonic-gate 	return( LDAP_NO_SUCH_OBJECT );
2004*7c478bd9Sstevel@tonic-gate 
2005*7c478bd9Sstevel@tonic-gate     if (pPrev)
2006*7c478bd9Sstevel@tonic-gate 	pPrev->ldmemcr_htable_next = pHead->ldmemcr_htable_next;
2007*7c478bd9Sstevel@tonic-gate     else
2008*7c478bd9Sstevel@tonic-gate 	*((ldapmemcacheRes**)ppTableData) = pHead->ldmemcr_htable_next;
2009*7c478bd9Sstevel@tonic-gate 
2010*7c478bd9Sstevel@tonic-gate     for (pCurRes = pHead; pHead; pCurRes = pHead) {
2011*7c478bd9Sstevel@tonic-gate 	pHead = pHead->ldmemcr_next[LIST_TTL];
2012*7c478bd9Sstevel@tonic-gate 	memcache_free_from_list(cache, pCurRes, LIST_TMP);
2013*7c478bd9Sstevel@tonic-gate 	memcache_free_entry(cache, pCurRes);
2014*7c478bd9Sstevel@tonic-gate     }
2015*7c478bd9Sstevel@tonic-gate 
2016*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
2017*7c478bd9Sstevel@tonic-gate }
2018*7c478bd9Sstevel@tonic-gate 
2019*7c478bd9Sstevel@tonic-gate /* Called by hash table for removing all items in the table. */
2020*7c478bd9Sstevel@tonic-gate static void
2021*7c478bd9Sstevel@tonic-gate msgid_clearnode(void **ppTableData, void *pData)
2022*7c478bd9Sstevel@tonic-gate {
2023*7c478bd9Sstevel@tonic-gate     LDAPMemCache *cache = (LDAPMemCache*)pData;
2024*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes **ppHead = (ldapmemcacheRes**)ppTableData;
2025*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pSubHead = *ppHead;
2026*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pCurRes = NULL;
2027*7c478bd9Sstevel@tonic-gate 
2028*7c478bd9Sstevel@tonic-gate     for (; *ppHead; pSubHead = *ppHead) {
2029*7c478bd9Sstevel@tonic-gate 	ppHead = &((*ppHead)->ldmemcr_htable_next);
2030*7c478bd9Sstevel@tonic-gate 	for (pCurRes = pSubHead; pSubHead; pCurRes = pSubHead) {
2031*7c478bd9Sstevel@tonic-gate 	    pSubHead = pSubHead->ldmemcr_next[LIST_TTL];
2032*7c478bd9Sstevel@tonic-gate 	    memcache_free_from_list(cache, pCurRes, LIST_TMP);
2033*7c478bd9Sstevel@tonic-gate 	    memcache_free_entry(cache, pCurRes);
2034*7c478bd9Sstevel@tonic-gate 	}
2035*7c478bd9Sstevel@tonic-gate     }
2036*7c478bd9Sstevel@tonic-gate }
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate /********************* Hash table for primary cache ************************/
2039*7c478bd9Sstevel@tonic-gate 
2040*7c478bd9Sstevel@tonic-gate /* Hash function */
2041*7c478bd9Sstevel@tonic-gate static int
2042*7c478bd9Sstevel@tonic-gate attrkey_hashf(int table_size, void *key)
2043*7c478bd9Sstevel@tonic-gate {
2044*7c478bd9Sstevel@tonic-gate     return ((*((unsigned long*)key)) % table_size);
2045*7c478bd9Sstevel@tonic-gate }
2046*7c478bd9Sstevel@tonic-gate 
2047*7c478bd9Sstevel@tonic-gate /* Called by hash table to insert an item. */
2048*7c478bd9Sstevel@tonic-gate static int
2049*7c478bd9Sstevel@tonic-gate attrkey_putdata(void **ppTableData, void *key, void *pData)
2050*7c478bd9Sstevel@tonic-gate {
2051*7c478bd9Sstevel@tonic-gate     unsigned long attrkey = *((unsigned long*)key);
2052*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes **ppHead = (ldapmemcacheRes**)ppTableData;
2053*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pRes = *ppHead;
2054*7c478bd9Sstevel@tonic-gate 
2055*7c478bd9Sstevel@tonic-gate     for (; pRes; pRes = pRes->ldmemcr_htable_next) {
2056*7c478bd9Sstevel@tonic-gate 	if (pRes->ldmemcr_crc_key == attrkey)
2057*7c478bd9Sstevel@tonic-gate 	    return( LDAP_ALREADY_EXISTS );
2058*7c478bd9Sstevel@tonic-gate     }
2059*7c478bd9Sstevel@tonic-gate 
2060*7c478bd9Sstevel@tonic-gate     pRes = (ldapmemcacheRes*)pData;
2061*7c478bd9Sstevel@tonic-gate     pRes->ldmemcr_htable_next = *ppHead;
2062*7c478bd9Sstevel@tonic-gate     *ppHead = pRes;
2063*7c478bd9Sstevel@tonic-gate 
2064*7c478bd9Sstevel@tonic-gate     return( LDAP_SUCCESS );
2065*7c478bd9Sstevel@tonic-gate }
2066*7c478bd9Sstevel@tonic-gate 
2067*7c478bd9Sstevel@tonic-gate /* Called by hash table to retrieve an item. */
2068*7c478bd9Sstevel@tonic-gate static int
2069*7c478bd9Sstevel@tonic-gate attrkey_getdata(void *pTableData, void *key, void **ppData)
2070*7c478bd9Sstevel@tonic-gate {
2071*7c478bd9Sstevel@tonic-gate     unsigned long attrkey = *((unsigned long*)key);
2072*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pRes = (ldapmemcacheRes*)pTableData;
2073*7c478bd9Sstevel@tonic-gate 
2074*7c478bd9Sstevel@tonic-gate     for (; pRes; pRes = pRes->ldmemcr_htable_next) {
2075*7c478bd9Sstevel@tonic-gate 	if (pRes->ldmemcr_crc_key == attrkey) {
2076*7c478bd9Sstevel@tonic-gate 	    *ppData = (void*)pRes;
2077*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
2078*7c478bd9Sstevel@tonic-gate 	}
2079*7c478bd9Sstevel@tonic-gate     }
2080*7c478bd9Sstevel@tonic-gate 
2081*7c478bd9Sstevel@tonic-gate     *ppData = NULL;
2082*7c478bd9Sstevel@tonic-gate 
2083*7c478bd9Sstevel@tonic-gate     return( LDAP_NO_SUCH_OBJECT );
2084*7c478bd9Sstevel@tonic-gate }
2085*7c478bd9Sstevel@tonic-gate 
2086*7c478bd9Sstevel@tonic-gate /* Called by hash table to remove an item. */
2087*7c478bd9Sstevel@tonic-gate static int
2088*7c478bd9Sstevel@tonic-gate attrkey_removedata(void **ppTableData, void *key, void **ppData)
2089*7c478bd9Sstevel@tonic-gate {
2090*7c478bd9Sstevel@tonic-gate     unsigned long attrkey = *((unsigned long*)key);
2091*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes **ppHead = (ldapmemcacheRes**)ppTableData;
2092*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pRes = *ppHead;
2093*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pPrev = NULL;
2094*7c478bd9Sstevel@tonic-gate 
2095*7c478bd9Sstevel@tonic-gate     for (; pRes; pRes = pRes->ldmemcr_htable_next) {
2096*7c478bd9Sstevel@tonic-gate 	if (pRes->ldmemcr_crc_key == attrkey) {
2097*7c478bd9Sstevel@tonic-gate 	    if (ppData)
2098*7c478bd9Sstevel@tonic-gate 		*ppData = (void*)pRes;
2099*7c478bd9Sstevel@tonic-gate 	    if (pPrev)
2100*7c478bd9Sstevel@tonic-gate 		pPrev->ldmemcr_htable_next = pRes->ldmemcr_htable_next;
2101*7c478bd9Sstevel@tonic-gate 	    else
2102*7c478bd9Sstevel@tonic-gate 		*ppHead = pRes->ldmemcr_htable_next;
2103*7c478bd9Sstevel@tonic-gate 	    pRes->ldmemcr_htable_next = NULL;
2104*7c478bd9Sstevel@tonic-gate 	    return( LDAP_SUCCESS );
2105*7c478bd9Sstevel@tonic-gate 	}
2106*7c478bd9Sstevel@tonic-gate 	pPrev = pRes;
2107*7c478bd9Sstevel@tonic-gate     }
2108*7c478bd9Sstevel@tonic-gate 
2109*7c478bd9Sstevel@tonic-gate     if (ppData)
2110*7c478bd9Sstevel@tonic-gate 	*ppData = NULL;
2111*7c478bd9Sstevel@tonic-gate 
2112*7c478bd9Sstevel@tonic-gate     return( LDAP_NO_SUCH_OBJECT );
2113*7c478bd9Sstevel@tonic-gate }
2114*7c478bd9Sstevel@tonic-gate 
2115*7c478bd9Sstevel@tonic-gate /* Called by hash table for removing all items in the table. */
2116*7c478bd9Sstevel@tonic-gate static void
2117*7c478bd9Sstevel@tonic-gate attrkey_clearnode(void **ppTableData, void *pData)
2118*7c478bd9Sstevel@tonic-gate {
2119*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes **ppHead = (ldapmemcacheRes**)ppTableData;
2120*7c478bd9Sstevel@tonic-gate     ldapmemcacheRes *pRes = *ppHead;
2121*7c478bd9Sstevel@tonic-gate 
2122*7c478bd9Sstevel@tonic-gate     (void)pData;
2123*7c478bd9Sstevel@tonic-gate 
2124*7c478bd9Sstevel@tonic-gate     for (; *ppHead; pRes = *ppHead) {
2125*7c478bd9Sstevel@tonic-gate 	ppHead = &((*ppHead)->ldmemcr_htable_next);
2126*7c478bd9Sstevel@tonic-gate 	pRes->ldmemcr_htable_next = NULL;
2127*7c478bd9Sstevel@tonic-gate     }
2128*7c478bd9Sstevel@tonic-gate }
2129*7c478bd9Sstevel@tonic-gate 
2130*7c478bd9Sstevel@tonic-gate /***************************** CRC algorithm ********************************/
2131*7c478bd9Sstevel@tonic-gate 
2132*7c478bd9Sstevel@tonic-gate /* From http://www.faqs.org/faqs/compression-faq/part1/section-25.html */
2133*7c478bd9Sstevel@tonic-gate 
2134*7c478bd9Sstevel@tonic-gate /*
2135*7c478bd9Sstevel@tonic-gate  * Build auxiliary table for parallel byte-at-a-time CRC-32.
2136*7c478bd9Sstevel@tonic-gate  */
2137*7c478bd9Sstevel@tonic-gate #define NSLDAPI_CRC32_POLY 0x04c11db7     /* AUTODIN II, Ethernet, & FDDI */
2138*7c478bd9Sstevel@tonic-gate 
2139*7c478bd9Sstevel@tonic-gate static unsigned long crc32_table[256] = {
2140*7c478bd9Sstevel@tonic-gate     0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
2141*7c478bd9Sstevel@tonic-gate     0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
2142*7c478bd9Sstevel@tonic-gate     0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
2143*7c478bd9Sstevel@tonic-gate     0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
2144*7c478bd9Sstevel@tonic-gate     0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
2145*7c478bd9Sstevel@tonic-gate     0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
2146*7c478bd9Sstevel@tonic-gate     0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
2147*7c478bd9Sstevel@tonic-gate     0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
2148*7c478bd9Sstevel@tonic-gate     0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
2149*7c478bd9Sstevel@tonic-gate     0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
2150*7c478bd9Sstevel@tonic-gate     0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
2151*7c478bd9Sstevel@tonic-gate     0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
2152*7c478bd9Sstevel@tonic-gate     0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
2153*7c478bd9Sstevel@tonic-gate     0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
2154*7c478bd9Sstevel@tonic-gate     0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
2155*7c478bd9Sstevel@tonic-gate     0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
2156*7c478bd9Sstevel@tonic-gate     0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
2157*7c478bd9Sstevel@tonic-gate     0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
2158*7c478bd9Sstevel@tonic-gate     0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
2159*7c478bd9Sstevel@tonic-gate     0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
2160*7c478bd9Sstevel@tonic-gate     0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
2161*7c478bd9Sstevel@tonic-gate     0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
2162*7c478bd9Sstevel@tonic-gate     0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
2163*7c478bd9Sstevel@tonic-gate     0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
2164*7c478bd9Sstevel@tonic-gate     0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
2165*7c478bd9Sstevel@tonic-gate     0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
2166*7c478bd9Sstevel@tonic-gate     0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
2167*7c478bd9Sstevel@tonic-gate     0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
2168*7c478bd9Sstevel@tonic-gate     0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
2169*7c478bd9Sstevel@tonic-gate     0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
2170*7c478bd9Sstevel@tonic-gate     0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
2171*7c478bd9Sstevel@tonic-gate     0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
2172*7c478bd9Sstevel@tonic-gate     0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
2173*7c478bd9Sstevel@tonic-gate     0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
2174*7c478bd9Sstevel@tonic-gate     0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
2175*7c478bd9Sstevel@tonic-gate     0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
2176*7c478bd9Sstevel@tonic-gate     0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
2177*7c478bd9Sstevel@tonic-gate     0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
2178*7c478bd9Sstevel@tonic-gate     0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
2179*7c478bd9Sstevel@tonic-gate     0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
2180*7c478bd9Sstevel@tonic-gate     0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
2181*7c478bd9Sstevel@tonic-gate     0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
2182*7c478bd9Sstevel@tonic-gate     0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 };
2183*7c478bd9Sstevel@tonic-gate 
2184*7c478bd9Sstevel@tonic-gate /* Initialized first time "crc32()" is called. If you prefer, you can
2185*7c478bd9Sstevel@tonic-gate  * statically initialize it at compile time. [Another exercise.]
2186*7c478bd9Sstevel@tonic-gate  */
2187*7c478bd9Sstevel@tonic-gate 
2188*7c478bd9Sstevel@tonic-gate static unsigned long
2189*7c478bd9Sstevel@tonic-gate crc32_convert(char *buf, int len)
2190*7c478bd9Sstevel@tonic-gate {
2191*7c478bd9Sstevel@tonic-gate     char *p;
2192*7c478bd9Sstevel@tonic-gate #ifdef OSF1V4D
2193*7c478bd9Sstevel@tonic-gate     unsigned int crc;
2194*7c478bd9Sstevel@tonic-gate #else
2195*7c478bd9Sstevel@tonic-gate     unsigned long crc;	    /* FIXME: this is not 32-bits on all platforms! */
2196*7c478bd9Sstevel@tonic-gate #endif /* OSF1V4D */
2197*7c478bd9Sstevel@tonic-gate 
2198*7c478bd9Sstevel@tonic-gate     crc = 0xffffffff;       /* preload shift register, per CRC-32 spec */
2199*7c478bd9Sstevel@tonic-gate     for (p = buf; len > 0; ++p, --len)
2200*7c478bd9Sstevel@tonic-gate 	crc = ((crc << 8) ^ crc32_table[(crc >> 24) ^ *p]) & 0xffffffff;
2201*7c478bd9Sstevel@tonic-gate 
2202*7c478bd9Sstevel@tonic-gate     return (unsigned long) ~crc;    /* transmit complement, per CRC-32 spec */
2203*7c478bd9Sstevel@tonic-gate }
2204