dc90270a187d6e9816c2117c1f9ff3f0869daa41
[dragonfly.git] / contrib / ldns / ldns / rdata.h
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 #define LDNS_MAX_RDFLEN 8192
28
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
34
35 #define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
36
37 /**
38  * The different types of RDATA fields.
39  */
40 enum ldns_enum_rdf_type
41 {
42         /** none */
43         LDNS_RDF_TYPE_NONE,
44         /** domain name */
45         LDNS_RDF_TYPE_DNAME,
46         /** 8 bits */
47         LDNS_RDF_TYPE_INT8,
48         /** 16 bits */
49         LDNS_RDF_TYPE_INT16,
50         /** 32 bits */
51         LDNS_RDF_TYPE_INT32,
52         /** A record */
53         LDNS_RDF_TYPE_A,
54         /** AAAA record */
55         LDNS_RDF_TYPE_AAAA,
56         /** txt string */
57         LDNS_RDF_TYPE_STR,
58         /** apl data */
59         LDNS_RDF_TYPE_APL,
60         /** b32 string */
61         LDNS_RDF_TYPE_B32_EXT,
62         /** b64 string */
63         LDNS_RDF_TYPE_B64,
64         /** hex string */
65         LDNS_RDF_TYPE_HEX,
66         /** nsec type codes */
67         LDNS_RDF_TYPE_NSEC,
68         /** a RR type */
69         LDNS_RDF_TYPE_TYPE,
70         /** a class */
71         LDNS_RDF_TYPE_CLASS,
72         /** certificate algorithm */
73         LDNS_RDF_TYPE_CERT_ALG,
74         /** a key algorithm */
75         LDNS_RDF_TYPE_ALG,
76         /** unknown types */
77         LDNS_RDF_TYPE_UNKNOWN,
78         /** time (32 bits) */
79         LDNS_RDF_TYPE_TIME,
80         /** period */
81         LDNS_RDF_TYPE_PERIOD,
82         /** tsig time 48 bits */
83         LDNS_RDF_TYPE_TSIGTIME,
84         LDNS_RDF_TYPE_TSIG,
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,
90         /** location data */
91         LDNS_RDF_TYPE_LOC,
92         /** well known services */
93         LDNS_RDF_TYPE_WKS,
94         /** NSAP */
95         LDNS_RDF_TYPE_NSAP,
96         /** ATMA */
97         LDNS_RDF_TYPE_ATMA,
98         /** IPSECKEY */
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
104 };
105 typedef enum ldns_enum_rdf_type ldns_rdf_type;
106
107 /**
108  * algorithms used in CERT rrs
109  */
110 enum ldns_enum_cert_algorithm
111 {
112         LDNS_CERT_PKIX          = 1,
113         LDNS_CERT_SPKI          = 2,
114         LDNS_CERT_PGP           = 3,
115         LDNS_CERT_URI           = 253,
116         LDNS_CERT_OID           = 254
117 };
118 typedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
119
120
121
122 /**
123  * Resource record data field.
124  *
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.
128  */
129 struct ldns_struct_rdf
130 {
131         /** The size of the data (in octets) */
132         size_t _size;
133         /** The type of the data */
134         ldns_rdf_type _type;
135         /** Pointer to the data (raw octets) */
136         void  *_data;
137 };
138 typedef struct ldns_struct_rdf ldns_rdf;
139
140 /* prototypes */
141
142 /* write access functions */
143
144 /**
145  * sets the size of the rdf.
146  * \param[in] *rd the rdf to operate on
147  * \param[in] size the new size
148  * \return void
149  */
150 void ldns_rdf_set_size(ldns_rdf *rd, size_t size);
151
152 /**
153  * sets the size of the rdf.
154  * \param[in] *rd the rdf to operate on
155  * \param[in] type the new type
156  * \return void
157  */
158 void ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
159
160 /**
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
164  * \return void
165  */
166 void ldns_rdf_set_data(ldns_rdf *rd, void *data);
167
168 /* read access */
169
170 /**
171  * returns the size of the rdf.
172  * \param[in] *rd the rdf to read from
173  * \return uint16_t with the size
174  */
175 size_t ldns_rdf_size(const ldns_rdf *rd);
176
177 /**
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
182  */
183 ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
184
185 /**
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
189  */
190 uint8_t *ldns_rdf_data(const ldns_rdf *rd);
191
192 /* creator functions */
193
194 /**
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
202  */
203 ldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
204
205 /**
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
213  */
214 ldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
215
216 /**
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
221  */
222 ldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
223
224 /**
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
230  */
231 ldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
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  * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
239  * \return LDNS_STATUS_OK or the error
240  */
241 ldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
242
243 /* destroy functions */
244
245 /**
246  * frees a rdf structure, leaving the
247  * data pointer intact.
248  * \param[in] rd the pointer to be freed
249  * \return void
250  */
251 void ldns_rdf_free(ldns_rdf *rd);
252
253 /**
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
257  * \return void
258  */
259 void ldns_rdf_deep_free(ldns_rdf *rd);
260
261 /* conversion functions */
262
263 /**
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
268  */
269 ldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
270
271 /**
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
276  */
277 ldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
278
279 /**
280  * returns an rdf that contains the given int32 value.
281  *
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
287  */
288 ldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
289
290 /**
291  * returns an int16_data rdf that contains the data in the
292  * given array, preceded by an int16 specifying the length.
293  *
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
298  */
299 ldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
300
301 /**
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)
306  */
307 ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
308
309 /**
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
313  */
314 uint8_t         ldns_rdf2native_int8(const ldns_rdf *rd);
315
316 /**
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
320  */
321 uint16_t        ldns_rdf2native_int16(const ldns_rdf *rd);
322
323 /**
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
327  */
328 uint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
329
330 /**
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)
334  */
335 time_t ldns_rdf2native_time_t(const ldns_rdf *rd);
336
337 /**
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
342  */
343 uint32_t ldns_str2period(const char *nptr, const char **endptr);
344
345 /**
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
351  */
352 ldns_status ldns_octet(char *word, size_t *length);
353
354 /**
355  * clones a rdf structure. The data is copied.
356  * \param[in] rd rdf to be copied
357  * \return a new rdf structure
358  */
359 ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
360
361 /**
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
366  * \return 0 if equal
367  * \return -1 if rd1 comes before rd2
368  * \return +1 if rd2 comes before rd1
369  */
370 int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
371
372 #endif  /* LDNS_RDATA_H */