From d638a096aa1c66387c43572662c284740ed38bbd Mon Sep 17 00:00:00 2001 From: igough Date: Thu, 2 Aug 2018 13:26:54 -0400 Subject: [PATCH 1/3] Fix for #147, where NetworkConnect cannot connect on FreeRTOS when an IP address is specified in the addr string and there is no DNS server specified Signed-off-by: igough --- MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c index 220c7f35..15d90753 100755 --- a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c +++ b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c @@ -155,13 +155,22 @@ int NetworkConnect(Network* n, char* addr, int port) { struct freertos_sockaddr sAddr; int retVal = -1; - uint32_t ipAddress; - if ((ipAddress = FreeRTOS_gethostbyname(addr)) == 0) - goto exit; + // FreeRTOS_gethostbyname does not check if its the addr is already + // an IP address so do that first, otherwise valid IP addresses will fail. + sAddr.sin_addr = FreeRTOS_inet_addr( addr ); + if ( sAddr.sin_addr == 0 ) + { + // addr was not a valid IP address so do a lookup. + sAddr.sin_addr = FreeRTOS_gethostbyname(addr); + if ( sAddr.sin_addr == 0 ) + { + // Lookup failed. + goto exit; + } + } sAddr.sin_port = FreeRTOS_htons(port); - sAddr.sin_addr = ipAddress; if ((n->my_socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP)) < 0) goto exit; From d95cd6ac940da48c21d1ed533764c8aab9be57a2 Mon Sep 17 00:00:00 2001 From: igough Date: Thu, 2 Aug 2018 15:27:15 -0400 Subject: [PATCH 2/3] Handle case where socket is in close-wait from a previous recent connection, when attempting to connect again. Signed-off-by: igough --- MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c index 15d90753..473876ec 100755 --- a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c +++ b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c @@ -175,6 +175,10 @@ int NetworkConnect(Network* n, char* addr, int port) if ((n->my_socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP)) < 0) goto exit; + // Set a timeout so the connect does not hang forever if the socket is in close_wait + uint32_t tmo = 2000; + FreeRTOS_setsockopt( n->my_socket, 1, FREERTOS_SO_RCVTIMEO, (void *)&tmo, sizeof(uint32_t) ); + if ((retVal = FreeRTOS_connect(n->my_socket, &sAddr, sizeof(sAddr))) < 0) { FreeRTOS_closesocket(n->my_socket); From 3f2853e23a5973bd4995ee76f09a578646678d46 Mon Sep 17 00:00:00 2001 From: igough Date: Tue, 25 Sep 2018 12:28:23 -0400 Subject: [PATCH 3/3] Fix comment. Force rebuild. --- MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c index 473876ec..68ea62bd 100755 --- a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c +++ b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c @@ -156,7 +156,7 @@ int NetworkConnect(Network* n, char* addr, int port) struct freertos_sockaddr sAddr; int retVal = -1; - // FreeRTOS_gethostbyname does not check if its the addr is already + // FreeRTOS_gethostbyname does not check if addr is already // an IP address so do that first, otherwise valid IP addresses will fail. sAddr.sin_addr = FreeRTOS_inet_addr( addr ); if ( sAddr.sin_addr == 0 )