1135446Strhodes/*
2214586Sdougb * Copyright (C) 2004-2010  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 2000, 2001  Internet Software Consortium.
4135446Strhodes *
5193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18234010Sdougb/* $Id: diff.h,v 1.19 2010/06/04 23:51:14 tbox Exp $ */
19135446Strhodes
20135446Strhodes#ifndef DNS_DIFF_H
21135446Strhodes#define DNS_DIFF_H 1
22135446Strhodes
23135446Strhodes/*****
24135446Strhodes ***** Module Info
25135446Strhodes *****/
26135446Strhodes
27193149Sdougb/*! \file dns/diff.h
28170222Sdougb * \brief
29135446Strhodes * A diff is a convenience type representing a list of changes to be
30135446Strhodes * made to a database.
31135446Strhodes */
32135446Strhodes
33135446Strhodes/***
34135446Strhodes *** Imports
35135446Strhodes ***/
36135446Strhodes
37135446Strhodes#include <isc/lang.h>
38135446Strhodes#include <isc/magic.h>
39135446Strhodes
40135446Strhodes#include <dns/name.h>
41135446Strhodes#include <dns/rdata.h>
42135446Strhodes#include <dns/types.h>
43135446Strhodes
44135446Strhodes/***
45135446Strhodes *** Types
46135446Strhodes ***/
47135446Strhodes
48170222Sdougb/*%
49135446Strhodes * A dns_difftuple_t represents a single RR being added or deleted.
50135446Strhodes * The RR type and class are in the 'rdata' member; the class is always
51135446Strhodes * the real one, not a DynDNS meta-class, so that the rdatas can be
52135446Strhodes * compared using dns_rdata_compare().  The TTL is significant
53135446Strhodes * even for deletions, because a deletion/addition pair cannot
54135446Strhodes * be canceled out if the TTL differs (it might be an explicit
55135446Strhodes * TTL update).
56135446Strhodes *
57135446Strhodes * Tuples are also used to represent complete RRs with owner
58135446Strhodes * names for a couple of other purposes, such as the
59135446Strhodes * individual RRs of a "RRset exists (value dependent)"
60135446Strhodes * prerequisite set.  In this case, op==DNS_DIFFOP_EXISTS,
61135446Strhodes * and the TTL is ignored.
62193149Sdougb *
63193149Sdougb * DNS_DIFFOP_*RESIGN will cause the 'resign' attribute of the resulting
64193149Sdougb * RRset to be recomputed to be 'resign' seconds before the earliest RRSIG
65193149Sdougb * timeexpire.
66135446Strhodes */
67135446Strhodes
68135446Strhodestypedef enum {
69193149Sdougb	DNS_DIFFOP_ADD = 0,		/*%< Add an RR. */
70193149Sdougb	DNS_DIFFOP_DEL = 1,		/*%< Delete an RR. */
71193149Sdougb	DNS_DIFFOP_EXISTS = 2,		/*%< Assert RR existence. */
72193149Sdougb	DNS_DIFFOP_ADDRESIGN = 4,	/*%< ADD + RESIGN. */
73214586Sdougb	DNS_DIFFOP_DELRESIGN = 5	/*%< DEL + RESIGN. */
74135446Strhodes} dns_diffop_t;
75135446Strhodes
76135446Strhodestypedef struct dns_difftuple dns_difftuple_t;
77135446Strhodes
78135446Strhodes#define DNS_DIFFTUPLE_MAGIC	ISC_MAGIC('D','I','F','T')
79135446Strhodes#define DNS_DIFFTUPLE_VALID(t)	ISC_MAGIC_VALID(t, DNS_DIFFTUPLE_MAGIC)
80135446Strhodes
81135446Strhodesstruct dns_difftuple {
82193149Sdougb	unsigned int			magic;
83135446Strhodes	isc_mem_t			*mctx;
84135446Strhodes	dns_diffop_t			op;
85135446Strhodes	dns_name_t			name;
86135446Strhodes	dns_ttl_t			ttl;
87135446Strhodes	dns_rdata_t			rdata;
88135446Strhodes	ISC_LINK(dns_difftuple_t)	link;
89135446Strhodes	/* Variable-size name data and rdata follows. */
90135446Strhodes};
91135446Strhodes
92170222Sdougb/*%
93135446Strhodes * A dns_diff_t represents a set of changes being applied to
94135446Strhodes * a zone.  Diffs are also used to represent "RRset exists
95135446Strhodes * (value dependent)" prerequisites.
96135446Strhodes */
97135446Strhodestypedef struct dns_diff dns_diff_t;
98135446Strhodes
99135446Strhodes#define DNS_DIFF_MAGIC		ISC_MAGIC('D','I','F','F')
100135446Strhodes#define DNS_DIFF_VALID(t)	ISC_MAGIC_VALID(t, DNS_DIFF_MAGIC)
101135446Strhodes
102135446Strhodesstruct dns_diff {
103135446Strhodes	unsigned int			magic;
104135446Strhodes	isc_mem_t *			mctx;
105193149Sdougb	/*
106193149Sdougb	 * Set the 'resign' attribute to this many second before the
107193149Sdougb	 * earliest RRSIG timeexpire.
108193149Sdougb	 */
109193149Sdougb	isc_uint32_t			resign;
110135446Strhodes	ISC_LIST(dns_difftuple_t)	tuples;
111135446Strhodes};
112135446Strhodes
113193149Sdougb/* Type of comparison function for sorting diffs. */
114135446Strhodestypedef int dns_diff_compare_func(const void *, const void *);
115135446Strhodes
116135446Strhodes/***
117135446Strhodes *** Functions
118135446Strhodes ***/
119135446Strhodes
120135446StrhodesISC_LANG_BEGINDECLS
121135446Strhodes
122135446Strhodes/**************************************************************************/
123135446Strhodes/*
124193149Sdougb * Manipulation of diffs and tuples.
125135446Strhodes */
126135446Strhodes
127135446Strhodesisc_result_t
128135446Strhodesdns_difftuple_create(isc_mem_t *mctx,
129135446Strhodes		     dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
130135446Strhodes		     dns_rdata_t *rdata, dns_difftuple_t **tp);
131170222Sdougb/*%<
132135446Strhodes * Create a tuple.  Deep copies are made of the name and rdata, so
133135446Strhodes * they need not remain valid after the call.
134135446Strhodes *
135135446Strhodes * Requires:
136170222Sdougb *\li	*tp != NULL && *tp == NULL.
137135446Strhodes *
138135446Strhodes * Returns:
139170222Sdougb *\li	ISC_R_SUCCESS
140170222Sdougb *  \li    ISC_R_NOMEMORY
141135446Strhodes */
142135446Strhodes
143135446Strhodesvoid
144135446Strhodesdns_difftuple_free(dns_difftuple_t **tp);
145170222Sdougb/*%<
146135446Strhodes * Free a tuple.
147135446Strhodes *
148135446Strhodes * Requires:
149170222Sdougb *    \li   **tp is a valid tuple.
150135446Strhodes *
151135446Strhodes * Ensures:
152170222Sdougb *     \li  *tp == NULL
153170222Sdougb *      \li All memory used by the tuple is freed.
154135446Strhodes */
155135446Strhodes
156135446Strhodesisc_result_t
157135446Strhodesdns_difftuple_copy(dns_difftuple_t *orig, dns_difftuple_t **copyp);
158170222Sdougb/*%<
159135446Strhodes * Copy a tuple.
160135446Strhodes *
161135446Strhodes * Requires:
162170222Sdougb * \li	'orig' points to a valid tuple
163170222Sdougb *\li	copyp != NULL && *copyp == NULL
164135446Strhodes */
165135446Strhodes
166135446Strhodesvoid
167135446Strhodesdns_diff_init(isc_mem_t *mctx, dns_diff_t *diff);
168170222Sdougb/*%<
169135446Strhodes * Initialize a diff.
170135446Strhodes *
171135446Strhodes * Requires:
172170222Sdougb * \li   'diff' points to an uninitialized dns_diff_t
173170222Sdougb *  \li  allocated by the caller.
174135446Strhodes *
175135446Strhodes * Ensures:
176170222Sdougb * \li   '*diff' is a valid, empty diff.
177135446Strhodes */
178135446Strhodes
179135446Strhodesvoid
180135446Strhodesdns_diff_clear(dns_diff_t *diff);
181170222Sdougb/*%<
182135446Strhodes * Clear a diff, destroying all its tuples.
183135446Strhodes *
184135446Strhodes * Requires:
185170222Sdougb * \li   'diff' points to a valid dns_diff_t.
186135446Strhodes *
187135446Strhodes * Ensures:
188170222Sdougb * \li    Any tuples in the diff are destroyed.
189135446Strhodes *     The diff now empty, but it is still valid
190135446Strhodes *     and may be reused without calling dns_diff_init
191135446Strhodes *     again.  The only memory used is that of the
192135446Strhodes *     dns_diff_t structure itself.
193135446Strhodes *
194135446Strhodes * Notes:
195170222Sdougb * \li    Managing the memory of the dns_diff_t structure itself
196135446Strhodes *     is the caller's responsibility.
197135446Strhodes */
198135446Strhodes
199135446Strhodesvoid
200135446Strhodesdns_diff_append(dns_diff_t *diff, dns_difftuple_t **tuple);
201170222Sdougb/*%<
202135446Strhodes * Append a single tuple to a diff.
203135446Strhodes *
204170222Sdougb *\li	'diff' is a valid diff.
205170222Sdougb * \li	'*tuple' is a valid tuple.
206135446Strhodes *
207135446Strhodes * Ensures:
208170222Sdougb *\li	*tuple is NULL.
209170222Sdougb *\li	The tuple has been freed, or will be freed when the diff is cleared.
210135446Strhodes */
211135446Strhodes
212135446Strhodesvoid
213135446Strhodesdns_diff_appendminimal(dns_diff_t *diff, dns_difftuple_t **tuple);
214170222Sdougb/*%<
215135446Strhodes * Append 'tuple' to 'diff', removing any duplicate
216135446Strhodes * or conflicting updates as needed to create a minimal diff.
217135446Strhodes *
218135446Strhodes * Requires:
219170222Sdougb *\li	'diff' is a minimal diff.
220135446Strhodes *
221135446Strhodes * Ensures:
222170222Sdougb *\li	'diff' is still a minimal diff.
223170222Sdougb *  \li 	*tuple is NULL.
224170222Sdougb *   \li	The tuple has been freed, or will be freed when the diff is cleared.
225135446Strhodes *
226135446Strhodes */
227135446Strhodes
228135446Strhodesisc_result_t
229135446Strhodesdns_diff_sort(dns_diff_t *diff, dns_diff_compare_func *compare);
230170222Sdougb/*%<
231135446Strhodes * Sort 'diff' in-place according to the comparison function 'compare'.
232135446Strhodes */
233135446Strhodes
234135446Strhodesisc_result_t
235135446Strhodesdns_diff_apply(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *ver);
236135446Strhodesisc_result_t
237135446Strhodesdns_diff_applysilently(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *ver);
238170222Sdougb/*%<
239135446Strhodes * Apply 'diff' to the database 'db'.
240135446Strhodes *
241135446Strhodes * dns_diff_apply() logs warnings about updates with no effect or
242135446Strhodes * with inconsistent TTLs; dns_diff_applysilently() does not.
243135446Strhodes *
244135446Strhodes * For efficiency, the diff should be sorted by owner name.
245135446Strhodes * If it is not sorted, operation will still be correct,
246135446Strhodes * but less efficient.
247135446Strhodes *
248135446Strhodes * Requires:
249170222Sdougb *\li	*diff is a valid diff (possibly empty), containing
250170222Sdougb *   	tuples of type #DNS_DIFFOP_ADD and/or
251170222Sdougb *  	For #DNS_DIFFOP_DEL tuples, the TTL is ignored.
252135446Strhodes *
253135446Strhodes */
254135446Strhodes
255135446Strhodesisc_result_t
256135446Strhodesdns_diff_load(dns_diff_t *diff, dns_addrdatasetfunc_t addfunc,
257135446Strhodes	      void *add_private);
258170222Sdougb/*%<
259135446Strhodes * Like dns_diff_apply, but for use when loading a new database
260135446Strhodes * instead of modifying an existing one.  This bypasses the
261135446Strhodes * database transaction mechanisms.
262135446Strhodes *
263135446Strhodes * Requires:
264170222Sdougb *\li 	'addfunc' is a valid dns_addradatasetfunc_t obtained from
265135446Strhodes * 	dns_db_beginload()
266135446Strhodes *
267170222Sdougb *\li	'add_private' points to a corresponding dns_dbload_t *
268135446Strhodes *      (XXX why is it a void pointer, then?)
269135446Strhodes */
270135446Strhodes
271135446Strhodesisc_result_t
272135446Strhodesdns_diff_print(dns_diff_t *diff, FILE *file);
273135446Strhodes
274170222Sdougb/*%<
275135446Strhodes * Print the differences to 'file' or if 'file' is NULL via the
276135446Strhodes * logging system.
277135446Strhodes *
278135446Strhodes * Require:
279170222Sdougb *\li	'diff' to be valid.
280170222Sdougb *\li	'file' to refer to a open file or NULL.
281135446Strhodes *
282135446Strhodes * Returns:
283170222Sdougb *\li	#ISC_R_SUCCESS
284170222Sdougb *\li	#ISC_R_NOMEMORY
285170222Sdougb *\li	#ISC_R_UNEXPECTED
286170222Sdougb *\li	any error from dns_rdataset_totext()
287135446Strhodes */
288135446Strhodes
289135446StrhodesISC_LANG_ENDDECLS
290135446Strhodes
291135446Strhodes#endif /* DNS_DIFF_H */
292