connection.c 41.3 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2005 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
25
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
#ifdef HAVE_LIMITS_H
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
#include <limits.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32

33
#include <ac/socket.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
34
35
36
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/time.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
37
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38

Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
#include "slap.h"

42
#ifdef LDAP_SLAPI
43
#include "slapi/slapi.h"
44
45
#endif

46
47
48
/* protected by connections_mutex */
static ldap_pvt_thread_mutex_t connections_mutex;
static Connection *connections = NULL;
49
50

static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51
static unsigned long conn_nextid = 0;
52

53
/* structure state (protected by connections_mutex) */
54
55
56
#define SLAP_C_UNINITIALIZED	0x00	/* MUST BE ZERO (0) */
#define SLAP_C_UNUSED			0x01
#define SLAP_C_USED				0x02
57
58

/* connection state (protected by c_mutex ) */
59
60
61
62
63
#define SLAP_C_INVALID			0x00	/* MUST BE ZERO (0) */
#define SLAP_C_INACTIVE			0x01	/* zero threads */
#define SLAP_C_ACTIVE			0x02	/* one or more threads */
#define SLAP_C_BINDING			0x03	/* binding */
#define SLAP_C_CLOSING			0x04	/* closing */
64
#define SLAP_C_CLIENT			0x05	/* outbound client conn */
65

66
67
68
const char *
connection_state2str( int state )
{
69
	switch( state ) {
70
71
72
	case SLAP_C_INVALID:	return "!";
	case SLAP_C_INACTIVE:	return "|";
	case SLAP_C_ACTIVE:		return "";
73
	case SLAP_C_BINDING:	return "B";
74
75
	case SLAP_C_CLOSING:	return "C";
	case SLAP_C_CLIENT:		return "L";
76
77
78
79
80
	}

	return "?";
}

81
static Connection* connection_get( ber_socket_t s );
82
83

static int connection_input( Connection *c );
84
static void connection_close( Connection *c );
85

86
static int connection_op_activate( Operation *op );
87
static int connection_resched( Connection *conn );
88
static void connection_abandon( Connection *conn );
89
static void connection_destroy( Connection *c );
90

91
92
static ldap_pvt_thread_start_t connection_operation;

93
94
95
96
97
/*
 * Initialize connection management infrastructure.
 */
int connections_init(void)
{
98
99
	int i;

100
101
	assert( connections == NULL );

102
	if( connections != NULL) {
103
104
105
106
107
108
109
		Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
			0, 0, 0 );
		return -1;
	}

	/* should check return of every call */
	ldap_pvt_thread_mutex_init( &connections_mutex );
110
	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
111

112
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
113
114
115

	if( connections == NULL ) {
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
116
			"connections_init: allocation (%d*%ld) of connection array failed\n",
117
			dtblsize, (long) sizeof(Connection), 0 );
118
119
120
		return -1;
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
121
	assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
122
	assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
123

124
	for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
125

126
127
128
129
130
131
132
133
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

134
135
136
137
138
/*
 * Destroy connection management infrastructure.
 */
int connections_destroy(void)
{
139
	ber_socket_t i;
140
141
142
143
144
145
146
147
148

	/* should check return of every call */

	if( connections == NULL) {
		Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
			0, 0, 0 );
		return -1;
	}

149
	for ( i = 0; i < dtblsize; i++ ) {
150
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
151
			ber_sockbuf_free( connections[i].c_sb );
152
153
154
			ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
155
#ifdef LDAP_SLAPI
156
			if ( slapi_plugins_used ) {
157
				slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
Howard Chu's avatar
Howard Chu committed
158
			}
159
#endif
160
		}
161
162
163
164
165
166
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
167
	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
168
169
170
171
172
173
174
175
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
176
	ber_socket_t i;
177
178
179

	ldap_pvt_thread_mutex_lock( &connections_mutex );

180
	for ( i = 0; i < dtblsize; i++ ) {
181
182
183
		if( connections[i].c_struct_state != SLAP_C_USED ) {
			continue;
		}
184
185
		/* give persistent clients a chance to cleanup */
		if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
186
			ldap_pvt_thread_pool_submit( &connection_pool,
187
188
189
			connections[i].c_clientfunc, connections[i].c_clientarg );
			continue;
		}
190
191

		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
192
193

		/* connections_mutex and c_mutex are locked */
194
195
		connection_closing( &connections[i] );
		connection_close( &connections[i] );
