diff --git a/Common/CacheManager.cpp b/Common/CacheManager.cpp
index 7915d1b..682ad58 100644
--- a/Common/CacheManager.cpp
+++ b/Common/CacheManager.cpp
@@ -54,7 +54,7 @@ CUserData* CCacheManager::findUser(const wxString& user)
if (gr == NULL)
return NULL;
- return new CUserData(user, ur->getRepeater(), gr->getGateway(), gr->getAddress());
+ return new CUserData(user, ur->getRepeater(), gr->getGateway(), gr->getAddress(), gr->getG2Port());
}
CGatewayData* CCacheManager::findGateway(const wxString& gateway)
@@ -65,7 +65,7 @@ CGatewayData* CCacheManager::findGateway(const wxString& gateway)
if (gr == NULL)
return NULL;
- return new CGatewayData(gateway, gr->getAddress(), gr->getProtocol());
+ return new CGatewayData(gateway, gr->getAddress(), gr->getProtocol(), gr->getG2Port());
}
CRepeaterData* CCacheManager::findRepeater(const wxString& repeater)
@@ -87,7 +87,7 @@ CRepeaterData* CCacheManager::findRepeater(const wxString& repeater)
if (gr == NULL)
return NULL;
- return new CRepeaterData(repeater, gr->getGateway(), gr->getAddress(), gr->getProtocol());
+ return new CRepeaterData(repeater, gr->getGateway(), gr->getAddress(), gr->getProtocol(), gr->getG2Port());
}
void CCacheManager::updateUser(const wxString& user, const wxString& repeater, const wxString& gateway, const wxString& address, const wxString& timestamp, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock)
@@ -126,3 +126,8 @@ void CCacheManager::updateGateway(const wxString& gateway, const wxString& addre
m_gatewayCache.update(gateway, address, protocol, addrLock, protoLock);
}
+
+void CCacheManager::updateGatewayG2(const wxString& gateway, const in_addr& address, unsigned int g2Port)
+{
+ m_gatewayCache.updateG2(gateway, address, g2Port);
+}
diff --git a/Common/CacheManager.h b/Common/CacheManager.h
index e8d669b..dc38392 100644
--- a/Common/CacheManager.h
+++ b/Common/CacheManager.h
@@ -33,11 +33,12 @@
class CUserData {
public:
- CUserData(const wxString& user, const wxString& repeater, const wxString& gateway, in_addr address) :
+ CUserData(const wxString& user, const wxString& repeater, const wxString& gateway, in_addr address, unsigned int g2Port) :
m_user(user),
m_repeater(repeater),
m_gateway(gateway),
- m_address(address)
+ m_address(address),
+ m_g2Port(g2Port)
{
}
@@ -61,20 +62,27 @@ public:
return m_address;
}
+ unsigned int getG2Port() const
+ {
+ return m_g2Port;
+ }
+
private:
wxString m_user;
wxString m_repeater;
wxString m_gateway;
in_addr m_address;
+ unsigned int m_g2Port;
};
class CRepeaterData {
public:
- CRepeaterData(const wxString& repeater, const wxString& gateway, in_addr address, DSTAR_PROTOCOL protocol) :
+ CRepeaterData(const wxString& repeater, const wxString& gateway, in_addr address, DSTAR_PROTOCOL protocol, unsigned int g2Port) :
m_repeater(repeater),
m_gateway(gateway),
m_address(address),
- m_protocol(protocol)
+ m_protocol(protocol),
+ m_g2Port(g2Port)
{
}
@@ -98,19 +106,26 @@ public:
return m_protocol;
}
+ unsigned int getG2Port() const
+ {
+ return m_g2Port;
+ }
+
private:
wxString m_repeater;
wxString m_gateway;
in_addr m_address;
DSTAR_PROTOCOL m_protocol;
+ unsigned int m_g2Port;
};
class CGatewayData {
public:
- CGatewayData(const wxString& gateway, in_addr address, DSTAR_PROTOCOL protocol) :
+ CGatewayData(const wxString& gateway, in_addr address, DSTAR_PROTOCOL protocol, unsigned int g2Port) :
m_gateway(gateway),
m_address(address),
- m_protocol(protocol)
+ m_protocol(protocol),
+ m_g2Port(g2Port)
{
}
@@ -129,10 +144,16 @@ public:
return m_protocol;
}
+ unsigned int getG2Port() const
+ {
+ return m_g2Port;
+ }
+
private:
wxString m_gateway;
in_addr m_address;
DSTAR_PROTOCOL m_protocol;
+ unsigned int m_g2Port;
};
class CCacheManager {
@@ -146,7 +167,8 @@ public:
void updateUser(const wxString& user, const wxString& repeater, const wxString& gateway, const wxString& address, const wxString& timeStamp, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock);
void updateRepeater(const wxString& repeater, const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock);
- void updateGateway(const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock);
+ void updateGateway(const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol,bool addrLock, bool protoLock);
+ void updateGatewayG2(const wxString& gateway, const in_addr& address, unsigned int g2Port);
private:
wxMutex m_mutex;
diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj
index bdbcfc3..abf30b4 100644
--- a/Common/Common.vcxproj
+++ b/Common/Common.vcxproj
@@ -189,6 +189,7 @@
+
@@ -260,6 +261,7 @@
+
diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters
index 8365240..82574fb 100644
--- a/Common/Common.vcxproj.filters
+++ b/Common/Common.vcxproj.filters
@@ -209,6 +209,9 @@
Source Files
+
+ Source Files
+
@@ -433,5 +436,8 @@
Header Files
+
+ Header Files
+
\ No newline at end of file
diff --git a/Common/G2ProtocolHandler.cpp b/Common/G2ProtocolHandler.cpp
index 7d1fc7e..fe9d5f6 100644
--- a/Common/G2ProtocolHandler.cpp
+++ b/Common/G2ProtocolHandler.cpp
@@ -29,9 +29,7 @@ CG2ProtocolHandler::CG2ProtocolHandler(unsigned int port, const wxString& addr)
m_socket(addr, port),
m_type(GT_NONE),
m_buffer(NULL),
-m_length(0U),
-m_address(),
-m_port(0U)
+m_length(0U)
{
m_buffer = new unsigned char[BUFFER_LENGTH];
}
@@ -76,29 +74,33 @@ bool CG2ProtocolHandler::writeAMBE(const CAMBEData& data)
return m_socket.write(buffer, length, data.getYourAddress(), data.getYourPort());
}
-G2_TYPE CG2ProtocolHandler::read()
+G2_TYPE CG2ProtocolHandler::read(in_addr& remoteAddress, unsigned int& remotePort)
{
bool res = true;
// Loop until we have no more data from the socket or we have data for the higher layers
while (res)
- res = readPackets();
+ res = readPackets(remoteAddress, remotePort);
return m_type;
}
-bool CG2ProtocolHandler::readPackets()
+bool CG2ProtocolHandler::readPackets(in_addr& remoteAddress, unsigned int& remotePort)
{
m_type = GT_NONE;
+ remotePort = 0;
// No more data?
- int length = m_socket.read(m_buffer, BUFFER_LENGTH, m_address, m_port);
+ int length = m_socket.read(m_buffer, BUFFER_LENGTH, remoteAddress, remotePort);
if (length <= 0)
return false;
m_length = length;
if (m_buffer[0] != 'D' || m_buffer[1] != 'S' || m_buffer[2] != 'V' || m_buffer[3] != 'T') {
+ if(length == 1 && m_buffer[0] == 0)
+ return false;//we have been udp punched
+
return true;
} else {
// Header or data packet type?
@@ -111,7 +113,7 @@ bool CG2ProtocolHandler::readPackets()
}
}
-CHeaderData* CG2ProtocolHandler::readHeader()
+CHeaderData* CG2ProtocolHandler::readHeader(in_addr remoteAddress, unsigned int remotePort)
{
if (m_type != GT_HEADER)
return NULL;
@@ -119,7 +121,7 @@ CHeaderData* CG2ProtocolHandler::readHeader()
CHeaderData* header = new CHeaderData;
// G2 checksums are unreliable
- bool res = header->setG2Data(m_buffer, m_length, false, m_address, m_port);
+ bool res = header->setG2Data(m_buffer, m_length, false, remoteAddress, remotePort);
if (!res) {
delete header;
return NULL;
@@ -128,14 +130,15 @@ CHeaderData* CG2ProtocolHandler::readHeader()
return header;
}
-CAMBEData* CG2ProtocolHandler::readAMBE()
+CAMBEData* CG2ProtocolHandler::readAMBE(in_addr remoteAddress, unsigned int remotePort)
{
if (m_type != GT_AMBE)
return NULL;
CAMBEData* data = new CAMBEData;
- bool res = data->setG2Data(m_buffer, m_length, m_address, m_port);
+ bool res = data->setG2Data(m_buffer, m_length, remoteAddress, remotePort
+);
if (!res) {
delete data;
return NULL;
@@ -144,7 +147,7 @@ CAMBEData* CG2ProtocolHandler::readAMBE()
return data;
}
-void CG2ProtocolHandler::punchUDPHole(const wxString& address)
+void CG2ProtocolHandler::traverseNat(const wxString& address)
{
unsigned char buffer[1];
::memset(buffer, 0, 1);
diff --git a/Common/G2ProtocolHandler.h b/Common/G2ProtocolHandler.h
index 8b74bd5..088af25 100644
--- a/Common/G2ProtocolHandler.h
+++ b/Common/G2ProtocolHandler.h
@@ -48,11 +48,11 @@ public:
bool writeHeader(const CHeaderData& header);
bool writeAMBE(const CAMBEData& data);
- G2_TYPE read();
- CHeaderData* readHeader();
- CAMBEData* readAMBE();
+ G2_TYPE read(in_addr& incomingAddress, unsigned int& incomingPort);
+ 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();
@@ -61,10 +61,8 @@ private:
G2_TYPE m_type;
unsigned char* m_buffer;
unsigned int m_length;
- in_addr m_address;
- unsigned int m_port;
- bool readPackets();
+ bool readPackets(in_addr& incomingAddress, unsigned int& incomingPort);
};
#endif
diff --git a/Common/GatewayCache.cpp b/Common/GatewayCache.cpp
index b029d7c..e97984e 100644
--- a/Common/GatewayCache.cpp
+++ b/Common/GatewayCache.cpp
@@ -38,19 +38,56 @@ CGatewayRecord* CGatewayCache::find(const wxString& gateway)
void CGatewayCache::update(const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock)
{
- CGatewayRecord* rec = m_cache[gateway];
-
in_addr addr_in;
addr_in.s_addr = ::inet_addr(address.mb_str());
+ CGatewayRecord* rec = m_cache[gateway];
+
+ if(rec == NULL) {
+ rec = findByAddress(addr_in);//did this gateway punch to us and we do not have a gateway set for it ?
+ if(rec != NULL && rec->getGateway().empty() && rec->getProtocol() == protocol)
+ rec->setGateway(gateway);
+ else
+ rec = NULL;
+ }
+
if (rec == NULL)
// A brand new record is needed
- m_cache[gateway] = new CGatewayRecord(gateway, addr_in, protocol, addrLock, protoLock);
+ m_cache[gateway] = new CGatewayRecord(gateway, addr_in, G2_DV_PORT, protocol, addrLock, protoLock);
else
// Update an existing record
rec->setData(addr_in, protocol, addrLock, protoLock);
}
+void CGatewayCache::updateG2(const wxString& gateway, in_addr address, unsigned int g2Port)
+{
+ //empty gateway means we are coming from udp hole punching, let see if we have an gateway with matching address
+ CGatewayRecord* rec = gateway.empty()? findByAddress(address) : m_cache[gateway];
+
+ if (rec == NULL) {
+ // A brand new record is needed
+ m_cache[gateway] = new CGatewayRecord(gateway, address, g2Port, DP_UNKNOWN, false, false);
+ }
+ else {
+ // Update an existing record
+ if(rec->getGateway().empty())//if this is a record created from a punch call, set its gateway
+ rec->setGateway(gateway);
+
+ rec->setG2Data(address, g2Port);
+ }
+}
+
+CGatewayRecord* CGatewayCache::findByAddress(in_addr address)
+{
+ for (CGatewayCache_t::iterator it = m_cache.begin(); it != m_cache.end(); ++it) {
+ if(it-> second != NULL
+ && it->second->getAddress().s_addr == address.s_addr)
+ return it->second;
+ }
+
+ return NULL;
+}
+
unsigned int CGatewayCache::getCount() const
{
return m_cache.size();
diff --git a/Common/GatewayCache.h b/Common/GatewayCache.h
index 561743c..dc963b9 100644
--- a/Common/GatewayCache.h
+++ b/Common/GatewayCache.h
@@ -35,9 +35,10 @@
class CGatewayRecord {
public:
- CGatewayRecord(const wxString& gateway, in_addr address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) :
+ CGatewayRecord(const wxString& gateway, in_addr address, unsigned int g2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) :
m_gateway(gateway),
m_address(address),
+ m_g2Port(g2Port),
m_protocol(DP_UNKNOWN),
m_addrLock(addrLock),
m_protoLock(false)
@@ -53,6 +54,11 @@ public:
return m_gateway;
}
+ void setGateway(const wxString& gateway)
+ {
+ m_gateway = gateway;
+ }
+
in_addr getAddress() const
{
return m_address;
@@ -63,6 +69,11 @@ public:
return m_protocol;
}
+ unsigned int getG2Port() const
+ {
+ return m_g2Port;
+ }
+
void setData(in_addr address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock)
{
if (!m_addrLock) {
@@ -78,9 +89,20 @@ public:
}
}
+ void setG2Data(in_addr address, unsigned int g2Port)
+ {
+ if (!m_addrLock) {
+ m_address = address;
+ }
+
+ m_g2Port = g2Port;
+ }
+
private:
wxString m_gateway;
- in_addr m_address;
+ in_addr m_address;
+ //the incoming G2 port, keep track of it and use it to answer back instead of the default one. This helps us defeat NAT with no port forwarding to G2_DVPORT
+ unsigned int m_g2Port;
DSTAR_PROTOCOL m_protocol;
bool m_addrLock;
bool m_protoLock;
@@ -96,10 +118,13 @@ public:
CGatewayRecord* find(const wxString& gateway);
void update(const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock);
+ void updateG2(const wxString& gateway, in_addr address, unsigned int g2Port);
unsigned int getCount() const;
private:
+ CGatewayRecord* findByAddress(in_addr address);
+
CGatewayCache_t m_cache;
};
diff --git a/Common/Makefile b/Common/Makefile
index 0243a4b..e215ea2 100644
--- a/Common/Makefile
+++ b/Common/Makefile
@@ -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
diff --git a/Common/NatTraversalHandler.cpp b/Common/NatTraversalHandler.cpp
new file mode 100644
index 0000000..052c5d9
--- /dev/null
+++ b/Common/NatTraversalHandler.cpp
@@ -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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Common/NatTraversalHandler.h b/Common/NatTraversalHandler.h
new file mode 100644
index 0000000..c5c424d
--- /dev/null
+++ b/Common/NatTraversalHandler.h
@@ -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 29 //seconds
+
+#include "G2ProtocolHandler.h"
+
+#include
+#include
+
+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
\ No newline at end of file
diff --git a/Common/RepeaterHandler.cpp b/Common/RepeaterHandler.cpp
index 1a25db0..c1f32a9 100644
--- a/Common/RepeaterHandler.cpp
+++ b/Common/RepeaterHandler.cpp
@@ -109,6 +109,7 @@ m_g2Repeater(),
m_g2Gateway(),
m_g2Header(NULL),
m_g2Address(),
+m_g2Port(G2_DV_PORT),
m_linkStatus(LS_NONE),
m_linkRepeater(),
m_linkGateway(),
@@ -632,6 +633,7 @@ void CRepeaterHandler::processRepeater(CHeaderData& header)
m_g2User.Clear();
m_g2Repeater.Clear();
m_g2Gateway.Clear();
+ m_g2Port = G2_DV_PORT;
// Check if this user is restricted
m_restricted = false;
@@ -829,7 +831,7 @@ void CRepeaterHandler::processRepeater(CAMBEData& data)
break;
case G2_OK:
- data.setDestination(m_g2Address, G2_DV_PORT);
+ data.setDestination(m_g2Address, m_g2Port);
m_g2Handler->writeAMBE(data);
if (data.isEnd()) {
@@ -1213,7 +1215,7 @@ void CRepeaterHandler::resolveUserInt(const wxString& user, const wxString& repe
m_g2Repeater = repeater;
m_g2Gateway = gateway;
- m_g2Header->setDestination(m_g2Address, G2_DV_PORT);
+ m_g2Header->setDestination(m_g2Address, m_g2Port);
m_g2Header->setRepeaters(m_g2Gateway, m_g2Repeater);
m_g2Handler->writeHeader(*m_g2Header);
@@ -1226,6 +1228,7 @@ void CRepeaterHandler::resolveUserInt(const wxString& user, const wxString& repe
m_g2User.Clear();
m_g2Repeater.Clear();
m_g2Gateway.Clear();
+ m_g2Port = G2_DV_PORT;
delete m_g2Header;
m_g2Header = NULL;
@@ -1245,7 +1248,10 @@ void CRepeaterHandler::resolveRepeaterInt(const wxString& repeater, const wxStri
m_g2Repeater = repeater;
m_g2Gateway = gateway;
- m_g2Header->setDestination(m_g2Address, G2_DV_PORT);
+ CRepeaterData* rpt = m_cache->findRepeater(repeater);
+ m_g2Port = rpt != NULL ? rpt->getG2Port() : G2_DV_PORT;
+
+ m_g2Header->setDestination(m_g2Address, m_g2Port);
m_g2Header->setRepeaters(m_g2Gateway, m_g2Repeater);
m_g2Handler->writeHeader(*m_g2Header);
@@ -1258,6 +1264,7 @@ void CRepeaterHandler::resolveRepeaterInt(const wxString& repeater, const wxStri
m_g2User.Clear();
m_g2Repeater.Clear();
m_g2Gateway.Clear();
+ m_g2Port = G2_DV_PORT;
delete m_g2Header;
m_g2Header = NULL;
@@ -1459,6 +1466,7 @@ void CRepeaterHandler::clockInt(unsigned int ms)
m_g2User.Clear();
m_g2Repeater.Clear();
m_g2Gateway.Clear();
+ m_g2Port = G2_DV_PORT;
delete m_g2Header;
m_g2Header = NULL;
@@ -1983,7 +1991,8 @@ void CRepeaterHandler::g2CommandHandler(const wxString& callsign, const wxString
m_g2Status = G2_OK;
m_g2Address = data->getAddress();
m_g2Gateway = data->getGateway();
- header.setDestination(m_g2Address, G2_DV_PORT);
+ m_g2Port = data->getG2Port();
+ header.setDestination(m_g2Address, m_g2Port);
header.setRepeaters(m_g2Gateway, m_g2Repeater);
m_g2Handler->writeHeader(header);
delete data;
@@ -2025,7 +2034,9 @@ void CRepeaterHandler::g2CommandHandler(const wxString& callsign, const wxString
m_g2Address = data->getAddress();
m_g2Repeater = data->getRepeater();
m_g2Gateway = data->getGateway();
- header.setDestination(m_g2Address, G2_DV_PORT);
+ m_g2Port = data->getG2Port();
+ wxLogMessage(wxT("%s is trying to G2 route to gateway %s on port %d"), user.c_str(), m_g2Gateway.c_str(), m_g2Port);
+ header.setDestination(m_g2Address, m_g2Port);
header.setRepeaters(m_g2Gateway, m_g2Repeater);
m_g2Handler->writeHeader(header);
diff --git a/Common/RepeaterHandler.h b/Common/RepeaterHandler.h
index 05baca2..df16c5a 100644
--- a/Common/RepeaterHandler.h
+++ b/Common/RepeaterHandler.h
@@ -232,6 +232,7 @@ private:
wxString m_g2Gateway;
CHeaderData* m_g2Header;
in_addr m_g2Address;
+ unsigned int m_g2Port;
// Link info
LINK_STATUS m_linkStatus;
diff --git a/StarNetServer/StarNetServerThread.cpp b/StarNetServer/StarNetServerThread.cpp
index 2b86002..7b31214 100644
--- a/StarNetServer/StarNetServerThread.cpp
+++ b/StarNetServer/StarNetServerThread.cpp
@@ -505,15 +505,15 @@ void CStarNetServerThread::processDCS()
void CStarNetServerThread::processG2()
{
+ in_addr incomingAddress;
+ unsigned int incomingPort;
+
for (;;) {
- G2_TYPE type = m_g2Handler->read();
+ G2_TYPE type = m_g2Handler->read(incomingAddress, incomingPort);
switch (type) {
- case GT_NONE:
- return;
-
case GT_HEADER: {
- CHeaderData* header = m_g2Handler->readHeader();
+ CHeaderData* header = m_g2Handler->readHeader(incomingAddress, incomingPort);
if (header != NULL) {
// wxLogMessage(wxT("G2 header - My: %s/%s Your: %s Rpt1: %s Rpt2: %s Flags: %02X %02X %02X"), header->getMyCall1().c_str(), header->getMyCall2().c_str(), header->getYourCall().c_str(), header->getRptCall1().c_str(), header->getRptCall2().c_str(), header->getFlag1(), header->getFlag2(), header->getFlag3());
CG2Handler::process(*header);
@@ -523,13 +523,17 @@ void CStarNetServerThread::processG2()
break;
case GT_AMBE: {
- CAMBEData* data = m_g2Handler->readAMBE();
+ CAMBEData* data = m_g2Handler->readAMBE(incomingAddress, incomingPort);
if (data != NULL) {
CG2Handler::process(*data);
delete data;
}
}
break;
+
+ default:
+ //Probably someone punching a UDP hole to us
+ return;
}
}
}
diff --git a/ircDDBGateway/IRCDDBGatewayThread.cpp b/ircDDBGateway/IRCDDBGatewayThread.cpp
index 9da5110..a15819a 100644
--- a/ircDDBGateway/IRCDDBGatewayThread.cpp
+++ b/ircDDBGateway/IRCDDBGatewayThread.cpp
@@ -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());
}
@@ -732,14 +738,16 @@ void CIRCDDBGatewayThread::processIrcDDB()
if (!res)
break;
- CRepeaterHandler::resolveRepeater(repeater, gateway, address, DP_DEXTRA);
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());
}
+
+ //resolve after updating cache so CRepeaterHandler gets latest g2 port from cache
+ CRepeaterHandler::resolveRepeater(repeater, gateway, address, DP_DEXTRA);
}
break;
@@ -754,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());
}
@@ -1025,22 +1033,29 @@ void CIRCDDBGatewayThread::processDCS()
void CIRCDDBGatewayThread::processG2()
{
+ in_addr remoteAddress;
+ unsigned int remotePort;
+
for (;;) {
- G2_TYPE type = m_g2Handler->read();
+ G2_TYPE type = m_g2Handler->read(remoteAddress, remotePort);
switch (type) {
case GT_HEADER: {
- CHeaderData* header = m_g2Handler->readHeader();
+ CHeaderData* header = m_g2Handler->readHeader(remoteAddress, remotePort);
+
if (header != NULL) {
// wxLogMessage(wxT("G2 header - My: %s/%s Your: %s Rpt1: %s Rpt2: %s Flags: %02X %02X %02X"), header->getMyCall1().c_str(), header->getMyCall2().c_str(), header->getYourCall().c_str(), header->getRptCall1().c_str(), header->getRptCall2().c_str(), header->getFlag1(), header->getFlag2(), header->getFlag3());
CG2Handler::process(*header);
+ m_cache.updateGatewayG2(header-> getRptCall1(), remoteAddress, remotePort);
+
delete header;
}
}
break;
case GT_AMBE: {
- CAMBEData* data = m_g2Handler->readAMBE();
+ CAMBEData* data = m_g2Handler->readAMBE(remoteAddress, remotePort);
+
if (data != NULL) {
CG2Handler::process(*data);
delete data;
@@ -1049,6 +1064,12 @@ void CIRCDDBGatewayThread::processG2()
break;
default:
+ //Probably someone punching a UDP hole to us, keep track of that
+ if(remoteAddress.s_addr != INADDR_NONE && remotePort > 0 && remotePort < 65536) {
+ wxLogMessage(wxT("Incoming G2 UDP traversal from %s:%i"), ::inet_ntoa(remoteAddress), remotePort);
+ m_cache.updateGatewayG2(wxT(""), remoteAddress, remotePort);
+ }
+
return;
}
}
diff --git a/ircDDBGateway/IRCDDBGatewayThread.h b/ircDDBGateway/IRCDDBGatewayThread.h
index e3cc863..6862b63 100644
--- a/ircDDBGateway/IRCDDBGatewayThread.h
+++ b/ircDDBGateway/IRCDDBGatewayThread.h
@@ -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;