1 /* $NetBSD: l32_105.c,v 1.1.1.4 2014/12/10 03:34:42 christos Exp $ */
2
3 /*
4 * Copyright (C) 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef RDATA_GENERIC_L32_105_C
20 #define RDATA_GENERIC_L32_105_C
21
22 #include <string.h>
23
24 #include <isc/net.h>
25
26 #define RRTYPE_L32_ATTRIBUTES (0)
27
28 static inline isc_result_t
fromtext_l32(ARGS_FROMTEXT)29 fromtext_l32(ARGS_FROMTEXT) {
30 isc_token_t token;
31 struct in_addr addr;
32 isc_region_t region;
33
34 REQUIRE(type == 105);
35
36 UNUSED(type);
37 UNUSED(rdclass);
38 UNUSED(origin);
39 UNUSED(options);
40 UNUSED(callbacks);
41
42 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
43 ISC_FALSE));
44 if (token.value.as_ulong > 0xffffU)
45 RETTOK(ISC_R_RANGE);
46 RETERR(uint16_tobuffer(token.value.as_ulong, target));
47
48 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
49 ISC_FALSE));
50
51 if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
52 RETTOK(DNS_R_BADDOTTEDQUAD);
53 isc_buffer_availableregion(target, ®ion);
54 if (region.length < 4)
55 return (ISC_R_NOSPACE);
56 memmove(region.base, &addr, 4);
57 isc_buffer_add(target, 4);
58 return (ISC_R_SUCCESS);
59 }
60
61 static inline 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 == 105);
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 sprintf(buf, "%u", num);
76 RETERR(str_totext(buf, target));
77
78 RETERR(str_totext(" ", target));
79
80 return (inet_totext(AF_INET, ®ion, target));
81 }
82
83 static inline isc_result_t
fromwire_l32(ARGS_FROMWIRE)84 fromwire_l32(ARGS_FROMWIRE) {
85 isc_region_t sregion;
86
87 REQUIRE(type == 105);
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 isc_buffer_forward(source, sregion.length);
98 return (mem_tobuffer(target, sregion.base, sregion.length));
99 }
100
101 static inline isc_result_t
towire_l32(ARGS_TOWIRE)102 towire_l32(ARGS_TOWIRE) {
103
104 REQUIRE(rdata->type == 105);
105 REQUIRE(rdata->length == 6);
106
107 UNUSED(cctx);
108
109 return (mem_tobuffer(target, rdata->data, rdata->length));
110 }
111
112 static inline 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 == 105);
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 inline isc_result_t
fromstruct_l32(ARGS_FROMSTRUCT)129 fromstruct_l32(ARGS_FROMSTRUCT) {
130 dns_rdata_l32_t *l32 = source;
131 isc_uint32_t n;
132
133 REQUIRE(type == 105);
134 REQUIRE(source != 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 inline 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 isc_uint32_t n;
151
152 REQUIRE(rdata->type == 105);
153 REQUIRE(target != 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 inline void
freestruct_l32(ARGS_FREESTRUCT)170 freestruct_l32(ARGS_FREESTRUCT) {
171 dns_rdata_l32_t *l32 = source;
172
173 REQUIRE(source != NULL);
174 REQUIRE(l32->common.rdtype == 105);
175
176 return;
177 }
178
179 static inline isc_result_t
additionaldata_l32(ARGS_ADDLDATA)180 additionaldata_l32(ARGS_ADDLDATA) {
181
182 REQUIRE(rdata->type == 105);
183 REQUIRE(rdata->length == 6);
184
185 UNUSED(rdata);
186 UNUSED(add);
187 UNUSED(arg);
188
189 return (ISC_R_SUCCESS);
190 }
191
192 static inline isc_result_t
digest_l32(ARGS_DIGEST)193 digest_l32(ARGS_DIGEST) {
194 isc_region_t r;
195
196 REQUIRE(rdata->type == 105);
197 REQUIRE(rdata->length == 6);
198
199 dns_rdata_toregion(rdata, &r);
200
201 return ((digest)(arg, &r));
202 }
203
204 static inline isc_boolean_t
checkowner_l32(ARGS_CHECKOWNER)205 checkowner_l32(ARGS_CHECKOWNER) {
206
207 REQUIRE(type == 105);
208
209 UNUSED(name);
210 UNUSED(type);
211 UNUSED(rdclass);
212 UNUSED(wildcard);
213
214 return (ISC_TRUE);
215 }
216
217 static inline isc_boolean_t
checknames_l32(ARGS_CHECKNAMES)218 checknames_l32(ARGS_CHECKNAMES) {
219
220 REQUIRE(rdata->type == 105);
221 REQUIRE(rdata->length == 6);
222
223 UNUSED(rdata);
224 UNUSED(owner);
225 UNUSED(bad);
226
227 return (ISC_TRUE);
228 }
229
230 static inline int
casecompare_l32(ARGS_COMPARE)231 casecompare_l32(ARGS_COMPARE) {
232 return (compare_l32(rdata1, rdata2));
233 }
234
235 #endif /* RDATA_GENERIC_L32_105_C */
236