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 
00060 /*
00061  * Get the list of glue records in a zone
00062  * XXX: there should be a way for this to return error, other than NULL, 
00063  *      since NULL is a valid return
00064  */
00065 ldns_rr_list *
00066 ldns_zone_glue_rr_list(const ldns_zone *z)
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_list *glue;
00084         ldns_rr *r, *ns, *a;
00085         ldns_rdf *dname_a, *ns_owner;
00086         size_t i,j;
00087 
00088         zone_cuts = NULL;
00089         addr = NULL;
00090         glue = NULL;
00091 
00092         /* we cannot determine glue in a 'zone' without a SOA */
00093         if (!ldns_zone_soa(z)) {
00094                 return NULL;
00095         }
00096 
00097         zone_cuts = ldns_rr_list_new();
00098         if (!zone_cuts) goto memory_error;
00099         addr = ldns_rr_list_new();
00100         if (!addr) goto memory_error;
00101         glue = ldns_rr_list_new();
00102         if (!glue) goto memory_error;
00103 
00104         for(i = 0; i < ldns_zone_rr_count(z); i++) {
00105                 r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
00106                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
00107                                 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
00108                         /* possibly glue */
00109                         if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
00110                         continue;
00111                 }
00112                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
00113                         /* multiple zones will end up here -
00114                          * for now; not a problem
00115                          */
00116                         /* don't add NS records for the current zone itself */
00117                         if (ldns_rdf_compare(ldns_rr_owner(r), 
00118                                                 ldns_rr_owner(ldns_zone_soa(z))) != 0) {
00119                                 if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
00120                         }
00121                         continue;
00122                 }
00123         }
00124 
00125         /* will sorting make it quicker ?? */
00126         for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
00127                 ns = ldns_rr_list_rr(zone_cuts, i);
00128                 ns_owner = ldns_rr_owner(ns);
00129 
00130                 for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
00131                         a = ldns_rr_list_rr(addr, j);
00132                         dname_a = ldns_rr_owner(a);
00133 
00134                         if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
00135                                 ldns_dname_compare(dname_a, ns_owner) == 0) {
00136                                 /* GLUE! */
00137                                 if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
00138                         }
00139                 }
00140         }
00141         
00142         ldns_rr_list_free(addr);
00143         ldns_rr_list_free(zone_cuts);
00144 
00145         if (ldns_rr_list_rr_count(glue) == 0) {
00146                 ldns_rr_list_free(glue);
00147                 return NULL;
00148         } else {
00149                 return glue;
00150         }
00151 
00152 memory_error:
00153         if (zone_cuts) {
00154                 LDNS_FREE(zone_cuts);
00155         }
00156         if (addr) {
00157                 ldns_rr_list_free(addr);
00158         }
00159         if (glue) {
00160                 ldns_rr_list_free(glue);
00161         }
00162         return NULL;
00163 }
00164 
00165 ldns_zone *
00166 ldns_zone_new(void)
00167 {
00168         ldns_zone *z;
00169 
00170         z = LDNS_MALLOC(ldns_zone);
00171         if (!z) {
00172                 return NULL;
00173         }
00174 
00175         z->_rrs = ldns_rr_list_new();
00176         if (!z->_rrs) {
00177                 LDNS_FREE(z);
00178                 return NULL;
00179         }
00180         ldns_zone_set_soa(z, NULL);
00181         return z;
00182 }
00183 
00184 /* we regocnize:
00185  * $TTL, $ORIGIN
00186  */
00187 ldns_status
00188 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
00189 {
00190         return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
00191 }
00192 
00193 /* XXX: class is never used */
00194 ldns_status
00195 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, 
00196         ldns_rr_class ATTR_UNUSED(c), int *line_nr)
00197 {
00198         ldns_zone *newzone;
00199         ldns_rr *rr;
00200         uint32_t my_ttl;
00201         ldns_rdf *my_origin;
00202         ldns_rdf *my_prev;
00203         bool soa_seen = false;  /* 2 soa are an error */
00204         ldns_status s;
00205         ldns_status ret;
00206 
00207         /* most cases of error are memory problems */
00208         ret = LDNS_STATUS_MEM_ERR;
00209 
00210         newzone = NULL;
00211         my_origin = NULL;
00212         my_prev = NULL;
00213 
00214         my_ttl    = ttl;
00215         
00216         if (origin) {
00217                 my_origin = ldns_rdf_clone(origin);
00218                 if (!my_origin) goto error;
00219                 /* also set the prev */
00220                 my_prev   = ldns_rdf_clone(origin);
00221                 if (!my_prev) goto error;
00222         }
00223 
00224         newzone = ldns_zone_new();
00225         if (!newzone) goto error;
00226 
00227         while(!feof(fp)) {
00228                 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
00229                 switch (s) {
00230                 case LDNS_STATUS_OK:
00231                         if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
00232                                 if (soa_seen) {
00233                                         /* second SOA 
00234                                          * just skip, maybe we want to say
00235                                          * something??? */
00236                                         ldns_rr_free(rr);
00237                                         continue;
00238                                 }
00239                                 soa_seen = true;
00240                                 ldns_zone_set_soa(newzone, rr);
00241                                 /* set origin to soa if not specified */
00242                                 if (!my_origin) {
00243                                         my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
00244                                 }
00245                                 continue;
00246                         }
00247                         
00248                         /* a normal RR - as sofar the DNS is normal */
00249                         if (!ldns_zone_push_rr(newzone, rr)) goto error;
00250 
00251                 case LDNS_STATUS_SYNTAX_EMPTY:
00252                         /* empty line was seen */
00253                 case LDNS_STATUS_SYNTAX_TTL:
00254                         /* the function set the ttl */
00255                         break;
00256                 case LDNS_STATUS_SYNTAX_ORIGIN:
00257                         /* the function set the origin */
00258                         break;
00259                 case LDNS_STATUS_SYNTAX_INCLUDE:
00260                         ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL;
00261                         break;
00262                 default:
00263                         ret = s;
00264                         goto error;
00265                 }
00266         }
00267 
00268         if (my_origin) {
00269                 ldns_rdf_deep_free(my_origin);
00270         }
00271         if (my_prev) {
00272                 ldns_rdf_deep_free(my_prev);
00273         }
00274         if (z) {
00275                 *z = newzone;
00276         } else {
00277                 ldns_zone_free(newzone);
00278         }
00279 
00280         return LDNS_STATUS_OK;
00281 
00282 error:
00283         if (my_origin) {
00284                 ldns_rdf_deep_free(my_origin);
00285         }
00286         if (my_prev) {
00287                 ldns_rdf_deep_free(my_prev);
00288         }
00289         if (newzone) {
00290                 ldns_zone_free(newzone);
00291         }
00292         return ret;
00293 }
00294 
00295 void
00296 ldns_zone_sort(ldns_zone *zone)
00297 {
00298         ldns_rr_list *zrr;
00299         assert(zone != NULL);
00300 
00301         zrr = ldns_zone_rrs(zone);
00302         ldns_rr_list_sort(zrr);
00303 }
00304 
00305 void
00306 ldns_zone_free(ldns_zone *zone) 
00307 {
00308         ldns_rr_list_free(zone->_rrs);
00309         LDNS_FREE(zone);
00310 }
00311 
00312 void
00313 ldns_zone_deep_free(ldns_zone *zone) 
00314 {
00315         ldns_rr_free(zone->_soa);
00316         ldns_rr_list_deep_free(zone->_rrs);
00317         LDNS_FREE(zone);
00318 }