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 /** 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. 227b7579f77SDag-Erling Smørgrav * int err: if 0 all is OK, otherwise an error occured 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 /** 236b7579f77SDag-Erling Smørgrav * Create a resolving and validation context. 237b7579f77SDag-Erling Smørgrav * The information from /etc/resolv.conf and /etc/hosts is not utilised by 238b7579f77SDag-Erling Smørgrav * default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them. 239b7579f77SDag-Erling Smørgrav * @return a new context. default initialisation. 240b7579f77SDag-Erling Smørgrav * returns NULL on error. 241b7579f77SDag-Erling Smørgrav */ 242b7579f77SDag-Erling Smørgrav struct ub_ctx* ub_ctx_create(void); 243b7579f77SDag-Erling Smørgrav 244b7579f77SDag-Erling Smørgrav /** 245b7579f77SDag-Erling Smørgrav * Destroy a validation context and free all its resources. 246b7579f77SDag-Erling Smørgrav * Outstanding async queries are killed and callbacks are not called for them. 247b7579f77SDag-Erling Smørgrav * @param ctx: context to delete. 248b7579f77SDag-Erling Smørgrav */ 249b7579f77SDag-Erling Smørgrav void ub_ctx_delete(struct ub_ctx* ctx); 250b7579f77SDag-Erling Smørgrav 251b7579f77SDag-Erling Smørgrav /** 252b7579f77SDag-Erling Smørgrav * Set an option for the context. 253b7579f77SDag-Erling Smørgrav * @param ctx: context. 254b7579f77SDag-Erling Smørgrav * @param opt: option name from the unbound.conf config file format. 255b7579f77SDag-Erling Smørgrav * (not all settings applicable). The name includes the trailing ':' 256b7579f77SDag-Erling Smørgrav * for example ub_ctx_set_option(ctx, "logfile:", "mylog.txt"); 257b7579f77SDag-Erling Smørgrav * This is a power-users interface that lets you specify all sorts 258b7579f77SDag-Erling Smørgrav * of options. 259b7579f77SDag-Erling Smørgrav * For some specific options, such as adding trust anchors, special 260b7579f77SDag-Erling Smørgrav * routines exist. 261b7579f77SDag-Erling Smørgrav * @param val: value of the option. 262b7579f77SDag-Erling Smørgrav * @return: 0 if OK, else error. 263b7579f77SDag-Erling Smørgrav */ 2645d649f2dSDag-Erling Smørgrav int ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val); 265b7579f77SDag-Erling Smørgrav 266b7579f77SDag-Erling Smørgrav /** 267b7579f77SDag-Erling Smørgrav * Get an option from the context. 268b7579f77SDag-Erling Smørgrav * @param ctx: context. 269b7579f77SDag-Erling Smørgrav * @param opt: option name from the unbound.conf config file format. 270b7579f77SDag-Erling Smørgrav * (not all settings applicable). The name excludes the trailing ':' 271b7579f77SDag-Erling Smørgrav * for example ub_ctx_get_option(ctx, "logfile", &result); 272b7579f77SDag-Erling Smørgrav * This is a power-users interface that lets you specify all sorts 273b7579f77SDag-Erling Smørgrav * of options. 274b7579f77SDag-Erling Smørgrav * @param str: the string is malloced and returned here. NULL on error. 275b7579f77SDag-Erling Smørgrav * The caller must free() the string. In cases with multiple 276b7579f77SDag-Erling Smørgrav * entries (auto-trust-anchor-file), a newline delimited list is 277b7579f77SDag-Erling Smørgrav * returned in the string. 278b7579f77SDag-Erling Smørgrav * @return 0 if OK else an error code (malloc failure, syntax error). 279b7579f77SDag-Erling Smørgrav */ 2805d649f2dSDag-Erling Smørgrav int ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str); 281b7579f77SDag-Erling Smørgrav 282b7579f77SDag-Erling Smørgrav /** 283b7579f77SDag-Erling Smørgrav * setup configuration for the given context. 284b7579f77SDag-Erling Smørgrav * @param ctx: context. 285b7579f77SDag-Erling Smørgrav * @param fname: unbound config file (not all settings applicable). 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 * @return: 0 if OK, else error. 291b7579f77SDag-Erling Smørgrav */ 2925d649f2dSDag-Erling Smørgrav int ub_ctx_config(struct ub_ctx* ctx, const char* fname); 293b7579f77SDag-Erling Smørgrav 294b7579f77SDag-Erling Smørgrav /** 295b7579f77SDag-Erling Smørgrav * Set machine to forward DNS queries to, the caching resolver to use. 296b7579f77SDag-Erling Smørgrav * IP4 or IP6 address. Forwards all DNS requests to that machine, which 297b7579f77SDag-Erling Smørgrav * is expected to run a recursive resolver. If the proxy is not 298b7579f77SDag-Erling Smørgrav * DNSSEC-capable, validation may fail. Can be called several times, in 299b7579f77SDag-Erling Smørgrav * that case the addresses are used as backup servers. 300b7579f77SDag-Erling Smørgrav * 301b7579f77SDag-Erling Smørgrav * To read the list of nameservers from /etc/resolv.conf (from DHCP or so), 302b7579f77SDag-Erling Smørgrav * use the call ub_ctx_resolvconf. 303b7579f77SDag-Erling Smørgrav * 304b7579f77SDag-Erling Smørgrav * @param ctx: context. 305b7579f77SDag-Erling Smørgrav * At this time it is only possible to set configuration before the 306b7579f77SDag-Erling Smørgrav * first resolve is done. 307b7579f77SDag-Erling Smørgrav * @param addr: address, IP4 or IP6 in string format. 308b7579f77SDag-Erling Smørgrav * If the addr is NULL, forwarding is disabled. 309b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 310b7579f77SDag-Erling Smørgrav */ 3115d649f2dSDag-Erling Smørgrav int ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr); 312b7579f77SDag-Erling Smørgrav 313b7579f77SDag-Erling Smørgrav /** 314e86b9096SDag-Erling Smørgrav * Use DNS over TLS to send queries to machines set with ub_ctx_set_fwd(). 315e86b9096SDag-Erling Smørgrav * 316e86b9096SDag-Erling Smørgrav * @param ctx: context. 317e86b9096SDag-Erling Smørgrav * At this time it is only possible to set configuration before the 318e86b9096SDag-Erling Smørgrav * first resolve is done. 319e86b9096SDag-Erling Smørgrav * @param tls: enable or disable DNS over TLS 320e86b9096SDag-Erling Smørgrav * @return 0 if OK, else error. 321e86b9096SDag-Erling Smørgrav */ 322e86b9096SDag-Erling Smørgrav int ub_ctx_set_tls(struct ub_ctx* ctx, int tls); 323e86b9096SDag-Erling Smørgrav 324e86b9096SDag-Erling Smørgrav /** 325f61ef7f6SDag-Erling Smørgrav * Add a stub zone, with given address to send to. This is for custom 326f61ef7f6SDag-Erling Smørgrav * root hints or pointing to a local authoritative dns server. 327f61ef7f6SDag-Erling Smørgrav * For dns resolvers and the 'DHCP DNS' ip address, use ub_ctx_set_fwd. 328f61ef7f6SDag-Erling Smørgrav * This is similar to a stub-zone entry in unbound.conf. 329f61ef7f6SDag-Erling Smørgrav * 330f61ef7f6SDag-Erling Smørgrav * @param ctx: context. 331f61ef7f6SDag-Erling Smørgrav * It is only possible to set configuration before the 332f61ef7f6SDag-Erling Smørgrav * first resolve is done. 333f61ef7f6SDag-Erling Smørgrav * @param zone: name of the zone, string. 334f61ef7f6SDag-Erling Smørgrav * @param addr: address, IP4 or IP6 in string format. 335f61ef7f6SDag-Erling Smørgrav * The addr is added to the list of stub-addresses if the entry exists. 336f61ef7f6SDag-Erling Smørgrav * If the addr is NULL the stub entry is removed. 337f61ef7f6SDag-Erling Smørgrav * @param isprime: set to true to set stub-prime to yes for the stub. 338f61ef7f6SDag-Erling Smørgrav * For local authoritative servers, people usually set it to false, 339f61ef7f6SDag-Erling Smørgrav * For root hints it should be set to true. 340f61ef7f6SDag-Erling Smørgrav * @return 0 if OK, else error. 341f61ef7f6SDag-Erling Smørgrav */ 342f61ef7f6SDag-Erling Smørgrav int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr, 343f61ef7f6SDag-Erling Smørgrav int isprime); 344f61ef7f6SDag-Erling Smørgrav 345f61ef7f6SDag-Erling Smørgrav /** 346b7579f77SDag-Erling Smørgrav * Read list of nameservers to use from the filename given. 347b7579f77SDag-Erling Smørgrav * Usually "/etc/resolv.conf". Uses those nameservers as caching proxies. 348b7579f77SDag-Erling Smørgrav * If they do not support DNSSEC, validation may fail. 349b7579f77SDag-Erling Smørgrav * 350b7579f77SDag-Erling Smørgrav * Only nameservers are picked up, the searchdomain, ndots and other 351b7579f77SDag-Erling Smørgrav * settings from resolv.conf(5) are ignored. 352b7579f77SDag-Erling Smørgrav * 353b7579f77SDag-Erling Smørgrav * @param ctx: context. 354b7579f77SDag-Erling Smørgrav * At this time it is only possible to set configuration before the 355b7579f77SDag-Erling Smørgrav * first resolve is done. 356b7579f77SDag-Erling Smørgrav * @param fname: file name string. If NULL "/etc/resolv.conf" is used. 357b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 358b7579f77SDag-Erling Smørgrav */ 3595d649f2dSDag-Erling Smørgrav int ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname); 360b7579f77SDag-Erling Smørgrav 361b7579f77SDag-Erling Smørgrav /** 362b7579f77SDag-Erling Smørgrav * Read list of hosts from the filename given. 363b7579f77SDag-Erling Smørgrav * Usually "/etc/hosts". 364b7579f77SDag-Erling Smørgrav * These addresses are not flagged as DNSSEC secure when queried for. 365b7579f77SDag-Erling Smørgrav * 366b7579f77SDag-Erling Smørgrav * @param ctx: context. 367b7579f77SDag-Erling Smørgrav * At this time it is only possible to set configuration before the 368b7579f77SDag-Erling Smørgrav * first resolve is done. 369b7579f77SDag-Erling Smørgrav * @param fname: file name string. If NULL "/etc/hosts" is used. 370b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 371b7579f77SDag-Erling Smørgrav */ 3725d649f2dSDag-Erling Smørgrav int ub_ctx_hosts(struct ub_ctx* ctx, const char* fname); 373b7579f77SDag-Erling Smørgrav 374b7579f77SDag-Erling Smørgrav /** 375b7579f77SDag-Erling Smørgrav * Add a trust anchor to the given context. 376b7579f77SDag-Erling Smørgrav * The trust anchor is a string, on one line, that holds a valid DNSKEY or 377b7579f77SDag-Erling Smørgrav * DS RR. 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 ta: string, with zone-format RR on one line. 382b7579f77SDag-Erling Smørgrav * [domainname] [TTL optional] [type] [class optional] [rdata contents] 383b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 384b7579f77SDag-Erling Smørgrav */ 3855d649f2dSDag-Erling Smørgrav int ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta); 386b7579f77SDag-Erling Smørgrav 387b7579f77SDag-Erling Smørgrav /** 388b7579f77SDag-Erling Smørgrav * Add trust anchors to the given context. 389b7579f77SDag-Erling Smørgrav * Pass name of a file with DS and DNSKEY records (like from dig or drill). 390b7579f77SDag-Erling Smørgrav * @param ctx: context. 391b7579f77SDag-Erling Smørgrav * At this time it is only possible to add trusted keys before the 392b7579f77SDag-Erling Smørgrav * first resolve is done. 393b7579f77SDag-Erling Smørgrav * @param fname: filename of file with keyfile with trust anchors. 394b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 395b7579f77SDag-Erling Smørgrav */ 3965d649f2dSDag-Erling Smørgrav int ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname); 397b7579f77SDag-Erling Smørgrav 398b7579f77SDag-Erling Smørgrav /** 399ff825849SDag-Erling Smørgrav * Add trust anchor to the given context that is tracked with RFC5011 400ff825849SDag-Erling Smørgrav * automated trust anchor maintenance. The file is written to when the 401ff825849SDag-Erling Smørgrav * trust anchor is changed. 402ff825849SDag-Erling Smørgrav * Pass the name of a file that was output from eg. unbound-anchor, 403ff825849SDag-Erling Smørgrav * or you can start it by providing a trusted DNSKEY or DS record on one 404ff825849SDag-Erling Smørgrav * line in the file. 405ff825849SDag-Erling Smørgrav * @param ctx: context. 406ff825849SDag-Erling Smørgrav * At this time it is only possible to add trusted keys before the 407ff825849SDag-Erling Smørgrav * first resolve is done. 408ff825849SDag-Erling Smørgrav * @param fname: filename of file with trust anchor. 409ff825849SDag-Erling Smørgrav * @return 0 if OK, else error. 410ff825849SDag-Erling Smørgrav */ 411ff825849SDag-Erling Smørgrav int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname); 412ff825849SDag-Erling Smørgrav 413ff825849SDag-Erling Smørgrav /** 414b7579f77SDag-Erling Smørgrav * Add trust anchors to the given context. 415b7579f77SDag-Erling Smørgrav * Pass the name of a bind-style config file with trusted-keys{}. 416b7579f77SDag-Erling Smørgrav * @param ctx: context. 417b7579f77SDag-Erling Smørgrav * At this time it is only possible to add trusted keys before the 418b7579f77SDag-Erling Smørgrav * first resolve is done. 419b7579f77SDag-Erling Smørgrav * @param fname: filename of file with bind-style config entries with trust 420b7579f77SDag-Erling Smørgrav * anchors. 421b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 422b7579f77SDag-Erling Smørgrav */ 4235d649f2dSDag-Erling Smørgrav int ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname); 424b7579f77SDag-Erling Smørgrav 425b7579f77SDag-Erling Smørgrav /** 426b7579f77SDag-Erling Smørgrav * Set debug output (and error output) to the specified stream. 427b7579f77SDag-Erling Smørgrav * Pass NULL to disable. Default is stderr. 428b7579f77SDag-Erling Smørgrav * @param ctx: context. 429b7579f77SDag-Erling Smørgrav * @param out: FILE* out file stream to log to. 430b7579f77SDag-Erling Smørgrav * Type void* to avoid stdio dependency of this header file. 431b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 432b7579f77SDag-Erling Smørgrav */ 433b7579f77SDag-Erling Smørgrav int ub_ctx_debugout(struct ub_ctx* ctx, void* out); 434b7579f77SDag-Erling Smørgrav 435b7579f77SDag-Erling Smørgrav /** 436b7579f77SDag-Erling Smørgrav * Set debug verbosity for the context 437b7579f77SDag-Erling Smørgrav * Output is directed to stderr. 438b7579f77SDag-Erling Smørgrav * @param ctx: context. 439b7579f77SDag-Erling Smørgrav * @param d: debug level, 0 is off, 1 is very minimal, 2 is detailed, 440b7579f77SDag-Erling Smørgrav * and 3 is lots. 441b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 442b7579f77SDag-Erling Smørgrav */ 443b7579f77SDag-Erling Smørgrav int ub_ctx_debuglevel(struct ub_ctx* ctx, int d); 444b7579f77SDag-Erling Smørgrav 445b7579f77SDag-Erling Smørgrav /** 446b7579f77SDag-Erling Smørgrav * Set a context behaviour for asynchronous action. 447b7579f77SDag-Erling Smørgrav * @param ctx: context. 448b7579f77SDag-Erling Smørgrav * @param dothread: if true, enables threading and a call to resolve_async() 449b7579f77SDag-Erling Smørgrav * creates a thread to handle work in the background. 450b7579f77SDag-Erling Smørgrav * If false, a process is forked to handle work in the background. 451b7579f77SDag-Erling Smørgrav * Changes to this setting after async() calls have been made have 452b7579f77SDag-Erling Smørgrav * no effect (delete and re-create the context to change). 453b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 454b7579f77SDag-Erling Smørgrav */ 455b7579f77SDag-Erling Smørgrav int ub_ctx_async(struct ub_ctx* ctx, int dothread); 456b7579f77SDag-Erling Smørgrav 457b7579f77SDag-Erling Smørgrav /** 458b7579f77SDag-Erling Smørgrav * Poll a context to see if it has any new results 459b7579f77SDag-Erling Smørgrav * Do not poll in a loop, instead extract the fd below to poll for readiness, 460b7579f77SDag-Erling Smørgrav * and then check, or wait using the wait routine. 461b7579f77SDag-Erling Smørgrav * @param ctx: context. 462b7579f77SDag-Erling Smørgrav * @return: 0 if nothing to read, or nonzero if a result is available. 463b7579f77SDag-Erling Smørgrav * If nonzero, call ctx_process() to do callbacks. 464b7579f77SDag-Erling Smørgrav */ 465b7579f77SDag-Erling Smørgrav int ub_poll(struct ub_ctx* ctx); 466b7579f77SDag-Erling Smørgrav 467b7579f77SDag-Erling Smørgrav /** 468b7579f77SDag-Erling Smørgrav * Wait for a context to finish with results. Calls ub_process() after 469b7579f77SDag-Erling Smørgrav * the wait for you. After the wait, there are no more outstanding 470b7579f77SDag-Erling Smørgrav * asynchronous queries. 471b7579f77SDag-Erling Smørgrav * @param ctx: context. 472b7579f77SDag-Erling Smørgrav * @return: 0 if OK, else error. 473b7579f77SDag-Erling Smørgrav */ 474b7579f77SDag-Erling Smørgrav int ub_wait(struct ub_ctx* ctx); 475b7579f77SDag-Erling Smørgrav 476b7579f77SDag-Erling Smørgrav /** 477b7579f77SDag-Erling Smørgrav * Get file descriptor. Wait for it to become readable, at this point 478b7579f77SDag-Erling Smørgrav * answers are returned from the asynchronous validating resolver. 479b7579f77SDag-Erling Smørgrav * Then call the ub_process to continue processing. 480b7579f77SDag-Erling Smørgrav * This routine works immediately after context creation, the fd 481b7579f77SDag-Erling Smørgrav * does not change. 482b7579f77SDag-Erling Smørgrav * @param ctx: context. 483b7579f77SDag-Erling Smørgrav * @return: -1 on error, or file descriptor to use select(2) with. 484b7579f77SDag-Erling Smørgrav */ 485b7579f77SDag-Erling Smørgrav int ub_fd(struct ub_ctx* ctx); 486b7579f77SDag-Erling Smørgrav 487b7579f77SDag-Erling Smørgrav /** 488b7579f77SDag-Erling Smørgrav * Call this routine to continue processing results from the validating 489b7579f77SDag-Erling Smørgrav * resolver (when the fd becomes readable). 490b7579f77SDag-Erling Smørgrav * Will perform necessary callbacks. 491b7579f77SDag-Erling Smørgrav * @param ctx: context 492b7579f77SDag-Erling Smørgrav * @return: 0 if OK, else error. 493b7579f77SDag-Erling Smørgrav */ 494b7579f77SDag-Erling Smørgrav int ub_process(struct ub_ctx* ctx); 495b7579f77SDag-Erling Smørgrav 496b7579f77SDag-Erling Smørgrav /** 497b7579f77SDag-Erling Smørgrav * Perform resolution and validation of the target name. 498b7579f77SDag-Erling Smørgrav * @param ctx: context. 499b7579f77SDag-Erling Smørgrav * The context is finalized, and can no longer accept config changes. 500b7579f77SDag-Erling Smørgrav * @param name: domain name in text format (a zero terminated text string). 501b7579f77SDag-Erling Smørgrav * @param rrtype: type of RR in host order, 1 is A (address). 502b7579f77SDag-Erling Smørgrav * @param rrclass: class of RR in host order, 1 is IN (for internet). 503b7579f77SDag-Erling Smørgrav * @param result: the result data is returned in a newly allocated result 504b7579f77SDag-Erling Smørgrav * structure. May be NULL on return, return value is set to an error 505b7579f77SDag-Erling Smørgrav * in that case (out of memory). 506b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 507b7579f77SDag-Erling Smørgrav */ 5085d649f2dSDag-Erling Smørgrav int ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype, 509b7579f77SDag-Erling Smørgrav int rrclass, struct ub_result** result); 510b7579f77SDag-Erling Smørgrav 511b7579f77SDag-Erling Smørgrav /** 512b7579f77SDag-Erling Smørgrav * Perform resolution and validation of the target name. 513b7579f77SDag-Erling Smørgrav * Asynchronous, after a while, the callback will be called with your 514b7579f77SDag-Erling Smørgrav * data and the result. 515b7579f77SDag-Erling Smørgrav * @param ctx: context. 516b7579f77SDag-Erling Smørgrav * If no thread or process has been created yet to perform the 517b7579f77SDag-Erling Smørgrav * work in the background, it is created now. 518b7579f77SDag-Erling Smørgrav * The context is finalized, and can no longer accept config changes. 519b7579f77SDag-Erling Smørgrav * @param name: domain name in text format (a string). 520b7579f77SDag-Erling Smørgrav * @param rrtype: type of RR in host order, 1 is A. 521b7579f77SDag-Erling Smørgrav * @param rrclass: class of RR in host order, 1 is IN (for internet). 522b7579f77SDag-Erling Smørgrav * @param mydata: this data is your own data (you can pass NULL), 523b7579f77SDag-Erling Smørgrav * and is passed on to the callback function. 524b7579f77SDag-Erling Smørgrav * @param callback: this is called on completion of the resolution. 525b7579f77SDag-Erling Smørgrav * It is called as: 526b7579f77SDag-Erling Smørgrav * void callback(void* mydata, int err, struct ub_result* result) 527b7579f77SDag-Erling Smørgrav * with mydata: the same as passed here, you may pass NULL, 528b7579f77SDag-Erling Smørgrav * with err: is 0 when a result has been found. 529b7579f77SDag-Erling Smørgrav * with result: a newly allocated result structure. 530b7579f77SDag-Erling Smørgrav * The result may be NULL, in that case err is set. 531b7579f77SDag-Erling Smørgrav * 532b7579f77SDag-Erling Smørgrav * If an error happens during processing, your callback will be called 533b7579f77SDag-Erling Smørgrav * with error set to a nonzero value (and result==NULL). 534b7579f77SDag-Erling Smørgrav * @param async_id: if you pass a non-NULL value, an identifier number is 535b7579f77SDag-Erling Smørgrav * returned for the query as it is in progress. It can be used to 536b7579f77SDag-Erling Smørgrav * cancel the query. 537b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 538b7579f77SDag-Erling Smørgrav */ 5395d649f2dSDag-Erling Smørgrav int ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype, 5403005e0a3SDag-Erling Smørgrav int rrclass, void* mydata, ub_callback_type callback, int* async_id); 541b7579f77SDag-Erling Smørgrav 542b7579f77SDag-Erling Smørgrav /** 543b7579f77SDag-Erling Smørgrav * Cancel an async query in progress. 544b7579f77SDag-Erling Smørgrav * Its callback will not be called. 545b7579f77SDag-Erling Smørgrav * 546b7579f77SDag-Erling Smørgrav * @param ctx: context. 547b7579f77SDag-Erling Smørgrav * @param async_id: which query to cancel. 548b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 549b7579f77SDag-Erling Smørgrav * This routine can return an error if the async_id passed does not exist 550b7579f77SDag-Erling Smørgrav * or has already been delivered. If another thread is processing results 551b7579f77SDag-Erling Smørgrav * at the same time, the result may be delivered at the same time and the 552b7579f77SDag-Erling Smørgrav * cancel fails with an error. Also the cancel can fail due to a system 553b7579f77SDag-Erling Smørgrav * error, no memory or socket failures. 554b7579f77SDag-Erling Smørgrav */ 555b7579f77SDag-Erling Smørgrav int ub_cancel(struct ub_ctx* ctx, int async_id); 556b7579f77SDag-Erling Smørgrav 557b7579f77SDag-Erling Smørgrav /** 558b7579f77SDag-Erling Smørgrav * Free storage associated with a result structure. 559b7579f77SDag-Erling Smørgrav * @param result: to free 560b7579f77SDag-Erling Smørgrav */ 561b7579f77SDag-Erling Smørgrav void ub_resolve_free(struct ub_result* result); 562b7579f77SDag-Erling Smørgrav 563b7579f77SDag-Erling Smørgrav /** 564b7579f77SDag-Erling Smørgrav * Convert error value to a human readable string. 565ff825849SDag-Erling Smørgrav * @param err: error code from one of the libunbound functions. 566b7579f77SDag-Erling Smørgrav * @return pointer to constant text string, zero terminated. 567b7579f77SDag-Erling Smørgrav */ 568b7579f77SDag-Erling Smørgrav const char* ub_strerror(int err); 569b7579f77SDag-Erling Smørgrav 570b7579f77SDag-Erling Smørgrav /** 571b7579f77SDag-Erling Smørgrav * Debug routine. Print the local zone information to debug output. 572b7579f77SDag-Erling Smørgrav * @param ctx: context. Is finalized by the routine. 573b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 574b7579f77SDag-Erling Smørgrav */ 575b7579f77SDag-Erling Smørgrav int ub_ctx_print_local_zones(struct ub_ctx* ctx); 576b7579f77SDag-Erling Smørgrav 577b7579f77SDag-Erling Smørgrav /** 578b7579f77SDag-Erling Smørgrav * Add a new zone with the zonetype to the local authority info of the 579b7579f77SDag-Erling Smørgrav * library. 580b7579f77SDag-Erling Smørgrav * @param ctx: context. Is finalized by the routine. 581b7579f77SDag-Erling Smørgrav * @param zone_name: name of the zone in text, "example.com" 582b7579f77SDag-Erling Smørgrav * If it already exists, the type is updated. 583b7579f77SDag-Erling Smørgrav * @param zone_type: type of the zone (like for unbound.conf) in text. 584b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 585b7579f77SDag-Erling Smørgrav */ 58617d15b25SDag-Erling Smørgrav int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name, 58717d15b25SDag-Erling Smørgrav const char *zone_type); 588b7579f77SDag-Erling Smørgrav 589b7579f77SDag-Erling Smørgrav /** 590b7579f77SDag-Erling Smørgrav * Remove zone from local authority info of the library. 591b7579f77SDag-Erling Smørgrav * @param ctx: context. Is finalized by the routine. 592b7579f77SDag-Erling Smørgrav * @param zone_name: name of the zone in text, "example.com" 593b7579f77SDag-Erling Smørgrav * If it does not exist, nothing happens. 594b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 595b7579f77SDag-Erling Smørgrav */ 59617d15b25SDag-Erling Smørgrav int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name); 597b7579f77SDag-Erling Smørgrav 598b7579f77SDag-Erling Smørgrav /** 599b7579f77SDag-Erling Smørgrav * Add localdata to the library local authority info. 600b7579f77SDag-Erling Smørgrav * Similar to local-data config statement. 601b7579f77SDag-Erling Smørgrav * @param ctx: context. Is finalized by the routine. 602b7579f77SDag-Erling Smørgrav * @param data: the resource record in text format, for example 603b7579f77SDag-Erling Smørgrav * "www.example.com IN A 127.0.0.1" 604b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 605b7579f77SDag-Erling Smørgrav */ 60617d15b25SDag-Erling Smørgrav int ub_ctx_data_add(struct ub_ctx* ctx, const char *data); 607b7579f77SDag-Erling Smørgrav 608b7579f77SDag-Erling Smørgrav /** 609b7579f77SDag-Erling Smørgrav * Remove localdata from the library local authority info. 610b7579f77SDag-Erling Smørgrav * @param ctx: context. Is finalized by the routine. 611b7579f77SDag-Erling Smørgrav * @param data: the name to delete all data from, like "www.example.com". 612b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 613b7579f77SDag-Erling Smørgrav */ 61417d15b25SDag-Erling Smørgrav int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data); 615b7579f77SDag-Erling Smørgrav 616b7579f77SDag-Erling Smørgrav /** 617b7579f77SDag-Erling Smørgrav * Get a version string from the libunbound implementation. 618b7579f77SDag-Erling Smørgrav * @return a static constant string with the version number. 619b7579f77SDag-Erling Smørgrav */ 620b7579f77SDag-Erling Smørgrav const char* ub_version(void); 621b7579f77SDag-Erling Smørgrav 622c7f4d7adSDag-Erling Smørgrav /** 623c7f4d7adSDag-Erling Smørgrav * Some global statistics that are not in struct stats_info, 624c7f4d7adSDag-Erling Smørgrav * this struct is shared on a shm segment (shm-key in unbound.conf) 625c7f4d7adSDag-Erling Smørgrav */ 626c7f4d7adSDag-Erling Smørgrav struct ub_shm_stat_info { 627c7f4d7adSDag-Erling Smørgrav int num_threads; 628c7f4d7adSDag-Erling Smørgrav 629c7f4d7adSDag-Erling Smørgrav struct { 630c7f4d7adSDag-Erling Smørgrav long long now_sec, now_usec; 631c7f4d7adSDag-Erling Smørgrav long long up_sec, up_usec; 632c7f4d7adSDag-Erling Smørgrav long long elapsed_sec, elapsed_usec; 633c7f4d7adSDag-Erling Smørgrav } time; 634c7f4d7adSDag-Erling Smørgrav 635c7f4d7adSDag-Erling Smørgrav struct { 636c7f4d7adSDag-Erling Smørgrav long long msg; 637c7f4d7adSDag-Erling Smørgrav long long rrset; 638c7f4d7adSDag-Erling Smørgrav long long val; 639c7f4d7adSDag-Erling Smørgrav long long iter; 640c7f4d7adSDag-Erling Smørgrav long long subnet; 641c7f4d7adSDag-Erling Smørgrav long long ipsecmod; 642c7f4d7adSDag-Erling Smørgrav long long respip; 643971980c3SDag-Erling Smørgrav long long dnscrypt_shared_secret; 6448a384985SDag-Erling Smørgrav long long dnscrypt_nonce; 64525039b37SCy Schubert long long dynlib; 646c7f4d7adSDag-Erling Smørgrav } mem; 647c7f4d7adSDag-Erling Smørgrav }; 648c7f4d7adSDag-Erling Smørgrav 649c7f4d7adSDag-Erling Smørgrav /** number of qtype that is stored for in array */ 650c7f4d7adSDag-Erling Smørgrav #define UB_STATS_QTYPE_NUM 256 651c7f4d7adSDag-Erling Smørgrav /** number of qclass that is stored for in array */ 652c7f4d7adSDag-Erling Smørgrav #define UB_STATS_QCLASS_NUM 256 653c7f4d7adSDag-Erling Smørgrav /** number of rcodes in stats */ 654c7f4d7adSDag-Erling Smørgrav #define UB_STATS_RCODE_NUM 16 655c7f4d7adSDag-Erling Smørgrav /** number of opcodes in stats */ 656c7f4d7adSDag-Erling Smørgrav #define UB_STATS_OPCODE_NUM 16 657c7f4d7adSDag-Erling Smørgrav /** number of histogram buckets */ 658c7f4d7adSDag-Erling Smørgrav #define UB_STATS_BUCKET_NUM 40 659091e9e46SCy Schubert /** number of RPZ actions */ 660091e9e46SCy Schubert #define UB_STATS_RPZ_ACTION_NUM 10 661c7f4d7adSDag-Erling Smørgrav 662c7f4d7adSDag-Erling Smørgrav /** per worker statistics. */ 663c7f4d7adSDag-Erling Smørgrav struct ub_server_stats { 664c7f4d7adSDag-Erling Smørgrav /** number of queries from clients received. */ 665c7f4d7adSDag-Erling Smørgrav long long num_queries; 666c7f4d7adSDag-Erling Smørgrav /** number of queries that have been dropped/ratelimited by ip. */ 667c7f4d7adSDag-Erling Smørgrav long long num_queries_ip_ratelimited; 668c7f4d7adSDag-Erling Smørgrav /** number of queries that had a cache-miss. */ 669c7f4d7adSDag-Erling Smørgrav long long num_queries_missed_cache; 670c7f4d7adSDag-Erling Smørgrav /** number of prefetch queries - cachehits with prefetch */ 671c7f4d7adSDag-Erling Smørgrav long long num_queries_prefetch; 672c7f4d7adSDag-Erling Smørgrav 673c7f4d7adSDag-Erling Smørgrav /** 674c7f4d7adSDag-Erling Smørgrav * Sum of the querylistsize of the worker for 675c7f4d7adSDag-Erling Smørgrav * every query that missed cache. To calculate average. 676c7f4d7adSDag-Erling Smørgrav */ 677c7f4d7adSDag-Erling Smørgrav long long sum_query_list_size; 678c7f4d7adSDag-Erling Smørgrav /** max value of query list size reached. */ 679c7f4d7adSDag-Erling Smørgrav long long max_query_list_size; 680c7f4d7adSDag-Erling Smørgrav 681c7f4d7adSDag-Erling Smørgrav /** Extended stats below (bool) */ 682c7f4d7adSDag-Erling Smørgrav int extended; 683c7f4d7adSDag-Erling Smørgrav 684c7f4d7adSDag-Erling Smørgrav /** qtype stats */ 685c7f4d7adSDag-Erling Smørgrav long long qtype[UB_STATS_QTYPE_NUM]; 686c7f4d7adSDag-Erling Smørgrav /** bigger qtype values not in array */ 687c7f4d7adSDag-Erling Smørgrav long long qtype_big; 688c7f4d7adSDag-Erling Smørgrav /** qclass stats */ 689c7f4d7adSDag-Erling Smørgrav long long qclass[UB_STATS_QCLASS_NUM]; 690c7f4d7adSDag-Erling Smørgrav /** bigger qclass values not in array */ 691c7f4d7adSDag-Erling Smørgrav long long qclass_big; 692c7f4d7adSDag-Erling Smørgrav /** query opcodes */ 693c7f4d7adSDag-Erling Smørgrav long long qopcode[UB_STATS_OPCODE_NUM]; 694c7f4d7adSDag-Erling Smørgrav /** number of queries over TCP */ 695c7f4d7adSDag-Erling Smørgrav long long qtcp; 696c7f4d7adSDag-Erling Smørgrav /** number of outgoing queries over TCP */ 697c7f4d7adSDag-Erling Smørgrav long long qtcp_outgoing; 6984c75e3aaSDag-Erling Smørgrav /** number of queries over (DNS over) TLS */ 6994c75e3aaSDag-Erling Smørgrav long long qtls; 700*c0caa2e2SCy Schubert /** number of queries over (DNS over) HTTPS */ 701*c0caa2e2SCy Schubert long long qhttps; 702c7f4d7adSDag-Erling Smørgrav /** number of queries over IPv6 */ 703c7f4d7adSDag-Erling Smørgrav long long qipv6; 704c7f4d7adSDag-Erling Smørgrav /** number of queries with QR bit */ 705c7f4d7adSDag-Erling Smørgrav long long qbit_QR; 706c7f4d7adSDag-Erling Smørgrav /** number of queries with AA bit */ 707c7f4d7adSDag-Erling Smørgrav long long qbit_AA; 708c7f4d7adSDag-Erling Smørgrav /** number of queries with TC bit */ 709c7f4d7adSDag-Erling Smørgrav long long qbit_TC; 710c7f4d7adSDag-Erling Smørgrav /** number of queries with RD bit */ 711c7f4d7adSDag-Erling Smørgrav long long qbit_RD; 712c7f4d7adSDag-Erling Smørgrav /** number of queries with RA bit */ 713c7f4d7adSDag-Erling Smørgrav long long qbit_RA; 714c7f4d7adSDag-Erling Smørgrav /** number of queries with Z bit */ 715c7f4d7adSDag-Erling Smørgrav long long qbit_Z; 716c7f4d7adSDag-Erling Smørgrav /** number of queries with AD bit */ 717c7f4d7adSDag-Erling Smørgrav long long qbit_AD; 718c7f4d7adSDag-Erling Smørgrav /** number of queries with CD bit */ 719c7f4d7adSDag-Erling Smørgrav long long qbit_CD; 720c7f4d7adSDag-Erling Smørgrav /** number of queries with EDNS OPT record */ 721c7f4d7adSDag-Erling Smørgrav long long qEDNS; 722c7f4d7adSDag-Erling Smørgrav /** number of queries with EDNS with DO flag */ 723c7f4d7adSDag-Erling Smørgrav long long qEDNS_DO; 724c7f4d7adSDag-Erling Smørgrav /** answer rcodes */ 725c7f4d7adSDag-Erling Smørgrav long long ans_rcode[UB_STATS_RCODE_NUM]; 726c7f4d7adSDag-Erling Smørgrav /** answers with pseudo rcode 'nodata' */ 727c7f4d7adSDag-Erling Smørgrav long long ans_rcode_nodata; 728c7f4d7adSDag-Erling Smørgrav /** answers that were secure (AD) */ 729c7f4d7adSDag-Erling Smørgrav long long ans_secure; 730c7f4d7adSDag-Erling Smørgrav /** answers that were bogus (withheld as SERVFAIL) */ 731c7f4d7adSDag-Erling Smørgrav long long ans_bogus; 732c7f4d7adSDag-Erling Smørgrav /** rrsets marked bogus by validator */ 733c7f4d7adSDag-Erling Smørgrav long long rrset_bogus; 734971980c3SDag-Erling Smørgrav /** number of queries that have been ratelimited by domain recursion. */ 735971980c3SDag-Erling Smørgrav long long queries_ratelimited; 736c7f4d7adSDag-Erling Smørgrav /** unwanted traffic received on server-facing ports */ 737c7f4d7adSDag-Erling Smørgrav long long unwanted_replies; 738c7f4d7adSDag-Erling Smørgrav /** unwanted traffic received on client-facing ports */ 739c7f4d7adSDag-Erling Smørgrav long long unwanted_queries; 740c7f4d7adSDag-Erling Smørgrav /** usage of tcp accept list */ 741c7f4d7adSDag-Erling Smørgrav long long tcp_accept_usage; 742091e9e46SCy Schubert /** expired answers served from cache */ 743091e9e46SCy Schubert long long ans_expired; 744c7f4d7adSDag-Erling Smørgrav /** histogram data exported to array 745c7f4d7adSDag-Erling Smørgrav * if the array is the same size, no data is lost, and 746c7f4d7adSDag-Erling Smørgrav * if all histograms are same size (is so by default) then 747c7f4d7adSDag-Erling Smørgrav * adding up works well. */ 748c7f4d7adSDag-Erling Smørgrav long long hist[UB_STATS_BUCKET_NUM]; 749c7f4d7adSDag-Erling Smørgrav 750c7f4d7adSDag-Erling Smørgrav /** number of message cache entries */ 751c7f4d7adSDag-Erling Smørgrav long long msg_cache_count; 752c7f4d7adSDag-Erling Smørgrav /** number of rrset cache entries */ 753c7f4d7adSDag-Erling Smørgrav long long rrset_cache_count; 754c7f4d7adSDag-Erling Smørgrav /** number of infra cache entries */ 755c7f4d7adSDag-Erling Smørgrav long long infra_cache_count; 756c7f4d7adSDag-Erling Smørgrav /** number of key cache entries */ 757c7f4d7adSDag-Erling Smørgrav long long key_cache_count; 758c7f4d7adSDag-Erling Smørgrav 759c7f4d7adSDag-Erling Smørgrav /** number of queries that used dnscrypt */ 760c7f4d7adSDag-Erling Smørgrav long long num_query_dnscrypt_crypted; 761c7f4d7adSDag-Erling Smørgrav /** number of queries that queried dnscrypt certificates */ 762c7f4d7adSDag-Erling Smørgrav long long num_query_dnscrypt_cert; 763c7f4d7adSDag-Erling Smørgrav /** number of queries in clear text and not asking for the certificates */ 764c7f4d7adSDag-Erling Smørgrav long long num_query_dnscrypt_cleartext; 765c7f4d7adSDag-Erling Smørgrav /** number of malformed encrypted queries */ 766c7f4d7adSDag-Erling Smørgrav long long num_query_dnscrypt_crypted_malformed; 767971980c3SDag-Erling Smørgrav /** number of queries which did not have a shared secret in cache */ 768971980c3SDag-Erling Smørgrav long long num_query_dnscrypt_secret_missed_cache; 769971980c3SDag-Erling Smørgrav /** number of dnscrypt shared secret cache entries */ 770971980c3SDag-Erling Smørgrav long long shared_secret_cache_count; 7718a384985SDag-Erling Smørgrav /** number of queries which are replays */ 7728a384985SDag-Erling Smørgrav long long num_query_dnscrypt_replay; 7738a384985SDag-Erling Smørgrav /** number of dnscrypt nonces cache entries */ 7748a384985SDag-Erling Smørgrav long long nonce_cache_count; 7750fb34990SDag-Erling Smørgrav /** number of queries for unbound's auth_zones, upstream query */ 7760fb34990SDag-Erling Smørgrav long long num_query_authzone_up; 7770fb34990SDag-Erling Smørgrav /** number of queries for unbound's auth_zones, downstream answers */ 7780fb34990SDag-Erling Smørgrav long long num_query_authzone_down; 7790fb34990SDag-Erling Smørgrav /** number of times neg cache records were used to generate NOERROR 7800fb34990SDag-Erling Smørgrav * responses. */ 7810fb34990SDag-Erling Smørgrav long long num_neg_cache_noerror; 7820fb34990SDag-Erling Smørgrav /** number of times neg cache records were used to generate NXDOMAIN 7830fb34990SDag-Erling Smørgrav * responses. */ 7840fb34990SDag-Erling Smørgrav long long num_neg_cache_nxdomain; 7854c75e3aaSDag-Erling Smørgrav /** number of queries answered from edns-subnet specific data */ 7864c75e3aaSDag-Erling Smørgrav long long num_query_subnet; 7874c75e3aaSDag-Erling Smørgrav /** number of queries answered from edns-subnet specific data, and 7884c75e3aaSDag-Erling Smørgrav * the answer was from the edns-subnet cache. */ 7894c75e3aaSDag-Erling Smørgrav long long num_query_subnet_cache; 790e86b9096SDag-Erling Smørgrav /** number of bytes in the stream wait buffers */ 791e86b9096SDag-Erling Smørgrav long long mem_stream_wait; 792*c0caa2e2SCy Schubert /** number of bytes in the HTTP2 query buffers */ 793*c0caa2e2SCy Schubert long long mem_http2_query_buffer; 794*c0caa2e2SCy Schubert /** number of bytes in the HTTP2 response buffers */ 795*c0caa2e2SCy Schubert long long mem_http2_response_buffer; 796e86b9096SDag-Erling Smørgrav /** number of TLS connection resume */ 797e86b9096SDag-Erling Smørgrav long long qtls_resume; 798091e9e46SCy Schubert /** RPZ action stats */ 799091e9e46SCy Schubert long long rpz_action[UB_STATS_RPZ_ACTION_NUM]; 800c7f4d7adSDag-Erling Smørgrav }; 801c7f4d7adSDag-Erling Smørgrav 802c7f4d7adSDag-Erling Smørgrav /** 803c7f4d7adSDag-Erling Smørgrav * Statistics to send over the control pipe when asked 8048a384985SDag-Erling Smørgrav * This struct is made to be memcopied, sent in binary. 805c7f4d7adSDag-Erling Smørgrav * shm mapped with (number+1) at num_threads+1, with first as total 806c7f4d7adSDag-Erling Smørgrav */ 807c7f4d7adSDag-Erling Smørgrav struct ub_stats_info { 808c7f4d7adSDag-Erling Smørgrav /** the thread stats */ 809c7f4d7adSDag-Erling Smørgrav struct ub_server_stats svr; 810c7f4d7adSDag-Erling Smørgrav 811c7f4d7adSDag-Erling Smørgrav /** mesh stats: current number of states */ 812c7f4d7adSDag-Erling Smørgrav long long mesh_num_states; 813c7f4d7adSDag-Erling Smørgrav /** mesh stats: current number of reply (user) states */ 814c7f4d7adSDag-Erling Smørgrav long long mesh_num_reply_states; 815c7f4d7adSDag-Erling Smørgrav /** mesh stats: number of reply states overwritten with a new one */ 816c7f4d7adSDag-Erling Smørgrav long long mesh_jostled; 817c7f4d7adSDag-Erling Smørgrav /** mesh stats: number of incoming queries dropped */ 818c7f4d7adSDag-Erling Smørgrav long long mesh_dropped; 819c7f4d7adSDag-Erling Smørgrav /** mesh stats: replies sent */ 820c7f4d7adSDag-Erling Smørgrav long long mesh_replies_sent; 821c7f4d7adSDag-Erling Smørgrav /** mesh stats: sum of waiting times for the replies */ 822c7f4d7adSDag-Erling Smørgrav long long mesh_replies_sum_wait_sec, mesh_replies_sum_wait_usec; 823c7f4d7adSDag-Erling Smørgrav /** mesh stats: median of waiting times for replies (in sec) */ 824c7f4d7adSDag-Erling Smørgrav double mesh_time_median; 825c7f4d7adSDag-Erling Smørgrav }; 826c7f4d7adSDag-Erling Smørgrav 827b7579f77SDag-Erling Smørgrav #ifdef __cplusplus 828b7579f77SDag-Erling Smørgrav } 829b7579f77SDag-Erling Smørgrav #endif 830b7579f77SDag-Erling Smørgrav 831b7579f77SDag-Erling Smørgrav #endif /* _UB_UNBOUND_H */ 832