libworker.h revision 249140
1/*
2 * libunbound/worker.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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * 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_WORKER_H
45#define LIBUNBOUND_WORKER_H
46#include "util/data/packed_rrset.h"
47struct ub_ctx;
48struct ub_result;
49struct module_env;
50struct comm_base;
51struct outside_network;
52struct ub_randstate;
53struct ctx_query;
54struct outbound_entry;
55struct module_qstate;
56struct comm_point;
57struct comm_reply;
58struct regional;
59struct tube;
60
61/**
62 * The library-worker status structure
63 * Internal to the worker.
64 */
65struct libworker {
66	/** every worker has a unique thread_num. (first in struct) */
67	int thread_num;
68	/** context we are operating under */
69	struct ub_ctx* ctx;
70
71	/** is this the bg worker? */
72	int is_bg;
73	/** is this a bg worker that is threaded (not forked)? */
74	int is_bg_thread;
75
76	/** copy of the module environment with worker local entries. */
77	struct module_env* env;
78	/** the event base this worker works with */
79	struct comm_base* base;
80	/** the backside outside network interface to the auth servers */
81	struct outside_network* back;
82	/** random() table for this worker. */
83	struct ub_randstate* rndstate;
84	/** sslcontext for SSL wrapped DNS over TCP queries */
85	void* sslctx;
86};
87
88/**
89 * Create a background worker
90 * @param ctx: is updated with pid/tid of the background worker.
91 *	a new allocation cache is obtained from ctx. It contains the
92 *	threadnumber and unique id for further (shared) cache insertions.
93 * @return 0 if OK, else error.
94 *	Further communication is done via the pipes in ctx.
95 */
96int libworker_bg(struct ub_ctx* ctx);
97
98/**
99 * Create a foreground worker.
100 * This worker will join the threadpool of resolver threads.
101 * It exits when the query answer has been obtained (or error).
102 * This routine blocks until the worker is finished.
103 * @param ctx: new allocation cache obtained and returned to it.
104 * @param q: query (result is stored in here).
105 * @return 0 if finished OK, else error.
106 */
107int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q);
108
109/** cleanup the cache to remove all rrset IDs from it, arg is libworker */
110void libworker_alloc_cleanup(void* arg);
111
112/**
113 * Worker service routine to send serviced queries to authoritative servers.
114 * @param qname: query name. (host order)
115 * @param qnamelen: length in bytes of qname, including trailing 0.
116 * @param qtype: query type. (host order)
117 * @param qclass: query class. (host order)
118 * @param flags: host order flags word, with opcode and CD bit.
119 * @param dnssec: if set, EDNS record will have DO bit set.
120 * @param want_dnssec: signatures needed.
121 * @param addr: where to.
122 * @param addrlen: length of addr.
123 * @param zone: delegation point name.
124 * @param zonelen: length of zone name wireformat dname.
125 * @param q: wich query state to reactivate upon return.
126 * @return: false on failure (memory or socket related). no query was
127 *      sent.
128 */
129struct outbound_entry* libworker_send_query(uint8_t* qname, size_t qnamelen,
130        uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec,
131	int want_dnssec, struct sockaddr_storage* addr, socklen_t addrlen,
132	uint8_t* zone, size_t zonelen, struct module_qstate* q);
133
134/** process incoming replies from the network */
135int libworker_handle_reply(struct comm_point* c, void* arg, int error,
136        struct comm_reply* reply_info);
137
138/** process incoming serviced query replies from the network */
139int libworker_handle_service_reply(struct comm_point* c, void* arg, int error,
140        struct comm_reply* reply_info);
141
142/** handle control command coming into server */
143void libworker_handle_control_cmd(struct tube* tube, uint8_t* msg, size_t len,
144	int err, void* arg);
145
146/** handle opportunity to write result back */
147void libworker_handle_result_write(struct tube* tube, uint8_t* msg, size_t len,
148	int err, void* arg);
149
150/** mesh callback with fg results */
151void libworker_fg_done_cb(void* arg, int rcode, ldns_buffer* buf,
152	enum sec_status s, char* why_bogus);
153
154/** mesh callback with bg results */
155void libworker_bg_done_cb(void* arg, int rcode, ldns_buffer* buf,
156	enum sec_status s, char* why_bogus);
157
158/**
159 * fill result from parsed message, on error fills servfail
160 * @param res: is clear at start, filled in at end.
161 * @param buf: contains DNS message.
162 * @param temp: temporary buffer for parse.
163 * @param msg_security: security status of the DNS message.
164 *   On error, the res may contain a different status
165 *   (out of memory is not secure, not bogus).
166 */
167void libworker_enter_result(struct ub_result* res, ldns_buffer* buf,
168	struct regional* temp, enum sec_status msg_security);
169
170#endif /* LIBUNBOUND_WORKER_H */
171