3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED 0x01
30 #define SLAP_C_USED 0x02
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE 0x01 /* zero threads */
35 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
36 #define SLAP_C_BINDING 0x03 /* binding */
37 #define SLAP_C_CLOSING 0x04 /* closing */
40 connection_state2str( int state )
43 case SLAP_C_INVALID: return "!";
44 case SLAP_C_INACTIVE: return "|";
45 case SLAP_C_ACTIVE: return "";
46 case SLAP_C_BINDING: return "B";
47 case SLAP_C_CLOSING: return "C";
53 static Connection* connection_get( ber_socket_t s );
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
58 static int connection_op_activate( Connection *conn, Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
69 * Initialize connection management infrastructure.
71 int connections_init(void)
73 assert( connections == NULL );
75 if( connections != NULL) {
76 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
81 /* should check return of every call */
82 ldap_pvt_thread_mutex_init( &connections_mutex );
84 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
86 if( connections == NULL ) {
87 Debug( LDAP_DEBUG_ANY,
88 "connections_init: allocation (%d*%ld) of connection array failed\n",
89 dtblsize, (long) sizeof(Connection), 0 );
93 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
94 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
97 * per entry initialization of the Connection array initialization
98 * will be done by connection_init()
105 * Destroy connection management infrastructure.
107 int connections_destroy(void)
111 /* should check return of every call */
113 if( connections == NULL) {
114 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
119 for ( i = 0; i < dtblsize; i++ ) {
120 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
121 ber_sockbuf_free( connections[i].c_sb );
122 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
123 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
124 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
131 ldap_pvt_thread_mutex_destroy( &connections_mutex );
136 * shutdown all connections
138 int connections_shutdown(void)
142 ldap_pvt_thread_mutex_lock( &connections_mutex );
144 for ( i = 0; i < dtblsize; i++ ) {
145 if( connections[i].c_struct_state != SLAP_C_USED ) {
149 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
151 /* connections_mutex and c_mutex are locked */
152 connection_closing( &connections[i] );
153 connection_close( &connections[i] );
155 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
158 ldap_pvt_thread_mutex_unlock( &connections_mutex );
164 * Timeout idle connections.
166 int connections_timeout_idle(time_t now)
172 for( c = connection_first( &connindex );
174 c = connection_next( c, &connindex ) )
176 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
178 connection_closing( c );
179 connection_close( c );
183 connection_done( c );
188 static Connection* connection_get( ber_socket_t s )
190 /* connections_mutex should be locked by caller */
194 Debug( LDAP_DEBUG_ARGS,
195 "connection_get(%ld)\n",
198 assert( connections != NULL );
200 if(s == AC_SOCKET_INVALID) {
207 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
214 for(i=0; i<dtblsize; i++) {
215 ber_sockbuf_ctrl( connections[i].c_sb,
216 LBER_SB_OPT_GET_FD, &sd );
218 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
219 assert( connections[i].c_conn_state == SLAP_C_INVALID );
220 assert( connections[i].c_sb == 0 );
224 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
225 assert( connections[i].c_conn_state == SLAP_C_INVALID );
226 assert( sd == AC_SOCKET_INVALID );
230 /* state can actually change from used -> unused by resched,
231 * so don't assert details here.
245 ldap_pvt_thread_mutex_lock( &c->c_mutex );
247 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
248 if( c->c_struct_state != SLAP_C_USED ) {
249 /* connection must have been closed due to resched */
251 assert( c->c_conn_state == SLAP_C_INVALID );
252 assert( sd == AC_SOCKET_INVALID );
254 Debug( LDAP_DEBUG_TRACE,
255 "connection_get(%d): connection not used\n",
258 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
262 Debug( LDAP_DEBUG_TRACE,
263 "connection_get(%d): got connid=%ld\n",
268 assert( c->c_struct_state == SLAP_C_USED );
269 assert( c->c_conn_state != SLAP_C_INVALID );
270 assert( sd != AC_SOCKET_INVALID );
272 c->c_activitytime = slap_get_time();
278 static void connection_return( Connection *c )
280 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
283 long connection_init(
287 const char* peername,
288 const char* sockname,
296 assert( connections != NULL );
298 assert( dnsname != NULL );
299 assert( peername != NULL );
300 assert( sockname != NULL );
306 if( s == AC_SOCKET_INVALID ) {
307 Debug( LDAP_DEBUG_ANY,
308 "connection_init(%ld): invalid.\n",
315 assert( s < dtblsize );
318 ldap_pvt_thread_mutex_lock( &connections_mutex );
329 for( i=0; i < dtblsize; i++) {
332 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
333 assert( connections[i].c_sb == 0 );
338 sd = AC_SOCKET_INVALID;
339 if (connections[i].c_sb != NULL)
340 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
342 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
343 assert( sd == AC_SOCKET_INVALID );
348 assert( connections[i].c_struct_state == SLAP_C_USED );
349 assert( connections[i].c_conn_state != SLAP_C_INVALID );
350 assert( sd != AC_SOCKET_INVALID );
354 Debug( LDAP_DEBUG_ANY,
355 "connection_init(%d): connection table full (%d/%d)\n",
357 ldap_pvt_thread_mutex_unlock( &connections_mutex );
365 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
366 c->c_authmech = NULL;
370 c->c_listener_url = NULL;
371 c->c_peer_domain = NULL;
372 c->c_peer_name = NULL;
373 c->c_sock_name = NULL;
376 c->c_pending_ops = NULL;
378 c->c_sasl_bind_mech = NULL;
379 c->c_sasl_context = NULL;
380 c->c_sasl_extra = NULL;
382 c->c_sb = ber_sockbuf_alloc( );
383 c->c_currentber = NULL;
385 /* should check status of thread calls */
386 ldap_pvt_thread_mutex_init( &c->c_mutex );
387 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
388 ldap_pvt_thread_cond_init( &c->c_write_cv );
390 c->c_struct_state = SLAP_C_UNUSED;
393 ldap_pvt_thread_mutex_lock( &c->c_mutex );
395 assert( c->c_struct_state == SLAP_C_UNUSED );
396 assert( c->c_authmech == NULL );
397 assert( c->c_dn == NULL );
398 assert( c->c_cdn == NULL );
399 assert( c->c_listener_url == NULL );
400 assert( c->c_peer_domain == NULL );
401 assert( c->c_peer_name == NULL );
402 assert( c->c_sock_name == NULL );
403 assert( c->c_ops == NULL );
404 assert( c->c_pending_ops == NULL );
405 assert( c->c_sasl_bind_mech == NULL );
406 assert( c->c_sasl_context == NULL );
407 assert( c->c_sasl_extra == NULL );
408 assert( c->c_currentber == NULL );
410 c->c_listener_url = ch_strdup( url );
411 c->c_peer_domain = ch_strdup( dnsname );
412 c->c_peer_name = ch_strdup( peername );
413 c->c_sock_name = ch_strdup( sockname );
415 c->c_n_ops_received = 0;
416 c->c_n_ops_executing = 0;
417 c->c_n_ops_pending = 0;
418 c->c_n_ops_completed = 0;
424 /* set to zero until bind, implies LDAP_VERSION3 */
427 c->c_activitytime = c->c_starttime = slap_get_time();
430 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
431 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
433 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
434 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
435 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
436 LBER_SBIOD_LEVEL_PROVIDER, NULL );
439 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
440 INT_MAX, (void*)"ldap_" );
443 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
444 c /* non-NULL */ ) < 0 )
446 Debug( LDAP_DEBUG_ANY,
447 "connection_init(%d, %s): set nonblocking failed\n",
448 s, c->c_peer_name, 0 );
451 id = c->c_connid = conn_nextid++;
453 c->c_conn_state = SLAP_C_INACTIVE;
454 c->c_struct_state = SLAP_C_USED;
456 c->c_ssf = c->c_transport_ssf = ssf;
462 c->c_needs_tls_accept = 1;
465 c->c_needs_tls_accept = 0;
470 slap_sasl_external( c, ssf, authid );
472 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
473 ldap_pvt_thread_mutex_unlock( &connections_mutex );
475 backend_connection_init(c);
480 void connection2anonymous( Connection *c )
482 assert( connections != NULL );
485 if(c->c_authmech != NULL ) {
487 c->c_authmech = NULL;
490 if(c->c_dn != NULL) {
495 if(c->c_cdn != NULL) {
500 c->c_authc_backend = NULL;
501 c->c_authz_backend = NULL;
505 connection_destroy( Connection *c )
507 /* note: connections_mutex should be locked by caller */
510 assert( connections != NULL );
512 assert( c->c_struct_state != SLAP_C_UNUSED );
513 assert( c->c_conn_state != SLAP_C_INVALID );
514 assert( c->c_ops == NULL );
516 backend_connection_destroy(c);
521 c->c_activitytime = c->c_starttime = 0;
523 connection2anonymous( c );
525 if(c->c_listener_url != NULL) {
526 free(c->c_listener_url);
527 c->c_listener_url = NULL;
530 if(c->c_peer_domain != NULL) {
531 free(c->c_peer_domain);
532 c->c_peer_domain = NULL;
534 if(c->c_peer_name != NULL) {
537 * If peer was a domain socket, unlink. Mind you,
538 * they may be un-named. Should we leave this to
541 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
542 char *path = c->c_peer_name + 5;
547 #endif /* LDAP_PF_LOCAL */
549 free(c->c_peer_name);
550 c->c_peer_name = NULL;
552 if(c->c_sock_name != NULL) {
553 free(c->c_sock_name);
554 c->c_sock_name = NULL;
557 c->c_sasl_bind_in_progress = 0;
558 if(c->c_sasl_bind_mech != NULL) {
559 free(c->c_sasl_bind_mech);
560 c->c_sasl_bind_mech = NULL;
563 slap_sasl_close( c );
565 if ( c->c_currentber != NULL ) {
566 ber_free( c->c_currentber, 1 );
567 c->c_currentber = NULL;
570 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
571 if ( sd != AC_SOCKET_INVALID ) {
572 slapd_remove( sd, 0 );
574 Statslog( LDAP_DEBUG_STATS,
575 "conn=%ld fd=%d closed\n",
576 c->c_connid, sd, 0, 0, 0 );
579 ber_sockbuf_free( c->c_sb );
580 c->c_sb = ber_sockbuf_alloc( );
582 c->c_conn_state = SLAP_C_INVALID;
583 c->c_struct_state = SLAP_C_UNUSED;
586 int connection_state_closing( Connection *c )
588 /* c_mutex must be locked by caller */
592 assert( c->c_struct_state == SLAP_C_USED );
594 state = c->c_conn_state;
596 assert( state != SLAP_C_INVALID );
598 return state == SLAP_C_CLOSING;
601 static void connection_abandon( Connection *c )
603 /* c_mutex must be locked by caller */
607 for( o = c->c_ops; o != NULL; o = o->o_next ) {
608 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
610 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
613 /* remove pending operations */
614 for( o = slap_op_pop( &c->c_pending_ops );
616 o = slap_op_pop( &c->c_pending_ops ) )
622 void connection_closing( Connection *c )
624 assert( connections != NULL );
626 assert( c->c_struct_state == SLAP_C_USED );
627 assert( c->c_conn_state != SLAP_C_INVALID );
629 /* c_mutex must be locked by caller */
631 if( c->c_conn_state != SLAP_C_CLOSING ) {
634 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
635 Debug( LDAP_DEBUG_TRACE,
636 "connection_closing: readying conn=%ld sd=%d for close\n",
637 c->c_connid, sd, 0 );
639 /* update state to closing */
640 c->c_conn_state = SLAP_C_CLOSING;
642 /* don't listen on this port anymore */
643 slapd_clr_read( sd, 1 );
645 /* abandon active operations */
646 connection_abandon( c );
648 /* wake write blocked operations */
649 slapd_clr_write( sd, 1 );
650 ldap_pvt_thread_cond_signal( &c->c_write_cv );
654 static void connection_close( Connection *c )
658 assert( connections != NULL );
660 assert( c->c_struct_state == SLAP_C_USED );
661 assert( c->c_conn_state == SLAP_C_CLOSING );
663 /* note: connections_mutex and c_mutex should be locked by caller */
665 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
666 if( c->c_ops != NULL ) {
667 Debug( LDAP_DEBUG_TRACE,
668 "connection_close: deferring conn=%ld sd=%d\n",
669 c->c_connid, sd, 0 );
674 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
675 c->c_connid, sd, 0 );
677 connection_destroy( c );
680 unsigned long connections_nextid(void)
683 assert( connections != NULL );
685 ldap_pvt_thread_mutex_lock( &connections_mutex );
689 ldap_pvt_thread_mutex_unlock( &connections_mutex );
694 Connection* connection_first( ber_socket_t *index )
696 assert( connections != NULL );
697 assert( index != NULL );
699 ldap_pvt_thread_mutex_lock( &connections_mutex );
703 return connection_next(NULL, index);
706 Connection* connection_next( Connection *c, ber_socket_t *index )
708 assert( connections != NULL );
709 assert( index != NULL );
710 assert( *index <= dtblsize );
713 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
718 for(; *index < dtblsize; (*index)++) {
719 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
720 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
728 if( connections[*index].c_struct_state == SLAP_C_USED ) {
729 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
730 c = &connections[(*index)++];
734 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
735 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
739 ldap_pvt_thread_mutex_lock( &c->c_mutex );
745 void connection_done( Connection *c )
747 assert( connections != NULL );
750 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
753 ldap_pvt_thread_mutex_unlock( &connections_mutex );
757 * connection_activity - handle the request operation op on connection
758 * conn. This routine figures out what kind of operation it is and
759 * calls the appropriate stub to handle it.
763 connection_operation( void *arg_v )
766 struct co_arg *arg = arg_v;
767 ber_tag_t tag = arg->co_op->o_tag;
768 Connection *conn = arg->co_conn;
770 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
772 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
774 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
775 Debug( LDAP_DEBUG_ANY, "connection_operation: "
776 "error: SASL bind in progress (tag=%ld).\n",
778 send_ldap_result( conn, arg->co_op,
779 rc = LDAP_OPERATIONS_ERROR,
780 NULL, "SASL bind in progress", NULL, NULL );
781 goto operations_error;
786 rc = do_bind( conn, arg->co_op );
789 case LDAP_REQ_UNBIND:
790 rc = do_unbind( conn, arg->co_op );
794 rc = do_add( conn, arg->co_op );
797 case LDAP_REQ_DELETE:
798 rc = do_delete( conn, arg->co_op );
801 case LDAP_REQ_MODRDN:
802 rc = do_modrdn( conn, arg->co_op );
805 case LDAP_REQ_MODIFY:
806 rc = do_modify( conn, arg->co_op );
809 case LDAP_REQ_COMPARE:
810 rc = do_compare( conn, arg->co_op );
813 case LDAP_REQ_SEARCH:
814 rc = do_search( conn, arg->co_op );
817 case LDAP_REQ_ABANDON:
818 rc = do_abandon( conn, arg->co_op );
821 case LDAP_REQ_EXTENDED:
822 rc = do_extended( conn, arg->co_op );
826 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
828 arg->co_op->o_tag = LBER_ERROR;
829 send_ldap_disconnect( conn, arg->co_op,
830 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
835 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
838 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
840 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
842 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
844 conn->c_n_ops_executing--;
845 conn->c_n_ops_completed++;
847 slap_op_remove( &conn->c_ops, arg->co_op );
848 slap_op_free( arg->co_op );
851 free( (char *) arg );
856 case LDAP_REQ_UNBIND:
857 /* c_mutex is locked */
858 connection_closing( conn );
862 conn->c_sasl_bind_in_progress =
863 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
865 if( conn->c_conn_state == SLAP_C_BINDING) {
866 conn->c_conn_state = SLAP_C_ACTIVE;
870 connection_resched( conn );
872 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
877 int connection_read(ber_socket_t s)
881 assert( connections != NULL );
883 ldap_pvt_thread_mutex_lock( &connections_mutex );
885 /* get (locked) connection */
886 c = connection_get( s );
889 Debug( LDAP_DEBUG_ANY,
890 "connection_read(%ld): no connection!\n",
895 ldap_pvt_thread_mutex_unlock( &connections_mutex );
901 if( c->c_conn_state == SLAP_C_CLOSING ) {
902 Debug( LDAP_DEBUG_TRACE,
903 "connection_read(%d): closing, ignoring input for id=%ld\n",
906 connection_return( c );
907 ldap_pvt_thread_mutex_unlock( &connections_mutex );
911 Debug( LDAP_DEBUG_TRACE,
912 "connection_read(%d): checking for input on id=%ld\n",
916 if ( c->c_is_tls && c->c_needs_tls_accept ) {
917 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
922 Debug( LDAP_DEBUG_TRACE,
923 "connection_read(%d): TLS accept error "
924 "error=%d id=%ld, closing\n",
925 s, rc, c->c_connid );
927 c->c_needs_tls_accept = 0;
928 /* connections_mutex and c_mutex are locked */
929 connection_closing( c );
932 /* Drain input before close, to allow SSL error codes
933 * to propagate to client. */
940 rc = select(s+1, &rfd, NULL, NULL, &tv);
942 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
946 connection_close( c );
948 } else if ( rc == 0 ) {
952 c->c_needs_tls_accept = 0;
954 /* we need to let SASL know */
955 ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
957 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
958 if( c->c_tls_ssf > c->c_ssf ) {
959 c->c_ssf = c->c_tls_ssf;
962 authid = (char *)ldap_pvt_tls_get_peer( ssl );
963 slap_sasl_external( c, c->c_tls_ssf, authid );
965 connection_return( c );
966 ldap_pvt_thread_mutex_unlock( &connections_mutex );
971 #ifdef HAVE_CYRUS_SASL
972 if ( c->c_sasl_layers ) {
973 c->c_sasl_layers = 0;
975 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
977 if( rc != LDAP_SUCCESS ) {
978 Debug( LDAP_DEBUG_TRACE,
979 "connection_read(%d): SASL install error "
980 "error=%d id=%ld, closing\n",
981 s, rc, c->c_connid );
983 /* connections_mutex and c_mutex are locked */
984 connection_closing( c );
985 connection_close( c );
986 connection_return( c );
987 ldap_pvt_thread_mutex_unlock( &connections_mutex );
993 #define CONNECTION_INPUT_LOOP 1
995 #ifdef DATA_READY_LOOP
996 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
997 #elif CONNECTION_INPUT_LOOP
1001 /* How do we do this without getting into a busy loop ? */
1002 rc = connection_input( c );
1006 Debug( LDAP_DEBUG_TRACE,
1007 "connection_read(%d): input error=%d id=%ld, closing.\n",
1008 s, rc, c->c_connid );
1010 /* connections_mutex and c_mutex are locked */
1011 connection_closing( c );
1012 connection_close( c );
1013 connection_return( c );
1014 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1018 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1019 slapd_set_read( s, 1 );
1022 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1023 slapd_set_write( s, 1 );
1026 connection_return( c );
1027 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1042 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1044 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1050 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1051 if ( tag != LDAP_TAG_MESSAGE ) {
1055 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1057 Debug( LDAP_DEBUG_TRACE,
1058 "ber_get_next on fd %d failed errno=%d (%s)\n",
1059 sd, err, sock_errstr(err) );
1061 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1062 /* log, close and send error */
1063 ber_free( conn->c_currentber, 1 );
1064 conn->c_currentber = NULL;
1071 ber = conn->c_currentber;
1072 conn->c_currentber = NULL;
1074 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1075 /* log, close and send error */
1076 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1082 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1083 /* log, close and send error */
1084 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1091 if(tag == LDAP_REQ_BIND) {
1092 /* immediately abandon all exiting operations upon BIND */
1093 connection_abandon( conn );
1096 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1098 if ( conn->c_conn_state == SLAP_C_BINDING
1099 || conn->c_conn_state == SLAP_C_CLOSING )
1101 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1102 conn->c_n_ops_pending++;
1103 slap_op_add( &conn->c_pending_ops, op );
1106 conn->c_n_ops_executing++;
1107 connection_op_activate( conn, op );
1111 if ( conn->c_struct_state != SLAP_C_USED ) {
1112 /* connection must have got closed underneath us */
1116 assert( conn->c_struct_state == SLAP_C_USED );
1122 connection_resched( Connection *conn )
1126 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1129 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1131 /* us trylock to avoid possible deadlock */
1132 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1135 Debug( LDAP_DEBUG_TRACE,
1136 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1137 conn->c_connid, sd, 0 );
1139 * reaquire locks in the right order...
1140 * this may allow another thread to close this connection,
1141 * so recheck state below.
1143 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1144 ldap_pvt_thread_mutex_lock( &connections_mutex );
1145 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1148 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1149 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1150 "closed by other thread conn=%ld sd=%d\n",
1151 conn->c_connid, sd, 0 );
1154 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1155 "attempting closing conn=%ld sd=%d\n",
1156 conn->c_connid, sd, 0 );
1158 connection_close( conn );
1161 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1165 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1166 /* other states need different handling */
1170 for( op = slap_op_pop( &conn->c_pending_ops );
1172 op = slap_op_pop( &conn->c_pending_ops ) )
1174 /* pending operations should not be marked for abandonment */
1175 assert(!op->o_abandon);
1177 conn->c_n_ops_pending--;
1178 conn->c_n_ops_executing++;
1180 connection_op_activate( conn, op );
1182 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1189 static int connection_op_activate( Connection *conn, Operation *op )
1193 ber_tag_t tag = op->o_tag;
1195 if(tag == LDAP_REQ_BIND) {
1196 conn->c_conn_state = SLAP_C_BINDING;
1199 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1200 arg->co_conn = conn;
1203 arg->co_op->o_authz = conn->c_authz;
1204 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1205 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1206 (void) dn_normalize( arg->co_op->o_ndn );
1207 arg->co_op->o_authtype = conn->c_authtype;
1208 arg->co_op->o_authmech = conn->c_authmech != NULL
1209 ? ch_strdup( conn->c_authmech ) : NULL;
1211 arg->co_op->o_protocol = conn->c_protocol
1212 ? conn->c_protocol : LDAP_VERSION3;
1213 arg->co_op->o_connid = conn->c_connid;
1215 slap_op_add( &conn->c_ops, arg->co_op );
1217 status = ldap_pvt_thread_pool_submit( &connection_pool,
1218 connection_operation, (void *) arg );
1220 if ( status != 0 ) {
1221 Debug( LDAP_DEBUG_ANY,
1222 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1223 /* should move op to pending list */
1229 int connection_write(ber_socket_t s)
1232 assert( connections != NULL );
1234 ldap_pvt_thread_mutex_lock( &connections_mutex );
1236 c = connection_get( s );
1238 slapd_clr_write( s, 0);
1241 Debug( LDAP_DEBUG_ANY,
1242 "connection_write(%ld): no connection!\n",
1245 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1251 Debug( LDAP_DEBUG_TRACE,
1252 "connection_write(%d): waking output for id=%ld\n",
1253 s, c->c_connid, 0 );
1255 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1257 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1258 slapd_set_read( s, 1 );
1259 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1260 slapd_set_write( s, 1 );
1261 connection_return( c );
1262 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1268 * Create client side and server side connection structures, connected to
1269 * one another, for the front end to use for searches on arbitrary back ends.
1272 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1275 ber_socket_t fd[2] = {-1,-1};
1281 rc = lutil_pair( fd );
1286 rc = connection_init( fd[1], "INT", "localhost",
1287 "localhost:0", "localhost:00", 0, 256, id );
1293 slapd_add_internal( fd[1] );
1295 /* A search operation, number 0 */
1296 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1297 op->o_ndn = ch_strdup( id );
1298 op->o_protocol = LDAP_VERSION3;
1300 (*conn) = connection_get( fd[1] );
1301 (*conn)->c_ops = op;
1302 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1305 /* Create the client side of the connection */
1306 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1307 if( rc != LDAP_SUCCESS ) {
1312 /* The connection_get() will have locked the connection's mutex */
1313 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1315 return LDAP_SUCCESS;
1319 void connection_internal_close( Connection *conn )
1321 connection_closing( conn );
1322 connection_close( conn );