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
31269257Sdes#define LDNS_MAX_RDFLEN	65535
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
37269257Sdes#define LDNS_RDF_SIZE_8BYTES            8
38238104Sdes#define LDNS_RDF_SIZE_16BYTES           16
39238104Sdes
40238104Sdes#define LDNS_NSEC3_VARS_OPTOUT_MASK 0x01
41238104Sdes
42238104Sdes/**
43238104Sdes * The different types of RDATA fields.
44238104Sdes */
45238104Sdesenum ldns_enum_rdf_type
46238104Sdes{
47238104Sdes	/** none */
48238104Sdes	LDNS_RDF_TYPE_NONE,
49238104Sdes	/** domain name */
50238104Sdes	LDNS_RDF_TYPE_DNAME,
51238104Sdes	/** 8 bits */
52238104Sdes	LDNS_RDF_TYPE_INT8,
53238104Sdes	/** 16 bits */
54238104Sdes	LDNS_RDF_TYPE_INT16,
55238104Sdes	/** 32 bits */
56238104Sdes	LDNS_RDF_TYPE_INT32,
57238104Sdes	/** A record */
58238104Sdes	LDNS_RDF_TYPE_A,
59238104Sdes	/** AAAA record */
60238104Sdes	LDNS_RDF_TYPE_AAAA,
61238104Sdes	/** txt string */
62238104Sdes	LDNS_RDF_TYPE_STR,
63238104Sdes	/** apl data */
64238104Sdes	LDNS_RDF_TYPE_APL,
65238104Sdes	/** b32 string */
66238104Sdes	LDNS_RDF_TYPE_B32_EXT,
67238104Sdes	/** b64 string */
68238104Sdes	LDNS_RDF_TYPE_B64,
69238104Sdes	/** hex string */
70238104Sdes	LDNS_RDF_TYPE_HEX,
71238104Sdes	/** nsec type codes */
72238104Sdes	LDNS_RDF_TYPE_NSEC,
73238104Sdes	/** a RR type */
74238104Sdes	LDNS_RDF_TYPE_TYPE,
75238104Sdes	/** a class */
76238104Sdes	LDNS_RDF_TYPE_CLASS,
77238104Sdes	/** certificate algorithm */
78238104Sdes	LDNS_RDF_TYPE_CERT_ALG,
79238104Sdes	/** a key algorithm */
80238104Sdes	LDNS_RDF_TYPE_ALG,
81238104Sdes	/** unknown types */
82238104Sdes	LDNS_RDF_TYPE_UNKNOWN,
83238104Sdes	/** time (32 bits) */
84238104Sdes	LDNS_RDF_TYPE_TIME,
85238104Sdes	/** period */
86238104Sdes	LDNS_RDF_TYPE_PERIOD,
87238104Sdes	/** tsig time 48 bits */
88238104Sdes	LDNS_RDF_TYPE_TSIGTIME,
89269257Sdes	/** Represents the Public Key Algorithm, HIT and Public Key fields
90269257Sdes	    for the HIP RR types.  A HIP specific rdf type is used because of
91269257Sdes	    the unusual layout in wireformat (see RFC 5205 Section 5) */
92269257Sdes	LDNS_RDF_TYPE_HIP,
93238104Sdes	/** variable length any type rdata where the length
94238104Sdes	    is specified by the first 2 bytes */
95238104Sdes	LDNS_RDF_TYPE_INT16_DATA,
96238104Sdes	/** protocol and port bitmaps */
97238104Sdes	LDNS_RDF_TYPE_SERVICE,
98238104Sdes	/** location data */
99238104Sdes	LDNS_RDF_TYPE_LOC,
100238104Sdes	/** well known services */
101238104Sdes	LDNS_RDF_TYPE_WKS,
102238104Sdes	/** NSAP */
103238104Sdes	LDNS_RDF_TYPE_NSAP,
104238104Sdes	/** ATMA */
105238104Sdes	LDNS_RDF_TYPE_ATMA,
106238104Sdes	/** IPSECKEY */
107238104Sdes	LDNS_RDF_TYPE_IPSECKEY,
108238104Sdes	/** nsec3 hash salt */
109238104Sdes	LDNS_RDF_TYPE_NSEC3_SALT,
110238104Sdes	/** nsec3 base32 string (with length byte on wire */
111269257Sdes	LDNS_RDF_TYPE_NSEC3_NEXT_OWNER,
112269257Sdes
113269257Sdes	/** 4 shorts represented as 4 * 16 bit hex numbers
114269257Sdes	 *  separated by colons. For NID and L64.
115269257Sdes	 */
116269257Sdes	LDNS_RDF_TYPE_ILNP64,
117269257Sdes
118269257Sdes	/** 6 * 8 bit hex numbers separated by dashes. For EUI48. */
119269257Sdes	LDNS_RDF_TYPE_EUI48,
120269257Sdes	/** 8 * 8 bit hex numbers separated by dashes. For EUI64. */
121269257Sdes	LDNS_RDF_TYPE_EUI64,
122269257Sdes
123269257Sdes	/** A non-zero sequence of US-ASCII letters and numbers in lower case.
124269257Sdes	 *  For CAA.
125269257Sdes	 */
126269257Sdes	LDNS_RDF_TYPE_TAG,
127269257Sdes
128269257Sdes	/** A <character-string> encoding of the value field as specified
129269257Sdes	 * [RFC1035], Section 5.1., encoded as remaining rdata.
130269257Sdes	 * For CAA.
131269257Sdes	 */
132269257Sdes	LDNS_RDF_TYPE_LONG_STR,
133269257Sdes
134269257Sdes	/* Aliases */
135269257Sdes	LDNS_RDF_TYPE_BITMAP = LDNS_RDF_TYPE_NSEC
136238104Sdes};
137238104Sdestypedef enum ldns_enum_rdf_type ldns_rdf_type;
138238104Sdes
139238104Sdes/**
140238104Sdes * algorithms used in CERT rrs
141238104Sdes */
142238104Sdesenum ldns_enum_cert_algorithm
143238104Sdes{
144238104Sdes        LDNS_CERT_PKIX		= 1,
145238104Sdes        LDNS_CERT_SPKI		= 2,
146238104Sdes        LDNS_CERT_PGP		= 3,
147238104Sdes        LDNS_CERT_IPKIX         = 4,
148238104Sdes        LDNS_CERT_ISPKI         = 5,
149238104Sdes        LDNS_CERT_IPGP          = 6,
150238104Sdes        LDNS_CERT_ACPKIX        = 7,
151238104Sdes        LDNS_CERT_IACPKIX       = 8,
152238104Sdes        LDNS_CERT_URI		= 253,
153238104Sdes        LDNS_CERT_OID		= 254
154238104Sdes};
155238104Sdestypedef enum ldns_enum_cert_algorithm ldns_cert_algorithm;
156238104Sdes
157238104Sdes
158238104Sdes
159238104Sdes/**
160238104Sdes * Resource record data field.
161238104Sdes *
162238104Sdes * The data is a network ordered array of bytes, which size is specified by
163238104Sdes * the (16-bit) size field. To correctly parse it, use the type
164238104Sdes * specified in the (16-bit) type field with a value from \ref ldns_rdf_type.
165238104Sdes */
166238104Sdesstruct ldns_struct_rdf
167238104Sdes{
168238104Sdes	/** The size of the data (in octets) */
169238104Sdes	size_t _size;
170238104Sdes	/** The type of the data */
171238104Sdes	ldns_rdf_type _type;
172238104Sdes	/** Pointer to the data (raw octets) */
173238104Sdes	void  *_data;
174238104Sdes};
175238104Sdestypedef struct ldns_struct_rdf ldns_rdf;
176238104Sdes
177238104Sdes/* prototypes */
178238104Sdes
179238104Sdes/* write access functions */
180238104Sdes
181238104Sdes/**
182238104Sdes * sets the size of the rdf.
183238104Sdes * \param[in] *rd the rdf to operate on
184238104Sdes * \param[in] size the new size
185238104Sdes * \return void
186238104Sdes */
187238104Sdesvoid ldns_rdf_set_size(ldns_rdf *rd, size_t size);
188238104Sdes
189238104Sdes/**
190238104Sdes * sets the size of the rdf.
191238104Sdes * \param[in] *rd the rdf to operate on
192238104Sdes * \param[in] type the new type
193238104Sdes * \return void
194238104Sdes */
195238104Sdesvoid ldns_rdf_set_type(ldns_rdf *rd, ldns_rdf_type type);
196238104Sdes
197238104Sdes/**
198238104Sdes * sets the size of the rdf.
199238104Sdes * \param[in] *rd the rdf to operate on
200238104Sdes * \param[in] *data pointer to the new data
201238104Sdes * \return void
202238104Sdes */
203238104Sdesvoid ldns_rdf_set_data(ldns_rdf *rd, void *data);
204238104Sdes
205238104Sdes/* read access */
206238104Sdes
207238104Sdes/**
208238104Sdes * returns the size of the rdf.
209238104Sdes * \param[in] *rd the rdf to read from
210238104Sdes * \return uint16_t with the size
211238104Sdes */
212238104Sdessize_t ldns_rdf_size(const ldns_rdf *rd);
213238104Sdes
214238104Sdes/**
215238104Sdes * returns the type of the rdf. We need to insert _get_
216238104Sdes * here to prevent conflict the the rdf_type TYPE.
217238104Sdes * \param[in] *rd the rdf to read from
218238104Sdes * \return ldns_rdf_type with the type
219238104Sdes */
220238104Sdesldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd);
221238104Sdes
222238104Sdes/**
223238104Sdes * returns the data of the rdf.
224238104Sdes * \param[in] *rd the rdf to read from
225246854Sdes *
226238104Sdes * \return uint8_t* pointer to the rdf's data
227238104Sdes */
228238104Sdesuint8_t *ldns_rdf_data(const ldns_rdf *rd);
229238104Sdes
230238104Sdes/* creator functions */
231238104Sdes
232238104Sdes/**
233238104Sdes * allocates a new rdf structure and fills it.
234238104Sdes * This function DOES NOT copy the contents from
235238104Sdes * the buffer, unlinke ldns_rdf_new_frm_data()
236238104Sdes * \param[in] type type of the rdf
237238104Sdes * \param[in] size size of the buffer
238238104Sdes * \param[in] data pointer to the buffer to be copied
239238104Sdes * \return the new rdf structure or NULL on failure
240238104Sdes */
241238104Sdesldns_rdf *ldns_rdf_new(ldns_rdf_type type, size_t size, void *data);
242238104Sdes
243238104Sdes/**
244238104Sdes * allocates a new rdf structure and fills it.
245238104Sdes * This function _does_ copy the contents from
246238104Sdes * the buffer, unlinke ldns_rdf_new()
247238104Sdes * \param[in] type type of the rdf
248238104Sdes * \param[in] size size of the buffer
249238104Sdes * \param[in] data pointer to the buffer to be copied
250238104Sdes * \return the new rdf structure or NULL on failure
251238104Sdes */
252238104Sdesldns_rdf *ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data);
253238104Sdes
254238104Sdes/**
255238104Sdes * creates a new rdf from a string.
256238104Sdes * \param[in] type   type to use
257238104Sdes * \param[in] str string to use
258238104Sdes * \return ldns_rdf* or NULL in case of an error
259238104Sdes */
260238104Sdesldns_rdf *ldns_rdf_new_frm_str(ldns_rdf_type type, const char *str);
261238104Sdes
262238104Sdes/**
263238104Sdes * creates a new rdf from a file containing a string.
264238104Sdes * \param[out] r the new rdf
265238104Sdes * \param[in] type   type to use
266238104Sdes * \param[in] fp the file pointer  to use
267238104Sdes * \return LDNS_STATUS_OK or the error
268238104Sdes */
269238104Sdesldns_status ldns_rdf_new_frm_fp(ldns_rdf **r, ldns_rdf_type type, FILE *fp);
270238104Sdes
271238104Sdes/**
272238104Sdes * creates a new rdf from a file containing a string.
273238104Sdes * \param[out] r the new rdf
274238104Sdes * \param[in] type   type to use
275238104Sdes * \param[in] fp the file pointer  to use
276238104Sdes * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
277238104Sdes * \return LDNS_STATUS_OK or the error
278238104Sdes */
279238104Sdesldns_status ldns_rdf_new_frm_fp_l(ldns_rdf **r, ldns_rdf_type type, FILE *fp, int *line_nr);
280238104Sdes
281238104Sdes/* destroy functions */
282238104Sdes
283238104Sdes/**
284238104Sdes * frees a rdf structure, leaving the
285238104Sdes * data pointer intact.
286238104Sdes * \param[in] rd the pointer to be freed
287238104Sdes * \return void
288238104Sdes */
289238104Sdesvoid ldns_rdf_free(ldns_rdf *rd);
290238104Sdes
291238104Sdes/**
292238104Sdes * frees a rdf structure _and_ frees the
293238104Sdes * data. rdf should be created with _new_frm_data
294238104Sdes * \param[in] rd the rdf structure to be freed
295238104Sdes * \return void
296238104Sdes */
297238104Sdesvoid ldns_rdf_deep_free(ldns_rdf *rd);
298238104Sdes
299238104Sdes/* conversion functions */
300238104Sdes
301238104Sdes/**
302238104Sdes * returns the rdf containing the native uint8_t repr.
303238104Sdes * \param[in] type the ldns_rdf type to use
304238104Sdes * \param[in] value the uint8_t to use
305238104Sdes * \return ldns_rdf* with the converted value
306238104Sdes */
307238104Sdesldns_rdf *ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value);
308238104Sdes
309238104Sdes/**
310238104Sdes * returns the rdf containing the native uint16_t representation.
311238104Sdes * \param[in] type the ldns_rdf type to use
312238104Sdes * \param[in] value the uint16_t to use
313238104Sdes * \return ldns_rdf* with the converted value
314238104Sdes */
315238104Sdesldns_rdf *ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value);
316238104Sdes
317238104Sdes/**
318238104Sdes * returns an rdf that contains the given int32 value.
319238104Sdes *
320238104Sdes * Because multiple rdf types can contain an int32, the
321238104Sdes * type must be specified
322238104Sdes * \param[in] type the ldns_rdf type to use
323238104Sdes * \param[in] value the uint32_t to use
324238104Sdes * \return ldns_rdf* with the converted value
325238104Sdes */
326238104Sdesldns_rdf *ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value);
327238104Sdes
328238104Sdes/**
329238104Sdes * returns an int16_data rdf that contains the data in the
330238104Sdes * given array, preceded by an int16 specifying the length.
331238104Sdes *
332238104Sdes * The memory is copied, and an LDNS_RDF_TYPE_INT16DATA is returned
333238104Sdes * \param[in] size the size of the data
334238104Sdes * \param[in] *data pointer to the actual data
335246854Sdes *
336238104Sdes * \return ldns_rd* the rdf with the data
337238104Sdes */
338238104Sdesldns_rdf *ldns_native2rdf_int16_data(size_t size, uint8_t *data);
339238104Sdes
340238104Sdes/**
341238104Sdes * reverses an rdf, only actually useful for AAAA and A records.
342238104Sdes * The returned rdf has the type LDNS_RDF_TYPE_DNAME!
343238104Sdes * \param[in] *rd rdf to be reversed
344238104Sdes * \return the reversed rdf (a newly created rdf)
345238104Sdes */
346238104Sdesldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd);
347238104Sdes
348238104Sdes/**
349238104Sdes * returns the native uint8_t representation from the rdf.
350238104Sdes * \param[in] rd the ldns_rdf to operate on
351238104Sdes * \return uint8_t the value extracted
352238104Sdes */
353238104Sdesuint8_t 	ldns_rdf2native_int8(const ldns_rdf *rd);
354238104Sdes
355238104Sdes/**
356238104Sdes * returns the native uint16_t representation from the rdf.
357238104Sdes * \param[in] rd the ldns_rdf to operate on
358238104Sdes * \return uint16_t the value extracted
359238104Sdes */
360238104Sdesuint16_t	ldns_rdf2native_int16(const ldns_rdf *rd);
361238104Sdes
362238104Sdes/**
363238104Sdes * returns the native uint32_t representation from the rdf.
364238104Sdes * \param[in] rd the ldns_rdf to operate on
365238104Sdes * \return uint32_t the value extracted
366238104Sdes */
367238104Sdesuint32_t ldns_rdf2native_int32(const ldns_rdf *rd);
368238104Sdes
369238104Sdes/**
370238104Sdes * returns the native time_t representation from the rdf.
371238104Sdes * \param[in] rd the ldns_rdf to operate on
372238104Sdes * \return time_t the value extracted (32 bits currently)
373238104Sdes */
374238104Sdestime_t ldns_rdf2native_time_t(const ldns_rdf *rd);
375238104Sdes
376238104Sdes/**
377238104Sdes * converts a ttl value (like 5d2h) to a long.
378238104Sdes * \param[in] nptr the start of the string
379238104Sdes * \param[out] endptr points to the last char in case of error
380238104Sdes * \return the convert duration value
381238104Sdes */
382238104Sdesuint32_t ldns_str2period(const char *nptr, const char **endptr);
383238104Sdes
384238104Sdes/**
385238104Sdes * removes \\DDD, \\[space] and other escapes from the input.
386238104Sdes * See RFC 1035, section 5.1.
387238104Sdes * \param[in] word what to check
388238104Sdes * \param[in] length the string
389238104Sdes * \return ldns_status mesg
390238104Sdes */
391238104Sdesldns_status ldns_octet(char *word, size_t *length);
392238104Sdes
393238104Sdes/**
394238104Sdes * clones a rdf structure. The data is copied.
395238104Sdes * \param[in] rd rdf to be copied
396238104Sdes * \return a new rdf structure
397238104Sdes */
398238104Sdesldns_rdf *ldns_rdf_clone(const ldns_rdf *rd);
399238104Sdes
400238104Sdes/**
401238104Sdes * compares two rdf's on their wire formats.
402238104Sdes * (To order dnames according to rfc4034, use ldns_dname_compare)
403238104Sdes * \param[in] rd1 the first one
404238104Sdes * \param[in] rd2 the second one
405238104Sdes * \return 0 if equal
406238104Sdes * \return -1 if rd1 comes before rd2
407238104Sdes * \return +1 if rd2 comes before rd1
408238104Sdes */
409238104Sdesint ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2);
410238104Sdes
411269257Sdes/**
412269257Sdes * Gets the algorithm value, the HIT and Public Key data from the rdf with
413269257Sdes * type LDNS_RDF_TYPE_HIP.
414269257Sdes * \param[in] rdf the rdf with type LDNS_RDF_TYPE_HIP
415269257Sdes * \param[out] alg      the algorithm
416269257Sdes * \param[out] hit_size the size of the HIT data
417269257Sdes * \param[out] hit      the hit data
418269257Sdes * \param[out] pk_size  the size of the Public Key data
419269257Sdes * \param[out] pk       the  Public Key data
420269257Sdes * \return LDNS_STATUS_OK on success, and the error otherwise
421269257Sdes */
422269257Sdesldns_status ldns_rdf_hip_get_alg_hit_pk(ldns_rdf *rdf, uint8_t* alg,
423269257Sdes		uint8_t *hit_size, uint8_t** hit,
424269257Sdes		uint16_t *pk_size, uint8_t** pk);
425269257Sdes
426269257Sdes/**
427269257Sdes * Creates a new LDNS_RDF_TYPE_HIP rdf from given data.
428269257Sdes * \param[out] rdf      the newly created LDNS_RDF_TYPE_HIP rdf
429269257Sdes * \param[in]  alg      the algorithm
430269257Sdes * \param[in]  hit_size the size of the HIT data
431269257Sdes * \param[in]  hit      the hit data
432269257Sdes * \param[in]  pk_size  the size of the Public Key data
433269257Sdes * \param[in]  pk       the  Public Key data
434269257Sdes * \return LDNS_STATUS_OK on success, and the error otherwise
435269257Sdes */
436269257Sdesldns_status ldns_rdf_hip_new_frm_alg_hit_pk(ldns_rdf** rdf, uint8_t alg,
437269257Sdes		uint8_t hit_size, uint8_t *hit, uint16_t pk_size, uint8_t *pk);
438269257Sdes
439238104Sdes#ifdef __cplusplus
440238104Sdes}
441238104Sdes#endif
442238104Sdes
443238104Sdes#endif	/* LDNS_RDATA_H */
444