xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/generic/lp_107.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
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 #ifndef RDATA_GENERIC_LP_107_C
18 #define RDATA_GENERIC_LP_107_C
19 
20 #include <string.h>
21 
22 #include <isc/net.h>
23 
24 static inline isc_result_t
25 totext_lp(ARGS_TOTEXT) {
26 	isc_region_t region;
27 	dns_name_t name;
28 	dns_name_t prefix;
29 	isc_boolean_t sub;
30 	char buf[sizeof("64000")];
31 	unsigned short num;
32 
33 	REQUIRE(rdata->type == dns_rdatatype_lp);
34 	REQUIRE(rdata->length != 0);
35 
36 	dns_name_init(&name, NULL);
37 	dns_name_init(&prefix, NULL);
38 
39 	dns_rdata_toregion(rdata, &region);
40 	num = uint16_fromregion(&region);
41 	isc_region_consume(&region, 2);
42 	snprintf(buf, sizeof(buf), "%u", num);
43 	RETERR(isc_str_tobuffer(buf, target));
44 
45 	RETERR(isc_str_tobuffer(" ", target));
46 
47 	dns_name_fromregion(&name, &region);
48 	sub = name_prefix(&name, tctx->origin, &prefix);
49 	return (dns_name_totext(&prefix, sub, target));
50 }
51 
52 static inline isc_result_t
53 fromwire_lp(ARGS_FROMWIRE) {
54 	dns_name_t name;
55 	isc_region_t sregion;
56 
57 	REQUIRE(type == dns_rdatatype_lp);
58 
59 	UNUSED(type);
60 	UNUSED(rdclass);
61 
62 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
63 
64 	dns_name_init(&name, NULL);
65 
66 	isc_buffer_activeregion(source, &sregion);
67 	if (sregion.length < 2)
68 		return (ISC_R_UNEXPECTEDEND);
69 	RETERR(isc_mem_tobuffer(target, sregion.base, 2));
70 	isc_buffer_forward(source, 2);
71 	return (dns_name_fromwire(&name, source, dctx, options, target));
72 }
73 
74 static inline isc_result_t
75 towire_lp(ARGS_TOWIRE) {
76 
77 	REQUIRE(rdata->type == dns_rdatatype_lp);
78 	REQUIRE(rdata->length != 0);
79 
80 	UNUSED(cctx);
81 
82 	return (isc_mem_tobuffer(target, rdata->data, rdata->length));
83 }
84 
85 #endif	/* RDATA_GENERIC_LP_107_C */
86