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