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