1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id: gpos_27.c,v 1.43 2009/12/04 22:06:37 tbox Exp $ */
19 
20 /* reviewed: Wed Mar 15 16:48:45 PST 2000 by brister */
21 
22 /* RFC1712 */
23 
24 #ifndef RDATA_GENERIC_GPOS_27_C
25 #define RDATA_GENERIC_GPOS_27_C
26 
27 #define RRTYPE_GPOS_ATTRIBUTES (0)
28 
29 static inline isc_result_t
fromtext_gpos(ARGS_FROMTEXT)30 fromtext_gpos(ARGS_FROMTEXT) {
31 	isc_token_t token;
32 	int i;
33 
34 	REQUIRE(type == dns_rdatatype_gpos);
35 
36 	UNUSED(type);
37 	UNUSED(rdclass);
38 	UNUSED(origin);
39 	UNUSED(options);
40 	UNUSED(callbacks);
41 
42 	for (i = 0; i < 3; i++) {
43 		RETERR(isc_lex_getmastertoken(lexer, &token,
44 					      isc_tokentype_qstring,
45 					      ISC_FALSE));
46 		RETTOK(txt_fromtext(&token.value.as_textregion, target));
47 	}
48 	return (ISC_R_SUCCESS);
49 }
50 
51 static inline isc_result_t
totext_gpos(ARGS_TOTEXT)52 totext_gpos(ARGS_TOTEXT) {
53 	isc_region_t region;
54 	int i;
55 
56 	REQUIRE(rdata->type == dns_rdatatype_gpos);
57 	REQUIRE(rdata->length != 0);
58 
59 	UNUSED(tctx);
60 
61 	dns_rdata_toregion(rdata, &region);
62 
63 	for (i = 0; i < 3; i++) {
64 		RETERR(txt_totext(&region, ISC_TRUE, target));
65 		if (i != 2)
66 			RETERR(str_totext(" ", target));
67 	}
68 
69 	return (ISC_R_SUCCESS);
70 }
71 
72 static inline isc_result_t
fromwire_gpos(ARGS_FROMWIRE)73 fromwire_gpos(ARGS_FROMWIRE) {
74 	int i;
75 
76 	REQUIRE(type == dns_rdatatype_gpos);
77 
78 	UNUSED(type);
79 	UNUSED(dctx);
80 	UNUSED(rdclass);
81 	UNUSED(options);
82 
83 	for (i = 0; i < 3; i++)
84 		RETERR(txt_fromwire(source, target));
85 	return (ISC_R_SUCCESS);
86 }
87 
88 static inline isc_result_t
towire_gpos(ARGS_TOWIRE)89 towire_gpos(ARGS_TOWIRE) {
90 
91 	REQUIRE(rdata->type == dns_rdatatype_gpos);
92 	REQUIRE(rdata->length != 0);
93 
94 	UNUSED(cctx);
95 
96 	return (mem_tobuffer(target, rdata->data, rdata->length));
97 }
98 
99 static inline int
compare_gpos(ARGS_COMPARE)100 compare_gpos(ARGS_COMPARE) {
101 	isc_region_t r1;
102 	isc_region_t r2;
103 
104 	REQUIRE(rdata1->type == rdata2->type);
105 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
106 	REQUIRE(rdata1->type == dns_rdatatype_gpos);
107 	REQUIRE(rdata1->length != 0);
108 	REQUIRE(rdata2->length != 0);
109 
110 	dns_rdata_toregion(rdata1, &r1);
111 	dns_rdata_toregion(rdata2, &r2);
112 	return (isc_region_compare(&r1, &r2));
113 }
114 
115 static inline isc_result_t
fromstruct_gpos(ARGS_FROMSTRUCT)116 fromstruct_gpos(ARGS_FROMSTRUCT) {
117 	dns_rdata_gpos_t *gpos = source;
118 
119 	REQUIRE(type == dns_rdatatype_gpos);
120 	REQUIRE(source != NULL);
121 	REQUIRE(gpos->common.rdtype == type);
122 	REQUIRE(gpos->common.rdclass == rdclass);
123 
124 	UNUSED(type);
125 	UNUSED(rdclass);
126 
127 	RETERR(uint8_tobuffer(gpos->long_len, target));
128 	RETERR(mem_tobuffer(target, gpos->longitude, gpos->long_len));
129 	RETERR(uint8_tobuffer(gpos->lat_len, target));
130 	RETERR(mem_tobuffer(target, gpos->latitude, gpos->lat_len));
131 	RETERR(uint8_tobuffer(gpos->alt_len, target));
132 	return (mem_tobuffer(target, gpos->altitude, gpos->alt_len));
133 }
134 
135 static inline isc_result_t
tostruct_gpos(ARGS_TOSTRUCT)136 tostruct_gpos(ARGS_TOSTRUCT) {
137 	dns_rdata_gpos_t *gpos = target;
138 	isc_region_t region;
139 
140 	REQUIRE(rdata->type == dns_rdatatype_gpos);
141 	REQUIRE(target != NULL);
142 	REQUIRE(rdata->length != 0);
143 
144 	gpos->common.rdclass = rdata->rdclass;
145 	gpos->common.rdtype = rdata->type;
146 	ISC_LINK_INIT(&gpos->common, link);
147 
148 	dns_rdata_toregion(rdata, &region);
149 	gpos->long_len = uint8_fromregion(&region);
150 	isc_region_consume(&region, 1);
151 	gpos->longitude = mem_maybedup(mctx, region.base, gpos->long_len);
152 	if (gpos->longitude == NULL)
153 		return (ISC_R_NOMEMORY);
154 	isc_region_consume(&region, gpos->long_len);
155 
156 	gpos->lat_len = uint8_fromregion(&region);
157 	isc_region_consume(&region, 1);
158 	gpos->latitude = mem_maybedup(mctx, region.base, gpos->lat_len);
159 	if (gpos->latitude == NULL)
160 		goto cleanup_longitude;
161 	isc_region_consume(&region, gpos->lat_len);
162 
163 	gpos->alt_len = uint8_fromregion(&region);
164 	isc_region_consume(&region, 1);
165 	if (gpos->lat_len > 0) {
166 		gpos->altitude =
167 			mem_maybedup(mctx, region.base, gpos->alt_len);
168 		if (gpos->altitude == NULL)
169 			goto cleanup_latitude;
170 	} else
171 		gpos->altitude = NULL;
172 
173 	gpos->mctx = mctx;
174 	return (ISC_R_SUCCESS);
175 
176  cleanup_latitude:
177 	if (mctx != NULL && gpos->longitude != NULL)
178 		isc_mem_free(mctx, gpos->longitude);
179 
180  cleanup_longitude:
181 	if (mctx != NULL && gpos->latitude != NULL)
182 		isc_mem_free(mctx, gpos->latitude);
183 	return (ISC_R_NOMEMORY);
184 }
185 
186 static inline void
freestruct_gpos(ARGS_FREESTRUCT)187 freestruct_gpos(ARGS_FREESTRUCT) {
188 	dns_rdata_gpos_t *gpos = source;
189 
190 	REQUIRE(source != NULL);
191 	REQUIRE(gpos->common.rdtype == dns_rdatatype_gpos);
192 
193 	if (gpos->mctx == NULL)
194 		return;
195 
196 	if (gpos->longitude != NULL)
197 		isc_mem_free(gpos->mctx, gpos->longitude);
198 	if (gpos->latitude != NULL)
199 		isc_mem_free(gpos->mctx, gpos->latitude);
200 	if (gpos->altitude != NULL)
201 		isc_mem_free(gpos->mctx, gpos->altitude);
202 	gpos->mctx = NULL;
203 }
204 
205 static inline isc_result_t
additionaldata_gpos(ARGS_ADDLDATA)206 additionaldata_gpos(ARGS_ADDLDATA) {
207 	REQUIRE(rdata->type == dns_rdatatype_gpos);
208 
209 	UNUSED(rdata);
210 	UNUSED(add);
211 	UNUSED(arg);
212 
213 	return (ISC_R_SUCCESS);
214 }
215 
216 static inline isc_result_t
digest_gpos(ARGS_DIGEST)217 digest_gpos(ARGS_DIGEST) {
218 	isc_region_t r;
219 
220 	REQUIRE(rdata->type == dns_rdatatype_gpos);
221 
222 	dns_rdata_toregion(rdata, &r);
223 
224 	return ((digest)(arg, &r));
225 }
226 
227 static inline isc_boolean_t
checkowner_gpos(ARGS_CHECKOWNER)228 checkowner_gpos(ARGS_CHECKOWNER) {
229 
230 	REQUIRE(type == dns_rdatatype_gpos);
231 
232 	UNUSED(name);
233 	UNUSED(type);
234 	UNUSED(rdclass);
235 	UNUSED(wildcard);
236 
237 	return (ISC_TRUE);
238 }
239 
240 static inline isc_boolean_t
checknames_gpos(ARGS_CHECKNAMES)241 checknames_gpos(ARGS_CHECKNAMES) {
242 
243 	REQUIRE(rdata->type == dns_rdatatype_gpos);
244 
245 	UNUSED(rdata);
246 	UNUSED(owner);
247 	UNUSED(bad);
248 
249 	return (ISC_TRUE);
250 }
251 
252 static inline int
casecompare_gpos(ARGS_COMPARE)253 casecompare_gpos(ARGS_COMPARE) {
254 	return (compare_gpos(rdata1, rdata2));
255 }
256 
257 #endif	/* RDATA_GENERIC_GPOS_27_C */
258