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