rdataset.c 7.69 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

Michael Graff's avatar
Michael Graff committed
18
19
#include <config.h>

Bob Halley's avatar
Bob Halley committed
20
#include <stddef.h>
Bob Halley's avatar
Bob Halley committed
21
#include <string.h>
Bob Halley's avatar
Bob Halley committed
22
23
24

#include <isc/assertions.h>

Bob Halley's avatar
Bob Halley committed
25
#include <dns/rdata.h>
26
27
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
Bob Halley's avatar
Bob Halley committed
28
29
30
31
32
33
34
35
36
37
38
#include <dns/rdataset.h>

void
dns_rdataset_init(dns_rdataset_t *rdataset) {

	/*
	 * Make 'rdataset' a valid, disassociated rdataset.
	 */

	REQUIRE(rdataset != NULL);

Bob Halley's avatar
Bob Halley committed
39
	rdataset->magic = DNS_RDATASET_MAGIC;
Bob Halley's avatar
Bob Halley committed
40
41
42
43
44
45
46
47
	rdataset->methods = NULL;
	ISC_LINK_INIT(rdataset, link);
	rdataset->class = 0;
	rdataset->type = 0;
	rdataset->ttl = 0;
	rdataset->private1 = NULL;
	rdataset->private2 = NULL;
	rdataset->private3 = NULL;
Bob Halley's avatar
Bob Halley committed
48
49
	rdataset->private4 = NULL;
	rdataset->private5 = NULL;
Bob Halley's avatar
Bob Halley committed
50
51
52
53
54
55
56
57
58
}

void
dns_rdataset_invalidate(dns_rdataset_t *rdataset) {

	/*
	 * Invalidate 'rdataset'.
	 */

Bob Halley's avatar
Bob Halley committed
59
	REQUIRE(DNS_RDATASET_VALID(rdataset));
Bob Halley's avatar
Bob Halley committed
60
61
62
63
64
65
66
67
68
69
	REQUIRE(rdataset->methods == NULL);
	
	rdataset->magic = 0;
	ISC_LINK_INIT(rdataset, link);
	rdataset->class = 0;
	rdataset->type = 0;
	rdataset->ttl = 0;
	rdataset->private1 = NULL;
	rdataset->private2 = NULL;
	rdataset->private3 = NULL;
Bob Halley's avatar
Bob Halley committed
70
71
	rdataset->private4 = NULL;
	rdataset->private5 = NULL;
Bob Halley's avatar
Bob Halley committed
72
73
74
75
76
77
}

void
dns_rdataset_disassociate(dns_rdataset_t *rdataset) {

	/*
Bob Halley's avatar
Bob Halley committed
78
	 * Disassociate 'rdataset' from its rdata, allowing it to be reused.
Bob Halley's avatar
Bob Halley committed
79
80
	 */

Bob Halley's avatar
Bob Halley committed
81
	REQUIRE(DNS_RDATASET_VALID(rdataset));
Bob Halley's avatar
Bob Halley committed
82
83
84
85
86
87
88
89
90
91

	(rdataset->methods->disassociate)(rdataset);
	rdataset->methods = NULL;
	ISC_LINK_INIT(rdataset, link);
	rdataset->class = 0;
	rdataset->type = 0;
	rdataset->ttl = 0;
	rdataset->private1 = NULL;
	rdataset->private2 = NULL;
	rdataset->private3 = NULL;
Bob Halley's avatar
Bob Halley committed
92
93
	rdataset->private4 = NULL;
	rdataset->private5 = NULL;
Bob Halley's avatar
Bob Halley committed
94
95
96
97
98
99
100
101
102
}

dns_result_t
dns_rdataset_first(dns_rdataset_t *rdataset) {

	/*
	 * Move the rdata cursor to the first rdata in the rdataset (if any).
	 */

Bob Halley's avatar
Bob Halley committed
103
	REQUIRE(DNS_RDATASET_VALID(rdataset));
Bob Halley's avatar
Bob Halley committed
104
105
106
107
108
109
110
111
112
113
114

	return ((rdataset->methods->first)(rdataset));
}

