Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • add_menu_vibration
  • blinkisync-as-preload
  • ch3/api-speed-eval2
  • ch3/dual-core
  • ch3/genapi-refactor
  • ch3/leds-api
  • ch3/splashscreen
  • dualcore
  • dx/flatten-config-module
  • dx/meh-bdf-to-stm
  • freertos-btle
  • genofire/ble-follow-py
  • koalo/bhi160-works-but-dirty
  • koalo/factory-reset
  • koalo/wip/i2c-for-python
  • master
  • msgctl/faultscreen
  • msgctl/textbuffer_api
  • plaetzchen/ios-workaround
  • rahix/bhi
  • rahix/bluetooth-app-favorite
  • rahix/bma
  • rahix/user-space-ctx
  • renze/hatchery_apps
  • renze/safe_mode
  • schleicher-test
  • schneider/212-reset-hardware-when-entering-repl
  • schneider/ancs
  • schneider/ble-buffers
  • schneider/ble-central
  • schneider/ble-ecg-stream-visu
  • schneider/ble-fixes-2020-3
  • schneider/ble-mini-demo
  • schneider/ble-stability
  • schneider/ble-stability-new-phy
  • schneider/bonding
  • schneider/bonding-fail-if-full
  • schneider/bootloader-update-9a0d158
  • schneider/deepsleep
  • schneider/deepsleep2
  • schneider/deepsleep4
  • schneider/default-main
  • schneider/freertos-list-debug
  • schneider/fundamental-test
  • schneider/iaq-python
  • schneider/ir
  • schneider/max30001
  • schneider/max30001-epicaridum
  • schneider/max30001-pycardium
  • schneider/maxim-sdk-update
  • schneider/mp-exception-print
  • schneider/mp-for-old-bl
  • schneider/png
  • schneider/schleicher-test
  • schneider/sdk-0.2.1-11
  • schneider/sdk-0.2.1-7
  • schneider/sleep-display
  • schneider/spo2-playground
  • schneider/stream-locks
  • schneider/v1.17-changelog
  • bootloader-v1
  • release-1
  • v0.0
  • v1.0
  • v1.1
  • v1.10
  • v1.11
  • v1.12
  • v1.13
  • v1.14
  • v1.15
  • v1.16
  • v1.17
  • v1.18
  • v1.2
  • v1.3
  • v1.4
  • v1.5
  • v1.6
  • v1.7
  • v1.8
  • v1.9
82 results

Target

Select target project
  • card10/firmware
  • annejan/firmware
  • astro/firmware
  • fpletz/firmware
  • gerd/firmware
  • fleur/firmware
  • swym/firmware
  • l/firmware
  • uberardy/firmware
  • wink/firmware
  • madonius/firmware
  • mot/firmware
  • filid/firmware
  • q3k/firmware
  • hauke/firmware
  • Woazboat/firmware
  • pink/firmware
  • mossmann/firmware
  • omniskop/firmware
  • zenox/firmware
  • trilader/firmware
  • Danukeru/firmware
  • shoragan/firmware
  • zlatko/firmware
  • sistason/firmware
  • datenwolf/firmware
  • bene/firmware
  • amedee/firmware
  • martinling/firmware
  • griffon/firmware
  • chris007/firmware
  • adisbladis/firmware
  • dbrgn/firmware
  • jelly/firmware
  • rnestler/firmware
  • mh/firmware
  • ln/firmware
  • penguineer/firmware
  • monkeydom/firmware
  • jens/firmware
  • jnaulty/firmware
  • jeffmakes/firmware
  • marekventur/firmware
  • pete/firmware
  • h2obrain/firmware
  • DooMMasteR/firmware
  • jackie/firmware
  • prof_r/firmware
  • Draradech/firmware
  • Kartoffel/firmware
  • hinerk/firmware
  • abbradar/firmware
  • JustTB/firmware
  • LuKaRo/firmware
  • iggy/firmware
  • ente/firmware
  • flgr/firmware
  • Lorphos/firmware
  • matejo/firmware
  • ceddral7/firmware
  • danb/firmware
  • joshi/firmware
  • melle/firmware
  • fitch/firmware
  • deurknop/firmware
  • sargon/firmware
  • markus/firmware
  • kloenk/firmware
  • lucaswerkmeister/firmware
  • derf/firmware
  • meh/firmware
  • dx/card10-firmware
  • torben/firmware
  • yuvadm/firmware
  • AndyBS/firmware
  • klausdieter1/firmware
  • katzenparadoxon/firmware
  • xiretza/firmware
  • ole/firmware
  • techy/firmware
  • thor77/firmware
  • TilCreator/firmware
  • fuchsi/firmware
  • dos/firmware
  • yrlf/firmware
  • PetePriority/firmware
  • SuperVirus/firmware
  • sur5r/firmware
  • tazz/firmware
  • Alienmaster/firmware
  • flo_h/firmware
  • baldo/firmware
  • mmu_man/firmware
  • Foaly/firmware
  • sodoku/firmware
  • Guinness/firmware
  • ssp/firmware
  • led02/firmware
  • Stormwind/firmware
  • arist/firmware
  • coon/firmware
  • mdik/firmware
  • pippin/firmware
  • royrobotiks/firmware
  • zigot83/firmware
  • mo_k/firmware
106 results
Select Git revision
  • add_menu_vibration
  • blinkisync-as-preload
  • ch3/api-speed-eval2
  • ch3/dual-core
  • ch3/genapi-refactor
  • ch3/leds-api
  • ch3/splashscreen
  • dualcore
  • dx/flatten-config-module
  • dx/meh-bdf-to-stm
  • dx/somewhat-more-dynamic-config
  • esp32-nimble-wiki
  • freertos-btle
  • genofire/ble-follow-py
  • koalo/bhi160-works-but-dirty
  • koalo/factory-reset
  • koalo/wip/i2c-for-python
  • master
  • msgctl/faultscreen
  • msgctl/textbuffer_api
  • plaetzchen/ios-workaround
  • rahix/bhi
  • rahix/bma
  • rahix/hw-lock-new-mutex
  • renze/hatchery_apps
  • renze/safe_mode
  • schleicher-test
  • schneider/ble-buffers
  • schneider/ble-stability
  • schneider/ble-stability-new-phy
  • schneider/bonding
  • schneider/bootloader-update-9a0d158
  • schneider/bsec
  • schneider/fundamental-test
  • schneider/max30001
  • schneider/max30001-epicaridum
  • schneider/max30001-pycardium
  • schneider/maxim-sdk-update
  • schneider/mp-for-old-bl
  • schneider/schleicher-test
  • schneider/sdk-0.2.1-7
  • schneider/stream-locks
  • bootloader-v1
  • release-1
  • v0.0
  • v1.0
  • v1.1
  • v1.10
  • v1.11
  • v1.12
  • v1.2
  • v1.3
  • v1.4
  • v1.5
  • v1.6
  • v1.7
  • v1.8
  • v1.9
58 results
Show changes
Showing
with 13095 additions and 0 deletions
l0dable_startup_lib = static_library(
'l0dable-startup',
'crt.s',
'hardware.c',
dependencies: [api_caller],
c_args: ['-fpie'],
)
l0dable_startup = declare_dependency(
link_args: [
'-nostdlib', '-n',
'-T', meson.current_source_dir() + 'l0dable.ld',
],
compile_args: [
'-fPIE', '-pie',
],
)
subdir('lib/')
subdir('blinky/')
/*
* FreeRTOS+CLI V1.0.4
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
/* Standard includes. */
#include <string.h>
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
/* Utils includes. */
#include "FreeRTOS_CLI.h"
/* If the application writer needs to place the buffer used by the CLI at a
fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in
FreeRTOSConfig.h, then declare an array with the following name and size in
one of the application files:
char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
*/
#ifndef configAPPLICATION_PROVIDES_cOutputBuffer
#define configAPPLICATION_PROVIDES_cOutputBuffer 0
#endif
typedef struct xCOMMAND_INPUT_LIST
{
const CLI_Command_Definition_t *pxCommandLineDefinition;
struct xCOMMAND_INPUT_LIST *pxNext;
} CLI_Definition_List_Item_t;
/*
* The callback function that is executed when "help" is entered. This is the
* only default command that is always present.
*/
static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
/*
* Return the number of parameters that follow the command name.
*/
static int8_t prvGetNumberOfParameters( const char *pcCommandString );
/* The definition of the "help" command. This command is always at the front
of the list of registered commands. */
static const CLI_Command_Definition_t xHelpCommand =
{
"help",
"\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
prvHelpCommand,
0
};
/* The definition of the list of commands. Commands that are registered are
added to this list. */
static CLI_Definition_List_Item_t xRegisteredCommands =
{
&xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
};
/* A buffer into which command outputs can be written is declared here, rather
than in the command console implementation, to allow multiple command consoles
to share the same buffer. For example, an application may allow access to the
command interpreter by UART and by Ethernet. Sharing a buffer is done purely
to save RAM. Note, however, that the command console itself is not re-entrant,
so only one command interpreter interface can be used at any one time. For that
reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
attempted.
configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application
writer to provide their own cOutputBuffer declaration in cases where the
buffer needs to be placed at a fixed address (rather than by the linker). */
#if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )
static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
#else
extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
#endif
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
{
static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
CLI_Definition_List_Item_t *pxNewListItem;
BaseType_t xReturn = pdFAIL;
/* Check the parameter is not NULL. */
configASSERT( pxCommandToRegister );
/* Create a new list item that will reference the command being registered. */
pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
configASSERT( pxNewListItem );
if( pxNewListItem != NULL )
{
taskENTER_CRITICAL();
{
/* Reference the command being registered from the newly created
list item. */
pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
/* The new list item will get added to the end of the list, so
pxNext has nowhere to point. */
pxNewListItem->pxNext = NULL;
/* Add the newly created list item to the end of the already existing
list. */
pxLastCommandInList->pxNext = pxNewListItem;
/* Set the end of list marker to the new list item. */
pxLastCommandInList = pxNewListItem;
}
taskEXIT_CRITICAL();
xReturn = pdPASS;
}
return xReturn;
}
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen )
{
static const CLI_Definition_List_Item_t *pxCommand = NULL;
BaseType_t xReturn = pdTRUE;
const char *pcRegisteredCommandString;
size_t xCommandStringLength;
/* Note: This function is not re-entrant. It must not be called from more
thank one task. */
if( pxCommand == NULL )
{
/* Search for the command string in the list of registered commands. */
for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
{
pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
xCommandStringLength = strlen( pcRegisteredCommandString );
/* To ensure the string lengths match exactly, so as not to pick up
a sub-string of a longer command, check the byte after the expected
end of the string is either the end of the string or a space before
a parameter. */
if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
{
if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )
{
/* The command has been found. Check it has the expected
number of parameters. If cExpectedNumberOfParameters is -1,
then there could be a variable number of parameters and no
check is made. */
if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
{
if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
{
xReturn = pdFALSE;
}
}
break;
}
}
}
}
if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
{
/* The command was found, but the number of parameters with the command
was incorrect. */
strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
pxCommand = NULL;
}
else if( pxCommand != NULL )
{
/* Call the callback function that is registered to this command. */
xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
/* If xReturn is pdFALSE, then no further strings will be returned
after this one, and pxCommand can be reset to NULL ready to search
for the next entered command. */
if( xReturn == pdFALSE )
{
pxCommand = NULL;
}
}
else
{
/* pxCommand was NULL, the command was not found. */
strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
xReturn = pdFALSE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
char *FreeRTOS_CLIGetOutputBuffer( void )
{
return cOutputBuffer;
}
/*-----------------------------------------------------------*/
const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )
{
UBaseType_t uxParametersFound = 0;
const char *pcReturn = NULL;
*pxParameterStringLength = 0;
while( uxParametersFound < uxWantedParameter )
{
/* Index the character pointer past the current word. If this is the start
of the command string then the first word is the command itself. */
while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
{
pcCommandString++;
}
/* Find the start of the next string. */
while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
{
pcCommandString++;
}
/* Was a string found? */
if( *pcCommandString != 0x00 )
{
/* Is this the start of the required parameter? */
uxParametersFound++;
if( uxParametersFound == uxWantedParameter )
{
/* How long is the parameter? */
pcReturn = pcCommandString;
while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
{
( *pxParameterStringLength )++;
pcCommandString++;
}
if( *pxParameterStringLength == 0 )
{
pcReturn = NULL;
}
break;
}
}
else
{
break;
}
}
return pcReturn;
}
/*-----------------------------------------------------------*/
static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
static const CLI_Definition_List_Item_t * pxCommand = NULL;
BaseType_t xReturn;
( void ) pcCommandString;
if( pxCommand == NULL )
{
/* Reset the pxCommand pointer back to the start of the list. */
pxCommand = &xRegisteredCommands;
}
/* Return the next command help string, before moving the pointer on to
the next command in the list. */
strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
pxCommand = pxCommand->pxNext;
if( pxCommand == NULL )
{
/* There are no more commands in the list, so there will be no more
strings to return after this one and pdFALSE should be returned. */
xReturn = pdFALSE;
}
else
{
xReturn = pdTRUE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
static int8_t prvGetNumberOfParameters( const char *pcCommandString )
{
int8_t cParameters = 0;
BaseType_t xLastCharacterWasSpace = pdFALSE;
/* Count the number of space delimited words in pcCommandString. */
while( *pcCommandString != 0x00 )
{
if( ( *pcCommandString ) == ' ' )
{
if( xLastCharacterWasSpace != pdTRUE )
{
cParameters++;
xLastCharacterWasSpace = pdTRUE;
}
}
else
{
xLastCharacterWasSpace = pdFALSE;
}
pcCommandString++;
}
/* If the command string ended with spaces, then there will have been too
many parameters counted. */
if( xLastCharacterWasSpace == pdTRUE )
{
cParameters--;
}
/* The value returned is one less than the number of space delimited words,
as the first word should be the command itself. */
return cParameters;
}
/*
* FreeRTOS+CLI V1.0.4
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#ifndef COMMAND_INTERPRETER_H
#define COMMAND_INTERPRETER_H
/* The prototype to which callback functions used to process command line
commands must comply. pcWriteBuffer is a buffer into which the output from
executing the command can be written, xWriteBufferLen is the length, in bytes of
the pcWriteBuffer buffer, and pcCommandString is the entire string as input by
the user (from which parameters can be extracted).*/
typedef BaseType_t (*pdCOMMAND_LINE_CALLBACK)( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
/* The structure that defines command line commands. A command line command
should be defined by declaring a const structure of this type. */
typedef struct xCOMMAND_LINE_INPUT
{
const char * const pcCommand; /* The command that causes pxCommandInterpreter to be executed. For example "help". Must be all lower case. */
const char * const pcHelpString; /* String that describes how to use the command. Should start with the command itself, and end with "\r\n". For example "help: Returns a list of all the commands\r\n". */
const pdCOMMAND_LINE_CALLBACK pxCommandInterpreter; /* A pointer to the callback function that will return the output generated by the command. */
int8_t cExpectedNumberOfParameters; /* Commands expect a fixed number of parameters, which may be zero. */
} CLI_Command_Definition_t;
/* For backward compatibility. */
#define xCommandLineInput CLI_Command_Definition_t
/*
* Register the command passed in using the pxCommandToRegister parameter.
* Registering a command adds the command to the list of commands that are
* handled by the command interpreter. Once a command has been registered it
* can be executed from the command line.
*/
BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister );
/*
* Runs the command interpreter for the command string "pcCommandInput". Any
* output generated by running the command will be placed into pcWriteBuffer.
* xWriteBufferLen must indicate the size, in bytes, of the buffer pointed to
* by pcWriteBuffer.
*
* FreeRTOS_CLIProcessCommand should be called repeatedly until it returns pdFALSE.
*
* pcCmdIntProcessCommand is not reentrant. It must not be called from more
* than one task - or at least - by more than one task at a time.
*/
BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen );
/*-----------------------------------------------------------*/
/*
* A buffer into which command outputs can be written is declared in the
* main command interpreter, rather than in the command console implementation,
* to allow application that provide access to the command console via multiple
* interfaces to share a buffer, and therefore save RAM. Note, however, that
* the command interpreter itself is not re-entrant, so only one command
* console interface can be used at any one time. For that reason, no attempt
* is made to provide any mutual exclusion mechanism on the output buffer.
*
* FreeRTOS_CLIGetOutputBuffer() returns the address of the output buffer.
*/
char *FreeRTOS_CLIGetOutputBuffer( void );
/*
* Return a pointer to the xParameterNumber'th word in pcCommandString.
*/
const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength );
#endif /* COMMAND_INTERPRETER_H */
Changes between V1.0.3 and V1.0.4 released
+ Update to use stdint and the FreeRTOS specific typedefs that were
introduced in FreeRTOS V8.0.0.
Changes between V1.0.2 and V1.0.3 released
+ Previously, and in line with good software engineering practice, the
FreeRTOS coding standard did not permit the use of char types that were
not explicitly qualified as either signed or unsigned. As a result char
pointers used to reference strings required casts, as did the use of any
standard string handling functions. The casts ensured compiler warnings
were not generated by compilers that defaulted unqualified char types to
be signed or compilers that defaulted unqualified char types to be
unsigned. As it has in later MISRA standards, this rule has now been
relaxed, and unqualified char types are now permitted, but only when:
1) The char is used to point to a human readable text string.
2) The char is used to hold a single ASCII character.
Changes between V1.0.1 and V1.0.2 released 14/10/2013
+ Changed double quotes (") to single quotes (') in the help string to
allow the strings to be used with JSON in FreeRTOS+Nabto.
Changes between V1.0.0 and V1.0.1 released 05/07/2012
+ Change the name of the structure used to map a function that implements
a CLI command to the string used to call the command from
xCommandLineInput to CLI_Command_Definition_t, as it was always intended
to be. A #define was added to map the old name to the new name for
reasons of backward compatibility.
FreeRTOS+CLI is released under the following MIT license.
Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[InternetShortcut]
URL=http://www.freertos.org/cli
IDList=
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
Contains source and header files that implement FreeRTOS+CLI. See
http://www.FreeRTOS.org/cli for documentation and license information.
\ No newline at end of file
[InternetShortcut]
URL=http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_IO/Demo_Applications/LPCXpresso_LPC1769/NXP_LPC1769_Demo_Description.shtml
IDList=
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
It is not possible to create an example FreeRTOS+IO project for the Windows
simulator. FreeRTOS+IO information can be found on http://www.FreeRTOS.org/IO.
A featured demo that includes telnet like functionality, a web server,
a command line interface (using FreeRTOS+CLI) and a FAT file system is
described on
http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_IO/Demo_Applications/LPCXpresso_LPC1769/NXP_LPC1769_Demo_Description.shtml
\ No newline at end of file
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_ARP.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_DHCP.h"
#if( ipconfigUSE_LLMNR == 1 )
#include "FreeRTOS_DNS.h"
#endif /* ipconfigUSE_LLMNR */
#include "NetworkInterface.h"
#include "NetworkBufferManagement.h"
/* When the age of an entry in the ARP table reaches this value (it counts down
to zero, so this is an old entry) an ARP request will be sent to see if the
entry is still valid and can therefore be refreshed. */
#define arpMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST ( 3 )
/* The time between gratuitous ARPs. */
#ifndef arpGRATUITOUS_ARP_PERIOD
#define arpGRATUITOUS_ARP_PERIOD ( pdMS_TO_TICKS( 20000 ) )
#endif
/*-----------------------------------------------------------*/
/*
* Lookup an MAC address in the ARP cache from the IP address.
*/
static eARPLookupResult_t prvCacheLookup( uint32_t ulAddressToLookup, MACAddress_t * const pxMACAddress );
/*-----------------------------------------------------------*/
/* The ARP cache. */
static ARPCacheRow_t xARPCache[ ipconfigARP_CACHE_ENTRIES ];
/* The time at which the last gratuitous ARP was sent. Gratuitous ARPs are used
to ensure ARP tables are up to date and to detect IP address conflicts. */
static TickType_t xLastGratuitousARPTime = ( TickType_t ) 0;
/*
* IP-clash detection is currently only used internally. When DHCP doesn't respond, the
* driver can try out a random LinkLayer IP address (169.254.x.x). It will send out a
* gratuitos ARP message and, after a period of time, check the variables here below:
*/
#if( ipconfigARP_USE_CLASH_DETECTION != 0 )
/* Becomes non-zero if another device responded to a gratuitos ARP message. */
BaseType_t xARPHadIPClash;
/* MAC-address of the other device containing the same IP-address. */
MACAddress_t xARPClashMacAddress;
#endif /* ipconfigARP_USE_CLASH_DETECTION */
/* Part of the Ethernet and ARP headers are always constant when sending an IPv4
ARP packet. This array defines the constant parts, allowing this part of the
packet to be filled in using a simple memcpy() instead of individual writes. */
static const uint8_t xDefaultPartARPPacketHeader[] =
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* Ethernet destination address. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Ethernet source address. */
0x08, 0x06, /* Ethernet frame type (ipARP_FRAME_TYPE). */
0x00, 0x01, /* usHardwareType (ipARP_HARDWARE_TYPE_ETHERNET). */
0x08, 0x00, /* usProtocolType. */
ipMAC_ADDRESS_LENGTH_BYTES, /* ucHardwareAddressLength. */
ipIP_ADDRESS_LENGTH_BYTES, /* ucProtocolAddressLength. */
0x00, 0x01, /* usOperation (ipARP_REQUEST). */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* xSenderHardwareAddress. */
0x00, 0x00, 0x00, 0x00, /* ulSenderProtocolAddress. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* xTargetHardwareAddress. */
};
/*-----------------------------------------------------------*/
eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame )
{
eFrameProcessingResult_t eReturn = eReleaseBuffer;
ARPHeader_t *pxARPHeader;
uint32_t ulTargetProtocolAddress, ulSenderProtocolAddress;
pxARPHeader = &( pxARPFrame->xARPHeader );
/* The field ulSenderProtocolAddress is badly aligned, copy byte-by-byte. */
memcpy( ( void *)&( ulSenderProtocolAddress ), ( void * )pxARPHeader->ucSenderProtocolAddress, sizeof( ulSenderProtocolAddress ) );
/* The field ulTargetProtocolAddress is well-aligned, a 32-bits copy. */
ulTargetProtocolAddress = pxARPHeader->ulTargetProtocolAddress;
traceARP_PACKET_RECEIVED();
/* Don't do anything if the local IP address is zero because
that means a DHCP request has not completed. */
if( *ipLOCAL_IP_ADDRESS_POINTER != 0UL )
{
switch( pxARPHeader->usOperation )
{
case ipARP_REQUEST :
/* The packet contained an ARP request. Was it for the IP
address of the node running this code? */
if( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER )
{
iptraceSENDING_ARP_REPLY( ulSenderProtocolAddress );
/* The request is for the address of this node. Add the
entry into the ARP cache, or refresh the entry if it
already exists. */
vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress );
/* Generate a reply payload in the same buffer. */
pxARPHeader->usOperation = ( uint16_t ) ipARP_REPLY;
if( ulTargetProtocolAddress == ulSenderProtocolAddress )
{
/* A double IP address is detected! */
/* Give the sources MAC address the value of the broadcast address, will be swapped later */
memcpy( pxARPFrame->xEthernetHeader.xSourceAddress.ucBytes, xBroadcastMACAddress.ucBytes, sizeof( xBroadcastMACAddress ) );
memset( pxARPHeader->xTargetHardwareAddress.ucBytes, '\0', sizeof( MACAddress_t ) );
pxARPHeader->ulTargetProtocolAddress = 0UL;
}
else
{
memcpy( pxARPHeader->xTargetHardwareAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, sizeof( MACAddress_t ) );
pxARPHeader->ulTargetProtocolAddress = ulSenderProtocolAddress;
}
memcpy( pxARPHeader->xSenderHardwareAddress.ucBytes, ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) );
memcpy( ( void* )pxARPHeader->ucSenderProtocolAddress, ( void* )ipLOCAL_IP_ADDRESS_POINTER, sizeof( pxARPHeader->ucSenderProtocolAddress ) );
eReturn = eReturnEthernetFrame;
}
break;
case ipARP_REPLY :
iptracePROCESSING_RECEIVED_ARP_REPLY( ulTargetProtocolAddress );
vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress );
/* Process received ARP frame to see if there is a clash. */
#if( ipconfigARP_USE_CLASH_DETECTION != 0 )
{
if( ulSenderProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER )
{
xARPHadIPClash = pdTRUE;
memcpy( xARPClashMacAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, sizeof( xARPClashMacAddress.ucBytes ) );
}
}
#endif /* ipconfigARP_USE_CLASH_DETECTION */
break;
default :
/* Invalid. */
break;
}
}
return eReturn;
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_ARP_REMOVE_ENTRY != 0 )
uint32_t ulARPRemoveCacheEntryByMac( const MACAddress_t * pxMACAddress )
{
BaseType_t x;
uint32_t lResult = 0;
/* For each entry in the ARP cache table. */
for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
{
if( ( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 ) )
{
lResult = xARPCache[ x ].ulIPAddress;
memset( &xARPCache[ x ], '\0', sizeof( xARPCache[ x ] ) );
break;
}
}
return lResult;
}
#endif /* ipconfigUSE_ARP_REMOVE_ENTRY != 0 */
/*-----------------------------------------------------------*/
void vARPRefreshCacheEntry( const MACAddress_t * pxMACAddress, const uint32_t ulIPAddress )
{
BaseType_t x = 0;
BaseType_t xIpEntry = -1;
BaseType_t xMacEntry = -1;
BaseType_t xUseEntry = 0;
uint8_t ucMinAgeFound = 0U;
#if( ipconfigARP_STORES_REMOTE_ADDRESSES == 0 )
/* Only process the IP address if it is on the local network.
Unless: when '*ipLOCAL_IP_ADDRESS_POINTER' equals zero, the IP-address
and netmask are still unknown. */
if( ( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) ) ||
( *ipLOCAL_IP_ADDRESS_POINTER == 0ul ) )
#else
/* If ipconfigARP_STORES_REMOTE_ADDRESSES is non-zero, IP addresses with
a different netmask will also be stored. After when replying to a UDP
message from a different netmask, the IP address can be looped up and a
reply sent. This option is useful for systems with multiple gateways,
the reply will surely arrive. If ipconfigARP_STORES_REMOTE_ADDRESSES is
zero the the gateway address is the only option. */
if( pdTRUE )
#endif
{
/* Start with the maximum possible number. */
ucMinAgeFound--;
/* For each entry in the ARP cache table. */
for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
{
/* Does this line in the cache table hold an entry for the IP
address being queried? */
if( xARPCache[ x ].ulIPAddress == ulIPAddress )
{
if( pxMACAddress == NULL )
{
/* In case the parameter pxMACAddress is NULL, an entry will be reserved to
indicate that there is an outstanding ARP request, This entry will have
"ucValid == pdFALSE". */
xIpEntry = x;
break;
}
/* See if the MAC-address also matches. */
if( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 )
{
/* This function will be called for each received packet
As this is by far the most common path the coding standard
is relaxed in this case and a return is permitted as an
optimisation. */
xARPCache[ x ].ucAge = ( uint8_t ) ipconfigMAX_ARP_AGE;
xARPCache[ x ].ucValid = ( uint8_t ) pdTRUE;
return;
}
/* Found an entry containing ulIPAddress, but the MAC address
doesn't match. Might be an entry with ucValid=pdFALSE, waiting
for an ARP reply. Still want to see if there is match with the
given MAC address.ucBytes. If found, either of the two entries
must be cleared. */
xIpEntry = x;
}
else if( ( pxMACAddress != NULL ) && ( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 ) )
{
/* Found an entry with the given MAC-address, but the IP-address
is different. Continue looping to find a possible match with
ulIPAddress. */
#if( ipconfigARP_STORES_REMOTE_ADDRESSES != 0 )
/* If ARP stores the MAC address of IP addresses outside the
network, than the MAC address of the gateway should not be
overwritten. */
BaseType_t bIsLocal[ 2 ];
bIsLocal[ 0 ] = ( ( xARPCache[ x ].ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) );
bIsLocal[ 1 ] = ( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) );
if( bIsLocal[ 0 ] == bIsLocal[ 1 ] )
{
xMacEntry = x;
}
#else
xMacEntry = x;
#endif
}
/* _HT_
Shouldn't we test for xARPCache[ x ].ucValid == pdFALSE here ? */
else if( xARPCache[ x ].ucAge < ucMinAgeFound )
{
/* As the table is traversed, remember the table row that
contains the oldest entry (the lowest age count, as ages are
decremented to zero) so the row can be re-used if this function
needs to add an entry that does not already exist. */
ucMinAgeFound = xARPCache[ x ].ucAge;
xUseEntry = x;
}
}
if( xMacEntry >= 0 )
{
xUseEntry = xMacEntry;
if( xIpEntry >= 0 )
{
/* Both the MAC address as well as the IP address were found in
different locations: clear the entry which matches the
IP-address */
memset( &xARPCache[ xIpEntry ], '\0', sizeof( xARPCache[ xIpEntry ] ) );
}
}
else if( xIpEntry >= 0 )
{
/* An entry containing the IP-address was found, but it had a different MAC address */
xUseEntry = xIpEntry;
}
/* If the entry was not found, we use the oldest entry and set the IPaddress */
xARPCache[ xUseEntry ].ulIPAddress = ulIPAddress;
if( pxMACAddress != NULL )
{
memcpy( xARPCache[ xUseEntry ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) );
iptraceARP_TABLE_ENTRY_CREATED( ulIPAddress, (*pxMACAddress) );
/* And this entry does not need immediate attention */
xARPCache[ xUseEntry ].ucAge = ( uint8_t ) ipconfigMAX_ARP_AGE;
xARPCache[ xUseEntry ].ucValid = ( uint8_t ) pdTRUE;
}
else if( xIpEntry < 0 )
{
xARPCache[ xUseEntry ].ucAge = ( uint8_t ) ipconfigMAX_ARP_RETRANSMISSIONS;
xARPCache[ xUseEntry ].ucValid = ( uint8_t ) pdFALSE;
}
}
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_ARP_REVERSED_LOOKUP == 1 )
eARPLookupResult_t eARPGetCacheEntryByMac( MACAddress_t * const pxMACAddress, uint32_t *pulIPAddress )
{
BaseType_t x;
eARPLookupResult_t eReturn = eARPCacheMiss;
/* Loop through each entry in the ARP cache. */
for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
{
/* Does this row in the ARP cache table hold an entry for the MAC
address being searched? */
if( memcmp( pxMACAddress->ucBytes, xARPCache[ x ].xMACAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 )
{
*pulIPAddress = xARPCache[ x ].ulIPAddress;
eReturn = eARPCacheHit;
break;
}
}
return eReturn;
}
#endif /* ipconfigUSE_ARP_REVERSED_LOOKUP */
/*-----------------------------------------------------------*/
eARPLookupResult_t eARPGetCacheEntry( uint32_t *pulIPAddress, MACAddress_t * const pxMACAddress )
{
eARPLookupResult_t eReturn;
uint32_t ulAddressToLookup;
#if( ipconfigUSE_LLMNR == 1 )
if( *pulIPAddress == ipLLMNR_IP_ADDR ) /* Is in network byte order. */
{
/* The LLMNR IP-address has a fixed virtual MAC address. */
memcpy( pxMACAddress->ucBytes, xLLMNR_MacAdress.ucBytes, sizeof( MACAddress_t ) );
eReturn = eARPCacheHit;
}
else
#endif
if( ( *pulIPAddress == ipBROADCAST_IP_ADDRESS ) || /* Is it the general broadcast address 255.255.255.255? */
( *pulIPAddress == xNetworkAddressing.ulBroadcastAddress ) )/* Or a local broadcast address, eg 192.168.1.255? */
{
/* This is a broadcast so uses the broadcast MAC address. */
memcpy( pxMACAddress->ucBytes, xBroadcastMACAddress.ucBytes, sizeof( MACAddress_t ) );
eReturn = eARPCacheHit;
}
else if( *ipLOCAL_IP_ADDRESS_POINTER == 0UL )
{
/* The IP address has not yet been assigned, so there is nothing that
can be done. */
eReturn = eCantSendPacket;
}
else
{
eReturn = eARPCacheMiss;
if( ( *pulIPAddress & xNetworkAddressing.ulNetMask ) != ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) )
{
#if( ipconfigARP_STORES_REMOTE_ADDRESSES == 1 )
eReturn = prvCacheLookup( *pulIPAddress, pxMACAddress );
if( eReturn == eARPCacheHit )
{
/* The stack is configured to store 'remote IP addresses', i.e. addresses
belonging to a different the netmask. prvCacheLookup() returned a hit, so
the MAC address is known */
}
else
#endif
{
/* The IP address is off the local network, so look up the
hardware address of the router, if any. */
if( xNetworkAddressing.ulGatewayAddress != ( uint32_t )0u )
{
ulAddressToLookup = xNetworkAddressing.ulGatewayAddress;
}
else
{
ulAddressToLookup = *pulIPAddress;
}
}
}
else
{
/* The IP address is on the local network, so lookup the requested
IP address directly. */
ulAddressToLookup = *pulIPAddress;
}
if( eReturn == eARPCacheMiss )
{
if( ulAddressToLookup == 0UL )
{
/* The address is not on the local network, and there is not a
router. */
eReturn = eCantSendPacket;
}
else
{
eReturn = prvCacheLookup( ulAddressToLookup, pxMACAddress );
if( eReturn == eARPCacheMiss )
{
/* It might be that the ARP has to go to the gateway. */
*pulIPAddress = ulAddressToLookup;
}
}
}
}
return eReturn;
}
/*-----------------------------------------------------------*/
static eARPLookupResult_t prvCacheLookup( uint32_t ulAddressToLookup, MACAddress_t * const pxMACAddress )
{
BaseType_t x;
eARPLookupResult_t eReturn = eARPCacheMiss;
/* Loop through each entry in the ARP cache. */
for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
{
/* Does this row in the ARP cache table hold an entry for the IP address
being queried? */
if( xARPCache[ x ].ulIPAddress == ulAddressToLookup )
{
/* A matching valid entry was found. */
if( xARPCache[ x ].ucValid == ( uint8_t ) pdFALSE )
{
/* This entry is waiting an ARP reply, so is not valid. */
eReturn = eCantSendPacket;
}
else
{
/* A valid entry was found. */
memcpy( pxMACAddress->ucBytes, xARPCache[ x ].xMACAddress.ucBytes, sizeof( MACAddress_t ) );
eReturn = eARPCacheHit;
}
break;
}
}
return eReturn;
}
/*-----------------------------------------------------------*/
void vARPAgeCache( void )
{
BaseType_t x;
TickType_t xTimeNow;
/* Loop through each entry in the ARP cache. */
for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
{
/* If the entry is valid (its age is greater than zero). */
if( xARPCache[ x ].ucAge > 0U )
{
/* Decrement the age value of the entry in this ARP cache table row.
When the age reaches zero it is no longer considered valid. */
( xARPCache[ x ].ucAge )--;
/* If the entry is not yet valid, then it is waiting an ARP
reply, and the ARP request should be retransmitted. */
if( xARPCache[ x ].ucValid == ( uint8_t ) pdFALSE )
{
FreeRTOS_OutputARPRequest( xARPCache[ x ].ulIPAddress );
}
else if( xARPCache[ x ].ucAge <= ( uint8_t ) arpMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST )
{
/* This entry will get removed soon. See if the MAC address is
still valid to prevent this happening. */
iptraceARP_TABLE_ENTRY_WILL_EXPIRE( xARPCache[ x ].ulIPAddress );
FreeRTOS_OutputARPRequest( xARPCache[ x ].ulIPAddress );
}
else
{
/* The age has just ticked down, with nothing to do. */
}
if( xARPCache[ x ].ucAge == 0u )
{
/* The entry is no longer valid. Wipe it out. */
iptraceARP_TABLE_ENTRY_EXPIRED( xARPCache[ x ].ulIPAddress );
xARPCache[ x ].ulIPAddress = 0UL;
}
}
}
xTimeNow = xTaskGetTickCount ();
if( ( xLastGratuitousARPTime == ( TickType_t ) 0 ) || ( ( xTimeNow - xLastGratuitousARPTime ) > ( TickType_t ) arpGRATUITOUS_ARP_PERIOD ) )
{
FreeRTOS_OutputARPRequest( *ipLOCAL_IP_ADDRESS_POINTER );
xLastGratuitousARPTime = xTimeNow;
}
}
/*-----------------------------------------------------------*/
void vARPSendGratuitous( void )
{
/* Setting xLastGratuitousARPTime to 0 will force a gratuitous ARP the next
time vARPAgeCache() is called. */
xLastGratuitousARPTime = ( TickType_t ) 0;
/* Let the IP-task call vARPAgeCache(). */
xSendEventToIPTask( eARPTimerEvent );
}
/*-----------------------------------------------------------*/
void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress )
{
NetworkBufferDescriptor_t *pxNetworkBuffer;
/* This is called from the context of the IP event task, so a block time
must not be used. */
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( sizeof( ARPPacket_t ), ( TickType_t ) 0 );
if( pxNetworkBuffer != NULL )
{
pxNetworkBuffer->ulIPAddress = ulIPAddress;
vARPGenerateRequestPacket( pxNetworkBuffer );
#if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
{
if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
{
BaseType_t xIndex;
for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
{
pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
}
pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
}
}
#endif
xNetworkInterfaceOutput( pxNetworkBuffer, pdTRUE );
}
}
void vARPGenerateRequestPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
{
ARPPacket_t *pxARPPacket;
pxARPPacket = ( ARPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
/* memcpy the const part of the header information into the correct
location in the packet. This copies:
xEthernetHeader.ulDestinationAddress
xEthernetHeader.usFrameType;
xARPHeader.usHardwareType;
xARPHeader.usProtocolType;
xARPHeader.ucHardwareAddressLength;
xARPHeader.ucProtocolAddressLength;
xARPHeader.usOperation;
xARPHeader.xTargetHardwareAddress;
*/
memcpy( ( void * ) pxARPPacket, ( void * ) xDefaultPartARPPacketHeader, sizeof( xDefaultPartARPPacketHeader ) );
memcpy( ( void * ) pxARPPacket->xEthernetHeader.xSourceAddress.ucBytes , ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
memcpy( ( void * ) pxARPPacket->xARPHeader.xSenderHardwareAddress.ucBytes, ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
memcpy( ( void* )pxARPPacket->xARPHeader.ucSenderProtocolAddress, ( void* )ipLOCAL_IP_ADDRESS_POINTER, sizeof( pxARPPacket->xARPHeader.ucSenderProtocolAddress ) );
pxARPPacket->xARPHeader.ulTargetProtocolAddress = pxNetworkBuffer->ulIPAddress;
pxNetworkBuffer->xDataLength = sizeof( ARPPacket_t );
iptraceCREATING_ARP_REQUEST( pxNetworkBuffer->ulIPAddress );
}
/*-----------------------------------------------------------*/
void FreeRTOS_ClearARP( void )
{
memset( xARPCache, '\0', sizeof( xARPCache ) );
}
/*-----------------------------------------------------------*/
#if( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 )
void FreeRTOS_PrintARPCache( void )
{
BaseType_t x, xCount = 0;
/* Loop through each entry in the ARP cache. */
for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
{
if( ( xARPCache[ x ].ulIPAddress != 0ul ) && ( xARPCache[ x ].ucAge > 0U ) )
{
/* See if the MAC-address also matches, and we're all happy */
FreeRTOS_printf( ( "Arp %2ld: %3u - %16lxip : %02x:%02x:%02x : %02x:%02x:%02x\n",
x,
xARPCache[ x ].ucAge,
xARPCache[ x ].ulIPAddress,
xARPCache[ x ].xMACAddress.ucBytes[0],
xARPCache[ x ].xMACAddress.ucBytes[1],
xARPCache[ x ].xMACAddress.ucBytes[2],
xARPCache[ x ].xMACAddress.ucBytes[3],
xARPCache[ x ].xMACAddress.ucBytes[4],
xARPCache[ x ].xMACAddress.ucBytes[5] ) );
xCount++;
}
}
FreeRTOS_printf( ( "Arp has %ld entries\n", xCount ) );
}
#endif /* ( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 ) */
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_TCP_IP.h"
#include "FreeRTOS_DHCP.h"
#include "FreeRTOS_ARP.h"
#include "NetworkInterface.h"
#include "NetworkBufferManagement.h"
/* Exclude the entire file if DHCP is not enabled. */
#if( ipconfigUSE_DHCP != 0 )
#if ( ipconfigUSE_DHCP != 0 ) && ( ipconfigNETWORK_MTU < 586u )
/* DHCP must be able to receive an options field of 312 bytes, the fixed
part of the DHCP packet is 240 bytes, and the IP/UDP headers take 28 bytes. */
#error ipconfigNETWORK_MTU needs to be at least 586 to use DHCP
#endif
/* Parameter widths in the DHCP packet. */
#define dhcpCLIENT_HARDWARE_ADDRESS_LENGTH 16
#define dhcpSERVER_HOST_NAME_LENGTH 64
#define dhcpBOOT_FILE_NAME_LENGTH 128
/* Timer parameters */
#ifndef dhcpINITIAL_DHCP_TX_PERIOD
#define dhcpINITIAL_TIMER_PERIOD ( pdMS_TO_TICKS( 250 ) )
#define dhcpINITIAL_DHCP_TX_PERIOD ( pdMS_TO_TICKS( 5000 ) )
#endif
/* Codes of interest found in the DHCP options field. */
#define dhcpZERO_PAD_OPTION_CODE ( 0u )
#define dhcpSUBNET_MASK_OPTION_CODE ( 1u )
#define dhcpGATEWAY_OPTION_CODE ( 3u )
#define dhcpDNS_SERVER_OPTIONS_CODE ( 6u )
#define dhcpDNS_HOSTNAME_OPTIONS_CODE ( 12u )
#define dhcpREQUEST_IP_ADDRESS_OPTION_CODE ( 50u )
#define dhcpLEASE_TIME_OPTION_CODE ( 51u )
#define dhcpMESSAGE_TYPE_OPTION_CODE ( 53u )
#define dhcpSERVER_IP_ADDRESS_OPTION_CODE ( 54u )
#define dhcpPARAMETER_REQUEST_OPTION_CODE ( 55u )
#define dhcpCLIENT_IDENTIFIER_OPTION_CODE ( 61u )
/* The four DHCP message types of interest. */
#define dhcpMESSAGE_TYPE_DISCOVER ( 1 )
#define dhcpMESSAGE_TYPE_OFFER ( 2 )
#define dhcpMESSAGE_TYPE_REQUEST ( 3 )
#define dhcpMESSAGE_TYPE_ACK ( 5 )
#define dhcpMESSAGE_TYPE_NACK ( 6 )
/* Offsets into the transmitted DHCP options fields at which various parameters
are located. */
#define dhcpCLIENT_IDENTIFIER_OFFSET ( 5 )
#define dhcpREQUESTED_IP_ADDRESS_OFFSET ( 13 )
#define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ( 19 )
/* Values used in the DHCP packets. */
#define dhcpREQUEST_OPCODE ( 1 )
#define dhcpREPLY_OPCODE ( 2 )
#define dhcpADDRESS_TYPE_ETHERNET ( 1 )
#define dhcpETHERNET_ADDRESS_LENGTH ( 6 )
/* If a lease time is not received, use the default of two days. */
/* 48 hours in ticks. Can not use pdMS_TO_TICKS() as integer overflow can occur. */
#define dhcpDEFAULT_LEASE_TIME ( ( 48UL * 60UL * 60UL ) * configTICK_RATE_HZ )
/* Don't allow the lease time to be too short. */
#define dhcpMINIMUM_LEASE_TIME ( pdMS_TO_TICKS( 60000UL ) ) /* 60 seconds in ticks. */
/* Marks the end of the variable length options field in the DHCP packet. */
#define dhcpOPTION_END_BYTE 0xffu
/* Offset into a DHCP message at which the first byte of the options is
located. */
#define dhcpFIRST_OPTION_BYTE_OFFSET ( 0xf0 )
/* When walking the variable length options field, the following value is used
to ensure the walk has not gone past the end of the valid options. 2 bytes is
made up of the length byte, and minimum one byte value. */
#define dhcpMAX_OPTION_LENGTH_OF_INTEREST ( 2L )
/* Standard DHCP port numbers and magic cookie value. */
#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
#define dhcpCLIENT_PORT 0x4400u
#define dhcpSERVER_PORT 0x4300u
#define dhcpCOOKIE 0x63538263ul
#define dhcpBROADCAST 0x0080u
#else
#define dhcpCLIENT_PORT 0x0044u
#define dhcpSERVER_PORT 0x0043u
#define dhcpCOOKIE 0x63825363ul
#define dhcpBROADCAST 0x8000u
#endif /* ipconfigBYTE_ORDER */
#include "pack_struct_start.h"
struct xDHCPMessage
{
uint8_t ucOpcode;
uint8_t ucAddressType;
uint8_t ucAddressLength;
uint8_t ucHops;
uint32_t ulTransactionID;
uint16_t usElapsedTime;
uint16_t usFlags;
uint32_t ulClientIPAddress_ciaddr;
uint32_t ulYourIPAddress_yiaddr;
uint32_t ulServerIPAddress_siaddr;
uint32_t ulRelayAgentIPAddress_giaddr;
uint8_t ucClientHardwareAddress[ dhcpCLIENT_HARDWARE_ADDRESS_LENGTH ];
uint8_t ucServerHostName[ dhcpSERVER_HOST_NAME_LENGTH ];
uint8_t ucBootFileName[ dhcpBOOT_FILE_NAME_LENGTH ];
uint32_t ulDHCPCookie;
uint8_t ucFirstOptionByte;
}
#include "pack_struct_end.h"
typedef struct xDHCPMessage DHCPMessage_t;
/* DHCP state machine states. */
typedef enum
{
eWaitingSendFirstDiscover = 0, /* Initial state. Send a discover the first time it is called, and reset all timers. */
eWaitingOffer, /* Either resend the discover, or, if the offer is forthcoming, send a request. */
eWaitingAcknowledge, /* Either resend the request. */
#if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )
eGetLinkLayerAddress, /* When DHCP didn't respond, try to obtain a LinkLayer address 168.254.x.x. */
#endif
eLeasedAddress, /* Resend the request at the appropriate time to renew the lease. */
eNotUsingLeasedAddress /* DHCP failed, and a default IP address is being used. */
} eDHCPState_t;
/* Hold information in between steps in the DHCP state machine. */
struct xDHCP_DATA
{
uint32_t ulTransactionId;
uint32_t ulOfferedIPAddress;
uint32_t ulDHCPServerAddress;
uint32_t ulLeaseTime;
/* Hold information on the current timer state. */
TickType_t xDHCPTxTime;
TickType_t xDHCPTxPeriod;
/* Try both without and with the broadcast flag */
BaseType_t xUseBroadcast;
/* Maintains the DHCP state machine state. */
eDHCPState_t eDHCPState;
/* The UDP socket used for all incoming and outgoing DHCP traffic. */
Socket_t xDHCPSocket;
};
typedef struct xDHCP_DATA DHCPData_t;
#if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )
/* Define the Link Layer IP address: 169.254.x.x */
#define LINK_LAYER_ADDRESS_0 169
#define LINK_LAYER_ADDRESS_1 254
/* Define the netmask used: 255.255.0.0 */
#define LINK_LAYER_NETMASK_0 255
#define LINK_LAYER_NETMASK_1 255
#define LINK_LAYER_NETMASK_2 0
#define LINK_LAYER_NETMASK_3 0
#endif
/*
* Generate a DHCP discover message and send it on the DHCP socket.
*/
static void prvSendDHCPDiscover( void );
/*
* Interpret message received on the DHCP socket.
*/
static BaseType_t prvProcessDHCPReplies( BaseType_t xExpectedMessageType );
/*
* Generate a DHCP request packet, and send it on the DHCP socket.
*/
static void prvSendDHCPRequest( void );
/*
* Prepare to start a DHCP transaction. This initialises some state variables
* and creates the DHCP socket if necessary.
*/
static void prvInitialiseDHCP( void );
/*
* Creates the part of outgoing DHCP messages that are common to all outgoing
* DHCP messages.
*/
static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, BaseType_t xOpcode, const uint8_t * const pucOptionsArray, size_t *pxOptionsArraySize );
/*
* Create the DHCP socket, if it has not been created already.
*/
static void prvCreateDHCPSocket( void );
/*
* After DHCP has failed to answer, prepare everything to start searching
* for (trying-out) LinkLayer IP-addresses, using the random method: Send
* a gratuitous ARP request and wait if another device responds to it.
*/
#if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )
static void prvPrepareLinkLayerIPLookUp( void );
#endif
/*-----------------------------------------------------------*/
/* The next DHCP transaction Id to be used. */
static DHCPData_t xDHCPData;
/*-----------------------------------------------------------*/
BaseType_t xIsDHCPSocket( Socket_t xSocket )
{
BaseType_t xReturn;
if( xDHCPData.xDHCPSocket == xSocket )
{
xReturn = pdTRUE;
}
else
{
xReturn = pdFALSE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
void vDHCPProcess( BaseType_t xReset )
{
BaseType_t xGivingUp = pdFALSE;
#if( ipconfigUSE_DHCP_HOOK != 0 )
eDHCPCallbackAnswer_t eAnswer;
#endif /* ipconfigUSE_DHCP_HOOK */
/* Is DHCP starting over? */
if( xReset != pdFALSE )
{
xDHCPData.eDHCPState = eWaitingSendFirstDiscover;
}
switch( xDHCPData.eDHCPState )
{
case eWaitingSendFirstDiscover :
/* Ask the user if a DHCP discovery is required. */
#if( ipconfigUSE_DHCP_HOOK != 0 )
eAnswer = xApplicationDHCPHook( eDHCPPhasePreDiscover, xNetworkAddressing.ulDefaultIPAddress );
if( eAnswer == eDHCPContinue )
#endif /* ipconfigUSE_DHCP_HOOK */
{
/* Initial state. Create the DHCP socket, timer, etc. if they
have not already been created. */
prvInitialiseDHCP();
/* See if prvInitialiseDHCP() has creates a socket. */
if( xDHCPData.xDHCPSocket == NULL )
{
xGivingUp = pdTRUE;
break;
}
*ipLOCAL_IP_ADDRESS_POINTER = 0UL;
/* Send the first discover request. */
if( xDHCPData.xDHCPSocket != NULL )
{
xDHCPData.xDHCPTxTime = xTaskGetTickCount();
prvSendDHCPDiscover( );
xDHCPData.eDHCPState = eWaitingOffer;
}
}
#if( ipconfigUSE_DHCP_HOOK != 0 )
else
{
if( eAnswer == eDHCPUseDefaults )
{
memcpy( &xNetworkAddressing, &xDefaultAddressing, sizeof( xNetworkAddressing ) );
}
/* The user indicates that the DHCP process does not continue. */
xGivingUp = pdTRUE;
}
#endif /* ipconfigUSE_DHCP_HOOK */
break;
case eWaitingOffer :
xGivingUp = pdFALSE;
/* Look for offers coming in. */
if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER ) == pdPASS )
{
#if( ipconfigUSE_DHCP_HOOK != 0 )
/* Ask the user if a DHCP request is required. */
eAnswer = xApplicationDHCPHook( eDHCPPhasePreRequest, xDHCPData.ulOfferedIPAddress );
if( eAnswer == eDHCPContinue )
#endif /* ipconfigUSE_DHCP_HOOK */
{
/* An offer has been made, the user wants to continue,
generate the request. */
xDHCPData.xDHCPTxTime = xTaskGetTickCount();
xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
prvSendDHCPRequest( );
xDHCPData.eDHCPState = eWaitingAcknowledge;
break;
}
#if( ipconfigUSE_DHCP_HOOK != 0 )
if( eAnswer == eDHCPUseDefaults )
{
memcpy( &xNetworkAddressing, &xDefaultAddressing, sizeof( xNetworkAddressing ) );
}
/* The user indicates that the DHCP process does not continue. */
xGivingUp = pdTRUE;
#endif /* ipconfigUSE_DHCP_HOOK */
}
else if( ( xTaskGetTickCount() - xDHCPData.xDHCPTxTime ) > xDHCPData.xDHCPTxPeriod )
{
/* It is time to send another Discover. Increase the time
period, and if it has not got to the point of giving up - send
another discovery. */
xDHCPData.xDHCPTxPeriod <<= 1;
if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
{
xDHCPData.ulTransactionId = ipconfigRAND32( );
if( 0 != xDHCPData.ulTransactionId )
{
xDHCPData.xDHCPTxTime = xTaskGetTickCount( );
xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast;
prvSendDHCPDiscover( );
FreeRTOS_debug_printf( ( "vDHCPProcess: timeout %lu ticks\n", xDHCPData.xDHCPTxPeriod ) );
}
else
{
FreeRTOS_debug_printf( ( "vDHCPProcess: failed to generate a random Transaction ID\n" ) );
}
}
else
{
FreeRTOS_debug_printf( ( "vDHCPProcess: giving up %lu > %lu ticks\n", xDHCPData.xDHCPTxPeriod, ipconfigMAXIMUM_DISCOVER_TX_PERIOD ) );
#if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )
{
/* Only use a fake Ack if the default IP address == 0x00
and the link local addressing is used. Start searching
a free LinkLayer IP-address. Next state will be
'eGetLinkLayerAddress'. */
prvPrepareLinkLayerIPLookUp();
/* Setting an IP address manually so set to not using
leased address mode. */
xDHCPData.eDHCPState = eGetLinkLayerAddress;
}
#else
{
xGivingUp = pdTRUE;
}
#endif /* ipconfigDHCP_FALL_BACK_AUTO_IP */
}
}
break;
case eWaitingAcknowledge :
/* Look for acks coming in. */
if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK ) == pdPASS )
{
FreeRTOS_debug_printf( ( "vDHCPProcess: acked %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );
/* DHCP completed. The IP address can now be used, and the
timer set to the lease timeout time. */
*ipLOCAL_IP_ADDRESS_POINTER = xDHCPData.ulOfferedIPAddress;
/* Setting the 'local' broadcast address, something like
'192.168.1.255'. */
xNetworkAddressing.ulBroadcastAddress = ( xDHCPData.ulOfferedIPAddress & xNetworkAddressing.ulNetMask ) | ~xNetworkAddressing.ulNetMask;
xDHCPData.eDHCPState = eLeasedAddress;
iptraceDHCP_SUCCEDEED( xDHCPData.ulOfferedIPAddress );
/* DHCP failed, the default configured IP-address will be used
Now call vIPNetworkUpCalls() to send the network-up event and
start the ARP timer. */
vIPNetworkUpCalls( );
/* Close socket to ensure packets don't queue on it. */
vSocketClose( xDHCPData.xDHCPSocket );
xDHCPData.xDHCPSocket = NULL;
if( xDHCPData.ulLeaseTime == 0UL )
{
xDHCPData.ulLeaseTime = dhcpDEFAULT_LEASE_TIME;
}
else if( xDHCPData.ulLeaseTime < dhcpMINIMUM_LEASE_TIME )
{
xDHCPData.ulLeaseTime = dhcpMINIMUM_LEASE_TIME;
}
else
{
/* The lease time is already valid. */
}
/* Check for clashes. */
vARPSendGratuitous();
vIPReloadDHCPTimer( xDHCPData.ulLeaseTime );
}
else
{
/* Is it time to send another Discover? */
if( ( xTaskGetTickCount() - xDHCPData.xDHCPTxTime ) > xDHCPData.xDHCPTxPeriod )
{
/* Increase the time period, and if it has not got to the
point of giving up - send another request. */
xDHCPData.xDHCPTxPeriod <<= 1;
if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
{
xDHCPData.xDHCPTxTime = xTaskGetTickCount();
prvSendDHCPRequest( );
}
else
{
/* Give up, start again. */
xDHCPData.eDHCPState = eWaitingSendFirstDiscover;
}
}
}
break;
#if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )
case eGetLinkLayerAddress:
if( ( xTaskGetTickCount() - xDHCPData.xDHCPTxTime ) > xDHCPData.xDHCPTxPeriod )
{
if( xARPHadIPClash == pdFALSE )
{
/* ARP OK. proceed. */
iptraceDHCP_SUCCEDEED( xDHCPData.ulOfferedIPAddress );
/* Auto-IP succeeded, the default configured IP-address will
be used. Now call vIPNetworkUpCalls() to send the
network-up event and start the ARP timer. */
vIPNetworkUpCalls( );
xDHCPData.eDHCPState = eNotUsingLeasedAddress;
}
else
{
/* ARP clashed - try another IP address. */
prvPrepareLinkLayerIPLookUp();
/* Setting an IP address manually so set to not using leased
address mode. */
xDHCPData.eDHCPState = eGetLinkLayerAddress;
}
}
break;
#endif /* ipconfigDHCP_FALL_BACK_AUTO_IP */
case eLeasedAddress :
/* Resend the request at the appropriate time to renew the lease. */
prvCreateDHCPSocket();
if( xDHCPData.xDHCPSocket != NULL )
{
xDHCPData.xDHCPTxTime = xTaskGetTickCount();
xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
prvSendDHCPRequest( );
xDHCPData.eDHCPState = eWaitingAcknowledge;
/* From now on, we should be called more often */
vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );
}
break;
case eNotUsingLeasedAddress:
vIPSetDHCPTimerEnableState( pdFALSE );
break;
default:
break;
}
if( xGivingUp != pdFALSE )
{
/* xGivingUp became true either because of a time-out, or because
xApplicationDHCPHook() returned another value than 'eDHCPContinue',
meaning that the conversion is canceled from here. */
/* Revert to static IP address. */
taskENTER_CRITICAL();
{
*ipLOCAL_IP_ADDRESS_POINTER = xNetworkAddressing.ulDefaultIPAddress;
iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( xNetworkAddressing.ulDefaultIPAddress );
}
taskEXIT_CRITICAL();
xDHCPData.eDHCPState = eNotUsingLeasedAddress;
vIPSetDHCPTimerEnableState( pdFALSE );
/* DHCP failed, the default configured IP-address will be used. Now
call vIPNetworkUpCalls() to send the network-up event and start the ARP
timer. */
vIPNetworkUpCalls( );
/* Test if socket was indeed created. */
if( xDHCPData.xDHCPSocket != NULL )
{
/* Close socket to ensure packets don't queue on it. */
vSocketClose( xDHCPData.xDHCPSocket );
xDHCPData.xDHCPSocket = NULL;
}
}
}
/*-----------------------------------------------------------*/
static void prvCreateDHCPSocket( void )
{
struct freertos_sockaddr xAddress;
BaseType_t xReturn;
TickType_t xTimeoutTime = ( TickType_t ) 0;
/* Create the socket, if it has not already been created. */
if( xDHCPData.xDHCPSocket == NULL )
{
xDHCPData.xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
if( xDHCPData.xDHCPSocket != FREERTOS_INVALID_SOCKET )
{
/* Ensure the Rx and Tx timeouts are zero as the DHCP executes in the
context of the IP task. */
FreeRTOS_setsockopt( xDHCPData.xDHCPSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
FreeRTOS_setsockopt( xDHCPData.xDHCPSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
/* Bind to the standard DHCP client port. */
xAddress.sin_port = ( uint16_t ) dhcpCLIENT_PORT;
xReturn = vSocketBind( xDHCPData.xDHCPSocket, &xAddress, sizeof( xAddress ), pdFALSE );
if( xReturn != 0 )
{
/* Binding failed, close the socket again. */
vSocketClose( xDHCPData.xDHCPSocket );
xDHCPData.xDHCPSocket = NULL;
}
}
else
{
/* Change to NULL for easier testing. */
xDHCPData.xDHCPSocket = NULL;
}
}
}
/*-----------------------------------------------------------*/
static void prvInitialiseDHCP( void )
{
/* Initialise the parameters that will be set by the DHCP process. Per
https://www.ietf.org/rfc/rfc2131.txt, Transaction ID should be a random
value chosen by the client. */
xDHCPData.ulTransactionId = ipconfigRAND32();
/* Check for random number generator API failure. */
if( 0 != xDHCPData.ulTransactionId )
{
xDHCPData.xUseBroadcast = 0;
xDHCPData.ulOfferedIPAddress = 0UL;
xDHCPData.ulDHCPServerAddress = 0UL;
xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
/* Create the DHCP socket if it has not already been created. */
prvCreateDHCPSocket();
FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) );
vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );
}
}
/*-----------------------------------------------------------*/
static BaseType_t prvProcessDHCPReplies( BaseType_t xExpectedMessageType )
{
uint8_t *pucUDPPayload, *pucLastByte;
struct freertos_sockaddr xClient;
uint32_t xClientLength = sizeof( xClient );
int32_t lBytes;
DHCPMessage_t *pxDHCPMessage;
uint8_t *pucByte, ucOptionCode, ucLength;
uint32_t ulProcessed, ulParameter;
BaseType_t xReturn = pdFALSE;
const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct DHCP message type must be present in the options. */
lBytes = FreeRTOS_recvfrom( xDHCPData.xDHCPSocket, ( void * ) &pucUDPPayload, 0ul, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
if( lBytes > 0 )
{
/* Map a DHCP structure onto the received data. */
pxDHCPMessage = ( DHCPMessage_t * ) ( pucUDPPayload );
/* Sanity check. */
if( ( lBytes >= sizeof( DHCPMessage_t ) ) &&
( pxDHCPMessage->ulDHCPCookie == ( uint32_t ) dhcpCOOKIE ) &&
( pxDHCPMessage->ucOpcode == ( uint8_t ) dhcpREPLY_OPCODE ) &&
( pxDHCPMessage->ulTransactionID == FreeRTOS_htonl( xDHCPData.ulTransactionId ) ) )
{
if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ),
( void * ) ipLOCAL_MAC_ADDRESS,
sizeof( MACAddress_t ) ) == 0 )
{
/* None of the essential options have been processed yet. */
ulProcessed = 0ul;
/* Walk through the options until the dhcpOPTION_END_BYTE byte
is found, taking care not to walk off the end of the options. */
pucByte = &( pxDHCPMessage->ucFirstOptionByte );
pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );
while( pucByte < pucLastByte )
{
ucOptionCode = pucByte[ 0 ];
if( ucOptionCode == dhcpOPTION_END_BYTE )
{
/* Ready, the last byte has been seen. */
break;
}
if( ucOptionCode == dhcpZERO_PAD_OPTION_CODE )
{
/* The value zero is used as a pad byte,
it is not followed by a length byte. */
pucByte += 1;
continue;
}
/* Stop if the response is malformed. */
if( pucByte < pucLastByte - 1 )
{
ucLength = pucByte[ 1 ];
pucByte += 2;
if( pucByte >= pucLastByte - ucLength )
{
break;
}
}
else
{
break;
}
/* In most cases, a 4-byte network-endian parameter follows,
just get it once here and use later. */
if( ucLength >= sizeof( ulParameter ) )
{
memcpy( ( void * ) &( ulParameter ),
( void * ) pucByte,
( size_t ) sizeof( ulParameter ) );
}
else
{
ulParameter = 0;
}
/* Option-specific handling. */
switch( ucOptionCode )
{
case dhcpMESSAGE_TYPE_OPTION_CODE :
if( *pucByte == ( uint8_t ) xExpectedMessageType )
{
/* The message type is the message type the
state machine is expecting. */
ulProcessed++;
}
else if( *pucByte == ( uint8_t ) dhcpMESSAGE_TYPE_NACK )
{
if( xExpectedMessageType == ( BaseType_t ) dhcpMESSAGE_TYPE_ACK )
{
/* Start again. */
xDHCPData.eDHCPState = eWaitingSendFirstDiscover;
}
}
else
{
/* Don't process other message types. */
}
break;
case dhcpSUBNET_MASK_OPTION_CODE :
if( ucLength == sizeof( uint32_t ) )
{
xNetworkAddressing.ulNetMask = ulParameter;
}
break;
case dhcpGATEWAY_OPTION_CODE :
if( ucLength == sizeof( uint32_t ) )
{
/* ulProcessed is not incremented in this case
because the gateway is not essential. */
xNetworkAddressing.ulGatewayAddress = ulParameter;
}
break;
case dhcpDNS_SERVER_OPTIONS_CODE :
/* ulProcessed is not incremented in this case
because the DNS server is not essential. Only the
first DNS server address is taken. */
xNetworkAddressing.ulDNSServerAddress = ulParameter;
break;
case dhcpSERVER_IP_ADDRESS_OPTION_CODE :
if( ucLength == sizeof( uint32_t ) )
{
if( xExpectedMessageType == ( BaseType_t ) dhcpMESSAGE_TYPE_OFFER )
{
/* Offers state the replying server. */
ulProcessed++;
xDHCPData.ulDHCPServerAddress = ulParameter;
}
else
{
/* The ack must come from the expected server. */
if( xDHCPData.ulDHCPServerAddress == ulParameter )
{
ulProcessed++;
}
}
}
break;
case dhcpLEASE_TIME_OPTION_CODE :
if( ucLength == sizeof( xDHCPData.ulLeaseTime ) )
{
/* ulProcessed is not incremented in this case
because the lease time is not essential. */
/* The DHCP parameter is in seconds, convert
to host-endian format. */
xDHCPData.ulLeaseTime = FreeRTOS_ntohl( ulParameter );
/* Divide the lease time by two to ensure a
renew request is sent before the lease actually
expires. */
xDHCPData.ulLeaseTime >>= 1UL;
/* Multiply with configTICK_RATE_HZ to get clock
ticks. */
xDHCPData.ulLeaseTime = configTICK_RATE_HZ * xDHCPData.ulLeaseTime;
}
break;
default :
/* Not interested in this field. */
break;
}
/* Jump over the data to find the next option code. */
if( ucLength == 0u )
{
break;
}
else
{
pucByte += ucLength;
}
}
/* Were all the mandatory options received? */
if( ulProcessed >= ulMandatoryOptions )
{
/* HT:endian: used to be network order */
xDHCPData.ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;
FreeRTOS_printf( ( "vDHCPProcess: offer %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );
xReturn = pdPASS;
}
}
}
FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );
}
return xReturn;
}
/*-----------------------------------------------------------*/
static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, BaseType_t xOpcode, const uint8_t * const pucOptionsArray, size_t *pxOptionsArraySize )
{
DHCPMessage_t *pxDHCPMessage;
size_t xRequiredBufferSize = sizeof( DHCPMessage_t ) + *pxOptionsArraySize;
uint8_t *pucUDPPayloadBuffer;
#if( ipconfigDHCP_REGISTER_HOSTNAME == 1 )
const char *pucHostName = pcApplicationHostnameHook ();
size_t xNameLength = strlen( pucHostName );
uint8_t *pucPtr;
xRequiredBufferSize += ( 2 + xNameLength );
#endif
/* Get a buffer. This uses a maximum delay, but the delay will be capped
to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value still needs to
be test. */
do
{
} while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );
pxDHCPMessage = ( DHCPMessage_t * ) pucUDPPayloadBuffer;
/* Most fields need to be zero. */
memset( ( void * ) pxDHCPMessage, 0x00, sizeof( DHCPMessage_t ) );
/* Create the message. */
pxDHCPMessage->ucOpcode = ( uint8_t ) xOpcode;
pxDHCPMessage->ucAddressType = ( uint8_t ) dhcpADDRESS_TYPE_ETHERNET;
pxDHCPMessage->ucAddressLength = ( uint8_t ) dhcpETHERNET_ADDRESS_LENGTH;
pxDHCPMessage->ulTransactionID = FreeRTOS_htonl( xDHCPData.ulTransactionId );
pxDHCPMessage->ulDHCPCookie = ( uint32_t ) dhcpCOOKIE;
if( xDHCPData.xUseBroadcast != pdFALSE )
{
pxDHCPMessage->usFlags = ( uint16_t ) dhcpBROADCAST;
}
else
{
pxDHCPMessage->usFlags = 0u;
}
memcpy( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress[ 0 ] ), ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) );
/* Copy in the const part of the options options. */
memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, *pxOptionsArraySize );
#if( ipconfigDHCP_REGISTER_HOSTNAME == 1 )
{
/* With this option, the hostname can be registered as well which makes
it easier to lookup a device in a router's list of DHCP clients. */
/* Point to where the OPTION_END was stored to add data. */
pucPtr = &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + ( *pxOptionsArraySize - 1 ) ] );
pucPtr[ 0 ] = dhcpDNS_HOSTNAME_OPTIONS_CODE;
pucPtr[ 1 ] = ( uint8_t ) xNameLength;
memcpy( ( void *) ( pucPtr + 2 ), pucHostName, xNameLength );
pucPtr[ 2 + xNameLength ] = dhcpOPTION_END_BYTE;
*pxOptionsArraySize += ( 2 + xNameLength );
}
#endif
/* Map in the client identifier. */
memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpCLIENT_IDENTIFIER_OFFSET ] ),
( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) );
/* Set the addressing. */
pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;
pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;
return pucUDPPayloadBuffer;
}
/*-----------------------------------------------------------*/
static void prvSendDHCPRequest( void )
{
uint8_t *pucUDPPayloadBuffer;
struct freertos_sockaddr xAddress;
static const uint8_t ucDHCPRequestOptions[] =
{
/* Do not change the ordering without also changing
dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and
dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */
dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST, /* Message type option. */
dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
dhcpREQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address being requested. */
dhcpSERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address of the DHCP server. */
dhcpOPTION_END_BYTE
};
size_t xOptionsLength = sizeof( ucDHCPRequestOptions );
pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, &xOptionsLength );
/* Copy in the IP address being requested. */
memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpREQUESTED_IP_ADDRESS_OFFSET ] ),
( void * ) &( xDHCPData.ulOfferedIPAddress ), sizeof( xDHCPData.ulOfferedIPAddress ) );
/* Copy in the address of the DHCP server being used. */
memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ] ),
( void * ) &( xDHCPData.ulDHCPServerAddress ), sizeof( xDHCPData.ulDHCPServerAddress ) );
FreeRTOS_debug_printf( ( "vDHCPProcess: reply %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );
iptraceSENDING_DHCP_REQUEST();
if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
{
/* The packet was not successfully queued for sending and must be
returned to the stack. */
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
}
}
/*-----------------------------------------------------------*/
static void prvSendDHCPDiscover( void )
{
uint8_t *pucUDPPayloadBuffer;
struct freertos_sockaddr xAddress;
static const uint8_t ucDHCPDiscoverOptions[] =
{
/* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */
dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER, /* Message type option. */
dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
dhcpPARAMETER_REQUEST_OPTION_CODE, 3, dhcpSUBNET_MASK_OPTION_CODE, dhcpGATEWAY_OPTION_CODE, dhcpDNS_SERVER_OPTIONS_CODE, /* Parameter request option. */
dhcpOPTION_END_BYTE
};
size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );
pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, &xOptionsLength );
FreeRTOS_debug_printf( ( "vDHCPProcess: discover\n" ) );
iptraceSENDING_DHCP_DISCOVER();
if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
{
/* The packet was not successfully queued for sending and must be
returned to the stack. */
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
}
}
/*-----------------------------------------------------------*/
#if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )
static void prvPrepareLinkLayerIPLookUp( void )
{
uint8_t ucLinkLayerIPAddress[ 2 ];
/* After DHCP has failed to answer, prepare everything to start
trying-out LinkLayer IP-addresses, using the random method. */
xDHCPData.xDHCPTxTime = xTaskGetTickCount();
ucLinkLayerIPAddress[ 0 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32() % 0xFDu ); /* get value 1..254 for IP-address 3rd byte of IP address to try. */
ucLinkLayerIPAddress[ 1 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32() % 0xFDu ); /* get value 1..254 for IP-address 4th byte of IP address to try. */
xNetworkAddressing.ulGatewayAddress = FreeRTOS_htonl( 0xA9FE0203 );
/* prepare xDHCPData with data to test. */
xDHCPData.ulOfferedIPAddress =
FreeRTOS_inet_addr_quick( LINK_LAYER_ADDRESS_0, LINK_LAYER_ADDRESS_1, ucLinkLayerIPAddress[ 0 ], ucLinkLayerIPAddress[ 1 ] );
xDHCPData.ulLeaseTime = dhcpDEFAULT_LEASE_TIME; /* don't care about lease time. just put anything. */
xNetworkAddressing.ulNetMask =
FreeRTOS_inet_addr_quick( LINK_LAYER_NETMASK_0, LINK_LAYER_NETMASK_1, LINK_LAYER_NETMASK_2, LINK_LAYER_NETMASK_3 );
/* DHCP completed. The IP address can now be used, and the
timer set to the lease timeout time. */
*ipLOCAL_IP_ADDRESS_POINTER = xDHCPData.ulOfferedIPAddress;
/* Setting the 'local' broadcast address, something like 192.168.1.255' */
xNetworkAddressing.ulBroadcastAddress = ( xDHCPData.ulOfferedIPAddress & xNetworkAddressing.ulNetMask ) | ~xNetworkAddressing.ulNetMask;
/* Close socket to ensure packets don't queue on it. not needed anymore as DHCP failed. but still need timer for ARP testing. */
vSocketClose( xDHCPData.xDHCPSocket );
xDHCPData.xDHCPSocket = NULL;
xDHCPData.xDHCPTxPeriod = pdMS_TO_TICKS( 3000ul + ( ipconfigRAND32() & 0x3fful ) ); /* do ARP test every (3 + 0-1024mS) seconds. */
xARPHadIPClash = pdFALSE; /* reset flag that shows if have ARP clash. */
vARPSendGratuitous();
}
#endif /* ipconfigDHCP_FALL_BACK_AUTO_IP */
/*-----------------------------------------------------------*/
#endif /* ipconfigUSE_DHCP != 0 */
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "list.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_DNS.h"
#include "NetworkBufferManagement.h"
#include "NetworkInterface.h"
#include "IPTraceMacroDefaults.h"
/* Exclude the entire file if DNS is not enabled. */
#if( ipconfigUSE_DNS != 0 )
#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
#define dnsDNS_PORT 0x3500
#define dnsONE_QUESTION 0x0100
#define dnsOUTGOING_FLAGS 0x0001 /* Standard query. */
#define dnsRX_FLAGS_MASK 0x0f80 /* The bits of interest in the flags field of incoming DNS messages. */
#define dnsEXPECTED_RX_FLAGS 0x0080 /* Should be a response, without any errors. */
#else
#define dnsDNS_PORT 0x0035
#define dnsONE_QUESTION 0x0001
#define dnsOUTGOING_FLAGS 0x0100 /* Standard query. */
#define dnsRX_FLAGS_MASK 0x800f /* The bits of interest in the flags field of incoming DNS messages. */
#define dnsEXPECTED_RX_FLAGS 0x8000 /* Should be a response, without any errors. */
#endif /* ipconfigBYTE_ORDER */
/* The maximum number of times a DNS request should be sent out if a response
is not received, before giving up. */
#ifndef ipconfigDNS_REQUEST_ATTEMPTS
#define ipconfigDNS_REQUEST_ATTEMPTS 5
#endif
/* If the top two bits in the first character of a name field are set then the
name field is an offset to the string, rather than the string itself. */
#define dnsNAME_IS_OFFSET ( ( uint8_t ) 0xc0 )
/* NBNS flags. */
#define dnsNBNS_FLAGS_RESPONSE 0x8000
#define dnsNBNS_FLAGS_OPCODE_MASK 0x7800
#define dnsNBNS_FLAGS_OPCODE_QUERY 0x0000
#define dnsNBNS_FLAGS_OPCODE_REGISTRATION 0x2800
/* Host types. */
#define dnsTYPE_A_HOST 0x01
#define dnsCLASS_IN 0x01
/* LLMNR constants. */
#define dnsLLMNR_TTL_VALUE 300000
#define dnsLLMNR_FLAGS_IS_REPONSE 0x8000
/* NBNS constants. */
#define dnsNBNS_TTL_VALUE 3600 /* 1 hour valid */
#define dnsNBNS_TYPE_NET_BIOS 0x0020
#define dnsNBNS_CLASS_IN 0x01
#define dnsNBNS_NAME_FLAGS 0x6000
#define dnsNBNS_ENCODED_NAME_LENGTH 32
/* If the queried NBNS name matches with the device's name,
the query will be responded to with these flags: */
#define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500 )
/* Flag DNS parsing errors in situations where an IPv4 address is the return
type. */
#define dnsPARSE_ERROR 0UL
/*
* Create a socket and bind it to the standard DNS port number. Return the
* the created socket - or NULL if the socket could not be created or bound.
*/
static Socket_t prvCreateDNSSocket( void );
/*
* Create the DNS message in the zero copy buffer passed in the first parameter.
*/
static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier );
/*
* Simple routine that jumps over the NAME field of a resource record.
*/
static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen );
/*
* Process a response packet from a DNS server.
*/
static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, TickType_t xIdentifier );
/*
* Prepare and send a message to a DNS server. 'xReadTimeOut_ms' will be passed as
* zero, in case the user has supplied a call-back function.
*/
static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms );
/*
* The NBNS and the LLMNR protocol share this reply function.
*/
#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength );
#endif
#if( ipconfigUSE_NBNS == 1 )
static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, uint32_t ulIPAddress );
#endif /* ipconfigUSE_NBNS */
#if( ipconfigUSE_DNS_CACHE == 1 )
static uint8_t *prvReadNameField( uint8_t *pucByte, size_t xSourceLen, char *pcName, size_t xLen );
static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, uint32_t ulTTL, BaseType_t xLookUp );
typedef struct xDNS_CACHE_TABLE_ROW
{
uint32_t ulIPAddress; /* The IP address of an ARP cache entry. */
char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /* The name of the host */
uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */
uint32_t ulTimeWhenAddedInSeconds;
} DNSCacheRow_t;
static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];
#endif /* ipconfigUSE_DNS_CACHE == 1 */
#if( ipconfigUSE_LLMNR == 1 )
const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
#endif /* ipconfigUSE_LLMNR == 1 */
/*-----------------------------------------------------------*/
#include "pack_struct_start.h"
struct xDNSMessage
{
uint16_t usIdentifier;
uint16_t usFlags;
uint16_t usQuestions;
uint16_t usAnswers;
uint16_t usAuthorityRRs;
uint16_t usAdditionalRRs;
}
#include "pack_struct_end.h"
typedef struct xDNSMessage DNSMessage_t;
/* A DNS query consists of a header, as described in 'struct xDNSMessage'
It is followed by 1 or more queries, each one consisting of a name and a tail,
with two fields: type and class
*/
#include "pack_struct_start.h"
struct xDNSTail
{
uint16_t usType;
uint16_t usClass;
}
#include "pack_struct_end.h"
typedef struct xDNSTail DNSTail_t;
/* DNS answer record header. */
#include "pack_struct_start.h"
struct xDNSAnswerRecord
{
uint16_t usType;
uint16_t usClass;
uint32_t ulTTL;
uint16_t usDataLength;
}
#include "pack_struct_end.h"
typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
#if( ipconfigUSE_LLMNR == 1 )
#include "pack_struct_start.h"
struct xLLMNRAnswer
{
uint8_t ucNameCode;
uint8_t ucNameOffset; /* The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */
uint16_t usType;
uint16_t usClass;
uint32_t ulTTL;
uint16_t usDataLength;
uint32_t ulIPAddress;
}
#include "pack_struct_end.h"
typedef struct xLLMNRAnswer LLMNRAnswer_t;
#endif /* ipconfigUSE_LLMNR == 1 */
#if( ipconfigUSE_NBNS == 1 )
#include "pack_struct_start.h"
struct xNBNSRequest
{
uint16_t usRequestId;
uint16_t usFlags;
uint16_t ulRequestCount;
uint16_t usAnswerRSS;
uint16_t usAuthRSS;
uint16_t usAdditionalRSS;
uint8_t ucNameSpace;
uint8_t ucName[ dnsNBNS_ENCODED_NAME_LENGTH ];
uint8_t ucNameZero;
uint16_t usType;
uint16_t usClass;
}
#include "pack_struct_end.h"
typedef struct xNBNSRequest NBNSRequest_t;
#include "pack_struct_start.h"
struct xNBNSAnswer
{
uint16_t usType;
uint16_t usClass;
uint32_t ulTTL;
uint16_t usDataLength;
uint16_t usNbFlags; /* NetBIOS flags 0x6000 : IP-address, big-endian */
uint32_t ulIPAddress;
}
#include "pack_struct_end.h"
typedef struct xNBNSAnswer NBNSAnswer_t;
#endif /* ipconfigUSE_NBNS == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_DNS_CACHE == 1 )
uint32_t FreeRTOS_dnslookup( const char *pcHostName )
{
uint32_t ulIPAddress = 0UL;
prvProcessDNSCache( pcHostName, &ulIPAddress, 0, pdTRUE );
return ulIPAddress;
}
#endif /* ipconfigUSE_DNS_CACHE == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigDNS_USE_CALLBACKS != 0 )
typedef struct xDNS_Callback {
TickType_t xRemaningTime; /* Timeout in ms */
FOnDNSEvent pCallbackFunction; /* Function to be called when the address has been found or when a timeout has beeen reached */
TimeOut_t xTimeoutState;
void *pvSearchID;
struct xLIST_ITEM xListItem;
char pcName[ 1 ];
} DNSCallback_t;
static List_t xCallbackList;
/* Define FreeRTOS_gethostbyname() as a normal blocking call. */
uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
{
return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void* )NULL, 0 );
}
/*-----------------------------------------------------------*/
/* Initialise the list of call-back structures. */
void vDNSInitialise( void );
void vDNSInitialise( void )
{
vListInitialise( &xCallbackList );
}
/*-----------------------------------------------------------*/
/* Iterate through the list of call-back structures and remove
old entries which have reached a timeout.
As soon as the list hase become empty, the DNS timer will be stopped
In case pvSearchID is supplied, the user wants to cancel a DNS request
*/
void vDNSCheckCallBack( void *pvSearchID );
void vDNSCheckCallBack( void *pvSearchID )
{
const ListItem_t *pxIterator;
const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
vTaskSuspendAll();
{
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
pxIterator != ( const ListItem_t * ) xEnd;
)
{
DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
/* Move to the next item because we might remove this item */
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )
{
uxListRemove( &pxCallback->xListItem );
vPortFree( pxCallback );
}
else if( xTaskCheckForTimeOut( &pxCallback->xTimeoutState, &pxCallback->xRemaningTime ) != pdFALSE )
{
pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );
uxListRemove( &pxCallback->xListItem );
vPortFree( ( void * ) pxCallback );
}
}
}
xTaskResumeAll();
if( listLIST_IS_EMPTY( &xCallbackList ) )
{
vIPSetDnsTimerEnableState( pdFALSE );
}
}
/*-----------------------------------------------------------*/
void FreeRTOS_gethostbyname_cancel( void *pvSearchID )
{
/* _HT_ Should better become a new API call to have the IP-task remove the callback */
vDNSCheckCallBack( pvSearchID );
}
/*-----------------------------------------------------------*/
/* FreeRTOS_gethostbyname_a() was called along with callback parameters.
Store them in a list for later reference. */
static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier );
static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier )
{
size_t lLength = strlen( pcHostName );
DNSCallback_t *pxCallback = ( DNSCallback_t * )pvPortMalloc( sizeof( *pxCallback ) + lLength );
/* Translate from ms to number of clock ticks. */
xTimeout /= portTICK_PERIOD_MS;
if( pxCallback != NULL )
{
if( listLIST_IS_EMPTY( &xCallbackList ) )
{
/* This is the first one, start the DNS timer to check for timeouts */
vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, xTimeout ) );
}
strcpy( pxCallback->pcName, pcHostName );
pxCallback->pCallbackFunction = pCallbackFunction;
pxCallback->pvSearchID = pvSearchID;
pxCallback->xRemaningTime = xTimeout;
vTaskSetTimeOutState( &pxCallback->xTimeoutState );
listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void* ) pxCallback );
listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), xIdentifier );
vTaskSuspendAll();
{
vListInsertEnd( &xCallbackList, &pxCallback->xListItem );
}
xTaskResumeAll();
}
}
/*-----------------------------------------------------------*/
/* A DNS reply was received, see if there is any matching entry and
call the handler. */
static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress );
static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress )
{
const ListItem_t *pxIterator;
const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
vTaskSuspendAll();
{
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
pxIterator != ( const ListItem_t * ) xEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
if( listGET_LIST_ITEM_VALUE( pxIterator ) == xIdentifier )
{
DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
pxCallback->pCallbackFunction( pcName, pxCallback->pvSearchID, ulIPAddress );
uxListRemove( &pxCallback->xListItem );
vPortFree( pxCallback );
if( listLIST_IS_EMPTY( &xCallbackList ) )
{
vIPSetDnsTimerEnableState( pdFALSE );
}
break;
}
}
}
xTaskResumeAll();
}
#endif /* ipconfigDNS_USE_CALLBACKS != 0 */
/*-----------------------------------------------------------*/
#if( ipconfigDNS_USE_CALLBACKS == 0 )
uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
#else
uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName, FOnDNSEvent pCallback, void *pvSearchID, TickType_t xTimeout )
#endif
{
uint32_t ulIPAddress = 0UL;
TickType_t xReadTimeOut_ms = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME;
TickType_t xIdentifier = 0;
/* If the supplied hostname is IP address, convert it to uint32_t
and return. */
#if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
{
ulIPAddress = FreeRTOS_inet_addr( pcHostName );
}
#endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */
/* If a DNS cache is used then check the cache before issuing another DNS
request. */
#if( ipconfigUSE_DNS_CACHE == 1 )
{
if( ulIPAddress == 0UL )
{
ulIPAddress = FreeRTOS_dnslookup( pcHostName );
if( ulIPAddress != 0 )
{
FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );
}
else
{
/* prvGetHostByName will be called to start a DNS lookup */
}
}
}
#endif /* ipconfigUSE_DNS_CACHE == 1 */
/* Generate a unique identifier. */
if( 0 == ulIPAddress )
{
xIdentifier = ( TickType_t )ipconfigRAND32( );
}
#if( ipconfigDNS_USE_CALLBACKS != 0 )
{
if( pCallback != NULL )
{
if( ulIPAddress == 0UL )
{
/* The user has provided a callback function, so do not block on recvfrom() */
if( 0 != xIdentifier )
{
xReadTimeOut_ms = 0;
vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t )xIdentifier );
}
}
else
{
/* The IP address is known, do the call-back now. */
pCallback( pcHostName, pvSearchID, ulIPAddress );
}
}
}
#endif
if( ( ulIPAddress == 0UL ) && ( 0 != xIdentifier ) )
{
ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );
}
return ulIPAddress;
}
/*-----------------------------------------------------------*/
static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms )
{
struct freertos_sockaddr xAddress;
Socket_t xDNSSocket;
uint32_t ulIPAddress = 0UL;
uint8_t *pucUDPPayloadBuffer;
uint32_t ulAddressLength = sizeof( struct freertos_sockaddr );
BaseType_t xAttempt;
int32_t lBytes;
size_t xPayloadLength, xExpectedPayloadLength;
TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
#if( ipconfigUSE_LLMNR == 1 )
BaseType_t bHasDot = pdFALSE;
#endif /* ipconfigUSE_LLMNR == 1 */
/* If LLMNR is being used then determine if the host name includes a '.' -
if not then LLMNR can be used as the lookup method. */
#if( ipconfigUSE_LLMNR == 1 )
{
const char *pucPtr;
for( pucPtr = pcHostName; *pucPtr; pucPtr++ )
{
if( *pucPtr == '.' )
{
bHasDot = pdTRUE;
break;
}
}
}
#endif /* ipconfigUSE_LLMNR == 1 */
/* Two is added at the end for the count of characters in the first
subdomain part and the string end byte. */
xExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2u;
xDNSSocket = prvCreateDNSSocket();
if( xDNSSocket != NULL )
{
FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xWriteTimeOut_ms, sizeof( TickType_t ) );
FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xReadTimeOut_ms, sizeof( TickType_t ) );
for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )
{
/* Get a buffer. This uses a maximum delay, but the delay will be
capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value
still needs to be tested. */
pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xExpectedPayloadLength, portMAX_DELAY );
if( pucUDPPayloadBuffer != NULL )
{
/* Create the message in the obtained buffer. */
xPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, xIdentifier );
iptraceSENDING_DNS_REQUEST();
/* Obtain the DNS server address. */
FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );
/* Send the DNS message. */
#if( ipconfigUSE_LLMNR == 1 )
if( bHasDot == pdFALSE )
{
/* Use LLMNR addressing. */
( ( DNSMessage_t * ) pucUDPPayloadBuffer) -> usFlags = 0;
xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */
xAddress.sin_port = FreeRTOS_ntohs( ipLLMNR_PORT );
}
else
#endif
{
/* Use DNS server. */
xAddress.sin_addr = ulIPAddress;
xAddress.sin_port = dnsDNS_PORT;
}
ulIPAddress = 0UL;
if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, xPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )
{
/* Wait for the reply. */
lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );
if( lBytes > 0 )
{
/* The reply was received. Process it. */
ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, lBytes, xIdentifier );
/* Finished with the buffer. The zero copy interface
is being used, so the buffer must be freed by the
task. */
FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
if( ulIPAddress != 0UL )
{
/* All done. */
break;
}
}
}
else
{
/* The message was not sent so the stack will not be
releasing the zero copy - it must be released here. */
FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
}
}
}
/* Finished with the socket. */
FreeRTOS_closesocket( xDNSSocket );
}
return ulIPAddress;
}
/*-----------------------------------------------------------*/
static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier )
{
DNSMessage_t *pxDNSMessageHeader;
uint8_t *pucStart, *pucByte;
DNSTail_t *pxTail;
static const DNSMessage_t xDefaultPartDNSHeader =
{
0, /* The identifier will be overwritten. */
dnsOUTGOING_FLAGS, /* Flags set for standard query. */
dnsONE_QUESTION, /* One question is being asked. */
0, /* No replies are included. */
0, /* No authorities. */
0 /* No additional authorities. */
};
/* Copy in the const part of the header. */
memcpy( ( void * ) pucUDPPayloadBuffer, ( void * ) &xDefaultPartDNSHeader, sizeof( xDefaultPartDNSHeader ) );
/* Write in a unique identifier. */
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
pxDNSMessageHeader->usIdentifier = ( uint16_t ) xIdentifier;
/* Create the resource record at the end of the header. First
find the end of the header. */
pucStart = pucUDPPayloadBuffer + sizeof( xDefaultPartDNSHeader );
/* Leave a gap for the first length bytes. */
pucByte = pucStart + 1;
/* Copy in the host name. */
strcpy( ( char * ) pucByte, pcHostName );
/* Mark the end of the string. */
pucByte += strlen( pcHostName );
*pucByte = 0x00u;
/* Walk the string to replace the '.' characters with byte counts.
pucStart holds the address of the byte count. Walking the string
starts after the byte count position. */
pucByte = pucStart;
do
{
pucByte++;
while( ( *pucByte != 0x00 ) && ( *pucByte != '.' ) )
{
pucByte++;
}
/* Fill in the byte count, then move the pucStart pointer up to
the found byte position. */
*pucStart = ( uint8_t ) ( ( uint32_t ) pucByte - ( uint32_t ) pucStart );
( *pucStart )--;
pucStart = pucByte;
} while( *pucByte != 0x00 );
/* Finish off the record. */
pxTail = (DNSTail_t *)( pucByte + 1 );
vSetField16( pxTail, DNSTail_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
vSetField16( pxTail, DNSTail_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
/* Return the total size of the generated message, which is the space from
the last written byte to the beginning of the buffer. */
return ( ( uint32_t ) pucByte - ( uint32_t ) pucUDPPayloadBuffer + 1 ) + sizeof( *pxTail );
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_DNS_CACHE == 1 )
static uint8_t *prvReadNameField( uint8_t *pucByte, size_t xSourceLen, char *pcName, size_t xDestLen )
{
size_t xNameLen = 0;
BaseType_t xCount;
if( 0 == xSourceLen )
{
return NULL;
}
/* Determine if the name is the fully coded name, or an offset to the name
elsewhere in the message. */
if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
if( xSourceLen > sizeof( uint16_t ) )
{
pucByte += sizeof( uint16_t );
}
else
{
pucByte = NULL;
}
}
else
{
/* pucByte points to the full name. Walk over the string. */
while( ( NULL != pucByte ) && ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) )
{
/* If this is not the first time through the loop, then add a
separator in the output. */
if( ( xNameLen > 0 ) && ( xNameLen < ( xDestLen - 1 ) ) )
{
pcName[ xNameLen++ ] = '.';
}
/* Process the first/next sub-string. */
for( xCount = *(pucByte++), xSourceLen--;
xCount-- && xSourceLen > 1;
pucByte++, xSourceLen-- )
{
if( xNameLen < xDestLen - 1 )
{
pcName[ xNameLen++ ] = *( ( char * )pucByte );
}
else
{
/* DNS name is too big for the provided buffer. */
pucByte = NULL;
break;
}
}
}
/* Confirm that a fully formed name was found. */
if( NULL != pucByte )
{
if( 0x00 == *pucByte )
{
pucByte++;
xSourceLen--;
pcName[ xNameLen++ ] = '\0';
}
else
{
pucByte = NULL;
}
}
}
return pucByte;
}
#endif /* ipconfigUSE_DNS_CACHE == 1 */
/*-----------------------------------------------------------*/
static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )
{
size_t xChunkLength;
if( 0 == xSourceLen )
{
return NULL;
}
/* Determine if the name is the fully coded name, or an offset to the name
elsewhere in the message. */
if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
if( xSourceLen > sizeof( uint16_t ) )
{
pucByte += sizeof( uint16_t );
}
else
{
pucByte = NULL;
}
}
else
{
/* pucByte points to the full name. Walk over the string. */
while( ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) )
{
xChunkLength = *pucByte + 1;
if( xSourceLen > xChunkLength )
{
xSourceLen -= xChunkLength;
pucByte += xChunkLength;
}
else
{
pucByte = NULL;
break;
}
}
/* Confirm that a fully formed name was found. */
if( NULL != pucByte )
{
if( 0x00 == *pucByte )
{
pucByte++;
}
else
{
pucByte = NULL;
}
}
}
return pucByte;
}
/*-----------------------------------------------------------*/
uint32_t ulDNSHandlePacket( NetworkBufferDescriptor_t *pxNetworkBuffer )
{
uint8_t *pucUDPPayloadBuffer;
size_t xPlayloadBufferLength;
DNSMessage_t *pxDNSMessageHeader;
xPlayloadBufferLength = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );
if ( xPlayloadBufferLength < sizeof( DNSMessage_t ) )
{
return pdFAIL;
}
pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t ) )
{
prvParseDNSReply( pucUDPPayloadBuffer,
xPlayloadBufferLength,
( uint32_t )pxDNSMessageHeader->usIdentifier );
}
/* The packet was not consumed. */
return pdFAIL;
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_NBNS == 1 )
uint32_t ulNBNSHandlePacket (NetworkBufferDescriptor_t *pxNetworkBuffer )
{
UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
if( pxNetworkBuffer->xDataLength > sizeof( UDPPacket_t) )
{
prvTreatNBNS( pucUDPPayloadBuffer,
pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t ),
pxUDPPacket->xIPHeader.ulSourceIPAddress );
}
/* The packet was not consumed. */
return pdFAIL;
}
#endif /* ipconfigUSE_NBNS */
/*-----------------------------------------------------------*/
static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, TickType_t xIdentifier )
{
DNSMessage_t *pxDNSMessageHeader;
DNSAnswerRecord_t *pxDNSAnswerRecord;
uint32_t ulIPAddress = 0UL;
#if( ipconfigUSE_LLMNR == 1 )
char *pcRequestedName = NULL;
#endif
uint8_t *pucByte;
size_t xSourceBytesRemaining;
uint16_t x, usDataLength, usQuestions;
#if( ipconfigUSE_LLMNR == 1 )
uint16_t usType = 0, usClass = 0;
#endif
#if( ipconfigUSE_DNS_CACHE == 1 )
char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = "";
#endif
/* Ensure that the buffer is of at least minimal DNS message length. */
if( xBufferLength < sizeof( DNSMessage_t ) )
{
return dnsPARSE_ERROR;
}
else
{
xSourceBytesRemaining = xBufferLength;
}
/* Parse the DNS message header. */
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier )
{
/* Start at the first byte after the header. */
pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );
xSourceBytesRemaining -= sizeof( DNSMessage_t );
/* Skip any question records. */
usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );
for( x = 0; x < usQuestions; x++ )
{
#if( ipconfigUSE_LLMNR == 1 )
{
if( x == 0 )
{
pcRequestedName = ( char * ) pucByte;
}
}
#endif
#if( ipconfigUSE_DNS_CACHE == 1 )
if( x == 0 )
{
pucByte = prvReadNameField( pucByte,
xSourceBytesRemaining,
pcName,
sizeof( pcName ) );
/* Check for a malformed response. */
if( NULL == pucByte )
{
return dnsPARSE_ERROR;
}
else
{
xSourceBytesRemaining = ( pucUDPPayloadBuffer + xBufferLength ) - pucByte;
}
}
else
#endif /* ipconfigUSE_DNS_CACHE */
{
/* Skip the variable length pcName field. */
pucByte = prvSkipNameField( pucByte,
xSourceBytesRemaining );
/* Check for a malformed response. */
if( NULL == pucByte )
{
return dnsPARSE_ERROR;
}
else
{
xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;
}
}
/* Check the remaining buffer size. */
if( xSourceBytesRemaining >= sizeof( uint32_t ) )
{
#if( ipconfigUSE_LLMNR == 1 )
{
/* usChar2u16 returns value in host endianness */
usType = usChar2u16( pucByte );
usClass = usChar2u16( pucByte + 2 );
}
#endif /* ipconfigUSE_LLMNR */
/* Skip the type and class fields. */
pucByte += sizeof( uint32_t );
xSourceBytesRemaining -= sizeof( uint32_t );
}
else
{
/* Malformed response. */
return dnsPARSE_ERROR;
}
}
/* Search through the answer records. */
pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );
if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )
{
for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )
{
pucByte = prvSkipNameField( pucByte,
xSourceBytesRemaining );
/* Check for a malformed response. */
if( NULL == pucByte )
{
return dnsPARSE_ERROR;
}
else
{
xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;
}
/* Is there enough data for an IPv4 A record answer and, if so,
is this an A record? */
if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) &&
usChar2u16( pucByte ) == dnsTYPE_A_HOST )
{
/* This is the required record type and is of sufficient size. */
pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte;
/* Sanity check the data length of an IPv4 answer. */
if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == sizeof( uint32_t ) )
{
/* Copy the IP address out of the record. */
memcpy( &ulIPAddress,
pucByte + sizeof( DNSAnswerRecord_t ),
sizeof( uint32_t ) );
#if( ipconfigUSE_DNS_CACHE == 1 )
{
prvProcessDNSCache( pcName, &ulIPAddress, pxDNSAnswerRecord->ulTTL, pdFALSE );
}
#endif /* ipconfigUSE_DNS_CACHE */
#if( ipconfigDNS_USE_CALLBACKS != 0 )
{
/* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */
vDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress );
}
#endif /* ipconfigDNS_USE_CALLBACKS != 0 */
}
pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t );
xSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );
break;
}
else if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) )
{
/* It's not an A record, so skip it. Get the header location
and then jump over the header. */
pxDNSAnswerRecord = ( DNSAnswerRecord_t * )pucByte;
pucByte += sizeof( DNSAnswerRecord_t );
xSourceBytesRemaining -= sizeof( DNSAnswerRecord_t );
/* Determine the length of the answer data from the header. */
usDataLength = FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength );
/* Jump over the answer. */
if( xSourceBytesRemaining >= usDataLength )
{
pucByte += usDataLength;
xSourceBytesRemaining -= usDataLength;
}
else
{
/* Malformed response. */
return dnsPARSE_ERROR;
}
}
}
}
#if( ipconfigUSE_LLMNR == 1 )
else if( usQuestions && ( usType == dnsTYPE_A_HOST ) && ( usClass == dnsCLASS_IN ) )
{
/* If this is not a reply to our DNS request, it might an LLMNR
request. */
if( xApplicationDNSQueryHook ( ( pcRequestedName + 1 ) ) )
{
int16_t usLength;
NetworkBufferDescriptor_t *pxNewBuffer = NULL;
NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
LLMNRAnswer_t *pxAnswer;
if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
{
BaseType_t xDataLength = xBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
/* The field xDataLength was set to the length of the UDP payload.
The answer (reply) will be longer than the request, so the packet
must be duplicaed into a bigger buffer */
pxNetworkBuffer->xDataLength = xDataLength;
pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );
if( pxNewBuffer != NULL )
{
BaseType_t xOffset1, xOffset2;
xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );
xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );
pxNetworkBuffer = pxNewBuffer;
pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + ipUDP_PAYLOAD_OFFSET_IPv4;
pucByte = pucUDPPayloadBuffer + xOffset1;
pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
}
else
{
/* Just to indicate that the message may not be answered. */
pxNetworkBuffer = NULL;
}
}
if( pxNetworkBuffer != NULL )
{
pxAnswer = (LLMNRAnswer_t *)pucByte;
/* We leave 'usIdentifier' and 'usQuestions' untouched */
vSetField16( pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_REPONSE ); /* Set the response flag */
vSetField16( pxDNSMessageHeader, DNSMessage_t, usAnswers, 1 ); /* Provide a single answer */
vSetField16( pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */
vSetField16( pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 ); /* No additional info */
pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;
pxAnswer->ucNameOffset = ( uint8_t )( pcRequestedName - ( char * ) pucUDPPayloadBuffer );
vSetField16( pxAnswer, LLMNRAnswer_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
vSetField16( pxAnswer, LLMNRAnswer_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
vSetField32( pxAnswer, LLMNRAnswer_t, ulTTL, dnsLLMNR_TTL_VALUE );
vSetField16( pxAnswer, LLMNRAnswer_t, usDataLength, 4 );
vSetField32( pxAnswer, LLMNRAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucUDPPayloadBuffer ) );
prvReplyDNSMessage( pxNetworkBuffer, usLength );
if( pxNewBuffer != NULL )
{
vReleaseNetworkBufferAndDescriptor( pxNewBuffer );
}
}
}
}
#endif /* ipconfigUSE_LLMNR == 1 */
}
return ulIPAddress;
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_NBNS == 1 )
static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, uint32_t ulIPAddress )
{
uint16_t usFlags, usType, usClass;
uint8_t *pucSource, *pucTarget;
uint8_t ucByte;
uint8_t ucNBNSName[ 17 ];
/* Check for minimum buffer size. */
if( xBufferLength < sizeof( NBNSRequest_t ) )
{
return;
}
/* Read the request flags in host endianness. */
usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );
if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )
{
usType = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );
/* Not used for now */
( void )usClass;
/* For NBNS a name is 16 bytes long, written with capitals only.
Make sure that the copy is terminated with a zero. */
pucTarget = ucNBNSName + sizeof(ucNBNSName ) - 2;
pucTarget[ 1 ] = '\0';
/* Start with decoding the last 2 bytes. */
pucSource = pucUDPPayloadBuffer + ( offsetof( NBNSRequest_t, ucName ) + ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) );
for( ;; )
{
ucByte = ( uint8_t ) ( ( ( pucSource[ 0 ] - 0x41 ) << 4 ) | ( pucSource[ 1 ] - 0x41 ) );
/* Make sure there are no trailing spaces in the name. */
if( ( ucByte == ' ' ) && ( pucTarget[ 1 ] == '\0' ) )
{
ucByte = '\0';
}
*pucTarget = ucByte;
if( pucTarget == ucNBNSName )
{
break;
}
pucTarget -= 1;
pucSource -= 2;
}
#if( ipconfigUSE_DNS_CACHE == 1 )
{
if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0 )
{
/* If this is a response from another device,
add the name to the DNS cache */
prvProcessDNSCache( ( char * ) ucNBNSName, &ulIPAddress, 0, pdFALSE );
}
}
#else
{
/* Avoid compiler warnings. */
( void ) ulIPAddress;
}
#endif /* ipconfigUSE_DNS_CACHE */
if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0 ) &&
( usType == dnsNBNS_TYPE_NET_BIOS ) &&
( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )
{
uint16_t usLength;
DNSMessage_t *pxMessage;
NBNSAnswer_t *pxAnswer;
/* Someone is looking for a device with ucNBNSName,
prepare a positive reply. */
NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
{
NetworkBufferDescriptor_t *pxNewBuffer;
BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +
sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
/* The field xDataLength was set to the length of the UDP payload.
The answer (reply) will be longer than the request, so the packet
must be duplicated into a bigger buffer */
pxNetworkBuffer->xDataLength = xDataLength;
pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );
if( pxNewBuffer != NULL )
{
pucUDPPayloadBuffer = pxNewBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
pxNetworkBuffer = pxNewBuffer;
}
else
{
/* Just prevent that a reply will be sent */
pxNetworkBuffer = NULL;
}
}
/* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */
if( pxNetworkBuffer != NULL )
{
pxMessage = (DNSMessage_t *)pucUDPPayloadBuffer;
/* As the fields in the structures are not word-aligned, we have to
copy the values byte-by-byte using macro's vSetField16() and vSetField32() */
vSetField16( pxMessage, DNSMessage_t, usFlags, dnsNBNS_QUERY_RESPONSE_FLAGS ); /* 0x8500 */
vSetField16( pxMessage, DNSMessage_t, usQuestions, 0 );
vSetField16( pxMessage, DNSMessage_t, usAnswers, 1 );
vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );
vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );
pxAnswer = (NBNSAnswer_t *)( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
vSetField16( pxAnswer, NBNSAnswer_t, usType, usType ); /* Type */
vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN ); /* Class */
vSetField32( pxAnswer, NBNSAnswer_t, ulTTL, dnsNBNS_TTL_VALUE );
vSetField16( pxAnswer, NBNSAnswer_t, usDataLength, 6 ); /* 6 bytes including the length field */
vSetField16( pxAnswer, NBNSAnswer_t, usNbFlags, dnsNBNS_NAME_FLAGS );
vSetField32( pxAnswer, NBNSAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
usLength = ( uint16_t ) ( offsetof( NBNSRequest_t, usType ) + sizeof( NBNSAnswer_t ) );
prvReplyDNSMessage( pxNetworkBuffer, usLength );
}
}
}
}
#endif /* ipconfigUSE_NBNS */
/*-----------------------------------------------------------*/
static Socket_t prvCreateDNSSocket( void )
{
Socket_t xSocket = NULL;
struct freertos_sockaddr xAddress;
BaseType_t xReturn;
TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
/* This must be the first time this function has been called. Create
the socket. */
xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
/* Auto bind the port. */
xAddress.sin_port = 0u;
xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );
/* Check the bind was successful, and clean up if not. */
if( xReturn != 0 )
{
FreeRTOS_closesocket( xSocket );
xSocket = NULL;
}
else
{
/* Set the send and receive timeouts. */
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
}
return xSocket;
}
/*-----------------------------------------------------------*/
#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength )
{
UDPPacket_t *pxUDPPacket;
IPHeader_t *pxIPHeader;
UDPHeader_t *pxUDPHeader;
pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;
pxIPHeader = &pxUDPPacket->xIPHeader;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
/* HT: started using defines like 'ipSIZE_OF_xxx' */
pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );
/* HT:endian: should not be translated, copying from packet to packet */
pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;
pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );
usPacketIdentifier++;
pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );
vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );
#if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
{
/* calculate the IP header checksum */
pxIPHeader->usHeaderChecksum = 0x00;
pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
/* calculate the UDP checksum for outgoing package */
usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, lNetLength, pdTRUE );
}
#endif
/* Important: tell NIC driver how many bytes must be sent */
pxNetworkBuffer->xDataLength = ( size_t ) ( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER );
/* This function will fill in the eth addresses and send the packet */
vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
}
#endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_DNS_CACHE == 1 )
static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, uint32_t ulTTL, BaseType_t xLookUp )
{
BaseType_t x;
BaseType_t xFound = pdFALSE;
uint32_t ulCurrentTimeSeconds = ( xTaskGetTickCount() / portTICK_PERIOD_MS ) / 1000;
static BaseType_t xFreeEntry = 0;
/* For each entry in the DNS cache table. */
for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )
{
if( xDNSCache[ x ].pcName[ 0 ] == 0 )
{
break;
}
if( 0 == strcmp( xDNSCache[ x ].pcName, pcName ) )
{
/* Is this function called for a lookup or to add/update an IP address? */
if( xLookUp != pdFALSE )
{
/* Confirm that the record is still fresh. */
if( ulCurrentTimeSeconds < ( xDNSCache[ x ].ulTimeWhenAddedInSeconds + FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) )
{
*pulIP = xDNSCache[ x ].ulIPAddress;
}
else
{
/* Age out the old cached record. */
xDNSCache[ x ].pcName[ 0 ] = 0;
}
}
else
{
xDNSCache[ x ].ulIPAddress = *pulIP;
xDNSCache[ x ].ulTTL = ulTTL;
xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
}
xFound = pdTRUE;
break;
}
}
if( xFound == pdFALSE )
{
if( xLookUp != pdFALSE )
{
*pulIP = 0;
}
else
{
/* Add or update the item. */
if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH )
{
strcpy( xDNSCache[ xFreeEntry ].pcName, pcName );
xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;
xDNSCache[ xFreeEntry ].ulTTL = ulTTL;
xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
xFreeEntry++;
if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )
{
xFreeEntry = 0;
}
}
}
}
if( ( xLookUp == 0 ) || ( *pulIP != 0 ) )
{
FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", xLookUp ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );
}
}
#endif /* ipconfigUSE_DNS_CACHE */
#endif /* ipconfigUSE_DNS != 0 */
/*-----------------------------------------------------------*/
/* Provide access to private members for testing. */
#ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS
#include "aws_freertos_tcp_test_access_dns_define.h"
#endif
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_ARP.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_TCP_IP.h"
#include "FreeRTOS_DHCP.h"
#include "NetworkInterface.h"
#include "NetworkBufferManagement.h"
#include "FreeRTOS_DNS.h"
/* Used to ensure the structure packing is having the desired effect. The
'volatile' is used to prevent compiler warnings about comparing a constant with
a constant. */
#define ipEXPECTED_EthernetHeader_t_SIZE ( ( size_t ) 14 )
#define ipEXPECTED_ARPHeader_t_SIZE ( ( size_t ) 28 )
#define ipEXPECTED_IPHeader_t_SIZE ( ( size_t ) 20 )
#define ipEXPECTED_IGMPHeader__SIZE ( ( size_t ) 8 )
#define ipEXPECTED_ICMPHeader_t_SIZE ( ( size_t ) 8 )
#define ipEXPECTED_UDPHeader_t_SIZE ( ( size_t ) 8 )
#define ipEXPECTED_TCPHeader_t_SIZE ( ( size_t ) 20 )
/* ICMP protocol definitions. */
#define ipICMP_ECHO_REQUEST ( ( uint8_t ) 8 )
#define ipICMP_ECHO_REPLY ( ( uint8_t ) 0 )
/* Time delay between repeated attempts to initialise the network hardware. */
#define ipINITIALISATION_RETRY_DELAY ( pdMS_TO_TICKS( 3000 ) )
/* Defines how often the ARP timer callback function is executed. The time is
shorted in the Windows simulator as simulated time is not real time. */
#ifndef ipARP_TIMER_PERIOD_MS
#ifdef _WINDOWS_
#define ipARP_TIMER_PERIOD_MS ( 500 ) /* For windows simulator builds. */
#else
#define ipARP_TIMER_PERIOD_MS ( 10000 )
#endif
#endif
#ifndef iptraceIP_TASK_STARTING
#define iptraceIP_TASK_STARTING() do {} while( 0 )
#endif
#if( ( ipconfigUSE_TCP == 1 ) && !defined( ipTCP_TIMER_PERIOD_MS ) )
/* When initialising the TCP timer,
give it an initial time-out of 1 second. */
#define ipTCP_TIMER_PERIOD_MS ( 1000 )
#endif
/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
driver will filter incoming packets and only pass the stack those packets it
considers need processing. In this case ipCONSIDER_FRAME_FOR_PROCESSING() can
be #defined away. If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 0
then the Ethernet driver will pass all received packets to the stack, and the
stack must do the filtering itself. In this case ipCONSIDER_FRAME_FOR_PROCESSING
needs to call eConsiderFrameForProcessing. */
#if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
#else
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
#endif
/* The character used to fill ICMP echo requests, and therefore also the
character expected to fill ICMP echo replies. */
#define ipECHO_DATA_FILL_BYTE 'x'
#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
/* The bits in the two byte IP header field that make up the fragment offset value. */
#define ipFRAGMENT_OFFSET_BIT_MASK ( ( uint16_t ) 0xff0f )
#else
/* The bits in the two byte IP header field that make up the fragment offset value. */
#define ipFRAGMENT_OFFSET_BIT_MASK ( ( uint16_t ) 0x0fff )
#endif /* ipconfigBYTE_ORDER */
/* The maximum time the IP task is allowed to remain in the Blocked state if no
events are posted to the network event queue. */
#ifndef ipconfigMAX_IP_TASK_SLEEP_TIME
#define ipconfigMAX_IP_TASK_SLEEP_TIME ( pdMS_TO_TICKS( 10000UL ) )
#endif
/* When a new TCP connection is established, the value of
'ulNextInitialSequenceNumber' will be used as the initial sequence number. It
is very important that at start-up, 'ulNextInitialSequenceNumber' contains a
random value. Also its value must be increased continuously in time, to prevent
a third party guessing the next sequence number and take-over a TCP connection.
It is advised to increment it by 1 ever 4us, which makes about 256 times
per ms: */
#define ipINITIAL_SEQUENCE_NUMBER_FACTOR 256UL
/* Returned as the (invalid) checksum when the protocol being checked is not
handled. The value is chosen simply to be easy to spot when debugging. */
#define ipUNHANDLED_PROTOCOL 0x4321u
/* Returned to indicate a valid checksum when the checksum does not need to be
calculated. */
#define ipCORRECT_CRC 0xffffu
/* Returned as the (invalid) checksum when the length of the data being checked
had an invalid length. */
#define ipINVALID_LENGTH 0x1234u
/*-----------------------------------------------------------*/
typedef struct xIP_TIMER
{
uint32_t
bActive : 1, /* This timer is running and must be processed. */
bExpired : 1; /* Timer has expired and a task must be processed. */
TimeOut_t xTimeOut;
TickType_t ulRemainingTime;
TickType_t ulReloadTime;
} IPTimer_t;
/* Used in checksum calculation. */
typedef union _xUnion32
{
uint32_t u32;
uint16_t u16[ 2 ];
uint8_t u8[ 4 ];
} xUnion32;
/* Used in checksum calculation. */
typedef union _xUnionPtr
{
uint32_t *u32ptr;
uint16_t *u16ptr;
uint8_t *u8ptr;
} xUnionPtr;
/*-----------------------------------------------------------*/
/*
* The main TCP/IP stack processing task. This task receives commands/events
* from the network hardware drivers and tasks that are using sockets. It also
* maintains a set of protocol timers.
*/
static void prvIPTask( void *pvParameters );
/*
* Called when new data is available from the network interface.
*/
static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer );
/*
* Process incoming IP packets.
*/
static eFrameProcessingResult_t prvProcessIPPacket( IPPacket_t * const pxIPPacket, NetworkBufferDescriptor_t * const pxNetworkBuffer );
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
/*
* Process incoming ICMP packets.
*/
static eFrameProcessingResult_t prvProcessICMPPacket( ICMPPacket_t * const pxICMPPacket );
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */
/*
* Turns around an incoming ping request to convert it into a ping reply.
*/
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 )
static eFrameProcessingResult_t prvProcessICMPEchoRequest( ICMPPacket_t * const pxICMPPacket );
#endif /* ipconfigREPLY_TO_INCOMING_PINGS */
/*
* Processes incoming ping replies. The application callback function
* vApplicationPingReplyHook() is called with the results.
*/
#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
static void prvProcessICMPEchoReply( ICMPPacket_t * const pxICMPPacket );
#endif /* ipconfigSUPPORT_OUTGOING_PINGS */
/*
* Called to create a network connection when the stack is first started, or
* when the network connection is lost.
*/
static void prvProcessNetworkDownEvent( void );
/*
* Checks the ARP, DHCP and TCP timers to see if any periodic or timeout
* processing is required.
*/
static void prvCheckNetworkTimers( void );
/*
* Determine how long the IP task can sleep for, which depends on when the next
* periodic or timeout processing must be performed.
*/
static TickType_t prvCalculateSleepTime( void );
/*
* The network card driver has received a packet. In the case that it is part
* of a linked packet chain, walk through it to handle every message.
*/
static void prvHandleEthernetPacket( NetworkBufferDescriptor_t *pxBuffer );
/*
* Utility functions for the light weight IP timers.
*/
static void prvIPTimerStart( IPTimer_t *pxTimer, TickType_t xTime );
static BaseType_t prvIPTimerCheck( IPTimer_t *pxTimer );
static void prvIPTimerReload( IPTimer_t *pxTimer, TickType_t xTime );
static eFrameProcessingResult_t prvAllowIPPacket( const IPPacket_t * const pxIPPacket,
NetworkBufferDescriptor_t * const pxNetworkBuffer, UBaseType_t uxHeaderLength );
/*-----------------------------------------------------------*/
/* The queue used to pass events into the IP-task for processing. */
QueueHandle_t xNetworkEventQueue = NULL;
/*_RB_ Requires comment. */
uint16_t usPacketIdentifier = 0U;
/* For convenience, a MAC address of all 0xffs is defined const for quick
reference. */
const MACAddress_t xBroadcastMACAddress = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
/* Structure that stores the netmask, gateway address and DNS server addresses. */
NetworkAddressingParameters_t xNetworkAddressing = { 0, 0, 0, 0, 0 };
/* Default values for the above struct in case DHCP
does not lead to a confirmed request. */
NetworkAddressingParameters_t xDefaultAddressing = { 0, 0, 0, 0, 0 };
/* Used to ensure network down events cannot be missed when they cannot be
posted to the network event queue because the network event queue is already
full. */
static BaseType_t xNetworkDownEventPending = pdFALSE;
/* Stores the handle of the task that handles the stack. The handle is used
(indirectly) by some utility function to determine if the utility function is
being called by a task (in which case it is ok to block) or by the IP task
itself (in which case it is not ok to block). */
static TaskHandle_t xIPTaskHandle = NULL;
#if( ipconfigUSE_TCP != 0 )
/* Set to a non-zero value if one or more TCP message have been processed
within the last round. */
static BaseType_t xProcessedTCPMessage;
#endif
/* Simple set to pdTRUE or pdFALSE depending on whether the network is up or
down (connected, not connected) respectively. */
static BaseType_t xNetworkUp = pdFALSE;
/*
A timer for each of the following processes, all of which need attention on a
regular basis:
1. ARP, to check its table entries
2. DPHC, to send requests and to renew a reservation
3. TCP, to check for timeouts, resends
4. DNS, to check for timeouts when looking-up a domain.
*/
static IPTimer_t xARPTimer;
#if( ipconfigUSE_DHCP != 0 )
static IPTimer_t xDHCPTimer;
#endif
#if( ipconfigUSE_TCP != 0 )
static IPTimer_t xTCPTimer;
#endif
#if( ipconfigDNS_USE_CALLBACKS != 0 )
static IPTimer_t xDNSTimer;
#endif
/* Set to pdTRUE when the IP task is ready to start processing packets. */
static BaseType_t xIPTaskInitialised = pdFALSE;
#if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
/* Keep track of the lowest amount of space in 'xNetworkEventQueue'. */
static UBaseType_t uxQueueMinimumSpace = ipconfigEVENT_QUEUE_LENGTH;
#endif
/*-----------------------------------------------------------*/
static void prvIPTask( void *pvParameters )
{
IPStackEvent_t xReceivedEvent;
TickType_t xNextIPSleep;
FreeRTOS_Socket_t *pxSocket;
struct freertos_sockaddr xAddress;
/* Just to prevent compiler warnings about unused parameters. */
( void ) pvParameters;
/* A possibility to set some additional task properties. */
iptraceIP_TASK_STARTING();
/* Generate a dummy message to say that the network connection has gone
down. This will cause this task to initialise the network interface. After
this it is the responsibility of the network interface hardware driver to
send this message if a previously connected network is disconnected. */
FreeRTOS_NetworkDown();
#if( ipconfigUSE_TCP == 1 )
{
/* Initialise the TCP timer. */
prvIPTimerReload( &xTCPTimer, pdMS_TO_TICKS( ipTCP_TIMER_PERIOD_MS ) );
}
#endif
/* Initialisation is complete and events can now be processed. */
xIPTaskInitialised = pdTRUE;
FreeRTOS_debug_printf( ( "prvIPTask started\n" ) );
/* Loop, processing IP events. */
for( ;; )
{
ipconfigWATCHDOG_TIMER();
/* Check the ARP, DHCP and TCP timers to see if there is any periodic
or timeout processing to perform. */
prvCheckNetworkTimers();
/* Calculate the acceptable maximum sleep time. */
xNextIPSleep = prvCalculateSleepTime();
/* Wait until there is something to do. If the following call exits
* due to a time out rather than a message being received, set a
* 'NoEvent' value. */
if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE )
{
xReceivedEvent.eEventType = eNoEvent;
}
#if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
{
if( xReceivedEvent.eEventType != eNoEvent )
{
UBaseType_t uxCount;
uxCount = uxQueueSpacesAvailable( xNetworkEventQueue );
if( uxQueueMinimumSpace > uxCount )
{
uxQueueMinimumSpace = uxCount;
}
}
}
#endif /* ipconfigCHECK_IP_QUEUE_SPACE */
iptraceNETWORK_EVENT_RECEIVED( xReceivedEvent.eEventType );
switch( xReceivedEvent.eEventType )
{
case eNetworkDownEvent :
/* Attempt to establish a connection. */
xNetworkUp = pdFALSE;
prvProcessNetworkDownEvent();
break;
case eNetworkRxEvent:
/* The network hardware driver has received a new packet. A
pointer to the received buffer is located in the pvData member
of the received event structure. */
prvHandleEthernetPacket( ( NetworkBufferDescriptor_t * ) ( xReceivedEvent.pvData ) );
break;
case eARPTimerEvent :
/* The ARP timer has expired, process the ARP cache. */
vARPAgeCache();
break;
case eSocketBindEvent:
/* FreeRTOS_bind (a user API) wants the IP-task to bind a socket
to a port. The port number is communicated in the socket field
usLocalPort. vSocketBind() will actually bind the socket and the
API will unblock as soon as the eSOCKET_BOUND event is
triggered. */
pxSocket = ( FreeRTOS_Socket_t * ) ( xReceivedEvent.pvData );
xAddress.sin_addr = 0u; /* For the moment. */
xAddress.sin_port = FreeRTOS_ntohs( pxSocket->usLocalPort );
pxSocket->usLocalPort = 0u;
vSocketBind( pxSocket, &xAddress, sizeof( xAddress ), pdFALSE );
/* Before 'eSocketBindEvent' was sent it was tested that
( xEventGroup != NULL ) so it can be used now to wake up the
user. */
pxSocket->xEventBits |= eSOCKET_BOUND;
vSocketWakeUpUser( pxSocket );
break;
case eSocketCloseEvent :
/* The user API FreeRTOS_closesocket() has sent a message to the
IP-task to actually close a socket. This is handled in
vSocketClose(). As the socket gets closed, there is no way to
report back to the API, so the API won't wait for the result */
vSocketClose( ( FreeRTOS_Socket_t * ) ( xReceivedEvent.pvData ) );
break;
case eStackTxEvent :
/* The network stack has generated a packet to send. A
pointer to the generated buffer is located in the pvData
member of the received event structure. */
vProcessGeneratedUDPPacket( ( NetworkBufferDescriptor_t * ) ( xReceivedEvent.pvData ) );
break;
case eDHCPEvent:
/* The DHCP state machine needs processing. */
#if( ipconfigUSE_DHCP == 1 )
{
vDHCPProcess( pdFALSE );
}
#endif /* ipconfigUSE_DHCP */
break;
case eSocketSelectEvent :
/* FreeRTOS_select() has got unblocked by a socket event,
vSocketSelect() will check which sockets actually have an event
and update the socket field xSocketBits. */
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
vSocketSelect( ( SocketSelect_t * ) ( xReceivedEvent.pvData ) );
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
break;
case eSocketSignalEvent :
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
/* Some task wants to signal the user of this socket in
order to interrupt a call to recv() or a call to select(). */
FreeRTOS_SignalSocket( ( Socket_t ) xReceivedEvent.pvData );
}
#endif /* ipconfigSUPPORT_SIGNALS */
break;
case eTCPTimerEvent :
#if( ipconfigUSE_TCP == 1 )
{
/* Simply mark the TCP timer as expired so it gets processed
the next time prvCheckNetworkTimers() is called. */
xTCPTimer.bExpired = pdTRUE_UNSIGNED;
}
#endif /* ipconfigUSE_TCP */
break;
case eTCPAcceptEvent:
/* The API FreeRTOS_accept() was called, the IP-task will now
check if the listening socket (communicated in pvData) actually
received a new connection. */
#if( ipconfigUSE_TCP == 1 )
{
pxSocket = ( FreeRTOS_Socket_t * ) ( xReceivedEvent.pvData );
if( xTCPCheckNewClient( pxSocket ) != pdFALSE )
{
pxSocket->xEventBits |= eSOCKET_ACCEPT;
vSocketWakeUpUser( pxSocket );
}
}
#endif /* ipconfigUSE_TCP */
break;
case eTCPNetStat:
/* FreeRTOS_netstat() was called to have the IP-task print an
overview of all sockets and their connections */
#if( ( ipconfigUSE_TCP == 1 ) && ( ipconfigHAS_PRINTF == 1 ) )
{
vTCPNetStat();
}
#endif /* ipconfigUSE_TCP */
break;
default :
/* Should not get here. */
break;
}
if( xNetworkDownEventPending != pdFALSE )
{
/* A network down event could not be posted to the network event
queue because the queue was full. Try posting again. */
FreeRTOS_NetworkDown();
}
}
}
/*-----------------------------------------------------------*/
BaseType_t xIsCallingFromIPTask( void )
{
BaseType_t xReturn;
if( xTaskGetCurrentTaskHandle() == xIPTaskHandle )
{
xReturn = pdTRUE;
}
else
{
xReturn = pdFALSE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
static void prvHandleEthernetPacket( NetworkBufferDescriptor_t *pxBuffer )
{
#if( ipconfigUSE_LINKED_RX_MESSAGES == 0 )
{
/* When ipconfigUSE_LINKED_RX_MESSAGES is not set to 0 then only one
buffer will be sent at a time. This is the default way for +TCP to pass
messages from the MAC to the TCP/IP stack. */
prvProcessEthernetPacket( pxBuffer );
}
#else /* ipconfigUSE_LINKED_RX_MESSAGES */
{
NetworkBufferDescriptor_t *pxNextBuffer;
/* An optimisation that is useful when there is high network traffic.
Instead of passing received packets into the IP task one at a time the
network interface can chain received packets together and pass them into
the IP task in one go. The packets are chained using the pxNextBuffer
member. The loop below walks through the chain processing each packet
in the chain in turn. */
do
{
/* Store a pointer to the buffer after pxBuffer for use later on. */
pxNextBuffer = pxBuffer->pxNextBuffer;
/* Make it NULL to avoid using it later on. */
pxBuffer->pxNextBuffer = NULL;
prvProcessEthernetPacket( pxBuffer );
pxBuffer = pxNextBuffer;
/* While there is another packet in the chain. */
} while( pxBuffer != NULL );
}
#endif /* ipconfigUSE_LINKED_RX_MESSAGES */
}
/*-----------------------------------------------------------*/
static TickType_t prvCalculateSleepTime( void )
{
TickType_t xMaximumSleepTime;
/* Start with the maximum sleep time, then check this against the remaining
time in any other timers that are active. */
xMaximumSleepTime = ipconfigMAX_IP_TASK_SLEEP_TIME;
if( xARPTimer.bActive != pdFALSE_UNSIGNED )
{
if( xARPTimer.ulRemainingTime < xMaximumSleepTime )
{
xMaximumSleepTime = xARPTimer.ulReloadTime;
}
}
#if( ipconfigUSE_DHCP == 1 )
{
if( xDHCPTimer.bActive != pdFALSE_UNSIGNED )
{
if( xDHCPTimer.ulRemainingTime < xMaximumSleepTime )
{
xMaximumSleepTime = xDHCPTimer.ulRemainingTime;
}
}
}
#endif /* ipconfigUSE_DHCP */
#if( ipconfigUSE_TCP == 1 )
{
if( xTCPTimer.ulRemainingTime < xMaximumSleepTime )
{
xMaximumSleepTime = xTCPTimer.ulRemainingTime;
}
}
#endif
#if( ipconfigDNS_USE_CALLBACKS != 0 )
{
if( xDNSTimer.bActive != pdFALSE )
{
if( xDNSTimer.ulRemainingTime < xMaximumSleepTime )
{
xMaximumSleepTime = xDNSTimer.ulRemainingTime;
}
}
}
#endif
return xMaximumSleepTime;
}
/*-----------------------------------------------------------*/
static void prvCheckNetworkTimers( void )
{
/* Is it time for ARP processing? */
if( prvIPTimerCheck( &xARPTimer ) != pdFALSE )
{
xSendEventToIPTask( eARPTimerEvent );
}
#if( ipconfigUSE_DHCP == 1 )
{
/* Is it time for DHCP processing? */
if( prvIPTimerCheck( &xDHCPTimer ) != pdFALSE )
{
xSendEventToIPTask( eDHCPEvent );
}
}
#endif /* ipconfigUSE_DHCP */
#if( ipconfigDNS_USE_CALLBACKS != 0 )
{
extern void vDNSCheckCallBack( void *pvSearchID );
/* Is it time for DNS processing? */
if( prvIPTimerCheck( &xDNSTimer ) != pdFALSE )
{
vDNSCheckCallBack( NULL );
}
}
#endif /* ipconfigDNS_USE_CALLBACKS */
#if( ipconfigUSE_TCP == 1 )
{
BaseType_t xWillSleep;
TickType_t xNextTime;
BaseType_t xCheckTCPSockets;
if( uxQueueMessagesWaiting( xNetworkEventQueue ) == 0u )
{
xWillSleep = pdTRUE;
}
else
{
xWillSleep = pdFALSE;
}
/* Sockets need to be checked if the TCP timer has expired. */
xCheckTCPSockets = prvIPTimerCheck( &xTCPTimer );
/* Sockets will also be checked if there are TCP messages but the
message queue is empty (indicated by xWillSleep being true). */
if( ( xProcessedTCPMessage != pdFALSE ) && ( xWillSleep != pdFALSE ) )
{
xCheckTCPSockets = pdTRUE;
}
if( xCheckTCPSockets != pdFALSE )
{
/* Attend to the sockets, returning the period after which the
check must be repeated. */
xNextTime = xTCPTimerCheck( xWillSleep );
prvIPTimerStart( &xTCPTimer, xNextTime );
xProcessedTCPMessage = 0;
}
}
#endif /* ipconfigUSE_TCP == 1 */
}
/*-----------------------------------------------------------*/
static void prvIPTimerStart( IPTimer_t *pxTimer, TickType_t xTime )
{
vTaskSetTimeOutState( &pxTimer->xTimeOut );
pxTimer->ulRemainingTime = xTime;
if( xTime == ( TickType_t ) 0 )
{
pxTimer->bExpired = pdTRUE_UNSIGNED;
}
else
{
pxTimer->bExpired = pdFALSE_UNSIGNED;
}
pxTimer->bActive = pdTRUE_UNSIGNED;
}
/*-----------------------------------------------------------*/
static void prvIPTimerReload( IPTimer_t *pxTimer, TickType_t xTime )
{
pxTimer->ulReloadTime = xTime;
prvIPTimerStart( pxTimer, xTime );
}
/*-----------------------------------------------------------*/
static BaseType_t prvIPTimerCheck( IPTimer_t *pxTimer )
{
BaseType_t xReturn;
if( pxTimer->bActive == pdFALSE_UNSIGNED )
{
/* The timer is not enabled. */
xReturn = pdFALSE;
}
else
{
/* The timer might have set the bExpired flag already, if not, check the
value of xTimeOut against ulRemainingTime. */
if( ( pxTimer->bExpired != pdFALSE_UNSIGNED ) ||
( xTaskCheckForTimeOut( &( pxTimer->xTimeOut ), &( pxTimer->ulRemainingTime ) ) != pdFALSE ) )
{
prvIPTimerStart( pxTimer, pxTimer->ulReloadTime );
xReturn = pdTRUE;
}
else
{
xReturn = pdFALSE;
}
}
return xReturn;
}
/*-----------------------------------------------------------*/
void FreeRTOS_NetworkDown( void )
{
static const IPStackEvent_t xNetworkDownEvent = { eNetworkDownEvent, NULL };
const TickType_t xDontBlock = ( TickType_t ) 0;
/* Simply send the network task the appropriate event. */
if( xSendEventStructToIPTask( &xNetworkDownEvent, xDontBlock ) != pdPASS )
{
/* Could not send the message, so it is still pending. */
xNetworkDownEventPending = pdTRUE;
}
else
{
/* Message was sent so it is not pending. */
xNetworkDownEventPending = pdFALSE;
}
iptraceNETWORK_DOWN();
}
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_NetworkDownFromISR( void )
{
static const IPStackEvent_t xNetworkDownEvent = { eNetworkDownEvent, NULL };
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
/* Simply send the network task the appropriate event. */
if( xQueueSendToBackFromISR( xNetworkEventQueue, &xNetworkDownEvent, &xHigherPriorityTaskWoken ) != pdPASS )
{
xNetworkDownEventPending = pdTRUE;
}
else
{
xNetworkDownEventPending = pdFALSE;
}
iptraceNETWORK_DOWN();
return xHigherPriorityTaskWoken;
}
/*-----------------------------------------------------------*/
void *FreeRTOS_GetUDPPayloadBuffer( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )
{
NetworkBufferDescriptor_t *pxNetworkBuffer;
void *pvReturn;
/* Cap the block time. The reason for this is explained where
ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS is defined (assuming an official
FreeRTOSIPConfig.h header file is being used). */
if( xBlockTimeTicks > ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS )
{
xBlockTimeTicks = ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS;
}
/* Obtain a network buffer with the required amount of storage. */
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( sizeof( UDPPacket_t ) + xRequestedSizeBytes, xBlockTimeTicks );
if( pxNetworkBuffer != NULL )
{
/* Set the actual packet size in case a bigger buffer was returned. */
pxNetworkBuffer->xDataLength = sizeof( UDPPacket_t ) + xRequestedSizeBytes;
/* Leave space for the UPD header. */
pvReturn = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );
}
else
{
pvReturn = NULL;
}
return ( void * ) pvReturn;
}
/*-----------------------------------------------------------*/
NetworkBufferDescriptor_t *pxDuplicateNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer,
BaseType_t xNewLength )
{
NetworkBufferDescriptor_t * pxNewBuffer;
/* This function is only used when 'ipconfigZERO_COPY_TX_DRIVER' is set to 1.
The transmit routine wants to have ownership of the network buffer
descriptor, because it will pass the buffer straight to DMA. */
pxNewBuffer = pxGetNetworkBufferWithDescriptor( ( size_t ) xNewLength, ( TickType_t ) 0 );
if( pxNewBuffer != NULL )
{
/* Set the actual packet size in case a bigger buffer than requested
was returned. */
pxNewBuffer->xDataLength = xNewLength;
/* Copy the original packet information. */
pxNewBuffer->ulIPAddress = pxNetworkBuffer->ulIPAddress;
pxNewBuffer->usPort = pxNetworkBuffer->usPort;
pxNewBuffer->usBoundPort = pxNetworkBuffer->usBoundPort;
memcpy( pxNewBuffer->pucEthernetBuffer, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
}
return pxNewBuffer;
}
/*-----------------------------------------------------------*/
#if( ipconfigZERO_COPY_TX_DRIVER != 0 ) || ( ipconfigZERO_COPY_RX_DRIVER != 0 )
NetworkBufferDescriptor_t *pxPacketBuffer_to_NetworkBuffer( const void *pvBuffer )
{
uint8_t *pucBuffer;
NetworkBufferDescriptor_t *pxResult;
if( pvBuffer == NULL )
{
pxResult = NULL;
}
else
{
/* Obtain the network buffer from the zero copy pointer. */
pucBuffer = ( uint8_t * ) pvBuffer;
/* The input here is a pointer to a payload buffer. Subtract the
size of the header in the network buffer, usually 8 + 2 bytes. */
pucBuffer -= ipBUFFER_PADDING;
/* Here a pointer was placed to the network descriptor. As a
pointer is dereferenced, make sure it is well aligned. */
if( ( ( ( uint32_t ) pucBuffer ) & ( sizeof( pucBuffer ) - ( size_t ) 1 ) ) == ( uint32_t ) 0 )
{
pxResult = * ( ( NetworkBufferDescriptor_t ** ) pucBuffer );
}
else
{
pxResult = NULL;
}
}
return pxResult;
}
#endif /* ipconfigZERO_COPY_TX_DRIVER != 0 */
/*-----------------------------------------------------------*/
NetworkBufferDescriptor_t *pxUDPPayloadBuffer_to_NetworkBuffer( void *pvBuffer )
{
uint8_t *pucBuffer;
NetworkBufferDescriptor_t *pxResult;
if( pvBuffer == NULL )
{
pxResult = NULL;
}
else
{
/* Obtain the network buffer from the zero copy pointer. */
pucBuffer = ( uint8_t * ) pvBuffer;
/* The input here is a pointer to a payload buffer. Subtract
the total size of a UDP/IP header plus the size of the header in
the network buffer, usually 8 + 2 bytes. */
pucBuffer -= ( sizeof( UDPPacket_t ) + ipBUFFER_PADDING );
/* Here a pointer was placed to the network descriptor,
As a pointer is dereferenced, make sure it is well aligned */
if( ( ( ( uint32_t ) pucBuffer ) & ( sizeof( pucBuffer ) - 1 ) ) == 0 )
{
/* The following statement may trigger a:
warning: cast increases required alignment of target type [-Wcast-align].
It has been confirmed though that the alignment is suitable. */
pxResult = * ( ( NetworkBufferDescriptor_t ** ) pucBuffer );
}
else
{
pxResult = NULL;
}
}
return pxResult;
}
/*-----------------------------------------------------------*/
void FreeRTOS_ReleaseUDPPayloadBuffer( void *pvBuffer )
{
vReleaseNetworkBufferAndDescriptor( pxUDPPayloadBuffer_to_NetworkBuffer( pvBuffer ) );
}
/*-----------------------------------------------------------*/
/*_RB_ Should we add an error or assert if the task priorities are set such that the servers won't function as expected? */
/*_HT_ There was a bug in FreeRTOS_TCP_IP.c that only occurred when the applications' priority was too high.
As that bug has been repaired, there is not an urgent reason to warn.
It is better though to use the advised priority scheme. */
BaseType_t FreeRTOS_IPInit( const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucDNSServerAddress[ ipIP_ADDRESS_LENGTH_BYTES ], const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] )
{
BaseType_t xReturn = pdFALSE;
/* This function should only be called once. */
configASSERT( xIPIsNetworkTaskReady() == pdFALSE );
configASSERT( xNetworkEventQueue == NULL );
configASSERT( xIPTaskHandle == NULL );
/* Check structure packing is correct. */
configASSERT( sizeof( EthernetHeader_t ) == ipEXPECTED_EthernetHeader_t_SIZE );
configASSERT( sizeof( ARPHeader_t ) == ipEXPECTED_ARPHeader_t_SIZE );
configASSERT( sizeof( IPHeader_t ) == ipEXPECTED_IPHeader_t_SIZE );
configASSERT( sizeof( ICMPHeader_t ) == ipEXPECTED_ICMPHeader_t_SIZE );
configASSERT( sizeof( UDPHeader_t ) == ipEXPECTED_UDPHeader_t_SIZE );
/* Attempt to create the queue used to communicate with the IP task. */
xNetworkEventQueue = xQueueCreate( ( UBaseType_t ) ipconfigEVENT_QUEUE_LENGTH, ( UBaseType_t ) sizeof( IPStackEvent_t ) );
configASSERT( xNetworkEventQueue );
if( xNetworkEventQueue != NULL )
{
#if ( configQUEUE_REGISTRY_SIZE > 0 )
{
/* A queue registry is normally used to assist a kernel aware
debugger. If one is in use then it will be helpful for the debugger
to show information about the network event queue. */
vQueueAddToRegistry( xNetworkEventQueue, "NetEvnt" );
}
#endif /* configQUEUE_REGISTRY_SIZE */
if( xNetworkBuffersInitialise() == pdPASS )
{
/* Store the local IP and MAC address. */
xNetworkAddressing.ulDefaultIPAddress = FreeRTOS_inet_addr_quick( ucIPAddress[ 0 ], ucIPAddress[ 1 ], ucIPAddress[ 2 ], ucIPAddress[ 3 ] );
xNetworkAddressing.ulNetMask = FreeRTOS_inet_addr_quick( ucNetMask[ 0 ], ucNetMask[ 1 ], ucNetMask[ 2 ], ucNetMask[ 3 ] );
xNetworkAddressing.ulGatewayAddress = FreeRTOS_inet_addr_quick( ucGatewayAddress[ 0 ], ucGatewayAddress[ 1 ], ucGatewayAddress[ 2 ], ucGatewayAddress[ 3 ] );
xNetworkAddressing.ulDNSServerAddress = FreeRTOS_inet_addr_quick( ucDNSServerAddress[ 0 ], ucDNSServerAddress[ 1 ], ucDNSServerAddress[ 2 ], ucDNSServerAddress[ 3 ] );
xNetworkAddressing.ulBroadcastAddress = ( xNetworkAddressing.ulDefaultIPAddress & xNetworkAddressing.ulNetMask ) | ~xNetworkAddressing.ulNetMask;
memcpy( &xDefaultAddressing, &xNetworkAddressing, sizeof( xDefaultAddressing ) );
#if ipconfigUSE_DHCP == 1
{
/* The IP address is not set until DHCP completes. */
*ipLOCAL_IP_ADDRESS_POINTER = 0x00UL;
}
#else
{
/* The IP address is set from the value passed in. */
*ipLOCAL_IP_ADDRESS_POINTER = xNetworkAddressing.ulDefaultIPAddress;
/* Added to prevent ARP flood to gateway. Ensure the
gateway is on the same subnet as the IP address. */
configASSERT( ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) == ( xNetworkAddressing.ulGatewayAddress & xNetworkAddressing.ulNetMask ) );
}
#endif /* ipconfigUSE_DHCP == 1 */
/* The MAC address is stored in the start of the default packet
header fragment, which is used when sending UDP packets. */
memcpy( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) ucMACAddress, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
/* Prepare the sockets interface. */
xReturn = vNetworkSocketsInit();
if( pdTRUE == xReturn )
{
/* Create the task that processes Ethernet and stack events. */
xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );
}
}
else
{
FreeRTOS_debug_printf( ( "FreeRTOS_IPInit: xNetworkBuffersInitialise() failed\n") );
/* Clean up. */
vQueueDelete( xNetworkEventQueue );
xNetworkEventQueue = NULL;
}
}
else
{
FreeRTOS_debug_printf( ( "FreeRTOS_IPInit: Network event queue could not be created\n") );
}
return xReturn;
}
/*-----------------------------------------------------------*/
void FreeRTOS_GetAddressConfiguration( uint32_t *pulIPAddress, uint32_t *pulNetMask, uint32_t *pulGatewayAddress, uint32_t *pulDNSServerAddress )
{
/* Return the address configuration to the caller. */
if( pulIPAddress != NULL )
{
*pulIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
}
if( pulNetMask != NULL )
{
*pulNetMask = xNetworkAddressing.ulNetMask;
}
if( pulGatewayAddress != NULL )
{
*pulGatewayAddress = xNetworkAddressing.ulGatewayAddress;
}
if( pulDNSServerAddress != NULL )
{
*pulDNSServerAddress = xNetworkAddressing.ulDNSServerAddress;
}
}
/*-----------------------------------------------------------*/
void FreeRTOS_SetAddressConfiguration( const uint32_t *pulIPAddress, const uint32_t *pulNetMask, const uint32_t *pulGatewayAddress, const uint32_t *pulDNSServerAddress )
{
/* Update the address configuration. */
if( pulIPAddress != NULL )
{
*ipLOCAL_IP_ADDRESS_POINTER = *pulIPAddress;
}
if( pulNetMask != NULL )
{
xNetworkAddressing.ulNetMask = *pulNetMask;
}
if( pulGatewayAddress != NULL )
{
xNetworkAddressing.ulGatewayAddress = *pulGatewayAddress;
}
if( pulDNSServerAddress != NULL )
{
xNetworkAddressing.ulDNSServerAddress = *pulDNSServerAddress;
}
}
/*-----------------------------------------------------------*/
#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
BaseType_t FreeRTOS_SendPingRequest( uint32_t ulIPAddress, size_t xNumberOfBytesToSend, TickType_t xBlockTimeTicks )
{
NetworkBufferDescriptor_t *pxNetworkBuffer;
ICMPHeader_t *pxICMPHeader;
BaseType_t xReturn = pdFAIL;
static uint16_t usSequenceNumber = 0;
uint8_t *pucChar;
IPStackEvent_t xStackTxEvent = { eStackTxEvent, NULL };
if( (xNumberOfBytesToSend >= 1 ) && ( xNumberOfBytesToSend < ( ( ipconfigNETWORK_MTU - sizeof( IPHeader_t ) ) - sizeof( ICMPHeader_t ) ) ) && ( uxGetNumberOfFreeNetworkBuffers() >= 3 ) )
{
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( xNumberOfBytesToSend + sizeof( ICMPPacket_t ), xBlockTimeTicks );
if( pxNetworkBuffer != NULL )
{
pxICMPHeader = ( ICMPHeader_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipIP_PAYLOAD_OFFSET ] );
usSequenceNumber++;
/* Fill in the basic header information. */
pxICMPHeader->ucTypeOfMessage = ipICMP_ECHO_REQUEST;
pxICMPHeader->ucTypeOfService = 0;
pxICMPHeader->usIdentifier = usSequenceNumber;
pxICMPHeader->usSequenceNumber = usSequenceNumber;
/* Find the start of the data. */
pucChar = ( uint8_t * ) pxICMPHeader;
pucChar += sizeof( ICMPHeader_t );
/* Just memset the data to a fixed value. */
memset( ( void * ) pucChar, ( int ) ipECHO_DATA_FILL_BYTE, xNumberOfBytesToSend );
/* The message is complete, IP and checksum's are handled by
vProcessGeneratedUDPPacket */
pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ] = FREERTOS_SO_UDPCKSUM_OUT;
pxNetworkBuffer->ulIPAddress = ulIPAddress;
pxNetworkBuffer->usPort = ipPACKET_CONTAINS_ICMP_DATA;
pxNetworkBuffer->xDataLength = xNumberOfBytesToSend + sizeof( ICMPHeader_t );
/* Send to the stack. */
xStackTxEvent.pvData = pxNetworkBuffer;
if( xSendEventStructToIPTask( &xStackTxEvent, xBlockTimeTicks) != pdPASS )
{
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
iptraceSTACK_TX_EVENT_LOST( ipSTACK_TX_EVENT );
}
else
{
xReturn = usSequenceNumber;
}
}
}
else
{
/* The requested number of bytes will not fit in the available space
in the network buffer. */
}
return xReturn;
}
#endif /* ipconfigSUPPORT_OUTGOING_PINGS == 1 */
/*-----------------------------------------------------------*/
BaseType_t xSendEventToIPTask( eIPEvent_t eEvent )
{
IPStackEvent_t xEventMessage;
const TickType_t xDontBlock = ( TickType_t ) 0;
xEventMessage.eEventType = eEvent;
xEventMessage.pvData = ( void* )NULL;
return xSendEventStructToIPTask( &xEventMessage, xDontBlock );
}
/*-----------------------------------------------------------*/
BaseType_t xSendEventStructToIPTask( const IPStackEvent_t *pxEvent, TickType_t xTimeout )
{
BaseType_t xReturn, xSendMessage;
if( ( xIPIsNetworkTaskReady() == pdFALSE ) && ( pxEvent->eEventType != eNetworkDownEvent ) )
{
/* Only allow eNetworkDownEvent events if the IP task is not ready
yet. Not going to attempt to send the message so the send failed. */
xReturn = pdFAIL;
}
else
{
xSendMessage = pdTRUE;
#if( ipconfigUSE_TCP == 1 )
{
if( pxEvent->eEventType == eTCPTimerEvent )
{
/* TCP timer events are sent to wake the timer task when
xTCPTimer has expired, but there is no point sending them if the
IP task is already awake processing other message. */
xTCPTimer.bExpired = pdTRUE_UNSIGNED;
if( uxQueueMessagesWaiting( xNetworkEventQueue ) != 0u )
{
/* Not actually going to send the message but this is not a
failure as the message didn't need to be sent. */
xSendMessage = pdFALSE;
}
}
}
#endif /* ipconfigUSE_TCP */
if( xSendMessage != pdFALSE )
{
/* The IP task cannot block itself while waiting for itself to
respond. */
if( ( xIsCallingFromIPTask() == pdTRUE ) && ( xTimeout > ( TickType_t ) 0 ) )
{
xTimeout = ( TickType_t ) 0;
}
xReturn = xQueueSendToBack( xNetworkEventQueue, pxEvent, xTimeout );
if( xReturn == pdFAIL )
{
/* A message should have been sent to the IP task, but wasn't. */
FreeRTOS_debug_printf( ( "xSendEventStructToIPTask: CAN NOT ADD %d\n", pxEvent->eEventType ) );
iptraceSTACK_TX_EVENT_LOST( pxEvent->eEventType );
}
}
else
{
/* It was not necessary to send the message to process the event so
even though the message was not sent the call was successful. */
xReturn = pdPASS;
}
}
return xReturn;
}
/*-----------------------------------------------------------*/
eFrameProcessingResult_t eConsiderFrameForProcessing( const uint8_t * const pucEthernetBuffer )
{
eFrameProcessingResult_t eReturn;
const EthernetHeader_t *pxEthernetHeader;
pxEthernetHeader = ( const EthernetHeader_t * ) pucEthernetBuffer;
if( memcmp( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) &( pxEthernetHeader->xDestinationAddress ), sizeof( MACAddress_t ) ) == 0 )
{
/* The packet was directed to this node directly - process it. */
eReturn = eProcessBuffer;
}
else if( memcmp( ( void * ) xBroadcastMACAddress.ucBytes, ( void * ) pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 )
{
/* The packet was a broadcast - process it. */
eReturn = eProcessBuffer;
}
else
#if( ipconfigUSE_LLMNR == 1 )
if( memcmp( ( void * ) xLLMNR_MacAdress.ucBytes, ( void * ) pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 )
{
/* The packet is a request for LLMNR - process it. */
eReturn = eProcessBuffer;
}
else
#endif /* ipconfigUSE_LLMNR */
{
/* The packet was not a broadcast, or for this node, just release
the buffer without taking any other action. */
eReturn = eReleaseBuffer;
}
#if( ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1 )
{
uint16_t usFrameType;
if( eReturn == eProcessBuffer )
{
usFrameType = pxEthernetHeader->usFrameType;
usFrameType = FreeRTOS_ntohs( usFrameType );
if( usFrameType <= 0x600U )
{
/* Not an Ethernet II frame. */
eReturn = eReleaseBuffer;
}
}
}
#endif /* ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1 */
return eReturn;
}
/*-----------------------------------------------------------*/
static void prvProcessNetworkDownEvent( void )
{
/* Stop the ARP timer while there is no network. */
xARPTimer.bActive = pdFALSE_UNSIGNED;
#if ipconfigUSE_NETWORK_EVENT_HOOK == 1
{
static BaseType_t xCallEventHook = pdFALSE;
/* The first network down event is generated by the IP stack itself to
initialise the network hardware, so do not call the network down event
the first time through. */
if( xCallEventHook == pdTRUE )
{
vApplicationIPNetworkEventHook( eNetworkDown );
}
xCallEventHook = pdTRUE;
}
#endif
/* Per the ARP Cache Validation section of https://tools.ietf.org/html/rfc1122,
treat network down as a "delivery problem" and flush the ARP cache for this
interface. */
FreeRTOS_ClearARP( );
/* The network has been disconnected (or is being initialised for the first
time). Perform whatever hardware processing is necessary to bring it up
again, or wait for it to be available again. This is hardware dependent. */
if( xNetworkInterfaceInitialise() != pdPASS )
{
/* Ideally the network interface initialisation function will only
return when the network is available. In case this is not the case,
wait a while before retrying the initialisation. */
vTaskDelay( ipINITIALISATION_RETRY_DELAY );
FreeRTOS_NetworkDown();
}
else
{
/* Set remaining time to 0 so it will become active immediately. */
#if ipconfigUSE_DHCP == 1
{
/* The network is not up until DHCP has completed. */
vDHCPProcess( pdTRUE );
xSendEventToIPTask( eDHCPEvent );
}
#else
{
/* Perform any necessary 'network up' processing. */
vIPNetworkUpCalls();
}
#endif
}
}
/*-----------------------------------------------------------*/
void vIPNetworkUpCalls( void )
{
xNetworkUp = pdTRUE;
#if( ipconfigUSE_NETWORK_EVENT_HOOK == 1 )
{
vApplicationIPNetworkEventHook( eNetworkUp );
}
#endif /* ipconfigUSE_NETWORK_EVENT_HOOK */
#if( ipconfigDNS_USE_CALLBACKS != 0 )
{
/* The following function is declared in FreeRTOS_DNS.c and 'private' to
this library */
extern void vDNSInitialise( void );
vDNSInitialise();
}
#endif /* ipconfigDNS_USE_CALLBACKS != 0 */
/* Set remaining time to 0 so it will become active immediately. */
prvIPTimerReload( &xARPTimer, pdMS_TO_TICKS( ipARP_TIMER_PERIOD_MS ) );
}
/*-----------------------------------------------------------*/
static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
{
EthernetHeader_t *pxEthernetHeader;
eFrameProcessingResult_t eReturned = eReleaseBuffer;
configASSERT( pxNetworkBuffer );
/* Interpret the Ethernet frame. */
if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) )
{
eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer );
pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer );
if( eReturned == eProcessBuffer )
{
/* Interpret the received Ethernet packet. */
switch( pxEthernetHeader->usFrameType )
{
case ipARP_FRAME_TYPE:
/* The Ethernet frame contains an ARP packet. */
if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) )
{
eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer );
}
else
{
eReturned = eReleaseBuffer;
}
break;
case ipIPv4_FRAME_TYPE:
/* The Ethernet frame contains an IP packet. */
if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) )
{
eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer );
}
else
{
eReturned = eReleaseBuffer;
}
break;
default:
/* No other packet types are handled. Nothing to do. */
eReturned = eReleaseBuffer;
break;
}
}
}
/* Perform any actions that resulted from processing the Ethernet frame. */
switch( eReturned )
{
case eReturnEthernetFrame :
/* The Ethernet frame will have been updated (maybe it was
an ARP request or a PING request?) and should be sent back to
its source. */
vReturnEthernetFrame( pxNetworkBuffer, pdTRUE );
/* parameter pdTRUE: the buffer must be released once
the frame has been transmitted */
break;
case eFrameConsumed :
/* The frame is in use somewhere, don't release the buffer
yet. */
break;
default :
/* The frame is not being used anywhere, and the
NetworkBufferDescriptor_t structure containing the frame should
just be released back to the list of free buffers. */
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
break;
}
}
/*-----------------------------------------------------------*/
static eFrameProcessingResult_t prvAllowIPPacket( const IPPacket_t * const pxIPPacket,
NetworkBufferDescriptor_t * const pxNetworkBuffer, UBaseType_t uxHeaderLength )
{
eFrameProcessingResult_t eReturn = eProcessBuffer;
#if( ( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 0 ) || ( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 0 ) )
const IPHeader_t * pxIPHeader = &( pxIPPacket->xIPHeader );
#else
/* or else, the parameter won't be used and the function will be optimised
away */
( void ) pxIPPacket;
#endif
#if( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 0 )
{
/* In systems with a very small amount of RAM, it might be advantageous
to have incoming messages checked earlier, by the network card driver.
This method may decrease the usage of sparse network buffers. */
uint32_t ulDestinationIPAddress = pxIPHeader->ulDestinationIPAddress;
/* Ensure that the incoming packet is not fragmented (only outgoing
packets can be fragmented) as these are the only handled IP frames
currently. */
if( ( pxIPHeader->usFragmentOffset & ipFRAGMENT_OFFSET_BIT_MASK ) != 0U )
{
/* Can not handle, fragmented packet. */
eReturn = eReleaseBuffer;
}
/* 0x45 means: IPv4 with an IP header of 5 x 4 = 20 bytes
* 0x47 means: IPv4 with an IP header of 7 x 4 = 28 bytes */
else if( ( pxIPHeader->ucVersionHeaderLength < 0x45u ) || ( pxIPHeader->ucVersionHeaderLength > 0x4Fu ) )
{
/* Can not handle, unknown or invalid header version. */
eReturn = eReleaseBuffer;
}
/* Is the packet for this IP address? */
else if( ( ulDestinationIPAddress != *ipLOCAL_IP_ADDRESS_POINTER ) &&
/* Is it the global broadcast address 255.255.255.255 ? */
( ulDestinationIPAddress != ipBROADCAST_IP_ADDRESS ) &&
/* Is it a specific broadcast address 192.168.1.255 ? */
( ulDestinationIPAddress != xNetworkAddressing.ulBroadcastAddress ) &&
#if( ipconfigUSE_LLMNR == 1 )
/* Is it the LLMNR multicast address? */
( ulDestinationIPAddress != ipLLMNR_IP_ADDR ) &&
#endif
/* Or (during DHCP negotiation) we have no IP-address yet? */
( *ipLOCAL_IP_ADDRESS_POINTER != 0UL ) )
{
/* Packet is not for this node, release it */
eReturn = eReleaseBuffer;
}
}
#endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS */
#if( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 0 )
{
/* Some drivers of NIC's with checksum-offloading will enable the above
define, so that the checksum won't be checked again here */
if (eReturn == eProcessBuffer )
{
/* Is the IP header checksum correct? */
if( ( pxIPHeader->ucProtocol != ( uint8_t ) ipPROTOCOL_ICMP ) &&
( usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ( size_t ) uxHeaderLength ) != ipCORRECT_CRC ) )
{
/* Check sum in IP-header not correct. */
eReturn = eReleaseBuffer;
}
/* Is the upper-layer checksum (TCP/UDP/ICMP) correct? */
else if( usGenerateProtocolChecksum( ( uint8_t * )( pxNetworkBuffer->pucEthernetBuffer ), pxNetworkBuffer->xDataLength, pdFALSE ) != ipCORRECT_CRC )
{
/* Protocol checksum not accepted. */
eReturn = eReleaseBuffer;
}
}
}
#else
{
/* to avoid warning unused parameters */
( void ) pxNetworkBuffer;
( void ) uxHeaderLength;
}
#endif /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 0 */
return eReturn;
}
/*-----------------------------------------------------------*/
static eFrameProcessingResult_t prvProcessIPPacket( IPPacket_t * const pxIPPacket, NetworkBufferDescriptor_t * const pxNetworkBuffer )
{
eFrameProcessingResult_t eReturn;
IPHeader_t * pxIPHeader = &( pxIPPacket->xIPHeader );
UBaseType_t uxHeaderLength = ( UBaseType_t ) ( ( pxIPHeader->ucVersionHeaderLength & 0x0Fu ) << 2 );
uint8_t ucProtocol;
/* Bound the calculated header length: take away the Ethernet header size,
then check if the IP header is claiming to be longer than the remaining
total packet size. Also check for minimal header field length. */
if( ( uxHeaderLength > ( pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER ) ) ||
( uxHeaderLength < ipSIZE_OF_IPv4_HEADER ) )
{
return eReleaseBuffer;
}
ucProtocol = pxIPPacket->xIPHeader.ucProtocol;
/* Check if the IP headers are acceptable and if it has our destination. */
eReturn = prvAllowIPPacket( pxIPPacket, pxNetworkBuffer, uxHeaderLength );
if( eReturn == eProcessBuffer )
{
if( uxHeaderLength > ipSIZE_OF_IPv4_HEADER )
{
/* All structs of headers expect a IP header size of 20 bytes
* IP header options were included, we'll ignore them and cut them out
* Note: IP options are mostly use in Multi-cast protocols */
const size_t optlen = ( ( size_t ) uxHeaderLength ) - ipSIZE_OF_IPv4_HEADER;
/* From: the previous start of UDP/ICMP/TCP data */
uint8_t *pucSource = ( uint8_t* )(pxNetworkBuffer->pucEthernetBuffer + sizeof( EthernetHeader_t ) + uxHeaderLength);
/* To: the usual start of UDP/ICMP/TCP data at offset 20 from IP header */
uint8_t *pucTarget = ( uint8_t* )(pxNetworkBuffer->pucEthernetBuffer + sizeof( EthernetHeader_t ) + ipSIZE_OF_IPv4_HEADER);
/* How many: total length minus the options and the lower headers */
const size_t xMoveLen = pxNetworkBuffer->xDataLength - optlen - ipSIZE_OF_IPv4_HEADER - ipSIZE_OF_ETH_HEADER;
memmove( pucTarget, pucSource, xMoveLen );
pxNetworkBuffer->xDataLength -= optlen;
/* Fix-up new version/header length field in IP packet. */
pxIPHeader->ucVersionHeaderLength = ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */
( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */
}
/* Add the IP and MAC addresses to the ARP table if they are not
already there - otherwise refresh the age of the existing
entry. */
if( ucProtocol != ( uint8_t ) ipPROTOCOL_UDP )
{
/* Refresh the ARP cache with the IP/MAC-address of the received packet
* For UDP packets, this will be done later in xProcessReceivedUDPPacket()
* as soon as know that the message will be handled by someone
* This will prevent that the ARP cache will get overwritten
* with the IP-address of useless broadcast packets
*/
vARPRefreshCacheEntry( &( pxIPPacket->xEthernetHeader.xSourceAddress ), pxIPHeader->ulSourceIPAddress );
}
switch( ucProtocol )
{
case ipPROTOCOL_ICMP :
/* The IP packet contained an ICMP frame. Don't bother
checking the ICMP checksum, as if it is wrong then the
wrong data will also be returned, and the source of the
ping will know something went wrong because it will not
be able to validate what it receives. */
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
{
if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) )
{
ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer );
if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )
{
eReturn = prvProcessICMPPacket( pxICMPPacket );
}
}
else
{
eReturn = eReleaseBuffer;
}
}
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */
break;
case ipPROTOCOL_UDP :
{
/* The IP packet contained a UDP frame. */
UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
/* Only proceed if the payload length indicated in the header
appears to be valid. */
if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )
{
/* Ensure that downstream UDP packet handling has the lesser
* of: the actual network buffer Ethernet frame length, or
* the sender's UDP packet header payload length, minus the
* size of the UDP header.
*
* The size of the UDP packet structure in this implementation
* includes the size of the Ethernet header, the size of
* the IP header, and the size of the UDP header.
*/
pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t );
if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) <
pxNetworkBuffer->xDataLength )
{
pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t );
}
/* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */
pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort;
pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;
/* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM:
* In some cases, the upper-layer checksum has been calculated
* by the NIC driver.
*
* Pass the packet payload to the UDP sockets implementation. */
if( xProcessReceivedUDPPacket( pxNetworkBuffer,
pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS )
{
eReturn = eFrameConsumed;
}
}
else
{
eReturn = eReleaseBuffer;
}
}
break;
#if ipconfigUSE_TCP == 1
case ipPROTOCOL_TCP :
{
if( xProcessReceivedTCPPacket( pxNetworkBuffer ) == pdPASS )
{
eReturn = eFrameConsumed;
}
/* Setting this variable will cause xTCPTimerCheck()
to be called just before the IP-task blocks. */
xProcessedTCPMessage++;
}
break;
#endif
default :
/* Not a supported frame type. */
break;
}
}
return eReturn;
}
/*-----------------------------------------------------------*/
#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
static void prvProcessICMPEchoReply( ICMPPacket_t * const pxICMPPacket )
{
ePingReplyStatus_t eStatus = eSuccess;
uint16_t usDataLength, usCount;
uint8_t *pucByte;
/* Find the total length of the IP packet. */
usDataLength = pxICMPPacket->xIPHeader.usLength;
usDataLength = FreeRTOS_ntohs( usDataLength );
/* Remove the length of the IP headers to obtain the length of the ICMP
message itself. */
usDataLength = ( uint16_t ) ( ( ( uint32_t ) usDataLength ) - ipSIZE_OF_IPv4_HEADER );
/* Remove the length of the ICMP header, to obtain the length of
data contained in the ping. */
usDataLength = ( uint16_t ) ( ( ( uint32_t ) usDataLength ) - ipSIZE_OF_ICMP_HEADER );
/* Checksum has already been checked before in prvProcessIPPacket */
/* Find the first byte of the data within the ICMP packet. */
pucByte = ( uint8_t * ) pxICMPPacket;
pucByte += sizeof( ICMPPacket_t );
/* Check each byte. */
for( usCount = 0; usCount < usDataLength; usCount++ )
{
if( *pucByte != ipECHO_DATA_FILL_BYTE )
{
eStatus = eInvalidData;
break;
}
pucByte++;
}
/* Call back into the application to pass it the result. */
vApplicationPingReplyHook( eStatus, pxICMPPacket->xICMPHeader.usIdentifier );
}
#endif
/*-----------------------------------------------------------*/
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 )
static eFrameProcessingResult_t prvProcessICMPEchoRequest( ICMPPacket_t * const pxICMPPacket )
{
ICMPHeader_t *pxICMPHeader;
IPHeader_t *pxIPHeader;
uint16_t usRequest;
pxICMPHeader = &( pxICMPPacket->xICMPHeader );
pxIPHeader = &( pxICMPPacket->xIPHeader );
/* HT:endian: changed back */
iptraceSENDING_PING_REPLY( pxIPHeader->ulSourceIPAddress );
/* The checksum can be checked here - but a ping reply should be
returned even if the checksum is incorrect so the other end can
tell that the ping was received - even if the ping reply contains
invalid data. */
pxICMPHeader->ucTypeOfMessage = ( uint8_t ) ipICMP_ECHO_REPLY;
pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
/* Update the checksum because the ucTypeOfMessage member in the header
has been changed to ipICMP_ECHO_REPLY. This is faster than calling
usGenerateChecksum(). */
/* due to compiler warning "integer operation result is out of range" */
usRequest = ( uint16_t ) ( ( uint16_t )ipICMP_ECHO_REQUEST << 8 );
if( pxICMPHeader->usChecksum >= FreeRTOS_htons( 0xFFFFu - usRequest ) )
{
pxICMPHeader->usChecksum = ( uint16_t )
( ( ( uint32_t ) pxICMPHeader->usChecksum ) +
FreeRTOS_htons( usRequest + 1UL ) );
}
else
{
pxICMPHeader->usChecksum = ( uint16_t )
( ( ( uint32_t ) pxICMPHeader->usChecksum ) +
FreeRTOS_htons( usRequest ) );
}
return eReturnEthernetFrame;
}
#endif /* ipconfigREPLY_TO_INCOMING_PINGS == 1 */
/*-----------------------------------------------------------*/
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
static eFrameProcessingResult_t prvProcessICMPPacket( ICMPPacket_t * const pxICMPPacket )
{
eFrameProcessingResult_t eReturn = eReleaseBuffer;
iptraceICMP_PACKET_RECEIVED();
switch( pxICMPPacket->xICMPHeader.ucTypeOfMessage )
{
case ipICMP_ECHO_REQUEST :
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 )
{
eReturn = prvProcessICMPEchoRequest( pxICMPPacket );
}
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) */
break;
case ipICMP_ECHO_REPLY :
#if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
{
prvProcessICMPEchoReply( pxICMPPacket );
}
#endif /* ipconfigSUPPORT_OUTGOING_PINGS */
break;
default :
break;
}
return eReturn;
}
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */
/*-----------------------------------------------------------*/
uint16_t usGenerateProtocolChecksum( const uint8_t * const pucEthernetBuffer, size_t uxBufferLength, BaseType_t xOutgoingPacket )
{
uint32_t ulLength;
uint16_t usChecksum, *pusChecksum;
const IPPacket_t * pxIPPacket;
UBaseType_t uxIPHeaderLength;
ProtocolPacket_t *pxProtPack;
uint8_t ucProtocol;
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
const char *pcType;
#endif
/* Check for minimum packet size. */
if( uxBufferLength < sizeof( IPPacket_t ) )
{
return ipINVALID_LENGTH;
}
/* Parse the packet length. */
pxIPPacket = ( const IPPacket_t * ) pucEthernetBuffer;
/* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header
Length field contains the length of the internet header in 32-bit words. */
uxIPHeaderLength = ( UBaseType_t ) ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) );
/* Check for minimum packet size. */
if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER )
{
return ipINVALID_LENGTH;
}
if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) )
{
return ipINVALID_LENGTH;
}
/* Identify the next protocol. */
ucProtocol = pxIPPacket->xIPHeader.ucProtocol;
/* N.B., if this IP packet header includes Options, then the following
assignment results in a pointer into the protocol packet with the Ethernet
and IP headers incorrectly aligned. However, either way, the "third"
protocol (Layer 3 or 4) header will be aligned, which is the convenience
of this calculation. */
pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) );
/* Switch on the Layer 3/4 protocol. */
if( ucProtocol == ( uint8_t ) ipPROTOCOL_UDP )
{
if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER ) )
{
return ipINVALID_LENGTH;
}
pusChecksum = ( uint16_t * ) ( &( pxProtPack->xUDPPacket.xUDPHeader.usChecksum ) );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
pcType = "UDP";
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
}
else if( ucProtocol == ( uint8_t ) ipPROTOCOL_TCP )
{
if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER ) )
{
return ipINVALID_LENGTH;
}
pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
pcType = "TCP";
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
}
else if( ( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP ) ||
( ucProtocol == ( uint8_t ) ipPROTOCOL_IGMP ) )
{
if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER ) )
{
return ipINVALID_LENGTH;
}
pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
if( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP )
{
pcType = "ICMP";
}
else
{
pcType = "IGMP";
}
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
}
else
{
/* Unhandled protocol, other than ICMP, IGMP, UDP, or TCP. */
return ipUNHANDLED_PROTOCOL;
}
/* The protocol and checksum field have been identified. Check the direction
of the packet. */
if( xOutgoingPacket != pdFALSE )
{
/* This is an outgoing packet. Before calculating the checksum, set it
to zero. */
*( pusChecksum ) = 0u;
}
else if( ( *pusChecksum == 0u ) && ( ucProtocol == ( uint8_t ) ipPROTOCOL_UDP ) )
{
/* Sender hasn't set the checksum, no use to calculate it. */
return ipCORRECT_CRC;
}
ulLength = ( uint32_t )
( FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) - ( ( uint16_t ) uxIPHeaderLength ) ); /* normally minus 20 */
if( ( ulLength < sizeof( pxProtPack->xUDPPacket.xUDPHeader ) ) ||
( ulLength > ( uint32_t )( ipconfigNETWORK_MTU - uxIPHeaderLength ) ) )
{
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
FreeRTOS_debug_printf( ( "usGenerateProtocolChecksum[%s]: len invalid: %lu\n", pcType, ulLength ) );
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
/* Again, in a 16-bit return value there is no space to indicate an
error. For incoming packets, 0x1234 will cause dropping of the packet.
For outgoing packets, there is a serious problem with the
format/length */
return ipINVALID_LENGTH;
}
if( ucProtocol <= ( uint8_t ) ipPROTOCOL_IGMP )
{
/* ICMP/IGMP do not have a pseudo header for CRC-calculation. */
usChecksum = ( uint16_t )
( ~usGenerateChecksum( 0UL,
( uint8_t * ) &( pxProtPack->xTCPPacket.xTCPHeader ), ( size_t ) ulLength ) );
}
else
{
/* For UDP and TCP, sum the pseudo header, i.e. IP protocol + length
fields */
usChecksum = ( uint16_t ) ( ulLength + ( ( uint16_t ) ucProtocol ) );
/* And then continue at the IPv4 source and destination addresses. */
usChecksum = ( uint16_t )
( ~usGenerateChecksum( ( uint32_t ) usChecksum, ( uint8_t * )&( pxIPPacket->xIPHeader.ulSourceIPAddress ),
( 2u * sizeof( pxIPPacket->xIPHeader.ulSourceIPAddress ) + ulLength ) ) );
/* Sum TCP header and data. */
}
if( xOutgoingPacket == pdFALSE )
{
/* This is in incoming packet. If the CRC is correct, it should be zero. */
if( usChecksum == 0u )
{
usChecksum = ( uint16_t )ipCORRECT_CRC;
}
}
else
{
if( ( usChecksum == 0u ) && ( ucProtocol == ( uint8_t ) ipPROTOCOL_UDP ) )
{
/* In case of UDP, a calculated checksum of 0x0000 is transmitted
as 0xffff. A value of zero would mean that the checksum is not used. */
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
if( xOutgoingPacket != pdFALSE )
{
FreeRTOS_debug_printf( ( "usGenerateProtocolChecksum[%s]: crc swap: %04X\n", pcType, usChecksum ) );
}
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
usChecksum = ( uint16_t )0xffffu;
}
}
usChecksum = FreeRTOS_htons( usChecksum );
if( xOutgoingPacket != pdFALSE )
{
*( pusChecksum ) = usChecksum;
}
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
else if( ( xOutgoingPacket == pdFALSE ) && ( usChecksum != ipCORRECT_CRC ) )
{
FreeRTOS_debug_printf( ( "usGenerateProtocolChecksum[%s]: ID %04X: from %lxip to %lxip bad crc: %04X\n",
pcType,
FreeRTOS_ntohs( pxIPPacket->xIPHeader.usIdentification ),
FreeRTOS_ntohl( pxIPPacket->xIPHeader.ulSourceIPAddress ),
FreeRTOS_ntohl( pxIPPacket->xIPHeader.ulDestinationIPAddress ),
FreeRTOS_ntohs( *pusChecksum ) ) );
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
return usChecksum;
}
/*-----------------------------------------------------------*/
/**
* This method generates a checksum for a given IPv4 header, per RFC791 (page 14).
* The checksum algorithm is decribed as:
* "[T]he 16 bit one's complement of the one's complement sum of all 16 bit words in the
* header. For purposes of computing the checksum, the value of the checksum field is zero."
*
* In a nutshell, that means that each 16-bit 'word' must be summed, after which
* the number of 'carries' (overflows) is added to the result. If that addition
* produces an overflow, that 'carry' must also be added to the final result. The final checksum
* should be the bitwise 'not' (ones-complement) of the result if the packet is
* meant to be transmitted, but this method simply returns the raw value, probably
* because when a packet is received, the checksum is verified by checking that
* ((received & calculated) == 0) without applying a bitwise 'not' to the 'calculated' checksum.
*
* This logic is optimized for microcontrollers which have limited resources, so the logic looks odd.
* It iterates over the full range of 16-bit words, but it does so by processing several 32-bit
* words at once whenever possible. Its first step is to align the memory pointer to a 32-bit boundary,
* after which it runs a fast loop to process multiple 32-bit words at once and adding their 'carries'.
* Finally, it finishes up by processing any remaining 16-bit words, and adding up all of the 'carries'.
* With 32-bit arithmetic, the number of 16-bit 'carries' produced by sequential additions can be found
* by looking at the 16 most-significant bits of the 32-bit integer, since a 32-bit int will continue
* counting up instead of overflowing after 16 bits. That is why the actual checksum calculations look like:
* union.u32 = ( uint32_t ) union.u16[ 0 ] + union.u16[ 1 ];
*
* Arguments:
* ulSum: This argument provides a value to initialize the progressive summation
* of the header's values to. It is often 0, but protocols like TCP or UDP
* can have pseudo-header fields which need to be included in the checksum.
* pucNextData: This argument contains the address of the first byte which this
* method should process. The method's memory iterator is initialized to this value.
* uxDataLengthBytes: This argument contains the number of bytes that this method
* should process.
*/
uint16_t usGenerateChecksum( uint32_t ulSum, const uint8_t * pucNextData, size_t uxDataLengthBytes )
{
xUnion32 xSum2, xSum, xTerm;
xUnionPtr xSource; /* Points to first byte */
xUnionPtr xLastSource; /* Points to last byte plus one */
uint32_t ulAlignBits, ulCarry = 0ul;
/* Small MCUs often spend up to 30% of the time doing checksum calculations
This function is optimised for 32-bit CPUs; Each time it will try to fetch
32-bits, sums it with an accumulator and counts the number of carries. */
/* Swap the input (little endian platform only). */
xSum.u32 = FreeRTOS_ntohs( ulSum );
xTerm.u32 = 0ul;
xSource.u8ptr = ( uint8_t * ) pucNextData;
ulAlignBits = ( ( ( uint32_t ) pucNextData ) & 0x03u ); /* gives 0, 1, 2, or 3 */
/* If byte (8-bit) aligned... */
if( ( ( ulAlignBits & 1ul ) != 0ul ) && ( uxDataLengthBytes >= ( size_t ) 1 ) )
{
xTerm.u8[ 1 ] = *( xSource.u8ptr );
( xSource.u8ptr )++;
uxDataLengthBytes--;
/* Now xSource is word (16-bit) aligned. */
}
/* If half-word (16-bit) aligned... */
if( ( ( ulAlignBits == 1u ) || ( ulAlignBits == 2u ) ) && ( uxDataLengthBytes >= 2u ) )
{
xSum.u32 += *(xSource.u16ptr);
( xSource.u16ptr )++;
uxDataLengthBytes -= 2u;
/* Now xSource is word (32-bit) aligned. */
}
/* Word (32-bit) aligned, do the most part. */
xLastSource.u32ptr = ( xSource.u32ptr + ( uxDataLengthBytes / 4u ) ) - 3u;
/* In this loop, four 32-bit additions will be done, in total 16 bytes.
Indexing with constants (0,1,2,3) gives faster code than using
post-increments. */
while( xSource.u32ptr < xLastSource.u32ptr )
{
/* Use a secondary Sum2, just to see if the addition produced an
overflow. */
xSum2.u32 = xSum.u32 + xSource.u32ptr[ 0 ];
if( xSum2.u32 < xSum.u32 )
{
ulCarry++;
}
/* Now add the secondary sum to the major sum, and remember if there was
a carry. */
xSum.u32 = xSum2.u32 + xSource.u32ptr[ 1 ];
if( xSum2.u32 > xSum.u32 )
{
ulCarry++;
}
/* And do the same trick once again for indexes 2 and 3 */
xSum2.u32 = xSum.u32 + xSource.u32ptr[ 2 ];
if( xSum2.u32 < xSum.u32 )
{
ulCarry++;
}
xSum.u32 = xSum2.u32 + xSource.u32ptr[ 3 ];
if( xSum2.u32 > xSum.u32 )
{
ulCarry++;
}
/* And finally advance the pointer 4 * 4 = 16 bytes. */
xSource.u32ptr += 4;
}
/* Now add all carries. */
xSum.u32 = ( uint32_t )xSum.u16[ 0 ] + xSum.u16[ 1 ] + ulCarry;
uxDataLengthBytes %= 16u;
xLastSource.u8ptr = ( uint8_t * ) ( xSource.u8ptr + ( uxDataLengthBytes & ~( ( size_t ) 1 ) ) );
/* Half-word aligned. */
while( xSource.u16ptr < xLastSource.u16ptr )
{
/* At least one more short. */
xSum.u32 += xSource.u16ptr[ 0 ];
xSource.u16ptr++;
}
if( ( uxDataLengthBytes & ( size_t ) 1 ) != 0u ) /* Maybe one more ? */
{
xTerm.u8[ 0 ] = xSource.u8ptr[ 0 ];
}
xSum.u32 += xTerm.u32;
/* Now add all carries again. */
xSum.u32 = ( uint32_t ) xSum.u16[ 0 ] + xSum.u16[ 1 ];
/* The previous summation might have given a 16-bit carry. */
xSum.u32 = ( uint32_t ) xSum.u16[ 0 ] + xSum.u16[ 1 ];
if( ( ulAlignBits & 1u ) != 0u )
{
/* Quite unlikely, but pucNextData might be non-aligned, which would
mean that a checksum is calculated starting at an odd position. */
xSum.u32 = ( ( xSum.u32 & 0xffu ) << 8 ) | ( ( xSum.u32 & 0xff00u ) >> 8 );
}
/* swap the output (little endian platform only). */
return FreeRTOS_htons( ( (uint16_t) xSum.u32 ) );
}
/*-----------------------------------------------------------*/
void vReturnEthernetFrame( NetworkBufferDescriptor_t * pxNetworkBuffer, BaseType_t xReleaseAfterSend )
{
EthernetHeader_t *pxEthernetHeader;
#if( ipconfigZERO_COPY_TX_DRIVER != 0 )
NetworkBufferDescriptor_t *pxNewBuffer;
#endif
#if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
{
if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
{
BaseType_t xIndex;
FreeRTOS_printf( ( "vReturnEthernetFrame: length %lu\n", ( uint32_t )pxNetworkBuffer->xDataLength ) );
for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
{
pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
}
pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
}
}
#endif
#if( ipconfigZERO_COPY_TX_DRIVER != 0 )
if( xReleaseAfterSend == pdFALSE )
{
pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, ( BaseType_t ) pxNetworkBuffer->xDataLength );
xReleaseAfterSend = pdTRUE;
pxNetworkBuffer = pxNewBuffer;
}
if( pxNetworkBuffer != NULL )
#endif
{
pxEthernetHeader = ( EthernetHeader_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
/* Swap source and destination MAC addresses. */
memcpy( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) &( pxEthernetHeader->xSourceAddress ), sizeof( pxEthernetHeader->xDestinationAddress ) );
memcpy( ( void * ) &( pxEthernetHeader->xSourceAddress) , ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
/* Send! */
xNetworkInterfaceOutput( pxNetworkBuffer, xReleaseAfterSend );
}
}
/*-----------------------------------------------------------*/
uint32_t FreeRTOS_GetIPAddress( void )
{
/* Returns the IP address of the NIC. */
return *ipLOCAL_IP_ADDRESS_POINTER;
}
/*-----------------------------------------------------------*/
void FreeRTOS_SetIPAddress( uint32_t ulIPAddress )
{
/* Sets the IP address of the NIC. */
*ipLOCAL_IP_ADDRESS_POINTER = ulIPAddress;
}
/*-----------------------------------------------------------*/
uint32_t FreeRTOS_GetGatewayAddress( void )
{
return xNetworkAddressing.ulGatewayAddress;
}
/*-----------------------------------------------------------*/
uint32_t FreeRTOS_GetDNSServerAddress( void )
{
return xNetworkAddressing.ulDNSServerAddress;
}
/*-----------------------------------------------------------*/
uint32_t FreeRTOS_GetNetmask( void )
{
return xNetworkAddressing.ulNetMask;
}
/*-----------------------------------------------------------*/
void FreeRTOS_UpdateMACAddress( const uint8_t ucMACAddress[ipMAC_ADDRESS_LENGTH_BYTES] )
{
/* Copy the MAC address at the start of the default packet header fragment. */
memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES );
}
/*-----------------------------------------------------------*/
const uint8_t * FreeRTOS_GetMACAddress( void )
{
return ipLOCAL_MAC_ADDRESS;
}
/*-----------------------------------------------------------*/
void FreeRTOS_SetNetmask ( uint32_t ulNetmask )
{
xNetworkAddressing.ulNetMask = ulNetmask;
}
/*-----------------------------------------------------------*/
void FreeRTOS_SetGatewayAddress ( uint32_t ulGatewayAddress )
{
xNetworkAddressing.ulGatewayAddress = ulGatewayAddress;
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_DHCP == 1 )
void vIPSetDHCPTimerEnableState( BaseType_t xEnableState )
{
if( xEnableState != pdFALSE )
{
xDHCPTimer.bActive = pdTRUE_UNSIGNED;
}
else
{
xDHCPTimer.bActive = pdFALSE_UNSIGNED;
}
}
#endif /* ipconfigUSE_DHCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_DHCP == 1 )
void vIPReloadDHCPTimer( uint32_t ulLeaseTime )
{
prvIPTimerReload( &xDHCPTimer, ulLeaseTime );
}
#endif /* ipconfigUSE_DHCP */
/*-----------------------------------------------------------*/
#if( ipconfigDNS_USE_CALLBACKS == 1 )
void vIPSetDnsTimerEnableState( BaseType_t xEnableState )
{
if( xEnableState != 0 )
{
xDNSTimer.bActive = pdTRUE;
}
else
{
xDNSTimer.bActive = pdFALSE;
}
}
#endif /* ipconfigUSE_DHCP */
/*-----------------------------------------------------------*/
#if( ipconfigDNS_USE_CALLBACKS != 0 )
void vIPReloadDNSTimer( uint32_t ulCheckTime )
{
prvIPTimerReload( &xDNSTimer, ulCheckTime );
}
#endif /* ipconfigDNS_USE_CALLBACKS != 0 */
/*-----------------------------------------------------------*/
BaseType_t xIPIsNetworkTaskReady( void )
{
return xIPTaskInitialised;
}
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_IsNetworkUp( void )
{
return xNetworkUp;
}
/*-----------------------------------------------------------*/
#if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
UBaseType_t uxGetMinimumIPQueueSpace( void )
{
return uxQueueMinimumSpace;
}
#endif
/*-----------------------------------------------------------*/
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_DNS.h"
#include "NetworkBufferManagement.h"
/* The ItemValue of the sockets xBoundSocketListItem member holds the socket's
port number. */
#define socketSET_SOCKET_PORT( pxSocket, usPort ) listSET_LIST_ITEM_VALUE( ( &( ( pxSocket )->xBoundSocketListItem ) ), ( usPort ) )
#define socketGET_SOCKET_PORT( pxSocket ) listGET_LIST_ITEM_VALUE( ( &( ( pxSocket )->xBoundSocketListItem ) ) )
/* Test if a socket it bound which means it is either included in
xBoundUDPSocketsList or xBoundTCPSocketsList */
#define socketSOCKET_IS_BOUND( pxSocket ) ( listLIST_ITEM_CONTAINER( & ( pxSocket )->xBoundSocketListItem ) != NULL )
/* If FreeRTOS_sendto() is called on a socket that is not bound to a port
number then, depending on the FreeRTOSIPConfig.h settings, it might be that a
port number is automatically generated for the socket. Automatically generated
port numbers will be between socketAUTO_PORT_ALLOCATION_START_NUMBER and
0xffff.
Per https://tools.ietf.org/html/rfc6056, "the dynamic ports consist of the range
49152-65535. However, ephemeral port selection algorithms should use the whole
range 1024-65535" excluding those already in use (inbound or outbound). */
#if !defined( socketAUTO_PORT_ALLOCATION_START_NUMBER )
#define socketAUTO_PORT_ALLOCATION_START_NUMBER ( ( uint16_t ) 0x0400 )
#endif
#define socketAUTO_PORT_ALLOCATION_MAX_NUMBER ( ( uint16_t ) 0xffff )
/* The number of octets that make up an IP address. */
#define socketMAX_IP_ADDRESS_OCTETS 4u
/* A block time of 0 simply means "don't block". */
#define socketDONT_BLOCK ( ( TickType_t ) 0 )
#if( ( ipconfigUSE_TCP == 1 ) && !defined( ipTCP_TIMER_PERIOD_MS ) )
#define ipTCP_TIMER_PERIOD_MS ( 1000 )
#endif
/* The next private port number to use when binding a client socket is stored in
the usNextPortToUse[] array - which has either 1 or two indexes depending on
whether TCP is being supported. */
#if( ipconfigUSE_TCP == 1 )
#define socketPROTOCOL_COUNT 2
#else
#define socketPROTOCOL_COUNT 1
#endif
/* Indexes into the usNextPortToUse[] array for UDP and TCP sockets
respectively. */
#define socketNEXT_UDP_PORT_NUMBER_INDEX 0
#define socketNEXT_TCP_PORT_NUMBER_INDEX 1
/*-----------------------------------------------------------*/
/*
* Allocate the next port number from the private allocation range.
* TCP and UDP each have their own series of port numbers
* ulProtocol is either ipPROTOCOL_UDP or ipPROTOCOL_TCP
*/
static uint16_t prvGetPrivatePortNumber( BaseType_t xProtocol );
/*
* Return the list item from within pxList that has an item value of
* xWantedItemValue. If there is no such list item return NULL.
*/
static const ListItem_t * pxListFindListItemWithValue( const List_t *pxList, TickType_t xWantedItemValue );
/*
* Return pdTRUE only if pxSocket is valid and bound, as far as can be
* determined.
*/
static BaseType_t prvValidSocket( FreeRTOS_Socket_t *pxSocket, BaseType_t xProtocol, BaseType_t xIsBound );
/*
* Before creating a socket, check the validity of the parameters used
* and find the size of the socket space, which is different for UDP and TCP
*/
static BaseType_t prvDetermineSocketSize( BaseType_t xDomain, BaseType_t xType, BaseType_t xProtocol, size_t *pxSocketSize );
#if( ipconfigUSE_TCP == 1 )
/*
* Create a txStream or a rxStream, depending on the parameter 'xIsInputStream'
*/
static StreamBuffer_t *prvTCPCreateStream (FreeRTOS_Socket_t *pxSocket, BaseType_t xIsInputStream );
#endif /* ipconfigUSE_TCP == 1 */
#if( ipconfigUSE_TCP == 1 )
/*
* Called from FreeRTOS_send(): some checks which will be done before
* sending a TCP packed.
*/
static int32_t prvTCPSendCheck( FreeRTOS_Socket_t *pxSocket, size_t xDataLength );
#endif /* ipconfigUSE_TCP */
#if( ipconfigUSE_TCP == 1 )
/*
* When a child socket gets closed, make sure to update the child-count of the parent
*/
static void prvTCPSetSocketCount( FreeRTOS_Socket_t *pxSocketToDelete );
#endif /* ipconfigUSE_TCP == 1 */
#if( ipconfigUSE_TCP == 1 )
/*
* Called from FreeRTOS_connect(): make some checks and if allowed, send a
* message to the IP-task to start connecting to a remote socket
*/
static BaseType_t prvTCPConnectStart( FreeRTOS_Socket_t *pxSocket, struct freertos_sockaddr *pxAddress );
#endif /* ipconfigUSE_TCP */
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
/* Executed by the IP-task, it will check all sockets belonging to a set */
static FreeRTOS_Socket_t *prvFindSelectedSocket( SocketSelect_t *pxSocketSet );
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
/* The list that contains mappings between sockets and port numbers. Accesses
to this list must be protected by critical sections of one kind or another. */
List_t xBoundUDPSocketsList;
#if ipconfigUSE_TCP == 1
List_t xBoundTCPSocketsList;
#endif /* ipconfigUSE_TCP == 1 */
/*-----------------------------------------------------------*/
static BaseType_t prvValidSocket( FreeRTOS_Socket_t *pxSocket, BaseType_t xProtocol, BaseType_t xIsBound )
{
BaseType_t xReturn = pdTRUE;
if( ( pxSocket == NULL ) || ( pxSocket == FREERTOS_INVALID_SOCKET ) )
{
xReturn = pdFALSE;
}
else if( ( xIsBound != pdFALSE ) && ( socketSOCKET_IS_BOUND( pxSocket ) == pdFALSE ) )
{
/* The caller expects the socket to be bound, but it isn't. */
xReturn = pdFALSE;
}
else if( pxSocket->ucProtocol != ( uint8_t ) xProtocol )
{
/* Socket has a wrong type (UDP != TCP). */
xReturn = pdFALSE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
BaseType_t vNetworkSocketsInit( void )
{
vListInitialise( &xBoundUDPSocketsList );
#if( ipconfigUSE_TCP == 1 )
{
vListInitialise( &xBoundTCPSocketsList );
}
#endif /* ipconfigUSE_TCP == 1 */
return pdTRUE;
}
/*-----------------------------------------------------------*/
static BaseType_t prvDetermineSocketSize( BaseType_t xDomain, BaseType_t xType, BaseType_t xProtocol, size_t *pxSocketSize )
{
BaseType_t xReturn = pdPASS;
FreeRTOS_Socket_t *pxSocket;
/* Asserts must not appear before it has been determined that the network
task is ready - otherwise the asserts will fail. */
if( xIPIsNetworkTaskReady() == pdFALSE )
{
xReturn = pdFAIL;
}
else
{
/* Only Ethernet is currently supported. */
configASSERT( xDomain == FREERTOS_AF_INET );
/* Check if the UDP socket-list has been initialised. */
configASSERT( listLIST_IS_INITIALISED( &xBoundUDPSocketsList ) );
#if( ipconfigUSE_TCP == 1 )
{
/* Check if the TCP socket-list has been initialised. */
configASSERT( listLIST_IS_INITIALISED( &xBoundTCPSocketsList ) );
}
#endif /* ipconfigUSE_TCP == 1 */
if( xProtocol == FREERTOS_IPPROTO_UDP )
{
if( xType != FREERTOS_SOCK_DGRAM )
{
xReturn = pdFAIL;
configASSERT( xReturn );
}
/* In case a UDP socket is created, do not allocate space for TCP data. */
*pxSocketSize = ( sizeof( *pxSocket ) - sizeof( pxSocket->u ) ) + sizeof( pxSocket->u.xUDP );
}
#if( ipconfigUSE_TCP == 1 )
else if( xProtocol == FREERTOS_IPPROTO_TCP )
{
if( xType != FREERTOS_SOCK_STREAM )
{
xReturn = pdFAIL;
configASSERT( xReturn );
}
*pxSocketSize = ( sizeof( *pxSocket ) - sizeof( pxSocket->u ) ) + sizeof( pxSocket->u.xTCP );
}
#endif /* ipconfigUSE_TCP == 1 */
else
{
xReturn = pdFAIL;
configASSERT( xReturn );
}
}
/* In case configASSERT() is not used */
( void )xDomain;
return xReturn;
}
/*-----------------------------------------------------------*/
/* FreeRTOS_socket() allocates and initiates a socket */
Socket_t FreeRTOS_socket( BaseType_t xDomain, BaseType_t xType, BaseType_t xProtocol )
{
FreeRTOS_Socket_t *pxSocket;
size_t uxSocketSize;
EventGroupHandle_t xEventGroup;
Socket_t xReturn;
if( prvDetermineSocketSize( xDomain, xType, xProtocol, &uxSocketSize ) == pdFAIL )
{
xReturn = FREERTOS_INVALID_SOCKET;
}
else
{
/* Allocate the structure that will hold the socket information. The
size depends on the type of socket: UDP sockets need less space. A
define 'pvPortMallocSocket' will used to allocate the necessary space.
By default it points to the FreeRTOS function 'pvPortMalloc()'. */
pxSocket = ( FreeRTOS_Socket_t * ) pvPortMallocSocket( uxSocketSize );
if( pxSocket == NULL )
{
pxSocket = ( FreeRTOS_Socket_t * ) FREERTOS_INVALID_SOCKET;
iptraceFAILED_TO_CREATE_SOCKET();
}
else if( ( xEventGroup = xEventGroupCreate() ) == NULL )
{
vPortFreeSocket( pxSocket );
pxSocket = ( FreeRTOS_Socket_t * ) FREERTOS_INVALID_SOCKET;
iptraceFAILED_TO_CREATE_EVENT_GROUP();
}
else
{
/* Clear the entire space to avoid nulling individual entries */
memset( pxSocket, '\0', uxSocketSize );
pxSocket->xEventGroup = xEventGroup;
/* Initialise the socket's members. The semaphore will be created
if the socket is bound to an address, for now the pointer to the
semaphore is just set to NULL to show it has not been created. */
if( xProtocol == FREERTOS_IPPROTO_UDP )
{
vListInitialise( &( pxSocket->u.xUDP.xWaitingPacketsList ) );
#if( ipconfigUDP_MAX_RX_PACKETS > 0 )
{
pxSocket->u.xUDP.uxMaxPackets = ( UBaseType_t ) ipconfigUDP_MAX_RX_PACKETS;
}
#endif /* ipconfigUDP_MAX_RX_PACKETS > 0 */
}
vListInitialiseItem( &( pxSocket->xBoundSocketListItem ) );
listSET_LIST_ITEM_OWNER( &( pxSocket->xBoundSocketListItem ), ( void * ) pxSocket );
pxSocket->xReceiveBlockTime = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME;
pxSocket->xSendBlockTime = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
pxSocket->ucSocketOptions = ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT;
pxSocket->ucProtocol = ( uint8_t ) xProtocol; /* protocol: UDP or TCP */
#if( ipconfigUSE_TCP == 1 )
{
if( xProtocol == FREERTOS_IPPROTO_TCP )
{
/* StreamSize is expressed in number of bytes */
/* Round up buffer sizes to nearest multiple of MSS */
pxSocket->u.xTCP.usInitMSS = pxSocket->u.xTCP.usCurMSS = ipconfigTCP_MSS;
pxSocket->u.xTCP.uxRxStreamSize = ( size_t ) ipconfigTCP_RX_BUFFER_LENGTH;
pxSocket->u.xTCP.uxTxStreamSize = ( size_t ) FreeRTOS_round_up( ipconfigTCP_TX_BUFFER_LENGTH, ipconfigTCP_MSS );
/* Use half of the buffer size of the TCP windows */
#if ( ipconfigUSE_TCP_WIN == 1 )
{
pxSocket->u.xTCP.uxRxWinSize = FreeRTOS_max_uint32( 1UL, ( uint32_t ) ( pxSocket->u.xTCP.uxRxStreamSize / 2 ) / ipconfigTCP_MSS );
pxSocket->u.xTCP.uxTxWinSize = FreeRTOS_max_uint32( 1UL, ( uint32_t ) ( pxSocket->u.xTCP.uxTxStreamSize / 2 ) / ipconfigTCP_MSS );
}
#else
{
pxSocket->u.xTCP.uxRxWinSize = 1u;
pxSocket->u.xTCP.uxTxWinSize = 1u;
}
#endif
/* The above values are just defaults, and can be overridden by
calling FreeRTOS_setsockopt(). No buffers will be allocated until a
socket is connected and data is exchanged. */
}
}
#endif /* ipconfigUSE_TCP == 1 */
}
xReturn = ( Socket_t ) pxSocket;
}
/* Remove compiler warnings in the case the configASSERT() is not defined. */
( void ) xDomain;
return xReturn;
}
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
SocketSet_t FreeRTOS_CreateSocketSet( void )
{
SocketSelect_t *pxSocketSet;
pxSocketSet = ( SocketSelect_t * ) pvPortMalloc( sizeof( *pxSocketSet ) );
if( pxSocketSet != NULL )
{
memset( pxSocketSet, '\0', sizeof( *pxSocketSet ) );
pxSocketSet->xSelectGroup = xEventGroupCreate();
if( pxSocketSet->xSelectGroup == NULL )
{
vPortFree( ( void* ) pxSocketSet );
pxSocketSet = NULL;
}
}
return ( SocketSet_t * ) pxSocketSet;
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
void FreeRTOS_DeleteSocketSet( SocketSet_t xSocketSet )
{
SocketSelect_t *pxSocketSet = ( SocketSelect_t*) xSocketSet;
vEventGroupDelete( pxSocketSet->xSelectGroup );
vPortFree( ( void* ) pxSocketSet );
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
/* Add a socket to a set */
void FreeRTOS_FD_SET( Socket_t xSocket, SocketSet_t xSocketSet, EventBits_t xSelectBits )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
SocketSelect_t *pxSocketSet = ( SocketSelect_t * ) xSocketSet;
configASSERT( pxSocket != NULL );
configASSERT( xSocketSet != NULL );
/* Make sure we're not adding bits which are reserved for internal use,
such as eSELECT_CALL_IP */
pxSocket->xSelectBits |= ( xSelectBits & eSELECT_ALL );
if( ( pxSocket->xSelectBits & eSELECT_ALL ) != 0 )
{
/* Adding a socket to a socket set. */
pxSocket->pxSocketSet = ( SocketSelect_t * ) xSocketSet;
/* Now have the IP-task call vSocketSelect() to see if the set contains
any sockets which are 'ready' and set the proper bits.
By setting 'bApiCalled = false', vSocketSelect() knows that it was
not called from a user API */
pxSocketSet->bApiCalled = pdFALSE;
prvFindSelectedSocket( pxSocketSet );
}
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
/* Clear select bits for a socket
If the mask becomes 0, remove the socket from the set */
void FreeRTOS_FD_CLR( Socket_t xSocket, SocketSet_t xSocketSet, EventBits_t xSelectBits )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
configASSERT( pxSocket != NULL );
configASSERT( xSocketSet != NULL );
pxSocket->xSelectBits &= ~( xSelectBits & eSELECT_ALL );
if( ( pxSocket->xSelectBits & eSELECT_ALL ) != 0 )
{
pxSocket->pxSocketSet = ( SocketSelect_t *)xSocketSet;
}
else
{
/* disconnect it from the socket set */
pxSocket->pxSocketSet = ( SocketSelect_t *)NULL;
}
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
/* Test if a socket belongs to a socket-set */
EventBits_t FreeRTOS_FD_ISSET( Socket_t xSocket, SocketSet_t xSocketSet )
{
EventBits_t xReturn;
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
configASSERT( pxSocket != NULL );
configASSERT( xSocketSet != NULL );
if( xSocketSet == ( SocketSet_t ) pxSocket->pxSocketSet )
{
/* Make sure we're not adding bits which are reserved for internal
use. */
xReturn = pxSocket->xSocketBits & eSELECT_ALL;
}
else
{
xReturn = 0;
}
return xReturn;
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
/* The select() statement: wait for an event to occur on any of the sockets
included in a socket set */
BaseType_t FreeRTOS_select( SocketSet_t xSocketSet, TickType_t xBlockTimeTicks )
{
TimeOut_t xTimeOut;
TickType_t xRemainingTime;
SocketSelect_t *pxSocketSet = ( SocketSelect_t*) xSocketSet;
BaseType_t xResult;
configASSERT( xSocketSet != NULL );
/* Only in the first round, check for non-blocking */
xRemainingTime = xBlockTimeTicks;
/* Fetch the current time */
vTaskSetTimeOutState( &xTimeOut );
for( ;; )
{
/* Find a socket which might have triggered the bit
This function might return immediately or block for a limited time */
xResult = ( BaseType_t ) xEventGroupWaitBits( pxSocketSet->xSelectGroup, eSELECT_ALL, pdFALSE, pdFALSE, xRemainingTime );
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
if( ( xResult & eSELECT_INTR ) != 0u )
{
xEventGroupClearBits( pxSocketSet->xSelectGroup, eSELECT_INTR );
FreeRTOS_debug_printf( ( "FreeRTOS_select: interrupted\n" ) );
break;
}
}
#endif /* ipconfigSUPPORT_SIGNALS */
/* Have the IP-task find the socket which had an event */
pxSocketSet->bApiCalled = pdTRUE;
prvFindSelectedSocket( pxSocketSet );
xResult = ( BaseType_t ) xEventGroupGetBits( pxSocketSet->xSelectGroup );
if( xResult != 0 )
{
break;
}
/* Has the timeout been reached? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) != pdFALSE )
{
break;
}
}
return xResult;
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
/* Send a message to the IP-task to have it check all sockets belonging to
'pxSocketSet' */
static FreeRTOS_Socket_t *prvFindSelectedSocket( SocketSelect_t *pxSocketSet )
{
IPStackEvent_t xSelectEvent;
FreeRTOS_Socket_t *xReturn;
xSelectEvent.eEventType = eSocketSelectEvent;
xSelectEvent.pvData = ( void * ) pxSocketSet;
/* while the IP-task works on the request, the API will block on
'eSELECT_CALL_IP'. So clear it first. */
xEventGroupClearBits( pxSocketSet->xSelectGroup, eSELECT_CALL_IP );
/* Now send the socket select event */
if( xSendEventStructToIPTask( &xSelectEvent, ( TickType_t ) portMAX_DELAY ) == pdFAIL )
{
/* Oops, we failed to wake-up the IP task. No use to wait for it. */
FreeRTOS_debug_printf( ( "prvFindSelectedSocket: failed\n" ) );
xReturn = NULL;
}
else
{
/* As soon as the IP-task is ready, it will set 'eSELECT_CALL_IP' to
wakeup the calling API */
xEventGroupWaitBits( pxSocketSet->xSelectGroup, eSELECT_CALL_IP, pdTRUE, pdFALSE, portMAX_DELAY );
/* Return 'pxSocket' which is set by the IP-task */
xReturn = pxSocketSet->pxSocket;
}
return xReturn;
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
/*
* FreeRTOS_recvfrom: receive data from a bound socket
* In this library, the function can only be used with connectionsless sockets
* (UDP)
*/
int32_t FreeRTOS_recvfrom( Socket_t xSocket, void *pvBuffer, size_t xBufferLength, BaseType_t xFlags, struct freertos_sockaddr *pxSourceAddress, socklen_t *pxSourceAddressLength )
{
BaseType_t lPacketCount = 0;
NetworkBufferDescriptor_t *pxNetworkBuffer;
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
TickType_t xRemainingTime = ( TickType_t ) 0; /* Obsolete assignment, but some compilers output a warning if its not done. */
BaseType_t xTimed = pdFALSE;
TimeOut_t xTimeOut;
int32_t lReturn;
EventBits_t xEventBits = ( EventBits_t ) 0;
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_UDP, pdTRUE ) == pdFALSE )
{
return -pdFREERTOS_ERRNO_EINVAL;
}
lPacketCount = ( BaseType_t ) listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) );
/* The function prototype is designed to maintain the expected Berkeley
sockets standard, but this implementation does not use all the parameters. */
( void ) pxSourceAddressLength;
while( lPacketCount == 0 )
{
if( xTimed == pdFALSE )
{
/* Check to see if the socket is non blocking on the first
iteration. */
xRemainingTime = pxSocket->xReceiveBlockTime;
if( xRemainingTime == ( TickType_t ) 0 )
{
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
/* Just check for the interrupt flag. */
xEventBits = xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_INTR,
pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, socketDONT_BLOCK );
}
#endif /* ipconfigSUPPORT_SIGNALS */
break;
}
if( ( xFlags & FREERTOS_MSG_DONTWAIT ) != 0 )
{
break;
}
/* To ensure this part only executes once. */
xTimed = pdTRUE;
/* Fetch the current time. */
vTaskSetTimeOutState( &xTimeOut );
}
/* Wait for arrival of data. While waiting, the IP-task may set the
'eSOCKET_RECEIVE' bit in 'xEventGroup', if it receives data for this
socket, thus unblocking this API call. */
xEventBits = xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_RECEIVE | eSOCKET_INTR,
pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
if( ( xEventBits & eSOCKET_INTR ) != 0 )
{
if( ( xEventBits & eSOCKET_RECEIVE ) != 0 )
{
/* Shouldn't have cleared the eSOCKET_RECEIVE flag. */
xEventGroupSetBits( pxSocket->xEventGroup, eSOCKET_RECEIVE );
}
break;
}
}
#else
{
( void ) xEventBits;
}
#endif /* ipconfigSUPPORT_SIGNALS */
lPacketCount = ( BaseType_t ) listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) );
if( lPacketCount != 0 )
{
break;
}
/* Has the timeout been reached ? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) )
{
break;
}
} /* while( lPacketCount == 0 ) */
if( lPacketCount != 0 )
{
taskENTER_CRITICAL();
{
/* The owner of the list item is the network buffer. */
pxNetworkBuffer = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &( pxSocket->u.xUDP.xWaitingPacketsList ) );
if( ( xFlags & FREERTOS_MSG_PEEK ) == 0 )
{
/* Remove the network buffer from the list of buffers waiting to
be processed by the socket. */
uxListRemove( &( pxNetworkBuffer->xBufferListItem ) );
}
}
taskEXIT_CRITICAL();
/* The returned value is the data length, which may have been capped to
the receive buffer size. */
lReturn = ( int32_t ) pxNetworkBuffer->xDataLength;
if( pxSourceAddress != NULL )
{
pxSourceAddress->sin_port = pxNetworkBuffer->usPort;
pxSourceAddress->sin_addr = pxNetworkBuffer->ulIPAddress;
}
if( ( xFlags & FREERTOS_ZERO_COPY ) == 0 )
{
/* The zero copy flag is not set. Truncate the length if it won't
fit in the provided buffer. */
if( lReturn > ( int32_t ) xBufferLength )
{
iptraceRECVFROM_DISCARDING_BYTES( ( xBufferLength - lReturn ) );
lReturn = ( int32_t )xBufferLength;
}
/* Copy the received data into the provided buffer, then release the
network buffer. */
memcpy( pvBuffer, ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] ), ( size_t )lReturn );
if( ( xFlags & FREERTOS_MSG_PEEK ) == 0 )
{
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
}
}
else
{
/* The zero copy flag was set. pvBuffer is not a buffer into which
the received data can be copied, but a pointer that must be set to
point to the buffer in which the received data has already been
placed. */
*( ( void** ) pvBuffer ) = ( void * ) ( &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] ) );
}
}
#if( ipconfigSUPPORT_SIGNALS != 0 )
else if( ( xEventBits & eSOCKET_INTR ) != 0 )
{
lReturn = -pdFREERTOS_ERRNO_EINTR;
iptraceRECVFROM_INTERRUPTED();
}
#endif /* ipconfigSUPPORT_SIGNALS */
else
{
lReturn = -pdFREERTOS_ERRNO_EWOULDBLOCK;
iptraceRECVFROM_TIMEOUT();
}
return lReturn;
}
/*-----------------------------------------------------------*/
int32_t FreeRTOS_sendto( Socket_t xSocket, const void *pvBuffer, size_t xTotalDataLength, BaseType_t xFlags, const struct freertos_sockaddr *pxDestinationAddress, socklen_t xDestinationAddressLength )
{
NetworkBufferDescriptor_t *pxNetworkBuffer;
IPStackEvent_t xStackTxEvent = { eStackTxEvent, NULL };
TimeOut_t xTimeOut;
TickType_t xTicksToWait;
int32_t lReturn = 0;
FreeRTOS_Socket_t *pxSocket;
pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
/* The function prototype is designed to maintain the expected Berkeley
sockets standard, but this implementation does not use all the
parameters. */
( void ) xDestinationAddressLength;
configASSERT( pvBuffer );
if( xTotalDataLength <= ( size_t ) ipMAX_UDP_PAYLOAD_LENGTH )
{
/* If the socket is not already bound to an address, bind it now.
Passing NULL as the address parameter tells FreeRTOS_bind() to select
the address to bind to. */
if( ( socketSOCKET_IS_BOUND( pxSocket ) != pdFALSE ) ||
( FreeRTOS_bind( xSocket, NULL, 0u ) == 0 ) )
{
xTicksToWait = pxSocket->xSendBlockTime;
#if( ipconfigUSE_CALLBACKS != 0 )
{
if( xIsCallingFromIPTask() != pdFALSE )
{
/* If this send function is called from within a call-back
handler it may not block, otherwise chances would be big to
get a deadlock: the IP-task waiting for itself. */
xTicksToWait = ( TickType_t )0;
}
}
#endif /* ipconfigUSE_CALLBACKS */
if( ( xFlags & FREERTOS_MSG_DONTWAIT ) != 0 )
{
xTicksToWait = ( TickType_t ) 0;
}
if( ( xFlags & FREERTOS_ZERO_COPY ) == 0 )
{
/* Zero copy is not set, so obtain a network buffer into
which the payload will be copied. */
vTaskSetTimeOutState( &xTimeOut );
/* Block until a buffer becomes available, or until a
timeout has been reached */
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( xTotalDataLength + sizeof( UDPPacket_t ), xTicksToWait );
if( pxNetworkBuffer != NULL )
{
memcpy( ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] ), ( void * ) pvBuffer, xTotalDataLength );
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdTRUE )
{
/* The entire block time has been used up. */
xTicksToWait = ( TickType_t ) 0;
}
}
}
else
{
/* When zero copy is used, pvBuffer is a pointer to the
payload of a buffer that has already been obtained from the
stack. Obtain the network buffer pointer from the buffer. */
pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( (void*)pvBuffer );
}
if( pxNetworkBuffer != NULL )
{
pxNetworkBuffer->xDataLength = xTotalDataLength;
pxNetworkBuffer->usPort = pxDestinationAddress->sin_port;
pxNetworkBuffer->usBoundPort = ( uint16_t ) socketGET_SOCKET_PORT( pxSocket );
pxNetworkBuffer->ulIPAddress = pxDestinationAddress->sin_addr;
/* The socket options are passed to the IP layer in the
space that will eventually get used by the Ethernet header. */
pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ] = pxSocket->ucSocketOptions;
/* Tell the networking task that the packet needs sending. */
xStackTxEvent.pvData = pxNetworkBuffer;
/* Ask the IP-task to send this packet */
if( xSendEventStructToIPTask( &xStackTxEvent, xTicksToWait ) == pdPASS )
{
/* The packet was successfully sent to the IP task. */
lReturn = ( int32_t ) xTotalDataLength;
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xUDP.pxHandleSent ) )
{
pxSocket->u.xUDP.pxHandleSent( (Socket_t *)pxSocket, xTotalDataLength );
}
}
#endif /* ipconfigUSE_CALLBACKS */
}
else
{
/* If the buffer was allocated in this function, release
it. */
if( ( xFlags & FREERTOS_ZERO_COPY ) == 0 )
{
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
}
iptraceSTACK_TX_EVENT_LOST( ipSTACK_TX_EVENT );
}
}
else
{
/* If errno was available, errno would be set to
FREERTOS_ENOPKTS. As it is, the function must return the
number of transmitted bytes, so the calling function knows
how much data was actually sent. */
iptraceNO_BUFFER_FOR_SENDTO();
}
}
else
{
iptraceSENDTO_SOCKET_NOT_BOUND();
}
}
else
{
/* The data is longer than the available buffer space. */
iptraceSENDTO_DATA_TOO_LONG();
}
return lReturn;
} /* Tested */
/*-----------------------------------------------------------*/
/*
* FreeRTOS_bind() : binds a sockt to a local port number. If port 0 is
* provided, a system provided port number will be assigned. This function can
* be used for both UDP and TCP sockets. The actual binding will be performed
* by the IP-task to avoid mutual access to the bound-socket-lists
* (xBoundUDPSocketsList or xBoundTCPSocketsList).
*/
BaseType_t FreeRTOS_bind( Socket_t xSocket, struct freertos_sockaddr * pxAddress, socklen_t xAddressLength )
{
IPStackEvent_t xBindEvent;
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn = 0;
( void ) xAddressLength;
if( ( pxSocket == NULL ) || ( pxSocket == FREERTOS_INVALID_SOCKET ) )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
/* Once a socket is bound to a port, it can not be bound to a different
port number */
else if( socketSOCKET_IS_BOUND( pxSocket) != pdFALSE )
{
/* The socket is already bound. */
FreeRTOS_debug_printf( ( "vSocketBind: Socket already bound to %d\n", pxSocket->usLocalPort ) );
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
/* Prepare a messages to the IP-task in order to perform the binding.
The desired port number will be passed in usLocalPort. */
xBindEvent.eEventType = eSocketBindEvent;
xBindEvent.pvData = ( void * ) xSocket;
if( pxAddress != NULL )
{
pxSocket->usLocalPort = FreeRTOS_ntohs( pxAddress->sin_port );
}
else
{
/* Caller wants to bind to a random port number. */
pxSocket->usLocalPort = 0u;
}
/* portMAX_DELAY is used as a the time-out parameter, as binding *must*
succeed before the socket can be used. _RB_ The use of an infinite
block time needs be changed as it could result in the task hanging. */
if( xSendEventStructToIPTask( &xBindEvent, ( TickType_t ) portMAX_DELAY ) == pdFAIL )
{
/* Failed to wake-up the IP-task, no use to wait for it */
FreeRTOS_debug_printf( ( "FreeRTOS_bind: send event failed\n" ) );
xReturn = -pdFREERTOS_ERRNO_ECANCELED;
}
else
{
/* The IP-task will set the 'eSOCKET_BOUND' bit when it has done its
job. */
xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_BOUND, pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, portMAX_DELAY );
if( socketSOCKET_IS_BOUND( pxSocket ) == pdFALSE )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
}
}
return xReturn;
}
/*
* vSocketBind(): internal version of bind() that should not be called directly.
* 'xInternal' is used for TCP sockets only: it allows to have several
* (connected) child sockets bound to the same server port.
*/
BaseType_t vSocketBind( FreeRTOS_Socket_t *pxSocket, struct freertos_sockaddr * pxAddress, size_t uxAddressLength, BaseType_t xInternal )
{
BaseType_t xReturn = 0; /* In Berkeley sockets, 0 means pass for bind(). */
List_t *pxSocketList;
#if( ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND == 1 )
struct freertos_sockaddr xAddress;
#endif /* ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND */
#if( ipconfigUSE_TCP == 1 )
if( pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
pxSocketList = &xBoundTCPSocketsList;
}
else
#endif /* ipconfigUSE_TCP == 1 */
{
pxSocketList = &xBoundUDPSocketsList;
}
/* The function prototype is designed to maintain the expected Berkeley
sockets standard, but this implementation does not use all the parameters. */
( void ) uxAddressLength;
configASSERT( pxSocket );
configASSERT( pxSocket != FREERTOS_INVALID_SOCKET );
#if( ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND == 1 )
{
/* pxAddress will be NULL if sendto() was called on a socket without the
socket being bound to an address. In this case, automatically allocate
an address and port to the socket. */
if( pxAddress == NULL )
{
pxAddress = &xAddress;
/* Put the port to zero to be assigned later. */
pxAddress->sin_port = 0u;
}
}
#endif /* ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND == 1 */
/* Sockets must be bound before calling FreeRTOS_sendto() if
ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is not set to 1. */
configASSERT( pxAddress );
if( pxAddress != NULL )
{
if( pxAddress->sin_port == 0u )
{
pxAddress->sin_port = prvGetPrivatePortNumber( ( BaseType_t )pxSocket->ucProtocol );
if( 0 == pxAddress->sin_port )
{
return -pdFREERTOS_ERRNO_EADDRNOTAVAIL;
}
}
/* If vSocketBind() is called from the API FreeRTOS_bind() it has been
confirmed that the socket was not yet bound to a port. If it is called
from the IP-task, no such check is necessary. */
/* Check to ensure the port is not already in use. If the bind is
called internally, a port MAY be used by more than one socket. */
if( ( ( xInternal == pdFALSE ) || ( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP ) ) &&
( pxListFindListItemWithValue( pxSocketList, ( TickType_t ) pxAddress->sin_port ) != NULL ) )
{
FreeRTOS_debug_printf( ( "vSocketBind: %sP port %d in use\n",
pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_TCP ? "TC" : "UD",
FreeRTOS_ntohs( pxAddress->sin_port ) ) );
xReturn = -pdFREERTOS_ERRNO_EADDRINUSE;
}
else
{
/* Allocate the port number to the socket.
This macro will set 'xBoundSocketListItem->xItemValue' */
socketSET_SOCKET_PORT( pxSocket, pxAddress->sin_port );
/* And also store it in a socket field 'usLocalPort' in host-byte-order,
mostly used for logging and debugging purposes */
pxSocket->usLocalPort = FreeRTOS_ntohs( pxAddress->sin_port );
/* Add the socket to the list of bound ports. */
{
/* If the network driver can iterate through 'xBoundUDPSocketsList',
by calling xPortHasUDPSocket() then the IP-task must temporarily
suspend the scheduler to keep the list in a consistent state. */
#if( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 1 )
{
vTaskSuspendAll();
}
#endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS */
/* Add the socket to 'xBoundUDPSocketsList' or 'xBoundTCPSocketsList' */
vListInsertEnd( pxSocketList, &( pxSocket->xBoundSocketListItem ) );
#if( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 1 )
{
xTaskResumeAll();
}
#endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS */
}
}
}
else
{
xReturn = -pdFREERTOS_ERRNO_EADDRNOTAVAIL;
FreeRTOS_debug_printf( ( "vSocketBind: Socket no addr\n" ) );
}
if( xReturn != 0 )
{
iptraceBIND_FAILED( xSocket, ( FreeRTOS_ntohs( pxAddress->sin_port ) ) );
}
return xReturn;
} /* Tested */
/*-----------------------------------------------------------*/
/*
* Close a socket and free the allocated space
* In case of a TCP socket: the connection will not be closed automatically
* Subsequent messages for the closed socket will be responded to with a RST
* The IP-task will actually close the socket, after receiving a 'eSocketCloseEvent' message
*/
BaseType_t FreeRTOS_closesocket( Socket_t xSocket )
{
BaseType_t xResult;
#if( ipconfigUSE_TCP == 1 ) && ( ipconfigUSE_CALLBACKS == 1 )
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * )xSocket;
#endif
IPStackEvent_t xCloseEvent;
xCloseEvent.eEventType = eSocketCloseEvent;
xCloseEvent.pvData = ( void * ) xSocket;
if( ( xSocket == NULL ) || ( xSocket == FREERTOS_INVALID_SOCKET ) )
{
xResult = 0;
}
else
{
#if( ( ipconfigUSE_TCP == 1 ) && ( ipconfigUSE_CALLBACKS == 1 ) )
{
if( pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
/* Make sure that IP-task won't call the user callback's anymore */
pxSocket->u.xTCP.pxHandleConnected = NULL;
pxSocket->u.xTCP.pxHandleReceive = NULL;
pxSocket->u.xTCP.pxHandleSent = NULL;
}
}
#endif /* ( ( ipconfigUSE_TCP == 1 ) && ( ipconfigUSE_CALLBACKS == 1 ) ) */
/* Let the IP task close the socket to keep it synchronised with the
packet handling. */
/* Note when changing the time-out value below, it must be checked who is calling
this function. If it is called by the IP-task, a deadlock could occur.
The IP-task would only call it in case of a user call-back */
if( xSendEventStructToIPTask( &xCloseEvent, ( TickType_t ) 0 ) == pdFAIL )
{
FreeRTOS_debug_printf( ( "FreeRTOS_closesocket: failed\n" ) );
xResult = -1;
}
else
{
xResult = 1;
}
}
return xResult;
}
/* This is the internal version of FreeRTOS_closesocket()
* It will be called by the IPtask only to avoid problems with synchronicity
*/
void *vSocketClose( FreeRTOS_Socket_t *pxSocket )
{
NetworkBufferDescriptor_t *pxNetworkBuffer;
#if( ipconfigUSE_TCP == 1 )
{
/* For TCP: clean up a little more. */
if( pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
#if( ipconfigUSE_TCP_WIN == 1 )
{
if( pxSocket->u.xTCP.pxAckMessage != NULL )
{
vReleaseNetworkBufferAndDescriptor( pxSocket->u.xTCP.pxAckMessage );
}
/* Free the resources which were claimed by the tcpWin member */
vTCPWindowDestroy( &pxSocket->u.xTCP.xTCPWindow );
}
#endif /* ipconfigUSE_TCP_WIN */
/* Free the input and output streams */
if( pxSocket->u.xTCP.rxStream != NULL )
{
vPortFreeLarge( pxSocket->u.xTCP.rxStream );
}
if( pxSocket->u.xTCP.txStream != NULL )
{
vPortFreeLarge( pxSocket->u.xTCP.txStream );
}
/* In case this is a child socket, make sure the child-count of the
parent socket is decreased. */
prvTCPSetSocketCount( pxSocket );
}
}
#endif /* ipconfigUSE_TCP == 1 */
/* Socket must be unbound first, to ensure no more packets are queued on
it. */
if( socketSOCKET_IS_BOUND( pxSocket ) != pdFALSE )
{
/* If the network driver can iterate through 'xBoundUDPSocketsList',
by calling xPortHasUDPSocket(), then the IP-task must temporarily
suspend the scheduler to keep the list in a consistent state. */
#if( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 1 )
{
vTaskSuspendAll();
}
#endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS */
uxListRemove( &( pxSocket->xBoundSocketListItem ) );
#if( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 1 )
{
xTaskResumeAll();
}
#endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS */
}
/* Now the socket is not bound the list of waiting packets can be
drained. */
if( pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_UDP )
{
while( listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ) > 0U )
{
pxNetworkBuffer = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &( pxSocket->u.xUDP.xWaitingPacketsList ) );
uxListRemove( &( pxNetworkBuffer->xBufferListItem ) );
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
}
}
if( pxSocket->xEventGroup )
{
vEventGroupDelete( pxSocket->xEventGroup );
}
#if( ipconfigUSE_TCP == 1 ) && ( ipconfigHAS_DEBUG_PRINTF != 0 )
{
if( pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
FreeRTOS_debug_printf( ( "FreeRTOS_closesocket[%u to %lxip:%u]: buffers %lu socks %lu\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort,
uxGetNumberOfFreeNetworkBuffers(),
listCURRENT_LIST_LENGTH( &xBoundTCPSocketsList ) ) );
}
}
#endif /* ( ipconfigUSE_TCP == 1 ) && ( ipconfigHAS_DEBUG_PRINTF != 0 ) */
/* Anf finally, after all resources have been freed, free the socket space */
vPortFreeSocket( pxSocket );
return 0;
} /* Tested */
/*-----------------------------------------------------------*/
#if ipconfigUSE_TCP == 1
/*
* When a child socket gets closed, make sure to update the child-count of the
* parent. When a listening parent socket is closed, make sure no child-sockets
* keep a pointer to it.
*/
static void prvTCPSetSocketCount( FreeRTOS_Socket_t *pxSocketToDelete )
{
const ListItem_t *pxIterator;
const MiniListItem_t *pxEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xBoundTCPSocketsList );
FreeRTOS_Socket_t *pxOtherSocket;
uint16_t usLocalPort = pxSocketToDelete->usLocalPort;
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( pxEnd );
pxIterator != ( const ListItem_t * ) pxEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
pxOtherSocket = ( FreeRTOS_Socket_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
if( ( pxOtherSocket->u.xTCP.ucTCPState == eTCP_LISTEN ) &&
( pxOtherSocket->usLocalPort == usLocalPort ) &&
( pxOtherSocket->u.xTCP.usChildCount ) )
{
pxOtherSocket->u.xTCP.usChildCount--;
FreeRTOS_debug_printf( ( "Lost: Socket %u now has %u / %u child%s\n",
pxOtherSocket->usLocalPort,
pxOtherSocket->u.xTCP.usChildCount,
pxOtherSocket->u.xTCP.usBacklog,
pxOtherSocket->u.xTCP.usChildCount == 1u ? "" : "ren" ) );
break;
}
}
}
#endif /* ipconfigUSE_TCP == 1 */
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_setsockopt( Socket_t xSocket, int32_t lLevel, int32_t lOptionName, const void *pvOptionValue, size_t xOptionLength )
{
/* The standard Berkeley function returns 0 for success. */
BaseType_t xReturn = -pdFREERTOS_ERRNO_EINVAL;
BaseType_t lOptionValue;
FreeRTOS_Socket_t *pxSocket;
pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
/* The function prototype is designed to maintain the expected Berkeley
sockets standard, but this implementation does not use all the parameters. */
( void ) lLevel;
( void ) xOptionLength;
configASSERT( xSocket );
switch( lOptionName )
{
case FREERTOS_SO_RCVTIMEO :
/* Receive time out. */
pxSocket->xReceiveBlockTime = *( ( TickType_t * ) pvOptionValue );
xReturn = 0;
break;
case FREERTOS_SO_SNDTIMEO :
pxSocket->xSendBlockTime = *( ( TickType_t * ) pvOptionValue );
if( pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_UDP )
{
/* The send time out is capped for the reason stated in the
comments where ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS is defined
in FreeRTOSIPConfig.h (assuming an official configuration file
is being used. */
if( pxSocket->xSendBlockTime > ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS )
{
pxSocket->xSendBlockTime = ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS;
}
}
else
{
/* For TCP socket, it isn't necessary to limit the blocking time
because the FreeRTOS_send() function does not wait for a network
buffer to become available. */
}
xReturn = 0;
break;
#if( ipconfigUDP_MAX_RX_PACKETS > 0 )
case FREERTOS_SO_UDP_MAX_RX_PACKETS:
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
pxSocket->u.xUDP.uxMaxPackets = *( ( UBaseType_t * ) pvOptionValue );
xReturn = 0;
break;
#endif /* ipconfigUDP_MAX_RX_PACKETS */
case FREERTOS_SO_UDPCKSUM_OUT :
/* Turn calculating of the UDP checksum on/off for this socket. */
lOptionValue = ( BaseType_t ) pvOptionValue;
if( lOptionValue == 0 )
{
pxSocket->ucSocketOptions &= ( uint8_t ) ~FREERTOS_SO_UDPCKSUM_OUT;
}
else
{
pxSocket->ucSocketOptions |= ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT;
}
xReturn = 0;
break;
#if( ipconfigUSE_CALLBACKS == 1 )
#if( ipconfigUSE_TCP == 1 )
case FREERTOS_SO_TCP_CONN_HANDLER: /* Set a callback for (dis)connection events */
case FREERTOS_SO_TCP_RECV_HANDLER: /* Install a callback for receiving TCP data. Supply pointer to 'F_TCP_UDP_Handler_t' (see below) */
case FREERTOS_SO_TCP_SENT_HANDLER: /* Install a callback for sending TCP data. Supply pointer to 'F_TCP_UDP_Handler_t' (see below) */
#endif /* ipconfigUSE_TCP */
case FREERTOS_SO_UDP_RECV_HANDLER: /* Install a callback for receiving UDP data. Supply pointer to 'F_TCP_UDP_Handler_t' (see below) */
case FREERTOS_SO_UDP_SENT_HANDLER: /* Install a callback for sending UDP data. Supply pointer to 'F_TCP_UDP_Handler_t' (see below) */
{
#if( ipconfigUSE_TCP == 1 )
{
UBaseType_t uxProtocol;
if( ( lOptionName == FREERTOS_SO_UDP_RECV_HANDLER ) ||
( lOptionName == FREERTOS_SO_UDP_SENT_HANDLER ) )
{
uxProtocol = ( UBaseType_t ) FREERTOS_IPPROTO_UDP;
}
else
{
uxProtocol = ( UBaseType_t ) FREERTOS_IPPROTO_TCP;
}
if( pxSocket->ucProtocol != ( uint8_t ) uxProtocol )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
}
#else
{
/* No need to check if the socket has the right
protocol, because only UDP socket can be created. */
}
#endif /* ipconfigUSE_TCP */
switch( lOptionName )
{
#if ipconfigUSE_TCP == 1
case FREERTOS_SO_TCP_CONN_HANDLER:
pxSocket->u.xTCP.pxHandleConnected = ((F_TCP_UDP_Handler_t *)pvOptionValue)->pxOnTCPConnected;
break;
case FREERTOS_SO_TCP_RECV_HANDLER:
pxSocket->u.xTCP.pxHandleReceive = ((F_TCP_UDP_Handler_t *)pvOptionValue)->pxOnTCPReceive;
break;
case FREERTOS_SO_TCP_SENT_HANDLER:
pxSocket->u.xTCP.pxHandleSent = ((F_TCP_UDP_Handler_t *)pvOptionValue)->pxOnTCPSent;
break;
#endif /* ipconfigUSE_TCP */
case FREERTOS_SO_UDP_RECV_HANDLER:
pxSocket->u.xUDP.pxHandleReceive = ((F_TCP_UDP_Handler_t *)pvOptionValue)->pxOnUDPReceive;
break;
case FREERTOS_SO_UDP_SENT_HANDLER:
pxSocket->u.xUDP.pxHandleSent = ((F_TCP_UDP_Handler_t *)pvOptionValue)->pxOnUDPSent;
break;
default:
break;
}
}
xReturn = 0;
break;
#endif /* ipconfigUSE_CALLBACKS */
#if( ipconfigUSE_TCP != 0 )
#if( ipconfigSOCKET_HAS_USER_SEMAPHORE != 0 )
/* Each socket has a semaphore on which the using task normally
sleeps. */
case FREERTOS_SO_SET_SEMAPHORE:
{
pxSocket->pxUserSemaphore = *( ( SemaphoreHandle_t * ) pvOptionValue );
xReturn = 0;
}
break;
#endif /* ipconfigSOCKET_HAS_USER_SEMAPHORE */
#if( ipconfigSOCKET_HAS_USER_WAKE_CALLBACK != 0 )
case FREERTOS_SO_WAKEUP_CALLBACK:
{
/* Each socket can have a callback function that is executed
when there is an event the socket's owner might want to
process. */
pxSocket->pxUserWakeCallback = ( SocketWakeupCallback_t ) pvOptionValue;
xReturn = 0;
}
break;
#endif /* ipconfigSOCKET_HAS_USER_WAKE_CALLBACK */
case FREERTOS_SO_SNDBUF: /* Set the size of the send buffer, in units of MSS (TCP only) */
case FREERTOS_SO_RCVBUF: /* Set the size of the receive buffer, in units of MSS (TCP only) */
{
uint32_t ulNewValue;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
FreeRTOS_debug_printf( ( "Set SO_%sBUF: wrong socket type\n",
( lOptionName == FREERTOS_SO_SNDBUF ) ? "SND" : "RCV" ) );
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
if( ( ( lOptionName == FREERTOS_SO_SNDBUF ) && ( pxSocket->u.xTCP.txStream != NULL ) ) ||
( ( lOptionName == FREERTOS_SO_RCVBUF ) && ( pxSocket->u.xTCP.rxStream != NULL ) ) )
{
FreeRTOS_debug_printf( ( "Set SO_%sBUF: buffer already created\n",
( lOptionName == FREERTOS_SO_SNDBUF ) ? "SND" : "RCV" ) );
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
ulNewValue = *( ( uint32_t * ) pvOptionValue );
if( lOptionName == FREERTOS_SO_SNDBUF )
{
/* Round up to nearest MSS size */
ulNewValue = FreeRTOS_round_up( ulNewValue, ( uint32_t ) pxSocket->u.xTCP.usInitMSS );
pxSocket->u.xTCP.uxTxStreamSize = ulNewValue;
}
else
{
pxSocket->u.xTCP.uxRxStreamSize = ulNewValue;
}
}
xReturn = 0;
break;
case FREERTOS_SO_WIN_PROPERTIES: /* Set all buffer and window properties in one call, parameter is pointer to WinProperties_t */
{
WinProperties_t* pxProps;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
FreeRTOS_debug_printf( ( "Set SO_WIN_PROP: wrong socket type\n" ) );
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
if( ( pxSocket->u.xTCP.txStream != NULL ) || ( pxSocket->u.xTCP.rxStream != NULL ) )
{
FreeRTOS_debug_printf( ( "Set SO_WIN_PROP: buffer already created\n" ) );
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
pxProps = ( ( WinProperties_t * ) pvOptionValue );
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDBUF, &( pxProps->lTxBufSize ), sizeof( pxProps->lTxBufSize ) );
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVBUF, &( pxProps->lRxBufSize ), sizeof( pxProps->lRxBufSize ) );
#if( ipconfigUSE_TCP_WIN == 1 )
{
pxSocket->u.xTCP.uxRxWinSize = ( uint32_t )pxProps->lRxWinSize; /* Fixed value: size of the TCP reception window */
pxSocket->u.xTCP.uxTxWinSize = ( uint32_t )pxProps->lTxWinSize; /* Fixed value: size of the TCP transmit window */
}
#else
{
pxSocket->u.xTCP.uxRxWinSize = 1u;
pxSocket->u.xTCP.uxTxWinSize = 1u;
}
#endif
/* In case the socket has already initialised its tcpWin,
adapt the window size parameters */
if( pxSocket->u.xTCP.xTCPWindow.u.bits.bHasInit != pdFALSE_UNSIGNED )
{
pxSocket->u.xTCP.xTCPWindow.xSize.ulRxWindowLength = pxSocket->u.xTCP.uxRxWinSize * pxSocket->u.xTCP.usInitMSS;
pxSocket->u.xTCP.xTCPWindow.xSize.ulTxWindowLength = pxSocket->u.xTCP.uxTxWinSize * pxSocket->u.xTCP.usInitMSS;
}
}
xReturn = 0;
break;
case FREERTOS_SO_REUSE_LISTEN_SOCKET: /* If true, the server-socket will turn into a connected socket */
{
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
if( *( ( BaseType_t * ) pvOptionValue ) != 0 )
{
pxSocket->u.xTCP.bits.bReuseSocket = pdTRUE_UNSIGNED;
}
else
{
pxSocket->u.xTCP.bits.bReuseSocket = pdFALSE_UNSIGNED;
}
}
xReturn = 0;
break;
case FREERTOS_SO_CLOSE_AFTER_SEND: /* As soon as the last byte has been transmitted, finalise the connection */
{
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
if( *( ( BaseType_t * ) pvOptionValue ) != 0 )
{
pxSocket->u.xTCP.bits.bCloseAfterSend = pdTRUE_UNSIGNED;
}
else
{
pxSocket->u.xTCP.bits.bCloseAfterSend = pdFALSE_UNSIGNED;
}
}
xReturn = 0;
break;
case FREERTOS_SO_SET_FULL_SIZE: /* Refuse to send packets smaller than MSS */
{
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
if( *( ( BaseType_t * ) pvOptionValue ) != 0 )
{
pxSocket->u.xTCP.xTCPWindow.u.bits.bSendFullSize = pdTRUE_UNSIGNED;
}
else
{
pxSocket->u.xTCP.xTCPWindow.u.bits.bSendFullSize = pdFALSE_UNSIGNED;
}
if( ( pxSocket->u.xTCP.xTCPWindow.u.bits.bSendFullSize == pdFALSE_UNSIGNED ) &&
( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED ) &&
( FreeRTOS_outstanding( pxSocket ) != 0 ) )
{
pxSocket->u.xTCP.usTimeout = 1u; /* to set/clear bSendFullSize */
xSendEventToIPTask( eTCPTimerEvent );
}
}
xReturn = 0;
break;
case FREERTOS_SO_STOP_RX: /* Refuse to receive more packts */
{
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}
if( *( ( BaseType_t * ) pvOptionValue ) != 0 )
{
pxSocket->u.xTCP.bits.bRxStopped = pdTRUE_UNSIGNED;
}
else
{
pxSocket->u.xTCP.bits.bRxStopped = pdFALSE_UNSIGNED;
}
pxSocket->u.xTCP.bits.bWinChange = pdTRUE_UNSIGNED;
pxSocket->u.xTCP.usTimeout = 1u; /* to set/clear bRxStopped */
xSendEventToIPTask( eTCPTimerEvent );
}
xReturn = 0;
break;
#endif /* ipconfigUSE_TCP == 1 */
default :
/* No other options are handled. */
xReturn = -pdFREERTOS_ERRNO_ENOPROTOOPT;
break;
}
return xReturn;
} /* Tested */
/*-----------------------------------------------------------*/
/* Find an available port number per https://tools.ietf.org/html/rfc6056. */
static uint16_t prvGetPrivatePortNumber( BaseType_t xProtocol )
{
const uint16_t usEphemeralPortCount =
socketAUTO_PORT_ALLOCATION_MAX_NUMBER - socketAUTO_PORT_ALLOCATION_START_NUMBER + 1;
uint16_t usIterations = usEphemeralPortCount;
uint32_t ulRandomSeed = 0;
uint16_t usResult = 0;
BaseType_t xGotZeroOnce = pdFALSE;
const List_t *pxList;
#if ipconfigUSE_TCP == 1
if( xProtocol == ( BaseType_t ) FREERTOS_IPPROTO_TCP )
{
pxList = &xBoundTCPSocketsList;
}
else
#endif
{
pxList = &xBoundUDPSocketsList;
}
/* Avoid compiler warnings if ipconfigUSE_TCP is not defined. */
( void ) xProtocol;
/* Find the next available port using the random seed as a starting
point. */
do
{
/* Generate a random seed. */
ulRandomSeed = ipconfigRAND32( );
/* Only proceed if the random number generator succeeded. */
if( 0 == ulRandomSeed )
{
if( pdFALSE == xGotZeroOnce )
{
xGotZeroOnce = pdTRUE;
continue;
}
else
{
break;
}
}
/* Map the random to a candidate port. */
usResult =
socketAUTO_PORT_ALLOCATION_START_NUMBER +
( ( ( uint16_t )ulRandomSeed ) % usEphemeralPortCount );
/* Check if there's already an open socket with the same protocol
and port. */
if( NULL == pxListFindListItemWithValue(
pxList,
( TickType_t )FreeRTOS_htons( usResult ) ) )
{
usResult = FreeRTOS_htons( usResult );
break;
}
else
{
usResult = 0;
}
usIterations--;
}
while( usIterations > 0 );
return usResult;
}
/*-----------------------------------------------------------*/
/* pxListFindListItemWithValue: find a list item in a bound socket list
'xWantedItemValue' refers to a port number */
static const ListItem_t * pxListFindListItemWithValue( const List_t *pxList, TickType_t xWantedItemValue )
{
const ListItem_t * pxResult = NULL;
if( ( xIPIsNetworkTaskReady() != pdFALSE ) && ( pxList != NULL ) )
{
const ListItem_t *pxIterator;
const MiniListItem_t *pxEnd = ( const MiniListItem_t* )listGET_END_MARKER( pxList );
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( pxEnd );
pxIterator != ( const ListItem_t * ) pxEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
if( listGET_LIST_ITEM_VALUE( pxIterator ) == xWantedItemValue )
{
pxResult = pxIterator;
break;
}
}
}
return pxResult;
} /* Tested */
/*-----------------------------------------------------------*/
FreeRTOS_Socket_t *pxUDPSocketLookup( UBaseType_t uxLocalPort )
{
const ListItem_t *pxListItem;
FreeRTOS_Socket_t *pxSocket = NULL;
/* Looking up a socket is quite simple, find a match with the local port.
See if there is a list item associated with the port number on the
list of bound sockets. */
pxListItem = pxListFindListItemWithValue( &xBoundUDPSocketsList, ( TickType_t ) uxLocalPort );
if( pxListItem != NULL )
{
/* The owner of the list item is the socket itself. */
pxSocket = ( FreeRTOS_Socket_t * ) listGET_LIST_ITEM_OWNER( pxListItem );
configASSERT( pxSocket != NULL );
}
return pxSocket;
}
/*-----------------------------------------------------------*/
#if ipconfigINCLUDE_FULL_INET_ADDR == 1
uint32_t FreeRTOS_inet_addr( const char * pcIPAddress )
{
const uint32_t ulDecimalBase = 10u;
uint8_t ucOctet[ socketMAX_IP_ADDRESS_OCTETS ];
const char *pcPointerOnEntering;
uint32_t ulReturn = 0UL, ulValue;
UBaseType_t uxOctetNumber;
BaseType_t xResult = pdPASS;
for( uxOctetNumber = 0u; uxOctetNumber < socketMAX_IP_ADDRESS_OCTETS; uxOctetNumber++ )
{
ulValue = 0ul;
pcPointerOnEntering = pcIPAddress;
while( ( *pcIPAddress >= '0' ) && ( *pcIPAddress <= '9' ) )
{
/* Move previous read characters into the next decimal
position. */
ulValue *= ulDecimalBase;
/* Add the binary value of the ascii character. */
ulValue += ( ( uint32_t ) ( *pcIPAddress ) - ( uint32_t ) '0' );
/* Move to next character in the string. */
pcIPAddress++;
}
/* Check characters were read. */
if( pcIPAddress == pcPointerOnEntering )
{
xResult = pdFAIL;
}
/* Check the value fits in an 8-bit number. */
if( ulValue > 0xffUL )
{
xResult = pdFAIL;
}
else
{
ucOctet[ uxOctetNumber ] = ( uint8_t ) ulValue;
/* Check the next character is as expected. */
if( uxOctetNumber < ( socketMAX_IP_ADDRESS_OCTETS - 1u ) )
{
if( *pcIPAddress != '.' )
{
xResult = pdFAIL;
}
else
{
/* Move past the dot. */
pcIPAddress++;
}
}
}
if( xResult == pdFAIL )
{
/* No point going on. */
break;
}
}
if( *pcIPAddress != ( char ) 0 )
{
/* Expected the end of the string. */
xResult = pdFAIL;
}
if( uxOctetNumber != socketMAX_IP_ADDRESS_OCTETS )
{
/* Didn't read enough octets. */
xResult = pdFAIL;
}
if( xResult == pdPASS )
{
ulReturn = FreeRTOS_inet_addr_quick( ucOctet[ 0 ], ucOctet[ 1 ], ucOctet[ 2 ], ucOctet[ 3 ] );
}
return ulReturn;
}
#endif /* ipconfigINCLUDE_FULL_INET_ADDR */
/*-----------------------------------------------------------*/
/* Function to get the local address and IP port */
size_t FreeRTOS_GetLocalAddress( Socket_t xSocket, struct freertos_sockaddr *pxAddress )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
/* IP address of local machine. */
pxAddress->sin_addr = *ipLOCAL_IP_ADDRESS_POINTER;
/* Local port on this machine. */
pxAddress->sin_port = FreeRTOS_htons( pxSocket->usLocalPort );
return sizeof( *pxAddress );
}
/*-----------------------------------------------------------*/
void vSocketWakeUpUser( FreeRTOS_Socket_t *pxSocket )
{
/* _HT_ must work this out, now vSocketWakeUpUser will be called for any important
* event or transition */
#if( ipconfigSOCKET_HAS_USER_SEMAPHORE == 1 )
{
if( pxSocket->pxUserSemaphore != NULL )
{
xSemaphoreGive( pxSocket->pxUserSemaphore );
}
}
#endif /* ipconfigSOCKET_HAS_USER_SEMAPHORE */
#if( ipconfigSOCKET_HAS_USER_WAKE_CALLBACK == 1 )
{
if( pxSocket->pxUserWakeCallback != NULL )
{
pxSocket->pxUserWakeCallback( pxSocket );
}
}
#endif /* ipconfigSOCKET_HAS_USER_SEMAPHORE */
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
if( pxSocket->pxSocketSet != NULL )
{
EventBits_t xSelectBits = ( pxSocket->xEventBits >> SOCKET_EVENT_BIT_COUNT ) & eSELECT_ALL;
if( xSelectBits != 0ul )
{
pxSocket->xSocketBits |= xSelectBits;
xEventGroupSetBits( pxSocket->pxSocketSet->xSelectGroup, xSelectBits );
}
}
pxSocket->xEventBits &= eSOCKET_ALL;
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION */
if( ( pxSocket->xEventGroup != NULL ) && ( pxSocket->xEventBits != 0u ) )
{
xEventGroupSetBits( pxSocket->xEventGroup, pxSocket->xEventBits );
}
pxSocket->xEventBits = 0ul;
}
/*-----------------------------------------------------------*/
#if( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 1 )
/* This define makes it possible for network-card drivers to inspect
* UDP message and see if there is any UDP socket bound to a given port
* number.
* This is probably only usefull in systems with a minimum of RAM and
* when lots of anonymous broadcast messages come in
*/
BaseType_t xPortHasUDPSocket( uint16_t usPortNr )
{
BaseType_t xFound = pdFALSE;
vTaskSuspendAll();
{
if( ( pxListFindListItemWithValue( &xBoundUDPSocketsList, ( TickType_t ) usPortNr ) != NULL ) )
{
xFound = pdTRUE;
}
}
xTaskResumeAll();
return xFound;
}
#endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
static BaseType_t bMayConnect( FreeRTOS_Socket_t *pxSocket );
static BaseType_t bMayConnect( FreeRTOS_Socket_t *pxSocket )
{
switch( pxSocket->u.xTCP.ucTCPState )
{
case eCLOSED:
case eCLOSE_WAIT: return 0;
case eCONNECT_SYN: return -pdFREERTOS_ERRNO_EINPROGRESS;
default: return -pdFREERTOS_ERRNO_EAGAIN;
}
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
static BaseType_t prvTCPConnectStart( FreeRTOS_Socket_t *pxSocket, struct freertos_sockaddr *pxAddress )
{
BaseType_t xResult = 0;
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdFALSE ) == pdFALSE )
{
/* Not a valid socket or wrong type */
xResult = -pdFREERTOS_ERRNO_EBADF;
}
else if( FreeRTOS_issocketconnected( pxSocket ) > 0 )
{
/* The socket is already connected. */
xResult = -pdFREERTOS_ERRNO_EISCONN;
}
else if( socketSOCKET_IS_BOUND( pxSocket ) == pdFALSE )
{
/* Bind the socket to the port that the client task will send from.
Non-standard, so the error returned is that returned by bind(). */
xResult = FreeRTOS_bind( ( Socket_t ) pxSocket, NULL, 0u );
}
if( xResult == 0 )
{
/* Check if it makes any sense to wait for a connect event, this condition
might change while sleeping, so it must be checked within each loop */
xResult = bMayConnect( pxSocket ); /* -EINPROGRESS, -EAGAIN, or 0 for OK */
/* Start the connect procedure, kernel will start working on it */
if( xResult == 0 )
{
pxSocket->u.xTCP.bits.bConnPrepared = pdFALSE_UNSIGNED;
pxSocket->u.xTCP.ucRepCount = 0u;
FreeRTOS_debug_printf( ( "FreeRTOS_connect: %u to %lxip:%u\n",
pxSocket->usLocalPort, FreeRTOS_ntohl( pxAddress->sin_addr ), FreeRTOS_ntohs( pxAddress->sin_port ) ) );
/* Port on remote machine. */
pxSocket->u.xTCP.usRemotePort = FreeRTOS_ntohs( pxAddress->sin_port );
/* IP address of remote machine. */
pxSocket->u.xTCP.ulRemoteIP = FreeRTOS_ntohl( pxAddress->sin_addr );
/* (client) internal state: socket wants to send a connect. */
vTCPStateChange( pxSocket, eCONNECT_SYN );
/* To start an active connect. */
pxSocket->u.xTCP.usTimeout = 1u;
if( xSendEventToIPTask( eTCPTimerEvent ) != pdPASS )
{
xResult = -pdFREERTOS_ERRNO_ECANCELED;
}
}
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* FreeRTOS_connect: socket wants to connect to a remote port
*/
BaseType_t FreeRTOS_connect( Socket_t xClientSocket, struct freertos_sockaddr *pxAddress, socklen_t xAddressLength )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t* ) xClientSocket;
TickType_t xRemainingTime;
BaseType_t xTimed = pdFALSE;
BaseType_t xResult;
TimeOut_t xTimeOut;
( void ) xAddressLength;
xResult = prvTCPConnectStart( pxSocket, pxAddress );
if( xResult == 0 )
{
/* And wait for the result */
for( ;; )
{
if( xTimed == pdFALSE )
{
/* Only in the first round, check for non-blocking */
xRemainingTime = pxSocket->xReceiveBlockTime;
if( xRemainingTime == ( TickType_t )0 )
{
/* Not yet connected, correct state, non-blocking. */
xResult = -pdFREERTOS_ERRNO_EWOULDBLOCK;
break;
}
/* Don't get here a second time. */
xTimed = pdTRUE;
/* Fetch the current time */
vTaskSetTimeOutState( &xTimeOut );
}
/* Did it get connected while sleeping ? */
xResult = FreeRTOS_issocketconnected( pxSocket );
/* Returns positive when connected, negative means an error */
if( xResult < 0 )
{
/* Return the error */
break;
}
if( xResult > 0 )
{
/* Socket now connected, return a zero */
xResult = 0;
break;
}
/* Is it allowed to sleep more? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) )
{
xResult = -pdFREERTOS_ERRNO_ETIMEDOUT;
break;
}
/* Go sleeping until we get any down-stream event */
xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_CONNECT, pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
}
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* FreeRTOS_accept: can return a new connected socket
* if the server socket is in listen mode and receives a connection request
* The new socket will be bound already to the same port number as the listing
* socket.
*/
Socket_t FreeRTOS_accept( Socket_t xServerSocket, struct freertos_sockaddr *pxAddress, socklen_t *pxAddressLength )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xServerSocket;
FreeRTOS_Socket_t *pxClientSocket = NULL;
TickType_t xRemainingTime;
BaseType_t xTimed = pdFALSE, xAsk = pdFALSE;
TimeOut_t xTimeOut;
IPStackEvent_t xAskEvent;
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
{
/* Not a valid socket or wrong type */
pxClientSocket = ( FreeRTOS_Socket_t * ) FREERTOS_INVALID_SOCKET;
}
else if( ( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED ) &&
( pxSocket->u.xTCP.ucTCPState != eTCP_LISTEN ) )
{
/* Parent socket is not in listening mode */
pxClientSocket = ( FreeRTOS_Socket_t * ) FREERTOS_INVALID_SOCKET;
}
else
{
/* Loop will stop with breaks. */
for( ; ; )
{
/* Is there a new client? */
vTaskSuspendAll();
{
if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
{
pxClientSocket = pxSocket->u.xTCP.pxPeerSocket;
}
else
{
pxClientSocket = pxSocket;
}
if( pxClientSocket != NULL )
{
pxSocket->u.xTCP.pxPeerSocket = NULL;
/* Is it still not taken ? */
if( pxClientSocket->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED )
{
pxClientSocket->u.xTCP.bits.bPassAccept = pdFALSE_UNSIGNED;
}
else
{
pxClientSocket = NULL;
}
}
}
xTaskResumeAll();
if( pxClientSocket != NULL )
{
if( pxAddress != NULL )
{
/* IP address of remote machine. */
pxAddress->sin_addr = FreeRTOS_ntohl( pxClientSocket->u.xTCP.ulRemoteIP );
/* Port on remote machine. */
pxAddress->sin_port = FreeRTOS_ntohs( pxClientSocket->u.xTCP.usRemotePort );
}
if( pxAddressLength != NULL )
{
*pxAddressLength = sizeof( *pxAddress );
}
if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
{
xAsk = pdTRUE;
}
}
if( xAsk != pdFALSE )
{
/* Ask to set an event in 'xEventGroup' as soon as a new
client gets connected for this listening socket. */
xAskEvent.eEventType = eTCPAcceptEvent;
xAskEvent.pvData = ( void * ) pxSocket;
xSendEventStructToIPTask( &xAskEvent, portMAX_DELAY );
}
if( pxClientSocket != NULL )
{
break;
}
if( xTimed == pdFALSE )
{
/* Only in the first round, check for non-blocking */
xRemainingTime = pxSocket->xReceiveBlockTime;
if( xRemainingTime == ( TickType_t ) 0 )
{
break;
}
/* Don't get here a second time */
xTimed = pdTRUE;
/* Fetch the current time */
vTaskSetTimeOutState( &xTimeOut );
}
/* Has the timeout been reached? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) != pdFALSE )
{
break;
}
/* Go sleeping until we get any down-stream event */
xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_ACCEPT, pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
}
}
return ( Socket_t ) pxClientSocket;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* Read incoming data from a TCP socket
* Only after the last byte has been read, a close error might be returned
*/
BaseType_t FreeRTOS_recv( Socket_t xSocket, void *pvBuffer, size_t xBufferLength, BaseType_t xFlags )
{
BaseType_t xByteCount;
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
TickType_t xRemainingTime;
BaseType_t xTimed = pdFALSE;
TimeOut_t xTimeOut;
EventBits_t xEventBits = ( EventBits_t ) 0;
/* Check if the socket is valid, has type TCP and if it is bound to a
port. */
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
{
xByteCount = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
if( pxSocket->u.xTCP.rxStream != NULL )
{
xByteCount = ( BaseType_t )uxStreamBufferGetSize ( pxSocket->u.xTCP.rxStream );
}
else
{
xByteCount = 0;
}
while( xByteCount == 0 )
{
switch( pxSocket->u.xTCP.ucTCPState )
{
case eCLOSED:
case eCLOSE_WAIT: /* (server + client) waiting for a connection termination request from the local user. */
case eCLOSING: /* (server + client) waiting for a connection termination request acknowledgement from the remote TCP. */
if( pxSocket->u.xTCP.bits.bMallocError != pdFALSE_UNSIGNED )
{
/* The no-memory error has priority above the non-connected error.
Both are fatal and will elad to closing the socket. */
xByteCount = -pdFREERTOS_ERRNO_ENOMEM;
}
else
{
xByteCount = -pdFREERTOS_ERRNO_ENOTCONN;
}
/* Call continue to break out of the switch and also the while
loop. */
continue;
default:
break;
}
if( xTimed == pdFALSE )
{
/* Only in the first round, check for non-blocking. */
xRemainingTime = pxSocket->xReceiveBlockTime;
if( xRemainingTime == ( TickType_t ) 0 )
{
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
/* Just check for the interrupt flag. */
xEventBits = xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_INTR,
pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, socketDONT_BLOCK );
}
#endif /* ipconfigSUPPORT_SIGNALS */
break;
}
if( ( xFlags & FREERTOS_MSG_DONTWAIT ) != 0 )
{
break;
}
/* Don't get here a second time. */
xTimed = pdTRUE;
/* Fetch the current time. */
vTaskSetTimeOutState( &xTimeOut );
}
/* Has the timeout been reached? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) != pdFALSE )
{
break;
}
/* Block until there is a down-stream event. */
xEventBits = xEventGroupWaitBits( pxSocket->xEventGroup,
eSOCKET_RECEIVE | eSOCKET_CLOSED | eSOCKET_INTR,
pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
if( ( xEventBits & eSOCKET_INTR ) != 0u )
{
break;
}
}
#else
{
( void ) xEventBits;
}
#endif /* ipconfigSUPPORT_SIGNALS */
if( pxSocket->u.xTCP.rxStream != NULL )
{
xByteCount = ( BaseType_t ) uxStreamBufferGetSize ( pxSocket->u.xTCP.rxStream );
}
else
{
xByteCount = 0;
}
}
#if( ipconfigSUPPORT_SIGNALS != 0 )
if( ( xEventBits & eSOCKET_INTR ) != 0 )
{
if( ( xEventBits & ( eSOCKET_RECEIVE | eSOCKET_CLOSED ) ) != 0 )
{
/* Shouldn't have cleared other flags. */
xEventBits &= ~eSOCKET_INTR;
xEventGroupSetBits( pxSocket->xEventGroup, xEventBits );
}
xByteCount = -pdFREERTOS_ERRNO_EINTR;
}
else
#endif /* ipconfigSUPPORT_SIGNALS */
if( xByteCount > 0 )
{
if( ( xFlags & FREERTOS_ZERO_COPY ) == 0 )
{
xByteCount = ( BaseType_t ) uxStreamBufferGet( pxSocket->u.xTCP.rxStream, 0ul, ( uint8_t * ) pvBuffer, ( size_t ) xBufferLength, ( xFlags & FREERTOS_MSG_PEEK ) != 0 );
if( pxSocket->u.xTCP.bits.bLowWater != pdFALSE_UNSIGNED )
{
/* We had reached the low-water mark, now see if the flag
can be cleared */
size_t uxFrontSpace = uxStreamBufferFrontSpace( pxSocket->u.xTCP.rxStream );
if( uxFrontSpace >= pxSocket->u.xTCP.uxEnoughSpace )
{
pxSocket->u.xTCP.bits.bLowWater = pdFALSE_UNSIGNED;
pxSocket->u.xTCP.bits.bWinChange = pdTRUE_UNSIGNED;
pxSocket->u.xTCP.usTimeout = 1u; /* because bLowWater is cleared. */
xSendEventToIPTask( eTCPTimerEvent );
}
}
}
else
{
/* Zero-copy reception of data: pvBuffer is a pointer to a pointer. */
xByteCount = ( BaseType_t ) uxStreamBufferGetPtr( pxSocket->u.xTCP.rxStream, (uint8_t **)pvBuffer );
}
}
} /* prvValidSocket() */
return xByteCount;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
static int32_t prvTCPSendCheck( FreeRTOS_Socket_t *pxSocket, size_t xDataLength )
{
int32_t xResult = 1;
/* Is this a socket of type TCP and is it already bound to a port number ? */
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
{
xResult = -pdFREERTOS_ERRNO_EINVAL;
}
else if( pxSocket->u.xTCP.bits.bMallocError != pdFALSE_UNSIGNED )
{
xResult = -pdFREERTOS_ERRNO_ENOMEM;
}
else if( pxSocket->u.xTCP.ucTCPState == eCLOSED )
{
xResult = -pdFREERTOS_ERRNO_ENOTCONN;
}
else if( pxSocket->u.xTCP.bits.bFinSent != pdFALSE_UNSIGNED )
{
/* This TCP connection is closing already, the FIN flag has been sent.
Maybe it is still delivering or receiving data.
Return OK in order not to get closed/deleted too quickly */
xResult = 0;
}
else if( xDataLength == 0ul )
{
/* send() is being called to send zero bytes */
xResult = 0;
}
else if( pxSocket->u.xTCP.txStream == NULL )
{
/* Create the outgoing stream only when it is needed */
prvTCPCreateStream( pxSocket, pdFALSE );
if( pxSocket->u.xTCP.txStream == NULL )
{
xResult = -pdFREERTOS_ERRNO_ENOMEM;
}
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* Get a direct pointer to the circular transmit buffer.
'*pxLength' will contain the number of bytes that may be written. */
uint8_t *FreeRTOS_get_tx_head( Socket_t xSocket, BaseType_t *pxLength )
{
uint8_t *pucReturn;
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
StreamBuffer_t *pxBuffer = pxSocket->u.xTCP.txStream;
if( pxBuffer != NULL )
{
BaseType_t xSpace = ( BaseType_t ) uxStreamBufferGetSpace( pxBuffer );
BaseType_t xRemain = ( BaseType_t ) ( pxBuffer->LENGTH - pxBuffer->uxHead );
*pxLength = FreeRTOS_min_BaseType( xSpace, xRemain );
pucReturn = pxBuffer->ucArray + pxBuffer->uxHead;
}
else
{
*pxLength = 0;
pucReturn = NULL;
}
return pucReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* Send data using a TCP socket. It is not necessary to have the socket
* connected already. Outgoing data will be stored and delivered as soon as
* the socket gets connected.
*/
BaseType_t FreeRTOS_send( Socket_t xSocket, const void *pvBuffer, size_t uxDataLength, BaseType_t xFlags )
{
BaseType_t xByteCount;
BaseType_t xBytesLeft;
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
TickType_t xRemainingTime;
BaseType_t xTimed = pdFALSE;
TimeOut_t xTimeOut;
BaseType_t xCloseAfterSend;
/* Prevent compiler warnings about unused parameters. The parameter
may be used in future versions. */
( void ) xFlags;
xByteCount = ( BaseType_t ) prvTCPSendCheck( pxSocket, uxDataLength );
if( xByteCount > 0 )
{
/* xBytesLeft is number of bytes to send, will count to zero. */
xBytesLeft = ( BaseType_t ) uxDataLength;
/* xByteCount is number of bytes that can be sent now. */
xByteCount = ( BaseType_t ) uxStreamBufferGetSpace( pxSocket->u.xTCP.txStream );
/* While there are still bytes to be sent. */
while( xBytesLeft > 0 )
{
/* If txStream has space. */
if( xByteCount > 0 )
{
/* Don't send more than necessary. */
if( xByteCount > xBytesLeft )
{
xByteCount = xBytesLeft;
}
/* Is the close-after-send flag set and is this really the
last transmission? */
if( ( pxSocket->u.xTCP.bits.bCloseAfterSend != pdFALSE_UNSIGNED ) && ( xByteCount == xBytesLeft ) )
{
xCloseAfterSend = pdTRUE;
}
else
{
xCloseAfterSend = pdFALSE;
}
/* The flag 'bCloseAfterSend' can be set before sending data
using setsockopt()
When the last data packet is being sent out, a FIN flag will
be included to let the peer know that no more data is to be
expected. The use of 'bCloseAfterSend' is not mandatory, it
is just a faster way of transferring files (e.g. when using
FTP). */
if( xCloseAfterSend != pdFALSE )
{
/* Now suspend the scheduler: sending the last data and
setting bCloseRequested must be done together */
vTaskSuspendAll();
pxSocket->u.xTCP.bits.bCloseRequested = pdTRUE_UNSIGNED;
}
xByteCount = ( BaseType_t ) uxStreamBufferAdd( pxSocket->u.xTCP.txStream, 0ul, ( const uint8_t * ) pvBuffer, ( size_t ) xByteCount );
if( xCloseAfterSend != pdFALSE )
{
/* Now when the IP-task transmits the data, it will also
see that bCloseRequested is true and include the FIN
flag to start closure of the connection. */
xTaskResumeAll();
}
/* Send a message to the IP-task so it can work on this
socket. Data is sent, let the IP-task work on it. */
pxSocket->u.xTCP.usTimeout = 1u;
if( xIsCallingFromIPTask() == pdFALSE )
{
/* Only send a TCP timer event when not called from the
IP-task. */
xSendEventToIPTask( eTCPTimerEvent );
}
xBytesLeft -= xByteCount;
if( xBytesLeft == 0 )
{
break;
}
/* As there are still bytes left to be sent, increase the
data pointer. */
pvBuffer = ( void * ) ( ( ( const uint8_t * ) pvBuffer) + xByteCount );
}
/* Not all bytes have been sent. In case the socket is marked as
blocking sleep for a while. */
if( xTimed == pdFALSE )
{
/* Only in the first round, check for non-blocking. */
xRemainingTime = pxSocket->xSendBlockTime;
#if( ipconfigUSE_CALLBACKS != 0 )
{
if( xIsCallingFromIPTask() != pdFALSE )
{
/* If this send function is called from within a
call-back handler it may not block, otherwise
chances would be big to get a deadlock: the IP-task
waiting for itself. */
xRemainingTime = ( TickType_t ) 0;
}
}
#endif /* ipconfigUSE_CALLBACKS */
if( xRemainingTime == ( TickType_t ) 0 )
{
break;
}
if( ( xFlags & FREERTOS_MSG_DONTWAIT ) != 0 )
{
break;
}
/* Don't get here a second time. */
xTimed = pdTRUE;
/* Fetch the current time. */
vTaskSetTimeOutState( &xTimeOut );
}
else
{
/* Has the timeout been reached? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) != pdFALSE )
{
break;
}
}
/* Go sleeping until down-stream events are received. */
xEventGroupWaitBits( pxSocket->xEventGroup, eSOCKET_SEND | eSOCKET_CLOSED,
pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
xByteCount = ( BaseType_t ) uxStreamBufferGetSpace( pxSocket->u.xTCP.txStream );
}
/* How much was actually sent? */
xByteCount = ( ( BaseType_t ) uxDataLength ) - xBytesLeft;
if( xByteCount == 0 )
{
if( pxSocket->u.xTCP.ucTCPState > eESTABLISHED )
{
xByteCount = ( BaseType_t ) -pdFREERTOS_ERRNO_ENOTCONN;
}
else
{
if( ipconfigTCP_MAY_LOG_PORT( pxSocket->usLocalPort ) != pdFALSE )
{
FreeRTOS_debug_printf( ( "FreeRTOS_send: %u -> %lxip:%d: no space\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort ) );
}
xByteCount = ( BaseType_t ) -pdFREERTOS_ERRNO_ENOSPC;
}
}
}
return xByteCount;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* Request to put a socket in listen mode
*/
BaseType_t FreeRTOS_listen( Socket_t xSocket, BaseType_t xBacklog )
{
FreeRTOS_Socket_t *pxSocket;
BaseType_t xResult = 0;
pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
/* listen() is allowed for a valid TCP socket in Closed state and already
bound. */
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
{
xResult = -pdFREERTOS_ERRNO_EOPNOTSUPP;
}
else if( ( pxSocket->u.xTCP.ucTCPState != eCLOSED ) && ( pxSocket->u.xTCP.ucTCPState != eCLOSE_WAIT ) )
{
/* Socket is in a wrong state. */
xResult = -pdFREERTOS_ERRNO_EOPNOTSUPP;
}
else
{
/* Backlog is interpreted here as "the maximum number of child
sockets. */
pxSocket->u.xTCP.usBacklog = ( uint16_t )FreeRTOS_min_int32( ( int32_t ) 0xffff, ( int32_t ) xBacklog );
/* This cleaning is necessary only if a listening socket is being
reused as it might have had a previous connection. */
if( pxSocket->u.xTCP.bits.bReuseSocket )
{
if( pxSocket->u.xTCP.rxStream != NULL )
{
vStreamBufferClear( pxSocket->u.xTCP.rxStream );
}
if( pxSocket->u.xTCP.txStream != NULL )
{
vStreamBufferClear( pxSocket->u.xTCP.txStream );
}
memset( pxSocket->u.xTCP.xPacket.u.ucLastPacket, '\0', sizeof( pxSocket->u.xTCP.xPacket.u.ucLastPacket ) );
memset( &pxSocket->u.xTCP.xTCPWindow, '\0', sizeof( pxSocket->u.xTCP.xTCPWindow ) );
memset( &pxSocket->u.xTCP.bits, '\0', sizeof( pxSocket->u.xTCP.bits ) );
/* Now set the bReuseSocket flag again, because the bits have
just been cleared. */
pxSocket->u.xTCP.bits.bReuseSocket = pdTRUE_UNSIGNED;
}
vTCPStateChange( pxSocket, eTCP_LISTEN );
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* shutdown - shut down part of a full-duplex connection */
BaseType_t FreeRTOS_shutdown( Socket_t xSocket, BaseType_t xHow )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xResult;
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
{
/*_RB_ Is this comment correct? The socket is not of a type that
supports the listen() operation. */
xResult = -pdFREERTOS_ERRNO_EOPNOTSUPP;
}
else if ( pxSocket->u.xTCP.ucTCPState != eESTABLISHED )
{
/*_RB_ Is this comment correct? The socket is not of a type that
supports the listen() operation. */
xResult = -pdFREERTOS_ERRNO_EOPNOTSUPP;
}
else
{
pxSocket->u.xTCP.bits.bUserShutdown = pdTRUE_UNSIGNED;
/* Let the IP-task perform the shutdown of the connection. */
pxSocket->u.xTCP.usTimeout = 1u;
xSendEventToIPTask( eTCPTimerEvent );
xResult = 0;
}
(void) xHow;
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* A TCP timer has expired, now check all TCP sockets for:
* - Active connect
* - Send a delayed ACK
* - Send new data
* - Send a keep-alive packet
* - Check for timeout (in non-connected states only)
*/
TickType_t xTCPTimerCheck( BaseType_t xWillSleep )
{
FreeRTOS_Socket_t *pxSocket;
TickType_t xShortest = pdMS_TO_TICKS( ( TickType_t ) ipTCP_TIMER_PERIOD_MS );
TickType_t xNow = xTaskGetTickCount();
static TickType_t xLastTime = 0u;
TickType_t xDelta = xNow - xLastTime;
ListItem_t* pxEnd = ( ListItem_t * ) listGET_END_MARKER( &xBoundTCPSocketsList );
ListItem_t *pxIterator = ( ListItem_t * ) listGET_HEAD_ENTRY( &xBoundTCPSocketsList );
xLastTime = xNow;
if( xDelta == 0u )
{
xDelta = 1u;
}
while( pxIterator != pxEnd )
{
pxSocket = ( FreeRTOS_Socket_t * )listGET_LIST_ITEM_OWNER( pxIterator );
pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator );
/* Sockets with 'tmout == 0' do not need any regular attention. */
if( pxSocket->u.xTCP.usTimeout == 0u )
{
continue;
}
if( xDelta < ( TickType_t ) pxSocket->u.xTCP.usTimeout )
{
pxSocket->u.xTCP.usTimeout = ( uint16_t ) ( ( ( TickType_t ) pxSocket->u.xTCP.usTimeout ) - xDelta );
}
else
{
int rc ;
pxSocket->u.xTCP.usTimeout = 0u;
rc = xTCPSocketCheck( pxSocket );
/* Within this function, the socket might want to send a delayed
ack or send out data or whatever it needs to do. */
if( rc < 0 )
{
/* Continue because the socket was deleted. */
continue;
}
}
/* In xEventBits the driver may indicate that the socket has
important events for the user. These are only done just before the
IP-task goes to sleep. */
if( pxSocket->xEventBits != 0u )
{
if( xWillSleep != pdFALSE )
{
/* The IP-task is about to go to sleep, so messages can be
sent to the socket owners. */
vSocketWakeUpUser( pxSocket );
}
else
{
/* Or else make sure this will be called again to wake-up
the sockets' owner. */
xShortest = ( TickType_t ) 0;
}
}
if( ( pxSocket->u.xTCP.usTimeout != 0u ) && ( xShortest > ( TickType_t ) pxSocket->u.xTCP.usTimeout ) )
{
xShortest = ( TickType_t ) pxSocket->u.xTCP.usTimeout;
}
}
return xShortest;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* TCP: as multiple sockets may be bound to the same local port number
* looking up a socket is a little more complex:
* Both a local port, and a remote port and IP address are being used
* For a socket in listening mode, the remote port and IP address are both 0
*/
FreeRTOS_Socket_t *pxTCPSocketLookup( uint32_t ulLocalIP, UBaseType_t uxLocalPort, uint32_t ulRemoteIP, UBaseType_t uxRemotePort )
{
ListItem_t *pxIterator;
FreeRTOS_Socket_t *pxResult = NULL, *pxListenSocket = NULL;
MiniListItem_t *pxEnd = ( MiniListItem_t* )listGET_END_MARKER( &xBoundTCPSocketsList );
/* Parameter not yet supported. */
( void ) ulLocalIP;
for( pxIterator = ( ListItem_t * ) listGET_NEXT( pxEnd );
pxIterator != ( ListItem_t * ) pxEnd;
pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator ) )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
if( pxSocket->usLocalPort == ( uint16_t ) uxLocalPort )
{
if( pxSocket->u.xTCP.ucTCPState == eTCP_LISTEN )
{
/* If this is a socket listening to uxLocalPort, remember it
in case there is no perfect match. */
pxListenSocket = pxSocket;
}
else if( ( pxSocket->u.xTCP.usRemotePort == ( uint16_t ) uxRemotePort ) && ( pxSocket->u.xTCP.ulRemoteIP == ulRemoteIP ) )
{
/* For sockets not in listening mode, find a match with
xLocalPort, ulRemoteIP AND xRemotePort. */
pxResult = pxSocket;
break;
}
}
}
if( pxResult == NULL )
{
/* An exact match was not found, maybe a listening socket was
found. */
pxResult = pxListenSocket;
}
return pxResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
const struct xSTREAM_BUFFER *FreeRTOS_get_rx_buf( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = (FreeRTOS_Socket_t *)xSocket;
return pxSocket->u.xTCP.rxStream;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
static StreamBuffer_t *prvTCPCreateStream ( FreeRTOS_Socket_t *pxSocket, BaseType_t xIsInputStream )
{
StreamBuffer_t *pxBuffer;
size_t uxLength;
size_t uxSize;
/* Now that a stream is created, the maximum size is fixed before
creation, it could still be changed with setsockopt(). */
if( xIsInputStream != pdFALSE )
{
uxLength = pxSocket->u.xTCP.uxRxStreamSize;
if( pxSocket->u.xTCP.uxLittleSpace == 0ul )
{
pxSocket->u.xTCP.uxLittleSpace = ( 1ul * pxSocket->u.xTCP.uxRxStreamSize ) / 5u; /*_RB_ Why divide by 5? Can this be changed to a #define? */
}
if( pxSocket->u.xTCP.uxEnoughSpace == 0ul )
{
pxSocket->u.xTCP.uxEnoughSpace = ( 4ul * pxSocket->u.xTCP.uxRxStreamSize ) / 5u; /*_RB_ Why multiply by 4? Maybe sock80_PERCENT?*/
}
}
else
{
uxLength = pxSocket->u.xTCP.uxTxStreamSize;
}
/* Add an extra 4 (or 8) bytes. */
uxLength += sizeof( size_t );
/* And make the length a multiple of sizeof( size_t ). */
uxLength &= ~( sizeof( size_t ) - 1u );
uxSize = sizeof( *pxBuffer ) - sizeof( pxBuffer->ucArray ) + uxLength;
pxBuffer = ( StreamBuffer_t * )pvPortMallocLarge( uxSize );
if( pxBuffer == NULL )
{
FreeRTOS_debug_printf( ( "prvTCPCreateStream: malloc failed\n" ) );
pxSocket->u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
vTCPStateChange( pxSocket, eCLOSE_WAIT );
}
else
{
/* Clear the markers of the stream */
memset( pxBuffer, '\0', sizeof( *pxBuffer ) - sizeof( pxBuffer->ucArray ) );
pxBuffer->LENGTH = ( size_t ) uxLength ;
if( xTCPWindowLoggingLevel != 0 )
{
FreeRTOS_debug_printf( ( "prvTCPCreateStream: %cxStream created %lu bytes (total %lu)\n", xIsInputStream ? 'R' : 'T', uxLength, uxSize ) );
}
if( xIsInputStream != 0 )
{
pxSocket->u.xTCP.rxStream = pxBuffer;
}
else
{
pxSocket->u.xTCP.txStream = pxBuffer;
}
}
return pxBuffer;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* Add data to the RxStream. When uxOffset > 0, data has come in out-of-order
* and will be put in front of the head so it can not be popped by the user.
*/
int32_t lTCPAddRxdata( FreeRTOS_Socket_t *pxSocket, size_t uxOffset, const uint8_t *pcData, uint32_t ulByteCount )
{
StreamBuffer_t *pxStream = pxSocket->u.xTCP.rxStream;
int32_t xResult;
#if( ipconfigUSE_CALLBACKS == 1 )
BaseType_t bHasHandler = ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xTCP.pxHandleReceive );
const uint8_t *pucBuffer = NULL;
#endif /* ipconfigUSE_CALLBACKS */
/* int32_t uxStreamBufferAdd( pxBuffer, uxOffset, pucData, aCount )
if( pucData != NULL ) copy data the the buffer
if( pucData == NULL ) no copying, just advance rxHead
if( uxOffset != 0 ) Just store data which has come out-of-order
if( uxOffset == 0 ) Also advance rxHead */
if( pxStream == NULL )
{
pxStream = prvTCPCreateStream( pxSocket, pdTRUE );
if( pxStream == NULL )
{
return -1;
}
}
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( ( bHasHandler != pdFALSE ) && ( uxStreamBufferGetSize( pxStream ) == 0u ) && ( uxOffset == 0ul ) && ( pcData != NULL ) )
{
/* Data can be passed directly to the user */
pucBuffer = pcData;
/* Zero-copy for call-back: no need to add the bytes to the
stream, only the pointer will be advanced by uxStreamBufferAdd(). */
pcData = NULL;
}
}
#endif /* ipconfigUSE_CALLBACKS */
xResult = ( int32_t ) uxStreamBufferAdd( pxStream, uxOffset, pcData, ( size_t ) ulByteCount );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
if( xResult != ( int32_t ) ulByteCount )
{
FreeRTOS_debug_printf( ( "lTCPAddRxdata: at %ld: %ld/%lu bytes (tail %lu head %lu space %lu front %lu)\n",
uxOffset, xResult, ulByteCount,
pxStream->uxTail,
pxStream->uxHead,
uxStreamBufferFrontSpace( pxStream ),
pxStream->uxFront ) );
}
}
#endif /* ipconfigHAS_DEBUG_PRINTF */
if( uxOffset == 0u )
{
/* Data is being added to rxStream at the head (offs = 0) */
#if( ipconfigUSE_CALLBACKS == 1 )
if( bHasHandler != pdFALSE )
{
/* The socket owner has installed an OnReceive handler. Pass the
Rx data, without copying from the rxStream, to the user. */
for (;;)
{
uint8_t *ucReadPtr = NULL;
uint32_t ulCount;
if( pucBuffer != NULL )
{
ucReadPtr = ( uint8_t * )pucBuffer;
ulCount = ulByteCount;
pucBuffer = NULL;
}
else
{
ulCount = ( uint32_t ) uxStreamBufferGetPtr( pxStream, &( ucReadPtr ) );
}
if( ulCount == 0ul )
{
break;
}
pxSocket->u.xTCP.pxHandleReceive( (Socket_t *)pxSocket, ( void* )ucReadPtr, ( size_t ) ulCount );
uxStreamBufferGet( pxStream, 0ul, NULL, ( size_t ) ulCount, pdFALSE );
}
} else
#endif /* ipconfigUSE_CALLBACKS */
{
/* See if running out of space. */
if( pxSocket->u.xTCP.bits.bLowWater == pdFALSE_UNSIGNED )
{
size_t uxFrontSpace = uxStreamBufferFrontSpace( pxSocket->u.xTCP.rxStream );
if( uxFrontSpace <= pxSocket->u.xTCP.uxLittleSpace )
{
pxSocket->u.xTCP.bits.bLowWater = pdTRUE_UNSIGNED;
pxSocket->u.xTCP.bits.bWinChange = pdTRUE_UNSIGNED;
/* bLowWater was reached, send the changed window size. */
pxSocket->u.xTCP.usTimeout = 1u;
xSendEventToIPTask( eTCPTimerEvent );
}
}
/* New incoming data is available, wake up the user. User's
semaphores will be set just before the IP-task goes asleep. */
pxSocket->xEventBits |= eSOCKET_RECEIVE;
#if ipconfigSUPPORT_SELECT_FUNCTION == 1
{
if( ( pxSocket->xSelectBits & eSELECT_READ ) != 0 )
{
pxSocket->xEventBits |= ( eSELECT_READ << SOCKET_EVENT_BIT_COUNT );
}
}
#endif
}
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* Function to get the remote address and IP port */
BaseType_t FreeRTOS_GetRemoteAddress( Socket_t xSocket, struct freertos_sockaddr *pxAddress )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xResult;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xResult = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
/* BSD style sockets communicate IP and port addresses in network
byte order.
IP address of remote machine. */
pxAddress->sin_addr = FreeRTOS_htonl ( pxSocket->u.xTCP.ulRemoteIP );
/* Port on remote machine. */
pxAddress->sin_port = FreeRTOS_htons ( pxSocket->u.xTCP.usRemotePort );
xResult = ( BaseType_t ) sizeof( ( *pxAddress ) );
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* Returns the number of bytes that may be added to txStream */
BaseType_t FreeRTOS_maywrite( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xResult;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xResult = -pdFREERTOS_ERRNO_EINVAL;
}
else if( pxSocket->u.xTCP.ucTCPState != eESTABLISHED )
{
if( ( pxSocket->u.xTCP.ucTCPState < eCONNECT_SYN ) || ( pxSocket->u.xTCP.ucTCPState > eESTABLISHED ) )
{
xResult = -1;
}
else
{
xResult = 0;
}
}
else if( pxSocket->u.xTCP.txStream == NULL )
{
xResult = ( BaseType_t ) pxSocket->u.xTCP.uxTxStreamSize;
}
else
{
xResult = ( BaseType_t ) uxStreamBufferGetSpace( pxSocket->u.xTCP.txStream );
}
return xResult;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP ==1 )
BaseType_t FreeRTOS_tx_space( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
if( pxSocket->u.xTCP.txStream != NULL )
{
xReturn = ( BaseType_t ) uxStreamBufferGetSpace ( pxSocket->u.xTCP.txStream );
}
else
{
xReturn = ( BaseType_t ) pxSocket->u.xTCP.uxTxStreamSize;
}
}
return xReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
BaseType_t FreeRTOS_tx_size( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
if( pxSocket->u.xTCP.txStream != NULL )
{
xReturn = ( BaseType_t ) uxStreamBufferGetSize ( pxSocket->u.xTCP.txStream );
}
else
{
xReturn = 0;
}
}
return xReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* Returns pdTRUE if TCP socket is connected. */
BaseType_t FreeRTOS_issocketconnected( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn = pdFALSE;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
if( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED )
{
if( pxSocket->u.xTCP.ucTCPState < eCLOSE_WAIT )
{
xReturn = pdTRUE;
}
}
}
return xReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* returns the actual size of MSS being used */
BaseType_t FreeRTOS_mss( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
/* usCurMSS is declared as uint16_t to save space. FreeRTOS_mss()
will often be used in signed native-size expressions cast it to
BaseType_t. */
xReturn = ( BaseType_t ) ( pxSocket->u.xTCP.usCurMSS );
}
return xReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/* HT: for internal use only: return the connection status */
BaseType_t FreeRTOS_connstatus( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
/* Cast it to BaseType_t */
xReturn = ( BaseType_t ) ( pxSocket->u.xTCP.ucTCPState );
}
return xReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
/*
* Returns the number of bytes which can be read.
*/
BaseType_t FreeRTOS_rx_size( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
if( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_TCP )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else if( pxSocket->u.xTCP.rxStream != NULL )
{
xReturn = ( BaseType_t ) uxStreamBufferGetSize( pxSocket->u.xTCP.rxStream );
}
else
{
xReturn = 0;
}
return xReturn;
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP == 1 )
void FreeRTOS_netstat( void )
{
IPStackEvent_t xAskEvent;
/* Ask the IP-task to call vTCPNetStat()
* to avoid accessing xBoundTCPSocketsList
*/
xAskEvent.eEventType = eTCPNetStat;
xAskEvent.pvData = ( void * ) NULL;
xSendEventStructToIPTask( &xAskEvent, 1000u );
}
#endif /* ipconfigUSE_TCP */
/*-----------------------------------------------------------*/
#if( ( ipconfigHAS_PRINTF != 0 ) && ( ipconfigUSE_TCP == 1 ) )
void vTCPNetStat( void )
{
/* Show a simple listing of all created sockets and their connections */
ListItem_t *pxIterator;
BaseType_t count = 0;
if( listLIST_IS_INITIALISED( &xBoundTCPSocketsList ) == pdFALSE )
{
FreeRTOS_printf( ( "PLUS-TCP not initialized\n" ) );
}
else
{
FreeRTOS_printf( ( "Prot Port IP-Remote : Port R/T Status Alive tmout Child\n" ) );
for( pxIterator = ( ListItem_t * ) listGET_HEAD_ENTRY( &xBoundTCPSocketsList );
pxIterator != ( ListItem_t * ) listGET_END_MARKER( &xBoundTCPSocketsList );
pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator ) )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
#if( ipconfigTCP_KEEP_ALIVE == 1 )
TickType_t age = xTaskGetTickCount() - pxSocket->u.xTCP.xLastAliveTime;
#else
TickType_t age = 0u;
#endif
#if( ipconfigUSE_CALLBACKS == 1 )
void *pxHandleReceive = (void*)pxSocket->u.xTCP.pxHandleReceive;
#else
void *pxHandleReceive = (void*)NULL;
#endif
char ucChildText[16] = "";
if (pxSocket->u.xTCP.ucTCPState == eTCP_LISTEN)
{
const int32_t copied_len = snprintf( ucChildText, sizeof( ucChildText ), " %d/%d",
( int ) pxSocket->u.xTCP.usChildCount,
( int ) pxSocket->u.xTCP.usBacklog);
/* These should never evaluate to false since the buffers are both shorter than 5-6 characters (<=65535) */
configASSERT( copied_len >= 0 );
configASSERT( copied_len < sizeof( ucChildText ) );
}
FreeRTOS_printf( ( "TCP %5d %-16lxip:%5d %d/%d %-13.13s %6lu %6u%s\n",
pxSocket->usLocalPort, /* Local port on this machine */
pxSocket->u.xTCP.ulRemoteIP, /* IP address of remote machine */
pxSocket->u.xTCP.usRemotePort, /* Port on remote machine */
pxSocket->u.xTCP.rxStream != NULL,
pxSocket->u.xTCP.txStream != NULL,
FreeRTOS_GetTCPStateName( pxSocket->u.xTCP.ucTCPState ),
(age > 999999 ? 999999 : age), /* Format 'age' for printing */
pxSocket->u.xTCP.usTimeout,
ucChildText ) );
/* Remove compiler warnings if FreeRTOS_debug_printf() is not defined. */
( void ) pxHandleReceive;
count++;
}
for( pxIterator = ( ListItem_t * ) listGET_HEAD_ENTRY( &xBoundUDPSocketsList );
pxIterator != ( ListItem_t * ) listGET_END_MARKER( &xBoundUDPSocketsList );
pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator ) )
{
/* Local port on this machine */
FreeRTOS_printf( ( "UDP Port %5u\n",
FreeRTOS_ntohs( listGET_LIST_ITEM_VALUE( pxIterator ) ) ) );
count++;
}
FreeRTOS_printf( ( "FreeRTOS_netstat: %lu sockets %lu < %lu < %d buffers free\n",
count,
uxGetMinimumFreeNetworkBuffers( ),
uxGetNumberOfFreeNetworkBuffers( ),
ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ) );
}
}
#endif /* ( ( ipconfigHAS_PRINTF != 0 ) && ( ipconfigUSE_TCP == 1 ) ) */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
void vSocketSelect( SocketSelect_t *pxSocketSet )
{
BaseType_t xRound;
EventBits_t xSocketBits, xBitsToClear;
#if ipconfigUSE_TCP == 1
BaseType_t xLastRound = 1;
#else
BaseType_t xLastRound = 0;
#endif
/* These flags will be switched on after checking the socket status. */
EventBits_t xGroupBits = 0;
pxSocketSet->pxSocket = NULL;
for( xRound = 0; xRound <= xLastRound; xRound++ )
{
const ListItem_t *pxIterator;
const MiniListItem_t *pxEnd;
if( xRound == 0 )
{
pxEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xBoundUDPSocketsList );
}
#if ipconfigUSE_TCP == 1
else
{
pxEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xBoundTCPSocketsList );
}
#endif /* ipconfigUSE_TCP == 1 */
for( pxIterator = ( const ListItem_t * ) ( listGET_NEXT( pxEnd ) );
pxIterator != ( const ListItem_t * ) pxEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
if( pxSocket->pxSocketSet != pxSocketSet )
{
/* Socket does not belong to this select group. */
continue;
}
xSocketBits = 0;
#if( ipconfigUSE_TCP == 1 )
if( pxSocket->ucProtocol == FREERTOS_IPPROTO_TCP )
{
/* Check if the socket has already been accepted by the
owner. If not, it is useless to return it from a
select(). */
BaseType_t bAccepted = pdFALSE;
if( pxSocket->u.xTCP.bits.bPassQueued == pdFALSE_UNSIGNED )
{
if( pxSocket->u.xTCP.bits.bPassAccept == pdFALSE_UNSIGNED )
{
bAccepted = pdTRUE;
}
}
/* Is the set owner interested in READ events? */
if( ( pxSocket->xSelectBits & eSELECT_READ ) != 0 )
{
if( pxSocket->u.xTCP.ucTCPState == eTCP_LISTEN )
{
if( ( pxSocket->u.xTCP.pxPeerSocket != NULL ) && ( pxSocket->u.xTCP.pxPeerSocket->u.xTCP.bits.bPassAccept != 0 ) )
{
xSocketBits |= eSELECT_READ;
}
}
else if( ( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED ) && ( pxSocket->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED ) )
{
/* This socket has the re-use flag. After connecting it turns into
aconnected socket. Set the READ event, so that accept() will be called. */
xSocketBits |= eSELECT_READ;
}
else if( ( bAccepted != 0 ) && ( FreeRTOS_recvcount( pxSocket ) > 0 ) )
{
xSocketBits |= eSELECT_READ;
}
}
/* Is the set owner interested in EXCEPTION events? */
if( ( pxSocket->xSelectBits & eSELECT_EXCEPT ) != 0 )
{
if( ( pxSocket->u.xTCP.ucTCPState == eCLOSE_WAIT ) || ( pxSocket->u.xTCP.ucTCPState == eCLOSED ) )
{
xSocketBits |= eSELECT_EXCEPT;
}
}
/* Is the set owner interested in WRITE events? */
if( ( pxSocket->xSelectBits & eSELECT_WRITE ) != 0 )
{
BaseType_t bMatch = pdFALSE;
if( bAccepted != 0 )
{
if( FreeRTOS_tx_space( pxSocket ) > 0 )
{
bMatch = pdTRUE;
}
}
if( bMatch == pdFALSE )
{
if( ( pxSocket->u.xTCP.bits.bConnPrepared != pdFALSE_UNSIGNED ) &&
( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED ) &&
( pxSocket->u.xTCP.bits.bConnPassed == pdFALSE_UNSIGNED ) )
{
pxSocket->u.xTCP.bits.bConnPassed = pdTRUE_UNSIGNED;
bMatch = pdTRUE;
}
}
if( bMatch != pdFALSE )
{
xSocketBits |= eSELECT_WRITE;
}
}
}
else
#endif /* ipconfigUSE_TCP == 1 */
{
/* Select events for UDP are simpler. */
if( ( ( pxSocket->xSelectBits & eSELECT_READ ) != 0 ) &&
( listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ) > 0U ) )
{
xSocketBits |= eSELECT_READ;
}
/* The WRITE and EXCEPT bits are not used for UDP */
} /* if( pxSocket->ucProtocol == FREERTOS_IPPROTO_TCP ) */
/* Each socket keeps its own event flags, which are looked-up
by FreeRTOS_FD_ISSSET() */
pxSocket->xSocketBits = xSocketBits;
/* The ORed value will be used to set the bits in the event
group. */
xGroupBits |= xSocketBits;
} /* for( pxIterator ... ) */
} /* for( xRound = 0; xRound <= xLastRound; xRound++ ) */
xBitsToClear = xEventGroupGetBits( pxSocketSet->xSelectGroup );
/* Now set the necessary bits. */
xBitsToClear = ( xBitsToClear & ~xGroupBits ) & eSELECT_ALL;
#if( ipconfigSUPPORT_SIGNALS != 0 )
{
/* Maybe the socketset was signalled, but don't
clear the 'eSELECT_INTR' bit here, as it will be used
and cleared in FreeRTOS_select(). */
xBitsToClear &= ( EventBits_t ) ~eSELECT_INTR;
}
#endif /* ipconfigSUPPORT_SIGNALS */
if( xBitsToClear != 0 )
{
xEventGroupClearBits( pxSocketSet->xSelectGroup, xBitsToClear );
}
/* Now include eSELECT_CALL_IP to wakeup the caller. */
xEventGroupSetBits( pxSocketSet->xSelectGroup, xGroupBits | eSELECT_CALL_IP );
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SIGNALS != 0 )
/* Send a signal to the task which reads from this socket. */
BaseType_t FreeRTOS_SignalSocket( Socket_t xSocket )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
if( pxSocket == NULL )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
if( ( pxSocket->pxSocketSet != NULL ) && ( pxSocket->pxSocketSet->xSelectGroup != NULL ) )
{
xEventGroupSetBits( pxSocket->pxSocketSet->xSelectGroup, eSELECT_INTR );
xReturn = 0;
}
else
#endif /* ipconfigSUPPORT_SELECT_FUNCTION */
if( pxSocket->xEventGroup != NULL )
{
xEventGroupSetBits( pxSocket->xEventGroup, eSOCKET_INTR );
xReturn = 0;
}
else
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
return xReturn;
}
#endif /* ipconfigSUPPORT_SIGNALS */
/*-----------------------------------------------------------*/
#if( ipconfigSUPPORT_SIGNALS != 0 )
/* Send a signal to the task which reads from this socket (FromISR version). */
BaseType_t FreeRTOS_SignalSocketFromISR( Socket_t xSocket, BaseType_t *pxHigherPriorityTaskWoken )
{
FreeRTOS_Socket_t *pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
BaseType_t xReturn;
IPStackEvent_t xEvent;
extern QueueHandle_t xNetworkEventQueue;
configASSERT( pxSocket != NULL );
configASSERT( pxSocket->ucProtocol == FREERTOS_IPPROTO_TCP );
configASSERT( pxSocket->xEventGroup );
xEvent.eEventType = eSocketSignalEvent;
xEvent.pvData = ( void * )pxSocket;
/* The IP-task will call FreeRTOS_SignalSocket for this socket. */
xReturn = xQueueSendToBackFromISR( xNetworkEventQueue, &xEvent, pxHigherPriorityTaskWoken );
return xReturn;
}
#endif /* ipconfigSUPPORT_SIGNALS */
/*-----------------------------------------------------------*/
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
/*
* uxStreamBufferAdd( )
* Adds data to a stream buffer. If uxOffset > 0, data will be written at
* an offset from uxHead while uxHead will not be moved yet. This possibility
* will be used when TCP data is received while earlier data is still missing.
* If 'pucData' equals NULL, the function is called to advance 'uxHead' only.
*/
size_t uxStreamBufferAdd( StreamBuffer_t *pxBuffer, size_t uxOffset, const uint8_t *pucData, size_t uxCount )
{
size_t uxSpace, uxNextHead, uxFirst;
uxSpace = uxStreamBufferGetSpace( pxBuffer );
/* If uxOffset > 0, items can be placed in front of uxHead */
if( uxSpace > uxOffset )
{
uxSpace -= uxOffset;
}
else
{
uxSpace = 0u;
}
/* The number of bytes that can be written is the minimum of the number of
bytes requested and the number available. */
uxCount = FreeRTOS_min_uint32( uxSpace, uxCount );
if( uxCount != 0u )
{
uxNextHead = pxBuffer->uxHead;
if( uxOffset != 0u )
{
/* ( uxOffset > 0 ) means: write in front if the uxHead marker */
uxNextHead += uxOffset;
if( uxNextHead >= pxBuffer->LENGTH )
{
uxNextHead -= pxBuffer->LENGTH;
}
}
if( pucData != NULL )
{
/* Calculate the number of bytes that can be added in the first
write - which may be less than the total number of bytes that need
to be added if the buffer will wrap back to the beginning. */
uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextHead, uxCount );
/* Write as many bytes as can be written in the first write. */
memcpy( ( void* ) ( pxBuffer->ucArray + uxNextHead ), pucData, uxFirst );
/* If the number of bytes written was less than the number that
could be written in the first write... */
if( uxCount > uxFirst )
{
/* ...then write the remaining bytes to the start of the
buffer. */
memcpy( ( void * )pxBuffer->ucArray, pucData + uxFirst, uxCount - uxFirst );
}
}
if( uxOffset == 0u )
{
/* ( uxOffset == 0 ) means: write at uxHead position */
uxNextHead += uxCount;
if( uxNextHead >= pxBuffer->LENGTH )
{
uxNextHead -= pxBuffer->LENGTH;
}
pxBuffer->uxHead = uxNextHead;
}
if( xStreamBufferLessThenEqual( pxBuffer, pxBuffer->uxFront, uxNextHead ) != pdFALSE )
{
/* Advance the front pointer */
pxBuffer->uxFront = uxNextHead;
}
}
return uxCount;
}
/*-----------------------------------------------------------*/
/*
* uxStreamBufferGet( )
* 'uxOffset' can be used to read data located at a certain offset from 'lTail'.
* If 'pucData' equals NULL, the function is called to advance 'lTail' only.
* if 'xPeek' is pdTRUE, or if 'uxOffset' is non-zero, the 'lTail' pointer will
* not be advanced.
*/
size_t uxStreamBufferGet( StreamBuffer_t *pxBuffer, size_t uxOffset, uint8_t *pucData, size_t uxMaxCount, BaseType_t xPeek )
{
size_t uxSize, uxCount, uxFirst, uxNextTail;
/* How much data is available? */
uxSize = uxStreamBufferGetSize( pxBuffer );
if( uxSize > uxOffset )
{
uxSize -= uxOffset;
}
else
{
uxSize = 0u;
}
/* Use the minimum of the wanted bytes and the available bytes. */
uxCount = FreeRTOS_min_uint32( uxSize, uxMaxCount );
if( uxCount > 0u )
{
uxNextTail = pxBuffer->uxTail;
if( uxOffset != 0u )
{
uxNextTail += uxOffset;
if( uxNextTail >= pxBuffer->LENGTH )
{
uxNextTail -= pxBuffer->LENGTH;
}
}
if( pucData != NULL )
{
/* Calculate the number of bytes that can be read - which may be
less than the number wanted if the data wraps around to the start of
the buffer. */
uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextTail, uxCount );
/* Obtain the number of bytes it is possible to obtain in the first
read. */
memcpy( pucData, pxBuffer->ucArray + uxNextTail, uxFirst );
/* If the total number of wanted bytes is greater than the number
that could be read in the first read... */
if( uxCount > uxFirst )
{
/*...then read the remaining bytes from the start of the buffer. */
memcpy( pucData + uxFirst, pxBuffer->ucArray, uxCount - uxFirst );
}
}
if( ( xPeek == pdFALSE ) && ( uxOffset == 0UL ) )
{
/* Move the tail pointer to effecively remove the data read from
the buffer. */
uxNextTail += uxCount;
if( uxNextTail >= pxBuffer->LENGTH )
{
uxNextTail -= pxBuffer->LENGTH;
}
pxBuffer->uxTail = uxNextTail;
}
}
return uxCount;
}
/*
* FreeRTOS+TCP V2.0.11
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/*
* FreeRTOS_TCP_IP.c
* Module which handles the TCP connections for FreeRTOS+TCP.
* It depends on FreeRTOS_TCP_WIN.c, which handles the TCP windowing
* schemes.
*
* Endianness: in this module all ports and IP addresses are stored in
* host byte-order, except fields in the IP-packets
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_TCP_IP.h"
#include "FreeRTOS_DHCP.h"
#include "NetworkInterface.h"
#include "NetworkBufferManagement.h"
#include "FreeRTOS_ARP.h"
#include "FreeRTOS_TCP_WIN.h"
/* Just make sure the contents doesn't get compiled if TCP is not enabled. */
#if ipconfigUSE_TCP == 1
/* This compile-time test was moved to here because some macro's
were unknown within 'FreeRTOSIPConfigDefaults.h'. It tests whether
the defined MTU size can contain at least a complete TCP packet. */
#if ( ( ipconfigTCP_MSS + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) > ipconfigNETWORK_MTU )
#error The ipconfigTCP_MSS setting in FreeRTOSIPConfig.h is too large.
#endif
/*
* The meaning of the TCP flags:
*/
#define ipTCP_FLAG_FIN 0x0001u /* No more data from sender */
#define ipTCP_FLAG_SYN 0x0002u /* Synchronize sequence numbers */
#define ipTCP_FLAG_RST 0x0004u /* Reset the connection */
#define ipTCP_FLAG_PSH 0x0008u /* Push function: please push buffered data to the recv application */
#define ipTCP_FLAG_ACK 0x0010u /* Acknowledgment field is significant */
#define ipTCP_FLAG_URG 0x0020u /* Urgent pointer field is significant */
#define ipTCP_FLAG_ECN 0x0040u /* ECN-Echo */
#define ipTCP_FLAG_CWR 0x0080u /* Congestion Window Reduced */
#define ipTCP_FLAG_NS 0x0100u /* ECN-nonce concealment protection */
#define ipTCP_FLAG_RSV 0x0E00u /* Reserved, keep 0 */
/* A mask to filter all protocol flags. */
#define ipTCP_FLAG_CTRL 0x001Fu
/*
* A few values of the TCP options:
*/
#define TCP_OPT_END 0u /* End of TCP options list */
#define TCP_OPT_NOOP 1u /* "No-operation" TCP option */
#define TCP_OPT_MSS 2u /* Maximum segment size TCP option */
#define TCP_OPT_WSOPT 3u /* TCP Window Scale Option (3-byte long) */
#define TCP_OPT_SACK_P 4u /* Advertize that SACK is permitted */
#define TCP_OPT_SACK_A 5u /* SACK option with first/last */
#define TCP_OPT_TIMESTAMP 8u /* Time-stamp option */
#define TCP_OPT_MSS_LEN 4u /* Length of TCP MSS option. */
#define TCP_OPT_WSOPT_LEN 3u /* Length of TCP WSOPT option. */
#define TCP_OPT_TIMESTAMP_LEN 10 /* fixed length of the time-stamp option */
#ifndef ipconfigTCP_ACK_EARLIER_PACKET
#define ipconfigTCP_ACK_EARLIER_PACKET 1
#endif
/*
* The macro NOW_CONNECTED() is use to determine if the connection makes a
* transition from connected to non-connected and vice versa.
* NOW_CONNECTED() returns true when the status has one of these values:
* eESTABLISHED, eFIN_WAIT_1, eFIN_WAIT_2, eCLOSING, eLAST_ACK, eTIME_WAIT
* Technically the connection status is closed earlier, but the library wants
* to prevent that the socket will be deleted before the last ACK has been
* and thus causing a 'RST' packet on either side.
*/
#define NOW_CONNECTED( status )\
( ( status >= eESTABLISHED ) && ( status != eCLOSE_WAIT ) )
/*
* The highest 4 bits in the TCP offset byte indicate the total length of the
* TCP header, divided by 4.
*/
#define VALID_BITS_IN_TCP_OFFSET_BYTE ( 0xF0u )
/*
* Acknowledgements to TCP data packets may be delayed as long as more is being expected.
* A normal delay would be 200ms. Here a much shorter delay of 20 ms is being used to
* gain performance.
*/
#define DELAYED_ACK_SHORT_DELAY_MS ( 2 )
#define DELAYED_ACK_LONGER_DELAY_MS ( 20 )
/*
* The MSS (Maximum Segment Size) will be taken as large as possible. However, packets with
* an MSS of 1460 bytes won't be transported through the internet. The MSS will be reduced
* to 1400 bytes.
*/
#define REDUCED_MSS_THROUGH_INTERNET ( 1400 )
/*
* When there are no TCP options, the TCP offset equals 20 bytes, which is stored as
* the number 5 (words) in the higher niblle of the TCP-offset byte.
*/
#define TCP_OFFSET_LENGTH_BITS ( 0xf0u )
#define TCP_OFFSET_STANDARD_LENGTH ( 0x50u )
/*
* Each TCP socket is checked regularly to see if it can send data packets.
* By default, the maximum number of packets sent during one check is limited to 8.
* This amount may be further limited by setting the socket's TX window size.
*/
#if( !defined( SEND_REPEATED_COUNT ) )
#define SEND_REPEATED_COUNT ( 8 )
#endif /* !defined( SEND_REPEATED_COUNT ) */
/*
* Define a maximum perdiod of time (ms) to leave a TCP-socket unattended.
* When a TCP timer expires, retries and keep-alive messages will be checked.
*/
#ifndef tcpMAXIMUM_TCP_WAKEUP_TIME_MS
#define tcpMAXIMUM_TCP_WAKEUP_TIME_MS 20000u
#endif
/*
* The names of the different TCP states may be useful in logging.
*/
#if( ( ipconfigHAS_DEBUG_PRINTF != 0 ) || ( ipconfigHAS_PRINTF != 0 ) )
static const char *pcStateNames[] = {
"eCLOSED",
"eTCP_LISTEN",
"eCONNECT_SYN",
"eSYN_FIRST",
"eSYN_RECEIVED",
"eESTABLISHED",
"eFIN_WAIT_1",
"eFIN_WAIT_2",
"eCLOSE_WAIT",
"eCLOSING",
"eLAST_ACK",
"eTIME_WAIT",
"eUNKNOWN",
};
#endif /* ( ipconfigHAS_DEBUG_PRINTF != 0 ) || ( ipconfigHAS_PRINTF != 0 ) */
/*
* Returns true if the socket must be checked. Non-active sockets are waiting
* for user action, either connect() or close().
*/
static BaseType_t prvTCPSocketIsActive( UBaseType_t uxStatus );
/*
* Either sends a SYN or calls prvTCPSendRepeated (for regular messages).
*/
static int32_t prvTCPSendPacket( FreeRTOS_Socket_t *pxSocket );
/*
* Try to send a series of messages.
*/
static int32_t prvTCPSendRepeated( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer );
/*
* Return or send a packet to the other party.
*/
static void prvTCPReturnPacket( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer,
uint32_t ulLen, BaseType_t xReleaseAfterSend );
/*
* Initialise the data structures which keep track of the TCP windowing system.
*/
static void prvTCPCreateWindow( FreeRTOS_Socket_t *pxSocket );
/*
* Let ARP look-up the MAC-address of the peer and initialise the first SYN
* packet.
*/
static BaseType_t prvTCPPrepareConnect( FreeRTOS_Socket_t *pxSocket );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
/*
* For logging and debugging: make a string showing the TCP flags.
*/
static const char *prvTCPFlagMeaning( UBaseType_t xFlags);
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
/*
* Parse the TCP option(s) received, if present.
*/
static void prvCheckOptions( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer );
/*
* Set the initial properties in the options fields, like the preferred
* value of MSS and whether SACK allowed. Will be transmitted in the state
* 'eCONNECT_SYN'.
*/
static UBaseType_t prvSetSynAckOptions( FreeRTOS_Socket_t *pxSocket, TCPPacket_t * pxTCPPacket );
/*
* For anti-hang protection and TCP keep-alive messages. Called in two places:
* after receiving a packet and after a state change. The socket's alive timer
* may be reset.
*/
static void prvTCPTouchSocket( FreeRTOS_Socket_t *pxSocket );
/*
* Prepare an outgoing message, if anything has to be sent.
*/
static int32_t prvTCPPrepareSend( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer, UBaseType_t uxOptionsLength );
/*
* Calculate when this socket needs to be checked to do (re-)transmissions.
*/
static TickType_t prvTCPNextTimeout( FreeRTOS_Socket_t *pxSocket );
/*
* The API FreeRTOS_send() adds data to the TX stream. Add
* this data to the windowing system to it can be transmitted.
*/
static void prvTCPAddTxData( FreeRTOS_Socket_t *pxSocket );
/*
* Called to handle the closure of a TCP connection.
*/
static BaseType_t prvTCPHandleFin( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer );
/*
* Called from prvTCPHandleState(). Find the TCP payload data and check and
* return its length.
*/
static BaseType_t prvCheckRxData( NetworkBufferDescriptor_t *pxNetworkBuffer, uint8_t **ppucRecvData );
/*
* Called from prvTCPHandleState(). Check if the payload data may be accepted.
* If so, it will be added to the socket's reception queue.
*/
static BaseType_t prvStoreRxData( FreeRTOS_Socket_t *pxSocket, uint8_t *pucRecvData,
NetworkBufferDescriptor_t *pxNetworkBuffer, uint32_t ulReceiveLength );
/*
* Set the TCP options (if any) for the outgoing packet.
*/
static UBaseType_t prvSetOptions( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer );
/*
* Called from prvTCPHandleState() as long as the TCP status is eSYN_RECEIVED to
* eCONNECT_SYN.
*/
static BaseType_t prvHandleSynReceived( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer,
uint32_t ulReceiveLength, UBaseType_t uxOptionsLength );
/*
* Called from prvTCPHandleState() as long as the TCP status is eESTABLISHED.
*/
static BaseType_t prvHandleEstablished( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer,
uint32_t ulReceiveLength, UBaseType_t uxOptionsLength );
/*
* Called from prvTCPHandleState(). There is data to be sent.
* If ipconfigUSE_TCP_WIN is defined, and if only an ACK must be sent, it will
* be checked if it would better be postponed for efficiency.
*/
static BaseType_t prvSendData( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer,
uint32_t ulReceiveLength, BaseType_t xSendLength );
/*
* The heart of all: check incoming packet for valid data and acks and do what
* is necessary in each state.
*/
static BaseType_t prvTCPHandleState( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer );
/*
* Reply to a peer with the RST flag on, in case a packet can not be handled.
*/
static BaseType_t prvTCPSendReset( NetworkBufferDescriptor_t *pxNetworkBuffer );
/*
* Set the initial value for MSS (Maximum Segment Size) to be used.
*/
static void prvSocketSetMSS( FreeRTOS_Socket_t *pxSocket );
/*
* Return either a newly created socket, or the current socket in a connected
* state (depends on the 'bReuseSocket' flag).
*/
static FreeRTOS_Socket_t *prvHandleListen( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer );
/*
* After a listening socket receives a new connection, it may duplicate itself.
* The copying takes place in prvTCPSocketCopy.
*/
static BaseType_t prvTCPSocketCopy( FreeRTOS_Socket_t *pxNewSocket, FreeRTOS_Socket_t *pxSocket );
/*
* prvTCPStatusAgeCheck() will see if the socket has been in a non-connected
* state for too long. If so, the socket will be closed, and -1 will be
* returned.
*/
#if( ipconfigTCP_HANG_PROTECTION == 1 )
static BaseType_t prvTCPStatusAgeCheck( FreeRTOS_Socket_t *pxSocket );
#endif
static NetworkBufferDescriptor_t *prvTCPBufferResize( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer,
int32_t lDataLen, UBaseType_t uxOptionsLength );
#if( ( ipconfigHAS_DEBUG_PRINTF != 0 ) || ( ipconfigHAS_PRINTF != 0 ) )
const char *FreeRTOS_GetTCPStateName( UBaseType_t ulState );
#endif
#if( ipconfigUSE_TCP_WIN != 0 )
static uint8_t prvWinScaleFactor( FreeRTOS_Socket_t *pxSocket );
#endif
/*
* Generate a randomized TCP Initial Sequence Number per RFC.
*/
extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
uint16_t usSourcePort,
uint32_t ulDestinationAddress,
uint16_t usDestinationPort );
/*-----------------------------------------------------------*/
/* prvTCPSocketIsActive() returns true if the socket must be checked.
* Non-active sockets are waiting for user action, either connect()
* or close(). */
static BaseType_t prvTCPSocketIsActive( UBaseType_t uxStatus )
{
switch( uxStatus )
{
case eCLOSED:
case eCLOSE_WAIT:
case eFIN_WAIT_2:
case eCLOSING:
case eTIME_WAIT:
return pdFALSE;
default:
return pdTRUE;
}
}
/*-----------------------------------------------------------*/
#if( ipconfigTCP_HANG_PROTECTION == 1 )
static BaseType_t prvTCPStatusAgeCheck( FreeRTOS_Socket_t *pxSocket )
{
BaseType_t xResult;
switch( pxSocket->u.xTCP.ucTCPState )
{
case eESTABLISHED:
/* If the 'ipconfigTCP_KEEP_ALIVE' option is enabled, sockets in
state ESTABLISHED can be protected using keep-alive messages. */
xResult = pdFALSE;
break;
case eCLOSED:
case eTCP_LISTEN:
case eCLOSE_WAIT:
/* These 3 states may last for ever, up to the owner. */
xResult = pdFALSE;
break;
default:
/* All other (non-connected) states will get anti-hanging
protection. */
xResult = pdTRUE;
break;
}
if( xResult != pdFALSE )
{
/* How much time has past since the last active moment which is
defined as A) a state change or B) a packet has arrived. */
TickType_t xAge = xTaskGetTickCount( ) - pxSocket->u.xTCP.xLastActTime;
/* ipconfigTCP_HANG_PROTECTION_TIME is in units of seconds. */
if( xAge > ( ipconfigTCP_HANG_PROTECTION_TIME * configTICK_RATE_HZ ) )
{
#if( ipconfigHAS_DEBUG_PRINTF == 1 )
{
FreeRTOS_debug_printf( ( "Inactive socket closed: port %u rem %lxip:%u status %s\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort,
FreeRTOS_GetTCPStateName( ( UBaseType_t ) pxSocket->u.xTCP.ucTCPState ) ) );
}
#endif /* ipconfigHAS_DEBUG_PRINTF */
/* Move to eCLOSE_WAIT, user may close the socket. */
vTCPStateChange( pxSocket, eCLOSE_WAIT );
/* When 'bPassQueued' true, this socket is an orphan until it
gets connected. */
if( pxSocket->u.xTCP.bits.bPassQueued != pdFALSE_UNSIGNED )
{
if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
{
/* As it did not get connected, and the user can never
accept() it anymore, it will be deleted now. Called from
the IP-task, so it's safe to call the internal Close
function: vSocketClose(). */
vSocketClose( pxSocket );
}
/* Return a negative value to tell to inform the caller
xTCPTimerCheck()
that the socket got closed and may not be accessed anymore. */
xResult = -1;
}
}
}
return xResult;
}
/*-----------------------------------------------------------*/
#endif
/*
* As soon as a TCP socket timer expires, this function xTCPSocketCheck
* will be called (from xTCPTimerCheck)
* It can send a delayed ACK or new data
* Sequence of calling (normally) :
* IP-Task:
* xTCPTimerCheck() // Check all sockets ( declared in FreeRTOS_Sockets.c )
* xTCPSocketCheck() // Either send a delayed ACK or call prvTCPSendPacket()
* prvTCPSendPacket() // Either send a SYN or call prvTCPSendRepeated ( regular messages )
* prvTCPSendRepeated() // Send at most 8 messages on a row
* prvTCPReturnPacket() // Prepare for returning
* xNetworkInterfaceOutput() // Sends data to the NIC ( declared in portable/NetworkInterface/xxx )
*/
BaseType_t xTCPSocketCheck( FreeRTOS_Socket_t *pxSocket )
{
BaseType_t xResult = 0;
BaseType_t xReady = pdFALSE;
if( ( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED ) && ( pxSocket->u.xTCP.txStream != NULL ) )
{
/* The API FreeRTOS_send() might have added data to the TX stream. Add
this data to the windowing system to it can be transmitted. */
prvTCPAddTxData( pxSocket );
}
#if ipconfigUSE_TCP_WIN == 1
{
if( pxSocket->u.xTCP.pxAckMessage != NULL )
{
/* The first task of this regular socket check is to send-out delayed
ACK's. */
if( pxSocket->u.xTCP.bits.bUserShutdown == pdFALSE_UNSIGNED )
{
/* Earlier data was received but not yet acknowledged. This
function is called when the TCP timer for the socket expires, the
ACK may be sent now. */
if( pxSocket->u.xTCP.ucTCPState != eCLOSED )
{
if( xTCPWindowLoggingLevel > 1 && ipconfigTCP_MAY_LOG_PORT( pxSocket->usLocalPort ) )
{
FreeRTOS_debug_printf( ( "Send[%u->%u] del ACK %lu SEQ %lu (len %u)\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usRemotePort,
pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber - pxSocket->u.xTCP.xTCPWindow.rx.ulFirstSequenceNumber,
pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber - pxSocket->u.xTCP.xTCPWindow.tx.ulFirstSequenceNumber,
ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) );
}
prvTCPReturnPacket( pxSocket, pxSocket->u.xTCP.pxAckMessage, ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER, ipconfigZERO_COPY_TX_DRIVER );
#if( ipconfigZERO_COPY_TX_DRIVER != 0 )
{
/* The ownership has been passed to the SEND routine,
clear the pointer to it. */
pxSocket->u.xTCP.pxAckMessage = NULL;
}
#endif /* ipconfigZERO_COPY_TX_DRIVER */
}
if( prvTCPNextTimeout( pxSocket ) > 1 )
{
/* Tell the code below that this function is ready. */
xReady = pdTRUE;
}
}
else
{
/* The user wants to perform an active shutdown(), skip sending
the delayed ACK. The function prvTCPSendPacket() will send the
FIN along with the ACK's. */
}
if( pxSocket->u.xTCP.pxAckMessage != NULL )
{
vReleaseNetworkBufferAndDescriptor( pxSocket->u.xTCP.pxAckMessage );
pxSocket->u.xTCP.pxAckMessage = NULL;
}
}
}
#endif /* ipconfigUSE_TCP_WIN */
if( xReady == pdFALSE )
{
/* The second task of this regular socket check is sending out data. */
if( ( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED ) ||
( pxSocket->u.xTCP.ucTCPState == eCONNECT_SYN ) )
{
prvTCPSendPacket( pxSocket );
}
/* Set the time-out for the next wakeup for this socket. */
prvTCPNextTimeout( pxSocket );
#if( ipconfigTCP_HANG_PROTECTION == 1 )
{
/* In all (non-connected) states in which keep-alive messages can not be sent
the anti-hang protocol will close sockets that are 'hanging'. */
xResult = prvTCPStatusAgeCheck( pxSocket );
}
#endif
}
return xResult;
}
/*-----------------------------------------------------------*/
/*
* prvTCPSendPacket() will be called when the socket time-out has been reached.
* It is only called by xTCPSocketCheck().
*/
static int32_t prvTCPSendPacket( FreeRTOS_Socket_t *pxSocket )
{
int32_t lResult = 0;
UBaseType_t uxOptionsLength;
TCPPacket_t *pxTCPPacket;
NetworkBufferDescriptor_t *pxNetworkBuffer;
if( pxSocket->u.xTCP.ucTCPState != eCONNECT_SYN )
{
/* The connection is in s state other than SYN. */
pxNetworkBuffer = NULL;
/* prvTCPSendRepeated() will only create a network buffer if necessary,
i.e. when data must be sent to the peer. */
lResult = prvTCPSendRepeated( pxSocket, &pxNetworkBuffer );
if( pxNetworkBuffer != NULL )
{
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
}
}
else
{
if( pxSocket->u.xTCP.ucRepCount >= 3u )
{
/* The connection is in the SYN status. The packet will be repeated
to most 3 times. When there is no response, the socket get the
status 'eCLOSE_WAIT'. */
FreeRTOS_debug_printf( ( "Connect: giving up %lxip:%u\n",
pxSocket->u.xTCP.ulRemoteIP, /* IP address of remote machine. */
pxSocket->u.xTCP.usRemotePort ) ); /* Port on remote machine. */
vTCPStateChange( pxSocket, eCLOSE_WAIT );
}
else if( ( pxSocket->u.xTCP.bits.bConnPrepared != pdFALSE_UNSIGNED ) || ( prvTCPPrepareConnect( pxSocket ) == pdTRUE ) )
{
/* Or else, if the connection has been prepared, or can be prepared
now, proceed to send the packet with the SYN flag.
prvTCPPrepareConnect() prepares 'xPacket' and returns pdTRUE if
the Ethernet address of the peer or the gateway is found. */
pxTCPPacket = ( TCPPacket_t * )pxSocket->u.xTCP.xPacket.u.ucLastPacket;
/* About to send a SYN packet. Call prvSetSynAckOptions() to set
the proper options: The size of MSS and whether SACK's are
allowed. */
uxOptionsLength = prvSetSynAckOptions( pxSocket, pxTCPPacket );
/* Return the number of bytes to be sent. */
lResult = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength );
/* Set the TCP offset field: ipSIZE_OF_TCP_HEADER equals 20 and
uxOptionsLength is always a multiple of 4. The complete expression
would be:
ucTCPOffset = ( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) / 4 ) << 4 */
pxTCPPacket->xTCPHeader.ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
/* Repeat Count is used for a connecting socket, to limit the number
of tries. */
pxSocket->u.xTCP.ucRepCount++;
/* Send the SYN message to make a connection. The messages is
stored in the socket field 'xPacket'. It will be wrapped in a
pseudo network buffer descriptor before it will be sent. */
prvTCPReturnPacket( pxSocket, NULL, ( uint32_t ) lResult, pdFALSE );
}
}
/* Return the total number of bytes sent. */
return lResult;
}
/*-----------------------------------------------------------*/
/*
* prvTCPSendRepeated will try to send a series of messages, as long as there is
* data to be sent and as long as the transmit window isn't full.
*/
static int32_t prvTCPSendRepeated( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer )
{
UBaseType_t uxIndex;
int32_t lResult = 0;
UBaseType_t uxOptionsLength = 0u;
int32_t xSendLength;
for( uxIndex = 0u; uxIndex < ( UBaseType_t ) SEND_REPEATED_COUNT; uxIndex++ )
{
/* prvTCPPrepareSend() might allocate a network buffer if there is data
to be sent. */
xSendLength = prvTCPPrepareSend( pxSocket, ppxNetworkBuffer, uxOptionsLength );
if( xSendLength <= 0 )
{
break;
}
/* And return the packet to the peer. */
prvTCPReturnPacket( pxSocket, *ppxNetworkBuffer, ( uint32_t ) xSendLength, ipconfigZERO_COPY_TX_DRIVER );
#if( ipconfigZERO_COPY_TX_DRIVER != 0 )
{
*ppxNetworkBuffer = NULL;
}
#endif /* ipconfigZERO_COPY_TX_DRIVER */
lResult += xSendLength;
}
/* Return the total number of bytes sent. */
return lResult;
}
/*-----------------------------------------------------------*/
/*
* Return (or send) a packet the the peer. The data is stored in pxBuffer,
* which may either point to a real network buffer or to a TCP socket field
* called 'xTCP.xPacket'. A temporary xNetworkBuffer will be used to pass
* the data to the NIC.
*/
static void prvTCPReturnPacket( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer, uint32_t ulLen, BaseType_t xReleaseAfterSend )
{
TCPPacket_t * pxTCPPacket;
IPHeader_t *pxIPHeader;
EthernetHeader_t *pxEthernetHeader;
uint32_t ulFrontSpace, ulSpace, ulSourceAddress, ulWinSize;
TCPWindow_t *pxTCPWindow;
NetworkBufferDescriptor_t xTempBuffer;
/* For sending, a pseudo network buffer will be used, as explained above. */
if( pxNetworkBuffer == NULL )
{
pxNetworkBuffer = &xTempBuffer;
#if( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
{
xTempBuffer.pxNextBuffer = NULL;
}
#endif
xTempBuffer.pucEthernetBuffer = pxSocket->u.xTCP.xPacket.u.ucLastPacket;
xTempBuffer.xDataLength = sizeof( pxSocket->u.xTCP.xPacket.u.ucLastPacket );
xReleaseAfterSend = pdFALSE;
}
#if( ipconfigZERO_COPY_TX_DRIVER != 0 )
{
if( xReleaseAfterSend == pdFALSE )
{
pxNetworkBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, ( BaseType_t ) pxNetworkBuffer->xDataLength );
if( pxNetworkBuffer == NULL )
{
FreeRTOS_debug_printf( ( "prvTCPReturnPacket: duplicate failed\n" ) );
}
xReleaseAfterSend = pdTRUE;
}
}
#endif /* ipconfigZERO_COPY_TX_DRIVER */
if( pxNetworkBuffer != NULL )
{
pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
pxIPHeader = &pxTCPPacket->xIPHeader;
pxEthernetHeader = &pxTCPPacket->xEthernetHeader;
/* Fill the packet, using hton translations. */
if( pxSocket != NULL )
{
/* Calculate the space in the RX buffer in order to advertise the
size of this socket's reception window. */
pxTCPWindow = &( pxSocket->u.xTCP.xTCPWindow );
if( pxSocket->u.xTCP.rxStream != NULL )
{
/* An RX stream was created already, see how much space is
available. */
ulFrontSpace = ( uint32_t ) uxStreamBufferFrontSpace( pxSocket->u.xTCP.rxStream );
}
else
{
/* No RX stream has been created, the full stream size is
available. */
ulFrontSpace = ( uint32_t ) pxSocket->u.xTCP.uxRxStreamSize;
}
/* Take the minimum of the RX buffer space and the RX window size. */
ulSpace = FreeRTOS_min_uint32( pxSocket->u.xTCP.ulRxCurWinSize, pxTCPWindow->xSize.ulRxWindowLength );
if( ( pxSocket->u.xTCP.bits.bLowWater != pdFALSE_UNSIGNED ) || ( pxSocket->u.xTCP.bits.bRxStopped != pdFALSE_UNSIGNED ) )
{
/* The low-water mark was reached, meaning there was little
space left. The socket will wait until the application has read
or flushed the incoming data, and 'zero-window' will be
advertised. */
ulSpace = 0u;
}
/* If possible, advertise an RX window size of at least 1 MSS, otherwise
the peer might start 'zero window probing', i.e. sending small packets
(1, 2, 4, 8... bytes). */
if( ( ulSpace < pxSocket->u.xTCP.usCurMSS ) && ( ulFrontSpace >= pxSocket->u.xTCP.usCurMSS ) )
{
ulSpace = pxSocket->u.xTCP.usCurMSS;
}
/* Avoid overflow of the 16-bit win field. */
#if( ipconfigUSE_TCP_WIN != 0 )
{
ulWinSize = ( ulSpace >> pxSocket->u.xTCP.ucMyWinScaleFactor );
}
#else
{
ulWinSize = ulSpace;
}
#endif
if( ulWinSize > 0xfffcUL )
{
ulWinSize = 0xfffcUL;
}
pxTCPPacket->xTCPHeader.usWindow = FreeRTOS_htons( ( uint16_t ) ulWinSize );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
if( ipconfigTCP_MAY_LOG_PORT( pxSocket->usLocalPort ) != pdFALSE )
{
if( ( xTCPWindowLoggingLevel != 0 ) && ( pxSocket->u.xTCP.bits.bWinChange != pdFALSE_UNSIGNED ) )
{
size_t uxFrontSpace;
if(pxSocket->u.xTCP.rxStream != NULL)
{
uxFrontSpace = uxStreamBufferFrontSpace( pxSocket->u.xTCP.rxStream ) ;
}
else
{
uxFrontSpace = 0u;
}
FreeRTOS_debug_printf( ( "%s: %lxip:%u: [%lu < %lu] winSize %ld\n",
pxSocket->u.xTCP.bits.bLowWater ? "STOP" : "GO ",
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort,
pxSocket->u.xTCP.bits.bLowWater ? pxSocket->u.xTCP.uxLittleSpace : uxFrontSpace, pxSocket->u.xTCP.uxEnoughSpace,
(int32_t) ( pxTCPWindow->rx.ulHighestSequenceNumber - pxTCPWindow->rx.ulCurrentSequenceNumber ) ) );
}
}
}
#endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
/* The new window size has been advertised, switch off the flag. */
pxSocket->u.xTCP.bits.bWinChange = pdFALSE_UNSIGNED;
/* Later on, when deciding to delay an ACK, a precise estimate is needed
of the free RX space. At this moment, 'ulHighestRxAllowed' would be the
highest sequence number minus 1 that the socket will accept. */
pxSocket->u.xTCP.ulHighestRxAllowed = pxTCPWindow->rx.ulCurrentSequenceNumber + ulSpace;
#if( ipconfigTCP_KEEP_ALIVE == 1 )
if( pxSocket->u.xTCP.bits.bSendKeepAlive != pdFALSE_UNSIGNED )
{
/* Sending a keep-alive packet, send the current sequence number
minus 1, which will be recognised as a keep-alive packet an
responded to by acknowledging the last byte. */
pxSocket->u.xTCP.bits.bSendKeepAlive = pdFALSE_UNSIGNED;
pxSocket->u.xTCP.bits.bWaitKeepAlive = pdTRUE_UNSIGNED;
pxTCPPacket->xTCPHeader.ulSequenceNumber = pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber - 1UL;
pxTCPPacket->xTCPHeader.ulSequenceNumber = FreeRTOS_htonl( pxTCPPacket->xTCPHeader.ulSequenceNumber );
}
else
#endif
{
pxTCPPacket->xTCPHeader.ulSequenceNumber = FreeRTOS_htonl( pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber );
if( ( pxTCPPacket->xTCPHeader.ucTCPFlags & ( uint8_t ) ipTCP_FLAG_FIN ) != 0u )
{
/* Suppress FIN in case this packet carries earlier data to be
retransmitted. */
uint32_t ulDataLen = ( uint32_t ) ( ulLen - ( ipSIZE_OF_TCP_HEADER + ipSIZE_OF_IPv4_HEADER ) );
if( ( pxTCPWindow->ulOurSequenceNumber + ulDataLen ) != pxTCPWindow->tx.ulFINSequenceNumber )
{
pxTCPPacket->xTCPHeader.ucTCPFlags &= ( ( uint8_t ) ~ipTCP_FLAG_FIN );
FreeRTOS_debug_printf( ( "Suppress FIN for %lu + %lu < %lu\n",
pxTCPWindow->ulOurSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber,
ulDataLen,
pxTCPWindow->tx.ulFINSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber ) );
}
}
}
/* Tell which sequence number is expected next time */
pxTCPPacket->xTCPHeader.ulAckNr = FreeRTOS_htonl( pxTCPWindow->rx.ulCurrentSequenceNumber );
}
else
{
/* Sending data without a socket, probably replying with a RST flag
Just swap the two sequence numbers. */
vFlip_32( pxTCPPacket->xTCPHeader.ulSequenceNumber, pxTCPPacket->xTCPHeader.ulAckNr );
}
pxIPHeader->ucTimeToLive = ( uint8_t ) ipconfigTCP_TIME_TO_LIVE;
pxIPHeader->usLength = FreeRTOS_htons( ulLen );
if( ( pxSocket == NULL ) || ( *ipLOCAL_IP_ADDRESS_POINTER == 0ul ) )
{
/* When pxSocket is NULL, this function is called by prvTCPSendReset()
and the IP-addresses must be swapped.
Also swap the IP-addresses in case the IP-tack doesn't have an
IP-address yet, i.e. when ( *ipLOCAL_IP_ADDRESS_POINTER == 0ul ). */
ulSourceAddress = pxIPHeader->ulDestinationIPAddress;
}
else
{
ulSourceAddress = *ipLOCAL_IP_ADDRESS_POINTER;
}
pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
pxIPHeader->ulSourceIPAddress = ulSourceAddress;
vFlip_16( pxTCPPacket->xTCPHeader.usSourcePort, pxTCPPacket->xTCPHeader.usDestinationPort );
/* Just an increasing number. */
pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );
usPacketIdentifier++;
pxIPHeader->usFragmentOffset = 0u;
#if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
{
/* calculate the IP header checksum, in case the driver won't do that. */
pxIPHeader->usHeaderChecksum = 0x00u;
pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
/* calculate the TCP checksum for an outgoing packet. */
usGenerateProtocolChecksum( (uint8_t*)pxTCPPacket, pxNetworkBuffer->xDataLength, pdTRUE );
/* A calculated checksum of 0 must be inverted as 0 means the checksum
is disabled. */
if( pxTCPPacket->xTCPHeader.usChecksum == 0x00u )
{
pxTCPPacket->xTCPHeader.usChecksum = 0xffffU;
}
}
#endif
#if( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
pxNetworkBuffer->pxNextBuffer = NULL;
#endif
/* Important: tell NIC driver how many bytes must be sent. */
pxNetworkBuffer->xDataLength = ulLen + ipSIZE_OF_ETH_HEADER;
/* Fill in the destination MAC addresses. */
memcpy( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) &( pxEthernetHeader->xSourceAddress ),
sizeof( pxEthernetHeader->xDestinationAddress ) );
/* The source MAC addresses is fixed to 'ipLOCAL_MAC_ADDRESS'. */
memcpy( ( void * ) &( pxEthernetHeader->xSourceAddress) , ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
#if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
{
if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
{
BaseType_t xIndex;
for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
{
pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
}
pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
}
}
#endif
/* Send! */
xNetworkInterfaceOutput( pxNetworkBuffer, xReleaseAfterSend );
if( xReleaseAfterSend == pdFALSE )
{
/* Swap-back some fields, as pxBuffer probably points to a socket field
containing the packet header. */
vFlip_16( pxTCPPacket->xTCPHeader.usSourcePort, pxTCPPacket->xTCPHeader.usDestinationPort);
pxTCPPacket->xIPHeader.ulSourceIPAddress = pxTCPPacket->xIPHeader.ulDestinationIPAddress;
memcpy( pxEthernetHeader->xSourceAddress.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
}
else
{
/* Nothing to do: the buffer has been passed to DMA and will be released after use */
}
} /* if( pxNetworkBuffer != NULL ) */
}
/*-----------------------------------------------------------*/
/*
* The SYN event is very important: the sequence numbers, which have a kind of
* random starting value, are being synchronised. The sliding window manager
* (in FreeRTOS_TCP_WIN.c) needs to know them, along with the Maximum Segment
* Size (MSS) in use.
*/
static void prvTCPCreateWindow( FreeRTOS_Socket_t *pxSocket )
{
if( xTCPWindowLoggingLevel )
FreeRTOS_debug_printf( ( "Limits (using): TCP Win size %lu Water %lu <= %lu <= %lu\n",
pxSocket->u.xTCP.uxRxWinSize * ipconfigTCP_MSS,
pxSocket->u.xTCP.uxLittleSpace ,
pxSocket->u.xTCP.uxEnoughSpace,
pxSocket->u.xTCP.uxRxStreamSize ) );
vTCPWindowCreate(
&pxSocket->u.xTCP.xTCPWindow,
ipconfigTCP_MSS * pxSocket->u.xTCP.uxRxWinSize,
ipconfigTCP_MSS * pxSocket->u.xTCP.uxTxWinSize,
pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber,
pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber,
( uint32_t ) pxSocket->u.xTCP.usInitMSS );
}
/*-----------------------------------------------------------*/
/*
* Connecting sockets have a special state: eCONNECT_SYN. In this phase,
* the Ethernet address of the target will be found using ARP. In case the
* target IP address is not within the netmask, the hardware address of the
* gateway will be used.
*/
static BaseType_t prvTCPPrepareConnect( FreeRTOS_Socket_t *pxSocket )
{
TCPPacket_t *pxTCPPacket;
IPHeader_t *pxIPHeader;
eARPLookupResult_t eReturned;
uint32_t ulRemoteIP;
MACAddress_t xEthAddress;
BaseType_t xReturn = pdTRUE;
uint32_t ulInitialSequenceNumber = 0;
#if( ipconfigHAS_PRINTF != 0 )
{
/* Only necessary for nicer logging. */
memset( xEthAddress.ucBytes, '\0', sizeof( xEthAddress.ucBytes ) );
}
#endif /* ipconfigHAS_PRINTF != 0 */
ulRemoteIP = FreeRTOS_htonl( pxSocket->u.xTCP.ulRemoteIP );
/* Determine the ARP cache status for the requested IP address. */
eReturned = eARPGetCacheEntry( &( ulRemoteIP ), &( xEthAddress ) );
switch( eReturned )
{
case eARPCacheHit: /* An ARP table lookup found a valid entry. */
break; /* We can now prepare the SYN packet. */
case eARPCacheMiss: /* An ARP table lookup did not find a valid entry. */
case eCantSendPacket: /* There is no IP address, or an ARP is still in progress. */
default:
/* Count the number of times it couldn't find the ARP address. */
pxSocket->u.xTCP.ucRepCount++;
FreeRTOS_debug_printf( ( "ARP for %lxip (using %lxip): rc=%d %02X:%02X:%02X %02X:%02X:%02X\n",
pxSocket->u.xTCP.ulRemoteIP,
FreeRTOS_htonl( ulRemoteIP ),
eReturned,
xEthAddress.ucBytes[ 0 ],
xEthAddress.ucBytes[ 1 ],
xEthAddress.ucBytes[ 2 ],
xEthAddress.ucBytes[ 3 ],
xEthAddress.ucBytes[ 4 ],
xEthAddress.ucBytes[ 5 ] ) );
/* And issue a (new) ARP request */
FreeRTOS_OutputARPRequest( ulRemoteIP );
xReturn = pdFALSE;
}
if( xReturn != pdFALSE )
{
/* Get a difficult-to-predict initial sequence number for this 4-tuple. */
ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( *ipLOCAL_IP_ADDRESS_POINTER,
pxSocket->usLocalPort,
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort );
/* Check for a random number generation error. */
if( 0 == ulInitialSequenceNumber )
{
xReturn = pdFALSE;
}
}
if( xReturn != pdFALSE )
{
/* The MAC-address of the peer (or gateway) has been found,
now prepare the initial TCP packet and some fields in the socket. */
pxTCPPacket = ( TCPPacket_t * )pxSocket->u.xTCP.xPacket.u.ucLastPacket;
pxIPHeader = &pxTCPPacket->xIPHeader;
/* reset the retry counter to zero. */
pxSocket->u.xTCP.ucRepCount = 0u;
/* And remember that the connect/SYN data are prepared. */
pxSocket->u.xTCP.bits.bConnPrepared = pdTRUE_UNSIGNED;
/* Now that the Ethernet address is known, the initial packet can be
prepared. */
memset( pxSocket->u.xTCP.xPacket.u.ucLastPacket, '\0', sizeof( pxSocket->u.xTCP.xPacket.u.ucLastPacket ) );
/* Write the Ethernet address in Source, because it will be swapped by
prvTCPReturnPacket(). */
memcpy( &pxTCPPacket->xEthernetHeader.xSourceAddress, &xEthAddress, sizeof( xEthAddress ) );
/* 'ipIPv4_FRAME_TYPE' is already in network-byte-order. */
pxTCPPacket->xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
pxIPHeader->ucVersionHeaderLength = 0x45u;
pxIPHeader->usLength = FreeRTOS_htons( sizeof( TCPPacket_t ) - sizeof( pxTCPPacket->xEthernetHeader ) );
pxIPHeader->ucTimeToLive = ( uint8_t ) ipconfigTCP_TIME_TO_LIVE;
pxIPHeader->ucProtocol = ( uint8_t ) ipPROTOCOL_TCP;
/* Addresses and ports will be stored swapped because prvTCPReturnPacket
will swap them back while replying. */
pxIPHeader->ulDestinationIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
pxIPHeader->ulSourceIPAddress = FreeRTOS_htonl( pxSocket->u.xTCP.ulRemoteIP );
pxTCPPacket->xTCPHeader.usSourcePort = FreeRTOS_htons( pxSocket->u.xTCP.usRemotePort );
pxTCPPacket->xTCPHeader.usDestinationPort = FreeRTOS_htons( pxSocket->usLocalPort );
/* We are actively connecting, so the peer's Initial Sequence Number (ISN)
isn't known yet. */
pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber = 0ul;
/* Start with ISN (Initial Sequence Number). */
pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber = ulInitialSequenceNumber;
/* The TCP header size is 20 bytes, divided by 4 equals 5, which is put in
the high nibble of the TCP offset field. */
pxTCPPacket->xTCPHeader.ucTCPOffset = 0x50u;
/* Only set the SYN flag. */
pxTCPPacket->xTCPHeader.ucTCPFlags = ipTCP_FLAG_SYN;
/* Set the values of usInitMSS / usCurMSS for this socket. */
prvSocketSetMSS( pxSocket );
/* For now this is also the advertised window size. */
pxSocket->u.xTCP.ulRxCurWinSize = pxSocket->u.xTCP.usInitMSS;
/* The initial sequence numbers at our side are known. Later
vTCPWindowInit() will be called to fill in the peer's sequence numbers, but
first wait for a SYN+ACK reply. */
prvTCPCreateWindow( pxSocket );
}
return xReturn;
}
/*-----------------------------------------------------------*/
/* For logging and debugging: make a string showing the TCP flags
*/
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
static const char *prvTCPFlagMeaning( UBaseType_t xFlags)
{
static char retString[10];
snprintf(retString, sizeof( retString ), "%c%c%c%c%c%c%c%c%c",
( xFlags & ipTCP_FLAG_FIN ) ? 'F' : '.', /* 0x0001: No more data from sender */
( xFlags & ipTCP_FLAG_SYN ) ? 'S' : '.', /* 0x0002: Synchronize sequence numbers */
( xFlags & ipTCP_FLAG_RST ) ? 'R' : '.', /* 0x0004: Reset the connection */
( xFlags & ipTCP_FLAG_PSH ) ? 'P' : '.', /* 0x0008: Push function: please push buffered data to the recv application */
( xFlags & ipTCP_FLAG_ACK ) ? 'A' : '.', /* 0x0010: Acknowledgment field is significant */
( xFlags & ipTCP_FLAG_URG ) ? 'U' : '.', /* 0x0020: Urgent pointer field is significant */
( xFlags & ipTCP_FLAG_ECN ) ? 'E' : '.', /* 0x0040: ECN-Echo */
( xFlags & ipTCP_FLAG_CWR ) ? 'C' : '.', /* 0x0080: Congestion Window Reduced */
( xFlags & ipTCP_FLAG_NS ) ? 'N' : '.'); /* 0x0100: ECN-nonce concealment protection */
return retString;
}
/*-----------------------------------------------------------*/
#endif /* ipconfigHAS_DEBUG_PRINTF */
/*
* Parse the TCP option(s) received, if present. It has already been verified
* that: ((pxTCPHeader->ucTCPOffset & 0xf0) > 0x50), meaning that the TP header
* is longer than the usual 20 (5 x 4) bytes.
*/
static void prvCheckOptions( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer )
{
TCPPacket_t * pxTCPPacket;
TCPHeader_t * pxTCPHeader;
const unsigned char *pucPtr;
const unsigned char *pucLast;
TCPWindow_t *pxTCPWindow;
UBaseType_t uxNewMSS;
pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
pxTCPHeader = &pxTCPPacket->xTCPHeader;
/* A character pointer to iterate through the option data */
pucPtr = pxTCPHeader->ucOptdata;
pucLast = pucPtr + (((pxTCPHeader->ucTCPOffset >> 4) - 5) << 2);
pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
/* Validate options size calculation. */
if( pucLast > ( pxNetworkBuffer->pucEthernetBuffer + pxNetworkBuffer->xDataLength ) )
{
return;
}
/* The comparison with pucLast is only necessary in case the option data are
corrupted, we don't like to run into invalid memory and crash. */
while( pucPtr < pucLast )
{
UBaseType_t xRemainingOptionsBytes = pucLast - pucPtr;
if( pucPtr[ 0 ] == TCP_OPT_END )
{
/* End of options. */
break;
}
if( pucPtr[ 0 ] == TCP_OPT_NOOP)
{
/* NOP option, inserted to make the length a multiple of 4. */
pucPtr++;
continue;
}
/* Any other well-formed option must be at least two bytes: the option
type byte followed by a length byte. */
if( xRemainingOptionsBytes < 2 )
{
break;
}
#if( ipconfigUSE_TCP_WIN != 0 )
else if( pucPtr[ 0 ] == TCP_OPT_WSOPT )
{
/* Confirm that the option fits in the remaining buffer space. */
if( ( xRemainingOptionsBytes < TCP_OPT_WSOPT_LEN ) || ( pucPtr[ 1 ] != TCP_OPT_WSOPT_LEN ) )
{
break;
}
pxSocket->u.xTCP.ucPeerWinScaleFactor = pucPtr[ 2 ];
pxSocket->u.xTCP.bits.bWinScaling = pdTRUE_UNSIGNED;
pucPtr += TCP_OPT_WSOPT_LEN;
}
#endif /* ipconfigUSE_TCP_WIN */
else if( pucPtr[ 0 ] == TCP_OPT_MSS )
{
/* Confirm that the option fits in the remaining buffer space. */
if( ( xRemainingOptionsBytes < TCP_OPT_MSS_LEN )|| ( pucPtr[ 1 ] != TCP_OPT_MSS_LEN ) )
{
break;
}
/* An MSS option with the correct option length. FreeRTOS_htons()
is not needed here because usChar2u16() already returns a host
endian number. */
uxNewMSS = usChar2u16( pucPtr + 2 );
if( pxSocket->u.xTCP.usInitMSS != uxNewMSS )
{
/* Perform a basic check on the the new MSS. */
if( uxNewMSS == 0 )
{
break;
}
FreeRTOS_debug_printf( ( "MSS change %u -> %lu\n", pxSocket->u.xTCP.usInitMSS, uxNewMSS ) );
}
if( pxSocket->u.xTCP.usInitMSS > uxNewMSS )
{
/* our MSS was bigger than the MSS of the other party: adapt it. */
pxSocket->u.xTCP.bits.bMssChange = pdTRUE_UNSIGNED;
if( ( pxTCPWindow != NULL ) && ( pxSocket->u.xTCP.usCurMSS > uxNewMSS ) )
{
/* The peer advertises a smaller MSS than this socket was
using. Use that as well. */
FreeRTOS_debug_printf( ( "Change mss %d => %lu\n", pxSocket->u.xTCP.usCurMSS, uxNewMSS ) );
pxSocket->u.xTCP.usCurMSS = ( uint16_t ) uxNewMSS;
}
pxTCPWindow->xSize.ulRxWindowLength = ( ( uint32_t ) uxNewMSS ) * ( pxTCPWindow->xSize.ulRxWindowLength / ( ( uint32_t ) uxNewMSS ) );
pxTCPWindow->usMSSInit = ( uint16_t ) uxNewMSS;
pxTCPWindow->usMSS = ( uint16_t ) uxNewMSS;
pxSocket->u.xTCP.usInitMSS = ( uint16_t ) uxNewMSS;
pxSocket->u.xTCP.usCurMSS = ( uint16_t ) uxNewMSS;
}
#if( ipconfigUSE_TCP_WIN != 1 )
/* Without scaled windows, MSS is the only interesting option. */
break;
#else
/* Or else we continue to check another option: selective ACK. */
pucPtr += TCP_OPT_MSS_LEN;
#endif /* ipconfigUSE_TCP_WIN != 1 */
}
else
{
/* All other options have a length field, so that we easily
can skip past them. */
unsigned char len = pucPtr[ 1 ];
if( ( len < 2 ) || ( len > xRemainingOptionsBytes ) )
{
/* If the length field is too small or too big, the options are malformed.
Don't process them further. */
break;
}
#if( ipconfigUSE_TCP_WIN == 1 )
{
/* Selective ACK: the peer has received a packet but it is missing earlier
packets. At least this packet does not need retransmission anymore
ulTCPWindowTxSack( ) takes care of this administration. */
if( pucPtr[0] == TCP_OPT_SACK_A )
{
len -= 2;
pucPtr += 2;
while( len >= 8 )
{
uint32_t ulFirst = ulChar2u32( pucPtr );
uint32_t ulLast = ulChar2u32( pucPtr + 4 );
uint32_t ulCount = ulTCPWindowTxSack( &pxSocket->u.xTCP.xTCPWindow, ulFirst, ulLast );
/* ulTCPWindowTxSack( ) returns the number of bytes which have been acked
starting from the head position.
Advance the tail pointer in txStream. */
if( ( pxSocket->u.xTCP.txStream != NULL ) && ( ulCount > 0 ) )
{
/* Just advancing the tail index, 'ulCount' bytes have been confirmed. */
uxStreamBufferGet( pxSocket->u.xTCP.txStream, 0, NULL, ( size_t ) ulCount, pdFALSE );
pxSocket->xEventBits |= eSOCKET_SEND;
#if ipconfigSUPPORT_SELECT_FUNCTION == 1
{
if( pxSocket->xSelectBits & eSELECT_WRITE )
{
/* The field 'xEventBits' is used to store regular socket events (at most 8),
as well as 'select events', which will be left-shifted */
pxSocket->xEventBits |= ( eSELECT_WRITE << SOCKET_EVENT_BIT_COUNT );
}
}
#endif
/* In case the socket owner has installed an OnSent handler,
call it now. */
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xTCP.pxHandleSent ) )
{
pxSocket->u.xTCP.pxHandleSent( (Socket_t *)pxSocket, ulCount );
}
}
#endif /* ipconfigUSE_CALLBACKS == 1 */
}
pucPtr += 8;
len -= 8;
}
/* len should be 0 by now. */
}
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
pucPtr += len;
}
}
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN != 0 )
static uint8_t prvWinScaleFactor( FreeRTOS_Socket_t *pxSocket )
{
size_t uxWinSize;
uint8_t ucFactor;
/* 'xTCP.uxRxWinSize' is the size of the reception window in units of MSS. */
uxWinSize = pxSocket->u.xTCP.uxRxWinSize * ( size_t ) pxSocket->u.xTCP.usInitMSS;
ucFactor = 0u;
while( uxWinSize > 0xfffful )
{
/* Divide by two and increase the binary factor by 1. */
uxWinSize >>= 1;
ucFactor++;
}
FreeRTOS_debug_printf( ( "prvWinScaleFactor: uxRxWinSize %lu MSS %lu Factor %u\n",
pxSocket->u.xTCP.uxRxWinSize,
pxSocket->u.xTCP.usInitMSS,
ucFactor ) );
return ucFactor;
}
#endif
/*-----------------------------------------------------------*/
/*
* When opening a TCP connection, while SYN's are being sent, the parties may
* communicate what MSS (Maximum Segment Size) they intend to use. MSS is the
* nett size of the payload, always smaller than MTU.
*/
static UBaseType_t prvSetSynAckOptions( FreeRTOS_Socket_t *pxSocket, TCPPacket_t * pxTCPPacket )
{
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
uint16_t usMSS = pxSocket->u.xTCP.usInitMSS;
UBaseType_t uxOptionsLength;
/* We send out the TCP Maximum Segment Size option with our SYN[+ACK]. */
pxTCPHeader->ucOptdata[ 0 ] = ( uint8_t ) TCP_OPT_MSS;
pxTCPHeader->ucOptdata[ 1 ] = ( uint8_t ) TCP_OPT_MSS_LEN;
pxTCPHeader->ucOptdata[ 2 ] = ( uint8_t ) ( usMSS >> 8 );
pxTCPHeader->ucOptdata[ 3 ] = ( uint8_t ) ( usMSS & 0xffu );
#if( ipconfigUSE_TCP_WIN != 0 )
{
pxSocket->u.xTCP.ucMyWinScaleFactor = prvWinScaleFactor( pxSocket );
pxTCPHeader->ucOptdata[ 4 ] = TCP_OPT_NOOP;
pxTCPHeader->ucOptdata[ 5 ] = ( uint8_t ) ( TCP_OPT_WSOPT );
pxTCPHeader->ucOptdata[ 6 ] = ( uint8_t ) ( TCP_OPT_WSOPT_LEN );
pxTCPHeader->ucOptdata[ 7 ] = ( uint8_t ) pxSocket->u.xTCP.ucMyWinScaleFactor;
uxOptionsLength = 8u;
}
#else
{
uxOptionsLength = 4u;
}
#endif
#if( ipconfigUSE_TCP_WIN == 0 )
{
return uxOptionsLength;
}
#else
{
pxTCPHeader->ucOptdata[ uxOptionsLength + 0 ] = TCP_OPT_NOOP;
pxTCPHeader->ucOptdata[ uxOptionsLength + 1 ] = TCP_OPT_NOOP;
pxTCPHeader->ucOptdata[ uxOptionsLength + 2 ] = TCP_OPT_SACK_P; /* 4: Sack-Permitted Option. */
pxTCPHeader->ucOptdata[ uxOptionsLength + 3 ] = 2; /* 2: length of this option. */
uxOptionsLength += 4u;
return uxOptionsLength; /* bytes, not words. */
}
#endif /* ipconfigUSE_TCP_WIN == 0 */
}
/*
* For anti-hanging protection and TCP keep-alive messages. Called in two
* places: after receiving a packet and after a state change. The socket's
* alive timer may be reset.
*/
static void prvTCPTouchSocket( FreeRTOS_Socket_t *pxSocket )
{
#if( ipconfigTCP_HANG_PROTECTION == 1 )
{
pxSocket->u.xTCP.xLastActTime = xTaskGetTickCount( );
}
#endif
#if( ipconfigTCP_KEEP_ALIVE == 1 )
{
pxSocket->u.xTCP.bits.bWaitKeepAlive = pdFALSE_UNSIGNED;
pxSocket->u.xTCP.bits.bSendKeepAlive = pdFALSE_UNSIGNED;
pxSocket->u.xTCP.ucKeepRepCount = 0u;
pxSocket->u.xTCP.xLastAliveTime = xTaskGetTickCount();
}
#endif
( void ) pxSocket;
}
/*-----------------------------------------------------------*/
/*
* Changing to a new state. Centralised here to do specific actions such as
* resetting the alive timer, calling the user's OnConnect handler to notify
* that a socket has got (dis)connected, and setting bit to unblock a call to
* FreeRTOS_select()
*/
void vTCPStateChange( FreeRTOS_Socket_t *pxSocket, enum eTCP_STATE eTCPState )
{
FreeRTOS_Socket_t *xParent = NULL;
BaseType_t bBefore = ( BaseType_t ) NOW_CONNECTED( pxSocket->u.xTCP.ucTCPState ); /* Was it connected ? */
BaseType_t bAfter = ( BaseType_t ) NOW_CONNECTED( eTCPState ); /* Is it connected now ? */
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
BaseType_t xPreviousState = ( BaseType_t ) pxSocket->u.xTCP.ucTCPState;
#endif
#if( ipconfigUSE_CALLBACKS == 1 )
FreeRTOS_Socket_t *xConnected = NULL;
#endif
/* Has the connected status changed? */
if( bBefore != bAfter )
{
/* Is the socket connected now ? */
if( bAfter != pdFALSE )
{
/* if bPassQueued is true, this socket is an orphan until it gets connected. */
if( pxSocket->u.xTCP.bits.bPassQueued != pdFALSE_UNSIGNED )
{
/* Now that it is connected, find it's parent. */
if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED )
{
xParent = pxSocket;
}
else
{
xParent = pxSocket->u.xTCP.pxPeerSocket;
configASSERT( xParent != NULL );
}
if( xParent != NULL )
{
if( xParent->u.xTCP.pxPeerSocket == NULL )
{
xParent->u.xTCP.pxPeerSocket = pxSocket;
}
xParent->xEventBits |= eSOCKET_ACCEPT;
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
/* Library support FreeRTOS_select(). Receiving a new
connection is being translated as a READ event. */
if( ( xParent->xSelectBits & eSELECT_READ ) != 0 )
{
xParent->xEventBits |= ( eSELECT_READ << SOCKET_EVENT_BIT_COUNT );
}
}
#endif
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( ( ipconfigIS_VALID_PROG_ADDRESS( xParent->u.xTCP.pxHandleConnected ) != pdFALSE ) &&
( xParent->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED ) )
{
/* The listening socket does not become connected itself, in stead
a child socket is created.
Postpone a call the OnConnect event until the end of this function. */
xConnected = xParent;
}
}
#endif
}
/* Don't need to access the parent socket anymore, so the
reference 'pxPeerSocket' may be cleared. */
pxSocket->u.xTCP.pxPeerSocket = NULL;
pxSocket->u.xTCP.bits.bPassQueued = pdFALSE_UNSIGNED;
/* When true, this socket may be returned in a call to accept(). */
pxSocket->u.xTCP.bits.bPassAccept = pdTRUE_UNSIGNED;
}
else
{
pxSocket->xEventBits |= eSOCKET_CONNECT;
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
if( pxSocket->xSelectBits & eSELECT_WRITE )
{
pxSocket->xEventBits |= ( eSELECT_WRITE << SOCKET_EVENT_BIT_COUNT );
}
}
#endif
}
}
else /* bAfter == pdFALSE, connection is closed. */
{
/* Notify/wake-up the socket-owner by setting a semaphore. */
pxSocket->xEventBits |= eSOCKET_CLOSED;
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
if( ( pxSocket->xSelectBits & eSELECT_EXCEPT ) != 0 )
{
pxSocket->xEventBits |= ( eSELECT_EXCEPT << SOCKET_EVENT_BIT_COUNT );
}
}
#endif
}
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( ( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xTCP.pxHandleConnected ) != pdFALSE ) && ( xConnected == NULL ) )
{
/* The 'connected' state has changed, call the user handler. */
xConnected = pxSocket;
}
}
#endif /* ipconfigUSE_CALLBACKS */
if( prvTCPSocketIsActive( ( UBaseType_t ) pxSocket->u.xTCP.ucTCPState ) == pdFALSE )
{
/* Now the socket isn't in an active state anymore so it
won't need further attention of the IP-task.
Setting time-out to zero means that the socket won't get checked during
timer events. */
pxSocket->u.xTCP.usTimeout = 0u;
}
}
else
{
if( eTCPState == eCLOSED )
{
/* Socket goes to status eCLOSED because of a RST.
When nobody owns the socket yet, delete it. */
if( ( pxSocket->u.xTCP.bits.bPassQueued != pdFALSE_UNSIGNED ) ||
( pxSocket->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED ) )
{
FreeRTOS_debug_printf( ( "vTCPStateChange: Closing socket\n" ) );
if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
{
FreeRTOS_closesocket( pxSocket );
}
}
}
}
/* Fill in the new state. */
pxSocket->u.xTCP.ucTCPState = ( uint8_t ) eTCPState;
/* touch the alive timers because moving to another state. */
prvTCPTouchSocket( pxSocket );
#if( ipconfigHAS_DEBUG_PRINTF == 1 )
{
if( ( xTCPWindowLoggingLevel >= 0 ) && ( ipconfigTCP_MAY_LOG_PORT( pxSocket->usLocalPort ) != pdFALSE ) )
FreeRTOS_debug_printf( ( "Socket %d -> %lxip:%u State %s->%s\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort,
FreeRTOS_GetTCPStateName( ( UBaseType_t ) xPreviousState ),
FreeRTOS_GetTCPStateName( ( UBaseType_t ) eTCPState ) ) );
}
#endif /* ipconfigHAS_DEBUG_PRINTF */
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( xConnected != NULL )
{
/* The 'connected' state has changed, call the OnConnect handler of the parent. */
xConnected->u.xTCP.pxHandleConnected( ( Socket_t * ) xConnected, bAfter );
}
}
#endif
if( xParent != NULL )
{
vSocketWakeUpUser( xParent );
}
}
/*-----------------------------------------------------------*/
static NetworkBufferDescriptor_t *prvTCPBufferResize( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer,
int32_t lDataLen, UBaseType_t uxOptionsLength )
{
NetworkBufferDescriptor_t *pxReturn;
int32_t lNeeded;
BaseType_t xResize;
if( xBufferAllocFixedSize != pdFALSE )
{
/* Network buffers are created with a fixed size and can hold the largest
MTU. */
lNeeded = ( int32_t ) ipTOTAL_ETHERNET_FRAME_SIZE;
/* and therefore, the buffer won't be too small.
Only ask for a new network buffer in case none was supplied. */
xResize = ( pxNetworkBuffer == NULL );
}
else
{
/* Network buffers are created with a variable size. See if it must
grow. */
lNeeded = FreeRTOS_max_int32( ( int32_t ) sizeof( pxSocket->u.xTCP.xPacket.u.ucLastPacket ),
( int32_t ) ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength ) + lDataLen );
/* In case we were called from a TCP timer event, a buffer must be
created. Otherwise, test 'xDataLength' of the provided buffer. */
xResize = ( pxNetworkBuffer == NULL ) || ( pxNetworkBuffer->xDataLength < (size_t)lNeeded );
}
if( xResize != pdFALSE )
{
/* The caller didn't provide a network buffer or the provided buffer is
too small. As we must send-out a data packet, a buffer will be created
here. */
pxReturn = pxGetNetworkBufferWithDescriptor( ( uint32_t ) lNeeded, 0u );
if( pxReturn != NULL )
{
/* Set the actual packet size, in case the returned buffer is larger. */
pxReturn->xDataLength = lNeeded;
/* Copy the existing data to the new created buffer. */
if( pxNetworkBuffer )
{
/* Either from the previous buffer... */
memcpy( pxReturn->pucEthernetBuffer, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
/* ...and release it. */
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
}
else
{
/* Or from the socket field 'xTCP.xPacket'. */
memcpy( pxReturn->pucEthernetBuffer, pxSocket->u.xTCP.xPacket.u.ucLastPacket, sizeof( pxSocket->u.xTCP.xPacket.u.ucLastPacket ) );
}
}
}
else
{
/* xResize is false, the network buffer provided was big enough. */
pxReturn = pxNetworkBuffer;
/* Thanks to Andrey Ivanov from swissEmbedded for reporting that the
xDataLength member must get the correct length too! */
pxNetworkBuffer->xDataLength = ( size_t ) ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength ) + ( size_t ) lDataLen;
}
return pxReturn;
}
/*-----------------------------------------------------------*/
/*
* Prepare an outgoing message, in case anything has to be sent.
*/
static int32_t prvTCPPrepareSend( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer, UBaseType_t uxOptionsLength )
{
int32_t lDataLen;
uint8_t *pucEthernetBuffer, *pucSendData;
TCPPacket_t *pxTCPPacket;
size_t uxOffset;
uint32_t ulDataGot, ulDistance;
TCPWindow_t *pxTCPWindow;
NetworkBufferDescriptor_t *pxNewBuffer;
int32_t lStreamPos;
if( ( *ppxNetworkBuffer ) != NULL )
{
/* A network buffer descriptor was already supplied */
pucEthernetBuffer = ( *ppxNetworkBuffer )->pucEthernetBuffer;
}
else
{
/* For now let it point to the last packet header */
pucEthernetBuffer = pxSocket->u.xTCP.xPacket.u.ucLastPacket;
}
pxTCPPacket = ( TCPPacket_t * ) ( pucEthernetBuffer );
pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
lDataLen = 0;
lStreamPos = 0;
pxTCPPacket->xTCPHeader.ucTCPFlags |= ipTCP_FLAG_ACK;
if( pxSocket->u.xTCP.txStream != NULL )
{
/* ulTCPWindowTxGet will return the amount of data which may be sent
along with the position in the txStream.
Why check for MSS > 1 ?
Because some TCP-stacks (like uIP) use it for flow-control. */
if( pxSocket->u.xTCP.usCurMSS > 1u )
{
lDataLen = ( int32_t ) ulTCPWindowTxGet( pxTCPWindow, pxSocket->u.xTCP.ulWindowSize, &lStreamPos );
}
if( lDataLen > 0 )
{
/* Check if the current network buffer is big enough, if not,
resize it. */
pxNewBuffer = prvTCPBufferResize( pxSocket, *ppxNetworkBuffer, lDataLen, uxOptionsLength );
if( pxNewBuffer != NULL )
{
*ppxNetworkBuffer = pxNewBuffer;
pucEthernetBuffer = pxNewBuffer->pucEthernetBuffer;
pxTCPPacket = ( TCPPacket_t * ) ( pucEthernetBuffer );
pucSendData = pucEthernetBuffer + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength;
/* Translate the position in txStream to an offset from the tail
marker. */
uxOffset = uxStreamBufferDistance( pxSocket->u.xTCP.txStream, pxSocket->u.xTCP.txStream->uxTail, ( size_t ) lStreamPos );
/* Here data is copied from the txStream in 'peek' mode. Only
when the packets are acked, the tail marker will be updated. */
ulDataGot = ( uint32_t ) uxStreamBufferGet( pxSocket->u.xTCP.txStream, uxOffset, pucSendData, ( size_t ) lDataLen, pdTRUE );
#if( ipconfigHAS_DEBUG_PRINTF != 0 )
{
if( ulDataGot != ( uint32_t ) lDataLen )
{
FreeRTOS_debug_printf( ( "uxStreamBufferGet: pos %lu offs %lu only %lu != %lu\n",
lStreamPos, uxOffset, ulDataGot, lDataLen ) );
}
}
#endif
/* If the owner of the socket requests a closure, add the FIN
flag to the last packet. */
if( ( pxSocket->u.xTCP.bits.bCloseRequested != pdFALSE_UNSIGNED ) && ( pxSocket->u.xTCP.bits.bFinSent == pdFALSE_UNSIGNED ) )
{
ulDistance = ( uint32_t ) uxStreamBufferDistance( pxSocket->u.xTCP.txStream, ( size_t ) lStreamPos, pxSocket->u.xTCP.txStream->uxHead );
if( ulDistance == ulDataGot )
{
#if (ipconfigHAS_DEBUG_PRINTF == 1)
{
/* the order of volatile accesses is undefined
so such workaround */
size_t uxHead = pxSocket->u.xTCP.txStream->uxHead;
size_t uxMid = pxSocket->u.xTCP.txStream->uxMid;
size_t uxTail = pxSocket->u.xTCP.txStream->uxTail;
FreeRTOS_debug_printf( ( "CheckClose %lu <= %lu (%lu <= %lu <= %lu)\n", ulDataGot, ulDistance,
uxTail, uxMid, uxHead ) );
}
#endif
/* Although the socket sends a FIN, it will stay in
ESTABLISHED until all current data has been received or
delivered. */
pxTCPPacket->xTCPHeader.ucTCPFlags |= ipTCP_FLAG_FIN;
pxTCPWindow->tx.ulFINSequenceNumber = pxTCPWindow->ulOurSequenceNumber + ( uint32_t ) lDataLen;
pxSocket->u.xTCP.bits.bFinSent = pdTRUE_UNSIGNED;
}
}
}
else
{
lDataLen = -1;
}
}
}
if( ( lDataLen >= 0 ) && ( pxSocket->u.xTCP.ucTCPState == eESTABLISHED ) )
{
/* See if the socket owner wants to shutdown this connection. */
if( ( pxSocket->u.xTCP.bits.bUserShutdown != pdFALSE_UNSIGNED ) &&
( xTCPWindowTxDone( pxTCPWindow ) != pdFALSE ) )
{
pxSocket->u.xTCP.bits.bUserShutdown = pdFALSE_UNSIGNED;
pxTCPPacket->xTCPHeader.ucTCPFlags |= ipTCP_FLAG_FIN;
pxSocket->u.xTCP.bits.bFinSent = pdTRUE_UNSIGNED;
pxSocket->u.xTCP.bits.bWinChange = pdTRUE_UNSIGNED;
pxTCPWindow->tx.ulFINSequenceNumber = pxTCPWindow->tx.ulCurrentSequenceNumber;
vTCPStateChange( pxSocket, eFIN_WAIT_1 );
}
#if( ipconfigTCP_KEEP_ALIVE != 0 )
{
if( pxSocket->u.xTCP.ucKeepRepCount > 3u )
{
FreeRTOS_debug_printf( ( "keep-alive: giving up %lxip:%u\n",
pxSocket->u.xTCP.ulRemoteIP, /* IP address of remote machine. */
pxSocket->u.xTCP.usRemotePort ) ); /* Port on remote machine. */
vTCPStateChange( pxSocket, eCLOSE_WAIT );
lDataLen = -1;
}
if( ( lDataLen == 0 ) && ( pxSocket->u.xTCP.bits.bWinChange == pdFALSE_UNSIGNED ) )
{
/* If there is no data to be sent, and no window-update message,
we might want to send a keep-alive message. */
TickType_t xAge = xTaskGetTickCount( ) - pxSocket->u.xTCP.xLastAliveTime;
TickType_t xMax;
xMax = ( ( TickType_t ) ipconfigTCP_KEEP_ALIVE_INTERVAL * configTICK_RATE_HZ );
if( pxSocket->u.xTCP.ucKeepRepCount )
{
xMax = ( 3u * configTICK_RATE_HZ );
}
if( xAge > xMax )
{
pxSocket->u.xTCP.xLastAliveTime = xTaskGetTickCount( );
if( xTCPWindowLoggingLevel )
FreeRTOS_debug_printf( ( "keep-alive: %lxip:%u count %u\n",
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort,
pxSocket->u.xTCP.ucKeepRepCount ) );
pxSocket->u.xTCP.bits.bSendKeepAlive = pdTRUE_UNSIGNED;
pxSocket->u.xTCP.usTimeout = ( ( uint16_t ) pdMS_TO_TICKS( 2500 ) );
pxSocket->u.xTCP.ucKeepRepCount++;
}
}
}
#endif /* ipconfigTCP_KEEP_ALIVE */
}
/* Anything to send, a change of the advertised window size, or maybe send a
keep-alive message? */
if( ( lDataLen > 0 ) ||
( pxSocket->u.xTCP.bits.bWinChange != pdFALSE_UNSIGNED ) ||
( pxSocket->u.xTCP.bits.bSendKeepAlive != pdFALSE_UNSIGNED ) )
{
pxTCPPacket->xTCPHeader.ucTCPFlags &= ( ( uint8_t ) ~ipTCP_FLAG_PSH );
pxTCPPacket->xTCPHeader.ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
pxTCPPacket->xTCPHeader.ucTCPFlags |= ( uint8_t ) ipTCP_FLAG_ACK;
if( lDataLen != 0l )
{
pxTCPPacket->xTCPHeader.ucTCPFlags |= ( uint8_t ) ipTCP_FLAG_PSH;
}
lDataLen += ( int32_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength );
}
return lDataLen;
}
/*-----------------------------------------------------------*/
/*
* Calculate after how much time this socket needs to be checked again.
*/
static TickType_t prvTCPNextTimeout ( FreeRTOS_Socket_t *pxSocket )
{
TickType_t ulDelayMs = ( TickType_t ) tcpMAXIMUM_TCP_WAKEUP_TIME_MS;
if( pxSocket->u.xTCP.ucTCPState == eCONNECT_SYN )
{
/* The socket is actively connecting to a peer. */
if( pxSocket->u.xTCP.bits.bConnPrepared )
{
/* Ethernet address has been found, use progressive timeout for
active connect(). */
if( pxSocket->u.xTCP.ucRepCount < 3u )
{
ulDelayMs = ( 3000UL << ( pxSocket->u.xTCP.ucRepCount - 1u ) );
}
else
{
ulDelayMs = 11000UL;
}
}
else
{
/* Still in the ARP phase: check every half second. */
ulDelayMs = 500UL;
}
FreeRTOS_debug_printf( ( "Connect[%lxip:%u]: next timeout %u: %lu ms\n",
pxSocket->u.xTCP.ulRemoteIP, pxSocket->u.xTCP.usRemotePort,
pxSocket->u.xTCP.ucRepCount, ulDelayMs ) );
pxSocket->u.xTCP.usTimeout = ( uint16_t )pdMS_TO_MIN_TICKS( ulDelayMs );
}
else if( pxSocket->u.xTCP.usTimeout == 0u )
{
/* Let the sliding window mechanism decide what time-out is appropriate. */
BaseType_t xResult = xTCPWindowTxHasData( &pxSocket->u.xTCP.xTCPWindow, pxSocket->u.xTCP.ulWindowSize, &ulDelayMs );
if( ulDelayMs == 0u )
{
if( xResult != ( BaseType_t )0 )
{
ulDelayMs = 1UL;
}
else
{
ulDelayMs = tcpMAXIMUM_TCP_WAKEUP_TIME_MS;
}
}
else
{
/* ulDelayMs contains the time to wait before a re-transmission. */
}
pxSocket->u.xTCP.usTimeout = ( uint16_t )pdMS_TO_MIN_TICKS( ulDelayMs );
}
else
{
/* field '.usTimeout' has already been set (by the
keep-alive/delayed-ACK mechanism). */
}
/* Return the number of clock ticks before the timer expires. */
return ( TickType_t ) pxSocket->u.xTCP.usTimeout;
}
/*-----------------------------------------------------------*/
static void prvTCPAddTxData( FreeRTOS_Socket_t *pxSocket )
{
int32_t lCount, lLength;
/* A txStream has been created already, see if the socket has new data for
the sliding window.
uxStreamBufferMidSpace() returns the distance between rxHead and rxMid. It contains new
Tx data which has not been passed to the sliding window yet. The oldest
data not-yet-confirmed can be found at rxTail. */
lLength = ( int32_t ) uxStreamBufferMidSpace( pxSocket->u.xTCP.txStream );
if( lLength > 0 )
{
/* All data between txMid and rxHead will now be passed to the sliding
window manager, so it can start transmitting them.
Hand over the new data to the sliding window handler. It will be
split-up in chunks of 1460 bytes each (or less, depending on
ipconfigTCP_MSS). */
lCount = lTCPWindowTxAdd( &pxSocket->u.xTCP.xTCPWindow,
( uint32_t ) lLength,
( int32_t ) pxSocket->u.xTCP.txStream->uxMid,
( int32_t ) pxSocket->u.xTCP.txStream->LENGTH );
/* Move the rxMid pointer forward up to rxHead. */
if( lCount > 0 )
{
vStreamBufferMoveMid( pxSocket->u.xTCP.txStream, ( size_t ) lCount );
}
}
}
/*-----------------------------------------------------------*/
/*
* prvTCPHandleFin() will be called to handle socket closure
* The Closure starts when either a FIN has been received and accepted,
* Or when the socket has sent a FIN flag to the peer
* Before being called, it has been checked that both reception and transmission
* are complete.
*/
static BaseType_t prvTCPHandleFin( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
uint8_t ucTCPFlags = pxTCPHeader->ucTCPFlags;
TCPWindow_t *pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
BaseType_t xSendLength = 0;
uint32_t ulAckNr = FreeRTOS_ntohl( pxTCPHeader->ulAckNr );
if( ( ucTCPFlags & ipTCP_FLAG_FIN ) != 0u )
{
pxTCPWindow->rx.ulCurrentSequenceNumber = pxTCPWindow->rx.ulFINSequenceNumber + 1u;
}
if( pxSocket->u.xTCP.bits.bFinSent == pdFALSE_UNSIGNED )
{
/* We haven't yet replied with a FIN, do so now. */
pxTCPWindow->tx.ulFINSequenceNumber = pxTCPWindow->tx.ulCurrentSequenceNumber;
pxSocket->u.xTCP.bits.bFinSent = pdTRUE_UNSIGNED;
}
else
{
/* We did send a FIN already, see if it's ACK'd. */
if( ulAckNr == pxTCPWindow->tx.ulFINSequenceNumber + 1u )
{
pxSocket->u.xTCP.bits.bFinAcked = pdTRUE_UNSIGNED;
}
}
if( pxSocket->u.xTCP.bits.bFinAcked == pdFALSE_UNSIGNED )
{
pxTCPWindow->tx.ulCurrentSequenceNumber = pxTCPWindow->tx.ulFINSequenceNumber;
pxTCPHeader->ucTCPFlags = ipTCP_FLAG_ACK | ipTCP_FLAG_FIN;
/* And wait for the final ACK. */
vTCPStateChange( pxSocket, eLAST_ACK );
}
else
{
/* Our FIN has been ACK'd, the outgoing sequence number is now fixed. */
pxTCPWindow->tx.ulCurrentSequenceNumber = pxTCPWindow->tx.ulFINSequenceNumber + 1u;
if( pxSocket->u.xTCP.bits.bFinRecv == pdFALSE_UNSIGNED )
{
/* We have sent out a FIN but the peer hasn't replied with a FIN
yet. Do nothing for the moment. */
pxTCPHeader->ucTCPFlags = 0u;
}
else
{
if( pxSocket->u.xTCP.bits.bFinLast == pdFALSE_UNSIGNED )
{
/* This is the third of the three-way hand shake: the last
ACK. */
pxTCPHeader->ucTCPFlags = ipTCP_FLAG_ACK;
}
else
{
/* The other party started the closure, so we just wait for the
last ACK. */
pxTCPHeader->ucTCPFlags = 0u;
}
/* And wait for the user to close this socket. */
vTCPStateChange( pxSocket, eCLOSE_WAIT );
}
}
pxTCPWindow->ulOurSequenceNumber = pxTCPWindow->tx.ulCurrentSequenceNumber;
if( pxTCPHeader->ucTCPFlags != 0u )
{
xSendLength = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + pxTCPWindow->ucOptionLength );
}
pxTCPHeader->ucTCPOffset = ( uint8_t ) ( ( ipSIZE_OF_TCP_HEADER + pxTCPWindow->ucOptionLength ) << 2 );
if( xTCPWindowLoggingLevel != 0 )
{
FreeRTOS_debug_printf( ( "TCP: send FIN+ACK (ack %lu, cur/nxt %lu/%lu) ourSeqNr %lu | Rx %lu\n",
ulAckNr - pxTCPWindow->tx.ulFirstSequenceNumber,
pxTCPWindow->tx.ulCurrentSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber,
pxTCPWindow->ulNextTxSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber,
pxTCPWindow->ulOurSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber,
pxTCPWindow->rx.ulCurrentSequenceNumber - pxTCPWindow->rx.ulFirstSequenceNumber ) );
}
return xSendLength;
}
/*-----------------------------------------------------------*/
/*
* prvCheckRxData(): called from prvTCPHandleState()
*
* The first thing that will be done is find the TCP payload data
* and check the length of this data.
*/
static BaseType_t prvCheckRxData( NetworkBufferDescriptor_t *pxNetworkBuffer, uint8_t **ppucRecvData )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &( pxTCPPacket->xTCPHeader );
int32_t lLength, lTCPHeaderLength, lReceiveLength, lUrgentLength;
/* Determine the length and the offset of the user-data sent to this
node.
The size of the TCP header is given in a multiple of 4-byte words (single
byte, needs no ntoh() translation). A shift-right 2: is the same as
(offset >> 4) * 4. */
lTCPHeaderLength = ( BaseType_t ) ( ( pxTCPHeader->ucTCPOffset & VALID_BITS_IN_TCP_OFFSET_BYTE ) >> 2 );
/* Let pucRecvData point to the first byte received. */
*ppucRecvData = pxNetworkBuffer->pucEthernetBuffer + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + lTCPHeaderLength;
/* Calculate lReceiveLength - the length of the TCP data received. This is
equal to the total packet length minus:
( LinkLayer length (14) + IP header length (20) + size of TCP header(20 +) ).*/
lReceiveLength = ( ( int32_t ) pxNetworkBuffer->xDataLength ) - ( int32_t ) ipSIZE_OF_ETH_HEADER;
lLength = ( int32_t )FreeRTOS_htons( pxTCPPacket->xIPHeader.usLength );
if( lReceiveLength > lLength )
{
/* More bytes were received than the reported length, often because of
padding bytes at the end. */
lReceiveLength = lLength;
}
/* Subtract the size of the TCP and IP headers and the actual data size is
known. */
if( lReceiveLength > ( lTCPHeaderLength + ( int32_t ) ipSIZE_OF_IPv4_HEADER ) )
{
lReceiveLength -= ( lTCPHeaderLength + ( int32_t ) ipSIZE_OF_IPv4_HEADER );
}
else
{
lReceiveLength = 0;
}
/* Urgent Pointer:
This field communicates the current value of the urgent pointer as a
positive offset from the sequence number in this segment. The urgent
pointer points to the sequence number of the octet following the urgent
data. This field is only be interpreted in segments with the URG control
bit set. */
if( ( pxTCPHeader->ucTCPFlags & ipTCP_FLAG_URG ) != 0u )
{
/* Although we ignore the urgent data, we have to skip it. */
lUrgentLength = ( int32_t ) FreeRTOS_htons( pxTCPHeader->usUrgent );
*ppucRecvData += lUrgentLength;
lReceiveLength -= FreeRTOS_min_int32( lReceiveLength, lUrgentLength );
}
return ( BaseType_t ) lReceiveLength;
}
/*-----------------------------------------------------------*/
/*
* prvStoreRxData(): called from prvTCPHandleState()
*
* The second thing is to do is check if the payload data may be accepted
* If so, they will be added to the reception queue.
*/
static BaseType_t prvStoreRxData( FreeRTOS_Socket_t *pxSocket, uint8_t *pucRecvData,
NetworkBufferDescriptor_t *pxNetworkBuffer, uint32_t ulReceiveLength )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
TCPWindow_t *pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
uint32_t ulSequenceNumber, ulSpace;
int32_t lOffset, lStored;
BaseType_t xResult = 0;
ulSequenceNumber = FreeRTOS_ntohl( pxTCPHeader->ulSequenceNumber );
if( ( ulReceiveLength > 0u ) && ( pxSocket->u.xTCP.ucTCPState >= eSYN_RECEIVED ) )
{
/* See if way may accept the data contents and forward it to the socket
owner.
If it can't be "accept"ed it may have to be stored and send a selective
ack (SACK) option to confirm it. In that case, xTCPWindowRxStore() will be
called later to store an out-of-order packet (in case lOffset is
negative). */
if ( pxSocket->u.xTCP.rxStream )
{
ulSpace = ( uint32_t )uxStreamBufferGetSpace ( pxSocket->u.xTCP.rxStream );
}
else
{
ulSpace = ( uint32_t )pxSocket->u.xTCP.uxRxStreamSize;
}
lOffset = lTCPWindowRxCheck( pxTCPWindow, ulSequenceNumber, ulReceiveLength, ulSpace );
if( lOffset >= 0 )
{
/* New data has arrived and may be made available to the user. See
if the head marker in rxStream may be advanced, only if lOffset == 0.
In case the low-water mark is reached, bLowWater will be set
"low-water" here stands for "little space". */
lStored = lTCPAddRxdata( pxSocket, ( uint32_t ) lOffset, pucRecvData, ulReceiveLength );
if( lStored != ( int32_t ) ulReceiveLength )
{
FreeRTOS_debug_printf( ( "lTCPAddRxdata: stored %ld / %lu bytes??\n", lStored, ulReceiveLength ) );
/* Received data could not be stored. The socket's flag
bMallocError has been set. The socket now has the status
eCLOSE_WAIT and a RST packet will be sent back. */
prvTCPSendReset( pxNetworkBuffer );
xResult = -1;
}
}
/* After a missing packet has come in, higher packets may be passed to
the user. */
#if( ipconfigUSE_TCP_WIN == 1 )
{
/* Now lTCPAddRxdata() will move the rxHead pointer forward
so data becomes available to the user immediately
In case the low-water mark is reached, bLowWater will be set. */
if( ( xResult == 0 ) && ( pxTCPWindow->ulUserDataLength > 0 ) )
{
lTCPAddRxdata( pxSocket, 0ul, NULL, pxTCPWindow->ulUserDataLength );
pxTCPWindow->ulUserDataLength = 0;
}
}
#endif /* ipconfigUSE_TCP_WIN */
}
else
{
pxTCPWindow->ucOptionLength = 0u;
}
return xResult;
}
/*-----------------------------------------------------------*/
/* Set the TCP options (if any) for the outgoing packet. */
static UBaseType_t prvSetOptions( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
TCPWindow_t *pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
UBaseType_t uxOptionsLength = pxTCPWindow->ucOptionLength;
#if( ipconfigUSE_TCP_WIN == 1 )
if( uxOptionsLength != 0u )
{
/* TCP options must be sent because a packet which is out-of-order
was received. */
if( xTCPWindowLoggingLevel >= 0 )
FreeRTOS_debug_printf( ( "SACK[%d,%d]: optlen %lu sending %lu - %lu\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usRemotePort,
uxOptionsLength,
FreeRTOS_ntohl( pxTCPWindow->ulOptionsData[ 1 ] ) - pxSocket->u.xTCP.xTCPWindow.rx.ulFirstSequenceNumber,
FreeRTOS_ntohl( pxTCPWindow->ulOptionsData[ 2 ] ) - pxSocket->u.xTCP.xTCPWindow.rx.ulFirstSequenceNumber ) );
memcpy( pxTCPHeader->ucOptdata, pxTCPWindow->ulOptionsData, ( size_t ) uxOptionsLength );
/* The header length divided by 4, goes into the higher nibble,
effectively a shift-left 2. */
pxTCPHeader->ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
}
else
#endif /* ipconfigUSE_TCP_WIN */
if( ( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED ) && ( pxSocket->u.xTCP.bits.bMssChange != pdFALSE_UNSIGNED ) )
{
/* TCP options must be sent because the MSS has changed. */
pxSocket->u.xTCP.bits.bMssChange = pdFALSE_UNSIGNED;
if( xTCPWindowLoggingLevel >= 0 )
{
FreeRTOS_debug_printf( ( "MSS: sending %d\n", pxSocket->u.xTCP.usCurMSS ) );
}
pxTCPHeader->ucOptdata[ 0 ] = TCP_OPT_MSS;
pxTCPHeader->ucOptdata[ 1 ] = TCP_OPT_MSS_LEN;
pxTCPHeader->ucOptdata[ 2 ] = ( uint8_t ) ( ( pxSocket->u.xTCP.usCurMSS ) >> 8 );
pxTCPHeader->ucOptdata[ 3 ] = ( uint8_t ) ( ( pxSocket->u.xTCP.usCurMSS ) & 0xffu );
uxOptionsLength = 4u;
pxTCPHeader->ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
}
return uxOptionsLength;
}
/*-----------------------------------------------------------*/
/*
* prvHandleSynReceived(): called from prvTCPHandleState()
*
* Called from the states: eSYN_RECEIVED and eCONNECT_SYN
* If the flags received are correct, the socket will move to eESTABLISHED.
*/
static BaseType_t prvHandleSynReceived( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer,
uint32_t ulReceiveLength, UBaseType_t uxOptionsLength )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( (*ppxNetworkBuffer)->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
TCPWindow_t *pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
uint8_t ucTCPFlags = pxTCPHeader->ucTCPFlags;
uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxTCPHeader->ulSequenceNumber );
BaseType_t xSendLength = 0;
/* Either expect a ACK or a SYN+ACK. */
uint16_t usExpect = ( uint16_t ) ipTCP_FLAG_ACK;
if( pxSocket->u.xTCP.ucTCPState == eCONNECT_SYN )
{
usExpect |= ( uint16_t ) ipTCP_FLAG_SYN;
}
if( ( ucTCPFlags & 0x17u ) != usExpect )
{
/* eSYN_RECEIVED: flags 0010 expected, not 0002. */
/* eSYN_RECEIVED: flags ACK expected, not SYN. */
FreeRTOS_debug_printf( ( "%s: flags %04X expected, not %04X\n",
pxSocket->u.xTCP.ucTCPState == eSYN_RECEIVED ? "eSYN_RECEIVED" : "eCONNECT_SYN",
usExpect, ucTCPFlags ) );
vTCPStateChange( pxSocket, eCLOSE_WAIT );
pxTCPHeader->ucTCPFlags |= ipTCP_FLAG_RST;
xSendLength = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength );
pxTCPHeader->ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
}
else
{
pxTCPWindow->usPeerPortNumber = pxSocket->u.xTCP.usRemotePort;
pxTCPWindow->usOurPortNumber = pxSocket->usLocalPort;
if( pxSocket->u.xTCP.ucTCPState == eCONNECT_SYN )
{
TCPPacket_t *pxLastTCPPacket = ( TCPPacket_t * ) ( pxSocket->u.xTCP.xPacket.u.ucLastPacket );
/* Clear the SYN flag in lastPacket. */
pxLastTCPPacket->xTCPHeader.ucTCPFlags = ipTCP_FLAG_ACK;
/* This socket was the one connecting actively so now perofmr the
synchronisation. */
vTCPWindowInit( &pxSocket->u.xTCP.xTCPWindow,
ulSequenceNumber, pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber, ( uint32_t ) pxSocket->u.xTCP.usCurMSS );
pxTCPWindow->rx.ulCurrentSequenceNumber = pxTCPWindow->rx.ulHighestSequenceNumber = ulSequenceNumber + 1u;
pxTCPWindow->tx.ulCurrentSequenceNumber++; /* because we send a TCP_SYN [ | TCP_ACK ]; */
pxTCPWindow->ulNextTxSequenceNumber++;
}
else if( ulReceiveLength == 0u )
{
pxTCPWindow->rx.ulCurrentSequenceNumber = ulSequenceNumber;
}
/* The SYN+ACK has been confirmed, increase the next sequence number by
1. */
pxTCPWindow->ulOurSequenceNumber = pxTCPWindow->tx.ulFirstSequenceNumber + 1u;
#if( ipconfigUSE_TCP_WIN == 1 )
{
FreeRTOS_debug_printf( ( "TCP: %s %d => %lxip:%d set ESTAB (scaling %u)\n",
pxSocket->u.xTCP.ucTCPState == eCONNECT_SYN ? "active" : "passive",
pxSocket->usLocalPort,
pxSocket->u.xTCP.ulRemoteIP,
pxSocket->u.xTCP.usRemotePort,
( unsigned ) pxSocket->u.xTCP.bits.bWinScaling ) );
}
#endif /* ipconfigUSE_TCP_WIN */
if( ( pxSocket->u.xTCP.ucTCPState == eCONNECT_SYN ) || ( ulReceiveLength != 0u ) )
{
pxTCPHeader->ucTCPFlags = ipTCP_FLAG_ACK;
xSendLength = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength );
pxTCPHeader->ucTCPOffset = ( uint8_t ) ( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
}
#if( ipconfigUSE_TCP_WIN != 0 )
{
if( pxSocket->u.xTCP.bits.bWinScaling == pdFALSE_UNSIGNED )
{
/* The other party did not send a scaling factor.
A shifting factor in this side must be canceled. */
pxSocket->u.xTCP.ucMyWinScaleFactor = 0;
pxSocket->u.xTCP.ucPeerWinScaleFactor = 0;
}
}
#endif /* ipconfigUSE_TCP_WIN */
/* This was the third step of connecting: SYN, SYN+ACK, ACK so now the
connection is established. */
vTCPStateChange( pxSocket, eESTABLISHED );
}
return xSendLength;
}
/*-----------------------------------------------------------*/
/*
* prvHandleEstablished(): called from prvTCPHandleState()
*
* Called if the status is eESTABLISHED. Data reception has been handled
* earlier. Here the ACK's from peer will be checked, and if a FIN is received,
* the code will check if it may be accepted, i.e. if all expected data has been
* completely received.
*/
static BaseType_t prvHandleEstablished( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer,
uint32_t ulReceiveLength, UBaseType_t uxOptionsLength )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( (*ppxNetworkBuffer)->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
TCPWindow_t *pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
uint8_t ucTCPFlags = pxTCPHeader->ucTCPFlags;
uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxTCPHeader->ulSequenceNumber ), ulCount;
BaseType_t xSendLength = 0, xMayClose = pdFALSE, bRxComplete, bTxDone;
int32_t lDistance, lSendResult;
/* Remember the window size the peer is advertising. */
pxSocket->u.xTCP.ulWindowSize = FreeRTOS_ntohs( pxTCPHeader->usWindow );
#if( ipconfigUSE_TCP_WIN != 0 )
{
pxSocket->u.xTCP.ulWindowSize =
( pxSocket->u.xTCP.ulWindowSize << pxSocket->u.xTCP.ucPeerWinScaleFactor );
}
#endif
if( ( ucTCPFlags & ( uint8_t ) ipTCP_FLAG_ACK ) != 0u )
{
ulCount = ulTCPWindowTxAck( pxTCPWindow, FreeRTOS_ntohl( pxTCPPacket->xTCPHeader.ulAckNr ) );
/* ulTCPWindowTxAck() returns the number of bytes which have been acked,
starting at 'tx.ulCurrentSequenceNumber'. Advance the tail pointer in
txStream. */
if( ( pxSocket->u.xTCP.txStream != NULL ) && ( ulCount > 0u ) )
{
/* Just advancing the tail index, 'ulCount' bytes have been
confirmed, and because there is new space in the txStream, the
user/owner should be woken up. */
/* _HT_ : only in case the socket's waiting? */
if( uxStreamBufferGet( pxSocket->u.xTCP.txStream, 0u, NULL, ( size_t ) ulCount, pdFALSE ) != 0u )
{
pxSocket->xEventBits |= eSOCKET_SEND;
#if ipconfigSUPPORT_SELECT_FUNCTION == 1
{
if( ( pxSocket->xSelectBits & eSELECT_WRITE ) != 0 )
{
pxSocket->xEventBits |= ( eSELECT_WRITE << SOCKET_EVENT_BIT_COUNT );
}
}
#endif
/* In case the socket owner has installed an OnSent handler,
call it now. */
#if( ipconfigUSE_CALLBACKS == 1 )
{
if( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xTCP.pxHandleSent ) )
{
pxSocket->u.xTCP.pxHandleSent( (Socket_t *)pxSocket, ulCount );
}
}
#endif /* ipconfigUSE_CALLBACKS == 1 */
}
}
}
/* If this socket has a stream for transmission, add the data to the
outgoing segment(s). */
if( pxSocket->u.xTCP.txStream != NULL )
{
prvTCPAddTxData( pxSocket );
}
pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber = pxTCPWindow->tx.ulCurrentSequenceNumber;
if( ( pxSocket->u.xTCP.bits.bFinAccepted != pdFALSE_UNSIGNED ) || ( ( ucTCPFlags & ( uint8_t ) ipTCP_FLAG_FIN ) != 0u ) )
{
/* Peer is requesting to stop, see if we're really finished. */
xMayClose = pdTRUE;
/* Checks are only necessary if we haven't sent a FIN yet. */
if( pxSocket->u.xTCP.bits.bFinSent == pdFALSE_UNSIGNED )
{
/* xTCPWindowTxDone returns true when all Tx queues are empty. */
bRxComplete = xTCPWindowRxEmpty( pxTCPWindow );
bTxDone = xTCPWindowTxDone( pxTCPWindow );
if( ( bRxComplete == 0 ) || ( bTxDone == 0 ) )
{
/* Refusing FIN: Rx incomp 1 optlen 4 tx done 1. */
FreeRTOS_debug_printf( ( "Refusing FIN[%u,%u]: RxCompl %lu tx done %ld\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usRemotePort,
bRxComplete, bTxDone ) );
xMayClose = pdFALSE;
}
else
{
lDistance = ( int32_t ) ( ulSequenceNumber + ulReceiveLength - pxTCPWindow->rx.ulCurrentSequenceNumber );
if( lDistance > 1 )
{
FreeRTOS_debug_printf( ( "Refusing FIN: Rx not complete %ld (cur %lu high %lu)\n",
lDistance, pxTCPWindow->rx.ulCurrentSequenceNumber - pxTCPWindow->rx.ulFirstSequenceNumber,
pxTCPWindow->rx.ulHighestSequenceNumber - pxTCPWindow->rx.ulFirstSequenceNumber ) );
xMayClose = pdFALSE;
}
}
}
if( xTCPWindowLoggingLevel > 0 )
{
FreeRTOS_debug_printf( ( "TCP: FIN received, mayClose = %ld (Rx %lu Len %ld, Tx %lu)\n",
xMayClose, ulSequenceNumber - pxSocket->u.xTCP.xTCPWindow.rx.ulFirstSequenceNumber, ulReceiveLength,
pxTCPWindow->tx.ulCurrentSequenceNumber - pxSocket->u.xTCP.xTCPWindow.tx.ulFirstSequenceNumber ) );
}
if( xMayClose != pdFALSE )
{
pxSocket->u.xTCP.bits.bFinAccepted = pdTRUE_UNSIGNED;
xSendLength = prvTCPHandleFin( pxSocket, *ppxNetworkBuffer );
}
}
if( xMayClose == pdFALSE )
{
pxTCPHeader->ucTCPFlags = ipTCP_FLAG_ACK;
if( ulReceiveLength != 0u )
{
xSendLength = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength );
/* TCP-offsett equals '( ( length / 4 ) << 4 )', resulting in a shift-left 2 */
pxTCPHeader->ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
if( pxSocket->u.xTCP.bits.bFinSent != pdFALSE_UNSIGNED )
{
pxTCPWindow->tx.ulCurrentSequenceNumber = pxTCPWindow->tx.ulFINSequenceNumber;
}
}
/* Now get data to be transmitted. */
/* _HT_ patch: since the MTU has be fixed at 1500 in stead of 1526, TCP
can not send-out both TCP options and also a full packet. Sending
options (SACK) is always more urgent than sending data, which can be
sent later. */
if( uxOptionsLength == 0u )
{
/* prvTCPPrepareSend might allocate a bigger network buffer, if
necessary. */
lSendResult = prvTCPPrepareSend( pxSocket, ppxNetworkBuffer, uxOptionsLength );
if( lSendResult > 0 )
{
xSendLength = ( BaseType_t ) lSendResult;
}
}
}
return xSendLength;
}
/*-----------------------------------------------------------*/
/*
* Called from prvTCPHandleState(). There is data to be sent. If
* ipconfigUSE_TCP_WIN is defined, and if only an ACK must be sent, it will be
* checked if it would better be postponed for efficiency.
*/
static BaseType_t prvSendData( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer,
uint32_t ulReceiveLength, BaseType_t xSendLength )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( (*ppxNetworkBuffer)->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &pxTCPPacket->xTCPHeader;
TCPWindow_t *pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
/* Find out what window size we may advertised. */
uint32_t ulFrontSpace;
int32_t lRxSpace;
#if( ipconfigUSE_TCP_WIN == 1 )
#if( ipconfigTCP_ACK_EARLIER_PACKET == 0 )
const int32_t lMinLength = 0;
#else
int32_t lMinLength;
#endif
#endif
pxSocket->u.xTCP.ulRxCurWinSize = pxTCPWindow->xSize.ulRxWindowLength -
( pxTCPWindow->rx.ulHighestSequenceNumber - pxTCPWindow->rx.ulCurrentSequenceNumber );
/* Free space in rxStream. */
if( pxSocket->u.xTCP.rxStream != NULL )
{
ulFrontSpace = ( uint32_t ) uxStreamBufferFrontSpace( pxSocket->u.xTCP.rxStream );
}
else
{
ulFrontSpace = ( uint32_t ) pxSocket->u.xTCP.uxRxStreamSize;
}
pxSocket->u.xTCP.ulRxCurWinSize = FreeRTOS_min_uint32( ulFrontSpace, pxSocket->u.xTCP.ulRxCurWinSize );
/* Set the time-out field, so that we'll be called by the IP-task in case no
next message will be received. */
lRxSpace = (int32_t)( pxSocket->u.xTCP.ulHighestRxAllowed - pxTCPWindow->rx.ulCurrentSequenceNumber );
#if ipconfigUSE_TCP_WIN == 1
{
#if( ipconfigTCP_ACK_EARLIER_PACKET != 0 )
{
lMinLength = ( ( int32_t ) 2 ) * ( ( int32_t ) pxSocket->u.xTCP.usCurMSS );
}
#endif /* ipconfigTCP_ACK_EARLIER_PACKET */
/* In case we're receiving data continuously, we might postpone sending
an ACK to gain performance. */
if( ( ulReceiveLength > 0 ) && /* Data was sent to this socket. */
( lRxSpace >= lMinLength ) && /* There is Rx space for more data. */
( pxSocket->u.xTCP.bits.bFinSent == pdFALSE_UNSIGNED ) && /* Not in a closure phase. */
( xSendLength == ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) ) && /* No Tx data or options to be sent. */
( pxSocket->u.xTCP.ucTCPState == eESTABLISHED ) && /* Connection established. */
( pxTCPHeader->ucTCPFlags == ipTCP_FLAG_ACK ) ) /* There are no other flags than an ACK. */
{
if( pxSocket->u.xTCP.pxAckMessage != *ppxNetworkBuffer )
{
/* There was still a delayed in queue, delete it. */
if( pxSocket->u.xTCP.pxAckMessage != 0 )
{
vReleaseNetworkBufferAndDescriptor( pxSocket->u.xTCP.pxAckMessage );
}
pxSocket->u.xTCP.pxAckMessage = *ppxNetworkBuffer;
}
if( ( ulReceiveLength < ( uint32_t ) pxSocket->u.xTCP.usCurMSS ) || /* Received a small message. */
( lRxSpace < ( int32_t ) ( 2U * pxSocket->u.xTCP.usCurMSS ) ) ) /* There are less than 2 x MSS space in the Rx buffer. */
{
pxSocket->u.xTCP.usTimeout = ( uint16_t ) pdMS_TO_MIN_TICKS( DELAYED_ACK_SHORT_DELAY_MS );
}
else
{
/* Normally a delayed ACK should wait 200 ms for a next incoming
packet. Only wait 20 ms here to gain performance. A slow ACK
for full-size message. */
pxSocket->u.xTCP.usTimeout = ( uint16_t ) pdMS_TO_MIN_TICKS( DELAYED_ACK_LONGER_DELAY_MS );
}
if( ( xTCPWindowLoggingLevel > 1 ) && ( ipconfigTCP_MAY_LOG_PORT( pxSocket->usLocalPort ) != pdFALSE ) )
{
FreeRTOS_debug_printf( ( "Send[%u->%u] del ACK %lu SEQ %lu (len %lu) tmout %u d %lu\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usRemotePort,
pxTCPWindow->rx.ulCurrentSequenceNumber - pxTCPWindow->rx.ulFirstSequenceNumber,
pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber,
xSendLength,
pxSocket->u.xTCP.usTimeout, lRxSpace ) );
}
*ppxNetworkBuffer = NULL;
xSendLength = 0;
}
else if( pxSocket->u.xTCP.pxAckMessage != NULL )
{
/* As an ACK is not being delayed, remove any earlier delayed ACK
message. */
if( pxSocket->u.xTCP.pxAckMessage != *ppxNetworkBuffer )
{
vReleaseNetworkBufferAndDescriptor( pxSocket->u.xTCP.pxAckMessage );
}
pxSocket->u.xTCP.pxAckMessage = NULL;
}
}
#else
{
/* Remove compiler warnings. */
( void ) ulReceiveLength;
( void ) pxTCPHeader;
( void ) lRxSpace;
}
#endif /* ipconfigUSE_TCP_WIN */
if( xSendLength != 0 )
{
if( ( xTCPWindowLoggingLevel > 1 ) && ( ipconfigTCP_MAY_LOG_PORT( pxSocket->usLocalPort ) != pdFALSE ) )
{
FreeRTOS_debug_printf( ( "Send[%u->%u] imm ACK %lu SEQ %lu (len %lu)\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usRemotePort,
pxTCPWindow->rx.ulCurrentSequenceNumber - pxTCPWindow->rx.ulFirstSequenceNumber,
pxTCPWindow->ulOurSequenceNumber - pxTCPWindow->tx.ulFirstSequenceNumber,
xSendLength ) );
}
/* Set the parameter 'xReleaseAfterSend' to the value of
ipconfigZERO_COPY_TX_DRIVER. */
prvTCPReturnPacket( pxSocket, *ppxNetworkBuffer, ( uint32_t ) xSendLength, ipconfigZERO_COPY_TX_DRIVER );
#if( ipconfigZERO_COPY_TX_DRIVER != 0 )
{
/* The driver has taken ownership of the Network Buffer. */
*ppxNetworkBuffer = NULL;
}
#endif
}
return xSendLength;
}
/*-----------------------------------------------------------*/
/*
* prvTCPHandleState()
* is the most important function of this TCP stack
* We've tried to keep it (relatively short) by putting a lot of code in
* the static functions above:
*
* prvCheckRxData()
* prvStoreRxData()
* prvSetOptions()
* prvHandleSynReceived()
* prvHandleEstablished()
* prvSendData()
*
* As these functions are declared static, and they're called from one location
* only, most compilers will inline them, thus avoiding a call and return.
*/
static BaseType_t prvTCPHandleState( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t **ppxNetworkBuffer )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( (*ppxNetworkBuffer)->pucEthernetBuffer );
TCPHeader_t *pxTCPHeader = &( pxTCPPacket->xTCPHeader );
BaseType_t xSendLength = 0;
uint32_t ulReceiveLength; /* Number of bytes contained in the TCP message. */
uint8_t *pucRecvData;
uint32_t ulSequenceNumber = FreeRTOS_ntohl (pxTCPHeader->ulSequenceNumber);
/* uxOptionsLength: the size of the options to be sent (always a multiple of
4 bytes)
1. in the SYN phase, we shall communicate the MSS
2. in case of a SACK, Selective ACK, ack a segment which comes in
out-of-order. */
UBaseType_t uxOptionsLength = 0u;
uint8_t ucTCPFlags = pxTCPHeader->ucTCPFlags;
TCPWindow_t *pxTCPWindow = &( pxSocket->u.xTCP.xTCPWindow );
/* First get the length and the position of the received data, if any.
pucRecvData will point to the first byte of the TCP payload. */
ulReceiveLength = ( uint32_t ) prvCheckRxData( *ppxNetworkBuffer, &pucRecvData );
if( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED )
{
if ( pxTCPWindow->rx.ulCurrentSequenceNumber == ulSequenceNumber + 1u )
{
/* This is most probably a keep-alive message from peer. Setting
'bWinChange' doesn't cause a window-size-change, the flag is used
here to force sending an immediate ACK. */
pxSocket->u.xTCP.bits.bWinChange = pdTRUE_UNSIGNED;
}
}
/* Keep track of the highest sequence number that might be expected within
this connection. */
if( ( ( int32_t ) ( ulSequenceNumber + ulReceiveLength - pxTCPWindow->rx.ulHighestSequenceNumber ) ) > 0 )
{
pxTCPWindow->rx.ulHighestSequenceNumber = ulSequenceNumber + ulReceiveLength;
}
/* Storing data may result in a fatal error if malloc() fails. */
if( prvStoreRxData( pxSocket, pucRecvData, *ppxNetworkBuffer, ulReceiveLength ) < 0 )
{
xSendLength = -1;
}
else
{
uxOptionsLength = prvSetOptions( pxSocket, *ppxNetworkBuffer );
if( ( pxSocket->u.xTCP.ucTCPState == eSYN_RECEIVED ) && ( ( ucTCPFlags & ipTCP_FLAG_CTRL ) == ipTCP_FLAG_SYN ) )
{
FreeRTOS_debug_printf( ( "eSYN_RECEIVED: ACK expected, not SYN: peer missed our SYN+ACK\n" ) );
/* In eSYN_RECEIVED a simple ACK is expected, but apparently the
'SYN+ACK' didn't arrive. Step back to the previous state in which
a first incoming SYN is handled. The SYN was counted already so
decrease it first. */
vTCPStateChange( pxSocket, eSYN_FIRST );
}
if( ( ( ucTCPFlags & ipTCP_FLAG_FIN ) != 0u ) && ( pxSocket->u.xTCP.bits.bFinRecv == pdFALSE_UNSIGNED ) )
{
/* It's the first time a FIN has been received, remember its
sequence number. */
pxTCPWindow->rx.ulFINSequenceNumber = ulSequenceNumber + ulReceiveLength;
pxSocket->u.xTCP.bits.bFinRecv = pdTRUE_UNSIGNED;
/* Was peer the first one to send a FIN? */
if( pxSocket->u.xTCP.bits.bFinSent == pdFALSE_UNSIGNED )
{
/* If so, don't send the-last-ACK. */
pxSocket->u.xTCP.bits.bFinLast = pdTRUE_UNSIGNED;
}
}
switch (pxSocket->u.xTCP.ucTCPState)
{
case eCLOSED: /* (server + client) no connection state at all. */
/* Nothing to do for a closed socket, except waiting for the
owner. */
break;
case eTCP_LISTEN: /* (server) waiting for a connection request from
any remote TCP and port. */
/* The listen state was handled in xProcessReceivedTCPPacket().
Should not come here. */
break;
case eSYN_FIRST: /* (server) Just received a SYN request for a server
socket. */
{
/* A new socket has been created, reply with a SYN+ACK.
Acknowledge with seq+1 because the SYN is seen as pseudo data
with len = 1. */
uxOptionsLength = prvSetSynAckOptions( pxSocket, pxTCPPacket );
pxTCPHeader->ucTCPFlags = ipTCP_FLAG_SYN | ipTCP_FLAG_ACK;
xSendLength = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + uxOptionsLength );
/* Set the TCP offset field: ipSIZE_OF_TCP_HEADER equals 20 and
uxOptionsLength is a multiple of 4. The complete expression is:
ucTCPOffset = ( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) / 4 ) << 4 */
pxTCPHeader->ucTCPOffset = ( uint8_t )( ( ipSIZE_OF_TCP_HEADER + uxOptionsLength ) << 2 );
vTCPStateChange( pxSocket, eSYN_RECEIVED );
pxTCPWindow->rx.ulCurrentSequenceNumber = pxTCPWindow->rx.ulHighestSequenceNumber = ulSequenceNumber + 1u;
pxTCPWindow->tx.ulCurrentSequenceNumber = pxTCPWindow->ulNextTxSequenceNumber = pxTCPWindow->tx.ulFirstSequenceNumber + 1u; /* because we send a TCP_SYN. */
}
break;
case eCONNECT_SYN: /* (client) also called SYN_SENT: we've just send a
SYN, expect a SYN+ACK and send a ACK now. */
/* Fall through */
case eSYN_RECEIVED: /* (server) we've had a SYN, replied with SYN+SCK
expect a ACK and do nothing. */
xSendLength = prvHandleSynReceived( pxSocket, ppxNetworkBuffer, ulReceiveLength, uxOptionsLength );
break;
case eESTABLISHED: /* (server + client) an open connection, data
received can be delivered to the user. The normal
state for the data transfer phase of the connection
The closing states are also handled here with the
use of some flags. */
xSendLength = prvHandleEstablished( pxSocket, ppxNetworkBuffer, ulReceiveLength, uxOptionsLength );
break;
case eLAST_ACK: /* (server + client) waiting for an acknowledgement
of the connection termination request previously
sent to the remote TCP (which includes an
acknowledgement of its connection termination
request). */
/* Fall through */
case eFIN_WAIT_1: /* (server + client) waiting for a connection termination request from the remote TCP,
* or an acknowledgement of the connection termination request previously sent. */
/* Fall through */
case eFIN_WAIT_2: /* (server + client) waiting for a connection termination request from the remote TCP. */
xSendLength = prvTCPHandleFin( pxSocket, *ppxNetworkBuffer );
break;
case eCLOSE_WAIT: /* (server + client) waiting for a connection
termination request from the local user. Nothing to
do, connection is closed, wait for owner to close
this socket. */
break;
case eCLOSING: /* (server + client) waiting for a connection
termination request acknowledgement from the remote
TCP. */
break;
case eTIME_WAIT: /* (either server or client) waiting for enough time
to pass to be sure the remote TCP received the
acknowledgement of its connection termination
request. [According to RFC 793 a connection can stay
in TIME-WAIT for a maximum of four minutes known as
a MSL (maximum segment lifetime).] These states are
implemented implicitly by settings flags like
'bFinSent', 'bFinRecv', and 'bFinAcked'. */
break;
default:
break;
}
}
if( xSendLength > 0 )
{
xSendLength = prvSendData( pxSocket, ppxNetworkBuffer, ulReceiveLength, xSendLength );
}
return xSendLength;
}
/*-----------------------------------------------------------*/
static BaseType_t prvTCPSendReset( NetworkBufferDescriptor_t *pxNetworkBuffer )
{
#if( ipconfigIGNORE_UNKNOWN_PACKETS == 0 )
{
TCPPacket_t *pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
const BaseType_t xSendLength = ( BaseType_t ) ( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER + 0u ); /* Plus 0 options. */
pxTCPPacket->xTCPHeader.ucTCPFlags = ipTCP_FLAG_ACK | ipTCP_FLAG_RST;
pxTCPPacket->xTCPHeader.ucTCPOffset = ( ipSIZE_OF_TCP_HEADER + 0u ) << 2;
prvTCPReturnPacket( NULL, pxNetworkBuffer, ( uint32_t ) xSendLength, pdFALSE );
}
#endif /* !ipconfigIGNORE_UNKNOWN_PACKETS */
/* Remove compiler warnings if ipconfigIGNORE_UNKNOWN_PACKETS == 1. */
( void ) pxNetworkBuffer;
/* The packet was not consumed. */
return pdFAIL;
}
/*-----------------------------------------------------------*/
static void prvSocketSetMSS( FreeRTOS_Socket_t *pxSocket )
{
uint32_t ulMSS = ipconfigTCP_MSS;
if( ( ( FreeRTOS_ntohl( pxSocket->u.xTCP.ulRemoteIP ) ^ *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) != 0ul )
{
/* Data for this peer will pass through a router, and maybe through
the internet. Limit the MSS to 1400 bytes or less. */
ulMSS = FreeRTOS_min_uint32( ( uint32_t ) REDUCED_MSS_THROUGH_INTERNET, ulMSS );
}
FreeRTOS_debug_printf( ( "prvSocketSetMSS: %lu bytes for %lxip:%u\n", ulMSS, pxSocket->u.xTCP.ulRemoteIP, pxSocket->u.xTCP.usRemotePort ) );
pxSocket->u.xTCP.usInitMSS = pxSocket->u.xTCP.usCurMSS = ( uint16_t ) ulMSS;
}
/*-----------------------------------------------------------*/
/*
* FreeRTOS_TCP_IP has only 2 public functions, this is the second one:
* xProcessReceivedTCPPacket()
* prvTCPHandleState()
* prvTCPPrepareSend()
* prvTCPReturnPacket()
* xNetworkInterfaceOutput() // Sends data to the NIC
* prvTCPSendRepeated()
* prvTCPReturnPacket() // Prepare for returning
* xNetworkInterfaceOutput() // Sends data to the NIC
*/
BaseType_t xProcessReceivedTCPPacket( NetworkBufferDescriptor_t *pxNetworkBuffer )
{
FreeRTOS_Socket_t *pxSocket;
TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
uint16_t ucTCPFlags;
uint32_t ulLocalIP;
uint16_t xLocalPort;
uint32_t ulRemoteIP;
uint16_t xRemotePort;
BaseType_t xResult = pdPASS;
/* Check for a minimum packet size. */
if( pxNetworkBuffer->xDataLength >= ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ) )
{
ucTCPFlags = pxTCPPacket->xTCPHeader.ucTCPFlags;
ulLocalIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulDestinationIPAddress );
xLocalPort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usDestinationPort );
ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress );
xRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort );
/* Find the destination socket, and if not found: return a socket listing to
the destination PORT. */
pxSocket = ( FreeRTOS_Socket_t * )pxTCPSocketLookup( ulLocalIP, xLocalPort, ulRemoteIP, xRemotePort );
}
else
{
return pdFAIL;
}
if( ( pxSocket == NULL ) || ( prvTCPSocketIsActive( ( UBaseType_t ) pxSocket->u.xTCP.ucTCPState ) == pdFALSE ) )
{
/* A TCP messages is received but either there is no socket with the
given port number or the there is a socket, but it is in one of these
non-active states: eCLOSED, eCLOSE_WAIT, eFIN_WAIT_2, eCLOSING, or
eTIME_WAIT. */
FreeRTOS_debug_printf( ( "TCP: No active socket on port %d (%lxip:%d)\n", xLocalPort, ulRemoteIP, xRemotePort ) );
/* Send a RST to all packets that can not be handled. As a result
the other party will get a ECONN error. There are two exceptions:
1) A packet that already has the RST flag set.
2) A packet that only has the ACK flag set.
A packet with only the ACK flag set might be the last ACK in
a three-way hand-shake that closes a connection. */
if( ( ( ucTCPFlags & ipTCP_FLAG_CTRL ) != ipTCP_FLAG_ACK ) &&
( ( ucTCPFlags & ipTCP_FLAG_RST ) == 0u ) )
{
prvTCPSendReset( pxNetworkBuffer );
}
/* The packet can't be handled. */
xResult = pdFAIL;
}
else
{
pxSocket->u.xTCP.ucRepCount = 0u;
if( pxSocket->u.xTCP.ucTCPState == eTCP_LISTEN )
{
/* The matching socket is in a listening state. Test if the peer
has set the SYN flag. */
if( ( ucTCPFlags & ipTCP_FLAG_CTRL ) != ipTCP_FLAG_SYN )
{
/* What happens: maybe after a reboot, a client doesn't know the
connection had gone. Send a RST in order to get a new connect
request. */
#if( ipconfigHAS_DEBUG_PRINTF == 1 )
{
FreeRTOS_debug_printf( ( "TCP: Server can't handle flags: %s from %lxip:%u to port %u\n",
prvTCPFlagMeaning( ( UBaseType_t ) ucTCPFlags ), ulRemoteIP, xRemotePort, xLocalPort ) );
}
#endif /* ipconfigHAS_DEBUG_PRINTF */
if( ( ucTCPFlags & ipTCP_FLAG_RST ) == 0u )
{
prvTCPSendReset( pxNetworkBuffer );
}
xResult = pdFAIL;
}
else
{
/* prvHandleListen() will either return a newly created socket
(if bReuseSocket is false), otherwise it returns the current
socket which will later get connected. */
pxSocket = prvHandleListen( pxSocket, pxNetworkBuffer );
if( pxSocket == NULL )
{
xResult = pdFAIL;
}
}
} /* if( pxSocket->u.xTCP.ucTCPState == eTCP_LISTEN ). */
else
{
/* This is not a socket in listening mode. Check for the RST
flag. */
if( ( ucTCPFlags & ipTCP_FLAG_RST ) != 0u )
{
/* The target socket is not in a listening state, any RST packet
will cause the socket to be closed. */
FreeRTOS_debug_printf( ( "TCP: RST received from %lxip:%u for %u\n", ulRemoteIP, xRemotePort, xLocalPort ) );
/* _HT_: should indicate that 'ECONNRESET' must be returned to the used during next API. */
vTCPStateChange( pxSocket, eCLOSED );
/* The packet cannot be handled. */
xResult = pdFAIL;
}
else if( ( ( ucTCPFlags & ipTCP_FLAG_CTRL ) == ipTCP_FLAG_SYN ) && ( pxSocket->u.xTCP.ucTCPState >= eESTABLISHED ) )
{
/* SYN flag while this socket is already connected. */
FreeRTOS_debug_printf( ( "TCP: SYN unexpected from %lxip:%u\n", ulRemoteIP, xRemotePort ) );
/* The packet cannot be handled. */
xResult = pdFAIL;
}
else
{
/* Update the copy of the TCP header only (skipping eth and IP
headers). It might be used later on, whenever data must be sent
to the peer. */
const BaseType_t lOffset = ( BaseType_t ) ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER );
memcpy( pxSocket->u.xTCP.xPacket.u.ucLastPacket + lOffset, pxNetworkBuffer->pucEthernetBuffer + lOffset, ipSIZE_OF_TCP_HEADER );
}
}
}
if( xResult != pdFAIL )
{
/* Touch the alive timers because we received a message for this
socket. */
prvTCPTouchSocket( pxSocket );
/* Parse the TCP option(s), if present. */
/* _HT_ : if we're in the SYN phase, and peer does not send a MSS option,
then we MUST assume an MSS size of 536 bytes for backward compatibility. */
/* When there are no TCP options, the TCP offset equals 20 bytes, which is stored as
the number 5 (words) in the higher niblle of the TCP-offset byte. */
if( ( pxTCPPacket->xTCPHeader.ucTCPOffset & TCP_OFFSET_LENGTH_BITS ) > TCP_OFFSET_STANDARD_LENGTH )
{
prvCheckOptions( pxSocket, pxNetworkBuffer );
}
#if( ipconfigUSE_TCP_WIN == 1 )
{
pxSocket->u.xTCP.ulWindowSize = FreeRTOS_ntohs( pxTCPPacket->xTCPHeader.usWindow );
pxSocket->u.xTCP.ulWindowSize =
( pxSocket->u.xTCP.ulWindowSize << pxSocket->u.xTCP.ucPeerWinScaleFactor );
}
#endif
/* In prvTCPHandleState() the incoming messages will be handled
depending on the current state of the connection. */
if( prvTCPHandleState( pxSocket, &pxNetworkBuffer ) > 0 )
{
/* prvTCPHandleState() has sent a message, see if there are more to
be transmitted. */
#if( ipconfigUSE_TCP_WIN == 1 )
{
prvTCPSendRepeated( pxSocket, &pxNetworkBuffer );
}
#endif /* ipconfigUSE_TCP_WIN */
}
if( pxNetworkBuffer != NULL )
{
/* We must check if the buffer is unequal to NULL, because the
socket might keep a reference to it in case a delayed ACK must be
sent. */
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
pxNetworkBuffer = NULL;
}
/* And finally, calculate when this socket wants to be woken up. */
prvTCPNextTimeout ( pxSocket );
/* Return pdPASS to tell that the network buffer is 'consumed'. */
xResult = pdPASS;
}
/* pdPASS being returned means the buffer has been consumed. */
return xResult;
}
/*-----------------------------------------------------------*/
static FreeRTOS_Socket_t *prvHandleListen( FreeRTOS_Socket_t *pxSocket, NetworkBufferDescriptor_t *pxNetworkBuffer )
{
TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );
FreeRTOS_Socket_t *pxReturn = NULL;
uint32_t ulInitialSequenceNumber;
/* Assume that a new Initial Sequence Number will be required. Request
it now in order to fail out if necessary. */
ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( *ipLOCAL_IP_ADDRESS_POINTER,
pxSocket->usLocalPort,
pxTCPPacket->xIPHeader.ulSourceIPAddress,
pxTCPPacket->xTCPHeader.usSourcePort );
/* A pure SYN (without ACK) has come in, create a new socket to answer
it. */
if( 0 != ulInitialSequenceNumber )
{
if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED )
{
/* The flag bReuseSocket indicates that the same instance of the
listening socket should be used for the connection. */
pxReturn = pxSocket;
pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;
pxSocket->u.xTCP.pxPeerSocket = pxSocket;
}
else
{
/* The socket does not have the bReuseSocket flag set meaning create a
new socket when a connection comes in. */
pxReturn = NULL;
if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog )
{
FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usChildCount,
pxSocket->u.xTCP.usBacklog,
pxSocket->u.xTCP.usChildCount == 1 ? "" : "ren" ) );
prvTCPSendReset( pxNetworkBuffer );
}
else
{
FreeRTOS_Socket_t *pxNewSocket = ( FreeRTOS_Socket_t * )
FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) )
{
FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) );
prvTCPSendReset( pxNetworkBuffer );
}
else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE )
{
/* The socket will be connected immediately, no time for the
owner to setsockopt's, therefore copy properties of the server
socket to the new socket. Only the binding might fail (due to
lack of resources). */
pxReturn = pxNewSocket;
}
}
}
}
if( ( 0 != ulInitialSequenceNumber ) && ( pxReturn != NULL ) )
{
pxReturn->u.xTCP.usRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort );
pxReturn->u.xTCP.ulRemoteIP = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress );
pxReturn->u.xTCP.xTCPWindow.ulOurSequenceNumber = ulInitialSequenceNumber;
/* Here is the SYN action. */
pxReturn->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber = FreeRTOS_ntohl( pxTCPPacket->xTCPHeader.ulSequenceNumber );
prvSocketSetMSS( pxReturn );
prvTCPCreateWindow( pxReturn );
vTCPStateChange( pxReturn, eSYN_FIRST );
/* Make a copy of the header up to the TCP header. It is needed later
on, whenever data must be sent to the peer. */
memcpy( pxReturn->u.xTCP.xPacket.u.ucLastPacket, pxNetworkBuffer->pucEthernetBuffer, sizeof( pxReturn->u.xTCP.xPacket.u.ucLastPacket ) );
}
return pxReturn;
}
/*-----------------------------------------------------------*/
/*
* Duplicates a socket after a listening socket receives a connection.
*/
static BaseType_t prvTCPSocketCopy( FreeRTOS_Socket_t *pxNewSocket, FreeRTOS_Socket_t *pxSocket )
{
struct freertos_sockaddr xAddress;
pxNewSocket->xReceiveBlockTime = pxSocket->xReceiveBlockTime;
pxNewSocket->xSendBlockTime = pxSocket->xSendBlockTime;
pxNewSocket->ucSocketOptions = pxSocket->ucSocketOptions;
pxNewSocket->u.xTCP.uxRxStreamSize = pxSocket->u.xTCP.uxRxStreamSize;
pxNewSocket->u.xTCP.uxTxStreamSize = pxSocket->u.xTCP.uxTxStreamSize;
pxNewSocket->u.xTCP.uxLittleSpace = pxSocket->u.xTCP.uxLittleSpace;
pxNewSocket->u.xTCP.uxEnoughSpace = pxSocket->u.xTCP.uxEnoughSpace;
pxNewSocket->u.xTCP.uxRxWinSize = pxSocket->u.xTCP.uxRxWinSize;
pxNewSocket->u.xTCP.uxTxWinSize = pxSocket->u.xTCP.uxTxWinSize;
#if( ipconfigSOCKET_HAS_USER_SEMAPHORE == 1 )
{
pxNewSocket->pxUserSemaphore = pxSocket->pxUserSemaphore;
}
#endif /* ipconfigSOCKET_HAS_USER_SEMAPHORE */
#if( ipconfigUSE_CALLBACKS == 1 )
{
/* In case call-backs are used, copy them from parent to child. */
pxNewSocket->u.xTCP.pxHandleConnected = pxSocket->u.xTCP.pxHandleConnected;
pxNewSocket->u.xTCP.pxHandleReceive = pxSocket->u.xTCP.pxHandleReceive;
pxNewSocket->u.xTCP.pxHandleSent = pxSocket->u.xTCP.pxHandleSent;
}
#endif /* ipconfigUSE_CALLBACKS */
#if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
/* Child socket of listening sockets will inherit the Socket Set
Otherwise the owner has no chance of including it into the set. */
if( pxSocket->pxSocketSet )
{
pxNewSocket->pxSocketSet = pxSocket->pxSocketSet;
pxNewSocket->xSelectBits = pxSocket->xSelectBits | eSELECT_READ | eSELECT_EXCEPT;
}
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION */
/* And bind it to the same local port as its parent. */
xAddress.sin_addr = *ipLOCAL_IP_ADDRESS_POINTER;
xAddress.sin_port = FreeRTOS_htons( pxSocket->usLocalPort );
#if( ipconfigTCP_HANG_PROTECTION == 1 )
{
/* Only when there is anti-hanging protection, a socket may become an
orphan temporarily. Once this socket is really connected, the owner of
the server socket will be notified. */
/* When bPassQueued is true, the socket is an orphan until it gets
connected. */
pxNewSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;
pxNewSocket->u.xTCP.pxPeerSocket = pxSocket;
}
#else
{
/* A reference to the new socket may be stored and the socket is marked
as 'passable'. */
/* When bPassAccept is pdTRUE_UNSIGNED this socket may be returned in a call to
accept(). */
pxNewSocket->u.xTCP.bits.bPassAccept = pdTRUE_UNSIGNED;
if(pxSocket->u.xTCP.pxPeerSocket == NULL )
{
pxSocket->u.xTCP.pxPeerSocket = pxNewSocket;
}
}
#endif
pxSocket->u.xTCP.usChildCount++;
FreeRTOS_debug_printf( ( "Gain: Socket %u now has %u / %u child%s\n",
pxSocket->usLocalPort,
pxSocket->u.xTCP.usChildCount,
pxSocket->u.xTCP.usBacklog,
pxSocket->u.xTCP.usChildCount == 1u ? "" : "ren" ) );
/* Now bind the child socket to the same port as the listening socket. */
if( vSocketBind ( pxNewSocket, &xAddress, sizeof( xAddress ), pdTRUE ) != 0 )
{
FreeRTOS_debug_printf( ( "TCP: Listen: new socket bind error\n" ) );
vSocketClose( pxNewSocket );
return pdFALSE;
}
return pdTRUE;
}
/*-----------------------------------------------------------*/
#if( ( ipconfigHAS_DEBUG_PRINTF != 0 ) || ( ipconfigHAS_PRINTF != 0 ) )
const char *FreeRTOS_GetTCPStateName( UBaseType_t ulState )
{
if( ulState >= ( UBaseType_t ) ARRAY_SIZE( pcStateNames ) )
{
ulState = ( UBaseType_t ) ARRAY_SIZE( pcStateNames ) - 1u;
}
return pcStateNames[ ulState ];
}
#endif /* ( ( ipconfigHAS_DEBUG_PRINTF != 0 ) || ( ipconfigHAS_PRINTF != 0 ) ) */
/*-----------------------------------------------------------*/
/*
* In the API accept(), the user asks is there is a new client? As API's can
* not walk through the xBoundTCPSocketsList the IP-task will do this.
*/
BaseType_t xTCPCheckNewClient( FreeRTOS_Socket_t *pxSocket )
{
TickType_t xLocalPort = FreeRTOS_htons( pxSocket->usLocalPort );
ListItem_t *pxIterator;
FreeRTOS_Socket_t *pxFound;
BaseType_t xResult = pdFALSE;
/* Here xBoundTCPSocketsList can be accessed safely IP-task is the only one
who has access. */
for( pxIterator = ( ListItem_t * ) listGET_HEAD_ENTRY( &xBoundTCPSocketsList );
pxIterator != ( ListItem_t * ) listGET_END_MARKER( &xBoundTCPSocketsList );
pxIterator = ( ListItem_t * ) listGET_NEXT( pxIterator ) )
{
if( listGET_LIST_ITEM_VALUE( pxIterator ) == xLocalPort )
{
pxFound = ( FreeRTOS_Socket_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
if( ( pxFound->ucProtocol == FREERTOS_IPPROTO_TCP ) && ( pxFound->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED ) )
{
pxSocket->u.xTCP.pxPeerSocket = pxFound;
FreeRTOS_debug_printf( ( "xTCPCheckNewClient[0]: client on port %u\n", pxSocket->usLocalPort ) );
xResult = pdTRUE;
break;
}
}
}
return xResult;
}
/*-----------------------------------------------------------*/
#endif /* ipconfigUSE_TCP == 1 */
/* Provide access to private members for testing. */
#ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS
#include "aws_freertos_tcp_test_access_tcp_define.h"
#endif