client.c revision 06430b1f6c10fed7aa079a02d1d24f2c366d952b
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence/*
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater * Copyright (C) 1999, 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * copyright notice and this permission notice appear in all copies.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/print.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/task.h>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <isc/string.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/timer.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/util.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <dns/dispatch.h>
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <dns/events.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <dns/message.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <dns/rdata.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/rdatalist.h>
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence#include <dns/rdataset.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <dns/view.h>
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#include <dns/zone.h>
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#include <named/interfacemgr.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <named/log.h>
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#include <named/notify.h>
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence#include <named/server.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <named/update.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence/***
e19501436a92cd48eba2ff47d90fa49c661ec8d8Brian Wellington *** Client
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ***/
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
669e9657c731176df235832367f61435f7b83ddfAndreas Gustafsson/*
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * Important note!
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * All client state changes, other than that from idle to listening, occur
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * as a result of events. This guarantees serialization and avoids the
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * need for locking.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * If a routine is ever created that allows someone other than the client's
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * task to change the client, then the client will have to be locked.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews#define NS_CLIENT_TRACE
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews#ifdef NS_CLIENT_TRACE
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#define CTRACE(m) ns_client_log(client, \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington NS_LOGCATEGORY_CLIENT, \
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington NS_LOGMODULE_CLIENT, \
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ISC_LOG_DEBUG(3), \
3f96cf3e4f96b36cc1ad2ec7edc5b8e285fced8fBrian Wellington "%s", (m))
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#define MTRACE(m) isc_log_write(ns_g_lctx, \
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington NS_LOGCATEGORY_GENERAL, \
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence NS_LOGMODULE_CLIENT, \
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence ISC_LOG_DEBUG(3), \
80b67b3a4f2d9fc7fdd32a50edc67ff189894da2Danny Mayer "clientmgr @%p: %s", manager, (m))
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews#else
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence#define CTRACE(m) ((void)(m))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define MTRACE(m) ((void)(m))
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#endif
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#define TCP_BUFFER_SIZE (65535 + 2)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews#define SEND_BUFFER_SIZE 2048
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define RECV_BUFFER_SIZE 2048
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstruct ns_clientmgr {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /* Unlocked. */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int magic;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence isc_mem_t * mctx;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington isc_taskmgr_t * taskmgr;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_timermgr_t * timermgr;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_mutex_t lock;
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington /* Locked by lock. */
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews isc_boolean_t exiting;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews client_list_t active; /* Active clients */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence client_list_t inactive; /* To be recycled */
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington};
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#define MANAGER_MAGIC 0x4E53436DU /* NSCm */
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#define VALID_MANAGER(m) ((m) != NULL && \
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence (m)->magic == MANAGER_MAGIC)
6d6529b5e5ab7223fa2560ebe144bcb82517cef3Mark Andrews
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater/*
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * Client object states. Ordering is significant: higher-numbered
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * states are generally "more active", meaning that the client can
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * have more dynamically allocated data, outstanding events, etc.
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * In the list below, any such properties listed for state N
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * also apply to any state > N.
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson *
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * To force the client into a less active state, set client->newstate
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * to that state and call exit_check(). This will cause any
5fe21da364d4397c9a413fe689ce82dea36a7b29Mark Andrews * activities defined for higher-numbered states to be aborted.
5fe21da364d4397c9a413fe689ce82dea36a7b29Mark Andrews */
7502c6600645f120434d84d0ce3df7c3585cfe43Mark Andrews
f29583dca7dc33d6fad6942a6eb4457c694a3974Brian Wellington#define NS_CLIENTSTATE_FREED 0
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater/*
7502c6600645f120434d84d0ce3df7c3585cfe43Mark Andrews * The client object no longer exists.
817a7fb62d05222fb287bbcde910ab3669f6d237Mark Andrews */
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater
2a71a21346ce6a098b7daf98358d5bdced8e6eb6Mark Andrews#define NS_CLIENTSTATE_INACTIVE 1
2a71a21346ce6a098b7daf98358d5bdced8e6eb6Mark Andrews/*
8b3fb4bded905d65153678f48a006eed5c13b27eAndreas Gustafsson * The client object exists and has a task and timer.
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * Its "query" struct and sendbuf are initialized.
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * It is on the client manager's list of inactive clients.
b326d7e3a3a50eb65dd06db007d2fddc62606bbfMark Andrews * It has a message and OPT, both in the reset state.
b326d7e3a3a50eb65dd06db007d2fddc62606bbfMark Andrews */
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson
cf300e03de3df3ff422db922520bf07c686c86daMark Andrews#define NS_CLIENTSTATE_READY 2
cf300e03de3df3ff422db922520bf07c686c86daMark Andrews/*
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * The client object is either a TCP or a UDP one, and
cf300e03de3df3ff422db922520bf07c686c86daMark Andrews * it is associated with a network interface. It is on the
cf300e03de3df3ff422db922520bf07c686c86daMark Andrews * client manager's list of active clients.
9e560b59a722d06a62b5aed761e71fec72638a7cBrian Wellington *
dfceef7e68b56f6970dd2e8eea9980ad16bcc653Brian Wellington * If it is a TCP client object, it has a TCP listener socket
9e560b59a722d06a62b5aed761e71fec72638a7cBrian Wellington * and an outstading TCP listen request.
5455f30a7532738d750252c00e649890c694ee30Brian Wellington *
5455f30a7532738d750252c00e649890c694ee30Brian Wellington * If it is a UDP client object, it is associated with a
60213f2815a7e6584a2285546d05633fa7b6f5b4Mark Andrews * dispatch and has an outstanding dispatch request.
c30d291128e099a284fa6272b91b2bd64519a209Mark Andrews */
b7ce2258cab8eadbd834de8ca9d0b6f4c71f8c48Brian Wellington
79da8302e8d78f51db5243ba6b2f7de87e667132Mark Andrews#define NS_CLIENTSTATE_READING 3
d2ef84e07b67e72a4bd9c729c6b8228067d17584Mark Andrews/*
d2ef84e07b67e72a4bd9c729c6b8228067d17584Mark Andrews * The client object is a TCP client object that has received
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * outstanding TCP read request. This state is not used for
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence * UDP client objects.
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence */
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#define NS_CLIENTSTATE_WORKING 4
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence/*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * The client object has received a request and is working
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * on it. It has a view, and it may have any of a non-reset OPT,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * recursion quota, and an outstanding write request. If it
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * is a UDP client object, it has a dispatch event.
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington */
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#define NS_CLIENTSTATE_MAX 9
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews/*
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * Sentinel value used to indicate "no state". When client->newstate
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * has this value, we are not attempting to exit the current state.
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * Must be greater than any valid state.
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater */
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void client_read(ns_client_t *client);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void client_accept(ns_client_t *client);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void clientmgr_destroy(ns_clientmgr_t *manager);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic isc_boolean_t exit_check(ns_client_t *client);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void ns_client_endrequest(ns_client_t *client);
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellingtonstatic void ns_client_checkactive(ns_client_t *client);
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington/*
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * Enter the inactive state.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Requires:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * No requests are outstanding.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonclient_deactivate(ns_client_t *client) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(NS_CLIENT_VALID(client));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington if (client->interface)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ns_interface_detach(&client->interface);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(client->naccepts == 0);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews if (client->tcplistener != NULL)
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_socket_detach(&client->tcplistener);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews if (client->dispentry != NULL) {
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews dns_dispatchevent_t **deventp;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (client->dispevent != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington deventp = &client->dispevent;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington else
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington deventp = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_dispatch_removerequest(&client->dispentry, deventp);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (client->dispatch != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_dispatch_detach(&client->dispatch);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->attributes = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->mortal = ISC_FALSE;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington LOCK(&client->manager->lock);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence ISC_LIST_UNLINK(client->manager->active, client, link);
e1d05d323526e7e65df13a6d3dfbec30f6ddb500Brian Wellington ISC_LIST_APPEND(client->manager->inactive, client, link);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews client->list = &client->manager->inactive;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews UNLOCK(&client->manager->lock);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews}
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews/*
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * Clean up a client object and free its memory.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Requires:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * The client is in the inactive state.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonclient_free(ns_client_t *client) {
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_boolean_t need_clientmgr_destroy = ISC_FALSE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ns_clientmgr_t *manager = NULL;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(NS_CLIENT_VALID(client));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * When "shuttingdown" is true, either the task has received
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * its shutdown event or no shutdown event has ever been
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * set up. Thus, we have no outstanding shutdown
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * event at this point.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ns_query_free(client);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(client->mctx, client->sendbuf, SEND_BUFFER_SIZE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_timer_detach(&client->timer);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington if (client->tcpbuf != NULL)
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington if (client->opt != NULL) {
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington INSIST(dns_rdataset_isassociated(client->opt));
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington dns_rdataset_disassociate(client->opt);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington dns_message_puttemprdataset(client->message, &client->opt);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_message_destroy(&client->message);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews if (client->task != NULL)
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_task_detach(&client->task);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews if (client->manager != NULL) {
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews manager = client->manager;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews LOCK(&manager->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ISC_LIST_UNLINK(*client->list, client, link);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->list = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (manager->exiting &&
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews ISC_LIST_EMPTY(manager->active) &&
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews ISC_LIST_EMPTY(manager->inactive))
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews need_clientmgr_destroy = ISC_TRUE;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews UNLOCK(&manager->lock);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews }
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews CTRACE("free");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews client->magic = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_mem_put(client->mctx, client, sizeof *client);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (need_clientmgr_destroy)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews clientmgr_destroy(manager);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews}
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsstatic void
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsset_timeout(ns_client_t *client, unsigned int seconds) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_result_t result;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_interval_t interval;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_interval_set(&interval, seconds, 0);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews &interval, ISC_FALSE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result != ISC_R_SUCCESS) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews ns_client_log(client, NS_LOGCATEGORY_CLIENT,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews "setting timouet: %s",
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_result_totext(result));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews /* Continue anyway. */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews }
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews}
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews/*
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Check for a deactivation or shutdown request and take appropriate
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * action. Returns ISC_TRUE if either is in progress; in this case
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * the caller must no longer use the client object as it may have been
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * freed.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsstatic isc_boolean_t
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsexit_check(ns_client_t *client) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews REQUIRE(NS_CLIENT_VALID(client));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (client->state <= client->newstate)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews return (ISC_FALSE); /* Business as usual. */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews INSIST(client->newstate < NS_CLIENTSTATE_WORKING);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews /*
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * We need to detach from the view early when shutting down
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * the server to break the following vicious circle:
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews *
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * - The resolver will not shut down until the view refcount is zero
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * - The view refcount does not go to zero until all clients detach
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * - The client does not detach from the view until references is zero
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * - references does not go to zero until the resolver has shut down
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews *
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_view_detach(&client->view);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (client->state == NS_CLIENTSTATE_WORKING) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews INSIST(client->newstate <= NS_CLIENTSTATE_READING);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews /*
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * We are trying to abort request processing.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (client->nsends > 0) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_socket_t *socket;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (TCP_CLIENT(client))
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews socket = client->tcpsocket;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews else
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews socket =
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_dispatch_getsocket(client->dispatch);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_socket_cancel(socket, client->task,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews ISC_SOCKCANCEL_SEND);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews }
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (! (client->nsends == 0 && client->references == 0)) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews /*
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Still waiting for I/O cancel completion.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * or lingering references.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * I/O cancel is complete. Burn down all state
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * related to the current request.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ns_client_endrequest(client);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->state = NS_CLIENTSTATE_READING;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (NS_CLIENTSTATE_READING == client->newstate) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client_read(client);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington client->newstate = NS_CLIENTSTATE_MAX;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington return (ISC_TRUE); /* We're done. */
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington }
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews }
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington if (client->state == NS_CLIENTSTATE_READING) {
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews /*
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * We are trying to abort the current TCP connection,
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews * if any.
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington */
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington INSIST(client->newstate <= NS_CLIENTSTATE_READY);
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington if (client->nreads > 0)
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews dns_tcpmsg_cancelread(&client->tcpmsg);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington if (! client->nreads == 0) {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington /* Still waiting for read cancel completion. */
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews return (ISC_TRUE);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington }
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (client->tcpmsg_valid) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_tcpmsg_invalidate(&client->tcpmsg);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews client->tcpmsg_valid = ISC_FALSE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (client->tcpsocket != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CTRACE("closetcp");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_socket_detach(&client->tcpsocket);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (client->tcpquota != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_quota_detach(&client->tcpquota);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington (void) isc_timer_reset(client->timer, isc_timertype_inactive,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington NULL, NULL, ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->peeraddr_valid = ISC_FALSE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->state = NS_CLIENTSTATE_READY;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Now the client is ready to accept a new TCP connection
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * or UDP request, but we may have enough clients doing
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * that already. Check whether this client needs to remain
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington * active and force it to go inactive if not.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ns_client_checkactive(client);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (NS_CLIENTSTATE_READY == client->newstate) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (TCP_CLIENT(client)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client_accept(client);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Give the processed dispatch event back to
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * the dispatch. This tells the dispatch
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * that we are ready to receive the next event.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington dns_dispatch_freeevent(client->dispatch,
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence client->dispentry,
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington &client->dispevent);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence }
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence client->newstate = NS_CLIENTSTATE_MAX;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence return (ISC_TRUE);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews }
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews }
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews if (client->state == NS_CLIENTSTATE_READY) {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews /*
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * We are trying to enter the inactive state.
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews */
9de0f9b0aed432ee357dbba8d1d807525f4b6d4aMark Andrews if (client->naccepts > 0)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_socket_cancel(client->tcplistener, client->task,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews ISC_SOCKCANCEL_ACCEPT);
9de0f9b0aed432ee357dbba8d1d807525f4b6d4aMark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (! (client->naccepts == 0)) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews /* Still waiting for accept cancel completion. */
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews return (ISC_TRUE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington /* Accept cancel is complete. */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client_deactivate(client);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington client->state = NS_CLIENTSTATE_INACTIVE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (client->state == client->newstate) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington client->newstate = NS_CLIENTSTATE_MAX;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington return (ISC_TRUE); /* We're done. */
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington }
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington }
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington if (client->state == NS_CLIENTSTATE_INACTIVE) {
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews INSIST(client->newstate == NS_CLIENTSTATE_FREED);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington /*
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * We are trying to free the client.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client_free(client);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews return (ISC_TRUE);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence }
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence return (ISC_TRUE);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews}
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews/*
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * The client's task has received a shutdown event.
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews */
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic void
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsclient_shutdown(isc_task_t *task, isc_event_t *event) {
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews ns_client_t *client;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews REQUIRE(event != NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews client = event->ev_arg;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews REQUIRE(NS_CLIENT_VALID(client));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(task == client->task);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews UNUSED(task);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews CTRACE("shutdown");
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_event_free(&event);
504f7802d4c9b43db4820f496c4d00e078effa18David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->shutdown != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence client->shutdown = NULL;
504f7802d4c9b43db4820f496c4d00e078effa18David Lawrence client->shutdown_arg = NULL;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client->newstate = NS_CLIENTSTATE_FREED;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence (void)exit_check(client);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence}
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic void
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencens_client_endrequest(ns_client_t *client) {
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence INSIST(client->naccepts == 0);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews INSIST(client->nreads == 0);
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson INSIST(client->nsends == 0);
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson INSIST(client->state == NS_CLIENTSTATE_WORKING);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews CTRACE("endrequest");
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->next != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (client->next)(client);
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson client->next = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->view != NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_view_detach(&client->view);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->opt != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews INSIST(dns_rdataset_isassociated(client->opt));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_rdataset_disassociate(client->opt);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_message_puttemprdataset(client->message, &client->opt);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client->udpsize = 512;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->recursionquota != NULL)
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater isc_quota_detach(&client->recursionquota);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews}
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsstatic void
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsns_client_checkactive(ns_client_t *client) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->mortal) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * This client object should normally go inactive
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * at this point, but if we have fewer active client
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * objects than desired due to earlier quota exhaustion,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * keep it active to make up for the shortage.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_boolean_t need_another_client = ISC_FALSE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (TCP_CLIENT(client)) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews LOCK(&client->interface->lock);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->interface->ntcpcurrent <
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client->interface->ntcptarget)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews need_another_client = ISC_TRUE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews UNLOCK(&client->interface->lock);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews } else {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * The UDP client quota is enforced by making
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * requests fail rather than by not listening
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * for new ones. Therefore, there is always a
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * full set of UDP clients listening.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (! need_another_client) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * We don't need this client object. Recycle it.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->newstate >= NS_CLIENTSTATE_INACTIVE)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client->newstate = NS_CLIENTSTATE_INACTIVE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews}
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsvoid
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsns_client_next(ns_client_t *client, isc_result_t result) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews int newstate;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(NS_CLIENT_VALID(client));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews CTRACE("next");
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (result != ISC_R_SUCCESS)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews "request failed: %s", isc_result_totext(result));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * An error processing a TCP request may have left
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * the connection out of sync. To be safe, we always
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * sever the connection when result != ISC_R_SUCCESS.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews newstate = NS_CLIENTSTATE_READING;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews else
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews newstate = NS_CLIENTSTATE_READY;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (client->newstate > newstate)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client->newstate = newstate;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (void) exit_check(client);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews}
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsstatic void
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsclient_senddone(isc_task_t *task, isc_event_t *event) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews ns_client_t *client;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_socketevent_t *sevent = (isc_socketevent_t *) event;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews UNUSED(task);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(sevent != NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews client = sevent->ev_arg;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(NS_CLIENT_VALID(client));
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews REQUIRE(task == client->task);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews CTRACE("senddone");
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (sevent->result != ISC_R_SUCCESS)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews ns_client_log(client, NS_LOGCATEGORY_CLIENT,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews "error sending response: %s",
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_result_totext(sevent->result));
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews INSIST(client->nsends > 0);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews client->nsends--;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (client->tcpbuf != NULL) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews INSIST(TCP_CLIENT(client));
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews client->tcpbuf = NULL;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews isc_event_free(&event);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (exit_check(client))
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews return;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews ns_client_next(client, ISC_R_SUCCESS);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews}
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrewsvoid
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrewsns_client_send(ns_client_t *client) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_result_t result;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews unsigned char *data;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_buffer_t buffer;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_buffer_t tcpbuffer;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_region_t r;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_socket_t *socket;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_sockaddr_t *address;
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater struct in6_pktinfo *pktinfo;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews unsigned int bufsize = 512;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews REQUIRE(NS_CLIENT_VALID(client));
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews CTRACE("send");
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if ((client->attributes & NS_CLIENTATTR_RA) != 0)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews client->message->flags |= DNS_MESSAGEFLAG_RA;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews /*
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * XXXRTH The following doesn't deal with TSIGs, TCP buffer resizing,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * or ENDS1 more data packets.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews */
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (TCP_CLIENT(client)) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews /*
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * XXXRTH "tcpbuffer" is a hack to get things working.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews */
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews INSIST(client->tcpbuf == NULL);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (client->tcpbuf == NULL)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews goto done;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews data = client->tcpbuf;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_buffer_init(&tcpbuffer, data, TCP_BUFFER_SIZE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_buffer_init(&buffer, data + 2, TCP_BUFFER_SIZE - 2);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews } else {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews data = client->sendbuf;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (client->udpsize < SEND_BUFFER_SIZE)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews bufsize = client->udpsize;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews else
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews bufsize = SEND_BUFFER_SIZE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_buffer_init(&buffer, data, bufsize);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = dns_message_renderbegin(client->message, &buffer);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result != ISC_R_SUCCESS)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews goto done;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (client->opt != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = dns_message_setopt(client->message, client->opt);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result != ISC_R_SUCCESS)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews goto done;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews /*
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * XXXRTH dns_message_setopt() should probably do this...
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews */
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews client->opt = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = dns_message_rendersection(client->message,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews DNS_SECTION_QUESTION, 0);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result != ISC_R_SUCCESS)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews goto done;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = dns_message_rendersection(client->message,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews DNS_SECTION_ANSWER, 0);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result == ISC_R_NOSPACE) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews client->message->flags |= DNS_MESSAGEFLAG_TC;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews goto renderend;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence }
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (result != ISC_R_SUCCESS)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence goto done;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = dns_message_rendersection(client->message,
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence DNS_SECTION_AUTHORITY, 0);
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence if (result == ISC_R_NOSPACE) {
4716e94840921878b26e493576f84afe4fe08752Mark Andrews client->message->flags |= DNS_MESSAGEFLAG_TC;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington goto renderend;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington }
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington if (result != ISC_R_SUCCESS)
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington goto done;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington result = dns_message_rendersection(client->message,
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington DNS_SECTION_ADDITIONAL, 0);
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews goto done;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews renderend:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = dns_message_renderend(client->message);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (result != ISC_R_SUCCESS)
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson goto done;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington if (TCP_CLIENT(client)) {
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence socket = client->tcpsocket;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews address = NULL;
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington isc_buffer_usedregion(&buffer, &r);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_buffer_add(&tcpbuffer, r.length);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_buffer_usedregion(&tcpbuffer, &r);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews } else {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews socket = dns_dispatch_getsocket(client->dispatch);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews address = &client->dispevent->addr;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_buffer_usedregion(&buffer, &r);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson CTRACE("sendto");
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews pktinfo = &client->pktinfo;
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews else
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews pktinfo = NULL;
fc8197c3ce31d81cd5e23703680572fac09a2e8aMark Andrews result = isc_socket_sendto(socket, &r, client->task, client_senddone,
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer client, address, pktinfo);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (result == ISC_R_SUCCESS) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews client->nsends++;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews return;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews done:
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (client->tcpbuf != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews client->tcpbuf = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews ns_client_next(client, result);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews}
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrencevoid
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencens_client_error(ns_client_t *client, isc_result_t result) {
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence dns_rcode_t rcode;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence dns_message_t *message;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(NS_CLIENT_VALID(client));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews CTRACE("error");
5096958739769958dd7a6b69356bf41260033873David Lawrence
b39ad8a69bc2859b99c9f5a63d916789b566e470Andreas Gustafsson message = client->message;
5096958739769958dd7a6b69356bf41260033873David Lawrence rcode = dns_result_torcode(result);
5096958739769958dd7a6b69356bf41260033873David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * message may be an in-progress reply that we had trouble
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * with, in which case QR will be set. We need to clear QR before
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * calling dns_message_reply() to avoid triggering an assertion.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington message->flags &= ~DNS_MESSAGEFLAG_QR;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * AA and AD shouldn't be set.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = dns_message_reply(message, ISC_TRUE);
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence if (result != ISC_R_SUCCESS) {
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence /*
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * It could be that we've got a query with a good header,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * but a bad question section, so we try again with
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer * want_question_section set to ISC_FALSE.
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence */
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence result = dns_message_reply(message, ISC_FALSE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result != ISC_R_SUCCESS) {
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence ns_client_next(client, result);
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence return;
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence }
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence message->rcode = rcode;
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews ns_client_send(client);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews}
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrewsstatic inline isc_result_t
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrewsclient_addopt(ns_client_t *client) {
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews dns_rdataset_t *rdataset;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_rdatalist_t *rdatalist;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_rdata_t *rdata;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence isc_result_t result;
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews REQUIRE(client->opt == NULL); /* XXXRTH free old. */
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence rdatalist = NULL;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = dns_message_gettemprdatalist(client->message, &rdatalist);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (result != ISC_R_SUCCESS)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (result);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence rdata = NULL;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = dns_message_gettemprdata(client->message, &rdata);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (result != ISC_R_SUCCESS)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence return (result);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence rdataset = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews result = dns_message_gettemprdataset(client->message, &rdataset);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (result != ISC_R_SUCCESS)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (result);
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence dns_rdataset_init(rdataset);
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rdatalist->type = dns_rdatatype_opt;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence rdatalist->covers = 0;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington /*
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * Set Maximum UDP buffer size.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington */
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington rdatalist->rdclass = RECV_BUFFER_SIZE;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington /*
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * Set EXTENDED-RCODE, VERSION, and Z to 0.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington */
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater rdatalist->ttl = 0;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington /*
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * No ENDS options.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews rdata->data = NULL;
d7ba3622ffa20c653ef6c8cfae42d8cd26465b7fBrian Wellington rdata->length = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rdata->rdclass = rdatalist->rdclass;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence rdata->type = rdatalist->type;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
6dc32b43da2c7af5bf460a45a17d03226c162c91Michael Sawyer ISC_LIST_INIT(rdatalist->rdata);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_rdatalist_tordataset(rdatalist, rdataset);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson client->opt = rdataset;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson return (ISC_R_SUCCESS);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson}
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson/*
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * Handle an incoming request event from the dispatch (UDP case)
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * or tcpmsg (TCP case).
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson */
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafssonstatic void
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafssonclient_request(isc_task_t *task, isc_event_t *event) {
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson ns_client_t *client;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_dispatchevent_t *devent;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson isc_result_t result;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson isc_buffer_t *buffer;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_view_t *view;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_rdataset_t *opt;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence isc_boolean_t ra; /* Recursion available. */
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson REQUIRE(event != NULL);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson client = event->ev_arg;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(NS_CLIENT_VALID(client));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(task == client->task);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington UNUSED(task);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(client->recursionquota == NULL);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews INSIST(client->state ==
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews TCP_CLIENT(client) ?
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington NS_CLIENTSTATE_READING :
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson NS_CLIENTSTATE_READY);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson RWLOCK(&ns_g_server->conflock, isc_rwlocktype_read);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson dns_zonemgr_lockconf(ns_g_server->zonemgr, isc_rwlocktype_read);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington if (event->ev_type == DNS_EVENT_DISPATCH) {
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews INSIST(!TCP_CLIENT(client));
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence devent = (dns_dispatchevent_t *)event;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(client->dispentry != NULL);
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence client->dispevent = devent;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington buffer = &devent->buffer;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington result = devent->result;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington client->peeraddr = devent->addr;
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence client->peeraddr_valid = ISC_TRUE;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington if ((devent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington client->attributes |= NS_CLIENTATTR_PKTINFO;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington client->pktinfo = devent->pktinfo;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington } else {
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington client->attributes &= ~NS_CLIENTATTR_PKTINFO;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington }
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews if ((devent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews client->attributes |= NS_CLIENTATTR_MULTICAST;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington else
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington client->attributes &= ~NS_CLIENTATTR_MULTICAST;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence } else {
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington INSIST(TCP_CLIENT(client));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(event->ev_sender == &client->tcpmsg);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington buffer = &client->tcpmsg.buffer;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = client->tcpmsg.result;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(client->nreads == 1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
* 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_serverlock;
client->state = client->newstate = NS_CLIENTSTATE_WORKING;
isc_stdtime_get(&client->requesttime);
client->now = client->requesttime;
set_timeout(client, 60);
if (result != ISC_R_SUCCESS) {
if (TCP_CLIENT(client))
ns_client_next(client, result);
else
isc_task_shutdown(client->task);
goto cleanup_serverlock;
}
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, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
ns_client_error(client, result);
goto cleanup_serverlock;
}
/*
* We expect a query, not a response. Unexpected UDP responses
* are discarded early by the dispatcher, but TCP responses
* bypass the dispatcher and must be discarded here.
*/
if ((client->message->flags & DNS_MESSAGEFLAG_QR) != 0) {
CTRACE("unexpected response");
ns_client_next(client, DNS_R_FORMERR);
goto cleanup_serverlock;
}
/*
* 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;
/*
* Create an OPT for our reply.
*/
result = client_addopt(client);
if (result != ISC_R_SUCCESS) {
ns_client_error(client, result);
goto cleanup_serverlock;
}
/*
* 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_serverlock;
}
}
/*
* Find a view that matches the client's source address.
*
* XXXRTH View list management code will be moving to its own module
* soon.
*/
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) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
"no matching view");
ns_client_error(client, DNS_R_REFUSED);
goto cleanup_serverlock;
}
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
"using view '%s'", view->name);
/*
* Lock the view's configuration data for reading.
* We must attach a separate view reference for this
* purpose instad of using client->view, because
* client->view may or may not be detached at the point
* when we return from this event handler depending
* on whether the request handler causes ns_client_next()
* to be called or not.
*/
dns_view_attach(client->view, &client->lockview);
RWLOCK(&client->lockview->conflock, isc_rwlocktype_read);
/*
* 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.
*/
result = dns_message_checksig(client->message, client->view);
if (result != ISC_R_SUCCESS) {
ns_client_error(client, result);
goto cleanup_viewlock;
}
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");
} 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));
}
/*
* 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_TRUE) == 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_update_start(client);
break;
case dns_opcode_notify:
CTRACE("notify");
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_viewlock:
RWUNLOCK(&client->lockview->conflock, isc_rwlocktype_read);
dns_view_detach(&client->lockview);
cleanup_serverlock:
dns_zonemgr_unlockconf(ns_g_server->zonemgr, isc_rwlocktype_read);
RWUNLOCK(&ns_g_server->conflock, isc_rwlocktype_read);
}
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->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)
goto cleanup_message;
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->references = 0;
client->attributes = 0;
client->view = NULL;
client->lockview = NULL;
client->dispatch = NULL;
client->dispentry = NULL;
client->dispevent = NULL;
client->tcplistener = NULL;
client->tcpsocket = NULL;
client->tcpmsg_valid = ISC_FALSE;
client->tcpbuf = NULL;
client->opt = NULL;
client->udpsize = 512;
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_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_sendbuf;
CTRACE("create");
*clientp = client;
return (ISC_R_SUCCESS);
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.
*/
set_timeout(client, 30);
client->state = client->newstate = NS_CLIENTSTATE_READING;
INSIST(client->nreads == 0);
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;
(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) {
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 35 (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));
}
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);
}
void
ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
REQUIRE(NS_CLIENT_VALID(source));
REQUIRE(targetp != NULL && *targetp == NULL);
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;
(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");
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++) {
/*
* 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;
}
client->state = NS_CLIENTSTATE_READY;
ns_interface_attach(ifp, &client->interface);
if (tcp) {
client->attributes |= NS_CLIENTATTR_TCP;
isc_socket_attach(ifp->tcpsocket,
&client->tcplistener);
client_accept(client);
} else {
dns_dispatch_attach(ifp->udpdispatch,
&client->dispatch);
result = dns_dispatch_addrequest(client->dispatch,
client->task,
client_request,
client,
&client->dispentry);
if (result != ISC_R_SUCCESS) {
ns_client_log(client,
DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT,
ISC_LOG_DEBUG(3),
"dns_dispatch_addrequest() "
"failed: %s",
isc_result_totext(result));
isc_task_shutdown(client->task);
break;
}
}
client->manager = manager;
ISC_LIST_APPEND(manager->active, client, link);
client->list = &manager->active;
}
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, isc_boolean_t logfailure)
{
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,
logfailure ? ISC_LOG_ERROR : ISC_LOG_DEBUG(3),
"%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[256];
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;
va_start(ap, fmt);
ns_client_logv(client, category, module, level, fmt, ap);
va_end(ap);
}