xref: /freebsd/contrib/unbound/util/data/dname.h (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*
2  * util/data/dname.h - domain name routines
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to deal with domain names (dnames).
40  *
41  * Some of the functions deal with domain names as a wireformat buffer,
42  * with a length.
43  */
44 
45 #ifndef UTIL_DATA_DNAME_H
46 #define UTIL_DATA_DNAME_H
47 #include "util/storage/lruhash.h"
48 struct sldns_buffer;
49 
50 /** max number of compression ptrs to follow */
51 #define MAX_COMPRESS_PTRS 256
52 
53 /**
54  * Determine length of dname in buffer, no compression ptrs allowed,
55  * @param query: the ldns buffer, current position at start of dname.
56  *	at end, position is at end of the dname.
57  * @return: 0 on parse failure, or length including ending 0 of dname.
58  */
59 size_t query_dname_len(struct sldns_buffer* query);
60 
61 /**
62  * Determine if dname in memory is correct. no compression ptrs allowed.
63  * @param dname: where dname starts in memory.
64  * @param len: dname is not allowed to exceed this length (i.e. of allocation).
65  * @return length of dname if dname is ok, 0 on a parse error.
66  */
67 size_t dname_valid(uint8_t* dname, size_t len);
68 
69 /** lowercase query dname */
70 void query_dname_tolower(uint8_t* dname);
71 
72 /**
73  * lowercase pkt dname (follows compression pointers)
74  * @param pkt: the packet, used to follow compression pointers. Position
75  *	is unchanged.
76  * @param dname: start of dname in packet.
77  */
78 void pkt_dname_tolower(struct sldns_buffer* pkt, uint8_t* dname);
79 
80 /**
81  * Compare query dnames (uncompressed storage). The Dnames passed do not
82  * have to be lowercased, comparison routine does this.
83  *
84  * This routine is special, in that the comparison that it does corresponds
85  * with the canonical comparison needed when comparing dnames inside rdata
86  * for RR types that need canonicalization. That means that the first byte
87  * that is smaller (possibly after lowercasing) makes an RR smaller, or the
88  * shortest name makes an RR smaller.
89  *
90  * This routine does not compute the canonical order needed for NSEC
91  * processing.
92  *
93  * Dnames have to be valid format.
94  * @param d1: dname to compare
95  * @param d2: dname to compare
96  * @return: -1, 0, or +1 depending on comparison results.
97  * 	Sort order is first difference found. not the canonical ordering.
98  */
99 int query_dname_compare(uint8_t* d1, uint8_t* d2);
100 
101 /**
102  * Determine correct, compressed, dname present in packet.
103  * Checks for parse errors.
104  * @param pkt: packet to read from (from current start position).
105  * @return: 0 on parse error.
106  *	At exit the position is right after the (compressed) dname.
107  *	Compression pointers are followed and checked for loops.
108  *	The uncompressed wireformat length is returned.
109  */
110 size_t pkt_dname_len(struct sldns_buffer* pkt);
111 
112 /**
113  * Compare dnames in packet (compressed). Dnames must be valid.
114  * routine performs lowercasing, so the packet casing is preserved.
115  * @param pkt: packet, used to resolve compression pointers.
116  * @param d1: dname to compare
117  * @param d2: dname to compare
118  * @return: -1, 0, or +1 depending on comparison results.
119  * 	Sort order is first difference found. not the canonical ordering.
120  */
121 int dname_pkt_compare(struct sldns_buffer* pkt, uint8_t* d1, uint8_t* d2);
122 
123 /**
124  * Hash dname, label by label, lowercasing, into hashvalue.
125  * Dname in query format (not compressed).
126  * @param dname: dname to hash.
127  * @param h: initial hash value.
128  * @return: result hash value.
129  */
130 hashvalue_t dname_query_hash(uint8_t* dname, hashvalue_t h);
131 
132 /**
133  * Hash dname, label by label, lowercasing, into hashvalue.
134  * Dname in pkt format (compressed).
135  * @param pkt: packet, for resolving compression pointers.
136  * @param dname: dname to hash, pointer to the pkt buffer.
137  * 	Must be valid format. No loops, etc.
138  * @param h: initial hash value.
139  * @return: result hash value.
140  * 	Result is the same as dname_query_hash, even if compression is used.
141  */
142 hashvalue_t dname_pkt_hash(struct sldns_buffer* pkt, uint8_t* dname, hashvalue_t h);
143 
144 /**
145  * Copy over a valid dname and decompress it.
146  * @param pkt: packet to resolve compression pointers.
147  * @param to: buffer of size from pkt_len function to hold result.
148  * @param dname: pointer into packet where dname starts.
149  */
150 void dname_pkt_copy(struct sldns_buffer* pkt, uint8_t* to, uint8_t* dname);
151 
152 /**
153  * Copy over a valid dname to a packet.
154  * @param pkt: packet to copy to.
155  * @param dname: dname to copy.
156  * @return: 0 if not enough space in buffer.
157  */
158 int dname_buffer_write(struct sldns_buffer* pkt, uint8_t* dname);
159 
160 /**
161  * Count the number of labels in an uncompressed dname in memory.
162  * @param dname: pointer to uncompressed dname.
163  * @return: count of labels, including root label, "com." has 2 labels.
164  */
165 int dname_count_labels(uint8_t* dname);
166 
167 /**
168  * Count labels and dname length both, for uncompressed dname in memory.
169  * @param dname: pointer to uncompressed dname.
170  * @param size: length of dname, including root label.
171  * @return: count of labels, including root label, "com." has 2 labels.
172  */
173 int dname_count_size_labels(uint8_t* dname, size_t* size);
174 
175 /**
176  * Compare dnames, sorted not canonical, but by label.
177  * Such that zone contents follows zone apex.
178  * @param d1: first dname. pointer to uncompressed wireformat.
179  * @param labs1: number of labels in first dname.
180  * @param d2: second dname. pointer to uncompressed wireformat.
181  * @param labs2: number of labels in second dname.
182  * @param mlabs: number of labels that matched exactly (the shared topdomain).
183  * @return: 0 for equal, -1 smaller, or +1 d1 larger than d2.
184  */
185 int dname_lab_cmp(uint8_t* d1, int labs1, uint8_t* d2, int labs2, int* mlabs);
186 
187 /**
188  * See if domain name d1 is a strict subdomain of d2.
189  * That is a subdomain, but not equal.
190  * @param d1: domain name, uncompressed wireformat
191  * @param labs1: number of labels in d1, including root label.
192  * @param d2: domain name, uncompressed wireformat
193  * @param labs2: number of labels in d2, including root label.
194  * @return true if d1 is a subdomain of d2, but not equal to d2.
195  */
196 int dname_strict_subdomain(uint8_t* d1, int labs1, uint8_t* d2, int labs2);
197 
198 /**
199  * Like dname_strict_subdomain but counts labels
200  * @param d1: domain name, uncompressed wireformat
201  * @param d2: domain name, uncompressed wireformat
202  * @return true if d1 is a subdomain of d2, but not equal to d2.
203  */
204 int dname_strict_subdomain_c(uint8_t* d1, uint8_t* d2);
205 
206 /**
207  * Counts labels. Tests is d1 is a subdomain of d2.
208  * @param d1: domain name, uncompressed wireformat
209  * @param d2: domain name, uncompressed wireformat
210  * @return true if d1 is a subdomain of d2.
211  */
212 int dname_subdomain_c(uint8_t* d1, uint8_t* d2);
213 
214 /**
215  * Debug helper. Print wireformat dname to output.
216  * @param out: like stdout or a file.
217  * @param pkt: if not NULL, the packet for resolving compression ptrs.
218  * @param dname: pointer to (start of) dname.
219  */
220 void dname_print(FILE* out, struct sldns_buffer* pkt, uint8_t* dname);
221 
222 /**
223  * Debug helper. Print dname to given string buffer (string buffer must
224  * be at least 255 chars + 1 for the 0, in printable form.
225  * This may lose information (? for nonprintable characters, or & if
226  * the name is too long, # for a bad label length).
227  * @param dname: uncompressed wireformat.
228  * @param str: buffer of 255+1 length.
229  */
230 void dname_str(uint8_t* dname, char* str);
231 
232 /**
233  * Returns true if the uncompressed wireformat dname is the root "."
234  * @param dname: the dname to check
235  * @return true if ".", false if not.
236  */
237 int dname_is_root(uint8_t* dname);
238 
239 /**
240  * Snip off first label from a dname, returning the parent zone.
241  * @param dname: from what to strip off. uncompressed wireformat.
242  * @param len: length, adjusted to become less.
243  * @return stripped off, or "." if input was ".".
244  */
245 void dname_remove_label(uint8_t** dname, size_t* len);
246 
247 /**
248  * Snip off first N labels from a dname, returning the parent zone.
249  * @param dname: from what to strip off. uncompressed wireformat.
250  * @param len: length, adjusted to become less.
251  * @param n: number of labels to strip off (from the left).
252  * 	if 0, nothing happens.
253  * @return stripped off, or "." if input was ".".
254  */
255 void dname_remove_labels(uint8_t** dname, size_t* len, int n);
256 
257 /**
258  * Count labels for the RRSIG signature label field.
259  * Like a normal labelcount, but "*" wildcard and "." root are not counted.
260  * @param dname: valid uncompressed wireformat.
261  * @return number of labels like in RRSIG; '*' and '.' are not counted.
262  */
263 int dname_signame_label_count(uint8_t* dname);
264 
265 /**
266  * Return true if the label is a wildcard, *.example.com.
267  * @param dname: valid uncompressed wireformat.
268  * @return true if wildcard, or false.
269  */
270 int dname_is_wild(uint8_t* dname);
271 
272 /**
273  * Compare dnames, Canonical in rfc4034 sense, but by label.
274  * Such that zone contents follows zone apex.
275  *
276  * @param d1: first dname. pointer to uncompressed wireformat.
277  * @param labs1: number of labels in first dname.
278  * @param d2: second dname. pointer to uncompressed wireformat.
279  * @param labs2: number of labels in second dname.
280  * @param mlabs: number of labels that matched exactly (the shared topdomain).
281  * @return: 0 for equal, -1 smaller, or +1 d1 larger than d2.
282  */
283 int dname_canon_lab_cmp(uint8_t* d1, int labs1, uint8_t* d2, int labs2,
284 	int* mlabs);
285 
286 /**
287  * Canonical dname compare. Takes care of counting labels.
288  * Per rfc 4034 canonical order.
289  *
290  * @param d1: first dname. pointer to uncompressed wireformat.
291  * @param d2: second dname. pointer to uncompressed wireformat.
292  * @return: 0 for equal, -1 smaller, or +1 d1 larger than d2.
293  */
294 int dname_canonical_compare(uint8_t* d1, uint8_t* d2);
295 
296 /**
297  * Get the shared topdomain between two names. Root "." or longer.
298  * @param d1: first dname. pointer to uncompressed wireformat.
299  * @param d2: second dname. pointer to uncompressed wireformat.
300  * @return pointer to shared topdomain. Ptr to a part of d1.
301  */
302 uint8_t* dname_get_shared_topdomain(uint8_t* d1, uint8_t* d2);
303 
304 #endif /* UTIL_DATA_DNAME_H */
305