1
0
Fork 0

Convert to IPv6

ycs232-kbc
Jonathan Naylor 4 years ago
parent 8283be1042
commit 0e064b93e6

@ -36,8 +36,8 @@ m_longitude(0.0F),
m_height(0),
m_desc(),
m_suffix(suffix),
m_aprsAddress(),
m_aprsPort(port),
m_aprsAddr(),
m_aprsAddrLen(),
m_aprsSocket()
#if defined(USE_GPSD)
,m_gpsdEnabled(false),
@ -55,7 +55,7 @@ m_gpsdData()
m_callsign.append(rptSuffix.substr(0U, 1U));
}
m_aprsAddress = CUDPSocket::lookup(address);
CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen);
}
CAPRSWriter::~CAPRSWriter()
@ -176,7 +176,7 @@ void CAPRSWriter::write(const unsigned char* source, const char* type, unsigned
if (m_debug)
LogDebug("APRS ==> %s", output);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddr, m_aprsAddrLen);
}
void CAPRSWriter::clock(unsigned int ms)
@ -275,7 +275,7 @@ void CAPRSWriter::sendIdFrameFixed()
if (m_debug)
LogDebug("APRS ==> %s", output);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddr, m_aprsAddrLen);
}
#if defined(USE_GPSD)
@ -376,6 +376,6 @@ void CAPRSWriter::sendIdFrameMobile()
if (m_debug)
LogDebug("APRS ==> %s", output);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddress, m_aprsPort);
m_aprsSocket.write((unsigned char*)output, (unsigned int)::strlen(output), m_aprsAddr, m_aprsAddrLen);
}
#endif

@ -70,8 +70,8 @@ private:
int m_height;
std::string m_desc;
std::string m_suffix;
in_addr m_aprsAddress;
unsigned int m_aprsPort;
sockaddr_storage m_aprsAddr;
unsigned int m_aprsAddrLen;
CUDPSocket m_aprsSocket;
#if defined(USE_GPSD)
bool m_gpsdEnabled;

@ -32,7 +32,8 @@ const unsigned int BUFFER_LENGTH = 200U;
CFCSNetwork::CFCSNetwork(unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, bool debug) :
m_socket(port),
m_debug(debug),
m_address(),
m_addr(),
m_addrLen(),
m_ping(NULL),
m_info(NULL),
m_reflector(),
@ -64,17 +65,48 @@ bool CFCSNetwork::open()
{
LogMessage("Resolving FCS00x addresses");
m_addresses["FCS001"] = CUDPSocket::lookup("fcs001.xreflector.net");
m_addresses["FCS002"] = CUDPSocket::lookup("fcs002.xreflector.net");
m_addresses["FCS003"] = CUDPSocket::lookup("fcs003.xreflector.net");
m_addresses["FCS004"] = CUDPSocket::lookup("fcs004.xreflector.net");
m_addresses["FCS005"] = CUDPSocket::lookup("fcs005.xreflector.net");
m_addresses["FCS222"] = CUDPSocket::lookup("fcs222.xreflector.net");
m_addresses["FCS224"] = CUDPSocket::lookup("fcs224.xreflector.net");
m_addresses["FCS232"] = CUDPSocket::lookup("fcs232.xreflector.net");
m_addresses["FCS260"] = CUDPSocket::lookup("fcs260.xreflector.net");
m_addresses["FCS262"] = CUDPSocket::lookup("fcs262.xreflector.net");
sockaddr_storage addr;
unsigned int addrLen;
CUDPSocket::lookup("fcs001.xreflector.net", FCS_PORT, addr, addrLen);
std::pair<sockaddr_storage, unsigned int> entry = std::make_pair(addr, addrLen);
m_addresses["FCS001"] = entry;
CUDPSocket::lookup("fcs002.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS002"] = entry;
CUDPSocket::lookup("fcs003.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS003"] = entry;
CUDPSocket::lookup("fcs004.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS004"] = entry;
CUDPSocket::lookup("fcs005.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS005"] = entry;
CUDPSocket::lookup("fcs222.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS222"] = entry;
CUDPSocket::lookup("fcs224.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS224"] = entry;
CUDPSocket::lookup("fcs232.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS232"] = entry;
CUDPSocket::lookup("fcs260.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS260"] = entry;
CUDPSocket::lookup("fcs262.xreflector.net", FCS_PORT, addr, addrLen);
entry = std::make_pair(addr, addrLen);
m_addresses["FCS262"] = entry;
LogMessage("Opening FCS network connection");
@ -104,7 +136,7 @@ void CFCSNetwork::write(const unsigned char* data)
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", buffer, 130U);
m_socket.write(buffer, 130U, m_address, FCS_PORT);
m_socket.write(buffer, 130U, m_addr, m_addrLen);
}
bool CFCSNetwork::writeLink(const std::string& reflector)
@ -116,11 +148,9 @@ bool CFCSNetwork::writeLink(const std::string& reflector)
return false;
}
m_address = m_addresses[name];
if (m_address.s_addr == INADDR_NONE) {
LogError("FCS reflector %s has no address", name.c_str());
return false;
}
std::pair<sockaddr_storage, unsigned int> entry = m_addresses[name];
m_addr = entry.first;
m_addrLen = entry.second;
}
m_reflector = reflector;
@ -143,7 +173,7 @@ void CFCSNetwork::writeUnlink(unsigned int count)
return;
for (unsigned int i = 0U; i < count; i++)
m_socket.write((unsigned char*)"CLOSE ", 11U, m_address, FCS_PORT);
m_socket.write((unsigned char*)"CLOSE ", 11U, m_addr, m_addrLen);
}
void CFCSNetwork::clock(unsigned int ms)
@ -162,16 +192,16 @@ void CFCSNetwork::clock(unsigned int ms)
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return;
if (m_state == FCS_UNLINKED)
return;
if (address.s_addr != m_address.s_addr || port != FCS_PORT)
if (!CUDPSocket::match(addr, m_addr))
return;
if (m_debug)
@ -251,7 +281,7 @@ void CFCSNetwork::writeInfo()
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_info, 100U);
m_socket.write(m_info, 100U, m_address, FCS_PORT);
m_socket.write(m_info, 100U, m_addr, m_addrLen);
}
void CFCSNetwork::writePing()
@ -262,5 +292,5 @@ void CFCSNetwork::writePing()
if (m_debug)
CUtils::dump(1U, "FCS Network Data Sent", m_ping, 25U);
m_socket.write(m_ping, 25U, m_address, FCS_PORT);
m_socket.write(m_ping, 25U, m_addr, m_addrLen);
}

