mirror of https://github.com/nostar/urfd.git
parent
381268fb0b
commit
a5495deb85
@ -0,0 +1,223 @@
|
||||
// urfd -- The universal reflector
|
||||
// Copyright © 2024 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 <iostream>
|
||||
#include <unistd.h>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "IP.h"
|
||||
#include "TCTCPSocket.h"
|
||||
|
||||
void CTCTCPSocket::Close()
|
||||
{
|
||||
for (auto item : m_FD)
|
||||
close(item.second);
|
||||
m_FD.clear();
|
||||
}
|
||||
|
||||
void CTCTCPSocket::Close(char mod)
|
||||
{
|
||||
auto item = m_FD.find(mod);
|
||||
if (m_FD.end() == item)
|
||||
return;
|
||||
|
||||
close(item->second);
|
||||
m_FD.erase(item);
|
||||
}
|
||||
|
||||
bool CTCTCPSocket::Send(const STCPacket *packet)
|
||||
{
|
||||
int fd = GetFD(packet->module);
|
||||
if (fd < 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
long count = 0;
|
||||
auto data = (const unsigned char *)packet;
|
||||
do {
|
||||
auto n = send(fd, data+count, sizeof(STCPacket)-count, 0);
|
||||
if (n <= 0)
|
||||
{
|
||||
if (0 == n)
|
||||
{
|
||||
std::cerr << "CTCTCPSocket::Send: socket on module '" << packet->module << "' has been closed!" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
perror("CTCTCPSocket::Send");
|
||||
}
|
||||
Close(packet->module);
|
||||
return true;
|
||||
}
|
||||
count += n;
|
||||
} while (count < sizeof(STCPacket));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CTCTCPSocket::Receive(int fd, STCPacket *packet)
|
||||
{
|
||||
auto data = (unsigned char *)packet;
|
||||
auto n = recv(fd, data, sizeof(STCPacket), MSG_WAITALL);
|
||||
if (n < 0)
|
||||
{
|
||||
perror("CTCTCPSocket::Receive");
|
||||
return true;
|
||||
}
|
||||
return n == sizeof(STCPacket);
|
||||
}
|
||||
|
||||
int CTCTCPSocket::GetFD(char module) const
|
||||
{
|
||||
const auto item = m_FD.find(module);
|
||||
if (m_FD.cend() == item)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return item->second;
|
||||
}
|
||||
|
||||
bool CTCTCPServer::Open(const std::string &address, const std::string &tcmodules, uint16_t port)
|
||||
{
|
||||
int fd;
|
||||
CIp ip(address.c_str(), AF_UNSPEC, SOCK_STREAM, port);
|
||||
|
||||
fd = socket(ip.GetFamily(), SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("Open socket");
|
||||
return true;
|
||||
}
|
||||
|
||||
int yes = 1;
|
||||
int rv = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
||||
if (rv < 0)
|
||||
{
|
||||
close(fd);
|
||||
perror("Open setsockopt");
|
||||
return true;
|
||||
}
|
||||
|
||||
rv = bind(fd, ip.GetCPointer(), ip.GetSize());
|
||||
if (rv < 0)
|
||||
{
|
||||
close(fd);
|
||||
perror("Open bind");
|
||||
return true;
|
||||
}
|
||||
|
||||
rv = listen(fd, 3);
|
||||
if (rv < 0)
|
||||
{
|
||||
perror("Open listen");
|
||||
close(fd);
|
||||
Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::cout << "Waiting for " << tcmodules.size() << " transcoder connection(s)..." << std::endl;
|
||||
for (unsigned x=0; x<tcmodules.size(); x++)
|
||||
{
|
||||
CIp their_addr; // connector's address information
|
||||
|
||||
socklen_t sin_size = sizeof(struct sockaddr_storage);
|
||||
|
||||
auto newfd = accept(fd, their_addr.GetPointer(), &sin_size);
|
||||
if (newfd < 0)
|
||||
{
|
||||
perror("Open accept");
|
||||
Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
char mod;
|
||||
rv = recv(newfd, &mod, 1, 0); // retrieve the identification byte
|
||||
if (rv != 1)
|
||||
{
|
||||
if (rv < 0) perror("Open recv");
|
||||
else std::cerr << "recv got no identification byte!" << std::endl;
|
||||
close(newfd);
|
||||
Close();
|
||||
return true;
|
||||
}
|
||||
if (std::string::npos == tcmodules.find(mod))
|
||||
{
|
||||
std::cerr << "New connection for module '" << mod << "', but it's not config'ed!" << std::endl;
|
||||
std::cerr << "The transcoded modules need to be configured identically for both urfd and tcd." << std::endl;
|
||||
close(newfd);
|
||||
Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::cout << "File descriptor " << newfd << " opened TCP port for module '" << mod << "' on " << their_addr << std::endl;
|
||||
m_FD[mod] = newfd;
|
||||
}
|
||||
close(fd); // we don't need this anymore.
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CTCTCPClient::Open(const std::string &address, const std::string &tcmodules, uint16_t port)
|
||||
{
|
||||
std::cout << "Connecting to the TCP server..." << std::endl;
|
||||
for (auto c : tcmodules)
|
||||
{
|
||||
CIp ip(address.c_str(), AF_UNSPEC, SOCK_STREAM, port);
|
||||
|
||||
auto fd = socket(ip.GetFamily(), SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
std::cout << "errno=" << errno << std::endl;
|
||||
perror("Open socket");
|
||||
Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
int yes = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)))
|
||||
{
|
||||
perror("Open setsockopt");
|
||||
Close();
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned count = 0;
|
||||
while (connect(fd, ip.GetCPointer(), ip.GetSize()))
|
||||
{
|
||||
if (ECONNREFUSED == errno)
|
||||
{
|
||||
if (0 == count++ % 15) std::cout << "Connection refused! Restart the server." << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
else
|
||||
{
|
||||
perror("Open connect");
|
||||
Close();
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "File descriptor " << fd << " on " << ip << " opened for module '" << c << "'" << std::endl;
|
||||
send(fd, &c, 1, 0); // send the identification byte
|
||||
m_FD[c] = fd;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
// urfd -- The universal reflector
|
||||
// Copyright © 2024 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 <string>
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "TCPacketDef.h"
|
||||
|
||||
class CTCTCPSocket
|
||||
{
|
||||
public:
|
||||
CTCTCPSocket() {}
|
||||
virtual ~CTCTCPSocket() { Close(); }
|
||||
|
||||
void Close(); // close all open sockets
|
||||
|
||||
// bool functions return true on failure
|
||||
virtual bool Open(const std::string &address, const std::string &modules, uint16_t port) = 0;
|
||||
bool Send(const STCPacket *packet);
|
||||
bool Receive(int fd, STCPacket *packet);
|
||||
|
||||
int GetFD(char module) const; // can return -1!
|
||||
|
||||
protected:
|
||||
void Close(char); // close a specific module
|
||||
|
||||
std::unordered_map<char, int> m_FD;
|
||||
};
|
||||
|
||||
class CTCTCPServer : public CTCTCPSocket
|
||||
{
|
||||
public:
|
||||
CTCTCPServer() : CTCTCPSocket() {}
|
||||
~CTCTCPServer() {}
|
||||
bool Open(const std::string &address, const std::string &modules, uint16_t port);
|
||||
};
|
||||
|
||||
class CTCTCPClient : public CTCTCPSocket
|
||||
{
|
||||
public:
|
||||
CTCTCPClient() : CTCTCPSocket() {}
|
||||
~CTCTCPClient() {}
|
||||
bool Open(const std::string &address, const std::string &modules, uint16_t port);
|
||||
};
|
||||
@ -1,167 +0,0 @@
|
||||
// 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 <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);
|
||||
|
||||
// We know path is a string, so we skip the first null, get the string length and add 1 for the begining Null
|
||||
int path_len = sizeof(addr.sun_family) + strlen(addr.sun_path + 1) + 1;
|
||||
int rval = bind(fd, (struct sockaddr *)&addr, path_len);
|
||||
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 false;
|
||||
|
||||
// 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 false;
|
||||
}
|
||||
|
||||
return Read(pack);
|
||||
}
|
||||
|
||||
bool CUnixDgramReader::Read(STCPacket *pack) const
|
||||
{
|
||||
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 false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
path_len = sizeof(addr.sun_family) + strlen(addr.sun_path + 1) + 1;
|
||||
}
|
||||
|
||||
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
|
||||
// We know path is a string, so we skip the first null, get the string length and add 1 for the begining Null
|
||||
int rval = connect(fd, (struct sockaddr *)&addr, path_len);
|
||||
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;
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// 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 "TCPacketDef.h"
|
||||
|
||||
class CUnixDgramReader
|
||||
{
|
||||
public:
|
||||
CUnixDgramReader();
|
||||
~CUnixDgramReader();
|
||||
bool Open(const char *path);
|
||||
bool Read(STCPacket *pack) const;
|
||||
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;
|
||||
int path_len;
|
||||
};
|
||||
Loading…
Reference in new issue