6 * a Net::DNS like library for C
8 * (c) NLnet Labs, 2005-2006
10 * See the file LICENSE for the license
17 * Defines ldns_rdf and functions to manipulate those.
24 #include <ldns/common.h>
25 #include <ldns/error.h>
31 #define LDNS_MAX_RDFLEN 65535
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
40 #define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
43 * The different types of RDATA fields.
45 enum ldns_enum_rdf_type
66 LDNS_RDF_TYPE_B32_EXT,
71 /** nsec type codes */
77 /** certificate algorithm */
78 LDNS_RDF_TYPE_CERT_ALG,
79 /** a key algorithm */
82 LDNS_RDF_TYPE_UNKNOWN,
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) */
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,
100 /** well known services */
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,
113 /** 4 shorts represented as 4 * 16 bit hex numbers
114 * separated by colons. For NID and L64.
116 LDNS_RDF_TYPE_ILNP64,
118 /** 6 * 8 bit hex numbers separated by dashes. For EUI48. */
120 /** 8 * 8 bit hex numbers separated by dashes. For EUI64. */
123 /** A non-zero sequence of US-ASCII letters and numbers in lower case.
128 /** A <character-string> encoding of the value field as specified
129 * [RFC1035], Section 5.1., encoded as remaining rdata.
132 LDNS_RDF_TYPE_LONG_STR,
134 /** Since RFC7218 TLSA records can be given with mnemonics,
135 * hence these rdata field types. But as with DNSKEYs, the output
138 LDNS_RDF_TYPE_CERTIFICATE_USAGE,
139 LDNS_RDF_TYPE_SELECTOR,
140 LDNS_RDF_TYPE_MATCHING_TYPE,
143 LDNS_RDF_TYPE_BITMAP = LDNS_RDF_TYPE_NSEC
145 typedef enum ldns_enum_rdf_type ldns_rdf_type;
148 * algorithms used in CERT rrs
150 enum ldns_enum_cert_algorithm
158 LDNS_CERT_ACPKIX = 7,
159 LDNS_CERT_IACPKIX = 8,
163 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
168 * Resource record data field.
170 * The data is a network ordered array of bytes, which size is specified by
171 * the (16-bit) size field. To correctly parse it, use the type
172 * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
174 struct ldns_struct_rdf
176 /** The size of the data (in octets) */
178 /** The type of the data */
180 /** Pointer to the data (raw octets) */
183 typedef struct ldns_struct_rdf ldns_rdf;
187 /* write access functions */
190 * sets the size of the rdf.
191 * \param[in] *rd the rdf to operate on
192 * \param[in] size the new size
195 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
198 * sets the size of the rdf.
199 * \param[in] *rd the rdf to operate on
200 * \param[in] type the new type
203 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
206 * sets the size of the rdf.
207 * \param[in] *rd the rdf to operate on
208 * \param[in] *data pointer to the new data
211 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
216 * returns the size of the rdf.
217 * \param[in] *rd the rdf to read from
218 * \return uint16_t with the size
220 size_t ldns_rdf_size(const ldns_rdf *rd);
223 * returns the type of the rdf. We need to insert _get_
224 * here to prevent conflict the the rdf_type TYPE.
225 * \param[in] *rd the rdf to read from
226 * \return ldns_rdf_type with the type
228 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
231 * returns the data of the rdf.
232 * \param[in] *rd the rdf to read from
234 * \return uint8_t* pointer to the rdf's data
236 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
238 /* creator functions */
241 * allocates a new rdf structure and fills it.
242 * This function DOES NOT copy the contents from
243 * the buffer, unlinke ldns_rdf_new_frm_data()
244 * \param[in] type type of the rdf
245 * \param[in] size size of the buffer
246 * \param[in] data pointer to the buffer to be copied
247 * \return the new rdf structure or NULL on failure
249 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
252 * allocates a new rdf structure and fills it.
253 * This function _does_ copy the contents from
254 * the buffer, unlinke ldns_rdf_new()
255 * \param[in] type type of the rdf
256 * \param[in] size size of the buffer
257 * \param[in] data pointer to the buffer to be copied
258 * \return the new rdf structure or NULL on failure
260 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
263 * creates a new rdf from a string.
264 * \param[in] type type to use
265 * \param[in] str string to use
266 * \return ldns_rdf* or NULL in case of an error
268 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
271 * creates a new rdf from a file containing a string.
272 * \param[out] r the new rdf
273 * \param[in] type type to use
274 * \param[in] fp the file pointer to use
275 * \return LDNS_STATUS_OK or the error
277 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
280 * creates a new rdf from a file containing a string.
281 * \param[out] r the new rdf
282 * \param[in] type type to use
283 * \param[in] fp the file pointer to use
284 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
285 * \return LDNS_STATUS_OK or the error
287 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
289 /* destroy functions */
292 * frees a rdf structure, leaving the
293 * data pointer intact.
294 * \param[in] rd the pointer to be freed
297 void ldns_rdf_free(ldns_rdf *rd);
300 * frees a rdf structure _and_ frees the
301 * data. rdf should be created with _new_frm_data
302 * \param[in] rd the rdf structure to be freed
305 void ldns_rdf_deep_free(ldns_rdf *rd);
307 /* conversion functions */
310 * returns the rdf containing the native uint8_t repr.
311 * \param[in] type the ldns_rdf type to use
312 * \param[in] value the uint8_t to use
313 * \return ldns_rdf* with the converted value
315 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
318 * returns the rdf containing the native uint16_t representation.
319 * \param[in] type the ldns_rdf type to use
320 * \param[in] value the uint16_t to use
321 * \return ldns_rdf* with the converted value
323 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
326 * returns an rdf that contains the given int32 value.
328 * Because multiple rdf types can contain an int32, the
329 * type must be specified
330 * \param[in] type the ldns_rdf type to use
331 * \param[in] value the uint32_t to use
332 * \return ldns_rdf* with the converted value
334 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
337 * returns an int16_data rdf that contains the data in the
338 * given array, preceded by an int16 specifying the length.
340 * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
341 * \param[in] size the size of the data
342 * \param[in] *data pointer to the actual data
344 * \return ldns_rd* the rdf with the data
346 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
349 * reverses an rdf, only actually useful for AAAA and A records.
350 * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
351 * \param[in] *rd rdf to be reversed
352 * \return the reversed rdf (a newly created rdf)
354 ldns_rdf *ldns_rdf_address_reverse(const ldns_rdf *rd);
357 * returns the native uint8_t representation from the rdf.
358 * \param[in] rd the ldns_rdf to operate on
359 * \return uint8_t the value extracted
361 uint8_t ldns_rdf2native_int8(const ldns_rdf *rd);
364 * returns the native uint16_t representation from the rdf.
365 * \param[in] rd the ldns_rdf to operate on
366 * \return uint16_t the value extracted
368 uint16_t ldns_rdf2native_int16(const ldns_rdf *rd);
371 * returns the native uint32_t representation from the rdf.
372 * \param[in] rd the ldns_rdf to operate on
373 * \return uint32_t the value extracted
375 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
378 * returns the native time_t representation from the rdf.
379 * \param[in] rd the ldns_rdf to operate on
380 * \return time_t the value extracted (32 bits currently)
382 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
385 * converts a ttl value (like 5d2h) to a long.
386 * \param[in] nptr the start of the string
387 * \param[out] endptr points to the last char in case of error
388 * \return the convert duration value
390 uint32_t ldns_str2period(const char *nptr, const char **endptr);
393 * removes \\DDD, \\[space] and other escapes from the input.
394 * See RFC 1035, section 5.1.
395 * \param[in] word what to check
396 * \param[in] length the string
397 * \return ldns_status mesg
399 ldns_status ldns_octet(char *word, size_t *length);
402 * clones a rdf structure. The data is copied.
403 * \param[in] rd rdf to be copied
404 * \return a new rdf structure
406 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
409 * compares two rdf's on their wire formats.
410 * (To order dnames according to rfc4034, use ldns_dname_compare)
411 * \param[in] rd1 the first one
412 * \param[in] rd2 the second one
414 * \return -1 if rd1 comes before rd2
415 * \return +1 if rd2 comes before rd1
417 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
420 * Gets the algorithm value, the HIT and Public Key data from the rdf with
421 * type LDNS_RDF_TYPE_HIP.
422 * \param[in] rdf the rdf with type LDNS_RDF_TYPE_HIP
423 * \param[out] alg the algorithm
424 * \param[out] hit_size the size of the HIT data
425 * \param[out] hit the hit data
426 * \param[out] pk_size the size of the Public Key data
427 * \param[out] pk the Public Key data
428 * \return LDNS_STATUS_OK on success, and the error otherwise
430 ldns_status ldns_rdf_hip_get_alg_hit_pk(ldns_rdf *rdf, uint8_t* alg,
431 uint8_t *hit_size, uint8_t** hit,
432 uint16_t *pk_size, uint8_t** pk);
435 * Creates a new LDNS_RDF_TYPE_HIP rdf from given data.
436 * \param[out] rdf the newly created LDNS_RDF_TYPE_HIP rdf
437 * \param[in] alg the algorithm
438 * \param[in] hit_size the size of the HIT data
439 * \param[in] hit the hit data
440 * \param[in] pk_size the size of the Public Key data
441 * \param[in] pk the Public Key data
442 * \return LDNS_STATUS_OK on success, and the error otherwise
444 ldns_status ldns_rdf_hip_new_frm_alg_hit_pk(ldns_rdf** rdf, uint8_t alg,
445 uint8_t hit_size, uint8_t *hit, uint16_t pk_size, uint8_t *pk);
451 #endif /* LDNS_RDATA_H */