From: Remi Gacogne Date: Fri, 7 Jun 2019 16:16:52 +0000 (+0200) Subject: rec: Reuse the outgoing query protobuf for the incoming response X-Git-Tag: dnsdist-1.4.0-rc1~130^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8623c19aff56deb660c176b48c89e7d3bc61c99f;p=pdns rec: Reuse the outgoing query protobuf for the incoming response --- diff --git a/pdns/lwres.cc b/pdns/lwres.cc index 3d3aee1e0..d6ff02696 100644 --- a/pdns/lwres.cc +++ b/pdns/lwres.cc @@ -118,48 +118,46 @@ static void logFstreamResponse(const std::shared_ptr>>& outgoingLoggers, boost::optional initialRequestId, const boost::uuids::uuid& uuid, const ComboAddress& ip, const DNSName& domain, int type, uint16_t qid, bool doTCP, size_t bytes, boost::optional& srcmask) +static void logOutgoingQuery(const std::shared_ptr>>& outgoingLoggers, boost::optional& message, boost::optional initialRequestId, const boost::uuids::uuid& uuid, const ComboAddress& ip, const DNSName& domain, int type, uint16_t qid, bool doTCP, size_t bytes, boost::optional& srcmask) { if(!outgoingLoggers) return; - RecProtoBufMessage message(DNSProtoBufMessage::OutgoingQuery, uuid, nullptr, &ip, domain, type, QClass::IN, qid, doTCP, bytes); - message.setServerIdentity(SyncRes::s_serverID); + message = RecProtoBufMessage(DNSProtoBufMessage::OutgoingQuery, uuid, nullptr, &ip, domain, type, QClass::IN, qid, doTCP, bytes); + message->setServerIdentity(SyncRes::s_serverID); if (initialRequestId) { - message.setInitialRequestID(*initialRequestId); + message->setInitialRequestID(*initialRequestId); } if (srcmask) { - message.setEDNSSubnet(*srcmask); + message->setEDNSSubnet(*srcmask); } // cerr <serialize(str); for (auto& logger : *outgoingLoggers) { logger->queueData(str); } } -static void logIncomingResponse(const std::shared_ptr>>& outgoingLoggers, boost::optional initialRequestId, const boost::uuids::uuid& uuid, const ComboAddress& ip, const DNSName& domain, int type, uint16_t qid, bool doTCP, size_t bytes, int rcode, const std::vector& records, const struct timeval& queryTime, const std::set& exportTypes) +static void logIncomingResponse(const std::shared_ptr>>& outgoingLoggers, boost::optional& message, size_t bytes, int rcode, const std::vector& records, const struct timeval& queryTime, const std::set& exportTypes) { - if(!outgoingLoggers) + if(!outgoingLoggers || !message) return; - RecProtoBufMessage message(DNSProtoBufMessage::IncomingResponse, uuid, nullptr, &ip, domain, type, QClass::IN, qid, doTCP, bytes); - message.setServerIdentity(SyncRes::s_serverID); - if (initialRequestId) { - message.setInitialRequestID(*initialRequestId); - } - message.setQueryTime(queryTime.tv_sec, queryTime.tv_usec); - message.setResponseCode(rcode); - message.addRRs(records, exportTypes); + message->updateTime(); + message->setType(DNSProtoBufMessage::IncomingResponse); + message->setBytes(bytes); + message->setQueryTime(queryTime.tv_sec, queryTime.tv_usec); + message->setResponseCode(rcode); + message->addRRs(records, exportTypes); // cerr <serialize(str); for (auto& logger : *outgoingLoggers) { logger->queueData(str); @@ -226,10 +224,11 @@ int asyncresolve(const ComboAddress& ip, const DNSName& domain, int type, bool d #ifdef HAVE_PROTOBUF boost::uuids::uuid uuid; const struct timeval queryTime = *now; + boost::optional pbMessage = boost::none; if (outgoingLoggers) { uuid = getUniqueID(); - logOutgoingQuery(outgoingLoggers, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, vpacket.size(), srcmask); + logOutgoingQuery(outgoingLoggers, pbMessage, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, vpacket.size(), srcmask); } #endif /* HAVE_PROTOBUF */ #ifdef HAVE_FSTRM @@ -327,7 +326,7 @@ int asyncresolve(const ComboAddress& ip, const DNSName& domain, int type, bool d if(mdp.d_header.rcode == RCode::FormErr && mdp.d_qname.empty() && mdp.d_qtype == 0 && mdp.d_qclass == 0) { #ifdef HAVE_PROTOBUF if(outgoingLoggers) { - logIncomingResponse(outgoingLoggers, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, len, lwr->d_rcode, lwr->d_records, queryTime, exportTypes); + logIncomingResponse(outgoingLoggers, pbMessage, len, lwr->d_rcode, lwr->d_records, queryTime, exportTypes); } #endif lwr->d_validpacket=true; @@ -373,7 +372,7 @@ int asyncresolve(const ComboAddress& ip, const DNSName& domain, int type, bool d #ifdef HAVE_PROTOBUF if(outgoingLoggers) { - logIncomingResponse(outgoingLoggers, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, len, lwr->d_rcode, lwr->d_records, queryTime, exportTypes); + logIncomingResponse(outgoingLoggers, pbMessage, len, lwr->d_rcode, lwr->d_records, queryTime, exportTypes); } #endif lwr->d_validpacket=true; @@ -386,7 +385,7 @@ int asyncresolve(const ComboAddress& ip, const DNSName& domain, int type, bool d g_stats.serverParseError++; #ifdef HAVE_PROTOBUF if(outgoingLoggers) { - logIncomingResponse(outgoingLoggers, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, len, lwr->d_rcode, lwr->d_records, queryTime, exportTypes); + logIncomingResponse(outgoingLoggers, pbMessage, len, lwr->d_rcode, lwr->d_records, queryTime, exportTypes); } #endif lwr->d_validpacket=false; diff --git a/pdns/protobuf.cc b/pdns/protobuf.cc index 37fb01e8b..fd491ac66 100644 --- a/pdns/protobuf.cc +++ b/pdns/protobuf.cc @@ -300,12 +300,16 @@ void DNSProtoBufMessage::setInitialRequestID(const boost::uuids::uuid& uuid) std::copy(uuid.begin(), uuid.end(), messageId->begin()); } -void DNSProtoBufMessage::update(const boost::uuids::uuid& uuid, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, uint16_t id) +void DNSProtoBufMessage::updateTime() { struct timespec ts; gettime(&ts, true); setTime(ts.tv_sec, ts.tv_nsec / 1000); +} +void DNSProtoBufMessage::update(const boost::uuids::uuid& uuid, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, uint16_t id) +{ + updateTime(); setUUID(uuid); d_message.set_id(ntohs(id)); diff --git a/pdns/protobuf.hh b/pdns/protobuf.hh index c52af4162..f87dd5f08 100644 --- a/pdns/protobuf.hh +++ b/pdns/protobuf.hh @@ -59,6 +59,7 @@ public: void setEDNSSubnet(const Netmask& subnet, uint8_t mask=128); void setBytes(size_t bytes); void setTime(time_t sec, uint32_t usec); + void updateTime(); void setQueryTime(time_t sec, uint32_t usec); void setResponseCode(uint8_t rcode); void addRRsFromPacket(const char* packet, const size_t len, bool includeCNAME=false);