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 (round up to a power of 2) */
53 # define NUM_CONNECTION_ARRAY 4
55 /* partition the array in a modulo manner */
56 # define MCA_conn_array_id(fd) ((int)(fd)%NUM_CONNECTION_ARRAY)
57 # define MCA_conn_array_element_id(fd) ((int)(fd)/NUM_CONNECTION_ARRAY)
58 # define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \
59 [MCA_conn_array_element_id(fd)])
60 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
63 /* protected by connections_mutex */
64 static ldap_pvt_thread_mutex_t connections_mutex;
65 static Connection *connections = NULL;
67 # define MCA_GET_CONNECTION(fd) (&connections[s])
68 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
71 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
72 static unsigned long conn_nextid = 0;
74 static const char conn_lost_str[] = "connection lost";
76 /* structure state (protected by connections_mutex) */
77 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
78 #define SLAP_C_UNUSED 0x01
79 #define SLAP_C_USED 0x02
81 /* connection state (protected by c_mutex ) */
82 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
83 #define SLAP_C_INACTIVE 0x01 /* zero threads */
84 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
85 #define SLAP_C_BINDING 0x03 /* binding */
86 #define SLAP_C_CLOSING 0x04 /* closing */
87 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
90 connection_state2str( int state )
93 case SLAP_C_INVALID: return "!";
94 case SLAP_C_INACTIVE: return "|";
95 case SLAP_C_ACTIVE: return "";
96 case SLAP_C_BINDING: return "B";
97 case SLAP_C_CLOSING: return "C";
98 case SLAP_C_CLIENT: return "L";
104 static Connection* connection_get( ber_socket_t s );
106 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
107 static int connection_input( Connection *c, Operation** op );
109 static int connection_input( Connection *c );
111 static void connection_close( Connection *c );
113 static int connection_op_activate( Operation *op );
114 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
115 static void connection_op_queue( Operation *op );
117 static int connection_resched( Connection *conn );
118 static void connection_abandon( Connection *conn );
119 static void connection_destroy( Connection *c );
121 static ldap_pvt_thread_start_t connection_operation;
124 * Initialize connection management infrastructure.
126 int connections_init(void)
127 #ifdef SLAP_MULTI_CONN_ARRAY
132 assert( connections == NULL );
134 if( connections != NULL) {
135 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
140 connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc(
141 NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
142 if( connections_mutex == NULL ) {
143 Debug( LDAP_DEBUG_ANY, "connections_init: "
144 "allocation of connection mutex[%d] failed\n", i, 0, 0 );
148 connections = (Connection**) ch_calloc(
149 NUM_CONNECTION_ARRAY, sizeof(Connection*));
150 if( connections == NULL ) {
151 Debug( LDAP_DEBUG_ANY, "connections_init: "
152 "allocation of connection[%d] failed\n", 0, 0, 0 );
156 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
157 ldap_pvt_thread_mutex_init( connections_mutex+i );
158 connections[i] = (Connection*) ch_calloc(
159 dtblsize/NUM_CONNECTION_ARRAY, sizeof(Connection) );
160 if( connections[i] == NULL ) {
161 Debug( LDAP_DEBUG_ANY, "connections_init: "
162 "allocation (%d*%ld) of connection array[%d] failed\n",
163 dtblsize, (long) sizeof(Connection), i );
168 /* should check return of every call */
169 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
171 assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
172 assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state ==
173 SLAP_C_UNINITIALIZED );
175 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
176 conn = connections[i];
177 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
178 conn[j].c_conn_idx = j;
183 * per entry initialization of the Connection array initialization
184 * will be done by connection_init()
193 assert( connections == NULL );
195 if( connections != NULL) {
196 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
201 /* should check return of every call */
202 ldap_pvt_thread_mutex_init( &connections_mutex );
203 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
205 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
207 if( connections == NULL ) {
208 Debug( LDAP_DEBUG_ANY, "connections_init: "
209 "allocation (%d*%ld) of connection array failed\n",
210 dtblsize, (long) sizeof(Connection), 0 );
214 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
215 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
217 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
220 * per entry initialization of the Connection array initialization
221 * will be done by connection_init()
229 * Destroy connection management infrastructure.
232 int connections_destroy(void)
233 #ifdef SLAP_MULTI_CONN_ARRAY
238 if( connections == NULL) {
239 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
244 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
245 Connection* conn = connections[i];
246 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
247 if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
248 ber_sockbuf_free( conn[j].c_sb );
249 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
250 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
251 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
254 if ( slapi_plugins_used ) {
255 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
263 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
264 free( connections[i] );
265 connections[i] = NULL;
266 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
269 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
278 /* should check return of every call */
280 if( connections == NULL) {
281 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
286 for ( i = 0; i < dtblsize; i++ ) {
287 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
288 ber_sockbuf_free( connections[i].c_sb );
289 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
290 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
291 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
293 if ( slapi_plugins_used ) {
294 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
304 ldap_pvt_thread_mutex_destroy( &connections_mutex );
305 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
311 * shutdown all connections
313 int connections_shutdown(void)
314 #ifdef SLAP_MULTI_CONN_ARRAY
319 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
320 Connection* conn = connections[i];
321 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
322 for ( j = 0; j < (dtblsize/NUM_CONNECTION_ARRAY); j++ ) {
323 if( conn[j].c_struct_state != SLAP_C_USED ) {
326 /* give persistent clients a chance to cleanup */
327 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
328 ldap_pvt_thread_pool_submit( &connection_pool,
329 conn[j].c_clientfunc, conn[j].c_clientarg );
333 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
334 /* connections_mutex and c_mutex are locked */
335 connection_closing( &conn[j], "connection shutdown" );
336 connection_close( &conn[j] );
337 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
340 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
350 ldap_pvt_thread_mutex_lock( &connections_mutex );
352 for ( i = 0; i < dtblsize; i++ ) {
353 if( connections[i].c_struct_state != SLAP_C_USED ) {
356 /* give persistent clients a chance to cleanup */
357 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
358 ldap_pvt_thread_pool_submit( &connection_pool,
359 connections[i].c_clientfunc, connections[i].c_clientarg );
363 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
365 /* connections_mutex and c_mutex are locked */
366 connection_closing( &connections[i], "slapd shutdown" );
367 connection_close( &connections[i] );
369 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
372 ldap_pvt_thread_mutex_unlock( &connections_mutex );
379 * Timeout idle connections.
381 int connections_timeout_idle(time_t now)
387 for( c = connection_first( &connindex );
389 c = connection_next( c, &connindex ) )
391 /* Don't timeout a slow-running request or a persistent
392 * outbound connection */
393 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
397 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
399 connection_closing( c, "idletimeout" );
400 connection_close( c );
404 connection_done( c );
409 static Connection* connection_get( ber_socket_t s )
411 /* connections_mutex should be locked by caller */
415 Debug( LDAP_DEBUG_ARGS,
416 "connection_get(%ld)\n",
419 assert( connections != NULL );
421 if(s == AC_SOCKET_INVALID) return NULL;
424 c = MCA_GET_CONNECTION(s);
426 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
433 for(i=0; i<dtblsize; i++) {
434 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
435 assert( connections[i].c_conn_state == SLAP_C_INVALID );
436 assert( connections[i].c_sb == 0 );
440 ber_sockbuf_ctrl( connections[i].c_sb,
441 LBER_SB_OPT_GET_FD, &sd );
443 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
444 assert( connections[i].c_conn_state == SLAP_C_INVALID );
445 assert( sd == AC_SOCKET_INVALID );
449 /* state can actually change from used -> unused by resched,
450 * so don't assert details here.
464 ldap_pvt_thread_mutex_lock( &c->c_mutex );
466 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
467 if( c->c_struct_state != SLAP_C_USED ) {
468 /* connection must have been closed due to resched */
470 assert( c->c_conn_state == SLAP_C_INVALID );
471 assert( sd == AC_SOCKET_INVALID );
473 Debug( LDAP_DEBUG_TRACE,
474 "connection_get(%d): connection not used\n",
477 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
481 Debug( LDAP_DEBUG_TRACE,
482 "connection_get(%d): got connid=%lu\n",
487 assert( c->c_struct_state == SLAP_C_USED );
488 assert( c->c_conn_state != SLAP_C_INVALID );
489 assert( sd != AC_SOCKET_INVALID );
491 #ifndef SLAPD_MONITOR
492 if ( global_idletimeout > 0 )
493 #endif /* ! SLAPD_MONITOR */
495 c->c_activitytime = slap_get_time();
502 static void connection_return( Connection *c )
504 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
507 long connection_init(
511 const char* peername,
514 struct berval *authid )
519 assert( connections != NULL );
521 assert( listener != NULL );
522 assert( dnsname != NULL );
523 assert( peername != NULL );
526 assert( flags != CONN_IS_TLS );
529 if( s == AC_SOCKET_INVALID ) {
530 Debug( LDAP_DEBUG_ANY,
531 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
537 assert( s < dtblsize );
540 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
543 c = MCA_GET_CONNECTION(s);
549 for( i=0; i < dtblsize; i++) {
552 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
553 assert( connections[i].c_sb == 0 );
558 sd = AC_SOCKET_INVALID;
559 if (connections[i].c_sb != NULL) {
560 ber_sockbuf_ctrl( connections[i].c_sb,
561 LBER_SB_OPT_GET_FD, &sd );
564 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
565 assert( sd == AC_SOCKET_INVALID );
570 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
572 assert( connections[i].c_struct_state == SLAP_C_USED );
573 assert( connections[i].c_conn_state != SLAP_C_INVALID );
574 assert( sd != AC_SOCKET_INVALID );
578 Debug( LDAP_DEBUG_ANY,
579 "connection_init(%d): connection table full "
580 "(%d/%d)\n", s, i, dtblsize);
581 ldap_pvt_thread_mutex_unlock( &connections_mutex );
589 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
590 c->c_send_ldap_result = slap_send_ldap_result;
591 c->c_send_search_entry = slap_send_search_entry;
592 c->c_send_search_reference = slap_send_search_reference;
593 c->c_send_ldap_extended = slap_send_ldap_extended;
594 #ifdef LDAP_RES_INTERMEDIATE
595 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
598 BER_BVZERO( &c->c_authmech );
599 BER_BVZERO( &c->c_dn );
600 BER_BVZERO( &c->c_ndn );
602 c->c_listener = NULL;
603 BER_BVZERO( &c->c_peer_domain );
604 BER_BVZERO( &c->c_peer_name );
606 LDAP_STAILQ_INIT(&c->c_ops);
607 LDAP_STAILQ_INIT(&c->c_pending_ops);
609 BER_BVZERO( &c->c_sasl_bind_mech );
611 c->c_sasl_authctx = NULL;
612 c->c_sasl_sockctx = NULL;
613 c->c_sasl_extra = NULL;
614 c->c_sasl_bindop = NULL;
616 c->c_sb = ber_sockbuf_alloc( );
619 ber_len_t max = sockbuf_max_incoming;
620 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
623 c->c_currentber = NULL;
625 /* should check status of thread calls */
626 ldap_pvt_thread_mutex_init( &c->c_mutex );
627 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
628 ldap_pvt_thread_cond_init( &c->c_write_cv );
631 if ( slapi_plugins_used ) {
632 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
636 c->c_struct_state = SLAP_C_UNUSED;
639 ldap_pvt_thread_mutex_lock( &c->c_mutex );
641 assert( c->c_struct_state == SLAP_C_UNUSED );
642 assert( BER_BVISNULL( &c->c_authmech ) );
643 assert( BER_BVISNULL( &c->c_dn ) );
644 assert( BER_BVISNULL( &c->c_ndn ) );
645 assert( c->c_listener == NULL );
646 assert( BER_BVISNULL( &c->c_peer_domain ) );
647 assert( BER_BVISNULL( &c->c_peer_name ) );
648 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
649 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
650 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
651 assert( c->c_sasl_done == 0 );
652 assert( c->c_sasl_authctx == NULL );
653 assert( c->c_sasl_sockctx == NULL );
654 assert( c->c_sasl_extra == NULL );
655 assert( c->c_sasl_bindop == NULL );
656 assert( c->c_currentber == NULL );
657 assert( c->c_writewaiter == 0);
659 c->c_listener = listener;
661 if ( flags == CONN_IS_CLIENT ) {
662 c->c_conn_state = SLAP_C_CLIENT;
663 c->c_struct_state = SLAP_C_USED;
664 c->c_close_reason = "?"; /* should never be needed */
665 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
666 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
667 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
672 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
673 ber_str2bv( peername, 0, 1, &c->c_peer_name );
675 c->c_n_ops_received = 0;
676 c->c_n_ops_executing = 0;
677 c->c_n_ops_pending = 0;
678 c->c_n_ops_completed = 0;
684 /* set to zero until bind, implies LDAP_VERSION3 */
687 #ifndef SLAPD_MONITOR
688 if ( global_idletimeout > 0 )
689 #endif /* ! SLAPD_MONITOR */
691 c->c_activitytime = c->c_starttime = slap_get_time();
694 #ifdef LDAP_CONNECTIONLESS
696 if( flags == CONN_IS_UDP ) {
699 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
700 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
702 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
703 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
704 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
705 LBER_SBIOD_LEVEL_PROVIDER, NULL );
710 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
711 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
713 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
714 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
718 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
719 INT_MAX, (void*)"ldap_" );
722 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
723 c /* non-NULL */ ) < 0 )
725 Debug( LDAP_DEBUG_ANY,
726 "connection_init(%d, %s): set nonblocking failed\n",
727 s, c->c_peer_name.bv_val, 0 );
730 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
731 id = c->c_connid = conn_nextid++;
732 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
734 c->c_conn_state = SLAP_C_INACTIVE;
735 c->c_struct_state = SLAP_C_USED;
736 c->c_close_reason = "?"; /* should never be needed */
738 c->c_ssf = c->c_transport_ssf = ssf;
742 if ( flags == CONN_IS_TLS ) {
744 c->c_needs_tls_accept = 1;
747 c->c_needs_tls_accept = 0;
751 slap_sasl_open( c, 0 );
752 slap_sasl_external( c, ssf, authid );
754 slapd_add_internal( s, 1 );
755 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
756 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
758 backend_connection_init(c);
763 void connection2anonymous( Connection *c )
765 assert( connections != NULL );
769 ber_len_t max = sockbuf_max_incoming;
770 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
773 if ( !BER_BVISNULL( &c->c_authmech ) ) {
774 ch_free(c->c_authmech.bv_val);
776 BER_BVZERO( &c->c_authmech );
778 if ( !BER_BVISNULL( &c->c_dn ) ) {
779 ch_free(c->c_dn.bv_val);
781 BER_BVZERO( &c->c_dn );
783 if ( !BER_BVISNULL( &c->c_ndn ) ) {
784 ch_free(c->c_ndn.bv_val);
786 BER_BVZERO( &c->c_ndn );
788 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
789 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
791 BER_BVZERO( &c->c_sasl_authz_dn );
793 c->c_authz_backend = NULL;
797 connection_destroy( Connection *c )
799 /* note: connections_mutex should be locked by caller */
801 unsigned long connid;
802 const char *close_reason;
804 assert( connections != NULL );
806 assert( c->c_struct_state != SLAP_C_UNUSED );
807 assert( c->c_conn_state != SLAP_C_INVALID );
808 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
809 assert( c->c_writewaiter == 0);
811 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
812 connid = c->c_connid;
813 close_reason = c->c_close_reason;
815 backend_connection_destroy(c);
820 c->c_activitytime = c->c_starttime = 0;
822 connection2anonymous( c );
823 c->c_listener = NULL;
825 if(c->c_peer_domain.bv_val != NULL) {
826 free(c->c_peer_domain.bv_val);
828 BER_BVZERO( &c->c_peer_domain );
829 if(c->c_peer_name.bv_val != NULL) {
830 free(c->c_peer_name.bv_val);
832 BER_BVZERO( &c->c_peer_name );
834 c->c_sasl_bind_in_progress = 0;
835 if(c->c_sasl_bind_mech.bv_val != NULL) {
836 free(c->c_sasl_bind_mech.bv_val);
838 BER_BVZERO( &c->c_sasl_bind_mech );
840 slap_sasl_close( c );
842 if ( c->c_currentber != NULL ) {
843 ber_free( c->c_currentber, 1 );
844 c->c_currentber = NULL;
847 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
849 ber_sockbuf_free( c->c_sb );
850 if ( sd != AC_SOCKET_INVALID ) {
851 slapd_remove( sd, 1, 0, 1 );
853 Statslog( LDAP_DEBUG_STATS, (close_reason
854 ? "conn=%lu fd=%ld closed (%s)\n"
855 : "conn=%lu fd=%ld closed\n"),
856 connid, (long) sd, close_reason, 0, 0 );
861 c->c_sb = ber_sockbuf_alloc( );
864 ber_len_t max = sockbuf_max_incoming;
865 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
868 c->c_conn_state = SLAP_C_INVALID;
869 c->c_struct_state = SLAP_C_UNUSED;
870 c->c_close_reason = "?"; /* should never be needed */
873 /* call destructors, then constructors; avoids unnecessary allocation */
874 if ( slapi_plugins_used ) {
875 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
880 int connection_state_closing( Connection *c )
882 /* c_mutex must be locked by caller */
886 assert( c->c_struct_state == SLAP_C_USED );
888 state = c->c_conn_state;
890 assert( state != SLAP_C_INVALID );
892 return state == SLAP_C_CLOSING;
895 static void connection_abandon( Connection *c )
897 /* c_mutex must be locked by caller */
899 Operation *o, *next, op = {0};
905 op.o_connid = c->c_connid;
906 op.o_tag = LDAP_REQ_ABANDON;
907 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
908 next = LDAP_STAILQ_NEXT( o, o_next );
909 op.orn_msgid = o->o_msgid;
911 op.o_bd = frontendDB;
912 frontendDB->be_abandon( &op, &rs );
915 /* remove pending operations */
916 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
917 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
918 LDAP_STAILQ_NEXT(o, o_next) = NULL;
923 void connection_closing( Connection *c, const char *why )
925 assert( connections != NULL );
927 assert( c->c_struct_state == SLAP_C_USED );
928 assert( c->c_conn_state != SLAP_C_INVALID );
930 /* c_mutex must be locked by caller */
932 if( c->c_conn_state != SLAP_C_CLOSING ) {
935 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
936 Debug( LDAP_DEBUG_TRACE,
937 "connection_closing: readying conn=%lu sd=%d for close\n",
938 c->c_connid, sd, 0 );
939 /* update state to closing */
940 c->c_conn_state = SLAP_C_CLOSING;
941 c->c_close_reason = why;
943 /* don't listen on this port anymore */
944 slapd_clr_read( sd, 1 );
946 /* abandon active operations */
947 connection_abandon( c );
949 /* wake write blocked operations */
950 slapd_clr_write( sd, 1 );
951 if ( c->c_writewaiter ) {
952 ldap_pvt_thread_cond_signal( &c->c_write_cv );
953 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
954 ldap_pvt_thread_yield();
955 ldap_pvt_thread_mutex_lock( &c->c_mutex );
957 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
958 /* Client closed connection after doing Unbind. */
959 c->c_close_reason = NULL;
963 static void connection_close( Connection *c )
967 assert( connections != NULL );
969 assert( c->c_struct_state == SLAP_C_USED );
970 assert( c->c_conn_state == SLAP_C_CLOSING );
972 /* note: connections_mutex and c_mutex should be locked by caller */
974 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
975 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
976 Debug( LDAP_DEBUG_TRACE,
977 "connection_close: deferring conn=%lu sd=%d\n",
978 c->c_connid, sd, 0 );
982 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
983 c->c_connid, sd, 0 );
984 connection_destroy( c );
987 unsigned long connections_nextid(void)
990 assert( connections != NULL );
992 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
996 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1001 Connection* connection_first( ber_socket_t *index )
1003 #ifdef SLAP_MULTI_CONN_ARRAY
1007 assert( connections != NULL );
1008 assert( index != NULL );
1010 #ifdef SLAP_MULTI_CONN_ARRAY
1011 for ( conn_array_id = 0;
1012 conn_array_id < NUM_CONNECTION_ARRAY;
1015 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1018 ldap_pvt_thread_mutex_lock( &connections_mutex );
1023 return connection_next(NULL, index);
1026 Connection* connection_next( Connection *c, ber_socket_t *index )
1027 #ifdef SLAP_MULTI_CONN_ARRAY
1031 assert( connections != NULL );
1032 assert( index != NULL );
1033 assert( *index <= (dtblsize/NUM_CONNECTION_ARRAY) );
1035 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1039 for(; *index < dtblsize; (*index)++) {
1040 conn = MCA_GET_CONNECTION(*index);
1041 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1042 assert( conn->c_conn_state == SLAP_C_INVALID );
1043 #ifndef HAVE_WINSOCK
1050 if( conn->c_struct_state == SLAP_C_USED ) {
1051 assert( conn->c_conn_state != SLAP_C_INVALID );
1057 assert( conn->c_struct_state == SLAP_C_UNUSED );
1058 assert( conn->c_conn_state == SLAP_C_INVALID );
1061 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1068 assert( connections != NULL );
1069 assert( index != NULL );
1070 assert( *index <= dtblsize );
1072 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1076 for(; *index < dtblsize; (*index)++) {
1077 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1078 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1079 #ifndef HAVE_WINSOCK
1086 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1087 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1088 c = &connections[(*index)++];
1092 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1093 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1096 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1101 void connection_done( Connection *c )
1103 #ifdef SLAP_MULTI_CONN_ARRAY
1107 assert( connections != NULL );
1109 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1111 #ifdef SLAP_MULTI_CONN_ARRAY
1112 for ( conn_array_id = 0;
1113 conn_array_id < NUM_CONNECTION_ARRAY;
1116 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1119 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1124 * connection_activity - handle the request operation op on connection
1125 * conn. This routine figures out what kind of operation it is and
1126 * calls the appropriate stub to handle it.
1129 #ifdef SLAPD_MONITOR
1130 /* FIXME: returns 0 in case of failure */
1131 #define INCR_OP_INITIATED(index) \
1133 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1134 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1135 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1137 #define INCR_OP_COMPLETED(index) \
1139 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1140 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1141 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1142 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1144 #else /* !SLAPD_MONITOR */
1145 #define INCR_OP_INITIATED(index) do { } while (0)
1146 #define INCR_OP_COMPLETED(index) \
1148 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1149 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1150 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1152 #endif /* !SLAPD_MONITOR */
1155 * NOTE: keep in sync with enum in slapd.h
1157 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1172 connection_operation( void *ctx, void *arg_v )
1174 int rc = LDAP_OTHER;
1175 Operation *op = arg_v;
1176 SlapReply rs = {REP_RESULT};
1177 ber_tag_t tag = op->o_tag;
1179 Connection *conn = op->o_conn;
1180 void *memctx = NULL;
1181 void *memctx_null = NULL;
1184 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1185 /* FIXME: returns 0 in case of failure */
1186 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1187 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1189 op->o_threadctx = ctx;
1193 case LDAP_REQ_UNBIND:
1195 case LDAP_REQ_DELETE:
1196 case LDAP_REQ_MODDN:
1197 case LDAP_REQ_MODIFY:
1198 case LDAP_REQ_COMPARE:
1199 case LDAP_REQ_SEARCH:
1200 case LDAP_REQ_ABANDON:
1201 case LDAP_REQ_EXTENDED:
1204 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1205 "conn %lu unknown LDAP request 0x%lx\n",
1206 conn->c_connid, tag, 0 );
1207 op->o_tag = LBER_ERROR;
1208 rs.sr_err = LDAP_PROTOCOL_ERROR;
1209 rs.sr_text = "unknown LDAP request";
1210 send_ldap_disconnect( op, &rs );
1211 rc = SLAPD_DISCONNECT;
1212 goto operations_error;
1215 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1216 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1217 "error: SASL bind in progress (tag=%ld).\n",
1219 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1220 "SASL bind in progress" );
1221 rc = LDAP_OPERATIONS_ERROR;
1222 goto operations_error;
1225 /* We can use Thread-Local storage for most mallocs. We can
1226 * also use TL for ber parsing, but not on Add or Modify.
1229 memsiz = ber_len( op->o_ber ) * 64;
1230 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1232 memsiz = SLAP_SLAB_SIZE;
1234 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1235 op->o_tmpmemctx = memctx;
1236 op->o_tmpmfuncs = &slap_sl_mfuncs;
1237 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1238 /* Note - the ber and its buffer are already allocated from
1239 * regular memory; this only affects subsequent mallocs that
1240 * ber_scanf may invoke.
1242 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1247 opidx = SLAP_OP_BIND;
1250 case LDAP_REQ_UNBIND:
1251 opidx = SLAP_OP_UNBIND;
1255 opidx = SLAP_OP_ADD;
1258 case LDAP_REQ_DELETE:
1259 opidx = SLAP_OP_DELETE;
1262 case LDAP_REQ_MODRDN:
1263 opidx = SLAP_OP_MODRDN;
1266 case LDAP_REQ_MODIFY:
1267 opidx = SLAP_OP_MODIFY;
1270 case LDAP_REQ_COMPARE:
1271 opidx = SLAP_OP_COMPARE;
1274 case LDAP_REQ_SEARCH:
1275 opidx = SLAP_OP_SEARCH;
1278 case LDAP_REQ_ABANDON:
1279 opidx = SLAP_OP_ABANDON;
1282 case LDAP_REQ_EXTENDED:
1283 opidx = SLAP_OP_EXTENDED;
1291 assert( opidx > -1 );
1292 INCR_OP_INITIATED( opidx );
1293 rc = (*(opfun[opidx]))( op, &rs );
1296 if ( rc == SLAPD_DISCONNECT ) {
1299 } else if ( opidx > -1 ) {
1300 /* increment completed operations count
1301 * only if operation was initiated
1302 * and rc != SLAPD_DISCONNECT */
1303 INCR_OP_COMPLETED( opidx );
1306 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1307 if ( rc == SLAPD_ABANDON ) {
1308 op->o_cancel = SLAP_CANCEL_ACK;
1310 op->o_cancel = LDAP_TOO_LATE;
1313 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1314 op->o_cancel != SLAP_CANCEL_DONE )
1316 ldap_pvt_thread_yield();
1319 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1321 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1323 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1324 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1326 conn->c_n_ops_executing--;
1327 conn->c_n_ops_completed++;
1331 case LDAP_REQ_UNBIND:
1332 /* c_mutex is locked */
1333 connection_closing( conn,
1334 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1338 connection_resched( conn );
1339 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1343 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1345 int connection_client_setup(
1347 ldap_pvt_thread_start_t *func,
1353 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1354 CONN_IS_CLIENT, 0, NULL );
1355 if ( rc < 0 ) return -1;
1357 c = connection_get( s );
1358 c->c_clientfunc = func;
1359 c->c_clientarg = arg;
1361 slapd_add_internal( s, 0 );
1362 slapd_set_read( s, 1 );
1363 connection_return( c );
1367 void connection_client_enable(
1370 slapd_set_read( s, 1 );
1373 void connection_client_stop(
1378 /* get (locked) connection */
1379 c = connection_get( s );
1381 assert( c->c_conn_state == SLAP_C_CLIENT );
1383 c->c_listener = NULL;
1384 c->c_conn_state = SLAP_C_INVALID;
1385 c->c_struct_state = SLAP_C_UNUSED;
1386 c->c_close_reason = "?"; /* should never be needed */
1388 ber_sockbuf_free( c->c_sb );
1389 slapd_remove( s, 0, 1, 1 );
1390 c->c_sb = ber_sockbuf_alloc( );
1392 ber_len_t max = sockbuf_max_incoming;
1393 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1396 connection_return( c );
1399 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1400 static int connection_read( ber_socket_t s, Operation** op );
1402 static void* connection_read_thread( void* ctx, void* argv )
1405 Operation* new_op = NULL;
1406 ber_socket_t s = (long)argv;
1409 * read incoming LDAP requests. If there is more than one,
1410 * the first one is returned with new_op
1412 if( ( rc = connection_read( s, &new_op ) ) < 0 ) {
1413 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1414 return (void*)(long)rc;
1417 /* execute the queued request in the same thread */
1419 rc = (long)connection_operation( ctx, new_op );
1422 return (void*)(long)rc;
1425 int connection_read_activate( ber_socket_t s )
1430 * suspend reading on this file descriptor until a connection processing
1431 * thread reads data on it. Otherwise the listener thread will repeatedly
1432 * submit the same event on it to the pool.
1434 rc = slapd_clr_read( s, 0 );
1438 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1439 connection_read_thread, (void *)(long)s );
1442 Debug( LDAP_DEBUG_ANY,
1443 "connection_read_activate(%d): submit failed (%d)\n",
1451 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1453 connection_read( ber_socket_t s, Operation** op )
1455 int connection_read(ber_socket_t s)
1461 assert( connections != NULL );
1463 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1465 /* get (locked) connection */
1466 c = connection_get( s );
1469 Debug( LDAP_DEBUG_ANY,
1470 "connection_read(%ld): no connection!\n",
1473 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1479 if( c->c_conn_state == SLAP_C_CLOSING ) {
1480 Debug( LDAP_DEBUG_TRACE,
1481 "connection_read(%d): closing, ignoring input for id=%lu\n",
1482 s, c->c_connid, 0 );
1484 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1485 slapd_set_read( s, 1 );
1487 connection_return( c );
1488 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1492 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1493 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1494 /* read should already be cleared */
1496 slapd_clr_read( s, 0 );
1498 ldap_pvt_thread_pool_submit( &connection_pool,
1499 c->c_clientfunc, c->c_clientarg );
1501 connection_return( c );
1502 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1506 Debug( LDAP_DEBUG_TRACE,
1507 "connection_read(%d): checking for input on id=%lu\n",
1508 s, c->c_connid, 0 );
1511 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1512 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1514 Debug( LDAP_DEBUG_TRACE,
1515 "connection_read(%d): TLS accept failure "
1516 "error=%d id=%lu, closing\n",
1517 s, rc, c->c_connid );
1519 c->c_needs_tls_accept = 0;
1520 /* connections_mutex and c_mutex are locked */
1521 connection_closing( c, "TLS negotiation failure" );
1527 /* Drain input before close, to allow SSL error codes
1528 * to propagate to client. */
1534 rc = select(s+1, &rfd, NULL, NULL, &tv);
1536 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1542 connection_close( c );
1543 connection_return( c );
1544 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1547 } else if ( rc == 0 ) {
1549 struct berval authid = BER_BVNULL;
1551 c->c_needs_tls_accept = 0;
1553 /* we need to let SASL know */
1554 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1556 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1557 if( c->c_tls_ssf > c->c_ssf ) {
1558 c->c_ssf = c->c_tls_ssf;
1561 rc = dnX509peerNormalize( ssl, &authid );
1562 if ( rc != LDAP_SUCCESS ) {
1563 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1564 "unable to get TLS client DN, error=%d id=%lu\n",
1565 s, rc, c->c_connid );
1567 Statslog( LDAP_DEBUG_STATS,
1568 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1569 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1570 slap_sasl_external( c, c->c_tls_ssf, &authid );
1571 if ( authid.bv_val ) free( authid.bv_val );
1574 /* if success and data is ready, fall thru to data input loop */
1576 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1578 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1579 slapd_set_read( s, 1 );
1582 connection_return( c );
1583 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1589 #ifdef HAVE_CYRUS_SASL
1590 if ( c->c_sasl_layers ) {
1591 /* If previous layer is not removed yet, give up for now */
1592 if ( !c->c_sasl_sockctx ) {
1593 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1594 slapd_set_read( s, 1 );
1597 connection_return( c );
1598 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1602 c->c_sasl_layers = 0;
1604 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1605 if( rc != LDAP_SUCCESS ) {
1606 Debug( LDAP_DEBUG_TRACE,
1607 "connection_read(%d): SASL install error "
1608 "error=%d id=%lu, closing\n",
1609 s, rc, c->c_connid );
1611 /* connections_mutex and c_mutex are locked */
1612 connection_closing( c, "SASL layer install failure" );
1613 connection_close( c );
1614 connection_return( c );
1615 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1621 #define CONNECTION_INPUT_LOOP 1
1622 /* #define DATA_READY_LOOP 1 */
1625 /* How do we do this without getting into a busy loop ? */
1626 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1627 rc = connection_input( c, op );
1629 rc = connection_input( c );
1632 #ifdef DATA_READY_LOOP
1633 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1634 #elif CONNECTION_INPUT_LOOP
1641 Debug( LDAP_DEBUG_TRACE,
1642 "connection_read(%d): input error=%d id=%lu, closing.\n",
1643 s, rc, c->c_connid );
1645 /* connections_mutex and c_mutex are locked */
1646 connection_closing( c, conn_lost_str );
1647 connection_close( c );
1648 connection_return( c );
1649 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1653 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1654 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1655 slapd_set_write( s, 0 );
1658 slapd_set_read( s, 1 );
1660 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1661 slapd_set_read( s, 1 );
1664 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1665 slapd_set_write( s, 1 );
1669 connection_return( c );
1670 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1676 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1677 connection_input( Connection *conn , Operation** c_op )
1679 connection_input( Connection *conn )
1688 #ifdef LDAP_CONNECTIONLESS
1694 if ( conn->c_currentber == NULL &&
1695 ( conn->c_currentber = ber_alloc()) == NULL )
1697 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1703 #ifdef LDAP_CONNECTIONLESS
1704 if ( conn->c_is_udp ) {
1705 char peername[sizeof("IP=255.255.255.255:65336")];
1707 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1708 if (len != sizeof(struct sockaddr)) return 1;
1710 sprintf( peername, "IP=%s:%d",
1711 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1712 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1713 Statslog( LDAP_DEBUG_STATS,
1714 "conn=%lu UDP request from %s (%s) accepted.\n",
1715 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1719 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1720 if ( tag != LDAP_TAG_MESSAGE ) {
1724 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1726 Debug( LDAP_DEBUG_TRACE,
1727 "ber_get_next on fd %d failed errno=%d (%s)\n",
1728 sd, err, sock_errstr(err) );
1729 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1730 /* log, close and send error */
1731 ber_free( conn->c_currentber, 1 );
1732 conn->c_currentber = NULL;
1739 ber = conn->c_currentber;
1740 conn->c_currentber = NULL;
1742 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1743 /* log, close and send error */
1744 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1749 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1750 /* log, close and send error */
1751 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1757 #ifdef LDAP_CONNECTIONLESS
1758 if( conn->c_is_udp ) {
1759 if( tag == LBER_OCTETSTRING ) {
1760 ber_get_stringa( ber, &cdn );
1761 tag = ber_peek_tag(ber, &len);
1763 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1764 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1771 if(tag == LDAP_REQ_BIND) {
1772 /* immediately abandon all existing operations upon BIND */
1773 connection_abandon( conn );
1776 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1779 /* clear state if the connection is being reused from inactive */
1780 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1781 memset( &conn->c_pagedresults_state, 0,
1782 sizeof( conn->c_pagedresults_state ) );
1785 op->o_res_ber = NULL;
1787 #ifdef LDAP_CONNECTIONLESS
1788 if (conn->c_is_udp) {
1790 ber_str2bv( cdn, 0, 1, &op->o_dn );
1791 op->o_protocol = LDAP_VERSION2;
1793 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1794 if (op->o_res_ber == NULL) return 1;
1796 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1797 sizeof(struct sockaddr), 0 );
1799 if (rc != sizeof(struct sockaddr)) {
1800 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1804 if (op->o_protocol == LDAP_VERSION2) {
1805 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1807 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1812 #endif /* LDAP_CONNECTIONLESS */
1816 /* Don't process requests when the conn is in the middle of a
1817 * Bind, or if it's closing. Also, don't let any single conn
1818 * use up all the available threads, and don't execute if we're
1819 * currently blocked on output. And don't execute if there are
1820 * already pending ops, let them go first. Abandon operations
1821 * get exceptions to some, but not all, cases.
1825 /* Abandon and Unbind are exempt from these checks */
1826 if (conn->c_conn_state == SLAP_C_CLOSING) {
1829 } else if (conn->c_writewaiter) {
1830 defer = "awaiting write";
1832 } else if (conn->c_n_ops_pending) {
1833 defer = "pending operations";
1837 case LDAP_REQ_ABANDON:
1838 /* Unbind is exempt from these checks */
1839 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1840 defer = "too many executing";
1842 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1847 case LDAP_REQ_UNBIND:
1852 int max = conn->c_dn.bv_len
1853 ? slap_conn_max_pending_auth
1854 : slap_conn_max_pending;
1856 Debug( LDAP_DEBUG_ANY,
1857 "connection_input: conn=%lu deferring operation: %s\n",
1858 conn->c_connid, defer, 0 );
1859 conn->c_n_ops_pending++;
1860 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1861 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1864 conn->c_n_ops_executing++;
1868 * The first op will be processed in the same thread context,
1869 * Subsequent ops will be submitted to the pool by
1870 * calling connection_op_activate()
1872 if ( *c_op == NULL ) {
1873 /* the first incoming request */
1874 connection_op_queue( op );
1879 connection_op_activate( op );
1884 if ( conn->c_struct_state != SLAP_C_USED ) {
1885 /* connection must have got closed underneath us */
1890 assert( conn->c_struct_state == SLAP_C_USED );
1895 connection_resched( Connection *conn )
1899 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1902 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1904 /* use trylock to avoid possible deadlock */
1905 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1908 Debug( LDAP_DEBUG_TRACE,
1909 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1910 conn->c_connid, sd, 0 );
1912 * reaquire locks in the right order...
1913 * this may allow another thread to close this connection,
1914 * so recheck state below.
1916 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1917 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1918 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1921 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1922 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1923 "closed by other thread conn=%lu sd=%d\n",
1924 conn->c_connid, sd, 0 );
1926 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1927 "attempting closing conn=%lu sd=%d\n",
1928 conn->c_connid, sd, 0 );
1929 connection_close( conn );
1932 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1936 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1937 /* other states need different handling */
1941 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1942 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1944 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1945 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1947 /* pending operations should not be marked for abandonment */
1948 assert(!op->o_abandon);
1950 conn->c_n_ops_pending--;
1951 conn->c_n_ops_executing++;
1953 connection_op_activate( op );
1955 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1961 connection_init_log_prefix( Operation *op )
1963 if ( op->o_connid == (unsigned long)(-1) ) {
1964 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1965 "conn=-1 op=%lu", op->o_opid );
1968 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1969 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1973 static int connection_bind_cb( Operation *op, SlapReply *rs )
1975 slap_callback *cb = op->o_callback;
1976 op->o_callback = cb->sc_next;
1978 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1979 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1980 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1982 return SLAP_CB_CONTINUE;
1985 static void connection_op_queue( Operation *op )
1988 ber_tag_t tag = op->o_tag;
1990 if (tag == LDAP_REQ_BIND) {
1991 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1992 sc->sc_response = connection_bind_cb;
1993 sc->sc_next = op->o_callback;
1994 op->o_callback = sc;
1995 op->o_conn->c_conn_state = SLAP_C_BINDING;
1998 if (!op->o_dn.bv_len) {
1999 op->o_authz = op->o_conn->c_authz;
2000 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2001 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2002 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2004 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2005 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2009 op->o_authtype = op->o_conn->c_authtype;
2010 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2012 if (!op->o_protocol) {
2013 op->o_protocol = op->o_conn->c_protocol
2014 ? op->o_conn->c_protocol : LDAP_VERSION3;
2017 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2018 op->o_protocol > LDAP_VERSION2)
2020 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2023 op->o_connid = op->o_conn->c_connid;
2024 connection_init_log_prefix( op );
2026 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2029 static int connection_op_activate( Operation *op )
2032 ber_tag_t tag = op->o_tag;
2034 connection_op_queue( op );
2036 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2037 connection_operation, (void *) op );
2040 Debug( LDAP_DEBUG_ANY,
2041 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2042 rc, op->o_connid, 0 );
2043 /* should move op to pending list */
2049 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2050 static int connection_write( ber_socket_t s );
2051 static void *connection_write_thread( void *ctx, void *arg )
2053 return (void *)(long)connection_write((long)arg);
2056 int connection_write_activate( ber_socket_t s )
2061 * suspend reading on this file descriptor until a connection processing
2062 * thread write data on it. Otherwise the listener thread will repeatedly
2063 * submit the same event on it to the pool.
2065 slapd_clr_write( s, 0);
2067 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2068 connection_write_thread, (void *)(long)s );
2071 Debug( LDAP_DEBUG_ANY,
2072 "connection_write_activate(%d): submit failed (%d)\n",
2080 int connection_write(ber_socket_t s)
2085 assert( connections != NULL );
2087 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2089 c = connection_get( s );
2091 Debug( LDAP_DEBUG_ANY,
2092 "connection_write(%ld): no connection!\n",
2094 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2098 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2099 slapd_clr_write( s, 0);
2103 Debug( LDAP_DEBUG_TRACE,
2104 "connection_write(%d): waking output for id=%lu\n",
2105 s, c->c_connid, 0 );
2106 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2108 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2109 slapd_set_read( s, 1 );
2111 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2112 slapd_set_write( s, 1 );
2115 /* If there are ops pending because of a writewaiter,
2118 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2119 if ( !c->c_writewaiter ) break;
2120 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2122 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2123 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2125 /* pending operations should not be marked for abandonment */
2126 assert(!op->o_abandon);
2128 c->c_n_ops_pending--;
2129 c->c_n_ops_executing++;
2131 connection_op_activate( op );
2136 connection_return( c );
2137 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2142 connection_fake_init(
2147 conn->c_connid = -1;
2148 conn->c_send_ldap_result = slap_send_ldap_result;
2149 conn->c_send_search_entry = slap_send_search_entry;
2150 conn->c_send_search_reference = slap_send_search_reference;
2151 conn->c_listener = (Listener *)&dummy_list;
2152 conn->c_peer_domain = slap_empty_bv;
2153 conn->c_peer_name = slap_empty_bv;
2155 memset(op, 0, OPERATION_BUFFER_SIZE);
2156 op->o_hdr = (Opheader *)(op+1);
2157 op->o_controls = (void **)(op->o_hdr+1);
2158 /* set memory context */
2159 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2160 op->o_tmpmfuncs = &slap_sl_mfuncs;
2161 op->o_threadctx = ctx;
2164 op->o_connid = op->o_conn->c_connid;
2165 connection_init_log_prefix( op );
2167 slap_op_time( &op->o_time, &op->o_tincr );
2171 connection_assign_nextid( Connection *conn )
2173 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2174 conn->c_connid = conn_nextid++;
2175 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );