Home | History | Annotate | Line # | Download | only in generic
      1 /*	$NetBSD: eui48_108.c,v 1.1 2024/02/18 20:57:41 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #ifndef RDATA_GENERIC_EUI48_108_C
     17 #define RDATA_GENERIC_EUI48_108_C
     18 
     19 #include <string.h>
     20 
     21 #define RRTYPE_EUI48_ATTRIBUTES (0)
     22 
     23 static isc_result_t
     24 fromtext_eui48(ARGS_FROMTEXT) {
     25 	isc_token_t token;
     26 	unsigned char eui48[6];
     27 	unsigned int l0, l1, l2, l3, l4, l5;
     28 	int n;
     29 
     30 	REQUIRE(type == dns_rdatatype_eui48);
     31 
     32 	UNUSED(type);
     33 	UNUSED(rdclass);
     34 	UNUSED(origin);
     35 	UNUSED(options);
     36 	UNUSED(callbacks);
     37 
     38 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
     39 				      false));
     40 	n = sscanf(DNS_AS_STR(token), "%2x-%2x-%2x-%2x-%2x-%2x", &l0, &l1, &l2,
     41 		   &l3, &l4, &l5);
     42 	if (n != 6 || l0 > 255U || l1 > 255U || l2 > 255U || l3 > 255U ||
     43 	    l4 > 255U || l5 > 255U)
     44 	{
     45 		return (DNS_R_BADEUI);
     46 	}
     47 
     48 	eui48[0] = l0;
     49 	eui48[1] = l1;
     50 	eui48[2] = l2;
     51 	eui48[3] = l3;
     52 	eui48[4] = l4;
     53 	eui48[5] = l5;
     54 	return (mem_tobuffer(target, eui48, sizeof(eui48)));
     55 }
     56 
     57 static isc_result_t
     58 totext_eui48(ARGS_TOTEXT) {
     59 	char buf[sizeof("xx-xx-xx-xx-xx-xx")];
     60 
     61 	REQUIRE(rdata->type == dns_rdatatype_eui48);
     62 	REQUIRE(rdata->length == 6);
     63 
     64 	UNUSED(tctx);
     65 
     66 	(void)snprintf(buf, sizeof(buf), "%02x-%02x-%02x-%02x-%02x-%02x",
     67 		       rdata->data[0], rdata->data[1], rdata->data[2],
     68 		       rdata->data[3], rdata->data[4], rdata->data[5]);
     69 	return (str_totext(buf, target));
     70 }
     71 
     72 static isc_result_t
     73 fromwire_eui48(ARGS_FROMWIRE) {
     74 	isc_region_t sregion;
     75 
     76 	REQUIRE(type == dns_rdatatype_eui48);
     77 
     78 	UNUSED(type);
     79 	UNUSED(options);
     80 	UNUSED(rdclass);
     81 	UNUSED(dctx);
     82 
     83 	isc_buffer_activeregion(source, &sregion);
     84 	if (sregion.length != 6) {
     85 		return (DNS_R_FORMERR);
     86 	}
     87 	isc_buffer_forward(source, sregion.length);
     88 	return (mem_tobuffer(target, sregion.base, sregion.length));
     89 }
     90 
     91 static isc_result_t
     92 towire_eui48(ARGS_TOWIRE) {
     93 	REQUIRE(rdata->type == dns_rdatatype_eui48);
     94 	REQUIRE(rdata->length == 6);
     95 
     96 	UNUSED(cctx);
     97 
     98 	return (mem_tobuffer(target, rdata->data, rdata->length));
     99 }
    100 
    101 static int
    102 compare_eui48(ARGS_COMPARE) {
    103 	isc_region_t region1;
    104 	isc_region_t region2;
    105 
    106 	REQUIRE(rdata1->type == rdata2->type);
    107 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
    108 	REQUIRE(rdata1->type == dns_rdatatype_eui48);
    109 	REQUIRE(rdata1->length == 6);
    110 	REQUIRE(rdata2->length == 6);
    111 
    112 	dns_rdata_toregion(rdata1, &region1);
    113 	dns_rdata_toregion(rdata2, &region2);
    114 	return (isc_region_compare(&region1, &region2));
    115 }
    116 
    117 static isc_result_t
    118 fromstruct_eui48(ARGS_FROMSTRUCT) {
    119 	dns_rdata_eui48_t *eui48 = source;
    120 
    121 	REQUIRE(type == dns_rdatatype_eui48);
    122 	REQUIRE(eui48 != NULL);
    123 	REQUIRE(eui48->common.rdtype == type);
    124 	REQUIRE(eui48->common.rdclass == rdclass);
    125 
    126 	UNUSED(type);
    127 	UNUSED(rdclass);
    128 
    129 	return (mem_tobuffer(target, eui48->eui48, sizeof(eui48->eui48)));
    130 }
    131 
    132 static isc_result_t
    133 tostruct_eui48(ARGS_TOSTRUCT) {
    134 	dns_rdata_eui48_t *eui48 = target;
    135 
    136 	REQUIRE(rdata->type == dns_rdatatype_eui48);
    137 	REQUIRE(eui48 != NULL);
    138 	REQUIRE(rdata->length == 6);
    139 
    140 	UNUSED(mctx);
    141 
    142 	eui48->common.rdclass = rdata->rdclass;
    143 	eui48->common.rdtype = rdata->type;
    144 	ISC_LINK_INIT(&eui48->common, link);
    145 
    146 	memmove(eui48->eui48, rdata->data, rdata->length);
    147 	return (ISC_R_SUCCESS);
    148 }
    149 
    150 static void
    151 freestruct_eui48(ARGS_FREESTRUCT) {
    152 	dns_rdata_eui48_t *eui48 = source;
    153 
    154 	REQUIRE(eui48 != NULL);
    155 	REQUIRE(eui48->common.rdtype == dns_rdatatype_eui48);
    156 
    157 	return;
    158 }
    159 
    160 static isc_result_t
    161 additionaldata_eui48(ARGS_ADDLDATA) {
    162 	REQUIRE(rdata->type == dns_rdatatype_eui48);
    163 	REQUIRE(rdata->length == 6);
    164 
    165 	UNUSED(rdata);
    166 	UNUSED(add);
    167 	UNUSED(arg);
    168 
    169 	return (ISC_R_SUCCESS);
    170 }
    171 
    172 static isc_result_t
    173 digest_eui48(ARGS_DIGEST) {
    174 	isc_region_t r;
    175 
    176 	REQUIRE(rdata->type == dns_rdatatype_eui48);
    177 	REQUIRE(rdata->length == 6);
    178 
    179 	dns_rdata_toregion(rdata, &r);
    180 
    181 	return ((digest)(arg, &r));
    182 }
    183 
    184 static bool
    185 checkowner_eui48(ARGS_CHECKOWNER) {
    186 	REQUIRE(type == dns_rdatatype_eui48);
    187 
    188 	UNUSED(name);
    189 	UNUSED(type);
    190 	UNUSED(rdclass);
    191 	UNUSED(wildcard);
    192 
    193 	return (true);
    194 }
    195 
    196 static bool
    197 checknames_eui48(ARGS_CHECKNAMES) {
    198 	REQUIRE(rdata->type == dns_rdatatype_eui48);
    199 	REQUIRE(rdata->length == 6);
    200 
    201 	UNUSED(rdata);
    202 	UNUSED(owner);
    203 	UNUSED(bad);
    204 
    205 	return (true);
    206 }
    207 
    208 static int
    209 casecompare_eui48(ARGS_COMPARE) {
    210 	return (compare_eui48(rdata1, rdata2));
    211 }
    212 
    213 #endif /* RDATA_GENERIC_EUI48_108_C */
    214