IPv6 and FCS changes.
This commit is contained in:
parent
bdf5195362
commit
241c76e606
13 changed files with 327 additions and 190 deletions
|
@ -37,7 +37,7 @@ m_height(0),
|
|||
m_desc(),
|
||||
m_suffix(suffix),
|
||||
m_aprsAddr(),
|
||||
m_aprsAddrLen(),
|
||||
m_aprsAddrLen(0U),
|
||||
m_aprsSocket()
|
||||
#if defined(USE_GPSD)
|
||||
,m_gpsdEnabled(false),
|
||||
|
@ -55,7 +55,8 @@ m_gpsdData()
|
|||
m_callsign.append(rptSuffix.substr(0U, 1U));
|
||||
}
|
||||
|
||||
CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen);
|
||||
if (CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen) != 0)
|
||||
m_aprsAddrLen = 0U;
|
||||
}
|
||||
|
||||
CAPRSWriter::~CAPRSWriter()
|
||||
|
@ -90,6 +91,11 @@ void CAPRSWriter::setGPSDLocation(const std::string& address, const std::string&
|
|||
|
||||
bool CAPRSWriter::open()
|
||||
{
|
||||
if (m_aprsAddrLen == 0U) {
|
||||
LogError("Unable to lookup the adress of the APRS-IS server");
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(USE_GPSD)
|
||||
if (m_gpsdEnabled) {
|
||||
int ret = ::gps_open(m_gpsdAddress.c_str(), m_gpsdPort.c_str(), &m_gpsdData);
|
||||
|
@ -103,7 +109,7 @@ bool CAPRSWriter::open()
|
|||
LogMessage("Connected to GPSD");
|
||||
}
|
||||
#endif
|
||||
bool ret = m_aprsSocket.open();
|
||||
bool ret = m_aprsSocket.open(m_aprsAddr);
|
||||
if (!ret)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -128,13 +128,33 @@ bool CConf::read()
|
|||
section = SECTION_NONE;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
char* key = ::strtok(buffer, " \t=\r\n");
|
||||
if (key == NULL)
|
||||
continue;
|
||||
char* key = ::strtok(buffer, " \t=\r\n");
|
||||
if (key == NULL)
|
||||
continue;
|
||||
|
||||
char* value = ::strtok(NULL, "\r\n");
|
||||
if (value == NULL)
|
||||
continue;
|
||||
|
||||
// Remove quotes from the value
|
||||
size_t len = ::strlen(value);
|
||||
if (len > 1U && *value == '"' && value[len - 1U] == '"') {
|
||||
value[len - 1U] = '\0';
|
||||
value++;
|
||||
} else {
|
||||
char *p;
|
||||
|
||||
// if value is not quoted, remove after # (to make comment)
|
||||
if ((p = strchr(value, '#')) != NULL)
|
||||
*p = '\0';
|
||||
|
||||
// remove trailing tab/space
|
||||
for (p = value + strlen(value) - 1U; p >= value && (*p == '\t' || *p == ' '); p--)
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
char* value = ::strtok(NULL, "\r\n");
|
||||
if (section == SECTION_GENERAL) {
|
||||
if (::strcmp(key, "Callsign") == 0) {
|
||||
// Convert the callsign to upper case
|
||||
|
@ -241,6 +261,8 @@ bool CConf::read()
|
|||
dgIdData->m_netHangTime = (unsigned int)::atoi(value);
|
||||
else if (::strcmp(key, "Static") == 0)
|
||||
dgIdData->m_static = ::atoi(value) == 1;
|
||||
else if (::strcmp(key, "Options") == 0)
|
||||
dgIdData->m_options = value;
|
||||
else if (::strcmp(key, "Address") == 0)
|
||||
dgIdData->m_address = value;
|
||||
else if (::strcmp(key, "Name") == 0)
|
||||
|
|
|
@ -31,6 +31,7 @@ struct DGIdData {
|
|||
unsigned int m_dgId;
|
||||
std::string m_type;
|
||||
bool m_static;
|
||||
std::string m_options;
|
||||
std::string m_name;
|
||||
std::string m_address;
|
||||
unsigned int m_port;
|
||||
|
|
|
@ -182,7 +182,10 @@ int CDGIdGateway::run()
|
|||
|
||||
sockaddr_storage rptAddr;
|
||||
unsigned int rptAddrLen;
|
||||
CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen);
|
||||
if (CUDPSocket::lookup(m_conf.getRptAddress(), m_conf.getRptPort(), rptAddr, rptAddrLen) != 0) {
|
||||
LogError("Unable to resolve the address of the host");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool debug = m_conf.getDebug();
|
||||
std::string myAddress = m_conf.getMyAddress();
|
||||
|
@ -229,8 +232,9 @@ int CDGIdGateway::run()
|
|||
unsigned int rxFrequency = m_conf.getRxFrequency();
|
||||
std::string locator = calculateLocator();
|
||||
unsigned int id = m_conf.getId();
|
||||
std::string options = (*it)->m_options;
|
||||
|
||||
dgIdNetwork[dgid] = new CFCSNetwork(name, local, m_callsign, rxFrequency, txFrequency, locator, id, debug);
|
||||
dgIdNetwork[dgid] = new CFCSNetwork(name, local, m_callsign, rxFrequency, txFrequency, locator, id, options, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
|
@ -254,10 +258,17 @@ int CDGIdGateway::run()
|
|||
std::string name = (*it)->m_name;
|
||||
|
||||
for (std::vector<IMRSDestination*>::const_iterator it = destinations.begin(); it != destinations.end(); ++it) {
|
||||
IMRSDest* dest = new IMRSDest;
|
||||
CUDPSocket::lookup((*it)->m_address, IMRS_PORT, dest->m_addr, dest->m_addrLen);
|
||||
dest->m_dgId = (*it)->m_dgId;
|
||||
dests.push_back(dest);
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrLen;
|
||||
if (CUDPSocket::lookup((*it)->m_address, IMRS_PORT, addr, addrLen) == 0) {
|
||||
IMRSDest* dest = new IMRSDest;
|
||||
dest->m_dgId = (*it)->m_dgId;
|
||||
dest->m_addr = addr;
|
||||
dest->m_addrLen = addrLen;
|
||||
dests.push_back(dest);
|
||||
} else {
|
||||
LogWarning("Unable to resolve the address for %s", (*it)->m_address.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
imrs->addDGId(dgid, name, dests, debug);
|
||||
|
@ -273,49 +284,57 @@ int CDGIdGateway::run()
|
|||
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrLen;
|
||||
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
|
||||
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "PARROT", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "PARROT", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2 | DT_VOICE_FR_MODE | DT_DATA_FR_MODE;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
} else {
|
||||
LogWarning("Unable to resolve the address for the YSF Parrot");
|
||||
}
|
||||
} else if (type == "YSF2DMR") {
|
||||
unsigned int local = (*it)->m_local;
|
||||
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrLen;
|
||||
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
|
||||
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2DMR", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2DMR", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
} else {
|
||||
LogWarning("Unable to resolve the address for YSF2DMR");
|
||||
}
|
||||
} else if (type == "YSF2NXDN") {
|
||||
unsigned int local = (*it)->m_local;
|
||||
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrLen;
|
||||
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
|
||||
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2NXDN", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2NXDN", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VD_MODE1 | DT_VD_MODE2;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
} else {
|
||||
LogWarning("Unable to resolve the address for YSF2NXDN");
|
||||
}
|
||||
} else if (type == "YSF2P25") {
|
||||
unsigned int local = (*it)->m_local;
|
||||
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrLen;
|
||||
CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen);
|
||||
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2P25", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VOICE_FR_MODE;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
if (CUDPSocket::lookup((*it)->m_address, (*it)->m_port, addr, addrLen) == 0) {
|
||||
dgIdNetwork[dgid] = new CYSFNetwork(local, "YSF2P25", addr, addrLen, m_callsign, debug);
|
||||
dgIdNetwork[dgid]->m_modes = DT_VOICE_FR_MODE;
|
||||
dgIdNetwork[dgid]->m_static = statc;
|
||||
dgIdNetwork[dgid]->m_rfHangTime = rfHangTime;
|
||||
dgIdNetwork[dgid]->m_netHangTime = netHangTime;
|
||||
} else {
|
||||
LogWarning("Unable to resolve the address for YSF2P25");
|
||||
}
|
||||
}
|
||||
|
||||
if (dgIdNetwork[dgid] != NULL && dgIdNetwork[dgid] != imrs) {
|
||||
|
|
|
@ -118,6 +118,7 @@ Type=FCS
|
|||
Static=0
|
||||
Name=FCS00101
|
||||
Local=42025
|
||||
# Options=20;21;
|
||||
#RFHangTime=120
|
||||
#NetHangTime=60
|
||||
Debug=0
|
||||
|
|
|
@ -29,12 +29,14 @@ const char* FCS_VERSION = "MMDVM";
|
|||
|
||||
const unsigned int BUFFER_LENGTH = 200U;
|
||||
|
||||
CFCSNetwork::CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, bool debug) :
|
||||
CFCSNetwork::CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, const std::string& options, bool debug) :
|
||||
m_socket(port),
|
||||
m_debug(debug),
|
||||
m_addr(),
|
||||
m_addrLen(0U),
|
||||
m_ping(NULL),
|
||||
m_options(NULL),
|
||||
m_opt(options),
|
||||
m_info(NULL),
|
||||
m_reflector(reflector),
|
||||
m_print(),
|
||||
|
@ -56,12 +58,23 @@ m_state(FCS_UNLINKED)
|
|||
::memcpy(m_ping + 10U, reflector.c_str(), 8U);
|
||||
|
||||
m_print = reflector.substr(0U, 6U) + "-" + reflector.substr(6U);
|
||||
|
||||
m_options = new unsigned char[50U];
|
||||
::memcpy(m_options + 0U, "FCSO", 4U);
|
||||
::memset(m_options + 4U, ' ', 46U);
|
||||
::memcpy(m_options + 4U, callsign.c_str(), callsign.size());
|
||||
|
||||
char url[50U];
|
||||
::sprintf(url, "%s.xreflector.net", reflector.c_str());
|
||||
if (CUDPSocket::lookup(std::string(url), FCS_PORT, m_addr, m_addrLen) != 0)
|
||||
m_addrLen = 0U;
|
||||
}
|
||||
|
||||
CFCSNetwork::~CFCSNetwork()
|
||||
{
|
||||
delete[] m_info;
|
||||
delete[] m_ping;
|
||||
delete[] m_options;
|
||||
}
|
||||
|
||||
std::string CFCSNetwork::getDesc(unsigned int dgId)
|
||||
|
@ -71,54 +84,14 @@ std::string CFCSNetwork::getDesc(unsigned int dgId)
|
|||
|
||||
bool CFCSNetwork::open()
|
||||
{
|
||||
LogMessage("Resolving FCS00x addresses");
|
||||
|
||||
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;
|
||||
if (m_addrLen == 0U) {
|
||||
LogError("Unable to resolve the address of %s", m_reflector.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
LogMessage("Opening FCS network connection");
|
||||
|
||||
return m_socket.open();
|
||||
return m_socket.open(m_addr);
|
||||
}
|
||||
|
||||
void CFCSNetwork::write(unsigned int dgid, const unsigned char* data)
|
||||
|
@ -131,6 +104,7 @@ void CFCSNetwork::write(unsigned int dgid, const unsigned char* data)
|
|||
unsigned char buffer[130U];
|
||||
::memset(buffer + 0U, ' ', 130U);
|
||||
::memcpy(buffer + 0U, data + 35U, 120U);
|
||||
::memcpy(buffer + 120U, data + 34U, 1U);
|
||||
::memcpy(buffer + 121U, m_reflector.c_str(), 8U);
|
||||
|
||||
if (m_debug)
|
||||
|
@ -144,16 +118,6 @@ void CFCSNetwork::link()
|
|||
if (m_state == FCS_LINKING || m_state == FCS_LINKED)
|
||||
return;
|
||||
|
||||
std::string name = m_reflector.substr(0U, 6U);
|
||||
if (m_addresses.count(name) == 0U) {
|
||||
LogError("Unknown FCS reflector - %s", name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
std::pair<sockaddr_storage, unsigned int> entry = m_addresses[name];
|
||||
m_addr = entry.first;
|
||||
m_addrLen = entry.second;
|
||||
|
||||
m_state = FCS_LINKING;
|
||||
|
||||
m_pingTimer.start();
|
||||
|
@ -205,12 +169,14 @@ void CFCSNetwork::clock(unsigned int ms)
|
|||
LogMessage("Linked to %s", m_print.c_str());
|
||||
m_state = FCS_LINKED;
|
||||
writeInfo();
|
||||
writeOptions(m_print);
|
||||
}
|
||||
|
||||
if (length == 10 && m_state == FCS_LINKING) {
|
||||
LogMessage("Linked to %s", m_print.c_str());
|
||||
m_state = FCS_LINKED;
|
||||
writeInfo();
|
||||
writeOptions(m_print);
|
||||
}
|
||||
|
||||
if (length == 7 || length == 10 || length == 130) {
|
||||
|
@ -286,3 +252,22 @@ void CFCSNetwork::writePing()
|
|||
|
||||
m_socket.write(m_ping, 25U, m_addr, m_addrLen);
|
||||
}
|
||||
|
||||
void CFCSNetwork::writeOptions(const std::string& reflector)
|
||||
{
|
||||
if (m_state != FCS_LINKED)
|
||||
return;
|
||||
|
||||
if (m_opt.size() < 1)
|
||||
return;
|
||||
|
||||
::memset(m_options + 14U, 0x20U, 36U);
|
||||
::memcpy(m_options + 4U, (reflector.substr(0, 6) + reflector.substr(7, 2)).c_str(), 8U);
|
||||
::memcpy(m_options + 12U, m_opt.c_str(), m_opt.size());
|
||||
|
||||
if (m_debug)
|
||||
CUtils::dump(1U, "FCS Network Options Sent", m_options, 50U);
|
||||
|
||||
m_socket.write(m_options, 50U, m_addr, m_addrLen);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
enum FCS_STATE {
|
||||
FCS_UNLINKED,
|
||||
|
@ -37,7 +36,7 @@ enum FCS_STATE {
|
|||
|
||||
class CFCSNetwork : public CDGIdNetwork {
|
||||
public:
|
||||
CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, bool debug);
|
||||
CFCSNetwork(const std::string& reflector, unsigned int port, const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, const std::string& locator, unsigned int id, const std::string& options, bool debug);
|
||||
virtual ~CFCSNetwork();
|
||||
|
||||
virtual std::string getDesc(unsigned int dgId);
|
||||
|
@ -62,16 +61,18 @@ private:
|
|||
sockaddr_storage m_addr;
|
||||
unsigned int m_addrLen;
|
||||
unsigned char* m_ping;
|
||||
unsigned char* m_options;
|
||||
std::string m_opt;
|
||||
unsigned char* m_info;
|
||||
std::string m_reflector;
|
||||
std::string m_print;
|
||||
CRingBuffer<unsigned char> m_buffer;
|
||||
std::map<std::string, std::pair<sockaddr_storage, unsigned int>> m_addresses;
|
||||
unsigned char m_n;
|
||||
CTimer m_pingTimer;
|
||||
CTimer m_resetTimer;
|
||||
FCS_STATE m_state;
|
||||
|
||||
void writeOptions(const std::string& reflector);
|
||||
void writeInfo();
|
||||
void writePing();
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
CC = gcc
|
||||
CXX = g++
|
||||
CC = cc
|
||||
CXX = c++
|
||||
|
||||
# Use the following CFLAGS and LIBS if you don't want to use gpsd.
|
||||
CFLAGS = -g -O3 -Wall -std=c++0x -pthread
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
*/
|
||||
|
||||
#include "UDPSocket.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
@ -26,26 +25,36 @@
|
|||
#include <cstring>
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_LOG_H)
|
||||
#include "Log.h"
|
||||
#else
|
||||
#define LogError(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
|
||||
#define LogInfo(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
|
||||
m_address(address),
|
||||
m_port(port),
|
||||
m_fd(-1)
|
||||
m_address_save(address),
|
||||
m_port_save(port),
|
||||
m_counter(0U)
|
||||
{
|
||||
assert(!address.empty());
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
WSAData data;
|
||||
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
||||
if (wsaRet != 0)
|
||||
LogError("Error from WSAStartup");
|
||||
#endif
|
||||
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
|
||||
m_address[i] = "";
|
||||
m_port[i] = 0U;
|
||||
m_af[i] = 0U;
|
||||
m_fd[i] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
CUDPSocket::CUDPSocket(unsigned int port) :
|
||||
m_address(),
|
||||
m_port(port),
|
||||
m_fd(-1)
|
||||
m_address_save(),
|
||||
m_port_save(port),
|
||||
m_counter(0U)
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
WSAData data;
|
||||
|
@ -53,6 +62,12 @@ m_fd(-1)
|
|||
if (wsaRet != 0)
|
||||
LogError("Error from WSAStartup");
|
||||
#endif
|
||||
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
|
||||
m_address[i] = "";
|
||||
m_port[i] = 0U;
|
||||
m_af[i] = 0U;
|
||||
m_fd[i] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
CUDPSocket::~CUDPSocket()
|
||||
|
@ -96,58 +111,82 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
|
|||
return 0;
|
||||
}
|
||||
|
||||
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2)
|
||||
bool CUDPSocket::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
|
||||
{
|
||||
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:
|
||||
if (type == IMT_ADDRESS_AND_PORT) {
|
||||
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;
|
||||
}
|
||||
} else if (type == IMT_ADDRESS_ONLY) {
|
||||
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;
|
||||
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);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CUDPSocket::isnone(const sockaddr_storage& addr)
|
||||
bool CUDPSocket::isNone(const sockaddr_storage& addr)
|
||||
{
|
||||
struct sockaddr_in *in = (struct sockaddr_in *)&addr;
|
||||
|
||||
return ((addr.ss_family == AF_INET) && (in->sin_addr.s_addr == htonl(INADDR_NONE)));
|
||||
}
|
||||
|
||||
bool CUDPSocket::open()
|
||||
bool CUDPSocket::open(const sockaddr_storage& address)
|
||||
{
|
||||
return open(AF_UNSPEC);
|
||||
return open(address.ss_family);
|
||||
}
|
||||
|
||||
bool CUDPSocket::open(const unsigned int af)
|
||||
bool CUDPSocket::open(unsigned int af)
|
||||
{
|
||||
return open(0, af, m_address_save, m_port_save);
|
||||
}
|
||||
|
||||
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int port)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrlen;
|
||||
struct addrinfo hints;
|
||||
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
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);
|
||||
int err = lookup(address, port, addr, addrlen, hints);
|
||||
if (err != 0) {
|
||||
LogError("The local address is invalid - %s", m_address.c_str());
|
||||
LogError("The local address is invalid - %s", address.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
||||
if (m_fd < 0) {
|
||||
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
|
||||
#else
|
||||
|
@ -156,9 +195,14 @@ bool CUDPSocket::open(const unsigned int af)
|
|||
return false;
|
||||
}
|
||||
|
||||
if (m_port > 0U) {
|
||||
m_address[index] = address;
|
||||
m_port[index] = port;
|
||||
m_af[index] = addr.ss_family;
|
||||
m_fd[index] = fd;
|
||||
|
||||
if (port > 0U) {
|
||||
int reuse = 1;
|
||||
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
||||
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
|
||||
#else
|
||||
|
@ -167,7 +211,7 @@ bool CUDPSocket::open(const unsigned int af)
|
|||
return false;
|
||||
}
|
||||
|
||||
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
|
||||
if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
|
||||
#else
|
||||
|
@ -176,7 +220,7 @@ bool CUDPSocket::open(const unsigned int af)
|
|||
return false;
|
||||
}
|
||||
|
||||
LogInfo("Opening UDP port on %u", m_port);
|
||||
LogInfo("Opening UDP port on %u", port);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -188,30 +232,43 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||
assert(length > 0U);
|
||||
|
||||
// Check that the readfrom() won't block
|
||||
fd_set readFds;
|
||||
FD_ZERO(&readFds);
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
FD_SET((unsigned int)m_fd, &readFds);
|
||||
#else
|
||||
FD_SET(m_fd, &readFds);
|
||||
#endif
|
||||
int i, n;
|
||||
struct pollfd pfd[UDP_SOCKET_MAX];
|
||||
for (i = n = 0; i < UDP_SOCKET_MAX; i++) {
|
||||
if (m_fd[i] >= 0) {
|
||||
pfd[n].fd = m_fd[i];
|
||||
pfd[n].events = POLLIN;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
// no socket descriptor to receive
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
// Return immediately
|
||||
timeval tv;
|
||||
tv.tv_sec = 0L;
|
||||
tv.tv_usec = 0L;
|
||||
|
||||
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int ret = WSAPoll(pfd, n, 0);
|
||||
#else
|
||||
int ret = ::poll(pfd, n, 0);
|
||||
#endif
|
||||
if (ret < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Error returned from UDP select, err: %lu", ::GetLastError());
|
||||
LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
|
||||
#else
|
||||
LogError("Error returned from UDP select, err: %d", errno);
|
||||
LogError("Error returned from UDP poll, err: %d", errno);
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ret == 0)
|
||||
int index;
|
||||
for (i = 0; i < n; i++) {
|
||||
// round robin
|
||||
index = (i + m_counter) % n;
|
||||
if (pfd[index].revents & POLLIN)
|
||||
break;
|
||||
}
|
||||
if (i == n)
|
||||
return 0;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
@ -221,9 +278,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||
int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||
#else
|
||||
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||
ssize_t len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
||||
#endif
|
||||
if (len <= 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
@ -234,6 +291,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|||
return -1;
|
||||
}
|
||||
|
||||
m_counter++;
|
||||
address_length = size;
|
||||
return len;
|
||||
}
|
||||
|
@ -243,36 +301,52 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
|
|||
assert(buffer != NULL);
|
||||
assert(length > 0U);
|
||||
|
||||
bool result = false;
|
||||
|
||||
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
|
||||
if (m_fd[i] < 0 || m_af[i] != address.ss_family)
|
||||
continue;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||
int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||
#else
|
||||
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||
ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
||||
#endif
|
||||
if (ret < 0) {
|
||||
|
||||
if (ret < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Error returned from sendto, err: %lu", ::GetLastError());
|
||||
LogError("Error returned from sendto, err: %lu", ::GetLastError());
|
||||
#else
|
||||
LogError("Error returned from sendto, err: %d", errno);
|
||||
LogError("Error returned from sendto, err: %d", errno);
|
||||
#endif
|
||||
return false;
|
||||
} else {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (ret == int(length))
|
||||
result = true;
|
||||
#else
|
||||
if (ret == ssize_t(length))
|
||||
result = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (ret != int(length))
|
||||
return false;
|
||||
#else
|
||||
if (ret != ssize_t(length))
|
||||
return false;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
void CUDPSocket::close()
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
::closesocket(m_fd);
|
||||
#else
|
||||
::close(m_fd);
|
||||
#endif
|
||||
for (int i = 0; i < UDP_SOCKET_MAX; i++)
|
||||
close(m_fd[i]);
|
||||
}
|
||||
|
||||
void CUDPSocket::close(const unsigned int index)
|
||||
{
|
||||
if (m_fd[index] >= 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
::closesocket(m_fd[index]);
|
||||
#else
|
||||
::close(m_fd[index]);
|
||||
#endif
|
||||
m_fd[index] = -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
@ -35,29 +36,45 @@
|
|||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#if !defined(UDP_SOCKET_MAX)
|
||||
#define UDP_SOCKET_MAX 1
|
||||
#endif
|
||||
|
||||
enum IPMATCHTYPE {
|
||||
IMT_ADDRESS_AND_PORT,
|
||||
IMT_ADDRESS_ONLY
|
||||
};
|
||||
|
||||
class CUDPSocket {
|
||||
public:
|
||||
CUDPSocket(const std::string& address, unsigned int port = 0U);
|
||||
CUDPSocket(unsigned int port = 0U);
|
||||
~CUDPSocket();
|
||||
|
||||
bool open();
|
||||
bool open(const unsigned int af);
|
||||
bool open(unsigned int af = AF_UNSPEC);
|
||||
bool open(const sockaddr_storage& address);
|
||||
bool open(const unsigned int index, const unsigned int af, const std::string& address, const 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();
|
||||
void close(const unsigned int index);
|
||||
|
||||
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);
|
||||
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT);
|
||||
|
||||
static bool isNone(const sockaddr_storage& addr);
|
||||
|
||||
private:
|
||||
std::string m_address;
|
||||
unsigned short m_port;
|
||||
int m_fd;
|
||||
std::string m_address_save;
|
||||
unsigned short m_port_save;
|
||||
std::string m_address[UDP_SOCKET_MAX];
|
||||
unsigned short m_port[UDP_SOCKET_MAX];
|
||||
unsigned int m_af[UDP_SOCKET_MAX];
|
||||
int m_fd[UDP_SOCKET_MAX];
|
||||
unsigned int m_counter;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -19,6 +19,6 @@
|
|||
#if !defined(VERSION_H)
|
||||
#define VERSION_H
|
||||
|
||||
const char* VERSION = "20200903";
|
||||
const char* VERSION = "20200906";
|
||||
|
||||
#endif
|
||||
|
|
|
@ -93,9 +93,14 @@ std::string CYSFNetwork::getDesc(unsigned int dgId)
|
|||
|
||||
bool CYSFNetwork::open()
|
||||
{
|
||||
if (m_addrLen == 0U) {
|
||||
LogError("Unable to resolve the address of the YSF network");
|
||||
return false;
|
||||
}
|
||||
|
||||
LogMessage("Opening YSF network connection");
|
||||
|
||||
return m_socket.open();
|
||||
return m_socket.open(m_addr);
|
||||
}
|
||||
|
||||
void CYSFNetwork::write(unsigned int dgid, const unsigned char* data)
|
||||
|
|
|
@ -60,12 +60,18 @@ bool CYSFReflectors::load()
|
|||
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);
|
||||
|
||||
m_reflectors.push_back(refl);
|
||||
sockaddr_storage addr;
|
||||
unsigned int addrLen;
|
||||
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
|
||||
CYSFReflector* refl = new CYSFReflector;
|
||||
refl->m_id = std::string(p1);
|
||||
refl->m_name = std::string(p2);
|
||||
refl->m_addr = addr;
|
||||
refl->m_addrLen = addrLen;
|
||||
m_reflectors.push_back(refl);
|
||||
} else {
|
||||
LogWarning("Unable to resolve the address for %s", host.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue