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