xref: /openbsd-src/usr.sbin/unbound/daemon/daemon.h (revision 98bc733b08604094f4138174a0ee0bb9faaca4bd)
1933707f3Ssthen /*
2933707f3Ssthen  * daemon/daemon.h - collection of workers that handles requests.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * The daemon consists of global settings and a number of workers.
40933707f3Ssthen  */
41933707f3Ssthen 
42933707f3Ssthen #ifndef DAEMON_H
43933707f3Ssthen #define DAEMON_H
44933707f3Ssthen 
45933707f3Ssthen #include "util/locks.h"
46933707f3Ssthen #include "util/alloc.h"
47933707f3Ssthen #include "services/modstack.h"
48933707f3Ssthen struct config_file;
49933707f3Ssthen struct worker;
50933707f3Ssthen struct listen_port;
51933707f3Ssthen struct slabhash;
52933707f3Ssthen struct module_env;
53933707f3Ssthen struct rrset_cache;
54933707f3Ssthen struct acl_list;
55933707f3Ssthen struct local_zones;
5677079be7Ssthen struct views;
57933707f3Ssthen struct ub_randstate;
58933707f3Ssthen struct daemon_remote;
592be9e038Ssthen struct respip_set;
602be9e038Ssthen struct shm_main_info;
61*98bc733bSsthen struct cookie_secrets;
62933707f3Ssthen 
6398f3ca02Sbrad #include "dnstap/dnstap_config.h"
6498f3ca02Sbrad #ifdef USE_DNSTAP
6598f3ca02Sbrad struct dt_env;
6698f3ca02Sbrad #endif
6798f3ca02Sbrad 
682be9e038Ssthen #include "dnscrypt/dnscrypt_config.h"
692be9e038Ssthen #ifdef USE_DNSCRYPT
702be9e038Ssthen struct dnsc_env;
712be9e038Ssthen #endif
722be9e038Ssthen 
73933707f3Ssthen /**
74933707f3Ssthen  * Structure holding worker list.
75933707f3Ssthen  * Holds globally visible information.
76933707f3Ssthen  */
77933707f3Ssthen struct daemon {
78933707f3Ssthen 	/** The config settings */
79933707f3Ssthen 	struct config_file* cfg;
80933707f3Ssthen 	/** the chroot dir in use, NULL if none */
81933707f3Ssthen 	char* chroot;
82933707f3Ssthen 	/** pidfile that is used */
83933707f3Ssthen 	char* pidfile;
84933707f3Ssthen 	/** port number that has ports opened. */
85933707f3Ssthen 	int listening_port;
865d76a658Ssthen 	/** array of listening ports, opened.  Listening ports per worker,
875d76a658Ssthen 	 * or just one element[0] shared by the worker threads. */
885d76a658Ssthen 	struct listen_port** ports;
895d76a658Ssthen 	/** size of ports array */
905d76a658Ssthen 	size_t num_ports;
9198f3ca02Sbrad 	/** reuseport is enabled if true */
9298f3ca02Sbrad 	int reuseport;
93933707f3Ssthen 	/** port number for remote that has ports opened. */
94933707f3Ssthen 	int rc_port;
95933707f3Ssthen 	/** listening ports for remote control */
96933707f3Ssthen 	struct listen_port* rc_ports;
97933707f3Ssthen 	/** remote control connections management (for first worker) */
98933707f3Ssthen 	struct daemon_remote* rc;
99933707f3Ssthen 	/** ssl context for listening to dnstcp over ssl, and connecting ssl */
100933707f3Ssthen 	void* listen_sslctx, *connect_sslctx;
101933707f3Ssthen 	/** num threads allocated */
102933707f3Ssthen 	int num;
1038b7325afSsthen 	/** num threads allocated in the previous config or 0 at first */
1048b7325afSsthen 	int old_num;
105933707f3Ssthen 	/** the worker entries */
106933707f3Ssthen 	struct worker** workers;
1078b7325afSsthen 	/** per-worker allocation cache */
1088b7325afSsthen 	struct alloc_cache **worker_allocs;
109933707f3Ssthen 	/** do we need to exit unbound (or is it only a reload?) */
110933707f3Ssthen 	int need_to_exit;
111933707f3Ssthen 	/** master random table ; used for port div between threads on reload*/
112933707f3Ssthen 	struct ub_randstate* rand;
113933707f3Ssthen 	/** master allocation cache */
114933707f3Ssthen 	struct alloc_cache superalloc;
115933707f3Ssthen 	/** the module environment master value, copied and changed by threads*/
116933707f3Ssthen 	struct module_env* env;
117933707f3Ssthen 	/** stack of module callbacks */
118933707f3Ssthen 	struct module_stack mods;
119*98bc733bSsthen 	/** The module stack has been inited */
120*98bc733bSsthen 	int mods_inited;
121933707f3Ssthen 	/** access control, which client IPs are allowed to connect */
122933707f3Ssthen 	struct acl_list* acl;
12345872187Ssthen 	/** access control, which interfaces are allowed to connect */
12445872187Ssthen 	struct acl_list* acl_interface;
1252308e98cSsthen 	/** TCP connection limit, limit connections from client IPs */
1262308e98cSsthen 	struct tcl_list* tcl;
127933707f3Ssthen 	/** local authority zones */
128933707f3Ssthen 	struct local_zones* local_zones;
129933707f3Ssthen 	/** last time of statistics printout */
130933707f3Ssthen 	struct timeval time_last_stat;
131933707f3Ssthen 	/** time when daemon started */
132933707f3Ssthen 	struct timeval time_boot;
13377079be7Ssthen 	/** views structure containing view tree */
13477079be7Ssthen 	struct views* views;
13598f3ca02Sbrad #ifdef USE_DNSTAP
13698f3ca02Sbrad 	/** the dnstap environment master value, copied and changed by threads*/
13798f3ca02Sbrad 	struct dt_env* dtenv;
13898f3ca02Sbrad #endif
1392be9e038Ssthen 	struct shm_main_info* shm_info;
1402be9e038Ssthen 	/** response-ip set with associated actions and tags. */
1412be9e038Ssthen 	struct respip_set* respip_set;
1422be9e038Ssthen 	/** some response-ip tags or actions are configured if true */
1432be9e038Ssthen 	int use_response_ip;
144eaf2578eSsthen 	/** some RPZ policies are configured */
145eaf2578eSsthen 	int use_rpz;
1462be9e038Ssthen #ifdef USE_DNSCRYPT
1472be9e038Ssthen 	/** the dnscrypt environment */
1482be9e038Ssthen 	struct dnsc_env* dnscenv;
1492be9e038Ssthen #endif
1508b7325afSsthen 	/** reuse existing cache on reload if other conditions allow it. */
1518b7325afSsthen 	int reuse_cache;
152*98bc733bSsthen 	/** the EDNS cookie secrets from the cookie-secret-file */
153*98bc733bSsthen 	struct cookie_secrets* cookie_secrets;
154933707f3Ssthen };
155933707f3Ssthen 
156933707f3Ssthen /**
157933707f3Ssthen  * Initialize daemon structure.
158933707f3Ssthen  * @return: The daemon structure, or NULL on error.
159933707f3Ssthen  */
160933707f3Ssthen struct daemon* daemon_init(void);
161933707f3Ssthen 
162933707f3Ssthen /**
163933707f3Ssthen  * Open shared listening ports (if needed).
164933707f3Ssthen  * The cfg member pointer must have been set for the daemon.
165933707f3Ssthen  * @param daemon: the daemon.
166933707f3Ssthen  * @return: false on error.
167933707f3Ssthen  */
168933707f3Ssthen int daemon_open_shared_ports(struct daemon* daemon);
169933707f3Ssthen 
170933707f3Ssthen /**
171*98bc733bSsthen  * Do daemon setup that needs privileges
172*98bc733bSsthen  * like opening privileged ports or opening device files.
173*98bc733bSsthen  * The cfg member pointer must have been set for the daemon.
174*98bc733bSsthen  * @param daemon: the daemon.
175*98bc733bSsthen  * @return: false on error.
176*98bc733bSsthen  */
177*98bc733bSsthen int daemon_privileged(struct daemon* daemon);
178*98bc733bSsthen 
179*98bc733bSsthen /**
180933707f3Ssthen  * Fork workers and start service.
181933707f3Ssthen  * When the routine exits, it is no longer forked.
182933707f3Ssthen  * @param daemon: the daemon.
183933707f3Ssthen  */
184933707f3Ssthen void daemon_fork(struct daemon* daemon);
185933707f3Ssthen 
186933707f3Ssthen /**
187933707f3Ssthen  * Close off the worker thread information.
188933707f3Ssthen  * Bring the daemon back into state ready for daemon_fork again.
189933707f3Ssthen  * @param daemon: the daemon.
190933707f3Ssthen  */
191933707f3Ssthen void daemon_cleanup(struct daemon* daemon);
192933707f3Ssthen 
193933707f3Ssthen /**
194933707f3Ssthen  * Delete workers, close listening ports.
195933707f3Ssthen  * @param daemon: the daemon.
196933707f3Ssthen  */
197933707f3Ssthen void daemon_delete(struct daemon* daemon);
198933707f3Ssthen 
199933707f3Ssthen /**
200933707f3Ssthen  * Apply config settings.
201933707f3Ssthen  * @param daemon: the daemon.
202933707f3Ssthen  * @param cfg: new config settings.
203933707f3Ssthen  */
204933707f3Ssthen void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
205933707f3Ssthen 
206933707f3Ssthen #endif /* DAEMON_H */
207