]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
fix for select_backend suggested G. Gombas (ITS 1090)
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <limits.h>
11
12 #include <ac/socket.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19 #include "lutil.h"
20 #include "slap.h"
21
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
26
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED                   0x01
30 #define SLAP_C_USED                             0x02
31
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
35 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
36 #define SLAP_C_BINDING                  0x03    /* binding */
37 #define SLAP_C_CLOSING                  0x04    /* closing */
38
39 const char *
40 connection_state2str( int state )
41 {
42         switch( state ) {
43         case SLAP_C_INVALID:    return "!";             
44         case SLAP_C_INACTIVE:   return "|";             
45         case SLAP_C_ACTIVE:             return "";                      
46         case SLAP_C_BINDING:    return "B";
47         case SLAP_C_CLOSING:    return "C";                     
48         }
49
50         return "?";
51 }
52
53 static Connection* connection_get( ber_socket_t s );
54
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
57
58 static int connection_op_activate( Connection *conn, Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
62
63 struct co_arg {
64         Connection      *co_conn;
65         Operation       *co_op;
66 };
67
68 /*
69  * Initialize connection management infrastructure.
70  */
71 int connections_init(void)
72 {
73         assert( connections == NULL );
74
75         if( connections != NULL) {
76 #ifdef NEW_LOGGING
77                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
78                            "connections_init:  already initialized.\n" ));
79 #else
80                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
81                         0, 0, 0 );
82 #endif
83                 return -1;
84         }
85
86         /* should check return of every call */
87         ldap_pvt_thread_mutex_init( &connections_mutex );
88
89         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
90
91         if( connections == NULL ) {
92 #ifdef NEW_LOGGING
93                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
94                            "connections_init: allocation (%d * %ld) of connection array failed\n",
95                            dtblsize, (long) sizeof(Connection) ));
96 #else
97                 Debug( LDAP_DEBUG_ANY,
98                         "connections_init: allocation (%d*%ld) of connection array failed\n",
99                         dtblsize, (long) sizeof(Connection), 0 );
100 #endif
101                 return -1;
102         }
103
104     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
105     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
106
107         /*
108          * per entry initialization of the Connection array initialization
109          * will be done by connection_init()
110          */ 
111
112         return 0;
113 }
114
115 /*
116  * Destroy connection management infrastructure.
117  */
118 int connections_destroy(void)
119 {
120         ber_socket_t i;
121
122         /* should check return of every call */
123
124         if( connections == NULL) {
125 #ifdef NEW_LOGGING
126                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
127                            "connections_destroy: nothing to destroy.\n"));
128 #else
129                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
130                         0, 0, 0 );
131 #endif
132                 return -1;
133         }
134
135         for ( i = 0; i < dtblsize; i++ ) {
136                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
137                         ber_sockbuf_free( connections[i].c_sb );
138                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
139                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
140                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
141                 }
142         }
143
144         free( connections );
145         connections = NULL;
146
147         ldap_pvt_thread_mutex_destroy( &connections_mutex );
148         return 0;
149 }
150
151 /*
152  * shutdown all connections
153  */
154 int connections_shutdown(void)
155 {
156         ber_socket_t i;
157
158         ldap_pvt_thread_mutex_lock( &connections_mutex );
159
160         for ( i = 0; i < dtblsize; i++ ) {
161                 if( connections[i].c_struct_state != SLAP_C_USED ) {
162                         continue;
163                 }
164
165                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
166
167                 /* connections_mutex and c_mutex are locked */
168                 connection_closing( &connections[i] );
169                 connection_close( &connections[i] );
170
171                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
172         }
173
174         ldap_pvt_thread_mutex_unlock( &connections_mutex );
175
176         return 0;
177 }
178
179 /*
180  * Timeout idle connections.
181  */
182 int connections_timeout_idle(time_t now)
183 {
184         int i = 0;
185         int connindex;
186         Connection* c;
187
188         for( c = connection_first( &connindex );
189                 c != NULL;
190                 c = connection_next( c, &connindex ) )
191         {
192                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
193                         /* close it */
194                         connection_closing( c );
195                         connection_close( c );
196                         i++;
197                 }
198         }
199         connection_done( c );
200
201         return i;
202 }
203
204 static Connection* connection_get( ber_socket_t s )
205 {
206         /* connections_mutex should be locked by caller */
207
208         Connection *c;
209
210 #ifdef NEW_LOGGING
211         LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
212                    "connection_get: socket %ld\n", (long)s ));
213 #else
214         Debug( LDAP_DEBUG_ARGS,
215                 "connection_get(%ld)\n",
216                 (long) s, 0, 0 );
217 #endif
218
219         assert( connections != NULL );
220
221         if(s == AC_SOCKET_INVALID) {
222                 return NULL;
223         }
224
225 #ifndef HAVE_WINSOCK
226         c = &connections[s];
227
228         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
229
230 #else
231         c = NULL;
232         {
233                 ber_socket_t i, sd;
234
235                 for(i=0; i<dtblsize; i++) {
236                         ber_sockbuf_ctrl( connections[i].c_sb,
237                                 LBER_SB_OPT_GET_FD, &sd );
238
239                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
240                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
241                                 assert( connections[i].c_sb == 0 );
242                                 break;
243                         }
244
245                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
246                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
247                                 assert( sd == AC_SOCKET_INVALID );
248                                 continue;
249                         }
250
251                         /* state can actually change from used -> unused by resched,
252                          * so don't assert details here.
253                          */
254
255                         if( sd == s ) {
256                                 c = &connections[i];
257                                 break;
258                         }
259                 }
260         }
261 #endif
262
263         if( c != NULL ) {
264                 ber_socket_t    sd;
265
266                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
267
268                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
269                 if( c->c_struct_state != SLAP_C_USED ) {
270                         /* connection must have been closed due to resched */
271
272                         assert( c->c_conn_state == SLAP_C_INVALID );
273                         assert( sd == AC_SOCKET_INVALID );
274
275 #ifdef NEW_LOGGING
276                         LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
277                                    "connection_get:  connection %d not used\n", s ));
278 #else
279                         Debug( LDAP_DEBUG_TRACE,
280                                 "connection_get(%d): connection not used\n",
281                                 s, 0, 0 );
282 #endif
283
284                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
285                         return NULL;
286                 }
287
288 #ifdef NEW_LOGGING
289                 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
290                            "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
291 #else
292                 Debug( LDAP_DEBUG_TRACE,
293                         "connection_get(%d): got connid=%ld\n",
294                         s, c->c_connid, 0 );
295 #endif
296
297                 c->c_n_get++;
298
299                 assert( c->c_struct_state == SLAP_C_USED );
300                 assert( c->c_conn_state != SLAP_C_INVALID );
301                 assert( sd != AC_SOCKET_INVALID );
302
303         c->c_activitytime = slap_get_time();
304         }
305
306         return c;
307 }
308
309 static void connection_return( Connection *c )
310 {
311         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
312 }
313
314 long connection_init(
315         ber_socket_t s,
316         const char* url,
317         const char* dnsname,
318         const char* peername,
319         const char* sockname,
320         int use_tls,
321         slap_ssf_t ssf,
322         const char *authid )
323 {
324         unsigned long id;
325         Connection *c;
326
327         assert( connections != NULL );
328
329         assert( dnsname != NULL );
330         assert( peername != NULL );
331         assert( sockname != NULL );
332
333 #ifndef HAVE_TLS
334         assert( !use_tls );
335 #endif
336
337         if( s == AC_SOCKET_INVALID ) {
338 #ifdef NEW_LOGGING
339                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
340                            "connection_init: init of socket %ld invalid.\n", (long)s ));
341 #else
342                 Debug( LDAP_DEBUG_ANY,
343                        "connection_init(%ld): invalid.\n",
344                        (long) s, 0, 0 );
345 #endif
346                 return -1;
347         }
348
349         assert( s >= 0 );
350 #ifndef HAVE_WINSOCK
351         assert( s < dtblsize );
352 #endif
353
354         ldap_pvt_thread_mutex_lock( &connections_mutex );
355
356 #ifndef HAVE_WINSOCK
357         c = &connections[s];
358
359 #else
360         {
361                 unsigned int i;
362
363                 c = NULL;
364
365         for( i=0; i < dtblsize; i++) {
366                 ber_socket_t    sd;
367
368             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
369                 assert( connections[i].c_sb == 0 );
370                 c = &connections[i];
371                 break;
372             }
373
374                         sd = AC_SOCKET_INVALID;
375                         if (connections[i].c_sb != NULL)
376                         ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
377             
378             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
379                 assert( sd == AC_SOCKET_INVALID );
380                 c = &connections[i];
381                 break;
382             }
383
384             assert( connections[i].c_struct_state == SLAP_C_USED );
385             assert( connections[i].c_conn_state != SLAP_C_INVALID );
386             assert( sd != AC_SOCKET_INVALID );
387         }
388
389         if( c == NULL ) {
390 #ifdef NEW_LOGGING
391                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
392                            "connection_init: skt %d      connection table full (%d/%d)\n",
393                            s, i, dtblsize ));
394 #else
395                 Debug( LDAP_DEBUG_ANY,
396                                 "connection_init(%d): connection table full (%d/%d)\n",
397                                 s, i, dtblsize);
398 #endif
399             ldap_pvt_thread_mutex_unlock( &connections_mutex );
400             return -1;
401         }
402     }
403 #endif
404
405     assert( c != NULL );
406
407     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
408                 c->c_authmech = NULL;
409         c->c_dn = NULL;
410         c->c_cdn = NULL;
411
412                 c->c_listener_url = NULL;
413                 c->c_peer_domain = NULL;
414         c->c_peer_name = NULL;
415         c->c_sock_name = NULL;
416
417         c->c_ops = NULL;
418         c->c_pending_ops = NULL;
419
420                 c->c_sasl_bind_mech = NULL;
421                 c->c_sasl_context = NULL;
422                 c->c_sasl_extra = NULL;
423
424         c->c_sb = ber_sockbuf_alloc( );
425                 c->c_currentber = NULL;
426
427         /* should check status of thread calls */
428         ldap_pvt_thread_mutex_init( &c->c_mutex );
429         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
430         ldap_pvt_thread_cond_init( &c->c_write_cv );
431
432         c->c_struct_state = SLAP_C_UNUSED;
433     }
434
435     ldap_pvt_thread_mutex_lock( &c->c_mutex );
436
437     assert( c->c_struct_state == SLAP_C_UNUSED );
438         assert( c->c_authmech == NULL );
439     assert(     c->c_dn == NULL );
440     assert(     c->c_cdn == NULL );
441     assert( c->c_listener_url == NULL );
442     assert( c->c_peer_domain == NULL );
443     assert( c->c_peer_name == NULL );
444     assert( c->c_sock_name == NULL );
445     assert( c->c_ops == NULL );
446     assert( c->c_pending_ops == NULL );
447         assert( c->c_sasl_bind_mech == NULL );
448         assert( c->c_sasl_context == NULL );
449         assert( c->c_sasl_extra == NULL );
450         assert( c->c_currentber == NULL );
451
452         c->c_listener_url = ch_strdup( url  );
453         c->c_peer_domain = ch_strdup( dnsname  );
454     c->c_peer_name = ch_strdup( peername  );
455     c->c_sock_name = ch_strdup( sockname );
456
457     c->c_n_ops_received = 0;
458     c->c_n_ops_executing = 0;
459     c->c_n_ops_pending = 0;
460     c->c_n_ops_completed = 0;
461
462         c->c_n_get = 0;
463         c->c_n_read = 0;
464         c->c_n_write = 0;
465
466         /* set to zero until bind, implies LDAP_VERSION3 */
467         c->c_protocol = 0;
468
469     c->c_activitytime = c->c_starttime = slap_get_time();
470
471 #ifdef LDAP_DEBUG
472         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
473                 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
474 #endif
475         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
476                 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
477         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
478                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
479
480 #ifdef LDAP_DEBUG
481         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
482                 INT_MAX, (void*)"ldap_" );
483 #endif
484
485         if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
486                 c /* non-NULL */ ) < 0 )
487         {
488 #ifdef NEW_LOGGING
489                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
490                            "connection_init: conn %d  set nonblocking failed\n",
491                            c->c_connid ));
492 #else
493                 Debug( LDAP_DEBUG_ANY,
494                         "connection_init(%d, %s): set nonblocking failed\n",
495                         s, c->c_peer_name, 0 );
496 #endif
497         }
498
499     id = c->c_connid = conn_nextid++;
500
501     c->c_conn_state = SLAP_C_INACTIVE;
502     c->c_struct_state = SLAP_C_USED;
503
504         c->c_ssf = c->c_transport_ssf = ssf;
505         c->c_tls_ssf = 0;
506
507 #ifdef HAVE_TLS
508     if ( use_tls ) {
509             c->c_is_tls = 1;
510             c->c_needs_tls_accept = 1;
511     } else {
512             c->c_is_tls = 0;
513             c->c_needs_tls_accept = 0;
514     }
515 #endif
516
517         slap_sasl_open( c );
518         slap_sasl_external( c, ssf, authid );
519
520     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
521     ldap_pvt_thread_mutex_unlock( &connections_mutex );
522
523     backend_connection_init(c);
524
525     return id;
526 }
527
528 void connection2anonymous( Connection *c )
529 {
530     assert( connections != NULL );
531     assert( c != NULL );
532
533         if(c->c_authmech != NULL ) {
534                 free(c->c_authmech);
535                 c->c_authmech = NULL;
536         }
537
538     if(c->c_dn != NULL) {
539         free(c->c_dn);
540         c->c_dn = NULL;
541     }
542
543         if(c->c_cdn != NULL) {
544                 free(c->c_cdn);
545                 c->c_cdn = NULL;
546         }
547
548         c->c_authc_backend = NULL;
549         c->c_authz_backend = NULL;
550 }
551
552 static void
553 connection_destroy( Connection *c )
554 {
555         /* note: connections_mutex should be locked by caller */
556     ber_socket_t        sd;
557
558     assert( connections != NULL );
559     assert( c != NULL );
560     assert( c->c_struct_state != SLAP_C_UNUSED );
561     assert( c->c_conn_state != SLAP_C_INVALID );
562     assert( c->c_ops == NULL );
563
564     backend_connection_destroy(c);
565
566     c->c_protocol = 0;
567     c->c_connid = -1;
568
569     c->c_activitytime = c->c_starttime = 0;
570
571         connection2anonymous( c );
572
573         if(c->c_listener_url != NULL) {
574                 free(c->c_listener_url);
575                 c->c_listener_url = NULL;
576         }
577
578         if(c->c_peer_domain != NULL) {
579                 free(c->c_peer_domain);
580                 c->c_peer_domain = NULL;
581         }
582         if(c->c_peer_name != NULL) {
583 #ifdef LDAP_PF_lOCAL
584                 /*
585                  * If peer was a domain socket, unlink. Mind you,
586                  * they may be un-named. Should we leave this to
587                  * the client?
588                  */
589                 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
590                         char *path = c->c_peer_name + 5;
591                         if (path != '\0') {
592                                 (void)unlink(path);
593                         }
594                 }
595 #endif /* LDAP_PF_LOCAL */
596
597                 free(c->c_peer_name);
598                 c->c_peer_name = NULL;
599         }
600         if(c->c_sock_name != NULL) {
601                 free(c->c_sock_name);
602                 c->c_sock_name = NULL;
603         }
604
605         c->c_sasl_bind_in_progress = 0;
606         if(c->c_sasl_bind_mech != NULL) {
607                 free(c->c_sasl_bind_mech);
608                 c->c_sasl_bind_mech = NULL;
609         }
610
611         slap_sasl_close( c );
612
613         if ( c->c_currentber != NULL ) {
614                 ber_free( c->c_currentber, 1 );
615                 c->c_currentber = NULL;
616         }
617
618         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
619         if ( sd != AC_SOCKET_INVALID ) {
620                 slapd_remove( sd, 0 );
621
622                 Statslog( LDAP_DEBUG_STATS,
623                     "conn=%ld fd=%d closed\n",
624                         c->c_connid, sd, 0, 0, 0 );
625         }
626
627         ber_sockbuf_free( c->c_sb );
628         c->c_sb = ber_sockbuf_alloc( );
629
630     c->c_conn_state = SLAP_C_INVALID;
631     c->c_struct_state = SLAP_C_UNUSED;
632 }
633
634 int connection_state_closing( Connection *c )
635 {
636         /* c_mutex must be locked by caller */
637
638         int state;
639         assert( c != NULL );
640         assert( c->c_struct_state == SLAP_C_USED );
641
642         state = c->c_conn_state;
643
644         assert( state != SLAP_C_INVALID );
645
646         return state == SLAP_C_CLOSING;
647 }
648
649 static void connection_abandon( Connection *c )
650 {
651         /* c_mutex must be locked by caller */
652
653         Operation *o;
654
655         for( o = c->c_ops; o != NULL; o = o->o_next ) {
656                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
657                 o->o_abandon = 1;
658                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
659         }
660
661         /* remove pending operations */
662         for( o = slap_op_pop( &c->c_pending_ops );
663                 o != NULL;
664                 o = slap_op_pop( &c->c_pending_ops ) )
665         {
666                 slap_op_free( o );
667         }
668 }
669
670 void connection_closing( Connection *c )
671 {
672         assert( connections != NULL );
673         assert( c != NULL );
674         assert( c->c_struct_state == SLAP_C_USED );
675         assert( c->c_conn_state != SLAP_C_INVALID );
676
677         /* c_mutex must be locked by caller */
678
679         if( c->c_conn_state != SLAP_C_CLOSING ) {
680                 ber_socket_t    sd;
681
682                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
683 #ifdef NEW_LOGGING
684                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
685                            "connection_closing: conn %d readying socket %d for close.\n",
686                            c->c_connid, sd ));
687 #else
688                 Debug( LDAP_DEBUG_TRACE,
689                         "connection_closing: readying conn=%ld sd=%d for close\n",
690                         c->c_connid, sd, 0 );
691 #endif
692                 /* update state to closing */
693                 c->c_conn_state = SLAP_C_CLOSING;
694
695                 /* don't listen on this port anymore */
696                 slapd_clr_read( sd, 1 );
697
698                 /* abandon active operations */
699                 connection_abandon( c );
700
701                 /* wake write blocked operations */
702                 slapd_clr_write( sd, 1 );
703                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
704         }
705 }
706
707 static void connection_close( Connection *c )
708 {
709         ber_socket_t    sd;
710
711         assert( connections != NULL );
712         assert( c != NULL );
713         assert( c->c_struct_state == SLAP_C_USED );
714         assert( c->c_conn_state == SLAP_C_CLOSING );
715
716         /* note: connections_mutex and c_mutex should be locked by caller */
717
718         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
719         if( c->c_ops != NULL ) {
720 #ifdef NEW_LOGGING
721                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
722                            "connection_close: conn %d  deferring sd %d\n",
723                            c->c_connid, sd ));
724 #else
725                 Debug( LDAP_DEBUG_TRACE,
726                         "connection_close: deferring conn=%ld sd=%d\n",
727                         c->c_connid, sd, 0 );
728 #endif
729                 return;
730         }
731
732 #ifdef NEW_LOGGING
733         LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
734                    "connection_close: conn %d  sd %d\n",
735                    c->c_connid, sd ));
736 #else
737         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
738                 c->c_connid, sd, 0 );
739 #endif
740         connection_destroy( c );
741 }
742
743 unsigned long connections_nextid(void)
744 {
745         unsigned long id;
746         assert( connections != NULL );
747
748         ldap_pvt_thread_mutex_lock( &connections_mutex );
749
750         id = conn_nextid;
751
752         ldap_pvt_thread_mutex_unlock( &connections_mutex );
753
754         return id;
755 }
756
757 Connection* connection_first( ber_socket_t *index )
758 {
759         assert( connections != NULL );
760         assert( index != NULL );
761
762         ldap_pvt_thread_mutex_lock( &connections_mutex );
763
764         *index = 0;
765
766         return connection_next(NULL, index);
767 }
768
769 Connection* connection_next( Connection *c, ber_socket_t *index )
770 {
771         assert( connections != NULL );
772         assert( index != NULL );
773         assert( *index <= dtblsize );
774
775         if( c != NULL ) {
776                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
777         }
778
779         c = NULL;
780
781         for(; *index < dtblsize; (*index)++) {
782                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
783                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
784 #ifndef HAVE_WINSOCK
785                         continue;
786 #else
787                         break;
788 #endif
789                 }
790
791                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
792                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
793                         c = &connections[(*index)++];
794                         break;
795                 }
796
797                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
798                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
799         }
800
801         if( c != NULL ) {
802                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
803         }
804
805         return c;
806 }
807
808 void connection_done( Connection *c )
809 {
810         assert( connections != NULL );
811
812         if( c != NULL ) {
813                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
814         }
815
816         ldap_pvt_thread_mutex_unlock( &connections_mutex );
817 }
818
819 /*
820  * connection_activity - handle the request operation op on connection
821  * conn.  This routine figures out what kind of operation it is and
822  * calls the appropriate stub to handle it.
823  */
824
825 static void *
826 connection_operation( void *arg_v )
827 {
828         int rc;
829         struct co_arg   *arg = arg_v;
830         ber_tag_t tag = arg->co_op->o_tag;
831         Connection *conn = arg->co_conn;
832
833         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
834         num_ops_initiated++;
835         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
836
837         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
838 #ifdef NEW_LOGGING
839                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
840                            "connection_operation: conn %d  SASL bind in progress (tag=%ld).\n",
841                            conn->c_connid, (long)tag ));
842 #else
843                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
844                         "error: SASL bind in progress (tag=%ld).\n",
845                         (long) tag, 0, 0 );
846 #endif
847                 send_ldap_result( conn, arg->co_op,
848                         rc = LDAP_OPERATIONS_ERROR,
849                         NULL, "SASL bind in progress", NULL, NULL );
850                 goto operations_error;
851         }
852
853         switch ( tag ) {
854         case LDAP_REQ_BIND:
855                 rc = do_bind( conn, arg->co_op );
856                 break;
857
858         case LDAP_REQ_UNBIND:
859                 rc = do_unbind( conn, arg->co_op );
860                 break;
861
862         case LDAP_REQ_ADD:
863                 rc = do_add( conn, arg->co_op );
864                 break;
865
866         case LDAP_REQ_DELETE:
867                 rc = do_delete( conn, arg->co_op );
868                 break;
869
870         case LDAP_REQ_MODRDN:
871                 rc = do_modrdn( conn, arg->co_op );
872                 break;
873
874         case LDAP_REQ_MODIFY:
875                 rc = do_modify( conn, arg->co_op );
876                 break;
877
878         case LDAP_REQ_COMPARE:
879                 rc = do_compare( conn, arg->co_op );
880                 break;
881
882         case LDAP_REQ_SEARCH:
883                 rc = do_search( conn, arg->co_op );
884                 break;
885
886         case LDAP_REQ_ABANDON:
887                 rc = do_abandon( conn, arg->co_op );
888                 break;
889
890         case LDAP_REQ_EXTENDED:
891                 rc = do_extended( conn, arg->co_op );
892                 break;
893
894         default:
895 #ifdef NEW_LOGGING
896                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
897                            "connection_operation: conn %d  unknown LDAP request 0x%lx\n",
898                            conn->c_connid, tag ));
899 #else
900                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
901                     tag, 0, 0 );
902 #endif
903                 arg->co_op->o_tag = LBER_ERROR;
904                 send_ldap_disconnect( conn, arg->co_op,
905                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
906                 rc = -1;
907                 break;
908         }
909
910         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
911
912 operations_error:
913         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
914         num_ops_completed++;
915         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
916
917         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
918
919         conn->c_n_ops_executing--;
920         conn->c_n_ops_completed++;
921
922         slap_op_remove( &conn->c_ops, arg->co_op );
923         slap_op_free( arg->co_op );
924         arg->co_op = NULL;
925         arg->co_conn = NULL;
926         free( (char *) arg );
927         arg = NULL;
928
929         switch( tag ) {
930         case LBER_ERROR:
931         case LDAP_REQ_UNBIND:
932                 /* c_mutex is locked */
933                 connection_closing( conn );
934                 break;
935
936         case LDAP_REQ_BIND:
937                 conn->c_sasl_bind_in_progress =
938                         rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
939
940                 if( conn->c_conn_state == SLAP_C_BINDING) {
941                         conn->c_conn_state = SLAP_C_ACTIVE;
942                 }
943         }
944
945         connection_resched( conn );
946
947         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
948
949         return NULL;
950 }
951
952 int connection_read(ber_socket_t s)
953 {
954         int rc = 0;
955         Connection *c;
956         assert( connections != NULL );
957
958         ldap_pvt_thread_mutex_lock( &connections_mutex );
959
960         /* get (locked) connection */
961         c = connection_get( s );
962
963         if( c == NULL ) {
964 #ifdef NEW_LOGGING
965                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
966                            "connection_read: sock %ld no connection\n",
967                            (long)s ));
968 #else
969                 Debug( LDAP_DEBUG_ANY,
970                         "connection_read(%ld): no connection!\n",
971                         (long) s, 0, 0 );
972 #endif
973                 slapd_remove(s, 0);
974
975                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
976                 return -1;
977         }
978
979         c->c_n_read++;
980
981         if( c->c_conn_state == SLAP_C_CLOSING ) {
982 #ifdef NEW_LOGGING
983                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
984                            "connection_read: conn %d connection closing, ignoring input\n",
985                            c->c_connid));
986 #else
987                 Debug( LDAP_DEBUG_TRACE,
988                         "connection_read(%d): closing, ignoring input for id=%ld\n",
989                         s, c->c_connid, 0 );
990 #endif
991                 connection_return( c );
992                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
993                 return 0;
994         }
995
996 #ifdef NEW_LOGGING
997         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
998                    "connection_read: conn %d  checking for input.\n", c->c_connid ));
999 #else
1000         Debug( LDAP_DEBUG_TRACE,
1001                 "connection_read(%d): checking for input on id=%ld\n",
1002                 s, c->c_connid, 0 );
1003 #endif
1004
1005 #ifdef HAVE_TLS
1006         if ( c->c_is_tls && c->c_needs_tls_accept ) {
1007                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1008                 if ( rc < 0 ) {
1009                         struct timeval tv;
1010                         fd_set rfd;
1011
1012 #ifdef NEW_LOGGING
1013                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1014                                    "connection_read: conn %d  TLS accept error, error %d\n",
1015                                    c->c_connid, rc ));
1016 #else
1017                         Debug( LDAP_DEBUG_TRACE,
1018                                 "connection_read(%d): TLS accept error "
1019                                 "error=%d id=%ld, closing\n",
1020                                 s, rc, c->c_connid );
1021 #endif
1022                         c->c_needs_tls_accept = 0;
1023                         /* connections_mutex and c_mutex are locked */
1024                         connection_closing( c );
1025
1026 #if 0
1027                         /* Drain input before close, to allow SSL error codes
1028                          * to propagate to client. */
1029                         FD_ZERO(&rfd);
1030                         FD_SET(s, &rfd);
1031                         for (rc=1; rc>0;)
1032                         {
1033                             tv.tv_sec = 1;
1034                             tv.tv_usec = 0;
1035                             rc = select(s+1, &rfd, NULL, NULL, &tv);
1036                             if (rc == 1)
1037                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1038                                     NULL);
1039                         }
1040 #endif
1041                         connection_close( c );
1042
1043                 } else if ( rc == 0 ) {
1044                         void *ssl;
1045                         char *authid;
1046
1047                         c->c_needs_tls_accept = 0;
1048
1049                         /* we need to let SASL know */
1050                         ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
1051
1052                         c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1053                         if( c->c_tls_ssf > c->c_ssf ) {
1054                                 c->c_ssf = c->c_tls_ssf;
1055                         }
1056
1057                         authid = (char *)ldap_pvt_tls_get_peer( ssl );
1058                         slap_sasl_external( c, c->c_tls_ssf, authid );
1059                 }
1060                 connection_return( c );
1061                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1062                 return 0;
1063         }
1064 #endif
1065
1066 #ifdef HAVE_CYRUS_SASL
1067         if ( c->c_sasl_layers ) {
1068                 c->c_sasl_layers = 0;
1069
1070                 rc = ldap_pvt_sasl_install( c->c_sb,  c->c_sasl_context );
1071
1072                 if( rc != LDAP_SUCCESS ) {
1073 #ifdef NEW_LOGGING
1074                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1075                                    "connection_read: conn %d SASL install error %d, closing\n",
1076                                    c->c_connid, rc ));
1077 #else
1078                         Debug( LDAP_DEBUG_TRACE,
1079                                 "connection_read(%d): SASL install error "
1080                                 "error=%d id=%ld, closing\n",
1081                                 s, rc, c->c_connid );
1082 #endif
1083                         /* connections_mutex and c_mutex are locked */
1084                         connection_closing( c );
1085                         connection_close( c );
1086                         connection_return( c );
1087                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1088                         return 0;
1089                 }
1090         }
1091 #endif
1092
1093 #define CONNECTION_INPUT_LOOP 1
1094
1095 #ifdef DATA_READY_LOOP
1096         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1097 #elif CONNECTION_INPUT_LOOP
1098         while(!rc)
1099 #endif
1100         {
1101                 /* How do we do this without getting into a busy loop ? */
1102                 rc = connection_input( c );
1103         }
1104
1105         if( rc < 0 ) {
1106 #ifdef NEW_LOGGING
1107                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1108                            "connection_read: conn %d  input error %d, closing.\n",
1109                            c->c_connid, rc ));
1110 #else
1111                 Debug( LDAP_DEBUG_TRACE,
1112                         "connection_read(%d): input error=%d id=%ld, closing.\n",
1113                         s, rc, c->c_connid );
1114 #endif
1115                 /* connections_mutex and c_mutex are locked */
1116                 connection_closing( c );
1117                 connection_close( c );
1118                 connection_return( c );
1119                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1120                 return 0;
1121         }
1122
1123         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1124                 slapd_set_read( s, 1 );
1125         }
1126
1127         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1128                 slapd_set_write( s, 1 );
1129         }
1130
1131         connection_return( c );
1132         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1133         return 0;
1134 }
1135
1136 static int
1137 connection_input(
1138     Connection *conn
1139 )
1140 {
1141         Operation *op;
1142         ber_tag_t       tag;
1143         ber_len_t       len;
1144         ber_int_t       msgid;
1145         BerElement      *ber;
1146
1147         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1148             == NULL ) {
1149 #ifdef NEW_LOGGING
1150                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1151                            "connection_input: conn %d  ber_alloc failed.\n", conn->c_connid ));
1152 #else
1153                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1154 #endif
1155                 return -1;
1156         }
1157
1158         errno = 0;
1159
1160         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1161         if ( tag != LDAP_TAG_MESSAGE ) {
1162                 int err = errno;
1163                 ber_socket_t    sd;
1164
1165                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1166
1167 #ifdef NEW_LOGGING
1168                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1169                            "connection_input: conn %d  ber_get_next failed, errno %d (%s).\n",
1170                            conn->c_connid, err, sock_errstr(err) ));
1171 #else
1172                 Debug( LDAP_DEBUG_TRACE,
1173                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1174                         sd, err, sock_errstr(err) );
1175 #endif
1176                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1177                         /* log, close and send error */
1178                         ber_free( conn->c_currentber, 1 );
1179                         conn->c_currentber = NULL;
1180
1181                         return -2;
1182                 }
1183                 return 1;
1184         }
1185
1186         ber = conn->c_currentber;
1187         conn->c_currentber = NULL;
1188
1189         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1190                 /* log, close and send error */
1191 #ifdef NEW_LOGGING
1192                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1193                            "connection_input: conn %d  ber_get_int returns 0x%lx.\n",
1194                            conn->c_connid, tag ));
1195 #else
1196                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1197                     0 );
1198 #endif
1199                 ber_free( ber, 1 );
1200                 return -1;
1201         }
1202
1203         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1204                 /* log, close and send error */
1205 #ifdef NEW_LOGGING
1206                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1207                            "connection_input: conn %d  ber_peek_tag returns 0x%lx.\n",
1208                            conn->c_connid, tag ));
1209 #else
1210                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1211                     0 );
1212 #endif
1213                 ber_free( ber, 1 );
1214
1215                 return -1;
1216         }
1217
1218         if(tag == LDAP_REQ_BIND) {
1219                 /* immediately abandon all exiting operations upon BIND */
1220                 connection_abandon( conn );
1221         }
1222
1223         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1224
1225         if ( conn->c_conn_state == SLAP_C_BINDING
1226                 || conn->c_conn_state == SLAP_C_CLOSING )
1227         {
1228 #ifdef NEW_LOGGING
1229                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1230                            "connection_input: conn %d  deferring operation\n",
1231                            conn->c_connid ));
1232 #else
1233                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1234 #endif
1235                 conn->c_n_ops_pending++;
1236                 slap_op_add( &conn->c_pending_ops, op );
1237
1238         } else {
1239                 conn->c_n_ops_executing++;
1240                 connection_op_activate( conn, op );
1241         }
1242
1243 #ifdef NO_THREADS
1244         if ( conn->c_struct_state != SLAP_C_USED ) {
1245                 /* connection must have got closed underneath us */
1246                 return 1;
1247         }
1248 #endif
1249         assert( conn->c_struct_state == SLAP_C_USED );
1250
1251         return 0;
1252 }
1253
1254 static int
1255 connection_resched( Connection *conn )
1256 {
1257         Operation *op;
1258
1259         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1260                 int rc;
1261                 ber_socket_t    sd;
1262                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1263
1264                 /* us trylock to avoid possible deadlock */
1265                 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1266
1267                 if( rc ) {
1268 #ifdef NEW_LOGGING
1269                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1270                                    "connection_resched: conn %d  reaquiring locks.\n",
1271                                    conn->c_connid ));
1272 #else
1273                         Debug( LDAP_DEBUG_TRACE,
1274                                 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1275                                 conn->c_connid, sd, 0 );
1276 #endif
1277                         /*
1278                          * reaquire locks in the right order...
1279                          * this may allow another thread to close this connection,
1280                          * so recheck state below.
1281                          */
1282                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1283                         ldap_pvt_thread_mutex_lock( &connections_mutex );
1284                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1285                 }
1286
1287                 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1288 #ifdef NEW_LOGGING
1289                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1290                                    "connection_resched: conn %d  closed by other thread.\n",
1291                                    conn->c_connid ));
1292 #else
1293                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1294                                 "closed by other thread conn=%ld sd=%d\n",
1295                                 conn->c_connid, sd, 0 );
1296 #endif
1297                 } else {
1298 #ifdef NEW_LOGGING
1299                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1300                                    "connection_resched: conn %d  attempting closing.\n",
1301                                    conn->c_connid ));
1302 #else
1303                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1304                                 "attempting closing conn=%ld sd=%d\n",
1305                                 conn->c_connid, sd, 0 );
1306 #endif
1307                         connection_close( conn );
1308                 }
1309
1310                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1311                 return 0;
1312         }
1313
1314         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1315                 /* other states need different handling */
1316                 return 0;
1317         }
1318
1319         for( op = slap_op_pop( &conn->c_pending_ops );
1320                 op != NULL;
1321                 op = slap_op_pop( &conn->c_pending_ops ) )
1322         {
1323                 /* pending operations should not be marked for abandonment */
1324                 assert(!op->o_abandon);
1325
1326                 conn->c_n_ops_pending--;
1327                 conn->c_n_ops_executing++;
1328
1329                 connection_op_activate( conn, op );
1330
1331                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1332                         break;
1333                 }
1334         }
1335         return 0;
1336 }
1337
1338 static int connection_op_activate( Connection *conn, Operation *op )
1339 {
1340         struct co_arg *arg;
1341         int status;
1342         ber_tag_t tag = op->o_tag;
1343
1344         if(tag == LDAP_REQ_BIND) {
1345                 conn->c_conn_state = SLAP_C_BINDING;
1346         }
1347
1348         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1349         arg->co_conn = conn;
1350         arg->co_op = op;
1351
1352         arg->co_op->o_authz = conn->c_authz;
1353         arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1354         arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1355         (void) dn_normalize( arg->co_op->o_ndn );
1356         arg->co_op->o_authtype = conn->c_authtype;
1357         arg->co_op->o_authmech = conn->c_authmech != NULL
1358                 ?  ch_strdup( conn->c_authmech ) : NULL;
1359         
1360         arg->co_op->o_protocol = conn->c_protocol
1361                 ? conn->c_protocol : LDAP_VERSION3;
1362         arg->co_op->o_connid = conn->c_connid;
1363
1364         slap_op_add( &conn->c_ops, arg->co_op );
1365
1366         status = ldap_pvt_thread_pool_submit( &connection_pool,
1367                 connection_operation, (void *) arg );
1368
1369         if ( status != 0 ) {
1370 #ifdef NEW_LOGGING
1371                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1372                            "connection_op_activate: conn %d      thread pool submit failed.\n",
1373                            conn->c_connid ));
1374 #else
1375                 Debug( LDAP_DEBUG_ANY,
1376                 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1377 #endif
1378                 /* should move op to pending list */
1379         }
1380
1381         return status;
1382 }
1383
1384 int connection_write(ber_socket_t s)
1385 {
1386         Connection *c;
1387         assert( connections != NULL );
1388
1389         ldap_pvt_thread_mutex_lock( &connections_mutex );
1390
1391         c = connection_get( s );
1392
1393         slapd_clr_write( s, 0);
1394
1395         if( c == NULL ) {
1396 #ifdef NEW_LOGGING
1397                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1398                            "connection_write: sock %ld  no connection!\n",(long)s));
1399 #else
1400                 Debug( LDAP_DEBUG_ANY,
1401                         "connection_write(%ld): no connection!\n",
1402                         (long) s, 0, 0 );
1403 #endif
1404                 slapd_remove(s, 0);
1405                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1406                 return -1;
1407         }
1408
1409         c->c_n_write++;
1410
1411 #ifdef NEW_LOGGING
1412         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1413                    "connection_write conn %d  waking output.\n",
1414                    c->c_connid ));
1415 #else
1416         Debug( LDAP_DEBUG_TRACE,
1417                 "connection_write(%d): waking output for id=%ld\n",
1418                 s, c->c_connid, 0 );
1419 #endif
1420         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1421
1422         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1423                 slapd_set_read( s, 1 );
1424         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1425                 slapd_set_write( s, 1 );
1426         connection_return( c );
1427         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1428         return 0;
1429 }
1430
1431
1432 /*
1433  * Create client side and server side connection structures, connected to
1434  * one another, for the front end to use for searches on arbitrary back ends.
1435  */
1436
1437 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1438 {
1439         int rc;
1440         ber_socket_t fd[2] = {-1,-1};
1441         Operation *op;
1442
1443         *conn=NULL;
1444         *ldp=NULL;
1445
1446         rc = lutil_pair( fd );
1447         if( rc == -1 ) {
1448                 return LDAP_OTHER;
1449         }
1450
1451         rc = connection_init( fd[1], "INT", "localhost", 
1452       "localhost:0", "localhost:00", 0, 256, id );
1453         if( rc < 0 ) {
1454                 tcp_close( fd[0] );
1455                 tcp_close( fd[1] );
1456                 return LDAP_OTHER;
1457         }
1458         slapd_add_internal( fd[1] );
1459
1460         /* A search operation, number 0 */
1461         op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1462         op->o_ndn = ch_strdup( id );
1463         op->o_protocol = LDAP_VERSION3;
1464
1465         (*conn) = connection_get( fd[1] );
1466         (*conn)->c_ops = op;
1467     (*conn)->c_conn_state = SLAP_C_ACTIVE;
1468
1469
1470         /* Create the client side of the connection */
1471         rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1472         if( rc != LDAP_SUCCESS ) {
1473                 tcp_close( fd[0] );
1474                 return LDAP_OTHER;
1475         }
1476
1477         /* The connection_get() will have locked the connection's mutex */
1478         ldap_pvt_thread_mutex_unlock(  &((*conn)->c_mutex) );
1479
1480         return LDAP_SUCCESS;
1481 }
1482
1483
1484 void connection_internal_close( Connection *conn )
1485 {
1486         Operation *op = conn->c_ops;
1487
1488         slap_op_remove( &conn->c_ops, op );
1489         slap_op_free( op );
1490         connection_closing( conn );
1491         connection_close( conn );
1492 }