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();
429 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
431 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
432 LBER_SBIOD_LEVEL_PROVIDER, NULL );
434 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
436 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
437 Debug( LDAP_DEBUG_ANY,
438 "connection_init(%d, %s): set nonblocking failed\n",
439 s, c->c_peer_name, 0 );
443 id = c->c_connid = conn_nextid++;
445 c->c_conn_state = SLAP_C_INACTIVE;
446 c->c_struct_state = SLAP_C_USED;
448 c->c_ssf = c->c_transport_ssf = ssf;
454 c->c_needs_tls_accept = 1;
457 c->c_needs_tls_accept = 0;
462 slap_sasl_external( c, ssf, authid );
464 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
465 ldap_pvt_thread_mutex_unlock( &connections_mutex );
467 backend_connection_init(c);
472 void connection2anonymous( Connection *c )
474 assert( connections != NULL );
477 if(c->c_authmech != NULL ) {
479 c->c_authmech = NULL;
482 if(c->c_dn != NULL) {
487 if(c->c_cdn != NULL) {
492 c->c_authc_backend = NULL;
493 c->c_authz_backend = NULL;
497 connection_destroy( Connection *c )
499 /* note: connections_mutex should be locked by caller */
502 assert( connections != NULL );
504 assert( c->c_struct_state != SLAP_C_UNUSED );
505 assert( c->c_conn_state != SLAP_C_INVALID );
506 assert( c->c_ops == NULL );
508 backend_connection_destroy(c);
513 c->c_activitytime = c->c_starttime = 0;
515 connection2anonymous( c );
517 if(c->c_listener_url != NULL) {
518 free(c->c_listener_url);
519 c->c_listener_url = NULL;
522 if(c->c_peer_domain != NULL) {
523 free(c->c_peer_domain);
524 c->c_peer_domain = NULL;
526 if(c->c_peer_name != NULL) {
529 * If peer was a domain socket, unlink. Mind you,
530 * they may be un-named. Should we leave this to
533 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
534 char *path = c->c_peer_name + 5;
539 #endif /* LDAP_PF_LOCAL */
541 free(c->c_peer_name);
542 c->c_peer_name = NULL;
544 if(c->c_sock_name != NULL) {
545 free(c->c_sock_name);
546 c->c_sock_name = NULL;
549 c->c_sasl_bind_in_progress = 0;
550 if(c->c_sasl_bind_mech != NULL) {
551 free(c->c_sasl_bind_mech);
552 c->c_sasl_bind_mech = NULL;
555 slap_sasl_close( c );
557 if ( c->c_currentber != NULL ) {
558 ber_free( c->c_currentber, 1 );
559 c->c_currentber = NULL;
562 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
563 if ( sd != AC_SOCKET_INVALID ) {
564 slapd_remove( sd, 0 );
566 Statslog( LDAP_DEBUG_STATS,
567 "conn=%ld fd=%d closed\n",
568 c->c_connid, sd, 0, 0, 0 );
571 ber_sockbuf_free( c->c_sb );
572 c->c_sb = ber_sockbuf_alloc( );
574 c->c_conn_state = SLAP_C_INVALID;
575 c->c_struct_state = SLAP_C_UNUSED;
578 int connection_state_closing( Connection *c )
580 /* c_mutex must be locked by caller */
584 assert( c->c_struct_state == SLAP_C_USED );
586 state = c->c_conn_state;
588 assert( state != SLAP_C_INVALID );
590 return state == SLAP_C_CLOSING;
593 static void connection_abandon( Connection *c )
595 /* c_mutex must be locked by caller */
599 for( o = c->c_ops; o != NULL; o = o->o_next ) {
600 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
602 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
605 /* remove pending operations */
606 for( o = slap_op_pop( &c->c_pending_ops );
608 o = slap_op_pop( &c->c_pending_ops ) )
614 void connection_closing( Connection *c )
616 assert( connections != NULL );
618 assert( c->c_struct_state == SLAP_C_USED );
619 assert( c->c_conn_state != SLAP_C_INVALID );
621 /* c_mutex must be locked by caller */
623 if( c->c_conn_state != SLAP_C_CLOSING ) {
626 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
627 Debug( LDAP_DEBUG_TRACE,
628 "connection_closing: readying conn=%ld sd=%d for close\n",
629 c->c_connid, sd, 0 );
631 /* update state to closing */
632 c->c_conn_state = SLAP_C_CLOSING;
634 /* don't listen on this port anymore */
635 slapd_clr_read( sd, 1 );
637 /* abandon active operations */
638 connection_abandon( c );
640 /* wake write blocked operations */
641 slapd_clr_write( sd, 1 );
642 ldap_pvt_thread_cond_signal( &c->c_write_cv );
646 static void connection_close( Connection *c )
650 assert( connections != NULL );
652 assert( c->c_struct_state == SLAP_C_USED );
653 assert( c->c_conn_state == SLAP_C_CLOSING );
655 /* note: connections_mutex and c_mutex should be locked by caller */
657 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
658 if( c->c_ops != NULL ) {
659 Debug( LDAP_DEBUG_TRACE,
660 "connection_close: deferring conn=%ld sd=%d\n",
661 c->c_connid, sd, 0 );
666 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
667 c->c_connid, sd, 0 );
669 connection_destroy( c );
672 unsigned long connections_nextid(void)
675 assert( connections != NULL );
677 ldap_pvt_thread_mutex_lock( &connections_mutex );
681 ldap_pvt_thread_mutex_unlock( &connections_mutex );
686 Connection* connection_first( ber_socket_t *index )
688 assert( connections != NULL );
689 assert( index != NULL );
691 ldap_pvt_thread_mutex_lock( &connections_mutex );
695 return connection_next(NULL, index);
698 Connection* connection_next( Connection *c, ber_socket_t *index )
700 assert( connections != NULL );
701 assert( index != NULL );
702 assert( *index <= dtblsize );
705 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
710 for(; *index < dtblsize; (*index)++) {
711 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
712 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
720 if( connections[*index].c_struct_state == SLAP_C_USED ) {
721 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
722 c = &connections[(*index)++];
726 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
727 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
731 ldap_pvt_thread_mutex_lock( &c->c_mutex );
737 void connection_done( Connection *c )
739 assert( connections != NULL );
742 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
745 ldap_pvt_thread_mutex_unlock( &connections_mutex );
749 * connection_activity - handle the request operation op on connection
750 * conn. This routine figures out what kind of operation it is and
751 * calls the appropriate stub to handle it.
755 connection_operation( void *arg_v )
758 struct co_arg *arg = arg_v;
759 ber_tag_t tag = arg->co_op->o_tag;
760 Connection *conn = arg->co_conn;
762 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
764 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
766 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
767 Debug( LDAP_DEBUG_ANY, "connection_operation: "
768 "error: SASL bind in progress (tag=%ld).\n",
770 send_ldap_result( conn, arg->co_op,
771 rc = LDAP_OPERATIONS_ERROR,
772 NULL, "SASL bind in progress", NULL, NULL );
773 goto operations_error;
778 rc = do_bind( conn, arg->co_op );
781 case LDAP_REQ_UNBIND:
782 rc = do_unbind( conn, arg->co_op );
786 rc = do_add( conn, arg->co_op );
789 case LDAP_REQ_DELETE:
790 rc = do_delete( conn, arg->co_op );
793 case LDAP_REQ_MODRDN:
794 rc = do_modrdn( conn, arg->co_op );
797 case LDAP_REQ_MODIFY:
798 rc = do_modify( conn, arg->co_op );
801 case LDAP_REQ_COMPARE:
802 rc = do_compare( conn, arg->co_op );
805 case LDAP_REQ_SEARCH:
806 rc = do_search( conn, arg->co_op );
809 case LDAP_REQ_ABANDON:
810 rc = do_abandon( conn, arg->co_op );
813 case LDAP_REQ_EXTENDED:
814 rc = do_extended( conn, arg->co_op );
818 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
820 arg->co_op->o_tag = LBER_ERROR;
821 send_ldap_disconnect( conn, arg->co_op,
822 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
827 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
830 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
832 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
834 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
836 conn->c_n_ops_executing--;
837 conn->c_n_ops_completed++;
839 slap_op_remove( &conn->c_ops, arg->co_op );
840 slap_op_free( arg->co_op );
843 free( (char *) arg );
848 case LDAP_REQ_UNBIND:
849 /* c_mutex is locked */
850 connection_closing( conn );
854 conn->c_sasl_bind_in_progress =
855 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
857 if( conn->c_conn_state == SLAP_C_BINDING) {
858 conn->c_conn_state = SLAP_C_ACTIVE;
862 connection_resched( conn );
864 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
869 int connection_read(ber_socket_t s)
873 assert( connections != NULL );
875 ldap_pvt_thread_mutex_lock( &connections_mutex );
877 /* get (locked) connection */
878 c = connection_get( s );
881 Debug( LDAP_DEBUG_ANY,
882 "connection_read(%ld): no connection!\n",
887 ldap_pvt_thread_mutex_unlock( &connections_mutex );
893 if( c->c_conn_state == SLAP_C_CLOSING ) {
894 Debug( LDAP_DEBUG_TRACE,
895 "connection_read(%d): closing, ignoring input for id=%ld\n",
898 connection_return( c );
899 ldap_pvt_thread_mutex_unlock( &connections_mutex );
903 Debug( LDAP_DEBUG_TRACE,
904 "connection_read(%d): checking for input on id=%ld\n",
908 if ( c->c_is_tls && c->c_needs_tls_accept ) {
909 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
914 Debug( LDAP_DEBUG_TRACE,
915 "connection_read(%d): TLS accept error "
916 "error=%d id=%ld, closing\n",
917 s, rc, c->c_connid );
919 c->c_needs_tls_accept = 0;
920 /* connections_mutex and c_mutex are locked */
921 connection_closing( c );
924 /* Drain input before close, to allow SSL error codes
925 * to propagate to client. */
932 rc = select(s+1, &rfd, NULL, NULL, &tv);
934 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
938 connection_close( c );
940 } else if ( rc == 0 ) {
944 c->c_needs_tls_accept = 0;
946 /* we need to let SASL know */
947 ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
949 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
950 if( c->c_tls_ssf > c->c_ssf ) {
951 c->c_ssf = c->c_tls_ssf;
954 authid = (char *)ldap_pvt_tls_get_peer( ssl );
955 slap_sasl_external( c, c->c_tls_ssf, authid );
957 connection_return( c );
958 ldap_pvt_thread_mutex_unlock( &connections_mutex );
963 #ifdef HAVE_CYRUS_SASL
964 if ( c->c_sasl_layers ) {
965 c->c_sasl_layers = 0;
967 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
969 if( rc != LDAP_SUCCESS ) {
970 Debug( LDAP_DEBUG_TRACE,
971 "connection_read(%d): SASL install error "
972 "error=%d id=%ld, closing\n",
973 s, rc, c->c_connid );
975 /* connections_mutex and c_mutex are locked */
976 connection_closing( c );
977 connection_close( c );
978 connection_return( c );
979 ldap_pvt_thread_mutex_unlock( &connections_mutex );
985 #define CONNECTION_INPUT_LOOP 1
987 #ifdef DATA_READY_LOOP
988 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
989 #elif CONNECTION_INPUT_LOOP
993 /* How do we do this without getting into a busy loop ? */
994 rc = connection_input( c );
998 Debug( LDAP_DEBUG_TRACE,
999 "connection_read(%d): input error=%d id=%ld, closing.\n",
1000 s, rc, c->c_connid );
1002 /* connections_mutex and c_mutex are locked */
1003 connection_closing( c );
1004 connection_close( c );
1005 connection_return( c );
1006 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1010 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1011 slapd_set_read( s, 1 );
1014 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1015 slapd_set_write( s, 1 );
1018 connection_return( c );
1019 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1034 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1036 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1042 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1043 if ( tag != LDAP_TAG_MESSAGE ) {
1047 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1049 Debug( LDAP_DEBUG_TRACE,
1050 "ber_get_next on fd %d failed errno=%d (%s)\n",
1051 sd, err, sock_errstr(err) );
1053 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1054 /* log, close and send error */
1055 ber_free( conn->c_currentber, 1 );
1056 conn->c_currentber = NULL;
1063 ber = conn->c_currentber;
1064 conn->c_currentber = NULL;
1066 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1067 /* log, close and send error */
1068 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1074 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1075 /* log, close and send error */
1076 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1083 if(tag == LDAP_REQ_BIND) {
1084 /* immediately abandon all exiting operations upon BIND */
1085 connection_abandon( conn );
1088 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1090 if ( conn->c_conn_state == SLAP_C_BINDING
1091 || conn->c_conn_state == SLAP_C_CLOSING )
1093 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1094 conn->c_n_ops_pending++;
1095 slap_op_add( &conn->c_pending_ops, op );
1098 conn->c_n_ops_executing++;
1099 connection_op_activate( conn, op );
1103 if ( conn->c_struct_state != SLAP_C_USED ) {
1104 /* connection must have got closed underneath us */
1108 assert( conn->c_struct_state == SLAP_C_USED );
1114 connection_resched( Connection *conn )
1118 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1121 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1123 /* us trylock to avoid possible deadlock */
1124 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1127 Debug( LDAP_DEBUG_TRACE,
1128 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1129 conn->c_connid, sd, 0 );
1131 * reaquire locks in the right order...
1132 * this may allow another thread to close this connection,
1133 * so recheck state below.
1135 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1136 ldap_pvt_thread_mutex_lock( &connections_mutex );
1137 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1140 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1141 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1142 "closed by other thread conn=%ld sd=%d\n",
1143 conn->c_connid, sd, 0 );
1146 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1147 "attempting closing conn=%ld sd=%d\n",
1148 conn->c_connid, sd, 0 );
1150 connection_close( conn );
1153 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1157 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1158 /* other states need different handling */
1162 for( op = slap_op_pop( &conn->c_pending_ops );
1164 op = slap_op_pop( &conn->c_pending_ops ) )
1166 /* pending operations should not be marked for abandonment */
1167 assert(!op->o_abandon);
1169 conn->c_n_ops_pending--;
1170 conn->c_n_ops_executing++;
1172 connection_op_activate( conn, op );
1174 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1181 static int connection_op_activate( Connection *conn, Operation *op )
1185 ber_tag_t tag = op->o_tag;
1187 if(tag == LDAP_REQ_BIND) {
1188 conn->c_conn_state = SLAP_C_BINDING;
1191 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1192 arg->co_conn = conn;
1195 arg->co_op->o_authz = conn->c_authz;
1196 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1197 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1198 (void) dn_normalize( arg->co_op->o_ndn );
1199 arg->co_op->o_authtype = conn->c_authtype;
1200 arg->co_op->o_authmech = conn->c_authmech != NULL
1201 ? ch_strdup( conn->c_authmech ) : NULL;
1203 arg->co_op->o_protocol = conn->c_protocol
1204 ? conn->c_protocol : LDAP_VERSION3;
1205 arg->co_op->o_connid = conn->c_connid;
1207 slap_op_add( &conn->c_ops, arg->co_op );
1209 status = ldap_pvt_thread_pool_submit( &connection_pool,
1210 connection_operation, (void *) arg );
1212 if ( status != 0 ) {
1213 Debug( LDAP_DEBUG_ANY,
1214 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1215 /* should move op to pending list */
1221 int connection_write(ber_socket_t s)
1224 assert( connections != NULL );
1226 ldap_pvt_thread_mutex_lock( &connections_mutex );
1228 c = connection_get( s );
1230 slapd_clr_write( s, 0);
1233 Debug( LDAP_DEBUG_ANY,
1234 "connection_write(%ld): no connection!\n",
1237 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1243 Debug( LDAP_DEBUG_TRACE,
1244 "connection_write(%d): waking output for id=%ld\n",
1245 s, c->c_connid, 0 );
1247 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1249 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1250 slapd_set_read( s, 1 );
1251 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1252 slapd_set_write( s, 1 );
1253 connection_return( c );
1254 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1260 * Create client side and server side connection structures, connected to
1261 * one another, for the front end to use for searches on arbitrary back ends.
1264 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1267 ber_socket_t fd[2] = {-1,-1};
1273 rc = lutil_pair( fd );
1278 rc = connection_init( fd[1], "INT", "localhost",
1279 "localhost:0", "localhost:00", 0, 256, id );
1285 slapd_add_internal( fd[1] );
1287 /* A search operation, number 0 */
1288 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1289 op->o_ndn = ch_strdup( id );
1290 op->o_protocol = LDAP_VERSION3;
1292 (*conn) = connection_get( fd[1] );
1293 (*conn)->c_ops = op;
1294 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1297 /* Create the client side of the connection */
1298 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1299 if( rc != LDAP_SUCCESS ) {
1304 /* The connection_get() will have locked the connection's mutex */
1305 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1307 return LDAP_SUCCESS;
1311 void connection_internal_close( Connection *conn )
1313 connection_closing( conn );
1314 connection_close( conn );