1 /* $NetBSD: l32_105.c,v 1.9 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_L32_105_C 17 #define RDATA_GENERIC_L32_105_C 18 19 #include <string.h> 20 21 #include <isc/net.h> 22 23 #define RRTYPE_L32_ATTRIBUTES (0) 24 25 static isc_result_t 26 fromtext_l32(ARGS_FROMTEXT) { 27 isc_token_t token; 28 struct in_addr addr; 29 isc_region_t region; 30 31 REQUIRE(type == dns_rdatatype_l32); 32 33 UNUSED(type); 34 UNUSED(rdclass); 35 UNUSED(origin); 36 UNUSED(options); 37 UNUSED(callbacks); 38 39 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, 40 false)); 41 if (token.value.as_ulong > 0xffffU) { 42 RETTOK(ISC_R_RANGE); 43 } 44 RETERR(uint16_tobuffer(token.value.as_ulong, target)); 45 46 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string, 47 false)); 48 49 if (inet_pton(AF_INET, DNS_AS_STR(token), &addr) != 1) { 50 RETTOK(DNS_R_BADDOTTEDQUAD); 51 } 52 isc_buffer_availableregion(target, ®ion); 53 if (region.length < 4) { 54 return (ISC_R_NOSPACE); 55 } 56 memmove(region.base, &addr, 4); 57 isc_buffer_add(target, 4); 58 return (ISC_R_SUCCESS); 59 } 60 61 static isc_result_t 62 totext_l32(ARGS_TOTEXT) { 63 isc_region_t region; 64 char buf[sizeof("65000")]; 65 unsigned short num; 66 67 REQUIRE(rdata->type == dns_rdatatype_l32); 68 REQUIRE(rdata->length == 6); 69 70 UNUSED(tctx); 71 72 dns_rdata_toregion(rdata, ®ion); 73 num = uint16_fromregion(®ion); 74 isc_region_consume(®ion, 2); 75 snprintf(buf, sizeof(buf), "%u", num); 76 RETERR(str_totext(buf, target)); 77 78 RETERR(str_totext(" ", target)); 79 80 return (inet_totext(AF_INET, tctx->flags, ®ion, target)); 81 } 82 83 static isc_result_t 84 fromwire_l32(ARGS_FROMWIRE) { 85 isc_region_t sregion; 86 87 REQUIRE(type == dns_rdatatype_l32); 88 89 UNUSED(type); 90 UNUSED(options); 91 UNUSED(rdclass); 92 UNUSED(dctx); 93 94 isc_buffer_activeregion(source, &sregion); 95 if (sregion.length != 6) { 96 return (DNS_R_FORMERR); 97 } 98 isc_buffer_forward(source, sregion.length); 99 return (mem_tobuffer(target, sregion.base, sregion.length)); 100 } 101 102 static isc_result_t 103 towire_l32(ARGS_TOWIRE) { 104 REQUIRE(rdata->type == dns_rdatatype_l32); 105 REQUIRE(rdata->length == 6); 106 107 UNUSED(cctx); 108 109 return (mem_tobuffer(target, rdata->data, rdata->length)); 110 } 111 112 static int 113 compare_l32(ARGS_COMPARE) { 114 isc_region_t region1; 115 isc_region_t region2; 116 117 REQUIRE(rdata1->type == rdata2->type); 118 REQUIRE(rdata1->rdclass == rdata2->rdclass); 119 REQUIRE(rdata1->type == dns_rdatatype_l32); 120 REQUIRE(rdata1->length == 6); 121 REQUIRE(rdata2->length == 6); 122 123 dns_rdata_toregion(rdata1, ®ion1); 124 dns_rdata_toregion(rdata2, ®ion2); 125 return (isc_region_compare(®ion1, ®ion2)); 126 } 127 128 static isc_result_t 129 fromstruct_l32(ARGS_FROMSTRUCT) { 130 dns_rdata_l32_t *l32 = source; 131 uint32_t n; 132 133 REQUIRE(type == dns_rdatatype_l32); 134 REQUIRE(l32 != NULL); 135 REQUIRE(l32->common.rdtype == type); 136 REQUIRE(l32->common.rdclass == rdclass); 137 138 UNUSED(type); 139 UNUSED(rdclass); 140 141 RETERR(uint16_tobuffer(l32->pref, target)); 142 n = ntohl(l32->l32.s_addr); 143 return (uint32_tobuffer(n, target)); 144 } 145 146 static isc_result_t 147 tostruct_l32(ARGS_TOSTRUCT) { 148 isc_region_t region; 149 dns_rdata_l32_t *l32 = target; 150 uint32_t n; 151 152 REQUIRE(rdata->type == dns_rdatatype_l32); 153 REQUIRE(l32 != NULL); 154 REQUIRE(rdata->length == 6); 155 156 UNUSED(mctx); 157 158 l32->common.rdclass = rdata->rdclass; 159 l32->common.rdtype = rdata->type; 160 ISC_LINK_INIT(&l32->common, link); 161 162 dns_rdata_toregion(rdata, ®ion); 163 l32->pref = uint16_fromregion(®ion); 164 n = uint32_fromregion(®ion); 165 l32->l32.s_addr = htonl(n); 166 return (ISC_R_SUCCESS); 167 } 168 169 static void 170 freestruct_l32(ARGS_FREESTRUCT) { 171 dns_rdata_l32_t *l32 = source; 172 173 REQUIRE(l32 != NULL); 174 REQUIRE(l32->common.rdtype == dns_rdatatype_l32); 175 176 return; 177 } 178 179 static isc_result_t 180 additionaldata_l32(ARGS_ADDLDATA) { 181 REQUIRE(rdata->type == dns_rdatatype_l32); 182 REQUIRE(rdata->length == 6); 183 184 UNUSED(rdata); 185 UNUSED(owner); 186 UNUSED(add); 187 UNUSED(arg); 188 189 return (ISC_R_SUCCESS); 190 } 191 192 static isc_result_t 193 digest_l32(ARGS_DIGEST) { 194 isc_region_t r; 195 196 REQUIRE(rdata->type == dns_rdatatype_l32); 197 REQUIRE(rdata->length == 6); 198 199 dns_rdata_toregion(rdata, &r); 200 201 return ((digest)(arg, &r)); 202 } 203 204 static bool 205 checkowner_l32(ARGS_CHECKOWNER) { 206 REQUIRE(type == dns_rdatatype_l32); 207 208 UNUSED(name); 209 UNUSED(type); 210 UNUSED(rdclass); 211 UNUSED(wildcard); 212 213 return (true); 214 } 215 216 static bool 217 checknames_l32(ARGS_CHECKNAMES) { 218 REQUIRE(rdata->type == dns_rdatatype_l32); 219 REQUIRE(rdata->length == 6); 220 221 UNUSED(rdata); 222 UNUSED(owner); 223 UNUSED(bad); 224 225 return (true); 226 } 227 228 static int 229 casecompare_l32(ARGS_COMPARE) { 230 return (compare_l32(rdata1, rdata2)); 231 } 232 233 #endif /* RDATA_GENERIC_L32_105_C */ 234