diff --git a/CuteDNS.files b/CuteDNS.files index 7b8465ff0220b159b3e9b0c20a2809a8feecc68f..3198fc5ab5ac5b6ee44223d347f55d06a32bd993 100644 --- a/CuteDNS.files +++ b/CuteDNS.files @@ -1,7 +1,6 @@ Makefile src/common.h src/main.c -src/ctl/main.c src/hash/cuckoo-hash-table.c src/hash/cuckoo-hash-table.h src/hash/hash-functions.c @@ -10,16 +9,9 @@ src/hash/universal-system.c src/hash/universal-system.h src/other/print.c src/other/print.h -src/other/tree.h -src/other/bitset.h -src/other/bitset.c src/other/log.h src/other/log.c src/other/debug.h -src/other/dynamic-array.h -src/other/dynamic-array.c -src/other/skip-list.h -src/other/skip-list.c src/server/socket.c src/server/socket.h src/server/name-server.c @@ -36,6 +28,13 @@ src/stat/gatherer.h src/stat/gatherer.c src/stat/stat.h src/stat/stat.c +src/lib/dynamic-array.h +src/lib/dynamic-array.c +src/lib/skip-list.h +src/lib/skip-list.c +src/lib/tree.h +src/lib/bitset.h +src/lib/bitset.c src/dnslib/dname.h src/dnslib/dname.c src/dnslib/rrset.h @@ -98,18 +97,7 @@ src/tests/dnslib/dnslib_rrset_tests.c src/tests/dnslib/dnslib_node_tests.c src/tests/dnslib/dnslib_response_tests.c src/tests/dnslib/dnslib_zone_tests.c -tests/querytcp.c -deprecated/zone-database.c -deprecated/zone-database.h -deprecated/zone-data-structure.c -deprecated/zone-data-structure.h -deprecated/zone-node.c -deprecated/zone-node.h -deprecated/zone-parser.c -deprecated/zone-parser.h -deprecated/dns/dns-simple.c -deprecated/dns/dns-simple.h -deprecated/dns/dns-utils.c -deprecated/dns/dns-utils.h +src/ctl/main.c src/ctl/process.c src/ctl/process.h +tests/querytcp.c diff --git a/Makefile b/Makefile index 158192f5a1132755d7992778586344feceed6d23..14bcf9bd2602ececae5e96800ce4574d030656ec 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ COL_CYAN = \033[01;36m COL_WHITE = \033[01;37m COL_END = \033[0m -INC_DIRS = obj/ src/ src/hash/ src/dns/ src/other/ src/server/ src/zoneparser/ src/tests src/tests/libtap src/dnslib/ src/stat src/alloc/ src/ctl +INC_DIRS = obj/ src/ src/hash/ src/dns/ src/other/ src/server/ src/zoneparser/ src/tests src/tests/libtap src/dnslib/ src/stat src/alloc/ src/ctl/ src/lib/ SRC_DIRS = src/ TESTS_DIR = src/tests/ ZONEC_DIR = src/zoneparser/ diff --git a/deprecated/dns/dns-simple.c b/deprecated/dns/dns-simple.c deleted file mode 100644 index 8c3993fdef8cddb054d266128ceea7a8e36be311..0000000000000000000000000000000000000000 --- a/deprecated/dns/dns-simple.c +++ /dev/null @@ -1,711 +0,0 @@ -#include "dns-simple.h" -#include <netinet/in.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <assert.h> -#include <stdint.h> -#include <ldns/rr.h> -#include <ldns/rdata.h> -#include <ldns/host2wire.h> - -#define HEADER_SET_QR(flags) (flags |= (1 << 15)) -#define HEADER_SET_AA(flags) (flags |= (1 << 10)) - -// RCODEs -static const uint16_t RCODE_NOERR = 0; // 0..000000000 -static const uint16_t RCODE_FORMERR = 1; // 0..000000001 -static const uint16_t RCODE_SERVFAIL = 2; // 0..000000010 -static const uint16_t RCODE_NXDOMAIN = 3; // 0..000000011 -static const uint16_t RCODE_NOTIMPL = 4; // 0..000000100 -static const uint16_t RCODE_REFUSED = 5; // 0..000000101 -static const uint16_t RCODE_YXDOMAIN = 6; // 0..000000110 -static const uint16_t RCODE_YXRRSET = 7; // 0..000000111 -static const uint16_t RCODE_NXRRSET = 8; // 0..000001000 -static const uint16_t RCODE_NOTAUTH = 9; // 0..000001001 -static const uint16_t RCODE_NOTZONE = 10; // 0..000001010 -static const uint16_t RCODE_CLEAR = 65520; // 1..111110000 - -// default test values -static const uint16_t RRTYPE_DEFAULT = 1; // A -static const uint16_t RRCLASS_DEFAULT = 1; // IN -static const uint32_t TTL_DEFAULT = 3600; -static const unsigned int RDLENGTH_DEFAULT = 4; -static const uint8_t RDATA_DEFAULT[4] = { 127, 0, 0, 1 }; - -// assuming flags is 16bit integer -#define RCODE_SET(flags, rcode) flags = (flags & RCODE_CLEAR) | rcode - -/*----------------------------------------------------------------------------*/ - -void dnss_copy_questions( dnss_question *from, dnss_question *to, uint count ) -{ - for (uint i = 0; i < count; ++i) { - to[i].qclass = from[i].qclass; - to[i].qtype = from[i].qtype; - to[i].qname = malloc(strlen(from[i].qname) + 1); - memcpy(to[i].qname, from[i].qname, strlen(from[i].qname) + 1); - } -} - -/*----------------------------------------------------------------------------*/ - -void dnss_copy_rrs( const dnss_rr *from, dnss_rr *to, uint count ) -{ - for (uint i = 0; i < count; ++i) { - to[i].rdlength = from[i].rdlength; - to[i].rrclass = from[i].rrclass; - to[i].rrtype = from[i].rrtype; - to[i].ttl = from[i].ttl; - - to[i].owner = malloc(strlen(from[i].owner) + 1); - // replace by check and error - assert(to[i].owner != NULL); - memcpy(to[i].owner, from[i].owner, strlen(from[i].owner) + 1); - - to[i].rdata = malloc(from[i].rdlength); - // replace by check and error - assert(to[i].rdata != NULL); - memcpy(to[i].rdata, from[i].rdata, from[i].rdlength); - } -} - -/*----------------------------------------------------------------------------*/ - -dnss_rr *dnss_create_rr( dnss_dname owner ) -{ - dnss_rr *rr; - - // assuming owner is in natural format => conversion to wire format needed - debug_dnss("Converting domain name to wire format.\n"); - - // convert domain name to wire format - uint wire_size = dnss_wire_dname_size(&owner); - assert(wire_size > 0); - dnss_dname_wire owner_wire = malloc(wire_size); - if (dnss_dname_to_wire(owner, owner_wire, wire_size) != 0) { - free(owner_wire); - return NULL; - } - - debug_dnss("Creating RR structure.\n"); - - rr = malloc(sizeof(dnss_rr)); - - if (rr == NULL) { - free(owner_wire); - return NULL; - } - - // rdata will be saved at the end of the RR - //rr->rdata = (unsigned char *)rr + sizeof(dnss_rr); - rr->rdata = malloc(RDLENGTH_DEFAULT); - memcpy(rr->rdata, RDATA_DEFAULT, RDLENGTH_DEFAULT); - - rr->rrtype = RRTYPE_DEFAULT; - rr->rrclass = RRCLASS_DEFAULT; - rr->ttl = TTL_DEFAULT; - rr->rdlength = RDLENGTH_DEFAULT; - - rr->owner = owner_wire; - - debug_dnss("Created RR: owner: %s, type: %u, rdlength: %u.\n", rr->owner, - rr->rrtype, rr->rdlength); - debug_dnss_hex(rr->owner, strlen(rr->owner)); - debug_dnss("Done.\n"); - - return rr; -} - -/*----------------------------------------------------------------------------*/ - -dnss_question *dnss_create_question( dnss_dname_wire qname, uint length ) -{ - dnss_question *question = malloc(sizeof(dnss_question) + length); - - question->qname = (char *)question + sizeof(dnss_question); - memcpy(question->qname, qname, length); - question->qclass = RRCLASS_DEFAULT; - question->qtype = RRTYPE_DEFAULT; - - return question; -} - -/*----------------------------------------------------------------------------*/ - -dnss_packet *dnss_create_empty_packet() -{ - dnss_packet *packet = malloc(sizeof(dnss_packet)); - - memset(packet, 0, sizeof(dnss_packet)); - // this should produce consistent packet structure - return packet; -} - -/*----------------------------------------------------------------------------*/ - -void dnss_rrs_from_rrset( const ldns_rr_list *from, dnss_rr *to, uint count ) -{ - /* - * Totally dumb and useless function, only to make it compile with the - * new zone node and without changing a lot of other things. - */ - assert(ldns_rr_list_rr_count(from) == count); - - uint8_t *owner = NULL; - uint8_t *rdata = NULL; - size_t size; - - dnss_rr *r = to; - - int j = 0; - while (j < ldns_rr_list_rr_count(from)) { - ldns_rr *rr = ldns_rr_list_rr(from, j); - ldns_rdf2wire(&owner, ldns_rr_owner(rr), &size); - if (owner != NULL) { - r->owner = (char *)owner; - r->rrclass = ldns_rr_get_class(rr); - r->rrtype = ldns_rr_get_type(rr); - r->ttl = ldns_rr_ttl(rr); - - r->rdata = malloc(ldns_rdf_size(ldns_rr_rdf(rr, 0))); - if (r->rdata == NULL) { - ERR_ALLOC_FAILED; - return; - } - uint8_t *pos = r->rdata; - for (int i = 0; i < ldns_rr_rd_count(rr); ++i) { - ldns_rdf2wire(&rdata, ldns_rr_rdf(rr, i), &size); - if (rdata != NULL) { - memcpy(pos, rdata, size); - pos += size; - } - if ((pos - r->rdata) >= ldns_rdf_size(ldns_rr_rdf(rr, 0))) { - unsigned char *tmp = realloc(r->rdata, (i + 2) * - ldns_rdf_size(ldns_rr_rdf(rr, 0))); - if (tmp == NULL) { - ERR_ALLOC_FAILED; - return; - } - r->rdata = tmp; - } - } - ++r; // move to next RR's place - } - ++j; - } -} - -/*----------------------------------------------------------------------------*/ - -int dnss_create_response( const dnss_packet *query, const ldns_rr_list *answers, - uint count, dnss_packet **response ) - /*! @todo change last argument to dnss_packet * ?? */ -{ - // header - memcpy(&(*response)->header, &query->header, sizeof(dnss_header)); - HEADER_SET_AA((*response)->header.flags); - HEADER_SET_QR((*response)->header.flags); - // copying other flags (maybe set TC, RA, AD, CD) - - (*response)->questions = malloc( - (*response)->header.qdcount * sizeof(dnss_question)); - if ((*response)->questions == NULL) { - ERR_ALLOC_FAILED; - return -1; - } - dnss_copy_questions(query->questions, (*response)->questions, - (*response)->header.qdcount); - - // check answer - if(answers == NULL) { - count = 0; - } - - // answers - (*response)->header.ancount = count; - - (*response)->answers = malloc(count * sizeof(dnss_rr)); - //dnss_copy_rrs(answers, (*response)->answers, count); - dnss_rrs_from_rrset(answers, (*response)->answers, count); - // distinguish between NODATA (good as it is) and NXDOMAIN (set RCODE) - - (*response)->header.nscount = 0; - (*response)->authority = NULL; - (*response)->header.arcount = 0; - (*response)->additional = NULL; - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -int dnss_create_error_response( dnss_packet *query, dnss_packet **response ) - /*! @todo change last argument to dnss_packet * ?? */ -{ - // header - memcpy(&(*response)->header, &query->header, sizeof(dnss_header)); - HEADER_SET_AA((*response)->header.flags); - HEADER_SET_QR((*response)->header.flags); - // copying other flags (maybe set TC, RA, AD, CD) - - // set SERVFAIL RCODE - RCODE_SET((*response)->header.flags, RCODE_SERVFAIL); - - (*response)->questions = malloc( - (*response)->header.qdcount * sizeof(dnss_question)); - if ((*response)->questions == NULL) { - ERR_ALLOC_FAILED; - return -1; - } - dnss_copy_questions(query->questions, (*response)->questions, - (*response)->header.qdcount); - - // no answers - (*response)->header.ancount = 0; - (*response)->answers = NULL; - (*response)->header.nscount = 0; - (*response)->authority = NULL; - (*response)->header.arcount = 0; - (*response)->additional = NULL; - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -int dnss_wire_format( dnss_packet *packet, char *packet_wire, - unsigned int *packet_size ) -{ - /* We can assume that the domain names are kept in the wire format during - * copying among the application. Thus no conversion needed here. - * All integers must be converted to the network byte order. - */ - - // in *packet_size there should be the max size of the wire format - - // determine the size of the packet - uint real_size = HEADER_SIZE; - real_size = HEADER_SIZE; - for (int i = 0; i < packet->header.qdcount; ++i) { - real_size += strlen(packet->questions[i].qname) + 5; - } - for (int i = 0; i < packet->header.ancount; ++i) { - real_size += strlen(packet->answers[i].owner) - + 11 + packet->answers[i].rdlength; - } - for (int i = 0; i < packet->header.nscount; ++i) { - real_size += strlen(packet->authority[i].owner) - + 11 + packet->authority[i].rdlength; - } - for (int i = 0; i < packet->header.arcount; ++i) { - real_size += strlen(packet->additional[i].owner) - + 11 + packet->additional[i].rdlength; - } - - if (real_size > *packet_size) { - log_error("%s: Space provided is not enough.\n", __func__); - return -1; - } - - *packet_size = real_size; - - char *p = packet_wire; - - ((dnss_header *)p)->id = htons(packet->header.id); - ((dnss_header *)p)->flags = htons(packet->header.flags); - ((dnss_header *)p)->qdcount = htons(packet->header.qdcount); - ((dnss_header *)p)->ancount = htons(packet->header.ancount); - ((dnss_header *)p)->nscount = htons(packet->header.nscount); - ((dnss_header *)p)->arcount = htons(packet->header.arcount); - - p += sizeof(dnss_header); - - for (int i = 0; i < packet->header.qdcount; ++i) { - memcpy(p, packet->questions[i].qname, - strlen(packet->questions[i].qname) + 1); // copy domain name - p += strlen(packet->questions[i].qname) + 1; - *((uint16_t *)p) = htons(packet->questions[i].qtype); - p += 2; - *((uint16_t *)p) = htons(packet->questions[i].qclass); - p += 2; - } - for (int i = 0; i < packet->header.ancount; ++i) { - memcpy(p, packet->answers[i].owner, - strlen(packet->answers[i].owner) + 1); // copy owner name - p += strlen(packet->answers[i].owner) + 1; - *((uint16_t *)p) = htons(packet->answers[i].rrtype); - p += 2; - *((uint16_t *)p) = htons(packet->answers[i].rrclass); - p += 2; - *((uint32_t *)p) = htonl(packet->answers[i].ttl); - p += 4; - *((uint16_t *)p) = htons(packet->answers[i].rdlength); - p += 2; - memcpy(p, packet->answers[i].rdata, - packet->answers[i].rdlength); // copy rdata - p += packet->answers[i].rdlength; - } - for (int i = 0; i < packet->header.nscount; ++i) { - memcpy(p, packet->authority[i].owner, - strlen(packet->authority[i].owner) + 1); // copy owner name - p += strlen(packet->authority[i].owner) + 1; - *((uint16_t *)p) = htons(packet->authority[i].rrtype); - p += 2; - *((uint16_t *)p) = htons(packet->authority[i].rrclass); - p += 2; - *((uint32_t *)p) = htonl(packet->authority[i].ttl); - p += 4; - *((uint16_t *)p) = htons(packet->authority[i].rdlength); - p += 2; - memcpy(p, packet->authority[i].rdata, - packet->authority[i].rdlength); // copy rdata - p += packet->authority[i].rdlength; - } - for (int i = 0; i < packet->header.arcount; ++i) { - memcpy(p, packet->additional[i].owner, - strlen(packet->additional[i].owner) + 1); // copy owner name - p += strlen(packet->additional[i].owner) + 1; - *((uint16_t *)p) = htons(packet->additional[i].rrtype); - p += 2; - *((uint16_t *)p) = htons(packet->additional[i].rrclass); - p += 2; - *((uint32_t *)p) = htonl(packet->additional[i].ttl); - p += 4; - *((uint16_t *)p) = htons(packet->additional[i].rdlength); - p += 2; - memcpy(p, packet->additional[i].rdata, - packet->additional[i].rdlength); // copy rdata - p += packet->additional[i].rdlength; - } - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -int dnss_dname_to_wire( dnss_dname dname, dnss_dname_wire dname_wire, - uint size ) // TESTING!! -{ - if (dname_wire == NULL) { - log_error("%s: Bad buffer pointer provided.\n", __func__); - return -1; - } - - // check if there is enough space - if (dnss_wire_dname_size(&dname) > size) { - log_error("%s: Given buffer is not big enough.\n", __func__); - return -1; - } - - debug_dnss("Domain name to convert: %s.\n", dname); - - int w = 0; - char *c = dname; - char *buffer = malloc(strlen(dname) + 1); - - if (buffer == NULL) { - ERR_ALLOC_FAILED; - return -1; - } - - while (*c != '\0') { - - // Read label - uint8_t chars = 0; - while (*c != '.' && *c != '\0') { - buffer[++chars] = *c++; - } - buffer[0] = chars; // number of characters in this label - -// dnss_debug("Chars: %d, Buffer: %.*s\n", chars, chars + 1, buffer); - - memcpy(&dname_wire[w], buffer, chars + 1); // copy the label - w += chars + 1; - - if (*c == '.') { - c++; - } - } - - dname_wire[w] = '\0'; - - debug_dnss("Wire format of the domain name: %.*s\n", w + 1, dname_wire); - debug_dnss_hex(dname_wire, w + 1); - - free(buffer); - return 0; -} - -/*----------------------------------------------------------------------------*/ - -uint dnss_wire_dname_size( const dnss_dname *dname ) -{ - // if there is a trailing dot, size of the wire name will be the same as the - // size of the normal domain name (for each dot there is a number of chars) - // otherwise it is +1 - return ((*dname)[strlen(*dname) - 1] == '.') - ? (strlen(*dname) + 1) - : (strlen(*dname) + 2); -} - -/*----------------------------------------------------------------------------*/ - -dnss_packet *dnss_parse_query( const char *query_wire, uint size ) -{ - assert(size > 12); - - debug_dnss("%s called with query size %d.\n", __func__, size); - debug_dnss_hex(query_wire, size); - - dnss_packet *query = dnss_create_empty_packet(); - if (query == NULL) { - ERR_ALLOC_FAILED; - return NULL; - } - - int p = 0; - - memcpy(&(query->header), query_wire, sizeof(dnss_header)); - - debug_dnss("Header copied.\n"); - - // parse header - convert from network byte order - query->header.id = ntohs(query->header.id); - query->header.flags = ntohs(query->header.flags); - query->header.qdcount = ntohs(query->header.qdcount); - query->header.ancount = ntohs(query->header.ancount); - query->header.nscount = ntohs(query->header.nscount); - query->header.arcount = ntohs(query->header.arcount); - - debug_dnss("Header parsed: \n"); - debug_dnss("ID: %u\n", query->header.id); - debug_dnss("Flags: %u\n", query->header.flags); - debug_dnss("QDCOUNT: %u\n", query->header.qdcount); - debug_dnss("ANCOUNT: %u\n", query->header.ancount); - debug_dnss("NSCOUNT: %u\n", query->header.nscount); - debug_dnss("ARCOUNT: %u\n", query->header.arcount); - - p += sizeof(dnss_header); - - /* - * parse questions - */ - query->questions = malloc(query->header.qdcount * sizeof(dnss_question)); - - char buffer[MAX_DNAME_SIZE]; - uint b; - - for (int i = 0; i < query->header.qdcount; ++i) { - // we do not clear the buffer, hope it's not needed - b = 0; - // parse domain name - just copy it (ignoring possible compression!!) - while (query_wire[p] != '\0' && p < size) { - assert(b != size && b < MAX_DNAME_SIZE); // instead return FORMERR - buffer[b++] = query_wire[p++]; - } - - debug_dnss("Domain name parsed: \n"); - debug_dnss_hex(buffer, b); - - assert(b < MAX_DNAME_SIZE); // instead return FORMERR - assert(p + 4 < size); // instead return FORMERR - assert(query_wire[p] == '\0'); - - buffer[b++] = '\0'; - p++; - - query->questions[i].qname = malloc(b * sizeof(char)); - memcpy(query->questions[i].qname, buffer, b); - - debug_dnss("QNAME: \n"); - debug_dnss_hex(query->questions[i].qname, b); - - query->questions[i].qtype = ntohs(*((uint16_t *)(&(query_wire[p])))); - debug_dnss("QTYPE: %u\n", query->questions[i].qtype); - - p += 2; - query->questions[i].qclass = ntohs(*((uint16_t *)(&(query_wire[p])))); - debug_dnss("QCLASS: %u\n", query->questions[i].qclass); - - p += 2; - } - - /*! @todo add more checks for the length of the packet */ - - // ignore rest of the packet (TODO: should parse additional for OPT) - query->answers = NULL; - query->authority = NULL; - query->additional = NULL; - - return query; -} - -/*----------------------------------------------------------------------------*/ - -void dnss_destroy_rr( dnss_rr **rr ) -{ - assert(*rr != NULL); - debug_dnss("Deleting RR: owner: %s, type: %u, rdlength: %u.\n", (*rr)->owner, - (*rr)->rrtype, (*rr)->rdlength); - debug_dnss_hex((*rr)->owner, strlen((*rr)->owner)); - - if ((*rr)->owner != NULL) { - debug_dnss("Deleting RR's owner on pointer %p\n", (*rr)->owner); - free((*rr)->owner); - (*rr)->owner = NULL; - } - - if ((*rr)->rdata != NULL) { - debug_dnss("Deleting RR's rdata on pointer %p\n", (*rr)->rdata); - free((*rr)->rdata); - (*rr)->rdata = NULL; - } - - debug_dnss("Deleting RR on pointer %p\n", (*rr)); - free(*rr); - *rr = NULL; -} - -/*----------------------------------------------------------------------------*/ - -void dnss_destroy_question( dnss_question **question ) -{ - assert(*question != NULL); - - if ((*question) != NULL) { - free((*question)->qname); - (*question)->qname = NULL; - } - - free(*question); - *question = NULL; -} - -/*----------------------------------------------------------------------------*/ - -void dnss_destroy_packet( dnss_packet **packet ) -{ - assert(*packet != NULL); - - if ((*packet)->questions != NULL) { - for (int i = 0; i < (*packet)->header.qdcount; ++i) { - assert(((*packet)->questions[i].qname != NULL)); - free((*packet)->questions[i].qname); - } - free((*packet)->questions); - (*packet)->questions = NULL; - } - - if ((*packet)->answers != NULL) { - for (int i = 0; i < (*packet)->header.ancount; ++i) { - assert((*packet)->answers[i].owner != NULL); - assert((*packet)->answers[i].rdata != NULL); - free((*packet)->answers[i].owner); - free((*packet)->answers[i].rdata); - } - free((*packet)->answers); - (*packet)->answers = NULL; - } - - if ((*packet)->answers != NULL) { - for (int i = 0; i < (*packet)->header.nscount; ++i) { - assert((*packet)->authority[i].owner != NULL); - assert((*packet)->authority[i].rdata != NULL); - free((*packet)->authority[i].owner); - free((*packet)->authority[i].rdata); - } - free((*packet)->authority); - (*packet)->authority = NULL; - } - - if ((*packet)->additional != NULL) { - for (int i = 0; i < (*packet)->header.arcount; ++i) { - assert((*packet)->additional[i].owner != NULL); - assert((*packet)->additional[i].rdata != NULL); - free((*packet)->additional[i].owner); - free((*packet)->additional[i].rdata); - } - free((*packet)->additional); - (*packet)->additional = NULL; - } - - free(*packet); - *packet = NULL; -} - -/*----------------------------------------------------------------------------*/ - -char *dnss_dname_wire_to_string( dnss_dname_wire dname_wire ) -{ - return dname_wire; -} - -/*----------------------------------------------------------------------------*/ - -size_t dnss_dname_wire_length( dnss_dname_wire dname_wire ) -{ - return strlen(dname_wire) + 1; -} - -/*----------------------------------------------------------------------------*/ - -void dnss_dname_wire_cp( dnss_dname_wire from, dnss_dname_wire to ) -{ - memcpy(to, from, (strlen(from) + 1)); -} - -/*----------------------------------------------------------------------------*/ - -dnss_dname_wire dnss_dname_wire_copy( dnss_dname_wire from ) -{ - dnss_dname_wire dw = malloc((strlen(from) + 1) * sizeof(char)); - - if (dw == NULL) { - return NULL; - } - - dnss_dname_wire_cp(from, dw); - return dw; -} - -/*----------------------------------------------------------------------------*/ - -int dnss_dname_wire_cmp( dnss_dname_wire dname1, dnss_dname_wire dname2 ) -{ - int l1 = dnss_dname_wire_length(dname1); - int l2 = dnss_dname_wire_length(dname1); - - int res = memcmp(dname1, dname2, (l1 < l2) ? l1 : l2); - - return (res == 0) - ? ((l1 > l2) ? 1 : ((l1 < l2) ? -1 : 0)) - : res; -} - -/*----------------------------------------------------------------------------*/ - -void dnss_dname_wire_destroy( dnss_dname_wire *dname ) -{ - free(*dname); - *dname = NULL; -} - -/*----------------------------------------------------------------------------*/ - -uint dnss_dname_wire_match( const dnss_dname_wire *dname1, - const dnss_dname_wire *dname2 ) -{ - - int i1 = strlen(*dname1) - 1; // not counting the last 0 - int i2 = strlen(*dname2) - 1; // dtto - uint matched = 0; - - while (i1 >= 0 && i2 >= 0 && ((*dname1)[i1] == (*dname2)[i2])) { - --i1; - --i2; - ++matched; - } - - return matched; -} diff --git a/deprecated/dns/dns-simple.h b/deprecated/dns/dns-simple.h deleted file mode 100644 index 6e1a8b166cc79fd466883fbc331506d52bceb386..0000000000000000000000000000000000000000 --- a/deprecated/dns/dns-simple.h +++ /dev/null @@ -1,129 +0,0 @@ -/*! - * @file dns-simple.h - * - * @todo Create a type for domain name in wire format based on char* but - * maybe with some checks? Or try to create implicitly shared struct. - * Add also create() and destroy() functions, so we don't need to use - * malloc and free. - * @todo Use dnss_dname_wire * as parameters!! - * @todo Owner name in RR type is redundant - the domain name will be copied - * multiple times unnecessarily! At least move it to RRSet structure when - * designed. Even then it will be in multiple places - investigate!! - * @todo This whole library needs to be done again. - */ - -#ifndef DNS_SIMPLE -#define DNS_SIMPLE - -#include <stdint.h> -#include <string.h> -#include "common.h" -#include <sys/types.h> -#include <ldns/rr.h> - -static const unsigned int HEADER_SIZE = 12; -static const unsigned int MAX_DNAME_SIZE = 255; // contains the ending 0? - -/*----------------------------------------------------------------------------*/ - -typedef char * dnss_dname_wire; -typedef char * dnss_dname; - -/*----------------------------------------------------------------------------*/ - -struct dnss_rr { - dnss_dname_wire owner; // domain name in wire format - uint16_t rrtype; - uint16_t rrclass; - uint32_t ttl; - uint16_t rdlength; - unsigned char *rdata; -}; // size: (10 + rdlength + strlen(owner) + 1) B - -typedef struct dnss_rr dnss_rr; - -/*----------------------------------------------------------------------------*/ - -struct dnss_header { - uint16_t id; - uint16_t flags; - uint16_t qdcount; - uint16_t ancount; - uint16_t nscount; - uint16_t arcount; -}; - -typedef struct dnss_header dnss_header; - -/*----------------------------------------------------------------------------*/ - -struct dnss_question { - dnss_dname_wire qname; // domain name in wire format - uint16_t qtype; - uint16_t qclass; -}; - -typedef struct dnss_question dnss_question; - -/*----------------------------------------------------------------------------*/ - -struct dnss_packet { - dnss_header header; - dnss_question *questions; - dnss_rr *answers; - dnss_rr *authority; - dnss_rr *additional; -}; - -typedef struct dnss_packet dnss_packet; - -/*----------------------------------------------------------------------------*/ - -dnss_rr *dnss_create_rr( dnss_dname owner ); - -dnss_question *dnss_create_question( dnss_dname_wire qname, uint length ); - -dnss_packet *dnss_create_empty_packet(); - -int dnss_create_response( const dnss_packet *query, const ldns_rr_list *answers, - uint count, dnss_packet **response ); - -int dnss_create_error_response( dnss_packet *query, dnss_packet **response ); - -dnss_packet *dnss_parse_query( const char *query_wire, uint size ); - -int dnss_wire_format( dnss_packet *packet, char *packet_wire, - uint *packet_size ); - -int dnss_dname_to_wire( dnss_dname dname, dnss_dname_wire dname_wire, - uint size ); - -/** \bug C99 inline won't be linkable (so not exportable) without .c and has compatibility issues. - Consider static inline definition in header or removing inline. - */ -uint dnss_wire_dname_size( const dnss_dname *dname ); - -void dnss_destroy_rr( dnss_rr **rr ); - -void dnss_destroy_question( dnss_question **question ); - -void dnss_destroy_packet( dnss_packet **packet ); - -/*----------------------------------------------------------------------------*/ - -char *dnss_dname_wire_to_string( dnss_dname_wire dname_wire ); - -size_t dnss_dname_wire_length( dnss_dname_wire dname_wire ); - -//void dnss_dname_wire_copy( dnss_dname_wire from, dnss_dname_wire to ); - -dnss_dname_wire dnss_dname_wire_copy( dnss_dname_wire from ); - -int dnss_dname_wire_cmp( dnss_dname_wire dname1, dnss_dname_wire dname2 ); - -void dnss_dname_wire_destroy( dnss_dname_wire *dname ); - -uint dnss_dname_wire_match( const dnss_dname_wire *dname1, - const dnss_dname_wire *dname2 ); - -#endif /* DNS_SIMPLE */ diff --git a/deprecated/dns/dns-utils.c b/deprecated/dns/dns-utils.c deleted file mode 100644 index eb25d5df5703a6b3722c9635510fa35cff1fe5bf..0000000000000000000000000000000000000000 --- a/deprecated/dns/dns-utils.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "dns-utils.h" -#include <ldns/rdata.h> -#include "common.h" -#include <string.h> -#include <assert.h> -#include <ctype.h> - -static const uint MAX_LABELS = 127; - -/*----------------------------------------------------------------------------*/ - -uint dnsu_subdomain_labels( const ldns_rdf *sub, const ldns_rdf *parent ) -{ - uint8_t *labels_sub[MAX_LABELS], *labels_par[MAX_LABELS]; // 254 B - - assert(ldns_rdf_get_type(sub) == LDNS_RDF_TYPE_DNAME); - assert(ldns_rdf_get_type(parent) == LDNS_RDF_TYPE_DNAME); - - size_t sub_size = ldns_rdf_size(sub); - size_t par_size = ldns_rdf_size(parent); - - if (sub_size == 0 || par_size == 0) { - return 0; - } - - uint8_t *sub_data = ldns_rdf_data(sub); - uint8_t *par_data = ldns_rdf_data(parent); - - /* - * walk the domain names from left to right and save labels start/end - * positions - */ - // walk sub - uint8_t *pos_sub = sub_data; - uint8_t len = *pos_sub; - uint label_sub_i = 0; - - assert(sub_data[sub_size - 1] == '\0'); - - while (len != 0) { - ++pos_sub; - labels_sub[label_sub_i++] = pos_sub; - pos_sub += len; - len = *pos_sub; - } - // save the pointer to the terminating 0 - labels_sub[label_sub_i] = pos_sub; - - assert(labels_sub[label_sub_i] == sub_data + sub_size - 1); - assert(len == 0); - assert(pos_sub == sub_data + sub_size - 1); - - // walk parent - uint8_t *pos_par = par_data; - len = *pos_par; - uint label_par_i = 0; - - assert(par_data[par_size - 1] == '\0'); - - while (len != 0) { - labels_par[label_par_i++] = pos_par; - ++pos_par; - pos_par += len; - len = *pos_par; - } - // save the pointer to the terminating 0 - labels_par[label_par_i] = pos_par; - - assert(labels_par[label_par_i] == par_data + par_size - 1); - assert(len == 0); - assert(pos_par == par_data + par_size - 1); - - // if parent has more labels than sub, then sub cannot be subdomain or the - // same domain - if (label_par_i > label_sub_i) { - return 0; - } - - /* - * Now we know that sub may be a subdomain of parent. - * Walk through labels from end to start and count labels which are equal - * case-insensitive. The characters in labels are walked from right to left. - */ - uint matched = 0; - while (label_par_i > 0) { - // set positions on the end of labels - pos_sub = labels_sub[label_sub_i]; - pos_par = labels_par[label_par_i]; - - --label_par_i; - --label_sub_i; - - uint8_t *pos_par_next = labels_par[label_par_i]; - - do { - // in first iteration we skip the labels' length / the last 0 - --pos_sub; - --pos_par; - } while (pos_par != pos_par_next - && (tolower(*pos_par) == tolower(*pos_sub))); - - // if the whole label was matched, we should be at the end of the next - // label - if (pos_par != pos_par_next) { - return matched; - } - // so just continue - ++matched; - } - - return matched; -} diff --git a/deprecated/dns/dns-utils.h b/deprecated/dns/dns-utils.h deleted file mode 100644 index b0ad38f547b5371c18f563f1fff084b053ca08b6..0000000000000000000000000000000000000000 --- a/deprecated/dns/dns-utils.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef DNS_UTILS -#define DNS_UTILS - -#include <ldns/rdata.h> -#include "common.h" - -uint dnsu_subdomain_labels( const ldns_rdf *sub, const ldns_rdf *parent ); - -#endif /* DNS_UTILS */ diff --git a/deprecated/zone-data-structure.c b/deprecated/zone-data-structure.c deleted file mode 100644 index f30fab39410f464bb2ebb4f0bc6282922c3e7692..0000000000000000000000000000000000000000 --- a/deprecated/zone-data-structure.c +++ /dev/null @@ -1,64 +0,0 @@ -#include <assert.h> -#include <stdio.h> - -#include <ldns/rdata.h> - -#include "zone-data-structure.h" -#include "cuckoo-hash-table.h" -#include "zone-node.h" - -/*----------------------------------------------------------------------------*/ - -zds_zone_t *zds_create(uint item_count) -{ - ck_hash_table_t *table = ck_create_table(item_count); - return table; -} - -/*----------------------------------------------------------------------------*/ - -int zds_insert(zds_zone_t *zone, zn_node_t *node) -{ - assert(zn_owner(node) != NULL); - assert(ldns_rdf_get_type(zn_owner(node)) == LDNS_RDF_TYPE_DNAME); - return ck_insert_item(zone, (char *)ldns_rdf_data(zn_owner(node)), - ldns_rdf_size(zn_owner(node)), node); -} - -/*----------------------------------------------------------------------------*/ - -zn_node_t *zds_find(zds_zone_t *zone, const ldns_rdf *owner) -{ - assert(ldns_rdf_get_type(owner) == LDNS_RDF_TYPE_DNAME); - const ck_hash_table_item_t *item = ck_find_item(zone, - (char *)ldns_rdf_data(owner), - ldns_rdf_size(owner)); - if (item == NULL) { - return NULL; - } - - debug_zdb("Item found\n"); - - return item->value; -} - -/*----------------------------------------------------------------------------*/ - -int zds_remove(zds_zone_t *zone, ldns_rdf *owner) -{ - assert(ldns_rdf_get_type(owner) == LDNS_RDF_TYPE_DNAME); - if (ck_remove_item(zone, (char *)ldns_rdf_data(owner), - ldns_rdf_size(owner), zn_destructor, 0) != 0) { - log_info("Trying to remove non-existing item: %s\n", - ldns_rdf_data(owner)); - return -1; - } - return 0; -} - -/*----------------------------------------------------------------------------*/ - -void zds_destroy(zds_zone_t **zone, void (*dtor_zone_node)(void *value)) -{ - ck_destroy_table(zone, dtor_zone_node, 0); -} diff --git a/deprecated/zone-data-structure.h b/deprecated/zone-data-structure.h deleted file mode 100644 index 3badcdebcd23bd5d67855da1e57b2fd18204802c..0000000000000000000000000000000000000000 --- a/deprecated/zone-data-structure.h +++ /dev/null @@ -1,93 +0,0 @@ -/*! - * \file zone-data-structure.h - * - * \author Lubos Slovak <lubos.slovak@nic.cz> - * - * \brief Generic interface to data structure for representing DNS zone. - * - * This allows to easily change the underlying data structure without affecting - * the rest of the code. - * - * The API contains functions for creating and destroying zones as well as for - * inserting, removing and searching for domain names. - * - * \addtogroup zonedb - * @{ - */ -#ifndef _CUTEDNS_ZONE_DATA_STRUCTURE_H_ -#define _CUTEDNS_ZONE_DATA_STRUCTURE_H_ - -#include <ldns/rdata.h> - -#include "common.h" -#include "cuckoo-hash-table.h" -#include "zone-node.h" - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Zone data structure implemented as a cuckoo hash table. - * - * \see ck-hash-table.h. - */ -typedef ck_hash_table_t zds_zone_t; - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Allocates and initializes the structure. - * - * \param item_count Number of items in the zone. It may be used to create a - * structure of appropriate size if the underlying structure - * supports it. - * - * \return Pointer to the created zone data structure. - */ -zds_zone_t *zds_create(uint item_count); - -/*! - * \brief Inserts one zone node to the given zone. - * - * \param zone Zone data structure to insert into. - * \param contents The zone node to be inserted. - * - * \retval 0 On success. - * \retval -1 If an error occured during insertion to the zone. - * - * \todo Should return positive integer when the item was inserted, but - * something went wrong. Otherwise negative. - */ -int zds_insert(zds_zone_t *zone, zn_node_t *node); - -/*! - * \brief Tries to find the given name in the zone and returns corresponding - * zone node. - * - * \param zone Zone data structure to search in. - * \param owner Domain name to find, in wire format (i.e. a null-terminated - * string). - * - * \return Proper zone node for the given name or NULL if not found. - */ -zn_node_t *zds_find(zds_zone_t *zone, const ldns_rdf *owner); - -/*! - * \brief Removes zone node corresponding to the given domain name from the - * given zone if such name exists in the zone. - * - * \param zone Zone data structure to remove from. - * \param owner Domain name of the node to be removed. - * - * \retval 0 On success. - * \retval -1 If the name was not found in the zone. - * - * \todo Maybe return the removed node? - */ -int zds_remove(zds_zone_t *zone, ldns_rdf *owner); - -/*! - * \brief Properly destroys the given zone data structure. - */ -void zds_destroy(zds_zone_t **zone, void (*dtor_zone_node)(void *value)); - -#endif /* _CUTEDNS_ZONE_DATA_STRUCTURE_H_ */ - -/*! @} */ diff --git a/deprecated/zone-database.c b/deprecated/zone-database.c deleted file mode 100644 index e9cc9c39841691ebd7e801a933a1c6f7d2b7d73a..0000000000000000000000000000000000000000 --- a/deprecated/zone-database.c +++ /dev/null @@ -1,1316 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include <ldns/ldns.h> -#include <urcu.h> - -#include "zone-database.h" -#include "common.h" -#include "dns-utils.h" - -/*----------------------------------------------------------------------------*/ -/* Private functions */ -/*----------------------------------------------------------------------------*/ - -static void zdb_find_zone(zdb_database_t *database, ldns_rdf *zone_name, - zdb_zone_t **zone, zdb_zone_t **prev) -{ - *zone = database->head; - *prev = NULL; - - // start of RCU reader critical section - rcu_read_lock(); - - while ((*zone) != NULL - && ldns_dname_compare((*zone)->zone_name, zone_name)) { - (*prev) = (*zone); - (*zone) = (*zone)->next; - } - - // end of RCU reader critical section - rcu_read_unlock(); -} - -/*----------------------------------------------------------------------------*/ - -static void zdb_disconnect_zone(zdb_database_t *database, zdb_zone_t *z, - zdb_zone_t *prev) -{ - // disconect the zone from the list - if (prev != NULL) { - assert(prev->next == z); - prev->next = z->next; - } else { - assert(database->head == z); - database->head = z->next; - } -} - -/*----------------------------------------------------------------------------*/ - -static uint zdb_create_list(zdb_zone_t *zone, ldns_zone *zone_ldns) -{ - uint nodes = 0; - - debug_zdb("Creating linked list of zone nodes...\n"); - - // sort the zone so we obtain RRSets - ldns_zone_sort(zone_ldns); - - debug_zdb("Done.\nProcessing RRSets...\n"); - /* - * Walk through all RRs, separate them into zone nodes and RRSets - * and create a linked list of nodes in canonical order. - * - * Some idiot implemented ldns_rr_list_pop_rrset() to return the LAST - * RRSet so we will fill the zone from the last node to the first. - */ - zn_node_t *act_node = NULL; - zn_node_t *last_node = NULL; - - uint rr_count = ldns_zone_rr_count(zone_ldns); - uint i = 0; - uint step = rr_count / 10; - uint next = step; - log_info("Processed: "); - - while (i < rr_count) { - ldns_rr_list *rrset = ldns_rr_list_pop_rrset( - ldns_zone_rrs(zone_ldns)); - i += ldns_rr_list_rr_count(rrset); - - if (i >= next) { - log_info("%.0f%% ", 100 *((float)next / rr_count)); - next += step; - } - - if (rrset == NULL) { - log_error("Unknown error while processing zone %s.\n", - ldns_rdf2str(zone->zone_name)); - // ignore rest of the zone - break; - } - debug_zdb("Processing RRSet with owner %s and type %s.\n", - ldns_rdf2str(ldns_rr_list_owner(rrset)), - ldns_rr_type2str(ldns_rr_list_type(rrset))); - - if (act_node != NULL - && ldns_dname_compare(ldns_rr_list_owner(rrset), - act_node->owner) == 0) { - // same owner, insert into the same node - debug_zdb("Inserting into node with owner %s.\n", - ldns_rdf2str(act_node->owner)); - if (zn_add_rrset(act_node, rrset) != 0) { - log_error("Error while processing zone %s: " - "Cannot add RRSet to a zone node.\n", - ldns_rdf2str(zone->zone_name)); - // ignore rest of the zone - break; - } - } else { - // create a new node, add the RRSet and connect to the - // list - debug_zdb("Creating new node.\n"); - zn_node_t *new_node = zn_create(); - if (new_node == NULL) { - log_error("Error while processing zone %s: " - "Cannot create new zone node.\n", - ldns_rdf2str(zone->zone_name)); - // ignore rest of the zone - break; - } - if (zn_add_rrset(new_node, rrset) != 0) { - log_error("Error while processing zone %s: " - "Cannot add RRSet to a zone node.\n", - ldns_rdf2str(zone->zone_name)); - // ignore rest of the zone - free(new_node); - break; - } - new_node->next = act_node; - if (act_node != NULL) { - act_node->prev = new_node; - } else { - last_node = new_node; - } - act_node = new_node; // continue with the next node - ++nodes; - } - } - - log_info("(%d RRs)\n", i); - - debug_zdb("Processing of RRSets done.\nLast node created (should be " - "zone apex): %s, last node of the list: %s.\n", - ldns_rdf2str(act_node->owner), ldns_rdf2str(last_node->owner) - ); - - // connect last node to the apex, creating cyclic list - last_node->next = act_node; - act_node->prev = last_node; - // save the zone apex - zone->apex = act_node; - - debug_zdb("Done.\nAdding SOA RR to the apex node...\n"); - - if (zn_add_rr(zone->apex, ldns_rr_clone(ldns_zone_soa(zone_ldns))) != 0 - || skip_is_empty(zone->apex->rrsets)) { - log_error("Error while processing zone %s: Cannot insert SOA RR" - "into the zone apex node.\n", - ldns_rdf2str(zone->zone_name)); - free(zone->apex); - return nodes; - } - - return nodes; -} - -/*----------------------------------------------------------------------------*/ - -static uint zdb_common_labels(const ldns_rdf *dname1, const ldns_rdf *dname2) -{ - uint common = 0; - ldns_rdf *dname1r = ldns_dname_reverse(dname1); - ldns_rdf *dname2r = ldns_dname_reverse(dname2); - - uint8_t *c1 = ldns_rdf_data(dname1r); - uint8_t *c2 = ldns_rdf_data(dname2r); - - while (*c1 != '\0' && *c1 == *c2 - && strncmp((char *)c1 + 1, (char *)c2 + 1, *c1) == 0) { - debug_zdb("Comparing labels of length %u: %.*s and %.*s\n", - *c1, *c1, (char *)c1 + 1, *c1, (char *)c2 + 1); - c1 += *c1 + 1; - c2 += *c2 + 1; - ++common; - } - - ldns_rdf_deep_free(dname1r); - ldns_rdf_deep_free(dname2r); - - return common; -} - -/*----------------------------------------------------------------------------*/ - -static void zdb_connect_node(zn_node_t *next, zn_node_t *node) -{ - node->prev = next->prev; - node->next = next; - next->prev->next = node; - next->prev = node; -} - -/*----------------------------------------------------------------------------*/ - -static int zdb_add_empty_nonterminals(zdb_zone_t *zone) -{ - debug_zdb("\nCreating empty non-terminals in the zone...\n"); - int created = 0; - - zn_node_t *current = zone->apex->next; - zn_node_t *parent = zone->apex; - uint8_t apex_labels = ldns_dname_label_count(parent->owner); - - while (current != zone->apex) { - zn_node_t *prev = current->prev; - ldns_rdf *curr_name = current->owner; - - debug_zdb("Current node: %s\n", ldns_rdf2str(curr_name)); - - if (ldns_dname_is_subdomain(curr_name, prev->owner)) { - // descendant of the previous node - parent = prev; - } else if (parent != prev && - !ldns_dname_is_subdomain(curr_name, parent->owner)) { - // we must find appropriate parent - // number of labels matching between current and parent - uint common = zdb_common_labels(curr_name, - parent->owner); - debug_zdb("Common labels with parent node (%s): %u\n", - ldns_rdf2str(parent->owner), common); - assert(common < ldns_dname_label_count(parent->owner)); - assert(common >= ldns_dname_label_count( - zone->apex->owner)); - - if (common == apex_labels) { - parent = zone->apex; - } else { - while (ldns_dname_label_count(parent->owner) - > common) { - parent = parent->prev; - } - } - } - - uint d = ldns_dname_label_count(curr_name) - - ldns_dname_label_count(parent->owner); - - debug_zdb("Parent node: %s, difference in label counts: %u\n", - ldns_rdf2str(parent->owner), d); - - prev = current; - - // if the difference in label length is more than one, create - // the empty non-terminal nodes - if (d > 1) { - do { - ldns_rdf *new_name = ldns_dname_left_chop( - curr_name); - if (new_name == NULL) { - log_error("Unknown error in " - "ldns_dname_left_chop().\n"); - return -1; - } - zn_node_t *new_node = zn_create(); - if (new_node == NULL) { - ldns_rdf_deep_free(new_name); - return -2; - } - new_node->owner = new_name; - - debug_zdb("Inserting new node with owner %s to " - "the list.\n",ldns_rdf2str(new_name)); - zdb_connect_node(current, new_node); - ++created; - - current = new_node; - curr_name = new_name; - --d; - } while (d > 1); - - // save the created node with most labels as new parent - parent = prev->prev; - } - - // if no new nodes were created, the parent remains the same - - current = prev->next; - } - - debug_zdb("Done, created nodes: %d\n\n", created); - - return created; -} - -/*----------------------------------------------------------------------------*/ - -static void zdb_delete_list_items(zdb_zone_t *zone) -{ - zn_node_t *node = zone->apex; - zn_node_t *old_node; - while (node->next != node) { - old_node = node; - node = node->next; - - assert(old_node->prev != NULL); - - old_node->prev->next = old_node->next; - old_node->next->prev = old_node->prev; - zn_destroy(&old_node); - } - - zn_destroy(&node); -} - -/*----------------------------------------------------------------------------*/ - -static zn_node_t *zdb_find_name_in_zone_nc(const zdb_zone_t *zone, - const ldns_rdf *dname) -{ - assert(zone != NULL); - // start of RCU reader critical section - rcu_read_lock(); - - zn_node_t *found = zds_find(zone->zone, dname); - - // end of RCU reader critical section - rcu_read_unlock(); - - return found; -} - -/*----------------------------------------------------------------------------*/ - -//static zn_node_t *zdb_find_name_in_list(const zdb_zone *zone, ldns_rdf *name) -//{ -// zn_node_t *node = zone->apex; -// int cmp; -// while ((cmp = ldns_dname_match_wildcard(name, node->owner)) != 1 -// && node->next != zone->apex) { -// node = node->next; -// } - -// return (cmp == 1) ? node : NULL; -//} - -/*----------------------------------------------------------------------------*/ - -static zn_node_t *zdb_find_name_or_wildcard(const zdb_zone_t *zone, - ldns_rdf *name) -{ - assert(ldns_rdf_get_type(name) == LDNS_RDF_TYPE_DNAME); - debug_zdb("zdb_find_name_or_wildcard(), name: %s.", ldns_rdf2str(name)); - zn_node_t *node = zdb_find_name_in_zone_nc(zone, name); - - if (node == NULL) { - ldns_rdf *name_orig = ldns_dname_clone_from(name, 0); - uint labels = ldns_dname_label_count(name_orig); - - do { - debug_zdb("Chopping leftmost label from name %s.\n", - ldns_rdf2str(name_orig)); - ldns_rdf *name_new = ldns_dname_left_chop(name_orig); - assert(ldns_rdf_get_type(name_new) - == LDNS_RDF_TYPE_DNAME); - // replace last label with * and search - ldns_rdf *wildcard = ldns_dname_new_frm_str("*"); - - if (ldns_dname_cat(wildcard, name_new) - != LDNS_STATUS_OK) { - log_error("Unknown error occured.\n"); - ldns_rdf_deep_free(wildcard); - ldns_rdf_deep_free(name_new); - break; - } - - assert(ldns_rdf_get_type(wildcard) - == LDNS_RDF_TYPE_DNAME); - debug_zdb("Searching for name %s in the hash table.\n", - ldns_rdf2str(wildcard)); - node = zdb_find_name_in_zone_nc(zone, wildcard); - - ldns_rdf_deep_free(wildcard); - ldns_rdf_deep_free(name_orig); - name_orig = name_new; - --labels; - } while (node == NULL && labels > 0); - - ldns_rdf_deep_free(name_orig); - } - - return node; -} - -/*----------------------------------------------------------------------------*/ - -static int zdb_adjust_cname(zdb_zone_t *zone, zn_node_t *node) -{ - int res = 0; - ldns_rr_list *cname_rrset = zn_find_rrset(node, LDNS_RR_TYPE_CNAME); - if (cname_rrset != NULL) { - res = 1; - // retreive the canonic name - debug_zdb("Found CNAME, resolving...\n"); - ldns_rdf *cname = ldns_rr_rdf( - ldns_rr_list_rr(cname_rrset, 0), 0); - assert(ldns_rdf_get_type(cname) == LDNS_RDF_TYPE_DNAME); - debug_zdb("Canonical name for alias %s is %s\n", - ldns_rdf2str(node->owner), ldns_rdf2str(cname)); - - zn_node_t *cname_node = zdb_find_name_or_wildcard(zone, cname); - - if (cname_node - && zn_add_referrer_cname(cname_node, node) != 0) { - log_error("Error saving referrer node to node %s\n", - ldns_rdf2str(cname_node->owner)); - return -1; - } - zn_set_ref_cname(node, cname_node); - - debug_zdb("Found node: %s\n\n", (node->ref.cname) - ? ldns_rdf2str(node->ref.cname->owner) - : "(nil)"); - } - return res; -} - -/*----------------------------------------------------------------------------*/ -/*! - * \todo We should remove the reference from the node, as we will be never able - * to clear it once the referred node gets deleted. - * - * \note Must be called after inserting all nodes into the zone data structure. - */ -static void zdb_adjust_additional(zdb_zone_t *zone, zn_node_t *node, - ldns_rr_type type) -{ - ldns_rr_list *rrset = zn_find_rrset(node, type); - if (rrset == NULL) { - return; - } - - // for each MX RR find the appropriate node in the zone (if any) - // and save a reference to it in the zone node - debug_zdb("\nFound %s, searching for corresponding A/AAAA " - "records...\n", ldns_rr_type2str(type)); - int count = ldns_rr_list_rr_count(rrset); - for (int i = 0; i < count; ++i) { - ldns_rdf *name; - - switch (type) { - case LDNS_RR_TYPE_MX: - name = ldns_rr_mx_exchange(ldns_rr_list_rr(rrset, i)); - break; - case LDNS_RR_TYPE_NS: - name = ldns_rr_ns_nsdname(ldns_rr_list_rr(rrset, i)); - break; - case LDNS_RR_TYPE_SRV: - // constant - name = ldns_rr_rdf(ldns_rr_list_rr(rrset, i), 3); - if (ldns_dname_label_count(name) == 0) { - // ignore - debug_zdb("SRV with empty name as a " - "target: %s\n", - ldns_rr2str(ldns_rr_list_rr(rrset, i)) - ); - return; - } - assert(ldns_rdf_get_type(name) == LDNS_RDF_TYPE_DNAME); - break; - default: - assert(0); - } - - assert(name != NULL); - debug_zdb("Searching for A/AAAA for %s name %s.\n", - ldns_rr_type2str(type), ldns_rdf2str(name)); - - // the authoritative nodes should already be in the hash table - zn_node_t *found = zdb_find_name_or_wildcard(zone, name); - - if (found == NULL) { - return; - } - - debug_zdb("Found node: %s\n\n", (found) - ? ldns_rdf2str(found->owner) : "(nil)"); - - if (zn_find_rrset(found, LDNS_RR_TYPE_CNAME) != NULL) { - debug_zdb("Found CNAME within the node, saving.\n"); - if (zn_add_ref(node, name, type, NULL, found) != 0) { - log_error("Error occured while saving A RRSet " - "for %s record in node %s\n\n", - ldns_rr_type2str(type), - ldns_rdf2str(node->owner)); - } - if (zn_add_referrer(found, node, type) != 0) { - log_error("Error occured while saving referrer " - "node to node %s\n", - ldns_rdf2str(found->owner)); - } - debug_zdb("Done.\n\n"); - continue; - } - - ldns_rr_list *rrset = zn_find_rrset(found, LDNS_RR_TYPE_A); - if (rrset != NULL) { - debug_zdb("Found A RRSet within the node, saving.\n"); - if (zn_add_ref(node, name, type, rrset, NULL) != 0) { - log_error("Error occured while saving A RRSet " - "for %s record in node %s\n\n", - ldns_rr_type2str(type), - ldns_rdf2str(node->owner)); - return; - } - if (zn_add_referrer(found, node, type) != 0) { - log_error("Error occured while saving referrer " - "node to node %s\n", - ldns_rdf2str(found->owner)); - } - } - - rrset = zn_find_rrset(found, LDNS_RR_TYPE_AAAA); - if (rrset != NULL) { - debug_zdb("Found AAAA RRSet within the node, saving\n"); - if (zn_add_ref(node, name, type, rrset, NULL) != 0) { - log_error("Error occured while saving AAAA " - "RRSet for %s record in node %s\n\n", - ldns_rr_type2str(type), - ldns_rdf2str(node->owner)); - return; - } - if (zn_add_referrer(found, node, type) != 0) { - log_error("Error occured while saving referrer " - "node to node %s\n", - ldns_rdf2str(found->owner)); - } - } - debug_zdb("Done.\n\n"); - } -} - -/*----------------------------------------------------------------------------*/ -/*! - * \note Must be called after inserting all nodes into the zone data structure. - */ -static void zdb_adjust_additional_apex(zdb_zone_t *zone, zn_node_t *node) -{ - zdb_adjust_additional(zone, node, LDNS_RR_TYPE_MX); - zdb_adjust_additional(zone, node, LDNS_RR_TYPE_NS); - zdb_adjust_additional(zone, node, LDNS_RR_TYPE_SRV); -} - -/*----------------------------------------------------------------------------*/ -/*! - * \note Must be called after inserting all nodes into the zone data structure. - */ -static void zdb_adjust_additional_all(zdb_zone_t *zone, zn_node_t *node) -{ - zdb_adjust_additional(zone, node, LDNS_RR_TYPE_MX); - // no need to adjust NS, as they may be only in zone apex - //zdb_adjust_additional(zone, node, LDNS_RR_TYPE_NS); - zdb_adjust_additional(zone, node, LDNS_RR_TYPE_SRV); -} - -/*----------------------------------------------------------------------------*/ -/*! - * \return Found matching domain name even if \a name is a wildcard, or NULL - * if not found. - */ -static ldns_rdf *zdb_dname_list_find(ldns_rdf **list, size_t count, - ldns_rdf *name) -{ - int i = 0; - int found = 0; - while (i < count - && (found = ldns_dname_match_wildcard(list[i], name)) != 1) { - ++i; - } - if (found == 1) { - return list[i]; - } else { - return NULL; - } -} - -/*----------------------------------------------------------------------------*/ - -//static ldns_rdf **zdb_extract_ns(ldns_rr_list *ns_rrset, size_t count) -//{ -// assert(ldns_is_rrset(ns_rrset)); -// ldns_rdf **ns_rrs = malloc(count * sizeof(ldns_rr_list *)); -// if (ns_rrs == NULL) { -// ERR_ALLOC_FAILED; -// return NULL; -// } -// for (int i = 0; i < count; ++i) { -// ns_rrs[i] = ldns_rr_rdf(ldns_rr_list_rr(ns_rrset, i), 0); -// debug_zdb("NS RR #%d: %s\n", i, ldns_rdf2str(ns_rrs[i])); -// } -// return ns_rrs; -//} - -/*----------------------------------------------------------------------------*/ - -static int zdb_rr_list_contains_dname(const ldns_rr_list *rrset, - const ldns_rdf *dname, size_t pos) -{ - assert(ldns_rdf_get_type(dname) == LDNS_RDF_TYPE_DNAME); - for (int i = 0; i < ldns_rr_list_rr_count(rrset); ++i) { - if (ldns_dname_match_wildcard(ldns_rr_rdf(ldns_rr_list_rr( - rrset, i), pos), dname)) { - return 1; - } - } - return 0; -} - -/*----------------------------------------------------------------------------*/ - -static int zdb_process_nonauth(zn_node_t *node, ldns_rr_list *ns_rrset, - ldns_rdf **processed, size_t *count, - zn_node_t *deleg) -{ - zn_set_non_authoritative(node); - - if (zn_is_empty(node)) { - return 0; - } - - if (!zdb_rr_list_contains_dname(ns_rrset, node->owner, 0)) { - log_error("Zone contains non-authoritative domain name %s," - " which is not referenced in %s NS records!\n", - ldns_rdf2str(node->owner), ldns_rdf2str(deleg->owner) - ); - return -3; - } - if (processed != NULL) { - assert(count != NULL); - // save the dname as processed - processed[(*count)++] = node->owner; - } - - debug_zdb("Saving glues from node %s\n", ldns_rdf2str(node->owner)); - // push the glues to the delegation point node - int res = zn_push_glue(deleg, zn_find_rrset(node, LDNS_RR_TYPE_A)); - res += zn_push_glue(deleg, zn_find_rrset(node, LDNS_RR_TYPE_AAAA)); - - if (res != 0) { - log_error("Error while saving glue records for delegation point" - " %s\n", ldns_rdf2str(deleg->owner)); - return -4; - } - - debug_zdb("Saved %d glue records.\n", - ldns_rr_list_rr_count(zn_get_glues(deleg))); - - return 0; -} - -/*----------------------------------------------------------------------------*/ -/*! - * \note Must be called after inserting all nodes into the zone data structure. - */ -static int zdb_find_other_glues(const zdb_zone_t *zone, zn_node_t *deleg_point, - ldns_rr_list *ns_rrset, ldns_rdf **processed, - size_t proc_count) -{ - debug_zdb("Some NS names are probably elsewhere in the zone, or " - "outside the zone\n"); - size_t ns_count = ldns_rr_list_rr_count(ns_rrset); - int i = 0; - while (proc_count < ns_count && i < ns_count) { - ldns_rdf *ns = /*ldns_rr_owner(ldns_rr_list_rr(ns_rrset, i));*/ - ldns_rr_rdf(ldns_rr_list_rr(ns_rrset, i), 0); - assert(ldns_rdf_get_type(ns) == LDNS_RDF_TYPE_DNAME); - if (zdb_dname_list_find(processed, proc_count, ns) == NULL) { - debug_zdb("NS name %s not found under deleg point %s\n", - ldns_rdf2str(ns), - ldns_rdf2str(deleg_point->owner)); - - // we must search in the list as the other nodes may not - // be inserted into the table yet - //zn_node *ns_node = zdb_find_name_in_list(zone, ns); - zn_node_t *ns_node = - zdb_find_name_or_wildcard(zone, ns); - - if (ns_node != NULL && - !zn_is_non_authoritative(ns_node)) { - debug_zdb("Found in authoritative data, " - "extracting glues.\n"); - int res = zn_push_glue(deleg_point, - zn_find_rrset(ns_node, - LDNS_RR_TYPE_A)); - res += zn_push_glue(deleg_point, - zn_find_rrset(ns_node, - LDNS_RR_TYPE_AAAA)); - if (res != 0) { - log_error("Error while saving glues " - "for delegation point %s\n", - ldns_rdf2str( - deleg_point->owner)); - return -4; - } - } - processed[proc_count++] = ns; - } - ++i; - } - return 0; -} - -/*----------------------------------------------------------------------------*/ - -static void zdb_set_delegation_point(zn_node_t **node) -{ - debug_zdb("Setting %s to be a delegation point and skipping its " - "subdomains\n", ldns_rdf2str((*node)->owner)); - zn_set_delegation_point(*node); - zn_node_t *deleg = *node; - while (ldns_dname_is_subdomain((*node)->next->owner, deleg->owner)) { - (*node) = (*node)->next; - } -} - -/*----------------------------------------------------------------------------*/ - -static int zdb_adjust_delegation_point(const zdb_zone_t *zone, zn_node_t **node) -{ - int res = 0; - - ldns_rr_list *ns_rrset = zn_find_rrset(*node, LDNS_RR_TYPE_NS); - if (ns_rrset != NULL) { - //zn_set_delegation_point(*node); - res = 1; - - debug_zdb("\nAdjusting delegation point %s\n", - ldns_rdf2str((*node)->owner)); - - size_t ns_count = ldns_rr_list_rr_count(ns_rrset); - - // mark all subsequent nodes which are subdomains of this node's - // owner as non authoritative and extract glue records from them - zn_node_t *deleg = *node; - ldns_rdf **processed = malloc(ns_count * sizeof(ldns_rdf *)); - memset(processed, 0, ns_count * sizeof(ldns_rdf *)); - size_t proc_count = 0; - - while (ldns_dname_is_subdomain((*node)->next->owner, - deleg->owner)) { - (*node) = (*node)->next; - if ((res = zdb_process_nonauth(*node, ns_rrset, - processed, &proc_count, - deleg)) < 0) { - break; - } - } - - if (proc_count < ns_count - && zdb_find_other_glues(zone, deleg, ns_rrset, - processed, proc_count) != 0) { - res = -1; - } - - free(processed); - // set to last processed node - debug_zdb("Done.\n\n"); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -static int zdb_insert_node_to_zone(zdb_zone_t *zone, zn_node_t *node) -{ - zn_node_t *n = zone->apex; - int cmp; - zn_node_t *deleg = NULL; - - // if there is no zone apex, only node with SOA record may be inserted - if (zone->apex == NULL) { - ldns_rr_list *soa_rrset = zn_find_rrset(node, LDNS_RR_TYPE_SOA); - if (soa_rrset == NULL) { - log_error("Trying to insert node %s with not SOA record" - "to an empty zone!\n", - ldns_rdf2str(node->owner)); - return -1; - } - if (ldns_rr_list_rr_count(soa_rrset) > 1) { - log_info("More than one SOA record in node %s, ignoring" - "other.\n", ldns_rdf2str(node->owner)); - } - if (ldns_dname_compare(zone->zone_name, node->owner) != 0) { - log_error("Trying to insert node %s with SOA record to " - "zone with different name %s.\n", - ldns_rdf2str(node->owner), - ldns_rdf2str(zone->zone_name)); - return -2; - } - zone->apex = node; - - // insert the node into the zone data structure - if (zds_insert(zone->zone, node) != 0) { - return -6; - } - - return 0; - } - - // find right place for the node to be connected - while ((cmp = ldns_dname_compare(n->owner, node->owner)) < 0) { - if (deleg == NULL && zn_is_delegation_point(n)) { - // start of delegated nodes - deleg = n; - } else if (deleg != NULL && !zn_is_delegation_point(n)) { - // end of delegated nodes - deleg = NULL; - } - n = n->next; - if (n == zone->apex) { - // all nodes come before the inserted node, we would get - // into cycle - break; - } - } - - if (cmp == 0) { - log_error("Trying to insert node with owner %s already present " - "in the zone\n", ldns_rdf2str(node->owner)); - return -5; // node exists in the zone - } - - int res = 0; - ldns_rr_list *ns_rrset = NULL; - - // check if the node's owner is not child of delegation point - if (deleg && ldns_dname_is_subdomain(node->owner, deleg->owner)) { - // mark the node as non-authoritative and save glue records - ns_rrset = zn_find_rrset(deleg, LDNS_RR_TYPE_NS); - assert(ns_rrset != NULL); - res = zdb_process_nonauth(node, ns_rrset, NULL, NULL, deleg); - if (res == 0) { - // if everything went well, connect the node before n - zdb_connect_node(n, node); - } - // do not insert the node into the zone data structure - } else { - if ((ns_rrset = zn_find_rrset(node, LDNS_RR_TYPE_NS)) != NULL) { - // delegation point; must connect to the list and then - // adjust the following nodes if needed - zdb_connect_node(n, node); - zn_node_t *d = node; - res = zdb_adjust_delegation_point(zone, &d); - } else { - // not a non-authoritative node or delegation point - // check if it has CNAME RR - if (zdb_adjust_cname(zone, node) == 0) { - // if not, adjust additional data if any needed - zdb_adjust_additional_all(zone, node); - } - zdb_connect_node(n, node); - } - - // insert the node into the zone data structure - if (res == 0 && zds_insert(zone->zone, node) != 0) { - res = -6; - } - } - - return res; -} - -#ifdef ZDB_DEBUG_INSERT_CHECK -static ldns_rdf **inserted_nodes; -#endif -/*----------------------------------------------------------------------------*/ -/*! - * \brief Inserts all nodes from list starting with \a head to the zone data - * structure. - * - * \param zone Zone data structure to insert to. - * \param head In: first item in the list of nodes to be inserted. Out: the same - * if successful, the first non-inserted node if a failure occured. - * - * \retval 0 On success. - * \retval -1 On failure. \a head will point to the first item not inserted. - */ -static int zdb_insert_nodes_into_zds(zdb_zone_t *z, uint *nodes, - zn_node_t **node) -{ - assert((*node) != NULL); - assert((*node)->prev != NULL); - zn_node_t *head = *node; - - // insert zone apex (no checking) - assert(zn_find_rrset(*node, LDNS_RR_TYPE_SOA) != NULL); - if (zds_insert(z->zone, *node) != 0) { - log_error("Error inserting zone apex to the zone data structure" - ".\n"); - return -1; - } - uint i = 1; - uint step = *nodes / 10; - uint next = step; - log_info("Inserted: "); - -#ifdef ZDB_DEBUG_INSERT_CHECK - inserted_nodes = (ldns_rdf **)malloc(*nodes * sizeof(ldns_rdf *)); -#endif - - do { - *node = (*node)->next; - -#ifdef ZDB_DEBUG_INSERT_CHECK - inserted_nodes[i - 1] = (*node)->owner; -#endif - - debug_zdb("Inserting node with key %s...\n", - ldns_rdf2str((*node)->owner)); - - if (zds_insert(z->zone, *node) != 0) { - log_error("Error filling the zone data structure.\n"); - return -1; - } - if (++i == next) { - log_info("%.0f%% ", 100 *((float)next / *nodes)); - next += step; - } - - if (zn_find_rrset(*node, LDNS_RR_TYPE_NS) != NULL) { - // this function will also skip non-authoritative nodes - zdb_set_delegation_point(node); - } - - assert((*node)->next != NULL); - } while ((*node)->next != head); - - log_info("100%% (%d nodes)\n", i); - return 0; -} - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Inserts the zone into the list of zones in \a database in right order. - * - * \param database Zone database to insert the zone into. - * \param zone Zone to be inserted. - * - * The zones are kept in reverse canonical order of their zone names. - */ -static void zdb_insert_zone(zdb_database_t *database, zdb_zone_t *zone) -{ - zdb_zone_t *z = database->head; - zdb_zone_t *prev = NULL; - - while (z != NULL - && ldns_dname_compare(z->zone_name, zone->zone_name) > 0) { - prev = z; - z = z->next; - } - - zone->next = z; - if (prev == NULL) { - database->head = zone; - } else { - prev->next = zone; - } -} - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Adjusts the zone structures for faster lookup. - * - * \param zone Zone to be adjusted. - * - * \retval 0 if successful. - * \retval -1 if an error occured. - * - * \todo Maybe also check for bogus data: - * - other RRSets in node with CNAME RR - * - more CNAMEs in one node - * - other RRSets in delegation point - */ -static int zdb_adjust_zone(zdb_zone_t *zone, uint nodes) -{ - debug_zdb("\nAdjusting zone %s for faster lookup...\n", - ldns_rdf2str(zone->zone_name)); - - zn_node_t *node = zone->apex; - debug_zdb("Adjusting zone apex: %s\n", ldns_rdf2str(node->owner)); - zdb_adjust_additional_apex(zone, node); - - uint i = 1; - uint step = nodes / 10; - uint next = step; - log_info("Adjusted nodes: "); - -#ifdef ZDB_DEBUG_INSERT_CHECK - uint dif = 0; -#endif - - while (node->next != zone->apex) { - - node = node->next; - -#ifdef ZDB_DEBUG_INSERT_CHECK - if (inserted_nodes[i - dif - 1] != node->owner) { - printf("Adjusting node which is not inserted to ZDS: " - "%s\n", ldns_rdf2str(node->owner)); - ++dif; - } -#endif - - if (++i == next) { - log_info("%.0f%% ", 100 *((float)next / nodes)); - next += step; - } - - debug_zdb("Adjusting node %s\n", ldns_rdf2str(node->owner)); - if (zdb_adjust_cname(zone, node) != 0) { - // no other records when CNAME - continue; - } - if (zdb_adjust_delegation_point(zone, &node) != 0) { - // no other records when delegation point - continue; - } - zdb_adjust_additional_all(zone, node); - } - - log_debug("100%% (%d nodes)\n", i); - return 0; -} - -/*----------------------------------------------------------------------------*/ - -static void zdb_destroy_zone(zdb_zone_t **zone) -{ - // free the zone data structure but do not delete the zone nodes in it - zds_destroy(&(*zone)->zone, NULL); - // free the zone name - ldns_rdf_deep_free((*zone)->zone_name); - - // free all zone nodes from the list - zn_node_t *node = (*zone)->apex; - // disconnect the last item - node->prev->next = NULL; - while (node != NULL) { - zn_node_t *n = node; - node = node->next; - // do not bother with adjusting the pointers - zn_destroy(&n); - } - - free((*zone)); - *zone = NULL; -} - -#ifdef ZDB_DEBUG -/*----------------------------------------------------------------------------*/ - -static void zdb_print_list(const zdb_zone *zone) -{ - int count = 0; - debug_zdb("Zone listing in canonical order. Zone '%s'\n\n", - ldns_rdf2str(zone->zone_name)); - zn_node *node = zone->apex; - do { - debug_zdb("%s\n", ldns_rdf2str(node->owner)); - node = node->next; - ++count; - } while (node != zone->apex); - debug_zdb("Nodes: %d\n\n", count); -} -#endif - -/*----------------------------------------------------------------------------*/ -/* Public functions */ -/*----------------------------------------------------------------------------*/ - -zdb_database_t *zdb_create() -{ - zdb_database_t *db = malloc(sizeof(zdb_database_t)); - - if (db == NULL) { - ERR_ALLOC_FAILED; - return NULL; - } - - db->head = NULL; - return db; -} - -/*----------------------------------------------------------------------------*/ - -int zdb_add_zone(zdb_database_t *database, ldns_zone *zone) -{ - zdb_zone_t *new_zone = malloc(sizeof(zdb_zone_t)); - - if (new_zone == NULL) { - ERR_ALLOC_FAILED; - return -1; - } - - // get the zone name - assert(ldns_zone_soa(zone) != NULL); - new_zone->zone_name = - ldns_rdf_clone(ldns_rr_owner(ldns_zone_soa(zone))); - log_info("Adding zone %s to Zone database...\n", - ldns_rdf2str(new_zone->zone_name)); - log_info("Creating zone list...\n"); - // create a linked list of zone nodes and get their count - uint nodes = zdb_create_list(new_zone, zone); - // get rid of the zone structure (no longer needed) - ldns_zone_deep_free(zone); - - log_info("Creating empty non-terminal zone nodes...\n"); - // create empty non-terminals - int nonterm = zdb_add_empty_nonterminals(new_zone); - if (nonterm < -1) { - zdb_delete_list_items(new_zone); - ldns_rdf_deep_free(new_zone->zone_name); - return -2; - } - - nodes += nonterm; - -#ifdef ZDB_DEBUG - zdb_print_list(new_zone); -#endif - - log_info("Creating Zone data structure (%d nodes)...\n", nodes); - // create the zone data structure - new_zone->zone = zds_create(nodes); - if (new_zone->zone == NULL) { - // destroy the list and all its contents - zdb_delete_list_items(new_zone); - ldns_rdf_deep_free(new_zone->zone_name); - return -3; - } - - // add created nodes to the zone data structure for lookup - log_info("Inserting zone nodes to the Zone data structure...\n"); - zn_node_t *node = new_zone->apex; - if (zdb_insert_nodes_into_zds(new_zone, &nodes, &node) != 0) { - // destroy the rest of the nodes in the list - // (from node to zone apex) - while (node != new_zone->apex) { - zn_node_t *prev = node; - node = node->next; - assert(node != NULL); - zn_destroy(&prev); - } - // and destroy the partially filled zone data structure - zds_destroy(&new_zone->zone, NULL); - return -4; - } - - log_info("Adjusting zone (%d nodes)...\n", nodes); - zdb_adjust_zone(new_zone, nodes); - - log_info("Inserting the zone to the Zone database...\n"); - // Insert into the database on the proper place, i.e. in reverse - // canonical order of zone names. - zdb_insert_zone(database, new_zone); - - log_info("Done.\n"); - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -int zdb_create_zone(zdb_database_t *database, ldns_rdf *zone_name, uint items) -{ - // add some lock to avoid multiple zone creations? - // add some check if the zone is not already in db? - - zdb_zone_t *zone = malloc(sizeof(zdb_zone_t)); - - if (zone == NULL) { - ERR_ALLOC_FAILED; - return -1; - } - - zone->apex = NULL; - zone->zone_name = ldns_rdf_clone(zone_name); - - if (zone->zone_name == NULL) { - ERR_ALLOC_FAILED; - free(zone); - return -1; - } - - zone->zone = zds_create(items); - - if (zone->zone == NULL) { - log_error("Could not create zone data structure for zone %s\n", - ldns_rdf_data(zone_name)); - ldns_rdf_deep_free(zone->zone_name); - free(zone); - return -1; - } - - // insert it into the right place in the database - zdb_insert_zone(database, zone); - - return 0; -} -/*----------------------------------------------------------------------------*/ - -int zdb_remove_zone(zdb_database_t *database, ldns_rdf *zone_name) -{ - // add some lock to avoid multiple removals - - zdb_zone_t *z = NULL, *zp = NULL; - zdb_find_zone(database, zone_name, &z, &zp); - - if (z == NULL) { - debug_zdb("Zone not found!\n"); - return -1; - } - - zdb_disconnect_zone(database, z, zp); - - // wait for all readers to finish - synchronize_rcu(); - - zds_destroy(&z->zone, NULL); - assert(z->zone == NULL); - ldns_rdf_deep_free(z->zone_name); - free(z); - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -int zdb_insert_name(zdb_database_t *database, ldns_rdf *zone_name, - zn_node_t *node) -{ - zdb_zone_t *z = NULL, *zp = NULL; - - // start of RCU reader critical section (the zone should not be removed) - rcu_read_lock(); - zdb_find_zone(database, zone_name, &z, &zp); - - if (z == NULL) { - debug_zdb("Zone not found!\n"); - return -2; - } - debug_zdb("Found zone: %.*s\n", ldns_rdf_size(z->zone_name), - ldns_rdf_data(z->zone_name)); - - int res = zdb_insert_node_to_zone(z, node); - - // end of RCU reader critical section - rcu_read_unlock(); - return res; -} - -/*----------------------------------------------------------------------------*/ - -const zdb_zone_t *zdb_find_zone_for_name(zdb_database_t *database, - const ldns_rdf *dname) -{ - zdb_zone_t *z = database->head; - - // start of RCU reader critical section - rcu_read_lock(); - - while (z != NULL && ldns_dname_compare(z->zone_name, dname) > 0) { - z = z->next; - } - // now z's zone name is either equal to dname or there is no other zone - // to search - if (z != NULL - && ldns_dname_compare(z->zone_name, dname) != 0 - && !ldns_dname_is_subdomain(dname, z->zone_name)) { - z = NULL; - } - // end of RCU reader critical section - rcu_read_unlock(); - - return z; -} - -/*----------------------------------------------------------------------------*/ - -const zn_node_t *zdb_find_name_in_zone(const zdb_zone_t *zone, - const ldns_rdf *dname) -{ - return zdb_find_name_in_zone_nc(zone, dname); -} - -/*----------------------------------------------------------------------------*/ - -void zdb_destroy(zdb_database_t **database) -{ - // add some lock to avoid multiple destroys - - zdb_zone_t *z; - - while ((*database)->head != NULL) { - z = (*database)->head; - // disconnect the first zone - (*database)->head = z->next; - // wait for all readers to finish - synchronize_rcu(); - // destroy zone - zdb_destroy_zone(&z); - } - - free(*database); - *database = NULL; -} diff --git a/deprecated/zone-database.h b/deprecated/zone-database.h deleted file mode 100644 index a649027453ca45bbfdd689c28be077aa7723eb97..0000000000000000000000000000000000000000 --- a/deprecated/zone-database.h +++ /dev/null @@ -1,172 +0,0 @@ -/*! - * \file zone-database.h - * - * \author Lubos Slovak <lubos.slovak@nic.cz> - * - * \brief Contains structures for keeping all zones the server manages and some - * basic routines for using them. - * - * As for now, the database uses only simple one-way linked list of zones. For - * individual zones, an underlying data structure with generic API is used (the - * API is provided in the zone-data-structure.h header). - * - * \note Some kind of tree will be probably best for the zone database, - * though crippling the performance in case of a lot of zones. - * We need the tree structure in order to find the appropriate zone where - * to search. - * \todo Consider using one large hash table for all zones for searching and - * the zone structure only for some additional issues. If we can avoid - * using the zone structure during each query, it may be worth it. - * Moreover it may save some space - less empty items in one large hash - * table than in several smaller. - * - * \addtogroup zonedb - * @{ - */ -#ifndef _CUTEDNS_ZONE_DATABASE_H_ -#define _CUTEDNS_ZONE_DATABASE_H_ - -#include <sys/types.h> - -#include <ldns/rdata.h> -#include <ldns/zone.h> - -#include "common.h" -#include "zone-data-structure.h" - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Structure for storing one zone. Uses zds_zone structure for data. - */ -struct zdb_zone { - /*! \brief Zone name in wire format (i.e. a null-terminated string). */ - ldns_rdf *zone_name; - - zds_zone_t *zone; /*!< Zone data structure. */ - - /*! \brief Zone apex. First item in a linked list of zone nodes. */ - zn_node_t *apex; - - struct zdb_zone *next; /*!< Next item pointer. */ -}; - -typedef struct zdb_zone zdb_zone_t; - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Zone database structure. - */ -struct zdb_database { - /*! \brief Pointer to the first item in the linked list of zones. */ - zdb_zone_t *head; -}; - -typedef struct zdb_database zdb_database_t; - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Allocates and initializes the zone database structure. - * \return Pointer to the created zone database structure. - */ -zdb_database_t *zdb_create(); - -/*! - * \brief Adds new empty zone to the given database. - * - * \param database Zone database to store the zone. - * \param zone_name Zone name in wire format (i.e. a null-terminated string). - * \param items Number of items in the zone. Is used for creating the zone data - * structure of appropriate size. - * - * \retval 0 On success. - * \retval -1 On failure. - */ -int zdb_create_zone(zdb_database_t *database, ldns_rdf *zone_name, uint items); - -/*! - * \brief Adds new zone stored in a ldns_zone structure to the database. - * - * \param database Zone database to store the zone. - * \param zone Parsed zone in ldns_zone format. - * - * \retval 0 On success. - * \retval -1 On failure. - */ -int zdb_add_zone(zdb_database_t *database, ldns_zone *zone); - -/*! - * \brief Removes the given zone from the database if it exists. - * - * \param database Zone database to remove from. - * \param zone_name Name of the zone to be removed. - * - * The removal of a zone is synchronized using RCU mechanism, so the zone data - * will not be destroyed while some thread may be using it. - * - * \retval 0 On success. - * \retval -1 If the zone was not found. - */ -int zdb_remove_zone(zdb_database_t *database, ldns_rdf *zone_name); - -/*! - * \brief Inserts one zone node to the given zone in the database. - * - * \param database Zone database to insert the node into. - * \param zone_name Name of the zone to insert the node into. - * \param node The zone node to be inserted. - * - * \retval 0 On success. - * \retval -2 If the zone was not found. - * \retval -1 If an error occured during insertion to the zone. - */ -int zdb_insert_name(zdb_database_t *database, ldns_rdf *zone_name, - zn_node_t *node); - -/*! - * \brief Finds zone the given domain name should belong to. - * - * \param database Zone database to search in. - * \param dname Domain name to find zone for. - * - * \retval Zone in which the domain name should be present. - * - * \note As the zones are ordered in reverse canonical order, a possible parent - * of the returned zone may be retrieved easily as it is the next item - * in the linked list (zdb_zone.next). - */ -const zdb_zone_t *zdb_find_zone_for_name(zdb_database_t *database, - const ldns_rdf *dname); - -/*! - * \brief Finds the given name in the zone database and returns corresponding - * zone node. - * - * \param database Zone database to search in. - * \param dname Domain name to find, in wire format (i.e. a null-terminated - * string). - * - * \return Proper zone node for the given name or NULL if not found. - */ -const zn_node_t *zdb_find_name_in_zone(const zdb_zone_t *zone, - const ldns_rdf *dname); - -/*! - * \brief Destroys and deallocates the whole zone database. - * - * \param database Pointer to pointer to the zone database to be destroyed. - * - * The zones are destroyed one-by-one and the process is synchronized using - * RCU mechanism, so the zone data will not be destroyed while some thread may - * be using it. - * - * \todo Destroy nodes which are not hashed into the table. Best will be to - * destroy zone nodes from the list and tell zds_destroy() not to destroy - * the stored items. - */ -void zdb_destroy(zdb_database_t **database); - -/*----------------------------------------------------------------------------*/ - -#endif /* _CUTEDNS_ZONE_DATABASE_H_ */ - -/*! @} */ diff --git a/deprecated/zone-node.c b/deprecated/zone-node.c deleted file mode 100644 index d22aeff18fe6ce0e27100747d1a0f10516673de6..0000000000000000000000000000000000000000 --- a/deprecated/zone-node.c +++ /dev/null @@ -1,803 +0,0 @@ -#include <stdlib.h> -#include <assert.h> -#include <stdio.h> - -#include <ldns/ldns.h> - -#include "zone-node.h" -#include "common.h" -#include "skip-list.h" - -/*----------------------------------------------------------------------------*/ - -#define RFRS(arr) ((const zn_node_t **)da_get_items(arr)) -#define RFRS_COUNT(arr) (da_get_count(arr)) - -/*----------------------------------------------------------------------------*/ - -static const uint RRSETS_COUNT = 10; - -/*! \brief Zone node flags. */ -enum zn_flags { - /*! \brief Xxxxxxxx1 - node is delegation point (ref.glues is set) */ - FLAGS_DELEG = 0x1, - - /*! - * \brief Xxxxxxx1x - node is non-authoritative (carrying only glue - * records) - */ - FLAGS_NONAUTH = 0x2, - - /*! \brief Xxxxxx1xx - node carries a CNAME record (ref.cname is set) */ - FLAGS_HAS_CNAME = 0x4, - - /*! - * \brief Xxxxx1xxx - node carries an MX record (ref.additional is set) - */ - FLAGS_HAS_MX = 0x8, - /*! - * \brief Xxxx1xxxx - node carries an NS record (ref.additional is set) - */ - FLAGS_HAS_NS = 0x10, - /*! - * \brief Xxx1xxxxx - node carries a SRV record (ref.additional is set) - */ - FLAGS_HAS_SRV = 0x20, - /*! - * \brief Xx1xxxxxx - node is referenced by some CNAME record (referrer - * is set) - */ - FLAGS_REF_CNAME = 0x40, - /*! - * \brief X1xxxxxxx - node is referenced by some MX record (referrer is - * set) - */ - FLAGS_REF_MX = 0x80, - - /*! - * \brief xxxxxxx1X - node is referenced by some NS record (referrer is - * set) - */ - FLAGS_REF_NS = 0x100, - - /*! - * \brief xxxxxx1xX - node is referenced by some SRV record (referrer - * is set) - */ - FLAGS_REF_SRV = 0x200 -}; - -typedef enum zn_flags zn_flags_t; - -/*----------------------------------------------------------------------------*/ -/* Private functions */ -/*----------------------------------------------------------------------------*/ - -static zn_ar_rrsets_t *zn_create_ar_rrsets() -{ - zn_ar_rrsets_t *ar = malloc(sizeof(zn_ar_rrsets_t)); - if (ar == NULL) { - ERR_ALLOC_FAILED; - return NULL; - } - ar->a = NULL; - ar->aaaa = NULL; - ar->cname = NULL; - - return ar; -} - -/*----------------------------------------------------------------------------*/ - -static void zn_destroy_ar_rrsets(zn_ar_rrsets_t **ar) -{ - free(*ar); -} - -/*----------------------------------------------------------------------------*/ - -static void zn_dtor_ar_rrsets(void *value) -{ - zn_ar_rrsets_t *ar = (zn_ar_rrsets_t *)value; - zn_destroy_ar_rrsets(&ar); -} - -/*----------------------------------------------------------------------------*/ - -static zn_ar_rrsets_t *zn_create_ar_rrsets_for_ref(ldns_rr_list *ref_rrset) -{ - zn_ar_rrsets_t *ar = zn_create_ar_rrsets(); - - switch (ldns_rr_list_type(ref_rrset)) { - case LDNS_RR_TYPE_A: - ar->a = ref_rrset; - ar->aaaa = NULL; - break; - case LDNS_RR_TYPE_AAAA: - ar->aaaa = ref_rrset; - ar->a = NULL; - break; - default: - free(ar); - log_error("Error: trying to add MX record reference to a type " - "other than A or AAAA.\n"); - return NULL; - } - return ar; -} - -/*----------------------------------------------------------------------------*/ - -static zn_ar_rrsets_t *zn_create_ar_rrsets_for_cname(const zn_node_t *node) -{ - zn_ar_rrsets_t *ar = zn_create_ar_rrsets(); - - assert(ar->a == NULL); - assert(ar->aaaa == NULL); - ar->cname = node; - - return ar; -} - -/*----------------------------------------------------------------------------*/ - -static int zn_compare_ar_keys(void *key1, void *key2) -{ - return ldns_dname_compare((ldns_rdf *)key1, (ldns_rdf *)key2); -} - -/*----------------------------------------------------------------------------*/ - -static int zn_merge_ar_values(void **value1, void **value2) -{ - zn_ar_rrsets_t *ar1 = (zn_ar_rrsets_t *)(*value1); - zn_ar_rrsets_t *ar2 = (zn_ar_rrsets_t *)(*value2); - - if ((ar2->a != NULL && ar1->a != NULL) - || (ar2->aaaa != NULL && ar1->aaaa != NULL) - || (ar2->cname != NULL && ar1->cname != NULL)) { - return -1; - } - - if (ar2->a != NULL) { - ar1->a = ar2->a; - } else if (ar2->aaaa != NULL) { - ar1->aaaa = ar2->aaaa; - } else if (ar2->cname != NULL) { - ar1->cname = ar2->cname; - } - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -static int zn_compare_keys(void *key1, void *key2) -{ - // in our case, key is of type ldns_rr_type, but as casting to enum may - // result in undefined behaviour, we use regular unsigned int. - return (key1 < key2) ? -1 : ((key1 > key2) ? 1 : 0); -} - -/*----------------------------------------------------------------------------*/ - -static int zn_merge_values(void **value1, void **value2) -{ - if (ldns_rr_list_cat((ldns_rr_list *)(*value1), - (ldns_rr_list *)(*value2))) { - return 0; - } else { - return -1; - } -} - -/*----------------------------------------------------------------------------*/ - -static void zn_destroy_value(void *value) -{ - ldns_rr_list_deep_free((ldns_rr_list *)value); -} - -/*----------------------------------------------------------------------------*/ - -static inline void zn_flags_set(uint16_t *flags, zn_flags_t flag) -{ - (*flags) |= flag; -} - -/*----------------------------------------------------------------------------*/ - -static inline int zn_flags_get(uint16_t flags, zn_flags_t flag) -{ - return (flags & flag); -} - -/*----------------------------------------------------------------------------*/ - -static inline int zn_flags_empty(uint16_t flags) -{ - return (flags == 0); -} - -/*----------------------------------------------------------------------------*/ - -static int zn_add_referrer_node(zn_node_t *node, const zn_node_t *referrer) -{ - if (node->referrers == NULL) { - node->referrers = da_create(1, sizeof(zn_node_t *)); - if (node->referrers == NULL) { - log_error("%s(): Error while creating array.\n", - __func__); - return -1; - } - } - - int res = da_reserve(node->referrers, 1); - if (res < 0) { - log_error("%s(): Error while reserving space.\n", - __func__); - return -2; - } - - RFRS(node->referrers)[RFRS_COUNT(node->referrers)] = referrer; - res = da_occupy(node->referrers, 1); - if (res != 0) { - log_error("%s(): Error while occupying space.\n", - __func__); - return -3; - } - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -static int zn_has_additional(const zn_node_t *node) -{ - return (zn_has_mx(node) + zn_has_ns(node) + zn_has_srv(node)); -} - -/*----------------------------------------------------------------------------*/ -/* Public functions */ -/*----------------------------------------------------------------------------*/ - -zn_node_t *zn_create() -{ - zn_node_t *node = malloc(sizeof(zn_node_t)); - if (node == NULL) { - ERR_ALLOC_FAILED; - return NULL; - } - - node->rrsets = skip_create_list(zn_compare_keys); - if (node->rrsets == NULL) { - free(node); - return NULL; - } - - node->next = NULL; - node->prev = NULL; - node->owner = NULL; - // not a CNAME - node->ref.cname = NULL; - // not a delegation point - node->flags = 0; - // referenced by no node (do not initialize the array to save space) - node->referrers = NULL; - - return node; -} - -/*----------------------------------------------------------------------------*/ - -ldns_rdf *zn_owner(zn_node_t *node) -{ - return node->owner; -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_rr(zn_node_t *node, ldns_rr *rr) -{ - /* - * This whole function can be written with less code if we first create - * new rr_list, insert the RR into it and then call skip_insert and - * provide the merging function. - * - * However, in that case the allocation will occur always, what may be - * time-consuming, so we retain this version for now. - */ - if (rr == NULL) { - return -7; - } - - // accept only RR with the same owner - if (node->owner - && ldns_dname_compare(node->owner, ldns_rr_owner(rr)) != 0) { - return -6; - } - - // find an appropriate RRSet for the RR - ldns_rr_list *rrset = (ldns_rr_list *)skip_find( - node->rrsets, (void *)ldns_rr_get_type(rr)); - - // found proper RRSet, insert into it - if (rrset != NULL) { - assert(ldns_rr_list_type(rrset) == ldns_rr_get_type(rr)); - if (ldns_rr_list_push_rr(rrset, rr) != true) { - return -3; - } - } else { - rrset = ldns_rr_list_new(); - if (rrset == NULL) { - ERR_ALLOC_FAILED; - return -4; - } - // add the RR to the RRSet - if (ldns_rr_list_push_rr(rrset, rr) != true) { - return -5; - } - // insert the rrset into the node - int res = skip_insert(node->rrsets, - (void *)ldns_rr_get_type(rr), - (void *)rrset, NULL); - assert(res != 2 && res != -2); - // if no owner yet and successfuly inserted - if (node->owner == NULL && res == 0) { - // set the owner - node->owner = ldns_rdf_clone(ldns_rr_owner(rr)); - } - return res; - } - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_rrset(zn_node_t *node, ldns_rr_list *rrset) -{ - assert(ldns_is_rrset(rrset)); - // here we do not have to allocate anything even if the RRSet is not in - // the list, so can use the shortcut (see comment in zn_add_rr()). - int res = skip_insert(node->rrsets, (void *)ldns_rr_list_type(rrset), - (void *)rrset, zn_merge_values); - - // if the node did not have any owner and insert successful, set owner - if (node->owner == NULL && res == 0) { - node->owner = ldns_rdf_clone(ldns_rr_list_owner(rrset)); - } - - return res; -} - -/*----------------------------------------------------------------------------*/ - -ldns_rr_list *zn_find_rrset(const zn_node_t *node, ldns_rr_type type) -{ - ldns_rr_list *rrset = (ldns_rr_list *)skip_find(node->rrsets, - (void *)type); - debug_zn("Searching for type %d,%s in RRSets:\n", type, - ldns_rr_type2str(type)); - skip_print_list(node->rrsets, zn_print_rrset); - - assert(rrset == NULL || ldns_is_rrset(rrset)); - if (rrset != NULL) { - debug_zn("Type demanded: %d,%s, type found: %d,%s\n", type, - ldns_rr_type2str(type), ldns_rr_list_type(rrset), - ldns_rr_type2str(ldns_rr_list_type(rrset))); - } - assert(rrset == NULL || ldns_rr_list_type(rrset) == type); - - return rrset; -} - -/*----------------------------------------------------------------------------*/ - -ldns_rr_list *zn_all_rrsets(const zn_node_t *node) -{ - ldns_rr_list *all = ldns_rr_list_new(); - - debug_zn("Extracting all RRSets from:\n"); - skip_print_list(node->rrsets, zn_print_rrset); - - const skip_node_t *sn = skip_first(node->rrsets); - while (sn != NULL) { - ldns_rr_list_push_rr_list(all, (ldns_rr_list *)sn->value); - sn = skip_next(sn); - } - - debug_zn("\nExtracted RRSets:\n%s\n", ldns_rr_list2str(all)); - return all; -} - -/*----------------------------------------------------------------------------*/ - -int zn_is_empty(const zn_node_t *node) -{ - return (skip_is_empty(node->rrsets)); -} - -/*----------------------------------------------------------------------------*/ - -void zn_set_non_authoritative(zn_node_t *node) -{ - zn_flags_set(&node->flags, FLAGS_NONAUTH); -} - -/*----------------------------------------------------------------------------*/ - -int zn_is_non_authoritative(const zn_node_t *node) -{ - return zn_flags_get(node->flags, FLAGS_NONAUTH); -} - -/*----------------------------------------------------------------------------*/ - -void zn_set_delegation_point(zn_node_t *node) -{ - assert(node->ref.glues == NULL); - node->ref.glues = ldns_rr_list_new(); - zn_flags_set(&node->flags, FLAGS_DELEG); -} - -/*----------------------------------------------------------------------------*/ - -int zn_is_delegation_point(const zn_node_t *node) -{ - assert((zn_flags_get(node->flags, FLAGS_DELEG) == 0 - || node->ref.glues != NULL)); - return zn_flags_get(node->flags, FLAGS_DELEG); -} - -/*----------------------------------------------------------------------------*/ - -void zn_set_ref_cname(zn_node_t *node, zn_node_t *cname_ref) -{ - assert(node->ref.cname == NULL); - node->ref.cname = cname_ref; - zn_flags_set(&node->flags, FLAGS_HAS_CNAME); -} - -/*----------------------------------------------------------------------------*/ - -int zn_has_cname(const zn_node_t *node) -{ - return zn_flags_get(node->flags, FLAGS_HAS_CNAME); -} - -/*----------------------------------------------------------------------------*/ - -zn_node_t *zn_get_ref_cname(const zn_node_t *node) -{ - if (zn_flags_get(node->flags, FLAGS_HAS_CNAME) > 0) { - return node->ref.cname; - } else { - return NULL; - } -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_ref(zn_node_t *node, ldns_rdf *name, ldns_rr_type type, - ldns_rr_list *ref_rrset, const zn_node_t *ref_node) -{ - zn_flags_t flag = 0; - - assert(ref_rrset != NULL || ref_node != NULL); - assert(ref_node != NULL || ref_rrset != NULL); - - switch (type) { - case LDNS_RR_TYPE_MX: - flag = FLAGS_HAS_MX; - break; - case LDNS_RR_TYPE_NS: - flag = FLAGS_HAS_NS; - break; - case LDNS_RR_TYPE_SRV: - flag = FLAGS_HAS_SRV; - break; - default: - log_error("zn_add_ref(): type %s not supported.\n", - ldns_rr_type2str(type)); - return -1; - } - - if (node->ref.additional == NULL) { - node->ref.additional = skip_create_list(zn_compare_ar_keys); - if (node->ref.additional == NULL) { - return -3; - } - } - - zn_ar_rrsets_t *ar; - if (ref_rrset != NULL) { - ar = zn_create_ar_rrsets_for_ref(ref_rrset); - } else { - assert(ref_node != NULL); - ar = zn_create_ar_rrsets_for_cname(ref_node); - } - if (ar == NULL) { - skip_destroy_list(&(node->ref.additional), NULL, - zn_dtor_ar_rrsets); - return -4; - } - - int res = 0; - res = skip_insert(node->ref.additional, name, ar, zn_merge_ar_values); - if (res != 0) { - debug_zn("Result other than 0, deleting ar rrset on %p\n", ar); - zn_destroy_ar_rrsets(&ar); - } - zn_flags_set(&node->flags, flag); - - debug_zn("zn_add_ref(%p, %p, %s)\n", node, ref_rrset, - ldns_rr_type2str(type)); - debug_zn("First item in the skip list: key: %s, value: %p\n", - ldns_rdf2str((ldns_rdf *)skip_first(node->ref.additional)->key), - skip_first(node->ref.additional)->value); - debug_zn("Inserted item: value: %p\n", ar); - - if (res < 0) { - return -5; - } - - return 0; -} - -/*----------------------------------------------------------------------------*/ - -skip_list_t *zn_get_refs(const zn_node_t *node) -{ - if ((zn_flags_get(node->flags, FLAGS_HAS_MX) - | zn_flags_get(node->flags, FLAGS_HAS_NS) - | zn_flags_get(node->flags, FLAGS_HAS_SRV)) > 0) { - return node->ref.additional; - } else { - return NULL; - } -} - -/*----------------------------------------------------------------------------*/ - -const zn_ar_rrsets_t *zn_get_ref(const zn_node_t *node, const ldns_rdf *name) -{ - if ((zn_flags_get(node->flags, FLAGS_HAS_MX) - | zn_flags_get(node->flags, FLAGS_HAS_NS) - | zn_flags_get(node->flags, FLAGS_HAS_SRV)) > 0) { - return (zn_ar_rrsets_t *)skip_find(node->ref.additional, - (void *)name); - } else { - return NULL; - } -} - -/*----------------------------------------------------------------------------*/ - -int zn_has_mx(const zn_node_t *node) -{ - return zn_flags_get(node->flags, FLAGS_HAS_MX); -} - -/*----------------------------------------------------------------------------*/ - -int zn_has_ns(const zn_node_t *node) -{ - return zn_flags_get(node->flags, FLAGS_HAS_NS); -} - -/*----------------------------------------------------------------------------*/ - -int zn_has_srv(const zn_node_t *node) -{ - return zn_flags_get(node->flags, FLAGS_HAS_SRV); -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_referrer_cname(zn_node_t *node, const zn_node_t *referrer) -{ - int res = zn_add_referrer_node(node, referrer); - if (res == 0) { - zn_flags_set(&node->flags, FLAGS_REF_CNAME); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_referrer_mx(zn_node_t *node, const zn_node_t *referrer) -{ - int res = zn_add_referrer_node(node, referrer); - if (res == 0) { - zn_flags_set(&node->flags, FLAGS_REF_MX); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_referrer_ns(zn_node_t *node, const zn_node_t *referrer) -{ - int res = zn_add_referrer_node(node, referrer); - if (res == 0) { - zn_flags_set(&node->flags, FLAGS_REF_NS); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_referrer_srv(zn_node_t *node, const zn_node_t *referrer) -{ - int res = zn_add_referrer_node(node, referrer); - if (res == 0) { - zn_flags_set(&node->flags, FLAGS_REF_SRV); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -int zn_add_referrer(zn_node_t *node, const zn_node_t *referrer, - ldns_rr_type type) -{ - int res = zn_add_referrer_node(node, referrer); - if (res == 0) { - uint16_t flag = 0; - switch (type) { - case LDNS_RR_TYPE_NS: - flag = FLAGS_REF_NS; - break; - case LDNS_RR_TYPE_MX: - flag = FLAGS_REF_MX; - break; - case LDNS_RR_TYPE_CNAME: - flag = FLAGS_REF_CNAME; - break; - case LDNS_RR_TYPE_SRV: - flag = FLAGS_REF_SRV; - break; - default: - debug_zn("zn_add_referrer(): type %s not supported.\n", - ldns_rr_type2str(type)); - return -2; - break; - } - - zn_flags_set(&node->flags, flag); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -int zn_referrers_count(const zn_node_t *node) -{ - int count = RFRS_COUNT(node->referrers); - assert(count == 0 || (zn_flags_get(node->flags, FLAGS_REF_CNAME) - | zn_flags_get(node->flags, FLAGS_REF_MX) - | zn_flags_get(node->flags, FLAGS_REF_NS) - | zn_flags_get(node->flags, FLAGS_REF_SRV)) > 0); - return count; -} - -/*----------------------------------------------------------------------------*/ - -int zn_push_glue(zn_node_t *node, ldns_rr_list *glue) -{ - assert((zn_flags_get(node->flags, FLAGS_DELEG) == 1 - && node->ref.glues != NULL)); - - if (glue == NULL) { - return 0; - } - - int res = ldns_rr_list_push_rr_list(node->ref.glues, glue) - 1; - if (res == 0) { - // sort the glue RRs - ldns_rr_list_sort(node->ref.glues); - } - return res; -} - -/*----------------------------------------------------------------------------*/ - -ldns_rr_list *zn_get_glues(const zn_node_t *node) -{ - if (!zn_is_delegation_point(node)) { - return NULL; - } - return node->ref.glues; -} - -/*----------------------------------------------------------------------------*/ - -ldns_rr_list *zn_get_glue(const zn_node_t *node, ldns_rdf *owner, - ldns_rr_type type, ldns_rr_list *copied_rrs) -{ - assert(type == LDNS_RR_TYPE_A || type == LDNS_RR_TYPE_AAAA); - - if (!zn_is_delegation_point(node)) { - return NULL; - } - - assert(copied_rrs != NULL); - ldns_rr_list *glue = ldns_rr_list_new(); - - ldns_rr *rr; - int i = -1; - int cmp; - do { - ++i; - rr = ldns_rr_list_rr(node->ref.glues, i); - } while ((cmp = ldns_dname_match_wildcard(owner, ldns_rr_owner(rr))) - < 0); - - // found owner - while (cmp == 0 && ldns_rr_get_type(rr) != type) { - ++i; - rr = ldns_rr_list_rr(node->ref.glues, i); - cmp = ldns_dname_match_wildcard(owner, ldns_rr_owner(rr)); - } - - // found owner & type - while (cmp == 0 && ldns_rr_get_type(rr) == type) { - // if the RR has a wildcard owner, copy the RR and replace the - // owner with the desired name - if (ldns_dname_is_wildcard(ldns_rr_owner(rr))) { - ldns_rr *rr_new = ldns_rr_clone(rr); - ldns_rdf_deep_free(ldns_rr_owner(rr_new)); - ldns_rr_set_owner(rr_new, ldns_rdf_clone(owner)); - ldns_rr_list_push_rr(glue, rr_new); - ldns_rr_list_push_rr(copied_rrs, rr_new); - } else { - ldns_rr_list_push_rr(glue, rr); - } - ++i; - rr = ldns_rr_list_rr(node->ref.glues, i); - cmp = ldns_dname_compare(ldns_rr_owner(rr), owner); - } - - return glue; -} - -/*----------------------------------------------------------------------------*/ - -void zn_destroy(zn_node_t **node) -{ - skip_destroy_list(&(*node)->rrsets, NULL, zn_destroy_value); - if (zn_has_additional(*node)) { - skip_destroy_list(&(*node)->ref.additional, NULL, - zn_dtor_ar_rrsets); - } - - ldns_rdf_deep_free((*node)->owner); - if (zn_is_delegation_point(*node)) { - ldns_rr_list_free((*node)->ref.glues); - } - if ((*node)->referrers != NULL) { - da_destroy((*node)->referrers); - free((*node)->referrers); - } - free(*node); - *node = NULL; -} - -/*----------------------------------------------------------------------------*/ - -void zn_destructor(void *item) -{ - zn_node_t *node = (zn_node_t *)item; - zn_destroy(&node); -} - -/*----------------------------------------------------------------------------*/ - -void zn_print_rrset(void *key, void *value) -{ - debug_zn("Type: %d,%s, RRSet: %s\n", - (ldns_rr_type)key, - ldns_rr_type2str((ldns_rr_type)key), - ldns_rr_list2str((ldns_rr_list *)value)); -} diff --git a/deprecated/zone-node.h b/deprecated/zone-node.h deleted file mode 100644 index c926e3cdd3ad5140295dd60aa78a3ca3f0a49889..0000000000000000000000000000000000000000 --- a/deprecated/zone-node.h +++ /dev/null @@ -1,284 +0,0 @@ -/*! - * \file zone-node.h - * - * \author Lubos Slovak <lubos.slovak@nic.cz> - * - * \brief Contains data structure for holding DNS data related to one domain - * name (node of the notional zone tree) and routines to manipulate - * the data. - * - * \todo CNAME chain should be resolved in advance not only by saving pointer - * to the next node in chain, but rather by having all the CNAME RRs - * somewhere in one place and saving only a pointer or index to this place - * - * \addtogroup zonedb - * @{ - */ -#ifndef _CUTEDNS_ZONE_NODE_H_ -#define _CUTEDNS_ZONE_NODE_H_ - -#include <sys/types.h> - -#include <ldns/rr.h> - -#include "common.h" -#include "skip-list.h" -#include "dynamic-array.h" - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Data structure for holding DNS data related to one zone node. - */ -struct zn_node { - skip_list_t *rrsets; /*!< Skip list of RRSets. */ - - ldns_rdf *owner; /*!< Owner domain name of the node. */ - - /*! - * \brief Provide some extra information about the node. - * - * Currently used flags: - * - Xxxxxxxx1 - node is delegation point (ref.glues is set) - * - Xxxxxxx1x - node is non-authoritative (e.g. carrying only glue - * records) - * - Xxxxxx1xx - node carries a CNAME record (ref.cname is set) - * - Xxxxx1xxx - node carries an MX record (ref.additional is set) - * - Xxxx1xxxx - node carries an NS record (ref.additional is set) - * - Xxx1xxxxx - node carries a SRV record (ref.additional is set) - * - Xx1xxxxxx - node is referenced by some CNAME record (referrer is - * set) - * - X1xxxxxxx - node is referenced by some MX record (referrer is set) - * - xxxxxxx1X - node is referenced by some NS record (referrer is set) - * - xxxxxx1xX - node is referenced by some SRV record (referrer is set) - * - * These flags are however set and used only after a zone to which the - * node belongs has been adjusted (see zdb_adjust_zone()). - */ - uint16_t flags; - - union { - /*! \brief Node with canonical name for this node's name. */ - struct zn_node *cname; - - /*! - * \brief Glue RRSets in canonical order. - * - * These are only references to actual ldns_rr objects stored - * elsewhere. - */ - ldns_rr_list *glues; - - /*! - * \brief List of RRSets with A and/or AAAA records for an MX - * or NS RRSet in canonical order. - * - * Records are saved to the skip list with the owner name - * (ldns_rdf *) as key and the zn_ar_rrsets structure as value. - * These are only references to actual objects stored elsewhere. - * - * \todo Consider using simple linked list instead of this - may - * save a lot of space, but will need linear time to find - * exact RRSet. - */ - skip_list_t *additional; - } ref; - - /*! \brief Nodes which carry references to this node. */ - da_array_t *referrers; - - /*! \brief Next zone node (should be in canonical order). */ - struct zn_node *next; - - /*! \brief Previous zone node (should be in canonical order). */ - struct zn_node *prev; -}; - -typedef struct zn_node zn_node_t; - -/*----------------------------------------------------------------------------*/ - -struct zn_ar_rrsets { - ldns_rr_list *a; - ldns_rr_list *aaaa; - const zn_node_t *cname; -}; - -typedef struct zn_ar_rrsets zn_ar_rrsets_t; - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Create a new zone node. - * - * \return Pointer to the created and initialized (empty) zone node. NULL if an - * error occured. - */ -zn_node_t *zn_create(); - -/*! - * \brief Returns the owner domain name of the zone node. - */ -ldns_rdf *zn_owner(zn_node_t *node); - -/*! - * \brief Adds one RR to the given node. - * - * If the node is empty, any RR can be inserted into it. If there are some - * records present, their owner is always the same and the inserted RR must have - * this owner as well. - * - * The RR is always inserted into proper RRSet. If there is no RRSet it should - * be part of, a new RRSet is created. - * - * \param node Zone node to add the record to. - * \param rr RR to be added into the zone node. - * - * \retval 0 On success. - * \retval TODO - */ -int zn_add_rr(zn_node_t *node, ldns_rr *rr); - -/*! - * \brief Adds a RRSet to the given node. - * - * If the node is empty, any RRSet can be inserted into it. If there are some - * records present, their owner is always the same and the inserted RRSet must - * have this owner as well. - * - * \param node Zone node to add the record to. - * \param rrset RRSet to be added into the zone node. - * - * \retval 0 On success. - * \retval TODO - */ -int zn_add_rrset(zn_node_t *node, ldns_rr_list *rrset); - -/*! - * \brief Finds a RR of the desired type in the node. - * - * \param node Zone node to search in. - * \param type Desired type of the RR to be found. - * - * \return Pointer to the RR if found. NULL otherwise. - */ -ldns_rr_list *zn_find_rrset(const zn_node_t *node, ldns_rr_type type); - -ldns_rr_list *zn_all_rrsets(const zn_node_t *node); - -int zn_is_empty(const zn_node_t *node); - -/*! - * \brief Marks the node as non-authoritative (e.g. carying only glue records). - */ -void zn_set_non_authoritative(zn_node_t *node); - -/*! - * \brief Returns 1 if \a node is non-authoritative. Otherwise 0. - */ -int zn_is_non_authoritative(const zn_node_t *node); - -/*! - * \brief Marks the node as delegation point. - */ -void zn_set_delegation_point(zn_node_t *node); - -/*! - * \brief Returns 1 if \a node is delegation point. Otherwise 0. - */ -int zn_is_delegation_point(const zn_node_t *node); - -/*! - * \brief Marks the node as a node carrying a CNAME record. - */ -void zn_set_ref_cname(zn_node_t *node, zn_node_t *cname_ref); - -/*! - * \brief Returns positive integer if \a node holds a CNAME record. Otherwise 0. - */ -int zn_has_cname(const zn_node_t *node); - -/*! - * \brief Returns the node which holds the canonical name for \a node's owner. - * - * \param node Node which holds a CNAME RR. - * - * \retval Node with owner being the canonical name of \a node's owner if there - * is such in the zone. - * \retval NULL otherwise or if \a node does not contain CNAME RR. - */ -zn_node_t *zn_get_ref_cname(const zn_node_t *node); - -int zn_add_ref(zn_node_t *node, ldns_rdf *name, ldns_rr_type type, - ldns_rr_list *ref_rrset, const zn_node_t *ref_node); - -skip_list_t *zn_get_refs(const zn_node_t *node); - -const zn_ar_rrsets_t *zn_get_ref(const zn_node_t *node, const ldns_rdf *name); - -int zn_has_mx(const zn_node_t *node); - -int zn_has_ns(const zn_node_t *node); - -int zn_has_srv(const zn_node_t *node); - -int zn_add_referrer_cname(zn_node_t *node, const zn_node_t *referrer); - -int zn_add_referrer_mx(zn_node_t *node, const zn_node_t *referrer); - -int zn_add_referrer_ns(zn_node_t *node, const zn_node_t *referrer); - -int zn_add_referrer_srv(zn_node_t *node, const zn_node_t *referrer); - -int zn_add_referrer(zn_node_t *node, const zn_node_t *referrer, - ldns_rr_type type); - -int zn_referrers_count(const zn_node_t *node); - -/*! - * \brief Adds the given glue RRSet to the list of glue RRSets in \a node. - */ -int zn_push_glue(zn_node_t *node, ldns_rr_list *glue); - -/*! - * \brief Returns all glue RRSets from the node. - * - * \param node Node to get the glue RRSets from. - - * - * \retval Glue RRSet of type \a type if \a node is a delegation point and has - * such glue stored. - * \retval NULL otherwise. - */ -ldns_rr_list *zn_get_glues(const zn_node_t *node); - -/*! - * \brief Returns the desired glue RRSet from the node. - * - * \param node Node to get the glue RRSet from. - * \param owner Owner of the glue RRSets. - * \param type Type of the glue RRSets (may be only A or AAAA). - * - * \note This function is quite ineffective. - */ -ldns_rr_list *zn_get_glue(const zn_node_t *node, ldns_rdf *owner, - ldns_rr_type type, ldns_rr_list *copied_rrs); - -/*! - * \brief Destroys the zone node, destroying all its RRSets and their RRs. - * - * \param node Pointer to pointer to the zone node. - */ -void zn_destroy(zn_node_t **node); - -/*! - * \brief Generic interface to zn_destroy() to be used from the zone data - * structure. - * - * \param item Pointer to the zn_node structure to be destroyed. - */ -void zn_destructor(void *item); - -void zn_print_rrset(void *key, void *value); - -/*----------------------------------------------------------------------------*/ - -#endif /* _CUTEDNS_ZONE_NODE_H_ */ diff --git a/deprecated/zone-parser.c b/deprecated/zone-parser.c deleted file mode 100644 index 3a73e307b3b3940e84928d5af6884be5d38a772c..0000000000000000000000000000000000000000 --- a/deprecated/zone-parser.c +++ /dev/null @@ -1,343 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include <ldns/ldns.h> - -#include "zone-parser.h" -#include "common.h" - -/*----------------------------------------------------------------------------*/ - -static const uint BUF_SIZE = 25; -static const int ERR_FILE_OPEN = -1; -static const int ERR_PARSE = -2; -static const int ERR_INSERT = -3; -static const int ERR_ALLOC = -4; -static const int ERR_COUNT = -5; -static const int ERR_ZONE_CREATE = -6; - -// default test values -static const uint16_t RRTYPE_DEFAULT = 1; // A -static const uint16_t RRCLASS_DEFAULT = 1; // IN -static const uint32_t TTL_DEFAULT = 3600; -static const unsigned int RDLENGTH_DEFAULT = 4; -static const uint8_t RDATA_DEFAULT[4] = { 127, 0, 0, 1 }; - -#define ERR_ZONE_CREATE_FAILED log_error("Zone could not be created.\n") -#define ERR_PARSING_FAILED log_error("Zone parsing failed.\n") - -/*----------------------------------------------------------------------------*/ -/* Private functions */ -/*----------------------------------------------------------------------------*/ - -//static uint zp_get_line_count(FILE *file) -//{ -// char ch = '\0'; -// uint c = 0; - -// while (ch != EOF) { -// ch = fgetc(file); -// if (ch == '\n') { -// c++; -// } -// } - -// return c; -//} - -/*----------------------------------------------------------------------------*/ - -//static int zp_resize_buffer(char **buffer, uint *buf_size, int new_size, -// int item_size) -//{ -// char *new_buf; - -// new_buf = realloc((void *)(*buffer), (new_size * item_size)); -// // if error -// if (new_buf == NULL) { -// ERR_ALLOC_FAILED; -// return -1; -// } -// *buffer = new_buf; -// *buf_size = new_size; - -// return 0; -//} - -/*----------------------------------------------------------------------------*/ - -//static int zp_test_count_domain_names(FILE *file, uint *names) -//{ -// debug_zp("Counting lines.."); -// *names = zp_get_line_count(file); -// debug_zp("%u\n", *names); - -// if (*names == -1) { -// log_error("Error reading domain names from file.\n"); -// return -1; -// } - -// debug_zp("Domains read: %d.\n", *names); - -// return 0; -//} - -/*----------------------------------------------------------------------------*/ - -//static int zp_test_read_dname(char **buffer, uint *buf_i, FILE *file, -// char *ch) -//{ -// // allocate some buffer -// debug_zp_parse("Allocating buffer\n"); - -// uint buf_size = BUF_SIZE; -// *buffer = (char *)malloc(buf_size * sizeof(char)); - -// if (*buffer == NULL) { -// ERR_ALLOC_FAILED; -// return -1; -// } - -// debug_zp_parse("Done\n"); -// *ch = fgetc(file); - -// *buf_i = 0; - -// while (*ch != ' ' && *ch != '\n' && *ch != EOF) { -// (*buffer)[*buf_i] = *ch; -// (*buf_i)++; - -// // if the buffer is not big enough, resize -// if ((*buf_i >= buf_size) -// && (zp_resize_buffer(buffer, &buf_size, -// buf_size * 2, sizeof(char)) != 0)) { -// free(*buffer); -// *buffer = NULL; -// return -1; -// } - -// *ch = fgetc(file); -// } - -// (*buffer)[*buf_i] = '\0'; - -// return 0; -//} - -/*----------------------------------------------------------------------------*/ - -//static ldns_rr *zp_test_create_rr(char *buffer) -//{ -// ldns_rr *rr = ldns_rr_new(); -// if (rr == NULL) { -// return NULL; -// } - -// ldns_rdf *rdata = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_A, -// RDLENGTH_DEFAULT, -// RDATA_DEFAULT); -// if (rdata == NULL) { -// free(rr); -// return NULL; -// } - -// ldns_rdf *owner = ldns_dname_new_frm_str(buffer); -// ldns_rr_set_owner(rr, owner); -// ldns_rr_set_class(rr, RRCLASS_DEFAULT); -// ldns_rr_set_type(rr, RRTYPE_DEFAULT); -// ldns_rr_set_ttl(rr, TTL_DEFAULT); -// ldns_rr_push_rdf(rr, rdata); - -// return rr; -//} - -/*----------------------------------------------------------------------------*/ - -//static int zp_test_parse_file(zdb_database *database, ldns_rdf *zone_name, -// FILE *file) -//{ -// int res; -// char ch = '\0'; -// char *buffer; -// ldns_rr *rr; -// int line = 0; - -// while (ch != EOF) { -// uint buf_i; -// if (zp_test_read_dname(&buffer, &buf_i, file, &ch) != 0) { -// return -1; -// } - -// line++; - -// // read rest of the characters (not interesting) -// while (ch != '\n' && ch != EOF) { -// ch = fgetc(file); -// } - -// debug_zp_parse("Read domain name %s, inserting...\n", buffer); - -// if (buf_i > 0) { -// debug_zp_parse("Creating RR with the given owner.\n"); - -// rr = zp_test_create_rr(buffer); -// if (rr == NULL) { -// ERR_ALLOC_FAILED; -// free(buffer); -// return ERR_INSERT; -// } - -// debug_zp_parse("Creating Zone Node with the given RR." -// "\n"); - -// zn_node_t *node = zn_create(1); -// if (node == NULL) { -// ERR_ALLOC_FAILED; -// free(buffer); -// ldns_rr_free(rr); -// return ERR_INSERT; -// } - -// zn_add_rr(node, rr); - -// if ((res = zdb_insert_name(database, zone_name, node)) -// != 0) { -// debug_zp_parse("\nInsert item returned %d.\n", -// res); -// if (res < 0) { -// zn_destroy(&node); -// } -// free(buffer); -// return ERR_INSERT; -// } - -// debug_zp_parse("Done.\n"); -// } -// free(buffer); -// buffer = NULL; -// } - -// return 0; -//} - -/*----------------------------------------------------------------------------*/ - -//static int zp_test_parse_zone(const char *filename, zdb_database *database) -//{ -// const char *DEFAULT_ZONE_NAME = "cz"; - -// // open the zone file -// debug_zp("Opening file...\n"); -// FILE *file = fopen(filename, "r"); - -// if (file == NULL) { -// log_error("Can't open file: %s.\n", filename); -// return ERR_FILE_OPEN; -// } - -// debug_zp("Done.\n"); - -// // determine name of the zone (and later other things) for now let's -// // assume there is only one zone and use the default name (cz) -// ldns_rdf *zone_name = ldns_dname_new_frm_str(DEFAULT_ZONE_NAME); - -// if (zone_name == NULL) { -// ERR_ALLOC_FAILED; -// fclose(file); -// return ERR_ALLOC; -// } - -// int res; - -// debug_zp("Counting domain names in the file...\n"); -// uint names; -// // count distinct domain names in the zone file -// if ((res = zp_test_count_domain_names(file, &names)) != 0) { -// fclose(file); -// free(zone_name); -// return ERR_COUNT; -// } - -// debug_zp("Done.\n"); -// debug_zp("Creating new zone with name '%s'...\n", -// ldns_rdf2str(zone_name)); - -// // create a new zone in the zone database -// if ((res = zdb_create_zone(database, zone_name, names)) != 0) { -// fclose(file); -// ldns_rdf_deep_free(zone_name); -// ERR_ZONE_CREATE_FAILED; -// return ERR_ZONE_CREATE; -// } - -// debug_zp("Done.\n"); -// fseek(file, 0, SEEK_SET); -// debug_zp("Parsing the zone file...\n"); - -// // parse the zone file and fill in the zone -// if ((res = zp_test_parse_file(database, zone_name, file)) != 0) { -// // is this necessary? -// zdb_remove_zone(database, zone_name); -// ldns_rdf_deep_free(zone_name); -// fclose(file); -// ERR_PARSING_FAILED; -// return ERR_PARSE; -// } - -// debug_zp("Done.\n"); -// ldns_rdf_deep_free(zone_name); - -//#ifdef ZP_DEBUG -// //debug_zp("\nTesting lookup..\n"); -// //test_lookup_from_file(database->head->zone, file); -//#endif - -// fclose(file); - -// return 0; -//} - -/*----------------------------------------------------------------------------*/ - -static int zp_parse_zonefile_bind(const char *filename, - zdb_database_t *database) -{ - debug_zp("Opening file...\n"); - FILE *file = fopen(filename, "r"); - - if (file == NULL) { - log_error("Can't open file: %s.\n", filename); - return ERR_FILE_OPEN; - } - - debug_zp("Done.\n"); - - ldns_zone *zone; - int line = 0; - ldns_status s; - log_info("\nParsing zone file %s...\n", filename); - s = ldns_zone_new_frm_fp_l(&zone, file, NULL, 0, LDNS_RR_CLASS_IN, - &line); - log_info("Done.\n"); - - fclose(file); - - if (s != LDNS_STATUS_OK) { - log_error("Error parsing zone file %s.\nldns returned: %s on " - "line %d\n", filename, ldns_get_errorstr_by_id(s), - line); - return -1; - } - - return zdb_add_zone(database, zone); -} - -/*----------------------------------------------------------------------------*/ -/* Public functions */ -/*----------------------------------------------------------------------------*/ - -int zp_parse_zone(const char *filename, zdb_database_t *database) -{ - //return zp_test_parse_zone(filename, database); - return zp_parse_zonefile_bind(filename, database); -} diff --git a/deprecated/zone-parser.h b/deprecated/zone-parser.h deleted file mode 100644 index fd4f288da39632d7defd1e31e19ce9f167af0aea..0000000000000000000000000000000000000000 --- a/deprecated/zone-parser.h +++ /dev/null @@ -1,40 +0,0 @@ -/*! - * \file zone-parser.h - * - * \author Lubos Slovak <lubos.slovak@nic.cz> - * - * \brief Provides interface to zone parsing. - * - * As of now, it only provides one API function for parsing a special testing - * file. Later a generic API for parsing any kind of zone file should be - * provided. - * - * \todo Consider creating whole zone and filling it prior to adding to zone - * database. Or create some API of zone database which will allow to do - * this and adds the zone only when told to. - * - * \addtogroup zonedb - * @{ - */ - -#ifndef _CUTEDNS_ZONE_PARSER_H_ -#define _CUTEDNS_ZONE_PARSER_H_ - -#include "zone-database.h" - -/*----------------------------------------------------------------------------*/ -/*! - * \brief Parses a special testing format of zone file and saves the data to the - * given database. - * - * The zone is created and added to the database prior to filling with data. - * Zone data are added one-by-one when the zone is already in the database. - * - * The testing zone file should contain each domain name on a separate row and - * followed by a space. Everything after the space is ignored. - */ -int zp_parse_zone(const char *filename, zdb_database_t *database); - -#endif /* _CUTEDNS_ZONE_PARSER_H_ */ - -/*! @} */ diff --git a/src/other/bitset.c b/src/lib/bitset.c similarity index 100% rename from src/other/bitset.c rename to src/lib/bitset.c diff --git a/src/other/bitset.h b/src/lib/bitset.h similarity index 100% rename from src/other/bitset.h rename to src/lib/bitset.h diff --git a/src/other/dynamic-array.c b/src/lib/dynamic-array.c similarity index 100% rename from src/other/dynamic-array.c rename to src/lib/dynamic-array.c diff --git a/src/other/dynamic-array.h b/src/lib/dynamic-array.h similarity index 100% rename from src/other/dynamic-array.h rename to src/lib/dynamic-array.h diff --git a/src/other/skip-list.c b/src/lib/skip-list.c similarity index 100% rename from src/other/skip-list.c rename to src/lib/skip-list.c diff --git a/src/other/skip-list.h b/src/lib/skip-list.h similarity index 100% rename from src/other/skip-list.h rename to src/lib/skip-list.h diff --git a/src/other/tree.h b/src/lib/tree.h similarity index 100% rename from src/other/tree.h rename to src/lib/tree.h