Skip to content

Commit 09192b0

Browse files
htiboschHein TiboschAniruddhaKanhere
authored
FreeRTOS_ARP.c : store local addresses only (#120)
* FreeRTOS_ARP.c : store local addresses only * Added the function xARPWaitResolution() * Added an entry to lexicon.txt. * Ran Uncrustify * Update unit test file * Update * Declared xARPWaitResolution() in FreeRTOS_IP.h * Compare the result of xIsCallingFromIPTask() with pdFALSE in stead of 0 Co-authored-by: Hein Tibosch <[email protected]> Co-authored-by: Aniruddha Kanhere <[email protected]> Co-authored-by: Aniruddha Kanhere <[email protected]>
1 parent 3372cd5 commit 09192b0

File tree

5 files changed

+90
-18
lines changed

5 files changed

+90
-18
lines changed

.github/lexicon.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,7 @@ uxstreambufferget
12391239
uxstreambuffermidspace
12401240
uxtail
12411241
uxtaskgetsystemstate
1242+
uxtickstowait
12421243
uxtimeout
12431244
uxtimeoutstate
12441245
uxtotaldatalength

FreeRTOS_ARP.c

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,8 @@ void vARPRefreshCacheEntry( const MACAddress_t * pxMACAddress,
271271
uint8_t ucMinAgeFound = 0U;
272272

273273
#if ( ipconfigARP_STORES_REMOTE_ADDRESSES == 0 )
274-
275-
/* Only process the IP address if it is on the local network.
276-
* Unless: when '*ipLOCAL_IP_ADDRESS_POINTER' equals zero, the IP-address
277-
* and netmask are still unknown. */
278-
if( ( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) ) ||
279-
( *ipLOCAL_IP_ADDRESS_POINTER == 0UL ) )
274+
/* Only process the IP address if it is on the local network. */
275+
if( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) )
280276
#else
281277

282278
/* If ipconfigARP_STORES_REMOTE_ADDRESSES is non-zero, IP addresses with
@@ -492,16 +488,6 @@ eARPLookupResult_t eARPGetCacheEntry( uint32_t * pulIPAddress,
492488

493489
ulAddressToLookup = *pulIPAddress;
494490

495-
#if ( ipconfigUSE_LLMNR == 1 )
496-
if( ulAddressToLookup == ipLLMNR_IP_ADDR ) /* Is in network byte order. */
497-
{
498-
/* The LLMNR IP-address has a fixed virtual MAC address. */
499-
( void ) memcpy( pxMACAddress->ucBytes, xLLMNR_MacAdress.ucBytes, sizeof( MACAddress_t ) );
500-
eReturn = eARPCacheHit;
501-
}
502-
else
503-
#endif
504-
505491
if( xIsIPv4Multicast( ulAddressToLookup ) != 0 )
506492
{
507493
/* Get the lowest 23 bits of the IP-address. */
@@ -522,6 +508,12 @@ eARPLookupResult_t eARPGetCacheEntry( uint32_t * pulIPAddress,
522508
* can be done. */
523509
eReturn = eCantSendPacket;
524510
}
511+
else if( *ipLOCAL_IP_ADDRESS_POINTER == *pulIPAddress )
512+
{
513+
/* The address of this device. May be useful for the loopback device. */
514+
eReturn = eARPCacheHit;
515+
memcpy( pxMACAddress->ucBytes, ipLOCAL_MAC_ADDRESS, sizeof( pxMACAddress->ucBytes ) );
516+
}
525517
else
526518
{
527519
eReturn = eARPCacheMiss;
@@ -747,7 +739,7 @@ void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress )
747739
}
748740
#endif /* if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES ) */
749741

