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>
21 /* protected by connections_mutex */
22 static ldap_pvt_thread_mutex_t connections_mutex;
23 static Connection *connections = NULL;
24 static unsigned long conn_nextid = 0;
26 /* structure state (protected by connections_mutex) */
27 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
28 #define SLAP_C_UNUSED 0x01
29 #define SLAP_C_USED 0x02
31 /* connection state (protected by c_mutex ) */
32 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
33 #define SLAP_C_INACTIVE 0x01 /* zero threads */
34 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
35 #define SLAP_C_BINDING 0x03 /* binding */
36 #define SLAP_C_CLOSING 0x04 /* closing */
39 connection_state2str( int state )
42 case SLAP_C_INVALID: return "!";
43 case SLAP_C_INACTIVE: return "|";
44 case SLAP_C_ACTIVE: return "";
45 case SLAP_C_BINDING: return "B";
46 case SLAP_C_CLOSING: return "C";
52 static Connection* connection_get( ber_socket_t s );
54 static int connection_input( Connection *c );
55 static void connection_close( Connection *c );
57 static int connection_op_activate( Connection *conn, Operation *op );
58 static int connection_resched( Connection *conn );
59 static void connection_abandon( Connection *conn );
60 static void connection_destroy( Connection *c );
68 * Initialize connection management infrastructure.
70 int connections_init(void)
72 assert( connections == NULL );
74 if( connections != NULL) {
75 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
80 /* should check return of every call */
81 ldap_pvt_thread_mutex_init( &connections_mutex );
83 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
85 if( connections == NULL ) {
86 Debug( LDAP_DEBUG_ANY,
87 "connections_init: allocation (%d*%ld) of connection array failed\n",
88 dtblsize, (long) sizeof(Connection), 0 );
92 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
93 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
96 * per entry initialization of the Connection array initialization
97 * will be done by connection_init()
104 * Destroy connection management infrastructure.
106 int connections_destroy(void)
110 /* should check return of every call */
112 if( connections == NULL) {
113 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
118 for ( i = 0; i < dtblsize; i++ ) {
119 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
120 ber_sockbuf_free( connections[i].c_sb );
121 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
122 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
123 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
130 ldap_pvt_thread_mutex_destroy( &connections_mutex );
135 * shutdown all connections
137 int connections_shutdown(void)
141 ldap_pvt_thread_mutex_lock( &connections_mutex );
143 for ( i = 0; i < dtblsize; i++ ) {
144 if( connections[i].c_struct_state != SLAP_C_USED ) {
148 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
150 /* connections_mutex and c_mutex are locked */
151 connection_closing( &connections[i] );
152 connection_close( &connections[i] );
154 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
157 ldap_pvt_thread_mutex_unlock( &connections_mutex );
163 * Timeout idle connections.
165 int connections_timeout_idle(time_t now)
171 ldap_pvt_thread_mutex_lock( &connections_mutex );
173 for( c = connection_first( &connindex );
175 c = connection_next( c, &connindex ) )
177 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
179 connection_closing( c );
180 connection_close( c );
184 connection_done( c );
186 ldap_pvt_thread_mutex_unlock( &connections_mutex );
191 static Connection* connection_get( ber_socket_t s )
193 /* connections_mutex should be locked by caller */
197 Debug( LDAP_DEBUG_ARGS,
198 "connection_get(%ld)\n",
201 assert( connections != NULL );
203 if(s == AC_SOCKET_INVALID) {
210 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
217 for(i=0; i<dtblsize; i++) {
218 ber_sockbuf_ctrl( connections[i].c_sb,
219 LBER_SB_OPT_GET_FD, &sd );
221 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
222 assert( connections[i].c_conn_state == SLAP_C_INVALID );
223 assert( connections[i].c_sb == 0 );
227 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
228 assert( connections[i].c_conn_state == SLAP_C_INVALID );
229 assert( sd == AC_SOCKET_INVALID );
233 /* state can actually change from used -> unused by resched,
234 * so don't assert details here.
248 ldap_pvt_thread_mutex_lock( &c->c_mutex );
250 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
251 if( c->c_struct_state != SLAP_C_USED ) {
252 /* connection must have been closed due to resched */
254 assert( c->c_conn_state == SLAP_C_INVALID );
255 assert( sd == AC_SOCKET_INVALID );
257 Debug( LDAP_DEBUG_TRACE,
258 "connection_get(%d): connection not used\n",
261 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
265 Debug( LDAP_DEBUG_TRACE,
266 "connection_get(%d): got connid=%ld\n",
271 assert( c->c_struct_state == SLAP_C_USED );
272 assert( c->c_conn_state != SLAP_C_INVALID );
273 assert( sd != AC_SOCKET_INVALID );
275 c->c_activitytime = slap_get_time();
281 static void connection_return( Connection *c )
283 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
286 long connection_init(
290 const char* peername,
291 const char* sockname,
299 assert( connections != NULL );
301 assert( dnsname != NULL );
302 assert( peername != NULL );
303 assert( sockname != NULL );
309 if( s == AC_SOCKET_INVALID ) {
310 Debug( LDAP_DEBUG_ANY,
311 "connection_init(%ld): invalid.\n",
318 assert( s < dtblsize );
321 ldap_pvt_thread_mutex_lock( &connections_mutex );
332 for( i=0; i < dtblsize; i++) {
335 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
336 assert( connections[i].c_sb == 0 );
341 sd = AC_SOCKET_INVALID;
342 if (connections[i].c_sb != NULL)
343 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
345 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
346 assert( sd == AC_SOCKET_INVALID );
351 assert( connections[i].c_struct_state == SLAP_C_USED );
352 assert( connections[i].c_conn_state != SLAP_C_INVALID );
353 assert( sd != AC_SOCKET_INVALID );
357 Debug( LDAP_DEBUG_ANY,
358 "connection_init(%d): connection table full (%d/%d)\n",
360 ldap_pvt_thread_mutex_unlock( &connections_mutex );
368 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
369 c->c_authmech = NULL;
373 c->c_listener_url = NULL;
374 c->c_peer_domain = NULL;
375 c->c_peer_name = NULL;
376 c->c_sock_name = NULL;
379 c->c_pending_ops = NULL;
381 c->c_sasl_bind_mech = NULL;
382 c->c_sasl_context = NULL;
383 c->c_sasl_extra = NULL;
385 c->c_sb = ber_sockbuf_alloc( );
386 c->c_currentber = NULL;
388 /* should check status of thread calls */
389 ldap_pvt_thread_mutex_init( &c->c_mutex );
390 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
391 ldap_pvt_thread_cond_init( &c->c_write_cv );
393 c->c_struct_state = SLAP_C_UNUSED;
396 ldap_pvt_thread_mutex_lock( &c->c_mutex );
398 assert( c->c_struct_state == SLAP_C_UNUSED );
399 assert( c->c_authmech == NULL );
400 assert( c->c_dn == NULL );
401 assert( c->c_cdn == NULL );
402 assert( c->c_listener_url == NULL );
403 assert( c->c_peer_domain == NULL );
404 assert( c->c_peer_name == NULL );
405 assert( c->c_sock_name == NULL );
406 assert( c->c_ops == NULL );
407 assert( c->c_pending_ops == NULL );
408 assert( c->c_sasl_bind_mech == NULL );
409 assert( c->c_sasl_context == NULL );
410 assert( c->c_sasl_extra == NULL );
411 assert( c->c_currentber == NULL );
413 c->c_listener_url = ch_strdup( url );
414 c->c_peer_domain = ch_strdup( dnsname );
415 c->c_peer_name = ch_strdup( peername );
416 c->c_sock_name = ch_strdup( sockname );
418 c->c_n_ops_received = 0;
419 c->c_n_ops_executing = 0;
420 c->c_n_ops_pending = 0;
421 c->c_n_ops_completed = 0;
427 /* set to zero until bind, implies LDAP_VERSION3 */
430 c->c_activitytime = c->c_starttime = slap_get_time();
432 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
434 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
435 LBER_SBIOD_LEVEL_PROVIDER, NULL );
437 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
439 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
440 Debug( LDAP_DEBUG_ANY,
441 "connection_init(%d, %s): set nonblocking failed\n",
442 s, c->c_peer_name, 0 );
446 id = c->c_connid = conn_nextid++;
448 c->c_conn_state = SLAP_C_INACTIVE;
449 c->c_struct_state = SLAP_C_USED;
451 c->c_ssf = c->c_transport_ssf = ssf;
457 c->c_needs_tls_accept = 1;
460 c->c_needs_tls_accept = 0;
465 slap_sasl_external( c, ssf, authid );
467 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
468 ldap_pvt_thread_mutex_unlock( &connections_mutex );
470 backend_connection_init(c);
475 void connection2anonymous( Connection *c )
477 assert( connections != NULL );
480 if(c->c_authmech != NULL ) {
482 c->c_authmech = NULL;
485 if(c->c_dn != NULL) {
490 if(c->c_cdn != NULL) {
495 c->c_authc_backend = NULL;
496 c->c_authz_backend = NULL;
500 connection_destroy( Connection *c )
502 /* note: connections_mutex should be locked by caller */
505 assert( connections != NULL );
507 assert( c->c_struct_state != SLAP_C_UNUSED );
508 assert( c->c_conn_state != SLAP_C_INVALID );
509 assert( c->c_ops == NULL );
511 backend_connection_destroy(c);
516 c->c_activitytime = c->c_starttime = 0;
518 connection2anonymous( c );
520 if(c->c_listener_url != NULL) {
521 free(c->c_listener_url);
522 c->c_listener_url = NULL;
525 if(c->c_peer_domain != NULL) {
526 free(c->c_peer_domain);
527 c->c_peer_domain = NULL;
529 if(c->c_peer_name != NULL) {
532 * If peer was a domain socket, unlink. Mind you,
533 * they may be un-named. Should we leave this to
536 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
537 char *path = c->c_peer_name + 5;
542 #endif /* LDAP_PF_LOCAL */
544 free(c->c_peer_name);
545 c->c_peer_name = NULL;
547 if(c->c_sock_name != NULL) {
548 free(c->c_sock_name);
549 c->c_sock_name = NULL;
552 c->c_sasl_bind_in_progress = 0;
553 if(c->c_sasl_bind_mech != NULL) {
554 free(c->c_sasl_bind_mech);
555 c->c_sasl_bind_mech = NULL;
558 slap_sasl_close( c );
560 if ( c->c_currentber != NULL ) {
561 ber_free( c->c_currentber, 1 );
562 c->c_currentber = NULL;
565 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
566 if ( sd != AC_SOCKET_INVALID ) {
567 slapd_remove( sd, 0 );
569 Statslog( LDAP_DEBUG_STATS,
570 "conn=%ld fd=%d closed\n",
571 c->c_connid, sd, 0, 0, 0 );
574 ber_sockbuf_free( c->c_sb );
575 c->c_sb = ber_sockbuf_alloc( );
577 c->c_conn_state = SLAP_C_INVALID;
578 c->c_struct_state = SLAP_C_UNUSED;
581 int connection_state_closing( Connection *c )
583 /* c_mutex must be locked by caller */
587 assert( c->c_struct_state == SLAP_C_USED );
589 state = c->c_conn_state;
591 assert( state != SLAP_C_INVALID );
593 return state == SLAP_C_CLOSING;
596 static void connection_abandon( Connection *c )
598 /* c_mutex must be locked by caller */
602 for( o = c->c_ops; o != NULL; o = o->o_next ) {
603 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
605 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
608 /* remove pending operations */
609 for( o = slap_op_pop( &c->c_pending_ops );
611 o = slap_op_pop( &c->c_pending_ops ) )
617 void connection_closing( Connection *c )
619 assert( connections != NULL );
621 assert( c->c_struct_state == SLAP_C_USED );
622 assert( c->c_conn_state != SLAP_C_INVALID );
624 /* c_mutex must be locked by caller */
626 if( c->c_conn_state != SLAP_C_CLOSING ) {
629 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
630 Debug( LDAP_DEBUG_TRACE,
631 "connection_closing: readying conn=%ld sd=%d for close\n",
632 c->c_connid, sd, 0 );
634 /* update state to closing */
635 c->c_conn_state = SLAP_C_CLOSING;
637 /* don't listen on this port anymore */
638 slapd_clr_read( sd, 1 );
640 /* abandon active operations */
641 connection_abandon( c );
643 /* wake write blocked operations */
644 slapd_clr_write( sd, 1 );
645 ldap_pvt_thread_cond_signal( &c->c_write_cv );
649 static void connection_close( Connection *c )
653 assert( connections != NULL );
655 assert( c->c_struct_state == SLAP_C_USED );
656 assert( c->c_conn_state == SLAP_C_CLOSING );
658 /* note: connections_mutex and c_mutex should be locked by caller */
660 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
661 if( c->c_ops != NULL ) {
662 Debug( LDAP_DEBUG_TRACE,
663 "connection_close: deferring conn=%ld sd=%d\n",
664 c->c_connid, sd, 0 );
669 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
670 c->c_connid, sd, 0 );
672 connection_destroy( c );
675 unsigned long connections_nextid(void)
678 assert( connections != NULL );
680 ldap_pvt_thread_mutex_lock( &connections_mutex );
684 ldap_pvt_thread_mutex_unlock( &connections_mutex );
689 Connection* connection_first( ber_socket_t *index )
691 assert( connections != NULL );
692 assert( index != NULL );
694 ldap_pvt_thread_mutex_lock( &connections_mutex );
698 return connection_next(NULL, index);
701 Connection* connection_next( Connection *c, ber_socket_t *index )
703 assert( connections != NULL );
704 assert( index != NULL );
705 assert( *index <= dtblsize );
708 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
713 for(; *index < dtblsize; (*index)++) {
714 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
715 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
723 if( connections[*index].c_struct_state == SLAP_C_USED ) {
724 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
725 c = &connections[(*index)++];
729 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
730 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
734 ldap_pvt_thread_mutex_lock( &c->c_mutex );
740 void connection_done( Connection *c )
742 assert( connections != NULL );
745 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
748 ldap_pvt_thread_mutex_unlock( &connections_mutex );
752 * connection_activity - handle the request operation op on connection
753 * conn. This routine figures out what kind of operation it is and
754 * calls the appropriate stub to handle it.
758 connection_operation( void *arg_v )
761 struct co_arg *arg = arg_v;
762 ber_tag_t tag = arg->co_op->o_tag;
763 Connection *conn = arg->co_conn;
765 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
767 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
769 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
770 Debug( LDAP_DEBUG_ANY, "connection_operation: "
771 "error: SASL bind in progress (tag=%ld).\n",
773 send_ldap_result( conn, arg->co_op,
774 rc = LDAP_OPERATIONS_ERROR,
775 NULL, "SASL bind in progress", NULL, NULL );
776 goto operations_error;
781 rc = do_bind( conn, arg->co_op );
784 case LDAP_REQ_UNBIND:
785 rc = do_unbind( conn, arg->co_op );
789 rc = do_add( conn, arg->co_op );
792 case LDAP_REQ_DELETE:
793 rc = do_delete( conn, arg->co_op );
796 case LDAP_REQ_MODRDN:
797 rc = do_modrdn( conn, arg->co_op );
800 case LDAP_REQ_MODIFY:
801 rc = do_modify( conn, arg->co_op );
804 case LDAP_REQ_COMPARE:
805 rc = do_compare( conn, arg->co_op );
808 case LDAP_REQ_SEARCH:
809 rc = do_search( conn, arg->co_op );
812 case LDAP_REQ_ABANDON:
813 rc = do_abandon( conn, arg->co_op );
816 case LDAP_REQ_EXTENDED:
817 rc = do_extended( conn, arg->co_op );
821 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
823 arg->co_op->o_tag = LBER_ERROR;
824 send_ldap_disconnect( conn, arg->co_op,
825 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
830 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
833 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
835 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
837 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
839 conn->c_n_ops_executing--;
840 conn->c_n_ops_completed++;
842 slap_op_remove( &conn->c_ops, arg->co_op );
843 slap_op_free( arg->co_op );
846 free( (char *) arg );
851 case LDAP_REQ_UNBIND:
852 /* c_mutex is locked */
853 connection_closing( conn );
857 conn->c_sasl_bind_in_progress =
858 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
860 if( conn->c_conn_state == SLAP_C_BINDING) {
861 conn->c_conn_state = SLAP_C_ACTIVE;
865 connection_resched( conn );
867 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
872 int connection_read(ber_socket_t s)
876 assert( connections != NULL );
878 ldap_pvt_thread_mutex_lock( &connections_mutex );
880 /* get (locked) connection */
881 c = connection_get( s );
884 Debug( LDAP_DEBUG_ANY,
885 "connection_read(%ld): no connection!\n",
890 ldap_pvt_thread_mutex_unlock( &connections_mutex );
896 if( c->c_conn_state == SLAP_C_CLOSING ) {
897 Debug( LDAP_DEBUG_TRACE,
898 "connection_read(%d): closing, ignoring input for id=%ld\n",
901 connection_return( c );
902 ldap_pvt_thread_mutex_unlock( &connections_mutex );
906 Debug( LDAP_DEBUG_TRACE,
907 "connection_read(%d): checking for input on id=%ld\n",
911 if ( c->c_is_tls && c->c_needs_tls_accept ) {
912 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
917 Debug( LDAP_DEBUG_TRACE,
918 "connection_read(%d): TLS accept error "
919 "error=%d id=%ld, closing\n",
920 s, rc, c->c_connid );
922 c->c_needs_tls_accept = 0;
923 /* connections_mutex and c_mutex are locked */
924 connection_closing( c );
927 /* Drain input before close, to allow SSL error codes
928 * to propagate to client. */
935 rc = select(s+1, &rfd, NULL, NULL, &tv);
937 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
941 connection_close( c );
943 } else if ( rc == 0 ) {
947 c->c_needs_tls_accept = 0;
949 /* we need to let SASL know */
950 ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
952 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
953 if( c->c_tls_ssf > c->c_ssf ) {
954 c->c_ssf = c->c_tls_ssf;
957 authid = (char *)ldap_pvt_tls_get_peer( ssl );
958 slap_sasl_external( c, c->c_tls_ssf, authid );
960 connection_return( c );
961 ldap_pvt_thread_mutex_unlock( &connections_mutex );
966 #ifdef HAVE_CYRUS_SASL
967 if ( c->c_sasl_layers ) {
968 c->c_sasl_layers = 0;
970 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
972 if( rc != LDAP_SUCCESS ) {
973 Debug( LDAP_DEBUG_TRACE,
974 "connection_read(%d): SASL install error "
975 "error=%d id=%ld, closing\n",
976 s, rc, c->c_connid );
978 /* connections_mutex and c_mutex are locked */
979 connection_closing( c );
980 connection_close( c );
981 connection_return( c );
982 ldap_pvt_thread_mutex_unlock( &connections_mutex );
988 #define CONNECTION_INPUT_LOOP 1
990 #ifdef DATA_READY_LOOP
991 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
992 #elif CONNECTION_INPUT_LOOP
996 /* How do we do this without getting into a busy loop ? */
997 rc = connection_input( c );
1001 Debug( LDAP_DEBUG_TRACE,
1002 "connection_read(%d): input error=%d id=%ld, closing.\n",
1003 s, rc, c->c_connid );
1005 /* connections_mutex and c_mutex are locked */
1006 connection_closing( c );
1007 connection_close( c );
1008 connection_return( c );
1009 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1013 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1014 slapd_set_read( s, 1 );
1017 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1018 slapd_set_write( s, 1 );
1021 connection_return( c );
1022 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1037 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1039 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1045 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1046 if ( tag != LDAP_TAG_MESSAGE ) {
1050 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1052 Debug( LDAP_DEBUG_TRACE,
1053 "ber_get_next on fd %d failed errno=%d (%s)\n",
1054 sd, err, sock_errstr(err) );
1056 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1057 /* log, close and send error */
1058 ber_free( conn->c_currentber, 1 );
1059 conn->c_currentber = NULL;
1066 ber = conn->c_currentber;
1067 conn->c_currentber = NULL;
1069 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1070 /* log, close and send error */
1071 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1077 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1078 /* log, close and send error */
1079 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1086 if(tag == LDAP_REQ_BIND) {
1087 /* immediately abandon all exiting operations upon BIND */
1088 connection_abandon( conn );
1091 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1093 if ( conn->c_conn_state == SLAP_C_BINDING
1094 || conn->c_conn_state == SLAP_C_CLOSING )
1096 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1097 conn->c_n_ops_pending++;
1098 slap_op_add( &conn->c_pending_ops, op );
1101 conn->c_n_ops_executing++;
1102 connection_op_activate( conn, op );
1106 if ( conn->c_struct_state != SLAP_C_USED ) {
1107 /* connection must have got closed underneath us */
1111 assert( conn->c_struct_state == SLAP_C_USED );
1117 connection_resched( Connection *conn )
1121 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1124 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1126 /* us trylock to avoid possible deadlock */
1127 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1130 Debug( LDAP_DEBUG_TRACE,
1131 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1132 conn->c_connid, sd, 0 );
1134 * reaquire locks in the right order...
1135 * this may allow another thread to close this connection,
1136 * so recheck state below.
1138 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1139 ldap_pvt_thread_mutex_lock( &connections_mutex );
1140 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1143 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1144 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1145 "closed by other thread conn=%ld sd=%d\n",
1146 conn->c_connid, sd, 0 );
1149 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1150 "attempting closing conn=%ld sd=%d\n",
1151 conn->c_connid, sd, 0 );
1153 connection_close( conn );
1156 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1160 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1161 /* other states need different handling */
1165 for( op = slap_op_pop( &conn->c_pending_ops );
1167 op = slap_op_pop( &conn->c_pending_ops ) )
1169 /* pending operations should not be marked for abandonment */
1170 assert(!op->o_abandon);
1172 conn->c_n_ops_pending--;
1173 conn->c_n_ops_executing++;
1175 connection_op_activate( conn, op );
1177 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1184 static int connection_op_activate( Connection *conn, Operation *op )
1188 ber_tag_t tag = op->o_tag;
1190 if(tag == LDAP_REQ_BIND) {
1191 conn->c_conn_state = SLAP_C_BINDING;
1194 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1195 arg->co_conn = conn;
1198 arg->co_op->o_authz = conn->c_authz;
1199 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1200 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1201 (void) dn_normalize( arg->co_op->o_ndn );
1202 arg->co_op->o_authtype = conn->c_authtype;
1203 arg->co_op->o_authmech = conn->c_authmech != NULL
1204 ? ch_strdup( conn->c_authmech ) : NULL;
1206 arg->co_op->o_protocol = conn->c_protocol
1207 ? conn->c_protocol : LDAP_VERSION3;
1208 arg->co_op->o_connid = conn->c_connid;
1210 slap_op_add( &conn->c_ops, arg->co_op );
1212 status = ldap_pvt_thread_pool_submit( &connection_pool,
1213 connection_operation, (void *) arg );
1215 if ( status != 0 ) {
1216 Debug( LDAP_DEBUG_ANY,
1217 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1218 /* should move op to pending list */
1224 int connection_write(ber_socket_t s)
1227 assert( connections != NULL );
1229 ldap_pvt_thread_mutex_lock( &connections_mutex );
1231 c = connection_get( s );
1233 slapd_clr_write( s, 0);
1236 Debug( LDAP_DEBUG_ANY,
1237 "connection_write(%ld): no connection!\n",
1240 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1246 Debug( LDAP_DEBUG_TRACE,
1247 "connection_write(%d): waking output for id=%ld\n",
1248 s, c->c_connid, 0 );
1250 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1252 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1253 slapd_set_read( s, 1 );
1254 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1255 slapd_set_write( s, 1 );
1256 connection_return( c );
1257 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1263 * Create client side and server side connection structures, connected to
1264 * one another, for the front end to use for searches on arbitrary back ends.
1267 int connection_internal_open( Connection **conn, LDAP **ldp, char *id )
1270 ber_socket_t fd[2] = {-1,-1};
1279 return( LDAP_OPERATIONS_ERROR );
1281 rc = connection_init( fd[1], "INT", "localhost",
1282 "localhost:0", "localhost:00", 0, 256, id );
1286 return( LDAP_OPERATIONS_ERROR );
1288 slapd_add_internal( fd[1] );
1290 /* A search operation, number 0 */
1291 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1292 op->o_ndn = ch_strdup( id );
1293 op->o_protocol = LDAP_VERSION3;
1295 (*conn) = connection_get( fd[1] );
1296 (*conn)->c_ops = op;
1297 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1300 /* Create the client side of the connection */
1301 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1302 if( rc != LDAP_SUCCESS ) {
1304 return( LDAP_OPERATIONS_ERROR );
1307 /* The connection_get() will have locked the connection's mutex */
1308 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1310 return( LDAP_SUCCESS );
1314 void connection_internal_close( Connection *conn )
1316 connection_closing( conn );
1317 connection_close( conn );