Skip to content

Commit

Permalink
Merge branch 'ipv6' into SimpleDMR
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Sep 3, 2020
2 parents 132fb62 + 34db212 commit 3de54f6
Show file tree
Hide file tree
Showing 24 changed files with 298 additions and 265 deletions.
8 changes: 8 additions & 0 deletions Conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ m_lcdprocDimOnIdle(false),
m_lockFileEnabled(false),
m_lockFileName(),
m_remoteControlEnabled(false),
m_remoteControlAddress("127.0.0.1"),
m_remoteControlPort(0U)
{
}
Expand Down Expand Up @@ -938,6 +939,8 @@ bool CConf::read()
} else if (section == SECTION_REMOTE_CONTROL) {
if (::strcmp(key, "Enable") == 0)
m_remoteControlEnabled = ::atoi(value) == 1;
else if (::strcmp(key, "Address") == 0)
m_remoteControlAddress = value;
else if (::strcmp(key, "Port") == 0)
m_remoteControlPort = (unsigned int)::atoi(value);
}
Expand Down Expand Up @@ -2019,6 +2022,11 @@ bool CConf::getRemoteControlEnabled() const
return m_remoteControlEnabled;
}

std::string CConf::getRemoteControlAddress() const
{
return m_remoteControlAddress;
}

unsigned int CConf::getRemoteControlPort() const
{
return m_remoteControlPort;
Expand Down
2 changes: 2 additions & 0 deletions Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class CConf

// The Remote Control section
bool getRemoteControlEnabled() const;
std::string getRemoteControlAddress() const;
unsigned int getRemoteControlPort() const;

private:
Expand Down Expand Up @@ -547,6 +548,7 @@ class CConf
std::string m_lockFileName;

bool m_remoteControlEnabled;
std::string m_remoteControlAddress;
unsigned int m_remoteControlPort;
};

Expand Down
19 changes: 11 additions & 8 deletions DMRNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const unsigned int HOMEBREW_DATA_PACKET_LENGTH = 55U;
CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, bool duplex, const char* version, bool debug, bool slot1, bool slot2, HW_TYPE hwType) :
m_addressStr(address),
m_address(),
m_addrlen(),
m_port(port),
m_id(NULL),
m_duplex(duplex),
Expand All @@ -61,7 +62,7 @@ m_pingTimer(1000U, 10U)
assert(port > 0U);
assert(id > 1000U);

m_address = CUDPSocket::lookup(address);
CUDPSocket::lookup(m_addressStr, m_port, m_address, m_addrlen);

m_buffer = new unsigned char[BUFFER_LENGTH];
m_id = new uint8_t[4U];
Expand Down Expand Up @@ -101,8 +102,10 @@ bool CDMRNetwork::open()
{
LogMessage("DMR, Opening DMR Network");

bool ret = m_socket.open();
if (CUDPSocket::isnone(m_address))
CUDPSocket::lookup(m_addressStr, m_port, m_address, m_addrlen);

bool ret = m_socket.open();
if (ret)
m_pingTimer.start();

Expand Down Expand Up @@ -293,14 +296,14 @@ void CDMRNetwork::clock(unsigned int ms)
m_pingTimer.start();
}

in_addr address;
unsigned int port;
int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, port);
sockaddr_storage address;
unsigned int addrlen;
int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, addrlen);
if (length <= 0)
return;

