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