From: Otto Moerbeek Date: Tue, 26 Feb 2019 15:30:50 +0000 (+0100) Subject: WIP code for dnstap of cache misses X-Git-Tag: dnsdist-1.4.0-beta1~13^2~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ebd679869d64d133ca12d9678d28a94bfb9c6e1b;p=thirdparty%2Fpdns.git WIP code for dnstap of cache misses --- diff --git a/pdns/dnstap.cc b/pdns/dnstap.cc index 65d9673676..488e621659 100644 --- a/pdns/dnstap.cc +++ b/pdns/dnstap.cc @@ -2,7 +2,7 @@ #include "gettime.hh" #include "dnstap.hh" -DnstapMessage::DnstapMessage(const std::string& identity, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, const char* packet, const size_t len, const struct timespec* queryTime, const struct timespec* responseTime) +DnstapMessage::DnstapMessage(const std::string& identity, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, const char* packet, const size_t len, const struct timespec* queryTime, const struct timespec* responseTime, bool recursor) { #ifdef HAVE_PROTOBUF const struct dnsheader* dh = reinterpret_cast(packet); @@ -13,34 +13,38 @@ DnstapMessage::DnstapMessage(const std::string& identity, const ComboAddress* re dnstap::Message* message = proto_message.mutable_message(); - message->set_type(!dh->qr ? dnstap::Message_Type_CLIENT_QUERY : dnstap::Message_Type_CLIENT_RESPONSE); - - message->set_socket_family(requestor->sin4.sin_family == AF_INET ? dnstap::INET : dnstap::INET6); - message->set_socket_protocol(isTCP ? dnstap::TCP : dnstap::UDP); - - if (requestor->sin4.sin_family == AF_INET) { - message->set_query_address(&requestor->sin4.sin_addr.s_addr, sizeof(requestor->sin4.sin_addr.s_addr)); - } - else if (requestor->sin4.sin_family == AF_INET6) { - message->set_query_address(&requestor->sin6.sin6_addr.s6_addr, sizeof(requestor->sin6.sin6_addr.s6_addr)); + if (recursor) { + message->set_type(!dh->qr ? dnstap::Message_Type_RESOLVER_QUERY : dnstap::Message_Type_RESOLVER_RESPONSE); + } else { + message->set_type(!dh->qr ? dnstap::Message_Type_CLIENT_QUERY : dnstap::Message_Type_CLIENT_RESPONSE); } - message->set_query_port(ntohs(requestor->sin4.sin_port)); + message->set_socket_protocol(isTCP ? dnstap::TCP : dnstap::UDP); - if (requestor->sin4.sin_family == AF_INET) { - message->set_response_address(&responder->sin4.sin_addr.s_addr, sizeof(responder->sin4.sin_addr.s_addr)); + if (requestor != nullptr) { + message->set_socket_family(requestor->sin4.sin_family == AF_INET ? dnstap::INET : dnstap::INET6); + if (requestor->sin4.sin_family == AF_INET) { + message->set_query_address(&requestor->sin4.sin_addr.s_addr, sizeof(requestor->sin4.sin_addr.s_addr)); + } else if (requestor->sin4.sin_family == AF_INET6) { + message->set_query_address(&requestor->sin6.sin6_addr.s6_addr, sizeof(requestor->sin6.sin6_addr.s6_addr)); + } + message->set_query_port(ntohs(requestor->sin4.sin_port)); } - else if (requestor->sin4.sin_family == AF_INET6) { - message->set_response_address(&responder->sin6.sin6_addr.s6_addr, sizeof(responder->sin6.sin6_addr.s6_addr)); + if (responder != nullptr) { + message->set_socket_family(responder->sin4.sin_family == AF_INET ? dnstap::INET : dnstap::INET6); + if (responder->sin4.sin_family == AF_INET) { + message->set_response_address(&responder->sin4.sin_addr.s_addr, sizeof(responder->sin4.sin_addr.s_addr)); + } else if (responder->sin4.sin_family == AF_INET6) { + message->set_response_address(&responder->sin6.sin6_addr.s6_addr, sizeof(responder->sin6.sin6_addr.s6_addr)); + } + message->set_response_port(ntohs(responder->sin4.sin_port)); } - message->set_response_port(ntohs(responder->sin4.sin_port)); - if (queryTime != nullptr) { message->set_query_time_sec(queryTime->tv_sec); - message->set_query_time_nsec(queryTime->tv_nsec / 1000); + message->set_query_time_nsec(queryTime->tv_nsec); } if (responseTime != nullptr) { message->set_response_time_sec(responseTime->tv_sec); - message->set_response_time_nsec(responseTime->tv_nsec / 1000); + message->set_response_time_nsec(responseTime->tv_nsec); } if (!dh->qr) { diff --git a/pdns/dnstap.hh b/pdns/dnstap.hh index d00bd0bca8..59dd3c752f 100644 --- a/pdns/dnstap.hh +++ b/pdns/dnstap.hh @@ -37,7 +37,7 @@ class DnstapMessage { public: - DnstapMessage(const std::string& identity, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, const char* packet, const size_t len, const struct timespec* queryTime, const struct timespec* responseTime); + DnstapMessage(const std::string& identity, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, const char* packet, const size_t len, const struct timespec* queryTime, const struct timespec* responseTime, bool recursor = false); void serialize(std::string& data) const; std::string toDebugString() const; diff --git a/pdns/lwres.cc b/pdns/lwres.cc index 794838924a..0bbbf07325 100644 --- a/pdns/lwres.cc +++ b/pdns/lwres.cc @@ -52,52 +52,87 @@ #include "uuid-utils.hh" -static void logOutgoingQuery(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) +#ifdef HAVE_FSTRM +#include "dnstap.hh" +#include "fstrm_logger.hh" +bool g_syslog; + +static FrameStreamLogger *mylogger = new FrameStreamLogger(AF_INET, "127.0.0.1:9999", true); +#endif // HAVE_FSTRM + +static void logOutgoingQuery(const std::shared_ptr>>& outgoingLoggers, const struct timeval &queryTime, boost::optional initialRequestId, const boost::uuids::uuid& uuid, const ComboAddress& ip, const DNSName& domain, int type, uint16_t qid, bool doTCP, const vector& packet, boost::optional& srcmask) { if(!outgoingLoggers) return; + std::string str; - RecProtoBufMessage message(DNSProtoBufMessage::OutgoingQuery, uuid, nullptr, &ip, domain, type, QClass::IN, qid, doTCP, bytes); - message.setServerIdentity(SyncRes::s_serverID); + if (0) { + RecProtoBufMessage message(DNSProtoBufMessage::OutgoingQuery, uuid, nullptr, &ip, domain, type, QClass::IN, qid, + doTCP, packet.size()); + message.setServerIdentity(SyncRes::s_serverID); - if (initialRequestId) { - message.setInitialRequestID(*initialRequestId); - } + if (initialRequestId) { + message.setInitialRequestID(*initialRequestId); + } - if (srcmask) { - message.setEDNSSubnet(*srcmask); - } + if (srcmask) { + message.setEDNSSubnet(*srcmask); + } // cerr <queueData(str); + for (auto& logger : *outgoingLoggers) { + logger->queueData(str); + } + } +#ifdef HAVE_FSTRM + else { + struct timespec ts; + TIMEVAL_TO_TIMESPEC(&queryTime, &ts); + DnstapMessage message(SyncRes::s_serverID, nullptr, &ip, doTCP, (const char*)&*packet.begin(), packet.size(), &ts, nullptr, true); + message.serialize(str); + mylogger->queueData(str); } +#endif // HAVE_FSTRM } -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 initialRequestId, const boost::uuids::uuid& uuid, const ComboAddress& ip, const DNSName& domain, int type, uint16_t qid, bool doTCP, const std::string& packet, int rcode, const std::vector& records, const struct timeval& queryTime, const struct timeval& replyTime, const std::set& exportTypes) { if(!outgoingLoggers) 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); - -// cerr <queueData(str); + if (0) { + RecProtoBufMessage message(DNSProtoBufMessage::IncomingResponse, uuid, nullptr, &ip, domain, type, QClass::IN, qid, + doTCP, packet.size()); + 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); + +// cerr <queueData(str); + } } +#ifdef HAVE_FSTRM + else { + struct timespec ts1, ts2; + TIMEVAL_TO_TIMESPEC(&queryTime, &ts1); + TIMEVAL_TO_TIMESPEC(&replyTime, &ts2); + DnstapMessage message(SyncRes::s_serverID, nullptr, &ip, doTCP, (const char*)&*packet.begin(), packet.size(), &ts1, &ts2, true); + message.serialize(str); + mylogger->queueData(str); + } +#endif // HAVE_FSTRM + } #endif /* HAVE_PROTOBUF */ @@ -163,7 +198,7 @@ int asyncresolve(const ComboAddress& ip, const DNSName& domain, int type, bool d if (outgoingLoggers) { uuid = getUniqueID(); - logOutgoingQuery(outgoingLoggers, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, vpacket.size(), srcmask); + logOutgoingQuery(outgoingLoggers, queryTime, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, vpacket, srcmask); } #endif @@ -250,7 +285,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, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, buf, lwr->d_rcode, lwr->d_records, queryTime, *now, exportTypes); } #endif lwr->d_validpacket=true; @@ -296,7 +331,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, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, buf, lwr->d_rcode, lwr->d_records, queryTime, *now, exportTypes); } #endif lwr->d_validpacket=true; @@ -309,7 +344,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, context ? context->d_initialRequestId : boost::none, uuid, ip, domain, type, qid, doTCP, buf, lwr->d_rcode, lwr->d_records, queryTime, *now, exportTypes); } #endif lwr->d_validpacket=false; diff --git a/pdns/recursordist/Makefile.am b/pdns/recursordist/Makefile.am index 191abb19f3..14eb71d8b4 100644 --- a/pdns/recursordist/Makefile.am +++ b/pdns/recursordist/Makefile.am @@ -18,6 +18,11 @@ AM_CXXFLAGS += \ -DNODCACHEDIR=\"$(nodcachedir)\" endif +if FSTRM +AM_CPPFLAGS += \ + $(FSTRM_CFLAGS) +endif + AM_LDFLAGS = \ $(PROGRAM_LDFLAGS) \ $(THREADFLAGS) @@ -352,7 +357,7 @@ if HAVE_PROTOC dnsmessage.pb.cc: dnsmessage.proto $(AM_V_GEN)$(PROTOC) --cpp_out=./ $< -if DNSTAP_ENABLED +if FSTRM dnstap.pb.cc: dnstap.proto $(AM_V_GEN)$(PROTOC) -I$(srcdir) --cpp_out=./ $< endif @@ -361,12 +366,13 @@ endif BUILT_SOURCES += dnsmessage.pb.cc pdns_recursor_LDADD += $(PROTOBUF_LIBS) nodist_pdns_recursor_SOURCES = dnsmessage.pb.cc dnsmessage.pb.h -pdns_recursor.$(OBJEXT): dnsmessage.pb.cc -if DNSTAP_ENABLED +if FSTRM BUILT_SOURCES += dnsmessage.pb.cc nodist_pdns_recursor_SOURCES += dnstap.pb.cc dnstap.pb.h -pdns_recursor.$(OBJEXT): dnstap.pb.cc +pdns_recursor.$(OBJEXT): dnstap.pb.cc dnsmessage.pb.cc +else +pdns_recursor.$(OBJEXT): dnsmessage.pb.cc endif nodist_testrunner_SOURCES = dnsmessage.pb.cc dnsmessage.pb.h @@ -380,9 +386,12 @@ pdns_recursor_SOURCES += \ endif -if DNSTAP_ENABLED +if FSTRM pdns_recursor_SOURCES += \ - dnstap.cc dnstap.hh + dnstap.cc dnstap.hh fstrm_logger.cc fstrm_logger.hh dolog.hh + +pdns_recursor_LDADD += \ + $(FSTRM_LIBS) endif rec_control_SOURCES = \ diff --git a/pdns/recursordist/configure.ac b/pdns/recursordist/configure.ac index 3d75936d3e..2e75672bb0 100644 --- a/pdns/recursordist/configure.ac +++ b/pdns/recursordist/configure.ac @@ -155,7 +155,7 @@ AC_ARG_WITH([nod-cache-dir], [nodcachedir="$withval"] ) -PDNS_ENABLE_DNSTAP +PDNS_CHECK_DNSTAP AC_MSG_CHECKING([whether we will enable compiler security checks]) AC_ARG_ENABLE([hardening], @@ -253,7 +253,7 @@ AM_COND_IF([NOD_ENABLED], [AC_MSG_NOTICE([nod: yes])], [AC_MSG_NOTICE([nod: no])] ) -AM_COND_IF([DNSTAP_ENABLED], +AM_COND_IF([FSTRM], [AC_MSG_NOTICE([dnstap: yes])], [AC_MSG_NOTICE([dnstap: no])] ) diff --git a/pdns/recursordist/dolog.hh b/pdns/recursordist/dolog.hh new file mode 120000 index 0000000000..e458b07d27 --- /dev/null +++ b/pdns/recursordist/dolog.hh @@ -0,0 +1 @@ +../dolog.hh \ No newline at end of file diff --git a/pdns/recursordist/fstrm_logger.cc b/pdns/recursordist/fstrm_logger.cc new file mode 120000 index 0000000000..e66c9cc6f5 --- /dev/null +++ b/pdns/recursordist/fstrm_logger.cc @@ -0,0 +1 @@ +../fstrm_logger.cc \ No newline at end of file diff --git a/pdns/recursordist/fstrm_logger.hh b/pdns/recursordist/fstrm_logger.hh new file mode 120000 index 0000000000..b4898e9c1f --- /dev/null +++ b/pdns/recursordist/fstrm_logger.hh @@ -0,0 +1 @@ +../fstrm_logger.hh \ No newline at end of file diff --git a/pdns/recursordist/m4/pdns_check_dnstap.m4 b/pdns/recursordist/m4/pdns_check_dnstap.m4 new file mode 120000 index 0000000000..ed71845d97 --- /dev/null +++ b/pdns/recursordist/m4/pdns_check_dnstap.m4 @@ -0,0 +1 @@ +../../../m4/pdns_check_dnstap.m4 \ No newline at end of file diff --git a/pdns/recursordist/m4/pdns_enable_dnstap.m4 b/pdns/recursordist/m4/pdns_enable_dnstap.m4 deleted file mode 100644 index d20f468ce2..0000000000 --- a/pdns/recursordist/m4/pdns_enable_dnstap.m4 +++ /dev/null @@ -1,19 +0,0 @@ -AC_DEFUN([PDNS_ENABLE_DNSTAP],[ - AC_MSG_CHECKING([whether to enable logging of outgoing queries using dnstap]) - - AC_ARG_ENABLE([dnstap], - AS_HELP_STRING([--enable-dnstap], - [enable logging of outgoing queries using dnstap @<:@default=no@:>@] - ), - [enable_dnstap=$enableval], - [enable_dnstap=no] - ) - - AS_IF([test "x$enable_dnstap" != "xno"], - [AC_DEFINE([DNSTAP_ENABLED], [1], [Define to 1 if dnstap is enabled])] - ) - - AM_CONDITIONAL([DNSTAP_ENABLED], [test "x$enable_dnstap" != "xno"]) - - AC_MSG_RESULT([$enable_dnstap]) -])