1 /* $NetBSD: nsap_22.c,v 1.9 2025/01/26 16:25:35 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 /* RFC1706 */ 17 18 #ifndef RDATA_IN_1_NSAP_22_C 19 #define RDATA_IN_1_NSAP_22_C 20 21 #define RRTYPE_NSAP_ATTRIBUTES (0) 22 23 static isc_result_t 24 fromtext_in_nsap(ARGS_FROMTEXT) { 25 isc_token_t token; 26 isc_textregion_t *sr; 27 int n; 28 bool valid = false; 29 int digits = 0; 30 unsigned char c = 0; 31 32 REQUIRE(type == dns_rdatatype_nsap); 33 REQUIRE(rdclass == dns_rdataclass_in); 34 35 UNUSED(type); 36 UNUSED(origin); 37 UNUSED(options); 38 UNUSED(rdclass); 39 UNUSED(callbacks); 40 41 /* 0x<hex.string.with.periods> */ 42 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string, 43 false)); 44 sr = &token.value.as_textregion; 45 if (sr->length < 2) { 46 RETTOK(ISC_R_UNEXPECTEDEND); 47 } 48 if (sr->base[0] != '0' || (sr->base[1] != 'x' && sr->base[1] != 'X')) { 49 RETTOK(DNS_R_SYNTAX); 50 } 51 isc_textregion_consume(sr, 2); 52 while (sr->length > 0) { 53 if (sr->base[0] == '.') { 54 isc_textregion_consume(sr, 1); 55 continue; 56 } 57 if ((n = hexvalue(sr->base[0])) == -1) { 58 RETTOK(DNS_R_SYNTAX); 59 } 60 c <<= 4; 61 c += n; 62 if (++digits == 2) { 63 RETERR(mem_tobuffer(target, &c, 1)); 64 valid = true; 65 digits = 0; 66 c = 0; 67 } 68 isc_textregion_consume(sr, 1); 69 } 70 if (digits != 0 || !valid) { 71 RETTOK(ISC_R_UNEXPECTEDEND); 72 } 73 return ISC_R_SUCCESS; 74 } 75 76 static isc_result_t 77 totext_in_nsap(ARGS_TOTEXT) { 78 isc_region_t region; 79 char buf[sizeof("xx")]; 80 81 REQUIRE(rdata->type == dns_rdatatype_nsap); 82 REQUIRE(rdata->rdclass == dns_rdataclass_in); 83 REQUIRE(rdata->length != 0); 84 85 UNUSED(tctx); 86 87 dns_rdata_toregion(rdata, ®ion); 88 RETERR(str_totext("0x", target)); 89 while (region.length != 0) { 90 snprintf(buf, sizeof(buf), "%02x", region.base[0]); 91 isc_region_consume(®ion, 1); 92 RETERR(str_totext(buf, target)); 93 } 94 return ISC_R_SUCCESS; 95 } 96 97 static isc_result_t 98 fromwire_in_nsap(ARGS_FROMWIRE) { 99 isc_region_t region; 100 101 REQUIRE(type == dns_rdatatype_nsap); 102 REQUIRE(rdclass == dns_rdataclass_in); 103 104 UNUSED(type); 105 UNUSED(dctx); 106 UNUSED(rdclass); 107 108 isc_buffer_activeregion(source, ®ion); 109 if (region.length < 1) { 110 return ISC_R_UNEXPECTEDEND; 111 } 112 113 RETERR(mem_tobuffer(target, region.base, region.length)); 114 isc_buffer_forward(source, region.length); 115 return ISC_R_SUCCESS; 116 } 117 118 static isc_result_t 119 towire_in_nsap(ARGS_TOWIRE) { 120 REQUIRE(rdata->type == dns_rdatatype_nsap); 121 REQUIRE(rdata->rdclass == dns_rdataclass_in); 122 REQUIRE(rdata->length != 0); 123 124 UNUSED(cctx); 125 126 return mem_tobuffer(target, rdata->data, rdata->length); 127 } 128 129 static int 130 compare_in_nsap(ARGS_COMPARE) { 131 isc_region_t r1; 132 isc_region_t r2; 133 134 REQUIRE(rdata1->type == rdata2->type); 135 REQUIRE(rdata1->rdclass == rdata2->rdclass); 136 REQUIRE(rdata1->type == dns_rdatatype_nsap); 137 REQUIRE(rdata1->rdclass == dns_rdataclass_in); 138 REQUIRE(rdata1->length != 0); 139 REQUIRE(rdata2->length != 0); 140 141 dns_rdata_toregion(rdata1, &r1); 142 dns_rdata_toregion(rdata2, &r2); 143 return isc_region_compare(&r1, &r2); 144 } 145 146 static isc_result_t 147 fromstruct_in_nsap(ARGS_FROMSTRUCT) { 148 dns_rdata_in_nsap_t *nsap = source; 149 150 REQUIRE(type == dns_rdatatype_nsap); 151 REQUIRE(rdclass == dns_rdataclass_in); 152 REQUIRE(nsap != NULL); 153 REQUIRE(nsap->common.rdtype == type); 154 REQUIRE(nsap->common.rdclass == rdclass); 155 REQUIRE(nsap->nsap != NULL || nsap->nsap_len == 0); 156 157 UNUSED(type); 158 UNUSED(rdclass); 159 160 return mem_tobuffer(target, nsap->nsap, nsap->nsap_len); 161 } 162 163 static isc_result_t 164 tostruct_in_nsap(ARGS_TOSTRUCT) { 165 dns_rdata_in_nsap_t *nsap = target; 166 isc_region_t r; 167 168 REQUIRE(rdata->type == dns_rdatatype_nsap); 169 REQUIRE(rdata->rdclass == dns_rdataclass_in); 170 REQUIRE(nsap != NULL); 171 REQUIRE(rdata->length != 0); 172 173 nsap->common.rdclass = rdata->rdclass; 174 nsap->common.rdtype = rdata->type; 175 ISC_LINK_INIT(&nsap->common, link); 176 177 dns_rdata_toregion(rdata, &r); 178 nsap->nsap_len = r.length; 179 nsap->nsap = mem_maybedup(mctx, r.base, r.length); 180 nsap->mctx = mctx; 181 return ISC_R_SUCCESS; 182 } 183 184 static void 185 freestruct_in_nsap(ARGS_FREESTRUCT) { 186 dns_rdata_in_nsap_t *nsap = source; 187 188 REQUIRE(nsap != NULL); 189 REQUIRE(nsap->common.rdclass == dns_rdataclass_in); 190 REQUIRE(nsap->common.rdtype == dns_rdatatype_nsap); 191 192 if (nsap->mctx == NULL) { 193 return; 194 } 195 196 if (nsap->nsap != NULL) { 197 isc_mem_free(nsap->mctx, nsap->nsap); 198 } 199 nsap->mctx = NULL; 200 } 201 202 static isc_result_t 203 additionaldata_in_nsap(ARGS_ADDLDATA) { 204 REQUIRE(rdata->type == dns_rdatatype_nsap); 205 REQUIRE(rdata->rdclass == dns_rdataclass_in); 206 207 UNUSED(rdata); 208 UNUSED(owner); 209 UNUSED(add); 210 UNUSED(arg); 211 212 return ISC_R_SUCCESS; 213 } 214 215 static isc_result_t 216 digest_in_nsap(ARGS_DIGEST) { 217 isc_region_t r; 218 219 REQUIRE(rdata->type == dns_rdatatype_nsap); 220 REQUIRE(rdata->rdclass == dns_rdataclass_in); 221 222 dns_rdata_toregion(rdata, &r); 223 224 return (digest)(arg, &r); 225 } 226 227 static bool 228 checkowner_in_nsap(ARGS_CHECKOWNER) { 229 REQUIRE(type == dns_rdatatype_nsap); 230 REQUIRE(rdclass == dns_rdataclass_in); 231 232 UNUSED(name); 233 UNUSED(type); 234 UNUSED(rdclass); 235 UNUSED(wildcard); 236 237 return true; 238 } 239 240 static bool 241 checknames_in_nsap(ARGS_CHECKNAMES) { 242 REQUIRE(rdata->type == dns_rdatatype_nsap); 243 REQUIRE(rdata->rdclass == dns_rdataclass_in); 244 245 UNUSED(rdata); 246 UNUSED(owner); 247 UNUSED(bad); 248 249 return true; 250 } 251 252 static int 253 casecompare_in_nsap(ARGS_COMPARE) { 254 return compare_in_nsap(rdata1, rdata2); 255 } 256 257 #endif /* RDATA_IN_1_NSAP_22_C */ 258