196

197
198
199
200
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
201
202

	return 0;
203
204
}

205
206
207
208
209
210
211
212
213
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
	int i = 0;
	int connindex;
	Connection* c;

Gary Williams's avatar
Gary Williams committed
214
	for( c = connection_first( &connindex );
215
216
217
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
218
219
220
221
		/* Don't timeout a slow-running request or a persistent
		 * outbound connection */
		if( c->c_n_ops_executing ||
			c->c_conn_state == SLAP_C_CLIENT ) continue;
222

223
		if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
224
225
226
227
228
229
230
231
232
233
234
			/* close it */
			connection_closing( c );
			connection_close( c );
			i++;
		}
	}
	connection_done( c );

	return i;
}

235
static Connection* connection_get( ber_socket_t s )
236
{
237
238
	/* connections_mutex should be locked by caller */

239
240
241
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
242
243
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
244
245
246

	assert( connections != NULL );

247
	if(s == AC_SOCKET_INVALID) {
248
249
250
251
252
		return NULL;
	}

#ifndef HAVE_WINSOCK
	c = &connections[s];
253

Kurt Zeilenga's avatar
Kurt Zeilenga committed
254
	assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
255

256
#else
257
	c = NULL;
258
	{
259
		ber_socket_t i, sd;
260

261
		for(i=0; i<dtblsize; i++) {
Gary Williams's avatar
NT port    
Gary Williams committed
262
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
263
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
264
				assert( connections[i].c_sb == 0 );
265
266
267
				break;
			}

268
269
270
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

Gary Williams's avatar
NT port    
Gary Williams committed
271
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
272
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
273
				assert( sd == AC_SOCKET_INVALID );
274
275
276
				continue;
			}

277
278
279
			/* state can actually change from used -> unused by resched,
			 * so don't assert details here.
			 */
280

281
			if( sd == s ) {
Gary Williams's avatar
NT port    
Gary Williams committed
282
				c = &connections[i];
283
284
285
286
287
				break;
			}
		}
	}
#endif
288

289
	if( c != NULL ) {
290
291
		ber_socket_t	sd;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
292
293
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

294
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
295
296
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
297

298
			assert( c->c_conn_state == SLAP_C_INVALID );
299
			assert( sd == AC_SOCKET_INVALID );
300
301

			Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
302
303
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
304

305
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
306
307
308
			return NULL;
		}

309
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
310
			"connection_get(%d): got connid=%lu\n",
311
			s, c->c_connid, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
312

313
314
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
315
316
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
317
		assert( sd != AC_SOCKET_INVALID );
318

319
#ifndef SLAPD_MONITOR
320
		if ( global_idletimeout > 0 )
321
322
#endif /* ! SLAPD_MONITOR */
		{
323
324
			c->c_activitytime = slap_get_time();
		}
325
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
326

327
328
329
330
331
332
333
334
335
	return c;
}

static void connection_return( Connection *c )
{
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
}

long connection_init(
336
	ber_socket_t s,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
337
	Listener *listener,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
338
339
	const char* dnsname,
	const char* peername,
340
	int flags,
341
	slap_ssf_t ssf,
342
	struct berval *authid )
343
{
344
	unsigned long id;
345
	Connection *c;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
346

347
348
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
349
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
350
351
352
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
353
#ifndef HAVE_TLS
354
	assert( flags != CONN_IS_TLS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
355
356
#endif

357
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
358
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
359
			"connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
360
361
362
363
364
		return -1;
	}

	assert( s >= 0 );
#ifndef HAVE_WINSOCK
365
	assert( s < dtblsize );
366
367
368
369
370
371
372
373
374
#endif

	ldap_pvt_thread_mutex_lock( &connections_mutex );

#ifndef HAVE_WINSOCK
	c = &connections[s];

#else
	{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
375
		ber_socket_t i;
376
377
		c = NULL;

378
		for( i=0; i < dtblsize; i++) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
379
			ber_socket_t	sd;
380

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
381
382
383
384
385
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
				assert( connections[i].c_sb == 0 );
				c = &connections[i];
				break;
			}
Gary Williams's avatar
NT port    
Gary Williams committed
386

387
			sd = AC_SOCKET_INVALID;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
388
389
390
391
			if (connections[i].c_sb != NULL) {
				ber_sockbuf_ctrl( connections[i].c_sb,
					LBER_SB_OPT_GET_FD, &sd );
			}
Gary Williams's avatar
Gary Williams committed
392

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
393
394
395
396
397
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
				assert( sd == AC_SOCKET_INVALID );
				c = &connections[i];
				break;
			}
