xref: /netbsd-src/external/bsd/unbound/dist/libunbound/libworker.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*
2  * libunbound/libworker.h - worker thread or process that resolves
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 the worker process or thread that performs
40  * the DNS resolving and validation. The worker is called by a procedure
41  * and if in the background continues until exit, if in the foreground
42  * returns from the procedure when done.
43  */
44 #ifndef LIBUNBOUND_LIBWORKER_H
45 #define LIBUNBOUND_LIBWORKER_H
46 #include "util/data/packed_rrset.h"
47 struct ub_ctx;
48 struct ub_result;
49 struct module_env;
50 struct comm_base;
51 struct outside_network;
52 struct ub_randstate;
53 struct ctx_query;
54 struct outbound_entry;
55 struct module_qstate;
56 struct comm_point;
57 struct comm_reply;
58 struct regional;
59 struct tube;
60 struct sldns_buffer;
61 struct ub_event_base;
62 struct query_info;
63 
64 /**
65  * The library-worker status structure
66  * Internal to the worker.
67  */
68 struct libworker {
69 	/** every worker has a unique thread_num. (first in struct) */
70 	int thread_num;
71 	/** context we are operating under */
72 	struct ub_ctx* ctx;
73 
74 	/** is this the bg worker? */
75 	int is_bg;
76 	/** is this a bg worker that is threaded (not forked)? */
77 	int is_bg_thread;
78 
79 	/** copy of the module environment with worker local entries. */
80 	struct module_env* env;
81 	/** the event base this worker works with */
82 	struct comm_base* base;
83 	/** the backside outside network interface to the auth servers */
84 	struct outside_network* back;
85 	/** random() table for this worker. */
86 	struct ub_randstate* rndstate;
87 	/** sslcontext for SSL wrapped DNS over TCP queries */
88 	void* sslctx;
89 };
90 
91 /**
92  * Create a background worker
93  * @param ctx: is updated with pid/tid of the background worker.
94  *	a new allocation cache is obtained from ctx. It contains the
95  *	threadnumber and unique id for further (shared) cache insertions.
96  * @return 0 if OK, else error.
97  *	Further communication is done via the pipes in ctx.
98  */
99 int libworker_bg(struct ub_ctx* ctx);
100 
101 /**
102  * Create a foreground worker.
103  * This worker will join the threadpool of resolver threads.
104  * It exits when the query answer has been obtained (or error).
105  * This routine blocks until the worker is finished.
106  * @param ctx: new allocation cache obtained and returned to it.
107  * @param q: query (result is stored in here).
108  * @return 0 if finished OK, else error.
109  */
110 int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q);
111 
112 /**
113  * create worker for event-based interface.
114  * @param ctx: context with config.
115  * @param eb: event base.
116  * @return new worker or NULL.
117  */
118 struct libworker* libworker_create_event(struct ub_ctx* ctx,
119 	struct ub_event_base* eb);
120 
121 /**
122  * Attach context_query to mesh for callback in event-driven setup.
123  * @param ctx: context
124  * @param q: context query entry
125  * @param async_id: store query num if query takes long.
126  * @return 0 if finished OK, else error.
127  */
128 int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q,
129 	int* async_id);
130 
131 /**
132  * delete worker for event-based interface.  does not free the event_base.
133  * @param w: event-based worker to delete.
134  */
135 void libworker_delete_event(struct libworker* w);
136 
137 /** cleanup the cache to remove all rrset IDs from it, arg is libworker */
138 void libworker_alloc_cleanup(void* arg);
139 
140 /**
141  * fill result from parsed message, on error fills servfail
142  * @param res: is clear at start, filled in at end.
143  * @param buf: contains DNS message.
144  * @param temp: temporary buffer for parse.
145  * @param msg_security: security status of the DNS message.
146  *   On error, the res may contain a different status
147  *   (out of memory is not secure, not bogus).
148  */
149 void libworker_enter_result(struct ub_result* res, struct sldns_buffer* buf,
150 	struct regional* temp, enum sec_status msg_security);
151 
152 #endif /* LIBUNBOUND_LIBWORKER_H */
153