xref: /dragonfly/contrib/ldns/ldns/rdata.h (revision d4ef6694)
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_IPKIX         = 4,
120         LDNS_CERT_ISPKI         = 5,
121         LDNS_CERT_IPGP          = 6,
122         LDNS_CERT_ACPKIX        = 7,
123         LDNS_CERT_IACPKIX       = 8,
124         LDNS_CERT_URI		= 253,
125         LDNS_CERT_OID		= 254
126 };
127 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
128 
129 
130 
131 /**
132  * Resource record data field.
133  *
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.
137  */
138 struct ldns_struct_rdf
139 {
140 	/** The size of the data (in octets) */
141 	size_t _size;
142 	/** The type of the data */
143 	ldns_rdf_type _type;
144 	/** Pointer to the data (raw octets) */
145 	void  *_data;
146 };
147 typedef struct ldns_struct_rdf ldns_rdf;
148 
149 /* prototypes */
150 
151 /* write access functions */
152 
153 /**
154  * sets the size of the rdf.
155  * \param[in] *rd the rdf to operate on
156  * \param[in] size the new size
157  * \return void
158  */
159 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
160 
161 /**
162  * sets the size of the rdf.
163  * \param[in] *rd the rdf to operate on
164  * \param[in] type the new type
165  * \return void
166  */
167 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
168 
169 /**
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
173  * \return void
174  */
175 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
176 
177 /* read access */
178 
179 /**
180  * returns the size of the rdf.
181  * \param[in] *rd the rdf to read from
182  * \return uint16_t with the size
183  */
184 size_t ldns_rdf_size(const ldns_rdf *rd);
185 
186 /**
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
191  */
192 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
193 
194 /**
195  * returns the data of the rdf.
196  * \param[in] *rd the rdf to read from
197  *
198  * \return uint8_t* pointer to the rdf's data
199  */
200 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
201 
202 /* creator functions */
203 
204 /**
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
212  */
213 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
214 
215 /**
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
223  */
224 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
225 
226 /**
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
231  */
232 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
233 
234 /**
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
240  */
241 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
242 
243 /**
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
250  */
251 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
252 
253 /* destroy functions */
254 
255 /**
256  * frees a rdf structure, leaving the
257  * data pointer intact.
258  * \param[in] rd the pointer to be freed
259  * \return void
260  */
261 void ldns_rdf_free(ldns_rdf *rd);
262 
263 /**
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
267  * \return void
268  */
269 void ldns_rdf_deep_free(ldns_rdf *rd);
270 
271 /* conversion functions */
272 
273 /**
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
278  */
279 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
280 
281 /**
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
286  */
287 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
288 
289 /**
290  * returns an rdf that contains the given int32 value.
291  *
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
297  */
298 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
299 
300 /**
301  * returns an int16_data rdf that contains the data in the
302  * given array, preceded by an int16 specifying the length.
303  *
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
307  *
308  * \return ldns_rd* the rdf with the data
309  */
310 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
311 
312 /**
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)
317  */
318 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
319 
320 /**
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
324  */
325 uint8_t 	ldns_rdf2native_int8(const ldns_rdf *rd);
326 
327 /**
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
331  */
332 uint16_t	ldns_rdf2native_int16(const ldns_rdf *rd);
333 
334 /**
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
338  */
339 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
340 
341 /**
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)
345  */
346 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
347 
348 /**
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
353  */
354 uint32_t ldns_str2period(const char *nptr, const char **endptr);
355 
356 /**
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
362  */
363 ldns_status ldns_octet(char *word, size_t *length);
364 
365 /**
366  * clones a rdf structure. The data is copied.
367  * \param[in] rd rdf to be copied
368  * \return a new rdf structure
369  */
370 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
371 
372 /**
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
377  * \return 0 if equal
378  * \return -1 if rd1 comes before rd2
379  * \return +1 if rd2 comes before rd1
380  */
381 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
382 
383 #ifdef __cplusplus
384 }
385 #endif
386 
387 #endif	/* LDNS_RDATA_H */
388