client.c revision dc9870de5554a8cbd5f4ad44a4e97012e44aaf60
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Permission to use, copy, modify, and distribute this software for any
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * purpose with or without fee is hereby granted, provided that the above
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * copyright notice and this permission notice appear in all copies.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/* $Id: client.c,v 1.153 2001/02/22 23:39:11 gson Exp $ */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <config.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/mutex.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/print.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/string.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/task.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/timer.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/util.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/db.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/dispatch.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/events.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/message.h>
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt#ifdef DNS_OPT_NEWCODES
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt#include <dns/opt.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* DNS_OPT_NEWCODES */
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt#include <dns/rdata.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/rdataclass.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/rdatalist.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/rdataset.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/tsig.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/view.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/zone.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <named/interfacemgr.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <named/log.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <named/notify.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <named/server.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <named/update.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/***
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *** Client
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ***/
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Important note!
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * All client state changes, other than that from idle to listening, occur
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * as a result of events. This guarantees serialization and avoids the
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * need for locking.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * If a routine is ever created that allows someone other than the client's
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * task to change the client, then the client will have to be locked.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENT_TRACE
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef NS_CLIENT_TRACE
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define CTRACE(m) ns_client_log(client, \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGCATEGORY_CLIENT, \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGMODULE_CLIENT, \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_DEBUG(3), \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "%s", (m))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define MTRACE(m) isc_log_write(ns_g_lctx, \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGCATEGORY_GENERAL, \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGMODULE_CLIENT, \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_DEBUG(3), \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "clientmgr @%p: %s", manager, (m))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define CTRACE(m) ((void)(m))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define MTRACE(m) ((void)(m))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define TCP_BUFFER_SIZE (65535 + 2)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define SEND_BUFFER_SIZE 4096
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define RECV_BUFFER_SIZE 4096
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstruct ns_clientmgr {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Unlocked. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned int magic;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_t * mctx;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_taskmgr_t * taskmgr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_timermgr_t * timermgr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mutex_t lock;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Locked by lock. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_boolean_t exiting;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_list_t active; /* Active clients */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_list_t inactive; /* To be recycled */
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt};
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define MANAGER_MAGIC 0x4E53436DU /* NSCm */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define VALID_MANAGER(m) ((m) != NULL && \
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (m)->magic == MANAGER_MAGIC)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Client object states. Ordering is significant: higher-numbered
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * states are generally "more active", meaning that the client can
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * have more dynamically allocated data, outstanding events, etc.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * In the list below, any such properties listed for state N
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * also apply to any state > N.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * To force the client into a less active state, set client->newstate
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * to that state and call exit_check(). This will cause any
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * activities defined for higher-numbered states to be aborted.
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENTSTATE_FREED 0
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client object no longer exists.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENTSTATE_INACTIVE 1
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt * The client object exists and has a task and timer.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Its "query" struct and sendbuf are initialized.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * It is on the client manager's list of inactive clients.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * It has a message and OPT, both in the reset state.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENTSTATE_READY 2
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client object is either a TCP or a UDP one, and
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt * it is associated with a network interface. It is on the
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * client manager's list of active clients.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * If it is a TCP client object, it has a TCP listener socket
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * and an outstanding TCP listen request.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * If it is a UDP client object, it has a UDP listener socket
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * and an outstanding UDP receive request.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENTSTATE_READING 3
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client object is a TCP client object that has received
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * outstanding TCP read request. This state is not used for
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * UDP client objects.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENTSTATE_WORKING 4
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client object has received a request and is working
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * on it. It has a view, and it may have any of a non-reset OPT,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * recursion quota, and an outstanding write request.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#define NS_CLIENTSTATE_MAX 9
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Sentinel value used to indicate "no state". When client->newstate
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * has this value, we are not attempting to exit the current state.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Must be greater than any valid state.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void client_read(ns_client_t *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void client_accept(ns_client_t *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void client_udprecv(ns_client_t *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void clientmgr_destroy(ns_clientmgr_t *manager);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_boolean_t exit_check(ns_client_t *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void ns_client_endrequest(ns_client_t *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void ns_client_checkactive(ns_client_t *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void client_start(isc_task_t *task, isc_event_t *event);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void client_request(isc_task_t *task, isc_event_t *event);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Enter the inactive state.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Requires:
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt * No requests are outstanding.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_deactivate(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->interface)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_interface_detach(&client->interface);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->naccepts == 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->recursionquota == NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcplistener != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_detach(&client->tcplistener);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->udpsocket != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_detach(&client->udpsocket);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt if (client->dispatch != NULL)
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt dns_dispatch_detach(&client->dispatch);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->attributes = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->mortal = ISC_FALSE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt LOCK(&client->manager->lock);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_UNLINK(client->manager->active, client, link);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_APPEND(client->manager->inactive, client, link);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt client->list = &client->manager->inactive;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNLOCK(&client->manager->lock);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Clean up a client object and free its memory.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Requires:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client is in the inactive state.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_free(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_boolean_t need_clientmgr_destroy = ISC_FALSE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_clientmgr_t *manager = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * When "shuttingdown" is true, either the task has received
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * its shutdown event or no shutdown event has ever been
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * set up. Thus, we have no outstanding shutdown
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * event at this point.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->recursionquota == NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_query_free(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->sendbuf, SEND_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_event_free((isc_event_t **)&client->sendevent);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt isc_event_free((isc_event_t **)&client->recvevent);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_timer_detach(&client->timer);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpbuf != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(dns_rdataset_isassociated(client->opt));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rdataset_disassociate(client->opt);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_message_puttemprdataset(client->message, &client->opt);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef DNS_OPT_NEWCODES
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_zone != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->opt_zone,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeof(*client->opt_zone));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt_zone = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_view != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_free(&client->opt_view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* DNS_OPT_NEWCODES */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_message_destroy(&client->message);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->manager != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt manager = client->manager;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt LOCK(&manager->lock);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_UNLINK(*client->list, client, link);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->list = NULL;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt if (manager->exiting &&
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_EMPTY(manager->active) &&
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_EMPTY(manager->inactive))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt need_clientmgr_destroy = ISC_TRUE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNLOCK(&manager->lock);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Detaching the task must be done after unlinking from
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * the manager's lists because the manager accesses
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * client->task.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->task != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_task_detach(&client->task);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("free");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->magic = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client, sizeof *client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (need_clientmgr_destroy)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt clientmgr_destroy(manager);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntvoid
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_settimeout(ns_client_t *client, unsigned int seconds) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_interval_t interval;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_interval_set(&interval, seconds, 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &interval, ISC_FALSE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->timerset = ISC_TRUE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_log(client, NS_LOGCATEGORY_CLIENT,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "setting timeout: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Continue anyway. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Check for a deactivation or shutdown request and take appropriate
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * action. Returns ISC_TRUE if either is in progress; in this case
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * the caller must no longer use the client object as it may have been
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * freed.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_boolean_t
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntexit_check(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->state <= client->newstate)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_FALSE); /* Business as usual. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->newstate < NS_CLIENTSTATE_WORKING);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * We need to detach from the view early when shutting down
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt * the server to break the following vicious circle:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * - The resolver will not shut down until the view refcount is zero
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * - The view refcount does not go to zero until all clients detach
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * - The client does not detach from the view until references is zero
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * - references does not go to zero until the resolver has shut down
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_view_detach(&client->view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->state == NS_CLIENTSTATE_WORKING) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->newstate <= NS_CLIENTSTATE_READING);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * We are trying to abort request processing.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->nsends > 0) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_t *socket;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (TCP_CLIENT(client))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt socket = client->tcpsocket;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt socket = client->udpsocket;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_cancel(socket, client->task,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_SOCKCANCEL_SEND);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (! (client->nsends == 0 && client->nrecvs == 0 &&
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->references == 0))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Still waiting for I/O cancel completion.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * or lingering references.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * I/O cancel is complete. Burn down all state
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * related to the current request.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_endrequest(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->state = NS_CLIENTSTATE_READING;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->recursionquota == NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (NS_CLIENTSTATE_READING == client->newstate) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_read(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->newstate = NS_CLIENTSTATE_MAX;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE); /* We're done. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->state == NS_CLIENTSTATE_READING) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * We are trying to abort the current TCP connection,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * if any.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->recursionquota == NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->newstate <= NS_CLIENTSTATE_READY);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->nreads > 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_tcpmsg_cancelread(&client->tcpmsg);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (! client->nreads == 0) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Still waiting for read cancel completion. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpmsg_valid) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_tcpmsg_invalidate(&client->tcpmsg);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->tcpmsg_valid = ISC_FALSE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpsocket != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("closetcp");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_detach(&client->tcpsocket);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpquota != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_quota_detach(&client->tcpquota);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->timerset) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt (void) isc_timer_reset(client->timer,
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt isc_timertype_inactive,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NULL, NULL, ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->timerset = ISC_FALSE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt client->peeraddr_valid = ISC_FALSE;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->state = NS_CLIENTSTATE_READY;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->recursionquota == NULL);
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Now the client is ready to accept a new TCP connection
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * or UDP request, but we may have enough clients doing
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * that already. Check whether this client needs to remain
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * active and force it to go inactive if not.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_checkactive(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (NS_CLIENTSTATE_READY == client->newstate) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (TCP_CLIENT(client)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_accept(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt client_udprecv(client);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt client->newstate = NS_CLIENTSTATE_MAX;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->state == NS_CLIENTSTATE_READY) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt /*
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt * We are trying to enter the inactive state.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->naccepts > 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_cancel(client->tcplistener, client->task,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_SOCKCANCEL_ACCEPT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (! (client->naccepts == 0)) {
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt /* Still waiting for accept cancel completion. */
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Accept cancel is complete. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->nrecvs > 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_cancel(client->udpsocket, client->task,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_SOCKCANCEL_RECV);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt if (! (client->nrecvs == 0)) {
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt /* Still waiting for recv cancel completion. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Recv cancel is complete. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_deactivate(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->state = NS_CLIENTSTATE_INACTIVE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->recursionquota == NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->state == client->newstate) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->newstate = NS_CLIENTSTATE_MAX;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE); /* We're done. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->state == NS_CLIENTSTATE_INACTIVE) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->newstate == NS_CLIENTSTATE_FREED);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * We are trying to free the client.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_free(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client's task has received the client's control event
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * as part of the startup process.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_start(isc_task_t *task, isc_event_t *event) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_t *client = (ns_client_t *) event->ev_arg;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(task == client->task);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNUSED(task);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (TCP_CLIENT(client)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_accept(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_udprecv(client);
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The client's task has received a shutdown event.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_shutdown(isc_task_t *task, isc_event_t *event) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_t *client;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(event != NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client = event->ev_arg;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(task == client->task);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNUSED(task);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("shutdown");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_event_free(&event);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->shutdown != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->shutdown = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->shutdown_arg = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->newstate = NS_CLIENTSTATE_FREED;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void)exit_check(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_endrequest(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->naccepts == 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->nreads == 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->nsends == 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->nrecvs == 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->state == NS_CLIENTSTATE_WORKING);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("endrequest");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->next != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (client->next)(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->next = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->view != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_view_detach(&client->view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(dns_rdataset_isassociated(client->opt));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rdataset_disassociate(client->opt);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_message_puttemprdataset(client->message, &client->opt);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->udpsize = 512;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->extflags = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->recursionquota != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_quota_detach(&client->recursionquota);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Clear all client attributes that are specific to
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * the request; that's all except the TCP flag.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->attributes &= NS_CLIENTATTR_TCP;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_checkactive(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->mortal) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * This client object should normally go inactive
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * at this point, but if we have fewer active client
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * objects than desired due to earlier quota exhaustion,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * keep it active to make up for the shortage.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_boolean_t need_another_client = ISC_FALSE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (TCP_CLIENT(client)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt LOCK(&client->interface->lock);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->interface->ntcpcurrent <
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->interface->ntcptarget)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt need_another_client = ISC_TRUE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNLOCK(&client->interface->lock);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * The UDP client quota is enforced by making
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * requests fail rather than by not listening
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * for new ones. Therefore, there is always a
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * full set of UDP clients listening.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (! need_another_client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * We don't need this client object. Recycle it.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->newstate >= NS_CLIENTSTATE_INACTIVE)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->newstate = NS_CLIENTSTATE_INACTIVE;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntvoid
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_next(ns_client_t *client, isc_result_t result) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt int newstate;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt CTRACE("next");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "request failed: %s", isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * An error processing a TCP request may have left
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * the connection out of sync. To be safe, we always
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * sever the connection when result != ISC_R_SUCCESS.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt newstate = NS_CLIENTSTATE_READING;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt newstate = NS_CLIENTSTATE_READY;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->newstate > newstate)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->newstate = newstate;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) exit_check(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_senddone(isc_task_t *task, isc_event_t *event) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_t *client;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socketevent_t *sevent = (isc_socketevent_t *) event;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(sevent != NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client = sevent->ev_arg;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(task == client->task);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(sevent == client->sendevent);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt UNUSED(task);
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt CTRACE("senddone");
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (sevent->result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_log(client, NS_LOGCATEGORY_CLIENT,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "error sending response: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(sevent->result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->nsends > 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->nsends--;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpbuf != NULL) {
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt INSIST(TCP_CLIENT(client));
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->tcpbuf = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (exit_check(client))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_next(client, ISC_R_SUCCESS);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_result_t
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_t *tcpbuffer, isc_uint32_t length,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned char **datap)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt{
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned char *data;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_uint32_t bufsize;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(datap != NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (TCP_CLIENT(client)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST(client->tcpbuf == NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (tcpbuffer == NULL && length + 2 > TCP_BUFFER_SIZE) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_NOSPACE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpbuf == NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_NOMEMORY;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt data = client->tcpbuf;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (tcpbuffer != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_init(tcpbuffer, data, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_init(buffer, data + 2, TCP_BUFFER_SIZE - 2);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_init(buffer, data, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_putuint16(buffer, length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt data = client->sendbuf;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->udpsize < SEND_BUFFER_SIZE)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt bufsize = client->udpsize;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt bufsize = SEND_BUFFER_SIZE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (tcpbuffer == NULL && length > bufsize) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_NOSPACE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_init(buffer, data, bufsize);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *datap = data;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_SUCCESS;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt done:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_result_t
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt struct in6_pktinfo *pktinfo;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt isc_region_t r;
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt isc_sockaddr_t *address;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_socket_t *socket;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_netaddr_t netaddr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt int match;
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt if (TCP_CLIENT(client)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt socket = client->tcpsocket;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt address = NULL;
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt } else {
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt socket = client->udpsocket;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt address = &client->peeraddr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (ns_g_server->blackholeacl != NULL &&
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_acl_match(&netaddr, NULL,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_g_server->blackholeacl,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NULL, &match, NULL) == ISC_R_SUCCESS &&
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt match > 0)
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt return (DNS_R_BLACKHOLED);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pktinfo = &client->pktinfo;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pktinfo = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_usedregion(buffer, &r);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("sendto");
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_socket_sendto2(socket, &r, client->task,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt address, pktinfo,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->sendevent, ISC_SOCKFLAG_IMMEDIATE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS || result == ISC_R_INPROGRESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->nsends++;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_senddone(client->task,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (isc_event_t *)client->sendevent);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_SUCCESS;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntvoid
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_sendraw(ns_client_t *client, dns_message_t *message) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned char *data;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_t buffer;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_region_t r;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_region_t *mr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("sendraw");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt mr = dns_message_getrawmessage(message);
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt if (mr == NULL) {
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt result = ISC_R_UNEXPECTEDEND;
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt goto done;
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt result = client_allocsendbuf(client, &buffer, NULL, mr->length, &data);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Copy message to buffer and fixup id.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_availableregion(&buffer, &r);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_buffer_copyregion(&buffer, mr);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt r.base[0] = (client->message->id >> 8) & 0xff;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt r.base[1] = client->message->id & 0xff;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = client_sendpkg(client, &buffer);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt done:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpbuf != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->tcpbuf = NULL;
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_next(client, result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntvoid
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_send(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned char *data;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_t buffer;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_t tcpbuffer;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_region_t r;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("send");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if ((client->attributes & NS_CLIENTATTR_RA) != 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->message->flags |= DNS_MESSAGEFLAG_RA;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * XXXRTH The following doesn't deal with TCP buffer resizing.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0, &data);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_renderbegin(client->message, &buffer);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_setopt(client->message, client->opt);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * XXXRTH dns_message_setopt() should probably do this...
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_rendersection(client->message,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt DNS_SECTION_QUESTION, 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_NOSPACE) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->message->flags |= DNS_MESSAGEFLAG_TC;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto renderend;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt result = dns_message_rendersection(client->message,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt DNS_SECTION_ANSWER, 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_NOSPACE) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->message->flags |= DNS_MESSAGEFLAG_TC;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto renderend;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_rendersection(client->message,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt DNS_SECTION_AUTHORITY, 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_NOSPACE) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->message->flags |= DNS_MESSAGEFLAG_TC;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto renderend;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ce9f893e21d2ffc6f6a78bf226c038c396740aebEvan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_rendersection(client->message,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt DNS_SECTION_ADDITIONAL, 0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt renderend:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_renderend(client->message);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto done;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (TCP_CLIENT(client)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_usedregion(&buffer, &r);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt isc_buffer_add(&tcpbuffer, r.length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = client_sendpkg(client, &tcpbuffer);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = client_sendpkg(client, &buffer);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt done:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->tcpbuf != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->tcpbuf = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_next(client, result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntvoid
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_client_error(ns_client_t *client, isc_result_t result) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rcode_t rcode;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_message_t *message;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(NS_CLIENT_VALID(client));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CTRACE("error");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt message = client->message;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rcode = dns_result_torcode(result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Message may be an in-progress reply that we had trouble
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * with, in which case QR will be set. We need to clear QR before
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * calling dns_message_reply() to avoid triggering an assertion.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt message->flags &= ~DNS_MESSAGEFLAG_QR;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * AA and AD shouldn't be set.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_reply(message, ISC_TRUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * It could be that we've got a query with a good header,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * but a bad question section, so we try again with
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * want_question_section set to ISC_FALSE.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_reply(message, ISC_FALSE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_client_next(client, result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt message->rcode = rcode;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt ns_client_send(client);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef DNS_OPT_NEWCODES
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_result_t
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_addoptattrs(ns_client_t *client, dns_rdata_t *rdata) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_t *zonebuf = NULL, *buf = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_optlist_t attrlist;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_optattr_t attrs[CLIENT_NUMATTRS];
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt dns_compress_t cctx;
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt int i, sizeneeded = 0;
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_compress_init(&cctx, 0, client->mctx);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto fail1;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_compress_setmethods(&cctx, DNS_COMPRESS_NONE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrlist.size=2;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrlist.used=0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrlist.attrs=attrs;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt for (i=0; i<CLIENT_NUMATTRS; i++) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[i].code = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[i].value.base = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[i].value.length = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_zone != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_buffer_allocate(client->mctx, &zonebuf,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt DNS_NAME_MAXWIRE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto fail2;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_name_towire(dns_fixedname_name(client->opt_zone),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &cctx, zonebuf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto fail2;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[attrlist.used].code = DNS_OPTCODE_ZONE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[attrlist.used].value.base = isc_buffer_base(zonebuf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[attrlist.used].value.length =
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_usedlength(zonebuf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrlist.used++;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeneeded += 4 + isc_buffer_usedlength(zonebuf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_view != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[attrlist.used].code = DNS_OPTCODE_VIEW;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[attrlist.used].value.base =
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_base(client->opt_view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrs[attrlist.used].value.length =
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_usedlength(client->opt_view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attrlist.used++;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeneeded += 4 + isc_buffer_usedlength(client->opt_view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt if (sizeneeded == 0) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_SUCCESS;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto fail2;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_buffer_allocate(client->mctx, &buf, sizeneeded+1);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt goto fail2;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_opt_add(rdata, &attrlist, buf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto fail2;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_message_takebuffer(client->message, &buf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt fail2:
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt dns_compress_invalidate(&cctx);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt fail1:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (buf != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_free(&buf);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (zonebuf != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_free(&zonebuf);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt return (result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* DNS_OPT_NEWCODES */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic inline isc_result_t
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_addopt(ns_client_t *client) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rdataset_t *rdataset;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rdatalist_t *rdatalist;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rdata_t *rdata;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(client->opt == NULL); /* XXXRTH free old. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdatalist = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_gettemprdatalist(client->message, &rdatalist);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdata = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_gettemprdata(client->message, &rdata);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (result);
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt rdataset = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_message_gettemprdataset(client->message, &rdataset);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (result);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_rdataset_init(rdataset);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdatalist->type = dns_rdatatype_opt;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdatalist->covers = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Set the maximum UDP buffer size.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt rdatalist->rdclass = RECV_BUFFER_SIZE;
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt /*
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt * Set EXTENDED-RCODE, VERSION, and Z to 0.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdatalist->ttl = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * No ENDS options in the default case.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdata->data = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdata->length = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdata->rdclass = rdatalist->rdclass;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdata->type = rdatalist->type;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rdata->flags = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef DNS_OPT_NEWCODES
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Set the attributes
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client_addoptattrs(client, rdata);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* DNS_OPT_NEWCODES */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_INIT(rdatalist->rdata);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt dns_rdatalist_tordataset(rdatalist, rdataset);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt = rdataset;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_R_SUCCESS);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef DNS_OPT_NEWCODES
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntclient_getoptattrs(ns_client_t *client, dns_rdataset_t *opt) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_optlist_t optlist;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_optattr_t optattr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result, iresult;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_t zonebuf;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_decompress_t dctx;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* If an old set of opts are still around, free them. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_zone != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(client->mctx, client->opt_zone,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeof(*client->opt_zone));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt_zone = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_view != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_free(&client->opt_view);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * If there are any options, extract them here
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optlist.size = 1;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optlist.used = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optlist.next = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optlist.attrs = &optattr;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt do {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dns_opt_decodeall(&optlist, opt);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS ||
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result == DNS_R_MOREDATA) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt switch (optattr.code) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt case DNS_OPTCODE_ZONE:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_decompress_init(&dctx, 0,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt DNS_DECOMPRESS_NONE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt_zone = isc_mem_get(
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->mctx,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeof(*client->opt_zone));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (client->opt_zone == NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt goto zonefail1;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_fixedname_init(client->opt_zone);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_init(&zonebuf,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optattr.value.base,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optattr.value.length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_add(&zonebuf,optattr.value.length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_setactive(&zonebuf,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optattr.value.length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt iresult = dns_name_fromwire(
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_fixedname_name(
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt_zone),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &zonebuf,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &dctx, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (iresult != ISC_R_SUCCESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_fixedname_invalidate(
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt client->opt_zone);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt zonefail1:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_decompress_invalidate(&dctx);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt break;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt case DNS_OPTCODE_VIEW:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt iresult = isc_buffer_allocate(client->mctx,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &client->opt_view,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optattr.value.length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (iresult != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt break;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_putmem(client->opt_view,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optattr.value.base,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt optattr.value.length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt break;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } while (result == DNS_R_MOREDATA);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* DNS_OPT_NEWCODES */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Handle an incoming request event from the socket (UDP case)
* or tcpmsg (TCP case).
*/
static void
client_request(isc_task_t *task, isc_event_t *event) {
ns_client_t *client;
isc_socketevent_t *sevent;
isc_result_t result;
isc_result_t sigresult;
isc_buffer_t *buffer;
isc_buffer_t tbuffer;
dns_view_t *view;
dns_rdataset_t *opt;
isc_boolean_t ra; /* Recursion available. */
REQUIRE(event != NULL);
client = event->ev_arg;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(task == client->task);
UNUSED(task);
INSIST(client->recursionquota == NULL);
INSIST(client->state ==
TCP_CLIENT(client) ?
NS_CLIENTSTATE_READING :
NS_CLIENTSTATE_READY);
if (event->ev_type == ISC_SOCKEVENT_RECVDONE) {
INSIST(!TCP_CLIENT(client));
sevent = (isc_socketevent_t *)event;
REQUIRE(sevent == client->recvevent);
isc_buffer_init(&tbuffer, sevent->region.base, sevent->n);
isc_buffer_add(&tbuffer, sevent->n);
buffer = &tbuffer;
result = sevent->result;
client->peeraddr = sevent->address;
client->peeraddr_valid = ISC_TRUE;
if ((sevent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
client->attributes |= NS_CLIENTATTR_PKTINFO;
client->pktinfo = sevent->pktinfo;
}
if ((sevent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
client->attributes |= NS_CLIENTATTR_MULTICAST;
client->nrecvs--;
} else {
INSIST(TCP_CLIENT(client));
REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
REQUIRE(event->ev_sender == &client->tcpmsg);
buffer = &client->tcpmsg.buffer;
result = client->tcpmsg.result;
INSIST(client->nreads == 1);
/*
* client->peeraddr was set when the connection was accepted.
*/
client->nreads--;
}
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"%s request",
TCP_CLIENT(client) ? "TCP" : "UDP");
if (exit_check(client))
goto cleanup;
client->state = client->newstate = NS_CLIENTSTATE_WORKING;
isc_stdtime_get(&client->requesttime);
client->now = client->requesttime;
if (result != ISC_R_SUCCESS) {
if (TCP_CLIENT(client))
ns_client_next(client, result);
else
isc_task_shutdown(client->task);
goto cleanup;
}
if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
"multicast request");
#if 0
ns_client_error(client, DNS_R_REFUSED);
#endif
}
result = dns_message_parse(client->message, buffer, 0);
if (result != ISC_R_SUCCESS) {
ns_client_error(client, result);
goto cleanup;
}
/*
* We expect a query, not a response. If this is a UDP response,
* forward it to the dispatcher. If it's a TCP response,
* discarded it here.
*/
if ((client->message->flags & DNS_MESSAGEFLAG_QR) != 0) {
if (TCP_CLIENT(client)) {
CTRACE("unexpected response");
ns_client_next(client, DNS_R_FORMERR);
goto cleanup;
} else {
dns_dispatch_importrecv(client->dispatch, event);
ns_client_next(client, ISC_R_SUCCESS);
goto cleanup;
}
}
/*
* Deal with EDNS.
*/
opt = dns_message_getopt(client->message);
if (opt != NULL) {
unsigned int version;
/*
* Set the client's UDP buffer size.
*/
client->udpsize = opt->rdclass;
#ifdef DNS_OPT_NEWCODES
/*
* Get the flags out of the OPT record.
*/
client->extflags = DNS_OPT_FLAGS(opt);
/*
* Set up the rest of the opt stuff
*/
client_getoptattrs(client, opt);
/*
* If we're using a fixed zone option (opt_zone), set it to
* allow glue here.
*/
if (client->opt_zone != NULL)
client->query.dboptions |= DNS_DBFIND_GLUEOK;
#else /* DNS_OPT_NEWCODES */
/*
* Get the flags out of the OPT record.
*/
client->extflags = opt->ttl & 0xFFFF;
#endif /* DNS_OPT_NEWCODES */
/*
* Create an OPT for our reply.
*/
result = client_addopt(client);
if (result != ISC_R_SUCCESS) {
ns_client_error(client, result);
goto cleanup;
}
/*
* Do we understand this version of ENDS?
*
* XXXRTH need library support for this!
*/
version = (opt->ttl & 0x00FF0000) >> 16;
if (version != 0) {
ns_client_error(client, DNS_R_BADVERS);
goto cleanup;
}
}
if (client->message->rdclass == 0) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
"message class could not be determined");
ns_client_error(client, DNS_R_FORMERR);
goto cleanup;
}
/*
* Find a view that matches the client's source address.
*/
for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link)) {
if (client->message->rdclass == view->rdclass ||
client->message->rdclass == dns_rdataclass_any)
{
isc_netaddr_t netaddr;
int match;
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
if (view->matchclients == NULL ||
(dns_acl_match(&netaddr, NULL, view->matchclients,
&ns_g_server->aclenv,
&match, NULL) == ISC_R_SUCCESS &&
match > 0))
{
dns_view_attach(view, &client->view);
break;
}
}
}
if (view == NULL) {
char classname[DNS_RDATACLASS_FORMATSIZE];
/*
* Do a dummy TSIG verification attempt so that the
* response will have a TSIG if the query did, as
* required by RFC2845.
*/
isc_buffer_t b;
isc_region_t *r;
r = dns_message_getrawmessage(client->message);
isc_buffer_init(&b, r->base, r->length);
isc_buffer_add(&b, r->length);
(void)dns_tsig_verify(&b, client->message, NULL, NULL);
dns_rdataclass_format(client->message->rdclass, classname,
sizeof(classname));
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
"no matching view in class '%s'", classname);
ns_client_error(client, DNS_R_REFUSED);
goto cleanup;
}
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
"using view '%s'", view->name);
/*
* Check for a signature. We log bad signatures regardless of
* whether they ultimately cause the request to be rejected or
* not. We do not log the lack of a signature unless we are
* debugging.
*/
sigresult = dns_message_checksig(client->message, client->view);
client->signer = NULL;
dns_name_init(&client->signername, NULL);
result = dns_message_signer(client->message, &client->signername);
if (result == ISC_R_SUCCESS) {
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request has valid signature");
client->signer = &client->signername;
} else if (result == ISC_R_NOTFOUND) {
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request is not signed");
} else if (result == DNS_R_NOIDENTITY) {
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request is signed by a nonauthoritative key");
/*
* Accept update messages signed by unknown keys so that
* update forwarding works transparently through slaves
* that don't have all the same keys as the master.
*/
if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
client->message->opcode == dns_opcode_update)) {
ns_client_error(client, sigresult);
goto cleanup;
}
} else {
/* There is a signature, but it is bad. */
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
"request has invalid signature: %s",
isc_result_totext(result));
if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
client->message->opcode == dns_opcode_update)) {
ns_client_error(client, sigresult);
goto cleanup;
}
}
/*
* Decide whether recursive service is available to this client.
* We do this here rather than in the query code so that we can
* set the RA bit correctly on all kinds of responses, not just
* responses to ordinary queries.
*/
ra = ISC_FALSE;
if (client->view->resolver != NULL &&
client->view->recursion == ISC_TRUE &&
/* XXX this will log too much too early */
ns_client_checkacl(client, "recursion",
client->view->recursionacl,
ISC_TRUE, ISC_LOG_DEBUG(1)) == ISC_R_SUCCESS)
ra = ISC_TRUE;
if (ra == ISC_TRUE)
client->attributes |= NS_CLIENTATTR_RA;
/*
* Dispatch the request.
*/
switch (client->message->opcode) {
case dns_opcode_query:
CTRACE("query");
ns_query_start(client);
break;
case dns_opcode_update:
CTRACE("update");
ns_client_settimeout(client, 60);
ns_update_start(client, sigresult);
break;
case dns_opcode_notify:
CTRACE("notify");
ns_client_settimeout(client, 60);
ns_notify_start(client);
break;
case dns_opcode_iquery:
CTRACE("iquery");
ns_client_error(client, DNS_R_NOTIMP);
break;
default:
CTRACE("unknown opcode");
ns_client_error(client, DNS_R_NOTIMP);
}
cleanup:
return;
}
static void
client_timeout(isc_task_t *task, isc_event_t *event) {
ns_client_t *client;
REQUIRE(event != NULL);
REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE ||
event->ev_type == ISC_TIMEREVENT_IDLE);
client = event->ev_arg;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(task == client->task);
REQUIRE(client->timer != NULL);
UNUSED(task);
CTRACE("timeout");
isc_event_free(&event);
if (client->shutdown != NULL) {
(client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
client->shutdown = NULL;
client->shutdown_arg = NULL;
}
if (client->newstate > NS_CLIENTSTATE_READY)
client->newstate = NS_CLIENTSTATE_READY;
(void) exit_check(client);
}
static isc_result_t
client_create(ns_clientmgr_t *manager, ns_client_t **clientp)
{
ns_client_t *client;
isc_result_t result;
/*
* Caller must be holding the manager lock.
*
* Note: creating a client does not add the client to the
* manager's client list or set the client's manager pointer.
* The caller is responsible for that.
*/
REQUIRE(clientp != NULL && *clientp == NULL);
client = isc_mem_get(manager->mctx, sizeof *client);
if (client == NULL)
return (ISC_R_NOMEMORY);
client->task = NULL;
result = isc_task_create(manager->taskmgr, 0, &client->task);
if (result != ISC_R_SUCCESS)
goto cleanup_client;
isc_task_setname(client->task, "client", client);
result = isc_task_onshutdown(client->task, client_shutdown, client);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
client->timer = NULL;
result = isc_timer_create(manager->timermgr, isc_timertype_inactive,
NULL, NULL, client->task, client_timeout,
client, &client->timer);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
client->timerset = ISC_FALSE;
client->message = NULL;
result = dns_message_create(manager->mctx, DNS_MESSAGE_INTENTPARSE,
&client->message);
if (result != ISC_R_SUCCESS)
goto cleanup_timer;
/* XXXRTH Hardwired constants */
client->sendbuf = isc_mem_get(manager->mctx, SEND_BUFFER_SIZE);
if (client->sendbuf == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_message;
}
client->sendevent = (isc_socketevent_t *)
isc_event_allocate(manager->mctx, client,
ISC_SOCKEVENT_SENDDONE,
client_senddone, client,
sizeof(isc_socketevent_t));
if (client->sendevent == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_sendbuf;
}
client->recvbuf = isc_mem_get(manager->mctx, RECV_BUFFER_SIZE);
if (client->recvbuf == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_sendevent;
}
client->recvevent = (isc_socketevent_t *)
isc_event_allocate(manager->mctx, client,
ISC_SOCKEVENT_RECVDONE,
client_request, client,
sizeof(isc_socketevent_t));
if (client->recvevent == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_recvbuf;
}
client->magic = NS_CLIENT_MAGIC;
client->mctx = manager->mctx;
client->manager = NULL;
client->state = NS_CLIENTSTATE_INACTIVE;
client->newstate = NS_CLIENTSTATE_MAX;
client->naccepts = 0;
client->nreads = 0;
client->nsends = 0;
client->nrecvs = 0;
client->references = 0;
client->attributes = 0;
client->view = NULL;
client->dispatch = NULL;
client->udpsocket = NULL;
client->tcplistener = NULL;
client->tcpsocket = NULL;
client->tcpmsg_valid = ISC_FALSE;
client->tcpbuf = NULL;
client->opt = NULL;
client->udpsize = 512;
client->extflags = 0;
#ifdef DNS_OPT_NEWCODES
client->opt_zone = NULL;
client->opt_view = NULL;
#endif /* DNS_OPT_NEWCODES */
client->next = NULL;
client->shutdown = NULL;
client->shutdown_arg = NULL;
dns_name_init(&client->signername, NULL);
client->mortal = ISC_FALSE;
client->tcpquota = NULL;
client->recursionquota = NULL;
client->interface = NULL;
client->peeraddr_valid = ISC_FALSE;
ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
NS_EVENT_CLIENTCONTROL, client_start, client, client,
NULL, NULL);
ISC_LINK_INIT(client, link);
client->list = NULL;
/*
* We call the init routines for the various kinds of client here,
* after we have created an otherwise valid client, because some
* of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
*/
result = ns_query_init(client);
if (result != ISC_R_SUCCESS)
goto cleanup_recvevent;
CTRACE("create");
*clientp = client;
return (ISC_R_SUCCESS);
cleanup_recvevent:
isc_event_free((isc_event_t **)&client->recvevent);
cleanup_recvbuf:
isc_mem_put(manager->mctx, client->recvbuf, RECV_BUFFER_SIZE);
cleanup_sendevent:
isc_event_free((isc_event_t **)&client->sendevent);
cleanup_sendbuf:
isc_mem_put(manager->mctx, client->sendbuf, SEND_BUFFER_SIZE);
client->magic = 0;
cleanup_message:
dns_message_destroy(&client->message);
cleanup_timer:
isc_timer_detach(&client->timer);
cleanup_task:
isc_task_detach(&client->task);
cleanup_client:
isc_mem_put(manager->mctx, client, sizeof *client);
return (result);
}
static void
client_read(ns_client_t *client) {
isc_result_t result;
CTRACE("read");
result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
client_request, client);
if (result != ISC_R_SUCCESS)
goto fail;
/*
* Set a timeout to limit the amount of time we will wait
* for a request on this TCP connection.
*/
ns_client_settimeout(client, 30);
client->state = client->newstate = NS_CLIENTSTATE_READING;
INSIST(client->nreads == 0);
INSIST(client->recursionquota == NULL);
client->nreads++;
return;
fail:
ns_client_next(client, result);
}
static void
client_newconn(isc_task_t *task, isc_event_t *event) {
ns_client_t *client = event->ev_arg;
isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
isc_result_t result;
REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(client->task == task);
UNUSED(task);
INSIST(client->state == NS_CLIENTSTATE_READY);
INSIST(client->naccepts == 1);
client->naccepts--;
LOCK(&client->interface->lock);
INSIST(client->interface->ntcpcurrent > 0);
client->interface->ntcpcurrent--;
UNLOCK(&client->interface->lock);
/*
* We must take ownership of the new socket before the exit
* check to make sure it gets destroyed if we decide to exit.
*/
if (nevent->result == ISC_R_SUCCESS) {
client->tcpsocket = nevent->newsocket;
client->state = NS_CLIENTSTATE_READING;
INSIST(client->recursionquota == NULL);
(void) isc_socket_getpeername(client->tcpsocket,
&client->peeraddr);
client->peeraddr_valid = ISC_TRUE;
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"new TCP connection");
} else {
/*
* XXXRTH What should we do? We're trying to accept but
* it didn't work. If we just give up, then TCP
* service may eventually stop.
*
* For now, we just go idle.
*
* Going idle is probably the right thing if the
* I/O was canceled.
*/
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"accept failed: %s",
isc_result_totext(nevent->result));
}
if (exit_check(client))
goto freeevent;
if (nevent->result == ISC_R_SUCCESS) {
int match;
isc_netaddr_t netaddr;
INSIST(client->tcpmsg_valid == ISC_FALSE);
dns_tcpmsg_init(client->mctx, client->tcpsocket,
&client->tcpmsg);
client->tcpmsg_valid = ISC_TRUE;
/*
* Let a new client take our place immediately, before
* we wait for a request packet. If we don't,
* telnetting to port 53 (once per CPU) will
* deny service to legititmate TCP clients.
*/
result = isc_quota_attach(&ns_g_server->tcpquota,
&client->tcpquota);
if (result == ISC_R_SUCCESS)
result = ns_client_replace(client);
if (result != ISC_R_SUCCESS) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
"no more TCP clients: %s",
isc_result_totext(result));
}
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
if (ns_g_server->blackholeacl != NULL &&
dns_acl_match(&netaddr, NULL,
ns_g_server->blackholeacl,
&ns_g_server->aclenv,
&match, NULL) == ISC_R_SUCCESS &&
match > 0)
{
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
"blackholed connection attempt");
goto freeevent;
}
client_read(client);
}
freeevent:
isc_event_free(&event);
}
static void
client_accept(ns_client_t *client) {
isc_result_t result;
CTRACE("accept");
result = isc_socket_accept(client->tcplistener, client->task,
client_newconn, client);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_socket_accept() failed: %s",
isc_result_totext(result));
/*
* XXXRTH What should we do? We're trying to accept but
* it didn't work. If we just give up, then TCP
* service may eventually stop.
*
* For now, we just go idle.
*/
return;
}
INSIST(client->naccepts == 0);
client->naccepts++;
LOCK(&client->interface->lock);
client->interface->ntcpcurrent++;
UNLOCK(&client->interface->lock);
}
static void
client_udprecv(ns_client_t *client) {
isc_result_t result;
isc_region_t r;
CTRACE("udprecv");
r.base = client->recvbuf;
r.length = RECV_BUFFER_SIZE;
result = isc_socket_recv2(client->udpsocket, &r, 1,
client->task, client->recvevent, 0);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_socket_recv() failed: %s",
isc_result_totext(result));
/*
* XXXBEW What should we do? We're trying to receive but
* it didn't work. If we just give up, then UDP
* service may eventually stop.
*
* For now, we just go idle.
*/
return;
}
INSIST(client->nrecvs == 0);
client->nrecvs++;
}
void
ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
REQUIRE(NS_CLIENT_VALID(source));
REQUIRE(targetp != NULL && *targetp == NULL);
source->references++;
ns_client_log(source, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
"ns_client_attach: ref = %d", source->references);
*targetp = source;
}
void
ns_client_detach(ns_client_t **clientp) {
ns_client_t *client = *clientp;
client->references--;
INSIST(client->references >= 0);
*clientp = NULL;
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
"ns_client_detach: ref = %d", client->references);
(void) exit_check(client);
}
isc_boolean_t
ns_client_shuttingdown(ns_client_t *client) {
return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
}
isc_result_t
ns_client_replace(ns_client_t *client) {
isc_result_t result;
CTRACE("replace");
result = ns_clientmgr_createclients(client->manager,
1, client->interface,
(TCP_CLIENT(client) ?
ISC_TRUE : ISC_FALSE));
if (result != ISC_R_SUCCESS)
return (result);
/*
* The responsibility for listening for new requests is hereby
* transferred to the new client. Therefore, the old client
* should refrain from listening for any more requests.
*/
client->mortal = ISC_TRUE;
return (ISC_R_SUCCESS);
}
/***
*** Client Manager
***/
static void
clientmgr_destroy(ns_clientmgr_t *manager) {
REQUIRE(ISC_LIST_EMPTY(manager->active));
REQUIRE(ISC_LIST_EMPTY(manager->inactive));
MTRACE("clientmgr_destroy");
DESTROYLOCK(&manager->lock);
manager->magic = 0;
isc_mem_put(manager->mctx, manager, sizeof *manager);
}
isc_result_t
ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
isc_timermgr_t *timermgr, ns_clientmgr_t **managerp)
{
ns_clientmgr_t *manager;
isc_result_t result;
manager = isc_mem_get(mctx, sizeof *manager);
if (manager == NULL)
return (ISC_R_NOMEMORY);
result = isc_mutex_init(&manager->lock);
if (result != ISC_R_SUCCESS)
goto cleanup_manager;
manager->mctx = mctx;
manager->taskmgr = taskmgr;
manager->timermgr = timermgr;
manager->exiting = ISC_FALSE;
ISC_LIST_INIT(manager->active);
ISC_LIST_INIT(manager->inactive);
manager->magic = MANAGER_MAGIC;
MTRACE("create");
*managerp = manager;
return (ISC_R_SUCCESS);
cleanup_manager:
isc_mem_put(manager->mctx, manager, sizeof *manager);
return (result);
}
void
ns_clientmgr_destroy(ns_clientmgr_t **managerp) {
ns_clientmgr_t *manager;
ns_client_t *client;
isc_boolean_t need_destroy = ISC_FALSE;
REQUIRE(managerp != NULL);
manager = *managerp;
REQUIRE(VALID_MANAGER(manager));
MTRACE("destroy");
LOCK(&manager->lock);
manager->exiting = ISC_TRUE;
for (client = ISC_LIST_HEAD(manager->active);
client != NULL;
client = ISC_LIST_NEXT(client, link))
isc_task_shutdown(client->task);
for (client = ISC_LIST_HEAD(manager->inactive);
client != NULL;
client = ISC_LIST_NEXT(client, link))
isc_task_shutdown(client->task);
if (ISC_LIST_EMPTY(manager->active) &&
ISC_LIST_EMPTY(manager->inactive))
need_destroy = ISC_TRUE;
UNLOCK(&manager->lock);
if (need_destroy)
clientmgr_destroy(manager);
*managerp = NULL;
}
isc_result_t
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
ns_interface_t *ifp, isc_boolean_t tcp)
{
isc_result_t result = ISC_R_SUCCESS;
unsigned int i;
ns_client_t *client;
REQUIRE(VALID_MANAGER(manager));
REQUIRE(n > 0);
MTRACE("createclients");
/*
* We MUST lock the manager lock for the entire client creation
* process. If we didn't do this, then a client could get a
* shutdown event and disappear out from under us.
*/
LOCK(&manager->lock);
for (i = 0; i < n; i++) {
isc_event_t *ev;
/*
* Allocate a client. First try to get a recycled one;
* if that fails, make a new one.
*/
client = ISC_LIST_HEAD(manager->inactive);
if (client != NULL) {
MTRACE("recycle");
ISC_LIST_UNLINK(manager->inactive, client, link);
client->list = NULL;
} else {
MTRACE("create new");
result = client_create(manager, &client);
if (result != ISC_R_SUCCESS)
break;
}
ns_interface_attach(ifp, &client->interface);
client->state = NS_CLIENTSTATE_READY;
INSIST(client->recursionquota == NULL);
if (tcp) {
client->attributes |= NS_CLIENTATTR_TCP;
isc_socket_attach(ifp->tcpsocket,
&client->tcplistener);
} else {
isc_socket_t *sock;
dns_dispatch_attach(ifp->udpdispatch,
&client->dispatch);
sock = dns_dispatch_getsocket(client->dispatch);
isc_socket_attach(sock, &client->udpsocket);
}
client->manager = manager;
ISC_LIST_APPEND(manager->active, client, link);
client->list = &manager->active;
ev = &client->ctlevent;
isc_task_send(client->task, &ev);
}
if (i != 0) {
/*
* We managed to create at least one client, so we
* declare victory.
*/
result = ISC_R_SUCCESS;
}
UNLOCK(&manager->lock);
return (result);
}
isc_sockaddr_t *
ns_client_getsockaddr(ns_client_t *client) {
return (&client->peeraddr);
}
isc_result_t
ns_client_checkacl(ns_client_t *client,
const char *opname, dns_acl_t *acl,
isc_boolean_t default_allow, int log_level)
{
isc_result_t result;
int match;
isc_netaddr_t netaddr;
if (acl == NULL) {
if (default_allow)
goto allow;
else
goto deny;
}
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
result = dns_acl_match(&netaddr, client->signer, acl,
&ns_g_server->aclenv,
&match, NULL);
if (result != ISC_R_SUCCESS)
goto deny; /* Internal error, already logged. */
if (match > 0)
goto allow;
goto deny; /* Negative match or no match. */
allow:
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"%s approved", opname);
return (ISC_R_SUCCESS);
deny:
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT,
log_level, "%s denied", opname);
return (DNS_R_REFUSED);
}
static void
ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
isc_logmodule_t *module, int level, const char *fmt, va_list ap)
{
char msgbuf[2048];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
if (client->peeraddr_valid) {
isc_sockaddr_format(&client->peeraddr,
peerbuf, sizeof peerbuf);
isc_log_write(ns_g_lctx, category, module, level,
"client %s: %s", peerbuf, msgbuf);
} else {
isc_log_write(ns_g_lctx, category, module, level,
"client @%p: %s", client, msgbuf);
}
}
void
ns_client_log(ns_client_t *client, isc_logcategory_t *category,
isc_logmodule_t *module, int level, const char *fmt, ...)
{
va_list ap;
if (! isc_log_wouldlog(ns_g_lctx, level))
return;
va_start(ap, fmt);
ns_client_logv(client, category, module, level, fmt, ap);
va_end(ap);
}