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