- for( i=0; i < dtblsize; i++) {
- if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
- assert( connections[i].c_sb == 0 );
- c = &connections[i];
- break;
- }
-
- if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
- assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
- c = &connections[i];
- break;
- }
-
- assert( connections[i].c_struct_state == SLAP_C_USED );
- assert( connections[i].c_conn_state != SLAP_C_INVALID );
- assert( ber_pvt_sb_in_use( connections[i].c_sb ));
- }
-
- if( c == NULL ) {
- Debug( LDAP_DEBUG_ANY,
- "connection_init(%d): connection table full (%d/%d).\n",
- s, i, dtblsize);
- ldap_pvt_thread_mutex_unlock( &connections_mutex );
- return -1;
- }
- }
-#endif
-
- assert( c != NULL );
- assert( c->c_struct_state != SLAP_C_USED );
- assert( c->c_conn_state == SLAP_C_INVALID );
-
- if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
- c->c_dn = NULL;
- c->c_cdn = NULL;
- c->c_client_name = NULL;
- c->c_client_addr = NULL;
- c->c_ops = NULL;
- c->c_pending_ops = NULL;
-
- c->c_sb = ber_sockbuf_alloc( );
-
- /* should check status of thread calls */
- ldap_pvt_thread_mutex_init( &c->c_mutex );
- ldap_pvt_thread_mutex_init( &c->c_write_mutex );
- ldap_pvt_thread_cond_init( &c->c_write_cv );
-
- c->c_struct_state = SLAP_C_UNUSED;
- }
-
- ldap_pvt_thread_mutex_lock( &c->c_mutex );
-
- assert( c->c_struct_state == SLAP_C_UNUSED );
- assert( c->c_dn == NULL );
- assert( c->c_cdn == NULL );
- assert( c->c_client_name == NULL );
- assert( c->c_client_addr == NULL );
- assert( c->c_ops == NULL );
- assert( c->c_pending_ops == NULL );
-
- c->c_client_name = ch_strdup( name == NULL ? "" : name );
- if ( addr != NULL )
- c->c_client_addr = ch_strdup( addr );
-
- c->c_n_ops_received = 0;
-#ifdef LDAP_COUNTERS
- c->c_n_ops_executing = 0;
- c->c_n_ops_pending = 0;
- c->c_n_ops_completed = 0;
-#endif
-
- c->c_starttime = slap_get_time();
-
- ber_pvt_sb_set_desc( c->c_sb, s );
- ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
-
- if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
- Debug( LDAP_DEBUG_ANY,
- "connection_init(%d, %s, %s): set nonblocking failed\n",
- s, c->c_client_name, c->c_client_addr);
- }
-
- id = c->c_connid = conn_nextid++;
-
- c->c_conn_state = SLAP_C_INACTIVE;
- c->c_struct_state = SLAP_C_USED;
-
- ldap_pvt_thread_mutex_unlock( &c->c_mutex );
- ldap_pvt_thread_mutex_unlock( &connections_mutex );
-
- return id;
+ for( i=0; i < dtblsize; i++) {
+ ber_socket_t sd;
+
+ if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
+ assert( connections[i].c_sb == 0 );
+ c = &connections[i];
+ break;
+ }
+
+ sd = AC_SOCKET_INVALID;
+ if (connections[i].c_sb != NULL) {
+ ber_sockbuf_ctrl( connections[i].c_sb,
+ LBER_SB_OPT_GET_FD, &sd );
+ }
+
+ if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
+ assert( sd == AC_SOCKET_INVALID );
+ c = &connections[i];
+ break;
+ }
+
+ if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
+ continue;
+ }
+
+ assert( connections[i].c_struct_state == SLAP_C_USED );
+ assert( connections[i].c_conn_state != SLAP_C_INVALID );
+ assert( sd != AC_SOCKET_INVALID );
+ }
+
+ if( c == NULL ) {
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONNECTION, INFO,
+ "connection_init(%d): connection table full "
+ "(%d/%d)\n", s, i, dtblsize );
+#else
+ Debug( LDAP_DEBUG_ANY,
+ "connection_init(%d): connection table full "
+ "(%d/%d)\n", s, i, dtblsize);
+#endif
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+ return -1;
+ }
+ }
+#endif
+
+ assert( c != NULL );
+
+ if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
+ c->c_send_ldap_result = slap_send_ldap_result;
+ c->c_send_search_entry = slap_send_search_entry;
+ c->c_send_search_reference = slap_send_search_reference;
+ c->c_send_ldap_extended = slap_send_ldap_extended;
+#ifdef LDAP_RES_INTERMEDIATE
+ c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
+#endif
+
+ c->c_authmech.bv_val = NULL;
+ c->c_authmech.bv_len = 0;
+ c->c_dn.bv_val = NULL;
+ c->c_dn.bv_len = 0;
+ c->c_ndn.bv_val = NULL;
+ c->c_ndn.bv_len = 0;
+
+ c->c_listener = NULL;
+ c->c_peer_domain.bv_val = NULL;
+ c->c_peer_domain.bv_len = 0;
+ c->c_peer_name.bv_val = NULL;
+ c->c_peer_name.bv_len = 0;
+
+ LDAP_STAILQ_INIT(&c->c_ops);
+ LDAP_STAILQ_INIT(&c->c_pending_ops);
+
+ c->c_sasl_bind_mech.bv_val = NULL;
+ c->c_sasl_bind_mech.bv_len = 0;
+ c->c_sasl_done = 0;
+ c->c_sasl_authctx = NULL;
+ c->c_sasl_sockctx = NULL;
+ c->c_sasl_extra = NULL;
+ c->c_sasl_bindop = NULL;
+
+ c->c_sb = ber_sockbuf_alloc( );
+
+ {
+ ber_len_t max = sockbuf_max_incoming;
+ ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
+ }
+
+ c->c_currentber = NULL;
+
+ /* should check status of thread calls */
+ ldap_pvt_thread_mutex_init( &c->c_mutex );
+ ldap_pvt_thread_mutex_init( &c->c_write_mutex );
+ ldap_pvt_thread_cond_init( &c->c_write_cv );
+
+#ifdef LDAP_SLAPI
+ if ( slapi_plugins_used ) {
+ slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
+ }
+#endif
+
+ c->c_struct_state = SLAP_C_UNUSED;
+ }
+
+ ldap_pvt_thread_mutex_lock( &c->c_mutex );
+
+ assert( c->c_struct_state == SLAP_C_UNUSED );
+ assert( c->c_authmech.bv_val == NULL );
+ assert( c->c_dn.bv_val == NULL );
+ assert( c->c_ndn.bv_val == NULL );
+ assert( c->c_listener == NULL );
+ assert( c->c_peer_domain.bv_val == NULL );
+ assert( c->c_peer_name.bv_val == NULL );
+ assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
+ assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
+ assert( c->c_sasl_bind_mech.bv_val == NULL );
+ assert( c->c_sasl_done == 0 );
+ assert( c->c_sasl_authctx == NULL );
+ assert( c->c_sasl_sockctx == NULL );
+ assert( c->c_sasl_extra == NULL );
+ assert( c->c_sasl_bindop == NULL );
+ assert( c->c_currentber == NULL );
+ assert( c->c_writewaiter == 0);
+
+ c->c_listener = listener;
+
+ if ( flags == CONN_IS_CLIENT ) {
+ c->c_conn_state = SLAP_C_CLIENT;
+ c->c_struct_state = SLAP_C_USED;
+ ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
+ return 0;
+ }
+
+ ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
+ ber_str2bv( peername, 0, 1, &c->c_peer_name );
+
+ c->c_n_ops_received = 0;
+ c->c_n_ops_executing = 0;
+ c->c_n_ops_pending = 0;
+ c->c_n_ops_completed = 0;
+
+ c->c_n_get = 0;
+ c->c_n_read = 0;
+ c->c_n_write = 0;
+
+ /* set to zero until bind, implies LDAP_VERSION3 */
+ c->c_protocol = 0;
+
+#ifdef SLAPD_MONITOR
+ c->c_activitytime = c->c_starttime = slap_get_time();
+#else
+ if( global_idletimeout > 0 ) {
+ c->c_activitytime = c->c_starttime = slap_get_time();
+ }
+#endif
+
+#ifdef LDAP_CONNECTIONLESS
+ c->c_is_udp = 0;
+ if( flags == CONN_IS_UDP ) {
+ c->c_is_udp = 1;
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
+ LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
+#endif
+ ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
+ LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
+ ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
+ LBER_SBIOD_LEVEL_PROVIDER, NULL );
+ } else
+#endif
+ {
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
+ LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
+#endif
+ ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
+ LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
+ }
+
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
+ INT_MAX, (void*)"ldap_" );
+#endif
+
+ if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
+ c /* non-NULL */ ) < 0 )
+ {
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONNECTION, INFO,
+ "connection_init: conn %lu set nonblocking failed\n",
+ c->c_connid, 0, 0 );
+#else
+ Debug( LDAP_DEBUG_ANY,
+ "connection_init(%d, %s): set nonblocking failed\n",
+ s, c->c_peer_name.bv_val, 0 );
+#endif
+ }
+
+ id = c->c_connid = conn_nextid++;
+
+ c->c_conn_state = SLAP_C_INACTIVE;
+ c->c_struct_state = SLAP_C_USED;
+
+ c->c_ssf = c->c_transport_ssf = ssf;
+ c->c_tls_ssf = 0;
+
+#ifdef HAVE_TLS
+ if ( flags == CONN_IS_TLS ) {
+ c->c_is_tls = 1;
+ c->c_needs_tls_accept = 1;
+ } else {
+ c->c_is_tls = 0;
+ c->c_needs_tls_accept = 0;
+ }
+#endif
+
+ slap_sasl_open( c, 0 );
+ slap_sasl_external( c, ssf, authid );
+
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
+ backend_connection_init(c);
+
+ return id;
+}
+
+void connection2anonymous( Connection *c )
+{
+ assert( connections != NULL );
+ assert( c != NULL );
+
+ {
+ ber_len_t max = sockbuf_max_incoming;
+ ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
+ }
+
+ if(c->c_authmech.bv_val != NULL ) {
+ free(c->c_authmech.bv_val);
+ c->c_authmech.bv_val = NULL;
+ }
+ c->c_authmech.bv_len = 0;
+
+ if(c->c_dn.bv_val != NULL) {
+ free(c->c_dn.bv_val);
+ c->c_dn.bv_val = NULL;
+ }
+ c->c_dn.bv_len = 0;
+ if(c->c_ndn.bv_val != NULL) {
+ free(c->c_ndn.bv_val);
+ c->c_ndn.bv_val = NULL;
+ }
+ c->c_ndn.bv_len = 0;
+
+ c->c_authz_backend = NULL;