From 553de6005fd96953e17d2a323d04f4b48417012b Mon Sep 17 00:00:00 2001 From: rtel Date: Thu, 30 Aug 2018 18:25:53 +0000 Subject: [PATCH] Fix mixed tabs and spaces in the latest TCP patches. git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2572 1d2547de-c912-0410-9cb9-b8ca96c0e9e2 --- .../Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c | 122 ++-- .../Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c | 625 +++++++++--------- .../Source/FreeRTOS-Plus-TCP/FreeRTOS_IP.c | 353 +++++----- .../FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c | 142 ++-- .../FreeRTOS-Plus-TCP/FreeRTOS_TCP_IP.c | 271 ++++---- .../FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c | 2 +- .../include/FreeRTOSIPConfigDefaults.h | 8 +- .../include/FreeRTOS_TCP_WIN.h | 4 +- 8 files changed, 758 insertions(+), 769 deletions(-) diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c index eef8ec848..8e475bf73 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c @@ -359,19 +359,19 @@ BaseType_t xGivingUp = pdFALSE; if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD ) { - xDHCPData.ulTransactionId = ipconfigRAND32( ); - - if( 0 != xDHCPData.ulTransactionId ) - { - xDHCPData.xDHCPTxTime = xTaskGetTickCount( ); - xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast; - prvSendDHCPDiscover( ); - FreeRTOS_debug_printf( ( "vDHCPProcess: timeout %lu ticks\n", xDHCPData.xDHCPTxPeriod ) ); - } - else - { - FreeRTOS_debug_printf( ( "vDHCPProcess: failed to generate a random Transaction ID\n" ) ); - } + xDHCPData.ulTransactionId = ipconfigRAND32( ); + + if( 0 != xDHCPData.ulTransactionId ) + { + xDHCPData.xDHCPTxTime = xTaskGetTickCount( ); + xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast; + prvSendDHCPDiscover( ); + FreeRTOS_debug_printf( ( "vDHCPProcess: timeout %lu ticks\n", xDHCPData.xDHCPTxPeriod ) ); + } + else + { + FreeRTOS_debug_printf( ( "vDHCPProcess: failed to generate a random Transaction ID\n" ) ); + } } else { @@ -592,23 +592,23 @@ TickType_t xTimeoutTime = ( TickType_t ) 0; static void prvInitialiseDHCP( void ) { /* Initialise the parameters that will be set by the DHCP process. Per - https://www.ietf.org/rfc/rfc2131.txt, Transaction ID should be a random - value chosen by the client. */ - xDHCPData.ulTransactionId = ipconfigRAND32(); - - /* Check for random number generator API failure. */ - if( 0 != xDHCPData.ulTransactionId ) - { - xDHCPData.xUseBroadcast = 0; - xDHCPData.ulOfferedIPAddress = 0UL; - xDHCPData.ulDHCPServerAddress = 0UL; - xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD; - - /* Create the DHCP socket if it has not already been created. */ - prvCreateDHCPSocket(); - FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) ); - vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD ); - } + https://www.ietf.org/rfc/rfc2131.txt, Transaction ID should be a random + value chosen by the client. */ + xDHCPData.ulTransactionId = ipconfigRAND32(); + + /* Check for random number generator API failure. */ + if( 0 != xDHCPData.ulTransactionId ) + { + xDHCPData.xUseBroadcast = 0; + xDHCPData.ulOfferedIPAddress = 0UL; + xDHCPData.ulDHCPServerAddress = 0UL; + xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD; + + /* Create the DHCP socket if it has not already been created. */ + prvCreateDHCPSocket(); + FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) ); + vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD ); + } } /*-----------------------------------------------------------*/ @@ -633,13 +633,13 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct /* Sanity check. */ if( ( lBytes >= sizeof( DHCPMessage_t ) ) && - ( pxDHCPMessage->ulDHCPCookie == ( uint32_t ) dhcpCOOKIE ) && + ( pxDHCPMessage->ulDHCPCookie == ( uint32_t ) dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == ( uint8_t ) dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == FreeRTOS_htonl( xDHCPData.ulTransactionId ) ) ) { - if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), - ( void * ) ipLOCAL_MAC_ADDRESS, - sizeof( MACAddress_t ) ) == 0 ) + if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), + ( void * ) ipLOCAL_MAC_ADDRESS, + sizeof( MACAddress_t ) ) == 0 ) { /* None of the essential options have been processed yet. */ ulProcessed = 0ul; @@ -665,36 +665,36 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct continue; } - /* Stop if the response is malformed. */ - if( pucByte < pucLastByte - 1 ) - { - ucLength = pucByte[ 1 ]; - pucByte += 2; - - if( pucByte >= pucLastByte - ucLength ) - { - break; - } - } - else - { - break; - } + /* Stop if the response is malformed. */ + if( pucByte < pucLastByte - 1 ) + { + ucLength = pucByte[ 1 ]; + pucByte += 2; + + if( pucByte >= pucLastByte - ucLength ) + { + break; + } + } + else + { + break; + } /* In most cases, a 4-byte network-endian parameter follows, just get it once here and use later. */ - if( ucLength >= sizeof( ulParameter ) ) - { - memcpy( ( void * ) &( ulParameter ), - ( void * ) pucByte, - ( size_t ) sizeof( ulParameter ) ); - } - else - { - ulParameter = 0; - } - - /* Option-specific handling. */ + if( ucLength >= sizeof( ulParameter ) ) + { + memcpy( ( void * ) &( ulParameter ), + ( void * ) pucByte, + ( size_t ) sizeof( ulParameter ) ); + } + else + { + ulParameter = 0; + } + + /* Option-specific handling. */ switch( ucOptionCode ) { case dhcpMESSAGE_TYPE_OPTION_CODE : diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c index 894b7240d..1897f408d 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c @@ -96,9 +96,9 @@ name field is an offset to the string, rather than the string itself. */ the query will be responded to with these flags: */ #define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500 ) -/* Flag DNS parsing errors in situations where an IPv4 address is the return +/* Flag DNS parsing errors in situations where an IPv4 address is the return type. */ -#define dnsPARSE_ERROR 0UL +#define dnsPARSE_ERROR 0UL /* * Create a socket and bind it to the standard DNS port number. Return the @@ -146,7 +146,7 @@ static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier { uint32_t ulIPAddress; /* The IP address of an ARP cache entry. */ char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /* The name of the host */ - uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */ + uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */ uint32_t ulTimeWhenAddedInSeconds; } DNSCacheRow_t; @@ -189,10 +189,10 @@ typedef struct xDNSTail DNSTail_t; #include "pack_struct_start.h" struct xDNSAnswerRecord { - uint16_t usType; - uint16_t usClass; - uint32_t ulTTL; - uint16_t usDataLength; + uint16_t usType; + uint16_t usClass; + uint32_t ulTTL; + uint16_t usDataLength; } #include "pack_struct_end.h" typedef struct xDNSAnswerRecord DNSAnswerRecord_t; @@ -416,67 +416,67 @@ uint32_t ulIPAddress = 0UL; TickType_t xReadTimeOut_ms = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME; TickType_t xIdentifier = 0; - /* If the supplied hostname is IP address, convert it to uint32_t - and return. */ - #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 ) - { - ulIPAddress = FreeRTOS_inet_addr( pcHostName ); - } - #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */ - - /* If a DNS cache is used then check the cache before issuing another DNS - request. */ - #if( ipconfigUSE_DNS_CACHE == 1 ) - { - if( ulIPAddress == 0UL ) - { - ulIPAddress = FreeRTOS_dnslookup( pcHostName ); - if( ulIPAddress != 0 ) - { - FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) ); - } - else - { - /* prvGetHostByName will be called to start a DNS lookup */ - } - } - } - #endif /* ipconfigUSE_DNS_CACHE == 1 */ - - /* Generate a unique identifier. */ - if( 0 == ulIPAddress ) - { - xIdentifier = ( TickType_t )ipconfigRAND32( ); - } - - #if( ipconfigDNS_USE_CALLBACKS != 0 ) - { - if( pCallback != NULL ) - { - if( ulIPAddress == 0UL ) - { - /* The user has provided a callback function, so do not block on recvfrom() */ - if( 0 != xIdentifier ) - { - xReadTimeOut_ms = 0; - vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t )xIdentifier ); - } - } - else - { - /* The IP address is known, do the call-back now. */ - pCallback( pcHostName, pvSearchID, ulIPAddress ); - } - } - } - #endif - - if( ulIPAddress == 0UL && 0 != xIdentifier ) - { - ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms ); - } - - return ulIPAddress; + /* If the supplied hostname is IP address, convert it to uint32_t + and return. */ + #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 ) + { + ulIPAddress = FreeRTOS_inet_addr( pcHostName ); + } + #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */ + + /* If a DNS cache is used then check the cache before issuing another DNS + request. */ + #if( ipconfigUSE_DNS_CACHE == 1 ) + { + if( ulIPAddress == 0UL ) + { + ulIPAddress = FreeRTOS_dnslookup( pcHostName ); + if( ulIPAddress != 0 ) + { + FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) ); + } + else + { + /* prvGetHostByName will be called to start a DNS lookup */ + } + } + } + #endif /* ipconfigUSE_DNS_CACHE == 1 */ + + /* Generate a unique identifier. */ + if( 0 == ulIPAddress ) + { + xIdentifier = ( TickType_t )ipconfigRAND32( ); + } + + #if( ipconfigDNS_USE_CALLBACKS != 0 ) + { + if( pCallback != NULL ) + { + if( ulIPAddress == 0UL ) + { + /* The user has provided a callback function, so do not block on recvfrom() */ + if( 0 != xIdentifier ) + { + xReadTimeOut_ms = 0; + vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t )xIdentifier ); + } + } + else + { + /* The IP address is known, do the call-back now. */ + pCallback( pcHostName, pvSearchID, ulIPAddress ); + } + } + } + #endif + + if( ( ulIPAddress == 0UL ) && ( 0 != xIdentifier ) ) + { + ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms ); + } + + return ulIPAddress; } /*-----------------------------------------------------------*/ @@ -675,71 +675,71 @@ static const DNSMessage_t xDefaultPartDNSHeader = static uint8_t *prvReadNameField( uint8_t *pucByte, size_t xSourceLen, char *pcName, size_t xDestLen ) { size_t xNameLen = 0; - BaseType_t xCount; + BaseType_t xCount; - if( 0 == xSourceLen ) - { - return NULL; - } + if( 0 == xSourceLen ) + { + return NULL; + } /* Determine if the name is the fully coded name, or an offset to the name elsewhere in the message. */ if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET ) { /* Jump over the two byte offset. */ - if( xSourceLen > sizeof( uint16_t ) ) - { - pucByte += sizeof( uint16_t ); - } - else - { - pucByte = NULL; - } + if( xSourceLen > sizeof( uint16_t ) ) + { + pucByte += sizeof( uint16_t ); + } + else + { + pucByte = NULL; + } } else { /* pucByte points to the full name. Walk over the string. */ - while( NULL != pucByte && *pucByte != 0x00 && xSourceLen > 1 ) + while( ( NULL != pucByte ) && ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) ) { - /* If this is not the first time through the loop, then add a - separator in the output. */ - if( xNameLen > 0 && xNameLen < xDestLen - 1 ) - { - pcName[ xNameLen++ ] = '.'; - } - - /* Process the first/next sub-string. */ + /* If this is not the first time through the loop, then add a + separator in the output. */ + if( ( xNameLen > 0 ) && ( xNameLen < ( xDestLen - 1 ) ) ) + { + pcName[ xNameLen++ ] = '.'; + } + + /* Process the first/next sub-string. */ for( xCount = *(pucByte++), xSourceLen--; - xCount-- && xSourceLen > 1; - pucByte++, xSourceLen-- ) + xCount-- && xSourceLen > 1; + pucByte++, xSourceLen-- ) { - if( xNameLen < xDestLen - 1 ) - { - pcName[ xNameLen++ ] = *( ( char * )pucByte ); - } - else - { - /* DNS name is too big for the provided buffer. */ - pucByte = NULL; - break; - } + if( xNameLen < xDestLen - 1 ) + { + pcName[ xNameLen++ ] = *( ( char * )pucByte ); + } + else + { + /* DNS name is too big for the provided buffer. */ + pucByte = NULL; + break; + } } } - /* Confirm that a fully formed name was found. */ - if( NULL != pucByte ) - { - if( 0x00 == *pucByte ) - { - pucByte++; - xSourceLen--; - pcName[ xNameLen++ ] = '\0'; - } - else - { - pucByte = NULL; - } - } + /* Confirm that a fully formed name was found. */ + if( NULL != pucByte ) + { + if( 0x00 == *pucByte ) + { + pucByte++; + xSourceLen--; + pcName[ xNameLen++ ] = '\0'; + } + else + { + pucByte = NULL; + } + } } return pucByte; @@ -749,58 +749,58 @@ static const DNSMessage_t xDefaultPartDNSHeader = static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen ) { - size_t xChunkLength; + size_t xChunkLength; - if( 0 == xSourceLen ) - { - return NULL; - } + if( 0 == xSourceLen ) + { + return NULL; + } - /* Determine if the name is the fully coded name, or an offset to the name + /* Determine if the name is the fully coded name, or an offset to the name elsewhere in the message. */ if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET ) { /* Jump over the two byte offset. */ - if( xSourceLen > sizeof( uint16_t ) ) - { - pucByte += sizeof( uint16_t ); - } - else - { - pucByte = NULL; - } + if( xSourceLen > sizeof( uint16_t ) ) + { + pucByte += sizeof( uint16_t ); + } + else + { + pucByte = NULL; + } } else { /* pucByte points to the full name. Walk over the string. */ - while( *pucByte != 0x00 && xSourceLen > 1 ) + while( ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) ) { - xChunkLength = *pucByte + 1; - - if( xSourceLen > xChunkLength ) - { - xSourceLen -= xChunkLength; - pucByte += xChunkLength; - } - else - { - pucByte = NULL; - break; - } + xChunkLength = *pucByte + 1; + + if( xSourceLen > xChunkLength ) + { + xSourceLen -= xChunkLength; + pucByte += xChunkLength; + } + else + { + pucByte = NULL; + break; + } } - /* Confirm that a fully formed name was found. */ - if( NULL != pucByte ) - { - if( 0x00 == *pucByte ) - { - pucByte++; - } - else - { - pucByte = NULL; - } - } + /* Confirm that a fully formed name was found. */ + if( NULL != pucByte ) + { + if( 0x00 == *pucByte ) + { + pucByte++; + } + else + { + pucByte = NULL; + } + } } return pucByte; @@ -822,12 +822,12 @@ DNSMessage_t *pxDNSMessageHeader; pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t ); pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer; - if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t ) ) - { - prvParseDNSReply( pucUDPPayloadBuffer, - xPlayloadBufferLength, - ( uint32_t )pxDNSMessageHeader->usIdentifier ); - } + if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t ) ) + { + prvParseDNSReply( pucUDPPayloadBuffer, + xPlayloadBufferLength, + ( uint32_t )pxDNSMessageHeader->usIdentifier ); + } /* The packet was not consumed. */ return pdFAIL; @@ -841,12 +841,12 @@ DNSMessage_t *pxDNSMessageHeader; UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer; uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t ); - if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t) ) - { - prvTreatNBNS( pucUDPPayloadBuffer, - pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t ), - pxUDPPacket->xIPHeader.ulSourceIPAddress ); - } + if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t) ) + { + prvTreatNBNS( pucUDPPayloadBuffer, + pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t ), + pxUDPPacket->xIPHeader.ulSourceIPAddress ); + } /* The packet was not consumed. */ return pdFAIL; @@ -870,27 +870,27 @@ uint16_t x, usDataLength, usQuestions; uint16_t usType = 0, usClass = 0; #endif #if( ipconfigUSE_DNS_CACHE == 1 ) - char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = ""; + char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = ""; #endif - /* Ensure that the buffer is of at least minimal DNS message length. */ - if( xBufferLength < sizeof( DNSMessage_t ) ) - { - return dnsPARSE_ERROR; - } - else - { - xSourceBytesRemaining = xBufferLength; - } - - /* Parse the DNS message header. */ + /* Ensure that the buffer is of at least minimal DNS message length. */ + if( xBufferLength < sizeof( DNSMessage_t ) ) + { + return dnsPARSE_ERROR; + } + else + { + xSourceBytesRemaining = xBufferLength; + } + + /* Parse the DNS message header. */ pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer; if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier ) { /* Start at the first byte after the header. */ pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t ); - xSourceBytesRemaining -= sizeof( DNSMessage_t ); + xSourceBytesRemaining -= sizeof( DNSMessage_t ); /* Skip any question records. */ usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions ); @@ -908,59 +908,59 @@ uint16_t x, usDataLength, usQuestions; #if( ipconfigUSE_DNS_CACHE == 1 ) if( x == 0 ) { - pucByte = prvReadNameField( pucByte, - xSourceBytesRemaining, - pcName, - sizeof( pcName ) ); - - /* Check for a malformed response. */ - if( NULL == pucByte ) - { - return dnsPARSE_ERROR; - } - else - { - xSourceBytesRemaining = ( pucUDPPayloadBuffer + xBufferLength ) - pucByte; - } + pucByte = prvReadNameField( pucByte, + xSourceBytesRemaining, + pcName, + sizeof( pcName ) ); + + /* Check for a malformed response. */ + if( NULL == pucByte ) + { + return dnsPARSE_ERROR; + } + else + { + xSourceBytesRemaining = ( pucUDPPayloadBuffer + xBufferLength ) - pucByte; + } } else #endif /* ipconfigUSE_DNS_CACHE */ { /* Skip the variable length pcName field. */ - pucByte = prvSkipNameField( pucByte, - xSourceBytesRemaining ); - - /* Check for a malformed response. */ - if( NULL == pucByte ) - { - return dnsPARSE_ERROR; - } - else - { - xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte; - } - } - - /* Check the remaining buffer size. */ - if( xSourceBytesRemaining >= sizeof( uint32_t ) ) - { - #if( ipconfigUSE_LLMNR == 1 ) - { - /* usChar2u16 returns value in host endianness */ - usType = usChar2u16( pucByte ); - usClass = usChar2u16( pucByte + 2 ); - } - #endif /* ipconfigUSE_LLMNR */ - - /* Skip the type and class fields. */ - pucByte += sizeof( uint32_t ); - xSourceBytesRemaining -= sizeof( uint32_t ); - } - else - { - /* Malformed response. */ - return dnsPARSE_ERROR; - } + pucByte = prvSkipNameField( pucByte, + xSourceBytesRemaining ); + + /* Check for a malformed response. */ + if( NULL == pucByte ) + { + return dnsPARSE_ERROR; + } + else + { + xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte; + } + } + + /* Check the remaining buffer size. */ + if( xSourceBytesRemaining >= sizeof( uint32_t ) ) + { + #if( ipconfigUSE_LLMNR == 1 ) + { + /* usChar2u16 returns value in host endianness */ + usType = usChar2u16( pucByte ); + usClass = usChar2u16( pucByte + 2 ); + } + #endif /* ipconfigUSE_LLMNR */ + + /* Skip the type and class fields. */ + pucByte += sizeof( uint32_t ); + xSourceBytesRemaining -= sizeof( uint32_t ); + } + else + { + /* Malformed response. */ + return dnsPARSE_ERROR; + } } /* Search through the answer records. */ @@ -971,33 +971,33 @@ uint16_t x, usDataLength, usQuestions; for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ ) { pucByte = prvSkipNameField( pucByte, - xSourceBytesRemaining ); - - /* Check for a malformed response. */ - if( NULL == pucByte ) - { - return dnsPARSE_ERROR; - } - else - { - xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte; - } - - /* Is there enough data for an IPv4 A record answer and, if so, - is this an A record? */ - if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) && - usChar2u16( pucByte ) == dnsTYPE_A_HOST ) + xSourceBytesRemaining ); + + /* Check for a malformed response. */ + if( NULL == pucByte ) + { + return dnsPARSE_ERROR; + } + else + { + xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte; + } + + /* Is there enough data for an IPv4 A record answer and, if so, + is this an A record? */ + if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) && + usChar2u16( pucByte ) == dnsTYPE_A_HOST ) { /* This is the required record type and is of sufficient size. */ - pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte; + pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte; /* Sanity check the data length of an IPv4 answer. */ if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == sizeof( uint32_t ) ) { /* Copy the IP address out of the record. */ - memcpy( &ulIPAddress, - pucByte + sizeof( DNSAnswerRecord_t ), - sizeof( uint32_t ) ); + memcpy( &ulIPAddress, + pucByte + sizeof( DNSAnswerRecord_t ), + sizeof( uint32_t ) ); #if( ipconfigUSE_DNS_CACHE == 1 ) { @@ -1012,32 +1012,32 @@ uint16_t x, usDataLength, usQuestions; #endif /* ipconfigDNS_USE_CALLBACKS != 0 */ } - pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ); - xSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) ); + pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ); + xSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) ); break; } else if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) ) { - /* It's not an A record, so skip it. Get the header location - and then jump over the header. */ - pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte; - pucByte += sizeof( DNSAnswerRecord_t ); - xSourceBytesRemaining -= sizeof( DNSAnswerRecord_t ); - + /* It's not an A record, so skip it. Get the header location + and then jump over the header. */ + pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte; + pucByte += sizeof( DNSAnswerRecord_t ); + xSourceBytesRemaining -= sizeof( DNSAnswerRecord_t ); + /* Determine the length of the answer data from the header. */ usDataLength = FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ); /* Jump over the answer. */ - if( xSourceBytesRemaining >= usDataLength ) - { - pucByte += usDataLength; - xSourceBytesRemaining -= usDataLength; - } - else - { - /* Malformed response. */ - return dnsPARSE_ERROR; - } + if( xSourceBytesRemaining >= usDataLength ) + { + pucByte += usDataLength; + xSourceBytesRemaining -= usDataLength; + } + else + { + /* Malformed response. */ + return dnsPARSE_ERROR; + } } } } @@ -1055,8 +1055,7 @@ uint16_t x, usDataLength, usQuestions; if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) ) { - BaseType_t xDataLength = xBufferLength + sizeof( UDPHeader_t ) + - sizeof( EthernetHeader_t ) + sizeof( IPHeader_t ); + BaseType_t xDataLength = xBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t ); /* The field xDataLength was set to the length of the UDP payload. The answer (reply) will be longer than the request, so the packet @@ -1130,13 +1129,13 @@ uint16_t x, usDataLength, usQuestions; uint8_t ucByte; uint8_t ucNBNSName[ 17 ]; - /* Check for minimum buffer size. */ - if( xBufferLength < sizeof( NBNSRequest_t ) ) - { - return; - } - - /* Read the request flags in host endianness. */ + /* Check for minimum buffer size. */ + if( xBufferLength < sizeof( NBNSRequest_t ) ) + { + return; + } + + /* Read the request flags in host endianness. */ usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) ); if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY ) @@ -1303,22 +1302,22 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 ); pxIPHeader = &pxUDPPacket->xIPHeader; pxUDPHeader = &pxUDPPacket->xUDPHeader; /* HT: started using defines like 'ipSIZE_OF_xxx' */ - pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER ); + pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER ); /* HT:endian: should not be translated, copying from packet to packet */ pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress; - pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER; - pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE; - pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier ); + pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER; + pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE; + pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier ); usPacketIdentifier++; - pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER ); + pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER ); vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort ); #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 ) { /* calculate the IP header checksum */ - pxIPHeader->usHeaderChecksum = 0x00; - pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER ); - pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum ); + pxIPHeader->usHeaderChecksum = 0x00; + pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER ); + pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum ); /* calculate the UDP checksum for outgoing package */ usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, lNetLength, pdTRUE ); @@ -1341,8 +1340,7 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 ); { BaseType_t x; BaseType_t xFound = pdFALSE; - uint32_t ulCurrentTimeSeconds = - xTaskGetTickCount( ) / portTICK_PERIOD_MS / 1000; + uint32_t ulCurrentTimeSeconds = ( xTaskGetTickCount() / portTICK_PERIOD_MS ) / 1000; static BaseType_t xFreeEntry = 0; /* For each entry in the DNS cache table. */ @@ -1358,24 +1356,22 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 ); /* Is this function called for a lookup or to add/update an IP address? */ if( xLookUp != pdFALSE ) { - /* Confirm that the record is still fresh. */ - if( ulCurrentTimeSeconds < - xDNSCache[ x ].ulTimeWhenAddedInSeconds + - FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) - { - *pulIP = xDNSCache[ x ].ulIPAddress; - } - else - { - /* Age out the old cached record. */ - xDNSCache[ x ].pcName[ 0 ] = 0; - } + /* Confirm that the record is still fresh. */ + if( ulCurrentTimeSeconds < ( xDNSCache[ x ].ulTimeWhenAddedInSeconds + FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) ) + { + *pulIP = xDNSCache[ x ].ulIPAddress; + } + else + { + /* Age out the old cached record. */ + xDNSCache[ x ].pcName[ 0 ] = 0; + } } else { xDNSCache[ x ].ulIPAddress = *pulIP; - xDNSCache[ x ].ulTTL = ulTTL; - xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds; + xDNSCache[ x ].ulTTL = ulTTL; + xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds; } xFound = pdTRUE; @@ -1392,20 +1388,20 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 ); else { /* Add or update the item. */ - if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH ) - { - strcpy( xDNSCache[ xFreeEntry ].pcName, pcName ); - - xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP; - xDNSCache[ xFreeEntry ].ulTTL = ulTTL; - xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds; - - xFreeEntry++; - if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES ) - { - xFreeEntry = 0; - } - } + if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH ) + { + strcpy( xDNSCache[ xFreeEntry ].pcName, pcName ); + + xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP; + xDNSCache[ xFreeEntry ].ulTTL = ulTTL; + xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds; + + xFreeEntry++; + if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES ) + { + xFreeEntry = 0; + } + } } } @@ -1423,5 +1419,6 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 ); /* Provide access to private members for testing. */ #ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS - #include "aws_freertos_tcp_test_access_dns_define.h" + #include "aws_freertos_tcp_test_access_dns_define.h" #endif + diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_IP.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_IP.c index fdb8fd116..996b52838 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_IP.c +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_IP.c @@ -357,7 +357,8 @@ struct freertos_sockaddr xAddress; /* Wait until there is something to do. If the following call exits * due to a time out rather than a message being received, set a * 'NoEvent' value. */ - if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE ) { + if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE ) + { xReceivedEvent.eEventType = eNoEvent; } @@ -803,9 +804,8 @@ void *pvReturn; if( pxNetworkBuffer != NULL ) { - /* Set the actual packet size in case a bigger buffer was returned. */ - pxNetworkBuffer->xDataLength = - sizeof( UDPPacket_t ) + xRequestedSizeBytes; + /* Set the actual packet size in case a bigger buffer was returned. */ + pxNetworkBuffer->xDataLength = sizeof( UDPPacket_t ) + xRequestedSizeBytes; /* Leave space for the UPD header. */ pvReturn = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] ); @@ -831,11 +831,11 @@ NetworkBufferDescriptor_t * pxNewBuffer; if( pxNewBuffer != NULL ) { - /* Set the actual packet size in case a bigger buffer than requested - was returned. */ - pxNewBuffer->xDataLength = xNewLength; + /* Set the actual packet size in case a bigger buffer than requested + was returned. */ + pxNewBuffer->xDataLength = xNewLength; - /* Copy the original packet information. */ + /* Copy the original packet information. */ pxNewBuffer->ulIPAddress = pxNetworkBuffer->ulIPAddress; pxNewBuffer->usPort = pxNetworkBuffer->usPort; pxNewBuffer->usBoundPort = pxNetworkBuffer->usBoundPort; @@ -995,13 +995,13 @@ BaseType_t xReturn = pdFALSE; memcpy( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) ucMACAddress, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES ); /* Prepare the sockets interface. */ - xReturn = vNetworkSocketsInit(); - - if( pdTRUE == xReturn ) - { - /* Create the task that processes Ethernet and stack events. */ - xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle ); - } + xReturn = vNetworkSocketsInit(); + + if( pdTRUE == xReturn ) + { + /* Create the task that processes Ethernet and stack events. */ + xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle ); + } } else { @@ -1350,47 +1350,47 @@ eFrameProcessingResult_t eReturned = eReleaseBuffer; configASSERT( pxNetworkBuffer ); /* Interpret the Ethernet frame. */ - if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) ) - { - eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer ); - pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer ); - - if( eReturned == eProcessBuffer ) - { - /* Interpret the received Ethernet packet. */ - switch( pxEthernetHeader->usFrameType ) - { - case ipARP_FRAME_TYPE: - /* The Ethernet frame contains an ARP packet. */ - if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) ) - { - eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer ); - } - else - { - eReturned = eReleaseBuffer; - } - break; - - case ipIPv4_FRAME_TYPE: - /* The Ethernet frame contains an IP packet. */ - if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) ) - { - eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer ); - } - else - { - eReturned = eReleaseBuffer; - } - break; - - default: - /* No other packet types are handled. Nothing to do. */ - eReturned = eReleaseBuffer; - break; - } - } - } + if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) ) + { + eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer ); + pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer ); + + if( eReturned == eProcessBuffer ) + { + /* Interpret the received Ethernet packet. */ + switch( pxEthernetHeader->usFrameType ) + { + case ipARP_FRAME_TYPE: + /* The Ethernet frame contains an ARP packet. */ + if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) ) + { + eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer ); + } + else + { + eReturned = eReleaseBuffer; + } + break; + + case ipIPv4_FRAME_TYPE: + /* The Ethernet frame contains an IP packet. */ + if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) ) + { + eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer ); + } + else + { + eReturned = eReleaseBuffer; + } + break; + + default: + /* No other packet types are handled. Nothing to do. */ + eReturned = eReleaseBuffer; + break; + } + } + } /* Perform any actions that resulted from processing the Ethernet frame. */ switch( eReturned ) @@ -1513,14 +1513,14 @@ IPHeader_t * pxIPHeader = &( pxIPPacket->xIPHeader ); UBaseType_t uxHeaderLength = ( UBaseType_t ) ( ( pxIPHeader->ucVersionHeaderLength & 0x0Fu ) << 2 ); uint8_t ucProtocol; - /* Bound the calculated header length: take away the Ethernet header size, - then check if the IP header is claiming to be longer than the remaining - total packet size. Also check for minimal header field length. */ - if( uxHeaderLength > pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER || - uxHeaderLength < ipSIZE_OF_IPv4_HEADER ) - { - return eReleaseBuffer; - } + /* Bound the calculated header length: take away the Ethernet header size, + then check if the IP header is claiming to be longer than the remaining + total packet size. Also check for minimal header field length. */ + if( ( uxHeaderLength > ( pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER ) ) || + ( uxHeaderLength < ipSIZE_OF_IPv4_HEADER ) ) + { + return eReleaseBuffer; + } ucProtocol = pxIPPacket->xIPHeader.ucProtocol; /* Check if the IP headers are acceptable and if it has our destination. */ @@ -1544,10 +1544,9 @@ uint8_t ucProtocol; memmove( pucTarget, pucSource, xMoveLen ); pxNetworkBuffer->xDataLength -= optlen; - /* Fix-up new version/header length field in IP packet. */ - pxIPHeader->ucVersionHeaderLength = - ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */ - ( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */ + /* Fix-up new version/header length field in IP packet. */ + pxIPHeader->ucVersionHeaderLength = ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */ + ( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */ } /* Add the IP and MAC addresses to the ARP table if they are not @@ -1573,18 +1572,18 @@ uint8_t ucProtocol; be able to validate what it receives. */ #if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) { - if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) ) - { - ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer ); - if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER ) - { - eReturn = prvProcessICMPPacket( pxICMPPacket ); - } - } - else - { - eReturn = eReleaseBuffer; - } + if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) ) + { + ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer ); + if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER ) + { + eReturn = prvProcessICMPPacket( pxICMPPacket ); + } + } + else + { + eReturn = eReleaseBuffer; + } } #endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */ break; @@ -1594,47 +1593,46 @@ uint8_t ucProtocol; /* The IP packet contained a UDP frame. */ UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer ); - /* Only proceed if the payload length indicated in the header - appears to be valid. */ - if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) ) - { - /* Ensure that downstream UDP packet handling has the lesser - * of: the actual network buffer Ethernet frame length, or - * the sender's UDP packet header payload length, minus the - * size of the UDP header. - * - * The size of the UDP packet structure in this implementation - * includes the size of the Ethernet header, the size of - * the IP header, and the size of the UDP header. - */ - - pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t ); - if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) < - pxNetworkBuffer->xDataLength ) - { - pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - - sizeof( UDPHeader_t ); - } - - /* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */ - pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort; - pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress; - - /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM: - * In some cases, the upper-layer checksum has been calculated - * by the NIC driver. - * - * Pass the packet payload to the UDP sockets implementation. */ - if( xProcessReceivedUDPPacket( pxNetworkBuffer, - pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS ) - { - eReturn = eFrameConsumed; - } - } - else - { - eReturn = eReleaseBuffer; - } + /* Only proceed if the payload length indicated in the header + appears to be valid. */ + if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) ) + { + /* Ensure that downstream UDP packet handling has the lesser + * of: the actual network buffer Ethernet frame length, or + * the sender's UDP packet header payload length, minus the + * size of the UDP header. + * + * The size of the UDP packet structure in this implementation + * includes the size of the Ethernet header, the size of + * the IP header, and the size of the UDP header. + */ + + pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t ); + if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) < + pxNetworkBuffer->xDataLength ) + { + pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ); + } + + /* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */ + pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort; + pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress; + + /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM: + * In some cases, the upper-layer checksum has been calculated + * by the NIC driver. + * + * Pass the packet payload to the UDP sockets implementation. */ + if( xProcessReceivedUDPPacket( pxNetworkBuffer, + pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS ) + { + eReturn = eFrameConsumed; + } + } + else + { + eReturn = eReleaseBuffer; + } } break; @@ -1803,47 +1801,46 @@ uint8_t ucProtocol; const char *pcType; #endif - /* Check for minimum packet size. */ - if( uxBufferLength < sizeof( IPPacket_t ) ) - { - return ipINVALID_LENGTH; - } + /* Check for minimum packet size. */ + if( uxBufferLength < sizeof( IPPacket_t ) ) + { + return ipINVALID_LENGTH; + } - /* Parse the packet length. */ + /* Parse the packet length. */ pxIPPacket = ( const IPPacket_t * ) pucEthernetBuffer; - - /* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header - Length field contains the length of the internet header in 32-bit words. */ - uxIPHeaderLength = ( UBaseType_t ) - ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) ); - - /* Check for minimum packet size. */ - if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) - { - return ipINVALID_LENGTH; - } - if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) ) - { - return ipINVALID_LENGTH; - } - - /* Identify the next protocol. */ - ucProtocol = pxIPPacket->xIPHeader.ucProtocol; - - /* N.B., if this IP packet header includes Options, then the following - assignment results in a pointer into the protocol packet with the Ethernet - and IP headers incorrectly aligned. However, either way, the "third" - protocol (Layer 3 or 4) header will be aligned, which is the convenience - of this calculation. */ - pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) ); - - /* Switch on the Layer 3/4 protocol. */ + + /* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header + Length field contains the length of the internet header in 32-bit words. */ + uxIPHeaderLength = ( UBaseType_t ) ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) ); + + /* Check for minimum packet size. */ + if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) + { + return ipINVALID_LENGTH; + } + if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) ) + { + return ipINVALID_LENGTH; + } + + /* Identify the next protocol. */ + ucProtocol = pxIPPacket->xIPHeader.ucProtocol; + + /* N.B., if this IP packet header includes Options, then the following + assignment results in a pointer into the protocol packet with the Ethernet + and IP headers incorrectly aligned. However, either way, the "third" + protocol (Layer 3 or 4) header will be aligned, which is the convenience + of this calculation. */ + pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) ); + + /* Switch on the Layer 3/4 protocol. */ if( ucProtocol == ( uint8_t ) ipPROTOCOL_UDP ) { - if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER ) - { - return ipINVALID_LENGTH; - } + if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER ) ) + { + return ipINVALID_LENGTH; + } pusChecksum = ( uint16_t * ) ( &( pxProtPack->xUDPPacket.xUDPHeader.usChecksum ) ); #if( ipconfigHAS_DEBUG_PRINTF != 0 ) @@ -1854,12 +1851,12 @@ uint8_t ucProtocol; } else if( ucProtocol == ( uint8_t ) ipPROTOCOL_TCP ) { - if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER ) - { - return ipINVALID_LENGTH; - } - - pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) ); + if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER ) ) + { + return ipINVALID_LENGTH; + } + + pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) ); #if( ipconfigHAS_DEBUG_PRINTF != 0 ) { pcType = "TCP"; @@ -1869,12 +1866,12 @@ uint8_t ucProtocol; else if( ( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP ) || ( ucProtocol == ( uint8_t ) ipPROTOCOL_IGMP ) ) { - if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER ) - { - return ipINVALID_LENGTH; - } - - pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) ); + if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER ) ) + { + return ipINVALID_LENGTH; + } + + pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) ); #if( ipconfigHAS_DEBUG_PRINTF != 0 ) { if( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP ) @@ -1894,8 +1891,8 @@ uint8_t ucProtocol; return ipUNHANDLED_PROTOCOL; } - /* The protocol and checksum field have been identified. Check the direction - of the packet. */ + /* The protocol and checksum field have been identified. Check the direction + of the packet. */ if( xOutgoingPacket != pdFALSE ) { /* This is an outgoing packet. Before calculating the checksum, set it @@ -2021,12 +2018,12 @@ uint8_t ucProtocol; * * Arguments: * ulSum: This argument provides a value to initialize the progressive summation - * of the header's values to. It is often 0, but protocols like TCP or UDP - * can have pseudo-header fields which need to be included in the checksum. + * of the header's values to. It is often 0, but protocols like TCP or UDP + * can have pseudo-header fields which need to be included in the checksum. * pucNextData: This argument contains the address of the first byte which this - * method should process. The method's memory iterator is initialized to this value. + * method should process. The method's memory iterator is initialized to this value. * uxDataLengthBytes: This argument contains the number of bytes that this method - * should process. + * should process. */ uint16_t usGenerateChecksum( uint32_t ulSum, const uint8_t * pucNextData, size_t uxDataLengthBytes ) { @@ -2226,8 +2223,8 @@ uint32_t FreeRTOS_GetNetmask( void ) void FreeRTOS_UpdateMACAddress( const uint8_t ucMACAddress[ipMAC_ADDRESS_LENGTH_BYTES] ) { - /* Copy the MAC address at the start of the default packet header fragment. */ - memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES ); + /* Copy the MAC address at the start of the default packet header fragment. */ + memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES ); } /*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c index 6ad9a86d3..896559a3d 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c @@ -48,16 +48,16 @@ port number. */ /* Test if a socket it bound which means it is either included in xBoundUDPSocketsList or xBoundTCPSocketsList */ -#define socketSOCKET_IS_BOUND( pxSocket ) ( listLIST_ITEM_CONTAINER( & ( pxSocket )->xBoundSocketListItem ) != NULL ) +#define socketSOCKET_IS_BOUND( pxSocket ) ( listLIST_ITEM_CONTAINER( & ( pxSocket )->xBoundSocketListItem ) != NULL ) /* If FreeRTOS_sendto() is called on a socket that is not bound to a port number then, depending on the FreeRTOSIPConfig.h settings, it might be that a port number is automatically generated for the socket. Automatically generated port numbers will be between socketAUTO_PORT_ALLOCATION_START_NUMBER and -0xffff. +0xffff. Per https://tools.ietf.org/html/rfc6056, "the dynamic ports consist of the range -49152-65535. However, ephemeral port selection algorithms should use the whole +49152-65535. However, ephemeral port selection algorithms should use the whole range 1024-65535" excluding those already in use (inbound or outbound). */ #if !defined( socketAUTO_PORT_ALLOCATION_START_NUMBER ) #define socketAUTO_PORT_ALLOCATION_START_NUMBER ( ( uint16_t ) 0x0400 ) @@ -190,15 +190,15 @@ BaseType_t xReturn = pdTRUE; BaseType_t vNetworkSocketsInit( void ) { - vListInitialise( &xBoundUDPSocketsList ); + vListInitialise( &xBoundUDPSocketsList ); - #if( ipconfigUSE_TCP == 1 ) - { - vListInitialise( &xBoundTCPSocketsList ); - } - #endif /* ipconfigUSE_TCP == 1 */ + #if( ipconfigUSE_TCP == 1 ) + { + vListInitialise( &xBoundTCPSocketsList ); + } + #endif /* ipconfigUSE_TCP == 1 */ - return pdTRUE; + return pdTRUE; } /*-----------------------------------------------------------*/ @@ -232,7 +232,7 @@ FreeRTOS_Socket_t *pxSocket; if( xType != FREERTOS_SOCK_DGRAM ) { xReturn = pdFAIL; - configASSERT( xReturn ); + configASSERT( xReturn ); } /* In case a UDP socket is created, do not allocate space for TCP data. */ *pxSocketSize = ( sizeof( *pxSocket ) - sizeof( pxSocket->u ) ) + sizeof( pxSocket->u.xUDP ); @@ -243,7 +243,7 @@ FreeRTOS_Socket_t *pxSocket; if( xType != FREERTOS_SOCK_STREAM ) { xReturn = pdFAIL; - configASSERT( xReturn ); + configASSERT( xReturn ); } *pxSocketSize = ( sizeof( *pxSocket ) - sizeof( pxSocket->u ) ) + sizeof( pxSocket->u.xTCP ); @@ -252,7 +252,7 @@ FreeRTOS_Socket_t *pxSocket; else { xReturn = pdFAIL; - configASSERT( xReturn ); + configASSERT( xReturn ); } } /* In case configASSERT() is not used */ @@ -317,9 +317,9 @@ Socket_t xReturn; listSET_LIST_ITEM_OWNER( &( pxSocket->xBoundSocketListItem ), ( void * ) pxSocket ); pxSocket->xReceiveBlockTime = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME; - pxSocket->xSendBlockTime = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME; + pxSocket->xSendBlockTime = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME; pxSocket->ucSocketOptions = ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT; - pxSocket->ucProtocol = ( uint8_t ) xProtocol; /* protocol: UDP or TCP */ + pxSocket->ucProtocol = ( uint8_t ) xProtocol; /* protocol: UDP or TCP */ #if( ipconfigUSE_TCP == 1 ) { @@ -327,7 +327,7 @@ Socket_t xReturn; { /* StreamSize is expressed in number of bytes */ /* Round up buffer sizes to nearest multiple of MSS */ - pxSocket->u.xTCP.usInitMSS = pxSocket->u.xTCP.usCurMSS = ipconfigTCP_MSS; + pxSocket->u.xTCP.usInitMSS = pxSocket->u.xTCP.usCurMSS = ipconfigTCP_MSS; pxSocket->u.xTCP.uxRxStreamSize = ( size_t ) ipconfigTCP_RX_BUFFER_LENGTH; pxSocket->u.xTCP.uxTxStreamSize = ( size_t ) FreeRTOS_round_up( ipconfigTCP_TX_BUFFER_LENGTH, ipconfigTCP_MSS ); /* Use half of the buffer size of the TCP windows */ @@ -1009,11 +1009,11 @@ List_t *pxSocketList; { if( pxAddress->sin_port == 0u ) { - pxAddress->sin_port = prvGetPrivatePortNumber( ( BaseType_t )pxSocket->ucProtocol ); - if( 0 == pxAddress->sin_port ) - { - return -pdFREERTOS_ERRNO_EADDRNOTAVAIL; - } + pxAddress->sin_port = prvGetPrivatePortNumber( ( BaseType_t )pxSocket->ucProtocol ); + if( 0 == pxAddress->sin_port ) + { + return -pdFREERTOS_ERRNO_EADDRNOTAVAIL; + } } /* If vSocketBind() is called from the API FreeRTOS_bind() it has been @@ -1610,9 +1610,9 @@ FreeRTOS_Socket_t *pxSocket; /* Find an available port number per https://tools.ietf.org/html/rfc6056. */ static uint16_t prvGetPrivatePortNumber( BaseType_t xProtocol ) { -const uint16_t usEphemeralPortCount = - socketAUTO_PORT_ALLOCATION_MAX_NUMBER - socketAUTO_PORT_ALLOCATION_START_NUMBER + 1; -uint16_t usIterations = usEphemeralPortCount; +const uint16_t usEphemeralPortCount = + socketAUTO_PORT_ALLOCATION_MAX_NUMBER - socketAUTO_PORT_ALLOCATION_START_NUMBER + 1; +uint16_t usIterations = usEphemeralPortCount; uint32_t ulRandomSeed = 0; uint16_t usResult = 0; BaseType_t xGotZeroOnce = pdFALSE; @@ -1632,52 +1632,52 @@ const List_t *pxList; /* Avoid compiler warnings if ipconfigUSE_TCP is not defined. */ ( void ) xProtocol; - /* Find the next available port using the random seed as a starting - point. */ - do - { - /* Generate a random seed. */ - ulRandomSeed = ipconfigRAND32( ); - - /* Only proceed if the random number generator succeeded. */ - if( 0 == ulRandomSeed ) - { - if( pdFALSE == xGotZeroOnce ) - { - xGotZeroOnce = pdTRUE; - continue; - } - else - { - break; - } - } - - /* Map the random to a candidate port. */ - usResult = - socketAUTO_PORT_ALLOCATION_START_NUMBER + - ( ( ( uint16_t )ulRandomSeed ) % usEphemeralPortCount ); - - /* Check if there's already an open socket with the same protocol - and port. */ - if( NULL == pxListFindListItemWithValue( - pxList, - ( TickType_t )FreeRTOS_htons( usResult ) ) ) - { - usResult = FreeRTOS_htons( usResult ); - break; - } - else - { - usResult = 0; - } - - usIterations--; - } - while( usIterations > 0 ); - - return usResult; -} + /* Find the next available port using the random seed as a starting + point. */ + do + { + /* Generate a random seed. */ + ulRandomSeed = ipconfigRAND32( ); + + /* Only proceed if the random number generator succeeded. */ + if( 0 == ulRandomSeed ) + { + if( pdFALSE == xGotZeroOnce ) + { + xGotZeroOnce = pdTRUE; + continue; + } + else + { + break; + } + } + + /* Map the random to a candidate port. */ + usResult = + socketAUTO_PORT_ALLOCATION_START_NUMBER + + ( ( ( uint16_t )ulRandomSeed ) % usEphemeralPortCount ); + + /* Check if there's already an open socket with the same protocol + and port. */ + if( NULL == pxListFindListItemWithValue( + pxList, + ( TickType_t )FreeRTOS_htons( usResult ) ) ) + { + usResult = FreeRTOS_htons( usResult ); + break; + } + else + { + usResult = 0; + } + + usIterations--; + } + while( usIterations > 0 ); + + return usResult; +} /*-----------------------------------------------------------*/ /* pxListFindListItemWithValue: find a list item in a bound socket list @@ -3339,7 +3339,7 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t *pxSocket ) } else { - FreeRTOS_printf( ( "Prot Port IP-Remote : Port R/T Status Alive tmout Child\n" ) ); + FreeRTOS_printf( ( "Prot Port IP-Remote : Port R/T Status Alive tmout Child\n" ) ); for( pxIterator = ( ListItem_t * ) listGET_HEAD_ENTRY( &xBoundTCPSocketsList ); pxIterator != ( ListItem_t * ) listGET_END_MARKER( &xBoundTCPSocketsList ); pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator ) ) diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_TCP_IP.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_TCP_IP.c index b8a6be04e..400a85556 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_TCP_IP.c +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_TCP_IP.c @@ -349,14 +349,13 @@ static NetworkBufferDescriptor_t *prvTCPBufferResize( FreeRTOS_Socket_t *pxSocke static uint8_t prvWinScaleFactor( FreeRTOS_Socket_t *pxSocket ); #endif -/* +/* * Generate a randomized TCP Initial Sequence Number per RFC. */ -extern uint32_t ulApplicationGetNextSequenceNumber( - uint32_t ulSourceAddress, - uint16_t usSourcePort, - uint32_t ulDestinationAddress, - uint16_t usDestinationPort ); +extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress, + uint16_t usSourcePort, + uint32_t ulDestinationAddress, + uint16_t usDestinationPort ); /*-----------------------------------------------------------*/ @@ -854,8 +853,8 @@ NetworkBufferDescriptor_t xTempBuffer; vFlip_32( pxTCPPacket->xTCPHeader.ulSequenceNumber, pxTCPPacket->xTCPHeader.ulAckNr ); } - pxIPHeader->ucTimeToLive = ( uint8_t ) ipconfigTCP_TIME_TO_LIVE; - pxIPHeader->usLength = FreeRTOS_htons( ulLen ); + pxIPHeader->ucTimeToLive = ( uint8_t ) ipconfigTCP_TIME_TO_LIVE; + pxIPHeader->usLength = FreeRTOS_htons( ulLen ); if( ( pxSocket == NULL ) || ( *ipLOCAL_IP_ADDRESS_POINTER == 0ul ) ) { /* When pxSocket is NULL, this function is called by prvTCPSendReset() @@ -1023,21 +1022,20 @@ uint32_t ulInitialSequenceNumber = 0; xReturn = pdFALSE; } - if( xReturn != pdFALSE ) - { - /* Get a difficult-to-predict initial sequence number for this 4-tuple. */ - ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( - *ipLOCAL_IP_ADDRESS_POINTER, - pxSocket->usLocalPort, - pxSocket->u.xTCP.ulRemoteIP, - pxSocket->u.xTCP.usRemotePort ); + if( xReturn != pdFALSE ) + { + /* Get a difficult-to-predict initial sequence number for this 4-tuple. */ + ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( *ipLOCAL_IP_ADDRESS_POINTER, + pxSocket->usLocalPort, + pxSocket->u.xTCP.ulRemoteIP, + pxSocket->u.xTCP.usRemotePort ); - /* Check for a random number generation error. */ - if( 0 == ulInitialSequenceNumber ) - { - xReturn = pdFALSE; - } - } + /* Check for a random number generation error. */ + if( 0 == ulInitialSequenceNumber ) + { + xReturn = pdFALSE; + } + } if( xReturn != pdFALSE ) { @@ -1152,17 +1150,17 @@ UBaseType_t uxNewMSS; pucLast = pucPtr + (((pxTCPHeader->ucTCPOffset >> 4) - 5) << 2); pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow; - /* Validate options size calculation. */ - if( pucLast > ( pxNetworkBuffer->pucEthernetBuffer + pxNetworkBuffer->xDataLength ) ) - { - return; - } + /* Validate options size calculation. */ + if( pucLast > ( pxNetworkBuffer->pucEthernetBuffer + pxNetworkBuffer->xDataLength ) ) + { + return; + } /* The comparison with pucLast is only necessary in case the option data are corrupted, we don't like to run into invalid memory and crash. */ while( pucPtr < pucLast ) { - UBaseType_t xRemainingOptionsBytes = pucLast - pucPtr; + UBaseType_t xRemainingOptionsBytes = pucLast - pucPtr; if( pucPtr[ 0 ] == TCP_OPT_END ) { @@ -1171,26 +1169,25 @@ UBaseType_t uxNewMSS; } if( pucPtr[ 0 ] == TCP_OPT_NOOP) { - /* NOP option, inserted to make the length a multiple of 4. */ - pucPtr++; - continue; + /* NOP option, inserted to make the length a multiple of 4. */ + pucPtr++; + continue; } - /* Any other well-formed option must be at least two bytes: the option - type byte followed by a length byte. */ - if( xRemainingOptionsBytes < 2 ) - { - break; - } + /* Any other well-formed option must be at least two bytes: the option + type byte followed by a length byte. */ + if( xRemainingOptionsBytes < 2 ) + { + break; + } #if( ipconfigUSE_TCP_WIN != 0 ) else if( pucPtr[ 0 ] == TCP_OPT_WSOPT ) { - /* Confirm that the option fits in the remaining buffer space. */ - if( xRemainingOptionsBytes < TCP_OPT_WSOPT_LEN || - pucPtr[ 1 ] != TCP_OPT_WSOPT_LEN ) - { - break; - } + /* Confirm that the option fits in the remaining buffer space. */ + if( ( xRemainingOptionsBytes < TCP_OPT_WSOPT_LEN ) || ( pucPtr[ 1 ] != TCP_OPT_WSOPT_LEN ) ) + { + break; + } pxSocket->u.xTCP.ucPeerWinScaleFactor = pucPtr[ 2 ]; pxSocket->u.xTCP.bits.bWinScaling = pdTRUE_UNSIGNED; @@ -1199,25 +1196,24 @@ UBaseType_t uxNewMSS; #endif /* ipconfigUSE_TCP_WIN */ else if( pucPtr[ 0 ] == TCP_OPT_MSS ) { - /* Confirm that the option fits in the remaining buffer space. */ - if( xRemainingOptionsBytes < TCP_OPT_MSS_LEN || - pucPtr[ 1 ] != TCP_OPT_MSS_LEN ) - { - break; - } - - /* An MSS option with the correct option length. FreeRTOS_htons() + /* Confirm that the option fits in the remaining buffer space. */ + if( ( xRemainingOptionsBytes < TCP_OPT_MSS_LEN )|| ( pucPtr[ 1 ] != TCP_OPT_MSS_LEN ) ) + { + break; + } + + /* An MSS option with the correct option length. FreeRTOS_htons() is not needed here because usChar2u16() already returns a host endian number. */ uxNewMSS = usChar2u16( pucPtr + 2 ); if( pxSocket->u.xTCP.usInitMSS != uxNewMSS ) { - /* Perform a basic check on the the new MSS. */ - if( uxNewMSS == 0 ) - { - break; - } + /* Perform a basic check on the the new MSS. */ + if( uxNewMSS == 0 ) + { + break; + } FreeRTOS_debug_printf( ( "MSS change %u -> %lu\n", pxSocket->u.xTCP.usInitMSS, uxNewMSS ) ); } @@ -1253,10 +1249,10 @@ UBaseType_t uxNewMSS; /* All other options have a length field, so that we easily can skip past them. */ unsigned char len = pucPtr[ 1 ]; - if( len < 2 || len > xRemainingOptionsBytes ) + if( ( len < 2 ) || ( len > xRemainingOptionsBytes ) ) { - /* If the length field is too small or too big, the options are malformed. - Don't process them further. */ + /* If the length field is too small or too big, the options are malformed. + Don't process them further. */ break; } @@ -1394,7 +1390,7 @@ UBaseType_t uxOptionsLength; pxTCPHeader->ucOptdata[ uxOptionsLength + 3 ] = 2; /* 2: length of this option. */ uxOptionsLength += 4u; - return uxOptionsLength; /* bytes, not words. */ + return uxOptionsLength; /* bytes, not words. */ } #endif /* ipconfigUSE_TCP_WIN == 0 */ } @@ -1639,8 +1635,8 @@ BaseType_t xResize; if( pxReturn != NULL ) { - /* Set the actual packet size, in case the returned buffer is larger. */ - pxReturn->xDataLength = lNeeded; + /* Set the actual packet size, in case the returned buffer is larger. */ + pxReturn->xDataLength = lNeeded; /* Copy the existing data to the new created buffer. */ if( pxNetworkBuffer ) @@ -2071,7 +2067,7 @@ int32_t lLength, lTCPHeaderLength, lReceiveLength, lUrgentLength; The size of the TCP header is given in a multiple of 4-byte words (single byte, needs no ntoh() translation). A shift-right 2: is the same as (offset >> 4) * 4. */ - lTCPHeaderLength = ( BaseType_t ) ( ( pxTCPHeader->ucTCPOffset & VALID_BITS_IN_TCP_OFFSET_BYTE ) >> 2 ); + lTCPHeaderLength = ( BaseType_t ) ( ( pxTCPHeader->ucTCPOffset & VALID_BITS_IN_TCP_OFFSET_BYTE ) >> 2 ); /* Let pucRecvData point to the first byte received. */ *ppucRecvData = pxNetworkBuffer->pucEthernetBuffer + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + lTCPHeaderLength; @@ -2436,7 +2432,7 @@ int32_t lDistance, lSendResult; { /* xTCPWindowTxDone returns true when all Tx queues are empty. */ bRxComplete = xTCPWindowRxEmpty( pxTCPWindow ); - bTxDone = xTCPWindowTxDone( pxTCPWindow ); + bTxDone = xTCPWindowTxDone( pxTCPWindow ); if( ( bRxComplete == 0 ) || ( bTxDone == 0 ) ) { @@ -2905,24 +2901,23 @@ uint32_t ulRemoteIP; uint16_t xRemotePort; BaseType_t xResult = pdPASS; - /* Check for a minimum packet size. */ - if( pxNetworkBuffer->xDataLength >= - ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) - { - ucTCPFlags = pxTCPPacket->xTCPHeader.ucTCPFlags; - ulLocalIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulDestinationIPAddress ); - xLocalPort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usDestinationPort ); - ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress ); - xRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort ); - - /* Find the destination socket, and if not found: return a socket listing to - the destination PORT. */ - pxSocket = ( FreeRTOS_Socket_t * )pxTCPSocketLookup( ulLocalIP, xLocalPort, ulRemoteIP, xRemotePort ); - } - else - { - return pdFAIL; - } + /* Check for a minimum packet size. */ + if( pxNetworkBuffer->xDataLength >= ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) ) + { + ucTCPFlags = pxTCPPacket->xTCPHeader.ucTCPFlags; + ulLocalIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulDestinationIPAddress ); + xLocalPort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usDestinationPort ); + ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress ); + xRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort ); + + /* Find the destination socket, and if not found: return a socket listing to + the destination PORT. */ + pxSocket = ( FreeRTOS_Socket_t * )pxTCPSocketLookup( ulLocalIP, xLocalPort, ulRemoteIP, xRemotePort ); + } + else + { + return pdFAIL; + } if( ( pxSocket == NULL ) || ( prvTCPSocketIsActive( ( UBaseType_t ) pxSocket->u.xTCP.ucTCPState ) == pdFALSE ) ) { @@ -3086,64 +3081,63 @@ TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuff FreeRTOS_Socket_t *pxReturn = NULL; uint32_t ulInitialSequenceNumber; - /* Assume that a new Initial Sequence Number will be required. Request - it now in order to fail out if necessary. */ - ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( - *ipLOCAL_IP_ADDRESS_POINTER, - pxSocket->usLocalPort, - pxTCPPacket->xIPHeader.ulSourceIPAddress, - pxTCPPacket->xTCPHeader.usSourcePort ); + /* Assume that a new Initial Sequence Number will be required. Request + it now in order to fail out if necessary. */ + ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( *ipLOCAL_IP_ADDRESS_POINTER, + pxSocket->usLocalPort, + pxTCPPacket->xIPHeader.ulSourceIPAddress, + pxTCPPacket->xTCPHeader.usSourcePort ); /* A pure SYN (without ACK) has come in, create a new socket to answer it. */ - if( 0 != ulInitialSequenceNumber ) - { - if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED ) - { - /* The flag bReuseSocket indicates that the same instance of the - listening socket should be used for the connection. */ - pxReturn = pxSocket; - pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED; - pxSocket->u.xTCP.pxPeerSocket = pxSocket; - } - else - { - /* The socket does not have the bReuseSocket flag set meaning create a - new socket when a connection comes in. */ - pxReturn = NULL; - - if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog ) - { - FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n", - pxSocket->usLocalPort, - pxSocket->u.xTCP.usChildCount, - pxSocket->u.xTCP.usBacklog, - pxSocket->u.xTCP.usChildCount == 1 ? "" : "ren" ) ); - prvTCPSendReset( pxNetworkBuffer ); - } - else - { - FreeRTOS_Socket_t *pxNewSocket = ( FreeRTOS_Socket_t * ) - FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP ); - - if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) ) - { - FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) ); - prvTCPSendReset( pxNetworkBuffer ); - } - else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE ) - { - /* The socket will be connected immediately, no time for the - owner to setsockopt's, therefore copy properties of the server - socket to the new socket. Only the binding might fail (due to - lack of resources). */ - pxReturn = pxNewSocket; - } - } - } - } - - if( 0 != ulInitialSequenceNumber && pxReturn != NULL ) + if( 0 != ulInitialSequenceNumber ) + { + if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED ) + { + /* The flag bReuseSocket indicates that the same instance of the + listening socket should be used for the connection. */ + pxReturn = pxSocket; + pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED; + pxSocket->u.xTCP.pxPeerSocket = pxSocket; + } + else + { + /* The socket does not have the bReuseSocket flag set meaning create a + new socket when a connection comes in. */ + pxReturn = NULL; + + if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog ) + { + FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n", + pxSocket->usLocalPort, + pxSocket->u.xTCP.usChildCount, + pxSocket->u.xTCP.usBacklog, + pxSocket->u.xTCP.usChildCount == 1 ? "" : "ren" ) ); + prvTCPSendReset( pxNetworkBuffer ); + } + else + { + FreeRTOS_Socket_t *pxNewSocket = ( FreeRTOS_Socket_t * ) + FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP ); + + if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) ) + { + FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) ); + prvTCPSendReset( pxNetworkBuffer ); + } + else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE ) + { + /* The socket will be connected immediately, no time for the + owner to setsockopt's, therefore copy properties of the server + socket to the new socket. Only the binding might fail (due to + lack of resources). */ + pxReturn = pxNewSocket; + } + } + } + } + + if( ( 0 != ulInitialSequenceNumber ) && ( pxReturn != NULL ) ) { pxReturn->u.xTCP.usRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort ); pxReturn->u.xTCP.ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress ); @@ -3310,5 +3304,6 @@ BaseType_t xResult = pdFALSE; /* Provide access to private members for testing. */ #ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS - #include "aws_freertos_tcp_test_access_tcp_define.h" + #include "aws_freertos_tcp_test_access_tcp_define.h" #endif + diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c index 94a8e17f8..532b89b91 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c @@ -244,7 +244,7 @@ FreeRTOS_Socket_t *pxSocket; UDPPacket_t *pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer; /* Caller must check for minimum packet size. */ - pxSocket = pxUDPSocketLookup( usPort ); + pxSocket = pxUDPSocketLookup( usPort ); if( pxSocket ) { diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOSIPConfigDefaults.h b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOSIPConfigDefaults.h index dd8ae07f4..4b3a74115 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOSIPConfigDefaults.h +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOSIPConfigDefaults.h @@ -389,9 +389,9 @@ from the FreeRTOSIPConfig.h configuration header file. */ #if( ipconfigUSE_DNS_CACHE != 0 ) #ifndef ipconfigDNS_CACHE_NAME_LENGTH - /* Per https://tools.ietf.org/html/rfc1035, 253 is the maximum string length - of a DNS name. The following default accounts for a null terminator. */ - #define ipconfigDNS_CACHE_NAME_LENGTH 254 + /* Per https://tools.ietf.org/html/rfc1035, 253 is the maximum string length + of a DNS name. The following default accounts for a null terminator. */ + #define ipconfigDNS_CACHE_NAME_LENGTH 254 #endif #ifndef ipconfigDNS_CACHE_ENTRIES @@ -535,7 +535,7 @@ connections, hang protection can help reduce the impact of SYN floods. */ /* Non-activity timeout is expressed in seconds. */ #ifndef ipconfigTCP_HANG_PROTECTION_TIME - #define ipconfigTCP_HANG_PROTECTION_TIME 30 + #define ipconfigTCP_HANG_PROTECTION_TIME 30 #endif #ifndef ipconfigTCP_IP_SANITY diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_TCP_WIN.h b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_TCP_WIN.h index 74bb71ebc..92b790297 100644 --- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_TCP_WIN.h +++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_TCP_WIN.h @@ -80,9 +80,9 @@ typedef struct xTCP_WINSIZE */ /* Keep this as a multiple of 4 */ #if( ipconfigUSE_TCP_WIN == 1 ) - #define ipSIZE_TCP_OPTIONS 16u + #define ipSIZE_TCP_OPTIONS 16u #else - #define ipSIZE_TCP_OPTIONS 12u + #define ipSIZE_TCP_OPTIONS 12u #endif /* -- 2.39.2