connection.c 40.7 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-2004 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
#include <limits.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
37
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38
39
#include "slap.h"

40
#ifdef LDAP_SLAPI
41
#include "slapi/slapi.h"
42
43
#endif

44
45
46
/* protected by connections_mutex */
static ldap_pvt_thread_mutex_t connections_mutex;
static Connection *connections = NULL;
47
48

static ldap_pvt_thread_mutex_t conn_nextid_mutex;
49
static unsigned long conn_nextid = 0;
50

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

/* connection state (protected by c_mutex ) */
57
58
59
60
61
#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 */
62
#define SLAP_C_CLIENT			0x05	/* outbound client conn */
63

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

	return "?";
}

79
static Connection* connection_get( ber_socket_t s );
80
81

static int connection_input( Connection *c );
82
static void connection_close( Connection *c );
83

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

89
90
static ldap_pvt_thread_start_t connection_operation;

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

98
99
	assert( connections == NULL );

100
	if( connections != NULL) {
101
102
103
104
105
106
107
		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 );
108
	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
109

110
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
111
112
113

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

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

122
	for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
123

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

	return 0;
}

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

	/* should check return of every call */

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

147
	for ( i = 0; i < dtblsize; i++ ) {
148
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
149
			ber_sockbuf_free( connections[i].c_sb );
150
151
152
			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 );
153
#ifdef LDAP_SLAPI
154
			if ( slapi_plugins_used ) {
155
				slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
Howard Chu's avatar
Howard Chu committed
156
			}
157
#endif
158
		}
159
160
161
162
163
164
	}

	free( connections );
	connections = NULL;

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

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

	ldap_pvt_thread_mutex_lock( &connections_mutex );

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

		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
190
191

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

195
196
197
198
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
199
200

	return 0;
201
202
}

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

Gary Williams's avatar
Gary Williams committed
212
	for( c = connection_first( &connindex );
213
214
215
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
216
217
218
219
		/* 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;
220

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

	return i;
}

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

237
238
239
	Connection *c;

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

	assert( connections != NULL );

245
	if(s == AC_SOCKET_INVALID) {
246
247
248
249
250
		return NULL;
	}

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

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

254
#else
255
	c = NULL;
256
	{
257
		ber_socket_t i, sd;
258

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

266
267
268
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

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

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

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

287
	if( c != NULL ) {
288
289
		ber_socket_t	sd;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
290
291
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

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

296
			assert( c->c_conn_state == SLAP_C_INVALID );
297
			assert( sd == AC_SOCKET_INVALID );
298
299

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

303
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
304
305
306
			return NULL;
		}

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

311
312
		c->c_n_get++;

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

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

325
326
327
328
329
330
331
332
333
	return c;
}

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

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

345
346
	assert( connections != NULL );

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

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

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

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

	ldap_pvt_thread_mutex_lock( &connections_mutex );

#ifndef HAVE_WINSOCK
	c = &connections[s];

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

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
379
380
381
382
383
			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
384

385
			sd = AC_SOCKET_INVALID;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
386
387
388
389
			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
390

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
391
392
393
394
395
			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
396

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
401
402
403
404
405
406
407
			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,
408
				"connection_init(%d): connection table full "
409
				"(%d/%d)\n", s, i, dtblsize);
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
410
411
412
			ldap_pvt_thread_mutex_unlock( &connections_mutex );
			return -1;
		}
Gary Williams's avatar
Gary Williams committed
413
	}
414
415
#endif

416
	assert( c != NULL );
417

418
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
419
420
421
422
		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;
423
424
#ifdef LDAP_RES_INTERMEDIATE
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
425
#endif
426

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

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

435
436
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
437

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

445
446
447
		c->c_sb = ber_sockbuf_alloc( );

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
452
		c->c_currentber = NULL;
453

454
455
456
457
		/* 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 );
458

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

465
466
		c->c_struct_state = SLAP_C_UNUSED;
	}
467

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
470
	assert( c->c_struct_state == SLAP_C_UNUSED );
471
472
473
	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
474
	assert( c->c_listener == NULL );
475
476
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
477
478
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
479
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
480
481
482
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
483
	assert( c->c_sasl_extra == NULL );
484
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
485
	assert( c->c_currentber == NULL );
486
	assert( c->c_writewaiter == 0);
487

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

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

		return 0;
	}

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

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

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

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

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

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

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
550
551
552
553
554
	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
555
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
556
	}
557

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

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

565
566
567
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
568
#ifdef HAVE_TLS
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
569
570
571
572
573
574
575
	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
576
#endif
577

578
	slap_sasl_open( c, 0 );
579
	slap_sasl_external( c, ssf, authid );
580

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
584
	backend_connection_init(c);
585

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
586
	return id;
587
588
}

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

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

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

604
605
606
	if(c->c_dn.bv_val != NULL) {
		free(c->c_dn.bv_val);
	}
607
	BER_BVZERO( &c->c_dn );
608
609
610
	if(c->c_ndn.bv_val != NULL) {
		free(c->c_ndn.bv_val);
	}
611
	BER_BVZERO( &c->c_ndn );
612
613
614
615

	c->c_authz_backend = NULL;
}

616
617
618
static void
connection_destroy( Connection *c )
{
619
	/* note: connections_mutex should be locked by caller */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
620
621
	ber_socket_t	sd;
	unsigned long	connid;
622

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
623
624
625
626
627
	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) );
628
	assert( c->c_writewaiter == 0);
629

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
633
	backend_connection_destroy(c);
634

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
635
636
	c->c_protocol = 0;
	c->c_connid = -1;
637

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
638
	c->c_activitytime = c->c_starttime = 0;
639

