]> granicus.if.org Git - pdns/commitdiff
dnsdist: Don't connect to remote logger in client/command mode
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Sep 2019 08:52:50 +0000 (10:52 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Sep 2019 08:52:50 +0000 (10:52 +0200)
pdns/dnsdist-lua-actions.cc
pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc

index 3f7c3a638a4e33dbfba70d70e3bfec778a22d8c4..4b9499340862ab18c2b961b5c0f81fc323427fae 100644 (file)
@@ -1330,11 +1330,13 @@ void setupLuaActions()
     });
 
   g_lua.writeFunction("RemoteLogAction", [](std::shared_ptr<RemoteLoggerInterface> logger, boost::optional<std::function<void(DNSQuestion*, DNSDistProtoBufMessage*)> > alterFunc, boost::optional<std::unordered_map<std::string, std::string>> vars) {
-      // avoids potentially-evaluated-expression warning with clang.
-      RemoteLoggerInterface& rl = *logger.get();
-      if (typeid(rl) != typeid(RemoteLogger)) {
-        // We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
-        throw std::runtime_error(std::string("RemoteLogAction only takes RemoteLogger. For other types, please look at DnstapLogAction."));
+      if (logger) {
+        // avoids potentially-evaluated-expression warning with clang.
+        RemoteLoggerInterface& rl = *logger.get();
+        if (typeid(rl) != typeid(RemoteLogger)) {
+          // We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
+          throw std::runtime_error(std::string("RemoteLogAction only takes RemoteLogger. For other types, please look at DnstapLogAction."));
+        }
       }
 
       std::string serverID;
@@ -1356,11 +1358,13 @@ void setupLuaActions()
     });
 
   g_lua.writeFunction("RemoteLogResponseAction", [](std::shared_ptr<RemoteLoggerInterface> logger, boost::optional<std::function<void(DNSResponse*, DNSDistProtoBufMessage*)> > alterFunc, boost::optional<bool> includeCNAME, boost::optional<std::unordered_map<std::string, std::string>> vars) {
-      // avoids potentially-evaluated-expression warning with clang.
-      RemoteLoggerInterface& rl = *logger.get();
-      if (typeid(rl) != typeid(RemoteLogger)) {
-        // We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
-        throw std::runtime_error("RemoteLogResponseAction only takes RemoteLogger. For other types, please look at DnstapLogResponseAction.");
+      if (logger) {
+        // avoids potentially-evaluated-expression warning with clang.
+        RemoteLoggerInterface& rl = *logger.get();
+        if (typeid(rl) != typeid(RemoteLogger)) {
+          // We could let the user do what he wants, but wrapping PowerDNS Protobuf inside a FrameStream tagged as dnstap is logically wrong.
+          throw std::runtime_error("RemoteLogResponseAction only takes RemoteLogger. For other types, please look at DnstapLogResponseAction.");
+        }
       }
 
       std::string serverID;
index ebc165fef12c3c5f7ffc206bfb268e101c49ebe0..876c8124ddf715a5e0e8c987c8be2ed11b1ce842 100644 (file)
@@ -95,11 +95,17 @@ void setupLuaBindingsProtoBuf(bool client)
 
   /* RemoteLogger */
   g_lua.writeFunction("newRemoteLogger", [client](const std::string& remote, boost::optional<uint16_t> timeout, boost::optional<uint64_t> maxQueuedEntries, boost::optional<uint8_t> reconnectWaitTime) {
+      if (client) {
+        return std::shared_ptr<RemoteLoggerInterface>(nullptr);
+      }
       return std::shared_ptr<RemoteLoggerInterface>(new RemoteLogger(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? (*maxQueuedEntries*100) : 10000, reconnectWaitTime ? *reconnectWaitTime : 1, client));
     });
 
   g_lua.writeFunction("newFrameStreamUnixLogger", [client](const std::string& address) {
 #ifdef HAVE_FSTRM
+      if (client) {
+        return std::shared_ptr<RemoteLoggerInterface>(nullptr);
+      }
       return std::shared_ptr<RemoteLoggerInterface>(new FrameStreamLogger(AF_UNIX, address, !client));
 #else
       throw std::runtime_error("fstrm support is required to build an AF_UNIX FrameStreamLogger");
@@ -108,11 +114,19 @@ void setupLuaBindingsProtoBuf(bool client)
 
   g_lua.writeFunction("newFrameStreamTcpLogger", [client](const std::string& address) {
 #if defined(HAVE_FSTRM) && defined(HAVE_FSTRM_TCP_WRITER_INIT)
+      if (client) {
+        return std::shared_ptr<RemoteLoggerInterface>(nullptr);
+      }
       return std::shared_ptr<RemoteLoggerInterface>(new FrameStreamLogger(AF_INET, address, !client));
 #else
       throw std::runtime_error("fstrm with TCP support is required to build an AF_INET FrameStreamLogger");
 #endif /* HAVE_FSTRM */
     });
 
-  g_lua.registerFunction("toString", &RemoteLoggerInterface::toString);
+  g_lua.registerFunction<std::string(std::shared_ptr<RemoteLoggerInterface>::*)()>("toString", [](const std::shared_ptr<RemoteLoggerInterface>& logger) {
+      if (logger) {
+        return logger->toString();
+      }
+      return std::string();
+  });
 }