1*b7579f77SDag-Erling Smørgrav /* 2*b7579f77SDag-Erling Smørgrav * libunbound/worker.h - worker thread or process that resolves 3*b7579f77SDag-Erling Smørgrav * 4*b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5*b7579f77SDag-Erling Smørgrav * 6*b7579f77SDag-Erling Smørgrav * This software is open source. 7*b7579f77SDag-Erling Smørgrav * 8*b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9*b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10*b7579f77SDag-Erling Smørgrav * are met: 11*b7579f77SDag-Erling Smørgrav * 12*b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13*b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14*b7579f77SDag-Erling Smørgrav * 15*b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16*b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17*b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18*b7579f77SDag-Erling Smørgrav * 19*b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20*b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21*b7579f77SDag-Erling Smørgrav * specific prior written permission. 22*b7579f77SDag-Erling Smørgrav * 23*b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24*b7579f77SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25*b7579f77SDag-Erling Smørgrav * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26*b7579f77SDag-Erling Smørgrav * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27*b7579f77SDag-Erling Smørgrav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28*b7579f77SDag-Erling Smørgrav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29*b7579f77SDag-Erling Smørgrav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30*b7579f77SDag-Erling Smørgrav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31*b7579f77SDag-Erling Smørgrav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32*b7579f77SDag-Erling Smørgrav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33*b7579f77SDag-Erling Smørgrav * POSSIBILITY OF SUCH DAMAGE. 34*b7579f77SDag-Erling Smørgrav */ 35*b7579f77SDag-Erling Smørgrav 36*b7579f77SDag-Erling Smørgrav /** 37*b7579f77SDag-Erling Smørgrav * \file 38*b7579f77SDag-Erling Smørgrav * 39*b7579f77SDag-Erling Smørgrav * This file contains the worker process or thread that performs 40*b7579f77SDag-Erling Smørgrav * the DNS resolving and validation. The worker is called by a procedure 41*b7579f77SDag-Erling Smørgrav * and if in the background continues until exit, if in the foreground 42*b7579f77SDag-Erling Smørgrav * returns from the procedure when done. 43*b7579f77SDag-Erling Smørgrav */ 44*b7579f77SDag-Erling Smørgrav #ifndef LIBUNBOUND_WORKER_H 45*b7579f77SDag-Erling Smørgrav #define LIBUNBOUND_WORKER_H 46*b7579f77SDag-Erling Smørgrav #include "util/data/packed_rrset.h" 47*b7579f77SDag-Erling Smørgrav struct ub_ctx; 48*b7579f77SDag-Erling Smørgrav struct ub_result; 49*b7579f77SDag-Erling Smørgrav struct module_env; 50*b7579f77SDag-Erling Smørgrav struct comm_base; 51*b7579f77SDag-Erling Smørgrav struct outside_network; 52*b7579f77SDag-Erling Smørgrav struct ub_randstate; 53*b7579f77SDag-Erling Smørgrav struct ctx_query; 54*b7579f77SDag-Erling Smørgrav struct outbound_entry; 55*b7579f77SDag-Erling Smørgrav struct module_qstate; 56*b7579f77SDag-Erling Smørgrav struct comm_point; 57*b7579f77SDag-Erling Smørgrav struct comm_reply; 58*b7579f77SDag-Erling Smørgrav struct regional; 59*b7579f77SDag-Erling Smørgrav struct tube; 60*b7579f77SDag-Erling Smørgrav 61*b7579f77SDag-Erling Smørgrav /** 62*b7579f77SDag-Erling Smørgrav * The library-worker status structure 63*b7579f77SDag-Erling Smørgrav * Internal to the worker. 64*b7579f77SDag-Erling Smørgrav */ 65*b7579f77SDag-Erling Smørgrav struct libworker { 66*b7579f77SDag-Erling Smørgrav /** every worker has a unique thread_num. (first in struct) */ 67*b7579f77SDag-Erling Smørgrav int thread_num; 68*b7579f77SDag-Erling Smørgrav /** context we are operating under */ 69*b7579f77SDag-Erling Smørgrav struct ub_ctx* ctx; 70*b7579f77SDag-Erling Smørgrav 71*b7579f77SDag-Erling Smørgrav /** is this the bg worker? */ 72*b7579f77SDag-Erling Smørgrav int is_bg; 73*b7579f77SDag-Erling Smørgrav /** is this a bg worker that is threaded (not forked)? */ 74*b7579f77SDag-Erling Smørgrav int is_bg_thread; 75*b7579f77SDag-Erling Smørgrav 76*b7579f77SDag-Erling Smørgrav /** copy of the module environment with worker local entries. */ 77*b7579f77SDag-Erling Smørgrav struct module_env* env; 78*b7579f77SDag-Erling Smørgrav /** the event base this worker works with */ 79*b7579f77SDag-Erling Smørgrav struct comm_base* base; 80*b7579f77SDag-Erling Smørgrav /** the backside outside network interface to the auth servers */ 81*b7579f77SDag-Erling Smørgrav struct outside_network* back; 82*b7579f77SDag-Erling Smørgrav /** random() table for this worker. */ 83*b7579f77SDag-Erling Smørgrav struct ub_randstate* rndstate; 84*b7579f77SDag-Erling Smørgrav /** sslcontext for SSL wrapped DNS over TCP queries */ 85*b7579f77SDag-Erling Smørgrav void* sslctx; 86*b7579f77SDag-Erling Smørgrav }; 87*b7579f77SDag-Erling Smørgrav 88*b7579f77SDag-Erling Smørgrav /** 89*b7579f77SDag-Erling Smørgrav * Create a background worker 90*b7579f77SDag-Erling Smørgrav * @param ctx: is updated with pid/tid of the background worker. 91*b7579f77SDag-Erling Smørgrav * a new allocation cache is obtained from ctx. It contains the 92*b7579f77SDag-Erling Smørgrav * threadnumber and unique id for further (shared) cache insertions. 93*b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 94*b7579f77SDag-Erling Smørgrav * Further communication is done via the pipes in ctx. 95*b7579f77SDag-Erling Smørgrav */ 96*b7579f77SDag-Erling Smørgrav int libworker_bg(struct ub_ctx* ctx); 97*b7579f77SDag-Erling Smørgrav 98*b7579f77SDag-Erling Smørgrav /** 99*b7579f77SDag-Erling Smørgrav * Create a foreground worker. 100*b7579f77SDag-Erling Smørgrav * This worker will join the threadpool of resolver threads. 101*b7579f77SDag-Erling Smørgrav * It exits when the query answer has been obtained (or error). 102*b7579f77SDag-Erling Smørgrav * This routine blocks until the worker is finished. 103*b7579f77SDag-Erling Smørgrav * @param ctx: new allocation cache obtained and returned to it. 104*b7579f77SDag-Erling Smørgrav * @param q: query (result is stored in here). 105*b7579f77SDag-Erling Smørgrav * @return 0 if finished OK, else error. 106*b7579f77SDag-Erling Smørgrav */ 107*b7579f77SDag-Erling Smørgrav int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q); 108*b7579f77SDag-Erling Smørgrav 109*b7579f77SDag-Erling Smørgrav /** cleanup the cache to remove all rrset IDs from it, arg is libworker */ 110*b7579f77SDag-Erling Smørgrav void libworker_alloc_cleanup(void* arg); 111*b7579f77SDag-Erling Smørgrav 112*b7579f77SDag-Erling Smørgrav /** 113*b7579f77SDag-Erling Smørgrav * Worker service routine to send serviced queries to authoritative servers. 114*b7579f77SDag-Erling Smørgrav * @param qname: query name. (host order) 115*b7579f77SDag-Erling Smørgrav * @param qnamelen: length in bytes of qname, including trailing 0. 116*b7579f77SDag-Erling Smørgrav * @param qtype: query type. (host order) 117*b7579f77SDag-Erling Smørgrav * @param qclass: query class. (host order) 118*b7579f77SDag-Erling Smørgrav * @param flags: host order flags word, with opcode and CD bit. 119*b7579f77SDag-Erling Smørgrav * @param dnssec: if set, EDNS record will have DO bit set. 120*b7579f77SDag-Erling Smørgrav * @param want_dnssec: signatures needed. 121*b7579f77SDag-Erling Smørgrav * @param addr: where to. 122*b7579f77SDag-Erling Smørgrav * @param addrlen: length of addr. 123*b7579f77SDag-Erling Smørgrav * @param zone: delegation point name. 124*b7579f77SDag-Erling Smørgrav * @param zonelen: length of zone name wireformat dname. 125*b7579f77SDag-Erling Smørgrav * @param q: wich query state to reactivate upon return. 126*b7579f77SDag-Erling Smørgrav * @return: false on failure (memory or socket related). no query was 127*b7579f77SDag-Erling Smørgrav * sent. 128*b7579f77SDag-Erling Smørgrav */ 129*b7579f77SDag-Erling Smørgrav struct outbound_entry* libworker_send_query(uint8_t* qname, size_t qnamelen, 130*b7579f77SDag-Erling Smørgrav uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec, 131*b7579f77SDag-Erling Smørgrav int want_dnssec, struct sockaddr_storage* addr, socklen_t addrlen, 132*b7579f77SDag-Erling Smørgrav uint8_t* zone, size_t zonelen, struct module_qstate* q); 133*b7579f77SDag-Erling Smørgrav 134*b7579f77SDag-Erling Smørgrav /** process incoming replies from the network */ 135*b7579f77SDag-Erling Smørgrav int libworker_handle_reply(struct comm_point* c, void* arg, int error, 136*b7579f77SDag-Erling Smørgrav struct comm_reply* reply_info); 137*b7579f77SDag-Erling Smørgrav 138*b7579f77SDag-Erling Smørgrav /** process incoming serviced query replies from the network */ 139*b7579f77SDag-Erling Smørgrav int libworker_handle_service_reply(struct comm_point* c, void* arg, int error, 140*b7579f77SDag-Erling Smørgrav struct comm_reply* reply_info); 141*b7579f77SDag-Erling Smørgrav 142*b7579f77SDag-Erling Smørgrav /** handle control command coming into server */ 143*b7579f77SDag-Erling Smørgrav void libworker_handle_control_cmd(struct tube* tube, uint8_t* msg, size_t len, 144*b7579f77SDag-Erling Smørgrav int err, void* arg); 145*b7579f77SDag-Erling Smørgrav 146*b7579f77SDag-Erling Smørgrav /** handle opportunity to write result back */ 147*b7579f77SDag-Erling Smørgrav void libworker_handle_result_write(struct tube* tube, uint8_t* msg, size_t len, 148*b7579f77SDag-Erling Smørgrav int err, void* arg); 149*b7579f77SDag-Erling Smørgrav 150*b7579f77SDag-Erling Smørgrav /** mesh callback with fg results */ 151*b7579f77SDag-Erling Smørgrav void libworker_fg_done_cb(void* arg, int rcode, ldns_buffer* buf, 152*b7579f77SDag-Erling Smørgrav enum sec_status s, char* why_bogus); 153*b7579f77SDag-Erling Smørgrav 154*b7579f77SDag-Erling Smørgrav /** mesh callback with bg results */ 155*b7579f77SDag-Erling Smørgrav void libworker_bg_done_cb(void* arg, int rcode, ldns_buffer* buf, 156*b7579f77SDag-Erling Smørgrav enum sec_status s, char* why_bogus); 157*b7579f77SDag-Erling Smørgrav 158*b7579f77SDag-Erling Smørgrav /** 159*b7579f77SDag-Erling Smørgrav * fill result from parsed message, on error fills servfail 160*b7579f77SDag-Erling Smørgrav * @param res: is clear at start, filled in at end. 161*b7579f77SDag-Erling Smørgrav * @param buf: contains DNS message. 162*b7579f77SDag-Erling Smørgrav * @param temp: temporary buffer for parse. 163*b7579f77SDag-Erling Smørgrav * @param msg_security: security status of the DNS message. 164*b7579f77SDag-Erling Smørgrav * On error, the res may contain a different status 165*b7579f77SDag-Erling Smørgrav * (out of memory is not secure, not bogus). 166*b7579f77SDag-Erling Smørgrav */ 167*b7579f77SDag-Erling Smørgrav void libworker_enter_result(struct ub_result* res, ldns_buffer* buf, 168*b7579f77SDag-Erling Smørgrav struct regional* temp, enum sec_status msg_security); 169*b7579f77SDag-Erling Smørgrav 170*b7579f77SDag-Erling Smørgrav #endif /* LIBUNBOUND_WORKER_H */ 171