Commit | Line | Data |
---|---|---|
b9b3af22 JL |
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 * | |
17 | ldns_zone_soa(const ldns_zone *z) | |
18 | { | |
19 | return z->_soa; | |
20 | } | |
21 | ||
22 | size_t | |
23 | ldns_zone_rr_count(const ldns_zone *z) | |
24 | { | |
25 | return ldns_rr_list_rr_count(z->_rrs); | |
26 | } | |
27 | ||
28 | void | |
29 | ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa) | |
30 | { | |
31 | z->_soa = soa; | |
32 | } | |
33 | ||
34 | ldns_rr_list * | |
35 | ldns_zone_rrs(const ldns_zone *z) | |
36 | { | |
37 | return z->_rrs; | |
38 | } | |
39 | ||
40 | void | |
41 | ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist) | |
42 | { | |
43 | z->_rrs = rrlist; | |
44 | } | |
45 | ||
46 | bool | |
47 | ldns_zone_push_rr_list(ldns_zone *z, ldns_rr_list *list) | |
48 | { | |
49 | return ldns_rr_list_cat(ldns_zone_rrs(z), list); | |
50 | ||
51 | } | |
52 | ||
53 | bool | |
54 | ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr) | |
55 | { | |
56 | return ldns_rr_list_push_rr( ldns_zone_rrs(z), rr); | |
57 | } | |
58 | ||
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 | |
62 | */ | |
63 | ldns_rr_list * | |
64 | ldns_zone_strip_glue_rrs(const ldns_rdf *zone_name, const ldns_rr_list *rrs, ldns_rr_list *glue_rrs) | |
65 | { | |
66 | ldns_rr_list *new_list; | |
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 *r, *ns, *a; | |
84 | ldns_rdf *dname_a, *dname_ns, *ns_owner; | |
85 | uint16_t i,j; | |
86 | ||
87 | new_list = NULL; | |
88 | zone_cuts = NULL; | |
89 | addr = NULL; | |
90 | ||
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; | |
97 | ||
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) { | |
102 | /* possibly glue */ | |
103 | if (!ldns_rr_list_push_rr(addr, r)) goto memory_error; | |
104 | continue; | |
105 | } | |
106 | if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) { | |
107 | /* multiple zones will end up here - | |
108 | * for now; not a problem | |
109 | */ | |
110 | /* don't add NS records for the current zone itself */ | |
111 | if (ldns_rdf_compare(ldns_rr_owner(r), | |
112 | zone_name) != 0) { | |
113 | if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error; | |
114 | } | |
115 | continue; | |
116 | } | |
117 | } | |
118 | ||
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); | |
127 | ||
128 | if (ldns_dname_is_subdomain(dname_a, ns_owner)) { | |
129 | /* GLUE! */ | |
130 | if (glue_rrs) { | |
131 | if (!ldns_rr_list_push_rr(glue_rrs, a)) goto memory_error; | |
132 | } | |
133 | break; | |
134 | } else { | |
135 | if (!ldns_rr_list_push_rr(new_list, a)) goto memory_error; | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | ldns_rr_list_free(addr); | |
141 | ldns_rr_list_free(zone_cuts); | |
142 | ||
143 | return new_list; | |
144 | ||
145 | memory_error: | |
146 | if (new_list) { | |
147 | ldns_rr_list_free(new_list); | |
148 | } | |
149 | if (zone_cuts) { | |
150 | ldns_rr_list_free(zone_cuts); | |
151 | } | |
152 | if (addr) { | |
153 | ldns_rr_list_free(addr); | |
154 | } | |
155 | return NULL; | |
156 | } | |
157 | ||
158 | /* | |
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 | |
162 | */ | |
163 | ldns_rr_list * | |
164 | ldns_zone_glue_rr_list(const ldns_zone *z) | |
165 | { | |
166 | /* when do we find glue? It means we find an IP address | |
167 | * (AAAA/A) for a nameserver listed in the zone | |
168 | * | |
169 | * Alg used here: | |
170 | * first find all the zonecuts (NS records) | |
171 | * find all the AAAA or A records (can be done it the | |
172 | * above loop). | |
173 | * | |
174 | * Check if the aaaa/a list are subdomains under the | |
175 | * NS domains. | |
176 | * If yes -> glue, if no -> not glue | |
177 | */ | |
178 | ||
179 | ldns_rr_list *zone_cuts; | |
180 | ldns_rr_list *addr; | |
181 | ldns_rr_list *glue; | |
182 | ldns_rr *r, *ns, *a; | |
183 | ldns_rdf *dname_a, *dname_ns, *ns_owner; | |
184 | size_t i,j; | |
185 | ||
186 | zone_cuts = NULL; | |
187 | addr = NULL; | |
188 | glue = NULL; | |
189 | ||
190 | /* we cannot determine glue in a 'zone' without a SOA */ | |
191 | if (!ldns_zone_soa(z)) { | |
192 | return NULL; | |
193 | } | |
194 | ||
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; | |
201 | ||
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) { | |
206 | /* possibly glue */ | |
207 | if (!ldns_rr_list_push_rr(addr, r)) goto memory_error; | |
208 | continue; | |
209 | } | |
210 | if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) { | |
211 | /* multiple zones will end up here - | |
212 | * for now; not a problem | |
213 | */ | |
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; | |
218 | } | |
219 | continue; | |
220 | } | |
221 | } | |
222 | ||
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); | |
227 | ||
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); | |
232 | ||
233 | if (ldns_dname_is_subdomain(dname_a, ns_owner)) { | |
234 | /* GLUE! */ | |
235 | if (!ldns_rr_list_push_rr(glue, a)) goto memory_error; | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | ldns_rr_list_free(addr); | |
241 | ldns_rr_list_free(zone_cuts); | |
242 | ||
243 | if (ldns_rr_list_rr_count(glue) == 0) { | |
244 | ldns_rr_list_free(glue); | |
245 | return NULL; | |
246 | } else { | |
247 | return glue; | |
248 | } | |
249 | ||
250 | memory_error: | |
251 | if (zone_cuts) { | |
252 | LDNS_FREE(zone_cuts); | |
253 | } | |
254 | if (addr) { | |
255 | ldns_rr_list_free(addr); | |
256 | } | |
257 | if (glue) { | |
258 | ldns_rr_list_free(glue); | |
259 | } | |
260 | return NULL; | |
261 | } | |
262 | ||
263 | ldns_zone * | |
264 | ldns_zone_new(void) | |
265 | { | |
266 | ldns_zone *z; | |
267 | ||
268 | z = LDNS_MALLOC(ldns_zone); | |
269 | if (!z) { | |
270 | return NULL; | |
271 | } | |
272 | ||
273 | z->_rrs = ldns_rr_list_new(); | |
274 | if (!z->_rrs) { | |
275 | LDNS_FREE(z); | |
276 | return NULL; | |
277 | } | |
278 | ldns_zone_set_soa(z, NULL); | |
279 | return z; | |
280 | } | |
281 | ||
282 | /* we regocnize: | |
283 | * $TTL, $ORIGIN | |
284 | */ | |
285 | ldns_status | |
286 | ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c) | |
287 | { | |
288 | return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL); | |
289 | } | |
290 | ||
291 | /* XXX: class is never used */ | |
292 | ldns_status | |
293 | ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c, | |
294 | int *line_nr) | |
295 | { | |
296 | ldns_zone *newzone; | |
297 | ldns_rr *rr; | |
298 | uint32_t my_ttl; | |
299 | ldns_rr_class my_class; | |
300 | ldns_rdf *my_origin; | |
301 | ldns_rdf *my_prev; | |
302 | bool soa_seen = false; /* 2 soa are an error */ | |
303 | ldns_status s; | |
304 | ldns_status ret; | |
305 | ||
306 | /* most cases of error are memory problems */ | |
307 | ret = LDNS_STATUS_MEM_ERR; | |
308 | ||
309 | newzone = NULL; | |
310 | my_origin = NULL; | |
311 | my_prev = NULL; | |
312 | ||
313 | my_ttl = ttl; | |
314 | my_class = c; | |
315 | ||
316 | if (origin) { | |
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; | |
322 | } | |
323 | ||
324 | newzone = ldns_zone_new(); | |
325 | if (!newzone) goto error; | |
326 | ||
327 | while(!feof(fp)) { | |
328 | s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr); | |
329 | switch (s) { | |
330 | case LDNS_STATUS_OK: | |
331 | if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) { | |
332 | if (soa_seen) { | |
333 | /* second SOA | |
334 | * just skip, maybe we want to say | |
335 | * something??? */ | |
336 | ldns_rr_free(rr); | |
337 | continue; | |
338 | } | |
339 | soa_seen = true; | |
340 | ldns_zone_set_soa(newzone, rr); | |
341 | /* set origin to soa if not specified */ | |
342 | if (!my_origin) { | |
343 | my_origin = ldns_rdf_clone(ldns_rr_owner(rr)); | |
344 | } | |
345 | continue; | |
346 | } | |
347 | ||
348 | /* a normal RR - as sofar the DNS is normal */ | |
349 | if (!ldns_zone_push_rr(newzone, rr)) goto error; | |
350 | ||
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 */ | |
358 | break; | |
359 | case LDNS_STATUS_SYNTAX_ORIGIN: | |
360 | /* the function set the origin */ | |
361 | break; | |
362 | default: | |
363 | ret = s; | |
364 | goto error; | |
365 | } | |
366 | } | |
367 | ||
368 | if (my_origin) { | |
369 | ldns_rdf_deep_free(my_origin); | |
370 | } | |
371 | if (my_prev) { | |
372 | ldns_rdf_deep_free(my_prev); | |
373 | } | |
374 | if (z) { | |
375 | *z = newzone; | |
376 | } else { | |
377 | ldns_zone_free(newzone); | |
378 | } | |
379 | ||
380 | return LDNS_STATUS_OK; | |
381 | ||
382 | error: | |
383 | if (my_origin) { | |
384 | ldns_rdf_deep_free(my_origin); | |
385 | } | |
386 | if (my_prev) { | |
387 | ldns_rdf_deep_free(my_prev); | |
388 | } | |
389 | if (newzone) { | |
390 | ldns_zone_free(newzone); | |
391 | } | |
392 | return ret; | |
393 | } | |
394 | ||
395 | void | |
396 | ldns_zone_sort(ldns_zone *zone) | |
397 | { | |
398 | ldns_rr_list *zrr; | |
399 | assert(zone != NULL); | |
400 | ||
401 | zrr = ldns_zone_rrs(zone); | |
402 | ldns_rr_list_sort(zrr); | |
403 | } | |
404 | ||
405 | #if 0 | |
406 | /** | |
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 | |
413 | */ | |
414 | void | |
415 | ldns_zone_ixfr_del_add(ldns_zone *z, ldns_rr_list *del, ldns_rr_list *add) | |
416 | { | |
417 | ||
418 | } | |
419 | #endif | |
420 | ||
421 | void | |
422 | ldns_zone_free(ldns_zone *zone) | |
423 | { | |
424 | ldns_rr_list_free(zone->_rrs); | |
425 | LDNS_FREE(zone); | |
426 | } | |
427 | ||
428 | void | |
429 | ldns_zone_deep_free(ldns_zone *zone) | |
430 | { | |
431 | ldns_rr_free(zone->_soa); | |
432 | ldns_rr_list_deep_free(zone->_rrs); | |
433 | LDNS_FREE(zone); | |
434 | } |