1 /*	$NetBSD: eui64_109.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_EUI64_109_C
20 #define RDATA_GENERIC_EUI64_109_C
21 
22 #include <string.h>
23 
24 #define RRTYPE_EUI64_ATTRIBUTES (0)
25 
26 static inline isc_result_t
27 fromtext_eui64(ARGS_FROMTEXT) {
28 	isc_token_t token;
29 	unsigned char eui64[8];
30 	unsigned int l0, l1, l2, l3, l4, l5, l6, l7;
31 	int n;
32 
33 	REQUIRE(type == 109);
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_string,
42 				      ISC_FALSE));
43 	n = sscanf(DNS_AS_STR(token), "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x",
44 		   &l0, &l1, &l2, &l3, &l4, &l5, &l6, &l7);
45 	if (n != 8 || l0 > 255U || l1 > 255U || l2 > 255U || l3 > 255U ||
46 	    l4 > 255U || l5 > 255U || l6 > 255U || l7 > 255U)
47 		return (DNS_R_BADEUI);
48 
49 	eui64[0] = l0;
50 	eui64[1] = l1;
51 	eui64[2] = l2;
52 	eui64[3] = l3;
53 	eui64[4] = l4;
54 	eui64[5] = l5;
55 	eui64[6] = l6;
56 	eui64[7] = l7;
57 	return (mem_tobuffer(target, eui64, sizeof(eui64)));
58 }
59 
60 static inline isc_result_t
61 totext_eui64(ARGS_TOTEXT) {
62 	char buf[sizeof("xx-xx-xx-xx-xx-xx-xx-xx")];
63 
64 	REQUIRE(rdata->type == 109);
65 	REQUIRE(rdata->length == 8);
66 
67 	UNUSED(tctx);
68 
69 	(void)snprintf(buf, sizeof(buf),
70 		       "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
71 		       rdata->data[0], rdata->data[1],
72 		       rdata->data[2], rdata->data[3],
73 		       rdata->data[4], rdata->data[5],
74 		       rdata->data[6], rdata->data[7]);
75 	return (str_totext(buf, target));
76 }
77 
78 static inline isc_result_t
79 fromwire_eui64(ARGS_FROMWIRE) {
80 	isc_region_t sregion;
81 
82 	REQUIRE(type == 109);
83 
84 	UNUSED(type);
85 	UNUSED(options);
86 	UNUSED(rdclass);
87 	UNUSED(dctx);
88 
89 	isc_buffer_activeregion(source, &sregion);
90 	if (sregion.length != 8)
91 		return (DNS_R_FORMERR);
92 	isc_buffer_forward(source, sregion.length);
93 	return (mem_tobuffer(target, sregion.base, sregion.length));
94 }
95 
96 static inline isc_result_t
97 towire_eui64(ARGS_TOWIRE) {
98 
99 	REQUIRE(rdata->type == 109);
100 	REQUIRE(rdata->length == 8);
101 
102 	UNUSED(cctx);
103 
104 	return (mem_tobuffer(target, rdata->data, rdata->length));
105 }
106 
107 static inline int
108 compare_eui64(ARGS_COMPARE) {
109 	isc_region_t region1;
110 	isc_region_t region2;
111 
112 	REQUIRE(rdata1->type == rdata2->type);
113 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
114 	REQUIRE(rdata1->type == 109);
115 	REQUIRE(rdata1->length == 8);
116 	REQUIRE(rdata2->length == 8);
117 
118 	dns_rdata_toregion(rdata1, &region1);
119 	dns_rdata_toregion(rdata2, &region2);
120 	return (isc_region_compare(&region1, &region2));
121 }
122 
123 static inline isc_result_t
124 fromstruct_eui64(ARGS_FROMSTRUCT) {
125 	dns_rdata_eui64_t *eui64 = source;
126 
127 	REQUIRE(type == 109);
128 	REQUIRE(source != NULL);
129 	REQUIRE(eui64->common.rdtype == type);
130 	REQUIRE(eui64->common.rdclass == rdclass);
131 
132 	UNUSED(type);
133 	UNUSED(rdclass);
134 
135 	return (mem_tobuffer(target, eui64->eui64, sizeof(eui64->eui64)));
136 }
137 
138 static inline isc_result_t
139 tostruct_eui64(ARGS_TOSTRUCT) {
140 	dns_rdata_eui64_t *eui64 = target;
141 
142 	REQUIRE(rdata->type == 109);
143 	REQUIRE(target != NULL);
144 	REQUIRE(rdata->length == 8);
145 
146 	UNUSED(mctx);
147 
148 	eui64->common.rdclass = rdata->rdclass;
149 	eui64->common.rdtype = rdata->type;
150 	ISC_LINK_INIT(&eui64->common, link);
151 
152 	memmove(eui64->eui64, rdata->data, rdata->length);
153 	return (ISC_R_SUCCESS);
154 }
155 
156 static inline void
157 freestruct_eui64(ARGS_FREESTRUCT) {
158 	dns_rdata_eui64_t *eui64 = source;
159 
160 	REQUIRE(source != NULL);
161 	REQUIRE(eui64->common.rdtype == 109);
162 
163 	return;
164 }
165 
166 static inline isc_result_t
167 additionaldata_eui64(ARGS_ADDLDATA) {
168 
169 	REQUIRE(rdata->type == 109);
170 	REQUIRE(rdata->length == 8);
171 
172 	UNUSED(rdata);
173 	UNUSED(add);
174 	UNUSED(arg);
175 
176 	return (ISC_R_SUCCESS);
177 }
178 
179 static inline isc_result_t
180 digest_eui64(ARGS_DIGEST) {
181 	isc_region_t r;
182 
183 	REQUIRE(rdata->type == 109);
184 	REQUIRE(rdata->length == 8);
185 
186 	dns_rdata_toregion(rdata, &r);
187 
188 	return ((digest)(arg, &r));
189 }
190 
191 static inline isc_boolean_t
192 checkowner_eui64(ARGS_CHECKOWNER) {
193 
194 	REQUIRE(type == 109);
195 
196 	UNUSED(name);
197 	UNUSED(type);
198 	UNUSED(rdclass);
199 	UNUSED(wildcard);
200 
201 	return (ISC_TRUE);
202 }
203 
204 static inline isc_boolean_t
205 checknames_eui64(ARGS_CHECKNAMES) {
206 
207 	REQUIRE(rdata->type == 109);
208 	REQUIRE(rdata->length == 8);
209 
210 	UNUSED(rdata);
211 	UNUSED(owner);
212 	UNUSED(bad);
213 
214 	return (ISC_TRUE);
215 }
216 
217 static inline int
218 casecompare_eui64(ARGS_COMPARE) {
219 	return (compare_eui64(rdata1, rdata2));
220 }
221 
222 #endif	/* RDATA_GENERIC_EUI64_109_C */
223