/*\r
- * FreeRTOS+TCP V2.0.11\r
+ * FreeRTOS+TCP V2.2.0\r
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
*\r
* Permission is hereby granted, free of charge, to any person obtaining a copy of\r
located. */\r
#define dhcpFIRST_OPTION_BYTE_OFFSET ( 0xf0 )\r
\r
-/* When walking the variable length options field, the following value is used\r
-to ensure the walk has not gone past the end of the valid options. 2 bytes is\r
-made up of the length byte, and minimum one byte value. */\r
-#define dhcpMAX_OPTION_LENGTH_OF_INTEREST ( 2L )\r
-\r
/* Standard DHCP port numbers and magic cookie value. */\r
#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )\r
#define dhcpCLIENT_PORT 0x4400u\r
\r
if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )\r
{\r
- xDHCPData.ulTransactionId = ipconfigRAND32( );\r
-\r
- if( 0 != xDHCPData.ulTransactionId )\r
+ if( xApplicationGetRandomNumber( &( xDHCPData.ulTransactionId ) ) != pdFALSE )\r
{\r
xDHCPData.xDHCPTxTime = xTaskGetTickCount( );\r
xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast;\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
+ if( xApplicationGetRandomNumber( &( xDHCPData.ulTransactionId ) ) != pdFALSE )\r
{\r
xDHCPData.xUseBroadcast = 0;\r
xDHCPData.ulOfferedIPAddress = 0UL;\r
FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) );\r
vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );\r
}\r
+ else\r
+ {\r
+ /* There was a problem with the randomiser. */\r
+ }\r
}\r
/*-----------------------------------------------------------*/\r
\r
/* Walk through the options until the dhcpOPTION_END_BYTE byte\r
is found, taking care not to walk off the end of the options. */\r
pucByte = &( pxDHCPMessage->ucFirstOptionByte );\r
- pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );\r
+ /* Maintain a pointer to the last valid byte (i.e. not the first\r
+ invalid byte). */\r
+ pucLastByte = pucUDPPayload + lBytes - 1;\r
\r
- while( pucByte < pucLastByte )\r
+ while( pucByte <= pucLastByte )\r
{\r
ucOptionCode = pucByte[ 0 ];\r
if( ucOptionCode == dhcpOPTION_END_BYTE )\r
}\r
\r
/* Stop if the response is malformed. */\r
- if( pucByte < pucLastByte - 1 )\r
+ if( pucByte < pucLastByte )\r
{\r
+ /* There are at least two bytes left. */\r
ucLength = pucByte[ 1 ];\r
pucByte += 2;\r
\r
- if( pucByte >= pucLastByte - ucLength )\r
+ if( pucByte + ucLength > pucLastByte )\r
{\r
break;\r
}\r
FreeRTOS_debug_printf( ( "vDHCPProcess: reply %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );\r
iptraceSENDING_DHCP_REQUEST();\r
\r
- if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
+ /* 'ucFirstOptionByte' is part of DHCP message struct, so subtract one byte. */\r
+ if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength - 1 ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
{\r
/* The packet was not successfully queued for sending and must be\r
returned to the stack. */\r
FreeRTOS_debug_printf( ( "vDHCPProcess: discover\n" ) );\r
iptraceSENDING_DHCP_DISCOVER();\r
\r
- if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
+ /* 'ucFirstOptionByte' is part of DHCP message struct, so subtract one byte. */\r
+ if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength - 1 ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
{\r
/* The packet was not successfully queued for sending and must be\r
returned to the stack. */\r
static void prvPrepareLinkLayerIPLookUp( void )\r
{\r
uint8_t ucLinkLayerIPAddress[ 2 ];\r
+ uint32_t ulNumbers[ 2 ];\r
\r
/* After DHCP has failed to answer, prepare everything to start\r
trying-out LinkLayer IP-addresses, using the random method. */\r
xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
\r
- ucLinkLayerIPAddress[ 0 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32() % 0xFDu ); /* get value 1..254 for IP-address 3rd byte of IP address to try. */\r
- ucLinkLayerIPAddress[ 1 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32() % 0xFDu ); /* get value 1..254 for IP-address 4th byte of IP address to try. */\r
+ xApplicationGetRandomNumber( &( ulNumbers[ 0 ] ) );\r
+ xApplicationGetRandomNumber( &( ulNumbers[ 1 ] ) );\r
+ ucLinkLayerIPAddress[ 0 ] = ( uint8_t )1 + ( uint8_t )( ulNumbers[ 0 ] % 0xFDu ); /* get value 1..254 for IP-address 3rd byte of IP address to try. */\r
+ ucLinkLayerIPAddress[ 1 ] = ( uint8_t )1 + ( uint8_t )( ulNumbers[ 1 ] % 0xFDu ); /* get value 1..254 for IP-address 4th byte of IP address to try. */\r
\r
xNetworkAddressing.ulGatewayAddress = FreeRTOS_htonl( 0xA9FE0203 );\r
\r
xNetworkAddressing.ulBroadcastAddress = ( xDHCPData.ulOfferedIPAddress & xNetworkAddressing.ulNetMask ) | ~xNetworkAddressing.ulNetMask;\r
\r
/* Close socket to ensure packets don't queue on it. not needed anymore as DHCP failed. but still need timer for ARP testing. */\r
- vSocketClose( xDHCPData.xDHCPSocket );\r
- xDHCPData.xDHCPSocket = NULL;\r
- xDHCPData.xDHCPTxPeriod = pdMS_TO_TICKS( 3000ul + ( ipconfigRAND32() & 0x3fful ) ); /* do ARP test every (3 + 0-1024mS) seconds. */\r
+ if( xDHCPData.xDHCPSocket != NULL )\r
+ {\r
+ /* Close socket to ensure packets don't queue on it. */\r
+ vSocketClose( xDHCPData.xDHCPSocket );\r
+ xDHCPData.xDHCPSocket = NULL;\r
+ }\r
+\r
+ xApplicationGetRandomNumber( &( ulNumbers[ 0 ] ) );\r
+ xDHCPData.xDHCPTxPeriod = pdMS_TO_TICKS( 3000ul + ( ulNumbers[ 0 ] & 0x3ffuL ) ); /* do ARP test every (3 + 0-1024mS) seconds. */\r
\r
xARPHadIPClash = pdFALSE; /* reset flag that shows if have ARP clash. */\r
vARPSendGratuitous();\r