@ -58,13 +58,14 @@ public:
private:
CUDPSocket m_socket;
bool m_debug;
in_addr m_address;
sockaddr_storage m_addr;
unsigned int m_addrLen;
unsigned char* m_ping;
unsigned char* m_info;
std::string m_reflector;
std::string m_print;
CRingBuffer<unsigned char> m_buffer;
std::map<std::string, in_addr> m_addresses;
std::map<std::string, std::pair<sockaddr_storage, unsigned int>> m_addresses;
unsigned char m_n;
CTimer m_pingTimer;
CTimer m_resetTimer;

@ -1,5 +1,5 @@
/*
* Copyright (C) 2006-2016 by Jonathan Naylor G4KLX
* Copyright (C) 2006-2016,2020 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
@ -62,49 +62,91 @@ CUDPSocket::~CUDPSocket()
#endif
}
in_addr CUDPSocket::lookup(const std::string& hostname)
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
{
in_addr addr;
#if defined(_WIN32) || defined(_WIN64)
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
}
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{
std::string portstr = std::to_string(port);
struct addrinfo *res;
/* port is always digits, no needs to lookup service */
hints.ai_flags |= AI_NUMERICSERV;
int err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err != 0) {
sockaddr_in* paddr = (sockaddr_in*)&addr;
::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in));
paddr->sin_family = AF_INET;
paddr->sin_port = htons(port);
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
LogError("Cannot find address for host %s", hostname.c_str());
return err;
}
LogError("Cannot find address for host %s", hostname.c_str());
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
addr.s_addr = INADDR_NONE;
return addr;
#else
in_addr_t address = ::inet_addr(hostname.c_str());
if (address != in_addr_t(-1)) {
addr.s_addr = address;
return addr;
}
freeaddrinfo(res);
return 0;
}
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
switch (addr1.ss_family) {
case AF_INET:
struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2;
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2;
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
default:
return false;
}
}
LogError("Cannot find address for host %s", hostname.c_str());
bool CUDPSocket::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
addr.s_addr = INADDR_NONE;
return addr;
#endif
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
}
bool CUDPSocket::open()
{
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
sockaddr_storage addr;
unsigned int addrlen;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = af;
/* to determine protocol family, call lookup() first. */
int err = lookup(m_address, m_port, addr, addrlen, hints);
if (err != 0) {
LogError("The local address is invalid - %s", m_address.c_str());
return false;
}
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
if (m_fd < 0) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
@ -115,24 +157,6 @@ bool CUDPSocket::open()
}
if (m_port > 0U) {
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64)
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#else
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#endif
if (addr.sin_addr.s_addr == INADDR_NONE) {
LogError("The local address is invalid - %s", m_address.c_str());
return false;
}
}
int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
@ -143,7 +167,7 @@ bool CUDPSocket::open()
return false;
}
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else
@ -158,7 +182,7 @@ bool CUDPSocket::open()
return true;
}
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
{
assert(buffer != NULL);
assert(length > 0U);
@ -190,17 +214,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0)
return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in);
int size = sizeof(sockaddr_storage);
#else
socklen_t size = sizeof(sockaddr_in);
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -211,28 +234,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1;
}
address = addr.sin_addr;
port = ntohs(addr.sin_port);
address_length = size;
return len;
}
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
{
assert(buffer != NULL);
assert(length > 0U);
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr = address;
addr.sin_port = htons(port);
#if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)

@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2011,2013,2015,2016 by Jonathan Naylor G4KLX
* Copyright (C) 2009-2011,2013,2015,2016,2020 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
@ -31,7 +31,8 @@
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
class CUDPSocket {
@ -41,13 +42,17 @@ public:
~CUDPSocket();
bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length);
bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close();
static in_addr lookup(const std::string& hostName);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2);
static bool isnone(const sockaddr_storage& addr);
private:
std::string m_address;

@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20200803";
const char* VERSION = "20200903";
#endif

