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