| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
/*! \file */ |
| 15 |
|
| 16 |
#include <isc/atomic.h> |
| 17 |
#include <isc/netmgr.h> |
| 18 |
#include <isc/util.h> |
| 19 |
|
| 20 |
#include <dns/view.h> |
| 21 |
|
| 22 |
#include <ns/client.h> |
| 23 |
|
| 24 |
#if ISC_NETMGR_TRACE |
| 25 |
#define FLARG \ |
| 26 |
, const char *func ISC_ATTR_UNUSED, const char *file ISC_ATTR_UNUSED, \ |
| 27 |
unsigned int line ISC_ATTR_UNUSED |
| 28 |
#else |
| 29 |
#define FLARG |
| 30 |
#endif |
| 31 |
|
| 32 |
/* |
| 33 |
* We don't want to use netmgr-based client accounting, we need to emulate it. |
| 34 |
*/ |
| 35 |
atomic_uint_fast32_t client_refs[32]; |
| 36 |
atomic_uintptr_t client_addrs[32]; |
| 37 |
|
| 38 |
#if ISC_NETMGR_TRACE |
| 39 |
void |
| 40 |
isc_nmhandle__attach(isc_nmhandle_t *source, isc_nmhandle_t **targetp FLARG) { |
| 41 |
#else |
| 42 |
void |
| 43 |
isc_nmhandle_attach(isc_nmhandle_t *source, isc_nmhandle_t **targetp) { |
| 44 |
#endif |
| 45 |
ns_client_t *client = (ns_client_t *)source; |
| 46 |
int i; |
| 47 |
|
| 48 |
for (i = 0; i < 32; i++) { |
| 49 |
if (atomic_load(&client_addrs[i]) == (uintptr_t)client) { |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
INSIST(i < 32); |
| 54 |
INSIST(atomic_load(&client_refs[i]) > 0); |
| 55 |
|
| 56 |
atomic_fetch_add(&client_refs[i], 1); |
| 57 |
#if 0 |
| 58 |
fprintf(stderr, "%s:%s:%s:%d -> %ld\n", __func__, func, file, line, |
| 59 |
client_refs[i]); |
| 60 |
#endif |
| 61 |
|
| 62 |
*targetp = source; |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
#if ISC_NETMGR_TRACE |
| 67 |
void |
| 68 |
isc_nmhandle__detach(isc_nmhandle_t **handlep FLARG) { |
| 69 |
#else |
| 70 |
void |
| 71 |
isc_nmhandle_detach(isc_nmhandle_t **handlep) { |
| 72 |
#endif |
| 73 |
isc_nmhandle_t *handle = *handlep; |
| 74 |
ns_client_t *client = (ns_client_t *)handle; |
| 75 |
int i; |
| 76 |
|
| 77 |
*handlep = NULL; |
| 78 |
|
| 79 |
for (i = 0; i < 32; i++) { |
| 80 |
if (atomic_load(&client_addrs[i]) == (uintptr_t)client) { |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
INSIST(i < 32); |
| 85 |
|
| 86 |
if (atomic_fetch_sub(&client_refs[i], 1) == 1) { |
| 87 |
dns_view_detach(&client->view); |
| 88 |
client->state = 4; |
| 89 |
ns__client_reset_cb(client); |
| 90 |
ns__client_put_cb(client); |
| 91 |
atomic_store(&client_addrs[i], (uintptr_t)NULL); |
| 92 |
} |
| 93 |
#if 0 |
| 94 |
fprintf(stderr, "%s:%s:%s:%d -> %ld\n", __func__, func, file, line, |
| 95 |
client_refs[i]); |
| 96 |
#endif |
| 97 |
|
| 98 |
return; |
| 99 |
} |