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_DISPATCHER
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_DISPATCHER
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_DISPATCHER
1412 static int connection_read( ber_socket_t s, Operation** op );
1414 static void* connection_read_thread( void* ctx, void* argv )
1417 Operation* new_op = NULL;
1418 ber_socket_t s = (ber_socket_t)argv;
1421 * read incoming LDAP requests. If there is more than one,
1422 * the first one is returned with new_op
1424 if( ( rc = connection_read( s, &new_op ) ) < 0 ) {
1425 Debug( LDAP_DEBUG_TRACE, "connection_read(%d) error\n", s, 0, 0 );
1430 /* execute the queued request in the same thread */
1432 rc = (int)connection_operation(
1433 ldap_pvt_thread_pool_context(), new_op );
1439 int connection_read_activate( ber_socket_t s )
1444 * suspend reading on this file descriptor until a connection processing
1445 * thread reads data on it. Otherwise the listener thread will repeatedly
1446 * submit the same event on it to the pool.
1448 slapd_clr_read( s, 0 );
1450 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1451 connection_read_thread, (void *) s );
1454 Debug( LDAP_DEBUG_ANY,
1455 "connection_read_activiate(%d): submit failed (%d)\n",
1463 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1465 connection_read( ber_socket_t s, Operation** op )
1467 int connection_read(ber_socket_t s)
1473 assert( connections != NULL );
1475 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1477 /* get (locked) connection */
1478 c = connection_get( s );
1481 Debug( LDAP_DEBUG_ANY,
1482 "connection_read(%ld): no connection!\n",
1484 slapd_remove(s, 1, 0);
1486 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1492 if( c->c_conn_state == SLAP_C_CLOSING ) {
1493 Debug( LDAP_DEBUG_TRACE,
1494 "connection_read(%d): closing, ignoring input for id=%lu\n",
1495 s, c->c_connid, 0 );
1496 connection_return( c );
1497 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1499 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1500 slapd_set_read( s, 1);
1505 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1506 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1507 /* read should already be cleared */
1509 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_DISPATCHER
1592 slapd_set_read( s, 1 );
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_DISPATCHER
1607 slapd_set_read( s, 1 );
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 );
1621 /* connections_mutex and c_mutex are locked */
1623 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1624 slapd_set_read( s, 1 );
1627 connection_closing( c, "SASL layer install failure" );
1628 connection_close( c );
1629 connection_return( c );
1630 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1636 #define CONNECTION_INPUT_LOOP 1
1637 /* #define DATA_READY_LOOP 1 */
1640 /* How do we do this without getting into a busy loop ? */
1641 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1642 rc = connection_input( c, op );
1644 rc = connection_input( c );
1647 #ifdef DATA_READY_LOOP
1648 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1649 #elif CONNECTION_INPUT_LOOP
1656 Debug( LDAP_DEBUG_TRACE,
1657 "connection_read(%d): input error=%d id=%lu, closing.\n",
1658 s, rc, c->c_connid );
1660 /* connections_mutex and c_mutex are locked */
1661 connection_closing( c, conn_lost_str );
1662 connection_close( c );
1663 connection_return( c );
1664 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1668 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1669 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1670 slapd_set_write( s, 0 );
1673 slapd_set_read( s, 1 );
1675 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1676 slapd_set_read( s, 1 );
1679 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1680 slapd_set_write( s, 1 );
1684 connection_return( c );
1685 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1691 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1692 connection_input( Connection *conn , Operation** c_op )
1694 connection_input( Connection *conn )
1703 #ifdef LDAP_CONNECTIONLESS
1709 if ( conn->c_currentber == NULL &&
1710 ( conn->c_currentber = ber_alloc()) == NULL )
1712 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1718 #ifdef LDAP_CONNECTIONLESS
1719 if ( conn->c_is_udp ) {
1720 char peername[sizeof("IP=255.255.255.255:65336")];
1722 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1723 if (len != sizeof(struct sockaddr)) return 1;
1725 sprintf( peername, "IP=%s:%d",
1726 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1727 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1728 Statslog( LDAP_DEBUG_STATS,
1729 "conn=%lu UDP request from %s (%s) accepted.\n",
1730 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1734 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1735 if ( tag != LDAP_TAG_MESSAGE ) {
1739 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1741 Debug( LDAP_DEBUG_TRACE,
1742 "ber_get_next on fd %d failed errno=%d (%s)\n",
1743 sd, err, sock_errstr(err) );
1744 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1745 /* log, close and send error */
1746 ber_free( conn->c_currentber, 1 );
1747 conn->c_currentber = NULL;
1754 ber = conn->c_currentber;
1755 conn->c_currentber = NULL;
1757 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1758 /* log, close and send error */
1759 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1764 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1765 /* log, close and send error */
1766 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1772 #ifdef LDAP_CONNECTIONLESS
1773 if( conn->c_is_udp ) {
1774 if( tag == LBER_OCTETSTRING ) {
1775 ber_get_stringa( ber, &cdn );
1776 tag = ber_peek_tag(ber, &len);
1778 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1779 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1786 if(tag == LDAP_REQ_BIND) {
1787 /* immediately abandon all existing operations upon BIND */
1788 connection_abandon( conn );
1791 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1794 /* clear state if the connection is being reused from inactive */
1795 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1796 memset( &conn->c_pagedresults_state, 0,
1797 sizeof( conn->c_pagedresults_state ) );
1800 op->o_res_ber = NULL;
1802 #ifdef LDAP_CONNECTIONLESS
1803 if (conn->c_is_udp) {
1805 ber_str2bv( cdn, 0, 1, &op->o_dn );
1806 op->o_protocol = LDAP_VERSION2;
1808 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1809 if (op->o_res_ber == NULL) return 1;
1811 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1812 sizeof(struct sockaddr), 0 );
1814 if (rc != sizeof(struct sockaddr)) {
1815 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1819 if (op->o_protocol == LDAP_VERSION2) {
1820 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1822 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1827 #endif /* LDAP_CONNECTIONLESS */
1831 /* Don't process requests when the conn is in the middle of a
1832 * Bind, or if it's closing. Also, don't let any single conn
1833 * use up all the available threads, and don't execute if we're
1834 * currently blocked on output. And don't execute if there are
1835 * already pending ops, let them go first. Abandon operations
1836 * get exceptions to some, but not all, cases.
1840 /* Abandon and Unbind are exempt from these checks */
1841 if (conn->c_conn_state == SLAP_C_CLOSING) {
1844 } else if (conn->c_writewaiter) {
1845 defer = "awaiting write";
1847 } else if (conn->c_n_ops_pending) {
1848 defer = "pending operations";
1852 case LDAP_REQ_ABANDON:
1853 /* Unbind is exempt from these checks */
1854 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1855 defer = "too many executing";
1857 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1862 case LDAP_REQ_UNBIND:
1867 int max = conn->c_dn.bv_len
1868 ? slap_conn_max_pending_auth
1869 : slap_conn_max_pending;
1871 Debug( LDAP_DEBUG_ANY,
1872 "connection_input: conn=%lu deferring operation: %s\n",
1873 conn->c_connid, defer, 0 );
1874 conn->c_n_ops_pending++;
1875 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1876 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1879 conn->c_n_ops_executing++;
1883 * The first op will be processed in the same thread context,
1884 * Subsequent ops will be submitted to the pool by
1885 * calling connection_op_activate()
1887 if ( *c_op == NULL ) {
1888 /* the first incoming request */
1889 connection_op_queue( op );
1894 connection_op_activate( op );
1899 if ( conn->c_struct_state != SLAP_C_USED ) {
1900 /* connection must have got closed underneath us */
1905 assert( conn->c_struct_state == SLAP_C_USED );
1910 connection_resched( Connection *conn )
1914 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1917 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1919 /* use trylock to avoid possible deadlock */
1920 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1923 Debug( LDAP_DEBUG_TRACE,
1924 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1925 conn->c_connid, sd, 0 );
1927 * reaquire locks in the right order...
1928 * this may allow another thread to close this connection,
1929 * so recheck state below.
1931 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1932 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1933 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1936 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1937 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1938 "closed by other thread conn=%lu sd=%d\n",
1939 conn->c_connid, sd, 0 );
1941 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1942 "attempting closing conn=%lu sd=%d\n",
1943 conn->c_connid, sd, 0 );
1944 connection_close( conn );
1947 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1951 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1952 /* other states need different handling */
1956 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1957 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1959 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1960 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1962 /* pending operations should not be marked for abandonment */
1963 assert(!op->o_abandon);
1965 conn->c_n_ops_pending--;
1966 conn->c_n_ops_executing++;
1968 connection_op_activate( op );
1970 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1976 connection_init_log_prefix( Operation *op )
1978 if ( op->o_connid == (unsigned long)(-1) ) {
1979 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1980 "conn=-1 op=%lu", op->o_opid );
1983 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1984 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1988 static void connection_op_queue( Operation *op )
1991 ber_tag_t tag = op->o_tag;
1993 if (tag == LDAP_REQ_BIND) {
1994 op->o_conn->c_conn_state = SLAP_C_BINDING;
1997 if (!op->o_dn.bv_len) {
1998 op->o_authz = op->o_conn->c_authz;
1999 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2000 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2001 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2003 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2004 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2008 op->o_authtype = op->o_conn->c_authtype;
2009 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2011 if (!op->o_protocol) {
2012 op->o_protocol = op->o_conn->c_protocol
2013 ? op->o_conn->c_protocol : LDAP_VERSION3;
2016 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2017 op->o_protocol > LDAP_VERSION2)
2019 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2022 op->o_connid = op->o_conn->c_connid;
2023 connection_init_log_prefix( op );
2025 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2028 static int connection_op_activate( Operation *op )
2031 ber_tag_t tag = op->o_tag;
2033 connection_op_queue( op );
2035 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2036 connection_operation, (void *) op );
2039 Debug( LDAP_DEBUG_ANY,
2040 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2041 rc, op->o_connid, 0 );
2042 /* should move op to pending list */
2048 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2049 int connection_write_activate( ber_socket_t s )
2054 * suspend reading on this file descriptor until a connection processing
2055 * thread write data on it. Otherwise the listener thread will repeatedly
2056 * submit the same event on it to the pool.
2059 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2060 slapd_clr_write( s, 0);
2064 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2065 connection_read_thread, (void *) s );
2068 Debug( LDAP_DEBUG_ANY,
2069 "connection_write_activiate(%d): submit failed (%d)\n",
2077 int connection_write(ber_socket_t s)
2082 assert( connections != NULL );
2084 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2086 c = connection_get( s );
2088 Debug( LDAP_DEBUG_ANY,
2089 "connection_write(%ld): no connection!\n",
2091 slapd_remove(s, 1, 0);
2092 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2096 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2097 slapd_clr_write( s, 0);
2101 Debug( LDAP_DEBUG_TRACE,
2102 "connection_write(%d): waking output for id=%lu\n",
2103 s, c->c_connid, 0 );
2104 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2106 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2107 slapd_set_read( s, 1 );
2109 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2110 slapd_set_write( s, 1 );
2113 /* If there are ops pending because of a writewaiter,
2116 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2117 if ( !c->c_writewaiter ) break;
2118 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2120 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2121 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2123 /* pending operations should not be marked for abandonment */
2124 assert(!op->o_abandon);
2126 c->c_n_ops_pending--;
2127 c->c_n_ops_executing++;
2129 connection_op_activate( op );
2133 connection_return( c );
2135 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2141 connection_fake_init(
2146 conn->c_connid = -1;
2147 conn->c_send_ldap_result = slap_send_ldap_result;
2148 conn->c_send_search_entry = slap_send_search_entry;
2149 conn->c_send_search_reference = slap_send_search_reference;
2150 conn->c_listener = (Listener *)&dummy_list;
2151 conn->c_peer_domain = slap_empty_bv;
2152 conn->c_peer_name = slap_empty_bv;
2154 memset(op, 0, OPERATION_BUFFER_SIZE);
2155 op->o_hdr = (Opheader *)(op+1);
2156 op->o_controls = (void **)(op->o_hdr+1);
2157 /* set memory context */
2158 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2159 op->o_tmpmfuncs = &slap_sl_mfuncs;
2160 op->o_threadctx = ctx;
2163 op->o_connid = op->o_conn->c_connid;
2164 connection_init_log_prefix( op );
2166 slap_op_time( &op->o_time, &op->o_tincr );
2170 connection_assign_nextid( Connection *conn )
2172 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2173 conn->c_connid = conn_nextid++;
2174 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );