]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Labs/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c
Sync FreeRTOS-Labs -CLI -TCP -Trace with the version in FreeRTOS-Plus.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-Plus-TCP / FreeRTOS_DHCP.c
index ea4629b14e269e0b83b5d8eff24dc691730cb5c3..9180426dfa8d94f6cd21e064ded1711d9817dbbd 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
- * FreeRTOS+TCP 191100 experimental\r
- * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\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
  * this software and associated documentation files (the "Software"), to deal in\r
@@ -84,9 +84,9 @@
 \r
 /* Offsets into the transmitted DHCP options fields at which various parameters\r
 are located. */\r
-#define dhcpCLIENT_IDENTIFIER_OFFSET                   ( 5 )\r
-#define dhcpREQUESTED_IP_ADDRESS_OFFSET                        ( 13 )\r
-#define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET              ( 19 )\r
+#define dhcpCLIENT_IDENTIFIER_OFFSET                   ( 6 )\r
+#define dhcpREQUESTED_IP_ADDRESS_OFFSET                        ( 14 )\r
+#define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET              ( 20 )\r
 \r
 /* Values used in the DHCP packets. */\r
 #define dhcpREQUEST_OPCODE                                             ( 1 )\r
@@ -354,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
@@ -589,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
@@ -604,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
@@ -642,8 +643,8 @@ 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
-                               /* Maintain a pointer to the last valid byte (i.e. not the first\r
-                               invalid byte). */\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
@@ -665,7 +666,7 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct
                                        /* Stop if the response is malformed. */\r
                                        if( pucByte < pucLastByte )\r
                                        {\r
-                                               /* There are at least two bytes left. */\r
+                        /* There are at least two bytes left. */\r
                                                ucLength = pucByte[ 1 ];\r
                                                pucByte += 2;\r
 \r
@@ -904,7 +905,7 @@ static const uint8_t ucDHCPRequestOptions[] =
        dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and\r
        dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */\r
        dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST,              /* Message type option. */\r
-       dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0,                 /* Client identifier. */\r
+       dhcpCLIENT_IDENTIFIER_OPTION_CODE, 7, 1, 0, 0, 0, 0, 0, 0,                      /* Client identifier. */\r
        dhcpREQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0,                              /* The IP address being requested. */\r
        dhcpSERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0,                               /* The IP address of the DHCP server. */\r
        dhcpOPTION_END_BYTE\r
@@ -942,7 +943,7 @@ static const uint8_t ucDHCPDiscoverOptions[] =
 {\r
        /* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */\r
        dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER,                                     /* Message type option. */\r
-       dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0,                                         /* Client identifier. */\r
+       dhcpCLIENT_IDENTIFIER_OPTION_CODE, 7, 1, 0, 0, 0, 0, 0, 0,                                              /* Client identifier. */\r
        dhcpPARAMETER_REQUEST_OPTION_CODE, 3, dhcpSUBNET_MASK_OPTION_CODE, dhcpGATEWAY_OPTION_CODE, dhcpDNS_SERVER_OPTIONS_CODE,        /* Parameter request option. */\r
        dhcpOPTION_END_BYTE\r
 };\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