Browse Source

one protocol

master
Tom Early 12 months ago
parent
commit
92286f4b78
  1. 1
      client.h
  2. 52
      clients.cpp
  3. 15
      clients.h
  4. 8
      gatekeeper.cpp
  5. 4
      gatekeeper.h
  6. 1
      m17client.h
  7. 1
      m17peer.h
  8. 34
      m17protocol.cpp
  9. 2
      m17protocol.h
  10. 4
      main.h
  11. 1
      peer.h
  12. 25
      peers.cpp
  13. 8
      peers.h
  14. 2
      protocol.cpp
  15. 2
      protocol.h
  16. 2
      reflector.cpp

1
client.h

@ -58,7 +58,6 @@ public:
void SetReflectorModule(char c) { m_ReflectorModule = c; }
// identity
virtual int GetProtocol(void) const { return PROTOCOL_NONE; }
virtual const char *GetProtocolName(void) const { return "none"; }
virtual bool IsNode(void) const { return false; }
virtual bool IsPeer(void) const { return false; }

52
clients.cpp

@ -132,12 +132,12 @@ std::shared_ptr<CClient> CClients::FindClient(const CIp &Ip)
return nullptr;
}
std::shared_ptr<CClient> CClients::FindClient(const CIp &Ip, int Protocol)
std::shared_ptr<CClient> CClients::FindClient(const CIp &Ip, char ReflectorModule)
{
// find client
for ( auto it=begin(); it!=end(); it++ )
{
if ( ((*it)->GetIp() == Ip) && ((*it)->GetProtocol() == Protocol))
if ( ((*it)->GetIp() == Ip) && ((*it)->GetReflectorModule() == ReflectorModule) )
{
return *it;
}
@ -147,27 +147,26 @@ std::shared_ptr<CClient> CClients::FindClient(const CIp &Ip, int Protocol)
return nullptr;
}
std::shared_ptr<CClient> CClients::FindClient(const CIp &Ip, int Protocol, char ReflectorModule)
std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, const CIp &Ip)
{
// find client
for ( auto it=begin(); it!=end(); it++ )
{
if ( ((*it)->GetIp() == Ip) && ((*it)->GetReflectorModule() == ReflectorModule) && ((*it)->GetProtocol() == Protocol) )
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) && ((*it)->GetIp() == Ip) )
{
return *it;
}
}
// done
return nullptr;
}
std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, const CIp &Ip, int Protocol)
std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, char module, const CIp &Ip)
{
// find client
for ( auto it=begin(); it!=end(); it++ )
{
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) && ((*it)->GetIp() == Ip) && ((*it)->GetProtocol() == Protocol) )
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) && ((*it)->GetModule() == module) && ((*it)->GetIp() == Ip) )
{
return *it;
}
@ -176,26 +175,12 @@ std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, const C
return nullptr;
}
std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, char module, const CIp &Ip, int Protocol)
std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign)
{
// find client
for ( auto it=begin(); it!=end(); it++ )
{
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) && ((*it)->GetModule() == module) && ((*it)->GetIp() == Ip) && ((*it)->GetProtocol() == Protocol) )
{
return *it;
}
}
return nullptr;
}
std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, int Protocol)
{
// find client
for ( auto it=begin(); it!=end(); it++ )
{
if ( ((*it)->GetProtocol() == Protocol) && (*it)->GetCallsign().HasSameCallsign(Callsign) )
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) )
{
return *it;
}
@ -207,24 +192,19 @@ std::shared_ptr<CClient> CClients::FindClient(const CCallsign &Callsign, int Pro
////////////////////////////////////////////////////////////////////////////////////////
// iterate on clients
std::shared_ptr<CClient> CClients::FindNextClient(int Protocol, std::list<std::shared_ptr<CClient>>::iterator &it)
std::shared_ptr<CClient> CClients::FindNextClient(std::list<std::shared_ptr<CClient>>::iterator &it)
{
while ( it != end() )
{
if ( (*it)->GetProtocol() == Protocol )
{
return *it++;
}
it++;
}
if ( it != end() )
return *it++;
return nullptr;
}
std::shared_ptr<CClient> CClients::FindNextClient(const CIp &Ip, int Protocol, std::list<std::shared_ptr<CClient>>::iterator &it)
std::shared_ptr<CClient> CClients::FindNextClient(const CIp &Ip, std::list<std::shared_ptr<CClient>>::iterator &it)
{
while ( it != end() )
{
if ( ((*it)->GetProtocol() == Protocol) && ((*it)->GetIp() == Ip) )
if ((*it)->GetIp() == Ip )
{
return *it++;
}
@ -233,11 +213,11 @@ std::shared_ptr<CClient> CClients::FindNextClient(const CIp &Ip, int Protocol, s
return nullptr;
}
std::shared_ptr<CClient> CClients::FindNextClient(const CCallsign &Callsign, const CIp &Ip, int Protocol, std::list<std::shared_ptr<CClient>>::iterator &it)
std::shared_ptr<CClient> CClients::FindNextClient(const CCallsign &Callsign, const CIp &Ip, std::list<std::shared_ptr<CClient>>::iterator &it)
{
while ( it != end() )
{
if ( ((*it)->GetProtocol() == Protocol) && ((*it)->GetIp() == Ip) && (*it)->GetCallsign().HasSameCallsign(Callsign) )
if ( ((*it)->GetIp() == Ip) && (*it)->GetCallsign().HasSameCallsign(Callsign) )
{
return *it++;
}

15
clients.h

@ -59,16 +59,15 @@ public:
// find clients
std::shared_ptr<CClient> FindClient(const CIp &);
std::shared_ptr<CClient> FindClient(const CIp &, int);
std::shared_ptr<CClient> FindClient(const CIp &, int, char);
std::shared_ptr<CClient> FindClient(const CCallsign &, const CIp &, int);
std::shared_ptr<CClient> FindClient(const CCallsign &, char, const CIp &, int);
std::shared_ptr<CClient> FindClient(const CCallsign &, int);
std::shared_ptr<CClient> FindClient(const CIp &, char);
std::shared_ptr<CClient> FindClient(const CCallsign &, const CIp &);
std::shared_ptr<CClient> FindClient(const CCallsign &, char, const CIp &);
std::shared_ptr<CClient> FindClient(const CCallsign &);
// iterate on clients
std::shared_ptr<CClient> FindNextClient(int, std::list<std::shared_ptr<CClient>>::iterator &);
std::shared_ptr<CClient> FindNextClient(const CIp &, int, std::list<std::shared_ptr<CClient>>::iterator &);
std::shared_ptr<CClient> FindNextClient(const CCallsign &, const CIp &, int, std::list<std::shared_ptr<CClient>>::iterator &);
std::shared_ptr<CClient> FindNextClient(std::list<std::shared_ptr<CClient>>::iterator &);
std::shared_ptr<CClient> FindNextClient(const CIp &, std::list<std::shared_ptr<CClient>>::iterator &);
std::shared_ptr<CClient> FindNextClient(const CCallsign &, const CIp &, std::list<std::shared_ptr<CClient>>::iterator &);
protected:
// data

8
gatekeeper.cpp

@ -81,7 +81,7 @@ void CGateKeeper::Close(void)
////////////////////////////////////////////////////////////////////////////////////////
// authorizations
bool CGateKeeper::MayLink(const CCallsign &callsign, const CIp &ip, int protocol, char *modules) const
bool CGateKeeper::MayLink(const CCallsign &callsign, const CIp &ip, char *modules) const
{
bool ok;
if (callsign.GetCS(4).compare("M17-"))
@ -95,20 +95,20 @@ bool CGateKeeper::MayLink(const CCallsign &callsign, const CIp &ip, int protocol
if ( !ok )
{
std::cout << "Gatekeeper blocking linking of " << callsign << " @ " << ip << " using protocol " << protocol << std::endl;
std::cout << "Gatekeeper blocking linking of " << callsign << " @ " << ip << std::endl;
}
// done
return ok;
}
bool CGateKeeper::MayTransmit(const CCallsign &callsign, const CIp &ip, int protocol, char module) const
bool CGateKeeper::MayTransmit(const CCallsign &callsign, const CIp &ip, char module) const
{
bool ok = IsNodeListedOk(callsign, ip, module);
if ( !ok )
{
std::cout << "Gatekeeper blocking transmitting of " << callsign << " @ " << ip << " using protocol " << protocol << std::endl;
std::cout << "Gatekeeper blocking transmitting of " << callsign << " @ " << ip << std::endl;
}
// done

4
gatekeeper.h

@ -47,8 +47,8 @@ public:
void Close(void);
// authorizations
bool MayLink(const CCallsign &, const CIp &, int, char * = nullptr) const;
bool MayTransmit(const CCallsign &, const CIp &, int = PROTOCOL_ANY, char = ' ') const;
bool MayLink(const CCallsign &, const CIp &, char * = nullptr) const;
bool MayTransmit(const CCallsign &, const CIp &, char = ' ') const;
// peer list handeling
CPeerCallsignList *GetPeerList(void) { m_PeerList.Lock(); return &m_PeerList; }

1
m17client.h

@ -40,7 +40,6 @@ public:
virtual ~CM17Client() {};
// identity
int GetProtocol(void) const { return PROTOCOL_M17; }
const char *GetProtocolName(void) const { return "M17"; }
bool IsNode(void) const { return true; }

1
m17peer.h

@ -44,7 +44,6 @@ public:
bool IsAlive(void) const;
// identity
int GetProtocol(void) const { return PROTOCOL_M17; }
const char *GetProtocolName(void) const { return "M17"; }
// revision helper

34
m17protocol.cpp

@ -42,10 +42,10 @@ CM17Protocol::CM17Protocol()
////////////////////////////////////////////////////////////////////////////////////////
// operation
bool CM17Protocol::Initialize(int ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6)
bool CM17Protocol::Initialize(const uint16_t port, const bool has_ipv4, const bool has_ipv6)
{
// base class
if (! CProtocol::Initialize(ptype, port, has_ipv4, has_ipv6))
if (! CProtocol::Initialize(port, has_ipv4, has_ipv6))
return false;
// update time
@ -105,7 +105,7 @@ void CM17Protocol::Task(void)
std::cout << "CONN packet from " << cs << " at " << ip << " to module(s) " << mods << std::endl;
// callsign authorized?
if ( g_GateKeeper.MayLink(cs, ip, PROTOCOL_M17, mods) )
if ( g_GateKeeper.MayLink(cs, ip, mods) )
{
SInterConnect ackn;
// acknowledge the request
@ -125,11 +125,11 @@ void CM17Protocol::Task(void)
std::cout << "ACQN packet from " << cs << " at " << ip << " on module(s) " << mods << std::endl;
// callsign authorized?
if ( g_GateKeeper.MayLink(cs, ip, PROTOCOL_M17) )
if ( g_GateKeeper.MayLink(cs, ip) )
{
// already connected ?
CPeers *peers = g_Reflector.GetPeers();
if ( nullptr == peers->FindPeer(cs, ip, PROTOCOL_M17) )
if ( nullptr == peers->FindPeer(cs, ip) )
{
// create the new peer
// this also create one client per module
@ -149,7 +149,7 @@ void CM17Protocol::Task(void)
std::cout << "Connect packet for module " << mod << " from " << cs << " at " << ip << std::endl;
// callsign authorized?
if ( g_GateKeeper.MayLink(cs, ip, PROTOCOL_M17) )
if ( g_GateKeeper.MayLink(cs, ip) )
{
// valid module ?
if ( g_Reflector.IsValidModule(mod) )
@ -187,7 +187,7 @@ void CM17Protocol::Task(void)
CClients *clients = g_Reflector.GetClients();
auto it = clients->begin();
std::shared_ptr<CClient>client = nullptr;
while (nullptr != (client = clients->FindNextClient(cs, ip, PROTOCOL_M17, it)))
while (nullptr != (client = clients->FindNextClient(cs, ip, it)))
{
client->Alive();
}
@ -197,7 +197,7 @@ void CM17Protocol::Task(void)
{
// find peer
CPeers *peers = g_Reflector.GetPeers();
std::shared_ptr<CPeer>peer = peers->FindPeer(ip, PROTOCOL_M17);
std::shared_ptr<CPeer>peer = peers->FindPeer(ip);
if ( peer != nullptr )
{
// keep it alive
@ -212,7 +212,7 @@ void CM17Protocol::Task(void)
if (cs.GetCS(4).compare("M17-")) {
// find the regular client & remove it
CClients *clients = g_Reflector.GetClients();
std::shared_ptr<CClient>client = clients->FindClient(ip, PROTOCOL_M17);
std::shared_ptr<CClient>client = clients->FindClient(ip);
if ( client != nullptr )
{
// ack disconnect packet
@ -227,7 +227,7 @@ void CM17Protocol::Task(void)
{
// find the peer and remove it
CPeers *peers = g_Reflector.GetPeers();
std::shared_ptr<CPeer>peer = peers->FindPeer(ip, PROTOCOL_M17);
std::shared_ptr<CPeer>peer = peers->FindPeer(ip);
if ( peer )
{
// remove it from reflector peer list
@ -290,7 +290,7 @@ void CM17Protocol::HandleQueue(void)
CClients *clients = g_Reflector.GetClients();
auto it = clients->begin();
std::shared_ptr<CClient>client = nullptr;
while (nullptr != (client = clients->FindNextClient(PROTOCOL_M17, it)))
while (nullptr != (client = clients->FindNextClient(it)))
{
// is this client busy ?
if ( !client->IsAMaster() && (client->GetReflectorModule() == packet->GetDestModule()) )
@ -331,7 +331,7 @@ void CM17Protocol::HandleKeepalives(void)
CClients *clients = g_Reflector.GetClients();
auto it = clients->begin();
std::shared_ptr<CClient>client = nullptr;
while ( (client = clients->FindNextClient(PROTOCOL_M17, it)) != nullptr )
while ( nullptr != (client = clients->FindNextClient(it)) )
{
// don't ping reflector modules, we'll do each interlinked refectors below
if (0 == client->GetCallsign().GetCS(4).compare("M17-"))
@ -350,7 +350,7 @@ void CM17Protocol::HandleKeepalives(void)
else if ( !client->IsAlive() )
{
CPeers *peers = g_Reflector.GetPeers();
std::shared_ptr<CPeer>peer = peers->FindPeer(client->GetCallsign(), client->GetIp(), PROTOCOL_M17);
std::shared_ptr<CPeer>peer = peers->FindPeer(client->GetCallsign(), client->GetIp());
if ( (peer != nullptr) && (peer->GetReflectorModules()[0] == client->GetReflectorModule()) )
{
// no, but this is a peer client, so it will be handled below
@ -376,7 +376,7 @@ void CM17Protocol::HandleKeepalives(void)
CPeers *peers = g_Reflector.GetPeers();
auto pit = peers->begin();
std::shared_ptr<CPeer>peer = nullptr;
while ( nullptr != (peer = peers->FindNextPeer(PROTOCOL_M17, pit)) )
while ( nullptr != (peer = peers->FindNextPeer(pit)) )
{
// send keepalive
Send(keepalive, 10, peer->GetIp());
@ -417,7 +417,7 @@ void CM17Protocol::HandlePeerLinks(void)
// if not, disconnect
auto pit = peers->begin();
std::shared_ptr<CPeer>peer = nullptr;
while ( (peer = peers->FindNextPeer(PROTOCOL_M17, pit)) != nullptr )
while ( (peer = peers->FindNextPeer(pit)) != nullptr )
{
if ( list->FindListItem(peer->GetCallsign()) == nullptr )
{
@ -437,7 +437,7 @@ void CM17Protocol::HandlePeerLinks(void)
{
if ( (*it).GetCallsign().HasSameCallsignWithWildcard(CCallsign("M17-*")) )
{
if ( nullptr == peers->FindPeer((*it).GetCallsign(), PROTOCOL_M17) )
if ( nullptr == peers->FindPeer((*it).GetCallsign()) )
{
// send connect packet to re-initiate peer link
EncodeInterlinkConnectPacket(connect, (*it).GetModules());
@ -468,7 +468,7 @@ void CM17Protocol::OnFirstPacketIn(std::unique_ptr<CPacket> &packet, const CIp &
else
{
// find this client
std::shared_ptr<CClient>client = g_Reflector.GetClients()->FindClient(ip, PROTOCOL_M17);
std::shared_ptr<CClient>client = g_Reflector.GetClients()->FindClient(ip);
if ( client )
{
// save the source and destination for Hearing().

2
m17protocol.h

@ -37,7 +37,7 @@ class CM17Protocol : public CProtocol
public:
CM17Protocol();
// initialization
bool Initialize(const int ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6);
bool Initialize(const uint16_t port, const bool has_ipv4, const bool has_ipv6);
// task
void Task(void);

4
main.h

@ -63,10 +63,6 @@
// protocols ---------------------------------------------------
#define PROTOCOL_ANY -1
#define PROTOCOL_NONE 0
#define PROTOCOL_M17 1
// M17
#define M17_PORT 17000
#define M17_KEEPALIVE_PERIOD 3

1
peer.h

@ -59,7 +59,6 @@ public:
// set
// identity
virtual int GetProtocol(void) const { return PROTOCOL_NONE; }
virtual int GetProtocolRevision(void) const { return 0; }
virtual const char *GetProtocolName(void) const { return "none"; }

25
peers.cpp

@ -114,11 +114,11 @@ void CPeers::RemovePeer(std::shared_ptr<CPeer> peer)
////////////////////////////////////////////////////////////////////////////////////////
// find peers
std::shared_ptr<CPeer> CPeers::FindPeer(const CIp &Ip, int Protocol)
std::shared_ptr<CPeer> CPeers::FindPeer(const CIp &Ip)
{
for ( auto it=begin(); it!=end(); it++ )
{
if ( ((*it)->GetIp() == Ip) && ((*it)->GetProtocol() == Protocol))
if ( ((*it)->GetIp() == Ip) )
{
return *it;
}
@ -127,11 +127,11 @@ std::shared_ptr<CPeer> CPeers::FindPeer(const CIp &Ip, int Protocol)
return nullptr;
}
std::shared_ptr<CPeer> CPeers::FindPeer(const CCallsign &Callsign, const CIp &Ip, int Protocol)
std::shared_ptr<CPeer> CPeers::FindPeer(const CCallsign &Callsign, const CIp &Ip)
{
for ( auto it=begin(); it!=end(); it++ )
{
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) && ((*it)->GetIp() == Ip) && ((*it)->GetProtocol() == Protocol) )
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) && ((*it)->GetIp() == Ip) )
{
return *it;
}
@ -140,11 +140,11 @@ std::shared_ptr<CPeer> CPeers::FindPeer(const CCallsign &Callsign, const CIp &Ip
return nullptr;
}
std::shared_ptr<CPeer> CPeers::FindPeer(const CCallsign &Callsign, int Protocol)
std::shared_ptr<CPeer> CPeers::FindPeer(const CCallsign &Callsign)
{
for ( auto it=begin(); it!=end(); it++ )
{
if ( ((*it)->GetProtocol() == Protocol) && (*it)->GetCallsign().HasSameCallsign(Callsign) )
if ( (*it)->GetCallsign().HasSameCallsign(Callsign) )
{
return *it;
}
@ -157,15 +157,10 @@ std::shared_ptr<CPeer> CPeers::FindPeer(const CCallsign &Callsign, int Protocol)
////////////////////////////////////////////////////////////////////////////////////////
// iterate on peers
std::shared_ptr<CPeer> CPeers::FindNextPeer(int Protocol, std::list<std::shared_ptr<CPeer>>::iterator &it)
std::shared_ptr<CPeer> CPeers::FindNextPeer(std::list<std::shared_ptr<CPeer>>::iterator &it)
{
while ( it!=end() )
{
if ( (*it)->GetProtocol() == Protocol )
{
return *it++;
}
it++;
}
if ( it!=end() )
return *it++;
return nullptr;
}

8
peers.h

@ -60,12 +60,12 @@ public:
std::list<std::shared_ptr<CPeer>>::const_iterator cend() const { return m_Peers.cend(); }
// find peers
std::shared_ptr<CPeer> FindPeer(const CIp &, int);
std::shared_ptr<CPeer> FindPeer(const CCallsign &, const CIp &, int);
std::shared_ptr<CPeer> FindPeer(const CCallsign &, int);
std::shared_ptr<CPeer> FindPeer(const CIp &);
std::shared_ptr<CPeer> FindPeer(const CCallsign &, const CIp &);
std::shared_ptr<CPeer> FindPeer(const CCallsign &);
// iterate on peers
std::shared_ptr<CPeer> FindNextPeer(int, std::list<std::shared_ptr<CPeer>>::iterator &);
std::shared_ptr<CPeer> FindNextPeer(std::list<std::shared_ptr<CPeer>>::iterator &);
protected:
// data

2
protocol.cpp

@ -56,7 +56,7 @@ CProtocol::~CProtocol()
////////////////////////////////////////////////////////////////////////////////////////
// initialization
bool CProtocol::Initialize(int ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6)
bool CProtocol::Initialize(const uint16_t port, const bool has_ipv4, const bool has_ipv6)
{
// init reflector apparent callsign
m_ReflectorCallsign = g_Reflector.GetCallsign();

2
protocol.h

@ -44,7 +44,7 @@ public:
virtual ~CProtocol();
// initialization
virtual bool Initialize(const int ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6);
virtual bool Initialize(const uint16_t port, const bool has_ipv4, const bool has_ipv6);
virtual void Close(void);
// queue

2
reflector.cpp

@ -74,7 +74,7 @@ bool CReflector::Start(void)
g_GateKeeper.Init();
// create protocols
if (! m_Protocol.Initialize(PROTOCOL_M17, M17_PORT, true, true))
if (! m_Protocol.Initialize(M17_PORT, true, true))
{
m_Protocol.Close();
return false;

Loading…
Cancel
Save