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 ldap_pvt_thread_mutex_lock( &connections_mutex );
174 for( c = connection_first( &connindex );
176 c = connection_next( c, &connindex ) )
178 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
180 connection_closing( c );
181 connection_close( c );
185 connection_done( c );
187 ldap_pvt_thread_mutex_unlock( &connections_mutex );
192 static Connection* connection_get( ber_socket_t s )
194 /* connections_mutex should be locked by caller */
198 Debug( LDAP_DEBUG_ARGS,
199 "connection_get(%ld)\n",
202 assert( connections != NULL );
204 if(s == AC_SOCKET_INVALID) {
211 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
218 for(i=0; i<dtblsize; i++) {
219 ber_sockbuf_ctrl( connections[i].c_sb,
220 LBER_SB_OPT_GET_FD, &sd );
222 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
223 assert( connections[i].c_conn_state == SLAP_C_INVALID );
224 assert( connections[i].c_sb == 0 );
228 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
229 assert( connections[i].c_conn_state == SLAP_C_INVALID );
230 assert( sd == AC_SOCKET_INVALID );
234 /* state can actually change from used -> unused by resched,
235 * so don't assert details here.
249 ldap_pvt_thread_mutex_lock( &c->c_mutex );
251 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
252 if( c->c_struct_state != SLAP_C_USED ) {
253 /* connection must have been closed due to resched */
255 assert( c->c_conn_state == SLAP_C_INVALID );
256 assert( sd == AC_SOCKET_INVALID );
258 Debug( LDAP_DEBUG_TRACE,
259 "connection_get(%d): connection not used\n",
262 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
266 Debug( LDAP_DEBUG_TRACE,
267 "connection_get(%d): got connid=%ld\n",
272 assert( c->c_struct_state == SLAP_C_USED );
273 assert( c->c_conn_state != SLAP_C_INVALID );
274 assert( sd != AC_SOCKET_INVALID );
276 c->c_activitytime = slap_get_time();
282 static void connection_return( Connection *c )
284 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
287 long connection_init(
291 const char* peername,
292 const char* sockname,
300 assert( connections != NULL );
302 assert( dnsname != NULL );
303 assert( peername != NULL );
304 assert( sockname != NULL );
310 if( s == AC_SOCKET_INVALID ) {
311 Debug( LDAP_DEBUG_ANY,
312 "connection_init(%ld): invalid.\n",
319 assert( s < dtblsize );
322 ldap_pvt_thread_mutex_lock( &connections_mutex );
333 for( i=0; i < dtblsize; i++) {
336 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
337 assert( connections[i].c_sb == 0 );
342 sd = AC_SOCKET_INVALID;
343 if (connections[i].c_sb != NULL)
344 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
346 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
347 assert( sd == AC_SOCKET_INVALID );
352 assert( connections[i].c_struct_state == SLAP_C_USED );
353 assert( connections[i].c_conn_state != SLAP_C_INVALID );
354 assert( sd != AC_SOCKET_INVALID );
358 Debug( LDAP_DEBUG_ANY,
359 "connection_init(%d): connection table full (%d/%d)\n",
361 ldap_pvt_thread_mutex_unlock( &connections_mutex );
369 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
370 c->c_authmech = NULL;
374 c->c_listener_url = NULL;
375 c->c_peer_domain = NULL;
376 c->c_peer_name = NULL;
377 c->c_sock_name = NULL;
380 c->c_pending_ops = NULL;
382 c->c_sasl_bind_mech = NULL;
383 c->c_sasl_context = NULL;
384 c->c_sasl_extra = NULL;
386 c->c_sb = ber_sockbuf_alloc( );
387 c->c_currentber = NULL;
389 /* should check status of thread calls */
390 ldap_pvt_thread_mutex_init( &c->c_mutex );
391 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
392 ldap_pvt_thread_cond_init( &c->c_write_cv );
394 c->c_struct_state = SLAP_C_UNUSED;
397 ldap_pvt_thread_mutex_lock( &c->c_mutex );
399 assert( c->c_struct_state == SLAP_C_UNUSED );
400 assert( c->c_authmech == NULL );
401 assert( c->c_dn == NULL );
402 assert( c->c_cdn == NULL );
403 assert( c->c_listener_url == NULL );
404 assert( c->c_peer_domain == NULL );
405 assert( c->c_peer_name == NULL );
406 assert( c->c_sock_name == NULL );
407 assert( c->c_ops == NULL );
408 assert( c->c_pending_ops == NULL );
409 assert( c->c_sasl_bind_mech == NULL );
410 assert( c->c_sasl_context == NULL );
411 assert( c->c_sasl_extra == NULL );
412 assert( c->c_currentber == NULL );
414 c->c_listener_url = ch_strdup( url );
415 c->c_peer_domain = ch_strdup( dnsname );
416 c->c_peer_name = ch_strdup( peername );
417 c->c_sock_name = ch_strdup( sockname );
419 c->c_n_ops_received = 0;
420 c->c_n_ops_executing = 0;
421 c->c_n_ops_pending = 0;
422 c->c_n_ops_completed = 0;
428 /* set to zero until bind, implies LDAP_VERSION3 */
431 c->c_activitytime = c->c_starttime = slap_get_time();
433 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
435 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
436 LBER_SBIOD_LEVEL_PROVIDER, NULL );
438 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
440 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
441 Debug( LDAP_DEBUG_ANY,
442 "connection_init(%d, %s): set nonblocking failed\n",
443 s, c->c_peer_name, 0 );
447 id = c->c_connid = conn_nextid++;
449 c->c_conn_state = SLAP_C_INACTIVE;
450 c->c_struct_state = SLAP_C_USED;
452 c->c_ssf = c->c_transport_ssf = ssf;
458 c->c_needs_tls_accept = 1;
461 c->c_needs_tls_accept = 0;
466 slap_sasl_external( c, ssf, authid );
468 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
469 ldap_pvt_thread_mutex_unlock( &connections_mutex );
471 backend_connection_init(c);
477 connection_destroy( Connection *c )
479 /* note: connections_mutex should be locked by caller */
482 assert( connections != NULL );
484 assert( c->c_struct_state != SLAP_C_UNUSED );
485 assert( c->c_conn_state != SLAP_C_INVALID );
486 assert( c->c_ops == NULL );
488 backend_connection_destroy(c);
493 c->c_activitytime = c->c_starttime = 0;
495 if(c->c_authmech != NULL ) {
497 c->c_authmech = NULL;
499 if(c->c_dn != NULL) {
503 if(c->c_cdn != NULL) {
507 if(c->c_listener_url != NULL) {
508 free(c->c_listener_url);
509 c->c_listener_url = NULL;
511 if(c->c_peer_domain != NULL) {
512 free(c->c_peer_domain);
513 c->c_peer_domain = NULL;
515 if(c->c_peer_name != NULL) {
518 * If peer was a domain socket, unlink. Mind you,
519 * they may be un-named. Should we leave this to
522 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
523 char *path = c->c_peer_name + 5;
528 #endif /* LDAP_PF_LOCAL */
530 free(c->c_peer_name);
531 c->c_peer_name = NULL;
533 if(c->c_sock_name != NULL) {
534 free(c->c_sock_name);
535 c->c_sock_name = NULL;
538 c->c_sasl_bind_in_progress = 0;
539 if(c->c_sasl_bind_mech != NULL) {
540 free(c->c_sasl_bind_mech);
541 c->c_sasl_bind_mech = NULL;
544 slap_sasl_close( c );
546 if ( c->c_currentber != NULL ) {
547 ber_free( c->c_currentber, 1 );
548 c->c_currentber = NULL;
551 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
552 if ( sd != AC_SOCKET_INVALID ) {
553 slapd_remove( sd, 0 );
555 Statslog( LDAP_DEBUG_STATS,
556 "conn=%ld fd=%d closed\n",
557 c->c_connid, sd, 0, 0, 0 );
560 ber_sockbuf_free( c->c_sb );
561 c->c_sb = ber_sockbuf_alloc( );
563 c->c_conn_state = SLAP_C_INVALID;
564 c->c_struct_state = SLAP_C_UNUSED;
567 int connection_state_closing( Connection *c )
569 /* c_mutex must be locked by caller */
573 assert( c->c_struct_state == SLAP_C_USED );
575 state = c->c_conn_state;
577 assert( state != SLAP_C_INVALID );
579 return state == SLAP_C_CLOSING;
582 static void connection_abandon( Connection *c )
584 /* c_mutex must be locked by caller */
588 for( o = c->c_ops; o != NULL; o = o->o_next ) {
589 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
591 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
594 /* remove pending operations */
595 for( o = slap_op_pop( &c->c_pending_ops );
597 o = slap_op_pop( &c->c_pending_ops ) )
603 void connection_closing( Connection *c )
605 assert( connections != NULL );
607 assert( c->c_struct_state == SLAP_C_USED );
608 assert( c->c_conn_state != SLAP_C_INVALID );
610 /* c_mutex must be locked by caller */
612 if( c->c_conn_state != SLAP_C_CLOSING ) {
615 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
616 Debug( LDAP_DEBUG_TRACE,
617 "connection_closing: readying conn=%ld sd=%d for close\n",
618 c->c_connid, sd, 0 );
620 /* update state to closing */
621 c->c_conn_state = SLAP_C_CLOSING;
623 /* don't listen on this port anymore */
624 slapd_clr_read( sd, 1 );
626 /* abandon active operations */
627 connection_abandon( c );
629 /* wake write blocked operations */
630 slapd_clr_write( sd, 1 );
631 ldap_pvt_thread_cond_signal( &c->c_write_cv );
635 static void connection_close( Connection *c )
639 assert( connections != NULL );
641 assert( c->c_struct_state == SLAP_C_USED );
642 assert( c->c_conn_state == SLAP_C_CLOSING );
644 /* note: connections_mutex and c_mutex should be locked by caller */
646 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
647 if( c->c_ops != NULL ) {
648 Debug( LDAP_DEBUG_TRACE,
649 "connection_close: deferring conn=%ld sd=%d\n",
650 c->c_connid, sd, 0 );
655 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
656 c->c_connid, sd, 0 );
658 connection_destroy( c );
661 unsigned long connections_nextid(void)
664 assert( connections != NULL );
666 ldap_pvt_thread_mutex_lock( &connections_mutex );
670 ldap_pvt_thread_mutex_unlock( &connections_mutex );
675 Connection* connection_first( ber_socket_t *index )
677 assert( connections != NULL );
678 assert( index != NULL );
680 ldap_pvt_thread_mutex_lock( &connections_mutex );
684 return connection_next(NULL, index);
687 Connection* connection_next( Connection *c, ber_socket_t *index )
689 assert( connections != NULL );
690 assert( index != NULL );
691 assert( *index <= dtblsize );
694 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
699 for(; *index < dtblsize; (*index)++) {
700 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
701 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
709 if( connections[*index].c_struct_state == SLAP_C_USED ) {
710 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
711 c = &connections[(*index)++];
715 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
716 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
720 ldap_pvt_thread_mutex_lock( &c->c_mutex );
726 void connection_done( Connection *c )
728 assert( connections != NULL );
731 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
734 ldap_pvt_thread_mutex_unlock( &connections_mutex );
738 * connection_activity - handle the request operation op on connection
739 * conn. This routine figures out what kind of operation it is and
740 * calls the appropriate stub to handle it.
744 connection_operation( void *arg_v )
747 struct co_arg *arg = arg_v;
748 ber_tag_t tag = arg->co_op->o_tag;
749 Connection *conn = arg->co_conn;
751 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
753 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
755 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
756 Debug( LDAP_DEBUG_ANY, "connection_operation: "
757 "error: SASL bind in progress (tag=%ld).\n",
759 send_ldap_result( conn, arg->co_op,
760 rc = LDAP_OPERATIONS_ERROR,
761 NULL, "SASL bind in progress", NULL, NULL );
762 goto operations_error;
767 rc = do_bind( conn, arg->co_op );
770 case LDAP_REQ_UNBIND:
771 rc = do_unbind( conn, arg->co_op );
775 rc = do_add( conn, arg->co_op );
778 case LDAP_REQ_DELETE:
779 rc = do_delete( conn, arg->co_op );
782 case LDAP_REQ_MODRDN:
783 rc = do_modrdn( conn, arg->co_op );
786 case LDAP_REQ_MODIFY:
787 rc = do_modify( conn, arg->co_op );
790 case LDAP_REQ_COMPARE:
791 rc = do_compare( conn, arg->co_op );
794 case LDAP_REQ_SEARCH:
795 rc = do_search( conn, arg->co_op );
798 case LDAP_REQ_ABANDON:
799 rc = do_abandon( conn, arg->co_op );
802 case LDAP_REQ_EXTENDED:
803 rc = do_extended( conn, arg->co_op );
807 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
809 arg->co_op->o_tag = LBER_ERROR;
810 send_ldap_disconnect( conn, arg->co_op,
811 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
816 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
819 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
821 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
823 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
825 conn->c_n_ops_executing--;
826 conn->c_n_ops_completed++;
828 slap_op_remove( &conn->c_ops, arg->co_op );
829 slap_op_free( arg->co_op );
832 free( (char *) arg );
837 case LDAP_REQ_UNBIND:
838 /* c_mutex is locked */
839 connection_closing( conn );
843 conn->c_sasl_bind_in_progress =
844 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
846 if( conn->c_conn_state == SLAP_C_BINDING) {
847 conn->c_conn_state = SLAP_C_ACTIVE;
851 connection_resched( conn );
853 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
858 int connection_read(ber_socket_t s)
862 assert( connections != NULL );
864 ldap_pvt_thread_mutex_lock( &connections_mutex );
866 /* get (locked) connection */
867 c = connection_get( s );
870 Debug( LDAP_DEBUG_ANY,
871 "connection_read(%ld): no connection!\n",
876 ldap_pvt_thread_mutex_unlock( &connections_mutex );
882 if( c->c_conn_state == SLAP_C_CLOSING ) {
883 Debug( LDAP_DEBUG_TRACE,
884 "connection_read(%d): closing, ignoring input for id=%ld\n",
887 connection_return( c );
888 ldap_pvt_thread_mutex_unlock( &connections_mutex );
892 Debug( LDAP_DEBUG_TRACE,
893 "connection_read(%d): checking for input on id=%ld\n",
897 if ( c->c_is_tls && c->c_needs_tls_accept ) {
898 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
903 Debug( LDAP_DEBUG_TRACE,
904 "connection_read(%d): TLS accept error "
905 "error=%d id=%ld, closing\n",
906 s, rc, c->c_connid );
908 c->c_needs_tls_accept = 0;
909 /* connections_mutex and c_mutex are locked */
910 connection_closing( c );
913 /* Drain input before close, to allow SSL error codes
914 * to propagate to client. */
921 rc = select(s+1, &rfd, NULL, NULL, &tv);
923 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
927 connection_close( c );
929 } else if ( rc == 0 ) {
933 c->c_needs_tls_accept = 0;
935 /* we need to let SASL know */
936 ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
938 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
939 if( c->c_tls_ssf > c->c_ssf ) {
940 c->c_ssf = c->c_tls_ssf;
943 authid = (char *)ldap_pvt_tls_get_peer( ssl );
944 slap_sasl_external( c, c->c_tls_ssf, authid );
946 connection_return( c );
947 ldap_pvt_thread_mutex_unlock( &connections_mutex );
952 #ifdef HAVE_CYRUS_SASL
953 if ( c->c_sasl_layers ) {
954 c->c_sasl_layers = 0;
956 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
958 if( rc != LDAP_SUCCESS ) {
959 Debug( LDAP_DEBUG_TRACE,
960 "connection_read(%d): SASL install error "
961 "error=%d id=%ld, closing\n",
962 s, rc, c->c_connid );
964 /* connections_mutex and c_mutex are locked */
965 connection_closing( c );
966 connection_close( c );
967 connection_return( c );
968 ldap_pvt_thread_mutex_unlock( &connections_mutex );
974 #define CONNECTION_INPUT_LOOP 1
976 #ifdef DATA_READY_LOOP
977 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
978 #elif CONNECTION_INPUT_LOOP
982 /* How do we do this without getting into a busy loop ? */
983 rc = connection_input( c );
987 Debug( LDAP_DEBUG_TRACE,
988 "connection_read(%d): input error=%d id=%ld, closing.\n",
989 s, rc, c->c_connid );
991 /* connections_mutex and c_mutex are locked */
992 connection_closing( c );
993 connection_close( c );
996 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
997 slapd_set_read( s, 1 );
998 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
999 slapd_set_write( s, 1 );
1000 connection_return( c );
1001 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1016 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1018 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1024 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1025 if ( tag != LDAP_TAG_MESSAGE ) {
1029 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1031 Debug( LDAP_DEBUG_TRACE,
1032 "ber_get_next on fd %d failed errno=%d (%s)\n",
1033 sd, err, sock_errstr(err) );
1035 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1036 /* log, close and send error */
1037 ber_free( conn->c_currentber, 1 );
1038 conn->c_currentber = NULL;
1045 ber = conn->c_currentber;
1046 conn->c_currentber = NULL;
1048 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1049 /* log, close and send error */
1050 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1056 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1057 /* log, close and send error */
1058 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1065 if(tag == LDAP_REQ_BIND) {
1066 /* immediately abandon all exiting operations upon BIND */
1067 connection_abandon( conn );
1070 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1072 if ( conn->c_conn_state == SLAP_C_BINDING
1073 || conn->c_conn_state == SLAP_C_CLOSING )
1075 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1076 conn->c_n_ops_pending++;
1077 slap_op_add( &conn->c_pending_ops, op );
1080 conn->c_n_ops_executing++;
1081 connection_op_activate( conn, op );
1085 if ( conn->c_struct_state != SLAP_C_USED ) {
1086 /* connection must have got closed underneath us */
1090 assert( conn->c_struct_state == SLAP_C_USED );
1096 connection_resched( Connection *conn )
1100 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1103 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1105 /* us trylock to avoid possible deadlock */
1106 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1109 Debug( LDAP_DEBUG_TRACE,
1110 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1111 conn->c_connid, sd, 0 );
1113 * reaquire locks in the right order...
1114 * this may allow another thread to close this connection,
1115 * so recheck state below.
1117 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1118 ldap_pvt_thread_mutex_lock( &connections_mutex );
1119 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1122 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1123 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1124 "closed by other thread conn=%ld sd=%d\n",
1125 conn->c_connid, sd, 0 );
1128 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1129 "attempting closing conn=%ld sd=%d\n",
1130 conn->c_connid, sd, 0 );
1132 connection_close( conn );
1135 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1139 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1140 /* other states need different handling */
1144 for( op = slap_op_pop( &conn->c_pending_ops );
1146 op = slap_op_pop( &conn->c_pending_ops ) )
1148 /* pending operations should not be marked for abandonment */
1149 assert(!op->o_abandon);
1151 conn->c_n_ops_pending--;
1152 conn->c_n_ops_executing++;
1154 connection_op_activate( conn, op );
1156 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1163 static int connection_op_activate( Connection *conn, Operation *op )
1167 ber_tag_t tag = op->o_tag;
1169 if(tag == LDAP_REQ_BIND) {
1170 conn->c_conn_state = SLAP_C_BINDING;
1173 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1174 arg->co_conn = conn;
1177 arg->co_op->o_authz = conn->c_authz;
1178 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1179 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1180 (void) dn_normalize( arg->co_op->o_ndn );
1181 arg->co_op->o_authtype = conn->c_authtype;
1182 arg->co_op->o_authmech = conn->c_authmech != NULL
1183 ? ch_strdup( conn->c_authmech ) : NULL;
1185 arg->co_op->o_protocol = conn->c_protocol
1186 ? conn->c_protocol : LDAP_VERSION3;
1187 arg->co_op->o_connid = conn->c_connid;
1189 slap_op_add( &conn->c_ops, arg->co_op );
1191 status = ldap_pvt_thread_pool_submit( &connection_pool,
1192 connection_operation, (void *) arg );
1194 if ( status != 0 ) {
1195 Debug( LDAP_DEBUG_ANY,
1196 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1197 /* should move op to pending list */
1203 int connection_write(ber_socket_t s)
1206 assert( connections != NULL );
1208 ldap_pvt_thread_mutex_lock( &connections_mutex );
1210 c = connection_get( s );
1212 slapd_clr_write( s, 0);
1215 Debug( LDAP_DEBUG_ANY,
1216 "connection_write(%ld): no connection!\n",
1219 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1225 Debug( LDAP_DEBUG_TRACE,
1226 "connection_write(%d): waking output for id=%ld\n",
1227 s, c->c_connid, 0 );
1229 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1231 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1232 slapd_set_read( s, 1 );
1233 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1234 slapd_set_write( s, 1 );
1235 connection_return( c );
1236 ldap_pvt_thread_mutex_unlock( &connections_mutex );