]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Matching logic
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 20 Oct 2025 15:02:33 +0000 (17:02 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 10 Nov 2025 14:20:43 +0000 (15:20 +0100)
Signed-off-by: Otto Moerbeek <otto.moerbeek@open-xchange.com>
pdns/protozero-trace.cc
pdns/protozero-trace.hh
pdns/recursordist/pdns_recursor.cc
pdns/recursordist/rec-lua-conf.hh
pdns/recursordist/rec-main.cc
pdns/recursordist/rec-rust-lib/cxxsupport.cc

index e86f6da5e04026bee434545971104e8c3509a861..66a524ff8580b3c721500b656a498d51e3fb39c1 100644 (file)
@@ -553,18 +553,13 @@ KeyValue KeyValue::decode(protozero::pbf_reader& reader)
   return value;
 }
 
-void extractOTraceIDs(const EDNSOptionViewMap& map, pdns::trace::InitialSpanInfo& span)
+bool extractOTraceIDs(const EDNSOptionViewMap& map, pdns::trace::InitialSpanInfo& span)
 {
-  // traceid gets set from edns options (if available and well-formed), otherwise random
+  // traceid gets set from edns options (if available and well-formed)
   // parent_span_id gets set from edns options (if available and well-formed, otherwise it remains cleared (no parent))
-  // span_id gets inited randomly
   auto traceidset = extractOTraceIDs(map, EDNSOptionCode::OTTRACEIDS, span.trace_id, span.parent_span_id);
 
-  if (!traceidset) {
-    span.trace_id.makeRandom();
-  }
-  // Empty parent span id indicated the client did not set one, thats fine
-  span.span_id.makeRandom();
+  return traceidset;
 }
 
 /**
index 23039fa7bbc4bea6b9de1443fd2c9de6285d9753..3735d0182620ca1e51fb0afdf44f0201d0c685d5 100644 (file)
@@ -818,7 +818,7 @@ private:
   const size_t size;
 };
 
-void extractOTraceIDs(const EDNSOptionViewMap& map, pdns::trace::InitialSpanInfo& span);
+bool extractOTraceIDs(const EDNSOptionViewMap& map, pdns::trace::InitialSpanInfo& span);
 bool extractOTraceIDs(const EDNSOptionViewMap& map, const EDNSOptionCode::EDNSOptionCodeEnum& eoc, pdns::trace::TraceID& traceID, pdns::trace::SpanID& spanID);
 
 } // namespace pdns::trace
index 69109303fca3cdb0b476b49ec27ec3e0767fb3ad..790cdd870363f992082ec886cb239e69e26a6909 100644 (file)
@@ -1870,7 +1870,7 @@ void startDoResolve(void* arg) // NOLINT(readability-function-cognitive-complexi
         string otData = otTrace.encode();
         pbMessage.setOpenTelemetryData(otData);
       }
-      // Currently only set if an OT trace is generated
+      // It can be set even if no OT Trace data was generated
       if (resolver.d_otTrace.trace_id != pdns::trace::s_emptyTraceID) {
         pbMessage.setOpenTelemetryTraceID(resolver.d_otTrace.trace_id);
       }
@@ -2144,6 +2144,40 @@ bool expectProxyProtocol(const ComboAddress& from, const ComboAddress& listenAdd
   return false;
 }
 
+static bool match(const std::unique_ptr<OpenTelemetryTraceConditions>& conditions, const ComboAddress& source, const DNSName& qname, QType qtype, uint16_t qid, bool edns_option_present)
+{
+  if (conditions == nullptr || conditions->size() == 0) {
+    cerr << "match 0 false" << endl;
+    return false;
+  }
+  if (auto const* match = conditions->lookup(source); match != nullptr) {
+    cerr << "match 1" << endl;
+    const auto& condition = match->second;
+    if (condition.d_traceid_only) {
+      cerr << "match 2 false" << endl;
+      return false;
+    }
+    if (condition.d_edns_option_required && !edns_option_present) {
+      cerr << "match 3 false" << endl;
+      return false;
+    }
+    if (condition.d_qid && condition.d_qid != qid) {
+    cerr << "match 4 false" << endl;
+      return false;
+    }
+    if (condition.d_qtypes && condition.d_qtypes->count(qtype) == 0) {
+    cerr << "match 5 false" << endl;
+      return false;
+    }
+    if (condition.d_qnames && !condition.d_qnames->check(qname)) {
+    cerr << "match 6 false" << endl;
+      return false;
+    }
+  }
+  cerr << "match return true" << endl;
+  return true;
+}
+
 // fromaddr: the address the query is coming from
 // destaddr: the address the query was received on
 // source: the address we assume the query is coming from, might be set by proxy protocol
@@ -2249,7 +2283,10 @@ static string* doProcessUDPQuestion(const std::string& question, const ComboAddr
         ecsParsed = true;
 
         if (SyncRes::eventTraceEnabled(SyncRes::event_trace_to_ot)) {
-          pdns::trace::extractOTraceIDs(ednsOptions, otTrace);
+          bool ednsFound = pdns::trace::extractOTraceIDs(ednsOptions, otTrace);
+          if (!match(t_OTConditions, source, qname, qtype, ntohs(headerdata->id), ednsFound)) {
+            eventTrace.setEnabled(false);
+          }
         }
 
         if (t_pdl) {
index d80f4468954a9496344469279ce49dfffd15b40d..b9b077cbf3af4a10b45d5ee78c8cf2987e6d5454 100644 (file)
@@ -100,8 +100,8 @@ using ProxyMapping = NetmaskTree<ProxyByTableValue, Netmask>;
 
 struct OpenTelemetryTraceCondition
 {
-  SuffixMatchTree<bool> d_qnames;
-  std::unordered_set<QType> d_qtypes;
+  std::optional<SuffixMatchNode> d_qnames;
+  std::optional<std::unordered_set<QType>> d_qtypes;
   std::optional<uint16_t> d_qid;
   bool d_edns_option_required{false};
   bool d_traceid_only{false};
index 75f9452c7576058b91be26988b30def628cb4542..349e5eac45fa08ef6e94de4bd65b3732e20c1650 100644 (file)
@@ -108,6 +108,7 @@ uint32_t g_disthashseed;
 bool g_useIncomingECS;
 static shared_ptr<NetmaskGroup> g_initialProxyProtocolACL;
 static shared_ptr<std::set<ComboAddress>> g_initialProxyProtocolExceptions;
+static shared_ptr<OpenTelemetryTraceConditions> g_initialOpenTelemetryConditions; // XXX shared ptr needed?
 boost::optional<ComboAddress> g_dns64Prefix{boost::none};
 DNSName g_dns64PrefixReverse;
 unsigned int g_maxChainLength;
@@ -2776,7 +2777,12 @@ static void recursorThread()
       else {
         t_proxyMapping = nullptr;
       }
-
+      if (g_OTConditions) {
+        t_OTConditions = make_unique<OpenTelemetryTraceConditions>(*g_OTConditions);
+      }
+      else {
+        t_OTConditions = nullptr;
+      }
       if (threadInfo.isHandler()) {
         if (!primeHints()) {
           threadInfo.setExitCode(EXIT_FAILURE);
index 6be5d2432e0887ec32755beff6aa9c3fa76b6897..ba9b0be8734d80526e96831c4550528f6f9ad6c3 100644 (file)
@@ -1338,11 +1338,17 @@ void fromRustToOTTraceConditions(const rust::Vec<pdns::rust::settings::rec::Open
 {
   for (const auto& setting : settings) {
     OpenTelemetryTraceCondition condition;
+    if (!setting.qnames.empty()) {
+      condition.d_qnames = SuffixMatchNode();
+    }
     for (const auto& qname : setting.qnames) {
-      condition.d_qnames.add(DNSName(std::string(qname)), true);
+      condition.d_qnames->add(DNSName(std::string(qname)));
+    }
+    if (!setting.qtypes.empty()) {
+      condition.d_qtypes = std::unordered_set<QType>();
     }
     for (const auto& qtype : setting.qtypes) {
-      condition.d_qtypes.insert(QType::chartocode(std::string(qtype).data()));
+      condition.d_qtypes->insert(QType::chartocode(std::string(qtype).data()));
     }
     if (setting.qid != std::numeric_limits<uint32_t>::max()) {
       condition.d_qid = setting.qid;