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