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