1 /*
2  * Copyright (C) 2013-2015  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 #ifndef RDATA_GENERIC_L32_105_C
18 #define RDATA_GENERIC_L32_105_C
19 
20 #include <string.h>
21 
22 #include <isc/net.h>
23 
24 #define RRTYPE_L32_ATTRIBUTES (0)
25 
26 static inline isc_result_t
fromtext_l32(ARGS_FROMTEXT)27 fromtext_l32(ARGS_FROMTEXT) {
28 	isc_token_t token;
29 	struct in_addr addr;
30 	isc_region_t region;
31 
32 	REQUIRE(type == dns_rdatatype_l32);
33 
34 	UNUSED(type);
35 	UNUSED(rdclass);
36 	UNUSED(origin);
37 	UNUSED(options);
38 	UNUSED(callbacks);
39 
40 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
41 				      ISC_FALSE));
42 	if (token.value.as_ulong > 0xffffU)
43 		RETTOK(ISC_R_RANGE);
44 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
45 
46 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
47 				      ISC_FALSE));
48 
49 	if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
50 		RETTOK(DNS_R_BADDOTTEDQUAD);
51 	isc_buffer_availableregion(target, &region);
52 	if (region.length < 4)
53 		return (ISC_R_NOSPACE);
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
totext_l32(ARGS_TOTEXT)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 	sprintf(buf, "%u", num);
74 	RETERR(str_totext(buf, target));
75 
76 	RETERR(str_totext(" ", target));
77 
78 	return (inet_totext(AF_INET, &region, target));
79 }
80 
81 static inline isc_result_t
fromwire_l32(ARGS_FROMWIRE)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 	isc_buffer_forward(source, sregion.length);
96 	return (mem_tobuffer(target, sregion.base, sregion.length));
97 }
98 
99 static inline isc_result_t
towire_l32(ARGS_TOWIRE)100 towire_l32(ARGS_TOWIRE) {
101 
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
compare_l32(ARGS_COMPARE)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
fromstruct_l32(ARGS_FROMSTRUCT)127 fromstruct_l32(ARGS_FROMSTRUCT) {
128 	dns_rdata_l32_t *l32 = source;
129 	isc_uint32_t n;
130 
131 	REQUIRE(type == dns_rdatatype_l32);
132 	REQUIRE(source != 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
tostruct_l32(ARGS_TOSTRUCT)145 tostruct_l32(ARGS_TOSTRUCT) {
146 	isc_region_t region;
147 	dns_rdata_l32_t *l32 = target;
148 	isc_uint32_t n;
149 
150 	REQUIRE(rdata->type == dns_rdatatype_l32);
151 	REQUIRE(target != 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
freestruct_l32(ARGS_FREESTRUCT)168 freestruct_l32(ARGS_FREESTRUCT) {
169 	dns_rdata_l32_t *l32 = source;
170 
171 	REQUIRE(source != NULL);
172 	REQUIRE(l32->common.rdtype == dns_rdatatype_l32);
173 
174 	return;
175 }
176 
177 static inline isc_result_t
additionaldata_l32(ARGS_ADDLDATA)178 additionaldata_l32(ARGS_ADDLDATA) {
179 
180 	REQUIRE(rdata->type == dns_rdatatype_l32);
181 	REQUIRE(rdata->length == 6);
182 
183 	UNUSED(rdata);
184 	UNUSED(add);
185 	UNUSED(arg);
186 
187 	return (ISC_R_SUCCESS);
188 }
189 
190 static inline isc_result_t
digest_l32(ARGS_DIGEST)191 digest_l32(ARGS_DIGEST) {
192 	isc_region_t r;
193 
194 	REQUIRE(rdata->type == dns_rdatatype_l32);
195 	REQUIRE(rdata->length == 6);
196 
197 	dns_rdata_toregion(rdata, &r);
198 
199 	return ((digest)(arg, &r));
200 }
201 
202 static inline isc_boolean_t
checkowner_l32(ARGS_CHECKOWNER)203 checkowner_l32(ARGS_CHECKOWNER) {
204 
205 	REQUIRE(type == dns_rdatatype_l32);
206 
207 	UNUSED(name);
208 	UNUSED(type);
209 	UNUSED(rdclass);
210 	UNUSED(wildcard);
211 
212 	return (ISC_TRUE);
213 }
214 
215 static inline isc_boolean_t
checknames_l32(ARGS_CHECKNAMES)216 checknames_l32(ARGS_CHECKNAMES) {
217 
218 	REQUIRE(rdata->type == dns_rdatatype_l32);
219 	REQUIRE(rdata->length == 6);
220 
221 	UNUSED(rdata);
222 	UNUSED(owner);
223 	UNUSED(bad);
224 
225 	return (ISC_TRUE);
226 }
227 
228 static inline int
casecompare_l32(ARGS_COMPARE)229 casecompare_l32(ARGS_COMPARE) {
230 	return (compare_l32(rdata1, rdata2));
231 }
232 
233 #endif	/* RDATA_GENERIC_L32_105_C */
234