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