@ -184,14 +184,16 @@ int CYSFGateway::run()
m_callsign = m_conf.getCallsign();
m_suffix = m_conf.getSuffix();
bool debug = m_conf.getNetworkDebug();
in_addr rptAddress = CUDPSocket::lookup(m_conf.getRptAddress());
unsigned int rptPort = m_conf.getRptPort();
bool debug = m_conf.getNetworkDebug();
sockaddr_storage rptAddr;
unsigned int rptAddrLen;
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen);
std::string myAddress = m_conf.getMyAddress();
unsigned int myPort = m_conf.getMyPort();
CYSFNetwork rptNetwork(myAddress, myPort, m_callsign, debug);
rptNetwork.setDestination("MMDVM", rptAddress, rptPort);
rptNetwork.setDestination("MMDVM", rptAddr, rptAddrLen);
ret = rptNetwork.open();
if (!ret) {
@ -550,7 +552,7 @@ bool CYSFGateway::processWiresX(const unsigned char* buffer, unsigned char fi, u
CYSFReflector* reflector = m_wiresX->getReflector();
LogMessage("Connect to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_address, reflector->m_port);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_addr, reflector->m_addrLen);
m_ysfNetwork->writePoll(3U);
m_current = reflector->m_id;
@ -660,7 +662,7 @@ void CYSFGateway::processDTMF(unsigned char* buffer, unsigned char dt)
LogMessage("Connect via DTMF to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_address, reflector->m_port);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_addr, reflector->m_addrLen);
m_ysfNetwork->writePoll(3U);
m_current = id;
@ -825,7 +827,7 @@ void CYSFGateway::startupLinking()
m_wiresX->setReflector(reflector);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_address, reflector->m_port);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_addr, reflector->m_addrLen);
m_ysfNetwork->writePoll(3U);
m_current = m_startup;
@ -869,10 +871,10 @@ void CYSFGateway::readFCSRoomsFile(const std::string& filename)
void CYSFGateway::processRemoteCommands()
{
unsigned char buffer[200U];
in_addr address;
unsigned int port;
sockaddr_storage addr;
unsigned int addrLen;
int res = m_remoteSocket->read(buffer, 200U, address, port);
int res = m_remoteSocket->read(buffer, 200U, addr, addrLen);
if (res > 0) {
buffer[res] = '\0';
if (::memcmp(buffer + 0U, "LinkYSF", 7U) == 0) {
@ -893,7 +895,7 @@ void CYSFGateway::processRemoteCommands()
LogMessage("Connect by remote command to %5.5s - \"%s\"", reflector->m_id.c_str(), reflector->m_name.c_str());
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_address, reflector->m_port);
m_ysfNetwork->setDestination(reflector->m_name, reflector->m_addr, reflector->m_addrLen);
m_ysfNetwork->writePoll(3U);
m_current = id;

@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2014,2016,2017,2018 by Jonathan Naylor G4KLX
* Copyright (C) 2009-2014,2016,2017,2018,2020 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
@ -30,8 +30,8 @@ const unsigned int BUFFER_LENGTH = 200U;
CYSFNetwork::CYSFNetwork(const std::string& address, unsigned int port, const std::string& callsign, bool debug) :
m_socket(address, port),
m_debug(debug),
m_address(),
m_port(0U),
m_addr(),
m_addrLen(0U),
m_poll(NULL),
m_unlink(NULL),
m_buffer(1000U, "YSF Network Buffer"),
@ -57,8 +57,8 @@ m_linked(false)
CYSFNetwork::CYSFNetwork(unsigned int port, const std::string& callsign, bool debug) :
m_socket(port),
m_debug(debug),
m_address(),
m_port(0U),
m_addr(),
m_addrLen(0U),
m_poll(NULL),
m_unlink(NULL),
m_buffer(1000U, "YSF Network Buffer"),
@ -91,19 +91,18 @@ bool CYSFNetwork::open()
return m_socket.open();
}
void CYSFNetwork::setDestination(const std::string& name, const in_addr& address, unsigned int port)
void CYSFNetwork::setDestination(const std::string& name, const sockaddr_storage& addr, unsigned int addrLen)
{
m_name = name;
m_address = address;
m_port = port;
m_addr = addr;
m_addrLen = addrLen;
m_linked = false;
}
void CYSFNetwork::clearDestination()
{
m_address.s_addr = INADDR_NONE;
m_port = 0U;
m_linked = false;
m_addrLen = 0U;
m_linked = false;
m_pollTimer.stop();
}
@ -112,57 +111,56 @@ void CYSFNetwork::write(const unsigned char* data)
{
assert(data != NULL);
if (m_port == 0U)
if (m_addrLen == 0U)
return;
if (m_debug)
CUtils::dump(1U, "YSF Network Data Sent", data, 155U);
m_socket.write(data, 155U, m_address, m_port);
m_socket.write(data, 155U, m_addr, m_addrLen);
}
void CYSFNetwork::writePoll(unsigned int count)
{
if (m_port == 0U)
if (m_addrLen == 0U)
return;
m_pollTimer.start();
for (unsigned int i = 0U; i < count; i++)
m_socket.write(m_poll, 14U, m_address, m_port);
m_socket.write(m_poll, 14U, m_addr, m_addrLen);
}
void CYSFNetwork::writeUnlink(unsigned int count)
{
m_pollTimer.stop();
if (m_port == 0U)
if (m_addrLen == 0U)
return;
for (unsigned int i = 0U; i < count; i++)
m_socket.write(m_unlink, 14U, m_address, m_port);
m_socket.write(m_unlink, 14U, m_addr, m_addrLen);
m_linked = false;
}
void CYSFNetwork::clock(unsigned int ms)
{
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
m_pollTimer.clock(ms);
if (m_pollTimer.isRunning() && m_pollTimer.hasExpired())
writePoll();
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
unsigned char buffer[BUFFER_LENGTH];
sockaddr_storage addr;
unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return;
if (m_port == 0U)
if (m_addrLen == 0U)
return;
if (address.s_addr != m_address.s_addr || port != m_port)
if (!CUDPSocket::match(addr, m_addr))
return;
if (::memcmp(buffer, "YSFP", 4U) == 0 && !m_linked) {

@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2014,2016,2017,2018 by Jonathan Naylor G4KLX
* Copyright (C) 2009-2014,2016,2017,2018,2020 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
@ -35,7 +35,7 @@ public:
bool open();
void setDestination(const std::string& name, const in_addr& address, unsigned int port);
void setDestination(const std::string& name, const sockaddr_storage& addr, unsigned int addrLen);
void clearDestination();
void write(const unsigned char* data);
@ -52,8 +52,8 @@ public:
private:
CUDPSocket m_socket;
bool m_debug;
in_addr m_address;
unsigned int m_port;
sockaddr_storage m_addr;
unsigned int m_addrLen;
unsigned char* m_poll;
unsigned char* m_unlink;
CRingBuffer<unsigned char> m_buffer;

@ -127,25 +127,24 @@ bool CYSFReflectors::load()
char* p6 = ::strtok(NULL, "\r\n");
if (p1 != NULL && p2 != NULL && p3 != NULL && p4 != NULL && p5 != NULL && p6 != NULL) {
std::string host = std::string(p4);
in_addr address = CUDPSocket::lookup(host);
if (address.s_addr != INADDR_NONE) {
CYSFReflector* refl = new CYSFReflector;
refl->m_id = std::string(p1);
refl->m_name = std::string(p2);
refl->m_desc = std::string(p3);
refl->m_address = address;
refl->m_port = (unsigned int)::atoi(p5);
refl->m_count = std::string(p6);
refl->m_type = YT_YSF;
refl->m_wiresX = (refl->m_name.compare(0, 3, "XLX") == 0);
refl->m_name.resize(16U, ' ');
refl->m_desc.resize(14U, ' ');
m_newReflectors.push_back(refl);
}
std::string host = std::string(p4);
unsigned int port = (unsigned int)::atoi(p5);
CYSFReflector* refl = new CYSFReflector;
CUDPSocket::lookup(host, port, refl->m_addr, refl->m_addrLen);
refl->m_id = std::string(p1);
refl->m_name = std::string(p2);
refl->m_desc = std::string(p3);
refl->m_count = std::string(p6);
refl->m_type = YT_YSF;
refl->m_wiresX = (refl->m_name.compare(0, 3, "XLX") == 0);
refl->m_name.resize(16U, ' ');
refl->m_desc.resize(14U, ' ');
m_newReflectors.push_back(refl);
}
}
@ -161,8 +160,7 @@ bool CYSFReflectors::load()
refl->m_id = "00001";
refl->m_name = "ZZ Parrot ";
refl->m_desc = "Parrot ";
refl->m_address = CUDPSocket::lookup(m_parrotAddress);
refl->m_port = m_parrotPort;
CUDPSocket::lookup(m_parrotAddress, m_parrotPort, refl->m_addr, refl->m_addrLen);
refl->m_count = "000";
refl->m_type = YT_YSF;
refl->m_wiresX = false;
@ -178,8 +176,7 @@ bool CYSFReflectors::load()
refl->m_id = "00002";
refl->m_name = "YSF2DMR ";
refl->m_desc = "Link YSF2DMR ";
refl->m_address = CUDPSocket::lookup(m_YSF2DMRAddress);
refl->m_port = m_YSF2DMRPort;
CUDPSocket::lookup(m_YSF2DMRAddress, m_YSF2DMRPort, refl->m_addr, refl->m_addrLen);
refl->m_count = "000";
refl->m_type = YT_YSF;
refl->m_wiresX = true;
@ -195,8 +192,7 @@ bool CYSFReflectors::load()
refl->m_id = "00003";
refl->m_name = "YSF2NXDN ";
refl->m_desc = "Link YSF2NXDN ";
refl->m_address = CUDPSocket::lookup(m_YSF2NXDNAddress);
refl->m_port = m_YSF2NXDNPort;
CUDPSocket::lookup(m_YSF2NXDNAddress, m_YSF2NXDNPort, refl->m_addr, refl->m_addrLen);
refl->m_count = "000";
refl->m_type = YT_YSF;
refl->m_wiresX = true;
@ -212,8 +208,7 @@ bool CYSFReflectors::load()
refl->m_id = "00004";
refl->m_name = "YSF2P25 ";
refl->m_desc = "Link YSF2P25 ";
refl->m_address = CUDPSocket::lookup(m_YSF2P25Address);
refl->m_port = m_YSF2P25Port;
CUDPSocket::lookup(m_YSF2P25Address, m_YSF2P25Port, refl->m_addr, refl->m_addrLen);
refl->m_count = "000";
refl->m_type = YT_YSF;
refl->m_wiresX = true;
@ -232,13 +227,13 @@ bool CYSFReflectors::load()
std::string desc = it->second;
CYSFReflector* refl = new CYSFReflector;
refl->m_id = text;
refl->m_name = name;
refl->m_desc = desc;
refl->m_port = 0U;
refl->m_count = "000";
refl->m_type = YT_FCS;
refl->m_wiresX = false;
refl->m_id = text;
refl->m_name = name;
refl->m_desc = desc;
refl->m_addrLen = 0U;
refl->m_count = "000";
refl->m_type = YT_FCS;
refl->m_wiresX = false;
refl->m_name.resize(16U, ' ');
refl->m_desc.resize(14U, ' ');

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2019 by Jonathan Naylor G4KLX
* Copyright (C) 2016-2020 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
@ -37,21 +37,21 @@ public:
m_name(),
m_desc(),
m_count("000"),
m_address(),
m_port(0U),
m_addr(),
m_addrLen(0U),
m_type(YT_YSF),
m_wiresX(false)
{
}
std::string m_id;
std::string m_name;
std::string m_desc;
std::string m_count;
in_addr m_address;
unsigned int m_port;
YSF_TYPE m_type;
bool m_wiresX;
std::string m_id;
std::string m_name;
std::string m_desc;
std::string m_count;
sockaddr_storage m_addr;
unsigned int m_addrLen;
YSF_TYPE m_type;
bool m_wiresX;
};
class CYSFReflectors {

@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2014,2016,2018 by Jonathan Naylor G4KLX
* Copyright (C) 2009-2014,2016,2018,2020 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
@ -26,8 +26,8 @@ const unsigned int BUFFER_LENGTH = 200U;
CNetwork::CNetwork(unsigned int port) :
m_socket(port),
m_address(),
m_port(0U)
m_addr(),
m_addrLen(0U)
{
}
@ -44,15 +44,15 @@ bool CNetwork::open()
bool CNetwork::write(const unsigned char* data)
{
if (m_port == 0U)
if (m_addrLen == 0U)
return true;
assert(data != NULL);
return m_socket.write(data, 155U, m_address, m_port);
return m_socket.write(data, 155U, m_addr, m_addrLen);
}
bool CNetwork::writePoll(const in_addr& address, unsigned int port)
bool CNetwork::writePoll(const sockaddr_storage& addr, unsigned int addrLen)
{
unsigned char buffer[20U];
@ -72,20 +72,20 @@ bool CNetwork::writePoll(const in_addr& address, unsigned int port)
buffer[12U] = ' ';
buffer[13U] = ' ';
return m_socket.write(buffer, 14U, address, port);
return m_socket.write(buffer, 14U, addr, addrLen);
}
unsigned int CNetwork::read(unsigned char* data)
{
in_addr address;
unsigned int port;
int length = m_socket.read(data, BUFFER_LENGTH, address, port);
sockaddr_storage addr;
unsigned int addrLen;
int length = m_socket.read(data, BUFFER_LENGTH, addr, addrLen);
if (length <= 0)
return 0U;
// Handle incoming polls
if (::memcmp(data, "YSFP", 4U) == 0) {
writePoll(address, port);
writePoll(addr, addrLen);
return 0U;
}
@ -97,7 +97,7 @@ unsigned int CNetwork::read(unsigned char* data)
if (::memcmp(data, "YSFS", 4U) == 0) {
unsigned char status[50U];
::sprintf((char*)status, "YSFS%05u%-16.16s%-14.14s%03u", 1U, "Parrot", "Parrot", 0U);
m_socket.write(status, 42U, address, port);
m_socket.write(status, 42U, addr, addrLen);
return 0U;
}
@ -105,15 +105,15 @@ unsigned int CNetwork::read(unsigned char* data)
if (::memcmp(data, "YSFD", 4U) != 0)
return 0U;
m_address.s_addr = address.s_addr;
m_port = port;
m_addr = addr;
m_addrLen = addrLen;
return 155U;
}
void CNetwork::end()
{
m_port = 0U;
m_addrLen = 0U;
}
void CNetwork::close()

@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2014,2016,2018 by Jonathan Naylor G4KLX
* Copyright (C) 2009-2014,2016,2018,2020 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
@ -40,11 +40,11 @@ public:
void close();
private:
CUDPSocket m_socket;
in_addr m_address;
unsigned int m_port;
CUDPSocket m_socket;
sockaddr_storage m_addr;
unsigned int m_addrLen;
bool writePoll(const in_addr& address, unsigned int port);
bool writePoll(const sockaddr_storage& addr, unsigned int addrLen);
};
#endif

@ -1,5 +1,5 @@
/*
* Copyright (C) 2006-2016,2018 by Jonathan Naylor G4KLX
* Copyright (C) 2006-2016 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
@ -61,49 +61,95 @@ CUDPSocket::~CUDPSocket()
#endif
}
in_addr CUDPSocket::lookup(const std::string& hostname)
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length)
{
in_addr addr;
#if defined(_WIN32) || defined(_WIN64)
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
}
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints)
{
int err;
std::string portstr = std::to_string(port);
struct addrinfo *res;
/* port is always digits, no needs to lookup service */
hints.ai_flags |= AI_NUMERICSERV;
err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err) {
sockaddr_in *paddr = (sockaddr_in *)&addr;
::memset(paddr, 0, address_length = sizeof(sockaddr_in));
paddr->sin_family = AF_INET;
paddr->sin_port = htons(port);
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
return err;
}
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
addr.s_addr = INADDR_NONE;
return addr;
#else
in_addr_t address = ::inet_addr(hostname.c_str());
if (address != in_addr_t(-1)) {
addr.s_addr = address;
return addr;
}
freeaddrinfo(res);
return 0;
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
bool CUDPSocket::match(const sockaddr_storage &addr1, const sockaddr_storage &addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
switch (addr1.ss_family) {
case AF_INET:
struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in *)&addr1;
in_2 = (struct sockaddr_in *)&addr2;
return ( (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) &&
(in_1->sin_port == in_2->sin_port) );
case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6 *)&addr1;
in6_2 = (struct sockaddr_in6 *)&addr2;
return ( IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) &&
(in6_1->sin6_port == in6_2->sin6_port) );
default:
return false;
}
}
::fprintf(stderr, "Cannot find address for host %s\n", hostname.c_str());
bool CUDPSocket::isnone(const sockaddr_storage &addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
addr.s_addr = INADDR_NONE;
return addr;
#endif
return ( (addr.ss_family == AF_INET) &&
(in->sin_addr.s_addr == htonl(INADDR_NONE)) );
}
bool CUDPSocket::open()
{
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
int err;
sockaddr_storage addr;
unsigned int addrlen;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = af;
/* to determine protocol family, call lookup() first. */
err = lookup(m_address, m_port, addr, addrlen, hints);
if (err) {
::fprintf(stderr, "The local address is invalid - %s\n", m_address.c_str());
return false;
}
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
if (m_fd < 0) {
#if defined(_WIN32) || defined(_WIN64)
::fprintf(stderr, "Cannot create the UDP socket, err: %lu\n", ::GetLastError());
@ -114,24 +160,6 @@ bool CUDPSocket::open()
}
if (m_port > 0U) {
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64)
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#else
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#endif
if (addr.sin_addr.s_addr == INADDR_NONE) {
::fprintf(stderr, "The local address is invalid - %s\n", m_address.c_str());
return false;
}
}
int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
@ -142,7 +170,7 @@ bool CUDPSocket::open()
return false;
}
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
::fprintf(stderr, "Cannot bind the UDP address, err: %lu\n", ::GetLastError());
#else
@ -157,7 +185,7 @@ bool CUDPSocket::open()
return true;
}
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
{
assert(buffer != NULL);
assert(length > 0U);
@ -189,17 +217,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0)
return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in);
int size = sizeof(sockaddr_storage);
#else
socklen_t size = sizeof(sockaddr_in);
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -210,28 +237,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1;
}
address = addr.sin_addr;
port = ntohs(addr.sin_port);
address_length = size;
return len;
}
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
{
assert(buffer != NULL);
assert(length > 0U);
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr = address;
addr.sin_port = htons(port);
#if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)

@ -31,7 +31,8 @@
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
class CUDPSocket {
@ -41,13 +42,17 @@ public:
~CUDPSocket();
bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length);
bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close();
static in_addr lookup(const std::string& hostName);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length, struct addrinfo &hints);
static bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2);
static bool isnone(const sockaddr_storage &addr);
private:
std::string m_address;

