xref: /dragonfly/contrib/ldns/ldns/rdata.h (revision 52f9f0d9)
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  * \return uint8_t* pointer to the rdf's data
198  */
199 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
200 
201 /* creator functions */
202 
203 /**
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
211  */
212 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
213 
214 /**
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
222  */
223 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
224 
225 /**
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
230  */
231 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
232 
233 /**
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
239  */
240 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
241 
242 /**
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
249  */
250 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
251 
252 /* destroy functions */
253 
254 /**
255  * frees a rdf structure, leaving the
256  * data pointer intact.
257  * \param[in] rd the pointer to be freed
258  * \return void
259  */
260 void ldns_rdf_free(ldns_rdf *rd);
261 
262 /**
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
266  * \return void
267  */
268 void ldns_rdf_deep_free(ldns_rdf *rd);
269 
270 /* conversion functions */
271 
272 /**
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
277  */
278 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
279 
280 /**
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
285  */
286 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
287 
288 /**
289  * returns an rdf that contains the given int32 value.
290  *
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
296  */
297 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
298 
299 /**
300  * returns an int16_data rdf that contains the data in the
301  * given array, preceded by an int16 specifying the length.
302  *
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
307  */
308 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
309 
310 /**
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)
315  */
316 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
317 
318 /**
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
322  */
323 uint8_t 	ldns_rdf2native_int8(const ldns_rdf *rd);
324 
325 /**
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
329  */
330 uint16_t	ldns_rdf2native_int16(const ldns_rdf *rd);
331 
332 /**
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
336  */
337 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
338 
339 /**
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)
343  */
344 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
345 
346 /**
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
351  */
352 uint32_t ldns_str2period(const char *nptr, const char **endptr);
353 
354 /**
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
360  */
361 ldns_status ldns_octet(char *word, size_t *length);
362 
363 /**
364  * clones a rdf structure. The data is copied.
365  * \param[in] rd rdf to be copied
366  * \return a new rdf structure
367  */
368 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
369 
370 /**
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
375  * \return 0 if equal
376  * \return -1 if rd1 comes before rd2
377  * \return +1 if rd2 comes before rd1
378  */
379 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
380 
381 #ifdef __cplusplus
382 }
383 #endif
384 
385 #endif	/* LDNS_RDATA_H */
386