mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 13:27:14 +02:00
replaced gethostbyname with getaddrinfo as it is not thread safe
This commit is contained in:
@ -46,13 +46,13 @@ public:
|
|||||||
socketDescriptor (NULL)
|
socketDescriptor (NULL)
|
||||||
{
|
{
|
||||||
char ip[MAX_STR_LENGTH] = "";
|
char ip[MAX_STR_LENGTH] = "";
|
||||||
strcpy(ip, hostname_or_ip);
|
memset(ip, 0, MAX_STR_LENGTH);
|
||||||
|
|
||||||
// convert hostname to ip
|
// convert hostname to ip (not required, but a test that returns if failed)
|
||||||
char* ptr = ConvertHostnameToIp (hostname_or_ip);
|
struct addrinfo *result;
|
||||||
if (ptr == NULL)
|
if ((ConvertHostnameToInternetAddress(hostname_or_ip, &result)) ||
|
||||||
|
(ConvertInternetAddresstoIpString(result, ip, MAX_STR_LENGTH)))
|
||||||
return;
|
return;
|
||||||
strcpy(ip, ptr);
|
|
||||||
|
|
||||||
// construct address
|
// construct address
|
||||||
sprintf (serverAddress, "tcp://%s:%d", ip, portno);
|
sprintf (serverAddress, "tcp://%s:%d", ip, portno);
|
||||||
@ -187,19 +187,57 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert Hostname to ip
|
/**
|
||||||
* @param hostname hostname
|
* Convert Hostname to Internet address info structure
|
||||||
* @returns string with ip or NULL if error
|
* One must use freeaddrinfo(res) after using it
|
||||||
*/
|
* @param hostname hostname
|
||||||
char* ConvertHostnameToIp (const char* const hostname) {
|
* @param res address of pointer to address info structure
|
||||||
struct hostent *he = gethostbyname (hostname);
|
* @return 1 for fail, 0 for success
|
||||||
if (he == NULL){
|
*/
|
||||||
cprintf (RED,"Error: Could not convert hostname to ip (zmq)\n");
|
// Do not make this static (for multi threading environment)
|
||||||
return NULL;
|
int ConvertHostnameToInternetAddress (const char* const hostname, struct addrinfo **res) {
|
||||||
}
|
// criteria in selecting socket address structures returned by res
|
||||||
return inet_ntoa (*(struct in_addr*)he->h_addr);
|
struct addrinfo hints;
|
||||||
};
|
memset (&hints, 0, sizeof (hints));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
// get host info into res
|
||||||
|
int errcode = getaddrinfo (hostname, NULL, &hints, res);
|
||||||
|
if (errcode != 0) {
|
||||||
|
cprintf (RED,"Error: Could not convert %s hostname to internet address (zmq):"
|
||||||
|
"%s\n", hostname, gai_strerror(errcode));
|
||||||
|
} else {
|
||||||
|
if (*res == NULL) {
|
||||||
|
cprintf (RED,"Error: Could not convert %s hostname to internet address (zmq): "
|
||||||
|
"gettaddrinfo returned null\n", hostname);
|
||||||
|
} else{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cerr << "Error: Could not convert hostname to internet address" << endl;
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Internet Address structure pointer to ip string (char*)
|
||||||
|
* Clears the internet address structure as well
|
||||||
|
* @param res pointer to internet address structure
|
||||||
|
* @param ip pointer to char array to store result in
|
||||||
|
* @param ipsize size available in ip buffer
|
||||||
|
* @return 1 for fail, 0 for success
|
||||||
|
*/
|
||||||
|
// Do not make this static (for multi threading environment)
|
||||||
|
int ConvertInternetAddresstoIpString (struct addrinfo *res, char* ip, const int ipsize) {
|
||||||
|
if (inet_ntop (res->ai_family, &((struct sockaddr_in *) res->ai_addr)->sin_addr, ip, ipsize) != NULL) {
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
cerr << "Error: Could not convert internet address to ip string" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send Message Header
|
* Send Message Header
|
||||||
|
@ -106,52 +106,15 @@ enum communicationProtocol{
|
|||||||
memset(dummyClientIP,0,INET_ADDRSTRLEN);
|
memset(dummyClientIP,0,INET_ADDRSTRLEN);
|
||||||
differentClients = 0;
|
differentClients = 0;
|
||||||
|
|
||||||
|
struct addrinfo *result;
|
||||||
|
if (!ConvertHostnameToInternetAddress(host_ip_or_name, &result)) {
|
||||||
struct addrinfo hints, *res;
|
serverAddress.sin_family = result->ai_family;
|
||||||
int errcode;
|
memcpy((char *) &serverAddress.sin_addr.s_addr, &((struct sockaddr_in *) result->ai_addr)->sin_addr, result->ai_addrlen);
|
||||||
char addrstr[100];
|
freeaddrinfo(result);
|
||||||
void *ptr;
|
|
||||||
memset (&hints, 0, sizeof (hints));
|
|
||||||
// criteria in selecting socket address structures returned by res
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
//hints.ai_flags |= AI_CANONNAME;
|
|
||||||
errcode = getaddrinfo (host_ip_or_name, NULL, &hints, &res);
|
|
||||||
if (errcode != 0) {
|
|
||||||
cerr << "Exiting: Problem interpreting host: " << host_ip_or_name << ". Error: " << gai_strerror(errcode) << endl;
|
|
||||||
perror ("getaddrinfo");
|
|
||||||
} else {
|
|
||||||
if (res == NULL) {
|
|
||||||
cerr << "Exiting: Problem interpreting host: " << host_ip_or_name << ". Null returned by getaddrinfo. " << endl;
|
|
||||||
}
|
|
||||||
// Set some fields in the serverAddress structure.
|
|
||||||
serverAddress.sin_family = res->ai_family;
|
|
||||||
memcpy((char *) &serverAddress.sin_addr.s_addr, &((struct sockaddr_in *) res->ai_addr)->sin_addr, res->ai_addrlen);
|
|
||||||
serverAddress.sin_port = htons(port_number);
|
serverAddress.sin_port = htons(port_number);
|
||||||
socketDescriptor=0;
|
socketDescriptor=0;
|
||||||
freeaddrinfo(res);
|
|
||||||
}
|
}
|
||||||
clientAddress_length=sizeof(clientAddress);
|
clientAddress_length=sizeof(clientAddress);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
struct hostent *hostInfo = gethostbyname(host_ip_or_name);
|
|
||||||
if (hostInfo == NULL){
|
|
||||||
cerr << "Exiting: Problem interpreting host: " << host_ip_or_name << "\n";
|
|
||||||
} else {
|
|
||||||
// Set some fields in the serverAddress structure.
|
|
||||||
serverAddress.sin_family = hostInfo->h_addrtype;
|
|
||||||
memcpy((char *) &serverAddress.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
|
|
||||||
//((char *) &serverAddress.sin_addr.s_addr)[hostInfo->h_length]='\0'; //a fix for valgrind
|
|
||||||
serverAddress.sin_port = htons(port_number);
|
|
||||||
socketDescriptor=0; //You can use send and recv, //would it work?????
|
|
||||||
}
|
|
||||||
clientAddress_length=sizeof(clientAddress);
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -164,7 +127,7 @@ enum communicationProtocol{
|
|||||||
return SOCK_DGRAM;
|
return SOCK_DGRAM;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
cerr << "unknow protocol " << p << endl;
|
cerr << "unknown protocol " << p << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -608,6 +571,57 @@ enum communicationProtocol{
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Hostname to Internet address info structure
|
||||||
|
* One must use freeaddrinfo(res) after using it
|
||||||
|
* @param hostname hostname
|
||||||
|
* @param res address of pointer to address info structure
|
||||||
|
* @return 1 for fail, 0 for success
|
||||||
|
*/
|
||||||
|
// Do not make this static (for multi threading environment)
|
||||||
|
int ConvertHostnameToInternetAddress (const char* const hostname, struct addrinfo **res) {
|
||||||
|
// criteria in selecting socket address structures returned by res
|
||||||
|
struct addrinfo hints;
|
||||||
|
memset (&hints, 0, sizeof (hints));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
// get host info into res
|
||||||
|
int errcode = getaddrinfo (hostname, NULL, &hints, res);
|
||||||
|
if (errcode != 0) {
|
||||||
|
cprintf (RED,"Error: Could not convert %s hostname to internet address (zmq):"
|
||||||
|
"%s\n", hostname, gai_strerror(errcode));
|
||||||
|
} else {
|
||||||
|
if (*res == NULL) {
|
||||||
|
cprintf (RED,"Error: Could not convert %s hostname to internet address (zmq): "
|
||||||
|
"gettaddrinfo returned null\n", hostname);
|
||||||
|
} else{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cerr << "Error: Could not convert hostname to internet address" << endl;
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Internet Address structure pointer to ip string (char*)
|
||||||
|
* Clears the internet address structure as well
|
||||||
|
* @param res pointer to internet address structure
|
||||||
|
* @param ip pointer to char array to store result in
|
||||||
|
* @param ipsize size available in ip buffer
|
||||||
|
* @return 1 for fail, 0 for success
|
||||||
|
*/
|
||||||
|
// Do not make this static (for multi threading environment)
|
||||||
|
int ConvertInternetAddresstoIpString (struct addrinfo *res, char* ip, const int ipsize) {
|
||||||
|
if (inet_ntop (res->ai_family, &((struct sockaddr_in *) res->ai_addr)->sin_addr, ip, ipsize) != NULL) {
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
cerr << "Error: Could not convert internet address to ip string" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int ReceiveDataOnly(void* buf,int length=0){
|
int ReceiveDataOnly(void* buf,int length=0){
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user