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
, *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 for(j
= 0; j
< ldns_rr_list_rr_count(addr
); j
++) {
124 a
= ldns_rr_list_rr(addr
, j
);
125 dname_a
= ldns_rr_owner(a
);
127 if (ldns_dname_is_subdomain(dname_a
, ns_owner
)) {
130 if (!ldns_rr_list_push_rr(glue_rrs
, a
)) goto memory_error
;
134 if (!ldns_rr_list_push_rr(new_list
, a
)) goto memory_error
;
139 ldns_rr_list_free(addr
);
140 ldns_rr_list_free(zone_cuts
);
146 ldns_rr_list_free(new_list
);
149 ldns_rr_list_free(zone_cuts
);
152 ldns_rr_list_free(addr
);
158 * Get the list of glue records in a zone
159 * XXX: there should be a way for this to return error, other than NULL,
160 * since NULL is a valid return
163 ldns_zone_glue_rr_list(const ldns_zone
*z
)
165 /* when do we find glue? It means we find an IP address
166 * (AAAA/A) for a nameserver listed in the zone
169 * first find all the zonecuts (NS records)
170 * find all the AAAA or A records (can be done it the
173 * Check if the aaaa/a list are subdomains under the
175 * If yes -> glue, if no -> not glue
178 ldns_rr_list
*zone_cuts
;
182 ldns_rdf
*dname_a
, *ns_owner
;
189 /* we cannot determine glue in a 'zone' without a SOA */
190 if (!ldns_zone_soa(z
)) {
194 zone_cuts
= ldns_rr_list_new();
195 if (!zone_cuts
) goto memory_error
;
196 addr
= ldns_rr_list_new();
197 if (!addr
) goto memory_error
;
198 glue
= ldns_rr_list_new();
199 if (!glue
) goto memory_error
;
201 for(i
= 0; i
< ldns_zone_rr_count(z
); i
++) {
202 r
= ldns_rr_list_rr(ldns_zone_rrs(z
), i
);
203 if (ldns_rr_get_type(r
) == LDNS_RR_TYPE_A
||
204 ldns_rr_get_type(r
) == LDNS_RR_TYPE_AAAA
) {
206 if (!ldns_rr_list_push_rr(addr
, r
)) goto memory_error
;
209 if (ldns_rr_get_type(r
) == LDNS_RR_TYPE_NS
) {
210 /* multiple zones will end up here -
211 * for now; not a problem
213 /* don't add NS records for the current zone itself */
214 if (ldns_rdf_compare(ldns_rr_owner(r
),
215 ldns_rr_owner(ldns_zone_soa(z
))) != 0) {
216 if (!ldns_rr_list_push_rr(zone_cuts
, r
)) goto memory_error
;
222 /* will sorting make it quicker ?? */
223 for(i
= 0; i
< ldns_rr_list_rr_count(zone_cuts
); i
++) {
224 ns
= ldns_rr_list_rr(zone_cuts
, i
);
225 ns_owner
= ldns_rr_owner(ns
);
227 for(j
= 0; j
< ldns_rr_list_rr_count(addr
); j
++) {
228 a
= ldns_rr_list_rr(addr
, j
);
229 dname_a
= ldns_rr_owner(a
);
231 if (ldns_dname_is_subdomain(dname_a
, ns_owner
) ||
232 ldns_dname_compare(dname_a
, ns_owner
) == 0) {
234 if (!ldns_rr_list_push_rr(glue
, a
)) goto memory_error
;
239 ldns_rr_list_free(addr
);
240 ldns_rr_list_free(zone_cuts
);
242 if (ldns_rr_list_rr_count(glue
) == 0) {
243 ldns_rr_list_free(glue
);
251 LDNS_FREE(zone_cuts
);
254 ldns_rr_list_free(addr
);
257 ldns_rr_list_free(glue
);
267 z
= LDNS_MALLOC(ldns_zone
);
272 z
->_rrs
= ldns_rr_list_new();
277 ldns_zone_set_soa(z
, NULL
);
285 ldns_zone_new_frm_fp(ldns_zone
**z
, FILE *fp
, ldns_rdf
*origin
, uint32_t ttl
, ldns_rr_class c
)
287 return ldns_zone_new_frm_fp_l(z
, fp
, origin
, ttl
, c
, NULL
);
290 /* XXX: class is never used */
292 ldns_zone_new_frm_fp_l(ldns_zone
**z
, FILE *fp
, ldns_rdf
*origin
, uint32_t ttl
,
293 ldns_rr_class
ATTR_UNUSED(c
), int *line_nr
)
300 bool soa_seen
= false; /* 2 soa are an error */
304 /* most cases of error are memory problems */
305 ret
= LDNS_STATUS_MEM_ERR
;
314 my_origin
= ldns_rdf_clone(origin
);
315 if (!my_origin
) goto error
;
316 /* also set the prev */
317 my_prev
= ldns_rdf_clone(origin
);
318 if (!my_prev
) goto error
;
321 newzone
= ldns_zone_new();
322 if (!newzone
) goto error
;
325 s
= ldns_rr_new_frm_fp_l(&rr
, fp
, &my_ttl
, &my_origin
, &my_prev
, line_nr
);
328 if (ldns_rr_get_type(rr
) == LDNS_RR_TYPE_SOA
) {
331 * just skip, maybe we want to say
337 ldns_zone_set_soa(newzone
, rr
);
338 /* set origin to soa if not specified */
340 my_origin
= ldns_rdf_clone(ldns_rr_owner(rr
));
345 /* a normal RR - as sofar the DNS is normal */
346 if (!ldns_zone_push_rr(newzone
, rr
)) goto error
;
348 case LDNS_STATUS_SYNTAX_EMPTY
:
349 /* empty line was seen */
350 case LDNS_STATUS_SYNTAX_TTL
:
351 /* the function set the ttl */
353 case LDNS_STATUS_SYNTAX_ORIGIN
:
354 /* the function set the origin */
356 case LDNS_STATUS_SYNTAX_INCLUDE
:
357 ret
= LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL
;
366 ldns_rdf_deep_free(my_origin
);
369 ldns_rdf_deep_free(my_prev
);
374 ldns_zone_free(newzone
);
377 return LDNS_STATUS_OK
;
381 ldns_rdf_deep_free(my_origin
);
384 ldns_rdf_deep_free(my_prev
);
387 ldns_zone_free(newzone
);
393 ldns_zone_sort(ldns_zone
*zone
)
396 assert(zone
!= NULL
);
398 zrr
= ldns_zone_rrs(zone
);
399 ldns_rr_list_sort(zrr
);
404 * ixfr function. Work on a ldns_zone and remove and add
405 * the rrs from the rrlist
406 * \param[in] z the zone to work on
407 * \param[in] del rr_list to remove from the zone
408 * \param[in] add rr_list to add to the zone
409 * \return Tja, wat zouden we eens returnen TODO
412 ldns_zone_ixfr_del_add(ldns_zone
*z
, ldns_rr_list
*del
, ldns_rr_list
*add
)
419 ldns_zone_free(ldns_zone
*zone
)
421 ldns_rr_list_free(zone
->_rrs
);
426 ldns_zone_deep_free(ldns_zone
*zone
)
428 ldns_rr_free(zone
->_soa
);
429 ldns_rr_list_deep_free(zone
->_rrs
);