From 990848554c139f9e106c4aa9d63493669742c671 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 28 Mar 2019 16:16:41 +0100 Subject: [PATCH 01/10] midpoint --- integrationTests/src/a.cpp | 5 +++- slsDetectorSoftware/include/slsDetector.h | 3 ++- slsDetectorSoftware/src/slsDetector.cpp | 33 +++++++++++++++-------- slsSupportLib/include/logger.h | 2 +- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index b37efdc19..8077f9968 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -41,6 +41,8 @@ int main() { // std::cout << "sizeof(result):" << sizeof(hints) << '\n'; // std::cout << "sizeof(serverAddr):" << sizeof(serverAddr) << '\n'; + uint64_t ip{0}; + int port = 1952; if (getaddrinfo(hostname.c_str(), NULL, &hints, &result) != 0) { @@ -54,9 +56,10 @@ int main() { memcpy(&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in_addr_t)); freeaddrinfo(result); + ip = serverAddr.sin_addr.s_addr; char address[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &serverAddr.sin_addr, address, INET_ADDRSTRLEN); + inet_ntop(AF_INET, &ip, address, INET_ADDRSTRLEN); std::cout << "ip of host is: " << address << '\n'; sls::ClientSocket(false, serverAddr); diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index 055454c41..8e3ccaacf 100644 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -163,7 +163,8 @@ struct sharedSlsDetector { char detectorMAC2[MAX_STR_LENGTH]; /** ip address of the detector **/ - char detectorIP[MAX_STR_LENGTH]; + // char detectorIP[MAX_STR_LENGTH]; + uint64_t detectorIP; /** ip address of the 2nd interface of the detector **/ char detectorIP2[MAX_STR_LENGTH]; diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index 01399d053..f93247d4d 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -323,7 +323,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) { sls::strcpy_safe(detector_shm()->receiverUDPMAC2, "none"); sls::strcpy_safe(detector_shm()->detectorMAC, DEFAULT_DET_MAC); sls::strcpy_safe(detector_shm()->detectorMAC2, DEFAULT_DET_MAC2); - sls::strcpy_safe(detector_shm()->detectorIP, DEFAULT_DET_IP); + // sls::strcpy_safe(detector_shm()->detectorIP, DEFAULT_DET_IP); sls::strcpy_safe(detector_shm()->detectorIP2, DEFAULT_DET_IP2); detector_shm()->numUDPInterfaces = 1; detector_shm()->selectedUDPInterface = 1; @@ -1614,7 +1614,12 @@ int slsDetector::configureMAC() { snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort); sls::strcpy_safe(args[1], detector_shm()->receiverUDPIP); sls::strcpy_safe(args[2], detector_shm()->receiverUDPMAC); - sls::strcpy_safe(args[3], detector_shm()->detectorIP); + + + char detectorIP[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &(detector_shm()->detectorIP), detectorIP, INET_ADDRSTRLEN); + + sls::strcpy_safe(args[3], detectorIP); sls::strcpy_safe(args[4], detector_shm()->detectorMAC); snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2); @@ -1711,10 +1716,10 @@ int slsDetector::configureMAC() { FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << detector_shm()->detectorMAC; } - if (strcasecmp(retvals[1], detector_shm()->detectorIP)) { + if (strcasecmp(retvals[1], detectorIP)) { // memset(detector_shm()->detectorIP, 0, MAX_STR_LENGTH); - sls::strcpy_safe(detector_shm()->detectorIP, retvals[1]); - FILE_LOG(logINFO) << detId << ": Detector IP updated to " << detector_shm()->detectorIP; + // sls::strcpy_safe(detectorIP, retvals[1]); + FILE_LOG(logINFO) << detId << ": Detector IP updated to " << detectorIP; } } if (ret == FORCE_UPDATE) { @@ -2133,14 +2138,16 @@ std::string slsDetector::getDetectorMAC2() { } std::string slsDetector::setDetectorIP(const std::string &detectorIP) { - struct sockaddr_in sa; - if (detectorIP.length() && detectorIP.length() < 16) { - int result = inet_pton(AF_INET, detectorIP.c_str(), &(sa.sin_addr)); + // struct sockaddr_in sa; + uint64_t ip{0}; + if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) { + int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); if (result == 0) { throw RuntimeError( "setDetectorIP: IP Address should be VALID and in xxx.xxx.xxx.xxx format"); } else { - sls::strcpy_safe(detector_shm()->detectorIP, detectorIP.c_str()); + // sls::strcpy_safe(detector_shm()->detectorIP, detectorIP.c_str()); + detector_shm()->detectorIP = ip; if (!strcmp(detector_shm()->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } else if (setUDPConnection() == FAIL) { @@ -2148,11 +2155,15 @@ std::string slsDetector::setDetectorIP(const std::string &detectorIP) { } } } - return std::string(detector_shm()->detectorIP); + char address[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &detector_shm()->detectorIP, address, INET_ADDRSTRLEN); + return address; } std::string slsDetector::getDetectorIP() const { - return std::string(detector_shm()->detectorIP); + char address[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &detector_shm()->detectorIP, address, INET_ADDRSTRLEN); + return address; } std::string slsDetector::setDetectorIP2(const std::string &detectorIP) { diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index 2be26378a..8506fb076 100644 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -17,7 +17,7 @@ #endif #ifndef FILELOG_MAX_LEVEL -#define FILELOG_MAX_LEVEL logINFO +#define FILELOG_MAX_LEVEL logDEBUG5 #endif From d134e54810cb0c691b32963ee4c90863adefa1ad Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Fri, 29 Mar 2019 10:18:10 +0100 Subject: [PATCH 02/10] ip --- integrationTests/src/a.cpp | 72 +++++----- slsDetectorSoftware/include/slsDetector.h | 5 +- slsDetectorSoftware/src/slsDetector.cpp | 166 +++++++++------------- slsSupportLib/include/logger.h | 2 +- 4 files changed, 110 insertions(+), 135 deletions(-) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index 8077f9968..b4d9f8a72 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -11,58 +11,66 @@ #include #include +#include #include #include -#include #include #include #define VERBOSE +using sls::DetectorError; using sls::RuntimeError; using sls::SharedMemoryError; using sls::SocketError; -using sls::DetectorError; int main() { + std::cout << "size: " <> hostname; - std::string hostname; - std::cout << "Enter hostname: "; - std::cin >> hostname; + // struct addrinfo hints, *result; + // memset(&hints, 0, sizeof(hints)); + // hints.ai_family = AF_INET; + // hints.ai_socktype = SOCK_STREAM; + // hints.ai_flags |= AI_CANONNAME; - struct addrinfo hints, *result; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags |= AI_CANONNAME; + // struct sockaddr_in serverAddr {}; + // // std::cout << "sizeof(result):" << sizeof(hints) << '\n'; + // // std::cout << "sizeof(serverAddr):" << sizeof(serverAddr) << '\n'; - struct sockaddr_in serverAddr {}; - // std::cout << "sizeof(result):" << sizeof(hints) << '\n'; - // std::cout << "sizeof(serverAddr):" << sizeof(serverAddr) << '\n'; + // uint32_t ip{0}; - uint64_t ip{0}; + // int port = 1952; - int port = 1952; + // if (getaddrinfo(hostname.c_str(), NULL, &hints, &result) != 0) { + // std::string msg = "ClientSocket cannot decode host:" + hostname + " on port " + + // std::to_string(port) + "\n"; + // throw 5; + // } - if (getaddrinfo(hostname.c_str(), NULL, &hints, &result) != 0) { - std::string msg = "ClientSocket cannot decode host:" + hostname + " on port " + - std::to_string(port) + "\n"; - throw 5; - } + // serverAddr.sin_family = AF_INET; + // serverAddr.sin_port = htons(port); + // memcpy(&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, + // sizeof(in_addr_t)); + // freeaddrinfo(result); + // ip = serverAddr.sin_addr.s_addr; - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(port); - memcpy(&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, - sizeof(in_addr_t)); - freeaddrinfo(result); - ip = serverAddr.sin_addr.s_addr; + // char address[INET_ADDRSTRLEN]; + // inet_ntop(AF_INET, &ip, address, INET_ADDRSTRLEN); + // std::cout << "ip of host is: " << address << '\n'; - char address[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &ip, address, INET_ADDRSTRLEN); - std::cout << "ip of host is: " << address << '\n'; + // // hints.ai_addr = reinterpret_cast(&serverAddr); - sls::ClientSocket(false, serverAddr); + // // if (getaddrinfo("NULL", NULL, &hints, &result) != 0) { + // // std::string msg = "ClientSocket cannot decode host:" + hostname + " on port " + + // // std::to_string(port) + "\n"; + // // throw 5; + // // } + + // sls::ClientSocket(false, serverAddr); // const std::string hostname = "beb083"; // auto type = slsDetector::getTypeFromDetector(hostname); @@ -79,9 +87,6 @@ int main() { // std::cout << "hostname: " << d.getHostname() << '\n'; // std::cout << "exptime: " << d.setDAC(-1, slsDetectorDefs::E_Vrf, 0) << '\n'; - - - // slsDetector d2(type); // std::cout << "Online: " << d2.getOnlineFlag() << '\n'; // d2.setHostname("beb55555"); @@ -131,7 +136,6 @@ int main() { // std::cout << "Caught Something else probably should have let me crash\n"; // } - // throw RuntimeError("This one we missed"); return 0; } diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index 8e3ccaacf..631e46eec 100644 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -163,11 +163,10 @@ struct sharedSlsDetector { char detectorMAC2[MAX_STR_LENGTH]; /** ip address of the detector **/ - // char detectorIP[MAX_STR_LENGTH]; - uint64_t detectorIP; + uint32_t detectorIP; /** ip address of the 2nd interface of the detector **/ - char detectorIP2[MAX_STR_LENGTH]; + uint32_t detectorIP2; /** number of udp interface */ int numUDPInterfaces; diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index f93247d4d..dad6eaede 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -323,8 +323,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) { sls::strcpy_safe(detector_shm()->receiverUDPMAC2, "none"); sls::strcpy_safe(detector_shm()->detectorMAC, DEFAULT_DET_MAC); sls::strcpy_safe(detector_shm()->detectorMAC2, DEFAULT_DET_MAC2); - // sls::strcpy_safe(detector_shm()->detectorIP, DEFAULT_DET_IP); - sls::strcpy_safe(detector_shm()->detectorIP2, DEFAULT_DET_IP2); + inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP)); + inet_pton(AF_INET, DEFAULT_DET_IP2, &(detector_shm()->detectorIP2)); detector_shm()->numUDPInterfaces = 1; detector_shm()->selectedUDPInterface = 1; detector_shm()->receiverOnlineFlag = OFFLINE_FLAG; @@ -1599,33 +1599,24 @@ int slsDetector::configureMAC() { // validate for the second interface if (detector_shm()->numUDPInterfaces == 2) { // copy from udpip (done here as well if udpconnection avoided (no slsrxr) - if (!strcmp(detector_shm()->receiverUDPIP2, "none")) { - sls::strcpy_safe(detector_shm()->receiverUDPIP2, detector_shm()->receiverUDPIP); - } - // rx_udpmac2 (udpip2 will be copied from udpip if empty) + if (!strcmp(detector_shm()->receiverUDPIP2, "none")) { + sls::strcpy_safe(detector_shm()->receiverUDPIP2, detector_shm()->receiverUDPIP); + } + // rx_udpmac2 (udpip2 will be copied from udpip if empty) if (!strcmp(detector_shm()->receiverUDPMAC2, "none")) { - throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set"); - } - FILE_LOG(logDEBUG1) << "rx_udpmac2 are valid "; + throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set"); + } + FILE_LOG(logDEBUG1) << "rx_udpmac2 are valid "; } - // copy to args snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort); sls::strcpy_safe(args[1], detector_shm()->receiverUDPIP); sls::strcpy_safe(args[2], detector_shm()->receiverUDPMAC); - - - char detectorIP[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &(detector_shm()->detectorIP), detectorIP, INET_ADDRSTRLEN); - - sls::strcpy_safe(args[3], detectorIP); sls::strcpy_safe(args[4], detector_shm()->detectorMAC); - snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2); sls::strcpy_safe(args[6], detector_shm()->receiverUDPIP2); sls::strcpy_safe(args[7], detector_shm()->receiverUDPMAC2); - sls::strcpy_safe(args[8], detector_shm()->detectorIP2); sls::strcpy_safe(args[9], detector_shm()->detectorMAC2); // number of interfaces and which one @@ -1646,7 +1637,6 @@ int slsDetector::configureMAC() { snprintf(args[13], array_size, "%x", pos[1]); } - FILE_LOG(logDEBUG1) << "receiver udp port:" << std::dec << args[0] << "-"; // converting receiverUDPIP to string hex sls::strcpy_safe(args[1], sls::stringIpToHex(args[1]).c_str()); @@ -1655,42 +1645,36 @@ int slsDetector::configureMAC() { sls::removeChar(args[2], ':'); FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[2] << "-"; // converting detectorIP to string hex - sls::strcpy_safe(args[3], sls::stringIpToHex(args[3]).c_str()); + sls::strcpy_safe(args[3], sls::stringIpToHex(getDetectorIP()).c_str()); FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[3] << "-"; // MAC already in hex removing : sls::removeChar(args[4], ':'); FILE_LOG(logDEBUG1) << "detector udp mac:" << args[4] << "-"; - FILE_LOG(logDEBUG1) << "receiver udp port2:" << std::dec << args[5] << "-"; if (!strcmp(args[6], "none")) { - sprintf(args[6], "%d", 0); + sprintf(args[6], "%d", 0); } else { - sls::strcpy_safe(args[6], sls::stringIpToHex(args[6]).c_str()); + sls::strcpy_safe(args[6], sls::stringIpToHex(args[6]).c_str()); } FILE_LOG(logDEBUG1) << "receiver udp ip2:" << args[6] << "-"; // MAC already in hex removing : sls::removeChar(args[7], ':'); FILE_LOG(logDEBUG1) << "receiver udp mac2:" << args[7] << "-"; // converting detectorIP to string hex - if (!strcmp(args[8], "none")) { - sprintf(args[8], "%d", 0); - } else { - sls::strcpy_safe(args[8], sls::stringIpToHex(args[8]).c_str()); - } + sls::strcpy_safe(args[8], sls::stringIpToHex(getDetectorIP2()).c_str()); + FILE_LOG(logDEBUG1) << "detecotor udp ip2:" << args[8] << "-"; // MAC already in hex removing : sls::removeChar(args[9], ':'); FILE_LOG(logDEBUG1) << "detector udp mac2:" << args[9] << "-"; - FILE_LOG(logDEBUG1) << "number of udp interfaces:" << std::dec << args[10] << "-"; FILE_LOG(logDEBUG1) << "selected udp interface:" << std::dec << args[11] << "-"; FILE_LOG(logDEBUG1) << "row:" << args[12] << "-"; FILE_LOG(logDEBUG1) << "col:" << args[13] << "-"; - // send to server if (detector_shm()->onlineFlag == ONLINE_FLAG) { auto client = DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); @@ -1700,6 +1684,8 @@ int slsDetector::configureMAC() { uint32_t idetectorip = 0; sscanf(retvals[0], "%lx", &idetectormac); sscanf(retvals[1], "%x", &idetectorip); + idetectorip = __builtin_bswap32(idetectorip); + snprintf(retvals[0], sizeof(retvals[0]), "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned int)((idetectormac >> 40) & 0xFF), (unsigned int)((idetectormac >> 32) & 0xFF), @@ -1707,19 +1693,20 @@ int slsDetector::configureMAC() { (unsigned int)((idetectormac >> 16) & 0xFF), (unsigned int)((idetectormac >> 8) & 0xFF), (unsigned int)((idetectormac >> 0) & 0xFF)); - snprintf(retvals[1], sizeof(retvals[1]), "%d.%d.%d.%d", (idetectorip >> 24) & 0xff, - (idetectorip >> 16) & 0xff, (idetectorip >> 8) & 0xff, (idetectorip)&0xff); + + // update if different if (strcasecmp(retvals[0], detector_shm()->detectorMAC)) { - // memset(detector_shm()->detectorMAC, 0, MAX_STR_LENGTH); + memset(detector_shm()->detectorMAC, 0, MAX_STR_LENGTH); sls::strcpy_safe(detector_shm()->detectorMAC, retvals[0]); FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << detector_shm()->detectorMAC; } - if (strcasecmp(retvals[1], detectorIP)) { - // memset(detector_shm()->detectorIP, 0, MAX_STR_LENGTH); - // sls::strcpy_safe(detectorIP, retvals[1]); - FILE_LOG(logINFO) << detId << ": Detector IP updated to " << detectorIP; + + if (detector_shm()->detectorIP != idetectorip) { + std::cout << "idetectorip: " << idetectorip << '\n'; + detector_shm()->detectorIP = idetectorip; + FILE_LOG(logINFO) << detId << ": Detector IP updated to " << getDetectorIP(); } } if (ret == FORCE_UPDATE) { @@ -2111,9 +2098,7 @@ std::string slsDetector::setDetectorMAC(const std::string &detectorMAC) { return std::string(detector_shm()->detectorMAC); } -std::string slsDetector::getDetectorMAC() { - return std::string(detector_shm()->detectorMAC); -} +std::string slsDetector::getDetectorMAC() { return std::string(detector_shm()->detectorMAC); } std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) { // invalid format @@ -2133,20 +2118,17 @@ std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) { return std::string(detector_shm()->detectorMAC2); } -std::string slsDetector::getDetectorMAC2() { - return std::string(detector_shm()->detectorMAC2); -} +std::string slsDetector::getDetectorMAC2() { return std::string(detector_shm()->detectorMAC2); } std::string slsDetector::setDetectorIP(const std::string &detectorIP) { // struct sockaddr_in sa; - uint64_t ip{0}; + uint32_t ip{0}; if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) { int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); if (result == 0) { throw RuntimeError( "setDetectorIP: IP Address should be VALID and in xxx.xxx.xxx.xxx format"); } else { - // sls::strcpy_safe(detector_shm()->detectorIP, detectorIP.c_str()); detector_shm()->detectorIP = ip; if (!strcmp(detector_shm()->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; @@ -2155,26 +2137,24 @@ std::string slsDetector::setDetectorIP(const std::string &detectorIP) { } } } - char address[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &detector_shm()->detectorIP, address, INET_ADDRSTRLEN); - return address; + return getDetectorIP(); } std::string slsDetector::getDetectorIP() const { - char address[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &detector_shm()->detectorIP, address, INET_ADDRSTRLEN); - return address; + char ipstring[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &detector_shm()->detectorIP, ipstring, INET_ADDRSTRLEN); + return ipstring; } std::string slsDetector::setDetectorIP2(const std::string &detectorIP) { - struct sockaddr_in sa; + uint32_t ip{0}; if (detectorIP.length() && detectorIP.length() < 16) { - int result = inet_pton(AF_INET, detectorIP.c_str(), &(sa.sin_addr)); + int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); if (result == 0) { throw RuntimeError( "setDetectorIP: IP Address 2 should be VALID and in xxx.xxx.xxx.xxx format"); } else { - sls::strcpy_safe(detector_shm()->detectorIP2, detectorIP.c_str()); + detector_shm()->detectorIP2 = ip; if (!strcmp(detector_shm()->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } else if (setUDPConnection() == FAIL) { @@ -2182,11 +2162,13 @@ std::string slsDetector::setDetectorIP2(const std::string &detectorIP) { } } } - return std::string(detector_shm()->detectorIP2); + return getDetectorIP2(); } std::string slsDetector::getDetectorIP2() const { - return std::string(detector_shm()->detectorIP2); + char ipstring[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &detector_shm()->detectorIP2, ipstring, INET_ADDRSTRLEN); + return ipstring; } std::string slsDetector::setReceiverHostname(const std::string &receiverIP) { @@ -2417,9 +2399,7 @@ int slsDetector::setReceiverUDPPort(int udpport) { return detector_shm()->receiverUDPPort; } -int slsDetector::getReceiverUDPPort() const { - return detector_shm()->receiverUDPPort; -} +int slsDetector::getReceiverUDPPort() const { return detector_shm()->receiverUDPPort; } int slsDetector::setReceiverUDPPort2(int udpport) { detector_shm()->receiverUDPPort2 = udpport; @@ -2431,43 +2411,37 @@ int slsDetector::setReceiverUDPPort2(int udpport) { return detector_shm()->receiverUDPPort2; } -int slsDetector::getReceiverUDPPort2() const { - return detector_shm()->receiverUDPPort2; -} +int slsDetector::getReceiverUDPPort2() const { return detector_shm()->receiverUDPPort2; } int slsDetector::setNumberofUDPInterfaces(int n) { - if (detector_shm()->myDetectorType != JUNGFRAU) { - throw RuntimeError("Cannot choose number of interfaces for this detector"); - } - detector_shm()->numUDPInterfaces = (n > 1 ? 2 : 1); + if (detector_shm()->myDetectorType != JUNGFRAU) { + throw RuntimeError("Cannot choose number of interfaces for this detector"); + } + detector_shm()->numUDPInterfaces = (n > 1 ? 2 : 1); 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 detector_shm()->numUDPInterfaces; + return detector_shm()->numUDPInterfaces; } -int slsDetector::getNumberofUDPInterfaces() const { - return detector_shm()->numUDPInterfaces; -} +int slsDetector::getNumberofUDPInterfaces() const { return detector_shm()->numUDPInterfaces; } int slsDetector::selectUDPInterface(int n) { - if (detector_shm()->myDetectorType != JUNGFRAU) { - throw RuntimeError("Cannot select an interface for this detector"); - } - detector_shm()->selectedUDPInterface = (n > 1 ? 2 : 1); + if (detector_shm()->myDetectorType != JUNGFRAU) { + throw RuntimeError("Cannot select an interface for this detector"); + } + detector_shm()->selectedUDPInterface = (n > 1 ? 2 : 1); 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 detector_shm()->selectedUDPInterface; + return detector_shm()->selectedUDPInterface; } -int slsDetector::getSelectedUDPInterface() const { - return detector_shm()->selectedUDPInterface; -} +int slsDetector::getSelectedUDPInterface() const { return detector_shm()->selectedUDPInterface; } void slsDetector::setClientStreamingPort(int port) { detector_shm()->zmqport = port; } @@ -2781,10 +2755,9 @@ int slsDetector::setUDPConnection() { // jungfrau 2 interfaces or (1 interface and 2nd interface), copy udpip if udpip2 empty if (detector_shm()->numUDPInterfaces == 2 || detector_shm()->selectedUDPInterface == 2) { // copy from udpip - if (!strcmp(detector_shm()->receiverUDPIP2, "none")) { - sls::strcpy_safe(detector_shm()->receiverUDPIP2, detector_shm()->receiverUDPIP); - } - + if (!strcmp(detector_shm()->receiverUDPIP2, "none")) { + sls::strcpy_safe(detector_shm()->receiverUDPIP2, detector_shm()->receiverUDPIP); + } } // copy arguments to args[][] @@ -2794,7 +2767,8 @@ int slsDetector::setUDPConnection() { sls::strcpy_safe(args[3], detector_shm()->receiverUDPIP2); 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: " << detector_shm()->numUDPInterfaces; + FILE_LOG(logDEBUG1) << "Receiver Number of UDP Interfaces: " + << detector_shm()->numUDPInterfaces; FILE_LOG(logDEBUG1) << "Receiver Selected Interface: " << detector_shm()->selectedUDPInterface; FILE_LOG(logDEBUG1) << "Receiver udp ip address: " << detector_shm()->receiverUDPIP; FILE_LOG(logDEBUG1) << "Receiver udp ip address2: " << detector_shm()->receiverUDPIP2; @@ -2806,14 +2780,14 @@ int slsDetector::setUDPConnection() { ReceiverSocket(detector_shm()->receiver_hostname, detector_shm()->receiverTCPPort); ret = receiver.sendCommandThenRead(fnum, args, sizeof(args), retvals, sizeof(retvals)); if (strlen(retvals[0])) { - FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0]; - memset(detector_shm()->receiverUDPMAC, 0, MAX_STR_LENGTH); - sls::strcpy_safe(detector_shm()->receiverUDPMAC, retvals[0]); + FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0]; + memset(detector_shm()->receiverUDPMAC, 0, MAX_STR_LENGTH); + sls::strcpy_safe(detector_shm()->receiverUDPMAC, retvals[0]); } if (strlen(retvals[1])) { - FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1]; - memset(detector_shm()->receiverUDPMAC2, 0, MAX_STR_LENGTH); - sls::strcpy_safe(detector_shm()->receiverUDPMAC2, retvals[1]); + FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1]; + memset(detector_shm()->receiverUDPMAC2, 0, MAX_STR_LENGTH); + sls::strcpy_safe(detector_shm()->receiverUDPMAC2, retvals[1]); } if (ret == FORCE_UPDATE) { receiver.close(); @@ -3777,14 +3751,12 @@ void slsDetector::printReceiverConfiguration(TLogLevel level) { FILE_LOG(level) << "#Detector " << detId << ":\n Receiver Hostname:\t" << getReceiverHostname() << "\nDetector UDP IP (Source):\t\t" << getDetectorIP() << "\nDetector UDP IP2 (Source):\t\t" << getDetectorIP2() - << "\nDetector UDP MAC:\t\t" << getDetectorMAC() - << "\nDetector UDP MAC2:\t\t" << getDetectorMAC2() - << "\nReceiver UDP IP:\t" << getReceiverUDPIP() - << "\nReceiver UDP IP2:\t" << getReceiverUDPIP2() - << "\nReceiver UDP MAC:\t" << getReceiverUDPMAC() - << "\nReceiver UDP MAC2:\t" << getReceiverUDPMAC2() - << "\nReceiver UDP Port:\t" << getReceiverUDPPort() - << "\nReceiver UDP Port2:\t"<< getReceiverUDPPort2(); + << "\nDetector UDP MAC:\t\t" << getDetectorMAC() << "\nDetector UDP MAC2:\t\t" + << getDetectorMAC2() << "\nReceiver UDP IP:\t" << getReceiverUDPIP() + << "\nReceiver UDP IP2:\t" << getReceiverUDPIP2() << "\nReceiver UDP MAC:\t" + << getReceiverUDPMAC() << "\nReceiver UDP MAC2:\t" << getReceiverUDPMAC2() + << "\nReceiver UDP Port:\t" << getReceiverUDPPort() << "\nReceiver UDP Port2:\t" + << getReceiverUDPPort2(); } int slsDetector::setReceiverOnline(int value) { diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index 8506fb076..6f1dcb8f1 100644 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -17,7 +17,7 @@ #endif #ifndef FILELOG_MAX_LEVEL -#define FILELOG_MAX_LEVEL logDEBUG5 +#define FILELOG_MAX_LEVEL logDEBUG1 #endif From 05709e2f475c0d8773cd914420b4e2a7c0f2c085 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Fri, 29 Mar 2019 16:29:00 +0100 Subject: [PATCH 03/10] finished --- slsDetectorSoftware/include/slsDetector.h | 16 +- slsDetectorSoftware/src/slsDetector.cpp | 347 +++++++++------------ slsSupportLib/CMakeLists.txt | 2 + slsSupportLib/include/logger.h | 2 +- slsSupportLib/include/network_utils.h | 14 + slsSupportLib/src/DataSocket.cpp | 2 +- slsSupportLib/src/network_utils.cpp | 69 ++++ slsSupportLib/tests/CMakeLists.txt | 1 + slsSupportLib/tests/test-network_utils.cpp | 44 +++ 9 files changed, 287 insertions(+), 210 deletions(-) create mode 100644 slsSupportLib/include/network_utils.h create mode 100644 slsSupportLib/src/network_utils.cpp create mode 100644 slsSupportLib/tests/test-network_utils.cpp diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index 631e46eec..f1bfabbc2 100644 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -109,10 +109,6 @@ struct sharedSlsDetector { /** size of the data that are transfered from the detector */ int dataBytes; - /** threaded processing flag - * (i.e. if data are processed in a separate thread) */ - // int threadedProcessing; - /** number of rois defined */ int nROI; @@ -145,22 +141,22 @@ struct sharedSlsDetector { int receiverUDPPort2; /** ip address of the receiver for the detector to send packets to**/ - char receiverUDPIP[MAX_STR_LENGTH]; + uint32_t receiverUDPIP; /** ip address of the receiver for the 2nd interface of the detector to send packets to**/ - char receiverUDPIP2[MAX_STR_LENGTH]; + uint32_t receiverUDPIP2; /** mac address of receiver for the detector to send packets to **/ - char receiverUDPMAC[MAX_STR_LENGTH]; + uint64_t receiverUDPMAC; /** mac address of receiver for the 2nd interface of the detector to send packets to **/ - char receiverUDPMAC2[MAX_STR_LENGTH]; + uint64_t receiverUDPMAC2; /** mac address of the detector **/ - char detectorMAC[MAX_STR_LENGTH]; + uint64_t detectorMAC; /** mac address of the 2nd interface of the detector **/ - char detectorMAC2[MAX_STR_LENGTH]; + uint64_t detectorMAC2; /** ip address of the detector **/ uint32_t detectorIP; diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index dad6eaede..90b401de9 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -7,11 +7,11 @@ #include "file_utils.h" #include "gitInfoLib.h" #include "multiSlsDetector.h" +#include "network_utils.h" #include "slsDetectorCommand.h" #include "sls_detector_exceptions.h" #include "string_utils.h" #include "versionAPI.h" - #include #include #include @@ -24,14 +24,7 @@ #include #include -using sls::DetectorError; -using sls::DetectorSocket; -using sls::NotImplementedError; -using sls::ReceiverError; -using sls::ReceiverSocket; -using sls::RuntimeError; -using sls::SharedMemory; -using sls::SharedMemoryError; +using namespace sls; #define DEFAULT_HOSTNAME "localhost" @@ -317,14 +310,18 @@ void slsDetector::initializeDetectorStructure(detectorType type) { detector_shm()->receiverTCPPort = DEFAULT_PORTNO + 2; detector_shm()->receiverUDPPort = DEFAULT_UDP_PORTNO; detector_shm()->receiverUDPPort2 = DEFAULT_UDP_PORTNO + 1; - sls::strcpy_safe(detector_shm()->receiverUDPIP, "none"); - sls::strcpy_safe(detector_shm()->receiverUDPIP2, "none"); - sls::strcpy_safe(detector_shm()->receiverUDPMAC, "none"); - sls::strcpy_safe(detector_shm()->receiverUDPMAC2, "none"); - sls::strcpy_safe(detector_shm()->detectorMAC, DEFAULT_DET_MAC); - sls::strcpy_safe(detector_shm()->detectorMAC2, DEFAULT_DET_MAC2); + + detector_shm()->receiverUDPIP = 0u; + detector_shm()->receiverUDPIP2 = 0u; + detector_shm()->receiverUDPMAC = 0u; + detector_shm()->receiverUDPMAC2 = 0u; + + detector_shm()->detectorMAC = MacStringToUint(DEFAULT_DET_MAC); + detector_shm()->detectorMAC2 = MacStringToUint(DEFAULT_DET_MAC2); + inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP)); inet_pton(AF_INET, DEFAULT_DET_IP2, &(detector_shm()->detectorIP2)); + detector_shm()->numUDPInterfaces = 1; detector_shm()->selectedUDPInterface = 1; detector_shm()->receiverOnlineFlag = OFFLINE_FLAG; @@ -401,7 +398,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) { detector_shm()->gappixels * detector_shm()->nGappixels[Y]) * detector_shm()->dynamicRange / 8; - // update #nchans and databytes, as it depends on #samples, roi, readoutflags (ctb only) + // update #nchans and databytes, as it depends on #samples, roi, + // readoutflags (ctb only) if (detector_shm()->myDetectorType == CHIPTESTBOARD || detector_shm()->myDetectorType == MOENCH) { updateTotalNumberOfChannels(); @@ -519,9 +517,9 @@ int slsDetector::receiveModule(sls_detector_module *myMod, sls::ClientSocket &cl slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multi_id, bool verify) { if (!detector_shm.IsExisting()) { - throw SharedMemoryError( - "Shared memory " + detector_shm.GetName() + - "does not exist.\n Corrupted Multi Shared memory. Please free shared memory."); + throw SharedMemoryError("Shared memory " + detector_shm.GetName() + + "does not exist.\n Corrupted Multi Shared " + "memory. Please free shared memory."); } detector_shm.OpenSharedMemory(); @@ -959,7 +957,8 @@ int slsDetector::updateDetectorNoWait(sls::ClientSocket &client) { } } - // update #nchans and databytes, as it depends on #samples, roi, readoutflags (ctb only) + // update #nchans and databytes, as it depends on #samples, roi, + // readoutflags (ctb only) if (detector_shm()->myDetectorType == CHIPTESTBOARD || detector_shm()->myDetectorType == MOENCH) { updateTotalNumberOfChannels(); @@ -1132,7 +1131,8 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings(detectorSettings iset } } - // others: send only the settings, detector server will update dac values already in server + // others: send only the settings, detector server will update dac values + // already in server return sendSettingsOnly(isettings); } @@ -1156,7 +1156,8 @@ slsDetectorDefs::detectorSettings slsDetector::sendSettingsOnly(detectorSettings } int slsDetector::getThresholdEnergy() { - // moench - get threshold energy from processor (due to different clients, diff shm) + // moench - get threshold energy from processor (due to different clients, + // diff shm) if (detector_shm()->myDetectorType == MOENCH) { // get json from rxr, parse for threshold and update shm getAdditionalJsonHeader(); @@ -1273,7 +1274,8 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, detectorSettings isetti FILE_LOG(logDEBUG1) << "Settings File is " << settingsfname; // read the files - // myMod = createModule(); // readSettings also checks if create module is null + // myMod = createModule(); // readSettings also checks if create module + // is null if (nullptr == readSettingsFile(settingsfname, myMod, tb)) { if (myMod) { deleteModule(myMod); @@ -1323,8 +1325,8 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, detectorSettings isetti if (myMod1->iodelay != myMod2->iodelay) { deleteModule(myMod1); deleteModule(myMod2); - throw RuntimeError( - "setThresholdEnergyAndSettings: Iodelays do not match between files"); + throw RuntimeError("setThresholdEnergyAndSettings: Iodelays do not " + "match between files"); } // interpolate module @@ -1332,7 +1334,8 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, detectorSettings isetti if (myMod == nullptr) { deleteModule(myMod1); deleteModule(myMod2); - throw RuntimeError("setThresholdEnergyAndSettings: Could not interpolate, different " + throw RuntimeError("setThresholdEnergyAndSettings: Could not " + "interpolate, different " "dac values in files"); } // interpolate tau @@ -1349,7 +1352,8 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, detectorSettings isetti setModule(*myMod, tb); deleteModule(myMod); if (getSettings() != is) { - throw RuntimeError("setThresholdEnergyAndSettings: Could not set settings in detector"); + throw RuntimeError("setThresholdEnergyAndSettings: Could not set " + "settings in detector"); } return OK; } @@ -1516,7 +1520,8 @@ int slsDetector::startAndReadAll() { if (detector_shm()->onlineFlag == ONLINE_FLAG) { auto client = DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); - // TODO! how to we hande this? ret == FAIL --> detector_shm()->stoppedFlag = 1; + // TODO! how to we hande this? ret == FAIL --> + // detector_shm()->stoppedFlag = 1; FILE_LOG(logDEBUG1) << "Detector successfully finished acquisition"; } if (ret == FORCE_UPDATE) { @@ -1548,7 +1553,8 @@ int slsDetector::readAll() { if (detector_shm()->onlineFlag == ONLINE_FLAG) { auto client = DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); ret = client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0); - // TODO! how to we hande this? ret == FAIL --> detector_shm()->stoppedFlag = 1; + // TODO! how to we hande this? ret == FAIL --> + // detector_shm()->stoppedFlag = 1; FILE_LOG(logDEBUG1) << "Detector successfully finished reading all frames"; } if (ret == FORCE_UPDATE) { @@ -1566,31 +1572,15 @@ int slsDetector::configureMAC() { char args[n_args][array_size] = {}; char retvals[n_retvals][array_size] = {}; FILE_LOG(logDEBUG1) << "Configuring MAC"; - - // if rx_udpip is none - if (!(strcmp(detector_shm()->receiverUDPIP, "none"))) { - // hostname is an ip address - if (strchr(detector_shm()->receiver_hostname, '.') != nullptr) { - sls::strcpy_safe(detector_shm()->receiverUDPIP, detector_shm()->receiver_hostname); - // if hostname not ip, convert it to ip - } else { - struct addrinfo *result; - if (sls::ConvertHostnameToInternetAddress(detector_shm()->receiver_hostname, &result) == - 0) { - // on success - memset(detector_shm()->receiverUDPIP, 0, MAX_STR_LENGTH); - // on failure, back to none - if (sls::ConvertInternetAddresstoIpString(result, detector_shm()->receiverUDPIP, - MAX_STR_LENGTH)) { - sls::strcpy_safe(detector_shm()->receiverUDPIP, "none"); - throw RuntimeError("configureMAC: Error. Receiver UDP IP Address not set"); - } - } + if (detector_shm()->receiverUDPIP == 0) { + // Hostname could be ip try to decode otherwise look up the hostname + detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname); + if (detector_shm()->receiverUDPIP == 0) { + detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname); } } - // rx_udpmac is none - if (!strcmp(detector_shm()->receiverUDPMAC, "none")) { + if (detector_shm()->receiverUDPMAC == 0) { throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses not set"); } FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpmac are valid "; @@ -1598,26 +1588,23 @@ int slsDetector::configureMAC() { // jungfrau 2 interfaces // validate for the second interface if (detector_shm()->numUDPInterfaces == 2) { - // copy from udpip (done here as well if udpconnection avoided (no slsrxr) - if (!strcmp(detector_shm()->receiverUDPIP2, "none")) { - sls::strcpy_safe(detector_shm()->receiverUDPIP2, detector_shm()->receiverUDPIP); + // copy from udpip (done here as well if udpconnection avoided (no + // slsrxr) + if (detector_shm()->receiverUDPIP2 == 0) { + detector_shm()->receiverUDPIP2 = detector_shm()->receiverUDPIP; } - // rx_udpmac2 (udpip2 will be copied from udpip if empty) - if (!strcmp(detector_shm()->receiverUDPMAC2, "none")) { - throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set"); - } - FILE_LOG(logDEBUG1) << "rx_udpmac2 are valid "; + FILE_LOG(logDEBUG1) << "rx_udpmac2 is valid "; } // copy to args snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort); - sls::strcpy_safe(args[1], detector_shm()->receiverUDPIP); - sls::strcpy_safe(args[2], detector_shm()->receiverUDPMAC); - sls::strcpy_safe(args[4], detector_shm()->detectorMAC); + sls::strcpy_safe(args[1], getReceiverUDPIP().c_str()); + sls::strcpy_safe(args[2], getReceiverUDPMAC().c_str()); + sls::strcpy_safe(args[4], getDetectorMAC().c_str()); snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2); - sls::strcpy_safe(args[6], detector_shm()->receiverUDPIP2); - sls::strcpy_safe(args[7], detector_shm()->receiverUDPMAC2); - sls::strcpy_safe(args[9], detector_shm()->detectorMAC2); + sls::strcpy_safe(args[6], getReceiverUDPIP2().c_str()); + sls::strcpy_safe(args[7], getReceiverUDPMAC2().c_str()); + sls::strcpy_safe(args[9], getDetectorMAC2().c_str()); // number of interfaces and which one snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces); @@ -1679,34 +1666,22 @@ int slsDetector::configureMAC() { if (detector_shm()->onlineFlag == ONLINE_FLAG) { auto client = DetectorSocket(detector_shm()->hostname, detector_shm()->controlPort); ret = client.sendCommandThenRead(fnum, args, sizeof(args), retvals, sizeof(retvals)); - // get detectormac, detector ip - uint64_t idetectormac = 0; - uint32_t idetectorip = 0; - sscanf(retvals[0], "%lx", &idetectormac); - sscanf(retvals[1], "%x", &idetectorip); - idetectorip = __builtin_bswap32(idetectorip); - snprintf(retvals[0], sizeof(retvals[0]), "%02x:%02x:%02x:%02x:%02x:%02x", - (unsigned int)((idetectormac >> 40) & 0xFF), - (unsigned int)((idetectormac >> 32) & 0xFF), - (unsigned int)((idetectormac >> 24) & 0xFF), - (unsigned int)((idetectormac >> 16) & 0xFF), - (unsigned int)((idetectormac >> 8) & 0xFF), - (unsigned int)((idetectormac >> 0) & 0xFF)); + uint64_t detector_mac = 0; + uint32_t detector_ip = 0; + sscanf(retvals[0], "%lx", &detector_mac); // TODO! (Erik) send mac and ip as int + sscanf(retvals[1], "%x", &detector_ip); + detector_ip = __builtin_bswap32(detector_ip); - - // update if different - if (strcasecmp(retvals[0], detector_shm()->detectorMAC)) { - memset(detector_shm()->detectorMAC, 0, MAX_STR_LENGTH); - sls::strcpy_safe(detector_shm()->detectorMAC, retvals[0]); - FILE_LOG(logINFO) << detId << ": Detector MAC updated to " - << detector_shm()->detectorMAC; + if (detector_shm()->detectorMAC != detector_mac) { + detector_shm()->detectorMAC = detector_mac; + FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << getDetectorMAC(); } - - if (detector_shm()->detectorIP != idetectorip) { - std::cout << "idetectorip: " << idetectorip << '\n'; - detector_shm()->detectorIP = idetectorip; + + if (detector_shm()->detectorIP != detector_ip) { + detector_shm()->detectorIP = detector_ip; FILE_LOG(logINFO) << detId << ": Detector IP updated to " << getDetectorIP(); + std::cout << "ip: " << detector_ip << "\n"; } } if (ret == FORCE_UPDATE) { @@ -1738,7 +1713,8 @@ int64_t slsDetector::setTimer(timerIndex index, int64_t t) { ret = client.sendCommandThenRead(fnum, args, sizeof(args), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << getTimerType(index) << ": " << retval; detector_shm()->timerValue[index] = retval; - // update #nchans and databytes, as it depends on #samples, roi, readoutflags (ctb only) + // update #nchans and databytes, as it depends on #samples, roi, + // readoutflags (ctb only) if (index == SAMPLES && (detector_shm()->myDetectorType == CHIPTESTBOARD || detector_shm()->myDetectorType == MOENCH)) { updateTotalNumberOfChannels(); @@ -1992,7 +1968,8 @@ int slsDetector::setReadOutFlags(readOutFlags flag) { ret = client.sendCommandThenRead(fnum, &arg, sizeof(arg), &retval, sizeof(retval)); FILE_LOG(logDEBUG1) << "Readout flag: " << retval; detector_shm()->roFlags = (readOutFlags)retval; - // update #nchans and databytes, as it depends on #samples, roi, readoutflags (ctb only) + // update #nchans and databytes, as it depends on #samples, roi, + // readoutflags (ctb only) if (detector_shm()->myDetectorType == CHIPTESTBOARD) { updateTotalNumberOfChannels(); } @@ -2088,17 +2065,17 @@ std::string slsDetector::setDetectorMAC(const std::string &detectorMAC) { } // valid format else { - sls::strcpy_safe(detector_shm()->detectorMAC, detectorMAC.c_str()); + detector_shm()->detectorMAC = MacStringToUint(detectorMAC); 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 std::string(detector_shm()->detectorMAC); + return getDetectorMAC(); } -std::string slsDetector::getDetectorMAC() { return std::string(detector_shm()->detectorMAC); } +std::string slsDetector::getDetectorMAC() { return MacAddrToString(detector_shm()->detectorMAC); } std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) { // invalid format @@ -2108,17 +2085,17 @@ std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) { } // valid format else { - sls::strcpy_safe(detector_shm()->detectorMAC2, detectorMAC.c_str()); + detector_shm()->detectorMAC2 = MacStringToUint(detectorMAC); 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 std::string(detector_shm()->detectorMAC2); + return getDetectorMAC2(); } -std::string slsDetector::getDetectorMAC2() { return std::string(detector_shm()->detectorMAC2); } +std::string slsDetector::getDetectorMAC2() { return MacAddrToString(detector_shm()->detectorMAC2); } std::string slsDetector::setDetectorIP(const std::string &detectorIP) { // struct sockaddr_in sa; @@ -2126,8 +2103,8 @@ std::string slsDetector::setDetectorIP(const std::string &detectorIP) { if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) { int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); if (result == 0) { - throw RuntimeError( - "setDetectorIP: IP Address should be VALID and in xxx.xxx.xxx.xxx format"); + 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")) { @@ -2151,8 +2128,8 @@ std::string slsDetector::setDetectorIP2(const std::string &detectorIP) { if (detectorIP.length() && detectorIP.length() < 16) { int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); if (result == 0) { - throw RuntimeError( - "setDetectorIP: IP Address 2 should be VALID and in xxx.xxx.xxx.xxx format"); + 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")) { @@ -2298,14 +2275,14 @@ std::string slsDetector::getReceiverHostname() const { } std::string slsDetector::setReceiverUDPIP(const std::string &udpip) { - struct sockaddr_in sa; + if (udpip.length() && udpip.length() < 16) { - int result = inet_pton(AF_INET, udpip.c_str(), &(sa.sin_addr)); - if (result == 0) { - throw ReceiverError( - "setReceiverUDPIP: UDP IP Address should be VALID and in xxx.xxx.xxx.xxx format"); + auto ip = IpStringToUint(udpip.c_str()); + if (ip == 0) { + throw ReceiverError("setReceiverUDPIP: UDP IP Address should be " + "VALID and in xxx.xxx.xxx.xxx format"); } else { - sls::strcpy_safe(detector_shm()->receiverUDPIP, udpip.c_str()); + detector_shm()->receiverUDPIP = ip; if (!strcmp(detector_shm()->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } else if (setUDPConnection() == FAIL) { @@ -2313,22 +2290,21 @@ std::string slsDetector::setReceiverUDPIP(const std::string &udpip) { } } } - return std::string(detector_shm()->receiverUDPIP); + return getReceiverUDPIP(); } std::string slsDetector::getReceiverUDPIP() const { - return std::string(detector_shm()->receiverUDPIP); + return IpToString(detector_shm()->receiverUDPIP); } std::string slsDetector::setReceiverUDPIP2(const std::string &udpip) { - struct sockaddr_in sa; if (udpip.length() && udpip.length() < 16) { - int result = inet_pton(AF_INET, udpip.c_str(), &(sa.sin_addr)); - if (result == 0) { - throw ReceiverError( - "setReceiverUDPIP: UDP IP Address 2 should be VALID and in xxx.xxx.xxx.xxx format"); + auto ip = IpStringToUint(udpip.c_str()); + if (ip == 0) { + throw ReceiverError("setReceiverUDPIP: UDP IP Address 2 should be " + "VALID and in xxx.xxx.xxx.xxx format"); } else { - sls::strcpy_safe(detector_shm()->receiverUDPIP2, udpip.c_str()); + detector_shm()->receiverUDPIP2 = ip; if (!strcmp(detector_shm()->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } else if (setUDPConnection() == FAIL) { @@ -2336,57 +2312,37 @@ std::string slsDetector::setReceiverUDPIP2(const std::string &udpip) { } } } - return std::string(detector_shm()->receiverUDPIP2); + return getReceiverUDPIP2(); } std::string slsDetector::getReceiverUDPIP2() const { - return std::string(detector_shm()->receiverUDPIP2); + return IpToString(detector_shm()->receiverUDPIP2); } std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) { - // invalid format - if ((udpmac.length() != 17) || (udpmac[2] != ':') || (udpmac[5] != ':') || (udpmac[8] != ':') || - (udpmac[11] != ':') || (udpmac[14] != ':')) { - throw ReceiverError( - "setReceiverUDPMAC: udp MAC Address should be in xx:xx:xx:xx:xx:xx format"); + auto mac = MacStringToUint(udpmac); + if (mac == 0) { + throw ReceiverError("Could not decode UDPMAC from: " + udpmac); } - // valid format - else { - sls::strcpy_safe(detector_shm()->receiverUDPMAC, udpmac.c_str()); - if (!strcmp(detector_shm()->receiver_hostname, "none")) { - FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; - } - // not doing setUDPConnection as rx_udpmac will get replaced,(must use configuremac) - sls::strcpy_safe(detector_shm()->receiverUDPMAC, udpmac.c_str()); - } - return std::string(detector_shm()->receiverUDPMAC); + detector_shm()->receiverUDPMAC = mac; + return getReceiverUDPMAC(); } std::string slsDetector::getReceiverUDPMAC() const { - return std::string(detector_shm()->receiverUDPMAC); + return MacAddrToString(detector_shm()->receiverUDPMAC); } std::string slsDetector::setReceiverUDPMAC2(const std::string &udpmac) { - // invalid format - if ((udpmac.length() != 17) || (udpmac[2] != ':') || (udpmac[5] != ':') || (udpmac[8] != ':') || - (udpmac[11] != ':') || (udpmac[14] != ':')) { - throw ReceiverError( - "setReceiverUDPMAC: udp MAC Address 2 should be in xx:xx:xx:xx:xx:xx format"); + auto mac = MacStringToUint(udpmac); + if (mac == 0) { + throw ReceiverError("Could not decode UDPMA2C from: " + udpmac); } - // valid format - else { - sls::strcpy_safe(detector_shm()->receiverUDPMAC2, udpmac.c_str()); - if (!strcmp(detector_shm()->receiver_hostname, "none")) { - FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; - } - // not doing setUDPConnection as rx_udpmac will get replaced,(must use configuremac) - sls::strcpy_safe(detector_shm()->receiverUDPMAC2, udpmac.c_str()); - } - return std::string(detector_shm()->receiverUDPMAC2); + detector_shm()->receiverUDPMAC2 = mac; + return getReceiverUDPMAC2(); } std::string slsDetector::getReceiverUDPMAC2() const { - return std::string(detector_shm()->receiverUDPMAC2); + return MacAddrToString(detector_shm()->receiverUDPMAC2); } int slsDetector::setReceiverUDPPort(int udpport) { @@ -2494,7 +2450,8 @@ void slsDetector::setReceiverStreamingIP(std::string sourceIP) { // if empty, give rx_hostname if (sourceIP.empty()) { if (!strcmp(detector_shm()->receiver_hostname, "none")) { - throw RuntimeError("Receiver hostname not set yet. Cannot create rx_zmqip from none"); + throw RuntimeError("Receiver hostname not set yet. Cannot create " + "rx_zmqip from none"); } sourceIP.assign(detector_shm()->receiver_hostname); } @@ -2604,18 +2561,20 @@ std::string slsDetector::setAdditionalJsonParameter(const std::string &key, const std::string &value) { // validation (value or key is empty) if (!key.length() || !value.length()) { - throw("Could not set additional json header parameter as the key or value is empty"); + throw("Could not set additional json header parameter as the key or " + "value is empty"); } // validation (ignore if key or value has , : ") if (key.find_first_of(",\":") != std::string::npos || value.find_first_of(",\":") != std::string::npos) { - throw RuntimeError("Could not set additional json header parameter as the key or value has " + throw RuntimeError("Could not set additional json header parameter as " + "the key or value has " "illegal characters (,\":)"); } - // create actual key to search for and actual value to put, (key has additional ':' as value - // could exist the same way) + // create actual key to search for and actual value to put, (key has + // additional ':' as value could exist the same way) std::string keyLiteral(std::string("\"") + key + std::string("\":")); std::string valueLiteral(value); // add quotations to value only if it is a string @@ -2634,7 +2593,8 @@ std::string slsDetector::setAdditionalJsonParameter(const std::string &key, if (keyPos != std::string::npos) { size_t valueStartPos = header.find(std::string(":"), keyPos) + 1; size_t valueEndPos = header.find(std::string(","), valueStartPos) - 1; - // if valueEndPos doesnt find comma (end of string), it goes anyway to end of line + // if valueEndPos doesnt find comma (end of string), it goes anyway to + // end of line header.replace(valueStartPos, valueEndPos - valueStartPos + 1, valueLiteral); } @@ -2730,41 +2690,27 @@ int slsDetector::setUDPConnection() { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet."; return FAIL; } - // if no udp ip given, use hostname - if (!strcmp(detector_shm()->receiverUDPIP, "none")) { - // hostname is an ip address - if (strchr(detector_shm()->receiver_hostname, '.') != nullptr) { - sls::strcpy_safe(detector_shm()->receiverUDPIP, detector_shm()->receiver_hostname); - // if hostname not ip, convert it to ip - } else { - struct addrinfo *result; - if (sls::ConvertHostnameToInternetAddress(detector_shm()->receiver_hostname, &result) == - 0) { - // on success - memset(detector_shm()->receiverUDPIP, 0, MAX_STR_LENGTH); - // on failure, back to none - if (sls::ConvertInternetAddresstoIpString(result, detector_shm()->receiverUDPIP, - MAX_STR_LENGTH)) { - sls::strcpy_safe(detector_shm()->receiverUDPIP, "none"); - throw RuntimeError("setUDPConnection: Error. Receiver UDP IP Address not set"); - } - } + + if (detector_shm()->receiverUDPIP == 0) { + // Hostname could be ip try to decode otherwise look up the hostname + detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname); + if (detector_shm()->receiverUDPIP == 0) { + detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname); } } - - // jungfrau 2 interfaces or (1 interface and 2nd interface), copy udpip if udpip2 empty + // jungfrau 2 interfaces or (1 interface and 2nd interface), copy udpip if + // udpip2 empty if (detector_shm()->numUDPInterfaces == 2 || detector_shm()->selectedUDPInterface == 2) { - // copy from udpip - if (!strcmp(detector_shm()->receiverUDPIP2, "none")) { - sls::strcpy_safe(detector_shm()->receiverUDPIP2, detector_shm()->receiverUDPIP); + if (detector_shm()->receiverUDPIP2 == 0) { + detector_shm()->receiverUDPIP2 = detector_shm()->receiverUDPIP; } } // 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], detector_shm()->receiverUDPIP); - sls::strcpy_safe(args[3], detector_shm()->receiverUDPIP2); + sls::strcpy_safe(args[2], getReceiverUDPIP().c_str()); + sls::strcpy_safe(args[3], getReceiverUDPIP2().c_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: " @@ -2781,13 +2727,12 @@ int slsDetector::setUDPConnection() { ret = receiver.sendCommandThenRead(fnum, args, sizeof(args), retvals, sizeof(retvals)); if (strlen(retvals[0])) { FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0]; - memset(detector_shm()->receiverUDPMAC, 0, MAX_STR_LENGTH); - sls::strcpy_safe(detector_shm()->receiverUDPMAC, retvals[0]); + detector_shm()->receiverUDPMAC = MacStringToUint(retvals[0]); + } if (strlen(retvals[1])) { FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1]; - memset(detector_shm()->receiverUDPMAC2, 0, MAX_STR_LENGTH); - sls::strcpy_safe(detector_shm()->receiverUDPMAC2, retvals[1]); + detector_shm()->receiverUDPMAC2 = MacStringToUint(retvals[1]); } if (ret == FORCE_UPDATE) { receiver.close(); @@ -2965,7 +2910,8 @@ int slsDetector::setROI(int n, ROI roiLimits[]) { if (detector_shm()->myDetectorType == MOENCH) { sendROIToProcessor(); } - // update #nchans and databytes, as it depends on #samples, roi, readoutflags (ctb only) + // update #nchans and databytes, as it depends on #samples, roi, + // readoutflags (ctb only) if (detector_shm()->myDetectorType == CHIPTESTBOARD || detector_shm()->myDetectorType == MOENCH) { updateTotalNumberOfChannels(); @@ -2976,12 +2922,13 @@ int slsDetector::setROI(int n, ROI roiLimits[]) { slsDetectorDefs::ROI *slsDetector::getROI(int &n) { sendROI(-1, nullptr); n = detector_shm()->nROI; - // moench - get json header(due to different clients, diff shm) (get roi is from detector: - // updated anyway) + // moench - get json header(due to different clients, diff shm) (get roi is + // from detector: updated anyway) if (detector_shm()->myDetectorType == MOENCH) { getAdditionalJsonHeader(); } - // update #nchans and databytes, as it depends on #samples, roi, readoutflags (ctb only) + // update #nchans and databytes, as it depends on #samples, roi, + // readoutflags (ctb only) if (detector_shm()->myDetectorType == CHIPTESTBOARD || detector_shm()->myDetectorType == MOENCH) { updateTotalNumberOfChannels(); @@ -3171,7 +3118,8 @@ int slsDetector::setFlippedData(dimension d, int value) { throw RuntimeError("Flipped across Y axis is not implemented"); } - // replace get with shm value (write to shm right away as it is a det value, not rx value) + // replace get with shm value (write to shm right away as it is a det value, + // not rx value) if (value > -1) { detector_shm()->flippedData[d] = (value > 0) ? 1 : 0; } @@ -3405,9 +3353,9 @@ int slsDetector::programFPGA(const std::string &fname) { int dst = mkstemp(destfname); // create temporary file and open it in r/w if (dst == -1) { fclose(src); - throw RuntimeError( - std::string("Could not create destination file in /tmp for programming: ") + - destfname); + throw RuntimeError(std::string("Could not create destination file " + "in /tmp for programming: ") + + destfname); } // convert src to dst rawbin @@ -3438,7 +3386,8 @@ int slsDetector::programFPGA(const std::string &fname) { write(dst, &y, 1); } if (filepos < 0x1000000) { - throw RuntimeError("Could not convert programming file. EOF before end of flash"); + throw RuntimeError("Could not convert programming file. EOF " + "before end of flash"); } } if (fclose(src)) { @@ -3473,7 +3422,8 @@ int slsDetector::programFPGA(const std::string &fname) { if (fclose(fp)) { free(fpgasrc); - throw RuntimeError("Program FPGA: Could not close destination file after converting"); + throw RuntimeError("Program FPGA: Could not close destination file " + "after converting"); } unlink(destfname); // delete temporary file FILE_LOG(logDEBUG1) << "Successfully loaded the rawbin file to program memory"; @@ -3775,7 +3725,6 @@ int slsDetector::setReceiverOnline(int value) { detector_shm()->receiverTCPPort); receiver.close(); detector_shm()->receiverOnlineFlag = ONLINE_FLAG; - // check for version compatibility if (detector_shm()->receiverAPIVersion == 0) { checkReceiverVersionCompatibility(); @@ -4501,7 +4450,8 @@ int slsDetector::setPattern(const std::string &fname) { FILE *fd = fopen(fname.c_str(), "r"); if (fd != nullptr) { while (fread(&word, sizeof(word), 1, fd)) { - setPatternWord(addr, word); // TODO! (Erik) do we need to send pattern in 64bit chunks? + setPatternWord(addr, word); // TODO! (Erik) do we need to send + // pattern in 64bit chunks? ++addr; } fclose(fd); @@ -4852,7 +4802,8 @@ slsDetector::readSettingsFile(const std::string &fname, sls_detector_module *myM deleteModule(myMod); } infile.close(); - throw RuntimeError("readSettingsFile: Could not load all values for settings for " + + throw RuntimeError("readSettingsFile: Could not load all values " + "for settings for " + fname); } for (int i = 0; i < myMod->ndac; ++i) { diff --git a/slsSupportLib/CMakeLists.txt b/slsSupportLib/CMakeLists.txt index 08e39c177..471fc9306 100644 --- a/slsSupportLib/CMakeLists.txt +++ b/slsSupportLib/CMakeLists.txt @@ -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 diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index 6f1dcb8f1..2be26378a 100644 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -17,7 +17,7 @@ #endif #ifndef FILELOG_MAX_LEVEL -#define FILELOG_MAX_LEVEL logDEBUG1 +#define FILELOG_MAX_LEVEL logINFO #endif diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h new file mode 100644 index 000000000..0f274d998 --- /dev/null +++ b/slsSupportLib/include/network_utils.h @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace sls { + +std::string MacAddrToString(uint64_t mac); +uint64_t MacStringToUint(std::string mac); + +uint32_t IpStringToUint(const char* ipstr); +std::string IpToString(uint32_t ip); + +uint32_t HostnameToIp(const char *const hostname); + +} // namespace sls diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index cbc9119e2..2991ff21e 100644 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -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"); diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp new file mode 100644 index 000000000..3d7fa26a3 --- /dev/null +++ b/slsSupportLib/src/network_utils.cpp @@ -0,0 +1,69 @@ +#include "sls_detector_exceptions.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace sls { + +std::string MacAddrToString(uint64_t mac) { + std::ostringstream ss; + ss << std::hex << std::setfill('0') << std::setw(2); + ss << ((mac >> 40) & 0xFF); + for (int i = 32; i >= 0; i -= 8) { + ss << ':' << ((mac >> i) & 0xFF); + } + return ss.str(); +} + +uint64_t MacStringToUint(std::string mac) { + if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') || + (mac[11] != ':') || (mac[14] != ':')) { + return 0; + } + mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end()); + return std::stol(mac, nullptr, 16); +} + +uint32_t IpStringToUint(const char *ipstr) { + uint32_t ip{0}; + inet_pton(AF_INET, ipstr, &ip); + return ip; +} + +std::string IpToString(uint32_t ip) { + char ipstring[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN) == nullptr) { + // handle error + } + // TODO! Check return + return ipstring; +} + +uint32_t HostnameToIp(const char *const 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 + +// char ipstring[INET_ADDRSTRLEN]; +// inet_ntop(AF_INET, &detector_shm()->detectorIP, ipstring, INET_ADDRSTRLEN); + +// inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP)); \ No newline at end of file diff --git a/slsSupportLib/tests/CMakeLists.txt b/slsSupportLib/tests/CMakeLists.txt index f46e60a68..a26b0c69c 100644 --- a/slsSupportLib/tests/CMakeLists.txt +++ b/slsSupportLib/tests/CMakeLists.txt @@ -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 ) diff --git a/slsSupportLib/tests/test-network_utils.cpp b/slsSupportLib/tests/test-network_utils.cpp new file mode 100644 index 000000000..f363f4e39 --- /dev/null +++ b/slsSupportLib/tests/test-network_utils.cpp @@ -0,0 +1,44 @@ + +#include "catch.hpp" +#include "network_utils.h" +#include +#include + +#include "string_utils.h" + +using namespace sls; +TEST_CASE("Convert mac address") { + + std::vector vec_addr{346856806822, 346856806852, 262027939863028}; + std::vector 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 mac = vec_addr[i]; + auto answer = vec_ans[i]; + + std::string string_addr = MacAddrToString(mac); + CHECK(string_addr == answer); + CHECK(MacStringToUint(string_addr) == mac); + } +} + +TEST_CASE("Convert IP"){ + std::vector vec_addr{4073554305, 2747957633, 2697625985}; + std::vector vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"}; + + for (int i=0; i!= vec_addr.size(); ++i){ + auto ip = vec_addr[i]; + auto answer = vec_ans[i]; + + auto string_addr = IpToString(ip); + CHECK(string_addr == answer); + CHECK(IpStringToUint(string_addr.c_str()) == ip); + } +} + +TEST_CASE("IP not valid"){ + + CHECK(IpStringToUint("hej") == 0); + CHECK(IpStringToUint("mpc2408") == 0); +} + +// TEST_CASE("Lookup ip") \ No newline at end of file From 959d9aa5d1e2156a7776b5c25c3714172913474a Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Mon, 1 Apr 2019 10:36:47 +0200 Subject: [PATCH 04/10] cleaning and new functions --- integrationTests/src/a.cpp | 6 +- slsDetectorSoftware/src/slsDetector.cpp | 80 +++++++--------------- slsSupportLib/include/logger.h | 2 +- slsSupportLib/include/network_utils.h | 8 ++- slsSupportLib/src/network_utils.cpp | 20 ++++-- slsSupportLib/tests/test-network_utils.cpp | 15 +++- 6 files changed, 62 insertions(+), 69 deletions(-) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index b4d9f8a72..a4b212a26 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -8,6 +8,7 @@ #include "sls_detector_defs.h" #include "sls_detector_exceptions.h" #include "sls_detector_funcs.h" +#include "network_utils.h" #include #include @@ -26,7 +27,10 @@ using sls::SocketError; int main() { - std::cout << "size: " <> hostname; diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index 90b401de9..d84afbfc4 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -318,9 +318,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) { detector_shm()->detectorMAC = MacStringToUint(DEFAULT_DET_MAC); detector_shm()->detectorMAC2 = MacStringToUint(DEFAULT_DET_MAC2); - - inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP)); - inet_pton(AF_INET, DEFAULT_DET_IP2, &(detector_shm()->detectorIP2)); + detector_shm()->detectorIP = IpStringToUint(DEFAULT_DET_MAC); + detector_shm()->detectorIP2 = IpStringToUint(DEFAULT_DET_MAC2); detector_shm()->numUDPInterfaces = 1; detector_shm()->selectedUDPInterface = 1; @@ -1569,42 +1568,48 @@ int slsDetector::configureMAC() { const size_t array_size = 50; const size_t n_args = 14; const size_t n_retvals = 2; - char args[n_args][array_size] = {}; - char retvals[n_retvals][array_size] = {}; + char args[n_args][array_size]{}; + char retvals[n_retvals][array_size]{}; FILE_LOG(logDEBUG1) << "Configuring MAC"; if (detector_shm()->receiverUDPIP == 0) { - // Hostname could be ip try to decode otherwise look up the 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); } } - // rx_udpmac is none + if (detector_shm()->receiverUDPMAC == 0) { throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses not set"); } FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpmac are valid "; - // jungfrau 2 interfaces - // validate for the second interface + // Jungfrau second interface if (detector_shm()->numUDPInterfaces == 2) { - // copy from udpip (done here as well if udpconnection avoided (no - // slsrxr) if (detector_shm()->receiverUDPIP2 == 0) { detector_shm()->receiverUDPIP2 = detector_shm()->receiverUDPIP; } + if (detector_shm()->receiverUDPMAC2 == 0) { + throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set"); + } FILE_LOG(logDEBUG1) << "rx_udpmac2 is valid "; } - // copy to args + // copy to args and convert to hex snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort); - sls::strcpy_safe(args[1], getReceiverUDPIP().c_str()); + 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[5], array_size, "%x", detector_shm()->receiverUDPPort2); - sls::strcpy_safe(args[6], getReceiverUDPIP2().c_str()); + 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], ':'); // number of interfaces and which one snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces); @@ -1625,40 +1630,17 @@ int slsDetector::configureMAC() { } FILE_LOG(logDEBUG1) << "receiver udp port:" << std::dec << args[0] << "-"; - // converting receiverUDPIP to string hex - sls::strcpy_safe(args[1], sls::stringIpToHex(args[1]).c_str()); FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[1] << "-"; - // MAC already in hex removing : - sls::removeChar(args[2], ':'); FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[2] << "-"; - // converting detectorIP to string hex - sls::strcpy_safe(args[3], sls::stringIpToHex(getDetectorIP()).c_str()); FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[3] << "-"; - // MAC already in hex removing : - sls::removeChar(args[4], ':'); FILE_LOG(logDEBUG1) << "detector udp mac:" << args[4] << "-"; - FILE_LOG(logDEBUG1) << "receiver udp port2:" << std::dec << args[5] << "-"; - if (!strcmp(args[6], "none")) { - sprintf(args[6], "%d", 0); - } else { - sls::strcpy_safe(args[6], sls::stringIpToHex(args[6]).c_str()); - } FILE_LOG(logDEBUG1) << "receiver udp ip2:" << args[6] << "-"; - // MAC already in hex removing : - sls::removeChar(args[7], ':'); FILE_LOG(logDEBUG1) << "receiver udp mac2:" << args[7] << "-"; - // converting detectorIP to string hex - sls::strcpy_safe(args[8], sls::stringIpToHex(getDetectorIP2()).c_str()); - FILE_LOG(logDEBUG1) << "detecotor udp ip2:" << args[8] << "-"; - // MAC already in hex removing : - sls::removeChar(args[9], ':'); FILE_LOG(logDEBUG1) << "detector udp mac2:" << args[9] << "-"; - FILE_LOG(logDEBUG1) << "number of udp interfaces:" << std::dec << args[10] << "-"; FILE_LOG(logDEBUG1) << "selected udp interface:" << std::dec << args[11] << "-"; - FILE_LOG(logDEBUG1) << "row:" << args[12] << "-"; FILE_LOG(logDEBUG1) << "col:" << args[13] << "-"; @@ -2098,11 +2080,9 @@ std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) { std::string slsDetector::getDetectorMAC2() { return MacAddrToString(detector_shm()->detectorMAC2); } std::string slsDetector::setDetectorIP(const std::string &detectorIP) { - // struct sockaddr_in sa; - uint32_t ip{0}; if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) { - int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); - if (result == 0) { + 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 { @@ -2117,17 +2097,12 @@ std::string slsDetector::setDetectorIP(const std::string &detectorIP) { return getDetectorIP(); } -std::string slsDetector::getDetectorIP() const { - char ipstring[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &detector_shm()->detectorIP, ipstring, INET_ADDRSTRLEN); - return ipstring; -} +std::string slsDetector::getDetectorIP() const { return IpToString(detector_shm()->detectorIP); } std::string slsDetector::setDetectorIP2(const std::string &detectorIP) { - uint32_t ip{0}; if (detectorIP.length() && detectorIP.length() < 16) { - int result = inet_pton(AF_INET, detectorIP.c_str(), &ip); - if (result == 0) { + 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 { @@ -2142,11 +2117,7 @@ std::string slsDetector::setDetectorIP2(const std::string &detectorIP) { return getDetectorIP2(); } -std::string slsDetector::getDetectorIP2() const { - char ipstring[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &detector_shm()->detectorIP2, ipstring, INET_ADDRSTRLEN); - return ipstring; -} +std::string slsDetector::getDetectorIP2() const { return IpToString(detector_shm()->detectorIP2); } std::string slsDetector::setReceiverHostname(const std::string &receiverIP) { FILE_LOG(logDEBUG1) << "Setting up Receiver with " << receiverIP; @@ -2728,7 +2699,6 @@ int slsDetector::setUDPConnection() { if (strlen(retvals[0])) { FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0]; detector_shm()->receiverUDPMAC = MacStringToUint(retvals[0]); - } if (strlen(retvals[1])) { FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1]; diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index 2be26378a..6f1dcb8f1 100644 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -17,7 +17,7 @@ #endif #ifndef FILELOG_MAX_LEVEL -#define FILELOG_MAX_LEVEL logINFO +#define FILELOG_MAX_LEVEL logDEBUG1 #endif diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h index 0f274d998..02fadad17 100644 --- a/slsSupportLib/include/network_utils.h +++ b/slsSupportLib/include/network_utils.h @@ -6,9 +6,11 @@ namespace sls { std::string MacAddrToString(uint64_t mac); uint64_t MacStringToUint(std::string mac); -uint32_t IpStringToUint(const char* ipstr); -std::string IpToString(uint32_t ip); +uint32_t IpStringToUint(const char *ipstr); -uint32_t HostnameToIp(const char *const hostname); +std::string IpToString(uint32_t ip); +std::string IpToHexString(uint32_t ip); + +uint32_t HostnameToIp(const char *hostname); } // namespace sls diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index 3d7fa26a3..cf153fada 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -39,15 +39,21 @@ uint32_t IpStringToUint(const char *ipstr) { } std::string IpToString(uint32_t ip) { - char ipstring[INET_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN) == nullptr) { - // handle error - } - // TODO! Check return + char ipstring[INET_ADDRSTRLEN]{}; + inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN); return ipstring; } -uint32_t HostnameToIp(const char *const hostname) { +std::string IpToHexString(uint32_t ip){ + std::ostringstream ss; + ss << std::hex << std::setfill('0') << std::setw(2); + for (int i=0; i!=4; ++i){ + ss << ((ip >> i*8) & 0xFF); + } + return ss.str(); +} + +uint32_t HostnameToIp(const char *hostname) { struct addrinfo hints, *result; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; @@ -61,6 +67,8 @@ uint32_t HostnameToIp(const char *const hostname) { return ip; } + + } // namespace sls // char ipstring[INET_ADDRSTRLEN]; diff --git a/slsSupportLib/tests/test-network_utils.cpp b/slsSupportLib/tests/test-network_utils.cpp index f363f4e39..7d3ca1ccf 100644 --- a/slsSupportLib/tests/test-network_utils.cpp +++ b/slsSupportLib/tests/test-network_utils.cpp @@ -21,11 +21,11 @@ TEST_CASE("Convert mac address") { } } -TEST_CASE("Convert IP"){ +TEST_CASE("Convert IP") { std::vector vec_addr{4073554305, 2747957633, 2697625985}; std::vector vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"}; - for (int i=0; i!= vec_addr.size(); ++i){ + for (int i = 0; i != vec_addr.size(); ++i) { auto ip = vec_addr[i]; auto answer = vec_ans[i]; @@ -35,10 +35,19 @@ TEST_CASE("Convert IP"){ } } -TEST_CASE("IP not valid"){ +TEST_CASE("IP not valid") { CHECK(IpStringToUint("hej") == 0); CHECK(IpStringToUint("mpc2408") == 0); } +TEST_CASE("Convert ip to hex") { + std::vector ipstrings{"74.125.43.99", "129.129.202.217"}; + std::vector vec_ans{"4a7d2b63", "8181cad9"}; + for (int i = 0; i != ipstrings.size(); ++i) { + uint32_t ip = IpStringToUint(ipstrings[i].c_str()); + CHECK(IpToHexString(ip) == vec_ans[i]); + } +} + // TEST_CASE("Lookup ip") \ No newline at end of file From 73ad2380281892fb39c390ed8e24b65fc79cf025 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Mon, 1 Apr 2019 13:15:38 +0200 Subject: [PATCH 05/10] removed missed cout --- slsDetectorSoftware/src/slsDetector.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index d84afbfc4..cc8e8dab2 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -1663,7 +1663,6 @@ int slsDetector::configureMAC() { if (detector_shm()->detectorIP != detector_ip) { detector_shm()->detectorIP = detector_ip; FILE_LOG(logINFO) << detId << ": Detector IP updated to " << getDetectorIP(); - std::cout << "ip: " << detector_ip << "\n"; } } if (ret == FORCE_UPDATE) { From df2d67d90d1a0b731d8c9078ebb76aa5a896e894 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 2 Apr 2019 11:56:33 +0200 Subject: [PATCH 06/10] classes for ip and mac addr --- integrationTests/src/a.cpp | 126 ++------------------------ slsSupportLib/include/network_utils.h | 34 ++++++- slsSupportLib/src/network_utils.cpp | 54 ++++++++--- 3 files changed, 84 insertions(+), 130 deletions(-) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index a4b212a26..8c676ea0c 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -4,11 +4,11 @@ #include "ClientSocket.h" #include "Timer.h" #include "logger.h" +#include "network_utils.h" #include "slsDetector.h" #include "sls_detector_defs.h" #include "sls_detector_exceptions.h" #include "sls_detector_funcs.h" -#include "network_utils.h" #include #include @@ -18,128 +18,22 @@ #include #include -#define VERBOSE +#include "network_utils.h" -using sls::DetectorError; -using sls::RuntimeError; -using sls::SharedMemoryError; -using sls::SocketError; +using namespace sls; int main() { - auto ip = sls::IpStringToUint("74.125.43.99"); - char buffer[50]; - snprintf(buffer, 50, "%x", __builtin_bswap32(ip)); - std::cout << "buffer: " << buffer << "\n"; - // std::string hostname; - // std::cout << "Enter hostname: "; - // std::cin >> hostname; + IpAddr a("129.129.205.242"); + IpAddr b(4073554305); - // struct addrinfo hints, *result; - // memset(&hints, 0, sizeof(hints)); - // hints.ai_family = AF_INET; - // hints.ai_socktype = SOCK_STREAM; - // hints.ai_flags |= AI_CANONNAME; + std::cout << "a is: " << a << " and b is: " << b << "\n"; + if (a == b) + std::cout << "a is equal to b\n"; - // struct sockaddr_in serverAddr {}; - // // std::cout << "sizeof(result):" << sizeof(hints) << '\n'; - // // std::cout << "sizeof(serverAddr):" << sizeof(serverAddr) << '\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"; - // uint32_t ip{0}; - // int port = 1952; - - // if (getaddrinfo(hostname.c_str(), NULL, &hints, &result) != 0) { - // std::string msg = "ClientSocket cannot decode host:" + hostname + " on port " + - // std::to_string(port) + "\n"; - // throw 5; - // } - - // serverAddr.sin_family = AF_INET; - // serverAddr.sin_port = htons(port); - // memcpy(&serverAddr.sin_addr.s_addr, &((struct sockaddr_in *)result->ai_addr)->sin_addr, - // sizeof(in_addr_t)); - // freeaddrinfo(result); - // ip = serverAddr.sin_addr.s_addr; - - // char address[INET_ADDRSTRLEN]; - // inet_ntop(AF_INET, &ip, address, INET_ADDRSTRLEN); - // std::cout << "ip of host is: " << address << '\n'; - - // // hints.ai_addr = reinterpret_cast(&serverAddr); - - // // if (getaddrinfo("NULL", NULL, &hints, &result) != 0) { - // // std::string msg = "ClientSocket cannot decode host:" + hostname + " on port " + - // // std::to_string(port) + "\n"; - // // throw 5; - // // } - - // sls::ClientSocket(false, serverAddr); - - // const std::string hostname = "beb083"; - // auto type = slsDetector::getTypeFromDetector(hostname); - // slsDetector d(type); - // d.setHostname(hostname); - // d.setOnline(true); - // std::cout << "hostname: " << d.getHostname() << '\n'; - // d.setThresholdTemperature(50); - // try{ - // d.setThresholdTemperature(50); - // }catch(const DetectorError &e){ - // std::cout << "Caught: " << e.what() << '\n'; - // } - // std::cout << "hostname: " << d.getHostname() << '\n'; - // std::cout << "exptime: " << d.setDAC(-1, slsDetectorDefs::E_Vrf, 0) << '\n'; - - // slsDetector d2(type); - // std::cout << "Online: " << d2.getOnlineFlag() << '\n'; - // d2.setHostname("beb55555"); - // d2.setOnline(true); - // std::cout << "Online: " << d2.getOnlineFlag() << '\n'; - // std::cout << "hostname: " << d2.getHostname() << '\n'; - - // std::cout << "port: " << d.getControlPort() << '\n'; - // d.setOnline(true); - // d.setReceiverOnline(true); - // std::cout << "reciver version: " << std::hex << d.getReceiverVersion() << '\n'; - // // std::cout << "version: " << d.getId(slsDetectorDefs::CLIENT_RECEIVER_API_VERSION) << '\n'; - // d.freeSharedMemory(); - // //Catch exception - // try { - // throw RuntimeError("something went wrong"); - // } catch (RuntimeError &e) { - // std::cout << "Caught RuntimeError with message : " << e.what() << '\n'; - // } - - // //Catch base class - // try { - // throw SharedMemoryError("Could not create shared memory"); - // } catch (RuntimeError &e) { - // std::cout << "Caught: " << e.what() << '\n'; - // } - - // //Catch base class after looking for something else - // try { - // throw SharedMemoryError("Could not create shared memory"); - // } catch (SocketError &e) { - - // std::cout << "Caught Socket error: " << e.what() << '\n'; - - // } catch (RuntimeError &e) { - // std::cout << "Caught base class: " << e.what() << '\n'; - // } - - // //Catch any after looking for something else - // try { - // throw SharedMemoryError("Could not create shared memory"); - // } catch (SocketError &e) { - - // std::cout << "Caught Socket error: " << e.what() << '\n'; - - // } catch (...) { - // std::cout << "Caught Something else probably should have let me crash\n"; - // } - - // throw RuntimeError("This one we missed"); return 0; } diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h index 02fadad17..ead706529 100644 --- a/slsSupportLib/include/network_utils.h +++ b/slsSupportLib/include/network_utils.h @@ -1,16 +1,44 @@ #pragma once +#include #include namespace sls { std::string MacAddrToString(uint64_t mac); uint64_t MacStringToUint(std::string mac); - uint32_t IpStringToUint(const char *ipstr); - std::string IpToString(uint32_t ip); std::string IpToHexString(uint32_t ip); - uint32_t HostnameToIp(const char *hostname); +class IpAddr { + private: + uint32_t addr_{0}; + + public: + explicit IpAddr(uint32_t address); + IpAddr(const std::string &address); + std::string str() const; + std::string hex() const; + bool operator==(const IpAddr &other) const { return addr_ == other.addr_; } + 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); + + public: + MacAddr(std::string mac); + std::string str() { return to_hex(':'); } + std::string hex() { return to_hex(); } + bool operator==(const MacAddr &other) const { return addr_ == other.addr_; } + bool operator==(const uint64_t other) const { return addr_ == other; } +}; + +std::ostream &operator<<(std::ostream &out, const IpAddr &addr){ + return out << addr.str(); +} + } // namespace sls diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index cf153fada..6d3a20d89 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -11,8 +11,47 @@ #include #include +#include "network_utils.h" + namespace sls { +IpAddr::IpAddr(uint32_t address) : addr_{address} {} +IpAddr::IpAddr(const std::string &address) { inet_pton(AF_INET, address.c_str(), &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; + } + mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end()); + addr_ = std::stol(mac, nullptr, 16); +} + +std::string MacAddr::to_hex(const char delimiter) { + 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 MacAddrToString(uint64_t mac) { std::ostringstream ss; ss << std::hex << std::setfill('0') << std::setw(2); @@ -44,11 +83,11 @@ std::string IpToString(uint32_t ip) { return ipstring; } -std::string IpToHexString(uint32_t ip){ - std::ostringstream ss; +std::string IpToHexString(uint32_t ip) { + std::ostringstream ss; ss << std::hex << std::setfill('0') << std::setw(2); - for (int i=0; i!=4; ++i){ - ss << ((ip >> i*8) & 0xFF); + for (int i = 0; i != 4; ++i) { + ss << ((ip >> i * 8) & 0xFF); } return ss.str(); } @@ -67,11 +106,4 @@ uint32_t HostnameToIp(const char *hostname) { return ip; } - - } // namespace sls - -// char ipstring[INET_ADDRSTRLEN]; -// inet_ntop(AF_INET, &detector_shm()->detectorIP, ipstring, INET_ADDRSTRLEN); - -// inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP)); \ No newline at end of file From b198b50377b226b85c69cd47ef7e785b667e9232 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 2 Apr 2019 15:24:25 +0200 Subject: [PATCH 07/10] class for Ip and Mac --- integrationTests/src/a.cpp | 10 +- slsDetectorSoftware/include/slsDetector.h | 37 ++--- slsDetectorSoftware/src/multiSlsDetector.cpp | 28 ++-- slsDetectorSoftware/src/slsDetector.cpp | 162 +++++++++---------- slsSupportLib/include/network_utils.h | 20 ++- slsSupportLib/include/string_utils.h | 16 ++ slsSupportLib/src/network_utils.cpp | 13 +- slsSupportLib/src/string_utils.cpp | 17 ++ slsSupportLib/tests/test-network_utils.cpp | 35 ++++ slsSupportLib/tests/test-string_utils.cpp | 4 +- 10 files changed, 204 insertions(+), 138 deletions(-) diff --git a/integrationTests/src/a.cpp b/integrationTests/src/a.cpp index 8c676ea0c..af5cd3f13 100644 --- a/integrationTests/src/a.cpp +++ b/integrationTests/src/a.cpp @@ -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 vec; + vec.push_back(a); return 0; } diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index f1bfabbc2..6b8fcbc54 100644 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -14,6 +14,7 @@ #include "error_defs.h" #include "logger.h" #include "sls_detector_defs.h" +#include "network_utils.h" class ClientInterface; #include @@ -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 diff --git a/slsDetectorSoftware/src/multiSlsDetector.cpp b/slsDetectorSoftware/src/multiSlsDetector.cpp index 11bc0c456..927dfa180 100644 --- a/slsDetectorSoftware/src/multiSlsDetector.cpp +++ b/slsDetectorSoftware/src/multiSlsDetector.cpp @@ -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 #include @@ -19,17 +22,14 @@ #include #include #include -//#include //clock() -#include "container_utils.h" + + #include #include #include -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 diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index cc8e8dab2..3e3d2eed4 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -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: " diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h index ead706529..6da3a7f9c 100644 --- a/slsSupportLib/include/network_utils.h +++ b/slsSupportLib/include/network_utils.h @@ -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 diff --git a/slsSupportLib/include/string_utils.h b/slsSupportLib/include/string_utils.h index cfd50cc1f..293af39d4 100644 --- a/slsSupportLib/include/string_utils.h +++ b/slsSupportLib/include/string_utils.h @@ -2,6 +2,8 @@ #include #include +#include +#include namespace sls { @@ -14,10 +16,18 @@ Still this is better than strcpy and a buffer overflow... */ template 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 +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 &container); +/* +Concatenate vector of things with str method using + if the strings are different +*/ +template +std::string concatenateIfDifferent(const std::vector &container); + /* Convert an ip address string to a string in hex format. (removing dots) */ diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index 6d3a20d89..a3e13aad6 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -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); diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index b8f4b2be0..3bf090577 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -1,6 +1,7 @@ #include "string_utils.h" #include "container_utils.h" +#include "network_utils.h" #include #include #include @@ -38,6 +39,22 @@ std::string concatenateIfDifferent(const std::vector& container) return result; } } +template +std::string concatenateIfDifferent(const std::vector& 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&); +template std::string concatenateIfDifferent(const std::vector&); + std::string stringIpToHex(const std::string& ip) { diff --git a/slsSupportLib/tests/test-network_utils.cpp b/slsSupportLib/tests/test-network_utils.cpp index 7d3ca1ccf..fda6335e0 100644 --- a/slsSupportLib/tests/test-network_utils.cpp +++ b/slsSupportLib/tests/test-network_utils.cpp @@ -21,6 +21,23 @@ TEST_CASE("Convert mac address") { } } +TEST_CASE("Convert mac address using classes") { + + std::vector vec_addr{346856806822, 346856806852, 262027939863028}; + std::vector 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 vec_addr{4073554305, 2747957633, 2697625985}; std::vector 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 vec_addr{4073554305, 2747957633, 2697625985}; + std::vector 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); diff --git a/slsSupportLib/tests/test-string_utils.cpp b/slsSupportLib/tests/test-string_utils.cpp index c4db9c61a..f5c342ec5 100644 --- a/slsSupportLib/tests/test-string_utils.cpp +++ b/slsSupportLib/tests/test-string_utils.cpp @@ -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"); -} \ No newline at end of file +} + +// TEST_CASE("concat things not being strings") \ No newline at end of file From 24f28f14f46b0589ecbc1bdba3e3e04fb4977fc2 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 2 Apr 2019 17:00:00 +0200 Subject: [PATCH 08/10] clean up --- slsDetectorSoftware/src/slsDetector.cpp | 83 +++++++++------------- slsSupportLib/include/network_utils.h | 9 +-- slsSupportLib/src/network_utils.cpp | 59 +++------------ slsSupportLib/tests/test-network_utils.cpp | 45 ++---------- slsSupportLib/tests/test-string_utils.cpp | 6 +- 5 files changed, 53 insertions(+), 149 deletions(-) diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index 3e3d2eed4..db894b727 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -1573,7 +1573,7 @@ int slsDetector::configureMAC() { FILE_LOG(logDEBUG1) << "Configuring MAC"; if (detector_shm()->receiverUDPIP == 0) { // If hostname is valid ip use that, oterwise lookup hostname - detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname); + detector_shm()->receiverUDPIP = detector_shm()->receiver_hostname; if (detector_shm()->receiverUDPIP == 0) { detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname); } @@ -1597,23 +1597,15 @@ int slsDetector::configureMAC() { // 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[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], ':'); + sls::strcpy_safe(args[1], getReceiverUDPIP().str()); //TODO! Why not hex? + sls::strcpy_safe(args[2], getReceiverUDPMAC().hex()); + sls::strcpy_safe(args[3], getDetectorIP().hex()); + sls::strcpy_safe(args[4], getDetectorMAC().hex()); snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2); - // 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 + sls::strcpy_safe(args[7], getReceiverUDPMAC2().hex()); + sls::strcpy_safe(args[8], getDetectorIP2().hex()); + sls::strcpy_safe(args[9], getDetectorMAC2().hex()); snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces); snprintf(args[11], array_size, "%x", detector_shm()->selectedUDPInterface); @@ -2237,48 +2229,43 @@ std::string slsDetector::getReceiverHostname() const { } std::string slsDetector::setReceiverUDPIP(const std::string &udpip) { - - if (udpip.length() && udpip.length() < 16) { - auto ip = IpStringToUint(udpip.c_str()); - if (ip == 0) { - throw ReceiverError("setReceiverUDPIP: UDP IP Address should be " - "VALID and in xxx.xxx.xxx.xxx format"); - } else { - detector_shm()->receiverUDPIP = 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"; - } + auto ip = IpAddr(udpip); + if (ip == 0) { + throw ReceiverError("setReceiverUDPIP: UDP IP Address should be " + "VALID and in xxx.xxx.xxx.xxx format"); + } else { + detector_shm()->receiverUDPIP = 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"; } + return getReceiverUDPIP().str(); } - return getReceiverUDPIP().str(); } sls::IpAddr slsDetector::getReceiverUDPIP() const { return detector_shm()->receiverUDPIP; } std::string slsDetector::setReceiverUDPIP2(const std::string &udpip) { - if (udpip.length() && udpip.length() < 16) { - auto ip = IpStringToUint(udpip.c_str()); - if (ip == 0) { - throw ReceiverError("setReceiverUDPIP: UDP IP Address 2 should be " - "VALID and in xxx.xxx.xxx.xxx format"); - } else { - detector_shm()->receiverUDPIP2 = 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"; - } + auto ip = IpAddr(udpip); + if (ip == 0) { + throw ReceiverError("setReceiverUDPIP: UDP IP Address 2 should be " + "VALID and in xxx.xxx.xxx.xxx format"); + } else { + detector_shm()->receiverUDPIP2 = 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"; } + return getReceiverUDPIP2().str(); } - return getReceiverUDPIP2().str(); } sls::IpAddr slsDetector::getReceiverUDPIP2() const { return detector_shm()->receiverUDPIP2; } std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) { - auto mac = MacStringToUint(udpmac); + auto mac = MacAddr(udpmac); if (mac == 0) { throw ReceiverError("Could not decode UDPMAC from: " + udpmac); } @@ -2289,7 +2276,7 @@ std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) { MacAddr slsDetector::getReceiverUDPMAC() const { return detector_shm()->receiverUDPMAC; } std::string slsDetector::setReceiverUDPMAC2(const std::string &udpmac) { - auto mac = MacStringToUint(udpmac); + auto mac = MacAddr(udpmac); if (mac == 0) { throw ReceiverError("Could not decode UDPMA2C from: " + udpmac); } @@ -2647,7 +2634,7 @@ int slsDetector::setUDPConnection() { if (detector_shm()->receiverUDPIP == 0) { // Hostname could be ip try to decode otherwise look up the hostname - detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname); + detector_shm()->receiverUDPIP = detector_shm()->receiver_hostname; if (detector_shm()->receiverUDPIP == 0) { detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname); } @@ -2681,11 +2668,11 @@ int slsDetector::setUDPConnection() { ret = receiver.sendCommandThenRead(fnum, args, sizeof(args), retvals, sizeof(retvals)); if (strlen(retvals[0])) { FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0]; - detector_shm()->receiverUDPMAC = MacStringToUint(retvals[0]); + detector_shm()->receiverUDPMAC = retvals[0]; } if (strlen(retvals[1])) { FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1]; - detector_shm()->receiverUDPMAC2 = MacStringToUint(retvals[1]); + detector_shm()->receiverUDPMAC2 = retvals[1]; } if (ret == FORCE_UPDATE) { receiver.close(); diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h index 6da3a7f9c..d546486f4 100644 --- a/slsSupportLib/include/network_utils.h +++ b/slsSupportLib/include/network_utils.h @@ -4,11 +4,6 @@ namespace sls { -std::string MacAddrToString(uint64_t mac); -uint64_t MacStringToUint(std::string mac); -uint32_t IpStringToUint(const char *ipstr); -std::string IpToString(uint32_t ip); -std::string IpToHexString(uint32_t ip); uint32_t HostnameToIp(const char *hostname); class IpAddr { @@ -16,7 +11,7 @@ class IpAddr { uint32_t addr_{0}; public: - IpAddr(uint32_t address); + constexpr IpAddr(uint32_t address) noexcept: addr_{address} {} IpAddr(const std::string &address); IpAddr(const char *address); std::string str() const; @@ -33,7 +28,7 @@ class MacAddr { std::string to_hex(const char delimiter = 0) const; public: - MacAddr(uint64_t mac); + constexpr MacAddr(uint64_t mac) noexcept : addr_{mac} {} MacAddr(std::string mac); MacAddr(const char *address); std::string str() const { return to_hex(':'); } diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index a3e13aad6..4eb785f8c 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -15,7 +16,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 { @@ -32,18 +33,18 @@ 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] != ':')) { addr_ = 0; + } else { + mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end()); + addr_ = std::strtoul(mac.c_str(), nullptr, 16); } - 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) const{ +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); @@ -55,53 +56,9 @@ std::string MacAddr::to_hex(const char delimiter) const{ return ss.str(); } -std::ostream &operator<<(std::ostream &out, const IpAddr &addr){ - return out << addr.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); - ss << ((mac >> 40) & 0xFF); - for (int i = 32; i >= 0; i -= 8) { - ss << ':' << ((mac >> i) & 0xFF); - } - return ss.str(); -} - -uint64_t MacStringToUint(std::string mac) { - if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') || - (mac[11] != ':') || (mac[14] != ':')) { - return 0; - } - mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end()); - return std::stol(mac, nullptr, 16); -} - -uint32_t IpStringToUint(const char *ipstr) { - uint32_t ip{0}; - inet_pton(AF_INET, ipstr, &ip); - return ip; -} - -std::string IpToString(uint32_t ip) { - char ipstring[INET_ADDRSTRLEN]{}; - inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN); - return ipstring; -} - -std::string IpToHexString(uint32_t ip) { - std::ostringstream ss; - ss << std::hex << std::setfill('0') << std::setw(2); - for (int i = 0; i != 4; ++i) { - ss << ((ip >> i * 8) & 0xFF); - } - return ss.str(); -} +std::ostream &operator<<(std::ostream &out, const MacAddr &addr) { return out << addr.str(); } uint32_t HostnameToIp(const char *hostname) { struct addrinfo hints, *result; diff --git a/slsSupportLib/tests/test-network_utils.cpp b/slsSupportLib/tests/test-network_utils.cpp index fda6335e0..d2225dea8 100644 --- a/slsSupportLib/tests/test-network_utils.cpp +++ b/slsSupportLib/tests/test-network_utils.cpp @@ -7,19 +7,7 @@ #include "string_utils.h" using namespace sls; -TEST_CASE("Convert mac address") { - std::vector vec_addr{346856806822, 346856806852, 262027939863028}; - std::vector 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 mac = vec_addr[i]; - auto answer = vec_ans[i]; - - std::string string_addr = MacAddrToString(mac); - CHECK(string_addr == answer); - CHECK(MacStringToUint(string_addr) == mac); - } -} TEST_CASE("Convert mac address using classes") { @@ -38,19 +26,6 @@ TEST_CASE("Convert mac address using classes") { } } -TEST_CASE("Convert IP") { - std::vector vec_addr{4073554305, 2747957633, 2697625985}; - std::vector vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"}; - - for (int i = 0; i != vec_addr.size(); ++i) { - auto ip = vec_addr[i]; - auto answer = vec_ans[i]; - - auto string_addr = IpToString(ip); - CHECK(string_addr == answer); - CHECK(IpStringToUint(string_addr.c_str()) == ip); - } -} TEST_CASE("Convert IP using classes ") { std::vector vec_addr{4073554305, 2747957633, 2697625985}; @@ -70,19 +45,7 @@ TEST_CASE("Convert IP using classes ") { } } -TEST_CASE("IP not valid") { - - CHECK(IpStringToUint("hej") == 0); - CHECK(IpStringToUint("mpc2408") == 0); -} - -TEST_CASE("Convert ip to hex") { - std::vector ipstrings{"74.125.43.99", "129.129.202.217"}; - std::vector vec_ans{"4a7d2b63", "8181cad9"}; - for (int i = 0; i != ipstrings.size(); ++i) { - uint32_t ip = IpStringToUint(ipstrings[i].c_str()); - CHECK(IpToHexString(ip) == vec_ans[i]); - } -} - -// TEST_CASE("Lookup ip") \ No newline at end of file +TEST_CASE("Strange input gives 0"){ + CHECK(IpAddr("hej")== 0); + CHECK(MacAddr("hej")== 0); +} \ No newline at end of file diff --git a/slsSupportLib/tests/test-string_utils.cpp b/slsSupportLib/tests/test-string_utils.cpp index f5c342ec5..aedbd2347 100644 --- a/slsSupportLib/tests/test-string_utils.cpp +++ b/slsSupportLib/tests/test-string_utils.cpp @@ -1,6 +1,5 @@ #include "MySocketTCP.h" #include "catch.hpp" -// #include "multiSlsDetector.h" #include "logger.h" #include #include @@ -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 v{"one", "one", "one"}; std::vector v2{"one", "one", "one"}; From 6fc388bf789cbadff6055aa938801c277853a462 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 2 Apr 2019 17:15:21 +0200 Subject: [PATCH 09/10] reset to info --- slsSupportLib/include/logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slsSupportLib/include/logger.h b/slsSupportLib/include/logger.h index 6f1dcb8f1..2be26378a 100644 --- a/slsSupportLib/include/logger.h +++ b/slsSupportLib/include/logger.h @@ -17,7 +17,7 @@ #endif #ifndef FILELOG_MAX_LEVEL -#define FILELOG_MAX_LEVEL logDEBUG1 +#define FILELOG_MAX_LEVEL logINFO #endif From d6c0f7be057aaff0c91c13d32998b6bb5b502100 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 2 Apr 2019 17:59:59 +0200 Subject: [PATCH 10/10] constexpr --- .clang-format | 2 +- slsSupportLib/include/network_utils.h | 18 ++++++------ slsSupportLib/src/network_utils.cpp | 17 ++++++++---- slsSupportLib/src/string_utils.cpp | 40 ++++++++++----------------- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/.clang-format b/.clang-format index 1032b76aa..dae9439c4 100644 --- a/.clang-format +++ b/.clang-format @@ -2,5 +2,5 @@ BasedOnStyle: LLVM IndentWidth: 4 UseTab: Never -ColumnLimit: 100 +ColumnLimit: 80 AlignConsecutiveAssignments: false \ No newline at end of file diff --git a/slsSupportLib/include/network_utils.h b/slsSupportLib/include/network_utils.h index d546486f4..5a759d765 100644 --- a/slsSupportLib/include/network_utils.h +++ b/slsSupportLib/include/network_utils.h @@ -11,15 +11,15 @@ class IpAddr { uint32_t addr_{0}; public: - constexpr IpAddr(uint32_t address) noexcept: addr_{address} {} + 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; - 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; } + 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 { @@ -33,10 +33,10 @@ class MacAddr { 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; } + 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); diff --git a/slsSupportLib/src/network_utils.cpp b/slsSupportLib/src/network_utils.cpp index 4eb785f8c..061837f21 100644 --- a/slsSupportLib/src/network_utils.cpp +++ b/slsSupportLib/src/network_utils.cpp @@ -16,8 +16,9 @@ namespace sls { - -IpAddr::IpAddr(const std::string &address) { inet_pton(AF_INET, address.c_str(), &addr_); } +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]{}; @@ -34,8 +35,8 @@ std::string IpAddr::hex() const { } MacAddr::MacAddr(std::string mac) { - if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') || - (mac[11] != ':') || (mac[14] != ':')) { + 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()); @@ -56,9 +57,13 @@ std::string MacAddr::to_hex(const char delimiter) const { return ss.str(); } -std::ostream &operator<<(std::ostream &out, const IpAddr &addr) { return out << addr.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::ostream &operator<<(std::ostream &out, const MacAddr &addr) { + return out << addr.str(); +} uint32_t HostnameToIp(const char *hostname) { struct addrinfo hints, *result; diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 3bf090577..7c90eab25 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -2,14 +2,12 @@ #include "string_utils.h" #include "container_utils.h" #include "network_utils.h" -#include -#include #include -namespace sls{ +#include +#include +namespace sls { - -std::vector split(const std::string& strToSplit, char delimeter) -{ +std::vector split(const std::string &strToSplit, char delimeter) { std::stringstream ss(strToSplit); std::string item; std::vector splittedStrings; @@ -19,55 +17,47 @@ std::vector split(const std::string& strToSplit, char delimeter) return splittedStrings; } - -std::string concatenateNonEmptyStrings(const std::vector& vec){ +std::string concatenateNonEmptyStrings(const std::vector &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& container) -{ +std::string concatenateIfDifferent(const std::vector &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 -std::string concatenateIfDifferent(const std::vector& container) -{ +template +std::string concatenateIfDifferent(const std::vector &container) { if (allEqual(container)) { return container.front().str(); } else { std::string result; - for (const auto& s : container) + for (const auto &s : container) result += s.str() + '+'; return result; } } -template std::string concatenateIfDifferent(const std::vector&); -template std::string concatenateIfDifferent(const std::vector&); +template std::string concatenateIfDifferent(const std::vector &); +template std::string concatenateIfDifferent(const std::vector &); - -std::string stringIpToHex(const std::string& ip) -{ +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 \ No newline at end of file