2007-01-30 11:36:46 -05:00
|
|
|
/*
|
|
|
|
|
* daemon/worker.c - worker that handles a pending list of requests.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2007, NLnet Labs. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This software is open source.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* Neither the name of the NLNET LABS nor the names of its contributors may
|
|
|
|
|
* be used to endorse or promote products derived from this software without
|
|
|
|
|
* specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
*
|
|
|
|
|
* This file implements the worker that handles callbacks on events, for
|
|
|
|
|
* pending requests.
|
|
|
|
|
*/
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "util/log.h"
|
2007-02-08 11:49:48 -05:00
|
|
|
#include "util/net_help.h"
|
2007-02-26 04:42:05 -05:00
|
|
|
#include "util/random.h"
|
2007-01-30 11:36:46 -05:00
|
|
|
#include "daemon/worker.h"
|
2007-03-09 08:37:57 -05:00
|
|
|
#include "daemon/daemon.h"
|
2007-01-30 11:36:46 -05:00
|
|
|
#include "util/netevent.h"
|
2007-02-22 08:36:29 -05:00
|
|
|
#include "util/config_file.h"
|
2007-05-22 08:36:02 -04:00
|
|
|
#include "util/module.h"
|
2007-05-04 08:35:01 -04:00
|
|
|
#include "util/region-allocator.h"
|
2007-03-26 06:33:41 -04:00
|
|
|
#include "util/storage/slabhash.h"
|
2007-01-30 11:36:46 -05:00
|
|
|
#include "services/listen_dnsport.h"
|
2007-01-31 10:38:44 -05:00
|
|
|
#include "services/outside_network.h"
|
2007-05-22 08:36:02 -04:00
|
|
|
#include "services/outbound_list.h"
|
2007-05-16 08:48:48 -04:00
|
|
|
#include "services/cache/rrset.h"
|
2007-05-07 09:17:27 -04:00
|
|
|
#include "util/data/msgparse.h"
|
2007-06-01 08:25:38 -04:00
|
|
|
#include "util/data/msgencode.h"
|
2007-01-31 10:38:44 -05:00
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <netdb.h>
|
2007-02-05 11:46:40 -05:00
|
|
|
#include <signal.h>
|
2007-01-31 10:38:44 -05:00
|
|
|
|
2007-06-19 10:46:14 -04:00
|
|
|
/** Size of an UDP datagram */
|
|
|
|
|
#define NORMAL_UDP_SIZE 512 /* bytes */
|
|
|
|
|
|
2007-02-26 09:49:11 -05:00
|
|
|
void
|
|
|
|
|
worker_send_cmd(struct worker* worker, ldns_buffer* buffer,
|
|
|
|
|
enum worker_commands cmd)
|
|
|
|
|
{
|
|
|
|
|
ldns_buffer_clear(buffer);
|
|
|
|
|
/* like DNS message, length data */
|
|
|
|
|
ldns_buffer_write_u16(buffer, sizeof(uint32_t));
|
|
|
|
|
ldns_buffer_write_u32(buffer, (uint32_t)cmd);
|
|
|
|
|
ldns_buffer_flip(buffer);
|
|
|
|
|
if(!write_socket(worker->cmd_send_fd, ldns_buffer_begin(buffer),
|
|
|
|
|
ldns_buffer_limit(buffer)))
|
|
|
|
|
log_err("write socket: %s", strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-24 09:24:44 -04:00
|
|
|
/** delete subrequest */
|
|
|
|
|
static void
|
2007-06-05 06:51:47 -04:00
|
|
|
qstate_cleanup(struct worker* worker, struct module_qstate* qstate)
|
2007-05-24 09:24:44 -04:00
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
if(!qstate)
|
|
|
|
|
return;
|
|
|
|
|
/* call de-init while all is OK */
|
|
|
|
|
for(i=0; i<worker->daemon->num_modules; i++)
|
|
|
|
|
(*worker->daemon->modfunc[i]->clear)(qstate, i);
|
|
|
|
|
/* cleanup this query */
|
|
|
|
|
region_free_all(qstate->region);
|
|
|
|
|
query_info_clear(&qstate->qinfo);
|
|
|
|
|
if(qstate->parent) {
|
2007-06-05 06:51:47 -04:00
|
|
|
/* subquery of parent */
|
|
|
|
|
module_subreq_remove(&qstate->parent->subquery_first, qstate);
|
|
|
|
|
region_destroy(qstate->region);
|
|
|
|
|
free(qstate);
|
|
|
|
|
} else if (!qstate->work_info) {
|
|
|
|
|
/* slumbering query */
|
|
|
|
|
module_subreq_remove(&worker->slumber_list, qstate);
|
2007-05-30 07:18:17 -04:00
|
|
|
region_destroy(qstate->region);
|
2007-05-30 07:45:44 -04:00
|
|
|
free(qstate);
|
2007-06-05 06:51:47 -04:00
|
|
|
verbose(VERB_ALGO, "cleanup: slumber list has %d entries",
|
|
|
|
|
module_subreq_num(worker->slumber_list));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** delete subrequest recursively */
|
|
|
|
|
static void
|
|
|
|
|
qstate_free_recurs_list(struct worker* worker, struct module_qstate* list)
|
|
|
|
|
{
|
|
|
|
|
struct module_qstate* n;
|
|
|
|
|
/* remove subqueries */
|
|
|
|
|
while(list) {
|
|
|
|
|
n = list->subquery_next;
|
|
|
|
|
qstate_free_recurs_list(worker, list->subquery_first);
|
|
|
|
|
qstate_cleanup(worker, list);
|
|
|
|
|
list = n;
|
2007-05-24 09:24:44 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-05 06:51:47 -04:00
|
|
|
/** delete subrequest */
|
|
|
|
|
static void
|
|
|
|
|
qstate_free(struct worker* worker, struct module_qstate* qstate)
|
|
|
|
|
{
|
|
|
|
|
if(!qstate)
|
|
|
|
|
return;
|
2007-06-15 05:13:54 -04:00
|
|
|
worker_slumber_subqueries(qstate);
|
2007-06-05 06:51:47 -04:00
|
|
|
qstate_cleanup(worker, qstate);
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-27 11:21:21 -04:00
|
|
|
/** release workrequest back to the freelist,
|
|
|
|
|
* note that the w->qinfo still needs to be cleared after this.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
req_release(struct work_query* w)
|
|
|
|
|
{
|
2007-05-11 10:16:42 -04:00
|
|
|
struct worker* worker = w->state.env->worker;
|
|
|
|
|
if(worker->num_requests == worker->request_size) {
|
2007-03-27 11:21:21 -04:00
|
|
|
/* no longer at max, start accepting again. */
|
2007-05-11 10:16:42 -04:00
|
|
|
listen_resume(worker->front);
|
2007-03-27 11:21:21 -04:00
|
|
|
}
|
2007-05-11 10:16:42 -04:00
|
|
|
log_assert(worker->num_requests >= 1);
|
|
|
|
|
worker->num_requests --;
|
|
|
|
|
w->next = worker->free_queries;
|
|
|
|
|
worker->free_queries = w;
|
2007-06-20 05:17:09 -04:00
|
|
|
verbose(VERB_ALGO, "released query to pool, %d in use",
|
|
|
|
|
(int)worker->num_requests);
|
2007-03-27 11:21:21 -04:00
|
|
|
}
|
|
|
|
|
|
2007-05-03 11:34:03 -04:00
|
|
|
/** create error and fill into buffer */
|
|
|
|
|
static void
|
|
|
|
|
replyerror_fillbuf(int r, struct comm_reply* repinfo, uint16_t id,
|
|
|
|
|
uint16_t qflags, struct query_info* qinfo)
|
2007-02-02 08:44:00 -05:00
|
|
|
{
|
2007-05-03 11:34:03 -04:00
|
|
|
ldns_buffer* buf = repinfo->c->buffer;
|
2007-03-22 12:26:14 -04:00
|
|
|
uint16_t flags;
|
|
|
|
|
verbose(VERB_DETAIL, "reply with error");
|
|
|
|
|
|
|
|
|
|
ldns_buffer_clear(buf);
|
2007-05-03 11:34:03 -04:00
|
|
|
ldns_buffer_write(buf, &id, sizeof(uint16_t));
|
2007-06-19 09:50:43 -04:00
|
|
|
flags = (uint16_t)(BIT_QR | BIT_RA | r); /* QR and retcode*/
|
2007-05-03 11:34:03 -04:00
|
|
|
flags |= (qflags & (BIT_RD|BIT_CD)); /* copy RD and CD bit */
|
2007-03-22 12:26:14 -04:00
|
|
|
ldns_buffer_write_u16(buf, flags);
|
|
|
|
|
flags = 1;
|
|
|
|
|
ldns_buffer_write_u16(buf, flags);
|
|
|
|
|
flags = 0;
|
|
|
|
|
ldns_buffer_write(buf, &flags, sizeof(uint16_t));
|
|
|
|
|
ldns_buffer_write(buf, &flags, sizeof(uint16_t));
|
|
|
|
|
ldns_buffer_write(buf, &flags, sizeof(uint16_t));
|
2007-05-29 08:31:02 -04:00
|
|
|
ldns_buffer_write(buf, qinfo->qname, qinfo->qname_len);
|
2007-05-03 11:34:03 -04:00
|
|
|
ldns_buffer_write_u16(buf, qinfo->qtype);
|
|
|
|
|
ldns_buffer_write_u16(buf, qinfo->qclass);
|
2007-03-22 12:26:14 -04:00
|
|
|
ldns_buffer_flip(buf);
|
2007-05-03 11:34:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** reply to query with given error code */
|
|
|
|
|
static void
|
|
|
|
|
replyerror(int r, struct work_query* w)
|
|
|
|
|
{
|
2007-05-11 10:16:42 -04:00
|
|
|
w->state.edns.edns_version = EDNS_ADVERTISED_VERSION;
|
|
|
|
|
w->state.edns.udp_size = EDNS_ADVERTISED_SIZE;
|
|
|
|
|
w->state.edns.ext_rcode = 0;
|
|
|
|
|
w->state.edns.bits &= EDNS_DO;
|
|
|
|
|
replyerror_fillbuf(r, &w->query_reply, w->query_id,
|
|
|
|
|
w->state.query_flags, &w->state.qinfo);
|
|
|
|
|
attach_edns_record(w->query_reply.c->buffer, &w->state.edns);
|
2007-03-27 11:21:21 -04:00
|
|
|
comm_point_send_reply(&w->query_reply);
|
|
|
|
|
req_release(w);
|
2007-05-11 10:16:42 -04:00
|
|
|
query_info_clear(&w->state.qinfo);
|
2007-05-04 06:10:52 -04:00
|
|
|
}
|
|
|
|
|
|
2007-05-30 07:18:17 -04:00
|
|
|
/** init qstate module states */
|
|
|
|
|
static void
|
|
|
|
|
set_extstates_initial(struct worker* worker, struct module_qstate* qstate)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for(i=0; i<worker->daemon->num_modules; i++)
|
|
|
|
|
qstate->ext_state[i] = module_state_initial;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-07 06:38:02 -04:00
|
|
|
/** recursive debug logging of (sub)query structure */
|
|
|
|
|
static void
|
|
|
|
|
run_debug(struct module_qstate* p, int d)
|
|
|
|
|
{
|
|
|
|
|
char buf[80+1+1]; /* max nn=80; marker is 1, zero at end is 1 */
|
|
|
|
|
int i, nn = d*2;
|
|
|
|
|
if(nn > 80)
|
|
|
|
|
nn = 80;
|
|
|
|
|
for(i=0; i<nn; i++) {
|
|
|
|
|
buf[i] = ' ';
|
|
|
|
|
}
|
|
|
|
|
buf[i++] = 'o';
|
|
|
|
|
buf[i] = 0;
|
2007-06-07 09:21:04 -04:00
|
|
|
log_nametypeclass(VERB_ALGO, buf, p->qinfo.qname, p->qinfo.qtype,
|
|
|
|
|
p->qinfo.qclass);
|
2007-06-07 06:38:02 -04:00
|
|
|
for(p = p->subquery_first; p; p = p->subquery_next) {
|
|
|
|
|
run_debug(p, d+1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-30 10:55:40 -04:00
|
|
|
/** find runnable recursive */
|
|
|
|
|
static struct module_qstate*
|
|
|
|
|
find_run_in(struct module_qstate* p)
|
|
|
|
|
{
|
|
|
|
|
struct module_qstate* q;
|
|
|
|
|
for(p = p->subquery_first; p; p = p->subquery_next) {
|
|
|
|
|
if(p->ext_state[p->curmod] == module_state_initial)
|
|
|
|
|
return p;
|
|
|
|
|
if((q=find_run_in(p)))
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** find other runnable subqueries */
|
|
|
|
|
static struct module_qstate*
|
|
|
|
|
find_runnable(struct module_qstate* subq)
|
|
|
|
|
{
|
|
|
|
|
struct module_qstate* p = subq;
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_ALGO, "find runnable");
|
2007-05-30 10:55:40 -04:00
|
|
|
if(p->subquery_next && p->subquery_next->ext_state[
|
|
|
|
|
p->subquery_next->curmod] == module_state_initial)
|
|
|
|
|
return p->subquery_next;
|
|
|
|
|
while(p->parent)
|
|
|
|
|
p = p->parent;
|
2007-06-07 06:38:02 -04:00
|
|
|
if(verbosity >= VERB_ALGO)
|
|
|
|
|
run_debug(p, 0);
|
2007-05-30 10:55:40 -04:00
|
|
|
return find_run_in(p);
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-11 10:16:42 -04:00
|
|
|
/** process incoming request */
|
|
|
|
|
static void
|
|
|
|
|
worker_process_query(struct worker* worker, struct work_query* w,
|
2007-05-24 09:24:44 -04:00
|
|
|
struct module_qstate* qstate, enum module_ev event,
|
|
|
|
|
struct outbound_entry* entry)
|
2007-05-03 11:34:03 -04:00
|
|
|
{
|
2007-05-24 09:24:44 -04:00
|
|
|
enum module_ext_state s;
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_DETAIL, "worker process handle event");
|
2007-05-11 10:16:42 -04:00
|
|
|
if(event == module_event_new) {
|
2007-05-24 09:24:44 -04:00
|
|
|
qstate->curmod = 0;
|
2007-05-30 07:18:17 -04:00
|
|
|
set_extstates_initial(worker, qstate);
|
2007-05-11 10:16:42 -04:00
|
|
|
}
|
|
|
|
|
/* allow current module to run */
|
2007-05-30 07:18:17 -04:00
|
|
|
/* loops for subqueries or parent queries. */
|
|
|
|
|
while(1) {
|
|
|
|
|
(*worker->daemon->modfunc[qstate->curmod]->operate)(qstate,
|
|
|
|
|
event, qstate->curmod, entry);
|
|
|
|
|
region_free_all(worker->scratchpad);
|
2007-06-04 07:52:10 -04:00
|
|
|
qstate->reply = NULL;
|
2007-05-30 07:18:17 -04:00
|
|
|
s = qstate->ext_state[qstate->curmod];
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_ALGO, "worker_process_query: module "
|
|
|
|
|
"exit state is %s", strextstate(s));
|
|
|
|
|
if(s == module_state_initial) {
|
|
|
|
|
log_err("module exit in initial state, "
|
2007-06-15 08:11:44 -04:00
|
|
|
"it loops; parent query is aborted");
|
|
|
|
|
while(qstate->parent)
|
|
|
|
|
qstate = qstate->parent;
|
2007-06-07 09:21:04 -04:00
|
|
|
s = module_error;
|
|
|
|
|
}
|
2007-05-30 07:18:17 -04:00
|
|
|
/* examine results, start further modules, etc. */
|
2007-06-07 09:21:04 -04:00
|
|
|
if(s != module_error && s != module_finished) {
|
|
|
|
|
/* see if we can continue with other subrequests */
|
|
|
|
|
struct module_qstate* nxt = find_runnable(qstate);
|
|
|
|
|
if(nxt) {
|
2007-05-30 07:18:17 -04:00
|
|
|
/* start submodule */
|
2007-06-07 09:21:04 -04:00
|
|
|
qstate = nxt;
|
2007-05-30 07:18:17 -04:00
|
|
|
set_extstates_initial(worker, qstate);
|
|
|
|
|
entry = NULL;
|
2007-06-07 09:21:04 -04:00
|
|
|
event = module_event_pass;
|
2007-05-30 07:18:17 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* subrequest done */
|
|
|
|
|
if(s == module_error && qstate->parent) {
|
|
|
|
|
struct module_qstate* up = qstate->parent;
|
|
|
|
|
qstate_free(worker, qstate);
|
|
|
|
|
qstate = up;
|
|
|
|
|
entry = NULL;
|
2007-06-05 06:51:47 -04:00
|
|
|
event = module_event_subq_error;
|
2007-05-30 07:18:17 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(s == module_finished && qstate->parent) {
|
|
|
|
|
struct module_qstate* up = qstate->parent;
|
|
|
|
|
qstate_free(worker, qstate);
|
|
|
|
|
qstate = up;
|
|
|
|
|
entry = NULL;
|
|
|
|
|
event = module_event_subq_done;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2007-05-24 09:24:44 -04:00
|
|
|
}
|
|
|
|
|
/* request done */
|
|
|
|
|
if(s == module_error) {
|
2007-06-05 06:51:47 -04:00
|
|
|
if(w) {
|
|
|
|
|
replyerror(LDNS_RCODE_SERVFAIL, w);
|
|
|
|
|
}
|
2007-05-24 09:41:30 -04:00
|
|
|
qstate_free(worker, qstate);
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_DETAIL, "worker process suspend");
|
2007-05-11 10:16:42 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2007-05-24 09:24:44 -04:00
|
|
|
if(s == module_finished) {
|
2007-06-05 06:51:47 -04:00
|
|
|
if(w) {
|
|
|
|
|
memcpy(ldns_buffer_begin(w->query_reply.c->buffer),
|
|
|
|
|
&w->query_id, sizeof(w->query_id));
|
|
|
|
|
comm_point_send_reply(&w->query_reply);
|
|
|
|
|
req_release(w);
|
|
|
|
|
}
|
2007-05-24 09:24:44 -04:00
|
|
|
qstate_free(worker, qstate);
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_DETAIL, "worker process suspend");
|
2007-05-11 10:16:42 -04:00
|
|
|
return;
|
2007-05-03 11:34:03 -04:00
|
|
|
}
|
2007-05-11 10:16:42 -04:00
|
|
|
/* suspend, waits for wakeup callback */
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_DETAIL, "worker process suspend");
|
2007-05-03 11:34:03 -04:00
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:44:00 -05:00
|
|
|
/** process incoming replies from the network */
|
2007-02-06 11:26:19 -05:00
|
|
|
static int
|
|
|
|
|
worker_handle_reply(struct comm_point* c, void* arg, int error,
|
2007-05-11 10:16:42 -04:00
|
|
|
struct comm_reply* reply_info)
|
2007-02-02 08:44:00 -05:00
|
|
|
{
|
2007-03-27 11:21:21 -04:00
|
|
|
struct work_query* w = (struct work_query*)arg;
|
2007-05-11 10:16:42 -04:00
|
|
|
struct worker* worker = w->state.env->worker;
|
2007-05-03 11:34:03 -04:00
|
|
|
|
2007-05-11 10:16:42 -04:00
|
|
|
w->state.reply = reply_info;
|
2007-02-02 08:44:00 -05:00
|
|
|
if(error != 0) {
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, &w->state,
|
|
|
|
|
module_event_timeout, NULL);
|
2007-02-02 08:44:00 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-03-23 09:12:49 -04:00
|
|
|
/* sanity check. */
|
2007-05-11 10:16:42 -04:00
|
|
|
if(!LDNS_QR_WIRE(ldns_buffer_begin(c->buffer))
|
|
|
|
|
|| LDNS_OPCODE_WIRE(ldns_buffer_begin(c->buffer)) !=
|
|
|
|
|
LDNS_PACKET_QUERY
|
|
|
|
|
|| LDNS_QDCOUNT(ldns_buffer_begin(c->buffer)) > 1) {
|
|
|
|
|
/* error becomes timeout for the module as if this reply
|
|
|
|
|
* never arrived. */
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, &w->state,
|
|
|
|
|
module_event_timeout, NULL);
|
2007-05-07 11:01:27 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, &w->state, module_event_reply, NULL);
|
2007-05-22 08:36:02 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** process incoming serviced query replies from the network */
|
|
|
|
|
static int
|
|
|
|
|
worker_handle_service_reply(struct comm_point* c, void* arg, int error,
|
|
|
|
|
struct comm_reply* reply_info)
|
|
|
|
|
{
|
|
|
|
|
struct outbound_entry* e = (struct outbound_entry*)arg;
|
|
|
|
|
struct work_query* w = e->qstate->work_info;
|
2007-06-05 06:51:47 -04:00
|
|
|
struct worker* worker = e->qstate->env->worker;
|
2007-05-22 08:36:02 -04:00
|
|
|
|
2007-06-04 07:52:10 -04:00
|
|
|
e->qstate->reply = reply_info;
|
2007-05-22 08:36:02 -04:00
|
|
|
if(error != 0) {
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, e->qstate,
|
|
|
|
|
module_event_timeout, e);
|
2007-05-22 08:36:02 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* sanity check. */
|
|
|
|
|
if(!LDNS_QR_WIRE(ldns_buffer_begin(c->buffer))
|
|
|
|
|
|| LDNS_OPCODE_WIRE(ldns_buffer_begin(c->buffer)) !=
|
|
|
|
|
LDNS_PACKET_QUERY
|
|
|
|
|
|| LDNS_QDCOUNT(ldns_buffer_begin(c->buffer)) > 1) {
|
|
|
|
|
/* error becomes timeout for the module as if this reply
|
|
|
|
|
* never arrived. */
|
2007-06-20 09:01:30 -04:00
|
|
|
verbose(VERB_ALGO, "worker: bad reply handled as timeout");
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, e->qstate,
|
|
|
|
|
module_event_timeout, e);
|
2007-05-22 08:36:02 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, e->qstate, module_event_reply, e);
|
2007-02-02 08:44:00 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-01-30 11:36:46 -05:00
|
|
|
|
2007-01-31 06:57:22 -05:00
|
|
|
/** check request sanity. Returns error code, 0 OK, or -1 discard.
|
|
|
|
|
* @param pkt: the wire packet to examine for sanity.
|
2007-06-19 10:46:14 -04:00
|
|
|
* @param worker: parameters for checking.
|
2007-01-31 06:57:22 -05:00
|
|
|
*/
|
2007-02-06 11:26:19 -05:00
|
|
|
static int
|
2007-06-19 10:46:14 -04:00
|
|
|
worker_check_request(ldns_buffer* pkt, struct worker* worker)
|
2007-01-31 04:32:30 -05:00
|
|
|
{
|
|
|
|
|
if(ldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
|
|
|
|
|
verbose(VERB_DETAIL, "request too short, discarded");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-06-19 10:46:14 -04:00
|
|
|
if(ldns_buffer_limit(pkt) > NORMAL_UDP_SIZE &&
|
|
|
|
|
worker->daemon->cfg->harden_large_queries) {
|
|
|
|
|
verbose(VERB_DETAIL, "request too large, discarded");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-01-31 04:32:30 -05:00
|
|
|
if(LDNS_QR_WIRE(ldns_buffer_begin(pkt))) {
|
|
|
|
|
verbose(VERB_DETAIL, "request has QR bit on, discarded");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if(LDNS_OPCODE_WIRE(ldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY) {
|
|
|
|
|
verbose(VERB_DETAIL, "request unknown opcode %d",
|
|
|
|
|
LDNS_OPCODE_WIRE(ldns_buffer_begin(pkt)));
|
|
|
|
|
return LDNS_RCODE_NOTIMPL;
|
|
|
|
|
}
|
|
|
|
|
if(LDNS_QDCOUNT(ldns_buffer_begin(pkt)) != 1) {
|
|
|
|
|
verbose(VERB_DETAIL, "request wrong nr qd=%d",
|
|
|
|
|
LDNS_QDCOUNT(ldns_buffer_begin(pkt)));
|
|
|
|
|
return LDNS_RCODE_FORMERR;
|
|
|
|
|
}
|
|
|
|
|
if(LDNS_ANCOUNT(ldns_buffer_begin(pkt)) != 0) {
|
|
|
|
|
verbose(VERB_DETAIL, "request wrong nr an=%d",
|
|
|
|
|
LDNS_ANCOUNT(ldns_buffer_begin(pkt)));
|
|
|
|
|
return LDNS_RCODE_FORMERR;
|
|
|
|
|
}
|
|
|
|
|
if(LDNS_NSCOUNT(ldns_buffer_begin(pkt)) != 0) {
|
|
|
|
|
verbose(VERB_DETAIL, "request wrong nr ns=%d",
|
|
|
|
|
LDNS_NSCOUNT(ldns_buffer_begin(pkt)));
|
|
|
|
|
return LDNS_RCODE_FORMERR;
|
|
|
|
|
}
|
2007-05-07 09:17:27 -04:00
|
|
|
if(LDNS_ARCOUNT(ldns_buffer_begin(pkt)) > 1) {
|
2007-01-31 04:32:30 -05:00
|
|
|
verbose(VERB_DETAIL, "request wrong nr ar=%d",
|
|
|
|
|
LDNS_ARCOUNT(ldns_buffer_begin(pkt)));
|
|
|
|
|
return LDNS_RCODE_FORMERR;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-12 10:51:49 -04:00
|
|
|
/** process control messages from the main thread.
|
|
|
|
|
* @param c: comm point to read from.
|
|
|
|
|
* @param arg: worker.
|
|
|
|
|
* @param error: error status of comm point.
|
|
|
|
|
* @param reply_info: not used.
|
|
|
|
|
*/
|
2007-02-26 09:49:11 -05:00
|
|
|
static int
|
|
|
|
|
worker_handle_control_cmd(struct comm_point* c, void* arg, int error,
|
|
|
|
|
struct comm_reply* ATTR_UNUSED(reply_info))
|
|
|
|
|
{
|
|
|
|
|
struct worker* worker = (struct worker*)arg;
|
|
|
|
|
enum worker_commands cmd;
|
|
|
|
|
if(error != NETEVENT_NOERROR) {
|
|
|
|
|
if(error == NETEVENT_CLOSED)
|
|
|
|
|
comm_base_exit(worker->base);
|
2007-02-27 09:28:20 -05:00
|
|
|
else log_info("control event: %d", error);
|
2007-02-26 09:49:11 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if(ldns_buffer_limit(c->buffer) != sizeof(uint32_t)) {
|
|
|
|
|
fatal_exit("bad control msg length %d",
|
|
|
|
|
(int)ldns_buffer_limit(c->buffer));
|
|
|
|
|
}
|
|
|
|
|
cmd = ldns_buffer_read_u32(c->buffer);
|
|
|
|
|
switch(cmd) {
|
|
|
|
|
case worker_cmd_quit:
|
2007-02-27 09:28:20 -05:00
|
|
|
verbose(VERB_ALGO, "got control cmd quit");
|
2007-02-26 09:49:11 -05:00
|
|
|
comm_base_exit(worker->base);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
log_err("bad command %d", (int)cmd);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-03 11:34:03 -04:00
|
|
|
/** answer query from the cache */
|
|
|
|
|
static int
|
2007-05-04 08:35:01 -04:00
|
|
|
answer_from_cache(struct worker* worker, struct lruhash_entry* e, uint16_t id,
|
2007-05-07 09:17:27 -04:00
|
|
|
uint16_t flags, struct comm_reply* repinfo, struct edns_data* edns)
|
2007-05-03 11:34:03 -04:00
|
|
|
{
|
|
|
|
|
struct msgreply_entry* mrentry = (struct msgreply_entry*)e->key;
|
|
|
|
|
struct reply_info* rep = (struct reply_info*)e->data;
|
|
|
|
|
uint32_t timenow = time(0);
|
2007-05-07 09:17:27 -04:00
|
|
|
uint16_t udpsize = edns->udp_size;
|
2007-05-03 11:34:03 -04:00
|
|
|
/* see if it is possible */
|
|
|
|
|
if(rep->ttl <= timenow) {
|
2007-05-16 08:48:48 -04:00
|
|
|
/* the rrsets may have been updated in the meantime.
|
|
|
|
|
* we will refetch the message format from the
|
|
|
|
|
* authoritative server
|
|
|
|
|
*/
|
2007-05-03 11:34:03 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-07 09:17:27 -04:00
|
|
|
edns->edns_version = EDNS_ADVERTISED_VERSION;
|
|
|
|
|
edns->udp_size = EDNS_ADVERTISED_SIZE;
|
|
|
|
|
edns->ext_rcode = 0;
|
|
|
|
|
edns->bits &= EDNS_DO;
|
2007-05-29 06:32:11 -04:00
|
|
|
if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
|
2007-05-16 08:48:48 -04:00
|
|
|
return 0;
|
2007-05-03 11:34:03 -04:00
|
|
|
/* locked and ids and ttls are OK. */
|
|
|
|
|
if(!reply_info_answer_encode(&mrentry->key, rep, id, flags,
|
2007-05-07 09:17:27 -04:00
|
|
|
repinfo->c->buffer, timenow, 1, worker->scratchpad,
|
2007-06-11 06:12:43 -04:00
|
|
|
udpsize, edns, (int)(edns->bits & EDNS_DO) )) {
|
2007-05-03 11:34:03 -04:00
|
|
|
replyerror_fillbuf(LDNS_RCODE_SERVFAIL, repinfo, id,
|
|
|
|
|
flags, &mrentry->key);
|
|
|
|
|
}
|
2007-05-21 11:10:55 -04:00
|
|
|
/* cannot send the reply right now, because blocking network syscall
|
|
|
|
|
* is bad while holding locks. */
|
2007-05-29 06:32:11 -04:00
|
|
|
rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
|
|
|
|
|
rep->ref, rep->rrset_count);
|
2007-05-04 08:35:01 -04:00
|
|
|
region_free_all(worker->scratchpad);
|
2007-05-03 11:34:03 -04:00
|
|
|
/* go and return this buffer to the client */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-30 11:36:46 -05:00
|
|
|
/** handles callbacks from listening event interface */
|
2007-02-06 11:26:19 -05:00
|
|
|
static int
|
|
|
|
|
worker_handle_request(struct comm_point* c, void* arg, int error,
|
2007-01-31 04:32:30 -05:00
|
|
|
struct comm_reply* repinfo)
|
2007-01-30 11:36:46 -05:00
|
|
|
{
|
2007-01-31 04:32:30 -05:00
|
|
|
struct worker* worker = (struct worker*)arg;
|
|
|
|
|
int ret;
|
2007-03-22 12:26:14 -04:00
|
|
|
hashvalue_t h;
|
|
|
|
|
struct lruhash_entry* e;
|
2007-03-27 11:21:21 -04:00
|
|
|
struct query_info qinfo;
|
|
|
|
|
struct work_query* w;
|
2007-05-07 09:17:27 -04:00
|
|
|
struct edns_data edns;
|
2007-03-27 11:21:21 -04:00
|
|
|
|
2007-02-06 11:26:19 -05:00
|
|
|
if(error != NETEVENT_NOERROR) {
|
2007-06-11 06:12:43 -04:00
|
|
|
log_err("handle request called with err=%d", error);
|
2007-01-31 04:32:30 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-06-19 10:46:14 -04:00
|
|
|
if((ret=worker_check_request(c->buffer, worker)) != 0) {
|
2007-06-11 06:12:43 -04:00
|
|
|
verbose(VERB_ALGO, "worker check request: bad query.");
|
2007-01-31 04:32:30 -05:00
|
|
|
if(ret != -1) {
|
2007-02-02 08:44:00 -05:00
|
|
|
LDNS_QR_SET(ldns_buffer_begin(c->buffer));
|
2007-01-31 04:32:30 -05:00
|
|
|
LDNS_RCODE_SET(ldns_buffer_begin(c->buffer), ret);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2007-02-07 10:53:51 -05:00
|
|
|
comm_point_drop_reply(repinfo);
|
2007-01-31 04:32:30 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-01 08:13:29 -04:00
|
|
|
worker->stats.num_queries++;
|
2007-03-22 12:26:14 -04:00
|
|
|
/* see if query is in the cache */
|
2007-03-27 11:21:21 -04:00
|
|
|
if(!query_info_parse(&qinfo, c->buffer)) {
|
2007-06-11 06:12:43 -04:00
|
|
|
verbose(VERB_ALGO, "worker parse request: formerror.");
|
2007-03-22 12:26:14 -04:00
|
|
|
LDNS_QR_SET(ldns_buffer_begin(c->buffer));
|
|
|
|
|
LDNS_RCODE_SET(ldns_buffer_begin(c->buffer),
|
|
|
|
|
LDNS_RCODE_FORMERR);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2007-06-19 08:34:54 -04:00
|
|
|
if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
|
|
|
|
|
qinfo.qtype == LDNS_RR_TYPE_IXFR) {
|
|
|
|
|
verbose(VERB_ALGO, "worker request: refused zone transfer.");
|
|
|
|
|
LDNS_QR_SET(ldns_buffer_begin(c->buffer));
|
|
|
|
|
LDNS_RCODE_SET(ldns_buffer_begin(c->buffer),
|
|
|
|
|
LDNS_RCODE_REFUSED);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2007-03-27 11:21:21 -04:00
|
|
|
h = query_info_hash(&qinfo);
|
2007-05-07 09:17:27 -04:00
|
|
|
if((ret=parse_edns_from_pkt(c->buffer, &edns)) != 0) {
|
2007-06-11 06:12:43 -04:00
|
|
|
verbose(VERB_ALGO, "worker parse edns: formerror.");
|
2007-05-07 09:17:27 -04:00
|
|
|
LDNS_QR_SET(ldns_buffer_begin(c->buffer));
|
|
|
|
|
LDNS_RCODE_SET(ldns_buffer_begin(c->buffer), ret);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(edns.edns_present && edns.edns_version != 0) {
|
2007-05-07 10:07:34 -04:00
|
|
|
edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
|
2007-05-07 10:05:51 -04:00
|
|
|
edns.edns_version = EDNS_ADVERTISED_VERSION;
|
|
|
|
|
edns.udp_size = EDNS_ADVERTISED_SIZE;
|
|
|
|
|
edns.bits &= EDNS_DO;
|
2007-06-11 06:12:43 -04:00
|
|
|
verbose(VERB_ALGO, "query with bad edns version.");
|
2007-05-07 10:05:51 -04:00
|
|
|
replyerror_fillbuf(EDNS_RCODE_BADVERS&0xf, repinfo,
|
|
|
|
|
*(uint16_t*)ldns_buffer_begin(c->buffer),
|
|
|
|
|
ldns_buffer_read_u16_at(c->buffer, 2), &qinfo);
|
|
|
|
|
attach_edns_record(c->buffer, &edns);
|
2007-05-07 09:17:27 -04:00
|
|
|
return 1;
|
|
|
|
|
}
|
2007-06-19 10:46:14 -04:00
|
|
|
if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
|
|
|
|
|
worker->daemon->cfg->harden_short_bufsize) {
|
|
|
|
|
verbose(VERB_DETAIL, "worker request: EDNS bufsize %d ignored",
|
|
|
|
|
(int)edns.udp_size);
|
|
|
|
|
edns.udp_size = NORMAL_UDP_SIZE;
|
|
|
|
|
}
|
2007-06-19 09:50:43 -04:00
|
|
|
if(edns.edns_present && edns.udp_size < LDNS_HEADER_SIZE) {
|
|
|
|
|
verbose(VERB_ALGO, "worker request: edns is too small.");
|
|
|
|
|
LDNS_QR_SET(ldns_buffer_begin(c->buffer));
|
|
|
|
|
LDNS_TC_SET(ldns_buffer_begin(c->buffer));
|
|
|
|
|
LDNS_RCODE_SET(ldns_buffer_begin(c->buffer),
|
|
|
|
|
LDNS_RCODE_SERVFAIL);
|
|
|
|
|
ldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
|
|
|
|
|
ldns_buffer_flip(c->buffer);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2007-05-07 09:17:27 -04:00
|
|
|
if(c->type != comm_udp)
|
|
|
|
|
edns.udp_size = 65535; /* max size for TCP replies */
|
2007-05-16 08:48:48 -04:00
|
|
|
if((e=slabhash_lookup(worker->env.msg_cache, h, &qinfo, 0))) {
|
2007-04-02 06:16:02 -04:00
|
|
|
/* answer from cache - we have acquired a readlock on it */
|
2007-05-04 08:35:01 -04:00
|
|
|
if(answer_from_cache(worker, e,
|
2007-05-03 11:34:03 -04:00
|
|
|
*(uint16_t*)ldns_buffer_begin(c->buffer),
|
2007-05-07 09:17:27 -04:00
|
|
|
ldns_buffer_read_u16_at(c->buffer, 2), repinfo,
|
|
|
|
|
&edns)) {
|
2007-05-03 11:34:03 -04:00
|
|
|
lock_rw_unlock(&e->lock);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2007-06-07 09:21:04 -04:00
|
|
|
verbose(VERB_DETAIL, "answer from the cache -- data has timed out");
|
2007-03-23 04:25:43 -04:00
|
|
|
lock_rw_unlock(&e->lock);
|
2007-03-22 12:26:14 -04:00
|
|
|
}
|
|
|
|
|
ldns_buffer_rewind(c->buffer);
|
2007-05-01 08:13:29 -04:00
|
|
|
server_stats_querymiss(&worker->stats, worker);
|
2007-03-27 11:21:21 -04:00
|
|
|
/* perform memory allocation(s) */
|
|
|
|
|
if(!query_info_allocqname(&qinfo)) {
|
|
|
|
|
comm_point_drop_reply(repinfo);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-03-22 12:26:14 -04:00
|
|
|
|
2007-03-27 11:21:21 -04:00
|
|
|
/* grab a work request structure for this new request */
|
2007-04-02 06:16:02 -04:00
|
|
|
if(!(w = worker->free_queries)) {
|
2007-03-27 11:21:21 -04:00
|
|
|
/* we could get this due to a slow tcp incoming query,
|
|
|
|
|
that started before we performed listen_pushback */
|
|
|
|
|
verbose(VERB_DETAIL, "worker: too many incoming requests "
|
|
|
|
|
"active. dropping incoming query.");
|
2007-06-20 05:17:09 -04:00
|
|
|
verbose(VERB_ALGO, "currently servicing %d of %d queries",
|
|
|
|
|
(int)worker->num_requests, (int)worker->request_size);
|
2007-05-01 08:13:29 -04:00
|
|
|
worker->stats.num_query_list_exceeded++;
|
2007-03-27 11:21:21 -04:00
|
|
|
comm_point_drop_reply(repinfo);
|
2007-04-02 09:58:02 -04:00
|
|
|
query_info_clear(&qinfo);
|
2007-03-27 11:21:21 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-11 10:16:42 -04:00
|
|
|
w->state.edns = edns;
|
2007-03-27 11:21:21 -04:00
|
|
|
worker->free_queries = w->next;
|
2007-01-31 04:32:30 -05:00
|
|
|
worker->num_requests ++;
|
2007-03-27 11:21:21 -04:00
|
|
|
log_assert(worker->num_requests <= worker->request_size);
|
|
|
|
|
if(worker->num_requests == worker->request_size) {
|
2007-02-27 06:25:44 -05:00
|
|
|
/* the max request number has been reached, stop accepting */
|
|
|
|
|
listen_pushback(worker->front);
|
|
|
|
|
}
|
2007-03-27 11:21:21 -04:00
|
|
|
|
|
|
|
|
/* init request */
|
|
|
|
|
w->next = NULL;
|
2007-05-11 10:16:42 -04:00
|
|
|
w->state.query_hash = h;
|
2007-03-27 11:21:21 -04:00
|
|
|
memcpy(&w->query_reply, repinfo, sizeof(struct comm_reply));
|
2007-05-11 10:16:42 -04:00
|
|
|
memcpy(&w->state.qinfo, &qinfo, sizeof(struct query_info));
|
2007-03-29 06:00:10 -04:00
|
|
|
memcpy(&w->query_id, ldns_buffer_begin(c->buffer), sizeof(uint16_t));
|
2007-05-11 10:16:42 -04:00
|
|
|
w->state.query_flags = ldns_buffer_read_u16_at(c->buffer, 2);
|
2007-03-27 11:21:21 -04:00
|
|
|
|
|
|
|
|
/* answer it */
|
2007-05-11 10:16:42 -04:00
|
|
|
w->state.buf = c->buffer;
|
2007-05-24 09:24:44 -04:00
|
|
|
worker_process_query(worker, w, &w->state, module_event_new, NULL);
|
2007-01-30 11:36:46 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-05 11:46:40 -05:00
|
|
|
/** worker signal callback */
|
2007-02-06 11:26:19 -05:00
|
|
|
void
|
|
|
|
|
worker_sighandler(int sig, void* arg)
|
2007-02-05 11:46:40 -05:00
|
|
|
{
|
|
|
|
|
/* note that log, print, syscalls here give race conditions. */
|
|
|
|
|
struct worker* worker = (struct worker*)arg;
|
|
|
|
|
switch(sig) {
|
|
|
|
|
case SIGHUP:
|
|
|
|
|
log_info("caught signal SIGHUP");
|
2007-02-23 05:04:50 -05:00
|
|
|
worker->need_to_restart = 1;
|
2007-02-05 11:46:40 -05:00
|
|
|
comm_base_exit(worker->base);
|
|
|
|
|
break;
|
|
|
|
|
case SIGINT:
|
|
|
|
|
log_info("caught signal SIGINT");
|
2007-04-02 06:16:02 -04:00
|
|
|
worker->need_to_restart = 0;
|
2007-02-05 11:46:40 -05:00
|
|
|
comm_base_exit(worker->base);
|
|
|
|
|
break;
|
|
|
|
|
case SIGQUIT:
|
|
|
|
|
log_info("caught signal SIGQUIT");
|
2007-04-02 06:16:02 -04:00
|
|
|
worker->need_to_restart = 0;
|
2007-02-05 11:46:40 -05:00
|
|
|
comm_base_exit(worker->base);
|
|
|
|
|
break;
|
2007-02-26 11:11:32 -05:00
|
|
|
case SIGTERM:
|
|
|
|
|
log_info("caught signal SIGTERM");
|
2007-04-02 06:16:02 -04:00
|
|
|
worker->need_to_restart = 0;
|
2007-02-26 11:11:32 -05:00
|
|
|
comm_base_exit(worker->base);
|
|
|
|
|
break;
|
2007-02-05 11:46:40 -05:00
|
|
|
default:
|
|
|
|
|
log_err("unknown signal: %d, ignored", sig);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-06 11:26:19 -05:00
|
|
|
struct worker*
|
2007-02-26 09:49:11 -05:00
|
|
|
worker_create(struct daemon* daemon, int id)
|
2007-01-30 11:36:46 -05:00
|
|
|
{
|
2007-01-31 04:32:30 -05:00
|
|
|
struct worker* worker = (struct worker*)calloc(1,
|
|
|
|
|
sizeof(struct worker));
|
2007-01-30 11:36:46 -05:00
|
|
|
if(!worker)
|
|
|
|
|
return NULL;
|
2007-02-26 09:49:11 -05:00
|
|
|
worker->daemon = daemon;
|
|
|
|
|
worker->thread_num = id;
|
|
|
|
|
worker->cmd_send_fd = -1;
|
|
|
|
|
worker->cmd_recv_fd = -1;
|
2007-02-26 11:18:35 -05:00
|
|
|
if(id != 0) {
|
|
|
|
|
int sv[2];
|
|
|
|
|
/* create socketpair to communicate with worker */
|
|
|
|
|
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
|
|
|
|
|
free(worker);
|
|
|
|
|
log_err("socketpair: %s", strerror(errno));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2007-03-01 10:50:57 -05:00
|
|
|
if(!fd_set_nonblock(sv[0]) || !fd_set_nonblock(sv[1])) {
|
|
|
|
|
close(sv[0]);
|
|
|
|
|
close(sv[1]);
|
|
|
|
|
free(worker);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2007-02-26 11:18:35 -05:00
|
|
|
worker->cmd_send_fd = sv[0];
|
|
|
|
|
worker->cmd_recv_fd = sv[1];
|
2007-02-26 09:49:11 -05:00
|
|
|
}
|
|
|
|
|
return worker;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-27 11:21:21 -04:00
|
|
|
/** create request handling structures */
|
|
|
|
|
static int
|
|
|
|
|
reqs_init(struct worker* worker)
|
|
|
|
|
{
|
2007-03-28 09:43:50 -04:00
|
|
|
size_t i;
|
2007-03-27 11:21:21 -04:00
|
|
|
for(i=0; i<worker->request_size; i++) {
|
|
|
|
|
struct work_query* q = (struct work_query*)calloc(1,
|
|
|
|
|
sizeof(struct work_query));
|
|
|
|
|
if(!q) return 0;
|
2007-05-11 10:16:42 -04:00
|
|
|
q->state.buf = worker->front->udp_buff;
|
|
|
|
|
q->state.scratch = worker->scratchpad;
|
|
|
|
|
q->state.region = region_create_custom(malloc, free, 1024,
|
|
|
|
|
64, 16, 0);
|
|
|
|
|
if(!q->state.region) {
|
|
|
|
|
free(q);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
q->state.env = &worker->env;
|
2007-06-07 09:21:04 -04:00
|
|
|
q->state.parent = NULL;
|
2007-05-11 10:16:42 -04:00
|
|
|
q->state.work_info = q;
|
2007-03-27 11:21:21 -04:00
|
|
|
q->next = worker->free_queries;
|
|
|
|
|
worker->free_queries = q;
|
2007-03-28 09:43:50 -04:00
|
|
|
q->all_next = worker->all_queries;
|
|
|
|
|
worker->all_queries = q;
|
2007-03-27 11:21:21 -04:00
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-28 09:43:50 -04:00
|
|
|
/** delete request list */
|
|
|
|
|
static void
|
|
|
|
|
reqs_delete(struct worker* worker)
|
|
|
|
|
{
|
|
|
|
|
struct work_query* q = worker->all_queries;
|
|
|
|
|
struct work_query* n;
|
|
|
|
|
while(q) {
|
|
|
|
|
n = q->all_next;
|
2007-05-11 10:16:42 -04:00
|
|
|
log_assert(q->state.env->worker == worker);
|
2007-03-28 09:43:50 -04:00
|
|
|
/* comm_reply closed in outside_network_delete */
|
2007-06-05 06:51:47 -04:00
|
|
|
qstate_free_recurs_list(worker, q->state.subquery_first);
|
|
|
|
|
qstate_cleanup(worker, &q->state);
|
2007-05-11 10:16:42 -04:00
|
|
|
region_destroy(q->state.region);
|
2007-03-28 09:43:50 -04:00
|
|
|
free(q);
|
|
|
|
|
q = n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-26 09:49:11 -05:00
|
|
|
int
|
|
|
|
|
worker_init(struct worker* worker, struct config_file *cfg,
|
|
|
|
|
struct listen_port* ports, size_t buffer_size, int do_sigs)
|
|
|
|
|
{
|
|
|
|
|
unsigned int seed;
|
|
|
|
|
int startport;
|
2007-02-23 05:04:50 -05:00
|
|
|
worker->need_to_restart = 0;
|
2007-01-30 11:36:46 -05:00
|
|
|
worker->base = comm_base_create();
|
|
|
|
|
if(!worker->base) {
|
|
|
|
|
log_err("could not create event handling base");
|
2007-02-05 11:46:40 -05:00
|
|
|
worker_delete(worker);
|
2007-02-26 09:49:11 -05:00
|
|
|
return 0;
|
2007-02-05 11:46:40 -05:00
|
|
|
}
|
2007-02-26 09:49:11 -05:00
|
|
|
if(do_sigs) {
|
2007-03-01 10:50:57 -05:00
|
|
|
ub_thread_sig_unblock(SIGHUP);
|
|
|
|
|
ub_thread_sig_unblock(SIGINT);
|
|
|
|
|
ub_thread_sig_unblock(SIGQUIT);
|
|
|
|
|
ub_thread_sig_unblock(SIGTERM);
|
2007-03-02 04:48:31 -05:00
|
|
|
#ifndef LIBEVENT_SIGNAL_PROBLEM
|
2007-02-26 09:49:11 -05:00
|
|
|
worker->comsig = comm_signal_create(worker->base,
|
|
|
|
|
worker_sighandler, worker);
|
|
|
|
|
if(!worker->comsig || !comm_signal_bind(worker->comsig, SIGHUP)
|
|
|
|
|
|| !comm_signal_bind(worker->comsig, SIGINT)
|
2007-02-26 11:11:32 -05:00
|
|
|
|| !comm_signal_bind(worker->comsig, SIGTERM)
|
2007-02-26 09:49:11 -05:00
|
|
|
|| !comm_signal_bind(worker->comsig, SIGQUIT)) {
|
|
|
|
|
log_err("could not create signal handlers");
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-03-02 04:48:31 -05:00
|
|
|
#endif /* LIBEVENT_SIGNAL_PROBLEM */
|
2007-02-26 09:49:11 -05:00
|
|
|
} else { /* !do_sigs */
|
|
|
|
|
worker->comsig = 0;
|
2007-01-30 11:36:46 -05:00
|
|
|
}
|
2007-05-21 11:10:55 -04:00
|
|
|
/* init random(), large table size. */
|
|
|
|
|
if(!(worker->rndstate = (struct ub_randstate*)calloc(1,
|
|
|
|
|
sizeof(struct ub_randstate)))) {
|
|
|
|
|
log_err("malloc rndtable failed.");
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
|
|
|
|
|
(unsigned int)worker->thread_num;
|
|
|
|
|
if(!ub_initstate(seed, worker->rndstate, RND_STATE_SIZE)) {
|
|
|
|
|
log_err("could not init random numbers.");
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-02-23 05:04:50 -05:00
|
|
|
worker->front = listen_create(worker->base, ports,
|
2007-02-22 08:36:29 -05:00
|
|
|
buffer_size, worker_handle_request, worker);
|
2007-01-30 11:36:46 -05:00
|
|
|
if(!worker->front) {
|
|
|
|
|
log_err("could not create listening sockets");
|
2007-02-06 11:26:19 -05:00
|
|
|
worker_delete(worker);
|
2007-02-26 09:49:11 -05:00
|
|
|
return 0;
|
2007-01-30 11:36:46 -05:00
|
|
|
}
|
2007-02-26 09:49:11 -05:00
|
|
|
startport = cfg->outgoing_base_port +
|
|
|
|
|
cfg->outgoing_num_ports * worker->thread_num;
|
2007-01-31 10:38:44 -05:00
|
|
|
worker->back = outside_network_create(worker->base,
|
2007-02-23 06:00:55 -05:00
|
|
|
buffer_size, (size_t)cfg->outgoing_num_ports, cfg->ifs,
|
2007-05-08 09:25:21 -04:00
|
|
|
cfg->num_ifs, cfg->do_ip4, cfg->do_ip6, startport,
|
2007-05-21 11:10:55 -04:00
|
|
|
cfg->do_tcp?cfg->outgoing_num_tcp:0,
|
|
|
|
|
worker->daemon->env->infra_cache, worker->rndstate);
|
2007-01-31 10:38:44 -05:00
|
|
|
if(!worker->back) {
|
|
|
|
|
log_err("could not create outgoing sockets");
|
2007-02-06 11:26:19 -05:00
|
|
|
worker_delete(worker);
|
2007-02-26 09:49:11 -05:00
|
|
|
return 0;
|
2007-01-31 10:38:44 -05:00
|
|
|
}
|
2007-06-20 10:01:58 -04:00
|
|
|
outside_network_set_secondary_buffer(worker->back,
|
|
|
|
|
worker->front->udp_buff);
|
2007-02-26 11:18:35 -05:00
|
|
|
if(worker->thread_num != 0) {
|
|
|
|
|
/* start listening to commands */
|
|
|
|
|
if(!(worker->cmd_com=comm_point_create_local(worker->base,
|
|
|
|
|
worker->cmd_recv_fd, buffer_size,
|
|
|
|
|
worker_handle_control_cmd, worker))) {
|
|
|
|
|
log_err("could not create control compt.");
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-02-26 09:49:11 -05:00
|
|
|
}
|
2007-05-11 10:16:42 -04:00
|
|
|
worker->scratchpad = region_create_custom(malloc, free,
|
|
|
|
|
65536, 8192, 32, 1);
|
|
|
|
|
if(!worker->scratchpad) {
|
|
|
|
|
log_err("malloc failure");
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-03-28 09:43:50 -04:00
|
|
|
worker->request_size = cfg->num_queries_per_thread;
|
2007-03-27 11:21:21 -04:00
|
|
|
if(!reqs_init(worker)) {
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-06-05 06:51:47 -04:00
|
|
|
worker->slumber_list = NULL;
|
2007-02-26 09:49:11 -05:00
|
|
|
|
2007-05-01 08:13:29 -04:00
|
|
|
server_stats_init(&worker->stats);
|
2007-04-16 11:21:50 -04:00
|
|
|
alloc_init(&worker->alloc, &worker->daemon->superalloc,
|
|
|
|
|
worker->thread_num);
|
2007-05-11 10:16:42 -04:00
|
|
|
worker->env = *worker->daemon->env;
|
|
|
|
|
worker->env.worker = worker;
|
|
|
|
|
worker->env.alloc = &worker->alloc;
|
2007-06-04 05:57:02 -04:00
|
|
|
worker->env.rnd = worker->rndstate;
|
2007-02-26 09:49:11 -05:00
|
|
|
return 1;
|
2007-01-30 11:36:46 -05:00
|
|
|
}
|
|
|
|
|
|
2007-02-06 11:26:19 -05:00
|
|
|
void
|
|
|
|
|
worker_work(struct worker* worker)
|
2007-01-30 11:36:46 -05:00
|
|
|
{
|
|
|
|
|
comm_base_dispatch(worker->base);
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-06 11:26:19 -05:00
|
|
|
void
|
|
|
|
|
worker_delete(struct worker* worker)
|
2007-01-30 11:36:46 -05:00
|
|
|
{
|
|
|
|
|
if(!worker)
|
|
|
|
|
return;
|
2007-05-01 08:13:29 -04:00
|
|
|
server_stats_log(&worker->stats, worker->thread_num);
|
2007-03-28 09:43:50 -04:00
|
|
|
reqs_delete(worker);
|
2007-06-05 06:51:47 -04:00
|
|
|
qstate_free_recurs_list(worker, worker->slumber_list);
|
2007-01-30 11:36:46 -05:00
|
|
|
listen_delete(worker->front);
|
2007-01-31 10:38:44 -05:00
|
|
|
outside_network_delete(worker->back);
|
2007-02-05 11:46:40 -05:00
|
|
|
comm_signal_delete(worker->comsig);
|
2007-02-26 11:11:32 -05:00
|
|
|
comm_point_delete(worker->cmd_com);
|
2007-01-30 11:36:46 -05:00
|
|
|
comm_base_delete(worker->base);
|
2007-02-27 05:33:04 -05:00
|
|
|
ub_randfree(worker->rndstate);
|
2007-02-22 11:22:54 -05:00
|
|
|
free(worker->rndstate);
|
2007-03-01 10:50:57 -05:00
|
|
|
/* close fds after deleting commpoints, to be sure.
|
|
|
|
|
Also epoll does not like closing fd before event_del */
|
|
|
|
|
if(worker->cmd_send_fd != -1)
|
|
|
|
|
close(worker->cmd_send_fd);
|
|
|
|
|
worker->cmd_send_fd = -1;
|
|
|
|
|
if(worker->cmd_recv_fd != -1)
|
|
|
|
|
close(worker->cmd_recv_fd);
|
|
|
|
|
worker->cmd_recv_fd = -1;
|
2007-03-09 08:37:57 -05:00
|
|
|
alloc_clear(&worker->alloc);
|
2007-05-04 08:35:01 -04:00
|
|
|
region_destroy(worker->scratchpad);
|
2007-01-30 11:36:46 -05:00
|
|
|
free(worker);
|
|
|
|
|
}
|
2007-01-31 10:38:44 -05:00
|
|
|
|
2007-02-06 11:26:19 -05:00
|
|
|
int
|
2007-05-22 08:36:02 -04:00
|
|
|
worker_send_packet(ldns_buffer* pkt, struct sockaddr_storage* addr,
|
2007-05-11 10:16:42 -04:00
|
|
|
socklen_t addrlen, int timeout, struct module_qstate* q, int use_tcp)
|
2007-01-31 10:38:44 -05:00
|
|
|
{
|
2007-05-11 10:16:42 -04:00
|
|
|
struct worker* worker = q->env->worker;
|
|
|
|
|
if(use_tcp) {
|
|
|
|
|
return pending_tcp_query(worker->back, pkt, addr, addrlen,
|
|
|
|
|
timeout, worker_handle_reply, q->work_info,
|
2007-05-21 11:10:55 -04:00
|
|
|
worker->rndstate) != 0;
|
2007-05-11 10:16:42 -04:00
|
|
|
}
|
2007-05-23 02:24:01 -04:00
|
|
|
return pending_udp_query(worker->back, pkt, addr, addrlen,
|
|
|
|
|
timeout*1000, worker_handle_reply, q->work_info,
|
|
|
|
|
worker->rndstate) != 0;
|
2007-01-31 10:38:44 -05:00
|
|
|
}
|
2007-05-22 08:36:02 -04:00
|
|
|
|
2007-06-18 06:27:54 -04:00
|
|
|
/** compare outbound entry qstates */
|
|
|
|
|
static int
|
|
|
|
|
outbound_entry_compare(void* a, void* b)
|
|
|
|
|
{
|
|
|
|
|
struct outbound_entry* e1 = (struct outbound_entry*)a;
|
|
|
|
|
struct outbound_entry* e2 = (struct outbound_entry*)b;
|
|
|
|
|
if(e1->qstate == e2->qstate)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-22 08:36:02 -04:00
|
|
|
struct outbound_entry*
|
|
|
|
|
worker_send_query(uint8_t* qname, size_t qnamelen, uint16_t qtype,
|
|
|
|
|
uint16_t qclass, uint16_t flags, int dnssec,
|
|
|
|
|
struct sockaddr_storage* addr, socklen_t addrlen,
|
|
|
|
|
struct module_qstate* q)
|
|
|
|
|
{
|
|
|
|
|
struct worker* worker = q->env->worker;
|
|
|
|
|
struct outbound_entry* e = (struct outbound_entry*)malloc(sizeof(*e));
|
|
|
|
|
if(!e)
|
|
|
|
|
return NULL;
|
|
|
|
|
e->qstate = q;
|
|
|
|
|
e->qsent = outnet_serviced_query(worker->back, qname,
|
|
|
|
|
qnamelen, qtype, qclass, flags, dnssec, addr, addrlen,
|
2007-06-18 06:27:54 -04:00
|
|
|
worker_handle_service_reply, e, worker->back->udp_buff,
|
|
|
|
|
&outbound_entry_compare);
|
2007-05-22 08:36:02 -04:00
|
|
|
if(!e->qsent) {
|
|
|
|
|
free(e);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return e;
|
|
|
|
|
}
|
2007-06-15 05:13:54 -04:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
worker_slumber_subqueries(struct module_qstate* qstate)
|
|
|
|
|
{
|
|
|
|
|
struct worker* worker = qstate->env->worker;
|
|
|
|
|
if(qstate->subquery_first) {
|
|
|
|
|
while(qstate->subquery_first) {
|
|
|
|
|
/* put subqueries on slumber list */
|
|
|
|
|
struct module_qstate* s = qstate->subquery_first;
|
|
|
|
|
module_subreq_remove(&qstate->subquery_first, s);
|
|
|
|
|
s->parent = NULL;
|
|
|
|
|
s->work_info = NULL;
|
|
|
|
|
module_subreq_insert(&worker->slumber_list, s);
|
|
|
|
|
}
|
|
|
|
|
verbose(VERB_ALGO, "worker: slumber list has %d entries",
|
|
|
|
|
module_subreq_num(worker->slumber_list));
|
|
|
|
|
}
|
|
|
|
|
}
|