2015-12-29 17:45:47 -05:00
|
|
|
//
|
|
|
|
// cgatekeeper.cpp
|
|
|
|
// xlxd
|
|
|
|
//
|
|
|
|
// Created by Jean-Luc Deltombe (LX3JL) on 07/11/2015.
|
|
|
|
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
|
|
|
//
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// This file is part of xlxd.
|
|
|
|
//
|
|
|
|
// xlxd 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 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-12-30 12:04:27 -05:00
|
|
|
#include "main.h"
|
|
|
|
#include "ctimepoint.h"
|
2015-12-29 17:45:47 -05:00
|
|
|
#include "cgatekeeper.h"
|
|
|
|
|
2015-12-30 12:04:27 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-12-29 17:45:47 -05:00
|
|
|
CGateKeeper g_GateKeeper;
|
2015-12-30 12:04:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// constructor
|
|
|
|
|
|
|
|
CGateKeeper::CGateKeeper()
|
|
|
|
{
|
|
|
|
m_bStopThread = false;
|
|
|
|
m_pThread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// destructor
|
|
|
|
|
|
|
|
CGateKeeper::~CGateKeeper()
|
|
|
|
{
|
|
|
|
// kill threads
|
|
|
|
m_bStopThread = true;
|
|
|
|
if ( m_pThread != NULL )
|
|
|
|
{
|
|
|
|
m_pThread->join();
|
|
|
|
delete m_pThread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// init & clode
|
|
|
|
|
|
|
|
bool CGateKeeper::Init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
// load lists from files
|
2016-02-11 12:51:12 -05:00
|
|
|
m_NodeWhiteList.LoadFromFile(WHITELIST_PATH);
|
|
|
|
m_NodeBlackList.LoadFromFile(BLACKLIST_PATH);
|
|
|
|
m_PeerList.LoadFromFile(INTERLINKLIST_PATH);
|
2015-12-30 12:04:27 -05:00
|
|
|
|
|
|
|
// reset stop flag
|
|
|
|
m_bStopThread = false;
|
|
|
|
|
|
|
|
// start thread;
|
|
|
|
m_pThread = new std::thread(CGateKeeper::Thread, this);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGateKeeper::Close(void)
|
|
|
|
{
|
|
|
|
m_bStopThread = true;
|
|
|
|
if ( m_pThread != NULL )
|
|
|
|
{
|
|
|
|
m_pThread->join();
|
|
|
|
delete m_pThread;
|
|
|
|
m_pThread = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
2016-02-11 12:51:12 -05:00
|
|
|
// authorisations
|
2015-12-30 12:04:27 -05:00
|
|
|
|
2016-02-11 12:51:12 -05:00
|
|
|
bool CGateKeeper::MayLink(const CCallsign &callsign, const CIp &ip, int protocol, char *modules) const
|
2015-12-30 12:04:27 -05:00
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
2016-02-11 12:51:12 -05:00
|
|
|
switch (protocol)
|
|
|
|
{
|
|
|
|
// repeaters
|
|
|
|
case PROTOCOL_DEXTRA:
|
|
|
|
case PROTOCOL_DPLUS:
|
|
|
|
case PROTOCOL_DCS:
|
|
|
|
// first check is IP & callsigned listed OK
|
|
|
|
ok &= IsNodeListedOk(callsign, ip);
|
|
|
|
// todo: then apply any protocol specific authorisation for the operation
|
|
|
|
break;
|
|
|
|
|
|
|
|
// XLX interlinks
|
|
|
|
case PROTOCOL_XLX:
|
|
|
|
ok &= IsPeerListedOk(callsign, ip, modules);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// unsupported
|
|
|
|
case PROTOCOL_NONE:
|
|
|
|
default:
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-30 12:04:27 -05:00
|
|
|
|
|
|
|
// report
|
|
|
|
if ( !ok )
|
|
|
|
{
|
|
|
|
std::cout << "Gatekeeper blocking linking of " << callsign << " @ " << ip << " using protocol " << protocol << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2016-02-11 12:51:12 -05:00
|
|
|
bool CGateKeeper::MayTransmit(const CCallsign &callsign, const CIp &ip, int protocol, char module) const
|
2015-12-30 12:04:27 -05:00
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
2016-02-11 12:51:12 -05:00
|
|
|
switch (protocol)
|
|
|
|
{
|
|
|
|
// repeaters, protocol specific
|
|
|
|
case PROTOCOL_ANY:
|
|
|
|
case PROTOCOL_DEXTRA:
|
|
|
|
case PROTOCOL_DPLUS:
|
|
|
|
case PROTOCOL_DCS:
|
|
|
|
// first check is IP & callsigned listed OK
|
|
|
|
ok &= IsNodeListedOk(callsign, ip);
|
|
|
|
// todo: then apply any protocol specific authorisation for the operation
|
|
|
|
break;
|
|
|
|
|
|
|
|
// XLX interlinks
|
|
|
|
case PROTOCOL_XLX:
|
|
|
|
ok &= IsPeerListedOk(callsign, ip, module);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// unsupported
|
|
|
|
case PROTOCOL_NONE:
|
|
|
|
default:
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-30 12:04:27 -05:00
|
|
|
|
|
|
|
// report
|
|
|
|
if ( !ok )
|
|
|
|
{
|
|
|
|
std::cout << "Gatekeeper blocking transmiting of " << callsign << " @ " << ip << " using protocol " << protocol << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// thread
|
|
|
|
|
|
|
|
void CGateKeeper::Thread(CGateKeeper *This)
|
|
|
|
{
|
|
|
|
while ( !This->m_bStopThread )
|
|
|
|
{
|
|
|
|
// Wait 30 seconds
|
|
|
|
CTimePoint::TaskSleepFor(30000);
|
|
|
|
|
|
|
|
// have lists files changed ?
|
2016-02-11 12:51:12 -05:00
|
|
|
if ( This->m_NodeWhiteList.NeedReload() )
|
|
|
|
{
|
|
|
|
This->m_NodeWhiteList.ReloadFromFile();
|
|
|
|
}
|
|
|
|
if ( This->m_NodeBlackList.NeedReload() )
|
2015-12-30 12:04:27 -05:00
|
|
|
{
|
2016-02-11 12:51:12 -05:00
|
|
|
This->m_NodeBlackList.ReloadFromFile();
|
2015-12-30 12:04:27 -05:00
|
|
|
}
|
2016-02-11 12:51:12 -05:00
|
|
|
if ( This->m_PeerList.NeedReload() )
|
2015-12-30 12:04:27 -05:00
|
|
|
{
|
2016-02-11 12:51:12 -05:00
|
|
|
This->m_PeerList.ReloadFromFile();
|
2015-12-30 12:04:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// operation helpers
|
|
|
|
|
2016-02-11 12:51:12 -05:00
|
|
|
bool CGateKeeper::IsNodeListedOk(const CCallsign &callsign, const CIp &ip) const
|
2015-12-30 12:04:27 -05:00
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
// first check IP
|
|
|
|
|
|
|
|
// next, check callsign
|
|
|
|
if ( ok )
|
|
|
|
{
|
|
|
|
// first check if callsign is in white list
|
|
|
|
// note if white list is empty, everybody is authorized
|
2016-02-11 12:51:12 -05:00
|
|
|
const_cast<CCallsignList &>(m_NodeWhiteList).Lock();
|
|
|
|
if ( !m_NodeWhiteList.empty() )
|
2015-12-30 12:04:27 -05:00
|
|
|
{
|
2016-02-11 12:51:12 -05:00
|
|
|
ok = m_NodeWhiteList.IsCallsignListed(callsign);
|
2015-12-30 12:04:27 -05:00
|
|
|
}
|
2016-02-11 12:51:12 -05:00
|
|
|
const_cast<CCallsignList &>(m_NodeWhiteList).Unlock();
|
2015-12-30 12:04:27 -05:00
|
|
|
|
|
|
|
// then check if not blacklisted
|
2016-02-11 12:51:12 -05:00
|
|
|
const_cast<CCallsignList &>(m_NodeBlackList).Lock();
|
|
|
|
ok &= !m_NodeBlackList.IsCallsignListed(callsign);
|
|
|
|
const_cast<CCallsignList &>(m_NodeBlackList).Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
return ok;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGateKeeper::IsPeerListedOk(const CCallsign &callsign, const CIp &ip, char module) const
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
// first check IP
|
|
|
|
|
|
|
|
// next, check callsign
|
|
|
|
if ( ok )
|
|
|
|
{
|
|
|
|
// look for an exact match in the list
|
|
|
|
const_cast<CPeerCallsignList &>(m_PeerList).Lock();
|
|
|
|
if ( !m_PeerList.empty() )
|
|
|
|
{
|
|
|
|
ok = m_PeerList.IsCallsignListed(callsign, module);
|
|
|
|
}
|
|
|
|
const_cast<CPeerCallsignList &>(m_PeerList).Unlock();
|
2015-12-30 12:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
return ok;
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGateKeeper::IsPeerListedOk(const CCallsign &callsign, const CIp &ip, char *modules) const
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
// first check IP
|
2015-12-30 12:04:27 -05:00
|
|
|
|
2016-02-11 12:51:12 -05:00
|
|
|
// next, check callsign
|
|
|
|
if ( ok )
|
|
|
|
{
|
|
|
|
// look for an exact match in the list
|
|
|
|
const_cast<CPeerCallsignList &>(m_PeerList).Lock();
|
|
|
|
if ( !m_PeerList.empty() )
|
|
|
|
{
|
|
|
|
ok = m_PeerList.IsCallsignListed(callsign, modules);
|
|
|
|
}
|
|
|
|
const_cast<CPeerCallsignList &>(m_PeerList).Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
return ok;
|
2015-12-30 12:04:27 -05:00
|
|
|
}
|
2016-02-11 12:51:12 -05:00
|
|
|
|