2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2013 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
22 #include "rewrite-int.h"
25 * Compares two cookies
33 const struct rewrite_session *s1, *s2;
35 s1 = ( const struct rewrite_session * )c1;
36 s2 = ( const struct rewrite_session * )c2;
40 assert( s1->ls_cookie != NULL );
41 assert( s2->ls_cookie != NULL );
43 return ( ( s1->ls_cookie < s2->ls_cookie ) ? -1 :
44 ( ( s1->ls_cookie > s2->ls_cookie ) ? 1 : 0 ) );
56 struct rewrite_session *s1, *s2;
58 s1 = ( struct rewrite_session * )c1;
59 s2 = ( struct rewrite_session * )c2;
63 assert( s1->ls_cookie != NULL );
64 assert( s2->ls_cookie != NULL );
66 assert( s1->ls_cookie != s2->ls_cookie );
68 return ( ( s1->ls_cookie == s2->ls_cookie ) ? -1 : 0 );
74 struct rewrite_session *
76 struct rewrite_info *info,
80 struct rewrite_session *session, tmp;
83 assert( info != NULL );
84 assert( cookie != NULL );
86 #ifdef USE_REWRITE_LDAP_PVT_THREADS
87 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
88 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
90 tmp.ls_cookie = ( void * )cookie;
91 session = ( struct rewrite_session * )avl_find( info->li_cookies,
92 ( caddr_t )&tmp, rewrite_cookie_cmp );
95 #ifdef USE_REWRITE_LDAP_PVT_THREADS
96 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
97 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
101 session = calloc( sizeof( struct rewrite_session ), 1 );
102 if ( session == NULL ) {
105 session->ls_cookie = ( void * )cookie;
106 session->ls_count = 1;
108 #ifdef USE_REWRITE_LDAP_PVT_THREADS
109 if ( ldap_pvt_thread_mutex_init( &session->ls_mutex ) ) {
113 if ( ldap_pvt_thread_rdwr_init( &session->ls_vars_mutex ) ) {
114 ldap_pvt_thread_mutex_destroy( &session->ls_mutex );
118 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
120 rc = avl_insert( &info->li_cookies, ( caddr_t )session,
121 rewrite_cookie_cmp, rewrite_cookie_dup );
122 info->li_num_cookies++;
124 #ifdef USE_REWRITE_LDAP_PVT_THREADS
125 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
126 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
129 #ifdef USE_REWRITE_LDAP_PVT_THREADS
130 ldap_pvt_thread_rdwr_destroy( &session->ls_vars_mutex );
131 ldap_pvt_thread_mutex_destroy( &session->ls_mutex );
132 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
144 struct rewrite_session *
145 rewrite_session_find(
146 struct rewrite_info *info,
150 struct rewrite_session *session, tmp;
152 assert( info != NULL );
153 assert( cookie != NULL );
155 tmp.ls_cookie = ( void * )cookie;
156 #ifdef USE_REWRITE_LDAP_PVT_THREADS
157 ldap_pvt_thread_rdwr_rlock( &info->li_cookies_mutex );
158 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
159 session = ( struct rewrite_session * )avl_find( info->li_cookies,
160 ( caddr_t )&tmp, rewrite_cookie_cmp );
161 #ifdef USE_REWRITE_LDAP_PVT_THREADS
163 ldap_pvt_thread_mutex_lock( &session->ls_mutex );
165 ldap_pvt_thread_rdwr_runlock( &info->li_cookies_mutex );
166 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
175 rewrite_session_return(
176 struct rewrite_info *info,
177 struct rewrite_session *session
180 assert( session != NULL );
181 ldap_pvt_thread_mutex_unlock( &session->ls_mutex );
185 * Defines and inits a var with session scope
188 rewrite_session_var_set_f(
189 struct rewrite_info *info,
196 struct rewrite_session *session;
197 struct rewrite_var *var;
199 assert( info != NULL );
200 assert( cookie != NULL );
201 assert( name != NULL );
202 assert( value != NULL );
204 session = rewrite_session_find( info, cookie );
205 if ( session == NULL ) {
206 session = rewrite_session_init( info, cookie );
207 if ( session == NULL ) {
211 #ifdef USE_REWRITE_LDAP_PVT_THREADS
212 ldap_pvt_thread_mutex_lock( &session->ls_mutex );
213 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
216 #ifdef USE_REWRITE_LDAP_PVT_THREADS
217 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
218 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
220 var = rewrite_var_find( session->ls_vars, name );
222 assert( var->lv_value.bv_val != NULL );
224 (void)rewrite_var_replace( var, value, flags );
227 var = rewrite_var_insert_f( &session->ls_vars, name, value, flags );
229 #ifdef USE_REWRITE_LDAP_PVT_THREADS
230 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
231 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
232 rewrite_session_return( info, session );
237 #ifdef USE_REWRITE_LDAP_PVT_THREADS
238 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
239 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
241 rewrite_session_return( info, session );
243 return REWRITE_SUCCESS;
247 * Gets a var with session scope
250 rewrite_session_var_get(
251 struct rewrite_info *info,
257 struct rewrite_session *session;
258 struct rewrite_var *var;
259 int rc = REWRITE_SUCCESS;
261 assert( info != NULL );
262 assert( cookie != NULL );
263 assert( name != NULL );
264 assert( value != NULL );
266 value->bv_val = NULL;
269 if ( cookie == NULL ) {
273 session = rewrite_session_find( info, cookie );
274 if ( session == NULL ) {
278 #ifdef USE_REWRITE_LDAP_PVT_THREADS
279 ldap_pvt_thread_rdwr_rlock( &session->ls_vars_mutex );
280 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
282 var = rewrite_var_find( session->ls_vars, name );
284 value->bv_val = strdup( var->lv_value.bv_val );
285 value->bv_len = var->lv_value.bv_len;
288 if ( var == NULL || value->bv_val == NULL ) {
292 #ifdef USE_REWRITE_LDAP_PVT_THREADS
293 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
294 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
296 rewrite_session_return( info, session );
302 rewrite_session_clean( void *v_session )
304 struct rewrite_session *session = (struct rewrite_session *)v_session;
306 #ifdef USE_REWRITE_LDAP_PVT_THREADS
307 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
308 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
310 rewrite_var_delete( session->ls_vars );
312 #ifdef USE_REWRITE_LDAP_PVT_THREADS
313 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
314 ldap_pvt_thread_rdwr_destroy( &session->ls_vars_mutex );
315 ldap_pvt_thread_mutex_unlock( &session->ls_mutex );
316 ldap_pvt_thread_mutex_destroy( &session->ls_mutex );
317 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
321 rewrite_session_free( void *v_session )
323 struct rewrite_session *session = (struct rewrite_session *)v_session;
325 ldap_pvt_thread_mutex_lock( &session->ls_mutex );
326 rewrite_session_clean( v_session );
334 rewrite_session_delete(
335 struct rewrite_info *info,
339 struct rewrite_session *session, tmp = { 0 };
341 assert( info != NULL );
342 assert( cookie != NULL );
344 session = rewrite_session_find( info, cookie );
346 if ( session == NULL ) {
347 return REWRITE_SUCCESS;
350 if ( --session->ls_count > 0 ) {
351 rewrite_session_return( info, session );
352 return REWRITE_SUCCESS;
355 rewrite_session_clean( session );
357 #ifdef USE_REWRITE_LDAP_PVT_THREADS
358 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
359 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
361 assert( info->li_num_cookies > 0 );
362 info->li_num_cookies--;
365 * There is nothing to delete in the return value
367 tmp.ls_cookie = ( void * )cookie;
368 avl_delete( &info->li_cookies, ( caddr_t )&tmp, rewrite_cookie_cmp );
372 #ifdef USE_REWRITE_LDAP_PVT_THREADS
373 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
374 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
376 return REWRITE_SUCCESS;
380 * Destroys the cookie tree
383 rewrite_session_destroy(
384 struct rewrite_info *info
389 assert( info != NULL );
391 #ifdef USE_REWRITE_LDAP_PVT_THREADS
392 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
393 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
396 * Should call per-session destruction routine ...
399 count = avl_free( info->li_cookies, rewrite_session_free );
400 info->li_cookies = NULL;
403 fprintf( stderr, "count = %d; num_cookies = %d\n",
404 count, info->li_num_cookies );
407 assert( count == info->li_num_cookies );
408 info->li_num_cookies = 0;
410 #ifdef USE_REWRITE_LDAP_PVT_THREADS
411 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
412 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
414 return REWRITE_SUCCESS;