xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/in_1/dhcid_49.c (revision 873f12b9e6aaf39ba104db6af3d0dc687cd95f7e)
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: dhcid_49.c,v 1.12 2020/02/26 18:47:59 florian Exp $ */
18 
19 /* RFC 4701 */
20 
21 #ifndef RDATA_IN_1_DHCID_49_C
22 #define RDATA_IN_1_DHCID_49_C 1
23 
24 static inline isc_result_t
totext_in_dhcid(ARGS_TOTEXT)25 totext_in_dhcid(ARGS_TOTEXT) {
26 	isc_region_t sr, sr2;
27 	char buf[sizeof(" ; 64000 255 64000")];
28 	size_t n;
29 
30 	REQUIRE(rdata->type == dns_rdatatype_dhcid);
31 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
32 	REQUIRE(rdata->length != 0);
33 
34 	dns_rdata_toregion(rdata, &sr);
35 	sr2 = sr;
36 
37 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
38 		RETERR(isc_str_tobuffer("( " /*)*/, target));
39 	if (tctx->width == 0)   /* No splitting */
40 		RETERR(isc_base64_totext(&sr, 60, "", target));
41 	else
42 		RETERR(isc_base64_totext(&sr, tctx->width - 2,
43 					 tctx->linebreak, target));
44 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
45 		RETERR(isc_str_tobuffer(/* ( */ " )", target));
46 		if (rdata->length > 2) {
47 			n = snprintf(buf, sizeof(buf), " ; %u %u %u",
48 				     sr2.base[0] * 256U + sr2.base[1],
49 				     sr2.base[2], rdata->length - 3U);
50 			INSIST(n < sizeof(buf));
51 			RETERR(isc_str_tobuffer(buf, target));
52 		}
53 	}
54 	return (ISC_R_SUCCESS);
55 }
56 
57 static inline isc_result_t
fromwire_in_dhcid(ARGS_FROMWIRE)58 fromwire_in_dhcid(ARGS_FROMWIRE) {
59 	isc_region_t sr;
60 
61 	REQUIRE(type == dns_rdatatype_dhcid);
62 	REQUIRE(rdclass == dns_rdataclass_in);
63 
64 	UNUSED(type);
65 	UNUSED(rdclass);
66 	UNUSED(dctx);
67 	UNUSED(options);
68 
69 	isc_buffer_activeregion(source, &sr);
70 	if (sr.length == 0)
71 		return (ISC_R_UNEXPECTEDEND);
72 
73 	isc_buffer_forward(source, sr.length);
74 	return (isc_mem_tobuffer(target, sr.base, sr.length));
75 }
76 
77 static inline isc_result_t
towire_in_dhcid(ARGS_TOWIRE)78 towire_in_dhcid(ARGS_TOWIRE) {
79 	isc_region_t sr;
80 
81 	REQUIRE(rdata->type == dns_rdatatype_dhcid);
82 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
83 	REQUIRE(rdata->length != 0);
84 
85 	UNUSED(cctx);
86 
87 	dns_rdata_toregion(rdata, &sr);
88 	return (isc_mem_tobuffer(target, sr.base, sr.length));
89 }
90 
91 #endif	/* RDATA_IN_1_DHCID_49_C */
92