2018-05-09 20:23:17 +02:00
/*
* Copyright ( C ) 2010 , 2011 , 2012 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 0213 9 , USA .
*/
# ifndef GatewayCache_H
# define GatewayCache_H
# include "DStarDefines.h"
# include "Defs.h"
# if defined(__WINDOWS__)
# include "Inaddr.h"
# else
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# endif
# include <wx/wx.h>
# include <wx/ffile.h>
class CGatewayRecord {
public :
2018-11-18 07:58:52 +01:00
CGatewayRecord ( const wxString & gateway , in_addr address , unsigned int g2Port , DSTAR_PROTOCOL protocol , bool addrLock , bool protoLock ) :
2018-05-09 20:23:17 +02:00
m_gateway ( gateway ) ,
m_address ( address ) ,
2018-11-18 07:58:52 +01:00
m_g2Port ( g2Port ) ,
2018-05-09 20:23:17 +02:00
m_protocol ( DP_UNKNOWN ) ,
m_addrLock ( addrLock ) ,
m_protoLock ( false )
{
if ( protocol ! = DP_UNKNOWN ) {
m_protocol = protocol ;
m_protoLock = protoLock ;
}
}
wxString getGateway ( ) const
{
return m_gateway ;
}
2018-11-18 16:54:57 +01:00
void setGateway ( const wxString & gateway )
{
m_gateway = gateway ;
}
2018-05-09 20:23:17 +02:00
in_addr getAddress ( ) const
{
return m_address ;
}
DSTAR_PROTOCOL getProtocol ( ) const
{
return m_protocol ;
}
2018-11-18 18:41:13 +01:00
unsigned int getG2Port ( ) const
2018-05-09 20:23:17 +02:00
{
2018-11-18 07:58:52 +01:00
return m_g2Port ;
}
2018-11-18 16:54:57 +01:00
void setData ( in_addr address , DSTAR_PROTOCOL protocol , bool addrLock , bool protoLock )
2018-11-18 07:58:52 +01:00
{
2018-05-09 20:23:17 +02:00
if ( ! m_addrLock ) {
m_address = address ;
m_addrLock = addrLock ;
}
if ( ! m_protoLock ) {
if ( protocol ! = DP_UNKNOWN ) {
m_protocol = protocol ;
m_protoLock = protoLock ;
}
}
}
2018-11-18 16:54:57 +01:00
void setG2Data ( in_addr address , unsigned int g2Port )
{
if ( ! m_addrLock ) {
m_address = address ;
}
m_g2Port = g2Port ;
}
2018-05-09 20:23:17 +02:00
private :
wxString m_gateway ;
2018-11-18 07:58:52 +01:00
in_addr m_address ;
2018-11-18 16:54:57 +01:00
//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
2018-11-18 07:58:52 +01:00
unsigned int m_g2Port ;
2018-05-09 20:23:17 +02:00
DSTAR_PROTOCOL m_protocol ;
bool m_addrLock ;
bool m_protoLock ;
} ;
WX_DECLARE_STRING_HASH_MAP ( CGatewayRecord * , CGatewayCache_t ) ;
class CGatewayCache {
public :
CGatewayCache ( ) ;
~ CGatewayCache ( ) ;
CGatewayRecord * find ( const wxString & gateway ) ;
2018-11-18 16:54:57 +01:00
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 ) ;
2018-05-09 20:23:17 +02:00
unsigned int getCount ( ) const ;
private :
2018-11-18 16:54:57 +01:00
CGatewayRecord * findByAddress ( in_addr address ) ;
2018-05-09 20:23:17 +02:00
CGatewayCache_t m_cache ;
} ;
# endif