2 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: xfrout.c,v 1.131.26.4 2009/01/29 22:40:34 jinmei Exp $ */
22 #include <isc/formatcheck.h>
24 #include <isc/timer.h>
25 #include <isc/print.h>
26 #include <isc/stats.h>
30 #include <dns/dbiterator.h>
34 #include <dns/fixedname.h>
35 #include <dns/journal.h>
36 #include <dns/message.h>
38 #include <dns/rdataclass.h>
39 #include <dns/rdatalist.h>
40 #include <dns/rdataset.h>
41 #include <dns/rdatasetiter.h>
42 #include <dns/result.h>
44 #include <dns/stats.h>
45 #include <dns/timer.h>
51 #include <named/client.h>
52 #include <named/log.h>
53 #include <named/server.h>
54 #include <named/xfrout.h>
58 * Outgoing AXFR and IXFR.
66 #define XFROUT_COMMON_LOGARGS \
67 ns_g_lctx, DNS_LOGCATEGORY_XFER_OUT, NS_LOGMODULE_XFER_OUT
69 #define XFROUT_PROTOCOL_LOGARGS \
70 XFROUT_COMMON_LOGARGS, ISC_LOG_INFO
72 #define XFROUT_DEBUG_LOGARGS(n) \
73 XFROUT_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
75 #define XFROUT_RR_LOGARGS \
76 XFROUT_COMMON_LOGARGS, XFROUT_RR_LOGLEVEL
78 #define XFROUT_RR_LOGLEVEL ISC_LOG_DEBUG(8)
81 * Fail unconditionally and log as a client error.
82 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
83 * from complaining about "end-of-loop code not reached".
85 #define FAILC(code, msg) \
88 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT, \
89 NS_LOGMODULE_XFER_OUT, ISC_LOG_INFO, \
90 "bad zone transfer request: %s (%s)", \
91 msg, isc_result_totext(code)); \
92 if (result != ISC_R_SUCCESS) goto failure; \
95 #define FAILQ(code, msg, question, rdclass) \
97 char _buf1[DNS_NAME_FORMATSIZE]; \
98 char _buf2[DNS_RDATACLASS_FORMATSIZE]; \
100 dns_name_format(question, _buf1, sizeof(_buf1)); \
101 dns_rdataclass_format(rdclass, _buf2, sizeof(_buf2)); \
102 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT, \
103 NS_LOGMODULE_XFER_OUT, ISC_LOG_INFO, \
104 "bad zone transfer request: '%s/%s': %s (%s)", \
105 _buf1, _buf2, msg, isc_result_totext(code)); \
106 if (result != ISC_R_SUCCESS) goto failure; \
110 do { result = (op); \
111 if (result != ISC_R_SUCCESS) goto failure; \
114 /**************************************************************************/
116 * A db_rr_iterator_t is an iterator that iterates over an entire database,
117 * returning one RR at a time, in some arbitrary order.
120 typedef struct db_rr_iterator db_rr_iterator_t;
122 /*% db_rr_iterator structure */
123 struct db_rr_iterator {
126 dns_dbiterator_t *dbit;
127 dns_dbversion_t *ver;
130 dns_fixedname_t fixedname;
131 dns_rdatasetiter_t *rdatasetit;
132 dns_rdataset_t rdataset;
137 db_rr_iterator_init(db_rr_iterator_t *it, dns_db_t *db, dns_dbversion_t *ver,
141 db_rr_iterator_first(db_rr_iterator_t *it);
144 db_rr_iterator_next(db_rr_iterator_t *it);
147 db_rr_iterator_current(db_rr_iterator_t *it, dns_name_t **name,
148 isc_uint32_t *ttl, dns_rdata_t **rdata);
151 db_rr_iterator_destroy(db_rr_iterator_t *it);
154 inc_stats(dns_zone_t *zone, isc_statscounter_t counter) {
155 isc_stats_increment(ns_g_server->nsstats, counter);
157 isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
158 if (zonestats != NULL)
159 isc_stats_increment(zonestats, counter);
164 db_rr_iterator_init(db_rr_iterator_t *it, dns_db_t *db, dns_dbversion_t *ver,
173 result = dns_db_createiterator(it->db, 0, &it->dbit);
174 if (result != ISC_R_SUCCESS)
176 it->rdatasetit = NULL;
177 dns_rdata_init(&it->rdata);
178 dns_rdataset_init(&it->rdataset);
179 dns_fixedname_init(&it->fixedname);
180 INSIST(! dns_rdataset_isassociated(&it->rdataset));
181 it->result = ISC_R_SUCCESS;
186 db_rr_iterator_first(db_rr_iterator_t *it) {
187 it->result = dns_dbiterator_first(it->dbit);
189 * The top node may be empty when out of zone glue exists.
190 * Walk the tree to find the first node with data.
192 while (it->result == ISC_R_SUCCESS) {
193 it->result = dns_dbiterator_current(it->dbit, &it->node,
194 dns_fixedname_name(&it->fixedname));
195 if (it->result != ISC_R_SUCCESS)
198 it->result = dns_db_allrdatasets(it->db, it->node,
201 if (it->result != ISC_R_SUCCESS)
204 it->result = dns_rdatasetiter_first(it->rdatasetit);
205 if (it->result != ISC_R_SUCCESS) {
207 * This node is empty. Try next node.
209 dns_rdatasetiter_destroy(&it->rdatasetit);
210 dns_db_detachnode(it->db, &it->node);
211 it->result = dns_dbiterator_next(it->dbit);
214 dns_rdatasetiter_current(it->rdatasetit, &it->rdataset);
215 it->rdataset.attributes |= DNS_RDATASETATTR_LOADORDER;
216 it->result = dns_rdataset_first(&it->rdataset);
224 db_rr_iterator_next(db_rr_iterator_t *it) {
225 if (it->result != ISC_R_SUCCESS)
228 INSIST(it->dbit != NULL);
229 INSIST(it->node != NULL);
230 INSIST(it->rdatasetit != NULL);
232 it->result = dns_rdataset_next(&it->rdataset);
233 if (it->result == ISC_R_NOMORE) {
234 dns_rdataset_disassociate(&it->rdataset);
235 it->result = dns_rdatasetiter_next(it->rdatasetit);
237 * The while loop body is executed more than once
238 * only when an empty dbnode needs to be skipped.
240 while (it->result == ISC_R_NOMORE) {
241 dns_rdatasetiter_destroy(&it->rdatasetit);
242 dns_db_detachnode(it->db, &it->node);
243 it->result = dns_dbiterator_next(it->dbit);
244 if (it->result == ISC_R_NOMORE) {
245 /* We are at the end of the entire database. */
248 if (it->result != ISC_R_SUCCESS)
250 it->result = dns_dbiterator_current(it->dbit,
252 dns_fixedname_name(&it->fixedname));
253 if (it->result != ISC_R_SUCCESS)
255 it->result = dns_db_allrdatasets(it->db, it->node,
258 if (it->result != ISC_R_SUCCESS)
260 it->result = dns_rdatasetiter_first(it->rdatasetit);
262 if (it->result != ISC_R_SUCCESS)
264 dns_rdatasetiter_current(it->rdatasetit, &it->rdataset);
265 it->rdataset.attributes |= DNS_RDATASETATTR_LOADORDER;
266 it->result = dns_rdataset_first(&it->rdataset);
267 if (it->result != ISC_R_SUCCESS)
274 db_rr_iterator_pause(db_rr_iterator_t *it) {
275 RUNTIME_CHECK(dns_dbiterator_pause(it->dbit) == ISC_R_SUCCESS);
279 db_rr_iterator_destroy(db_rr_iterator_t *it) {
280 if (dns_rdataset_isassociated(&it->rdataset))
281 dns_rdataset_disassociate(&it->rdataset);
282 if (it->rdatasetit != NULL)
283 dns_rdatasetiter_destroy(&it->rdatasetit);
284 if (it->node != NULL)
285 dns_db_detachnode(it->db, &it->node);
286 dns_dbiterator_destroy(&it->dbit);
290 db_rr_iterator_current(db_rr_iterator_t *it, dns_name_t **name,
291 isc_uint32_t *ttl, dns_rdata_t **rdata)
293 REQUIRE(name != NULL && *name == NULL);
294 REQUIRE(it->result == ISC_R_SUCCESS);
295 *name = dns_fixedname_name(&it->fixedname);
296 *ttl = it->rdataset.ttl;
297 dns_rdata_reset(&it->rdata);
298 dns_rdataset_current(&it->rdataset, &it->rdata);
302 /**************************************************************************/
304 /*% Log an RR (for debugging) */
307 log_rr(dns_name_t *name, dns_rdata_t *rdata, isc_uint32_t ttl) {
313 dns_rdata_t rd = DNS_RDATA_INIT;
315 rdl.type = rdata->type;
316 rdl.rdclass = rdata->rdclass;
318 if (rdata->type == dns_rdatatype_sig ||
319 rdata->type == dns_rdatatype_rrsig)
320 rdl.covers = dns_rdata_covers(rdata);
322 rdl.covers = dns_rdatatype_none;
323 ISC_LIST_INIT(rdl.rdata);
324 ISC_LINK_INIT(&rdl, link);
325 dns_rdataset_init(&rds);
327 dns_rdata_clone(rdata, &rd);
328 ISC_LIST_APPEND(rdl.rdata, &rd, link);
329 RUNTIME_CHECK(dns_rdatalist_tordataset(&rdl, &rds) == ISC_R_SUCCESS);
331 isc_buffer_init(&buf, mem, sizeof(mem));
332 result = dns_rdataset_totext(&rds, name,
333 ISC_FALSE, ISC_FALSE, &buf);
336 * We could use xfrout_log(), but that would produce
337 * very long lines with a repetitive prefix.
339 if (result == ISC_R_SUCCESS) {
341 * Get rid of final newline.
343 INSIST(buf.used >= 1 &&
344 ((char *) buf.base)[buf.used - 1] == '\n');
347 isc_log_write(XFROUT_RR_LOGARGS, "%.*s",
348 (int)isc_buffer_usedlength(&buf),
349 (char *)isc_buffer_base(&buf));
351 isc_log_write(XFROUT_RR_LOGARGS, "<RR too large to print>");
355 /**************************************************************************/
357 * An 'rrstream_t' is a polymorphic iterator that returns
358 * a stream of resource records. There are multiple implementations,
359 * e.g. for generating AXFR and IXFR records streams.
362 typedef struct rrstream_methods rrstream_methods_t;
364 typedef struct rrstream {
366 rrstream_methods_t *methods;
369 struct rrstream_methods {
370 isc_result_t (*first)(rrstream_t *);
371 isc_result_t (*next)(rrstream_t *);
372 void (*current)(rrstream_t *,
376 void (*pause)(rrstream_t *);
377 void (*destroy)(rrstream_t **);
381 rrstream_noop_pause(rrstream_t *rs) {
385 /**************************************************************************/
387 * An 'ixfr_rrstream_t' is an 'rrstream_t' that returns
388 * an IXFR-like RR stream from a journal file.
390 * The SOA at the beginning of each sequence of additions
391 * or deletions are included in the stream, but the extra
392 * SOAs at the beginning and end of the entire transfer are
396 typedef struct ixfr_rrstream {
398 dns_journal_t *journal;
401 /* Forward declarations. */
403 ixfr_rrstream_destroy(rrstream_t **sp);
405 static rrstream_methods_t ixfr_rrstream_methods;
408 * Returns: anything dns_journal_open() or dns_journal_iter_init()
413 ixfr_rrstream_create(isc_mem_t *mctx,
414 const char *journal_filename,
415 isc_uint32_t begin_serial,
416 isc_uint32_t end_serial,
422 INSIST(sp != NULL && *sp == NULL);
424 s = isc_mem_get(mctx, sizeof(*s));
426 return (ISC_R_NOMEMORY);
427 s->common.mctx = mctx;
428 s->common.methods = &ixfr_rrstream_methods;
431 CHECK(dns_journal_open(mctx, journal_filename,
432 ISC_FALSE, &s->journal));
433 CHECK(dns_journal_iter_init(s->journal, begin_serial, end_serial));
435 *sp = (rrstream_t *) s;
436 return (ISC_R_SUCCESS);
439 ixfr_rrstream_destroy((rrstream_t **) (void *)&s);
444 ixfr_rrstream_first(rrstream_t *rs) {
445 ixfr_rrstream_t *s = (ixfr_rrstream_t *) rs;
446 return (dns_journal_first_rr(s->journal));
450 ixfr_rrstream_next(rrstream_t *rs) {
451 ixfr_rrstream_t *s = (ixfr_rrstream_t *) rs;
452 return (dns_journal_next_rr(s->journal));
456 ixfr_rrstream_current(rrstream_t *rs,
457 dns_name_t **name, isc_uint32_t *ttl,
460 ixfr_rrstream_t *s = (ixfr_rrstream_t *) rs;
461 dns_journal_current_rr(s->journal, name, ttl, rdata);
465 ixfr_rrstream_destroy(rrstream_t **rsp) {
466 ixfr_rrstream_t *s = (ixfr_rrstream_t *) *rsp;
468 dns_journal_destroy(&s->journal);
469 isc_mem_put(s->common.mctx, s, sizeof(*s));
472 static rrstream_methods_t ixfr_rrstream_methods = {
475 ixfr_rrstream_current,
477 ixfr_rrstream_destroy
480 /**************************************************************************/
482 * An 'axfr_rrstream_t' is an 'rrstream_t' that returns
483 * an AXFR-like RR stream from a database.
485 * The SOAs at the beginning and end of the transfer are
486 * not included in the stream.
489 typedef struct axfr_rrstream {
492 isc_boolean_t it_valid;
496 * Forward declarations.
499 axfr_rrstream_destroy(rrstream_t **rsp);
501 static rrstream_methods_t axfr_rrstream_methods;
504 axfr_rrstream_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *ver,
510 INSIST(sp != NULL && *sp == NULL);
512 s = isc_mem_get(mctx, sizeof(*s));
514 return (ISC_R_NOMEMORY);
515 s->common.mctx = mctx;
516 s->common.methods = &axfr_rrstream_methods;
517 s->it_valid = ISC_FALSE;
519 CHECK(db_rr_iterator_init(&s->it, db, ver, 0));
520 s->it_valid = ISC_TRUE;
522 *sp = (rrstream_t *) s;
523 return (ISC_R_SUCCESS);
526 axfr_rrstream_destroy((rrstream_t **) (void *)&s);
531 axfr_rrstream_first(rrstream_t *rs) {
532 axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
534 result = db_rr_iterator_first(&s->it);
535 if (result != ISC_R_SUCCESS)
537 /* Skip SOA records. */
539 dns_name_t *name_dummy = NULL;
540 isc_uint32_t ttl_dummy;
541 dns_rdata_t *rdata = NULL;
542 db_rr_iterator_current(&s->it, &name_dummy,
544 if (rdata->type != dns_rdatatype_soa)
546 result = db_rr_iterator_next(&s->it);
547 if (result != ISC_R_SUCCESS)
554 axfr_rrstream_next(rrstream_t *rs) {
555 axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
558 /* Skip SOA records. */
560 dns_name_t *name_dummy = NULL;
561 isc_uint32_t ttl_dummy;
562 dns_rdata_t *rdata = NULL;
563 result = db_rr_iterator_next(&s->it);
564 if (result != ISC_R_SUCCESS)
566 db_rr_iterator_current(&s->it, &name_dummy,
568 if (rdata->type != dns_rdatatype_soa)
575 axfr_rrstream_current(rrstream_t *rs, dns_name_t **name, isc_uint32_t *ttl,
578 axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
579 db_rr_iterator_current(&s->it, name, ttl, rdata);
583 axfr_rrstream_pause(rrstream_t *rs) {
584 axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
585 db_rr_iterator_pause(&s->it);
589 axfr_rrstream_destroy(rrstream_t **rsp) {
590 axfr_rrstream_t *s = (axfr_rrstream_t *) *rsp;
592 db_rr_iterator_destroy(&s->it);
593 isc_mem_put(s->common.mctx, s, sizeof(*s));
596 static rrstream_methods_t axfr_rrstream_methods = {
599 axfr_rrstream_current,
601 axfr_rrstream_destroy
604 /**************************************************************************/
606 * An 'soa_rrstream_t' is a degenerate 'rrstream_t' that returns
607 * a single SOA record.
610 typedef struct soa_rrstream {
612 dns_difftuple_t *soa_tuple;
616 * Forward declarations.
619 soa_rrstream_destroy(rrstream_t **rsp);
621 static rrstream_methods_t soa_rrstream_methods;
624 soa_rrstream_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *ver,
630 INSIST(sp != NULL && *sp == NULL);
632 s = isc_mem_get(mctx, sizeof(*s));
634 return (ISC_R_NOMEMORY);
635 s->common.mctx = mctx;
636 s->common.methods = &soa_rrstream_methods;
639 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_EXISTS,
642 *sp = (rrstream_t *) s;
643 return (ISC_R_SUCCESS);
646 soa_rrstream_destroy((rrstream_t **) (void *)&s);
651 soa_rrstream_first(rrstream_t *rs) {
653 return (ISC_R_SUCCESS);
657 soa_rrstream_next(rrstream_t *rs) {
659 return (ISC_R_NOMORE);
663 soa_rrstream_current(rrstream_t *rs, dns_name_t **name, isc_uint32_t *ttl,
666 soa_rrstream_t *s = (soa_rrstream_t *) rs;
667 *name = &s->soa_tuple->name;
668 *ttl = s->soa_tuple->ttl;
669 *rdata = &s->soa_tuple->rdata;
673 soa_rrstream_destroy(rrstream_t **rsp) {
674 soa_rrstream_t *s = (soa_rrstream_t *) *rsp;
675 if (s->soa_tuple != NULL)
676 dns_difftuple_free(&s->soa_tuple);
677 isc_mem_put(s->common.mctx, s, sizeof(*s));
680 static rrstream_methods_t soa_rrstream_methods = {
683 soa_rrstream_current,
688 /**************************************************************************/
690 * A 'compound_rrstream_t' objects owns a soa_rrstream
691 * and another rrstream, the "data stream". It returns
692 * a concatenated stream consisting of the soa_rrstream, then
693 * the data stream, then the soa_rrstream again.
695 * The component streams are owned by the compound_rrstream_t
696 * and are destroyed with it.
699 typedef struct compound_rrstream {
701 rrstream_t *components[3];
704 } compound_rrstream_t;
707 * Forward declarations.
710 compound_rrstream_destroy(rrstream_t **rsp);
713 compound_rrstream_next(rrstream_t *rs);
715 static rrstream_methods_t compound_rrstream_methods;
719 * soa_stream != NULL && *soa_stream != NULL
720 * data_stream != NULL && *data_stream != NULL
721 * sp != NULL && *sp == NULL
724 * *soa_stream == NULL
725 * *data_stream == NULL
726 * *sp points to a valid compound_rrstream_t
727 * The soa and data streams will be destroyed
728 * when the compound_rrstream_t is destroyed.
731 compound_rrstream_create(isc_mem_t *mctx, rrstream_t **soa_stream,
732 rrstream_t **data_stream, rrstream_t **sp)
734 compound_rrstream_t *s;
736 INSIST(sp != NULL && *sp == NULL);
738 s = isc_mem_get(mctx, sizeof(*s));
740 return (ISC_R_NOMEMORY);
741 s->common.mctx = mctx;
742 s->common.methods = &compound_rrstream_methods;
743 s->components[0] = *soa_stream;
744 s->components[1] = *data_stream;
745 s->components[2] = *soa_stream;
747 s->result = ISC_R_FAILURE;
751 *sp = (rrstream_t *) s;
752 return (ISC_R_SUCCESS);
756 compound_rrstream_first(rrstream_t *rs) {
757 compound_rrstream_t *s = (compound_rrstream_t *) rs;
760 rrstream_t *curstream = s->components[s->state];
761 s->result = curstream->methods->first(curstream);
762 } while (s->result == ISC_R_NOMORE && s->state < 2);
767 compound_rrstream_next(rrstream_t *rs) {
768 compound_rrstream_t *s = (compound_rrstream_t *) rs;
769 rrstream_t *curstream = s->components[s->state];
770 s->result = curstream->methods->next(curstream);
771 while (s->result == ISC_R_NOMORE) {
773 * Make sure locks held by the current stream
774 * are released before we switch streams.
776 curstream->methods->pause(curstream);
778 return (ISC_R_NOMORE);
780 curstream = s->components[s->state];
781 s->result = curstream->methods->first(curstream);
787 compound_rrstream_current(rrstream_t *rs, dns_name_t **name, isc_uint32_t *ttl,
790 compound_rrstream_t *s = (compound_rrstream_t *) rs;
791 rrstream_t *curstream;
792 INSIST(0 <= s->state && s->state < 3);
793 INSIST(s->result == ISC_R_SUCCESS);
794 curstream = s->components[s->state];
795 curstream->methods->current(curstream, name, ttl, rdata);
799 compound_rrstream_pause(rrstream_t *rs)
801 compound_rrstream_t *s = (compound_rrstream_t *) rs;
802 rrstream_t *curstream;
803 INSIST(0 <= s->state && s->state < 3);
804 curstream = s->components[s->state];
805 curstream->methods->pause(curstream);
809 compound_rrstream_destroy(rrstream_t **rsp) {
810 compound_rrstream_t *s = (compound_rrstream_t *) *rsp;
811 s->components[0]->methods->destroy(&s->components[0]);
812 s->components[1]->methods->destroy(&s->components[1]);
813 s->components[2] = NULL; /* Copy of components[0]. */
814 isc_mem_put(s->common.mctx, s, sizeof(*s));
817 static rrstream_methods_t compound_rrstream_methods = {
818 compound_rrstream_first,
819 compound_rrstream_next,
820 compound_rrstream_current,
821 compound_rrstream_pause,
822 compound_rrstream_destroy
825 /**************************************************************************/
827 * An 'xfrout_ctx_t' contains the state of an outgoing AXFR or IXFR
834 unsigned int id; /* ID of request */
835 dns_name_t *qname; /* Question name of request */
836 dns_rdatatype_t qtype; /* dns_rdatatype_{a,i}xfr */
837 dns_rdataclass_t qclass;
838 dns_zone_t *zone; /* (necessary for stats) */
840 dns_dbversion_t *ver;
842 rrstream_t *stream; /* The XFR RR stream */
843 isc_boolean_t end_of_stream; /* EOS has been reached */
844 isc_buffer_t buf; /* Buffer for message owner
846 isc_buffer_t txlenbuf; /* Transmit length buffer */
847 isc_buffer_t txbuf; /* Transmit message buffer */
849 unsigned int txmemlen;
850 unsigned int nmsg; /* Number of messages sent */
851 dns_tsigkey_t *tsigkey; /* Key used to create TSIG */
852 isc_buffer_t *lasttsig; /* the last TSIG */
853 isc_boolean_t many_answers;
854 int sends; /* Send in progress */
855 isc_boolean_t shuttingdown;
856 const char *mnemonic; /* Style of transfer */
860 xfrout_ctx_create(isc_mem_t *mctx, ns_client_t *client,
861 unsigned int id, dns_name_t *qname, dns_rdatatype_t qtype,
862 dns_rdataclass_t qclass, dns_zone_t *zone,
863 dns_db_t *db, dns_dbversion_t *ver, isc_quota_t *quota,
864 rrstream_t *stream, dns_tsigkey_t *tsigkey,
865 isc_buffer_t *lasttsig,
866 unsigned int maxtime,
867 unsigned int idletime,
868 isc_boolean_t many_answers,
869 xfrout_ctx_t **xfrp);
872 sendstream(xfrout_ctx_t *xfr);
875 xfrout_senddone(isc_task_t *task, isc_event_t *event);
878 xfrout_fail(xfrout_ctx_t *xfr, isc_result_t result, const char *msg);
881 xfrout_maybe_destroy(xfrout_ctx_t *xfr);
884 xfrout_ctx_destroy(xfrout_ctx_t **xfrp);
887 xfrout_client_shutdown(void *arg, isc_result_t result);
890 xfrout_log1(ns_client_t *client, dns_name_t *zonename,
891 dns_rdataclass_t rdclass, int level,
892 const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
895 xfrout_log(xfrout_ctx_t *xfr, int level, const char *fmt, ...)
896 ISC_FORMAT_PRINTF(3, 4);
898 /**************************************************************************/
901 ns_xfr_start(ns_client_t *client, dns_rdatatype_t reqtype) {
903 dns_name_t *question_name;
904 dns_rdataset_t *question_rdataset;
905 dns_zone_t *zone = NULL;
907 dns_dbversion_t *ver = NULL;
908 dns_rdataclass_t question_class;
909 rrstream_t *soa_stream = NULL;
910 rrstream_t *data_stream = NULL;
911 rrstream_t *stream = NULL;
912 dns_difftuple_t *current_soa_tuple = NULL;
913 dns_name_t *soa_name;
914 dns_rdataset_t *soa_rdataset;
915 dns_rdata_t soa_rdata = DNS_RDATA_INIT;
916 isc_boolean_t have_soa = ISC_FALSE;
917 const char *mnemonic = NULL;
918 isc_mem_t *mctx = client->mctx;
919 dns_message_t *request = client->message;
920 xfrout_ctx_t *xfr = NULL;
921 isc_quota_t *quota = NULL;
922 dns_transfer_format_t format = client->view->transfer_format;
924 dns_peer_t *peer = NULL;
925 isc_buffer_t *tsigbuf = NULL;
927 char msg[NS_CLIENT_ACLMSGSIZE("zone transfer")];
928 char keyname[DNS_NAME_FORMATSIZE];
929 isc_boolean_t is_poll = ISC_FALSE;
931 isc_boolean_t is_dlz = ISC_FALSE;
935 case dns_rdatatype_axfr:
938 case dns_rdatatype_ixfr:
946 ns_client_log(client,
947 DNS_LOGCATEGORY_XFER_OUT, NS_LOGMODULE_XFER_OUT,
948 ISC_LOG_DEBUG(6), "%s request", mnemonic);
952 result = isc_quota_attach(&ns_g_server->xfroutquota, "a);
953 if (result != ISC_R_SUCCESS) {
954 isc_log_write(XFROUT_COMMON_LOGARGS, ISC_LOG_WARNING,
955 "%s request denied: %s", mnemonic,
956 isc_result_totext(result));
961 * Interpret the question section.
963 result = dns_message_firstname(request, DNS_SECTION_QUESTION);
964 INSIST(result == ISC_R_SUCCESS);
967 * The question section must contain exactly one question, and
968 * it must be for AXFR/IXFR as appropriate.
970 question_name = NULL;
971 dns_message_currentname(request, DNS_SECTION_QUESTION, &question_name);
972 question_rdataset = ISC_LIST_HEAD(question_name->list);
973 question_class = question_rdataset->rdclass;
974 INSIST(question_rdataset->type == reqtype);
975 if (ISC_LIST_NEXT(question_rdataset, link) != NULL)
976 FAILC(DNS_R_FORMERR, "multiple questions");
977 result = dns_message_nextname(request, DNS_SECTION_QUESTION);
978 if (result != ISC_R_NOMORE)
979 FAILC(DNS_R_FORMERR, "multiple questions");
981 result = dns_zt_find(client->view->zonetable, question_name, 0, NULL,
984 if (result != ISC_R_SUCCESS)
988 * Normal zone table does not have a match. Try the DLZ database
990 if (client->view->dlzdatabase != NULL) {
991 result = dns_dlzallowzonexfr(client->view,
992 question_name, &client->peeraddr,
995 if (result == ISC_R_NOPERM) {
996 char _buf1[DNS_NAME_FORMATSIZE];
997 char _buf2[DNS_RDATACLASS_FORMATSIZE];
999 result = DNS_R_REFUSED;
1000 dns_name_format(question_name, _buf1,
1002 dns_rdataclass_format(question_class,
1003 _buf2, sizeof(_buf2));
1004 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1005 NS_LOGMODULE_XFER_OUT,
1007 "zone transfer '%s/%s' denied",
1011 if (result != ISC_R_SUCCESS)
1013 FAILQ(DNS_R_NOTAUTH, "non-authoritative zone",
1014 question_name, question_class);
1018 * DLZ only support full zone transfer, not incremental
1020 if (reqtype != dns_rdatatype_axfr) {
1021 mnemonic = "AXFR-style IXFR";
1022 reqtype = dns_rdatatype_axfr;
1027 * not DLZ and not in normal zone table, we are
1030 FAILQ(DNS_R_NOTAUTH, "non-authoritative zone",
1031 question_name, question_class);
1034 /* zone table has a match */
1036 switch(dns_zone_gettype(zone)) {
1037 case dns_zone_master:
1038 case dns_zone_slave:
1039 break; /* Master and slave zones are OK for transfer. */
1041 FAILQ(DNS_R_NOTAUTH, "non-authoritative zone", question_name, question_class);
1043 CHECK(dns_zone_getdb(zone, &db));
1044 dns_db_currentversion(db, &ver);
1049 xfrout_log1(client, question_name, question_class, ISC_LOG_DEBUG(6),
1050 "%s question section OK", mnemonic);
1053 * Check the authority section. Look for a SOA record with
1054 * the same name and class as the question.
1056 for (result = dns_message_firstname(request, DNS_SECTION_AUTHORITY);
1057 result == ISC_R_SUCCESS;
1058 result = dns_message_nextname(request, DNS_SECTION_AUTHORITY))
1061 dns_message_currentname(request, DNS_SECTION_AUTHORITY,
1065 * Ignore data whose owner name is not the zone apex.
1067 if (! dns_name_equal(soa_name, question_name))
1070 for (soa_rdataset = ISC_LIST_HEAD(soa_name->list);
1071 soa_rdataset != NULL;
1072 soa_rdataset = ISC_LIST_NEXT(soa_rdataset, link))
1075 * Ignore non-SOA data.
1077 if (soa_rdataset->type != dns_rdatatype_soa)
1079 if (soa_rdataset->rdclass != question_class)
1082 CHECK(dns_rdataset_first(soa_rdataset));
1083 dns_rdataset_current(soa_rdataset, &soa_rdata);
1084 result = dns_rdataset_next(soa_rdataset);
1085 if (result == ISC_R_SUCCESS)
1086 FAILC(DNS_R_FORMERR,
1087 "IXFR authority section "
1088 "has multiple SOAs");
1089 have_soa = ISC_TRUE;
1094 if (result != ISC_R_NOMORE)
1097 xfrout_log1(client, question_name, question_class, ISC_LOG_DEBUG(6),
1098 "%s authority section OK", mnemonic);
1101 * Decide whether to allow this transfer.
1105 * if not a DLZ zone decide whether to allow this transfer.
1109 ns_client_aclmsg("zone transfer", question_name, reqtype,
1110 client->view->rdclass, msg, sizeof(msg));
1111 CHECK(ns_client_checkacl(client, NULL, msg,
1112 dns_zone_getxfracl(zone),
1113 ISC_TRUE, ISC_LOG_ERROR));
1119 * AXFR over UDP is not possible.
1121 if (reqtype == dns_rdatatype_axfr &&
1122 (client->attributes & NS_CLIENTATTR_TCP) == 0)
1123 FAILC(DNS_R_FORMERR, "attempted AXFR over UDP");
1126 * Look up the requesting server in the peer table.
1128 isc_netaddr_fromsockaddr(&na, &client->peeraddr);
1129 (void)dns_peerlist_peerbyaddr(client->view->peers, &na, &peer);
1132 * Decide on the transfer format (one-answer or many-answers).
1135 (void)dns_peer_gettransferformat(peer, &format);
1138 * Get a dynamically allocated copy of the current SOA.
1142 dns_db_currentversion(db, &ver);
1144 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_EXISTS,
1145 ¤t_soa_tuple));
1147 if (reqtype == dns_rdatatype_ixfr) {
1148 isc_uint32_t begin_serial, current_serial;
1149 isc_boolean_t provide_ixfr;
1152 * Outgoing IXFR may have been disabled for this peer
1155 provide_ixfr = client->view->provideixfr;
1157 (void) dns_peer_getprovideixfr(peer, &provide_ixfr);
1158 if (provide_ixfr == ISC_FALSE)
1162 FAILC(DNS_R_FORMERR,
1163 "IXFR request missing SOA");
1165 begin_serial = dns_soa_getserial(&soa_rdata);
1166 current_serial = dns_soa_getserial(¤t_soa_tuple->rdata);
1169 * RFC1995 says "If an IXFR query with the same or
1170 * newer version number than that of the server
1171 * is received, it is replied to with a single SOA
1172 * record of the server's current version, just as
1173 * in AXFR". The claim about AXFR is incorrect,
1174 * but other than that, we do as the RFC says.
1176 * Sending a single SOA record is also how we refuse
1177 * IXFR over UDP (currently, we always do).
1179 if (DNS_SERIAL_GE(begin_serial, current_serial) ||
1180 (client->attributes & NS_CLIENTATTR_TCP) == 0)
1182 CHECK(soa_rrstream_create(mctx, db, ver, &stream));
1186 journalfile = dns_zone_getjournal(zone);
1187 if (journalfile != NULL)
1188 result = ixfr_rrstream_create(mctx,
1194 result = ISC_R_NOTFOUND;
1195 if (result == ISC_R_NOTFOUND ||
1196 result == ISC_R_RANGE) {
1197 xfrout_log1(client, question_name, question_class,
1199 "IXFR version not in journal, "
1200 "falling back to AXFR");
1201 mnemonic = "AXFR-style IXFR";
1207 CHECK(axfr_rrstream_create(mctx, db, ver,
1212 * Bracket the data stream with SOAs.
1214 CHECK(soa_rrstream_create(mctx, db, ver, &soa_stream));
1215 CHECK(compound_rrstream_create(mctx, &soa_stream, &data_stream,
1221 CHECK(dns_message_getquerytsig(request, mctx, &tsigbuf));
1223 * Create the xfrout context object. This transfers the ownership
1224 * of "stream", "db", "ver", and "quota" to the xfrout context object.
1231 CHECK(xfrout_ctx_create(mctx, client, request->id, question_name,
1232 reqtype, question_class, zone, db, ver,
1234 dns_message_gettsigkey(request),
1238 (format == dns_many_answers) ?
1239 ISC_TRUE : ISC_FALSE,
1243 CHECK(xfrout_ctx_create(mctx, client, request->id, question_name,
1244 reqtype, question_class, zone, db, ver,
1246 dns_message_gettsigkey(request),
1248 dns_zone_getmaxxfrout(zone),
1249 dns_zone_getidleout(zone),
1250 (format == dns_many_answers) ?
1251 ISC_TRUE : ISC_FALSE,
1254 xfr->mnemonic = mnemonic;
1258 CHECK(xfr->stream->methods->first(xfr->stream));
1260 if (xfr->tsigkey != NULL) {
1261 dns_name_format(&xfr->tsigkey->name, keyname, sizeof(keyname));
1265 xfrout_log1(client, question_name, question_class,
1266 ISC_LOG_DEBUG(1), "IXFR poll up to date%s%s",
1267 (xfr->tsigkey != NULL) ? ": TSIG " : "", keyname);
1269 xfrout_log1(client, question_name, question_class,
1270 ISC_LOG_INFO, "%s started%s%s", mnemonic,
1271 (xfr->tsigkey != NULL) ? ": TSIG " : "", keyname);
1274 * Hand the context over to sendstream(). Set xfr to NULL;
1275 * sendstream() is responsible for either passing the
1276 * context on to a later event handler or destroying it.
1281 result = ISC_R_SUCCESS;
1284 if (result == DNS_R_REFUSED)
1285 inc_stats(zone, dns_nsstatscounter_xfrrej);
1287 isc_quota_detach("a);
1288 if (current_soa_tuple != NULL)
1289 dns_difftuple_free(¤t_soa_tuple);
1291 stream->methods->destroy(&stream);
1292 if (soa_stream != NULL)
1293 soa_stream->methods->destroy(&soa_stream);
1294 if (data_stream != NULL)
1295 data_stream->methods->destroy(&data_stream);
1297 dns_db_closeversion(db, &ver, ISC_FALSE);
1301 dns_zone_detach(&zone);
1304 xfrout_fail(xfr, result, "setting up zone transfer");
1305 } else if (result != ISC_R_SUCCESS) {
1306 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT,
1307 NS_LOGMODULE_XFER_OUT,
1308 ISC_LOG_DEBUG(3), "zone transfer setup failed");
1309 ns_client_error(client, result);
1314 xfrout_ctx_create(isc_mem_t *mctx, ns_client_t *client, unsigned int id,
1315 dns_name_t *qname, dns_rdatatype_t qtype,
1316 dns_rdataclass_t qclass, dns_zone_t *zone,
1317 dns_db_t *db, dns_dbversion_t *ver, isc_quota_t *quota,
1318 rrstream_t *stream, dns_tsigkey_t *tsigkey,
1319 isc_buffer_t *lasttsig, unsigned int maxtime,
1320 unsigned int idletime, isc_boolean_t many_answers,
1321 xfrout_ctx_t **xfrp)
1324 isc_result_t result;
1328 INSIST(xfrp != NULL && *xfrp == NULL);
1329 xfr = isc_mem_get(mctx, sizeof(*xfr));
1331 return (ISC_R_NOMEMORY);
1334 ns_client_attach(client, &xfr->client);
1338 xfr->qclass = qclass;
1342 if (zone != NULL) /* zone will be NULL if it's DLZ */
1343 dns_zone_attach(zone, &xfr->zone);
1344 dns_db_attach(db, &xfr->db);
1345 dns_db_attachversion(db, ver, &xfr->ver);
1346 xfr->end_of_stream = ISC_FALSE;
1347 xfr->tsigkey = tsigkey;
1348 xfr->lasttsig = lasttsig;
1352 xfr->many_answers = many_answers,
1354 xfr->shuttingdown = ISC_FALSE;
1355 xfr->mnemonic = NULL;
1356 xfr->buf.base = NULL;
1357 xfr->buf.length = 0;
1364 * Allocate a temporary buffer for the uncompressed response
1365 * message data. The size should be no more than 65535 bytes
1366 * so that the compressed data will fit in a TCP message,
1367 * and no less than 65535 bytes so that an almost maximum-sized
1368 * RR will fit. Note that although 65535-byte RRs are allowed
1369 * in principle, they cannot be zone-transferred (at least not
1370 * if uncompressible), because the message and RR headers would
1371 * push the size of the TCP message over the 65536 byte limit.
1374 mem = isc_mem_get(mctx, len);
1376 result = ISC_R_NOMEMORY;
1379 isc_buffer_init(&xfr->buf, mem, len);
1382 * Allocate another temporary buffer for the compressed
1383 * response message and its TCP length prefix.
1386 mem = isc_mem_get(mctx, len);
1388 result = ISC_R_NOMEMORY;
1391 isc_buffer_init(&xfr->txlenbuf, mem, 2);
1392 isc_buffer_init(&xfr->txbuf, (char *) mem + 2, len - 2);
1394 xfr->txmemlen = len;
1396 CHECK(dns_timer_setidle(xfr->client->timer,
1397 maxtime, idletime, ISC_FALSE));
1400 * Register a shutdown callback with the client, so that we
1401 * can stop the transfer immediately when the client task
1402 * gets a shutdown event.
1404 xfr->client->shutdown = xfrout_client_shutdown;
1405 xfr->client->shutdown_arg = xfr;
1407 * These MUST be after the last "goto failure;" / CHECK to
1408 * prevent a double free by the caller.
1411 xfr->stream = stream;
1414 return (ISC_R_SUCCESS);
1417 xfrout_ctx_destroy(&xfr);
1423 * Arrange to send as much as we can of "stream" without blocking.
1426 * The stream iterator is initialized and points at an RR,
1427 * or possibly at the end of the stream (that is, the
1428 * _first method of the iterator has been called).
1431 sendstream(xfrout_ctx_t *xfr) {
1432 dns_message_t *tcpmsg = NULL;
1433 dns_message_t *msg = NULL; /* Client message if UDP, tcpmsg if TCP */
1434 isc_result_t result;
1436 isc_region_t region;
1437 dns_rdataset_t *qrdataset;
1438 dns_name_t *msgname = NULL;
1439 dns_rdata_t *msgrdata = NULL;
1440 dns_rdatalist_t *msgrdl = NULL;
1441 dns_rdataset_t *msgrds = NULL;
1442 dns_compress_t cctx;
1443 isc_boolean_t cleanup_cctx = ISC_FALSE;
1447 isc_buffer_clear(&xfr->buf);
1448 isc_buffer_clear(&xfr->txlenbuf);
1449 isc_buffer_clear(&xfr->txbuf);
1451 if ((xfr->client->attributes & NS_CLIENTATTR_TCP) == 0) {
1453 * In the UDP case, we put the response data directly into
1454 * the client message.
1456 msg = xfr->client->message;
1457 CHECK(dns_message_reply(msg, ISC_TRUE));
1460 * TCP. Build a response dns_message_t, temporarily storing
1461 * the raw, uncompressed owner names and RR data contiguously
1462 * in xfr->buf. We know that if the uncompressed data fits
1463 * in xfr->buf, the compressed data will surely fit in a TCP
1467 CHECK(dns_message_create(xfr->mctx,
1468 DNS_MESSAGE_INTENTRENDER, &tcpmsg));
1472 msg->rcode = dns_rcode_noerror;
1473 msg->flags = DNS_MESSAGEFLAG_QR | DNS_MESSAGEFLAG_AA;
1474 if ((xfr->client->attributes & NS_CLIENTATTR_RA) != 0)
1475 msg->flags |= DNS_MESSAGEFLAG_RA;
1476 CHECK(dns_message_settsigkey(msg, xfr->tsigkey));
1477 CHECK(dns_message_setquerytsig(msg, xfr->lasttsig));
1478 if (xfr->lasttsig != NULL)
1479 isc_buffer_free(&xfr->lasttsig);
1482 * Include a question section in the first message only.
1483 * BIND 8.2.1 will not recognize an IXFR if it does not
1484 * have a question section.
1486 if (xfr->nmsg == 0) {
1487 dns_name_t *qname = NULL;
1491 * Reserve space for the 12-byte message header
1492 * and 4 bytes of question.
1494 isc_buffer_add(&xfr->buf, 12 + 4);
1497 result = dns_message_gettemprdataset(msg, &qrdataset);
1498 if (result != ISC_R_SUCCESS)
1500 dns_rdataset_init(qrdataset);
1501 dns_rdataset_makequestion(qrdataset,
1502 xfr->client->message->rdclass,
1505 result = dns_message_gettempname(msg, &qname);
1506 if (result != ISC_R_SUCCESS)
1508 dns_name_init(qname, NULL);
1509 isc_buffer_availableregion(&xfr->buf, &r);
1510 INSIST(r.length >= xfr->qname->length);
1511 r.length = xfr->qname->length;
1512 isc_buffer_putmem(&xfr->buf, xfr->qname->ndata,
1513 xfr->qname->length);
1514 dns_name_fromregion(qname, &r);
1515 ISC_LIST_INIT(qname->list);
1516 ISC_LIST_APPEND(qname->list, qrdataset, link);
1518 dns_message_addname(msg, qname, DNS_SECTION_QUESTION);
1521 msg->tcp_continuation = 1;
1525 * Try to fit in as many RRs as possible, unless "one-answer"
1526 * format has been requested.
1528 for (n_rrs = 0; ; n_rrs++) {
1529 dns_name_t *name = NULL;
1531 dns_rdata_t *rdata = NULL;
1541 xfr->stream->methods->current(xfr->stream,
1542 &name, &ttl, &rdata);
1543 size = name->length + 10 + rdata->length;
1544 isc_buffer_availableregion(&xfr->buf, &r);
1545 if (size >= r.length) {
1547 * RR would not fit. If there are other RRs in the
1548 * buffer, send them now and leave this RR to the
1549 * next message. If this RR overflows the buffer
1550 * all by itself, fail.
1552 * In theory some RRs might fit in a TCP message
1553 * when compressed even if they do not fit when
1554 * uncompressed, but surely we don't want
1555 * to send such monstrosities to an unsuspecting
1559 xfrout_log(xfr, ISC_LOG_WARNING,
1560 "RR too large for zone transfer "
1561 "(%d bytes)", size);
1562 /* XXX DNS_R_RRTOOLARGE? */
1563 result = ISC_R_NOSPACE;
1569 if (isc_log_wouldlog(ns_g_lctx, XFROUT_RR_LOGLEVEL))
1570 log_rr(name, rdata, ttl); /* XXX */
1572 result = dns_message_gettempname(msg, &msgname);
1573 if (result != ISC_R_SUCCESS)
1575 dns_name_init(msgname, NULL);
1576 isc_buffer_availableregion(&xfr->buf, &r);
1577 INSIST(r.length >= name->length);
1578 r.length = name->length;
1579 isc_buffer_putmem(&xfr->buf, name->ndata, name->length);
1580 dns_name_fromregion(msgname, &r);
1582 /* Reserve space for RR header. */
1583 isc_buffer_add(&xfr->buf, 10);
1585 result = dns_message_gettemprdata(msg, &msgrdata);
1586 if (result != ISC_R_SUCCESS)
1588 isc_buffer_availableregion(&xfr->buf, &r);
1589 r.length = rdata->length;
1590 isc_buffer_putmem(&xfr->buf, rdata->data, rdata->length);
1591 dns_rdata_init(msgrdata);
1592 dns_rdata_fromregion(msgrdata,
1593 rdata->rdclass, rdata->type, &r);
1595 result = dns_message_gettemprdatalist(msg, &msgrdl);
1596 if (result != ISC_R_SUCCESS)
1598 msgrdl->type = rdata->type;
1599 msgrdl->rdclass = rdata->rdclass;
1601 if (rdata->type == dns_rdatatype_sig ||
1602 rdata->type == dns_rdatatype_rrsig)
1603 msgrdl->covers = dns_rdata_covers(rdata);
1605 msgrdl->covers = dns_rdatatype_none;
1606 ISC_LINK_INIT(msgrdl, link);
1607 ISC_LIST_INIT(msgrdl->rdata);
1608 ISC_LIST_APPEND(msgrdl->rdata, msgrdata, link);
1610 result = dns_message_gettemprdataset(msg, &msgrds);
1611 if (result != ISC_R_SUCCESS)
1613 dns_rdataset_init(msgrds);
1614 result = dns_rdatalist_tordataset(msgrdl, msgrds);
1615 INSIST(result == ISC_R_SUCCESS);
1617 ISC_LIST_APPEND(msgname->list, msgrds, link);
1619 dns_message_addname(msg, msgname, DNS_SECTION_ANSWER);
1622 result = xfr->stream->methods->next(xfr->stream);
1623 if (result == ISC_R_NOMORE) {
1624 xfr->end_of_stream = ISC_TRUE;
1629 if (! xfr->many_answers)
1633 if ((xfr->client->attributes & NS_CLIENTATTR_TCP) != 0) {
1634 CHECK(dns_compress_init(&cctx, -1, xfr->mctx));
1635 dns_compress_setsensitive(&cctx, ISC_TRUE);
1636 cleanup_cctx = ISC_TRUE;
1637 CHECK(dns_message_renderbegin(msg, &cctx, &xfr->txbuf));
1638 CHECK(dns_message_rendersection(msg, DNS_SECTION_QUESTION, 0));
1639 CHECK(dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0));
1640 CHECK(dns_message_renderend(msg));
1641 dns_compress_invalidate(&cctx);
1642 cleanup_cctx = ISC_FALSE;
1644 isc_buffer_usedregion(&xfr->txbuf, &used);
1645 isc_buffer_putuint16(&xfr->txlenbuf,
1646 (isc_uint16_t)used.length);
1647 region.base = xfr->txlenbuf.base;
1648 region.length = 2 + used.length;
1649 xfrout_log(xfr, ISC_LOG_DEBUG(8),
1650 "sending TCP message of %d bytes",
1652 CHECK(isc_socket_send(xfr->client->tcpsocket, /* XXX */
1653 ®ion, xfr->client->task,
1658 xfrout_log(xfr, ISC_LOG_DEBUG(8), "sending IXFR UDP response");
1659 ns_client_send(xfr->client);
1660 xfr->stream->methods->pause(xfr->stream);
1661 xfrout_ctx_destroy(&xfr);
1665 /* Advance lasttsig to be the last TSIG generated */
1666 CHECK(dns_message_getquerytsig(msg, xfr->mctx, &xfr->lasttsig));
1671 if (msgname != NULL) {
1672 if (msgrds != NULL) {
1673 if (dns_rdataset_isassociated(msgrds))
1674 dns_rdataset_disassociate(msgrds);
1675 dns_message_puttemprdataset(msg, &msgrds);
1677 if (msgrdl != NULL) {
1678 ISC_LIST_UNLINK(msgrdl->rdata, msgrdata, link);
1679 dns_message_puttemprdatalist(msg, &msgrdl);
1681 if (msgrdata != NULL)
1682 dns_message_puttemprdata(msg, &msgrdata);
1683 dns_message_puttempname(msg, &msgname);
1687 dns_message_destroy(&tcpmsg);
1690 dns_compress_invalidate(&cctx);
1692 * Make sure to release any locks held by database
1693 * iterators before returning from the event handler.
1695 xfr->stream->methods->pause(xfr->stream);
1697 if (result == ISC_R_SUCCESS)
1700 xfrout_fail(xfr, result, "sending zone data");
1704 xfrout_ctx_destroy(xfrout_ctx_t **xfrp) {
1705 xfrout_ctx_t *xfr = *xfrp;
1707 INSIST(xfr->sends == 0);
1709 xfr->client->shutdown = NULL;
1710 xfr->client->shutdown_arg = NULL;
1712 if (xfr->stream != NULL)
1713 xfr->stream->methods->destroy(&xfr->stream);
1714 if (xfr->buf.base != NULL)
1715 isc_mem_put(xfr->mctx, xfr->buf.base, xfr->buf.length);
1716 if (xfr->txmem != NULL)
1717 isc_mem_put(xfr->mctx, xfr->txmem, xfr->txmemlen);
1718 if (xfr->lasttsig != NULL)
1719 isc_buffer_free(&xfr->lasttsig);
1720 if (xfr->quota != NULL)
1721 isc_quota_detach(&xfr->quota);
1722 if (xfr->ver != NULL)
1723 dns_db_closeversion(xfr->db, &xfr->ver, ISC_FALSE);
1724 if (xfr->zone != NULL)
1725 dns_zone_detach(&xfr->zone);
1726 if (xfr->db != NULL)
1727 dns_db_detach(&xfr->db);
1729 ns_client_detach(&xfr->client);
1731 isc_mem_put(xfr->mctx, xfr, sizeof(*xfr));
1737 xfrout_senddone(isc_task_t *task, isc_event_t *event) {
1738 isc_socketevent_t *sev = (isc_socketevent_t *)event;
1739 xfrout_ctx_t *xfr = (xfrout_ctx_t *)event->ev_arg;
1740 isc_result_t evresult = sev->result;
1744 INSIST(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1746 isc_event_free(&event);
1748 INSIST(xfr->sends == 0);
1750 (void)isc_timer_touch(xfr->client->timer);
1751 if (xfr->shuttingdown == ISC_TRUE) {
1752 xfrout_maybe_destroy(xfr);
1753 } else if (evresult != ISC_R_SUCCESS) {
1754 xfrout_fail(xfr, evresult, "send");
1755 } else if (xfr->end_of_stream == ISC_FALSE) {
1758 /* End of zone transfer stream. */
1759 inc_stats(xfr->zone, dns_nsstatscounter_xfrdone);
1760 xfrout_log(xfr, ISC_LOG_INFO, "%s ended", xfr->mnemonic);
1761 ns_client_next(xfr->client, ISC_R_SUCCESS);
1762 xfrout_ctx_destroy(&xfr);
1767 xfrout_fail(xfrout_ctx_t *xfr, isc_result_t result, const char *msg) {
1768 xfr->shuttingdown = ISC_TRUE;
1769 xfrout_log(xfr, ISC_LOG_ERROR, "%s: %s",
1770 msg, isc_result_totext(result));
1771 xfrout_maybe_destroy(xfr);
1775 xfrout_maybe_destroy(xfrout_ctx_t *xfr) {
1776 INSIST(xfr->shuttingdown == ISC_TRUE);
1777 if (xfr->sends > 0) {
1779 * If we are currently sending, cancel it and wait for
1780 * cancel event before destroying the context.
1782 isc_socket_cancel(xfr->client->tcpsocket, xfr->client->task,
1783 ISC_SOCKCANCEL_SEND);
1785 ns_client_next(xfr->client, ISC_R_CANCELED);
1786 xfrout_ctx_destroy(&xfr);
1791 xfrout_client_shutdown(void *arg, isc_result_t result) {
1792 xfrout_ctx_t *xfr = (xfrout_ctx_t *) arg;
1793 xfrout_fail(xfr, result, "aborted");
1797 * Log outgoing zone transfer messages in a format like
1798 * <client>: transfer of <zone>: <message>
1802 xfrout_logv(ns_client_t *client, dns_name_t *zonename,
1803 dns_rdataclass_t rdclass, int level, const char *fmt, va_list ap)
1804 ISC_FORMAT_PRINTF(5, 0);
1807 xfrout_logv(ns_client_t *client, dns_name_t *zonename,
1808 dns_rdataclass_t rdclass, int level, const char *fmt, va_list ap)
1811 char namebuf[DNS_NAME_FORMATSIZE];
1812 char classbuf[DNS_RDATACLASS_FORMATSIZE];
1814 dns_name_format(zonename, namebuf, sizeof(namebuf));
1815 dns_rdataclass_format(rdclass, classbuf, sizeof(classbuf));
1816 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
1817 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT,
1818 NS_LOGMODULE_XFER_OUT, level,
1819 "transfer of '%s/%s': %s", namebuf, classbuf, msgbuf);
1823 * Logging function for use when a xfrout_ctx_t has not yet been created.
1826 xfrout_log1(ns_client_t *client, dns_name_t *zonename,
1827 dns_rdataclass_t rdclass, int level, const char *fmt, ...) {
1830 xfrout_logv(client, zonename, rdclass, level, fmt, ap);
1835 * Logging function for use when there is a xfrout_ctx_t.
1838 xfrout_log(xfrout_ctx_t *xfr, int level, const char *fmt, ...) {
1841 xfrout_logv(xfr->client, xfr->qname, xfr->qclass, level, fmt, ap);