1238104Sdes/*
2238104Sdes * rr.h -  resource record definitions
3238104Sdes *
4238104Sdes * a Net::DNS like library for C
5238104Sdes *
6238104Sdes * (c) NLnet Labs, 2005-2006
7238104Sdes *
8238104Sdes * See the file LICENSE for the license
9238104Sdes */
10238104Sdes
11238104Sdes/**
12238104Sdes * \file
13238104Sdes *
14238104Sdes * Contains the definition of ldns_rr and functions to manipulate those.
15238104Sdes */
16238104Sdes
17238104Sdes
18238104Sdes#ifndef LDNS_RR_H
19238104Sdes#define LDNS_RR_H
20238104Sdes
21238104Sdes#include <ldns/common.h>
22238104Sdes#include <ldns/rdata.h>
23238104Sdes#include <ldns/buffer.h>
24238104Sdes#include <ldns/error.h>
25238104Sdes
26238104Sdes#ifdef __cplusplus
27238104Sdesextern "C" {
28238104Sdes#endif
29238104Sdes
30238104Sdes/** Maximum length of a dname label */
31238104Sdes#define LDNS_MAX_LABELLEN     63
32238104Sdes/** Maximum length of a complete dname */
33238104Sdes#define LDNS_MAX_DOMAINLEN    255
34238104Sdes/** Maximum number of pointers in 1 dname */
35238104Sdes#define LDNS_MAX_POINTERS	65535
36238104Sdes/** The bytes TTL, CLASS and length use up in an rr */
37238104Sdes#define LDNS_RR_OVERHEAD	10
38238104Sdes
39266114Sdes/* The first fields are contiguous and can be referenced instantly */
40266114Sdes#define LDNS_RDATA_FIELD_DESCRIPTORS_COMMON 258
41238104Sdes
42238104Sdes
43238104Sdes
44238104Sdes/**
45238104Sdes *  The different RR classes.
46238104Sdes */
47238104Sdesenum ldns_enum_rr_class
48238104Sdes{
49238104Sdes	/** the Internet */
50238104Sdes	LDNS_RR_CLASS_IN 	= 1,
51238104Sdes	/** Chaos class */
52238104Sdes	LDNS_RR_CLASS_CH	= 3,
53238104Sdes	/** Hesiod (Dyer 87) */
54238104Sdes	LDNS_RR_CLASS_HS	= 4,
55238104Sdes    /** None class, dynamic update */
56238104Sdes    LDNS_RR_CLASS_NONE      = 254,
57238104Sdes	/** Any class */
58238104Sdes	LDNS_RR_CLASS_ANY	= 255,
59238104Sdes
60238104Sdes	LDNS_RR_CLASS_FIRST     = 0,
61238104Sdes	LDNS_RR_CLASS_LAST      = 65535,
62238104Sdes	LDNS_RR_CLASS_COUNT     = LDNS_RR_CLASS_LAST - LDNS_RR_CLASS_FIRST + 1
63238104Sdes};
64238104Sdestypedef enum ldns_enum_rr_class ldns_rr_class;
65238104Sdes
66238104Sdes/**
67238104Sdes *  Used to specify whether compression is allowed.
68238104Sdes */
69238104Sdesenum ldns_enum_rr_compress
70238104Sdes{
71238104Sdes	/** compression is allowed */
72238104Sdes	LDNS_RR_COMPRESS,
73238104Sdes	LDNS_RR_NO_COMPRESS
74238104Sdes};
75238104Sdestypedef enum ldns_enum_rr_compress ldns_rr_compress;
76238104Sdes
77238104Sdes/**
78238104Sdes * The different RR types.
79238104Sdes */
80238104Sdesenum ldns_enum_rr_type
81238104Sdes{
82238104Sdes	/**  a host address */
83238104Sdes	LDNS_RR_TYPE_A = 1,
84238104Sdes	/**  an authoritative name server */
85238104Sdes	LDNS_RR_TYPE_NS = 2,
86238104Sdes	/**  a mail destination (Obsolete - use MX) */
87238104Sdes	LDNS_RR_TYPE_MD = 3,
88238104Sdes	/**  a mail forwarder (Obsolete - use MX) */
89238104Sdes	LDNS_RR_TYPE_MF = 4,
90238104Sdes	/**  the canonical name for an alias */
91238104Sdes	LDNS_RR_TYPE_CNAME = 5,
92238104Sdes	/**  marks the start of a zone of authority */
93238104Sdes	LDNS_RR_TYPE_SOA = 6,
94238104Sdes	/**  a mailbox domain name (EXPERIMENTAL) */
95238104Sdes	LDNS_RR_TYPE_MB = 7,
96238104Sdes	/**  a mail group member (EXPERIMENTAL) */
97238104Sdes	LDNS_RR_TYPE_MG = 8,
98238104Sdes	/**  a mail rename domain name (EXPERIMENTAL) */
99238104Sdes	LDNS_RR_TYPE_MR = 9,
100238104Sdes	/**  a null RR (EXPERIMENTAL) */
101238104Sdes	LDNS_RR_TYPE_NULL = 10,
102238104Sdes	/**  a well known service description */
103238104Sdes	LDNS_RR_TYPE_WKS = 11,
104238104Sdes	/**  a domain name pointer */
105238104Sdes	LDNS_RR_TYPE_PTR = 12,
106238104Sdes	/**  host information */
107238104Sdes	LDNS_RR_TYPE_HINFO = 13,
108238104Sdes	/**  mailbox or mail list information */
109238104Sdes	LDNS_RR_TYPE_MINFO = 14,
110238104Sdes	/**  mail exchange */
111238104Sdes	LDNS_RR_TYPE_MX = 15,
112238104Sdes	/**  text strings */
113238104Sdes	LDNS_RR_TYPE_TXT = 16,
114238104Sdes	/**  RFC1183 */
115238104Sdes	LDNS_RR_TYPE_RP = 17,
116238104Sdes	/**  RFC1183 */
117238104Sdes	LDNS_RR_TYPE_AFSDB = 18,
118238104Sdes	/**  RFC1183 */
119238104Sdes	LDNS_RR_TYPE_X25 = 19,
120238104Sdes	/**  RFC1183 */
121238104Sdes	LDNS_RR_TYPE_ISDN = 20,
122238104Sdes	/**  RFC1183 */
123238104Sdes	LDNS_RR_TYPE_RT = 21,
124238104Sdes	/**  RFC1706 */
125238104Sdes	LDNS_RR_TYPE_NSAP = 22,
126238104Sdes	/**  RFC1348 */
127238104Sdes	LDNS_RR_TYPE_NSAP_PTR = 23,
128238104Sdes	/**  2535typecode */
129238104Sdes	LDNS_RR_TYPE_SIG = 24,
130238104Sdes	/**  2535typecode */
131238104Sdes	LDNS_RR_TYPE_KEY = 25,
132238104Sdes	/**  RFC2163 */
133238104Sdes	LDNS_RR_TYPE_PX = 26,
134238104Sdes	/**  RFC1712 */
135238104Sdes	LDNS_RR_TYPE_GPOS = 27,
136238104Sdes	/**  ipv6 address */
137238104Sdes	LDNS_RR_TYPE_AAAA = 28,
138238104Sdes	/**  LOC record  RFC1876 */
139238104Sdes	LDNS_RR_TYPE_LOC = 29,
140238104Sdes	/**  2535typecode */
141238104Sdes	LDNS_RR_TYPE_NXT = 30,
142238104Sdes	/**  draft-ietf-nimrod-dns-01.txt */
143238104Sdes	LDNS_RR_TYPE_EID = 31,
144238104Sdes	/**  draft-ietf-nimrod-dns-01.txt */
145238104Sdes	LDNS_RR_TYPE_NIMLOC = 32,
146238104Sdes	/**  SRV record RFC2782 */
147238104Sdes	LDNS_RR_TYPE_SRV = 33,
148238104Sdes	/**  http://www.jhsoft.com/rfc/af-saa-0069.000.rtf */
149238104Sdes	LDNS_RR_TYPE_ATMA = 34,
150238104Sdes	/**  RFC2915 */
151238104Sdes	LDNS_RR_TYPE_NAPTR = 35,
152238104Sdes	/**  RFC2230 */
153238104Sdes	LDNS_RR_TYPE_KX = 36,
154238104Sdes	/**  RFC2538 */
155238104Sdes	LDNS_RR_TYPE_CERT = 37,
156238104Sdes	/**  RFC2874 */
157238104Sdes	LDNS_RR_TYPE_A6 = 38,
158238104Sdes	/**  RFC2672 */
159238104Sdes	LDNS_RR_TYPE_DNAME = 39,
160238104Sdes	/**  dnsind-kitchen-sink-02.txt */
161238104Sdes	LDNS_RR_TYPE_SINK = 40,
162238104Sdes	/**  Pseudo OPT record... */
163238104Sdes	LDNS_RR_TYPE_OPT = 41,
164238104Sdes	/**  RFC3123 */
165238104Sdes	LDNS_RR_TYPE_APL = 42,
166266114Sdes	/**  RFC4034, RFC3658 */
167238104Sdes	LDNS_RR_TYPE_DS = 43,
168238104Sdes	/**  SSH Key Fingerprint */
169238104Sdes	LDNS_RR_TYPE_SSHFP = 44, /* RFC 4255 */
170238104Sdes	/**  IPsec Key */
171238104Sdes	LDNS_RR_TYPE_IPSECKEY = 45, /* RFC 4025 */
172238104Sdes	/**  DNSSEC */
173238104Sdes	LDNS_RR_TYPE_RRSIG = 46, /* RFC 4034 */
174238104Sdes	LDNS_RR_TYPE_NSEC = 47, /* RFC 4034 */
175238104Sdes	LDNS_RR_TYPE_DNSKEY = 48, /* RFC 4034 */
176238104Sdes
177238104Sdes	LDNS_RR_TYPE_DHCID = 49, /* RFC 4701 */
178238104Sdes	/* NSEC3 */
179238104Sdes	LDNS_RR_TYPE_NSEC3 = 50, /* RFC 5155 */
180238104Sdes	LDNS_RR_TYPE_NSEC3PARAM = 51, /* RFC 5155 */
181238104Sdes	LDNS_RR_TYPE_NSEC3PARAMS = 51,
182266114Sdes	LDNS_RR_TYPE_TLSA = 52, /* RFC 6698 */
183238104Sdes
184266114Sdes	LDNS_RR_TYPE_HIP = 55, /* RFC 5205 */
185266114Sdes
186266114Sdes	/** draft-reid-dnsext-zs */
187266114Sdes	LDNS_RR_TYPE_NINFO = 56,
188266114Sdes	/** draft-reid-dnsext-rkey */
189266114Sdes	LDNS_RR_TYPE_RKEY = 57,
190238104Sdes        /** draft-ietf-dnsop-trust-history */
191238104Sdes        LDNS_RR_TYPE_TALINK = 58,
192266114Sdes	/** draft-barwood-dnsop-ds-publis */
193266114Sdes	LDNS_RR_TYPE_CDS = 59,
194238104Sdes
195266114Sdes	LDNS_RR_TYPE_SPF = 99, /* RFC 4408 */
196238104Sdes
197238104Sdes	LDNS_RR_TYPE_UINFO = 100,
198238104Sdes	LDNS_RR_TYPE_UID = 101,
199238104Sdes	LDNS_RR_TYPE_GID = 102,
200238104Sdes	LDNS_RR_TYPE_UNSPEC = 103,
201238104Sdes
202266114Sdes	LDNS_RR_TYPE_NID = 104, /* RFC 6742 */
203266114Sdes	LDNS_RR_TYPE_L32 = 105, /* RFC 6742 */
204266114Sdes	LDNS_RR_TYPE_L64 = 106, /* RFC 6742 */
205266114Sdes	LDNS_RR_TYPE_LP = 107, /* RFC 6742 */
206266114Sdes
207266114Sdes	LDNS_RR_TYPE_EUI48 = 108, /* RFC 7043 */
208266114Sdes	LDNS_RR_TYPE_EUI64 = 109, /* RFC 7043 */
209266114Sdes
210266114Sdes	LDNS_RR_TYPE_TKEY = 249, /* RFC 2930 */
211238104Sdes	LDNS_RR_TYPE_TSIG = 250,
212238104Sdes	LDNS_RR_TYPE_IXFR = 251,
213238104Sdes	LDNS_RR_TYPE_AXFR = 252,
214238104Sdes	/**  A request for mailbox-related records (MB, MG or MR) */
215238104Sdes	LDNS_RR_TYPE_MAILB = 253,
216238104Sdes	/**  A request for mail agent RRs (Obsolete - see MX) */
217238104Sdes	LDNS_RR_TYPE_MAILA = 254,
218238104Sdes	/**  any type (wildcard) */
219238104Sdes	LDNS_RR_TYPE_ANY = 255,
220266114Sdes	/** draft-faltstrom-uri-06 */
221266114Sdes	LDNS_RR_TYPE_URI = 256,
222266114Sdes	LDNS_RR_TYPE_CAA = 257, /* RFC 6844 */
223238104Sdes
224266114Sdes	/** DNSSEC Trust Authorities */
225266114Sdes	LDNS_RR_TYPE_TA = 32768,
226238104Sdes	/* RFC 4431, 5074, DNSSEC Lookaside Validation */
227238104Sdes	LDNS_RR_TYPE_DLV = 32769,
228238104Sdes
229238104Sdes	/* type codes from nsec3 experimental phase
230238104Sdes	LDNS_RR_TYPE_NSEC3 = 65324,
231238104Sdes	LDNS_RR_TYPE_NSEC3PARAMS = 65325, */
232238104Sdes	LDNS_RR_TYPE_FIRST = 0,
233238104Sdes	LDNS_RR_TYPE_LAST  = 65535,
234238104Sdes	LDNS_RR_TYPE_COUNT = LDNS_RR_TYPE_LAST - LDNS_RR_TYPE_FIRST + 1
235238104Sdes};
236238104Sdestypedef enum ldns_enum_rr_type ldns_rr_type;
237238104Sdes
238238104Sdes/**
239238104Sdes * Resource Record
240238104Sdes *
241238104Sdes * This is the basic DNS element that contains actual data
242238104Sdes *
243238104Sdes * From RFC1035:
244238104Sdes * <pre>
245238104Sdes3.2.1. Format
246238104Sdes
247238104SdesAll RRs have the same top level format shown below:
248238104Sdes
249238104Sdes                                    1  1  1  1  1  1
250238104Sdes      0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
251238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
252238104Sdes    |                                               |
253238104Sdes    /                                               /
254238104Sdes    /                      NAME                     /
255238104Sdes    |                                               |
256238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
257238104Sdes    |                      TYPE                     |
258238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
259238104Sdes    |                     CLASS                     |
260238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
261238104Sdes    |                      TTL                      |
262238104Sdes    |                                               |
263238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
264238104Sdes    |                   RDLENGTH                    |
265238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
266238104Sdes    /                     RDATA                     /
267238104Sdes    /                                               /
268238104Sdes    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
269238104Sdes
270238104Sdeswhere:
271238104Sdes
272238104SdesNAME            an owner name, i.e., the name of the node to which this
273238104Sdes                resource record pertains.
274238104Sdes
275238104SdesTYPE            two octets containing one of the RR TYPE codes.
276238104Sdes
277238104SdesCLASS           two octets containing one of the RR CLASS codes.
278238104Sdes
279238104SdesTTL             a 32 bit signed integer that specifies the time interval
280238104Sdes                that the resource record may be cached before the source
281238104Sdes                of the information should again be consulted.  Zero
282238104Sdes                values are interpreted to mean that the RR can only be
283238104Sdes                used for the transaction in progress, and should not be
284238104Sdes                cached.  For example, SOA records are always distributed
285238104Sdes                with a zero TTL to prohibit caching.  Zero values can
286238104Sdes                also be used for extremely volatile data.
287238104Sdes
288238104SdesRDLENGTH        an unsigned 16 bit integer that specifies the length in
289238104Sdes                octets of the RDATA field.
290238104Sdes
291238104SdesRDATA           a variable length string of octets that describes the
292238104Sdes                resource.  The format of this information varies
293238104Sdes                according to the TYPE and CLASS of the resource record.
294238104Sdes * </pre>
295238104Sdes *
296238104Sdes * The actual amount and type of rdata fields depend on the RR type of the
297238104Sdes * RR, and can be found by using \ref ldns_rr_descriptor functions.
298238104Sdes */
299238104Sdesstruct ldns_struct_rr
300238104Sdes{
301238104Sdes	/**  Owner name, uncompressed */
302238104Sdes	ldns_rdf	*_owner;
303238104Sdes	/**  Time to live  */
304238104Sdes	uint32_t	_ttl;
305238104Sdes	/**  Number of data fields */
306238104Sdes	size_t	        _rd_count;
307238104Sdes	/**  the type of the RR. A, MX etc. */
308238104Sdes	ldns_rr_type	_rr_type;
309238104Sdes	/**  Class of the resource record.  */
310238104Sdes	ldns_rr_class	_rr_class;
311238104Sdes	/* everything in the rdata is in network order */
312238104Sdes	/**  The array of rdata's */
313238104Sdes	ldns_rdf	 **_rdata_fields;
314238104Sdes	/**  question rr [it would be nicer if thous is after _rd_count]
315238104Sdes		 ABI change: Fix this in next major release
316238104Sdes	 */
317238104Sdes	bool		_rr_question;
318238104Sdes};
319238104Sdestypedef struct ldns_struct_rr ldns_rr;
320238104Sdes
321238104Sdes/**
322238104Sdes * List or Set of Resource Records
323238104Sdes *
324238104Sdes * Contains a list of rr's <br>
325238104Sdes * No official RFC-like checks are made
326238104Sdes */
327238104Sdesstruct ldns_struct_rr_list
328238104Sdes{
329238104Sdes	size_t _rr_count;
330238104Sdes	size_t _rr_capacity;
331238104Sdes	ldns_rr **_rrs;
332238104Sdes};
333238104Sdestypedef struct ldns_struct_rr_list ldns_rr_list;
334238104Sdes
335238104Sdes/**
336238104Sdes * Contains all information about resource record types.
337238104Sdes *
338238104Sdes * This structure contains, for all rr types, the rdata fields that are defined.
339238104Sdes */
340238104Sdesstruct ldns_struct_rr_descriptor
341238104Sdes{
342238104Sdes	/** Type of the RR that is described here */
343238104Sdes	ldns_rr_type    _type;
344238104Sdes	/** Textual name of the RR type.  */
345238104Sdes	const char *_name;
346238104Sdes	/** Minimum number of rdata fields in the RRs of this type.  */
347238104Sdes	uint8_t     _minimum;
348238104Sdes	/** Maximum number of rdata fields in the RRs of this type.  */
349238104Sdes	uint8_t     _maximum;
350238104Sdes	/** Wireformat specification for the rr, i.e. the types of rdata fields in their respective order. */
351238104Sdes	const ldns_rdf_type *_wireformat;
352238104Sdes	/** Special rdf types */
353238104Sdes	ldns_rdf_type _variable;
354238104Sdes	/** Specifies whether compression can be used for dnames in this RR type. */
355238104Sdes	ldns_rr_compress _compress;
356238104Sdes	/** The number of DNAMEs in the _wireformat string, for parsing. */
357238104Sdes	uint8_t _dname_count;
358238104Sdes};
359238104Sdestypedef struct ldns_struct_rr_descriptor ldns_rr_descriptor;
360238104Sdes
361266114Sdes
362238104Sdes/**
363266114Sdes * Create a rr type bitmap rdf providing enough space to set all
364266114Sdes * known (to ldns) rr types.
365266114Sdes * \param[out] rdf the constructed rdf
366266114Sdes * \return LDNS_STATUS_OK if all went well.
367266114Sdes */
368266114Sdesldns_status ldns_rdf_bitmap_known_rr_types_space(ldns_rdf** rdf);
369266114Sdes
370266114Sdes/**
371266114Sdes * Create a rr type bitmap rdf with at least all known (to ldns) rr types set.
372266114Sdes * \param[out] rdf the constructed rdf
373266114Sdes * \return LDNS_STATUS_OK if all went well.
374266114Sdes */
375266114Sdesldns_status ldns_rdf_bitmap_known_rr_types(ldns_rdf** rdf);
376266114Sdes
377266114Sdes
378266114Sdes/**
379238104Sdes * creates a new rr structure.
380238104Sdes * \return ldns_rr *
381238104Sdes */
382238104Sdesldns_rr* ldns_rr_new(void);
383238104Sdes
384238104Sdes/**
385238104Sdes * creates a new rr structure, based on the given type.
386238104Sdes * alloc enough space to hold all the rdf's
387238104Sdes */
388238104Sdesldns_rr* ldns_rr_new_frm_type(ldns_rr_type t);
389238104Sdes
390238104Sdes/**
391238104Sdes * frees an RR structure
392238104Sdes * \param[in] *rr the RR to be freed
393238104Sdes * \return void
394238104Sdes */
395238104Sdesvoid ldns_rr_free(ldns_rr *rr);
396238104Sdes
397238104Sdes/**
398238104Sdes * creates an rr from a string.
399238104Sdes * The string should be a fully filled-in rr, like
400238104Sdes * ownername &lt;space&gt; TTL &lt;space&gt; CLASS &lt;space&gt;
401238104Sdes * TYPE &lt;space&gt; RDATA.
402238104Sdes * \param[out] n the rr to return
403238104Sdes * \param[in] str the string to convert
404238104Sdes * \param[in] default_ttl default ttl value for the rr.
405238104Sdes *            If 0 DEF_TTL will be used
406238104Sdes * \param[in] origin when the owner is relative add this.
407238104Sdes *	The caller must ldns_rdf_deep_free it.
408238104Sdes * \param[out] prev the previous ownername. if this value is not NULL,
409238104Sdes * the function overwrites this with the ownername found in this
410238104Sdes * string. The caller must then ldns_rdf_deep_free it.
411238104Sdes * \return a status msg describing an error or LDNS_STATUS_OK
412238104Sdes */
413238104Sdesldns_status ldns_rr_new_frm_str(ldns_rr **n, const char *str,
414238104Sdes                                uint32_t default_ttl, ldns_rdf *origin,
415238104Sdes                                ldns_rdf **prev);
416238104Sdes
417238104Sdes/**
418238104Sdes * creates an rr for the question section from a string, i.e.
419238104Sdes * without RDATA fields
420238104Sdes * Origin and previous RR functionality are the same as in
421238104Sdes * ldns_rr_new_frm_str()
422238104Sdes * \param[out] n the rr to return
423238104Sdes * \param[in] str the string to convert
424238104Sdes * \param[in] origin when the owner is relative add this.
425238104Sdes *	The caller must ldns_rdf_deep_free it.
426238104Sdes * \param prev the previous ownername. the function overwrite this with
427238104Sdes * the current found ownername. The caller must ldns_rdf_deep_free it.
428238104Sdes * \return a status msg describing an error or LDNS_STATUS_OK
429238104Sdes */
430238104Sdesldns_status ldns_rr_new_question_frm_str(ldns_rr **n, const char *str,
431238104Sdes                                ldns_rdf *origin, ldns_rdf **prev);
432238104Sdes
433238104Sdes/**
434238104Sdes * creates a new rr from a file containing a string.
435238104Sdes * \param[out] rr the new rr
436238104Sdes * \param[in] fp the file pointer to use
437238104Sdes * \param[in] default_ttl pointer to a default ttl for the rr. If NULL DEF_TTL will be used
438238104Sdes *            the pointer will be updated if the file contains a $TTL directive
439238104Sdes * \param[in] origin when the owner is relative add this
440238104Sdes * 	      the pointer will be updated if the file contains a $ORIGIN directive
441238104Sdes *	      The caller must ldns_rdf_deep_free it.
442238104Sdes * \param[in] prev when the owner is whitespaces use this as the * ownername
443238104Sdes *            the pointer will be updated after the call
444238104Sdes *	      The caller must ldns_rdf_deep_free it.
445238104Sdes * \return a ldns_status with an error or LDNS_STATUS_OK
446238104Sdes */
447238104Sdesldns_status ldns_rr_new_frm_fp(ldns_rr **rr, FILE *fp, uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev);
448238104Sdes
449238104Sdes/**
450238104Sdes * creates a new rr from a file containing a string.
451238104Sdes * \param[out] rr the new rr
452238104Sdes * \param[in] fp the file pointer to use
453238104Sdes * \param[in] default_ttl a default ttl for the rr. If NULL DEF_TTL will be used
454238104Sdes *            the pointer will be updated if the file contains a $TTL directive
455238104Sdes * \param[in] origin when the owner is relative add this
456238104Sdes * 	      the pointer will be updated if the file contains a $ORIGIN directive
457238104Sdes *	      The caller must ldns_rdf_deep_free it.
458238104Sdes * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
459238104Sdes * \param[in] prev when the owner is whitespaces use this as the * ownername
460238104Sdes *            the pointer will be updated after the call
461238104Sdes *	      The caller must ldns_rdf_deep_free it.
462238104Sdes * \return a ldns_status with an error or LDNS_STATUS_OK
463238104Sdes */
464238104Sdesldns_status ldns_rr_new_frm_fp_l(ldns_rr **rr, FILE *fp, uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev, int *line_nr);
465238104Sdes
466238104Sdes/**
467238104Sdes * sets the owner in the rr structure.
468238104Sdes * \param[in] *rr rr to operate on
469238104Sdes * \param[in] *owner set to this owner
470238104Sdes * \return void
471238104Sdes */
472238104Sdesvoid ldns_rr_set_owner(ldns_rr *rr, ldns_rdf *owner);
473238104Sdes
474238104Sdes/**
475238104Sdes * sets the question flag in the rr structure.
476238104Sdes * \param[in] *rr rr to operate on
477238104Sdes * \param[in] question question flag
478238104Sdes * \return void
479238104Sdes */
480238104Sdesvoid ldns_rr_set_question(ldns_rr *rr, bool question);
481238104Sdes
482238104Sdes/**
483238104Sdes * sets the ttl in the rr structure.
484238104Sdes * \param[in] *rr rr to operate on
485238104Sdes * \param[in] ttl set to this ttl
486238104Sdes * \return void
487238104Sdes */
488238104Sdesvoid ldns_rr_set_ttl(ldns_rr *rr, uint32_t ttl);
489238104Sdes
490238104Sdes/**
491238104Sdes * sets the rd_count in the rr.
492238104Sdes * \param[in] *rr rr to operate on
493238104Sdes * \param[in] count set to this count
494238104Sdes * \return void
495238104Sdes */
496238104Sdesvoid ldns_rr_set_rd_count(ldns_rr *rr, size_t count);
497238104Sdes
498238104Sdes/**
499238104Sdes * sets the type in the rr.
500238104Sdes * \param[in] *rr rr to operate on
501238104Sdes * \param[in] rr_type set to this type
502238104Sdes * \return void
503238104Sdes */
504238104Sdesvoid ldns_rr_set_type(ldns_rr *rr, ldns_rr_type rr_type);
505238104Sdes
506238104Sdes/**
507238104Sdes * sets the class in the rr.
508238104Sdes * \param[in] *rr rr to operate on
509238104Sdes * \param[in] rr_class set to this class
510238104Sdes * \return void
511238104Sdes */
512238104Sdesvoid ldns_rr_set_class(ldns_rr *rr, ldns_rr_class rr_class);
513238104Sdes
514238104Sdes/**
515238104Sdes * sets a rdf member, it will be set on the
516238104Sdes * position given. The old value is returned, like pop.
517238104Sdes * \param[in] *rr the rr to operate on
518238104Sdes * \param[in] *f the rdf to set
519238104Sdes * \param[in] position the position the set the rdf
520238104Sdes * \return  the old value in the rr, NULL on failyre
521238104Sdes */
522238104Sdesldns_rdf* ldns_rr_set_rdf(ldns_rr *rr, const ldns_rdf *f, size_t position);
523238104Sdes
524238104Sdes/**
525238104Sdes * sets rd_field member, it will be
526238104Sdes * placed in the next available spot.
527238104Sdes * \param[in] *rr rr to operate on
528238104Sdes * \param[in] *f the data field member to set
529238104Sdes * \return bool
530238104Sdes */
531238104Sdesbool ldns_rr_push_rdf(ldns_rr *rr, const ldns_rdf *f);
532238104Sdes
533238104Sdes/**
534238104Sdes * removes a rd_field member, it will be
535238104Sdes * popped from the last position.
536238104Sdes * \param[in] *rr rr to operate on
537238104Sdes * \return rdf which was popped (null if nothing)
538238104Sdes */
539238104Sdesldns_rdf* ldns_rr_pop_rdf(ldns_rr *rr);
540238104Sdes
541238104Sdes/**
542238104Sdes * returns the rdata field member counter.
543238104Sdes * \param[in] *rr rr to operate on
544238104Sdes * \param[in] nr the number of the rdf to return
545238104Sdes * \return ldns_rdf *
546238104Sdes */
547238104Sdesldns_rdf* ldns_rr_rdf(const ldns_rr *rr, size_t nr);
548238104Sdes
549238104Sdes/**
550238104Sdes * returns the owner name of an rr structure.
551238104Sdes * \param[in] *rr rr to operate on
552238104Sdes * \return ldns_rdf *
553238104Sdes */
554238104Sdesldns_rdf* ldns_rr_owner(const ldns_rr *rr);
555238104Sdes
556238104Sdes/**
557238104Sdes * returns the question flag of an rr structure.
558238104Sdes * \param[in] *rr rr to operate on
559238104Sdes * \return bool true if question
560238104Sdes */
561238104Sdesbool ldns_rr_is_question(const ldns_rr *rr);
562238104Sdes
563238104Sdes/**
564238104Sdes * returns the ttl of an rr structure.
565238104Sdes * \param[in] *rr the rr to read from
566238104Sdes * \return the ttl of the rr
567238104Sdes */
568238104Sdesuint32_t ldns_rr_ttl(const ldns_rr *rr);
569238104Sdes
570238104Sdes/**
571238104Sdes * returns the rd_count of an rr structure.
572238104Sdes * \param[in] *rr the rr to read from
573238104Sdes * \return the rd count of the rr
574238104Sdes */
575238104Sdessize_t ldns_rr_rd_count(const ldns_rr *rr);
576238104Sdes
577238104Sdes/**
578238104Sdes * returns the type of the rr.
579238104Sdes * \param[in] *rr the rr to read from
580238104Sdes * \return the type of the rr
581238104Sdes */
582238104Sdesldns_rr_type ldns_rr_get_type(const ldns_rr *rr);
583238104Sdes
584238104Sdes/**
585238104Sdes * returns the class of the rr.
586238104Sdes * \param[in] *rr the rr to read from
587238104Sdes * \return the class of the rr
588238104Sdes */
589238104Sdesldns_rr_class ldns_rr_get_class(const ldns_rr *rr);
590238104Sdes
591238104Sdes/* rr_lists */
592238104Sdes
593238104Sdes/**
594238104Sdes * returns the number of rr's in an rr_list.
595238104Sdes * \param[in] rr_list  the rr_list to read from
596238104Sdes * \return the number of rr's
597238104Sdes */
598238104Sdessize_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list);
599238104Sdes
600238104Sdes/**
601238104Sdes * sets the number of rr's in an rr_list.
602238104Sdes * \param[in] rr_list the rr_list to set the count on
603238104Sdes * \param[in] count the number of rr in this list
604238104Sdes * \return void
605238104Sdes */
606238104Sdesvoid ldns_rr_list_set_rr_count(ldns_rr_list *rr_list, size_t count);
607238104Sdes
608238104Sdes/**
609238104Sdes * set a rr on a specific index in a ldns_rr_list
610238104Sdes * \param[in] rr_list the rr_list to use
611238104Sdes * \param[in] r the rr to set
612238104Sdes * \param[in] count index into the rr_list
613238104Sdes * \return the old rr which was stored in the rr_list, or
614238104Sdes * NULL is the index was too large
615238104Sdes * set a specific rr */
616238104Sdesldns_rr * ldns_rr_list_set_rr(ldns_rr_list *rr_list, const ldns_rr *r, size_t count);
617238104Sdes
618238104Sdes/**
619238104Sdes * returns a specific rr of an rrlist.
620238104Sdes * \param[in] rr_list the rr_list to read from
621238104Sdes * \param[in] nr return this rr
622238104Sdes * \return the rr at position nr
623238104Sdes */
624238104Sdesldns_rr* ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr);
625238104Sdes
626238104Sdes/**
627238104Sdes * creates a new rr_list structure.
628238104Sdes * \return a new rr_list structure
629238104Sdes */
630246827Sdesldns_rr_list* ldns_rr_list_new(void);
631238104Sdes
632238104Sdes/**
633238104Sdes * frees an rr_list structure.
634238104Sdes * \param[in] rr_list the list to free
635238104Sdes */
636238104Sdesvoid ldns_rr_list_free(ldns_rr_list *rr_list);
637238104Sdes
638238104Sdes/**
639238104Sdes * frees an rr_list structure and all rrs contained therein.
640238104Sdes * \param[in] rr_list the list to free
641238104Sdes */
642238104Sdesvoid ldns_rr_list_deep_free(ldns_rr_list *rr_list);
643238104Sdes
644238104Sdes/**
645238104Sdes * concatenates two ldns_rr_lists together. This modifies
646238104Sdes * *left (to extend it and add the pointers from *right).
647238104Sdes * \param[in] left the leftside
648238104Sdes * \param[in] right the rightside
649238104Sdes * \return a left with right concatenated to it
650238104Sdes */
651238104Sdesbool ldns_rr_list_cat(ldns_rr_list *left, ldns_rr_list *right);
652238104Sdes
653238104Sdes/**
654238104Sdes * concatenates two ldns_rr_lists together, but makes clones of the rr's
655238104Sdes * (instead of pointer copying).
656238104Sdes * \param[in] left the leftside
657238104Sdes * \param[in] right the rightside
658238104Sdes * \return a new rr_list with leftside/rightside concatenated
659238104Sdes */
660238104Sdesldns_rr_list* ldns_rr_list_cat_clone(ldns_rr_list *left, ldns_rr_list *right);
661238104Sdes
662238104Sdes/**
663238104Sdes * pushes an rr to an rrlist.
664238104Sdes * \param[in] rr_list the rr_list to push to
665238104Sdes * \param[in] rr the rr to push
666238104Sdes * \return false on error, otherwise true
667238104Sdes */
668238104Sdesbool ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr);
669238104Sdes
670238104Sdes/**
671238104Sdes * pushes an rr_list to an rrlist.
672238104Sdes * \param[in] rr_list the rr_list to push to
673238104Sdes * \param[in] push_list the rr_list to push
674238104Sdes * \return false on error, otherwise true
675238104Sdes */
676238104Sdesbool ldns_rr_list_push_rr_list(ldns_rr_list *rr_list, const ldns_rr_list *push_list);
677238104Sdes
678238104Sdes/**
679238104Sdes * pops the last rr from an rrlist.
680238104Sdes * \param[in] rr_list the rr_list to pop from
681238104Sdes * \return NULL if nothing to pop. Otherwise the popped RR
682238104Sdes */
683238104Sdesldns_rr* ldns_rr_list_pop_rr(ldns_rr_list *rr_list);
684238104Sdes
685238104Sdes/**
686238104Sdes * pops an  rr_list of size s from an rrlist.
687238104Sdes * \param[in] rr_list the rr_list to pop from
688238104Sdes * \param[in] size the number of rr's to pop
689238104Sdes * \return NULL if nothing to pop. Otherwise the popped rr_list
690238104Sdes */
691238104Sdesldns_rr_list* ldns_rr_list_pop_rr_list(ldns_rr_list *rr_list, size_t size);
692238104Sdes
693238104Sdes/**
694238104Sdes * returns true if the given rr is one of the rrs in the
695238104Sdes * list, or if it is equal to one
696238104Sdes * \param[in] rr_list the rr_list to check
697238104Sdes * \param[in] rr the rr to check
698238104Sdes * \return true if rr_list contains rr, false otherwise
699238104Sdes */
700238104Sdesbool ldns_rr_list_contains_rr(const ldns_rr_list *rr_list, ldns_rr *rr);
701238104Sdes
702238104Sdes/**
703238104Sdes * checks if an rr_list is a rrset.
704238104Sdes * \param[in] rr_list the rr_list to check
705238104Sdes * \return true if it is an rrset otherwise false
706238104Sdes */
707238104Sdesbool ldns_is_rrset(ldns_rr_list *rr_list);
708238104Sdes
709238104Sdes/**
710238104Sdes * pushes an rr to an rrset (which really are rr_list's).
711238104Sdes * \param[in] *rr_list the rrset to push the rr to
712238104Sdes * \param[in] *rr the rr to push
713238104Sdes * \return true if the push succeeded otherwise false
714238104Sdes */
715238104Sdesbool ldns_rr_set_push_rr(ldns_rr_list *rr_list, ldns_rr *rr);
716238104Sdes
717238104Sdes/**
718238104Sdes * pops the last rr from an rrset. This function is there only
719238104Sdes * for the symmetry.
720238104Sdes * \param[in] rr_list the rr_list to pop from
721238104Sdes * \return NULL if nothing to pop. Otherwise the popped RR
722238104Sdes *
723238104Sdes */
724238104Sdesldns_rr* ldns_rr_set_pop_rr(ldns_rr_list *rr_list);
725238104Sdes
726238104Sdes/**
727238104Sdes * pops the first rrset from the list,
728238104Sdes * the list must be sorted, so that all rr's from each rrset
729238104Sdes * are next to each other
730238104Sdes */
731238104Sdesldns_rr_list *ldns_rr_list_pop_rrset(ldns_rr_list *rr_list);
732238104Sdes
733238104Sdes
734238104Sdes/**
735238104Sdes * retrieves a rrtype by looking up its name.
736238104Sdes * \param[in] name a string with the name
737238104Sdes * \return the type which corresponds with the name
738238104Sdes */
739238104Sdesldns_rr_type ldns_get_rr_type_by_name(const char *name);
740238104Sdes
741238104Sdes/**
742238104Sdes * retrieves a class by looking up its name.
743238104Sdes * \param[in] name string with the name
744238104Sdes * \return the cass which corresponds with the name
745238104Sdes */
746238104Sdesldns_rr_class ldns_get_rr_class_by_name(const char *name);
747238104Sdes
748238104Sdes/**
749238104Sdes * clones a rr and all its data
750238104Sdes * \param[in] rr the rr to clone
751238104Sdes * \return the new rr or NULL on failure
752238104Sdes */
753238104Sdesldns_rr* ldns_rr_clone(const ldns_rr *rr);
754238104Sdes
755238104Sdes/**
756238104Sdes * clones an rrlist.
757238104Sdes * \param[in] rrlist the rrlist to clone
758238104Sdes * \return the cloned rr list
759238104Sdes */
760238104Sdesldns_rr_list* ldns_rr_list_clone(const ldns_rr_list *rrlist);
761238104Sdes
762238104Sdes/**
763238104Sdes * sorts an rr_list (canonical wire format). the sorting is done inband.
764238104Sdes * \param[in] unsorted the rr_list to be sorted
765238104Sdes * \return void
766238104Sdes */
767238104Sdesvoid ldns_rr_list_sort(ldns_rr_list *unsorted);
768238104Sdes
769238104Sdes/**
770238104Sdes * compares two rrs. The TTL is not looked at.
771238104Sdes * \param[in] rr1 the first one
772238104Sdes * \param[in] rr2 the second one
773238104Sdes * \return 0 if equal
774238104Sdes *         -1 if rr1 comes before rr2
775238104Sdes *         +1 if rr2 comes before rr1
776238104Sdes */
777238104Sdesint ldns_rr_compare(const ldns_rr *rr1, const ldns_rr *rr2);
778238104Sdes
779238104Sdes/**
780238104Sdes * compares two rrs, up to the rdata.
781238104Sdes * \param[in] rr1 the first one
782238104Sdes * \param[in] rr2 the second one
783238104Sdes * \return 0 if equal
784238104Sdes *         -1 if rr1 comes before rr2
785238104Sdes *         +1 if rr2 comes before rr1
786238104Sdes */
787238104Sdesint ldns_rr_compare_no_rdata(const ldns_rr *rr1, const ldns_rr *rr2);
788238104Sdes
789238104Sdes/**
790238104Sdes * compares the wireformat of two rrs, contained in the given buffers.
791238104Sdes * \param[in] rr1_buf the first one
792238104Sdes * \param[in] rr2_buf the second one
793238104Sdes * \return 0 if equal
794238104Sdes *         -1 if rr1_buf comes before rr2_buf
795238104Sdes *         +1 if rr2_buf comes before rr1_buf
796238104Sdes */
797238104Sdesint ldns_rr_compare_wire(ldns_buffer *rr1_buf, ldns_buffer *rr2_buf);
798238104Sdes
799238104Sdes/**
800238104Sdes * returns true of the given rr's are equal.
801238104Sdes * Also returns true if one record is a DS that represents the
802238104Sdes * same DNSKEY record as the other record
803238104Sdes * \param[in] rr1 the first rr
804238104Sdes * \param[in] rr2 the second rr
805238104Sdes * \return true if equal otherwise false
806238104Sdes */
807238104Sdesbool ldns_rr_compare_ds(const ldns_rr *rr1, const ldns_rr *rr2);
808238104Sdes
809238104Sdes/**
810238104Sdes * compares two rr listss.
811238104Sdes * \param[in] rrl1 the first one
812238104Sdes * \param[in] rrl2 the second one
813238104Sdes * \return 0 if equal
814238104Sdes *         -1 if rrl1 comes before rrl2
815238104Sdes *         +1 if rrl2 comes before rrl1
816238104Sdes */
817238104Sdesint ldns_rr_list_compare(const ldns_rr_list *rrl1, const ldns_rr_list *rrl2);
818238104Sdes
819238104Sdes/**
820238104Sdes * calculates the uncompressed size of an RR.
821238104Sdes * \param[in] r the rr to operate on
822238104Sdes * \return size of the rr
823238104Sdes */
824238104Sdessize_t ldns_rr_uncompressed_size(const ldns_rr *r);
825238104Sdes
826238104Sdes/**
827238104Sdes * converts each dname in a rr to its canonical form.
828238104Sdes * \param[in] rr the rr to work on
829238104Sdes * \return void
830238104Sdes */
831238104Sdesvoid ldns_rr2canonical(ldns_rr *rr);
832238104Sdes
833238104Sdes/**
834238104Sdes * converts each dname in each rr in a rr_list to its canonical form.
835238104Sdes * \param[in] rr_list the rr_list to work on
836238104Sdes * \return void
837238104Sdes */
838238104Sdesvoid ldns_rr_list2canonical(ldns_rr_list *rr_list);
839238104Sdes
840238104Sdes/**
841238104Sdes * counts the number of labels of the ownername.
842238104Sdes * \param[in] rr count the labels of this rr
843238104Sdes * \return the number of labels
844238104Sdes */
845238104Sdesuint8_t ldns_rr_label_count(ldns_rr *rr);
846238104Sdes
847238104Sdes/**
848238104Sdes * returns the resource record descriptor for the given rr type.
849238104Sdes *
850238104Sdes * \param[in] type the type value of the rr type
851238104Sdes *\return the ldns_rr_descriptor for this type
852238104Sdes */
853238104Sdesconst ldns_rr_descriptor *ldns_rr_descript(uint16_t type);
854238104Sdes
855238104Sdes/**
856238104Sdes * returns the minimum number of rdata fields of the rr type this descriptor describes.
857238104Sdes *
858238104Sdes * \param[in]  descriptor for an rr type
859238104Sdes * \return the minimum number of rdata fields
860238104Sdes */
861238104Sdessize_t ldns_rr_descriptor_minimum(const ldns_rr_descriptor *descriptor);
862238104Sdes
863238104Sdes/**
864238104Sdes * returns the maximum number of rdata fields of the rr type this descriptor describes.
865238104Sdes *
866238104Sdes * \param[in]  descriptor for an rr type
867238104Sdes * \return the maximum number of rdata fields
868238104Sdes */
869238104Sdessize_t ldns_rr_descriptor_maximum(const ldns_rr_descriptor *descriptor);
870238104Sdes
871238104Sdes/**
872238104Sdes * returns the rdf type for the given rdata field number of the rr type for the given descriptor.
873238104Sdes *
874238104Sdes * \param[in] descriptor for an rr type
875238104Sdes * \param[in] field the field number
876238104Sdes * \return the rdf type for the field
877238104Sdes */
878238104Sdesldns_rdf_type ldns_rr_descriptor_field_type(const ldns_rr_descriptor *descriptor, size_t field);
879238104Sdes
880238104Sdes/**
881238104Sdes * Return the rr_list which matches the rdf at position field. Think
882238104Sdes * type-covered stuff for RRSIG
883238104Sdes *
884238104Sdes * \param[in] l the rr_list to look in
885238104Sdes * \param[in] r the rdf to use for the comparison
886238104Sdes * \param[in] pos at which position can we find the rdf
887238104Sdes *
888238104Sdes * \return a new rr list with only the RRs that match
889238104Sdes *
890238104Sdes */
891238104Sdesldns_rr_list *ldns_rr_list_subtype_by_rdf(ldns_rr_list *l, ldns_rdf *r, size_t pos);
892238104Sdes
893238104Sdes/**
894238104Sdes * convert an rdf of type LDNS_RDF_TYPE_TYPE to an actual
895238104Sdes * LDNS_RR_TYPE. This is usefull in the case when inspecting
896238104Sdes * the rrtype covered field of an RRSIG.
897238104Sdes * \param[in] rd the rdf to look at
898238104Sdes * \return a ldns_rr_type with equivalent LDNS_RR_TYPE
899238104Sdes *
900238104Sdes */
901238104Sdesldns_rr_type    ldns_rdf2rr_type(const ldns_rdf *rd);
902238104Sdes
903238104Sdes/**
904238104Sdes * Returns the type of the first element of the RR
905238104Sdes * If there are no elements present, 0 is returned
906238104Sdes *
907238104Sdes * \param[in] rr_list The rr list
908238104Sdes * \return rr_type of the first element, or 0 if the list is empty
909238104Sdes */
910238104Sdesldns_rr_type
911238104Sdesldns_rr_list_type(const ldns_rr_list *rr_list);
912238104Sdes
913238104Sdes/**
914238104Sdes * Returns the owner domain name rdf of the first element of the RR
915238104Sdes * If there are no elements present, NULL is returned
916238104Sdes *
917238104Sdes * \param[in] rr_list The rr list
918238104Sdes * \return dname of the first element, or NULL if the list is empty
919238104Sdes */
920238104Sdesldns_rdf *
921238104Sdesldns_rr_list_owner(const ldns_rr_list *rr_list);
922238104Sdes
923238104Sdes#ifdef __cplusplus
924238104Sdes}
925238104Sdes#endif
926238104Sdes
927238104Sdes#endif /* LDNS_RR_H */
928