1 /*
2  * unbound.h - unbound validating resolver public API
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to resolve DNS queries and
40  * validate the answers. Synchronously and asynchronously.
41  *
42  * Several ways to use this interface from an application wishing
43  * to perform (validated) DNS lookups.
44  *
45  * All start with
46  *	ctx = ub_ctx_create();
47  *	err = ub_ctx_add_ta(ctx, "...");
48  *	err = ub_ctx_add_ta(ctx, "...");
49  *	... some lookups
50  *	... call ub_ctx_delete(ctx); when you want to stop.
51  *
52  * Application not threaded. Blocking.
53  *	int err = ub_resolve(ctx, "www.example.com", ...
54  *	if(err) fprintf(stderr, "lookup error: %s\n", ub_strerror(err));
55  *	... use the answer
56  *
57  * Application not threaded. Non-blocking ('asynchronous').
58  *      err = ub_resolve_async(ctx, "www.example.com", ... my_callback);
59  *	... application resumes processing ...
60  *	... and when either ub_poll(ctx) is true
61  *	... or when the file descriptor ub_fd(ctx) is readable,
62  *	... or whenever, the app calls ...
63  *	ub_process(ctx);
64  *	... if no result is ready, the app resumes processing above,
65  *	... or process() calls my_callback() with results.
66  *
67  *      ... if the application has nothing more to do, wait for answer
68  *      ub_wait(ctx);
69  *
70  * Application threaded. Blocking.
71  *	Blocking, same as above. The current thread does the work.
72  *	Multiple threads can use the *same context*, each does work and uses
73  *	shared cache data from the context.
74  *
75  * Application threaded. Non-blocking ('asynchronous').
76  *	... setup threaded-asynchronous config option
77  *	err = ub_ctx_async(ctx, 1);
78  *	... same as async for non-threaded
79  *	... the callbacks are called in the thread that calls process(ctx)
80  *
81  * Openssl needs to have locking in place, and the application must set
82  * it up, because a mere library cannot do this, use the calls
83  * CRYPTO_set_id_callback and CRYPTO_set_locking_callback.
84  *
85  * If no threading is compiled in, the above async example uses fork(2) to
86  * create a process to perform the work. The forked process exits when the
87  * calling process exits, or ctx_delete() is called.
88  * Otherwise, for asynchronous with threading, a worker thread is created.
89  *
90  * The blocking calls use shared ctx-cache when threaded. Thus
91  * ub_resolve() and ub_resolve_async() && ub_wait() are
92  * not the same. The first makes the current thread do the work, setting
93  * up buffers, etc, to perform the work (but using shared cache data).
94  * The second calls another worker thread (or process) to perform the work.
95  * And no buffers need to be set up, but a context-switch happens.
96  */
97 #ifndef _UB_UNBOUND_H
98 #define _UB_UNBOUND_H
99 
100 #ifdef __cplusplus
101 extern "C" {
102 #endif
103 
104 /** the version of this header file */
105 #define UNBOUND_VERSION_MAJOR @UNBOUND_VERSION_MAJOR@
106 #define UNBOUND_VERSION_MINOR @UNBOUND_VERSION_MINOR@
107 #define UNBOUND_VERSION_MICRO @UNBOUND_VERSION_MICRO@
108 
109 /**
110  * The validation context is created to hold the resolver status,
111  * validation keys and a small cache (containing messages, rrsets,
112  * roundtrip times, trusted keys, lameness information).
113  *
114  * Its contents are internally defined.
115  */
116 struct ub_ctx;
117 
118 /**
119  * The validation and resolution results.
120  * Allocated by the resolver, and need to be freed by the application
121  * with ub_resolve_free().
122  */
123 struct ub_result {
124 	/** The original question, name text string. */
125 	char* qname;
126 	/** the type asked for */
127 	int qtype;
128 	/** the class asked for */
129 	int qclass;
130 
131 	/**
132 	 * a list of network order DNS rdata items, terminated with a
133 	 * NULL pointer, so that data[0] is the first result entry,
134 	 * data[1] the second, and the last entry is NULL.
135 	 * If there was no data, data[0] is NULL.
136 	 */
137 	char** data;
138 
139 	/** the length in bytes of the data items, len[i] for data[i] */
140 	int* len;
141 
142 	/**
143 	 * canonical name for the result (the final cname).
144 	 * zero terminated string.
145 	 * May be NULL if no canonical name exists.
146 	 */
147 	char* canonname;
148 
149 	/**
150 	 * DNS RCODE for the result. May contain additional error code if
151 	 * there was no data due to an error. 0 (NOERROR) if okay.
152 	 */
153 	int rcode;
154 
155 	/**
156 	 * The DNS answer packet. Network formatted. Can contain DNSSEC types.
157 	 */
158 	void* answer_packet;
159 	/** length of the answer packet in octets. */
160 	int answer_len;
161 
162 	/**
163 	 * If there is any data, this is true.
164 	 * If false, there was no data (nxdomain may be true, rcode can be set).
165 	 */
166 	int havedata;
167 
168 	/**
169 	 * If there was no data, and the domain did not exist, this is true.
170 	 * If it is false, and there was no data, then the domain name
171 	 * is purported to exist, but the requested data type is not available.
172 	 */
173 	int nxdomain;
174 
175 	/**
176 	 * True, if the result is validated securely.
177 	 * False, if validation failed or domain queried has no security info.
178 	 *
179 	 * It is possible to get a result with no data (havedata is false),
180 	 * and secure is true. This means that the non-existence of the data
181 	 * was cryptographically proven (with signatures).
182 	 */
183 	int secure;
184 
185 	/**
186 	 * If the result was not secure (secure==0), and this result is due
187 	 * to a security failure, bogus is true.
188 	 * This means the data has been actively tampered with, signatures
189 	 * failed, expected signatures were not present, timestamps on
190 	 * signatures were out of date and so on.
191 	 *
192 	 * If !secure and !bogus, this can happen if the data is not secure
193 	 * because security is disabled for that domain name.
194 	 * This means the data is from a domain where data is not signed.
195 	 */
196 	int bogus;
197 
198 	/**
199 	 * If the result is bogus this contains a string (zero terminated)
200 	 * that describes the failure.  There may be other errors as well
201 	 * as the one described, the description may not be perfectly accurate.
202 	 * Is NULL if the result is not bogus.
203 	 */
204 	char* why_bogus;
205 
206 	/**
207 	 * If the query or one of its subqueries was ratelimited. Useful if
208 	 * ratelimiting is enabled and answer is SERVFAIL.
209 	 */
210 	int was_ratelimited;
211 
212 	/**
213 	 * TTL for the result, in seconds.  If the security is bogus, then
214 	 * you also cannot trust this value.
215 	 */
216 	int ttl;
217 };
218 
219 /**
220  * Callback for results of async queries.
221  * The readable function definition looks like:
222  * void my_callback(void* my_arg, int err, struct ub_result* result);
223  * It is called with
224  *	void* my_arg: your pointer to a (struct of) data of your choice,
225  *		or NULL.
226  *	int err: if 0 all is OK, otherwise an error occured and no results
227  *	     are forthcoming.
228  *	struct result: pointer to more detailed result structure.
229  *		This structure is allocated on the heap and needs to be
230  *		freed with ub_resolve_free(result);
231  */
232 typedef void (*ub_callback_type)(void*, int, struct ub_result*);
233 
234 /**
235  * Create a resolving and validation context.
236  * The information from /etc/resolv.conf and /etc/hosts is not utilised by
237  * default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them.
238  * @return a new context. default initialisation.
239  * 	returns NULL on error.
240  */
241 struct ub_ctx* ub_ctx_create(void);
242 
243 /**
244  * Destroy a validation context and free all its resources.
245  * Outstanding async queries are killed and callbacks are not called for them.
246  * @param ctx: context to delete.
247  */
248 void ub_ctx_delete(struct ub_ctx* ctx);
249 
250 /**
251  * Set an option for the context.
252  * @param ctx: context.
253  * @param opt: option name from the unbound.conf config file format.
254  *	(not all settings applicable). The name includes the trailing ':'
255  *	for example ub_ctx_set_option(ctx, "logfile:", "mylog.txt");
256  * 	This is a power-users interface that lets you specify all sorts
257  * 	of options.
258  * 	For some specific options, such as adding trust anchors, special
259  * 	routines exist.
260  * @param val: value of the option.
261  * @return: 0 if OK, else error.
262  */
263 int ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val);
264 
265 /**
266  * Get an option from the context.
267  * @param ctx: context.
268  * @param opt: option name from the unbound.conf config file format.
269  *	(not all settings applicable). The name excludes the trailing ':'
270  *	for example ub_ctx_get_option(ctx, "logfile", &result);
271  * 	This is a power-users interface that lets you specify all sorts
272  * 	of options.
273  * @param str: the string is malloced and returned here. NULL on error.
274  * 	The caller must free() the string.  In cases with multiple
275  * 	entries (auto-trust-anchor-file), a newline delimited list is
276  * 	returned in the string.
277  * @return 0 if OK else an error code (malloc failure, syntax error).
278  */
279 int ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str);
280 
281 /**
282  * setup configuration for the given context.
283  * @param ctx: context.
284  * @param fname: unbound config file (not all settings applicable).
285  * 	This is a power-users interface that lets you specify all sorts
286  * 	of options.
287  * 	For some specific options, such as adding trust anchors, special
288  * 	routines exist.
289  * @return: 0 if OK, else error.
290  */
291 int ub_ctx_config(struct ub_ctx* ctx, const char* fname);
292 
293 /**
294  * Set machine to forward DNS queries to, the caching resolver to use.
295  * IP4 or IP6 address. Forwards all DNS requests to that machine, which
296  * is expected to run a recursive resolver. If the proxy is not
297  * DNSSEC-capable, validation may fail. Can be called several times, in
298  * that case the addresses are used as backup servers.
299  *
300  * To read the list of nameservers from /etc/resolv.conf (from DHCP or so),
301  * use the call ub_ctx_resolvconf.
302  *
303  * @param ctx: context.
304  *	At this time it is only possible to set configuration before the
305  *	first resolve is done.
306  * @param addr: address, IP4 or IP6 in string format.
307  * 	If the addr is NULL, forwarding is disabled.
308  * @return 0 if OK, else error.
309  */
310 int ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr);
311 
312 /**
313  * Add a stub zone, with given address to send to.  This is for custom
314  * root hints or pointing to a local authoritative dns server.
315  * For dns resolvers and the 'DHCP DNS' ip address, use ub_ctx_set_fwd.
316  * This is similar to a stub-zone entry in unbound.conf.
317  *
318  * @param ctx: context.
319  *	It is only possible to set configuration before the
320  *	first resolve is done.
321  * @param zone: name of the zone, string.
322  * @param addr: address, IP4 or IP6 in string format.
323  * 	The addr is added to the list of stub-addresses if the entry exists.
324  * 	If the addr is NULL the stub entry is removed.
325  * @param isprime: set to true to set stub-prime to yes for the stub.
326  * 	For local authoritative servers, people usually set it to false,
327  * 	For root hints it should be set to true.
328  * @return 0 if OK, else error.
329  */
330 int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
331 	int isprime);
332 
333 /**
334  * Read list of nameservers to use from the filename given.
335  * Usually "/etc/resolv.conf". Uses those nameservers as caching proxies.
336  * If they do not support DNSSEC, validation may fail.
337  *
338  * Only nameservers are picked up, the searchdomain, ndots and other
339  * settings from resolv.conf(5) are ignored.
340  *
341  * @param ctx: context.
342  *	At this time it is only possible to set configuration before the
343  *	first resolve is done.
344  * @param fname: file name string. If NULL "/etc/resolv.conf" is used.
345  * @return 0 if OK, else error.
346  */
347 int ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname);
348 
349 /**
350  * Read list of hosts from the filename given.
351  * Usually "/etc/hosts".
352  * These addresses are not flagged as DNSSEC secure when queried for.
353  *
354  * @param ctx: context.
355  *	At this time it is only possible to set configuration before the
356  *	first resolve is done.
357  * @param fname: file name string. If NULL "/etc/hosts" is used.
358  * @return 0 if OK, else error.
359  */
360 int ub_ctx_hosts(struct ub_ctx* ctx, const char* fname);
361 
362 /**
363  * Add a trust anchor to the given context.
364  * The trust anchor is a string, on one line, that holds a valid DNSKEY or
365  * DS RR.
366  * @param ctx: context.
367  *	At this time it is only possible to add trusted keys before the
368  *	first resolve is done.
369  * @param ta: string, with zone-format RR on one line.
370  * 	[domainname] [TTL optional] [type] [class optional] [rdata contents]
371  * @return 0 if OK, else error.
372  */
373 int ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta);
374 
375 /**
376  * Add trust anchors to the given context.
377  * Pass name of a file with DS and DNSKEY records (like from dig or drill).
378  * @param ctx: context.
379  *	At this time it is only possible to add trusted keys before the
380  *	first resolve is done.
381  * @param fname: filename of file with keyfile with trust anchors.
382  * @return 0 if OK, else error.
383  */
384 int ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname);
385 
386 /**
387  * Add trust anchor to the given context that is tracked with RFC5011
388  * automated trust anchor maintenance.  The file is written to when the
389  * trust anchor is changed.
390  * Pass the name of a file that was output from eg. unbound-anchor,
391  * or you can start it by providing a trusted DNSKEY or DS record on one
392  * line in the file.
393  * @param ctx: context.
394  *	At this time it is only possible to add trusted keys before the
395  *	first resolve is done.
396  * @param fname: filename of file with trust anchor.
397  * @return 0 if OK, else error.
398  */
399 int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname);
400 
401 /**
402  * Add trust anchors to the given context.
403  * Pass the name of a bind-style config file with trusted-keys{}.
404  * @param ctx: context.
405  *	At this time it is only possible to add trusted keys before the
406  *	first resolve is done.
407  * @param fname: filename of file with bind-style config entries with trust
408  * 	anchors.
409  * @return 0 if OK, else error.
410  */
411 int ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname);
412 
413 /**
414  * Set debug output (and error output) to the specified stream.
415  * Pass NULL to disable. Default is stderr.
416  * @param ctx: context.
417  * @param out: FILE* out file stream to log to.
418  * 	Type void* to avoid stdio dependency of this header file.
419  * @return 0 if OK, else error.
420  */
421 int ub_ctx_debugout(struct ub_ctx* ctx, void* out);
422 
423 /**
424  * Set debug verbosity for the context
425  * Output is directed to stderr.
426  * @param ctx: context.
427  * @param d: debug level, 0 is off, 1 is very minimal, 2 is detailed,
428  *	and 3 is lots.
429  * @return 0 if OK, else error.
430  */
431 int ub_ctx_debuglevel(struct ub_ctx* ctx, int d);
432 
433 /**
434  * Set a context behaviour for asynchronous action.
435  * @param ctx: context.
436  * @param dothread: if true, enables threading and a call to resolve_async()
437  *	creates a thread to handle work in the background.
438  *	If false, a process is forked to handle work in the background.
439  *	Changes to this setting after async() calls have been made have
440  *	no effect (delete and re-create the context to change).
441  * @return 0 if OK, else error.
442  */
443 int ub_ctx_async(struct ub_ctx* ctx, int dothread);
444 
445 /**
446  * Poll a context to see if it has any new results
447  * Do not poll in a loop, instead extract the fd below to poll for readiness,
448  * and then check, or wait using the wait routine.
449  * @param ctx: context.
450  * @return: 0 if nothing to read, or nonzero if a result is available.
451  * 	If nonzero, call ctx_process() to do callbacks.
452  */
453 int ub_poll(struct ub_ctx* ctx);
454 
455 /**
456  * Wait for a context to finish with results. Calls ub_process() after
457  * the wait for you. After the wait, there are no more outstanding
458  * asynchronous queries.
459  * @param ctx: context.
460  * @return: 0 if OK, else error.
461  */
462 int ub_wait(struct ub_ctx* ctx);
463 
464 /**
465  * Get file descriptor. Wait for it to become readable, at this point
466  * answers are returned from the asynchronous validating resolver.
467  * Then call the ub_process to continue processing.
468  * This routine works immediately after context creation, the fd
469  * does not change.
470  * @param ctx: context.
471  * @return: -1 on error, or file descriptor to use select(2) with.
472  */
473 int ub_fd(struct ub_ctx* ctx);
474 
475 /**
476  * Call this routine to continue processing results from the validating
477  * resolver (when the fd becomes readable).
478  * Will perform necessary callbacks.
479  * @param ctx: context
480  * @return: 0 if OK, else error.
481  */
482 int ub_process(struct ub_ctx* ctx);
483 
484 /**
485  * Perform resolution and validation of the target name.
486  * @param ctx: context.
487  *	The context is finalized, and can no longer accept config changes.
488  * @param name: domain name in text format (a zero terminated text string).
489  * @param rrtype: type of RR in host order, 1 is A (address).
490  * @param rrclass: class of RR in host order, 1 is IN (for internet).
491  * @param result: the result data is returned in a newly allocated result
492  * 	structure. May be NULL on return, return value is set to an error
493  * 	in that case (out of memory).
494  * @return 0 if OK, else error.
495  */
496 int ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
497 	int rrclass, struct ub_result** result);
498 
499 /**
500  * Perform resolution and validation of the target name.
501  * Asynchronous, after a while, the callback will be called with your
502  * data and the result.
503  * @param ctx: context.
504  *	If no thread or process has been created yet to perform the
505  *	work in the background, it is created now.
506  *	The context is finalized, and can no longer accept config changes.
507  * @param name: domain name in text format (a string).
508  * @param rrtype: type of RR in host order, 1 is A.
509  * @param rrclass: class of RR in host order, 1 is IN (for internet).
510  * @param mydata: this data is your own data (you can pass NULL),
511  * 	and is passed on to the callback function.
512  * @param callback: this is called on completion of the resolution.
513  * 	It is called as:
514  * 	void callback(void* mydata, int err, struct ub_result* result)
515  * 	with mydata: the same as passed here, you may pass NULL,
516  * 	with err: is 0 when a result has been found.
517  * 	with result: a newly allocated result structure.
518  *		The result may be NULL, in that case err is set.
519  *
520  * 	If an error happens during processing, your callback will be called
521  * 	with error set to a nonzero value (and result==NULL).
522  * @param async_id: if you pass a non-NULL value, an identifier number is
523  *	returned for the query as it is in progress. It can be used to
524  *	cancel the query.
525  * @return 0 if OK, else error.
526  */
527 int ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
528 	int rrclass, void* mydata, ub_callback_type callback, int* async_id);
529 
530 /**
531  * Cancel an async query in progress.
532  * Its callback will not be called.
533  *
534  * @param ctx: context.
535  * @param async_id: which query to cancel.
536  * @return 0 if OK, else error.
537  * This routine can return an error if the async_id passed does not exist
538  * or has already been delivered. If another thread is processing results
539  * at the same time, the result may be delivered at the same time and the
540  * cancel fails with an error.  Also the cancel can fail due to a system
541  * error, no memory or socket failures.
542  */
543 int ub_cancel(struct ub_ctx* ctx, int async_id);
544 
545 /**
546  * Free storage associated with a result structure.
547  * @param result: to free
548  */
549 void ub_resolve_free(struct ub_result* result);
550 
551 /**
552  * Convert error value to a human readable string.
553  * @param err: error code from one of the libunbound functions.
554  * @return pointer to constant text string, zero terminated.
555  */
556 const char* ub_strerror(int err);
557 
558 /**
559  * Debug routine.  Print the local zone information to debug output.
560  * @param ctx: context.  Is finalized by the routine.
561  * @return 0 if OK, else error.
562  */
563 int ub_ctx_print_local_zones(struct ub_ctx* ctx);
564 
565 /**
566  * Add a new zone with the zonetype to the local authority info of the
567  * library.
568  * @param ctx: context.  Is finalized by the routine.
569  * @param zone_name: name of the zone in text, "example.com"
570  *	If it already exists, the type is updated.
571  * @param zone_type: type of the zone (like for unbound.conf) in text.
572  * @return 0 if OK, else error.
573  */
574 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
575 	const char *zone_type);
576 
577 /**
578  * Remove zone from local authority info of the library.
579  * @param ctx: context.  Is finalized by the routine.
580  * @param zone_name: name of the zone in text, "example.com"
581  *	If it does not exist, nothing happens.
582  * @return 0 if OK, else error.
583  */
584 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name);
585 
586 /**
587  * Add localdata to the library local authority info.
588  * Similar to local-data config statement.
589  * @param ctx: context.  Is finalized by the routine.
590  * @param data: the resource record in text format, for example
591  *	"www.example.com IN A 127.0.0.1"
592  * @return 0 if OK, else error.
593  */
594 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data);
595 
596 /**
597  * Remove localdata from the library local authority info.
598  * @param ctx: context.  Is finalized by the routine.
599  * @param data: the name to delete all data from, like "www.example.com".
600  * @return 0 if OK, else error.
601  */
602 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data);
603 
604 /**
605  * Get a version string from the libunbound implementation.
606  * @return a static constant string with the version number.
607  */
608 const char* ub_version(void);
609 
610 /**
611  * Some global statistics that are not in struct stats_info,
612  * this struct is shared on a shm segment (shm-key in unbound.conf)
613  */
614 struct ub_shm_stat_info {
615 	int num_threads;
616 
617 	struct {
618 		long long now_sec, now_usec;
619 		long long up_sec, up_usec;
620 		long long elapsed_sec, elapsed_usec;
621 	} time;
622 
623 	struct {
624 		long long msg;
625 		long long rrset;
626 		long long val;
627 		long long iter;
628 		long long subnet;
629 		long long ipsecmod;
630 		long long respip;
631 		long long dnscrypt_shared_secret;
632 		long long dnscrypt_nonce;
633 	} mem;
634 };
635 
636 /** number of qtype that is stored for in array */
637 #define UB_STATS_QTYPE_NUM 256
638 /** number of qclass that is stored for in array */
639 #define UB_STATS_QCLASS_NUM 256
640 /** number of rcodes in stats */
641 #define UB_STATS_RCODE_NUM 16
642 /** number of opcodes in stats */
643 #define UB_STATS_OPCODE_NUM 16
644 /** number of histogram buckets */
645 #define UB_STATS_BUCKET_NUM 40
646 
647 /** per worker statistics. */
648 struct ub_server_stats {
649 	/** number of queries from clients received. */
650 	long long num_queries;
651 	/** number of queries that have been dropped/ratelimited by ip. */
652 	long long num_queries_ip_ratelimited;
653 	/** number of queries that had a cache-miss. */
654 	long long num_queries_missed_cache;
655 	/** number of prefetch queries - cachehits with prefetch */
656 	long long num_queries_prefetch;
657 
658 	/**
659 	 * Sum of the querylistsize of the worker for
660 	 * every query that missed cache. To calculate average.
661 	 */
662 	long long sum_query_list_size;
663 	/** max value of query list size reached. */
664 	long long max_query_list_size;
665 
666 	/** Extended stats below (bool) */
667 	int extended;
668 
669 	/** qtype stats */
670 	long long qtype[UB_STATS_QTYPE_NUM];
671 	/** bigger qtype values not in array */
672 	long long qtype_big;
673 	/** qclass stats */
674 	long long qclass[UB_STATS_QCLASS_NUM];
675 	/** bigger qclass values not in array */
676 	long long qclass_big;
677 	/** query opcodes */
678 	long long qopcode[UB_STATS_OPCODE_NUM];
679 	/** number of queries over TCP */
680 	long long qtcp;
681 	/** number of outgoing queries over TCP */
682 	long long qtcp_outgoing;
683 	/** number of queries over (DNS over) TLS */
684 	long long qtls;
685 	/** number of queries over IPv6 */
686 	long long qipv6;
687 	/** number of queries with QR bit */
688 	long long qbit_QR;
689 	/** number of queries with AA bit */
690 	long long qbit_AA;
691 	/** number of queries with TC bit */
692 	long long qbit_TC;
693 	/** number of queries with RD bit */
694 	long long qbit_RD;
695 	/** number of queries with RA bit */
696 	long long qbit_RA;
697 	/** number of queries with Z bit */
698 	long long qbit_Z;
699 	/** number of queries with AD bit */
700 	long long qbit_AD;
701 	/** number of queries with CD bit */
702 	long long qbit_CD;
703 	/** number of queries with EDNS OPT record */
704 	long long qEDNS;
705 	/** number of queries with EDNS with DO flag */
706 	long long qEDNS_DO;
707 	/** answer rcodes */
708 	long long ans_rcode[UB_STATS_RCODE_NUM];
709 	/** answers with pseudo rcode 'nodata' */
710 	long long ans_rcode_nodata;
711 	/** answers that were secure (AD) */
712 	long long ans_secure;
713 	/** answers that were bogus (withheld as SERVFAIL) */
714 	long long ans_bogus;
715 	/** rrsets marked bogus by validator */
716 	long long rrset_bogus;
717 	/** number of queries that have been ratelimited by domain recursion. */
718 	long long queries_ratelimited;
719 	/** unwanted traffic received on server-facing ports */
720 	long long unwanted_replies;
721 	/** unwanted traffic received on client-facing ports */
722 	long long unwanted_queries;
723 	/** usage of tcp accept list */
724 	long long tcp_accept_usage;
725 	/** answers served from expired cache */
726 	long long zero_ttl_responses;
727 	/** histogram data exported to array
728 	 * if the array is the same size, no data is lost, and
729 	 * if all histograms are same size (is so by default) then
730 	 * adding up works well. */
731 	long long hist[UB_STATS_BUCKET_NUM];
732 
733 	/** number of message cache entries */
734 	long long msg_cache_count;
735 	/** number of rrset cache entries */
736 	long long rrset_cache_count;
737 	/** number of infra cache entries */
738 	long long infra_cache_count;
739 	/** number of key cache entries */
740 	long long key_cache_count;
741 
742 	/** number of queries that used dnscrypt */
743 	long long num_query_dnscrypt_crypted;
744 	/** number of queries that queried dnscrypt certificates */
745 	long long num_query_dnscrypt_cert;
746 	/** number of queries in clear text and not asking for the certificates */
747 	long long num_query_dnscrypt_cleartext;
748 	/** number of malformed encrypted queries */
749 	long long num_query_dnscrypt_crypted_malformed;
750 	/** number of queries which did not have a shared secret in cache */
751 	long long num_query_dnscrypt_secret_missed_cache;
752 	/** number of dnscrypt shared secret cache entries */
753 	long long shared_secret_cache_count;
754 	/** number of queries which are replays */
755 	long long num_query_dnscrypt_replay;
756 	/** number of dnscrypt nonces cache entries */
757 	long long nonce_cache_count;
758 	/** number of queries for unbound's auth_zones, upstream query */
759 	long long num_query_authzone_up;
760 	/** number of queries for unbound's auth_zones, downstream answers */
761 	long long num_query_authzone_down;
762 	/** number of times neg cache records were used to generate NOERROR
763 	 * responses. */
764 	long long num_neg_cache_noerror;
765 	/** number of times neg cache records were used to generate NXDOMAIN
766 	 * responses. */
767 	long long num_neg_cache_nxdomain;
768 	/** number of queries answered from edns-subnet specific data */
769 	long long num_query_subnet;
770 	/** number of queries answered from edns-subnet specific data, and
771 	 * the answer was from the edns-subnet cache. */
772 	long long num_query_subnet_cache;
773 };
774 
775 /**
776  * Statistics to send over the control pipe when asked
777  * This struct is made to be memcopied, sent in binary.
778  * shm mapped with (number+1) at num_threads+1, with first as total
779  */
780 struct ub_stats_info {
781 	/** the thread stats */
782 	struct ub_server_stats svr;
783 
784 	/** mesh stats: current number of states */
785 	long long mesh_num_states;
786 	/** mesh stats: current number of reply (user) states */
787 	long long mesh_num_reply_states;
788 	/** mesh stats: number of reply states overwritten with a new one */
789 	long long mesh_jostled;
790 	/** mesh stats: number of incoming queries dropped */
791 	long long mesh_dropped;
792 	/** mesh stats: replies sent */
793 	long long mesh_replies_sent;
794 	/** mesh stats: sum of waiting times for the replies */
795 	long long mesh_replies_sum_wait_sec, mesh_replies_sum_wait_usec;
796 	/** mesh stats: median of waiting times for replies (in sec) */
797 	double mesh_time_median;
798 };
799 
800 #ifdef __cplusplus
801 }
802 #endif
803 
804 #endif /* _UB_UNBOUND_H */
805