libworker.c File Reference

This file contains the worker process or thread that performs the DNS resolving and validation. More...

#include "config.h"
#include "libunbound/libworker.h"
#include "libunbound/context.h"
#include "libunbound/unbound.h"
#include "libunbound/worker.h"
#include "libunbound/unbound-event.h"
#include "services/outside_network.h"
#include "services/mesh.h"
#include "services/localzone.h"
#include "services/cache/rrset.h"
#include "services/outbound_list.h"
#include "services/authzone.h"
#include "util/fptr_wlist.h"
#include "util/module.h"
#include "util/regional.h"
#include "util/random.h"
#include "util/config_file.h"
#include "util/netevent.h"
#include "util/proxy_protocol.h"
#include "util/storage/lookup3.h"
#include "util/storage/slabhash.h"
#include "util/net_help.h"
#include "util/data/dname.h"
#include "util/data/msgreply.h"
#include "util/data/msgencode.h"
#include "util/tube.h"
#include "sldns/sbuffer.h"
#include "sldns/str2wire.h"

Functions

static void handle_newq (struct libworker *w, uint8_t *buf, uint32_t len)
 handle new query command for bg worker
 
static void libworker_delete_env (struct libworker *w)
 delete libworker env
 
static void libworker_delete (struct libworker *w)
 delete libworker struct
 
void libworker_delete_event (struct libworker *w)
 delete worker for event-based interface. More...
 
static struct libworkerlibworker_setup (struct ub_ctx *ctx, int is_bg, struct ub_event_base *eb)
 setup fresh libworker struct
 
struct libworkerlibworker_create_event (struct ub_ctx *ctx, struct ub_event_base *eb)
 create worker for event-based interface. More...
 
static void handle_cancel (struct libworker *w, uint8_t *buf, uint32_t len)
 handle cancel command for bg worker
 
static void libworker_do_cmd (struct libworker *w, uint8_t *msg, uint32_t len)
 do control command coming into bg server
 
void libworker_handle_control_cmd (struct tube *ATTR_UNUSED(tube), uint8_t *msg, size_t len, int err, void *arg)
 handle control command coming into server
 
static void * libworker_dobg (void *arg)
 the background thread func
 
int libworker_bg (struct ub_ctx *ctx)
 Create a background worker. More...
 
static int fill_canon (struct ub_result *res, uint8_t *s)
 insert canonname
 
static int fill_res (struct ub_result *res, struct ub_packed_rrset_key *answer, uint8_t *finalcname, struct query_info *rq, struct reply_info *rep)
 fill data into result
 
void libworker_enter_result (struct ub_result *res, sldns_buffer *buf, struct regional *temp, enum sec_status msg_security)
 fill result from parsed message, on error fills servfail More...
 
static void libworker_fillup_fg (struct ctx_query *q, int rcode, sldns_buffer *buf, enum sec_status s, char *why_bogus, int was_ratelimited)
 fillup fg results
 
void libworker_fg_done_cb (void *arg, int rcode, sldns_buffer *buf, enum sec_status s, char *why_bogus, int was_ratelimited)
 mesh callback with fg results
 
static int setup_qinfo_edns (struct libworker *w, struct ctx_query *q, struct query_info *qinfo, struct edns_data *edns)
 setup qinfo and edns
 
int libworker_fg (struct ub_ctx *ctx, struct ctx_query *q)
 Create a foreground worker. More...
 
void libworker_event_done_cb (void *arg, int rcode, sldns_buffer *buf, enum sec_status s, char *why_bogus, int was_ratelimited)
 mesh callback with event results
 
int libworker_attach_mesh (struct ub_ctx *ctx, struct ctx_query *q, int *async_id)
 Attach context_query to mesh for callback in event-driven setup. More...
 
static void add_bg_result (struct libworker *w, struct ctx_query *q, sldns_buffer *pkt, int err, char *reason, int was_ratelimited)
 add result to the bg worker result queue
 
void libworker_bg_done_cb (void *arg, int rcode, sldns_buffer *buf, enum sec_status s, char *why_bogus, int was_ratelimited)
 mesh callback with bg results
 
