1 /* $NetBSD: nid_104.c,v 1.8 2024/02/21 22:52:13 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, ®ion); 66 num = uint16_fromregion(®ion); 67 isc_region_consume(®ion, 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(options); 89 UNUSED(rdclass); 90 UNUSED(dctx); 91 92 isc_buffer_activeregion(source, &sregion); 93 if (sregion.length != 10) { 94 return (DNS_R_FORMERR); 95 } 96 isc_buffer_forward(source, sregion.length); 97 return (mem_tobuffer(target, sregion.base, sregion.length)); 98 } 99 100 static isc_result_t 101 towire_nid(ARGS_TOWIRE) { 102 REQUIRE(rdata->type == dns_rdatatype_nid); 103 REQUIRE(rdata->length == 10); 104 105 UNUSED(cctx); 106 107 return (mem_tobuffer(target, rdata->data, rdata->length)); 108 } 109 110 static int 111 compare_nid(ARGS_COMPARE) { 112 isc_region_t region1; 113 isc_region_t region2; 114 115 REQUIRE(rdata1->type == rdata2->type); 116 REQUIRE(rdata1->rdclass == rdata2->rdclass); 117 REQUIRE(rdata1->type == dns_rdatatype_nid); 118 REQUIRE(rdata1->length == 10); 119 REQUIRE(rdata2->length == 10); 120 121 dns_rdata_toregion(rdata1, ®ion1); 122 dns_rdata_toregion(rdata2, ®ion2); 123 return (isc_region_compare(®ion1, ®ion2)); 124 } 125 126 static isc_result_t 127 fromstruct_nid(ARGS_FROMSTRUCT) { 128 dns_rdata_nid_t *nid = source; 129 130 REQUIRE(type == dns_rdatatype_nid); 131 REQUIRE(nid != NULL); 132 REQUIRE(nid->common.rdtype == type); 133 REQUIRE(nid->common.rdclass == rdclass); 134 135 UNUSED(type); 136 UNUSED(rdclass); 137 138 RETERR(uint16_tobuffer(nid->pref, target)); 139 return (mem_tobuffer(target, nid->nid, sizeof(nid->nid))); 140 } 141 142 static isc_result_t 143 tostruct_nid(ARGS_TOSTRUCT) { 144 isc_region_t region; 145 dns_rdata_nid_t *nid = target; 146 147 REQUIRE(rdata->type == dns_rdatatype_nid); 148 REQUIRE(nid != NULL); 149 REQUIRE(rdata->length == 10); 150 151 UNUSED(mctx); 152 153 nid->common.rdclass = rdata->rdclass; 154 nid->common.rdtype = rdata->type; 155 ISC_LINK_INIT(&nid->common, link); 156 157 dns_rdata_toregion(rdata, ®ion); 158 nid->pref = uint16_fromregion(®ion); 159 memmove(nid->nid, region.base, region.length); 160 return (ISC_R_SUCCESS); 161 } 162 163 static void 164 freestruct_nid(ARGS_FREESTRUCT) { 165 dns_rdata_nid_t *nid = source; 166 167 REQUIRE(nid != NULL); 168 REQUIRE(nid->common.rdtype == dns_rdatatype_nid); 169 170 return; 171 } 172 173 static isc_result_t 174 additionaldata_nid(ARGS_ADDLDATA) { 175 REQUIRE(rdata->type == dns_rdatatype_nid); 176 REQUIRE(rdata->length == 10); 177 178 UNUSED(rdata); 179 UNUSED(owner); 180 UNUSED(add); 181 UNUSED(arg); 182 183 return (ISC_R_SUCCESS); 184 } 185 186 static isc_result_t 187 digest_nid(ARGS_DIGEST) { 188 isc_region_t r; 189 190 REQUIRE(rdata->type == dns_rdatatype_nid); 191 REQUIRE(rdata->length == 10); 192 193 dns_rdata_toregion(rdata, &r); 194 195 return ((digest)(arg, &r)); 196 } 197 198 static bool 199 checkowner_nid(ARGS_CHECKOWNER) { 200 REQUIRE(type == dns_rdatatype_nid); 201 202 UNUSED(name); 203 UNUSED(type); 204 UNUSED(rdclass); 205 UNUSED(wildcard); 206 207 return (true); 208 } 209 210 static bool 211 checknames_nid(ARGS_CHECKNAMES) { 212 REQUIRE(rdata->type == dns_rdatatype_nid); 213 REQUIRE(rdata->length == 10); 214 215 UNUSED(rdata); 216 UNUSED(owner); 217 UNUSED(bad); 218 219 return (true); 220 } 221 222 static int 223 casecompare_nid(ARGS_COMPARE) { 224 return (compare_nid(rdata1, rdata2)); 225 } 226 227 #endif /* RDATA_GENERIC_NID_104_C */ 228