xref: /minix/external/bsd/bind/dist/lib/dns/rdata/in_1/a_1.c (revision bb9622b5)
1 /*	$NetBSD: a_1.c,v 1.5 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2009, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2002  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: a_1.c,v 1.55 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Thu Mar 16 16:52:50 PST 2000 by bwelling */
23 
24 #ifndef RDATA_IN_1_A_1_C
25 #define RDATA_IN_1_A_1_C
26 
27 #include <string.h>
28 
29 #include <isc/net.h>
30 
31 #define RRTYPE_A_ATTRIBUTES (0)
32 
33 static inline isc_result_t
34 fromtext_in_a(ARGS_FROMTEXT) {
35 	isc_token_t token;
36 	struct in_addr addr;
37 	isc_region_t region;
38 
39 	REQUIRE(type == 1);
40 	REQUIRE(rdclass == 1);
41 
42 	UNUSED(type);
43 	UNUSED(origin);
44 	UNUSED(options);
45 	UNUSED(rdclass);
46 
47 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
48 				      ISC_FALSE));
49 
50 	if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
51 		RETTOK(DNS_R_BADDOTTEDQUAD);
52 	isc_buffer_availableregion(target, &region);
53 	if (region.length < 4)
54 		return (ISC_R_NOSPACE);
55 	memmove(region.base, &addr, 4);
56 	isc_buffer_add(target, 4);
57 	return (ISC_R_SUCCESS);
58 }
59 
60 static inline isc_result_t
61 totext_in_a(ARGS_TOTEXT) {
62 	isc_region_t region;
63 
64 	REQUIRE(rdata->type == 1);
65 	REQUIRE(rdata->rdclass == 1);
66 	REQUIRE(rdata->length == 4);
67 
68 	UNUSED(tctx);
69 
70 	dns_rdata_toregion(rdata, &region);
71 	return (inet_totext(AF_INET, &region, target));
72 }
73 
74 static inline isc_result_t
75 fromwire_in_a(ARGS_FROMWIRE) {
76 	isc_region_t sregion;
77 	isc_region_t tregion;
78 
79 	REQUIRE(type == 1);
80 	REQUIRE(rdclass == 1);
81 
82 	UNUSED(type);
83 	UNUSED(dctx);
84 	UNUSED(options);
85 	UNUSED(rdclass);
86 
87 	isc_buffer_activeregion(source, &sregion);
88 	isc_buffer_availableregion(target, &tregion);
89 	if (sregion.length < 4)
90 		return (ISC_R_UNEXPECTEDEND);
91 	if (tregion.length < 4)
92 		return (ISC_R_NOSPACE);
93 
94 	memmove(tregion.base, sregion.base, 4);
95 	isc_buffer_forward(source, 4);
96 	isc_buffer_add(target, 4);
97 	return (ISC_R_SUCCESS);
98 }
99 
100 static inline isc_result_t
101 towire_in_a(ARGS_TOWIRE) {
102 	isc_region_t region;
103 
104 	REQUIRE(rdata->type == 1);
105 	REQUIRE(rdata->rdclass == 1);
106 	REQUIRE(rdata->length == 4);
107 
108 	UNUSED(cctx);
109 
110 	isc_buffer_availableregion(target, &region);
111 	if (region.length < rdata->length)
112 		return (ISC_R_NOSPACE);
113 	memmove(region.base, rdata->data, rdata->length);
114 	isc_buffer_add(target, 4);
115 	return (ISC_R_SUCCESS);
116 }
117 
118 static inline int
119 compare_in_a(ARGS_COMPARE) {
120 	isc_region_t r1;
121 	isc_region_t r2;
122 
123 	REQUIRE(rdata1->type == rdata2->type);
124 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
125 	REQUIRE(rdata1->type == 1);
126 	REQUIRE(rdata1->rdclass == 1);
127 	REQUIRE(rdata1->length == 4);
128 	REQUIRE(rdata2->length == 4);
129 
130 	dns_rdata_toregion(rdata1, &r1);
131 	dns_rdata_toregion(rdata2, &r2);
132 	return (isc_region_compare(&r1, &r2));
133 }
134 
135 static inline isc_result_t
136 fromstruct_in_a(ARGS_FROMSTRUCT) {
137 	dns_rdata_in_a_t *a = source;
138 	isc_uint32_t n;
139 
140 	REQUIRE(type == 1);
141 	REQUIRE(rdclass == 1);
142 	REQUIRE(source != NULL);
143 	REQUIRE(a->common.rdtype == type);
144 	REQUIRE(a->common.rdclass == rdclass);
145 
146 	UNUSED(type);
147 	UNUSED(rdclass);
148 
149 	n = ntohl(a->in_addr.s_addr);
150 
151 	return (uint32_tobuffer(n, target));
152 }
153 
154 
155 static inline isc_result_t
156 tostruct_in_a(ARGS_TOSTRUCT) {
157 	dns_rdata_in_a_t *a = target;
158 	isc_uint32_t n;
159 	isc_region_t region;
160 
161 	REQUIRE(rdata->type == 1);
162 	REQUIRE(rdata->rdclass == 1);
163 	REQUIRE(rdata->length == 4);
164 
165 	UNUSED(mctx);
166 
167 	a->common.rdclass = rdata->rdclass;
168 	a->common.rdtype = rdata->type;
169 	ISC_LINK_INIT(&a->common, link);
170 
171 	dns_rdata_toregion(rdata, &region);
172 	n = uint32_fromregion(&region);
173 	a->in_addr.s_addr = htonl(n);
174 
175 	return (ISC_R_SUCCESS);
176 }
177 
178 static inline void
179 freestruct_in_a(ARGS_FREESTRUCT) {
180 	dns_rdata_in_a_t *a = source;
181 
182 	REQUIRE(source != NULL);
183 	REQUIRE(a->common.rdtype == 1);
184 	REQUIRE(a->common.rdclass == 1);
185 
186 	UNUSED(a);
187 }
188 
189 static inline isc_result_t
190 additionaldata_in_a(ARGS_ADDLDATA) {
191 	REQUIRE(rdata->type == 1);
192 	REQUIRE(rdata->rdclass == 1);
193 
194 	UNUSED(rdata);
195 	UNUSED(add);
196 	UNUSED(arg);
197 
198 	return (ISC_R_SUCCESS);
199 }
200 
201 static inline isc_result_t
202 digest_in_a(ARGS_DIGEST) {
203 	isc_region_t r;
204 
205 	REQUIRE(rdata->type == 1);
206 	REQUIRE(rdata->rdclass == 1);
207 
208 	dns_rdata_toregion(rdata, &r);
209 
210 	return ((digest)(arg, &r));
211 }
212 
213 static inline isc_boolean_t
214 checkowner_in_a(ARGS_CHECKOWNER) {
215 
216 	REQUIRE(type == 1);
217 	REQUIRE(rdclass == 1);
218 
219 	UNUSED(type);
220 	UNUSED(rdclass);
221 
222 	return (dns_name_ishostname(name, wildcard));
223 }
224 
225 static inline isc_boolean_t
226 checknames_in_a(ARGS_CHECKNAMES) {
227 
228 	REQUIRE(rdata->type == 1);
229 	REQUIRE(rdata->rdclass == 1);
230 
231 	UNUSED(rdata);
232 	UNUSED(owner);
233 	UNUSED(bad);
234 
235 	return (ISC_TRUE);
236 }
237 
238 static inline int
239 casecompare_in_a(ARGS_COMPARE) {
240 	return (compare_in_a(rdata1, rdata2));
241 }
242 
243 #endif	/* RDATA_IN_1_A_1_C */
244