void libworker_alloc_cleanup (void *arg)
 cleanup the cache to remove all rrset IDs from it, arg is libworker
 
struct outbound_entrylibworker_send_query (struct query_info *qinfo, uint16_t flags, int dnssec, int want_dnssec, int nocaps, int check_ratelimit, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen, int tcp_upstream, int ssl_upstream, char *tls_auth_name, struct module_qstate *q, int *was_ratelimited)
 Worker service routine to send serviced queries to authoritative servers. More...
 
int libworker_handle_service_reply (struct comm_point *c, void *arg, int error, struct comm_reply *reply_info)
 process incoming serviced query replies from the network
 
void worker_handle_control_cmd (struct tube *ATTR_UNUSED(tube), uint8_t *ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len), int ATTR_UNUSED(error), void *ATTR_UNUSED(arg))
 
int worker_handle_request (struct comm_point *ATTR_UNUSED(c), void *ATTR_UNUSED(arg), int ATTR_UNUSED(error), struct comm_reply *ATTR_UNUSED(repinfo))
 
int worker_handle_service_reply (struct comm_point *ATTR_UNUSED(c), void *ATTR_UNUSED(arg), int ATTR_UNUSED(error), struct comm_reply *ATTR_UNUSED(reply_info))
 
int remote_accept_callback (struct comm_point *ATTR_UNUSED(c), void *ATTR_UNUSED(arg), int ATTR_UNUSED(error), struct comm_reply *ATTR_UNUSED(repinfo))
 
int remote_control_callback (struct comm_point *ATTR_UNUSED(c), void *ATTR_UNUSED(arg), int ATTR_UNUSED(error), struct comm_reply *ATTR_UNUSED(repinfo))
 
void worker_sighandler (int ATTR_UNUSED(sig), void *ATTR_UNUSED(arg))
 
struct outbound_entryworker_send_query (struct query_info *ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps), int ATTR_UNUSED(check_ratelimit), struct sockaddr_storage *ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen), uint8_t *ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(tcp_upstream), int ATTR_UNUSED(ssl_upstream), char *ATTR_UNUSED(tls_auth_name), struct module_qstate *ATTR_UNUSED(q), int *ATTR_UNUSED(was_ratelimited))
 
void worker_alloc_cleanup (void *ATTR_UNUSED(arg))
 
void worker_stat_timer_cb (void *ATTR_UNUSED(arg))
 
void worker_probe_timer_cb (void *ATTR_UNUSED(arg))
 
void worker_start_accept (void *ATTR_UNUSED(arg))
 
void worker_stop_accept (void *ATTR_UNUSED(arg))
 
int order_lock_cmp (const void *ATTR_UNUSED(e1), const void *ATTR_UNUSED(e2))
 
int codeline_cmp (const void *ATTR_UNUSED(a), const void *ATTR_UNUSED(b))
 
int replay_var_compare (const void *ATTR_UNUSED(a), const void *ATTR_UNUSED(b))
 
void remote_get_opt_ssl (char *ATTR_UNUSED(str), void *ATTR_UNUSED(arg))
 

Detailed Description

This file contains the worker process or thread that performs the DNS resolving and validation.

The worker is called by a procedure and if in the background continues until exit, if in the foreground returns from the procedure when done.

Function Documentation

◆ libworker_delete_event()

void libworker_delete_event ( struct libworker w)

delete worker for event-based interface.

does not free the event_base.

Parameters
wevent-based worker to delete.

References libworker::base, comm_base_delete_no_base(), and libworker_delete_env().

Referenced by ub_ctx_set_event().

◆ libworker_create_event()

struct libworker* libworker_create_event ( struct ub_ctx ctx,
struct ub_event_base eb 
)

create worker for event-based interface.

Parameters
ctxcontext with config.
ebevent base.
Returns
new worker or NULL.

References libworker::ctx, and libworker_setup().

◆ libworker_bg()

int libworker_bg ( struct ub_ctx ctx)

Create a background worker.

