1238106Sdes/*
2238106Sdes * daemon/daemon.h - collection of workers that handles requests.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * The daemon consists of global settings and a number of workers.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef DAEMON_H
43238106Sdes#define DAEMON_H
44238106Sdes
45238106Sdes#include "util/locks.h"
46238106Sdes#include "util/alloc.h"
47238106Sdes#include "services/modstack.h"
48238106Sdes#ifdef UB_ON_WINDOWS
49238106Sdes#  include "util/winsock_event.h"
50238106Sdes#endif
51238106Sdesstruct config_file;
52238106Sdesstruct worker;
53238106Sdesstruct listen_port;
54238106Sdesstruct slabhash;
55238106Sdesstruct module_env;
56238106Sdesstruct rrset_cache;
57238106Sdesstruct acl_list;
58238106Sdesstruct local_zones;
59238106Sdesstruct ub_randstate;
60238106Sdesstruct daemon_remote;
61238106Sdes
62238106Sdes/**
63238106Sdes * Structure holding worker list.
64238106Sdes * Holds globally visible information.
65238106Sdes */
66238106Sdesstruct daemon {
67238106Sdes	/** The config settings */
68238106Sdes	struct config_file* cfg;
69238106Sdes	/** the chroot dir in use, NULL if none */
70238106Sdes	char* chroot;
71238106Sdes	/** pidfile that is used */
72238106Sdes	char* pidfile;
73238106Sdes	/** port number that has ports opened. */
74238106Sdes	int listening_port;
75269257Sdes	/** array of listening ports, opened.  Listening ports per worker,
76269257Sdes	 * or just one element[0] shared by the worker threads. */
77269257Sdes	struct listen_port** ports;
78269257Sdes	/** size of ports array */
79269257Sdes	size_t num_ports;
80238106Sdes	/** port number for remote that has ports opened. */
81238106Sdes	int rc_port;
82238106Sdes	/** listening ports for remote control */
83238106Sdes	struct listen_port* rc_ports;
84238106Sdes	/** remote control connections management (for first worker) */
85238106Sdes	struct daemon_remote* rc;
86238106Sdes	/** ssl context for listening to dnstcp over ssl, and connecting ssl */
87238106Sdes	void* listen_sslctx, *connect_sslctx;
88238106Sdes	/** num threads allocated */
89238106Sdes	int num;
90238106Sdes	/** the worker entries */
91238106Sdes	struct worker** workers;
92238106Sdes	/** do we need to exit unbound (or is it only a reload?) */
93238106Sdes	int need_to_exit;
94238106Sdes	/** master random table ; used for port div between threads on reload*/
95238106Sdes	struct ub_randstate* rand;
96238106Sdes	/** master allocation cache */
97238106Sdes	struct alloc_cache superalloc;
98238106Sdes	/** the module environment master value, copied and changed by threads*/
99238106Sdes	struct module_env* env;
100238106Sdes	/** stack of module callbacks */
101238106Sdes	struct module_stack mods;
102238106Sdes	/** access control, which client IPs are allowed to connect */
103238106Sdes	struct acl_list* acl;
104238106Sdes	/** local authority zones */
105238106Sdes	struct local_zones* local_zones;
106238106Sdes	/** last time of statistics printout */
107238106Sdes	struct timeval time_last_stat;
108238106Sdes	/** time when daemon started */
109238106Sdes	struct timeval time_boot;
110238106Sdes};
111238106Sdes
112238106Sdes/**
113238106Sdes * Initialize daemon structure.
114238106Sdes * @return: The daemon structure, or NULL on error.
115238106Sdes */
116238106Sdesstruct daemon* daemon_init(void);
117238106Sdes
118238106Sdes/**
119238106Sdes * Open shared listening ports (if needed).
120238106Sdes * The cfg member pointer must have been set for the daemon.
121238106Sdes * @param daemon: the daemon.
122238106Sdes * @return: false on error.
123238106Sdes */
124238106Sdesint daemon_open_shared_ports(struct daemon* daemon);
125238106Sdes
126238106Sdes/**
127238106Sdes * Fork workers and start service.
128238106Sdes * When the routine exits, it is no longer forked.
129238106Sdes * @param daemon: the daemon.
130238106Sdes */
131238106Sdesvoid daemon_fork(struct daemon* daemon);
132238106Sdes
133238106Sdes/**
134238106Sdes * Close off the worker thread information.
135238106Sdes * Bring the daemon back into state ready for daemon_fork again.
136238106Sdes * @param daemon: the daemon.
137238106Sdes */
138238106Sdesvoid daemon_cleanup(struct daemon* daemon);
139238106Sdes
140238106Sdes/**
141238106Sdes * Delete workers, close listening ports.
142238106Sdes * @param daemon: the daemon.
143238106Sdes */
144238106Sdesvoid daemon_delete(struct daemon* daemon);
145238106Sdes
146238106Sdes/**
147238106Sdes * Apply config settings.
148238106Sdes * @param daemon: the daemon.
149238106Sdes * @param cfg: new config settings.
150238106Sdes */
151238106Sdesvoid daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
152238106Sdes
153238106Sdes#endif /* DAEMON_H */
154