xref: /freebsd/contrib/unbound/libunbound/unbound.h (revision 4c75e3aa0f1368f18240b8bfd0e1e88f64994a1c)
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  */
97b7579f77SDag-Erling Smørgrav #ifndef _UB_UNBOUND_H
98b7579f77SDag-Erling Smørgrav #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 	/**
207*4c75e3aaSDag-Erling Smørgrav 	 * If the query or one of its subqueries was ratelimited. Useful if
208*4c75e3aaSDag-Erling Smørgrav 	 * ratelimiting is enabled and answer is SERVFAIL.
209*4c75e3aaSDag-Erling Smørgrav 	 */
210*4c75e3aaSDag-Erling Smørgrav 	int was_ratelimited;
211*4c75e3aaSDag-Erling Smørgrav 
212*4c75e3aaSDag-Erling Smørgrav 	/**
2138ed2b524SDag-Erling Smørgrav 	 * TTL for the result, in seconds.  If the security is bogus, then
2148ed2b524SDag-Erling Smørgrav 	 * you also cannot trust this value.
2158ed2b524SDag-Erling Smørgrav 	 */
2168ed2b524SDag-Erling Smørgrav 	int ttl;
217b7579f77SDag-Erling Smørgrav };
218b7579f77SDag-Erling Smørgrav 
219b7579f77SDag-Erling Smørgrav /**
220b7579f77SDag-Erling Smørgrav  * Callback for results of async queries.
221b7579f77SDag-Erling Smørgrav  * The readable function definition looks like:
222b7579f77SDag-Erling Smørgrav  * void my_callback(void* my_arg, int err, struct ub_result* result);
223b7579f77SDag-Erling Smørgrav  * It is called with
224b7579f77SDag-Erling Smørgrav  *	void* my_arg: your pointer to a (struct of) data of your choice,
225b7579f77SDag-Erling Smørgrav  *		or NULL.
226b7579f77SDag-Erling Smørgrav  *	int err: if 0 all is OK, otherwise an error occured and no results
227b7579f77SDag-Erling Smørgrav  *	     are forthcoming.
228b7579f77SDag-Erling Smørgrav  *	struct result: pointer to more detailed result structure.
229b7579f77SDag-Erling Smørgrav  *		This structure is allocated on the heap and needs to be
230b7579f77SDag-Erling Smørgrav  *		freed with ub_resolve_free(result);
231b7579f77SDag-Erling Smørgrav  */
2323005e0a3SDag-Erling Smørgrav typedef void (*ub_callback_type)(void*, int, struct ub_result*);
233b7579f77SDag-Erling Smørgrav 
234b7579f77SDag-Erling Smørgrav /**
235b7579f77SDag-Erling Smørgrav  * Create a resolving and validation context.
236b7579f77SDag-Erling Smørgrav  * The information from /etc/resolv.conf and /etc/hosts is not utilised by
237b7579f77SDag-Erling Smørgrav  * default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them.
238b7579f77SDag-Erling Smørgrav  * @return a new context. default initialisation.
239b7579f77SDag-Erling Smørgrav  * 	returns NULL on error.
240b7579f77SDag-Erling Smørgrav  */
241b7579f77SDag-Erling Smørgrav struct ub_ctx* ub_ctx_create(void);
242b7579f77SDag-Erling Smørgrav 
243b7579f77SDag-Erling Smørgrav /**
244b7579f77SDag-Erling Smørgrav  * Destroy a validation context and free all its resources.
245b7579f77SDag-Erling Smørgrav  * Outstanding async queries are killed and callbacks are not called for them.
246b7579f77SDag-Erling Smørgrav  * @param ctx: context to delete.
247b7579f77SDag-Erling Smørgrav  */
248b7579f77SDag-Erling Smørgrav void ub_ctx_delete(struct ub_ctx* ctx);
249b7579f77SDag-Erling Smørgrav 
250b7579f77SDag-Erling Smørgrav /**
251b7579f77SDag-Erling Smørgrav  * Set an option for the context.
252b7579f77SDag-Erling Smørgrav  * @param ctx: context.
253b7579f77SDag-Erling Smørgrav  * @param opt: option name from the unbound.conf config file format.
254b7579f77SDag-Erling Smørgrav  *	(not all settings applicable). The name includes the trailing ':'
255b7579f77SDag-Erling Smørgrav  *	for example ub_ctx_set_option(ctx, "logfile:", "mylog.txt");
256b7579f77SDag-Erling Smørgrav  * 	This is a power-users interface that lets you specify all sorts
257b7579f77SDag-Erling Smørgrav  * 	of options.
258b7579f77SDag-Erling Smørgrav  * 	For some specific options, such as adding trust anchors, special
259b7579f77SDag-Erling Smørgrav  * 	routines exist.
260b7579f77SDag-Erling Smørgrav  * @param val: value of the option.
261b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
262b7579f77SDag-Erling Smørgrav  */
2635d649f2dSDag-Erling Smørgrav int ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val);
264b7579f77SDag-Erling Smørgrav 
265b7579f77SDag-Erling Smørgrav /**
266b7579f77SDag-Erling Smørgrav  * Get an option from the context.
267b7579f77SDag-Erling Smørgrav  * @param ctx: context.
268b7579f77SDag-Erling Smørgrav  * @param opt: option name from the unbound.conf config file format.
269b7579f77SDag-Erling Smørgrav  *	(not all settings applicable). The name excludes the trailing ':'
270b7579f77SDag-Erling Smørgrav  *	for example ub_ctx_get_option(ctx, "logfile", &result);
271b7579f77SDag-Erling Smørgrav  * 	This is a power-users interface that lets you specify all sorts
272b7579f77SDag-Erling Smørgrav  * 	of options.
273b7579f77SDag-Erling Smørgrav  * @param str: the string is malloced and returned here. NULL on error.
274b7579f77SDag-Erling Smørgrav  * 	The caller must free() the string.  In cases with multiple
275b7579f77SDag-Erling Smørgrav  * 	entries (auto-trust-anchor-file), a newline delimited list is
276b7579f77SDag-Erling Smørgrav  * 	returned in the string.
277b7579f77SDag-Erling Smørgrav  * @return 0 if OK else an error code (malloc failure, syntax error).
278b7579f77SDag-Erling Smørgrav  */
2795d649f2dSDag-Erling Smørgrav int ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str);
280b7579f77SDag-Erling Smørgrav 
281b7579f77SDag-Erling Smørgrav /**
282b7579f77SDag-Erling Smørgrav  * setup configuration for the given context.
283b7579f77SDag-Erling Smørgrav  * @param ctx: context.
284b7579f77SDag-Erling Smørgrav  * @param fname: unbound config file (not all settings applicable).
285b7579f77SDag-Erling Smørgrav  * 	This is a power-users interface that lets you specify all sorts
286b7579f77SDag-Erling Smørgrav  * 	of options.
287b7579f77SDag-Erling Smørgrav  * 	For some specific options, such as adding trust anchors, special
288b7579f77SDag-Erling Smørgrav  * 	routines exist.
289b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
290b7579f77SDag-Erling Smørgrav  */
2915d649f2dSDag-Erling Smørgrav int ub_ctx_config(struct ub_ctx* ctx, const char* fname);
292b7579f77SDag-Erling Smørgrav 
293b7579f77SDag-Erling Smørgrav /**
294b7579f77SDag-Erling Smørgrav  * Set machine to forward DNS queries to, the caching resolver to use.
295b7579f77SDag-Erling Smørgrav  * IP4 or IP6 address. Forwards all DNS requests to that machine, which
296b7579f77SDag-Erling Smørgrav  * is expected to run a recursive resolver. If the proxy is not
297b7579f77SDag-Erling Smørgrav  * DNSSEC-capable, validation may fail. Can be called several times, in
298b7579f77SDag-Erling Smørgrav  * that case the addresses are used as backup servers.
299b7579f77SDag-Erling Smørgrav  *
300b7579f77SDag-Erling Smørgrav  * To read the list of nameservers from /etc/resolv.conf (from DHCP or so),
301b7579f77SDag-Erling Smørgrav  * use the call ub_ctx_resolvconf.
302b7579f77SDag-Erling Smørgrav  *
303b7579f77SDag-Erling Smørgrav  * @param ctx: context.
304b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
305b7579f77SDag-Erling Smørgrav  *	first resolve is done.
306b7579f77SDag-Erling Smørgrav  * @param addr: address, IP4 or IP6 in string format.
307b7579f77SDag-Erling Smørgrav  * 	If the addr is NULL, forwarding is disabled.
308b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
309b7579f77SDag-Erling Smørgrav  */
3105d649f2dSDag-Erling Smørgrav int ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr);
311b7579f77SDag-Erling Smørgrav 
312b7579f77SDag-Erling Smørgrav /**
313f61ef7f6SDag-Erling Smørgrav  * Add a stub zone, with given address to send to.  This is for custom
314f61ef7f6SDag-Erling Smørgrav  * root hints or pointing to a local authoritative dns server.
315f61ef7f6SDag-Erling Smørgrav  * For dns resolvers and the 'DHCP DNS' ip address, use ub_ctx_set_fwd.
316f61ef7f6SDag-Erling Smørgrav  * This is similar to a stub-zone entry in unbound.conf.
317f61ef7f6SDag-Erling Smørgrav  *
318f61ef7f6SDag-Erling Smørgrav  * @param ctx: context.
319f61ef7f6SDag-Erling Smørgrav  *	It is only possible to set configuration before the
320f61ef7f6SDag-Erling Smørgrav  *	first resolve is done.
321f61ef7f6SDag-Erling Smørgrav  * @param zone: name of the zone, string.
322f61ef7f6SDag-Erling Smørgrav  * @param addr: address, IP4 or IP6 in string format.
323f61ef7f6SDag-Erling Smørgrav  * 	The addr is added to the list of stub-addresses if the entry exists.
324f61ef7f6SDag-Erling Smørgrav  * 	If the addr is NULL the stub entry is removed.
325f61ef7f6SDag-Erling Smørgrav  * @param isprime: set to true to set stub-prime to yes for the stub.
326f61ef7f6SDag-Erling Smørgrav  * 	For local authoritative servers, people usually set it to false,
327f61ef7f6SDag-Erling Smørgrav  * 	For root hints it should be set to true.
328f61ef7f6SDag-Erling Smørgrav  * @return 0 if OK, else error.
329f61ef7f6SDag-Erling Smørgrav  */
330f61ef7f6SDag-Erling Smørgrav int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
331f61ef7f6SDag-Erling Smørgrav 	int isprime);
332f61ef7f6SDag-Erling Smørgrav 
333f61ef7f6SDag-Erling Smørgrav /**
334b7579f77SDag-Erling Smørgrav  * Read list of nameservers to use from the filename given.
335b7579f77SDag-Erling Smørgrav  * Usually "/etc/resolv.conf". Uses those nameservers as caching proxies.
336b7579f77SDag-Erling Smørgrav  * If they do not support DNSSEC, validation may fail.
337b7579f77SDag-Erling Smørgrav  *
338b7579f77SDag-Erling Smørgrav  * Only nameservers are picked up, the searchdomain, ndots and other
339b7579f77SDag-Erling Smørgrav  * settings from resolv.conf(5) are ignored.
340b7579f77SDag-Erling Smørgrav  *
341b7579f77SDag-Erling Smørgrav  * @param ctx: context.
342b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
343b7579f77SDag-Erling Smørgrav  *	first resolve is done.
344b7579f77SDag-Erling Smørgrav  * @param fname: file name string. If NULL "/etc/resolv.conf" is used.
345b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
346b7579f77SDag-Erling Smørgrav  */
3475d649f2dSDag-Erling Smørgrav int ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname);
348b7579f77SDag-Erling Smørgrav 
349b7579f77SDag-Erling Smørgrav /**
350b7579f77SDag-Erling Smørgrav  * Read list of hosts from the filename given.
351b7579f77SDag-Erling Smørgrav  * Usually "/etc/hosts".
352b7579f77SDag-Erling Smørgrav  * These addresses are not flagged as DNSSEC secure when queried for.
353b7579f77SDag-Erling Smørgrav  *
354b7579f77SDag-Erling Smørgrav  * @param ctx: context.
355b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to set configuration before the
356b7579f77SDag-Erling Smørgrav  *	first resolve is done.
357b7579f77SDag-Erling Smørgrav  * @param fname: file name string. If NULL "/etc/hosts" is used.
358b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
359b7579f77SDag-Erling Smørgrav  */
3605d649f2dSDag-Erling Smørgrav int ub_ctx_hosts(struct ub_ctx* ctx, const char* fname);
361b7579f77SDag-Erling Smørgrav 
362b7579f77SDag-Erling Smørgrav /**
363b7579f77SDag-Erling Smørgrav  * Add a trust anchor to the given context.
364b7579f77SDag-Erling Smørgrav  * The trust anchor is a string, on one line, that holds a valid DNSKEY or
365b7579f77SDag-Erling Smørgrav  * DS RR.
366b7579f77SDag-Erling Smørgrav  * @param ctx: context.
367b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
368b7579f77SDag-Erling Smørgrav  *	first resolve is done.
369b7579f77SDag-Erling Smørgrav  * @param ta: string, with zone-format RR on one line.
370b7579f77SDag-Erling Smørgrav  * 	[domainname] [TTL optional] [type] [class optional] [rdata contents]
371b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
372b7579f77SDag-Erling Smørgrav  */
3735d649f2dSDag-Erling Smørgrav int ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta);
374b7579f77SDag-Erling Smørgrav 
375b7579f77SDag-Erling Smørgrav /**
376b7579f77SDag-Erling Smørgrav  * Add trust anchors to the given context.
377b7579f77SDag-Erling Smørgrav  * Pass name of a file with DS and DNSKEY records (like from dig or drill).
378b7579f77SDag-Erling Smørgrav  * @param ctx: context.
379b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
380b7579f77SDag-Erling Smørgrav  *	first resolve is done.
381b7579f77SDag-Erling Smørgrav  * @param fname: filename of file with keyfile with trust anchors.
382b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
383b7579f77SDag-Erling Smørgrav  */
3845d649f2dSDag-Erling Smørgrav int ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname);
385b7579f77SDag-Erling Smørgrav 
386b7579f77SDag-Erling Smørgrav /**
387ff825849SDag-Erling Smørgrav  * Add trust anchor to the given context that is tracked with RFC5011
388ff825849SDag-Erling Smørgrav  * automated trust anchor maintenance.  The file is written to when the
389ff825849SDag-Erling Smørgrav  * trust anchor is changed.
390ff825849SDag-Erling Smørgrav  * Pass the name of a file that was output from eg. unbound-anchor,
391ff825849SDag-Erling Smørgrav  * or you can start it by providing a trusted DNSKEY or DS record on one
392ff825849SDag-Erling Smørgrav  * line in the file.
393ff825849SDag-Erling Smørgrav  * @param ctx: context.
394ff825849SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
395ff825849SDag-Erling Smørgrav  *	first resolve is done.
396ff825849SDag-Erling Smørgrav  * @param fname: filename of file with trust anchor.
397ff825849SDag-Erling Smørgrav  * @return 0 if OK, else error.
398ff825849SDag-Erling Smørgrav  */
399ff825849SDag-Erling Smørgrav int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname);
400ff825849SDag-Erling Smørgrav 
401ff825849SDag-Erling Smørgrav /**
402b7579f77SDag-Erling Smørgrav  * Add trust anchors to the given context.
403b7579f77SDag-Erling Smørgrav  * Pass the name of a bind-style config file with trusted-keys{}.
404b7579f77SDag-Erling Smørgrav  * @param ctx: context.
405b7579f77SDag-Erling Smørgrav  *	At this time it is only possible to add trusted keys before the
406b7579f77SDag-Erling Smørgrav  *	first resolve is done.
407b7579f77SDag-Erling Smørgrav  * @param fname: filename of file with bind-style config entries with trust
408b7579f77SDag-Erling Smørgrav  * 	anchors.
409b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
410b7579f77SDag-Erling Smørgrav  */
4115d649f2dSDag-Erling Smørgrav int ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname);
412b7579f77SDag-Erling Smørgrav 
413b7579f77SDag-Erling Smørgrav /**
414b7579f77SDag-Erling Smørgrav  * Set debug output (and error output) to the specified stream.
415b7579f77SDag-Erling Smørgrav  * Pass NULL to disable. Default is stderr.
416b7579f77SDag-Erling Smørgrav  * @param ctx: context.
417b7579f77SDag-Erling Smørgrav  * @param out: FILE* out file stream to log to.
418b7579f77SDag-Erling Smørgrav  * 	Type void* to avoid stdio dependency of this header file.
419b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
420b7579f77SDag-Erling Smørgrav  */
421b7579f77SDag-Erling Smørgrav int ub_ctx_debugout(struct ub_ctx* ctx, void* out);
422b7579f77SDag-Erling Smørgrav 
423b7579f77SDag-Erling Smørgrav /**
424b7579f77SDag-Erling Smørgrav  * Set debug verbosity for the context
425b7579f77SDag-Erling Smørgrav  * Output is directed to stderr.
426b7579f77SDag-Erling Smørgrav  * @param ctx: context.
427b7579f77SDag-Erling Smørgrav  * @param d: debug level, 0 is off, 1 is very minimal, 2 is detailed,
428b7579f77SDag-Erling Smørgrav  *	and 3 is lots.
429b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
430b7579f77SDag-Erling Smørgrav  */
431b7579f77SDag-Erling Smørgrav int ub_ctx_debuglevel(struct ub_ctx* ctx, int d);
432b7579f77SDag-Erling Smørgrav 
433b7579f77SDag-Erling Smørgrav /**
434b7579f77SDag-Erling Smørgrav  * Set a context behaviour for asynchronous action.
435b7579f77SDag-Erling Smørgrav  * @param ctx: context.
436b7579f77SDag-Erling Smørgrav  * @param dothread: if true, enables threading and a call to resolve_async()
437b7579f77SDag-Erling Smørgrav  *	creates a thread to handle work in the background.
438b7579f77SDag-Erling Smørgrav  *	If false, a process is forked to handle work in the background.
439b7579f77SDag-Erling Smørgrav  *	Changes to this setting after async() calls have been made have
440b7579f77SDag-Erling Smørgrav  *	no effect (delete and re-create the context to change).
441b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
442b7579f77SDag-Erling Smørgrav  */
443b7579f77SDag-Erling Smørgrav int ub_ctx_async(struct ub_ctx* ctx, int dothread);
444b7579f77SDag-Erling Smørgrav 
445b7579f77SDag-Erling Smørgrav /**
446b7579f77SDag-Erling Smørgrav  * Poll a context to see if it has any new results
447b7579f77SDag-Erling Smørgrav  * Do not poll in a loop, instead extract the fd below to poll for readiness,
448b7579f77SDag-Erling Smørgrav  * and then check, or wait using the wait routine.
449b7579f77SDag-Erling Smørgrav  * @param ctx: context.
450b7579f77SDag-Erling Smørgrav  * @return: 0 if nothing to read, or nonzero if a result is available.
451b7579f77SDag-Erling Smørgrav  * 	If nonzero, call ctx_process() to do callbacks.
452b7579f77SDag-Erling Smørgrav  */
453b7579f77SDag-Erling Smørgrav int ub_poll(struct ub_ctx* ctx);
454b7579f77SDag-Erling Smørgrav 
455b7579f77SDag-Erling Smørgrav /**
456b7579f77SDag-Erling Smørgrav  * Wait for a context to finish with results. Calls ub_process() after
457b7579f77SDag-Erling Smørgrav  * the wait for you. After the wait, there are no more outstanding
458b7579f77SDag-Erling Smørgrav  * asynchronous queries.
459b7579f77SDag-Erling Smørgrav  * @param ctx: context.
460b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
461b7579f77SDag-Erling Smørgrav  */
462b7579f77SDag-Erling Smørgrav int ub_wait(struct ub_ctx* ctx);
463b7579f77SDag-Erling Smørgrav 
464b7579f77SDag-Erling Smørgrav /**
465b7579f77SDag-Erling Smørgrav  * Get file descriptor. Wait for it to become readable, at this point
466b7579f77SDag-Erling Smørgrav  * answers are returned from the asynchronous validating resolver.
467b7579f77SDag-Erling Smørgrav  * Then call the ub_process to continue processing.
468b7579f77SDag-Erling Smørgrav  * This routine works immediately after context creation, the fd
469b7579f77SDag-Erling Smørgrav  * does not change.
470b7579f77SDag-Erling Smørgrav  * @param ctx: context.
471b7579f77SDag-Erling Smørgrav  * @return: -1 on error, or file descriptor to use select(2) with.
472b7579f77SDag-Erling Smørgrav  */
473b7579f77SDag-Erling Smørgrav int ub_fd(struct ub_ctx* ctx);
474b7579f77SDag-Erling Smørgrav 
475b7579f77SDag-Erling Smørgrav /**
476b7579f77SDag-Erling Smørgrav  * Call this routine to continue processing results from the validating
477b7579f77SDag-Erling Smørgrav  * resolver (when the fd becomes readable).
478b7579f77SDag-Erling Smørgrav  * Will perform necessary callbacks.
479b7579f77SDag-Erling Smørgrav  * @param ctx: context
480b7579f77SDag-Erling Smørgrav  * @return: 0 if OK, else error.
481b7579f77SDag-Erling Smørgrav  */
482b7579f77SDag-Erling Smørgrav int ub_process(struct ub_ctx* ctx);
483b7579f77SDag-Erling Smørgrav 
484b7579f77SDag-Erling Smørgrav /**
485b7579f77SDag-Erling Smørgrav  * Perform resolution and validation of the target name.
486b7579f77SDag-Erling Smørgrav  * @param ctx: context.
487b7579f77SDag-Erling Smørgrav  *	The context is finalized, and can no longer accept config changes.
488b7579f77SDag-Erling Smørgrav  * @param name: domain name in text format (a zero terminated text string).
489b7579f77SDag-Erling Smørgrav  * @param rrtype: type of RR in host order, 1 is A (address).
490b7579f77SDag-Erling Smørgrav  * @param rrclass: class of RR in host order, 1 is IN (for internet).
491b7579f77SDag-Erling Smørgrav  * @param result: the result data is returned in a newly allocated result
492b7579f77SDag-Erling Smørgrav  * 	structure. May be NULL on return, return value is set to an error
493b7579f77SDag-Erling Smørgrav  * 	in that case (out of memory).
494b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
495b7579f77SDag-Erling Smørgrav  */
4965d649f2dSDag-Erling Smørgrav int ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
497b7579f77SDag-Erling Smørgrav 	int rrclass, struct ub_result** result);
498b7579f77SDag-Erling Smørgrav 
499b7579f77SDag-Erling Smørgrav /**
500b7579f77SDag-Erling Smørgrav  * Perform resolution and validation of the target name.
501b7579f77SDag-Erling Smørgrav  * Asynchronous, after a while, the callback will be called with your
502b7579f77SDag-Erling Smørgrav  * data and the result.
503b7579f77SDag-Erling Smørgrav  * @param ctx: context.
504b7579f77SDag-Erling Smørgrav  *	If no thread or process has been created yet to perform the
505b7579f77SDag-Erling Smørgrav  *	work in the background, it is created now.
506b7579f77SDag-Erling Smørgrav  *	The context is finalized, and can no longer accept config changes.
507b7579f77SDag-Erling Smørgrav  * @param name: domain name in text format (a string).
508b7579f77SDag-Erling Smørgrav  * @param rrtype: type of RR in host order, 1 is A.
509b7579f77SDag-Erling Smørgrav  * @param rrclass: class of RR in host order, 1 is IN (for internet).
510b7579f77SDag-Erling Smørgrav  * @param mydata: this data is your own data (you can pass NULL),
511b7579f77SDag-Erling Smørgrav  * 	and is passed on to the callback function.
512b7579f77SDag-Erling Smørgrav  * @param callback: this is called on completion of the resolution.
513b7579f77SDag-Erling Smørgrav  * 	It is called as:
514b7579f77SDag-Erling Smørgrav  * 	void callback(void* mydata, int err, struct ub_result* result)
515b7579f77SDag-Erling Smørgrav  * 	with mydata: the same as passed here, you may pass NULL,
516b7579f77SDag-Erling Smørgrav  * 	with err: is 0 when a result has been found.
517b7579f77SDag-Erling Smørgrav  * 	with result: a newly allocated result structure.
518b7579f77SDag-Erling Smørgrav  *		The result may be NULL, in that case err is set.
519b7579f77SDag-Erling Smørgrav  *
520b7579f77SDag-Erling Smørgrav  * 	If an error happens during processing, your callback will be called
521b7579f77SDag-Erling Smørgrav  * 	with error set to a nonzero value (and result==NULL).
522b7579f77SDag-Erling Smørgrav  * @param async_id: if you pass a non-NULL value, an identifier number is
523b7579f77SDag-Erling Smørgrav  *	returned for the query as it is in progress. It can be used to
524b7579f77SDag-Erling Smørgrav  *	cancel the query.
525b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
526b7579f77SDag-Erling Smørgrav  */
5275d649f2dSDag-Erling Smørgrav int ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
5283005e0a3SDag-Erling Smørgrav 	int rrclass, void* mydata, ub_callback_type callback, int* async_id);
529b7579f77SDag-Erling Smørgrav 
530b7579f77SDag-Erling Smørgrav /**
531b7579f77SDag-Erling Smørgrav  * Cancel an async query in progress.
532b7579f77SDag-Erling Smørgrav  * Its callback will not be called.
533b7579f77SDag-Erling Smørgrav  *
534b7579f77SDag-Erling Smørgrav  * @param ctx: context.
535b7579f77SDag-Erling Smørgrav  * @param async_id: which query to cancel.
536b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
537b7579f77SDag-Erling Smørgrav  * This routine can return an error if the async_id passed does not exist
538b7579f77SDag-Erling Smørgrav  * or has already been delivered. If another thread is processing results
539b7579f77SDag-Erling Smørgrav  * at the same time, the result may be delivered at the same time and the
540b7579f77SDag-Erling Smørgrav  * cancel fails with an error.  Also the cancel can fail due to a system
541b7579f77SDag-Erling Smørgrav  * error, no memory or socket failures.
542b7579f77SDag-Erling Smørgrav  */
543b7579f77SDag-Erling Smørgrav int ub_cancel(struct ub_ctx* ctx, int async_id);
544b7579f77SDag-Erling Smørgrav 
545b7579f77SDag-Erling Smørgrav /**
546b7579f77SDag-Erling Smørgrav  * Free storage associated with a result structure.
547b7579f77SDag-Erling Smørgrav  * @param result: to free
548b7579f77SDag-Erling Smørgrav  */
549b7579f77SDag-Erling Smørgrav void ub_resolve_free(struct ub_result* result);
550b7579f77SDag-Erling Smørgrav 
551b7579f77SDag-Erling Smørgrav /**
552b7579f77SDag-Erling Smørgrav  * Convert error value to a human readable string.
553ff825849SDag-Erling Smørgrav  * @param err: error code from one of the libunbound functions.
554b7579f77SDag-Erling Smørgrav  * @return pointer to constant text string, zero terminated.
555b7579f77SDag-Erling Smørgrav  */
556b7579f77SDag-Erling Smørgrav const char* ub_strerror(int err);
557b7579f77SDag-Erling Smørgrav 
558b7579f77SDag-Erling Smørgrav /**
559b7579f77SDag-Erling Smørgrav  * Debug routine.  Print the local zone information to debug output.
560b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
561b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
562b7579f77SDag-Erling Smørgrav  */
563b7579f77SDag-Erling Smørgrav int ub_ctx_print_local_zones(struct ub_ctx* ctx);
564b7579f77SDag-Erling Smørgrav 
565b7579f77SDag-Erling Smørgrav /**
566b7579f77SDag-Erling Smørgrav  * Add a new zone with the zonetype to the local authority info of the
567b7579f77SDag-Erling Smørgrav  * library.
568b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
569b7579f77SDag-Erling Smørgrav  * @param zone_name: name of the zone in text, "example.com"
570b7579f77SDag-Erling Smørgrav  *	If it already exists, the type is updated.
571b7579f77SDag-Erling Smørgrav  * @param zone_type: type of the zone (like for unbound.conf) in text.
572b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
573b7579f77SDag-Erling Smørgrav  */
57417d15b25SDag-Erling Smørgrav int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
57517d15b25SDag-Erling Smørgrav 	const char *zone_type);
576b7579f77SDag-Erling Smørgrav 
577b7579f77SDag-Erling Smørgrav /**
578b7579f77SDag-Erling Smørgrav  * Remove zone from local authority info of the library.
579b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
580b7579f77SDag-Erling Smørgrav  * @param zone_name: name of the zone in text, "example.com"
581b7579f77SDag-Erling Smørgrav  *	If it does not exist, nothing happens.
582b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
583b7579f77SDag-Erling Smørgrav  */
58417d15b25SDag-Erling Smørgrav int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name);
585b7579f77SDag-Erling Smørgrav 
586b7579f77SDag-Erling Smørgrav /**
587b7579f77SDag-Erling Smørgrav  * Add localdata to the library local authority info.
588b7579f77SDag-Erling Smørgrav  * Similar to local-data config statement.
589b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
590b7579f77SDag-Erling Smørgrav  * @param data: the resource record in text format, for example
591b7579f77SDag-Erling Smørgrav  *	"www.example.com IN A 127.0.0.1"
592b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
593b7579f77SDag-Erling Smørgrav  */
59417d15b25SDag-Erling Smørgrav int ub_ctx_data_add(struct ub_ctx* ctx, const char *data);
595b7579f77SDag-Erling Smørgrav 
596b7579f77SDag-Erling Smørgrav /**
597b7579f77SDag-Erling Smørgrav  * Remove localdata from the library local authority info.
598b7579f77SDag-Erling Smørgrav  * @param ctx: context.  Is finalized by the routine.
599b7579f77SDag-Erling Smørgrav  * @param data: the name to delete all data from, like "www.example.com".
600b7579f77SDag-Erling Smørgrav  * @return 0 if OK, else error.
601b7579f77SDag-Erling Smørgrav  */
60217d15b25SDag-Erling Smørgrav int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data);
603b7579f77SDag-Erling Smørgrav 
604b7579f77SDag-Erling Smørgrav /**
605b7579f77SDag-Erling Smørgrav  * Get a version string from the libunbound implementation.
606b7579f77SDag-Erling Smørgrav  * @return a static constant string with the version number.
607b7579f77SDag-Erling Smørgrav  */
608b7579f77SDag-Erling Smørgrav const char* ub_version(void);
609b7579f77SDag-Erling Smørgrav 
610c7f4d7adSDag-Erling Smørgrav /**
611c7f4d7adSDag-Erling Smørgrav  * Some global statistics that are not in struct stats_info,
612c7f4d7adSDag-Erling Smørgrav  * this struct is shared on a shm segment (shm-key in unbound.conf)
613c7f4d7adSDag-Erling Smørgrav  */
614c7f4d7adSDag-Erling Smørgrav struct ub_shm_stat_info {
615c7f4d7adSDag-Erling Smørgrav 	int num_threads;
616c7f4d7adSDag-Erling Smørgrav 
617c7f4d7adSDag-Erling Smørgrav 	struct {
618c7f4d7adSDag-Erling Smørgrav 		long long now_sec, now_usec;
619c7f4d7adSDag-Erling Smørgrav 		long long up_sec, up_usec;
620c7f4d7adSDag-Erling Smørgrav 		long long elapsed_sec, elapsed_usec;
621c7f4d7adSDag-Erling Smørgrav 	} time;
622c7f4d7adSDag-Erling Smørgrav 
623c7f4d7adSDag-Erling Smørgrav 	struct {
624c7f4d7adSDag-Erling Smørgrav 		long long msg;
625c7f4d7adSDag-Erling Smørgrav 		long long rrset;
626c7f4d7adSDag-Erling Smørgrav 		long long val;
627c7f4d7adSDag-Erling Smørgrav 		long long iter;
628c7f4d7adSDag-Erling Smørgrav 		long long subnet;
629c7f4d7adSDag-Erling Smørgrav 		long long ipsecmod;
630c7f4d7adSDag-Erling Smørgrav 		long long respip;
631971980c3SDag-Erling Smørgrav 		long long dnscrypt_shared_secret;
6328a384985SDag-Erling Smørgrav 		long long dnscrypt_nonce;
633c7f4d7adSDag-Erling Smørgrav 	} mem;
634c7f4d7adSDag-Erling Smørgrav };
635c7f4d7adSDag-Erling Smørgrav 
636c7f4d7adSDag-Erling Smørgrav /** number of qtype that is stored for in array */
637c7f4d7adSDag-Erling Smørgrav #define UB_STATS_QTYPE_NUM 256
638c7f4d7adSDag-Erling Smørgrav /** number of qclass that is stored for in array */
639c7f4d7adSDag-Erling Smørgrav #define UB_STATS_QCLASS_NUM 256
640c7f4d7adSDag-Erling Smørgrav /** number of rcodes in stats */
641c7f4d7adSDag-Erling Smørgrav #define UB_STATS_RCODE_NUM 16
642c7f4d7adSDag-Erling Smørgrav /** number of opcodes in stats */
643c7f4d7adSDag-Erling Smørgrav #define UB_STATS_OPCODE_NUM 16
644c7f4d7adSDag-Erling Smørgrav /** number of histogram buckets */
645c7f4d7adSDag-Erling Smørgrav #define UB_STATS_BUCKET_NUM 40
646c7f4d7adSDag-Erling Smørgrav 
647c7f4d7adSDag-Erling Smørgrav /** per worker statistics. */
648c7f4d7adSDag-Erling Smørgrav struct ub_server_stats {
649c7f4d7adSDag-Erling Smørgrav 	/** number of queries from clients received. */
650c7f4d7adSDag-Erling Smørgrav 	long long num_queries;
651c7f4d7adSDag-Erling Smørgrav 	/** number of queries that have been dropped/ratelimited by ip. */
652c7f4d7adSDag-Erling Smørgrav 	long long num_queries_ip_ratelimited;
653c7f4d7adSDag-Erling Smørgrav 	/** number of queries that had a cache-miss. */
654c7f4d7adSDag-Erling Smørgrav 	long long num_queries_missed_cache;
655c7f4d7adSDag-Erling Smørgrav 	/** number of prefetch queries - cachehits with prefetch */
656c7f4d7adSDag-Erling Smørgrav 	long long num_queries_prefetch;
657c7f4d7adSDag-Erling Smørgrav 
658c7f4d7adSDag-Erling Smørgrav 	/**
659c7f4d7adSDag-Erling Smørgrav 	 * Sum of the querylistsize of the worker for
660c7f4d7adSDag-Erling Smørgrav 	 * every query that missed cache. To calculate average.
661c7f4d7adSDag-Erling Smørgrav 	 */
662c7f4d7adSDag-Erling Smørgrav 	long long sum_query_list_size;
663c7f4d7adSDag-Erling Smørgrav 	/** max value of query list size reached. */
664c7f4d7adSDag-Erling Smørgrav 	long long max_query_list_size;
665c7f4d7adSDag-Erling Smørgrav 
666c7f4d7adSDag-Erling Smørgrav 	/** Extended stats below (bool) */
667c7f4d7adSDag-Erling Smørgrav 	int extended;
668c7f4d7adSDag-Erling Smørgrav 
669c7f4d7adSDag-Erling Smørgrav 	/** qtype stats */
670c7f4d7adSDag-Erling Smørgrav 	long long qtype[UB_STATS_QTYPE_NUM];
671c7f4d7adSDag-Erling Smørgrav 	/** bigger qtype values not in array */
672c7f4d7adSDag-Erling Smørgrav 	long long qtype_big;
673c7f4d7adSDag-Erling Smørgrav 	/** qclass stats */
674c7f4d7adSDag-Erling Smørgrav 	long long qclass[UB_STATS_QCLASS_NUM];
675c7f4d7adSDag-Erling Smørgrav 	/** bigger qclass values not in array */
676c7f4d7adSDag-Erling Smørgrav 	long long qclass_big;
677c7f4d7adSDag-Erling Smørgrav 	/** query opcodes */
678c7f4d7adSDag-Erling Smørgrav 	long long qopcode[UB_STATS_OPCODE_NUM];
679c7f4d7adSDag-Erling Smørgrav 	/** number of queries over TCP */
680c7f4d7adSDag-Erling Smørgrav 	long long qtcp;
681c7f4d7adSDag-Erling Smørgrav 	/** number of outgoing queries over TCP */
682c7f4d7adSDag-Erling Smørgrav 	long long qtcp_outgoing;
683*4c75e3aaSDag-Erling Smørgrav 	/** number of queries over (DNS over) TLS */
684*4c75e3aaSDag-Erling Smørgrav 	long long qtls;
685c7f4d7adSDag-Erling Smørgrav 	/** number of queries over IPv6 */
686c7f4d7adSDag-Erling Smørgrav 	long long qipv6;
687c7f4d7adSDag-Erling Smørgrav 	/** number of queries with QR bit */
688c7f4d7adSDag-Erling Smørgrav 	long long qbit_QR;
689c7f4d7adSDag-Erling Smørgrav 	/** number of queries with AA bit */
690c7f4d7adSDag-Erling Smørgrav 	long long qbit_AA;
691c7f4d7adSDag-Erling Smørgrav 	/** number of queries with TC bit */
692c7f4d7adSDag-Erling Smørgrav 	long long qbit_TC;
693c7f4d7adSDag-Erling Smørgrav 	/** number of queries with RD bit */
694c7f4d7adSDag-Erling Smørgrav 	long long qbit_RD;
695c7f4d7adSDag-Erling Smørgrav 	/** number of queries with RA bit */
696c7f4d7adSDag-Erling Smørgrav 	long long qbit_RA;
697c7f4d7adSDag-Erling Smørgrav 	/** number of queries with Z bit */
698c7f4d7adSDag-Erling Smørgrav 	long long qbit_Z;
699c7f4d7adSDag-Erling Smørgrav 	/** number of queries with AD bit */
700c7f4d7adSDag-Erling Smørgrav 	long long qbit_AD;
701c7f4d7adSDag-Erling Smørgrav 	/** number of queries with CD bit */
702c7f4d7adSDag-Erling Smørgrav 	long long qbit_CD;
703c7f4d7adSDag-Erling Smørgrav 	/** number of queries with EDNS OPT record */
704c7f4d7adSDag-Erling Smørgrav 	long long qEDNS;
705c7f4d7adSDag-Erling Smørgrav 	/** number of queries with EDNS with DO flag */
706c7f4d7adSDag-Erling Smørgrav 	long long qEDNS_DO;
707c7f4d7adSDag-Erling Smørgrav 	/** answer rcodes */
708c7f4d7adSDag-Erling Smørgrav 	long long ans_rcode[UB_STATS_RCODE_NUM];
709c7f4d7adSDag-Erling Smørgrav 	/** answers with pseudo rcode 'nodata' */
710c7f4d7adSDag-Erling Smørgrav 	long long ans_rcode_nodata;
711c7f4d7adSDag-Erling Smørgrav 	/** answers that were secure (AD) */
712c7f4d7adSDag-Erling Smørgrav 	long long ans_secure;
713c7f4d7adSDag-Erling Smørgrav 	/** answers that were bogus (withheld as SERVFAIL) */
714c7f4d7adSDag-Erling Smørgrav 	long long ans_bogus;
715c7f4d7adSDag-Erling Smørgrav 	/** rrsets marked bogus by validator */
716c7f4d7adSDag-Erling Smørgrav 	long long rrset_bogus;
717971980c3SDag-Erling Smørgrav 	/** number of queries that have been ratelimited by domain recursion. */
718971980c3SDag-Erling Smørgrav 	long long queries_ratelimited;
719c7f4d7adSDag-Erling Smørgrav 	/** unwanted traffic received on server-facing ports */
720c7f4d7adSDag-Erling Smørgrav 	long long unwanted_replies;
721c7f4d7adSDag-Erling Smørgrav 	/** unwanted traffic received on client-facing ports */
722c7f4d7adSDag-Erling Smørgrav 	long long unwanted_queries;
723c7f4d7adSDag-Erling Smørgrav 	/** usage of tcp accept list */
724c7f4d7adSDag-Erling Smørgrav 	long long tcp_accept_usage;
725c7f4d7adSDag-Erling Smørgrav 	/** answers served from expired cache */
726c7f4d7adSDag-Erling Smørgrav 	long long zero_ttl_responses;
727c7f4d7adSDag-Erling Smørgrav 	/** histogram data exported to array
728c7f4d7adSDag-Erling Smørgrav 	 * if the array is the same size, no data is lost, and
729c7f4d7adSDag-Erling Smørgrav 	 * if all histograms are same size (is so by default) then
730c7f4d7adSDag-Erling Smørgrav 	 * adding up works well. */
731c7f4d7adSDag-Erling Smørgrav 	long long hist[UB_STATS_BUCKET_NUM];
732c7f4d7adSDag-Erling Smørgrav 
733c7f4d7adSDag-Erling Smørgrav 	/** number of message cache entries */
734c7f4d7adSDag-Erling Smørgrav 	long long msg_cache_count;
735c7f4d7adSDag-Erling Smørgrav 	/** number of rrset cache entries */
736c7f4d7adSDag-Erling Smørgrav 	long long rrset_cache_count;
737c7f4d7adSDag-Erling Smørgrav 	/** number of infra cache entries */
738c7f4d7adSDag-Erling Smørgrav 	long long infra_cache_count;
739c7f4d7adSDag-Erling Smørgrav 	/** number of key cache entries */
740c7f4d7adSDag-Erling Smørgrav 	long long key_cache_count;
741c7f4d7adSDag-Erling Smørgrav 
742c7f4d7adSDag-Erling Smørgrav 	/** number of queries that used dnscrypt */
743c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_crypted;
744c7f4d7adSDag-Erling Smørgrav 	/** number of queries that queried dnscrypt certificates */
745c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_cert;
746c7f4d7adSDag-Erling Smørgrav 	/** number of queries in clear text and not asking for the certificates */
747c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_cleartext;
748c7f4d7adSDag-Erling Smørgrav 	/** number of malformed encrypted queries */
749c7f4d7adSDag-Erling Smørgrav 	long long num_query_dnscrypt_crypted_malformed;
750971980c3SDag-Erling Smørgrav 	/** number of queries which did not have a shared secret in cache */
751971980c3SDag-Erling Smørgrav 	long long num_query_dnscrypt_secret_missed_cache;
752971980c3SDag-Erling Smørgrav 	/** number of dnscrypt shared secret cache entries */
753971980c3SDag-Erling Smørgrav 	long long shared_secret_cache_count;
7548a384985SDag-Erling Smørgrav 	/** number of queries which are replays */
7558a384985SDag-Erling Smørgrav 	long long num_query_dnscrypt_replay;
7568a384985SDag-Erling Smørgrav 	/** number of dnscrypt nonces cache entries */
7578a384985SDag-Erling Smørgrav 	long long nonce_cache_count;
7580fb34990SDag-Erling Smørgrav 	/** number of queries for unbound's auth_zones, upstream query */
7590fb34990SDag-Erling Smørgrav 	long long num_query_authzone_up;
7600fb34990SDag-Erling Smørgrav 	/** number of queries for unbound's auth_zones, downstream answers */
7610fb34990SDag-Erling Smørgrav 	long long num_query_authzone_down;
7620fb34990SDag-Erling Smørgrav 	/** number of times neg cache records were used to generate NOERROR
7630fb34990SDag-Erling Smørgrav 	 * responses. */
7640fb34990SDag-Erling Smørgrav 	long long num_neg_cache_noerror;
7650fb34990SDag-Erling Smørgrav 	/** number of times neg cache records were used to generate NXDOMAIN
7660fb34990SDag-Erling Smørgrav 	 * responses. */
7670fb34990SDag-Erling Smørgrav 	long long num_neg_cache_nxdomain;
768*4c75e3aaSDag-Erling Smørgrav 	/** number of queries answered from edns-subnet specific data */
769*4c75e3aaSDag-Erling Smørgrav 	long long num_query_subnet;
770*4c75e3aaSDag-Erling Smørgrav 	/** number of queries answered from edns-subnet specific data, and
771*4c75e3aaSDag-Erling Smørgrav 	 * the answer was from the edns-subnet cache. */
772*4c75e3aaSDag-Erling Smørgrav 	long long num_query_subnet_cache;
773c7f4d7adSDag-Erling Smørgrav };
774c7f4d7adSDag-Erling Smørgrav 
775c7f4d7adSDag-Erling Smørgrav /**
776c7f4d7adSDag-Erling Smørgrav  * Statistics to send over the control pipe when asked
7778a384985SDag-Erling Smørgrav  * This struct is made to be memcopied, sent in binary.
778c7f4d7adSDag-Erling Smørgrav  * shm mapped with (number+1) at num_threads+1, with first as total
779c7f4d7adSDag-Erling Smørgrav  */
780c7f4d7adSDag-Erling Smørgrav struct ub_stats_info {
781c7f4d7adSDag-Erling Smørgrav 	/** the thread stats */
782c7f4d7adSDag-Erling Smørgrav 	struct ub_server_stats svr;
783c7f4d7adSDag-Erling Smørgrav 
784c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: current number of states */
785c7f4d7adSDag-Erling Smørgrav 	long long mesh_num_states;
786c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: current number of reply (user) states */
787c7f4d7adSDag-Erling Smørgrav 	long long mesh_num_reply_states;
788c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: number of reply states overwritten with a new one */
789c7f4d7adSDag-Erling Smørgrav 	long long mesh_jostled;
790c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: number of incoming queries dropped */
791c7f4d7adSDag-Erling Smørgrav 	long long mesh_dropped;
792c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: replies sent */
793c7f4d7adSDag-Erling Smørgrav 	long long mesh_replies_sent;
794c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: sum of waiting times for the replies */
795c7f4d7adSDag-Erling Smørgrav 	long long mesh_replies_sum_wait_sec, mesh_replies_sum_wait_usec;
796c7f4d7adSDag-Erling Smørgrav 	/** mesh stats: median of waiting times for replies (in sec) */
797c7f4d7adSDag-Erling Smørgrav 	double mesh_time_median;
798c7f4d7adSDag-Erling Smørgrav };
799c7f4d7adSDag-Erling Smørgrav 
800b7579f77SDag-Erling Smørgrav #ifdef __cplusplus
801b7579f77SDag-Erling Smørgrav }
802b7579f77SDag-Erling Smørgrav #endif
803b7579f77SDag-Erling Smørgrav 
804b7579f77SDag-Erling Smørgrav #endif /* _UB_UNBOUND_H */
805