From 8246bfa8fb6e917ef7009ce28ee43e37db91632d Mon Sep 17 00:00:00 2001 From: Liu Han Date: Wed, 24 Jul 2019 12:23:03 +0800 Subject: [PATCH 1/2] fix(transport): Fix a bug of the connection whether be active or not. --- components/tcp_transport/transport_ssl.c | 28 ++++++++++++++++++++++-- components/tcp_transport/transport_tcp.c | 28 ++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index cf8a1fda32..cafe5173d9 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -80,24 +80,48 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int static int ssl_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_ssl_t *ssl = esp_transport_get_context_data(t); + int ret = -1; fd_set readset; + fd_set errset; FD_ZERO(&readset); + FD_ZERO(&errset); FD_SET(ssl->tls->sockfd, &readset); + FD_SET(ssl->tls->sockfd, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout); + ret = select(ssl->tls->sockfd + 1, &readset, NULL, &errset, &timeout); + if (ret > 0 && FD_ISSET(ssl->tls->sockfd, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "Poll read catch a error, errno = %s, fd = %d", strerror(sock_errno), ssl->tls->sockfd); + ret = -1; + } + return ret; } static int ssl_poll_write(esp_transport_handle_t t, int timeout_ms) { transport_ssl_t *ssl = esp_transport_get_context_data(t); + int ret = -1; fd_set writeset; + fd_set errset; FD_ZERO(&writeset); + FD_ZERO(&errset); FD_SET(ssl->tls->sockfd, &writeset); + FD_SET(ssl->tls->sockfd, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout); + ret = select(ssl->tls->sockfd + 1, NULL, &writeset, &errset, &timeout); + if (ret > 0 && FD_ISSET(ssl->tls->sockfd, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "Poll write catch a error, errno = %s, fd = %d", strerror(sock_errno), ssl->tls->sockfd); + ret = -1; + } + return ret; } static int ssl_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms) diff --git a/components/tcp_transport/transport_tcp.c b/components/tcp_transport/transport_tcp.c index ff6925b2d1..3d6e3032d6 100644 --- a/components/tcp_transport/transport_tcp.c +++ b/components/tcp_transport/transport_tcp.c @@ -115,23 +115,47 @@ static int tcp_read(esp_transport_handle_t t, char *buffer, int len, int timeout static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_tcp_t *tcp = esp_transport_get_context_data(t); + int ret = -1; fd_set readset; + fd_set errset; FD_ZERO(&readset); + FD_ZERO(&errset); FD_SET(tcp->sock, &readset); + FD_SET(tcp->sock, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(tcp->sock + 1, &readset, NULL, NULL, &timeout); + ret = select(tcp->sock + 1, &readset, NULL, &errset, &timeout); + if (ret > 0 && FD_ISSET(tcp->sock, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "Poll read catch a error, errno = %s, fd = %d", strerror(sock_errno), tcp->sock); + ret = -1; + } + return ret; } static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms) { transport_tcp_t *tcp = esp_transport_get_context_data(t); + int ret = -1; fd_set writeset; + fd_set errset; FD_ZERO(&writeset); + FD_ZERO(&errset); FD_SET(tcp->sock, &writeset); + FD_SET(tcp->sock, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(tcp->sock + 1, NULL, &writeset, NULL, &timeout); + ret = select(tcp->sock + 1, NULL, &writeset, &errset, &timeout); + if (ret > 0 && FD_ISSET(tcp->sock, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "Poll write catch a error, errno = %s, fd = %d", strerror(sock_errno), tcp->sock); + ret = -1; + } + return ret; } static int tcp_close(esp_transport_handle_t t) From 67be0ae585c3c54c43c06333723d1fa3ee72a756 Mon Sep 17 00:00:00 2001 From: Liu Han Date: Wed, 24 Jul 2019 19:22:00 +0800 Subject: [PATCH 2/2] fix(transport): Fix a bug of the connection whether be active or not by timeout option when the select function return a correct value. --- components/esp-tls/esp_tls.c | 1 + components/tcp_transport/transport_ssl.c | 5 +++-- components/tcp_transport/transport_tcp.c | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index 77bc7e6514..07b53b777f 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -117,6 +117,7 @@ static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, struct timeval tv; ms_to_timeval(cfg->timeout_ms, &tv); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); } if (cfg->non_block) { int flags = fcntl(fd, F_GETFL, 0); diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index cafe5173d9..28f0c1e12c 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -74,6 +74,7 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int ESP_LOGE(TAG, "Failed to open a new connection"); return -1; } + return 0; } @@ -95,7 +96,7 @@ static int ssl_poll_read(esp_transport_handle_t t, int timeout_ms) int sock_errno = 0; uint32_t optlen = sizeof(sock_errno); getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); - ESP_LOGE(TAG, "Poll read catch a error, errno = %s, fd = %d", strerror(sock_errno), ssl->tls->sockfd); + ESP_LOGE(TAG, "ssl_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd); ret = -1; } return ret; @@ -118,7 +119,7 @@ static int ssl_poll_write(esp_transport_handle_t t, int timeout_ms) int sock_errno = 0; uint32_t optlen = sizeof(sock_errno); getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); - ESP_LOGE(TAG, "Poll write catch a error, errno = %s, fd = %d", strerror(sock_errno), ssl->tls->sockfd); + ESP_LOGE(TAG, "ssl_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd); ret = -1; } return ret; diff --git a/components/tcp_transport/transport_tcp.c b/components/tcp_transport/transport_tcp.c index 3d6e3032d6..9ccc79c5f8 100644 --- a/components/tcp_transport/transport_tcp.c +++ b/components/tcp_transport/transport_tcp.c @@ -77,6 +77,7 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int esp_transport_utils_ms_to_timeval(timeout_ms, &tv); setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(tcp->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); ESP_LOGD(TAG, "[sock=%d],connecting to server IP:%s,Port:%d...", tcp->sock, ipaddr_ntoa((const ip_addr_t*)&remote_ip.sin_addr.s_addr), port); @@ -129,7 +130,7 @@ static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms) int sock_errno = 0; uint32_t optlen = sizeof(sock_errno); getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); - ESP_LOGE(TAG, "Poll read catch a error, errno = %s, fd = %d", strerror(sock_errno), tcp->sock); + ESP_LOGE(TAG, "tcp_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock); ret = -1; } return ret; @@ -152,7 +153,7 @@ static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms) int sock_errno = 0; uint32_t optlen = sizeof(sock_errno); getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); - ESP_LOGE(TAG, "Poll write catch a error, errno = %s, fd = %d", strerror(sock_errno), tcp->sock); + ESP_LOGE(TAG, "tcp_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock); ret = -1; } return ret;