xref: /openbsd/usr.bin/dig/lib/dns/include/dns/compress.h (revision db6316a3)
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: compress.h,v 1.6 2024/05/17 23:56:19 jsg Exp $ */
18 
19 #ifndef DNS_COMPRESS_H
20 #define DNS_COMPRESS_H 1
21 
22 #include <isc/region.h>
23 
24 #include <dns/types.h>
25 
26 #define DNS_COMPRESS_NONE		0x00	/*%< no compression */
27 #define DNS_COMPRESS_GLOBAL14		0x01	/*%< "normal" compression. */
28 #define DNS_COMPRESS_ALL		0x01	/*%< all compression. */
29 #define DNS_COMPRESS_CASESENSITIVE	0x02	/*%< case sensitive compression. */
30 
31 /*! \file dns/compress.h
32  *	Direct manipulation of the structures is strongly discouraged.
33  */
34 
35 #define DNS_COMPRESS_TABLESIZE 64
36 #define DNS_COMPRESS_INITIALNODES 16
37 
38 typedef struct dns_compressnode dns_compressnode_t;
39 
40 struct dns_compressnode {
41 	isc_region_t		r;
42 	uint16_t		offset;
43 	uint16_t		count;
44 	uint8_t		labels;
45 	dns_compressnode_t	*next;
46 };
47 
48 struct dns_compress {
49 	unsigned int		allowed;	/*%< Allowed methods. */
50 	int			edns;		/*%< Edns version or -1. */
51 	/*% Global compression table. */
52 	dns_compressnode_t	*table[DNS_COMPRESS_TABLESIZE];
53 	/*% Preallocated nodes for the table. */
54 	dns_compressnode_t	initialnodes[DNS_COMPRESS_INITIALNODES];
55 	uint16_t		count;		/*%< Number of nodes. */
56 };
57 
58 typedef enum {
59 	DNS_DECOMPRESS_ANY,			/*%< Any compression */
60 	DNS_DECOMPRESS_STRICT,			/*%< Allowed compression */
61 	DNS_DECOMPRESS_NONE			/*%< No compression */
62 } dns_decompresstype_t;
63 
64 struct dns_decompress {
65 	unsigned int		allowed;	/*%< Allowed methods. */
66 	int			edns;		/*%< Edns version or -1. */
67 	dns_decompresstype_t	type;		/*%< Strict checking */
68 };
69 
70 isc_result_t
71 dns_compress_init(dns_compress_t *cctx, int edns);
72 /*%<
73  *	Initialise the compression context structure pointed to by 'cctx'.
74  *
75  *	Requires:
76  *	\li	'cctx' is a valid dns_compress_t structure.
77  *	\li	'mctx' is an initialized memory context.
78  *	Ensures:
79  *	\li	cctx->global is initialized.
80  *
81  *	Returns:
82  *	\li	#ISC_R_SUCCESS
83  */
84 
85 void
86 dns_compress_invalidate(dns_compress_t *cctx);
87 
88 /*%<
89  *	Invalidate the compression structure pointed to by cctx.
90  *
91  *	Requires:
92  *\li		'cctx' to be initialized.
93  */
94 
95 void
96 dns_compress_setmethods(dns_compress_t *cctx, unsigned int allowed);
97 
98 /*%<
99  *	Sets allowed compression methods.
100  *
101  *	Requires:
102  *\li		'cctx' to be initialized.
103  */
104 
105 unsigned int
106 dns_compress_getmethods(dns_compress_t *cctx);
107 
108 /*%<
109  *	Gets allowed compression methods.
110  *
111  *	Requires:
112  *\li		'cctx' to be initialized.
113  *
114  *	Returns:
115  *\li		allowed compression bitmap.
116  */
117 
118 int
119 dns_compress_findglobal(dns_compress_t *cctx, const dns_name_t *name,
120 			dns_name_t *prefix, uint16_t *offset);
121 /*%<
122  *	Finds longest possible match of 'name' in the global compression table.
123  *
124  *	Requires:
125  *\li		'cctx' to be initialized.
126  *\li		'name' to be a absolute name.
127  *\li		'prefix' to be initialized.
128  *\li		'offset' to point to an uint16_t.
129  *
130  *	Ensures:
131  *\li		'prefix' and 'offset' are valid if 1 is 	returned.
132  *
133  *	Returns:
134  *\li		#1 / #0
135  */
136 
137 void
138 dns_compress_add(dns_compress_t *cctx, const dns_name_t *name,
139 		 const dns_name_t *prefix, uint16_t offset);
140 /*%<
141  *	Add compression pointers for 'name' to the compression table,
142  *	not replacing existing pointers.
143  *
144  *	Requires:
145  *\li		'cctx' initialized
146  *
147  *\li		'name' must be initialized and absolute, and must remain
148  *		valid until the message compression is complete.
149  *
150  *\li		'prefix' must be a prefix returned by
151  *		dns_compress_findglobal(), or the same as 'name'.
152  */
153 
154 void
155 dns_compress_rollback(dns_compress_t *cctx, uint16_t offset);
156 
157 /*%<
158  *	Remove any compression pointers from global table >= offset.
159  *
160  *	Requires:
161  *\li		'cctx' is initialized.
162  */
163 
164 void
165 dns_decompress_init(dns_decompress_t *dctx, int edns,
166 		    dns_decompresstype_t type);
167 
168 /*%<
169  *	Initializes 'dctx'.
170  *	Records 'edns' and 'type' into the structure.
171  *
172  *	Requires:
173  *\li		'dctx' to be a valid pointer.
174  */
175 
176 void
177 dns_decompress_setmethods(dns_decompress_t *dctx, unsigned int allowed);
178 
179 /*%<
180  *	Sets 'dctx->allowed' to 'allowed'.
181  *
182  *	Requires:
183  *\li		'dctx' to be initialized
184  */
185 
186 #endif /* DNS_COMPRESS_H */
187