/build/ldns-NeVfgT/ldns-1.9.2/zone.c
Go to the documentation of this file.
/* zone.c
*
* Functions for ldns_zone structure
* a Net::DNS like library for C
*
* (c) NLnet Labs, 2005-2006
* See the file LICENSE for the license
*/
#include <ldns/config.h>
#include <ldns/ldns.h>
#include <strings.h>
#include <limits.h>
ldns_rr *
ldns_zone_soa(const ldns_zone *z)
{
return z->_soa;
}
size_t
ldns_zone_rr_count(const ldns_zone *z)
{
return ldns_rr_list_rr_count(z->_rrs);
}
void
ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
{
z->_soa = soa;
}
ldns_rr_list *
ldns_zone_rrs(const ldns_zone *z)
{
return z->_rrs;
}
void
ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
{
z->_rrs = rrlist;
}
bool
ldns_zone_push_rr_list(ldns_zone *z, const ldns_rr_list *list)
{
return ldns_rr_list_cat(ldns_zone_rrs(z), list);
}
bool
ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
{
return ldns_rr_list_push_rr(ldns_zone_rrs(z), rr);
}
/*
* Get the list of glue records in a zone
* XXX: there should be a way for this to return error, other than NULL,
* since NULL is a valid return
*/
ldns_rr_list *
ldns_zone_glue_rr_list(const ldns_zone *z)
{
/* when do we find glue? It means we find an IP address
* (AAAA/A) for a nameserver listed in the zone
*
* Alg used here:
* first find all the zonecuts (NS records)
* find all the AAAA or A records (can be done it the
* above loop).
*
* Check if the aaaa/a list are subdomains under the
* NS domains.
* If yes -> glue, if no -> not glue
*/
ldns_rr_list *zone_cuts;
ldns_rr_list *addr;
ldns_rr_list *glue;
ldns_rr *r, *ns, *a;
ldns_rdf *dname_a, *ns_owner;
size_t i,j;
zone_cuts = NULL;
addr = NULL;
glue = NULL;
/* we cannot determine glue in a 'zone' without a SOA */
if (!ldns_zone_soa(z)) {
return NULL;
}
zone_cuts = ldns_rr_list_new();
if (!zone_cuts) goto memory_error;
addr = ldns_rr_list_new();
if (!addr) goto memory_error;
glue = ldns_rr_list_new();
if (!glue) goto memory_error;
for(i = 0; i < ldns_zone_rr_count(z); i++) {
r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
/* possibly glue */
if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
continue;
}
if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
/* multiple zones will end up here -
* for now; not a problem
*/
/* don't add NS records for the current zone itself */
if (ldns_rdf_compare(ldns_rr_owner(r),
ldns_rr_owner(ldns_zone_soa(z))) != 0) {
if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
}
continue;
}
}
/* will sorting make it quicker ?? */
for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
ns = ldns_rr_list_rr(zone_cuts, i);
ns_owner = ldns_rr_owner(ns);
for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
a = ldns_rr_list_rr(addr, j);
dname_a = ldns_rr_owner(a);
if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
ldns_dname_compare(dname_a, ns_owner) == 0) {
/* GLUE! */
if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
}
}
}
ldns_rr_list_free(addr);
ldns_rr_list_free(zone_cuts);
if (ldns_rr_list_rr_count(glue) == 0) {
ldns_rr_list_free(glue);
return NULL;
} else {
return glue;
}
memory_error:
if (zone_cuts) {
LDNS_FREE(zone_cuts);
}
if (addr) {
ldns_rr_list_free(addr);
}
if (glue) {
ldns_rr_list_free(glue);
}
return NULL;
}
ldns_zone *
ldns_zone_new(void)
{
ldns_zone *z;
z = LDNS_MALLOC(ldns_zone);
if (!z) {
return NULL;
}
z->_rrs = ldns_rr_list_new();
if (!z->_rrs) {
LDNS_FREE(z);
return NULL;
}
ldns_zone_set_soa(z, NULL);
return z;
}
/* we recognize:
* $TTL, $ORIGIN
*/
ldns_status
ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
{
return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
}
ldns_status _ldns_rr_new_frm_fp_l_internal(ldns_rr **newrr, FILE *fp,
uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev,
int *line_nr, bool *explicit_ttl);
/* XXX: class is never used */
ldns_status
ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, const ldns_rdf *origin,
uint32_t default_ttl, ldns_rr_class ATTR_UNUSED(c), int *line_nr)
{
ldns_zone *newzone;
ldns_rr *rr, *prev_rr = NULL;
uint32_t my_ttl;
ldns_rdf *my_origin;
ldns_rdf *my_prev;
bool soa_seen = false; /* 2 soa are an error */
ldns_status s;
ldns_status ret;
/* RFC 1035 Section 5.1, says 'Omitted class and TTL values are default
* to the last explicitly stated values.'
*/
bool ttl_from_TTL = false;
bool explicit_ttl = false;
/* most cases of error are memory problems */
ret = LDNS_STATUS_MEM_ERR;
newzone = NULL;
my_origin = NULL;
my_prev = NULL;
my_ttl = default_ttl;
if (origin) {
my_origin = ldns_rdf_clone(origin);
if (!my_origin) goto error;
/* also set the prev */
my_prev = ldns_rdf_clone(origin);
if (!my_prev) goto error;
}
newzone = ldns_zone_new();
if (!newzone) goto error;
while(!feof(fp)) {
/* If ttl came from $TTL line, then it should be the default.
* (RFC 2308 Section 4)
* Otherwise it "defaults to the last explicitly stated value"
* (RFC 1035 Section 5.1)
*/
if (ttl_from_TTL)
my_ttl = default_ttl;
s = _ldns_rr_new_frm_fp_l_internal(&rr, fp, &my_ttl, &my_origin,
&my_prev, line_nr, &explicit_ttl);
switch (s) {
case LDNS_STATUS_OK:
if (explicit_ttl) {
if (!ttl_from_TTL) {
/* No $TTL, so ttl "defaults to the
* last explicitly stated value"
* (RFC 1035 Section 5.1)
*/
my_ttl = ldns_rr_ttl(rr);
}
/* When ttl is implicit, try to adhere to the rules as
* much as possible. (also for compatibility with bind)
* This was changed when fixing an issue with ZONEMD
* which hashes the TTL too.
*/
} else if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SIG
|| ldns_rr_get_type(rr) == LDNS_RR_TYPE_RRSIG) {
if (ldns_rr_rd_count(rr) >= 4
&& ldns_rdf_get_type(ldns_rr_rdf(rr, 3)) == LDNS_RDF_TYPE_INT32)
/* SIG without explicit ttl get ttl
* from the original_ttl field
* (RFC 2535 Section 7.2)
*
* Similarly for RRSIG, but stated less
* specifically in the spec.
* (RFC 4034 Section 3)
*/
ldns_rr_set_ttl(rr,
ldns_rdf2native_int32(
ldns_rr_rdf(rr, 3)));
} else if (prev_rr
&& ldns_rr_get_type(prev_rr) == ldns_rr_get_type(rr)
&& ldns_dname_compare( ldns_rr_owner(prev_rr)
, ldns_rr_owner(rr)) == 0)
/* "TTLs of all RRs in an RRSet must be the same"
* (RFC 2881 Section 5.2)
*/
ldns_rr_set_ttl(rr, ldns_rr_ttl(prev_rr));
prev_rr = rr;
if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
if (soa_seen) {
/* second SOA
* just skip, maybe we want to say
* something??? */
ldns_rr_free(rr);
continue;
}
soa_seen = true;
ldns_zone_set_soa(newzone, rr);
/* set origin to soa if not specified */
if (!my_origin) {
my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
}
continue;
}
/* a normal RR - as sofar the DNS is normal */
if (!ldns_zone_push_rr(newzone, rr)) {
ldns_rr_free(rr);
goto error;
}
break;
case LDNS_STATUS_SYNTAX_EMPTY:
/* empty line was seen */
case LDNS_STATUS_SYNTAX_TTL:
/* the function set the ttl */
default_ttl = my_ttl;
ttl_from_TTL = true;
break;
case LDNS_STATUS_SYNTAX_ORIGIN:
/* the function set the origin */
break;
case LDNS_STATUS_SYNTAX_INCLUDE:
ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL;
goto error;
default:
ret = s;
goto error;
}
}
if (my_origin) {
ldns_rdf_deep_free(my_origin);
}
if (my_prev) {
ldns_rdf_deep_free(my_prev);
}
if (z) {
*z = newzone;
} else {
ldns_zone_free(newzone);
}
return LDNS_STATUS_OK;
error:
if (my_origin) {
ldns_rdf_deep_free(my_origin);
}
if (my_prev) {
ldns_rdf_deep_free(my_prev);
}
if (newzone) {
ldns_zone_deep_free(newzone);
}
return ret;
}
void
ldns_zone_sort(ldns_zone *zone)
{
ldns_rr_list *zrr;
assert(zone != NULL);
zrr = ldns_zone_rrs(zone);
ldns_rr_list_sort(zrr);
}
void
ldns_zone_free(ldns_zone *zone)
{
ldns_rr_list_free(zone->_rrs);
LDNS_FREE(zone);
}
void
ldns_zone_deep_free(ldns_zone *zone)
{
ldns_rr_free(zone->_soa);
ldns_rr_list_deep_free(zone->_rrs);
LDNS_FREE(zone);
}
ATTR_UNUSED
#define ATTR_UNUSED(x)
Definition common.h:72
config.h
ldns_dname_is_subdomain
signed char ldns_dname_is_subdomain(const ldns_rdf *sub, const ldns_rdf *parent)
test whether the name sub falls under parent (i.e.
Definition dname.c:296
ldns_dname_compare
int ldns_dname_compare(const ldns_rdf *dname1, const ldns_rdf *dname2)
Compares the two dname rdf's according to the algorithm for ordering in RFC4034 Section 6.
Definition dname.c:359
LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL
@ LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL
Definition error.h:82
LDNS_STATUS_MEM_ERR
@ LDNS_STATUS_MEM_ERR
Definition error.h:34
LDNS_STATUS_SYNTAX_TTL
@ LDNS_STATUS_SYNTAX_TTL
Definition error.h:88
LDNS_STATUS_OK
@ LDNS_STATUS_OK
Definition error.h:26
LDNS_STATUS_SYNTAX_EMPTY
@ LDNS_STATUS_SYNTAX_EMPTY
Definition error.h:91
LDNS_STATUS_SYNTAX_INCLUDE
@ LDNS_STATUS_SYNTAX_INCLUDE
Definition error.h:90
LDNS_STATUS_SYNTAX_ORIGIN
@ LDNS_STATUS_SYNTAX_ORIGIN
Definition error.h:89
ldns_status
enum ldns_enum_status ldns_status
Definition error.h:152
ldns_rdf_clone
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition rdata.c:222
ldns_rdf_get_type
ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd)
returns the type of the rdf.
Definition rdata.c:31
ldns_rdf_deep_free
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition rdata.c:230
ldns_rdf2native_int32
uint32_t ldns_rdf2native_int32(const ldns_rdf *rd)
returns the native uint32_t representation from the rdf.
Definition rdata.c:98
LDNS_RDF_TYPE_INT32
@ LDNS_RDF_TYPE_INT32
32 bits
Definition rdata.h:56
ldns_rdf
struct ldns_struct_rdf ldns_rdf
Definition rdata.h:211
ldns_rdf_compare
int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2)
compares two rdf's on their wire formats.
Definition rdata.c:663
ldns_rr_list_free
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition rr.c:1009
ldns_rr_ttl
uint32_t ldns_rr_ttl(const ldns_rr *rr)
returns the ttl of an rr structure.
Definition rr.c:929
ldns_rr_list_deep_free
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition rr.c:1018
ldns_rr_free
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition rr.c:81
ldns_rr
struct ldns_struct_rr ldns_rr
Definition rr.h:346
ldns_rr_list_new
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition rr.c:998
ldns_rr_list_sort
void ldns_rr_list_sort(ldns_rr_list *unsorted)
sorts an rr_list (canonical wire format).
Definition rr.c:1554
LDNS_RR_TYPE_RRSIG
@ LDNS_RR_TYPE_RRSIG
DNSSEC.
Definition rr.h:170
LDNS_RR_TYPE_A
@ LDNS_RR_TYPE_A
a host address
Definition rr.h:80
LDNS_RR_TYPE_SOA
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
Definition rr.h:90
LDNS_RR_TYPE_SIG
@ LDNS_RR_TYPE_SIG
2535typecode
Definition rr.h:126
LDNS_RR_TYPE_AAAA
@ LDNS_RR_TYPE_AAAA
ipv6 address
Definition rr.h:134
LDNS_RR_TYPE_NS
@ LDNS_RR_TYPE_NS
an authoritative name server
Definition rr.h:82
ldns_rr_list_push_rr
signed char ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
Definition rr.c:1130
ldns_rr_rd_count
size_t ldns_rr_rd_count(const ldns_rr *rr)
returns the rd_count of an rr structure.
Definition rr.c:935
ldns_rr_rdf
ldns_rdf * ldns_rr_rdf(const ldns_rr *rr, size_t nr)
returns the rdata field member counter.
Definition rr.c:907
ldns_rr_list_rr_count
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr's in an rr_list.
Definition rr.c:955
ldns_rr_get_type
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition rr.c:941
ldns_rr_set_ttl
void ldns_rr_set_ttl(ldns_rr *rr, uint32_t ttl)
sets the ttl in the rr structure.
Definition rr.c:814
ldns_rr_list
struct ldns_struct_rr_list ldns_rr_list
Definition rr.h:360
ldns_rr_class
enum ldns_enum_rr_class ldns_rr_class
Definition rr.h:61
ldns_rr_list_rr
ldns_rr * ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
returns a specific rr of an rrlist.
Definition rr.c:988
ldns_rr_list_cat
signed char ldns_rr_list_cat(ldns_rr_list *left, const ldns_rr_list *right)
concatenates two ldns_rr_lists together.
Definition rr.c:1034
ldns_rr_owner
ldns_rdf * ldns_rr_owner(const ldns_rr *rr)
returns the owner name of an rr structure.
Definition rr.c:917
ldns_struct_zone::_rrs
ldns_rr_list * _rrs
Definition zone.h:47
ldns_struct_zone::_soa
ldns_rr * _soa
the soa defines a zone
Definition zone.h:45
LDNS_FREE
#define LDNS_FREE(ptr)
Definition util.h:60
LDNS_MALLOC
#define LDNS_MALLOC(type)
Memory management macros.
Definition util.h:49
ldns_zone_set_soa
void ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
Set the zone's soa record.
Definition zone.c:29
ldns_zone_soa
ldns_rr * ldns_zone_soa(const ldns_zone *z)
Return the soa record of a zone.
Definition zone.c:17
ldns_zone_push_rr
signed char ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
push an single rr to a zone structure.
Definition zone.c:53
ldns_zone_rr_count
size_t ldns_zone_rr_count(const ldns_zone *z)
Returns the number of resource records in the zone, NOT counting the SOA record.
Definition zone.c:23
ldns_zone_set_rrs
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone's contents.
Definition zone.c:41
ldns_zone_rrs
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone's content.
Definition zone.c:35
ldns_zone_glue_rr_list
ldns_rr_list * ldns_zone_glue_rr_list(const ldns_zone *z)
Retrieve all resource records from the zone that are glue records.
Definition zone.c:65
ldns_zone_deep_free
void ldns_zone_deep_free(ldns_zone *zone)
Frees the allocated memory for the zone, the soa rr in it, and the rr_list structure in it,...
Definition zone.c:376
_ldns_rr_new_frm_fp_l_internal
ldns_status _ldns_rr_new_frm_fp_l_internal(ldns_rr **newrr, FILE *fp, uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev, int *line_nr, signed char *explicit_ttl)
Definition rr.c:723
ldns_zone_push_rr_list
signed char ldns_zone_push_rr_list(ldns_zone *z, const ldns_rr_list *list)
push an rrlist to a zone structure.
Definition zone.c:47
ldns_zone_new_frm_fp
ldns_status ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
Create a new zone from a file.
Definition zone.c:187
ldns_zone_new
ldns_zone * ldns_zone_new(void)
create a new ldns_zone structure
Definition zone.c:165
ldns_zone_free
void ldns_zone_free(ldns_zone *zone)
Frees the allocated memory for the zone, and the rr_list structure in it.
Definition zone.c:369
ldns_zone_sort
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition zone.c:359
ldns_zone_new_frm_fp_l
ldns_status ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t default_ttl, ldns_rr_class c __attribute__((unused)), int *line_nr)
Definition zone.c:198
ldns_zone
struct ldns_struct_zone ldns_zone
Definition zone.h:50