client.c revision 50f93634353f94a3bf54d10c42fe0eb1c7f35a68
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff/*
ddad35552931651426ad70912d29d9ab7d2a8d80Automatic Updater * Copyright (C) 1999 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * copyright notice and this permission notice appear in all copies.
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff */
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff
2a03c0af765860308edb715be012d4bc743c47f1Tatuya JINMEI 神明達哉#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/assertions.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <isc/mem.h>
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff#include <isc/mutex.h>
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff#include <isc/result.h>
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff#include <isc/task.h>
07072c9456f1112705db701bb35f12dbdcb217b0Mark Andrews#include <isc/timer.h>
07072c9456f1112705db701bb35f12dbdcb217b0Mark Andrews
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#include <dns/dispatch.h>
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff#include <dns/events.h>
80617c8faa03b2c425e320597eb768073fd0b987Michael Graff#include <dns/message.h>
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff#include <dns/rdata.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <dns/rdatalist.h>
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#include <dns/rdataset.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/view.h>
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#include <dns/xfrin.h>
3d181bc9f12154a56bfbb536198a6c481cbcd525David Lawrence
32eddfc189108fa93e31761e13150594c7a79d2bDavid Lawrence#include <named/globals.h>
a095cd0ef599eb04d74fb9f9bc46ae13081c5bcbMark Andrews#include <named/client.h>
440be4c866f6935ac069db79a414304507a664c2Michael Graff#include <named/log.h>
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff#include <named/query.h>
e21d199dca95aff5d50f133d6b064309e209af00Brian Wellington#include <named/update.h>
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff#include "../../isc/util.h" /* XXX */
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff#define NS_CLIENT_TRACE
b312748a11d27fe387984973ba79975a9d6863c4Mark Andrews#ifdef NS_CLIENT_TRACE
8907d8fa04fdaa65baf0bc6b01230b2ebde93106Mark Andrews#define CTRACE(m) isc_log_write(ns_g_lctx, \
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff NS_LOGCATEGORY_CLIENT, \
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff NS_LOGMODULE_CLIENT, \
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff ISC_LOG_DEBUG(3), \
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews "client %p: %s", client, (m))
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews#define MTRACE(m) isc_log_write(ns_g_lctx, \
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 NS_LOGCATEGORY_GENERAL, \
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 NS_LOGMODULE_CLIENT, \
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LOG_DEBUG(3), \
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "clientmgr %p: %s", manager, (m))
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#endif
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#define SEND_BUFFER_SIZE 2048
07072c9456f1112705db701bb35f12dbdcb217b0Mark Andrews
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrewsstruct ns_clientmgr {
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews /* Unlocked. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int magic;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mem_t * mctx;
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews isc_taskmgr_t * taskmgr;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_timermgr_t * timermgr;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mutex_t lock;
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews /* Locked by lock. */
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews isc_boolean_t exiting;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff unsigned int nclients;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff ISC_LIST(ns_client_t) clients;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff};
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
e21d199dca95aff5d50f133d6b064309e209af00Brian Wellington#define MANAGER_MAGIC 0x4E53436DU /* NSCm */
b312748a11d27fe387984973ba79975a9d6863c4Mark Andrews#define VALID_MANAGER(m) ((m) != NULL && \
8907d8fa04fdaa65baf0bc6b01230b2ebde93106Mark Andrews (m)->magic == MANAGER_MAGIC)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graffstatic void clientmgr_destroy(ns_clientmgr_t *manager);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff/***
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews *** Client
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ***/
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉/*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Important note!
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews *
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * All client state changes, other than that from idle to listening, occur
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews * as a result of events. This guarantees serialization and avoids the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * need for locking.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * If a routine is ever created that allows someone other than the client's
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * task to change the client, then the client will have to be locked.
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff */
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic inline void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinclient_free(ns_client_t *client) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_dispatchevent_t **deventp;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("free");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_query_free(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mempool_destroy(&client->sendbufs);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_message_destroy(&client->message);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_timer_detach(&client->timer);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->dispentry != NULL) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->dispevent != NULL)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 deventp = &client->dispevent;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 else
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 deventp = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_dispatch_removerequest(client->dispatch,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 &client->dispentry,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 deventp);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->dispatch != NULL)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_dispatch_detach(&client->dispatch);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->tcplistener != NULL)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_socket_detach(&client->tcplistener);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->tcpsocket != NULL) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->state == ns_clientstate_reading)
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff dns_tcpmsg_cancelread(&client->tcpmsg);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff dns_tcpmsg_invalidate(&client->tcpmsg);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff isc_socket_detach(&client->tcpsocket);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff }
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff isc_task_detach(&client->task);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff client->magic = 0;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mem_put(client->mctx, client, sizeof *client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graffstatic void
46993e1d9d18410a5852b7d990338b70b158855cMichael Graffclient_destroy(ns_client_t *client) {
038a3e6bfe6e15479b0469bd87de3a7139ad1109Brian Wellington ns_clientmgr_t *manager;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_boolean_t need_clientmgr_destroy = ISC_FALSE;
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(NS_CLIENT_VALID(client));
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff CTRACE("destroy");
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff manager = client->manager;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 LOCK(&manager->lock);
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff INSIST(manager->nclients > 0);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 manager->nclients--;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (manager->nclients == 0 && manager->exiting)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 need_clientmgr_destroy = ISC_TRUE;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LIST_UNLINK(manager->clients, client, link);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 UNLOCK(&manager->lock);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client_free(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (need_clientmgr_destroy)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 clientmgr_destroy(manager);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉client_shutdown(isc_task_t *task, isc_event_t *event) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_t *client;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(event != NULL);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(event->type == ISC_TASKEVENT_SHUTDOWN);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client = event->arg;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(NS_CLIENT_VALID(client));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(task == client->task);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("shutdown");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client_destroy(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_event_free(&event);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void client_read(ns_client_t *client);
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graffstatic void client_accept(ns_client_t *client);
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff
607dc8013a8cb34cd03b59292aa1ac01e008c9c1Michael Graffvoid
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graffns_client_next(ns_client_t *client, isc_result_t result) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(NS_CLIENT_VALID(client));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(client->state == ns_clientstate_listening ||
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state == ns_clientstate_working);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("next");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff if (client->next != NULL) {
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff (client->next)(client, result);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein client->next = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * XXXRTH If result != ISC_R_SUCCESS:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Log result if there is interest in doing so.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->view != NULL)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_view_detach(&client->view);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (client->opt != NULL) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 INSIST(dns_rdataset_isassociated(client->opt));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_rdataset_disassociate(client->opt);
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington dns_message_puttemprdataset(client->message, &client->opt);
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein client->udpsize = 512;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff if (client->dispevent != NULL) {
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff dns_dispatch_freeevent(client->dispatch, client->dispentry,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein &client->dispevent);
ce5bd92088923d1c76e26d313d1d8c0b8bbe38feMichael Graff client->state = ns_clientstate_listening;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff } else if (TCP_CLIENT(client)) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (result == ISC_R_SUCCESS)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein client_read(client);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff else {
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (client->tcpsocket != NULL) {
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff dns_tcpmsg_invalidate(&client->tcpmsg);
f98d6edb191348477c9c5a156003df627d9bc42cBrian Wellington isc_socket_detach(&client->tcpsocket);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
6f1422b81ed2c5142092e2ced8e3faf0e61f3ba0Michael Graff client_accept(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinclient_senddone(isc_task_t *task, isc_event_t *event) {
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews ns_client_t *client;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff REQUIRE(sevent != NULL);
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews REQUIRE(sevent->type == ISC_SOCKEVENT_SENDDONE);
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews client = sevent->arg;
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews REQUIRE(NS_CLIENT_VALID(client));
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff REQUIRE(task == client->task);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff CTRACE("senddone");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 INSIST(client->nsends > 0);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->nsends--;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff isc_mempool_put(client->sendbufs, sevent->region.base);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff isc_event_free(&event);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /*
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff * If all of its sendbufs buffers were busy, the client might be
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * waiting for one to become available.
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (client->state == ns_clientstate_waiting) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state = ns_clientstate_working;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_send(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /* XXXRTH need to add exit draining mode. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉ns_client_send(ns_client_t *client) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 unsigned char *data;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_buffer_t buffer;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff isc_buffer_t tcpbuffer;
3d181bc9f12154a56bfbb536198a6c481cbcd525David Lawrence isc_region_t r;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff isc_socket_t *socket;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_sockaddr_t *address;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 unsigned int bufsize = 512;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff REQUIRE(NS_CLIENT_VALID(client));
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("send");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 data = isc_mempool_get(client->sendbufs);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (data == NULL) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("no buffers available");
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff if (client->nsends > 0) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * We couldn't get memory, but there is at least one
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * send outstanding. We arrange to be restarted when a
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff * send completes.
8470db5b12c18cfb32a757e265ce4e7789052c92Michael Graff */
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff CTRACE("waiting");
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff INSIST(client->state == ns_clientstate_working);
d8c2b1787119de43dd6b7e3927ff193ed5df682fMark Andrews client->state = ns_clientstate_waiting;
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews } else
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews ns_client_next(client, ISC_R_NOMEMORY);
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff return;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * XXXRTH The following doesn't deal with TSIGs, TCP buffer resizing,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * or ENDS1 more data packets.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (TCP_CLIENT(client)) {
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff /*
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * XXXRTH "tcpbuffer" is a hack to get things working.
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff */
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff isc_buffer_init(&tcpbuffer, data, SEND_BUFFER_SIZE,
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff ISC_BUFFERTYPE_BINARY);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff isc_buffer_init(&buffer, data + 2, SEND_BUFFER_SIZE - 2,
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff ISC_BUFFERTYPE_BINARY);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff } else {
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (client->udpsize < SEND_BUFFER_SIZE)
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews bufsize = client->udpsize;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 else
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 bufsize = SEND_BUFFER_SIZE;
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews isc_buffer_init(&buffer, data, bufsize, ISC_BUFFERTYPE_BINARY);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_renderbegin(client->message, &buffer);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff goto done;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (client->opt != NULL) {
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff result = dns_message_setopt(client->message, client->opt);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson if (result != ISC_R_SUCCESS)
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson goto done;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson /*
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson * XXXRTH dns_message_setopt() should probably do this...
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff */
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff client->opt = NULL;
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff }
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff result = dns_message_rendersection(client->message,
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff DNS_SECTION_QUESTION, 0, 0);
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington if (result != ISC_R_SUCCESS)
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington goto done;
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington result = dns_message_rendersection(client->message,
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff DNS_SECTION_ANSWER, 0, 0);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff if (result == ISC_R_NOSPACE) {
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff client->message->flags |= DNS_MESSAGEFLAG_TC;
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff goto renderend;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff }
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (result != ISC_R_SUCCESS)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff goto done;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff result = dns_message_rendersection(client->message,
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff DNS_SECTION_AUTHORITY, 0, 0);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson if (result == ISC_R_NOSPACE) {
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson client->message->flags |= DNS_MESSAGEFLAG_TC;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson goto renderend;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson }
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (result != ISC_R_SUCCESS)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff goto done;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff result = dns_message_rendersection(client->message,
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff DNS_SECTION_ADDITIONAL, 0, 0);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
9317787889b6d2437b2f8e77583b49246f7511feAndreas Gustafsson goto done;
9317787889b6d2437b2f8e77583b49246f7511feAndreas Gustafsson renderend:
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence result = dns_message_renderend(client->message);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (result != ISC_R_SUCCESS)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff goto done;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (TCP_CLIENT(client)) {
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff socket = client->tcpsocket;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff address = NULL;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff isc_buffer_used(&buffer, &r);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff isc_buffer_add(&tcpbuffer, r.length);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson isc_buffer_used(&tcpbuffer, &r);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson } else {
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson socket = dns_dispatch_getsocket(client->dispatch);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson address = &client->dispevent->addr;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson isc_buffer_used(&buffer, &r);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff }
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff CTRACE("sendto");
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff result = isc_socket_sendto(socket, &r, client->task, client_senddone,
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff client, address);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff if (result == ISC_R_SUCCESS)
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff client->nsends++;
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff done:
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington if (result != ISC_R_SUCCESS)
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington isc_mempool_put(client->sendbufs, data);
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff ns_client_next(client, result);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff}
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graffvoid
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graffns_client_error(ns_client_t *client, isc_result_t result) {
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson dns_rcode_t rcode;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff dns_message_t *message;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
ec46482ef3c0c3e9747f0074cb9263adb8aef961Andreas Gustafsson REQUIRE(NS_CLIENT_VALID(client));
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff CTRACE("error");
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff message = client->message;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff rcode = dns_result_torcode(result);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff /*
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff * message may be an in-progress reply that we had trouble
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff * with, in which case QR will be set. We need to clear QR before
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * calling dns_message_reply() to avoid triggering an assertion.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 message->flags &= ~DNS_MESSAGEFLAG_QR;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * AA and AD shouldn't be set.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_reply(message, ISC_TRUE);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * It could be that we've got a query with a good header,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * but a bad question section, so we try again with
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * want_question_section set to ISC_FALSE.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_reply(message, ISC_FALSE);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * There's no hope of replying to this request.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * XXXRTH Mark this client to that if it is a
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * TCP session, the session will be closed.
281bab0f36eaedc56f859721fbdf45568b71cd60Mark Andrews */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_next(client, result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 message->rcode = rcode;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_send(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static inline isc_result_t
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉client_addopt(ns_client_t *client) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_rdataset_t *rdataset;
281bab0f36eaedc56f859721fbdf45568b71cd60Mark Andrews dns_rdatalist_t *rdatalist;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_rdata_t *rdata;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(client->opt == NULL); /* XXXRTH free old. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdatalist = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_gettemprdatalist(client->message, &rdatalist);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdata = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_gettemprdata(client->message, &rdata);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdataset = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_gettemprdataset(client->message, &rdataset);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_rdataset_init(rdataset);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdatalist->type = dns_rdatatype_opt;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdatalist->covers = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Set Maximum UDP buffer size.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdatalist->rdclass = SEND_BUFFER_SIZE;
281bab0f36eaedc56f859721fbdf45568b71cd60Mark Andrews
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
281bab0f36eaedc56f859721fbdf45568b71cd60Mark Andrews * Set EXTENDED-RCODE, VERSION, and Z to 0.
281bab0f36eaedc56f859721fbdf45568b71cd60Mark Andrews */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdatalist->ttl = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * No ENDS options.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdata->data = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rdata->length = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LIST_INIT(rdatalist->rdata);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_rdatalist_tordataset(rdatalist, rdataset);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->opt = rdataset;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_SUCCESS);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉client_request(isc_task_t *task, isc_event_t *event) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_t *client;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_dispatchevent_t *devent;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_buffer_t *buffer;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_view_t *view;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_rdataset_t *opt;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(event != NULL);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client = event->arg;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(NS_CLIENT_VALID(client));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(task == client->task);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (event->type == DNS_EVENT_DISPATCH) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 devent = (dns_dispatchevent_t *)event;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(client->dispentry != NULL);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->dispevent = devent;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 buffer = &devent->buffer;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = devent->result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 } else {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(event->type == DNS_EVENT_TCPMSG);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(event->sender == &client->tcpmsg);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 buffer = &client->tcpmsg.buffer;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = client->tcpmsg.result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("request");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state = ns_clientstate_working;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (isc_stdtime_get(&client->requesttime) != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->requesttime = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->now = client->requesttime;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (TCP_CLIENT(client))
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_next(client, result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 else
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_task_shutdown(client->task);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_message_parse(client->message, buffer, ISC_FALSE);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ns_client_error(client, result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 INSIST((client->message->flags & DNS_MESSAGEFLAG_QR) == 0);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Deal with EDNS.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 opt = dns_message_getopt(client->message);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (opt != NULL) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 unsigned int version;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Set the client's UDP buffer size.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff client->udpsize = opt->rdclass;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Create an OPT for our reply.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = client_addopt(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_error(client, result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Do we understand this version of ENDS?
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * XXXRTH need library support for this!
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 version = (opt->ttl & 0x00FF0000) >> 16;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (version != 0) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_error(client, DNS_R_BADVERS);
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff return;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff }
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff }
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff /*
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff * XXXRTH View list management code will be moving to its own module
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * soon.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 RWLOCK(&ns_g_viewlock, isc_rwlocktype_read);
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff for (view = ISC_LIST_HEAD(ns_g_viewlist);
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff view != NULL;
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff view = ISC_LIST_NEXT(view, link)) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * XXXRTH View matching will become more powerful later.
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff */
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews if (client->message->rdclass == view->rdclass) {
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff dns_view_attach(view, &client->view);
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff break;
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff }
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff }
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_read);
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson if (view == NULL) {
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff CTRACE("no view");
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews ns_client_error(client, DNS_R_REFUSED);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff return;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff }
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff /*
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * Dispatch the request.
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews */
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews switch (client->message->opcode) {
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff case dns_opcode_query:
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff CTRACE("query");
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ns_query_start(client);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff break;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff case dns_opcode_update:
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff CTRACE("update");
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ns_update_start(client);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff break;
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson case dns_opcode_notify:
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson CTRACE("notify");
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson ns_client_error(client, DNS_R_NOTIMP);
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson break;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff case dns_opcode_iquery:
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews CTRACE("iquery");
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ns_client_error(client, DNS_R_NOTIMP);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff default:
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff CTRACE("unknown opcode");
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ns_client_error(client, DNS_R_NOTIMP);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff }
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff}
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graffstatic void
c17c59662f0969a5e52e8b7529cbde1a7c746095Andreas Gustafssonclient_timeout(isc_task_t *task, isc_event_t *event) {
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews ns_client_t *client;
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(event != NULL);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(event->type == ISC_TIMEREVENT_LIFE ||
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff event->type == ISC_TIMEREVENT_IDLE);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff client = event->arg;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(NS_CLIENT_VALID(client));
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(task == client->task);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(client->timer != NULL);
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff CTRACE("timeout");
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff isc_event_free(&event);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff ns_client_next(client, ISC_R_TIMEDOUT);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff}
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graffstatic isc_result_t
46993e1d9d18410a5852b7d990338b70b158855cMichael Graffclient_create(ns_clientmgr_t *manager, ns_clienttype_t type,
f98d6edb191348477c9c5a156003df627d9bc42cBrian Wellington ns_client_t **clientp)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff{
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff ns_client_t *client;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * Caller must be holding the manager lock.
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff *
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * Note: creating a client does not add the client to the manager's
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * client list. The caller is responsible for that.
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff */
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff REQUIRE(clientp != NULL && *clientp == NULL);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff client = isc_mem_get(manager->mctx, sizeof *client);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff if (client == NULL)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff return (ISC_R_NOMEMORY);
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff client->task = NULL;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington result = isc_task_create(manager->taskmgr, manager->mctx, 0,
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff &client->task);
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington if (result != ISC_R_SUCCESS)
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington goto cleanup_client;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = isc_task_onshutdown(client->task, client_shutdown, client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff goto cleanup_task;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client->timer = NULL;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington result = isc_timer_create(manager->timermgr, isc_timertype_inactive,
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington NULL, NULL, client->task, client_timeout,
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client, &client->timer);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff if (result != ISC_R_SUCCESS)
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff goto cleanup_task;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff client->message = NULL;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff result = dns_message_create(manager->mctx, DNS_MESSAGE_INTENTPARSE,
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff &client->message);
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 goto cleanup_timer;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /* XXXRTH Hardwired constants */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->sendbufs = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = isc_mempool_create(manager->mctx, SEND_BUFFER_SIZE,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 &client->sendbufs);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 goto cleanup_message;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mempool_setfreemax(client->sendbufs, 3);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mempool_setmaxalloc(client->sendbufs, 3);
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington
04458c8b5da001294e820289b34bf3aee6258f27Michael Graff client->magic = NS_CLIENT_MAGIC;
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff client->mctx = manager->mctx;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client->manager = manager;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client->type = type;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client->state = ns_clientstate_idle;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client->attributes = 0;
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington client->waiting = 0;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff client->view = NULL;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff client->dispatch = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->dispentry = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->dispevent = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->tcplistener = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->tcpsocket = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->nsends = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->opt = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->udpsize = 512;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->next = NULL;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LINK_INIT(client, link);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * We call the init routines for the various kinds of client here,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * after we have created an otherwise valid client, because some
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = ns_query_init(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 goto cleanup_sendbufs;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("create");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *clientp = client;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_SUCCESS);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 cleanup_sendbufs:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mempool_destroy(&client->sendbufs);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->magic = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 cleanup_message:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_message_destroy(&client->message);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 cleanup_timer:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_timer_detach(&client->timer);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 cleanup_task:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_task_detach(&client->task);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 cleanup_client:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mem_put(manager->mctx, client, sizeof *client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉client_read(ns_client_t *client) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("read");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client_request, client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_next(client, result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state = ns_clientstate_reading;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉client_newconn(isc_task_t *task, isc_event_t *event) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_client_t *client = event->arg;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(event->type == ISC_SOCKEVENT_NEWCONN);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(NS_CLIENT_VALID(client));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(client->task == task);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("newconn");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (nevent->result == ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->tcpsocket = nevent->newsocket;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 dns_tcpmsg_init(client->mctx, client->tcpsocket,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 &client->tcpmsg);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client_read(client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 } else {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * XXXRTH What should we do? We're trying to accept but
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * it didn't work. If we just give up, then TCP
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * service may eventually stop.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * For now, we just go idle.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Going idle is probably the right thing if the
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * I/O was canceled.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state = ns_clientstate_idle;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_event_free(&event);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉client_accept(ns_client_t *client) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 CTRACE("accept");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = isc_socket_accept(client->tcplistener, client->task,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client_newconn, client);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 UNEXPECTED_ERROR(__FILE__, __LINE__,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "isc_socket_accept() failed: %s",
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_totext(result));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * XXXRTH What should we do? We're trying to accept but
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * it didn't work. If we just give up, then TCP
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * service may eventually stop.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * For now, we just go idle.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state = ns_clientstate_idle;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉/***
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *** Client Manager
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ***/
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉static void
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉clientmgr_destroy(ns_clientmgr_t *manager) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(manager->nclients == 0);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(ISC_LIST_EMPTY(manager->clients));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 MTRACE("clientmgr_destroy");
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 manager->magic = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mem_put(manager->mctx, manager, sizeof *manager);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉isc_result_t
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_timermgr_t *timermgr, ns_clientmgr_t **managerp)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉{
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_clientmgr_t *manager;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 manager = isc_mem_get(mctx, sizeof *manager);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (manager == NULL)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_NOMEMORY);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = isc_mutex_init(&manager->lock);
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff if (result != ISC_R_SUCCESS)
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson goto cleanup_manager;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 manager->mctx = mctx;
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson manager->taskmgr = taskmgr;
3530e10080e5a7d95c7d13abdc02c1d8bd12ec18Andreas Gustafsson manager->timermgr = timermgr;
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff manager->exiting = ISC_FALSE;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 manager->nclients = 0;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LIST_INIT(manager->clients);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 manager->magic = MANAGER_MAGIC;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff MTRACE("create");
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews *managerp = manager;
bd7237e653e15bfb7f202a1c0914271912597ef4Michael Graff
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_SUCCESS);
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff cleanup_manager:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_mem_put(manager->mctx, manager, sizeof *manager);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (result);
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graffvoid
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graffns_clientmgr_destroy(ns_clientmgr_t **managerp) {
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff ns_clientmgr_t *manager;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff ns_client_t *client;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff isc_boolean_t need_destroy = ISC_FALSE;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(managerp != NULL);
3d181bc9f12154a56bfbb536198a6c481cbcd525David Lawrence manager = *managerp;
6f1422b81ed2c5142092e2ced8e3faf0e61f3ba0Michael Graff REQUIRE(VALID_MANAGER(manager));
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews
6fe411037dafd168a9f65e6b254c50f3db616688Michael Graff MTRACE("destroy");
ce5bd92088923d1c76e26d313d1d8c0b8bbe38feMichael Graff
ef4b66d3557505af25fec8ce461f07ddd671ea1eBob Halley LOCK(&manager->lock);
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews manager->exiting = ISC_TRUE;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff for (client = ISC_LIST_HEAD(manager->clients);
ef4b66d3557505af25fec8ce461f07ddd671ea1eBob Halley client != NULL;
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews client = ISC_LIST_NEXT(client, link))
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews isc_task_shutdown(client->task);
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews if (ISC_LIST_EMPTY(manager->clients))
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews need_destroy = ISC_TRUE;
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff UNLOCK(&manager->lock);
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff
6f1422b81ed2c5142092e2ced8e3faf0e61f3ba0Michael Graff if (need_destroy)
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff clientmgr_destroy(manager);
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff *managerp = NULL;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff}
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
8470db5b12c18cfb32a757e265ce4e7789052c92Michael Graffisc_result_t
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graffns_clientmgr_addtodispatch(ns_clientmgr_t *manager, unsigned int n,
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff dns_dispatch_t *dispatch)
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews{
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews isc_result_t result = ISC_R_SUCCESS;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff unsigned int i;
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff ns_client_t *client;
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews
8affb49a70de247206cb04aae87730f2b4c90dd0Mark Andrews REQUIRE(VALID_MANAGER(manager));
6fe411037dafd168a9f65e6b254c50f3db616688Michael Graff REQUIRE(n > 0);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff MTRACE("addtodispatch");
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff /*
3d181bc9f12154a56bfbb536198a6c481cbcd525David Lawrence * We MUST lock the manager lock for the entire client creation
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * process. If we didn't do this, then a client could get a
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * shutdown event and disappear out from under us.
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff */
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff LOCK(&manager->lock);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff for (i = 0; i < n; i++) {
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff client = NULL;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff result = client_create(manager, ns_clienttype_basic,
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff &client);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff if (result != ISC_R_SUCCESS)
3d181bc9f12154a56bfbb536198a6c481cbcd525David Lawrence break;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff client->state = ns_clientstate_listening;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff dns_dispatch_attach(dispatch, &client->dispatch);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff result = dns_dispatch_addrequest(dispatch, client->task,
ce10da99834b806d5a92a6e5b4c8ff3990a4ae0fBrian Wellington client_request,
ce10da99834b806d5a92a6e5b4c8ff3990a4ae0fBrian Wellington client, &client->dispentry);
5e589b5356a4125b5af32605dead82ab8b467c88Mark Andrews if (result != ISC_R_SUCCESS) {
5e589b5356a4125b5af32605dead82ab8b467c88Mark Andrews client_free(client);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff break;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff }
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff manager->nclients++;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ISC_LIST_APPEND(manager->clients, client, link);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (i != 0) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /*
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * We managed to create at least one client, so we
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * declare victory.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = ISC_R_SUCCESS;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 UNLOCK(&manager->lock);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (result);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉isc_result_t
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉ns_clientmgr_accepttcp(ns_clientmgr_t *manager, isc_socket_t *socket,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 unsigned int n)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉{
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_t result = ISC_R_SUCCESS;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 unsigned int i;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff ns_client_t *client;
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff REQUIRE(VALID_MANAGER(manager));
135b1206c96d03353ac59fde7be0053dacc6568eAndreas Gustafsson REQUIRE(n > 0);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
e992af4209b737f511b6f2fad3ddb7bdfc17b9aeTatuya JINMEI 神明達哉 MTRACE("accepttcp");
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff /*
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * XXXRTH
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff *
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * This does not represent the planned method for TCP support,
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * because we are dedicating a few clients to servicing TCP requests
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * instead of allocating TCP clients from a pool and applying quotas.
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff *
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff * All this will be fixed later, but this code will allow parts of
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * the server that need TCP support, e.g. IXFR and AXFR, to progress.
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff */
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff
6f1422b81ed2c5142092e2ced8e3faf0e61f3ba0Michael Graff /*
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff * We MUST lock the manager lock for the entire client creation
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff * process. If we didn't do this, then a client could get a
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * shutdown event and disappear out from under us.
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff */
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff
d1ba6e9180c7d7458656a6e53e52fde056fd244cMichael Graff LOCK(&manager->lock);
58efc48a9623797187726360efb57cbabd916f0dMichael Graff
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff for (i = 0; i < n; i++) {
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews client = NULL;
e21d199dca95aff5d50f133d6b064309e209af00Brian Wellington result = client_create(manager, ns_clienttype_tcp, &client);
e21d199dca95aff5d50f133d6b064309e209af00Brian Wellington if (result != ISC_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 break;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 client->state = ns_clientstate_listening;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff client->attributes |= NS_CLIENTATTR_TCP;
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff isc_socket_attach(socket, &client->tcplistener);
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff client_accept(client);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff manager->nclients++;
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews ISC_LIST_APPEND(manager->clients, client, link);
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff }
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff if (i != 0) {
8ed2c82a73a6c0a9c04ad26a4ef39b5a738099f6Andreas Gustafsson /*
f98d6edb191348477c9c5a156003df627d9bc42cBrian Wellington * We managed to create at least one client, so we
6f1422b81ed2c5142092e2ced8e3faf0e61f3ba0Michael Graff * declare victory.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 */
e992af4209b737f511b6f2fad3ddb7bdfc17b9aeTatuya JINMEI 神明達哉 result = ISC_R_SUCCESS;
e992af4209b737f511b6f2fad3ddb7bdfc17b9aeTatuya JINMEI 神明達哉 }
e992af4209b737f511b6f2fad3ddb7bdfc17b9aeTatuya JINMEI 神明達哉
e992af4209b737f511b6f2fad3ddb7bdfc17b9aeTatuya JINMEI 神明達哉 UNLOCK(&manager->lock);
e992af4209b737f511b6f2fad3ddb7bdfc17b9aeTatuya JINMEI 神明達哉
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington return (result);
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington}
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington
0b14ebe3132846e91bea02992493e3d506233e76Michael Graffisc_sockaddr_t *
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉ns_client_getsockaddr(ns_client_t *client) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (TCP_CLIENT(client))
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (&client->tcpmsg.address);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 else
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (&client->dispevent->addr);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉}
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