From e9f6dcf4790f4997749087f14ba8c0e29beaadcf Mon Sep 17 00:00:00 2001 From: Dhanya Maliakal Date: Mon, 1 May 2017 10:24:16 +0200 Subject: [PATCH] strtok is not thread safe..used to set receiver udp ip etc to detector, fixed --- .../slsDetector/slsDetector.cpp | 304 ++++++++++-------- 1 file changed, 163 insertions(+), 141 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 0d81233ca..4cae95f7c 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -6181,151 +6181,173 @@ int slsDetector::setUDPConnection(){ int slsDetector::configureMAC(){ - int i; - int ret=FAIL; - int fnum=F_CONFIGURE_MAC,fnum2=F_RECEIVER_SHORT_FRAME; - char mess[MAX_STR_LENGTH]=""; - char arg[6][50]={"","","","","",""}; - char cword[50]="", *pcword; - string sword; - int retval=-1; + int i; + int ret=FAIL; + int fnum=F_CONFIGURE_MAC,fnum2=F_RECEIVER_SHORT_FRAME; + char mess[MAX_STR_LENGTH]=""; + char arg[6][50]={"","","","","",""}; + int retval=-1; - //if udpip wasnt initialized in config file - if(!(strcmp(thisDetector->receiverUDPIP,"none"))){ - //hostname is an ip address - if(strchr(thisDetector->receiver_hostname,'.')!=NULL) - strcpy(thisDetector->receiverUDPIP,thisDetector->receiver_hostname); - //if hostname not ip, convert it to ip - else{ - struct hostent *he = gethostbyname(thisDetector->receiver_hostname); - if (he != NULL) - strcpy(thisDetector->receiverUDPIP,inet_ntoa(*(struct in_addr*)he->h_addr)); - else{ - std::cout << "configure mac failed. no rx_udpip given and invalid receiver hostname" << endl; - setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); - return FAIL; - } - } - } - strcpy(arg[0],thisDetector->receiverUDPIP); - strcpy(arg[1],thisDetector->receiverUDPMAC); - sprintf(arg[2],"%x",thisDetector->receiverUDPPort); - strcpy(arg[3],thisDetector->detectorMAC); - strcpy(arg[4],thisDetector->detectorIP); - sprintf(arg[5],"%x",thisDetector->receiverUDPPort2); - -#ifdef VERBOSE - std::cout<< "Configuring MAC"<< std::endl; -#endif - - - for(i=0;i<2;i++){ - if(!strcmp(arg[i],"none")){ - std::cout<< "Configure MAC Error. IP/MAC Addresses not set"<< std::endl; - setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); - return FAIL; - } - } - -#ifdef VERBOSE - std::cout<< "IP/MAC Addresses valid "<< std::endl; -#endif - - //converting IPaddress to hex. - pcword = strtok (arg[0],"."); - while (pcword != NULL) { - sprintf(arg[0],"%02x",atoi(pcword)); - strcat(cword,arg[0]); - pcword = strtok (NULL, "."); - } - strcpy(arg[0],cword); -#ifdef VERBOSE - std::cout<<"receiver udp ip:"<onlineFlag==ONLINE_FLAG) { - if (connectControl() == OK){ - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - controlSocket->SendDataOnly(arg,sizeof(arg)); - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret==FAIL){ - controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); - } - else - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - disconnectControl(); - if (ret==FORCE_UPDATE) - updateDetector(); - } - } - if (ret==FAIL) { - ret=FAIL; - std::cout<< "Configuring MAC failed " << std::endl; - setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); - } - else if (thisDetector->myDetectorType==GOTTHARD){ - //set frames per file - only for gotthard - pthread_mutex_lock(&ms); - if(retval==-1) - setFramesPerFile(MAX_FRAMES_PER_FILE); - else - setFramesPerFile(SHORT_MAX_FRAMES_PER_FILE); - pthread_mutex_unlock(&ms); - //connect to receiver - if(thisDetector->receiverOnlineFlag==ONLINE_FLAG){ -#ifdef VERBOSE - std::cout << "Sending adc val to receiver " << retval << std::endl; -#endif - if (connectData() == OK){ - ret=thisReceiver->sendInt(fnum2,retval,retval); - disconnectData(); + //if udpip wasnt initialized in config file + if(!(strcmp(thisDetector->receiverUDPIP,"none"))){ + //hostname is an ip address + if(strchr(thisDetector->receiver_hostname,'.')!=NULL) + strcpy(thisDetector->receiverUDPIP,thisDetector->receiver_hostname); + //if hostname not ip, convert it to ip + else{ + struct hostent *he = gethostbyname(thisDetector->receiver_hostname); + if (he != NULL) + strcpy(thisDetector->receiverUDPIP,inet_ntoa(*(struct in_addr*)he->h_addr)); + else{ + std::cout << "configure mac failed. no rx_udpip given and invalid receiver hostname" << endl; + setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); + return FAIL; + } + } } - if(ret==FAIL) - setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); - } - } + strcpy(arg[0],thisDetector->receiverUDPIP); + strcpy(arg[1],thisDetector->receiverUDPMAC); + sprintf(arg[2],"%x",thisDetector->receiverUDPPort); + strcpy(arg[3],thisDetector->detectorMAC); + strcpy(arg[4],thisDetector->detectorIP); + sprintf(arg[5],"%x",thisDetector->receiverUDPPort2); - return ret; +#ifdef VERBOSE + std::cout<< "Configuring MAC"<< std::endl; +#endif + + + for(i=0;i<2;i++){ + if(!strcmp(arg[i],"none")){ + std::cout<< "Configure MAC Error. IP/MAC Addresses not set"<< std::endl; + setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); + return FAIL; + } + } + +#ifdef VERBOSE + std::cout<< "IP/MAC Addresses valid "<< std::endl; +#endif + + + { + //converting IPaddress to hex + stringstream ss(arg[0]); + char cword[50]=""; + bzero(cword, 50); + string s; + while (getline(ss, s, '.')) { + sprintf(cword,"%s%02x",cword,atoi(s.c_str())); + } + bzero(arg[0], 50); + strcpy(arg[0],cword); +#ifdef VERBOSE + std::cout<<"receiver udp ip:"<onlineFlag==ONLINE_FLAG) { + if (connectControl() == OK){ + controlSocket->SendDataOnly(&fnum,sizeof(fnum)); + controlSocket->SendDataOnly(arg,sizeof(arg)); + controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret==FAIL){ + controlSocket->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Detector returned error: " << mess << std::endl; + setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); + } + else + controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); + disconnectControl(); + if (ret==FORCE_UPDATE) + updateDetector(); + } + } + if (ret==FAIL) { + ret=FAIL; + std::cout<< "Configuring MAC failed " << std::endl; + setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); + } + else if (thisDetector->myDetectorType==GOTTHARD){ + //set frames per file - only for gotthard + pthread_mutex_lock(&ms); + if(retval==-1) + setFramesPerFile(MAX_FRAMES_PER_FILE); + else + setFramesPerFile(SHORT_MAX_FRAMES_PER_FILE); + pthread_mutex_unlock(&ms); + //connect to receiver + if(thisDetector->receiverOnlineFlag==ONLINE_FLAG){ +#ifdef VERBOSE + std::cout << "Sending adc val to receiver " << retval << std::endl; +#endif + if (connectData() == OK){ + ret=thisReceiver->sendInt(fnum2,retval,retval); + disconnectData(); + } + if(ret==FAIL) + setErrorMask((getErrorMask())|(COULD_NOT_CONFIGURE_MAC)); + } + } + + return ret; }