@ -1,5 +1,5 @@
/*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
* Copyright (C) 2015,2016,2020 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
@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20161021";
const char* VERSION = "20200903";
#endif

@ -45,8 +45,7 @@ m_logFileLevel(0U),
m_logFilePath(),
m_logFileRoot(),
m_networkPort(0U),
m_networkDebug(false),
m_networkBindAddr()
m_networkDebug(false)
{
}
@ -113,8 +112,6 @@ bool CConf::read()
m_networkPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "Debug") == 0)
m_networkDebug = ::atoi(value) == 1;
else if (::strcmp(key, "BindAddress") == 0)
m_networkBindAddr = value;
}
}
@ -173,7 +170,3 @@ bool CConf::getNetworkDebug() const
return m_networkDebug;
}
std::string CConf::getNetworkBindAddr() const
{
return m_networkBindAddr;
}

@ -47,7 +47,6 @@ public:
// The Network section
unsigned int getNetworkPort() const;
bool getNetworkDebug() const;
std::string getNetworkBindAddr() const;
private:
std::string m_file;
@ -64,7 +63,6 @@ private:
unsigned int m_networkPort;
bool m_networkDebug;
std::string m_networkBindAddr;
};
#endif

@ -19,6 +19,7 @@
#include "YSFDefines.h"
#include "Network.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
@ -44,27 +45,24 @@ CNetwork::~CNetwork()
delete[] m_status;
}
bool CNetwork::open(const std::string& bindaddr)
bool CNetwork::open()
{
if (bindaddr.length() > 0)
::fprintf(stdout, "Opening YSF network connection on address %s\n", bindaddr.c_str());
else
::fprintf(stdout, "Opening YSF network connection on all interfaces\n");
LogInfo("Opening YSF network connection");
return m_socket.open(bindaddr);
return m_socket.open();
}
bool CNetwork::writeData(const unsigned char* data, const in_addr& address, unsigned int port)
bool CNetwork::writeData(const unsigned char* data, const sockaddr_storage& addr, unsigned int addrLen)
{
assert(data != NULL);
if (m_debug)
CUtils::dump(1U, "YSF Network Data Sent", data, 155U);
return m_socket.write(data, 155U, address, port);
return m_socket.write(data, 155U, addr, addrLen);
}
bool CNetwork::writePoll(const in_addr& address, unsigned int port)
bool CNetwork::writePoll(const sockaddr_storage& addr, unsigned int addrLen)
{
unsigned char buffer[20U];
@ -87,21 +85,21 @@ bool CNetwork::writePoll(const in_addr& address, unsigned int port)
if (m_debug)
CUtils::dump(1U, "YSF Network Poll Sent", buffer, 14U);
return m_socket.write(buffer, 14U, address, port);
return m_socket.write(buffer, 14U, addr, addrLen);
}
unsigned int CNetwork::readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port)
unsigned int CNetwork::readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen)
{
assert(data != NULL);
assert(length > 0U);
int len = m_socket.read(data, length, address, port);
int len = m_socket.read(data, length, addr, addrLen);
if (len <= 0)
return 0U;
// Handle incoming status requests
if (::memcmp(data, "YSFS", 4U) == 0) {
m_socket.write(m_status, 42U, address, port);
m_socket.write(m_status, 42U, addr, addrLen);
return 0U;
}
@ -138,5 +136,5 @@ void CNetwork::close()
{
m_socket.close();
::fprintf(stdout, "Closing YSF network connection\n");
LogInfo("Closing YSF network connection");
}

@ -31,25 +31,26 @@ public:
CNetwork(unsigned int port, unsigned int id, const std::string& name, const std::string& description, bool debug);
~CNetwork();
bool open(const std::string& bindaddr);
bool open();
bool writeData(const unsigned char* data, const in_addr& address, unsigned int port);
bool writePoll(const in_addr& address, unsigned int port);
bool writeData(const unsigned char* data, const sockaddr_storage& addr, unsigned int addrLen);
bool writePoll(const sockaddr_storage& addr, unsigned int addrLen);
unsigned int readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port);
unsigned int readData(unsigned char* data, unsigned int length, sockaddr_storage& addr, unsigned int& addrLen);
void close();
void setCount(unsigned int count);
private:
CUDPSocket m_socket;
unsigned int m_id;
std::string m_name;
std::string m_description;
std::string m_callsign;
bool m_debug;
CUDPSocket m_socket;
unsigned int m_id;
std::string m_name;
std::string m_description;
std::string m_callsign;
bool m_debug;
unsigned char* m_status;
};
#endif

@ -1,5 +1,5 @@
/*
* Copyright (C) 2006-2016 by Jonathan Naylor G4KLX
* Copyright (C) 2006-2016,2020 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
@ -62,49 +62,91 @@ CUDPSocket::~CUDPSocket()
#endif
}
in_addr CUDPSocket::lookup(const std::string& hostname)
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
{
in_addr addr;
#if defined(_WIN32) || defined(_WIN64)
unsigned long address = ::inet_addr(hostname.c_str());
if (address != INADDR_NONE && address != INADDR_ANY) {
addr.s_addr = address;
return addr;
}
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
return lookup(hostname, port, addr, address_length, hints);
}
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{
std::string portstr = std::to_string(port);
struct addrinfo *res;
/* port is always digits, no needs to lookup service */
hints.ai_flags |= AI_NUMERICSERV;
int err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err != 0) {
sockaddr_in* paddr = (sockaddr_in*)&addr;
::memset(paddr, 0x00U, address_length = sizeof(sockaddr_in));
paddr->sin_family = AF_INET;
paddr->sin_port = htons(port);
paddr->sin_addr.s_addr = htonl(INADDR_NONE);
LogError("Cannot find address for host %s", hostname.c_str());
return err;
}
LogError("Cannot find address for host %s", hostname.c_str());
::memcpy(&addr, res->ai_addr, address_length = res->ai_addrlen);
addr.s_addr = INADDR_NONE;
return addr;
#else
in_addr_t address = ::inet_addr(hostname.c_str());
if (address != in_addr_t(-1)) {
addr.s_addr = address;
return addr;
}
freeaddrinfo(res);
return 0;
}
struct hostent* hp = ::gethostbyname(hostname.c_str());
if (hp != NULL) {
::memcpy(&addr, hp->h_addr_list[0], sizeof(struct in_addr));
return addr;
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
{
if (addr1.ss_family != addr2.ss_family)
return false;
switch (addr1.ss_family) {
case AF_INET:
struct sockaddr_in *in_1, *in_2;
in_1 = (struct sockaddr_in*)&addr1;
in_2 = (struct sockaddr_in*)&addr2;
return ((in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port));
case AF_INET6:
struct sockaddr_in6 *in6_1, *in6_2;
in6_1 = (struct sockaddr_in6*)&addr1;
in6_2 = (struct sockaddr_in6*)&addr2;
return (IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port));
default:
return false;
}
}
LogError("Cannot find address for host %s", hostname.c_str());
bool CUDPSocket::isnone(const sockaddr_storage& addr)
{
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
addr.s_addr = INADDR_NONE;
return addr;
#endif
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
}
bool CUDPSocket::open(const std::string& bindaddr)
bool CUDPSocket::open()
{
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
return open(AF_UNSPEC);
}
bool CUDPSocket::open(const unsigned int af)
{
sockaddr_storage addr;
unsigned int addrlen;
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = af;
/* to determine protocol family, call lookup() first. */
int err = lookup(m_address, m_port, addr, addrlen, hints);
if (err != 0) {
LogError("The local address is invalid - %s", m_address.c_str());
return false;
}
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
if (m_fd < 0) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
@ -115,33 +157,6 @@ bool CUDPSocket::open(const std::string& bindaddr)
}
if (m_port > 0U) {
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
if ( bindaddr.length() > 0){
int validaddr = inet_pton(AF_INET, bindaddr.c_str(), &(addr.sin_addr));
if (validaddr != 1){
LogError("The BindAddress in the .ini is invalid - %s", bindaddr.c_str());
return false;
}
} else {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64)
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#else
addr.sin_addr.s_addr = ::inet_addr(m_address.c_str());
#endif
if (addr.sin_addr.s_addr == INADDR_NONE) {
LogError("The local address is invalid - %s", m_address.c_str());
return false;
}
}
int reuse = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
@ -152,7 +167,7 @@ bool CUDPSocket::open(const std::string& bindaddr)
return false;
}
if (::bind(m_fd, (sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
#else
@ -160,12 +175,14 @@ bool CUDPSocket::open(const std::string& bindaddr)
#endif
return false;
}
LogInfo("Opening UDP port on %u", m_port);
}
return true;
}
int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port)
int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length)
{
assert(buffer != NULL);
assert(length > 0U);
@ -197,17 +214,16 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
if (ret == 0)
return 0;
sockaddr_in addr;
#if defined(_WIN32) || defined(_WIN64)
int size = sizeof(sockaddr_in);
int size = sizeof(sockaddr_storage);
#else
socklen_t size = sizeof(sockaddr_in);
socklen_t size = sizeof(sockaddr_storage);
#endif
#if defined(_WIN32) || defined(_WIN64)
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#else
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&addr, &size);
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
#endif
if (len <= 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -218,28 +234,19 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, in_addr& addres
return -1;
}
address = addr.sin_addr;
port = ntohs(addr.sin_port);
address_length = size;
return len;
}
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port)
bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length)
{
assert(buffer != NULL);
assert(length > 0U);
sockaddr_in addr;
::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr = address;
addr.sin_port = htons(port);
#if defined(_WIN32) || defined(_WIN64)
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#else
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&addr, sizeof(sockaddr_in));
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
#endif
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)

