2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
47 #ifdef SLAP_MULTI_CONN_ARRAY
48 /* for Multiple Connection Arrary (MCA) Support */
49 static ldap_pvt_thread_mutex_t* connections_mutex;
50 static Connection **connections = NULL;
52 /* set to the number of processors */
53 # define NUM_CONNECTION_ARRAY 4
55 /* partition the array in a modulo manner */
56 # define MCA_conn_array_id(fd) ((int)(fd)%NUM_CONNECTION_ARRAY)
57 # define MCA_conn_array_element_id(fd) ((int)(fd)/NUM_CONNECTION_ARRAY)
58 # define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)])\
59 [MCA_conn_array_element_id(fd)])
60 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
63 /* protected by connections_mutex */
64 static ldap_pvt_thread_mutex_t connections_mutex;
65 static Connection *connections = NULL;
67 # define MCA_GET_CONNECTION(fd) (&connections[s])
68 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
71 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
72 static unsigned long conn_nextid = 0;
74 static const char conn_lost_str[] = "connection lost";
76 /* structure state (protected by connections_mutex) */
77 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
78 #define SLAP_C_UNUSED 0x01
79 #define SLAP_C_USED 0x02
81 /* connection state (protected by c_mutex ) */
82 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
83 #define SLAP_C_INACTIVE 0x01 /* zero threads */
84 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
85 #define SLAP_C_BINDING 0x03 /* binding */
86 #define SLAP_C_CLOSING 0x04 /* closing */
87 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
90 connection_state2str( int state )
93 case SLAP_C_INVALID: return "!";
94 case SLAP_C_INACTIVE: return "|";
95 case SLAP_C_ACTIVE: return "";
96 case SLAP_C_BINDING: return "B";
97 case SLAP_C_CLOSING: return "C";
98 case SLAP_C_CLIENT: return "L";
104 static Connection* connection_get( ber_socket_t s );
106 #ifdef SLAP_LIGHTWEIGHT_LISTENER
107 static int connection_input( Connection *c, Operation** op );
109 static int connection_input( Connection *c );
111 static void connection_close( Connection *c );
113 static int connection_op_activate( Operation *op );
114 #ifdef SLAP_LIGHTWEIGHT_LISTENER
115 static void connection_op_queue( Operation *op );
117 static int connection_resched( Connection *conn );
118 static void connection_abandon( Connection *conn );
119 static void connection_destroy( Connection *c );
121 static ldap_pvt_thread_start_t connection_operation;
124 * Initialize connection management infrastructure.
126 int connections_init(void)
127 #ifdef SLAP_MULTI_CONN_ARRAY
132 assert( connections == NULL );
134 if( connections != NULL) {
135 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
140 connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc( NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
141 if( connections_mutex == NULL ) {
142 Debug( LDAP_DEBUG_ANY,
143 "connections_init: allocation of connection mutex[%d] failed\n", i, 0, 0 );
147 connections = (Connection**) ch_calloc( NUM_CONNECTION_ARRAY, sizeof(Connection*));
148 if( connections == NULL ) {
149 Debug( LDAP_DEBUG_ANY,
150 "connections_init: allocation of connection[%d] failed\n", 0, 0, 0 );
154 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
155 ldap_pvt_thread_mutex_init( connections_mutex+i );
156 connections[i] = (Connection*) ch_calloc( (dtblsize/NUM_CONNECTION_ARRAY), sizeof(Connection) );
157 if( connections[i] == NULL ) {
158 Debug( LDAP_DEBUG_ANY,
159 "connections_init: allocation (%d*%ld) of connection array[%d] failed\n",
160 dtblsize, (long) sizeof(Connection), i );
165 /* should check return of every call */
166 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
168 assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
169 assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state == SLAP_C_UNINITIALIZED );
171 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
172 conn = connections[i];
173 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
174 conn[j].c_conn_idx = j;
179 * per entry initialization of the Connection array initialization
180 * will be done by connection_init()
189 assert( connections == NULL );
191 if( connections != NULL) {
192 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
197 /* should check return of every call */
198 ldap_pvt_thread_mutex_init( &connections_mutex );
199 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
201 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
203 if( connections == NULL ) {
204 Debug( LDAP_DEBUG_ANY,
205 "connections_init: allocation (%d*%ld) of connection array failed\n",
206 dtblsize, (long) sizeof(Connection), 0 );
210 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
211 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
213 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
216 * per entry initialization of the Connection array initialization
217 * will be done by connection_init()
225 * Destroy connection management infrastructure.
228 int connections_destroy(void)
229 #ifdef SLAP_MULTI_CONN_ARRAY
234 if( connections == NULL) {
235 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
240 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
241 Connection* conn = connections[i];
242 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
243 if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
244 ber_sockbuf_free( conn[j].c_sb );
245 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
246 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
247 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
250 if ( slapi_plugins_used ) {
251 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
259 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
260 free( connections[i] );
261 connections[i] = NULL;
262 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
265 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
274 /* should check return of every call */
276 if( connections == NULL) {
277 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
282 for ( i = 0; i < dtblsize; i++ ) {
283 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
284 ber_sockbuf_free( connections[i].c_sb );
285 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
286 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
287 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
289 if ( slapi_plugins_used ) {
290 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
300 ldap_pvt_thread_mutex_destroy( &connections_mutex );
301 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
307 * shutdown all connections
309 int connections_shutdown(void)
310 #ifdef SLAP_MULTI_CONN_ARRAY
315 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
316 Connection* conn = connections[i];
317 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
318 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
319 if( conn[j].c_struct_state != SLAP_C_USED ) {
322 /* give persistent clients a chance to cleanup */
323 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
324 ldap_pvt_thread_pool_submit( &connection_pool,
325 conn[j].c_clientfunc, conn[j].c_clientarg );
329 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
330 /* connections_mutex and c_mutex are locked */
331 connection_closing( &conn[j], "connection shutdown" );
332 connection_close( &conn[j] );
333 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
336 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
346 ldap_pvt_thread_mutex_lock( &connections_mutex );
348 for ( i = 0; i < dtblsize; i++ ) {
349 if( connections[i].c_struct_state != SLAP_C_USED ) {
352 /* give persistent clients a chance to cleanup */
353 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
354 ldap_pvt_thread_pool_submit( &connection_pool,
355 connections[i].c_clientfunc, connections[i].c_clientarg );
359 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
361 /* connections_mutex and c_mutex are locked */
362 connection_closing( &connections[i], "slapd shutdown" );
363 connection_close( &connections[i] );
365 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
368 ldap_pvt_thread_mutex_unlock( &connections_mutex );
375 * Timeout idle connections.
377 int connections_timeout_idle(time_t now)
383 for( c = connection_first( &connindex );
385 c = connection_next( c, &connindex ) )
387 /* Don't timeout a slow-running request or a persistent
388 * outbound connection */
389 if( c->c_n_ops_executing ||
390 c->c_conn_state == SLAP_C_CLIENT ) continue;
392 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
394 connection_closing( c, "idletimeout" );
395 connection_close( c );
399 connection_done( c );
404 static Connection* connection_get( ber_socket_t s )
406 /* connections_mutex should be locked by caller */
410 Debug( LDAP_DEBUG_ARGS,
411 "connection_get(%ld)\n",
414 assert( connections != NULL );
416 if(s == AC_SOCKET_INVALID) {
421 c = MCA_GET_CONNECTION(s);
423 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
430 for(i=0; i<dtblsize; i++) {
431 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
432 assert( connections[i].c_conn_state == SLAP_C_INVALID );
433 assert( connections[i].c_sb == 0 );
437 ber_sockbuf_ctrl( connections[i].c_sb,
438 LBER_SB_OPT_GET_FD, &sd );
440 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
441 assert( connections[i].c_conn_state == SLAP_C_INVALID );
442 assert( sd == AC_SOCKET_INVALID );
446 /* state can actually change from used -> unused by resched,
447 * so don't assert details here.
461 ldap_pvt_thread_mutex_lock( &c->c_mutex );
463 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
464 if( c->c_struct_state != SLAP_C_USED ) {
465 /* connection must have been closed due to resched */
467 assert( c->c_conn_state == SLAP_C_INVALID );
468 assert( sd == AC_SOCKET_INVALID );
470 Debug( LDAP_DEBUG_TRACE,
471 "connection_get(%d): connection not used\n",
474 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
478 Debug( LDAP_DEBUG_TRACE,
479 "connection_get(%d): got connid=%lu\n",
484 assert( c->c_struct_state == SLAP_C_USED );
485 assert( c->c_conn_state != SLAP_C_INVALID );
486 assert( sd != AC_SOCKET_INVALID );
488 #ifndef SLAPD_MONITOR
489 if ( global_idletimeout > 0 )
490 #endif /* ! SLAPD_MONITOR */
492 c->c_activitytime = slap_get_time();
499 static void connection_return( Connection *c )
501 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
504 long connection_init(
508 const char* peername,
511 struct berval *authid )
516 assert( connections != NULL );
518 assert( listener != NULL );
519 assert( dnsname != NULL );
520 assert( peername != NULL );
523 assert( flags != CONN_IS_TLS );
526 if( s == AC_SOCKET_INVALID ) {
527 Debug( LDAP_DEBUG_ANY,
528 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
534 assert( s < dtblsize );
537 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
540 c = MCA_GET_CONNECTION(s);
546 for( i=0; i < dtblsize; i++) {
549 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
550 assert( connections[i].c_sb == 0 );
555 sd = AC_SOCKET_INVALID;
556 if (connections[i].c_sb != NULL) {
557 ber_sockbuf_ctrl( connections[i].c_sb,
558 LBER_SB_OPT_GET_FD, &sd );
561 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
562 assert( sd == AC_SOCKET_INVALID );
567 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
571 assert( connections[i].c_struct_state == SLAP_C_USED );
572 assert( connections[i].c_conn_state != SLAP_C_INVALID );
573 assert( sd != AC_SOCKET_INVALID );
577 Debug( LDAP_DEBUG_ANY,
578 "connection_init(%d): connection table full "
579 "(%d/%d)\n", s, i, dtblsize);
580 ldap_pvt_thread_mutex_unlock( &connections_mutex );
588 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
589 c->c_send_ldap_result = slap_send_ldap_result;
590 c->c_send_search_entry = slap_send_search_entry;
591 c->c_send_search_reference = slap_send_search_reference;
592 c->c_send_ldap_extended = slap_send_ldap_extended;
593 #ifdef LDAP_RES_INTERMEDIATE
594 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
597 BER_BVZERO( &c->c_authmech );
598 BER_BVZERO( &c->c_dn );
599 BER_BVZERO( &c->c_ndn );
601 c->c_listener = NULL;
602 BER_BVZERO( &c->c_peer_domain );
603 BER_BVZERO( &c->c_peer_name );
605 LDAP_STAILQ_INIT(&c->c_ops);
606 LDAP_STAILQ_INIT(&c->c_pending_ops);
608 BER_BVZERO( &c->c_sasl_bind_mech );
610 c->c_sasl_authctx = NULL;
611 c->c_sasl_sockctx = NULL;
612 c->c_sasl_extra = NULL;
613 c->c_sasl_bindop = NULL;
615 c->c_sb = ber_sockbuf_alloc( );
618 ber_len_t max = sockbuf_max_incoming;
619 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
622 c->c_currentber = NULL;
624 /* should check status of thread calls */
625 ldap_pvt_thread_mutex_init( &c->c_mutex );
626 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
627 ldap_pvt_thread_cond_init( &c->c_write_cv );
630 if ( slapi_plugins_used ) {
631 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
635 c->c_struct_state = SLAP_C_UNUSED;
638 ldap_pvt_thread_mutex_lock( &c->c_mutex );
640 assert( c->c_struct_state == SLAP_C_UNUSED );
641 assert( BER_BVISNULL( &c->c_authmech ) );
642 assert( BER_BVISNULL( &c->c_dn ) );
643 assert( BER_BVISNULL( &c->c_ndn ) );
644 assert( c->c_listener == NULL );
645 assert( BER_BVISNULL( &c->c_peer_domain ) );
646 assert( BER_BVISNULL( &c->c_peer_name ) );
647 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
648 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
649 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
650 assert( c->c_sasl_done == 0 );
651 assert( c->c_sasl_authctx == NULL );
652 assert( c->c_sasl_sockctx == NULL );
653 assert( c->c_sasl_extra == NULL );
654 assert( c->c_sasl_bindop == NULL );
655 assert( c->c_currentber == NULL );
656 assert( c->c_writewaiter == 0);
658 c->c_listener = listener;
660 if ( flags == CONN_IS_CLIENT ) {
661 c->c_conn_state = SLAP_C_CLIENT;
662 c->c_struct_state = SLAP_C_USED;
663 c->c_close_reason = "?"; /* should never be needed */
664 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
665 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
666 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
671 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
672 ber_str2bv( peername, 0, 1, &c->c_peer_name );
674 c->c_n_ops_received = 0;
675 c->c_n_ops_executing = 0;
676 c->c_n_ops_pending = 0;
677 c->c_n_ops_completed = 0;
683 /* set to zero until bind, implies LDAP_VERSION3 */
686 #ifndef SLAPD_MONITOR
687 if ( global_idletimeout > 0 )
688 #endif /* ! SLAPD_MONITOR */
690 c->c_activitytime = c->c_starttime = slap_get_time();
693 #ifdef LDAP_CONNECTIONLESS
695 if( flags == CONN_IS_UDP ) {
698 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
699 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
701 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
702 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
703 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
704 LBER_SBIOD_LEVEL_PROVIDER, NULL );
709 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
710 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
712 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
713 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
717 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
718 INT_MAX, (void*)"ldap_" );
721 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
722 c /* non-NULL */ ) < 0 )
724 Debug( LDAP_DEBUG_ANY,
725 "connection_init(%d, %s): set nonblocking failed\n",
726 s, c->c_peer_name.bv_val, 0 );
729 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
730 id = c->c_connid = conn_nextid++;
731 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
733 c->c_conn_state = SLAP_C_INACTIVE;
734 c->c_struct_state = SLAP_C_USED;
735 c->c_close_reason = "?"; /* should never be needed */
737 c->c_ssf = c->c_transport_ssf = ssf;
741 if ( flags == CONN_IS_TLS ) {
743 c->c_needs_tls_accept = 1;
746 c->c_needs_tls_accept = 0;
750 slap_sasl_open( c, 0 );
751 slap_sasl_external( c, ssf, authid );
753 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
754 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
756 backend_connection_init(c);
761 void connection2anonymous( Connection *c )
763 assert( connections != NULL );
767 ber_len_t max = sockbuf_max_incoming;
768 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
771 if ( !BER_BVISNULL( &c->c_authmech ) ) {
772 ch_free(c->c_authmech.bv_val);
774 BER_BVZERO( &c->c_authmech );
776 if ( !BER_BVISNULL( &c->c_dn ) ) {
777 ch_free(c->c_dn.bv_val);
779 BER_BVZERO( &c->c_dn );
781 if ( !BER_BVISNULL( &c->c_ndn ) ) {
782 ch_free(c->c_ndn.bv_val);
784 BER_BVZERO( &c->c_ndn );
786 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
787 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
789 BER_BVZERO( &c->c_sasl_authz_dn );
791 c->c_authz_backend = NULL;
795 connection_destroy( Connection *c )
797 /* note: connections_mutex should be locked by caller */
799 unsigned long connid;
800 const char *close_reason;
802 assert( connections != NULL );
804 assert( c->c_struct_state != SLAP_C_UNUSED );
805 assert( c->c_conn_state != SLAP_C_INVALID );
806 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
807 assert( c->c_writewaiter == 0);
809 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
810 connid = c->c_connid;
811 close_reason = c->c_close_reason;
813 backend_connection_destroy(c);
818 c->c_activitytime = c->c_starttime = 0;
820 connection2anonymous( c );
821 c->c_listener = NULL;
823 if(c->c_peer_domain.bv_val != NULL) {
824 free(c->c_peer_domain.bv_val);
826 BER_BVZERO( &c->c_peer_domain );
827 if(c->c_peer_name.bv_val != NULL) {
828 free(c->c_peer_name.bv_val);
830 BER_BVZERO( &c->c_peer_name );
832 c->c_sasl_bind_in_progress = 0;
833 if(c->c_sasl_bind_mech.bv_val != NULL) {
834 free(c->c_sasl_bind_mech.bv_val);
836 BER_BVZERO( &c->c_sasl_bind_mech );
838 slap_sasl_close( c );
840 if ( c->c_currentber != NULL ) {
841 ber_free( c->c_currentber, 1 );
842 c->c_currentber = NULL;
845 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
846 if ( sd != AC_SOCKET_INVALID ) {
847 slapd_remove( sd, 1, 0 );
849 Statslog( LDAP_DEBUG_STATS, (close_reason
850 ? "conn=%lu fd=%ld closed (%s)\n"
851 : "conn=%lu fd=%ld closed\n"),
852 connid, (long) sd, close_reason, 0, 0 );
855 ber_sockbuf_free( c->c_sb );
857 c->c_sb = ber_sockbuf_alloc( );
860 ber_len_t max = sockbuf_max_incoming;
861 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
864 c->c_conn_state = SLAP_C_INVALID;
865 c->c_struct_state = SLAP_C_UNUSED;
866 c->c_close_reason = "?"; /* should never be needed */
869 /* call destructors, then constructors; avoids unnecessary allocation */
870 if ( slapi_plugins_used ) {
871 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
876 int connection_state_closing( Connection *c )
878 /* c_mutex must be locked by caller */
882 assert( c->c_struct_state == SLAP_C_USED );
884 state = c->c_conn_state;
886 assert( state != SLAP_C_INVALID );
888 return state == SLAP_C_CLOSING;
891 static void connection_abandon( Connection *c )
893 /* c_mutex must be locked by caller */
895 Operation *o, *next, op = {0};
901 op.o_connid = c->c_connid;
902 op.o_tag = LDAP_REQ_ABANDON;
903 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
904 next = LDAP_STAILQ_NEXT( o, o_next );
905 op.orn_msgid = o->o_msgid;
907 op.o_bd = frontendDB;
908 frontendDB->be_abandon( &op, &rs );
911 /* remove pending operations */
912 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
913 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
914 LDAP_STAILQ_NEXT(o, o_next) = NULL;
919 void connection_closing( Connection *c, const char *why )
921 assert( connections != NULL );
923 assert( c->c_struct_state == SLAP_C_USED );
924 assert( c->c_conn_state != SLAP_C_INVALID );
926 /* c_mutex must be locked by caller */
928 if( c->c_conn_state != SLAP_C_CLOSING ) {
931 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
932 Debug( LDAP_DEBUG_TRACE,
933 "connection_closing: readying conn=%lu sd=%d for close\n",
934 c->c_connid, sd, 0 );
935 /* update state to closing */
936 c->c_conn_state = SLAP_C_CLOSING;
937 c->c_close_reason = why;
939 /* don't listen on this port anymore */
940 slapd_clr_read( sd, 1 );
942 /* abandon active operations */
943 connection_abandon( c );
945 /* wake write blocked operations */
946 slapd_clr_write( sd, 1 );
947 if ( c->c_writewaiter ) {
948 ldap_pvt_thread_cond_signal( &c->c_write_cv );
949 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
950 ldap_pvt_thread_yield();
951 ldap_pvt_thread_mutex_lock( &c->c_mutex );
953 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
954 /* Client closed connection after doing Unbind. */
955 c->c_close_reason = NULL;
959 static void connection_close( Connection *c )
963 assert( connections != NULL );
965 assert( c->c_struct_state == SLAP_C_USED );
966 assert( c->c_conn_state == SLAP_C_CLOSING );
968 /* note: connections_mutex and c_mutex should be locked by caller */
970 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
971 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
972 Debug( LDAP_DEBUG_TRACE,
973 "connection_close: deferring conn=%lu sd=%d\n",
974 c->c_connid, sd, 0 );
978 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
979 c->c_connid, sd, 0 );
980 connection_destroy( c );
983 unsigned long connections_nextid(void)
986 assert( connections != NULL );
988 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
992 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
997 Connection* connection_first( ber_socket_t *index )
999 #ifdef SLAP_MULTI_CONN_ARRAY
1003 assert( connections != NULL );
1004 assert( index != NULL );
1006 #ifdef SLAP_MULTI_CONN_ARRAY
1007 for ( conn_array_id = 0;
1008 conn_array_id < NUM_CONNECTION_ARRAY;
1011 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1014 ldap_pvt_thread_mutex_lock( &connections_mutex );
1019 return connection_next(NULL, index);
1022 Connection* connection_next( Connection *c, ber_socket_t *index )
1023 #ifdef SLAP_MULTI_CONN_ARRAY
1027 assert( connections != NULL );
1028 assert( index != NULL );
1029 assert( *index <= (dtblsize/NUM_CONNECTION_ARRAY) );
1032 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1037 for(; *index < dtblsize; (*index)++) {
1038 conn = MCA_GET_CONNECTION(*index);
1039 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1040 assert( conn->c_conn_state == SLAP_C_INVALID );
1041 #ifndef HAVE_WINSOCK
1048 if( conn->c_struct_state == SLAP_C_USED ) {
1049 assert( conn->c_conn_state != SLAP_C_INVALID );
1055 assert( conn->c_struct_state == SLAP_C_UNUSED );
1056 assert( conn->c_conn_state == SLAP_C_INVALID );
1060 ldap_pvt_thread_mutex_lock( &c->c_mutex );
1068 assert( connections != NULL );
1069 assert( index != NULL );
1070 assert( *index <= dtblsize );
1073 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1078 for(; *index < dtblsize; (*index)++) {
1079 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1080 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1081 #ifndef HAVE_WINSOCK
1088 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1089 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1090 c = &connections[(*index)++];
1094 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1095 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1099 ldap_pvt_thread_mutex_lock( &c->c_mutex );
1106 void connection_done( Connection *c )
1108 #ifdef SLAP_MULTI_CONN_ARRAY
1112 assert( connections != NULL );
1115 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1118 #ifdef SLAP_MULTI_CONN_ARRAY
1119 for ( conn_array_id = 0;
1120 conn_array_id < NUM_CONNECTION_ARRAY;
1123 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1126 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1131 * connection_activity - handle the request operation op on connection
1132 * conn. This routine figures out what kind of operation it is and
1133 * calls the appropriate stub to handle it.
1136 #ifdef SLAPD_MONITOR
1137 /* FIXME: returns 0 in case of failure */
1138 #define INCR_OP_INITIATED(index) \
1140 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1141 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1142 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1144 #define INCR_OP_COMPLETED(index) \
1146 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1147 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1148 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1149 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1151 #else /* !SLAPD_MONITOR */
1152 #define INCR_OP_INITIATED(index) do { } while (0)
1153 #define INCR_OP_COMPLETED(index) \
1155 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1156 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1157 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1159 #endif /* !SLAPD_MONITOR */
1162 * NOTE: keep in sync with enum in slapd.h
1164 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1179 connection_operation( void *ctx, void *arg_v )
1181 int rc = LDAP_OTHER;
1182 Operation *op = arg_v;
1183 SlapReply rs = {REP_RESULT};
1184 ber_tag_t tag = op->o_tag;
1186 Connection *conn = op->o_conn;
1187 void *memctx = NULL;
1188 void *memctx_null = NULL;
1191 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1192 /* FIXME: returns 0 in case of failure */
1193 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1194 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1196 op->o_threadctx = ctx;
1200 case LDAP_REQ_UNBIND:
1202 case LDAP_REQ_DELETE:
1203 case LDAP_REQ_MODDN:
1204 case LDAP_REQ_MODIFY:
1205 case LDAP_REQ_COMPARE:
1206 case LDAP_REQ_SEARCH:
1207 case LDAP_REQ_ABANDON:
1208 case LDAP_REQ_EXTENDED:
1211 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1212 "conn %lu unknown LDAP request 0x%lx\n",
1213 conn->c_connid, tag, 0 );
1214 op->o_tag = LBER_ERROR;
1215 rs.sr_err = LDAP_PROTOCOL_ERROR;
1216 rs.sr_text = "unknown LDAP request";
1217 send_ldap_disconnect( op, &rs );
1218 rc = SLAPD_DISCONNECT;
1219 goto operations_error;
1222 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1223 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1224 "error: SASL bind in progress (tag=%ld).\n",
1226 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1227 "SASL bind in progress" );
1228 rc = LDAP_OPERATIONS_ERROR;
1229 goto operations_error;
1232 /* We can use Thread-Local storage for most mallocs. We can
1233 * also use TL for ber parsing, but not on Add or Modify.
1236 memsiz = ber_len( op->o_ber ) * 64;
1237 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1239 memsiz = SLAP_SLAB_SIZE;
1241 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1242 op->o_tmpmemctx = memctx;
1243 op->o_tmpmfuncs = &slap_sl_mfuncs;
1244 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1245 /* Note - the ber and its buffer are already allocated from
1246 * regular memory; this only affects subsequent mallocs that
1247 * ber_scanf may invoke.
1249 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1254 opidx = SLAP_OP_BIND;
1257 case LDAP_REQ_UNBIND:
1258 opidx = SLAP_OP_UNBIND;
1262 opidx = SLAP_OP_ADD;
1265 case LDAP_REQ_DELETE:
1266 opidx = SLAP_OP_DELETE;
1269 case LDAP_REQ_MODRDN:
1270 opidx = SLAP_OP_MODRDN;
1273 case LDAP_REQ_MODIFY:
1274 opidx = SLAP_OP_MODIFY;
1277 case LDAP_REQ_COMPARE:
1278 opidx = SLAP_OP_COMPARE;
1281 case LDAP_REQ_SEARCH:
1282 opidx = SLAP_OP_SEARCH;
1285 case LDAP_REQ_ABANDON:
1286 opidx = SLAP_OP_ABANDON;
1289 case LDAP_REQ_EXTENDED:
1290 opidx = SLAP_OP_EXTENDED;
1298 assert( opidx > -1 );
1299 INCR_OP_INITIATED( opidx );
1300 rc = (*(opfun[opidx]))( op, &rs );
1303 if ( rc == SLAPD_DISCONNECT ) {
1306 } else if ( opidx > -1 ) {
1307 /* increment completed operations count
1308 * only if operation was initiated
1309 * and rc != SLAPD_DISCONNECT */
1310 INCR_OP_COMPLETED( opidx );
1313 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1314 if ( rc == SLAPD_ABANDON )
1315 op->o_cancel = SLAP_CANCEL_ACK;
1317 op->o_cancel = LDAP_TOO_LATE;
1319 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1320 op->o_cancel != SLAP_CANCEL_DONE )
1322 ldap_pvt_thread_yield();
1325 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1327 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1329 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1330 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1332 conn->c_n_ops_executing--;
1333 conn->c_n_ops_completed++;
1337 case LDAP_REQ_UNBIND:
1338 /* c_mutex is locked */
1340 conn, tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1344 conn->c_sasl_bind_in_progress =
1345 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1347 if( conn->c_conn_state == SLAP_C_BINDING) {
1348 conn->c_conn_state = SLAP_C_ACTIVE;
1352 connection_resched( conn );
1353 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1357 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1359 int connection_client_setup(
1361 ldap_pvt_thread_start_t *func,
1367 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1368 CONN_IS_CLIENT, 0, NULL );
1369 if ( rc < 0 ) return -1;
1371 c = connection_get( s );
1372 c->c_clientfunc = func;
1373 c->c_clientarg = arg;
1374 connection_return( c );
1375 slapd_add_internal( s, 0 );
1376 slapd_set_read( s, 1 );
1380 void connection_client_enable(
1383 slapd_set_read( s, 1 );
1386 void connection_client_stop(
1391 /* get (locked) connection */
1392 c = connection_get( s );
1394 assert( c->c_conn_state == SLAP_C_CLIENT );
1396 c->c_listener = NULL;
1397 c->c_conn_state = SLAP_C_INVALID;
1398 c->c_struct_state = SLAP_C_UNUSED;
1399 c->c_close_reason = "?"; /* should never be needed */
1400 ber_sockbuf_free( c->c_sb );
1401 c->c_sb = ber_sockbuf_alloc( );
1403 ber_len_t max = sockbuf_max_incoming;
1404 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1407 connection_return( c );
1408 slapd_remove( s, 0, 1 );
1411 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1412 void* connection_processing_thread( void* ctx, void* argv )
1415 Operation* new_op = NULL;
1416 ber_socket_t s = (ber_socket_t)argv;
1419 * read incoming LDAP requests. If there is more than one,
1420 * the first one is returned with new_op
1422 if( ( rc = connection_read( s, &new_op ) ) < 0 ) {
1423 Debug( LDAP_DEBUG_TRACE, "connection_read(%d) error\n", s, 0, 0 );
1428 /* execute the queued request in the same thread */
1430 rc = (int)connection_operation(
1431 ldap_pvt_thread_pool_context(), new_op );
1437 int connection_processing_activate( ber_socket_t s )
1442 * suspend reading on this file descriptor until a connection processing
1443 * thread reads data on it. Otherwise the listener thread will repeatedly
1444 * submit the same event on it to the pool.
1446 if( !slapd_suspend( s ) ) return 0;
1448 status = ldap_pvt_thread_pool_submit( &connection_pool,
1449 connection_processing_thread, (void *) s );
1452 Debug( LDAP_DEBUG_ANY, "connection_processing_activiate(%d): "
1453 "ldap_pvt_thread_pool_submit failed\n",
1462 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1463 int connection_read( ber_socket_t s, Operation** op )
1465 int connection_read(ber_socket_t s)
1470 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1471 int need_resume = 1;
1474 assert( connections != NULL );
1476 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1478 /* get (locked) connection */
1479 c = connection_get( s );
1482 Debug( LDAP_DEBUG_ANY,
1483 "connection_read(%ld): no connection!\n",
1485 slapd_remove(s, 1, 0);
1487 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1493 if( c->c_conn_state == SLAP_C_CLOSING ) {
1494 Debug( LDAP_DEBUG_TRACE,
1495 "connection_read(%d): closing, ignoring input for id=%lu\n",
1496 s, c->c_connid, 0 );
1497 connection_return( c );
1498 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1500 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1506 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1507 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1510 slapd_clr_read( s, 0 );
1511 ldap_pvt_thread_pool_submit( &connection_pool,
1512 c->c_clientfunc, c->c_clientarg );
1514 connection_return( c );
1515 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1519 Debug( LDAP_DEBUG_TRACE,
1520 "connection_read(%d): checking for input on id=%lu\n",
1521 s, c->c_connid, 0 );
1524 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1525 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1527 Debug( LDAP_DEBUG_TRACE,
1528 "connection_read(%d): TLS accept error "
1529 "error=%d id=%lu, closing\n",
1530 s, rc, c->c_connid );
1532 c->c_needs_tls_accept = 0;
1533 /* connections_mutex and c_mutex are locked */
1534 connection_closing( c, "TLS negotiation failure" );
1540 /* Drain input before close, to allow SSL error codes
1541 * to propagate to client. */
1547 rc = select(s+1, &rfd, NULL, NULL, &tv);
1549 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1555 connection_close( c );
1557 } else if ( rc == 0 ) {
1559 struct berval authid = BER_BVNULL;
1561 c->c_needs_tls_accept = 0;
1563 /* we need to let SASL know */
1564 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1566 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1567 if( c->c_tls_ssf > c->c_ssf ) {
1568 c->c_ssf = c->c_tls_ssf;
1571 rc = dnX509peerNormalize( ssl, &authid );
1572 if ( rc != LDAP_SUCCESS ) {
1573 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1574 "unable to get TLS client DN, error=%d id=%lu\n",
1575 s, rc, c->c_connid );
1577 Statslog( LDAP_DEBUG_STATS,
1578 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1579 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1580 slap_sasl_external( c, c->c_tls_ssf, &authid );
1581 if ( authid.bv_val ) free( authid.bv_val );
1584 /* if success and data is ready, fall thru to data input loop */
1586 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1588 connection_return( c );
1589 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1591 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1599 #ifdef HAVE_CYRUS_SASL
1600 if ( c->c_sasl_layers ) {
1601 /* If previous layer is not removed yet, give up for now */
1602 if ( !c->c_sasl_sockctx ) {
1603 connection_return( c );
1604 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1606 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1612 c->c_sasl_layers = 0;
1614 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1615 if( rc != LDAP_SUCCESS ) {
1616 Debug( LDAP_DEBUG_TRACE,
1617 "connection_read(%d): SASL install error "
1618 "error=%d id=%lu, closing\n",
1619 s, rc, c->c_connid );
1620 /* connections_mutex and c_mutex are locked */
1621 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1624 connection_closing( c, "SASL layer install failure" );
1625 connection_close( c );
1626 connection_return( c );
1627 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1633 #define CONNECTION_INPUT_LOOP 1
1634 /* #define DATA_READY_LOOP 1 */
1637 /* How do we do this without getting into a busy loop ? */
1638 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1639 rc = connection_input( c, op );
1641 rc = connection_input( c );
1643 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1644 if( *op && (*op)->o_tag == LDAP_REQ_UNBIND ) {
1646 * After the reception of an unbind request,
1647 * no more incoming requests via the connection
1648 * is expected. Therefore, don't resume connection reading.
1654 #ifdef DATA_READY_LOOP
1655 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1656 #elif CONNECTION_INPUT_LOOP
1663 Debug( LDAP_DEBUG_TRACE,
1664 "connection_read(%d): input error=%d id=%lu, closing.\n",
1665 s, rc, c->c_connid );
1667 /* connections_mutex and c_mutex are locked */
1668 connection_closing( c, conn_lost_str );
1669 connection_close( c );
1670 connection_return( c );
1671 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1675 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1676 if ( need_resume ) slapd_resume( s );
1679 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1680 slapd_set_read( s, 1 );
1683 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1684 slapd_set_write( s, 1 );
1687 connection_return( c );
1688 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1693 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1695 connection_input( Connection *conn, Operation** c_op )
1698 connection_input( Connection *conn )
1707 #ifdef LDAP_CONNECTIONLESS
1713 if ( conn->c_currentber == NULL &&
1714 ( conn->c_currentber = ber_alloc()) == NULL )
1716 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1722 #ifdef LDAP_CONNECTIONLESS
1723 if ( conn->c_is_udp ) {
1724 char peername[sizeof("IP=255.255.255.255:65336")];
1726 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1727 sizeof(struct sockaddr));
1728 if (len != sizeof(struct sockaddr)) return 1;
1730 sprintf( peername, "IP=%s:%d",
1731 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1732 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1733 Statslog( LDAP_DEBUG_STATS,
1734 "conn=%lu UDP request from %s (%s) accepted.\n",
1735 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1739 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1740 if ( tag != LDAP_TAG_MESSAGE ) {
1744 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1746 Debug( LDAP_DEBUG_TRACE,
1747 "ber_get_next on fd %d failed errno=%d (%s)\n",
1748 sd, err, sock_errstr(err) );
1749 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1750 /* log, close and send error */
1751 ber_free( conn->c_currentber, 1 );
1752 conn->c_currentber = NULL;
1759 ber = conn->c_currentber;
1760 conn->c_currentber = NULL;
1762 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1763 /* log, close and send error */
1764 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1770 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1771 /* log, close and send error */
1772 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1779 #ifdef LDAP_CONNECTIONLESS
1780 if( conn->c_is_udp ) {
1781 if( tag == LBER_OCTETSTRING ) {
1782 ber_get_stringa( ber, &cdn );
1783 tag = ber_peek_tag(ber, &len);
1785 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1786 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1793 if(tag == LDAP_REQ_BIND) {
1794 /* immediately abandon all existing operations upon BIND */
1795 connection_abandon( conn );
1798 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1801 /* clear state if the connection is being reused from inactive */
1802 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1803 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1806 op->o_res_ber = NULL;
1808 #ifdef LDAP_CONNECTIONLESS
1809 if (conn->c_is_udp) {
1811 ber_str2bv( cdn, 0, 1, &op->o_dn );
1812 op->o_protocol = LDAP_VERSION2;
1814 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1815 if (op->o_res_ber == NULL) return 1;
1817 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1818 sizeof(struct sockaddr), 0 );
1820 if (rc != sizeof(struct sockaddr)) {
1821 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1825 if (op->o_protocol == LDAP_VERSION2) {
1826 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1828 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1833 #endif /* LDAP_CONNECTIONLESS */
1837 /* Don't process requests when the conn is in the middle of a
1838 * Bind, or if it's closing. Also, don't let any single conn
1839 * use up all the available threads, and don't execute if we're
1840 * currently blocked on output. And don't execute if there are
1841 * already pending ops, let them go first. Abandon operations
1842 * get exceptions to some, but not all, cases.
1846 /* Abandon and Unbind are exempt from these checks */
1847 if (conn->c_conn_state == SLAP_C_CLOSING) {
1850 } else if (conn->c_writewaiter) {
1851 defer = "awaiting write";
1853 } else if (conn->c_n_ops_pending) {
1854 defer = "pending operations";
1858 case LDAP_REQ_ABANDON:
1859 /* Unbind is exempt from these checks */
1860 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1861 defer = "too many executing";
1863 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1868 case LDAP_REQ_UNBIND:
1873 int max = conn->c_dn.bv_len
1874 ? slap_conn_max_pending_auth
1875 : slap_conn_max_pending;
1877 Debug( LDAP_DEBUG_ANY,
1878 "connection_input: conn=%lu deferring operation: %s\n",
1879 conn->c_connid, defer, 0 );
1880 conn->c_n_ops_pending++;
1881 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1882 if ( conn->c_n_ops_pending > max ) {
1888 conn->c_n_ops_executing++;
1889 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1891 * The first op will be processed in the same thread context,
1892 * Subsequent ops will be submitted to the pool by
1893 * calling connection_op_activate()
1895 if ( *c_op == NULL ) {
1896 /* the first incoming request */
1897 connection_op_queue( op );
1902 connection_op_activate( op );
1907 if ( conn->c_struct_state != SLAP_C_USED ) {
1908 /* connection must have got closed underneath us */
1912 assert( conn->c_struct_state == SLAP_C_USED );
1918 connection_resched( Connection *conn )
1922 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1925 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1927 /* use trylock to avoid possible deadlock */
1928 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1931 Debug( LDAP_DEBUG_TRACE,
1932 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1933 conn->c_connid, sd, 0 );
1935 * reaquire locks in the right order...
1936 * this may allow another thread to close this connection,
1937 * so recheck state below.
1939 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1940 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1941 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1944 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1945 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1946 "closed by other thread conn=%lu sd=%d\n",
1947 conn->c_connid, sd, 0 );
1949 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1950 "attempting closing conn=%lu sd=%d\n",
1951 conn->c_connid, sd, 0 );
1952 connection_close( conn );
1955 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1959 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1960 /* other states need different handling */
1964 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1965 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1968 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1969 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1970 /* pending operations should not be marked for abandonment */
1971 assert(!op->o_abandon);
1973 conn->c_n_ops_pending--;
1974 conn->c_n_ops_executing++;
1976 connection_op_activate( op );
1978 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1986 connection_init_log_prefix( Operation *op )
1988 if ( op->o_connid == (unsigned long)(-1) ) {
1989 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1990 "conn=-1 op=%lu", op->o_opid );
1993 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1994 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1998 static void connection_op_queue( Operation *op )
2001 ber_tag_t tag = op->o_tag;
2003 if(tag == LDAP_REQ_BIND) {
2004 op->o_conn->c_conn_state = SLAP_C_BINDING;
2007 if (!op->o_dn.bv_len) {
2008 op->o_authz = op->o_conn->c_authz;
2009 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2010 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2011 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2013 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2014 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2018 op->o_authtype = op->o_conn->c_authtype;
2019 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2021 if (!op->o_protocol) {
2022 op->o_protocol = op->o_conn->c_protocol
2023 ? op->o_conn->c_protocol : LDAP_VERSION3;
2026 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
2027 && op->o_protocol > LDAP_VERSION2)
2029 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2032 op->o_connid = op->o_conn->c_connid;
2033 connection_init_log_prefix( op );
2035 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2038 static int connection_op_activate( Operation *op )
2041 ber_tag_t tag = op->o_tag;
2043 connection_op_queue( op );
2045 status = ldap_pvt_thread_pool_submit( &connection_pool,
2046 connection_operation, (void *) op );
2048 if ( status != 0 ) {
2049 Debug( LDAP_DEBUG_ANY,
2050 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
2051 status, op->o_connid, 0 );
2052 /* should move op to pending list */
2058 int connection_write(ber_socket_t s)
2063 assert( connections != NULL );
2065 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2067 c = connection_get( s );
2069 Debug( LDAP_DEBUG_ANY,
2070 "connection_write(%ld): no connection!\n",
2072 slapd_remove(s, 1, 0);
2073 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2077 slapd_clr_write( s, 0);
2080 Debug( LDAP_DEBUG_TRACE,
2081 "connection_write(%d): waking output for id=%lu\n",
2082 s, c->c_connid, 0 );
2083 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2085 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2086 slapd_set_read( s, 1 );
2088 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2089 slapd_set_write( s, 1 );
2092 /* If there are ops pending because of a writewaiter,
2095 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2096 if ( !c->c_writewaiter ) break;
2097 if ( c->c_n_ops_executing > connection_pool_max/2 ) {
2100 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2101 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2102 /* pending operations should not be marked for abandonment */
2103 assert(!op->o_abandon);
2105 c->c_n_ops_pending--;
2106 c->c_n_ops_executing++;
2108 connection_op_activate( op );
2112 connection_return( c );
2114 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2120 connection_fake_init(
2125 conn->c_connid = -1;
2126 conn->c_send_ldap_result = slap_send_ldap_result;
2127 conn->c_send_search_entry = slap_send_search_entry;
2128 conn->c_send_search_reference = slap_send_search_reference;
2129 conn->c_listener = (Listener *)&dummy_list;
2130 conn->c_peer_domain = slap_empty_bv;
2131 conn->c_peer_name = slap_empty_bv;
2133 memset(op, 0, OPERATION_BUFFER_SIZE);
2134 op->o_hdr = (Opheader *)(op+1);
2135 op->o_controls = (void **)(op->o_hdr+1);
2136 /* set memory context */
2137 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2138 op->o_tmpmfuncs = &slap_sl_mfuncs;
2139 op->o_threadctx = ctx;
2142 op->o_connid = op->o_conn->c_connid;
2143 connection_init_log_prefix( op );
2145 op->o_time = slap_get_time();
2149 connection_assign_nextid( Connection *conn )
2151 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2152 conn->c_connid = conn_nextid++;
2153 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );