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
198 * \return uint8_t* pointer to the rdf's data
200 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
202 /* creator functions */
205 * allocates a new rdf structure and fills it.
206 * This function DOES NOT copy the contents from
207 * the buffer, unlinke ldns_rdf_new_frm_data()
208 * \param[in] type type of the rdf
209 * \param[in] size size of the buffer
210 * \param[in] data pointer to the buffer to be copied
211 * \return the new rdf structure or NULL on failure
213 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
216 * allocates a new rdf structure and fills it.
217 * This function _does_ copy the contents from
218 * the buffer, unlinke ldns_rdf_new()
219 * \param[in] type type of the rdf
220 * \param[in] size size of the buffer
221 * \param[in] data pointer to the buffer to be copied
222 * \return the new rdf structure or NULL on failure
224 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
227 * creates a new rdf from a string.
228 * \param[in] type type to use
229 * \param[in] str string to use
230 * \return ldns_rdf* or NULL in case of an error
232 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
235 * creates a new rdf from a file containing a string.
236 * \param[out] r the new rdf
237 * \param[in] type type to use
238 * \param[in] fp the file pointer to use
239 * \return LDNS_STATUS_OK or the error
241 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
244 * creates a new rdf from a file containing a string.
245 * \param[out] r the new rdf
246 * \param[in] type type to use
247 * \param[in] fp the file pointer to use
248 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
249 * \return LDNS_STATUS_OK or the error
251 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
253 /* destroy functions */
256 * frees a rdf structure, leaving the
257 * data pointer intact.
258 * \param[in] rd the pointer to be freed
261 void ldns_rdf_free(ldns_rdf *rd);
264 * frees a rdf structure _and_ frees the
265 * data. rdf should be created with _new_frm_data
266 * \param[in] rd the rdf structure to be freed
269 void ldns_rdf_deep_free(ldns_rdf *rd);
271 /* conversion functions */
274 * returns the rdf containing the native uint8_t repr.
275 * \param[in] type the ldns_rdf type to use
276 * \param[in] value the uint8_t to use
277 * \return ldns_rdf* with the converted value
279 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
282 * returns the rdf containing the native uint16_t representation.
283 * \param[in] type the ldns_rdf type to use
284 * \param[in] value the uint16_t to use
285 * \return ldns_rdf* with the converted value
287 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
290 * returns an rdf that contains the given int32 value.
292 * Because multiple rdf types can contain an int32, the
293 * type must be specified
294 * \param[in] type the ldns_rdf type to use
295 * \param[in] value the uint32_t to use
296 * \return ldns_rdf* with the converted value
298 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
301 * returns an int16_data rdf that contains the data in the
302 * given array, preceded by an int16 specifying the length.
304 * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
305 * \param[in] size the size of the data
306 * \param[in] *data pointer to the actual data
308 * \return ldns_rd* the rdf with the data
310 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
313 * reverses an rdf, only actually useful for AAAA and A records.
314 * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
315 * \param[in] *rd rdf to be reversed
316 * \return the reversed rdf (a newly created rdf)
318 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
321 * returns the native uint8_t representation from the rdf.
322 * \param[in] rd the ldns_rdf to operate on
323 * \return uint8_t the value extracted
325 uint8_t ldns_rdf2native_int8(const ldns_rdf *rd);
328 * returns the native uint16_t representation from the rdf.
329 * \param[in] rd the ldns_rdf to operate on
330 * \return uint16_t the value extracted
332 uint16_t ldns_rdf2native_int16(const ldns_rdf *rd);
335 * returns the native uint32_t representation from the rdf.
336 * \param[in] rd the ldns_rdf to operate on
337 * \return uint32_t the value extracted
339 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
342 * returns the native time_t representation from the rdf.
343 * \param[in] rd the ldns_rdf to operate on
344 * \return time_t the value extracted (32 bits currently)
346 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
349 * converts a ttl value (like 5d2h) to a long.
350 * \param[in] nptr the start of the string
351 * \param[out] endptr points to the last char in case of error
352 * \return the convert duration value
354 uint32_t ldns_str2period(const char *nptr, const char **endptr);
357 * removes \\DDD, \\[space] and other escapes from the input.
358 * See RFC 1035, section 5.1.
359 * \param[in] word what to check
360 * \param[in] length the string
361 * \return ldns_status mesg
363 ldns_status ldns_octet(char *word, size_t *length);
366 * clones a rdf structure. The data is copied.
367 * \param[in] rd rdf to be copied
368 * \return a new rdf structure
370 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
373 * compares two rdf's on their wire formats.
374 * (To order dnames according to rfc4034, use ldns_dname_compare)
375 * \param[in] rd1 the first one
376 * \param[in] rd2 the second one
378 * \return -1 if rd1 comes before rd2
379 * \return +1 if rd2 comes before rd1
381 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
387 #endif /* LDNS_RDATA_H */