client.c revision ae8d0aedd64a7580dc2fc4a9cd61934527552f3b
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1999, 2000 Internet Software Consortium.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * SOFTWARE.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <config.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <string.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/assertions.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/mem.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/mutex.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <isc/result.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/task.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/timer.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <isc/util.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/acl.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/dispatch.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/events.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/message.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdata.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/rdatalist.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/rdataset.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/view.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/zone.h>
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff#include <named/client.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/globals.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/interfacemgr.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/log.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/notify.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/query.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/server.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <named/update.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/***
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *** Client
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ***/
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Important note!
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * All client state changes, other than that from idle to listening, occur
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * as a result of events. This guarantees serialization and avoids the
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * need for locking.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * If a routine is ever created that allows someone other than the client's
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * task to change the client, then the client will have to be locked.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define NS_CLIENT_TRACE
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#ifdef NS_CLIENT_TRACE
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define CTRACE(m) isc_log_write(ns_g_lctx, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NS_LOGCATEGORY_CLIENT, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NS_LOGMODULE_CLIENT, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_LOG_DEBUG(3), \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson "client %p: %s", client, (m))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define MTRACE(m) isc_log_write(ns_g_lctx, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NS_LOGCATEGORY_GENERAL, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NS_LOGMODULE_CLIENT, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_LOG_DEBUG(3), \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson "clientmgr %p: %s", manager, (m))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#endif
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define SEND_BUFFER_SIZE 2048
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstruct ns_clientmgr {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Unlocked. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int magic;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_mem_t * mctx;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_taskmgr_t * taskmgr;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_timermgr_t * timermgr;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_mutex_t lock;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Locked by lock. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t exiting;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client_list_t active; /* Active clients */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client_list_t inactive; /* To be recycled */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson};
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define MANAGER_MAGIC 0x4E53436DU /* NSCm */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define VALID_MANAGER(m) ((m) != NULL && \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (m)->magic == MANAGER_MAGIC)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Client object states. Ordering is significant: higher-numbered
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * states are generally "more active", meaning that the client can
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * have more dynamically allocated data, outstanding events, etc.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * In the list below, any such properties listed for state N
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * also apply to any state > N.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * To force the client into a less active state, set client->newstate
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * to that state and call exit_check(). This will cause any
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * activities defined for higher-numbered states to be aborted.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define NS_CLIENTSTATE_FREED 0
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson/*
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * The client object no longer exists.
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define NS_CLIENTSTATE_INACTIVE 1
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The client object exists and has a task and timer.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Its "query" struct and sendbuf are initialized.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * It is on the client manager's list of inactive clients.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * It has a message and OPT, both in the reset state.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define NS_CLIENTSTATE_READY 2
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley * The client object is either a TCP or a UDP one, and
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * it is associated with a network interface. It is on the
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * client manager's list of active clients.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * If it is a TCP client object, it has a TCP listener socket
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * and an outstading TCP listen request.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * If it is a UDP client object, it is associated with a
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * dispatch and has an outstanding dispatch request.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define NS_CLIENTSTATE_READING 3
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The client object is a TCP client object that has received
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * outstanding TCP read request. This state is not used for
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * UDP client objects.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define NS_CLIENTSTATE_WORKING 4
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The client object has received a request and is working
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * on it. It has a view, and it may have any of a non-reset OPT,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * recursion quota, and an outstanding write request. If it
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graff * is a UDP client object, it has a dispatch event.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graff#define NS_CLIENTSTATE_MAX 9
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson * Sentinel value used to indicate "no state". When client->newstate
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson * has this value, we are not attempting to exit the current state.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Must be greater than any valid state.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void client_read(ns_client_t *client);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic void client_accept(ns_client_t *client);
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halleystatic void clientmgr_destroy(ns_clientmgr_t *manager);
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halleystatic isc_boolean_t exit_check(ns_client_t *client);
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halleystatic void ns_client_endrequest(ns_client_t *client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void ns_client_checkactive(ns_client_t *client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
544f5611e1fc84597db819d111c8fd98bb719de6Bob Halley/*
544f5611e1fc84597db819d111c8fd98bb719de6Bob Halley * Format a human-readable representation of the socket address '*sa'
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * into the character array 'array', which is of size 'size'.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The resulting string is guaranteed to be null-terminated.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonsockaddr_format(isc_sockaddr_t *sa, char *array, unsigned int size)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_result_t result;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t buf;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_init(&buf, array, size, ISC_BUFFERTYPE_TEXT);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = isc_sockaddr_totext(sa, &buf);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence strncpy(array, "<unknown address>", size);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson array[size-1] = '\0';
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Enter the inactive state.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Requires:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * No requests are outstanding.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonclient_deactivate(ns_client_t *client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->interface)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_interface_detach(&client->interface);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->naccepts == 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->tcplistener != NULL)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_socket_detach(&client->tcplistener);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (client->dispentry != NULL) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_dispatchevent_t **deventp;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (client->dispevent != NULL)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson deventp = &client->dispevent;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson else
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson deventp = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_dispatch_removerequest(client->dispatch,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson &client->dispentry,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson deventp);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->dispatch != NULL)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_dispatch_detach(&client->dispatch);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->attributes = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->mortal = ISC_FALSE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson LOCK(&client->manager->lock);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ISC_LIST_UNLINK(client->manager->active, client, link);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_LIST_APPEND(client->manager->inactive, client, link);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->list = &client->manager->inactive;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff UNLOCK(&client->manager->lock);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence}
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Clean up a client object and free its memory.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Requires:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The client is in the inactive state.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceclient_free(ns_client_t *client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t need_clientmgr_destroy = ISC_FALSE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_clientmgr_t *manager = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley /*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * When "shuttingdown" is true, either the task has received
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * its shutdown event or no shutdown event has ever been
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * set up. Thus, we have no outstanding shutdown
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * event at this point.
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_query_free(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_mem_put(client->mctx, client->sendbuf, SEND_BUFFER_SIZE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_timer_detach(&client->timer);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (client->opt != NULL) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(dns_rdataset_isassociated(client->opt));
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson dns_rdataset_disassociate(client->opt);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_message_puttemprdataset(client->message, &client->opt);
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_message_destroy(&client->message);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (client->task != NULL)
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley isc_task_detach(&client->task);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (client->manager != NULL) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson manager = client->manager;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson LOCK(&manager->lock);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ISC_LIST_UNLINK(*client->list, client, link);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->list = NULL;
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley if (manager->exiting &&
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_LIST_EMPTY(manager->active) &&
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_LIST_EMPTY(manager->inactive))
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson need_clientmgr_destroy = ISC_TRUE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson UNLOCK(&manager->lock);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("free");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->magic = 0;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson isc_mem_put(client->mctx, client, sizeof *client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (need_clientmgr_destroy)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson clientmgr_destroy(manager);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonset_timeout(ns_client_t *client, unsigned int seconds) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_result_t result;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_interval_t interval;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_interval_set(&interval, seconds, 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson &interval, ISC_FALSE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_log_write(dns_lctx, NS_LOGCATEGORY_CLIENT,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson "setting timouet: %s",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_result_totext(result));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Continue anyway. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Check for a deactivation or shutdown request and take appropriate
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * action. Returns ISC_TRUE if either is in progress; in this case
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * the caller must no longer use the client object as it may have been
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * freed.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_boolean_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonexit_check(ns_client_t *client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->state <= client->newstate)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_FALSE); /* Business as usual. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->newstate < NS_CLIENTSTATE_WORKING);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * We need to detach from the view early when shutting down
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * the server to break the following vicious circle:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * - The resolver will not shut down until the view refcount is zero
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * - The view refcount does not go to zero until all clients detach
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * - The client does not detach from the view until references is zero
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * - references does not go to zero until the resolver has shut down
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_view_detach(&client->view);
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley if (client->state == NS_CLIENTSTATE_WORKING) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->newstate <= NS_CLIENTSTATE_READING);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * We are trying to abort request processing.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->nsends > 0) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_socket_t *socket;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (TCP_CLIENT(client))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson socket = client->tcpsocket;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence else
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence socket =
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_dispatch_getsocket(client->dispatch);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_socket_cancel(socket, client->task,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_SOCKCANCEL_SEND);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (! (client->nsends == 0 && client->references == 0)) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Still waiting for I/O cancel completion.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * or lingering references.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * I/O cancel is complete. Burn down all state
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * related to the current request.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_client_endrequest(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->state = NS_CLIENTSTATE_READING;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (NS_CLIENTSTATE_READING == client->newstate) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client_read(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->newstate = NS_CLIENTSTATE_MAX;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE); /* We're done. */
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->state == NS_CLIENTSTATE_READING) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * We are trying to abort the current TCP connection,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * if any.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence INSIST(client->newstate <= NS_CLIENTSTATE_READY);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("closetcp");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->nreads > 0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_tcpmsg_cancelread(&client->tcpmsg);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (! client->nreads == 0) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Still waiting for read cancel completion. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE);
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley }
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (client->tcpmsg_valid) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_tcpmsg_invalidate(&client->tcpmsg);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->tcpmsg_valid = ISC_FALSE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->tcpsocket != NULL)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_socket_detach(&client->tcpsocket);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (client->tcpquota != NULL)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_quota_detach(&client->tcpquota);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (void) isc_timer_reset(client->timer, isc_timertype_inactive,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NULL, NULL, ISC_TRUE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff client->state = NS_CLIENTSTATE_READY;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Now the client is ready to accept a new TCP connection
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * or UDP request, but we may have enough clients doing
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * that already. Check whether this client needs to remain
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * active and force it to go inactive if not.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_client_checkactive(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (NS_CLIENTSTATE_READY == client->newstate) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (TCP_CLIENT(client)) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client_accept(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson } else {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * Give the processed dispatch event back to
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * the dispatch. This tells the dispatch
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * that we are ready to receive the next event.
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson */
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson dns_dispatch_freeevent(client->dispatch,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson client->dispentry,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson &client->dispevent);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence client->newstate = NS_CLIENTSTATE_MAX;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->state == NS_CLIENTSTATE_READY) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * We are trying to enter the inactive state.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->naccepts > 0)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_socket_cancel(client->tcplistener, client->task,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_SOCKCANCEL_ACCEPT);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (! (client->naccepts == 0)) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Still waiting for accept cancel completion. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Accept cancel is complete. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client_deactivate(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->state = NS_CLIENTSTATE_INACTIVE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->state == client->newstate) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->newstate = NS_CLIENTSTATE_MAX;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE); /* We're done. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->state == NS_CLIENTSTATE_INACTIVE) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->newstate == NS_CLIENTSTATE_FREED);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * We are trying to free the client.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client_free(client);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff return (ISC_TRUE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_TRUE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The client's task has received a shutdown event.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff */
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halleystatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonclient_shutdown(isc_task_t *task, isc_event_t *event) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_client_t *client;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(event != NULL);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(event->type == ISC_TASKEVENT_SHUTDOWN);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client = event->arg;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(task == client->task);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("shutdown");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_event_free(&event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->shutdown != NULL) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->shutdown = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->shutdown_arg = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->newstate = NS_CLIENTSTATE_FREED;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (void) exit_check(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonns_client_endrequest(ns_client_t *client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->naccepts == 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->nreads == 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->nsends == 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->lockview == NULL);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("endrequest");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->state == NS_CLIENTSTATE_WORKING);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->next != NULL) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (client->next)(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->next = NULL;
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley if (client->view != NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_view_detach(&client->view);
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley if (client->opt != NULL) {
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley INSIST(dns_rdataset_isassociated(client->opt));
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley dns_rdataset_disassociate(client->opt);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_message_puttemprdataset(client->message, &client->opt);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->udpsize = 512;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->recursionquota != NULL)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_quota_detach(&client->recursionquota);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonns_client_checkactive(ns_client_t *client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->mortal) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * This client object should normally go inactive
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * at this point, but if we have fewer active client
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * objects than desired due to earlier quota exhaustion,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * keep it active to make up for the shortage.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t need_another_client = ISC_FALSE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (TCP_CLIENT(client)) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff LOCK(&client->interface->lock);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->interface->ntcpcurrent <
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->interface->ntcptarget)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson need_another_client = ISC_TRUE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson UNLOCK(&client->interface->lock);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson } else {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The UDP client quota is enforced by making
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * requests fail rather than by not listening
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * for new ones. Therefore, there is always a
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * full set of UDP clients listening.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (! need_another_client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * We don't need this client object. Recycle it.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->newstate >= NS_CLIENTSTATE_INACTIVE)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->newstate = NS_CLIENTSTATE_INACTIVE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonvoid
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonns_client_next(ns_client_t *client, isc_result_t result) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson int newstate;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("next");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff "request failed: %s", isc_result_totext(result));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * An error processing a TCP request may have left
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * the connection out of sync. To be safe, we always
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * sever the connection when result != ISC_R_SUCCESS.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson newstate = NS_CLIENTSTATE_READING;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson else
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson newstate = NS_CLIENTSTATE_READY;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (client->newstate > newstate)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->newstate = newstate;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (void) exit_check(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencestatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonclient_senddone(isc_task_t *task, isc_event_t *event) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_client_t *client;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_socketevent_t *sevent = (isc_socketevent_t *) event;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(sevent != NULL);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(sevent->type == ISC_SOCKEVENT_SENDDONE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client = sevent->arg;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(task == client->task);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("senddone");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(client->nsends > 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->nsends--;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_event_free(&event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (exit_check(client))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_client_next(client, ISC_R_SUCCESS);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonvoid
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonns_client_send(ns_client_t *client) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_result_t result;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned char *data;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t buffer;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_buffer_t tcpbuffer;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_region_t r;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_socket_t *socket;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_sockaddr_t *address;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson struct in6_pktinfo *pktinfo;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int bufsize = 512;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("send");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if ((client->attributes & NS_CLIENTATTR_RA) != 0)
c0d0a59d1b665423b8a0d1829d0f0da121cb3473Andreas Gustafsson client->message->flags |= DNS_MESSAGEFLAG_RA;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff data = client->sendbuf;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * XXXRTH The following doesn't deal with TSIGs, TCP buffer resizing,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * or ENDS1 more data packets.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (TCP_CLIENT(client)) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * XXXRTH "tcpbuffer" is a hack to get things working.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_buffer_init(&tcpbuffer, data, SEND_BUFFER_SIZE,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_BUFFERTYPE_BINARY);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_init(&buffer, data + 2, SEND_BUFFER_SIZE - 2,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_BUFFERTYPE_BINARY);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson } else {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (client->udpsize < SEND_BUFFER_SIZE)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson bufsize = client->udpsize;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson else
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson bufsize = SEND_BUFFER_SIZE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_init(&buffer, data, bufsize, ISC_BUFFERTYPE_BINARY);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley result = dns_message_renderbegin(client->message, &buffer);
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley if (result != ISC_R_SUCCESS)
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley goto done;
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley if (client->opt != NULL) {
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley result = dns_message_setopt(client->message, client->opt);
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley if (result != ISC_R_SUCCESS)
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley goto done;
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley /*
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley * XXXRTH dns_message_setopt() should probably do this...
980dc596269e6f1ff05ab665be7d1f563befb2b8Bob Halley */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff client->opt = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = dns_message_rendersection(client->message,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_SECTION_QUESTION, 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson goto done;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_rendersection(client->message,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_SECTION_ANSWER, 0);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result == ISC_R_NOSPACE) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->message->flags |= DNS_MESSAGEFLAG_TC;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto renderend;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson goto done;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_rendersection(client->message,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_SECTION_AUTHORITY, 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result == ISC_R_NOSPACE) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->message->flags |= DNS_MESSAGEFLAG_TC;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson goto renderend;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson goto done;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_rendersection(client->message,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_SECTION_ADDITIONAL, 0);
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graff if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson goto done;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson renderend:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_renderend(client->message);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
fb5363b8972014e3580339e9df850763b0620ac4Mark Andrews goto done;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (TCP_CLIENT(client)) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson socket = client->tcpsocket;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson address = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_used(&buffer, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_add(&tcpbuffer, r.length);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_used(&tcpbuffer, &r);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff } else {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson socket = dns_dispatch_getsocket(client->dispatch);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson address = &client->dispevent->addr;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_used(&buffer, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("sendto");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson pktinfo = &client->pktinfo;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff else
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson pktinfo = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = isc_socket_sendto(socket, &r, client->task, client_senddone,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff client, address, pktinfo);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result == ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson client->nsends++;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson done:
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graff ns_client_next(client, result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonvoid
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffns_client_error(ns_client_t *client, isc_result_t result) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_rcode_t rcode;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_message_t *message;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson CTRACE("error");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson message = client->message;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rcode = dns_result_torcode(result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * message may be an in-progress reply that we had trouble
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * with, in which case QR will be set. We need to clear QR before
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * calling dns_message_reply() to avoid triggering an assertion.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson message->flags &= ~DNS_MESSAGEFLAG_QR;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * AA and AD shouldn't be set.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_reply(message, ISC_TRUE);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * It could be that we've got a query with a good header,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * but a bad question section, so we try again with
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * want_question_section set to ISC_FALSE.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_reply(message, ISC_FALSE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ns_client_next(client, result);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley message->rcode = rcode;
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley ns_client_send(client);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic inline isc_result_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonclient_addopt(ns_client_t *client) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_rdataset_t *rdataset;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_rdatalist_t *rdatalist;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_rdata_t *rdata;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_result_t result;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson REQUIRE(client->opt == NULL); /* XXXRTH free old. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdatalist = NULL;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = dns_message_gettemprdatalist(client->message, &rdatalist);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdata = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_gettemprdata(client->message, &rdata);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdataset = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_message_gettemprdataset(client->message, &rdataset);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_rdataset_init(rdataset);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdatalist->type = dns_rdatatype_opt;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff rdatalist->covers = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * Set Maximum UDP buffer size.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdatalist->rdclass = SEND_BUFFER_SIZE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Set EXTENDED-RCODE, VERSION, and Z to 0.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdatalist->ttl = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * No ENDS options.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson rdata->data = NULL;
bad8294771671374e811afac79a20cc6927e3e2fBob Halley rdata->length = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson ISC_LIST_INIT(rdatalist->rdata);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson dns_rdatalist_tordataset(rdatalist, rdataset);
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson client->opt = rdataset;
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson return (ISC_R_SUCCESS);
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson}
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Handle an incoming request event from the dispatch (UDP case)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * or tcpmsg (TCP case).
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafssonclient_request(isc_task_t *task, isc_event_t *event) {
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson ns_client_t *client;
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson dns_dispatchevent_t *devent;
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson isc_result_t result;
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson isc_buffer_t *buffer;
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson dns_view_t *view;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_rdataset_t *opt;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t ra; /* Recursion available. */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson char peerbuf[256];
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(event != NULL);
client = event->arg;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(task == client->task);
INSIST(client->recursionquota == NULL);
INSIST(client->state ==
TCP_CLIENT(client) ?
NS_CLIENTSTATE_READING :
NS_CLIENTSTATE_READY);
RWLOCK(&ns_g_server->conflock, isc_rwlocktype_read);
dns_zonemgr_lockconf(ns_g_server->zonemgr, isc_rwlocktype_read);
if (event->type == DNS_EVENT_DISPATCH) {
INSIST(!TCP_CLIENT(client));
devent = (dns_dispatchevent_t *)event;
REQUIRE(client->dispentry != NULL);
client->dispevent = devent;
buffer = &devent->buffer;
result = devent->result;
client->peeraddr = devent->addr;
if ((devent->attributes & DNS_DISPATCHATTR_PKTINFO) != 0) {
client->attributes |= NS_CLIENTATTR_PKTINFO;
client->pktinfo = devent->pktinfo;
printf("client: interface %u\n",
client->pktinfo.ipi6_ifindex);
} else {
client->attributes &= ~NS_CLIENTATTR_PKTINFO;
}
} else {
INSIST(TCP_CLIENT(client));
REQUIRE(event->type == DNS_EVENT_TCPMSG);
REQUIRE(event->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--;
}
sockaddr_format(&client->peeraddr, peerbuf, sizeof(peerbuf));
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"client %p: %s request from %s",
client, TCP_CLIENT(client) ? "TCP" : "UDP",
peerbuf);
if (exit_check(client))
goto cleanup_serverlock;
client->state = 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;
}
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;
}
}
/*
* 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)) {
/*
* XXXRTH View matching will become more powerful later.
*/
if (client->message->rdclass == view->rdclass ||
client->message->rdclass == dns_rdataclass_any)
{
dns_view_attach(view, &client->view);
break;
}
}
if (view == NULL) {
CTRACE("no view");
ns_client_error(client, DNS_R_REFUSED);
goto cleanup_serverlock;
}
/*
* 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 whe 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 == DNS_R_SUCCESS) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request has valid signature");
client->signer = &client->signername;
} else if (result == DNS_R_NOTFOUND) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request is not signed");
} else if (result == DNS_R_NOIDENTITY) {
isc_log_write(dns_lctx, 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. */
isc_log_write(dns_lctx, 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.
*/
if (client->view->resolver == NULL) {
ra = ISC_FALSE;
} else {
ra = ISC_TRUE;
if (ns_g_server->recursion == ISC_TRUE) {
/* XXX ACL should be view specific. */
/* XXX this will log too much too early */
result = ns_client_checkacl(client, "recursion",
ns_g_server->recursionacl,
ISC_TRUE);
if (result != DNS_R_SUCCESS)
ra = ISC_FALSE;
}
}
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->type == ISC_TIMEREVENT_LIFE ||
event->type == ISC_TIMEREVENT_IDLE);
client = event->arg;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(task == client->task);
REQUIRE(client->timer != NULL);
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, manager->mctx, 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->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;
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->arg;
isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
isc_result_t result;
char peerbuf[256];
REQUIRE(event->type == ISC_SOCKEVENT_NEWCONN);
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(client->task == task);
INSIST(client->state == NS_CLIENTSTATE_READY);
CTRACE("newconn");
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);
sockaddr_format(&client->peeraddr, peerbuf, sizeof(peerbuf));
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"client %p: TCP connection from %s",
client, peerbuf);
} 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.
*/
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"client %p: accept failed: %s", client,
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) {
isc_log_write(ns_g_lctx, 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 (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) {
isc_log_write(dns_lctx,
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_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 != DNS_R_SUCCESS)
goto deny; /* Internal error, already logged. */
if (match > 0)
goto allow;
goto deny; /* Negative match or no match. */
allow:
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"%s approved", opname);
return (DNS_R_SUCCESS);
deny:
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
"%s denied", opname);
return (DNS_R_REFUSED);
}