xref: /freebsd/contrib/unbound/daemon/daemon.h (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1 /*
2  * daemon/daemon.h - collection of workers that handles requests.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * The daemon consists of global settings and a number of workers.
40  */
41 
42 #ifndef DAEMON_H
43 #define DAEMON_H
44 
45 #include "util/locks.h"
46 #include "util/alloc.h"
47 #include "services/modstack.h"
48 struct config_file;
49 struct worker;
50 struct listen_port;
51 struct slabhash;
52 struct module_env;
53 struct rrset_cache;
54 struct acl_list;
55 struct local_zones;
56 struct views;
57 struct ub_randstate;
58 struct daemon_remote;
59 struct respip_set;
60 struct shm_main_info;
61 struct doq_table;
62 struct cookie_secrets;
63 struct fast_reload_thread;
64 struct fast_reload_printq;
65 
66 #include "dnstap/dnstap_config.h"
67 #ifdef USE_DNSTAP
68 struct dt_env;
69 #endif
70 
71 #include "dnscrypt/dnscrypt_config.h"
72 #ifdef USE_DNSCRYPT
73 struct dnsc_env;
74 #endif
75 
76 /**
77  * Structure holding worker list.
78  * Holds globally visible information.
79  */
80 struct daemon {
81 	/** The config settings */
82 	struct config_file* cfg;
83 	/** the chroot dir in use, NULL if none */
84 	char* chroot;
85 	/** pidfile that is used */
86 	char* pidfile;
87 	/** port number that has ports opened. */
88 	int listening_port;
89 	/** array of listening ports, opened.  Listening ports per worker,
90 	 * or just one element[0] shared by the worker threads. */
91 	struct listen_port** ports;
92 	/** size of ports array */
93 	size_t num_ports;
94 	/** reuseport is enabled if true */
95 	int reuseport;
96 	/** port number for remote that has ports opened. */
97 	int rc_port;
98 	/** listening ports for remote control */
99 	struct listen_port* rc_ports;
100 	/** remote control connections management (for first worker) */
101 	struct daemon_remote* rc;
102 	/** ssl context for listening to dnstcp over ssl */
103 	void* listen_dot_sslctx;
104 	/** ssl context for connecting to dnstcp over ssl */
105 	void* connect_dot_sslctx;
106 	/** ssl context for listening to DoH */
107 	void* listen_doh_sslctx;
108 	/** ssl context for listening to quic */
109 	void* listen_quic_sslctx;
110 	/** num threads allocated */
111 	int num;
112 	/** num threads allocated in the previous config or 0 at first */
113 	int old_num;
114 	/** the worker entries */
115 	struct worker** workers;
116 	/** per-worker allocation cache */
117 	struct alloc_cache **worker_allocs;
118 	/** do we need to exit unbound (or is it only a reload?) */
119 	int need_to_exit;
120 	/** master random table ; used for port div between threads on reload*/
121 	struct ub_randstate* rand;
122 	/** master allocation cache */
123 	struct alloc_cache superalloc;
124 	/** the module environment master value, copied and changed by threads*/
125 	struct module_env* env;
126 	/** stack of module callbacks */
127 	struct module_stack mods;
128 	/** The module stack has been inited */
129 	int mods_inited;
130 	/** access control, which client IPs are allowed to connect */
131 	struct acl_list* acl;
132 	/** access control, which interfaces are allowed to connect */
133 	struct acl_list* acl_interface;
134 	/** TCP connection limit, limit connections from client IPs */
135 	struct tcl_list* tcl;
136 	/** local authority zones */
137 	struct local_zones* local_zones;
138 	/** last time of statistics printout */
139 	struct timeval time_last_stat;
140 	/** time when daemon started */
141 	struct timeval time_boot;
142 #ifdef USE_DNSTAP
143 	/** the dnstap environment master value, copied and changed by threads*/
144 	struct dt_env* dtenv;
145 #endif
146 	struct shm_main_info* shm_info;
147 	/** some response-ip tags or actions are configured if true */
148 	int use_response_ip;
149 	/** some RPZ policies are configured */
150 	int use_rpz;
151 #ifdef USE_DNSCRYPT
152 	/** the dnscrypt environment */
153 	struct dnsc_env* dnscenv;
154 #endif
155 	/** the doq connection table */
156 	struct doq_table* doq_table;
157 	/** reuse existing cache on reload if other conditions allow it. */
158 	int reuse_cache;
159 	/** the EDNS cookie secrets from the cookie-secret-file */
160 	struct cookie_secrets* cookie_secrets;
161 	/** the fast reload thread, or NULL */
162 	struct fast_reload_thread* fast_reload_thread;
163 	/** the fast reload printq list */
164 	struct fast_reload_printq* fast_reload_printq_list;
165 	/** the fast reload option to drop mesh queries, true if so. */
166 	int fast_reload_drop_mesh;
167 	/** for fast reload, if the tcl, tcp connection limits, has
168 	 * changes for workers */
169 	int fast_reload_tcl_has_changes;
170 	/** config file name */
171 	char* cfgfile;
172 };
173 
174 /**
175  * Initialize daemon structure.
176  * @return: The daemon structure, or NULL on error.
177  */
178 struct daemon* daemon_init(void);
179 
180 /**
181  * Open shared listening ports (if needed).
182  * The cfg member pointer must have been set for the daemon.
183  * @param daemon: the daemon.
184  * @return: false on error.
185  */
186 int daemon_open_shared_ports(struct daemon* daemon);
187 
188 /**
189  * Do daemon setup that needs privileges
190  * like opening privileged ports or opening device files.
191  * The cfg member pointer must have been set for the daemon.
192  * @param daemon: the daemon.
193  * @return: false on error.
194  */
195 int daemon_privileged(struct daemon* daemon);
196 
197 /**
198  * Fork workers and start service.
199  * When the routine exits, it is no longer forked.
200  * @param daemon: the daemon.
201  */
202 void daemon_fork(struct daemon* daemon);
203 
204 /**
205  * Close off the worker thread information.
206  * Bring the daemon back into state ready for daemon_fork again.
207  * @param daemon: the daemon.
208  */
209 void daemon_cleanup(struct daemon* daemon);
210 
211 /**
212  * Delete workers, close listening ports.
213  * @param daemon: the daemon.
214  */
215 void daemon_delete(struct daemon* daemon);
216 
217 /**
218  * Apply config settings.
219  * @param daemon: the daemon.
220  * @param cfg: new config settings.
221  */
222 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
223 
224 /**
225  * Setup acl list to have entries for the port list.
226  * @param list: the acl interface
227  * @param port_list: list of open ports, or none.
228  * @return false on failure
229  */
230 int setup_acl_for_ports(struct acl_list* list, struct listen_port* port_list);
231 
232 #endif /* DAEMON_H */
233