xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/generic/gpos_27.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: gpos_27.c,v 1.9 2025/01/26 16:25: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 /* RFC1712 */
17 
18 #ifndef RDATA_GENERIC_GPOS_27_C
19 #define RDATA_GENERIC_GPOS_27_C
20 
21 #define RRTYPE_GPOS_ATTRIBUTES (0)
22 
23 static isc_result_t
24 fromtext_gpos(ARGS_FROMTEXT) {
25 	isc_token_t token;
26 	int i;
27 
28 	REQUIRE(type == dns_rdatatype_gpos);
29 
30 	UNUSED(type);
31 	UNUSED(rdclass);
32 	UNUSED(origin);
33 	UNUSED(options);
34 	UNUSED(callbacks);
35 
36 	for (i = 0; i < 3; i++) {
37 		RETERR(isc_lex_getmastertoken(lexer, &token,
38 					      isc_tokentype_qstring, false));
39 		RETTOK(txt_fromtext(&token.value.as_textregion, target));
40 	}
41 	return ISC_R_SUCCESS;
42 }
43 
44 static isc_result_t
45 totext_gpos(ARGS_TOTEXT) {
46 	isc_region_t region;
47 	int i;
48 
49 	REQUIRE(rdata->type == dns_rdatatype_gpos);
50 	REQUIRE(rdata->length != 0);
51 
52 	UNUSED(tctx);
53 
54 	dns_rdata_toregion(rdata, &region);
55 
56 	for (i = 0; i < 3; i++) {
57 		RETERR(txt_totext(&region, true, target));
58 		if (i != 2) {
59 			RETERR(str_totext(" ", target));
60 		}
61 	}
62 
63 	return ISC_R_SUCCESS;
64 }
65 
66 static isc_result_t
67 fromwire_gpos(ARGS_FROMWIRE) {
68 	int i;
69 
70 	REQUIRE(type == dns_rdatatype_gpos);
71 
72 	UNUSED(type);
73 	UNUSED(dctx);
74 	UNUSED(rdclass);
75 
76 	for (i = 0; i < 3; i++) {
77 		RETERR(txt_fromwire(source, target));
78 	}
79 	return ISC_R_SUCCESS;
80 }
81 
82 static isc_result_t
83 towire_gpos(ARGS_TOWIRE) {
84 	REQUIRE(rdata->type == dns_rdatatype_gpos);
85 	REQUIRE(rdata->length != 0);
86 
87 	UNUSED(cctx);
88 
89 	return mem_tobuffer(target, rdata->data, rdata->length);
90 }
91 
92 static int
93 compare_gpos(ARGS_COMPARE) {
94 	isc_region_t r1;
95 	isc_region_t r2;
96 
97 	REQUIRE(rdata1->type == rdata2->type);
98 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
99 	REQUIRE(rdata1->type == dns_rdatatype_gpos);
100 	REQUIRE(rdata1->length != 0);
101 	REQUIRE(rdata2->length != 0);
102 
103 	dns_rdata_toregion(rdata1, &r1);
104 	dns_rdata_toregion(rdata2, &r2);
105 	return isc_region_compare(&r1, &r2);
106 }
107 
108 static isc_result_t
109 fromstruct_gpos(ARGS_FROMSTRUCT) {
110 	dns_rdata_gpos_t *gpos = source;
111 
112 	REQUIRE(type == dns_rdatatype_gpos);
113 	REQUIRE(gpos != NULL);
114 	REQUIRE(gpos->common.rdtype == type);
115 	REQUIRE(gpos->common.rdclass == rdclass);
116 
117 	UNUSED(type);
118 	UNUSED(rdclass);
119 
120 	RETERR(uint8_tobuffer(gpos->long_len, target));
121 	RETERR(mem_tobuffer(target, gpos->longitude, gpos->long_len));
122 	RETERR(uint8_tobuffer(gpos->lat_len, target));
123 	RETERR(mem_tobuffer(target, gpos->latitude, gpos->lat_len));
124 	RETERR(uint8_tobuffer(gpos->alt_len, target));
125 	return mem_tobuffer(target, gpos->altitude, gpos->alt_len);
126 }
127 
128 static isc_result_t
129 tostruct_gpos(ARGS_TOSTRUCT) {
130 	dns_rdata_gpos_t *gpos = target;
131 	isc_region_t region;
132 
133 	REQUIRE(rdata->type == dns_rdatatype_gpos);
134 	REQUIRE(gpos != NULL);
135 	REQUIRE(rdata->length != 0);
136 
137 	gpos->common.rdclass = rdata->rdclass;
138 	gpos->common.rdtype = rdata->type;
139 	ISC_LINK_INIT(&gpos->common, link);
140 
141 	dns_rdata_toregion(rdata, &region);
142 	gpos->long_len = uint8_fromregion(&region);
143 	isc_region_consume(&region, 1);
144 	gpos->longitude = mem_maybedup(mctx, region.base, gpos->long_len);
145 	isc_region_consume(&region, gpos->long_len);
146 
147 	gpos->lat_len = uint8_fromregion(&region);
148 	isc_region_consume(&region, 1);
149 	gpos->latitude = mem_maybedup(mctx, region.base, gpos->lat_len);
150 	isc_region_consume(&region, gpos->lat_len);
151 
152 	gpos->alt_len = uint8_fromregion(&region);
153 	isc_region_consume(&region, 1);
154 	if (gpos->lat_len > 0) {
155 		gpos->altitude = mem_maybedup(mctx, region.base, gpos->alt_len);
156 	} else {
157 		gpos->altitude = NULL;
158 	}
159 
160 	gpos->mctx = mctx;
161 	return ISC_R_SUCCESS;
162 }
163 
164 static void
165 freestruct_gpos(ARGS_FREESTRUCT) {
166 	dns_rdata_gpos_t *gpos = source;
167 
168 	REQUIRE(gpos != NULL);
169 	REQUIRE(gpos->common.rdtype == dns_rdatatype_gpos);
170 
171 	if (gpos->mctx == NULL) {
172 		return;
173 	}
174 
175 	if (gpos->longitude != NULL) {
176 		isc_mem_free(gpos->mctx, gpos->longitude);
177 	}
178 	if (gpos->latitude != NULL) {
179 		isc_mem_free(gpos->mctx, gpos->latitude);
180 	}
181 	if (gpos->altitude != NULL) {
182 		isc_mem_free(gpos->mctx, gpos->altitude);
183 	}
184 	gpos->mctx = NULL;
185 }
186 
187 static isc_result_t
188 additionaldata_gpos(ARGS_ADDLDATA) {
189 	REQUIRE(rdata->type == dns_rdatatype_gpos);
190 
191 	UNUSED(rdata);
192 	UNUSED(owner);
193 	UNUSED(add);
194 	UNUSED(arg);
195 
196 	return ISC_R_SUCCESS;
197 }
198 
199 static isc_result_t
200 digest_gpos(ARGS_DIGEST) {
201 	isc_region_t r;
202 
203 	REQUIRE(rdata->type == dns_rdatatype_gpos);
204 
205 	dns_rdata_toregion(rdata, &r);
206 
207 	return (digest)(arg, &r);
208 }
209 
210 static bool
211 checkowner_gpos(ARGS_CHECKOWNER) {
212 	REQUIRE(type == dns_rdatatype_gpos);
213 
214 	UNUSED(name);
215 	UNUSED(type);
216 	UNUSED(rdclass);
217 	UNUSED(wildcard);
218 
219 	return true;
220 }
221 
222 static bool
223 checknames_gpos(ARGS_CHECKNAMES) {
224 	REQUIRE(rdata->type == dns_rdatatype_gpos);
225 
226 	UNUSED(rdata);
227 	UNUSED(owner);
228 	UNUSED(bad);
229 
230 	return true;
231 }
232 
233 static int
234 casecompare_gpos(ARGS_COMPARE) {
235 	return compare_gpos(rdata1, rdata2);
236 }
237 
238 #endif /* RDATA_GENERIC_GPOS_27_C */
239