diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 474066edf..0cfbe88d7 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -842,8 +842,8 @@ int slsDetector::setDetectorType(detectorType const type) { -int slsDetector::setDetectorType(std::string const stype) { - return setDetectorType(getDetectorType(stype)); +int slsDetector::setDetectorType(const std::string& detector_type) { + return setDetectorType(getDetectorType(detector_type)); } @@ -2027,18 +2027,18 @@ int slsDetector::readAll() { int slsDetector::configureMAC() { int fnum = F_CONFIGURE_MAC; int ret = FAIL; - char args[9][50]; - memset(args, 0, sizeof(args)); - char retvals[2][50]; - memset(retvals, 0, sizeof(retvals)); + const size_t array_size = 50; + const size_t n_args = 9; + const size_t n_retvals = 2; + char args[n_args][array_size] = {}; + char retvals[n_retvals][array_size] = {}; FILE_LOG(logDEBUG1) << "Configuring MAC"; - // if rx_udpip is none if (!(strcmp(thisDetector->receiverUDPIP,"none"))) { //hostname is an ip address if (strchr(thisDetector->receiver_hostname,'.') != nullptr) - strcpy(thisDetector->receiverUDPIP, thisDetector->receiver_hostname); + sls::strcpy_safe(thisDetector->receiverUDPIP, thisDetector->receiver_hostname); //if hostname not ip, convert it to ip else { struct addrinfo *result; @@ -2049,7 +2049,7 @@ int slsDetector::configureMAC() { // on failure, back to none if (dataSocket->ConvertInternetAddresstoIpString(result, thisDetector->receiverUDPIP, MAX_STR_LENGTH)) { - strcpy(thisDetector->receiverUDPIP, "none"); + sls::strcpy_safe(thisDetector->receiverUDPIP, "none"); } } } @@ -2064,12 +2064,12 @@ int slsDetector::configureMAC() { FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpip is valid "; // copy to args - strcpy(args[0],thisDetector->receiverUDPIP); - strcpy(args[1],thisDetector->receiverUDPMAC); - sprintf(args[2],"%x",thisDetector->receiverUDPPort); - strcpy(args[3],thisDetector->detectorMAC); - strcpy(args[4],thisDetector->detectorIP); - sprintf(args[5],"%x",thisDetector->receiverUDPPort2); + sls::strcpy_safe(args[0],thisDetector->receiverUDPIP); + sls::strcpy_safe(args[1],thisDetector->receiverUDPMAC); + snprintf(args[2],array_size, "%x",thisDetector->receiverUDPPort); + sls::strcpy_safe(args[3],thisDetector->detectorMAC); + sls::strcpy_safe(args[4],thisDetector->detectorIP); + snprintf(args[5], array_size, "%x",thisDetector->receiverUDPPort2); // 2d positions to detector to put into udp header { int pos[3] = {0, 0, 0}; @@ -2081,60 +2081,47 @@ int slsDetector::configureMAC() { // pos[2] (z is reserved) FILE_LOG(logDEBUG1) << "Detector [" << detId << "] - (" << pos[0] << "," << pos[1] << ")"; - sprintf(args[6], "%x", pos[0]); - sprintf(args[7], "%x", pos[1]); - sprintf(args[8], "%x", pos[2]); + snprintf(args[6], array_size, "%x", pos[0]); + snprintf(args[7], array_size, "%x", pos[1]); + snprintf(args[8], array_size, "%x", pos[2]); } { //converting IPaddress to std::hex - std::stringstream ss(args[0]); - char cword[50]=""; - bzero(cword, 50); - std::string s; - while (getline(ss, s, '.')) { - sprintf(cword,"%s%02x",cword,atoi(s.c_str())); - } - bzero(args[0], 50); - strcpy(args[0],cword); + const std::string cword = sls::stringIpToHex(args[0]); + sls::strcpy_safe(args[0],cword.c_str()); FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-"; } { //converting MACaddress to std::hex std::stringstream ss(args[1]); - char cword[50]=""; - bzero(cword, 50); + char cword[array_size] = {}; std::string s; while (getline(ss, s, ':')) { sprintf(cword,"%s%s",cword,s.c_str()); } - bzero(args[1], 50); - strcpy(args[1],cword); + sls::strcpy_safe(args[1],cword); FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-"; } FILE_LOG(logDEBUG1) << "receiver udp port:" << args[2] << "-"; { std::stringstream ss(args[3]); - char cword[50]=""; - bzero(cword, 50); + char cword[array_size] = {}; std::string s; while (getline(ss, s, ':')) { sprintf(cword,"%s%s",cword,s.c_str()); } - bzero(args[3], 50); - strcpy(args[3],cword); - FILE_LOG(logDEBUG1) << "detecotor udp mac:" << args[3] << "-"; + sls::strcpy_safe(args[3],cword); + FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-"; } { //converting IPaddress to std::hex std::stringstream ss(args[4]); - char cword[50]=""; - bzero(cword, 50); + char cword[50] = {}; std::string s; while (getline(ss, s, '.')) { sprintf(cword,"%s%02x",cword,atoi(s.c_str())); } - bzero(args[4], 50); - strcpy(args[4],cword); + sls::strcpy_safe(args[4],cword); FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-"; } FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-"; @@ -2171,14 +2158,14 @@ int slsDetector::configureMAC() { (idetectorip)&0xff); // update if different if (strcasecmp(retvals[0],thisDetector->detectorMAC)) { - memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH); - strcpy(thisDetector->detectorMAC, retvals[0]); + // memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH); + sls::strcpy_safe(thisDetector->detectorMAC, retvals[0]); FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << thisDetector->detectorMAC; } if (strcasecmp(retvals[1],thisDetector->detectorIP)) { - memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH); - strcpy(thisDetector->detectorIP, retvals[1]); + // memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH); + sls::strcpy_safe(thisDetector->detectorIP, retvals[1]); FILE_LOG(logINFO) << detId << ": Detector IP updated to " << thisDetector->detectorIP; } diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index cbacbc1cf..05b5232b4 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -395,7 +395,7 @@ public: * @param type string of detector type * @returns detector type in receiver */ - int setDetectorType(std::string stype); + int setDetectorType(const std::string& detector_type); /** * Get Detector type from shared memory variable diff --git a/slsSupportLib/include/string_utils.h b/slsSupportLib/include/string_utils.h index 37ddc588d..fd35f1253 100644 --- a/slsSupportLib/include/string_utils.h +++ b/slsSupportLib/include/string_utils.h @@ -35,4 +35,9 @@ Concatenate strings using + if the strings are different */ std::string concatenateIfDifferent(std::vector container); +/* +Convert an ip address string to a string in hex format. (removing dots) +*/ +std::string stringIpToHex(const std::string& ip); + }; // namespace sls diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 341c1cf45..ae6b24c2d 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -2,6 +2,7 @@ #include "string_utils.h" #include "container_utils.h" #include +#include namespace sls{ @@ -37,5 +38,17 @@ std::string concatenateIfDifferent(std::vector container) } } +std::string stringIpToHex(const std::string& ip) +{ + std::istringstream iss(ip); + std::ostringstream oss; + std::string 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 diff --git a/tests/src/test-string_utils.cpp b/tests/src/test-string_utils.cpp index 8f27294dc..1437a81fb 100644 --- a/tests/src/test-string_utils.cpp +++ b/tests/src/test-string_utils.cpp @@ -87,4 +87,10 @@ TEST_CASE("concatenate non empty strings with one element"){ REQUIRE(vec.size()==5); auto ret = sls::concatenateNonEmptyStrings(vec); REQUIRE(ret=="hej+"); +} + +TEST_CASE("Convert ip address"){ + std::string address = "101.255.103.1"; + REQUIRE(sls::stringIpToHex(address) == "65ff6701"); + } \ No newline at end of file