xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/generic/l32_105.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /*	$NetBSD: l32_105.c,v 1.7 2021/02/19 16:42:17 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #ifndef RDATA_GENERIC_L32_105_C
15 #define RDATA_GENERIC_L32_105_C
16 
17 #include <string.h>
18 
19 #include <isc/net.h>
20 
21 #define RRTYPE_L32_ATTRIBUTES (0)
22 
23 static inline isc_result_t
24 fromtext_l32(ARGS_FROMTEXT) {
25 	isc_token_t token;
26 	struct in_addr addr;
27 	isc_region_t region;
28 
29 	REQUIRE(type == dns_rdatatype_l32);
30 
31 	UNUSED(type);
32 	UNUSED(rdclass);
33 	UNUSED(origin);
34 	UNUSED(options);
35 	UNUSED(callbacks);
36 
37 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
38 				      false));
39 	if (token.value.as_ulong > 0xffffU) {
40 		RETTOK(ISC_R_RANGE);
41 	}
42 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
43 
44 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
45 				      false));
46 
47 	if (inet_pton(AF_INET, DNS_AS_STR(token), &addr) != 1) {
48 		RETTOK(DNS_R_BADDOTTEDQUAD);
49 	}
50 	isc_buffer_availableregion(target, &region);
51 	if (region.length < 4) {
52 		return (ISC_R_NOSPACE);
53 	}
54 	memmove(region.base, &addr, 4);
55 	isc_buffer_add(target, 4);
56 	return (ISC_R_SUCCESS);
57 }
58 
59 static inline isc_result_t
60 totext_l32(ARGS_TOTEXT) {
61 	isc_region_t region;
62 	char buf[sizeof("65000")];
63 	unsigned short num;
64 
65 	REQUIRE(rdata->type == dns_rdatatype_l32);
66 	REQUIRE(rdata->length == 6);
67 
68 	UNUSED(tctx);
69 
70 	dns_rdata_toregion(rdata, &region);
71 	num = uint16_fromregion(&region);
72 	isc_region_consume(&region, 2);
73 	snprintf(buf, sizeof(buf), "%u", num);
74 	RETERR(str_totext(buf, target));
75 
76 	RETERR(str_totext(" ", target));
77 
78 	return (inet_totext(AF_INET, tctx->flags, &region, target));
79 }
80 
81 static inline isc_result_t
82 fromwire_l32(ARGS_FROMWIRE) {
83 	isc_region_t sregion;
84 
85 	REQUIRE(type == dns_rdatatype_l32);
86 
87 	UNUSED(type);
88 	UNUSED(options);
89 	UNUSED(rdclass);
90 	UNUSED(dctx);
91 
92 	isc_buffer_activeregion(source, &sregion);
93 	if (sregion.length != 6) {
94 		return (DNS_R_FORMERR);
95 	}
96 	isc_buffer_forward(source, sregion.length);
97 	return (mem_tobuffer(target, sregion.base, sregion.length));
98 }
99 
100 static inline isc_result_t
101 towire_l32(ARGS_TOWIRE) {
102 	REQUIRE(rdata->type == dns_rdatatype_l32);
103 	REQUIRE(rdata->length == 6);
104 
105 	UNUSED(cctx);
106 
107 	return (mem_tobuffer(target, rdata->data, rdata->length));
108 }
109 
110 static inline int
111 compare_l32(ARGS_COMPARE) {
112 	isc_region_t region1;
113 	isc_region_t region2;
114 
115 	REQUIRE(rdata1->type == rdata2->type);
116 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
117 	REQUIRE(rdata1->type == dns_rdatatype_l32);
118 	REQUIRE(rdata1->length == 6);
119 	REQUIRE(rdata2->length == 6);
120 
121 	dns_rdata_toregion(rdata1, &region1);
122 	dns_rdata_toregion(rdata2, &region2);
123 	return (isc_region_compare(&region1, &region2));
124 }
125 
126 static inline isc_result_t
127 fromstruct_l32(ARGS_FROMSTRUCT) {
128 	dns_rdata_l32_t *l32 = source;
129 	uint32_t n;
130 
131 	REQUIRE(type == dns_rdatatype_l32);
132 	REQUIRE(l32 != NULL);
133 	REQUIRE(l32->common.rdtype == type);
134 	REQUIRE(l32->common.rdclass == rdclass);
135 
136 	UNUSED(type);
137 	UNUSED(rdclass);
138 
139 	RETERR(uint16_tobuffer(l32->pref, target));
140 	n = ntohl(l32->l32.s_addr);
141 	return (uint32_tobuffer(n, target));
142 }
143 
144 static inline isc_result_t
145 tostruct_l32(ARGS_TOSTRUCT) {
146 	isc_region_t region;
147 	dns_rdata_l32_t *l32 = target;
148 	uint32_t n;
149 
150 	REQUIRE(rdata->type == dns_rdatatype_l32);
151 	REQUIRE(l32 != NULL);
152 	REQUIRE(rdata->length == 6);
153 
154 	UNUSED(mctx);
155 
156 	l32->common.rdclass = rdata->rdclass;
157 	l32->common.rdtype = rdata->type;
158 	ISC_LINK_INIT(&l32->common, link);
159 
160 	dns_rdata_toregion(rdata, &region);
161 	l32->pref = uint16_fromregion(&region);
162 	n = uint32_fromregion(&region);
163 	l32->l32.s_addr = htonl(n);
164 	return (ISC_R_SUCCESS);
165 }
166 
167 static inline void
168 freestruct_l32(ARGS_FREESTRUCT) {
169 	dns_rdata_l32_t *l32 = source;
170 
171 	REQUIRE(l32 != NULL);
172 	REQUIRE(l32->common.rdtype == dns_rdatatype_l32);
173 
174 	return;
175 }
176 
177 static inline isc_result_t
178 additionaldata_l32(ARGS_ADDLDATA) {
179 	REQUIRE(rdata->type == dns_rdatatype_l32);
180 	REQUIRE(rdata->length == 6);
181 
182 	UNUSED(rdata);
183 	UNUSED(add);
184 	UNUSED(arg);
185 
186 	return (ISC_R_SUCCESS);
187 }
188 
189 static inline isc_result_t
190 digest_l32(ARGS_DIGEST) {
191 	isc_region_t r;
192 
193 	REQUIRE(rdata->type == dns_rdatatype_l32);
194 	REQUIRE(rdata->length == 6);
195 
196 	dns_rdata_toregion(rdata, &r);
197 
198 	return ((digest)(arg, &r));
199 }
200 
201 static inline bool
202 checkowner_l32(ARGS_CHECKOWNER) {
203 	REQUIRE(type == dns_rdatatype_l32);
204 
205 	UNUSED(name);
206 	UNUSED(type);
207 	UNUSED(rdclass);
208 	UNUSED(wildcard);
209 
210 	return (true);
211 }
212 
213 static inline bool
214 checknames_l32(ARGS_CHECKNAMES) {
215 	REQUIRE(rdata->type == dns_rdatatype_l32);
216 	REQUIRE(rdata->length == 6);
217 
218 	UNUSED(rdata);
219 	UNUSED(owner);
220 	UNUSED(bad);
221 
222 	return (true);
223 }
224 
225 static inline int
226 casecompare_l32(ARGS_COMPARE) {
227 	return (compare_l32(rdata1, rdata2));
228 }
229 
230 #endif /* RDATA_GENERIC_L32_105_C */
231