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 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_WORKER_H 45 #define LIBUNBOUND_WORKER_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 event_base; 62 63 /** 64 * The library-worker status structure 65 * Internal to the worker. 66 */ 67 struct libworker { 68 /** every worker has a unique thread_num. (first in struct) */ 69 int thread_num; 70 /** context we are operating under */ 71 struct ub_ctx* ctx; 72 73 /** is this the bg worker? */ 74 int is_bg; 75 /** is this a bg worker that is threaded (not forked)? */ 76 int is_bg_thread; 77 78 /** copy of the module environment with worker local entries. */ 79 struct module_env* env; 80 /** the event base this worker works with */ 81 struct comm_base* base; 82 /** the backside outside network interface to the auth servers */ 83 struct outside_network* back; 84 /** random() table for this worker. */ 85 struct ub_randstate* rndstate; 86 /** sslcontext for SSL wrapped DNS over TCP queries */ 87 void* sslctx; 88 }; 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 */ 98 int 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 */ 109 int 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 */ 117 struct 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 */ 127 int 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 */ 134 void libworker_delete_event(struct libworker* w); 135 136 /** cleanup the cache to remove all rrset IDs from it, arg is libworker */ 137 void libworker_alloc_cleanup(void* arg); 138 139 /** 140 * Worker service routine to send serviced queries to authoritative servers. 141 * @param qname: query name. (host order) 142 * @param qnamelen: length in bytes of qname, including trailing 0. 143 * @param qtype: query type. (host order) 144 * @param qclass: query class. (host order) 145 * @param flags: host order flags word, with opcode and CD bit. 146 * @param dnssec: if set, EDNS record will have DO bit set. 147 * @param want_dnssec: signatures needed. 148 * @param addr: where to. 149 * @param addrlen: length of addr. 150 * @param zone: delegation point name. 151 * @param zonelen: length of zone name wireformat dname. 152 * @param q: wich query state to reactivate upon return. 153 * @return: false on failure (memory or socket related). no query was 154 * sent. 155 */ 156 struct outbound_entry* libworker_send_query(uint8_t* qname, size_t qnamelen, 157 uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec, 158 int want_dnssec, struct sockaddr_storage* addr, socklen_t addrlen, 159 uint8_t* zone, size_t zonelen, struct module_qstate* q); 160 161 /** process incoming replies from the network */ 162 int libworker_handle_reply(struct comm_point* c, void* arg, int error, 163 struct comm_reply* reply_info); 164 165 /** process incoming serviced query replies from the network */ 166 int libworker_handle_service_reply(struct comm_point* c, void* arg, int error, 167 struct comm_reply* reply_info); 168 169 /** handle control command coming into server */ 170 void libworker_handle_control_cmd(struct tube* tube, uint8_t* msg, size_t len, 171 int err, void* arg); 172 173 /** handle opportunity to write result back */ 174 void libworker_handle_result_write(struct tube* tube, uint8_t* msg, size_t len, 175 int err, void* arg); 176 177 /** mesh callback with fg results */ 178 void libworker_fg_done_cb(void* arg, int rcode, struct sldns_buffer* buf, 179 enum sec_status s, char* why_bogus); 180 181 /** mesh callback with bg results */ 182 void libworker_bg_done_cb(void* arg, int rcode, struct sldns_buffer* buf, 183 enum sec_status s, char* why_bogus); 184 185 /** mesh callback with event results */ 186 void libworker_event_done_cb(void* arg, int rcode, struct sldns_buffer* buf, 187 enum sec_status s, char* why_bogus); 188 189 /** 190 * fill result from parsed message, on error fills servfail 191 * @param res: is clear at start, filled in at end. 192 * @param buf: contains DNS message. 193 * @param temp: temporary buffer for parse. 194 * @param msg_security: security status of the DNS message. 195 * On error, the res may contain a different status 196 * (out of memory is not secure, not bogus). 197 */ 198 void libworker_enter_result(struct ub_result* res, struct sldns_buffer* buf, 199 struct regional* temp, enum sec_status msg_security); 200 201 #endif /* LIBUNBOUND_WORKER_H */ 202