PageRenderTime 42ms CodeModel.GetById 14ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/bind9/lib/dns/rdata/generic/soa_6.c

https://bitbucket.org/freebsd/freebsd-head/
C | 449 lines | 328 code | 102 blank | 19 comment | 65 complexity | a7ee339973f54cef3660969c9e7cd967 MD5 | raw file
  1/*
  2 * Copyright (C) 2004, 2007, 2009, 2012  Internet Systems Consortium, Inc. ("ISC")
  3 * Copyright (C) 1998-2002  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$ */
 19
 20/* Reviewed: Thu Mar 16 15:18:32 PST 2000 by explorer */
 21
 22#ifndef RDATA_GENERIC_SOA_6_C
 23#define RDATA_GENERIC_SOA_6_C
 24
 25#define RRTYPE_SOA_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON)
 26
 27static inline isc_result_t
 28fromtext_soa(ARGS_FROMTEXT) {
 29	isc_token_t token;
 30	dns_name_t name;
 31	isc_buffer_t buffer;
 32	int i;
 33	isc_uint32_t n;
 34	isc_boolean_t ok;
 35
 36	REQUIRE(type == 6);
 37
 38	UNUSED(type);
 39	UNUSED(rdclass);
 40	UNUSED(callbacks);
 41
 42	origin = (origin != NULL) ? origin : dns_rootname;
 43
 44	for (i = 0; i < 2; i++) {
 45		RETERR(isc_lex_getmastertoken(lexer, &token,
 46					      isc_tokentype_string,
 47					      ISC_FALSE));
 48
 49		dns_name_init(&name, NULL);
 50		buffer_fromregion(&buffer, &token.value.as_region);
 51		RETTOK(dns_name_fromtext(&name, &buffer, origin,
 52					 options, target));
 53		ok = ISC_TRUE;
 54		if ((options & DNS_RDATA_CHECKNAMES) != 0)
 55			switch (i) {
 56			case 0:
 57				ok = dns_name_ishostname(&name, ISC_FALSE);
 58				break;
 59			case 1:
 60				ok = dns_name_ismailbox(&name);
 61				break;
 62
 63			}
 64		if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0)
 65			RETTOK(DNS_R_BADNAME);
 66		if (!ok && callbacks != NULL)
 67			warn_badname(&name, lexer, callbacks);
 68	}
 69
 70	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
 71				      ISC_FALSE));
 72	RETERR(uint32_tobuffer(token.value.as_ulong, target));
 73
 74	for (i = 0; i < 4; i++) {
 75		RETERR(isc_lex_getmastertoken(lexer, &token,
 76					      isc_tokentype_string,
 77					      ISC_FALSE));
 78		RETTOK(dns_counter_fromtext(&token.value.as_textregion, &n));
 79		RETERR(uint32_tobuffer(n, target));
 80	}
 81
 82	return (ISC_R_SUCCESS);
 83}
 84
 85static const char *soa_fieldnames[5] = {
 86	"serial", "refresh", "retry", "expire", "minimum"
 87};
 88
 89static inline isc_result_t
 90totext_soa(ARGS_TOTEXT) {
 91	isc_region_t dregion;
 92	dns_name_t mname;
 93	dns_name_t rname;
 94	dns_name_t prefix;
 95	isc_boolean_t sub;
 96	int i;
 97	isc_boolean_t multiline;
 98	isc_boolean_t comment;
 99
100	REQUIRE(rdata->type == 6);
101	REQUIRE(rdata->length != 0);
102
103	multiline = ISC_TF((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0);
104	if (multiline)
105		comment = ISC_TF((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0);
106	else
107		comment = ISC_FALSE;
108
109
110	dns_name_init(&mname, NULL);
111	dns_name_init(&rname, NULL);
112	dns_name_init(&prefix, NULL);
113
114	dns_rdata_toregion(rdata, &dregion);
115
116	dns_name_fromregion(&mname, &dregion);
117	isc_region_consume(&dregion, name_length(&mname));
118
119	dns_name_fromregion(&rname, &dregion);
120	isc_region_consume(&dregion, name_length(&rname));
121
122	sub = name_prefix(&mname, tctx->origin, &prefix);
123	RETERR(dns_name_totext(&prefix, sub, target));
124
125	RETERR(str_totext(" ", target));
126
127	sub = name_prefix(&rname, tctx->origin, &prefix);
128	RETERR(dns_name_totext(&prefix, sub, target));
129
130	if (multiline)
131		RETERR(str_totext(" (" , target));
132	RETERR(str_totext(tctx->linebreak, target));
133
134	for (i = 0; i < 5; i++) {
135		char buf[sizeof("0123456789 ; ")];
136		unsigned long num;
137		num = uint32_fromregion(&dregion);
138		isc_region_consume(&dregion, 4);
139		sprintf(buf, comment ? "%-10lu ; " : "%lu", num);
140		RETERR(str_totext(buf, target));
141		if (comment) {
142			RETERR(str_totext(soa_fieldnames[i], target));
143			/* Print times in week/day/hour/minute/second form */
144			if (i >= 1) {
145				RETERR(str_totext(" (", target));
146				RETERR(dns_ttl_totext(num, ISC_TRUE, target));
147				RETERR(str_totext(")", target));
148			}
149			RETERR(str_totext(tctx->linebreak, target));
150		} else if (i < 4) {
151			RETERR(str_totext(tctx->linebreak, target));
152		}
153	}
154
155	if (multiline)
156		RETERR(str_totext(")", target));
157
158	return (ISC_R_SUCCESS);
159}
160
161static inline isc_result_t
162fromwire_soa(ARGS_FROMWIRE) {
163	dns_name_t mname;
164	dns_name_t rname;
165	isc_region_t sregion;
166	isc_region_t tregion;
167
168	REQUIRE(type == 6);
169
170	UNUSED(type);
171	UNUSED(rdclass);
172
173	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
174
175	dns_name_init(&mname, NULL);
176	dns_name_init(&rname, NULL);
177
178	RETERR(dns_name_fromwire(&mname, source, dctx, options, target));
179	RETERR(dns_name_fromwire(&rname, source, dctx, options, target));
180
181	isc_buffer_activeregion(source, &sregion);
182	isc_buffer_availableregion(target, &tregion);
183
184	if (sregion.length < 20)
185		return (ISC_R_UNEXPECTEDEND);
186	if (tregion.length < 20)
187		return (ISC_R_NOSPACE);
188
189	memcpy(tregion.base, sregion.base, 20);
190	isc_buffer_forward(source, 20);
191	isc_buffer_add(target, 20);
192
193	return (ISC_R_SUCCESS);
194}
195
196static inline isc_result_t
197towire_soa(ARGS_TOWIRE) {
198	isc_region_t sregion;
199	isc_region_t tregion;
200	dns_name_t mname;
201	dns_name_t rname;
202	dns_offsets_t moffsets;
203	dns_offsets_t roffsets;
204
205	REQUIRE(rdata->type == 6);
206	REQUIRE(rdata->length != 0);
207
208	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
209
210	dns_name_init(&mname, moffsets);
211	dns_name_init(&rname, roffsets);
212
213	dns_rdata_toregion(rdata, &sregion);
214
215	dns_name_fromregion(&mname, &sregion);
216	isc_region_consume(&sregion, name_length(&mname));
217	RETERR(dns_name_towire(&mname, cctx, target));
218
219	dns_name_fromregion(&rname, &sregion);
220	isc_region_consume(&sregion, name_length(&rname));
221	RETERR(dns_name_towire(&rname, cctx, target));
222
223	isc_buffer_availableregion(target, &tregion);
224	if (tregion.length < 20)
225		return (ISC_R_NOSPACE);
226
227	memcpy(tregion.base, sregion.base, 20);
228	isc_buffer_add(target, 20);
229	return (ISC_R_SUCCESS);
230}
231
232static inline int
233compare_soa(ARGS_COMPARE) {
234	isc_region_t region1;
235	isc_region_t region2;
236	dns_name_t name1;
237	dns_name_t name2;
238	int order;
239
240	REQUIRE(rdata1->type == rdata2->type);
241	REQUIRE(rdata1->rdclass == rdata2->rdclass);
242	REQUIRE(rdata1->type == 6);
243	REQUIRE(rdata1->length != 0);
244	REQUIRE(rdata2->length != 0);
245
246	dns_name_init(&name1, NULL);
247	dns_name_init(&name2, NULL);
248
249	dns_rdata_toregion(rdata1, &region1);
250	dns_rdata_toregion(rdata2, &region2);
251
252	dns_name_fromregion(&name1, &region1);
253	dns_name_fromregion(&name2, &region2);
254
255	order = dns_name_rdatacompare(&name1, &name2);
256	if (order != 0)
257		return (order);
258
259	isc_region_consume(&region1, name_length(&name1));
260	isc_region_consume(&region2, name_length(&name2));
261
262	dns_name_init(&name1, NULL);
263	dns_name_init(&name2, NULL);
264
265	dns_name_fromregion(&name1, &region1);
266	dns_name_fromregion(&name2, &region2);
267
268	order = dns_name_rdatacompare(&name1, &name2);
269	if (order != 0)
270		return (order);
271
272	isc_region_consume(&region1, name_length(&name1));
273	isc_region_consume(&region2, name_length(&name2));
274
275	return (isc_region_compare(&region1, &region2));
276}
277
278static inline isc_result_t
279fromstruct_soa(ARGS_FROMSTRUCT) {
280	dns_rdata_soa_t *soa = source;
281	isc_region_t region;
282
283	REQUIRE(type == 6);
284	REQUIRE(source != NULL);
285	REQUIRE(soa->common.rdtype == type);
286	REQUIRE(soa->common.rdclass == rdclass);
287
288	UNUSED(type);
289	UNUSED(rdclass);
290
291	dns_name_toregion(&soa->origin, &region);
292	RETERR(isc_buffer_copyregion(target, &region));
293	dns_name_toregion(&soa->contact, &region);
294	RETERR(isc_buffer_copyregion(target, &region));
295	RETERR(uint32_tobuffer(soa->serial, target));
296	RETERR(uint32_tobuffer(soa->refresh, target));
297	RETERR(uint32_tobuffer(soa->retry, target));
298	RETERR(uint32_tobuffer(soa->expire, target));
299	return (uint32_tobuffer(soa->minimum, target));
300}
301
302static inline isc_result_t
303tostruct_soa(ARGS_TOSTRUCT) {
304	isc_region_t region;
305	dns_rdata_soa_t *soa = target;
306	dns_name_t name;
307	isc_result_t result;
308
309	REQUIRE(rdata->type == 6);
310	REQUIRE(target != NULL);
311	REQUIRE(rdata->length != 0);
312
313	soa->common.rdclass = rdata->rdclass;
314	soa->common.rdtype = rdata->type;
315	ISC_LINK_INIT(&soa->common, link);
316
317
318	dns_rdata_toregion(rdata, &region);
319
320	dns_name_init(&name, NULL);
321	dns_name_fromregion(&name, &region);
322	isc_region_consume(&region, name_length(&name));
323	dns_name_init(&soa->origin, NULL);
324	RETERR(name_duporclone(&name, mctx, &soa->origin));
325
326	dns_name_fromregion(&name, &region);
327	isc_region_consume(&region, name_length(&name));
328	dns_name_init(&soa->contact, NULL);
329	result = name_duporclone(&name, mctx, &soa->contact);
330	if (result != ISC_R_SUCCESS)
331		goto cleanup;
332
333	soa->serial = uint32_fromregion(&region);
334	isc_region_consume(&region, 4);
335
336	soa->refresh = uint32_fromregion(&region);
337	isc_region_consume(&region, 4);
338
339	soa->retry = uint32_fromregion(&region);
340	isc_region_consume(&region, 4);
341
342	soa->expire = uint32_fromregion(&region);
343	isc_region_consume(&region, 4);
344
345	soa->minimum = uint32_fromregion(&region);
346
347	soa->mctx = mctx;
348	return (ISC_R_SUCCESS);
349
350 cleanup:
351	if (mctx != NULL)
352		dns_name_free(&soa->origin, mctx);
353	return (ISC_R_NOMEMORY);
354}
355
356static inline void
357freestruct_soa(ARGS_FREESTRUCT) {
358	dns_rdata_soa_t *soa = source;
359
360	REQUIRE(source != NULL);
361	REQUIRE(soa->common.rdtype == 6);
362
363	if (soa->mctx == NULL)
364		return;
365
366	dns_name_free(&soa->origin, soa->mctx);
367	dns_name_free(&soa->contact, soa->mctx);
368	soa->mctx = NULL;
369}
370
371static inline isc_result_t
372additionaldata_soa(ARGS_ADDLDATA) {
373	UNUSED(rdata);
374	UNUSED(add);
375	UNUSED(arg);
376
377	REQUIRE(rdata->type == 6);
378
379	return (ISC_R_SUCCESS);
380}
381
382static inline isc_result_t
383digest_soa(ARGS_DIGEST) {
384	isc_region_t r;
385	dns_name_t name;
386
387	REQUIRE(rdata->type == 6);
388
389	dns_rdata_toregion(rdata, &r);
390
391	dns_name_init(&name, NULL);
392	dns_name_fromregion(&name, &r);
393	RETERR(dns_name_digest(&name, digest, arg));
394	isc_region_consume(&r, name_length(&name));
395
396	dns_name_init(&name, NULL);
397	dns_name_fromregion(&name, &r);
398	RETERR(dns_name_digest(&name, digest, arg));
399	isc_region_consume(&r, name_length(&name));
400
401	return ((digest)(arg, &r));
402}
403
404static inline isc_boolean_t
405checkowner_soa(ARGS_CHECKOWNER) {
406
407	REQUIRE(type == 6);
408
409	UNUSED(name);
410	UNUSED(type);
411	UNUSED(rdclass);
412	UNUSED(wildcard);
413
414	return (ISC_TRUE);
415}
416
417static inline isc_boolean_t
418checknames_soa(ARGS_CHECKNAMES) {
419	isc_region_t region;
420	dns_name_t name;
421
422	REQUIRE(rdata->type == 6);
423
424	UNUSED(owner);
425
426	dns_rdata_toregion(rdata, &region);
427	dns_name_init(&name, NULL);
428	dns_name_fromregion(&name, &region);
429	if (!dns_name_ishostname(&name, ISC_FALSE)) {
430		if (bad != NULL)
431			dns_name_clone(&name, bad);
432		return (ISC_FALSE);
433	}
434	isc_region_consume(&region, name_length(&name));
435	dns_name_fromregion(&name, &region);
436	if (!dns_name_ismailbox(&name)) {
437		if (bad != NULL)
438			dns_name_clone(&name, bad);
439		return (ISC_FALSE);
440	}
441	return (ISC_TRUE);
442}
443
444static inline int
445casecompare_soa(ARGS_COMPARE) {
446	return (compare_soa(rdata1, rdata2));
447}
448
449#endif	/* RDATA_GENERIC_SOA_6_C */