]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
joel: piglet inspector instanitation fix
authorRuss Combs <rucombs@cisco.com>
Mon, 17 Aug 2015 16:56:38 +0000 (12:56 -0400)
committerRuss Combs <rucombs@cisco.com>
Mon, 17 Aug 2015 16:56:38 +0000 (12:56 -0400)
src/managers/inspector_manager.cc
src/managers/inspector_manager.h
src/piglet/piglet_api.h
src/piglet_plugins/pp_inspector.cc

index 8fccbfac9614773784f2c85d869707931ba7177b..36aaecdc5574b811e08f18b6df3664671006ae0d 100644 (file)
@@ -591,6 +591,30 @@ void InspectorManager::instantiate(
     }
 }
 
+#ifdef PIGLET
+// FIXIT-M duplicates logic in void InspectorManager::instantiate()
+
+Inspector* InspectorManager::instantiate(
+    const char* name, Module* mod, SnortConfig* sc)
+{
+    auto ppc = get_class(name, sc->framework_config);
+
+    if ( !ppc )
+        return nullptr;
+
+    auto fp = get_inspection_policy()->framework_policy;
+    auto ppi = get_new(ppc, fp, name, mod);
+
+    if ( !ppi )
+        return nullptr;
+
+    ppi->set_name(name);
+
+    // FIXIT-L can't we just unify PHInstance and InspectorWrapper?
+    return ppi->handler;
+}
+#endif
+
 // create default binding for wizard and configured services
 static void instantiate_binder(SnortConfig* sc, FrameworkPolicy* fp)
 {
@@ -786,22 +810,3 @@ void InspectorManager::clear(Packet* p)
     s_clear = false;
 }
 
-#ifdef PIGLET
-
-InspectorWrapper* InspectorManager::instantiate(const char* name, Module* m)
-{
-    auto api = ::get_plugin(name);
-    if ( !api || !api->ctor )
-        return nullptr;
-
-    auto p = api->ctor(m);
-    if ( !p )
-        return nullptr;
-
-    p->set_api(api);
-
-    return new InspectorWrapper(api, p);
-}
-
-#endif
-
index 27f473090d24041aaf4a547b17a18dccfefabe58..b78218c0bc57ce1f84c3d8bb04e631b734e68ea9 100644 (file)
@@ -43,23 +43,6 @@ struct InspectionPolicy;
 
 //-------------------------------------------------------------------------
 
-#ifdef PIGLET
-struct InspectorWrapper
-{
-    InspectorWrapper(const InspectApi* a, Inspector* p) :
-        api { a }, instance { p } { }
-
-    ~InspectorWrapper()
-    {
-        if ( api && instance && api->dtor )
-            api->dtor(instance);
-    }
-
-    const InspectApi* api;
-    Inspector* instance;
-};
-#endif
-
 class InspectorManager
 {
 public:
@@ -104,7 +87,7 @@ public:
     static void empty_trash();
 
 #ifdef PIGLET
-    static InspectorWrapper* instantiate(const char*, Module*);
+    static Inspector* instantiate(const char*, Module*, SnortConfig*);
 #endif
 
 private:
index 2fc13adaefb3b7b857f0b01b508200759b1a8149..053830bbdf08ae02c6a3bb51afbe9153204aba78 100644 (file)
 // Piglet plugin API
 
 #include <string>
+#include <utility>
 
 #include "framework/base_api.h"
+#include "log/messages.h"
 #include "lua/lua.h"
 #include "main/snort_types.h"
 
@@ -100,6 +102,15 @@ struct Api
     PluginDtor dtor;
     PlugType target;
 };
+
+template<typename... Args>
+static inline void error(std::string fmt, Args&&... args)
+{
+    fmt.insert(0, "piglet: ");
+    fmt.append("\n");
+    ErrorMessage(fmt.c_str(), std::forward<Args>(args)...);
+}
+
 } // namespace Piglet
 
 #endif
index 4dbfdf04cdc0e580a95ce400c3a9d5344344f498..95fd08712ab3c75ace043fe2e7b1f92ec45fd7fd 100644 (file)
@@ -22,6 +22,7 @@
 #include <string>
 #include <assert.h>
 
+#include "log/messages.h"
 #include "lua/lua_iface.h"
 #include "managers/inspector_manager.h"
 #include "piglet/piglet_api.h"
 class InspectorPiglet : public Piglet::BasePlugin
 {
 public:
-    InspectorPiglet(Lua::State&, std::string, Module*);
+    InspectorPiglet(Lua::State&, std::string, Module*, SnortConfig*);
     virtual ~InspectorPiglet() override;
     virtual bool setup() override;
 
 private:
-    InspectorWrapper* wrapper;
+    Inspector* instance;
 };
 
 InspectorPiglet::InspectorPiglet(
-    Lua::State& state, std::string target, Module* m) :
-    BasePlugin(state, target, m)
+    Lua::State& state, std::string target, Module* m, SnortConfig* sc) :
+    BasePlugin(state, target, m, sc)
 {
     FlushBucket::set(0);
 
     assert(module);
-    wrapper = InspectorManager::instantiate(target.c_str(), module);
-}
+    assert(snort_conf);
 
-InspectorPiglet::~InspectorPiglet()
-{
-    if ( wrapper )
-        delete wrapper;
+    instance = InspectorManager::instantiate(target.c_str(), module, snort_conf);
 }
 
+InspectorPiglet::~InspectorPiglet() { }
+
 bool InspectorPiglet::setup()
 {
-    if ( !wrapper )
+    if ( !instance )
+    {
+        Piglet::error("couldn't instantiate Inspector '%s'", target.c_str());
         return true;
+    }
 
     install(L, DecodeDataIface);
     install(L, RawBufferIface);
@@ -73,7 +75,7 @@ bool InspectorPiglet::setup()
     install(L, PacketIface);
     install(L, StreamSplitterIface);
 
-    install(L, InspectorIface, wrapper->instance);
+    install(L, InspectorIface, instance);
 
     return false;
 }
@@ -82,8 +84,8 @@ bool InspectorPiglet::setup()
 // API foo
 // -----------------------------------------------------------------------------
 static Piglet::BasePlugin* ctor(
-    Lua::State& state, std::string target, Module* m, SnortConfig*)
-{ return new InspectorPiglet(state, target, m); }
+    Lua::State& state, std::string target, Module* m, SnortConfig* sc)
+{ return new InspectorPiglet(state, target, m, sc); }
 
 static void dtor(Piglet::BasePlugin* p)
 { delete p; }