1 /* $NetBSD: server.h,v 1.12 2026/01/29 18:37:56 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 */ 19 20 #include <inttypes.h> 21 #include <stdbool.h> 22 23 #include <isc/fuzz.h> 24 #include <isc/histo.h> 25 #include <isc/log.h> 26 #include <isc/magic.h> 27 #include <isc/quota.h> 28 #include <isc/random.h> 29 #include <isc/sockaddr.h> 30 #include <isc/types.h> 31 32 #include <dns/acl.h> 33 #include <dns/types.h> 34 35 #include <ns/types.h> 36 37 #define NS_SERVER_LOGQUERIES 0x00000001U /*%< log queries */ 38 #define NS_SERVER_NOAA 0x00000002U /*%< -T noaa */ 39 #define NS_SERVER_NOSOA 0x00000004U /*%< -T nosoa */ 40 #define NS_SERVER_NONEAREST 0x00000008U /*%< -T nonearest */ 41 #define NS_SERVER_NOEDNS 0x00000020U /*%< -T noedns */ 42 #define NS_SERVER_DROPEDNS 0x00000040U /*%< -T dropedns */ 43 #define NS_SERVER_NOTCP 0x00000080U /*%< -T notcp */ 44 #define NS_SERVER_DISABLE4 0x00000100U /*%< -6 */ 45 #define NS_SERVER_DISABLE6 0x00000200U /*%< -4 */ 46 #define NS_SERVER_FIXEDLOCAL 0x00000400U /*%< -T fixedlocal */ 47 #define NS_SERVER_SIGVALINSECS 0x00000800U /*%< -T sigvalinsecs */ 48 #define NS_SERVER_EDNSFORMERR 0x00001000U /*%< -T ednsformerr (STD13) */ 49 #define NS_SERVER_EDNSNOTIMP 0x00002000U /*%< -T ednsnotimp */ 50 #define NS_SERVER_EDNSREFUSED 0x00004000U /*%< -T ednsrefused */ 51 #define NS_SERVER_TRANSFERINSECS 0x00008000U /*%< -T transferinsecs */ 52 #define NS_SERVER_TRANSFERSLOWLY 0x00010000U /*%< -T transferslowly */ 53 #define NS_SERVER_TRANSFERSTUCK 0x00020000U /*%< -T transferstuck */ 54 #define NS_SERVER_LOGRESPONSES 0x00040000U /*%< log responses */ 55 #define NS_SERVER_COOKIEALWAYSVALID 0x00080000U /*%< -T cookiealwaysvalid */ 56 #define NS_SERVER_RPZSLOW 0x00100000U /*%< -T rpzslow */ 57 58 /*% 59 * Type for callback function to get hostname. 60 */ 61 typedef isc_result_t (*ns_hostnamecb_t)(char *buf, size_t len); 62 63 /*% 64 * Type for callback function to signal the fuzzer thread 65 * when built with AFL. 66 */ 67 typedef void (*ns_fuzzcb_t)(void); 68 69 /*% 70 * Type for callback function to get the view that can answer a query. 71 */ 72 typedef isc_result_t (*ns_matchview_t)( 73 isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr, dns_message_t *message, 74 dns_aclenv_t *env, ns_server_t *sctx, isc_loop_t *loop, isc_job_cb cb, 75 void *cbarg, isc_result_t *sigresultp, isc_result_t *viewmatchresult, 76 dns_view_t **viewp); 77 78 /*% 79 * Server context. 80 */ 81 struct ns_server { 82 unsigned int magic; 83 isc_mem_t *mctx; 84 85 isc_refcount_t references; 86 87 /*% Server cookie secret and algorithm */ 88 unsigned char secret[32]; 89 ns_cookiealg_t cookiealg; 90 ns_altsecretlist_t altsecrets; 91 bool answercookie; 92 93 /*% Quotas */ 94 isc_quota_t recursionquota; 95 isc_quota_t tcpquota; 96 isc_quota_t xfroutquota; 97 isc_quota_t updquota; 98 isc_quota_t sig0checksquota; 99 dns_acl_t *sig0checksquota_exempt; 100 ISC_LIST(isc_quota_t) http_quotas; 101 isc_mutex_t http_quotas_lock; 102 103 /*% Test options and other configurables */ 104 uint32_t options; 105 106 dns_acl_t *blackholeacl; 107 uint16_t udpsize; 108 uint16_t transfer_tcp_message_size; 109 bool interface_auto; 110 dns_tkeyctx_t *tkeyctx; 111 uint8_t max_restarts; 112 113 /*% Server id for NSID */ 114 char *server_id; 115 bool usehostname; 116 117 /*% Fuzzer callback */ 118 isc_fuzztype_t fuzztype; 119 ns_fuzzcb_t fuzznotify; 120 121 /*% Callback to find a matching view for a query */ 122 ns_matchview_t matchingview; 123 124 /*% Stats counters */ 125 ns_stats_t *nsstats; 126 dns_stats_t *rcvquerystats; 127 dns_stats_t *opcodestats; 128 dns_stats_t *rcodestats; 129 130 isc_histomulti_t *udpinstats4; 131 isc_histomulti_t *udpoutstats4; 132 isc_histomulti_t *udpinstats6; 133 isc_histomulti_t *udpoutstats6; 134 135 isc_histomulti_t *tcpinstats4; 136 isc_histomulti_t *tcpoutstats4; 137 isc_histomulti_t *tcpinstats6; 138 isc_histomulti_t *tcpoutstats6; 139 }; 140 141 struct ns_altsecret { 142 ISC_LINK(ns_altsecret_t) link; 143 unsigned char secret[32]; 144 }; 145 146 void 147 ns_server_create(isc_mem_t *mctx, ns_matchview_t matchingview, 148 ns_server_t **sctxp); 149 /*%< 150 * Create a server context object with default settings. 151 */ 152 153 void 154 ns_server_attach(ns_server_t *src, ns_server_t **dest); 155 /*%< 156 * Attach a server context. 157 * 158 * Requires: 159 *\li 'src' is valid. 160 */ 161 162 void 163 ns_server_detach(ns_server_t **sctxp); 164 /*%< 165 * Detach from a server context. If its reference count drops to zero, destroy 166 * it, freeing its memory. 167 * 168 * Requires: 169 *\li '*sctxp' is valid. 170 * Ensures: 171 *\li '*sctxp' is NULL on return. 172 */ 173 174 isc_result_t 175 ns_server_setserverid(ns_server_t *sctx, const char *serverid); 176 /*%< 177 * Set sctx->server_id to 'serverid'. If it was set previously, free the memory. 178 * 179 * Requires: 180 *\li 'sctx' is valid. 181 */ 182 183 void 184 ns_server_setoption(ns_server_t *sctx, unsigned int option, bool value); 185 /*%< 186 * Set the given options on (if 'value' == #true) 187 * or off (if 'value' == #false). 188 * 189 * Requires: 190 *\li 'sctx' is valid 191 */ 192 193 bool 194 ns_server_getoption(ns_server_t *sctx, unsigned int option); 195 /*%< 196 * Returns the current value of the specified server option. 197 * 198 * Requires: 199 *\li 'sctx' is valid. 200 */ 201 202 void 203 ns_server_append_http_quota(ns_server_t *sctx, isc_quota_t *http_quota); 204 /*%< 205 * Add a quota to the list of HTTP quotas to destroy it safely later. 206 * 207 * Requires: 208 *\li 'sctx' is valid; 209 *\li 'http_quota' is not 'NULL'. 210 */ 211