zone.c
Go to the documentation of this file.
1 /* zone.c
2  *
3  * Functions for ldns_zone structure
4  * a Net::DNS like library for C
5  *
6  * (c) NLnet Labs, 2005-2006
7  * See the file LICENSE for the license
8  */
9 #include <ldns/config.h>
10 
11 #include <ldns/ldns.h>
12 
13 #include <strings.h>
14 #include <limits.h>
15 
16 ldns_rr *
18 {
19  return z->_soa;
20 }
21 
22 size_t
24 {
25  return ldns_rr_list_rr_count(z->_rrs);
26 }
27 
28 void
30 {
31  z->_soa = soa;
32 }
33 
36 {
37  return z->_rrs;
38 }
39 
40 void
42 {
43  z->_rrs = rrlist;
44 }
45 
46 bool
48 {
49  return ldns_rr_list_cat(ldns_zone_rrs(z), list);
50 }
51 
52 bool
54 {
55  return ldns_rr_list_push_rr(ldns_zone_rrs(z), rr);
56 }
57 
58 
59 /*
60  * Get the list of glue records in a zone
61  * XXX: there should be a way for this to return error, other than NULL,
62  * since NULL is a valid return
63  */
66 {
67  /* when do we find glue? It means we find an IP address
68  * (AAAA/A) for a nameserver listed in the zone
69  *
70  * Alg used here:
71  * first find all the zonecuts (NS records)
72  * find all the AAAA or A records (can be done it the
73  * above loop).
74  *
75  * Check if the aaaa/a list are subdomains under the
76  * NS domains.
77  * If yes -> glue, if no -> not glue
78  */
79 
80  ldns_rr_list *zone_cuts;
81  ldns_rr_list *addr;
82  ldns_rr_list *glue;
83  ldns_rr *r, *ns, *a;
84  ldns_rdf *dname_a, *ns_owner;
85  size_t i,j;
86 
87  zone_cuts = NULL;
88  addr = NULL;
89  glue = NULL;
90 
91  /* we cannot determine glue in a 'zone' without a SOA */
92  if (!ldns_zone_soa(z)) {
93  return NULL;
94  }
95 
96  zone_cuts = ldns_rr_list_new();
97  if (!zone_cuts) goto memory_error;
98  addr = ldns_rr_list_new();
99  if (!addr) goto memory_error;
100  glue = ldns_rr_list_new();
101  if (!glue) goto memory_error;
102 
103  for(i = 0; i < ldns_zone_rr_count(z); i++) {
104  r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
105  if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
107  /* possibly glue */
108  if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
109  continue;
110  }
111  if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
112  /* multiple zones will end up here -
113  * for now; not a problem
114  */
115  /* don't add NS records for the current zone itself */
117  ldns_rr_owner(ldns_zone_soa(z))) != 0) {
118  if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
119  }
120  continue;
121  }
122  }
123 
124  /* will sorting make it quicker ?? */
125  for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
126  ns = ldns_rr_list_rr(zone_cuts, i);
127  ns_owner = ldns_rr_owner(ns);
128 
129  for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
130  a = ldns_rr_list_rr(addr, j);
131  dname_a = ldns_rr_owner(a);
132 
133  if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
134  ldns_dname_compare(dname_a, ns_owner) == 0) {
135  /* GLUE! */
136  if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
137  }
138  }
139  }
140 
141  ldns_rr_list_free(addr);
142  ldns_rr_list_free(zone_cuts);
143 
144  if (ldns_rr_list_rr_count(glue) == 0) {
145  ldns_rr_list_free(glue);
146  return NULL;
147  } else {
148  return glue;
149  }
150 
151 memory_error:
152  if (zone_cuts) {
153  LDNS_FREE(zone_cuts);
154  }
155  if (addr) {
156  ldns_rr_list_free(addr);
157  }
158  if (glue) {
159  ldns_rr_list_free(glue);
160  }
161  return NULL;
162 }
163 
164 ldns_zone *
166 {
167  ldns_zone *z;
168 
169  z = LDNS_MALLOC(ldns_zone);
170  if (!z) {
171  return NULL;
172  }
173 
174  z->_rrs = ldns_rr_list_new();
175  if (!z->_rrs) {
176  LDNS_FREE(z);
177  return NULL;
178  }
179  ldns_zone_set_soa(z, NULL);
180  return z;
181 }
182 
183 /* we recognize:
184  * $TTL, $ORIGIN
185  */
187 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
188 {
189  return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
190 }
191 
193  uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev,
194  int *line_nr, bool *explicit_ttl);
195 
196 /* XXX: class is never used */
198 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, const ldns_rdf *origin,
199  uint32_t default_ttl, ldns_rr_class ATTR_UNUSED(c), int *line_nr)
200 {
201  ldns_zone *newzone;
202  ldns_rr *rr, *prev_rr = NULL;
203  uint32_t my_ttl;
204  ldns_rdf *my_origin;
205  ldns_rdf *my_prev;
206  bool soa_seen = false; /* 2 soa are an error */
207  ldns_status s;
208  ldns_status ret;
209  /* RFC 1035 Section 5.1, says 'Omitted class and TTL values are default
210  * to the last explicitly stated values.'
211  */
212  bool ttl_from_TTL = false;
213  bool explicit_ttl = false;
214 
215  /* most cases of error are memory problems */
216  ret = LDNS_STATUS_MEM_ERR;
217 
218  newzone = NULL;
219  my_origin = NULL;
220  my_prev = NULL;
221 
222  my_ttl = default_ttl;
223 
224  if (origin) {
225  my_origin = ldns_rdf_clone(origin);
226  if (!my_origin) goto error;
227  /* also set the prev */
228  my_prev = ldns_rdf_clone(origin);
229  if (!my_prev) goto error;
230  }
231 
232  newzone = ldns_zone_new();
233  if (!newzone) goto error;
234 
235  while(!feof(fp)) {
236  /* If ttl came from $TTL line, then it should be the default.
237  * (RFC 2308 Section 4)
238  * Otherwise it "defaults to the last explicitly stated value"
239  * (RFC 1035 Section 5.1)
240  */
241  if (ttl_from_TTL)
242  my_ttl = default_ttl;
243  s = _ldns_rr_new_frm_fp_l_internal(&rr, fp, &my_ttl, &my_origin,
244  &my_prev, line_nr, &explicit_ttl);
245  switch (s) {
246  case LDNS_STATUS_OK:
247  if (explicit_ttl) {
248  if (!ttl_from_TTL) {
249  /* No $TTL, so ttl "defaults to the
250  * last explicitly stated value"
251  * (RFC 1035 Section 5.1)
252  */
253  my_ttl = ldns_rr_ttl(rr);
254  }
255  /* When ttl is implicit, try to adhere to the rules as
256  * much as posssible. (also for compatibility with bind)
257  * This was changed when fixing an issue with ZONEMD
258  * which hashes the TTL too.
259  */
260  } else if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SIG
262  if (ldns_rr_rd_count(rr) >= 4
264 
265  /* SIG without explicit ttl get ttl
266  * from the original_ttl field
267  * (RFC 2535 Section 7.2)
268  *
269  * Similarly for RRSIG, but stated less
270  * specifically in the spec.
271  * (RFC 4034 Section 3)
272  */
273  ldns_rr_set_ttl(rr,
275  ldns_rr_rdf(rr, 3)));
276 
277  } else if (prev_rr
278  && ldns_rr_get_type(prev_rr) == ldns_rr_get_type(rr)
279  && ldns_dname_compare( ldns_rr_owner(prev_rr)
280  , ldns_rr_owner(rr)) == 0)
281 
282  /* "TTLs of all RRs in an RRSet must be the same"
283  * (RFC 2881 Section 5.2)
284  */
285  ldns_rr_set_ttl(rr, ldns_rr_ttl(prev_rr));
286 
287  prev_rr = rr;
288  if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
289  if (soa_seen) {
290  /* second SOA
291  * just skip, maybe we want to say
292  * something??? */
293  ldns_rr_free(rr);
294  continue;
295  }
296  soa_seen = true;
297  ldns_zone_set_soa(newzone, rr);
298  /* set origin to soa if not specified */
299  if (!my_origin) {
300  my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
301  }
302  continue;
303  }
304 
305  /* a normal RR - as sofar the DNS is normal */
306  if (!ldns_zone_push_rr(newzone, rr)) {
307  ldns_rr_free(rr);
308  goto error;
309  }
310  break;
311 
313  /* empty line was seen */
315  /* the function set the ttl */
316  default_ttl = my_ttl;
317  ttl_from_TTL = true;
318  break;
320  /* the function set the origin */
321  break;
324  goto error;
325  default:
326  ret = s;
327  goto error;
328  }
329  }
330 
331  if (my_origin) {
332  ldns_rdf_deep_free(my_origin);
333  }
334  if (my_prev) {
335  ldns_rdf_deep_free(my_prev);
336  }
337  if (z) {
338  *z = newzone;
339  } else {
340  ldns_zone_free(newzone);
341  }
342 
343  return LDNS_STATUS_OK;
344 
345 error:
346  if (my_origin) {
347  ldns_rdf_deep_free(my_origin);
348  }
349  if (my_prev) {
350  ldns_rdf_deep_free(my_prev);
351  }
352  if (newzone) {
353  ldns_zone_free(newzone);
354  }
355  return ret;
356 }
357 
358 void
360 {
361  ldns_rr_list *zrr;
362  assert(zone != NULL);
363 
364  zrr = ldns_zone_rrs(zone);
365  ldns_rr_list_sort(zrr);
366 }
367 
368 void
370 {
371  ldns_rr_list_free(zone->_rrs);
372  LDNS_FREE(zone);
373 }
374 
375 void
377 {
378  ldns_rr_free(zone->_soa);
380  LDNS_FREE(zone);
381 }
ldns_rdf * ldns_rr_rdf(const ldns_rr *rr, size_t nr)
returns the rdata field member counter.
Definition: rr.c:901
@ LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL
Definition: error.h:82
List or Set of Resource Records.
Definition: rr.h:337
enum ldns_enum_rr_class ldns_rr_class
Definition: rr.h:61
ldns_status ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t default_ttl, ldns_rr_class c __attribute__((unused)), int *line_nr)
Definition: zone.c:198
ldns_rr_list * ldns_zone_glue_rr_list(const ldns_zone *z)
Retrieve all resource records from the zone that are glue records.
Definition: zone.c:65
@ LDNS_RDF_TYPE_INT32
32 bits
Definition: rdata.h:56
@ LDNS_STATUS_SYNTAX_ORIGIN
Definition: error.h:89
ldns_zone * ldns_zone_new(void)
create a new ldns_zone structure
Definition: zone.c:165
Resource Record.
Definition: rr.h:309
#define LDNS_FREE(ptr)
Definition: util.h:60
@ LDNS_STATUS_OK
Definition: error.h:26
bool ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
push an single rr to a zone structure.
Definition: zone.c:53
bool ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
Definition: rr.c:1124
void ldns_rr_set_ttl(ldns_rr *rr, uint32_t ttl)
sets the ttl in the rr structure.
Definition: rr.c:808
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition: rr.c:1012
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone's content.
Definition: zone.c:35
@ LDNS_STATUS_SYNTAX_INCLUDE
Definition: error.h:90
size_t ldns_zone_rr_count(const ldns_zone *z)
Returns the number of resource records in the zone, NOT counting the SOA record.
Definition: zone.c:23
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:75
void ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
Set the zone's soa record.
Definition: zone.c:29
@ LDNS_STATUS_SYNTAX_EMPTY
Definition: error.h:91
@ LDNS_RR_TYPE_NS
an authoritative name server
Definition: rr.h:82
bool ldns_rr_list_cat(ldns_rr_list *left, const ldns_rr_list *right)
concatenates two ldns_rr_lists together.
Definition: rr.c:1028
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
Definition: rr.h:90
ldns_rr_list * _rrs
Definition: zone.h:47
int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2)
compares two rdf's on their wire formats.
Definition: rdata.c:657
bool ldns_dname_is_subdomain(const ldns_rdf *sub, const ldns_rdf *parent)
test whether the name sub falls under parent (i.e.
Definition: dname.c:296
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition: rdata.c:230
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition: rdata.c:222
#define LDNS_MALLOC(type)
Memory management macros.
Definition: util.h:49
uint32_t ldns_rdf2native_int32(const ldns_rdf *rd)
returns the native uint32_t representation from the rdf.
Definition: rdata.c:98
void ldns_rr_list_sort(ldns_rr_list *unsorted)
sorts an rr_list (canonical wire format).
Definition: rr.c:1507
enum ldns_enum_status ldns_status
Definition: error.h:146
#define ATTR_UNUSED(x)
Definition: common.h:72
ldns_status ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
Create a new zone from a file.
Definition: zone.c:187
ldns_rr * ldns_zone_soa(const ldns_zone *z)
Return the soa record of a zone.
Definition: zone.c:17
size_t ldns_rr_rd_count(const ldns_rr *rr)
returns the rd_count of an rr structure.
Definition: rr.c:929
Resource record data field.
Definition: rdata.h:195
uint32_t ldns_rr_ttl(const ldns_rr *rr)
returns the ttl of an rr structure.
Definition: rr.c:923
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition: zone.c:359
void ldns_zone_free(ldns_zone *zone)
Frees the allocated memory for the zone, and the rr_list structure in it.
Definition: zone.c:369
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone's contents.
Definition: zone.c:41
DNS Zone.
Definition: zone.h:42
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition: rr.c:992
@ LDNS_RR_TYPE_RRSIG
DNSSEC.
Definition: rr.h:170
ldns_rr * _soa
the soa defines a zone
Definition: zone.h:45
@ LDNS_RR_TYPE_A
a host address
Definition: rr.h:80
@ LDNS_STATUS_SYNTAX_TTL
Definition: error.h:88
ldns_status _ldns_rr_new_frm_fp_l_internal(ldns_rr **newrr, FILE *fp, uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev, int *line_nr, bool *explicit_ttl)
Definition: rr.c:719
int ldns_dname_compare(const ldns_rdf *dname1, const ldns_rdf *dname2)
Compares the two dname rdf's according to the algorithm for ordering in RFC4034 Section 6.
Definition: dname.c:359
bool ldns_zone_push_rr_list(ldns_zone *z, const ldns_rr_list *list)
push an rrlist to a zone structure.
Definition: zone.c:47
ldns_rr * ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
returns a specific rr of an rrlist.
Definition: rr.c:982
@ LDNS_STATUS_MEM_ERR
Definition: error.h:34
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition: rr.c:1003
void ldns_zone_deep_free(ldns_zone *zone)
Frees the allocated memory for the zone, the soa rr in it, and the rr_list structure in it,...
Definition: zone.c:376
ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd)
returns the type of the rdf.
Definition: rdata.c:31
ldns_rdf * ldns_rr_owner(const ldns_rr *rr)
returns the owner name of an rr structure.
Definition: rr.c:911
@ LDNS_RR_TYPE_SIG
2535typecode
Definition: rr.h:126
@ LDNS_RR_TYPE_AAAA
ipv6 address
Definition: rr.h:134
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:935
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr's in an rr_list.
Definition: rr.c:949