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 2
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)
59 #define MCA_GET_CONNECTION(fd) &(connections[MCA_conn_array_id(fd)])[MCA_conn_array_element_id( fd )]
60 #define MCA_GET_CONN_MUTEX(fd) &connections_mutex[MCA_conn_array_id(fd)]
62 /* protected by connections_mutex */
63 static ldap_pvt_thread_mutex_t connections_mutex;
64 static Connection *connections = NULL;
67 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
68 static unsigned long conn_nextid = 0;
70 static const char conn_lost_str[] = "connection lost";
72 /* structure state (protected by connections_mutex) */
73 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
74 #define SLAP_C_UNUSED 0x01
75 #define SLAP_C_USED 0x02
77 /* connection state (protected by c_mutex ) */
78 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
79 #define SLAP_C_INACTIVE 0x01 /* zero threads */
80 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
81 #define SLAP_C_BINDING 0x03 /* binding */
82 #define SLAP_C_CLOSING 0x04 /* closing */
83 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
86 connection_state2str( int state )
89 case SLAP_C_INVALID: return "!";
90 case SLAP_C_INACTIVE: return "|";
91 case SLAP_C_ACTIVE: return "";
92 case SLAP_C_BINDING: return "B";
93 case SLAP_C_CLOSING: return "C";
94 case SLAP_C_CLIENT: return "L";
100 static Connection* connection_get( ber_socket_t s );
102 #ifdef SLAP_LIGHTWEIGHT_LISTENER
103 static int connection_input( Connection *c, Operation** op );
105 static int connection_input( Connection *c );
107 static void connection_close( Connection *c );
109 static int connection_op_activate( Operation *op );
110 #ifdef SLAP_LIGHTWEIGHT_LISTENER
111 static void connection_op_queue( Operation *op );
113 static int connection_resched( Connection *conn );
114 static void connection_abandon( Connection *conn );
115 static void connection_destroy( Connection *c );
117 static ldap_pvt_thread_start_t connection_operation;
120 * Initialize connection management infrastructure.
122 int connections_init(void)
123 #ifdef SLAP_MULTI_CONN_ARRAY
128 assert( connections == NULL );
130 if( connections != NULL) {
131 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
136 connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc( NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
137 if( connections_mutex == NULL ) {
138 Debug( LDAP_DEBUG_ANY,
139 "connections_init: allocation of connection mutex[%d] failed\n", i, 0, 0 );
143 connections = (Connection**) ch_calloc( NUM_CONNECTION_ARRAY, sizeof(Connection*));
144 if( connections == NULL ) {
145 Debug( LDAP_DEBUG_ANY,
146 "connections_init: allocation of connection[%d] failed\n", 0, 0, 0 );
150 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
151 ldap_pvt_thread_mutex_init( connections_mutex+i );
152 connections[i] = (Connection*) ch_calloc( (dtblsize/NUM_CONNECTION_ARRAY), sizeof(Connection) );
153 if( connections[i] == NULL ) {
154 Debug( LDAP_DEBUG_ANY,
155 "connections_init: allocation (%d*%ld) of connection array[%d] failed\n",
156 dtblsize, (long) sizeof(Connection), i );
161 /* should check return of every call */
162 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
164 assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
165 assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state == SLAP_C_UNINITIALIZED );
167 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
168 conn = connections[i];
169 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
170 conn[j].c_conn_idx = j;
175 * per entry initialization of the Connection array initialization
176 * will be done by connection_init()
185 assert( connections == NULL );
187 if( connections != NULL) {
188 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
193 /* should check return of every call */
194 ldap_pvt_thread_mutex_init( &connections_mutex );
195 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
197 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
199 if( connections == NULL ) {
200 Debug( LDAP_DEBUG_ANY,
201 "connections_init: allocation (%d*%ld) of connection array failed\n",
202 dtblsize, (long) sizeof(Connection), 0 );
206 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
207 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
209 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
212 * per entry initialization of the Connection array initialization
213 * will be done by connection_init()
221 * Destroy connection management infrastructure.
224 int connections_destroy(void)
225 #ifdef SLAP_MULTI_CONN_ARRAY
230 if( connections == NULL) {
231 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
236 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
237 Connection* conn = connections[i];
238 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
239 if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
240 ber_sockbuf_free( conn[j].c_sb );
241 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
242 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
243 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
246 if ( slapi_plugins_used ) {
247 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
255 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
256 free( connections[i] );
257 connections[i] = NULL;
258 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
261 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
270 /* should check return of every call */
272 if( connections == NULL) {
273 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
278 for ( i = 0; i < dtblsize; i++ ) {
279 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
280 ber_sockbuf_free( connections[i].c_sb );
281 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
282 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
283 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
285 if ( slapi_plugins_used ) {
286 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
296 ldap_pvt_thread_mutex_destroy( &connections_mutex );
297 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
303 * shutdown all connections
305 int connections_shutdown(void)
306 #ifdef SLAP_MULTI_CONN_ARRAY
311 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
312 Connection* conn = connections[i];
313 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
314 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
315 if( conn[j].c_struct_state != SLAP_C_USED ) {
318 /* give persistent clients a chance to cleanup */
319 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
320 ldap_pvt_thread_pool_submit( &connection_pool,
321 conn[j].c_clientfunc, conn[j].c_clientarg );
325 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
326 /* connections_mutex and c_mutex are locked */
327 connection_closing( &conn[j], "connection shutdown" );
328 connection_close( &conn[j] );
329 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
332 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
342 ldap_pvt_thread_mutex_lock( &connections_mutex );
344 for ( i = 0; i < dtblsize; i++ ) {
345 if( connections[i].c_struct_state != SLAP_C_USED ) {
348 /* give persistent clients a chance to cleanup */
349 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
350 ldap_pvt_thread_pool_submit( &connection_pool,
351 connections[i].c_clientfunc, connections[i].c_clientarg );
355 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
357 /* connections_mutex and c_mutex are locked */
358 connection_closing( &connections[i], "slapd shutdown" );
359 connection_close( &connections[i] );
361 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
364 ldap_pvt_thread_mutex_unlock( &connections_mutex );
371 * Timeout idle connections.
373 int connections_timeout_idle(time_t now)
379 for( c = connection_first( &connindex );
381 c = connection_next( c, &connindex ) )
383 /* Don't timeout a slow-running request or a persistent
384 * outbound connection */
385 if( c->c_n_ops_executing ||
386 c->c_conn_state == SLAP_C_CLIENT ) continue;
388 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
390 connection_closing( c, "idletimeout" );
391 connection_close( c );
395 connection_done( c );
400 static Connection* connection_get( ber_socket_t s )
402 /* connections_mutex should be locked by caller */
406 Debug( LDAP_DEBUG_ARGS,
407 "connection_get(%ld)\n",
410 assert( connections != NULL );
412 if(s == AC_SOCKET_INVALID) {
417 #ifdef SLAP_MULTI_CONN_ARRAY
418 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 #ifdef SLAP_MULTI_CONN_ARRAY
538 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
540 ldap_pvt_thread_mutex_lock( &connections_mutex );
544 #ifdef SLAP_MULTI_CONN_ARRAY
545 c = MCA_GET_CONNECTION(s);
555 for( i=0; i < dtblsize; i++) {
558 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
559 assert( connections[i].c_sb == 0 );
564 sd = AC_SOCKET_INVALID;
565 if (connections[i].c_sb != NULL) {
566 ber_sockbuf_ctrl( connections[i].c_sb,
567 LBER_SB_OPT_GET_FD, &sd );
570 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
571 assert( sd == AC_SOCKET_INVALID );
576 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
580 assert( connections[i].c_struct_state == SLAP_C_USED );
581 assert( connections[i].c_conn_state != SLAP_C_INVALID );
582 assert( sd != AC_SOCKET_INVALID );
586 Debug( LDAP_DEBUG_ANY,
587 "connection_init(%d): connection table full "
588 "(%d/%d)\n", s, i, dtblsize);
589 ldap_pvt_thread_mutex_unlock( &connections_mutex );
597 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
598 c->c_send_ldap_result = slap_send_ldap_result;
599 c->c_send_search_entry = slap_send_search_entry;
600 c->c_send_search_reference = slap_send_search_reference;
601 c->c_send_ldap_extended = slap_send_ldap_extended;
602 #ifdef LDAP_RES_INTERMEDIATE
603 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
606 BER_BVZERO( &c->c_authmech );
607 BER_BVZERO( &c->c_dn );
608 BER_BVZERO( &c->c_ndn );
610 c->c_listener = NULL;
611 BER_BVZERO( &c->c_peer_domain );
612 BER_BVZERO( &c->c_peer_name );
614 LDAP_STAILQ_INIT(&c->c_ops);
615 LDAP_STAILQ_INIT(&c->c_pending_ops);
617 BER_BVZERO( &c->c_sasl_bind_mech );
619 c->c_sasl_authctx = NULL;
620 c->c_sasl_sockctx = NULL;
621 c->c_sasl_extra = NULL;
622 c->c_sasl_bindop = NULL;
624 c->c_sb = ber_sockbuf_alloc( );
627 ber_len_t max = sockbuf_max_incoming;
628 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
631 c->c_currentber = NULL;
633 /* should check status of thread calls */
634 ldap_pvt_thread_mutex_init( &c->c_mutex );
635 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
636 ldap_pvt_thread_cond_init( &c->c_write_cv );
639 if ( slapi_plugins_used ) {
640 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
644 c->c_struct_state = SLAP_C_UNUSED;
647 ldap_pvt_thread_mutex_lock( &c->c_mutex );
649 assert( c->c_struct_state == SLAP_C_UNUSED );
650 assert( BER_BVISNULL( &c->c_authmech ) );
651 assert( BER_BVISNULL( &c->c_dn ) );
652 assert( BER_BVISNULL( &c->c_ndn ) );
653 assert( c->c_listener == NULL );
654 assert( BER_BVISNULL( &c->c_peer_domain ) );
655 assert( BER_BVISNULL( &c->c_peer_name ) );
656 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
657 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
658 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
659 assert( c->c_sasl_done == 0 );
660 assert( c->c_sasl_authctx == NULL );
661 assert( c->c_sasl_sockctx == NULL );
662 assert( c->c_sasl_extra == NULL );
663 assert( c->c_sasl_bindop == NULL );
664 assert( c->c_currentber == NULL );
665 assert( c->c_writewaiter == 0);
667 c->c_listener = listener;
669 if ( flags == CONN_IS_CLIENT ) {
670 c->c_conn_state = SLAP_C_CLIENT;
671 c->c_struct_state = SLAP_C_USED;
672 c->c_close_reason = "?"; /* should never be needed */
673 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
674 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
675 #ifdef SLAP_MULTI_CONN_ARRAY
676 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
678 ldap_pvt_thread_mutex_unlock( &connections_mutex );
684 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
685 ber_str2bv( peername, 0, 1, &c->c_peer_name );
687 c->c_n_ops_received = 0;
688 c->c_n_ops_executing = 0;
689 c->c_n_ops_pending = 0;
690 c->c_n_ops_completed = 0;
696 /* set to zero until bind, implies LDAP_VERSION3 */
699 #ifndef SLAPD_MONITOR
700 if ( global_idletimeout > 0 )
701 #endif /* ! SLAPD_MONITOR */
703 c->c_activitytime = c->c_starttime = slap_get_time();
706 #ifdef LDAP_CONNECTIONLESS
708 if( flags == CONN_IS_UDP ) {
711 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
712 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
714 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
715 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
716 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
717 LBER_SBIOD_LEVEL_PROVIDER, NULL );
722 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
723 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
725 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
726 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
730 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
731 INT_MAX, (void*)"ldap_" );
734 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
735 c /* non-NULL */ ) < 0 )
737 Debug( LDAP_DEBUG_ANY,
738 "connection_init(%d, %s): set nonblocking failed\n",
739 s, c->c_peer_name.bv_val, 0 );
742 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
743 id = c->c_connid = conn_nextid++;
744 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
746 c->c_conn_state = SLAP_C_INACTIVE;
747 c->c_struct_state = SLAP_C_USED;
748 c->c_close_reason = "?"; /* should never be needed */
750 c->c_ssf = c->c_transport_ssf = ssf;
754 if ( flags == CONN_IS_TLS ) {
756 c->c_needs_tls_accept = 1;
759 c->c_needs_tls_accept = 0;
763 slap_sasl_open( c, 0 );
764 slap_sasl_external( c, ssf, authid );
766 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
767 #ifdef SLAP_MULTI_CONN_ARRAY
768 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
770 ldap_pvt_thread_mutex_unlock( &connections_mutex );
773 backend_connection_init(c);
778 void connection2anonymous( Connection *c )
780 assert( connections != NULL );
784 ber_len_t max = sockbuf_max_incoming;
785 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
788 if ( !BER_BVISNULL( &c->c_authmech ) ) {
789 ch_free(c->c_authmech.bv_val);
791 BER_BVZERO( &c->c_authmech );
793 if ( !BER_BVISNULL( &c->c_dn ) ) {
794 ch_free(c->c_dn.bv_val);
796 BER_BVZERO( &c->c_dn );
798 if ( !BER_BVISNULL( &c->c_ndn ) ) {
799 ch_free(c->c_ndn.bv_val);
801 BER_BVZERO( &c->c_ndn );
803 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
804 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
806 BER_BVZERO( &c->c_sasl_authz_dn );
808 c->c_authz_backend = NULL;
812 connection_destroy( Connection *c )
814 /* note: connections_mutex should be locked by caller */
816 unsigned long connid;
817 const char *close_reason;
819 assert( connections != NULL );
821 assert( c->c_struct_state != SLAP_C_UNUSED );
822 assert( c->c_conn_state != SLAP_C_INVALID );
823 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
824 assert( c->c_writewaiter == 0);
826 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
827 connid = c->c_connid;
828 close_reason = c->c_close_reason;
830 backend_connection_destroy(c);
835 c->c_activitytime = c->c_starttime = 0;
837 connection2anonymous( c );
838 c->c_listener = NULL;
840 if(c->c_peer_domain.bv_val != NULL) {
841 free(c->c_peer_domain.bv_val);
843 BER_BVZERO( &c->c_peer_domain );
844 if(c->c_peer_name.bv_val != NULL) {
845 free(c->c_peer_name.bv_val);
847 BER_BVZERO( &c->c_peer_name );
849 c->c_sasl_bind_in_progress = 0;
850 if(c->c_sasl_bind_mech.bv_val != NULL) {
851 free(c->c_sasl_bind_mech.bv_val);
853 BER_BVZERO( &c->c_sasl_bind_mech );
855 slap_sasl_close( c );
857 if ( c->c_currentber != NULL ) {
858 ber_free( c->c_currentber, 1 );
859 c->c_currentber = NULL;
862 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
863 if ( sd != AC_SOCKET_INVALID ) {
864 slapd_remove( sd, 1, 0 );
866 Statslog( LDAP_DEBUG_STATS, (close_reason
867 ? "conn=%lu fd=%ld closed (%s)\n"
868 : "conn=%lu fd=%ld closed\n"),
869 connid, (long) sd, close_reason, 0, 0 );
872 ber_sockbuf_free( c->c_sb );
874 c->c_sb = ber_sockbuf_alloc( );
877 ber_len_t max = sockbuf_max_incoming;
878 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
881 c->c_conn_state = SLAP_C_INVALID;
882 c->c_struct_state = SLAP_C_UNUSED;
883 c->c_close_reason = "?"; /* should never be needed */
886 /* call destructors, then constructors; avoids unnecessary allocation */
887 if ( slapi_plugins_used ) {
888 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
893 int connection_state_closing( Connection *c )
895 /* c_mutex must be locked by caller */
899 assert( c->c_struct_state == SLAP_C_USED );
901 state = c->c_conn_state;
903 assert( state != SLAP_C_INVALID );
905 return state == SLAP_C_CLOSING;
908 static void connection_abandon( Connection *c )
910 /* c_mutex must be locked by caller */
912 Operation *o, *next, op = {0};
918 op.o_connid = c->c_connid;
919 op.o_tag = LDAP_REQ_ABANDON;
920 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
921 next = LDAP_STAILQ_NEXT( o, o_next );
922 op.orn_msgid = o->o_msgid;
924 op.o_bd = frontendDB;
925 frontendDB->be_abandon( &op, &rs );
928 /* remove pending operations */
929 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
930 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
931 LDAP_STAILQ_NEXT(o, o_next) = NULL;
936 void connection_closing( Connection *c, const char *why )
938 assert( connections != NULL );
940 assert( c->c_struct_state == SLAP_C_USED );
941 assert( c->c_conn_state != SLAP_C_INVALID );
943 /* c_mutex must be locked by caller */
945 if( c->c_conn_state != SLAP_C_CLOSING ) {
948 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
949 Debug( LDAP_DEBUG_TRACE,
950 "connection_closing: readying conn=%lu sd=%d for close\n",
951 c->c_connid, sd, 0 );
952 /* update state to closing */
953 c->c_conn_state = SLAP_C_CLOSING;
954 c->c_close_reason = why;
956 /* don't listen on this port anymore */
957 slapd_clr_read( sd, 1 );
959 /* abandon active operations */
960 connection_abandon( c );
962 /* wake write blocked operations */
963 slapd_clr_write( sd, 1 );
964 if ( c->c_writewaiter ) {
965 ldap_pvt_thread_cond_signal( &c->c_write_cv );
966 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
967 ldap_pvt_thread_yield();
968 ldap_pvt_thread_mutex_lock( &c->c_mutex );
970 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
971 /* Client closed connection after doing Unbind. */
972 c->c_close_reason = NULL;
976 static void connection_close( Connection *c )
980 assert( connections != NULL );
982 assert( c->c_struct_state == SLAP_C_USED );
983 assert( c->c_conn_state == SLAP_C_CLOSING );
985 /* note: connections_mutex and c_mutex should be locked by caller */
987 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
988 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
989 Debug( LDAP_DEBUG_TRACE,
990 "connection_close: deferring conn=%lu sd=%d\n",
991 c->c_connid, sd, 0 );
995 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
996 c->c_connid, sd, 0 );
997 connection_destroy( c );
1000 unsigned long connections_nextid(void)
1003 assert( connections != NULL );
1005 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1009 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1014 Connection* connection_first( ber_socket_t *index )
1016 #ifdef SLAP_MULTI_CONN_ARRAY
1020 assert( connections != NULL );
1021 assert( index != NULL );
1023 #ifdef SLAP_MULTI_CONN_ARRAY
1024 for ( conn_array_id = 0;
1025 conn_array_id < NUM_CONNECTION_ARRAY;
1028 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1031 ldap_pvt_thread_mutex_lock( &connections_mutex );
1036 return connection_next(NULL, index);
1039 Connection* connection_next( Connection *c, ber_socket_t *index )
1040 #ifdef SLAP_MULTI_CONN_ARRAY
1044 assert( connections != NULL );
1045 assert( index != NULL );
1046 assert( *index <= (dtblsize/NUM_CONNECTION_ARRAY) );
1049 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1054 for(; *index < dtblsize; (*index)++) {
1055 conn = MCA_GET_CONNECTION(*index);
1056 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1057 assert( conn->c_conn_state == SLAP_C_INVALID );
1058 #ifndef HAVE_WINSOCK
1065 if( conn->c_struct_state == SLAP_C_USED ) {
1066 assert( conn->c_conn_state != SLAP_C_INVALID );
1072 assert( conn->c_struct_state == SLAP_C_UNUSED );
1073 assert( conn->c_conn_state == SLAP_C_INVALID );
1077 ldap_pvt_thread_mutex_lock( &c->c_mutex );
1085 assert( connections != NULL );
1086 assert( index != NULL );
1087 assert( *index <= dtblsize );
1090 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1095 for(; *index < dtblsize; (*index)++) {
1096 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1097 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1098 #ifndef HAVE_WINSOCK
1105 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1106 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1107 c = &connections[(*index)++];
1111 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1112 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1116 ldap_pvt_thread_mutex_lock( &c->c_mutex );
1123 void connection_done( Connection *c )
1125 #ifdef SLAP_MULTI_CONN_ARRAY
1129 assert( connections != NULL );
1132 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1135 #ifdef SLAP_MULTI_CONN_ARRAY
1136 for ( conn_array_id = 0;
1137 conn_array_id < NUM_CONNECTION_ARRAY;
1140 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1143 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1148 * connection_activity - handle the request operation op on connection
1149 * conn. This routine figures out what kind of operation it is and
1150 * calls the appropriate stub to handle it.
1153 #ifdef SLAPD_MONITOR
1154 /* FIXME: returns 0 in case of failure */
1155 #define INCR_OP_INITIATED(index) \
1157 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1158 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1159 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1161 #define INCR_OP_COMPLETED(index) \
1163 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1164 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1165 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1166 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1168 #else /* !SLAPD_MONITOR */
1169 #define INCR_OP_INITIATED(index) do { } while (0)
1170 #define INCR_OP_COMPLETED(index) \
1172 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1173 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1174 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1176 #endif /* !SLAPD_MONITOR */
1179 * NOTE: keep in sync with enum in slapd.h
1181 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1196 connection_operation( void *ctx, void *arg_v )
1198 int rc = LDAP_OTHER;
1199 Operation *op = arg_v;
1200 SlapReply rs = {REP_RESULT};
1201 ber_tag_t tag = op->o_tag;
1203 Connection *conn = op->o_conn;
1204 void *memctx = NULL;
1205 void *memctx_null = NULL;
1208 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1209 /* FIXME: returns 0 in case of failure */
1210 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1211 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1213 op->o_threadctx = ctx;
1217 case LDAP_REQ_UNBIND:
1219 case LDAP_REQ_DELETE:
1220 case LDAP_REQ_MODDN:
1221 case LDAP_REQ_MODIFY:
1222 case LDAP_REQ_COMPARE:
1223 case LDAP_REQ_SEARCH:
1224 case LDAP_REQ_ABANDON:
1225 case LDAP_REQ_EXTENDED:
1228 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1229 "conn %lu unknown LDAP request 0x%lx\n",
1230 conn->c_connid, tag, 0 );
1231 op->o_tag = LBER_ERROR;
1232 rs.sr_err = LDAP_PROTOCOL_ERROR;
1233 rs.sr_text = "unknown LDAP request";
1234 send_ldap_disconnect( op, &rs );
1235 rc = SLAPD_DISCONNECT;
1236 goto operations_error;
1239 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1240 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1241 "error: SASL bind in progress (tag=%ld).\n",
1243 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1244 "SASL bind in progress" );
1245 rc = LDAP_OPERATIONS_ERROR;
1246 goto operations_error;
1249 /* We can use Thread-Local storage for most mallocs. We can
1250 * also use TL for ber parsing, but not on Add or Modify.
1253 memsiz = ber_len( op->o_ber ) * 64;
1254 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1256 memsiz = SLAP_SLAB_SIZE;
1258 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1259 op->o_tmpmemctx = memctx;
1260 op->o_tmpmfuncs = &slap_sl_mfuncs;
1261 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1262 /* Note - the ber and its buffer are already allocated from
1263 * regular memory; this only affects subsequent mallocs that
1264 * ber_scanf may invoke.
1266 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1271 opidx = SLAP_OP_BIND;
1274 case LDAP_REQ_UNBIND:
1275 opidx = SLAP_OP_UNBIND;
1279 opidx = SLAP_OP_ADD;
1282 case LDAP_REQ_DELETE:
1283 opidx = SLAP_OP_DELETE;
1286 case LDAP_REQ_MODRDN:
1287 opidx = SLAP_OP_MODRDN;
1290 case LDAP_REQ_MODIFY:
1291 opidx = SLAP_OP_MODIFY;
1294 case LDAP_REQ_COMPARE:
1295 opidx = SLAP_OP_COMPARE;
1298 case LDAP_REQ_SEARCH:
1299 opidx = SLAP_OP_SEARCH;
1302 case LDAP_REQ_ABANDON:
1303 opidx = SLAP_OP_ABANDON;
1306 case LDAP_REQ_EXTENDED:
1307 opidx = SLAP_OP_EXTENDED;
1315 assert( opidx > -1 );
1316 INCR_OP_INITIATED( opidx );
1317 rc = (*(opfun[opidx]))( op, &rs );
1320 if ( rc == SLAPD_DISCONNECT ) {
1323 } else if ( opidx > -1 ) {
1324 /* increment completed operations count
1325 * only if operation was initiated
1326 * and rc != SLAPD_DISCONNECT */
1327 INCR_OP_COMPLETED( opidx );
1330 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1331 if ( rc == SLAPD_ABANDON )
1332 op->o_cancel = SLAP_CANCEL_ACK;
1334 op->o_cancel = LDAP_TOO_LATE;
1336 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1337 op->o_cancel != SLAP_CANCEL_DONE )
1339 ldap_pvt_thread_yield();
1342 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1344 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1346 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1347 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1349 conn->c_n_ops_executing--;
1350 conn->c_n_ops_completed++;
1354 case LDAP_REQ_UNBIND:
1355 /* c_mutex is locked */
1357 conn, tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1361 conn->c_sasl_bind_in_progress =
1362 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1364 if( conn->c_conn_state == SLAP_C_BINDING) {
1365 conn->c_conn_state = SLAP_C_ACTIVE;
1369 connection_resched( conn );
1370 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1374 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1376 int connection_client_setup(
1378 ldap_pvt_thread_start_t *func,
1384 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1385 CONN_IS_CLIENT, 0, NULL );
1386 if ( rc < 0 ) return -1;
1388 c = connection_get( s );
1389 c->c_clientfunc = func;
1390 c->c_clientarg = arg;
1391 connection_return( c );
1392 slapd_add_internal( s, 0 );
1393 slapd_set_read( s, 1 );
1397 void connection_client_enable(
1400 slapd_set_read( s, 1 );
1403 void connection_client_stop(
1408 /* get (locked) connection */
1409 c = connection_get( s );
1411 assert( c->c_conn_state == SLAP_C_CLIENT );
1413 c->c_listener = NULL;
1414 c->c_conn_state = SLAP_C_INVALID;
1415 c->c_struct_state = SLAP_C_UNUSED;
1416 c->c_close_reason = "?"; /* should never be needed */
1417 ber_sockbuf_free( c->c_sb );
1418 c->c_sb = ber_sockbuf_alloc( );
1420 ber_len_t max = sockbuf_max_incoming;
1421 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1424 connection_return( c );
1425 slapd_remove( s, 0, 1 );
1428 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1429 void* connection_processing_thread( void* ctx, void* argv )
1432 Operation* new_op = NULL;
1433 ber_socket_t s = (ber_socket_t)argv;
1436 * read incoming LDAP requests. If there is more than one,
1437 * the first one is returned with new_op
1439 if( ( rc = connection_read( s, &new_op ) ) < 0 ) {
1440 Debug( LDAP_DEBUG_TRACE, "connection_read(%d) error\n", s, 0, 0 );
1445 /* execute the queued request in the same thread */
1447 rc = (int)connection_operation(
1448 ldap_pvt_thread_pool_context(), new_op );
1454 int connection_processing_activate( ber_socket_t s )
1459 * suspend reading on this file descriptor until a connection processing
1460 * thread reads data on it. Otherwise the listener thread will repeatedly
1461 * submit the same event on it to the pool.
1463 if( !slapd_suspend( s ) ) return 0;
1465 status = ldap_pvt_thread_pool_submit( &connection_pool,
1466 connection_processing_thread, (void *) s );
1469 Debug( LDAP_DEBUG_ANY, "connection_processing_activiate(%d): "
1470 "ldap_pvt_thread_pool_submit failed\n",
1479 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1480 int connection_read( ber_socket_t s, Operation** op )
1482 int connection_read(ber_socket_t s)
1487 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1488 int need_resume = 1;
1491 assert( connections != NULL );
1493 #ifdef SLAP_MULTI_CONN_ARRAY
1494 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1496 ldap_pvt_thread_mutex_lock( &connections_mutex );
1499 /* get (locked) connection */
1500 c = connection_get( s );
1503 Debug( LDAP_DEBUG_ANY,
1504 "connection_read(%ld): no connection!\n",
1506 slapd_remove(s, 1, 0);
1508 #ifdef SLAP_MULTI_CONN_ARRAY
1509 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1511 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1518 if( c->c_conn_state == SLAP_C_CLOSING ) {
1519 Debug( LDAP_DEBUG_TRACE,
1520 "connection_read(%d): closing, ignoring input for id=%lu\n",
1521 s, c->c_connid, 0 );
1522 connection_return( c );
1523 #ifdef SLAP_MULTI_CONN_ARRAY
1524 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1526 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1529 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1535 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1536 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1539 slapd_clr_read( s, 0 );
1540 ldap_pvt_thread_pool_submit( &connection_pool,
1541 c->c_clientfunc, c->c_clientarg );
1543 connection_return( c );
1544 #ifdef SLAP_MULTI_CONN_ARRAY
1545 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1547 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1552 Debug( LDAP_DEBUG_TRACE,
1553 "connection_read(%d): checking for input on id=%lu\n",
1554 s, c->c_connid, 0 );
1557 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1558 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1560 Debug( LDAP_DEBUG_TRACE,
1561 "connection_read(%d): TLS accept error "
1562 "error=%d id=%lu, closing\n",
1563 s, rc, c->c_connid );
1565 c->c_needs_tls_accept = 0;
1566 /* connections_mutex and c_mutex are locked */
1567 connection_closing( c, "TLS negotiation failure" );
1573 /* Drain input before close, to allow SSL error codes
1574 * to propagate to client. */
1580 rc = select(s+1, &rfd, NULL, NULL, &tv);
1582 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1588 connection_close( c );
1590 } else if ( rc == 0 ) {
1592 struct berval authid = BER_BVNULL;
1594 c->c_needs_tls_accept = 0;
1596 /* we need to let SASL know */
1597 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1599 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1600 if( c->c_tls_ssf > c->c_ssf ) {
1601 c->c_ssf = c->c_tls_ssf;
1604 rc = dnX509peerNormalize( ssl, &authid );
1605 if ( rc != LDAP_SUCCESS ) {
1606 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1607 "unable to get TLS client DN, error=%d id=%lu\n",
1608 s, rc, c->c_connid );
1610 Statslog( LDAP_DEBUG_STATS,
1611 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1612 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1613 slap_sasl_external( c, c->c_tls_ssf, &authid );
1614 if ( authid.bv_val ) free( authid.bv_val );
1617 /* if success and data is ready, fall thru to data input loop */
1619 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1621 connection_return( c );
1622 #ifdef SLAP_MULTI_CONN_ARRAY
1623 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1625 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1628 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1636 #ifdef HAVE_CYRUS_SASL
1637 if ( c->c_sasl_layers ) {
1638 /* If previous layer is not removed yet, give up for now */
1639 if ( !c->c_sasl_sockctx ) {
1640 connection_return( c );
1641 #ifdef SLAP_MULTI_CONN_ARRAY
1642 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1644 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1647 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1653 c->c_sasl_layers = 0;
1655 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1656 if( rc != LDAP_SUCCESS ) {
1657 Debug( LDAP_DEBUG_TRACE,
1658 "connection_read(%d): SASL install error "
1659 "error=%d id=%lu, closing\n",
1660 s, rc, c->c_connid );
1661 /* connections_mutex and c_mutex are locked */
1662 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1665 connection_closing( c, "SASL layer install failure" );
1666 connection_close( c );
1667 connection_return( c );
1668 #ifdef SLAP_MULTI_CONN_ARRAY
1669 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1671 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1678 #define CONNECTION_INPUT_LOOP 1
1679 /* #define DATA_READY_LOOP 1 */
1682 /* How do we do this without getting into a busy loop ? */
1683 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1684 rc = connection_input( c, op );
1686 rc = connection_input( c );
1688 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1689 if( *op && (*op)->o_tag == LDAP_REQ_UNBIND ) {
1691 * After the reception of an unbind request,
1692 * no more incoming requests via the connection
1693 * is expected. Therefore, don't resume connection reading.
1699 #ifdef DATA_READY_LOOP
1700 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1701 #elif CONNECTION_INPUT_LOOP
1708 Debug( LDAP_DEBUG_TRACE,
1709 "connection_read(%d): input error=%d id=%lu, closing.\n",
1710 s, rc, c->c_connid );
1712 /* connections_mutex and c_mutex are locked */
1713 connection_closing( c, conn_lost_str );
1714 connection_close( c );
1715 connection_return( c );
1716 #ifdef SLAP_MULTI_CONN_ARRAY
1717 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1719 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1724 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1725 if ( need_resume ) slapd_resume( s );
1728 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1729 slapd_set_read( s, 1 );
1732 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1733 slapd_set_write( s, 1 );
1736 connection_return( c );
1737 #ifdef SLAP_MULTI_CONN_ARRAY
1738 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1740 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1746 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1748 connection_input( Connection *conn, Operation** c_op )
1751 connection_input( Connection *conn )
1760 #ifdef LDAP_CONNECTIONLESS
1766 if ( conn->c_currentber == NULL &&
1767 ( conn->c_currentber = ber_alloc()) == NULL )
1769 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1775 #ifdef LDAP_CONNECTIONLESS
1776 if ( conn->c_is_udp ) {
1777 char peername[sizeof("IP=255.255.255.255:65336")];
1779 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1780 sizeof(struct sockaddr));
1781 if (len != sizeof(struct sockaddr)) return 1;
1783 sprintf( peername, "IP=%s:%d",
1784 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1785 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1786 Statslog( LDAP_DEBUG_STATS,
1787 "conn=%lu UDP request from %s (%s) accepted.\n",
1788 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1792 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1793 if ( tag != LDAP_TAG_MESSAGE ) {
1797 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1799 Debug( LDAP_DEBUG_TRACE,
1800 "ber_get_next on fd %d failed errno=%d (%s)\n",
1801 sd, err, sock_errstr(err) );
1802 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1803 /* log, close and send error */
1804 ber_free( conn->c_currentber, 1 );
1805 conn->c_currentber = NULL;
1812 ber = conn->c_currentber;
1813 conn->c_currentber = NULL;
1815 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1816 /* log, close and send error */
1817 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1823 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1824 /* log, close and send error */
1825 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1832 #ifdef LDAP_CONNECTIONLESS
1833 if( conn->c_is_udp ) {
1834 if( tag == LBER_OCTETSTRING ) {
1835 ber_get_stringa( ber, &cdn );
1836 tag = ber_peek_tag(ber, &len);
1838 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1839 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1846 if(tag == LDAP_REQ_BIND) {
1847 /* immediately abandon all existing operations upon BIND */
1848 connection_abandon( conn );
1851 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1854 /* clear state if the connection is being reused from inactive */
1855 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1856 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1859 op->o_res_ber = NULL;
1861 #ifdef LDAP_CONNECTIONLESS
1862 if (conn->c_is_udp) {
1864 ber_str2bv( cdn, 0, 1, &op->o_dn );
1865 op->o_protocol = LDAP_VERSION2;
1867 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1868 if (op->o_res_ber == NULL) return 1;
1870 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1871 sizeof(struct sockaddr), 0 );
1873 if (rc != sizeof(struct sockaddr)) {
1874 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1878 if (op->o_protocol == LDAP_VERSION2) {
1879 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1881 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1886 #endif /* LDAP_CONNECTIONLESS */
1890 /* Don't process requests when the conn is in the middle of a
1891 * Bind, or if it's closing. Also, don't let any single conn
1892 * use up all the available threads, and don't execute if we're
1893 * currently blocked on output. And don't execute if there are
1894 * already pending ops, let them go first. Abandon operations
1895 * get exceptions to some, but not all, cases.
1899 /* Abandon and Unbind are exempt from these checks */
1900 if (conn->c_conn_state == SLAP_C_CLOSING) {
1903 } else if (conn->c_writewaiter) {
1904 defer = "awaiting write";
1906 } else if (conn->c_n_ops_pending) {
1907 defer = "pending operations";
1911 case LDAP_REQ_ABANDON:
1912 /* Unbind is exempt from these checks */
1913 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1914 defer = "too many executing";
1916 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1921 case LDAP_REQ_UNBIND:
1926 int max = conn->c_dn.bv_len
1927 ? slap_conn_max_pending_auth
1928 : slap_conn_max_pending;
1930 Debug( LDAP_DEBUG_ANY,
1931 "connection_input: conn=%lu deferring operation: %s\n",
1932 conn->c_connid, defer, 0 );
1933 conn->c_n_ops_pending++;
1934 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1935 if ( conn->c_n_ops_pending > max ) {
1941 conn->c_n_ops_executing++;
1942 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1944 * The first op will be processed in the same thread context,
1945 * Subsequent ops will be submitted to the pool by
1946 * calling connection_op_activate()
1948 if ( *c_op == NULL ) {
1949 /* the first incoming request */
1950 connection_op_queue( op );
1955 connection_op_activate( op );
1960 if ( conn->c_struct_state != SLAP_C_USED ) {
1961 /* connection must have got closed underneath us */
1965 assert( conn->c_struct_state == SLAP_C_USED );
1971 connection_resched( Connection *conn )
1975 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1978 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1980 /* use trylock to avoid possible deadlock */
1981 #ifdef SLAP_MULTI_CONN_ARRAY
1982 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1984 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1988 Debug( LDAP_DEBUG_TRACE,
1989 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1990 conn->c_connid, sd, 0 );
1992 * reaquire locks in the right order...
1993 * this may allow another thread to close this connection,
1994 * so recheck state below.
1996 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1997 #ifdef SLAP_MULTI_CONN_ARRAY
1998 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
2000 ldap_pvt_thread_mutex_lock( &connections_mutex );
2002 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
2005 if( conn->c_conn_state != SLAP_C_CLOSING ) {
2006 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
2007 "closed by other thread conn=%lu sd=%d\n",
2008 conn->c_connid, sd, 0 );
2010 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
2011 "attempting closing conn=%lu sd=%d\n",
2012 conn->c_connid, sd, 0 );
2013 connection_close( conn );
2016 #ifdef SLAP_MULTI_CONN_ARRAY
2017 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
2019 ldap_pvt_thread_mutex_unlock( &connections_mutex );
2024 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
2025 /* other states need different handling */
2029 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
2030 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
2033 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
2034 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2035 /* pending operations should not be marked for abandonment */
2036 assert(!op->o_abandon);
2038 conn->c_n_ops_pending--;
2039 conn->c_n_ops_executing++;
2041 connection_op_activate( op );
2043 if ( conn->c_conn_state == SLAP_C_BINDING ) {
2051 connection_init_log_prefix( Operation *op )
2053 if ( op->o_connid == (unsigned long)(-1) ) {
2054 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2055 "conn=-1 op=%lu", op->o_opid );
2058 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2059 "conn=%lu op=%lu", op->o_connid, op->o_opid );
2063 static void connection_op_queue( Operation *op )
2066 ber_tag_t tag = op->o_tag;
2068 if(tag == LDAP_REQ_BIND) {
2069 op->o_conn->c_conn_state = SLAP_C_BINDING;
2072 if (!op->o_dn.bv_len) {
2073 op->o_authz = op->o_conn->c_authz;
2074 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2075 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2076 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2078 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2079 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2083 op->o_authtype = op->o_conn->c_authtype;
2084 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2086 if (!op->o_protocol) {
2087 op->o_protocol = op->o_conn->c_protocol
2088 ? op->o_conn->c_protocol : LDAP_VERSION3;
2091 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
2092 && op->o_protocol > LDAP_VERSION2)
2094 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2097 op->o_connid = op->o_conn->c_connid;
2098 connection_init_log_prefix( op );
2100 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2103 static int connection_op_activate( Operation *op )
2106 ber_tag_t tag = op->o_tag;
2108 connection_op_queue( op );
2110 status = ldap_pvt_thread_pool_submit( &connection_pool,
2111 connection_operation, (void *) op );
2113 if ( status != 0 ) {
2114 Debug( LDAP_DEBUG_ANY,
2115 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
2116 status, op->o_connid, 0 );
2117 /* should move op to pending list */
2123 int connection_write(ber_socket_t s)
2128 assert( connections != NULL );
2130 #ifdef SLAP_MULTI_CONN_ARRAY
2131 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2133 ldap_pvt_thread_mutex_lock( &connections_mutex );
2136 c = connection_get( s );
2138 Debug( LDAP_DEBUG_ANY,
2139 "connection_write(%ld): no connection!\n",
2141 slapd_remove(s, 1, 0);
2142 #ifdef SLAP_MULTI_CONN_ARRAY
2143 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2145 ldap_pvt_thread_mutex_unlock( &connections_mutex );
2150 slapd_clr_write( s, 0);
2153 Debug( LDAP_DEBUG_TRACE,
2154 "connection_write(%d): waking output for id=%lu\n",
2155 s, c->c_connid, 0 );
2156 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2158 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2159 slapd_set_read( s, 1 );
2161 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2162 slapd_set_write( s, 1 );
2165 /* If there are ops pending because of a writewaiter,
2168 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2169 if ( !c->c_writewaiter ) break;
2170 if ( c->c_n_ops_executing > connection_pool_max/2 ) {
2173 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2174 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2175 /* pending operations should not be marked for abandonment */
2176 assert(!op->o_abandon);
2178 c->c_n_ops_pending--;
2179 c->c_n_ops_executing++;
2181 connection_op_activate( op );
2185 connection_return( c );
2187 #ifdef SLAP_MULTI_CONN_ARRAY
2188 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2190 ldap_pvt_thread_mutex_unlock( &connections_mutex );
2197 connection_fake_init(
2202 conn->c_connid = -1;
2203 conn->c_send_ldap_result = slap_send_ldap_result;
2204 conn->c_send_search_entry = slap_send_search_entry;
2205 conn->c_send_search_reference = slap_send_search_reference;
2206 conn->c_listener = (Listener *)&dummy_list;
2207 conn->c_peer_domain = slap_empty_bv;
2208 conn->c_peer_name = slap_empty_bv;
2210 memset(op, 0, OPERATION_BUFFER_SIZE);
2211 op->o_hdr = (Opheader *)(op+1);
2212 op->o_controls = (void **)(op->o_hdr+1);
2213 /* set memory context */
2214 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2215 op->o_tmpmfuncs = &slap_sl_mfuncs;
2216 op->o_threadctx = ctx;
2219 op->o_connid = op->o_conn->c_connid;
2220 connection_init_log_prefix( op );
2222 op->o_time = slap_get_time();
2226 connection_assign_nextid( Connection *conn )
2228 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2229 conn->c_connid = conn_nextid++;
2230 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );