xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/in_1/wks_11.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 /* $Id: wks_11.c,v 1.13 2020/02/26 18:47:59 florian Exp $ */
18 
19 /* Reviewed: Fri Mar 17 15:01:49 PST 2000 by explorer */
20 
21 #ifndef RDATA_IN_1_WKS_11_C
22 #define RDATA_IN_1_WKS_11_C
23 
24 #include <netdb.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 
28 #include <isc/net.h>
29 
30 static inline isc_result_t
31 totext_in_wks(ARGS_TOTEXT) {
32 	isc_region_t sr;
33 	unsigned short proto;
34 	char buf[sizeof("65535")];
35 	unsigned int i, j;
36 
37 	UNUSED(tctx);
38 
39 	REQUIRE(rdata->type == dns_rdatatype_wks);
40 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
41 	REQUIRE(rdata->length >= 5);
42 
43 	dns_rdata_toregion(rdata, &sr);
44 	RETERR(inet_totext(AF_INET, &sr, target));
45 	isc_region_consume(&sr, 4);
46 
47 	proto = uint8_fromregion(&sr);
48 	snprintf(buf, sizeof(buf), "%u", proto);
49 	RETERR(isc_str_tobuffer(" ", target));
50 	RETERR(isc_str_tobuffer(buf, target));
51 	isc_region_consume(&sr, 1);
52 
53 	INSIST(sr.length <= 8*1024);
54 	for (i = 0; i < sr.length; i++) {
55 		if (sr.base[i] != 0)
56 			for (j = 0; j < 8; j++)
57 				if ((sr.base[i] & (0x80 >> j)) != 0) {
58 					snprintf(buf, sizeof(buf),
59 						 "%u", i * 8 + j);
60 					RETERR(isc_str_tobuffer(" ", target));
61 					RETERR(isc_str_tobuffer(buf, target));
62 				}
63 	}
64 
65 	return (ISC_R_SUCCESS);
66 }
67 
68 static inline isc_result_t
69 fromwire_in_wks(ARGS_FROMWIRE) {
70 	isc_region_t sr;
71 	isc_region_t tr;
72 
73 	REQUIRE(type == dns_rdatatype_wks);
74 	REQUIRE(rdclass == dns_rdataclass_in);
75 
76 	UNUSED(type);
77 	UNUSED(dctx);
78 	UNUSED(options);
79 	UNUSED(rdclass);
80 
81 	isc_buffer_activeregion(source, &sr);
82 	isc_buffer_availableregion(target, &tr);
83 
84 	if (sr.length < 5)
85 		return (ISC_R_UNEXPECTEDEND);
86 	if (sr.length > 8 * 1024 + 5)
87 		return (DNS_R_EXTRADATA);
88 	if (tr.length < sr.length)
89 		return (ISC_R_NOSPACE);
90 
91 	memmove(tr.base, sr.base, sr.length);
92 	isc_buffer_add(target, sr.length);
93 	isc_buffer_forward(source, sr.length);
94 
95 	return (ISC_R_SUCCESS);
96 }
97 
98 static inline isc_result_t
99 towire_in_wks(ARGS_TOWIRE) {
100 	isc_region_t sr;
101 
102 	UNUSED(cctx);
103 
104 	REQUIRE(rdata->type == dns_rdatatype_wks);
105 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
106 	REQUIRE(rdata->length != 0);
107 
108 	dns_rdata_toregion(rdata, &sr);
109 	return (isc_mem_tobuffer(target, sr.base, sr.length));
110 }
111 
112 #endif	/* RDATA_IN_1_WKS_11_C */
113