working on removing undefined beaviour in configureMAC

This commit is contained in:
Erik Frojdh
2019-01-11 16:56:31 +01:00
parent 5ea5e83236
commit b7582e93d3
5 changed files with 56 additions and 45 deletions

View File

@ -842,8 +842,8 @@ int slsDetector::setDetectorType(detectorType const type) {
int slsDetector::setDetectorType(std::string const stype) { int slsDetector::setDetectorType(const std::string& detector_type) {
return setDetectorType(getDetectorType(stype)); return setDetectorType(getDetectorType(detector_type));
} }
@ -2027,18 +2027,18 @@ int slsDetector::readAll() {
int slsDetector::configureMAC() { int slsDetector::configureMAC() {
int fnum = F_CONFIGURE_MAC; int fnum = F_CONFIGURE_MAC;
int ret = FAIL; int ret = FAIL;
char args[9][50]; const size_t array_size = 50;
memset(args, 0, sizeof(args)); const size_t n_args = 9;
char retvals[2][50]; const size_t n_retvals = 2;
memset(retvals, 0, sizeof(retvals)); char args[n_args][array_size] = {};
char retvals[n_retvals][array_size] = {};
FILE_LOG(logDEBUG1) << "Configuring MAC"; FILE_LOG(logDEBUG1) << "Configuring MAC";
// if rx_udpip is none // if rx_udpip is none
if (!(strcmp(thisDetector->receiverUDPIP,"none"))) { if (!(strcmp(thisDetector->receiverUDPIP,"none"))) {
//hostname is an ip address //hostname is an ip address
if (strchr(thisDetector->receiver_hostname,'.') != nullptr) 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 //if hostname not ip, convert it to ip
else { else {
struct addrinfo *result; struct addrinfo *result;
@ -2049,7 +2049,7 @@ int slsDetector::configureMAC() {
// on failure, back to none // on failure, back to none
if (dataSocket->ConvertInternetAddresstoIpString(result, if (dataSocket->ConvertInternetAddresstoIpString(result,
thisDetector->receiverUDPIP, MAX_STR_LENGTH)) { 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 "; FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpip is valid ";
// copy to args // copy to args
strcpy(args[0],thisDetector->receiverUDPIP); sls::strcpy_safe(args[0],thisDetector->receiverUDPIP);
strcpy(args[1],thisDetector->receiverUDPMAC); sls::strcpy_safe(args[1],thisDetector->receiverUDPMAC);
sprintf(args[2],"%x",thisDetector->receiverUDPPort); snprintf(args[2],array_size, "%x",thisDetector->receiverUDPPort);
strcpy(args[3],thisDetector->detectorMAC); sls::strcpy_safe(args[3],thisDetector->detectorMAC);
strcpy(args[4],thisDetector->detectorIP); sls::strcpy_safe(args[4],thisDetector->detectorIP);
sprintf(args[5],"%x",thisDetector->receiverUDPPort2); snprintf(args[5], array_size, "%x",thisDetector->receiverUDPPort2);
// 2d positions to detector to put into udp header // 2d positions to detector to put into udp header
{ {
int pos[3] = {0, 0, 0}; int pos[3] = {0, 0, 0};
@ -2081,60 +2081,47 @@ int slsDetector::configureMAC() {
// pos[2] (z is reserved) // pos[2] (z is reserved)
FILE_LOG(logDEBUG1) << "Detector [" << detId << "] - (" FILE_LOG(logDEBUG1) << "Detector [" << detId << "] - ("
<< pos[0] << "," << pos[1] << ")"; << pos[0] << "," << pos[1] << ")";
sprintf(args[6], "%x", pos[0]); snprintf(args[6], array_size, "%x", pos[0]);
sprintf(args[7], "%x", pos[1]); snprintf(args[7], array_size, "%x", pos[1]);
sprintf(args[8], "%x", pos[2]); snprintf(args[8], array_size, "%x", pos[2]);
} }
{ {
//converting IPaddress to std::hex //converting IPaddress to std::hex
std::stringstream ss(args[0]); const std::string cword = sls::stringIpToHex(args[0]);
char cword[50]=""; sls::strcpy_safe(args[0],cword.c_str());
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);
FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-"; FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-";
} }
{ {
//converting MACaddress to std::hex //converting MACaddress to std::hex
std::stringstream ss(args[1]); std::stringstream ss(args[1]);
char cword[50]=""; char cword[array_size] = {};
bzero(cword, 50);
std::string s; std::string s;
while (getline(ss, s, ':')) { while (getline(ss, s, ':')) {
sprintf(cword,"%s%s",cword,s.c_str()); sprintf(cword,"%s%s",cword,s.c_str());
} }
bzero(args[1], 50); sls::strcpy_safe(args[1],cword);
strcpy(args[1],cword);
FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-"; FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-";
} }
FILE_LOG(logDEBUG1) << "receiver udp port:" << args[2] << "-"; FILE_LOG(logDEBUG1) << "receiver udp port:" << args[2] << "-";
{ {
std::stringstream ss(args[3]); std::stringstream ss(args[3]);
char cword[50]=""; char cword[array_size] = {};
bzero(cword, 50);
std::string s; std::string s;
while (getline(ss, s, ':')) { while (getline(ss, s, ':')) {
sprintf(cword,"%s%s",cword,s.c_str()); sprintf(cword,"%s%s",cword,s.c_str());
} }
bzero(args[3], 50); sls::strcpy_safe(args[3],cword);
strcpy(args[3],cword); FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-";
FILE_LOG(logDEBUG1) << "detecotor udp mac:" << args[3] << "-";
} }
{ {
//converting IPaddress to std::hex //converting IPaddress to std::hex
std::stringstream ss(args[4]); std::stringstream ss(args[4]);
char cword[50]=""; char cword[50] = {};
bzero(cword, 50);
std::string s; std::string s;
while (getline(ss, s, '.')) { while (getline(ss, s, '.')) {
sprintf(cword,"%s%02x",cword,atoi(s.c_str())); sprintf(cword,"%s%02x",cword,atoi(s.c_str()));
} }
bzero(args[4], 50); sls::strcpy_safe(args[4],cword);
strcpy(args[4],cword);
FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-"; FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-";
} }
FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-"; FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-";
@ -2171,14 +2158,14 @@ int slsDetector::configureMAC() {
(idetectorip)&0xff); (idetectorip)&0xff);
// update if different // update if different
if (strcasecmp(retvals[0],thisDetector->detectorMAC)) { if (strcasecmp(retvals[0],thisDetector->detectorMAC)) {
memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH); // memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH);
strcpy(thisDetector->detectorMAC, retvals[0]); sls::strcpy_safe(thisDetector->detectorMAC, retvals[0]);
FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << FILE_LOG(logINFO) << detId << ": Detector MAC updated to " <<
thisDetector->detectorMAC; thisDetector->detectorMAC;
} }
if (strcasecmp(retvals[1],thisDetector->detectorIP)) { if (strcasecmp(retvals[1],thisDetector->detectorIP)) {
memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH); // memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH);
strcpy(thisDetector->detectorIP, retvals[1]); sls::strcpy_safe(thisDetector->detectorIP, retvals[1]);
FILE_LOG(logINFO) << detId << ": Detector IP updated to " << FILE_LOG(logINFO) << detId << ": Detector IP updated to " <<
thisDetector->detectorIP; thisDetector->detectorIP;
} }

View File

@ -395,7 +395,7 @@ public:
* @param type string of detector type * @param type string of detector type
* @returns detector type in receiver * @returns detector type in receiver
*/ */
int setDetectorType(std::string stype); int setDetectorType(const std::string& detector_type);
/** /**
* Get Detector type from shared memory variable * Get Detector type from shared memory variable

View File

@ -35,4 +35,9 @@ Concatenate strings using + if the strings are different
*/ */
std::string concatenateIfDifferent(std::vector<std::string> container); std::string concatenateIfDifferent(std::vector<std::string> container);
/*
Convert an ip address string to a string in hex format. (removing dots)
*/
std::string stringIpToHex(const std::string& ip);
}; // namespace sls }; // namespace sls

View File

@ -2,6 +2,7 @@
#include "string_utils.h" #include "string_utils.h"
#include "container_utils.h" #include "container_utils.h"
#include <sstream> #include <sstream>
#include <iomanip>
namespace sls{ namespace sls{
@ -37,5 +38,17 @@ std::string concatenateIfDifferent(std::vector<std::string> 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 }; // namespace sls

View File

@ -87,4 +87,10 @@ TEST_CASE("concatenate non empty strings with one element"){
REQUIRE(vec.size()==5); REQUIRE(vec.size()==5);
auto ret = sls::concatenateNonEmptyStrings(vec); auto ret = sls::concatenateNonEmptyStrings(vec);
REQUIRE(ret=="hej+"); REQUIRE(ret=="hej+");
}
TEST_CASE("Convert ip address"){
std::string address = "101.255.103.1";
REQUIRE(sls::stringIpToHex(address) == "65ff6701");
} }