Skip to content

Commit

Permalink
Merge pull request #148 from igough/ipfix
Browse files Browse the repository at this point in the history
Fix for #147, where NetworkConnect cannot connect on FreeRTOS when an…
  • Loading branch information
icraggs authored Nov 16, 2018
2 parents de71f9a + 3f2853e commit b8e63d5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,30 @@ 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 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;

// 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);
Expand Down

0 comments on commit b8e63d5

Please sign in to comment.