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>
27 #define LDNS_MAX_RDFLEN 8192
29 #define LDNS_RDF_SIZE_BYTE 1
30 #define LDNS_RDF_SIZE_WORD 2
31 #define LDNS_RDF_SIZE_DOUBLEWORD 4
32 #define LDNS_RDF_SIZE_6BYTES 6
33 #define LDNS_RDF_SIZE_16BYTES 16
35 #define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
38 * The different types of RDATA fields.
40 enum ldns_enum_rdf_type
61 LDNS_RDF_TYPE_B32_EXT,
66 /** nsec type codes */
72 /** certificate algorithm */
73 LDNS_RDF_TYPE_CERT_ALG,
74 /** a key algorithm */
77 LDNS_RDF_TYPE_UNKNOWN,
82 /** tsig time 48 bits */
83 LDNS_RDF_TYPE_TSIGTIME,
85 /** variable length any type rdata where the length
86 is specified by the first 2 bytes */
87 LDNS_RDF_TYPE_INT16_DATA,
88 /** protocol and port bitmaps */
89 LDNS_RDF_TYPE_SERVICE,
92 /** well known services */
99 LDNS_RDF_TYPE_IPSECKEY,
100 /** nsec3 hash salt */
101 LDNS_RDF_TYPE_NSEC3_SALT,
102 /** nsec3 base32 string (with length byte on wire */
103 LDNS_RDF_TYPE_NSEC3_NEXT_OWNER
105 typedef enum ldns_enum_rdf_type ldns_rdf_type;
108 * algorithms used in CERT rrs
110 enum ldns_enum_cert_algorithm
118 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
123 * Resource record data field.
125 * The data is a network ordered array of bytes, which size is specified by
126 * the (16-bit) size field. To correctly parse it, use the type
127 * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
129 struct ldns_struct_rdf
131 /** The size of the data (in octets) */
133 /** The type of the data */
135 /** Pointer to the data (raw octets) */
138 typedef struct ldns_struct_rdf ldns_rdf;
142 /* write access functions */
145 * sets the size of the rdf.
146 * \param[in] *rd the rdf to operate on
147 * \param[in] size the new size
150 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
153 * sets the size of the rdf.
154 * \param[in] *rd the rdf to operate on
155 * \param[in] type the new type
158 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
161 * sets the size of the rdf.
162 * \param[in] *rd the rdf to operate on
163 * \param[in] *data pointer to the new data
166 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
171 * returns the size of the rdf.
172 * \param[in] *rd the rdf to read from
173 * \return uint16_t with the size
175 size_t ldns_rdf_size(const ldns_rdf *rd);
178 * returns the type of the rdf. We need to insert _get_
179 * here to prevent conflict the the rdf_type TYPE.
180 * \param[in] *rd the rdf to read from
181 * \return ldns_rdf_type with the type
183 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
186 * returns the data of the rdf.
187 * \param[in] *rd the rdf to read from
188 * \return uint8_t* pointer to the rdf's data
190 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
192 /* creator functions */
195 * allocates a new rdf structure and fills it.
196 * This function DOES NOT copy the contents from
197 * the buffer, unlinke ldns_rdf_new_frm_data()
198 * \param[in] type type of the rdf
199 * \param[in] size size of the buffer
200 * \param[in] data pointer to the buffer to be copied
201 * \return the new rdf structure or NULL on failure
203 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
206 * allocates a new rdf structure and fills it.
207 * This function _does_ copy the contents from
208 * the buffer, unlinke ldns_rdf_new()
209 * \param[in] type type of the rdf
210 * \param[in] size size of the buffer
211 * \param[in] data pointer to the buffer to be copied
212 * \return the new rdf structure or NULL on failure
214 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
217 * creates a new rdf from a string.
218 * \param[in] type type to use
219 * \param[in] str string to use
220 * \return ldns_rdf* or NULL in case of an error
222 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
225 * creates a new rdf from a file containing a string.
226 * \param[out] r the new rdf
227 * \param[in] type type to use
228 * \param[in] fp the file pointer to use
229 * \return LDNS_STATUS_OK or the error
231 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
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 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
239 * \return LDNS_STATUS_OK or the error
241 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
243 /* destroy functions */
246 * frees a rdf structure, leaving the
247 * data pointer intact.
248 * \param[in] rd the pointer to be freed
251 void ldns_rdf_free(ldns_rdf *rd);
254 * frees a rdf structure _and_ frees the
255 * data. rdf should be created with _new_frm_data
256 * \param[in] rd the rdf structure to be freed
259 void ldns_rdf_deep_free(ldns_rdf *rd);
261 /* conversion functions */
264 * returns the rdf containing the native uint8_t repr.
265 * \param[in] type the ldns_rdf type to use
266 * \param[in] value the uint8_t to use
267 * \return ldns_rdf* with the converted value
269 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
272 * returns the rdf containing the native uint16_t representation.
273 * \param[in] type the ldns_rdf type to use
274 * \param[in] value the uint16_t to use
275 * \return ldns_rdf* with the converted value
277 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
280 * returns an rdf that contains the given int32 value.
282 * Because multiple rdf types can contain an int32, the
283 * type must be specified
284 * \param[in] type the ldns_rdf type to use
285 * \param[in] value the uint32_t to use
286 * \return ldns_rdf* with the converted value
288 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
291 * returns an int16_data rdf that contains the data in the
292 * given array, preceded by an int16 specifying the length.
294 * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
295 * \param[in] size the size of the data
296 * \param[in] *data pointer to the actual data
297 * \return ldns_rd* the rdf with the data
299 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
302 * reverses an rdf, only actually useful for AAAA and A records.
303 * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
304 * \param[in] *rd rdf to be reversed
305 * \return the reversed rdf (a newly created rdf)
307 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
310 * returns the native uint8_t representation from the rdf.
311 * \param[in] rd the ldns_rdf to operate on
312 * \return uint8_t the value extracted
314 uint8_t ldns_rdf2native_int8(const ldns_rdf *rd);
317 * returns the native uint16_t representation from the rdf.
318 * \param[in] rd the ldns_rdf to operate on
319 * \return uint16_t the value extracted
321 uint16_t ldns_rdf2native_int16(const ldns_rdf *rd);
324 * returns the native uint32_t representation from the rdf.
325 * \param[in] rd the ldns_rdf to operate on
326 * \return uint32_t the value extracted
328 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
331 * returns the native time_t representation from the rdf.
332 * \param[in] rd the ldns_rdf to operate on
333 * \return time_t the value extracted (32 bits currently)
335 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
338 * converts a ttl value (like 5d2h) to a long.
339 * \param[in] nptr the start of the string
340 * \param[out] endptr points to the last char in case of error
341 * \return the convert duration value
343 uint32_t ldns_str2period(const char *nptr, const char **endptr);
346 * removes \\DDD, \\[space] and other escapes from the input.
347 * See RFC 1035, section 5.1.
348 * \param[in] word what to check
349 * \param[in] length the string
350 * \return ldns_status mesg
352 ldns_status ldns_octet(char *word, size_t *length);
355 * clones a rdf structure. The data is copied.
356 * \param[in] rd rdf to be copied
357 * \return a new rdf structure
359 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
362 * compares two rdf's on their wire formats.
363 * (To order dnames according to rfc4034, use ldns_dname_compare)
364 * \param[in] rd1 the first one
365 * \param[in] rd2 the second one
367 * \return -1 if rd1 comes before rd2
368 * \return +1 if rd2 comes before rd1
370 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
372 #endif /* LDNS_RDATA_H */