dns_result_t
dns_rdataset_next(dns_rdataset_t *rdataset) {

	/*
	 * Move the rdata cursor to the next rdata in the rdataset (if any).
	 */

Bob Halley's avatar
Bob Halley committed
115
	REQUIRE(DNS_RDATASET_VALID(rdataset));
Bob Halley's avatar
Bob Halley committed
116
117
118
119
120
121
122
123
124
125
126

	return ((rdataset->methods->next)(rdataset));
}

void
dns_rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {

	/*
	 * Make 'rdata' refer to the current rdata.
	 */

Bob Halley's avatar
Bob Halley committed
127
	REQUIRE(DNS_RDATASET_VALID(rdataset));
Bob Halley's avatar
Bob Halley committed
128
129
130
131

	(rdataset->methods->current)(rdataset, rdata);
}

Bob Halley's avatar
Bob Halley committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
static char *tabs = "\t\t\t\t\t\t\t\t\t\t";

static inline int
tabs_needed(unsigned int current_offset, unsigned int desired_offset) {
	unsigned int needed;
	unsigned int spaces;

	/*
	 * Assumes tabs are 8 characters.
	 */

	if (current_offset >= desired_offset)
		return (1);
	spaces = desired_offset - current_offset;
	needed = spaces / 8;
	if (spaces % 8 != 0)
		needed++;
	if (needed > 10)
		needed = 10;
	return (needed);
}

