xref: /openbsd/usr.bin/dig/lib/dns/rdata/in_1/kx_36.c (revision 2fb68d9f)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: kx_36.c,v 1.3 2020/02/23 19:54:26 jung Exp $ */
18 
19 /* Reviewed: Thu Mar 16 17:24:54 PST 2000 by explorer */
20 
21 /* RFC2230 */
22 
23 #ifndef RDATA_IN_1_KX_36_C
24 #define RDATA_IN_1_KX_36_C
25 
26 #define RRTYPE_KX_ATTRIBUTES (0)
27 
28 static inline isc_result_t
29 totext_in_kx(ARGS_TOTEXT) {
30 	isc_region_t region;
31 	dns_name_t name;
32 	dns_name_t prefix;
33 	isc_boolean_t sub;
34 	char buf[sizeof("64000")];
35 	unsigned short num;
36 
37 	REQUIRE(rdata->type == dns_rdatatype_kx);
38 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
39 	REQUIRE(rdata->length != 0);
40 
41 	dns_name_init(&name, NULL);
42 	dns_name_init(&prefix, NULL);
43 
44 	dns_rdata_toregion(rdata, &region);
45 	num = uint16_fromregion(&region);
46 	isc_region_consume(&region, 2);
47 	snprintf(buf, sizeof(buf), "%u", num);
48 	RETERR(str_totext(buf, target));
49 
50 	RETERR(str_totext(" ", target));
51 
52 	dns_name_fromregion(&name, &region);
53 	sub = name_prefix(&name, tctx->origin, &prefix);
54 	return (dns_name_totext(&prefix, sub, target));
55 }
56 
57 static inline isc_result_t
58 fromwire_in_kx(ARGS_FROMWIRE) {
59 	dns_name_t name;
60 	isc_region_t sregion;
61 
62 	REQUIRE(type == dns_rdatatype_kx);
63 	REQUIRE(rdclass == dns_rdataclass_in);
64 
65 	UNUSED(type);
66 	UNUSED(rdclass);
67 
68 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
69 
70 	dns_name_init(&name, NULL);
71 
72 	isc_buffer_activeregion(source, &sregion);
73 	if (sregion.length < 2)
74 		return (ISC_R_UNEXPECTEDEND);
75 	RETERR(mem_tobuffer(target, sregion.base, 2));
76 	isc_buffer_forward(source, 2);
77 	return (dns_name_fromwire(&name, source, dctx, options, target));
78 }
79 
80 static inline isc_result_t
81 towire_in_kx(ARGS_TOWIRE) {
82 	dns_name_t name;
83 	dns_offsets_t offsets;
84 	isc_region_t region;
85 
86 	REQUIRE(rdata->type == dns_rdatatype_kx);
87 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
88 	REQUIRE(rdata->length != 0);
89 
90 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
91 	dns_rdata_toregion(rdata, &region);
92 	RETERR(mem_tobuffer(target, region.base, 2));
93 	isc_region_consume(&region, 2);
94 
95 	dns_name_init(&name, offsets);
96 	dns_name_fromregion(&name, &region);
97 
98 	return (dns_name_towire(&name, cctx, target));
99 }
100 
101 static inline int
102 compare_in_kx(ARGS_COMPARE) {
103 	dns_name_t name1;
104 	dns_name_t name2;
105 	isc_region_t region1;
106 	isc_region_t region2;
107 	int order;
108 
109 	REQUIRE(rdata1->type == rdata2->type);
110 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
111 	REQUIRE(rdata1->type == dns_rdatatype_kx);
112 	REQUIRE(rdata1->rdclass == dns_rdataclass_in);
113 	REQUIRE(rdata1->length != 0);
114 	REQUIRE(rdata2->length != 0);
115 
116 	order = memcmp(rdata1->data, rdata2->data, 2);
117 	if (order != 0)
118 		return (order < 0 ? -1 : 1);
119 
120 	dns_name_init(&name1, NULL);
121 	dns_name_init(&name2, NULL);
122 
123 	dns_rdata_toregion(rdata1, &region1);
124 	dns_rdata_toregion(rdata2, &region2);
125 
126 	isc_region_consume(&region1, 2);
127 	isc_region_consume(&region2, 2);
128 
129 	dns_name_fromregion(&name1, &region1);
130 	dns_name_fromregion(&name2, &region2);
131 
132 	return (dns_name_rdatacompare(&name1, &name2));
133 }
134 
135 static inline isc_result_t
136 fromstruct_in_kx(ARGS_FROMSTRUCT) {
137 	dns_rdata_in_kx_t *kx = source;
138 	isc_region_t region;
139 
140 	REQUIRE(type == dns_rdatatype_kx);
141 	REQUIRE(rdclass == dns_rdataclass_in);
142 	REQUIRE(source != NULL);
143 	REQUIRE(kx->common.rdtype == type);
144 	REQUIRE(kx->common.rdclass == rdclass);
145 
146 	UNUSED(type);
147 	UNUSED(rdclass);
148 
149 	RETERR(uint16_tobuffer(kx->preference, target));
150 	dns_name_toregion(&kx->exchange, &region);
151 	return (isc_buffer_copyregion(target, &region));
152 }
153 
154 static inline isc_result_t
155 tostruct_in_kx(ARGS_TOSTRUCT) {
156 	isc_region_t region;
157 	dns_rdata_in_kx_t *kx = target;
158 	dns_name_t name;
159 
160 	REQUIRE(rdata->type == dns_rdatatype_kx);
161 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
162 	REQUIRE(target != NULL);
163 	REQUIRE(rdata->length != 0);
164 
165 	kx->common.rdclass = rdata->rdclass;
166 	kx->common.rdtype = rdata->type;
167 	ISC_LINK_INIT(&kx->common, link);
168 
169 	dns_name_init(&name, NULL);
170 	dns_rdata_toregion(rdata, &region);
171 
172 	kx->preference = uint16_fromregion(&region);
173 	isc_region_consume(&region, 2);
174 
175 	dns_name_fromregion(&name, &region);
176 	dns_name_init(&kx->exchange, NULL);
177 	RETERR(name_duporclone(&name, &kx->exchange));
178 	return (ISC_R_SUCCESS);
179 }
180 
181 static inline void
182 freestruct_in_kx(ARGS_FREESTRUCT) {
183 	dns_rdata_in_kx_t *kx = source;
184 
185 	REQUIRE(source != NULL);
186 	REQUIRE(kx->common.rdclass == dns_rdataclass_in);
187 	REQUIRE(kx->common.rdtype == dns_rdatatype_kx);
188 
189 	dns_name_free(&kx->exchange);
190 }
191 
192 static inline isc_boolean_t
193 checkowner_in_kx(ARGS_CHECKOWNER) {
194 
195 	REQUIRE(type == dns_rdatatype_kx);
196 	REQUIRE(rdclass == dns_rdataclass_in);
197 
198 	UNUSED(name);
199 	UNUSED(type);
200 	UNUSED(rdclass);
201 	UNUSED(wildcard);
202 
203 	return (ISC_TRUE);
204 }
205 
206 static inline int
207 casecompare_in_kx(ARGS_COMPARE) {
208 	return (compare_in_kx(rdata1, rdata2));
209 }
210 
211 #endif	/* RDATA_IN_1_KX_36_C */
212