3 * Functions for ldns_zone structure
4 * a Net::DNS like library for C
6 * (c) NLnet Labs, 2005-2006
7 * See the file LICENSE for the license
9 #include <ldns/config.h>
11 #include <ldns/ldns.h>
17 ldns_zone_soa(const ldns_zone *z)
23 ldns_zone_rr_count(const ldns_zone *z)
25 return ldns_rr_list_rr_count(z->_rrs);
29 ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
35 ldns_zone_rrs(const ldns_zone *z)
41 ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
47 ldns_zone_push_rr_list(ldns_zone *z, ldns_rr_list *list)
49 return ldns_rr_list_cat(ldns_zone_rrs(z), list);
54 ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
56 return ldns_rr_list_push_rr( ldns_zone_rrs(z), rr);
60 /* return a clone of the given rr list, without the glue records
61 * rr list should be the complete zone
62 * if present, stripped records are added to the list *glue_records
65 ldns_zone_strip_glue_rrs(const ldns_rdf *zone_name, const ldns_rr_list *rrs, ldns_rr_list *glue_rrs)
67 ldns_rr_list *new_list;
69 /* when do we find glue? It means we find an IP address
70 * (AAAA/A) for a nameserver listed in the zone
73 * first find all the zonecuts (NS records)
74 * find all the AAAA or A records (can be done it the
77 * Check if the aaaa/a list are subdomains under the
79 * If yes -> glue, if no -> not glue
82 ldns_rr_list *zone_cuts;
85 ldns_rdf *dname_a, *ns_owner;
92 new_list = ldns_rr_list_new();
93 if (!new_list) goto memory_error;
94 zone_cuts = ldns_rr_list_new();
95 if (!zone_cuts) goto memory_error;
96 addr = ldns_rr_list_new();
97 if (!addr) goto memory_error;
99 for(i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
100 r = ldns_rr_list_rr(rrs, i);
101 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
102 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
104 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
107 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
108 /* multiple zones will end up here -
109 * for now; not a problem
111 /* don't add NS records for the current zone itself */
112 if (ldns_rdf_compare(ldns_rr_owner(r),
114 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
120 /* will sorting make it quicker ?? */
121 for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
122 ns = ldns_rr_list_rr(zone_cuts, i);
123 ns_owner = ldns_rr_owner(ns);
124 for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
125 a = ldns_rr_list_rr(addr, j);
126 dname_a = ldns_rr_owner(a);
128 if (ldns_dname_is_subdomain(dname_a, ns_owner)) {
131 if (!ldns_rr_list_push_rr(glue_rrs, a)) goto memory_error;
135 if (!ldns_rr_list_push_rr(new_list, a)) goto memory_error;
140 ldns_rr_list_free(addr);
141 ldns_rr_list_free(zone_cuts);
147 ldns_rr_list_free(new_list);
150 ldns_rr_list_free(zone_cuts);
153 ldns_rr_list_free(addr);
160 * Get the list of glue records in a zone
161 * XXX: there should be a way for this to return error, other than NULL,
162 * since NULL is a valid return
165 ldns_zone_glue_rr_list(const ldns_zone *z)
167 /* when do we find glue? It means we find an IP address
168 * (AAAA/A) for a nameserver listed in the zone
171 * first find all the zonecuts (NS records)
172 * find all the AAAA or A records (can be done it the
175 * Check if the aaaa/a list are subdomains under the
177 * If yes -> glue, if no -> not glue
180 ldns_rr_list *zone_cuts;
184 ldns_rdf *dname_a, *ns_owner;
191 /* we cannot determine glue in a 'zone' without a SOA */
192 if (!ldns_zone_soa(z)) {
196 zone_cuts = ldns_rr_list_new();
197 if (!zone_cuts) goto memory_error;
198 addr = ldns_rr_list_new();
199 if (!addr) goto memory_error;
200 glue = ldns_rr_list_new();
201 if (!glue) goto memory_error;
203 for(i = 0; i < ldns_zone_rr_count(z); i++) {
204 r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
205 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
206 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
208 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
211 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
212 /* multiple zones will end up here -
213 * for now; not a problem
215 /* don't add NS records for the current zone itself */
216 if (ldns_rdf_compare(ldns_rr_owner(r),
217 ldns_rr_owner(ldns_zone_soa(z))) != 0) {
218 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
224 /* will sorting make it quicker ?? */
225 for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
226 ns = ldns_rr_list_rr(zone_cuts, i);
227 ns_owner = ldns_rr_owner(ns);
229 for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
230 a = ldns_rr_list_rr(addr, j);
231 dname_a = ldns_rr_owner(a);
233 if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
234 ldns_dname_compare(dname_a, ns_owner) == 0) {
236 if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
241 ldns_rr_list_free(addr);
242 ldns_rr_list_free(zone_cuts);
244 if (ldns_rr_list_rr_count(glue) == 0) {
245 ldns_rr_list_free(glue);
253 LDNS_FREE(zone_cuts);
256 ldns_rr_list_free(addr);
259 ldns_rr_list_free(glue);
269 z = LDNS_MALLOC(ldns_zone);
274 z->_rrs = ldns_rr_list_new();
279 ldns_zone_set_soa(z, NULL);
287 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
289 return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
292 /* XXX: class is never used */
294 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl,
295 ldns_rr_class ATTR_UNUSED(c), int *line_nr)
302 bool soa_seen = false; /* 2 soa are an error */
306 /* most cases of error are memory problems */
307 ret = LDNS_STATUS_MEM_ERR;
316 my_origin = ldns_rdf_clone(origin);
317 if (!my_origin) goto error;
318 /* also set the prev */
319 my_prev = ldns_rdf_clone(origin);
320 if (!my_prev) goto error;
323 newzone = ldns_zone_new();
324 if (!newzone) goto error;
327 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
330 if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
333 * just skip, maybe we want to say
339 ldns_zone_set_soa(newzone, rr);
340 /* set origin to soa if not specified */
342 my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
347 /* a normal RR - as sofar the DNS is normal */
348 if (!ldns_zone_push_rr(newzone, rr)) goto error;
350 case LDNS_STATUS_SYNTAX_EMPTY:
351 /* empty line was seen */
352 case LDNS_STATUS_SYNTAX_TTL:
353 /* the function set the ttl */
355 case LDNS_STATUS_SYNTAX_ORIGIN:
356 /* the function set the origin */
358 case LDNS_STATUS_SYNTAX_INCLUDE:
359 ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL;
368 ldns_rdf_deep_free(my_origin);
371 ldns_rdf_deep_free(my_prev);
376 ldns_zone_free(newzone);
379 return LDNS_STATUS_OK;
383 ldns_rdf_deep_free(my_origin);
386 ldns_rdf_deep_free(my_prev);
389 ldns_zone_free(newzone);
395 ldns_zone_sort(ldns_zone *zone)
398 assert(zone != NULL);
400 zrr = ldns_zone_rrs(zone);
401 ldns_rr_list_sort(zrr);
406 * ixfr function. Work on a ldns_zone and remove and add
407 * the rrs from the rrlist
408 * \param[in] z the zone to work on
409 * \param[in] del rr_list to remove from the zone
410 * \param[in] add rr_list to add to the zone
411 * \return Tja, wat zouden we eens returnen TODO
414 ldns_zone_ixfr_del_add(ldns_zone *z, ldns_rr_list *del, ldns_rr_list *add)
421 ldns_zone_free(ldns_zone *zone)
423 ldns_rr_list_free(zone->_rrs);
428 ldns_zone_deep_free(ldns_zone *zone)
430 ldns_rr_free(zone->_soa);
431 ldns_rr_list_deep_free(zone->_rrs);