Gary Williams's avatar
Gary Williams committed
398

399
400
401
402
			if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
				continue;
			}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
403
404
405
406
407
408
409
			assert( connections[i].c_struct_state == SLAP_C_USED );
			assert( connections[i].c_conn_state != SLAP_C_INVALID );
			assert( sd != AC_SOCKET_INVALID );
		}

		if( c == NULL ) {
			Debug( LDAP_DEBUG_ANY,
410
				"connection_init(%d): connection table full "
411
				"(%d/%d)\n", s, i, dtblsize);
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
412
413
414
			ldap_pvt_thread_mutex_unlock( &connections_mutex );
			return -1;
		}
Gary Williams's avatar
Gary Williams committed
415
	}
416
417
#endif

418
	assert( c != NULL );
419

420
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
421
422
423
424
		c->c_send_ldap_result = slap_send_ldap_result;
		c->c_send_search_entry = slap_send_search_entry;
		c->c_send_search_reference = slap_send_search_reference;
		c->c_send_ldap_extended = slap_send_ldap_extended;
425
426
#ifdef LDAP_RES_INTERMEDIATE
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
427
#endif
428

429
430
431
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
432

Pierangelo Masarati's avatar
Pierangelo Masarati committed
433
		c->c_listener = NULL;
434
435
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
436

437
438
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
439

440
		BER_BVZERO( &c->c_sasl_bind_mech );
441
442
443
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
444
		c->c_sasl_extra = NULL;
445
		c->c_sasl_bindop = NULL;
446

447
448
449
		c->c_sb = ber_sockbuf_alloc( );

		{
450
			ber_len_t max = sockbuf_max_incoming;
451
452
453
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
454
		c->c_currentber = NULL;
455

456
457
458
459
		/* should check status of thread calls */
		ldap_pvt_thread_mutex_init( &c->c_mutex );
		ldap_pvt_thread_mutex_init( &c->c_write_mutex );
		ldap_pvt_thread_cond_init( &c->c_write_cv );
460

461
#ifdef LDAP_SLAPI
462
		if ( slapi_plugins_used ) {
463
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
464
		}
465
466
#endif

467
468
		c->c_struct_state = SLAP_C_UNUSED;
	}
469

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
470
	ldap_pvt_thread_mutex_lock( &c->c_mutex );
471

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
472
	assert( c->c_struct_state == SLAP_C_UNUSED );
