mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-25 07:40:03 +02:00
class for Ip and Mac
This commit is contained in:
parent
df2d67d90d
commit
b198b50377
@ -27,13 +27,7 @@ int main() {
|
||||
IpAddr a("129.129.205.242");
|
||||
IpAddr b(4073554305);
|
||||
|
||||
std::cout << "a is: " << a << " and b is: " << b << "\n";
|
||||
if (a == b)
|
||||
std::cout << "a is equal to b\n";
|
||||
|
||||
std::cout << "as hex they look like: " << a.hex() << "\n";
|
||||
std::cout << "and the best thing is that the size is only: " << sizeof(a) << " bytes\n";
|
||||
|
||||
|
||||
std::vector<IpAddr> vec;
|
||||
vec.push_back(a);
|
||||
return 0;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "error_defs.h"
|
||||
#include "logger.h"
|
||||
#include "sls_detector_defs.h"
|
||||
#include "network_utils.h"
|
||||
class ClientInterface;
|
||||
|
||||
#include <cmath>
|
||||
@ -141,28 +142,28 @@ struct sharedSlsDetector {
|
||||
int receiverUDPPort2;
|
||||
|
||||
/** ip address of the receiver for the detector to send packets to**/
|
||||
uint32_t receiverUDPIP;
|
||||
sls::IpAddr receiverUDPIP;
|
||||
|
||||
/** ip address of the receiver for the 2nd interface of the detector to send packets to**/
|
||||
uint32_t receiverUDPIP2;
|
||||
sls::IpAddr receiverUDPIP2;
|
||||
|
||||
/** mac address of receiver for the detector to send packets to **/
|
||||
uint64_t receiverUDPMAC;
|
||||
sls::MacAddr receiverUDPMAC;
|
||||
|
||||
/** mac address of receiver for the 2nd interface of the detector to send packets to **/
|
||||
uint64_t receiverUDPMAC2;
|
||||
sls::MacAddr receiverUDPMAC2;
|
||||
|
||||
/** mac address of the detector **/
|
||||
uint64_t detectorMAC;
|
||||
sls::MacAddr detectorMAC;
|
||||
|
||||
/** mac address of the 2nd interface of the detector **/
|
||||
uint64_t detectorMAC2;
|
||||
sls::MacAddr detectorMAC2;
|
||||
|
||||
/** ip address of the detector **/
|
||||
uint32_t detectorIP;
|
||||
sls::IpAddr detectorIP;
|
||||
|
||||
/** ip address of the 2nd interface of the detector **/
|
||||
uint32_t detectorIP2;
|
||||
sls::IpAddr detectorIP2;
|
||||
|
||||
/** number of udp interface */
|
||||
int numUDPInterfaces;
|
||||
@ -795,26 +796,26 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* @param detectorMAC detector MAC address
|
||||
* @returns the detector MAC address
|
||||
*/
|
||||
std::string setDetectorMAC(const std::string &detectorMAC);
|
||||
std::string setDetectorMAC(const std::string &address);
|
||||
|
||||
/**
|
||||
* Returns the detector MAC address\sa sharedSlsDetector
|
||||
* @returns the detector MAC address
|
||||
*/
|
||||
std::string getDetectorMAC();
|
||||
sls::MacAddr getDetectorMAC();
|
||||
|
||||
/**
|
||||
* Validates the format of the detector MAC address (bottom half) and sets it (Jungfrau only)
|
||||
* @param detectorMAC detector MAC address (bottom half)
|
||||
* @returns the detector MAC address (bottom half)
|
||||
*/
|
||||
std::string setDetectorMAC2(const std::string &detectorMAC);
|
||||
std::string setDetectorMAC2(const std::string &address);
|
||||
|
||||
/**
|
||||
* Returns the detector MAC address (bottom half) Jungfrau only
|
||||
* @returns the detector MAC address (bottom half)
|
||||
*/
|
||||
std::string getDetectorMAC2();
|
||||
sls::MacAddr getDetectorMAC2();
|
||||
|
||||
/**
|
||||
* Validates the format of the detector IP address and sets it \sa sharedSlsDetector
|
||||
@ -827,7 +828,7 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* Returns the detector IP address\sa sharedSlsDetector
|
||||
* @returns the detector IP address
|
||||
*/
|
||||
std::string getDetectorIP() const;
|
||||
sls::IpAddr getDetectorIP() const;
|
||||
|
||||
/**
|
||||
* Validates the format of the detector IP address (bottom half) and sets it (Jungfrau only)
|
||||
@ -840,7 +841,7 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* Returns the detector IP address (bottom half) Jungfrau only
|
||||
* @returns the detector IP address (bottom half)
|
||||
*/
|
||||
std::string getDetectorIP2() const;
|
||||
sls::IpAddr getDetectorIP2() const;
|
||||
|
||||
/**
|
||||
* Validates and sets the receiver.
|
||||
@ -868,7 +869,7 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* Returns the receiver UDP IP address\sa sharedSlsDetector
|
||||
* @returns the receiver UDP IP address
|
||||
*/
|
||||
std::string getReceiverUDPIP() const;
|
||||
sls::IpAddr getReceiverUDPIP() const;
|
||||
|
||||
/**
|
||||
* Validates the format of the receiver UDP IP address (bottom half) and sets it(Jungfrau only)
|
||||
@ -881,7 +882,7 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* Returns the receiver UDP IP address (bottom half) Jungfrau only
|
||||
* @returns the receiver UDP IP address (bottom half)
|
||||
*/
|
||||
std::string getReceiverUDPIP2() const;
|
||||
sls::IpAddr getReceiverUDPIP2() const;
|
||||
|
||||
/**
|
||||
* Validates the format of the receiver UDP MAC address and sets it \sa sharedSlsDetector
|
||||
@ -894,7 +895,7 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* Returns the receiver UDP MAC address\sa sharedSlsDetector
|
||||
* @returns the receiver UDP MAC address
|
||||
*/
|
||||
std::string getReceiverUDPMAC() const;
|
||||
sls::MacAddr getReceiverUDPMAC() const;
|
||||
|
||||
/**
|
||||
* Validates the format of the receiver UDP MAC address (bottom half) and sets it (Jungfrau only)
|
||||
@ -907,7 +908,7 @@ class slsDetector : public virtual slsDetectorDefs{
|
||||
* Returns the receiver UDP MAC address (bottom half) Jungfrau only
|
||||
* @returns the receiver UDP MAC address (bottom half)
|
||||
*/
|
||||
std::string getReceiverUDPMAC2() const;
|
||||
sls::MacAddr getReceiverUDPMAC2() const;
|
||||
|
||||
/**
|
||||
* Sets the receiver UDP port\sa sharedSlsDetector
|
||||
|
@ -9,7 +9,10 @@
|
||||
#include "slsDetectorCommand.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
|
||||
#include "container_utils.h"
|
||||
#include "string_utils.h"
|
||||
#include "network_utils.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
@ -19,17 +22,14 @@
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/types.h>
|
||||
//#include <time.h> //clock()
|
||||
|
||||
#include "container_utils.h"
|
||||
|
||||
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
#include <vector>
|
||||
|
||||
using sls::NotImplementedError;
|
||||
using sls::RuntimeError;
|
||||
using sls::SharedMemory;
|
||||
using sls::SharedMemoryError;
|
||||
using namespace sls;
|
||||
|
||||
multiSlsDetector::multiSlsDetector(int multi_id, bool verify, bool update)
|
||||
: multiId(multi_id), multi_shm(multi_id, -1) {
|
||||
@ -1430,7 +1430,7 @@ std::string multiSlsDetector::setDetectorMAC(const std::string &detectorMAC, int
|
||||
std::string multiSlsDetector::getDetectorMAC(int detPos) {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getDetectorMAC();
|
||||
return detectors[detPos]->getDetectorMAC().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1452,7 +1452,7 @@ std::string multiSlsDetector::setDetectorMAC2(const std::string &detectorMAC, in
|
||||
std::string multiSlsDetector::getDetectorMAC2(int detPos) {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getDetectorMAC2();
|
||||
return detectors[detPos]->getDetectorMAC2().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1474,7 +1474,7 @@ std::string multiSlsDetector::setDetectorIP(const std::string &detectorIP, int d
|
||||
std::string multiSlsDetector::getDetectorIP(int detPos) const {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getDetectorIP();
|
||||
return detectors[detPos]->getDetectorIP().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1496,7 +1496,7 @@ std::string multiSlsDetector::setDetectorIP2(const std::string &detectorIP, int
|
||||
std::string multiSlsDetector::getDetectorIP2(int detPos) const {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getDetectorIP2();
|
||||
return detectors[detPos]->getDetectorIP2().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1541,7 +1541,7 @@ std::string multiSlsDetector::setReceiverUDPIP(const std::string &udpip, int det
|
||||
std::string multiSlsDetector::getReceiverUDPIP(int detPos) const {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getReceiverUDPIP();
|
||||
return detectors[detPos]->getReceiverUDPIP().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1563,7 +1563,7 @@ std::string multiSlsDetector::setReceiverUDPIP2(const std::string &udpip, int de
|
||||
std::string multiSlsDetector::getReceiverUDPIP2(int detPos) const {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getReceiverUDPIP2();
|
||||
return detectors[detPos]->getReceiverUDPIP2().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1585,7 +1585,7 @@ std::string multiSlsDetector::setReceiverUDPMAC(const std::string &udpmac, int d
|
||||
std::string multiSlsDetector::getReceiverUDPMAC(int detPos) const {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getReceiverUDPMAC();
|
||||
return detectors[detPos]->getReceiverUDPMAC().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
@ -1607,7 +1607,7 @@ std::string multiSlsDetector::setReceiverUDPMAC2(const std::string &udpmac, int
|
||||
std::string multiSlsDetector::getReceiverUDPMAC2(int detPos) const {
|
||||
// single
|
||||
if (detPos >= 0) {
|
||||
return detectors[detPos]->getReceiverUDPMAC2();
|
||||
return detectors[detPos]->getReceiverUDPMAC2().str();
|
||||
}
|
||||
|
||||
// multi
|
||||
|
@ -313,13 +313,13 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
|
||||
|
||||
detector_shm()->receiverUDPIP = 0u;
|
||||
detector_shm()->receiverUDPIP2 = 0u;
|
||||
detector_shm()->receiverUDPMAC = 0u;
|
||||
detector_shm()->receiverUDPMAC2 = 0u;
|
||||
detector_shm()->receiverUDPMAC = 0ul;
|
||||
detector_shm()->receiverUDPMAC2 = 0ul;
|
||||
|
||||
detector_shm()->detectorMAC = MacStringToUint(DEFAULT_DET_MAC);
|
||||
detector_shm()->detectorMAC2 = MacStringToUint(DEFAULT_DET_MAC2);
|
||||
detector_shm()->detectorIP = IpStringToUint(DEFAULT_DET_MAC);
|
||||
detector_shm()->detectorIP2 = IpStringToUint(DEFAULT_DET_MAC2);
|
||||
detector_shm()->detectorMAC = DEFAULT_DET_MAC;
|
||||
detector_shm()->detectorMAC2 = DEFAULT_DET_MAC2;
|
||||
detector_shm()->detectorIP = DEFAULT_DET_MAC;
|
||||
detector_shm()->detectorIP2 = DEFAULT_DET_MAC2;
|
||||
|
||||
detector_shm()->numUDPInterfaces = 1;
|
||||
detector_shm()->selectedUDPInterface = 1;
|
||||
@ -1572,7 +1572,7 @@ int slsDetector::configureMAC() {
|
||||
char retvals[n_retvals][array_size]{};
|
||||
FILE_LOG(logDEBUG1) << "Configuring MAC";
|
||||
if (detector_shm()->receiverUDPIP == 0) {
|
||||
//If hostname is valid ip use that, oterwise lookup hostname
|
||||
// If hostname is valid ip use that, oterwise lookup hostname
|
||||
detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname);
|
||||
if (detector_shm()->receiverUDPIP == 0) {
|
||||
detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname);
|
||||
@ -1590,26 +1590,28 @@ int slsDetector::configureMAC() {
|
||||
detector_shm()->receiverUDPIP2 = detector_shm()->receiverUDPIP;
|
||||
}
|
||||
if (detector_shm()->receiverUDPMAC2 == 0) {
|
||||
throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set");
|
||||
}
|
||||
throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set");
|
||||
}
|
||||
FILE_LOG(logDEBUG1) << "rx_udpmac2 is valid ";
|
||||
}
|
||||
|
||||
// copy to args and convert to hex
|
||||
snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort);
|
||||
snprintf(args[1], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP));
|
||||
sls::strcpy_safe(args[2], getReceiverUDPMAC().c_str());
|
||||
sls::removeChar(args[2], ':');
|
||||
snprintf(args[3], array_size, "%x", __builtin_bswap32(detector_shm()->detectorIP));
|
||||
sls::strcpy_safe(args[4], getDetectorMAC().c_str());
|
||||
sls::removeChar(args[4], ':');
|
||||
// snprintf(args[1], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP));
|
||||
sls::strcpy_safe(args[1], getReceiverUDPIP().str());
|
||||
sls::strcpy_safe(args[2], getReceiverUDPMAC().hex().c_str());
|
||||
// sls::removeChar(args[2], ':');
|
||||
sls::strcpy_safe(args[3], getDetectorIP().hex().c_str());
|
||||
sls::strcpy_safe(args[4], getDetectorMAC().hex().c_str());
|
||||
// sls::removeChar(args[4], ':');
|
||||
snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2);
|
||||
snprintf(args[6], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP2));
|
||||
sls::strcpy_safe(args[7], getReceiverUDPMAC2().c_str());
|
||||
sls::removeChar(args[7], ':');
|
||||
snprintf(args[8], array_size, "%x", __builtin_bswap32(detector_shm()->detectorIP2));
|
||||
sls::strcpy_safe(args[9], getDetectorMAC2().c_str());
|
||||
sls::removeChar(args[9], ':');
|
||||
// snprintf(args[6], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP2));
|
||||
sls::strcpy_safe(args[6], getReceiverUDPIP2().str());
|
||||
sls::strcpy_safe(args[7], getReceiverUDPMAC2().hex().c_str());
|
||||
// sls::removeChar(args[7], ':');
|
||||
sls::strcpy_safe(args[8], getDetectorIP2().hex().c_str());
|
||||
sls::strcpy_safe(args[9], getDetectorMAC2().hex().c_str());
|
||||
// sls::removeChar(args[9], ':');
|
||||
|
||||
// number of interfaces and which one
|
||||
snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces);
|
||||
@ -2038,85 +2040,75 @@ uint32_t slsDetector::clearBit(uint32_t addr, int n) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string slsDetector::setDetectorMAC(const std::string &detectorMAC) {
|
||||
// invalid format
|
||||
if ((detectorMAC.length() != 17) || (detectorMAC[2] != ':') || (detectorMAC[5] != ':') ||
|
||||
(detectorMAC[8] != ':') || (detectorMAC[11] != ':') || (detectorMAC[14] != ':')) {
|
||||
std::string slsDetector::setDetectorMAC(const std::string &address) {
|
||||
auto addr = MacAddr(address);
|
||||
if (addr == 0) {
|
||||
throw RuntimeError("server MAC Address should be in xx:xx:xx:xx:xx:xx format");
|
||||
}
|
||||
// valid format
|
||||
else {
|
||||
detector_shm()->detectorMAC = MacStringToUint(detectorMAC);
|
||||
} else {
|
||||
detector_shm()->detectorMAC = addr;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
}
|
||||
return getDetectorMAC();
|
||||
return getDetectorMAC().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getDetectorMAC() { return MacAddrToString(detector_shm()->detectorMAC); }
|
||||
MacAddr slsDetector::getDetectorMAC() { return detector_shm()->detectorMAC; }
|
||||
|
||||
std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) {
|
||||
// invalid format
|
||||
if ((detectorMAC.length() != 17) || (detectorMAC[2] != ':') || (detectorMAC[5] != ':') ||
|
||||
(detectorMAC[8] != ':') || (detectorMAC[11] != ':') || (detectorMAC[14] != ':')) {
|
||||
std::string slsDetector::setDetectorMAC2(const std::string &address) {
|
||||
auto addr = MacAddr(address);
|
||||
if (addr == 0) {
|
||||
throw RuntimeError("server MAC Address 2 should be in xx:xx:xx:xx:xx:xx format");
|
||||
}
|
||||
// valid format
|
||||
else {
|
||||
detector_shm()->detectorMAC2 = MacStringToUint(detectorMAC);
|
||||
} else {
|
||||
detector_shm()->detectorMAC2 = addr;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
}
|
||||
return getDetectorMAC2();
|
||||
return getDetectorMAC2().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getDetectorMAC2() { return MacAddrToString(detector_shm()->detectorMAC2); }
|
||||
MacAddr slsDetector::getDetectorMAC2() { return detector_shm()->detectorMAC2; }
|
||||
|
||||
std::string slsDetector::setDetectorIP(const std::string &detectorIP) {
|
||||
if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) {
|
||||
auto ip = IpStringToUint(detectorIP.c_str());
|
||||
if (ip == 0) {
|
||||
throw RuntimeError("setDetectorIP: IP Address should be VALID and "
|
||||
"in xxx.xxx.xxx.xxx format");
|
||||
} else {
|
||||
detector_shm()->detectorIP = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
std::string slsDetector::setDetectorIP(const std::string &ip) {
|
||||
auto addr = IpAddr(ip);
|
||||
if (addr != 0) {
|
||||
detector_shm()->detectorIP = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
} else {
|
||||
throw RuntimeError("setDetectorIP: IP Address should be VALID and "
|
||||
"in xxx.xxx.xxx.xxx format");
|
||||
}
|
||||
return getDetectorIP();
|
||||
return getDetectorIP().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getDetectorIP() const { return IpToString(detector_shm()->detectorIP); }
|
||||
IpAddr slsDetector::getDetectorIP() const { return detector_shm()->detectorIP; }
|
||||
|
||||
std::string slsDetector::setDetectorIP2(const std::string &detectorIP) {
|
||||
if (detectorIP.length() && detectorIP.length() < 16) {
|
||||
auto ip = IpStringToUint(detectorIP.c_str());
|
||||
if (ip == 0) {
|
||||
throw RuntimeError("setDetectorIP: IP Address 2 should be VALID "
|
||||
"and in xxx.xxx.xxx.xxx format");
|
||||
} else {
|
||||
detector_shm()->detectorIP2 = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
std::string slsDetector::setDetectorIP2(const std::string &ip) {
|
||||
auto addr = IpAddr(ip);
|
||||
if (addr != 0) {
|
||||
detector_shm()->detectorIP2 = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
} else {
|
||||
throw RuntimeError("setDetectorIP: IP2 Address should be VALID and "
|
||||
"in xxx.xxx.xxx.xxx format");
|
||||
}
|
||||
return getDetectorIP2();
|
||||
return getDetectorIP().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getDetectorIP2() const { return IpToString(detector_shm()->detectorIP2); }
|
||||
IpAddr slsDetector::getDetectorIP2() const { return detector_shm()->detectorIP2; }
|
||||
|
||||
std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
|
||||
FILE_LOG(logDEBUG1) << "Setting up Receiver with " << receiverIP;
|
||||
@ -2260,12 +2252,10 @@ std::string slsDetector::setReceiverUDPIP(const std::string &udpip) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return getReceiverUDPIP();
|
||||
return getReceiverUDPIP().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getReceiverUDPIP() const {
|
||||
return IpToString(detector_shm()->receiverUDPIP);
|
||||
}
|
||||
sls::IpAddr slsDetector::getReceiverUDPIP() const { return detector_shm()->receiverUDPIP; }
|
||||
|
||||
std::string slsDetector::setReceiverUDPIP2(const std::string &udpip) {
|
||||
if (udpip.length() && udpip.length() < 16) {
|
||||
@ -2282,12 +2272,10 @@ std::string slsDetector::setReceiverUDPIP2(const std::string &udpip) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return getReceiverUDPIP2();
|
||||
return getReceiverUDPIP2().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getReceiverUDPIP2() const {
|
||||
return IpToString(detector_shm()->receiverUDPIP2);
|
||||
}
|
||||
sls::IpAddr slsDetector::getReceiverUDPIP2() const { return detector_shm()->receiverUDPIP2; }
|
||||
|
||||
std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) {
|
||||
auto mac = MacStringToUint(udpmac);
|
||||
@ -2295,12 +2283,10 @@ std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) {
|
||||
throw ReceiverError("Could not decode UDPMAC from: " + udpmac);
|
||||
}
|
||||
detector_shm()->receiverUDPMAC = mac;
|
||||
return getReceiverUDPMAC();
|
||||
return getReceiverUDPMAC().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getReceiverUDPMAC() const {
|
||||
return MacAddrToString(detector_shm()->receiverUDPMAC);
|
||||
}
|
||||
MacAddr slsDetector::getReceiverUDPMAC() const { return detector_shm()->receiverUDPMAC; }
|
||||
|
||||
std::string slsDetector::setReceiverUDPMAC2(const std::string &udpmac) {
|
||||
auto mac = MacStringToUint(udpmac);
|
||||
@ -2308,12 +2294,10 @@ std::string slsDetector::setReceiverUDPMAC2(const std::string &udpmac) {
|
||||
throw ReceiverError("Could not decode UDPMA2C from: " + udpmac);
|
||||
}
|
||||
detector_shm()->receiverUDPMAC2 = mac;
|
||||
return getReceiverUDPMAC2();
|
||||
return getReceiverUDPMAC2().str();
|
||||
}
|
||||
|
||||
std::string slsDetector::getReceiverUDPMAC2() const {
|
||||
return MacAddrToString(detector_shm()->receiverUDPMAC2);
|
||||
}
|
||||
MacAddr slsDetector::getReceiverUDPMAC2() const { return detector_shm()->receiverUDPMAC2; }
|
||||
|
||||
int slsDetector::setReceiverUDPPort(int udpport) {
|
||||
detector_shm()->receiverUDPPort = udpport;
|
||||
@ -2679,8 +2663,8 @@ int slsDetector::setUDPConnection() {
|
||||
// copy arguments to args[][]
|
||||
snprintf(args[0], sizeof(args[0]), "%d", detector_shm()->numUDPInterfaces);
|
||||
snprintf(args[1], sizeof(args[1]), "%d", detector_shm()->selectedUDPInterface);
|
||||
sls::strcpy_safe(args[2], getReceiverUDPIP().c_str());
|
||||
sls::strcpy_safe(args[3], getReceiverUDPIP2().c_str());
|
||||
sls::strcpy_safe(args[2], getReceiverUDPIP().str());
|
||||
sls::strcpy_safe(args[3], getReceiverUDPIP2().str());
|
||||
snprintf(args[4], sizeof(args[4]), "%d", detector_shm()->receiverUDPPort);
|
||||
snprintf(args[5], sizeof(args[5]), "%d", detector_shm()->receiverUDPPort2);
|
||||
FILE_LOG(logDEBUG1) << "Receiver Number of UDP Interfaces: "
|
||||
|
@ -16,29 +16,35 @@ class IpAddr {
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
explicit IpAddr(uint32_t address);
|
||||
IpAddr(uint32_t address);
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
std::string str() const;
|
||||
std::string hex() const;
|
||||
bool operator==(const IpAddr &other) const { return addr_ == other.addr_; }
|
||||
bool operator!=(const IpAddr &other) const { return addr_ != other.addr_; }
|
||||
bool operator==(const uint32_t other) const { return addr_ == other; }
|
||||
bool operator!=(const uint32_t other) const { return addr_ != other; }
|
||||
};
|
||||
|
||||
class MacAddr {
|
||||
private:
|
||||
uint64_t addr_{0};
|
||||
std::string to_hex(const char delimiter = 0);
|
||||
std::string to_hex(const char delimiter = 0) const;
|
||||
|
||||
public:
|
||||
MacAddr(uint64_t mac);
|
||||
MacAddr(std::string mac);
|
||||
std::string str() { return to_hex(':'); }
|
||||
std::string hex() { return to_hex(); }
|
||||
MacAddr(const char *address);
|
||||
std::string str() const { return to_hex(':'); }
|
||||
std::string hex() const { return to_hex(); }
|
||||
bool operator==(const MacAddr &other) const { return addr_ == other.addr_; }
|
||||
bool operator!=(const MacAddr &other) const { return addr_ != other.addr_; }
|
||||
bool operator==(const uint64_t other) const { return addr_ == other; }
|
||||
bool operator!=(const uint64_t other) const { return addr_ != other; }
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
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)
|
||||
*/
|
||||
|
@ -17,6 +17,7 @@ namespace sls {
|
||||
|
||||
IpAddr::IpAddr(uint32_t address) : addr_{address} {}
|
||||
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);
|
||||
@ -31,6 +32,7 @@ std::string IpAddr::hex() const {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
MacAddr::MacAddr(uint64_t mac) : addr_{mac} {}
|
||||
MacAddr::MacAddr(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
@ -39,8 +41,9 @@ MacAddr::MacAddr(std::string mac) {
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::stol(mac, nullptr, 16);
|
||||
}
|
||||
MacAddr::MacAddr(const char *address) : MacAddr(std::string(address)) {}
|
||||
|
||||
std::string MacAddr::to_hex(const char delimiter) {
|
||||
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);
|
||||
@ -52,6 +55,14 @@ std::string MacAddr::to_hex(const char delimiter) {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
std::string MacAddrToString(uint64_t mac) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
#include "string_utils.h"
|
||||
#include "container_utils.h"
|
||||
#include "network_utils.h"
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
@ -38,6 +39,22 @@ std::string concatenateIfDifferent(const std::vector<std::string>& container)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
template std::string concatenateIfDifferent(const std::vector<IpAddr>&);
|
||||
template std::string concatenateIfDifferent(const std::vector<MacAddr>&);
|
||||
|
||||
|
||||
std::string stringIpToHex(const std::string& ip)
|
||||
{
|
||||
|
@ -21,6 +21,23 @@ TEST_CASE("Convert mac address") {
|
||||
}
|
||||
}
|
||||
|
||||
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") {
|
||||
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"};
|
||||
@ -35,6 +52,24 @@ TEST_CASE("Convert IP") {
|
||||
}
|
||||
}
|
||||
|
||||
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("IP not valid") {
|
||||
|
||||
CHECK(IpStringToUint("hej") == 0);
|
||||
|
@ -110,4 +110,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")
|
Loading…
x
Reference in New Issue
Block a user