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 uint16_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 /* this will be an EXPENSIVE op with our zone structure */
00060 ldns_rr_list *
00061 ldns_zone_glue_rr_list(const ldns_zone *z)
00062 {
00063         /* when do we find glue? It means we find an IP address
00064          * (AAAA/A) for a nameserver listed in the zone
00065          *
00066          * Alg used here:
00067          * first find all the zonecuts (NS records)
00068          * find all the AAAA or A records (can be done it the 
00069          * above loop).
00070          *
00071          * Check if the aaaa/a list are subdomains under the
00072          * NS domains. If yes -> glue, if no -> not glue
00073          */
00074 
00075         ldns_rr_list *zone_cuts;
00076         ldns_rr_list *addr;
00077         ldns_rr_list *glue;
00078         ldns_rr *r, *ns, *a;
00079         ldns_rdf *dname_a, *dname_ns, *ns_owner;
00080         uint16_t i,j;
00081 
00082         zone_cuts = ldns_rr_list_new();
00083         addr = ldns_rr_list_new();
00084         glue = ldns_rr_list_new();
00085 
00086         for(i = 0; i < ldns_zone_rr_count(z); i++) {
00087                 r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
00088                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
00089                                 ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
00090                         /* possibly glue */
00091                         ldns_rr_list_push_rr(addr, r);
00092                         continue;
00093                 }
00094                 if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
00095                         /* multiple zones will end up here -
00096                          * for now; not a problem
00097                          */
00098                         /* don't add NS records for the current zone itself */
00099                         if (ldns_rdf_compare(ldns_rr_owner(r), 
00100                                                 ldns_rr_owner(ldns_zone_soa(z))) != 0) {
00101                                 ldns_rr_list_push_rr(zone_cuts, r);
00102                         }
00103                         continue;
00104                 }
00105         }
00106 
00107         /* will sorting make it quicker ?? */
00108         for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
00109                 ns = ldns_rr_list_rr(zone_cuts, i);
00110                 ns_owner = ldns_rr_owner(ns);
00111                 dname_ns = ldns_rr_ns_nsdname(ns);
00112                 for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
00113                         a = ldns_rr_list_rr(addr, j);
00114                         dname_a = ldns_rr_owner(a);
00115                         
00116                         if (ldns_dname_is_subdomain(dname_a, ns_owner) &&
00117                             ldns_rdf_compare(dname_ns, dname_a) == 0) {
00118                                 /* GLUE! */
00119                                 ldns_rr_list_push_rr(glue, a);
00120                                 break;
00121                         }
00122                 }
00123         }
00124         
00125         ldns_rr_list_free(addr);
00126         ldns_rr_list_free(zone_cuts);
00127 
00128         if (ldns_rr_list_rr_count(glue) == 0) {
00129                 ldns_rr_list_free(glue);
00130                 return NULL;
00131         } else {
00132                 return glue;
00133         }
00134 }
00135 
00136 ldns_zone *
00137 ldns_zone_new(void)
00138 {
00139         ldns_zone *z;
00140 
00141         z = LDNS_MALLOC(ldns_zone);
00142         if (!z) {
00143                 return NULL;
00144         }
00145 
00146         z->_rrs = ldns_rr_list_new();
00147         ldns_zone_set_soa(z, NULL);
00148         return z;
00149 }
00150 
00151 /* we regocnize:
00152  * $TTL, $ORIGIN
00153  */
00154 ldns_status
00155 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint16_t ttl, ldns_rr_class c)
00156 {
00157         return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
00158 }
00159 
00160 ldns_status
00161 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint16_t ttl, ldns_rr_class c, 
00162                 int *line_nr)
00163 {
00164         ldns_zone *newzone;
00165         ldns_rr *rr;
00166         uint16_t my_ttl = ttl;
00167         ldns_rr_class my_class = c;
00168         ldns_rr *last_rr = NULL;
00169         ldns_rdf *my_origin;
00170         ldns_rdf *my_prev;
00171         bool soa_seen = false;  /* 2 soa are an error */
00172         ldns_status s;
00173 
00174         newzone = ldns_zone_new();
00175         my_origin = origin;
00176         my_ttl    = ttl;
00177         my_class  = c;
00178         
00179         if (origin) {
00180                 my_origin = ldns_rdf_clone(origin);
00181                 /* also set the prev */
00182                 my_prev   = ldns_rdf_clone(origin);
00183         } else {
00184                 my_origin = ldns_dname_new_frm_str(".");
00185                 my_prev = NULL;
00186         }
00187 
00188         while(!feof(fp)) {
00189                 s = ldns_rr_new_frm_fp_l(&rr, fp, &my_ttl, &my_origin, &my_prev, line_nr);
00190                 switch (s) {
00191                 case LDNS_STATUS_OK:
00192                         if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
00193                                 if (soa_seen) {
00194                                         /* second SOA 
00195                                          * just skip, maybe we want to say
00196                                          * something??? */
00197                                         continue;
00198                                 }
00199                                 soa_seen = true;
00200                                 ldns_zone_set_soa(newzone, rr);
00201                                 continue;
00202                         }
00203                         
00204                         /* a normal RR - as sofar the DNS is normal */
00205                         last_rr = rr;
00206                         if (!ldns_zone_push_rr(newzone, rr)) {
00207                                 if (my_origin) {
00208                                         ldns_rdf_deep_free(my_origin);
00209                                 }
00210                                 ldns_zone_free(newzone);
00211                                 return LDNS_STATUS_MEM_ERR;
00212                         }
00213 
00214                         /*my_origin = ldns_rr_owner(rr);*/
00215                         my_ttl    = ldns_rr_ttl(rr);
00216                         my_class  = ldns_rr_get_class(rr);
00217                 case LDNS_STATUS_SYNTAX_EMPTY:
00218                         /* empty line was seen */
00219                 case LDNS_STATUS_SYNTAX_TTL:
00220                         /* the function set the ttl */
00221                         break;
00222                 case LDNS_STATUS_SYNTAX_ORIGIN:
00223                         /* the function set the origin */
00224                         break;
00225                 default:
00226                         ldns_zone_free(newzone);
00227                         return s;
00228                 }
00229         }
00230 
00231         if (my_origin) {
00232                 ldns_rdf_deep_free(my_origin);
00233         }
00234         if (my_prev) {
00235                 ldns_rdf_deep_free(my_prev);
00236         }
00237         if (z) {
00238                 *z = newzone;
00239         }
00240 
00241         return LDNS_STATUS_OK;
00242 }
00243 
00244 void
00245 ldns_zone_sort(ldns_zone *zone)
00246 {
00247         ldns_rr_list *zrr;
00248         assert(zone != NULL);
00249 
00250         zrr = ldns_zone_rrs(zone);
00251         ldns_rr_list_sort(zrr);
00252 }
00253 
00254 #if 0
00255 
00263 void
00264 ldns_zone_ixfr_del_add(ldns_zone *z, ldns_rr_list *del, ldns_rr_list *add)
00265 {
00266         
00267 }
00268 #endif
00269 
00270 void
00271 ldns_zone_free(ldns_zone *zone) 
00272 {
00273         ldns_rr_list_free(zone->_rrs);
00274         LDNS_FREE(zone);
00275 }
00276 
00277 void
00278 ldns_zone_deep_free(ldns_zone *zone) 
00279 {
00280         ldns_rr_free(zone->_soa);
00281         ldns_rr_list_deep_free(zone->_rrs);
00282         LDNS_FREE(zone);
00283 }

Generated on Wed Oct 10 16:24:49 2007 for ldns by  doxygen 1.5.2