xref: /freebsd/contrib/ldns/ldns/rdata.h (revision 9ed998a81bab54203604d08293089db875758686)
1 /*
2  * rdata.h
3  *
4  * rdata definitions
5  *
6  * a Net::DNS like library for C
7  *
8  * (c) NLnet Labs, 2005-2006
9  *
10  * See the file LICENSE for the license
11  */
12 
13 
14 /**
15  * \file
16  *
17  * Defines ldns_rdf and functions to manipulate those.
18  */
19 
20 
21 #ifndef LDNS_RDATA_H
22 #define LDNS_RDATA_H
23 
24 #include <ldns/common.h>
25 #include <ldns/error.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define LDNS_MAX_RDFLEN	65535
32 
33 #define LDNS_RDF_SIZE_BYTE              1
34 #define LDNS_RDF_SIZE_WORD              2
35 #define LDNS_RDF_SIZE_DOUBLEWORD        4
36 #define LDNS_RDF_SIZE_6BYTES            6
37 #define LDNS_RDF_SIZE_8BYTES            8
38 #define LDNS_RDF_SIZE_16BYTES           16
39 
40 #define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
41 
42 /**
43  * The different types of RDATA fields.
44  */
45 enum ldns_enum_rdf_type
46 {
47 	/** none */
48 	LDNS_RDF_TYPE_NONE,
49 	/** domain name */
50 	LDNS_RDF_TYPE_DNAME,
51 	/** 8 bits */
52 	LDNS_RDF_TYPE_INT8,
53 	/** 16 bits */
54 	LDNS_RDF_TYPE_INT16,
55 	/** 32 bits */
56 	LDNS_RDF_TYPE_INT32,
57 	/** 64 bits */
58 	LDNS_RDF_TYPE_INT64,
59 	/** A record */
60 	LDNS_RDF_TYPE_A,
61 	/** AAAA record */
62 	LDNS_RDF_TYPE_AAAA,
63 	/** txt string */
64 	LDNS_RDF_TYPE_STR,
65 	/** apl data */
66 	LDNS_RDF_TYPE_APL,
67 	/** b32 string */
68 	LDNS_RDF_TYPE_B32_EXT,
69 	/** b64 string */
70 	LDNS_RDF_TYPE_B64,
71 	/** hex string */
72 	LDNS_RDF_TYPE_HEX,
73 	/** nsec type codes */
74 	LDNS_RDF_TYPE_NSEC,
75 	LDNS_RDF_TYPE_BITMAP = LDNS_RDF_TYPE_NSEC,
76 	/** a RR type */
77 	LDNS_RDF_TYPE_TYPE,
78 	/** a class */
79 	LDNS_RDF_TYPE_CLASS,
80 	/** certificate algorithm */
81 	LDNS_RDF_TYPE_CERT_ALG,
82 	/** a key algorithm */
83 	LDNS_RDF_TYPE_ALG,
84 	/** unknown types */
85 	LDNS_RDF_TYPE_UNKNOWN,
86 	/** time (32 bits) */
87 	LDNS_RDF_TYPE_TIME,
88 	/** period */
89 	LDNS_RDF_TYPE_PERIOD,
90 	/** tsig time 48 bits */
91 	LDNS_RDF_TYPE_TSIGTIME,
92 	/** Represents the Public Key Algorithm, HIT and Public Key fields
93 	    for the HIP RR types.  A HIP specific rdf type is used because of
94 	    the unusual layout in wireformat (see RFC 5205 Section 5) */
95 	LDNS_RDF_TYPE_HIP,
96 	/** variable length any type rdata where the length
97 	    is specified by the first 2 bytes */
98 	LDNS_RDF_TYPE_INT16_DATA,
99 	/** protocol and port bitmaps */
100 	LDNS_RDF_TYPE_SERVICE,
101 	/** location data */
102 	LDNS_RDF_TYPE_LOC,
103 	/** well known services */
104 	LDNS_RDF_TYPE_WKS,
105 	/** NSAP */
106 	LDNS_RDF_TYPE_NSAP,
107 	/** ATMA */
108 	LDNS_RDF_TYPE_ATMA,
109 	/** IPSECKEY */
110 	LDNS_RDF_TYPE_IPSECKEY,
111 	/** nsec3 hash salt */
112 	LDNS_RDF_TYPE_NSEC3_SALT,
113 	/** nsec3 base32 string (with length byte on wire */
114 	LDNS_RDF_TYPE_NSEC3_NEXT_OWNER,
115 
116 	/** 4 shorts represented as 4 * 16 bit hex numbers
117 	 *  separated by colons. For NID and L64.
118 	 */
119 	LDNS_RDF_TYPE_ILNP64,
120 
121 	/** 6 * 8 bit hex numbers separated by dashes. For EUI48. */
122 	LDNS_RDF_TYPE_EUI48,
123 	/** 8 * 8 bit hex numbers separated by dashes. For EUI64. */
124 	LDNS_RDF_TYPE_EUI64,
125 
126 	/** Character string without quotes. */
127 	LDNS_RDF_TYPE_UNQUOTED,
128 
129 	/** A non-zero sequence of US-ASCII letters and numbers in lower case.
130 	 *  For CAA.
131 	 */
132 	LDNS_RDF_TYPE_TAG,
133 
134 	/** A <character-string> encoding of the value field as specified
135 	 * [RFC1035], Section 5.1., encoded as remaining rdata.
136 	 * For CAA.
137 	 */
138 	LDNS_RDF_TYPE_LONG_STR,
139 
140 	/** Since RFC7218 TLSA records can be given with mnemonics,
141 	 * hence these rdata field types.  But as with DNSKEYs, the output
142 	 * is always numeric.
143 	 */
144 	LDNS_RDF_TYPE_CERTIFICATE_USAGE,
145 	LDNS_RDF_TYPE_SELECTOR,
146 	LDNS_RDF_TYPE_MATCHING_TYPE,
147 
148 	/** draft-ietf-mboned-driad-amt-discovery **/
149 	LDNS_RDF_TYPE_AMTRELAY,
150 
151 	/** draft-ietf-dnsop-svcb-https **/
152 	LDNS_RDF_TYPE_SVCPARAMS,
153 
154 	/** draft-johnson-dns-ipn-cla-07 **/
155 	LDNS_RDF_TYPE_IPN
156 };
157 typedef enum ldns_enum_rdf_type ldns_rdf_type;
158 
159 /**
160  * algorithms used in CERT rrs
161  */
162 enum ldns_enum_cert_algorithm
163 {
164         LDNS_CERT_PKIX		= 1,
165         LDNS_CERT_SPKI		= 2,
166         LDNS_CERT_PGP		= 3,
167         LDNS_CERT_IPKIX         = 4,
168         LDNS_CERT_ISPKI         = 5,
169         LDNS_CERT_IPGP          = 6,
170         LDNS_CERT_ACPKIX        = 7,
171         LDNS_CERT_IACPKIX       = 8,
172         LDNS_CERT_URI		= 253,
173         LDNS_CERT_OID		= 254
174 };
175 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
176 
177 /**
178  * keys types in SVCPARAMS rdata fields
179  */
180 enum ldns_enum_svcparam_key
181 {
182 	LDNS_SVCPARAM_KEY_MANDATORY		= 0,
183 	LDNS_SVCPARAM_KEY_ALPN			= 1,
184 	LDNS_SVCPARAM_KEY_NO_DEFAULT_ALPN	= 2,
185 	LDNS_SVCPARAM_KEY_PORT			= 3,
186 	LDNS_SVCPARAM_KEY_IPV4HINT		= 4,
187 	LDNS_SVCPARAM_KEY_ECH			= 5,
188 	LDNS_SVCPARAM_KEY_IPV6HINT		= 6,
189         LDNS_SVCPARAM_KEY_DOHPATH               = 7,
190 	LDNS_SVCPARAM_KEY_LAST_KEY		= 7,
191 	LDNS_SVCPARAM_KEY_RESERVED		= 65535
192 };
193 typedef	enum ldns_enum_svcparam_key ldns_svcparam_key;
194 
195 /**
196  * Resource record data field.
197  *
198  * The data is a network ordered array of bytes, which size is specified by
199  * the (16-bit) size field. To correctly parse it, use the type
200  * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
201  */
202 struct ldns_struct_rdf
203 {
204 	/** The size of the data (in octets) */
205 	size_t _size;
206 	/** The type of the data */
207 	ldns_rdf_type _type;
208 	/** Pointer to the data (raw octets) */
209 	void  *_data;
210 };
211 typedef struct ldns_struct_rdf ldns_rdf;
212 
213 /* prototypes */
214 
215 /* write access functions */
216 
217 /**
218  * sets the size of the rdf.
219  * \param[in] *rd the rdf to operate on
220  * \param[in] size the new size
221  * \return void
222  */
223 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
224 
225 /**
226  * sets the size of the rdf.
227  * \param[in] *rd the rdf to operate on
228  * \param[in] type the new type
229  * \return void
230  */
231 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
232 
233 /**
234  * sets the size of the rdf.
235  * \param[in] *rd the rdf to operate on
236  * \param[in] *data pointer to the new data
237  * \return void
238  */
239 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
240 
241 /* read access */
242 
243 /**
244  * returns the size of the rdf.
245  * \param[in] *rd the rdf to read from
246  * \return uint16_t with the size
247  */
248 size_t ldns_rdf_size(const ldns_rdf *rd);
249 
250 /**
251  * returns the type of the rdf. We need to insert _get_
252  * here to prevent conflict the rdf_type TYPE.
253  * \param[in] *rd the rdf to read from
254  * \return ldns_rdf_type with the type
255  */
256 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
257 
258 /**
259  * returns the data of the rdf.
260  * \param[in] *rd the rdf to read from
261  *
262  * \return uint8_t* pointer to the rdf's data
263  */
264 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
265 
266 /* creator functions */
267 
268 /**
269  * allocates a new rdf structure and fills it.
270  * This function DOES NOT copy the contents from
271  * the buffer, unlike ldns_rdf_new_frm_data()
272  * \param[in] type type of the rdf
273  * \param[in] size size of the buffer
274  * \param[in] data pointer to the buffer to be copied
275  * \return the new rdf structure or NULL on failure
276  */
277 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
278 
279 /**
280  * allocates a new rdf structure and fills it.
281  * This function _does_ copy the contents from
282  * the buffer, unlike ldns_rdf_new()
283  * \param[in] type type of the rdf
284  * \param[in] size size of the buffer
285  * \param[in] data pointer to the buffer to be copied
286  * \return the new rdf structure or NULL on failure
287  */
288 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
289 
290 /**
291  * creates a new rdf from a string.
292  * \param[in] type   type to use
293  * \param[in] str string to use
294  * \return ldns_rdf* or NULL in case of an error
295  */
296 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
297 
298 /**
299  * creates a new rdf from a file containing a string.
300  * \param[out] r the new rdf
301  * \param[in] type   type to use
302  * \param[in] fp the file pointer  to use
303  * \return LDNS_STATUS_OK or the error
304  */
305 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
306 
307 /**
308  * creates a new rdf from a file containing a string.
309  * \param[out] r the new rdf
310  * \param[in] type   type to use
311  * \param[in] fp the file pointer  to use
312  * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
313  * \return LDNS_STATUS_OK or the error
314  */
315 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
316 
317 /* destroy functions */
318 
319 /**
320  * frees a rdf structure, leaving the
321  * data pointer intact.
322  * \param[in] rd the pointer to be freed
323  * \return void
324  */
325 void ldns_rdf_free(ldns_rdf *rd);
326 
327 /**
328  * frees a rdf structure _and_ frees the
329  * data. rdf should be created with _new_frm_data
330  * \param[in] rd the rdf structure to be freed
331  * \return void
332  */
333 void ldns_rdf_deep_free(ldns_rdf *rd);
334 
335 /* conversion functions */
336 
337 /**
338  * returns the rdf containing the native uint8_t repr.
339  * \param[in] type the ldns_rdf type to use
340  * \param[in] value the uint8_t to use
341  * \return ldns_rdf* with the converted value
342  */
343 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
344 
345 /**
346  * returns the rdf containing the native uint16_t representation.
347  * \param[in] type the ldns_rdf type to use
348  * \param[in] value the uint16_t to use
349  * \return ldns_rdf* with the converted value
350  */
351 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
352 
353 /**
354  * returns an rdf that contains the given int32 value.
355  *
356  * Because multiple rdf types can contain an int32, the
357  * type must be specified
358  * \param[in] type the ldns_rdf type to use
359  * \param[in] value the uint32_t to use
360  * \return ldns_rdf* with the converted value
361  */
362 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
363 
364 /**
365  * returns an int16_data rdf that contains the data in the
366  * given array, preceded by an int16 specifying the length.
367  *
368  * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
369  * \param[in] size the size of the data
370  * \param[in] *data pointer to the actual data
371  *
372  * \return ldns_rd* the rdf with the data
373  */
374 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
375 
376 /**
377  * reverses an rdf, only actually useful for AAAA and A records.
378  * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
379  * \param[in] *rd rdf to be reversed
380  * \return the reversed rdf (a newly created rdf)
381  */
382 ldns_rdf *ldns_rdf_address_reverse(const ldns_rdf *rd);
383 
384 /**
385  * returns the native uint8_t representation from the rdf.
386  * \param[in] rd the ldns_rdf to operate on
387  * \return uint8_t the value extracted
388  */
389 uint8_t 	ldns_rdf2native_int8(const ldns_rdf *rd);
390 
391 /**
392  * returns the native uint16_t representation from the rdf.
393  * \param[in] rd the ldns_rdf to operate on
394  * \return uint16_t the value extracted
395  */
396 uint16_t	ldns_rdf2native_int16(const ldns_rdf *rd);
397 
398 /**
399  * returns the native uint32_t representation from the rdf.
400  * \param[in] rd the ldns_rdf to operate on
401  * \return uint32_t the value extracted
402  */
403 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
404 
405 /**
406  * returns the native time_t representation from the rdf.
407  * \param[in] rd the ldns_rdf to operate on
408  * \return time_t the value extracted (32 bits currently)
409  */
410 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
411 
412 /**
413  * converts a ttl value (like 5d2h) to a long.
414  * \param[in] nptr the start of the string
415  * \param[out] endptr points to the last char in case of error
416  * \return the convert duration value
417  */
418 uint32_t ldns_str2period(const char *nptr, const char **endptr);
419 
420 /**
421  * removes \\DDD, \\[space] and other escapes from the input.
422  * See RFC 1035, section 5.1.
423  * \param[in] word what to check
424  * \param[in] length the string
425  * \return ldns_status mesg
426  */
427 ldns_status ldns_octet(char *word, size_t *length);
428 
429 /**
430  * clones a rdf structure. The data is copied.
431  * \param[in] rd rdf to be copied
432  * \return a new rdf structure
433  */
434 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
435 
436 /**
437  * compares two rdf's on their wire formats.
438  * (To order dnames according to rfc4034, use ldns_dname_compare)
439  * \param[in] rd1 the first one
440  * \param[in] rd2 the second one
441  * \return 0 if equal
442  * \return -1 if rd1 comes before rd2
443  * \return +1 if rd2 comes before rd1
444  */
445 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
446 
447 /**
448  * Gets the algorithm value, the HIT and Public Key data from the rdf with
449  * type LDNS_RDF_TYPE_HIP.
450  * \param[in] rdf the rdf with type LDNS_RDF_TYPE_HIP
451  * \param[out] alg      the algorithm
452  * \param[out] hit_size the size of the HIT data
453  * \param[out] hit      the hit data
454  * \param[out] pk_size  the size of the Public Key data
455  * \param[out] pk       the  Public Key data
456  * \return LDNS_STATUS_OK on success, and the error otherwise
457  */
458 ldns_status ldns_rdf_hip_get_alg_hit_pk(ldns_rdf *rdf, uint8_t* alg,
459 		uint8_t *hit_size, uint8_t** hit,
460 		uint16_t *pk_size, uint8_t** pk);
461 
462 /**
463  * Creates a new LDNS_RDF_TYPE_HIP rdf from given data.
464  * \param[out] rdf      the newly created LDNS_RDF_TYPE_HIP rdf
465  * \param[in]  alg      the algorithm
466  * \param[in]  hit_size the size of the HIT data
467  * \param[in]  hit      the hit data
468  * \param[in]  pk_size  the size of the Public Key data
469  * \param[in]  pk       the  Public Key data
470  * \return LDNS_STATUS_OK on success, and the error otherwise
471  */
472 ldns_status ldns_rdf_hip_new_frm_alg_hit_pk(ldns_rdf** rdf, uint8_t alg,
473 		uint8_t hit_size, uint8_t *hit, uint16_t pk_size, uint8_t *pk);
474 
475 #ifdef __cplusplus
476 }
477 #endif
478 
479 #endif	/* LDNS_RDATA_H */
480