]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c
Update TCP to last release versions in preparation for kernel V10.3.0 release.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / FreeRTOS_DHCP.c
index 62cfd874b56334dd46df10e0fe36d1046af2111c..8871d6563990607693bcf2f52932f197ff4f7cdb 100644 (file)
@@ -1,5 +1,5 @@
 /*\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
@@ -108,11 +108,6 @@ are located. */
 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
@@ -359,9 +354,7 @@ BaseType_t xGivingUp = pdFALSE;
 \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
@@ -594,10 +587,9 @@ static void prvInitialiseDHCP( void )
        /* 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
@@ -609,6 +601,10 @@ static void prvInitialiseDHCP( void )
                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
@@ -647,9 +643,11 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct
                                /* 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
@@ -666,12 +664,13 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct
                                        }\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
@@ -926,7 +925,8 @@ size_t xOptionsLength = sizeof( ucDHCPRequestOptions );
        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
@@ -954,7 +954,8 @@ size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );
        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
@@ -968,13 +969,16 @@ size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );
        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
@@ -995,9 +999,15 @@ size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );
                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