mirror of https://github.com/LX3JL/xlxd.git
commit
28e1c10f30
@ -0,0 +1,94 @@
|
||||
//
|
||||
// cagc.cpp
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 28/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA AGC code borrowed from Liquid DSP
|
||||
// Only took the parts we need qnd recoeded it to be close the XLX coding style
|
||||
// https://github.com/jgaeddert/liquid-dsp/blob/master/src/agc/src/agc.c
|
||||
|
||||
#include <math.h>
|
||||
#include "cagc.h"
|
||||
#include "main.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// constructor
|
||||
|
||||
CAGC::CAGC(float initialLeveldB)
|
||||
{
|
||||
// set internal gain appropriately
|
||||
m_Gain = pow(10.0f, initialLeveldB/20.0f);
|
||||
//+- 10dB Margin, TODO Move margin to constant
|
||||
m_GainMax = pow(10.0f, (initialLeveldB + AGC_CLAMPING)/20.0f);
|
||||
m_GainMin = pow(10.0f, (initialLeveldB - AGC_CLAMPING)/20.0f);
|
||||
|
||||
m_EnergyPrime = 1.0f;
|
||||
|
||||
// We do not target full scale to avoid stauration
|
||||
m_targetEnergy = 32767.0f * pow(10.0f, (initialLeveldB - 25.0)/20.0f);//25 dB below saturation as stated in docs
|
||||
//we also substract our target gain
|
||||
|
||||
//this is the time constant of our AGC...
|
||||
m_Bandwidth = 1e-2f;//TODO : Move to parameter ?
|
||||
m_Alpha = m_Bandwidth;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// get
|
||||
|
||||
float CAGC::GetGain()
|
||||
{
|
||||
return 20.0f*log10(m_Gain);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// process
|
||||
|
||||
inline void CAGC::ProcessSampleBlock(uint8* voice, int length)
|
||||
{
|
||||
for(int i = 0; i < length; i += 2)
|
||||
{
|
||||
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
||||
//apply AGC
|
||||
// apply gain to input sample
|
||||
float output = input * m_Gain;
|
||||
|
||||
// compute output signal energy, scaled to 0 to 1
|
||||
float instantEnergy = abs(output) / m_targetEnergy;
|
||||
|
||||
// smooth energy estimate using single-pole low-pass filter
|
||||
m_EnergyPrime = (1.0f - m_Alpha) * m_EnergyPrime + m_Alpha * instantEnergy;
|
||||
|
||||
// update gain according to output energy
|
||||
if (m_EnergyPrime > 1e-6f)
|
||||
m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) );
|
||||
|
||||
// clamp gain
|
||||
if (m_Gain > m_GainMax)
|
||||
m_Gain = m_GainMax;
|
||||
else if(m_Gain < m_GainMin)
|
||||
m_Gain = m_GainMin;
|
||||
|
||||
//write processed sample back
|
||||
voice[i] = HIBYTE((short)output);
|
||||
voice[i+1] = LOBYTE((short)output);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
//
|
||||
// cagc.h
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA AGC code largely inspired by Liquid DSP
|
||||
// Only took the parts we need qnd recoeded it to be close the XLX coding style
|
||||
// https://github.com/jgaeddert/liquid-dsp/blob/master/src/agc/src/agc.c
|
||||
|
||||
#ifndef cagc_h
|
||||
#define cagc_h
|
||||
|
||||
#include "csampleblockprocessor.h"
|
||||
|
||||
class CAGC : CSampleBlockProcessor
|
||||
{
|
||||
public:
|
||||
//Constructor
|
||||
CAGC(float initialLeveldB);
|
||||
|
||||
//methods
|
||||
void ProcessSampleBlock(uint8* voice, int length) ;
|
||||
float GetGain();//gets current gain
|
||||
|
||||
private:
|
||||
float m_Gain; // current gain value
|
||||
float m_GainMax, m_GainMin; //gain clamping
|
||||
float m_targetEnergy; // scale value for target energy
|
||||
|
||||
// gain control loop filter parameters
|
||||
float m_Bandwidth; // bandwidth-time constant
|
||||
float m_Alpha; // feed-back gain
|
||||
|
||||
// signal level estimate
|
||||
float m_EnergyPrime; // filtered output signal energy estimate
|
||||
};
|
||||
|
||||
#endif /* cgc_h */
|
||||
@ -0,0 +1,75 @@
|
||||
//
|
||||
// cfirfilter.cpp
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// FIRFilter by Geoffrey Merck F4FXL / KC3FRA
|
||||
|
||||
#include "cfirfilter.h"
|
||||
#include <string.h>
|
||||
|
||||
CFIRFilter::CFIRFilter(const float* taps, int tapsLength)
|
||||
{
|
||||
m_taps = new float[tapsLength];
|
||||
m_buffer = new float[tapsLength];
|
||||
m_tapsLength = tapsLength;
|
||||
|
||||
::memcpy(m_taps, taps, tapsLength * sizeof(float));
|
||||
::memset(m_buffer, 0, tapsLength * sizeof(float));
|
||||
m_currentBufferPosition = 0;
|
||||
}
|
||||
|
||||
CFIRFilter::~CFIRFilter()
|
||||
{
|
||||
delete[] m_taps;
|
||||
delete[] m_buffer;
|
||||
}
|
||||
|
||||
inline void CFIRFilter::ProcessSampleBlock(uint8* voice, int length)
|
||||
{
|
||||
for(int i = 0; i < length; i += 2)
|
||||
{
|
||||
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
||||
float output = 0.0f;
|
||||
int iTaps = 0;
|
||||
|
||||
// Buffer latest sample into delay line
|
||||
m_buffer[m_currentBufferPosition] = input;
|
||||
|
||||
for(int i = m_currentBufferPosition; i >= 0; i--)
|
||||
{
|
||||
output += m_taps[iTaps++] * m_buffer[i];
|
||||
}
|
||||
|
||||
for(int i = m_tapsLength - 1; i > m_currentBufferPosition; i--)
|
||||
{
|
||||
output += m_taps[iTaps++] * m_buffer[i];
|
||||
}
|
||||
|
||||
m_currentBufferPosition = (m_currentBufferPosition + 1) % m_tapsLength;
|
||||
|
||||
//write processed sample back
|
||||
voice[i] = HIBYTE((short)output);
|
||||
voice[i+1] = LOBYTE((short)output);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,51 @@
|
||||
//
|
||||
// cfirfilter.h
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// FIRFilter by Geoffrey Merck F4FXL / KC3FRA
|
||||
|
||||
#ifndef cfirfilter_h
|
||||
#define cfirfilter_h
|
||||
|
||||
#include "csampleblockprocessor.h"
|
||||
|
||||
class CFIRFilter : CSampleBlockProcessor
|
||||
{
|
||||
public :
|
||||
//Constructor
|
||||
CFIRFilter(const float* taps, int tapsLength);
|
||||
|
||||
// Destructor
|
||||
~CFIRFilter();
|
||||
|
||||
// Processing
|
||||
void ProcessSampleBlock(uint8* voice, int length);
|
||||
|
||||
private:
|
||||
float* m_taps;
|
||||
int m_tapsLength;
|
||||
float* m_buffer;
|
||||
int m_currentBufferPosition;
|
||||
};
|
||||
|
||||
#endif //cfirfilter_h
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
//
|
||||
// cfixedgain.cpp
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 28/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA AGC
|
||||
|
||||
#include "cfixedgain.h"
|
||||
#include <math.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// constructor
|
||||
|
||||
CFixedGain::CFixedGain(float gaindB)
|
||||
{
|
||||
m_gaindB = gaindB;
|
||||
m_gainLinear = pow(10.0f, m_gaindB/20.0f);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// processing
|
||||
|
||||
inline void CFixedGain::ProcessSampleBlock(uint8* voice, int length)
|
||||
{
|
||||
for(int i = 0; i < length; i += 2)
|
||||
{
|
||||
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
||||
//apply gain
|
||||
float output = input * m_gainLinear;
|
||||
|
||||
//write processed sample back
|
||||
voice[i] = HIBYTE((short)output);
|
||||
voice[i+1] = LOBYTE((short)output);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
//
|
||||
// cfixedgain.h
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA
|
||||
|
||||
#ifndef cfixedgain_h
|
||||
#define cfixedgain_h
|
||||
|
||||
#include "csampleblockprocessor.h"
|
||||
|
||||
class CFixedGain : CSampleBlockProcessor
|
||||
{
|
||||
public:
|
||||
//Constructor
|
||||
CFixedGain(float gaindB);
|
||||
|
||||
//processing
|
||||
void ProcessSampleBlock(uint8* voice, int length);
|
||||
|
||||
private:
|
||||
float m_gaindB; //gain in dB
|
||||
float m_gainLinear; //linearized gain
|
||||
};
|
||||
|
||||
#endif /* cfixedgain_h */
|
||||
@ -0,0 +1,38 @@
|
||||
//
|
||||
// csampleprocessor.h
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA
|
||||
|
||||
#ifndef csamplebloclprocessor_h
|
||||
#define csamplebloclprocessor_h
|
||||
|
||||
#include "main.h"
|
||||
|
||||
class CSampleBlockProcessor
|
||||
{
|
||||
public:
|
||||
//processing
|
||||
virtual void ProcessSampleBlock(uint8* voice, int length) = 0;
|
||||
};
|
||||
|
||||
#endif /* csampleprocessor_h */
|
||||
@ -0,0 +1,94 @@
|
||||
//
|
||||
// cagc.cpp
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 28/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA AGC
|
||||
|
||||
#include "main.h"
|
||||
#include "csignalprocessor.h"
|
||||
|
||||
#if USE_AGC == 1
|
||||
#include "cagc.h"
|
||||
#else
|
||||
#include "cfixedgain.h"
|
||||
#endif
|
||||
|
||||
#if USE_BANDPASSFILTER == 1
|
||||
#include "cfirfilter.h"
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// constructor
|
||||
|
||||
CSignalProcessor::CSignalProcessor(float gaindB)
|
||||
{
|
||||
#if USE_BANDPASSFILTER
|
||||
m_sampleProcessors.push_back((CSampleBlockProcessor*)new CFIRFilter(FILTER_TAPS, FILTER_TAPS_LENGTH));
|
||||
#endif
|
||||
#if USE_AGC == 1
|
||||
m_sampleProcessors.push_back((CSampleBlockProcessor*)new CAGC(gaindB));
|
||||
#else
|
||||
m_sampleProcessors.push_back((CSampleBlockProcessor*)new CFixedGain(gaindB));
|
||||
#endif
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// destructor
|
||||
|
||||
CSignalProcessor::~CSignalProcessor()
|
||||
{
|
||||
for(int i = 0; i < m_sampleProcessors.size(); i++)
|
||||
{
|
||||
delete m_sampleProcessors[i];
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// processing
|
||||
|
||||
void CSignalProcessor::Process(uint8* voice, int length)
|
||||
{
|
||||
/*float sample;
|
||||
int j;*/
|
||||
auto processorsSize = m_sampleProcessors.size();
|
||||
|
||||
for(int j = 0; j < processorsSize; j++)
|
||||
{
|
||||
m_sampleProcessors[j]->ProcessSampleBlock(voice, length);
|
||||
}
|
||||
|
||||
/*for(int i = 0; i < length; i += 2)
|
||||
{
|
||||
//Get the sample
|
||||
sample = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
||||
|
||||
for(j = 0; j < processorsSize; j++)
|
||||
{
|
||||
sample = m_sampleProcessors[j]->ProcessSample(sample);
|
||||
}
|
||||
|
||||
//write processed sample back
|
||||
voice[i] = HIBYTE((short)sample);
|
||||
voice[i+1] = LOBYTE((short)sample);
|
||||
}*/
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
//
|
||||
// csignalprocessor.h
|
||||
// ambed
|
||||
//
|
||||
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
|
||||
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file is part of ambed.
|
||||
//
|
||||
// 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
// Geoffrey Merck F4FXL / KC3FRA
|
||||
|
||||
#ifndef csignalprocessor_h
|
||||
#define csignalprocessor_h
|
||||
|
||||
#include <vector>
|
||||
#include "csampleblockprocessor.h"
|
||||
|
||||
class CSignalProcessor
|
||||
{
|
||||
public:
|
||||
//Constructor
|
||||
CSignalProcessor(float gaindB);
|
||||
|
||||
//Destructor
|
||||
~CSignalProcessor();
|
||||
|
||||
//Processing
|
||||
void Process(uint8* voice, int length);
|
||||
|
||||
private:
|
||||
std::vector<CSampleBlockProcessor *> m_sampleProcessors;
|
||||
};
|
||||
|
||||
#endif /* csignalprocessor_h */
|
||||
@ -0,0 +1,17 @@
|
||||
#########################################################################################
|
||||
# XLXD terminal option file
|
||||
#
|
||||
# one line per entry
|
||||
# each entry specifies a terminal option
|
||||
#
|
||||
# Valid option:
|
||||
# address <ip> - Ip address to be used by the terminal route responder
|
||||
# By default, the request destination address is used.
|
||||
# If the system is behind a router, set it to the public IP
|
||||
# If the system runs on the public IP, leave unset.
|
||||
# modules <modules> - a string with all modules to accept a terminal connection
|
||||
# Default value is "*", meaning accept all
|
||||
#
|
||||
#########################################################################################
|
||||
#address 193.1.2.3
|
||||
#modules BCD
|
||||
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
// Always start this first
|
||||
session_start();
|
||||
|
||||
// Destroying the session clears the $_SESSION variable, thus "logging" the user
|
||||
// out. This also happens automatically when the browser is closed
|
||||
session_destroy();
|
||||
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
<script>
|
||||
|
||||
function closeMe()
|
||||
{
|
||||
var win=window.open("","_self");
|
||||
win.close();
|
||||
}
|
||||
</script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<form>
|
||||
<input type="button" name="Close"
|
||||
onclick="closeMe()" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -0,0 +1,20 @@
|
||||
*************************************************
|
||||
*copy xlxd into /etc.init.d/
|
||||
*copy ambed.service into /etc/systemd/system/
|
||||
*
|
||||
*************************************************
|
||||
* xlxd executable must be in /xlxd/ folder
|
||||
* ambed executable must be in /ambed/ folder
|
||||
*
|
||||
*************************************************
|
||||
* possible options:
|
||||
*
|
||||
* #systemctl start ambed /starts ambed
|
||||
* #systemctl status ambed /shows status of ambed
|
||||
* #systemctl stop ambed /stops ambed
|
||||
* # systemctl restart ambed /restarts ambed
|
||||
*
|
||||
* automatically get it to start on boot:
|
||||
* #systemctl enable ambed
|
||||
*
|
||||
*************************************************
|
||||
@ -0,0 +1,54 @@
|
||||
//
|
||||
// cg3client.cpp
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 03/06/2019.
|
||||
// Copyright © 2019 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "main.h"
|
||||
#include "cg3client.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// constructors
|
||||
|
||||
CG3Client::CG3Client()
|
||||
{
|
||||
}
|
||||
|
||||
CG3Client::CG3Client(const CCallsign &callsign, const CIp &ip, char reflectorModule)
|
||||
: CClient(callsign, ip, reflectorModule)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CG3Client::CG3Client(const CG3Client &client)
|
||||
: CClient(client)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// status
|
||||
|
||||
bool CG3Client::IsAlive(void) const
|
||||
{
|
||||
return (m_LastKeepaliveTime.DurationSinceNow() < G3_KEEPALIVE_TIMEOUT);
|
||||
}
|
||||
|
||||
@ -0,0 +1,62 @@
|
||||
//
|
||||
// cg3client.h
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 03/06/2019.
|
||||
// Copyright © 2019 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#ifndef cg3client_h
|
||||
#define cg3client_h
|
||||
|
||||
#include "cclient.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// define
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// class
|
||||
|
||||
class CG3Client : public CClient
|
||||
{
|
||||
public:
|
||||
// constructors
|
||||
CG3Client();
|
||||
CG3Client(const CCallsign &, const CIp &, char = ' ');
|
||||
CG3Client(const CG3Client &);
|
||||
|
||||
// destructor
|
||||
virtual ~CG3Client() {};
|
||||
|
||||
// identity
|
||||
int GetProtocol(void) const { return PROTOCOL_G3; }
|
||||
const char *GetProtocolName(void) const { return "Terminal/AP"; }
|
||||
int GetCodec(void) const { return CODEC_AMBEPLUS; }
|
||||
bool IsNode(void) const { return true; }
|
||||
|
||||
// status
|
||||
bool IsAlive(void) const;
|
||||
|
||||
protected:
|
||||
// data
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif /* cg3client_h */
|
||||
@ -0,0 +1,861 @@
|
||||
//
|
||||
// cg3protocol.cpp
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 03/06/2019.
|
||||
// Copyright © 2019 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "main.h"
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include "cg3client.h"
|
||||
#include "cg3protocol.h"
|
||||
#include "creflector.h"
|
||||
#include "cgatekeeper.h"
|
||||
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip_icmp.h>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// operation
|
||||
|
||||
bool CG3Protocol::Init(void)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ReadOptions();
|
||||
|
||||
// base class
|
||||
ok = CProtocol::Init();
|
||||
|
||||
// update reflector callsign
|
||||
m_ReflectorCallsign.PatchCallsign(0, (const uint8 *)"XLX", 3);
|
||||
|
||||
// create our DV socket
|
||||
ok &= m_Socket.Open(G3_DV_PORT);
|
||||
if ( !ok )
|
||||
{
|
||||
std::cout << "Error opening socket on port UDP" << G3_DV_PORT << " on ip " << g_Reflector.GetListenIp() << std::endl;
|
||||
}
|
||||
|
||||
//create helper sockets
|
||||
ok &= m_PresenceSocket.Open(G3_PRESENCE_PORT);
|
||||
if ( !ok )
|
||||
{
|
||||
std::cout << "Error opening socket on port UDP" << G3_PRESENCE_PORT << " on ip " << g_Reflector.GetListenIp() << std::endl;
|
||||
}
|
||||
|
||||
ok &= m_ConfigSocket.Open(G3_CONFIG_PORT);
|
||||
if ( !ok )
|
||||
{
|
||||
std::cout << "Error opening socket on port UDP" << G3_CONFIG_PORT << " on ip " << g_Reflector.GetListenIp() << std::endl;
|
||||
}
|
||||
|
||||
ok &= m_IcmpRawSocket.Open(IPPROTO_ICMP);
|
||||
if ( !ok )
|
||||
{
|
||||
std::cout << "Error opening raw socket for ICMP" << std::endl;
|
||||
}
|
||||
|
||||
if (ok)
|
||||
{
|
||||
// start helper threads
|
||||
m_pPresenceThread = new std::thread(PresenceThread, this);
|
||||
m_pPresenceThread = new std::thread(ConfigThread, this);
|
||||
m_pPresenceThread = new std::thread(IcmpThread, this);
|
||||
}
|
||||
|
||||
// update time
|
||||
m_LastKeepaliveTime.Now();
|
||||
|
||||
// done
|
||||
return ok;
|
||||
}
|
||||
|
||||
void CG3Protocol::Close(void)
|
||||
{
|
||||
if (m_pPresenceThread != NULL)
|
||||
{
|
||||
m_pPresenceThread->join();
|
||||
delete m_pPresenceThread;
|
||||
m_pPresenceThread = NULL;
|
||||
}
|
||||
|
||||
if (m_pConfigThread != NULL)
|
||||
{
|
||||
m_pConfigThread->join();
|
||||
delete m_pConfigThread;
|
||||
m_pConfigThread = NULL;
|
||||
}
|
||||
|
||||
if (m_pIcmpThread != NULL)
|
||||
{
|
||||
m_pIcmpThread->join();
|
||||
delete m_pIcmpThread;
|
||||
m_pIcmpThread = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// private threads
|
||||
|
||||
void CG3Protocol::PresenceThread(CG3Protocol *This)
|
||||
{
|
||||
while ( !This->m_bStopThread )
|
||||
{
|
||||
This->PresenceTask();
|
||||
}
|
||||
}
|
||||
|
||||
void CG3Protocol::ConfigThread(CG3Protocol *This)
|
||||
{
|
||||
while ( !This->m_bStopThread )
|
||||
{
|
||||
This->ConfigTask();
|
||||
}
|
||||
}
|
||||
|
||||
void CG3Protocol::IcmpThread(CG3Protocol *This)
|
||||
{
|
||||
while ( !This->m_bStopThread )
|
||||
{
|
||||
This->IcmpTask();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// presence task
|
||||
|
||||
void CG3Protocol::PresenceTask(void)
|
||||
{
|
||||
CBuffer Buffer;
|
||||
CIp ReqIp;
|
||||
CCallsign Callsign;
|
||||
CCallsign Owner;
|
||||
CCallsign Terminal;
|
||||
|
||||
|
||||
if ( m_PresenceSocket.Receive(&Buffer, &ReqIp, 20) != -1 )
|
||||
{
|
||||
|
||||
CIp Ip(ReqIp);
|
||||
Ip.GetSockAddr()->sin_port = htons(G3_DV_PORT);
|
||||
|
||||
if (Buffer.size() == 32)
|
||||
{
|
||||
Callsign.SetCallsign(&Buffer.data()[8], 8);
|
||||
Owner.SetCallsign(&Buffer.data()[16], 8);
|
||||
Terminal.SetCallsign(&Buffer.data()[24], 8);
|
||||
|
||||
std::cout << "Presence from " << Ip << " as " << Callsign << " on terminal " << Terminal << std::endl;
|
||||
|
||||
// accept
|
||||
Buffer.data()[2] = 0x80; // response
|
||||
Buffer.data()[3] = 0x00; // ok
|
||||
|
||||
if (m_GwAddress == 0)
|
||||
{
|
||||
Buffer.Append(*(uint32 *)m_ConfigSocket.GetLocalAddr());
|
||||
}
|
||||
else
|
||||
{
|
||||
Buffer.Append(m_GwAddress);
|
||||
}
|
||||
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
|
||||
int index = -1;
|
||||
CClient *extant = NULL;
|
||||
while ( (extant = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
CIp ClIp = extant->GetIp();
|
||||
if (ClIp.GetAddr() == Ip.GetAddr())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (extant == NULL)
|
||||
{
|
||||
index = -1;
|
||||
|
||||
// do we already have a client with the same call (IP changed)?
|
||||
while ( (extant = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
{
|
||||
if (extant->GetCallsign().HasSameCallsign(Terminal))
|
||||
{
|
||||
//delete old client
|
||||
clients->RemoveClient(extant);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create new client
|
||||
CG3Client *client = new CG3Client(Terminal, Ip);
|
||||
|
||||
// and append
|
||||
clients->AddClient(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
// client changed callsign
|
||||
if (!extant->GetCallsign().HasSameCallsign(Terminal))
|
||||
{
|
||||
//delete old client
|
||||
clients->RemoveClient(extant);
|
||||
|
||||
// create new client
|
||||
CG3Client *client = new CG3Client(Terminal, Ip);
|
||||
|
||||
// and append
|
||||
clients->AddClient(client);
|
||||
}
|
||||
}
|
||||
g_Reflector.ReleaseClients();
|
||||
|
||||
m_PresenceSocket.Send(Buffer, ReqIp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// configuration task
|
||||
|
||||
void CG3Protocol::ConfigTask(void)
|
||||
{
|
||||
CBuffer Buffer;
|
||||
CIp Ip;
|
||||
CCallsign Call;
|
||||
bool isRepeaterCall;
|
||||
|
||||
if ( m_ConfigSocket.Receive(&Buffer, &Ip, 20) != -1 )
|
||||
{
|
||||
|
||||
if (Buffer.size() == 16)
|
||||
{
|
||||
if (memcmp(&Buffer.data()[8], " ", 8) == 0)
|
||||
{
|
||||
Call.SetCallsign(GetReflectorCallsign(), 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
Call.SetCallsign(&Buffer.data()[8], 8);
|
||||
}
|
||||
|
||||
isRepeaterCall = ((Buffer.data()[2] & 0x10) == 0x10);
|
||||
|
||||
std::cout << "Config request from " << Ip << " for " << Call << " (" << ((char *)(isRepeaterCall)?"repeater":"routed") << ")" << std::endl;
|
||||
|
||||
//std::cout << "Local address: " << inet_ntoa(*m_ConfigSocket.GetLocalAddr()) << std::endl;
|
||||
|
||||
Buffer.data()[2] |= 0x80; // response
|
||||
|
||||
if (isRepeaterCall)
|
||||
{
|
||||
if ((Call.HasSameCallsign(GetReflectorCallsign())) && (g_Reflector.IsValidModule(Call.GetModule())))
|
||||
{
|
||||
Buffer.data()[3] = 0x00; // ok
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Module " << Call << " invalid" << std::endl;
|
||||
Buffer.data()[3] = 0x01; // reject
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// reject routed calls for now
|
||||
Buffer.data()[3] = 0x01; // reject
|
||||
}
|
||||
|
||||
char module = Call.GetModule();
|
||||
|
||||
if (!strchr(m_Modules.c_str(), module) && !strchr(m_Modules.c_str(), '*'))
|
||||
{
|
||||
// restricted
|
||||
std::cout << "Module " << Call << " restricted by configuration" << std::endl;
|
||||
Buffer.data()[3] = 0x01; // reject
|
||||
}
|
||||
|
||||
// UR
|
||||
Buffer.resize(8);
|
||||
Buffer.Append((uint8 *)(const char *)Call, CALLSIGN_LEN - 1);
|
||||
Buffer.Append((uint8)module);
|
||||
|
||||
// RPT1
|
||||
Buffer.Append((uint8 *)(const char *)GetReflectorCallsign(), CALLSIGN_LEN - 1);
|
||||
Buffer.Append((uint8)'G');
|
||||
|
||||
// RPT2
|
||||
Buffer.Append((uint8 *)(const char *)GetReflectorCallsign(), CALLSIGN_LEN - 1);
|
||||
|
||||
if (isRepeaterCall)
|
||||
{
|
||||
Buffer.Append((uint8)Call.GetModule());
|
||||
}
|
||||
else
|
||||
{
|
||||
// routed - no module for now
|
||||
Buffer.Append((uint8)' ');
|
||||
}
|
||||
|
||||
if (Buffer.data()[3] == 0x00)
|
||||
{
|
||||
std::cout << "External G3 gateway address " << inet_ntoa(*(in_addr *)&m_GwAddress) << std::endl;
|
||||
|
||||
if (m_GwAddress == 0)
|
||||
{
|
||||
Buffer.Append(*(uint32 *)m_ConfigSocket.GetLocalAddr());
|
||||
}
|
||||
else
|
||||
{
|
||||
Buffer.Append(m_GwAddress);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Buffer.Append(0u);
|
||||
}
|
||||
|
||||
m_ConfigSocket.Send(Buffer, Ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// icmp task
|
||||
|
||||
void CG3Protocol::IcmpTask(void)
|
||||
{
|
||||
CBuffer Buffer;
|
||||
CIp Ip;
|
||||
int iIcmpType;
|
||||
|
||||
if ((iIcmpType = m_IcmpRawSocket.IcmpReceive(&Buffer, &Ip, 20)) != -1)
|
||||
{
|
||||
if (iIcmpType == ICMP_DEST_UNREACH)
|
||||
{
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
|
||||
int index = -1;
|
||||
CClient *client = NULL;
|
||||
while ( (client = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
CIp ClientIp = client->GetIp();
|
||||
if (ClientIp.GetAddr() == Ip.GetAddr())
|
||||
{
|
||||
clients->RemoveClient(client);
|
||||
}
|
||||
}
|
||||
g_Reflector.ReleaseClients();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// DV task
|
||||
|
||||
void CG3Protocol::Task(void)
|
||||
{
|
||||
CBuffer Buffer;
|
||||
CIp Ip;
|
||||
CCallsign Callsign;
|
||||
char ToLinkModule;
|
||||
int ProtRev;
|
||||
CDvHeaderPacket *Header;
|
||||
CDvFramePacket *Frame;
|
||||
CDvLastFramePacket *LastFrame;
|
||||
|
||||
// any incoming packet ?
|
||||
if ( m_Socket.Receive(&Buffer, &Ip, 20) != -1 )
|
||||
{
|
||||
CIp ClIp;
|
||||
CIp *BaseIp = NULL;
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
int index = -1;
|
||||
CClient *client = NULL;
|
||||
while ( (client = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
ClIp = client->GetIp();
|
||||
if (ClIp.GetAddr() == Ip.GetAddr())
|
||||
{
|
||||
BaseIp = &ClIp;
|
||||
client->Alive();
|
||||
// supress host checks - no ping needed to trigger potential ICMPs
|
||||
// the regular data flow will do it
|
||||
m_LastKeepaliveTime.Now();
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_Reflector.ReleaseClients();
|
||||
|
||||
if (BaseIp != NULL)
|
||||
{
|
||||
// crack the packet
|
||||
if ( (Frame = IsValidDvFramePacket(Buffer)) != NULL )
|
||||
{
|
||||
//std::cout << "Terminal DV frame" << std::endl;
|
||||
|
||||
// handle it
|
||||
OnDvFramePacketIn(Frame, BaseIp);
|
||||
}
|
||||
else if ( (Header = IsValidDvHeaderPacket(Buffer)) != NULL )
|
||||
{
|
||||
//std::cout << "Terminal DV header" << std::endl;
|
||||
|
||||
// callsign muted?
|
||||
if ( g_GateKeeper.MayTransmit(Header->GetMyCallsign(), Ip, PROTOCOL_G3, Header->GetRpt2Module()) )
|
||||
{
|
||||
// handle it
|
||||
OnDvHeaderPacketIn(Header, *BaseIp);
|
||||
}
|
||||
else
|
||||
{
|
||||
delete Header;
|
||||
}
|
||||
}
|
||||
else if ( (LastFrame = IsValidDvLastFramePacket(Buffer)) != NULL )
|
||||
{
|
||||
//std::cout << "Terminal DV last frame" << std::endl;
|
||||
|
||||
// handle it
|
||||
OnDvLastFramePacketIn(LastFrame, BaseIp);
|
||||
}
|
||||
else
|
||||
{
|
||||
//std::cout << "Invalid terminal packet (" << Buffer.size() << ")" << std::endl;
|
||||
//std::cout << Buffer.data() << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//std::cout << "Invalid client " << Ip << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// handle end of streaming timeout
|
||||
CheckStreamsTimeout();
|
||||
|
||||
// handle queue from reflector
|
||||
HandleQueue();
|
||||
|
||||
// keep alive during idle if needed
|
||||
if ( m_LastKeepaliveTime.DurationSinceNow() > G3_KEEPALIVE_PERIOD )
|
||||
{
|
||||
// handle keep alives
|
||||
HandleKeepalives();
|
||||
|
||||
// update time
|
||||
m_LastKeepaliveTime.Now();
|
||||
|
||||
// reload option if needed - called once every G3_KEEPALIVE_PERIOD
|
||||
NeedReload();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// queue helper
|
||||
|
||||
void CG3Protocol::HandleQueue(void)
|
||||
{
|
||||
m_Queue.Lock();
|
||||
while ( !m_Queue.empty() )
|
||||
{
|
||||
// supress host checks
|
||||
m_LastKeepaliveTime.Now();
|
||||
|
||||
// get the packet
|
||||
CPacket *packet = m_Queue.front();
|
||||
m_Queue.pop();
|
||||
|
||||
// encode it
|
||||
CBuffer buffer;
|
||||
if ( EncodeDvPacket(*packet, &buffer) )
|
||||
{
|
||||
// and push it to all our clients linked to the module and who are not streaming in
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
int index = -1;
|
||||
CClient *client = NULL;
|
||||
while ( (client = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
// is this client busy ?
|
||||
if ( !client->IsAMaster() && (client->GetReflectorModule() == packet->GetModuleId()) )
|
||||
{
|
||||
// not busy, send the packet
|
||||
int n = packet->IsDvHeader() ? 5 : 1;
|
||||
for ( int i = 0; i < n; i++ )
|
||||
{
|
||||
m_Socket.Send(buffer, client->GetIp());
|
||||
}
|
||||
}
|
||||
}
|
||||
g_Reflector.ReleaseClients();
|
||||
}
|
||||
|
||||
// done
|
||||
delete packet;
|
||||
}
|
||||
m_Queue.Unlock();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// keepalive helpers
|
||||
|
||||
void CG3Protocol::HandleKeepalives(void)
|
||||
{
|
||||
// G3 Terminal mode does not support keepalive
|
||||
// We will send some short packed and expect
|
||||
// A ICMP unreachable on failure
|
||||
CBuffer keepalive((uint8 *)"PING", 4);
|
||||
|
||||
// iterate on clients
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
int index = -1;
|
||||
CClient *client = NULL;
|
||||
while ( (client = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
if (!client->IsAlive())
|
||||
{
|
||||
clients->RemoveClient(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
// send keepalive packet
|
||||
m_Socket.Send(keepalive, client->GetIp());
|
||||
}
|
||||
}
|
||||
g_Reflector.ReleaseClients();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// streams helpers
|
||||
|
||||
bool CG3Protocol::OnDvHeaderPacketIn(CDvHeaderPacket *Header, const CIp &Ip)
|
||||
{
|
||||
bool newstream = false;
|
||||
|
||||
// find the stream
|
||||
CPacketStream *stream = GetStream(Header->GetStreamId(), &Ip);
|
||||
|
||||
if ( stream == NULL )
|
||||
{
|
||||
// no stream open yet, open a new one
|
||||
CCallsign via(Header->GetRpt1Callsign());
|
||||
|
||||
// find this client
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
|
||||
int index = -1;
|
||||
CClient *client = NULL;
|
||||
while ( (client = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
CIp ClIp = client->GetIp();
|
||||
if (ClIp.GetAddr() == Ip.GetAddr())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( client != NULL )
|
||||
{
|
||||
|
||||
// move it to the proper module
|
||||
if (m_ReflectorCallsign.HasSameCallsign(Header->GetRpt2Callsign()))
|
||||
{
|
||||
if (client->GetReflectorModule() != Header->GetRpt2Callsign().GetModule())
|
||||
{
|
||||
char new_module = Header->GetRpt2Callsign().GetModule();
|
||||
if (strchr(m_Modules.c_str(), '*') || strchr(m_Modules.c_str(), new_module))
|
||||
{
|
||||
client->SetReflectorModule(new_module);
|
||||
}
|
||||
else
|
||||
{
|
||||
// drop if invalid module
|
||||
delete Header;
|
||||
g_Reflector.ReleaseClients();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// get client callsign
|
||||
via = client->GetCallsign();
|
||||
|
||||
// and try to open the stream
|
||||
if ( (stream = g_Reflector.OpenStream(Header, client)) != NULL )
|
||||
{
|
||||
// keep the handle
|
||||
m_Streams.push_back(stream);
|
||||
newstream = true;
|
||||
}
|
||||
|
||||
// update last heard
|
||||
g_Reflector.GetUsers()->Hearing(Header->GetMyCallsign(), via, Header->GetRpt2Callsign());
|
||||
g_Reflector.ReleaseUsers();
|
||||
|
||||
// delete header if needed
|
||||
if ( !newstream )
|
||||
{
|
||||
delete Header;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// drop
|
||||
delete Header;
|
||||
}
|
||||
}
|
||||
// release
|
||||
g_Reflector.ReleaseClients();
|
||||
}
|
||||
else
|
||||
{
|
||||
// stream already open
|
||||
// skip packet, but tickle the stream
|
||||
stream->Tickle();
|
||||
// and delete packet
|
||||
delete Header;
|
||||
}
|
||||
|
||||
// done
|
||||
return newstream;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// packet decoding helpers
|
||||
|
||||
CDvHeaderPacket *CG3Protocol::IsValidDvHeaderPacket(const CBuffer &Buffer)
|
||||
{
|
||||
CDvHeaderPacket *header = NULL;
|
||||
|
||||
if ( (Buffer.size() == 56) && (Buffer.Compare((uint8 *)"DSVT", 4) == 0) &&
|
||||
(Buffer.data()[4] == 0x10) && (Buffer.data()[8] == 0x20) )
|
||||
{
|
||||
// create packet
|
||||
header = new CDvHeaderPacket((struct dstar_header *)&(Buffer.data()[15]),
|
||||
*((uint16 *)&(Buffer.data()[12])), 0x80);
|
||||
// check validity of packet
|
||||
if ( !header->IsValid() )
|
||||
{
|
||||
delete header;
|
||||
header = NULL;
|
||||
}
|
||||
}
|
||||
return header;
|
||||
}
|
||||
|
||||
CDvFramePacket *CG3Protocol::IsValidDvFramePacket(const CBuffer &Buffer)
|
||||
{
|
||||
CDvFramePacket *dvframe = NULL;
|
||||
|
||||
if ( (Buffer.size() == 27) && (Buffer.Compare((uint8 *)"DSVT", 4) == 0) &&
|
||||
(Buffer.data()[4] == 0x20) && (Buffer.data()[8] == 0x20) &&
|
||||
((Buffer.data()[14] & 0x40) == 0) )
|
||||
{
|
||||
// create packet
|
||||
dvframe = new CDvFramePacket((struct dstar_dvframe *)&(Buffer.data()[15]),
|
||||
*((uint16 *)&(Buffer.data()[12])), Buffer.data()[14]);
|
||||
// check validity of packet
|
||||
if ( !dvframe->IsValid() )
|
||||
{
|
||||
delete dvframe;
|
||||
dvframe = NULL;
|
||||
}
|
||||
}
|
||||
return dvframe;
|
||||
}
|
||||
|
||||
CDvLastFramePacket *CG3Protocol::IsValidDvLastFramePacket(const CBuffer &Buffer)
|
||||
{
|
||||
CDvLastFramePacket *dvframe = NULL;
|
||||
|
||||
if ( (Buffer.size() == 27) && (Buffer.Compare((uint8 *)"DSVT", 4) == 0) &&
|
||||
(Buffer.data()[4] == 0x20) && (Buffer.data()[8] == 0x20) &&
|
||||
((Buffer.data()[14] & 0x40) != 0) )
|
||||
{
|
||||
// create packet
|
||||
dvframe = new CDvLastFramePacket((struct dstar_dvframe *)&(Buffer.data()[15]),
|
||||
*((uint16 *)&(Buffer.data()[12])), Buffer.data()[14]);
|
||||
// check validity of packet
|
||||
if ( !dvframe->IsValid() )
|
||||
{
|
||||
delete dvframe;
|
||||
dvframe = NULL;
|
||||
}
|
||||
}
|
||||
return dvframe;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// packet encoding helpers
|
||||
|
||||
bool CG3Protocol::EncodeDvHeaderPacket(const CDvHeaderPacket &Packet, CBuffer *Buffer) const
|
||||
{
|
||||
uint8 tag[] = { 'D','S','V','T',0x10,0x00,0x00,0x00,0x20,0x00,0x01,0x02 };
|
||||
struct dstar_header DstarHeader;
|
||||
|
||||
Packet.ConvertToDstarStruct(&DstarHeader);
|
||||
|
||||
Buffer->Set(tag, sizeof(tag));
|
||||
Buffer->Append(Packet.GetStreamId());
|
||||
Buffer->Append((uint8)0x80);
|
||||
Buffer->Append((uint8 *)&DstarHeader, sizeof(struct dstar_header));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CG3Protocol::EncodeDvFramePacket(const CDvFramePacket &Packet, CBuffer *Buffer) const
|
||||
{
|
||||
uint8 tag[] = { 'D','S','V','T',0x20,0x00,0x00,0x00,0x20,0x00,0x01,0x02 };
|
||||
|
||||
Buffer->Set(tag, sizeof(tag));
|
||||
Buffer->Append(Packet.GetStreamId());
|
||||
Buffer->Append((uint8)(Packet.GetPacketId() % 21));
|
||||
Buffer->Append((uint8 *)Packet.GetAmbe(), AMBE_SIZE);
|
||||
Buffer->Append((uint8 *)Packet.GetDvData(), DVDATA_SIZE);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool CG3Protocol::EncodeDvLastFramePacket(const CDvLastFramePacket &Packet, CBuffer *Buffer) const
|
||||
{
|
||||
uint8 tag1[] = { 'D','S','V','T',0x20,0x00,0x00,0x00,0x20,0x00,0x01,0x02 };
|
||||
uint8 tag2[] = { 0x55,0xC8,0x7A,0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x1A,0xC6 };
|
||||
|
||||
Buffer->Set(tag1, sizeof(tag1));
|
||||
Buffer->Append(Packet.GetStreamId());
|
||||
Buffer->Append((uint8)((Packet.GetPacketId() % 21) | 0x40));
|
||||
Buffer->Append(tag2, sizeof(tag2));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// option helpers
|
||||
|
||||
char *CG3Protocol::TrimWhiteSpaces(char *str)
|
||||
{
|
||||
char *end;
|
||||
while ((*str == ' ') || (*str == '\t')) str++;
|
||||
if (*str == 0)
|
||||
return str;
|
||||
end = str + strlen(str) - 1;
|
||||
while ((end > str) && ((*end == ' ') || (*end == '\t') || (*end == '\r'))) end --;
|
||||
*(end + 1) = 0;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
void CG3Protocol::NeedReload(void)
|
||||
{
|
||||
struct stat fileStat;
|
||||
|
||||
if (::stat(TERMINALOPTIONS_PATH, &fileStat) != -1)
|
||||
{
|
||||
if (m_LastModTime != fileStat.st_mtime)
|
||||
{
|
||||
ReadOptions();
|
||||
|
||||
// we have new options - iterate on clients for potential removal
|
||||
CClients *clients = g_Reflector.GetClients();
|
||||
int index = -1;
|
||||
CClient *client = NULL;
|
||||
while ( (client = clients->FindNextClient(PROTOCOL_G3, &index)) != NULL )
|
||||
{
|
||||
char module = client->GetReflectorModule();
|
||||
if (!strchr(m_Modules.c_str(), module) && !strchr(m_Modules.c_str(), '*'))
|
||||
{
|
||||
clients->RemoveClient(client);
|
||||
}
|
||||
}
|
||||
g_Reflector.ReleaseClients();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CG3Protocol::ReadOptions(void)
|
||||
{
|
||||
char sz[256];
|
||||
int opts = 0;
|
||||
|
||||
|
||||
std::ifstream file(TERMINALOPTIONS_PATH);
|
||||
if (file.is_open())
|
||||
{
|
||||
m_GwAddress = 0u;
|
||||
m_Modules = "*";
|
||||
|
||||
while (file.getline(sz, sizeof(sz)).good())
|
||||
{
|
||||
char *szt = TrimWhiteSpaces(sz);
|
||||
char *szval;
|
||||
|
||||
if ((::strlen(szt) > 0) && szt[0] != '#')
|
||||
{
|
||||
if ((szt = ::strtok(szt, " ,\t")) != NULL)
|
||||
{
|
||||
if ((szval = ::strtok(NULL, " ,\t")) != NULL)
|
||||
{
|
||||
if (::strncmp(szt, "address", 7) == 0)
|
||||
{
|
||||
in_addr addr = { .s_addr = inet_addr(szval) };
|
||||
if (addr.s_addr)
|
||||
{
|
||||
std::cout << "G3 handler address set to " << inet_ntoa(addr) << std::endl;
|
||||
m_GwAddress = addr.s_addr;
|
||||
opts++;
|
||||
}
|
||||
}
|
||||
else if (strncmp(szt, "modules", 7) == 0)
|
||||
{
|
||||
std::cout << "G3 handler module list set to " << szval << std::endl;
|
||||
m_Modules = szval;
|
||||
opts++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// unknown option - ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << "G3 handler loaded " << opts << " options from file " << TERMINALOPTIONS_PATH << std::endl;
|
||||
file.close();
|
||||
|
||||
struct stat fileStat;
|
||||
|
||||
if (::stat(TERMINALOPTIONS_PATH, &fileStat) != -1)
|
||||
{
|
||||
m_LastModTime = fileStat.st_mtime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,139 @@
|
||||
//
|
||||
// cg3protocol.h
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 03/06/2019.
|
||||
// Copyright © 2019 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#ifndef cg3protocol_h
|
||||
#define cg3protocol_h
|
||||
|
||||
#include <string>
|
||||
#include "ctimepoint.h"
|
||||
#include "cprotocol.h"
|
||||
#include "cdvheaderpacket.h"
|
||||
#include "cdvframepacket.h"
|
||||
#include "cdvlastframepacket.h"
|
||||
#include "crawsocket.h"
|
||||
#include "cudpmsgsocket.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// note on the G3 terminal/AP protocol:
|
||||
//
|
||||
// There are 3 steps in handling an incoming connection
|
||||
//
|
||||
// 1 - Notification of terminal call on port UDP 12346
|
||||
// - Call will be rejected if in blacklisted
|
||||
//
|
||||
// 2 - Destination request on port UDP 12345
|
||||
// - Calls to specific callsigns will be accepted for a default module
|
||||
// - Repeater calls will be accepted for local modules
|
||||
// - All other calls are rejected
|
||||
//
|
||||
// 3 - Actual D-star flow like in Dextra to/from port 40000
|
||||
// 2 distinct sockets where used in the initial protocol
|
||||
// later firmwares implement a single bidirectional socket
|
||||
//
|
||||
// Alive monitoring is done via a "PING" to remote port 40000. We will get an
|
||||
// ICMP unreachable on terminal mode close or on station shut down if routing is done
|
||||
// correctly. Otherwise a long timeout is used (e.g. 1h)
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// class
|
||||
|
||||
class CG3Protocol : public CProtocol
|
||||
{
|
||||
public:
|
||||
// constructor
|
||||
CG3Protocol() : m_GwAddress(0u), m_Modules("*"), m_LastModTime(0) {};
|
||||
|
||||
// destructor
|
||||
virtual ~CG3Protocol() {};
|
||||
|
||||
// initialization
|
||||
bool Init(void);
|
||||
|
||||
// close
|
||||
void Close(void);
|
||||
|
||||
// task
|
||||
void Task(void);
|
||||
|
||||
protected:
|
||||
// threads
|
||||
static void PresenceThread(CG3Protocol *);
|
||||
static void ConfigThread(CG3Protocol *);
|
||||
static void IcmpThread(CG3Protocol *);
|
||||
|
||||
// helper tasks
|
||||
void PresenceTask(void);
|
||||
void ConfigTask(void);
|
||||
void IcmpTask(void);
|
||||
|
||||
// config
|
||||
void ReadOptions(void);
|
||||
|
||||
// helper
|
||||
char *TrimWhiteSpaces(char *);
|
||||
void NeedReload(void);
|
||||
|
||||
// queue helper
|
||||
void HandleQueue(void);
|
||||
|
||||
// keepalive helpers
|
||||
void HandleKeepalives(void);
|
||||
|
||||
// stream helpers
|
||||
bool OnDvHeaderPacketIn(CDvHeaderPacket *, const CIp &);
|
||||
|
||||
// packet decoding helpers
|
||||
CDvHeaderPacket *IsValidDvHeaderPacket(const CBuffer &);
|
||||
CDvFramePacket *IsValidDvFramePacket(const CBuffer &);
|
||||
CDvLastFramePacket *IsValidDvLastFramePacket(const CBuffer &);
|
||||
|
||||
// packet encoding helpers
|
||||
bool EncodeDvHeaderPacket(const CDvHeaderPacket &, CBuffer *) const;
|
||||
bool EncodeDvFramePacket(const CDvFramePacket &, CBuffer *) const;
|
||||
bool EncodeDvLastFramePacket(const CDvLastFramePacket &, CBuffer *) const;
|
||||
|
||||
protected:
|
||||
std::thread *m_pPresenceThread;
|
||||
std::thread *m_pConfigThread;
|
||||
std::thread *m_pIcmpThread;
|
||||
|
||||
// time
|
||||
CTimePoint m_LastKeepaliveTime;
|
||||
|
||||
// sockets
|
||||
CUdpSocket m_DvOutSocket;
|
||||
CUdpSocket m_PresenceSocket;
|
||||
CUdpMsgSocket m_ConfigSocket;
|
||||
CRawSocket m_IcmpRawSocket;
|
||||
|
||||
// optional params
|
||||
uint32 m_GwAddress;
|
||||
std::string m_Modules;
|
||||
time_t m_LastModTime;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif /* cg3protocol_h */
|
||||
@ -0,0 +1,156 @@
|
||||
//
|
||||
// crawsocket.cpp
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||
// Copyright © 2020 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "main.h"
|
||||
#include <string.h>
|
||||
#include "creflector.h"
|
||||
#include "crawsocket.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// constructor
|
||||
|
||||
CRawSocket::CRawSocket()
|
||||
{
|
||||
m_Socket = -1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// destructor
|
||||
|
||||
CRawSocket::~CRawSocket()
|
||||
{
|
||||
if ( m_Socket != -1 )
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// open & close
|
||||
|
||||
bool CRawSocket::Open(uint16 uiProto)
|
||||
{
|
||||
bool open = false;
|
||||
int on = 1;
|
||||
|
||||
// create socket
|
||||
m_Socket = socket(AF_INET,SOCK_RAW,uiProto);
|
||||
if ( m_Socket != -1 )
|
||||
{
|
||||
fcntl(m_Socket, F_SETFL, O_NONBLOCK);
|
||||
open = true;
|
||||
m_Proto = uiProto;
|
||||
}
|
||||
|
||||
// done
|
||||
return open;
|
||||
}
|
||||
|
||||
void CRawSocket::Close(void)
|
||||
{
|
||||
if ( m_Socket != -1 )
|
||||
{
|
||||
close(m_Socket);
|
||||
m_Socket = -1;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// read
|
||||
|
||||
int CRawSocket::Receive(CBuffer *Buffer, CIp *Ip, int timeout)
|
||||
{
|
||||
struct sockaddr_in Sin;
|
||||
fd_set FdSet;
|
||||
unsigned int uiFromLen = sizeof(struct sockaddr_in);
|
||||
int iRecvLen = -1;
|
||||
struct timeval tv;
|
||||
|
||||
// socket valid ?
|
||||
if ( m_Socket != -1 )
|
||||
{
|
||||
// allocate buffer
|
||||
Buffer->resize(UDP_BUFFER_LENMAX);
|
||||
|
||||
// control socket
|
||||
FD_ZERO(&FdSet);
|
||||
FD_SET(m_Socket, &FdSet);
|
||||
tv.tv_sec = timeout / 1000;
|
||||
tv.tv_usec = (timeout % 1000) * 1000;
|
||||
select(m_Socket + 1, &FdSet, 0, 0, &tv);
|
||||
|
||||
// read
|
||||
iRecvLen = (int)recvfrom(m_Socket,
|
||||
(void *)Buffer->data(), RAW_BUFFER_LENMAX,
|
||||
0, (struct sockaddr *)&Sin, &uiFromLen);
|
||||
|
||||
// handle
|
||||
if ( iRecvLen != -1 )
|
||||
{
|
||||
// adjust buffer size
|
||||
Buffer->resize(iRecvLen);
|
||||
|
||||
// get IP
|
||||
Ip->SetSockAddr(&Sin);
|
||||
}
|
||||
}
|
||||
|
||||
// done
|
||||
return iRecvLen;
|
||||
}
|
||||
|
||||
// protocol specific
|
||||
|
||||
// ICMP
|
||||
|
||||
int CRawSocket::IcmpReceive(CBuffer *Buffer, CIp *Ip, int timeout)
|
||||
{
|
||||
int iIcmpType = -1;
|
||||
int iRecv;
|
||||
|
||||
if (m_Proto == IPPROTO_ICMP)
|
||||
{
|
||||
iRecv = Receive(Buffer, Ip, timeout);
|
||||
|
||||
if (iRecv >= (int)(sizeof(struct ip) + sizeof(struct icmp)))
|
||||
{
|
||||
struct ip *iph = (struct ip *)Buffer->data();
|
||||
int iphdrlen = iph->ip_hl * 4;
|
||||
struct icmp *icmph = (struct icmp *)((unsigned char *)iph + iphdrlen);
|
||||
struct ip *remote_iph = (struct ip *)((unsigned char *)icmph + 8);
|
||||
|
||||
iIcmpType = icmph->icmp_type;
|
||||
|
||||
struct sockaddr_in Sin;
|
||||
bzero(&Sin, sizeof(Sin));
|
||||
Sin.sin_family = AF_INET;
|
||||
Sin.sin_addr.s_addr = remote_iph->ip_dst.s_addr;
|
||||
|
||||
Ip->SetSockAddr(&Sin);
|
||||
|
||||
}
|
||||
}
|
||||
return iIcmpType;
|
||||
}
|
||||
@ -0,0 +1,99 @@
|
||||
//
|
||||
// crawsocket.h
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||
// Copyright © 2020 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Description:
|
||||
// Raw socket access class with protocol specific
|
||||
|
||||
|
||||
#ifndef crawsocket_h
|
||||
#define crawsocket_h
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip_icmp.h>
|
||||
#include "cip.h"
|
||||
#include "cbuffer.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// define
|
||||
|
||||
#define RAW_BUFFER_LENMAX 65536
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// class
|
||||
|
||||
class CRawSocket
|
||||
{
|
||||
public:
|
||||
// constructor
|
||||
CRawSocket();
|
||||
|
||||
// destructor
|
||||
~CRawSocket();
|
||||
|
||||
// open & close
|
||||
bool Open(uint16);
|
||||
void Close(void);
|
||||
int GetSocket(void) { return m_Socket; }
|
||||
|
||||
// read
|
||||
|
||||
// if ETH_P_ALL is used, the received data buffer will hold
|
||||
// the ethernet header (struct ethhdr) followed by the IP header (struct iphdr),
|
||||
// the protocol header (e.g tcp, udp, icmp) and the data.
|
||||
// For specific protocols, the data content may vary depending on the protocol
|
||||
// Returns the number of received bytes in buffer
|
||||
|
||||
int Receive(CBuffer *, CIp *, int);
|
||||
|
||||
// ICMP receive helper
|
||||
// parameters:
|
||||
// buffer - packet receive buffer (starting with ip header)
|
||||
// ip - remote address (filled in on receive)
|
||||
// timeout - receive timeout in msec
|
||||
// return value:
|
||||
// ICMP type, -1 if nothing was received
|
||||
|
||||
int IcmpReceive(CBuffer *, CIp *, int);
|
||||
|
||||
// write
|
||||
// no write support - complexity makes it out of scope for now
|
||||
// to be added if needed
|
||||
|
||||
protected:
|
||||
// data
|
||||
int m_Socket;
|
||||
int m_Proto;
|
||||
struct sockaddr_in m_SocketAddr;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif /* crawsocket_h */
|
||||
@ -0,0 +1,116 @@
|
||||
//
|
||||
// cudpmsgsocket.cpp
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||
// Copyright © 2020 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "main.h"
|
||||
#include <string.h>
|
||||
#include "cudpmsgsocket.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// open
|
||||
bool CUdpMsgSocket::Open(uint16 uiPort)
|
||||
{
|
||||
bool ret;
|
||||
int on = 1;
|
||||
|
||||
ret = CUdpSocket::Open(uiPort);
|
||||
setsockopt(m_Socket, IPPROTO_IP, IP_PKTINFO, (char *)&on, sizeof(on));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// read
|
||||
|
||||
int CUdpMsgSocket::Receive(CBuffer *Buffer, CIp *Ip, int timeout)
|
||||
{
|
||||
struct sockaddr_in Sin;
|
||||
fd_set FdSet;
|
||||
unsigned int uiFromLen = sizeof(struct sockaddr_in);
|
||||
int iRecvLen = -1;
|
||||
struct timeval tv;
|
||||
|
||||
struct msghdr Msg;
|
||||
struct iovec Iov[1];
|
||||
|
||||
union {
|
||||
struct cmsghdr cm;
|
||||
unsigned char pktinfo_sizer[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
|
||||
} Control;
|
||||
|
||||
// socket valid ?
|
||||
if ( m_Socket != -1 )
|
||||
{
|
||||
// allocate buffer
|
||||
Buffer->resize(UDP_MSG_BUFFER_LENMAX);
|
||||
|
||||
//prepare msghdr
|
||||
bzero(&Msg, sizeof(Msg));
|
||||
Iov[0].iov_base = Buffer->data();
|
||||
Iov[0].iov_len = UDP_MSG_BUFFER_LENMAX;
|
||||
|
||||
bzero(&Sin, sizeof(Sin));
|
||||
Msg.msg_name = &Sin;
|
||||
Msg.msg_namelen = sizeof(Sin);
|
||||
Msg.msg_iov = Iov;
|
||||
Msg.msg_iovlen = 1;
|
||||
Msg.msg_control = &Control;
|
||||
Msg.msg_controllen = sizeof(Control);
|
||||
|
||||
// control socket
|
||||
FD_ZERO(&FdSet);
|
||||
FD_SET(m_Socket, &FdSet);
|
||||
tv.tv_sec = timeout / 1000;
|
||||
tv.tv_usec = (timeout % 1000) * 1000;
|
||||
select(m_Socket + 1, &FdSet, 0, 0, &tv);
|
||||
|
||||
// read
|
||||
iRecvLen = (int)recvmsg(m_Socket, &Msg, 0);
|
||||
|
||||
// handle
|
||||
if ( iRecvLen != -1 )
|
||||
{
|
||||
// adjust buffer size
|
||||
Buffer->resize(iRecvLen);
|
||||
|
||||
// get IP
|
||||
Ip->SetSockAddr(&Sin);
|
||||
|
||||
// get local IP
|
||||
struct cmsghdr *Cmsg;
|
||||
for (Cmsg = CMSG_FIRSTHDR(&Msg); Cmsg != NULL; Cmsg = CMSG_NXTHDR(&Msg, Cmsg))
|
||||
{
|
||||
if (Cmsg->cmsg_level == IPPROTO_IP && Cmsg->cmsg_type == IP_PKTINFO)
|
||||
{
|
||||
struct in_pktinfo *PktInfo = (struct in_pktinfo *)CMSG_DATA(Cmsg);
|
||||
m_LocalAddr.s_addr = PktInfo->ipi_spec_dst.s_addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// done
|
||||
return iRecvLen;
|
||||
}
|
||||
|
||||
@ -0,0 +1,56 @@
|
||||
//
|
||||
// cudpmsgsocket.h
|
||||
// xlxd
|
||||
//
|
||||
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||
// Copyright © 2020 Marius Petrescu (YO2LOJ). 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/>.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Description:
|
||||
// Extension of the CUdpSocket class to allow retrieving
|
||||
// the local target IP for a G3 Terminal mode config request
|
||||
|
||||
#ifndef cudpmsgsocket_h
|
||||
#define cudpmsgsocket_h
|
||||
|
||||
#include "cudpsocket.h"
|
||||
|
||||
#define UDP_MSG_BUFFER_LENMAX 1024
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// class
|
||||
|
||||
class CUdpMsgSocket : public CUdpSocket
|
||||
{
|
||||
public:
|
||||
// open
|
||||
bool Open(uint16);
|
||||
|
||||
// read
|
||||
int Receive(CBuffer *, CIp *, int);
|
||||
|
||||
struct in_addr *GetLocalAddr(void) { return &m_LocalAddr; }
|
||||
|
||||
protected:
|
||||
// data
|
||||
struct in_addr m_LocalAddr;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#endif /* cudpmsgsocket_h */
|
||||
Loading…
Reference in new issue