PageRenderTime 100ms CodeModel.GetById 24ms app.highlight 65ms RepoModel.GetById 1ms app.codeStats 1ms

/contrib/bind9/bin/named/statschannel.c

https://bitbucket.org/freebsd/freebsd-head/
C | 1456 lines | 1187 code | 191 blank | 78 comment | 190 complexity | 1b75465069ecc9a6b59bb4df69ac6961 MD5 | raw file
   1/*
   2 * Copyright (C) 2008-2011  Internet Systems Consortium, Inc. ("ISC")
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
   9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14 * PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17/* $Id: statschannel.c,v 1.26.150.2 2011/03/12 04:59:14 tbox Exp $ */
  18
  19/*! \file */
  20
  21#include <config.h>
  22
  23#include <isc/buffer.h>
  24#include <isc/httpd.h>
  25#include <isc/mem.h>
  26#include <isc/once.h>
  27#include <isc/print.h>
  28#include <isc/socket.h>
  29#include <isc/stats.h>
  30#include <isc/task.h>
  31
  32#include <dns/cache.h>
  33#include <dns/db.h>
  34#include <dns/opcode.h>
  35#include <dns/resolver.h>
  36#include <dns/rdataclass.h>
  37#include <dns/rdatatype.h>
  38#include <dns/stats.h>
  39#include <dns/view.h>
  40#include <dns/zt.h>
  41
  42#include <named/log.h>
  43#include <named/server.h>
  44#include <named/statschannel.h>
  45
  46#include "bind9.xsl.h"
  47
  48struct ns_statschannel {
  49	/* Unlocked */
  50	isc_httpdmgr_t				*httpdmgr;
  51	isc_sockaddr_t				address;
  52	isc_mem_t				*mctx;
  53
  54	/*
  55	 * Locked by channel lock: can be referenced and modified by both
  56	 * the server task and the channel task.
  57	 */
  58	isc_mutex_t				lock;
  59	dns_acl_t				*acl;
  60
  61	/* Locked by server task */
  62	ISC_LINK(struct ns_statschannel)	link;
  63};
  64
  65typedef enum { statsformat_file, statsformat_xml } statsformat_t;
  66
  67typedef struct
  68stats_dumparg {
  69	statsformat_t	type;
  70	void		*arg;		/* type dependent argument */
  71	int		ncounters;	/* used for general statistics */
  72	int		*counterindices; /* used for general statistics */
  73	isc_uint64_t	*countervalues;	 /* used for general statistics */
  74	isc_result_t	result;
  75} stats_dumparg_t;
  76
  77static isc_once_t once = ISC_ONCE_INIT;
  78
  79/*%
  80 * Statistics descriptions.  These could be statistically initialized at
  81 * compile time, but we configure them run time in the init_desc() function
  82 * below so that they'll be less susceptible to counter name changes.
  83 */
  84static const char *nsstats_desc[dns_nsstatscounter_max];
  85static const char *resstats_desc[dns_resstatscounter_max];
  86static const char *zonestats_desc[dns_zonestatscounter_max];
  87static const char *sockstats_desc[isc_sockstatscounter_max];
  88#ifdef HAVE_LIBXML2
  89static const char *nsstats_xmldesc[dns_nsstatscounter_max];
  90static const char *resstats_xmldesc[dns_resstatscounter_max];
  91static const char *zonestats_xmldesc[dns_zonestatscounter_max];
  92static const char *sockstats_xmldesc[isc_sockstatscounter_max];
  93#else
  94#define nsstats_xmldesc NULL
  95#define resstats_xmldesc NULL
  96#define zonestats_xmldesc NULL
  97#define sockstats_xmldesc NULL
  98#endif	/* HAVE_LIBXML2 */
  99
 100#define TRY0(a) do { xmlrc = (a); if (xmlrc < 0) goto error; } while(0)
 101
 102/*%
 103 * Mapping arrays to represent statistics counters in the order of our
 104 * preference, regardless of the order of counter indices.  For example,
 105 * nsstats_desc[nsstats_index[0]] will be the description that is shown first.
 106 */
 107static int nsstats_index[dns_nsstatscounter_max];
 108static int resstats_index[dns_resstatscounter_max];
 109static int zonestats_index[dns_zonestatscounter_max];
 110static int sockstats_index[isc_sockstatscounter_max];
 111
 112static inline void
 113set_desc(int counter, int maxcounter, const char *fdesc, const char **fdescs,
 114	 const char *xdesc, const char **xdescs)
 115{
 116	REQUIRE(counter < maxcounter);
 117	REQUIRE(fdescs[counter] == NULL);
 118#ifdef HAVE_LIBXML2
 119	REQUIRE(xdescs[counter] == NULL);
 120#endif
 121
 122	fdescs[counter] = fdesc;
 123#ifdef HAVE_LIBXML2
 124	xdescs[counter] = xdesc;
 125#else
 126	UNUSED(xdesc);
 127	UNUSED(xdescs);
 128#endif
 129}
 130
 131static void
 132init_desc(void) {
 133	int i;
 134
 135	/* Initialize name server statistics */
 136	for (i = 0; i < dns_nsstatscounter_max; i++)
 137		nsstats_desc[i] = NULL;
 138#ifdef HAVE_LIBXML2
 139	for (i = 0; i < dns_nsstatscounter_max; i++)
 140		nsstats_xmldesc[i] = NULL;
 141#endif
 142
 143#define SET_NSSTATDESC(counterid, desc, xmldesc) \
 144	do { \
 145		set_desc(dns_nsstatscounter_ ## counterid, \
 146			 dns_nsstatscounter_max, \
 147			 desc, nsstats_desc, xmldesc, nsstats_xmldesc); \
 148		nsstats_index[i++] = dns_nsstatscounter_ ## counterid; \
 149	} while (0)
 150
 151	i = 0;
 152	SET_NSSTATDESC(requestv4, "IPv4 requests received", "Requestv4");
 153	SET_NSSTATDESC(requestv6, "IPv6 requests received", "Requestv6");
 154	SET_NSSTATDESC(edns0in, "requests with EDNS(0) received", "ReqEdns0");
 155	SET_NSSTATDESC(badednsver,
 156		       "requests with unsupported EDNS version received",
 157		       "ReqBadEDNSVer");
 158	SET_NSSTATDESC(tsigin, "requests with TSIG received", "ReqTSIG");
 159	SET_NSSTATDESC(sig0in, "requests with SIG(0) received", "ReqSIG0");
 160	SET_NSSTATDESC(invalidsig, "requests with invalid signature",
 161		       "ReqBadSIG");
 162	SET_NSSTATDESC(tcp, "TCP requests received", "ReqTCP");
 163	SET_NSSTATDESC(authrej, "auth queries rejected", "AuthQryRej");
 164	SET_NSSTATDESC(recurserej, "recursive queries rejected", "RecQryRej");
 165	SET_NSSTATDESC(xfrrej, "transfer requests rejected", "XfrRej");
 166	SET_NSSTATDESC(updaterej, "update requests rejected", "UpdateRej");
 167	SET_NSSTATDESC(response, "responses sent", "Response");
 168	SET_NSSTATDESC(truncatedresp, "truncated responses sent",
 169		       "TruncatedResp");
 170	SET_NSSTATDESC(edns0out, "responses with EDNS(0) sent", "RespEDNS0");
 171	SET_NSSTATDESC(tsigout, "responses with TSIG sent", "RespTSIG");
 172	SET_NSSTATDESC(sig0out, "responses with SIG(0) sent", "RespSIG0");
 173	SET_NSSTATDESC(success, "queries resulted in successful answer",
 174		       "QrySuccess");
 175	SET_NSSTATDESC(authans, "queries resulted in authoritative answer",
 176		       "QryAuthAns");
 177	SET_NSSTATDESC(nonauthans,
 178		       "queries resulted in non authoritative answer",
 179		       "QryNoauthAns");
 180	SET_NSSTATDESC(referral, "queries resulted in referral answer",
 181		       "QryReferral");
 182	SET_NSSTATDESC(nxrrset, "queries resulted in nxrrset", "QryNxrrset");
 183	SET_NSSTATDESC(servfail, "queries resulted in SERVFAIL", "QrySERVFAIL");
 184	SET_NSSTATDESC(formerr, "queries resulted in FORMERR", "QryFORMERR");
 185	SET_NSSTATDESC(nxdomain, "queries resulted in NXDOMAIN", "QryNXDOMAIN");
 186	SET_NSSTATDESC(recursion, "queries caused recursion","QryRecursion");
 187	SET_NSSTATDESC(duplicate, "duplicate queries received", "QryDuplicate");
 188	SET_NSSTATDESC(dropped, "queries dropped", "QryDropped");
 189	SET_NSSTATDESC(failure, "other query failures", "QryFailure");
 190	SET_NSSTATDESC(xfrdone, "requested transfers completed", "XfrReqDone");
 191	SET_NSSTATDESC(updatereqfwd, "update requests forwarded",
 192		       "UpdateReqFwd");
 193	SET_NSSTATDESC(updaterespfwd, "update responses forwarded",
 194		       "UpdateRespFwd");
 195	SET_NSSTATDESC(updatefwdfail, "update forward failed", "UpdateFwdFail");
 196	SET_NSSTATDESC(updatedone, "updates completed", "UpdateDone");
 197	SET_NSSTATDESC(updatefail, "updates failed", "UpdateFail");
 198	SET_NSSTATDESC(updatebadprereq,
 199		       "updates rejected due to prerequisite failure",
 200		       "UpdateBadPrereq");
 201	INSIST(i == dns_nsstatscounter_max);
 202
 203	/* Initialize resolver statistics */
 204	for (i = 0; i < dns_resstatscounter_max; i++)
 205		resstats_desc[i] = NULL;
 206#ifdef  HAVE_LIBXML2
 207	for (i = 0; i < dns_resstatscounter_max; i++)
 208		resstats_xmldesc[i] = NULL;
 209#endif
 210
 211#define SET_RESSTATDESC(counterid, desc, xmldesc) \
 212	do { \
 213		set_desc(dns_resstatscounter_ ## counterid, \
 214			 dns_resstatscounter_max, \
 215			 desc, resstats_desc, xmldesc, resstats_xmldesc); \
 216		resstats_index[i++] = dns_resstatscounter_ ## counterid; \
 217	} while (0)
 218
 219	i = 0;
 220	SET_RESSTATDESC(queryv4, "IPv4 queries sent", "Queryv4");
 221	SET_RESSTATDESC(queryv6, "IPv6 queries sent", "Queryv6");
 222	SET_RESSTATDESC(responsev4, "IPv4 responses received", "Responsev4");
 223	SET_RESSTATDESC(responsev6, "IPv6 responses received", "Responsev6");
 224	SET_RESSTATDESC(nxdomain, "NXDOMAIN received", "NXDOMAIN");
 225	SET_RESSTATDESC(servfail, "SERVFAIL received", "SERVFAIL");
 226	SET_RESSTATDESC(formerr, "FORMERR received", "FORMERR");
 227	SET_RESSTATDESC(othererror, "other errors received", "OtherError");
 228	SET_RESSTATDESC(edns0fail, "EDNS(0) query failures", "EDNS0Fail");
 229	SET_RESSTATDESC(mismatch, "mismatch responses received", "Mismatch");
 230	SET_RESSTATDESC(truncated, "truncated responses received", "Truncated");
 231	SET_RESSTATDESC(lame, "lame delegations received", "Lame");
 232	SET_RESSTATDESC(retry, "query retries", "Retry");
 233	SET_RESSTATDESC(dispabort, "queries aborted due to quota",
 234			"QueryAbort");
 235	SET_RESSTATDESC(dispsockfail, "failures in opening query sockets",
 236			"QuerySockFail");
 237	SET_RESSTATDESC(querytimeout, "query timeouts", "QueryTimeout");
 238	SET_RESSTATDESC(gluefetchv4, "IPv4 NS address fetches", "GlueFetchv4");
 239	SET_RESSTATDESC(gluefetchv6, "IPv6 NS address fetches", "GlueFetchv6");
 240	SET_RESSTATDESC(gluefetchv4fail, "IPv4 NS address fetch failed",
 241			"GlueFetchv4Fail");
 242	SET_RESSTATDESC(gluefetchv6fail, "IPv6 NS address fetch failed",
 243			"GlueFetchv6Fail");
 244	SET_RESSTATDESC(val, "DNSSEC validation attempted", "ValAttempt");
 245	SET_RESSTATDESC(valsuccess, "DNSSEC validation succeeded", "ValOk");
 246	SET_RESSTATDESC(valnegsuccess, "DNSSEC NX validation succeeded",
 247			"ValNegOk");
 248	SET_RESSTATDESC(valfail, "DNSSEC validation failed", "ValFail");
 249	SET_RESSTATDESC(queryrtt0, "queries with RTT < "
 250			DNS_RESOLVER_QRYRTTCLASS0STR "ms",
 251			"QryRTT" DNS_RESOLVER_QRYRTTCLASS0STR);
 252	SET_RESSTATDESC(queryrtt1, "queries with RTT "
 253			DNS_RESOLVER_QRYRTTCLASS0STR "-"
 254			DNS_RESOLVER_QRYRTTCLASS1STR "ms",
 255			"QryRTT" DNS_RESOLVER_QRYRTTCLASS1STR);
 256	SET_RESSTATDESC(queryrtt2, "queries with RTT "
 257			DNS_RESOLVER_QRYRTTCLASS1STR "-"
 258			DNS_RESOLVER_QRYRTTCLASS2STR "ms",
 259			"QryRTT" DNS_RESOLVER_QRYRTTCLASS2STR);
 260	SET_RESSTATDESC(queryrtt3, "queries with RTT "
 261			DNS_RESOLVER_QRYRTTCLASS2STR "-"
 262			DNS_RESOLVER_QRYRTTCLASS3STR "ms",
 263			"QryRTT" DNS_RESOLVER_QRYRTTCLASS3STR);
 264	SET_RESSTATDESC(queryrtt4, "queries with RTT "
 265			DNS_RESOLVER_QRYRTTCLASS3STR "-"
 266			DNS_RESOLVER_QRYRTTCLASS4STR "ms",
 267			"QryRTT" DNS_RESOLVER_QRYRTTCLASS4STR);
 268	SET_RESSTATDESC(queryrtt5, "queries with RTT > "
 269			DNS_RESOLVER_QRYRTTCLASS4STR "ms",
 270			"QryRTT" DNS_RESOLVER_QRYRTTCLASS4STR "+");
 271	INSIST(i == dns_resstatscounter_max);
 272
 273	/* Initialize zone statistics */
 274	for (i = 0; i < dns_zonestatscounter_max; i++)
 275		zonestats_desc[i] = NULL;
 276#ifdef  HAVE_LIBXML2
 277	for (i = 0; i < dns_zonestatscounter_max; i++)
 278		zonestats_xmldesc[i] = NULL;
 279#endif
 280
 281#define SET_ZONESTATDESC(counterid, desc, xmldesc) \
 282	do { \
 283		set_desc(dns_zonestatscounter_ ## counterid, \
 284			 dns_zonestatscounter_max, \
 285			 desc, zonestats_desc, xmldesc, zonestats_xmldesc); \
 286		zonestats_index[i++] = dns_zonestatscounter_ ## counterid; \
 287	} while (0)
 288
 289	i = 0;
 290	SET_ZONESTATDESC(notifyoutv4, "IPv4 notifies sent", "NotifyOutv4");
 291	SET_ZONESTATDESC(notifyoutv6, "IPv6 notifies sent", "NotifyOutv6");
 292	SET_ZONESTATDESC(notifyinv4, "IPv4 notifies received", "NotifyInv4");
 293	SET_ZONESTATDESC(notifyinv6, "IPv6 notifies received", "NotifyInv6");
 294	SET_ZONESTATDESC(notifyrej, "notifies rejected", "NotifyRej");
 295	SET_ZONESTATDESC(soaoutv4, "IPv4 SOA queries sent", "SOAOutv4");
 296	SET_ZONESTATDESC(soaoutv6, "IPv6 SOA queries sent", "SOAOutv6");
 297	SET_ZONESTATDESC(axfrreqv4, "IPv4 AXFR requested", "AXFRReqv4");
 298	SET_ZONESTATDESC(axfrreqv6, "IPv6 AXFR requested", "AXFRReqv6");
 299	SET_ZONESTATDESC(ixfrreqv4, "IPv4 IXFR requested", "IXFRReqv4");
 300	SET_ZONESTATDESC(ixfrreqv6, "IPv6 IXFR requested", "IXFRReqv6");
 301	SET_ZONESTATDESC(xfrsuccess, "transfer requests succeeded","XfrSuccess");
 302	SET_ZONESTATDESC(xfrfail, "transfer requests failed", "XfrFail");
 303	INSIST(i == dns_zonestatscounter_max);
 304
 305	/* Initialize socket statistics */
 306	for (i = 0; i < isc_sockstatscounter_max; i++)
 307		sockstats_desc[i] = NULL;
 308#ifdef  HAVE_LIBXML2
 309	for (i = 0; i < isc_sockstatscounter_max; i++)
 310		sockstats_xmldesc[i] = NULL;
 311#endif
 312
 313#define SET_SOCKSTATDESC(counterid, desc, xmldesc) \
 314	do { \
 315		set_desc(isc_sockstatscounter_ ## counterid, \
 316			 isc_sockstatscounter_max, \
 317			 desc, sockstats_desc, xmldesc, sockstats_xmldesc); \
 318		sockstats_index[i++] = isc_sockstatscounter_ ## counterid; \
 319	} while (0)
 320
 321	i = 0;
 322	SET_SOCKSTATDESC(udp4open, "UDP/IPv4 sockets opened", "UDP4Open");
 323	SET_SOCKSTATDESC(udp6open, "UDP/IPv6 sockets opened", "UDP6Open");
 324	SET_SOCKSTATDESC(tcp4open, "TCP/IPv4 sockets opened", "TCP4Open");
 325	SET_SOCKSTATDESC(tcp6open, "TCP/IPv6 sockets opened", "TCP6Open");
 326	SET_SOCKSTATDESC(unixopen, "Unix domain sockets opened", "UnixOpen");
 327	SET_SOCKSTATDESC(udp4openfail, "UDP/IPv4 socket open failures",
 328			 "UDP4OpenFail");
 329	SET_SOCKSTATDESC(udp6openfail, "UDP/IPv6 socket open failures",
 330			 "UDP6OpenFail");
 331	SET_SOCKSTATDESC(tcp4openfail, "TCP/IPv4 socket open failures",
 332			 "TCP4OpenFail");
 333	SET_SOCKSTATDESC(tcp6openfail, "TCP/IPv6 socket open failures",
 334			 "TCP6OpenFail");
 335	SET_SOCKSTATDESC(unixopenfail, "Unix domain socket open failures",
 336			 "UnixOpenFail");
 337	SET_SOCKSTATDESC(udp4close, "UDP/IPv4 sockets closed", "UDP4Close");
 338	SET_SOCKSTATDESC(udp6close, "UDP/IPv6 sockets closed", "UDP6Close");
 339	SET_SOCKSTATDESC(tcp4close, "TCP/IPv4 sockets closed", "TCP4Close");
 340	SET_SOCKSTATDESC(tcp6close, "TCP/IPv6 sockets closed", "TCP6Close");
 341	SET_SOCKSTATDESC(unixclose, "Unix domain sockets closed", "UnixClose");
 342	SET_SOCKSTATDESC(fdwatchclose, "FDwatch sockets closed",
 343			 "FDWatchClose");
 344	SET_SOCKSTATDESC(udp4bindfail, "UDP/IPv4 socket bind failures",
 345			 "UDP4BindFail");
 346	SET_SOCKSTATDESC(udp6bindfail, "UDP/IPv6 socket bind failures",
 347			 "UDP6BindFail");
 348	SET_SOCKSTATDESC(tcp4bindfail, "TCP/IPv4 socket bind failures",
 349			 "TCP4BindFail");
 350	SET_SOCKSTATDESC(tcp6bindfail, "TCP/IPv6 socket bind failures",
 351			 "TCP6BindFail");
 352	SET_SOCKSTATDESC(unixbindfail, "Unix domain socket bind failures",
 353			 "UnixBindFail");
 354	SET_SOCKSTATDESC(fdwatchbindfail, "FDwatch socket bind failures",
 355			 "FdwatchBindFail");
 356	SET_SOCKSTATDESC(udp4connectfail, "UDP/IPv4 socket connect failures",
 357			 "UDP4ConnFail");
 358	SET_SOCKSTATDESC(udp6connectfail, "UDP/IPv6 socket connect failures",
 359			 "UDP6ConnFail");
 360	SET_SOCKSTATDESC(tcp4connectfail, "TCP/IPv4 socket connect failures",
 361			 "TCP4ConnFail");
 362	SET_SOCKSTATDESC(tcp6connectfail, "TCP/IPv6 socket connect failures",
 363			 "TCP6ConnFail");
 364	SET_SOCKSTATDESC(unixconnectfail, "Unix domain socket connect failures",
 365			 "UnixConnFail");
 366	SET_SOCKSTATDESC(fdwatchconnectfail, "FDwatch socket connect failures",
 367			 "FDwatchConnFail");
 368	SET_SOCKSTATDESC(udp4connect, "UDP/IPv4 connections established",
 369			 "UDP4Conn");
 370	SET_SOCKSTATDESC(udp6connect, "UDP/IPv6 connections established",
 371			 "UDP6Conn");
 372	SET_SOCKSTATDESC(tcp4connect, "TCP/IPv4 connections established",
 373			 "TCP4Conn");
 374	SET_SOCKSTATDESC(tcp6connect, "TCP/IPv6 connections established",
 375			 "TCP6Conn");
 376	SET_SOCKSTATDESC(unixconnect, "Unix domain connections established",
 377			 "UnixConn");
 378	SET_SOCKSTATDESC(fdwatchconnect,
 379			 "FDwatch domain connections established",
 380			 "FDwatchConn");
 381	SET_SOCKSTATDESC(tcp4acceptfail, "TCP/IPv4 connection accept failures",
 382			 "TCP4AcceptFail");
 383	SET_SOCKSTATDESC(tcp6acceptfail, "TCP/IPv6 connection accept failures",
 384			 "TCP6AcceptFail");
 385	SET_SOCKSTATDESC(unixacceptfail,
 386			 "Unix domain connection accept failures",
 387			 "UnixAcceptFail");
 388	SET_SOCKSTATDESC(tcp4accept, "TCP/IPv4 connections accepted",
 389			 "TCP4Accept");
 390	SET_SOCKSTATDESC(tcp6accept, "TCP/IPv6 connections accepted",
 391			 "TCP6Accept");
 392	SET_SOCKSTATDESC(unixaccept, "Unix domain connections accepted",
 393			 "UnixAccept");
 394	SET_SOCKSTATDESC(udp4sendfail, "UDP/IPv4 send errors", "UDP4SendErr");
 395	SET_SOCKSTATDESC(udp6sendfail, "UDP/IPv6 send errors", "UDP6SendErr");
 396	SET_SOCKSTATDESC(tcp4sendfail, "TCP/IPv4 send errors", "TCP4SendErr");
 397	SET_SOCKSTATDESC(tcp6sendfail, "TCP/IPv6 send errors", "TCP6SendErr");
 398	SET_SOCKSTATDESC(unixsendfail, "Unix domain send errors",
 399			 "UnixSendErr");
 400	SET_SOCKSTATDESC(fdwatchsendfail, "FDwatch send errors",
 401			 "FDwatchSendErr");
 402	SET_SOCKSTATDESC(udp4recvfail, "UDP/IPv4 recv errors", "UDP4RecvErr");
 403	SET_SOCKSTATDESC(udp6recvfail, "UDP/IPv6 recv errors", "UDP6RecvErr");
 404	SET_SOCKSTATDESC(tcp4recvfail, "TCP/IPv4 recv errors", "TCP4RecvErr");
 405	SET_SOCKSTATDESC(tcp6recvfail, "TCP/IPv6 recv errors", "TCP6RecvErr");
 406	SET_SOCKSTATDESC(unixrecvfail, "Unix domain recv errors",
 407			 "UnixRecvErr");
 408	SET_SOCKSTATDESC(fdwatchrecvfail, "FDwatch recv errors",
 409			 "FDwatchRecvErr");
 410	INSIST(i == isc_sockstatscounter_max);
 411
 412	/* Sanity check */
 413	for (i = 0; i < dns_nsstatscounter_max; i++)
 414		INSIST(nsstats_desc[i] != NULL);
 415	for (i = 0; i < dns_resstatscounter_max; i++)
 416		INSIST(resstats_desc[i] != NULL);
 417	for (i = 0; i < dns_zonestatscounter_max; i++)
 418		INSIST(zonestats_desc[i] != NULL);
 419	for (i = 0; i < isc_sockstatscounter_max; i++)
 420		INSIST(sockstats_desc[i] != NULL);
 421#ifdef  HAVE_LIBXML2
 422	for (i = 0; i < dns_nsstatscounter_max; i++)
 423		INSIST(nsstats_xmldesc[i] != NULL);
 424	for (i = 0; i < dns_resstatscounter_max; i++)
 425		INSIST(resstats_xmldesc[i] != NULL);
 426	for (i = 0; i < dns_zonestatscounter_max; i++)
 427		INSIST(zonestats_xmldesc[i] != NULL);
 428	for (i = 0; i < isc_sockstatscounter_max; i++)
 429		INSIST(sockstats_xmldesc[i] != NULL);
 430#endif
 431}
 432
 433/*%
 434 * Dump callback functions.
 435 */
 436static void
 437generalstat_dump(isc_statscounter_t counter, isc_uint64_t val, void *arg) {
 438	stats_dumparg_t *dumparg = arg;
 439
 440	REQUIRE(counter < dumparg->ncounters);
 441	dumparg->countervalues[counter] = val;
 442}
 443
 444static isc_result_t
 445dump_counters(isc_stats_t *stats, statsformat_t type, void *arg,
 446	      const char *category, const char **desc, int ncounters,
 447	      int *indices, isc_uint64_t *values, int options)
 448{
 449	int i, index;
 450	isc_uint64_t value;
 451	stats_dumparg_t dumparg;
 452	FILE *fp;
 453#ifdef HAVE_LIBXML2
 454	xmlTextWriterPtr writer;
 455	int xmlrc;
 456#endif
 457
 458#ifndef HAVE_LIBXML2
 459	UNUSED(category);
 460#endif
 461
 462	dumparg.type = type;
 463	dumparg.ncounters = ncounters;
 464	dumparg.counterindices = indices;
 465	dumparg.countervalues = values;
 466
 467	memset(values, 0, sizeof(values[0]) * ncounters);
 468	isc_stats_dump(stats, generalstat_dump, &dumparg, options);
 469
 470	for (i = 0; i < ncounters; i++) {
 471		index = indices[i];
 472		value = values[index];
 473
 474		if (value == 0 && (options & ISC_STATSDUMP_VERBOSE) == 0)
 475			continue;
 476
 477		switch (dumparg.type) {
 478		case statsformat_file:
 479			fp = arg;
 480			fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
 481				value, desc[index]);
 482			break;
 483		case statsformat_xml:
 484#ifdef HAVE_LIBXML2
 485			writer = arg;
 486
 487			if (category != NULL) {
 488				TRY0(xmlTextWriterStartElement(writer,
 489							       ISC_XMLCHAR
 490							       category));
 491				TRY0(xmlTextWriterStartElement(writer,
 492							       ISC_XMLCHAR
 493							       "name"));
 494				TRY0(xmlTextWriterWriteString(writer,
 495							      ISC_XMLCHAR
 496							      desc[index]));
 497				TRY0(xmlTextWriterEndElement(writer)); /* name */
 498
 499				TRY0(xmlTextWriterStartElement(writer,
 500							       ISC_XMLCHAR
 501							       "counter"));
 502			} else {
 503				TRY0(xmlTextWriterStartElement(writer,
 504							       ISC_XMLCHAR
 505							       desc[index]));
 506			}
 507			TRY0(xmlTextWriterWriteFormatString(writer,
 508							    "%"
 509							    ISC_PRINT_QUADFORMAT
 510							    "u", value));
 511			TRY0(xmlTextWriterEndElement(writer)); /* counter */
 512			if (category != NULL)
 513				TRY0(xmlTextWriterEndElement(writer)); /* category */
 514#endif
 515			break;
 516		}
 517	}
 518	return (ISC_R_SUCCESS);
 519#ifdef HAVE_LIBXML2
 520 error:
 521	return (ISC_R_FAILURE);
 522#endif
 523}
 524
 525static void
 526rdtypestat_dump(dns_rdatastatstype_t type, isc_uint64_t val, void *arg) {
 527	char typebuf[64];
 528	const char *typestr;
 529	stats_dumparg_t *dumparg = arg;
 530	FILE *fp;
 531#ifdef HAVE_LIBXML2
 532	xmlTextWriterPtr writer;
 533	int xmlrc;
 534#endif
 535
 536	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_OTHERTYPE)
 537	    == 0) {
 538		dns_rdatatype_format(DNS_RDATASTATSTYPE_BASE(type), typebuf,
 539				     sizeof(typebuf));
 540		typestr = typebuf;
 541	} else
 542		typestr = "Others";
 543
 544	switch (dumparg->type) {
 545	case statsformat_file:
 546		fp = dumparg->arg;
 547		fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n", val, typestr);
 548		break;
 549	case statsformat_xml:
 550#ifdef HAVE_LIBXML2
 551		writer = dumparg->arg;
 552
 553		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "rdtype"));
 554
 555		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
 556		TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR typestr));
 557		TRY0(xmlTextWriterEndElement(writer)); /* name */
 558
 559		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
 560		TRY0(xmlTextWriterWriteFormatString(writer,
 561					       "%" ISC_PRINT_QUADFORMAT "u",
 562					       val));
 563		TRY0(xmlTextWriterEndElement(writer)); /* counter */
 564
 565		TRY0(xmlTextWriterEndElement(writer)); /* rdtype */
 566#endif
 567		break;
 568	}
 569	return;
 570#ifdef HAVE_LIBXML2
 571 error:
 572	dumparg->result = ISC_R_FAILURE;
 573	return;
 574#endif
 575}
 576
 577static void
 578rdatasetstats_dump(dns_rdatastatstype_t type, isc_uint64_t val, void *arg) {
 579	stats_dumparg_t *dumparg = arg;
 580	FILE *fp;
 581	char typebuf[64];
 582	const char *typestr;
 583	isc_boolean_t nxrrset = ISC_FALSE;
 584#ifdef HAVE_LIBXML2
 585	xmlTextWriterPtr writer;
 586	int xmlrc;
 587#endif
 588
 589	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_NXDOMAIN)
 590	    != 0) {
 591		typestr = "NXDOMAIN";
 592	} else if ((DNS_RDATASTATSTYPE_ATTR(type) &
 593		    DNS_RDATASTATSTYPE_ATTR_OTHERTYPE) != 0) {
 594		typestr = "Others";
 595	} else {
 596		dns_rdatatype_format(DNS_RDATASTATSTYPE_BASE(type), typebuf,
 597				     sizeof(typebuf));
 598		typestr = typebuf;
 599	}
 600
 601	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_NXRRSET)
 602	    != 0)
 603		nxrrset = ISC_TRUE;
 604
 605	switch (dumparg->type) {
 606	case statsformat_file:
 607		fp = dumparg->arg;
 608		fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s%s\n", val,
 609			nxrrset ? "!" : "", typestr);
 610		break;
 611	case statsformat_xml:
 612#ifdef HAVE_LIBXML2
 613		writer = dumparg->arg;
 614
 615		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "rrset"));
 616		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
 617		TRY0(xmlTextWriterWriteFormatString(writer, "%s%s",
 618					       nxrrset ? "!" : "", typestr));
 619		TRY0(xmlTextWriterEndElement(writer)); /* name */
 620
 621		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
 622		TRY0(xmlTextWriterWriteFormatString(writer,
 623					       "%" ISC_PRINT_QUADFORMAT "u",
 624					       val));
 625		TRY0(xmlTextWriterEndElement(writer)); /* counter */
 626
 627		TRY0(xmlTextWriterEndElement(writer)); /* rrset */
 628#endif
 629		break;
 630	}
 631	return;
 632#ifdef HAVE_LIBXML2
 633 error:
 634	dumparg->result = ISC_R_FAILURE;
 635#endif
 636
 637}
 638
 639static void
 640opcodestat_dump(dns_opcode_t code, isc_uint64_t val, void *arg) {
 641	FILE *fp;
 642	isc_buffer_t b;
 643	char codebuf[64];
 644	stats_dumparg_t *dumparg = arg;
 645#ifdef HAVE_LIBXML2
 646	xmlTextWriterPtr writer;
 647	int xmlrc;
 648#endif
 649
 650	isc_buffer_init(&b, codebuf, sizeof(codebuf) - 1);
 651	dns_opcode_totext(code, &b);
 652	codebuf[isc_buffer_usedlength(&b)] = '\0';
 653
 654	switch (dumparg->type) {
 655	case statsformat_file:
 656		fp = dumparg->arg;
 657		fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n", val, codebuf);
 658		break;
 659	case statsformat_xml:
 660#ifdef HAVE_LIBXML2
 661		writer = dumparg->arg;
 662
 663		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "opcode"));
 664
 665		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
 666		TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR codebuf));
 667		TRY0(xmlTextWriterEndElement(writer)); /* name */
 668
 669		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
 670		TRY0(xmlTextWriterWriteFormatString(writer,
 671					       "%" ISC_PRINT_QUADFORMAT "u",
 672					       val));
 673		TRY0(xmlTextWriterEndElement(writer)); /* counter */
 674
 675		TRY0(xmlTextWriterEndElement(writer)); /* opcode */
 676#endif
 677		break;
 678	}
 679	return;
 680
 681#ifdef HAVE_LIBXML2
 682 error:
 683	dumparg->result = ISC_R_FAILURE;
 684	return;
 685#endif
 686}
 687
 688#ifdef HAVE_LIBXML2
 689
 690/* XXXMLG below here sucks. */
 691
 692
 693static isc_result_t
 694zone_xmlrender(dns_zone_t *zone, void *arg) {
 695	char buf[1024 + 32];	/* sufficiently large for zone name and class */
 696	dns_rdataclass_t rdclass;
 697	isc_uint32_t serial;
 698	xmlTextWriterPtr writer = arg;
 699	isc_stats_t *zonestats;
 700	isc_uint64_t nsstat_values[dns_nsstatscounter_max];
 701	int xmlrc;
 702	isc_result_t result;
 703
 704	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "zone"));
 705
 706	dns_zone_name(zone, buf, sizeof(buf));
 707	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
 708	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR buf));
 709	TRY0(xmlTextWriterEndElement(writer));
 710
 711	rdclass = dns_zone_getclass(zone);
 712	dns_rdataclass_format(rdclass, buf, sizeof(buf));
 713	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "rdataclass"));
 714	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR buf));
 715	TRY0(xmlTextWriterEndElement(writer));
 716
 717	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "serial"));
 718	if (dns_zone_getserial2(zone, &serial) == ISC_R_SUCCESS)
 719		TRY0(xmlTextWriterWriteFormatString(writer, "%u", serial));
 720	else
 721		TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR "-"));
 722	TRY0(xmlTextWriterEndElement(writer));
 723
 724	zonestats = dns_zone_getrequeststats(zone);
 725	if (zonestats != NULL) {
 726		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
 727		result = dump_counters(zonestats, statsformat_xml, writer, NULL,
 728				      nsstats_xmldesc, dns_nsstatscounter_max,
 729				      nsstats_index, nsstat_values,
 730				      ISC_STATSDUMP_VERBOSE);
 731		if (result != ISC_R_SUCCESS)
 732			goto error;
 733		TRY0(xmlTextWriterEndElement(writer)); /* counters */
 734	}
 735
 736	TRY0(xmlTextWriterEndElement(writer)); /* zone */
 737
 738	return (ISC_R_SUCCESS);
 739 error:
 740	return (ISC_R_FAILURE);
 741}
 742
 743static isc_result_t
 744generatexml(ns_server_t *server, int *buflen, xmlChar **buf) {
 745	char boottime[sizeof "yyyy-mm-ddThh:mm:ssZ"];
 746	char nowstr[sizeof "yyyy-mm-ddThh:mm:ssZ"];
 747	isc_time_t now;
 748	xmlTextWriterPtr writer = NULL;
 749	xmlDocPtr doc = NULL;
 750	int xmlrc;
 751	dns_view_t *view;
 752	stats_dumparg_t dumparg;
 753	dns_stats_t *cachestats;
 754	isc_uint64_t nsstat_values[dns_nsstatscounter_max];
 755	isc_uint64_t resstat_values[dns_resstatscounter_max];
 756	isc_uint64_t zonestat_values[dns_zonestatscounter_max];
 757	isc_uint64_t sockstat_values[isc_sockstatscounter_max];
 758	isc_result_t result;
 759
 760	isc_time_now(&now);
 761	isc_time_formatISO8601(&ns_g_boottime, boottime, sizeof boottime);
 762	isc_time_formatISO8601(&now, nowstr, sizeof nowstr);
 763
 764	writer = xmlNewTextWriterDoc(&doc, 0);
 765	if (writer == NULL)
 766		goto error;
 767	TRY0(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL));
 768	TRY0(xmlTextWriterWritePI(writer, ISC_XMLCHAR "xml-stylesheet",
 769			ISC_XMLCHAR "type=\"text/xsl\" href=\"/bind9.xsl\""));
 770	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "isc"));
 771	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version",
 772					 ISC_XMLCHAR "1.0"));
 773
 774	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "bind"));
 775	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "statistics"));
 776	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version",
 777					 ISC_XMLCHAR "2.2"));
 778
 779	/* Set common fields for statistics dump */
 780	dumparg.type = statsformat_xml;
 781	dumparg.arg = writer;
 782
 783	/*
 784	 * Start by rendering the views we know of here.  For each view we
 785	 * know of, call its rendering function.
 786	 */
 787	view = ISC_LIST_HEAD(server->viewlist);
 788	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "views"));
 789	while (view != NULL) {
 790		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "view"));
 791
 792		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
 793		TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR view->name));
 794		TRY0(xmlTextWriterEndElement(writer));
 795
 796		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "zones"));
 797		result = dns_zt_apply(view->zonetable, ISC_TRUE, zone_xmlrender,
 798				      writer);
 799		if (result != ISC_R_SUCCESS)
 800			goto error;
 801		TRY0(xmlTextWriterEndElement(writer));
 802
 803		if (view->resquerystats != NULL) {
 804			dumparg.result = ISC_R_SUCCESS;
 805			dns_rdatatypestats_dump(view->resquerystats,
 806						rdtypestat_dump, &dumparg, 0);
 807			if (dumparg.result != ISC_R_SUCCESS)
 808				goto error;
 809		}
 810
 811		if (view->resstats != NULL) {
 812			result = dump_counters(view->resstats, statsformat_xml,
 813					       writer, "resstat",
 814					       resstats_xmldesc,
 815					       dns_resstatscounter_max,
 816					       resstats_index, resstat_values,
 817					       ISC_STATSDUMP_VERBOSE);
 818			if (result != ISC_R_SUCCESS)
 819				goto error;
 820		}
 821
 822		cachestats = dns_db_getrrsetstats(view->cachedb);
 823		if (cachestats != NULL) {
 824			TRY0(xmlTextWriterStartElement(writer,
 825						       ISC_XMLCHAR "cache"));
 826			TRY0(xmlTextWriterWriteAttribute(writer,
 827					 ISC_XMLCHAR "name",
 828					 ISC_XMLCHAR
 829					 dns_cache_getname(view->cache)));
 830			dumparg.result = ISC_R_SUCCESS;
 831			dns_rdatasetstats_dump(cachestats, rdatasetstats_dump,
 832					       &dumparg, 0);
 833			if (dumparg.result != ISC_R_SUCCESS)
 834				goto error;
 835			TRY0(xmlTextWriterEndElement(writer)); /* cache */
 836		}
 837
 838		TRY0(xmlTextWriterEndElement(writer)); /* view */
 839
 840		view = ISC_LIST_NEXT(view, link);
 841	}
 842	TRY0(xmlTextWriterEndElement(writer)); /* views */
 843
 844	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "socketmgr"));
 845	isc_socketmgr_renderxml(ns_g_socketmgr, writer);
 846	TRY0(xmlTextWriterEndElement(writer)); /* socketmgr */
 847
 848	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "taskmgr"));
 849	isc_taskmgr_renderxml(ns_g_taskmgr, writer);
 850	TRY0(xmlTextWriterEndElement(writer)); /* taskmgr */
 851
 852	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "server"));
 853	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "boot-time"));
 854	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR boottime));
 855	TRY0(xmlTextWriterEndElement(writer));
 856	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "current-time"));
 857	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR nowstr));
 858	TRY0(xmlTextWriterEndElement(writer));
 859
 860	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "requests"));
 861	dumparg.result = ISC_R_SUCCESS;
 862	dns_opcodestats_dump(server->opcodestats, opcodestat_dump, &dumparg,
 863			     0);
 864	if (dumparg.result != ISC_R_SUCCESS)
 865		goto error;
 866	TRY0(xmlTextWriterEndElement(writer)); /* requests */
 867
 868	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "queries-in"));
 869	dumparg.result = ISC_R_SUCCESS;
 870	dns_rdatatypestats_dump(server->rcvquerystats, rdtypestat_dump,
 871				&dumparg, 0);
 872	if (dumparg.result != ISC_R_SUCCESS)
 873		goto error;
 874	TRY0(xmlTextWriterEndElement(writer)); /* queries-in */
 875
 876	result = dump_counters(server->nsstats, statsformat_xml, writer,
 877			       "nsstat", nsstats_xmldesc,
 878				dns_nsstatscounter_max,
 879				nsstats_index, nsstat_values,
 880				ISC_STATSDUMP_VERBOSE);
 881	if (result != ISC_R_SUCCESS)
 882		goto error;
 883
 884	result = dump_counters(server->zonestats, statsformat_xml, writer,
 885			       "zonestat", zonestats_xmldesc,
 886			       dns_zonestatscounter_max, zonestats_index,
 887			       zonestat_values, ISC_STATSDUMP_VERBOSE);
 888	if (result != ISC_R_SUCCESS)
 889		goto error;
 890
 891	/*
 892	 * Most of the common resolver statistics entries are 0, so we don't
 893	 * use the verbose dump here.
 894	 */
 895	result = dump_counters(server->resolverstats, statsformat_xml, writer,
 896			       "resstat", resstats_xmldesc,
 897			       dns_resstatscounter_max, resstats_index,
 898			       resstat_values, 0);
 899	if (result != ISC_R_SUCCESS)
 900		goto error;
 901
 902	result = dump_counters(server->sockstats, statsformat_xml, writer,
 903			       "sockstat", sockstats_xmldesc,
 904			       isc_sockstatscounter_max, sockstats_index,
 905			       sockstat_values, ISC_STATSDUMP_VERBOSE);
 906	if (result != ISC_R_SUCCESS)
 907		goto error;
 908
 909	TRY0(xmlTextWriterEndElement(writer)); /* server */
 910
 911	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "memory"));
 912	isc_mem_renderxml(writer);
 913	TRY0(xmlTextWriterEndElement(writer)); /* memory */
 914
 915	TRY0(xmlTextWriterEndElement(writer)); /* statistics */
 916	TRY0(xmlTextWriterEndElement(writer)); /* bind */
 917	TRY0(xmlTextWriterEndElement(writer)); /* isc */
 918
 919	TRY0(xmlTextWriterEndDocument(writer));
 920
 921	xmlFreeTextWriter(writer);
 922
 923	xmlDocDumpFormatMemoryEnc(doc, buf, buflen, "UTF-8", 1);
 924	xmlFreeDoc(doc);
 925	return (ISC_R_SUCCESS);
 926
 927 error:
 928	if (writer != NULL)
 929		xmlFreeTextWriter(writer);
 930	if (doc != NULL)
 931		xmlFreeDoc(doc);
 932	return (ISC_R_FAILURE);
 933}
 934
 935static void
 936wrap_xmlfree(isc_buffer_t *buffer, void *arg) {
 937	UNUSED(arg);
 938
 939	xmlFree(isc_buffer_base(buffer));
 940}
 941
 942static isc_result_t
 943render_index(const char *url, const char *querystring, void *arg,
 944	     unsigned int *retcode, const char **retmsg, const char **mimetype,
 945	     isc_buffer_t *b, isc_httpdfree_t **freecb,
 946	     void **freecb_args)
 947{
 948	unsigned char *msg;
 949	int msglen;
 950	ns_server_t *server = arg;
 951	isc_result_t result;
 952
 953	UNUSED(url);
 954	UNUSED(querystring);
 955
 956	result = generatexml(server, &msglen, &msg);
 957
 958	if (result == ISC_R_SUCCESS) {
 959		*retcode = 200;
 960		*retmsg = "OK";
 961		*mimetype = "text/xml";
 962		isc_buffer_reinit(b, msg, msglen);
 963		isc_buffer_add(b, msglen);
 964		*freecb = wrap_xmlfree;
 965		*freecb_args = NULL;
 966	}
 967
 968	return (result);
 969}
 970
 971#endif	/* HAVE_LIBXML2 */
 972
 973static isc_result_t
 974render_xsl(const char *url, const char *querystring, void *args,
 975	   unsigned int *retcode, const char **retmsg, const char **mimetype,
 976	   isc_buffer_t *b, isc_httpdfree_t **freecb,
 977	   void **freecb_args)
 978{
 979	UNUSED(url);
 980	UNUSED(querystring);
 981	UNUSED(args);
 982
 983	*retcode = 200;
 984	*retmsg = "OK";
 985	*mimetype = "text/xslt+xml";
 986	isc_buffer_reinit(b, xslmsg, strlen(xslmsg));
 987	isc_buffer_add(b, strlen(xslmsg));
 988	*freecb = NULL;
 989	*freecb_args = NULL;
 990
 991	return (ISC_R_SUCCESS);
 992}
 993
 994static void
 995shutdown_listener(ns_statschannel_t *listener) {
 996	char socktext[ISC_SOCKADDR_FORMATSIZE];
 997	isc_sockaddr_format(&listener->address, socktext, sizeof(socktext));
 998	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,NS_LOGMODULE_SERVER,
 999		      ISC_LOG_NOTICE, "stopping statistics channel on %s",
1000		      socktext);
1001
1002	isc_httpdmgr_shutdown(&listener->httpdmgr);
1003}
1004
1005static isc_boolean_t
1006client_ok(const isc_sockaddr_t *fromaddr, void *arg) {
1007	ns_statschannel_t *listener = arg;
1008	isc_netaddr_t netaddr;
1009	char socktext[ISC_SOCKADDR_FORMATSIZE];
1010	int match;
1011
1012	REQUIRE(listener != NULL);
1013
1014	isc_netaddr_fromsockaddr(&netaddr, fromaddr);
1015
1016	LOCK(&listener->lock);
1017	if (dns_acl_match(&netaddr, NULL, listener->acl, &ns_g_server->aclenv,
1018			  &match, NULL) == ISC_R_SUCCESS && match > 0) {
1019		UNLOCK(&listener->lock);
1020		return (ISC_TRUE);
1021	}
1022	UNLOCK(&listener->lock);
1023
1024	isc_sockaddr_format(fromaddr, socktext, sizeof(socktext));
1025	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1026		      NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
1027		      "rejected statistics connection from %s", socktext);
1028
1029	return (ISC_FALSE);
1030}
1031
1032static void
1033destroy_listener(void *arg) {
1034	ns_statschannel_t *listener = arg;
1035
1036	REQUIRE(listener != NULL);
1037	REQUIRE(!ISC_LINK_LINKED(listener, link));
1038
1039	/* We don't have to acquire the lock here since it's already unlinked */
1040	dns_acl_detach(&listener->acl);
1041
1042	DESTROYLOCK(&listener->lock);
1043	isc_mem_putanddetach(&listener->mctx, listener, sizeof(*listener));
1044}
1045
1046static isc_result_t
1047add_listener(ns_server_t *server, ns_statschannel_t **listenerp,
1048	     const cfg_obj_t *listen_params, const cfg_obj_t *config,
1049	     isc_sockaddr_t *addr, cfg_aclconfctx_t *aclconfctx,
1050	     const char *socktext)
1051{
1052	isc_result_t result;
1053	ns_statschannel_t *listener;
1054	isc_task_t *task = NULL;
1055	isc_socket_t *sock = NULL;
1056	const cfg_obj_t *allow;
1057	dns_acl_t *new_acl = NULL;
1058
1059	listener = isc_mem_get(server->mctx, sizeof(*listener));
1060	if (listener == NULL)
1061		return (ISC_R_NOMEMORY);
1062
1063	listener->httpdmgr = NULL;
1064	listener->address = *addr;
1065	listener->acl = NULL;
1066	listener->mctx = NULL;
1067	ISC_LINK_INIT(listener, link);
1068
1069	result = isc_mutex_init(&listener->lock);
1070	if (result != ISC_R_SUCCESS) {
1071		isc_mem_put(server->mctx, listener, sizeof(*listener));
1072		return (ISC_R_FAILURE);
1073	}
1074
1075	isc_mem_attach(server->mctx, &listener->mctx);
1076
1077	allow = cfg_tuple_get(listen_params, "allow");
1078	if (allow != NULL && cfg_obj_islist(allow)) {
1079		result = cfg_acl_fromconfig(allow, config, ns_g_lctx,
1080					    aclconfctx, listener->mctx, 0,
1081					    &new_acl);
1082	} else
1083		result = dns_acl_any(listener->mctx, &new_acl);
1084	if (result != ISC_R_SUCCESS)
1085		goto cleanup;
1086	dns_acl_attach(new_acl, &listener->acl);
1087	dns_acl_detach(&new_acl);
1088
1089	result = isc_task_create(ns_g_taskmgr, 0, &task);
1090	if (result != ISC_R_SUCCESS)
1091		goto cleanup;
1092	isc_task_setname(task, "statchannel", NULL);
1093
1094	result = isc_socket_create(ns_g_socketmgr, isc_sockaddr_pf(addr),
1095				   isc_sockettype_tcp, &sock);
1096	if (result != ISC_R_SUCCESS)
1097		goto cleanup;
1098	isc_socket_setname(sock, "statchannel", NULL);
1099
1100#ifndef ISC_ALLOW_MAPPED
1101	isc_socket_ipv6only(sock, ISC_TRUE);
1102#endif
1103
1104	result = isc_socket_bind(sock, addr, ISC_SOCKET_REUSEADDRESS);
1105	if (result != ISC_R_SUCCESS)
1106		goto cleanup;
1107
1108	result = isc_httpdmgr_create(server->mctx, sock, task, client_ok,
1109				     destroy_listener, listener, ns_g_timermgr,
1110				     &listener->httpdmgr);
1111	if (result != ISC_R_SUCCESS)
1112		goto cleanup;
1113
1114#ifdef HAVE_LIBXML2
1115	isc_httpdmgr_addurl(listener->httpdmgr, "/", render_index, server);
1116#endif
1117	isc_httpdmgr_addurl(listener->httpdmgr, "/bind9.xsl", render_xsl,
1118			    server);
1119
1120	*listenerp = listener;
1121	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1122		      NS_LOGMODULE_SERVER, ISC_LOG_NOTICE,
1123		      "statistics channel listening on %s", socktext);
1124
1125cleanup:
1126	if (result != ISC_R_SUCCESS) {
1127		if (listener->acl != NULL)
1128			dns_acl_detach(&listener->acl);
1129		DESTROYLOCK(&listener->lock);
1130		isc_mem_putanddetach(&listener->mctx, listener,
1131				     sizeof(*listener));
1132	}
1133	if (task != NULL)
1134		isc_task_detach(&task);
1135	if (sock != NULL)
1136		isc_socket_detach(&sock);
1137
1138	return (result);
1139}
1140
1141static void
1142update_listener(ns_server_t *server, ns_statschannel_t **listenerp,
1143		const cfg_obj_t *listen_params, const cfg_obj_t *config,
1144		isc_sockaddr_t *addr, cfg_aclconfctx_t *aclconfctx,
1145		const char *socktext)
1146{
1147	ns_statschannel_t *listener;
1148	const cfg_obj_t *allow = NULL;
1149	dns_acl_t *new_acl = NULL;
1150	isc_result_t result = ISC_R_SUCCESS;
1151
1152	for (listener = ISC_LIST_HEAD(server->statschannels);
1153	     listener != NULL;
1154	     listener = ISC_LIST_NEXT(listener, link))
1155		if (isc_sockaddr_equal(addr, &listener->address))
1156			break;
1157
1158	if (listener == NULL) {
1159		*listenerp = NULL;
1160		return;
1161	}
1162
1163	/*
1164	 * Now, keep the old access list unless a new one can be made.
1165	 */
1166	allow = cfg_tuple_get(listen_params, "allow");
1167	if (allow != NULL && cfg_obj_islist(allow)) {
1168		result = cfg_acl_fromconfig(allow, config, ns_g_lctx,
1169					    aclconfctx, listener->mctx, 0,
1170					    &new_acl);
1171	} else
1172		result = dns_acl_any(listener->mctx, &new_acl);
1173
1174	if (result == ISC_R_SUCCESS) {
1175		LOCK(&listener->lock);
1176
1177		dns_acl_detach(&listener->acl);
1178		dns_acl_attach(new_acl, &listener->acl);
1179		dns_acl_detach(&new_acl);
1180
1181		UNLOCK(&listener->lock);
1182	} else {
1183		cfg_obj_log(listen_params, ns_g_lctx, ISC_LOG_WARNING,
1184			    "couldn't install new acl for "
1185			    "statistics channel %s: %s",
1186			    socktext, isc_result_totext(result));
1187	}
1188
1189	*listenerp = listener;
1190}
1191
1192isc_result_t
1193ns_statschannels_configure(ns_server_t *server, const cfg_obj_t *config,
1194			 cfg_aclconfctx_t *aclconfctx)
1195{
1196	ns_statschannel_t *listener, *listener_next;
1197	ns_statschannellist_t new_listeners;
1198	const cfg_obj_t *statschannellist = NULL;
1199	const cfg_listelt_t *element, *element2;
1200	char socktext[ISC_SOCKADDR_FORMATSIZE];
1201
1202	RUNTIME_CHECK(isc_once_do(&once, init_desc) == ISC_R_SUCCESS);
1203
1204	ISC_LIST_INIT(new_listeners);
1205
1206	/*
1207	 * Get the list of named.conf 'statistics-channels' statements.
1208	 */
1209	(void)cfg_map_get(config, "statistics-channels", &statschannellist);
1210
1211	/*
1212	 * Run through the new address/port list, noting sockets that are
1213	 * already being listened on and moving them to the new list.
1214	 *
1215	 * Identifying duplicate addr/port combinations is left to either
1216	 * the underlying config code, or to the bind attempt getting an
1217	 * address-in-use error.
1218	 */
1219	if (statschannellist != NULL) {
1220#ifndef HAVE_LIBXML2
1221		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1222			      NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
1223			      "statistics-channels specified but not effective "
1224			      "due to missing XML library");
1225#endif
1226
1227		for (element = cfg_list_first(statschannellist);
1228		     element != NULL;
1229		     element = cfg_list_next(element)) {
1230			const cfg_obj_t *statschannel;
1231			const cfg_obj_t *listenercfg = NULL;
1232
1233			statschannel = cfg_listelt_value(element);
1234			(void)cfg_map_get(statschannel, "inet",
1235					  &listenercfg);
1236			if (listenercfg == NULL)
1237				continue;
1238
1239			for (element2 = cfg_list_first(listenercfg);
1240			     element2 != NULL;
1241			     element2 = cfg_list_next(element2)) {
1242				const cfg_obj_t *listen_params;
1243				const cfg_obj_t *obj;
1244				isc_sockaddr_t addr;
1245
1246				listen_params = cfg_listelt_value(element2);
1247
1248				obj = cfg_tuple_get(listen_params, "address");
1249				addr = *cfg_obj_assockaddr(obj);
1250				if (isc_sockaddr_getport(&addr) == 0)
1251					isc_sockaddr_setport(&addr, NS_STATSCHANNEL_HTTPPORT);
1252
1253				isc_sockaddr_format(&addr, socktext,
1254						    sizeof(socktext));
1255
1256				isc_log_write(ns_g_lctx,
1257					      NS_LOGCATEGORY_GENERAL,
1258					      NS_LOGMODULE_SERVER,
1259					      ISC_LOG_DEBUG(9),
1260					      "processing statistics "
1261					      "channel %s",
1262					      socktext);
1263
1264				update_listener(server, &listener,
1265						listen_params, config, &addr,
1266						aclconfctx, socktext);
1267
1268				if (listener != NULL) {
1269					/*
1270					 * Remove the listener from the old
1271					 * list, so it won't be shut down.
1272					 */
1273					ISC_LIST_UNLINK(server->statschannels,
1274							listener, link);
1275				} else {
1276					/*
1277					 * This is a new listener.
1278					 */
1279					isc_result_t r;
1280
1281					r = add_listener(server, &listener,
1282							 listen_params, config,
1283							 &addr, aclconfctx,
1284							 socktext);
1285					if (r != ISC_R_SUCCESS) {
1286						cfg_obj_log(listen_params,
1287							    ns_g_lctx,
1288							    ISC_LOG_WARNING,
1289							    "couldn't allocate "
1290							    "statistics channel"
1291							    " %s: %s",
1292							    socktext,
1293							    isc_result_totext(r));
1294					}
1295				}
1296
1297				if (listener != NULL)
1298					ISC_LIST_APPEND(new_listeners, listener,
1299							link);
1300			}
1301		}
1302	}
1303
1304	for (listener = ISC_LIST_HEAD(server->statschannels);
1305	     listener != NULL;
1306	     listener = listener_next) {
1307		listener_next = ISC_LIST_NEXT(listener, link);
1308		ISC_LIST_UNLINK(server->statschannels, listener, link);
1309		shutdown_listener(listener);
1310	}
1311
1312	ISC_LIST_APPENDLIST(server->statschannels, new_listeners, link);
1313	return (ISC_R_SUCCESS);
1314}
1315
1316void
1317ns_statschannels_shutdown(ns_server_t *server) {
1318	ns_statschannel_t *listener;
1319
1320	while ((listener = ISC_LIST_HEAD(server->statschannels)) != NULL) {
1321		ISC_LIST_UNLINK(server->statschannels, listener, link);
1322		shutdown_listener(listener);
1323	}
1324}
1325
1326isc_result_t
1327ns_stats_dump(ns_server_t *server, FILE *fp) {
1328	isc_stdtime_t now;
1329	isc_result_t result;
1330	dns_view_t *view;
1331	dns_zone_t *zone, *next;
1332	stats_dumparg_t dumparg;
1333	isc_uint64_t nsstat_values[dns_nsstatscounter_max];
1334	isc_uint64_t resstat_values[dns_resstatscounter_max];
1335	isc_uint64_t zonestat_values[dns_zonestatscounter_max];
1336	isc_uint64_t sockstat_values[isc_sockstatscounter_max];
1337
1338	RUNTIME_CHECK(isc_once_do(&once, init_desc) == ISC_R_SUCCESS);
1339
1340	/* Set common fields */
1341	dumparg.type = statsformat_file;
1342	dumparg.arg = fp;
1343
1344	isc_stdtime_get(&now);
1345	fprintf(fp, "+++ Statistics Dump +++ (%lu)\n", (unsigned long)now);
1346
1347	fprintf(fp, "++ Incoming Requests ++\n");
1348	dns_opcodestats_dump(server->opcodestats, opcodestat_dump, &dumparg, 0);
1349
1350	fprintf(fp, "++ Incoming Queries ++\n");
1351	dns_rdatatypestats_dump(server->rcvquerystats, rdtypestat_dump,
1352				&dumparg, 0);
1353
1354	fprintf(fp, "++ Outgoing Queries ++\n");
1355	for (view = ISC_LIST_HEAD(server->viewlist);
1356	     view != NULL;
1357	     view = ISC_LIST_NEXT(view, link)) {
1358		if (view->resquerystats == NULL)
1359			continue;
1360		if (strcmp(view->name, "_default") == 0)
1361			fprintf(fp, "[View: default]\n");
1362		else
1363			fprintf(fp, "[View: %s]\n", view->name);
1364		dns_rdatatypestats_dump(view->resquerystats, rdtypestat_dump,
1365					&dumparg, 0);
1366	}
1367
1368	fprintf(fp, "++ Name Server Statistics ++\n");
1369	(void) dump_counters(server->nsstats, statsformat_file, fp, NULL,
1370			     nsstats_desc, dns_nsstatscounter_max,
1371			     nsstats_index, nsstat_values, 0);
1372
1373	fprintf(fp, "++ Zone Maintenance Statistics ++\n");
1374	(void) dump_counters(server->zonestats, statsformat_file, fp, NULL,
1375			     zonestats_desc, dns_zonestatscounter_max,
1376			     zonestats_index, zonestat_values, 0);
1377
1378	fprintf(fp, "++ Resolver Statistics ++\n");
1379	fprintf(fp, "[Common]\n");
1380	(void) dump_counters(server->resolverstats, statsformat_file, fp, NULL,
1381			     resstats_desc, dns_resstatscounter_max,
1382			     resstats_index, resstat_values, 0);
1383	for (view = ISC_LIST_HEAD(server->viewlist);
1384	     view != NULL;
1385	     view = ISC_LIST_NEXT(view, link)) {
1386		if (view->resstats == NULL)
1387			continue;
1388		if (strcmp(view->name, "_default") == 0)
1389			fprintf(fp, "[View: default]\n");
1390		else
1391			fprintf(fp, "[View: %s]\n", view->name);
1392		(void) dump_counters(view->resstats, statsformat_file, fp, NULL,
1393				     resstats_desc, dns_resstatscounter_max,
1394				     resstats_index, resstat_values, 0);
1395	}
1396
1397	fprintf(fp, "++ Cache DB RRsets ++\n");
1398	for (view = ISC_LIST_HEAD(server->viewlist);
1399	     view != NULL;
1400	     view = ISC_LIST_NEXT(view, link)) {
1401		dns_stats_t *cachestats;
1402
1403		cachestats = dns_db_getrrsetstats(view->cachedb);
1404		if (cachestats == NULL)
1405			continue;
1406		if (strcmp(view->name, "_default") == 0)
1407			fprintf(fp, "[View: default]\n");
1408		else
1409			fprintf(fp, "[View: %s (Cache: %s)]\n", view->name,
1410				dns_cache_getname(view->cache));
1411		if (dns_view_iscacheshared(view)) {
1412			/*
1413			 * Avoid dumping redundant statistics when the cache is
1414			 * shared.
1415			 */
1416			continue;
1417		}
1418		dns_rdatasetstats_dump(cachestats, rdatasetstats_dump, &dumparg,
1419				       0);
1420	}
1421
1422	fprintf(fp, "++ Socket I/O Statistics ++\n");
1423	(void) dump_counters(server->sockstats, statsformat_file, fp, NULL,
1424			     sockstats_desc, isc_sockstatscounter_max,
1425			     sockstats_index, sockstat_values, 0);
1426
1427	fprintf(fp, "++ Per Zone Query Statistics ++\n");
1428	zone = NULL;
1429	for (result = dns_zone_first(server->zonemgr, &zone);
1430	     result == ISC_R_SUCCESS;
1431	     next = NULL, result = dns_zone_next(zone, &next), zone = next)
1432	{
1433		isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
1434		if (zonestats != NULL) {
1435			char zonename[DNS_NAME_FORMATSIZE];
1436
1437			dns_name_format(dns_zone_getorigin(zone),
1438					zonename, sizeof(zonename));
1439			view = dns_zone_getview(zone);
1440
1441			fprintf(fp, "[%s", zonename);
1442			if (strcmp(view->name, "_default") != 0)
1443				fprintf(fp, " (view: %s)", view->name);
1444			fprintf(fp, "]\n");
1445
1446			(void) dump_counters(zonestats, statsformat_file, fp,
1447					     NULL, nsstats_desc,
1448					     dns_nsstatscounter_max,
1449					     nsstats_index, nsstat_values, 0);
1450		}
1451	}
1452
1453	fprintf(fp, "--- Statistics Dump --- (%lu)\n", (unsigned long)now);
1454
1455	return (ISC_R_SUCCESS);	/* this function currently always succeeds */
1456}