1 /* $NetBSD: lp_107.c,v 1.6 2021/02/19 16:42:17 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 9 * 10 * See the COPYRIGHT file distributed with this work for additional 11 * information regarding copyright ownership. 12 */ 13 14 #ifndef RDATA_GENERIC_LP_107_C 15 #define RDATA_GENERIC_LP_107_C 16 17 #include <string.h> 18 19 #include <isc/net.h> 20 21 #define RRTYPE_LP_ATTRIBUTES (0) 22 23 static inline isc_result_t 24 fromtext_lp(ARGS_FROMTEXT) { 25 isc_token_t token; 26 dns_name_t name; 27 isc_buffer_t buffer; 28 29 REQUIRE(type == dns_rdatatype_lp); 30 31 UNUSED(type); 32 UNUSED(rdclass); 33 UNUSED(callbacks); 34 35 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, 36 false)); 37 if (token.value.as_ulong > 0xffffU) { 38 RETTOK(ISC_R_RANGE); 39 } 40 RETERR(uint16_tobuffer(token.value.as_ulong, target)); 41 42 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string, 43 false)); 44 45 dns_name_init(&name, NULL); 46 buffer_fromregion(&buffer, &token.value.as_region); 47 if (origin == NULL) { 48 origin = dns_rootname; 49 } 50 return (dns_name_fromtext(&name, &buffer, origin, options, target)); 51 } 52 53 static inline isc_result_t 54 totext_lp(ARGS_TOTEXT) { 55 isc_region_t region; 56 dns_name_t name; 57 dns_name_t prefix; 58 bool sub; 59 char buf[sizeof("64000")]; 60 unsigned short num; 61 62 REQUIRE(rdata->type == dns_rdatatype_lp); 63 REQUIRE(rdata->length != 0); 64 65 dns_name_init(&name, NULL); 66 dns_name_init(&prefix, NULL); 67 68 dns_rdata_toregion(rdata, ®ion); 69 num = uint16_fromregion(®ion); 70 isc_region_consume(®ion, 2); 71 snprintf(buf, sizeof(buf), "%u", num); 72 RETERR(str_totext(buf, target)); 73 74 RETERR(str_totext(" ", target)); 75 76 dns_name_fromregion(&name, ®ion); 77 sub = name_prefix(&name, tctx->origin, &prefix); 78 return (dns_name_totext(&prefix, sub, target)); 79 } 80 81 static inline isc_result_t 82 fromwire_lp(ARGS_FROMWIRE) { 83 dns_name_t name; 84 isc_region_t sregion; 85 86 REQUIRE(type == dns_rdatatype_lp); 87 88 UNUSED(type); 89 UNUSED(rdclass); 90 91 dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14); 92 93 dns_name_init(&name, NULL); 94 95 isc_buffer_activeregion(source, &sregion); 96 if (sregion.length < 2) { 97 return (ISC_R_UNEXPECTEDEND); 98 } 99 RETERR(mem_tobuffer(target, sregion.base, 2)); 100 isc_buffer_forward(source, 2); 101 return (dns_name_fromwire(&name, source, dctx, options, target)); 102 } 103 104 static inline isc_result_t 105 towire_lp(ARGS_TOWIRE) { 106 REQUIRE(rdata->type == dns_rdatatype_lp); 107 REQUIRE(rdata->length != 0); 108 109 UNUSED(cctx); 110 111 return (mem_tobuffer(target, rdata->data, rdata->length)); 112 } 113 114 static inline int 115 compare_lp(ARGS_COMPARE) { 116 isc_region_t region1; 117 isc_region_t region2; 118 119 REQUIRE(rdata1->type == rdata2->type); 120 REQUIRE(rdata1->rdclass == rdata2->rdclass); 121 REQUIRE(rdata1->type == dns_rdatatype_lp); 122 REQUIRE(rdata1->length != 0); 123 REQUIRE(rdata2->length != 0); 124 125 dns_rdata_toregion(rdata1, ®ion1); 126 dns_rdata_toregion(rdata2, ®ion2); 127 128 return (isc_region_compare(®ion1, ®ion2)); 129 } 130 131 static inline isc_result_t 132 fromstruct_lp(ARGS_FROMSTRUCT) { 133 dns_rdata_lp_t *lp = source; 134 isc_region_t region; 135 136 REQUIRE(type == dns_rdatatype_lp); 137 REQUIRE(lp != NULL); 138 REQUIRE(lp->common.rdtype == type); 139 REQUIRE(lp->common.rdclass == rdclass); 140 141 UNUSED(type); 142 UNUSED(rdclass); 143 144 RETERR(uint16_tobuffer(lp->pref, target)); 145 dns_name_toregion(&lp->lp, ®ion); 146 return (isc_buffer_copyregion(target, ®ion)); 147 } 148 149 static inline isc_result_t 150 tostruct_lp(ARGS_TOSTRUCT) { 151 isc_region_t region; 152 dns_rdata_lp_t *lp = target; 153 dns_name_t name; 154 155 REQUIRE(rdata->type == dns_rdatatype_lp); 156 REQUIRE(lp != NULL); 157 REQUIRE(rdata->length != 0); 158 159 lp->common.rdclass = rdata->rdclass; 160 lp->common.rdtype = rdata->type; 161 ISC_LINK_INIT(&lp->common, link); 162 163 dns_name_init(&name, NULL); 164 dns_rdata_toregion(rdata, ®ion); 165 lp->pref = uint16_fromregion(®ion); 166 isc_region_consume(®ion, 2); 167 dns_name_fromregion(&name, ®ion); 168 dns_name_init(&lp->lp, NULL); 169 RETERR(name_duporclone(&name, mctx, &lp->lp)); 170 lp->mctx = mctx; 171 return (ISC_R_SUCCESS); 172 } 173 174 static inline void 175 freestruct_lp(ARGS_FREESTRUCT) { 176 dns_rdata_lp_t *lp = source; 177 178 REQUIRE(lp != NULL); 179 REQUIRE(lp->common.rdtype == dns_rdatatype_lp); 180 181 if (lp->mctx == NULL) { 182 return; 183 } 184 185 dns_name_free(&lp->lp, lp->mctx); 186 lp->mctx = NULL; 187 } 188 189 static inline isc_result_t 190 additionaldata_lp(ARGS_ADDLDATA) { 191 dns_name_t name; 192 dns_offsets_t offsets; 193 isc_region_t region; 194 isc_result_t result; 195 196 REQUIRE(rdata->type == dns_rdatatype_lp); 197 198 dns_name_init(&name, offsets); 199 dns_rdata_toregion(rdata, ®ion); 200 isc_region_consume(®ion, 2); 201 dns_name_fromregion(&name, ®ion); 202 203 result = (add)(arg, &name, dns_rdatatype_l32); 204 if (result != ISC_R_SUCCESS) { 205 return (result); 206 } 207 return ((add)(arg, &name, dns_rdatatype_l64)); 208 } 209 210 static inline isc_result_t 211 digest_lp(ARGS_DIGEST) { 212 isc_region_t region; 213 214 REQUIRE(rdata->type == dns_rdatatype_lp); 215 216 dns_rdata_toregion(rdata, ®ion); 217 return ((digest)(arg, ®ion)); 218 } 219 220 static inline bool 221 checkowner_lp(ARGS_CHECKOWNER) { 222 REQUIRE(type == dns_rdatatype_lp); 223 224 UNUSED(type); 225 UNUSED(rdclass); 226 UNUSED(name); 227 UNUSED(wildcard); 228 229 return (true); 230 } 231 232 static inline bool 233 checknames_lp(ARGS_CHECKNAMES) { 234 REQUIRE(rdata->type == dns_rdatatype_lp); 235 236 UNUSED(bad); 237 UNUSED(owner); 238 239 return (true); 240 } 241 242 static inline int 243 casecompare_lp(ARGS_COMPARE) { 244 dns_name_t name1; 245 dns_name_t name2; 246 isc_region_t region1; 247 isc_region_t region2; 248 int order; 249 250 REQUIRE(rdata1->type == rdata2->type); 251 REQUIRE(rdata1->rdclass == rdata2->rdclass); 252 REQUIRE(rdata1->type == dns_rdatatype_lp); 253 REQUIRE(rdata1->length != 0); 254 REQUIRE(rdata2->length != 0); 255 256 order = memcmp(rdata1->data, rdata2->data, 2); 257 if (order != 0) { 258 return (order < 0 ? -1 : 1); 259 } 260 261 dns_name_init(&name1, NULL); 262 dns_name_init(&name2, NULL); 263 264 dns_rdata_toregion(rdata1, ®ion1); 265 dns_rdata_toregion(rdata2, ®ion2); 266 267 isc_region_consume(®ion1, 2); 268 isc_region_consume(®ion2, 2); 269 270 dns_name_fromregion(&name1, ®ion1); 271 dns_name_fromregion(&name2, ®ion2); 272 273 return (dns_name_rdatacompare(&name1, &name2)); 274 } 275 276 #endif /* RDATA_GENERIC_LP_107_C */ 277