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