@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2011,2013,2015,2016 by Jonathan Naylor G4KLX
* Copyright (C) 2009-2011,2013,2015,2016,2020 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
@ -31,7 +31,8 @@
#include <arpa/inet.h>
#include <errno.h>
#else
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
class CUDPSocket {
@ -40,14 +41,18 @@ public:
CUDPSocket(unsigned int port = 0U);
~CUDPSocket();
bool open(const std::string& bindaddr);
bool open();
bool open(const unsigned int af);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);
int read(unsigned char* buffer, unsigned int length, sockaddr_storage& address, unsigned int &address_length);
bool write(const unsigned char* buffer, unsigned int length, const sockaddr_storage& address, unsigned int address_length);
void close();
static in_addr lookup(const std::string& hostName);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2);
static bool isnone(const sockaddr_storage& addr);
private:
std::string m_address;

@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20200429";
const char* VERSION = "20200903";
#endif

@ -164,7 +164,7 @@ void CYSFReflector::run()
CNetwork network(m_conf.getNetworkPort(), m_conf.getId(), m_conf.getName(), m_conf.getDescription(), m_conf.getNetworkDebug());
ret = network.open(m_conf.getNetworkBindAddr());
ret = network.open();
if (!ret) {
::LogFinalise();
return;
@ -191,29 +191,29 @@ void CYSFReflector::run()
for (;;) {
unsigned char buffer[200U];
in_addr address;
unsigned int port;
sockaddr_storage addr;
unsigned int addrLen;
unsigned int len = network.readData(buffer, 200U, address, port);
unsigned int len = network.readData(buffer, 200U, addr, addrLen);
if (len > 0U) {
CYSFRepeater* rpt = findRepeater(address, port);
CYSFRepeater* rpt = findRepeater(addr);
if (::memcmp(buffer, "YSFP", 4U) == 0) {
if (rpt == NULL) {
rpt = new CYSFRepeater;
rpt->m_callsign = std::string((char*)(buffer + 4U), 10U);
rpt->m_address = address;
rpt->m_port = port;
rpt->m_addr = addr;
rpt->m_addrLen = addrLen;
m_repeaters.push_back(rpt);
network.setCount(m_repeaters.size());
LogMessage("Adding %s (%s:%u)", rpt->m_callsign.c_str(), ::inet_ntoa(address), port);
LogMessage("Adding %s", rpt->m_callsign.c_str());
}
rpt->m_timer.start();
network.writePoll(address, port);
network.writePoll(addr, addrLen);
} else if (::memcmp(buffer + 0U, "YSFU", 4U) == 0 && rpt != NULL) {
LogMessage("Removing %s (%s:%u) unlinked", rpt->m_callsign.c_str(), ::inet_ntoa(address), port);
LogMessage("Removing %s unlinked", rpt->m_callsign.c_str());
for (std::vector<CYSFRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
CYSFRepeater* itRpt = *it;
if (itRpt->m_address.s_addr == rpt->m_address.s_addr && itRpt->m_port == rpt->m_port) {
if (CUDPSocket::match(itRpt->m_addr, rpt->m_addr)) {
m_repeaters.erase(it);
delete itRpt;
break;
@ -257,8 +257,8 @@ void CYSFReflector::run()
watchdogTimer.start();
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if ((*it)->m_address.s_addr != address.s_addr || (*it)->m_port != port)
network.writeData(buffer, (*it)->m_address, (*it)->m_port);
if (!CUDPSocket::match((*it)->m_addr, addr))
network.writeData(buffer, (*it)->m_addr, (*it)->m_addrLen);
}
if ((buffer[34U] & 0x01U) == 0x01U) {
@ -274,7 +274,7 @@ void CYSFReflector::run()
pollTimer.clock(ms);
if (pollTimer.hasExpired()) {
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it)
network.writePoll((*it)->m_address, (*it)->m_port);
network.writePoll((*it)->m_addr, (*it)->m_addrLen);
pollTimer.start();
}
@ -285,7 +285,7 @@ void CYSFReflector::run()
for (std::vector<CYSFRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
CYSFRepeater* itRpt = *it;
if (itRpt->m_timer.hasExpired()) {
LogMessage("Removing %s (%s:%u) disappeared", itRpt->m_callsign.c_str(), ::inet_ntoa(itRpt->m_address), itRpt->m_port);
LogMessage("Removing %s disappeared", itRpt->m_callsign.c_str());
m_repeaters.erase(it);
delete itRpt;
network.setCount(m_repeaters.size());
@ -314,10 +314,10 @@ void CYSFReflector::run()
::LogFinalise();
}
CYSFRepeater* CYSFReflector::findRepeater(const in_addr& address, unsigned int port) const
CYSFRepeater* CYSFReflector::findRepeater(const sockaddr_storage& addr) const
{
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if (address.s_addr == (*it)->m_address.s_addr && (*it)->m_port == port)
if (CUDPSocket::match(addr, (*it)->m_addr))
return *it;
}
@ -335,10 +335,8 @@ void CYSFReflector::dumpRepeaters() const
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
std::string callsign = (*it)->m_callsign;
in_addr address = (*it)->m_address;
unsigned int port = (*it)->m_port;
unsigned int timer = (*it)->m_timer.getTimer();
unsigned int timeout = (*it)->m_timer.getTimeout();
LogMessage(" %s: %s:%u %u/%u", callsign.c_str(), ::inet_ntoa(address), port, timer, timeout);
LogMessage(" %s: %u/%u", callsign.c_str(), timer, timeout);
}
}

@ -42,16 +42,16 @@ class CYSFRepeater {
public:
CYSFRepeater() :
m_callsign(),
m_address(),
m_port(0U),
m_addr(),
m_addrLen(0U),
m_timer(1000U, 60U)
{
}
std::string m_callsign;
in_addr m_address;
unsigned int m_port;
CTimer m_timer;
std::string m_callsign;
sockaddr_storage m_addr;
unsigned int m_addrLen;
CTimer m_timer;
};
class CYSFReflector
@ -66,7 +66,7 @@ private:
CConf m_conf;
std::vector<CYSFRepeater*> m_repeaters;
CYSFRepeater* findRepeater(const in_addr& address, unsigned int port) const;
CYSFRepeater* findRepeater(const sockaddr_storage& addr) const;
void dumpRepeaters() const;
};

@ -18,6 +18,4 @@ FileRoot=YSFReflector
[Network]
Port=42000
Debug=0
# By default, listen on all IPs. To bind to
# a specific IP uncomment and set a local address
#BindAddress=192.0.2.1

Loading…
Cancel
Save