From a76b702d4ca1801599c640987839a58913183bc4 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Sun, 3 Dec 2006 20:56:25 +0000 Subject: [PATCH] fix request/result abandon --- include/lutil.h | 7 + libraries/libldap/abandon.c | 80 ++-------- libraries/libldap/ldap-int.h | 3 +- libraries/libldap/request.c | 2 +- libraries/libldap/result.c | 301 +++++++++++++++++++---------------- libraries/liblutil/utils.c | 156 +++++++++++++++++- 6 files changed, 340 insertions(+), 209 deletions(-) diff --git a/include/lutil.h b/include/lutil.h index b4d0108e6b..7162c4bf9d 100644 --- a/include/lutil.h +++ b/include/lutil.h @@ -322,6 +322,13 @@ lutil_unparse_time( char *buf, size_t buflen, unsigned long t ); } while ( 0 ); #endif /* ! timermul */ +LDAP_LUTIL_F (int) +lutil_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp ); +LDAP_LUTIL_F (int) +lutil_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ); +LDAP_LUTIL_F (int) +lutil_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx ); + LDAP_END_DECL #endif /* _LUTIL_H */ diff --git a/libraries/libldap/abandon.c b/libraries/libldap/abandon.c index cdb1808d7a..9785b21b16 100644 --- a/libraries/libldap/abandon.c +++ b/libraries/libldap/abandon.c @@ -34,6 +34,7 @@ * AbandonRequest ::= [APPLICATION 16] MessageID * and has no response. (Source: RFC 4511) */ +#include "lutil.h" static int do_abandon( @@ -137,7 +138,6 @@ do_abandon( { BerElement *ber; int i, err; - ber_int_t *old_abandon; Sockbuf *sb; LDAPRequest *lr; @@ -154,7 +154,7 @@ start_again:; } /* child: abandon it */ - if ( lr->lr_origid == msgid ) { + if ( lr->lr_origid == msgid && !lr->lr_abandoned ) { (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid, sctrls, sendabandon ); @@ -293,14 +293,11 @@ start_again:; ldap_free_connection( ld, lr->lr_conn, 0, 1 ); } -#if 0 - /* FIXME: this is needed so that restarting - * the initial search for lr doesn't result - * in an endless loop */ - if ( origid == msgid ) -#endif - { + if ( origid == msgid ) { ldap_free_request( ld, lr ); + + } else { + lr->lr_abandoned = 1; } } @@ -313,73 +310,16 @@ start_again:; /* use bisection */ i = 0; - if ( ld->ld_abandoned != NULL ) { - int begin, - end; - - assert( ld->ld_nabandoned >= 0 ); - - begin = 0; - end = ld->ld_nabandoned - 1; - - if ( ld->ld_nabandoned == 0 || ld->ld_abandoned[ begin ] > msgid ) { - i = 0; - - } else if ( ld->ld_abandoned[ end ] < msgid ) { - i = ld->ld_nabandoned; - - } else { - int pos, curid; - - while ( end >= begin ) { - pos = (begin + end)/2; - curid = ld->ld_abandoned[ pos ]; - - if ( msgid < curid ) { - end = pos - 1; - - } else if ( msgid > curid ) { - begin = pos + 1; - - } else { - /* already abandoned? */ - i = -1; - break; - } - } - - if ( i == 0 ) { - i = pos; - } - } - } - - if ( i != -1 ) { - int pos = i; - - old_abandon = ld->ld_abandoned; - - ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)ld->ld_abandoned, - ( ld->ld_nabandoned + 1 ) * sizeof( ber_int_t ) ); - - if ( ld->ld_abandoned == NULL ) { - ld->ld_abandoned = old_abandon; - ld->ld_errno = LDAP_NO_MEMORY; - goto done; - } - - for ( i = ld->ld_nabandoned; i > pos; i-- ) { - ld->ld_abandoned[ i ] = ld->ld_abandoned[ i - 1 ]; - } - ld->ld_abandoned[ pos ] = msgid; - ++ld->ld_nabandoned; + if ( ld->ld_nabandoned == 0 || + lutil_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, &i ) == 0 ) + { + lutil_bisect_insert( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, i ); } if ( err != -1 ) { ld->ld_errno = LDAP_SUCCESS; } -done:; #ifdef LDAP_R_COMPILE ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex ); ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index 8b02d5faf8..9a19f29aa5 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -272,6 +272,7 @@ typedef struct ldapreq { #define LDAP_REQST_WRITING 4 int lr_refcnt; /* count of references */ int lr_outrefcnt; /* count of outstanding referrals */ + int lr_abandoned; /* the request has been abandoned */ ber_int_t lr_origid; /* original request's message id */ int lr_parentcnt; /* count of parent requests */ ber_tag_t lr_res_msgtype; /* result message type */ @@ -549,7 +550,7 @@ LDAP_F (int) ldap_int_flush_request( LDAP *ld, LDAPRequest *lr ); /* * in result.c: */ -LDAP_F (char *) ldap_int_msgtype2str( ber_tag_t tag ); +LDAP_F (const char *) ldap_int_msgtype2str( ber_tag_t tag ); /* * in search.c diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c index 531ac0f4f1..177a78c78b 100644 --- a/libraries/libldap/request.c +++ b/libraries/libldap/request.c @@ -1218,7 +1218,7 @@ ldap_chase_referrals( LDAP *ld, } } if ( looped ) { - ldap_free_urllist(srv); + ldap_free_urllist( srv ); ld->ld_errno = LDAP_CLIENT_LOOP; rc = -1; continue; diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c index ebbb97c17e..380d37e213 100644 --- a/libraries/libldap/result.c +++ b/libraries/libldap/result.c @@ -64,10 +64,10 @@ #include "ldap-int.h" #include "ldap_log.h" +#include "lutil.h" -static int ldap_abandoned_idx LDAP_P(( LDAP *ld, ber_int_t msgid )); -#define ldap_abandoned(ld, msgid) ( ldap_abandoned_idx((ld), (msgid)) > -1 ) -static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid )); +static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid, int *idx )); +static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid, int idx )); static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout, LDAPMessage **result )); static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid, @@ -164,14 +164,29 @@ chkResponseList( lastlm = &ld->ld_responses; for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) { + int idx; + nextlm = lm->lm_next; ++cnt; - if ( ldap_abandoned( ld, lm->lm_msgid ) ) { - Debug( LDAP_DEBUG_TRACE, - "ldap_chkResponseList msg abandoned, msgid %d\n", - msgid, 0, 0 ); - ldap_mark_abandoned( ld, lm->lm_msgid ); + if ( ldap_abandoned( ld, lm->lm_msgid, &idx ) ) { + Debug( LDAP_DEBUG_ANY, + "response list msg abandoned, " + "msgid %d message type %s\n", + lm->lm_msgid, ldap_int_msgtype2str( lm->lm_msgtype ), 0 ); + + switch ( lm->lm_msgtype ) { + case LDAP_RES_SEARCH_ENTRY: + case LDAP_RES_SEARCH_REFERENCE: + case LDAP_RES_INTERMEDIATE: + break; + + default: + /* there's no need to keep the id + * in the abandoned list any longer */ + ldap_mark_abandoned( ld, lm->lm_msgid, idx ); + break; + } /* Remove this entry from list */ *lastlm = nextlm; @@ -184,16 +199,17 @@ chkResponseList( if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) { LDAPMessage *tmp; - if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED || + if ( all == LDAP_MSG_ONE || + all == LDAP_MSG_RECEIVED || msgid == LDAP_RES_UNSOLICITED ) { break; } tmp = lm->lm_chain_tail; - if ( (tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) || - (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) || - (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE) ) + if ( tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY || + tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE || + tmp->lm_msgtype == LDAP_RES_INTERMEDIATE ) { tmp = NULL; } @@ -207,16 +223,6 @@ chkResponseList( lastlm = &lm->lm_next; } -#if 0 - { - char buf[ BUFSIZ ]; - - snprintf( buf, sizeof( buf ), "ld=%p msgid=%d%s cnt=%d", - ld, msgid, all ? " all" : "", cnt ); - Debug( LDAP_DEBUG_TRACE, "+++ chkResponseList %s\n", buf, 0, 0 ); - } -#endif - if ( lm != NULL ) { /* Found an entry, remove it from the list */ if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) { @@ -238,10 +244,11 @@ chkResponseList( } else { Debug( LDAP_DEBUG_TRACE, "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n", - (void *)ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype ); + (void *)ld, lm->lm_msgid, (unsigned long)lm->lm_msgtype ); } #endif - return lm; + + return lm; } static int @@ -318,7 +325,7 @@ wait4msg( #endif for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) { if ( ber_sockbuf_ctrl( lc->lconn_sb, - LBER_SB_OPT_DATA_READY, NULL ) ) + LBER_SB_OPT_DATA_READY, NULL ) ) { #ifdef LDAP_R_COMPILE ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex ); @@ -440,6 +447,7 @@ try_read1msg( BerElement *ber; LDAPMessage *newmsg, *l, *prev; ber_int_t id; + int idx; ber_tag_t tag; ber_len_t len; int foundit = 0; @@ -481,7 +489,7 @@ retry: if ( lc->lconn_ber == NULL ) { lc->lconn_ber = ldap_alloc_ber_with_options( ld ); - if( lc->lconn_ber == NULL ) { + if ( lc->lconn_ber == NULL ) { return -1; } } @@ -495,7 +503,7 @@ retry: if ( LDAP_IS_UDP(ld) ) { struct sockaddr from; ber_int_sb_read( lc->lconn_sb, &from, sizeof(struct sockaddr) ); - if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2 = 1; + if ( ld->ld_options.ldo_version == LDAP_VERSION2 ) isv2 = 1; } nextresp3: #endif @@ -536,9 +544,27 @@ nextresp3: } /* if it's been abandoned, toss it */ - if ( ldap_abandoned( ld, id ) ) { - Debug( LDAP_DEBUG_ANY, "abandoned/discarded ld %p msgid %ld\n", - (void *)ld, (long) id, 0); + if ( ldap_abandoned( ld, id, &idx ) ) { + /* the message type */ + tag = ber_peek_tag( ber, &len ); + switch ( tag ) { + case LDAP_RES_SEARCH_ENTRY: + case LDAP_RES_SEARCH_REFERENCE: + case LDAP_RES_INTERMEDIATE: + case LBER_ERROR: + break; + + default: + /* there's no need to keep the id + * in the abandoned list any longer */ + ldap_mark_abandoned( ld, id, idx ); + break; + } + + Debug( LDAP_DEBUG_ANY, + "abandoned/discarded ld %p msgid %ld message type %s\n", + (void *)ld, (long)id, ldap_int_msgtype2str( tag ) ); + retry_ber: ber_free( ber, 1 ); if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) { @@ -549,19 +575,34 @@ retry_ber: lr = ldap_find_request_by_msgid( ld, id ); if ( lr == NULL ) { + const char *msg = "unknown"; + + /* the message type */ + tag = ber_peek_tag( ber, &len ); + switch ( tag ) { + case LBER_ERROR: + break; + + default: + msg = ldap_int_msgtype2str( tag ); + break; + } + Debug( LDAP_DEBUG_ANY, - "no request for response on ld %p msgid %ld (tossing)\n", - (void *)ld, (long)id, 0 ); + "no request for response on ld %p msgid %ld message type %s (tossing)\n", + (void *)ld, (long)id, msg ); + goto retry_ber; } #ifdef LDAP_CONNECTIONLESS - if (LDAP_IS_UDP(ld) && isv2) { + if ( LDAP_IS_UDP(ld) && isv2 ) { ber_scanf(ber, "x{"); } nextresp2: #endif /* the message type */ - if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) { + tag = ber_peek_tag( ber, &len ); + if ( tag == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; ber_free( ber, 1 ); return( -1 ); @@ -569,7 +610,7 @@ nextresp2: Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %ld message type %s\n", - (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag )); + (void *)ld, (long)lr->lr_msgid, ldap_int_msgtype2str( tag ) ); id = lr->lr_origid; refer_cnt = 0; @@ -582,17 +623,17 @@ nextresp2: * referral / search reference, or pass it back to the application */ v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */ - if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) { + if ( tag != LDAP_RES_SEARCH_ENTRY && ld->ld_version > LDAP_VERSION2 ) { BerElement tmpber = *ber; /* struct copy */ - char **refs = NULL; + char **refs = NULL; - if( tag == LDAP_RES_SEARCH_REFERENCE ) { + if ( tag == LDAP_RES_SEARCH_REFERENCE ) { /* This is a V3 search reference */ /* Assume we do not chase the reference, * but pass it to application */ v3ref = V3REF_TOAPP; - if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) || - (lr->lr_parent != NULL) ) + if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) || + lr->lr_parent != NULL ) { /* Get the referral list */ if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) { @@ -601,7 +642,7 @@ nextresp2: } else { /* Note: refs array is freed by ldap_chase_v3referrals */ refer_cnt = ldap_chase_v3referrals( ld, lr, refs, - 1, &lr->lr_res_error, &hadref ); + 1, &lr->lr_res_error, &hadref ); if ( refer_cnt > 0 ) { /* sucessfully chased reference */ /* If haven't got end search, set chasing referrals */ @@ -611,7 +652,7 @@ nextresp2: "read1msg: search ref chased, " "mark request chasing refs, " "id = %d\n", - lr->lr_msgid, 0, 0); + lr->lr_msgid, 0, 0 ); } /* We sucessfully chased the reference */ @@ -644,19 +685,20 @@ nextresp2: if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) { /* We have a V3 referral, assume we cannot chase it */ v3ref = V3REF_TOAPP; - if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) - || (lr->lr_parent != NULL) ) + if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) + || lr->lr_parent != NULL ) { /* Assume referral not chased and return it to app */ v3ref = V3REF_TOAPP; /* Get the referral list */ - if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) { + if ( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) { rc = LDAP_DECODING_ERROR; lr->lr_status = LDAP_REQST_COMPLETED; Debug( LDAP_DEBUG_TRACE, - "read1msg: referral decode error, mark request completed, ld %p msgid %d\n", - (void *)ld, lr->lr_msgid, 0); + "read1msg: referral decode error, " + "mark request completed, ld %p msgid %d\n", + (void *)ld, lr->lr_msgid, 0 ); } else { /* Chase the referral @@ -666,9 +708,10 @@ nextresp2: 0, &lr->lr_res_error, &hadref ); lr->lr_status = LDAP_REQST_COMPLETED; Debug( LDAP_DEBUG_TRACE, - "read1msg: referral chased, mark request completed, ld %p msgid %d\n", + "read1msg: referral chased, " + "mark request completed, ld %p msgid %d\n", (void *)ld, lr->lr_msgid, 0); - if( refer_cnt > 0) { + if ( refer_cnt > 0 ) { /* Referral successfully chased */ v3ref = V3REF_SUCCESS; } @@ -676,12 +719,12 @@ nextresp2: } } - if( lr->lr_res_matched != NULL ) { + if ( lr->lr_res_matched != NULL ) { LDAP_FREE( lr->lr_res_matched ); lr->lr_res_matched = NULL; } - if( lr->lr_res_error != NULL ) { + if ( lr->lr_res_error != NULL ) { LDAP_FREE( lr->lr_res_error ); lr->lr_res_error = NULL; } @@ -693,8 +736,9 @@ nextresp2: * go through the following code. This code also chases V2 referrals * and checks if all referrals have been chased. */ - if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) && - (tag != LDAP_RES_INTERMEDIATE )) + if ( tag != LDAP_RES_SEARCH_ENTRY && + v3ref != V3REF_TOAPP && + tag != LDAP_RES_INTERMEDIATE ) { /* For a v3 search referral/reference, only come here if already chased it */ if ( ld->ld_version >= LDAP_VERSION2 && @@ -734,8 +778,8 @@ nextresp2: break; default: - if ( lr->lr_res_error == NULL - || lr->lr_res_error[ 0 ] == '\0' ) + if ( lr->lr_res_error == NULL || + lr->lr_res_error[ 0 ] == '\0' ) { break; } @@ -753,9 +797,9 @@ nextresp2: /* save errno, message, and matched string */ if ( !hadref || lr->lr_res_error == NULL ) { - lr->lr_res_errno = ( lderr == - LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS - : lderr; + lr->lr_res_errno = + lderr == LDAP_PARTIAL_RESULTS + ? LDAP_SUCCESS : lderr; } else if ( ld->ld_errno != LDAP_SUCCESS ) { lr->lr_res_errno = ld->ld_errno; @@ -828,17 +872,20 @@ nextresp2: } /* This is the parent request if the request has referrals */ - if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && + if ( lr->lr_outrefcnt <= 0 && + lr->lr_parent == NULL && tmplr == NULL ) { id = lr->lr_msgid; tag = lr->lr_res_msgtype; Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n", (void *)ld, (long) id, 0 ); -Debug( LDAP_DEBUG_TRACE, -"res_errno: %d, res_error: <%s>, res_matched: <%s>\n", -lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "", -lr->lr_res_matched ? lr->lr_res_matched : "" ); + Debug( LDAP_DEBUG_TRACE, + "res_errno: %d, res_error: <%s>, " + "res_matched: <%s>\n", + lr->lr_res_errno, + lr->lr_res_error ? lr->lr_res_error : "", + lr->lr_res_matched ? lr->lr_res_matched : "" ); if ( !simple_request ) { ber_free( ber, 1 ); ber = NULL; @@ -908,18 +955,18 @@ lr->lr_res_matched ? lr->lr_res_matched : "" ); */ ber = ber_dup( ber ); ber_scanf( ber, "x" ); - if (ber_peek_tag(ber, &len) != LBER_DEFAULT) { + if ( ber_peek_tag( ber, &len ) != LBER_DEFAULT ) { /* There's more - dup the ber buffer so they can all be * individually freed by ldap_msgfree. */ struct berval bv; - ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len); - bv.bv_val = LDAP_MALLOC(len); - if (bv.bv_val) { - ok=1; - ber_read(ber, bv.bv_val, len); + ber_get_option( ber, LBER_OPT_BER_REMAINING_BYTES, &len ); + bv.bv_val = LDAP_MALLOC( len ); + if ( bv.bv_val ) { + ok = 1; + ber_read( ber, bv.bv_val, len ); bv.bv_len = len; - ber_init2(ber, &bv, ld->ld_lberoptions ); + ber_init2( ber, &bv, ld->ld_lberoptions ); } } } else { @@ -941,16 +988,20 @@ lr->lr_res_matched ? lr->lr_res_matched : "" ); chain_head->lm_chain_tail = newmsg; tmp = newmsg; /* "ok" means there's more to parse */ - if (ok) { - if (isv2) goto nextresp2; - else goto nextresp3; + if ( ok ) { + if ( isv2 ) { + goto nextresp2; + + } else { + goto nextresp3; + } } else { /* got to end of datagram without a SearchResult. Free * our dup'd ber, but leave any buffer alone. For v2 case, * the previous response is still using this buffer. For v3, * the new ber has no buffer to free yet. */ - ber_free(ber, 0); + ber_free( ber, 0 ); return -1; } } else if ( moremsgs ) { @@ -970,12 +1021,14 @@ lr->lr_res_matched ? lr->lr_res_matched : "" ); /* is this the one we're looking for? */ if ( msgid == LDAP_RES_ANY || id == msgid ) { if ( all == LDAP_MSG_ONE - || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT - && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY - && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) { + || ( newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT + && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY + && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE ) ) + { *result = newmsg; ld->ld_errno = LDAP_SUCCESS; return( tag ); + } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) { foundit = 1; /* return the chain later */ } @@ -1056,7 +1109,7 @@ build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr ) lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) { ld->ld_errno = LDAP_ENCODING_ERROR; - ber_free(ber, 1); + ber_free( ber, 1 ); return( LBER_ERROR ); } @@ -1064,13 +1117,13 @@ build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr ) if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; - ber_free(ber, 1); + ber_free( ber, 1 ); return( LBER_ERROR ); } if ( ber_get_enum( ber, &along ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; - ber_free(ber, 1); + ber_free( ber, 1 ); return( LBER_ERROR ); } @@ -1078,7 +1131,7 @@ build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr ) if ( tag == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; - ber_free(ber, 1); + ber_free( ber, 1 ); return( LBER_ERROR ); } @@ -1097,7 +1150,7 @@ merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ) parentr->lr_res_errno = lr->lr_res_errno; if ( lr->lr_res_error != NULL ) { (void)ldap_append_referral( ld, &parentr->lr_res_error, - lr->lr_res_error ); + lr->lr_res_error ); } } else if ( lr->lr_res_errno != LDAP_SUCCESS && @@ -1145,7 +1198,8 @@ ldap_msgid( LDAPMessage *lm ) } -char * ldap_int_msgtype2str( ber_tag_t tag ) +const char * +ldap_int_msgtype2str( ber_tag_t tag ) { switch( tag ) { case LDAP_RES_ADD: return "add"; @@ -1190,7 +1244,7 @@ int ldap_msgdelete( LDAP *ld, int msgid ) { LDAPMessage *lm, *prev; - int rc = 0; + int rc = 0; assert( ld != NULL ); @@ -1221,8 +1275,17 @@ ldap_msgdelete( LDAP *ld, int msgid ) #ifdef LDAP_R_COMPILE ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex ); #endif - if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY ) { - rc = -1; + if ( lm ) { + switch ( ldap_msgfree( lm ) ) { + case LDAP_RES_SEARCH_ENTRY: + case LDAP_RES_SEARCH_REFERENCE: + case LDAP_RES_INTERMEDIATE: + rc = -1; + break; + + default: + break; + } } return rc; @@ -1230,7 +1293,7 @@ ldap_msgdelete( LDAP *ld, int msgid ) /* - * ldap_abandoned_idx + * ldap_abandoned * * return the location of the message id in the array of abandoned * message ids, or -1 @@ -1238,50 +1301,17 @@ ldap_msgdelete( LDAP *ld, int msgid ) * expects ld_res_mutex to be locked */ static int -ldap_abandoned_idx( LDAP *ld, ber_int_t msgid ) +ldap_abandoned( LDAP *ld, ber_int_t msgid, int *idxp ) { - int begin, - end; - #ifdef LDAP_R_COMPILE LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex ); #endif + assert( idxp != NULL ); + assert( msgid >= 0 ); assert( ld->ld_nabandoned >= 0 ); - if ( ld->ld_abandoned == NULL || ld->ld_nabandoned == 0 ) { - return -1; - } - - begin = 0; - end = ld->ld_nabandoned - 1; - - /* use bisection */ - if ( msgid < ld->ld_abandoned[ begin ] ) { - return -1; - } - - if ( msgid > ld->ld_abandoned[ end ] ) { - return -1; - } - - while ( end >= begin ) { - int pos = (begin + end)/2; - int curid = ld->ld_abandoned[ pos ]; - - if ( msgid < curid ) { - end = pos - 1; - - } else if ( msgid > curid ) { - begin = pos + 1; - - } else { - return pos; - } - } - - /* not abandoned */ - return -1; + return lutil_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, idxp ); } /* @@ -1290,24 +1320,25 @@ ldap_abandoned_idx( LDAP *ld, ber_int_t msgid ) * expects ld_res_mutex to be locked */ static int -ldap_mark_abandoned( LDAP *ld, ber_int_t msgid ) +ldap_mark_abandoned( LDAP *ld, ber_int_t msgid, int idx ) { - int i, idx; + int i; #ifdef LDAP_R_COMPILE LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex ); #endif - idx = ldap_abandoned_idx( ld, msgid ); - if ( idx == -1 ) { - return -1; - } + /* NOTE: those assertions are repeated in lutil_bisect_delete() */ + assert( idx >= 0 ); + assert( idx < ld->ld_nabandoned ); + assert( ld->ld_abandoned[ idx ] == msgid ); - --ld->ld_nabandoned; - assert( ld->ld_nabandoned >= 0 ); - for ( i = idx; i < ld->ld_nabandoned; i++ ) { - ld->ld_abandoned[ i ] = ld->ld_abandoned[ i + 1 ]; +#if 0 + fprintf( stderr, "--> ldap_mark_abandoned %p %p %d %d:\n", (void *)&i, (void *)ld, msgid, idx ); + for ( i = 0; i < ld->ld_nabandoned; i++ ) { + fprintf( stderr, "\t%p %p %d\n", (void *)&i, (void *)ld, ld->ld_abandoned[ i ] ); } +#endif - return 0; + return lutil_bisect_delete( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, idx ); } diff --git a/libraries/liblutil/utils.c b/libraries/liblutil/utils.c index e74f9093ce..bffeef84a4 100644 --- a/libraries/liblutil/utils.c +++ b/libraries/liblutil/utils.c @@ -28,8 +28,9 @@ #include #endif -#include -#include +#include "lutil.h" +#include "ldap_defaults.h" +#include "ldap_pvt.h" #ifdef HAVE_EBCDIC int _trans_argv = 1; @@ -571,3 +572,154 @@ lutil_unparse_time( return 0; } +/* + * lutil_bisect_find + * + * args: + * v: array of length n (in) + * n: length of array v (in) + * id: value to look for (in) + * idxp: pointer to location of value/insert point + * + * return: + * 0: not found + * 1: found + * -1: error + */ +int +lutil_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp ) +{ + int begin, + end, + i, + rc = 0; + + assert( n >= 0 ); + assert( id >= 0 ); + + begin = 0; + end = n - 1; + + if ( n > 0 ) { + if ( id < v[ begin ] ) { + *idxp = 0; + + } else if ( id > v[ end ] ) { + *idxp = n; + + } else { + int pos; + ber_int_t curid; + + while ( end >= begin ) { + pos = (begin + end)/2; + curid = v[ pos ]; + + if ( id < curid ) { + end = pos - 1; + + } else if ( id > curid ) { + begin = pos + 1; + + } else { + /* already abandoned? */ + *idxp = pos; + rc = 1; + break; + } + } + + if ( rc == 0 ) { + *idxp = pos + ( id > curid ? 1 : 0 ); + } + } + + } else { + *idxp = 0; + } + + return rc; +} + +/* + * lutil_bisect_insert + * + * args: + * vp: pointer to array of length *np (in/out) + * np: pointer to length of array *vp (in/out) + * id: value to insert (in) + * idx: location of insert point (as computed by lutil_bosect_find()) + * + * return: + * 0: inserted + * -1: error + */ +int +lutil_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ) +{ + ber_int_t *v; + ber_len_t n; + int i; + + assert( vp != NULL ); + assert( np != NULL ); + assert( *np >= 0 ); + assert( idx >= 0 ); + assert( idx <= *np ); + + n = *np; + + v = ber_memrealloc( *vp, sizeof( ber_int_t ) * ( n + 1 ) ); + if ( v == NULL ) { + return -1; + } + *vp = v; + + for ( i = n; i > idx; i-- ) { + v[ i ] = v[ i - 1 ]; + } + v[ idx ] = id; + ++(*np); + + return 0; +} + +/* + * lutil_bisect_delete + * + * args: + * vp: pointer to array of length *np (in/out) + * np: pointer to length of array *vp (in/out) + * id: value to delete (in) + * idx: location of value to delete (as computed by lutil_bosect_find()) + * + * return: + * 0: deleted + */ +int +lutil_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx ) +{ + ber_int_t *v; + ber_len_t n; + int i; + + assert( vp != NULL ); + assert( np != NULL ); + assert( *np >= 0 ); + assert( idx >= 0 ); + assert( idx < *np ); + + v = *vp; + + assert( v[ idx ] == id ); + + --(*np); + n = *np; + + for ( i = idx; i < n; i++ ) { + v[ i ] = v[ i + 1 ]; + } + + return 0; +} + -- 2.39.5