1
1
/*
2
- * FreeRTOS+TCP 191100 experimental
3
- * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ * FreeRTOS+TCP V2.2.0
3
+ * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
4
*
5
5
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
* this software and associated documentation files (the "Software"), to deal in
84
84
85
85
/* Offsets into the transmitted DHCP options fields at which various parameters
86
86
are located. */
87
- #define dhcpCLIENT_IDENTIFIER_OFFSET ( 5 )
88
- #define dhcpREQUESTED_IP_ADDRESS_OFFSET ( 13 )
89
- #define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ( 19 )
87
+ #define dhcpCLIENT_IDENTIFIER_OFFSET ( 6 )
88
+ #define dhcpREQUESTED_IP_ADDRESS_OFFSET ( 14 )
89
+ #define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ( 20 )
90
90
91
91
/* Values used in the DHCP packets. */
92
92
#define dhcpREQUEST_OPCODE ( 1 )
@@ -354,9 +354,7 @@ BaseType_t xGivingUp = pdFALSE;
354
354
355
355
if ( xDHCPData .xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
356
356
{
357
- xDHCPData .ulTransactionId = ipconfigRAND32 ( );
358
-
359
- if ( 0 != xDHCPData .ulTransactionId )
357
+ if ( xApplicationGetRandomNumber ( & ( xDHCPData .ulTransactionId ) ) != pdFALSE )
360
358
{
361
359
xDHCPData .xDHCPTxTime = xTaskGetTickCount ( );
362
360
xDHCPData .xUseBroadcast = !xDHCPData .xUseBroadcast ;
@@ -589,10 +587,9 @@ static void prvInitialiseDHCP( void )
589
587
/* Initialise the parameters that will be set by the DHCP process. Per
590
588
https://www.ietf.org/rfc/rfc2131.txt, Transaction ID should be a random
591
589
value chosen by the client. */
592
- xDHCPData .ulTransactionId = ipconfigRAND32 ();
593
590
594
591
/* Check for random number generator API failure. */
595
- if ( 0 != xDHCPData .ulTransactionId )
592
+ if ( xApplicationGetRandomNumber ( & ( xDHCPData .ulTransactionId ) ) != pdFALSE )
596
593
{
597
594
xDHCPData .xUseBroadcast = 0 ;
598
595
xDHCPData .ulOfferedIPAddress = 0UL ;
@@ -604,6 +601,10 @@ static void prvInitialiseDHCP( void )
604
601
FreeRTOS_debug_printf ( ( "prvInitialiseDHCP: start after %lu ticks\n" , dhcpINITIAL_TIMER_PERIOD ) );
605
602
vIPReloadDHCPTimer ( dhcpINITIAL_TIMER_PERIOD );
606
603
}
604
+ else
605
+ {
606
+ /* There was a problem with the randomiser. */
607
+ }
607
608
}
608
609
/*-----------------------------------------------------------*/
609
610
@@ -642,8 +643,8 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct
642
643
/* Walk through the options until the dhcpOPTION_END_BYTE byte
643
644
is found, taking care not to walk off the end of the options. */
644
645
pucByte = & ( pxDHCPMessage -> ucFirstOptionByte );
645
- /* Maintain a pointer to the last valid byte (i.e. not the first
646
- invalid byte). */
646
+ /* Maintain a pointer to the last valid byte (i.e. not the first
647
+ invalid byte). */
647
648
pucLastByte = pucUDPPayload + lBytes - 1 ;
648
649
649
650
while ( pucByte <= pucLastByte )
@@ -665,7 +666,7 @@ const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct
665
666
/* Stop if the response is malformed. */
666
667
if ( pucByte < pucLastByte )
667
668
{
668
- /* There are at least two bytes left. */
669
+ /* There are at least two bytes left. */
669
670
ucLength = pucByte [ 1 ];
670
671
pucByte += 2 ;
671
672
@@ -904,7 +905,7 @@ static const uint8_t ucDHCPRequestOptions[] =
904
905
dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and
905
906
dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */
906
907
dhcpMESSAGE_TYPE_OPTION_CODE , 1 , dhcpMESSAGE_TYPE_REQUEST , /* Message type option. */
907
- dhcpCLIENT_IDENTIFIER_OPTION_CODE , 6 , 0 , 0 , 0 , 0 , 0 , 0 , /* Client identifier. */
908
+ dhcpCLIENT_IDENTIFIER_OPTION_CODE , 7 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , /* Client identifier. */
908
909
dhcpREQUEST_IP_ADDRESS_OPTION_CODE , 4 , 0 , 0 , 0 , 0 , /* The IP address being requested. */
909
910
dhcpSERVER_IP_ADDRESS_OPTION_CODE , 4 , 0 , 0 , 0 , 0 , /* The IP address of the DHCP server. */
910
911
dhcpOPTION_END_BYTE
@@ -942,7 +943,7 @@ static const uint8_t ucDHCPDiscoverOptions[] =
942
943
{
943
944
/* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */
944
945
dhcpMESSAGE_TYPE_OPTION_CODE , 1 , dhcpMESSAGE_TYPE_DISCOVER , /* Message type option. */
945
- dhcpCLIENT_IDENTIFIER_OPTION_CODE , 6 , 0 , 0 , 0 , 0 , 0 , 0 , /* Client identifier. */
946
+ dhcpCLIENT_IDENTIFIER_OPTION_CODE , 7 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , /* Client identifier. */
946
947
dhcpPARAMETER_REQUEST_OPTION_CODE , 3 , dhcpSUBNET_MASK_OPTION_CODE , dhcpGATEWAY_OPTION_CODE , dhcpDNS_SERVER_OPTIONS_CODE , /* Parameter request option. */
947
948
dhcpOPTION_END_BYTE
948
949
};
@@ -968,13 +969,16 @@ size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );
968
969
static void prvPrepareLinkLayerIPLookUp ( void )
969
970
{
970
971
uint8_t ucLinkLayerIPAddress [ 2 ];
972
+ uint32_t ulNumbers [ 2 ];
971
973
972
974
/* After DHCP has failed to answer, prepare everything to start
973
975
trying-out LinkLayer IP-addresses, using the random method. */
974
976
xDHCPData .xDHCPTxTime = xTaskGetTickCount ();
975
977
976
- ucLinkLayerIPAddress [ 0 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32 () % 0xFDu ); /* get value 1..254 for IP-address 3rd byte of IP address to try. */
977
- ucLinkLayerIPAddress [ 1 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32 () % 0xFDu ); /* get value 1..254 for IP-address 4th byte of IP address to try. */
978
+ xApplicationGetRandomNumber ( & ( ulNumbers [ 0 ] ) );
979
+ xApplicationGetRandomNumber ( & ( ulNumbers [ 1 ] ) );
980
+ ucLinkLayerIPAddress [ 0 ] = ( uint8_t )1 + ( uint8_t )( ulNumbers [ 0 ] % 0xFDu ); /* get value 1..254 for IP-address 3rd byte of IP address to try. */
981
+ ucLinkLayerIPAddress [ 1 ] = ( uint8_t )1 + ( uint8_t )( ulNumbers [ 1 ] % 0xFDu ); /* get value 1..254 for IP-address 4th byte of IP address to try. */
978
982
979
983
xNetworkAddressing .ulGatewayAddress = FreeRTOS_htonl ( 0xA9FE0203 );
980
984
@@ -995,9 +999,15 @@ size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );
995
999
xNetworkAddressing .ulBroadcastAddress = ( xDHCPData .ulOfferedIPAddress & xNetworkAddressing .ulNetMask ) | ~xNetworkAddressing .ulNetMask ;
996
1000
997
1001
/* Close socket to ensure packets don't queue on it. not needed anymore as DHCP failed. but still need timer for ARP testing. */
998
- vSocketClose ( xDHCPData .xDHCPSocket );
999
- xDHCPData .xDHCPSocket = NULL ;
1000
- xDHCPData .xDHCPTxPeriod = pdMS_TO_TICKS ( 3000ul + ( ipconfigRAND32 () & 0x3fful ) ); /* do ARP test every (3 + 0-1024mS) seconds. */
1002
+ if ( xDHCPData .xDHCPSocket != NULL )
1003
+ {
1004
+ /* Close socket to ensure packets don't queue on it. */
1005
+ vSocketClose ( xDHCPData .xDHCPSocket );
1006
+ xDHCPData .xDHCPSocket = NULL ;
1007
+ }
1008
+
1009
+ xApplicationGetRandomNumber ( & ( ulNumbers [ 0 ] ) );
1010
+ xDHCPData .xDHCPTxPeriod = pdMS_TO_TICKS ( 3000ul + ( ulNumbers [ 0 ] & 0x3ffuL ) ); /* do ARP test every (3 + 0-1024mS) seconds. */
1001
1011
1002
1012
xARPHadIPClash = pdFALSE ; /* reset flag that shows if have ARP clash. */
1003
1013
vARPSendGratuitous ();
0 commit comments