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>
17 #include <ac/unistd.h>
23 /* protected by connections_mutex */
24 static ldap_pvt_thread_mutex_t connections_mutex;
25 static Connection *connections = NULL;
26 static unsigned long conn_nextid = 0;
28 /* structure state (protected by connections_mutex) */
29 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
30 #define SLAP_C_UNUSED 0x01
31 #define SLAP_C_USED 0x02
33 /* connection state (protected by c_mutex ) */
34 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
35 #define SLAP_C_INACTIVE 0x01 /* zero threads */
36 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
37 #define SLAP_C_BINDING 0x03 /* binding */
38 #define SLAP_C_CLOSING 0x04 /* closing */
41 connection_state2str( int state )
44 case SLAP_C_INVALID: return "!";
45 case SLAP_C_INACTIVE: return "|";
46 case SLAP_C_ACTIVE: return "";
47 case SLAP_C_BINDING: return "B";
48 case SLAP_C_CLOSING: return "C";
54 static Connection* connection_get( ber_socket_t s );
56 static int connection_input( Connection *c );
57 static void connection_close( Connection *c );
59 static int connection_op_activate( Connection *conn, Operation *op );
60 static int connection_resched( Connection *conn );
61 static void connection_abandon( Connection *conn );
62 static void connection_destroy( Connection *c );
70 * Initialize connection management infrastructure.
72 int connections_init(void)
74 assert( connections == NULL );
76 if( connections != NULL) {
78 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
79 "connections_init: already initialized.\n" ));
81 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
87 /* should check return of every call */
88 ldap_pvt_thread_mutex_init( &connections_mutex );
90 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
92 if( connections == NULL ) {
94 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
95 "connections_init: allocation (%d * %ld) of connection array failed\n",
96 dtblsize, (long) sizeof(Connection) ));
98 Debug( LDAP_DEBUG_ANY,
99 "connections_init: allocation (%d*%ld) of connection array failed\n",
100 dtblsize, (long) sizeof(Connection), 0 );
105 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
106 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
109 * per entry initialization of the Connection array initialization
110 * will be done by connection_init()
117 * Destroy connection management infrastructure.
119 int connections_destroy(void)
123 /* should check return of every call */
125 if( connections == NULL) {
127 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
128 "connections_destroy: nothing to destroy.\n"));
130 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
136 for ( i = 0; i < dtblsize; i++ ) {
137 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
138 ber_sockbuf_free( connections[i].c_sb );
139 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
140 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
141 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
148 ldap_pvt_thread_mutex_destroy( &connections_mutex );
153 * shutdown all connections
155 int connections_shutdown(void)
159 ldap_pvt_thread_mutex_lock( &connections_mutex );
161 for ( i = 0; i < dtblsize; i++ ) {
162 if( connections[i].c_struct_state != SLAP_C_USED ) {
166 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
168 /* connections_mutex and c_mutex are locked */
169 connection_closing( &connections[i] );
170 connection_close( &connections[i] );
172 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
175 ldap_pvt_thread_mutex_unlock( &connections_mutex );
181 * Timeout idle connections.
183 int connections_timeout_idle(time_t now)
189 for( c = connection_first( &connindex );
191 c = connection_next( c, &connindex ) )
193 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
195 connection_closing( c );
196 connection_close( c );
200 connection_done( c );
205 static Connection* connection_get( ber_socket_t s )
207 /* connections_mutex should be locked by caller */
212 LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
213 "connection_get: socket %ld\n", (long)s ));
215 Debug( LDAP_DEBUG_ARGS,
216 "connection_get(%ld)\n",
220 assert( connections != NULL );
222 if(s == AC_SOCKET_INVALID) {
229 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
236 for(i=0; i<dtblsize; i++) {
237 ber_sockbuf_ctrl( connections[i].c_sb,
238 LBER_SB_OPT_GET_FD, &sd );
240 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
241 assert( connections[i].c_conn_state == SLAP_C_INVALID );
242 assert( connections[i].c_sb == 0 );
246 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
247 assert( connections[i].c_conn_state == SLAP_C_INVALID );
248 assert( sd == AC_SOCKET_INVALID );
252 /* state can actually change from used -> unused by resched,
253 * so don't assert details here.
267 ldap_pvt_thread_mutex_lock( &c->c_mutex );
269 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
270 if( c->c_struct_state != SLAP_C_USED ) {
271 /* connection must have been closed due to resched */
273 assert( c->c_conn_state == SLAP_C_INVALID );
274 assert( sd == AC_SOCKET_INVALID );
277 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
278 "connection_get: connection %d not used\n", s ));
280 Debug( LDAP_DEBUG_TRACE,
281 "connection_get(%d): connection not used\n",
285 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
290 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
291 "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
293 Debug( LDAP_DEBUG_TRACE,
294 "connection_get(%d): got connid=%ld\n",
300 assert( c->c_struct_state == SLAP_C_USED );
301 assert( c->c_conn_state != SLAP_C_INVALID );
302 assert( sd != AC_SOCKET_INVALID );
305 c->c_activitytime = slap_get_time();
307 if( global_idletimeout > 0 ) {
308 c->c_activitytime = slap_get_time();
316 static void connection_return( Connection *c )
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
321 long connection_init(
325 const char* peername,
326 const char* sockname,
334 assert( connections != NULL );
336 assert( dnsname != NULL );
337 assert( peername != NULL );
338 assert( sockname != NULL );
341 assert( tls_udp_option != 1 );
344 if( s == AC_SOCKET_INVALID ) {
346 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
347 "connection_init: init of socket %ld invalid.\n", (long)s ));
349 Debug( LDAP_DEBUG_ANY,
350 "connection_init(%ld): invalid.\n",
358 assert( s < dtblsize );
361 ldap_pvt_thread_mutex_lock( &connections_mutex );
372 for( i=0; i < dtblsize; i++) {
375 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
376 assert( connections[i].c_sb == 0 );
381 sd = AC_SOCKET_INVALID;
382 if (connections[i].c_sb != NULL)
383 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
385 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
386 assert( sd == AC_SOCKET_INVALID );
391 assert( connections[i].c_struct_state == SLAP_C_USED );
392 assert( connections[i].c_conn_state != SLAP_C_INVALID );
393 assert( sd != AC_SOCKET_INVALID );
398 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
399 "connection_init: skt %d connection table full (%d/%d)\n",
402 Debug( LDAP_DEBUG_ANY,
403 "connection_init(%d): connection table full (%d/%d)\n",
406 ldap_pvt_thread_mutex_unlock( &connections_mutex );
414 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
415 c->c_authmech = NULL;
416 c->c_dn.bv_val = NULL;
418 c->c_ndn.bv_val = NULL;
423 c->c_listener_url = NULL;
424 c->c_peer_domain = NULL;
425 c->c_peer_name = NULL;
426 c->c_sock_name = NULL;
429 c->c_pending_ops = NULL;
431 c->c_sasl_bind_mech = NULL;
432 c->c_sasl_context = NULL;
433 c->c_sasl_extra = NULL;
435 c->c_sb = ber_sockbuf_alloc( );
438 ber_len_t max = sockbuf_max_incoming;
439 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
442 c->c_currentber = NULL;
444 /* should check status of thread calls */
445 ldap_pvt_thread_mutex_init( &c->c_mutex );
446 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
447 ldap_pvt_thread_cond_init( &c->c_write_cv );
449 c->c_struct_state = SLAP_C_UNUSED;
452 ldap_pvt_thread_mutex_lock( &c->c_mutex );
454 assert( c->c_struct_state == SLAP_C_UNUSED );
455 assert( c->c_authmech == NULL );
456 assert( c->c_dn.bv_val == NULL );
457 assert( c->c_ndn.bv_val == NULL );
458 assert( c->c_cdn == NULL );
459 assert( c->c_groups == NULL );
460 assert( c->c_listener_url == NULL );
461 assert( c->c_peer_domain == NULL );
462 assert( c->c_peer_name == NULL );
463 assert( c->c_sock_name == NULL );
464 assert( c->c_ops == NULL );
465 assert( c->c_pending_ops == NULL );
466 assert( c->c_sasl_bind_mech == NULL );
467 assert( c->c_sasl_context == NULL );
468 assert( c->c_sasl_extra == NULL );
469 assert( c->c_currentber == NULL );
471 c->c_listener_url = ch_strdup( url );
472 c->c_peer_domain = ch_strdup( dnsname );
473 c->c_peer_name = ch_strdup( peername );
474 c->c_sock_name = ch_strdup( sockname );
476 c->c_n_ops_received = 0;
477 c->c_n_ops_executing = 0;
478 c->c_n_ops_pending = 0;
479 c->c_n_ops_completed = 0;
485 /* set to zero until bind, implies LDAP_VERSION3 */
489 c->c_activitytime = c->c_starttime = slap_get_time();
491 if( global_idletimeout > 0 ) {
492 c->c_activitytime = c->c_starttime = slap_get_time();
496 #ifdef LDAP_CONNECTIONLESS
498 if (tls_udp_option == 2)
502 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
503 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
505 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
506 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
511 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
512 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
514 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
515 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
517 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
518 LBER_SBIOD_LEVEL_PROVIDER, NULL );
521 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
522 INT_MAX, (void*)"ldap_" );
525 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
526 c /* non-NULL */ ) < 0 )
529 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
530 "connection_init: conn %d set nonblocking failed\n",
533 Debug( LDAP_DEBUG_ANY,
534 "connection_init(%d, %s): set nonblocking failed\n",
535 s, c->c_peer_name, 0 );
539 id = c->c_connid = conn_nextid++;
541 c->c_conn_state = SLAP_C_INACTIVE;
542 c->c_struct_state = SLAP_C_USED;
544 c->c_ssf = c->c_transport_ssf = ssf;
548 if ( tls_udp_option == 1 ) {
550 c->c_needs_tls_accept = 1;
553 c->c_needs_tls_accept = 0;
558 slap_sasl_external( c, ssf, authid );
560 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
561 ldap_pvt_thread_mutex_unlock( &connections_mutex );
563 backend_connection_init(c);
568 void connection2anonymous( Connection *c )
570 assert( connections != NULL );
574 ber_len_t max = sockbuf_max_incoming;
575 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
578 if(c->c_authmech != NULL ) {
580 c->c_authmech = NULL;
583 if(c->c_dn.bv_val != NULL) {
584 free(c->c_dn.bv_val);
585 c->c_dn.bv_val = NULL;
588 if(c->c_ndn.bv_val != NULL) {
589 free(c->c_ndn.bv_val);
590 c->c_ndn.bv_val = NULL;
594 if(c->c_cdn != NULL) {
599 c->c_authc_backend = NULL;
600 c->c_authz_backend = NULL;
603 GroupAssertion *g, *n;
604 for (g = c->c_groups; g; g=n)
615 connection_destroy( Connection *c )
617 /* note: connections_mutex should be locked by caller */
620 assert( connections != NULL );
622 assert( c->c_struct_state != SLAP_C_UNUSED );
623 assert( c->c_conn_state != SLAP_C_INVALID );
624 assert( c->c_ops == NULL );
626 backend_connection_destroy(c);
631 c->c_activitytime = c->c_starttime = 0;
633 connection2anonymous( c );
635 if(c->c_listener_url != NULL) {
636 free(c->c_listener_url);
637 c->c_listener_url = NULL;
640 if(c->c_peer_domain != NULL) {
641 free(c->c_peer_domain);
642 c->c_peer_domain = NULL;
644 if(c->c_peer_name != NULL) {
647 * If peer was a domain socket, unlink. Mind you,
648 * they may be un-named. Should we leave this to
651 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
652 char *path = c->c_peer_name + 5;
657 #endif /* LDAP_PF_LOCAL */
659 free(c->c_peer_name);
660 c->c_peer_name = NULL;
662 if(c->c_sock_name != NULL) {
663 free(c->c_sock_name);
664 c->c_sock_name = NULL;
667 c->c_sasl_bind_in_progress = 0;
668 if(c->c_sasl_bind_mech != NULL) {
669 free(c->c_sasl_bind_mech);
670 c->c_sasl_bind_mech = NULL;
673 slap_sasl_close( c );
675 if ( c->c_currentber != NULL ) {
676 ber_free( c->c_currentber, 1 );
677 c->c_currentber = NULL;
680 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
681 if ( sd != AC_SOCKET_INVALID ) {
682 slapd_remove( sd, 0 );
684 Statslog( LDAP_DEBUG_STATS,
685 "conn=%ld fd=%d closed\n",
686 c->c_connid, sd, 0, 0, 0 );
689 ber_sockbuf_free( c->c_sb );
691 c->c_sb = ber_sockbuf_alloc( );
694 ber_len_t max = sockbuf_max_incoming;
695 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
698 c->c_conn_state = SLAP_C_INVALID;
699 c->c_struct_state = SLAP_C_UNUSED;
702 int connection_state_closing( Connection *c )
704 /* c_mutex must be locked by caller */
708 assert( c->c_struct_state == SLAP_C_USED );
710 state = c->c_conn_state;
712 assert( state != SLAP_C_INVALID );
714 return state == SLAP_C_CLOSING;
717 static void connection_abandon( Connection *c )
719 /* c_mutex must be locked by caller */
723 for( o = c->c_ops; o != NULL; o = o->o_next ) {
724 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
726 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
729 /* remove pending operations */
730 for( o = slap_op_pop( &c->c_pending_ops );
732 o = slap_op_pop( &c->c_pending_ops ) )
738 void connection_closing( Connection *c )
740 assert( connections != NULL );
742 assert( c->c_struct_state == SLAP_C_USED );
743 assert( c->c_conn_state != SLAP_C_INVALID );
745 /* c_mutex must be locked by caller */
747 if( c->c_conn_state != SLAP_C_CLOSING ) {
750 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
752 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
753 "connection_closing: conn %d readying socket %d for close.\n",
756 Debug( LDAP_DEBUG_TRACE,
757 "connection_closing: readying conn=%ld sd=%d for close\n",
758 c->c_connid, sd, 0 );
760 /* update state to closing */
761 c->c_conn_state = SLAP_C_CLOSING;
763 /* don't listen on this port anymore */
764 slapd_clr_read( sd, 1 );
766 /* abandon active operations */
767 connection_abandon( c );
769 /* wake write blocked operations */
770 slapd_clr_write( sd, 1 );
771 ldap_pvt_thread_cond_signal( &c->c_write_cv );
775 static void connection_close( Connection *c )
779 assert( connections != NULL );
781 assert( c->c_struct_state == SLAP_C_USED );
782 assert( c->c_conn_state == SLAP_C_CLOSING );
784 /* note: connections_mutex and c_mutex should be locked by caller */
786 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
787 if( c->c_ops != NULL ) {
789 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
790 "connection_close: conn %d deferring sd %d\n",
793 Debug( LDAP_DEBUG_TRACE,
794 "connection_close: deferring conn=%ld sd=%d\n",
795 c->c_connid, sd, 0 );
801 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
802 "connection_close: conn %d sd %d\n",
805 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
806 c->c_connid, sd, 0 );
808 connection_destroy( c );
811 unsigned long connections_nextid(void)
814 assert( connections != NULL );
816 ldap_pvt_thread_mutex_lock( &connections_mutex );
820 ldap_pvt_thread_mutex_unlock( &connections_mutex );
825 Connection* connection_first( ber_socket_t *index )
827 assert( connections != NULL );
828 assert( index != NULL );
830 ldap_pvt_thread_mutex_lock( &connections_mutex );
834 return connection_next(NULL, index);
837 Connection* connection_next( Connection *c, ber_socket_t *index )
839 assert( connections != NULL );
840 assert( index != NULL );
841 assert( *index <= dtblsize );
844 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
849 for(; *index < dtblsize; (*index)++) {
850 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
851 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
859 if( connections[*index].c_struct_state == SLAP_C_USED ) {
860 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
861 c = &connections[(*index)++];
865 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
866 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
870 ldap_pvt_thread_mutex_lock( &c->c_mutex );
876 void connection_done( Connection *c )
878 assert( connections != NULL );
881 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
884 ldap_pvt_thread_mutex_unlock( &connections_mutex );
888 * connection_activity - handle the request operation op on connection
889 * conn. This routine figures out what kind of operation it is and
890 * calls the appropriate stub to handle it.
894 connection_operation( void *arg_v )
897 struct co_arg *arg = arg_v;
898 ber_tag_t tag = arg->co_op->o_tag;
899 Connection *conn = arg->co_conn;
901 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
903 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
905 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
907 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
908 "connection_operation: conn %d SASL bind in progress (tag=%ld).\n",
909 conn->c_connid, (long)tag ));
911 Debug( LDAP_DEBUG_ANY, "connection_operation: "
912 "error: SASL bind in progress (tag=%ld).\n",
915 send_ldap_result( conn, arg->co_op,
916 rc = LDAP_OPERATIONS_ERROR,
917 NULL, "SASL bind in progress", NULL, NULL );
918 goto operations_error;
923 rc = do_bind( conn, arg->co_op );
926 case LDAP_REQ_UNBIND:
927 rc = do_unbind( conn, arg->co_op );
931 rc = do_add( conn, arg->co_op );
934 case LDAP_REQ_DELETE:
935 rc = do_delete( conn, arg->co_op );
938 case LDAP_REQ_MODRDN:
939 rc = do_modrdn( conn, arg->co_op );
942 case LDAP_REQ_MODIFY:
943 rc = do_modify( conn, arg->co_op );
946 case LDAP_REQ_COMPARE:
947 rc = do_compare( conn, arg->co_op );
950 case LDAP_REQ_SEARCH:
951 rc = do_search( conn, arg->co_op );
954 case LDAP_REQ_ABANDON:
955 rc = do_abandon( conn, arg->co_op );
958 case LDAP_REQ_EXTENDED:
959 rc = do_extended( conn, arg->co_op );
964 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
965 "connection_operation: conn %d unknown LDAP request 0x%lx\n",
966 conn->c_connid, tag ));
968 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
971 arg->co_op->o_tag = LBER_ERROR;
972 send_ldap_disconnect( conn, arg->co_op,
973 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
978 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
981 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
983 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
985 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
987 conn->c_n_ops_executing--;
988 conn->c_n_ops_completed++;
990 slap_op_remove( &conn->c_ops, arg->co_op );
991 slap_op_free( arg->co_op );
994 free( (char *) arg );
999 case LDAP_REQ_UNBIND:
1000 /* c_mutex is locked */
1001 connection_closing( conn );
1005 conn->c_sasl_bind_in_progress =
1006 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1008 if( conn->c_conn_state == SLAP_C_BINDING) {
1009 conn->c_conn_state = SLAP_C_ACTIVE;
1013 connection_resched( conn );
1015 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1020 int connection_read(ber_socket_t s)
1024 assert( connections != NULL );
1026 ldap_pvt_thread_mutex_lock( &connections_mutex );
1028 /* get (locked) connection */
1029 c = connection_get( s );
1033 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1034 "connection_read: sock %ld no connection\n",
1037 Debug( LDAP_DEBUG_ANY,
1038 "connection_read(%ld): no connection!\n",
1043 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1049 if( c->c_conn_state == SLAP_C_CLOSING ) {
1051 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1052 "connection_read: conn %d connection closing, ignoring input\n",
1055 Debug( LDAP_DEBUG_TRACE,
1056 "connection_read(%d): closing, ignoring input for id=%ld\n",
1057 s, c->c_connid, 0 );
1059 connection_return( c );
1060 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1065 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1066 "connection_read: conn %d checking for input.\n", c->c_connid ));
1068 Debug( LDAP_DEBUG_TRACE,
1069 "connection_read(%d): checking for input on id=%ld\n",
1070 s, c->c_connid, 0 );
1074 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1075 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1077 #if 0 /* required by next #if 0 */
1083 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1084 "connection_read: conn %d TLS accept error, error %d\n",
1087 Debug( LDAP_DEBUG_TRACE,
1088 "connection_read(%d): TLS accept error "
1089 "error=%d id=%ld, closing\n",
1090 s, rc, c->c_connid );
1092 c->c_needs_tls_accept = 0;
1093 /* connections_mutex and c_mutex are locked */
1094 connection_closing( c );
1097 /* Drain input before close, to allow SSL error codes
1098 * to propagate to client. */
1105 rc = select(s+1, &rfd, NULL, NULL, &tv);
1107 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1111 connection_close( c );
1113 } else if ( rc == 0 ) {
1117 c->c_needs_tls_accept = 0;
1119 /* we need to let SASL know */
1120 ssl = (void *)ldap_pvt_tls_sb_ctx( c->c_sb );
1122 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1123 if( c->c_tls_ssf > c->c_ssf ) {
1124 c->c_ssf = c->c_tls_ssf;
1127 authid = (char *)ldap_pvt_tls_get_peer( ssl );
1128 slap_sasl_external( c, c->c_tls_ssf, authid );
1130 connection_return( c );
1131 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1136 #ifdef HAVE_CYRUS_SASL
1137 if ( c->c_sasl_layers ) {
1138 c->c_sasl_layers = 0;
1140 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1142 if( rc != LDAP_SUCCESS ) {
1144 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1145 "connection_read: conn %d SASL install error %d, closing\n",
1148 Debug( LDAP_DEBUG_TRACE,
1149 "connection_read(%d): SASL install error "
1150 "error=%d id=%ld, closing\n",
1151 s, rc, c->c_connid );
1153 /* connections_mutex and c_mutex are locked */
1154 connection_closing( c );
1155 connection_close( c );
1156 connection_return( c );
1157 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1163 #define CONNECTION_INPUT_LOOP 1
1165 #ifdef DATA_READY_LOOP
1166 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1167 #elif CONNECTION_INPUT_LOOP
1171 /* How do we do this without getting into a busy loop ? */
1172 rc = connection_input( c );
1177 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1178 "connection_read: conn %d input error %d, closing.\n",
1181 Debug( LDAP_DEBUG_TRACE,
1182 "connection_read(%d): input error=%d id=%ld, closing.\n",
1183 s, rc, c->c_connid );
1185 /* connections_mutex and c_mutex are locked */
1186 connection_closing( c );
1187 connection_close( c );
1188 connection_return( c );
1189 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1193 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1194 slapd_set_read( s, 1 );
1197 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1198 slapd_set_write( s, 1 );
1201 connection_return( c );
1202 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1216 #ifdef LDAP_CONNECTIONLESS
1221 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc_t(0))
1224 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1225 "connection_input: conn %d ber_alloc_t failed.\n", conn->c_connid ));
1227 Debug( LDAP_DEBUG_ANY, "ber_alloc_t failed\n", 0, 0, 0 );
1234 #ifdef LDAP_CONNECTIONLESS
1237 char peername[sizeof("IP=255.255.255.255:65336")];
1238 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1239 sizeof(struct sockaddr));
1240 if (len != sizeof(struct sockaddr))
1242 sprintf( peername, "IP=%s:%d",
1243 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1244 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1245 Statslog( LDAP_DEBUG_STATS,
1246 "conn=%ld UDP request from %s (%s) accepted.\n",
1247 conn->c_connid, peername,
1248 conn->c_sock_name, 0, 0 );
1251 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1252 if ( tag != LDAP_TAG_MESSAGE ) {
1256 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1259 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1260 "connection_input: conn %d ber_get_next failed, errno %d (%s).\n",
1261 conn->c_connid, err, sock_errstr(err) ));
1263 Debug( LDAP_DEBUG_TRACE,
1264 "ber_get_next on fd %d failed errno=%d (%s)\n",
1265 sd, err, sock_errstr(err) );
1267 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1268 /* log, close and send error */
1269 ber_free( conn->c_currentber, 1 );
1270 conn->c_currentber = NULL;
1277 ber = conn->c_currentber;
1278 conn->c_currentber = NULL;
1280 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1281 /* log, close and send error */
1283 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1284 "connection_input: conn %d ber_get_int returns 0x%lx.\n",
1285 conn->c_connid, tag ));
1287 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1294 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1295 /* log, close and send error */
1297 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1298 "connection_input: conn %d ber_peek_tag returns 0x%lx.\n",
1299 conn->c_connid, tag ));
1301 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1309 #ifdef LDAP_CONNECTIONLESS
1310 if (conn->c_is_udp) {
1311 if (tag == LBER_OCTETSTRING) {
1312 ber_get_stringa( ber, &cdn );
1313 tag = ber_peek_tag(ber, &len);
1315 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1317 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1318 "connection_input: conn %d invalid req for UDP 0x%lx.\n",
1319 conn->c_connid, tag ));
1321 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1329 if(tag == LDAP_REQ_BIND) {
1330 /* immediately abandon all exiting operations upon BIND */
1331 connection_abandon( conn );
1334 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1336 #ifdef LDAP_CONNECTIONLESS
1337 op->o_peeraddr = peeraddr;
1340 op->o_protocol = LDAP_VERSION2;
1343 if ( conn->c_conn_state == SLAP_C_BINDING
1344 || conn->c_conn_state == SLAP_C_CLOSING )
1347 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1348 "connection_input: conn %d deferring operation\n",
1351 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1353 conn->c_n_ops_pending++;
1354 slap_op_add( &conn->c_pending_ops, op );
1357 conn->c_n_ops_executing++;
1358 connection_op_activate( conn, op );
1362 if ( conn->c_struct_state != SLAP_C_USED ) {
1363 /* connection must have got closed underneath us */
1367 assert( conn->c_struct_state == SLAP_C_USED );
1373 connection_resched( Connection *conn )
1377 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1380 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1382 /* us trylock to avoid possible deadlock */
1383 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1387 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1388 "connection_resched: conn %d reaquiring locks.\n",
1391 Debug( LDAP_DEBUG_TRACE,
1392 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1393 conn->c_connid, sd, 0 );
1396 * reaquire locks in the right order...
1397 * this may allow another thread to close this connection,
1398 * so recheck state below.
1400 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1401 ldap_pvt_thread_mutex_lock( &connections_mutex );
1402 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1405 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1407 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1408 "connection_resched: conn %d closed by other thread.\n",
1411 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1412 "closed by other thread conn=%ld sd=%d\n",
1413 conn->c_connid, sd, 0 );
1417 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1418 "connection_resched: conn %d attempting closing.\n",
1421 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1422 "attempting closing conn=%ld sd=%d\n",
1423 conn->c_connid, sd, 0 );
1425 connection_close( conn );
1428 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1432 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1433 /* other states need different handling */
1437 for( op = slap_op_pop( &conn->c_pending_ops );
1439 op = slap_op_pop( &conn->c_pending_ops ) )
1441 /* pending operations should not be marked for abandonment */
1442 assert(!op->o_abandon);
1444 conn->c_n_ops_pending--;
1445 conn->c_n_ops_executing++;
1447 connection_op_activate( conn, op );
1449 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1456 static int connection_op_activate( Connection *conn, Operation *op )
1460 ber_tag_t tag = op->o_tag;
1462 if(tag == LDAP_REQ_BIND) {
1463 conn->c_conn_state = SLAP_C_BINDING;
1466 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1467 arg->co_conn = conn;
1470 if (!arg->co_op->o_dn.bv_len) {
1471 arg->co_op->o_authz = conn->c_authz;
1472 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1473 conn->c_dn.bv_val : "" );
1474 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1475 conn->c_ndn.bv_val : "" );
1477 arg->co_op->o_authtype = conn->c_authtype;
1478 arg->co_op->o_authmech = conn->c_authmech != NULL
1479 ? ch_strdup( conn->c_authmech ) : NULL;
1481 if (!arg->co_op->o_protocol) {
1482 arg->co_op->o_protocol = conn->c_protocol
1483 ? conn->c_protocol : LDAP_VERSION3;
1485 arg->co_op->o_connid = conn->c_connid;
1487 slap_op_add( &conn->c_ops, arg->co_op );
1489 status = ldap_pvt_thread_pool_submit( &connection_pool,
1490 connection_operation, (void *) arg );
1492 if ( status != 0 ) {
1494 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1495 "connection_op_activate: conn %d thread pool submit failed.\n",
1498 Debug( LDAP_DEBUG_ANY,
1499 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1501 /* should move op to pending list */
1507 int connection_write(ber_socket_t s)
1510 assert( connections != NULL );
1512 ldap_pvt_thread_mutex_lock( &connections_mutex );
1514 c = connection_get( s );
1516 slapd_clr_write( s, 0);
1520 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1521 "connection_write: sock %ld no connection!\n",(long)s));
1523 Debug( LDAP_DEBUG_ANY,
1524 "connection_write(%ld): no connection!\n",
1528 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1535 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1536 "connection_write conn %d waking output.\n",
1539 Debug( LDAP_DEBUG_TRACE,
1540 "connection_write(%d): waking output for id=%ld\n",
1541 s, c->c_connid, 0 );
1543 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1545 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1546 slapd_set_read( s, 1 );
1547 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1548 slapd_set_write( s, 1 );
1549 connection_return( c );
1550 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1556 * Create client side and server side connection structures, connected to
1557 * one another, for the front end to use for searches on arbitrary back ends.
1560 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1563 ber_socket_t fd[2] = {-1,-1};
1569 rc = lutil_pair( fd );
1574 rc = connection_init( fd[1], "INT", "localhost",
1575 "localhost:0", "localhost:00", 0, 256, id );
1581 slapd_add_internal( fd[1] );
1583 /* A search operation, number 0 */
1584 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1585 op->o_ndn.bv_val = ch_strdup( id );
1586 op->o_ndn.bv_len = strlen( id );
1587 op->o_protocol = LDAP_VERSION3;
1589 (*conn) = connection_get( fd[1] );
1590 (*conn)->c_ops = op;
1591 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1594 /* Create the client side of the connection */
1595 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1596 if( rc != LDAP_SUCCESS ) {
1601 /* The connection_get() will have locked the connection's mutex */
1602 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1604 return LDAP_SUCCESS;
1608 void connection_internal_close( Connection *conn )
1610 Operation *op = conn->c_ops;
1612 slap_op_remove( &conn->c_ops, op );
1614 connection_closing( conn );
1615 connection_close( conn );