Parameters
ctxis updated with pid/tid of the background worker. a new allocation cache is obtained from ctx. It contains the threadnumber and unique id for further (shared) cache insertions.
Returns
0 if OK, else error. Further communication is done via the pipes in ctx.

References ub_ctx::bg_pid, ub_ctx::bg_tid, ub_ctx::cfglock, libworker::ctx, ub_ctx::dothread, libworker::is_bg_thread, libworker_dobg(), libworker_setup(), libworker::thread_num, ub_ctx::thread_worker, UB_FORKFAIL, and UB_NOMEM.

Referenced by ub_resolve_async().

◆ libworker_enter_result()

void libworker_enter_result ( struct ub_result res,
struct sldns_buffer buf,
struct regional temp,
enum sec_status  msg_security 
)

fill result from parsed message, on error fills servfail

Parameters
resis clear at start, filled in at end.
bufcontains DNS message.
temptemporary buffer for parse.
msg_securitysecurity status of the DNS message. On error, the res may contain a different status (out of memory is not secure, not bogus).

References ub_result::rcode.

◆ libworker_fg()

int libworker_fg ( struct ub_ctx ctx,
struct ctx_query q 
)

Create a foreground worker.

This worker will join the threadpool of resolver threads. It exits when the query answer has been obtained (or error). This routine blocks until the worker is finished.

Parameters
ctxnew allocation cache obtained and returned to it.
qquery (result is stored in here).
Returns
0 if finished OK, else error.

References libworker::back, BIT_RD, libworker::ctx, libworker::env, libworker_delete(), libworker_fillup_fg(), libworker_setup(), ub_ctx::local_zones, local_zones_answer(), regional_free_all(), module_env::scratch, setup_qinfo_edns(), sldns_buffer_write_u16_at(), UB_INITFAIL, UB_SYNTAX, outside_network::udp_buff, and ctx_query::w.

Referenced by ub_resolve().

◆ libworker_attach_mesh()

int libworker_attach_mesh ( struct ub_ctx ctx,
struct ctx_query q,
int *  async_id 
)

Attach context_query to mesh for callback in event-driven setup.

Parameters
ctxcontext
qcontext query entry
async_idstore query num if query takes long.
Returns
0 if finished OK, else error.

References libworker::back, BIT_RD, libworker::ctx, libworker::env, ub_ctx::event_worker, libworker_event_done_cb(), ub_ctx::local_zones, local_zones_answer(), query_info::qname, regional_free_all(), module_env::scratch, setup_qinfo_edns(), sldns_buffer_write_u16_at(), UB_INITFAIL, UB_SYNTAX, outside_network::udp_buff, and ctx_query::w.

◆ libworker_send_query()

struct outbound_entry* libworker_send_query ( struct query_info qinfo,
uint16_t  flags,
int  dnssec,
int  want_dnssec,
int  nocaps,
int  check_ratelimit,
struct sockaddr_storage *  addr,
socklen_t  addrlen,
uint8_t *  zone,
size_t  zonelen,
int  tcp_upstream,
int  ssl_upstream,
char *  tls_auth_name,
struct module_qstate q,
int *  was_ratelimited 
)

Worker service routine to send serviced queries to authoritative servers.

Parameters
qinfoquery info.
flagshost order flags word, with opcode and CD bit.
dnssecif set, EDNS record will have DO bit set.
want_dnssecsignatures needed.
nocapsignore capsforid(if in config), do not perturb qname.
check_ratelimitif set, will check ratelimit before sending out.
addrwhere to.
addrlenlength of addr.
zonedelegation point name.
zonelenlength of zone name wireformat dname.
tcp_upstreamuse TCP for upstream queries.
ssl_upstreamuse SSL for upstream queries.
tls_auth_nameif ssl_upstream, use this name with TLS authentication.
qwhich query state to reactivate upon return.
was_ratelimitedit will signal back if the query failed to pass the ratelimit check.
Returns
: false on failure (memory or socket related). no query was sent.

References libworker::back, module_qstate::env, libworker_handle_service_reply(), outnet_serviced_query(), module_qstate::region, regional_alloc(), outside_network::udp_buff, and module_env::worker.