YSFClients/YSFGateway/FCSNetwork.cpp

304 lines
6.9 KiB
C++
Raw Normal View History

2018-02-21 19:46:21 +00:00
/*
* Copyright (C) 2009-2014,2016,2017,2018 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 "YSFDefines.h"
#include "FCSNetwork.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
2018-02-23 07:19:58 +00:00
const char* FCS_VERSION = "MMDVM";
2018-02-21 19:46:21 +00:00
const unsigned int BUFFER_LENGTH = 200U;
2018-02-21 19:46:21 +00:00
CFCSNetwork::CFCSNetwork(unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, bool debug) :
2018-02-21 19:46:21 +00:00
m_socket(port),
m_debug(debug),
2020-09-03 16:13:27 +01:00
m_addr(),
m_addrLen(),
2018-02-22 19:11:21 +00:00
m_ping(NULL),
m_options(NULL),
2020-09-05 21:14:17 +01:00
m_opt(),
m_info(NULL),
m_reflector(),
m_print(),
2018-02-22 08:05:21 +00:00
m_buffer(1000U, "FCS Network Buffer"),
2018-02-22 20:14:49 +00:00
m_n(0U),
2018-02-26 19:05:34 +00:00
m_pingTimer(1000U, 0U, 800U),
2018-02-26 19:12:26 +00:00
m_resetTimer(1000U, 1U),
2018-02-22 20:14:49 +00:00
m_state(FCS_UNLINKED)
2018-02-21 19:46:21 +00:00
{
m_info = new unsigned char[100U];
::sprintf((char*)m_info, "%9u%9u%-6.6s%-12.12s%7u", rxFrequency, txFrequency, locator.c_str(), FCS_VERSION, id);
2018-02-23 07:19:58 +00:00
::memset(m_info + 43U, ' ', 57U);
2018-02-22 19:11:21 +00:00
m_ping = new unsigned char[25U];
::memcpy(m_ping + 0U, "PING", 4U);
::memset(m_ping + 4U, ' ', 6U);
::memcpy(m_ping + 4U, callsign.c_str(), callsign.size());
::memset(m_ping + 10U, 0x00U, 15U);
m_options = new unsigned char[50U];
::memcpy(m_options + 0U, "FCSO", 4U);
::memset(m_options + 4U, ' ', 46U);
::memcpy(m_options + 4U, callsign.c_str(), callsign.size());
2018-02-21 19:46:21 +00:00
}
CFCSNetwork::~CFCSNetwork()
{
delete[] m_info;
2018-02-22 19:11:21 +00:00
delete[] m_ping;
delete[] m_options;
2018-02-21 19:46:21 +00:00
}
bool CFCSNetwork::open()
{
2020-09-05 21:09:29 +01:00
LogMessage("Resolving FCS999 address");
2018-02-22 08:05:21 +00:00
2020-09-03 16:13:27 +01:00
sockaddr_storage addr;
unsigned int addrLen;
2020-09-05 21:09:29 +01:00
if (CUDPSocket::lookup("fcs999.xreflector.net", FCS_PORT, addr, addrLen) != 0) {
LogWarning("Unable to lookup the address for FCS999");
return false;
} else {
std::pair<sockaddr_storage, unsigned int> entry = std::make_pair(addr, addrLen);
m_addresses["FCS999"] = entry;
}
2018-02-21 19:46:21 +00:00
LogMessage("Opening FCS network connection");
return m_socket.open();
}
void CFCSNetwork::clearDestination()
{
2018-02-22 20:14:49 +00:00
m_pingTimer.stop();
2018-02-26 19:12:26 +00:00
m_resetTimer.stop();
2018-02-22 20:14:49 +00:00
m_state = FCS_UNLINKED;
2018-02-21 19:46:21 +00:00
}
2018-02-22 19:11:21 +00:00
void CFCSNetwork::write(const unsigned char* data)
2018-02-21 19:46:21 +00:00
{
assert(data != NULL);
2018-02-22 20:14:49 +00:00
if (m_state != FCS_LINKED)
return;
unsigned char buffer[130U];
::memset(buffer + 0U, ' ', 130U);
::memcpy(buffer + 0U, data + 35U, 120U);
2020-09-05 08:56:18 +02:00
::memcpy(buffer + 120U, data + 34U, 1U);
::memcpy(buffer + 121U, m_reflector.c_str(), 8U);
2018-02-21 19:46:21 +00:00
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", buffer, 130U);
2018-02-21 19:46:21 +00:00
2020-09-03 16:13:27 +01:00
m_socket.write(buffer, 130U, m_addr, m_addrLen);
2018-02-21 19:46:21 +00:00
}
2018-02-22 22:06:24 +00:00
bool CFCSNetwork::writeLink(const std::string& reflector)
2018-02-21 19:46:21 +00:00
{
2018-02-26 19:05:34 +00:00
if (m_state != FCS_LINKED) {
2018-02-22 08:05:21 +00:00
std::string name = reflector.substr(0U, 6U);
2020-09-05 20:09:31 +01:00
if (m_addresses.count(name) == 0U) {
2020-09-05 21:09:29 +01:00
char url[30U];
::sprintf(url, "%s.xreflector.net", name.c_str());
2020-09-05 21:09:29 +01:00
if (CUDPSocket::lookup(url, FCS_PORT, m_addr, m_addrLen) != 0) {
LogWarning("Unknown FCS reflector - %s", name.c_str());
return false;
}
} else {
2020-09-05 21:09:29 +01:00
std::pair<sockaddr_storage, unsigned int> entry = m_addresses[name];
m_addr = entry.first;
m_addrLen = entry.second;
}
2018-02-22 08:05:21 +00:00
}
m_reflector = reflector;
::memcpy(m_ping + 10U, reflector.c_str(), 8U);
m_print = reflector.substr(0U, 6U) + "-" + reflector.substr(6U);
2018-02-26 19:05:34 +00:00
m_state = FCS_LINKING;
2018-02-22 20:14:49 +00:00
m_pingTimer.start();
2018-02-26 19:05:34 +00:00
writePing();
2018-02-22 22:06:24 +00:00
return true;
2018-02-21 19:46:21 +00:00
}
void CFCSNetwork::setOptions(const std::string& options)
{
m_opt = options;
}
2018-02-22 19:11:21 +00:00
void CFCSNetwork::writeUnlink(unsigned int count)
2018-02-21 19:46:21 +00:00
{
2018-02-26 19:05:34 +00:00
if (m_state != FCS_LINKED)
2018-02-22 19:11:21 +00:00
return;
2018-02-21 19:46:21 +00:00
2018-02-22 19:11:21 +00:00
for (unsigned int i = 0U; i < count; i++)
2020-09-03 16:13:27 +01:00
m_socket.write((unsigned char*)"CLOSE ", 11U, m_addr, m_addrLen);
2018-02-21 19:46:21 +00:00
}
void CFCSNetwork::clock(unsigned int ms)
{
2018-02-26 19:12:26 +00:00
m_pingTimer.clock(ms);
if (m_pingTimer.isRunning() && m_pingTimer.hasExpired()) {
writePing();
m_pingTimer.start();
}
m_resetTimer.clock(ms);
if (m_resetTimer.isRunning() && m_resetTimer.hasExpired()) {
m_n = 0U;
m_resetTimer.stop();
}
2018-02-21 19:46:21 +00:00
unsigned char buffer[BUFFER_LENGTH];
2020-09-03 16:13:27 +01:00
sockaddr_storage addr;
unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
2018-02-21 19:46:21 +00:00
if (length <= 0)
return;
2018-02-26 19:05:34 +00:00
if (m_state == FCS_UNLINKED)
2018-02-22 19:11:21 +00:00
return;
2020-09-03 16:13:27 +01:00
if (!CUDPSocket::match(addr, m_addr))
2018-02-21 19:46:21 +00:00
return;
if (m_debug)
CUtils::dump(1U, "FCS Network Data Received", buffer, length);
2018-02-22 20:14:49 +00:00
if (length == 7) {
2018-02-27 08:02:02 +00:00
if (m_state == FCS_LINKING)
LogMessage("Linked to %s", m_print.c_str());
2018-02-22 20:14:49 +00:00
m_state = FCS_LINKED;
writeInfo();
2020-09-06 16:15:07 +02:00
writeOptions(m_print);
2018-02-22 20:14:49 +00:00
}
if (length == 10 && m_state == FCS_LINKING) {
LogMessage("Linked to %s", m_print.c_str());
2018-02-22 20:14:49 +00:00
m_state = FCS_LINKED;
writeInfo();
2020-09-06 16:15:07 +02:00
writeOptions(m_print);
2018-02-22 20:14:49 +00:00
}
2018-02-21 19:46:21 +00:00
if (length == 7 || length == 10 || length == 130) {
2018-02-22 20:14:49 +00:00
unsigned char len = length;
m_buffer.addData(&len, 1U);
m_buffer.addData(buffer, len);
}
2018-02-21 19:46:21 +00:00
}
unsigned int CFCSNetwork::read(unsigned char* data)
{
assert(data != NULL);
if (m_buffer.isEmpty())
return 0U;
2018-02-22 20:14:49 +00:00
unsigned char len = 0U;
m_buffer.getData(&len, 1U);
// Pass pings up to the gateway to reset the lost timer.
if (len != 130U) {
2018-02-22 20:14:49 +00:00
m_buffer.getData(data, len);
2018-02-26 19:05:34 +00:00
::memset(data + 0U, ' ', 14U);
::memcpy(data + 0U, "YSFP", 4U);
::memcpy(data + 4U, m_print.c_str(), 8U);
2018-02-26 19:05:34 +00:00
return 14U;
2018-02-22 20:14:49 +00:00
}
2018-02-26 19:12:26 +00:00
m_resetTimer.start();
2018-02-22 19:11:21 +00:00
unsigned char buffer[130U];
2018-02-22 20:14:49 +00:00
m_buffer.getData(buffer, len);
2018-02-22 19:11:21 +00:00
::memset(data + 0U, ' ', 35U);
::memcpy(data + 0U, "YSFD", 4U);
2018-02-22 19:11:21 +00:00
::memcpy(data + 35U, buffer, 120U);
2018-02-22 08:05:21 +00:00
2018-02-22 19:11:21 +00:00
// Put the reflector name as the via callsign.
::memcpy(data + 4U, m_print.c_str(), 9U);
2018-02-22 08:05:21 +00:00
data[34U] = m_n;
m_n += 2U;
2018-02-21 19:46:21 +00:00
return 155U;
2018-02-21 19:46:21 +00:00
}
void CFCSNetwork::close()
{
m_socket.close();
LogMessage("Closing FCS network connection");
}
void CFCSNetwork::writeInfo()
{
2018-02-26 19:05:34 +00:00
if (m_state != FCS_LINKED)
return;
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_info, 100U);
2020-09-03 16:13:27 +01:00
m_socket.write(m_info, 100U, m_addr, m_addrLen);
}
2018-02-22 19:11:21 +00:00
void CFCSNetwork::writePing()
{
2018-02-26 19:05:34 +00:00
if (m_state == FCS_UNLINKED)
2018-02-22 19:11:21 +00:00
return;
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_ping, 25U);
2020-09-03 16:13:27 +01:00
m_socket.write(m_ping, 25U, m_addr, m_addrLen);
2018-02-22 19:11:21 +00:00
}
2020-09-06 16:15:07 +02:00
void CFCSNetwork::writeOptions(const std::string& reflector)
{
if (m_state != FCS_LINKED)
return;
if (m_opt.size() < 1)
return;
::memset(m_options + 14U, 0x20U, 36U);
2020-09-06 16:15:07 +02:00
::memcpy(m_options + 4U, (reflector.substr(0,6)+reflector.substr(7,2)).c_str(), 8U);
::memcpy(m_options + 12U, m_opt.c_str(), m_opt.size());
if (m_debug)
CUtils::dump(1U, "FCS Network Options Sent", m_options, 50U);
2020-09-05 22:04:49 +01:00
m_socket.write(m_options, 50U, m_addr, m_addrLen);
}