mirror of https://github.com/nostar/urfd.git
parent
d013a3d532
commit
47abcf0fdc
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Library: libcrc
|
||||||
|
* Git: https://github.com/lammertb/libcrc
|
||||||
|
* Author: Lammert Bies
|
||||||
|
*
|
||||||
|
* This file is licensed under the MIT License as stated below
|
||||||
|
*
|
||||||
|
* Copyright (c) 1999-2016 Lammert Bies
|
||||||
|
* Copyright (c) 2020 Thomas A. Early, N7TAE
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* Description
|
||||||
|
* -----------
|
||||||
|
* The source file contains routines which calculate the CCITT CRC
|
||||||
|
* values for an incomming byte string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "M17CRC.h"
|
||||||
|
|
||||||
|
#define CRC_POLY_16 0x5935u
|
||||||
|
#define CRC_START_16 0xFFFFu
|
||||||
|
|
||||||
|
CM17CRC::CM17CRC()
|
||||||
|
{
|
||||||
|
for (uint16_t i=0; i<256; i++)
|
||||||
|
{
|
||||||
|
uint16_t crc = 0;
|
||||||
|
uint16_t c = i << 8;
|
||||||
|
|
||||||
|
for (uint16_t j=0; j<8; j++)
|
||||||
|
{
|
||||||
|
if ( (crc ^ c) & 0x8000 )
|
||||||
|
crc = ( crc << 1 ) ^ CRC_POLY_16;
|
||||||
|
else
|
||||||
|
crc = crc << 1;
|
||||||
|
|
||||||
|
c = c << 1;
|
||||||
|
}
|
||||||
|
crc_tab16[i] = crc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t CM17CRC::CalcCRC( const uint8_t *input_str, size_t num_bytes ) const
|
||||||
|
{
|
||||||
|
uint16_t crc = CRC_START_16;
|
||||||
|
|
||||||
|
if ( input_str )
|
||||||
|
for (size_t a=0; a<num_bytes; a++)
|
||||||
|
{
|
||||||
|
crc = (crc << 8) ^ crc_tab16[ ((crc >> 8) ^ uint16_t(input_str[a])) & 0x00FF ];
|
||||||
|
}
|
||||||
|
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Library: libcrc
|
||||||
|
* Git: https://github.com/lammertb/libcrc
|
||||||
|
* Author: Lammert Bies
|
||||||
|
*
|
||||||
|
* This file is licensed under the MIT License as stated below
|
||||||
|
*
|
||||||
|
* Copyright (c) 1999-2016 Lammert Bies
|
||||||
|
* Copyright (c) 2020 Thomas A. Early, N7TAE
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* Description
|
||||||
|
* -----------
|
||||||
|
* The source file contains routines which calculate the CCITT CRC
|
||||||
|
* values for an incomming byte string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
class CM17CRC
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CM17CRC();
|
||||||
|
uint16_t CalcCRC(const uint8_t *buf, size_t len) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint16_t crc_tab16[256];
|
||||||
|
};
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
// ulxd -- The universal reflector
|
||||||
|
// Copyright © 2021 Thomas A. Early N7TAE
|
||||||
|
//
|
||||||
|
// 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include "Main.h"
|
||||||
|
#include "M17Client.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
CM17Client::CM17Client()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CM17Client::CM17Client(const CCallsign &callsign, const CIp &ip, char reflectorModule)
|
||||||
|
: CClient(callsign, ip, reflectorModule)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CM17Client::CM17Client(const CM17Client &client)
|
||||||
|
: CClient(client)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// status
|
||||||
|
|
||||||
|
bool CM17Client::IsAlive(void) const
|
||||||
|
{
|
||||||
|
return (m_LastKeepaliveTime.time() < M17_KEEPALIVE_TIMEOUT);
|
||||||
|
}
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// ulxd -- The universal reflector
|
||||||
|
// Copyright © 2021 Thomas A. Early N7TAE
|
||||||
|
//
|
||||||
|
// 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include "Client.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// define
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CM17Client : public CClient
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
CM17Client();
|
||||||
|
CM17Client(const CCallsign &, const CIp &, char);
|
||||||
|
CM17Client(const CM17Client &);
|
||||||
|
|
||||||
|
// destructor
|
||||||
|
virtual ~CM17Client() {};
|
||||||
|
|
||||||
|
// identity
|
||||||
|
const char *GetProtocolName(void) const { return "M17"; }
|
||||||
|
bool IsNode(void) const { return true; }
|
||||||
|
|
||||||
|
// status
|
||||||
|
bool IsAlive(void) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -0,0 +1,95 @@
|
|||||||
|
//
|
||||||
|
// Copyright © 2020 Thomas A. Early, N7TAE
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// m17ref 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.
|
||||||
|
//
|
||||||
|
// m17ref 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
|
||||||
|
// with this software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "M17Packet.h"
|
||||||
|
|
||||||
|
CM17Packet::CM17Packet(const uint8_t *buf)
|
||||||
|
{
|
||||||
|
memcpy(m17.magic, buf, sizeof(SM17Frame));
|
||||||
|
|
||||||
|
destination.CodeIn(m17.lich.addr_dst);
|
||||||
|
source.CodeIn(m17.lich.addr_src);
|
||||||
|
}
|
||||||
|
|
||||||
|
const CCallsign &CM17Packet::GetDestCallsign() const
|
||||||
|
{
|
||||||
|
return destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CCallsign &CM17Packet::GetSourceCallsign() const
|
||||||
|
{
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
char CM17Packet::GetDestModule() const
|
||||||
|
{
|
||||||
|
return destination.GetModule();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t CM17Packet::GetFrameNumber() const
|
||||||
|
{
|
||||||
|
return ntohs(m17.framenumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t CM17Packet::GetFrameType() const
|
||||||
|
{
|
||||||
|
return ntohs(m17.lich.frametype);
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *CM17Packet::GetPayload() const
|
||||||
|
{
|
||||||
|
return m17.payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *CM17Packet::GetNonce() const
|
||||||
|
{
|
||||||
|
return m17.lich.nonce;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CM17Packet::SetPayload(const uint8_t *newpayload)
|
||||||
|
{
|
||||||
|
memcpy(m17.payload, newpayload, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t CM17Packet::GetStreamId() const
|
||||||
|
{
|
||||||
|
return ntohs(m17.streamid);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t CM17Packet::GetCRC() const
|
||||||
|
{
|
||||||
|
return ntohs(m17.crc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CM17Packet::SetCRC(uint16_t crc)
|
||||||
|
{
|
||||||
|
m17.crc = htons(crc);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<CM17Packet> CM17Packet::Duplicate(void) const
|
||||||
|
{
|
||||||
|
return std::unique_ptr<CM17Packet>(new CM17Packet(*this));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CM17Packet::IsLastPacket() const
|
||||||
|
{
|
||||||
|
return ((0x8000u & ntohs(m17.framenumber)) == 0x8000u);
|
||||||
|
}
|
||||||
@ -0,0 +1,78 @@
|
|||||||
|
//
|
||||||
|
// Copyright © 2020 Thomas A. Early, N7TAE
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// m17ref 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.
|
||||||
|
//
|
||||||
|
// m17ref 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
|
||||||
|
// with this software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "Callsign.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// aliases
|
||||||
|
|
||||||
|
// M17 Packets
|
||||||
|
//all structures must be big endian on the wire, so you'll want htonl (man byteorder 3) and such.
|
||||||
|
using SM17Lich = struct __attribute__((__packed__)) lich_tag {
|
||||||
|
uint8_t addr_dst[6];
|
||||||
|
uint8_t addr_src[6];
|
||||||
|
uint16_t frametype; //frametype flag field per the M17 spec
|
||||||
|
uint8_t nonce[14]; //bytes for the nonce
|
||||||
|
}; // 6 + 6 + 2 + 14 = 28 bytes
|
||||||
|
|
||||||
|
//without SYNC or other parts
|
||||||
|
using SM17Frame = struct __attribute__((__packed__)) m17_tag {
|
||||||
|
uint8_t magic[4];
|
||||||
|
uint16_t streamid;
|
||||||
|
SM17Lich lich;
|
||||||
|
uint16_t framenumber;
|
||||||
|
uint8_t payload[16];
|
||||||
|
uint16_t crc; //16 bit CRC
|
||||||
|
}; // 4 + 2 + 28 + 2 + 16 + 2 = 54 bytes
|
||||||
|
|
||||||
|
using SLinkPacket = struct __attribute__((__packed__)) link_tag {
|
||||||
|
uint8_t magic[4];
|
||||||
|
uint8_t fromcs[6];
|
||||||
|
uint8_t mod;
|
||||||
|
}; // 37 bytes
|
||||||
|
|
||||||
|
class CM17Packet
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CM17Packet() {}
|
||||||
|
CM17Packet(const uint8_t *buf);
|
||||||
|
const CCallsign &GetDestCallsign() const;
|
||||||
|
const CCallsign &GetSourceCallsign() const;
|
||||||
|
char GetDestModule() const;
|
||||||
|
uint16_t GetFrameNumber() const;
|
||||||
|
uint16_t GetFrameType() const;
|
||||||
|
const uint8_t *GetPayload() const;
|
||||||
|
const uint8_t *GetNonce() const;
|
||||||
|
void SetPayload(const uint8_t *newpayload);
|
||||||
|
uint16_t GetStreamId() const;
|
||||||
|
uint16_t GetCRC() const;
|
||||||
|
void SetCRC(uint16_t crc);
|
||||||
|
std::unique_ptr<CM17Packet> Duplicate(void) const;
|
||||||
|
bool IsLastPacket() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
CCallsign destination, source;
|
||||||
|
SM17Frame m17;
|
||||||
|
};
|
||||||
@ -0,0 +1,434 @@
|
|||||||
|
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
|
||||||
|
// ulxd -- The universal reflector
|
||||||
|
// Copyright © 2021 Thomas A. Early N7TAE
|
||||||
|
//
|
||||||
|
// 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include "Main.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "M17Client.h"
|
||||||
|
#include "M17Protocol.h"
|
||||||
|
#include "M17Packet.h"
|
||||||
|
#include "Reflector.h"
|
||||||
|
#include "GateKeeper.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// operation
|
||||||
|
|
||||||
|
bool CM17Protocol::Initialize(const char *type, const EProtocol ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6)
|
||||||
|
{
|
||||||
|
// base class
|
||||||
|
if (! CProtocol::Initialize(type, ptype, port, has_ipv4, has_ipv6))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// update time
|
||||||
|
m_LastKeepaliveTime.start();
|
||||||
|
|
||||||
|
// done
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// task
|
||||||
|
|
||||||
|
void CM17Protocol::Task(void)
|
||||||
|
{
|
||||||
|
CBuffer Buffer;
|
||||||
|
CIp Ip;
|
||||||
|
CCallsign Callsign;
|
||||||
|
char ToLinkModule;
|
||||||
|
std::unique_ptr<CDvHeaderPacket> Header;
|
||||||
|
std::unique_ptr<CDvFramePacket> Frame;
|
||||||
|
|
||||||
|
// handle incoming packets
|
||||||
|
#if DSTAR_IPV6==true
|
||||||
|
#if DSTAR_IPV4==true
|
||||||
|
if ( ReceiveDS(Buffer, Ip, 20) )
|
||||||
|
#else
|
||||||
|
if ( Receive6(Buffer, Ip, 20) )
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
if ( Receive4(Buffer, Ip, 20) )
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
// crack the packet
|
||||||
|
if ( IsValidDvPacket(Buffer, Header, Frame) )
|
||||||
|
{
|
||||||
|
// callsign muted?
|
||||||
|
if ( g_GateKeeper.MayTransmit(Header->GetMyCallsign(), Ip, EProtocol::m17, Header->GetRpt2Module()) )
|
||||||
|
{
|
||||||
|
OnDvHeaderPacketIn(Header, Ip);
|
||||||
|
|
||||||
|
if ( !Frame->IsLastPacket() )
|
||||||
|
{
|
||||||
|
//std::cout << "M17 DV frame" << std::endl;
|
||||||
|
OnDvFramePacketIn(Frame, &Ip);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//std::cout << "M17 DV last frame" << std::endl;
|
||||||
|
OnDvLastFramePacketIn((std::unique_ptr<CDvLastFramePacket> &)Frame, &Ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ( IsValidConnectPacket(Buffer, Callsign, ToLinkModule) )
|
||||||
|
{
|
||||||
|
std::cout << "M17 connect packet for module " << ToLinkModule << " from " << Callsign << " at " << Ip << std::endl;
|
||||||
|
|
||||||
|
// callsign authorized?
|
||||||
|
if ( g_GateKeeper.MayLink(Callsign, Ip, EProtocol::m17) && g_Reflector.IsValidModule(ToLinkModule) )
|
||||||
|
{
|
||||||
|
// valid module ?
|
||||||
|
if ( g_Reflector.IsValidModule(ToLinkModule) )
|
||||||
|
{
|
||||||
|
// acknowledge the request
|
||||||
|
Buffer.Set("ACKN");
|
||||||
|
Send(Buffer, Ip);
|
||||||
|
|
||||||
|
// create the client and append
|
||||||
|
g_Reflector.GetClients()->AddClient(std::make_shared<CM17Client>(Callsign, Ip, ToLinkModule));
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "M17 node " << Callsign << " connect attempt on non-existing module" << std::endl;
|
||||||
|
|
||||||
|
// deny the request
|
||||||
|
Buffer.Set("NACK");
|
||||||
|
Send(Buffer, Ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// deny the request
|
||||||
|
Buffer.Set("NACK");
|
||||||
|
Send(Buffer, Ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else if ( IsValidDisconnectPacket(Buffer, Callsign) )
|
||||||
|
{
|
||||||
|
std::cout << "M17 disconnect packet from " << Callsign << " at " << Ip << std::endl;
|
||||||
|
|
||||||
|
// find client
|
||||||
|
CClients *clients = g_Reflector.GetClients();
|
||||||
|
std::shared_ptr<CClient>client = clients->FindClient(Ip, EProtocol::m17);
|
||||||
|
if ( client != nullptr )
|
||||||
|
{
|
||||||
|
// remove it
|
||||||
|
clients->RemoveClient(client);
|
||||||
|
// and acknowledge the disconnect
|
||||||
|
Buffer.Set("DISC");
|
||||||
|
Send(Buffer, Ip);
|
||||||
|
}
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
}
|
||||||
|
else if ( IsValidKeepAlivePacket(Buffer, Callsign) )
|
||||||
|
{
|
||||||
|
//std::cout << "M17 keepalive packet from " << Callsign << " at " << Ip << std::endl;
|
||||||
|
|
||||||
|
// find all clients with that callsign & ip and keep them alive
|
||||||
|
CClients *clients = g_Reflector.GetClients();
|
||||||
|
auto it = clients->begin();
|
||||||
|
std::shared_ptr<CClient>client = nullptr;
|
||||||
|
while ( (client = clients->FindNextClient(Callsign, Ip, EProtocol::m17, it)) != nullptr )
|
||||||
|
{
|
||||||
|
client->Alive();
|
||||||
|
}
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// invalid packet
|
||||||
|
std::string title("Unknown M17 packet from ");
|
||||||
|
title += Ip.GetAddress();
|
||||||
|
Buffer.Dump(title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle end of streaming timeout
|
||||||
|
CheckStreamsTimeout();
|
||||||
|
|
||||||
|
// handle queue from reflector
|
||||||
|
HandleQueue();
|
||||||
|
|
||||||
|
// keep client alive
|
||||||
|
if ( m_LastKeepaliveTime.time() > M17_KEEPALIVE_PERIOD )
|
||||||
|
{
|
||||||
|
//
|
||||||
|
HandleKeepalives();
|
||||||
|
|
||||||
|
// update time
|
||||||
|
m_LastKeepaliveTime.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// streams helpers
|
||||||
|
|
||||||
|
void CM17Protocol::OnDvHeaderPacketIn(std::unique_ptr<CDvHeaderPacket> &Header, const CIp &Ip)
|
||||||
|
{
|
||||||
|
// find the stream
|
||||||
|
auto stream = GetStream(Header->GetStreamId());
|
||||||
|
if ( stream )
|
||||||
|
{
|
||||||
|
// stream already open
|
||||||
|
// skip packet, but tickle the stream
|
||||||
|
stream->Tickle();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// no stream open yet, open a new one
|
||||||
|
CCallsign my(Header->GetMyCallsign());
|
||||||
|
CCallsign rpt1(Header->GetRpt1Callsign());
|
||||||
|
CCallsign rpt2(Header->GetRpt2Callsign());
|
||||||
|
|
||||||
|
// find this client
|
||||||
|
std::shared_ptr<CClient>client = g_Reflector.GetClients()->FindClient(Ip, EProtocol::m17);
|
||||||
|
if ( client )
|
||||||
|
{
|
||||||
|
// get client callsign
|
||||||
|
rpt1 = client->GetCallsign();
|
||||||
|
// and try to open the stream
|
||||||
|
if ( (stream = g_Reflector.OpenStream(Header, client)) != nullptr )
|
||||||
|
{
|
||||||
|
// keep the handle
|
||||||
|
m_Streams.push_back(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// release
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
|
||||||
|
// update last heard
|
||||||
|
g_Reflector.GetUsers()->Hearing(my, rpt1, rpt2);
|
||||||
|
g_Reflector.ReleaseUsers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// queue helper
|
||||||
|
|
||||||
|
void CM17Protocol::HandleQueue(void)
|
||||||
|
{
|
||||||
|
m_Queue.Lock();
|
||||||
|
while ( !m_Queue.empty() )
|
||||||
|
{
|
||||||
|
// get the packet
|
||||||
|
auto packet = m_Queue.pop();
|
||||||
|
|
||||||
|
// get our sender's id
|
||||||
|
const auto module = packet->GetModule();
|
||||||
|
|
||||||
|
// check if it's header and update cache
|
||||||
|
if ( packet->IsDvHeader() )
|
||||||
|
{
|
||||||
|
// this relies on queue feeder setting valid module id
|
||||||
|
// m_StreamsCache[module] will be created if it doesn't exist
|
||||||
|
m_StreamsCache[module].m_dvHeader = CDvHeaderPacket((const CDvHeaderPacket &)*packet.get());
|
||||||
|
m_StreamsCache[module].m_iSeqCounter = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// encode it
|
||||||
|
SM17Frame frame;
|
||||||
|
memcpy(frame.magic, "M17 ", 4);
|
||||||
|
if ( packet->IsLastPacket() )
|
||||||
|
{
|
||||||
|
EncodeDvLastPacket(frame, m_StreamsCache[module].m_dvHeader, (const CDvFramePacket &)*packet.get(), m_StreamsCache[module].m_iSeqCounter++);
|
||||||
|
}
|
||||||
|
else if ( packet->IsDvFrame() )
|
||||||
|
{
|
||||||
|
EncodeDvPacket(frame, m_StreamsCache[module].m_dvHeader, (const CDvFramePacket &)*packet.get(), m_StreamsCache[module].m_iSeqCounter++);
|
||||||
|
}
|
||||||
|
|
||||||
|
// push it to all our clients linked to the module and who are not streaming in
|
||||||
|
CClients *clients = g_Reflector.GetClients();
|
||||||
|
auto it = clients->begin();
|
||||||
|
std::shared_ptr<CClient>client = nullptr;
|
||||||
|
while ( (client = clients->FindNextClient(EProtocol::m17, it)) != nullptr )
|
||||||
|
{
|
||||||
|
// is this client busy ?
|
||||||
|
if ( !client->IsAMaster() && (client->GetReflectorModule() == module) )
|
||||||
|
{
|
||||||
|
CBuffer Buffer(frame.magic, sizeof(SM17Frame));
|
||||||
|
// no, send the packet
|
||||||
|
Send(Buffer, client->GetIp());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_Queue.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// keepalive helpers
|
||||||
|
|
||||||
|
void CM17Protocol::HandleKeepalives(void)
|
||||||
|
{
|
||||||
|
// M17 protocol sends and monitors keepalives packets
|
||||||
|
// event if the client is currently streaming
|
||||||
|
// so, send keepalives to all
|
||||||
|
CBuffer keepalive;
|
||||||
|
EncodeKeepAlivePacket(keepalive);
|
||||||
|
|
||||||
|
// iterate on clients
|
||||||
|
CClients *clients = g_Reflector.GetClients();
|
||||||
|
auto it = clients->begin();
|
||||||
|
std::shared_ptr<CClient>client = nullptr;
|
||||||
|
while ( (client = clients->FindNextClient(EProtocol::m17, it)) != nullptr )
|
||||||
|
{
|
||||||
|
// send keepalive
|
||||||
|
Send(keepalive, client->GetIp());
|
||||||
|
|
||||||
|
// is this client busy ?
|
||||||
|
if ( client->IsAMaster() )
|
||||||
|
{
|
||||||
|
// yes, just tickle it
|
||||||
|
client->Alive();
|
||||||
|
}
|
||||||
|
// check it's still with us
|
||||||
|
else if ( !client->IsAlive() )
|
||||||
|
{
|
||||||
|
// no, disconnect
|
||||||
|
CBuffer disconnect;
|
||||||
|
disconnect.Set("DISC");
|
||||||
|
Send(disconnect, client->GetIp());
|
||||||
|
|
||||||
|
// remove it
|
||||||
|
std::cout << "M17 client " << client->GetCallsign() << " keepalive timeout" << std::endl;
|
||||||
|
clients->RemoveClient(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// packet decoding helpers
|
||||||
|
|
||||||
|
bool CM17Protocol::IsValidConnectPacket(const CBuffer &Buffer, CCallsign &callsign, char &mod)
|
||||||
|
{
|
||||||
|
uint8_t tag[] = { 'C', 'O', 'N', 'N' };
|
||||||
|
bool valid = false;
|
||||||
|
if (11 == Buffer.size() && 0 == Buffer.Compare(tag, 4))
|
||||||
|
{
|
||||||
|
callsign.CodeIn(Buffer.data() + 4);
|
||||||
|
mod = Buffer.data()[10];
|
||||||
|
valid = (callsign.IsValid() && IsLetter(mod));
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CM17Protocol::IsValidDisconnectPacket(const CBuffer &Buffer, CCallsign &callsign)
|
||||||
|
{
|
||||||
|
uint8_t tag[] = { 'D', 'I', 'S', 'C' };
|
||||||
|
bool valid = false;
|
||||||
|
if ((Buffer.size() == 10) && (0 == Buffer.Compare(tag, 4)))
|
||||||
|
{
|
||||||
|
callsign.CodeIn(Buffer.data() + 4);
|
||||||
|
valid = callsign.IsValid();
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CM17Protocol::IsValidKeepAlivePacket(const CBuffer &Buffer, CCallsign &callsign)
|
||||||
|
{
|
||||||
|
uint8_t tag[] = { 'P', 'O', 'N', 'G' };
|
||||||
|
bool valid = false;
|
||||||
|
if ( (Buffer.size() == 10) || (0 == Buffer.Compare(tag, 4)) )
|
||||||
|
{
|
||||||
|
callsign.CodeIn(Buffer.data() + 4);
|
||||||
|
valid = callsign.IsValid();
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CM17Protocol::IsValidDvPacket(const CBuffer &Buffer, std::unique_ptr<CDvHeaderPacket> &header, std::unique_ptr<CDvFramePacket> &frame)
|
||||||
|
{
|
||||||
|
uint8_t tag[] = { 'M', '1', '7', ' ' };
|
||||||
|
|
||||||
|
if ( (Buffer.size() == sizeof(SM17Frame)) && (0 == Buffer.Compare(tag, sizeof(tag))) && (0x4U == (0x1CU & Buffer[13])) )
|
||||||
|
// Buffer[13] is the lsb byte of the frametype. 0x4 means payload contains Codec2 voice data and there is no encryption.
|
||||||
|
// the 0x1CU mask just lets us see the encryptions bytes (must be zero) and the msb of the payload type (must be set)
|
||||||
|
{
|
||||||
|
// Make the M17 header
|
||||||
|
CM17Packet m17(Buffer.data());
|
||||||
|
// get the header
|
||||||
|
header = std::unique_ptr<CDvHeaderPacket>(new CDvHeaderPacket(m17));
|
||||||
|
|
||||||
|
// get the frame
|
||||||
|
if (m17.IsLastPacket())
|
||||||
|
{
|
||||||
|
// it's the last frame
|
||||||
|
frame = std::unique_ptr<CDvLastFramePacket>(new CDvLastFramePacket(m17));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// it's a regular DV frame
|
||||||
|
frame = std::unique_ptr<CDvFramePacket>(new CDvFramePacket(m17));
|
||||||
|
}
|
||||||
|
|
||||||
|
// check validity of packets
|
||||||
|
if ( header && header->IsValid() && frame && frame->IsValid() )
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// packet encoding helpers
|
||||||
|
|
||||||
|
void CM17Protocol::EncodeKeepAlivePacket(CBuffer &Buffer)
|
||||||
|
{
|
||||||
|
Buffer.resize(10);
|
||||||
|
memcpy(Buffer.data(), "PING", 4);
|
||||||
|
g_Reflector.GetCallsign().CodeOut(Buffer.data() + 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CM17Protocol::EncodeDvPacket(SM17Frame &frame, const CDvHeaderPacket &Header, const CDvFramePacket &DvFrame, uint32_t iSeq) const
|
||||||
|
{
|
||||||
|
ECodecType codec_in = Header.GetCodecIn(); // We'll need this
|
||||||
|
|
||||||
|
|
||||||
|
// do the lich structure first
|
||||||
|
// first, the dest and src callsigns
|
||||||
|
Header.GetRpt2Callsign().CodeOut(frame.lich.addr_dst);
|
||||||
|
CCallsign from = g_Reflector.GetCallsign();
|
||||||
|
from.SetModule(Header.GetModule());
|
||||||
|
from.CodeOut(frame.lich.addr_src);
|
||||||
|
// then the frame type, if the incoming frame is NOT an M17 1600, then it will be Voice only
|
||||||
|
frame.lich.frametype = htons((ECodecType::c2_1600==codec_in) ? 0x7U : 0x5U);
|
||||||
|
memcpy(frame.lich.nonce, DvFrame.GetNonce(), 14);
|
||||||
|
|
||||||
|
// now the main part of the packet
|
||||||
|
memcpy(frame.magic, "M17 ", 4);
|
||||||
|
// the frame number comes from the stream sequence counter
|
||||||
|
frame.framenumber = htons(iSeq % 0x8000U);
|
||||||
|
memcpy(frame.payload, DvFrame.GetCodecData(codec_in), 16);
|
||||||
|
frame.streamid = Header.GetStreamId(); // no host<--->network byte swapping since we never do any math on this value
|
||||||
|
// finally, calcualte the m17 CRC value and load it
|
||||||
|
frame.crc = htons(m17crc.CalcCRC(frame.magic, sizeof(SM17Frame)-2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CM17Protocol::EncodeDvLastPacket(SM17Frame &frame, const CDvHeaderPacket &Header, const CDvFramePacket &DvFrame, uint32_t iSeq) const
|
||||||
|
{
|
||||||
|
EncodeDvPacket(frame, Header, DvFrame, iSeq);
|
||||||
|
frame.framenumber |= 0x8000U;
|
||||||
|
}
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
|
||||||
|
// ulxd -- The universal reflector
|
||||||
|
// Copyright © 2021 Thomas A. Early N7TAE
|
||||||
|
//
|
||||||
|
// 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Timer.h"
|
||||||
|
#include "Protocol.h"
|
||||||
|
#include "DVHeaderPacket.h"
|
||||||
|
#include "DVFramePacket.h"
|
||||||
|
#include "DVLastFramePacket.h"
|
||||||
|
#include "M17CRC.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// define
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CM17StreamCacheItem
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CM17StreamCacheItem() : m_iSeqCounter(0) {}
|
||||||
|
|
||||||
|
CDvHeaderPacket m_dvHeader;
|
||||||
|
uint32_t m_iSeqCounter;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CM17Protocol : public CProtocol
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// initialization
|
||||||
|
bool Initialize(const char *type, const EProtocol ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6);
|
||||||
|
|
||||||
|
// task
|
||||||
|
void Task(void);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// queue helper
|
||||||
|
void HandleQueue(void);
|
||||||
|
|
||||||
|
// keepalive helpers
|
||||||
|
void HandleKeepalives(void);
|
||||||
|
|
||||||
|
// stream helpers
|
||||||
|
void OnDvHeaderPacketIn(std::unique_ptr<CDvHeaderPacket> &, const CIp &);
|
||||||
|
|
||||||
|
// packet decoding helpers
|
||||||
|
bool IsValidConnectPacket(const CBuffer &, CCallsign &, char &);
|
||||||
|
bool IsValidDisconnectPacket(const CBuffer &, CCallsign &);
|
||||||
|
bool IsValidKeepAlivePacket(const CBuffer &, CCallsign &);
|
||||||
|
bool IsValidDvPacket(const CBuffer &, std::unique_ptr<CDvHeaderPacket> &, std::unique_ptr<CDvFramePacket> &);
|
||||||
|
|
||||||
|
// packet encoding helpers
|
||||||
|
void EncodeKeepAlivePacket(CBuffer &);
|
||||||
|
void EncodeDvPacket(SM17Frame &, const CDvHeaderPacket &, const CDvFramePacket &, uint32_t) const;
|
||||||
|
void EncodeDvLastPacket(SM17Frame &, const CDvHeaderPacket &, const CDvFramePacket &, uint32_t) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// for keep alive
|
||||||
|
CTimer m_LastKeepaliveTime;
|
||||||
|
|
||||||
|
// for queue header caches
|
||||||
|
std::unordered_map<char, CM17StreamCacheItem> m_StreamsCache;
|
||||||
|
|
||||||
|
private:
|
||||||
|
CM17CRC m17crc;
|
||||||
|
};
|
||||||
@ -0,0 +1,207 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2020 by Thomas Early N7TAE
|
||||||
|
*
|
||||||
|
* 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 <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include "UnixPacketSock.h"
|
||||||
|
|
||||||
|
CUnixPacket::CUnixPacket() : m_fd(-1), m_host(NULL) {}
|
||||||
|
|
||||||
|
ssize_t CUnixPacket::Read(void *buffer, const ssize_t size)
|
||||||
|
{
|
||||||
|
if (0 > m_fd)
|
||||||
|
return -1;
|
||||||
|
ssize_t len = read(m_fd, buffer, size);
|
||||||
|
if (len < 1)
|
||||||
|
{
|
||||||
|
if (-1 == len)
|
||||||
|
{
|
||||||
|
std::cerr << "Read error on '" << m_name << "': " << strerror(errno) << std::endl;
|
||||||
|
}
|
||||||
|
else if (0 == len)
|
||||||
|
{
|
||||||
|
std::cerr << "Read error on '" << m_name << "': EOF" << std::endl;
|
||||||
|
}
|
||||||
|
if (Restart())
|
||||||
|
return -1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixPacket::Write(const void *buffer, const ssize_t size)
|
||||||
|
{
|
||||||
|
if (0 > m_fd)
|
||||||
|
return true;
|
||||||
|
ssize_t written = write(m_fd, buffer, size);
|
||||||
|
if (written != size)
|
||||||
|
{
|
||||||
|
if (-1 == written)
|
||||||
|
{
|
||||||
|
std::cerr << "Write error on '" << m_name << "': " << strerror(errno) << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Write error on '" << m_name << "': Only wrote " << written << " of " << size << " bytes" << std::endl;
|
||||||
|
}
|
||||||
|
return Restart();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixPacket::Restart()
|
||||||
|
{
|
||||||
|
if (! m_host->IsRunning())
|
||||||
|
return true;
|
||||||
|
std::cout << "Restarting '" << m_name << "'... " << std::endl;
|
||||||
|
Close();
|
||||||
|
std::string name(m_name);
|
||||||
|
return Open(name.c_str(), m_host);
|
||||||
|
}
|
||||||
|
|
||||||
|
int CUnixPacket::GetFD()
|
||||||
|
{
|
||||||
|
return m_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
CUnixPacketServer::CUnixPacketServer() : m_server(-1) {}
|
||||||
|
|
||||||
|
CUnixPacketServer::~CUnixPacketServer()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixPacketServer::Open(const char *name, CKRBase *host)
|
||||||
|
{
|
||||||
|
m_server = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
||||||
|
m_host = host;
|
||||||
|
if (m_server < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Cannot open '" << name << "' socket: " << strerror(errno) << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
memcpy(addr.sun_path+1, name, strlen(name));
|
||||||
|
if (-1 == bind(m_server, (struct sockaddr *)&addr, sizeof(addr)))
|
||||||
|
{
|
||||||
|
std::cerr << "Cannot bind '" << name << "' socket: " << strerror(errno) << std::endl;
|
||||||
|
Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-1 == listen(m_server, 1))
|
||||||
|
{
|
||||||
|
std::cerr << "Cannot listen on '" << name << "' socket: " << strerror(errno) << std::endl;
|
||||||
|
Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_fd = accept(m_server, nullptr, 0);
|
||||||
|
if (m_fd < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Cannot accept on '" << name << "' socket: " << strerror(errno) << std::endl;
|
||||||
|
Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(m_name, name, 108);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CUnixPacketServer::Close()
|
||||||
|
{
|
||||||
|
if (m_server >= 0)
|
||||||
|
{
|
||||||
|
close(m_server);
|
||||||
|
m_server = -1;
|
||||||
|
}
|
||||||
|
if (m_fd >= 0)
|
||||||
|
{
|
||||||
|
close(m_fd);
|
||||||
|
m_fd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CUnixPacketClient::~CUnixPacketClient()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixPacketClient::Open(const char *name, CKRBase *host)
|
||||||
|
{
|
||||||
|
m_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
||||||
|
if (m_fd < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Cannot open unix client socket " << name << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
memcpy(addr.sun_path+1, name, strlen(name));
|
||||||
|
int rval = -1;
|
||||||
|
int tries = 0;
|
||||||
|
while (rval < 0)
|
||||||
|
{
|
||||||
|
rval = connect(m_fd, (struct sockaddr *)&addr, sizeof(addr));
|
||||||
|
if (rval < 0)
|
||||||
|
{
|
||||||
|
if (ECONNREFUSED == errno)
|
||||||
|
{
|
||||||
|
if (0 == tries++ % 20)
|
||||||
|
std::cout << "Waiting for " << name << " server to start..." << std::endl;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cerr << "Cannot connect '" << name << "' socket: " << strerror(errno) << std::endl;
|
||||||
|
Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (! m_host->IsRunning())
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_host = host;
|
||||||
|
strncpy(m_name, name, 108);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CUnixPacketClient::Close()
|
||||||
|
{
|
||||||
|
if (m_fd >= 0)
|
||||||
|
{
|
||||||
|
close(m_fd);
|
||||||
|
m_fd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2020 by Thomas Early N7TAE
|
||||||
|
*
|
||||||
|
* 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 <sys/types.h>
|
||||||
|
|
||||||
|
#include "KRBase.h"
|
||||||
|
|
||||||
|
class CUnixPacket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CUnixPacket();
|
||||||
|
virtual bool Open(const char *name, CKRBase *host) = 0;
|
||||||
|
virtual void Close() = 0;
|
||||||
|
bool Write(const void *buffer, const ssize_t size);
|
||||||
|
ssize_t Read(void *buffer, const ssize_t size);
|
||||||
|
int GetFD();
|
||||||
|
protected:
|
||||||
|
bool Restart();
|
||||||
|
int m_fd;
|
||||||
|
CKRBase *m_host;
|
||||||
|
char m_name[108];
|
||||||
|
};
|
||||||
|
|
||||||
|
class CUnixPacketServer : public CUnixPacket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CUnixPacketServer();
|
||||||
|
~CUnixPacketServer();
|
||||||
|
bool Open(const char *name, CKRBase *host);
|
||||||
|
void Close();
|
||||||
|
protected:
|
||||||
|
int m_server;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CUnixPacketClient : public CUnixPacket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~CUnixPacketClient();
|
||||||
|
bool Open(const char *name, CKRBase *host);
|
||||||
|
void Close();
|
||||||
|
};
|
||||||
Loading…
Reference in new issue