From: Pierangelo Masarati Date: Wed, 19 Aug 2009 16:45:15 +0000 (+0000) Subject: refactor connection handling X-Git-Tag: ACLCHECK_0~272 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=69878f43c8b739e53d1090779b7917fbca911f08;p=openldap refactor connection handling --- diff --git a/servers/slapd/back-ldap/back-ldap.h b/servers/slapd/back-ldap/back-ldap.h index 8b7f464931..aa01eb5642 100644 --- a/servers/slapd/back-ldap/back-ldap.h +++ b/servers/slapd/back-ldap/back-ldap.h @@ -61,8 +61,8 @@ enum { LDAP_BACK_PCONN_LAST }; -typedef struct ldapconn_t { - Connection *lc_conn; +typedef struct ldapconn_base_t { + Connection *lcb_conn; #define LDAP_BACK_CONN2PRIV(lc) ((unsigned long)(lc)->lc_conn) #define LDAP_BACK_PCONN_ISPRIV(lc) (((void *)(lc)->lc_conn) >= ((void *)LDAP_BACK_PCONN_FIRST) \ && ((void *)(lc)->lc_conn) < ((void *)LDAP_BACK_PCONN_LAST)) @@ -94,10 +94,22 @@ typedef struct ldapconn_t { (BER_BVISEMPTY(&(op)->o_ndn) ? \ LDAP_BACK_PCONN_ANON_SET((lc), (op)) : LDAP_BACK_PCONN_ROOTDN_SET((lc), (op))) - LDAP *lc_ld; - struct berval lc_cred; - struct berval lc_bound_ndn; - struct berval lc_local_ndn; + struct berval lcb_local_ndn; + unsigned lcb_refcnt; + time_t lcb_create_time; + time_t lcb_time; +} ldapconn_base_t; + +typedef struct ldapconn_t { + ldapconn_base_t lc_base; +#define lc_conn lc_base.lcb_conn +#define lc_local_ndn lc_base.lcb_local_ndn +#define lc_refcnt lc_base.lcb_refcnt +#define lc_create_time lc_base.lcb_create_time +#define lc_time lc_base.lcb_time + + LDAP_TAILQ_ENTRY(ldapconn_t) lc_q; + unsigned lc_lcflags; #define LDAP_BACK_CONN_ISSET_F(fp,f) (*(fp) & (f)) #define LDAP_BACK_CONN_SET_F(fp,f) (*(fp) |= (f)) @@ -162,12 +174,10 @@ typedef struct ldapconn_t { #define LDAP_BACK_CONN_CACHED_SET(lc) LDAP_BACK_CONN_SET((lc), LDAP_BACK_FCONN_CACHED) #define LDAP_BACK_CONN_CACHED_CLEAR(lc) LDAP_BACK_CONN_CLEAR((lc), LDAP_BACK_FCONN_CACHED) - unsigned lc_refcnt; + LDAP *lc_ld; + struct berval lc_cred; + struct berval lc_bound_ndn; unsigned lc_flags; - time_t lc_create_time; - time_t lc_time; - - LDAP_TAILQ_ENTRY(ldapconn_t) lc_q; } ldapconn_t; typedef struct ldap_avl_info_t { diff --git a/servers/slapd/back-ldap/bind.c b/servers/slapd/back-ldap/bind.c index 3bd12209f3..d44a795890 100644 --- a/servers/slapd/back-ldap/bind.c +++ b/servers/slapd/back-ldap/bind.c @@ -40,6 +40,45 @@ #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12" #if LDAP_BACK_PRINT_CONNTREE > 0 + +static const struct { + slap_mask_t f; + char c; +} flagsmap[] = { + { LDAP_BACK_FCONN_ISBOUND, 'B' }, + { LDAP_BACK_FCONN_ISANON, 'A' }, + { LDAP_BACK_FCONN_ISPRIV, 'P' }, + { LDAP_BACK_FCONN_ISTLS, 'T' }, + { LDAP_BACK_FCONN_BINDING, 'X' }, + { LDAP_BACK_FCONN_TAINTED, 'E' }, + { LDAP_BACK_FCONN_ABANDON, 'N' }, + { LDAP_BACK_FCONN_ISIDASR, 'S' }, + { LDAP_BACK_FCONN_CACHED, 'C' }, + { 0, '\0' } +}; + +static void +ldap_back_conn_print( ldapconn_t *lc, const char *avlstr ) +{ + char buf[ SLAP_TEXT_BUFLEN ]; + char fbuf[ sizeof("BAPTIENSC") ]; + int i; + + ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) ); + for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) { + if ( lc->lc_lcflags & flagsmap[i].f ) { + fbuf[i] = flagsmap[i].c; + + } else { + fbuf[i] = '.'; + } + } + fbuf[i] = '\0'; + + fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n", + (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf ); +} + static void ldap_back_ravl_print( Avlnode *root, int depth ) { @@ -57,13 +96,9 @@ ldap_back_ravl_print( Avlnode *root, int depth ) } lc = root->avl_data; - fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d flags=0x%08x\n", - (void *)lc, - lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "", - (void *)lc->lc_conn, - avl_bf2str( root->avl_bf ), lc->lc_refcnt, lc->lc_lcflags ); - - ldap_back_ravl_print( root->avl_left, depth+1 ); + ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) ); + + ldap_back_ravl_print( root->avl_left, depth + 1 ); } static char* priv2str[] = { @@ -91,11 +126,8 @@ ldap_back_print_conntree( ldapinfo_t *li, char *msg ) LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q ) { - fprintf( stderr, " [%d] lc=%p local=\"%s\" conn=%p refcnt=%d flags=0x%08x\n", - i, - (void *)lc, - lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "", - (void *)lc->lc_conn, lc->lc_refcnt, lc->lc_lcflags ); + fprintf( stderr, " [%d] ", i ); + ldap_back_conn_print( lc, "" ); i++; } } @@ -632,7 +664,7 @@ ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_ LDAP *ld = NULL; #ifdef HAVE_TLS int is_tls = op->o_conn->c_is_tls; - time_t lc_time = (time_t)(-1); + time_t lctime = (time_t)(-1); slap_bindconf *sb; #endif /* HAVE_TLS */ @@ -705,7 +737,7 @@ ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_ } else if ( li->li_idle_timeout ) { /* only touch when activity actually took place... */ - lc_time = op->o_time; + lctime = op->o_time; } #endif /* HAVE_TLS */ @@ -717,8 +749,8 @@ ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_ } else { LDAP_BACK_CONN_ISTLS_CLEAR( lc ); } - if ( lc_time != (time_t)(-1) ) { - lc->lc_time = lc_time; + if ( lctime != (time_t)(-1) ) { + lc->lc_time = lctime; } #endif /* HAVE_TLS */ @@ -2717,7 +2749,50 @@ ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls ) } int -ldap_back_conn2str( ldapconn_t *lc, char *buf, ber_len_t buflen ) +ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen ) +{ + char tbuf[ SLAP_TEXT_BUFLEN ]; + char *ptr = buf, *end = buf + buflen; + int len; + + if ( ptr + sizeof("conn=") > end ) return -1; + ptr = lutil_strcopy( ptr, "conn=" ); + + len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) ); + ptr += len; + if ( ptr >= end ) return -1; + + if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) { + if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1; + ptr = lutil_strcopy( ptr, " DN=\"" ); + ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len ); + *ptr++ = '"'; + } + + if ( lc->lcb_create_time != 0 ) { + len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time ); + if ( ptr + sizeof(" created=") + len >= end ) return -1; + ptr = lutil_strcopy( ptr, " created=" ); + ptr = lutil_strcopy( ptr, tbuf ); + } + + if ( lc->lcb_time != 0 ) { + len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time ); + if ( ptr + sizeof(" modified=") + len >= end ) return -1; + ptr = lutil_strcopy( ptr, " modified=" ); + ptr = lutil_strcopy( ptr, tbuf ); + } + + len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt ); + if ( ptr + sizeof(" refcnt=") + len >= end ) return -1; + ptr = lutil_strcopy( ptr, " refcnt=" ); + ptr = lutil_strcopy( ptr, tbuf ); + + return ptr - buf; +} + +int +ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen ) { static struct berval conns[] = { BER_BVC("ROOTDN"), @@ -2731,11 +2806,11 @@ ldap_back_conn2str( ldapconn_t *lc, char *buf, ber_len_t buflen ) int len = 0; - if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) { + if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) { long cid; struct berval *bv; - cid = (long)lc->lc_conn; + cid = (long)lc->lcb_conn; assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST ); bv = &conns[ cid ]; @@ -2748,7 +2823,7 @@ ldap_back_conn2str( ldapconn_t *lc, char *buf, ber_len_t buflen ) lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 ); } else { - len = snprintf( buf, buflen, "%lu", lc->lc_conn->c_connid ); + len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid ); } return len; diff --git a/servers/slapd/back-ldap/proto-ldap.h b/servers/slapd/back-ldap/proto-ldap.h index 0c7f2e4983..7cdf9bbb78 100644 --- a/servers/slapd/back-ldap/proto-ldap.h +++ b/servers/slapd/back-ldap/proto-ldap.h @@ -63,7 +63,8 @@ extern void ldap_back_conn_free( void *c ); extern ldapconn_t * ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc ); -extern int ldap_back_conn2str( ldapconn_t *lc, char *buf, ber_len_t buflen ); +extern int ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen ); +extern int ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen ); extern int ldap_back_proxy_authz_ctrl( diff --git a/servers/slapd/back-meta/back-meta.h b/servers/slapd/back-meta/back-meta.h index 898b6f2c77..267b774483 100644 --- a/servers/slapd/back-meta/back-meta.h +++ b/servers/slapd/back-meta/back-meta.h @@ -221,14 +221,16 @@ typedef struct metasingleconn_t { } metasingleconn_t; typedef struct metaconn_t { - Connection *mc_conn; -#define lc_conn mc_conn - unsigned mc_refcnt; - - time_t mc_create_time; - time_t mc_time; + ldapconn_base_t lc_base; +#define mc_base lc_base +#define mc_conn mc_base.lcb_conn +#define mc_local_ndn mc_base.lcb_local_ndn +#define mc_refcnt mc_base.lcb_refcnt +#define mc_create_time mc_base.lcb_create_time +#define mc_time mc_base.lcb_time - struct berval mc_local_ndn; + LDAP_TAILQ_ENTRY(metaconn_t) mc_q; + /* NOTE: msc_mscflags is used to recycle the #define * in metasingleconn_t */ unsigned msc_mscflags; @@ -243,8 +245,6 @@ typedef struct metaconn_t { struct metainfo_t *mc_info; - LDAP_TAILQ_ENTRY(metaconn_t) mc_q; - /* supersedes the connection stuff */ metasingleconn_t mc_conns[ 1 ]; /* NOTE: mc_conns must be last, because diff --git a/servers/slapd/back-meta/bind.c b/servers/slapd/back-meta/bind.c index fa8ab230a3..d6962358ff 100644 --- a/servers/slapd/back-meta/bind.c +++ b/servers/slapd/back-meta/bind.c @@ -663,7 +663,7 @@ meta_back_dobind( if ( LogTest( LDAP_DEBUG_TRACE ) ) { char buf[STRLENOF("4294967295U") + 1] = { 0 }; - ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) ); + ldap_back_connid2str( &mc->mc_base, buf, sizeof(buf) ); Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind: conn=%s%s\n", @@ -803,7 +803,7 @@ retry_ok:; done:; if ( LogTest( LDAP_DEBUG_TRACE ) ) { char buf[STRLENOF("4294967295U") + 1] = { 0 }; - ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) ); + ldap_back_connid2str( &mc->mc_base, buf, sizeof(buf) ); Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind: conn=%s bound=%d\n", diff --git a/servers/slapd/back-meta/conn.c b/servers/slapd/back-meta/conn.c index 3de4b63cb5..a9b36baf2a 100644 --- a/servers/slapd/back-meta/conn.c +++ b/servers/slapd/back-meta/conn.c @@ -1170,7 +1170,7 @@ retry_lock:; if ( LogTest( LDAP_DEBUG_TRACE ) ) { char buf[STRLENOF("4294967295U") + 1] = { 0 }; - ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) ); + ldap_back_connid2str( &mc->mc_base, buf, sizeof(buf) ); Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%s expired (tainted).\n", @@ -1662,7 +1662,7 @@ done:; LDAP_BACK_CONN_CACHED_CLEAR( mc ); if ( LogTest( LDAP_DEBUG_ANY ) ) { char buf[STRLENOF("4294967295U") + 1] = { 0 }; - ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) ); + ldap_back_connid2str( &mc->mc_base, buf, sizeof(buf) ); Debug( LDAP_DEBUG_ANY, "%s meta_back_getconn: candidates=%d conn=%s insert failed\n", @@ -1683,7 +1683,7 @@ done:; if ( LogTest( LDAP_DEBUG_TRACE ) ) { char buf[STRLENOF("4294967295U") + 1] = { 0 }; - ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) ); + ldap_back_connid2str( &mc->mc_base, buf, sizeof(buf) ); Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: candidates=%d conn=%s inserted\n", @@ -1693,7 +1693,7 @@ done:; } else { if ( LogTest( LDAP_DEBUG_TRACE ) ) { char buf[STRLENOF("4294967295U") + 1] = { 0 }; - ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) ); + ldap_back_connid2str( &mc->mc_base, buf, sizeof(buf) ); Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: candidates=%d conn=%s fetched\n",