xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/in_1/px_26.c (revision 1fb015a8af3a7e9b85db2510147a155826ef04d9)
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: px_26.c,v 1.13 2020/09/14 08:40:44 florian Exp $ */
18 
19 /* Reviewed: Mon Mar 20 10:44:27 PST 2000 */
20 
21 /* RFC2163 */
22 
23 #ifndef RDATA_IN_1_PX_26_C
24 #define RDATA_IN_1_PX_26_C
25 
26 static inline isc_result_t
totext_in_px(ARGS_TOTEXT)27 totext_in_px(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_px);
36 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
37 	REQUIRE(rdata->length != 0);
38 
39 	dns_name_init(&name, NULL);
40 	dns_name_init(&prefix, NULL);
41 
42 	/*
43 	 * Preference.
44 	 */
45 	dns_rdata_toregion(rdata, &region);
46 	num = uint16_fromregion(&region);
47 	isc_region_consume(&region, 2);
48 	snprintf(buf, sizeof(buf), "%u", num);
49 	RETERR(isc_str_tobuffer(buf, target));
50 	RETERR(isc_str_tobuffer(" ", target));
51 
52 	/*
53 	 * MAP822.
54 	 */
55 	dns_name_fromregion(&name, &region);
56 	sub = name_prefix(&name, tctx->origin, &prefix);
57 	isc_region_consume(&region, name_length(&name));
58 	RETERR(dns_name_totext(&prefix, sub, target));
59 	RETERR(isc_str_tobuffer(" ", target));
60 
61 	/*
62 	 * MAPX400.
63 	 */
64 	dns_name_fromregion(&name, &region);
65 	sub = name_prefix(&name, tctx->origin, &prefix);
66 	return(dns_name_totext(&prefix, sub, target));
67 }
68 
69 static inline isc_result_t
fromwire_in_px(ARGS_FROMWIRE)70 fromwire_in_px(ARGS_FROMWIRE) {
71 	dns_name_t name;
72 	isc_region_t sregion;
73 
74 	REQUIRE(type == dns_rdatatype_px);
75 	REQUIRE(rdclass == dns_rdataclass_in);
76 
77 	UNUSED(type);
78 	UNUSED(rdclass);
79 
80 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
81 
82 	dns_name_init(&name, NULL);
83 
84 	/*
85 	 * Preference.
86 	 */
87 	isc_buffer_activeregion(source, &sregion);
88 	if (sregion.length < 2)
89 		return (ISC_R_UNEXPECTEDEND);
90 	RETERR(isc_mem_tobuffer(target, sregion.base, 2));
91 	isc_buffer_forward(source, 2);
92 
93 	/*
94 	 * MAP822.
95 	 */
96 	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
97 
98 	/*
99 	 * MAPX400.
100 	 */
101 	return (dns_name_fromwire(&name, source, dctx, options, target));
102 }
103 
104 static inline isc_result_t
towire_in_px(ARGS_TOWIRE)105 towire_in_px(ARGS_TOWIRE) {
106 	dns_name_t name;
107 	dns_offsets_t offsets;
108 	isc_region_t region;
109 
110 	REQUIRE(rdata->type == dns_rdatatype_px);
111 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
112 	REQUIRE(rdata->length != 0);
113 
114 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
115 	/*
116 	 * Preference.
117 	 */
118 	dns_rdata_toregion(rdata, &region);
119 	RETERR(isc_mem_tobuffer(target, region.base, 2));
120 	isc_region_consume(&region, 2);
121 
122 	/*
123 	 * MAP822.
124 	 */
125 	dns_name_init(&name, offsets);
126 	dns_name_fromregion(&name, &region);
127 	RETERR(dns_name_towire(&name, cctx, target));
128 	isc_region_consume(&region, name_length(&name));
129 
130 	/*
131 	 * MAPX400.
132 	 */
133 	dns_name_init(&name, offsets);
134 	dns_name_fromregion(&name, &region);
135 	return (dns_name_towire(&name, cctx, target));
136 }
137 
138 #endif	/* RDATA_IN_1_PX_26_C */
139