No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / diff.c
blob74c4b89a578299283d89d77aface40a56e7e1b26
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: diff.c,v 1.23 2009/12/01 00:47:09 each Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stdlib.h>
28 #include <isc/buffer.h>
29 #include <isc/file.h>
30 #include <isc/mem.h>
31 #include <isc/string.h>
32 #include <isc/util.h>
34 #include <dns/db.h>
35 #include <dns/diff.h>
36 #include <dns/log.h>
37 #include <dns/rdataclass.h>
38 #include <dns/rdatalist.h>
39 #include <dns/rdataset.h>
40 #include <dns/rdatastruct.h>
41 #include <dns/rdatatype.h>
42 #include <dns/result.h>
44 #define CHECK(op) \
45 do { result = (op); \
46 if (result != ISC_R_SUCCESS) goto failure; \
47 } while (0)
49 #define DIFF_COMMON_LOGARGS \
50 dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_DIFF
52 static dns_rdatatype_t
53 rdata_covers(dns_rdata_t *rdata) {
54 return (rdata->type == dns_rdatatype_rrsig ?
55 dns_rdata_covers(rdata) : 0);
58 isc_result_t
59 dns_difftuple_create(isc_mem_t *mctx,
60 dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
61 dns_rdata_t *rdata, dns_difftuple_t **tp)
63 dns_difftuple_t *t;
64 unsigned int size;
65 unsigned char *datap;
67 REQUIRE(tp != NULL && *tp == NULL);
70 * Create a new tuple. The variable-size wire-format name data and
71 * rdata immediately follow the dns_difftuple_t structure
72 * in memory.
74 size = sizeof(*t) + name->length + rdata->length;
75 t = isc_mem_allocate(mctx, size);
76 if (t == NULL)
77 return (ISC_R_NOMEMORY);
78 t->mctx = mctx;
79 t->op = op;
81 datap = (unsigned char *)(t + 1);
83 memcpy(datap, name->ndata, name->length);
84 dns_name_init(&t->name, NULL);
85 dns_name_clone(name, &t->name);
86 t->name.ndata = datap;
87 datap += name->length;
89 t->ttl = ttl;
91 memcpy(datap, rdata->data, rdata->length);
92 dns_rdata_init(&t->rdata);
93 dns_rdata_clone(rdata, &t->rdata);
94 t->rdata.data = datap;
95 datap += rdata->length;
97 ISC_LINK_INIT(&t->rdata, link);
98 ISC_LINK_INIT(t, link);
99 t->magic = DNS_DIFFTUPLE_MAGIC;
101 INSIST(datap == (unsigned char *)t + size);
103 *tp = t;
104 return (ISC_R_SUCCESS);
107 void
108 dns_difftuple_free(dns_difftuple_t **tp) {
109 dns_difftuple_t *t = *tp;
110 REQUIRE(DNS_DIFFTUPLE_VALID(t));
111 dns_name_invalidate(&t->name);
112 t->magic = 0;
113 isc_mem_free(t->mctx, t);
114 *tp = NULL;
117 isc_result_t
118 dns_difftuple_copy(dns_difftuple_t *orig, dns_difftuple_t **copyp) {
119 return (dns_difftuple_create(orig->mctx, orig->op, &orig->name,
120 orig->ttl, &orig->rdata, copyp));
123 void
124 dns_diff_init(isc_mem_t *mctx, dns_diff_t *diff) {
125 diff->mctx = mctx;
126 diff->resign = 0;
127 ISC_LIST_INIT(diff->tuples);
128 diff->magic = DNS_DIFF_MAGIC;
131 void
132 dns_diff_clear(dns_diff_t *diff) {
133 dns_difftuple_t *t;
134 REQUIRE(DNS_DIFF_VALID(diff));
135 while ((t = ISC_LIST_HEAD(diff->tuples)) != NULL) {
136 ISC_LIST_UNLINK(diff->tuples, t, link);
137 dns_difftuple_free(&t);
139 ENSURE(ISC_LIST_EMPTY(diff->tuples));
142 void
143 dns_diff_append(dns_diff_t *diff, dns_difftuple_t **tuplep)
145 ISC_LIST_APPEND(diff->tuples, *tuplep, link);
146 *tuplep = NULL;
149 /* XXX this is O(N) */
151 void
152 dns_diff_appendminimal(dns_diff_t *diff, dns_difftuple_t **tuplep)
154 dns_difftuple_t *ot, *next_ot;
156 REQUIRE(DNS_DIFF_VALID(diff));
157 REQUIRE(DNS_DIFFTUPLE_VALID(*tuplep));
160 * Look for an existing tuple with the same owner name,
161 * rdata, and TTL. If we are doing an addition and find a
162 * deletion or vice versa, remove both the old and the
163 * new tuple since they cancel each other out (assuming
164 * that we never delete nonexistent data or add existing
165 * data).
167 * If we find an old update of the same kind as
168 * the one we are doing, there must be a programming
169 * error. We report it but try to continue anyway.
171 for (ot = ISC_LIST_HEAD(diff->tuples); ot != NULL;
172 ot = next_ot)
174 next_ot = ISC_LIST_NEXT(ot, link);
175 if (dns_name_equal(&ot->name, &(*tuplep)->name) &&
176 dns_rdata_compare(&ot->rdata, &(*tuplep)->rdata) == 0 &&
177 ot->ttl == (*tuplep)->ttl)
179 ISC_LIST_UNLINK(diff->tuples, ot, link);
180 if ((*tuplep)->op == ot->op) {
181 UNEXPECTED_ERROR(__FILE__, __LINE__,
182 "unexpected non-minimal diff");
183 } else {
184 dns_difftuple_free(tuplep);
186 dns_difftuple_free(&ot);
187 break;
191 if (*tuplep != NULL) {
192 ISC_LIST_APPEND(diff->tuples, *tuplep, link);
193 *tuplep = NULL;
196 ENSURE(*tuplep == NULL);
199 static isc_stdtime_t
200 setresign(dns_rdataset_t *modified, isc_uint32_t delta) {
201 dns_rdata_t rdata = DNS_RDATA_INIT;
202 dns_rdata_rrsig_t sig;
203 isc_stdtime_t when;
204 isc_result_t result;
206 result = dns_rdataset_first(modified);
207 INSIST(result == ISC_R_SUCCESS);
208 dns_rdataset_current(modified, &rdata);
209 (void)dns_rdata_tostruct(&rdata, &sig, NULL);
210 if ((rdata.flags & DNS_RDATA_OFFLINE) != 0)
211 when = 0;
212 else
213 when = sig.timeexpire - delta;
214 dns_rdata_reset(&rdata);
216 result = dns_rdataset_next(modified);
217 while (result == ISC_R_SUCCESS) {
218 dns_rdataset_current(modified, &rdata);
219 (void)dns_rdata_tostruct(&rdata, &sig, NULL);
220 if ((rdata.flags & DNS_RDATA_OFFLINE) != 0) {
221 goto next_rr;
223 if (when == 0 || sig.timeexpire - delta < when)
224 when = sig.timeexpire - delta;
225 next_rr:
226 dns_rdata_reset(&rdata);
227 result = dns_rdataset_next(modified);
229 INSIST(result == ISC_R_NOMORE);
230 return (when);
233 static isc_result_t
234 diff_apply(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *ver,
235 isc_boolean_t warn)
237 dns_difftuple_t *t;
238 dns_dbnode_t *node = NULL;
239 isc_result_t result;
240 char namebuf[DNS_NAME_FORMATSIZE];
241 char typebuf[DNS_RDATATYPE_FORMATSIZE];
242 char classbuf[DNS_RDATACLASS_FORMATSIZE];
244 REQUIRE(DNS_DIFF_VALID(diff));
245 REQUIRE(DNS_DB_VALID(db));
247 t = ISC_LIST_HEAD(diff->tuples);
248 while (t != NULL) {
249 dns_name_t *name;
251 INSIST(node == NULL);
252 name = &t->name;
254 * Find the node.
255 * We create the node if it does not exist.
256 * This will cause an empty node to be created if the diff
257 * contains a deletion of an RR at a nonexistent name,
258 * but such diffs should never be created in the first
259 * place.
262 while (t != NULL && dns_name_equal(&t->name, name)) {
263 dns_rdatatype_t type, covers;
264 dns_diffop_t op;
265 dns_rdatalist_t rdl;
266 dns_rdataset_t rds;
267 dns_rdataset_t ardataset;
268 dns_rdataset_t *modified = NULL;
269 isc_boolean_t offline;
271 op = t->op;
272 type = t->rdata.type;
273 covers = rdata_covers(&t->rdata);
276 * Collect a contiguous set of updates with
277 * the same operation (add/delete) and RR type
278 * into a single rdatalist so that the
279 * database rrset merging/subtraction code
280 * can work more efficiently than if each
281 * RR were merged into / subtracted from
282 * the database separately.
284 * This is done by linking rdata structures from the
285 * diff into "rdatalist". This uses the rdata link
286 * field, not the diff link field, so the structure
287 * of the diff itself is not affected.
290 rdl.type = type;
291 rdl.covers = covers;
292 rdl.rdclass = t->rdata.rdclass;
293 rdl.ttl = t->ttl;
294 ISC_LIST_INIT(rdl.rdata);
295 ISC_LINK_INIT(&rdl, link);
297 node = NULL;
298 if (type != dns_rdatatype_nsec3 &&
299 covers != dns_rdatatype_nsec3)
300 CHECK(dns_db_findnode(db, name, ISC_TRUE,
301 &node));
302 else
303 CHECK(dns_db_findnsec3node(db, name, ISC_TRUE,
304 &node));
306 offline = ISC_FALSE;
307 while (t != NULL &&
308 dns_name_equal(&t->name, name) &&
309 t->op == op &&
310 t->rdata.type == type &&
311 rdata_covers(&t->rdata) == covers)
313 dns_name_format(name, namebuf, sizeof(namebuf));
314 dns_rdatatype_format(t->rdata.type, typebuf,
315 sizeof(typebuf));
316 dns_rdataclass_format(t->rdata.rdclass,
317 classbuf,
318 sizeof(classbuf));
319 if (t->ttl != rdl.ttl && warn)
320 isc_log_write(DIFF_COMMON_LOGARGS,
321 ISC_LOG_WARNING,
322 "'%s/%s/%s': TTL differs in "
323 "rdataset, adjusting "
324 "%lu -> %lu",
325 namebuf, typebuf, classbuf,
326 (unsigned long) t->ttl,
327 (unsigned long) rdl.ttl);
328 if (t->rdata.flags & DNS_RDATA_OFFLINE)
329 offline = ISC_TRUE;
330 ISC_LIST_APPEND(rdl.rdata, &t->rdata, link);
331 t = ISC_LIST_NEXT(t, link);
335 * Convert the rdatalist into a rdataset.
337 dns_rdataset_init(&rds);
338 CHECK(dns_rdatalist_tordataset(&rdl, &rds));
339 if (rds.type == dns_rdatatype_rrsig)
340 switch (op) {
341 case DNS_DIFFOP_ADDRESIGN:
342 case DNS_DIFFOP_DELRESIGN:
343 modified = &ardataset;
344 dns_rdataset_init(modified);
345 break;
346 default:
347 break;
349 rds.trust = dns_trust_ultimate;
352 * Merge the rdataset into the database.
354 switch (op) {
355 case DNS_DIFFOP_ADD:
356 case DNS_DIFFOP_ADDRESIGN:
357 result = dns_db_addrdataset(db, node, ver,
358 0, &rds,
359 DNS_DBADD_MERGE|
360 DNS_DBADD_EXACT|
361 DNS_DBADD_EXACTTTL,
362 modified);
363 break;
364 case DNS_DIFFOP_DEL:
365 case DNS_DIFFOP_DELRESIGN:
366 result = dns_db_subtractrdataset(db, node, ver,
367 &rds,
368 DNS_DBSUB_EXACT,
369 modified);
370 break;
371 default:
372 INSIST(0);
375 if (result == ISC_R_SUCCESS) {
376 if (modified != NULL) {
377 isc_stdtime_t resign;
378 resign = setresign(modified,
379 diff->resign);
380 dns_db_setsigningtime(db, modified,
381 resign);
383 } else if (result == DNS_R_UNCHANGED) {
385 * This will not happen when executing a
386 * dynamic update, because that code will
387 * generate strictly minimal diffs.
388 * It may happen when receiving an IXFR
389 * from a server that is not as careful.
390 * Issue a warning and continue.
392 if (warn) {
393 char classbuf[DNS_RDATATYPE_FORMATSIZE];
394 char namebuf[DNS_NAME_FORMATSIZE];
396 dns_name_format(dns_db_origin(db),
397 namebuf,
398 sizeof(namebuf));
399 dns_rdataclass_format(dns_db_class(db),
400 classbuf,
401 sizeof(classbuf));
402 isc_log_write(DIFF_COMMON_LOGARGS,
403 ISC_LOG_WARNING,
404 "%s/%s: dns_diff_apply: "
405 "update with no effect",
406 namebuf, classbuf);
408 } else if (result == DNS_R_NXRRSET) {
410 * OK.
412 } else {
413 if (modified != NULL &&
414 dns_rdataset_isassociated(modified))
415 dns_rdataset_disassociate(modified);
416 CHECK(result);
418 dns_db_detachnode(db, &node);
419 if (modified != NULL &&
420 dns_rdataset_isassociated(modified))
421 dns_rdataset_disassociate(modified);
424 return (ISC_R_SUCCESS);
426 failure:
427 if (node != NULL)
428 dns_db_detachnode(db, &node);
429 return (result);
432 isc_result_t
433 dns_diff_apply(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *ver) {
434 return (diff_apply(diff, db, ver, ISC_TRUE));
437 isc_result_t
438 dns_diff_applysilently(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *ver) {
439 return (diff_apply(diff, db, ver, ISC_FALSE));
442 /* XXX this duplicates lots of code in diff_apply(). */
444 isc_result_t
445 dns_diff_load(dns_diff_t *diff, dns_addrdatasetfunc_t addfunc,
446 void *add_private)
448 dns_difftuple_t *t;
449 isc_result_t result;
451 REQUIRE(DNS_DIFF_VALID(diff));
453 t = ISC_LIST_HEAD(diff->tuples);
454 while (t != NULL) {
455 dns_name_t *name;
457 name = &t->name;
458 while (t != NULL && dns_name_equal(&t->name, name)) {
459 dns_rdatatype_t type, covers;
460 dns_diffop_t op;
461 dns_rdatalist_t rdl;
462 dns_rdataset_t rds;
464 op = t->op;
465 type = t->rdata.type;
466 covers = rdata_covers(&t->rdata);
468 rdl.type = type;
469 rdl.covers = covers;
470 rdl.rdclass = t->rdata.rdclass;
471 rdl.ttl = t->ttl;
472 ISC_LIST_INIT(rdl.rdata);
473 ISC_LINK_INIT(&rdl, link);
475 while (t != NULL && dns_name_equal(&t->name, name) &&
476 t->op == op && t->rdata.type == type &&
477 rdata_covers(&t->rdata) == covers)
479 ISC_LIST_APPEND(rdl.rdata, &t->rdata, link);
480 t = ISC_LIST_NEXT(t, link);
484 * Convert the rdatalist into a rdataset.
486 dns_rdataset_init(&rds);
487 CHECK(dns_rdatalist_tordataset(&rdl, &rds));
488 rds.trust = dns_trust_ultimate;
490 INSIST(op == DNS_DIFFOP_ADD);
491 result = (*addfunc)(add_private, name, &rds);
492 if (result == DNS_R_UNCHANGED) {
493 isc_log_write(DIFF_COMMON_LOGARGS,
494 ISC_LOG_WARNING,
495 "dns_diff_load: "
496 "update with no effect");
497 } else if (result == ISC_R_SUCCESS ||
498 result == DNS_R_NXRRSET) {
500 * OK.
502 } else {
503 CHECK(result);
507 result = ISC_R_SUCCESS;
508 failure:
509 return (result);
513 * XXX uses qsort(); a merge sort would be more natural for lists,
514 * and perhaps safer wrt thread stack overflow.
516 isc_result_t
517 dns_diff_sort(dns_diff_t *diff, dns_diff_compare_func *compare) {
518 unsigned int length = 0;
519 unsigned int i;
520 dns_difftuple_t **v;
521 dns_difftuple_t *p;
522 REQUIRE(DNS_DIFF_VALID(diff));
524 for (p = ISC_LIST_HEAD(diff->tuples);
525 p != NULL;
526 p = ISC_LIST_NEXT(p, link))
527 length++;
528 if (length == 0)
529 return (ISC_R_SUCCESS);
530 v = isc_mem_get(diff->mctx, length * sizeof(dns_difftuple_t *));
531 if (v == NULL)
532 return (ISC_R_NOMEMORY);
533 i = 0;
534 for (i = 0; i < length; i++) {
535 p = ISC_LIST_HEAD(diff->tuples);
536 v[i] = p;
537 ISC_LIST_UNLINK(diff->tuples, p, link);
539 INSIST(ISC_LIST_HEAD(diff->tuples) == NULL);
540 qsort(v, length, sizeof(v[0]), compare);
541 for (i = 0; i < length; i++) {
542 ISC_LIST_APPEND(diff->tuples, v[i], link);
544 isc_mem_put(diff->mctx, v, length * sizeof(dns_difftuple_t *));
545 return (ISC_R_SUCCESS);
550 * Create an rdataset containing the single RR of the given
551 * tuple. The caller must allocate the rdata, rdataset and
552 * an rdatalist structure for it to refer to.
555 static isc_result_t
556 diff_tuple_tordataset(dns_difftuple_t *t, dns_rdata_t *rdata,
557 dns_rdatalist_t *rdl, dns_rdataset_t *rds)
559 REQUIRE(DNS_DIFFTUPLE_VALID(t));
560 REQUIRE(rdl != NULL);
561 REQUIRE(rds != NULL);
563 rdl->type = t->rdata.type;
564 rdl->rdclass = t->rdata.rdclass;
565 rdl->ttl = t->ttl;
566 ISC_LIST_INIT(rdl->rdata);
567 ISC_LINK_INIT(rdl, link);
568 dns_rdataset_init(rds);
569 ISC_LINK_INIT(rdata, link);
570 dns_rdata_clone(&t->rdata, rdata);
571 ISC_LIST_APPEND(rdl->rdata, rdata, link);
572 return (dns_rdatalist_tordataset(rdl, rds));
575 isc_result_t
576 dns_diff_print(dns_diff_t *diff, FILE *file) {
577 isc_result_t result;
578 dns_difftuple_t *t;
579 char *mem = NULL;
580 unsigned int size = 2048;
581 const char *op = NULL;
583 REQUIRE(DNS_DIFF_VALID(diff));
585 mem = isc_mem_get(diff->mctx, size);
586 if (mem == NULL)
587 return (ISC_R_NOMEMORY);
589 for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
590 t = ISC_LIST_NEXT(t, link))
592 isc_buffer_t buf;
593 isc_region_t r;
595 dns_rdatalist_t rdl;
596 dns_rdataset_t rds;
597 dns_rdata_t rd = DNS_RDATA_INIT;
599 result = diff_tuple_tordataset(t, &rd, &rdl, &rds);
600 if (result != ISC_R_SUCCESS) {
601 UNEXPECTED_ERROR(__FILE__, __LINE__,
602 "diff_tuple_tordataset failed: %s",
603 dns_result_totext(result));
604 result = ISC_R_UNEXPECTED;
605 goto cleanup;
607 again:
608 isc_buffer_init(&buf, mem, size);
609 result = dns_rdataset_totext(&rds, &t->name,
610 ISC_FALSE, ISC_FALSE, &buf);
612 if (result == ISC_R_NOSPACE) {
613 isc_mem_put(diff->mctx, mem, size);
614 size += 1024;
615 mem = isc_mem_get(diff->mctx, size);
616 if (mem == NULL) {
617 result = ISC_R_NOMEMORY;
618 goto cleanup;
620 goto again;
623 if (result != ISC_R_SUCCESS)
624 goto cleanup;
626 * Get rid of final newline.
628 INSIST(buf.used >= 1 &&
629 ((char *) buf.base)[buf.used-1] == '\n');
630 buf.used--;
632 isc_buffer_usedregion(&buf, &r);
633 switch (t->op) {
634 case DNS_DIFFOP_EXISTS: op = "exists"; break;
635 case DNS_DIFFOP_ADD: op = "add"; break;
636 case DNS_DIFFOP_DEL: op = "del"; break;
637 case DNS_DIFFOP_ADDRESIGN: op = "add re-sign"; break;
638 case DNS_DIFFOP_DELRESIGN: op = "del re-sign"; break;
640 if (file != NULL)
641 fprintf(file, "%s %.*s\n", op, (int) r.length,
642 (char *) r.base);
643 else
644 isc_log_write(DIFF_COMMON_LOGARGS, ISC_LOG_DEBUG(7),
645 "%s %.*s", op, (int) r.length,
646 (char *) r.base);
648 result = ISC_R_SUCCESS;
649 cleanup:
650 if (mem != NULL)
651 isc_mem_put(diff->mctx, mem, size);
652 return (result);