xref: /freebsd/contrib/unbound/libunbound/unbound.h (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * unbound.h - unbound validating resolver public API
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains functions to resolve DNS queries and
408a384985SDag-Erling Smørgrav  * validate the answers. Synchronously and asynchronously.
41b7579f77SDag-Erling Smørgrav  *
42b7579f77SDag-Erling Smørgrav  * Several ways to use this interface from an application wishing
43b7579f77SDag-Erling Smørgrav  * to perform (validated) DNS lookups.
44b7579f77SDag-Erling Smørgrav  *
45b7579f77SDag-Erling Smørgrav  * All start with
46b7579f77SDag-Erling Smørgrav  *	ctx = ub_ctx_create();
47b7579f77SDag-Erling Smørgrav  *	err = ub_ctx_add_ta(ctx, "...");
48b7579f77SDag-Erling Smørgrav  *	err = ub_ctx_add_ta(ctx, "...");
49b7579f77SDag-Erling Smørgrav  *	... some lookups
50b7579f77SDag-Erling Smørgrav  *	... call ub_ctx_delete(ctx); when you want to stop.
51b7579f77SDag-Erling Smørgrav  *
52b7579f77SDag-Erling Smørgrav  * Application not threaded. Blocking.
53b7579f77SDag-Erling Smørgrav  *	int err = ub_resolve(ctx, "www.example.com", ...
54b7579f77SDag-Erling Smørgrav  *	if(err) fprintf(stderr, "lookup error: %s\n", ub_strerror(err));
55b7579f77SDag-Erling Smørgrav  *	... use the answer
56b7579f77SDag-Erling Smørgrav  *
57b7579f77SDag-Erling Smørgrav  * Application not threaded. Non-blocking ('asynchronous').
58b7579f77SDag-Erling Smørgrav  *      err = ub_resolve_async(ctx, "www.example.com", ... my_callback);
59b7579f77SDag-Erling Smørgrav  *	... application resumes processing ...
60b7579f77SDag-Erling Smørgrav  *	... and when either ub_poll(ctx) is true
61b7579f77SDag-Erling Smørgrav  *	... or when the file descriptor ub_fd(ctx) is readable,
62b7579f77SDag-Erling Smørgrav  *	... or whenever, the app calls ...
63b7579f77SDag-Erling Smørgrav  *	ub_process(ctx);
64b7579f77SDag-Erling Smørgrav  *	... if no result is ready, the app resumes processing above,
65b7579f77SDag-Erling Smørgrav  *	... or process() calls my_callback() with results.
66b7579f77SDag-Erling Smørgrav  *
67b7579f77SDag-Erling Smørgrav  *      ... if the application has nothing more to do, wait for answer
68b7579f77SDag-Erling Smørgrav  *      ub_wait(ctx);
69b7579f77SDag-Erling Smørgrav  *
70b7579f77SDag-Erling Smørgrav  * Application threaded. Blocking.
71b7579f77SDag-Erling Smørgrav  *	Blocking, same as above. The current thread does the work.
72b7579f77SDag-Erling Smørgrav  *	Multiple threads can use the *same context*, each does work and uses
73b7579f77SDag-Erling Smørgrav  *	shared cache data from the context.
74b7579f77SDag-Erling Smørgrav  *
75b7579f77SDag-Erling Smørgrav  * Application threaded. Non-blocking ('asynchronous').
76b7579f77SDag-Erling Smørgrav  *	... setup threaded-asynchronous config option
77b7579f77SDag-Erling Smørgrav  *	err = ub_ctx_async(ctx, 1);
78b7579f77SDag-Erling Smørgrav  *	... same as async for non-threaded
79b7579f77SDag-Erling Smørgrav  *	... the callbacks are called in the thread that calls process(ctx)
80b7579f77SDag-Erling Smørgrav  *
8117d15b25SDag-Erling Smørgrav  * Openssl needs to have locking in place, and the application must set
8217d15b25SDag-Erling Smørgrav  * it up, because a mere library cannot do this, use the calls
8317d15b25SDag-Erling Smørgrav  * CRYPTO_set_id_callback and CRYPTO_set_locking_callback.
8417d15b25SDag-Erling Smørgrav  *
85b7579f77SDag-Erling Smørgrav  * If no threading is compiled in, the above async example uses fork(2) to
86b7579f77SDag-Erling Smørgrav  * create a process to perform the work. The forked process exits when the
87b7579f77SDag-Erling Smørgrav  * calling process exits, or ctx_delete() is called.
88b7579f77SDag-Erling Smørgrav  * Otherwise, for asynchronous with threading, a worker thread is created.
89b7579f77SDag-Erling Smørgrav  *
90b7579f77SDag-Erling Smørgrav  * The blocking calls use shared ctx-cache when threaded. Thus
91b7579f77SDag-Erling Smørgrav  * ub_resolve() and ub_resolve_async() && ub_wait() are
92b7579f77SDag-Erling Smørgrav  * not the same. The first makes the current thread do the work, setting
93b7579f77SDag-Erling Smørgrav  * up buffers, etc, to perform the work (but using shared cache data).
94b7579f77SDag-Erling Smørgrav  * The second calls another worker thread (or process) to perform the work.
95b7579f77SDag-Erling Smørgrav  * And no buffers need to be set up, but a context-switch happens.
96b7579f77SDag-Erling Smørgrav  */
978f76bb7dSCy Schubert #ifndef UB_UNBOUND_H
988f76bb7dSCy Schubert #define UB_UNBOUND_H
99b7579f77SDag-Erling Smørgrav 
100b7579f77SDag-Erling Smørgrav #ifdef __cplusplus
101b7579f77SDag-Erling Smørgrav extern "C" {
102b7579f77SDag-Erling Smørgrav #endif
103b7579f77SDag-Erling Smørgrav 
10417d15b25SDag-Erling Smørgrav /** the version of this header file */
10517d15b25SDag-Erling Smørgrav #define UNBOUND_VERSION_MAJOR @UNBOUND_VERSION_MAJOR@
10617d15b25SDag-Erling Smørgrav #define UNBOUND_VERSION_MINOR @UNBOUND_VERSION_MINOR@
10717d15b25SDag-Erling Smørgrav #define UNBOUND_VERSION_MICRO @UNBOUND_VERSION_MICRO@
10817d15b25SDag-Erling Smørgrav 
109b7579f77SDag-Erling Smørgrav /**
110b7579f77SDag-Erling Smørgrav  * The validation context is created to hold the resolver status,
111b7579f77SDag-Erling Smørgrav  * validation keys and a small cache (containing messages, rrsets,
112b7579f77SDag-Erling Smørgrav  * roundtrip times, trusted keys, lameness information).
113b7579f77SDag-Erling Smørgrav  *
114b7579f77SDag-Erling Smørgrav  * Its contents are internally defined.
115b7579f77SDag-Erling Smørgrav  */
116b7579f77SDag-Erling Smørgrav struct ub_ctx;
117b7579f77SDag-Erling Smørgrav 
118b7579f77SDag-Erling Smørgrav /**
119b7579f77SDag-Erling Smørgrav  * The validation and resolution results.
120b7579f77SDag-Erling Smørgrav  * Allocated by the resolver, and need to be freed by the application
121b7579f77SDag-Erling Smørgrav  * with ub_resolve_free().
122b7579f77SDag-Erling Smørgrav  */
123b7579f77SDag-Erling Smørgrav struct ub_result {
124b7579f77SDag-Erling Smørgrav 	/** The original question, name text string. */
125b7579f77SDag-Erling Smørgrav 	char* qname;
126b7579f77SDag-Erling Smørgrav 	/** the type asked for */
127b7579f77SDag-Erling Smørgrav 	int qtype;
128b7579f77SDag-Erling Smørgrav 	/** the class asked for */
129b7579f77SDag-Erling Smørgrav 	int qclass;
130b7579f77SDag-Erling Smørgrav 
131b7579f77SDag-Erling Smørgrav 	/**
132b7579f77SDag-Erling Smørgrav 	 * a list of network order DNS rdata items, terminated with a
133b7579f77SDag-Erling Smørgrav 	 * NULL pointer, so that data[0] is the first result entry,
134b7579f77SDag-Erling Smørgrav 	 * data[1] the second, and the last entry is NULL.
135b7579f77SDag-Erling Smørgrav 	 * If there was no data, data[0] is NULL.
136b7579f77SDag-Erling Smørgrav 	 */
137b7579f77SDag-Erling Smørgrav 	char** data;
138b7579f77SDag-Erling Smørgrav 
139b7579f77SDag-Erling Smørgrav 	/** the length in bytes of the data items, len[i] for data[i] */
140b7579f77SDag-Erling Smørgrav 	int* len;
141b7579f77SDag-Erling Smørgrav 
142b7579f77SDag-Erling Smørgrav 	/**
143b7579f77SDag-Erling Smørgrav 	 * canonical name for the result (the final cname).
144b7579f77SDag-Erling Smørgrav 	 * zero terminated string.
145b7579f77SDag-Erling Smørgrav 	 * May be NULL if no canonical name exists.
146b7579f77SDag-Erling Smørgrav 	 */
147b7579f77SDag-Erling Smørgrav 	char* canonname;
148b7579f77SDag-Erling Smørgrav 
149b7579f77SDag-Erling Smørgrav 	/**
150b7579f77SDag-Erling Smørgrav 	 * DNS RCODE for the result. May contain additional error code if
151b7579f77SDag-Erling Smørgrav 	 * there was no data due to an error. 0 (NOERROR) if okay.
152b7579f77SDag-Erling Smørgrav 	 */
153b7579f77SDag-Erling Smørgrav 	int rcode;
154b7579f77SDag-Erling Smørgrav 
155b7579f77SDag-Erling Smørgrav 	/**
156b7579f77SDag-Erling Smørgrav 	 * The DNS answer packet. Network formatted. Can contain DNSSEC types.
157b7579f77SDag-Erling Smørgrav 	 */
158b7579f77SDag-Erling Smørgrav 	void* answer_packet;
159b7579f77SDag-Erling Smørgrav 	/** length of the answer packet in octets. */
160b7579f77SDag-Erling Smørgrav 	int answer_len;
161b7579f77SDag-Erling Smørgrav 
162b7579f77SDag-Erling Smørgrav 	/**
163b7579f77SDag-Erling Smørgrav 	 * If there is any data, this is true.
164b7579f77SDag-Erling Smørgrav 	 * If false, there was no data (nxdomain may be true, rcode can be set).
165b7579f77SDag-Erling Smørgrav 	 */
166b7579f77SDag-Erling Smørgrav 	int havedata;
167b7579f77SDag-Erling Smørgrav 
168b7579f77SDag-Erling Smørgrav 	/**
169b7579f77SDag-Erling Smørgrav 	 * If there was no data, and the domain did not exist, this is true.
170b7579f77SDag-Erling Smørgrav 	 * If it is false, and there was no data, then the domain name
171b7579f77SDag-Erling Smørgrav 	 * is purported to exist, but the requested data type is not available.
172b7579f77SDag-Erling Smørgrav 	 */
173b7579f77SDag-Erling Smørgrav 	int nxdomain;
174b7579f77SDag-Erling Smørgrav 
175b7579f77SDag-Erling Smørgrav 	/**
176b7579f77SDag-Erling Smørgrav 	 * True, if the result is validated securely.
177b7579f77SDag-Erling Smørgrav 	 * False, if validation failed or domain queried has no security info.
178b7579f77SDag-Erling Smørgrav 	 *
179b7579f77SDag-Erling Smørgrav 	 * It is possible to get a result with no data (havedata is false),
1808a384985SDag-Erling Smørgrav 	 * and secure is true. This means that the non-existence of the data
181b7579f77SDag-Erling Smørgrav 	 * was cryptographically proven (with signatures).
182b7579f77SDag-Erling Smørgrav 	 */
183b7579f77SDag-Erling Smørgrav 	int secure;
184b7579f77SDag-Erling Smørgrav 
185b7579f77SDag-Erling Smørgrav 	/**
186b7579f77SDag-Erling Smørgrav 	 * If the result was not secure (secure==0), and this result is due
187b7579f77SDag-Erling Smørgrav 	 * to a security failure, bogus is true.
188b7579f77SDag-Erling Smørgrav 	 * This means the data has been actively tampered with, signatures
189b7579f77SDag-Erling Smørgrav 	 * failed, expected signatures were not present, timestamps on
190b7579f77SDag-Erling Smørgrav 	 * signatures were out of date and so on.
191b7579f77SDag-Erling Smørgrav 	 *
192b7579f77SDag-Erling Smørgrav 	 * If !secure and !bogus, this can happen if the data is not secure
193b7579f77SDag-Erling Smørgrav 	 * because security is disabled for that domain name.
194b7579f77SDag-Erling Smørgrav 	 * This means the data is from a domain where data is not signed.
195b7579f77SDag-Erling Smørgrav 	 */
196b7579f77SDag-Erling Smørgrav 	int bogus;
197b7579f77SDag-Erling Smørgrav 
198b7579f77SDag-Erling Smørgrav 	/**
199b7579f77SDag-Erling Smørgrav 	 * If the result is bogus this contains a string (zero terminated)
200b7579f77SDag-Erling Smørgrav 	 * that describes the failure.  There may be other errors as well
201b7579f77SDag-Erling Smørgrav 	 * as the one described, the description may not be perfectly accurate.
202b7579f77SDag-Erling Smørgrav 	 * Is NULL if the result is not bogus.
203b7579f77SDag-Erling Smørgrav 	 */
204b7579f77SDag-Erling Smørgrav 	char* why_bogus;
2058ed2b524SDag-Erling Smørgrav 
2068ed2b524SDag-Erling Smørgrav 	/**
2074c75e3aaSDag-Erling Smørgrav 	 * If the query or one of its subqueries was ratelimited.  Useful if
208091e9e46SCy Schubert 	 * ratelimiting is enabled and answer to the client is SERVFAIL as a
209091e9e46SCy Schubert 	 * result.
2104c75e3aaSDag-Erling Smørgrav 	 */
2114c75e3aaSDag-Erling Smørgrav 	int was_ratelimited;
2124c75e3aaSDag-Erling Smørgrav 
2134c75e3aaSDag-Erling Smørgrav 	/**
2148ed2b524SDag-Erling Smørgrav 	 * TTL for the result, in seconds.  If the security is bogus, then
2158ed2b524SDag-Erling Smørgrav 	 * you also cannot trust this value.
2168ed2b524SDag-Erling Smørgrav 	 */
2178ed2b524SDag-Erling Smørgrav 	int ttl;
218b7579f77SDag-Erling Smørgrav };
219b7579f77SDag-Erling Smørgrav 
220b7579f77SDag-Erling Smørgrav /**
221b7579f77SDag-Erling Smørgrav  * Callback for results of async queries.
222b7579f77SDag-Erling Smørgrav  * The readable function definition looks like:
223b7579f77SDag-Erling Smørgrav  * void my_callback(void* my_arg, int err, struct ub_result* result);
224b7579f77SDag-Erling Smørgrav  * It is called with
225b7579f77SDag-Erling Smørgrav  *	void* my_arg: your pointer to a (struct of) data of your choice,
226b7579f77SDag-Erling Smørgrav  *		or NULL.
22724e36522SCy Schubert  *	int err: if 0 all is OK, otherwise an error occurred and no results
228b7579f77SDag-Erling Smørgrav  *	     are forthcoming.
229b7579f77SDag-Erling Smørgrav  *	struct result: pointer to more detailed result structure.
230b7579f77SDag-Erling Smørgrav  *		This structure is allocated on the heap and needs to be
231b7579f77SDag-Erling Smørgrav  *		freed with ub_resolve_free(result);
232b7579f77SDag-Erling Smørgrav  */
2333005e0a3SDag-Erling Smørgrav typedef void (*ub_callback_type)(void*, int, struct ub_result*);
234b7579f77SDag-Erling Smørgrav 
235b7579f77SDag-Erling Smørgrav /**
23624e36522SCy Schubert  * The error constants
23724e36522SCy Schubert  */
23824e36522SCy Schubert enum ub_ctx_err {
23924e36522SCy Schubert 	/** no error */
24024e36522SCy Schubert 	UB_NOERROR = 0,
24124e36522SCy Schubert 	/** socket operation. Set to -1, so that if an error from _fd() is
24224e36522SCy Schubert 	 * passed (-1) it gives a socket error. */
24324e36522SCy Schubert 	UB_SOCKET = -1,
24424e36522SCy Schubert 	/** alloc failure */
24524e36522SCy Schubert 	UB_NOMEM = -2,
24624e36522SCy Schubert 	/** syntax error */
24724e36522SCy Schubert 	UB_SYNTAX = -3,
24824e36522SCy Schubert 	/** DNS service failed */
24924e36522SCy Schubert 	UB_SERVFAIL = -4,
25024e36522SCy Schubert 	/** fork() failed */
25124e36522SCy Schubert 	UB_FORKFAIL = -5,
25224e36522SCy Schubert 	/** cfg change after finalize() */
25324e36522SCy Schubert 	UB_AFTERFINAL = -6,
25424e36522SCy Schubert 	/** initialization failed (bad settings) */
25524e36522SCy Schubert 	UB_INITFAIL = -7,
25624e36522SCy Schubert 	/** error in pipe communication with async bg worker */
25724e36522SCy Schubert 	UB_PIPE = -8,
25824e36522SCy Schubert 	/** error reading from file (resolv.conf) */
25924e36522SCy Schubert 	UB_READFILE = -9,
26024e36522SCy Schubert 	/** error async_id does not exist or result already been delivered */
26124e36522SCy Schubert 	UB_NOID = -10
26224e36522SCy Schubert };
26324e36522SCy Schubert 
26424e36522SCy Schubert /**
265b7579f77SDag-Erling Smørgrav  * Create a resolving and validation context.
266b7579f77SDag-Erling Smørgrav  * The information from /etc/resolv.conf and /etc/hosts is not utilised by
267b7579f77SDag-Erling Smørgrav  * default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them.
268b7579f77SDag-Erling Smørgrav  * @return a new context. default initialisation.
269b7579f77SDag-Erling Smørgrav  * 	returns NULL on error.
270b7579f77SDag-Erling Smørgrav  */
271b7579f77SDag-Erling Smørgrav struct ub_ctx* ub_ctx_create(void);
272b7579f77SDag-Erling Smørgrav 
273b7579f77SDag-Erling Smørgrav /**
274b7579f77SDag-Erling Smørgrav  * Destroy a validation context and free all its resources.
275b7579f77SDag-Erling Smørgrav  * Outstanding async queries are killed and callbacks are not called for them.
276b7579f77SDag-Erling Smørgrav  * @param ctx: context to delete.
277b7579f77SDag-Erling Smørgrav  */
278b7579f77SDag-Erling Smørgrav void ub_ctx_delete(struct ub_ctx* ctx);
279b7579f77SDag-Erling Smørgrav 
280b7579f77SDag-Erling Smørgrav /**
281b7579f77SDag-Erling Smørgrav  * Set an option for the context.
282b7579f77SDag-Erling Smørgrav  * @param ctx: context.
283b7579f77SDag-Erling Smørgrav  * @param opt: option name from the unbound.conf config file format.
284b7579f77SDag-Erling Smørgrav  *	(not all settings applicable). The name includes the trailing ':'
285b7579f77SDag-Erling Smørgrav  *	for example ub_ctx_set_option(ctx, "logfile:", "mylog.txt");
286b7579f77SDag-Erling Smørgrav  * 	This is a power-users interface that lets you specify all sorts
287b7579f77SDag-Erling Smørgrav  * 	of options.
288b7579f77SDag-Erling Smørgrav  * 	For some specific options, such as adding trust anchors, special
289b7579f77SDag-Erling Smørgrav  * 	routines exist.
290b7579f77SDag-Erling Smørgrav  * @param val: value of the option.
291b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
292b7579f77SDag-Erling Smørgrav  */
2935d649f2dSDag-Erling Smørgrav int ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val);
294b7579f77SDag-Erling Smørgrav 
295b7579f77SDag-Erling Smørgrav /**
296b7579f77SDag-Erling Smørgrav  * Get an option from the context.
297b7579f77SDag-Erling Smørgrav  * @param ctx: context.
298b7579f77SDag-Erling Smørgrav  * @param opt: option name from the unbound.conf config file format.
299b7579f77SDag-Erling Smørgrav  *	(not all settings applicable). The name excludes the trailing ':'
300b7579f77SDag-Erling Smørgrav  *	for example ub_ctx_get_option(ctx, "logfile", &result);
301b7579f77SDag-Erling Smørgrav  * 	This is a power-users interface that lets you specify all sorts
302b7579f77SDag-Erling Smørgrav  * 	of options.
303b7579f77SDag-Erling Smørgrav  * @param str: the string is malloced and returned here. NULL on error.
304b7579f77SDag-Erling Smørgrav  * 	The caller must free() the string.  In cases with multiple
305b7579f77SDag-Erling Smørgrav  * 	entries (auto-trust-anchor-file), a newline delimited list is
306b7579f77SDag-Erling Smørgrav  * 	returned in the string.
307b7579f77SDag-Erling Smørgrav  * @return 0 if OK else an error code (malloc failure, syntax error).
308b7579f77SDag-Erling Smørgrav  */
3095d649f2dSDag-Erling Smørgrav int ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str);
310b7579f77SDag-Erling Smørgrav 
311b7579f77SDag-Erling Smørgrav /**
312b7579f77SDag-Erling Smørgrav  * setup configuration for the given context.
313b7579f77SDag-Erling Smørgrav  * @param ctx: context.
314b7579f77SDag-Erling Smørgrav  * @param fname: unbound config file (not all settings applicable).
315b7579f77SDag-Erling Smørgrav  * 	This is a power-users interface that lets you specify all sorts
316b7579f77SDag-Erling Smørgrav  * 	of options.
317b7579f77SDag-Erling Smørgrav  * 	For some specific options, such as adding trust anchors, special
318b7579f77SDag-Erling Smørgrav  * 	routines exist.
319b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
320b7579f77SDag-Erling Smørgrav  */
3215d649f2dSDag-Erling Smørgrav int ub_ctx_config(struct ub_ctx* ctx, const char* fname);
322b7579f77SDag-Erling Smørgrav 
323b7579f77SDag-Erling Smørgrav /**
324b7579f77SDag-Erling Smørgrav  * Set machine to forward DNS queries to, the caching resolver to use.
325b7579f77SDag-Erling Smørgrav  * IP4 or IP6 address. Forwards all DNS requests to that machine, which
326b7579f77SDag-Erling Smørgrav  * is expected to run a recursive resolver. If the proxy is not
327b7579f77SDag-Erling Smørgrav  * DNSSEC-capable, validation may fail. Can be called several times, in
328b7579f77SDag-Erling Smørgrav  * that case the addresses are used as backup servers.
329b7579f77SDag-Erling Smørgrav  *
330b7579f77SDag-Erling Smørgrav  * To read the list of nameservers from /etc/resolv.conf (from DHCP or so),
331b7579f77SDag-Erling Smørgrav  * use the call ub_ctx_resolvconf.
332b7579f77SDag-Erling Smørgrav  *
333b7579f77SDag-Erling Smørgrav  * @param ctx: context.
334b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
335b7579f77SDag-Erling Smørgrav  *	first resolve is done.
336b7579f77SDag-Erling Smørgrav  * @param addr: address, IP4 or IP6 in string format.
337b7579f77SDag-Erling Smørgrav  * 	If the addr is NULL, forwarding is disabled.
338b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
339b7579f77SDag-Erling Smørgrav  */
3405d649f2dSDag-Erling Smørgrav int ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr);
341b7579f77SDag-Erling Smørgrav 
342b7579f77SDag-Erling Smørgrav /**
343e86b9096SDag-Erling Smørgrav  * Use DNS over TLS to send queries to machines set with ub_ctx_set_fwd().
344e86b9096SDag-Erling Smørgrav  *
345e86b9096SDag-Erling Smørgrav  * @param ctx: context.
346e86b9096SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
347e86b9096SDag-Erling Smørgrav  *	first resolve is done.
348e86b9096SDag-Erling Smørgrav  * @param tls: enable or disable DNS over TLS
349e86b9096SDag-Erling Smørgrav  * @return 0 if OK, else error.
350e86b9096SDag-Erling Smørgrav  */
351e86b9096SDag-Erling Smørgrav int ub_ctx_set_tls(struct ub_ctx* ctx, int tls);
352e86b9096SDag-Erling Smørgrav 
353e86b9096SDag-Erling Smørgrav /**
354f61ef7f6SDag-Erling Smørgrav  * Add a stub zone, with given address to send to.  This is for custom
355f61ef7f6SDag-Erling Smørgrav  * root hints or pointing to a local authoritative dns server.
356f61ef7f6SDag-Erling Smørgrav  * For dns resolvers and the 'DHCP DNS' ip address, use ub_ctx_set_fwd.
357f61ef7f6SDag-Erling Smørgrav  * This is similar to a stub-zone entry in unbound.conf.
358f61ef7f6SDag-Erling Smørgrav  *
359f61ef7f6SDag-Erling Smørgrav  * @param ctx: context.
360f61ef7f6SDag-Erling Smørgrav  *	It is only possible to set configuration before the
361f61ef7f6SDag-Erling Smørgrav  *	first resolve is done.
362f61ef7f6SDag-Erling Smørgrav  * @param zone: name of the zone, string.
363f61ef7f6SDag-Erling Smørgrav  * @param addr: address, IP4 or IP6 in string format.
364f61ef7f6SDag-Erling Smørgrav  * 	The addr is added to the list of stub-addresses if the entry exists.
365f61ef7f6SDag-Erling Smørgrav  * 	If the addr is NULL the stub entry is removed.
366f61ef7f6SDag-Erling Smørgrav  * @param isprime: set to true to set stub-prime to yes for the stub.
367f61ef7f6SDag-Erling Smørgrav  * 	For local authoritative servers, people usually set it to false,
368f61ef7f6SDag-Erling Smørgrav  * 	For root hints it should be set to true.
369f61ef7f6SDag-Erling Smørgrav  * @return 0 if OK, else error.
370f61ef7f6SDag-Erling Smørgrav  */
371f61ef7f6SDag-Erling Smørgrav int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
372f61ef7f6SDag-Erling Smørgrav 	int isprime);
373f61ef7f6SDag-Erling Smørgrav 
374f61ef7f6SDag-Erling Smørgrav /**
375b7579f77SDag-Erling Smørgrav  * Read list of nameservers to use from the filename given.
376b7579f77SDag-Erling Smørgrav  * Usually "/etc/resolv.conf". Uses those nameservers as caching proxies.
377b7579f77SDag-Erling Smørgrav  * If they do not support DNSSEC, validation may fail.
378b7579f77SDag-Erling Smørgrav  *
379b7579f77SDag-Erling Smørgrav  * Only nameservers are picked up, the searchdomain, ndots and other
380b7579f77SDag-Erling Smørgrav  * settings from resolv.conf(5) are ignored.
381b7579f77SDag-Erling Smørgrav  *
382b7579f77SDag-Erling Smørgrav  * @param ctx: context.
383b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
384b7579f77SDag-Erling Smørgrav  *	first resolve is done.
385b7579f77SDag-Erling Smørgrav  * @param fname: file name string. If NULL "/etc/resolv.conf" is used.
386b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
387b7579f77SDag-Erling Smørgrav  */
3885d649f2dSDag-Erling Smørgrav int ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname);
389b7579f77SDag-Erling Smørgrav 
390b7579f77SDag-Erling Smørgrav /**
391b7579f77SDag-Erling Smørgrav  * Read list of hosts from the filename given.
392b7579f77SDag-Erling Smørgrav  * Usually "/etc/hosts".
393b7579f77SDag-Erling Smørgrav  * These addresses are not flagged as DNSSEC secure when queried for.
394b7579f77SDag-Erling Smørgrav  *
395b7579f77SDag-Erling Smørgrav  * @param ctx: context.
396b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
397b7579f77SDag-Erling Smørgrav  *	first resolve is done.
398b7579f77SDag-Erling Smørgrav  * @param fname: file name string. If NULL "/etc/hosts" is used.
399b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
400b7579f77SDag-Erling Smørgrav  */
4015d649f2dSDag-Erling Smørgrav int ub_ctx_hosts(struct ub_ctx* ctx, const char* fname);
402b7579f77SDag-Erling Smørgrav 
403b7579f77SDag-Erling Smørgrav /**
404b7579f77SDag-Erling Smørgrav  * Add a trust anchor to the given context.
405b7579f77SDag-Erling Smørgrav  * The trust anchor is a string, on one line, that holds a valid DNSKEY or
406b7579f77SDag-Erling Smørgrav  * DS RR.
407b7579f77SDag-Erling Smørgrav  * @param ctx: context.
408b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
409b7579f77SDag-Erling Smørgrav  *	first resolve is done.
410b7579f77SDag-Erling Smørgrav  * @param ta: string, with zone-format RR on one line.
411b7579f77SDag-Erling Smørgrav  * 	[domainname] [TTL optional] [type] [class optional] [rdata contents]
412b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
413b7579f77SDag-Erling Smørgrav  */
4145d649f2dSDag-Erling Smørgrav int ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta);
415b7579f77SDag-Erling Smørgrav 
416b7579f77SDag-Erling Smørgrav /**
417b7579f77SDag-Erling Smørgrav  * Add trust anchors to the given context.
418b7579f77SDag-Erling Smørgrav  * Pass name of a file with DS and DNSKEY records (like from dig or drill).
419b7579f77SDag-Erling Smørgrav  * @param ctx: context.
420b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
421b7579f77SDag-Erling Smørgrav  *	first resolve is done.
422b7579f77SDag-Erling Smørgrav  * @param fname: filename of file with keyfile with trust anchors.
423b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
424b7579f77SDag-Erling Smørgrav  */
4255d649f2dSDag-Erling Smørgrav int ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname);
426b7579f77SDag-Erling Smørgrav 
427b7579f77SDag-Erling Smørgrav /**
428ff825849SDag-Erling Smørgrav  * Add trust anchor to the given context that is tracked with RFC5011
429ff825849SDag-Erling Smørgrav  * automated trust anchor maintenance.  The file is written to when the
430ff825849SDag-Erling Smørgrav  * trust anchor is changed.
431ff825849SDag-Erling Smørgrav  * Pass the name of a file that was output from eg. unbound-anchor,
432ff825849SDag-Erling Smørgrav  * or you can start it by providing a trusted DNSKEY or DS record on one
433ff825849SDag-Erling Smørgrav  * line in the file.
434ff825849SDag-Erling Smørgrav  * @param ctx: context.
435ff825849SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
436ff825849SDag-Erling Smørgrav  *	first resolve is done.
437ff825849SDag-Erling Smørgrav  * @param fname: filename of file with trust anchor.
438ff825849SDag-Erling Smørgrav  * @return 0 if OK, else error.
439ff825849SDag-Erling Smørgrav  */
440ff825849SDag-Erling Smørgrav int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname);
441ff825849SDag-Erling Smørgrav 
442ff825849SDag-Erling Smørgrav /**
443b7579f77SDag-Erling Smørgrav  * Add trust anchors to the given context.
444b7579f77SDag-Erling Smørgrav  * Pass the name of a bind-style config file with trusted-keys{}.
445b7579f77SDag-Erling Smørgrav  * @param ctx: context.
446b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
447b7579f77SDag-Erling Smørgrav  *	first resolve is done.
448b7579f77SDag-Erling Smørgrav  * @param fname: filename of file with bind-style config entries with trust
449b7579f77SDag-Erling Smørgrav  * 	anchors.
450b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
451b7579f77SDag-Erling Smørgrav  */
4525d649f2dSDag-Erling Smørgrav int ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname);
453b7579f77SDag-Erling Smørgrav 
454b7579f77SDag-Erling Smørgrav /**
455b7579f77SDag-Erling Smørgrav  * Set debug output (and error output) to the specified stream.
456b7579f77SDag-Erling Smørgrav  * Pass NULL to disable. Default is stderr.
457b7579f77SDag-Erling Smørgrav  * @param ctx: context.
458b7579f77SDag-Erling Smørgrav  * @param out: FILE* out file stream to log to.
459b7579f77SDag-Erling Smørgrav  * 	Type void* to avoid stdio dependency of this header file.
460b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
461b7579f77SDag-Erling Smørgrav  */
462b7579f77SDag-Erling Smørgrav int ub_ctx_debugout(struct ub_ctx* ctx, void* out);
463b7579f77SDag-Erling Smørgrav 
464b7579f77SDag-Erling Smørgrav /**
465b7579f77SDag-Erling Smørgrav  * Set debug verbosity for the context
466b7579f77SDag-Erling Smørgrav  * Output is directed to stderr.
467b7579f77SDag-Erling Smørgrav  * @param ctx: context.
468b7579f77SDag-Erling Smørgrav  * @param d: debug level, 0 is off, 1 is very minimal, 2 is detailed,
469b7579f77SDag-Erling Smørgrav  *	and 3 is lots.
470b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
471b7579f77SDag-Erling Smørgrav  */
472b7579f77SDag-Erling Smørgrav int ub_ctx_debuglevel(struct ub_ctx* ctx, int d);
473b7579f77SDag-Erling Smørgrav 
474b7579f77SDag-Erling Smørgrav /**
475b7579f77SDag-Erling Smørgrav  * Set a context behaviour for asynchronous action.
476b7579f77SDag-Erling Smørgrav  * @param ctx: context.
477b7579f77SDag-Erling Smørgrav  * @param dothread: if true, enables threading and a call to resolve_async()
478b7579f77SDag-Erling Smørgrav  *	creates a thread to handle work in the background.
479b7579f77SDag-Erling Smørgrav  *	If false, a process is forked to handle work in the background.
480b7579f77SDag-Erling Smørgrav  *	Changes to this setting after async() calls have been made have
481b7579f77SDag-Erling Smørgrav  *	no effect (delete and re-create the context to change).
482b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
483b7579f77SDag-Erling Smørgrav  */
484b7579f77SDag-Erling Smørgrav int ub_ctx_async(struct ub_ctx* ctx, int dothread);
485b7579f77SDag-Erling Smørgrav 
486b7579f77SDag-Erling Smørgrav /**
487b7579f77SDag-Erling Smørgrav  * Poll a context to see if it has any new results
488b7579f77SDag-Erling Smørgrav  * Do not poll in a loop, instead extract the fd below to poll for readiness,
489b7579f77SDag-Erling Smørgrav  * and then check, or wait using the wait routine.
490b7579f77SDag-Erling Smørgrav  * @param ctx: context.
491b7579f77SDag-Erling Smørgrav  * @return: 0 if nothing to read, or nonzero if a result is available.
492b7579f77SDag-Erling Smørgrav  * 	If nonzero, call ctx_process() to do callbacks.
493b7579f77SDag-Erling Smørgrav  */
494b7579f77SDag-Erling Smørgrav int ub_poll(struct ub_ctx* ctx);
495b7579f77SDag-Erling Smørgrav 
496b7579f77SDag-Erling Smørgrav /**
497b7579f77SDag-Erling Smørgrav  * Wait for a context to finish with results. Calls ub_process() after
498b7579f77SDag-Erling Smørgrav  * the wait for you. After the wait, there are no more outstanding
499b7579f77SDag-Erling Smørgrav  * asynchronous queries.
500b7579f77SDag-Erling Smørgrav  * @param ctx: context.
501b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
502b7579f77SDag-Erling Smørgrav  */
503b7579f77SDag-Erling Smørgrav int ub_wait(struct ub_ctx* ctx);
504b7579f77SDag-Erling Smørgrav 
505b7579f77SDag-Erling Smørgrav /**
506b7579f77SDag-Erling Smørgrav  * Get file descriptor. Wait for it to become readable, at this point
507b7579f77SDag-Erling Smørgrav  * answers are returned from the asynchronous validating resolver.
508b7579f77SDag-Erling Smørgrav  * Then call the ub_process to continue processing.
509b7579f77SDag-Erling Smørgrav  * This routine works immediately after context creation, the fd
510b7579f77SDag-Erling Smørgrav  * does not change.
511b7579f77SDag-Erling Smørgrav  * @param ctx: context.
512b7579f77SDag-Erling Smørgrav  * @return: -1 on error, or file descriptor to use select(2) with.
513b7579f77SDag-Erling Smørgrav  */
514b7579f77SDag-Erling Smørgrav int ub_fd(struct ub_ctx* ctx);
515b7579f77SDag-Erling Smørgrav 
516b7579f77SDag-Erling Smørgrav /**
517b7579f77SDag-Erling Smørgrav  * Call this routine to continue processing results from the validating
518b7579f77SDag-Erling Smørgrav  * resolver (when the fd becomes readable).
519b7579f77SDag-Erling Smørgrav  * Will perform necessary callbacks.
520b7579f77SDag-Erling Smørgrav  * @param ctx: context
521b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
522b7579f77SDag-Erling Smørgrav  */
523b7579f77SDag-Erling Smørgrav int ub_process(struct ub_ctx* ctx);
524b7579f77SDag-Erling Smørgrav 
525b7579f77SDag-Erling Smørgrav /**
526b7579f77SDag-Erling Smørgrav  * Perform resolution and validation of the target name.
527b7579f77SDag-Erling Smørgrav  * @param ctx: context.
528b7579f77SDag-Erling Smørgrav  *	The context is finalized, and can no longer accept config changes.
529b7579f77SDag-Erling Smørgrav  * @param name: domain name in text format (a zero terminated text string).
530b7579f77SDag-Erling Smørgrav  * @param rrtype: type of RR in host order, 1 is A (address).
531b7579f77SDag-Erling Smørgrav  * @param rrclass: class of RR in host order, 1 is IN (for internet).
532b7579f77SDag-Erling Smørgrav  * @param result: the result data is returned in a newly allocated result
533b7579f77SDag-Erling Smørgrav  * 	structure. May be NULL on return, return value is set to an error
534b7579f77SDag-Erling Smørgrav  * 	in that case (out of memory).
535b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
536b7579f77SDag-Erling Smørgrav  */
5375d649f2dSDag-Erling Smørgrav int ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
538b7579f77SDag-Erling Smørgrav 	int rrclass, struct ub_result** result);
539b7579f77SDag-Erling Smørgrav 
540b7579f77SDag-Erling Smørgrav /**
541b7579f77SDag-Erling Smørgrav  * Perform resolution and validation of the target name.
542b7579f77SDag-Erling Smørgrav  * Asynchronous, after a while, the callback will be called with your
543b7579f77SDag-Erling Smørgrav  * data and the result.
544b7579f77SDag-Erling Smørgrav  * @param ctx: context.
545b7579f77SDag-Erling Smørgrav  *	If no thread or process has been created yet to perform the
546b7579f77SDag-Erling Smørgrav  *	work in the background, it is created now.
547b7579f77SDag-Erling Smørgrav  *	The context is finalized, and can no longer accept config changes.
548b7579f77SDag-Erling Smørgrav  * @param name: domain name in text format (a string).
549b7579f77SDag-Erling Smørgrav  * @param rrtype: type of RR in host order, 1 is A.
550b7579f77SDag-Erling Smørgrav  * @param rrclass: class of RR in host order, 1 is IN (for internet).
551b7579f77SDag-Erling Smørgrav  * @param mydata: this data is your own data (you can pass NULL),
552b7579f77SDag-Erling Smørgrav  * 	and is passed on to the callback function.
553b7579f77SDag-Erling Smørgrav  * @param callback: this is called on completion of the resolution.
554b7579f77SDag-Erling Smørgrav  * 	It is called as:
555b7579f77SDag-Erling Smørgrav  * 	void callback(void* mydata, int err, struct ub_result* result)
556b7579f77SDag-Erling Smørgrav  * 	with mydata: the same as passed here, you may pass NULL,
557b7579f77SDag-Erling Smørgrav  * 	with err: is 0 when a result has been found.
558b7579f77SDag-Erling Smørgrav  * 	with result: a newly allocated result structure.
559b7579f77SDag-Erling Smørgrav  *		The result may be NULL, in that case err is set.
560b7579f77SDag-Erling Smørgrav  *
561b7579f77SDag-Erling Smørgrav  * 	If an error happens during processing, your callback will be called
562b7579f77SDag-Erling Smørgrav  * 	with error set to a nonzero value (and result==NULL).
563b7579f77SDag-Erling Smørgrav  * @param async_id: if you pass a non-NULL value, an identifier number is
564b7579f77SDag-Erling Smørgrav  *	returned for the query as it is in progress. It can be used to
565b7579f77SDag-Erling Smørgrav  *	cancel the query.
566b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
567b7579f77SDag-Erling Smørgrav  */
5685d649f2dSDag-Erling Smørgrav int ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
5693005e0a3SDag-Erling Smørgrav 	int rrclass, void* mydata, ub_callback_type callback, int* async_id);
570b7579f77SDag-Erling Smørgrav 
571b7579f77SDag-Erling Smørgrav /**
572b7579f77SDag-Erling Smørgrav  * Cancel an async query in progress.
573b7579f77SDag-Erling Smørgrav  * Its callback will not be called.
574b7579f77SDag-Erling Smørgrav  *
575b7579f77SDag-Erling Smørgrav  * @param ctx: context.
576b7579f77SDag-Erling Smørgrav  * @param async_id: which query to cancel.
577b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
578b7579f77SDag-Erling Smørgrav  * This routine can return an error if the async_id passed does not exist
579b7579f77SDag-Erling Smørgrav  * or has already been delivered. If another thread is processing results
580b7579f77SDag-Erling Smørgrav  * at the same time, the result may be delivered at the same time and the
581b7579f77SDag-Erling Smørgrav  * cancel fails with an error.  Also the cancel can fail due to a system
582b7579f77SDag-Erling Smørgrav  * error, no memory or socket failures.
583b7579f77SDag-Erling Smørgrav  */
584b7579f77SDag-Erling Smørgrav int ub_cancel(struct ub_ctx* ctx, int async_id);
585b7579f77SDag-Erling Smørgrav 
586b7579f77SDag-Erling Smørgrav /**
587b7579f77SDag-Erling Smørgrav  * Free storage associated with a result structure.
588b7579f77SDag-Erling Smørgrav  * @param result: to free
589b7579f77SDag-Erling Smørgrav  */
590b7579f77SDag-Erling Smørgrav void ub_resolve_free(struct ub_result* result);
591b7579f77SDag-Erling Smørgrav 
592b7579f77SDag-Erling Smørgrav /**
593b7579f77SDag-Erling Smørgrav  * Convert error value to a human readable string.
594ff825849SDag-Erling Smørgrav  * @param err: error code from one of the libunbound functions.
59524e36522SCy Schubert  * 	The error codes are from the type enum ub_ctx_err.
596b7579f77SDag-Erling Smørgrav  * @return pointer to constant text string, zero terminated.
597b7579f77SDag-Erling Smørgrav  */
598b7579f77SDag-Erling Smørgrav const char* ub_strerror(int err);
599b7579f77SDag-Erling Smørgrav 
600b7579f77SDag-Erling Smørgrav /**
601b7579f77SDag-Erling Smørgrav  * Debug routine.  Print the local zone information to debug output.
602b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
603b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
604b7579f77SDag-Erling Smørgrav  */
605b7579f77SDag-Erling Smørgrav int ub_ctx_print_local_zones(struct ub_ctx* ctx);
606b7579f77SDag-Erling Smørgrav 
607b7579f77SDag-Erling Smørgrav /**
608b7579f77SDag-Erling Smørgrav  * Add a new zone with the zonetype to the local authority info of the
609b7579f77SDag-Erling Smørgrav  * library.
610b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
611b7579f77SDag-Erling Smørgrav  * @param zone_name: name of the zone in text, "example.com"
612b7579f77SDag-Erling Smørgrav  *	If it already exists, the type is updated.
613b7579f77SDag-Erling Smørgrav  * @param zone_type: type of the zone (like for unbound.conf) in text.
614b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
615b7579f77SDag-Erling Smørgrav  */
61617d15b25SDag-Erling Smørgrav int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
61717d15b25SDag-Erling Smørgrav 	const char *zone_type);
618b7579f77SDag-Erling Smørgrav 
619b7579f77SDag-Erling Smørgrav /**
620b7579f77SDag-Erling Smørgrav  * Remove zone from local authority info of the library.
621b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
622b7579f77SDag-Erling Smørgrav  * @param zone_name: name of the zone in text, "example.com"
623b7579f77SDag-Erling Smørgrav  *	If it does not exist, nothing happens.
624b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
625b7579f77SDag-Erling Smørgrav  */
62617d15b25SDag-Erling Smørgrav int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name);
627b7579f77SDag-Erling Smørgrav 
628b7579f77SDag-Erling Smørgrav /**
629b7579f77SDag-Erling Smørgrav  * Add localdata to the library local authority info.
630b7579f77SDag-Erling Smørgrav  * Similar to local-data config statement.
631b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
632b7579f77SDag-Erling Smørgrav  * @param data: the resource record in text format, for example
633b7579f77SDag-Erling Smørgrav  *	"www.example.com IN A 127.0.0.1"
634b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
635b7579f77SDag-Erling Smørgrav  */
63617d15b25SDag-Erling Smørgrav int ub_ctx_data_add(struct ub_ctx* ctx, const char *data);
637b7579f77SDag-Erling Smørgrav 
638b7579f77SDag-Erling Smørgrav /**
639b7579f77SDag-Erling Smørgrav  * Remove localdata from the library local authority info.
640b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
641b7579f77SDag-Erling Smørgrav  * @param data: the name to delete all data from, like "www.example.com".
642b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
643b7579f77SDag-Erling Smørgrav  */
64417d15b25SDag-Erling Smørgrav int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data);
645b7579f77SDag-Erling Smørgrav 
646b7579f77SDag-Erling Smørgrav /**
647b7579f77SDag-Erling Smørgrav  * Get a version string from the libunbound implementation.
648b7579f77SDag-Erling Smørgrav  * @return a static constant string with the version number.
649b7579f77SDag-Erling Smørgrav  */
650b7579f77SDag-Erling Smørgrav const char* ub_version(void);
651b7579f77SDag-Erling Smørgrav 
652c7f4d7adSDag-Erling Smørgrav /**
653c7f4d7adSDag-Erling Smørgrav  * Some global statistics that are not in struct stats_info,
654c7f4d7adSDag-Erling Smørgrav  * this struct is shared on a shm segment (shm-key in unbound.conf)
655c7f4d7adSDag-Erling Smørgrav  */
656c7f4d7adSDag-Erling Smørgrav struct ub_shm_stat_info {
657c7f4d7adSDag-Erling Smørgrav 	int num_threads;
658c7f4d7adSDag-Erling Smørgrav 
659c7f4d7adSDag-Erling Smørgrav 	struct {
660c7f4d7adSDag-Erling Smørgrav 		long long now_sec, now_usec;
661c7f4d7adSDag-Erling Smørgrav 		long long up_sec, up_usec;
662c7f4d7adSDag-Erling Smørgrav 		long long elapsed_sec, elapsed_usec;
663c7f4d7adSDag-Erling Smørgrav 	} time;
664c7f4d7adSDag-Erling Smørgrav 
665c7f4d7adSDag-Erling Smørgrav 	struct {
666c7f4d7adSDag-Erling Smørgrav 		long long msg;
667c7f4d7adSDag-Erling Smørgrav 		long long rrset;
668c7f4d7adSDag-Erling Smørgrav 		long long val;
669c7f4d7adSDag-Erling Smørgrav 		long long iter;
670c7f4d7adSDag-Erling Smørgrav 		long long subnet;
671c7f4d7adSDag-Erling Smørgrav 		long long ipsecmod;
672c7f4d7adSDag-Erling Smørgrav 		long long respip;
673971980c3SDag-Erling Smørgrav 		long long dnscrypt_shared_secret;
6748a384985SDag-Erling Smørgrav 		long long dnscrypt_nonce;
67525039b37SCy Schubert 		long long dynlib;
676c7f4d7adSDag-Erling Smørgrav 	} mem;
677c7f4d7adSDag-Erling Smørgrav };
678c7f4d7adSDag-Erling Smørgrav 
679c7f4d7adSDag-Erling Smørgrav /** number of qtype that is stored for in array */
680c7f4d7adSDag-Erling Smørgrav #define UB_STATS_QTYPE_NUM 256
681c7f4d7adSDag-Erling Smørgrav /** number of qclass that is stored for in array */
682c7f4d7adSDag-Erling Smørgrav #define UB_STATS_QCLASS_NUM 256
683c7f4d7adSDag-Erling Smørgrav /** number of rcodes in stats */
684c7f4d7adSDag-Erling Smørgrav #define UB_STATS_RCODE_NUM 16
685c7f4d7adSDag-Erling Smørgrav /** number of opcodes in stats */
686c7f4d7adSDag-Erling Smørgrav #define UB_STATS_OPCODE_NUM 16
687c7f4d7adSDag-Erling Smørgrav /** number of histogram buckets */
688c7f4d7adSDag-Erling Smørgrav #define UB_STATS_BUCKET_NUM 40
689091e9e46SCy Schubert /** number of RPZ actions */
690091e9e46SCy Schubert #define UB_STATS_RPZ_ACTION_NUM 10
691c7f4d7adSDag-Erling Smørgrav 
692c7f4d7adSDag-Erling Smørgrav /** per worker statistics. */
693c7f4d7adSDag-Erling Smørgrav struct ub_server_stats {
694c7f4d7adSDag-Erling Smørgrav 	/** number of queries from clients received. */
695c7f4d7adSDag-Erling Smørgrav 	long long num_queries;
696c7f4d7adSDag-Erling Smørgrav 	/** number of queries that have been dropped/ratelimited by ip. */
697c7f4d7adSDag-Erling Smørgrav 	long long num_queries_ip_ratelimited;
6988f76bb7dSCy Schubert 	/** number of queries with a valid DNS Cookie. */
6998f76bb7dSCy Schubert 	long long num_queries_cookie_valid;
7008f76bb7dSCy Schubert 	/** number of queries with only the client part of the DNS Cookie. */
7018f76bb7dSCy Schubert 	long long num_queries_cookie_client;
7028f76bb7dSCy Schubert 	/** number of queries with invalid DNS Cookie. */
7038f76bb7dSCy Schubert 	long long num_queries_cookie_invalid;
704c7f4d7adSDag-Erling Smørgrav 	/** number of queries that had a cache-miss. */
705c7f4d7adSDag-Erling Smørgrav 	long long num_queries_missed_cache;
706c7f4d7adSDag-Erling Smørgrav 	/** number of prefetch queries - cachehits with prefetch */
707c7f4d7adSDag-Erling Smørgrav 	long long num_queries_prefetch;
7088f76bb7dSCy Schubert 	/** number of queries which are too late to process */
7098f76bb7dSCy Schubert 	long long num_queries_timed_out;
7108f76bb7dSCy Schubert 	/** the longest wait time in the queue */
7118f76bb7dSCy Schubert 	long long max_query_time_us;
712c7f4d7adSDag-Erling Smørgrav 	/**
713c7f4d7adSDag-Erling Smørgrav 	 * Sum of the querylistsize of the worker for
714c7f4d7adSDag-Erling Smørgrav 	 * every query that missed cache. To calculate average.
715c7f4d7adSDag-Erling Smørgrav 	 */
716c7f4d7adSDag-Erling Smørgrav 	long long sum_query_list_size;
717c7f4d7adSDag-Erling Smørgrav 	/** max value of query list size reached. */
718c7f4d7adSDag-Erling Smørgrav 	long long max_query_list_size;
719c7f4d7adSDag-Erling Smørgrav 
720c7f4d7adSDag-Erling Smørgrav 	/** Extended stats below (bool) */
721c7f4d7adSDag-Erling Smørgrav 	int extended;
722c7f4d7adSDag-Erling Smørgrav 
723c7f4d7adSDag-Erling Smørgrav 	/** qtype stats */
724c7f4d7adSDag-Erling Smørgrav 	long long qtype[UB_STATS_QTYPE_NUM];
725c7f4d7adSDag-Erling Smørgrav 	/** bigger qtype values not in array */
726c7f4d7adSDag-Erling Smørgrav 	long long qtype_big;
727c7f4d7adSDag-Erling Smørgrav 	/** qclass stats */
728c7f4d7adSDag-Erling Smørgrav 	long long qclass[UB_STATS_QCLASS_NUM];
729c7f4d7adSDag-Erling Smørgrav 	/** bigger qclass values not in array */
730c7f4d7adSDag-Erling Smørgrav 	long long qclass_big;
731c7f4d7adSDag-Erling Smørgrav 	/** query opcodes */
732c7f4d7adSDag-Erling Smørgrav 	long long qopcode[UB_STATS_OPCODE_NUM];
733c7f4d7adSDag-Erling Smørgrav 	/** number of queries over TCP */
734c7f4d7adSDag-Erling Smørgrav 	long long qtcp;
735c7f4d7adSDag-Erling Smørgrav 	/** number of outgoing queries over TCP */
736c7f4d7adSDag-Erling Smørgrav 	long long qtcp_outgoing;
7370a92a9fcSCy Schubert 	/** number of outgoing queries over UDP */
7380a92a9fcSCy Schubert 	long long qudp_outgoing;
7394c75e3aaSDag-Erling Smørgrav 	/** number of queries over (DNS over) TLS */
7404c75e3aaSDag-Erling Smørgrav 	long long qtls;
741c0caa2e2SCy Schubert 	/** number of queries over (DNS over) HTTPS */
742c0caa2e2SCy Schubert 	long long qhttps;
743c7f4d7adSDag-Erling Smørgrav 	/** number of queries over IPv6 */
744c7f4d7adSDag-Erling Smørgrav 	long long qipv6;
745c7f4d7adSDag-Erling Smørgrav 	/** number of queries with QR bit */
746c7f4d7adSDag-Erling Smørgrav 	long long qbit_QR;
747c7f4d7adSDag-Erling Smørgrav 	/** number of queries with AA bit */
748c7f4d7adSDag-Erling Smørgrav 	long long qbit_AA;
749c7f4d7adSDag-Erling Smørgrav 	/** number of queries with TC bit */
750c7f4d7adSDag-Erling Smørgrav 	long long qbit_TC;
751c7f4d7adSDag-Erling Smørgrav 	/** number of queries with RD bit */
752c7f4d7adSDag-Erling Smørgrav 	long long qbit_RD;
753c7f4d7adSDag-Erling Smørgrav 	/** number of queries with RA bit */
754c7f4d7adSDag-Erling Smørgrav 	long long qbit_RA;
755c7f4d7adSDag-Erling Smørgrav 	/** number of queries with Z bit */
756c7f4d7adSDag-Erling Smørgrav 	long long qbit_Z;
757c7f4d7adSDag-Erling Smørgrav 	/** number of queries with AD bit */
758c7f4d7adSDag-Erling Smørgrav 	long long qbit_AD;
759c7f4d7adSDag-Erling Smørgrav 	/** number of queries with CD bit */
760c7f4d7adSDag-Erling Smørgrav 	long long qbit_CD;
761c7f4d7adSDag-Erling Smørgrav 	/** number of queries with EDNS OPT record */
762c7f4d7adSDag-Erling Smørgrav 	long long qEDNS;
763c7f4d7adSDag-Erling Smørgrav 	/** number of queries with EDNS with DO flag */
764c7f4d7adSDag-Erling Smørgrav 	long long qEDNS_DO;
765c7f4d7adSDag-Erling Smørgrav 	/** answer rcodes */
766c7f4d7adSDag-Erling Smørgrav 	long long ans_rcode[UB_STATS_RCODE_NUM];
767c7f4d7adSDag-Erling Smørgrav 	/** answers with pseudo rcode 'nodata' */
768c7f4d7adSDag-Erling Smørgrav 	long long ans_rcode_nodata;
769c7f4d7adSDag-Erling Smørgrav 	/** answers that were secure (AD) */
770c7f4d7adSDag-Erling Smørgrav 	long long ans_secure;
771c7f4d7adSDag-Erling Smørgrav 	/** answers that were bogus (withheld as SERVFAIL) */
772c7f4d7adSDag-Erling Smørgrav 	long long ans_bogus;
773c7f4d7adSDag-Erling Smørgrav 	/** rrsets marked bogus by validator */
774c7f4d7adSDag-Erling Smørgrav 	long long rrset_bogus;
775971980c3SDag-Erling Smørgrav 	/** number of queries that have been ratelimited by domain recursion. */
776971980c3SDag-Erling Smørgrav 	long long queries_ratelimited;
777c7f4d7adSDag-Erling Smørgrav 	/** unwanted traffic received on server-facing ports */
778c7f4d7adSDag-Erling Smørgrav 	long long unwanted_replies;
779c7f4d7adSDag-Erling Smørgrav 	/** unwanted traffic received on client-facing ports */
780c7f4d7adSDag-Erling Smørgrav 	long long unwanted_queries;
781c7f4d7adSDag-Erling Smørgrav 	/** usage of tcp accept list */
782c7f4d7adSDag-Erling Smørgrav 	long long tcp_accept_usage;
783091e9e46SCy Schubert 	/** expired answers served from cache */
784091e9e46SCy Schubert 	long long ans_expired;
785c7f4d7adSDag-Erling Smørgrav 	/** histogram data exported to array
786c7f4d7adSDag-Erling Smørgrav 	 * if the array is the same size, no data is lost, and
787c7f4d7adSDag-Erling Smørgrav 	 * if all histograms are same size (is so by default) then
788c7f4d7adSDag-Erling Smørgrav 	 * adding up works well. */
789c7f4d7adSDag-Erling Smørgrav 	long long hist[UB_STATS_BUCKET_NUM];
790c7f4d7adSDag-Erling Smørgrav 
791c7f4d7adSDag-Erling Smørgrav 	/** number of message cache entries */
792c7f4d7adSDag-Erling Smørgrav 	long long msg_cache_count;
793c7f4d7adSDag-Erling Smørgrav 	/** number of rrset cache entries */
794c7f4d7adSDag-Erling Smørgrav 	long long rrset_cache_count;
795c7f4d7adSDag-Erling Smørgrav 	/** number of infra cache entries */
796c7f4d7adSDag-Erling Smørgrav 	long long infra_cache_count;
797c7f4d7adSDag-Erling Smørgrav 	/** number of key cache entries */
798c7f4d7adSDag-Erling Smørgrav 	long long key_cache_count;
799c7f4d7adSDag-Erling Smørgrav 
8008f76bb7dSCy Schubert 	/** maximum number of collisions in the msg cache */
8018f76bb7dSCy Schubert 	long long msg_cache_max_collisions;
8028f76bb7dSCy Schubert 	/** maximum number of collisions in the rrset cache */
8038f76bb7dSCy Schubert 	long long rrset_cache_max_collisions;
8048f76bb7dSCy Schubert 
805c7f4d7adSDag-Erling Smørgrav 	/** number of queries that used dnscrypt */
806c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_crypted;
807c7f4d7adSDag-Erling Smørgrav 	/** number of queries that queried dnscrypt certificates */
808c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_cert;
809c7f4d7adSDag-Erling Smørgrav 	/** number of queries in clear text and not asking for the certificates */
810c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_cleartext;
811c7f4d7adSDag-Erling Smørgrav 	/** number of malformed encrypted queries */
812c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_crypted_malformed;
813971980c3SDag-Erling Smørgrav 	/** number of queries which did not have a shared secret in cache */
814971980c3SDag-Erling Smørgrav 	long long num_query_dnscrypt_secret_missed_cache;
815971980c3SDag-Erling Smørgrav 	/** number of dnscrypt shared secret cache entries */
816971980c3SDag-Erling Smørgrav 	long long shared_secret_cache_count;
8178a384985SDag-Erling Smørgrav 	/** number of queries which are replays */
8188a384985SDag-Erling Smørgrav 	long long num_query_dnscrypt_replay;
8198a384985SDag-Erling Smørgrav 	/** number of dnscrypt nonces cache entries */
8208a384985SDag-Erling Smørgrav 	long long nonce_cache_count;
8210fb34990SDag-Erling Smørgrav 	/** number of queries for unbound's auth_zones, upstream query */
8220fb34990SDag-Erling Smørgrav 	long long num_query_authzone_up;
8230fb34990SDag-Erling Smørgrav 	/** number of queries for unbound's auth_zones, downstream answers */
8240fb34990SDag-Erling Smørgrav 	long long num_query_authzone_down;
8250fb34990SDag-Erling Smørgrav 	/** number of times neg cache records were used to generate NOERROR
8260fb34990SDag-Erling Smørgrav 	 * responses. */
8270fb34990SDag-Erling Smørgrav 	long long num_neg_cache_noerror;
8280fb34990SDag-Erling Smørgrav 	/** number of times neg cache records were used to generate NXDOMAIN
8290fb34990SDag-Erling Smørgrav 	 * responses. */
8300fb34990SDag-Erling Smørgrav 	long long num_neg_cache_nxdomain;
8314c75e3aaSDag-Erling Smørgrav 	/** number of queries answered from edns-subnet specific data */
8324c75e3aaSDag-Erling Smørgrav 	long long num_query_subnet;
8334c75e3aaSDag-Erling Smørgrav 	/** number of queries answered from edns-subnet specific data, and
8344c75e3aaSDag-Erling Smørgrav 	 * the answer was from the edns-subnet cache. */
8354c75e3aaSDag-Erling Smørgrav 	long long num_query_subnet_cache;
8368f76bb7dSCy Schubert 	/** number of queries served from cachedb */
8378f76bb7dSCy Schubert 	long long num_query_cachedb;
838e86b9096SDag-Erling Smørgrav 	/** number of bytes in the stream wait buffers */
839e86b9096SDag-Erling Smørgrav 	long long mem_stream_wait;
840c0caa2e2SCy Schubert 	/** number of bytes in the HTTP2 query buffers */
841c0caa2e2SCy Schubert 	long long mem_http2_query_buffer;
842c0caa2e2SCy Schubert 	/** number of bytes in the HTTP2 response buffers */
843c0caa2e2SCy Schubert 	long long mem_http2_response_buffer;
844e86b9096SDag-Erling Smørgrav 	/** number of TLS connection resume */
845e86b9096SDag-Erling Smørgrav 	long long qtls_resume;
846091e9e46SCy Schubert 	/** RPZ action stats */
847091e9e46SCy Schubert 	long long rpz_action[UB_STATS_RPZ_ACTION_NUM];
84846d2f618SCy Schubert 	/** number of bytes in QUIC buffers */
84946d2f618SCy Schubert 	long long mem_quic;
85046d2f618SCy Schubert 	/** number of queries over (DNS over) QUIC */
85146d2f618SCy Schubert 	long long qquic;
852*be771a7bSCy Schubert 	/** number of queries removed due to discard-timeout */
853*be771a7bSCy Schubert 	long long num_queries_discard_timeout;
854*be771a7bSCy Schubert 	/** number of queries removed due to wait-limit */
855*be771a7bSCy Schubert 	long long num_queries_wait_limit;
856*be771a7bSCy Schubert 	/** number of dns error reports generated */
857*be771a7bSCy Schubert 	long long num_dns_error_reports;
858c7f4d7adSDag-Erling Smørgrav };
859c7f4d7adSDag-Erling Smørgrav 
860c7f4d7adSDag-Erling Smørgrav /**
861c7f4d7adSDag-Erling Smørgrav  * Statistics to send over the control pipe when asked
8628a384985SDag-Erling Smørgrav  * This struct is made to be memcopied, sent in binary.
863c7f4d7adSDag-Erling Smørgrav  * shm mapped with (number+1) at num_threads+1, with first as total
864c7f4d7adSDag-Erling Smørgrav  */
865c7f4d7adSDag-Erling Smørgrav struct ub_stats_info {
866c7f4d7adSDag-Erling Smørgrav 	/** the thread stats */
867c7f4d7adSDag-Erling Smørgrav 	struct ub_server_stats svr;
868c7f4d7adSDag-Erling Smørgrav 
869c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: current number of states */
870c7f4d7adSDag-Erling Smørgrav 	long long mesh_num_states;
871c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: current number of reply (user) states */
872c7f4d7adSDag-Erling Smørgrav 	long long mesh_num_reply_states;
873c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: number of reply states overwritten with a new one */
874c7f4d7adSDag-Erling Smørgrav 	long long mesh_jostled;
875c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: number of incoming queries dropped */
876c7f4d7adSDag-Erling Smørgrav 	long long mesh_dropped;
877c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: replies sent */
878c7f4d7adSDag-Erling Smørgrav 	long long mesh_replies_sent;
879c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: sum of waiting times for the replies */
880c7f4d7adSDag-Erling Smørgrav 	long long mesh_replies_sum_wait_sec, mesh_replies_sum_wait_usec;
881c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: median of waiting times for replies (in sec) */
882c7f4d7adSDag-Erling Smørgrav 	double mesh_time_median;
883c7f4d7adSDag-Erling Smørgrav };
884c7f4d7adSDag-Erling Smørgrav 
885b7579f77SDag-Erling Smørgrav #ifdef __cplusplus
886b7579f77SDag-Erling Smørgrav }
887b7579f77SDag-Erling Smørgrav #endif
888b7579f77SDag-Erling Smørgrav 
8898f76bb7dSCy Schubert #endif /* UB_UNBOUND_H */
890