3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
19 /* protected by connections_mutex */
20 static ldap_pvt_thread_mutex_t connections_mutex;
21 static Connection *connections = NULL;
22 static unsigned long conn_nextid = 0;
24 /* structure state (protected by connections_mutex) */
25 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
26 #define SLAP_C_UNUSED 0x01
27 #define SLAP_C_USED 0x02
29 /* connection state (protected by c_mutex ) */
30 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
31 #define SLAP_C_INACTIVE 0x01 /* zero threads */
32 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
33 #define SLAP_C_BINDING 0x03 /* binding */
34 #define SLAP_C_CLOSING 0x04 /* closing */
37 connection_state2str( int state )
40 case SLAP_C_INVALID: return "!";
41 case SLAP_C_INACTIVE: return "|";
42 case SLAP_C_ACTIVE: return "";
43 case SLAP_C_BINDING: return "B";
44 case SLAP_C_CLOSING: return "C";
50 static Connection* connection_get( ber_socket_t s );
52 static int connection_input( Connection *c );
53 static void connection_close( Connection *c );
55 static int connection_op_activate( Connection *conn, Operation *op );
56 static int connection_resched( Connection *conn );
57 static void connection_abandon( Connection *conn );
58 static void connection_destroy( Connection *c );
66 * Initialize connection management infrastructure.
68 int connections_init(void)
70 assert( connections == NULL );
72 if( connections != NULL) {
73 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
78 /* should check return of every call */
79 ldap_pvt_thread_mutex_init( &connections_mutex );
81 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
83 if( connections == NULL ) {
84 Debug( LDAP_DEBUG_ANY,
85 "connections_init: allocation (%d*%ld) of connection array failed\n",
86 dtblsize, (long) sizeof(Connection), 0 );
90 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
91 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
94 * per entry initialization of the Connection array initialization
95 * will be done by connection_init()
102 * Destroy connection management infrastructure.
104 int connections_destroy(void)
108 /* should check return of every call */
110 if( connections == NULL) {
111 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
116 for ( i = 0; i < dtblsize; i++ ) {
117 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
118 ber_sockbuf_free( connections[i].c_sb );
119 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
120 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
121 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
128 ldap_pvt_thread_mutex_destroy( &connections_mutex );
133 * shutdown all connections
135 int connections_shutdown(void)
139 ldap_pvt_thread_mutex_lock( &connections_mutex );
141 for ( i = 0; i < dtblsize; i++ ) {
142 if( connections[i].c_struct_state != SLAP_C_USED ) {
146 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
148 /* connections_mutex and c_mutex are locked */
149 connection_closing( &connections[i] );
150 connection_close( &connections[i] );
152 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
155 ldap_pvt_thread_mutex_unlock( &connections_mutex );
161 * Timeout idle connections.
163 int connections_timeout_idle(time_t now)
169 ldap_pvt_thread_mutex_lock( &connections_mutex );
171 for( c = connection_first( &connindex );
173 c = connection_next( c, &connindex ) )
175 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
177 connection_closing( c );
178 connection_close( c );
182 connection_done( c );
184 ldap_pvt_thread_mutex_unlock( &connections_mutex );
189 static Connection* connection_get( ber_socket_t s )
191 /* connections_mutex should be locked by caller */
195 Debug( LDAP_DEBUG_ARGS,
196 "connection_get(%ld)\n",
199 assert( connections != NULL );
201 if(s == AC_SOCKET_INVALID) {
208 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
215 for(i=0; i<dtblsize; i++) {
216 ber_sockbuf_ctrl( connections[i].c_sb,
217 LBER_SB_OPT_GET_FD, &sd );
219 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
220 assert( connections[i].c_conn_state == SLAP_C_INVALID );
221 assert( connections[i].c_sb == 0 );
225 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
226 assert( connections[i].c_conn_state == SLAP_C_INVALID );
227 assert( sd == AC_SOCKET_INVALID );
231 /* state can actually change from used -> unused by resched,
232 * so don't assert details here.
246 ldap_pvt_thread_mutex_lock( &c->c_mutex );
248 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
249 if( c->c_struct_state != SLAP_C_USED ) {
250 /* connection must have been closed due to resched */
252 assert( c->c_conn_state == SLAP_C_INVALID );
253 assert( sd == AC_SOCKET_INVALID );
255 Debug( LDAP_DEBUG_TRACE,
256 "connection_get(%d): connection not used\n",
259 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
263 Debug( LDAP_DEBUG_TRACE,
264 "connection_get(%d): got connid=%ld\n",
269 assert( c->c_struct_state == SLAP_C_USED );
270 assert( c->c_conn_state != SLAP_C_INVALID );
271 assert( sd != AC_SOCKET_INVALID );
273 c->c_activitytime = slap_get_time();
279 static void connection_return( Connection *c )
281 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
284 long connection_init(
288 const char* peername,
289 const char* sockname,
295 assert( connections != NULL );
297 assert( dnsname != NULL );
298 assert( peername != NULL );
299 assert( sockname != NULL );
305 if( s == AC_SOCKET_INVALID ) {
306 Debug( LDAP_DEBUG_ANY,
307 "connection_init(%ld): invalid.\n",
314 assert( s < dtblsize );
317 ldap_pvt_thread_mutex_lock( &connections_mutex );
328 for( i=0; i < dtblsize; i++) {
331 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
333 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
334 assert( connections[i].c_sb == 0 );
339 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
340 assert( sd == AC_SOCKET_INVALID );
345 assert( connections[i].c_struct_state == SLAP_C_USED );
346 assert( connections[i].c_conn_state != SLAP_C_INVALID );
347 assert( sd != AC_SOCKET_INVALID );
351 Debug( LDAP_DEBUG_ANY,
352 "connection_init(%d): connection table full (%d/%d)\n",
354 ldap_pvt_thread_mutex_unlock( &connections_mutex );
362 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
363 c->c_authmech = NULL;
367 c->c_listener_url = NULL;
368 c->c_peer_domain = NULL;
369 c->c_peer_name = NULL;
370 c->c_sock_name = NULL;
373 c->c_pending_ops = NULL;
375 c->c_sasl_bind_mech = NULL;
376 #ifdef HAVE_CYRUS_SASL
377 c->c_sasl_bind_context = NULL;
380 c->c_sb = ber_sockbuf_alloc( );
381 c->c_currentber = NULL;
383 /* should check status of thread calls */
384 ldap_pvt_thread_mutex_init( &c->c_mutex );
385 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
386 ldap_pvt_thread_cond_init( &c->c_write_cv );
388 c->c_struct_state = SLAP_C_UNUSED;
391 ldap_pvt_thread_mutex_lock( &c->c_mutex );
393 assert( c->c_struct_state == SLAP_C_UNUSED );
394 assert( c->c_authmech == NULL );
395 assert( c->c_dn == NULL );
396 assert( c->c_cdn == NULL );
397 assert( c->c_listener_url == NULL );
398 assert( c->c_peer_domain == NULL );
399 assert( c->c_peer_name == NULL );
400 assert( c->c_sock_name == NULL );
401 assert( c->c_ops == NULL );
402 assert( c->c_pending_ops == NULL );
403 assert( c->c_sasl_bind_mech == NULL );
404 #ifdef HAVE_CYRUS_SASL
405 assert( c->c_sasl_bind_context == NULL );
407 assert( c->c_currentber == NULL );
409 c->c_listener_url = ch_strdup( url );
410 c->c_peer_domain = ch_strdup( dnsname );
411 c->c_peer_name = ch_strdup( peername );
412 c->c_sock_name = ch_strdup( sockname );
414 c->c_n_ops_received = 0;
415 c->c_n_ops_executing = 0;
416 c->c_n_ops_pending = 0;
417 c->c_n_ops_completed = 0;
423 /* assume LDAPv3 until bind */
424 c->c_protocol = LDAP_VERSION3;
426 c->c_activitytime = c->c_starttime = slap_get_time();
428 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
430 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
431 LBER_SBIOD_LEVEL_PROVIDER, NULL );
433 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
435 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, (void *)1 ) < 0 ) {
436 Debug( LDAP_DEBUG_ANY,
437 "connection_init(%d, %s): set nonblocking failed\n",
438 s, c->c_peer_name,0 );
441 id = c->c_connid = conn_nextid++;
443 c->c_conn_state = SLAP_C_INACTIVE;
444 c->c_struct_state = SLAP_C_USED;
449 c->c_needs_tls_accept = 1;
452 c->c_needs_tls_accept = 0;
456 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
457 ldap_pvt_thread_mutex_unlock( &connections_mutex );
459 backend_connection_init(c);
465 connection_destroy( Connection *c )
467 /* note: connections_mutex should be locked by caller */
470 assert( connections != NULL );
472 assert( c->c_struct_state != SLAP_C_UNUSED );
473 assert( c->c_conn_state != SLAP_C_INVALID );
474 assert( c->c_ops == NULL );
476 backend_connection_destroy(c);
481 c->c_activitytime = c->c_starttime = 0;
483 if(c->c_authmech != NULL ) {
485 c->c_authmech = NULL;
487 if(c->c_dn != NULL) {
491 if(c->c_cdn != NULL) {
495 if(c->c_listener_url != NULL) {
496 free(c->c_listener_url);
497 c->c_listener_url = NULL;
499 if(c->c_peer_domain != NULL) {
500 free(c->c_peer_domain);
501 c->c_peer_domain = NULL;
503 if(c->c_peer_name != NULL) {
506 * If peer was a domain socket, unlink. Mind you,
507 * they may be un-named. Should we leave this to
510 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
511 char *path = c->c_peer_name + 5;
516 #endif /* LDAP_PF_LOCAL */
517 free(c->c_peer_name);
518 c->c_peer_name = NULL;
520 if(c->c_sock_name != NULL) {
521 free(c->c_sock_name);
522 c->c_sock_name = NULL;
525 c->c_sasl_bind_in_progress = 0;
526 if(c->c_sasl_bind_mech != NULL) {
527 free(c->c_sasl_bind_mech);
528 c->c_sasl_bind_mech = NULL;
530 #ifdef HAVE_CYRUS_SASL
531 if(c->c_sasl_bind_context != NULL ) {
532 sasl_dispose( &c->c_sasl_bind_context );
533 c->c_sasl_bind_context = NULL;
537 if ( c->c_currentber != NULL ) {
538 ber_free( c->c_currentber, 1 );
539 c->c_currentber = NULL;
542 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
543 if ( sd != AC_SOCKET_INVALID ) {
544 slapd_remove( sd, 0 );
546 Statslog( LDAP_DEBUG_STATS,
547 "conn=%ld fd=%d closed\n",
548 c->c_connid, sd, 0, 0, 0 );
551 ber_sockbuf_free( c->c_sb );
552 c->c_sb = ber_sockbuf_alloc( );
554 c->c_conn_state = SLAP_C_INVALID;
555 c->c_struct_state = SLAP_C_UNUSED;
558 int connection_state_closing( Connection *c )
560 /* c_mutex must be locked by caller */
564 assert( c->c_struct_state == SLAP_C_USED );
566 state = c->c_conn_state;
568 assert( state != SLAP_C_INVALID );
570 return state == SLAP_C_CLOSING;
573 static void connection_abandon( Connection *c )
575 /* c_mutex must be locked by caller */
579 for( o = c->c_ops; o != NULL; o = o->o_next ) {
580 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
582 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
585 /* remove pending operations */
586 for( o = slap_op_pop( &c->c_pending_ops );
588 o = slap_op_pop( &c->c_pending_ops ) )
594 void connection_closing( Connection *c )
596 assert( connections != NULL );
598 assert( c->c_struct_state == SLAP_C_USED );
599 assert( c->c_conn_state != SLAP_C_INVALID );
601 /* c_mutex must be locked by caller */
603 if( c->c_conn_state != SLAP_C_CLOSING ) {
606 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
607 Debug( LDAP_DEBUG_TRACE,
608 "connection_closing: readying conn=%ld sd=%d for close\n",
609 c->c_connid, sd, 0 );
611 /* update state to closing */
612 c->c_conn_state = SLAP_C_CLOSING;
614 /* don't listen on this port anymore */
615 slapd_clr_read( sd, 1 );
617 /* abandon active operations */
618 connection_abandon( c );
620 /* wake write blocked operations */
621 slapd_clr_write( sd, 1 );
622 ldap_pvt_thread_cond_signal( &c->c_write_cv );
626 static void connection_close( Connection *c )
630 assert( connections != NULL );
632 assert( c->c_struct_state == SLAP_C_USED );
633 assert( c->c_conn_state == SLAP_C_CLOSING );
635 /* note: connections_mutex and c_mutex should be locked by caller */
637 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
638 if( c->c_ops != NULL ) {
639 Debug( LDAP_DEBUG_TRACE,
640 "connection_close: deferring conn=%ld sd=%d\n",
641 c->c_connid, sd, 0 );
646 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
647 c->c_connid, sd, 0 );
649 connection_destroy( c );
652 unsigned long connections_nextid(void)
655 assert( connections != NULL );
657 ldap_pvt_thread_mutex_lock( &connections_mutex );
661 ldap_pvt_thread_mutex_unlock( &connections_mutex );
666 Connection* connection_first( ber_socket_t *index )
668 assert( connections != NULL );
669 assert( index != NULL );
671 ldap_pvt_thread_mutex_lock( &connections_mutex );
675 return connection_next(NULL, index);
678 Connection* connection_next( Connection *c, ber_socket_t *index )
680 assert( connections != NULL );
681 assert( index != NULL );
682 assert( *index <= dtblsize );
685 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
690 for(; *index < dtblsize; (*index)++) {
691 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
692 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
700 if( connections[*index].c_struct_state == SLAP_C_USED ) {
701 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
702 c = &connections[(*index)++];
706 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
707 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
711 ldap_pvt_thread_mutex_lock( &c->c_mutex );
717 void connection_done( Connection *c )
719 assert( connections != NULL );
722 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
725 ldap_pvt_thread_mutex_unlock( &connections_mutex );
729 * connection_activity - handle the request operation op on connection
730 * conn. This routine figures out what kind of operation it is and
731 * calls the appropriate stub to handle it.
735 connection_operation( void *arg_v )
738 struct co_arg *arg = arg_v;
739 ber_tag_t tag = arg->co_op->o_tag;
740 Connection *conn = arg->co_conn;
742 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
744 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
746 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
747 Debug( LDAP_DEBUG_ANY, "connection_operation: "
748 "error: SASL bind in progress (tag=%ld).\n",
750 send_ldap_result( conn, arg->co_op,
751 rc = LDAP_OPERATIONS_ERROR,
752 NULL, "SASL bind in progress", NULL, NULL );
753 goto operations_error;
758 rc = do_bind( conn, arg->co_op );
761 case LDAP_REQ_UNBIND:
762 rc = do_unbind( conn, arg->co_op );
766 rc = do_add( conn, arg->co_op );
769 case LDAP_REQ_DELETE:
770 rc = do_delete( conn, arg->co_op );
773 case LDAP_REQ_MODRDN:
774 rc = do_modrdn( conn, arg->co_op );
777 case LDAP_REQ_MODIFY:
778 rc = do_modify( conn, arg->co_op );
781 case LDAP_REQ_COMPARE:
782 rc = do_compare( conn, arg->co_op );
785 case LDAP_REQ_SEARCH:
786 rc = do_search( conn, arg->co_op );
789 case LDAP_REQ_ABANDON:
790 rc = do_abandon( conn, arg->co_op );
793 case LDAP_REQ_EXTENDED:
794 rc = do_extended( conn, arg->co_op );
798 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
800 arg->co_op->o_tag = LBER_ERROR;
801 send_ldap_disconnect( conn, arg->co_op,
802 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
807 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
810 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
812 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
814 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
816 conn->c_n_ops_executing--;
817 conn->c_n_ops_completed++;
819 slap_op_remove( &conn->c_ops, arg->co_op );
820 slap_op_free( arg->co_op );
823 free( (char *) arg );
828 case LDAP_REQ_UNBIND:
829 /* c_mutex is locked */
830 connection_closing( conn );
834 conn->c_sasl_bind_in_progress =
835 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
837 if( conn->c_conn_state == SLAP_C_BINDING) {
838 conn->c_conn_state = SLAP_C_ACTIVE;
842 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
844 if( active_threads < 1 ) {
845 ldap_pvt_thread_cond_signal(&active_threads_cond);
847 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
849 connection_resched( conn );
851 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
856 int connection_read(ber_socket_t s)
860 assert( connections != NULL );
862 ldap_pvt_thread_mutex_lock( &connections_mutex );
864 /* get (locked) connection */
865 c = connection_get( s );
868 Debug( LDAP_DEBUG_ANY,
869 "connection_read(%ld): no connection!\n",
874 ldap_pvt_thread_mutex_unlock( &connections_mutex );
880 if( c->c_conn_state == SLAP_C_CLOSING ) {
881 Debug( LDAP_DEBUG_TRACE,
882 "connection_read(%d): closing, ignoring input for id=%ld\n",
885 connection_return( c );
886 ldap_pvt_thread_mutex_unlock( &connections_mutex );
890 Debug( LDAP_DEBUG_TRACE,
891 "connection_read(%d): checking for input on id=%ld\n",
895 if ( c->c_is_tls && c->c_needs_tls_accept ) {
896 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
901 Debug( LDAP_DEBUG_TRACE,
902 "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
903 s, rc, c->c_connid );
905 c->c_needs_tls_accept = 0;
906 /* connections_mutex and c_mutex are locked */
907 connection_closing( c );
909 /* Drain input before close, to allow SSL error codes
910 * to propagate to client. */
917 rc = select(s+1, &rfd, NULL, NULL, &tv);
919 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
922 connection_close( c );
923 } else if ( rc == 0 ) {
924 c->c_needs_tls_accept = 0;
926 connection_return( c );
927 ldap_pvt_thread_mutex_unlock( &connections_mutex );
932 #define CONNECTION_INPUT_LOOP 1
934 #ifdef DATA_READY_LOOP
935 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
936 #elif CONNECTION_INPUT_LOOP
940 /* How do we do this without getting into a busy loop ? */
941 rc = connection_input( c );
945 Debug( LDAP_DEBUG_TRACE,
946 "connection_read(%d): input error=%d id=%ld, closing.\n",
947 s, rc, c->c_connid );
949 /* connections_mutex and c_mutex are locked */
950 connection_closing( c );
951 connection_close( c );
954 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
955 slapd_set_read( s, 1 );
956 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
957 slapd_set_write( s, 1 );
958 connection_return( c );
959 ldap_pvt_thread_mutex_unlock( &connections_mutex );
974 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
976 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
981 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
982 != LDAP_TAG_MESSAGE )
987 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
989 Debug( LDAP_DEBUG_TRACE,
990 "ber_get_next on fd %d failed errno=%d (%s)\n",
991 sd, err, sock_errstr(err) );
993 if ( err != EWOULDBLOCK && err != EAGAIN ) {
994 /* log, close and send error */
995 ber_free( conn->c_currentber, 1 );
996 conn->c_currentber = NULL;
1003 ber = conn->c_currentber;
1004 conn->c_currentber = NULL;
1006 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1007 /* log, close and send error */
1008 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1014 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1015 /* log, close and send error */
1016 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1023 if(tag == LDAP_REQ_BIND) {
1024 /* immediately abandon all exiting operations upon BIND */
1025 connection_abandon( conn );
1028 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1030 if ( conn->c_conn_state == SLAP_C_BINDING
1031 || conn->c_conn_state == SLAP_C_CLOSING )
1033 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1034 conn->c_n_ops_pending++;
1035 slap_op_add( &conn->c_pending_ops, op );
1038 conn->c_n_ops_executing++;
1039 connection_op_activate( conn, op );
1043 if ( conn->c_struct_state != SLAP_C_USED ) {
1044 /* connection must have got closed underneath us */
1048 assert( conn->c_struct_state == SLAP_C_USED );
1054 connection_resched( Connection *conn )
1058 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1061 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1062 Debug( LDAP_DEBUG_TRACE,
1063 "connection_resched: attempting closing conn=%ld sd=%d\n",
1064 conn->c_connid, sd, 0 );
1066 connection_close( conn );
1070 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1071 /* other states need different handling */
1075 for( op = slap_op_pop( &conn->c_pending_ops );
1077 op = slap_op_pop( &conn->c_pending_ops ) )
1079 /* pending operations should not be marked for abandonment */
1080 assert(!op->o_abandon);
1082 conn->c_n_ops_pending--;
1083 conn->c_n_ops_executing++;
1085 connection_op_activate( conn, op );
1087 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1094 static int connection_op_activate( Connection *conn, Operation *op )
1099 ber_tag_t tag = op->o_tag;
1101 if(tag == LDAP_REQ_BIND) {
1102 conn->c_conn_state = SLAP_C_BINDING;
1105 if ( conn->c_dn != NULL ) {
1106 tmpdn = ch_strdup( conn->c_dn );
1111 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1112 arg->co_conn = conn;
1115 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1116 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1117 (void) dn_normalize( arg->co_op->o_ndn );
1119 arg->co_op->o_protocol = conn->c_protocol;
1120 arg->co_op->o_connid = conn->c_connid;
1122 arg->co_op->o_authtype = conn->c_authtype;
1123 arg->co_op->o_authmech = conn->c_authmech != NULL
1124 ? ch_strdup( conn->c_authmech ) : NULL;
1126 slap_op_add( &conn->c_ops, arg->co_op );
1128 if( tmpdn != NULL ) {
1132 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1134 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1136 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1137 connection_operation, (void *) arg );
1139 if ( status != 0 ) {
1140 Debug( LDAP_DEBUG_ANY,
1141 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1143 /* should move op to pending list */
1149 int connection_write(ber_socket_t s)
1152 assert( connections != NULL );
1154 ldap_pvt_thread_mutex_lock( &connections_mutex );
1156 c = connection_get( s );
1158 slapd_clr_write( s, 0);
1161 Debug( LDAP_DEBUG_ANY,
1162 "connection_write(%ld): no connection!\n",
1165 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1171 Debug( LDAP_DEBUG_TRACE,
1172 "connection_write(%d): waking output for id=%ld\n",
1173 s, c->c_connid, 0 );
1175 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1177 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1178 slapd_set_read( s, 1 );
1179 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1180 slapd_set_write( s, 1 );
1181 connection_return( c );
1182 ldap_pvt_thread_mutex_unlock( &connections_mutex );