473
474
475
	assert( BER_BVISNULL( &c->c_authmech ) );
	assert( BER_BVISNULL( &c->c_dn ) );
	assert( BER_BVISNULL( &c->c_ndn ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
476
	assert( c->c_listener == NULL );
477
478
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
479
480
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
481
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
482
483
484
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
485
	assert( c->c_sasl_extra == NULL );
486
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
487
	assert( c->c_currentber == NULL );
488
	assert( c->c_writewaiter == 0);
489

Pierangelo Masarati's avatar
Pierangelo Masarati committed
490
	c->c_listener = listener;
491
492
493
494

	if ( flags == CONN_IS_CLIENT ) {
		c->c_conn_state = SLAP_C_CLIENT;
		c->c_struct_state = SLAP_C_USED;
495
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
496
497
498
499
500
501
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
		ldap_pvt_thread_mutex_unlock( &connections_mutex );

		return 0;
	}

502
503
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
504

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
505
506
507
508
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
509
510
511
512

	c->c_n_get = 0;
	c->c_n_read = 0;
	c->c_n_write = 0;
513

514
515
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516

517
#ifndef SLAPD_MONITOR
518
	if ( global_idletimeout > 0 )
519
520
#endif /* ! SLAPD_MONITOR */
	{
521
522
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
523

524
525
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
526
	if( flags == CONN_IS_UDP ) {
527
528
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
529
530
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
531
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
532
533
534
535
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
			LBER_SBIOD_LEVEL_PROVIDER, NULL );
536
537
538
	} else
#endif
	{
539
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
540
541
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
542
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
543
544
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
545
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
546
547
548
549

#ifdef LDAP_DEBUG
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		INT_MAX, (void*)"ldap_" );
550
#endif
551

Kurt Zeilenga's avatar
Kurt Zeilenga committed
552
553
554
555
556
	if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
		c /* non-NULL */ ) < 0 )
	{
		Debug( LDAP_DEBUG_ANY,
			"connection_init(%d, %s): set nonblocking failed\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
557
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
558
	}
559

560
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
561
	id = c->c_connid = conn_nextid++;
562
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
563

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
564
565
	c->c_conn_state = SLAP_C_INACTIVE;
	c->c_struct_state = SLAP_C_USED;
566

567
568
569
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
570
#ifdef HAVE_TLS
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
571
572
573
574
575
576
577
	if ( flags == CONN_IS_TLS ) {
		c->c_is_tls = 1;
		c->c_needs_tls_accept = 1;
	} else {
		c->c_is_tls = 0;
		c->c_needs_tls_accept = 0;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
578
#endif
579

580
	slap_sasl_open( c, 0 );
581
	slap_sasl_external( c, ssf, authid );
582

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
583
584
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	ldap_pvt_thread_mutex_unlock( &connections_mutex );
585

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
586
	backend_connection_init(c);
587

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
588
	return id;
589
590
}

591
592
void connection2anonymous( Connection *c )
{
593
594
	assert( connections != NULL );
	assert( c != NULL );
595

596
	{
597
		ber_len_t max = sockbuf_max_incoming;
598
599
600
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

601
602
	if(c->c_authmech.bv_val != NULL ) {
		free(c->c_authmech.bv_val);
603
	}
604
	BER_BVZERO( &c->c_authmech );
605

606
607
608
	if(c->c_dn.bv_val != NULL) {
		free(c->c_dn.bv_val);
	}
609
	BER_BVZERO( &c->c_dn );
610
611
612
	if(c->c_ndn.bv_val != NULL) {
		free(c->c_ndn.bv_val);
	}
613
	BER_BVZERO( &c->c_ndn );
614
615
616
617
	if(c->c_sasl_authz_dn.bv_val != NULL) {
		free(c->c_sasl_authz_dn.bv_val);
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
618
619
620
621

	c->c_authz_backend = NULL;
}

622
623
624
static void
connection_destroy( Connection *c )
{
625
	/* note: connections_mutex should be locked by caller */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
626
627
	ber_socket_t	sd;
	unsigned long	connid;
628

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
629
630
631
632
633
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state != SLAP_C_UNUSED );
	assert( c->c_conn_state != SLAP_C_INVALID );
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
634
	assert( c->c_writewaiter == 0);
635

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
636
637
	/* only for stats (print -1 as "%lu" may give unexpected results ;) */
	connid = c->c_connid;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
638

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
639
	backend_connection_destroy(c);
640

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
641
642
	c->c_protocol = 0;
	c->c_connid = -1;
643

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
644
	c->c_activitytime = c->c_starttime = 0;
645

646
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
647
	c->c_listener = NULL;
648

649
650
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
651
	}
652
	BER_BVZERO( &c->c_peer_domain );
653
654
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
655
	}
656
	BER_BVZERO( &c->c_peer_name );
657

658
	c->c_sasl_bind_in_progress = 0;
659
660
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
661
	}
662
	BER_BVZERO( &c->c_sasl_bind_mech );
663
664

	slap_sasl_close( c );
665

Kurt Zeilenga's avatar
Kurt Zeilenga committed
666
667
668
669
670
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

671
672
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
	if ( sd != AC_SOCKET_INVALID ) {
673
		slapd_remove( sd, 1, 0 );
674
675

		Statslog( LDAP_DEBUG_STATS,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
676
			"conn=%lu fd=%ld closed\n",
677
			connid, (long) sd, 0, 0, 0 );
678
679
	}

Gary Williams's avatar
Gary Williams committed
680
	ber_sockbuf_free( c->c_sb );
681

682
	c->c_sb = ber_sockbuf_alloc( );
683

684
	{
685
		ber_len_t max = sockbuf_max_incoming;
686
687
688
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
689
690
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
691
692
693

#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
694
	if ( slapi_plugins_used ) {
695
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
696
	}
697
#endif
698
699
}

700
701
int connection_state_closing( Connection *c )
{
702
703
	/* c_mutex must be locked by caller */

704
	int state;
705
706
707
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

708
709
710
711
712
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
713
714
}

715
716
717
718
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

719
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
720
	Opheader ohdr = {0};
721
	SlapReply rs = {0};
722

