\r
if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )\r
{\r
- xDHCPData.ulTransactionId = ipconfigRAND32( );\r
-\r
- if( 0 != xDHCPData.ulTransactionId )\r
- {\r
- xDHCPData.xDHCPTxTime = xTaskGetTickCount( );\r
- xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast;\r
- prvSendDHCPDiscover( );\r
- FreeRTOS_debug_printf( ( "vDHCPProcess: timeout %lu ticks\n", xDHCPData.xDHCPTxPeriod ) );\r
- }\r
- else\r
- {\r
- FreeRTOS_debug_printf( ( "vDHCPProcess: failed to generate a random Transaction ID\n" ) );\r
- }\r
+ xDHCPData.ulTransactionId = ipconfigRAND32( );\r
+\r
+ if( 0 != xDHCPData.ulTransactionId )\r
+ {\r
+ xDHCPData.xDHCPTxTime = xTaskGetTickCount( );\r
+ xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast;\r
+ prvSendDHCPDiscover( );\r
+ FreeRTOS_debug_printf( ( "vDHCPProcess: timeout %lu ticks\n", xDHCPData.xDHCPTxPeriod ) );\r
+ }\r
+ else\r
+ {\r
+ FreeRTOS_debug_printf( ( "vDHCPProcess: failed to generate a random Transaction ID\n" ) );\r
+ }\r
}\r
else\r
{\r
static void prvInitialiseDHCP( void )\r
{\r
/* Initialise the parameters that will be set by the DHCP process. Per\r
- https://www.ietf.org/rfc/rfc2131.txt, Transaction ID should be a random\r
- value chosen by the client. */\r
- xDHCPData.ulTransactionId = ipconfigRAND32();\r
-\r
- /* Check for random number generator API failure. */\r
- if( 0 != xDHCPData.ulTransactionId )\r
- {\r
- xDHCPData.xUseBroadcast = 0;\r
- xDHCPData.ulOfferedIPAddress = 0UL;\r
- xDHCPData.ulDHCPServerAddress = 0UL;\r
- xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
-\r
- /* Create the DHCP socket if it has not already been created. */\r
- prvCreateDHCPSocket();\r
- FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) );\r
- vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );\r
- }\r
+ https://www.ietf.org/rfc/rfc2131.txt, Transaction ID should be a random\r
+ value chosen by the client. */\r
+ xDHCPData.ulTransactionId = ipconfigRAND32();\r
+\r
+ /* Check for random number generator API failure. */\r
+ if( 0 != xDHCPData.ulTransactionId )\r
+ {\r
+ xDHCPData.xUseBroadcast = 0;\r
+ xDHCPData.ulOfferedIPAddress = 0UL;\r
+ xDHCPData.ulDHCPServerAddress = 0UL;\r
+ xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
+\r
+ /* Create the DHCP socket if it has not already been created. */\r
+ prvCreateDHCPSocket();\r
+ FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) );\r
+ vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );\r
+ }\r
}\r
/*-----------------------------------------------------------*/\r
\r
\r
/* Sanity check. */\r
if( ( lBytes >= sizeof( DHCPMessage_t ) ) &&\r
- ( pxDHCPMessage->ulDHCPCookie == ( uint32_t ) dhcpCOOKIE ) &&\r
+ ( pxDHCPMessage->ulDHCPCookie == ( uint32_t ) dhcpCOOKIE ) &&\r
( pxDHCPMessage->ucOpcode == ( uint8_t ) dhcpREPLY_OPCODE ) &&\r
( pxDHCPMessage->ulTransactionID == FreeRTOS_htonl( xDHCPData.ulTransactionId ) ) )\r
{\r
- if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), \r
- ( void * ) ipLOCAL_MAC_ADDRESS, \r
- sizeof( MACAddress_t ) ) == 0 )\r
+ if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ),\r
+ ( void * ) ipLOCAL_MAC_ADDRESS,\r
+ sizeof( MACAddress_t ) ) == 0 )\r
{\r
/* None of the essential options have been processed yet. */\r
ulProcessed = 0ul;\r
continue;\r
}\r
\r
- /* Stop if the response is malformed. */\r
- if( pucByte < pucLastByte - 1 )\r
- {\r
- ucLength = pucByte[ 1 ];\r
- pucByte += 2;\r
-\r
- if( pucByte >= pucLastByte - ucLength )\r
- {\r
- break;\r
- }\r
- }\r
- else\r
- {\r
- break;\r
- } \r
+ /* Stop if the response is malformed. */\r
+ if( pucByte < pucLastByte - 1 )\r
+ {\r
+ ucLength = pucByte[ 1 ];\r
+ pucByte += 2;\r
+\r
+ if( pucByte >= pucLastByte - ucLength )\r
+ {\r
+ break;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ break;\r
+ }\r
\r
/* In most cases, a 4-byte network-endian parameter follows,\r
just get it once here and use later. */\r
- if( ucLength >= sizeof( ulParameter ) )\r
- {\r
- memcpy( ( void * ) &( ulParameter ), \r
- ( void * ) pucByte, \r
- ( size_t ) sizeof( ulParameter ) );\r
- }\r
- else\r
- {\r
- ulParameter = 0;\r
- }\r
-\r
- /* Option-specific handling. */\r
+ if( ucLength >= sizeof( ulParameter ) )\r
+ {\r
+ memcpy( ( void * ) &( ulParameter ),\r
+ ( void * ) pucByte,\r
+ ( size_t ) sizeof( ulParameter ) );\r
+ }\r
+ else\r
+ {\r
+ ulParameter = 0;\r
+ }\r
+\r
+ /* Option-specific handling. */\r
switch( ucOptionCode )\r
{\r
case dhcpMESSAGE_TYPE_OPTION_CODE :\r
the query will be responded to with these flags: */\r
#define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500 )\r
\r
-/* Flag DNS parsing errors in situations where an IPv4 address is the return \r
+/* Flag DNS parsing errors in situations where an IPv4 address is the return\r
type. */\r
-#define dnsPARSE_ERROR 0UL\r
+#define dnsPARSE_ERROR 0UL\r
\r
/*\r
* Create a socket and bind it to the standard DNS port number. Return the\r
{\r
uint32_t ulIPAddress; /* The IP address of an ARP cache entry. */\r
char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /* The name of the host */\r
- uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */\r
+ uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */\r
uint32_t ulTimeWhenAddedInSeconds;\r
} DNSCacheRow_t;\r
\r
#include "pack_struct_start.h"\r
struct xDNSAnswerRecord\r
{\r
- uint16_t usType;\r
- uint16_t usClass;\r
- uint32_t ulTTL;\r
- uint16_t usDataLength;\r
+ uint16_t usType;\r
+ uint16_t usClass;\r
+ uint32_t ulTTL;\r
+ uint16_t usDataLength;\r
}\r
#include "pack_struct_end.h"\r
typedef struct xDNSAnswerRecord DNSAnswerRecord_t;\r
TickType_t xReadTimeOut_ms = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME;\r
TickType_t xIdentifier = 0;\r
\r
- /* If the supplied hostname is IP address, convert it to uint32_t\r
- and return. */\r
- #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )\r
- {\r
- ulIPAddress = FreeRTOS_inet_addr( pcHostName );\r
- }\r
- #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */\r
-\r
- /* If a DNS cache is used then check the cache before issuing another DNS\r
- request. */\r
- #if( ipconfigUSE_DNS_CACHE == 1 )\r
- {\r
- if( ulIPAddress == 0UL )\r
- {\r
- ulIPAddress = FreeRTOS_dnslookup( pcHostName );\r
- if( ulIPAddress != 0 )\r
- {\r
- FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );\r
- }\r
- else\r
- {\r
- /* prvGetHostByName will be called to start a DNS lookup */\r
- }\r
- }\r
- }\r
- #endif /* ipconfigUSE_DNS_CACHE == 1 */\r
-\r
- /* Generate a unique identifier. */\r
- if( 0 == ulIPAddress )\r
- {\r
- xIdentifier = ( TickType_t )ipconfigRAND32( );\r
- }\r
-\r
- #if( ipconfigDNS_USE_CALLBACKS != 0 )\r
- {\r
- if( pCallback != NULL )\r
- {\r
- if( ulIPAddress == 0UL )\r
- {\r
- /* The user has provided a callback function, so do not block on recvfrom() */\r
- if( 0 != xIdentifier )\r
- {\r
- xReadTimeOut_ms = 0;\r
- vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t )xIdentifier );\r
- }\r
- }\r
- else\r
- {\r
- /* The IP address is known, do the call-back now. */\r
- pCallback( pcHostName, pvSearchID, ulIPAddress );\r
- }\r
- }\r
- }\r
- #endif\r
-\r
- if( ulIPAddress == 0UL && 0 != xIdentifier )\r
- {\r
- ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );\r
- }\r
-\r
- return ulIPAddress;\r
+ /* If the supplied hostname is IP address, convert it to uint32_t\r
+ and return. */\r
+ #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )\r
+ {\r
+ ulIPAddress = FreeRTOS_inet_addr( pcHostName );\r
+ }\r
+ #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */\r
+\r
+ /* If a DNS cache is used then check the cache before issuing another DNS\r
+ request. */\r
+ #if( ipconfigUSE_DNS_CACHE == 1 )\r
+ {\r
+ if( ulIPAddress == 0UL )\r
+ {\r
+ ulIPAddress = FreeRTOS_dnslookup( pcHostName );\r
+ if( ulIPAddress != 0 )\r
+ {\r
+ FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );\r
+ }\r
+ else\r
+ {\r
+ /* prvGetHostByName will be called to start a DNS lookup */\r
+ }\r
+ }\r
+ }\r
+ #endif /* ipconfigUSE_DNS_CACHE == 1 */\r
+\r
+ /* Generate a unique identifier. */\r
+ if( 0 == ulIPAddress )\r
+ {\r
+ xIdentifier = ( TickType_t )ipconfigRAND32( );\r
+ }\r
+\r
+ #if( ipconfigDNS_USE_CALLBACKS != 0 )\r
+ {\r
+ if( pCallback != NULL )\r
+ {\r
+ if( ulIPAddress == 0UL )\r
+ {\r
+ /* The user has provided a callback function, so do not block on recvfrom() */\r
+ if( 0 != xIdentifier )\r
+ {\r
+ xReadTimeOut_ms = 0;\r
+ vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t )xIdentifier );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* The IP address is known, do the call-back now. */\r
+ pCallback( pcHostName, pvSearchID, ulIPAddress );\r
+ }\r
+ }\r
+ }\r
+ #endif\r
+\r
+ if( ( ulIPAddress == 0UL ) && ( 0 != xIdentifier ) )\r
+ {\r
+ ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );\r
+ }\r
+\r
+ return ulIPAddress;\r
}\r
/*-----------------------------------------------------------*/\r
\r
static uint8_t *prvReadNameField( uint8_t *pucByte, size_t xSourceLen, char *pcName, size_t xDestLen )\r
{\r
size_t xNameLen = 0;\r
- BaseType_t xCount;\r
+ BaseType_t xCount;\r
\r
- if( 0 == xSourceLen )\r
- {\r
- return NULL;\r
- }\r
+ if( 0 == xSourceLen )\r
+ {\r
+ return NULL;\r
+ }\r
\r
/* Determine if the name is the fully coded name, or an offset to the name\r
elsewhere in the message. */\r
if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )\r
{\r
/* Jump over the two byte offset. */\r
- if( xSourceLen > sizeof( uint16_t ) )\r
- {\r
- pucByte += sizeof( uint16_t );\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
+ if( xSourceLen > sizeof( uint16_t ) )\r
+ {\r
+ pucByte += sizeof( uint16_t );\r
+ }\r
+ else\r
+ {\r
+ pucByte = NULL;\r
+ }\r
}\r
else\r
{\r
/* pucByte points to the full name. Walk over the string. */\r
- while( NULL != pucByte && *pucByte != 0x00 && xSourceLen > 1 )\r
+ while( ( NULL != pucByte ) && ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) )\r
{\r
- /* If this is not the first time through the loop, then add a \r
- separator in the output. */\r
- if( xNameLen > 0 && xNameLen < xDestLen - 1 )\r
- {\r
- pcName[ xNameLen++ ] = '.';\r
- }\r
-\r
- /* Process the first/next sub-string. */\r
+ /* If this is not the first time through the loop, then add a\r
+ separator in the output. */\r
+ if( ( xNameLen > 0 ) && ( xNameLen < ( xDestLen - 1 ) ) )\r
+ {\r
+ pcName[ xNameLen++ ] = '.';\r
+ }\r
+\r
+ /* Process the first/next sub-string. */\r
for( xCount = *(pucByte++), xSourceLen--;\r
- xCount-- && xSourceLen > 1; \r
- pucByte++, xSourceLen-- )\r
+ xCount-- && xSourceLen > 1;\r
+ pucByte++, xSourceLen-- )\r
{\r
- if( xNameLen < xDestLen - 1 )\r
- {\r
- pcName[ xNameLen++ ] = *( ( char * )pucByte );\r
- }\r
- else\r
- {\r
- /* DNS name is too big for the provided buffer. */\r
- pucByte = NULL;\r
- break;\r
- }\r
+ if( xNameLen < xDestLen - 1 )\r
+ {\r
+ pcName[ xNameLen++ ] = *( ( char * )pucByte );\r
+ }\r
+ else\r
+ {\r
+ /* DNS name is too big for the provided buffer. */\r
+ pucByte = NULL;\r
+ break;\r
+ }\r
}\r
}\r
\r
- /* Confirm that a fully formed name was found. */\r
- if( NULL != pucByte )\r
- {\r
- if( 0x00 == *pucByte )\r
- {\r
- pucByte++;\r
- xSourceLen--;\r
- pcName[ xNameLen++ ] = '\0';\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
- }\r
+ /* Confirm that a fully formed name was found. */\r
+ if( NULL != pucByte )\r
+ {\r
+ if( 0x00 == *pucByte )\r
+ {\r
+ pucByte++;\r
+ xSourceLen--;\r
+ pcName[ xNameLen++ ] = '\0';\r
+ }\r
+ else\r
+ {\r
+ pucByte = NULL;\r
+ }\r
+ }\r
}\r
\r
return pucByte;\r
\r
static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )\r
{\r
- size_t xChunkLength;\r
+ size_t xChunkLength;\r
\r
- if( 0 == xSourceLen )\r
- {\r
- return NULL;\r
- }\r
+ if( 0 == xSourceLen )\r
+ {\r
+ return NULL;\r
+ }\r
\r
- /* Determine if the name is the fully coded name, or an offset to the name\r
+ /* Determine if the name is the fully coded name, or an offset to the name\r
elsewhere in the message. */\r
if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )\r
{\r
/* Jump over the two byte offset. */\r
- if( xSourceLen > sizeof( uint16_t ) )\r
- {\r
- pucByte += sizeof( uint16_t );\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
+ if( xSourceLen > sizeof( uint16_t ) )\r
+ {\r
+ pucByte += sizeof( uint16_t );\r
+ }\r
+ else\r
+ {\r
+ pucByte = NULL;\r
+ }\r
}\r
else\r
{\r
/* pucByte points to the full name. Walk over the string. */\r
- while( *pucByte != 0x00 && xSourceLen > 1 )\r
+ while( ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) )\r
{\r
- xChunkLength = *pucByte + 1;\r
-\r
- if( xSourceLen > xChunkLength )\r
- {\r
- xSourceLen -= xChunkLength;\r
- pucByte += xChunkLength;\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- break;\r
- }\r
+ xChunkLength = *pucByte + 1;\r
+\r
+ if( xSourceLen > xChunkLength )\r
+ {\r
+ xSourceLen -= xChunkLength;\r
+ pucByte += xChunkLength;\r
+ }\r
+ else\r
+ {\r
+ pucByte = NULL;\r
+ break;\r
+ }\r
}\r
\r
- /* Confirm that a fully formed name was found. */\r
- if( NULL != pucByte )\r
- {\r
- if( 0x00 == *pucByte )\r
- {\r
- pucByte++;\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
- }\r
+ /* Confirm that a fully formed name was found. */\r
+ if( NULL != pucByte )\r
+ {\r
+ if( 0x00 == *pucByte )\r
+ {\r
+ pucByte++;\r
+ }\r
+ else\r
+ {\r
+ pucByte = NULL;\r
+ }\r
+ }\r
}\r
\r
return pucByte;\r
pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );\r
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
\r
- if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t ) )\r
- {\r
- prvParseDNSReply( pucUDPPayloadBuffer, \r
- xPlayloadBufferLength,\r
- ( uint32_t )pxDNSMessageHeader->usIdentifier );\r
- }\r
+ if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t ) )\r
+ {\r
+ prvParseDNSReply( pucUDPPayloadBuffer,\r
+ xPlayloadBufferLength,\r
+ ( uint32_t )pxDNSMessageHeader->usIdentifier );\r
+ }\r
\r
/* The packet was not consumed. */\r
return pdFAIL;\r
UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;\r
uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );\r
\r
- if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t) )\r
- {\r
- prvTreatNBNS( pucUDPPayloadBuffer, \r
- pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t ),\r
- pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
- }\r
+ if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t) )\r
+ {\r
+ prvTreatNBNS( pucUDPPayloadBuffer,\r
+ pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t ),\r
+ pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
+ }\r
\r
/* The packet was not consumed. */\r
return pdFAIL;\r
uint16_t usType = 0, usClass = 0;\r
#endif\r
#if( ipconfigUSE_DNS_CACHE == 1 )\r
- char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = ""; \r
+ char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = "";\r
#endif\r
\r
- /* Ensure that the buffer is of at least minimal DNS message length. */\r
- if( xBufferLength < sizeof( DNSMessage_t ) )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
- else\r
- {\r
- xSourceBytesRemaining = xBufferLength;\r
- }\r
-\r
- /* Parse the DNS message header. */\r
+ /* Ensure that the buffer is of at least minimal DNS message length. */\r
+ if( xBufferLength < sizeof( DNSMessage_t ) )\r
+ {\r
+ return dnsPARSE_ERROR;\r
+ }\r
+ else\r
+ {\r
+ xSourceBytesRemaining = xBufferLength;\r
+ }\r
+\r
+ /* Parse the DNS message header. */\r
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
\r
if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier )\r
{\r
/* Start at the first byte after the header. */\r
pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );\r
- xSourceBytesRemaining -= sizeof( DNSMessage_t );\r
+ xSourceBytesRemaining -= sizeof( DNSMessage_t );\r
\r
/* Skip any question records. */\r
usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );\r
#if( ipconfigUSE_DNS_CACHE == 1 )\r
if( x == 0 )\r
{\r
- pucByte = prvReadNameField( pucByte, \r
- xSourceBytesRemaining,\r
- pcName, \r
- sizeof( pcName ) );\r
- \r
- /* Check for a malformed response. */\r
- if( NULL == pucByte )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
- else\r
- {\r
- xSourceBytesRemaining = ( pucUDPPayloadBuffer + xBufferLength ) - pucByte;\r
- }\r
+ pucByte = prvReadNameField( pucByte,\r
+ xSourceBytesRemaining,\r
+ pcName,\r
+ sizeof( pcName ) );\r
+\r
+ /* Check for a malformed response. */\r
+ if( NULL == pucByte )\r
+ {\r
+ return dnsPARSE_ERROR;\r
+ }\r
+ else\r
+ {\r
+ xSourceBytesRemaining = ( pucUDPPayloadBuffer + xBufferLength ) - pucByte;\r
+ }\r
}\r
else\r
#endif /* ipconfigUSE_DNS_CACHE */\r
{\r
/* Skip the variable length pcName field. */\r
- pucByte = prvSkipNameField( pucByte, \r
- xSourceBytesRemaining );\r
- \r
- /* Check for a malformed response. */\r
- if( NULL == pucByte )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
- else\r
- {\r
- xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;\r
- }\r
- }\r
-\r
- /* Check the remaining buffer size. */\r
- if( xSourceBytesRemaining >= sizeof( uint32_t ) )\r
- {\r
- #if( ipconfigUSE_LLMNR == 1 )\r
- {\r
- /* usChar2u16 returns value in host endianness */\r
- usType = usChar2u16( pucByte );\r
- usClass = usChar2u16( pucByte + 2 );\r
- }\r
- #endif /* ipconfigUSE_LLMNR */\r
-\r
- /* Skip the type and class fields. */\r
- pucByte += sizeof( uint32_t );\r
- xSourceBytesRemaining -= sizeof( uint32_t );\r
- }\r
- else\r
- {\r
- /* Malformed response. */\r
- return dnsPARSE_ERROR;\r
- }\r
+ pucByte = prvSkipNameField( pucByte,\r
+ xSourceBytesRemaining );\r
+\r
+ /* Check for a malformed response. */\r
+ if( NULL == pucByte )\r
+ {\r
+ return dnsPARSE_ERROR;\r
+ }\r
+ else\r
+ {\r
+ xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;\r
+ }\r
+ }\r
+\r
+ /* Check the remaining buffer size. */\r
+ if( xSourceBytesRemaining >= sizeof( uint32_t ) )\r
+ {\r
+ #if( ipconfigUSE_LLMNR == 1 )\r
+ {\r
+ /* usChar2u16 returns value in host endianness */\r
+ usType = usChar2u16( pucByte );\r
+ usClass = usChar2u16( pucByte + 2 );\r
+ }\r
+ #endif /* ipconfigUSE_LLMNR */\r
+\r
+ /* Skip the type and class fields. */\r
+ pucByte += sizeof( uint32_t );\r
+ xSourceBytesRemaining -= sizeof( uint32_t );\r
+ }\r
+ else\r
+ {\r
+ /* Malformed response. */\r
+ return dnsPARSE_ERROR;\r
+ }\r
}\r
\r
/* Search through the answer records. */\r
for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )\r
{\r
pucByte = prvSkipNameField( pucByte,\r
- xSourceBytesRemaining );\r
-\r
- /* Check for a malformed response. */\r
- if( NULL == pucByte )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
- else\r
- {\r
- xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;\r
- }\r
-\r
- /* Is there enough data for an IPv4 A record answer and, if so, \r
- is this an A record? */\r
- if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) && \r
- usChar2u16( pucByte ) == dnsTYPE_A_HOST )\r
+ xSourceBytesRemaining );\r
+\r
+ /* Check for a malformed response. */\r
+ if( NULL == pucByte )\r
+ {\r
+ return dnsPARSE_ERROR;\r
+ }\r
+ else\r
+ {\r
+ xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;\r
+ }\r
+\r
+ /* Is there enough data for an IPv4 A record answer and, if so,\r
+ is this an A record? */\r
+ if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) &&\r
+ usChar2u16( pucByte ) == dnsTYPE_A_HOST )\r
{\r
/* This is the required record type and is of sufficient size. */\r
- pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte;\r
+ pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte;\r
\r
/* Sanity check the data length of an IPv4 answer. */\r
if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == sizeof( uint32_t ) )\r
{\r
/* Copy the IP address out of the record. */\r
- memcpy( &ulIPAddress, \r
- pucByte + sizeof( DNSAnswerRecord_t ),\r
- sizeof( uint32_t ) );\r
+ memcpy( &ulIPAddress,\r
+ pucByte + sizeof( DNSAnswerRecord_t ),\r
+ sizeof( uint32_t ) );\r
\r
#if( ipconfigUSE_DNS_CACHE == 1 )\r
{\r
#endif /* ipconfigDNS_USE_CALLBACKS != 0 */\r
}\r
\r
- pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t );\r
- xSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );\r
+ pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t );\r
+ xSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );\r
break;\r
}\r
else if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) )\r
{\r
- /* It's not an A record, so skip it. Get the header location \r
- and then jump over the header. */\r
- pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte;\r
- pucByte += sizeof( DNSAnswerRecord_t );\r
- xSourceBytesRemaining -= sizeof( DNSAnswerRecord_t );\r
- \r
+ /* It's not an A record, so skip it. Get the header location\r
+ and then jump over the header. */\r
+ pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte;\r
+ pucByte += sizeof( DNSAnswerRecord_t );\r
+ xSourceBytesRemaining -= sizeof( DNSAnswerRecord_t );\r
+\r
/* Determine the length of the answer data from the header. */\r
usDataLength = FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength );\r
\r
/* Jump over the answer. */\r
- if( xSourceBytesRemaining >= usDataLength )\r
- {\r
- pucByte += usDataLength;\r
- xSourceBytesRemaining -= usDataLength;\r
- }\r
- else\r
- {\r
- /* Malformed response. */\r
- return dnsPARSE_ERROR;\r
- }\r
+ if( xSourceBytesRemaining >= usDataLength )\r
+ {\r
+ pucByte += usDataLength;\r
+ xSourceBytesRemaining -= usDataLength;\r
+ }\r
+ else\r
+ {\r
+ /* Malformed response. */\r
+ return dnsPARSE_ERROR;\r
+ }\r
}\r
}\r
}\r
\r
if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )\r
{\r
- BaseType_t xDataLength = xBufferLength + sizeof( UDPHeader_t ) +\r
- sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );\r
+ BaseType_t xDataLength = xBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );\r
\r
/* The field xDataLength was set to the length of the UDP payload.\r
The answer (reply) will be longer than the request, so the packet\r
uint8_t ucByte;\r
uint8_t ucNBNSName[ 17 ];\r
\r
- /* Check for minimum buffer size. */\r
- if( xBufferLength < sizeof( NBNSRequest_t ) )\r
- {\r
- return;\r
- }\r
- \r
- /* Read the request flags in host endianness. */\r
+ /* Check for minimum buffer size. */\r
+ if( xBufferLength < sizeof( NBNSRequest_t ) )\r
+ {\r
+ return;\r
+ }\r
+\r
+ /* Read the request flags in host endianness. */\r
usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );\r
\r
if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )\r
pxIPHeader = &pxUDPPacket->xIPHeader;\r
pxUDPHeader = &pxUDPPacket->xUDPHeader;\r
/* HT: started using defines like 'ipSIZE_OF_xxx' */\r
- pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );\r
+ pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );\r
/* HT:endian: should not be translated, copying from packet to packet */\r
pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;\r
- pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;\r
- pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;\r
- pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );\r
+ pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;\r
+ pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;\r
+ pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );\r
usPacketIdentifier++;\r
- pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );\r
+ pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );\r
vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );\r
\r
#if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )\r
{\r
/* calculate the IP header checksum */\r
- pxIPHeader->usHeaderChecksum = 0x00;\r
- pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );\r
- pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );\r
+ pxIPHeader->usHeaderChecksum = 0x00;\r
+ pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );\r
+ pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );\r
\r
/* calculate the UDP checksum for outgoing package */\r
usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, lNetLength, pdTRUE );\r
{\r
BaseType_t x;\r
BaseType_t xFound = pdFALSE;\r
- uint32_t ulCurrentTimeSeconds = \r
- xTaskGetTickCount( ) / portTICK_PERIOD_MS / 1000;\r
+ uint32_t ulCurrentTimeSeconds = ( xTaskGetTickCount() / portTICK_PERIOD_MS ) / 1000;\r
static BaseType_t xFreeEntry = 0;\r
\r
/* For each entry in the DNS cache table. */\r
/* Is this function called for a lookup or to add/update an IP address? */\r
if( xLookUp != pdFALSE )\r
{\r
- /* Confirm that the record is still fresh. */\r
- if( ulCurrentTimeSeconds < \r
- xDNSCache[ x ].ulTimeWhenAddedInSeconds + \r
- FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) )\r
- {\r
- *pulIP = xDNSCache[ x ].ulIPAddress;\r
- }\r
- else\r
- {\r
- /* Age out the old cached record. */\r
- xDNSCache[ x ].pcName[ 0 ] = 0;\r
- }\r
+ /* Confirm that the record is still fresh. */\r
+ if( ulCurrentTimeSeconds < ( xDNSCache[ x ].ulTimeWhenAddedInSeconds + FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) )\r
+ {\r
+ *pulIP = xDNSCache[ x ].ulIPAddress;\r
+ }\r
+ else\r
+ {\r
+ /* Age out the old cached record. */\r
+ xDNSCache[ x ].pcName[ 0 ] = 0;\r
+ }\r
}\r
else\r
{\r
xDNSCache[ x ].ulIPAddress = *pulIP;\r
- xDNSCache[ x ].ulTTL = ulTTL;\r
- xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;\r
+ xDNSCache[ x ].ulTTL = ulTTL;\r
+ xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;\r
}\r
\r
xFound = pdTRUE;\r
else\r
{\r
/* Add or update the item. */\r
- if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH )\r
- {\r
- strcpy( xDNSCache[ xFreeEntry ].pcName, pcName );\r
-\r
- xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;\r
- xDNSCache[ xFreeEntry ].ulTTL = ulTTL;\r
- xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;\r
-\r
- xFreeEntry++;\r
- if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )\r
- {\r
- xFreeEntry = 0;\r
- }\r
- }\r
+ if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH )\r
+ {\r
+ strcpy( xDNSCache[ xFreeEntry ].pcName, pcName );\r
+\r
+ xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;\r
+ xDNSCache[ xFreeEntry ].ulTTL = ulTTL;\r
+ xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;\r
+\r
+ xFreeEntry++;\r
+ if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )\r
+ {\r
+ xFreeEntry = 0;\r
+ }\r
+ }\r
}\r
}\r
\r
\r
/* Provide access to private members for testing. */\r
#ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS\r
- #include "aws_freertos_tcp_test_access_dns_define.h"\r
+ #include "aws_freertos_tcp_test_access_dns_define.h"\r
#endif\r
+\r
/* Wait until there is something to do. If the following call exits\r
* due to a time out rather than a message being received, set a\r
* 'NoEvent' value. */\r
- if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE ) {\r
+ if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE ) \r
+ {\r
xReceivedEvent.eEventType = eNoEvent;\r
}\r
\r
\r
if( pxNetworkBuffer != NULL )\r
{\r
- /* Set the actual packet size in case a bigger buffer was returned. */\r
- pxNetworkBuffer->xDataLength = \r
- sizeof( UDPPacket_t ) + xRequestedSizeBytes;\r
+ /* Set the actual packet size in case a bigger buffer was returned. */\r
+ pxNetworkBuffer->xDataLength = sizeof( UDPPacket_t ) + xRequestedSizeBytes;\r
\r
/* Leave space for the UPD header. */\r
pvReturn = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );\r
\r
if( pxNewBuffer != NULL )\r
{\r
- /* Set the actual packet size in case a bigger buffer than requested \r
- was returned. */\r
- pxNewBuffer->xDataLength = xNewLength;\r
+ /* Set the actual packet size in case a bigger buffer than requested\r
+ was returned. */\r
+ pxNewBuffer->xDataLength = xNewLength;\r
\r
- /* Copy the original packet information. */\r
+ /* Copy the original packet information. */\r
pxNewBuffer->ulIPAddress = pxNetworkBuffer->ulIPAddress;\r
pxNewBuffer->usPort = pxNetworkBuffer->usPort;\r
pxNewBuffer->usBoundPort = pxNetworkBuffer->usBoundPort;\r
memcpy( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) ucMACAddress, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );\r
\r
/* Prepare the sockets interface. */\r
- xReturn = vNetworkSocketsInit();\r
- \r
- if( pdTRUE == xReturn )\r
- {\r
- /* Create the task that processes Ethernet and stack events. */\r
- xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );\r
- }\r
+ xReturn = vNetworkSocketsInit();\r
+\r
+ if( pdTRUE == xReturn )\r
+ {\r
+ /* Create the task that processes Ethernet and stack events. */\r
+ xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );\r
+ }\r
}\r
else\r
{\r
configASSERT( pxNetworkBuffer );\r
\r
/* Interpret the Ethernet frame. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) )\r
- {\r
- eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer );\r
- pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
-\r
- if( eReturned == eProcessBuffer )\r
- {\r
- /* Interpret the received Ethernet packet. */\r
- switch( pxEthernetHeader->usFrameType )\r
- {\r
- case ipARP_FRAME_TYPE:\r
- /* The Ethernet frame contains an ARP packet. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) )\r
- {\r
- eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer );\r
- }\r
- else\r
- {\r
- eReturned = eReleaseBuffer;\r
- }\r
- break;\r
-\r
- case ipIPv4_FRAME_TYPE:\r
- /* The Ethernet frame contains an IP packet. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) )\r
- {\r
- eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer );\r
- }\r
- else\r
- {\r
- eReturned = eReleaseBuffer;\r
- }\r
- break;\r
-\r
- default:\r
- /* No other packet types are handled. Nothing to do. */\r
- eReturned = eReleaseBuffer;\r
- break;\r
- }\r
- }\r
- }\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) )\r
+ {\r
+ eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer );\r
+ pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
+\r
+ if( eReturned == eProcessBuffer )\r
+ {\r
+ /* Interpret the received Ethernet packet. */\r
+ switch( pxEthernetHeader->usFrameType )\r
+ {\r
+ case ipARP_FRAME_TYPE:\r
+ /* The Ethernet frame contains an ARP packet. */\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) )\r
+ {\r
+ eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer );\r
+ }\r
+ else\r
+ {\r
+ eReturned = eReleaseBuffer;\r
+ }\r
+ break;\r
+\r
+ case ipIPv4_FRAME_TYPE:\r
+ /* The Ethernet frame contains an IP packet. */\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) )\r
+ {\r
+ eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer );\r
+ }\r
+ else\r
+ {\r
+ eReturned = eReleaseBuffer;\r
+ }\r
+ break;\r
+\r
+ default:\r
+ /* No other packet types are handled. Nothing to do. */\r
+ eReturned = eReleaseBuffer;\r
+ break;\r
+ }\r
+ }\r
+ }\r
\r
/* Perform any actions that resulted from processing the Ethernet frame. */\r
switch( eReturned )\r
UBaseType_t uxHeaderLength = ( UBaseType_t ) ( ( pxIPHeader->ucVersionHeaderLength & 0x0Fu ) << 2 );\r
uint8_t ucProtocol;\r
\r
- /* Bound the calculated header length: take away the Ethernet header size,\r
- then check if the IP header is claiming to be longer than the remaining\r
- total packet size. Also check for minimal header field length. */\r
- if( uxHeaderLength > pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER ||\r
- uxHeaderLength < ipSIZE_OF_IPv4_HEADER )\r
- {\r
- return eReleaseBuffer;\r
- }\r
+ /* Bound the calculated header length: take away the Ethernet header size,\r
+ then check if the IP header is claiming to be longer than the remaining\r
+ total packet size. Also check for minimal header field length. */\r
+ if( ( uxHeaderLength > ( pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER ) ) ||\r
+ ( uxHeaderLength < ipSIZE_OF_IPv4_HEADER ) )\r
+ {\r
+ return eReleaseBuffer;\r
+ }\r
\r
ucProtocol = pxIPPacket->xIPHeader.ucProtocol;\r
/* Check if the IP headers are acceptable and if it has our destination. */\r
memmove( pucTarget, pucSource, xMoveLen );\r
pxNetworkBuffer->xDataLength -= optlen;\r
\r
- /* Fix-up new version/header length field in IP packet. */\r
- pxIPHeader->ucVersionHeaderLength =\r
- ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */\r
- ( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */\r
+ /* Fix-up new version/header length field in IP packet. */\r
+ pxIPHeader->ucVersionHeaderLength = ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */\r
+ ( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */\r
}\r
\r
/* Add the IP and MAC addresses to the ARP table if they are not\r
be able to validate what it receives. */\r
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
{\r
- if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) )\r
- {\r
- ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
- if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )\r
- {\r
- eReturn = prvProcessICMPPacket( pxICMPPacket );\r
- }\r
- }\r
- else\r
- {\r
- eReturn = eReleaseBuffer;\r
- }\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) )\r
+ {\r
+ ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
+ if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )\r
+ {\r
+ eReturn = prvProcessICMPPacket( pxICMPPacket );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ eReturn = eReleaseBuffer;\r
+ }\r
}\r
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */\r
break;\r
/* The IP packet contained a UDP frame. */\r
UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );\r
\r
- /* Only proceed if the payload length indicated in the header\r
- appears to be valid. */\r
- if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )\r
- {\r
- /* Ensure that downstream UDP packet handling has the lesser\r
- * of: the actual network buffer Ethernet frame length, or \r
- * the sender's UDP packet header payload length, minus the\r
- * size of the UDP header. \r
- *\r
- * The size of the UDP packet structure in this implementation \r
- * includes the size of the Ethernet header, the size of \r
- * the IP header, and the size of the UDP header.\r
- */\r
- \r
- pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t );\r
- if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) <\r
- pxNetworkBuffer->xDataLength )\r
- {\r
- pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - \r
- sizeof( UDPHeader_t );\r
- }\r
-\r
- /* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */\r
- pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort;\r
- pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;\r
-\r
- /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM:\r
- * In some cases, the upper-layer checksum has been calculated\r
- * by the NIC driver.\r
- * \r
- * Pass the packet payload to the UDP sockets implementation. */\r
- if( xProcessReceivedUDPPacket( pxNetworkBuffer, \r
- pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS )\r
- {\r
- eReturn = eFrameConsumed;\r
- }\r
- }\r
- else\r
- {\r
- eReturn = eReleaseBuffer;\r
- }\r
+ /* Only proceed if the payload length indicated in the header\r
+ appears to be valid. */\r
+ if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )\r
+ {\r
+ /* Ensure that downstream UDP packet handling has the lesser\r
+ * of: the actual network buffer Ethernet frame length, or\r
+ * the sender's UDP packet header payload length, minus the\r
+ * size of the UDP header.\r
+ *\r
+ * The size of the UDP packet structure in this implementation\r
+ * includes the size of the Ethernet header, the size of\r
+ * the IP header, and the size of the UDP header.\r
+ */\r
+\r
+ pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t );\r
+ if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) <\r
+ pxNetworkBuffer->xDataLength )\r
+ {\r
+ pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t );\r
+ }\r
+\r
+ /* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */\r
+ pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort;\r
+ pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;\r
+\r
+ /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM:\r
+ * In some cases, the upper-layer checksum has been calculated\r
+ * by the NIC driver.\r
+ *\r
+ * Pass the packet payload to the UDP sockets implementation. */\r
+ if( xProcessReceivedUDPPacket( pxNetworkBuffer,\r
+ pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS )\r
+ {\r
+ eReturn = eFrameConsumed;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ eReturn = eReleaseBuffer;\r
+ }\r
}\r
break;\r
\r
const char *pcType;\r
#endif\r
\r
- /* Check for minimum packet size. */\r
- if( uxBufferLength < sizeof( IPPacket_t ) )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
+ /* Check for minimum packet size. */\r
+ if( uxBufferLength < sizeof( IPPacket_t ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
\r
- /* Parse the packet length. */\r
+ /* Parse the packet length. */\r
pxIPPacket = ( const IPPacket_t * ) pucEthernetBuffer;\r
- \r
- /* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header \r
- Length field contains the length of the internet header in 32-bit words. */\r
- uxIPHeaderLength = ( UBaseType_t ) \r
- ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) ); \r
-\r
- /* Check for minimum packet size. */\r
- if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
- if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
-\r
- /* Identify the next protocol. */\r
- ucProtocol = pxIPPacket->xIPHeader.ucProtocol; \r
-\r
- /* N.B., if this IP packet header includes Options, then the following \r
- assignment results in a pointer into the protocol packet with the Ethernet \r
- and IP headers incorrectly aligned. However, either way, the "third"\r
- protocol (Layer 3 or 4) header will be aligned, which is the convenience \r
- of this calculation. */\r
- pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) );\r
-\r
- /* Switch on the Layer 3/4 protocol. */\r
+\r
+ /* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header\r
+ Length field contains the length of the internet header in 32-bit words. */\r
+ uxIPHeaderLength = ( UBaseType_t ) ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) );\r
+\r
+ /* Check for minimum packet size. */\r
+ if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+ if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+\r
+ /* Identify the next protocol. */\r
+ ucProtocol = pxIPPacket->xIPHeader.ucProtocol;\r
+\r
+ /* N.B., if this IP packet header includes Options, then the following\r
+ assignment results in a pointer into the protocol packet with the Ethernet\r
+ and IP headers incorrectly aligned. However, either way, the "third"\r
+ protocol (Layer 3 or 4) header will be aligned, which is the convenience\r
+ of this calculation. */\r
+ pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) );\r
+\r
+ /* Switch on the Layer 3/4 protocol. */\r
if( ucProtocol == ( uint8_t ) ipPROTOCOL_UDP )\r
{\r
- if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
+ if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
\r
pusChecksum = ( uint16_t * ) ( &( pxProtPack->xUDPPacket.xUDPHeader.usChecksum ) );\r
#if( ipconfigHAS_DEBUG_PRINTF != 0 )\r
}\r
else if( ucProtocol == ( uint8_t ) ipPROTOCOL_TCP )\r
{\r
- if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
- \r
- pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) );\r
+ if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+\r
+ pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) );\r
#if( ipconfigHAS_DEBUG_PRINTF != 0 )\r
{\r
pcType = "TCP";\r
else if( ( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP ) ||\r
( ucProtocol == ( uint8_t ) ipPROTOCOL_IGMP ) )\r
{\r
- if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
- \r
- pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) );\r
+ if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+\r
+ pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) );\r
#if( ipconfigHAS_DEBUG_PRINTF != 0 )\r
{\r
if( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP )\r
return ipUNHANDLED_PROTOCOL;\r
}\r
\r
- /* The protocol and checksum field have been identified. Check the direction\r
- of the packet. */\r
+ /* The protocol and checksum field have been identified. Check the direction\r
+ of the packet. */\r
if( xOutgoingPacket != pdFALSE )\r
{\r
/* This is an outgoing packet. Before calculating the checksum, set it\r
*\r
* Arguments:\r
* ulSum: This argument provides a value to initialize the progressive summation\r
- * of the header's values to. It is often 0, but protocols like TCP or UDP\r
- * can have pseudo-header fields which need to be included in the checksum.\r
+ * of the header's values to. It is often 0, but protocols like TCP or UDP\r
+ * can have pseudo-header fields which need to be included in the checksum.\r
* pucNextData: This argument contains the address of the first byte which this\r
- * method should process. The method's memory iterator is initialized to this value.\r
+ * method should process. The method's memory iterator is initialized to this value.\r
* uxDataLengthBytes: This argument contains the number of bytes that this method\r
- * should process.\r
+ * should process.\r
*/\r
uint16_t usGenerateChecksum( uint32_t ulSum, const uint8_t * pucNextData, size_t uxDataLengthBytes )\r
{\r
\r
void FreeRTOS_UpdateMACAddress( const uint8_t ucMACAddress[ipMAC_ADDRESS_LENGTH_BYTES] )\r
{\r
- /* Copy the MAC address at the start of the default packet header fragment. */\r
- memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES );\r
+ /* Copy the MAC address at the start of the default packet header fragment. */\r
+ memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES );\r
}\r
/*-----------------------------------------------------------*/\r
\r
\r
/* Test if a socket it bound which means it is either included in\r
xBoundUDPSocketsList or xBoundTCPSocketsList */\r
-#define socketSOCKET_IS_BOUND( pxSocket ) ( listLIST_ITEM_CONTAINER( & ( pxSocket )->xBoundSocketListItem ) != NULL )\r
+#define socketSOCKET_IS_BOUND( pxSocket ) ( listLIST_ITEM_CONTAINER( & ( pxSocket )->xBoundSocketListItem ) != NULL )\r
\r
/* If FreeRTOS_sendto() is called on a socket that is not bound to a port\r
number then, depending on the FreeRTOSIPConfig.h settings, it might be that a\r
port number is automatically generated for the socket. Automatically generated\r
port numbers will be between socketAUTO_PORT_ALLOCATION_START_NUMBER and\r
-0xffff. \r
+0xffff.\r
\r
Per https://tools.ietf.org/html/rfc6056, "the dynamic ports consist of the range\r
-49152-65535. However, ephemeral port selection algorithms should use the whole \r
+49152-65535. However, ephemeral port selection algorithms should use the whole\r
range 1024-65535" excluding those already in use (inbound or outbound). */\r
#if !defined( socketAUTO_PORT_ALLOCATION_START_NUMBER )\r
#define socketAUTO_PORT_ALLOCATION_START_NUMBER ( ( uint16_t ) 0x0400 )\r
\r
BaseType_t vNetworkSocketsInit( void )\r
{\r
- vListInitialise( &xBoundUDPSocketsList );\r
+ vListInitialise( &xBoundUDPSocketsList );\r
\r
- #if( ipconfigUSE_TCP == 1 )\r
- {\r
- vListInitialise( &xBoundTCPSocketsList );\r
- }\r
- #endif /* ipconfigUSE_TCP == 1 */\r
+ #if( ipconfigUSE_TCP == 1 )\r
+ {\r
+ vListInitialise( &xBoundTCPSocketsList );\r
+ }\r
+ #endif /* ipconfigUSE_TCP == 1 */\r
\r
- return pdTRUE;\r
+ return pdTRUE;\r
}\r
/*-----------------------------------------------------------*/\r
\r
if( xType != FREERTOS_SOCK_DGRAM )\r
{\r
xReturn = pdFAIL;\r
- configASSERT( xReturn );\r
+ configASSERT( xReturn );\r
}\r
/* In case a UDP socket is created, do not allocate space for TCP data. */\r
*pxSocketSize = ( sizeof( *pxSocket ) - sizeof( pxSocket->u ) ) + sizeof( pxSocket->u.xUDP );\r
if( xType != FREERTOS_SOCK_STREAM )\r
{\r
xReturn = pdFAIL;\r
- configASSERT( xReturn );\r
+ configASSERT( xReturn );\r
}\r
\r
*pxSocketSize = ( sizeof( *pxSocket ) - sizeof( pxSocket->u ) ) + sizeof( pxSocket->u.xTCP );\r
else\r
{\r
xReturn = pdFAIL;\r
- configASSERT( xReturn );\r
+ configASSERT( xReturn );\r
}\r
}\r
/* In case configASSERT() is not used */\r
listSET_LIST_ITEM_OWNER( &( pxSocket->xBoundSocketListItem ), ( void * ) pxSocket );\r
\r
pxSocket->xReceiveBlockTime = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME;\r
- pxSocket->xSendBlockTime = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;\r
+ pxSocket->xSendBlockTime = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;\r
pxSocket->ucSocketOptions = ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT;\r
- pxSocket->ucProtocol = ( uint8_t ) xProtocol; /* protocol: UDP or TCP */\r
+ pxSocket->ucProtocol = ( uint8_t ) xProtocol; /* protocol: UDP or TCP */\r
\r
#if( ipconfigUSE_TCP == 1 )\r
{\r
{\r
/* StreamSize is expressed in number of bytes */\r
/* Round up buffer sizes to nearest multiple of MSS */\r
- pxSocket->u.xTCP.usInitMSS = pxSocket->u.xTCP.usCurMSS = ipconfigTCP_MSS;\r
+ pxSocket->u.xTCP.usInitMSS = pxSocket->u.xTCP.usCurMSS = ipconfigTCP_MSS;\r
pxSocket->u.xTCP.uxRxStreamSize = ( size_t ) ipconfigTCP_RX_BUFFER_LENGTH;\r
pxSocket->u.xTCP.uxTxStreamSize = ( size_t ) FreeRTOS_round_up( ipconfigTCP_TX_BUFFER_LENGTH, ipconfigTCP_MSS );\r
/* Use half of the buffer size of the TCP windows */\r
{\r
if( pxAddress->sin_port == 0u )\r
{\r
- pxAddress->sin_port = prvGetPrivatePortNumber( ( BaseType_t )pxSocket->ucProtocol ); \r
- if( 0 == pxAddress->sin_port )\r
- {\r
- return -pdFREERTOS_ERRNO_EADDRNOTAVAIL;\r
- }\r
+ pxAddress->sin_port = prvGetPrivatePortNumber( ( BaseType_t )pxSocket->ucProtocol );\r
+ if( 0 == pxAddress->sin_port )\r
+ {\r
+ return -pdFREERTOS_ERRNO_EADDRNOTAVAIL;\r
+ }\r
}\r
\r
/* If vSocketBind() is called from the API FreeRTOS_bind() it has been\r
/* Find an available port number per https://tools.ietf.org/html/rfc6056. */\r
static uint16_t prvGetPrivatePortNumber( BaseType_t xProtocol )\r
{\r
-const uint16_t usEphemeralPortCount = \r
- socketAUTO_PORT_ALLOCATION_MAX_NUMBER - socketAUTO_PORT_ALLOCATION_START_NUMBER + 1;\r
-uint16_t usIterations = usEphemeralPortCount; \r
+const uint16_t usEphemeralPortCount =\r
+ socketAUTO_PORT_ALLOCATION_MAX_NUMBER - socketAUTO_PORT_ALLOCATION_START_NUMBER + 1;\r
+uint16_t usIterations = usEphemeralPortCount;\r
uint32_t ulRandomSeed = 0;\r
uint16_t usResult = 0;\r
BaseType_t xGotZeroOnce = pdFALSE;\r
/* Avoid compiler warnings if ipconfigUSE_TCP is not defined. */\r
( void ) xProtocol;\r
\r
- /* Find the next available port using the random seed as a starting \r
- point. */\r
- do\r
- {\r
- /* Generate a random seed. */\r
- ulRandomSeed = ipconfigRAND32( );\r
-\r
- /* Only proceed if the random number generator succeeded. */\r
- if( 0 == ulRandomSeed )\r
- {\r
- if( pdFALSE == xGotZeroOnce )\r
- {\r
- xGotZeroOnce = pdTRUE;\r
- continue;\r
- }\r
- else\r
- {\r
- break;\r
- }\r
- }\r
-\r
- /* Map the random to a candidate port. */\r
- usResult =\r
- socketAUTO_PORT_ALLOCATION_START_NUMBER +\r
- ( ( ( uint16_t )ulRandomSeed ) % usEphemeralPortCount );\r
-\r
- /* Check if there's already an open socket with the same protocol\r
- and port. */\r
- if( NULL == pxListFindListItemWithValue( \r
- pxList, \r
- ( TickType_t )FreeRTOS_htons( usResult ) ) )\r
- {\r
- usResult = FreeRTOS_htons( usResult );\r
- break;\r
- }\r
- else\r
- {\r
- usResult = 0;\r
- }\r
-\r
- usIterations--;\r
- }\r
- while( usIterations > 0 );\r
- \r
- return usResult;\r
-} \r
+ /* Find the next available port using the random seed as a starting\r
+ point. */\r
+ do\r
+ {\r
+ /* Generate a random seed. */\r
+ ulRandomSeed = ipconfigRAND32( );\r
+\r
+ /* Only proceed if the random number generator succeeded. */\r
+ if( 0 == ulRandomSeed )\r
+ {\r
+ if( pdFALSE == xGotZeroOnce )\r
+ {\r
+ xGotZeroOnce = pdTRUE;\r
+ continue;\r
+ }\r
+ else\r
+ {\r
+ break;\r
+ }\r
+ }\r
+\r
+ /* Map the random to a candidate port. */\r
+ usResult =\r
+ socketAUTO_PORT_ALLOCATION_START_NUMBER +\r
+ ( ( ( uint16_t )ulRandomSeed ) % usEphemeralPortCount );\r
+\r
+ /* Check if there's already an open socket with the same protocol\r
+ and port. */\r
+ if( NULL == pxListFindListItemWithValue(\r
+ pxList,\r
+ ( TickType_t )FreeRTOS_htons( usResult ) ) )\r
+ {\r
+ usResult = FreeRTOS_htons( usResult );\r
+ break;\r
+ }\r
+ else\r
+ {\r
+ usResult = 0;\r
+ }\r
+\r
+ usIterations--;\r
+ }\r
+ while( usIterations > 0 );\r
+\r
+ return usResult;\r
+}\r
/*-----------------------------------------------------------*/\r
\r
/* pxListFindListItemWithValue: find a list item in a bound socket list\r
}\r
else\r
{\r
- FreeRTOS_printf( ( "Prot Port IP-Remote : Port R/T Status Alive tmout Child\n" ) );\r
+ FreeRTOS_printf( ( "Prot Port IP-Remote : Port R/T Status Alive tmout Child\n" ) );\r
for( pxIterator = ( ListItem_t * ) listGET_HEAD_ENTRY( &xBoundTCPSocketsList );\r
pxIterator != ( ListItem_t * ) listGET_END_MARKER( &xBoundTCPSocketsList );\r
pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator ) )\r
static uint8_t prvWinScaleFactor( FreeRTOS_Socket_t *pxSocket );\r
#endif\r
\r
-/* \r
+/*\r
* Generate a randomized TCP Initial Sequence Number per RFC.\r
*/\r
-extern uint32_t ulApplicationGetNextSequenceNumber(\r
- uint32_t ulSourceAddress,\r
- uint16_t usSourcePort,\r
- uint32_t ulDestinationAddress,\r
- uint16_t usDestinationPort );\r
+extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,\r
+ uint16_t usSourcePort,\r
+ uint32_t ulDestinationAddress,\r
+ uint16_t usDestinationPort );\r
\r
/*-----------------------------------------------------------*/\r
\r
vFlip_32( pxTCPPacket->xTCPHeader.ulSequenceNumber, pxTCPPacket->xTCPHeader.ulAckNr );\r
}\r
\r
- pxIPHeader->ucTimeToLive = ( uint8_t ) ipconfigTCP_TIME_TO_LIVE;\r
- pxIPHeader->usLength = FreeRTOS_htons( ulLen );\r
+ pxIPHeader->ucTimeToLive = ( uint8_t ) ipconfigTCP_TIME_TO_LIVE;\r
+ pxIPHeader->usLength = FreeRTOS_htons( ulLen );\r
if( ( pxSocket == NULL ) || ( *ipLOCAL_IP_ADDRESS_POINTER == 0ul ) )\r
{\r
/* When pxSocket is NULL, this function is called by prvTCPSendReset()\r
xReturn = pdFALSE;\r
}\r
\r
- if( xReturn != pdFALSE )\r
- {\r
- /* Get a difficult-to-predict initial sequence number for this 4-tuple. */\r
- ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( \r
- *ipLOCAL_IP_ADDRESS_POINTER, \r
- pxSocket->usLocalPort, \r
- pxSocket->u.xTCP.ulRemoteIP, \r
- pxSocket->u.xTCP.usRemotePort );\r
+ if( xReturn != pdFALSE )\r
+ {\r
+ /* Get a difficult-to-predict initial sequence number for this 4-tuple. */\r
+ ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( *ipLOCAL_IP_ADDRESS_POINTER,\r
+ pxSocket->usLocalPort,\r
+ pxSocket->u.xTCP.ulRemoteIP,\r
+ pxSocket->u.xTCP.usRemotePort );\r
\r
- /* Check for a random number generation error. */\r
- if( 0 == ulInitialSequenceNumber )\r
- {\r
- xReturn = pdFALSE;\r
- }\r
- }\r
+ /* Check for a random number generation error. */\r
+ if( 0 == ulInitialSequenceNumber )\r
+ {\r
+ xReturn = pdFALSE;\r
+ }\r
+ }\r
\r
if( xReturn != pdFALSE )\r
{\r
pucLast = pucPtr + (((pxTCPHeader->ucTCPOffset >> 4) - 5) << 2);\r
pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;\r
\r
- /* Validate options size calculation. */\r
- if( pucLast > ( pxNetworkBuffer->pucEthernetBuffer + pxNetworkBuffer->xDataLength ) )\r
- {\r
- return;\r
- }\r
+ /* Validate options size calculation. */\r
+ if( pucLast > ( pxNetworkBuffer->pucEthernetBuffer + pxNetworkBuffer->xDataLength ) )\r
+ {\r
+ return;\r
+ }\r
\r
/* The comparison with pucLast is only necessary in case the option data are\r
corrupted, we don't like to run into invalid memory and crash. */\r
while( pucPtr < pucLast )\r
{\r
- UBaseType_t xRemainingOptionsBytes = pucLast - pucPtr;\r
+ UBaseType_t xRemainingOptionsBytes = pucLast - pucPtr;\r
\r
if( pucPtr[ 0 ] == TCP_OPT_END )\r
{\r
}\r
if( pucPtr[ 0 ] == TCP_OPT_NOOP)\r
{\r
- /* NOP option, inserted to make the length a multiple of 4. */\r
- pucPtr++;\r
- continue;\r
+ /* NOP option, inserted to make the length a multiple of 4. */\r
+ pucPtr++;\r
+ continue;\r
}\r
\r
- /* Any other well-formed option must be at least two bytes: the option\r
- type byte followed by a length byte. */\r
- if( xRemainingOptionsBytes < 2 )\r
- { \r
- break;\r
- }\r
+ /* Any other well-formed option must be at least two bytes: the option\r
+ type byte followed by a length byte. */\r
+ if( xRemainingOptionsBytes < 2 )\r
+ {\r
+ break;\r
+ }\r
#if( ipconfigUSE_TCP_WIN != 0 )\r
else if( pucPtr[ 0 ] == TCP_OPT_WSOPT )\r
{\r
- /* Confirm that the option fits in the remaining buffer space. */\r
- if( xRemainingOptionsBytes < TCP_OPT_WSOPT_LEN ||\r
- pucPtr[ 1 ] != TCP_OPT_WSOPT_LEN )\r
- {\r
- break;\r
- }\r
+ /* Confirm that the option fits in the remaining buffer space. */\r
+ if( ( xRemainingOptionsBytes < TCP_OPT_WSOPT_LEN ) || ( pucPtr[ 1 ] != TCP_OPT_WSOPT_LEN ) )\r
+ {\r
+ break;\r
+ }\r
\r
pxSocket->u.xTCP.ucPeerWinScaleFactor = pucPtr[ 2 ];\r
pxSocket->u.xTCP.bits.bWinScaling = pdTRUE_UNSIGNED;\r
#endif /* ipconfigUSE_TCP_WIN */\r
else if( pucPtr[ 0 ] == TCP_OPT_MSS )\r
{\r
- /* Confirm that the option fits in the remaining buffer space. */\r
- if( xRemainingOptionsBytes < TCP_OPT_MSS_LEN ||\r
- pucPtr[ 1 ] != TCP_OPT_MSS_LEN )\r
- {\r
- break;\r
- }\r
- \r
- /* An MSS option with the correct option length. FreeRTOS_htons()\r
+ /* Confirm that the option fits in the remaining buffer space. */\r
+ if( ( xRemainingOptionsBytes < TCP_OPT_MSS_LEN )|| ( pucPtr[ 1 ] != TCP_OPT_MSS_LEN ) )\r
+ {\r
+ break;\r
+ }\r
+\r
+ /* An MSS option with the correct option length. FreeRTOS_htons()\r
is not needed here because usChar2u16() already returns a host\r
endian number. */\r
uxNewMSS = usChar2u16( pucPtr + 2 );\r
\r
if( pxSocket->u.xTCP.usInitMSS != uxNewMSS )\r
{\r
- /* Perform a basic check on the the new MSS. */\r
- if( uxNewMSS == 0 )\r
- {\r
- break;\r
- }\r
+ /* Perform a basic check on the the new MSS. */\r
+ if( uxNewMSS == 0 )\r
+ {\r
+ break;\r
+ }\r
\r
FreeRTOS_debug_printf( ( "MSS change %u -> %lu\n", pxSocket->u.xTCP.usInitMSS, uxNewMSS ) );\r
}\r
/* All other options have a length field, so that we easily\r
can skip past them. */\r
unsigned char len = pucPtr[ 1 ];\r
- if( len < 2 || len > xRemainingOptionsBytes )\r
+ if( ( len < 2 ) || ( len > xRemainingOptionsBytes ) )\r
{\r
- /* If the length field is too small or too big, the options are malformed. \r
- Don't process them further. */\r
+ /* If the length field is too small or too big, the options are malformed.\r
+ Don't process them further. */\r
break;\r
}\r
\r
pxTCPHeader->ucOptdata[ uxOptionsLength + 3 ] = 2; /* 2: length of this option. */\r
uxOptionsLength += 4u;\r
\r
- return uxOptionsLength; /* bytes, not words. */\r
+ return uxOptionsLength; /* bytes, not words. */\r
}\r
#endif /* ipconfigUSE_TCP_WIN == 0 */\r
}\r
\r
if( pxReturn != NULL )\r
{\r
- /* Set the actual packet size, in case the returned buffer is larger. */\r
- pxReturn->xDataLength = lNeeded;\r
+ /* Set the actual packet size, in case the returned buffer is larger. */\r
+ pxReturn->xDataLength = lNeeded;\r
\r
/* Copy the existing data to the new created buffer. */\r
if( pxNetworkBuffer )\r
The size of the TCP header is given in a multiple of 4-byte words (single\r
byte, needs no ntoh() translation). A shift-right 2: is the same as\r
(offset >> 4) * 4. */\r
- lTCPHeaderLength = ( BaseType_t ) ( ( pxTCPHeader->ucTCPOffset & VALID_BITS_IN_TCP_OFFSET_BYTE ) >> 2 );\r
+ lTCPHeaderLength = ( BaseType_t ) ( ( pxTCPHeader->ucTCPOffset & VALID_BITS_IN_TCP_OFFSET_BYTE ) >> 2 );\r
\r
/* Let pucRecvData point to the first byte received. */\r
*ppucRecvData = pxNetworkBuffer->pucEthernetBuffer + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + lTCPHeaderLength;\r
{\r
/* xTCPWindowTxDone returns true when all Tx queues are empty. */\r
bRxComplete = xTCPWindowRxEmpty( pxTCPWindow );\r
- bTxDone = xTCPWindowTxDone( pxTCPWindow );\r
+ bTxDone = xTCPWindowTxDone( pxTCPWindow );\r
\r
if( ( bRxComplete == 0 ) || ( bTxDone == 0 ) )\r
{\r
uint16_t xRemotePort;\r
BaseType_t xResult = pdPASS;\r
\r
- /* Check for a minimum packet size. */\r
- if( pxNetworkBuffer->xDataLength >= \r
- ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER )\r
- {\r
- ucTCPFlags = pxTCPPacket->xTCPHeader.ucTCPFlags;\r
- ulLocalIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulDestinationIPAddress );\r
- xLocalPort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usDestinationPort );\r
- ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress );\r
- xRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort );\r
-\r
- /* Find the destination socket, and if not found: return a socket listing to\r
- the destination PORT. */\r
- pxSocket = ( FreeRTOS_Socket_t * )pxTCPSocketLookup( ulLocalIP, xLocalPort, ulRemoteIP, xRemotePort );\r
- }\r
- else\r
- {\r
- return pdFAIL;\r
- }\r
+ /* Check for a minimum packet size. */\r
+ if( pxNetworkBuffer->xDataLength >= ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) )\r
+ {\r
+ ucTCPFlags = pxTCPPacket->xTCPHeader.ucTCPFlags;\r
+ ulLocalIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulDestinationIPAddress );\r
+ xLocalPort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usDestinationPort );\r
+ ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress );\r
+ xRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort );\r
+\r
+ /* Find the destination socket, and if not found: return a socket listing to\r
+ the destination PORT. */\r
+ pxSocket = ( FreeRTOS_Socket_t * )pxTCPSocketLookup( ulLocalIP, xLocalPort, ulRemoteIP, xRemotePort );\r
+ }\r
+ else\r
+ {\r
+ return pdFAIL;\r
+ }\r
\r
if( ( pxSocket == NULL ) || ( prvTCPSocketIsActive( ( UBaseType_t ) pxSocket->u.xTCP.ucTCPState ) == pdFALSE ) )\r
{\r
FreeRTOS_Socket_t *pxReturn = NULL;\r
uint32_t ulInitialSequenceNumber;\r
\r
- /* Assume that a new Initial Sequence Number will be required. Request \r
- it now in order to fail out if necessary. */\r
- ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber(\r
- *ipLOCAL_IP_ADDRESS_POINTER,\r
- pxSocket->usLocalPort,\r
- pxTCPPacket->xIPHeader.ulSourceIPAddress,\r
- pxTCPPacket->xTCPHeader.usSourcePort );\r
+ /* Assume that a new Initial Sequence Number will be required. Request\r
+ it now in order to fail out if necessary. */\r
+ ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( *ipLOCAL_IP_ADDRESS_POINTER,\r
+ pxSocket->usLocalPort,\r
+ pxTCPPacket->xIPHeader.ulSourceIPAddress,\r
+ pxTCPPacket->xTCPHeader.usSourcePort );\r
\r
/* A pure SYN (without ACK) has come in, create a new socket to answer\r
it. */\r
- if( 0 != ulInitialSequenceNumber )\r
- {\r
- if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED )\r
- {\r
- /* The flag bReuseSocket indicates that the same instance of the\r
- listening socket should be used for the connection. */\r
- pxReturn = pxSocket;\r
- pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;\r
- pxSocket->u.xTCP.pxPeerSocket = pxSocket;\r
- }\r
- else\r
- {\r
- /* The socket does not have the bReuseSocket flag set meaning create a\r
- new socket when a connection comes in. */\r
- pxReturn = NULL;\r
-\r
- if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog )\r
- {\r
- FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n",\r
- pxSocket->usLocalPort,\r
- pxSocket->u.xTCP.usChildCount,\r
- pxSocket->u.xTCP.usBacklog,\r
- pxSocket->u.xTCP.usChildCount == 1 ? "" : "ren" ) );\r
- prvTCPSendReset( pxNetworkBuffer );\r
- }\r
- else\r
- {\r
- FreeRTOS_Socket_t *pxNewSocket = ( FreeRTOS_Socket_t * )\r
- FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );\r
-\r
- if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) )\r
- {\r
- FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) );\r
- prvTCPSendReset( pxNetworkBuffer );\r
- }\r
- else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE )\r
- {\r
- /* The socket will be connected immediately, no time for the\r
- owner to setsockopt's, therefore copy properties of the server\r
- socket to the new socket. Only the binding might fail (due to\r
- lack of resources). */\r
- pxReturn = pxNewSocket;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if( 0 != ulInitialSequenceNumber && pxReturn != NULL )\r
+ if( 0 != ulInitialSequenceNumber )\r
+ {\r
+ if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED )\r
+ {\r
+ /* The flag bReuseSocket indicates that the same instance of the\r
+ listening socket should be used for the connection. */\r
+ pxReturn = pxSocket;\r
+ pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;\r
+ pxSocket->u.xTCP.pxPeerSocket = pxSocket;\r
+ }\r
+ else\r
+ {\r
+ /* The socket does not have the bReuseSocket flag set meaning create a\r
+ new socket when a connection comes in. */\r
+ pxReturn = NULL;\r
+\r
+ if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog )\r
+ {\r
+ FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n",\r
+ pxSocket->usLocalPort,\r
+ pxSocket->u.xTCP.usChildCount,\r
+ pxSocket->u.xTCP.usBacklog,\r
+ pxSocket->u.xTCP.usChildCount == 1 ? "" : "ren" ) );\r
+ prvTCPSendReset( pxNetworkBuffer );\r
+ }\r
+ else\r
+ {\r
+ FreeRTOS_Socket_t *pxNewSocket = ( FreeRTOS_Socket_t * )\r
+ FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );\r
+\r
+ if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) )\r
+ {\r
+ FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) );\r
+ prvTCPSendReset( pxNetworkBuffer );\r
+ }\r
+ else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE )\r
+ {\r
+ /* The socket will be connected immediately, no time for the\r
+ owner to setsockopt's, therefore copy properties of the server\r
+ socket to the new socket. Only the binding might fail (due to\r
+ lack of resources). */\r
+ pxReturn = pxNewSocket;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ if( ( 0 != ulInitialSequenceNumber ) && ( pxReturn != NULL ) )\r
{\r
pxReturn->u.xTCP.usRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort );\r
pxReturn->u.xTCP.ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress );\r
\r
/* Provide access to private members for testing. */\r
#ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS\r
- #include "aws_freertos_tcp_test_access_tcp_define.h"\r
+ #include "aws_freertos_tcp_test_access_tcp_define.h"\r
#endif\r
+\r
UDPPacket_t *pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;\r
\r
/* Caller must check for minimum packet size. */\r
- pxSocket = pxUDPSocketLookup( usPort );\r
+ pxSocket = pxUDPSocketLookup( usPort );\r
\r
if( pxSocket )\r
{\r
\r
#if( ipconfigUSE_DNS_CACHE != 0 )\r
#ifndef ipconfigDNS_CACHE_NAME_LENGTH\r
- /* Per https://tools.ietf.org/html/rfc1035, 253 is the maximum string length\r
- of a DNS name. The following default accounts for a null terminator. */\r
- #define ipconfigDNS_CACHE_NAME_LENGTH 254\r
+ /* Per https://tools.ietf.org/html/rfc1035, 253 is the maximum string length\r
+ of a DNS name. The following default accounts for a null terminator. */\r
+ #define ipconfigDNS_CACHE_NAME_LENGTH 254\r
#endif\r
\r
#ifndef ipconfigDNS_CACHE_ENTRIES\r
\r
/* Non-activity timeout is expressed in seconds. */\r
#ifndef ipconfigTCP_HANG_PROTECTION_TIME\r
- #define ipconfigTCP_HANG_PROTECTION_TIME 30\r
+ #define ipconfigTCP_HANG_PROTECTION_TIME 30\r
#endif\r
\r
#ifndef ipconfigTCP_IP_SANITY\r
*/\r
/* Keep this as a multiple of 4 */\r
#if( ipconfigUSE_TCP_WIN == 1 )\r
- #define ipSIZE_TCP_OPTIONS 16u\r
+ #define ipSIZE_TCP_OPTIONS 16u\r
#else\r
- #define ipSIZE_TCP_OPTIONS 12u\r
+ #define ipSIZE_TCP_OPTIONS 12u\r
#endif\r
\r
/*\r