xref: /openbsd-src/usr.sbin/unbound/libunbound/libworker.h (revision 20237c5596ac37c947fe7dc6b504ad5cf4d698eb)
1933707f3Ssthen /*
277079be7Ssthen  * libunbound/libworker.h - worker thread or process that resolves
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains the worker process or thread that performs
40933707f3Ssthen  * the DNS resolving and validation. The worker is called by a procedure
41933707f3Ssthen  * and if in the background continues until exit, if in the foreground
42933707f3Ssthen  * returns from the procedure when done.
43933707f3Ssthen  */
4498f3ca02Sbrad #ifndef LIBUNBOUND_LIBWORKER_H
4598f3ca02Sbrad #define LIBUNBOUND_LIBWORKER_H
46933707f3Ssthen #include "util/data/packed_rrset.h"
47933707f3Ssthen struct ub_ctx;
48933707f3Ssthen struct ub_result;
49933707f3Ssthen struct module_env;
50933707f3Ssthen struct comm_base;
51933707f3Ssthen struct outside_network;
52933707f3Ssthen struct ub_randstate;
53933707f3Ssthen struct ctx_query;
54933707f3Ssthen struct outbound_entry;
55933707f3Ssthen struct module_qstate;
56933707f3Ssthen struct comm_point;
57933707f3Ssthen struct comm_reply;
58933707f3Ssthen struct regional;
59933707f3Ssthen struct tube;
605d76a658Ssthen struct sldns_buffer;
612ee382b6Ssthen struct ub_event_base;
6277079be7Ssthen struct query_info;
63933707f3Ssthen 
64933707f3Ssthen /**
65933707f3Ssthen  * The library-worker status structure
66933707f3Ssthen  * Internal to the worker.
67933707f3Ssthen  */
68933707f3Ssthen struct libworker {
69933707f3Ssthen 	/** every worker has a unique thread_num. (first in struct) */
70933707f3Ssthen 	int thread_num;
71933707f3Ssthen 	/** context we are operating under */
72933707f3Ssthen 	struct ub_ctx* ctx;
73933707f3Ssthen 
74933707f3Ssthen 	/** is this the bg worker? */
75933707f3Ssthen 	int is_bg;
76933707f3Ssthen 	/** is this a bg worker that is threaded (not forked)? */
77933707f3Ssthen 	int is_bg_thread;
78*20237c55Ssthen 	/** want to quit, stop handling new content */
79*20237c55Ssthen 	int want_quit;
80933707f3Ssthen 
81933707f3Ssthen 	/** copy of the module environment with worker local entries. */
82933707f3Ssthen 	struct module_env* env;
83933707f3Ssthen 	/** the event base this worker works with */
84933707f3Ssthen 	struct comm_base* base;
85933707f3Ssthen 	/** the backside outside network interface to the auth servers */
86933707f3Ssthen 	struct outside_network* back;
87933707f3Ssthen 	/** random() table for this worker. */
88933707f3Ssthen 	struct ub_randstate* rndstate;
89933707f3Ssthen 	/** sslcontext for SSL wrapped DNS over TCP queries */
90933707f3Ssthen 	void* sslctx;
91933707f3Ssthen };
92933707f3Ssthen 
93933707f3Ssthen /**
94933707f3Ssthen  * Create a background worker
95933707f3Ssthen  * @param ctx: is updated with pid/tid of the background worker.
96933707f3Ssthen  *	a new allocation cache is obtained from ctx. It contains the
97933707f3Ssthen  *	threadnumber and unique id for further (shared) cache insertions.
98933707f3Ssthen  * @return 0 if OK, else error.
99933707f3Ssthen  *	Further communication is done via the pipes in ctx.
100933707f3Ssthen  */
101933707f3Ssthen int libworker_bg(struct ub_ctx* ctx);
102933707f3Ssthen 
103933707f3Ssthen /**
104933707f3Ssthen  * Create a foreground worker.
105933707f3Ssthen  * This worker will join the threadpool of resolver threads.
106933707f3Ssthen  * It exits when the query answer has been obtained (or error).
107933707f3Ssthen  * This routine blocks until the worker is finished.
108933707f3Ssthen  * @param ctx: new allocation cache obtained and returned to it.
109933707f3Ssthen  * @param q: query (result is stored in here).
110933707f3Ssthen  * @return 0 if finished OK, else error.
111933707f3Ssthen  */
112933707f3Ssthen int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q);
113933707f3Ssthen 
1145d76a658Ssthen /**
1155d76a658Ssthen  * create worker for event-based interface.
1165d76a658Ssthen  * @param ctx: context with config.
1175d76a658Ssthen  * @param eb: event base.
1185d76a658Ssthen  * @return new worker or NULL.
1195d76a658Ssthen  */
1205d76a658Ssthen struct libworker* libworker_create_event(struct ub_ctx* ctx,
1212ee382b6Ssthen 	struct ub_event_base* eb);
1225d76a658Ssthen 
1235d76a658Ssthen /**
1245d76a658Ssthen  * Attach context_query to mesh for callback in event-driven setup.
1255d76a658Ssthen  * @param ctx: context
1265d76a658Ssthen  * @param q: context query entry
1275d76a658Ssthen  * @param async_id: store query num if query takes long.
1285d76a658Ssthen  * @return 0 if finished OK, else error.
1295d76a658Ssthen  */
1305d76a658Ssthen int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q,
1315d76a658Ssthen 	int* async_id);
1325d76a658Ssthen 
1335d76a658Ssthen /**
1345d76a658Ssthen  * delete worker for event-based interface.  does not free the event_base.
1355d76a658Ssthen  * @param w: event-based worker to delete.
1365d76a658Ssthen  */
1375d76a658Ssthen void libworker_delete_event(struct libworker* w);
1385d76a658Ssthen 
139933707f3Ssthen /** cleanup the cache to remove all rrset IDs from it, arg is libworker */
140933707f3Ssthen void libworker_alloc_cleanup(void* arg);
141933707f3Ssthen 
142933707f3Ssthen /**
143933707f3Ssthen  * fill result from parsed message, on error fills servfail
144933707f3Ssthen  * @param res: is clear at start, filled in at end.
145933707f3Ssthen  * @param buf: contains DNS message.
146933707f3Ssthen  * @param temp: temporary buffer for parse.
147933707f3Ssthen  * @param msg_security: security status of the DNS message.
148933707f3Ssthen  *   On error, the res may contain a different status
149933707f3Ssthen  *   (out of memory is not secure, not bogus).
150933707f3Ssthen  */
1515d76a658Ssthen void libworker_enter_result(struct ub_result* res, struct sldns_buffer* buf,
152933707f3Ssthen 	struct regional* temp, enum sec_status msg_security);
153933707f3Ssthen 
15498f3ca02Sbrad #endif /* LIBUNBOUND_LIBWORKER_H */
155