1235368Sgnn/*
2235368Sgnn * libunbound/worker.h - worker thread or process that resolves
3235368Sgnn *
4235368Sgnn * Copyright (c) 2007, NLnet Labs. All rights reserved.
5235368Sgnn *
6235368Sgnn * This software is open source.
7235368Sgnn *
8235368Sgnn * Redistribution and use in source and binary forms, with or without
9235368Sgnn * modification, are permitted provided that the following conditions
10235368Sgnn * are met:
11235368Sgnn *
12235368Sgnn * Redistributions of source code must retain the above copyright notice,
13235368Sgnn * this list of conditions and the following disclaimer.
14235368Sgnn *
15235368Sgnn * Redistributions in binary form must reproduce the above copyright notice,
16235368Sgnn * this list of conditions and the following disclaimer in the documentation
17235368Sgnn * and/or other materials provided with the distribution.
18235368Sgnn *
19235368Sgnn * Neither the name of the NLNET LABS nor the names of its contributors may
20235368Sgnn * be used to endorse or promote products derived from this software without
21235368Sgnn * specific prior written permission.
22235368Sgnn *
23235368Sgnn * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24235368Sgnn * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25235368Sgnn * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26235368Sgnn * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27235368Sgnn * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28235368Sgnn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29235368Sgnn * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30235368Sgnn * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31235368Sgnn * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32235368Sgnn * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33235368Sgnn * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34235368Sgnn */
35235368Sgnn
36235368Sgnn/**
37235368Sgnn * \file
38235368Sgnn *
39235368Sgnn * This file contains the worker process or thread that performs
40235368Sgnn * the DNS resolving and validation. The worker is called by a procedure
41235368Sgnn * and if in the background continues until exit, if in the foreground
42235368Sgnn * returns from the procedure when done.
43235368Sgnn */
44235368Sgnn#ifndef LIBUNBOUND_LIBWORKER_H
45235368Sgnn#define LIBUNBOUND_LIBWORKER_H
46235368Sgnn#include "util/data/packed_rrset.h"
47235368Sgnnstruct ub_ctx;
48235368Sgnnstruct ub_result;
49235368Sgnnstruct module_env;
50235368Sgnnstruct comm_base;
51235368Sgnnstruct outside_network;
52235368Sgnnstruct ub_randstate;
53235368Sgnnstruct ctx_query;
54235368Sgnnstruct outbound_entry;
55235368Sgnnstruct module_qstate;
56235368Sgnnstruct comm_point;
57235368Sgnnstruct comm_reply;
58235368Sgnnstruct regional;
59235368Sgnnstruct tube;
60235368Sgnnstruct sldns_buffer;
61235368Sgnnstruct event_base;
62235368Sgnn
63235368Sgnn/**
64235368Sgnn * The library-worker status structure
65235368Sgnn * Internal to the worker.
66235368Sgnn */
67235368Sgnnstruct libworker {
68235368Sgnn	/** every worker has a unique thread_num. (first in struct) */
69235368Sgnn	int thread_num;
70235368Sgnn	/** context we are operating under */
71235368Sgnn	struct ub_ctx* ctx;
72235368Sgnn
73235368Sgnn	/** is this the bg worker? */
74235368Sgnn	int is_bg;
75235368Sgnn	/** is this a bg worker that is threaded (not forked)? */
76235368Sgnn	int is_bg_thread;
77235368Sgnn
78235368Sgnn	/** copy of the module environment with worker local entries. */
79235368Sgnn	struct module_env* env;
80235368Sgnn	/** the event base this worker works with */
81235368Sgnn	struct comm_base* base;
82235368Sgnn	/** the backside outside network interface to the auth servers */
83235368Sgnn	struct outside_network* back;
84235368Sgnn	/** random() table for this worker. */
85235368Sgnn	struct ub_randstate* rndstate;
86235368Sgnn	/** sslcontext for SSL wrapped DNS over TCP queries */
87235368Sgnn	void* sslctx;
88235368Sgnn};
89
90/**
91 * Create a background worker
92 * @param ctx: is updated with pid/tid of the background worker.
93 *	a new allocation cache is obtained from ctx. It contains the
94 *	threadnumber and unique id for further (shared) cache insertions.
95 * @return 0 if OK, else error.
96 *	Further communication is done via the pipes in ctx.
97 */
98int libworker_bg(struct ub_ctx* ctx);
99
100/**
101 * Create a foreground worker.
102 * This worker will join the threadpool of resolver threads.
103 * It exits when the query answer has been obtained (or error).
104 * This routine blocks until the worker is finished.
105 * @param ctx: new allocation cache obtained and returned to it.
106 * @param q: query (result is stored in here).
107 * @return 0 if finished OK, else error.
108 */
109int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q);
110
111/**
112 * create worker for event-based interface.
113 * @param ctx: context with config.
114 * @param eb: event base.
115 * @return new worker or NULL.
116 */
117struct libworker* libworker_create_event(struct ub_ctx* ctx,
118	struct event_base* eb);
119
120/**
121 * Attach context_query to mesh for callback in event-driven setup.
122 * @param ctx: context
123 * @param q: context query entry
124 * @param async_id: store query num if query takes long.
125 * @return 0 if finished OK, else error.
126 */
127int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q,
128	int* async_id);
129
130/**
131 * delete worker for event-based interface.  does not free the event_base.
132 * @param w: event-based worker to delete.
133 */
134void libworker_delete_event(struct libworker* w);
135
136/** cleanup the cache to remove all rrset IDs from it, arg is libworker */
137void libworker_alloc_cleanup(void* arg);
138
139/**
140 * fill result from parsed message, on error fills servfail
141 * @param res: is clear at start, filled in at end.
142 * @param buf: contains DNS message.
143 * @param temp: temporary buffer for parse.
144 * @param msg_security: security status of the DNS message.
145 *   On error, the res may contain a different status
146 *   (out of memory is not secure, not bogus).
147 */
148void libworker_enter_result(struct ub_result* res, struct sldns_buffer* buf,
149	struct regional* temp, enum sec_status msg_security);
150
151#endif /* LIBUNBOUND_LIBWORKER_H */
152