1 /* $NetBSD: csync_62.c,v 1.9 2025/01/26 16:25:30 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 /* RFC 7477 */ 17 18 #ifndef RDATA_GENERIC_CSYNC_62_C 19 #define RDATA_GENERIC_CSYNC_62_C 20 21 #define RRTYPE_CSYNC_ATTRIBUTES 0 22 23 static isc_result_t 24 fromtext_csync(ARGS_FROMTEXT) { 25 isc_token_t token; 26 27 REQUIRE(type == dns_rdatatype_csync); 28 29 UNUSED(type); 30 UNUSED(rdclass); 31 UNUSED(origin); 32 UNUSED(options); 33 UNUSED(callbacks); 34 35 /* Serial. */ 36 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, 37 false)); 38 RETERR(uint32_tobuffer(token.value.as_ulong, target)); 39 40 /* Flags. */ 41 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, 42 false)); 43 if (token.value.as_ulong > 0xffffU) { 44 RETTOK(ISC_R_RANGE); 45 } 46 RETERR(uint16_tobuffer(token.value.as_ulong, target)); 47 48 /* Type Map */ 49 return typemap_fromtext(lexer, target, true); 50 } 51 52 static isc_result_t 53 totext_csync(ARGS_TOTEXT) { 54 unsigned long num; 55 char buf[sizeof("0123456789")]; /* Also TYPE65535 */ 56 isc_region_t sr; 57 58 REQUIRE(rdata->type == dns_rdatatype_csync); 59 REQUIRE(rdata->length >= 6); 60 61 UNUSED(tctx); 62 63 dns_rdata_toregion(rdata, &sr); 64 65 num = uint32_fromregion(&sr); 66 isc_region_consume(&sr, 4); 67 snprintf(buf, sizeof(buf), "%lu", num); 68 RETERR(str_totext(buf, target)); 69 70 RETERR(str_totext(" ", target)); 71 72 num = uint16_fromregion(&sr); 73 isc_region_consume(&sr, 2); 74 snprintf(buf, sizeof(buf), "%lu", num); 75 RETERR(str_totext(buf, target)); 76 77 /* 78 * Don't leave a trailing space when there's no typemap present. 79 */ 80 if (sr.length > 0) { 81 RETERR(str_totext(" ", target)); 82 } 83 return typemap_totext(&sr, NULL, target); 84 } 85 86 static isc_result_t 87 fromwire_csync(ARGS_FROMWIRE) { 88 isc_region_t sr; 89 90 REQUIRE(type == dns_rdatatype_csync); 91 92 UNUSED(type); 93 UNUSED(rdclass); 94 UNUSED(dctx); 95 96 /* 97 * Serial + Flags 98 */ 99 isc_buffer_activeregion(source, &sr); 100 if (sr.length < 6) { 101 return ISC_R_UNEXPECTEDEND; 102 } 103 104 RETERR(mem_tobuffer(target, sr.base, 6)); 105 isc_buffer_forward(source, 6); 106 isc_region_consume(&sr, 6); 107 108 RETERR(typemap_test(&sr, true)); 109 110 RETERR(mem_tobuffer(target, sr.base, sr.length)); 111 isc_buffer_forward(source, sr.length); 112 return ISC_R_SUCCESS; 113 } 114 115 static isc_result_t 116 towire_csync(ARGS_TOWIRE) { 117 REQUIRE(rdata->type == dns_rdatatype_csync); 118 REQUIRE(rdata->length >= 6); 119 120 UNUSED(cctx); 121 122 return mem_tobuffer(target, rdata->data, rdata->length); 123 } 124 125 static int 126 compare_csync(ARGS_COMPARE) { 127 isc_region_t r1; 128 isc_region_t r2; 129 130 REQUIRE(rdata1->type == rdata2->type); 131 REQUIRE(rdata1->rdclass == rdata2->rdclass); 132 REQUIRE(rdata1->type == dns_rdatatype_csync); 133 REQUIRE(rdata1->length >= 6); 134 REQUIRE(rdata2->length >= 6); 135 136 dns_rdata_toregion(rdata1, &r1); 137 dns_rdata_toregion(rdata2, &r2); 138 return isc_region_compare(&r1, &r2); 139 } 140 141 static isc_result_t 142 fromstruct_csync(ARGS_FROMSTRUCT) { 143 dns_rdata_csync_t *csync = source; 144 isc_region_t region; 145 146 REQUIRE(type == dns_rdatatype_csync); 147 REQUIRE(csync != NULL); 148 REQUIRE(csync->common.rdtype == type); 149 REQUIRE(csync->common.rdclass == rdclass); 150 REQUIRE(csync->typebits != NULL || csync->len == 0); 151 152 UNUSED(type); 153 UNUSED(rdclass); 154 155 RETERR(uint32_tobuffer(csync->serial, target)); 156 RETERR(uint16_tobuffer(csync->flags, target)); 157 158 region.base = csync->typebits; 159 region.length = csync->len; 160 RETERR(typemap_test(®ion, true)); 161 return mem_tobuffer(target, csync->typebits, csync->len); 162 } 163 164 static isc_result_t 165 tostruct_csync(ARGS_TOSTRUCT) { 166 isc_region_t region; 167 dns_rdata_csync_t *csync = target; 168 169 REQUIRE(rdata->type == dns_rdatatype_csync); 170 REQUIRE(csync != NULL); 171 REQUIRE(rdata->length != 0); 172 173 csync->common.rdclass = rdata->rdclass; 174 csync->common.rdtype = rdata->type; 175 ISC_LINK_INIT(&csync->common, link); 176 177 dns_rdata_toregion(rdata, ®ion); 178 179 csync->serial = uint32_fromregion(®ion); 180 isc_region_consume(®ion, 4); 181 182 csync->flags = uint16_fromregion(®ion); 183 isc_region_consume(®ion, 2); 184 185 csync->len = region.length; 186 csync->typebits = mem_maybedup(mctx, region.base, region.length); 187 csync->mctx = mctx; 188 return ISC_R_SUCCESS; 189 } 190 191 static void 192 freestruct_csync(ARGS_FREESTRUCT) { 193 dns_rdata_csync_t *csync = source; 194 195 REQUIRE(csync != NULL); 196 REQUIRE(csync->common.rdtype == dns_rdatatype_csync); 197 198 if (csync->mctx == NULL) { 199 return; 200 } 201 202 if (csync->typebits != NULL) { 203 isc_mem_free(csync->mctx, csync->typebits); 204 } 205 csync->mctx = NULL; 206 } 207 208 static isc_result_t 209 additionaldata_csync(ARGS_ADDLDATA) { 210 REQUIRE(rdata->type == dns_rdatatype_csync); 211 212 UNUSED(rdata); 213 UNUSED(owner); 214 UNUSED(add); 215 UNUSED(arg); 216 217 return ISC_R_SUCCESS; 218 } 219 220 static isc_result_t 221 digest_csync(ARGS_DIGEST) { 222 isc_region_t r; 223 224 REQUIRE(rdata->type == dns_rdatatype_csync); 225 226 dns_rdata_toregion(rdata, &r); 227 return (digest)(arg, &r); 228 } 229 230 static bool 231 checkowner_csync(ARGS_CHECKOWNER) { 232 REQUIRE(type == dns_rdatatype_csync); 233 234 UNUSED(name); 235 UNUSED(type); 236 UNUSED(rdclass); 237 UNUSED(wildcard); 238 239 return true; 240 } 241 242 static bool 243 checknames_csync(ARGS_CHECKNAMES) { 244 REQUIRE(rdata->type == dns_rdatatype_csync); 245 246 UNUSED(rdata); 247 UNUSED(owner); 248 UNUSED(bad); 249 250 return true; 251 } 252 253 static int 254 casecompare_csync(ARGS_COMPARE) { 255 isc_region_t region1; 256 isc_region_t region2; 257 258 REQUIRE(rdata1->type == rdata2->type); 259 REQUIRE(rdata1->rdclass == rdata2->rdclass); 260 REQUIRE(rdata1->type == dns_rdatatype_csync); 261 REQUIRE(rdata1->length >= 6); 262 REQUIRE(rdata2->length >= 6); 263 264 dns_rdata_toregion(rdata1, ®ion1); 265 dns_rdata_toregion(rdata2, ®ion2); 266 return isc_region_compare(®ion1, ®ion2); 267 } 268 #endif /* RDATA_GENERIC_CSYNC_62_C */ 269