client.c revision 1.1 1 1.1 christos /* $NetBSD: client.c,v 1.1 2021/08/14 16:05:25 christos Exp $ */
2 1.1 christos
3 1.1 christos /* $OpenLDAP$ */
4 1.1 christos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 1.1 christos *
6 1.1 christos * Copyright 1998-2021 The OpenLDAP Foundation.
7 1.1 christos * All rights reserved.
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted only as authorized by the OpenLDAP
11 1.1 christos * Public License.
12 1.1 christos *
13 1.1 christos * A copy of this license is available in the file LICENSE in the
14 1.1 christos * top-level directory of the distribution or, alternatively, at
15 1.1 christos * <http://www.OpenLDAP.org/license.html>.
16 1.1 christos */
17 1.1 christos
18 1.1 christos #include <sys/cdefs.h>
19 1.1 christos __RCSID("$NetBSD: client.c,v 1.1 2021/08/14 16:05:25 christos Exp $");
20 1.1 christos
21 1.1 christos #include "portable.h"
22 1.1 christos
23 1.1 christos #include <ac/socket.h>
24 1.1 christos #include <ac/errno.h>
25 1.1 christos #include <ac/string.h>
26 1.1 christos #include <ac/time.h>
27 1.1 christos #include <ac/unistd.h>
28 1.1 christos
29 1.1 christos #include "lutil.h"
30 1.1 christos #include "lload.h"
31 1.1 christos
32 1.1 christos long lload_client_max_pending = 0;
33 1.1 christos
34 1.1 christos lload_c_head clients = LDAP_CIRCLEQ_HEAD_INITIALIZER( clients );
35 1.1 christos
36 1.1 christos ldap_pvt_thread_mutex_t clients_mutex;
37 1.1 christos
38 1.1 christos static void client_unlink( LloadConnection *upstream );
39 1.1 christos
40 1.1 christos int
41 1.1 christos request_abandon( LloadConnection *c, LloadOperation *op )
42 1.1 christos {
43 1.1 christos LloadOperation *request, needle = { .o_client_connid = c->c_connid };
44 1.1 christos int rc = LDAP_SUCCESS;
45 1.1 christos
46 1.1 christos op->o_res = LLOAD_OP_COMPLETED;
47 1.1 christos
48 1.1 christos if ( ber_decode_int( &op->o_request, &needle.o_client_msgid ) ) {
49 1.1 christos Debug( LDAP_DEBUG_STATS, "request_abandon: "
50 1.1 christos "connid=%lu msgid=%d invalid integer sent in abandon request\n",
51 1.1 christos c->c_connid, op->o_client_msgid );
52 1.1 christos
53 1.1 christos operation_unlink( op );
54 1.1 christos CONNECTION_LOCK_DESTROY(c);
55 1.1 christos return -1;
56 1.1 christos }
57 1.1 christos
58 1.1 christos CONNECTION_LOCK(c);
59 1.1 christos request = ldap_tavl_find( c->c_ops, &needle, operation_client_cmp );
60 1.1 christos if ( !request ) {
61 1.1 christos Debug( LDAP_DEBUG_STATS, "request_abandon: "
62 1.1 christos "connid=%lu msgid=%d requests abandon of an operation "
63 1.1 christos "msgid=%d not being processed anymore\n",
64 1.1 christos c->c_connid, op->o_client_msgid, needle.o_client_msgid );
65 1.1 christos CONNECTION_UNLOCK(c);
66 1.1 christos goto done;
67 1.1 christos } else if ( request->o_tag == LDAP_REQ_BIND ) {
68 1.1 christos /* RFC 4511 states we must not allow Abandon on Binds */
69 1.1 christos Debug( LDAP_DEBUG_STATS, "request_abandon: "
70 1.1 christos "connid=%lu msgid=%d requests abandon of a bind operation "
71 1.1 christos "msgid=%d\n",
72 1.1 christos c->c_connid, op->o_client_msgid, needle.o_client_msgid );
73 1.1 christos CONNECTION_UNLOCK(c);
74 1.1 christos goto done;
75 1.1 christos }
76 1.1 christos Debug( LDAP_DEBUG_STATS, "request_abandon: "
77 1.1 christos "connid=%lu msgid=%d abandoning %s msgid=%d\n",
78 1.1 christos c->c_connid, op->o_client_msgid,
79 1.1 christos lload_msgtype2str( request->o_tag ), needle.o_client_msgid );
80 1.1 christos
81 1.1 christos if ( c->c_state == LLOAD_C_BINDING ) {
82 1.1 christos assert(0);
83 1.1 christos }
84 1.1 christos
85 1.1 christos CONNECTION_UNLOCK(c);
86 1.1 christos operation_abandon( request );
87 1.1 christos
88 1.1 christos done:
89 1.1 christos operation_unlink( op );
90 1.1 christos return rc;
91 1.1 christos }
92 1.1 christos
93 1.1 christos int
94 1.1 christos request_process( LloadConnection *client, LloadOperation *op )
95 1.1 christos {
96 1.1 christos BerElement *output;
97 1.1 christos LloadConnection *upstream;
98 1.1 christos ber_int_t msgid;
99 1.1 christos int res, rc = LDAP_SUCCESS;
100 1.1 christos
101 1.1 christos upstream = backend_select( op, &res );
102 1.1 christos if ( !upstream ) {
103 1.1 christos Debug( LDAP_DEBUG_STATS, "request_process: "
104 1.1 christos "connid=%lu, msgid=%d no available connection found\n",
105 1.1 christos op->o_client_connid, op->o_client_msgid );
106 1.1 christos
107 1.1 christos operation_send_reject( op, res, "no connections available", 1 );
108 1.1 christos goto fail;
109 1.1 christos }
110 1.1 christos CONNECTION_ASSERT_LOCKED(upstream);
111 1.1 christos assert_locked( &upstream->c_io_mutex );
112 1.1 christos op->o_upstream = upstream;
113 1.1 christos op->o_upstream_connid = upstream->c_connid;
114 1.1 christos op->o_res = LLOAD_OP_FAILED;
115 1.1 christos
116 1.1 christos /* Was it unlinked in the meantime? No need to send a response since the
117 1.1 christos * client is dead */
118 1.1 christos if ( !IS_ALIVE( op, o_refcnt ) ) {
119 1.1 christos LloadBackend *b = upstream->c_backend;
120 1.1 christos
121 1.1 christos upstream->c_n_ops_executing--;
122 1.1 christos checked_unlock( &upstream->c_io_mutex );
123 1.1 christos CONNECTION_UNLOCK(upstream);
124 1.1 christos
125 1.1 christos checked_lock( &b->b_mutex );
126 1.1 christos b->b_n_ops_executing--;
127 1.1 christos checked_unlock( &b->b_mutex );
128 1.1 christos
129 1.1 christos assert( !IS_ALIVE( client, c_live ) );
130 1.1 christos checked_lock( &op->o_link_mutex );
131 1.1 christos if ( op->o_upstream ) {
132 1.1 christos op->o_upstream = NULL;
133 1.1 christos }
134 1.1 christos checked_unlock( &op->o_link_mutex );
135 1.1 christos return -1;
136 1.1 christos }
137 1.1 christos
138 1.1 christos output = upstream->c_pendingber;
139 1.1 christos if ( output == NULL && (output = ber_alloc()) == NULL ) {
140 1.1 christos LloadBackend *b = upstream->c_backend;
141 1.1 christos
142 1.1 christos upstream->c_n_ops_executing--;
143 1.1 christos CONNECTION_UNLOCK(upstream);
144 1.1 christos checked_unlock( &upstream->c_io_mutex );
145 1.1 christos
146 1.1 christos checked_lock( &b->b_mutex );
147 1.1 christos b->b_n_ops_executing--;
148 1.1 christos operation_update_backend_counters( op, b );
149 1.1 christos checked_unlock( &b->b_mutex );
150 1.1 christos
151 1.1 christos Debug( LDAP_DEBUG_ANY, "request_process: "
152 1.1 christos "ber_alloc failed\n" );
153 1.1 christos
154 1.1 christos rc = -1;
155 1.1 christos goto fail;
156 1.1 christos }
157 1.1 christos upstream->c_pendingber = output;
158 1.1 christos
159 1.1 christos op->o_upstream_msgid = msgid = upstream->c_next_msgid++;
160 1.1 christos rc = ldap_tavl_insert(
161 1.1 christos &upstream->c_ops, op, operation_upstream_cmp, ldap_avl_dup_error );
162 1.1 christos CONNECTION_UNLOCK(upstream);
163 1.1 christos
164 1.1 christos Debug( LDAP_DEBUG_TRACE, "request_process: "
165 1.1 christos "client connid=%lu added %s msgid=%d to upstream connid=%lu as "
166 1.1 christos "msgid=%d\n",
167 1.1 christos op->o_client_connid, lload_msgtype2str( op->o_tag ),
168 1.1 christos op->o_client_msgid, op->o_upstream_connid, op->o_upstream_msgid );
169 1.1 christos assert( rc == LDAP_SUCCESS );
170 1.1 christos
171 1.1 christos lload_stats.counters[LLOAD_STATS_OPS_OTHER].lc_ops_forwarded++;
172 1.1 christos
173 1.1 christos if ( (lload_features & LLOAD_FEATURE_PROXYAUTHZ) &&
174 1.1 christos client->c_type != LLOAD_C_PRIVILEGED ) {
175 1.1 christos CONNECTION_LOCK(client);
176 1.1 christos Debug( LDAP_DEBUG_TRACE, "request_process: "
177 1.1 christos "proxying identity %s to upstream\n",
178 1.1 christos client->c_auth.bv_val );
179 1.1 christos ber_printf( output, "t{titOt{{sbO}" /* "}}" */, LDAP_TAG_MESSAGE,
180 1.1 christos LDAP_TAG_MSGID, msgid,
181 1.1 christos op->o_tag, &op->o_request,
182 1.1 christos LDAP_TAG_CONTROLS,
183 1.1 christos LDAP_CONTROL_PROXY_AUTHZ, 1, &client->c_auth );
184 1.1 christos CONNECTION_UNLOCK(client);
185 1.1 christos
186 1.1 christos if ( !BER_BVISNULL( &op->o_ctrls ) ) {
187 1.1 christos ber_write( output, op->o_ctrls.bv_val, op->o_ctrls.bv_len, 0 );
188 1.1 christos }
189 1.1 christos
190 1.1 christos ber_printf( output, /* "{{" */ "}}" );
191 1.1 christos } else {
192 1.1 christos ber_printf( output, "t{titOtO}", LDAP_TAG_MESSAGE,
193 1.1 christos LDAP_TAG_MSGID, msgid,
194 1.1 christos op->o_tag, &op->o_request,
195 1.1 christos LDAP_TAG_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );
196 1.1 christos }
197 1.1 christos checked_unlock( &upstream->c_io_mutex );
198 1.1 christos
199 1.1 christos connection_write_cb( -1, 0, upstream );
200 1.1 christos return rc;
201 1.1 christos
202 1.1 christos fail:
203 1.1 christos if ( upstream ) {
204 1.1 christos CONNECTION_LOCK_DESTROY(upstream);
205 1.1 christos
206 1.1 christos operation_send_reject( op, LDAP_OTHER, "internal error", 0 );
207 1.1 christos }
208 1.1 christos
209 1.1 christos operation_unlink( op );
210 1.1 christos if ( rc ) {
211 1.1 christos CONNECTION_LOCK_DESTROY(client);
212 1.1 christos }
213 1.1 christos return rc;
214 1.1 christos }
215 1.1 christos
216 1.1 christos int
217 1.1 christos handle_one_request( LloadConnection *c )
218 1.1 christos {
219 1.1 christos BerElement *ber;
220 1.1 christos LloadOperation *op = NULL;
221 1.1 christos RequestHandler handler = NULL;
222 1.1 christos int over_limit = 0;
223 1.1 christos
224 1.1 christos ber = c->c_currentber;
225 1.1 christos c->c_currentber = NULL;
226 1.1 christos
227 1.1 christos CONNECTION_LOCK(c);
228 1.1 christos op = operation_init( c, ber );
229 1.1 christos if ( !op ) {
230 1.1 christos Debug( LDAP_DEBUG_ANY, "handle_one_request: "
231 1.1 christos "connid=%lu, operation_init failed\n",
232 1.1 christos c->c_connid );
233 1.1 christos CONNECTION_DESTROY(c);
234 1.1 christos ber_free( ber, 1 );
235 1.1 christos return -1;
236 1.1 christos }
237 1.1 christos if ( lload_client_max_pending &&
238 1.1 christos c->c_n_ops_executing >= lload_client_max_pending ) {
239 1.1 christos over_limit = 1;
240 1.1 christos }
241 1.1 christos CONNECTION_UNLOCK(c);
242 1.1 christos
243 1.1 christos switch ( op->o_tag ) {
244 1.1 christos case LDAP_REQ_UNBIND:
245 1.1 christos /* There is never a response for this operation */
246 1.1 christos op->o_res = LLOAD_OP_COMPLETED;
247 1.1 christos operation_unlink( op );
248 1.1 christos
249 1.1 christos Debug( LDAP_DEBUG_STATS, "handle_one_request: "
250 1.1 christos "received unbind, closing client connid=%lu\n",
251 1.1 christos c->c_connid );
252 1.1 christos CONNECTION_LOCK_DESTROY(c);
253 1.1 christos return -1;
254 1.1 christos case LDAP_REQ_BIND:
255 1.1 christos handler = request_bind;
256 1.1 christos break;
257 1.1 christos case LDAP_REQ_ABANDON:
258 1.1 christos /* We can't send a response to abandon requests even if a bind is
259 1.1 christos * currently in progress */
260 1.1 christos return request_abandon( c, op );
261 1.1 christos case LDAP_REQ_EXTENDED:
262 1.1 christos default:
263 1.1 christos if ( c->c_state == LLOAD_C_BINDING ) {
264 1.1 christos operation_send_reject(
265 1.1 christos op, LDAP_PROTOCOL_ERROR, "bind in progress", 0 );
266 1.1 christos return LDAP_SUCCESS;
267 1.1 christos }
268 1.1 christos if ( over_limit ) {
269 1.1 christos operation_send_reject( op, LDAP_BUSY,
270 1.1 christos "pending operation limit reached on this connection",
271 1.1 christos 0 );
272 1.1 christos return LDAP_SUCCESS;
273 1.1 christos }
274 1.1 christos if ( c->c_io_state & LLOAD_C_READ_PAUSE ) {
275 1.1 christos operation_send_reject( op, LDAP_BUSY,
276 1.1 christos "writing side backlogged, please keep reading", 0 );
277 1.1 christos return LDAP_SUCCESS;
278 1.1 christos }
279 1.1 christos if ( op->o_tag == LDAP_REQ_EXTENDED ) {
280 1.1 christos handler = request_extended;
281 1.1 christos } else {
282 1.1 christos handler = request_process;
283 1.1 christos }
284 1.1 christos break;
285 1.1 christos }
286 1.1 christos
287 1.1 christos if ( c->c_state == LLOAD_C_CLOSING ) {
288 1.1 christos operation_send_reject(
289 1.1 christos op, LDAP_UNAVAILABLE, "connection is shutting down", 0 );
290 1.1 christos return LDAP_SUCCESS;
291 1.1 christos }
292 1.1 christos
293 1.1 christos return handler( c, op );
294 1.1 christos }
295 1.1 christos
296 1.1 christos #ifdef HAVE_TLS
297 1.1 christos /*
298 1.1 christos * The connection has a token assigned to it when the callback is set up.
299 1.1 christos */
300 1.1 christos void
301 1.1 christos client_tls_handshake_cb( evutil_socket_t s, short what, void *arg )
302 1.1 christos {
303 1.1 christos LloadConnection *c = arg;
304 1.1 christos epoch_t epoch;
305 1.1 christos int rc = 0;
306 1.1 christos
307 1.1 christos if ( what & EV_TIMEOUT ) {
308 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
309 1.1 christos "connid=%lu, timeout reached, destroying\n",
310 1.1 christos c->c_connid );
311 1.1 christos goto fail;
312 1.1 christos }
313 1.1 christos
314 1.1 christos /*
315 1.1 christos * In case of StartTLS, make sure we flush the response first.
316 1.1 christos * Also before we try to read anything from the connection, it isn't
317 1.1 christos * permitted to Abandon a StartTLS exop per RFC4511 anyway.
318 1.1 christos */
319 1.1 christos checked_lock( &c->c_io_mutex );
320 1.1 christos if ( c->c_pendingber ) {
321 1.1 christos checked_unlock( &c->c_io_mutex );
322 1.1 christos connection_write_cb( s, what, arg );
323 1.1 christos
324 1.1 christos if ( !IS_ALIVE( c, c_live ) ) {
325 1.1 christos goto fail;
326 1.1 christos }
327 1.1 christos
328 1.1 christos /* Do we still have data pending? If so, connection_write_cb would
329 1.1 christos * already have arranged the write callback to trigger again */
330 1.1 christos checked_lock( &c->c_io_mutex );
331 1.1 christos if ( c->c_pendingber ) {
332 1.1 christos checked_unlock( &c->c_io_mutex );
333 1.1 christos return;
334 1.1 christos }
335 1.1 christos }
336 1.1 christos
337 1.1 christos rc = ldap_pvt_tls_accept( c->c_sb, LLOAD_TLS_CTX );
338 1.1 christos checked_unlock( &c->c_io_mutex );
339 1.1 christos if ( rc < 0 ) {
340 1.1 christos goto fail;
341 1.1 christos }
342 1.1 christos
343 1.1 christos if ( rc == 0 ) {
344 1.1 christos struct event_base *base = event_get_base( c->c_read_event );
345 1.1 christos
346 1.1 christos /*
347 1.1 christos * We're finished, replace the callbacks
348 1.1 christos *
349 1.1 christos * This is deadlock-safe, since both share the same base - the one
350 1.1 christos * that's just running us.
351 1.1 christos */
352 1.1 christos CONNECTION_LOCK(c);
353 1.1 christos event_del( c->c_read_event );
354 1.1 christos event_del( c->c_write_event );
355 1.1 christos
356 1.1 christos c->c_read_timeout = NULL;
357 1.1 christos event_assign( c->c_read_event, base, c->c_fd, EV_READ|EV_PERSIST,
358 1.1 christos connection_read_cb, c );
359 1.1 christos if ( IS_ALIVE( c, c_live ) ) {
360 1.1 christos event_add( c->c_read_event, c->c_read_timeout );
361 1.1 christos }
362 1.1 christos
363 1.1 christos event_assign( c->c_write_event, base, c->c_fd, EV_WRITE,
364 1.1 christos connection_write_cb, c );
365 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
366 1.1 christos "connid=%lu finished\n",
367 1.1 christos c->c_connid );
368 1.1 christos
369 1.1 christos c->c_is_tls = LLOAD_TLS_ESTABLISHED;
370 1.1 christos CONNECTION_UNLOCK(c);
371 1.1 christos return;
372 1.1 christos } else if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
373 1.1 christos if ( IS_ALIVE( c, c_live ) ) {
374 1.1 christos CONNECTION_LOCK(c);
375 1.1 christos event_add( c->c_write_event, lload_write_timeout );
376 1.1 christos CONNECTION_UNLOCK(c);
377 1.1 christos }
378 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
379 1.1 christos "connid=%lu need write rc=%d\n",
380 1.1 christos c->c_connid, rc );
381 1.1 christos }
382 1.1 christos return;
383 1.1 christos
384 1.1 christos fail:
385 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
386 1.1 christos "connid=%lu failed rc=%d\n",
387 1.1 christos c->c_connid, rc );
388 1.1 christos
389 1.1 christos assert( c->c_ops == NULL );
390 1.1 christos epoch = epoch_join();
391 1.1 christos CONNECTION_LOCK_DESTROY(c);
392 1.1 christos epoch_leave( epoch );
393 1.1 christos }
394 1.1 christos #endif /* HAVE_TLS */
395 1.1 christos
396 1.1 christos LloadConnection *
397 1.1 christos client_init(
398 1.1 christos ber_socket_t s,
399 1.1 christos const char *peername,
400 1.1 christos struct event_base *base,
401 1.1 christos int flags )
402 1.1 christos {
403 1.1 christos LloadConnection *c;
404 1.1 christos struct event *event;
405 1.1 christos event_callback_fn read_cb = connection_read_cb,
406 1.1 christos write_cb = connection_write_cb;
407 1.1 christos
408 1.1 christos if ( (c = lload_connection_init( s, peername, flags) ) == NULL ) {
409 1.1 christos return NULL;
410 1.1 christos }
411 1.1 christos
412 1.1 christos {
413 1.1 christos ber_len_t max = sockbuf_max_incoming_client;
414 1.1 christos ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
415 1.1 christos }
416 1.1 christos
417 1.1 christos c->c_state = LLOAD_C_READY;
418 1.1 christos
419 1.1 christos if ( flags & CONN_IS_TLS ) {
420 1.1 christos #ifdef HAVE_TLS
421 1.1 christos int rc;
422 1.1 christos
423 1.1 christos c->c_is_tls = LLOAD_LDAPS;
424 1.1 christos
425 1.1 christos rc = ldap_pvt_tls_accept( c->c_sb, LLOAD_TLS_CTX );
426 1.1 christos if ( rc < 0 ) {
427 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_init: "
428 1.1 christos "connid=%lu failed initial TLS accept rc=%d\n",
429 1.1 christos c->c_connid, rc );
430 1.1 christos CONNECTION_LOCK(c);
431 1.1 christos goto fail;
432 1.1 christos }
433 1.1 christos
434 1.1 christos if ( rc ) {
435 1.1 christos c->c_read_timeout = lload_timeout_net;
436 1.1 christos read_cb = write_cb = client_tls_handshake_cb;
437 1.1 christos }
438 1.1 christos #else /* ! HAVE_TLS */
439 1.1 christos assert(0);
440 1.1 christos #endif /* ! HAVE_TLS */
441 1.1 christos }
442 1.1 christos
443 1.1 christos event = event_new( base, s, EV_READ|EV_PERSIST, read_cb, c );
444 1.1 christos if ( !event ) {
445 1.1 christos Debug( LDAP_DEBUG_ANY, "client_init: "
446 1.1 christos "Read event could not be allocated\n" );
447 1.1 christos CONNECTION_LOCK(c);
448 1.1 christos goto fail;
449 1.1 christos }
450 1.1 christos c->c_read_event = event;
451 1.1 christos
452 1.1 christos event = event_new( base, s, EV_WRITE, write_cb, c );
453 1.1 christos if ( !event ) {
454 1.1 christos Debug( LDAP_DEBUG_ANY, "client_init: "
455 1.1 christos "Write event could not be allocated\n" );
456 1.1 christos CONNECTION_LOCK(c);
457 1.1 christos goto fail;
458 1.1 christos }
459 1.1 christos c->c_write_event = event;
460 1.1 christos
461 1.1 christos c->c_destroy = client_destroy;
462 1.1 christos c->c_unlink = client_unlink;
463 1.1 christos c->c_pdu_cb = handle_one_request;
464 1.1 christos
465 1.1 christos CONNECTION_LOCK(c);
466 1.1 christos /* We only register the write event when we have data pending */
467 1.1 christos event_add( c->c_read_event, c->c_read_timeout );
468 1.1 christos
469 1.1 christos checked_lock( &clients_mutex );
470 1.1 christos LDAP_CIRCLEQ_INSERT_TAIL( &clients, c, c_next );
471 1.1 christos checked_unlock( &clients_mutex );
472 1.1 christos CONNECTION_UNLOCK(c);
473 1.1 christos
474 1.1 christos return c;
475 1.1 christos fail:
476 1.1 christos if ( c->c_write_event ) {
477 1.1 christos event_free( c->c_write_event );
478 1.1 christos c->c_write_event = NULL;
479 1.1 christos }
480 1.1 christos if ( c->c_read_event ) {
481 1.1 christos event_free( c->c_read_event );
482 1.1 christos c->c_read_event = NULL;
483 1.1 christos }
484 1.1 christos
485 1.1 christos c->c_state = LLOAD_C_INVALID;
486 1.1 christos c->c_live--;
487 1.1 christos c->c_refcnt--;
488 1.1 christos connection_destroy( c );
489 1.1 christos return NULL;
490 1.1 christos }
491 1.1 christos
492 1.1 christos void
493 1.1 christos client_reset( LloadConnection *c )
494 1.1 christos {
495 1.1 christos TAvlnode *root;
496 1.1 christos long freed = 0, executing;
497 1.1 christos
498 1.1 christos CONNECTION_ASSERT_LOCKED(c);
499 1.1 christos root = c->c_ops;
500 1.1 christos c->c_ops = NULL;
501 1.1 christos executing = c->c_n_ops_executing;
502 1.1 christos c->c_n_ops_executing = 0;
503 1.1 christos
504 1.1 christos if ( !BER_BVISNULL( &c->c_auth ) ) {
505 1.1 christos ch_free( c->c_auth.bv_val );
506 1.1 christos BER_BVZERO( &c->c_auth );
507 1.1 christos }
508 1.1 christos if ( !BER_BVISNULL( &c->c_sasl_bind_mech ) ) {
509 1.1 christos ch_free( c->c_sasl_bind_mech.bv_val );
510 1.1 christos BER_BVZERO( &c->c_sasl_bind_mech );
511 1.1 christos }
512 1.1 christos CONNECTION_UNLOCK(c);
513 1.1 christos
514 1.1 christos if ( root ) {
515 1.1 christos freed = ldap_tavl_free( root, (AVL_FREE)operation_abandon );
516 1.1 christos Debug( LDAP_DEBUG_TRACE, "client_reset: "
517 1.1 christos "dropped %ld operations\n",
518 1.1 christos freed );
519 1.1 christos }
520 1.1 christos assert( freed == executing );
521 1.1 christos
522 1.1 christos CONNECTION_LOCK(c);
523 1.1 christos CONNECTION_ASSERT_LOCKED(c);
524 1.1 christos }
525 1.1 christos
526 1.1 christos void
527 1.1 christos client_unlink( LloadConnection *c )
528 1.1 christos {
529 1.1 christos enum sc_state state;
530 1.1 christos struct event *read_event, *write_event;
531 1.1 christos
532 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_unlink: "
533 1.1 christos "removing client connid=%lu\n",
534 1.1 christos c->c_connid );
535 1.1 christos
536 1.1 christos CONNECTION_ASSERT_LOCKED(c);
537 1.1 christos assert( c->c_state != LLOAD_C_INVALID );
538 1.1 christos assert( c->c_state != LLOAD_C_DYING );
539 1.1 christos
540 1.1 christos state = c->c_state;
541 1.1 christos c->c_state = LLOAD_C_DYING;
542 1.1 christos
543 1.1 christos read_event = c->c_read_event;
544 1.1 christos write_event = c->c_write_event;
545 1.1 christos CONNECTION_UNLOCK(c);
546 1.1 christos
547 1.1 christos if ( read_event ) {
548 1.1 christos event_del( read_event );
549 1.1 christos }
550 1.1 christos
551 1.1 christos if ( write_event ) {
552 1.1 christos event_del( write_event );
553 1.1 christos }
554 1.1 christos
555 1.1 christos if ( state != LLOAD_C_DYING ) {
556 1.1 christos checked_lock( &clients_mutex );
557 1.1 christos LDAP_CIRCLEQ_REMOVE( &clients, c, c_next );
558 1.1 christos checked_unlock( &clients_mutex );
559 1.1 christos }
560 1.1 christos
561 1.1 christos CONNECTION_LOCK(c);
562 1.1 christos client_reset( c );
563 1.1 christos CONNECTION_ASSERT_LOCKED(c);
564 1.1 christos }
565 1.1 christos
566 1.1 christos void
567 1.1 christos client_destroy( LloadConnection *c )
568 1.1 christos {
569 1.1 christos Debug( LDAP_DEBUG_CONNS, "client_destroy: "
570 1.1 christos "destroying client connid=%lu\n",
571 1.1 christos c->c_connid );
572 1.1 christos
573 1.1 christos CONNECTION_LOCK(c);
574 1.1 christos assert( c->c_state == LLOAD_C_DYING );
575 1.1 christos c->c_state = LLOAD_C_INVALID;
576 1.1 christos
577 1.1 christos assert( c->c_ops == NULL );
578 1.1 christos
579 1.1 christos if ( c->c_read_event ) {
580 1.1 christos event_free( c->c_read_event );
581 1.1 christos c->c_read_event = NULL;
582 1.1 christos }
583 1.1 christos
584 1.1 christos if ( c->c_write_event ) {
585 1.1 christos event_free( c->c_write_event );
586 1.1 christos c->c_write_event = NULL;
587 1.1 christos }
588 1.1 christos
589 1.1 christos assert( c->c_refcnt == 0 );
590 1.1 christos connection_destroy( c );
591 1.1 christos }
592 1.1 christos
593 1.1 christos void
594 1.1 christos clients_destroy( int gentle )
595 1.1 christos {
596 1.1 christos checked_lock( &clients_mutex );
597 1.1 christos connections_walk(
598 1.1 christos &clients_mutex, &clients, lload_connection_close, &gentle );
599 1.1 christos checked_unlock( &clients_mutex );
600 1.1 christos }
601