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);
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
66 ldns_zone_glue_rr_list(const ldns_zone *z)
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;
85 ldns_rdf *dname_a, *ns_owner;
92 /* we cannot determine glue in a 'zone' without a SOA */
93 if (!ldns_zone_soa(z)) {
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;
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 ||
107 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
109 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
112 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
113 /* multiple zones will end up here -
114 * for now; not a problem
116 /* don't add NS records for the current zone itself */
117 if (ldns_rdf_compare(ldns_rr_owner(r),
118 ldns_rr_owner(ldns_zone_soa(z))) != 0) {
119 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
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);
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);
134 if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
135 ldns_dname_compare(dname_a, ns_owner) == 0) {
137 if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
142 ldns_rr_list_free(addr);
143 ldns_rr_list_free(zone_cuts);
145 if (ldns_rr_list_rr_count(glue) == 0) {
146 ldns_rr_list_free(glue);
154 LDNS_FREE(zone_cuts);
157 ldns_rr_list_free(addr);
160 ldns_rr_list_free(glue);
170 z = LDNS_MALLOC(ldns_zone);
175 z->_rrs = ldns_rr_list_new();
180 ldns_zone_set_soa(z, NULL);
188 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
190 return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
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)
203 bool soa_seen = false; /* 2 soa are an error */
207 /* most cases of error are memory problems */
208 ret = LDNS_STATUS_MEM_ERR;
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;
224 newzone = ldns_zone_new();
225 if (!newzone) goto error;
228 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
231 if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
234 * just skip, maybe we want to say
240 ldns_zone_set_soa(newzone, rr);
241 /* set origin to soa if not specified */
243 my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
248 /* a normal RR - as sofar the DNS is normal */
249 if (!ldns_zone_push_rr(newzone, rr)) goto error;
251 case LDNS_STATUS_SYNTAX_EMPTY:
252 /* empty line was seen */
253 case LDNS_STATUS_SYNTAX_TTL:
254 /* the function set the ttl */
256 case LDNS_STATUS_SYNTAX_ORIGIN:
257 /* the function set the origin */
259 case LDNS_STATUS_SYNTAX_INCLUDE:
260 ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL;
269 ldns_rdf_deep_free(my_origin);
272 ldns_rdf_deep_free(my_prev);
277 ldns_zone_free(newzone);
280 return LDNS_STATUS_OK;
284 ldns_rdf_deep_free(my_origin);
287 ldns_rdf_deep_free(my_prev);
290 ldns_zone_free(newzone);
296 ldns_zone_sort(ldns_zone *zone)
299 assert(zone != NULL);
301 zrr = ldns_zone_rrs(zone);
302 ldns_rr_list_sort(zrr);
306 ldns_zone_free(ldns_zone *zone)
308 ldns_rr_list_free(zone->_rrs);
313 ldns_zone_deep_free(ldns_zone *zone)
315 ldns_rr_free(zone->_soa);
316 ldns_rr_list_deep_free(zone->_rrs);