Home | History | Annotate | Line # | Download | only in in_1
      1 /*	$NetBSD: nsap_22.c,v 1.10 2026/01/29 18:37:54 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, &region);
     88 	RETERR(str_totext("0x", target));
     89 	while (region.length != 0) {
     90 		snprintf(buf, sizeof(buf), "%02x", region.base[0]);
     91 		isc_region_consume(&region, 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, &region);
    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 	DNS_RDATACOMMON_INIT(nsap, rdata->type, rdata->rdclass);
    174 
    175 	dns_rdata_toregion(rdata, &r);
    176 	nsap->nsap_len = r.length;
    177 	nsap->nsap = mem_maybedup(mctx, r.base, r.length);
    178 	nsap->mctx = mctx;
    179 	return ISC_R_SUCCESS;
    180 }
    181 
    182 static void
    183 freestruct_in_nsap(ARGS_FREESTRUCT) {
    184 	dns_rdata_in_nsap_t *nsap = source;
    185 
    186 	REQUIRE(nsap != NULL);
    187 	REQUIRE(nsap->common.rdclass == dns_rdataclass_in);
    188 	REQUIRE(nsap->common.rdtype == dns_rdatatype_nsap);
    189 
    190 	if (nsap->mctx == NULL) {
    191 		return;
    192 	}
    193 
    194 	if (nsap->nsap != NULL) {
    195 		isc_mem_free(nsap->mctx, nsap->nsap);
    196 	}
    197 	nsap->mctx = NULL;
    198 }
    199 
    200 static isc_result_t
    201 additionaldata_in_nsap(ARGS_ADDLDATA) {
    202 	REQUIRE(rdata->type == dns_rdatatype_nsap);
    203 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
    204 
    205 	UNUSED(rdata);
    206 	UNUSED(owner);
    207 	UNUSED(add);
    208 	UNUSED(arg);
    209 
    210 	return ISC_R_SUCCESS;
    211 }
    212 
    213 static isc_result_t
    214 digest_in_nsap(ARGS_DIGEST) {
    215 	isc_region_t r;
    216 
    217 	REQUIRE(rdata->type == dns_rdatatype_nsap);
    218 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
    219 
    220 	dns_rdata_toregion(rdata, &r);
    221 
    222 	return (digest)(arg, &r);
    223 }
    224 
    225 static bool
    226 checkowner_in_nsap(ARGS_CHECKOWNER) {
    227 	REQUIRE(type == dns_rdatatype_nsap);
    228 	REQUIRE(rdclass == dns_rdataclass_in);
    229 
    230 	UNUSED(name);
    231 	UNUSED(type);
    232 	UNUSED(rdclass);
    233 	UNUSED(wildcard);
    234 
    235 	return true;
    236 }
    237 
    238 static bool
    239 checknames_in_nsap(ARGS_CHECKNAMES) {
    240 	REQUIRE(rdata->type == dns_rdatatype_nsap);
    241 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
    242 
    243 	UNUSED(rdata);
    244 	UNUSED(owner);
    245 	UNUSED(bad);
    246 
    247 	return true;
    248 }
    249 
    250 static int
    251 casecompare_in_nsap(ARGS_COMPARE) {
    252 	return compare_in_nsap(rdata1, rdata2);
    253 }
    254 
    255 #endif /* RDATA_IN_1_NSAP_22_C */
    256