From 739187943d4e21ee2bed82d80d11546c1ed5fe60 Mon Sep 17 00:00:00 2001 From: Dhanya Maliakal Date: Fri, 10 Nov 2017 16:30:38 +0100 Subject: [PATCH] replaced gethostbyname with getaddrinfo as it is not thread safe --- .../slsDetector/slsDetector.cpp | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index be39d7e7f..380223a02 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -6316,13 +6316,15 @@ int slsDetector::setUDPConnection(){ 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){ - cout<<"rx_hostname:"<receiver_hostname<receiverUDPIP,inet_ntoa(*(struct in_addr*)he->h_addr)); + struct addrinfo *result; + if (!dataSocket->ConvertHostnameToInternetAddress(thisDetector->receiver_hostname, &result)) { + // on success + memset(thisDetector->receiverUDPIP, 0, MAX_STR_LENGTH); + // on failure, back to none + if (dataSocket->ConvertInternetAddresstoIpString(result, thisDetector->receiverUDPIP, MAX_STR_LENGTH)) { + strcpy(thisDetector->receiverUDPIP, "none"); + } + } } } @@ -6379,7 +6381,6 @@ int slsDetector::configureMAC(){ char arg[6][50]={"","","","","",""}; int retval=-1; - //if udpip wasnt initialized in config file if(!(strcmp(thisDetector->receiverUDPIP,"none"))){ //hostname is an ip address @@ -6387,13 +6388,14 @@ int slsDetector::configureMAC(){ 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; + struct addrinfo *result; + if (!dataSocket->ConvertHostnameToInternetAddress(thisDetector->receiver_hostname, &result)) { + // on success + memset(thisDetector->receiverUDPIP, 0, MAX_STR_LENGTH); + // on failure, back to none + if (dataSocket->ConvertInternetAddresstoIpString(result, thisDetector->receiverUDPIP, MAX_STR_LENGTH)) { + strcpy(thisDetector->receiverUDPIP, "none"); + } } } }