diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index c1332fc..75f677f 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -22,6 +22,7 @@ #include "Thread.h" #include "Timer.h" #include "Log.h" +#include "REGEXes.h" #include #if defined(_WIN32) || defined(_WIN64) @@ -265,6 +266,10 @@ int CDAPNETGateway::run() } std::vector whiteList = m_conf.getWhiteList(); + m_regexBlacklist = new CREGEX("/tmp/regexes.txt"); + if (m_regexBlacklist->load()) + m_regex = m_regexBlacklist->get(); + for (;;) { unsigned char buffer[200U]; @@ -303,17 +308,20 @@ int CDAPNETGateway::run() // If we have a white list of RICs, use it. if (!whiteList.empty()) found = std::find(whiteList.begin(), whiteList.end(), message->m_ric) != whiteList.end(); - if (m_regex.empty()) + if (m_regex.empty()) { m_regex.push_back(std::regex("^E.*$")); + m_regex.push_back(std::regex("^NAGIOS.*$")); + } //If we have a list of blacklist REGEXes, use them if (!m_regex.empty()) { std::string messageBody(reinterpret_cast(message->m_message)); for (std::regex regex : m_regex) { bool ret = std::regex_match(messageBody,regex); //If the regex matches the message body, don't send the message - if (ret) + if (ret) { regexmatch = true; - LogDebug("Blacklist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, message->m_message); + LogDebug("Blacklist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str()); + } } } diff --git a/DAPNETGateway.h b/DAPNETGateway.h index e4a87dd..99389be 100644 --- a/DAPNETGateway.h +++ b/DAPNETGateway.h @@ -24,6 +24,7 @@ #include "POCSAGMessage.h" #include "StopWatch.h" #include "Conf.h" +#include "REGEXes.h" #include #include @@ -42,6 +43,7 @@ private: CConf m_conf; CDAPNETNetwork* m_dapnetNetwork; CPOCSAGNetwork* m_pocsagNetwork; + CREGEX* m_regexBlacklist; std::deque m_queue; CStopWatch m_slotTimer; bool* m_schedule; diff --git a/Makefile b/Makefile index c29d512..816e39d 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS = -g -O3 -Wall -std=c++0x -pthread LIBS = -lm -lpthread LDFLAGS = -g -OBJECTS = Conf.o DAPNETGateway.o DAPNETNetwork.o Log.o POCSAGMessage.o POCSAGNetwork.o StopWatch.o TCPSocket.o Thread.o Timer.o UDPSocket.o Utils.o +OBJECTS = Conf.o DAPNETGateway.o DAPNETNetwork.o Log.o POCSAGMessage.o POCSAGNetwork.o StopWatch.o TCPSocket.o Thread.o Timer.o UDPSocket.o Utils.o REGEXes.o all: DAPNETGateway diff --git a/REGEXes.cpp b/REGEXes.cpp new file mode 100644 index 0000000..48531ed --- /dev/null +++ b/REGEXes.cpp @@ -0,0 +1,77 @@ +/* +* Copyright (C) 2016,2017 by Jonathan Naylor G4KLX +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* 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 +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "REGEXes.h" +#include "Log.h" + +#include +#include +#include +#include +#include +#include + +CREGEX::CREGEX(const std::string& regexFile) : +m_regexFile(regexFile), +m_regex() +{ +} + +bool CREGEX::load() +{ + FILE* fp = ::fopen(m_regexFile.c_str(), "rt"); + if (fp != NULL) { + char buffer[100U]; + std::regex regex; + while (::fgets(buffer, 100U, fp) != NULL) { + if (buffer[0U] == '#') + continue; + + const char* regexStr = ::strtok(buffer, "\r\n"); + LogDebug("Process regex: %s",regexStr); + + if (regexStr != NULL) { + try { + regex = std::regex(regexStr); + } + + catch(const std::regex_error& e) { + LogDebug("error in regex %s (%s), skipping",regexStr,e.what()); + continue; + } + m_regex.push_back(regex); + } + } + + ::fclose(fp); + } + + size_t size = m_regex.size(); + LogInfo("Loaded %u REGEXes", size); + + size = m_regex.size(); + if (size == 0U) + return false; + + return true; +} + +std::vector CREGEX::get() +{ + return(m_regex); +} diff --git a/REGEXes.h b/REGEXes.h new file mode 100644 index 0000000..1ab6c14 --- /dev/null +++ b/REGEXes.h @@ -0,0 +1,41 @@ +/* +* Copyright (C) 2016,2017 by Jonathan Naylor G4KLX +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* 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 +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#if !defined(REGEX_H) +#define REGEX_H + + +#include +#include +#include + +class CREGEX { +public: + + std::vector get(); + bool load(); + CREGEX(const std::string& regexFile); + ~CREGEX(); + +private: + + std::string m_regexFile; + std::vector m_regex; +}; + +#endif