if (m_address.s_addr != address.s_addr || m_port != port) {
LogMessage("DMR, packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port);
if (!CUDPSocket::match(m_address, address)) {
LogMessage("DMR, packet received from an invalid source");
return;
}

Expand Down Expand Up @@ -424,7 +427,7 @@ bool CDMRNetwork::write(const unsigned char* data, unsigned int length)
if (m_debug)
CUtils::dump(1U, "Network Transmitted", data, length);

bool ret = m_socket.write(data, length, m_address, m_port);
bool ret = m_socket.write(data, length, m_address, m_addrlen);
if (!ret) {
LogError("DMR, socket error when writing to the DMR Gateway");
return false;
Expand Down
3 changes: 2 additions & 1 deletion DMRNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class CDMRNetwork

private:
std::string m_addressStr;
in_addr m_address;
sockaddr_storage m_address;
unsigned int m_addrlen;
unsigned int m_port;
uint8_t* m_id;
bool m_duplex;
Expand Down
28 changes: 11 additions & 17 deletions DStarNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const unsigned int BUFFER_LENGTH = 100U;
CDStarNetwork::CDStarNetwork(const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int localPort, bool duplex, const char* version, bool debug) :
m_socket(localPort),
m_address(),
m_port(gatewayPort),
m_addrlen(),
m_duplex(duplex),
m_version(version),
m_debug(debug),
Expand All @@ -47,7 +47,7 @@ m_linkStatus(LS_NONE),
m_linkReflector(NULL),
m_random()
{
m_address = CUDPSocket::lookup(gatewayAddress);
CUDPSocket::lookup(gatewayAddress, gatewayPort, m_address, m_addrlen);

m_linkReflector = new unsigned char[DSTAR_LONG_CALLSIGN_LENGTH];

Expand All @@ -65,12 +65,12 @@ bool CDStarNetwork::open()
{
LogMessage("Opening D-Star network connection");

if (m_address.s_addr == INADDR_NONE)
if (CUDPSocket::isnone(m_address))
return false;

m_pollTimer.start();

return m_socket.open();
return m_socket.open(m_address.ss_family);
}

bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length, bool busy)
Expand Down Expand Up @@ -103,7 +103,7 @@ bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length
CUtils::dump(1U, "D-Star Network Header Sent", buffer, 49U);

for (unsigned int i = 0U; i < 2U; i++) {
bool ret = m_socket.write(buffer, 49U, m_address, m_port);
bool ret = m_socket.write(buffer, 49U, m_address, m_addrlen);
if (!ret)
return false;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ bool CDStarNetwork::writeData(const unsigned char* data, unsigned int length, un
if (m_debug)
CUtils::dump(1U, "D-Star Network Data Sent", buffer, length + 9U);

return m_socket.write(buffer, length + 9U, m_address, m_port);
return m_socket.write(buffer, length + 9U, m_address, m_addrlen);
}

bool CDStarNetwork::writePoll(const char* text)
Expand All @@ -170,7 +170,7 @@ bool CDStarNetwork::writePoll(const char* text)
// if (m_debug)
// CUtils::dump(1U, "D-Star Network Poll Sent", buffer, 6U + length);

return m_socket.write(buffer, 6U + length, m_address, m_port);
return m_socket.write(buffer, 6U + length, m_address, m_addrlen);
}

void CDStarNetwork::clock(unsigned int ms)
Expand All @@ -195,18 +195,12 @@ void CDStarNetwork::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);
if (length <= 0)
sockaddr_storage address;
unsigned int addrlen;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrlen);
if (length <= 0 || !CUDPSocket::match(m_address, address))
return;

// Check if the data is for us
if (m_address.s_addr != address.s_addr || m_port != port) {
LogMessage("D-Star packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port);
return;
}

// Invalid packet type?
if (::memcmp(buffer, "DSRP", 4U) != 0)
return;
Expand Down
4 changes: 2 additions & 2 deletions DStarNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class CDStarNetwork {

private:
CUDPSocket m_socket;
in_addr m_address;
unsigned int m_port;
sockaddr_storage m_address;
unsigned int m_addrlen;
bool m_duplex;
const char* m_version;
bool m_debug;
Expand Down
57 changes: 33 additions & 24 deletions LCDproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,44 +121,53 @@ CLCDproc::~CLCDproc()

bool CLCDproc::open()
{
const char *server;
unsigned int port, localPort;
struct sockaddr_in serverAddress, clientAddress;
struct hostent *h;
int err;
unsigned int addrlen;
std::string port, localPort;
struct sockaddr_storage serverAddress, clientAddress;
struct addrinfo hints, *res;

server = m_address.c_str();
port = m_port;
localPort = m_localPort;
port = std::to_string(m_port);
localPort = std::to_string(m_localPort);
memset(&hints, 0, sizeof(hints));

/* Lookup the hostname address */
hints.ai_flags = AI_NUMERICSERV;
hints.ai_socktype = SOCK_STREAM;
err = getaddrinfo(m_address.c_str(), port.c_str(), &hints, &res);
if (err) {
LogError("LCDproc, cannot lookup server");
return false;
}
memcpy(&serverAddress, res->ai_addr, addrlen = res->ai_addrlen);
freeaddrinfo(res);

/* Lookup the client address (random port - need to specify manual port from ini file) */
hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;
hints.ai_family = serverAddress.ss_family;
err = getaddrinfo(NULL, localPort.c_str(), &hints, &res);
if (err) {
LogError("LCDproc, cannot lookup client");
return false;
}
memcpy(&clientAddress, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);

/* Create TCP socket */
m_socketfd = socket(AF_INET, SOCK_STREAM, 0);
m_socketfd = socket(clientAddress.ss_family, SOCK_STREAM, 0);
if (m_socketfd == -1) {
LogError("LCDproc, failed to create socket");
return false;
}

/* Sets client address (random port - need to specify manual port from ini file?) */
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
//clientAddress.sin_port = htons(0);
clientAddress.sin_port = htons(localPort);

/* Bind the address to the socket */
if (bind(m_socketfd, (struct sockaddr *)&clientAddress, sizeof(clientAddress)) == -1) {
if (bind(m_socketfd, (struct sockaddr *)&clientAddress, addrlen) == -1) {
LogError("LCDproc, error whilst binding address");
return false;
}

/* Lookup the hostname address */
h = gethostbyname(server);

/* Sets server address */
serverAddress.sin_family = h->h_addrtype;
memcpy((char*)&serverAddress.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
serverAddress.sin_port = htons(port);

if (connect(m_socketfd, (struct sockaddr * )&serverAddress, sizeof(serverAddress))==-1) {
/* Connect to server */
if (connect(m_socketfd, (struct sockaddr *)&serverAddress, addrlen) == -1) {
LogError("LCDproc, cannot connect to server");
return false;
}
Expand Down
1 change: 1 addition & 0 deletions MMDVM.ini
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,5 @@ File=/tmp/MMDVM_Active.lck

[Remote Control]
Enable=0
Address=127.0.0.1
Port=7642
21 changes: 11 additions & 10 deletions MMDVMHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ int CMMDVMHost::run()
return 1;
}

in_addr transparentAddress;
unsigned int transparentPort = 0U;
sockaddr_storage transparentAddress;
unsigned int transparentAddrLen;
CUDPSocket* transparentSocket = NULL;

unsigned int sendFrameType = 0U;
Expand All @@ -338,11 +338,10 @@ int CMMDVMHost::run()
LogInfo(" Local Port: %u", localPort);
LogInfo(" Send Frame Type: %u", sendFrameType);

transparentAddress = CUDPSocket::lookup(remoteAddress);
transparentPort = remotePort;
CUDPSocket::lookup(remoteAddress, remotePort, transparentAddress, transparentAddrLen);

transparentSocket = new CUDPSocket(localPort);
ret = transparentSocket->open();
ret = transparentSocket->open(transparentAddress.ss_family);
if (!ret) {
LogWarning("Could not open the Transparent data socket, disabling");
delete transparentSocket;
Expand Down Expand Up @@ -611,12 +610,14 @@ int CMMDVMHost::run()

bool remoteControlEnabled = m_conf.getRemoteControlEnabled();
if (remoteControlEnabled) {
std::string address = m_conf.getRemoteControlAddress();
unsigned int port = m_conf.getRemoteControlPort();

LogInfo("Remote Control Parameters");
LogInfo(" Address: %s", address.c_str());
LogInfo(" Port: %u", port);

m_remoteControl = new CRemoteControl(port);
m_remoteControl = new CRemoteControl(address, port);

ret = m_remoteControl->open();
if (!ret) {
Expand Down Expand Up @@ -803,7 +804,7 @@ int CMMDVMHost::run()

len = m_modem->readTransparentData(data);
if (transparentSocket != NULL && len > 0U)
transparentSocket->write(data, len, transparentAddress, transparentPort);
transparentSocket->write(data, len, transparentAddress, transparentAddrLen);

if (!m_fixedMode) {
if (m_modeTimer.isRunning() && m_modeTimer.hasExpired())
Expand Down Expand Up @@ -952,9 +953,9 @@ int CMMDVMHost::run()
}

if (transparentSocket != NULL) {
in_addr address;
unsigned int port = 0U;
len = transparentSocket->read(data, 200U, address, port);
sockaddr_storage address;
unsigned int addrlen;
len = transparentSocket->read(data, 200U, address, addrlen);
if (len > 0U)
m_modem->writeTransparentData(data, len);
}
Expand Down
Loading

0 comments on commit 3de54f6

Please sign in to comment.