Merge pull request #40 from Lawlieta/master

【修复】开启 MbedTLS 支持后 getaddrinfo() 失败问题
master
朱天龙 (Armink) 7 years ago committed by GitHub
commit 6518b1a9e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -225,39 +225,30 @@ static int webclient_resolve_address(struct webclient_session *session, struct a
memcpy(host_addr_new, host_addr, host_addr_len); memcpy(host_addr_new, host_addr, host_addr_len);
host_addr_new[host_addr_len] = '\0'; host_addr_new[host_addr_len] = '\0';
session->host = host_addr_new; session->host = host_addr_new;
#ifdef WEBCLIENT_USING_MBED_TLS
if (session->tls_session)
{
session->tls_session->host = web_strdup(host_addr_new);
}
#endif
} }
LOG_D("host address: %s , port: %s", session->host, port_str); LOG_D("host address: %s , port: %s", session->host, port_str);
/* resolve the host name. */
{
struct addrinfo hint;
int ret;
rt_memset(&hint, 0, sizeof(hint));
#ifdef WEBCLIENT_USING_MBED_TLS #ifdef WEBCLIENT_USING_MBED_TLS
if (session->tls_session) if (session->tls_session)
{ {
session->tls_session->port = web_strdup(port_str); session->tls_session->port = web_strdup(port_str);
ret = getaddrinfo(session->tls_session->host, port_str, &hint, res); session->tls_session->host = web_strdup(session->host);
if (ret != 0) if (session->tls_session->port == RT_NULL || session->tls_session->host == RT_NULL)
{ {
LOG_E("getaddrinfo err: %d '%s'", ret, session->host); return -WEBCLIENT_NOMEM;
rc = -WEBCLIENT_ERROR;
} }
goto __exit; return rc;
} }
#endif #endif
/* resolve the host name. */
{
struct addrinfo hint;
int ret;
rt_memset(&hint, 0, sizeof(hint));
ret = getaddrinfo(session->host, port_str, &hint, res); ret = getaddrinfo(session->host, port_str, &hint, res);
if (ret != 0) if (ret != 0)
{ {
@ -265,8 +256,8 @@ static int webclient_resolve_address(struct webclient_session *session, struct a
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
} }
__exit: __exit:
if (rc != WEBCLIENT_OK) if (rc != WEBCLIENT_OK)
{ {
@ -379,7 +370,8 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
goto __exit; goto __exit;
} }
if (!res) /* Not use 'getaddrinfo()' for https connection */
if (session->is_tls == RT_FALSE && res == RT_NULL)
{ {
rc = -WEBCLIENT_ERROR; rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
@ -411,8 +403,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
if ((tls_ret = mbedtls_client_connect(session->tls_session)) < 0) if ((tls_ret = mbedtls_client_connect(session->tls_session)) < 0)
{ {
LOG_E("connect failed, https client connect return: -0x%x", -tls_ret); LOG_E("connect failed, https client connect return: -0x%x", -tls_ret);
rc = -WEBCLIENT_CONNECT_FAILED; return -WEBCLIENT_CONNECT_FAILED;
goto __exit;
} }
socket_handle = session->tls_session->server_fd.fd; socket_handle = session->tls_session->server_fd.fd;
@ -425,8 +416,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
session->socket = socket_handle; session->socket = socket_handle;
rc = WEBCLIENT_OK; return WEBCLIENT_OK;
goto __exit;
} }
#endif #endif

Loading…
Cancel
Save