mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-21 00:58:01 +02:00
Merge branch 'refactor' of github.com:slsdetectorgroup/slsDetectorPackage into refactor
This commit is contained in:
@ -7,6 +7,7 @@ set(SOURCES
|
||||
src/DataSocket.cpp
|
||||
src/ServerSocket.cpp
|
||||
src/ServerInterface.cpp
|
||||
src/network_utils.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
@ -30,6 +31,7 @@ set(PUBLICHEADERS
|
||||
include/DataSocket.h
|
||||
include/ServerSocket.h
|
||||
include/ServerInterface.h
|
||||
include/network_utils.h
|
||||
)
|
||||
|
||||
add_library(slsSupportLib SHARED
|
||||
|
45
slsSupportLib/include/network_utils.h
Normal file
45
slsSupportLib/include/network_utils.h
Normal file
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace sls {
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
|
||||
class IpAddr {
|
||||
private:
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
constexpr IpAddr(uint32_t address) noexcept : addr_{address} {}
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
std::string str() const;
|
||||
std::string hex() const;
|
||||
constexpr bool operator==(const IpAddr &other) const noexcept { return addr_ == other.addr_; }
|
||||
constexpr bool operator!=(const IpAddr &other) const noexcept { return addr_ != other.addr_; }
|
||||
constexpr bool operator==(const uint32_t other) const noexcept { return addr_ == other; }
|
||||
constexpr bool operator!=(const uint32_t other) const noexcept { return addr_ != other; }
|
||||
};
|
||||
|
||||
class MacAddr {
|
||||
private:
|
||||
uint64_t addr_{0};
|
||||
std::string to_hex(const char delimiter = 0) const;
|
||||
|
||||
public:
|
||||
constexpr MacAddr(uint64_t mac) noexcept : addr_{mac} {}
|
||||
MacAddr(std::string mac);
|
||||
MacAddr(const char *address);
|
||||
std::string str() const;
|
||||
std::string hex() const;
|
||||
constexpr bool operator==(const MacAddr &other) const noexcept { return addr_ == other.addr_; }
|
||||
constexpr bool operator!=(const MacAddr &other) const noexcept { return addr_ != other.addr_; }
|
||||
constexpr bool operator==(const uint64_t other) const noexcept { return addr_ == other; }
|
||||
constexpr bool operator!=(const uint64_t other) const noexcept { return addr_ != other; }
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr);
|
||||
|
||||
} // namespace sls
|
@ -2,6 +2,8 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
namespace sls {
|
||||
|
||||
@ -14,10 +16,18 @@ Still this is better than strcpy and a buffer overflow...
|
||||
*/
|
||||
template <size_t array_size>
|
||||
void strcpy_safe(char (&destination)[array_size], const char *source) {
|
||||
assert(array_size > strlen(source));
|
||||
strncpy(destination, source, array_size-1);
|
||||
destination[array_size - 1] = '\0';
|
||||
}
|
||||
|
||||
template <size_t array_size>
|
||||
void strcpy_safe(char (&destination)[array_size], const std::string& source) {
|
||||
assert(array_size > source.size());
|
||||
strncpy(destination, source.c_str(), array_size-1);
|
||||
destination[array_size - 1] = '\0';
|
||||
}
|
||||
|
||||
/*
|
||||
Removes all occurrences of the specified char from a c string
|
||||
Templated on array size to ensure no access after buffer limits.
|
||||
@ -51,6 +61,12 @@ Concatenate strings using + if the strings are different
|
||||
*/
|
||||
std::string concatenateIfDifferent(const std::vector<std::string> &container);
|
||||
|
||||
/*
|
||||
Concatenate vector of things with str method using + if the strings are different
|
||||
*/
|
||||
template<typename T>
|
||||
std::string concatenateIfDifferent(const std::vector<T> &container);
|
||||
|
||||
/*
|
||||
Convert an ip address string to a string in hex format. (removing dots)
|
||||
*/
|
||||
|
@ -81,7 +81,7 @@ void DataSocket::close() {
|
||||
if(::close(socketId_)){
|
||||
throw SocketError("could not close socket");
|
||||
}
|
||||
socketId_ = 0;
|
||||
socketId_ = -1;
|
||||
|
||||
} else {
|
||||
throw std::runtime_error("Socket ERROR: close called on bad socket\n");
|
||||
|
94
slsSupportLib/src/network_utils.cpp
Normal file
94
slsSupportLib/src/network_utils.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "network_utils.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
IpAddr::IpAddr(const std::string &address) {
|
||||
inet_pton(AF_INET, address.c_str(), &addr_);
|
||||
}
|
||||
|
||||
IpAddr::IpAddr(const char *address) {
|
||||
inet_pton(AF_INET, address, &addr_);
|
||||
}
|
||||
|
||||
std::string IpAddr::str() const {
|
||||
char ipstring[INET_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET, &addr_, ipstring, INET_ADDRSTRLEN);
|
||||
return ipstring;
|
||||
}
|
||||
std::string IpAddr::hex() const {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
for (int i = 0; i != 4; ++i) {
|
||||
ss << ((addr_ >> i * 8) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
MacAddr::MacAddr(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') ||
|
||||
(mac[8] != ':') || (mac[11] != ':') || (mac[14] != ':')) {
|
||||
addr_ = 0;
|
||||
} else {
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::strtoul(mac.c_str(), nullptr, 16);
|
||||
}
|
||||
}
|
||||
MacAddr::MacAddr(const char *address) : MacAddr(std::string(address)) {}
|
||||
|
||||
std::string MacAddr::to_hex(const char delimiter) const {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((addr_ >> 40) & 0xFF);
|
||||
for (int i = 32; i >= 0; i -= 8) {
|
||||
if (delimiter)
|
||||
ss << delimiter;
|
||||
ss << ((addr_ >> i) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string MacAddr::str() const {
|
||||
return to_hex(':');
|
||||
}
|
||||
|
||||
std::string MacAddr::hex() const {
|
||||
return to_hex();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr) {
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr) {
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname) {
|
||||
struct addrinfo hints, *result;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
if (getaddrinfo(hostname, NULL, &hints, &result)) {
|
||||
freeaddrinfo(result);
|
||||
throw RuntimeError("Could not convert hostname to ip");
|
||||
}
|
||||
uint32_t ip = ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
|
||||
freeaddrinfo(result);
|
||||
return ip;
|
||||
}
|
||||
|
||||
} // namespace sls
|
@ -1,14 +1,13 @@
|
||||
|
||||
#include "string_utils.h"
|
||||
#include "container_utils.h"
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include "network_utils.h"
|
||||
#include <algorithm>
|
||||
namespace sls{
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
namespace sls {
|
||||
|
||||
|
||||
std::vector<std::string> split(const std::string& strToSplit, char delimeter)
|
||||
{
|
||||
std::vector<std::string> split(const std::string &strToSplit, char delimeter) {
|
||||
std::stringstream ss(strToSplit);
|
||||
std::string item;
|
||||
std::vector<std::string> splittedStrings;
|
||||
@ -18,39 +17,47 @@ std::vector<std::string> split(const std::string& strToSplit, char delimeter)
|
||||
return splittedStrings;
|
||||
}
|
||||
|
||||
|
||||
std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
|
||||
std::string concatenateNonEmptyStrings(const std::vector<std::string> &vec) {
|
||||
std::string ret;
|
||||
for (const auto& s : vec)
|
||||
for (const auto &s : vec)
|
||||
if (!s.empty())
|
||||
ret += s + '+';
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string concatenateIfDifferent(const std::vector<std::string>& container)
|
||||
{
|
||||
std::string concatenateIfDifferent(const std::vector<std::string> &container) {
|
||||
if (allEqual(container)) {
|
||||
return container.front();
|
||||
} else {
|
||||
std::string result;
|
||||
for (const auto& s : container)
|
||||
for (const auto &s : container)
|
||||
result += s + '+';
|
||||
return result;
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
std::string concatenateIfDifferent(const std::vector<T> &container) {
|
||||
if (allEqual(container)) {
|
||||
return container.front().str();
|
||||
} else {
|
||||
std::string result;
|
||||
for (const auto &s : container)
|
||||
result += s.str() + '+';
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
std::string stringIpToHex(const std::string& ip)
|
||||
{
|
||||
template std::string concatenateIfDifferent(const std::vector<IpAddr> &);
|
||||
template std::string concatenateIfDifferent(const std::vector<MacAddr> &);
|
||||
|
||||
std::string stringIpToHex(const std::string &ip) {
|
||||
std::istringstream iss(ip);
|
||||
std::ostringstream oss;
|
||||
std::string item;
|
||||
while (std::getline(iss, item, '.'))
|
||||
{
|
||||
while (std::getline(iss, item, '.')) {
|
||||
oss << std::setw(2) << std::setfill('0') << std::hex << std::stoi(item);
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}; // namespace sls
|
@ -10,6 +10,7 @@ set(SOURCES
|
||||
test-ClientInterface.cpp
|
||||
test-CmdLineParser.cpp
|
||||
test-container_utils.cpp
|
||||
test-network_utils.cpp
|
||||
test-string_utils.cpp
|
||||
test-Timer.cpp
|
||||
)
|
||||
|
51
slsSupportLib/tests/test-network_utils.cpp
Normal file
51
slsSupportLib/tests/test-network_utils.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "network_utils.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "string_utils.h"
|
||||
|
||||
using namespace sls;
|
||||
|
||||
|
||||
TEST_CASE("Convert mac address using classes") {
|
||||
|
||||
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
|
||||
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4", "ee:50:22:46:d9:f4"};
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto mac0 = MacAddr(vec_addr[i]);
|
||||
auto mac1 = MacAddr(vec_ans[i]);
|
||||
|
||||
CHECK(mac0 == vec_addr[i]);
|
||||
CHECK(mac1 == vec_addr[i]);
|
||||
CHECK(mac0 == vec_ans[i]);
|
||||
CHECK(mac1 == vec_ans[i]);
|
||||
CHECK(mac0.str() == vec_ans[i]);
|
||||
CHECK(mac1.str() == vec_ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Convert IP using classes ") {
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"};
|
||||
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto ip0 = IpAddr(vec_addr[i]);
|
||||
auto ip1 = IpAddr(vec_ans[i]);
|
||||
|
||||
CHECK(ip0 == ip1);
|
||||
CHECK(ip0 == vec_addr[i]);
|
||||
CHECK(ip1 == vec_addr[i]);
|
||||
CHECK(ip0 == vec_ans[i]);
|
||||
CHECK(ip1 == vec_ans[i]);
|
||||
CHECK(ip0.str() == vec_ans[i]);
|
||||
CHECK(ip1.str() == vec_ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Strange input gives 0"){
|
||||
CHECK(IpAddr("hej")== 0);
|
||||
CHECK(MacAddr("hej")== 0);
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
#include "MySocketTCP.h"
|
||||
#include "catch.hpp"
|
||||
// #include "multiSlsDetector.h"
|
||||
#include "logger.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
@ -23,6 +22,9 @@ TEST_CASE("copy a string") {
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifdef NDEBUG
|
||||
//This test can only run in release since we assert on the length of the string
|
||||
TEST_CASE("copy a long string"){
|
||||
auto src = "some very very long sting that does not fit";
|
||||
char dst[3];
|
||||
@ -32,7 +34,7 @@ TEST_CASE("copy a long string"){
|
||||
REQUIRE(dst[2]=='\0');
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
TEST_CASE("Concat") {
|
||||
std::vector<std::string> v{"one", "one", "one"};
|
||||
std::vector<std::string> v2{"one", "one", "one"};
|
||||
@ -110,4 +112,6 @@ TEST_CASE("Many characters in a row"){
|
||||
char str[] = "someeequitellll::ongstring";
|
||||
sls::removeChar(str, 'l');
|
||||
REQUIRE(std::string(str) == "someeequite::ongstring");
|
||||
}
|
||||
}
|
||||
|
||||
// TEST_CASE("concat things not being strings")
|
Reference in New Issue
Block a user