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