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