2018-06-06 20:33:31 +01:00
|
|
|
/*
|
2025-03-18 15:13:07 +00:00
|
|
|
* Copyright (C) 2018,2020,2023,2025 by Jonathan Naylor G4KLX
|
2018-06-06 20:33:31 +01:00
|
|
|
*
|
|
|
|
|
* 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 "Conf.h"
|
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
|
|
const int BUFFER_SIZE = 500;
|
|
|
|
|
|
2025-03-18 13:52:17 +00:00
|
|
|
enum class SECTION {
|
|
|
|
|
NONE,
|
|
|
|
|
GENERAL,
|
|
|
|
|
LOG,
|
2025-03-18 15:13:07 +00:00
|
|
|
MQTT,
|
2025-03-18 13:52:17 +00:00
|
|
|
DAPNET
|
2018-06-06 20:33:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CConf::CConf(const std::string& file) :
|
|
|
|
|
m_file(file),
|
|
|
|
|
m_callsign(),
|
2018-07-10 19:18:27 +01:00
|
|
|
m_whiteList(),
|
2018-11-17 16:37:11 +00:00
|
|
|
m_blacklistRegexfile(),
|
|
|
|
|
m_whitelistRegexfile(),
|
2018-06-06 20:33:31 +01:00
|
|
|
m_rptAddress(),
|
|
|
|
|
m_rptPort(0U),
|
|
|
|
|
m_myAddress(),
|
|
|
|
|
m_myPort(0U),
|
|
|
|
|
m_daemon(false),
|
|
|
|
|
m_logDisplayLevel(0U),
|
2023-07-07 17:57:36 +01:00
|
|
|
m_logMQTTLevel(0U),
|
|
|
|
|
m_mqttAddress("127.0.0.1"),
|
|
|
|
|
m_mqttPort(1883U),
|
|
|
|
|
m_mqttKeepalive(60U),
|
|
|
|
|
m_mqttName("dapnet-gateway"),
|
2018-06-06 20:33:31 +01:00
|
|
|
m_dapnetAddress(),
|
|
|
|
|
m_dapnetPort(0U),
|
|
|
|
|
m_dapnetAuthKey(),
|
|
|
|
|
m_dapnetDebug(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CConf::~CConf()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConf::read()
|
|
|
|
|
{
|
2018-11-18 23:35:06 +01:00
|
|
|
FILE* fp = ::fopen(m_file.c_str(), "rt");
|
2025-03-18 13:52:17 +00:00
|
|
|
if (fp == nullptr) {
|
2018-11-18 23:35:06 +01:00
|
|
|
::fprintf(stderr, "Couldn't open the .ini file - %s\n", m_file.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-18 13:52:17 +00:00
|
|
|
SECTION section = SECTION::NONE;
|
2018-11-18 23:35:06 +01:00
|
|
|
|
|
|
|
|
char buffer[BUFFER_SIZE];
|
2025-03-18 13:52:17 +00:00
|
|
|
while (::fgets(buffer, BUFFER_SIZE, fp) != nullptr) {
|
2018-11-18 23:35:06 +01:00
|
|
|
if (buffer[0U] == '#')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (buffer[0U] == '[') {
|
|
|
|
|
if (::strncmp(buffer, "[General]", 9U) == 0)
|
2025-03-18 13:52:17 +00:00
|
|
|
section = SECTION::GENERAL;
|
2018-11-18 23:35:06 +01:00
|
|
|
else if (::strncmp(buffer, "[Log]", 5U) == 0)
|
2025-03-18 13:52:17 +00:00
|
|
|
section = SECTION::LOG;
|
2023-07-07 17:57:36 +01:00
|
|
|
else if (::strncmp(buffer, "[MQTT]", 6U) == 0)
|
2025-03-18 15:13:07 +00:00
|
|
|
section = SECTION::MQTT;
|
2018-11-18 23:35:06 +01:00
|
|
|
else if (::strncmp(buffer, "[DAPNET]", 8U) == 0)
|
2025-03-18 13:52:17 +00:00
|
|
|
section = SECTION::DAPNET;
|
2018-11-18 23:35:06 +01:00
|
|
|
else
|
2025-03-18 13:52:17 +00:00
|
|
|
section = SECTION::NONE;
|
2018-11-18 23:35:06 +01:00
|
|
|
|
|
|
|
|
continue;
|
2018-08-28 22:56:36 +02:00
|
|
|
}
|
2018-11-18 23:35:06 +01:00
|
|
|
|
2020-09-06 14:20:00 +01:00
|
|
|
char* key = ::strtok(buffer, " \t=\r\n");
|
2025-03-18 13:52:17 +00:00
|
|
|
if (key == nullptr)
|
2020-09-06 14:20:00 +01:00
|
|
|
continue;
|
2018-11-18 23:35:06 +01:00
|
|
|
|
2025-03-18 13:52:17 +00:00
|
|
|
char* value = ::strtok(nullptr, "\r\n");
|
|
|
|
|
if (value == nullptr)
|
2020-09-06 14:20:00 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Remove quotes from the value
|
|
|
|
|
size_t len = ::strlen(value);
|
|
|
|
|
if (len > 1U && *value == '"' && value[len - 1U] == '"') {
|
|
|
|
|
value[len - 1U] = '\0';
|
|
|
|
|
value++;
|
|
|
|
|
} else {
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
// if value is not quoted, remove after # (to make comment)
|
2025-03-18 13:52:17 +00:00
|
|
|
if ((p = strchr(value, '#')) != nullptr)
|
2020-09-06 14:20:00 +01:00
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
|
|
// remove trailing tab/space
|
|
|
|
|
for (p = value + strlen(value) - 1U; p >= value && (*p == '\t' || *p == ' '); p--)
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-18 13:52:17 +00:00
|
|
|
if (section == SECTION::GENERAL) {
|
2018-11-18 23:35:06 +01:00
|
|
|
if (::strcmp(key, "Callsign") == 0) {
|
|
|
|
|
for (unsigned int i = 0U; value[i] != '\0'; i++) {
|
|
|
|
|
if (!::isspace(value[i]))
|
|
|
|
|
m_callsign.insert(m_callsign.end(), 1, value[i]);
|
|
|
|
|
}
|
2025-03-18 13:52:17 +00:00
|
|
|
} else if (::strcmp(key, "WhiteList") == 0) {
|
2018-11-18 23:35:06 +01:00
|
|
|
char* p = ::strtok(value, ",\r\n");
|
2025-03-18 13:52:17 +00:00
|
|
|
while (p != nullptr) {
|
2018-11-18 23:35:06 +01:00
|
|
|
unsigned int ric = (unsigned int)::atoi(p);
|
|
|
|
|
if (ric > 0U)
|
|
|
|
|
m_whiteList.push_back(ric);
|
2025-03-18 13:52:17 +00:00
|
|
|
p = ::strtok(nullptr, ",\r\n");
|
2018-11-18 23:35:06 +01:00
|
|
|
}
|
2025-03-18 13:52:17 +00:00
|
|
|
} else if (::strcmp(key, "BlackList") == 0) {
|
2018-12-10 20:59:40 +01:00
|
|
|
char* p = ::strtok(value, ",\r\n");
|
2025-03-18 13:52:17 +00:00
|
|
|
while (p != nullptr) {
|
2018-12-10 20:59:40 +01:00
|
|
|
unsigned int ric = (unsigned int)::atoi(p);
|
|
|
|
|
if (ric > 0U)
|
|
|
|
|
m_blackList.push_back(ric);
|
2025-03-18 13:52:17 +00:00
|
|
|
p = ::strtok(nullptr, ",\r\n");
|
2018-12-10 20:59:40 +01:00
|
|
|
}
|
2025-03-18 13:52:17 +00:00
|
|
|
} else if (::strcmp(key,"BlacklistRegexfile") == 0)
|
2018-11-18 23:35:06 +01:00
|
|
|
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)
|
2021-04-25 07:43:56 +02:00
|
|
|
m_rptPort = (unsigned short)::atoi(value);
|
2018-11-18 23:35:06 +01:00
|
|
|
else if (::strcmp(key, "LocalAddress") == 0)
|
|
|
|
|
m_myAddress = value;
|
|
|
|
|
else if (::strcmp(key, "LocalPort") == 0)
|
2021-04-25 07:43:56 +02:00
|
|
|
m_myPort = (unsigned short)::atoi(value);
|
2018-11-18 23:35:06 +01:00
|
|
|
else if (::strcmp(key, "Daemon") == 0)
|
|
|
|
|
m_daemon = ::atoi(value) == 1;
|
2025-03-18 13:52:17 +00:00
|
|
|
} else if (section == SECTION::LOG) {
|
2023-07-07 17:57:36 +01:00
|
|
|
if (::strcmp(key, "MQTTLevel") == 0)
|
|
|
|
|
m_logMQTTLevel = (unsigned int)::atoi(value);
|
2018-11-18 23:35:06 +01:00
|
|
|
else if (::strcmp(key, "DisplayLevel") == 0)
|
|
|
|
|
m_logDisplayLevel = (unsigned int)::atoi(value);
|
2025-03-18 15:13:07 +00:00
|
|
|
} else if (section == SECTION::MQTT) {
|
2023-07-07 17:57:36 +01:00
|
|
|
if (::strcmp(key, "Address") == 0)
|
|
|
|
|
m_mqttAddress = value;
|
|
|
|
|
else if (::strcmp(key, "Port") == 0)
|
|
|
|
|
m_mqttPort = (unsigned short)::atoi(value);
|
|
|
|
|
else if (::strcmp(key, "Keepalive") == 0)
|
|
|
|
|
m_mqttKeepalive = (unsigned int)::atoi(value);
|
|
|
|
|
else if (::strcmp(key, "Name") == 0)
|
|
|
|
|
m_mqttName = value;
|
2025-03-18 13:52:17 +00:00
|
|
|
} else if (section == SECTION::DAPNET) {
|
2018-11-18 23:35:06 +01:00
|
|
|
if (::strcmp(key, "Address") == 0)
|
|
|
|
|
m_dapnetAddress = value;
|
|
|
|
|
else if (::strcmp(key, "Port") == 0)
|
2021-04-25 07:43:56 +02:00
|
|
|
m_dapnetPort = (unsigned short)::atoi(value);
|
2018-11-18 23:35:06 +01:00
|
|
|
else if (::strcmp(key, "AuthKey") == 0) {
|
|
|
|
|
for (unsigned int i = 0U; value[i] != '\0'; i++) {
|
|
|
|
|
if (!::isspace(value[i]))
|
|
|
|
|
m_dapnetAuthKey.insert(m_dapnetAuthKey.end(), 1, value[i]);
|
|
|
|
|
}
|
2025-03-18 13:52:17 +00:00
|
|
|
} else if (::strcmp(key, "Debug") == 0)
|
2018-11-18 23:35:06 +01:00
|
|
|
m_dapnetDebug = ::atoi(value) == 1;
|
2018-08-28 22:59:31 +02:00
|
|
|
}
|
2018-06-06 20:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-18 23:35:06 +01:00
|
|
|
::fclose(fp);
|
2018-06-06 20:33:31 +01:00
|
|
|
|
2018-11-18 23:35:06 +01:00
|
|
|
return true;
|
2018-06-06 20:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CConf::getCallsign() const
|
|
|
|
|
{
|
2018-11-18 23:35:06 +01:00
|
|
|
return m_callsign;
|
2018-06-06 20:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-10 19:18:27 +01:00
|
|
|
std::vector<uint32_t> CConf::getWhiteList() const
|
|
|
|
|
{
|
|
|
|
|
return m_whiteList;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 14:17:34 +00:00
|
|
|
std::vector<uint32_t> CConf::getBlackList() const
|
|
|
|
|
{
|
2018-12-10 20:59:40 +01:00
|
|
|
return m_blackList;
|
2018-12-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-17 16:37:11 +00:00
|
|
|
std::string CConf::getblacklistRegexfile() const
|
|
|
|
|
{
|
|
|
|
|
return m_blacklistRegexfile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CConf::getwhitelistRegexfile() const
|
|
|
|
|
{
|
|
|
|
|
return m_whitelistRegexfile;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-06 20:33:31 +01:00
|
|
|
std::string CConf::getRptAddress() const
|
|
|
|
|
{
|
|
|
|
|
return m_rptAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 07:43:56 +02:00
|
|
|
unsigned short CConf::getRptPort() const
|
2018-06-06 20:33:31 +01:00
|
|
|
{
|
|
|
|
|
return m_rptPort;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CConf::getMyAddress() const
|
|
|
|
|
{
|
|
|
|
|
return m_myAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 07:43:56 +02:00
|
|
|
unsigned short CConf::getMyPort() const
|
2018-06-06 20:33:31 +01:00
|
|
|
{
|
|
|
|
|
return m_myPort;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConf::getDaemon() const
|
|
|
|
|
{
|
|
|
|
|
return m_daemon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int CConf::getLogDisplayLevel() const
|
|
|
|
|
{
|
|
|
|
|
return m_logDisplayLevel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:57:36 +01:00
|
|
|
unsigned int CConf::getLogMQTTLevel() const
|
|
|
|
|
{
|
|
|
|
|
return m_logMQTTLevel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CConf::getMQTTAddress() const
|
2018-06-06 20:33:31 +01:00
|
|
|
{
|
2023-07-07 17:57:36 +01:00
|
|
|
return m_mqttAddress;
|
2018-06-06 20:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:57:36 +01:00
|
|
|
unsigned short CConf::getMQTTPort() const
|
2018-06-06 20:33:31 +01:00
|
|
|
{
|
2023-07-07 17:57:36 +01:00
|
|
|
return m_mqttPort;
|
2018-06-06 20:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:57:36 +01:00
|
|
|
unsigned int CConf::getMQTTKeepalive() const
|
2018-06-06 20:33:31 +01:00
|
|
|
{
|
2023-07-07 17:57:36 +01:00
|
|
|
return m_mqttKeepalive;
|
2018-06-06 20:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-07 17:57:36 +01:00
|
|
|
std::string CConf::getMQTTName() const
|
2020-10-31 22:16:07 +00:00
|
|
|
{
|
2023-07-07 17:57:36 +01:00
|
|
|
return m_mqttName;
|
2020-10-31 22:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-06 20:33:31 +01:00
|
|
|
std::string CConf::getDAPNETAddress() const
|
|
|
|
|
{
|
|
|
|
|
return m_dapnetAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 07:43:56 +02:00
|
|
|
unsigned short CConf::getDAPNETPort() const
|
2018-06-06 20:33:31 +01:00
|
|
|
{
|
|
|
|
|
return m_dapnetPort;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CConf::getDAPNETAuthKey() const
|
|
|
|
|
{
|
|
|
|
|
return m_dapnetAuthKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConf::getDAPNETDebug() const
|
|
|
|
|
{
|
|
|
|
|
return m_dapnetDebug;
|
|
|
|
|
}
|