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 8192
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_16BYTES 16
39 #define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
42 * The different types of RDATA fields.
44 enum ldns_enum_rdf_type
65 LDNS_RDF_TYPE_B32_EXT,
70 /** nsec type codes */
76 /** certificate algorithm */
77 LDNS_RDF_TYPE_CERT_ALG,
78 /** a key algorithm */
81 LDNS_RDF_TYPE_UNKNOWN,
86 /** tsig time 48 bits */
87 LDNS_RDF_TYPE_TSIGTIME,
89 /** variable length any type rdata where the length
90 is specified by the first 2 bytes */
91 LDNS_RDF_TYPE_INT16_DATA,
92 /** protocol and port bitmaps */
93 LDNS_RDF_TYPE_SERVICE,
96 /** well known services */
103 LDNS_RDF_TYPE_IPSECKEY,
104 /** nsec3 hash salt */
105 LDNS_RDF_TYPE_NSEC3_SALT,
106 /** nsec3 base32 string (with length byte on wire */
107 LDNS_RDF_TYPE_NSEC3_NEXT_OWNER
109 typedef enum ldns_enum_rdf_type ldns_rdf_type;
112 * algorithms used in CERT rrs
114 enum ldns_enum_cert_algorithm
122 LDNS_CERT_ACPKIX = 7,
123 LDNS_CERT_IACPKIX = 8,
127 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
132 * Resource record data field.
134 * The data is a network ordered array of bytes, which size is specified by
135 * the (16-bit) size field. To correctly parse it, use the type
136 * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
138 struct ldns_struct_rdf
140 /** The size of the data (in octets) */
142 /** The type of the data */
144 /** Pointer to the data (raw octets) */
147 typedef struct ldns_struct_rdf ldns_rdf;
151 /* write access functions */
154 * sets the size of the rdf.
155 * \param[in] *rd the rdf to operate on
156 * \param[in] size the new size
159 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
162 * sets the size of the rdf.
163 * \param[in] *rd the rdf to operate on
164 * \param[in] type the new type
167 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
170 * sets the size of the rdf.
171 * \param[in] *rd the rdf to operate on
172 * \param[in] *data pointer to the new data
175 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
180 * returns the size of the rdf.
181 * \param[in] *rd the rdf to read from
182 * \return uint16_t with the size
184 size_t ldns_rdf_size(const ldns_rdf *rd);
187 * returns the type of the rdf. We need to insert _get_
188 * here to prevent conflict the the rdf_type TYPE.
189 * \param[in] *rd the rdf to read from
190 * \return ldns_rdf_type with the type
192 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
195 * returns the data of the rdf.
196 * \param[in] *rd the rdf to read from
197 * \return uint8_t* pointer to the rdf's data
199 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
201 /* creator functions */
204 * allocates a new rdf structure and fills it.
205 * This function DOES NOT copy the contents from
206 * the buffer, unlinke ldns_rdf_new_frm_data()
207 * \param[in] type type of the rdf
208 * \param[in] size size of the buffer
209 * \param[in] data pointer to the buffer to be copied
210 * \return the new rdf structure or NULL on failure
212 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
215 * allocates a new rdf structure and fills it.
216 * This function _does_ copy the contents from
217 * the buffer, unlinke ldns_rdf_new()
218 * \param[in] type type of the rdf
219 * \param[in] size size of the buffer
220 * \param[in] data pointer to the buffer to be copied
221 * \return the new rdf structure or NULL on failure
223 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
226 * creates a new rdf from a string.
227 * \param[in] type type to use
228 * \param[in] str string to use
229 * \return ldns_rdf* or NULL in case of an error
231 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
234 * creates a new rdf from a file containing a string.
235 * \param[out] r the new rdf
236 * \param[in] type type to use
237 * \param[in] fp the file pointer to use
238 * \return LDNS_STATUS_OK or the error
240 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
243 * creates a new rdf from a file containing a string.
244 * \param[out] r the new rdf
245 * \param[in] type type to use
246 * \param[in] fp the file pointer to use
247 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
248 * \return LDNS_STATUS_OK or the error
250 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
252 /* destroy functions */
255 * frees a rdf structure, leaving the
256 * data pointer intact.
257 * \param[in] rd the pointer to be freed
260 void ldns_rdf_free(ldns_rdf *rd);
263 * frees a rdf structure _and_ frees the
264 * data. rdf should be created with _new_frm_data
265 * \param[in] rd the rdf structure to be freed
268 void ldns_rdf_deep_free(ldns_rdf *rd);
270 /* conversion functions */
273 * returns the rdf containing the native uint8_t repr.
274 * \param[in] type the ldns_rdf type to use
275 * \param[in] value the uint8_t to use
276 * \return ldns_rdf* with the converted value
278 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
281 * returns the rdf containing the native uint16_t representation.
282 * \param[in] type the ldns_rdf type to use
283 * \param[in] value the uint16_t to use
284 * \return ldns_rdf* with the converted value
286 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
289 * returns an rdf that contains the given int32 value.
291 * Because multiple rdf types can contain an int32, the
292 * type must be specified
293 * \param[in] type the ldns_rdf type to use
294 * \param[in] value the uint32_t to use
295 * \return ldns_rdf* with the converted value
297 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
300 * returns an int16_data rdf that contains the data in the
301 * given array, preceded by an int16 specifying the length.
303 * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
304 * \param[in] size the size of the data
305 * \param[in] *data pointer to the actual data
306 * \return ldns_rd* the rdf with the data
308 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
311 * reverses an rdf, only actually useful for AAAA and A records.
312 * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
313 * \param[in] *rd rdf to be reversed
314 * \return the reversed rdf (a newly created rdf)
316 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
319 * returns the native uint8_t representation from the rdf.
320 * \param[in] rd the ldns_rdf to operate on
321 * \return uint8_t the value extracted
323 uint8_t ldns_rdf2native_int8(const ldns_rdf *rd);
326 * returns the native uint16_t representation from the rdf.
327 * \param[in] rd the ldns_rdf to operate on
328 * \return uint16_t the value extracted
330 uint16_t ldns_rdf2native_int16(const ldns_rdf *rd);
333 * returns the native uint32_t representation from the rdf.
334 * \param[in] rd the ldns_rdf to operate on
335 * \return uint32_t the value extracted
337 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
340 * returns the native time_t representation from the rdf.
341 * \param[in] rd the ldns_rdf to operate on
342 * \return time_t the value extracted (32 bits currently)
344 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
347 * converts a ttl value (like 5d2h) to a long.
348 * \param[in] nptr the start of the string
349 * \param[out] endptr points to the last char in case of error
350 * \return the convert duration value
352 uint32_t ldns_str2period(const char *nptr, const char **endptr);
355 * removes \\DDD, \\[space] and other escapes from the input.
356 * See RFC 1035, section 5.1.
357 * \param[in] word what to check
358 * \param[in] length the string
359 * \return ldns_status mesg
361 ldns_status ldns_octet(char *word, size_t *length);
364 * clones a rdf structure. The data is copied.
365 * \param[in] rd rdf to be copied
366 * \return a new rdf structure
368 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
371 * compares two rdf's on their wire formats.
372 * (To order dnames according to rfc4034, use ldns_dname_compare)
373 * \param[in] rd1 the first one
374 * \param[in] rd2 the second one
376 * \return -1 if rd1 comes before rd2
377 * \return +1 if rd2 comes before rd1
379 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
385 #endif /* LDNS_RDATA_H */