Dev/fix port size (#805)

* port datatype changing from int to uint16_t
* throwing for -1 given for uint16_t ports
This commit is contained in:
2023-09-28 09:36:39 +02:00
committed by GitHub
parent 77d13f0794
commit 9834b07b47
61 changed files with 519 additions and 345 deletions

View File

@ -1083,6 +1083,17 @@ template <> defs::polarity StringTo(const std::string &s) {
throw RuntimeError("Unknown polarity mode " + s);
}
template <> uint16_t StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10;
int value = std::stoi(s, nullptr, base);
if (value < std::numeric_limits<uint16_t>::min() ||
value > std::numeric_limits<uint16_t>::max()) {
throw RuntimeError("Cannot scan uint16_t from string '" + s +
"'. Value must be in range 0 - 65535.");
}
return static_cast<uint16_t>(value);
}
template <> uint32_t StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stoul(s, nullptr, base);

View File

@ -15,8 +15,8 @@
namespace sls {
UdpRxSocket::UdpRxSocket(int port, ssize_t packet_size, const char *hostname,
int kernel_buffer_size)
UdpRxSocket::UdpRxSocket(uint16_t port, ssize_t packet_size,
const char *hostname, int kernel_buffer_size)
: packet_size_(packet_size) {
struct addrinfo hints {};
hints.ai_family = AF_UNSPEC;

View File

@ -15,7 +15,7 @@ namespace sls {
using namespace rapidjson;
ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
const uint32_t portnumber)
const uint16_t portnumber)
: portno(portnumber), sockfd(false) {
// Extra check that throws if conversion fails, could be removed
auto ipstr = HostnameToIp(hostname_or_ip).str();
@ -55,7 +55,7 @@ ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
<< GetReceiveHighWaterMark();
}
ZmqSocket::ZmqSocket(const uint32_t portnumber, const char *ethip)
ZmqSocket::ZmqSocket(const uint16_t portnumber, const char *ethip)
: portno(portnumber), sockfd(true) {
// create context
sockfd.contextDescriptor = zmq_ctx_new();
@ -289,24 +289,24 @@ int ZmqSocket::ReceiveHeader(const int index, zmqHeader &zHeader,
header_buffer.get(), MAX_STR_LENGTH, 0);
if (bytes_received > 0) {
#ifdef ZMQ_DETAIL
cprintf(BLUE, "Header %d [%d] Length: %d Header:%s \n", index, portno,
cprintf(BLUE, "Header %d [%hu] Length: %d Header:%s \n", index, portno,
bytes_received, header_buffer.get());
#endif
if (ParseHeader(index, bytes_received, header_buffer.get(), zHeader,
version)) {
#ifdef ZMQ_DETAIL
cprintf(RED, "Parsed Header %d [%d] Length: %d Header:%s \n", index,
portno, bytes_received, header_buffer.get());
cprintf(RED, "Parsed Header %d [%hu] Length: %d Header:%s \n",
index, portno, bytes_received, header_buffer.get());
#endif
if (!zHeader.data) {
#ifdef ZMQ_DETAIL
cprintf(RED, "%d [%d] Received end of acquisition\n", index,
cprintf(RED, "%d [%hu] Received end of acquisition\n", index,
portno);
#endif
return 0;
}
#ifdef ZMQ_DETAIL
cprintf(GREEN, "%d [%d] data\n", index, portno);
cprintf(GREEN, "%d [%hu] data\n", index, portno);
#endif
return 1;
}

View File

@ -10,6 +10,7 @@
#include <cstring>
#include <ifaddrs.h>
#include <iomanip>
#include <limits>
#include <net/if.h>
#include <netdb.h>
#include <sstream>
@ -203,4 +204,18 @@ MacAddr InterfaceNameToMac(const std::string &inf) {
return MacAddr(mac);
}
void validatePortNumber(uint16_t port) {
// random local port. might work if internal = bad practise
if (port == 0) {
throw RuntimeError("Invalid port number. Must be between 1 - 65535.");
}
}
void validatePortRange(uint16_t startPort, int numPorts) {
validatePortNumber(startPort);
if ((startPort + numPorts) > std::numeric_limits<uint16_t>::max()) {
throw RuntimeError("Invalid port range. Must be between 1 - 65535.");
}
}
} // namespace sls

View File

@ -4,9 +4,12 @@
#include "sls/string_utils.h"
#include "sls/container_utils.h"
#include "sls/network_utils.h"
#include <algorithm>
#include <iomanip>
#include <sls/ToString.h>
#include <sstream>
namespace sls {
std::vector<std::string> split(const std::string &strToSplit, char delimeter) {
@ -50,15 +53,15 @@ bool replace_first(std::string *s, const std::string &substr,
return false;
}
std::pair<std::string, int> ParseHostPort(const std::string &s) {
std::pair<std::string, uint16_t> ParseHostPort(const std::string &s) {
// TODO deal with to many :, port not there?
// no port return hostname as is and port as 0
std::string host;
int port{0};
uint16_t port{0};
auto res = split(s, ':');
host = res[0];
if (res.size() > 1) {
port = std::stoi(res[1]);
port = StringTo<uint16_t>(res[1]);
}
return std::make_pair(host, port);
}