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);
59 /* return a clone of the given rr list, without the glue records
60 * rr list should be the complete zone
61 * if present, stripped records are added to the list *glue_records
64 ldns_zone_strip_glue_rrs(const ldns_rdf *zone_name, const ldns_rr_list *rrs, ldns_rr_list *glue_rrs)
66 ldns_rr_list *new_list;
68 /* when do we find glue? It means we find an IP address
69 * (AAAA/A) for a nameserver listed in the zone
72 * first find all the zonecuts (NS records)
73 * find all the AAAA or A records (can be done it the
76 * Check if the aaaa/a list are subdomains under the
78 * If yes -> glue, if no -> not glue
81 ldns_rr_list *zone_cuts;
84 ldns_rdf *dname_a, *dname_ns, *ns_owner;
91 new_list = ldns_rr_list_new();
92 if (!new_list) goto memory_error;
93 zone_cuts = ldns_rr_list_new();
94 if (!zone_cuts) goto memory_error;
95 addr = ldns_rr_list_new();
96 if (!addr) goto memory_error;
98 for(i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
99 r = ldns_rr_list_rr(rrs, i);
100 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
101 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
103 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
106 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
107 /* multiple zones will end up here -
108 * for now; not a problem
110 /* don't add NS records for the current zone itself */
111 if (ldns_rdf_compare(ldns_rr_owner(r),
113 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
119 /* will sorting make it quicker ?? */
120 for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
121 ns = ldns_rr_list_rr(zone_cuts, i);
122 ns_owner = ldns_rr_owner(ns);
123 dname_ns = ldns_rr_ns_nsdname(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);
159 * Get the list of glue records in a zone
160 * XXX: there should be a way for this to return error, other than NULL,
161 * since NULL is a valid return
164 ldns_zone_glue_rr_list(const ldns_zone *z)
166 /* when do we find glue? It means we find an IP address
167 * (AAAA/A) for a nameserver listed in the zone
170 * first find all the zonecuts (NS records)
171 * find all the AAAA or A records (can be done it the
174 * Check if the aaaa/a list are subdomains under the
176 * If yes -> glue, if no -> not glue
179 ldns_rr_list *zone_cuts;
183 ldns_rdf *dname_a, *dname_ns, *ns_owner;
190 /* we cannot determine glue in a 'zone' without a SOA */
191 if (!ldns_zone_soa(z)) {
195 zone_cuts = ldns_rr_list_new();
196 if (!zone_cuts) goto memory_error;
197 addr = ldns_rr_list_new();
198 if (!addr) goto memory_error;
199 glue = ldns_rr_list_new();
200 if (!glue) goto memory_error;
202 for(i = 0; i < ldns_zone_rr_count(z); i++) {
203 r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
204 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
205 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
207 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
210 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
211 /* multiple zones will end up here -
212 * for now; not a problem
214 /* don't add NS records for the current zone itself */
215 if (ldns_rdf_compare(ldns_rr_owner(r),
216 ldns_rr_owner(ldns_zone_soa(z))) != 0) {
217 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
223 /* will sorting make it quicker ?? */
224 for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
225 ns = ldns_rr_list_rr(zone_cuts, i);
226 ns_owner = ldns_rr_owner(ns);
228 dname_ns = ldns_rr_ns_nsdname(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)) {
235 if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
240 ldns_rr_list_free(addr);
241 ldns_rr_list_free(zone_cuts);
243 if (ldns_rr_list_rr_count(glue) == 0) {
244 ldns_rr_list_free(glue);
252 LDNS_FREE(zone_cuts);
255 ldns_rr_list_free(addr);
258 ldns_rr_list_free(glue);
268 z = LDNS_MALLOC(ldns_zone);
273 z->_rrs = ldns_rr_list_new();
278 ldns_zone_set_soa(z, NULL);
286 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
288 return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
291 /* XXX: class is never used */
293 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c,
299 ldns_rr_class my_class;
302 bool soa_seen = false; /* 2 soa are an error */
306 /* most cases of error are memory problems */
307 ret = LDNS_STATUS_MEM_ERR;
317 my_origin = ldns_rdf_clone(origin);
318 if (!my_origin) goto error;
319 /* also set the prev */
320 my_prev = ldns_rdf_clone(origin);
321 if (!my_prev) goto error;
324 newzone = ldns_zone_new();
325 if (!newzone) goto error;
328 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
331 if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
334 * just skip, maybe we want to say
340 ldns_zone_set_soa(newzone, rr);
341 /* set origin to soa if not specified */
343 my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
348 /* a normal RR - as sofar the DNS is normal */
349 if (!ldns_zone_push_rr(newzone, rr)) goto error;
351 /*my_origin = ldns_rr_owner(rr);*/
352 my_ttl = ldns_rr_ttl(rr); /* XXX: this seems like an error */
353 my_class = ldns_rr_get_class(rr);
354 case LDNS_STATUS_SYNTAX_EMPTY:
355 /* empty line was seen */
356 case LDNS_STATUS_SYNTAX_TTL:
357 /* the function set the ttl */
359 case LDNS_STATUS_SYNTAX_ORIGIN:
360 /* the function set the origin */
369 ldns_rdf_deep_free(my_origin);
372 ldns_rdf_deep_free(my_prev);
377 ldns_zone_free(newzone);
380 return LDNS_STATUS_OK;
384 ldns_rdf_deep_free(my_origin);
387 ldns_rdf_deep_free(my_prev);
390 ldns_zone_free(newzone);
396 ldns_zone_sort(ldns_zone *zone)
399 assert(zone != NULL);
401 zrr = ldns_zone_rrs(zone);
402 ldns_rr_list_sort(zrr);
407 * ixfr function. Work on a ldns_zone and remove and add
408 * the rrs from the rrlist
409 * \param[in] z the zone to work on
410 * \param[in] del rr_list to remove from the zone
411 * \param[in] add rr_list to add to the zone
412 * \return Tja, wat zouden we eens returnen TODO
415 ldns_zone_ixfr_del_add(ldns_zone *z, ldns_rr_list *del, ldns_rr_list *add)
422 ldns_zone_free(ldns_zone *zone)
424 ldns_rr_list_free(zone->_rrs);
429 ldns_zone_deep_free(ldns_zone *zone)
431 ldns_rr_free(zone->_soa);
432 ldns_rr_list_deep_free(zone->_rrs);