1b7579f77SDag-Erling Smørgrav /* 2bc892140SDag-Erling Smørgrav * libunbound/libworker.h - worker thread or process that resolves 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2417d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2517d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2617d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2717d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2817d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2917d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3017d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3117d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3217d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3317d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav 36b7579f77SDag-Erling Smørgrav /** 37b7579f77SDag-Erling Smørgrav * \file 38b7579f77SDag-Erling Smørgrav * 39b7579f77SDag-Erling Smørgrav * This file contains the worker process or thread that performs 40b7579f77SDag-Erling Smørgrav * the DNS resolving and validation. The worker is called by a procedure 41b7579f77SDag-Erling Smørgrav * and if in the background continues until exit, if in the foreground 42b7579f77SDag-Erling Smørgrav * returns from the procedure when done. 43b7579f77SDag-Erling Smørgrav */ 4404b59eacSDag-Erling Smørgrav #ifndef LIBUNBOUND_LIBWORKER_H 4504b59eacSDag-Erling Smørgrav #define LIBUNBOUND_LIBWORKER_H 46b7579f77SDag-Erling Smørgrav #include "util/data/packed_rrset.h" 47b7579f77SDag-Erling Smørgrav struct ub_ctx; 48b7579f77SDag-Erling Smørgrav struct ub_result; 49b7579f77SDag-Erling Smørgrav struct module_env; 50b7579f77SDag-Erling Smørgrav struct comm_base; 51b7579f77SDag-Erling Smørgrav struct outside_network; 52b7579f77SDag-Erling Smørgrav struct ub_randstate; 53b7579f77SDag-Erling Smørgrav struct ctx_query; 54b7579f77SDag-Erling Smørgrav struct outbound_entry; 55b7579f77SDag-Erling Smørgrav struct module_qstate; 56b7579f77SDag-Erling Smørgrav struct comm_point; 57b7579f77SDag-Erling Smørgrav struct comm_reply; 58b7579f77SDag-Erling Smørgrav struct regional; 59b7579f77SDag-Erling Smørgrav struct tube; 6017d15b25SDag-Erling Smørgrav struct sldns_buffer; 61e2d15004SDag-Erling Smørgrav struct ub_event_base; 62bc892140SDag-Erling Smørgrav struct query_info; 63b7579f77SDag-Erling Smørgrav 64b7579f77SDag-Erling Smørgrav /** 65b7579f77SDag-Erling Smørgrav * The library-worker status structure 66b7579f77SDag-Erling Smørgrav * Internal to the worker. 67b7579f77SDag-Erling Smørgrav */ 68b7579f77SDag-Erling Smørgrav struct libworker { 69b7579f77SDag-Erling Smørgrav /** every worker has a unique thread_num. (first in struct) */ 70b7579f77SDag-Erling Smørgrav int thread_num; 71b7579f77SDag-Erling Smørgrav /** context we are operating under */ 72b7579f77SDag-Erling Smørgrav struct ub_ctx* ctx; 73b7579f77SDag-Erling Smørgrav 74b7579f77SDag-Erling Smørgrav /** is this the bg worker? */ 75b7579f77SDag-Erling Smørgrav int is_bg; 76b7579f77SDag-Erling Smørgrav /** is this a bg worker that is threaded (not forked)? */ 77b7579f77SDag-Erling Smørgrav int is_bg_thread; 78*0fb34990SDag-Erling Smørgrav /** want to quit, stop handling new content */ 79*0fb34990SDag-Erling Smørgrav int want_quit; 80b7579f77SDag-Erling Smørgrav 81b7579f77SDag-Erling Smørgrav /** copy of the module environment with worker local entries. */ 82b7579f77SDag-Erling Smørgrav struct module_env* env; 83b7579f77SDag-Erling Smørgrav /** the event base this worker works with */ 84b7579f77SDag-Erling Smørgrav struct comm_base* base; 85b7579f77SDag-Erling Smørgrav /** the backside outside network interface to the auth servers */ 86b7579f77SDag-Erling Smørgrav struct outside_network* back; 87b7579f77SDag-Erling Smørgrav /** random() table for this worker. */ 88b7579f77SDag-Erling Smørgrav struct ub_randstate* rndstate; 89b7579f77SDag-Erling Smørgrav /** sslcontext for SSL wrapped DNS over TCP queries */ 90b7579f77SDag-Erling Smørgrav void* sslctx; 91b7579f77SDag-Erling Smørgrav }; 92b7579f77SDag-Erling Smørgrav 93b7579f77SDag-Erling Smørgrav /** 94b7579f77SDag-Erling Smørgrav * Create a background worker 95b7579f77SDag-Erling Smørgrav * @param ctx: is updated with pid/tid of the background worker. 96b7579f77SDag-Erling Smørgrav * a new allocation cache is obtained from ctx. It contains the 97b7579f77SDag-Erling Smørgrav * threadnumber and unique id for further (shared) cache insertions. 98b7579f77SDag-Erling Smørgrav * @return 0 if OK, else error. 99b7579f77SDag-Erling Smørgrav * Further communication is done via the pipes in ctx. 100b7579f77SDag-Erling Smørgrav */ 101b7579f77SDag-Erling Smørgrav int libworker_bg(struct ub_ctx* ctx); 102b7579f77SDag-Erling Smørgrav 103b7579f77SDag-Erling Smørgrav /** 104b7579f77SDag-Erling Smørgrav * Create a foreground worker. 105b7579f77SDag-Erling Smørgrav * This worker will join the threadpool of resolver threads. 106b7579f77SDag-Erling Smørgrav * It exits when the query answer has been obtained (or error). 107b7579f77SDag-Erling Smørgrav * This routine blocks until the worker is finished. 108b7579f77SDag-Erling Smørgrav * @param ctx: new allocation cache obtained and returned to it. 109b7579f77SDag-Erling Smørgrav * @param q: query (result is stored in here). 110b7579f77SDag-Erling Smørgrav * @return 0 if finished OK, else error. 111b7579f77SDag-Erling Smørgrav */ 112b7579f77SDag-Erling Smørgrav int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q); 113b7579f77SDag-Erling Smørgrav 11417d15b25SDag-Erling Smørgrav /** 11517d15b25SDag-Erling Smørgrav * create worker for event-based interface. 11617d15b25SDag-Erling Smørgrav * @param ctx: context with config. 11717d15b25SDag-Erling Smørgrav * @param eb: event base. 11817d15b25SDag-Erling Smørgrav * @return new worker or NULL. 11917d15b25SDag-Erling Smørgrav */ 12017d15b25SDag-Erling Smørgrav struct libworker* libworker_create_event(struct ub_ctx* ctx, 121e2d15004SDag-Erling Smørgrav struct ub_event_base* eb); 12217d15b25SDag-Erling Smørgrav 12317d15b25SDag-Erling Smørgrav /** 12417d15b25SDag-Erling Smørgrav * Attach context_query to mesh for callback in event-driven setup. 12517d15b25SDag-Erling Smørgrav * @param ctx: context 12617d15b25SDag-Erling Smørgrav * @param q: context query entry 12717d15b25SDag-Erling Smørgrav * @param async_id: store query num if query takes long. 12817d15b25SDag-Erling Smørgrav * @return 0 if finished OK, else error. 12917d15b25SDag-Erling Smørgrav */ 13017d15b25SDag-Erling Smørgrav int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q, 13117d15b25SDag-Erling Smørgrav int* async_id); 13217d15b25SDag-Erling Smørgrav 13317d15b25SDag-Erling Smørgrav /** 13417d15b25SDag-Erling Smørgrav * delete worker for event-based interface. does not free the event_base. 13517d15b25SDag-Erling Smørgrav * @param w: event-based worker to delete. 13617d15b25SDag-Erling Smørgrav */ 13717d15b25SDag-Erling Smørgrav void libworker_delete_event(struct libworker* w); 13817d15b25SDag-Erling Smørgrav 139b7579f77SDag-Erling Smørgrav /** cleanup the cache to remove all rrset IDs from it, arg is libworker */ 140b7579f77SDag-Erling Smørgrav void libworker_alloc_cleanup(void* arg); 141b7579f77SDag-Erling Smørgrav 142b7579f77SDag-Erling Smørgrav /** 143b7579f77SDag-Erling Smørgrav * fill result from parsed message, on error fills servfail 144b7579f77SDag-Erling Smørgrav * @param res: is clear at start, filled in at end. 145b7579f77SDag-Erling Smørgrav * @param buf: contains DNS message. 146b7579f77SDag-Erling Smørgrav * @param temp: temporary buffer for parse. 147b7579f77SDag-Erling Smørgrav * @param msg_security: security status of the DNS message. 148b7579f77SDag-Erling Smørgrav * On error, the res may contain a different status 149b7579f77SDag-Erling Smørgrav * (out of memory is not secure, not bogus). 150b7579f77SDag-Erling Smørgrav */ 15117d15b25SDag-Erling Smørgrav void libworker_enter_result(struct ub_result* res, struct sldns_buffer* buf, 152b7579f77SDag-Erling Smørgrav struct regional* temp, enum sec_status msg_security); 153b7579f77SDag-Erling Smørgrav 15404b59eacSDag-Erling Smørgrav #endif /* LIBUNBOUND_LIBWORKER_H */ 155