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