xref: /dragonfly/contrib/ldns/ldns/rdata.h (revision 81c11cd3)
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	8192
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_16BYTES           16
38 
39 #define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
40 
41 /**
42  * The different types of RDATA fields.
43  */
44 enum ldns_enum_rdf_type
45 {
46 	/** none */
47 	LDNS_RDF_TYPE_NONE,
48 	/** domain name */
49 	LDNS_RDF_TYPE_DNAME,
50 	/** 8 bits */
51 	LDNS_RDF_TYPE_INT8,
52 	/** 16 bits */
53 	LDNS_RDF_TYPE_INT16,
54 	/** 32 bits */
55 	LDNS_RDF_TYPE_INT32,
56 	/** A record */
57 	LDNS_RDF_TYPE_A,
58 	/** AAAA record */
59 	LDNS_RDF_TYPE_AAAA,
60 	/** txt string */
61 	LDNS_RDF_TYPE_STR,
62 	/** apl data */
63 	LDNS_RDF_TYPE_APL,
64 	/** b32 string */
65 	LDNS_RDF_TYPE_B32_EXT,
66 	/** b64 string */
67 	LDNS_RDF_TYPE_B64,
68 	/** hex string */
69 	LDNS_RDF_TYPE_HEX,
70 	/** nsec type codes */
71 	LDNS_RDF_TYPE_NSEC,
72 	/** a RR type */
73 	LDNS_RDF_TYPE_TYPE,
74 	/** a class */
75 	LDNS_RDF_TYPE_CLASS,
76 	/** certificate algorithm */
77 	LDNS_RDF_TYPE_CERT_ALG,
78 	/** a key algorithm */
79 	LDNS_RDF_TYPE_ALG,
80 	/** unknown types */
81 	LDNS_RDF_TYPE_UNKNOWN,
82 	/** time (32 bits) */
83 	LDNS_RDF_TYPE_TIME,
84 	/** period */
85 	LDNS_RDF_TYPE_PERIOD,
86 	/** tsig time 48 bits */
87 	LDNS_RDF_TYPE_TSIGTIME,
88 	LDNS_RDF_TYPE_TSIG,
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,
94 	/** location data */
95 	LDNS_RDF_TYPE_LOC,
96 	/** well known services */
97 	LDNS_RDF_TYPE_WKS,
98 	/** NSAP */
99 	LDNS_RDF_TYPE_NSAP,
100 	/** ATMA */
101 	LDNS_RDF_TYPE_ATMA,
102 	/** IPSECKEY */
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
108 };
109 typedef enum ldns_enum_rdf_type ldns_rdf_type;
110 
111 /**
112  * algorithms used in CERT rrs
113  */
114 enum ldns_enum_cert_algorithm
115 {
116 	LDNS_CERT_PKIX		= 1,
117 	LDNS_CERT_SPKI		= 2,
118 	LDNS_CERT_PGP		= 3,
119 	LDNS_CERT_URI		= 253,
120 	LDNS_CERT_OID		= 254
121 };
122 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
123 
124 
125 
126 /**
127  * Resource record data field.
128  *
129  * The data is a network ordered array of bytes, which size is specified by
130  * the (16-bit) size field. To correctly parse it, use the type
131  * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
132  */
133 struct ldns_struct_rdf
134 {
135 	/** The size of the data (in octets) */
136 	size_t _size;
137 	/** The type of the data */
138 	ldns_rdf_type _type;
139 	/** Pointer to the data (raw octets) */
140 	void  *_data;
141 };
142 typedef struct ldns_struct_rdf ldns_rdf;
143 
144 /* prototypes */
145 
146 /* write access functions */
147 
148 /**
149  * sets the size of the rdf.
150  * \param[in] *rd the rdf to operate on
151  * \param[in] size the new size
152  * \return void
153  */
154 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
155 
156 /**
157  * sets the size of the rdf.
158  * \param[in] *rd the rdf to operate on
159  * \param[in] type the new type
160  * \return void
161  */
162 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
163 
164 /**
165  * sets the size of the rdf.
166  * \param[in] *rd the rdf to operate on
167  * \param[in] *data pointer to the new data
168  * \return void
169  */
170 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
171 
172 /* read access */
173 
174 /**
175  * returns the size of the rdf.
176  * \param[in] *rd the rdf to read from
177  * \return uint16_t with the size
178  */
179 size_t ldns_rdf_size(const ldns_rdf *rd);
180 
181 /**
182  * returns the type of the rdf. We need to insert _get_
183  * here to prevent conflict the the rdf_type TYPE.
184  * \param[in] *rd the rdf to read from
185  * \return ldns_rdf_type with the type
186  */
187 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
188 
189 /**
190  * returns the data of the rdf.
191  * \param[in] *rd the rdf to read from
192  * \return uint8_t* pointer to the rdf's data
193  */
194 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
195 
196 /* creator functions */
197 
198 /**
199  * allocates a new rdf structure and fills it.
200  * This function DOES NOT copy the contents from
201  * the buffer, unlinke ldns_rdf_new_frm_data()
202  * \param[in] type type of the rdf
203  * \param[in] size size of the buffer
204  * \param[in] data pointer to the buffer to be copied
205  * \return the new rdf structure or NULL on failure
206  */
207 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
208 
209 /**
210  * allocates a new rdf structure and fills it.
211  * This function _does_ copy the contents from
212  * the buffer, unlinke ldns_rdf_new()
213  * \param[in] type type of the rdf
214  * \param[in] size size of the buffer
215  * \param[in] data pointer to the buffer to be copied
216  * \return the new rdf structure or NULL on failure
217  */
218 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
219 
220 /**
221  * creates a new rdf from a string.
222  * \param[in] type   type to use
223  * \param[in] str string to use
224  * \return ldns_rdf* or NULL in case of an error
225  */
226 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
227 
228 /**
229  * creates a new rdf from a file containing a string.
230  * \param[out] r the new rdf
231  * \param[in] type   type to use
232  * \param[in] fp the file pointer  to use
233  * \return LDNS_STATUS_OK or the error
234  */
235 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
236 
237 /**
238  * creates a new rdf from a file containing a string.
239  * \param[out] r the new rdf
240  * \param[in] type   type to use
241  * \param[in] fp the file pointer  to use
242  * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
243  * \return LDNS_STATUS_OK or the error
244  */
245 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
246 
247 /* destroy functions */
248 
249 /**
250  * frees a rdf structure, leaving the
251  * data pointer intact.
252  * \param[in] rd the pointer to be freed
253  * \return void
254  */
255 void ldns_rdf_free(ldns_rdf *rd);
256 
257 /**
258  * frees a rdf structure _and_ frees the
259  * data. rdf should be created with _new_frm_data
260  * \param[in] rd the rdf structure to be freed
261  * \return void
262  */
263 void ldns_rdf_deep_free(ldns_rdf *rd);
264 
265 /* conversion functions */
266 
267 /**
268  * returns the rdf containing the native uint8_t repr.
269  * \param[in] type the ldns_rdf type to use
270  * \param[in] value the uint8_t to use
271  * \return ldns_rdf* with the converted value
272  */
273 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
274 
275 /**
276  * returns the rdf containing the native uint16_t representation.
277  * \param[in] type the ldns_rdf type to use
278  * \param[in] value the uint16_t to use
279  * \return ldns_rdf* with the converted value
280  */
281 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
282 
283 /**
284  * returns an rdf that contains the given int32 value.
285  *
286  * Because multiple rdf types can contain an int32, the
287  * type must be specified
288  * \param[in] type the ldns_rdf type to use
289  * \param[in] value the uint32_t to use
290  * \return ldns_rdf* with the converted value
291  */
292 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
293 
294 /**
295  * returns an int16_data rdf that contains the data in the
296  * given array, preceded by an int16 specifying the length.
297  *
298  * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
299  * \param[in] size the size of the data
300  * \param[in] *data pointer to the actual data
301  * \return ldns_rd* the rdf with the data
302  */
303 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
304 
305 /**
306  * reverses an rdf, only actually useful for AAAA and A records.
307  * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
308  * \param[in] *rd rdf to be reversed
309  * \return the reversed rdf (a newly created rdf)
310  */
311 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
312 
313 /**
314  * returns the native uint8_t representation from the rdf.
315  * \param[in] rd the ldns_rdf to operate on
316  * \return uint8_t the value extracted
317  */
318 uint8_t 	ldns_rdf2native_int8(const ldns_rdf *rd);
319 
320 /**
321  * returns the native uint16_t representation from the rdf.
322  * \param[in] rd the ldns_rdf to operate on
323  * \return uint16_t the value extracted
324  */
325 uint16_t	ldns_rdf2native_int16(const ldns_rdf *rd);
326 
327 /**
328  * returns the native uint32_t representation from the rdf.
329  * \param[in] rd the ldns_rdf to operate on
330  * \return uint32_t the value extracted
331  */
332 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
333 
334 /**
335  * returns the native time_t representation from the rdf.
336  * \param[in] rd the ldns_rdf to operate on
337  * \return time_t the value extracted (32 bits currently)
338  */
339 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
340 
341 /**
342  * converts a ttl value (like 5d2h) to a long.
343  * \param[in] nptr the start of the string
344  * \param[out] endptr points to the last char in case of error
345  * \return the convert duration value
346  */
347 uint32_t ldns_str2period(const char *nptr, const char **endptr);
348 
349 /**
350  * removes \\DDD, \\[space] and other escapes from the input.
351  * See RFC 1035, section 5.1.
352  * \param[in] word what to check
353  * \param[in] length the string
354  * \return ldns_status mesg
355  */
356 ldns_status ldns_octet(char *word, size_t *length);
357 
358 /**
359  * clones a rdf structure. The data is copied.
360  * \param[in] rd rdf to be copied
361  * \return a new rdf structure
362  */
363 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
364 
365 /**
366  * compares two rdf's on their wire formats.
367  * (To order dnames according to rfc4034, use ldns_dname_compare)
368  * \param[in] rd1 the first one
369  * \param[in] rd2 the second one
370  * \return 0 if equal
371  * \return -1 if rd1 comes before rd2
372  * \return +1 if rd2 comes before rd1
373  */
374 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
375 
376 #ifdef __cplusplus
377 }
378 #endif
379 
380 #endif	/* LDNS_RDATA_H */
381