1999-07-24 01:17:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1999 Internet Software Consortium.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/timer.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-12-16 23:11:07 +00:00
|
|
|
#include <dns/acl.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <dns/dispatch.h>
|
|
|
|
#include <dns/events.h>
|
|
|
|
#include <dns/message.h>
|
1999-09-02 01:52:31 +00:00
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
1999-08-05 22:14:43 +00:00
|
|
|
#include <dns/view.h>
|
1999-10-29 05:08:55 +00:00
|
|
|
#include <dns/xfrin.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-10-22 19:35:19 +00:00
|
|
|
#include <named/globals.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <named/client.h>
|
1999-10-22 19:35:19 +00:00
|
|
|
#include <named/log.h>
|
1999-08-20 06:03:02 +00:00
|
|
|
#include <named/query.h>
|
1999-12-16 23:11:07 +00:00
|
|
|
#include <named/server.h>
|
1999-08-20 06:03:02 +00:00
|
|
|
#include <named/update.h>
|
1999-12-16 01:23:17 +00:00
|
|
|
#include <named/notify.h>
|
2000-01-11 21:18:22 +00:00
|
|
|
#include <named/interfacemgr.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
#define NS_CLIENT_TRACE
|
|
|
|
#ifdef NS_CLIENT_TRACE
|
1999-10-22 19:35:19 +00:00
|
|
|
#define CTRACE(m) isc_log_write(ns_g_lctx, \
|
|
|
|
NS_LOGCATEGORY_CLIENT, \
|
|
|
|
NS_LOGMODULE_CLIENT, \
|
1999-11-23 20:55:02 +00:00
|
|
|
ISC_LOG_DEBUG(3), \
|
1999-10-22 19:35:19 +00:00
|
|
|
"client %p: %s", client, (m))
|
|
|
|
#define MTRACE(m) isc_log_write(ns_g_lctx, \
|
|
|
|
NS_LOGCATEGORY_GENERAL, \
|
|
|
|
NS_LOGMODULE_CLIENT, \
|
1999-11-23 20:55:02 +00:00
|
|
|
ISC_LOG_DEBUG(3), \
|
1999-10-22 19:35:19 +00:00
|
|
|
"clientmgr %p: %s", manager, (m))
|
1999-07-24 01:17:44 +00:00
|
|
|
#endif
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
|
|
|
|
|
1999-09-01 18:36:57 +00:00
|
|
|
#define SEND_BUFFER_SIZE 2048
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
struct ns_clientmgr {
|
|
|
|
/* Unlocked. */
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
|
|
|
isc_taskmgr_t * taskmgr;
|
|
|
|
isc_timermgr_t * timermgr;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
/* Locked by lock. */
|
|
|
|
isc_boolean_t exiting;
|
|
|
|
unsigned int nclients;
|
|
|
|
ISC_LIST(ns_client_t) clients;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MANAGER_MAGIC 0x4E53436DU /* NSCm */
|
|
|
|
#define VALID_MANAGER(m) ((m) != NULL && \
|
|
|
|
(m)->magic == MANAGER_MAGIC)
|
|
|
|
|
|
|
|
|
|
|
|
static void clientmgr_destroy(ns_clientmgr_t *manager);
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Client
|
|
|
|
***/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Important note!
|
|
|
|
*
|
|
|
|
* All client state changes, other than that from idle to listening, occur
|
|
|
|
* as a result of events. This guarantees serialization and avoids the
|
|
|
|
* need for locking.
|
|
|
|
*
|
|
|
|
* If a routine is ever created that allows someone other than the client's
|
|
|
|
* task to change the client, then the client will have to be locked.
|
|
|
|
*/
|
|
|
|
|
2000-01-15 00:36:26 +00:00
|
|
|
static void
|
|
|
|
release_quotas(ns_client_t *client) {
|
|
|
|
if (client->tcpquota != NULL)
|
|
|
|
isc_quota_detach(&client->tcpquota);
|
|
|
|
if (client->recursionquota != NULL)
|
|
|
|
isc_quota_detach(&client->recursionquota);
|
|
|
|
}
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
/*
|
|
|
|
* Free a client immediately if possible, otherwise start
|
|
|
|
* shutting it down and postpone freeing to later.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
maybe_free(ns_client_t *client) {
|
|
|
|
isc_boolean_t need_clientmgr_destroy = ISC_FALSE;
|
2000-01-18 18:16:34 +00:00
|
|
|
ns_clientmgr_t *manager = NULL;
|
2000-01-18 18:07:07 +00:00
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When "shuttingdown" is true, either the task has received
|
|
|
|
* its shutdown event or no shutdown event has ever been
|
|
|
|
* set up. Thus, we have no outstanding shutdown
|
|
|
|
* event at this point.
|
|
|
|
*/
|
1999-12-22 16:59:05 +00:00
|
|
|
REQUIRE(client->shuttingdown == ISC_TRUE);
|
|
|
|
|
|
|
|
if (client->naccepts > 0)
|
2000-01-04 22:05:31 +00:00
|
|
|
isc_socket_cancel(client->tcplistener, client->task,
|
1999-12-22 16:59:05 +00:00
|
|
|
ISC_SOCKCANCEL_ACCEPT);
|
|
|
|
if (client->nreads > 0)
|
|
|
|
dns_tcpmsg_cancelread(&client->tcpmsg);
|
2000-01-15 00:36:26 +00:00
|
|
|
if (client->nsends > 0) {
|
|
|
|
isc_socket_t *socket;
|
|
|
|
if (TCP_CLIENT(client))
|
|
|
|
socket = client->tcpsocket;
|
|
|
|
else
|
|
|
|
socket = dns_dispatch_getsocket(client->dispatch);
|
1999-12-22 16:59:05 +00:00
|
|
|
isc_socket_cancel(client->tcpsocket, client->task,
|
|
|
|
ISC_SOCKCANCEL_SEND);
|
2000-01-15 00:36:26 +00:00
|
|
|
}
|
1999-12-22 16:59:05 +00:00
|
|
|
|
|
|
|
if (!(client->nreads == 0 && client->naccepts == 0 &&
|
|
|
|
client->nsends == 0 && client->nwaiting == 0)) {
|
|
|
|
/* Still waiting for events. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have received our last event. */
|
2000-01-11 21:18:22 +00:00
|
|
|
|
|
|
|
ns_interface_detach(&client->interface);
|
1999-07-29 00:55:35 +00:00
|
|
|
ns_query_free(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_mempool_destroy(&client->sendbufs);
|
|
|
|
isc_timer_detach(&client->timer);
|
1999-12-22 16:59:05 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
if (client->dispentry != NULL) {
|
1999-12-22 16:59:05 +00:00
|
|
|
dns_dispatchevent_t **deventp;
|
1999-07-24 01:17:44 +00:00
|
|
|
if (client->dispevent != NULL)
|
|
|
|
deventp = &client->dispevent;
|
|
|
|
else
|
|
|
|
deventp = NULL;
|
|
|
|
dns_dispatch_removerequest(client->dispatch,
|
|
|
|
&client->dispentry,
|
|
|
|
deventp);
|
|
|
|
}
|
2000-01-06 01:09:27 +00:00
|
|
|
if (client->view != NULL)
|
|
|
|
dns_view_detach(&client->view);
|
|
|
|
if (client->opt != NULL) {
|
|
|
|
INSIST(dns_rdataset_isassociated(client->opt));
|
|
|
|
dns_rdataset_disassociate(client->opt);
|
|
|
|
dns_message_puttemprdataset(client->message, &client->opt);
|
|
|
|
}
|
|
|
|
dns_message_destroy(&client->message);
|
1999-12-22 16:59:05 +00:00
|
|
|
if (client->tcpmsg_valid)
|
|
|
|
dns_tcpmsg_invalidate(&client->tcpmsg);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (client->dispatch != NULL)
|
|
|
|
dns_dispatch_detach(&client->dispatch);
|
1999-12-22 16:59:05 +00:00
|
|
|
if (client->tcpsocket != NULL)
|
|
|
|
isc_socket_detach(&client->tcpsocket);
|
1999-08-05 01:51:32 +00:00
|
|
|
if (client->tcplistener != NULL)
|
|
|
|
isc_socket_detach(&client->tcplistener);
|
1999-12-22 16:59:05 +00:00
|
|
|
if (client->task != NULL)
|
|
|
|
isc_task_detach(&client->task);
|
|
|
|
if (client->manager != NULL) {
|
2000-01-18 18:07:07 +00:00
|
|
|
manager = client->manager;
|
1999-12-22 16:59:05 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
INSIST(manager->nclients > 0);
|
|
|
|
manager->nclients--;
|
|
|
|
if (manager->nclients == 0 && manager->exiting)
|
|
|
|
need_clientmgr_destroy = ISC_TRUE;
|
|
|
|
ISC_LIST_UNLINK(manager->clients, client, link);
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
2000-01-11 21:18:22 +00:00
|
|
|
|
2000-01-15 00:36:26 +00:00
|
|
|
release_quotas(client);
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
CTRACE("free");
|
1999-07-24 01:17:44 +00:00
|
|
|
client->magic = 0;
|
1999-07-29 00:55:35 +00:00
|
|
|
isc_mem_put(client->mctx, client, sizeof *client);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
if (need_clientmgr_destroy)
|
2000-01-18 18:07:07 +00:00
|
|
|
clientmgr_destroy(manager);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
/*
|
|
|
|
* The client's task has received a shutdown event.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
|
|
|
client_shutdown(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(event != NULL);
|
|
|
|
REQUIRE(event->type == ISC_TASKEVENT_SHUTDOWN);
|
|
|
|
client = event->arg;
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
|
|
|
CTRACE("shutdown");
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
client->shuttingdown = ISC_TRUE;
|
2000-01-12 18:01:12 +00:00
|
|
|
|
|
|
|
if (client->shutdown != NULL)
|
|
|
|
(client->shutdown)(client->shutdown_arg);
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
maybe_free(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
static void client_read(ns_client_t *client);
|
|
|
|
static void client_accept(ns_client_t *client);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
void
|
|
|
|
ns_client_next(ns_client_t *client, isc_result_t result) {
|
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("next");
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
if (client->next != NULL) {
|
|
|
|
(client->next)(client, result);
|
|
|
|
client->next = NULL;
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/*
|
|
|
|
* XXXRTH If result != ISC_R_SUCCESS:
|
|
|
|
* Log result if there is interest in doing so.
|
|
|
|
*/
|
|
|
|
|
1999-08-05 22:14:43 +00:00
|
|
|
if (client->view != NULL)
|
|
|
|
dns_view_detach(&client->view);
|
1999-11-24 21:05:45 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
INSIST(dns_rdataset_isassociated(client->opt));
|
|
|
|
dns_rdataset_disassociate(client->opt);
|
|
|
|
dns_message_puttemprdataset(client->message, &client->opt);
|
|
|
|
}
|
2000-01-07 19:20:25 +00:00
|
|
|
|
1999-10-07 19:43:18 +00:00
|
|
|
client->udpsize = 512;
|
1999-07-24 01:17:44 +00:00
|
|
|
dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
|
2000-01-15 00:36:26 +00:00
|
|
|
|
|
|
|
release_quotas(client);
|
|
|
|
|
2000-01-11 21:18:22 +00:00
|
|
|
if (client->mortal) {
|
|
|
|
/*
|
|
|
|
* This client object is supposed to die now, but if we
|
|
|
|
* have fewer client objects than planned due to
|
|
|
|
* quota exhaustion, don't.
|
|
|
|
*/
|
|
|
|
isc_boolean_t need_another_client = ISC_FALSE;
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
if (client->interface->ntcpcurrent <
|
|
|
|
client->interface->ntcptarget)
|
|
|
|
need_another_client = ISC_TRUE;
|
|
|
|
UNLOCK(&client->interface->lock);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The UDP client quota is enforced by making
|
|
|
|
* requests fail rather than by not listening
|
|
|
|
* for new ones. Therefore, there is always a
|
|
|
|
* full set of UDP clients listening.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
if (! need_another_client) {
|
|
|
|
/* XXX should put in "idle client pool" instead. */
|
|
|
|
isc_task_shutdown(client->task);
|
|
|
|
return;
|
|
|
|
}
|
2000-01-15 00:36:26 +00:00
|
|
|
client->mortal = ISC_FALSE;
|
2000-01-07 19:20:25 +00:00
|
|
|
}
|
2000-01-15 00:36:26 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
if (client->dispevent != NULL) {
|
2000-01-11 21:18:22 +00:00
|
|
|
/*
|
|
|
|
* Give the processed dispatch event back to the dispatch.
|
|
|
|
* This tells the dispatch that we are ready to receive
|
|
|
|
* the next event.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
dns_dispatch_freeevent(client->dispatch, client->dispentry,
|
|
|
|
&client->dispevent);
|
1999-08-05 01:51:32 +00:00
|
|
|
} else if (TCP_CLIENT(client)) {
|
2000-01-11 21:18:22 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
1999-08-05 01:51:32 +00:00
|
|
|
client_read(client);
|
2000-01-11 21:18:22 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* There was an error processing a TCP request.
|
|
|
|
* It may have have left the connection out of
|
|
|
|
* sync. Close the connection and listen for a
|
|
|
|
* new one.
|
|
|
|
*/
|
1999-08-05 01:51:32 +00:00
|
|
|
if (client->tcpsocket != NULL) {
|
1999-12-22 16:59:05 +00:00
|
|
|
/*
|
2000-01-06 18:25:08 +00:00
|
|
|
* There should be no outstanding read
|
|
|
|
* request on the TCP socket at this point,
|
|
|
|
* therefore invalidating the tcpmsg is safe.
|
1999-12-22 16:59:05 +00:00
|
|
|
*/
|
2000-01-06 18:25:08 +00:00
|
|
|
INSIST(client->nreads == 0);
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->tcpmsg_valid == ISC_TRUE);
|
1999-08-05 01:51:32 +00:00
|
|
|
dns_tcpmsg_invalidate(&client->tcpmsg);
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_FALSE;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_socket_detach(&client->tcpsocket);
|
|
|
|
}
|
|
|
|
client_accept(client);
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_senddone(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
|
|
|
isc_socketevent_t *sevent = (isc_socketevent_t *)event;
|
|
|
|
|
|
|
|
REQUIRE(sevent != NULL);
|
1999-07-28 03:06:24 +00:00
|
|
|
REQUIRE(sevent->type == ISC_SOCKEVENT_SENDDONE);
|
|
|
|
client = sevent->arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
|
|
|
CTRACE("senddone");
|
|
|
|
|
1999-07-24 03:02:57 +00:00
|
|
|
INSIST(client->nsends > 0);
|
1999-07-24 01:17:44 +00:00
|
|
|
client->nsends--;
|
|
|
|
isc_mempool_put(client->sendbufs, sevent->region.base);
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If all of its sendbufs buffers were busy, the client might be
|
|
|
|
* waiting for one to become available.
|
|
|
|
*/
|
1999-12-22 16:59:05 +00:00
|
|
|
if (client->waiting_for_bufs == ISC_TRUE) {
|
|
|
|
client->waiting_for_bufs = ISC_FALSE;
|
1999-07-28 02:20:36 +00:00
|
|
|
ns_client_send(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* XXXRTH need to add exit draining mode. */
|
|
|
|
}
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
void
|
|
|
|
ns_client_send(ns_client_t *client) {
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t result;
|
|
|
|
unsigned char *data;
|
|
|
|
isc_buffer_t buffer;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_t tcpbuffer;
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_region_t r;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_socket_t *socket;
|
|
|
|
isc_sockaddr_t *address;
|
1999-09-02 01:52:31 +00:00
|
|
|
unsigned int bufsize = 512;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("send");
|
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_RA) != 0)
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_RA;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
data = isc_mempool_get(client->sendbufs);
|
|
|
|
if (data == NULL) {
|
|
|
|
CTRACE("no buffers available");
|
|
|
|
if (client->nsends > 0) {
|
|
|
|
/*
|
|
|
|
* We couldn't get memory, but there is at least one
|
|
|
|
* send outstanding. We arrange to be restarted when a
|
|
|
|
* send completes.
|
|
|
|
*/
|
|
|
|
CTRACE("waiting");
|
1999-12-22 16:59:05 +00:00
|
|
|
client->waiting_for_bufs = ISC_TRUE;
|
1999-07-24 01:17:44 +00:00
|
|
|
} else
|
|
|
|
ns_client_next(client, ISC_R_NOMEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
/*
|
1999-09-01 18:36:57 +00:00
|
|
|
* XXXRTH The following doesn't deal with TSIGs, TCP buffer resizing,
|
1999-09-02 01:52:31 +00:00
|
|
|
* or ENDS1 more data packets.
|
1999-07-28 02:20:36 +00:00
|
|
|
*/
|
1999-08-05 01:51:32 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
/*
|
|
|
|
* XXXRTH "tcpbuffer" is a hack to get things working.
|
|
|
|
*/
|
|
|
|
isc_buffer_init(&tcpbuffer, data, SEND_BUFFER_SIZE,
|
|
|
|
ISC_BUFFERTYPE_BINARY);
|
|
|
|
isc_buffer_init(&buffer, data + 2, SEND_BUFFER_SIZE - 2,
|
|
|
|
ISC_BUFFERTYPE_BINARY);
|
|
|
|
} else {
|
1999-10-07 19:43:18 +00:00
|
|
|
if (client->udpsize < SEND_BUFFER_SIZE)
|
|
|
|
bufsize = client->udpsize;
|
|
|
|
else
|
|
|
|
bufsize = SEND_BUFFER_SIZE;
|
1999-09-02 01:52:31 +00:00
|
|
|
isc_buffer_init(&buffer, data, bufsize, ISC_BUFFERTYPE_BINARY);
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_renderbegin(client->message, &buffer);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-09-02 01:52:31 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
result = dns_message_setopt(client->message, client->opt);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-11-24 21:05:45 +00:00
|
|
|
/*
|
|
|
|
* XXXRTH dns_message_setopt() should probably do this...
|
|
|
|
*/
|
|
|
|
client->opt = NULL;
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_QUESTION, 0);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-07-28 02:20:36 +00:00
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_ANSWER, 0);
|
1999-09-01 18:25:05 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_AUTHORITY, 0);
|
1999-09-01 18:25:05 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_ADDITIONAL, 0);
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
|
|
|
|
goto done;
|
1999-09-01 18:25:05 +00:00
|
|
|
renderend:
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_renderend(client->message);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-08-05 01:51:32 +00:00
|
|
|
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
socket = client->tcpsocket;
|
|
|
|
address = NULL;
|
|
|
|
isc_buffer_used(&buffer, &r);
|
|
|
|
isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
|
|
|
|
isc_buffer_add(&tcpbuffer, r.length);
|
|
|
|
isc_buffer_used(&tcpbuffer, &r);
|
|
|
|
} else {
|
|
|
|
socket = dns_dispatch_getsocket(client->dispatch);
|
|
|
|
address = &client->dispevent->addr;
|
|
|
|
isc_buffer_used(&buffer, &r);
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("sendto");
|
1999-08-05 01:51:32 +00:00
|
|
|
result = isc_socket_sendto(socket, &r, client->task, client_senddone,
|
1999-12-04 01:27:44 +00:00
|
|
|
client, address, NULL);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
client->nsends++;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
isc_mempool_put(client->sendbufs, data);
|
|
|
|
|
|
|
|
ns_client_next(client, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_error(ns_client_t *client, isc_result_t result) {
|
|
|
|
dns_rcode_t rcode;
|
1999-08-03 01:21:00 +00:00
|
|
|
dns_message_t *message;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("error");
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
message = client->message;
|
1999-07-24 01:17:44 +00:00
|
|
|
rcode = dns_result_torcode(result);
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
/*
|
1999-08-03 01:21:00 +00:00
|
|
|
* message may be an in-progress reply that we had trouble
|
1999-07-28 02:20:36 +00:00
|
|
|
* with, in which case QR will be set. We need to clear QR before
|
|
|
|
* calling dns_message_reply() to avoid triggering an assertion.
|
|
|
|
*/
|
1999-08-03 01:21:00 +00:00
|
|
|
message->flags &= ~DNS_MESSAGEFLAG_QR;
|
1999-10-07 19:43:18 +00:00
|
|
|
/*
|
|
|
|
* AA and AD shouldn't be set.
|
|
|
|
*/
|
|
|
|
message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
|
1999-08-03 01:21:00 +00:00
|
|
|
result = dns_message_reply(message, ISC_TRUE);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1999-08-03 01:21:00 +00:00
|
|
|
/*
|
|
|
|
* It could be that we've got a query with a good header,
|
|
|
|
* but a bad question section, so we try again with
|
|
|
|
* want_question_section set to ISC_FALSE.
|
|
|
|
*/
|
|
|
|
result = dns_message_reply(message, ISC_FALSE);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* There's no hope of replying to this request.
|
|
|
|
*
|
|
|
|
* XXXRTH Mark this client to that if it is a
|
|
|
|
* TCP session, the session will be closed.
|
|
|
|
*/
|
|
|
|
ns_client_next(client, result);
|
|
|
|
return;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-08-03 01:21:00 +00:00
|
|
|
message->rcode = rcode;
|
1999-07-28 02:20:36 +00:00
|
|
|
ns_client_send(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
static inline isc_result_t
|
|
|
|
client_addopt(ns_client_t *client) {
|
|
|
|
dns_rdataset_t *rdataset;
|
|
|
|
dns_rdatalist_t *rdatalist;
|
|
|
|
dns_rdata_t *rdata;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(client->opt == NULL); /* XXXRTH free old. */
|
|
|
|
|
|
|
|
rdatalist = NULL;
|
|
|
|
result = dns_message_gettemprdatalist(client->message, &rdatalist);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
rdata = NULL;
|
|
|
|
result = dns_message_gettemprdata(client->message, &rdata);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
1999-10-20 23:20:30 +00:00
|
|
|
rdataset = NULL;
|
|
|
|
result = dns_message_gettemprdataset(client->message, &rdataset);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
dns_rdataset_init(rdataset);
|
1999-09-02 01:52:31 +00:00
|
|
|
|
|
|
|
rdatalist->type = dns_rdatatype_opt;
|
|
|
|
rdatalist->covers = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set Maximum UDP buffer size.
|
|
|
|
*/
|
|
|
|
rdatalist->rdclass = SEND_BUFFER_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set EXTENDED-RCODE, VERSION, and Z to 0.
|
|
|
|
*/
|
|
|
|
rdatalist->ttl = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No ENDS options.
|
|
|
|
*/
|
|
|
|
rdata->data = NULL;
|
|
|
|
rdata->length = 0;
|
|
|
|
|
|
|
|
ISC_LIST_INIT(rdatalist->rdata);
|
|
|
|
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
|
|
|
|
dns_rdatalist_tordataset(rdatalist, rdataset);
|
|
|
|
|
|
|
|
client->opt = rdataset;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-01-06 01:09:27 +00:00
|
|
|
/*
|
|
|
|
* Handle an incoming request event from the dispatch (UDP case)
|
|
|
|
* or tcpmsg (TCP case).
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
1999-08-05 01:51:32 +00:00
|
|
|
client_request(isc_task_t *task, isc_event_t *event) {
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_client_t *client;
|
1999-08-05 01:51:32 +00:00
|
|
|
dns_dispatchevent_t *devent;
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t result;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_t *buffer;
|
1999-08-05 22:14:43 +00:00
|
|
|
dns_view_t *view;
|
1999-09-02 01:52:31 +00:00
|
|
|
dns_rdataset_t *opt;
|
1999-12-10 23:58:04 +00:00
|
|
|
isc_boolean_t ra; /* Recursion available. */
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
REQUIRE(event != NULL);
|
|
|
|
client = event->arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
2000-01-15 00:36:26 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
if (event->type == DNS_EVENT_DISPATCH) {
|
|
|
|
devent = (dns_dispatchevent_t *)event;
|
|
|
|
REQUIRE(client->dispentry != NULL);
|
|
|
|
client->dispevent = devent;
|
|
|
|
buffer = &devent->buffer;
|
|
|
|
result = devent->result;
|
|
|
|
} else {
|
|
|
|
REQUIRE(event->type == DNS_EVENT_TCPMSG);
|
|
|
|
REQUIRE(event->sender == &client->tcpmsg);
|
|
|
|
buffer = &client->tcpmsg.buffer;
|
|
|
|
result = client->tcpmsg.result;
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->nreads == 1);
|
|
|
|
client->nreads--;
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CTRACE("request");
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
if (client->shuttingdown) {
|
|
|
|
maybe_free(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-16 23:29:07 +00:00
|
|
|
isc_stdtime_get(&client->requesttime);
|
1999-10-29 16:16:54 +00:00
|
|
|
client->now = client->requesttime;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
if (TCP_CLIENT(client))
|
|
|
|
ns_client_next(client, result);
|
|
|
|
else
|
|
|
|
isc_task_shutdown(client->task);
|
1999-07-24 01:17:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-08-20 06:03:02 +00:00
|
|
|
result = dns_message_parse(client->message, buffer, ISC_FALSE);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_error(client, result);
|
|
|
|
return;
|
|
|
|
}
|
1999-12-20 23:06:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
return;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
/*
|
|
|
|
* Deal with EDNS.
|
|
|
|
*/
|
|
|
|
opt = dns_message_getopt(client->message);
|
|
|
|
if (opt != NULL) {
|
|
|
|
unsigned int version;
|
|
|
|
|
1999-10-07 19:43:18 +00:00
|
|
|
/*
|
|
|
|
* Set the client's UDP buffer size.
|
|
|
|
*/
|
|
|
|
client->udpsize = opt->rdclass;
|
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
/*
|
|
|
|
* Create an OPT for our reply.
|
|
|
|
*/
|
|
|
|
result = client_addopt(client);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_error(client, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-10 18:14:49 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
client->signer = 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 {
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
/*
|
1999-08-12 07:52:32 +00:00
|
|
|
* XXXRTH View list management code will be moving to its own module
|
|
|
|
* soon.
|
1999-08-03 01:21:00 +00:00
|
|
|
*/
|
1999-12-22 18:45:56 +00:00
|
|
|
RWLOCK(&ns_g_server->viewlock, isc_rwlocktype_read);
|
|
|
|
for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
|
1999-08-05 22:14:43 +00:00
|
|
|
view != NULL;
|
|
|
|
view = ISC_LIST_NEXT(view, link)) {
|
|
|
|
/*
|
|
|
|
* XXXRTH View matching will become more powerful later.
|
|
|
|
*/
|
|
|
|
if (client->message->rdclass == view->rdclass) {
|
|
|
|
dns_view_attach(view, &client->view);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-12-22 18:45:56 +00:00
|
|
|
RWUNLOCK(&ns_g_server->viewlock, isc_rwlocktype_read);
|
1999-08-05 22:14:43 +00:00
|
|
|
|
|
|
|
if (view == NULL) {
|
|
|
|
CTRACE("no view");
|
|
|
|
ns_client_error(client, DNS_R_REFUSED);
|
|
|
|
return;
|
|
|
|
}
|
1999-12-10 23:58:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
1999-12-16 23:11:07 +00:00
|
|
|
if (ns_g_server->recursion == ISC_TRUE) {
|
1999-12-10 23:58:04 +00:00
|
|
|
/* XXX ACL should be view specific. */
|
|
|
|
/* XXX this will log too much too early */
|
1999-12-16 23:11:07 +00:00
|
|
|
result = dns_acl_checkrequest(client->signer,
|
1999-12-10 23:58:04 +00:00
|
|
|
ns_client_getsockaddr(client),
|
1999-12-16 23:11:07 +00:00
|
|
|
"recursion",
|
|
|
|
ns_g_server->recursionacl,
|
|
|
|
NULL, ISC_TRUE);
|
1999-12-10 23:58:04 +00:00
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
ra = ISC_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ra == ISC_TRUE)
|
|
|
|
client->attributes |= NS_CLIENTATTR_RA;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/*
|
|
|
|
* Dispatch the request.
|
|
|
|
*/
|
|
|
|
switch (client->message->opcode) {
|
|
|
|
case dns_opcode_query:
|
|
|
|
CTRACE("query");
|
|
|
|
ns_query_start(client);
|
|
|
|
break;
|
1999-08-20 06:03:02 +00:00
|
|
|
case dns_opcode_update:
|
|
|
|
CTRACE("update");
|
|
|
|
ns_update_start(client);
|
|
|
|
break;
|
|
|
|
case dns_opcode_notify:
|
|
|
|
CTRACE("notify");
|
1999-12-16 01:23:17 +00:00
|
|
|
ns_notify_start(client);
|
1999-08-20 06:03:02 +00:00
|
|
|
break;
|
1999-07-24 01:17:44 +00:00
|
|
|
case dns_opcode_iquery:
|
|
|
|
CTRACE("iquery");
|
1999-11-30 22:35:43 +00:00
|
|
|
ns_client_error(client, DNS_R_NOTIMP);
|
1999-07-24 01:17:44 +00:00
|
|
|
default:
|
|
|
|
CTRACE("unknown opcode");
|
|
|
|
ns_client_error(client, DNS_R_NOTIMP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
ns_client_next(client, ISC_R_TIMEDOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2000-01-15 00:36:26 +00:00
|
|
|
client_create(ns_clientmgr_t *manager,
|
2000-01-11 21:18:22 +00:00
|
|
|
ns_interface_t *ifp, ns_client_t **clientp)
|
1999-07-24 01:17:44 +00:00
|
|
|
{
|
|
|
|
ns_client_t *client;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Caller must be holding the manager lock.
|
|
|
|
*
|
1999-12-22 16:59:05 +00:00
|
|
|
* 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.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
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->sendbufs = NULL;
|
|
|
|
result = isc_mempool_create(manager->mctx, SEND_BUFFER_SIZE,
|
|
|
|
&client->sendbufs);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_message;
|
|
|
|
isc_mempool_setfreemax(client->sendbufs, 3);
|
|
|
|
isc_mempool_setmaxalloc(client->sendbufs, 3);
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
client->magic = NS_CLIENT_MAGIC;
|
1999-07-29 00:55:35 +00:00
|
|
|
client->mctx = manager->mctx;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->manager = NULL;
|
|
|
|
client->shuttingdown = ISC_FALSE;
|
|
|
|
client->waiting_for_bufs = ISC_FALSE;
|
|
|
|
client->naccepts = 0;
|
|
|
|
client->nreads = 0;
|
|
|
|
client->nsends = 0;
|
|
|
|
client->nwaiting = 0;
|
1999-07-24 01:17:44 +00:00
|
|
|
client->attributes = 0;
|
1999-08-05 22:14:43 +00:00
|
|
|
client->view = NULL;
|
1999-07-24 01:17:44 +00:00
|
|
|
client->dispatch = NULL;
|
|
|
|
client->dispentry = NULL;
|
|
|
|
client->dispevent = NULL;
|
1999-08-05 01:51:32 +00:00
|
|
|
client->tcplistener = NULL;
|
|
|
|
client->tcpsocket = NULL;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_FALSE;
|
1999-09-02 01:52:31 +00:00
|
|
|
client->opt = NULL;
|
1999-10-07 19:43:18 +00:00
|
|
|
client->udpsize = 512;
|
1999-08-03 01:21:00 +00:00
|
|
|
client->next = NULL;
|
2000-01-12 18:01:12 +00:00
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
1999-12-10 18:14:49 +00:00
|
|
|
dns_name_init(&client->signername, NULL);
|
2000-01-11 21:18:22 +00:00
|
|
|
client->mortal = ISC_FALSE;
|
2000-01-15 00:36:26 +00:00
|
|
|
client->tcpquota = NULL;
|
|
|
|
client->recursionquota = NULL;
|
2000-01-11 21:18:22 +00:00
|
|
|
client->interface = NULL;
|
1999-07-24 01:17:44 +00:00
|
|
|
ISC_LINK_INIT(client, link);
|
1999-07-28 02:20:36 +00:00
|
|
|
|
1999-07-29 00:55:35 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_sendbufs;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-01-11 21:18:22 +00:00
|
|
|
ns_interface_attach(ifp, &client->interface);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("create");
|
|
|
|
|
|
|
|
*clientp = client;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
cleanup_sendbufs:
|
|
|
|
isc_mempool_destroy(&client->sendbufs);
|
|
|
|
|
1999-07-29 00:55:35 +00:00
|
|
|
client->magic = 0;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
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)
|
|
|
|
ns_client_next(client, result);
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->nreads == 0);
|
|
|
|
client->nreads++;
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_t result;
|
1999-08-05 01:51:32 +00:00
|
|
|
|
|
|
|
REQUIRE(event->type == ISC_SOCKEVENT_NEWCONN);
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(client->task == task);
|
|
|
|
|
|
|
|
CTRACE("newconn");
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->naccepts == 1);
|
|
|
|
client->naccepts--;
|
2000-01-11 21:18:22 +00:00
|
|
|
|
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
INSIST(client->interface->ntcpcurrent > 0);
|
|
|
|
client->interface->ntcpcurrent--;
|
|
|
|
UNLOCK(&client->interface->lock);
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
if (client->shuttingdown) {
|
|
|
|
maybe_free(client);
|
|
|
|
} else if (nevent->result == ISC_R_SUCCESS) {
|
1999-08-05 01:51:32 +00:00
|
|
|
client->tcpsocket = nevent->newsocket;
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->tcpmsg_valid == ISC_FALSE);
|
1999-08-05 01:51:32 +00:00
|
|
|
dns_tcpmsg_init(client->mctx, client->tcpsocket,
|
|
|
|
&client->tcpmsg);
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_TRUE;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-01-15 00:36:26 +00:00
|
|
|
result = isc_quota_attach(&ns_g_server->tcpquota,
|
|
|
|
&client->tcpquota);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
result = ns_client_replace(client);
|
2000-01-11 21:18:22 +00:00
|
|
|
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));
|
|
|
|
}
|
1999-08-05 01:51:32 +00:00
|
|
|
client_read(client);
|
|
|
|
} 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_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;
|
|
|
|
}
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
client->naccepts++;
|
2000-01-11 21:18:22 +00:00
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
client->interface->ntcpcurrent++;
|
|
|
|
UNLOCK(&client->interface->lock);
|
1999-12-22 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_wait(ns_client_t *client) {
|
|
|
|
client->nwaiting++;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
2000-01-06 01:09:27 +00:00
|
|
|
ns_client_shuttingdown(ns_client_t *client) {
|
|
|
|
return (client->shuttingdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-12-22 16:59:05 +00:00
|
|
|
ns_client_unwait(ns_client_t *client) {
|
|
|
|
client->nwaiting--;
|
|
|
|
INSIST(client->nwaiting >= 0);
|
2000-01-06 01:09:27 +00:00
|
|
|
if (client->shuttingdown)
|
1999-12-22 16:59:05 +00:00
|
|
|
maybe_free(client);
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_t
|
2000-01-15 00:36:26 +00:00
|
|
|
ns_client_replace(ns_client_t *client) {
|
2000-01-07 19:20:25 +00:00
|
|
|
isc_result_t result;
|
|
|
|
CTRACE("replace");
|
|
|
|
|
2000-01-17 23:48:15 +00:00
|
|
|
result = ns_clientmgr_createclients(client->manager,
|
|
|
|
1, client->interface,
|
|
|
|
(TCP_CLIENT(client) ?
|
|
|
|
ISC_TRUE : ISC_FALSE));
|
2000-01-07 19:20:25 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
/*
|
2000-01-17 23:48:15 +00:00
|
|
|
* 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.
|
2000-01-07 19:20:25 +00:00
|
|
|
*/
|
2000-01-11 21:18:22 +00:00
|
|
|
client->mortal = ISC_TRUE;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/***
|
|
|
|
*** Client Manager
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
clientmgr_destroy(ns_clientmgr_t *manager) {
|
|
|
|
REQUIRE(manager->nclients == 0);
|
|
|
|
REQUIRE(ISC_LIST_EMPTY(manager->clients));
|
|
|
|
|
|
|
|
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;
|
|
|
|
manager->nclients = 0;
|
|
|
|
ISC_LIST_INIT(manager->clients);
|
|
|
|
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->clients);
|
|
|
|
client != NULL;
|
|
|
|
client = ISC_LIST_NEXT(client, link))
|
|
|
|
isc_task_shutdown(client->task);
|
|
|
|
|
|
|
|
if (ISC_LIST_EMPTY(manager->clients))
|
|
|
|
need_destroy = ISC_TRUE;
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
if (need_destroy)
|
|
|
|
clientmgr_destroy(manager);
|
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2000-01-17 23:48:15 +00:00
|
|
|
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
|
|
|
|
ns_interface_t *ifp, isc_boolean_t tcp)
|
1999-07-24 01:17:44 +00:00
|
|
|
{
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
unsigned int i;
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(n > 0);
|
|
|
|
|
2000-01-17 23:48:15 +00:00
|
|
|
MTRACE("createclients");
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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++) {
|
|
|
|
client = NULL;
|
2000-01-15 00:36:26 +00:00
|
|
|
result = client_create(manager, ifp, &client);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
break;
|
2000-01-17 23:48:15 +00:00
|
|
|
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;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-12-22 16:59:05 +00:00
|
|
|
client->manager = manager;
|
1999-08-05 01:51:32 +00:00
|
|
|
ISC_LIST_APPEND(manager->clients, client, link);
|
1999-12-22 16:59:05 +00:00
|
|
|
manager->nclients++;
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
if (i != 0) {
|
|
|
|
/*
|
|
|
|
* We managed to create at least one client, so we
|
|
|
|
* declare victory.
|
|
|
|
*/
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-11-30 02:49:38 +00:00
|
|
|
isc_sockaddr_t *
|
|
|
|
ns_client_getsockaddr(ns_client_t *client) {
|
|
|
|
if (TCP_CLIENT(client))
|
|
|
|
return (&client->tcpmsg.address);
|
|
|
|
else
|
|
|
|
return (&client->dispevent->addr);
|
|
|
|
}
|