Howard Chu's avatar
Howard Chu committed
723
724
725
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
726
727
728
729
	op.o_tag = LDAP_REQ_ABANDON;
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
		next = LDAP_STAILQ_NEXT( o, o_next );
		op.orn_msgid = o->o_msgid;
730
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
731
		op.o_bd = frontendDB;
732
		frontendDB->be_abandon( &op, &rs );
733
734
735
	}

	/* remove pending operations */
736
737
738
	while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
		LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
		LDAP_STAILQ_NEXT(o, o_next) = NULL;
739
740
741
742
		slap_op_free( o );
	}
}

743
744
745
746
747
748
749
void connection_closing( Connection *c )
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

750
751
	/* c_mutex must be locked by caller */

752
	if( c->c_conn_state != SLAP_C_CLOSING ) {
753
754
755
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
756
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
757
			"connection_closing: readying conn=%lu sd=%d for close\n",
758
			c->c_connid, sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
759
760
761
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;

762
		/* don't listen on this port anymore */
763
		slapd_clr_read( sd, 1 );
764
765

		/* abandon active operations */
766
		connection_abandon( c );
767
768

		/* wake write blocked operations */
769
		slapd_clr_write( sd, 1 );
770
771
772
773
774
775
		if ( c->c_writewaiter ) {
			ldap_pvt_thread_cond_signal( &c->c_write_cv );
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
			ldap_pvt_thread_yield();
			ldap_pvt_thread_mutex_lock( &c->c_mutex );
		}
776
777
778
	}
}

779
780
static void connection_close( Connection *c )
{
781
782
	ber_socket_t	sd;

783
784
785
786
787
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

788
	/* note: connections_mutex and c_mutex should be locked by caller */
789

790
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
791
	if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
792
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
793
			"connection_close: deferring conn=%lu sd=%d\n",
794
			c->c_connid, sd, 0 );
795
796
797
		return;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
798
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
799
		c->c_connid, sd, 0 );
800
801
802
	connection_destroy( c );
}

803
unsigned long connections_nextid(void)
804
{
805
	unsigned long id;
806
807
808
809
810
811
812
813
814
815
816
	assert( connections != NULL );

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	id = conn_nextid;

	ldap_pvt_thread_mutex_unlock( &connections_mutex );

	return id;
}

817
Connection* connection_first( ber_socket_t *index )
818
819
{
	assert( connections != NULL );
820
	assert( index != NULL );
821
822
823

	ldap_pvt_thread_mutex_lock( &connections_mutex );

824
	*index = 0;
825

826
	return connection_next(NULL, index);
827
828
}

829
Connection* connection_next( Connection *c, ber_socket_t *index )
830
831
{
	assert( connections != NULL );
832
	assert( index != NULL );
833
	assert( *index <= dtblsize );
834
835
836
837
838
839
840

	if( c != NULL ) {
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	}

	c = NULL;

841
	for(; *index < dtblsize; (*index)++) {
842
843
		if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
			assert( connections[*index].c_conn_state == SLAP_C_INVALID );
844
845
846
847
848
849
850
#ifndef HAVE_WINSOCK
			continue;
#else
			break;
#endif
		}

851
852
853
		if( connections[*index].c_struct_state == SLAP_C_USED ) {
			assert( connections[*index].c_conn_state != SLAP_C_INVALID );
			c = &connections[(*index)++];
854
855
856
			break;
		}

857
858
		assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
		assert( connections[*index].c_conn_state == SLAP_C_INVALID );
859
860
861
862
863
864
865
866
867
	}

	if( c != NULL ) {
		ldap_pvt_thread_mutex_lock( &c->c_mutex );
	}

	return c;
}

868
void connection_done( Connection *c )
869
870
871
872
873
874
875
876
877
878
{
	assert( connections != NULL );

	if( c != NULL ) {
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
879
880
881
882
883
884
/*
 * connection_activity - handle the request operation op on connection
 * conn.  This routine figures out what kind of operation it is and
 * calls the appropriate stub to handle it.
 */

885
#ifdef SLAPD_MONITOR
886
/* FIXME: returns 0 in case of failure */
887
#define INCR_OP_INITIATED(index) \
888
	do { \
889
890
891
		ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
		ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
		ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
892
	} while (0)
893
894
#define INCR_OP_COMPLETED(index) \
	do { \
895
896
897
898
		ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
		ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
		ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
		ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
899
	} while (0)
900
#else /* !SLAPD_MONITOR */
901
#define INCR_OP_INITIATED(index)