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