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