+/*
+ * Initialize connection management infrastructure.
+ */
+int connections_init(void)
+{
+ assert( connections == NULL );
+
+ if( connections != NULL) {
+ Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
+ 0, 0, 0 );
+ return -1;
+ }
+
+ /* should check return of every call */
+ ldap_pvt_thread_mutex_init( &connections_mutex );
+
+ connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
+
+ if( connections == NULL ) {
+ Debug( LDAP_DEBUG_ANY,
+ "connections_init: allocation (%d*%ld) of connection array failed.\n",
+ dtblsize, (long) sizeof(Connection), 0 );
+ return -1;
+ }
+
+ assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
+ assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
+
+ /*
+ * per entry initialization of the Connection array initialization
+ * will be done by connection_init()
+ */
+
+ return 0;
+}
+
+/*
+ * Destroy connection management infrastructure.
+ */
+int connections_destroy(void)
+{
+ ber_socket_t i;
+
+ /* should check return of every call */
+
+ if( connections == NULL) {
+ Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
+ 0, 0, 0 );
+ return -1;
+ }
+
+ for ( i = 0; i < dtblsize; i++ ) {
+ if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
+ ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
+ ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
+ ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
+ }
+ }
+
+ free( connections );
+ connections = NULL;
+
+ ldap_pvt_thread_mutex_destroy( &connections_mutex );
+ return 0;
+}
+
+/*
+ * shutdown all connections
+ */
+int connections_shutdown(void)
+{
+ ber_socket_t i;
+
+ ldap_pvt_thread_mutex_lock( &connections_mutex );
+
+ for ( i = 0; i < dtblsize; i++ ) {
+ if( connections[i].c_struct_state != SLAP_C_USED ) {
+ continue;
+ }
+
+ ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
+
+ /* connections_mutex and c_mutex are locked */
+ connection_closing( &connections[i] );
+ connection_close( &connections[i] );
+
+ ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
+ }
+
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
+ return 0;
+}
+
+/*
+ * Timeout idle connections.
+ */
+int connections_timeout_idle(time_t now)
+{
+ int i = 0;
+ int connindex;
+ Connection* c;
+
+ ldap_pvt_thread_mutex_lock( &connections_mutex );
+
+ for( c = connection_first( &connindex );
+ c != NULL;
+ c = connection_next( c, &connindex ) )
+ {
+ if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
+ /* close it */
+ connection_closing( c );
+ connection_close( c );
+ i++;
+ }
+ }
+ connection_done( c );
+
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
+ return i;
+}
+
+static Connection* connection_get( ber_socket_t s )
+{
+ /* connections_mutex should be locked by caller */
+
+ Connection *c;
+
+ Debug( LDAP_DEBUG_ARGS,
+ "connection_get(%ld)\n",
+ (long) s, 0, 0 );
+
+ assert( connections != NULL );
+
+ if(s == AC_SOCKET_INVALID) {
+ return NULL;
+ }
+
+#ifndef HAVE_WINSOCK
+ c = &connections[s];
+
+ assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
+
+#else
+ c = NULL;
+ {
+ ber_socket_t i;
+
+ for(i=0; i<dtblsize; i++) {
+ if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
+ assert( connections[i].c_conn_state == SLAP_C_INVALID );
+ assert( connections[i].c_sb == 0 );
+ break;
+ }
+
+ if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
+ assert( connections[i].c_conn_state == SLAP_C_INVALID );
+ assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
+ continue;
+ }
+
+ /* state can actually change from used -> unused by resched,
+ * so don't assert details here.
+ */
+
+ if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
+ c = &connections[i];
+ break;
+ }
+ }
+ }
+#endif
+
+ if( c != NULL ) {
+ ldap_pvt_thread_mutex_lock( &c->c_mutex );
+
+ if( c->c_struct_state != SLAP_C_USED ) {
+ /* connection must have been closed due to resched */
+
+ assert( c->c_conn_state == SLAP_C_INVALID );
+ assert( !ber_pvt_sb_in_use( c->c_sb ) );
+
+ Debug( LDAP_DEBUG_TRACE,
+ "connection_get(%d): connection not used.\n",
+ s, c->c_connid, 0 );
+
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+ return NULL;
+ }
+
+ Debug( LDAP_DEBUG_TRACE,
+ "connection_get(%d): got connid=%ld\n",
+ s, c->c_connid, 0 );
+
+ c->c_n_get++;
+
+ assert( c->c_struct_state == SLAP_C_USED );
+ assert( c->c_conn_state != SLAP_C_INVALID );
+ assert( ber_pvt_sb_in_use( c->c_sb ) );
+
+ c->c_activitytime = slap_get_time();
+ }
+
+ return c;
+}
+
+static void connection_return( Connection *c )
+{
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+}
+
+long connection_init(
+ ber_socket_t s,
+ const char* url,
+ const char* dnsname,
+ const char* peername,
+ const char* sockname,
+ int use_tls )
+{
+ unsigned long id;
+ Connection *c;
+
+ assert( connections != NULL );
+
+ assert( dnsname != NULL );
+ assert( peername != NULL );
+ assert( sockname != NULL );
+
+#ifndef HAVE_TLS
+ assert( !use_tls );
+#endif
+
+ if( s == AC_SOCKET_INVALID ) {
+ Debug( LDAP_DEBUG_ANY,
+ "connection_init(%ld): invalid.\n",
+ (long) s, 0, 0 );
+ return -1;
+ }
+
+ assert( s >= 0 );
+#ifndef HAVE_WINSOCK
+ assert( s < dtblsize );
+#endif
+
+ ldap_pvt_thread_mutex_lock( &connections_mutex );
+
+#ifndef HAVE_WINSOCK
+ c = &connections[s];
+
+#else
+ {
+ unsigned int i;
+
+ c = NULL;
+
+ 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_listener_url = NULL;
+ c->c_peer_domain = NULL;
+ c->c_peer_name = NULL;
+ c->c_sock_name = NULL;
+
+ c->c_ops = NULL;
+ c->c_pending_ops = NULL;
+ c->c_authmech = NULL;
+ c->c_authstate = 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_listener_url == NULL );
+ assert( c->c_peer_domain == NULL );
+ assert( c->c_peer_name == NULL );
+ assert( c->c_sock_name == NULL );
+ assert( c->c_ops == NULL );
+ assert( c->c_pending_ops == NULL );
+ assert( c->c_authmech == NULL );
+ assert( c->c_authstate == NULL );
+
+ c->c_listener_url = ch_strdup( url );
+ c->c_peer_domain = ch_strdup( dnsname );
+ c->c_peer_name = ch_strdup( peername );
+ c->c_sock_name = ch_strdup( sockname );
+
+ 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;
+
+ c->c_activitytime = 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): set nonblocking failed\n",
+ s, c->c_peer_name,0 );
+ }
+
+ id = c->c_connid = conn_nextid++;
+
+ c->c_conn_state = SLAP_C_INACTIVE;
+ c->c_struct_state = SLAP_C_USED;
+
+#ifdef HAVE_TLS
+ if ( use_tls ) {
+ c->c_is_tls = 1;
+ c->c_needs_tls_accept = 1;
+ }
+#endif
+
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
+ backend_connection_init(c);
+
+ return id;
+}
+
+static void
+connection_destroy( Connection *c )
+{
+ /* note: connections_mutex should be locked by caller */
+
+ assert( connections != NULL );
+ assert( c != NULL );
+ assert( c->c_struct_state != SLAP_C_UNUSED );
+ assert( c->c_conn_state != SLAP_C_INVALID );
+ assert( c->c_ops == NULL );
+
+ backend_connection_destroy(c);
+
+ c->c_protocol = 0;
+ c->c_connid = -1;
+
+ c->c_activitytime = c->c_starttime = 0;
+
+ if(c->c_dn != NULL) {
+ free(c->c_dn);
+ c->c_dn = NULL;
+ }
+ if(c->c_cdn != NULL) {
+ free(c->c_cdn);
+ c->c_cdn = NULL;
+ }
+ if(c->c_listener_url != NULL) {
+ free(c->c_listener_url);
+ c->c_listener_url = NULL;
+ }
+ if(c->c_peer_domain != NULL) {
+ free(c->c_peer_domain);
+ c->c_peer_domain = NULL;
+ }
+ if(c->c_peer_name != NULL) {
+ free(c->c_peer_name);
+ c->c_peer_name = NULL;
+ }
+ if(c->c_sock_name != NULL) {
+ free(c->c_sock_name);
+ c->c_sock_name = NULL;
+ }
+ if(c->c_authmech != NULL ) {
+ free(c->c_authmech);
+ c->c_authmech = NULL;
+ }
+ if(c->c_authstate != NULL ) {
+ free(c->c_authstate);
+ c->c_authstate = NULL;
+ }
+
+ if ( ber_pvt_sb_in_use(c->c_sb) ) {
+ int sd = ber_pvt_sb_get_desc(c->c_sb);
+
+ slapd_remove( sd, 0 );
+ ber_pvt_sb_close( c->c_sb );
+
+ Statslog( LDAP_DEBUG_STATS,
+ "conn=%d fd=%d closed.\n",
+ c->c_connid, sd, 0, 0, 0 );
+ }
+
+ ber_pvt_sb_destroy( c->c_sb );
+
+ c->c_conn_state = SLAP_C_INVALID;
+ c->c_struct_state = SLAP_C_UNUSED;
+}
+
+int connection_state_closing( Connection *c )
+{
+ /* c_mutex must be locked by caller */
+
+ int state;
+ assert( c != NULL );
+ assert( c->c_struct_state == SLAP_C_USED );
+
+ state = c->c_conn_state;
+
+ assert( state != SLAP_C_INVALID );
+
+ return state == SLAP_C_CLOSING;
+}
+
+static void connection_abandon( Connection *c )
+{
+ /* c_mutex must be locked by caller */
+
+ Operation *o;
+
+ for( o = c->c_ops; o != NULL; o = o->o_next ) {
+ ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
+ o->o_abandon = 1;
+ ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
+ }
+
+ /* remove pending operations */
+ for( o = slap_op_pop( &c->c_pending_ops );
+ o != NULL;
+ o = slap_op_pop( &c->c_pending_ops ) )
+ {
+ slap_op_free( o );
+ }
+}
+
+void connection_closing( Connection *c )
+{
+ assert( connections != NULL );
+ assert( c != NULL );
+ assert( c->c_struct_state == SLAP_C_USED );
+ assert( c->c_conn_state != SLAP_C_INVALID );
+
+ /* c_mutex must be locked by caller */
+
+ if( c->c_conn_state != SLAP_C_CLOSING ) {
+
+ Debug( LDAP_DEBUG_TRACE,
+ "connection_closing: readying conn=%ld sd=%d for close.\n",
+ c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
+
+ /* update state to closing */
+ c->c_conn_state = SLAP_C_CLOSING;
+
+ /* don't listen on this port anymore */
+ slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
+
+ /* shutdown I/O -- not yet implemented */
+
+ /* abandon active operations */
+ connection_abandon( c );
+
+ /* wake write blocked operations */
+ slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
+ ldap_pvt_thread_cond_signal( &c->c_write_cv );
+ }
+}
+
+static void connection_close( Connection *c )
+{
+ assert( connections != NULL );
+ assert( c != NULL );
+ assert( c->c_struct_state == SLAP_C_USED );
+ assert( c->c_conn_state == SLAP_C_CLOSING );
+
+ /* note: connections_mutex and c_mutex should be locked by caller */
+
+ if( c->c_ops != NULL ) {
+ Debug( LDAP_DEBUG_TRACE,
+ "connection_close: deferring conn=%ld sd=%d.\n",
+ c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
+
+ return;
+ }
+
+ Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
+ c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
+
+ connection_destroy( c );
+}
+
+unsigned long connections_nextid(void)
+{
+ unsigned long id;
+ assert( connections != NULL );
+
+ ldap_pvt_thread_mutex_lock( &connections_mutex );
+
+ id = conn_nextid;
+
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
+ return id;
+}
+
+Connection* connection_first( ber_socket_t *index )
+{
+ assert( connections != NULL );
+ assert( index != NULL );
+
+ ldap_pvt_thread_mutex_lock( &connections_mutex );
+
+ *index = 0;
+
+ return connection_next(NULL, index);
+}
+
+Connection* connection_next( Connection *c, ber_socket_t *index )
+{
+ assert( connections != NULL );
+ assert( index != NULL );
+ assert( *index <= dtblsize );
+
+ if( c != NULL ) {
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+ }
+
+ c = NULL;
+
+ for(; *index < dtblsize; (*index)++) {
+ if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
+ assert( connections[*index].c_conn_state == SLAP_C_INVALID );
+#ifndef HAVE_WINSOCK
+ continue;
+#else
+ break;
+#endif
+ }
+
+ if( connections[*index].c_struct_state == SLAP_C_USED ) {
+ assert( connections[*index].c_conn_state != SLAP_C_INVALID );
+ c = &connections[(*index)++];
+ break;
+ }
+
+ assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
+ assert( connections[*index].c_conn_state == SLAP_C_INVALID );
+ }
+
+ if( c != NULL ) {
+ ldap_pvt_thread_mutex_lock( &c->c_mutex );
+ }
+
+ return c;
+}
+
+void connection_done( Connection *c )
+{
+ assert( connections != NULL );
+
+ if( c != NULL ) {
+ ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+ }
+
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
+}
+