1 /* $NetBSD: forward.h,v 1.8 2025/01/26 16:25:27 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 #pragma once 17 18 /*! \file dns/forward.h */ 19 20 #include <isc/lang.h> 21 #include <isc/mem.h> 22 #include <isc/refcount.h> 23 #include <isc/result.h> 24 #include <isc/sockaddr.h> 25 26 #include <dns/fixedname.h> 27 #include <dns/qp.h> 28 #include <dns/types.h> 29 30 /* Add -DDNS_FORWARD_TRACE=1 to CFLAGS for detailed reference tracing */ 31 32 ISC_LANG_BEGINDECLS 33 34 struct dns_forwarder { 35 isc_sockaddr_t addr; 36 dns_name_t *tlsname; 37 ISC_LINK(dns_forwarder_t) link; 38 }; 39 40 typedef ISC_LIST(struct dns_forwarder) dns_forwarderlist_t; 41 42 struct dns_forwarders { 43 dns_forwarderlist_t fwdrs; 44 dns_fwdpolicy_t fwdpolicy; 45 isc_mem_t *mctx; 46 isc_refcount_t references; 47 dns_name_t name; 48 }; 49 50 void 51 dns_fwdtable_create(isc_mem_t *mctx, dns_view_t *view, 52 dns_fwdtable_t **fwdtablep); 53 /*%< 54 * Creates a new forwarding table. 55 * 56 * Requires: 57 * \li mctx is a valid memory context. 58 * \li fwdtablep != NULL && *fwdtablep == NULL 59 */ 60 61 isc_result_t 62 dns_fwdtable_addfwd(dns_fwdtable_t *fwdtable, const dns_name_t *name, 63 dns_forwarderlist_t *fwdrs, dns_fwdpolicy_t policy); 64 isc_result_t 65 dns_fwdtable_add(dns_fwdtable_t *fwdtable, const dns_name_t *name, 66 isc_sockaddrlist_t *addrs, dns_fwdpolicy_t policy); 67 /*%< 68 * Adds an entry to the forwarding table. The entry associates 69 * a domain with a list of forwarders and a forwarding policy. The 70 * addrs/fwdrs list is copied if not empty, so the caller should free 71 * its copy. 72 * 73 * Requires: 74 * \li fwdtable is a valid forwarding table. 75 * \li name is a valid name 76 * \li addrs/fwdrs is a valid list of isc_sockaddr/dns_forwarder 77 * structures, which may be empty. 78 * 79 * Returns: 80 * \li #ISC_R_SUCCESS 81 * \li #ISC_R_NOMEMORY 82 */ 83 84 isc_result_t 85 dns_fwdtable_find(dns_fwdtable_t *fwdtable, const dns_name_t *name, 86 dns_forwarders_t **forwardersp); 87 /*%< 88 * Finds a domain in the forwarding table. The closest matching parent 89 * domain is returned. 90 * 91 * Requires: 92 * \li fwdtable is a valid forwarding table. 93 * \li name is a valid name 94 * \li forwardersp != NULL && *forwardersp == NULL 95 * 96 * Returns: 97 * \li #ISC_R_SUCCESS Success 98 * \li #DNS_R_PARTIALMATCH Superdomain found with data 99 * \li #ISC_R_NOTFOUND No match 100 */ 101 102 void 103 dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep); 104 /*%< 105 * Destroys a forwarding table. 106 * 107 * Requires: 108 * \li fwtablep != NULL && *fwtablep != NULL 109 * 110 * Ensures: 111 * \li all memory associated with the forwarding table is freed. 112 */ 113 114 #if DNS_FORWARD_TRACE 115 #define dns_forwarders_ref(ptr) \ 116 dns_forwarders__ref(ptr, __func__, __FILE__, __LINE__) 117 #define dns_forwarders_unref(ptr) \ 118 dns_forwarders__unref(ptr, __func__, __FILE__, __LINE__) 119 #define dns_forwarders_attach(ptr, ptrp) \ 120 dns_forwarders__attach(ptr, ptrp, __func__, __FILE__, __LINE__) 121 #define dns_forwarders_detach(ptrp) \ 122 dns_forwarders__detach(ptrp, __func__, __FILE__, __LINE__) 123 ISC_REFCOUNT_TRACE_DECL(dns_forwarders); 124 #else 125 ISC_REFCOUNT_DECL(dns_forwarders); 126 #endif 127 ISC_LANG_ENDDECLS 128