Merge branch 'refactor' into networkParameterFix

This commit is contained in:
2019-01-15 12:12:53 +01:00
6 changed files with 149 additions and 128 deletions

View File

@ -359,8 +359,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->shmversion = SLS_SHMVERSION; thisDetector->shmversion = SLS_SHMVERSION;
thisDetector->onlineFlag = OFFLINE_FLAG; thisDetector->onlineFlag = OFFLINE_FLAG;
thisDetector->stoppedFlag = 0; thisDetector->stoppedFlag = 0;
strncpy(thisDetector->hostname, DEFAULT_HOSTNAME, MAX_STR_LENGTH-1); sls::strcpy_safe(thisDetector->hostname, DEFAULT_HOSTNAME);
thisDetector->hostname[MAX_STR_LENGTH-1] = 0;
thisDetector->myDetectorType = type; thisDetector->myDetectorType = type;
thisDetector->offset[X] = 0; thisDetector->offset[X] = 0;
thisDetector->offset[Y] = 0; thisDetector->offset[Y] = 0;
@ -368,8 +367,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->multiSize[Y] = 0; thisDetector->multiSize[Y] = 0;
thisDetector->controlPort = DEFAULT_PORTNO; thisDetector->controlPort = DEFAULT_PORTNO;
thisDetector->stopPort = DEFAULT_PORTNO + 1; thisDetector->stopPort = DEFAULT_PORTNO + 1;
strncpy(thisDetector->settingsDir, getenv("HOME"), MAX_STR_LENGTH-1); sls::strcpy_safe(thisDetector->settingsDir, getenv("HOME"));
thisDetector->settingsDir[MAX_STR_LENGTH-1] = 0;
thisDetector->nTrimEn = 0; thisDetector->nTrimEn = 0;
for(int & trimEnergie : thisDetector->trimEnergies) for(int & trimEnergie : thisDetector->trimEnergies)
trimEnergie = 0; trimEnergie = 0;
@ -377,7 +375,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->nROI = 0; thisDetector->nROI = 0;
memset(thisDetector->roiLimits, 0, MAX_ROIS * sizeof(ROI)); memset(thisDetector->roiLimits, 0, MAX_ROIS * sizeof(ROI));
thisDetector->roFlags = NORMAL_READOUT; thisDetector->roFlags = NORMAL_READOUT;
strcpy(thisDetector->settingsFile, "none"); sls::strcpy_safe(thisDetector->settingsFile, "none");
thisDetector->currentSettings = UNINITIALIZED; thisDetector->currentSettings = UNINITIALIZED;
thisDetector->currentThresholdEV = -1; thisDetector->currentThresholdEV = -1;
thisDetector->timerValue[FRAME_NUMBER] = 1; thisDetector->timerValue[FRAME_NUMBER] = 1;
@ -396,16 +394,14 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->timerValue[SUBFRAME_ACQUISITION_TIME] = 0; thisDetector->timerValue[SUBFRAME_ACQUISITION_TIME] = 0;
thisDetector->timerValue[STORAGE_CELL_NUMBER] = 0; thisDetector->timerValue[STORAGE_CELL_NUMBER] = 0;
thisDetector->timerValue[SUBFRAME_DEADTIME] = 0; thisDetector->timerValue[SUBFRAME_DEADTIME] = 0;
strcpy(thisDetector->receiver_hostname, "none"); sls::strcpy_safe(thisDetector->receiver_hostname, "none");
thisDetector->receiverTCPPort = DEFAULT_PORTNO+2; thisDetector->receiverTCPPort = DEFAULT_PORTNO+2;
thisDetector->receiverUDPPort = DEFAULT_UDP_PORTNO; thisDetector->receiverUDPPort = DEFAULT_UDP_PORTNO;
thisDetector->receiverUDPPort2 = DEFAULT_UDP_PORTNO + 1; thisDetector->receiverUDPPort2 = DEFAULT_UDP_PORTNO + 1;
strcpy(thisDetector->receiverUDPIP, "none"); sls::strcpy_safe(thisDetector->receiverUDPIP, "none");
strcpy(thisDetector->receiverUDPMAC, "none"); sls::strcpy_safe(thisDetector->receiverUDPMAC, "none");
strncpy(thisDetector->detectorMAC, DEFAULT_DET_MAC, MAX_STR_LENGTH-1); sls::strcpy_safe(thisDetector->detectorMAC, DEFAULT_DET_MAC);
thisDetector->detectorMAC[MAX_STR_LENGTH-1] = 0; sls::strcpy_safe(thisDetector->detectorIP, DEFAULT_DET_IP);
strncpy(thisDetector->detectorIP, DEFAULT_DET_IP, MAX_STR_LENGTH-1);
thisDetector->detectorIP[MAX_STR_LENGTH-1] = 0;
thisDetector->receiverOnlineFlag = OFFLINE_FLAG; thisDetector->receiverOnlineFlag = OFFLINE_FLAG;
thisDetector->tenGigaEnable = 0; thisDetector->tenGigaEnable = 0;
thisDetector->flippedData[X] = 0; thisDetector->flippedData[X] = 0;
@ -428,8 +424,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->activated = true; thisDetector->activated = true;
thisDetector->receiver_deactivatedPaddingEnable = true; thisDetector->receiver_deactivatedPaddingEnable = true;
thisDetector->receiver_silentMode = false; thisDetector->receiver_silentMode = false;
strcpy(thisDetector->receiver_filePath, "/"); sls::strcpy_safe(thisDetector->receiver_filePath, "/");
strcpy(thisDetector->receiver_fileName, "run"); sls::strcpy_safe(thisDetector->receiver_fileName, "run");
thisDetector->receiver_fileIndex = 0; thisDetector->receiver_fileIndex = 0;
thisDetector->receiver_fileFormatType = BINARY; thisDetector->receiver_fileFormatType = BINARY;
switch(thisDetector->myDetectorType) { switch(thisDetector->myDetectorType) {
@ -842,8 +838,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 +2023,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 +2045,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 +2060,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,62 +2077,27 @@ 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
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);
FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-";
}
{
//converting MACaddress to std::hex
std::stringstream ss(args[1]);
char cword[50]="";
bzero(cword, 50);
std::string s;
while (getline(ss, s, ':')) {
sprintf(cword,"%s%s",cword,s.c_str());
}
bzero(args[1], 50);
strcpy(args[1],cword);
FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-";
} }
//converting receiverUDPIP to string hex
sls::strcpy_safe(args[0],sls::stringIpToHex(args[0]).c_str());
FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-";
//MAC already in hex removing :
sls::removeChar(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]); // MAC already in hex removing :
char cword[50]=""; sls::removeChar(args[3], ':');
bzero(cword, 50); FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-";
std::string s; //converting detectorIP to string hex
while (getline(ss, s, ':')) { sls::strcpy_safe(args[4],sls::stringIpToHex(args[4]).c_str());
sprintf(cword,"%s%s",cword,s.c_str()); FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-";
}
bzero(args[3], 50);
strcpy(args[3],cword);
FILE_LOG(logDEBUG1) << "detecotor udp mac:" << args[3] << "-";
}
{
//converting IPaddress to std::hex
std::stringstream ss(args[4]);
char cword[50]="";
bzero(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);
FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-";
}
FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-"; FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-";
FILE_LOG(logDEBUG1) << "row:" << args[6] << "-"; FILE_LOG(logDEBUG1) << "row:" << args[6] << "-";
FILE_LOG(logDEBUG1) << "col:" << args[7] << "-"; FILE_LOG(logDEBUG1) << "col:" << args[7] << "-";
@ -2157,28 +2118,28 @@ int slsDetector::configureMAC() {
uint32_t idetectorip = 0; uint32_t idetectorip = 0;
sscanf(retvals[0], "%lx", &idetectormac); sscanf(retvals[0], "%lx", &idetectormac);
sscanf(retvals[1], "%x", &idetectorip); sscanf(retvals[1], "%x", &idetectorip);
sprintf(retvals[0],"%02x:%02x:%02x:%02x:%02x:%02x", snprintf(retvals[0], sizeof(retvals[0]), "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned int)((idetectormac>>40)&0xFF), (unsigned int)((idetectormac>>40)&0xFF),
(unsigned int)((idetectormac>>32)&0xFF), (unsigned int)((idetectormac>>32)&0xFF),
(unsigned int)((idetectormac>>24)&0xFF), (unsigned int)((idetectormac>>24)&0xFF),
(unsigned int)((idetectormac>>16)&0xFF), (unsigned int)((idetectormac>>16)&0xFF),
(unsigned int)((idetectormac>>8)&0xFF), (unsigned int)((idetectormac>>8)&0xFF),
(unsigned int)((idetectormac>>0)&0xFF)); (unsigned int)((idetectormac>>0)&0xFF));
sprintf(retvals[1],"%d.%d.%d.%d", snprintf(retvals[1],sizeof(retvals[1]), "%d.%d.%d.%d",
(idetectorip>>24)&0xff, (idetectorip>>24)&0xff,
(idetectorip>>16)&0xff, (idetectorip>>16)&0xff,
(idetectorip>>8)&0xff, (idetectorip>>8)&0xff,
(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;
} }
@ -2673,7 +2634,7 @@ std::string slsDetector::setDetectorMAC(const std::string& detectorMAC) {
} }
// valid format // valid format
else { else {
strcpy(thisDetector->detectorMAC, detectorMAC.c_str()); sls::strcpy_safe(thisDetector->detectorMAC, detectorMAC.c_str());
if (!strcmp(thisDetector->receiver_hostname, "none")) { if (!strcmp(thisDetector->receiver_hostname, "none")) {
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
} else if (setUDPConnection() == FAIL) { } else if (setUDPConnection() == FAIL) {
@ -2699,7 +2660,7 @@ std::string slsDetector::setDetectorIP(const std::string& detectorIP) {
} }
// valid format // valid format
else { else {
strcpy(thisDetector->detectorIP, detectorIP.c_str()); sls::strcpy_safe(thisDetector->detectorIP, detectorIP.c_str());
if (!strcmp(thisDetector->receiver_hostname, "none")) { if (!strcmp(thisDetector->receiver_hostname, "none")) {
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
} else if (setUDPConnection() == FAIL) { } else if (setUDPConnection() == FAIL) {
@ -2732,7 +2693,7 @@ std::string slsDetector::setReceiver(const std::string& receiverIP) {
updateDetector(); updateDetector();
// start updating // start updating
strcpy(thisDetector->receiver_hostname, receiverIP.c_str()); sls::strcpy_safe(thisDetector->receiver_hostname, receiverIP.c_str());
if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) { if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) {
FILE_LOG(logDEBUG1) << FILE_LOG(logDEBUG1) <<
@ -2856,12 +2817,12 @@ std::string slsDetector::setReceiverUDPMAC(const std::string& udpmac) {
} }
// valid format // valid format
else { else {
strcpy(thisDetector->receiverUDPMAC, udpmac.c_str()); sls::strcpy_safe(thisDetector->receiverUDPMAC, udpmac.c_str());
if (!strcmp(thisDetector->receiver_hostname, "none")) { if (!strcmp(thisDetector->receiver_hostname, "none")) {
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
} }
// not doing setUDPConnection as rx_udpmac will get replaced,(must use configuremac) // not doing setUDPConnection as rx_udpmac will get replaced,(must use configuremac)
strcpy(thisDetector->receiverUDPMAC,udpmac.c_str()); sls::strcpy_safe(thisDetector->receiverUDPMAC,udpmac.c_str());
} }
return std::string(thisDetector->receiverUDPMAC); return std::string(thisDetector->receiverUDPMAC);
} }
@ -2990,10 +2951,10 @@ void slsDetector::setReceiverStreamingIP(std::string sourceIP) {
// set it anyway, else it is lost if rx_hostname is not set yet // set it anyway, else it is lost if rx_hostname is not set yet
memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH); memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiver_zmqip, args); sls::strcpy_safe(thisDetector->receiver_zmqip, args);
// if zmqip is empty, update it // if zmqip is empty, update it
if (! strlen(thisDetector->zmqip)) if (! strlen(thisDetector->zmqip))
strcpy(thisDetector->zmqip, args); sls::strcpy_safe(thisDetector->zmqip, args);
FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: " << args; FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: " << args;
// send to receiver // send to receiver
@ -3007,7 +2968,7 @@ void slsDetector::setReceiverStreamingIP(std::string sourceIP) {
} else { } else {
FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retvals; FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retvals;
memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH); memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiver_zmqip, retvals); sls::strcpy_safe(thisDetector->receiver_zmqip, retvals);
if (ret == FORCE_UPDATE) if (ret == FORCE_UPDATE)
ret = updateReceiver(); ret = updateReceiver();
} }
@ -3060,7 +3021,7 @@ std::string slsDetector::setAdditionalJsonHeader(const std::string& jsonheader)
} else { } else {
FILE_LOG(logDEBUG1) << "Additional json header: " << retvals; FILE_LOG(logDEBUG1) << "Additional json header: " << retvals;
memset(thisDetector->receiver_additionalJsonHeader, 0, MAX_STR_LENGTH); memset(thisDetector->receiver_additionalJsonHeader, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiver_additionalJsonHeader, retvals); sls::strcpy_safe(thisDetector->receiver_additionalJsonHeader, retvals);
if (ret == FORCE_UPDATE) if (ret == FORCE_UPDATE)
ret = updateReceiver(); ret = updateReceiver();
} }
@ -3128,7 +3089,7 @@ int slsDetector::setUDPConnection() {
int fnum = F_SETUP_RECEIVER_UDP; int fnum = F_SETUP_RECEIVER_UDP;
int ret = FAIL; int ret = FAIL;
char args[3][MAX_STR_LENGTH] = {{""}, {""}, {""}};; char args[3][MAX_STR_LENGTH] = {{""}, {""}, {""}};;
char retvals[MAX_STR_LENGTH] = {""}; char retvals[MAX_STR_LENGTH] = {};
FILE_LOG(logDEBUG1) << "Setting UDP Connection"; FILE_LOG(logDEBUG1) << "Setting UDP Connection";
// called before set up // called before set up
@ -3140,7 +3101,7 @@ int slsDetector::setUDPConnection() {
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;
@ -3151,15 +3112,15 @@ int slsDetector::setUDPConnection() {
// 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");
} }
} }
} }
} }
//copy arguments to args[][] //copy arguments to args[][]
strcpy(args[0],thisDetector->receiverUDPIP); sls::strcpy_safe(args[0],thisDetector->receiverUDPIP);
sprintf(args[1],"%d",thisDetector->receiverUDPPort); snprintf(args[1], sizeof(args[2]), "%d",thisDetector->receiverUDPPort);
sprintf(args[2],"%d",thisDetector->receiverUDPPort2); snprintf(args[2], sizeof(args[2]), "%d",thisDetector->receiverUDPPort2);
FILE_LOG(logDEBUG1) << "Receiver udp ip address: " << thisDetector->receiverUDPIP; FILE_LOG(logDEBUG1) << "Receiver udp ip address: " << thisDetector->receiverUDPIP;
FILE_LOG(logDEBUG1) << "Receiver udp port: " << thisDetector->receiverUDPPort; FILE_LOG(logDEBUG1) << "Receiver udp port: " << thisDetector->receiverUDPPort;
FILE_LOG(logDEBUG1) << "Receiver udp port2: " << thisDetector->receiverUDPPort2; FILE_LOG(logDEBUG1) << "Receiver udp port2: " << thisDetector->receiverUDPPort2;
@ -3174,7 +3135,7 @@ int slsDetector::setUDPConnection() {
} else { } else {
FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals; FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals;
memset(thisDetector->receiverUDPMAC, 0, MAX_STR_LENGTH); memset(thisDetector->receiverUDPMAC, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiverUDPMAC, retvals); sls::strcpy_safe(thisDetector->receiverUDPMAC, retvals);
if (ret == FORCE_UPDATE) if (ret == FORCE_UPDATE)
ret = updateReceiver(); ret = updateReceiver();
@ -4418,11 +4379,11 @@ int slsDetector::setReceiverTCPSocket(const std::string& name, int const receiv
thisDetector->receiverOnlineFlag = OFFLINE_FLAG; thisDetector->receiverOnlineFlag = OFFLINE_FLAG;
return FAIL; return FAIL;
} }
strcpy(thisName,thisDetector->receiver_hostname); sls::strcpy_safe(thisName,thisDetector->receiver_hostname);
} else { } else {
FILE_LOG(logDEBUG1) << "Setting rx_hostname"; FILE_LOG(logDEBUG1) << "Setting rx_hostname";
strcpy(thisName, name.c_str()); sls::strcpy_safe(thisName, name.c_str());
strcpy(thisDetector->receiver_hostname, thisName); sls::strcpy_safe(thisDetector->receiver_hostname, thisName);
if (dataSocket) { if (dataSocket) {
delete dataSocket; delete dataSocket;
dataSocket = nullptr; dataSocket = nullptr;
@ -4539,7 +4500,7 @@ int slsDetector::execReceiverCommand(const std::string& cmd) {
int ret = FAIL; int ret = FAIL;
char arg[MAX_STR_LENGTH] = {0}; char arg[MAX_STR_LENGTH] = {0};
char retval[MAX_STR_LENGTH] = {0}; char retval[MAX_STR_LENGTH] = {0};
strcpy(arg, cmd.c_str()); sls::strcpy_safe(arg, cmd.c_str());
FILE_LOG(logDEBUG1) << "Sending command to receiver: " << arg; FILE_LOG(logDEBUG1) << "Sending command to receiver: " << arg;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -4560,19 +4521,19 @@ int slsDetector::execReceiverCommand(const std::string& cmd) {
int slsDetector::updateReceiverNoWait() { int slsDetector::updateReceiverNoWait() {
int n = 0, i32 = 0; int n = 0, i32 = 0;
char cstring[MAX_STR_LENGTH] = {0}; char cstring[MAX_STR_LENGTH] = {};
char lastClientIP[INET_ADDRSTRLEN] = {0}; char lastClientIP[INET_ADDRSTRLEN] = {};
n += dataSocket->ReceiveDataOnly(lastClientIP, sizeof(lastClientIP)); n += dataSocket->ReceiveDataOnly(lastClientIP, sizeof(lastClientIP));
FILE_LOG(logDEBUG1) << "Updating receiver last modified by " << lastClientIP; FILE_LOG(logDEBUG1) << "Updating receiver last modified by " << lastClientIP;
// filepath // filepath
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_filePath, cstring); sls::strcpy_safe(thisDetector->receiver_filePath, cstring);
// filename // filename
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_fileName, cstring); sls::strcpy_safe(thisDetector->receiver_fileName, cstring);
// index // index
n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32)); n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32));
@ -4616,11 +4577,11 @@ int slsDetector::updateReceiverNoWait() {
// streaming source ip // streaming source ip
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_zmqip, cstring); sls::strcpy_safe(thisDetector->receiver_zmqip, cstring);
// additional json header // additional json header
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_additionalJsonHeader, cstring); sls::strcpy_safe(thisDetector->receiver_additionalJsonHeader, cstring);
// receiver streaming enable // receiver streaming enable
n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32)); n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32));
@ -4715,9 +4676,9 @@ void slsDetector::setDetectorId() {
void slsDetector::setDetectorHostname() { void slsDetector::setDetectorHostname() {
int fnum = F_SEND_RECEIVER_DETHOSTNAME; int fnum = F_SEND_RECEIVER_DETHOSTNAME;
int ret = FAIL; int ret = FAIL;
char args[MAX_STR_LENGTH] = {0}; char args[MAX_STR_LENGTH] = {};
char retvals[MAX_STR_LENGTH] = {0}; char retvals[MAX_STR_LENGTH] = {};
strcpy(args, thisDetector->hostname); sls::strcpy_safe(args, thisDetector->hostname);
FILE_LOG(logDEBUG1) << "Sending detector hostname to receiver: " << args; FILE_LOG(logDEBUG1) << "Sending detector hostname to receiver: " << args;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -4745,9 +4706,9 @@ std::string slsDetector::setFilePath(const std::string& path) {
if (!path.empty()) { if (!path.empty()) {
int fnum = F_SET_RECEIVER_FILE_PATH; int fnum = F_SET_RECEIVER_FILE_PATH;
int ret = FAIL; int ret = FAIL;
char args[MAX_STR_LENGTH] = {0}; char args[MAX_STR_LENGTH] = {};
char retvals[MAX_STR_LENGTH] = {0}; char retvals[MAX_STR_LENGTH] = {};
strcpy(args, path.c_str()); sls::strcpy_safe(args, path.c_str());
FILE_LOG(logDEBUG1) << "Sending file path to receiver: " << args; FILE_LOG(logDEBUG1) << "Sending file path to receiver: " << args;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -4763,7 +4724,7 @@ std::string slsDetector::setFilePath(const std::string& path) {
setErrorMask((getErrorMask())|(RECEIVER_PARAMETER_NOT_SET)); setErrorMask((getErrorMask())|(RECEIVER_PARAMETER_NOT_SET));
} else { } else {
FILE_LOG(logDEBUG1) << "Receiver file path: " << retvals; FILE_LOG(logDEBUG1) << "Receiver file path: " << retvals;
strcpy(thisDetector->receiver_filePath, retvals); sls::strcpy_safe(thisDetector->receiver_filePath, retvals);
if (ret == FORCE_UPDATE) if (ret == FORCE_UPDATE)
ret = updateReceiver(); ret = updateReceiver();
} }
@ -4784,7 +4745,7 @@ std::string slsDetector::setFileName(const std::string& fname) {
int ret = FAIL; int ret = FAIL;
char args[MAX_STR_LENGTH] = {""}; char args[MAX_STR_LENGTH] = {""};
char retvals[MAX_STR_LENGTH] = {""}; char retvals[MAX_STR_LENGTH] = {""};
strcpy(args, fname.c_str()); sls::strcpy_safe(args, fname.c_str());
FILE_LOG(logDEBUG1) << "Sending file name to receiver: " << args; FILE_LOG(logDEBUG1) << "Sending file name to receiver: " << args;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -5665,7 +5626,7 @@ slsDetectorDefs::sls_detector_module* slsDetector::readSettingsFile(const std::s
} }
infile.close(); infile.close();
strcpy(thisDetector->settingsFile, fname.c_str()); sls::strcpy_safe(thisDetector->settingsFile, fname.c_str());
FILE_LOG(logINFO) << "Settings file loaded: " << thisDetector->settingsFile; FILE_LOG(logINFO) << "Settings file loaded: " << thisDetector->settingsFile;
return myMod; return myMod;
} }

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

@ -12,7 +12,7 @@ namespace sls {
// C++11 make_unique implementation for exeption safety // C++11 make_unique implementation for exception safety
// already available as std::make_unique in C++14 // already available as std::make_unique in C++14
template <typename T, typename... Args> template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args) { std::unique_ptr<T> make_unique(Args &&... args) {

View File

@ -18,6 +18,22 @@ void strcpy_safe(char (&destination)[array_size], const char *source) {
destination[array_size - 1] = '\0'; 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.
*/
template <size_t array_size>
void removeChar(char (&str)[array_size], char ch) {
int count = 0;
for (int i = 0; str[i]; i++) {
if (str[i] != ch)
str[count++] = str[i];
if (i == array_size - 1)
break;
}
str[count] = '\0';
}
/* /*
Split a string using the specified delimeter and return a vector of strings. Split a string using the specified delimeter and return a vector of strings.
TODO! Look into switching to absl or a string_view based implementation. Current TODO! Look into switching to absl or a string_view based implementation. Current
@ -33,6 +49,11 @@ std::string concatenateNonEmptyStrings(const std::vector<std::string> &vec);
/* /*
Concatenate strings using + if the strings are different Concatenate strings using + if the strings are different
*/ */
std::string concatenateIfDifferent(std::vector<std::string> container); std::string concatenateIfDifferent(const 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,8 @@
#include "string_utils.h" #include "string_utils.h"
#include "container_utils.h" #include "container_utils.h"
#include <sstream> #include <sstream>
#include <iomanip>
#include <algorithm>
namespace sls{ namespace sls{
@ -25,7 +27,7 @@ std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
return ret; return ret;
} }
std::string concatenateIfDifferent(std::vector<std::string> container) std::string concatenateIfDifferent(const std::vector<std::string>& container)
{ {
if (allEqual(container)) { if (allEqual(container)) {
return container.front(); return container.front();
@ -37,5 +39,18 @@ 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,28 @@ 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");
}
TEST_CASE("Remove char from string"){
char str[] = "sometest";
sls::removeChar(str, 'e');
REQUIRE(std::string(str) == "somtst");
}
TEST_CASE("Remove char from empty string"){
char str[50] = {};
sls::removeChar(str, 'e');
REQUIRE(std::string(str) == "");
}
TEST_CASE("Many characters in a row"){
char str[] = "someeequitellll::ongstring";
sls::removeChar(str, 'l');
REQUIRE(std::string(str) == "someeequite::ongstring");
} }