Lines Matching refs:client

66  * All client state changes, other than that from idle to listening, occur
70 * If a routine is ever created that allows someone other than the client's
71 * task to change the client, then the client will have to be locked.
76 #define CTRACE(m) ns_client_log(client, \
102 * client objects, since concurrent access to a shared context would cause
115 /*% nameserver client manager structure */
140 * states are generally "more active", meaning that the client can
145 * To force the client into a less active state, set client->newstate
152 * The client object no longer exists.
157 * The client object exists and has a task and timer.
159 * It is on the client manager's list of inactive clients.
165 * The client object is either a TCP or a UDP one, and
167 * client manager's list of active clients.
169 * If it is a TCP client object, it has a TCP listener socket
172 * If it is a UDP client object, it has a UDP listener socket
178 * The client object is a TCP client object that has received
181 * UDP client objects.
186 * The client object has received a request and is working
193 * Sentinel value used to indicate "no state". When client->newstate
207 static void client_read(ns_client_t *client);
208 static void client_accept(ns_client_t *client);
209 static void client_udprecv(ns_client_t *client);
211 static isc_boolean_t exit_check(ns_client_t *client);
212 static void ns_client_endrequest(ns_client_t *client);
213 static void ns_client_checkactive(ns_client_t *client);
216 static void ns_client_dumpmessage(ns_client_t *client, const char *reason);
219 ns_client_recursing(ns_client_t *client) {
220 REQUIRE(NS_CLIENT_VALID(client));
222 LOCK(&client->manager->lock);
223 ISC_LIST_UNLINK(*client->list, client, link);
224 ISC_LIST_APPEND(client->manager->recursing, client, link);
225 client->list = &client->manager->recursing;
226 UNLOCK(&client->manager->lock);
230 ns_client_killoldestquery(ns_client_t *client) {
232 REQUIRE(NS_CLIENT_VALID(client));
234 LOCK(&client->manager->lock);
235 oldest = ISC_LIST_HEAD(client->manager->recursing);
239 ISC_LIST_APPEND(client->manager->active, oldest, link);
240 oldest->list = &client->manager->active;
242 UNLOCK(&client->manager->lock);
246 ns_client_settimeout(ns_client_t *client, unsigned int seconds) {
251 result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
253 client->timerset = ISC_TRUE;
255 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
266 * the caller must no longer use the client object as it may have been
270 exit_check(ns_client_t *client) {
274 REQUIRE(NS_CLIENT_VALID(client));
276 if (client->state <= client->newstate)
279 INSIST(client->newstate < NS_CLIENTSTATE_WORKING);
287 * - The client does not detach from the view until references is zero
292 if (client->nupdates == 0 &&
293 client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
294 dns_view_detach(&client->view);
296 if (client->state == NS_CLIENTSTATE_WORKING) {
297 INSIST(client->newstate <= NS_CLIENTSTATE_READING);
301 if (client->nupdates > 0)
306 if (client->nsends > 0) {
308 if (TCP_CLIENT(client))
309 socket = client->tcpsocket;
311 socket = client->udpsocket;
312 isc_socket_cancel(socket, client->task,
316 if (! (client->nsends == 0 && client->nrecvs == 0 &&
317 client->references == 0))
328 * the client is on the active list and not the
331 LOCK(&client->manager->lock);
332 if (client->list == &client->manager->recursing) {
333 ISC_LIST_UNLINK(*client->list, client, link);
334 ISC_LIST_APPEND(client->manager->active, client, link);
335 client->list = &client->manager->active;
337 UNLOCK(&client->manager->lock);
338 ns_client_endrequest(client);
340 client->state = NS_CLIENTSTATE_READING;
341 INSIST(client->recursionquota == NULL);
342 if (NS_CLIENTSTATE_READING == client->newstate) {
343 client_read(client);
344 client->newstate = NS_CLIENTSTATE_MAX;
349 if (client->state == NS_CLIENTSTATE_READING) {
354 INSIST(client->recursionquota == NULL);
355 INSIST(client->newstate <= NS_CLIENTSTATE_READY);
356 if (client->nreads > 0)
357 dns_tcpmsg_cancelread(&client->tcpmsg);
358 if (! client->nreads == 0) {
363 if (client->tcpmsg_valid) {
364 dns_tcpmsg_invalidate(&client->tcpmsg);
365 client->tcpmsg_valid = ISC_FALSE;
367 if (client->tcpsocket != NULL) {
369 isc_socket_detach(&client->tcpsocket);
372 if (client->tcpquota != NULL)
373 isc_quota_detach(&client->tcpquota);
375 if (client->timerset) {
376 (void)isc_timer_reset(client->timer,
379 client->timerset = ISC_FALSE;
382 client->peeraddr_valid = ISC_FALSE;
384 client->state = NS_CLIENTSTATE_READY;
385 INSIST(client->recursionquota == NULL);
388 * Now the client is ready to accept a new TCP connection
390 * that already. Check whether this client needs to remain
393 ns_client_checkactive(client);
395 if (NS_CLIENTSTATE_READY == client->newstate) {
396 if (TCP_CLIENT(client)) {
397 client_accept(client);
399 client_udprecv(client);
400 client->newstate = NS_CLIENTSTATE_MAX;
405 if (client->state == NS_CLIENTSTATE_READY) {
406 INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
410 if (client->naccepts > 0)
411 isc_socket_cancel(client->tcplistener, client->task,
414 if (! (client->naccepts == 0)) {
420 if (client->nrecvs > 0)
421 isc_socket_cancel(client->udpsocket, client->task,
423 if (! (client->nrecvs == 0)) {
429 if (client->nctls > 0) {
434 /* Deactivate the client. */
435 if (client->interface)
436 ns_interface_detach(&client->interface);
438 INSIST(client->naccepts == 0);
439 INSIST(client->recursionquota == NULL);
440 if (client->tcplistener != NULL)
441 isc_socket_detach(&client->tcplistener);
443 if (client->udpsocket != NULL)
444 isc_socket_detach(&client->udpsocket);
446 if (client->dispatch != NULL)
447 dns_dispatch_detach(&client->dispatch);
449 client->attributes = 0;
450 client->mortal = ISC_FALSE;
452 LOCK(&client->manager->lock);
454 * Put the client on the inactive list. If we are aiming for
458 * the dying client inbetween.
460 locked_manager = client->manager;
461 ISC_LIST_UNLINK(*client->list, client, link);
462 ISC_LIST_APPEND(client->manager->inactive, client, link);
463 client->list = &client->manager->inactive;
464 client->state = NS_CLIENTSTATE_INACTIVE;
465 INSIST(client->recursionquota == NULL);
467 if (client->state == client->newstate) {
468 client->newstate = NS_CLIENTSTATE_MAX;
469 if (client->needshutdown)
470 isc_task_shutdown(client->task);
475 if (client->state == NS_CLIENTSTATE_INACTIVE) {
476 INSIST(client->newstate == NS_CLIENTSTATE_FREED);
478 * We are trying to free the client.
485 REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
487 INSIST(client->recursionquota == NULL);
489 ns_query_free(client);
490 isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
491 isc_event_free((isc_event_t **)&client->sendevent);
492 isc_event_free((isc_event_t **)&client->recvevent);
493 isc_timer_detach(&client->timer);
495 if (client->tcpbuf != NULL)
496 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
497 if (client->opt != NULL) {
498 INSIST(dns_rdataset_isassociated(client->opt));
499 dns_rdataset_disassociate(client->opt);
500 dns_message_puttemprdataset(client->message, &client->opt);
502 dns_message_destroy(&client->message);
503 if (client->manager != NULL) {
504 ns_clientmgr_t *manager = client->manager;
509 ISC_LIST_UNLINK(*client->list, client, link);
510 client->list = NULL;
520 * client->task.
522 if (client->task != NULL)
523 isc_task_detach(&client->task);
526 client->magic = 0;
531 if (ns_g_clienttest && isc_mem_references(client->mctx) != 1) {
532 isc_mem_stats(client->mctx, stderr);
535 isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
547 * Only now is it safe to destroy the client manager (if needed),
557 * The client's task has received the client's control event
562 ns_client_t *client = (ns_client_t *) event->ev_arg;
564 INSIST(task == client->task);
568 INSIST(client->nctls == 1);
569 client->nctls--;
571 if (exit_check(client))
574 if (TCP_CLIENT(client)) {
575 client_accept(client);
577 client_udprecv(client);
583 * The client's task has received a shutdown event.
587 ns_client_t *client;
591 client = event->ev_arg;
592 REQUIRE(NS_CLIENT_VALID(client));
593 REQUIRE(task == client->task);
601 if (client->shutdown != NULL) {
602 (client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
603 client->shutdown = NULL;
604 client->shutdown_arg = NULL;
607 client->newstate = NS_CLIENTSTATE_FREED;
608 client->needshutdown = ISC_FALSE;
609 (void)exit_check(client);
613 ns_client_endrequest(ns_client_t *client) {
614 INSIST(client->naccepts == 0);
615 INSIST(client->nreads == 0);
616 INSIST(client->nsends == 0);
617 INSIST(client->nrecvs == 0);
618 INSIST(client->nupdates == 0);
619 INSIST(client->state == NS_CLIENTSTATE_WORKING);
623 if (client->next != NULL) {
624 (client->next)(client);
625 client->next = NULL;
628 if (client->view != NULL)
629 dns_view_detach(&client->view);
630 if (client->opt != NULL) {
631 INSIST(dns_rdataset_isassociated(client->opt));
632 dns_rdataset_disassociate(client->opt);
633 dns_message_puttemprdataset(client->message, &client->opt);
636 client->signer = NULL;
637 client->udpsize = 512;
638 client->extflags = 0;
639 client->ednsversion = -1;
640 dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
642 if (client->recursionquota != NULL)
643 isc_quota_detach(&client->recursionquota);
646 * Clear all client attributes that are specific to
649 client->attributes &= NS_CLIENTATTR_TCP;
653 ns_client_checkactive(ns_client_t *client) {
654 if (client->mortal) {
656 * This client object should normally go inactive
657 * at this point, but if we have fewer active client
662 if (TCP_CLIENT(client) && !ns_g_clienttest) {
663 LOCK(&client->interface->lock);
664 if (client->interface->ntcpcurrent <
665 client->interface->ntcptarget)
667 UNLOCK(&client->interface->lock);
670 * The UDP client quota is enforced by making
678 * We don't need this client object. Recycle it.
680 if (client->newstate >= NS_CLIENTSTATE_INACTIVE)
681 client->newstate = NS_CLIENTSTATE_INACTIVE;
687 ns_client_next(ns_client_t *client, isc_result_t result) {
690 REQUIRE(NS_CLIENT_VALID(client));
691 REQUIRE(client->state == NS_CLIENTSTATE_WORKING ||
692 client->state == NS_CLIENTSTATE_READING);
697 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
706 if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
711 if (client->newstate > newstate)
712 client->newstate = newstate;
713 (void)exit_check(client);
719 ns_client_t *client;
724 client = sevent->ev_arg;
725 REQUIRE(NS_CLIENT_VALID(client));
726 REQUIRE(task == client->task);
727 REQUIRE(sevent == client->sendevent);
734 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
739 INSIST(client->nsends > 0);
740 client->nsends--;
742 if (client->tcpbuf != NULL) {
743 INSIST(TCP_CLIENT(client));
744 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
745 client->tcpbuf = NULL;
748 if (exit_check(client))
751 ns_client_next(client, ISC_R_SUCCESS);
763 client_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
775 if (TCP_CLIENT(client)) {
776 INSIST(client->tcpbuf == NULL);
781 client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
782 if (client->tcpbuf == NULL) {
786 data = client->tcpbuf;
797 if (client->udpsize < SEND_BUFFER_SIZE)
798 bufsize = client->udpsize;
815 client_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
825 if (TCP_CLIENT(client)) {
826 socket = client->tcpsocket;
829 socket = client->udpsocket;
830 address = &client->peeraddr;
832 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
843 if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0 &&
844 (client->attributes & NS_CLIENTATTR_MULTICAST) == 0)
845 pktinfo = &client->pktinfo;
853 result = isc_socket_sendto2(socket, &r, client->task,
855 client->sendevent, sockflags);
857 client->nsends++;
859 client_senddone(client->task,
860 (isc_event_t *)client->sendevent);
867 ns_client_sendraw(ns_client_t *client, dns_message_t *message) {
875 REQUIRE(NS_CLIENT_VALID(client));
885 result = client_allocsendbuf(client, &buffer, NULL, mr->length,
897 r.base[0] = (client->message->id >> 8) & 0xff;
898 r.base[1] = client->message->id & 0xff;
900 result = client_sendpkg(client, &buffer);
905 if (client->tcpbuf != NULL) {
906 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
907 client->tcpbuf = NULL;
909 ns_client_next(client, result);
913 ns_client_send(ns_client_t *client) {
926 REQUIRE(NS_CLIENT_VALID(client));
930 if ((client->attributes & NS_CLIENTATTR_RA) != 0)
931 client->message->flags |= DNS_MESSAGEFLAG_RA;
933 if ((client->attributes & NS_CLIENTATTR_WANTDNSSEC) != 0)
939 if (client->view != NULL) {
940 if (client->view->preferred_glue == dns_rdatatype_a)
942 else if (client->view->preferred_glue == dns_rdatatype_aaaa)
949 result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0,
954 result = dns_compress_init(&cctx, -1, client->mctx);
960 result = dns_message_renderbegin(client->message, &cctx, &buffer);
964 if (client->opt != NULL) {
965 result = dns_message_setopt(client->message, client->opt);
967 client->opt = NULL;
971 result = dns_message_rendersection(client->message,
974 client->message->flags |= DNS_MESSAGEFLAG_TC;
979 result = dns_message_rendersection(client->message,
984 client->message->flags |= DNS_MESSAGEFLAG_TC;
989 result = dns_message_rendersection(client->message,
994 client->message->flags |= DNS_MESSAGEFLAG_TC;
999 result = dns_message_rendersection(client->message,
1005 result = dns_message_renderend(client->message);
1015 if (TCP_CLIENT(client)) {
1019 result = client_sendpkg(client, &tcpbuffer);
1021 result = client_sendpkg(client, &buffer);
1029 if (client->message->tsigkey != NULL) {
1033 if (client->message->sig0key != NULL) {
1037 if ((client->message->flags & DNS_MESSAGEFLAG_TC) != 0)
1045 if (client->tcpbuf != NULL) {
1046 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
1047 client->tcpbuf = NULL;
1053 ns_client_next(client, result);
1085 ns_client_error(ns_client_t *client, isc_result_t result) {
1089 REQUIRE(NS_CLIENT_VALID(client));
1093 message = client->message;
1101 ns_client_dropport(isc_sockaddr_getport(&client->peeraddr)) !=
1109 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1113 ns_client_next(client, ISC_R_SUCCESS);
1137 ns_client_next(client, result);
1145 * with the same ID to the same client less than two
1153 if (isc_sockaddr_equal(&client->peeraddr,
1154 &client->formerrcache.addr) &&
1155 message->id == client->formerrcache.id &&
1156 client->requesttime - client->formerrcache.time < 2) {
1158 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1162 ns_client_next(client, result);
1165 client->formerrcache.addr = client->peeraddr;
1166 client->formerrcache.time = client->requesttime;
1167 client->formerrcache.id = message->id;
1169 ns_client_send(client);
1173 client_addopt(ns_client_t *client) {
1183 REQUIRE(client->opt == NULL); /* XXXRTH free old. */
1185 view = client->view;
1192 flags = client->extflags & DNS_MESSAGEEXTFLAG_REPLYPRESERVE;
1195 if ((client->attributes & NS_CLIENTATTR_WANTNSID) != 0 &&
1214 result = dns_message_buildopt(client->message, &client->opt, 0,
1298 process_opt(ns_client_t *client, dns_rdataset_t *opt) {
1306 * Set the client's UDP buffer size.
1308 client->udpsize = opt->rdclass;
1314 if (client->udpsize < 512)
1315 client->udpsize = 512;
1320 client->extflags = (isc_uint16_t)(opt->ttl & 0xFFFF);
1327 client->ednsversion = (opt->ttl & 0x00FF0000) >> 16;
1328 if (client->ednsversion > 0) {
1331 result = client_addopt(client);
1334 ns_client_error(client, result);
1350 client->attributes |= NS_CLIENTATTR_WANTNSID;
1365 result = client_addopt(client);
1367 ns_client_error(client, result);
1380 ns_client_t *client;
1398 client = event->ev_arg;
1399 REQUIRE(NS_CLIENT_VALID(client));
1400 REQUIRE(task == client->task);
1402 INSIST(client->recursionquota == NULL);
1404 INSIST(client->state == (TCP_CLIENT(client) ?
1411 INSIST(!TCP_CLIENT(client));
1413 REQUIRE(sevent == client->recvevent);
1419 client->peeraddr = sevent->address;
1420 client->peeraddr_valid = ISC_TRUE;
1423 client->attributes |= NS_CLIENTATTR_PKTINFO;
1424 client->pktinfo = sevent->pktinfo;
1427 client->attributes |= NS_CLIENTATTR_MULTICAST;
1428 client->nrecvs--;
1430 INSIST(TCP_CLIENT(client));
1432 REQUIRE(event->ev_sender == &client->tcpmsg);
1433 buffer = &client->tcpmsg.buffer;
1434 result = client->tcpmsg.result;
1435 INSIST(client->nreads == 1);
1437 * client->peeraddr was set when the connection was accepted.
1439 client->nreads--;
1442 if (exit_check(client))
1444 client->state = client->newstate = NS_CLIENTSTATE_WORKING;
1446 isc_task_getcurrenttime(task, &client->requesttime);
1447 client->now = client->requesttime;
1450 if (TCP_CLIENT(client)) {
1451 ns_client_next(client, result);
1457 "UDP client handler shutting "
1461 isc_task_shutdown(client->task);
1466 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
1469 if (ns_client_dropport(isc_sockaddr_getport(&client->peeraddr)) ==
1471 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1474 ns_client_next(client, ISC_R_SUCCESS);
1479 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1482 TCP_CLIENT(client) ? "TCP" : "UDP");
1488 if (!TCP_CLIENT(client)) {
1496 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1499 ns_client_next(client, ISC_R_SUCCESS);
1508 if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) {
1509 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1512 ns_client_next(client, DNS_R_REFUSED);
1522 ns_client_next(client, result);
1527 * The client object handles requests, not responses.
1532 if (TCP_CLIENT(client)) {
1534 ns_client_next(client, DNS_R_FORMERR);
1537 dns_dispatch_importrecv(client->dispatch, event);
1538 ns_client_next(client, ISC_R_SUCCESS);
1546 if (isc_sockaddr_pf(&client->peeraddr) == PF_INET) {
1553 if (TCP_CLIENT(client))
1560 result = dns_message_parse(client->message, buffer, 0);
1566 ns_client_error(client, result);
1571 client->message->opcode);
1572 switch (client->message->opcode) {
1584 client->message->rcode = dns_rcode_noerror;
1587 if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0)
1588 client->message->flags &= ~DNS_MESSAGEFLAG_RD;
1593 opt = dns_message_getopt(client->message);
1595 result = process_opt(client, opt);
1600 if (client->message->rdclass == 0) {
1601 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1604 ns_client_dumpmessage(client,
1606 ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR);
1621 if ((client->interface->flags & NS_INTERFACEFLAG_ANYADDR) == 0)
1622 isc_netaddr_fromsockaddr(&destaddr, &client->interface->addr);
1626 if (TCP_CLIENT(client)) {
1629 result = isc_socket_getsockname(client->tcpsocket,
1636 client->interface->addr.type.sa.sa_family == AF_INET6 &&
1637 (client->attributes & NS_CLIENTATTR_PKTINFO) != 0) {
1648 if (IN6_IS_ADDR_LINKLOCAL(&client->pktinfo.ipi6_addr))
1649 zone = (isc_uint32_t)client->pktinfo.ipi6_ifindex;
1652 &client->pktinfo.ipi6_addr);
1661 ns_client_next(client, ISC_R_SUCCESS);
1667 * Find a view that matches the client's source address.
1672 if (client->message->rdclass == view->rdclass ||
1673 client->message->rdclass == dns_rdataclass_any)
1677 sigresult = dns_message_rechecksig(client->message,
1680 tsig = dns_tsigkey_identity(client->message->tsigkey);
1684 !((client->message->flags & DNS_MESSAGEFLAG_RD)
1687 dns_view_attach(view, &client->view);
1704 dns_message_resetsig(client->message);
1706 r = dns_message_getrawmessage(client->message);
1709 (void)dns_tsig_verify(&b, client->message, NULL, NULL);
1711 dns_rdataclass_format(client->message->rdclass, classname,
1713 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1716 ns_client_dumpmessage(client, "no matching view in class");
1717 ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED);
1721 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1731 client->signer = NULL;
1732 dns_name_init(&client->signername, NULL);
1733 result = dns_message_signer(client->message, &client->signername);
1736 if (dns_message_gettsig(client->message, &signame) != NULL) {
1746 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1749 client->signer = &client->signername;
1751 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1755 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1768 if (dns_message_gettsig(client->message, &signame) != NULL) {
1772 status = client->message->tsigstatus;
1777 if (client->message->tsigkey->generated) {
1778 dns_name_format(client->message->tsigkey->creator,
1780 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1789 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1798 status = client->message->sig0status;
1803 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1813 if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
1814 client->message->opcode == dns_opcode_update)) {
1815 ns_client_error(client, sigresult);
1821 * Decide whether recursive service is available to this client.
1828 if (client->view->resolver != NULL &&
1829 client->view->recursion == ISC_TRUE &&
1830 ns_client_checkaclsilent(client, NULL,
1831 client->view->recursionacl,
1833 ns_client_checkaclsilent(client, NULL,
1834 client->view->cacheacl,
1836 ns_client_checkaclsilent(client, &client->interface->addr,
1837 client->view->recursiononacl,
1839 ns_client_checkaclsilent(client, &client->interface->addr,
1840 client->view->cacheonacl,
1845 client->attributes |= NS_CLIENTATTR_RA;
1847 ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT,
1852 * Adjust maximum UDP response size for this client.
1854 if (client->udpsize > 512) {
1860 if (client->udpsize > udpsize)
1861 client->udpsize = udpsize;
1867 switch (client->message->opcode) {
1870 ns_query_start(client);
1874 ns_client_settimeout(client, 60);
1875 ns_update_start(client, sigresult);
1879 ns_client_settimeout(client, 60);
1880 ns_notify_start(client);
1884 ns_client_error(client, DNS_R_NOTIMP);
1888 ns_client_error(client, DNS_R_NOTIMP);
1897 ns_client_t *client;
1902 client = event->ev_arg;
1903 REQUIRE(NS_CLIENT_VALID(client));
1904 REQUIRE(task == client->task);
1905 REQUIRE(client->timer != NULL);
1913 if (client->shutdown != NULL) {
1914 (client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
1915 client->shutdown = NULL;
1916 client->shutdown_arg = NULL;
1919 if (client->newstate > NS_CLIENTSTATE_READY)
1920 client->newstate = NS_CLIENTSTATE_READY;
1921 (void)exit_check(client);
1935 isc_mem_setname(*mctxp, "client", NULL);
1945 isc_mem_setname(clientmctx, "client", NULL);
1963 ns_client_t *client;
1970 * Note: creating a client does not add the client to the
1971 * manager's client list or set the client's manager pointer.
1981 client = isc_mem_get(mctx, sizeof(*client));
1982 if (client == NULL) {
1986 client->mctx = mctx;
1988 client->task = NULL;
1989 result = isc_task_create(manager->taskmgr, 0, &client->task);
1992 isc_task_setname(client->task, "client", client);
1994 client->timer = NULL;
1996 NULL, NULL, client->task, client_timeout,
1997 client, &client->timer);
2000 client->timerset = ISC_FALSE;
2002 client->message = NULL;
2003 result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE,
2004 &client->message);
2010 client->sendevent = (isc_socketevent_t *)
2011 isc_event_allocate(client->mctx, client,
2013 client_senddone, client,
2015 if (client->sendevent == NULL) {
2020 client->recvbuf = isc_mem_get(client->mctx, RECV_BUFFER_SIZE);
2021 if (client->recvbuf == NULL) {
2026 client->recvevent = (isc_socketevent_t *)
2027 isc_event_allocate(client->mctx, client,
2029 client_request, client,
2031 if (client->recvevent == NULL) {
2036 client->magic = NS_CLIENT_MAGIC;
2037 client->manager = NULL;
2038 client->state = NS_CLIENTSTATE_INACTIVE;
2039 client->newstate = NS_CLIENTSTATE_MAX;
2040 client->naccepts = 0;
2041 client->nreads = 0;
2042 client->nsends = 0;
2043 client->nrecvs = 0;
2044 client->nupdates = 0;
2045 client->nctls = 0;
2046 client->references = 0;
2047 client->attributes = 0;
2048 client->view = NULL;
2049 client->dispatch = NULL;
2050 client->udpsocket = NULL;
2051 client->tcplistener = NULL;
2052 client->tcpsocket = NULL;
2053 client->tcpmsg_valid = ISC_FALSE;
2054 client->tcpbuf = NULL;
2055 client->opt = NULL;
2056 client->udpsize = 512;
2057 client->extflags = 0;
2058 client->ednsversion = -1;
2059 client->next = NULL;
2060 client->shutdown = NULL;
2061 client->shutdown_arg = NULL;
2062 client->signer = NULL;
2063 dns_name_init(&client->signername, NULL);
2064 client->mortal = ISC_FALSE;
2065 client->tcpquota = NULL;
2066 client->recursionquota = NULL;
2067 client->interface = NULL;
2068 client->peeraddr_valid = ISC_FALSE;
2069 ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
2070 NS_EVENT_CLIENTCONTROL, client_start, client, client,
2076 isc_sockaddr_any(&client->formerrcache.addr);
2077 client->formerrcache.time = 0;
2078 client->formerrcache.id = 0;
2079 ISC_LINK_INIT(client, link);
2080 client->list = NULL;
2083 * We call the init routines for the various kinds of client here,
2084 * after we have created an otherwise valid client, because some
2085 * of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
2087 result = ns_query_init(client);
2091 result = isc_task_onshutdown(client->task, client_shutdown, client);
2095 client->needshutdown = ns_g_clienttest;
2099 *clientp = client;
2104 ns_query_free(client);
2107 isc_event_free((isc_event_t **)&client->recvevent);
2110 isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
2113 isc_event_free((isc_event_t **)&client->sendevent);
2115 client->magic = 0;
2118 dns_message_destroy(&client->message);
2121 isc_timer_detach(&client->timer);
2124 isc_task_detach(&client->task);
2127 isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
2133 client_read(ns_client_t *client) {
2138 result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
2139 client_request, client);
2147 ns_client_settimeout(client, 30);
2149 client->state = client->newstate = NS_CLIENTSTATE_READING;
2150 INSIST(client->nreads == 0);
2151 INSIST(client->recursionquota == NULL);
2152 client->nreads++;
2156 ns_client_next(client, result);
2161 ns_client_t *client = event->ev_arg;
2166 REQUIRE(NS_CLIENT_VALID(client));
2167 REQUIRE(client->task == task);
2171 INSIST(client->state == NS_CLIENTSTATE_READY);
2173 INSIST(client->naccepts == 1);
2174 client->naccepts--;
2176 LOCK(&client->interface->lock);
2177 INSIST(client->interface->ntcpcurrent > 0);
2178 client->interface->ntcpcurrent--;
2179 UNLOCK(&client->interface->lock);
2186 client->tcpsocket = nevent->newsocket;
2187 isc_socket_setname(client->tcpsocket, "client-tcp", NULL);
2188 client->state = NS_CLIENTSTATE_READING;
2189 INSIST(client->recursionquota == NULL);
2191 (void)isc_socket_getpeername(client->tcpsocket,
2192 &client->peeraddr);
2193 client->peeraddr_valid = ISC_TRUE;
2194 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2208 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2214 if (exit_check(client))
2221 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
2230 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2233 client->newstate = NS_CLIENTSTATE_READY;
2234 (void)exit_check(client);
2238 INSIST(client->tcpmsg_valid == ISC_FALSE);
2239 dns_tcpmsg_init(client->mctx, client->tcpsocket,
2240 &client->tcpmsg);
2241 client->tcpmsg_valid = ISC_TRUE;
2244 * Let a new client take our place immediately, before
2250 &client->tcpquota);
2252 result = ns_client_replace(client);
2254 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2260 client_read(client);
2268 client_accept(ns_client_t *client) {
2273 result = isc_socket_accept(client->tcplistener, client->task,
2274 client_newconn, client);
2288 INSIST(client->naccepts == 0);
2289 client->naccepts++;
2290 LOCK(&client->interface->lock);
2291 client->interface->ntcpcurrent++;
2292 UNLOCK(&client->interface->lock);
2296 client_udprecv(ns_client_t *client) {
2302 r.base = client->recvbuf;
2304 result = isc_socket_recv2(client->udpsocket, &r, 1,
2305 client->task, client->recvevent, 0);
2318 INSIST(client->nrecvs == 0);
2319 client->nrecvs++;
2336 ns_client_t *client = *clientp;
2338 client->references--;
2339 INSIST(client->references >= 0);
2341 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2343 "ns_client_detach: ref = %d", client->references);
2344 (void)exit_check(client);
2348 ns_client_shuttingdown(ns_client_t *client) {
2349 return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
2353 ns_client_replace(ns_client_t *client) {
2358 REQUIRE(client != NULL);
2359 REQUIRE(client->manager != NULL);
2361 result = ns_clientmgr_createclients(client->manager,
2362 1, client->interface,
2363 (TCP_CLIENT(client) ?
2370 * transferred to the new client. Therefore, the old client
2373 client->mortal = ISC_TRUE;
2453 ns_client_t *client;
2466 for (client = ISC_LIST_HEAD(manager->recursing);
2467 client != NULL;
2468 client = ISC_LIST_NEXT(client, link))
2469 isc_task_shutdown(client->task);
2471 for (client = ISC_LIST_HEAD(manager->active);
2472 client != NULL;
2473 client = ISC_LIST_NEXT(client, link))
2474 isc_task_shutdown(client->task);
2476 for (client = ISC_LIST_HEAD(manager->inactive);
2477 client != NULL;
2478 client = ISC_LIST_NEXT(client, link))
2479 isc_task_shutdown(client->task);
2500 ns_client_t *client;
2508 * We MUST lock the manager lock for the entire client creation
2509 * process. If we didn't do this, then a client could get a
2518 * Allocate a client. First try to get a recycled one;
2521 client = NULL;
2523 client = ISC_LIST_HEAD(manager->inactive);
2524 if (client != NULL) {
2526 ISC_LIST_UNLINK(manager->inactive, client, link);
2527 client->list = NULL;
2530 result = client_create(manager, &client);
2535 ns_interface_attach(ifp, &client->interface);
2536 client->state = NS_CLIENTSTATE_READY;
2537 INSIST(client->recursionquota == NULL);
2540 client->attributes |= NS_CLIENTATTR_TCP;
2542 &client->tcplistener);
2547 &client->dispatch);
2548 sock = dns_dispatch_getsocket(client->dispatch);
2549 isc_socket_attach(sock, &client->udpsocket);
2551 client->manager = manager;
2552 ISC_LIST_APPEND(manager->active, client, link);
2553 client->list = &manager->active;
2555 INSIST(client->nctls == 0);
2556 client->nctls++;
2557 ev = &client->ctlevent;
2558 isc_task_send(client->task, &ev);
2562 * We managed to create at least one client, so we
2574 ns_client_getsockaddr(ns_client_t *client) {
2575 return (&client->peeraddr);
2579 ns_client_checkaclsilent(ns_client_t *client, isc_sockaddr_t *sockaddr,
2595 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
2599 result = dns_acl_match(&netaddr, client->signer, acl,
2617 ns_client_checkacl(ns_client_t *client, isc_sockaddr_t *sockaddr,
2622 ns_client_checkaclsilent(client, sockaddr, acl, default_allow);
2625 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2629 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2636 ns_client_name(ns_client_t *client, char *peerbuf, size_t len) {
2637 if (client->peeraddr_valid)
2638 isc_sockaddr_format(&client->peeraddr, peerbuf, len);
2640 snprintf(peerbuf, len, "@%p", client);
2644 ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
2653 ns_client_name(client, peerbuf, sizeof(peerbuf));
2654 if (client->view != NULL && strcmp(client->view->name, "_bind") != 0 &&
2655 strcmp(client->view->name, "_default") != 0) {
2656 name = client->view->name;
2661 "client %s%s%s: %s", peerbuf, sep, name, msgbuf);
2665 ns_client_log(ns_client_t *client, isc_logcategory_t *category,
2674 ns_client_logv(client, category, module, level, fmt, ap);
2694 ns_client_dumpmessage(ns_client_t *client, const char *reason) {
2706 buf = isc_mem_get(client->mctx, len);
2710 result = dns_message_totext(client->message,
2714 isc_mem_put(client->mctx, buf, len);
2717 ns_client_log(client, NS_LOGCATEGORY_UNMATCHED,
2725 isc_mem_put(client->mctx, buf, len);
2730 ns_client_t *client;
2739 client = ISC_LIST_HEAD(manager->recursing);
2740 while (client != NULL) {
2741 ns_client_name(client, peerbuf, sizeof(peerbuf));
2742 if (client->view != NULL &&
2743 strcmp(client->view->name, "_bind") != 0 &&
2744 strcmp(client->view->name, "_default") != 0) {
2745 name = client->view->name;
2751 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
2752 fprintf(f, "; client %s%s%s: '%s' requesttime %d\n",
2753 peerbuf, sep, name, namebuf, client->requesttime);
2754 client = ISC_LIST_NEXT(client, link);
2760 ns_client_qnamereplace(ns_client_t *client, dns_name_t *name) {
2762 if (client->manager != NULL)
2763 LOCK(&client->manager->lock);
2764 if (client->query.restarts > 0) {
2766 * client->query.qname was dynamically allocated.
2768 dns_message_puttempname(client->message,
2769 &client->query.qname);
2771 client->query.qname = name;
2772 if (client->manager != NULL)
2773 UNLOCK(&client->manager->lock);