From: Aki Tuomi Date: Mon, 19 Oct 2015 05:42:04 +0000 (+0300) Subject: Convert tabs to spaces X-Git-Tag: dnsdist-1.0.0-alpha1~252^2~24^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9153c7cfa40f4ba99040136ea298775eccec39ff;p=pdns Convert tabs to spaces --- diff --git a/modules/tinydnsbackend/cdb.cc b/modules/tinydnsbackend/cdb.cc index 12ab64892..a44dc6e41 100644 --- a/modules/tinydnsbackend/cdb.cc +++ b/modules/tinydnsbackend/cdb.cc @@ -10,121 +10,119 @@ CDB::CDB(const string &cdbfile) { - - d_fd = open(cdbfile.c_str(), O_RDONLY); - if (d_fd < 0) - { - L< 0); + int hasNext = 0; + if (d_searchType == SearchKey) { + hasNext = cdb_findnext(&d_cdbf); + } else { + hasNext = cdb_seqnext(&d_seqPtr, &d_cdb); + } + return (hasNext > 0); } bool CDB::readNext(pair &value) { - while (moveToNext()) { - unsigned int pos; - unsigned int len; - - pos = cdb_keypos(&d_cdb); - len = cdb_keylen(&d_cdb); - - char *key = (char *)malloc(len); - cdb_read(&d_cdb, key, len, pos); - - if (d_searchType == SearchSuffix) { - char *p = strstr(key, d_key); - if (p == NULL) { - free(key); - continue; - } - } - string skey(key, len); - free(key); - - pos = cdb_datapos(&d_cdb); - len = cdb_datalen(&d_cdb); - char *val = (char *)malloc(len); - cdb_read(&d_cdb, val, len, pos); - string sval(val, len); - free(val); - - value = make_pair(skey, sval); - return true; - } - // We're done searching, so we can clean up d_key - if (d_searchType != SearchAll) { - free(d_key); - } - return false; + while (moveToNext()) { + unsigned int pos; + unsigned int len; + + pos = cdb_keypos(&d_cdb); + len = cdb_keylen(&d_cdb); + + char *key = (char *)malloc(len); + cdb_read(&d_cdb, key, len, pos); + + if (d_searchType == SearchSuffix) { + char *p = strstr(key, d_key); + if (p == NULL) { + free(key); + continue; + } + } + string skey(key, len); + free(key); + + pos = cdb_datapos(&d_cdb); + len = cdb_datalen(&d_cdb); + char *val = (char *)malloc(len); + cdb_read(&d_cdb, val, len, pos); + string sval(val, len); + free(val); + + value = make_pair(skey, sval); + return true; + } + // We're done searching, so we can clean up d_key + if (d_searchType != SearchAll) { + free(d_key); + } + return false; } - vector CDB::findall(string &key) { - vector ret; - struct cdb_find cdbf; - - cdb_findinit(&cdbf, &d_cdb, key.c_str(), key.size()); - int x=0; - while(cdb_findnext(&cdbf) > 0) { - x++; - unsigned int vpos = cdb_datapos(&d_cdb); - unsigned int vlen = cdb_datalen(&d_cdb); - char *val = (char *)malloc(vlen); - cdb_read(&d_cdb, val, vlen, vpos); - string sval(val, vlen); - ret.push_back(sval); - free(val); - } - return ret; + vector ret; + struct cdb_find cdbf; + + cdb_findinit(&cdbf, &d_cdb, key.c_str(), key.size()); + int x=0; + while(cdb_findnext(&cdbf) > 0) { + x++; + unsigned int vpos = cdb_datapos(&d_cdb); + unsigned int vlen = cdb_datalen(&d_cdb); + char *val = (char *)malloc(vlen); + cdb_read(&d_cdb, val, vlen, vpos); + string sval(val, vlen); + ret.push_back(sval); + free(val); + } + return ret; } diff --git a/modules/tinydnsbackend/cdb.hh b/modules/tinydnsbackend/cdb.hh index 707e8bff5..db5f2edd8 100644 --- a/modules/tinydnsbackend/cdb.hh +++ b/modules/tinydnsbackend/cdb.hh @@ -12,23 +12,23 @@ class CDB { public: - CDB(const string &cdbfile); - ~CDB(); + CDB(const string &cdbfile); + ~CDB(); - int searchKey(const string &key); - bool searchSuffix(const string &key); - void searchAll(); - bool readNext(pair &value); - vector findall(string &key); + int searchKey(const string &key); + bool searchSuffix(const string &key); + void searchAll(); + bool readNext(pair &value); + vector findall(string &key); private: - int d_fd; - bool moveToNext(); - struct cdb d_cdb; - struct cdb_find d_cdbf; - char *d_key; - unsigned d_seqPtr; - enum SearchType { SearchSuffix, SearchKey, SearchAll } d_searchType; + int d_fd; + bool moveToNext(); + struct cdb d_cdb; + struct cdb_find d_cdbf; + char *d_key; + unsigned d_seqPtr; + enum SearchType { SearchSuffix, SearchKey, SearchAll } d_searchType; }; -#endif // CDB_HH +#endif // CDB_HH diff --git a/modules/tinydnsbackend/generate-data.sh b/modules/tinydnsbackend/generate-data.sh index 3a42061ba..5a5edb6f8 100755 --- a/modules/tinydnsbackend/generate-data.sh +++ b/modules/tinydnsbackend/generate-data.sh @@ -12,28 +12,28 @@ tinydnsdata=$2 if [ ! -x $axfrget ] || [ "$axfrget" = help ] || [ ! -x $tinydnsdata ] || [ "$tinydnsdata" = help ] then - echo "Usage: ./generate-data.sh "; - exit 1 + echo "Usage: ./generate-data.sh "; + exit 1 fi # Copy original zones because the test might modify them (well only the dyndns stuff, but let's make this work for others as well) for zone in $(grep 'zone ' ../../regression-tests/named.conf | cut -f2 -d\") do - if [ -f ../../regression-tests/zones/$zone.orig ] - then - cp -f ../../regression-tests/zones/$zone.orig ../../regression-tests/zones/$zone - fi + if [ -f ../../regression-tests/zones/$zone.orig ] + then + cp -f ../../regression-tests/zones/$zone.orig ../../regression-tests/zones/$zone + fi done # CD to regression testt because named.conf has relative paths. cd ../../regression-tests ../pdns/pdns_server --daemon=no --local-port=5300 --socket-dir=./ \ - --no-shuffle --launch=bind --bind-config=../regression-tests/named.conf \ - --query-logging --send-root-referral --loglevel=0 \ - --cache-ttl=0 --no-config --local-address=127.0.0.1 \ - --bind-ignore-broken-records=yes & + --no-shuffle --launch=bind --bind-config=../regression-tests/named.conf \ + --query-logging --send-root-referral --loglevel=0 \ + --cache-ttl=0 --no-config --local-address=127.0.0.1 \ + --bind-ignore-broken-records=yes & # wait for pdns to start up sleep 5 @@ -44,9 +44,9 @@ cd $startdir for zone in $(grep 'zone ' ../../regression-tests/named.conf | cut -f2 -d\") do - $tcpclient 127.0.0.1 5300 $axfrget $zone $zone.out $zone.out.tmp - cat $zone.out >> data - rm $zone.out + $tcpclient 127.0.0.1 5300 $axfrget $zone $zone.out $zone.out.tmp + cat $zone.out >> data + rm $zone.out done $tinydnsdata diff --git a/modules/tinydnsbackend/tinydnsbackend.cc b/modules/tinydnsbackend/tinydnsbackend.cc index e22352114..3d74d97dc 100644 --- a/modules/tinydnsbackend/tinydnsbackend.cc +++ b/modules/tinydnsbackend/tinydnsbackend.cc @@ -11,329 +11,327 @@ #include #include - static string backendname="[TinyDNSBackend] "; uint32_t TinyDNSBackend::s_lastId; pthread_mutex_t TinyDNSBackend::s_domainInfoLock=PTHREAD_MUTEX_INITIALIZER; TinyDNSBackend::TDI_suffix_t TinyDNSBackend::s_domainInfo; - vector TinyDNSBackend::getLocations() { - vector ret; - - if (! d_dnspacket) { - return ret; - } - - //TODO: We do not have IPv6 support. - Netmask remote = d_dnspacket->getRealRemote(); - if (remote.getBits() != 32) { - return ret; - } - - unsigned long addr = remote.getNetwork().sin4.sin_addr.s_addr; - - char key[6]; - key[0] = '\000'; - key[1] = '\045'; - key[2] = (addr )&0xff; - key[3] = (addr >> 8)&0xff; - key[4] = (addr >> 16)&0xff; - key[5] = (addr >> 24)&0xff; - - for (int i=4;i>=0;i--) { - string searchkey(key, i+2); - CDB *reader = new CDB(getArg("dbfile")); - ret = reader->findall(searchkey); - delete reader; - - //Biggest item wins, so when we find something, we can jump out. - if (ret.size() > 0) { - break; - } - } - - return ret; + vector ret; + + if (! d_dnspacket) { + return ret; + } + + //TODO: We do not have IPv6 support. + Netmask remote = d_dnspacket->getRealRemote(); + if (remote.getBits() != 32) { + return ret; + } + + unsigned long addr = remote.getNetwork().sin4.sin_addr.s_addr; + + char key[6]; + key[0] = '\000'; + key[1] = '\045'; + key[2] = (addr )&0xff; + key[3] = (addr >> 8)&0xff; + key[4] = (addr >> 16)&0xff; + key[5] = (addr >> 24)&0xff; + + for (int i=4;i>=0;i--) { + string searchkey(key, i+2); + CDB *reader = new CDB(getArg("dbfile")); + ret = reader->findall(searchkey); + delete reader; + + //Biggest item wins, so when we find something, we can jump out. + if (ret.size() > 0) { + break; + } + } + + return ret; } TinyDNSBackend::TinyDNSBackend(const string &suffix) { - setArgPrefix("tinydns"+suffix); - d_suffix = suffix; - d_locations = mustDo("locations"); - d_ignorebogus = mustDo("ignore-bogus-records"); - d_taiepoch = 4611686018427387904ULL + getArgAsNum("tai-adjust"); - d_dnspacket = NULL; - d_cdbReader = NULL; - d_isAxfr = false; - d_isWildcardQuery = false; + setArgPrefix("tinydns"+suffix); + d_suffix = suffix; + d_locations = mustDo("locations"); + d_ignorebogus = mustDo("ignore-bogus-records"); + d_taiepoch = 4611686018427387904ULL + getArgAsNum("tai-adjust"); + d_dnspacket = NULL; + d_cdbReader = NULL; + d_isAxfr = false; + d_isWildcardQuery = false; } void TinyDNSBackend::getUpdatedMasters(vector* retDomains) { - Lock l(&s_domainInfoLock); //TODO: We could actually lock less if we do it per suffix. - - if (! s_domainInfo.count(d_suffix)) { - TDI_t tmp; - s_domainInfo.insert( make_pair(d_suffix,tmp) ); - } - TDI_t *domains = &s_domainInfo[d_suffix]; - - vector allDomains; - getAllDomains(&allDomains); - if (domains->size() == 0 && !mustDo("notify-on-startup")) { - for (vector::iterator di=allDomains.begin(); di!=allDomains.end(); ++di) { - di->notified_serial = 0; - } - } - - for(vector::iterator di=allDomains.begin(); di!=allDomains.end(); ++di) { - TDIByZone_t& zone_index = domains->get(); - TDIByZone_t::iterator itByZone = zone_index.find(di->zone); - if (itByZone == zone_index.end()) { - s_lastId++; - - TinyDomainInfo tmp; - tmp.zone = di->zone; - tmp.id = s_lastId; - tmp.notified_serial = di->serial; - domains->insert(tmp); - - di->id = s_lastId; - if (di->notified_serial > 0) { - retDomains->push_back(*di); - } - } else { - if (itByZone->notified_serial < di->serial) { - di->id = itByZone->id; - retDomains->push_back(*di); - } - } - } + Lock l(&s_domainInfoLock); //TODO: We could actually lock less if we do it per suffix. + + if (! s_domainInfo.count(d_suffix)) { + TDI_t tmp; + s_domainInfo.insert( make_pair(d_suffix,tmp) ); + } + + TDI_t *domains = &s_domainInfo[d_suffix]; + + vector allDomains; + getAllDomains(&allDomains); + if (domains->size() == 0 && !mustDo("notify-on-startup")) { + for (vector::iterator di=allDomains.begin(); di!=allDomains.end(); ++di) { + di->notified_serial = 0; + } + } + + for(vector::iterator di=allDomains.begin(); di!=allDomains.end(); ++di) { + TDIByZone_t& zone_index = domains->get(); + TDIByZone_t::iterator itByZone = zone_index.find(di->zone); + if (itByZone == zone_index.end()) { + s_lastId++; + + TinyDomainInfo tmp; + tmp.zone = di->zone; + tmp.id = s_lastId; + tmp.notified_serial = di->serial; + domains->insert(tmp); + + di->id = s_lastId; + if (di->notified_serial > 0) { + retDomains->push_back(*di); + } + } else { + if (itByZone->notified_serial < di->serial) { + di->id = itByZone->id; + retDomains->push_back(*di); + } + } + } } void TinyDNSBackend::setNotified(uint32_t id, uint32_t serial) { - Lock l(&s_domainInfoLock); - if (!s_domainInfo.count(d_suffix)) { - throw new PDNSException("Can't get list of domains to set the serial."); - } - TDI_t *domains = &s_domainInfo[d_suffix]; - TDIById_t& domain_index = domains->get(); - TDIById_t::iterator itById = domain_index.find(id); - if (itById == domain_index.end()) { - L<zone<<" to "<get(); + TDIById_t::iterator itById = domain_index.find(id); + if (itById == domain_index.end()) { + L<zone<<" to "< *domains, bool include_disabled) { - d_isAxfr=true; - d_dnspacket = NULL; - - d_cdbReader=new CDB(getArg("dbfile")); - d_cdbReader->searchAll(); - DNSResourceRecord rr; - - while (get(rr)) { - if (rr.qtype.getCode() == QType::SOA) { - SOAData sd; - fillSOAData(rr.content, sd); - - DomainInfo di; - di.id = -1; //TODO: Check if this is ok. - di.backend=this; - di.zone = rr.qname; - di.serial = sd.serial; - di.notified_serial = sd.serial; - di.kind = DomainInfo::Master; - di.last_check = time(0); - domains->push_back(di); - } - } + d_isAxfr=true; + d_dnspacket = NULL; + + d_cdbReader=new CDB(getArg("dbfile")); + d_cdbReader->searchAll(); + DNSResourceRecord rr; + + while (get(rr)) { + if (rr.qtype.getCode() == QType::SOA) { + SOAData sd; + fillSOAData(rr.content, sd); + + DomainInfo di; + di.id = -1; //TODO: Check if this is ok. + di.backend=this; + di.zone = rr.qname; + di.serial = sd.serial; + di.notified_serial = sd.serial; + di.kind = DomainInfo::Master; + di.last_check = time(0); + domains->push_back(di); + } + } } bool TinyDNSBackend::list(const DNSName &target, int domain_id, bool include_disabled) { - d_isAxfr=true; - string key = target.toDNSString(); // FIXME400 bug: no lowercase here? or promise that from core? - d_cdbReader=new CDB(getArg("dbfile")); - return d_cdbReader->searchSuffix(key); + d_isAxfr=true; + string key = target.toDNSString(); // FIXME400 bug: no lowercase here? or promise that from core? + d_cdbReader=new CDB(getArg("dbfile")); + return d_cdbReader->searchSuffix(key); } void TinyDNSBackend::lookup(const QType &qtype, const DNSName &qdomain, DNSPacket *pkt_p, int zoneId) { - d_isAxfr = false; - string queryDomain = toLowerCanonic(qdomain.toString()); + d_isAxfr = false; + string queryDomain = toLowerCanonic(qdomain.toString()); - string key=simpleCompress(queryDomain); + string key=simpleCompress(queryDomain); - DLOG(L<searchKey(key); - d_dnspacket = pkt_p; + d_cdbReader=new CDB(getArg("dbfile")); + d_cdbReader->searchKey(key); + d_dnspacket = pkt_p; } bool TinyDNSBackend::get(DNSResourceRecord &rr) { - pair record; - - while (d_cdbReader->readNext(record)) { - string val = record.second; - string key = record.first; - - //DLOG(L< bytes; - const char *sval = val.c_str(); - unsigned int len = val.size(); - bytes.resize(len); - copy(sval, sval+len, bytes.begin()); - PacketReader pr(bytes); - rr.qtype = QType(pr.get16BitInt()); - - if(d_isAxfr || d_qtype.getCode() == QType::ANY || rr.qtype == d_qtype) { - char locwild = pr.get8BitInt(); - if(locwild != '\075' && (locwild == '\076' || locwild == '\053')) { - if (d_isAxfr && d_locations) { // We skip records with a location in AXFR, unless we disable locations. - continue; - } - char recloc[2]; - recloc[0] = pr.get8BitInt(); - recloc[1] = pr.get8BitInt(); - - if (d_locations) { - bool foundLocation = false; - vector locations = getLocations(); - while(locations.size() > 0) { - string locId = locations.back(); - locations.pop_back(); - - if (recloc[0] == locId[0] && recloc[1] == locId[1]) { - foundLocation = true; - break; - } - } - if (!foundLocation) { - continue; - } - } - } - - if (d_isAxfr && (val[2] == '\052' || val[2] == '\053' )) { // Keys are not stored with wildcard character, with AXFR we need to add that. - key.insert(0, 1, '\052'); - key.insert(0, 1, '\001'); - } - // rr.qname.clear(); - rr.qname=DNSName(key.c_str(), key.size(), 0, false); - rr.domain_id=-1; - // 11:13.21 <@ahu> IT IS ALWAYS AUTH --- well not really because we are just a backend :-) - // We could actually do NSEC3-NARROW DNSSEC according to Habbie, if we do, we need to change something ehre. - rr.auth = true; - - rr.ttl = pr.get32BitInt(); - uint64_t timestamp = pr.get32BitInt(); - timestamp <<= 32; - timestamp += pr.get32BitInt(); - if(timestamp) { - uint64_t now = d_taiepoch + time(NULL); - if (rr.ttl == 0) { - if (timestamp < now) { - continue; - } - rr.ttl = timestamp - now; - } else if (now <= timestamp) { - continue; - } - } - try { - DNSRecord dr; - dr.d_class = 1; - dr.d_type = rr.qtype.getCode(); - dr.d_clen = val.size()-pr.d_pos; - - DNSRecordContent *drc = DNSRecordContent::mastermake(dr, pr); - rr.content = drc->getZoneRepresentation(); - cerr<<"CONTENT: "< record; + + while (d_cdbReader->readNext(record)) { + string val = record.second; + string key = record.first; + + //DLOG(L< bytes; + const char *sval = val.c_str(); + unsigned int len = val.size(); + bytes.resize(len); + copy(sval, sval+len, bytes.begin()); + PacketReader pr(bytes); + rr.qtype = QType(pr.get16BitInt()); + + if(d_isAxfr || d_qtype.getCode() == QType::ANY || rr.qtype == d_qtype) { + char locwild = pr.get8BitInt(); + if(locwild != '\075' && (locwild == '\076' || locwild == '\053')) { + if (d_isAxfr && d_locations) { // We skip records with a location in AXFR, unless we disable locations. + continue; + } + char recloc[2]; + recloc[0] = pr.get8BitInt(); + recloc[1] = pr.get8BitInt(); + + if (d_locations) { + bool foundLocation = false; + vector locations = getLocations(); + while(locations.size() > 0) { + string locId = locations.back(); + locations.pop_back(); + + if (recloc[0] == locId[0] && recloc[1] == locId[1]) { + foundLocation = true; + break; + } + } + if (!foundLocation) { + continue; + } + } + } + + if (d_isAxfr && (val[2] == '\052' || val[2] == '\053' )) { // Keys are not stored with wildcard character, with AXFR we need to add that. + key.insert(0, 1, '\052'); + key.insert(0, 1, '\001'); + } + // rr.qname.clear(); + rr.qname=DNSName(key.c_str(), key.size(), 0, false); + rr.domain_id=-1; + // 11:13.21 <@ahu> IT IS ALWAYS AUTH --- well not really because we are just a backend :-) + // We could actually do NSEC3-NARROW DNSSEC according to Habbie, if we do, we need to change something ehre. + rr.auth = true; + + rr.ttl = pr.get32BitInt(); + uint64_t timestamp = pr.get32BitInt(); + timestamp <<= 32; + timestamp += pr.get32BitInt(); + if(timestamp) { + uint64_t now = d_taiepoch + time(NULL); + if (rr.ttl == 0) { + if (timestamp < now) { + continue; + } + rr.ttl = timestamp - now; + } else if (now <= timestamp) { + continue; + } + } + try { + DNSRecord dr; + dr.d_class = 1; + dr.d_type = rr.qtype.getCode(); + dr.d_clen = val.size()-pr.d_pos; + + DNSRecordContent *drc = DNSRecordContent::mastermake(dr, pr); + rr.content = drc->getZoneRepresentation(); + cerr<<"CONTENT: "< *domains, bool include_disabled=false); + // Methods for simple operation + TinyDNSBackend(const string &suffix); + void lookup(const QType &qtype, const DNSName &qdomain, DNSPacket *pkt_p=0, int zoneId=-1); + bool list(const DNSName &target, int domain_id, bool include_disabled=false); + bool get(DNSResourceRecord &rr); + void getAllDomains(vector *domains, bool include_disabled=false); - //Master mode operation - void getUpdatedMasters(vector* domains); - void setNotified(uint32_t id, uint32_t serial); + //Master mode operation + void getUpdatedMasters(vector* domains); + void setNotified(uint32_t id, uint32_t serial); private: - vector getLocations(); + vector getLocations(); - //TypeDefs - struct tag_zone{}; - struct tag_domainid{}; - typedef multi_index_container< - TinyDomainInfo, - indexed_by< - hashed_unique, member >, - hashed_unique, member > - > - > TDI_t; - typedef map TDI_suffix_t; - typedef TDI_t::index::type TDIByZone_t; - typedef TDI_t::index::type TDIById_t; + //TypeDefs + struct tag_zone{}; + struct tag_domainid{}; + typedef multi_index_container< + TinyDomainInfo, + indexed_by< + hashed_unique, member >, + hashed_unique, member > + > + > TDI_t; + typedef map TDI_suffix_t; + typedef TDI_t::index::type TDIByZone_t; + typedef TDI_t::index::type TDIById_t; - //data member variables - uint64_t d_taiepoch; - QType d_qtype; - CDB *d_cdbReader; - DNSPacket *d_dnspacket; // used for location and edns-client support. - bool d_isWildcardQuery; // Indicate if the query received was a wildcard query. - bool d_isAxfr; // Indicate if we received a list() and not a lookup(). - bool d_locations; - bool d_ignorebogus; - string d_suffix; - + //data member variables + uint64_t d_taiepoch; + QType d_qtype; + CDB *d_cdbReader; + DNSPacket *d_dnspacket; // used for location and edns-client support. + bool d_isWildcardQuery; // Indicate if the query received was a wildcard query. + bool d_isAxfr; // Indicate if we received a list() and not a lookup(). + bool d_locations; + bool d_ignorebogus; + string d_suffix; - // Statics - static pthread_mutex_t s_domainInfoLock; - static TDI_suffix_t s_domainInfo; - static uint32_t s_lastId; // used to give a domain an id. + // Statics + static pthread_mutex_t s_domainInfoLock; + static TDI_suffix_t s_domainInfo; + static uint32_t s_lastId; // used to give a domain an id. }; -#endif // TINYDNSBACKEND_HH +#endif // TINYDNSBACKEND_HH