Fix network ports datatype (unsigned int -> unsigned short). UDPSocket: fix old bug using m_port instead of m_port[x].

This commit is contained in:
Daniel Caujolle-Bert 2021-04-25 07:43:56 +02:00
parent 183615d52e
commit 310cf0c8ff
No known key found for this signature in database
GPG key ID: 51AED7171EC00614
9 changed files with 34 additions and 33 deletions

View file

@ -145,11 +145,11 @@ bool CConf::read()
else if (::strcmp(key, "RptAddress") == 0)
m_rptAddress = value;
else if (::strcmp(key, "RptPort") == 0)
m_rptPort = (unsigned int)::atoi(value);
m_rptPort = (unsigned short)::atoi(value);
else if (::strcmp(key, "LocalAddress") == 0)
m_myAddress = value;
else if (::strcmp(key, "LocalPort") == 0)
m_myPort = (unsigned int)::atoi(value);
m_myPort = (unsigned short)::atoi(value);
else if (::strcmp(key, "Daemon") == 0)
m_daemon = ::atoi(value) == 1;
} else if (section == SECTION_LOG) {
@ -167,7 +167,7 @@ bool CConf::read()
if (::strcmp(key, "Address") == 0)
m_dapnetAddress = value;
else if (::strcmp(key, "Port") == 0)
m_dapnetPort = (unsigned int)::atoi(value);
m_dapnetPort = (unsigned short)::atoi(value);
else if (::strcmp(key, "AuthKey") == 0) {
for (unsigned int i = 0U; value[i] != '\0'; i++) {
if (!::isspace(value[i]))
@ -214,7 +214,7 @@ std::string CConf::getRptAddress() const
return m_rptAddress;
}
unsigned int CConf::getRptPort() const
unsigned short CConf::getRptPort() const
{
return m_rptPort;
}
@ -224,7 +224,7 @@ std::string CConf::getMyAddress() const
return m_myAddress;
}
unsigned int CConf::getMyPort() const
unsigned short CConf::getMyPort() const
{
return m_myPort;
}
@ -264,7 +264,7 @@ std::string CConf::getDAPNETAddress() const
return m_dapnetAddress;
}
unsigned int CConf::getDAPNETPort() const
unsigned short CConf::getDAPNETPort() const
{
return m_dapnetPort;
}

12
Conf.h
View file

@ -37,9 +37,9 @@ public:
std::string getblacklistRegexfile() const;
std::string getwhitelistRegexfile() const;
std::string getRptAddress() const;
unsigned int getRptPort() const;
unsigned short getRptPort() const;
std::string getMyAddress() const;
unsigned int getMyPort() const;
unsigned short getMyPort() const;
bool getDaemon() const;
// The Log section
@ -51,7 +51,7 @@ public:
// The DAPNET section
std::string getDAPNETAddress() const;
unsigned int getDAPNETPort() const;
unsigned short getDAPNETPort() const;
std::string getDAPNETAuthKey() const;
bool getDAPNETDebug() const;
@ -65,9 +65,9 @@ private:
std::string m_blacklistRegexfile;
std::string m_whitelistRegexfile;
std::string m_rptAddress;
unsigned int m_rptPort;
unsigned short m_rptPort;
std::string m_myAddress;
unsigned int m_myPort;
unsigned short m_myPort;
bool m_daemon;
unsigned int m_logDisplayLevel;
@ -77,7 +77,7 @@ private:
bool m_logFileRotate;
std::string m_dapnetAddress;
unsigned int m_dapnetPort;
unsigned short m_dapnetPort;
std::string m_dapnetAuthKey;
bool m_dapnetDebug;
};

View file

