diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..420ebfb --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0.0) +project(rfmon-to-influx VERSION 0.1.0) + +include(CTest) +enable_testing() + +add_executable(rfmon-to-influx main.cpp) + +set(CPACK_PROJECT_NAME ${PROJECT_NAME}) +set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) +include(CPack) diff --git a/helper/exec.hpp b/helper/exec.hpp new file mode 100644 index 0000000..b3d122b --- /dev/null +++ b/helper/exec.hpp @@ -0,0 +1,31 @@ +#ifndef B89BC3C5_AD59_4765_AA06_8110111D316F +#define B89BC3C5_AD59_4765_AA06_8110111D316F + +#include +#include + +/// @brief Executes given command and optionally sends buffer to handler +/// @param cmd is the command +/// @param handler is the handler(char*)-function +/// @return Return-code form command +int exec(const char* cmd, const int *(handler)(char*) = nullptr){ + const int buf_size = 256; + char buf[buf_size]; + + // Open execution-pipe + FILE *pipe = popen(cmd, "r"); + + if (!pipe) { + throw std::runtime_error("popen() failed!"); + } + while (fgets(buf, buf_size, pipe) != nullptr) { + + // When a handler is specified, call it + if(handler != nullptr) (*handler)(buf); + } + + // Close pipe and read exit-code + return WEXITSTATUS(pclose(pipe)); +} + +#endif /* B89BC3C5_AD59_4765_AA06_8110111D316F */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..654a2bf --- /dev/null +++ b/main.cpp @@ -0,0 +1,26 @@ +#include +#include +#include "./helper/exec.hpp" + + +const std::string tcpdump_baseCmd = "tcpdump -vvv -e -n -X -s0 -i "; + +int main(int argc, char *args[]){ + + std::string tcpdump_cmd; + if(argc == 2){ + tcpdump_cmd = tcpdump_baseCmd + args[1]; + } else { + fprintf(stderr, "Missing interface\n"); + exit(1); + } + + int exitCode = exec(tcpdump_cmd.c_str()); + + if(exitCode){ + fprintf(stderr, "\ntcpdump exited with non-zero ExitCode: %d\n Something went wrong! Check tcpdump-output for more information.\n", exitCode); + exit(1); + } + + return 0; +}