640
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
641
	c->c_listener = NULL;
642

643
644
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
645
	}
646
	BER_BVZERO( &c->c_peer_domain );
647
648
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
649
	}
650
	BER_BVZERO( &c->c_peer_name );
651

652
	c->c_sasl_bind_in_progress = 0;
653
654
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
655
	}
656
	BER_BVZERO( &c->c_sasl_bind_mech );
657
658

	slap_sasl_close( c );
659

Kurt Zeilenga's avatar
Kurt Zeilenga committed
660
661
662
663
664
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

665
666
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
	if ( sd != AC_SOCKET_INVALID ) {
667
		slapd_remove( sd, 1, 0 );
668
669

		Statslog( LDAP_DEBUG_STATS,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
670
			"conn=%lu fd=%ld closed\n",
671
			connid, (long) sd, 0, 0, 0 );
672
673
	}

Gary Williams's avatar
Gary Williams committed
674
	ber_sockbuf_free( c->c_sb );
675

676
	c->c_sb = ber_sockbuf_alloc( );
677

678
	{
679
		ber_len_t max = sockbuf_max_incoming;
680
681
682
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
683
684
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
685
686
687

#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
688
	if ( slapi_plugins_used ) {
689
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
690
	}
691
#endif
692
693
}

694
695
int connection_state_closing( Connection *c )
{
696
697
	/* c_mutex must be locked by caller */

698
	int state;
699
700
701
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

702
703
704
705
706
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
707
708
}

709
710
711
712
713
714
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

	Operation *o;

715
	LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
716
717
718
719
		o->o_abandon = 1;
	}

	/* remove pending operations */
720
721
722
	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;
723
724
725
726
		slap_op_free( o );
	}
}

727
728
729
730
731
732
733
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 );

734
735
	/* c_mutex must be locked by caller */

736
	if( c->c_conn_state != SLAP_C_CLOSING ) {
737
738
739
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
740
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
741
			"connection_closing: readying conn=%lu sd=%d for close\n",
742
			c->c_connid, sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
743
744
745
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;

746
		/* don't listen on this port anymore */
747
		slapd_clr_read( sd, 1 );
748
749

		/* abandon active operations */
750
		connection_abandon( c );
751
752

		/* wake write blocked operations */
753
		slapd_clr_write( sd, 1 );
754
		ldap_pvt_thread_cond_signal( &c->c_write_cv );
755
756
757
	}
}

758
759
static void connection_close( Connection *c )
{
760
761
	ber_socket_t	sd;

762
763
764
765
766
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

767
	/* note: connections_mutex and c_mutex should be locked by caller */
768

769
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
770
	if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
771
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
772
			"connection_close: deferring conn=%lu sd=%d\n",
773
			c->c_connid, sd, 0 );
774
775
776
		return;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
777
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
778
		c->c_connid, sd, 0 );
779
780
781
	connection_destroy( c );
}

782
unsigned long connections_nextid(void)
783
{
784
	unsigned long id;
785
786
787
788
789
790
791
792
793
794
795
	assert( connections != NULL );

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	id = conn_nextid;

	ldap_pvt_thread_mutex_unlock( &connections_mutex );

	return id;
}

796
Connection* connection_first( ber_socket_t *index )
797
798
{
	assert( connections != NULL );
799
	assert( index != NULL );
800
801
802

	ldap_pvt_thread_mutex_lock( &connections_mutex );

803
	*index = 0;
804

805
	return connection_next(NULL, index);
806
807
}

808
Connection* connection_next( Connection *c, ber_socket_t *index )
809
810
{
	assert( connections != NULL );
811
	assert( index != NULL );
812
	assert( *index <= dtblsize );
813
814
815
816
817
818
819

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

	c = NULL;

820
	for(; *index < dtblsize; (*index)++) {
821
822
		if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
			assert( connections[*index].c_conn_state == SLAP_C_INVALID );
823
824
825
826
827
828
829
#ifndef HAVE_WINSOCK
			continue;
#else
			break;
#endif
		}

830
831
832
		if( connections[*index].c_struct_state == SLAP_C_USED ) {
			assert( connections[*index].c_conn_state != SLAP_C_INVALID );
			c = &connections[(*index)++];
833
834
835
			break;
		}

836
837
		assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
		assert( connections[*index].c_conn_state == SLAP_C_INVALID );
838
839
840
841
842
843
844
845
846
	}

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

	return c;
}

847
void connection_done( Connection *c )
848
849
850
851
852
853
854
855
856
857
{
	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
858
859
860
861
862
863
/*
 * 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.
 */

864
#ifdef SLAPD_MONITOR
865
/* FIXME: returns 0 in case of failure */
866
#define INCR_OP_INITIATED(index) \
867
	do { \
868
869
870
		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 ); \
871
	} while (0)
872
873
#define INCR_OP_COMPLETED(index) \
	do { \
874
875
876
877
		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 ); \
878
	} while (0)
879
#else /* !SLAPD_MONITOR */
880
881
#define INCR_OP_INITIATED(index) 
#define INCR_OP_COMPLETED(index) 
882
#endif /* !SLAPD_MONITOR */
883

884
static void *
885
connection_operation( void *ctx, void *arg_v )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
886
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
887
	int rc = LDAP_OTHER;
888
	Operation *op = arg_v;
889
	SlapReply rs = {REP_RESULT};
890
	ber_tag_t tag = op->o_tag;
891
892
893
#ifdef SLAPD_MONITOR
	ber_tag_t oldtag = tag;
#endif /* SLAPD_MONITOR */
894
	Connection *conn = op->o_conn;
895
	void *memctx = NULL;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
896
	void *memctx_null = NULL;