750-
if( xIsCallingFromIPTask() != 0 )
742+
if( xIsCallingFromIPTask() != pdFALSE )
751743
{
752744
iptraceNETWORK_INTERFACE_OUTPUT( pxNetworkBuffer->xDataLength, pxNetworkBuffer->pucEthernetBuffer );
753745
/* Only the IP-task is allowed to call this function directly. */
@@ -769,7 +761,63 @@ void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress )
769761
}
770762
}
771763
}
772-
/*--------------------------------------*/
764+
/*-----------------------------------------------------------*/
765+
766+
/**
767+
* @brief Wait for address resolution: look-up the IP-address in the ARP-cache, and if
768+
* needed send an ARP request, and wait for a reply. This function is useful when
769+
* called before FreeRTOS_sendto().
770+
*
771+
* @param[in] ulIPAddress: The IP-address to look-up.
772+
* @param[in] uxTicksToWait: The maximum number of clock ticks to wait for a reply.
773+
*
774+
* @return Zero when successful.
775+
*/
776+
BaseType_t xARPWaitResolution( uint32_t ulIPAddress,
777+
TickType_t uxTicksToWait )
778+
{
779+
BaseType_t xResult = -pdFREERTOS_ERRNO_EADDRNOTAVAIL;
780+
TimeOut_t xTimeOut;
781+
MACAddress_t xMACAddress;
782+
eARPLookupResult_t xLookupResult;
783+
size_t uxSendCount = ipconfigMAX_ARP_RETRANSMISSIONS;
784+
785+
/* The IP-task is not supposed to call this function. */
786+
configASSERT( xIsCallingFromIPTask() == pdFALSE );
787+
788+
xLookupResult = eARPGetCacheEntry( &( ulIPAddress ), &( xMACAddress ) );
789+
790+
if( xLookupResult == eARPCacheMiss )
791+
{
792+
const TickType_t uxSleepTime = pdMS_TO_TICKS( 250U );
793+
794+
/* We might use ipconfigMAX_ARP_RETRANSMISSIONS here. */
795+
vTaskSetTimeOutState( &xTimeOut );
796+
797+
while( uxSendCount > 0 )
798+
{
799+
FreeRTOS_OutputARPRequest( ulIPAddress );
800+
801+
vTaskDelay( uxSleepTime );
802+
803+
xLookupResult = eARPGetCacheEntry( &( ulIPAddress ), &( xMACAddress ) );
804+
805+
if( ( xTaskCheckForTimeOut( &( xTimeOut ), &( uxTicksToWait ) ) == pdTRUE ) ||
806+
( xLookupResult != eARPCacheMiss ) )
807+
{
808+
break;
809+
}
810+
}
811+
}
812+
813+
if( xLookupResult == eARPCacheHit )
814+
{
815+
xResult = 0;
816+
}
817+
818+
return xResult;
819+
}
820+
/*-----------------------------------------------------------*/
773821

774822
/**
775823
* @brief Generate an ARP request packet by copying various constant details to

include/FreeRTOS_IP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@
330330
uint32_t FreeRTOS_GetGatewayAddress( void );
331331
uint32_t FreeRTOS_GetDNSServerAddress( void );
332332
uint32_t FreeRTOS_GetNetmask( void );
333+
BaseType_t xARPWaitResolution( uint32_t ulIPAddress,
334+
TickType_t uxTicksToWait );
333335
void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress );
334336
BaseType_t FreeRTOS_IsNetworkUp( void );
335337

test/unit-test/FreeRTOS_TCP_Unit_test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <stdlib.h>
66
#include <string.h>
77

8+
#include "FreeRTOS.h"
9+
#include "task.h"
10+
#include "list.h"
11+
812
/* Include header file(s) which have declaration
913
* of functions under test */
1014
#include "FreeRTOS_IP.h"

test/unit-test/stubs/FreeRTOS_ARP_stubs.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ NetworkAddressingParameters_t xNetworkAddressing = { 0, 0, 0, 0, 0 };
1212
/* The expected IP version and header length coded into the IP header itself. */
1313
#define ipIP_VERSION_AND_HEADER_LENGTH_BYTE ( ( uint8_t ) 0x45 )
1414

15+
void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
16+
{
17+
}
18+
/*-----------------------------------------------------------*/
19+
20+
BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
21+
TickType_t * const pxTicksToWait )
22+
{
23+
return pdTRUE;
24+
}
25+
/*-----------------------------------------------------------*/
26+
27+
void vTaskDelay( const TickType_t xTicksToDelay )
28+
{
29+
}
30+
/*-----------------------------------------------------------*/
31+
1532
BaseType_t xIsIPv4Multicast( uint32_t ulIPAddress )
1633
{
1734
BaseType_t xReturn;

0 commit comments

Comments
 (0)