@ -213,9 +213,9 @@ int CDAPNETGateway::run()
bool debug = m_conf.getDAPNETDebug();
std::string rptAddress = m_conf.getRptAddress();
unsigned int rptPort = m_conf.getRptPort();
unsigned short rptPort = m_conf.getRptPort();
std::string myAddress = m_conf.getMyAddress();
unsigned int myPort = m_conf.getMyPort();
unsigned short myPort = m_conf.getMyPort();
m_pocsagNetwork = new CPOCSAGNetwork(myAddress, myPort, rptAddress, rptPort, debug);
ret = m_pocsagNetwork->open();
@ -228,7 +228,7 @@ int CDAPNETGateway::run()
std::string callsign = m_conf.getCallsign();
std::string dapnetAddress = m_conf.getDAPNETAddress();
unsigned int dapnetPort = m_conf.getDAPNETPort();
unsigned short dapnetPort = m_conf.getDAPNETPort();
std::string dapnetAuthKey = m_conf.getDAPNETAuthKey();
if (dapnetAuthKey.length() == 0 || dapnetAuthKey == "TOPSECRET") {

View file

@ -29,7 +29,7 @@
const unsigned int BUFFER_LENGTH = 200U;
CDAPNETNetwork::CDAPNETNetwork(const std::string& address, unsigned int port, const std::string& callsign, const std::string& authKey, const char* version, bool loggedIn, int failCount, bool debug) :
CDAPNETNetwork::CDAPNETNetwork(const std::string& address, unsigned short port, const std::string& callsign, const std::string& authKey, const char* version, bool loggedIn, int failCount, bool debug) :
m_socket(address, port),
m_callsign(callsign),
m_authKey(authKey),

View file

@ -28,7 +28,7 @@
class CDAPNETNetwork {
public:
CDAPNETNetwork(const std::string& address, unsigned int port, const std::string& callsign, const std::string& authKey, const char* version, bool loggedIn, int failCount, bool debug);
CDAPNETNetwork(const std::string& address, unsigned short port, const std::string& callsign, const std::string& authKey, const char* version, bool loggedIn, int failCount, bool debug);
~CDAPNETNetwork();
bool open();

View file

@ -25,7 +25,7 @@
#include <cstring>
CPOCSAGNetwork::CPOCSAGNetwork(const std::string& localAddress, unsigned int localPort, const std::string& remoteAddress, unsigned int remotePort, bool debug) :
CPOCSAGNetwork::CPOCSAGNetwork(const std::string& localAddress, unsigned short localPort, const std::string& remoteAddress, unsigned short remotePort, bool debug) :
m_socket(localAddress, localPort),
m_addr(),
m_addrLen(0U),

View file

@ -27,7 +27,7 @@
class CPOCSAGNetwork {
public:
CPOCSAGNetwork(const std::string& localAddress, unsigned int localPort, const std::string& remoteAddress, unsigned int remotePort, bool debug);
CPOCSAGNetwork(const std::string& localAddress, unsigned short localPort, const std::string& remoteAddress, unsigned short remotePort, bool debug);
~CPOCSAGNetwork();
bool open();

View file

@ -28,11 +28,12 @@
#if defined(HAVE_LOG_H)
#include "Log.h"
#else
#define LogMessage(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
#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) :
CUDPSocket::CUDPSocket(const std::string& address, unsigned short port) :
m_address_save(address),
m_port_save(port),
m_counter(0U)
@ -45,7 +46,7 @@ m_counter(0U)
}
}
CUDPSocket::CUDPSocket(unsigned int port) :
CUDPSocket::CUDPSocket(unsigned short port) :
m_address_save(),
m_port_save(port),
m_counter(0U)
@ -79,7 +80,7 @@ void CUDPSocket::shutdown()
#endif
}
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage& addr, unsigned int& address_length)
int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockaddr_storage& addr, unsigned int& address_length)
{
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
@ -87,7 +88,7 @@ int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_
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)
int CUDPSocket::lookup(const std::string& hostname, unsigned short port, sockaddr_storage& addr, unsigned int& address_length, struct addrinfo& hints)
{
std::string portstr = std::to_string(port);
struct addrinfo *res;
@ -170,7 +171,7 @@ 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)
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned short port)
{
sockaddr_storage addr;
unsigned int addrlen;
@ -180,8 +181,6 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std
hints.ai_flags = AI_PASSIVE;
hints.ai_family = af;
close(index);
/* to determine protocol family, call lookup() first. */
int err = lookup(address, port, addr, addrlen, hints);
if (err != 0) {
@ -189,6 +188,8 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std
return false;
}
close(index);
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
if (fd < 0) {
#if defined(_WIN32) || defined(_WIN64)
@ -224,7 +225,7 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std
return false;
}
LogInfo("Opening UDP port on %u", port);
LogInfo("Opening UDP port on %hu", port);
}
return true;
@ -293,7 +294,7 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
LogError("Error returned from recvfrom, err: %d", errno);
if (len == -1 && errno == ENOTSOCK) {
LogMessage("Re-opening UDP port on %u", m_port);
LogMessage("Re-opening UDP port on %hu", m_port[index]);
close();
open();
}

View file

@ -46,13 +46,13 @@ enum IPMATCHTYPE {
class CUDPSocket {
public:
CUDPSocket(const std::string& address, unsigned int port = 0U);
CUDPSocket(unsigned int port = 0U);
CUDPSocket(const std::string& address, unsigned short port = 0U);
CUDPSocket(unsigned short port = 0U);
~CUDPSocket();
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);
bool open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned short 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);
@ -63,8 +63,8 @@ public:
static void startup();
static void shutdown();
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 int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& address_length);
static int lookup(const std::string& hostName, unsigned short port, sockaddr_storage& address, unsigned int& address_length, struct addrinfo& hints);
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type = IMT_ADDRESS_AND_PORT);