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-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-07 09:17:27 -04:00
|
|
|
#include "util/data/msgparse.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-03-29 06:00:10 -04:00
|
|
|
/** size of ID+FLAGS in a DNS message */
|
|
|
|
|
#define DNS_ID_AND_FLAGS 4
|
2007-01-31 10:38:44 -05:00
|
|
|
/** timeout in seconds for UDP queries to auth servers. TODO: proper rtt */
|
2007-03-22 12:26:14 -04:00
|
|
|
#define UDP_QUERY_TIMEOUT 4
|
2007-05-09 03:00:10 -04:00
|
|
|
/** timeout in seconds for TCP queries to auth servers. TODO: proper rtt */
|
|
|
|
|
#define TCP_QUERY_TIMEOUT 30
|
2007-05-07 09:17:27 -04:00
|
|
|
/** Advertised version of EDNS capabilities */
|
|
|
|
|
#define EDNS_ADVERTISED_VERSION 0
|
|
|
|
|
/** Advertised size of EDNS capabilities */
|
|
|
|
|
#define EDNS_ADVERTISED_SIZE 4096
|
|
|
|
|
|
2007-02-02 08:44:00 -05:00
|
|
|
|
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-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)
|
|
|
|
|
{
|
|
|
|
|
if(w->worker->num_requests == w->worker->request_size) {
|
|
|
|
|
/* no longer at max, start accepting again. */
|
|
|
|
|
listen_resume(w->worker->front);
|
|
|
|
|
}
|
2007-03-28 09:43:50 -04:00
|
|
|
log_assert(w->worker->num_requests >= 1);
|
2007-03-27 11:21:21 -04:00
|
|
|
w->worker->num_requests --;
|
|
|
|
|
w->next = w->worker->free_queries;
|
|
|
|
|
w->worker->free_queries = w;
|
|
|
|
|
}
|
|
|
|
|
|
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-04-03 05:29:09 -04:00
|
|
|
flags = (uint16_t)(BIT_QR | 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-03 11:34:03 -04:00
|
|
|
ldns_buffer_write(buf, qinfo->qname, qinfo->qnamesize);
|
|
|
|
|
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-07 10:05:51 -04:00
|
|
|
w->edns.edns_version = EDNS_ADVERTISED_VERSION;
|
|
|
|
|
w->edns.udp_size = EDNS_ADVERTISED_SIZE;
|
|
|
|
|
w->edns.ext_rcode = 0;
|
|
|
|
|
w->edns.bits &= EDNS_DO;
|
2007-05-03 11:34:03 -04:00
|
|
|
replyerror_fillbuf(r, &w->query_reply, w->query_id, w->query_flags,
|
|
|
|
|
&w->qinfo);
|
2007-05-07 10:05:51 -04:00
|
|
|
attach_edns_record(w->query_reply.c->buffer, &w->edns);
|
2007-03-27 11:21:21 -04:00
|
|
|
comm_point_send_reply(&w->query_reply);
|
|
|
|
|
req_release(w);
|
|
|
|
|
query_info_clear(&w->qinfo);
|
2007-02-02 08:44:00 -05:00
|
|
|
}
|
|
|
|
|
|
2007-05-04 06:10:52 -04:00
|
|
|
/** see if rrset needs to be updated in the cache */
|
|
|
|
|
static int
|
|
|
|
|
need_to_update_rrset(struct packed_rrset_data* newd,
|
|
|
|
|
struct packed_rrset_data* cached)
|
|
|
|
|
{
|
2007-05-04 09:48:24 -04:00
|
|
|
/* o if current RRset is more trustworthy - insert it */
|
2007-05-04 06:10:52 -04:00
|
|
|
if( newd->trust > cached->trust )
|
|
|
|
|
return 1;
|
2007-05-04 09:48:24 -04:00
|
|
|
/* o same trust, but different in data - insert it */
|
|
|
|
|
if( newd->trust == cached->trust &&
|
|
|
|
|
!rrsetdata_equal(newd, cached))
|
|
|
|
|
return 1;
|
|
|
|
|
/* o see if TTL is better than TTL in cache. */
|
|
|
|
|
/* if so, see if rrset+rdata is the same */
|
|
|
|
|
/* if so, update TTL in cache, even if trust is worse. */
|
2007-05-04 06:10:52 -04:00
|
|
|
if( newd->ttl > cached->ttl &&
|
|
|
|
|
rrsetdata_equal(newd, cached))
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-03 11:34:03 -04:00
|
|
|
/** store rrsets in the rrset cache. */
|
|
|
|
|
static void
|
|
|
|
|
worker_store_rrsets(struct worker* worker, struct reply_info* rep)
|
|
|
|
|
{
|
2007-05-04 06:10:52 -04:00
|
|
|
struct lruhash_entry* e;
|
2007-05-03 11:34:03 -04:00
|
|
|
size_t i;
|
2007-05-04 06:10:52 -04:00
|
|
|
/* see if rrset already exists in cache, if not insert it. */
|
2007-05-04 09:48:24 -04:00
|
|
|
/* if it does exist: check to insert it */
|
2007-05-03 11:34:03 -04:00
|
|
|
for(i=0; i<rep->rrset_count; i++) {
|
2007-05-04 06:10:52 -04:00
|
|
|
rep->ref[i].key = rep->rrsets[i];
|
|
|
|
|
rep->ref[i].id = rep->rrsets[i]->id;
|
|
|
|
|
/* looks up item with a readlock - no editing! */
|
|
|
|
|
if((e=slabhash_lookup(worker->daemon->rrset_cache,
|
|
|
|
|
rep->rrsets[i]->entry.hash, rep->rrsets[i]->entry.key,
|
|
|
|
|
0)) != 0) {
|
|
|
|
|
struct packed_rrset_data* data =
|
|
|
|
|
(struct packed_rrset_data*)e->data;
|
|
|
|
|
struct packed_rrset_data* rd =
|
|
|
|
|
(struct packed_rrset_data*)
|
|
|
|
|
rep->rrsets[i]->entry.data;
|
|
|
|
|
rep->ref[i].key = (struct ub_packed_rrset_key*)e->key;
|
|
|
|
|
rep->ref[i].id = rep->rrsets[i]->id;
|
|
|
|
|
/* found in cache, do checks above */
|
|
|
|
|
if(!need_to_update_rrset(rd, data)) {
|
|
|
|
|
lock_rw_unlock(&e->lock);
|
|
|
|
|
ub_packed_rrset_parsedelete(rep->rrsets[i],
|
|
|
|
|
&worker->alloc);
|
|
|
|
|
rep->rrsets[i] = rep->ref[i].key;
|
|
|
|
|
continue; /* use cached item instead */
|
|
|
|
|
}
|
|
|
|
|
if(rd->trust < data->trust)
|
|
|
|
|
rd->trust = data->trust;
|
|
|
|
|
lock_rw_unlock(&e->lock);
|
|
|
|
|
/* small gap here, where entry is not locked.
|
|
|
|
|
* possibly entry is updated with something else.
|
|
|
|
|
* this is just too bad, its cache anyway. */
|
|
|
|
|
/* use insert to update entry to manage lruhash
|
|
|
|
|
* cache size values nicely. */
|
|
|
|
|
}
|
2007-05-03 11:34:03 -04:00
|
|
|
slabhash_insert(worker->daemon->rrset_cache,
|
|
|
|
|
rep->rrsets[i]->entry.hash, &rep->rrsets[i]->entry,
|
2007-05-04 04:05:56 -04:00
|
|
|
rep->rrsets[i]->entry.data, &worker->alloc);
|
2007-05-04 08:35:01 -04:00
|
|
|
if(e) rep->rrsets[i] = rep->ref[i].key;
|
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-02-02 08:44:00 -05:00
|
|
|
struct comm_reply* ATTR_UNUSED(reply_info))
|
|
|
|
|
{
|
2007-03-27 11:21:21 -04:00
|
|
|
struct work_query* w = (struct work_query*)arg;
|
2007-05-03 11:34:03 -04:00
|
|
|
struct query_info qinf;
|
2007-03-22 12:26:14 -04:00
|
|
|
struct reply_info* rep;
|
|
|
|
|
struct msgreply_entry* e;
|
2007-05-07 09:17:27 -04:00
|
|
|
struct edns_data svr_edns; /* unused server edns advertisement */
|
|
|
|
|
uint16_t us;
|
2007-05-03 11:34:03 -04:00
|
|
|
int r;
|
|
|
|
|
|
2007-03-29 06:00:10 -04:00
|
|
|
verbose(VERB_DETAIL, "reply to query with stored ID %d",
|
|
|
|
|
ntohs(w->query_id)); /* byteswapped so same as dig prints */
|
2007-02-02 08:44:00 -05:00
|
|
|
if(error != 0) {
|
2007-03-27 11:21:21 -04:00
|
|
|
replyerror(LDNS_RCODE_SERVFAIL, w);
|
2007-02-02 08:44:00 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-03-23 09:12:49 -04:00
|
|
|
/* sanity check. */
|
|
|
|
|
if(!LDNS_QR_WIRE(ldns_buffer_begin(c->buffer)))
|
|
|
|
|
return 0; /* not a reply. */
|
|
|
|
|
if(LDNS_OPCODE_WIRE(ldns_buffer_begin(c->buffer)) != LDNS_PACKET_QUERY)
|
|
|
|
|
return 0; /* not a reply to a query. */
|
|
|
|
|
if(LDNS_QDCOUNT(ldns_buffer_begin(c->buffer)) > 1)
|
|
|
|
|
return 0; /* too much in the query section */
|
2007-05-07 11:01:27 -04:00
|
|
|
/* see if it is truncated */
|
|
|
|
|
if(LDNS_TC_WIRE(ldns_buffer_begin(c->buffer)) && c->type == comm_udp) {
|
|
|
|
|
log_info("TC: truncated. retry in TCP mode.");
|
2007-05-09 03:00:10 -04:00
|
|
|
qinfo_query_encode(w->worker->back->udp_buff, &w->qinfo);
|
|
|
|
|
pending_tcp_query(w->worker->back, w->worker->back->udp_buff,
|
|
|
|
|
&w->worker->fwd_addr, w->worker->fwd_addrlen,
|
|
|
|
|
TCP_QUERY_TIMEOUT, worker_handle_reply, w,
|
|
|
|
|
w->worker->rndstate);
|
2007-05-07 11:01:27 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-02-02 08:44:00 -05:00
|
|
|
/* woohoo a reply! */
|
2007-05-04 08:35:01 -04:00
|
|
|
if((r=reply_info_parse(c->buffer, &w->worker->alloc, &qinf, &rep,
|
2007-05-07 09:17:27 -04:00
|
|
|
w->worker->scratchpad, &svr_edns))!=0) {
|
2007-05-03 11:34:03 -04:00
|
|
|
if(r == LDNS_RCODE_SERVFAIL)
|
|
|
|
|
log_err("reply_info_parse: out of memory");
|
|
|
|
|
/* formerr on my parse gives servfail to my client */
|
2007-03-27 11:21:21 -04:00
|
|
|
replyerror(LDNS_RCODE_SERVFAIL, w);
|
2007-05-04 08:35:01 -04:00
|
|
|
region_free_all(w->worker->scratchpad);
|
2007-03-22 12:26:14 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-07 09:17:27 -04:00
|
|
|
us = w->edns.udp_size;
|
|
|
|
|
w->edns.edns_version = EDNS_ADVERTISED_VERSION;
|
|
|
|
|
w->edns.udp_size = EDNS_ADVERTISED_SIZE;
|
|
|
|
|
w->edns.ext_rcode = 0;
|
|
|
|
|
w->edns.bits &= EDNS_DO;
|
2007-05-03 11:34:03 -04:00
|
|
|
if(!reply_info_answer_encode(&qinf, rep, w->query_id, w->query_flags,
|
2007-05-07 09:17:27 -04:00
|
|
|
w->query_reply.c->buffer, 0, 0, w->worker->scratchpad, us,
|
|
|
|
|
&w->edns)) {
|
2007-03-27 11:21:21 -04:00
|
|
|
replyerror(LDNS_RCODE_SERVFAIL, w);
|
2007-05-03 11:34:03 -04:00
|
|
|
query_info_clear(&qinf);
|
|
|
|
|
reply_info_parsedelete(rep, &w->worker->alloc);
|
2007-05-04 08:35:01 -04:00
|
|
|
region_free_all(w->worker->scratchpad);
|
2007-03-22 12:26:14 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-05-03 11:34:03 -04:00
|
|
|
comm_point_send_reply(&w->query_reply);
|
2007-05-04 08:35:01 -04:00
|
|
|
region_free_all(w->worker->scratchpad);
|
2007-03-27 11:21:21 -04:00
|
|
|
req_release(w);
|
2007-05-03 11:34:03 -04:00
|
|
|
query_info_clear(&w->qinfo);
|
|
|
|
|
if(rep->ttl == 0) {
|
|
|
|
|
log_info("TTL 0: dropped");
|
|
|
|
|
query_info_clear(&qinf);
|
|
|
|
|
reply_info_parsedelete(rep, &w->worker->alloc);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
reply_info_set_ttls(rep, time(0));
|
|
|
|
|
worker_store_rrsets(w->worker, rep);
|
2007-05-04 06:10:52 -04:00
|
|
|
reply_info_sortref(rep);
|
2007-05-03 11:34:03 -04:00
|
|
|
/* store msg in the cache */
|
|
|
|
|
if(!(e = query_info_entrysetup(&qinf, rep, w->query_hash))) {
|
|
|
|
|
query_info_clear(&qinf);
|
|
|
|
|
reply_info_parsedelete(rep, &w->worker->alloc);
|
2007-03-22 12:26:14 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-03-27 11:21:21 -04:00
|
|
|
slabhash_insert(w->worker->daemon->msg_cache, w->query_hash,
|
2007-05-04 04:05:56 -04:00
|
|
|
&e->entry, rep, &w->worker->alloc);
|
2007-02-02 08:44:00 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-01-30 11:36:46 -05:00
|
|
|
|
2007-01-31 04:32:30 -05:00
|
|
|
/** process incoming request */
|
2007-02-06 11:26:19 -05:00
|
|
|
static void
|
2007-03-27 11:21:21 -04:00
|
|
|
worker_process_query(struct worker* worker, struct work_query* w)
|
2007-01-31 04:32:30 -05:00
|
|
|
{
|
|
|
|
|
/* query the forwarding address */
|
2007-03-29 06:00:10 -04:00
|
|
|
verbose(VERB_DETAIL, "process_query ID %d", ntohs(w->query_id));
|
2007-03-27 11:21:21 -04:00
|
|
|
pending_udp_query(worker->back, w->query_reply.c->buffer,
|
2007-02-02 08:44:00 -05:00
|
|
|
&worker->fwd_addr, worker->fwd_addrlen, UDP_QUERY_TIMEOUT,
|
2007-03-27 11:21:21 -04:00
|
|
|
worker_handle_reply, w, worker->rndstate);
|
2007-01-31 04:32:30 -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-02-06 11:26:19 -05:00
|
|
|
static int
|
|
|
|
|
worker_check_request(ldns_buffer* pkt)
|
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;
|
|
|
|
|
}
|
|
|
|
|
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-02-26 09:49:11 -05:00
|
|
|
/** process control messages from the main thread. */
|
|
|
|
|
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
|
|
|
size_t i;
|
|
|
|
|
/* see if it is possible */
|
|
|
|
|
if(rep->ttl <= timenow) {
|
|
|
|
|
/* the rrsets may have been updated in the meantime */
|
|
|
|
|
/* but this ignores it */
|
|
|
|
|
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-03 11:34:03 -04:00
|
|
|
/* check rrsets */
|
|
|
|
|
for(i=0; i<rep->rrset_count; i++) {
|
2007-05-04 09:48:24 -04:00
|
|
|
if(i>0 && rep->ref[i].key == rep->ref[i-1].key)
|
|
|
|
|
continue; /* only lock items once */
|
2007-05-03 11:34:03 -04:00
|
|
|
lock_rw_rdlock(&rep->ref[i].key->entry.lock);
|
|
|
|
|
if(rep->ref[i].id != rep->ref[i].key->id ||
|
|
|
|
|
rep->ttl <= timenow) {
|
|
|
|
|
/* failure! rollback our readlocks */
|
|
|
|
|
size_t j;
|
|
|
|
|
for(j=0; j<=i; j++)
|
|
|
|
|
lock_rw_unlock(&rep->ref[j].key->entry.lock);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 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,
|
|
|
|
|
udpsize, edns)) {
|
2007-05-03 11:34:03 -04:00
|
|
|
replyerror_fillbuf(LDNS_RCODE_SERVFAIL, repinfo, id,
|
|
|
|
|
flags, &mrentry->key);
|
|
|
|
|
}
|
|
|
|
|
/* unlock */
|
2007-05-04 09:48:24 -04:00
|
|
|
for(i=0; i<rep->rrset_count; i++) {
|
|
|
|
|
if(i>0 && rep->ref[i].key == rep->ref[i-1].key)
|
|
|
|
|
continue; /* only unlock items once */
|
2007-05-03 11:34:03 -04:00
|
|
|
lock_rw_unlock(&rep->ref[i].key->entry.lock);
|
2007-05-04 09:48:24 -04:00
|
|
|
}
|
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-15 10:50:22 -05:00
|
|
|
verbose(VERB_DETAIL, "worker handle request");
|
2007-02-06 11:26:19 -05:00
|
|
|
if(error != NETEVENT_NOERROR) {
|
2007-01-31 04:32:30 -05:00
|
|
|
log_err("called with err=%d", error);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if((ret=worker_check_request(c->buffer)) != 0) {
|
|
|
|
|
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-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-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) {
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
if(c->type != comm_udp)
|
|
|
|
|
edns.udp_size = 65535; /* max size for TCP replies */
|
2007-03-27 11:21:21 -04:00
|
|
|
if((e=slabhash_lookup(worker->daemon->msg_cache, h, &qinfo, 0))) {
|
2007-04-02 06:16:02 -04:00
|
|
|
/* answer from cache - we have acquired a readlock on it */
|
2007-03-23 08:41:38 -04:00
|
|
|
log_info("answer from the cache");
|
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;
|
|
|
|
|
}
|
|
|
|
|
log_info("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-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-07 09:17:27 -04:00
|
|
|
w->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;
|
|
|
|
|
w->query_hash = h;
|
|
|
|
|
memcpy(&w->query_reply, repinfo, sizeof(struct comm_reply));
|
|
|
|
|
memcpy(&w->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-03-27 11:21:21 -04:00
|
|
|
w->query_flags = ldns_buffer_read_u16_at(c->buffer, 2);
|
|
|
|
|
|
|
|
|
|
/* answer it */
|
|
|
|
|
worker_process_query(worker, w);
|
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;
|
|
|
|
|
q->worker = worker;
|
|
|
|
|
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;
|
|
|
|
|
log_assert(q->worker == worker);
|
|
|
|
|
/* comm_reply closed in outside_network_delete */
|
|
|
|
|
query_info_clear(&q->qinfo);
|
|
|
|
|
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-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,
|
|
|
|
|
cfg->do_tcp?cfg->outgoing_num_tcp:0);
|
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
|
|
|
}
|
|
|
|
|
/* init random(), large table size. */
|
2007-02-26 04:42:05 -05:00
|
|
|
if(!(worker->rndstate = (struct ub_randstate*)calloc(1,
|
|
|
|
|
sizeof(struct ub_randstate)))) {
|
2007-02-15 07:54:14 -05:00
|
|
|
log_err("malloc rndtable failed.");
|
|
|
|
|
worker_delete(worker);
|
2007-02-26 09:49:11 -05:00
|
|
|
return 0;
|
2007-02-15 07:54:14 -05:00
|
|
|
}
|
2007-02-26 09:49:11 -05:00
|
|
|
seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
|
|
|
|
|
(unsigned int)worker->thread_num;
|
2007-02-26 04:42:05 -05:00
|
|
|
if(!ub_initstate(seed, worker->rndstate, RND_STATE_SIZE)) {
|
2007-01-31 10:38:44 -05:00
|
|
|
log_err("could not init random numbers.");
|
2007-02-05 11:46:40 -05:00
|
|
|
worker_delete(worker);
|
2007-02-26 09:49:11 -05:00
|
|
|
return 0;
|
2007-01-31 10:38:44 -05:00
|
|
|
}
|
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-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-02-26 09:49:11 -05:00
|
|
|
|
2007-02-22 08:36:29 -05:00
|
|
|
/* set forwarder address */
|
|
|
|
|
if(cfg->fwd_address && cfg->fwd_address[0]) {
|
|
|
|
|
if(!worker_set_fwd(worker, cfg->fwd_address, cfg->fwd_port)) {
|
|
|
|
|
worker_delete(worker);
|
|
|
|
|
fatal_exit("could not set forwarder address");
|
|
|
|
|
}
|
|
|
|
|
}
|
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-04 08:35:01 -04:00
|
|
|
worker->scratchpad = region_create_custom(malloc, free,
|
|
|
|
|
65536, 8192, 32, 1);
|
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-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-02-22 08:36:29 -05:00
|
|
|
worker_set_fwd(struct worker* worker, const char* ip, int port)
|
2007-01-31 10:38:44 -05:00
|
|
|
{
|
2007-02-02 07:52:40 -05:00
|
|
|
uint16_t p;
|
2007-01-31 10:38:44 -05:00
|
|
|
log_assert(worker && ip);
|
2007-02-22 08:36:29 -05:00
|
|
|
p = (uint16_t) port;
|
2007-02-02 07:52:40 -05:00
|
|
|
if(str_is_ip6(ip)) {
|
|
|
|
|
struct sockaddr_in6* sa =
|
|
|
|
|
(struct sockaddr_in6*)&worker->fwd_addr;
|
|
|
|
|
worker->fwd_addrlen = (socklen_t)sizeof(struct sockaddr_in6);
|
|
|
|
|
memset(sa, 0, worker->fwd_addrlen);
|
|
|
|
|
sa->sin6_family = AF_INET6;
|
2007-02-02 09:19:56 -05:00
|
|
|
sa->sin6_port = (in_port_t)htons(p);
|
2007-02-02 07:52:40 -05:00
|
|
|
if(inet_pton((int)sa->sin6_family, ip, &sa->sin6_addr) <= 0) {
|
|
|
|
|
log_err("Bad ip6 address %s", ip);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else { /* ip4 */
|
|
|
|
|
struct sockaddr_in* sa =
|
|
|
|
|
(struct sockaddr_in*)&worker->fwd_addr;
|
|
|
|
|
worker->fwd_addrlen = (socklen_t)sizeof(struct sockaddr_in);
|
|
|
|
|
memset(sa, 0, worker->fwd_addrlen);
|
|
|
|
|
sa->sin_family = AF_INET;
|
2007-02-02 09:19:56 -05:00
|
|
|
sa->sin_port = (in_port_t)htons(p);
|
2007-02-02 07:52:40 -05:00
|
|
|
if(inet_pton((int)sa->sin_family, ip, &sa->sin_addr) <= 0) {
|
|
|
|
|
log_err("Bad ip4 address %s", ip);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
verbose(VERB_ALGO, "fwd queries to: %s %d", ip, p);
|
2007-01-31 10:38:44 -05:00
|
|
|
return 1;
|
|
|
|
|
}
|