]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
483c7d68b228ed04ad448ec2a8ecc68960839202
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/socket.h>
12 #include <ac/errno.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include "slap.h"
18
19 /* we need LBER internals */
20 #include "../../libraries/liblber/lber-int.h"
21
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;
26
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
31
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 */
38
39 const char *
40 connection_state2str( int state )
41 {
42         switch( 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";                     
48         }
49
50         return "?";
51 }
52
53 static Connection* connection_get( ber_socket_t s );
54
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
57
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 );
62
63 struct co_arg {
64         Connection      *co_conn;
65         Operation       *co_op;
66 };
67
68 /*
69  * Initialize connection management infrastructure.
70  */
71 int connections_init(void)
72 {
73         assert( connections == NULL );
74
75         if( connections != NULL) {
76                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
77                         0, 0, 0 );
78                 return -1;
79         }
80
81         /* should check return of every call */
82         ldap_pvt_thread_mutex_init( &connections_mutex );
83
84         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
85
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 );
90                 return -1;
91         }
92
93     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
94     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
95
96         /*
97          * per entry initialization of the Connection array initialization
98          * will be done by connection_init()
99          */ 
100
101         return 0;
102 }
103
104 /*
105  * Destroy connection management infrastructure.
106  */
107 int connections_destroy(void)
108 {
109         ber_socket_t i;
110
111         /* should check return of every call */
112
113         if( connections == NULL) {
114                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
115                         0, 0, 0 );
116                 return -1;
117         }
118
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 );
125                 }
126         }
127
128         free( connections );
129         connections = NULL;
130
131         ldap_pvt_thread_mutex_destroy( &connections_mutex );
132         return 0;
133 }
134
135 /*
136  * shutdown all connections
137  */
138 int connections_shutdown(void)
139 {
140         ber_socket_t i;
141
142         ldap_pvt_thread_mutex_lock( &connections_mutex );
143
144         for ( i = 0; i < dtblsize; i++ ) {
145                 if( connections[i].c_struct_state != SLAP_C_USED ) {
146                         continue;
147                 }
148
149                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
150
151                 /* connections_mutex and c_mutex are locked */
152                 connection_closing( &connections[i] );
153                 connection_close( &connections[i] );
154
155                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
156         }
157
158         ldap_pvt_thread_mutex_unlock( &connections_mutex );
159
160         return 0;
161 }
162
163 /*
164  * Timeout idle connections.
165  */
166 int connections_timeout_idle(time_t now)
167 {
168         int i = 0;
169         int connindex;
170         Connection* c;
171
172         ldap_pvt_thread_mutex_lock( &connections_mutex );
173
174         for( c = connection_first( &connindex );
175                 c != NULL;
176                 c = connection_next( c, &connindex ) )
177         {
178                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
179                         /* close it */
180                         connection_closing( c );
181                         connection_close( c );
182                         i++;
183                 }
184         }
185         connection_done( c );
186
187         ldap_pvt_thread_mutex_unlock( &connections_mutex );
188
189         return i;
190 }
191
192 static Connection* connection_get( ber_socket_t s )
193 {
194         /* connections_mutex should be locked by caller */
195
196         Connection *c;
197
198         Debug( LDAP_DEBUG_ARGS,
199                 "connection_get(%ld)\n",
200                 (long) s, 0, 0 );
201
202         assert( connections != NULL );
203
204         if(s == AC_SOCKET_INVALID) {
205                 return NULL;
206         }
207
208 #ifndef HAVE_WINSOCK
209         c = &connections[s];
210
211         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
212
213 #else
214         c = NULL;
215         {
216                 ber_socket_t i;
217
218                 for(i=0; i<dtblsize; i++) {
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 );
222                                 break;
223                         }
224
225                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
226                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
227                                 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
228                                 continue;
229                         }
230
231                         /* state can actually change from used -> unused by resched,
232                          * so don't assert details here.
233                          */
234
235                         if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
236                                 c = &connections[i];
237                                 break;
238                         }
239                 }
240         }
241 #endif
242
243         if( c != NULL ) {
244                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
245
246                 if( c->c_struct_state != SLAP_C_USED ) {
247                         /* connection must have been closed due to resched */
248
249                         assert( c->c_conn_state == SLAP_C_INVALID );
250                         assert( !ber_pvt_sb_in_use( c->c_sb ) );
251
252                         Debug( LDAP_DEBUG_TRACE,
253                                 "connection_get(%d): connection not used\n",
254                                 s, 0, 0 );
255
256                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
257                         return NULL;
258                 }
259
260                 Debug( LDAP_DEBUG_TRACE,
261                         "connection_get(%d): got connid=%ld\n",
262                         s, c->c_connid, 0 );
263
264                 c->c_n_get++;
265
266                 assert( c->c_struct_state == SLAP_C_USED );
267                 assert( c->c_conn_state != SLAP_C_INVALID );
268                 assert( ber_pvt_sb_in_use( c->c_sb ) );
269
270         c->c_activitytime = slap_get_time();
271         }
272
273         return c;
274 }
275
276 static void connection_return( Connection *c )
277 {
278         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
279 }
280
281 long connection_init(
282         ber_socket_t s,
283         const char* url,
284         const char* dnsname,
285         const char* peername,
286         const char* sockname,
287         int use_tls )
288 {
289         unsigned long id;
290         Connection *c;
291
292         assert( connections != NULL );
293
294         assert( dnsname != NULL );
295         assert( peername != NULL );
296         assert( sockname != NULL );
297
298 #ifndef HAVE_TLS
299         assert( !use_tls );
300 #endif
301
302         if( s == AC_SOCKET_INVALID ) {
303         Debug( LDAP_DEBUG_ANY,
304                         "connection_init(%ld): invalid.\n",
305                         (long) s, 0, 0 );
306                 return -1;
307         }
308
309         assert( s >= 0 );
310 #ifndef HAVE_WINSOCK
311         assert( s < dtblsize );
312 #endif
313
314         ldap_pvt_thread_mutex_lock( &connections_mutex );
315
316 #ifndef HAVE_WINSOCK
317         c = &connections[s];
318
319 #else
320         {
321                 unsigned int i;
322
323                 c = NULL;
324
325         for( i=0; i < dtblsize; i++) {
326             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
327                 assert( connections[i].c_sb == 0 );
328                 c = &connections[i];
329                 break;
330             }
331
332             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
333                 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
334                 c = &connections[i];
335                 break;
336             }
337
338             assert( connections[i].c_struct_state == SLAP_C_USED );
339             assert( connections[i].c_conn_state != SLAP_C_INVALID );
340             assert( ber_pvt_sb_in_use( connections[i].c_sb ));
341         }
342
343         if( c == NULL ) {
344                 Debug( LDAP_DEBUG_ANY,
345                                 "connection_init(%d): connection table full (%d/%d)\n",
346                                 s, i, dtblsize);
347             ldap_pvt_thread_mutex_unlock( &connections_mutex );
348             return -1;
349         }
350     }
351 #endif
352
353     assert( c != NULL );
354
355     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
356         c->c_dn = NULL;
357         c->c_cdn = NULL;
358
359                 c->c_listener_url = NULL;
360                 c->c_peer_domain = NULL;
361         c->c_peer_name = NULL;
362         c->c_sock_name = NULL;
363
364         c->c_ops = NULL;
365         c->c_pending_ops = NULL;
366                 c->c_authmech = NULL;
367                 c->c_authstate = NULL;
368
369 #ifdef HAVE_CYRUS_SASL
370                 c->c_sasl_context = NULL;
371 #endif
372
373         c->c_sb = ber_sockbuf_alloc( );
374                 c->c_currentber = NULL;
375
376         /* should check status of thread calls */
377         ldap_pvt_thread_mutex_init( &c->c_mutex );
378         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
379         ldap_pvt_thread_cond_init( &c->c_write_cv );
380
381         c->c_struct_state = SLAP_C_UNUSED;
382     }
383
384     ldap_pvt_thread_mutex_lock( &c->c_mutex );
385
386     assert( c->c_struct_state == SLAP_C_UNUSED );
387     assert(     c->c_dn == NULL );
388     assert(     c->c_cdn == NULL );
389     assert( c->c_listener_url == NULL );
390     assert( c->c_peer_domain == NULL );
391     assert( c->c_peer_name == NULL );
392     assert( c->c_sock_name == NULL );
393     assert( c->c_ops == NULL );
394     assert( c->c_pending_ops == NULL );
395         assert( c->c_authmech == NULL );
396         assert( c->c_authstate == NULL );
397 #ifdef HAVE_CYRUS_SASL
398         assert( c->c_sasl_context == NULL );
399 #endif
400         assert( c->c_currentber == NULL );
401
402         c->c_listener_url = ch_strdup( url  );
403         c->c_peer_domain = ch_strdup( dnsname  );
404     c->c_peer_name = ch_strdup( peername  );
405     c->c_sock_name = ch_strdup( sockname );
406
407     c->c_n_ops_received = 0;
408     c->c_n_ops_executing = 0;
409     c->c_n_ops_pending = 0;
410     c->c_n_ops_completed = 0;
411
412         c->c_n_get = 0;
413         c->c_n_read = 0;
414         c->c_n_write = 0;
415
416         /* assume LDAPv3 until bind */
417         c->c_protocol = LDAP_VERSION3;
418
419     c->c_activitytime = c->c_starttime = slap_get_time();
420
421     ber_pvt_sb_set_desc( c->c_sb, s );
422     ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
423
424     if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
425         Debug( LDAP_DEBUG_ANY,
426             "connection_init(%d, %s): set nonblocking failed\n",
427             s, c->c_peer_name,0 );
428     }
429
430     id = c->c_connid = conn_nextid++;
431
432     c->c_conn_state = SLAP_C_INACTIVE;
433     c->c_struct_state = SLAP_C_USED;
434
435 #ifdef HAVE_TLS
436     if ( use_tls ) {
437             c->c_is_tls = 1;
438             c->c_needs_tls_accept = 1;
439     }
440 #endif
441
442     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
443     ldap_pvt_thread_mutex_unlock( &connections_mutex );
444
445     backend_connection_init(c);
446
447     return id;
448 }
449
450 static void
451 connection_destroy( Connection *c )
452 {
453         /* note: connections_mutex should be locked by caller */
454
455     assert( connections != NULL );
456     assert( c != NULL );
457     assert( c->c_struct_state != SLAP_C_UNUSED );
458     assert( c->c_conn_state != SLAP_C_INVALID );
459     assert( c->c_ops == NULL );
460
461     backend_connection_destroy(c);
462
463     c->c_protocol = 0;
464     c->c_connid = -1;
465
466     c->c_activitytime = c->c_starttime = 0;
467
468     if(c->c_dn != NULL) {
469         free(c->c_dn);
470         c->c_dn = NULL;
471     }
472         if(c->c_cdn != NULL) {
473                 free(c->c_cdn);
474                 c->c_cdn = NULL;
475         }
476         if(c->c_listener_url != NULL) {
477                 free(c->c_listener_url);
478                 c->c_listener_url = NULL;
479         }
480         if(c->c_peer_domain != NULL) {
481                 free(c->c_peer_domain);
482                 c->c_peer_domain = NULL;
483         }
484         if(c->c_peer_name != NULL) {
485                 free(c->c_peer_name);
486                 c->c_peer_name = NULL;
487         }
488         if(c->c_sock_name != NULL) {
489                 free(c->c_sock_name);
490                 c->c_sock_name = NULL;
491         }
492         if(c->c_authmech != NULL ) {
493                 free(c->c_authmech);
494                 c->c_authmech = NULL;
495         }
496         if(c->c_authstate != NULL ) {
497                 free(c->c_authstate);
498                 c->c_authstate = NULL;
499         }
500
501 #ifdef HAVE_CYRUS_SASL
502         if(c->c_sasl_context != NULL ) {
503                 sasl_dispose( &c->c_sasl_context );
504                 c->c_sasl_context = NULL;
505         }
506 #endif
507
508         if ( c->c_currentber != NULL ) {
509                 ber_free( c->c_currentber, 1 );
510                 c->c_currentber = NULL;
511         }
512
513         if ( ber_pvt_sb_in_use(c->c_sb) ) {
514                 int sd = ber_pvt_sb_get_desc(c->c_sb);
515
516                 slapd_remove( sd, 0 );
517                 ber_pvt_sb_close( c->c_sb );
518
519                 Statslog( LDAP_DEBUG_STATS,
520                     "conn=%ld fd=%d closed\n",
521                         c->c_connid, sd, 0, 0, 0 );
522         }
523
524         ber_pvt_sb_destroy( c->c_sb );
525
526     c->c_conn_state = SLAP_C_INVALID;
527     c->c_struct_state = SLAP_C_UNUSED;
528 }
529
530 int connection_state_closing( Connection *c )
531 {
532         /* c_mutex must be locked by caller */
533
534         int state;
535         assert( c != NULL );
536         assert( c->c_struct_state == SLAP_C_USED );
537
538         state = c->c_conn_state;
539
540         assert( state != SLAP_C_INVALID );
541
542         return state == SLAP_C_CLOSING;
543 }
544
545 static void connection_abandon( Connection *c )
546 {
547         /* c_mutex must be locked by caller */
548
549         Operation *o;
550
551         for( o = c->c_ops; o != NULL; o = o->o_next ) {
552                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
553                 o->o_abandon = 1;
554                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
555         }
556
557         /* remove pending operations */
558         for( o = slap_op_pop( &c->c_pending_ops );
559                 o != NULL;
560                 o = slap_op_pop( &c->c_pending_ops ) )
561         {
562                 slap_op_free( o );
563         }
564 }
565
566 void connection_closing( Connection *c )
567 {
568         assert( connections != NULL );
569         assert( c != NULL );
570         assert( c->c_struct_state == SLAP_C_USED );
571         assert( c->c_conn_state != SLAP_C_INVALID );
572
573         /* c_mutex must be locked by caller */
574
575         if( c->c_conn_state != SLAP_C_CLOSING ) {
576
577                 Debug( LDAP_DEBUG_TRACE,
578                         "connection_closing: readying conn=%ld sd=%d for close\n",
579                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
580
581                 /* update state to closing */
582                 c->c_conn_state = SLAP_C_CLOSING;
583
584                 /* don't listen on this port anymore */
585                 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
586
587                 /* shutdown I/O -- not yet implemented */
588
589                 /* abandon active operations */
590                 connection_abandon( c );
591
592                 /* wake write blocked operations */
593                 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
594                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
595         }
596 }
597
598 static void connection_close( Connection *c )
599 {
600         assert( connections != NULL );
601         assert( c != NULL );
602         assert( c->c_struct_state == SLAP_C_USED );
603         assert( c->c_conn_state == SLAP_C_CLOSING );
604
605         /* note: connections_mutex and c_mutex should be locked by caller */
606
607         if( c->c_ops != NULL ) {
608                 Debug( LDAP_DEBUG_TRACE,
609                         "connection_close: deferring conn=%ld sd=%d\n",
610                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
611
612                 return;
613         }
614
615         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
616                 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
617
618         connection_destroy( c );
619 }
620
621 unsigned long connections_nextid(void)
622 {
623         unsigned long id;
624         assert( connections != NULL );
625
626         ldap_pvt_thread_mutex_lock( &connections_mutex );
627
628         id = conn_nextid;
629
630         ldap_pvt_thread_mutex_unlock( &connections_mutex );
631
632         return id;
633 }
634
635 Connection* connection_first( ber_socket_t *index )
636 {
637         assert( connections != NULL );
638         assert( index != NULL );
639
640         ldap_pvt_thread_mutex_lock( &connections_mutex );
641
642         *index = 0;
643
644         return connection_next(NULL, index);
645 }
646
647 Connection* connection_next( Connection *c, ber_socket_t *index )
648 {
649         assert( connections != NULL );
650         assert( index != NULL );
651         assert( *index <= dtblsize );
652
653         if( c != NULL ) {
654                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
655         }
656
657         c = NULL;
658
659         for(; *index < dtblsize; (*index)++) {
660                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
661                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
662 #ifndef HAVE_WINSOCK
663                         continue;
664 #else
665                         break;
666 #endif
667                 }
668
669                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
670                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
671                         c = &connections[(*index)++];
672                         break;
673                 }
674
675                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
676                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
677         }
678
679         if( c != NULL ) {
680                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
681         }
682
683         return c;
684 }
685
686 void connection_done( Connection *c )
687 {
688         assert( connections != NULL );
689
690         if( c != NULL ) {
691                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
692         }
693
694         ldap_pvt_thread_mutex_unlock( &connections_mutex );
695 }
696
697 /*
698  * connection_activity - handle the request operation op on connection
699  * conn.  This routine figures out what kind of operation it is and
700  * calls the appropriate stub to handle it.
701  */
702
703 static void *
704 connection_operation( void *arg_v )
705 {
706         int rc;
707         struct co_arg   *arg = arg_v;
708         ber_tag_t tag = arg->co_op->o_tag;
709         Connection *conn = arg->co_conn;
710
711         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
712         num_ops_initiated++;
713         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
714
715         switch ( tag ) {
716         case LDAP_REQ_BIND:
717                 rc = do_bind( conn, arg->co_op );
718                 break;
719
720         case LDAP_REQ_UNBIND:
721                 rc = do_unbind( conn, arg->co_op );
722                 break;
723
724         case LDAP_REQ_ADD:
725                 rc = do_add( conn, arg->co_op );
726                 break;
727
728         case LDAP_REQ_DELETE:
729                 rc = do_delete( conn, arg->co_op );
730                 break;
731
732         case LDAP_REQ_MODRDN:
733                 rc = do_modrdn( conn, arg->co_op );
734                 break;
735
736         case LDAP_REQ_MODIFY:
737                 rc = do_modify( conn, arg->co_op );
738                 break;
739
740         case LDAP_REQ_COMPARE:
741                 rc = do_compare( conn, arg->co_op );
742                 break;
743
744         case LDAP_REQ_SEARCH:
745                 rc = do_search( conn, arg->co_op );
746                 break;
747
748         case LDAP_REQ_ABANDON:
749                 rc = do_abandon( conn, arg->co_op );
750                 break;
751
752         case LDAP_REQ_EXTENDED:
753                 rc = do_extended( conn, arg->co_op );
754                 break;
755
756         default:
757                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
758                     tag, 0, 0 );
759                 arg->co_op->o_tag = LBER_ERROR;
760                 send_ldap_disconnect( conn, arg->co_op,
761                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
762                 rc = -1;
763                 break;
764         }
765
766         if( rc == -1 ) tag = LBER_ERROR;
767
768         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
769         num_ops_completed++;
770         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
771
772         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
773
774         conn->c_n_ops_executing--;
775         conn->c_n_ops_completed++;
776
777         slap_op_remove( &conn->c_ops, arg->co_op );
778         slap_op_free( arg->co_op );
779         arg->co_op = NULL;
780         arg->co_conn = NULL;
781         free( (char *) arg );
782         arg = NULL;
783
784         switch( tag ) {
785         case LBER_ERROR:
786         case LDAP_REQ_UNBIND:
787                 /* c_mutex is locked */
788                 connection_closing( conn );
789                 break;
790
791         case LDAP_REQ_BIND:
792                 if( conn->c_conn_state == SLAP_C_BINDING) {
793                         conn->c_conn_state = SLAP_C_ACTIVE;
794                 }
795                 conn->c_bind_in_progress = ( rc == LDAP_SASL_BIND_IN_PROGRESS );
796         }
797
798         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
799         active_threads--;
800         if( active_threads < 1 ) {
801                 ldap_pvt_thread_cond_signal(&active_threads_cond);
802         }
803         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
804
805         connection_resched( conn );
806
807         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
808
809         return NULL;
810 }
811
812 int connection_read(ber_socket_t s)
813 {
814         int rc = 0;
815         Connection *c;
816         assert( connections != NULL );
817
818         ldap_pvt_thread_mutex_lock( &connections_mutex );
819
820         /* get (locked) connection */
821         c = connection_get( s );
822
823         if( c == NULL ) {
824                 Debug( LDAP_DEBUG_ANY,
825                         "connection_read(%ld): no connection!\n",
826                         (long) s, 0, 0 );
827
828                 slapd_remove(s, 0);
829
830                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
831                 return -1;
832         }
833
834         c->c_n_read++;
835
836         if( c->c_conn_state == SLAP_C_CLOSING ) {
837                 Debug( LDAP_DEBUG_TRACE,
838                         "connection_read(%d): closing, ignoring input for id=%ld\n",
839                         s, c->c_connid, 0 );
840
841                 connection_return( c );
842                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
843                 return 0;
844         }
845
846         Debug( LDAP_DEBUG_TRACE,
847                 "connection_read(%d): checking for input on id=%ld\n",
848                 s, c->c_connid, 0 );
849
850 #ifdef HAVE_TLS
851         if ( c->c_is_tls && c->c_needs_tls_accept ) {
852                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
853                 if ( rc < 0 ) {
854                         Debug( LDAP_DEBUG_TRACE,
855                                "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
856                                s, rc, c->c_connid );
857
858                         c->c_needs_tls_accept = 0;
859                         /* connections_mutex and c_mutex are locked */
860                         connection_closing( c );
861                         connection_close( c );
862                 } else if ( rc == 0 ) {
863                         c->c_needs_tls_accept = 0;
864                 }
865                 connection_return( c );
866                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
867                 return 0;
868         }
869 #endif
870
871 #define CONNECTION_INPUT_LOOP 1
872
873 #ifdef DATA_READY_LOOP
874         while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
875 #elif CONNECTION_INPUT_LOOP
876         while(!rc)
877 #endif
878         {
879                 /* How do we do this without getting into a busy loop ? */
880                 rc = connection_input( c );
881         }
882
883         if( rc < 0 ) {
884                 Debug( LDAP_DEBUG_TRACE,
885                         "connection_read(%d): input error=%d id=%ld, closing.\n",
886                         s, rc, c->c_connid );
887
888                 /* connections_mutex and c_mutex are locked */
889                 connection_closing( c );
890                 connection_close( c );
891         }
892
893         if ( ber_pvt_sb_needs_read( c->c_sb ) )
894                 slapd_set_read( s, 1 );
895         if ( ber_pvt_sb_needs_write( c->c_sb ) )
896                 slapd_set_write( s, 1 );
897         connection_return( c );
898         ldap_pvt_thread_mutex_unlock( &connections_mutex );
899         return 0;
900 }
901
902 static int
903 connection_input(
904     Connection *conn
905 )
906 {
907         Operation *op;
908         ber_tag_t       tag;
909         ber_len_t       len;
910         ber_int_t       msgid;
911         BerElement      *ber;
912
913         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
914             == NULL ) {
915                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
916                 return -1;
917         }
918
919         errno = 0;
920         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
921             != LDAP_TAG_MESSAGE )
922         {
923                 int err = errno;
924
925                 Debug( LDAP_DEBUG_TRACE,
926                         "ber_get_next on fd %d failed errno=%d (%s)\n",
927                         ber_pvt_sb_get_desc( conn->c_sb ), err, STRERROR(err) );
928                 Debug( LDAP_DEBUG_TRACE,
929                         "\t*** got %ld of %lu so far\n",
930                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
931                         conn->c_currentber->ber_len, 0 );
932
933                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
934                         /* log, close and send error */
935                         ber_free( conn->c_currentber, 1 );
936                         conn->c_currentber = NULL;
937
938                         return -2;
939                 }
940                 return 1;
941         }
942
943         ber = conn->c_currentber;
944         conn->c_currentber = NULL;
945
946         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
947                 /* log, close and send error */
948                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
949                     0 );
950                 ber_free( ber, 1 );
951                 return -1;
952         }
953
954         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
955                 /* log, close and send error */
956                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
957                     0 );
958                 ber_free( ber, 1 );
959
960                 return -1;
961         }
962
963         if(tag == LDAP_REQ_BIND) {
964                 /* immediately abandon all exiting operations upon BIND */
965                 connection_abandon( conn );
966         }
967
968         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
969
970         if ( conn->c_conn_state == SLAP_C_BINDING
971                 || conn->c_conn_state == SLAP_C_CLOSING )
972         {
973                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
974                 conn->c_n_ops_pending++;
975                 slap_op_add( &conn->c_pending_ops, op );
976
977         } else {
978                 conn->c_n_ops_executing++;
979                 connection_op_activate( conn, op );
980         }
981
982 #ifdef NO_THREADS
983         if ( conn->c_struct_state != SLAP_C_USED ) {
984                 /* connection must have got closed underneath us */
985                 return 1;
986         }
987 #endif
988         assert( conn->c_struct_state == SLAP_C_USED );
989
990         return 0;
991 }
992
993 static int
994 connection_resched( Connection *conn )
995 {
996         Operation *op;
997
998         if( conn->c_conn_state == SLAP_C_CLOSING ) {
999                 Debug( LDAP_DEBUG_TRACE,
1000                         "connection_resched: attempting closing conn=%ld sd=%d\n",
1001                         conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
1002
1003                 connection_close( conn );
1004                 return 0;
1005         }
1006
1007         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1008                 /* other states need different handling */
1009                 return 0;
1010         }
1011
1012         for( op = slap_op_pop( &conn->c_pending_ops );
1013                 op != NULL;
1014                 op = slap_op_pop( &conn->c_pending_ops ) )
1015         {
1016                 /* pending operations should not be marked for abandonment */
1017                 assert(!op->o_abandon);
1018
1019                 conn->c_n_ops_pending--;
1020                 conn->c_n_ops_executing++;
1021
1022                 connection_op_activate( conn, op );
1023
1024                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1025                         break;
1026                 }
1027         }
1028         return 0;
1029 }
1030
1031 static int connection_op_activate( Connection *conn, Operation *op )
1032 {
1033         struct co_arg *arg;
1034         char *tmpdn;
1035         int status;
1036         ber_tag_t tag = op->o_tag;
1037
1038         if(tag == LDAP_REQ_BIND) {
1039                 conn->c_conn_state = SLAP_C_BINDING;
1040         }
1041
1042         if ( conn->c_dn != NULL ) {
1043                 tmpdn = ch_strdup( conn->c_dn );
1044         } else {
1045                 tmpdn = NULL;
1046         }
1047
1048         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1049         arg->co_conn = conn;
1050         arg->co_op = op;
1051
1052         arg->co_op->o_bind_in_progress = conn->c_bind_in_progress;
1053
1054         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1055         arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1056         (void) dn_normalize( arg->co_op->o_ndn );
1057
1058         arg->co_op->o_protocol = conn->c_protocol;
1059         arg->co_op->o_connid = conn->c_connid;
1060
1061         arg->co_op->o_authtype = conn->c_authtype;
1062         arg->co_op->o_authmech = conn->c_authmech != NULL
1063                 ?  ch_strdup( conn->c_authmech ) : NULL;
1064         
1065         slap_op_add( &conn->c_ops, arg->co_op );
1066
1067         if( tmpdn != NULL ) {
1068                 free( tmpdn );
1069         }
1070
1071         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1072         active_threads++;
1073         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1074
1075         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1076                                          connection_operation, (void *) arg );
1077
1078         if ( status != 0 ) {
1079                 Debug( LDAP_DEBUG_ANY,
1080                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1081
1082                 /* should move op to pending list */
1083         }
1084
1085         return status;
1086 }
1087
1088 int connection_write(ber_socket_t s)
1089 {
1090         Connection *c;
1091         assert( connections != NULL );
1092
1093         ldap_pvt_thread_mutex_lock( &connections_mutex );
1094
1095         c = connection_get( s );
1096
1097         slapd_clr_write( s, 0);
1098
1099         if( c == NULL ) {
1100                 Debug( LDAP_DEBUG_ANY,
1101                         "connection_write(%ld): no connection!\n",
1102                         (long) s, 0, 0 );
1103                 slapd_remove(s, 0);
1104                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1105                 return -1;
1106         }
1107
1108         c->c_n_write++;
1109
1110         Debug( LDAP_DEBUG_TRACE,
1111                 "connection_write(%d): waking output for id=%ld\n",
1112                 s, c->c_connid, 0 );
1113
1114         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1115
1116         if ( ber_pvt_sb_needs_read( c->c_sb ) )
1117                 slapd_set_read( s, 1 );
1118         if ( ber_pvt_sb_needs_write( c->c_sb ) )
1119                 slapd_set_write( s, 1 );
1120         connection_return( c );
1121         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1122         return 0;
1123 }