Bob Halley's avatar
Bob Halley committed
154
155
156
157
158
159
dns_result_t
dns_rdataset_totext(dns_rdataset_t *rdataset,
		    dns_name_t *owner_name,
		    isc_boolean_t omit_final_dot,
		    isc_buffer_t *target)
{
Bob Halley's avatar
Bob Halley committed
160
	dns_result_t result;
161
	unsigned int common_start, common_length, length, ntabs, ttabs;
Bob Halley's avatar
Bob Halley committed
162
	char *common;
Bob Halley's avatar
Bob Halley committed
163
164
165
	dns_rdata_t rdata;
	isc_boolean_t first = ISC_TRUE;
	isc_region_t r;
166
	char ttl[64];
Bob Halley's avatar
Bob Halley committed
167
168
169
170
171

	/*
	 * Convert 'rdataset' to text format, storing the result in 'target'.
	 */

Bob Halley's avatar
Bob Halley committed
172
	REQUIRE(DNS_RDATASET_VALID(rdataset));
Bob Halley's avatar
Bob Halley committed
173
174
	result = dns_rdataset_first(rdataset);
	REQUIRE(result == DNS_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
175

Bob Halley's avatar
Bob Halley committed
176
177
178
179
	/*
	 * XXX Explicit buffer structure references here.  Improve buffer
	 * API.
	 */
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	common_start = target->used;
	/*
	 * The caller might want to give us an empty owner
	 * name (e.g. if they are outputting into a master
	 * file and this rdataset has the same name as the
	 * previous one.)
	 */
	if (dns_name_countlabels(owner_name) != 0) {
		result = dns_name_totext(owner_name,
					 omit_final_dot,
					 target);
		if (result != DNS_R_SUCCESS)
			return (result);
	}
	common = (char *)target->base + common_start;
	common_length = target->used - common_start;
	ntabs = tabs_needed(common_length, 24);
	ttabs = ntabs;
	isc_buffer_available(target, &r);
	if (r.length < ntabs)
		return (DNS_R_NOSPACE);
	memcpy(r.base, tabs, ntabs);
	isc_buffer_add(target, ntabs);
	/*
	 * XXX The following sprintf() is safe, but it
	 * would still be good to use snprintf if we had it.
	 */
	length = sprintf(ttl, "%u ", rdataset->ttl);
	INSIST(length <= sizeof ttl);
	isc_buffer_available(target, &r);
	if (r.length < length)
		return (DNS_R_NOSPACE);
	memcpy(r.base, ttl, length);
	isc_buffer_add(target, length);
	result = dns_rdataclass_totext(rdataset->class, target);
	if (result != DNS_R_SUCCESS)
		return (result);
	isc_buffer_available(target, &r);
	if (r.length == 0)
		return (DNS_R_NOSPACE);
	*r.base = ' ';
	isc_buffer_add(target, 1);
	result = dns_rdatatype_totext(rdataset->type, target);
	if (result != DNS_R_SUCCESS)
		return (result);
	common_length = target->used - common_start;
	ntabs = tabs_needed(common_length + ttabs * 7, 40);
	ttabs += ntabs;
	isc_buffer_available(target, &r);
	if (r.length < ntabs)
		return (DNS_R_NOSPACE);
	memcpy(r.base, tabs, ntabs);
	isc_buffer_add(target, ntabs);
	common_length = target->used - common_start;

Bob Halley's avatar
Bob Halley committed
235
	do {
236
		if (!first) {
Bob Halley's avatar
Bob Halley committed
237
238
239
240
241
			isc_buffer_available(target, &r);
			if (r.length < common_length)
				return (DNS_R_NOSPACE);
			memcpy(r.base, common, common_length);
			isc_buffer_add(target, common_length);
242
243
		} else
			first = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
244
245

		dns_rdataset_current(rdataset, &rdata);
246
		result = dns_rdata_totext(&rdata, target);
Bob Halley's avatar
Bob Halley committed
247
248
		if (result != DNS_R_SUCCESS)
			return (result);
Bob Halley's avatar
Bob Halley committed
249
250
251
252
253
254
255
256
257
258
259
260
261
		isc_buffer_available(target, &r);
		if (r.length < 1)
			return (DNS_R_NOSPACE);
		memcpy(r.base, "\n", 1);
		isc_buffer_add(target, 1);

		result = dns_rdataset_next(rdataset);
	} while (result == DNS_R_SUCCESS);

	if (result != DNS_R_NOMORE)
		return (result);

	return (DNS_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
262
263
264
265
266
267
268
269
}

dns_result_t
dns_rdataset_towire(dns_rdataset_t *rdataset,
		    dns_name_t *owner_name,
		    dns_compress_t *cctx,
		    isc_buffer_t *target)
{
270
271
272
	dns_rdata_t rdata;
	isc_region_t r;
	dns_result_t result;
Bob Halley's avatar
Bob Halley committed
273
274
275
276
277
278

	/*
	 * Convert 'rdataset' to wire format, compressing names as specified
	 * in cctx, and storing the result in 'target'.
	 */

Bob Halley's avatar
Bob Halley committed
279
	REQUIRE(DNS_RDATASET_VALID(rdataset));
280
281
	result = dns_rdataset_first(rdataset);
	REQUIRE(result == DNS_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
282

283
284
285
286
287
288
289
290
291
292
293
294
295
	do {
		/*
		 * copy out the name, type, class, ttl.
		 */
		result = dns_name_towire(owner_name, cctx, target);
		if (result != DNS_R_SUCCESS)
			return (result);
		isc_buffer_available(target, &r);
		if (r.length < (sizeof(dns_rdataclass_t)
				+ sizeof(dns_rdatatype_t)
				+ sizeof(dns_ttl_t)
				+ 2)) /* XXX 2? it's for the rdata length */
			return (DNS_R_NOSPACE);
296
297
298
299
		isc_buffer_putuint16(target, rdataset->type);
		isc_buffer_putuint16(target, rdataset->class);
		isc_buffer_putuint32(target, rdataset->ttl);

300
301
302
303
		/*
		 * copy out the rdata length
		 */
		dns_rdataset_current(rdataset, &rdata);
304
		isc_buffer_putuint16(target, rdata.length);
Bob Halley's avatar
Bob Halley committed
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
		/*
		 * copy out the rdata
		 */
		result = dns_rdata_towire(&rdata, cctx, target);
		if (result != DNS_R_SUCCESS)
			return (result);

		result = dns_rdataset_next(rdataset);
	} while (result == DNS_R_SUCCESS);

	if (result != DNS_R_NOMORE)
		return (result);

	return (DNS_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
320
}