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 
53 bool
55 {
56  return ldns_rr_list_push_rr( ldns_zone_rrs(z), rr);
57 }
58 
59 
60 /*
61  * Get the list of glue records in a zone
62  * XXX: there should be a way for this to return error, other than NULL,
63  * since NULL is a valid return
64  */
67 {
68  /* when do we find glue? It means we find an IP address
69  * (AAAA/A) for a nameserver listed in the zone
70  *
71  * Alg used here:
72  * first find all the zonecuts (NS records)
73  * find all the AAAA or A records (can be done it the
74  * above loop).
75  *
76  * Check if the aaaa/a list are subdomains under the
77  * NS domains.
78  * If yes -> glue, if no -> not glue
79  */
80 
81  ldns_rr_list *zone_cuts;
82  ldns_rr_list *addr;
83  ldns_rr_list *glue;
84  ldns_rr *r, *ns, *a;
85  ldns_rdf *dname_a, *ns_owner;
86  size_t i,j;
87 
88  zone_cuts = NULL;
89  addr = NULL;
90  glue = NULL;
91 
92  /* we cannot determine glue in a 'zone' without a SOA */
93  if (!ldns_zone_soa(z)) {
94  return NULL;
95  }
96 
97  zone_cuts = ldns_rr_list_new();
98  if (!zone_cuts) goto memory_error;
99  addr = ldns_rr_list_new();
100  if (!addr) goto memory_error;
101  glue = ldns_rr_list_new();
102  if (!glue) goto memory_error;
103 
104  for(i = 0; i < ldns_zone_rr_count(z); i++) {
105  r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
106  if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
108  /* possibly glue */
109  if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
110  continue;
111  }
112  if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
113  /* multiple zones will end up here -
114  * for now; not a problem
115  */
116  /* don't add NS records for the current zone itself */
118  ldns_rr_owner(ldns_zone_soa(z))) != 0) {
119  if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
120  }
121  continue;
122  }
123  }
124 
125  /* will sorting make it quicker ?? */
126  for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
127  ns = ldns_rr_list_rr(zone_cuts, i);
128  ns_owner = ldns_rr_owner(ns);
129 
130  for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
131  a = ldns_rr_list_rr(addr, j);
132  dname_a = ldns_rr_owner(a);
133 
134  if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
135  ldns_dname_compare(dname_a, ns_owner) == 0) {
136  /* GLUE! */
137  if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
138  }
139  }
140  }
141 
142  ldns_rr_list_free(addr);
143  ldns_rr_list_free(zone_cuts);
144 
145  if (ldns_rr_list_rr_count(glue) == 0) {
146  ldns_rr_list_free(glue);
147  return NULL;
148  } else {
149  return glue;
150  }
151 
152 memory_error:
153  if (zone_cuts) {
154  LDNS_FREE(zone_cuts);
155  }
156  if (addr) {
157  ldns_rr_list_free(addr);
158  }
159  if (glue) {
160  ldns_rr_list_free(glue);
161  }
162  return NULL;
163 }
164 
165 ldns_zone *
167 {
168  ldns_zone *z;
169 
170  z = LDNS_MALLOC(ldns_zone);
171  if (!z) {
172  return NULL;
173  }
174 
175  z->_rrs = ldns_rr_list_new();
176  if (!z->_rrs) {
177  LDNS_FREE(z);
178  return NULL;
179  }
180  ldns_zone_set_soa(z, NULL);
181  return z;
182 }
183 
184 /* we regocnize:
185  * $TTL, $ORIGIN
186  */
188 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
189 {
190  return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
191 }
192 
193 /* XXX: class is never used */
195 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl,
196  ldns_rr_class ATTR_UNUSED(c), int *line_nr)
197 {
198  ldns_zone *newzone;
199  ldns_rr *rr;
200  uint32_t my_ttl;
201  ldns_rdf *my_origin;
202  ldns_rdf *my_prev;
203  bool soa_seen = false; /* 2 soa are an error */
204  ldns_status s;
205  ldns_status ret;
206 
207  /* most cases of error are memory problems */
208  ret = LDNS_STATUS_MEM_ERR;
209 
210  newzone = NULL;
211  my_origin = NULL;
212  my_prev = NULL;
213 
214  my_ttl = ttl;
215 
216  if (origin) {
217  my_origin = ldns_rdf_clone(origin);
218  if (!my_origin) goto error;
219  /* also set the prev */
220  my_prev = ldns_rdf_clone(origin);
221  if (!my_prev) goto error;
222  }
223 
224  newzone = ldns_zone_new();
225  if (!newzone) goto error;
226 
227  while(!feof(fp)) {
228  s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
229  switch (s) {
230  case LDNS_STATUS_OK:
231  if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
232  if (soa_seen) {
233  /* second SOA
234  * just skip, maybe we want to say
235  * something??? */
236  ldns_rr_free(rr);
237  continue;
238  }
239  soa_seen = true;
240  ldns_zone_set_soa(newzone, rr);
241  /* set origin to soa if not specified */
242  if (!my_origin) {
243  my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
244  }
245  continue;
246  }
247 
248  /* a normal RR - as sofar the DNS is normal */
249  if (!ldns_zone_push_rr(newzone, rr)) goto error;
250 
252  /* empty line was seen */
254  /* the function set the ttl */
255  break;
257  /* the function set the origin */
258  break;
261  break;
262  default:
263  ret = s;
264  goto error;
265  }
266  }
267 
268  if (my_origin) {
269  ldns_rdf_deep_free(my_origin);
270  }
271  if (my_prev) {
272  ldns_rdf_deep_free(my_prev);
273  }
274  if (z) {
275  *z = newzone;
276  } else {
277  ldns_zone_free(newzone);
278  }
279 
280  return LDNS_STATUS_OK;
281 
282 error:
283  if (my_origin) {
284  ldns_rdf_deep_free(my_origin);
285  }
286  if (my_prev) {
287  ldns_rdf_deep_free(my_prev);
288  }
289  if (newzone) {
290  ldns_zone_free(newzone);
291  }
292  return ret;
293 }
294 
295 void
297 {
298  ldns_rr_list *zrr;
299  assert(zone != NULL);
300 
301  zrr = ldns_zone_rrs(zone);
302  ldns_rr_list_sort(zrr);
303 }
304 
305 void
307 {
308  ldns_rr_list_free(zone->_rrs);
309  LDNS_FREE(zone);
310 }
311 
312 void
314 {
315  ldns_rr_free(zone->_soa);
317  LDNS_FREE(zone);
318 }
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone's contents.
Definition: zone.c:41
ldns_rr * ldns_zone_soa(const ldns_zone *z)
Return the soa record of a zone.
Definition: zone.c:17
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition: rdata.c:230
a host address
Definition: rr.h:83
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:66
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:356
enum ldns_enum_rr_class ldns_rr_class
Definition: rr.h:64
List or Set of Resource Records.
Definition: rr.h:327
_Bool ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
push an single rr to a zone structure.
Definition: zone.c:54
ldns_status ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c __attribute__((unused)), int *line_nr)
Definition: zone.c:195
ldns_rr_list * _rrs
Definition: zone.h:47
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition: rr.c:984
DNS Zone.
Definition: zone.h:42
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition: rdata.c:222
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:75
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition: rr.c:975
void ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
Set the zone's soa record.
Definition: zone.c:29
Resource Record.
Definition: rr.h:299
Including this file will include all ldns files, and define some lookup tables.
ipv6 address
Definition: rr.h:137
marks the start of a zone of authority
Definition: rr.h:93
ldns_status ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
Create a new zone from a file.
Definition: zone.c:188
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:954
_Bool ldns_zone_push_rr_list(ldns_zone *z, ldns_rr_list *list)
push an rrlist to a zone structure.
Definition: zone.c:47
#define ATTR_UNUSED(x)
Definition: common.h:67
ldns_rr * _soa
the soa defines a zone
Definition: zone.h:45
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition: rr.c:964
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_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:313
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:907
enum ldns_enum_status ldns_status
Definition: error.h:131
#define LDNS_MALLOC(type)
Memory management macros.
Definition: util.h:49
ldns_status ldns_rr_new_frm_fp_l(ldns_rr **newrr, FILE *fp, uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev, int *line_nr)
creates a new rr from a file containing a string.
Definition: rr.c:680
_Bool ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
Definition: rr.c:1096
_Bool ldns_rr_list_cat(ldns_rr_list *left, ldns_rr_list *right)
concatenates two ldns_rr_lists together.
Definition: rr.c:1000
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone's content.
Definition: zone.c:35
Resource record data field.
Definition: rdata.h:166
ldns_rdf * ldns_rr_owner(const ldns_rr *rr)
returns the owner name of an rr structure.
Definition: rr.c:883
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:921
#define LDNS_FREE(ptr)
Definition: util.h:60
an authoritative name server
Definition: rr.h:85
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition: zone.c:296
_Bool ldns_dname_is_subdomain(const ldns_rdf *sub, const ldns_rdf *parent)
test wether the name sub falls under parent (i.e.
Definition: dname.c:293
void ldns_zone_free(ldns_zone *zone)
Frees the allocated memory for the zone, and the rr_list structure in it.
Definition: zone.c:306
ldns_zone * ldns_zone_new(void)
create a new ldns_zone structure
Definition: zone.c:166
void ldns_rr_list_sort(ldns_rr_list *unsorted)
sorts an rr_list (canonical wire format).
Definition: rr.c:1479
int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2)
compares two rdf's on their wire formats.
Definition: rdata.c:642