1 /*	$NetBSD: nid_104.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_NID_104_C
20 #define RDATA_GENERIC_NID_104_C
21 
22 #include <string.h>
23 
24 #include <isc/net.h>
25 
26 #define RRTYPE_NID_ATTRIBUTES (0)
27 
28 static inline isc_result_t
29 fromtext_nid(ARGS_FROMTEXT) {
30 	isc_token_t token;
31 	unsigned char locator[NS_LOCATORSZ];
32 
33 	REQUIRE(type == 104);
34 
35 	UNUSED(type);
36 	UNUSED(rdclass);
37 	UNUSED(origin);
38 	UNUSED(options);
39 	UNUSED(callbacks);
40 
41 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
42 				      ISC_FALSE));
43 	if (token.value.as_ulong > 0xffffU)
44 		RETTOK(ISC_R_RANGE);
45 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
46 
47 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
48 				      ISC_FALSE));
49 
50 	if (locator_pton(DNS_AS_STR(token), locator) != 1)
51 		RETTOK(DNS_R_SYNTAX);
52 	return (mem_tobuffer(target, locator, NS_LOCATORSZ));
53 }
54 
55 static inline isc_result_t
56 totext_nid(ARGS_TOTEXT) {
57 	isc_region_t region;
58 	char buf[sizeof("xxxx:xxxx:xxxx:xxxx")];
59 	unsigned short num;
60 
61 	REQUIRE(rdata->type == 104);
62 	REQUIRE(rdata->length != 0);
63 
64 	UNUSED(tctx);
65 
66 	dns_rdata_toregion(rdata, &region);
67 	num = uint16_fromregion(&region);
68 	isc_region_consume(&region, 2);
69 	sprintf(buf, "%u", num);
70 	RETERR(str_totext(buf, target));
71 
72 	RETERR(str_totext(" ", target));
73 
74 	sprintf(buf, "%x:%x:%x:%x",
75 		region.base[0]<<8 | region.base[1],
76 		region.base[2]<<8 | region.base[3],
77 		region.base[4]<<8 | region.base[5],
78 		region.base[6]<<8 | region.base[7]);
79 	return (str_totext(buf, target));
80 }
81 
82 static inline isc_result_t
83 fromwire_nid(ARGS_FROMWIRE) {
84 	isc_region_t sregion;
85 
86 	REQUIRE(type == 104);
87 
88 	UNUSED(type);
89 	UNUSED(options);
90 	UNUSED(rdclass);
91 	UNUSED(dctx);
92 
93 	isc_buffer_activeregion(source, &sregion);
94 	if (sregion.length != 10)
95 		return (DNS_R_FORMERR);
96 	isc_buffer_forward(source, sregion.length);
97 	return (mem_tobuffer(target, sregion.base, sregion.length));
98 }
99 
100 static inline isc_result_t
101 towire_nid(ARGS_TOWIRE) {
102 
103 	REQUIRE(rdata->type == 104);
104 	REQUIRE(rdata->length == 10);
105 
106 	UNUSED(cctx);
107 
108 	return (mem_tobuffer(target, rdata->data, rdata->length));
109 }
110 
111 static inline int
112 compare_nid(ARGS_COMPARE) {
113 	isc_region_t region1;
114 	isc_region_t region2;
115 
116 	REQUIRE(rdata1->type == rdata2->type);
117 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
118 	REQUIRE(rdata1->type == 104);
119 	REQUIRE(rdata1->length == 10);
120 	REQUIRE(rdata2->length == 10);
121 
122 	dns_rdata_toregion(rdata1, &region1);
123 	dns_rdata_toregion(rdata2, &region2);
124 	return (isc_region_compare(&region1, &region2));
125 }
126 
127 static inline isc_result_t
128 fromstruct_nid(ARGS_FROMSTRUCT) {
129 	dns_rdata_nid_t *nid = source;
130 
131 	REQUIRE(type == 104);
132 	REQUIRE(source != NULL);
133 	REQUIRE(nid->common.rdtype == type);
134 	REQUIRE(nid->common.rdclass == rdclass);
135 
136 	UNUSED(type);
137 	UNUSED(rdclass);
138 
139 	RETERR(uint16_tobuffer(nid->pref, target));
140 	return (mem_tobuffer(target, nid->nid, sizeof(nid->nid)));
141 }
142 
143 static inline isc_result_t
144 tostruct_nid(ARGS_TOSTRUCT) {
145 	isc_region_t region;
146 	dns_rdata_nid_t *nid = target;
147 
148 	REQUIRE(rdata->type == 104);
149 	REQUIRE(target != NULL);
150 	REQUIRE(rdata->length == 10);
151 
152 	UNUSED(mctx);
153 
154 	nid->common.rdclass = rdata->rdclass;
155 	nid->common.rdtype = rdata->type;
156 	ISC_LINK_INIT(&nid->common, link);
157 
158 	dns_rdata_toregion(rdata, &region);
159 	nid->pref = uint16_fromregion(&region);
160 	memmove(nid->nid, region.base, region.length);
161 	return (ISC_R_SUCCESS);
162 }
163 
164 static inline void
165 freestruct_nid(ARGS_FREESTRUCT) {
166 	dns_rdata_nid_t *nid = source;
167 
168 	REQUIRE(source != NULL);
169 	REQUIRE(nid->common.rdtype == 104);
170 
171 	return;
172 }
173 
174 static inline isc_result_t
175 additionaldata_nid(ARGS_ADDLDATA) {
176 
177 	REQUIRE(rdata->type == 104);
178 	REQUIRE(rdata->length == 10);
179 
180 	UNUSED(rdata);
181 	UNUSED(add);
182 	UNUSED(arg);
183 
184 	return (ISC_R_SUCCESS);
185 }
186 
187 static inline isc_result_t
188 digest_nid(ARGS_DIGEST) {
189 	isc_region_t r;
190 
191 	REQUIRE(rdata->type == 104);
192 	REQUIRE(rdata->length == 10);
193 
194 	dns_rdata_toregion(rdata, &r);
195 
196 	return ((digest)(arg, &r));
197 }
198 
199 static inline isc_boolean_t
200 checkowner_nid(ARGS_CHECKOWNER) {
201 
202 	REQUIRE(type == 104);
203 
204 	UNUSED(name);
205 	UNUSED(type);
206 	UNUSED(rdclass);
207 	UNUSED(wildcard);
208 
209 	return (ISC_TRUE);
210 }
211 
212 static inline isc_boolean_t
213 checknames_nid(ARGS_CHECKNAMES) {
214 
215 	REQUIRE(rdata->type == 104);
216 	REQUIRE(rdata->length == 10);
217 
218 	UNUSED(rdata);
219 	UNUSED(owner);
220 	UNUSED(bad);
221 
222 	return (ISC_TRUE);
223 }
224 
225 static inline int
226 casecompare_nid(ARGS_COMPARE) {
227 	return (compare_nid(rdata1, rdata2));
228 }
229 
230 #endif	/* RDATA_GENERIC_NID_104_C */
231