zone.c

Go to the documentation of this file.
00001 /* zone.c
00002  *
00003  * Functions for ldns_zone structure
00004  * a Net::DNS like library for C
00005  *
00006  * (c) NLnet Labs, 2005-2006
00007  * See the file LICENSE for the license
00008  */
00009 #include <ldns/config.h>
00010 
00011 #include <ldns/ldns.h>
00012 
00013 #include <strings.h>
00014 #include <limits.h>
00015 
00016 ldns_rr *
00017 ldns_zone_soa(const ldns_zone *z)
00018 {
00019         return z->_soa;
00020 }
00021 
00022 size_t
00023 ldns_zone_rr_count(const ldns_zone *z)
00024 {
00025         return ldns_rr_list_rr_count(z->_rrs);
00026 }
00027 
00028 void
00029 ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
00030 {
00031         z->_soa = soa;
00032 }
00033 
00034 ldns_rr_list *
00035 ldns_zone_rrs(const ldns_zone *z)
00036 {
00037         return z->_rrs;
00038 }
00039 
00040 void
00041 ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
00042 {
00043         z->_rrs = rrlist;
00044 }
00045 
00046 bool
00047 ldns_zone_push_rr_list(ldns_zone *z, ldns_rr_list *list)
00048 {
00049         return ldns_rr_list_cat(ldns_zone_rrs(z), list);
00050 
00051 }
00052 
00053 bool
00054 ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
00055 {
00056         return ldns_rr_list_push_rr( ldns_zone_rrs(z), rr);
00057 }
00058 
00059 /* return a clone of the given rr list, without the glue records
00060  * rr list should be the complete zone
00061  * if present, stripped records are added to the list *glue_records
00062  */
00063 ldns_rr_list *
00064 ldns_zone_strip_glue_rrs(const ldns_rdf *zone_name, const ldns_rr_list *rrs, ldns_rr_list *glue_rrs)
00065 {
00066         ldns_rr_list *new_list;
00067 
00068         /* when do we find glue? It means we find an IP address
00069          * (AAAA/A) for a nameserver listed in the zone
00070          *
00071          * Alg used here:
00072          * first find all the zonecuts (NS records)
00073          * find all the AAAA or A records (can be done it the 
00074          * above loop).
00075          *
00076          * Check if the aaaa/a list are subdomains under the
00077          * NS domains.
00078          * If yes -> glue, if no -> not glue
00079          */
00080 
00081         ldns_rr_list *zone_cuts;
00082         ldns_rr_list *addr;
00083         ldns_rr *r, *ns, *a;
00084         ldns_rdf *dname_a, *ns_owner;
00085         uint16_t i,j;
00086 
00087         new_list = NULL;
00088         zone_cuts = NULL;
00089         addr = NULL;
00090 
00091         new_list = ldns_rr_list_new();
00092         if (!new_list) goto memory_error;
00093         zone_cuts = ldns_rr_list_new();
00094         if (!zone_cuts) goto memory_error;
00095         addr = ldns_rr_list_new();
00096         if (!addr) goto memory_error;
00097 
00098         for(i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
00099                 r = ldns_rr_list_rr(rrs, i);
00100                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
00101                                 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
00102                         /* possibly glue */
00103                         if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
00104                         continue;
00105                 }
00106                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
00107                         /* multiple zones will end up here -
00108                          * for now; not a problem
00109                          */
00110                         /* don't add NS records for the current zone itself */
00111                         if (ldns_rdf_compare(ldns_rr_owner(r), 
00112                                                 zone_name) != 0) {
00113                                 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
00114                         }
00115                         continue;
00116                 }
00117         }
00118 
00119         /* will sorting make it quicker ?? */
00120         for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
00121                 ns = ldns_rr_list_rr(zone_cuts, i);
00122                 ns_owner = ldns_rr_owner(ns);
00123                 for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
00124                         a = ldns_rr_list_rr(addr, j);
00125                         dname_a = ldns_rr_owner(a);
00126                         
00127                         if (ldns_dname_is_subdomain(dname_a, ns_owner)) {
00128                                 /* GLUE! */
00129                                 if (glue_rrs) {
00130                                         if (!ldns_rr_list_push_rr(glue_rrs, a)) goto memory_error;
00131                                 }
00132                                 break;
00133                         } else {
00134                                 if (!ldns_rr_list_push_rr(new_list, a)) goto memory_error;
00135                         }
00136                 }
00137         }
00138         
00139         ldns_rr_list_free(addr);
00140         ldns_rr_list_free(zone_cuts);
00141 
00142         return new_list;
00143 
00144 memory_error:
00145         if (new_list) {
00146                 ldns_rr_list_free(new_list);
00147         }
00148         if (zone_cuts) {
00149                 ldns_rr_list_free(zone_cuts);
00150         }
00151         if (addr) {
00152                 ldns_rr_list_free(addr);
00153         }
00154         return NULL;
00155 }
00156 
00157 /*
00158  * Get the list of glue records in a zone
00159  * XXX: there should be a way for this to return error, other than NULL, 
00160  *      since NULL is a valid return
00161  */
00162 ldns_rr_list *
00163 ldns_zone_glue_rr_list(const ldns_zone *z)
00164 {
00165         /* when do we find glue? It means we find an IP address
00166          * (AAAA/A) for a nameserver listed in the zone
00167          *
00168          * Alg used here:
00169          * first find all the zonecuts (NS records)
00170          * find all the AAAA or A records (can be done it the 
00171          * above loop).
00172          *
00173          * Check if the aaaa/a list are subdomains under the
00174          * NS domains.
00175          * If yes -> glue, if no -> not glue
00176          */
00177 
00178         ldns_rr_list *zone_cuts;
00179         ldns_rr_list *addr;
00180         ldns_rr_list *glue;
00181         ldns_rr *r, *ns, *a;
00182         ldns_rdf *dname_a, *ns_owner;
00183         size_t i,j;
00184 
00185         zone_cuts = NULL;
00186         addr = NULL;
00187         glue = NULL;
00188 
00189         /* we cannot determine glue in a 'zone' without a SOA */
00190         if (!ldns_zone_soa(z)) {
00191                 return NULL;
00192         }
00193 
00194         zone_cuts = ldns_rr_list_new();
00195         if (!zone_cuts) goto memory_error;
00196         addr = ldns_rr_list_new();
00197         if (!addr) goto memory_error;
00198         glue = ldns_rr_list_new();
00199         if (!glue) goto memory_error;
00200 
00201         for(i = 0; i < ldns_zone_rr_count(z); i++) {
00202                 r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
00203                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
00204                                 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
00205                         /* possibly glue */
00206                         if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
00207                         continue;
00208                 }
00209                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
00210                         /* multiple zones will end up here -
00211                          * for now; not a problem
00212                          */
00213                         /* don't add NS records for the current zone itself */
00214                         if (ldns_rdf_compare(ldns_rr_owner(r), 
00215                                                 ldns_rr_owner(ldns_zone_soa(z))) != 0) {
00216                                 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
00217                         }
00218                         continue;
00219                 }
00220         }
00221 
00222         /* will sorting make it quicker ?? */
00223         for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
00224                 ns = ldns_rr_list_rr(zone_cuts, i);
00225                 ns_owner = ldns_rr_owner(ns);
00226 
00227                 for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
00228                         a = ldns_rr_list_rr(addr, j);
00229                         dname_a = ldns_rr_owner(a);
00230 
00231                         if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
00232                                 ldns_dname_compare(dname_a, ns_owner) == 0) {
00233                                 /* GLUE! */
00234                                 if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
00235                         }
00236                 }
00237         }
00238         
00239         ldns_rr_list_free(addr);
00240         ldns_rr_list_free(zone_cuts);
00241 
00242         if (ldns_rr_list_rr_count(glue) == 0) {
00243                 ldns_rr_list_free(glue);
00244                 return NULL;
00245         } else {
00246                 return glue;
00247         }
00248 
00249 memory_error:
00250         if (zone_cuts) {
00251                 LDNS_FREE(zone_cuts);
00252         }
00253         if (addr) {
00254                 ldns_rr_list_free(addr);
00255         }
00256         if (glue) {
00257                 ldns_rr_list_free(glue);
00258         }
00259         return NULL;
00260 }
00261 
00262 ldns_zone *
00263 ldns_zone_new(void)
00264 {
00265         ldns_zone *z;
00266 
00267         z = LDNS_MALLOC(ldns_zone);
00268         if (!z) {
00269                 return NULL;
00270         }
00271 
00272         z->_rrs = ldns_rr_list_new();
00273         if (!z->_rrs) {
00274                 LDNS_FREE(z);
00275                 return NULL;
00276         }
00277         ldns_zone_set_soa(z, NULL);
00278         return z;
00279 }
00280 
00281 /* we regocnize:
00282  * $TTL, $ORIGIN
00283  */
00284 ldns_status
00285 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
00286 {
00287         return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
00288 }
00289 
00290 /* XXX: class is never used */
00291 ldns_status
00292 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, 
00293         ldns_rr_class ATTR_UNUSED(c), int *line_nr)
00294 {
00295         ldns_zone *newzone;
00296         ldns_rr *rr;
00297         uint32_t my_ttl;
00298         ldns_rdf *my_origin;
00299         ldns_rdf *my_prev;
00300         bool soa_seen = false;  /* 2 soa are an error */
00301         ldns_status s;
00302         ldns_status ret;
00303 
00304         /* most cases of error are memory problems */
00305         ret = LDNS_STATUS_MEM_ERR;
00306 
00307         newzone = NULL;
00308         my_origin = NULL;
00309         my_prev = NULL;
00310 
00311         my_ttl    = ttl;
00312         
00313         if (origin) {
00314                 my_origin = ldns_rdf_clone(origin);
00315                 if (!my_origin) goto error;
00316                 /* also set the prev */
00317                 my_prev   = ldns_rdf_clone(origin);
00318                 if (!my_prev) goto error;
00319         }
00320 
00321         newzone = ldns_zone_new();
00322         if (!newzone) goto error;
00323 
00324         while(!feof(fp)) {
00325                 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
00326                 switch (s) {
00327                 case LDNS_STATUS_OK:
00328                         if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
00329                                 if (soa_seen) {
00330                                         /* second SOA 
00331                                          * just skip, maybe we want to say
00332                                          * something??? */
00333                                         ldns_rr_free(rr);
00334                                         continue;
00335                                 }
00336                                 soa_seen = true;
00337                                 ldns_zone_set_soa(newzone, rr);
00338                                 /* set origin to soa if not specified */
00339                                 if (!my_origin) {
00340                                         my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
00341                                 }
00342                                 continue;
00343                         }
00344                         
00345                         /* a normal RR - as sofar the DNS is normal */
00346                         if (!ldns_zone_push_rr(newzone, rr)) goto error;
00347 
00348                 case LDNS_STATUS_SYNTAX_EMPTY:
00349                         /* empty line was seen */
00350                 case LDNS_STATUS_SYNTAX_TTL:
00351                         /* the function set the ttl */
00352                         break;
00353                 case LDNS_STATUS_SYNTAX_ORIGIN:
00354                         /* the function set the origin */
00355                         break;
00356                 case LDNS_STATUS_SYNTAX_INCLUDE:
00357                         ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL;
00358                         break;
00359                 default:
00360                         ret = s;
00361                         goto error;
00362                 }
00363         }
00364 
00365         if (my_origin) {
00366                 ldns_rdf_deep_free(my_origin);
00367         }
00368         if (my_prev) {
00369                 ldns_rdf_deep_free(my_prev);
00370         }
00371         if (z) {
00372                 *z = newzone;
00373         } else {
00374                 ldns_zone_free(newzone);
00375         }
00376 
00377         return LDNS_STATUS_OK;
00378 
00379 error:
00380         if (my_origin) {
00381                 ldns_rdf_deep_free(my_origin);
00382         }
00383         if (my_prev) {
00384                 ldns_rdf_deep_free(my_prev);
00385         }
00386         if (newzone) {
00387                 ldns_zone_free(newzone);
00388         }
00389         return ret;
00390 }
00391 
00392 void
00393 ldns_zone_sort(ldns_zone *zone)
00394 {
00395         ldns_rr_list *zrr;
00396         assert(zone != NULL);
00397 
00398         zrr = ldns_zone_rrs(zone);
00399         ldns_rr_list_sort(zrr);
00400 }
00401 
00402 #if 0
00403 
00411 void
00412 ldns_zone_ixfr_del_add(ldns_zone *z, ldns_rr_list *del, ldns_rr_list *add)
00413 {
00414         
00415 }
00416 #endif
00417 
00418 void
00419 ldns_zone_free(ldns_zone *zone) 
00420 {
00421         ldns_rr_list_free(zone->_rrs);
00422         LDNS_FREE(zone);
00423 }
00424 
00425 void
00426 ldns_zone_deep_free(ldns_zone *zone) 
00427 {
00428         ldns_rr_free(zone->_soa);
00429         ldns_rr_list_deep_free(zone->_rrs);
00430         LDNS_FREE(zone);
00431 }

Generated on Wed Dec 19 16:56:50 2012 for ldns by  doxygen 1.4.7