Detect FPU by checking CPUID features.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / masterdump.c
1 /*
2  * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: masterdump.c,v 1.89.128.5 2009/01/19 23:47:02 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25
26 #include <isc/event.h>
27 #include <isc/file.h>
28 #include <isc/magic.h>
29 #include <isc/mem.h>
30 #include <isc/print.h>
31 #include <isc/stdio.h>
32 #include <isc/string.h>
33 #include <isc/task.h>
34 #include <isc/time.h>
35 #include <isc/util.h>
36
37 #include <dns/db.h>
38 #include <dns/dbiterator.h>
39 #include <dns/events.h>
40 #include <dns/fixedname.h>
41 #include <dns/lib.h>
42 #include <dns/log.h>
43 #include <dns/master.h>
44 #include <dns/masterdump.h>
45 #include <dns/rdata.h>
46 #include <dns/rdataclass.h>
47 #include <dns/rdataset.h>
48 #include <dns/rdatasetiter.h>
49 #include <dns/rdatatype.h>
50 #include <dns/result.h>
51 #include <dns/time.h>
52 #include <dns/ttl.h>
53
54 #define DNS_DCTX_MAGIC          ISC_MAGIC('D', 'c', 't', 'x')
55 #define DNS_DCTX_VALID(d)       ISC_MAGIC_VALID(d, DNS_DCTX_MAGIC)
56
57 #define RETERR(x) do { \
58         isc_result_t _r = (x); \
59         if (_r != ISC_R_SUCCESS) \
60                 return (_r); \
61         } while (0)
62
63 struct dns_master_style {
64         unsigned int flags;             /* DNS_STYLEFLAG_* */
65         unsigned int ttl_column;
66         unsigned int class_column;
67         unsigned int type_column;
68         unsigned int rdata_column;
69         unsigned int line_length;
70         unsigned int tab_width;
71 };
72
73 /*%
74  * The maximum length of the newline+indentation that is output
75  * when inserting a line break in an RR.  This effectively puts an
76  * upper limits on the value of "rdata_column", because if it is
77  * very large, the tabs and spaces needed to reach it will not fit.
78  */
79 #define DNS_TOTEXT_LINEBREAK_MAXLEN 100
80
81 /*%
82  * Context structure for a masterfile dump in progress.
83  */
84 typedef struct dns_totext_ctx {
85         dns_master_style_t      style;
86         isc_boolean_t           class_printed;
87         char *                  linebreak;
88         char                    linebreak_buf[DNS_TOTEXT_LINEBREAK_MAXLEN];
89         dns_name_t *            origin;
90         dns_name_t *            neworigin;
91         dns_fixedname_t         origin_fixname;
92         isc_uint32_t            current_ttl;
93         isc_boolean_t           current_ttl_valid;
94 } dns_totext_ctx_t;
95
96 LIBDNS_EXTERNAL_DATA const dns_master_style_t
97 dns_master_style_default = {
98         DNS_STYLEFLAG_OMIT_OWNER |
99         DNS_STYLEFLAG_OMIT_CLASS |
100         DNS_STYLEFLAG_REL_OWNER |
101         DNS_STYLEFLAG_REL_DATA |
102         DNS_STYLEFLAG_OMIT_TTL |
103         DNS_STYLEFLAG_TTL |
104         DNS_STYLEFLAG_COMMENT |
105         DNS_STYLEFLAG_MULTILINE,
106         24, 24, 24, 32, 80, 8
107 };
108
109 LIBDNS_EXTERNAL_DATA const dns_master_style_t
110 dns_master_style_full = {
111         DNS_STYLEFLAG_COMMENT,
112         46, 46, 46, 64, 120, 8
113 };
114
115 LIBDNS_EXTERNAL_DATA const dns_master_style_t
116 dns_master_style_explicitttl = {
117         DNS_STYLEFLAG_OMIT_OWNER |
118         DNS_STYLEFLAG_OMIT_CLASS |
119         DNS_STYLEFLAG_REL_OWNER |
120         DNS_STYLEFLAG_REL_DATA |
121         DNS_STYLEFLAG_COMMENT |
122         DNS_STYLEFLAG_MULTILINE,
123         24, 32, 32, 40, 80, 8
124 };
125
126 LIBDNS_EXTERNAL_DATA const dns_master_style_t
127 dns_master_style_cache = {
128         DNS_STYLEFLAG_OMIT_OWNER |
129         DNS_STYLEFLAG_OMIT_CLASS |
130         DNS_STYLEFLAG_MULTILINE |
131         DNS_STYLEFLAG_TRUST |
132         DNS_STYLEFLAG_NCACHE,
133         24, 32, 32, 40, 80, 8
134 };
135
136 LIBDNS_EXTERNAL_DATA const dns_master_style_t
137 dns_master_style_simple = {
138         0,
139         24, 32, 32, 40, 80, 8
140 };
141
142 /*%
143  * A style suitable for dns_rdataset_totext().
144  */
145 LIBDNS_EXTERNAL_DATA const dns_master_style_t
146 dns_master_style_debug = {
147         DNS_STYLEFLAG_REL_OWNER,
148         24, 32, 40, 48, 80, 8
149 };
150
151
152 #define N_SPACES 10
153 static char spaces[N_SPACES+1] = "          ";
154
155 #define N_TABS 10
156 static char tabs[N_TABS+1] = "\t\t\t\t\t\t\t\t\t\t";
157
158 struct dns_dumpctx {
159         unsigned int            magic;
160         isc_mem_t               *mctx;
161         isc_mutex_t             lock;
162         unsigned int            references;
163         isc_boolean_t           canceled;
164         isc_boolean_t           first;
165         isc_boolean_t           do_date;
166         isc_stdtime_t           now;
167         FILE                    *f;
168         dns_db_t                *db;
169         dns_dbversion_t         *version;
170         dns_dbiterator_t        *dbiter;
171         dns_totext_ctx_t        tctx;
172         isc_task_t              *task;
173         dns_dumpdonefunc_t      done;
174         void                    *done_arg;
175         unsigned int            nodes;
176         /* dns_master_dumpinc() */
177         char                    *file;
178         char                    *tmpfile;
179         dns_masterformat_t      format;
180         isc_result_t            (*dumpsets)(isc_mem_t *mctx, dns_name_t *name,
181                                             dns_rdatasetiter_t *rdsiter,
182                                             dns_totext_ctx_t *ctx,
183                                             isc_buffer_t *buffer, FILE *f);
184 };
185
186 #define NXDOMAIN(x) (((x)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
187
188 /*%
189  * Output tabs and spaces to go from column '*current' to
190  * column 'to', and update '*current' to reflect the new
191  * current column.
192  */
193 static isc_result_t
194 indent(unsigned int *current, unsigned int to, int tabwidth,
195        isc_buffer_t *target)
196 {
197         isc_region_t r;
198         unsigned char *p;
199         unsigned int from;
200         int ntabs, nspaces, t;
201
202         from = *current;
203
204         if (to < from + 1)
205                 to = from + 1;
206
207         ntabs = to / tabwidth - from / tabwidth;
208         if (ntabs < 0)
209                 ntabs = 0;
210
211         if (ntabs > 0) {
212                 isc_buffer_availableregion(target, &r);
213                 if (r.length < (unsigned) ntabs)
214                         return (ISC_R_NOSPACE);
215                 p = r.base;
216
217                 t = ntabs;
218                 while (t) {
219                         int n = t;
220                         if (n > N_TABS)
221                                 n = N_TABS;
222                         memcpy(p, tabs, n);
223                         p += n;
224                         t -= n;
225                 }
226                 isc_buffer_add(target, ntabs);
227                 from = (to / tabwidth) * tabwidth;
228         }
229
230         nspaces = to - from;
231         INSIST(nspaces >= 0);
232
233         isc_buffer_availableregion(target, &r);
234         if (r.length < (unsigned) nspaces)
235                 return (ISC_R_NOSPACE);
236         p = r.base;
237
238         t = nspaces;
239         while (t) {
240                 int n = t;
241                 if (n > N_SPACES)
242                         n = N_SPACES;
243                 memcpy(p, spaces, n);
244                 p += n;
245                 t -= n;
246         }
247         isc_buffer_add(target, nspaces);
248
249         *current = to;
250         return (ISC_R_SUCCESS);
251 }
252
253 static isc_result_t
254 totext_ctx_init(const dns_master_style_t *style, dns_totext_ctx_t *ctx) {
255         isc_result_t result;
256
257         REQUIRE(style->tab_width != 0);
258
259         ctx->style = *style;
260         ctx->class_printed = ISC_FALSE;
261
262         dns_fixedname_init(&ctx->origin_fixname);
263
264         /*
265          * Set up the line break string if needed.
266          */
267         if ((ctx->style.flags & DNS_STYLEFLAG_MULTILINE) != 0) {
268                 isc_buffer_t buf;
269                 isc_region_t r;
270                 unsigned int col = 0;
271
272                 isc_buffer_init(&buf, ctx->linebreak_buf,
273                                 sizeof(ctx->linebreak_buf));
274
275                 isc_buffer_availableregion(&buf, &r);
276                 if (r.length < 1)
277                         return (DNS_R_TEXTTOOLONG);
278                 r.base[0] = '\n';
279                 isc_buffer_add(&buf, 1);
280
281                 result = indent(&col, ctx->style.rdata_column,
282                                 ctx->style.tab_width, &buf);
283                 /*
284                  * Do not return ISC_R_NOSPACE if the line break string
285                  * buffer is too small, because that would just make
286                  * dump_rdataset() retry indefinitely with ever
287                  * bigger target buffers.  That's a different buffer,
288                  * so it won't help.  Use DNS_R_TEXTTOOLONG as a substitute.
289                  */
290                 if (result == ISC_R_NOSPACE)
291                         return (DNS_R_TEXTTOOLONG);
292                 if (result != ISC_R_SUCCESS)
293                         return (result);
294
295                 isc_buffer_availableregion(&buf, &r);
296                 if (r.length < 1)
297                         return (DNS_R_TEXTTOOLONG);
298                 r.base[0] = '\0';
299                 isc_buffer_add(&buf, 1);
300                 ctx->linebreak = ctx->linebreak_buf;
301         } else {
302                 ctx->linebreak = NULL;
303         }
304
305         ctx->origin = NULL;
306         ctx->neworigin = NULL;
307         ctx->current_ttl = 0;
308         ctx->current_ttl_valid = ISC_FALSE;
309
310         return (ISC_R_SUCCESS);
311 }
312
313 #define INDENT_TO(col) \
314         do { \
315                  if ((result = indent(&column, ctx->style.col, \
316                                       ctx->style.tab_width, target)) \
317                      != ISC_R_SUCCESS) \
318                             return (result); \
319         } while (0)
320
321
322 static isc_result_t
323 str_totext(const char *source, isc_buffer_t *target) {
324         unsigned int l;
325         isc_region_t region;
326
327         isc_buffer_availableregion(target, &region);
328         l = strlen(source);
329
330         if (l > region.length)
331                 return (ISC_R_NOSPACE);
332
333         memcpy(region.base, source, l);
334         isc_buffer_add(target, l);
335         return (ISC_R_SUCCESS);
336 }
337
338 /*
339  * Convert 'rdataset' to master file text format according to 'ctx',
340  * storing the result in 'target'.  If 'owner_name' is NULL, it
341  * is omitted; otherwise 'owner_name' must be valid and have at least
342  * one label.
343  */
344
345 static isc_result_t
346 rdataset_totext(dns_rdataset_t *rdataset,
347                 dns_name_t *owner_name,
348                 dns_totext_ctx_t *ctx,
349                 isc_boolean_t omit_final_dot,
350                 isc_buffer_t *target)
351 {
352         isc_result_t result;
353         unsigned int column;
354         isc_boolean_t first = ISC_TRUE;
355         isc_uint32_t current_ttl;
356         isc_boolean_t current_ttl_valid;
357         dns_rdatatype_t type;
358
359         REQUIRE(DNS_RDATASET_VALID(rdataset));
360
361         rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
362         result = dns_rdataset_first(rdataset);
363         REQUIRE(result == ISC_R_SUCCESS);
364
365         current_ttl = ctx->current_ttl;
366         current_ttl_valid = ctx->current_ttl_valid;
367
368         do {
369                 column = 0;
370
371                 /*
372                  * Owner name.
373                  */
374                 if (owner_name != NULL &&
375                     ! ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0 &&
376                        !first))
377                 {
378                         unsigned int name_start = target->used;
379                         RETERR(dns_name_totext(owner_name,
380                                                omit_final_dot,
381                                                target));
382                         column += target->used - name_start;
383                 }
384
385                 /*
386                  * TTL.
387                  */
388                 if ((ctx->style.flags & DNS_STYLEFLAG_NO_TTL) == 0 &&
389                     !((ctx->style.flags & DNS_STYLEFLAG_OMIT_TTL) != 0 &&
390                       current_ttl_valid &&
391                       rdataset->ttl == current_ttl))
392                 {
393                         char ttlbuf[64];
394                         isc_region_t r;
395                         unsigned int length;
396
397                         INDENT_TO(ttl_column);
398                         length = snprintf(ttlbuf, sizeof(ttlbuf), "%u",
399                                           rdataset->ttl);
400                         INSIST(length <= sizeof(ttlbuf));
401                         isc_buffer_availableregion(target, &r);
402                         if (r.length < length)
403                                 return (ISC_R_NOSPACE);
404                         memcpy(r.base, ttlbuf, length);
405                         isc_buffer_add(target, length);
406                         column += length;
407
408                         /*
409                          * If the $TTL directive is not in use, the TTL we
410                          * just printed becomes the default for subsequent RRs.
411                          */
412                         if ((ctx->style.flags & DNS_STYLEFLAG_TTL) == 0) {
413                                 current_ttl = rdataset->ttl;
414                                 current_ttl_valid = ISC_TRUE;
415                         }
416                 }
417
418                 /*
419                  * Class.
420                  */
421                 if ((ctx->style.flags & DNS_STYLEFLAG_NO_CLASS) == 0 &&
422                     ((ctx->style.flags & DNS_STYLEFLAG_OMIT_CLASS) == 0 ||
423                      ctx->class_printed == ISC_FALSE))
424                 {
425                         unsigned int class_start;
426                         INDENT_TO(class_column);
427                         class_start = target->used;
428                         result = dns_rdataclass_totext(rdataset->rdclass,
429                                                        target);
430                         if (result != ISC_R_SUCCESS)
431                                 return (result);
432                         column += (target->used - class_start);
433                 }
434
435                 /*
436                  * Type.
437                  */
438
439                 if (rdataset->type == 0) {
440                         type = rdataset->covers;
441                 } else {
442                         type = rdataset->type;
443                 }
444
445                 {
446                         unsigned int type_start;
447                         INDENT_TO(type_column);
448                         type_start = target->used;
449                         if (rdataset->type == 0)
450                                 RETERR(str_totext("\\-", target));
451                         result = dns_rdatatype_totext(type, target);
452                         if (result != ISC_R_SUCCESS)
453                                 return (result);
454                         column += (target->used - type_start);
455                 }
456
457                 /*
458                  * Rdata.
459                  */
460                 INDENT_TO(rdata_column);
461                 if (rdataset->type == 0) {
462                         if (NXDOMAIN(rdataset))
463                                 RETERR(str_totext(";-$NXDOMAIN\n", target));
464                         else
465                                 RETERR(str_totext(";-$NXRRSET\n", target));
466                 } else {
467                         dns_rdata_t rdata = DNS_RDATA_INIT;
468                         isc_region_t r;
469
470                         dns_rdataset_current(rdataset, &rdata);
471
472                         RETERR(dns_rdata_tofmttext(&rdata,
473                                                    ctx->origin,
474                                                    ctx->style.flags,
475                                                    ctx->style.line_length -
476                                                        ctx->style.rdata_column,
477                                                    ctx->linebreak,
478                                                    target));
479
480                         isc_buffer_availableregion(target, &r);
481                         if (r.length < 1)
482                                 return (ISC_R_NOSPACE);
483                         r.base[0] = '\n';
484                         isc_buffer_add(target, 1);
485                 }
486
487                 first = ISC_FALSE;
488                 result = dns_rdataset_next(rdataset);
489         } while (result == ISC_R_SUCCESS);
490
491         if (result != ISC_R_NOMORE)
492                 return (result);
493
494         /*
495          * Update the ctx state to reflect what we just printed.
496          * This is done last, only when we are sure we will return
497          * success, because this function may be called multiple
498          * times with increasing buffer sizes until it succeeds,
499          * and failed attempts must not update the state prematurely.
500          */
501         ctx->class_printed = ISC_TRUE;
502         ctx->current_ttl= current_ttl;
503         ctx->current_ttl_valid = current_ttl_valid;
504
505         return (ISC_R_SUCCESS);
506 }
507
508 /*
509  * Print the name, type, and class of an empty rdataset,
510  * such as those used to represent the question section
511  * of a DNS message.
512  */
513 static isc_result_t
514 question_totext(dns_rdataset_t *rdataset,
515                 dns_name_t *owner_name,
516                 dns_totext_ctx_t *ctx,
517                 isc_boolean_t omit_final_dot,
518                 isc_buffer_t *target)
519 {
520         unsigned int column;
521         isc_result_t result;
522         isc_region_t r;
523
524         REQUIRE(DNS_RDATASET_VALID(rdataset));
525         result = dns_rdataset_first(rdataset);
526         REQUIRE(result == ISC_R_NOMORE);
527
528         column = 0;
529
530         /* Owner name */
531         {
532                 unsigned int name_start = target->used;
533                 RETERR(dns_name_totext(owner_name,
534                                        omit_final_dot,
535                                        target));
536                 column += target->used - name_start;
537         }
538
539         /* Class */
540         {
541                 unsigned int class_start;
542                 INDENT_TO(class_column);
543                 class_start = target->used;
544                 result = dns_rdataclass_totext(rdataset->rdclass, target);
545                 if (result != ISC_R_SUCCESS)
546                         return (result);
547                 column += (target->used - class_start);
548         }
549
550         /* Type */
551         {
552                 unsigned int type_start;
553                 INDENT_TO(type_column);
554                 type_start = target->used;
555                 result = dns_rdatatype_totext(rdataset->type, target);
556                 if (result != ISC_R_SUCCESS)
557                         return (result);
558                 column += (target->used - type_start);
559         }
560
561         isc_buffer_availableregion(target, &r);
562         if (r.length < 1)
563                 return (ISC_R_NOSPACE);
564         r.base[0] = '\n';
565         isc_buffer_add(target, 1);
566
567         return (ISC_R_SUCCESS);
568 }
569
570 isc_result_t
571 dns_rdataset_totext(dns_rdataset_t *rdataset,
572                     dns_name_t *owner_name,
573                     isc_boolean_t omit_final_dot,
574                     isc_boolean_t question,
575                     isc_buffer_t *target)
576 {
577         dns_totext_ctx_t ctx;
578         isc_result_t result;
579         result = totext_ctx_init(&dns_master_style_debug, &ctx);
580         if (result != ISC_R_SUCCESS) {
581                 UNEXPECTED_ERROR(__FILE__, __LINE__,
582                                  "could not set master file style");
583                 return (ISC_R_UNEXPECTED);
584         }
585
586         /*
587          * The caller might want to give us an empty owner
588          * name (e.g. if they are outputting into a master
589          * file and this rdataset has the same name as the
590          * previous one.)
591          */
592         if (dns_name_countlabels(owner_name) == 0)
593                 owner_name = NULL;
594
595         if (question)
596                 return (question_totext(rdataset, owner_name, &ctx,
597                                         omit_final_dot, target));
598         else
599                 return (rdataset_totext(rdataset, owner_name, &ctx,
600                                         omit_final_dot, target));
601 }
602
603 isc_result_t
604 dns_master_rdatasettotext(dns_name_t *owner_name,
605                           dns_rdataset_t *rdataset,
606                           const dns_master_style_t *style,
607                           isc_buffer_t *target)
608 {
609         dns_totext_ctx_t ctx;
610         isc_result_t result;
611         result = totext_ctx_init(style, &ctx);
612         if (result != ISC_R_SUCCESS) {
613                 UNEXPECTED_ERROR(__FILE__, __LINE__,
614                                  "could not set master file style");
615                 return (ISC_R_UNEXPECTED);
616         }
617
618         return (rdataset_totext(rdataset, owner_name, &ctx,
619                                 ISC_FALSE, target));
620 }
621
622 isc_result_t
623 dns_master_questiontotext(dns_name_t *owner_name,
624                           dns_rdataset_t *rdataset,
625                           const dns_master_style_t *style,
626                           isc_buffer_t *target)
627 {
628         dns_totext_ctx_t ctx;
629         isc_result_t result;
630         result = totext_ctx_init(style, &ctx);
631         if (result != ISC_R_SUCCESS) {
632                 UNEXPECTED_ERROR(__FILE__, __LINE__,
633                                  "could not set master file style");
634                 return (ISC_R_UNEXPECTED);
635         }
636
637         return (question_totext(rdataset, owner_name, &ctx,
638                                 ISC_FALSE, target));
639 }
640
641 /*
642  * Print an rdataset.  'buffer' is a scratch buffer, which must have been
643  * dynamically allocated by the caller.  It must be large enough to
644  * hold the result from dns_ttl_totext().  If more than that is needed,
645  * the buffer will be grown automatically.
646  */
647
648 static isc_result_t
649 dump_rdataset(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
650               dns_totext_ctx_t *ctx,
651               isc_buffer_t *buffer, FILE *f)
652 {
653         isc_region_t r;
654         isc_result_t result;
655
656         REQUIRE(buffer->length > 0);
657
658         /*
659          * Output a $TTL directive if needed.
660          */
661
662         if ((ctx->style.flags & DNS_STYLEFLAG_TTL) != 0) {
663                 if (ctx->current_ttl_valid == ISC_FALSE ||
664                     ctx->current_ttl != rdataset->ttl)
665                 {
666                         if ((ctx->style.flags & DNS_STYLEFLAG_COMMENT) != 0)
667                         {
668                                 isc_buffer_clear(buffer);
669                                 result = dns_ttl_totext(rdataset->ttl,
670                                                         ISC_TRUE, buffer);
671                                 INSIST(result == ISC_R_SUCCESS);
672                                 isc_buffer_usedregion(buffer, &r);
673                                 fprintf(f, "$TTL %u\t; %.*s\n", rdataset->ttl,
674                                         (int) r.length, (char *) r.base);
675                         } else {
676                                 fprintf(f, "$TTL %u\n", rdataset->ttl);
677                         }
678                         ctx->current_ttl = rdataset->ttl;
679                         ctx->current_ttl_valid = ISC_TRUE;
680                 }
681         }
682
683         isc_buffer_clear(buffer);
684
685         /*
686          * Generate the text representation of the rdataset into
687          * the buffer.  If the buffer is too small, grow it.
688          */
689         for (;;) {
690                 int newlength;
691                 void *newmem;
692                 result = rdataset_totext(rdataset, name, ctx,
693                                          ISC_FALSE, buffer);
694                 if (result != ISC_R_NOSPACE)
695                         break;
696
697                 newlength = buffer->length * 2;
698                 newmem = isc_mem_get(mctx, newlength);
699                 if (newmem == NULL)
700                         return (ISC_R_NOMEMORY);
701                 isc_mem_put(mctx, buffer->base, buffer->length);
702                 isc_buffer_init(buffer, newmem, newlength);
703         }
704         if (result != ISC_R_SUCCESS)
705                 return (result);
706
707         /*
708          * Write the buffer contents to the master file.
709          */
710         isc_buffer_usedregion(buffer, &r);
711         result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
712
713         if (result != ISC_R_SUCCESS) {
714                 UNEXPECTED_ERROR(__FILE__, __LINE__,
715                                  "master file write failed: %s",
716                                  isc_result_totext(result));
717                 return (result);
718         }
719
720         return (ISC_R_SUCCESS);
721 }
722
723 /*
724  * Define the order in which rdatasets should be printed in zone
725  * files.  We will print SOA and NS records before others, SIGs
726  * immediately following the things they sign, and order everything
727  * else by RR number.  This is all just for aesthetics and
728  * compatibility with buggy software that expects the SOA to be first;
729  * the DNS specifications allow any order.
730  */
731
732 static int
733 dump_order(const dns_rdataset_t *rds) {
734         int t;
735         int sig;
736         if (rds->type == dns_rdatatype_rrsig) {
737                 t = rds->covers;
738                 sig = 1;
739         } else {
740                 t = rds->type;
741                 sig = 0;
742         }
743         switch (t) {
744         case dns_rdatatype_soa:
745                 t = 0;
746                 break;
747         case dns_rdatatype_ns:
748                 t = 1;
749                 break;
750         default:
751                 t += 2;
752                 break;
753         }
754         return (t << 1) + sig;
755 }
756
757 static int
758 dump_order_compare(const void *a, const void *b) {
759         return (dump_order(*((const dns_rdataset_t * const *) a)) -
760                 dump_order(*((const dns_rdataset_t * const *) b)));
761 }
762
763 /*
764  * Dump all the rdatasets of a domain name to a master file.  We make
765  * a "best effort" attempt to sort the RRsets in a nice order, but if
766  * there are more than MAXSORT RRsets, we punt and only sort them in
767  * groups of MAXSORT.  This is not expected to ever happen in practice
768  * since much less than 64 RR types have been registered with the
769  * IANA, so far, and the output will be correct (though not
770  * aesthetically pleasing) even if it does happen.
771  */
772
773 #define MAXSORT 64
774
775 static const char *trustnames[] = {
776         "none",
777         "pending",
778         "additional",
779         "glue",
780         "answer",
781         "authauthority",
782         "authanswer",
783         "secure",
784         "local" /* aka ultimate */
785 };
786
787 static isc_result_t
788 dump_rdatasets_text(isc_mem_t *mctx, dns_name_t *name,
789                     dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
790                     isc_buffer_t *buffer, FILE *f)
791 {
792         isc_result_t itresult, dumpresult;
793         isc_region_t r;
794         dns_rdataset_t rdatasets[MAXSORT];
795         dns_rdataset_t *sorted[MAXSORT];
796         int i, n;
797
798         itresult = dns_rdatasetiter_first(rdsiter);
799         dumpresult = ISC_R_SUCCESS;
800
801         if (itresult == ISC_R_SUCCESS && ctx->neworigin != NULL) {
802                 isc_buffer_clear(buffer);
803                 itresult = dns_name_totext(ctx->neworigin, ISC_FALSE, buffer);
804                 RUNTIME_CHECK(itresult == ISC_R_SUCCESS);
805                 isc_buffer_usedregion(buffer, &r);
806                 fprintf(f, "$ORIGIN %.*s\n", (int) r.length, (char *) r.base);
807                 ctx->neworigin = NULL;
808         }
809
810  again:
811         for (i = 0;
812              itresult == ISC_R_SUCCESS && i < MAXSORT;
813              itresult = dns_rdatasetiter_next(rdsiter), i++) {
814                 dns_rdataset_init(&rdatasets[i]);
815                 dns_rdatasetiter_current(rdsiter, &rdatasets[i]);
816                 sorted[i] = &rdatasets[i];
817         }
818         n = i;
819         INSIST(n <= MAXSORT);
820
821         qsort(sorted, n, sizeof(sorted[0]), dump_order_compare);
822
823         for (i = 0; i < n; i++) {
824                 dns_rdataset_t *rds = sorted[i];
825                 if (ctx->style.flags & DNS_STYLEFLAG_TRUST) {
826                         unsigned int trust = rds->trust;
827                         INSIST(trust < (sizeof(trustnames) /
828                                         sizeof(trustnames[0])));
829                         fprintf(f, "; %s\n", trustnames[trust]);
830                 }
831                 if (rds->type == 0 &&
832                     (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
833                         /* Omit negative cache entries */
834                 } else {
835                         isc_result_t result =
836                                 dump_rdataset(mctx, name, rds, ctx,
837                                                buffer, f);
838                         if (result != ISC_R_SUCCESS)
839                                 dumpresult = result;
840                         if ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0)
841                                 name = NULL;
842                 }
843                 dns_rdataset_disassociate(rds);
844         }
845
846         if (dumpresult != ISC_R_SUCCESS)
847                 return (dumpresult);
848
849         /*
850          * If we got more data than could be sorted at once,
851          * go handle the rest.
852          */
853         if (itresult == ISC_R_SUCCESS)
854                 goto again;
855
856         if (itresult == ISC_R_NOMORE)
857                 itresult = ISC_R_SUCCESS;
858
859         return (itresult);
860 }
861
862 /*
863  * Dump given RRsets in the "raw" format.
864  */
865 static isc_result_t
866 dump_rdataset_raw(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
867                   isc_buffer_t *buffer, FILE *f)
868 {
869         isc_result_t result;
870         isc_uint32_t totallen;
871         isc_uint16_t dlen;
872         isc_region_t r, r_hdr;
873
874         REQUIRE(buffer->length > 0);
875         REQUIRE(DNS_RDATASET_VALID(rdataset));
876
877  restart:
878         totallen = 0;
879         result = dns_rdataset_first(rdataset);
880         REQUIRE(result == ISC_R_SUCCESS);
881
882         isc_buffer_clear(buffer);
883
884         /*
885          * Common header and owner name (length followed by name)
886          * These fields should be in a moderate length, so we assume we
887          * can store all of them in the initial buffer.
888          */
889         isc_buffer_availableregion(buffer, &r_hdr);
890         INSIST(r_hdr.length >= sizeof(dns_masterrawrdataset_t));
891         isc_buffer_putuint32(buffer, totallen); /* XXX: leave space */
892         isc_buffer_putuint16(buffer, rdataset->rdclass); /* 16-bit class */
893         isc_buffer_putuint16(buffer, rdataset->type); /* 16-bit type */
894         isc_buffer_putuint16(buffer, rdataset->covers); /* same as type */
895         isc_buffer_putuint32(buffer, rdataset->ttl); /* 32-bit TTL */
896         isc_buffer_putuint32(buffer, dns_rdataset_count(rdataset));
897         totallen = isc_buffer_usedlength(buffer);
898         INSIST(totallen <= sizeof(dns_masterrawrdataset_t));
899
900         dns_name_toregion(name, &r);
901         INSIST(isc_buffer_availablelength(buffer) >=
902                (sizeof(dlen) + r.length));
903         dlen = (isc_uint16_t)r.length;
904         isc_buffer_putuint16(buffer, dlen);
905         isc_buffer_copyregion(buffer, &r);
906         totallen += sizeof(dlen) + r.length;
907
908         do {
909                 dns_rdata_t rdata = DNS_RDATA_INIT;
910                 isc_region_t r;
911
912                 dns_rdataset_current(rdataset, &rdata);
913                 dns_rdata_toregion(&rdata, &r);
914                 INSIST(r.length <= 0xffffU);
915                 dlen = (isc_uint16_t)r.length;
916
917                 /*
918                  * Copy the rdata into the buffer.  If the buffer is too small,
919                  * grow it.  This should be rare, so we'll simply restart the
920                  * entire procedure (or should we copy the old data and
921                  * continue?).
922                  */
923                 if (isc_buffer_availablelength(buffer) <
924                                                  sizeof(dlen) + r.length) {
925                         int newlength;
926                         void *newmem;
927
928                         newlength = buffer->length * 2;
929                         newmem = isc_mem_get(mctx, newlength);
930                         if (newmem == NULL)
931                                 return (ISC_R_NOMEMORY);
932                         isc_mem_put(mctx, buffer->base, buffer->length);
933                         isc_buffer_init(buffer, newmem, newlength);
934                         goto restart;
935                 }
936                 isc_buffer_putuint16(buffer, dlen);
937                 isc_buffer_copyregion(buffer, &r);
938                 totallen += sizeof(dlen) + r.length;
939
940                 result = dns_rdataset_next(rdataset);
941         } while (result == ISC_R_SUCCESS);
942
943         if (result != ISC_R_NOMORE)
944                 return (result);
945
946         /*
947          * Fill in the total length field.
948          * XXX: this is a bit tricky.  Since we have already "used" the space
949          * for the total length in the buffer, we first remember the entire
950          * buffer length in the region, "rewind", and then write the value.
951          */
952         isc_buffer_usedregion(buffer, &r);
953         isc_buffer_clear(buffer);
954         isc_buffer_putuint32(buffer, totallen);
955         INSIST(isc_buffer_usedlength(buffer) < totallen);
956
957         /*
958          * Write the buffer contents to the raw master file.
959          */
960         result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
961
962         if (result != ISC_R_SUCCESS) {
963                 UNEXPECTED_ERROR(__FILE__, __LINE__,
964                                  "raw master file write failed: %s",
965                                  isc_result_totext(result));
966                 return (result);
967         }
968
969         return (result);
970 }
971
972 static isc_result_t
973 dump_rdatasets_raw(isc_mem_t *mctx, dns_name_t *name,
974                    dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
975                    isc_buffer_t *buffer, FILE *f)
976 {
977         isc_result_t result;
978         dns_rdataset_t rdataset;
979
980         for (result = dns_rdatasetiter_first(rdsiter);
981              result == ISC_R_SUCCESS;
982              result = dns_rdatasetiter_next(rdsiter)) {
983
984                 dns_rdataset_init(&rdataset);
985                 dns_rdatasetiter_current(rdsiter, &rdataset);
986
987                 if (rdataset.type == 0 &&
988                     (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
989                         /* Omit negative cache entries */
990                 } else {
991                         result = dump_rdataset_raw(mctx, name, &rdataset,
992                                                    buffer, f);
993                 }
994                 dns_rdataset_disassociate(&rdataset);
995         }
996
997         if (result == ISC_R_NOMORE)
998                 result = ISC_R_SUCCESS;
999
1000         return (result);
1001 }
1002
1003 /*
1004  * Initial size of text conversion buffer.  The buffer is used
1005  * for several purposes: converting origin names, rdatasets,
1006  * $DATE timestamps, and comment strings for $TTL directives.
1007  *
1008  * When converting rdatasets, it is dynamically resized, but
1009  * when converting origins, timestamps, etc it is not.  Therefore,
1010  * the initial size must large enough to hold the longest possible
1011  * text representation of any domain name (for $ORIGIN).
1012  */
1013 static const int initial_buffer_length = 1200;
1014
1015 static isc_result_t
1016 dumptostreaminc(dns_dumpctx_t *dctx);
1017
1018 static void
1019 dumpctx_destroy(dns_dumpctx_t *dctx) {
1020
1021         dctx->magic = 0;
1022         DESTROYLOCK(&dctx->lock);
1023         dns_dbiterator_destroy(&dctx->dbiter);
1024         if (dctx->version != NULL)
1025                 dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
1026         dns_db_detach(&dctx->db);
1027         if (dctx->task != NULL)
1028                 isc_task_detach(&dctx->task);
1029         if (dctx->file != NULL)
1030                 isc_mem_free(dctx->mctx, dctx->file);
1031         if (dctx->tmpfile != NULL)
1032                 isc_mem_free(dctx->mctx, dctx->tmpfile);
1033         isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(*dctx));
1034 }
1035
1036 void
1037 dns_dumpctx_attach(dns_dumpctx_t *source, dns_dumpctx_t **target) {
1038
1039         REQUIRE(DNS_DCTX_VALID(source));
1040         REQUIRE(target != NULL && *target == NULL);
1041
1042         LOCK(&source->lock);
1043         INSIST(source->references > 0);
1044         source->references++;
1045         INSIST(source->references != 0);        /* Overflow? */
1046         UNLOCK(&source->lock);
1047
1048         *target = source;
1049 }
1050
1051 void
1052 dns_dumpctx_detach(dns_dumpctx_t **dctxp) {
1053         dns_dumpctx_t *dctx;
1054         isc_boolean_t need_destroy = ISC_FALSE;
1055
1056         REQUIRE(dctxp != NULL);
1057         dctx = *dctxp;
1058         REQUIRE(DNS_DCTX_VALID(dctx));
1059
1060         *dctxp = NULL;
1061
1062         LOCK(&dctx->lock);
1063         INSIST(dctx->references != 0);
1064         dctx->references--;
1065         if (dctx->references == 0)
1066                 need_destroy = ISC_TRUE;
1067         UNLOCK(&dctx->lock);
1068         if (need_destroy)
1069                 dumpctx_destroy(dctx);
1070 }
1071
1072 dns_dbversion_t *
1073 dns_dumpctx_version(dns_dumpctx_t *dctx) {
1074         REQUIRE(DNS_DCTX_VALID(dctx));
1075         return (dctx->version);
1076 }
1077
1078 dns_db_t *
1079 dns_dumpctx_db(dns_dumpctx_t *dctx) {
1080         REQUIRE(DNS_DCTX_VALID(dctx));
1081         return (dctx->db);
1082 }
1083
1084 void
1085 dns_dumpctx_cancel(dns_dumpctx_t *dctx) {
1086         REQUIRE(DNS_DCTX_VALID(dctx));
1087
1088         LOCK(&dctx->lock);
1089         dctx->canceled = ISC_TRUE;
1090         UNLOCK(&dctx->lock);
1091 }
1092
1093 static isc_result_t
1094 closeandrename(FILE *f, isc_result_t result, const char *temp, const char *file)
1095 {
1096         isc_result_t tresult;
1097         isc_boolean_t logit = ISC_TF(result == ISC_R_SUCCESS);
1098
1099         if (result == ISC_R_SUCCESS)
1100                 result = isc_stdio_sync(f);
1101         if (result != ISC_R_SUCCESS && logit) {
1102                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1103                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1104                               "dumping master file: %s: fsync: %s",
1105                               temp, isc_result_totext(result));
1106                 logit = ISC_FALSE;
1107         }
1108         tresult = isc_stdio_close(f);
1109         if (result == ISC_R_SUCCESS)
1110                 result = tresult;
1111         if (result != ISC_R_SUCCESS && logit) {
1112                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1113                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1114                               "dumping master file: %s: fclose: %s",
1115                               temp, isc_result_totext(result));
1116                 logit = ISC_FALSE;
1117         }
1118         if (result == ISC_R_SUCCESS)
1119                 result = isc_file_rename(temp, file);
1120         else
1121                 (void)isc_file_remove(temp);
1122         if (result != ISC_R_SUCCESS && logit) {
1123                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1124                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1125                               "dumping master file: rename: %s: %s",
1126                               file, isc_result_totext(result));
1127         }
1128         return (result);
1129 }
1130
1131 static void
1132 dump_quantum(isc_task_t *task, isc_event_t *event) {
1133         isc_result_t result;
1134         isc_result_t tresult;
1135         dns_dumpctx_t *dctx;
1136
1137         REQUIRE(event != NULL);
1138         dctx = event->ev_arg;
1139         REQUIRE(DNS_DCTX_VALID(dctx));
1140         if (dctx->canceled)
1141                 result = ISC_R_CANCELED;
1142         else
1143                 result = dumptostreaminc(dctx);
1144         if (result == DNS_R_CONTINUE) {
1145                 event->ev_arg = dctx;
1146                 isc_task_send(task, &event);
1147                 return;
1148         }
1149
1150         if (dctx->file != NULL) {
1151                 tresult = closeandrename(dctx->f, result,
1152                                          dctx->tmpfile, dctx->file);
1153                 if (tresult != ISC_R_SUCCESS && result == ISC_R_SUCCESS)
1154                         result = tresult;
1155         }
1156         (dctx->done)(dctx->done_arg, result);
1157         isc_event_free(&event);
1158         dns_dumpctx_detach(&dctx);
1159 }
1160
1161 static isc_result_t
1162 task_send(dns_dumpctx_t *dctx) {
1163         isc_event_t *event;
1164
1165         event = isc_event_allocate(dctx->mctx, NULL, DNS_EVENT_DUMPQUANTUM,
1166                                    dump_quantum, dctx, sizeof(*event));
1167         if (event == NULL)
1168                 return (ISC_R_NOMEMORY);
1169         isc_task_send(dctx->task, &event);
1170         return (ISC_R_SUCCESS);
1171 }
1172
1173 static isc_result_t
1174 dumpctx_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1175                const dns_master_style_t *style, FILE *f, dns_dumpctx_t **dctxp,
1176                dns_masterformat_t format)
1177 {
1178         dns_dumpctx_t *dctx;
1179         isc_result_t result;
1180         isc_boolean_t relative;
1181
1182         dctx = isc_mem_get(mctx, sizeof(*dctx));
1183         if (dctx == NULL)
1184                 return (ISC_R_NOMEMORY);
1185
1186         dctx->mctx = NULL;
1187         dctx->f = f;
1188         dctx->dbiter = NULL;
1189         dctx->db = NULL;
1190         dctx->version = NULL;
1191         dctx->done = NULL;
1192         dctx->done_arg = NULL;
1193         dctx->task = NULL;
1194         dctx->nodes = 0;
1195         dctx->first = ISC_TRUE;
1196         dctx->canceled = ISC_FALSE;
1197         dctx->file = NULL;
1198         dctx->tmpfile = NULL;
1199         dctx->format = format;
1200
1201         switch (format) {
1202         case dns_masterformat_text:
1203                 dctx->dumpsets = dump_rdatasets_text;
1204                 break;
1205         case dns_masterformat_raw:
1206                 dctx->dumpsets = dump_rdatasets_raw;
1207                 break;
1208         default:
1209                 INSIST(0);
1210                 break;
1211         }
1212
1213         result = totext_ctx_init(style, &dctx->tctx);
1214         if (result != ISC_R_SUCCESS) {
1215                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1216                                  "could not set master file style");
1217                 goto cleanup;
1218         }
1219
1220         isc_stdtime_get(&dctx->now);
1221         dns_db_attach(db, &dctx->db);
1222
1223         dctx->do_date = dns_db_iscache(dctx->db);
1224
1225         if (dctx->format == dns_masterformat_text &&
1226             (dctx->tctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) {
1227                 relative = ISC_TRUE;
1228         } else
1229                 relative = ISC_FALSE;
1230         result = dns_db_createiterator(dctx->db, relative, &dctx->dbiter);
1231         if (result != ISC_R_SUCCESS)
1232                 goto cleanup;
1233
1234         result = isc_mutex_init(&dctx->lock);
1235         if (result != ISC_R_SUCCESS)
1236                 goto cleanup;
1237         if (version != NULL)
1238                 dns_db_attachversion(dctx->db, version, &dctx->version);
1239         else if (!dns_db_iscache(db))
1240                 dns_db_currentversion(dctx->db, &dctx->version);
1241         isc_mem_attach(mctx, &dctx->mctx);
1242         dctx->references = 1;
1243         dctx->magic = DNS_DCTX_MAGIC;
1244         *dctxp = dctx;
1245         return (ISC_R_SUCCESS);
1246
1247  cleanup:
1248         if (dctx->dbiter != NULL)
1249                 dns_dbiterator_destroy(&dctx->dbiter);
1250         if (dctx->db != NULL)
1251                 dns_db_detach(&dctx->db);
1252         if (dctx != NULL)
1253                 isc_mem_put(mctx, dctx, sizeof(*dctx));
1254         return (result);
1255 }
1256
1257 static isc_result_t
1258 dumptostreaminc(dns_dumpctx_t *dctx) {
1259         isc_result_t result;
1260         isc_buffer_t buffer;
1261         char *bufmem;
1262         isc_region_t r;
1263         dns_name_t *name;
1264         dns_fixedname_t fixname;
1265         unsigned int nodes;
1266         dns_masterrawheader_t rawheader;
1267         isc_uint32_t now32;
1268         isc_time_t start;
1269
1270         bufmem = isc_mem_get(dctx->mctx, initial_buffer_length);
1271         if (bufmem == NULL)
1272                 return (ISC_R_NOMEMORY);
1273
1274         isc_buffer_init(&buffer, bufmem, initial_buffer_length);
1275
1276         dns_fixedname_init(&fixname);
1277         name = dns_fixedname_name(&fixname);
1278
1279         if (dctx->first) {
1280                 switch (dctx->format) {
1281                 case dns_masterformat_text:
1282                         /*
1283                          * If the database has cache semantics, output an
1284                          * RFC2540 $DATE directive so that the TTLs can be
1285                          * adjusted when it is reloaded.  For zones it is not
1286                          * really needed, and it would make the file
1287                          * incompatible with pre-RFC2540 software, so we omit
1288                          * it in the zone case.
1289                          */
1290                         if (dctx->do_date) {
1291                                 result = dns_time32_totext(dctx->now, &buffer);
1292                                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1293                                 isc_buffer_usedregion(&buffer, &r);
1294                                 fprintf(dctx->f, "$DATE %.*s\n",
1295                                         (int) r.length, (char *) r.base);
1296                         }
1297                         break;
1298                 case dns_masterformat_raw:
1299                         r.base = (unsigned char *)&rawheader;
1300                         r.length = sizeof(rawheader);
1301                         isc_buffer_region(&buffer, &r);
1302                         isc_buffer_putuint32(&buffer, dns_masterformat_raw);
1303                         isc_buffer_putuint32(&buffer, DNS_RAWFORMAT_VERSION);
1304                         if (sizeof(now32) != sizeof(dctx->now)) {
1305                                 /*
1306                                  * We assume isc_stdtime_t is a 32-bit integer,
1307                                  * which should be the case on most cases.
1308                                  * If it turns out to be uncommon, we'll need
1309                                  * to bump the version number and revise the
1310                                  * header format.
1311                                  */
1312                                 isc_log_write(dns_lctx,
1313                                               ISC_LOGCATEGORY_GENERAL,
1314                                               DNS_LOGMODULE_MASTERDUMP,
1315                                               ISC_LOG_INFO,
1316                                               "dumping master file in raw "
1317                                               "format: stdtime is not 32bits");
1318                                 now32 = 0;
1319                         } else
1320                                 now32 = dctx->now;
1321                         isc_buffer_putuint32(&buffer, now32);
1322                         INSIST(isc_buffer_usedlength(&buffer) <=
1323                                sizeof(rawheader));
1324                         result = isc_stdio_write(buffer.base, 1,
1325                                                  isc_buffer_usedlength(&buffer),
1326                                                  dctx->f, NULL);
1327                         if (result != ISC_R_SUCCESS)
1328                                 return (result);
1329                         isc_buffer_clear(&buffer);
1330                         break;
1331                 default:
1332                         INSIST(0);
1333                 }
1334
1335                 result = dns_dbiterator_first(dctx->dbiter);
1336                 dctx->first = ISC_FALSE;
1337         } else
1338                 result = ISC_R_SUCCESS;
1339
1340         nodes = dctx->nodes;
1341         isc_time_now(&start);
1342         while (result == ISC_R_SUCCESS && (dctx->nodes == 0 || nodes--)) {
1343                 dns_rdatasetiter_t *rdsiter = NULL;
1344                 dns_dbnode_t *node = NULL;
1345
1346                 result = dns_dbiterator_current(dctx->dbiter, &node, name);
1347                 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
1348                         break;
1349                 if (result == DNS_R_NEWORIGIN) {
1350                         dns_name_t *origin =
1351                                 dns_fixedname_name(&dctx->tctx.origin_fixname);
1352                         result = dns_dbiterator_origin(dctx->dbiter, origin);
1353                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
1354                         if ((dctx->tctx.style.flags & DNS_STYLEFLAG_REL_DATA) != 0)
1355                                 dctx->tctx.origin = origin;
1356                         dctx->tctx.neworigin = origin;
1357                 }
1358                 result = dns_db_allrdatasets(dctx->db, node, dctx->version,
1359                                              dctx->now, &rdsiter);
1360                 if (result != ISC_R_SUCCESS) {
1361                         dns_db_detachnode(dctx->db, &node);
1362                         goto fail;
1363                 }
1364                 result = (dctx->dumpsets)(dctx->mctx, name, rdsiter,
1365                                           &dctx->tctx, &buffer, dctx->f);
1366                 dns_rdatasetiter_destroy(&rdsiter);
1367                 if (result != ISC_R_SUCCESS) {
1368                         dns_db_detachnode(dctx->db, &node);
1369                         goto fail;
1370                 }
1371                 dns_db_detachnode(dctx->db, &node);
1372                 result = dns_dbiterator_next(dctx->dbiter);
1373         }
1374
1375         /*
1376          * Work out how many nodes can be written in the time between
1377          * two requests to the nameserver.  Smooth the resulting number and
1378          * use it as a estimate for the number of nodes to be written in the
1379          * next iteration.
1380          */
1381         if (dctx->nodes != 0 && result == ISC_R_SUCCESS) {
1382                 unsigned int pps = dns_pps;     /* packets per second */
1383                 unsigned int interval;
1384                 isc_uint64_t usecs;
1385                 isc_time_t end;
1386
1387                 isc_time_now(&end);
1388                 if (pps < 100)
1389                         pps = 100;
1390                 interval = 1000000 / pps;       /* interval in usecs */
1391                 if (interval == 0)
1392                         interval = 1;
1393                 usecs = isc_time_microdiff(&end, &start);
1394                 if (usecs == 0) {
1395                         dctx->nodes = dctx->nodes * 2;
1396                         if (dctx->nodes > 1000)
1397                                 dctx->nodes = 1000;
1398                 } else {
1399                         nodes = dctx->nodes * interval;
1400                         nodes /= (unsigned int)usecs;
1401                         if (nodes == 0)
1402                                 nodes = 1;
1403                         else if (nodes > 1000)
1404                                 nodes = 1000;
1405
1406                         /* Smooth and assign. */
1407                         dctx->nodes = (nodes + dctx->nodes * 7) / 8;
1408
1409                         isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1410                                       DNS_LOGMODULE_MASTERDUMP,
1411                                       ISC_LOG_DEBUG(1),
1412                                       "dumptostreaminc(%p) new nodes -> %d\n",
1413                                       dctx, dctx->nodes);
1414                 }
1415                 result = DNS_R_CONTINUE;
1416         } else if (result == ISC_R_NOMORE)
1417                 result = ISC_R_SUCCESS;
1418  fail:
1419         RUNTIME_CHECK(dns_dbiterator_pause(dctx->dbiter) == ISC_R_SUCCESS);
1420         isc_mem_put(dctx->mctx, buffer.base, buffer.length);
1421         return (result);
1422 }
1423
1424 isc_result_t
1425 dns_master_dumptostreaminc(isc_mem_t *mctx, dns_db_t *db,
1426                            dns_dbversion_t *version,
1427                            const dns_master_style_t *style,
1428                            FILE *f, isc_task_t *task,
1429                            dns_dumpdonefunc_t done, void *done_arg,
1430                            dns_dumpctx_t **dctxp)
1431 {
1432         dns_dumpctx_t *dctx = NULL;
1433         isc_result_t result;
1434
1435         REQUIRE(task != NULL);
1436         REQUIRE(f != NULL);
1437         REQUIRE(done != NULL);
1438
1439         result = dumpctx_create(mctx, db, version, style, f, &dctx,
1440                                 dns_masterformat_text);
1441         if (result != ISC_R_SUCCESS)
1442                 return (result);
1443         isc_task_attach(task, &dctx->task);
1444         dctx->done = done;
1445         dctx->done_arg = done_arg;
1446         dctx->nodes = 100;
1447
1448         result = task_send(dctx);
1449         if (result == ISC_R_SUCCESS) {
1450                 dns_dumpctx_attach(dctx, dctxp);
1451                 return (DNS_R_CONTINUE);
1452         }
1453
1454         dns_dumpctx_detach(&dctx);
1455         return (result);
1456 }
1457
1458 /*
1459  * Dump an entire database into a master file.
1460  */
1461 isc_result_t
1462 dns_master_dumptostream(isc_mem_t *mctx, dns_db_t *db,
1463                         dns_dbversion_t *version,
1464                         const dns_master_style_t *style,
1465                         FILE *f)
1466 {
1467         return (dns_master_dumptostream2(mctx, db, version, style,
1468                                          dns_masterformat_text, f));
1469 }
1470
1471 isc_result_t
1472 dns_master_dumptostream2(isc_mem_t *mctx, dns_db_t *db,
1473                          dns_dbversion_t *version,
1474                          const dns_master_style_t *style,
1475                          dns_masterformat_t format, FILE *f)
1476 {
1477         dns_dumpctx_t *dctx = NULL;
1478         isc_result_t result;
1479
1480         result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1481         if (result != ISC_R_SUCCESS)
1482                 return (result);
1483
1484         result = dumptostreaminc(dctx);
1485         INSIST(result != DNS_R_CONTINUE);
1486         dns_dumpctx_detach(&dctx);
1487         return (result);
1488 }
1489
1490 static isc_result_t
1491 opentmp(isc_mem_t *mctx, const char *file, char **tempp, FILE **fp) {
1492         FILE *f = NULL;
1493         isc_result_t result;
1494         char *tempname = NULL;
1495         int tempnamelen;
1496
1497         tempnamelen = strlen(file) + 20;
1498         tempname = isc_mem_allocate(mctx, tempnamelen);
1499         if (tempname == NULL)
1500                 return (ISC_R_NOMEMORY);
1501
1502         result = isc_file_mktemplate(file, tempname, tempnamelen);
1503         if (result != ISC_R_SUCCESS)
1504                 goto cleanup;
1505
1506         result = isc_file_openunique(tempname, &f);
1507         if (result != ISC_R_SUCCESS) {
1508                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1509                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1510                               "dumping master file: %s: open: %s",
1511                               tempname, isc_result_totext(result));
1512                 goto cleanup;
1513         }
1514         *tempp = tempname;
1515         *fp = f;
1516         return (ISC_R_SUCCESS);
1517
1518 cleanup:
1519         isc_mem_free(mctx, tempname);
1520         return (result);
1521 }
1522
1523 isc_result_t
1524 dns_master_dumpinc(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1525                    const dns_master_style_t *style, const char *filename,
1526                    isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
1527                    dns_dumpctx_t **dctxp)
1528 {
1529         return (dns_master_dumpinc2(mctx, db, version, style, filename, task,
1530                                     done, done_arg, dctxp,
1531                                     dns_masterformat_text));
1532 }
1533
1534 isc_result_t
1535 dns_master_dumpinc2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1536                     const dns_master_style_t *style, const char *filename,
1537                     isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
1538                     dns_dumpctx_t **dctxp, dns_masterformat_t format)
1539 {
1540         FILE *f = NULL;
1541         isc_result_t result;
1542         char *tempname = NULL;
1543         char *file = NULL;
1544         dns_dumpctx_t *dctx = NULL;
1545
1546         file = isc_mem_strdup(mctx, filename);
1547         if (file == NULL)
1548                 return (ISC_R_NOMEMORY);
1549
1550         result = opentmp(mctx, filename, &tempname, &f);
1551         if (result != ISC_R_SUCCESS)
1552                 goto cleanup;
1553
1554         result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1555         if (result != ISC_R_SUCCESS) {
1556                 (void)isc_stdio_close(f);
1557                 (void)isc_file_remove(tempname);
1558                 goto cleanup;
1559         }
1560
1561         isc_task_attach(task, &dctx->task);
1562         dctx->done = done;
1563         dctx->done_arg = done_arg;
1564         dctx->nodes = 100;
1565         dctx->file = file;
1566         file = NULL;
1567         dctx->tmpfile = tempname;
1568         tempname = NULL;
1569
1570         result = task_send(dctx);
1571         if (result == ISC_R_SUCCESS) {
1572                 dns_dumpctx_attach(dctx, dctxp);
1573                 return (DNS_R_CONTINUE);
1574         }
1575
1576  cleanup:
1577         if (dctx != NULL)
1578                 dns_dumpctx_detach(&dctx);
1579         if (file != NULL)
1580                 isc_mem_free(mctx, file);
1581         if (tempname != NULL)
1582                 isc_mem_free(mctx, tempname);
1583         return (result);
1584 }
1585
1586 isc_result_t
1587 dns_master_dump(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1588                 const dns_master_style_t *style, const char *filename)
1589 {
1590         return (dns_master_dump2(mctx, db, version, style, filename,
1591                                  dns_masterformat_text));
1592 }
1593
1594 isc_result_t
1595 dns_master_dump2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1596                  const dns_master_style_t *style, const char *filename,
1597                  dns_masterformat_t format)
1598 {
1599         FILE *f = NULL;
1600         isc_result_t result;
1601         char *tempname;
1602         dns_dumpctx_t *dctx = NULL;
1603
1604         result = opentmp(mctx, filename, &tempname, &f);
1605         if (result != ISC_R_SUCCESS)
1606                 return (result);
1607
1608         result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1609         if (result != ISC_R_SUCCESS)
1610                 goto cleanup;
1611
1612         result = dumptostreaminc(dctx);
1613         INSIST(result != DNS_R_CONTINUE);
1614         dns_dumpctx_detach(&dctx);
1615
1616         result = closeandrename(f, result, tempname, filename);
1617
1618  cleanup:
1619         isc_mem_free(mctx, tempname);
1620         return (result);
1621 }
1622
1623 /*
1624  * Dump a database node into a master file.
1625  * XXX: this function assumes the text format.
1626  */
1627 isc_result_t
1628 dns_master_dumpnodetostream(isc_mem_t *mctx, dns_db_t *db,
1629                             dns_dbversion_t *version,
1630                             dns_dbnode_t *node, dns_name_t *name,
1631                             const dns_master_style_t *style,
1632                             FILE *f)
1633 {
1634         isc_result_t result;
1635         isc_buffer_t buffer;
1636         char *bufmem;
1637         isc_stdtime_t now;
1638         dns_totext_ctx_t ctx;
1639         dns_rdatasetiter_t *rdsiter = NULL;
1640
1641         result = totext_ctx_init(style, &ctx);
1642         if (result != ISC_R_SUCCESS) {
1643                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1644                                  "could not set master file style");
1645                 return (ISC_R_UNEXPECTED);
1646         }
1647
1648         isc_stdtime_get(&now);
1649
1650         bufmem = isc_mem_get(mctx, initial_buffer_length);
1651         if (bufmem == NULL)
1652                 return (ISC_R_NOMEMORY);
1653
1654         isc_buffer_init(&buffer, bufmem, initial_buffer_length);
1655
1656         result = dns_db_allrdatasets(db, node, version, now, &rdsiter);
1657         if (result != ISC_R_SUCCESS)
1658                 goto failure;
1659         result = dump_rdatasets_text(mctx, name, rdsiter, &ctx, &buffer, f);
1660         if (result != ISC_R_SUCCESS)
1661                 goto failure;
1662         dns_rdatasetiter_destroy(&rdsiter);
1663
1664         result = ISC_R_SUCCESS;
1665
1666  failure:
1667         isc_mem_put(mctx, buffer.base, buffer.length);
1668         return (result);
1669 }
1670
1671 isc_result_t
1672 dns_master_dumpnode(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1673                     dns_dbnode_t *node, dns_name_t *name,
1674                     const dns_master_style_t *style, const char *filename)
1675 {
1676         FILE *f = NULL;
1677         isc_result_t result;
1678
1679         result = isc_stdio_open(filename, "w", &f);
1680         if (result != ISC_R_SUCCESS) {
1681                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1682                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1683                               "dumping node to file: %s: open: %s", filename,
1684                               isc_result_totext(result));
1685                 return (ISC_R_UNEXPECTED);
1686         }
1687
1688         result = dns_master_dumpnodetostream(mctx, db, version, node, name,
1689                                              style, f);
1690
1691         result = isc_stdio_close(f);
1692         if (result != ISC_R_SUCCESS) {
1693                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1694                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1695                               "dumping master file: %s: close: %s", filename,
1696                               isc_result_totext(result));
1697                 return (ISC_R_UNEXPECTED);
1698         }
1699
1700         return (result);
1701 }
1702
1703 isc_result_t
1704 dns_master_stylecreate(dns_master_style_t **stylep, unsigned int flags,
1705                        unsigned int ttl_column, unsigned int class_column,
1706                        unsigned int type_column, unsigned int rdata_column,
1707                        unsigned int line_length, unsigned int tab_width,
1708                        isc_mem_t *mctx)
1709 {
1710         dns_master_style_t *style;
1711
1712         REQUIRE(stylep != NULL && *stylep == NULL);
1713         style = isc_mem_get(mctx, sizeof(*style));
1714         if (style == NULL)
1715                 return (ISC_R_NOMEMORY);
1716
1717         style->flags = flags;
1718         style->ttl_column = ttl_column;
1719         style->class_column = class_column;
1720         style->type_column = type_column;
1721         style->rdata_column = rdata_column;
1722         style->line_length = line_length;
1723         style->tab_width = tab_width;
1724
1725         *stylep = style;
1726         return (ISC_R_SUCCESS);
1727 }
1728
1729 void
1730 dns_master_styledestroy(dns_master_style_t **stylep, isc_mem_t *mctx) {
1731         dns_master_style_t *style;
1732
1733         REQUIRE(stylep != NULL && *stylep != NULL);
1734         style = *stylep;
1735         *stylep = NULL;
1736         isc_mem_put(mctx, style, sizeof(*style));
1737 }