shared_ptr<DNSCryptoKeyEngine> DNSCryptoKeyEngine::makeFromISCFile(DNSKEYRecordContent& drc, const char* fname)
{
string sline, isc;
- FILE *fp=fopen(fname, "r");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname, "r"), fclose);
if(!fp) {
throw runtime_error("Unable to read file '"+string(fname)+"' for generating DNS Private Key");
}
- while(stringfgets(fp, sline)) {
+ while(stringfgets(fp.get(), sline)) {
isc += sline;
}
- fclose(fp);
+ fp.reset();
+
shared_ptr<DNSCryptoKeyEngine> dke = makeFromISCString(drc, isc);
if(!dke->checkKey()) {
throw runtime_error("Invalid DNS Private Key in file '"+string(fname));
bool readFileIfThere(const char* fname, std::string* line)
{
line->clear();
- FILE* fp = fopen(fname, "r");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname, "r"), fclose);
if(!fp)
return false;
- stringfgets(fp, *line);
- fclose(fp);
+ stringfgets(fp.get(), *line);
+ fp.reset();
+
return true;
}
static uint64_t dumpNegCache(NegCache& negcache, int fd)
{
- FILE* fp=fdopen(dup(fd), "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(dup(fd), "w"), fclose);
if(!fp) { // dup probably failed
return 0;
}
uint64_t ret;
- fprintf(fp, "; negcache dump from thread follows\n;\n");
- ret = negcache.dumpToFile(fp);
- fclose(fp);
+ fprintf(fp.get(), "; negcache dump from thread follows\n;\n");
+ ret = negcache.dumpToFile(fp.get());
return ret;
}
return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
}
- FILE* fp = fdopen(fd, "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(fd, "w"), fclose);
if (!fp) {
close(fd);
return "Error converting file descriptor: "+string(strerror(errno))+"\n";
}
- zone->dump(fp);
- fclose(fp);
+ zone->dump(fp.get());
return "done\n";
}
}
-void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, uint16_t ecsBegin, uint16_t ecsEnd)
-{
- vState valState;
- boost::optional<RecProtoBufMessage> pb(boost::none);
- insertResponsePacket(tag, qhash, std::move(query), qname, qtype, qclass, std::move(responsePacket), now, ttl, valState, ecsBegin, ecsEnd, std::move(pb));
-}
-
void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, uint16_t ecsBegin, uint16_t ecsEnd, boost::optional<RecProtoBufMessage>&& protobufMessage)
{
auto& idx = d_packetCache.get<HashTag>();
uint64_t RecursorPacketCache::doDump(int fd)
{
- FILE* fp=fdopen(dup(fd), "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(dup(fd), "w"), fclose);
if(!fp) { // dup probably failed
return 0;
}
- fprintf(fp, "; main packet cache dump from thread follows\n;\n");
+ fprintf(fp.get(), "; main packet cache dump from thread follows\n;\n");
const auto& sidx=d_packetCache.get<1>();
uint64_t count=0;
for(auto i=sidx.cbegin(); i != sidx.cend(); ++i) {
count++;
try {
- fprintf(fp, "%s %" PRId64 " %s ; tag %d\n", i->d_name.toString().c_str(), static_cast<int64_t>(i->d_ttd - now), DNSRecordContent::NumberToType(i->d_type).c_str(), i->d_tag);
+ fprintf(fp.get(), "%s %" PRId64 " %s ; tag %d\n", i->d_name.toString().c_str(), static_cast<int64_t>(i->d_ttd - now), DNSRecordContent::NumberToType(i->d_type).c_str(), i->d_tag);
}
catch(...) {
- fprintf(fp, "; error printing '%s'\n", i->d_name.empty() ? "EMPTY" : i->d_name.toString().c_str());
+ fprintf(fp.get(), "; error printing '%s'\n", i->d_name.empty() ? "EMPTY" : i->d_name.toString().c_str());
}
}
- fclose(fp);
return count;
}
bool getResponsePacket(unsigned int tag, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, uint32_t* qhash);
bool getResponsePacket(unsigned int tag, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, uint16_t* ecsBegin, uint16_t* ecsEnd, RecProtoBufMessage* protobufMessage);
bool getResponsePacket(unsigned int tag, const std::string& queryPacket, DNSName& qname, uint16_t* qtype, uint16_t* qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, uint16_t* ecsBegin, uint16_t* ecsEnd, RecProtoBufMessage* protobufMessage);
- void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, uint16_t ecsBegin, uint16_t ecsEnd);
void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, uint16_t ecsBegin, uint16_t ecsEnd, boost::optional<RecProtoBufMessage>&& protobufMessage);
void doPruneTo(unsigned int maxSize=250000);
uint64_t doDump(int fd);
uint64_t MemRecursorCache::doDump(int fd)
{
- FILE* fp=fdopen(dup(fd), "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(dup(fd), "w"), fclose);
if(!fp) { // dup probably failed
return 0;
}
- fprintf(fp, "; main record cache dump from thread follows\n;\n");
+ fprintf(fp.get(), "; main record cache dump from thread follows\n;\n");
const auto& sidx=d_cache.get<SequencedTag>();
uint64_t count=0;
for(const auto j : i.d_records) {
count++;
try {
- fprintf(fp, "%s %" PRId64 " IN %s %s ; (%s) auth=%i %s\n", i.d_qname.toString().c_str(), static_cast<int64_t>(i.d_ttd - now), DNSRecordContent::NumberToType(i.d_qtype).c_str(), j->getZoneRepresentation().c_str(), vStates[i.d_state], i.d_auth, i.d_netmask.empty() ? "" : i.d_netmask.toString().c_str());
+ fprintf(fp.get(), "%s %" PRId64 " IN %s %s ; (%s) auth=%i %s\n", i.d_qname.toString().c_str(), static_cast<int64_t>(i.d_ttd - now), DNSRecordContent::NumberToType(i.d_qtype).c_str(), j->getZoneRepresentation().c_str(), vStates[i.d_state], i.d_auth, i.d_netmask.empty() ? "" : i.d_netmask.toString().c_str());
}
catch(...) {
- fprintf(fp, "; error printing '%s'\n", i.d_qname.empty() ? "EMPTY" : i.d_qname.toString().c_str());
+ fprintf(fp.get(), "; error printing '%s'\n", i.d_qname.empty() ? "EMPTY" : i.d_qname.toString().c_str());
}
}
for(const auto &sig : i.d_signatures) {
count++;
try {
- fprintf(fp, "%s %" PRId64 " IN RRSIG %s ; %s\n", i.d_qname.toString().c_str(), static_cast<int64_t>(i.d_ttd - now), sig->getZoneRepresentation().c_str(), i.d_netmask.empty() ? "" : i.d_netmask.toString().c_str());
+ fprintf(fp.get(), "%s %" PRId64 " IN RRSIG %s ; %s\n", i.d_qname.toString().c_str(), static_cast<int64_t>(i.d_ttd - now), sig->getZoneRepresentation().c_str(), i.d_netmask.empty() ? "" : i.d_netmask.toString().c_str());
}
catch(...) {
- fprintf(fp, "; error printing '%s'\n", i.d_qname.empty() ? "EMPTY" : i.d_qname.toString().c_str());
+ fprintf(fp.get(), "; error printing '%s'\n", i.d_qname.empty() ? "EMPTY" : i.d_qname.toString().c_str());
}
}
}
- fclose(fp);
return count;
}
if(!::arg()["forward-zones-file"].empty()) {
g_log<<Logger::Warning<<"Reading zone forwarding information from '"<<::arg()["forward-zones-file"]<<"'"<<endl;
SyncRes::AuthDomain ad;
- FILE *rfp=fopen(::arg()["forward-zones-file"].c_str(), "r");
-
- if(!rfp) {
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(::arg()["forward-zones-file"].c_str(), "r"), fclose);
+ if(!fp) {
throw PDNSException("Error opening forward-zones-file '"+::arg()["forward-zones-file"]+"': "+stringerror());
}
- shared_ptr<FILE> fp=shared_ptr<FILE>(rfp, fclose);
-
string line;
int linenum=0;
uint64_t before = newMap->size();
return false;
}
- FILE * fp = fdopen(fd, "w+");
- if (fp == nullptr) {
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(fd, "w+"), fclose);
+ if (!fp) {
close(fd);
g_log<<Logger::Warning<<"Unable to open a file pointer to dump the content of the RPZ zone "<<zoneName.toLogString()<<endl;
return false;
}
-
fd = -1;
try {
- newZone->dump(fp);
+ newZone->dump(fp.get());
}
catch(const std::exception& e) {
g_log<<Logger::Warning<<"Error while dumping the content of the RPZ zone "<<zoneName.toLogString()<<": "<<e.what()<<endl;
- fclose(fp);
return false;
}
- if (fflush(fp) != 0) {
+ if (fflush(fp.get()) != 0) {
g_log<<Logger::Warning<<"Error while flushing the content of the RPZ zone "<<zoneName.toLogString()<<" to the dump file: "<<strerror(errno)<<endl;
return false;
}
- if (fsync(fileno(fp)) != 0) {
+ if (fsync(fileno(fp.get())) != 0) {
g_log<<Logger::Warning<<"Error while syncing the content of the RPZ zone "<<zoneName.toLogString()<<" to the dump file: "<<strerror(errno)<<endl;
return false;
}
- if (fclose(fp) != 0) {
+ if (fclose(fp.release()) != 0) {
g_log<<Logger::Warning<<"Error while writing the content of the RPZ zone "<<zoneName.toLogString()<<" to the dump file: "<<strerror(errno)<<endl;
return false;
}
uint64_t SyncRes::doEDNSDump(int fd)
{
- FILE* fp=fdopen(dup(fd), "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(dup(fd), "w"), fclose);
if (!fp) {
return 0;
}
uint64_t count = 0;
- fprintf(fp,"; edns from thread follows\n;\n");
+ fprintf(fp.get(),"; edns from thread follows\n;\n");
for(const auto& eds : t_sstorage.ednsstatus) {
count++;
- fprintf(fp, "%s\t%d\t%s", eds.first.toString().c_str(), (int)eds.second.mode, ctime(&eds.second.modeSetAt));
+ fprintf(fp.get(), "%s\t%d\t%s", eds.first.toString().c_str(), (int)eds.second.mode, ctime(&eds.second.modeSetAt));
}
- fclose(fp);
return count;
}
uint64_t SyncRes::doDumpNSSpeeds(int fd)
{
- FILE* fp=fdopen(dup(fd), "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(dup(fd), "w"), fclose);
if(!fp)
return 0;
- fprintf(fp, "; nsspeed dump from thread follows\n;\n");
+ fprintf(fp.get(), "; nsspeed dump from thread follows\n;\n");
uint64_t count=0;
for(const auto& i : t_sstorage.nsSpeeds)
count++;
// an <empty> can appear hear in case of authoritative (hosted) zones
- fprintf(fp, "%s -> ", i.first.toLogString().c_str());
+ fprintf(fp.get(), "%s -> ", i.first.toLogString().c_str());
for(const auto& j : i.second.d_collection)
{
// typedef vector<pair<ComboAddress, DecayingEwma> > collection_t;
- fprintf(fp, "%s/%f ", j.first.toString().c_str(), j.second.peek());
+ fprintf(fp.get(), "%s/%f ", j.first.toString().c_str(), j.second.peek());
}
- fprintf(fp, "\n");
+ fprintf(fp.get(), "\n");
}
- fclose(fp);
return count;
}
uint64_t SyncRes::doDumpThrottleMap(int fd)
{
- FILE* fp=fdopen(dup(fd), "w");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(dup(fd), "w"), fclose);
if(!fp)
return 0;
- fprintf(fp, "; throttle map dump follows\n");
- fprintf(fp, "; remote IP\tqname\tqtype\tcount\tttd\n");
+ fprintf(fp.get(), "; throttle map dump follows\n");
+ fprintf(fp.get(), "; remote IP\tqname\tqtype\tcount\tttd\n");
uint64_t count=0;
const auto& throttleMap = t_sstorage.throttle.getThrottleMap();
{
count++;
// remote IP, dns name, qtype, count, ttd
- fprintf(fp, "%s\t%s\t%d\t%u\t%s", i.first.get<0>().toString().c_str(), i.first.get<1>().toLogString().c_str(), i.first.get<2>(), i.second.count, ctime(&i.second.ttd));
+ fprintf(fp.get(), "%s\t%s\t%d\t%u\t%s", i.first.get<0>().toString().c_str(), i.first.get<1>().toLogString().c_str(), i.first.get<2>(), i.second.count, ctime(&i.second.ttd));
}
- fclose(fp);
+
return count;
}
bool found = locateEDNSOption(query, EDNSOptionCode::ECS, &optContentStart, &optContentLen);
BOOST_CHECK_EQUAL(found, true);
- BOOST_CHECK_EQUAL(optContentStart, optRDExpectedOffset + sizeof(uint16_t) /* RD len */ + /* option code */ 2 + /* option length */ 2);
- BOOST_CHECK_EQUAL(optContentLen, sizeOfECSContent);
+ if (found == true) {
+ BOOST_CHECK_EQUAL(optContentStart, optRDExpectedOffset + sizeof(uint16_t) /* RD len */ + /* option code */ 2 + /* option length */ 2);
+ BOOST_CHECK_EQUAL(optContentLen, sizeOfECSContent);
+ }
/* truncated packet */
query.resize(query.size() - 1);
bool found = locateEDNSOption(query, EDNSOptionCode::ECS, &optContentStart, &optContentLen);
BOOST_CHECK_EQUAL(found, true);
- BOOST_CHECK_EQUAL(optContentStart, optRDExpectedOffset + sizeof(uint16_t) /* RD len */ + sizeOfCookieOption + /* option code */ 2 + /* option length */ 2);
- BOOST_CHECK_EQUAL(optContentLen, sizeOfECSContent);
+ if (found == true) {
+ BOOST_CHECK_EQUAL(optContentStart, optRDExpectedOffset + sizeof(uint16_t) /* RD len */ + sizeOfCookieOption + /* option code */ 2 + /* option length */ 2);
+ BOOST_CHECK_EQUAL(optContentLen, sizeOfECSContent);
+ }
/* truncated packet */
query.resize(query.size() - 1);
pw.commit();
string rpacket((const char*)&packet[0], packet.size());
- rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 1);
rpc.doPruneTo(0);
BOOST_CHECK_EQUAL(rpc.size(), 0);
- rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 1);
rpc.doWipePacketCache(qname);
BOOST_CHECK_EQUAL(rpc.size(), 0);
- rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 1);
uint32_t qhash2 = 0;
bool found = rpc.getResponsePacket(tag, qpacket, time(nullptr), &fpacket, &age, &qhash2);
BOOST_CHECK(r1packet != r2packet);
/* inserting a response for tag1 */
- rpc.insertResponsePacket(tag1, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r1packet), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag1, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r1packet), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 1);
/* inserting a different response for tag2, should not override the first one */
- rpc.insertResponsePacket(tag2, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r2packet), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag2, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r2packet), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 2);
/* remove all responses from the cache */
BOOST_CHECK_EQUAL(rpc.size(), 0);
/* reinsert both */
- rpc.insertResponsePacket(tag1, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r1packet), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag1, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r1packet), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 1);
- rpc.insertResponsePacket(tag2, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r2packet), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag2, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r2packet), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 2);
/* remove the responses by qname, should remove both */
BOOST_CHECK_EQUAL(rpc.size(), 0);
/* insert the response for tag1 */
- rpc.insertResponsePacket(tag1, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r1packet), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag1, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r1packet), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 1);
/* we can retrieve it */
BOOST_CHECK_EQUAL(temphash, qhash);
/* adding a response for the second tag */
- rpc.insertResponsePacket(tag2, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r2packet), time(0), ttd, 0, 0);
+ rpc.insertResponsePacket(tag2, qhash, string(qpacket), qname, QType::A, QClass::IN, string(r2packet), time(0), ttd, Indeterminate, 0, 0, boost::none);
BOOST_CHECK_EQUAL(rpc.size(), 2);
/* We still get the correct response for the first tag */