Skip to content

Commit

Permalink
plugins/ndpi: stub in dummy ndpi plugin
Browse files Browse the repository at this point in the history
This plugin stub shows how a plugin like nDPI might be use the flow
init and flow update callbacks to do its work. Also shows usage of
FlowStorage to avoid modifying the Flow struct directly.
  • Loading branch information
jasonish committed Oct 10, 2024
1 parent 2907164 commit fe62c1c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
14 changes: 14 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,19 @@ fi
])
AC_SUBST(RUST_FEATURES)

# nDPI support (no library checks for this stub)
AC_ARG_ENABLE(ndpi,
AS_HELP_STRING([--enable-ndpi], [Enable nDPI support]),
[enable_ndpi=$enableval],[enable_ndpi=no])
if test "x$enable_ndpi" = "xyes"; then
AM_CONDITIONAL([BUILD_NDPI], [true])
ndpi_comment=""
else
AM_CONDITIONAL([BUILD_NDPI], [false])
ndpi_comment="#"
fi
AC_SUBST([ndpi_comment])

AC_ARG_ENABLE(warnings,
AS_HELP_STRING([--enable-warnings], [Enable supported C compiler warnings]),[enable_warnings=$enableval],[enable_warnings=no])
AS_IF([test "x$enable_warnings" = "xyes"], [
Expand Down Expand Up @@ -2513,6 +2526,7 @@ AC_CONFIG_FILES(examples/plugins/ci-capture/Makefile)
AC_CONFIG_FILES(examples/lib/simple/Makefile examples/lib/simple/Makefile.example)
AC_CONFIG_FILES(plugins/Makefile)
AC_CONFIG_FILES(plugins/pfring/Makefile)
AC_CONFIG_FILES(plugins/ndpi-dummy/Makefile)

AC_OUTPUT

Expand Down
4 changes: 4 additions & 0 deletions plugins/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ SUBDIRS =
if BUILD_PFRING
SUBDIRS += pfring
endif

if BUILD_NDPI
SUBDIRS += ndpi-dummy
endif
8 changes: 8 additions & 0 deletions plugins/ndpi-dummy/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pkglib_LTLIBRARIES = ndpi.la

ndpi_la_LDFLAGS = -module -avoid-version -shared

ndpi_la_SOURCES = ndpi.c

install-exec-hook:
cd $(DESTDIR)$(pkglibdir) && $(RM) $(pkglib_LTLIBRARIES)
84 changes: 84 additions & 0 deletions plugins/ndpi-dummy/ndpi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Copyright (C) 2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

/* License note: While this "glue" code to the nDPI library is GPLv2,
* nDPI is itself LGPLv3 which is known to be incompatible with the
* GPLv2. */

#include "suricata-common.h"
#include "suricata-plugin.h"
#include "util-debug.h"

#include "flow-callbacks.h"
#include "flow-storage.h"

static FlowStorageId flow_storage_id = { .id = -1 };

static void *FlowStorageAlloc(unsigned int size)
{
SCLogNotice("Allocating nDPI flow storage, size=%d", size);
return NULL;
}

static void FlowStorageFree(void *ptr)
{
SCLogNotice("De-allocating nDPI flow storage");
int *dummy_storage = ptr;
SCLogNotice("%d", *dummy_storage);
SCFree(ptr);
}

static void OnFlowInit(Flow *f, const Packet *p)
{
SCLogNotice("...");
static int counter = 0;
int *dummy_storage = SCCalloc(1, sizeof(int));
*dummy_storage = counter++;
FlowSetStorageById(f, flow_storage_id, dummy_storage);
}

static void OnFlowUpdate(Flow *f, Packet *p, ThreadVars *tv)
{
SCLogNotice("...");
int *dummy_storage = FlowGetStorageById(f, flow_storage_id);
SCLogNotice("dummy_storage=%d", *dummy_storage);
}

static void NdpiInit(void)
{
SCLogNotice("Initializing nDPI plugin");

flow_storage_id = FlowStorageRegister("ndpi", sizeof(void *), NULL, FlowStorageFree);
if (flow_storage_id.id < 0) {
FatalError("Failed to register nDPI flow storage");
}

SCFlowRegisterInitCallback(OnFlowInit);
SCFlowRegisterUpdateCallback(OnFlowUpdate);
}

const SCPlugin PluginRegistration = {
.name = "ndpi-dummy",
.author = "FirstName LastName",
.license = "GPLv2",
.Init = NdpiInit,
};

const SCPlugin *SCPluginRegister()
{
return &PluginRegistration;
}
1 change: 1 addition & 0 deletions suricata.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ stats:
# Plugins -- Experimental -- specify the filename for each plugin shared object
plugins:
@pfring_comment@- @prefix@/lib/@PACKAGE_NAME@/pfring.so
@ndpi_comment@- @prefix@/lib/@PACKAGE_NAME@/ndpi.so
# - /path/to/plugin.so

# Configure the type of alert (and other) logging you would like.
Expand Down

0 comments on commit fe62c1c

Please sign in to comment.