Off loaded G2 Nat Traversal to specific handler class

G2 Nat traversal handling in its own class
This might serve as a starting point for upcoming DExtra/DCS/DPlus
traversal
This commit is contained in:
Geoffrey Merck F4FXL - KC3FRA 2018-11-24 15:22:34 +01:00
parent d6cfc2552f
commit 51c734faa7
8 changed files with 150 additions and 6 deletions

View file

@ -189,6 +189,7 @@
<ClCompile Include="IRCDDBGatewayConfig.cpp" />
<ClCompile Include="LogEvent.cpp" />
<ClCompile Include="Logger.cpp" />
<ClCompile Include="NatTraversalHandler.cpp" />
<ClCompile Include="PollData.cpp" />
<ClCompile Include="RemoteHandler.cpp" />
<ClCompile Include="RemoteLinkData.cpp" />
@ -260,6 +261,7 @@
<ClInclude Include="IRCDDBGatewayConfig.h" />
<ClInclude Include="LogEvent.h" />
<ClInclude Include="Logger.h" />
<ClInclude Include="NatTraversalHandler.h" />
<ClInclude Include="PollData.h" />
<ClInclude Include="ReflectorCallback.h" />
<ClInclude Include="RemoteHandler.h" />

View file

@ -147,7 +147,7 @@ CAMBEData* CG2ProtocolHandler::readAMBE(in_addr remoteAddress, unsigned int remo
return data;
}
void CG2ProtocolHandler::punchUDPHole(const wxString& address)
void CG2ProtocolHandler::traverseNat(const wxString& address)
{
unsigned char buffer[1];
::memset(buffer, 0, 1);

View file

@ -52,7 +52,7 @@ public:
CHeaderData* readHeader(in_addr incomingAddress, unsigned int incomingPort);
CAMBEData* readAMBE(in_addr incomingAddress, unsigned int incomingPort);
void punchUDPHole(const wxString& addr);
void traverseNat(const wxString& addr);
void close();

View file

@ -4,7 +4,7 @@ OBJECTS = AMBEData.o AnnouncementUnit.o APRSCollector.o APRSWriter.o APRSWriterT
DPlusAuthenticator.o DPlusHandler.o DPlusProtocolHandler.o DPlusProtocolHandlerPool.o DRATSServer.o DTMF.o \
DummyRepeaterProtocolHandler.o DVTOOLFileReader.o EchoUnit.o G2Handler.o G2ProtocolHandler.o GatewayCache.o \
HBRepeaterProtocolHandler.o HeaderData.o HeaderLogger.o HeardData.o HostFile.o IcomRepeaterProtocolHandler.o IRCDDBGatewayConfig.o \
LogEvent.o Logger.o PollData.o RemoteHandler.o RemoteLinkData.o RemoteProtocolHandler.o RemoteRepeaterData.o RemoteStarNetGroup.o \
LogEvent.o Logger.o NatTraversalHandler.o PollData.o RemoteHandler.o RemoteLinkData.o RemoteProtocolHandler.o RemoteRepeaterData.o RemoteStarNetGroup.o \
RemoteStarNetUser.o RepeaterCache.o RepeaterHandler.o SHA256.o SlowDataEncoder.o StarNetHandler.o StatusData.o \
TCPReaderWriterClient.o TCPReaderWriterServer.o TextCollector.o TextData.o Timer.o UDPReaderWriter.o UserCache.o Utils.o \
VersionUnit.o XLXHostsFileDownloader.o

View file

@ -0,0 +1,57 @@
/*
* Copyright (C) 2010,2011,2012,2013,2014,2015,2016,2017,2018 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 "NatTraversalHandler.h"
const unsigned int CACHE_SIZE = 500U;
CNatTraversalHandler::CNatTraversalHandler() :
m_g2cache(CACHE_SIZE),
m_g2Handler(NULL)
{
}
CNatTraversalHandler::~CNatTraversalHandler()
{
for (CNatTraversalCache_t::iterator it = m_g2cache.begin(); it != m_g2cache.end(); ++it)
delete it->second;
}
void CNatTraversalHandler::setG2Handler(CG2ProtocolHandler * handler)
{
m_g2Handler = handler;
}
void CNatTraversalHandler::traverseNatG2(const wxString& address)
{
if(m_g2Handler != NULL){
CNatTraversalRecord* record = m_g2cache[address];
if(record == NULL) {
record = new CNatTraversalRecord(address);
m_g2cache[address] = record;
}
std::time_t currentTime = std::time(NULL);
if(currentTime - record->getTimestamp() > G2_TRAVERSAL_TIMEOUT) {
record->setTimestamp(currentTime);
m_g2Handler->traverseNat(address);
}
}
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (C) 2010,2011,2012,2013,2014,2015,2016,2017,2018 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.
*/
#ifndef NatTraversalHandler_H
#define NatTraversalHandler_H
#define G2_TRAVERSAL_TIMEOUT 120 //seconds
#include "G2ProtocolHandler.h"
#include <wx/wx.h>
#include <ctime>
enum NAT_TRAVERSAL_TYPE {
NTT_G2,
//NTT_DEXTRA
//NTT_DCS
//NTT_DPLUS
};
class CNatTraversalRecord {
public:
CNatTraversalRecord(const wxString& address) :
m_address(address),
m_timestamp(0)
{
}
std::time_t getTimestamp() const
{
return m_timestamp;
}
void setTimestamp(std::time_t timestamp)
{
m_timestamp = timestamp;
}
private:
wxString m_address;
std::time_t m_timestamp;
};
WX_DECLARE_STRING_HASH_MAP(CNatTraversalRecord*, CNatTraversalCache_t);
/*
* This keeps track of when we UDP punched to one destination so to avoid unnecessary traffic on each ircddb reporting
*/
class CNatTraversalHandler {
public:
CNatTraversalHandler();
~CNatTraversalHandler();
void setG2Handler(CG2ProtocolHandler* handler);
void traverseNatG2(const wxString& address);
private:
CNatTraversalCache_t m_g2cache;
CG2ProtocolHandler* m_g2Handler;
};
#endif

View file

@ -72,6 +72,7 @@ m_dextraPool(NULL),
m_dplusPool(NULL),
m_dcsPool(NULL),
m_g2Handler(NULL),
m_natTraversal(NULL),
m_aprsWriter(NULL),
m_irc(NULL),
m_cache(),
@ -217,6 +218,11 @@ void CIRCDDBGatewayThread::run()
m_g2Handler = NULL;
}
if(m_g2Handler != NULL) {
m_natTraversal = new CNatTraversalHandler();
m_natTraversal->setG2Handler(m_g2Handler);
}
// Wait here until we have the essentials to run
while (!m_killed && (m_dextraPool == NULL || m_dplusPool == NULL || m_dcsPool == NULL || m_g2Handler == NULL || (m_icomRepeaterHandler == NULL && m_hbRepeaterHandler == NULL && m_dummyRepeaterHandler == NULL) || m_gatewayCallsign.IsEmpty()))
::wxMilliSleep(500UL); // 1/2 sec
@ -719,7 +725,7 @@ void CIRCDDBGatewayThread::processIrcDDB()
if (!address.IsEmpty()) {
wxLogMessage(wxT("USER: %s %s %s %s"), user.c_str(), repeater.c_str(), gateway.c_str(), address.c_str());
m_cache.updateUser(user, repeater, gateway, address, timestamp, DP_DEXTRA, false, false);
m_g2Handler->punchUDPHole(address);
m_natTraversal->traverseNatG2(address);
} else {
wxLogMessage(wxT("USER: %s NOT FOUND"), user.c_str());
}
@ -735,7 +741,7 @@ void CIRCDDBGatewayThread::processIrcDDB()
if (!address.IsEmpty()) {
wxLogMessage(wxT("REPEATER: %s %s %s"), repeater.c_str(), gateway.c_str(), address.c_str());
m_cache.updateRepeater(repeater, gateway, address, DP_DEXTRA, false, false);
m_g2Handler->punchUDPHole(address);
m_natTraversal->traverseNatG2(address);
} else {
wxLogMessage(wxT("REPEATER: %s NOT FOUND"), repeater.c_str());
}
@ -756,7 +762,7 @@ void CIRCDDBGatewayThread::processIrcDDB()
if (!address.IsEmpty()) {
wxLogMessage(wxT("GATEWAY: %s %s"), gateway.c_str(), address.c_str());
m_cache.updateGateway(gateway, address, DP_DEXTRA, false, false);
m_g2Handler->punchUDPHole(address);
m_natTraversal->traverseNatG2(address);
} else {
wxLogMessage(wxT("GATEWAY: %s NOT FOUND"), gateway.c_str());
}

View file

@ -28,6 +28,7 @@
#include "IRCDDBGatewayStatusData.h"
#include "DCSProtocolHandlerPool.h"
#include "G2ProtocolHandler.h"
#include "NatTraversalHandler.h"
#include "RemoteHandler.h"
#include "CacheManager.h"
#include "CallsignList.h"
@ -92,6 +93,7 @@ private:
CDPlusProtocolHandlerPool* m_dplusPool;
CDCSProtocolHandlerPool* m_dcsPool;
CG2ProtocolHandler* m_g2Handler;
CNatTraversalHandler* m_natTraversal;
CAPRSWriter* m_aprsWriter;
CIRCDDB* m_irc;
CCacheManager m_cache;