2018-05-09 20:23:17 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010,2011,2013 by Jonathan Naylor G4KLX
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "G2ProtocolHandler.h"
|
|
|
|
|
|
|
|
|
|
#include "DStarDefines.h"
|
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
|
|
// #define DUMP_TX
|
|
|
|
|
|
|
|
|
|
const unsigned int BUFFER_LENGTH = 255U;
|
|
|
|
|
|
|
|
|
|
CG2ProtocolHandler::CG2ProtocolHandler(unsigned int port, const wxString& addr) :
|
|
|
|
|
m_socket(addr, port),
|
|
|
|
|
m_type(GT_NONE),
|
|
|
|
|
m_buffer(NULL),
|
2019-01-15 21:50:58 +01:00
|
|
|
m_length(0U),
|
|
|
|
|
m_address(),
|
|
|
|
|
m_port(0U)
|
2018-05-09 20:23:17 +02:00
|
|
|
{
|
|
|
|
|
m_buffer = new unsigned char[BUFFER_LENGTH];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CG2ProtocolHandler::~CG2ProtocolHandler()
|
|
|
|
|
{
|
|
|
|
|
delete[] m_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CG2ProtocolHandler::open()
|
|
|
|
|
{
|
|
|
|
|
return m_socket.open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CG2ProtocolHandler::writeHeader(const CHeaderData& header)
|
|
|
|
|
{
|
|
|
|
|
unsigned char buffer[60U];
|
|
|
|
|
unsigned int length = header.getG2Data(buffer, 60U, true);
|
|
|
|
|
|
|
|
|
|
#if defined(DUMP_TX)
|
|
|
|
|
CUtils::dump(wxT("Sending Header"), buffer, length);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0U; i < 5U; i++) {
|
|
|
|
|
bool res = m_socket.write(buffer, length, header.getYourAddress(), header.getYourPort());
|
|
|
|
|
if (!res)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CG2ProtocolHandler::writeAMBE(const CAMBEData& data)
|
|
|
|
|
{
|
|
|
|
|
unsigned char buffer[40U];
|
|
|
|
|
unsigned int length = data.getG2Data(buffer, 40U);
|
|
|
|
|
|
|
|
|
|
#if defined(DUMP_TX)
|
|
|
|
|
CUtils::dump(wxT("Sending Data"), buffer, length);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return m_socket.write(buffer, length, data.getYourAddress(), data.getYourPort());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 21:50:58 +01:00
|
|
|
G2_TYPE CG2ProtocolHandler::read()
|
2018-05-09 20:23:17 +02:00
|
|
|
{
|
|
|
|
|
bool res = true;
|
|
|
|
|
|
|
|
|
|
// Loop until we have no more data from the socket or we have data for the higher layers
|
|
|
|
|
while (res)
|
2019-01-15 21:50:58 +01:00
|
|
|
res = readPackets();
|
2018-05-09 20:23:17 +02:00
|
|
|
|
|
|
|
|
return m_type;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 21:50:58 +01:00
|
|
|
bool CG2ProtocolHandler::readPackets()
|
2018-05-09 20:23:17 +02:00
|
|
|
{
|
|
|
|
|
m_type = GT_NONE;
|
2018-11-18 17:14:29 +01:00
|
|
|
|
2018-05-09 20:23:17 +02:00
|
|
|
// No more data?
|
2019-01-15 21:50:58 +01:00
|
|
|
int length = m_socket.read(m_buffer, BUFFER_LENGTH, m_address, m_port);
|
2018-05-09 20:23:17 +02:00
|
|
|
if (length <= 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_length = length;
|
|
|
|
|
|
|
|
|
|
if (m_buffer[0] != 'D' || m_buffer[1] != 'S' || m_buffer[2] != 'V' || m_buffer[3] != 'T') {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// Header or data packet type?
|
|
|
|
|
if ((m_buffer[14] & 0x80) == 0x80)
|
|
|
|
|
m_type = GT_HEADER;
|
|
|
|
|
else
|
|
|
|
|
m_type = GT_AMBE;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 21:50:58 +01:00
|
|
|
CHeaderData* CG2ProtocolHandler::readHeader()
|
2018-05-09 20:23:17 +02:00
|
|
|
{
|
|
|
|
|
if (m_type != GT_HEADER)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
CHeaderData* header = new CHeaderData;
|
|
|
|
|
|
|
|
|
|
// G2 checksums are unreliable
|
2019-01-15 21:50:58 +01:00
|
|
|
bool res = header->setG2Data(m_buffer, m_length, false, m_address, m_port);
|
2018-05-09 20:23:17 +02:00
|
|
|
if (!res) {
|
|
|
|
|
delete header;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return header;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 21:50:58 +01:00
|
|
|
CAMBEData* CG2ProtocolHandler::readAMBE()
|
2018-05-09 20:23:17 +02:00
|
|
|
{
|
|
|
|
|
if (m_type != GT_AMBE)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
CAMBEData* data = new CAMBEData;
|
|
|
|
|
|
2019-01-15 21:50:58 +01:00
|
|
|
bool res = data->setG2Data(m_buffer, m_length, m_address, m_port);
|
2018-05-09 20:23:17 +02:00
|
|
|
if (!res) {
|
|
|
|
|
delete data;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 20:41:57 +01:00
|
|
|
#if defined(ENABLE_NAT_TRAVERSAL)
|
2018-11-24 15:22:34 +01:00
|
|
|
void CG2ProtocolHandler::traverseNat(const wxString& address)
|
2018-11-02 09:17:18 +01:00
|
|
|
{
|
|
|
|
|
unsigned char buffer[1];
|
|
|
|
|
::memset(buffer, 0, 1);
|
|
|
|
|
|
|
|
|
|
in_addr addr = CUDPReaderWriter::lookup(address);
|
|
|
|
|
|
|
|
|
|
//wxLogError(wxT("Punching hole to %s"), address.mb_str());
|
|
|
|
|
|
|
|
|
|
m_socket.write(buffer, 1, addr, G2_DV_PORT);
|
|
|
|
|
}
|
2019-01-16 20:41:57 +01:00
|
|
|
#endif
|
2018-11-02 09:17:18 +01:00
|
|
|
|
2018-05-09 20:23:17 +02:00
|
|
|
void CG2ProtocolHandler::close()
|
|
|
|
|
{
|
|
|
|
|
m_socket.close();
|
|
|
|
|
}
|