1 /* 2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14 * PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 /* $Id: mx_15.c,v 1.12 2020/02/26 18:47:59 florian Exp $ */ 18 19 /* reviewed: Wed Mar 15 18:05:46 PST 2000 by brister */ 20 21 #ifndef RDATA_GENERIC_MX_15_C 22 #define RDATA_GENERIC_MX_15_C 23 24 #include <string.h> 25 26 #include <isc/net.h> 27 28 static inline isc_result_t 29 totext_mx(ARGS_TOTEXT) { 30 isc_region_t region; 31 dns_name_t name; 32 dns_name_t prefix; 33 isc_boolean_t sub; 34 char buf[sizeof("64000")]; 35 unsigned short num; 36 37 REQUIRE(rdata->type == dns_rdatatype_mx); 38 REQUIRE(rdata->length != 0); 39 40 dns_name_init(&name, NULL); 41 dns_name_init(&prefix, NULL); 42 43 dns_rdata_toregion(rdata, ®ion); 44 num = uint16_fromregion(®ion); 45 isc_region_consume(®ion, 2); 46 snprintf(buf, sizeof(buf), "%u", num); 47 RETERR(isc_str_tobuffer(buf, target)); 48 49 RETERR(isc_str_tobuffer(" ", target)); 50 51 dns_name_fromregion(&name, ®ion); 52 sub = name_prefix(&name, tctx->origin, &prefix); 53 return (dns_name_totext(&prefix, sub, target)); 54 } 55 56 static inline isc_result_t 57 fromwire_mx(ARGS_FROMWIRE) { 58 dns_name_t name; 59 isc_region_t sregion; 60 61 REQUIRE(type == dns_rdatatype_mx); 62 63 UNUSED(type); 64 UNUSED(rdclass); 65 66 dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14); 67 68 dns_name_init(&name, NULL); 69 70 isc_buffer_activeregion(source, &sregion); 71 if (sregion.length < 2) 72 return (ISC_R_UNEXPECTEDEND); 73 RETERR(isc_mem_tobuffer(target, sregion.base, 2)); 74 isc_buffer_forward(source, 2); 75 return (dns_name_fromwire(&name, source, dctx, options, target)); 76 } 77 78 static inline isc_result_t 79 towire_mx(ARGS_TOWIRE) { 80 dns_name_t name; 81 dns_offsets_t offsets; 82 isc_region_t region; 83 84 REQUIRE(rdata->type == dns_rdatatype_mx); 85 REQUIRE(rdata->length != 0); 86 87 dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14); 88 89 dns_rdata_toregion(rdata, ®ion); 90 RETERR(isc_mem_tobuffer(target, region.base, 2)); 91 isc_region_consume(®ion, 2); 92 93 dns_name_init(&name, offsets); 94 dns_name_fromregion(&name, ®ion); 95 96 return (dns_name_towire(&name, cctx, target)); 97 } 98 99 #endif /* RDATA_GENERIC_MX_15_C */ 100