1238104Sdes/*
2238104Sdes * rdata.h
3238104Sdes *
4238104Sdes * rdata definitions
5238104Sdes *
6238104Sdes * a Net::DNS like library for C
7238104Sdes *
8238104Sdes * (c) NLnet Labs, 2005-2006
9238104Sdes *
10238104Sdes * See the file LICENSE for the license
11238104Sdes */
12238104Sdes
13238104Sdes
14238104Sdes/**
15238104Sdes * \file
16238104Sdes *
17238104Sdes * Defines ldns_rdf and functions to manipulate those.
18238104Sdes */
19238104Sdes
20238104Sdes
21238104Sdes#ifndef LDNS_RDATA_H
22238104Sdes#define LDNS_RDATA_H
23238104Sdes
24238104Sdes#include <ldns/common.h>
25238104Sdes#include <ldns/error.h>
26238104Sdes
27238104Sdes#ifdef __cplusplus
28238104Sdesextern "C" {
29238104Sdes#endif
30238104Sdes
31238104Sdes#define LDNS_MAX_RDFLEN	8192
32238104Sdes
33238104Sdes#define LDNS_RDF_SIZE_BYTE              1
34238104Sdes#define LDNS_RDF_SIZE_WORD              2
35238104Sdes#define LDNS_RDF_SIZE_DOUBLEWORD        4
36238104Sdes#define LDNS_RDF_SIZE_6BYTES            6
37238104Sdes#define LDNS_RDF_SIZE_16BYTES           16
38238104Sdes
39238104Sdes#define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
40238104Sdes
41238104Sdes/**
42238104Sdes * The different types of RDATA fields.
43238104Sdes */
44238104Sdesenum ldns_enum_rdf_type
45238104Sdes{
46238104Sdes	/** none */
47238104Sdes	LDNS_RDF_TYPE_NONE,
48238104Sdes	/** domain name */
49238104Sdes	LDNS_RDF_TYPE_DNAME,
50238104Sdes	/** 8 bits */
51238104Sdes	LDNS_RDF_TYPE_INT8,
52238104Sdes	/** 16 bits */
53238104Sdes	LDNS_RDF_TYPE_INT16,
54238104Sdes	/** 32 bits */
55238104Sdes	LDNS_RDF_TYPE_INT32,
56238104Sdes	/** A record */
57238104Sdes	LDNS_RDF_TYPE_A,
58238104Sdes	/** AAAA record */
59238104Sdes	LDNS_RDF_TYPE_AAAA,
60238104Sdes	/** txt string */
61238104Sdes	LDNS_RDF_TYPE_STR,
62238104Sdes	/** apl data */
63238104Sdes	LDNS_RDF_TYPE_APL,
64238104Sdes	/** b32 string */
65238104Sdes	LDNS_RDF_TYPE_B32_EXT,
66238104Sdes	/** b64 string */
67238104Sdes	LDNS_RDF_TYPE_B64,
68238104Sdes	/** hex string */
69238104Sdes	LDNS_RDF_TYPE_HEX,
70238104Sdes	/** nsec type codes */
71238104Sdes	LDNS_RDF_TYPE_NSEC,
72238104Sdes	/** a RR type */
73238104Sdes	LDNS_RDF_TYPE_TYPE,
74238104Sdes	/** a class */
75238104Sdes	LDNS_RDF_TYPE_CLASS,
76238104Sdes	/** certificate algorithm */
77238104Sdes	LDNS_RDF_TYPE_CERT_ALG,
78238104Sdes	/** a key algorithm */
79238104Sdes	LDNS_RDF_TYPE_ALG,
80238104Sdes	/** unknown types */
81238104Sdes	LDNS_RDF_TYPE_UNKNOWN,
82238104Sdes	/** time (32 bits) */
83238104Sdes	LDNS_RDF_TYPE_TIME,
84238104Sdes	/** period */
85238104Sdes	LDNS_RDF_TYPE_PERIOD,
86238104Sdes	/** tsig time 48 bits */
87238104Sdes	LDNS_RDF_TYPE_TSIGTIME,
88238104Sdes	LDNS_RDF_TYPE_TSIG,
89238104Sdes	/** variable length any type rdata where the length
90238104Sdes	    is specified by the first 2 bytes */
91238104Sdes	LDNS_RDF_TYPE_INT16_DATA,
92238104Sdes	/** protocol and port bitmaps */
93238104Sdes	LDNS_RDF_TYPE_SERVICE,
94238104Sdes	/** location data */
95238104Sdes	LDNS_RDF_TYPE_LOC,
96238104Sdes	/** well known services */
97238104Sdes	LDNS_RDF_TYPE_WKS,
98238104Sdes	/** NSAP */
99238104Sdes	LDNS_RDF_TYPE_NSAP,
100238104Sdes	/** ATMA */
101238104Sdes	LDNS_RDF_TYPE_ATMA,
102238104Sdes	/** IPSECKEY */
103238104Sdes	LDNS_RDF_TYPE_IPSECKEY,
104238104Sdes	/** nsec3 hash salt */
105238104Sdes	LDNS_RDF_TYPE_NSEC3_SALT,
106238104Sdes	/** nsec3 base32 string (with length byte on wire */
107238104Sdes	LDNS_RDF_TYPE_NSEC3_NEXT_OWNER
108238104Sdes};
109238104Sdestypedef enum ldns_enum_rdf_type ldns_rdf_type;
110238104Sdes
111238104Sdes/**
112238104Sdes * algorithms used in CERT rrs
113238104Sdes */
114238104Sdesenum ldns_enum_cert_algorithm
115238104Sdes{
116238104Sdes        LDNS_CERT_PKIX		= 1,
117238104Sdes        LDNS_CERT_SPKI		= 2,
118238104Sdes        LDNS_CERT_PGP		= 3,
119238104Sdes        LDNS_CERT_IPKIX         = 4,
120238104Sdes        LDNS_CERT_ISPKI         = 5,
121238104Sdes        LDNS_CERT_IPGP          = 6,
122238104Sdes        LDNS_CERT_ACPKIX        = 7,
123238104Sdes        LDNS_CERT_IACPKIX       = 8,
124238104Sdes        LDNS_CERT_URI		= 253,
125238104Sdes        LDNS_CERT_OID		= 254
126238104Sdes};
127238104Sdestypedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
128238104Sdes
129238104Sdes
130238104Sdes
131238104Sdes/**
132238104Sdes * Resource record data field.
133238104Sdes *
134238104Sdes * The data is a network ordered array of bytes, which size is specified by
135238104Sdes * the (16-bit) size field. To correctly parse it, use the type
136238104Sdes * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
137238104Sdes */
138238104Sdesstruct ldns_struct_rdf
139238104Sdes{
140238104Sdes	/** The size of the data (in octets) */
141238104Sdes	size_t _size;
142238104Sdes	/** The type of the data */
143238104Sdes	ldns_rdf_type _type;
144238104Sdes	/** Pointer to the data (raw octets) */
145238104Sdes	void  *_data;
146238104Sdes};
147238104Sdestypedef struct ldns_struct_rdf ldns_rdf;
148238104Sdes
149238104Sdes/* prototypes */
150238104Sdes
151238104Sdes/* write access functions */
152238104Sdes
153238104Sdes/**
154238104Sdes * sets the size of the rdf.
155238104Sdes * \param[in] *rd the rdf to operate on
156238104Sdes * \param[in] size the new size
157238104Sdes * \return void
158238104Sdes */
159238104Sdesvoid ldns_rdf_set_size(ldns_rdf *rd, size_t size);
160238104Sdes
161238104Sdes/**
162238104Sdes * sets the size of the rdf.
163238104Sdes * \param[in] *rd the rdf to operate on
164238104Sdes * \param[in] type the new type
165238104Sdes * \return void
166238104Sdes */
167238104Sdesvoid ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
168238104Sdes
169238104Sdes/**
170238104Sdes * sets the size of the rdf.
171238104Sdes * \param[in] *rd the rdf to operate on
172238104Sdes * \param[in] *data pointer to the new data
173238104Sdes * \return void
174238104Sdes */
175238104Sdesvoid ldns_rdf_set_data(ldns_rdf *rd, void *data);
176238104Sdes
177238104Sdes/* read access */
178238104Sdes
179238104Sdes/**
180238104Sdes * returns the size of the rdf.
181238104Sdes * \param[in] *rd the rdf to read from
182238104Sdes * \return uint16_t with the size
183238104Sdes */
184238104Sdessize_t ldns_rdf_size(const ldns_rdf *rd);
185238104Sdes
186238104Sdes/**
187238104Sdes * returns the type of the rdf. We need to insert _get_
188238104Sdes * here to prevent conflict the the rdf_type TYPE.
189238104Sdes * \param[in] *rd the rdf to read from
190238104Sdes * \return ldns_rdf_type with the type
191238104Sdes */
192238104Sdesldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
193238104Sdes
194238104Sdes/**
195238104Sdes * returns the data of the rdf.
196238104Sdes * \param[in] *rd the rdf to read from
197246854Sdes *
198238104Sdes * \return uint8_t* pointer to the rdf's data
199238104Sdes */
200238104Sdesuint8_t *ldns_rdf_data(const ldns_rdf *rd);
201238104Sdes
202238104Sdes/* creator functions */
203238104Sdes
204238104Sdes/**
205238104Sdes * allocates a new rdf structure and fills it.
206238104Sdes * This function DOES NOT copy the contents from
207238104Sdes * the buffer, unlinke ldns_rdf_new_frm_data()
208238104Sdes * \param[in] type type of the rdf
209238104Sdes * \param[in] size size of the buffer
210238104Sdes * \param[in] data pointer to the buffer to be copied
211238104Sdes * \return the new rdf structure or NULL on failure
212238104Sdes */
213238104Sdesldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
214238104Sdes
215238104Sdes/**
216238104Sdes * allocates a new rdf structure and fills it.
217238104Sdes * This function _does_ copy the contents from
218238104Sdes * the buffer, unlinke ldns_rdf_new()
219238104Sdes * \param[in] type type of the rdf
220238104Sdes * \param[in] size size of the buffer
221238104Sdes * \param[in] data pointer to the buffer to be copied
222238104Sdes * \return the new rdf structure or NULL on failure
223238104Sdes */
224238104Sdesldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
225238104Sdes
226238104Sdes/**
227238104Sdes * creates a new rdf from a string.
228238104Sdes * \param[in] type   type to use
229238104Sdes * \param[in] str string to use
230238104Sdes * \return ldns_rdf* or NULL in case of an error
231238104Sdes */
232238104Sdesldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
233238104Sdes
234238104Sdes/**
235238104Sdes * creates a new rdf from a file containing a string.
236238104Sdes * \param[out] r the new rdf
237238104Sdes * \param[in] type   type to use
238238104Sdes * \param[in] fp the file pointer  to use
239238104Sdes * \return LDNS_STATUS_OK or the error
240238104Sdes */
241238104Sdesldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
242238104Sdes
243238104Sdes/**
244238104Sdes * creates a new rdf from a file containing a string.
245238104Sdes * \param[out] r the new rdf
246238104Sdes * \param[in] type   type to use
247238104Sdes * \param[in] fp the file pointer  to use
248238104Sdes * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
249238104Sdes * \return LDNS_STATUS_OK or the error
250238104Sdes */
251238104Sdesldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
252238104Sdes
253238104Sdes/* destroy functions */
254238104Sdes
255238104Sdes/**
256238104Sdes * frees a rdf structure, leaving the
257238104Sdes * data pointer intact.
258238104Sdes * \param[in] rd the pointer to be freed
259238104Sdes * \return void
260238104Sdes */
261238104Sdesvoid ldns_rdf_free(ldns_rdf *rd);
262238104Sdes
263238104Sdes/**
264238104Sdes * frees a rdf structure _and_ frees the
265238104Sdes * data. rdf should be created with _new_frm_data
266238104Sdes * \param[in] rd the rdf structure to be freed
267238104Sdes * \return void
268238104Sdes */
269238104Sdesvoid ldns_rdf_deep_free(ldns_rdf *rd);
270238104Sdes
271238104Sdes/* conversion functions */
272238104Sdes
273238104Sdes/**
274238104Sdes * returns the rdf containing the native uint8_t repr.
275238104Sdes * \param[in] type the ldns_rdf type to use
276238104Sdes * \param[in] value the uint8_t to use
277238104Sdes * \return ldns_rdf* with the converted value
278238104Sdes */
279238104Sdesldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
280238104Sdes
281238104Sdes/**
282238104Sdes * returns the rdf containing the native uint16_t representation.
283238104Sdes * \param[in] type the ldns_rdf type to use
284238104Sdes * \param[in] value the uint16_t to use
285238104Sdes * \return ldns_rdf* with the converted value
286238104Sdes */
287238104Sdesldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
288238104Sdes
289238104Sdes/**
290238104Sdes * returns an rdf that contains the given int32 value.
291238104Sdes *
292238104Sdes * Because multiple rdf types can contain an int32, the
293238104Sdes * type must be specified
294238104Sdes * \param[in] type the ldns_rdf type to use
295238104Sdes * \param[in] value the uint32_t to use
296238104Sdes * \return ldns_rdf* with the converted value
297238104Sdes */
298238104Sdesldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
299238104Sdes
300238104Sdes/**
301238104Sdes * returns an int16_data rdf that contains the data in the
302238104Sdes * given array, preceded by an int16 specifying the length.
303238104Sdes *
304238104Sdes * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
305238104Sdes * \param[in] size the size of the data
306238104Sdes * \param[in] *data pointer to the actual data
307246854Sdes *
308238104Sdes * \return ldns_rd* the rdf with the data
309238104Sdes */
310238104Sdesldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
311238104Sdes
312238104Sdes/**
313238104Sdes * reverses an rdf, only actually useful for AAAA and A records.
314238104Sdes * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
315238104Sdes * \param[in] *rd rdf to be reversed
316238104Sdes * \return the reversed rdf (a newly created rdf)
317238104Sdes */
318238104Sdesldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
319238104Sdes
320238104Sdes/**
321238104Sdes * returns the native uint8_t representation from the rdf.
322238104Sdes * \param[in] rd the ldns_rdf to operate on
323238104Sdes * \return uint8_t the value extracted
324238104Sdes */
325238104Sdesuint8_t 	ldns_rdf2native_int8(const ldns_rdf *rd);
326238104Sdes
327238104Sdes/**
328238104Sdes * returns the native uint16_t representation from the rdf.
329238104Sdes * \param[in] rd the ldns_rdf to operate on
330238104Sdes * \return uint16_t the value extracted
331238104Sdes */
332238104Sdesuint16_t	ldns_rdf2native_int16(const ldns_rdf *rd);
333238104Sdes
334238104Sdes/**
335238104Sdes * returns the native uint32_t representation from the rdf.
336238104Sdes * \param[in] rd the ldns_rdf to operate on
337238104Sdes * \return uint32_t the value extracted
338238104Sdes */
339238104Sdesuint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
340238104Sdes
341238104Sdes/**
342238104Sdes * returns the native time_t representation from the rdf.
343238104Sdes * \param[in] rd the ldns_rdf to operate on
344238104Sdes * \return time_t the value extracted (32 bits currently)
345238104Sdes */
346238104Sdestime_t ldns_rdf2native_time_t(const ldns_rdf *rd);
347238104Sdes
348238104Sdes/**
349238104Sdes * converts a ttl value (like 5d2h) to a long.
350238104Sdes * \param[in] nptr the start of the string
351238104Sdes * \param[out] endptr points to the last char in case of error
352238104Sdes * \return the convert duration value
353238104Sdes */
354238104Sdesuint32_t ldns_str2period(const char *nptr, const char **endptr);
355238104Sdes
356238104Sdes/**
357238104Sdes * removes \\DDD, \\[space] and other escapes from the input.
358238104Sdes * See RFC 1035, section 5.1.
359238104Sdes * \param[in] word what to check
360238104Sdes * \param[in] length the string
361238104Sdes * \return ldns_status mesg
362238104Sdes */
363238104Sdesldns_status ldns_octet(char *word, size_t *length);
364238104Sdes
365238104Sdes/**
366238104Sdes * clones a rdf structure. The data is copied.
367238104Sdes * \param[in] rd rdf to be copied
368238104Sdes * \return a new rdf structure
369238104Sdes */
370238104Sdesldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
371238104Sdes
372238104Sdes/**
373238104Sdes * compares two rdf's on their wire formats.
374238104Sdes * (To order dnames according to rfc4034, use ldns_dname_compare)
375238104Sdes * \param[in] rd1 the first one
376238104Sdes * \param[in] rd2 the second one
377238104Sdes * \return 0 if equal
378238104Sdes * \return -1 if rd1 comes before rd2
379238104Sdes * \return +1 if rd2 comes before rd1
380238104Sdes */
381238104Sdesint ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
382238104Sdes
383238104Sdes#ifdef __cplusplus
384238104Sdes}
385238104Sdes#endif
386238104Sdes
387238104Sdes#endif	/* LDNS_RDATA_H */
388