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 #if 0 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 63 */ 64 static ldns_rr_list * 65 ldns_zone_strip_glue_rrs(const ldns_rdf *zone_name, const ldns_rr_list *rrs, ldns_rr_list *glue_rrs) 66 { 67 ldns_rr_list *new_list; 68 69 /* when do we find glue? It means we find an IP address 70 * (AAAA/A) for a nameserver listed in the zone 71 * 72 * Alg used here: 73 * first find all the zonecuts (NS records) 74 * find all the AAAA or A records (can be done it the 75 * above loop). 76 * 77 * Check if the aaaa/a list are subdomains under the 78 * NS domains. 79 * If yes -> glue, if no -> not glue 80 */ 81 82 ldns_rr_list *zone_cuts; 83 ldns_rr_list *addr; 84 ldns_rr *r, *ns, *a; 85 ldns_rdf *dname_a, *ns_owner; 86 uint16_t i,j; 87 88 new_list = NULL; 89 zone_cuts = NULL; 90 addr = NULL; 91 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; 98 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) { 103 /* possibly glue */ 104 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error; 105 continue; 106 } 107 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) { 108 /* multiple zones will end up here - 109 * for now; not a problem 110 */ 111 /* don't add NS records for the current zone itself */ 112 if (ldns_rdf_compare(ldns_rr_owner(r), 113 zone_name) != 0) { 114 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error; 115 } 116 continue; 117 } 118 } 119 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); 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 #endif 158 159 /* 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 163 */ 164 ldns_rr_list * 165 ldns_zone_glue_rr_list(const ldns_zone *z) 166 { 167 /* when do we find glue? It means we find an IP address 168 * (AAAA/A) for a nameserver listed in the zone 169 * 170 * Alg used here: 171 * first find all the zonecuts (NS records) 172 * find all the AAAA or A records (can be done it the 173 * above loop). 174 * 175 * Check if the aaaa/a list are subdomains under the 176 * NS domains. 177 * If yes -> glue, if no -> not glue 178 */ 179 180 ldns_rr_list *zone_cuts; 181 ldns_rr_list *addr; 182 ldns_rr_list *glue; 183 ldns_rr *r, *ns, *a; 184 ldns_rdf *dname_a, *ns_owner; 185 size_t i,j; 186 187 zone_cuts = NULL; 188 addr = NULL; 189 glue = NULL; 190 191 /* we cannot determine glue in a 'zone' without a SOA */ 192 if (!ldns_zone_soa(z)) { 193 return NULL; 194 } 195 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; 202 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) { 207 /* possibly glue */ 208 if (!ldns_rr_list_push_rr(addr, r)) goto memory_error; 209 continue; 210 } 211 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) { 212 /* multiple zones will end up here - 213 * for now; not a problem 214 */ 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; 219 } 220 continue; 221 } 222 } 223 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); 228 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 ldns_dname_compare(dname_a, ns_owner) == 0) { 235 /* GLUE! */ 236 if (!ldns_rr_list_push_rr(glue, a)) goto memory_error; 237 } 238 } 239 } 240 241 ldns_rr_list_free(addr); 242 ldns_rr_list_free(zone_cuts); 243 244 if (ldns_rr_list_rr_count(glue) == 0) { 245 ldns_rr_list_free(glue); 246 return NULL; 247 } else { 248 return glue; 249 } 250 251 memory_error: 252 if (zone_cuts) { 253 LDNS_FREE(zone_cuts); 254 } 255 if (addr) { 256 ldns_rr_list_free(addr); 257 } 258 if (glue) { 259 ldns_rr_list_free(glue); 260 } 261 return NULL; 262 } 263 264 ldns_zone * 265 ldns_zone_new(void) 266 { 267 ldns_zone *z; 268 269 z = LDNS_MALLOC(ldns_zone); 270 if (!z) { 271 return NULL; 272 } 273 274 z->_rrs = ldns_rr_list_new(); 275 if (!z->_rrs) { 276 LDNS_FREE(z); 277 return NULL; 278 } 279 ldns_zone_set_soa(z, NULL); 280 return z; 281 } 282 283 /* we regocnize: 284 * $TTL, $ORIGIN 285 */ 286 ldns_status 287 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c) 288 { 289 return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL); 290 } 291 292 /* XXX: class is never used */ 293 ldns_status 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) 296 { 297 ldns_zone *newzone; 298 ldns_rr *rr; 299 uint32_t my_ttl; 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 315 if (origin) { 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; 321 } 322 323 newzone = ldns_zone_new(); 324 if (!newzone) goto error; 325 326 while(!feof(fp)) { 327 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr); 328 switch (s) { 329 case LDNS_STATUS_OK: 330 if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) { 331 if (soa_seen) { 332 /* second SOA 333 * just skip, maybe we want to say 334 * something??? */ 335 ldns_rr_free(rr); 336 continue; 337 } 338 soa_seen = true; 339 ldns_zone_set_soa(newzone, rr); 340 /* set origin to soa if not specified */ 341 if (!my_origin) { 342 my_origin = ldns_rdf_clone(ldns_rr_owner(rr)); 343 } 344 continue; 345 } 346 347 /* a normal RR - as sofar the DNS is normal */ 348 if (!ldns_zone_push_rr(newzone, rr)) goto error; 349 350 case LDNS_STATUS_SYNTAX_EMPTY: 351 /* empty line was seen */ 352 case LDNS_STATUS_SYNTAX_TTL: 353 /* the function set the ttl */ 354 break; 355 case LDNS_STATUS_SYNTAX_ORIGIN: 356 /* the function set the origin */ 357 break; 358 case LDNS_STATUS_SYNTAX_INCLUDE: 359 ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL; 360 break; 361 default: 362 ret = s; 363 goto error; 364 } 365 } 366 367 if (my_origin) { 368 ldns_rdf_deep_free(my_origin); 369 } 370 if (my_prev) { 371 ldns_rdf_deep_free(my_prev); 372 } 373 if (z) { 374 *z = newzone; 375 } else { 376 ldns_zone_free(newzone); 377 } 378 379 return LDNS_STATUS_OK; 380 381 error: 382 if (my_origin) { 383 ldns_rdf_deep_free(my_origin); 384 } 385 if (my_prev) { 386 ldns_rdf_deep_free(my_prev); 387 } 388 if (newzone) { 389 ldns_zone_free(newzone); 390 } 391 return ret; 392 } 393 394 void 395 ldns_zone_sort(ldns_zone *zone) 396 { 397 ldns_rr_list *zrr; 398 assert(zone != NULL); 399 400 zrr = ldns_zone_rrs(zone); 401 ldns_rr_list_sort(zrr); 402 } 403 404 #if 0 405 /** 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 412 */ 413 void 414 ldns_zone_ixfr_del_add(ldns_zone *z, ldns_rr_list *del, ldns_rr_list *add) 415 { 416 417 } 418 #endif 419 420 void 421 ldns_zone_free(ldns_zone *zone) 422 { 423 ldns_rr_list_free(zone->_rrs); 424 LDNS_FREE(zone); 425 } 426 427 void 428 ldns_zone_deep_free(ldns_zone *zone) 429 { 430 ldns_rr_free(zone->_soa); 431 ldns_rr_list_deep_free(zone->_rrs); 432 LDNS_FREE(zone); 433 } 434