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"
46 #ifdef SLAP_MULTI_CONN_ARRAY
47 /* for Multiple Connection Arrary (MCA) Support */
48 static ldap_pvt_thread_mutex_t* connections_mutex;
49 static Connection **connections = NULL;
51 /* set to the number of processors (round up to a power of 2) */
52 # define NUM_CONNECTION_ARRAY 4
54 /* partition the array in a modulo manner */
55 # define MCA_conn_array_id(fd) ((int)(fd)%NUM_CONNECTION_ARRAY)
56 # define MCA_conn_array_element_id(fd) ((int)(fd)/NUM_CONNECTION_ARRAY)
57 # define MCA_ARRAY_SIZE ((int)(MCA_conn_array_element_id(dtblsize) + (MCA_conn_array_id(dtblsize) ? 1 : 0)))
58 # define MCA_conn_check(fd) (dtblsize > 0 && (fd) >= 0 && (fd) < (MCA_ARRAY_SIZE*NUM_CONNECTION_ARRAY))
59 # define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \
60 [MCA_conn_array_element_id(fd)])
61 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
64 /* protected by connections_mutex */
65 static ldap_pvt_thread_mutex_t connections_mutex;
66 static Connection *connections = NULL;
68 # define MCA_conn_check(fd) (dtblsize > 0 && (fd) < dtblsize)
69 # define MCA_GET_CONNECTION(fd) (&connections[s])
70 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
73 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
74 static unsigned long conn_nextid = 0;
76 static const char conn_lost_str[] = "connection lost";
78 /* structure state (protected by connections_mutex) */
79 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
80 #define SLAP_C_UNUSED 0x01
81 #define SLAP_C_USED 0x02
83 /* connection state (protected by c_mutex ) */
84 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
85 #define SLAP_C_INACTIVE 0x01 /* zero threads */
86 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
87 #define SLAP_C_BINDING 0x03 /* binding */
88 #define SLAP_C_CLOSING 0x04 /* closing */
89 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
92 connection_state2str( int state )
95 case SLAP_C_INVALID: return "!";
96 case SLAP_C_INACTIVE: return "|";
97 case SLAP_C_ACTIVE: return "";
98 case SLAP_C_BINDING: return "B";
99 case SLAP_C_CLOSING: return "C";
100 case SLAP_C_CLIENT: return "L";
106 static Connection* connection_get( ber_socket_t s );
108 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
109 static int connection_input( Connection *c, Operation** op );
111 static int connection_input( Connection *c );
113 static void connection_close( Connection *c );
115 static int connection_op_activate( Operation *op );
116 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
117 static void connection_op_queue( Operation *op );
119 static int connection_resched( Connection *conn );
120 static void connection_abandon( Connection *conn );
121 static void connection_destroy( Connection *c );
123 static ldap_pvt_thread_start_t connection_operation;
126 * Initialize connection management infrastructure.
128 int connections_init(void)
129 #ifdef SLAP_MULTI_CONN_ARRAY
134 assert( connections == NULL );
136 if( connections != NULL) {
137 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
142 connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc(
143 NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
144 if( connections_mutex == NULL ) {
145 Debug( LDAP_DEBUG_ANY, "connections_init: "
146 "allocation of connection mutex[%d] failed\n", i, 0, 0 );
150 connections = (Connection**) ch_calloc(
151 NUM_CONNECTION_ARRAY, sizeof(Connection*));
152 if( connections == NULL ) {
153 Debug( LDAP_DEBUG_ANY, "connections_init: "
154 "allocation of connection[%d] failed\n", 0, 0, 0 );
158 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
159 ldap_pvt_thread_mutex_init( connections_mutex+i );
160 connections[i] = (Connection*) ch_calloc(
161 MCA_ARRAY_SIZE, sizeof(Connection) );
162 if( connections[i] == NULL ) {
163 Debug( LDAP_DEBUG_ANY, "connections_init: "
164 "allocation (%d*%ld) of connection array[%d] failed\n",
165 dtblsize, (long) sizeof(Connection), i );
170 /* should check return of every call */
171 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
173 assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
174 assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state ==
175 SLAP_C_UNINITIALIZED );
177 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
178 conn = connections[i];
179 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
180 conn[j].c_conn_idx = j;
185 * per entry initialization of the Connection array initialization
186 * will be done by connection_init()
195 assert( connections == NULL );
197 if( connections != NULL) {
198 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
203 /* should check return of every call */
204 ldap_pvt_thread_mutex_init( &connections_mutex );
205 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
207 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
209 if( connections == NULL ) {
210 Debug( LDAP_DEBUG_ANY, "connections_init: "
211 "allocation (%d*%ld) of connection array failed\n",
212 dtblsize, (long) sizeof(Connection), 0 );
216 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
217 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
219 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
222 * per entry initialization of the Connection array initialization
223 * will be done by connection_init()
231 * Destroy connection management infrastructure.
234 int connections_destroy(void)
235 #ifdef SLAP_MULTI_CONN_ARRAY
240 if( connections == NULL) {
241 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
246 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
247 Connection* conn = connections[i];
248 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
249 if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
250 ber_sockbuf_free( conn[j].c_sb );
251 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
252 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
253 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
256 if ( slapi_plugins_used ) {
257 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
265 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
266 free( connections[i] );
267 connections[i] = NULL;
268 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
272 free( connections_mutex );
274 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
283 /* should check return of every call */
285 if( connections == NULL) {
286 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
291 for ( i = 0; i < dtblsize; i++ ) {
292 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
293 ber_sockbuf_free( connections[i].c_sb );
294 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
295 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
296 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
298 if ( slapi_plugins_used ) {
299 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
309 ldap_pvt_thread_mutex_destroy( &connections_mutex );
310 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
316 * shutdown all connections
318 int connections_shutdown(void)
319 #ifdef SLAP_MULTI_CONN_ARRAY
324 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
325 Connection* conn = connections[i];
326 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
327 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
328 if( conn[j].c_struct_state != SLAP_C_USED ) {
331 /* give persistent clients a chance to cleanup */
332 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
333 ldap_pvt_thread_pool_submit( &connection_pool,
334 conn[j].c_clientfunc, conn[j].c_clientarg );
338 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
339 /* connections_mutex and c_mutex are locked */
340 connection_closing( &conn[j], "connection shutdown" );
341 connection_close( &conn[j] );
342 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
345 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
355 ldap_pvt_thread_mutex_lock( &connections_mutex );
357 for ( i = 0; i < dtblsize; i++ ) {
358 if( connections[i].c_struct_state != SLAP_C_USED ) {
361 /* give persistent clients a chance to cleanup */
362 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
363 ldap_pvt_thread_pool_submit( &connection_pool,
364 connections[i].c_clientfunc, connections[i].c_clientarg );
368 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
370 /* connections_mutex and c_mutex are locked */
371 connection_closing( &connections[i], "slapd shutdown" );
372 connection_close( &connections[i] );
374 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
377 ldap_pvt_thread_mutex_unlock( &connections_mutex );
384 * Timeout idle connections.
386 int connections_timeout_idle(time_t now)
392 for( c = connection_first( &connindex );
394 c = connection_next( c, &connindex ) )
396 /* Don't timeout a slow-running request or a persistent
397 * outbound connection */
398 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
402 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
404 connection_closing( c, "idletimeout" );
405 connection_close( c );
409 connection_done( c );
414 static Connection* connection_get( ber_socket_t s )
416 /* connections_mutex should be locked by caller */
420 Debug( LDAP_DEBUG_ARGS,
421 "connection_get(%ld)\n",
424 assert( connections != NULL );
426 if(s == AC_SOCKET_INVALID) return NULL;
429 assert( MCA_conn_check( s ) );
430 c = MCA_GET_CONNECTION(s);
432 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
439 for(i=0; i<dtblsize; i++) {
440 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
441 assert( connections[i].c_conn_state == SLAP_C_INVALID );
442 assert( connections[i].c_sb == 0 );
446 ber_sockbuf_ctrl( connections[i].c_sb,
447 LBER_SB_OPT_GET_FD, &sd );
449 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
450 assert( connections[i].c_conn_state == SLAP_C_INVALID );
451 assert( sd == AC_SOCKET_INVALID );
455 /* state can actually change from used -> unused by resched,
456 * so don't assert details here.
470 ldap_pvt_thread_mutex_lock( &c->c_mutex );
472 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
473 if( c->c_struct_state != SLAP_C_USED ) {
474 /* connection must have been closed due to resched */
476 assert( c->c_conn_state == SLAP_C_INVALID );
477 assert( sd == AC_SOCKET_INVALID );
479 Debug( LDAP_DEBUG_TRACE,
480 "connection_get(%d): connection not used\n",
483 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
487 Debug( LDAP_DEBUG_TRACE,
488 "connection_get(%d): got connid=%lu\n",
493 assert( c->c_struct_state == SLAP_C_USED );
494 assert( c->c_conn_state != SLAP_C_INVALID );
495 assert( sd != AC_SOCKET_INVALID );
497 #ifndef SLAPD_MONITOR
498 if ( global_idletimeout > 0 )
499 #endif /* ! SLAPD_MONITOR */
501 c->c_activitytime = slap_get_time();
508 static void connection_return( Connection *c )
510 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
513 long connection_init(
517 const char* peername,
520 struct berval *authid )
525 assert( connections != NULL );
527 assert( listener != NULL );
528 assert( dnsname != NULL );
529 assert( peername != NULL );
532 assert( flags != CONN_IS_TLS );
535 if( s == AC_SOCKET_INVALID ) {
536 Debug( LDAP_DEBUG_ANY,
537 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
543 assert( s < dtblsize );
546 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
549 assert( MCA_conn_check( s ) );
550 c = MCA_GET_CONNECTION(s);
556 for( i=0; i < dtblsize; i++) {
559 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
560 assert( connections[i].c_sb == 0 );
565 sd = AC_SOCKET_INVALID;
566 if (connections[i].c_sb != NULL) {
567 ber_sockbuf_ctrl( connections[i].c_sb,
568 LBER_SB_OPT_GET_FD, &sd );
571 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
572 assert( sd == AC_SOCKET_INVALID );
577 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
579 assert( connections[i].c_struct_state == SLAP_C_USED );
580 assert( connections[i].c_conn_state != SLAP_C_INVALID );
581 assert( sd != AC_SOCKET_INVALID );
585 Debug( LDAP_DEBUG_ANY,
586 "connection_init(%d): connection table full "
587 "(%d/%d)\n", s, i, dtblsize);
588 ldap_pvt_thread_mutex_unlock( &connections_mutex );
596 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
597 c->c_send_ldap_result = slap_send_ldap_result;
598 c->c_send_search_entry = slap_send_search_entry;
599 c->c_send_search_reference = slap_send_search_reference;
600 c->c_send_ldap_extended = slap_send_ldap_extended;
601 #ifdef LDAP_RES_INTERMEDIATE
602 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
605 BER_BVZERO( &c->c_authmech );
606 BER_BVZERO( &c->c_dn );
607 BER_BVZERO( &c->c_ndn );
609 c->c_listener = NULL;
610 BER_BVZERO( &c->c_peer_domain );
611 BER_BVZERO( &c->c_peer_name );
613 LDAP_STAILQ_INIT(&c->c_ops);
614 LDAP_STAILQ_INIT(&c->c_pending_ops);
616 BER_BVZERO( &c->c_sasl_bind_mech );
618 c->c_sasl_authctx = NULL;
619 c->c_sasl_sockctx = NULL;
620 c->c_sasl_extra = NULL;
621 c->c_sasl_bindop = NULL;
623 c->c_sb = ber_sockbuf_alloc( );
626 ber_len_t max = sockbuf_max_incoming;
627 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
630 c->c_currentber = NULL;
632 /* should check status of thread calls */
633 ldap_pvt_thread_mutex_init( &c->c_mutex );
634 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
635 ldap_pvt_thread_cond_init( &c->c_write_cv );
638 if ( slapi_plugins_used ) {
639 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
643 c->c_struct_state = SLAP_C_UNUSED;
646 ldap_pvt_thread_mutex_lock( &c->c_mutex );
648 assert( c->c_struct_state == SLAP_C_UNUSED );
649 assert( BER_BVISNULL( &c->c_authmech ) );
650 assert( BER_BVISNULL( &c->c_dn ) );
651 assert( BER_BVISNULL( &c->c_ndn ) );
652 assert( c->c_listener == NULL );
653 assert( BER_BVISNULL( &c->c_peer_domain ) );
654 assert( BER_BVISNULL( &c->c_peer_name ) );
655 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
656 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
657 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
658 assert( c->c_sasl_done == 0 );
659 assert( c->c_sasl_authctx == NULL );
660 assert( c->c_sasl_sockctx == NULL );
661 assert( c->c_sasl_extra == NULL );
662 assert( c->c_sasl_bindop == NULL );
663 assert( c->c_currentber == NULL );
664 assert( c->c_writewaiter == 0);
666 c->c_listener = listener;
668 if ( flags == CONN_IS_CLIENT ) {
669 c->c_conn_state = SLAP_C_CLIENT;
670 c->c_struct_state = SLAP_C_USED;
671 c->c_close_reason = "?"; /* should never be needed */
672 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
673 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
674 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
679 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
680 ber_str2bv( peername, 0, 1, &c->c_peer_name );
682 c->c_n_ops_received = 0;
683 c->c_n_ops_executing = 0;
684 c->c_n_ops_pending = 0;
685 c->c_n_ops_completed = 0;
691 /* set to zero until bind, implies LDAP_VERSION3 */
694 #ifndef SLAPD_MONITOR
695 if ( global_idletimeout > 0 )
696 #endif /* ! SLAPD_MONITOR */
698 c->c_activitytime = c->c_starttime = slap_get_time();
701 #ifdef LDAP_CONNECTIONLESS
703 if( flags == CONN_IS_UDP ) {
706 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
707 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
709 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
710 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
711 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
712 LBER_SBIOD_LEVEL_PROVIDER, NULL );
717 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
718 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
720 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
721 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
725 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
726 INT_MAX, (void*)"ldap_" );
729 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
730 c /* non-NULL */ ) < 0 )
732 Debug( LDAP_DEBUG_ANY,
733 "connection_init(%d, %s): set nonblocking failed\n",
734 s, c->c_peer_name.bv_val, 0 );
737 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
738 id = c->c_connid = conn_nextid++;
739 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
741 c->c_conn_state = SLAP_C_INACTIVE;
742 c->c_struct_state = SLAP_C_USED;
743 c->c_close_reason = "?"; /* should never be needed */
745 c->c_ssf = c->c_transport_ssf = ssf;
749 if ( flags == CONN_IS_TLS ) {
751 c->c_needs_tls_accept = 1;
754 c->c_needs_tls_accept = 0;
758 slap_sasl_open( c, 0 );
759 slap_sasl_external( c, ssf, authid );
761 slapd_add_internal( s, 1 );
762 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
763 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
765 backend_connection_init(c);
770 void connection2anonymous( Connection *c )
772 assert( connections != NULL );
776 ber_len_t max = sockbuf_max_incoming;
777 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
780 if ( !BER_BVISNULL( &c->c_authmech ) ) {
781 ch_free(c->c_authmech.bv_val);
783 BER_BVZERO( &c->c_authmech );
785 if ( !BER_BVISNULL( &c->c_dn ) ) {
786 ch_free(c->c_dn.bv_val);
788 BER_BVZERO( &c->c_dn );
790 if ( !BER_BVISNULL( &c->c_ndn ) ) {
791 ch_free(c->c_ndn.bv_val);
793 BER_BVZERO( &c->c_ndn );
795 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
796 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
798 BER_BVZERO( &c->c_sasl_authz_dn );
800 c->c_authz_backend = NULL;
804 connection_destroy( Connection *c )
806 /* note: connections_mutex should be locked by caller */
808 unsigned long connid;
809 const char *close_reason;
811 assert( connections != NULL );
813 assert( c->c_struct_state != SLAP_C_UNUSED );
814 assert( c->c_conn_state != SLAP_C_INVALID );
815 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
816 assert( c->c_writewaiter == 0);
818 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
819 connid = c->c_connid;
820 close_reason = c->c_close_reason;
822 backend_connection_destroy(c);
827 c->c_activitytime = c->c_starttime = 0;
829 connection2anonymous( c );
830 c->c_listener = NULL;
832 if(c->c_peer_domain.bv_val != NULL) {
833 free(c->c_peer_domain.bv_val);
835 BER_BVZERO( &c->c_peer_domain );
836 if(c->c_peer_name.bv_val != NULL) {
837 free(c->c_peer_name.bv_val);
839 BER_BVZERO( &c->c_peer_name );
841 c->c_sasl_bind_in_progress = 0;
842 if(c->c_sasl_bind_mech.bv_val != NULL) {
843 free(c->c_sasl_bind_mech.bv_val);
845 BER_BVZERO( &c->c_sasl_bind_mech );
847 slap_sasl_close( c );
849 if ( c->c_currentber != NULL ) {
850 ber_free( c->c_currentber, 1 );
851 c->c_currentber = NULL;
854 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
856 ber_sockbuf_free( c->c_sb );
857 if ( sd != AC_SOCKET_INVALID ) {
858 slapd_remove( sd, 1, 0, 1 );
860 Statslog( LDAP_DEBUG_STATS, (close_reason
861 ? "conn=%lu fd=%ld closed (%s)\n"
862 : "conn=%lu fd=%ld closed\n"),
863 connid, (long) sd, close_reason, 0, 0 );
868 c->c_sb = ber_sockbuf_alloc( );
871 ber_len_t max = sockbuf_max_incoming;
872 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
875 c->c_conn_state = SLAP_C_INVALID;
876 c->c_struct_state = SLAP_C_UNUSED;
877 c->c_close_reason = "?"; /* should never be needed */
880 /* call destructors, then constructors; avoids unnecessary allocation */
881 if ( slapi_plugins_used ) {
882 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
887 int connection_state_closing( Connection *c )
889 /* c_mutex must be locked by caller */
893 assert( c->c_struct_state == SLAP_C_USED );
895 state = c->c_conn_state;
897 assert( state != SLAP_C_INVALID );
899 return state == SLAP_C_CLOSING;
902 static void connection_abandon( Connection *c )
904 /* c_mutex must be locked by caller */
906 Operation *o, *next, op = {0};
912 op.o_connid = c->c_connid;
913 op.o_tag = LDAP_REQ_ABANDON;
914 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
915 next = LDAP_STAILQ_NEXT( o, o_next );
916 op.orn_msgid = o->o_msgid;
918 op.o_bd = frontendDB;
919 frontendDB->be_abandon( &op, &rs );
922 /* remove pending operations */
923 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
924 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
925 LDAP_STAILQ_NEXT(o, o_next) = NULL;
930 void connection_closing( Connection *c, const char *why )
932 assert( connections != NULL );
934 assert( c->c_struct_state == SLAP_C_USED );
935 assert( c->c_conn_state != SLAP_C_INVALID );
937 /* c_mutex must be locked by caller */
939 if( c->c_conn_state != SLAP_C_CLOSING ) {
942 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
943 Debug( LDAP_DEBUG_TRACE,
944 "connection_closing: readying conn=%lu sd=%d for close\n",
945 c->c_connid, sd, 0 );
946 /* update state to closing */
947 c->c_conn_state = SLAP_C_CLOSING;
948 c->c_close_reason = why;
950 /* don't listen on this port anymore */
951 slapd_clr_read( sd, 1 );
953 /* abandon active operations */
954 connection_abandon( c );
956 /* wake write blocked operations */
957 slapd_clr_write( sd, 1 );
958 if ( c->c_writewaiter ) {
959 ldap_pvt_thread_cond_signal( &c->c_write_cv );
960 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
961 ldap_pvt_thread_yield();
962 ldap_pvt_thread_mutex_lock( &c->c_mutex );
964 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
965 /* Client closed connection after doing Unbind. */
966 c->c_close_reason = NULL;
970 static void connection_close( Connection *c )
974 assert( connections != NULL );
976 assert( c->c_struct_state == SLAP_C_USED );
977 assert( c->c_conn_state == SLAP_C_CLOSING );
979 /* note: connections_mutex and c_mutex should be locked by caller */
981 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
982 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
983 Debug( LDAP_DEBUG_TRACE,
984 "connection_close: deferring conn=%lu sd=%d\n",
985 c->c_connid, sd, 0 );
989 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
990 c->c_connid, sd, 0 );
991 connection_destroy( c );
994 unsigned long connections_nextid(void)
997 assert( connections != NULL );
999 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1003 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1008 Connection* connection_first( ber_socket_t *index )
1010 #ifdef SLAP_MULTI_CONN_ARRAY
1014 assert( connections != NULL );
1015 assert( index != NULL );
1017 #ifdef SLAP_MULTI_CONN_ARRAY
1018 for ( conn_array_id = 0;
1019 conn_array_id < NUM_CONNECTION_ARRAY;
1022 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1025 ldap_pvt_thread_mutex_lock( &connections_mutex );
1030 return connection_next(NULL, index);
1033 Connection* connection_next( Connection *c, ber_socket_t *index )
1034 #ifdef SLAP_MULTI_CONN_ARRAY
1038 assert( connections != NULL );
1039 assert( index != NULL );
1040 assert( *index >= 0 && *index < MCA_ARRAY_SIZE );
1042 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1046 for(; *index < dtblsize; (*index)++) {
1047 assert( MCA_conn_check( *index ) );
1048 conn = MCA_GET_CONNECTION(*index);
1049 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1050 assert( conn->c_conn_state == SLAP_C_INVALID );
1051 #ifndef HAVE_WINSOCK
1058 if( conn->c_struct_state == SLAP_C_USED ) {
1059 assert( conn->c_conn_state != SLAP_C_INVALID );
1065 assert( conn->c_struct_state == SLAP_C_UNUSED );
1066 assert( conn->c_conn_state == SLAP_C_INVALID );
1069 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1076 assert( connections != NULL );
1077 assert( index != NULL );
1078 assert( *index <= dtblsize );
1080 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1084 for(; *index < dtblsize; (*index)++) {
1085 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1086 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1087 #ifndef HAVE_WINSOCK
1094 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1095 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1096 c = &connections[(*index)++];
1100 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1101 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1104 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1109 void connection_done( Connection *c )
1111 #ifdef SLAP_MULTI_CONN_ARRAY
1115 assert( connections != NULL );
1117 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1119 #ifdef SLAP_MULTI_CONN_ARRAY
1120 for ( conn_array_id = 0;
1121 conn_array_id < NUM_CONNECTION_ARRAY;
1124 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1127 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1132 * connection_activity - handle the request operation op on connection
1133 * conn. This routine figures out what kind of operation it is and
1134 * calls the appropriate stub to handle it.
1137 #ifdef SLAPD_MONITOR
1138 /* FIXME: returns 0 in case of failure */
1139 #define INCR_OP_INITIATED(index) \
1141 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1142 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1143 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1145 #define INCR_OP_COMPLETED(index) \
1147 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1148 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1149 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1150 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1152 #else /* !SLAPD_MONITOR */
1153 #define INCR_OP_INITIATED(index) do { } while (0)
1154 #define INCR_OP_COMPLETED(index) \
1156 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1157 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1158 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1160 #endif /* !SLAPD_MONITOR */
1163 * NOTE: keep in sync with enum in slapd.h
1165 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1180 connection_operation( void *ctx, void *arg_v )
1182 int rc = LDAP_OTHER;
1183 Operation *op = arg_v;
1184 SlapReply rs = {REP_RESULT};
1185 ber_tag_t tag = op->o_tag;
1187 Connection *conn = op->o_conn;
1188 void *memctx = NULL;
1189 void *memctx_null = NULL;
1192 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1193 /* FIXME: returns 0 in case of failure */
1194 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1195 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1197 op->o_threadctx = ctx;
1201 case LDAP_REQ_UNBIND:
1203 case LDAP_REQ_DELETE:
1204 case LDAP_REQ_MODDN:
1205 case LDAP_REQ_MODIFY:
1206 case LDAP_REQ_COMPARE:
1207 case LDAP_REQ_SEARCH:
1208 case LDAP_REQ_ABANDON:
1209 case LDAP_REQ_EXTENDED:
1212 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1213 "conn %lu unknown LDAP request 0x%lx\n",
1214 conn->c_connid, tag, 0 );
1215 op->o_tag = LBER_ERROR;
1216 rs.sr_err = LDAP_PROTOCOL_ERROR;
1217 rs.sr_text = "unknown LDAP request";
1218 send_ldap_disconnect( op, &rs );
1219 rc = SLAPD_DISCONNECT;
1220 goto operations_error;
1223 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1224 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1225 "error: SASL bind in progress (tag=%ld).\n",
1227 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1228 "SASL bind in progress" );
1229 rc = LDAP_OPERATIONS_ERROR;
1230 goto operations_error;
1233 /* We can use Thread-Local storage for most mallocs. We can
1234 * also use TL for ber parsing, but not on Add or Modify.
1237 memsiz = ber_len( op->o_ber ) * 64;
1238 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1240 memsiz = SLAP_SLAB_SIZE;
1242 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1243 op->o_tmpmemctx = memctx;
1244 op->o_tmpmfuncs = &slap_sl_mfuncs;
1245 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1246 /* Note - the ber and its buffer are already allocated from
1247 * regular memory; this only affects subsequent mallocs that
1248 * ber_scanf may invoke.
1250 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1255 opidx = SLAP_OP_BIND;
1258 case LDAP_REQ_UNBIND:
1259 opidx = SLAP_OP_UNBIND;
1263 opidx = SLAP_OP_ADD;
1266 case LDAP_REQ_DELETE:
1267 opidx = SLAP_OP_DELETE;
1270 case LDAP_REQ_MODRDN:
1271 opidx = SLAP_OP_MODRDN;
1274 case LDAP_REQ_MODIFY:
1275 opidx = SLAP_OP_MODIFY;
1278 case LDAP_REQ_COMPARE:
1279 opidx = SLAP_OP_COMPARE;
1282 case LDAP_REQ_SEARCH:
1283 opidx = SLAP_OP_SEARCH;
1286 case LDAP_REQ_ABANDON:
1287 opidx = SLAP_OP_ABANDON;
1290 case LDAP_REQ_EXTENDED:
1291 opidx = SLAP_OP_EXTENDED;
1299 assert( opidx > -1 );
1300 INCR_OP_INITIATED( opidx );
1301 rc = (*(opfun[opidx]))( op, &rs );
1304 if ( rc == SLAPD_DISCONNECT ) {
1307 } else if ( opidx > -1 ) {
1308 /* increment completed operations count
1309 * only if operation was initiated
1310 * and rc != SLAPD_DISCONNECT */
1311 INCR_OP_COMPLETED( opidx );
1314 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1315 if ( rc == SLAPD_ABANDON ) {
1316 op->o_cancel = SLAP_CANCEL_ACK;
1318 op->o_cancel = LDAP_TOO_LATE;
1321 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1322 op->o_cancel != SLAP_CANCEL_DONE )
1324 ldap_pvt_thread_yield();
1327 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1329 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1331 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1332 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1334 conn->c_n_ops_executing--;
1335 conn->c_n_ops_completed++;
1339 case LDAP_REQ_UNBIND:
1340 /* c_mutex is locked */
1341 connection_closing( conn,
1342 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1346 connection_resched( conn );
1347 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1351 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1353 int connection_client_setup(
1355 ldap_pvt_thread_start_t *func,
1361 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1362 CONN_IS_CLIENT, 0, NULL );
1363 if ( rc < 0 ) return -1;
1365 c = connection_get( s );
1366 c->c_clientfunc = func;
1367 c->c_clientarg = arg;
1369 slapd_add_internal( s, 0 );
1370 slapd_set_read( s, 1 );
1371 connection_return( c );
1375 void connection_client_enable(
1378 slapd_set_read( s, 1 );
1381 void connection_client_stop(
1386 /* get (locked) connection */
1387 c = connection_get( s );
1389 assert( c->c_conn_state == SLAP_C_CLIENT );
1391 c->c_listener = NULL;
1392 c->c_conn_state = SLAP_C_INVALID;
1393 c->c_struct_state = SLAP_C_UNUSED;
1394 c->c_close_reason = "?"; /* should never be needed */
1396 ber_sockbuf_free( c->c_sb );
1397 slapd_remove( s, 0, 1, 1 );
1398 c->c_sb = ber_sockbuf_alloc( );
1400 ber_len_t max = sockbuf_max_incoming;
1401 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1404 connection_return( c );
1407 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1408 static int connection_read( ber_socket_t s, Operation** op );
1410 static void* connection_read_thread( void* ctx, void* argv )
1413 Operation* new_op[2] = { NULL, NULL };
1414 ber_socket_t s = (long)argv;
1417 * read incoming LDAP requests. If there is more than one,
1418 * the first one is returned with new_op
1420 if( ( rc = connection_read( s, new_op ) ) < 0 ) {
1421 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1422 return (void*)(long)rc;
1425 /* execute a single queued request in the same thread */
1426 if( new_op[0] && !new_op[1] ) {
1427 rc = (long)connection_operation( ctx, new_op[0] );
1430 return (void*)(long)rc;
1433 int connection_read_activate( ber_socket_t s )
1438 * suspend reading on this file descriptor until a connection processing
1439 * thread reads data on it. Otherwise the listener thread will repeatedly
1440 * submit the same event on it to the pool.
1442 rc = slapd_clr_read( s, 0 );
1446 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1447 connection_read_thread, (void *)(long)s );
1450 Debug( LDAP_DEBUG_ANY,
1451 "connection_read_activate(%d): submit failed (%d)\n",
1459 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1461 connection_read( ber_socket_t s, Operation** op )
1463 int connection_read(ber_socket_t s)
1469 assert( connections != NULL );
1471 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1473 /* get (locked) connection */
1474 c = connection_get( s );
1477 Debug( LDAP_DEBUG_ANY,
1478 "connection_read(%ld): no connection!\n",
1481 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1487 if( c->c_conn_state == SLAP_C_CLOSING ) {
1488 Debug( LDAP_DEBUG_TRACE,
1489 "connection_read(%d): closing, ignoring input for id=%lu\n",
1490 s, c->c_connid, 0 );
1492 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1493 slapd_set_read( s, 1 );
1495 connection_return( c );
1496 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1500 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1501 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1502 /* read should already be cleared */
1504 slapd_clr_read( s, 0 );
1506 ldap_pvt_thread_pool_submit( &connection_pool,
1507 c->c_clientfunc, c->c_clientarg );
1509 connection_return( c );
1510 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1514 Debug( LDAP_DEBUG_TRACE,
1515 "connection_read(%d): checking for input on id=%lu\n",
1516 s, c->c_connid, 0 );
1519 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1520 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1522 Debug( LDAP_DEBUG_TRACE,
1523 "connection_read(%d): TLS accept failure "
1524 "error=%d id=%lu, closing\n",
1525 s, rc, c->c_connid );
1527 c->c_needs_tls_accept = 0;
1528 /* connections_mutex and c_mutex are locked */
1529 connection_closing( c, "TLS negotiation failure" );
1535 /* Drain input before close, to allow SSL error codes
1536 * to propagate to client. */
1542 rc = select(s+1, &rfd, NULL, NULL, &tv);
1544 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1550 connection_close( c );
1551 connection_return( c );
1552 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1555 } else if ( rc == 0 ) {
1557 struct berval authid = BER_BVNULL;
1559 c->c_needs_tls_accept = 0;
1561 /* we need to let SASL know */
1562 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1564 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1565 if( c->c_tls_ssf > c->c_ssf ) {
1566 c->c_ssf = c->c_tls_ssf;
1569 rc = dnX509peerNormalize( ssl, &authid );
1570 if ( rc != LDAP_SUCCESS ) {
1571 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1572 "unable to get TLS client DN, error=%d id=%lu\n",
1573 s, rc, c->c_connid );
1575 Statslog( LDAP_DEBUG_STATS,
1576 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1577 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1578 slap_sasl_external( c, c->c_tls_ssf, &authid );
1579 if ( authid.bv_val ) free( authid.bv_val );
1582 /* if success and data is ready, fall thru to data input loop */
1584 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1586 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1587 slapd_set_read( s, 1 );
1590 connection_return( c );
1591 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1597 #ifdef HAVE_CYRUS_SASL
1598 if ( c->c_sasl_layers ) {
1599 /* If previous layer is not removed yet, give up for now */
1600 if ( !c->c_sasl_sockctx ) {
1601 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1602 slapd_set_read( s, 1 );
1605 connection_return( c );
1606 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1610 c->c_sasl_layers = 0;
1612 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1613 if( rc != LDAP_SUCCESS ) {
1614 Debug( LDAP_DEBUG_TRACE,
1615 "connection_read(%d): SASL install error "
1616 "error=%d id=%lu, closing\n",
1617 s, rc, c->c_connid );
1619 /* connections_mutex and c_mutex are locked */
1620 connection_closing( c, "SASL layer install failure" );
1621 connection_close( c );
1622 connection_return( c );
1623 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1629 #define CONNECTION_INPUT_LOOP 1
1630 /* #define DATA_READY_LOOP 1 */
1633 /* How do we do this without getting into a busy loop ? */
1634 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1635 rc = connection_input( c, op );
1637 rc = connection_input( c );
1640 #ifdef DATA_READY_LOOP
1641 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1642 #elif CONNECTION_INPUT_LOOP
1649 Debug( LDAP_DEBUG_TRACE,
1650 "connection_read(%d): input error=%d id=%lu, closing.\n",
1651 s, rc, c->c_connid );
1653 /* connections_mutex and c_mutex are locked */
1654 connection_closing( c, conn_lost_str );
1655 connection_close( c );
1656 connection_return( c );
1657 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1661 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1662 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1663 slapd_set_write( s, 0 );
1666 slapd_set_read( s, 1 );
1668 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1669 slapd_set_read( s, 1 );
1672 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1673 slapd_set_write( s, 1 );
1677 connection_return( c );
1678 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1684 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1685 connection_input( Connection *conn , Operation** c_op )
1687 connection_input( Connection *conn )
1696 #ifdef LDAP_CONNECTIONLESS
1702 if ( conn->c_currentber == NULL &&
1703 ( conn->c_currentber = ber_alloc()) == NULL )
1705 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1711 #ifdef LDAP_CONNECTIONLESS
1712 if ( conn->c_is_udp ) {
1713 char peername[sizeof("IP=255.255.255.255:65336")];
1715 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1716 if (len != sizeof(struct sockaddr)) return 1;
1718 sprintf( peername, "IP=%s:%d",
1719 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1720 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1721 Statslog( LDAP_DEBUG_STATS,
1722 "conn=%lu UDP request from %s (%s) accepted.\n",
1723 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1727 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1728 if ( tag != LDAP_TAG_MESSAGE ) {
1732 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1734 Debug( LDAP_DEBUG_TRACE,
1735 "ber_get_next on fd %d failed errno=%d (%s)\n",
1736 sd, err, sock_errstr(err) );
1737 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1738 /* log, close and send error */
1739 ber_free( conn->c_currentber, 1 );
1740 conn->c_currentber = NULL;
1747 ber = conn->c_currentber;
1748 conn->c_currentber = NULL;
1750 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1751 /* log, close and send error */
1752 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1757 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1758 /* log, close and send error */
1759 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1765 #ifdef LDAP_CONNECTIONLESS
1766 if( conn->c_is_udp ) {
1767 if( tag == LBER_OCTETSTRING ) {
1768 ber_get_stringa( ber, &cdn );
1769 tag = ber_peek_tag(ber, &len);
1771 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1772 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1779 if(tag == LDAP_REQ_BIND) {
1780 /* immediately abandon all existing operations upon BIND */
1781 connection_abandon( conn );
1784 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1787 /* clear state if the connection is being reused from inactive */
1788 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1789 memset( &conn->c_pagedresults_state, 0,
1790 sizeof( conn->c_pagedresults_state ) );
1793 op->o_res_ber = NULL;
1795 #ifdef LDAP_CONNECTIONLESS
1796 if (conn->c_is_udp) {
1798 ber_str2bv( cdn, 0, 1, &op->o_dn );
1799 op->o_protocol = LDAP_VERSION2;
1801 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1802 if (op->o_res_ber == NULL) return 1;
1804 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1805 sizeof(struct sockaddr), 0 );
1807 if (rc != sizeof(struct sockaddr)) {
1808 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1812 if (op->o_protocol == LDAP_VERSION2) {
1813 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1815 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1820 #endif /* LDAP_CONNECTIONLESS */
1824 /* Don't process requests when the conn is in the middle of a
1825 * Bind, or if it's closing. Also, don't let any single conn
1826 * use up all the available threads, and don't execute if we're
1827 * currently blocked on output. And don't execute if there are
1828 * already pending ops, let them go first. Abandon operations
1829 * get exceptions to some, but not all, cases.
1833 /* Abandon and Unbind are exempt from these checks */
1834 if (conn->c_conn_state == SLAP_C_CLOSING) {
1837 } else if (conn->c_writewaiter) {
1838 defer = "awaiting write";
1840 } else if (conn->c_n_ops_pending) {
1841 defer = "pending operations";
1845 case LDAP_REQ_ABANDON:
1846 /* Unbind is exempt from these checks */
1847 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1848 defer = "too many executing";
1850 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1855 case LDAP_REQ_UNBIND:
1860 int max = conn->c_dn.bv_len
1861 ? slap_conn_max_pending_auth
1862 : slap_conn_max_pending;
1864 Debug( LDAP_DEBUG_ANY,
1865 "connection_input: conn=%lu deferring operation: %s\n",
1866 conn->c_connid, defer, 0 );
1867 conn->c_n_ops_pending++;
1868 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1869 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1872 conn->c_n_ops_executing++;
1874 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1876 * The first op will be processed in the same thread context,
1877 * as long as there is only one op total.
1878 * Subsequent ops will be submitted to the pool by
1879 * calling connection_op_activate()
1881 if ( c_op[0] == NULL ) {
1882 /* the first incoming request */
1883 connection_op_queue( op );
1887 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1888 connection_operation, (void *) c_op[0] );
1889 connection_op_activate( op );
1892 connection_op_activate( op );
1897 if ( conn->c_struct_state != SLAP_C_USED ) {
1898 /* connection must have got closed underneath us */
1903 assert( conn->c_struct_state == SLAP_C_USED );
1908 connection_resched( Connection *conn )
1912 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1915 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1917 /* use trylock to avoid possible deadlock */
1918 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1921 Debug( LDAP_DEBUG_TRACE,
1922 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1923 conn->c_connid, sd, 0 );
1925 * reaquire locks in the right order...
1926 * this may allow another thread to close this connection,
1927 * so recheck state below.
1929 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1930 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1931 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1934 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1935 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1936 "closed by other thread conn=%lu sd=%d\n",
1937 conn->c_connid, sd, 0 );
1939 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1940 "attempting closing conn=%lu sd=%d\n",
1941 conn->c_connid, sd, 0 );
1942 connection_close( conn );
1945 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1949 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1950 /* other states need different handling */
1954 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1955 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1957 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1958 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1960 /* pending operations should not be marked for abandonment */
1961 assert(!op->o_abandon);
1963 conn->c_n_ops_pending--;
1964 conn->c_n_ops_executing++;
1966 connection_op_activate( op );
1968 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1974 connection_init_log_prefix( Operation *op )
1976 if ( op->o_connid == (unsigned long)(-1) ) {
1977 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1978 "conn=-1 op=%lu", op->o_opid );
1981 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1982 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1986 static int connection_bind_cb( Operation *op, SlapReply *rs )
1988 slap_callback *cb = op->o_callback;
1989 op->o_callback = cb->sc_next;
1991 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1992 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1993 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1996 return SLAP_CB_CONTINUE;
1999 static void connection_op_queue( Operation *op )
2001 ber_tag_t tag = op->o_tag;
2003 if (tag == LDAP_REQ_BIND) {
2004 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
2005 sc->sc_response = connection_bind_cb;
2006 sc->sc_next = op->o_callback;
2007 op->o_callback = sc;
2008 op->o_conn->c_conn_state = SLAP_C_BINDING;
2011 if (!op->o_dn.bv_len) {
2012 op->o_authz = op->o_conn->c_authz;
2013 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2014 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2015 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2017 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2018 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2022 op->o_authtype = op->o_conn->c_authtype;
2023 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2025 if (!op->o_protocol) {
2026 op->o_protocol = op->o_conn->c_protocol
2027 ? op->o_conn->c_protocol : LDAP_VERSION3;
2030 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2031 op->o_protocol > LDAP_VERSION2)
2033 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2036 op->o_connid = op->o_conn->c_connid;
2037 connection_init_log_prefix( op );
2039 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2042 static int connection_op_activate( Operation *op )
2046 connection_op_queue( op );
2048 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2049 connection_operation, (void *) op );
2052 Debug( LDAP_DEBUG_ANY,
2053 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2054 rc, op->o_connid, 0 );
2055 /* should move op to pending list */
2061 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2062 static int connection_write( ber_socket_t s );
2063 static void *connection_write_thread( void *ctx, void *arg )
2065 return (void *)(long)connection_write((long)arg);
2068 int connection_write_activate( ber_socket_t s )
2073 * suspend reading on this file descriptor until a connection processing
2074 * thread write data on it. Otherwise the listener thread will repeatedly
2075 * submit the same event on it to the pool.
2077 slapd_clr_write( s, 0);
2079 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2080 connection_write_thread, (void *)(long)s );
2083 Debug( LDAP_DEBUG_ANY,
2084 "connection_write_activate(%d): submit failed (%d)\n",
2092 int connection_write(ber_socket_t s)
2097 assert( connections != NULL );
2099 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2101 c = connection_get( s );
2103 Debug( LDAP_DEBUG_ANY,
2104 "connection_write(%ld): no connection!\n",
2106 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2110 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2111 slapd_clr_write( s, 0);
2115 Debug( LDAP_DEBUG_TRACE,
2116 "connection_write(%d): waking output for id=%lu\n",
2117 s, c->c_connid, 0 );
2118 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2120 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2121 slapd_set_read( s, 1 );
2123 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2124 slapd_set_write( s, 1 );
2127 /* If there are ops pending because of a writewaiter,
2130 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2131 if ( !c->c_writewaiter ) break;
2132 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2134 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2135 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2137 /* pending operations should not be marked for abandonment */
2138 assert(!op->o_abandon);
2140 c->c_n_ops_pending--;
2141 c->c_n_ops_executing++;
2143 connection_op_activate( op );
2148 connection_return( c );
2149 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2154 connection_fake_init(
2159 conn->c_connid = -1;
2160 conn->c_send_ldap_result = slap_send_ldap_result;
2161 conn->c_send_search_entry = slap_send_search_entry;
2162 conn->c_send_search_reference = slap_send_search_reference;
2163 conn->c_listener = (Listener *)&dummy_list;
2164 conn->c_peer_domain = slap_empty_bv;
2165 conn->c_peer_name = slap_empty_bv;
2167 memset(op, 0, OPERATION_BUFFER_SIZE);
2168 op->o_hdr = (Opheader *)(op+1);
2169 op->o_controls = (void **)(op->o_hdr+1);
2170 /* set memory context */
2171 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2172 op->o_tmpmfuncs = &slap_sl_mfuncs;
2173 op->o_threadctx = ctx;
2176 op->o_connid = op->o_conn->c_connid;
2177 connection_init_log_prefix( op );
2179 slap_op_time( &op->o_time, &op->o_tincr );
2183 connection_assign_nextid( Connection *conn )
2185 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2186 conn->c_connid = conn_nextid++;
2187 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );