strcpy and cleanup of configuremac

This commit is contained in:
Erik Frojdh
2019-01-14 15:33:35 +01:00
parent 172c9218d1
commit 991567ba1c
4 changed files with 102 additions and 92 deletions

View File

@ -359,8 +359,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->shmversion = SLS_SHMVERSION;
thisDetector->onlineFlag = OFFLINE_FLAG;
thisDetector->stoppedFlag = 0;
strncpy(thisDetector->hostname, DEFAULT_HOSTNAME, MAX_STR_LENGTH-1);
thisDetector->hostname[MAX_STR_LENGTH-1] = 0;
sls::strcpy_safe(thisDetector->hostname, DEFAULT_HOSTNAME);
thisDetector->myDetectorType = type;
thisDetector->offset[X] = 0;
thisDetector->offset[Y] = 0;
@ -368,8 +367,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->multiSize[Y] = 0;
thisDetector->controlPort = DEFAULT_PORTNO;
thisDetector->stopPort = DEFAULT_PORTNO + 1;
strncpy(thisDetector->settingsDir, getenv("HOME"), MAX_STR_LENGTH-1);
thisDetector->settingsDir[MAX_STR_LENGTH-1] = 0;
sls::strcpy_safe(thisDetector->settingsDir, getenv("HOME"));
thisDetector->nTrimEn = 0;
for(int & trimEnergie : thisDetector->trimEnergies)
trimEnergie = 0;
@ -377,7 +375,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->nROI = 0;
memset(thisDetector->roiLimits, 0, MAX_ROIS * sizeof(ROI));
thisDetector->roFlags = NORMAL_READOUT;
strcpy(thisDetector->settingsFile, "none");
sls::strcpy_safe(thisDetector->settingsFile, "none");
thisDetector->currentSettings = UNINITIALIZED;
thisDetector->currentThresholdEV = -1;
thisDetector->timerValue[FRAME_NUMBER] = 1;
@ -396,16 +394,14 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->timerValue[SUBFRAME_ACQUISITION_TIME] = 0;
thisDetector->timerValue[STORAGE_CELL_NUMBER] = 0;
thisDetector->timerValue[SUBFRAME_DEADTIME] = 0;
strcpy(thisDetector->receiver_hostname, "none");
sls::strcpy_safe(thisDetector->receiver_hostname, "none");
thisDetector->receiverTCPPort = DEFAULT_PORTNO+2;
thisDetector->receiverUDPPort = DEFAULT_UDP_PORTNO;
thisDetector->receiverUDPPort2 = DEFAULT_UDP_PORTNO + 1;
strcpy(thisDetector->receiverUDPIP, "none");
strcpy(thisDetector->receiverUDPMAC, "none");
strncpy(thisDetector->detectorMAC, DEFAULT_DET_MAC, MAX_STR_LENGTH-1);
thisDetector->detectorMAC[MAX_STR_LENGTH-1] = 0;
strncpy(thisDetector->detectorIP, DEFAULT_DET_IP, MAX_STR_LENGTH-1);
thisDetector->detectorIP[MAX_STR_LENGTH-1] = 0;
sls::strcpy_safe(thisDetector->receiverUDPIP, "none");
sls::strcpy_safe(thisDetector->receiverUDPMAC, "none");
sls::strcpy_safe(thisDetector->detectorMAC, DEFAULT_DET_MAC);
sls::strcpy_safe(thisDetector->detectorIP, DEFAULT_DET_IP);
thisDetector->receiverOnlineFlag = OFFLINE_FLAG;
thisDetector->tenGigaEnable = 0;
thisDetector->flippedData[X] = 0;
@ -428,8 +424,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
thisDetector->activated = true;
thisDetector->receiver_deactivatedPaddingEnable = true;
thisDetector->receiver_silentMode = false;
strcpy(thisDetector->receiver_filePath, "/");
strcpy(thisDetector->receiver_fileName, "run");
sls::strcpy_safe(thisDetector->receiver_filePath, "/");
sls::strcpy_safe(thisDetector->receiver_fileName, "run");
thisDetector->receiver_fileIndex = 0;
thisDetector->receiver_fileFormatType = BINARY;
switch(thisDetector->myDetectorType) {
@ -2085,45 +2081,23 @@ int slsDetector::configureMAC() {
snprintf(args[7], array_size, "%x", pos[1]);
snprintf(args[8], array_size, "%x", pos[2]);
}
{
//converting IPaddress to std::hex
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[array_size] = {};
std::string s;
while (getline(ss, s, ':')) {
sprintf(cword,"%s%s",cword,s.c_str());
}
sls::strcpy_safe(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] << "-";
{
std::stringstream ss(args[3]);
char cword[array_size] = {};
std::string s;
while (getline(ss, s, ':')) {
sprintf(cword,"%s%s",cword,s.c_str());
}
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] = {};
std::string s;
while (getline(ss, s, '.')) {
sprintf(cword,"%s%02x",cword,atoi(s.c_str()));
}
sls::strcpy_safe(args[4],cword);
FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-";
}
// MAC already in hex removing :
sls::removeChar(args[3], ':');
FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-";
//converting detectorIP to string hex
sls::strcpy_safe(args[4],sls::stringIpToHex(args[4]).c_str());
FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-";
FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-";
FILE_LOG(logDEBUG1) << "row:" << args[6] << "-";
FILE_LOG(logDEBUG1) << "col:" << args[7] << "-";
@ -2144,14 +2118,14 @@ int slsDetector::configureMAC() {
uint32_t idetectorip = 0;
sscanf(retvals[0], "%lx", &idetectormac);
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>>32)&0xFF),
(unsigned int)((idetectormac>>24)&0xFF),
(unsigned int)((idetectormac>>16)&0xFF),
(unsigned int)((idetectormac>>8)&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>>16)&0xff,
(idetectorip>>8)&0xff,
@ -2838,7 +2812,7 @@ std::string slsDetector::setDetectorMAC(const std::string& detectorMAC) {
}
// valid format
else {
strcpy(thisDetector->detectorMAC, detectorMAC.c_str());
sls::strcpy_safe(thisDetector->detectorMAC, detectorMAC.c_str());
if (!strcmp(thisDetector->receiver_hostname, "none")) {
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
} else if (setUDPConnection() == FAIL) {
@ -2861,7 +2835,7 @@ std::string slsDetector::setDetectorIP(const std::string& detectorIP) {
}
// valid format
else {
strcpy(thisDetector->detectorIP, detectorIP.c_str());
sls::strcpy_safe(thisDetector->detectorIP, detectorIP.c_str());
if (!strcmp(thisDetector->receiver_hostname, "none")) {
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
} else if (setUDPConnection() == FAIL) {
@ -2891,7 +2865,7 @@ std::string slsDetector::setReceiver(const std::string& receiverIP) {
updateDetector();
// start updating
strcpy(thisDetector->receiver_hostname, receiverIP.c_str());
sls::strcpy_safe(thisDetector->receiver_hostname, receiverIP.c_str());
if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) {
FILE_LOG(logDEBUG1) <<
@ -3008,12 +2982,12 @@ std::string slsDetector::setReceiverUDPMAC(const std::string& udpmac) {
}
// valid format
else {
strcpy(thisDetector->receiverUDPMAC, udpmac.c_str());
sls::strcpy_safe(thisDetector->receiverUDPMAC, udpmac.c_str());
if (!strcmp(thisDetector->receiver_hostname, "none")) {
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
}
// 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);
}
@ -3124,10 +3098,10 @@ std::string slsDetector::setReceiverStreamingIP(std::string sourceIP) {
// set it anyway, else it is lost if rx_hostname is not set yet
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 (! strlen(thisDetector->zmqip))
strcpy(thisDetector->zmqip, args);
sls::strcpy_safe(thisDetector->zmqip, args);
FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: " << args;
// send to receiver
@ -3141,7 +3115,7 @@ std::string slsDetector::setReceiverStreamingIP(std::string sourceIP) {
} else {
FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retvals;
memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiver_zmqip, retvals);
sls::strcpy_safe(thisDetector->receiver_zmqip, retvals);
if (ret == FORCE_UPDATE)
ret = updateReceiver();
}
@ -3168,7 +3142,7 @@ std::string slsDetector::setAdditionalJsonHeader(const std::string& jsonheader)
} else {
FILE_LOG(logDEBUG1) << "Additional json header: " << retvals;
memset(thisDetector->receiver_additionalJsonHeader, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiver_additionalJsonHeader, retvals);
sls::strcpy_safe(thisDetector->receiver_additionalJsonHeader, retvals);
if (ret == FORCE_UPDATE)
ret = updateReceiver();
}
@ -3229,7 +3203,7 @@ int slsDetector::setUDPConnection() {
int fnum = F_SETUP_RECEIVER_UDP;
int ret = FAIL;
char args[3][MAX_STR_LENGTH] = {{""}, {""}, {""}};;
char retvals[MAX_STR_LENGTH] = {""};
char retvals[MAX_STR_LENGTH] = {};
FILE_LOG(logDEBUG1) << "Setting UDP Connection";
// called before set up
@ -3241,7 +3215,7 @@ int slsDetector::setUDPConnection() {
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;
@ -3252,15 +3226,15 @@ int slsDetector::setUDPConnection() {
// on failure, back to none
if (dataSocket->ConvertInternetAddresstoIpString(result,
thisDetector->receiverUDPIP, MAX_STR_LENGTH)) {
strcpy(thisDetector->receiverUDPIP, "none");
sls::strcpy_safe(thisDetector->receiverUDPIP, "none");
}
}
}
}
//copy arguments to args[][]
strcpy(args[0],thisDetector->receiverUDPIP);
sprintf(args[1],"%d",thisDetector->receiverUDPPort);
sprintf(args[2],"%d",thisDetector->receiverUDPPort2);
sls::strcpy_safe(args[0],thisDetector->receiverUDPIP);
snprintf(args[1], sizeof(args[2]), "%d",thisDetector->receiverUDPPort);
snprintf(args[2], sizeof(args[2]), "%d",thisDetector->receiverUDPPort2);
FILE_LOG(logDEBUG1) << "Receiver udp ip address: " << thisDetector->receiverUDPIP;
FILE_LOG(logDEBUG1) << "Receiver udp port: " << thisDetector->receiverUDPPort;
FILE_LOG(logDEBUG1) << "Receiver udp port2: " << thisDetector->receiverUDPPort2;
@ -3275,7 +3249,7 @@ int slsDetector::setUDPConnection() {
} else {
FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals;
memset(thisDetector->receiverUDPMAC, 0, MAX_STR_LENGTH);
strcpy(thisDetector->receiverUDPMAC, retvals);
sls::strcpy_safe(thisDetector->receiverUDPMAC, retvals);
if (ret == FORCE_UPDATE)
ret = updateReceiver();
@ -4519,11 +4493,11 @@ int slsDetector::setReceiverTCPSocket(const std::string& name, int const receiv
thisDetector->receiverOnlineFlag = OFFLINE_FLAG;
return FAIL;
}
strcpy(thisName,thisDetector->receiver_hostname);
sls::strcpy_safe(thisName,thisDetector->receiver_hostname);
} else {
FILE_LOG(logDEBUG1) << "Setting rx_hostname";
strcpy(thisName, name.c_str());
strcpy(thisDetector->receiver_hostname, thisName);
sls::strcpy_safe(thisName, name.c_str());
sls::strcpy_safe(thisDetector->receiver_hostname, thisName);
if (dataSocket) {
delete dataSocket;
dataSocket = nullptr;
@ -4640,7 +4614,7 @@ int slsDetector::execReceiverCommand(const std::string& cmd) {
int ret = FAIL;
char arg[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;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -4661,19 +4635,19 @@ int slsDetector::execReceiverCommand(const std::string& cmd) {
int slsDetector::updateReceiverNoWait() {
int n = 0, i32 = 0;
char cstring[MAX_STR_LENGTH] = {0};
char lastClientIP[INET_ADDRSTRLEN] = {0};
char cstring[MAX_STR_LENGTH] = {};
char lastClientIP[INET_ADDRSTRLEN] = {};
n += dataSocket->ReceiveDataOnly(lastClientIP, sizeof(lastClientIP));
FILE_LOG(logDEBUG1) << "Updating receiver last modified by " << lastClientIP;
// filepath
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_filePath, cstring);
sls::strcpy_safe(thisDetector->receiver_filePath, cstring);
// filename
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_fileName, cstring);
sls::strcpy_safe(thisDetector->receiver_fileName, cstring);
// index
n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32));
@ -4717,11 +4691,11 @@ int slsDetector::updateReceiverNoWait() {
// streaming source ip
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_zmqip, cstring);
sls::strcpy_safe(thisDetector->receiver_zmqip, cstring);
// additional json header
n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring));
strcpy(thisDetector->receiver_additionalJsonHeader, cstring);
sls::strcpy_safe(thisDetector->receiver_additionalJsonHeader, cstring);
// receiver streaming enable
n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32));
@ -4816,9 +4790,9 @@ void slsDetector::setDetectorId() {
void slsDetector::setDetectorHostname() {
int fnum = F_SEND_RECEIVER_DETHOSTNAME;
int ret = FAIL;
char args[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
strcpy(args, thisDetector->hostname);
char args[MAX_STR_LENGTH] = {};
char retvals[MAX_STR_LENGTH] = {};
sls::strcpy_safe(args, thisDetector->hostname);
FILE_LOG(logDEBUG1) << "Sending detector hostname to receiver: " << args;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -4846,9 +4820,9 @@ std::string slsDetector::setFilePath(const std::string& path) {
if (!path.empty()) {
int fnum = F_SET_RECEIVER_FILE_PATH;
int ret = FAIL;
char args[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
strcpy(args, path.c_str());
char args[MAX_STR_LENGTH] = {};
char retvals[MAX_STR_LENGTH] = {};
sls::strcpy_safe(args, path.c_str());
FILE_LOG(logDEBUG1) << "Sending file path to receiver: " << args;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -4864,7 +4838,7 @@ std::string slsDetector::setFilePath(const std::string& path) {
setErrorMask((getErrorMask())|(RECEIVER_PARAMETER_NOT_SET));
} else {
FILE_LOG(logDEBUG1) << "Receiver file path: " << retvals;
strcpy(thisDetector->receiver_filePath, retvals);
sls::strcpy_safe(thisDetector->receiver_filePath, retvals);
if (ret == FORCE_UPDATE)
ret = updateReceiver();
}
@ -4885,7 +4859,7 @@ std::string slsDetector::setFileName(const std::string& fname) {
int ret = FAIL;
char args[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;
if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) {
@ -5766,7 +5740,7 @@ slsDetectorDefs::sls_detector_module* slsDetector::readSettingsFile(const std::s
}
infile.close();
strcpy(thisDetector->settingsFile, fname.c_str());
sls::strcpy_safe(thisDetector->settingsFile, fname.c_str());
FILE_LOG(logINFO) << "Settings file loaded: " << thisDetector->settingsFile;
return myMod;
}

View File

@ -18,6 +18,22 @@ void strcpy_safe(char (&destination)[array_size], const char *source) {
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.
TODO! Look into switching to absl or a string_view based implementation. Current
@ -33,11 +49,11 @@ std::string concatenateNonEmptyStrings(const std::vector<std::string> &vec);
/*
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);
std::string stringIpToHex(const std::string &ip);
}; // namespace sls

View File

@ -3,6 +3,7 @@
#include "container_utils.h"
#include <sstream>
#include <iomanip>
#include <algorithm>
namespace sls{
@ -26,7 +27,7 @@ std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
return ret;
}
std::string concatenateIfDifferent(std::vector<std::string> container)
std::string concatenateIfDifferent(const std::vector<std::string>& container)
{
if (allEqual(container)) {
return container.front();
@ -51,4 +52,5 @@ std::string stringIpToHex(const std::string& ip)
}
}; // namespace sls

View File

@ -93,4 +93,22 @@ 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");
}