diff --git a/Common/CacheManager.cpp b/Common/CacheManager.cpp index 713624c..fd12a47 100644 --- a/Common/CacheManager.cpp +++ b/Common/CacheManager.cpp @@ -91,11 +91,6 @@ CRepeaterData* CCacheManager::findRepeater(const wxString& repeater) } 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) -{ - updateUser(user, repeater, gateway, address, G2_DV_PORT, true, timestamp, protocol, addrLock, protoLock); -} - -void CCacheManager::updateUser(const wxString& user, const wxString& repeater, const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, const wxString& timestamp, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) { wxMutexLocker locker(m_mutex); @@ -108,15 +103,10 @@ void CCacheManager::updateUser(const wxString& user, const wxString& repeater, c if (!repeater7.IsSameAs(gateway7)) m_repeaterCache.update(repeater, gateway); - updateGateway(gateway, address, g2Port, ignoreG2Port, protocol, addrLock, protoLock); + m_gatewayCache.update(gateway, address, protocol, addrLock, protoLock); } void CCacheManager::updateRepeater(const wxString& repeater, const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) -{ - updateRepeater(repeater, gateway, address, G2_DV_PORT, true, protocol, addrLock, protoLock); -} - -void CCacheManager::updateRepeater(const wxString& repeater, const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) { wxMutexLocker locker(m_mutex); @@ -127,24 +117,17 @@ void CCacheManager::updateRepeater(const wxString& repeater, const wxString& gat if (!repeater7.IsSameAs(gateway7)) m_repeaterCache.update(repeater, gateway); - updateGateway(gateway, address, g2Port, ignoreG2Port, protocol, addrLock, protoLock); + m_gatewayCache.update(gateway, address, protocol, addrLock, protoLock); } void CCacheManager::updateGateway(const wxString& gateway, const wxString& address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) -{ - updateGateway(gateway, address, G2_DV_PORT, true, protocol, addrLock, protoLock); -} - -void CCacheManager::updateGateway(const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) { wxMutexLocker locker(m_mutex); - m_gatewayCache.update(gateway, address, g2Port, ignoreG2Port, protocol, addrLock, protoLock); + m_gatewayCache.update(gateway, address, protocol, addrLock, protoLock); } -void CCacheManager::updateGatewayG2(const wxString& gateway, const wxString& address, unsigned int g2Port) +void CCacheManager::updateGatewayG2(const wxString& gateway, const in_addr& address, unsigned int g2Port) { - CGatewayRecord* gr = m_gatewayCache.find(gateway); - DSTAR_PROTOCOL protocol = gr != NULL? gr->getProtocol() : DP_UNKNOWN; - updateGateway(gateway, address, g2Port, false, protocol, false, false); + m_gatewayCache.updateG2(gateway, address, g2Port); } diff --git a/Common/CacheManager.h b/Common/CacheManager.h index 80d29b1..c014ce7 100644 --- a/Common/CacheManager.h +++ b/Common/CacheManager.h @@ -147,13 +147,9 @@ 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 updateGatewayG2(const wxString& gateway, const wxString& address, unsigned int g2Port); + void updateGatewayG2(const wxString& gateway, const in_addr& address, unsigned int g2Port); private: - void updateUser(const wxString& user, const wxString& repeater, const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, const wxString& timeStamp, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock); - void updateRepeater(const wxString& repeater, const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock); - void updateGateway(const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, DSTAR_PROTOCOL protocol,bool addrLock, bool protoLock); - wxMutex m_mutex; CUserCache m_userCache; CGatewayCache m_gatewayCache; diff --git a/Common/GatewayCache.cpp b/Common/GatewayCache.cpp index 17ffc5b..83bc62a 100644 --- a/Common/GatewayCache.cpp +++ b/Common/GatewayCache.cpp @@ -36,19 +36,48 @@ CGatewayRecord* CGatewayCache::find(const wxString& gateway) return m_cache[gateway]; } -void CGatewayCache::update(const wxString& gateway, const wxString& address, unsigned int g2Port, bool ignoreG2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) +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) // A brand new record is needed - m_cache[gateway] = new CGatewayRecord(gateway, addr_in, g2Port, 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, g2Port, ignoreG2Port, protocol, addrLock, protoLock); + 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 getway 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 diff --git a/Common/GatewayCache.h b/Common/GatewayCache.h index 4140d24..7729241 100644 --- a/Common/GatewayCache.h +++ b/Common/GatewayCache.h @@ -54,6 +54,11 @@ public: return m_gateway; } + void setGateway(const wxString& gateway) + { + m_gateway = gateway; + } + in_addr getAddress() const { return m_address; @@ -69,11 +74,8 @@ public: return m_g2Port; } - void setData(in_addr address, unsigned int g2Port, bool ignoreG2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) + void setData(in_addr address, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock) { - if(!ignoreG2Port) - m_g2Port = g2Port; - if (!m_addrLock) { m_address = address; m_addrLock = addrLock; @@ -87,10 +89,19 @@ 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; - //the incoming G2 port, usually the default one unless the calling hotspot is behind a NAT, therefore keep track of it and use it to answer back instead of the default one + //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; @@ -106,11 +117,14 @@ public: CGatewayRecord* find(const wxString& gateway); - void update(const wxString& gateway, const wxString& address, unsigned int g2port, bool ignoreG2Port, DSTAR_PROTOCOL protocol, bool addrLock, bool protoLock); + 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/ircDDBGateway/IRCDDBGatewayThread.cpp b/ircDDBGateway/IRCDDBGatewayThread.cpp index fbecd53..4281e72 100644 --- a/ircDDBGateway/IRCDDBGatewayThread.cpp +++ b/ircDDBGateway/IRCDDBGatewayThread.cpp @@ -1037,7 +1037,7 @@ void CIRCDDBGatewayThread::processG2() 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(), wxT("127.0.0.1"), incomingPort); + m_cache.updateGatewayG2(header-> getRptCall1(), incomingAddress, incomingPort); delete header; } } @@ -1053,7 +1053,8 @@ void CIRCDDBGatewayThread::processG2() break; default: - //Probably someone punching a UDP hole to us + //Probably someone punching a UDP hole to us, keep track of that + m_cache.updateGatewayG2(wxT(""), incomingAddress, incomingPort); return; } }