Add optional MQTT authentication.

This commit is contained in:
Jonathan Naylor 2025-03-21 17:37:30 +00:00
parent a2e2ea3675
commit 538108b192
9 changed files with 68 additions and 10 deletions

View file

@ -183,6 +183,9 @@ m_mqttAddress("127.0.0.1"),
m_mqttPort(1883U),
m_mqttKeepalive(60U),
m_mqttName("dmr-gateway"),
m_mqttAuthEnabled(false),
m_mqttUsername(),
m_mqttPassword(),
m_dynamicTGControlEnabled(false),
m_remoteCommandsEnabled(false)
{
@ -989,6 +992,12 @@ bool CConf::read()
m_mqttKeepalive = (unsigned int)::atoi(value);
else if (::strcmp(key, "Name") == 0)
m_mqttName = value;
else if (::strcmp(key, "Auth") == 0)
m_mqttAuthEnabled = ::atoi(value) == 1;
else if (::strcmp(key, "Username") == 0)
m_mqttUsername = value;
else if (::strcmp(key, "Password") == 0)
m_mqttPassword = value;
} else if (section == SECTION::DYNAMIC_TG_CONTROL) {
if (::strcmp(key, "Enable") == 0)
m_dynamicTGControlEnabled = ::atoi(value) == 1;
@ -1696,6 +1705,21 @@ std::string CConf::getMQTTName() const
return m_mqttName;
}
bool CConf::getMQTTAuthEnabled() const
{
return m_mqttAuthEnabled;
}
std::string CConf::getMQTTUsername() const
{
return m_mqttUsername;
}
std::string CConf::getMQTTPassword() const
{
return m_mqttPassword;
}
bool CConf::getDynamicTGControlEnabled() const
{
return m_dynamicTGControlEnabled;

8
Conf.h
View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2015,2016,2017,2019,2020,2023 by Jonathan Naylor G4KLX
* Copyright (C) 2015,2016,2017,2019,2020,2023,2025 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
@ -238,6 +238,9 @@ public:
unsigned short getMQTTPort() const;
unsigned int getMQTTKeepalive() const;
std::string getMQTTName() const;
bool getMQTTAuthEnabled() const;
std::string getMQTTUsername() const;
std::string getMQTTPassword() const;
// The Dynamic TG Control section
bool getDynamicTGControlEnabled() const;
@ -395,6 +398,9 @@ private:
unsigned short m_mqttPort;
unsigned int m_mqttKeepalive;
std::string m_mqttName;
bool m_mqttAuthEnabled;
std::string m_mqttUsername;
std::string m_mqttPassword;
bool m_dynamicTGControlEnabled;

View file

@ -77,7 +77,7 @@ static CDMRGateway* gateway = nullptr;
const char* HEADER1 = "This software is for use on amateur radio networks only,";
const char* HEADER2 = "it is to be used for educational purposes only. Its use on";
const char* HEADER3 = "commercial networks is strictly prohibited.";
const char* HEADER4 = "Copyright(C) 2017-2024 by Jonathan Naylor, G4KLX and others";
const char* HEADER4 = "Copyright(C) 2017-2025 by Jonathan Naylor, G4KLX and others";
int main(int argc, char** argv)
{
@ -373,7 +373,7 @@ int CDMRGateway::run()
if (m_conf.getRemoteCommandsEnabled())
subscriptions.push_back(std::make_pair("command", CDMRGateway::onCommand));
m_mqtt = new CMQTTConnection(m_conf.getMQTTAddress(), m_conf.getMQTTPort(), m_conf.getMQTTName(), subscriptions, m_conf.getMQTTKeepalive());
m_mqtt = new CMQTTConnection(m_conf.getMQTTAddress(), m_conf.getMQTTPort(), m_conf.getMQTTName(), m_conf.getMQTTAuthEnabled(), m_conf.getMQTTUsername(), m_conf.getMQTTPassword(), subscriptions, m_conf.getMQTTKeepalive());
ret = m_mqtt->open();
if (!ret)
return 1;

View file

@ -150,6 +150,9 @@ Suffix=3
Address=127.0.0.1
Port=1883
Keepalive=60
Auth=0
Username=mmdvm
Password=mmdvm
Name=dmr-gateway
[Dynamic TG Control]

View file

@ -88,11 +88,13 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;HAVE_LOG_H</PreprocessorDefinitions>
<AdditionalIncludeDirectories>C:\Program Files\mosquitto\devel;C:\Program Files</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;mosquitto.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Program Files\mosquitto\devel</AdditionalLibraryDirectories>
</Link>
<PreBuildEvent>
<Command>prebuild.cmd</Command>
@ -105,11 +107,13 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;HAVE_LOG_H</PreprocessorDefinitions>
<AdditionalIncludeDirectories>C:\Program Files\mosquitto\devel;C:\Program Files</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;mosquitto.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Program Files\mosquitto\devel</AdditionalLibraryDirectories>
</Link>
<PreBuildEvent>
<Command>prebuild.cmd</Command>
@ -128,13 +132,15 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;HAVE_LOG_H</PreprocessorDefinitions>
<AdditionalIncludeDirectories>C:\Program Files\mosquitto\devel;C:\Program Files</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;mosquitto.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Program Files\mosquitto\devel</AdditionalLibraryDirectories>
</Link>
<PreBuildEvent>
<Command>prebuild.cmd</Command>
@ -149,13 +155,15 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;HAVE_LOG_H</PreprocessorDefinitions>
<AdditionalIncludeDirectories>C:\Program Files\mosquitto\devel;C:\Program Files</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;mosquitto.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>C:\Program Files\mosquitto\devel</AdditionalLibraryDirectories>
</Link>
<PreBuildEvent>
<Command>prebuild.cmd</Command>
@ -183,6 +191,7 @@
<ClInclude Include="Hamming.h" />
<ClInclude Include="Log.h" />
<ClInclude Include="MMDVMNetwork.h" />
<ClInclude Include="MQTTConnection.h" />
<ClInclude Include="PassAllPC.h" />
<ClInclude Include="PassAllTG.h" />
<ClInclude Include="QR1676.h" />
@ -230,6 +239,7 @@
<ClCompile Include="Hamming.cpp" />
<ClCompile Include="Log.cpp" />
<ClCompile Include="MMDVMNetwork.cpp" />
<ClCompile Include="MQTTConnection.cpp" />
<ClCompile Include="PassAllPC.cpp" />
<ClCompile Include="PassAllTG.cpp" />
<ClCompile Include="QR1676.cpp" />

View file

@ -149,6 +149,9 @@
<ClInclude Include="RemoteControl.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MQTTConnection.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Conf.cpp">
@ -280,5 +283,8 @@
<ClCompile Include="RemoteControl.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MQTTConnection.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View file

@ -23,10 +23,13 @@
#include <cstring>
#include <ctime>
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& username, const std::string& password, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
m_host(host),
m_port(port),
m_name(name),
m_authEnabled(authEnabled),
m_username(username),
m_password(password),
m_subs(subs),
m_keepalive(keepalive),
m_qos(qos),
@ -59,6 +62,9 @@ bool CMQTTConnection::open()
return false;
}
if (m_authEnabled)
::mosquitto_username_pw_set(m_mosq, m_username.c_str(), m_password.c_str());
::mosquitto_connect_callback_set(m_mosq, onConnect);
::mosquitto_subscribe_callback_set(m_mosq, onSubscribe);
::mosquitto_message_callback_set(m_mosq, onMessage);

View file

@ -32,7 +32,7 @@ enum class MQTT_QOS : int {
class CMQTTConnection {
public:
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS::EXACTLY_ONCE);
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& username, const std::string& password, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS::EXACTLY_ONCE);
~CMQTTConnection();
bool open();
@ -47,6 +47,9 @@ private:
std::string m_host;
unsigned short m_port;
std::string m_name;
bool m_authEnabled;
std::string m_username;
std::string m_password;
std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> m_subs;
unsigned int m_keepalive;
MQTT_QOS m_qos;

View file

@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20250315";
const char* VERSION = "20250321";
#endif