From 984bfb00bbc3e5895cbc62a26512462a08f3f239 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 14 Nov 2018 16:15:07 +0000 Subject: [PATCH 01/13] Initial work on REGEX blacklist on message body --- DAPNETGateway.cpp | 15 ++++++++++++++- DAPNETGateway.h | 3 +++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index bf520b2..6d483a9 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -22,6 +22,7 @@ #include "Thread.h" #include "Timer.h" #include "Log.h" +#include #if defined(_WIN32) || defined(_WIN64) #include @@ -108,7 +109,8 @@ m_schedule(NULL), m_allSlots(false), m_currentSlot(0U), m_sentCodewords(0U), -m_mmdvmFree(false) +m_mmdvmFree(false), +m_regex() { } @@ -300,6 +302,17 @@ 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(); + m_regex.push_back(std::regex("^E.*")); + //If the regex matches the message body, don't send the message + 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 (ret) + found = false; + LogDebug("blacklist Regex match, not queueing message"); + } + } if (found) { switch (message->m_functional) { diff --git a/DAPNETGateway.h b/DAPNETGateway.h index f5c338b..e4a87dd 100644 --- a/DAPNETGateway.h +++ b/DAPNETGateway.h @@ -28,6 +28,7 @@ #include #include #include +#include class CDAPNETGateway { @@ -56,6 +57,8 @@ private: unsigned int calculateCodewords(const CPOCSAGMessage* message) const; void loadSchedule(); bool sendMessage(CPOCSAGMessage* message) const; + + std::vector m_regex; }; #endif From 9a0b79b30113e95e7e0b9788407cc97512bbe828 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 14 Nov 2018 16:26:57 +0000 Subject: [PATCH 02/13] Further work on REGEX --- DAPNETGateway.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 6d483a9..a2b4efa 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -303,11 +303,12 @@ int CDAPNETGateway::run() if (!whiteList.empty()) found = std::find(whiteList.begin(), whiteList.end(), message->m_ric) != whiteList.end(); m_regex.push_back(std::regex("^E.*")); - //If the regex matches the message body, don't send the message + //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) found = false; LogDebug("blacklist Regex match, not queueing message"); From 373526223d8538519a8a7cc23c0eb3a1fce55e0f Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 15 Nov 2018 11:23:46 +0000 Subject: [PATCH 03/13] Increase verbosity of REGEX logging --- DAPNETGateway.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index a2b4efa..6a0509c 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -311,7 +311,7 @@ int CDAPNETGateway::run() //If the regex matches the message body, don't send the message if (ret) found = false; - LogDebug("blacklist Regex match, not queueing message"); + 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);); } } From 70b754620d61d4bfee30061e0ad162b4e4908f64 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 15 Nov 2018 11:38:22 +0000 Subject: [PATCH 04/13] typo --- DAPNETGateway.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 6a0509c..b8932a5 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -311,7 +311,7 @@ int CDAPNETGateway::run() //If the regex matches the message body, don't send the message if (ret) found = false; - 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, message->m_message); } } From 4835852190b9ee4491ba3b63892290fad7f7b7a0 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 15 Nov 2018 11:51:11 +0000 Subject: [PATCH 05/13] REGEX logic --- DAPNETGateway.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index b8932a5..a7ef045 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -302,7 +302,8 @@ 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(); - m_regex.push_back(std::regex("^E.*")); + if (m_regex.empty()); + m_regex.push_back(std::regex("^E.*")); //If we have a list of blacklist REGEXes, use them if (!m_regex.empty()) { std::string messageBody(reinterpret_cast(message->m_message)); From 09bd988f93705f45212c9bc3c5797a0bf8af71eb Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 15 Nov 2018 11:55:49 +0000 Subject: [PATCH 06/13] REGEX logic --- DAPNETGateway.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index a7ef045..114f21a 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -298,6 +298,7 @@ int CDAPNETGateway::run() CPOCSAGMessage* message = m_dapnetNetwork->readMessage(); if (message != NULL) { bool found = true; + bool regexmatch = false; // If we have a white list of RICs, use it. if (!whiteList.empty()) @@ -311,12 +312,12 @@ int CDAPNETGateway::run() bool ret = std::regex_match(messageBody,regex); //If the regex matches the message body, don't send the message if (ret) - found = false; + regexmatch = false; 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); } } - if (found) { + if (found && !regexmatch) { switch (message->m_functional) { case FUNCTIONAL_ALPHANUMERIC: LogDebug("Queueing message to %07u, type %u, func Alphanumeric: \"%.*s\"", message->m_ric, message->m_type, message->m_length, message->m_message); From d233580d9b9bc9bb72e7ab16676a11922eebf1f1 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 15 Nov 2018 12:02:52 +0000 Subject: [PATCH 07/13] typo --- DAPNETGateway.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 114f21a..ed13c3e 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -303,7 +303,7 @@ 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.*")); //If we have a list of blacklist REGEXes, use them if (!m_regex.empty()) { From ba2a92e8e2cdca1be102d84573dc1b18a3860e1e Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 15 Nov 2018 13:21:57 +0000 Subject: [PATCH 08/13] more REGEX logic --- DAPNETGateway.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index ed13c3e..c1332fc 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -304,7 +304,7 @@ int CDAPNETGateway::run() if (!whiteList.empty()) found = std::find(whiteList.begin(), whiteList.end(), message->m_ric) != whiteList.end(); if (m_regex.empty()) - m_regex.push_back(std::regex("^E.*")); + m_regex.push_back(std::regex("^E.*$")); //If we have a list of blacklist REGEXes, use them if (!m_regex.empty()) { std::string messageBody(reinterpret_cast(message->m_message)); @@ -312,7 +312,7 @@ int CDAPNETGateway::run() bool ret = std::regex_match(messageBody,regex); //If the regex matches the message body, don't send the message if (ret) - regexmatch = false; + 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); } } From a3db88cc4cb9e3113012633ed8d1c38c679d61f4 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2018 11:25:45 +0000 Subject: [PATCH 09/13] Load REGEXes from file --- DAPNETGateway.cpp | 14 +++++++-- DAPNETGateway.h | 2 ++ Makefile | 2 +- REGEXes.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++ REGEXes.h | 41 +++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 REGEXes.cpp create mode 100644 REGEXes.h 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 From 4c47986da000d14e877ef6b11bbcb70c8eade342 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2018 14:57:29 +0000 Subject: [PATCH 10/13] Implement whitelist REGEX match --- DAPNETGateway.cpp | 49 ++++++++++++++++++++++++++++------------ DAPNETGateway.h | 8 ++++--- Makefile | 2 +- REGEXes.cpp => REGEX.cpp | 7 +----- REGEXes.h => REGEX.h | 0 5 files changed, 41 insertions(+), 25 deletions(-) rename REGEXes.cpp => REGEX.cpp (92%) rename REGEXes.h => REGEX.h (100%) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 75f677f..9eb9772 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -22,7 +22,7 @@ #include "Thread.h" #include "Timer.h" #include "Log.h" -#include "REGEXes.h" +#include "REGEX.h" #include #if defined(_WIN32) || defined(_WIN64) @@ -110,8 +110,9 @@ m_schedule(NULL), m_allSlots(false), m_currentSlot(0U), m_sentCodewords(0U), -m_mmdvmFree(false), -m_regex() +m_regexBlacklist(), +m_regexWhitelist(), +m_mmdvmFree(false) { } @@ -266,9 +267,17 @@ int CDAPNETGateway::run() } std::vector whiteList = m_conf.getWhiteList(); - m_regexBlacklist = new CREGEX("/tmp/regexes.txt"); + std::vector regexBlacklist; + std::vector regexWhitelist; + + m_regexBlacklist = new CREGEX("/tmp/blregexes.txt"); if (m_regexBlacklist->load()) - m_regex = m_regexBlacklist->get(); + regexBlacklist = m_regexBlacklist->get(); + + m_regexWhitelist = new CREGEX("/tmp/wlregexes.txt"); + if (m_regexWhitelist->load()) + regexWhitelist = m_regexWhitelist->get(); + for (;;) { @@ -303,29 +312,39 @@ int CDAPNETGateway::run() CPOCSAGMessage* message = m_dapnetNetwork->readMessage(); if (message != NULL) { bool found = true; - bool regexmatch = false; + bool blacklistregexmatch = false; + bool whitelistregexmatch = true; // 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()) { - m_regex.push_back(std::regex("^E.*$")); - m_regex.push_back(std::regex("^NAGIOS.*$")); - } + + std::string messageBody(reinterpret_cast(message->m_message)); //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) { + if (!regexBlacklist.empty()) { + for (std::regex regex : regexBlacklist) { bool ret = std::regex_match(messageBody,regex); //If the regex matches the message body, don't send the message if (ret) { - regexmatch = true; + blacklistregexmatch = true; 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()); } } } - if (found && !regexmatch) { + if(!regexWhitelist.empty() && !blacklistregexmatch) { + for (std::regex regex : regexWhitelist) { + bool ret = std::regex_match(messageBody,regex); + //If the regex does not match the message body, don't send the message + if (!ret) { + whitelistregexmatch = false; + LogDebug("No whitelist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str()); + } + } + + } + + if (found && !blacklistregexmatch && whitelistregexmatch) { switch (message->m_functional) { case FUNCTIONAL_ALPHANUMERIC: LogDebug("Queueing message to %07u, type %u, func Alphanumeric: \"%.*s\"", message->m_ric, message->m_type, message->m_length, message->m_message); diff --git a/DAPNETGateway.h b/DAPNETGateway.h index 99389be..3b29a3e 100644 --- a/DAPNETGateway.h +++ b/DAPNETGateway.h @@ -24,7 +24,7 @@ #include "POCSAGMessage.h" #include "StopWatch.h" #include "Conf.h" -#include "REGEXes.h" +#include "REGEX.h" #include #include @@ -43,13 +43,14 @@ private: CConf m_conf; CDAPNETNetwork* m_dapnetNetwork; CPOCSAGNetwork* m_pocsagNetwork; - CREGEX* m_regexBlacklist; std::deque m_queue; CStopWatch m_slotTimer; bool* m_schedule; bool m_allSlots; unsigned int m_currentSlot; unsigned int m_sentCodewords; + CREGEX* m_regexBlacklist; + CREGEX* m_regexWhitelist; bool m_mmdvmFree; @@ -60,7 +61,8 @@ private: void loadSchedule(); bool sendMessage(CPOCSAGMessage* message) const; - std::vector m_regex; +// std::vector m_regexBlacklist; +// std::vector m_regexWhitelist; }; #endif diff --git a/Makefile b/Makefile index 816e39d..c13b5da 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 REGEXes.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 REGEX.o all: DAPNETGateway diff --git a/REGEXes.cpp b/REGEX.cpp similarity index 92% rename from REGEXes.cpp rename to REGEX.cpp index 48531ed..e1b916f 100644 --- a/REGEXes.cpp +++ b/REGEX.cpp @@ -16,15 +16,11 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "REGEXes.h" +#include "REGEX.h" #include "Log.h" -#include -#include #include -#include #include -#include CREGEX::CREGEX(const std::string& regexFile) : m_regexFile(regexFile), @@ -43,7 +39,6 @@ bool CREGEX::load() continue; const char* regexStr = ::strtok(buffer, "\r\n"); - LogDebug("Process regex: %s",regexStr); if (regexStr != NULL) { try { diff --git a/REGEXes.h b/REGEX.h similarity index 100% rename from REGEXes.h rename to REGEX.h From 08157c342c13a9314d94d3e2d0f93148c0248cb5 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2018 16:37:11 +0000 Subject: [PATCH 11/13] Add config options for REGEX support. Also add README.REGEX --- Conf.cpp | 18 +++++++++++++++++- Conf.h | 4 ++++ DAPNETGateway.cpp | 16 ++++++++-------- DAPNETGateway.ini | 2 ++ README.REGEX | 21 +++++++++++++++++++++ REGEX.cpp | 2 +- 6 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 README.REGEX diff --git a/Conf.cpp b/Conf.cpp index 1ea9f82..d532d5c 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -37,6 +37,8 @@ CConf::CConf(const std::string& file) : m_file(file), m_callsign(), m_whiteList(), +m_blacklistRegexfile(), +m_whitelistRegexfile(), m_rptAddress(), m_rptPort(0U), m_myAddress(), @@ -105,7 +107,11 @@ bool CConf::read() m_whiteList.push_back(ric); p = ::strtok(NULL, ",\r\n"); } - } else if (::strcmp(key, "RptAddress") == 0) + } else if (::strcmp(key,"BlacklistRegexfile") == 0) + m_blacklistRegexfile = value; + else if (::strcmp(key,"WhitelistRegexfile") == 0) + m_whitelistRegexfile = value; + else if (::strcmp(key, "RptAddress") == 0) m_rptAddress = value; else if (::strcmp(key, "RptPort") == 0) m_rptPort = (unsigned int)::atoi(value); @@ -155,6 +161,16 @@ std::vector CConf::getWhiteList() const return m_whiteList; } +std::string CConf::getblacklistRegexfile() const +{ + return m_blacklistRegexfile; +} + +std::string CConf::getwhitelistRegexfile() const +{ + return m_whitelistRegexfile; +} + std::string CConf::getRptAddress() const { return m_rptAddress; diff --git a/Conf.h b/Conf.h index 4a8378a..bd3be30 100644 --- a/Conf.h +++ b/Conf.h @@ -33,6 +33,8 @@ public: // The General section std::string getCallsign() const; std::vector getWhiteList() const; + std::string getblacklistRegexfile() const; + std::string getwhitelistRegexfile() const; std::string getRptAddress() const; unsigned int getRptPort() const; std::string getMyAddress() const; @@ -56,6 +58,8 @@ private: std::string m_callsign; std::vector m_whiteList; + std::string m_blacklistRegexfile; + std::string m_whitelistRegexfile; std::string m_rptAddress; unsigned int m_rptPort; std::string m_myAddress; diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 9eb9772..60b3745 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -270,11 +270,11 @@ int CDAPNETGateway::run() std::vector regexBlacklist; std::vector regexWhitelist; - m_regexBlacklist = new CREGEX("/tmp/blregexes.txt"); + m_regexBlacklist = new CREGEX(m_conf.getblacklistRegexfile()); if (m_regexBlacklist->load()) regexBlacklist = m_regexBlacklist->get(); - m_regexWhitelist = new CREGEX("/tmp/wlregexes.txt"); + m_regexWhitelist = new CREGEX(m_conf.getwhitelistRegexfile()); if (m_regexWhitelist->load()) regexWhitelist = m_regexWhitelist->get(); @@ -312,8 +312,8 @@ int CDAPNETGateway::run() CPOCSAGMessage* message = m_dapnetNetwork->readMessage(); if (message != NULL) { bool found = true; - bool blacklistregexmatch = false; - bool whitelistregexmatch = true; + bool blacklistRegexmatch = false; + bool whitelistRegexmatch = true; // If we have a white list of RICs, use it. if (!whiteList.empty()) @@ -326,25 +326,25 @@ int CDAPNETGateway::run() bool ret = std::regex_match(messageBody,regex); //If the regex matches the message body, don't send the message if (ret) { - blacklistregexmatch = true; + blacklistRegexmatch = true; 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()); } } } - if(!regexWhitelist.empty() && !blacklistregexmatch) { + if(!regexWhitelist.empty() && !blacklistRegexmatch) { for (std::regex regex : regexWhitelist) { bool ret = std::regex_match(messageBody,regex); //If the regex does not match the message body, don't send the message if (!ret) { - whitelistregexmatch = false; + whitelistRegexmatch = false; LogDebug("No whitelist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str()); } } } - if (found && !blacklistregexmatch && whitelistregexmatch) { + if (found && !blacklistRegexmatch && whitelistRegexmatch) { switch (message->m_functional) { case FUNCTIONAL_ALPHANUMERIC: LogDebug("Queueing message to %07u, type %u, func Alphanumeric: \"%.*s\"", message->m_ric, message->m_type, message->m_length, message->m_message); diff --git a/DAPNETGateway.ini b/DAPNETGateway.ini index 104fddf..fe9abba 100644 --- a/DAPNETGateway.ini +++ b/DAPNETGateway.ini @@ -1,6 +1,8 @@ [General] Callsign=g9bf # WhiteList=12345,78901 +#BlacklistRegexfile=/tmp/blregexes.txt +#WhitelistRegexfile=/tmp/wlregexes.txt RptAddress=127.0.0.1 RptPort=3800 LocalAddress=127.0.0.1 diff --git a/README.REGEX b/README.REGEX new file mode 100644 index 0000000..9bce991 --- /dev/null +++ b/README.REGEX @@ -0,0 +1,21 @@ +The REGEX functionality allows you to whitelist or blacklist messages based on +pattern matching the message body itself (not the RIC). + +The options for this are: + + +WhitelistRegexfile= +BlacklistRegexfile= + +REGEXs are placed in the the respective textfile, one per line. These need to +be a full match, i.e. they need to match the entire message, begining with ^ +and ending with $. + +For example: + +^NAGIOS.*$ - matches all Nagios alerts + +^(G|M|2).+$ - Matches messages with the first character as a G.M or 2 - i.e. +match all UK callsigns. + + diff --git a/REGEX.cpp b/REGEX.cpp index e1b916f..5605b57 100644 --- a/REGEX.cpp +++ b/REGEX.cpp @@ -57,7 +57,7 @@ bool CREGEX::load() } size_t size = m_regex.size(); - LogInfo("Loaded %u REGEXes", size); + LogInfo("Loaded %u REGEX from file %s", size, regexFile.c_str()); size = m_regex.size(); if (size == 0U) From 54f3cd72817bdadfbf3e7298ee9c60d1bed5cfc3 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2018 16:56:46 +0000 Subject: [PATCH 12/13] Header --- REGEX.cpp | 2 ++ REGEX.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/REGEX.cpp b/REGEX.cpp index 5605b57..5ad3f8b 100644 --- a/REGEX.cpp +++ b/REGEX.cpp @@ -1,6 +1,8 @@ /* * Copyright (C) 2016,2017 by Jonathan Naylor G4KLX * +* Contributed by Simon G7RZU +* * 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 diff --git a/REGEX.h b/REGEX.h index 1ab6c14..a1888bb 100644 --- a/REGEX.h +++ b/REGEX.h @@ -1,6 +1,8 @@ /* * Copyright (C) 2016,2017 by Jonathan Naylor G4KLX * +* Contributed by Simon G7RZU +* * 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 From 886f682a03725ddd871577a2792c3c29548ab2ce Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2018 18:37:00 +0000 Subject: [PATCH 13/13] Typo --- REGEX.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REGEX.cpp b/REGEX.cpp index 5ad3f8b..9653f27 100644 --- a/REGEX.cpp +++ b/REGEX.cpp @@ -59,7 +59,7 @@ bool CREGEX::load() } size_t size = m_regex.size(); - LogInfo("Loaded %u REGEX from file %s", size, regexFile.c_str()); + LogInfo("Loaded %u REGEX from file %s", size, m_regexFile.c_str()); size = m_regex.size(); if (size == 0U)