mirror of https://github.com/nostar/urfd.git
parent
a7e52fc330
commit
60bc3ab68f
@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// urfd -- 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 <cstdint>
|
||||||
|
|
||||||
|
enum class ECodecType { none, dstar, dmr, c2_1600, c2_3200 };
|
||||||
|
|
||||||
|
using STCPacket = struct tcpacket_tag {
|
||||||
|
char module;
|
||||||
|
bool is_last_packet;
|
||||||
|
bool has_valid_m17;
|
||||||
|
uint16_t streamid;
|
||||||
|
ECodecType codec_in;
|
||||||
|
uint8_t dstar[9];
|
||||||
|
uint8_t dmr[9];
|
||||||
|
uint8_t m17[16];
|
||||||
|
};
|
||||||
@ -0,0 +1,161 @@
|
|||||||
|
// urfd -- 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 <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "UnixDgramSocket.h"
|
||||||
|
|
||||||
|
CUnixDgramReader::CUnixDgramReader() : fd(-1) {}
|
||||||
|
|
||||||
|
CUnixDgramReader::~CUnixDgramReader()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixDgramReader::Open(const char *path) // returns true on failure
|
||||||
|
{
|
||||||
|
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "socket() failed for " << path << ": " << strerror(errno) << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||||
|
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
strncpy(addr.sun_path+1, path, sizeof(addr.sun_path)-2);
|
||||||
|
|
||||||
|
int rval = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
|
||||||
|
if (rval < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "bind() failed for " << path << ": " << strerror(errno) << std::endl;
|
||||||
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixDgramReader::Receive(STCPacket *pack, unsigned timeout) const
|
||||||
|
{
|
||||||
|
// socket valid ?
|
||||||
|
if ( 0 > fd )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// control socket
|
||||||
|
fd_set FdSet;
|
||||||
|
FD_ZERO(&FdSet);
|
||||||
|
FD_SET(fd, &FdSet);
|
||||||
|
struct timeval tv;
|
||||||
|
tv.tv_sec = timeout / 1000;
|
||||||
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
|
|
||||||
|
auto rval = select(fd + 1, &FdSet, 0, 0, &tv);
|
||||||
|
if (rval <= 0) {
|
||||||
|
if (rval < 0) {
|
||||||
|
std::cerr << "select() error on transcoder socket: " << strerror(errno) << std::endl;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto len = read(fd, pack, sizeof(STCPacket));
|
||||||
|
if (len != sizeof(STCPacket)) {
|
||||||
|
std::cerr << "Received transcoder packet is wrong size: " << len << " but should be " << sizeof(STCPacket) << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CUnixDgramReader::Close()
|
||||||
|
{
|
||||||
|
if (fd >= 0)
|
||||||
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CUnixDgramReader::GetFD() const
|
||||||
|
{
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
CUnixDgramWriter::CUnixDgramWriter() {}
|
||||||
|
|
||||||
|
CUnixDgramWriter::~CUnixDgramWriter() {}
|
||||||
|
|
||||||
|
void CUnixDgramWriter::SetUp(const char *path) // returns true on failure
|
||||||
|
{
|
||||||
|
// setup the socket address
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
strncpy(addr.sun_path+1, path, sizeof(addr.sun_path)-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUnixDgramWriter::Send(const STCPacket *pack) const
|
||||||
|
{
|
||||||
|
auto len = Write(pack, sizeof(STCPacket));
|
||||||
|
|
||||||
|
if (len != sizeof(STCPacket))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t CUnixDgramWriter::Write(const void *buf, ssize_t size) const
|
||||||
|
{
|
||||||
|
// open the socket
|
||||||
|
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "socket() failed for " << addr.sun_path+1 << ": " << strerror(errno) << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// connect to the receiver
|
||||||
|
int rval = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
|
||||||
|
if (rval < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "connect() failed for " << addr.sun_path+1 << ": " << strerror(errno) << std::endl;
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto written = write(fd, buf, size);
|
||||||
|
if (written != size) {
|
||||||
|
std::cerr << "write on " << addr.sun_path+1;
|
||||||
|
if (written < 0)
|
||||||
|
std::cerr << " returned error: " << strerror(errno) << std::endl;
|
||||||
|
else if (written == 0)
|
||||||
|
std::cerr << " returned zero" << std::endl;
|
||||||
|
else
|
||||||
|
std::cerr << " only wrote " << written << " bytes, should be " << size << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
return written;
|
||||||
|
}
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// urfd -- 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 <stdlib.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
|
#include "Defines.h"
|
||||||
|
|
||||||
|
class CUnixDgramReader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CUnixDgramReader();
|
||||||
|
~CUnixDgramReader();
|
||||||
|
bool Open(const char *path);
|
||||||
|
bool Receive(STCPacket *pack, unsigned timeout) const;
|
||||||
|
void Close();
|
||||||
|
int GetFD() const;
|
||||||
|
private:
|
||||||
|
int fd;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CUnixDgramWriter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CUnixDgramWriter();
|
||||||
|
~CUnixDgramWriter();
|
||||||
|
void SetUp(const char *path);
|
||||||
|
bool Send(const STCPacket *pack) const;
|
||||||
|
private:
|
||||||
|
ssize_t Write(const void *buf, ssize_t size) const;
|
||||||
|
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
};
|
||||||
@ -1,207 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
#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