xref: /openbsd/usr.bin/dig/lib/dns/rdata/generic/mx_15.c (revision 76d0caae)
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.14 2020/09/14 08:40:43 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 static inline isc_result_t
27 totext_mx(ARGS_TOTEXT) {
28 	isc_region_t region;
29 	dns_name_t name;
30 	dns_name_t prefix;
31 	int sub;
32 	char buf[sizeof("64000")];
33 	unsigned short num;
34 
35 	REQUIRE(rdata->type == dns_rdatatype_mx);
36 	REQUIRE(rdata->length != 0);
37 
38 	dns_name_init(&name, NULL);
39 	dns_name_init(&prefix, NULL);
40 
41 	dns_rdata_toregion(rdata, &region);
42 	num = uint16_fromregion(&region);
43 	isc_region_consume(&region, 2);
44 	snprintf(buf, sizeof(buf), "%u", num);
45 	RETERR(isc_str_tobuffer(buf, target));
46 
47 	RETERR(isc_str_tobuffer(" ", target));
48 
49 	dns_name_fromregion(&name, &region);
50 	sub = name_prefix(&name, tctx->origin, &prefix);
51 	return (dns_name_totext(&prefix, sub, target));
52 }
53 
54 static inline isc_result_t
55 fromwire_mx(ARGS_FROMWIRE) {
56 	dns_name_t name;
57 	isc_region_t sregion;
58 
59 	REQUIRE(type == dns_rdatatype_mx);
60 
61 	UNUSED(type);
62 	UNUSED(rdclass);
63 
64 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
65 
66 	dns_name_init(&name, NULL);
67 
68 	isc_buffer_activeregion(source, &sregion);
69 	if (sregion.length < 2)
70 		return (ISC_R_UNEXPECTEDEND);
71 	RETERR(isc_mem_tobuffer(target, sregion.base, 2));
72 	isc_buffer_forward(source, 2);
73 	return (dns_name_fromwire(&name, source, dctx, options, target));
74 }
75 
76 static inline isc_result_t
77 towire_mx(ARGS_TOWIRE) {
78 	dns_name_t name;
79 	dns_offsets_t offsets;
80 	isc_region_t region;
81 
82 	REQUIRE(rdata->type == dns_rdatatype_mx);
83 	REQUIRE(rdata->length != 0);
84 
85 	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
86 
87 	dns_rdata_toregion(rdata, &region);
88 	RETERR(isc_mem_tobuffer(target, region.base, 2));
89 	isc_region_consume(&region, 2);
90 
91 	dns_name_init(&name, offsets);
92 	dns_name_fromregion(&name, &region);
93 
94 	return (dns_name_towire(&name, cctx, target));
95 }
96 
97 #endif	/* RDATA_GENERIC_MX_15_C */
98