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