esl: make esl_connect thread-safe by using getaddrinfo instead of gethostbyname
This commit is contained in:
parent
a31222acba
commit
52885dfc1d
|
@ -606,11 +606,10 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
|
||||||
|
|
||||||
ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password)
|
ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password)
|
||||||
{
|
{
|
||||||
|
|
||||||
struct hostent *result;
|
|
||||||
char sendbuf[256];
|
char sendbuf[256];
|
||||||
int rval = 0;
|
int rval = 0;
|
||||||
const char *hval;
|
const char *hval;
|
||||||
|
struct addrinfo hints = { 0 }, *result;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WORD wVersionRequested = MAKEWORD(2, 0);
|
WORD wVersionRequested = MAKEWORD(2, 0);
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
|
@ -633,26 +632,23 @@ ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, es
|
||||||
return ESL_FAIL;
|
return ESL_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&handle->sockaddr, 0, sizeof(handle->sockaddr));
|
|
||||||
handle->sockaddr.sin_family = AF_INET;
|
|
||||||
handle->sockaddr.sin_port = htons(port);
|
|
||||||
|
|
||||||
memset(&handle->hostent, 0, sizeof(handle->hostent));
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
if ((result = gethostbyname(host))) {
|
if (getaddrinfo(host, NULL, &hints, &result)) {
|
||||||
handle->hostent = *result;
|
strncpy(handle->err, "Cannot resolve host", sizeof(handle->err));
|
||||||
} else {
|
|
||||||
rval = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rval) {
|
|
||||||
strerror_r(handle->errnum, handle->err, sizeof(handle->err));
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(&handle->sockaddr.sin_addr, result->h_addr_list[0], result->h_length);
|
memcpy(&handle->sockaddr, result->ai_addr, result->ai_addrlen);
|
||||||
|
handle->sockaddr.sin_family = AF_INET;
|
||||||
|
handle->sockaddr.sin_port = htons(port);
|
||||||
|
|
||||||
rval = connect(handle->sock, (struct sockaddr *) &handle->sockaddr, sizeof(handle->sockaddr));
|
rval = connect(handle->sock, (struct sockaddr*)&handle->sockaddr, sizeof(handle->sockaddr));
|
||||||
|
|
||||||
|
freeaddrinfo(result);
|
||||||
|
result = NULL;
|
||||||
|
|
||||||
if (rval) {
|
if (rval) {
|
||||||
snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
|
snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
|
||||||
|
|
Loading…
Reference in New Issue