1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #ifndef NS_SERVER_H
13 #define NS_SERVER_H 1
14 
15 /*! \file */
16 
17 #include <inttypes.h>
18 #include <stdbool.h>
19 
20 #include <isc/fuzz.h>
21 #include <isc/log.h>
22 #include <isc/magic.h>
23 #include <isc/quota.h>
24 #include <isc/random.h>
25 #include <isc/sockaddr.h>
26 #include <isc/types.h>
27 
28 #include <dns/acl.h>
29 #include <dns/types.h>
30 
31 #include <ns/events.h>
32 #include <ns/types.h>
33 
34 #define NS_SERVER_LOGQUERIES   0x00000001U /*%< log queries */
35 #define NS_SERVER_NOAA	       0x00000002U /*%< -T noaa */
36 #define NS_SERVER_NOSOA	       0x00000004U /*%< -T nosoa */
37 #define NS_SERVER_NONEAREST    0x00000008U /*%< -T nonearest */
38 #define NS_SERVER_NOEDNS       0x00000020U /*%< -T noedns */
39 #define NS_SERVER_DROPEDNS     0x00000040U /*%< -T dropedns */
40 #define NS_SERVER_NOTCP	       0x00000080U /*%< -T notcp */
41 #define NS_SERVER_DISABLE4     0x00000100U /*%< -6 */
42 #define NS_SERVER_DISABLE6     0x00000200U /*%< -4 */
43 #define NS_SERVER_FIXEDLOCAL   0x00000400U /*%< -T fixedlocal */
44 #define NS_SERVER_SIGVALINSECS 0x00000800U /*%< -T sigvalinsecs */
45 #define NS_SERVER_EDNSFORMERR  0x00001000U /*%< -T ednsformerr (STD13) */
46 #define NS_SERVER_EDNSNOTIMP   0x00002000U /*%< -T ednsnotimp */
47 #define NS_SERVER_EDNSREFUSED  0x00004000U /*%< -T ednsrefused */
48 
49 /*%
50  * Type for callback function to get hostname.
51  */
52 typedef isc_result_t (*ns_hostnamecb_t)(char *buf, size_t len);
53 
54 /*%
55  * Type for callback function to signal the fuzzer thread
56  * when built with AFL.
57  */
58 typedef void (*ns_fuzzcb_t)(void);
59 
60 /*%
61  * Type for callback function to get the view that can answer a query.
62  */
63 typedef isc_result_t (*ns_matchview_t)(
64 	isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr, dns_message_t *message,
65 	dns_aclenv_t *env, isc_result_t *sigresultp, dns_view_t **viewp);
66 
67 /*%
68  * Server context.
69  */
70 struct ns_server {
71 	unsigned int magic;
72 	isc_mem_t *  mctx;
73 
74 	isc_refcount_t references;
75 
76 	/*% Server cookie secret and algorithm */
77 	unsigned char	   secret[32];
78 	ns_cookiealg_t	   cookiealg;
79 	ns_altsecretlist_t altsecrets;
80 	bool		   answercookie;
81 
82 	/*% Quotas */
83 	isc_quota_t recursionquota;
84 	isc_quota_t tcpquota;
85 	isc_quota_t xfroutquota;
86 	ISC_LIST(isc_quota_t) http_quotas;
87 
88 	/*% Test options and other configurables */
89 	uint32_t options;
90 
91 	dns_acl_t *    blackholeacl;
92 	dns_acl_t *    keepresporder;
93 	uint16_t       udpsize;
94 	uint16_t       transfer_tcp_message_size;
95 	bool	       interface_auto;
96 	dns_tkeyctx_t *tkeyctx;
97 
98 	/*% Server id for NSID */
99 	char *		server_id;
100 	ns_hostnamecb_t gethostname;
101 
102 	/*% Fuzzer callback */
103 	isc_fuzztype_t fuzztype;
104 	ns_fuzzcb_t    fuzznotify;
105 
106 	/*% Callback to find a matching view for a query */
107 	ns_matchview_t matchingview;
108 
109 	/*% Stats counters */
110 	ns_stats_t * nsstats;
111 	dns_stats_t *rcvquerystats;
112 	dns_stats_t *opcodestats;
113 	dns_stats_t *rcodestats;
114 
115 	isc_stats_t *udpinstats4;
116 	isc_stats_t *udpoutstats4;
117 	isc_stats_t *udpinstats6;
118 	isc_stats_t *udpoutstats6;
119 
120 	isc_stats_t *tcpinstats4;
121 	isc_stats_t *tcpoutstats4;
122 	isc_stats_t *tcpinstats6;
123 	isc_stats_t *tcpoutstats6;
124 };
125 
126 struct ns_altsecret {
127 	ISC_LINK(ns_altsecret_t) link;
128 	unsigned char secret[32];
129 };
130 
131 isc_result_t
132 ns_server_create(isc_mem_t *mctx, ns_matchview_t matchingview,
133 		 ns_server_t **sctxp);
134 /*%<
135  * Create a server context object with default settings.
136  */
137 
138 void
139 ns_server_attach(ns_server_t *src, ns_server_t **dest);
140 /*%<
141  * Attach a server context.
142  *
143  * Requires:
144  *\li	'src' is valid.
145  */
146 
147 void
148 ns_server_detach(ns_server_t **sctxp);
149 /*%<
150  * Detach from a server context.  If its reference count drops to zero, destroy
151  * it, freeing its memory.
152  *
153  * Requires:
154  *\li	'*sctxp' is valid.
155  * Ensures:
156  *\li	'*sctxp' is NULL on return.
157  */
158 
159 isc_result_t
160 ns_server_setserverid(ns_server_t *sctx, const char *serverid);
161 /*%<
162  * Set sctx->server_id to 'serverid'. If it was set previously, free the memory.
163  *
164  * Requires:
165  *\li	'sctx' is valid.
166  */
167 
168 void
169 ns_server_setoption(ns_server_t *sctx, unsigned int option, bool value);
170 /*%<
171  *	Set the given options on (if 'value' == #true)
172  *	or off (if 'value' == #false).
173  *
174  * Requires:
175  *\li	'sctx' is valid
176  */
177 
178 bool
179 ns_server_getoption(ns_server_t *sctx, unsigned int option);
180 /*%<
181  *	Returns the current value of the specified server option.
182  *
183  * Requires:
184  *\li	'sctx' is valid.
185  */
186 #endif /* NS_SERVER_H */
187