xref: /openbsd/usr.bin/dig/lib/dns/rdata/generic/soa_6.c (revision 1fb015a8)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: soa_6.c,v 1.9 2020/09/14 08:40:43 florian Exp $ */
18 
19 /* Reviewed: Thu Mar 16 15:18:32 PST 2000 by explorer */
20 
21 #ifndef RDATA_GENERIC_SOA_6_C
22 #define RDATA_GENERIC_SOA_6_C
23 
24 static const char *soa_fieldnames[5] = {
25 	"serial", "refresh", "retry", "expire", "minimum"
26 };
27 
28 static inline isc_result_t
totext_soa(ARGS_TOTEXT)29 totext_soa(ARGS_TOTEXT) {
30 	isc_region_t dregion;
31 	dns_name_t mname;
32 	dns_name_t rname;
33 	dns_name_t prefix;
34 	int sub;
35 	int i;
36 	int multiline;
37 	int comm;
38 
39 	REQUIRE(rdata->type == dns_rdatatype_soa);
40 	REQUIRE(rdata->length != 0);
41 
42 	multiline = (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0;
43 	if (multiline)
44 		comm = (tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0;
45 	else
46 		comm = 0;
47 
48 	dns_name_init(&mname, NULL);
49 	dns_name_init(&rname, NULL);
50 	dns_name_init(&prefix, NULL);
51 
52 	dns_rdata_toregion(rdata, &dregion);
53 
54 	dns_name_fromregion(&mname, &dregion);
55 	isc_region_consume(&dregion, name_length(&mname));
56 
57 	dns_name_fromregion(&rname, &dregion);
58 	isc_region_consume(&dregion, name_length(&rname));
59 
60 	sub = name_prefix(&mname, tctx->origin, &prefix);
61 	RETERR(dns_name_totext(&prefix, sub, target));
62 
63 	RETERR(isc_str_tobuffer(" ", target));
64 
65 	sub = name_prefix(&rname, tctx->origin, &prefix);
66 	RETERR(dns_name_totext(&prefix, sub, target));
67 
68 	if (multiline)
69 		RETERR(isc_str_tobuffer(" (" , target));
70 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
71 
72 	for (i = 0; i < 5; i++) {
73 		char buf[sizeof("0123456789 ; ")];
74 		unsigned long num;
75 		num = uint32_fromregion(&dregion);
76 		isc_region_consume(&dregion, 4);
77 		snprintf(buf, sizeof(buf), comm ? "%-10lu ; " : "%lu", num);
78 		RETERR(isc_str_tobuffer(buf, target));
79 		if (comm) {
80 			RETERR(isc_str_tobuffer(soa_fieldnames[i], target));
81 			/* Print times in week/day/hour/minute/second form */
82 			if (i >= 1) {
83 				RETERR(isc_str_tobuffer(" (", target));
84 				RETERR(dns_ttl_totext(num, 1, target));
85 				RETERR(isc_str_tobuffer(")", target));
86 			}
87 			RETERR(isc_str_tobuffer(tctx->linebreak, target));
88 		} else if (i < 4) {
89 			RETERR(isc_str_tobuffer(tctx->linebreak, target));
90 		}
91 	}
92 
93 	if (multiline)
94 		RETERR(isc_str_tobuffer(")", target));
95 
96 	return (ISC_R_SUCCESS);
97 }
98 
99 static inline isc_result_t
fromwire_soa(ARGS_FROMWIRE)100 fromwire_soa(ARGS_FROMWIRE) {
101 	dns_name_t mname;
102 	dns_name_t rname;
103 	isc_region_t sregion;
104 	isc_region_t tregion;
105 
106 	REQUIRE(type == dns_rdatatype_soa);
107 
108 	UNUSED(type);
109 	UNUSED(rdclass);
110 
111 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
112 
113 	dns_name_init(&mname, NULL);
114 	dns_name_init(&rname, NULL);
115 
116 	RETERR(dns_name_fromwire(&mname, source, dctx, options, target));
117 	RETERR(dns_name_fromwire(&rname, source, dctx, options, target));
118 
119 	isc_buffer_activeregion(source, &sregion);
120 	isc_buffer_availableregion(target, &tregion);
121 
122 	if (sregion.length < 20)
123 		return (ISC_R_UNEXPECTEDEND);
124 	if (tregion.length < 20)
125 		return (ISC_R_NOSPACE);
126 
127 	memmove(tregion.base, sregion.base, 20);
128 	isc_buffer_forward(source, 20);
129 	isc_buffer_add(target, 20);
130 
131 	return (ISC_R_SUCCESS);
132 }
133 
134 static inline isc_result_t
towire_soa(ARGS_TOWIRE)135 towire_soa(ARGS_TOWIRE) {
136 	isc_region_t sregion;
137 	isc_region_t tregion;
138 	dns_name_t mname;
139 	dns_name_t rname;
140 	dns_offsets_t moffsets;
141 	dns_offsets_t roffsets;
142 
143 	REQUIRE(rdata->type == dns_rdatatype_soa);
144 	REQUIRE(rdata->length != 0);
145 
146 	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
147 
148 	dns_name_init(&mname, moffsets);
149 	dns_name_init(&rname, roffsets);
150 
151 	dns_rdata_toregion(rdata, &sregion);
152 
153 	dns_name_fromregion(&mname, &sregion);
154 	isc_region_consume(&sregion, name_length(&mname));
155 	RETERR(dns_name_towire(&mname, cctx, target));
156 
157 	dns_name_fromregion(&rname, &sregion);
158 	isc_region_consume(&sregion, name_length(&rname));
159 	RETERR(dns_name_towire(&rname, cctx, target));
160 
161 	isc_buffer_availableregion(target, &tregion);
162 	if (tregion.length < 20)
163 		return (ISC_R_NOSPACE);
164 
165 	memmove(tregion.base, sregion.base, 20);
166 	isc_buffer_add(target, 20);
167 	return (ISC_R_SUCCESS);
168 }
169 
170 static inline isc_result_t
fromstruct_soa(ARGS_FROMSTRUCT)171 fromstruct_soa(ARGS_FROMSTRUCT) {
172 	dns_rdata_soa_t *soa = source;
173 	isc_region_t region;
174 
175 	REQUIRE(type == dns_rdatatype_soa);
176 	REQUIRE(source != NULL);
177 	REQUIRE(soa->common.rdtype == type);
178 	REQUIRE(soa->common.rdclass == rdclass);
179 
180 	UNUSED(type);
181 	UNUSED(rdclass);
182 
183 	dns_name_toregion(&soa->origin, &region);
184 	RETERR(isc_buffer_copyregion(target, &region));
185 	dns_name_toregion(&soa->contact, &region);
186 	RETERR(isc_buffer_copyregion(target, &region));
187 	RETERR(uint32_tobuffer(soa->serial, target));
188 	RETERR(uint32_tobuffer(soa->refresh, target));
189 	RETERR(uint32_tobuffer(soa->retry, target));
190 	RETERR(uint32_tobuffer(soa->expire, target));
191 	return (uint32_tobuffer(soa->minimum, target));
192 }
193 
194 static inline isc_result_t
tostruct_soa(ARGS_TOSTRUCT)195 tostruct_soa(ARGS_TOSTRUCT) {
196 	isc_region_t region;
197 	dns_rdata_soa_t *soa = target;
198 	dns_name_t name;
199 	isc_result_t result;
200 
201 	REQUIRE(rdata->type == dns_rdatatype_soa);
202 	REQUIRE(target != NULL);
203 	REQUIRE(rdata->length != 0);
204 
205 	soa->common.rdclass = rdata->rdclass;
206 	soa->common.rdtype = rdata->type;
207 	ISC_LINK_INIT(&soa->common, link);
208 
209 	dns_rdata_toregion(rdata, &region);
210 
211 	dns_name_init(&name, NULL);
212 	dns_name_fromregion(&name, &region);
213 	isc_region_consume(&region, name_length(&name));
214 	dns_name_init(&soa->origin, NULL);
215 	RETERR(name_duporclone(&name, &soa->origin));
216 
217 	dns_name_fromregion(&name, &region);
218 	isc_region_consume(&region, name_length(&name));
219 	dns_name_init(&soa->contact, NULL);
220 	result = name_duporclone(&name, &soa->contact);
221 	if (result != ISC_R_SUCCESS)
222 		goto cleanup;
223 
224 	soa->serial = uint32_fromregion(&region);
225 	isc_region_consume(&region, 4);
226 
227 	soa->refresh = uint32_fromregion(&region);
228 	isc_region_consume(&region, 4);
229 
230 	soa->retry = uint32_fromregion(&region);
231 	isc_region_consume(&region, 4);
232 
233 	soa->expire = uint32_fromregion(&region);
234 	isc_region_consume(&region, 4);
235 
236 	soa->minimum = uint32_fromregion(&region);
237 
238 	return (ISC_R_SUCCESS);
239 
240  cleanup:
241 	dns_name_free(&soa->origin);
242 	return (ISC_R_NOMEMORY);
243 }
244 
245 static inline void
freestruct_soa(ARGS_FREESTRUCT)246 freestruct_soa(ARGS_FREESTRUCT) {
247 	dns_rdata_soa_t *soa = source;
248 
249 	REQUIRE(source != NULL);
250 	REQUIRE(soa->common.rdtype == dns_rdatatype_soa);
251 
252 	dns_name_free(&soa->origin);
253 	dns_name_free(&soa->contact);
254 }
255 
256 #endif	/* RDATA_GENERIC_SOA_6_C */
257